summaryrefslogtreecommitdiffstats
path: root/drivers/virt/tegra/vm_err.c
blob: 72f59c128d89b6286581e3c15462d0284d56d2e2 (plain) (blame)
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
 * Copyright (c) 2019-2020, NVIDIA CORPORATION.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */
#define pr_fmt(fmt) "vm-err: " fmt

#include <linux/interrupt.h>
#include <linux/of_irq.h>
#include <linux/platform_device.h>
#include <linux/vm_err.h>
#include <asm/traps.h>
#include <asm-generic/irq_regs.h>
#include <asm/system_misc.h>
#include <soc/tegra/virt/syscalls.h>
#include <soc/tegra/chip-id.h>

struct tegra_hv_err_ctrl {
	struct device *dev;
	struct err_info_t *err_info;
	unsigned int async_err_arr_items;
	int hv_peer_err_irq_id;
	unsigned int vcpu_cnt;
	struct serr_hook hook;
	struct tegra_hv_vm_err_handlers handlers;
};

static struct tegra_hv_config config;

static unsigned int tegra_hv_virq_intr_info[3]; /* intr_property_size = 3 */

static struct property tegra_hv_virq_intr_prop = {
	.name = "interrupts",
};

static bool check_sync_err(const unsigned int vcpu_id,
	const struct tegra_hv_err_ctrl *const ctrl,
	bool *send_sync_err_ack)
{
	uint64_t rd_idx;
	const struct err_data_t *err_data;

	if (vcpu_id >= ctrl->vcpu_cnt) {
		dev_crit(ctrl->dev, "%s: Invalid vcpu id %u\n", __func__,
			vcpu_id);
		*send_sync_err_ack = false;
		/* Unexpected vcpu id. Enter bad mode. */
		return true;
	}

	/* Shared memory layout is:
	 * |--async-err-metadata--|--async-errors-array-|--sync-errors-array-|
	 * Size of async errors array = Max errors + 1(to avoid same empty
	 * and full conditions of the buffer)
	 * Size of sync errors array = 1 error per VCPU * number of VCPUs in VM
	 */
	rd_idx = ctrl->async_err_arr_items + vcpu_id;
	/* It's already validated at init time that sufficient memory is
	 * allocated to hold async_err_arr_items + sync error per vcpu. Hence,
	 * after validating the vcpu_id above, no need to validate rd_idx here.
	 */
	err_data = &(ctrl->err_info->err_data[rd_idx]);
	if (err_data->err_reason != REASON_SYNC) {
		dev_crit(ctrl->dev, "%s: unexpected reason id %u\n", __func__,
			err_data->err_reason);
		*send_sync_err_ack = true;
		/* Invalid reason. Enter bad mode. */
		return true;
	}

	if (err_data->err_type != SYNC) {
		dev_crit(ctrl->dev, "%s: unexpected error Type %d\n",
			__func__, err_data->err_type);
		*send_sync_err_ack = true;
		/* Unexpected error id. Enter bad mode. */
		return true;
	}

	if (err_data->offending_guest_id != config.guest_id_self) {
		dev_crit(ctrl->dev, "%s: invalid offender id %u\n", __func__,
			err_data->offending_guest_id);
		*send_sync_err_ack = true;
		/* Invalid id of offending guest. Enter bad mode. */
		return true;
	}
	dev_err(ctrl->dev, "Synchronous error on vcpu %u\n", vcpu_id);

	if (ctrl->handlers.fn_self_sync) {
		*send_sync_err_ack = true;
		/* Enter bad_mode (or otherwise) as custom handler dictates */
		return ctrl->handlers.fn_self_sync(err_data);
	}

	/* should never reach here */
	*send_sync_err_ack = true;
	/* Reaching here is unexpected. Enter bad mode. */
	return true;
}

