blob: a46701c22b3521b30683ed2d385436fe17622891 [file] [log] [blame]
Hu Tao8b10acd2013-05-08 11:15:32 +08001/*
2 * pvpanic.c - pvpanic Device Support
3 *
4 * Copyright (C) 2013 Fujitsu.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23#include <linux/kernel.h>
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/types.h>
Lv Zheng8b484632013-12-03 08:49:16 +080027#include <linux/acpi.h>
Hu Tao8b10acd2013-05-08 11:15:32 +080028
Peng Hao725eba22018-11-06 22:57:14 +080029static void __iomem *base;
30
Hu Tao8b10acd2013-05-08 11:15:32 +080031MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
32MODULE_DESCRIPTION("pvpanic device driver");
33MODULE_LICENSE("GPL");
34
35static int pvpanic_add(struct acpi_device *device);
36static int pvpanic_remove(struct acpi_device *device);
37
38static const struct acpi_device_id pvpanic_device_ids[] = {
39 { "QEMU0001", 0 },
40 { "", 0 },
41};
42MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
43
44#define PVPANIC_PANICKED (1 << 0)
45
Hu Tao8b10acd2013-05-08 11:15:32 +080046static struct acpi_driver pvpanic_driver = {
47 .name = "pvpanic",
48 .class = "QEMU",
49 .ids = pvpanic_device_ids,
50 .ops = {
51 .add = pvpanic_add,
52 .remove = pvpanic_remove,
53 },
54 .owner = THIS_MODULE,
55};
56
57static void
58pvpanic_send_event(unsigned int event)
59{
Peng Hao725eba22018-11-06 22:57:14 +080060 iowrite8(event, base);
Hu Tao8b10acd2013-05-08 11:15:32 +080061}
62
63static int
64pvpanic_panic_notify(struct notifier_block *nb, unsigned long code,
65 void *unused)
66{
67 pvpanic_send_event(PVPANIC_PANICKED);
68 return NOTIFY_DONE;
69}
70
71static struct notifier_block pvpanic_panic_nb = {
72 .notifier_call = pvpanic_panic_notify,
Takashi Iwai79398312014-05-06 17:52:26 +020073 .priority = 1, /* let this called before broken drm_fb_helper */
Hu Tao8b10acd2013-05-08 11:15:32 +080074};
75
76
77static acpi_status
78pvpanic_walk_resources(struct acpi_resource *res, void *context)
79{
Peng Haod2ae1712018-11-06 22:57:13 +080080 struct resource r;
Hu Tao8b10acd2013-05-08 11:15:32 +080081
Peng Haod2ae1712018-11-06 22:57:13 +080082 if (acpi_dev_resource_io(res, &r)) {
Peng Hao725eba22018-11-06 22:57:14 +080083 base = ioport_map(r.start, resource_size(&r));
84 return AE_OK;
85 } 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
126module_acpi_driver(pvpanic_driver);