aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/soc/marvell/octeontx2-ghes/otx2-ghes-bert.c
blob: 4d0e84205e2e69d78837589c17632eb7ee3ad8a3 (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
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Supports OcteonTX2 Generic Hardware Error Source (BED)
 * Boot Error Data (BED) from BERT table DT and ACPI
 *
 * Copyright (C) 2021 Marvell.
 */

#include <linux/io.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/platform_device.h>
#include <linux/of_address.h>
#include <linux/arm_sdei.h>
#include <linux/uuid.h>
#include <linux/acpi.h>
#include <acpi/apei.h>
#include <linux/pci.h>
#include <linux/crash_dump.h>
#include "otx2-ghes-bert.h"
#include "otx2-sdei-ghes.h"

#define DRV_NAME	"bed-bert"

#define initerrmsg(fmt, ...) pr_err(DRV_NAME ":" fmt, __VA_ARGS__)
#ifdef CONFIG_OCTEONTX2_SDEI_GHES_DEBUG
#  define initdbgmsg(fmt, ...) pr_info(DRV_NAME ":" fmt, __VA_ARGS__)
#  define dbgmsg(dev, ...) dev_info((dev), __VA_ARGS__)
#else
#  define initdbgmsg(fmt, ...) (void)(fmt)
#  define dbgmsg(dev, ...) (void)(dev)
#endif // CONFIG_OCTEONTX2_SDEI_GHES_DEBUG

#define BERT_TBL_OEM_ID	"OTX2    "
#define BERT_OEM_ID     "MRVL  "

#ifdef CONFIG_OF
static const struct of_device_id bed_bert_of_match[] = {
	{ .compatible = "marvell,bed-bert", },
	{},
};
MODULE_DEVICE_TABLE(of, bed_bert_of_match);
#endif

#ifdef CONFIG_ACPI
static const struct acpi_device_id bed_bert_acpi_match[] = {
	{ "BERT0001", 0 },
	{ },
};
MODULE_DEVICE_TABLE(acpi, bed_bert_acpi_match);
#endif

static int __init ghes_bed_acpi_match_resource(struct platform_device *pdev,
		struct mrvl_bed_source *bsrc)
{
	struct resource *res;

	// BERT
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		initerrmsg("%s ACPI unable get bert block\n", __func__);
		return -ENOENT;
	}
	initdbgmsg("%s Status BERT %s [%llx - %llx, %lx, %lx]\n", __func__,
			res->name, res->start, res->end, res->flags, res->desc);
	bsrc->bert_pa = res->start;
	bsrc->bert_sz = resource_size(res);
	initdbgmsg("BERT RING: 0x%llx/0x%llx\n", bsrc->bert_pa, bsrc->bert_sz);

	// Error Block Ring
	res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
	if (!res) {
		initerrmsg("%s ACPI unable get ring block\n", __func__);
		return -ENOENT;
	}
	initdbgmsg("%s Status Ring %s [%llx - %llx, %lx, %lx]\n", __func__,
			res->name, res->start, res->end, res->flags, res->desc);
	bsrc->block_pa = res->start;
	bsrc->block_sz = resource_size(res);
	initdbgmsg("BERT RING: 0x%llx/0x%llx\n", bsrc->block_pa, bsrc->block_sz);

	return 0;
}

static int __init ghes_bed_of_match_resource(struct mrvl_bed_source *bsrc)
{
	struct device_node *of_node;
	struct device_node *child_node;
	const __be32 *res;
	u64 size;
	u64 base;

	of_node = of_find_matching_node_and_match(NULL, bed_bert_of_match, NULL);
	if (!of_node) {
		initerrmsg("BERT initialization no dev node %p\n", of_node);
		return -ENODEV;
	}

	child_node = of_get_next_available_child(of_node, NULL);
	if (!child_node) {
		initerrmsg("BERT initialization no child node %p\n", child_node);
		return -ENODEV;
	}

	res = of_get_address(child_node, 0, &size, NULL);
	if (!res)
		goto err;

	base = of_translate_address(child_node, res);
	if (base == OF_BAD_ADDR)
		goto err;

	bsrc->bert_pa = (phys_addr_t)base;
	bsrc->bert_sz = (phys_addr_t)size;

	res = of_get_address(child_node, 2, &size, NULL);
	if (!res)
		goto err;

	base = of_translate_address(child_node, res);
	if (base == OF_BAD_ADDR)
		goto err;

	bsrc->block_pa = (phys_addr_t)base;
	bsrc->block_sz = (phys_addr_t)size;

	initdbgmsg("BERT: 0x%llx/0x%llx  0x%llx/0x%llx\n",
			bsrc->bert_pa, bsrc->bert_sz, bsrc->block_pa, bsrc->block_sz);

	return 0;

err:
	initerrmsg("%s BERT unable get/translate address block\n", __func__);
	return -ENODEV;
}

