1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This file implements handling of
4  * Arm Generic Diagnostic Dump and Reset Interface table (AGDI)
5  *
6  * Copyright (c) 2022, Ampere Computing LLC
7  */
8 
9 #define pr_fmt(fmt) "ACPI: AGDI: " fmt
10 
11 #include <linux/acpi.h>
12 #include <linux/acpi_agdi.h>
13 #include <linux/arm_sdei.h>
14 #include <linux/io.h>
15 #include <linux/kernel.h>
16 #include <linux/platform_device.h>
17 
18 struct agdi_data {
19 	int sdei_event;
20 };
21 
agdi_sdei_handler(u32 sdei_event,struct pt_regs * regs,void * arg)22 static int agdi_sdei_handler(u32 sdei_event, struct pt_regs *regs, void *arg)
23 {
24 	nmi_panic(regs, "Arm Generic Diagnostic Dump and Reset SDEI event issued");
25 	return 0;
26 }
27 
agdi_sdei_probe(struct platform_device * pdev,struct agdi_data * adata)28 static int agdi_sdei_probe(struct platform_device *pdev,
29 			   struct agdi_data *adata)
30 {
31 	int err;
32 
33 	err = sdei_event_register(adata->sdei_event, agdi_sdei_handler, pdev);
34 	if (err) {
35 		dev_err(&pdev->dev, "Failed to register for SDEI event %d",
36 			adata->sdei_event);
37 		return err;
38 	}
39 
40 	err = sdei_event_enable(adata->sdei_event);
41 	if (err)  {
42 		sdei_event_unregister(adata->sdei_event);
43 		dev_err(&pdev->dev, "Failed to enable event %d\n",
44 			adata->sdei_event);
45 		return err;
46 	}
47 
48 	return 0;
49 }
50 
agdi_probe(struct platform_device * pdev)51 static int agdi_probe(struct platform_device *pdev)
52 {
53 	struct agdi_data *adata = dev_get_platdata(&pdev->dev);
54 
55 	if (!adata)
56 		return -EINVAL;
57 
58 	return agdi_sdei_probe(pdev, adata);
59 }
60 
agdi_remove(struct platform_device * pdev)61 static int agdi_remove(struct platform_device *pdev)
62 {
63 	struct agdi_data *adata = dev_get_platdata(&pdev->dev);
64 	int err, i;
65 
66 	err = sdei_event_disable(adata->sdei_event);
67 	if (err)
68 		return err;
69 
70 	for (i = 0; i < 3; i++) {
71 		err = sdei_event_unregister(adata->sdei_event);
72 		if (err != -EINPROGRESS)
73 			break;
74 
75 		schedule();
76 	}
77 
78 	return err;
79 }
80 
81 static struct platform_driver agdi_driver = {
82 	.driver = {
83 		.name = "agdi",
84 	},
85 	.probe = agdi_probe,
86 	.remove = agdi_remove,
87 };
88 
acpi_agdi_init(void)89 void __init acpi_agdi_init(void)
90 {
91 	struct acpi_table_agdi *agdi_table;
92 	struct agdi_data pdata;
93 	struct platform_device *pdev;
94 	acpi_status status;
95 
96 	status = acpi_get_table(ACPI_SIG_AGDI, 0,
97 				(struct acpi_table_header **) &agdi_table);
98 	if (ACPI_FAILURE(status))
99 		return;
100 
101 	if (agdi_table->flags & ACPI_AGDI_SIGNALING_MODE) {
102 		pr_warn("Interrupt signaling is not supported");
103 		goto err_put_table;
104 	}
105 
106 	pdata.sdei_event = agdi_table->sdei_event;
107 
108 	pdev = platform_device_register_data(NULL, "agdi", 0, &pdata, sizeof(pdata));
109 	if (IS_ERR(pdev))
110 		goto err_put_table;
111 
112 	if (platform_driver_register(&agdi_driver))
113 		platform_device_unregister(pdev);
114 
115 err_put_table:
116 	acpi_put_table((struct acpi_table_header *)agdi_table);
117 }
118