Peng Hao | 7247932 | 2018-11-06 22:57:21 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 2 | /* |
Peng Hao | 7247932 | 2018-11-06 22:57:21 +0800 | [diff] [blame] | 3 | * Pvpanic Device Support |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2013 Fujitsu. |
Peng Hao | 46f934c | 2018-11-06 22:57:16 +0800 | [diff] [blame] | 6 | * Copyright (C) 2018 ZTE. |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Peng Hao | 8eeffed | 2018-11-06 22:57:20 +0800 | [diff] [blame] | 11 | #include <linux/acpi.h> |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 12 | #include <linux/kernel.h> |
zhenwei pi | 1919416 | 2020-01-02 10:35:13 +0800 | [diff] [blame] | 13 | #include <linux/kexec.h> |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 14 | #include <linux/module.h> |
Peng Hao | 46f934c | 2018-11-06 22:57:16 +0800 | [diff] [blame] | 15 | #include <linux/of.h> |
| 16 | #include <linux/of_address.h> |
| 17 | #include <linux/platform_device.h> |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 18 | #include <linux/types.h> |
zhenwei pi | e0b9a42 | 2020-01-02 10:35:12 +0800 | [diff] [blame] | 19 | #include <uapi/misc/pvpanic.h> |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 20 | |
Peng Hao | 725eba2 | 2018-11-06 22:57:14 +0800 | [diff] [blame] | 21 | static void __iomem *base; |
| 22 | |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 23 | MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>"); |
| 24 | MODULE_DESCRIPTION("pvpanic device driver"); |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 27 | static void |
| 28 | pvpanic_send_event(unsigned int event) |
| 29 | { |
Peng Hao | 725eba2 | 2018-11-06 22:57:14 +0800 | [diff] [blame] | 30 | iowrite8(event, base); |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | static int |
| 34 | pvpanic_panic_notify(struct notifier_block *nb, unsigned long code, |
| 35 | void *unused) |
| 36 | { |
zhenwei pi | 1919416 | 2020-01-02 10:35:13 +0800 | [diff] [blame] | 37 | unsigned int event = PVPANIC_PANICKED; |
| 38 | |
| 39 | if (kexec_crash_loaded()) |
| 40 | event = PVPANIC_CRASH_LOADED; |
| 41 | |
| 42 | pvpanic_send_event(event); |
| 43 | |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 44 | return NOTIFY_DONE; |
| 45 | } |
| 46 | |
| 47 | static struct notifier_block pvpanic_panic_nb = { |
| 48 | .notifier_call = pvpanic_panic_notify, |
Takashi Iwai | 7939831 | 2014-05-06 17:52:26 +0200 | [diff] [blame] | 49 | .priority = 1, /* let this called before broken drm_fb_helper */ |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 50 | }; |
| 51 | |
Peng Hao | 77703e0 | 2018-11-06 22:57:18 +0800 | [diff] [blame] | 52 | #ifdef CONFIG_ACPI |
Peng Hao | a8b71d2 | 2018-11-06 22:57:17 +0800 | [diff] [blame] | 53 | static int pvpanic_add(struct acpi_device *device); |
| 54 | static int pvpanic_remove(struct acpi_device *device); |
| 55 | |
| 56 | static const struct acpi_device_id pvpanic_device_ids[] = { |
| 57 | { "QEMU0001", 0 }, |
Peng Hao | 78ef419 | 2018-11-06 22:57:22 +0800 | [diff] [blame] | 58 | { "", 0 } |
Peng Hao | a8b71d2 | 2018-11-06 22:57:17 +0800 | [diff] [blame] | 59 | }; |
| 60 | MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids); |
| 61 | |
| 62 | static 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 Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 72 | |
| 73 | static acpi_status |
| 74 | pvpanic_walk_resources(struct acpi_resource *res, void *context) |
| 75 | { |
Peng Hao | d2ae171 | 2018-11-06 22:57:13 +0800 | [diff] [blame] | 76 | struct resource r; |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 77 | |
Peng Hao | d2ae171 | 2018-11-06 22:57:13 +0800 | [diff] [blame] | 78 | if (acpi_dev_resource_io(res, &r)) { |
Anders Roxell | d8e346e | 2019-01-11 13:25:25 +0100 | [diff] [blame] | 79 | #ifdef CONFIG_HAS_IOPORT_MAP |
Peng Hao | 725eba2 | 2018-11-06 22:57:14 +0800 | [diff] [blame] | 80 | base = ioport_map(r.start, resource_size(&r)); |
| 81 | return AE_OK; |
Anders Roxell | d8e346e | 2019-01-11 13:25:25 +0100 | [diff] [blame] | 82 | #else |
| 83 | return AE_ERROR; |
| 84 | #endif |
Peng Hao | 725eba2 | 2018-11-06 22:57:14 +0800 | [diff] [blame] | 85 | } else if (acpi_dev_resource_memory(res, &r)) { |
| 86 | base = ioremap(r.start, resource_size(&r)); |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 87 | return AE_OK; |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 88 | } |
Peng Hao | d2ae171 | 2018-11-06 22:57:13 +0800 | [diff] [blame] | 89 | |
| 90 | return AE_ERROR; |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static int pvpanic_add(struct acpi_device *device) |
| 94 | { |
Radim Krčmář | 55cd3f0 | 2015-05-29 22:18:52 +0200 | [diff] [blame] | 95 | int ret; |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 96 | |
Radim Krčmář | 55cd3f0 | 2015-05-29 22:18:52 +0200 | [diff] [blame] | 97 | ret = acpi_bus_get_status(device); |
| 98 | if (ret < 0) |
| 99 | return ret; |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 100 | |
Radim Krčmář | 55cd3f0 | 2015-05-29 22:18:52 +0200 | [diff] [blame] | 101 | if (!device->status.enabled || !device->status.functional) |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 102 | return -ENODEV; |
| 103 | |
| 104 | acpi_walk_resources(device->handle, METHOD_NAME__CRS, |
| 105 | pvpanic_walk_resources, NULL); |
| 106 | |
Peng Hao | 725eba2 | 2018-11-06 22:57:14 +0800 | [diff] [blame] | 107 | if (!base) |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 108 | return -ENODEV; |
| 109 | |
| 110 | atomic_notifier_chain_register(&panic_notifier_list, |
| 111 | &pvpanic_panic_nb); |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | static int pvpanic_remove(struct acpi_device *device) |
| 117 | { |
| 118 | |
| 119 | atomic_notifier_chain_unregister(&panic_notifier_list, |
| 120 | &pvpanic_panic_nb); |
Peng Hao | 725eba2 | 2018-11-06 22:57:14 +0800 | [diff] [blame] | 121 | iounmap(base); |
| 122 | |
Hu Tao | 8b10acd | 2013-05-08 11:15:32 +0800 | [diff] [blame] | 123 | return 0; |
| 124 | } |
| 125 | |
Peng Hao | 77703e0 | 2018-11-06 22:57:18 +0800 | [diff] [blame] | 126 | static int pvpanic_register_acpi_driver(void) |
| 127 | { |
| 128 | return acpi_bus_register_driver(&pvpanic_driver); |
| 129 | } |
| 130 | |
| 131 | static void pvpanic_unregister_acpi_driver(void) |
| 132 | { |
| 133 | acpi_bus_unregister_driver(&pvpanic_driver); |
| 134 | } |
| 135 | #else |
| 136 | static int pvpanic_register_acpi_driver(void) |
| 137 | { |
| 138 | return -ENODEV; |
| 139 | } |
| 140 | |
| 141 | static void pvpanic_unregister_acpi_driver(void) {} |
| 142 | #endif |
| 143 | |
Peng Hao | 46f934c | 2018-11-06 22:57:16 +0800 | [diff] [blame] | 144 | static 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 Carpenter | 97a64ba | 2018-11-26 11:12:29 +0300 | [diff] [blame] | 153 | if (IS_ERR(base)) |
| 154 | return PTR_ERR(base); |
Peng Hao | 46f934c | 2018-11-06 22:57:16 +0800 | [diff] [blame] | 155 | |
| 156 | atomic_notifier_chain_register(&panic_notifier_list, |
| 157 | &pvpanic_panic_nb); |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | static 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 | |
| 171 | static const struct of_device_id pvpanic_mmio_match[] = { |
| 172 | { .compatible = "qemu,pvpanic-mmio", }, |
| 173 | {} |
| 174 | }; |
| 175 | |
| 176 | static 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 | |
| 185 | static int __init pvpanic_mmio_init(void) |
| 186 | { |
| 187 | if (acpi_disabled) |
| 188 | return platform_driver_register(&pvpanic_mmio_driver); |
| 189 | else |
Peng Hao | 77703e0 | 2018-11-06 22:57:18 +0800 | [diff] [blame] | 190 | return pvpanic_register_acpi_driver(); |
Peng Hao | 46f934c | 2018-11-06 22:57:16 +0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | static void __exit pvpanic_mmio_exit(void) |
| 194 | { |
| 195 | if (acpi_disabled) |
| 196 | platform_driver_unregister(&pvpanic_mmio_driver); |
| 197 | else |
Peng Hao | 77703e0 | 2018-11-06 22:57:18 +0800 | [diff] [blame] | 198 | pvpanic_unregister_acpi_driver(); |
Peng Hao | 46f934c | 2018-11-06 22:57:16 +0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | module_init(pvpanic_mmio_init); |
| 202 | module_exit(pvpanic_mmio_exit); |