static int __init ghes_bed_map_resource(struct device *dev, struct mrvl_bed_source *bsrc)
{
	if (pfn_valid(PHYS_PFN(bsrc->block_pa))) {
		bsrc->block_va = phys_to_virt(bsrc->block_pa);
	} else {
		if (!devm_request_mem_region(dev, bsrc->block_pa, bsrc->block_sz, "BERT")) {
			initerrmsg("Failure BERT request 0x%llx\n", bsrc->block_pa);
			return -ENODEV;
		}
		bsrc->block_va = devm_ioremap(dev, bsrc->block_pa, bsrc->block_sz);
		if (!bsrc->block_va) {
			initerrmsg("%s Unable to map Boot Error Data\n", __func__);
			return -ENODEV;
		}
	}
	initdbgmsg("%s BERT Ring block VA=0x%llx\n", __func__, (long long)bsrc->block_va);

	if (pfn_valid(PHYS_PFN(bsrc->bert_pa))) {
		bsrc->bert_va = phys_to_virt(bsrc->bert_pa);
	} else {
		if (!devm_request_mem_region(dev, bsrc->bert_pa, bsrc->bert_sz, "BERT")) {
			initerrmsg("Failure BERT request 0x%llx\n", bsrc->bert_pa);
			return -ENODEV;
		}
		bsrc->bert_va = devm_ioremap(dev, bsrc->bert_pa, bsrc->bert_sz);
		if (!bsrc->bert_va) {
			initerrmsg("%s Unable to map Boot Error Data\n", __func__);
			return -ENODEV;
		}
	}
	initdbgmsg("%s BERT Ring block VA=0x%llx\n", __func__, (long long)bsrc->bert_va);


	return 0;
}

static int __init ghes_bed_count_error(struct mrvl_bed_source *bsrc)
{
	struct otx2_ghes_err_ring *ring;
	size_t error_cnt = 0;

	ring = bsrc->block_va;
	ring->reg = OTX2_GHES_ERR_RING_SIG;

	if (!ring->size) {
		initerrmsg("%s BERT support disabled by firmware\n", __func__);
		return 0;
	}

	if (ring->head >= ring->tail)
		error_cnt = ring->head - ring->tail;
	else
		error_cnt = ring->size - (ring->tail - ring->head);

	bsrc->error_cnt = error_cnt;

	initdbgmsg("BED mem @ %llx (%llx PA), %llu B, error entries %ld\n",
		   (long long)bsrc->block_va, bsrc->block_pa,
		   (long long)bsrc->block_sz, error_cnt);

	return error_cnt;
}

static int __init ghes_bed_fetch_errors(struct mrvl_bed_source *bsrc)
{
	struct acpi_table_bert *bert_tbl;
	struct acpi_bert_region *bert_esb;
	struct bed_bert_mem_entry *bert_entries;
	struct otx2_ghes_err_ring *ring;
	struct acpi_hest_generic_data *hest_gen_data;
	struct bed_bert_mem_entry *bert_mem_entry;
	struct acpi_hest_generic_status *estatus;
	struct cper_sec_mem_err_old *mem_err;
	struct otx2_ghes_err_record *err_rec;
	u8 *p;
	u8 sum = 0;
	u32 idx = 0;

	ring = bsrc->block_va;

	bert_tbl = kzalloc(bsrc->bert_sz, GFP_KERNEL);

	if (!bert_tbl) {
		initerrmsg("Unable to allocate BERT data (0x%llx B)\n", bsrc->bert_sz);
		return -ENOMEM;
	}

	bert_esb = (struct acpi_bert_region *)(bert_tbl + 1);

	bert_entries = (struct bed_bert_mem_entry *)bert_esb;

	strcpy(bert_tbl->header.signature, ACPI_SIG_BERT);
	bert_tbl->header.length = sizeof(*bert_tbl);
	bert_tbl->header.revision = 1;
	bert_tbl->header.oem_revision = 1;
	strcpy(bert_tbl->header.oem_id, BERT_OEM_ID);
	strcpy(bert_tbl->header.oem_table_id, BERT_TBL_OEM_ID);
	strcpy(bert_tbl->header.asl_compiler_id, BERT_OEM_ID);
	bert_tbl->header.asl_compiler_revision = 1;

	p = (u8 *)&bert_tbl->header;
	while (p < (u8 *)(&bert_tbl->header + 1))
		sum += *p, p++;
	bert_tbl->header.checksum -= sum;

	bert_tbl->region_length = (bsrc->error_cnt * sizeof(*bert_entries));
	bert_tbl->address = bsrc->bert_pa + ((void *)bert_esb - (void *)bert_tbl);

	initdbgmsg("BERT: 0x%llx -> 0x%llx\n", bsrc->bert_pa, (long long)bsrc->bert_va);

	for (idx = 0; idx < bsrc->error_cnt; idx++) {
		err_rec = &ring->records[ring->tail];

		bert_mem_entry = &bert_entries[idx];

		estatus = &bert_mem_entry->estatus.hest;

		estatus->raw_data_length = 0;
		estatus->raw_data_offset = 0;
		estatus->data_length = sizeof(bert_mem_entry->gen_data);
		estatus->error_severity = err_rec->severity;

		hest_gen_data = &bert_mem_entry->gen_data;

		hest_gen_data->revision = 0x201; /* ACPI 4.x */
		if (err_rec->fru_text[0]) {
			hest_gen_data->validation_bits = ACPI_HEST_GEN_VALID_FRU_STRING;
			strncpy(hest_gen_data->fru_text, err_rec->fru_text,
				sizeof(hest_gen_data->fru_text));
		}

		hest_gen_data->error_severity = estatus->error_severity;
		memcpy((guid_t *)hest_gen_data->section_type,
		       &CPER_SEC_PLATFORM_MEM, sizeof(guid_t));

		hest_gen_data->error_data_length = sizeof(*mem_err);
		estatus->data_length += hest_gen_data->error_data_length;

		mem_err = &bert_mem_entry->mem_err;

		if (pfn_valid(PHYS_PFN(bsrc->block_pa)))
			memcpy(mem_err, &err_rec->u.mcc, sizeof(*mem_err));
		else
			memcpy_fromio(mem_err, &err_rec->u.mcc, sizeof(*mem_err));

		/*
		 * This simply needs the entry count to be non-zero.
		 * Set entry count to one (see ACPI_HEST_ERROR_ENTRY_COUNT).
		 */
		estatus->block_status = (1 << 4); /* i.e. one entry */

		if (++ring->tail >= ring->size)
			ring->tail = 0;
	}

	memcpy(bsrc->bert_va, bert_tbl, bsrc->bert_sz);
	kfree(bert_tbl);

	return 0;
}

