summaryrefslogtreecommitdiffstats
path: root/hw/fsp/fsp-mdst-table.c
blob: b220fbe468f22981792fb762b820e6fc7bb28a3e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* Copyright 2013-2014 IBM Corp.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/*
 * Sapphire dump design:
 *   - During initialization we setup Memory Dump Source Table (MDST) table
 *     which contains address, size pair.
 *   - We send MDST table update notification to FSP via MBOX command.
 *   - During Sapphire checkstop:
 *     - FSP retrieves HWDUMP.
 *     - FSP retrieves CEC memory based on MDST table.
 *   - Once Sapphire reboot FSP sends new dump avialable notification via HDAT
 */

#include <fsp.h>
#include <psi.h>
#include <opal.h>
#include <lock.h>
#include <skiboot.h>
#include <errorlog.h>
#include <fsp-mdst-table.h>

/*
 * Sapphire dump size
 *   This is the maximum memory that FSP can retrieve during checkstop.
 *
 * Note:
 *   Presently we are hardcoding this parameter. Eventually we need
 *   new System parameter so that we can get max size dynamically.
 */
#define MAX_SAPPHIRE_DUMP_SIZE	0x1000000

DEFINE_LOG_ENTRY(OPAL_RC_DUMP_MDST_INIT, OPAL_PLATFORM_ERR_EVT, OPAL_DUMP,
		 OPAL_PLATFORM_FIRMWARE, OPAL_PREDICTIVE_ERR_FAULT_RECTIFY_REBOOT,
		 OPAL_NA, NULL);

DEFINE_LOG_ENTRY(OPAL_RC_DUMP_MDST_UPDATE, OPAL_PLATFORM_ERR_EVT, OPAL_DUMP,
		 OPAL_PLATFORM_FIRMWARE,
		 OPAL_PREDICTIVE_ERR_FAULT_RECTIFY_REBOOT,
		 OPAL_NA, NULL);

DEFINE_LOG_ENTRY(OPAL_RC_DUMP_MDST_ADD, OPAL_PLATFORM_ERR_EVT, OPAL_DUMP,
		 OPAL_PLATFORM_FIRMWARE, OPAL_INFO, OPAL_NA, NULL);

DEFINE_LOG_ENTRY(OPAL_RC_DUMP_MDST_REMOVE, OPAL_PLATFORM_ERR_EVT, OPAL_DUMP,
		 OPAL_PLATFORM_FIRMWARE, OPAL_INFO, OPAL_NA, NULL);


static struct dump_mdst_table *mdst_table;
static struct dump_mdst_table *dump_mem_region;

static int cur_mdst_entry;
static int max_mdst_entry;
static int cur_dump_size;
/*
 * Presently both sizes are same.. But if someday FSP gives more space
 * than our TCE mapping then we need this validation..
 *
 * Also once FSP implements MAX_SAPPHIRE_DUMP_SIZE system param, we can
 * move this validation to separate function.
 */
static int max_dump_size = MIN(MAX_SAPPHIRE_DUMP_SIZE, PSI_DMA_HYP_DUMP_SIZE);

/* Protect MDST table entries */
static struct lock mdst_lock = LOCK_UNLOCKED;

/* Not supported on P7 */
static inline bool fsp_mdst_supported(void)
{
	return proc_gen >= proc_gen_p8;
}

static inline uint32_t get_dump_region_map_size(uint64_t addr, uint32_t size)
{
	uint64_t start, end;

	start = addr & ~TCE_MASK;
	end = addr + size;
	end = (end + (TCE_MASK - 1)) & ~TCE_MASK;

	return (end - start);
}

static int dump_region_tce_map(void)
{
	int i;
	uint32_t t_size = 0, size;
	uint64_t addr;

	for (i = 0; i < cur_mdst_entry; i++) {

		addr = dump_mem_region[i].addr & ~TCE_MASK;
		size = get_dump_region_map_size(dump_mem_region[i].addr,
						dump_mem_region[i].size);

		if (t_size + size > max_dump_size)
			break;

		/* TCE mapping */
		fsp_tce_map(PSI_DMA_HYP_DUMP + t_size, (void *)addr, size);

		/* Add entry to MDST table */
		mdst_table[i].type = dump_mem_region[i].type;
		mdst_table[i].size = dump_mem_region[i].size;
		mdst_table[i].addr = PSI_DMA_HYP_DUMP + t_size;

		/* TCE alignment adjustment */
		mdst_table[i].addr += dump_mem_region[i].addr & 0xfff;

		t_size += size;
	}

	return i;
}

