Merge tag 'dmaengine-4.21-rc1' of git://git.infradead.org/users/vkoul/slave-dma
[linux-2.6-microblaze.git] / drivers / misc / pvpanic.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *  Pvpanic Device Support
4  *
5  *  Copyright (C) 2013 Fujitsu.
6  *  Copyright (C) 2018 ZTE.
7  */
8
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
11 #include <linux/acpi.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/platform_device.h>
17 #include <linux/types.h>
18
19 static void __iomem *base;
20
21 #define PVPANIC_PANICKED        (1 << 0)
22
23 MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
24 MODULE_DESCRIPTION("pvpanic device driver");
25 MODULE_LICENSE("GPL");
26
27 static void
28 pvpanic_send_event(unsigned int event)
29 {
30         iowrite8(event, base);
31 }
32
33 static int
34 pvpanic_panic_notify(struct notifier_block *nb, unsigned long code,
35                      void *unused)
36 {
37         pvpanic_send_event(PVPANIC_PANICKED);
38         return NOTIFY_DONE;
39 }
40
41 static struct notifier_block pvpanic_panic_nb = {
42         .notifier_call = pvpanic_panic_notify,
43         .priority = 1, /* let this called before broken drm_fb_helper */
44 };
45
46 #ifdef CONFIG_ACPI
47 static int pvpanic_add(struct acpi_device *device);
48 static int pvpanic_remove(struct acpi_device *device);
49
50 static const struct acpi_device_id pvpanic_device_ids[] = {
51         { "QEMU0001", 0 },
52         { "", 0 }
53 };
54 MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
55
56 static struct acpi_driver pvpanic_driver = {
57         .name =         "pvpanic",
58         .class =        "QEMU",
59         .ids =          pvpanic_device_ids,
60         .ops =          {
61                                 .add =          pvpanic_add,
62                                 .remove =       pvpanic_remove,
63                         },
64         .owner =        THIS_MODULE,
65 };
66
67 static acpi_status
68 pvpanic_walk_resources(struct acpi_resource *res, void *context)
69 {
70         struct resource r;
71
72         if (acpi_dev_resource_io(res, &r)) {
73                 base = ioport_map(r.start, resource_size(&r));
74                 return AE_OK;
75         } else if (acpi_dev_resource_memory(res, &r)) {
76                 base = ioremap(r.start, resource_size(&r));
77                 return AE_OK;
78         }
79
80         return AE_ERROR;
81 }
82
83 static int pvpanic_add(struct acpi_device *device)
84 {
85         int ret;
86
87         ret = acpi_bus_get_status(device);
88         if (ret < 0)
89                 return ret;
90
91         if (!device->status.enabled || !device->status.functional)
92                 return -ENODEV;
93
94         acpi_walk_resources(device->handle, METHOD_NAME__CRS,
95                             pvpanic_walk_resources, NULL);
96
97         if (!base)
98                 return -ENODEV;
99
100         atomic_notifier_chain_register(&panic_notifier_list,
101                                        &pvpanic_panic_nb);
102
103         return 0;
104 }
105
106 static int pvpanic_remove(struct acpi_device *device)
107 {
108
109         atomic_notifier_chain_unregister(&panic_notifier_list,
110                                          &pvpanic_panic_nb);
111         iounmap(base);
112
113         return 0;
114 }
115
116 static int pvpanic_register_acpi_driver(void)
117 {
118         return acpi_bus_register_driver(&pvpanic_driver);
119 }
120
121 static void pvpanic_unregister_acpi_driver(void)
122 {
123         acpi_bus_unregister_driver(&pvpanic_driver);
124 }
125 #else
126 static int pvpanic_register_acpi_driver(void)
127 {
128         return -ENODEV;
129 }
130
131 static void pvpanic_unregister_acpi_driver(void) {}
132 #endif
133
134 static int pvpanic_mmio_probe(struct platform_device *pdev)
135 {
136         struct resource *mem;
137
138         mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
139         if (!mem)
140                 return -EINVAL;
141
142         base = devm_ioremap_resource(&pdev->dev, mem);
143         if (IS_ERR(base))
144                 return PTR_ERR(base);
145
146         atomic_notifier_chain_register(&panic_notifier_list,
147                                        &pvpanic_panic_nb);
148
149         return 0;
150 }
151
152 static int pvpanic_mmio_remove(struct platform_device *pdev)
153 {
154
155         atomic_notifier_chain_unregister(&panic_notifier_list,
156                                          &pvpanic_panic_nb);
157
158         return 0;
159 }
160
161 static const struct of_device_id pvpanic_mmio_match[] = {
162         { .compatible = "qemu,pvpanic-mmio", },
163         {}
164 };
165
166 static struct platform_driver pvpanic_mmio_driver = {
167         .driver = {
168                 .name = "pvpanic-mmio",
169                 .of_match_table = pvpanic_mmio_match,
170         },
171         .probe = pvpanic_mmio_probe,
172         .remove = pvpanic_mmio_remove,
173 };
174
175 static int __init pvpanic_mmio_init(void)
176 {
177         if (acpi_disabled)
178                 return platform_driver_register(&pvpanic_mmio_driver);
179         else
180                 return pvpanic_register_acpi_driver();
181 }
182
183 static void __exit pvpanic_mmio_exit(void)
184 {
185         if (acpi_disabled)
186                 platform_driver_unregister(&pvpanic_mmio_driver);
187         else
188                 pvpanic_unregister_acpi_driver();
189 }
190
191 module_init(pvpanic_mmio_init);
192 module_exit(pvpanic_mmio_exit);