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>
15 #include <linux/of_address.h>
16 #include <linux/of_irq.h>
17 #include <linux/platform_device.h>
18 #include <linux/property.h>
19 #include <linux/acpi.h>
23 static bool platform_registered
;
24 static bool si_tryplatform
= true;
26 static bool si_tryacpi
= true;
29 static bool si_tryopenfirmware
= true;
32 static bool si_trydmi
= true;
34 static bool si_trydmi
= false;
37 module_param_named(tryplatform
, si_tryplatform
, bool, 0);
38 MODULE_PARM_DESC(tryplatform
,
39 "Setting this to zero will disable the default scan of the interfaces identified via platform interfaces besides ACPI, OpenFirmware, and DMI");
41 module_param_named(tryacpi
, si_tryacpi
, bool, 0);
42 MODULE_PARM_DESC(tryacpi
,
43 "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
46 module_param_named(tryopenfirmware
, si_tryopenfirmware
, bool, 0);
47 MODULE_PARM_DESC(tryopenfirmware
,
48 "Setting this to zero will disable the default scan of the interfaces identified via OpenFirmware");
51 module_param_named(trydmi
, si_trydmi
, bool, 0);
52 MODULE_PARM_DESC(trydmi
,
53 "Setting this to zero will disable the default scan of the interfaces identified via DMI");
57 /* For GPE-type interrupts. */
58 static u32
ipmi_acpi_gpe(acpi_handle gpe_device
,
59 u32 gpe_number
, void *context
)
61 struct si_sm_io
*io
= context
;
63 ipmi_si_irq_handler(io
->irq
, io
->irq_handler_data
);
64 return ACPI_INTERRUPT_HANDLED
;
67 static void acpi_gpe_irq_cleanup(struct si_sm_io
*io
)
72 ipmi_irq_start_cleanup(io
);
73 acpi_remove_gpe_handler(NULL
, io
->irq
, &ipmi_acpi_gpe
);
76 static int acpi_gpe_irq_setup(struct si_sm_io
*io
)
83 status
= acpi_install_gpe_handler(NULL
,
85 ACPI_GPE_LEVEL_TRIGGERED
,
88 if (ACPI_FAILURE(status
)) {
90 "Unable to claim ACPI GPE %d, running polled\n",
96 io
->irq_cleanup
= acpi_gpe_irq_cleanup
;
97 ipmi_irq_finish_setup(io
);
98 dev_info(io
->dev
, "Using ACPI GPE %d\n", io
->irq
);
103 static void ipmi_set_addr_data_and_space(struct resource
*r
, struct si_sm_io
*io
)
105 if (resource_type(r
) == IORESOURCE_IO
)
106 io
->addr_space
= IPMI_IO_ADDR_SPACE
;
108 io
->addr_space
= IPMI_MEM_ADDR_SPACE
;
109 io
->addr_data
= r
->start
;
112 static struct resource
*
113 ipmi_get_info_from_resources(struct platform_device
*pdev
,
116 struct resource
*res
, *res_second
;
118 res
= platform_get_mem_or_io(pdev
, 0);
120 dev_err(&pdev
->dev
, "no I/O or memory address\n");
123 ipmi_set_addr_data_and_space(res
, io
);
125 io
->regspacing
= DEFAULT_REGSPACING
;
126 res_second
= platform_get_mem_or_io(pdev
, 1);
127 if (res_second
&& resource_type(res_second
) == resource_type(res
)) {
128 if (res_second
->start
> io
->addr_data
)
129 io
->regspacing
= res_second
->start
- io
->addr_data
;
135 static int platform_ipmi_probe(struct platform_device
*pdev
)
138 u8 type
, slave_addr
, addr_source
, regsize
, regshift
;
141 rv
= device_property_read_u8(&pdev
->dev
, "addr-source", &addr_source
);
143 addr_source
= SI_PLATFORM
;
144 if (addr_source
>= SI_LAST
)
147 if (addr_source
== SI_SMBIOS
) {
150 } else if (addr_source
!= SI_HARDCODED
) {
155 rv
= device_property_read_u8(&pdev
->dev
, "ipmi-type", &type
);
159 memset(&io
, 0, sizeof(io
));
160 io
.addr_source
= addr_source
;
161 dev_info(&pdev
->dev
, "probing via %s\n",
162 ipmi_addr_src_to_str(addr_source
));
170 case SI_TYPE_INVALID
: /* User disabled this in hardcode. */
173 dev_err(&pdev
->dev
, "ipmi-type property is invalid\n");
177 io
.regsize
= DEFAULT_REGSIZE
;
178 rv
= device_property_read_u8(&pdev
->dev
, "reg-size", ®size
);
180 io
.regsize
= regsize
;
183 rv
= device_property_read_u8(&pdev
->dev
, "reg-shift", ®shift
);
185 io
.regshift
= regshift
;
187 if (!ipmi_get_info_from_resources(pdev
, &io
))
190 rv
= device_property_read_u8(&pdev
->dev
, "slave-addr", &slave_addr
);
192 io
.slave_addr
= 0x20;
194 io
.slave_addr
= slave_addr
;
196 io
.irq
= platform_get_irq_optional(pdev
, 0);
198 io
.irq_setup
= ipmi_std_irq_setup
;
204 pr_info("ipmi_si: %s: %s %#lx regsize %d spacing %d irq %d\n",
205 ipmi_addr_src_to_str(addr_source
),
206 (io
.addr_space
== IPMI_IO_ADDR_SPACE
) ? "io" : "mem",
207 io
.addr_data
, io
.regsize
, io
.regspacing
, io
.irq
);
209 ipmi_si_add_smi(&io
);
215 static const struct of_device_id of_ipmi_match
[] = {
216 { .type
= "ipmi", .compatible
= "ipmi-kcs",
217 .data
= (void *)(unsigned long) SI_KCS
},
218 { .type
= "ipmi", .compatible
= "ipmi-smic",
219 .data
= (void *)(unsigned long) SI_SMIC
},
220 { .type
= "ipmi", .compatible
= "ipmi-bt",
221 .data
= (void *)(unsigned long) SI_BT
},
224 MODULE_DEVICE_TABLE(of
, of_ipmi_match
);
226 static int of_ipmi_probe(struct platform_device
*pdev
)
229 struct resource resource
;
230 const __be32
*regsize
, *regspacing
, *regshift
;
231 struct device_node
*np
= pdev
->dev
.of_node
;
235 if (!si_tryopenfirmware
)
238 dev_info(&pdev
->dev
, "probing via device tree\n");
240 if (!of_device_is_available(np
))
243 ret
= of_address_to_resource(np
, 0, &resource
);
245 dev_warn(&pdev
->dev
, "invalid address from OF\n");
249 regsize
= of_get_property(np
, "reg-size", &proplen
);
250 if (regsize
&& proplen
!= 4) {
251 dev_warn(&pdev
->dev
, "invalid regsize from OF\n");
255 regspacing
= of_get_property(np
, "reg-spacing", &proplen
);
256 if (regspacing
&& proplen
!= 4) {
257 dev_warn(&pdev
->dev
, "invalid regspacing from OF\n");
261 regshift
= of_get_property(np
, "reg-shift", &proplen
);
262 if (regshift
&& proplen
!= 4) {
263 dev_warn(&pdev
->dev
, "invalid regshift from OF\n");
267 memset(&io
, 0, sizeof(io
));
268 io
.si_type
= (enum si_type
)device_get_match_data(&pdev
->dev
);
269 io
.addr_source
= SI_DEVICETREE
;
270 io
.irq_setup
= ipmi_std_irq_setup
;
272 ipmi_set_addr_data_and_space(&resource
, &io
);
274 io
.regsize
= regsize
? be32_to_cpup(regsize
) : DEFAULT_REGSIZE
;
275 io
.regspacing
= regspacing
? be32_to_cpup(regspacing
) : DEFAULT_REGSPACING
;
276 io
.regshift
= regshift
? be32_to_cpup(regshift
) : 0;
278 io
.irq
= irq_of_parse_and_map(pdev
->dev
.of_node
, 0);
281 dev_dbg(&pdev
->dev
, "addr 0x%lx regsize %d spacing %d irq %d\n",
282 io
.addr_data
, io
.regsize
, io
.regspacing
, io
.irq
);
284 return ipmi_si_add_smi(&io
);
287 #define of_ipmi_match NULL
288 static int of_ipmi_probe(struct platform_device
*dev
)
295 static int find_slave_address(struct si_sm_io
*io
, int slave_addr
)
297 #ifdef CONFIG_IPMI_DMI_DECODE
299 slave_addr
= ipmi_dmi_get_slave_addr(io
->si_type
,
307 static int acpi_ipmi_probe(struct platform_device
*pdev
)
309 struct device
*dev
= &pdev
->dev
;
313 unsigned long long tmp
;
314 struct resource
*res
;
319 handle
= ACPI_HANDLE(dev
);
323 memset(&io
, 0, sizeof(io
));
324 io
.addr_source
= SI_ACPI
;
325 dev_info(dev
, "probing via ACPI\n");
327 io
.addr_info
.acpi_info
.acpi_handle
= handle
;
329 /* _IFT tells us the interface type: KCS, BT, etc */
330 status
= acpi_evaluate_integer(handle
, "_IFT", NULL
, &tmp
);
331 if (ACPI_FAILURE(status
)) {
332 dev_err(dev
, "Could not find ACPI IPMI interface type\n");
341 io
.si_type
= SI_SMIC
;
346 case 4: /* SSIF, just ignore */
349 dev_info(dev
, "unknown IPMI type %lld\n", tmp
);
354 io
.regsize
= DEFAULT_REGSIZE
;
357 res
= ipmi_get_info_from_resources(pdev
, &io
);
361 /* If _GPE exists, use it; otherwise use standard interrupts */
362 status
= acpi_evaluate_integer(handle
, "_GPE", NULL
, &tmp
);
363 if (ACPI_SUCCESS(status
)) {
365 io
.irq_setup
= acpi_gpe_irq_setup
;
367 int irq
= platform_get_irq_optional(pdev
, 0);
371 io
.irq_setup
= ipmi_std_irq_setup
;
375 io
.slave_addr
= find_slave_address(&io
, io
.slave_addr
);
377 dev_info(dev
, "%pR regsize %d spacing %d irq %d\n",
378 res
, io
.regsize
, io
.regspacing
, io
.irq
);
380 request_module_nowait("acpi_ipmi");
382 return ipmi_si_add_smi(&io
);
385 static const struct acpi_device_id acpi_ipmi_match
[] = {
389 MODULE_DEVICE_TABLE(acpi
, acpi_ipmi_match
);
391 static int acpi_ipmi_probe(struct platform_device
*dev
)
397 static int ipmi_probe(struct platform_device
*pdev
)
399 if (pdev
->dev
.of_node
&& of_ipmi_probe(pdev
) == 0)
402 if (acpi_ipmi_probe(pdev
) == 0)
405 return platform_ipmi_probe(pdev
);
408 static void ipmi_remove(struct platform_device
*pdev
)
410 ipmi_si_remove_by_dev(&pdev
->dev
);
413 static int pdev_match_name(struct device
*dev
, const void *data
)
415 struct platform_device
*pdev
= to_platform_device(dev
);
416 const char *name
= data
;
418 return strcmp(pdev
->name
, name
) == 0;
421 void ipmi_remove_platform_device_by_name(char *name
)
425 while ((dev
= bus_find_device(&platform_bus_type
, NULL
, name
,
427 struct platform_device
*pdev
= to_platform_device(dev
);
429 platform_device_unregister(pdev
);
434 static const struct platform_device_id si_plat_ids
[] = {
435 { "dmi-ipmi-si", 0 },
436 { "hardcode-ipmi-si", 0 },
437 { "hotmod-ipmi-si", 0 },
441 struct platform_driver ipmi_platform_driver
= {
443 .name
= SI_DEVICE_NAME
,
444 .of_match_table
= of_ipmi_match
,
445 .acpi_match_table
= ACPI_PTR(acpi_ipmi_match
),
448 .remove
= ipmi_remove
,
449 .id_table
= si_plat_ids
452 void ipmi_si_platform_init(void)
454 int rv
= platform_driver_register(&ipmi_platform_driver
);
456 pr_err("Unable to register driver: %d\n", rv
);
458 platform_registered
= true;
461 void ipmi_si_platform_shutdown(void)
463 if (platform_registered
)
464 platform_driver_unregister(&ipmi_platform_driver
);