static inline void dump_region_tce_unmap(void)
{
	fsp_tce_unmap(PSI_DMA_HYP_DUMP, PSI_DMA_HYP_DUMP_SIZE);
}

static void update_mdst_table_complete(struct fsp_msg *msg)
{
	uint8_t status = (msg->resp->word1 >> 8) & 0xff;

	if (status)
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_UPDATE),
				 "MDST: Update table MBOX command failed: "
				 "0x%x\n", status);
	else
		printf("MDST: Table updated.\n");

	fsp_freemsg(msg);
}

/* Send MDST table to FSP */
static int64_t fsp_update_mdst_table(void)
{
	struct fsp_msg *msg;
	int count;
	int rc = OPAL_SUCCESS;

	if (cur_mdst_entry <= 0) {
		printf("MDST: Table is empty\n");
		return OPAL_INTERNAL_ERROR;
	}

	lock(&mdst_lock);

	/* Unmap previous mapping */
	dump_region_tce_unmap();
	count = dump_region_tce_map();

	msg = fsp_mkmsg(FSP_CMD_HYP_MDST_TABLE, 4, 0,
			PSI_DMA_MDST_TABLE,
			sizeof(*mdst_table) * count,
			sizeof(*mdst_table));
	unlock(&mdst_lock);

	if (!msg) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_UPDATE),
				 "MDST: Message allocation failed.!\n");
		rc = OPAL_INTERNAL_ERROR;
	} else if (fsp_queue_msg(msg, update_mdst_table_complete)) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_UPDATE),
				 "MDST: Failed to queue MDST table message.\n");
		fsp_freemsg(msg);
		rc = OPAL_INTERNAL_ERROR;
	}
	return rc;
}

static int dump_region_del_entry(uint32_t id)
{
	int i;
	uint32_t size;
	bool found = false;
	int rc = OPAL_SUCCESS;

	lock(&mdst_lock);

	for (i = 0; i < cur_mdst_entry; i++) {
		if (dump_mem_region[i].type != id)
			continue;

		found = true;
		break;
	}

	if (!found) {
		rc = OPAL_PARAMETER;
		goto del_out;
	}

	/* Adjust current dump size */
	size = get_dump_region_map_size(dump_mem_region[i].addr,
					dump_mem_region[i].size);
	cur_dump_size -= size;

	for ( ; i < cur_mdst_entry - 1; i++)
		dump_mem_region[i] = dump_mem_region[i + 1];

	dump_mem_region[i].type = 0;
	cur_mdst_entry--;

del_out:
	unlock(&mdst_lock);
	return rc;
}

/* Add entry to MDST table */
static int __dump_region_add_entry(uint32_t id, uint64_t addr, uint32_t size)
{
	int rc = OPAL_INTERNAL_ERROR;
	uint32_t act_size;

	/* Delete function takes lock before modifying table */
	dump_region_del_entry(id);

	lock(&mdst_lock);

	if (cur_mdst_entry >= max_mdst_entry) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_ADD),
				 "MDST: Table is full.\n");
		goto out;
	}

	/* TCE alignment adjustment */
	act_size = get_dump_region_map_size(addr, size);

	/* Make sure we don't cross dump size limit */
	if (cur_dump_size + act_size > max_dump_size) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_ADD),
			 "MDST: 0x%x is crossing max dump size (0x%x) limit.\n",
			 cur_dump_size + act_size, max_dump_size);
		goto out;
	}

	/* Add entry to dump memory region table */
	dump_mem_region[cur_mdst_entry].type = id;
	dump_mem_region[cur_mdst_entry].addr = addr;
	dump_mem_region[cur_mdst_entry].size = size;

	/* Update dump region count and dump size */
	cur_mdst_entry++;
	cur_dump_size += act_size;

	printf("MDST: Addr = 0x%llx [size : 0x%x bytes] added to MDST table.\n",
	       (uint64_t)addr, size);

	rc = OPAL_SUCCESS;

out:
	unlock(&mdst_lock);
	return rc;
}

static int dump_region_add_entries(void)
{
	int rc;

	/* Add console buffer */
	rc = __dump_region_add_entry(DUMP_REGION_CONSOLE,
				     INMEM_CON_START, INMEM_CON_LEN);
	if (rc)
		return rc;

	/* Add HBRT buffer */
	rc = __dump_region_add_entry(DUMP_REGION_HBRT_LOG,
				     HBRT_CON_START, HBRT_CON_LEN);

	return rc;
}

