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_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>
19 #include "ipmi_si.h"
20 #include "ipmi_dmi.h"
21 
22 static bool si_tryplatform = true;
23 #ifdef CONFIG_ACPI
24 static bool          si_tryacpi = true;
25 #endif
26 #ifdef CONFIG_OF
27 static bool          si_tryopenfirmware = true;
28 #endif
29 #ifdef CONFIG_DMI
30 static bool          si_trydmi = true;
31 #else
32 static bool          si_trydmi = false;
33 #endif
34 
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");
39 #ifdef CONFIG_ACPI
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");
43 #endif
44 #ifdef CONFIG_OF
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");
48 #endif
49 #ifdef CONFIG_DMI
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");
53 #endif
54 
55 #ifdef CONFIG_ACPI
56 /* For GPE-type interrupts. */
57 static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
58 	u32 gpe_number, void *context)
59 {
60 	struct si_sm_io *io = context;
61 
62 	ipmi_si_irq_handler(io->irq, io->irq_handler_data);
63 	return ACPI_INTERRUPT_HANDLED;
64 }
65 
66 static void acpi_gpe_irq_cleanup(struct si_sm_io *io)
67 {
68 	if (!io->irq)
69 		return;
70 
71 	ipmi_irq_start_cleanup(io);
72 	acpi_remove_gpe_handler(NULL, io->irq, &ipmi_acpi_gpe);
73 }
74 
75 static int acpi_gpe_irq_setup(struct si_sm_io *io)
76 {
77 	acpi_status status;
78 
79 	if (!io->irq)
80 		return 0;
81 
82 	status = acpi_install_gpe_handler(NULL,
83 					  io->irq,
84 					  ACPI_GPE_LEVEL_TRIGGERED,
85 					  &ipmi_acpi_gpe,
86 					  io);
87 	if (status != AE_OK) {
88 		dev_warn(io->dev,
89 			 "Unable to claim ACPI GPE %d, running polled\n",
90 			 io->irq);
91 		io->irq = 0;
92 		return -EINVAL;
93 	} else {
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);
97 		return 0;
98 	}
99 }
100 #endif
101 
102 static struct resource *
103 ipmi_get_info_from_resources(struct platform_device *pdev,
104 			     struct si_sm_io *io)
105 {
106 	struct resource *res, *res_second;
107 
108 	res = platform_get_resource(pdev, IORESOURCE_IO, 0);
109 	if (res) {
110 		io->addr_space = IPMI_IO_ADDR_SPACE;
111 	} else {
112 		res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
113 		if (res)
114 			io->addr_space = IPMI_MEM_ADDR_SPACE;
115 	}
116 	if (!res) {
117 		dev_err(&pdev->dev, "no I/O or memory address\n");
118 		return NULL;
119 	}
120 	io->addr_data = res->start;
121 
122 	io->regspacing = DEFAULT_REGSPACING;
123 	res_second = platform_get_resource(pdev,
124 			       (io->addr_space == IPMI_IO_ADDR_SPACE) ?
125 					IORESOURCE_IO : IORESOURCE_MEM,
126 			       1);
127 	if (res_second) {
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(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 	const struct of_device_id *match;
229 	struct si_sm_io io;
230 	struct resource resource;
231 	const __be32 *regsize, *regspacing, *regshift;
232 	struct device_node *np = pdev->dev.of_node;
233 	int ret;
234 	int proplen;
235 
236 	if (!si_tryopenfirmware)
237 		return -ENODEV;
238 
239 	dev_info(&pdev->dev, "probing via device tree\n");
240 
241 	match = of_match_device(of_ipmi_match, &pdev->dev);
242 	if (!match)
243 		return -ENODEV;
244 
245 	if (!of_device_is_available(np))
246 		return -EINVAL;
247 
248 	ret = of_address_to_resource(np, 0, &resource);
249 	if (ret) {
250 		dev_warn(&pdev->dev, "invalid address from OF\n");
251 		return ret;
252 	}
253 
254 	regsize = of_get_property(np, "reg-size", &proplen);
255 	if (regsize && proplen != 4) {
256 		dev_warn(&pdev->dev, "invalid regsize from OF\n");
257 		return -EINVAL;
258 	}
259 
260 	regspacing = of_get_property(np, "reg-spacing", &proplen);
261 	if (regspacing && proplen != 4) {
262 		dev_warn(&pdev->dev, "invalid regspacing from OF\n");
263 		return -EINVAL;
264 	}
265 
266 	regshift = of_get_property(np, "reg-shift", &proplen);
267 	if (regshift && proplen != 4) {
268 		dev_warn(&pdev->dev, "invalid regshift from OF\n");
269 		return -EINVAL;
270 	}
271 
272 	memset(&io, 0, sizeof(io));
273 	io.si_type	= (enum si_type) match->data;
274 	io.addr_source	= SI_DEVICETREE;
275 	io.irq_setup	= ipmi_std_irq_setup;
276 
277 	if (resource.flags & IORESOURCE_IO)
278 		io.addr_space = IPMI_IO_ADDR_SPACE;
279 	else
280 		io.addr_space = IPMI_MEM_ADDR_SPACE;
281 
282 	io.addr_data	= resource.start;
283 
284 	io.regsize	= regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
285 	io.regspacing	= regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
286 	io.regshift	= regshift ? be32_to_cpup(regshift) : 0;
287 
288 	io.irq		= irq_of_parse_and_map(pdev->dev.of_node, 0);
289 	io.dev		= &pdev->dev;
290 
291 	dev_dbg(&pdev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
292 		io.addr_data, io.regsize, io.regspacing, io.irq);
293 
294 	return ipmi_si_add_smi(&io);
295 }
296 #else
297 #define of_ipmi_match NULL
298 static int of_ipmi_probe(struct platform_device *dev)
299 {
300 	return -ENODEV;
301 }
302 #endif
303 
304 #ifdef CONFIG_ACPI
305 static int find_slave_address(struct si_sm_io *io, int slave_addr)
306 {
307 #ifdef CONFIG_IPMI_DMI_DECODE
308 	if (!slave_addr)
309 		slave_addr = ipmi_dmi_get_slave_addr(io->si_type,
310 						     io->addr_space,
311 						     io->addr_data);
312 #endif
313 
314 	return slave_addr;
315 }
316 
317 static int acpi_ipmi_probe(struct platform_device *pdev)
318 {
319 	struct si_sm_io io;
320 	acpi_handle handle;
321 	acpi_status status;
322 	unsigned long long tmp;
323 	struct resource *res;
324 	int rv = -EINVAL;
325 
326 	if (!si_tryacpi)
327 		return -ENODEV;
328 
329 	handle = ACPI_HANDLE(&pdev->dev);
330 	if (!handle)
331 		return -ENODEV;
332 
333 	memset(&io, 0, sizeof(io));
334 	io.addr_source = SI_ACPI;
335 	dev_info(&pdev->dev, "probing via ACPI\n");
336 
337 	io.addr_info.acpi_info.acpi_handle = handle;
338 
339 	/* _IFT tells us the interface type: KCS, BT, etc */
340 	status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
341 	if (ACPI_FAILURE(status)) {
342 		dev_err(&pdev->dev,
343 			"Could not find ACPI IPMI interface type\n");
344 		goto err_free;
345 	}
346 
347 	switch (tmp) {
348 	case 1:
349 		io.si_type = SI_KCS;
350 		break;
351 	case 2:
352 		io.si_type = SI_SMIC;
353 		break;
354 	case 3:
355 		io.si_type = SI_BT;
356 		break;
357 	case 4: /* SSIF, just ignore */
358 		rv = -ENODEV;
359 		goto err_free;
360 	default:
361 		dev_info(&pdev->dev, "unknown IPMI type %lld\n", tmp);
362 		goto err_free;
363 	}
364 
365 	io.regsize = DEFAULT_REGSIZE;
366 	io.regshift = 0;
367 
368 	res = ipmi_get_info_from_resources(pdev, &io);
369 	if (!res) {
370 		rv = -EINVAL;
371 		goto err_free;
372 	}
373 
374 	/* If _GPE exists, use it; otherwise use standard interrupts */
375 	status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
376 	if (ACPI_SUCCESS(status)) {
377 		io.irq = tmp;
378 		io.irq_setup = acpi_gpe_irq_setup;
379 	} else {
380 		int irq = platform_get_irq(pdev, 0);
381 
382 		if (irq > 0) {
383 			io.irq = irq;
384 			io.irq_setup = ipmi_std_irq_setup;
385 		}
386 	}
387 
388 	io.slave_addr = find_slave_address(&io, io.slave_addr);
389 
390 	io.dev = &pdev->dev;
391 
392 	dev_info(io.dev, "%pR regsize %d spacing %d irq %d\n",
393 		 res, io.regsize, io.regspacing, io.irq);
394 
395 	return ipmi_si_add_smi(&io);
396 
397 err_free:
398 	return rv;
399 }
400 
401 static const struct acpi_device_id acpi_ipmi_match[] = {
402 	{ "IPI0001", 0 },
403 	{ },
404 };
405 MODULE_DEVICE_TABLE(acpi, acpi_ipmi_match);
406 #else
407 static int acpi_ipmi_probe(struct platform_device *dev)
408 {
409 	return -ENODEV;
410 }
411 #endif
412 
413 static int ipmi_probe(struct platform_device *pdev)
414 {
415 	if (pdev->dev.of_node && of_ipmi_probe(pdev) == 0)
416 		return 0;
417 
418 	if (acpi_ipmi_probe(pdev) == 0)
419 		return 0;
420 
421 	return platform_ipmi_probe(pdev);
422 }
423 
424 static int ipmi_remove(struct platform_device *pdev)
425 {
426 	return ipmi_si_remove_by_dev(&pdev->dev);
427 }
428 
429 static int pdev_match_name(struct device *dev, void *data)
430 {
431 	struct platform_device *pdev = to_platform_device(dev);
432 	const char *name = data;
433 
434 	return strcmp(pdev->name, name) == 0;
435 }
436 
437 void ipmi_remove_platform_device_by_name(char *name)
438 {
439 	struct device *dev;
440 
441 	while ((dev = bus_find_device(&platform_bus_type, NULL, name,
442 				      pdev_match_name))) {
443 		struct platform_device *pdev = to_platform_device(dev);
444 
445 		platform_device_unregister(pdev);
446 	}
447 }
448 
449 static const struct platform_device_id si_plat_ids[] = {
450 	{ "dmi-ipmi-si", 0 },
451 	{ "hardcode-ipmi-si", 0 },
452 	{ "hotmod-ipmi-si", 0 },
453 	{ }
454 };
455 
456 struct platform_driver ipmi_platform_driver = {
457 	.driver = {
458 		.name = DEVICE_NAME,
459 		.of_match_table = of_ipmi_match,
460 		.acpi_match_table = ACPI_PTR(acpi_ipmi_match),
461 	},
462 	.probe		= ipmi_probe,
463 	.remove		= ipmi_remove,
464 	.id_table       = si_plat_ids
465 };
466 
467 void ipmi_si_platform_init(void)
468 {
469 	int rv = platform_driver_register(&ipmi_platform_driver);
470 	if (rv)
471 		pr_err("Unable to register driver: %d\n", rv);
472 }
473 
474 void ipmi_si_platform_shutdown(void)
475 {
476 	platform_driver_unregister(&ipmi_platform_driver);
477 }
478