xref: /linux/drivers/char/ipmi/ipmi_si_platform.c (revision e6a901a00822659181c93c86d8bbc2a17779fddc)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * ipmi_si_platform.c
4  *
5  * Handling for platform devices in IPMI (ACPI, OF, and things
6  * coming from the platform.
7  */
8 
9 #define pr_fmt(fmt) "ipmi_platform: " fmt
10 #define dev_fmt pr_fmt
11 
12 #include <linux/types.h>
13 #include <linux/module.h>
14 #include <linux/of.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>
20 #include "ipmi_si.h"
21 #include "ipmi_dmi.h"
22 
23 static bool platform_registered;
24 static bool si_tryplatform = true;
25 #ifdef CONFIG_ACPI
26 static bool          si_tryacpi = true;
27 #endif
28 #ifdef CONFIG_OF
29 static bool          si_tryopenfirmware = true;
30 #endif
31 #ifdef CONFIG_DMI
32 static bool          si_trydmi = true;
33 #else
34 static bool          si_trydmi = false;
35 #endif
36 
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");
40 #ifdef CONFIG_ACPI
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");
44 #endif
45 #ifdef CONFIG_OF
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");
49 #endif
50 #ifdef CONFIG_DMI
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");
54 #endif
55 
56 #ifdef CONFIG_ACPI
57 /* For GPE-type interrupts. */
58 static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
59 	u32 gpe_number, void *context)
60 {
61 	struct si_sm_io *io = context;
62 
63 	ipmi_si_irq_handler(io->irq, io->irq_handler_data);
64 	return ACPI_INTERRUPT_HANDLED;
65 }
66 
67 static void acpi_gpe_irq_cleanup(struct si_sm_io *io)
68 {
69 	if (!io->irq)
70 		return;
71 
72 	ipmi_irq_start_cleanup(io);
73 	acpi_remove_gpe_handler(NULL, io->irq, &ipmi_acpi_gpe);
74 }
75 
76 static int acpi_gpe_irq_setup(struct si_sm_io *io)
77 {
78 	acpi_status status;
79 
80 	if (!io->irq)
81 		return 0;
82 
83 	status = acpi_install_gpe_handler(NULL,
84 					  io->irq,
85 					  ACPI_GPE_LEVEL_TRIGGERED,
86 					  &ipmi_acpi_gpe,
87 					  io);
88 	if (ACPI_FAILURE(status)) {
89 		dev_warn(io->dev,
90 			 "Unable to claim ACPI GPE %d, running polled\n",
91 			 io->irq);
92 		io->irq = 0;
93 		return -EINVAL;
94 	}
95 
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);
99 	return 0;
100 }
101 #endif
102 
103 static void ipmi_set_addr_data_and_space(struct resource *r, struct si_sm_io *io)
104 {
105 	if (resource_type(r) == IORESOURCE_IO)
106 		io->addr_space = IPMI_IO_ADDR_SPACE;
107 	else
108 		io->addr_space = IPMI_MEM_ADDR_SPACE;
109 	io->addr_data = r->start;
110 }
111 
112 static struct resource *
113 ipmi_get_info_from_resources(struct platform_device *pdev,
114 			     struct si_sm_io *io)
115 {
116 	struct resource *res, *res_second;
117 
118 	res = platform_get_mem_or_io(pdev, 0);
119 	if (!res) {
120 		dev_err(&pdev->dev, "no I/O or memory address\n");
121 		return NULL;
122 	}
123 	ipmi_set_addr_data_and_space(res, io);
124 
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;
130 	}
131 
132 	return res;
133 }
134 
135 static int platform_ipmi_probe(struct platform_device *pdev)
136 {
137 	struct si_sm_io io;
138 	u8 type, slave_addr, addr_source, regsize, regshift;
139 	int rv;
140 
141 	rv = device_property_read_u8(&pdev->dev, "addr-source", &addr_source);
142 	if (rv)
143 		addr_source = SI_PLATFORM;
144 	if (addr_source >= SI_LAST)
145 		return -EINVAL;
146 
147 	if (addr_source == SI_SMBIOS) {
148 		if (!si_trydmi)
149 			return -ENODEV;
150 	} else if (addr_source != SI_HARDCODED) {
151 		if (!si_tryplatform)
152 			return -ENODEV;
153 	}
154 
155 	rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
156 	if (rv)
157 		return -ENODEV;
158 
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));
163 
164 	switch (type) {
165 	case SI_KCS:
166 	case SI_SMIC:
167 	case SI_BT:
168 		io.si_type = type;
169 		break;
170 	case SI_TYPE_INVALID: /* User disabled this in hardcode. */
171 		return -ENODEV;
172 	default:
173 		dev_err(&pdev->dev, "ipmi-type property is invalid\n");
174 		return -EINVAL;
175 	}
176 
177 	io.regsize = DEFAULT_REGSIZE;
178 	rv = device_property_read_u8(&pdev->dev, "reg-size", &regsize);
179 	if (!rv)
180 		io.regsize = regsize;
181 
182 	io.regshift = 0;
183 	rv = device_property_read_u8(&pdev->dev, "reg-shift", &regshift);
184 	if (!rv)
185 		io.regshift = regshift;
186 
187 	if (!ipmi_get_info_from_resources(pdev, &io))
188 		return -EINVAL;
189 
190 	rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
191 	if (rv)
192 		io.slave_addr = 0x20;
193 	else
194 		io.slave_addr = slave_addr;
195 
196 	io.irq = platform_get_irq_optional(pdev, 0);
197 	if (io.irq > 0)
198 		io.irq_setup = ipmi_std_irq_setup;
199 	else
200 		io.irq = 0;
201 
202 	io.dev = &pdev->dev;
203 
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);
208 
209 	ipmi_si_add_smi(&io);
210 
211 	return 0;
212 }
213 
214 #ifdef CONFIG_OF
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 },
222 	{},
223 };
224 MODULE_DEVICE_TABLE(of, of_ipmi_match);
225 
226 static int of_ipmi_probe(struct platform_device *pdev)
227 {
228 	struct si_sm_io io;
229 	struct resource resource;
230 	const __be32 *regsize, *regspacing, *regshift;
231 	struct device_node *np = pdev->dev.of_node;
232 	int ret;
233 	int proplen;
234 
235 	if (!si_tryopenfirmware)
236 		return -ENODEV;
237 
238 	dev_info(&pdev->dev, "probing via device tree\n");
239 
240 	if (!of_device_is_available(np))
241 		return -EINVAL;
242 
243 	ret = of_address_to_resource(np, 0, &resource);
244 	if (ret) {
245 		dev_warn(&pdev->dev, "invalid address from OF\n");
246 		return ret;
247 	}
248 
249 	regsize = of_get_property(np, "reg-size", &proplen);
250 	if (regsize && proplen != 4) {
251 		dev_warn(&pdev->dev, "invalid regsize from OF\n");
252 		return -EINVAL;
253 	}
254 
255 	regspacing = of_get_property(np, "reg-spacing", &proplen);
256 	if (regspacing && proplen != 4) {
257 		dev_warn(&pdev->dev, "invalid regspacing from OF\n");
258 		return -EINVAL;
259 	}
260 
261 	regshift = of_get_property(np, "reg-shift", &proplen);
262 	if (regshift && proplen != 4) {
263 		dev_warn(&pdev->dev, "invalid regshift from OF\n");
264 		return -EINVAL;
265 	}
266 
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;
271 
272 	ipmi_set_addr_data_and_space(&resource, &io);
273 
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;
277 
278 	io.irq		= irq_of_parse_and_map(pdev->dev.of_node, 0);
279 	io.dev		= &pdev->dev;
280 
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);
283 
284 	return ipmi_si_add_smi(&io);
285 }
286 #else
287 #define of_ipmi_match NULL
288 static int of_ipmi_probe(struct platform_device *dev)
289 {
290 	return -ENODEV;
291 }
292 #endif
293 
294 #ifdef CONFIG_ACPI
295 static int find_slave_address(struct si_sm_io *io, int slave_addr)
296 {
297 #ifdef CONFIG_IPMI_DMI_DECODE
298 	if (!slave_addr)
299 		slave_addr = ipmi_dmi_get_slave_addr(io->si_type,
300 						     io->addr_space,
301 						     io->addr_data);
302 #endif
303 
304 	return slave_addr;
305 }
306 
307 static int acpi_ipmi_probe(struct platform_device *pdev)
308 {
309 	struct device *dev = &pdev->dev;
310 	struct si_sm_io io;
311 	acpi_handle handle;
312 	acpi_status status;
313 	unsigned long long tmp;
314 	struct resource *res;
315 
316 	if (!si_tryacpi)
317 		return -ENODEV;
318 
319 	handle = ACPI_HANDLE(dev);
320 	if (!handle)
321 		return -ENODEV;
322 
323 	memset(&io, 0, sizeof(io));
324 	io.addr_source = SI_ACPI;
325 	dev_info(dev, "probing via ACPI\n");
326 
327 	io.addr_info.acpi_info.acpi_handle = handle;
328 
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");
333 		return -EINVAL;
334 	}
335 
336 	switch (tmp) {
337 	case 1:
338 		io.si_type = SI_KCS;
339 		break;
340 	case 2:
341 		io.si_type = SI_SMIC;
342 		break;
343 	case 3:
344 		io.si_type = SI_BT;
345 		break;
346 	case 4: /* SSIF, just ignore */
347 		return -ENODEV;
348 	default:
349 		dev_info(dev, "unknown IPMI type %lld\n", tmp);
350 		return -EINVAL;
351 	}
352 
353 	io.dev = dev;
354 	io.regsize = DEFAULT_REGSIZE;
355 	io.regshift = 0;
356 
357 	res = ipmi_get_info_from_resources(pdev, &io);
358 	if (!res)
359 		return -EINVAL;
360 
361 	/* If _GPE exists, use it; otherwise use standard interrupts */
362 	status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
363 	if (ACPI_SUCCESS(status)) {
364 		io.irq = tmp;
365 		io.irq_setup = acpi_gpe_irq_setup;
366 	} else {
367 		int irq = platform_get_irq_optional(pdev, 0);
368 
369 		if (irq > 0) {
370 			io.irq = irq;
371 			io.irq_setup = ipmi_std_irq_setup;
372 		}
373 	}
374 
375 	io.slave_addr = find_slave_address(&io, io.slave_addr);
376 
377 	dev_info(dev, "%pR regsize %d spacing %d irq %d\n",
378 		 res, io.regsize, io.regspacing, io.irq);
379 
380 	request_module_nowait("acpi_ipmi");
381 
382 	return ipmi_si_add_smi(&io);
383 }
384 
385 static const struct acpi_device_id acpi_ipmi_match[] = {
386 	{ "IPI0001", 0 },
387 	{ },
388 };
389 MODULE_DEVICE_TABLE(acpi, acpi_ipmi_match);
390 #else
391 static int acpi_ipmi_probe(struct platform_device *dev)
392 {
393 	return -ENODEV;
394 }
395 #endif
396 
397 static int ipmi_probe(struct platform_device *pdev)
398 {
399 	if (pdev->dev.of_node && of_ipmi_probe(pdev) == 0)
400 		return 0;
401 
402 	if (acpi_ipmi_probe(pdev) == 0)
403 		return 0;
404 
405 	return platform_ipmi_probe(pdev);
406 }
407 
408 static int ipmi_remove(struct platform_device *pdev)
409 {
410 	ipmi_si_remove_by_dev(&pdev->dev);
411 
412 	return 0;
413 }
414 
415 static int pdev_match_name(struct device *dev, const void *data)
416 {
417 	struct platform_device *pdev = to_platform_device(dev);
418 	const char *name = data;
419 
420 	return strcmp(pdev->name, name) == 0;
421 }
422 
423 void ipmi_remove_platform_device_by_name(char *name)
424 {
425 	struct device *dev;
426 
427 	while ((dev = bus_find_device(&platform_bus_type, NULL, name,
428 				      pdev_match_name))) {
429 		struct platform_device *pdev = to_platform_device(dev);
430 
431 		platform_device_unregister(pdev);
432 		put_device(dev);
433 	}
434 }
435 
436 static const struct platform_device_id si_plat_ids[] = {
437 	{ "dmi-ipmi-si", 0 },
438 	{ "hardcode-ipmi-si", 0 },
439 	{ "hotmod-ipmi-si", 0 },
440 	{ }
441 };
442 
443 struct platform_driver ipmi_platform_driver = {
444 	.driver = {
445 		.name = SI_DEVICE_NAME,
446 		.of_match_table = of_ipmi_match,
447 		.acpi_match_table = ACPI_PTR(acpi_ipmi_match),
448 	},
449 	.probe		= ipmi_probe,
450 	.remove		= ipmi_remove,
451 	.id_table       = si_plat_ids
452 };
453 
454 void ipmi_si_platform_init(void)
455 {
456 	int rv = platform_driver_register(&ipmi_platform_driver);
457 	if (rv)
458 		pr_err("Unable to register driver: %d\n", rv);
459 	else
460 		platform_registered = true;
461 }
462 
463 void ipmi_si_platform_shutdown(void)
464 {
465 	if (platform_registered)
466 		platform_driver_unregister(&ipmi_platform_driver);
467 }
468