1 // SPDX-License-Identifier: GPL-2.0+
5 * Handling for platform devices in IPMI (ACPI, OF, and things
6 * coming from the platform.
8 #include <linux/types.h>
9 #include <linux/module.h>
10 #include <linux/of_device.h>
11 #include <linux/of_platform.h>
12 #include <linux/of_address.h>
13 #include <linux/of_irq.h>
14 #include <linux/acpi.h>
18 #define PFX "ipmi_platform: "
20 static bool si_tryplatform
= true;
22 static bool si_tryacpi
= true;
25 static bool si_tryopenfirmware
= true;
28 static bool si_trydmi
= true;
30 static bool si_trydmi
= false;
33 module_param_named(tryplatform
, si_tryplatform
, bool, 0);
34 MODULE_PARM_DESC(tryplatform
, "Setting this to zero will disable the"
35 " default scan of the interfaces identified via platform"
36 " interfaces besides ACPI, OpenFirmware, and DMI");
38 module_param_named(tryacpi
, si_tryacpi
, bool, 0);
39 MODULE_PARM_DESC(tryacpi
, "Setting this to zero will disable the"
40 " default scan of the interfaces identified via ACPI");
43 module_param_named(tryopenfirmware
, si_tryopenfirmware
, bool, 0);
44 MODULE_PARM_DESC(tryopenfirmware
, "Setting this to zero will disable the"
45 " default scan of the interfaces identified via OpenFirmware");
48 module_param_named(trydmi
, si_trydmi
, bool, 0);
49 MODULE_PARM_DESC(trydmi
, "Setting this to zero will disable the"
50 " default scan of the interfaces identified via DMI");
54 /* For GPE-type interrupts. */
55 static u32
ipmi_acpi_gpe(acpi_handle gpe_device
,
56 u32 gpe_number
, void *context
)
58 struct si_sm_io
*io
= context
;
60 ipmi_si_irq_handler(io
->irq
, io
->irq_handler_data
);
61 return ACPI_INTERRUPT_HANDLED
;
64 static void acpi_gpe_irq_cleanup(struct si_sm_io
*io
)
69 ipmi_irq_start_cleanup(io
);
70 acpi_remove_gpe_handler(NULL
, io
->irq
, &ipmi_acpi_gpe
);
73 static int acpi_gpe_irq_setup(struct si_sm_io
*io
)
80 status
= acpi_install_gpe_handler(NULL
,
82 ACPI_GPE_LEVEL_TRIGGERED
,
85 if (status
!= AE_OK
) {
87 "Unable to claim ACPI GPE %d, running polled\n",
92 io
->irq_cleanup
= acpi_gpe_irq_cleanup
;
93 ipmi_irq_finish_setup(io
);
94 dev_info(io
->dev
, "Using ACPI GPE %d\n", io
->irq
);
100 static struct resource
*
101 ipmi_get_info_from_resources(struct platform_device
*pdev
,
104 struct resource
*res
, *res_second
;
106 res
= platform_get_resource(pdev
, IORESOURCE_IO
, 0);
108 io
->addr_type
= IPMI_IO_ADDR_SPACE
;
110 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
112 io
->addr_type
= IPMI_MEM_ADDR_SPACE
;
115 dev_err(&pdev
->dev
, "no I/O or memory address\n");
118 io
->addr_data
= res
->start
;
120 io
->regspacing
= DEFAULT_REGSPACING
;
121 res_second
= platform_get_resource(pdev
,
122 (io
->addr_type
== IPMI_IO_ADDR_SPACE
) ?
123 IORESOURCE_IO
: IORESOURCE_MEM
,
126 if (res_second
->start
> io
->addr_data
)
127 io
->regspacing
= res_second
->start
- io
->addr_data
;
129 io
->regsize
= DEFAULT_REGSIZE
;
135 static int platform_ipmi_probe(struct platform_device
*pdev
)
138 u8 type
, slave_addr
, addr_source
;
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
) {
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
, PFX
"probing via %s\n",
162 ipmi_addr_src_to_str(addr_source
));
171 dev_err(&pdev
->dev
, "ipmi-type property is invalid\n");
175 if (!ipmi_get_info_from_resources(pdev
, &io
))
178 rv
= device_property_read_u8(&pdev
->dev
, "slave-addr", &slave_addr
);
180 dev_warn(&pdev
->dev
, "device has no slave-addr property\n");
181 io
.slave_addr
= 0x20;
183 io
.slave_addr
= slave_addr
;
186 io
.irq
= platform_get_irq(pdev
, 0);
188 io
.irq_setup
= ipmi_std_irq_setup
;
194 pr_info("ipmi_si: SMBIOS: %s %#lx regsize %d spacing %d irq %d\n",
195 (io
.addr_type
== IPMI_IO_ADDR_SPACE
) ? "io" : "mem",
196 io
.addr_data
, io
.regsize
, io
.regspacing
, io
.irq
);
198 ipmi_si_add_smi(&io
);
204 static const struct of_device_id of_ipmi_match
[] = {
205 { .type
= "ipmi", .compatible
= "ipmi-kcs",
206 .data
= (void *)(unsigned long) SI_KCS
},
207 { .type
= "ipmi", .compatible
= "ipmi-smic",
208 .data
= (void *)(unsigned long) SI_SMIC
},
209 { .type
= "ipmi", .compatible
= "ipmi-bt",
210 .data
= (void *)(unsigned long) SI_BT
},
213 MODULE_DEVICE_TABLE(of
, of_ipmi_match
);
215 static int of_ipmi_probe(struct platform_device
*pdev
)
217 const struct of_device_id
*match
;
219 struct resource resource
;
220 const __be32
*regsize
, *regspacing
, *regshift
;
221 struct device_node
*np
= pdev
->dev
.of_node
;
225 if (!si_tryopenfirmware
)
228 dev_info(&pdev
->dev
, "probing via device tree\n");
230 match
= of_match_device(of_ipmi_match
, &pdev
->dev
);
234 if (!of_device_is_available(np
))
237 ret
= of_address_to_resource(np
, 0, &resource
);
239 dev_warn(&pdev
->dev
, PFX
"invalid address from OF\n");
243 regsize
= of_get_property(np
, "reg-size", &proplen
);
244 if (regsize
&& proplen
!= 4) {
245 dev_warn(&pdev
->dev
, PFX
"invalid regsize from OF\n");
249 regspacing
= of_get_property(np
, "reg-spacing", &proplen
);
250 if (regspacing
&& proplen
!= 4) {
251 dev_warn(&pdev
->dev
, PFX
"invalid regspacing from OF\n");
255 regshift
= of_get_property(np
, "reg-shift", &proplen
);
256 if (regshift
&& proplen
!= 4) {
257 dev_warn(&pdev
->dev
, PFX
"invalid regshift from OF\n");
261 memset(&io
, 0, sizeof(io
));
262 io
.si_type
= (enum si_type
) match
->data
;
263 io
.addr_source
= SI_DEVICETREE
;
264 io
.irq_setup
= ipmi_std_irq_setup
;
266 if (resource
.flags
& IORESOURCE_IO
)
267 io
.addr_type
= IPMI_IO_ADDR_SPACE
;
269 io
.addr_type
= IPMI_MEM_ADDR_SPACE
;
271 io
.addr_data
= resource
.start
;
273 io
.regsize
= regsize
? be32_to_cpup(regsize
) : DEFAULT_REGSIZE
;
274 io
.regspacing
= regspacing
? be32_to_cpup(regspacing
) : DEFAULT_REGSPACING
;
275 io
.regshift
= regshift
? be32_to_cpup(regshift
) : 0;
277 io
.irq
= irq_of_parse_and_map(pdev
->dev
.of_node
, 0);
280 dev_dbg(&pdev
->dev
, "addr 0x%lx regsize %d spacing %d irq %d\n",
281 io
.addr_data
, io
.regsize
, io
.regspacing
, io
.irq
);
283 return ipmi_si_add_smi(&io
);
286 #define of_ipmi_match NULL
287 static int of_ipmi_probe(struct platform_device
*dev
)
294 static int find_slave_address(struct si_sm_io
*io
, int slave_addr
)
296 #ifdef CONFIG_IPMI_DMI_DECODE
298 u32 flags
= IORESOURCE_IO
;
300 if (io
->addr_type
== IPMI_MEM_ADDR_SPACE
)
301 flags
= IORESOURCE_MEM
;
303 slave_addr
= ipmi_dmi_get_slave_addr(io
->si_type
, flags
,
311 static int acpi_ipmi_probe(struct platform_device
*pdev
)
316 unsigned long long tmp
;
317 struct resource
*res
;
323 handle
= ACPI_HANDLE(&pdev
->dev
);
327 memset(&io
, 0, sizeof(io
));
328 io
.addr_source
= SI_ACPI
;
329 dev_info(&pdev
->dev
, PFX
"probing via ACPI\n");
331 io
.addr_info
.acpi_info
.acpi_handle
= handle
;
333 /* _IFT tells us the interface type: KCS, BT, etc */
334 status
= acpi_evaluate_integer(handle
, "_IFT", NULL
, &tmp
);
335 if (ACPI_FAILURE(status
)) {
337 "Could not find ACPI IPMI interface type\n");
346 io
.si_type
= SI_SMIC
;
351 case 4: /* SSIF, just ignore */
355 dev_info(&pdev
->dev
, "unknown IPMI type %lld\n", tmp
);
359 res
= ipmi_get_info_from_resources(pdev
, &io
);
365 /* If _GPE exists, use it; otherwise use standard interrupts */
366 status
= acpi_evaluate_integer(handle
, "_GPE", NULL
, &tmp
);
367 if (ACPI_SUCCESS(status
)) {
369 io
.irq_setup
= acpi_gpe_irq_setup
;
371 int irq
= platform_get_irq(pdev
, 0);
375 io
.irq_setup
= ipmi_std_irq_setup
;
379 io
.slave_addr
= find_slave_address(&io
, io
.slave_addr
);
383 dev_info(io
.dev
, "%pR regsize %d spacing %d irq %d\n",
384 res
, io
.regsize
, io
.regspacing
, io
.irq
);
386 return ipmi_si_add_smi(&io
);
392 static const struct acpi_device_id acpi_ipmi_match
[] = {
396 MODULE_DEVICE_TABLE(acpi
, acpi_ipmi_match
);
398 static int acpi_ipmi_probe(struct platform_device
*dev
)
404 static int ipmi_probe(struct platform_device
*pdev
)
406 if (pdev
->dev
.of_node
&& of_ipmi_probe(pdev
) == 0)
409 if (acpi_ipmi_probe(pdev
) == 0)
412 return platform_ipmi_probe(pdev
);
415 static int ipmi_remove(struct platform_device
*pdev
)
417 return ipmi_si_remove_by_dev(&pdev
->dev
);
420 struct platform_driver ipmi_platform_driver
= {
423 .of_match_table
= of_ipmi_match
,
424 .acpi_match_table
= ACPI_PTR(acpi_ipmi_match
),
427 .remove
= ipmi_remove
,
430 void ipmi_si_platform_init(void)
432 int rv
= platform_driver_register(&ipmi_platform_driver
);
434 pr_err(PFX
"Unable to register driver: %d\n", rv
);
437 void ipmi_si_platform_shutdown(void)
439 platform_driver_unregister(&ipmi_platform_driver
);