blob: a6e1a8983e1fcc6201f50140672babe34a9613c7 [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>
zhenwei pi19194162020-01-02 10:35:13 +080013#include <linux/kexec.h>
Hu Tao8b10acd2013-05-08 11:15:32 +080014#include <linux/module.h>
Peng Hao46f934c2018-11-06 22:57:16 +080015#include <linux/of.h>
16#include <linux/of_address.h>
17#include <linux/platform_device.h>
Hu Tao8b10acd2013-05-08 11:15:32 +080018#include <linux/types.h>
zhenwei pie0b9a422020-01-02 10:35:12 +080019#include <uapi/misc/pvpanic.h>
Hu Tao8b10acd2013-05-08 11:15:32 +080020
Peng Hao725eba22018-11-06 22:57:14 +080021static void __iomem *base;
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{
zhenwei pi19194162020-01-02 10:35:13 +080037 unsigned int event = PVPANIC_PANICKED;
38
39 if (kexec_crash_loaded())
40 event = PVPANIC_CRASH_LOADED;
41
42 pvpanic_send_event(event);
43
Hu Tao8b10acd2013-05-08 11:15:32 +080044 return NOTIFY_DONE;
45}
46
47static struct notifier_block pvpanic_panic_nb = {
48 .notifier_call = pvpanic_panic_notify,
Takashi Iwai79398312014-05-06 17:52:26 +020049 .priority = 1, /* let this called before broken drm_fb_helper */
Hu Tao8b10acd2013-05-08 11:15:32 +080050};
51
Peng Hao77703e02018-11-06 22:57:18 +080052#ifdef CONFIG_ACPI
Peng Haoa8b71d22018-11-06 22:57:17 +080053static int pvpanic_add(struct acpi_device *device);
54static int pvpanic_remove(struct acpi_device *device);
55
56static const struct acpi_device_id pvpanic_device_ids[] = {
57 { "QEMU0001", 0 },
Peng Hao78ef4192018-11-06 22:57:22 +080058 { "", 0 }
Peng Haoa8b71d22018-11-06 22:57:17 +080059};
60MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
61
62static struct acpi_driver pvpanic_driver = {
63 .name = "pvpanic",
64 .class = "QEMU",
65 .ids = pvpanic_device_ids,
66 .ops = {
67 .add = pvpanic_add,
68 .remove = pvpanic_remove,
69 },
70 .owner = THIS_MODULE,
71};
Hu Tao8b10acd2013-05-08 11:15:32 +080072
73static acpi_status
74pvpanic_walk_resources(struct acpi_resource *res, void *context)
75{
Peng Haod2ae1712018-11-06 22:57:13 +080076 struct resource r;
Hu Tao8b10acd2013-05-08 11:15:32 +080077
Peng Haod2ae1712018-11-06 22:57:13 +080078 if (acpi_dev_resource_io(res, &r)) {
Anders Roxelld8e346e2019-01-11 13:25:25 +010079#ifdef CONFIG_HAS_IOPORT_MAP
Peng Hao725eba22018-11-06 22:57:14 +080080 base = ioport_map(r.start, resource_size(&r));
81 return AE_OK;
Anders Roxelld8e346e2019-01-11 13:25:25 +010082#else
83 return AE_ERROR;
84#endif
Peng Hao725eba22018-11-06 22:57:14 +080085 } else if (acpi_dev_resource_memory(res, &r)) {
86 base = ioremap(r.start, resource_size(&r));
Hu Tao8b10acd2013-05-08 11:15:32 +080087 return AE_OK;
Hu Tao8b10acd2013-05-08 11:15:32 +080088 }
Peng Haod2ae1712018-11-06 22:57:13 +080089
90 return AE_ERROR;
Hu Tao8b10acd2013-05-08 11:15:32 +080091}
92
93static int pvpanic_add(struct acpi_device *device)
94{
Radim Krčmář55cd3f02015-05-29 22:18:52 +020095 int ret;
Hu Tao8b10acd2013-05-08 11:15:32 +080096
Radim Krčmář55cd3f02015-05-29 22:18:52 +020097 ret = acpi_bus_get_status(device);
98 if (ret < 0)
99 return ret;
Hu Tao8b10acd2013-05-08 11:15:32 +0800100
Radim Krčmář55cd3f02015-05-29 22:18:52 +0200101 if (!device->status.enabled || !device->status.functional)
Hu Tao8b10acd2013-05-08 11:15:32 +0800102 return -ENODEV;
103
104 acpi_walk_resources(device->handle, METHOD_NAME__CRS,
105 pvpanic_walk_resources, NULL);
106
Peng Hao725eba22018-11-06 22:57:14 +0800107 if (!base)
Hu Tao8b10acd2013-05-08 11:15:32 +0800108 return -ENODEV;
109
110 atomic_notifier_chain_register(&panic_notifier_list,
111 &pvpanic_panic_nb);
112
113 return 0;
114}
115
116static int pvpanic_remove(struct acpi_device *device)
117{
118
119 atomic_notifier_chain_unregister(&panic_notifier_list,
120 &pvpanic_panic_nb);
Peng Hao725eba22018-11-06 22:57:14 +0800121 iounmap(base);
122
Hu Tao8b10acd2013-05-08 11:15:32 +0800123 return 0;
124}
125
Peng Hao77703e02018-11-06 22:57:18 +0800126static int pvpanic_register_acpi_driver(void)
127{
128 return acpi_bus_register_driver(&pvpanic_driver);
129}
130
131static void pvpanic_unregister_acpi_driver(void)
132{
133 acpi_bus_unregister_driver(&pvpanic_driver);
134}
135#else
136static int pvpanic_register_acpi_driver(void)
137{
138 return -ENODEV;
139}
140
141static void pvpanic_unregister_acpi_driver(void) {}
142#endif
143
Peng Hao46f934c2018-11-06 22:57:16 +0800144static int pvpanic_mmio_probe(struct platform_device *pdev)
145{
146 struct resource *mem;
147
148 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149 if (!mem)
150 return -EINVAL;
151
152 base = devm_ioremap_resource(&pdev->dev, mem);
Dan Carpenter97a64ba2018-11-26 11:12:29 +0300153 if (IS_ERR(base))
154 return PTR_ERR(base);
Peng Hao46f934c2018-11-06 22:57:16 +0800155
156 atomic_notifier_chain_register(&panic_notifier_list,
157 &pvpanic_panic_nb);
158
159 return 0;
160}
161
162static int pvpanic_mmio_remove(struct platform_device *pdev)
163{
164
165 atomic_notifier_chain_unregister(&panic_notifier_list,
166 &pvpanic_panic_nb);
167
168 return 0;
169}
170
171static const struct of_device_id pvpanic_mmio_match[] = {
172 { .compatible = "qemu,pvpanic-mmio", },
173 {}
174};
175
176static struct platform_driver pvpanic_mmio_driver = {
177 .driver = {
178 .name = "pvpanic-mmio",
179 .of_match_table = pvpanic_mmio_match,
180 },
181 .probe = pvpanic_mmio_probe,
182 .remove = pvpanic_mmio_remove,
183};
184
185static int __init pvpanic_mmio_init(void)
186{
187 if (acpi_disabled)
188 return platform_driver_register(&pvpanic_mmio_driver);
189 else
Peng Hao77703e02018-11-06 22:57:18 +0800190 return pvpanic_register_acpi_driver();
Peng Hao46f934c2018-11-06 22:57:16 +0800191}
192
193static void __exit pvpanic_mmio_exit(void)
194{
195 if (acpi_disabled)
196 platform_driver_unregister(&pvpanic_mmio_driver);
197 else
Peng Hao77703e02018-11-06 22:57:18 +0800198 pvpanic_unregister_acpi_driver();
Peng Hao46f934c2018-11-06 22:57:16 +0800199}
200
201module_init(pvpanic_mmio_init);
202module_exit(pvpanic_mmio_exit);