static irqreturn_t async_err_handler(int irq, void *context)
{
	unsigned int num_async_errs_read = 0;
	bool enter_bad_mode = false;
	const struct tegra_hv_err_ctrl *const ctrl = context;
	const unsigned int vcpu_id = hyp_read_vcpu_id();
	uint64_t local_rd_idx, next_rd_idx;
	const struct err_data_t *err_data;
	bool (*fn_self_async)(const struct err_data_t *const err_data);
	bool (*fn_peer)(const struct err_data_t *const err_data);
	bool (*handler)(const struct err_data_t *const err_data);
	struct pt_regs *regs;

	if (vcpu_id != 0) {
		dev_err(ctrl->dev, "Asynchronous error on vcpu %u\n", vcpu_id);
		/* Only VCPU0 is expected to receive async error vIRQ */
		return IRQ_HANDLED;
	}

	fn_self_async = ctrl->handlers.fn_self_async;
	fn_peer = ctrl->handlers.fn_peer;

	if ((fn_self_async == NULL) && (fn_peer == NULL)) {
		dev_err(ctrl->dev, "Asynchronous error handlers absent\n");
		return IRQ_HANDLED;
	}

	local_rd_idx = ctrl->err_info->async_metadata.rd_idx;
	dev_dbg(ctrl->dev, "Local Rd Idx = %llu, shared Wr Idx = %llu\n",
		local_rd_idx, ctrl->err_info->async_metadata.wr_idx);

	/* Check async error. Read until error queue gets empty */
	while (local_rd_idx != ctrl->err_info->async_metadata.wr_idx) {
		next_rd_idx = (local_rd_idx + 1) % ctrl->async_err_arr_items;

		err_data = &(ctrl->err_info->err_data[next_rd_idx]);
		if (err_data->offending_guest_id == config.guest_id_self)
			handler = fn_self_async;
		else
			handler = fn_peer;

		if (handler) {
			enter_bad_mode = handler(err_data);
		}

		local_rd_idx = next_rd_idx;
		num_async_errs_read++;
		dev_dbg(ctrl->dev, "Local Rd Idx = %llu\n", local_rd_idx);
	}

	if (num_async_errs_read) {
		dev_err(ctrl->dev, "%u asynchronous error(s) read\n",
			num_async_errs_read);

		/* Send ack for async error(s) to HV */
		if (hyp_send_async_err_ack(local_rd_idx) != 0) {
			dev_crit(ctrl->dev,
				"%s: Sending ack failed. Setting bad mode\n",
				__func__);
			/* Unexpected */
			enter_bad_mode = true;
		}
	}

	if (enter_bad_mode) {
		regs = get_irq_regs();
		die("Oops - bad mode", regs, 0);
		panic("bad mode");
	}

	return IRQ_HANDLED;
}

static int sync_err_handler(struct pt_regs *regs, int reason,
	uint32_t esr, void *context)
{
	bool enter_bad_mode = false;
	bool send_sync_err_ack = false;
	const struct tegra_hv_err_ctrl *const ctrl = context;
	const unsigned int vcpu_id = hyp_read_vcpu_id();

	/* Check sync error */
	enter_bad_mode = check_sync_err(vcpu_id, ctrl, &send_sync_err_ack);

	/* Send ack for error to HV. */
	if (send_sync_err_ack) {
		if (hyp_send_sync_err_ack() != 0) {
			dev_crit(ctrl->dev,
				"%s: Sending ack failed. Setting bad mode\n",
				__func__);
			/* Unexpected */
			enter_bad_mode = true;
		}
	}

	/* bad_mode() will call die(). Force the latter to panic. */
	if (enter_bad_mode) {
		panic_on_oops = 1;
		wmb();
	}

	/* Caller expects 0 to enter bad mode */
	return (!enter_bad_mode);
}

void tegra_hv_get_config(struct tegra_hv_config *cfg)
{
	cfg->guest_id_self = config.guest_id_self;
	cfg->num_guests = config.num_guests;
}
EXPORT_SYMBOL(tegra_hv_get_config);