static int64_t fsp_opal_register_dump_region(uint32_t id,
					     uint64_t addr, uint64_t size)
{
	int rc = OPAL_SUCCESS;

	if (!fsp_present())
		return rc;

	if (!fsp_mdst_supported()) {
		printf("MDST: Not supported\n");
		return rc;
	}

	/* Validate memory region id */
	if (id < DUMP_REGION_HOST_START || id > DUMP_REGION_HOST_END) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_ADD),
				 "MDST: Invalid dump region id : 0x%x\n", id);
		return OPAL_PARAMETER;
	}

	if (size <= 0) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_ADD),
				 "MDST: Invalid size : 0x%llx\n", size);
		return OPAL_PARAMETER;
	}

	rc = __dump_region_add_entry(id, addr, size);
	if (rc)
		return rc;

	/* Send updated MDST to FSP */
	rc = fsp_update_mdst_table();

	return rc;
}

static int64_t fsp_opal_unregister_dump_region(uint32_t id)
{
	int rc = OPAL_SUCCESS;

	if (!fsp_present())
		return rc;

	if (!fsp_mdst_supported()) {
		printf("MDST: Not supported\n");
		return rc;
	}

	/* Validate memory region id */
	if (id < DUMP_REGION_HOST_START || id > DUMP_REGION_HOST_END) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_REMOVE),
				 "MDST: Invalid dump region id : 0x%x\n", id);
		return OPAL_PARAMETER;
	}

	rc = dump_region_del_entry(id);
	if (rc) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_REMOVE),
				 "MDST: dump region id : 0x%x not found\n", id);
		return OPAL_PARAMETER;
	}

	/* Send updated MDST to FSP */
	rc = fsp_update_mdst_table();

	return rc;
}

/* TCE mapping */
static inline void mdst_table_tce_map(void)
{
	fsp_tce_map(PSI_DMA_MDST_TABLE, mdst_table, PSI_DMA_MDST_TABLE_SIZE);
}

/* Initialize MDST table */
static int mdst_table_init(void)
{
	dump_mem_region = memalign(TCE_PSIZE, PSI_DMA_MDST_TABLE_SIZE);
	if (!dump_mem_region) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_INIT),
			 "MDST: Failed to allocate memory for dump "
			 "memory region table.\n");
		return -ENOMEM;
	}

	memset(dump_mem_region, 0, PSI_DMA_MDST_TABLE_SIZE);

	mdst_table = memalign(TCE_PSIZE, PSI_DMA_MDST_TABLE_SIZE);
	if (!mdst_table) {
		log_simple_error(&e_info(OPAL_RC_DUMP_MDST_INIT),
			 "MDST: Failed to allocate memory for MDST table.\n");
		return -ENOMEM;
	}

	memset(mdst_table, 0, PSI_DMA_MDST_TABLE_SIZE);
	mdst_table_tce_map();

	max_mdst_entry = PSI_DMA_MDST_TABLE_SIZE / sizeof(*mdst_table);
	printf("MDST: Max entries in MDST table : %d\n", max_mdst_entry);

	return OPAL_SUCCESS;
}

/*
 * Handle FSP R/R event.
 */
static bool fsp_mdst_update_rr(uint32_t cmd_sub_mod,
			       struct fsp_msg *msg __unused)
{
	switch (cmd_sub_mod) {
	case FSP_RESET_START:
		return true;
	case FSP_RELOAD_COMPLETE: /* Send MDST to FSP */
		fsp_update_mdst_table();
		return true;
	}
	return false;
}

static struct fsp_client fsp_mdst_client_rr = {
	.message = fsp_mdst_update_rr,
};

/* Initialize MDST table and send notification to FSP */
void fsp_mdst_table_init(void)
{
	if (!fsp_present())
		return;

	/* OPAL interface */
	opal_register(OPAL_REGISTER_DUMP_REGION,
		      fsp_opal_register_dump_region, 3);
	opal_register(OPAL_UNREGISTER_DUMP_REGION,
		      fsp_opal_unregister_dump_region, 1);

	if (!fsp_mdst_supported())
		return;

	/* Initiate MDST */
	if (mdst_table_init() != OPAL_SUCCESS)
		return;

	/*
	 * Ignore return code from mdst_table_add_entries so that
	 * we can atleast capture partial dump.
	 */
	dump_region_add_entries();
	fsp_update_mdst_table();

	/* Register for Class AA (FSP R/R) */
	fsp_register_client(&fsp_mdst_client_rr, FSP_MCLASS_RR_EVENT);
}
OpenPOWER on IntegriCloud