1 // SPDX-License-Identifier: GPL-2.0+
5 * Handling for platform devices in IPMI (ACPI, OF, and things
6 * coming from the platform.
9 #define pr_fmt(fmt) "ipmi_platform: " fmt
10 #define dev_fmt pr_fmt
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/of_device.h>
15 #include <linux/of_platform.h>
16 #include <linux/of_address.h>
17 #include <linux/of_irq.h>
18 #include <linux/acpi.h>
22 static bool si_tryplatform = true;
24 static bool si_tryacpi = true;
27 static bool si_tryopenfirmware = true;
30 static bool si_trydmi = true;
32 static bool si_trydmi = false;
35 module_param_named(tryplatform, si_tryplatform, bool, 0);
36 MODULE_PARM_DESC(tryplatform, "Setting this to zero will disable the"
37 " default scan of the interfaces identified via platform"
38 " interfaces besides ACPI, OpenFirmware, and DMI");
40 module_param_named(tryacpi, si_tryacpi, bool, 0);
41 MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the"
42 " default scan of the interfaces identified via ACPI");
45 module_param_named(tryopenfirmware, si_tryopenfirmware, bool, 0);
46 MODULE_PARM_DESC(tryopenfirmware, "Setting this to zero will disable the"
47 " default scan of the interfaces identified via OpenFirmware");
50 module_param_named(trydmi, si_trydmi, bool, 0);
51 MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the"
52 " default scan of the interfaces identified via DMI");
56 /* For GPE-type interrupts. */
57 static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
58 u32 gpe_number, void *context)
60 struct si_sm_io *io = context;
62 ipmi_si_irq_handler(io->irq, io->irq_handler_data);
63 return ACPI_INTERRUPT_HANDLED;
66 static void acpi_gpe_irq_cleanup(struct si_sm_io *io)
71 ipmi_irq_start_cleanup(io);
72 acpi_remove_gpe_handler(NULL, io->irq, &ipmi_acpi_gpe);
75 static int acpi_gpe_irq_setup(struct si_sm_io *io)
82 status = acpi_install_gpe_handler(NULL,
84 ACPI_GPE_LEVEL_TRIGGERED,
87 if (status != AE_OK) {
89 "Unable to claim ACPI GPE %d, running polled\n",
94 io->irq_cleanup = acpi_gpe_irq_cleanup;
95 ipmi_irq_finish_setup(io);
96 dev_info(io->dev, "Using ACPI GPE %d\n", io->irq);
102 static struct resource *
103 ipmi_get_info_from_resources(struct platform_device *pdev,
106 struct resource *res, *res_second;
108 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
110 io->addr_type = IPMI_IO_ADDR_SPACE;
112 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
114 io->addr_type = IPMI_MEM_ADDR_SPACE;
117 dev_err(&pdev->dev, "no I/O or memory address\n");
120 io->addr_data = res->start;
122 io->regspacing = DEFAULT_REGSPACING;
123 res_second = platform_get_resource(pdev,
124 (io->addr_type == IPMI_IO_ADDR_SPACE) ?
125 IORESOURCE_IO : IORESOURCE_MEM,
128 if (res_second->start > io->addr_data)
129 io->regspacing = res_second->start - io->addr_data;
131 io->regsize = DEFAULT_REGSIZE;
137 static int platform_ipmi_probe(struct platform_device *pdev)
140 u8 type, slave_addr, addr_source;
143 rv = device_property_read_u8(&pdev->dev, "addr-source", &addr_source);
145 addr_source = SI_PLATFORM;
146 if (addr_source >= SI_LAST)
149 if (addr_source == SI_SMBIOS) {
157 rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
161 memset(&io, 0, sizeof(io));
162 io.addr_source = addr_source;
163 dev_info(&pdev->dev, "probing via %s\n",
164 ipmi_addr_src_to_str(addr_source));
173 dev_err(&pdev->dev, "ipmi-type property is invalid\n");
177 if (!ipmi_get_info_from_resources(pdev, &io))
180 rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
182 dev_warn(&pdev->dev, "device has no slave-addr property\n");
183 io.slave_addr = 0x20;
185 io.slave_addr = slave_addr;
188 io.irq = platform_get_irq(pdev, 0);
190 io.irq_setup = ipmi_std_irq_setup;
196 pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
197 (io.addr_type == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
198 io.addr_data, io.regsize, io.regspacing, io.irq);
200 ipmi_si_add_smi(&io);
206 static const struct of_device_id of_ipmi_match[] = {
207 { .type = "ipmi", .compatible = "ipmi-kcs",
208 .data = (void *)(unsigned long) SI_KCS },
209 { .type = "ipmi", .compatible = "ipmi-smic",
210 .data = (void *)(unsigned long) SI_SMIC },
211 { .type = "ipmi", .compatible = "ipmi-bt",
212 .data = (void *)(unsigned long) SI_BT },
215 MODULE_DEVICE_TABLE(of, of_ipmi_match);
217 static int of_ipmi_probe(struct platform_device *pdev)
219 const struct of_device_id *match;
221 struct resource resource;
222 const __be32 *regsize, *regspacing, *regshift;
223 struct device_node *np = pdev->dev.of_node;
227 if (!si_tryopenfirmware)
230 dev_info(&pdev->dev, "probing via device tree\n");
232 match = of_match_device(of_ipmi_match, &pdev->dev);
236 if (!of_device_is_available(np))
239 ret = of_address_to_resource(np, 0, &resource);
241 dev_warn(&pdev->dev, "invalid address from OF\n");
245 regsize = of_get_property(np, "reg-size", &proplen);
246 if (regsize && proplen != 4) {
247 dev_warn(&pdev->dev, "invalid regsize from OF\n");
251 regspacing = of_get_property(np, "reg-spacing", &proplen);
252 if (regspacing && proplen != 4) {
253 dev_warn(&pdev->dev, "invalid regspacing from OF\n");
257 regshift = of_get_property(np, "reg-shift", &proplen);
258 if (regshift && proplen != 4) {
259 dev_warn(&pdev->dev, "invalid regshift from OF\n");
263 memset(&io, 0, sizeof(io));
264 io.si_type = (enum si_type) match->data;
265 io.addr_source = SI_DEVICETREE;
266 io.irq_setup = ipmi_std_irq_setup;
268 if (resource.flags & IORESOURCE_IO)
269 io.addr_type = IPMI_IO_ADDR_SPACE;
271 io.addr_type = IPMI_MEM_ADDR_SPACE;
273 io.addr_data = resource.start;
275 io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
276 io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
277 io.regshift = regshift ? be32_to_cpup(regshift) : 0;
279 io.irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
282 dev_dbg(&pdev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
283 io.addr_data, io.regsize, io.regspacing, io.irq);
285 return ipmi_si_add_smi(&io);
288 #define of_ipmi_match NULL
289 static int of_ipmi_probe(struct platform_device *dev)
296 static int find_slave_address(struct si_sm_io *io, int slave_addr)
298 #ifdef CONFIG_IPMI_DMI_DECODE
300 u32 flags = IORESOURCE_IO;
302 if (io->addr_type == IPMI_MEM_ADDR_SPACE)
303 flags = IORESOURCE_MEM;
305 slave_addr = ipmi_dmi_get_slave_addr(io->si_type, flags,
313 static int acpi_ipmi_probe(struct platform_device *pdev)
318 unsigned long long tmp;
319 struct resource *res;
325 handle = ACPI_HANDLE(&pdev->dev);
329 memset(&io, 0, sizeof(io));
330 io.addr_source = SI_ACPI;
331 dev_info(&pdev->dev, "probing via ACPI\n");
333 io.addr_info.acpi_info.acpi_handle = handle;
335 /* _IFT tells us the interface type: KCS, BT, etc */
336 status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
337 if (ACPI_FAILURE(status)) {
339 "Could not find ACPI IPMI interface type\n");
348 io.si_type = SI_SMIC;
353 case 4: /* SSIF, just ignore */
357 dev_info(&pdev->dev, "unknown IPMI type %lld\n", tmp);
361 res = ipmi_get_info_from_resources(pdev, &io);
367 /* If _GPE exists, use it; otherwise use standard interrupts */
368 status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
369 if (ACPI_SUCCESS(status)) {
371 io.irq_setup = acpi_gpe_irq_setup;
373 int irq = platform_get_irq(pdev, 0);
377 io.irq_setup = ipmi_std_irq_setup;
381 io.slave_addr = find_slave_address(&io, io.slave_addr);
385 dev_info(io.dev, "%pR regsize %d spacing %d irq %d\n",
386 res, io.regsize, io.regspacing, io.irq);
388 return ipmi_si_add_smi(&io);
394 static const struct acpi_device_id acpi_ipmi_match[] = {
398 MODULE_DEVICE_TABLE(acpi, acpi_ipmi_match);
400 static int acpi_ipmi_probe(struct platform_device *dev)
406 static int ipmi_probe(struct platform_device *pdev)
408 if (pdev->dev.of_node && of_ipmi_probe(pdev) == 0)
411 if (acpi_ipmi_probe(pdev) == 0)
414 return platform_ipmi_probe(pdev);
417 static int ipmi_remove(struct platform_device *pdev)
419 return ipmi_si_remove_by_dev(&pdev->dev);
422 static const struct platform_device_id si_plat_ids[] = {
423 { "dmi-ipmi-si", 0 },
427 struct platform_driver ipmi_platform_driver = {
430 .of_match_table = of_ipmi_match,
431 .acpi_match_table = ACPI_PTR(acpi_ipmi_match),
434 .remove = ipmi_remove,
435 .id_table = si_plat_ids
438 void ipmi_si_platform_init(void)
440 int rv = platform_driver_register(&ipmi_platform_driver);
442 pr_err("Unable to register driver: %d\n", rv);
445 void ipmi_si_platform_shutdown(void)
447 platform_driver_unregister(&ipmi_platform_driver);