static int virq_handler_init(struct platform_device *pdev)
{
	int ret;
	struct irq_data *peer_err_irq_data;
	int lin_peer_err_irq_id;
	struct tegra_hv_err_ctrl *ctrl = platform_get_drvdata(pdev);
	struct device *dev = &pdev->dev;

	dev_info(ctrl->dev, "Error notification HV IRQ id: %d\n",
		ctrl->hv_peer_err_irq_id);

	/* Ensure HV returned valid irq */
	if (ctrl->hv_peer_err_irq_id == -1)
		return 0;

	/* Set indicate irq type 0 to indicate Shared Peripheral Irq */
	tegra_hv_virq_intr_info[0] = cpu_to_be32(0);
	/* Id in SPI namespace - subtract number of PPIs
	 * (Private Peripheral Irqs) which is = 32
	 */
	tegra_hv_virq_intr_info[1] = cpu_to_be32(ctrl->hv_peer_err_irq_id - 32);
	/* Trigger irq on low-to-high edge (0x1) */
	tegra_hv_virq_intr_info[2] = cpu_to_be32(IRQF_TRIGGER_RISING);

	tegra_hv_virq_intr_prop.length = sizeof(tegra_hv_virq_intr_info);
	dev_info(ctrl->dev, "tegra_hv_virq_intr_prop.length %u\n",
		tegra_hv_virq_intr_prop.length);

	tegra_hv_virq_intr_prop.value = tegra_hv_virq_intr_info;

	if (of_add_property(dev->of_node, &tegra_hv_virq_intr_prop)) {
		dev_err(ctrl->dev, "%s: failed to add interrupts property\n",
			__func__);
		return -EACCES;
	}

	lin_peer_err_irq_id = of_irq_get(dev->of_node, 0);
	if (lin_peer_err_irq_id < 0) {
		dev_err(ctrl->dev, "%s: Unable to get Linux irq for id %d\n",
			__func__, ctrl->hv_peer_err_irq_id);
		return lin_peer_err_irq_id;
	}

	peer_err_irq_data = irq_get_irq_data(lin_peer_err_irq_id);
	if (peer_err_irq_data == NULL) {
		dev_err(ctrl->dev, "%s: Failed to get data for Linux irq %d\n",
			__func__, lin_peer_err_irq_id);
		return -ENODEV;
	}

	ret = devm_request_irq(dev, lin_peer_err_irq_id, async_err_handler,
			IRQ_NOTHREAD, dev_name(dev), ctrl);
	if (ret < 0) {
		dev_err(ctrl->dev,
			"%s: failed to register IRQ %d, Err %d, %s\n",
			__func__, lin_peer_err_irq_id, ret, dev_name(dev));
		return ret;
	}
	dev_info(ctrl->dev, "Registered Linux IRQ %d for peer notification\n",
		lin_peer_err_irq_id);

	return 0;
}

static int serr_handler_init(struct platform_device *pdev)
{
	struct tegra_hv_err_ctrl *ctrl = platform_get_drvdata(pdev);

	ctrl->hook.fn = sync_err_handler;
	ctrl->hook.priv = platform_get_drvdata(pdev);
	register_serr_hook(&ctrl->hook);

	return 0;
}