/*
 * Boot Error Data probe BERT ring
 */
static int __init ghes_bert_probe(struct platform_device *pdev)
{
	struct mrvl_bed_source bed_src;
	struct device *dev = &pdev->dev;
	int ret = -ENODEV;

#ifdef CONFIG_CRASH_DUMP
	if (is_kdump_kernel())
#else
	#pragma message "CONFIG_CRASH_DUMP setting is required for this module"
	if (true)
#endif
		return ret;

	if (has_acpi_companion(dev)) {
		initdbgmsg("%s ACPI\n", __func__);
		ret = ghes_bed_acpi_match_resource(pdev, &bed_src);
	} else {
		initdbgmsg("%s Device Tree\n", __func__);
		ret = ghes_bed_of_match_resource(&bed_src);
	}
	if (ret)
		goto exit0;

	ret = ghes_bed_map_resource(dev, &bed_src);
	if (ret) {
		initerrmsg("%s Unable map BERT resource\n", __func__);
		goto exit0;
	}

	ret = ghes_bed_count_error(&bed_src);
	if (ret <= 0) {
		initdbgmsg("%s No BERT errors\n", __func__);
		goto exit1;
	}

	ret = ghes_bed_fetch_errors(&bed_src);
	if (ret) {
		initerrmsg("%s Unable setup BERT\n", __func__);
		goto exit1;
	}

	if (!has_acpi_companion(dev)) {
		bert_table_set(bed_src.bert_va);
	}

	if (has_acpi_companion(dev) && !pfn_valid(PHYS_PFN(bed_src.block_pa))) {
		devm_iounmap(dev, bed_src.block_va);
		devm_release_mem_region(dev, bed_src.block_pa, bed_src.block_sz);
	}

exit1:
	initdbgmsg("%s BERT setup done.\n", __func__);
	return ret;

exit0:
	initerrmsg("%s BERT setup failure %d\n", __func__, ret);
	return ret;
}

static void ghes_bert_shutdown(struct platform_device *pdev)
{
}

static int ghes_bert_remove(struct platform_device *pdev)
{
	return 0;
}


static const struct platform_device_id ghes_bert_pdev_match[] = {
	{ .name = DRV_NAME, },
	{},
};
MODULE_DEVICE_TABLE(platform, ghes_bert_pdev_match);

static struct platform_driver ghes_bert_drv_ops __refdata = {
	.driver = {
		.name             = DRV_NAME,
		.of_match_table   = bed_bert_of_match,
		.acpi_match_table = ACPI_PTR(bed_bert_acpi_match),
	},
	.probe    = ghes_bert_probe,
	.remove   = ghes_bert_remove,
	.shutdown = ghes_bert_shutdown,
	.id_table = ghes_bert_pdev_match,
};

static int __init ghes_bert_init(void)
{
	platform_driver_probe(&ghes_bert_drv_ops, ghes_bert_probe);
	return 0;
}

static void __exit ghes_bert_exit(void)
{
	platform_driver_unregister(&ghes_bert_drv_ops);
}

module_init(ghes_bert_init);
module_exit(ghes_bert_exit);

MODULE_DESCRIPTION("OcteonTX2 GHES BERT Module");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:" DRV_NAME);