blob: 95ff7c5a1dfb62c1363006b5a0919562572cfbce [file] [log] [blame]
Peng Hao72479322018-11-06 22:57:21 +08001// SPDX-License-Identifier: GPL-2.0+
Hu Tao8b10acd2013-05-08 11:15:32 +08002/*
Peng Hao72479322018-11-06 22:57:21 +08003 * Pvpanic Device Support
Hu Tao8b10acd2013-05-08 11:15:32 +08004 *
5 * Copyright (C) 2013 Fujitsu.
Peng Hao46f934c2018-11-06 22:57:16 +08006 * Copyright (C) 2018 ZTE.
Hu Tao8b10acd2013-05-08 11:15:32 +08007 */
8
9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10
Peng Hao8eeffed2018-11-06 22:57:20 +080011#include <linux/acpi.h>
Hu Tao8b10acd2013-05-08 11:15:32 +080012#include <linux/kernel.h>
13#include <linux/module.h>
Peng Hao46f934c2018-11-06 22:57:16 +080014#include <linux/of.h>
15#include <linux/of_address.h>
16#include <linux/platform_device.h>
Hu Tao8b10acd2013-05-08 11:15:32 +080017#include <linux/types.h>
Hu Tao8b10acd2013-05-08 11:15:32 +080018
Peng Hao725eba22018-11-06 22:57:14 +080019static void __iomem *base;
20
Peng Haoa8b71d22018-11-06 22:57:17 +080021#define PVPANIC_PANICKED (1 << 0)
22
Hu Tao8b10acd2013-05-08 11:15:32 +080023MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
24MODULE_DESCRIPTION("pvpanic device driver");
25MODULE_LICENSE("GPL");
26
Hu Tao8b10acd2013-05-08 11:15:32 +080027static void
28pvpanic_send_event(unsigned int event)
29{
Peng Hao725eba22018-11-06 22:57:14 +080030 iowrite8(event, base);
Hu Tao8b10acd2013-05-08 11:15:32 +080031}
32
33static int
34pvpanic_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
41static struct notifier_block pvpanic_panic_nb = {
42 .notifier_call = pvpanic_panic_notify,
Takashi Iwai79398312014-05-06 17:52:26 +020043 .priority = 1, /* let this called before broken drm_fb_helper */
Hu Tao8b10acd2013-05-08 11:15:32 +080044};
45
Peng Hao77703e02018-11-06 22:57:18 +080046#ifdef CONFIG_ACPI
Peng Haoa8b71d22018-11-06 22:57:17 +080047static int pvpanic_add(struct acpi_device *device);
48static int pvpanic_remove(struct acpi_device *device);
49
50static const struct acpi_device_id pvpanic_device_ids[] = {
51 { "QEMU0001", 0 },
Peng Hao78ef4192018-11-06 22:57:22 +080052 { "", 0 }
Peng Haoa8b71d22018-11-06 22:57:17 +080053};
54MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
55
56static 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};
Hu Tao8b10acd2013-05-08 11:15:32 +080066
67static acpi_status
68pvpanic_walk_resources(struct acpi_resource *res, void *context)
69{
Peng Haod2ae1712018-11-06 22:57:13 +080070 struct resource r;
Hu Tao8b10acd2013-05-08 11:15:32 +080071
Peng Haod2ae1712018-11-06 22:57:13 +080072 if (acpi_dev_resource_io(res, &r)) {
Anders Roxelld8e346e2019-01-11 13:25:25 +010073#ifdef CONFIG_HAS_IOPORT_MAP
Peng Hao725eba22018-11-06 22:57:14 +080074 base = ioport_map(r.start, resource_size(&r));
75 return AE_OK;
Anders Roxelld8e346e2019-01-11 13:25:25 +010076#else
77 return AE_ERROR;
78#endif
Peng Hao725eba22018-11-06 22:57:14 +080079 } else if (acpi_dev_resource_memory(res, &r)) {
80 base = ioremap(r.start, resource_size(&r));
Hu Tao8b10acd2013-05-08 11:15:32 +080081 return AE_OK;
Hu Tao8b10acd2013-05-08 11:15:32 +080082 }
Peng Haod2ae1712018-11-06 22:57:13 +080083
84 return AE_ERROR;
Hu Tao8b10acd2013-05-08 11:15:32 +080085}
86
87static int pvpanic_add(struct acpi_device *device)
88{
Radim Krčmář55cd3f02015-05-29 22:18:52 +020089 int ret;
Hu Tao8b10acd2013-05-08 11:15:32 +080090
Radim Krčmář55cd3f02015-05-29 22:18:52 +020091 ret = acpi_bus_get_status(device);
92 if (ret < 0)
93 return ret;
Hu Tao8b10acd2013-05-08 11:15:32 +080094
Radim Krčmář55cd3f02015-05-29 22:18:52 +020095 if (!device->status.enabled || !device->status.functional)
Hu Tao8b10acd2013-05-08 11:15:32 +080096 return -ENODEV;
97
98 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
99 pvpanic_walk_resources, NULL);
100
Peng Hao725eba22018-11-06 22:57:14 +0800101 if (!base)
Hu Tao8b10acd2013-05-08 11:15:32 +0800102 return -ENODEV;
103
104 atomic_notifier_chain_register(&panic_notifier_list,
105 &pvpanic_panic_nb);
106
107 return 0;
108}
109
110static int pvpanic_remove(struct acpi_device *device)
111{
112
113 atomic_notifier_chain_unregister(&panic_notifier_list,
114 &pvpanic_panic_nb);
Peng Hao725eba22018-11-06 22:57:14 +0800115 iounmap(base);
116
Hu Tao8b10acd2013-05-08 11:15:32 +0800117 return 0;
118}
119
Peng Hao77703e02018-11-06 22:57:18 +0800120static int pvpanic_register_acpi_driver(void)
121{
122 return acpi_bus_register_driver(&pvpanic_driver);
123}
124
125static void pvpanic_unregister_acpi_driver(void)
126{
127 acpi_bus_unregister_driver(&pvpanic_driver);
128}
129#else
130static int pvpanic_register_acpi_driver(void)
131{
132 return -ENODEV;
133}
134
135static void pvpanic_unregister_acpi_driver(void) {}
136#endif
137
Peng Hao46f934c2018-11-06 22:57:16 +0800138static int pvpanic_mmio_probe(struct platform_device *pdev)
139{
140 struct resource *mem;
141
142 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
143 if (!mem)
144 return -EINVAL;
145
146 base = devm_ioremap_resource(&pdev->dev, mem);
Dan Carpenter97a64ba2018-11-26 11:12:29 +0300147 if (IS_ERR(base))
148 return PTR_ERR(base);
Peng Hao46f934c2018-11-06 22:57:16 +0800149
150 atomic_notifier_chain_register(&panic_notifier_list,
151 &pvpanic_panic_nb);
152
153 return 0;
154}
155
156static int pvpanic_mmio_remove(struct platform_device *pdev)
157{
158
159 atomic_notifier_chain_unregister(&panic_notifier_list,
160 &pvpanic_panic_nb);
161
162 return 0;
163}
164
165static const struct of_device_id pvpanic_mmio_match[] = {
166 { .compatible = "qemu,pvpanic-mmio", },
167 {}
168};
169
170static struct platform_driver pvpanic_mmio_driver = {
171 .driver = {
172 .name = "pvpanic-mmio",
173 .of_match_table = pvpanic_mmio_match,
174 },
175 .probe = pvpanic_mmio_probe,
176 .remove = pvpanic_mmio_remove,
177};
178
179static int __init pvpanic_mmio_init(void)
180{
181 if (acpi_disabled)
182 return platform_driver_register(&pvpanic_mmio_driver);
183 else
Peng Hao77703e02018-11-06 22:57:18 +0800184 return pvpanic_register_acpi_driver();
Peng Hao46f934c2018-11-06 22:57:16 +0800185}
186
187static void __exit pvpanic_mmio_exit(void)
188{
189 if (acpi_disabled)
190 platform_driver_unregister(&pvpanic_mmio_driver);
191 else
Peng Hao77703e02018-11-06 22:57:18 +0800192 pvpanic_unregister_acpi_driver();
Peng Hao46f934c2018-11-06 22:57:16 +0800193}
194
195module_init(pvpanic_mmio_init);
196module_exit(pvpanic_mmio_exit);