static int shared_mem_map(struct platform_device *pdev)
{
	uint64_t ipa, buff_size, required_size;
	int ret;
	struct tegra_hv_err_ctrl *ctrl = platform_get_drvdata(pdev);

	/* Get error info details */
	ret = hyp_read_err_info_get(&ipa, &buff_size,
		&ctrl->async_err_arr_items, &ctrl->hv_peer_err_irq_id,
		&ctrl->vcpu_cnt);
	if (ret != 0) {
		/* It could come here if DTS and defconfig enable execution
		 * of this code, but HV hasn't implemented the hypercall.
		 * Flag error.
		 */
		dev_err(ctrl->dev,
			"%s: failed to get err memory address. Err %d\n",
			__func__, ret);
		return -ENODEV;
	}

	if ((ipa == 0) || (buff_size == 0) ||
		(ctrl->async_err_arr_items == 0)) {
		/* It could come here if DTS and defconfig enable execution
		 * of this code, but PCT hasn't enabled error injection.
		 * A warning should suffice.
		 */
		dev_warn(ctrl->dev, "%s: invalid shared memory parameters.\n",
			__func__);
		dev_warn(ctrl->dev,
			"%s: make sure error injection is enabled in PCT\n",
			__func__);
		return -ENOMEM;
	}

	/* Shared memory layout is:
	 * |--async-err-metadata--|--async-errors-array-|--sync-errors-array-|
	 * Size of async errors array = Max errors + 1 (to avoid same empty and
	 * full conditions of the buffer)
	 * Size of sync errors array = 1 error per VCPU * number of VCPUs on
	 * a VM
	 */
	required_size = sizeof(struct async_metadata_t) +
		(sizeof(struct err_data_t) *
		(ctrl->async_err_arr_items + ctrl->vcpu_cnt));
	if (buff_size < required_size) {
		dev_err(ctrl->dev,
			"%s:invalid params. size %llu. required size %llu\n",
			__func__, buff_size, required_size);
		dev_err(ctrl->dev, "%s: async arr size %u. vcpus %u\n",
			__func__, ctrl->async_err_arr_items, ctrl->vcpu_cnt);
		return -ENOMEM;
	}

	dev_info(ctrl->dev, "%s: Err info IPA for guest %u: 0x%llx\n",
		__func__, config.guest_id_self, ipa);
	dev_info(ctrl->dev, "Err info buf size 0x%llX\n", buff_size);
	dev_info(ctrl->dev, "Async err arr size %u. Number of VCPUs %u\n",
		ctrl->async_err_arr_items, ctrl->vcpu_cnt);

	/* Map shared memory */
	ctrl->err_info = (struct err_info_t *) ioremap_cache(ipa, buff_size);
	if (ctrl->err_info == NULL)
		return -ENOMEM;

	return 0;
}

static int hyp_config_init(struct device *dev)
{
	int ret = hyp_read_gid(&config.guest_id_self);

	if (ret != 0) {
		dev_err(dev, "%s: failed to read guest id. Err %d\n",
			__func__, ret);
		return ret;
	}

	ret = hyp_read_nguests(&config.num_guests);
	if (ret != 0) {
		/* Only privileged guest can query number of guests */
		dev_warn(dev, "%s: can't read number of guests. Err %d\n",
			__func__, ret);
	}

	dev_info(dev, "%s: guest id %u num guests %u\n", __func__,
		config.guest_id_self, config.num_guests);

	return 0;
}

static void shared_structs_check(struct device *dev)
{
	/* Ensure coherency with common header */
	BUILD_BUG_ON(REASON_ENUM_SIZE !=
		(ARRAY_SIZE(tegra_hv_err_reason_desc)));

	/* Dump shared structures sizes. This is needed to manually compare
	 * with HV console dump, if a build bug fires below.
	 */
	dev_info(dev, "async_metadata size 0x%lx\n",
		sizeof(struct async_metadata_t));
	dev_info(dev, "async_bridge_err size 0x%lx\n",
		sizeof(struct async_bridge_err_t));
	dev_info(dev, "async_cbb_err size 0x%lx\n",
		sizeof(struct async_cbb_err_t));
	dev_info(dev, "async_smmu_err size 0x%lx\n",
		sizeof(struct async_smmu_err_t));
	dev_info(dev, "async_mc_err size 0x%lx\n",
		sizeof(struct async_mc_err_t));
	dev_info(dev, "async_mc_err_t19x size 0x%lx\n",
		sizeof(struct async_mc_err_t19x_t));
	dev_info(dev, "sync size 0x%lx\n",
		sizeof(struct sync_t));
	dev_info(dev, "err_data size 0x%lx\n", sizeof(struct err_data_t));

	/* Ensure common structures shared by HV and Linux are in sync */
	BUILD_BUG_ON(sizeof(struct async_metadata_t) != 0x10);
	BUILD_BUG_ON(sizeof(struct async_bridge_err_t) != 0x74);
	BUILD_BUG_ON(sizeof(struct async_cbb_err_t) != 0x259);
	BUILD_BUG_ON(sizeof(struct async_smmu_err_t) != 0x1C);
	BUILD_BUG_ON(sizeof(struct async_mc_err_t) != 0x24);
	BUILD_BUG_ON(sizeof(struct async_mc_err_t19x_t) != 0xBB);
	BUILD_BUG_ON(sizeof(struct sync_t) != 0x149);
	BUILD_BUG_ON(sizeof(struct err_data_t) != 0x265);
}

