1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Pvpanic MMIO Device Support
4 *
5 * Copyright (C) 2013 Fujitsu.
6 * Copyright (C) 2018 ZTE.
7 * Copyright (C) 2021 Oracle.
8 */
9
10 #include <linux/io.h>
11 #include <linux/kernel.h>
12 #include <linux/kexec.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/types.h>
17 #include <linux/slab.h>
18
19 #include <uapi/misc/pvpanic.h>
20
21 #include "pvpanic.h"
22
23 MODULE_AUTHOR("Hu Tao <hutao@cn.fujitsu.com>");
24 MODULE_DESCRIPTION("pvpanic-mmio device driver");
25 MODULE_LICENSE("GPL");
26
capability_show(struct device * dev,struct device_attribute * attr,char * buf)27 static ssize_t capability_show(struct device *dev, struct device_attribute *attr, char *buf)
28 {
29 struct pvpanic_instance *pi = dev_get_drvdata(dev);
30
31 return sysfs_emit(buf, "%x\n", pi->capability);
32 }
33 static DEVICE_ATTR_RO(capability);
34
events_show(struct device * dev,struct device_attribute * attr,char * buf)35 static ssize_t events_show(struct device *dev, struct device_attribute *attr, char *buf)
36 {
37 struct pvpanic_instance *pi = dev_get_drvdata(dev);
38
39 return sysfs_emit(buf, "%x\n", pi->events);
40 }
41
events_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)42 static ssize_t events_store(struct device *dev, struct device_attribute *attr,
43 const char *buf, size_t count)
44 {
45 struct pvpanic_instance *pi = dev_get_drvdata(dev);
46 unsigned int tmp;
47 int err;
48
49 err = kstrtouint(buf, 16, &tmp);
50 if (err)
51 return err;
52
53 if ((tmp & pi->capability) != tmp)
54 return -EINVAL;
55
56 pi->events = tmp;
57
58 return count;
59 }
60 static DEVICE_ATTR_RW(events);
61
62 static struct attribute *pvpanic_mmio_dev_attrs[] = {
63 &dev_attr_capability.attr,
64 &dev_attr_events.attr,
65 NULL
66 };
67 ATTRIBUTE_GROUPS(pvpanic_mmio_dev);
68
pvpanic_mmio_probe(struct platform_device * pdev)69 static int pvpanic_mmio_probe(struct platform_device *pdev)
70 {
71 struct device *dev = &pdev->dev;
72 struct pvpanic_instance *pi;
73 struct resource *res;
74 void __iomem *base;
75
76 res = platform_get_mem_or_io(pdev, 0);
77 if (!res)
78 return -EINVAL;
79
80 switch (resource_type(res)) {
81 case IORESOURCE_IO:
82 base = devm_ioport_map(dev, res->start, resource_size(res));
83 if (!base)
84 return -ENOMEM;
85 break;
86 case IORESOURCE_MEM:
87 base = devm_ioremap_resource(dev, res);
88 if (IS_ERR(base))
89 return PTR_ERR(base);
90 break;
91 default:
92 return -EINVAL;
93 }
94
95 pi = devm_kmalloc(dev, sizeof(*pi), GFP_KERNEL);
96 if (!pi)
97 return -ENOMEM;
98
99 pi->base = base;
100 pi->capability = PVPANIC_PANICKED | PVPANIC_CRASH_LOADED;
101
102 /* initialize capability by RDPT */
103 pi->capability &= ioread8(base);
104 pi->events = pi->capability;
105
106 return devm_pvpanic_probe(dev, pi);
107 }
108
109 static const struct of_device_id pvpanic_mmio_match[] = {
110 { .compatible = "qemu,pvpanic-mmio", },
111 {}
112 };
113 MODULE_DEVICE_TABLE(of, pvpanic_mmio_match);
114
115 static const struct acpi_device_id pvpanic_device_ids[] = {
116 { "QEMU0001", 0 },
117 { "", 0 }
118 };
119 MODULE_DEVICE_TABLE(acpi, pvpanic_device_ids);
120
121 static struct platform_driver pvpanic_mmio_driver = {
122 .driver = {
123 .name = "pvpanic-mmio",
124 .of_match_table = pvpanic_mmio_match,
125 .acpi_match_table = pvpanic_device_ids,
126 .dev_groups = pvpanic_mmio_dev_groups,
127 },
128 .probe = pvpanic_mmio_probe,
129 };
130 module_platform_driver(pvpanic_mmio_driver);
131