static int vm_err_handler_init(struct platform_device *pdev)
{
	int ret;
	struct tegra_hv_err_ctrl *ctrl;
	struct device *dev = &pdev->dev;

	if (!is_tegra_hypervisor_mode()) {
		dev_err(dev, "%s: hypervisor is not present\n", __func__);
		return -ENODEV;
	}

	shared_structs_check(dev);

	ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
	if (!ctrl)
		return -ENOMEM;

	ctrl->dev = dev;
	platform_set_drvdata(pdev, ctrl);

	ret = hyp_config_init(dev);
	if (ret)
		return ret;

	ret = shared_mem_map(pdev);
	if (ret)
		return -ENOMEM;

	ret = serr_handler_init(pdev);
	if (ret)
		return ret;

	ret = virq_handler_init(pdev);
	if (ret)
		return ret;

	return 0;
}

static int vm_err_handler_remove(struct platform_device *pdev)
{
	struct tegra_hv_err_ctrl *ctrl = platform_get_drvdata(pdev);
	struct device_node *node = pdev->dev.of_node;

	if (of_remove_property(node,
		of_find_property(node, "interrupts", NULL))) {
		dev_err(ctrl->dev, "%s: failed to add interrupts property\n",
			__func__);
		return -EACCES;
	}

	unregister_serr_hook(&ctrl->hook);
	iounmap(ctrl->err_info);

	dev_info(ctrl->dev, "%s: cleaned up and unregistered handler\n",
		__func__);

	return 0;
}

static const struct of_device_id tegra_hv_err_match[] = {
	{ .compatible = "nvidia,tegra-hv-err", .data = NULL},
	{},
};

static struct platform_driver tegra_hv_err_pdriver = {
	.driver = {
		.name = "tegra-hv-err-handler",
		.owner = THIS_MODULE,
		.of_match_table = of_match_ptr(tegra_hv_err_match),
	},
	.probe = vm_err_handler_init,
	.remove = vm_err_handler_remove,
};

static int tegra_hv_register_hooks_for_device(struct device *dev,
	void *handlers)
{
	struct tegra_hv_err_ctrl *ctrl;
	const struct platform_device *pd = container_of(dev,
		struct platform_device, dev);
	const struct tegra_hv_vm_err_handlers *_handlers =
		(struct tegra_hv_vm_err_handlers *) handlers;

	ctrl = platform_get_drvdata(pd);
	if (!ctrl) {
		dev_err(dev, "%s: no platform data", __func__);
		return 0;
	}

	if (ctrl->handlers.fn_self_async == NULL)
		ctrl->handlers.fn_self_async = _handlers->fn_self_async;

	if (ctrl->handlers.fn_self_sync == NULL)
		ctrl->handlers.fn_self_sync = _handlers->fn_self_sync;

	if (ctrl->handlers.fn_peer == NULL)
		ctrl->handlers.fn_peer = _handlers->fn_peer;

	return 0;
}

int tegra_hv_register_vm_err_hooks(struct tegra_hv_vm_err_handlers *handlers)
{
	int ret;

	if (!handlers) {
		pr_err("%s: invalid error handlers\n", __func__);
		return 1;
	}

	if (!handlers->fn_self_async && !handlers->fn_self_sync
		&& !handlers->fn_peer) {
		platform_driver_unregister(&tegra_hv_err_pdriver);
		return 0;
	}

	if (!tegra_hv_err_pdriver.driver.p) {
		/* Not registered/bound yet */
		ret = platform_driver_register(&tegra_hv_err_pdriver);
		if (ret) {
			pr_err("%s: failed to register driver. Err %d\n",
				__func__, ret);
			return ret;
		}
	}

	ret = driver_for_each_device(&tegra_hv_err_pdriver.driver, NULL,
		handlers, tegra_hv_register_hooks_for_device);
	if (ret) {
		pr_err("%s: failed to attach driver. Err %d\n", __func__, ret);
		return ret;
	}

	return 0;
}
EXPORT_SYMBOL(tegra_hv_register_vm_err_hooks);