ipmi_si_platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. #define pr_fmt(fmt) "ipmi_platform: " fmt
  9. #define dev_fmt pr_fmt
  10. #include <linux/types.h>
  11. #include <linux/module.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/property.h>
  17. #include <linux/acpi.h>
  18. #include "ipmi_si.h"
  19. #include "ipmi_dmi.h"
  20. static bool platform_registered;
  21. static bool si_tryplatform = true;
  22. #ifdef CONFIG_ACPI
  23. static bool si_tryacpi = true;
  24. #endif
  25. #ifdef CONFIG_OF
  26. static bool si_tryopenfirmware = true;
  27. #endif
  28. #ifdef CONFIG_DMI
  29. static bool si_trydmi = true;
  30. #else
  31. static bool si_trydmi = false;
  32. #endif
  33. module_param_named(tryplatform, si_tryplatform, bool, 0);
  34. MODULE_PARM_DESC(tryplatform,
  35. "Setting this to zero will disable the default scan of the interfaces identified via platform interfaces besides ACPI, OpenFirmware, and DMI");
  36. #ifdef CONFIG_ACPI
  37. module_param_named(tryacpi, si_tryacpi, bool, 0);
  38. MODULE_PARM_DESC(tryacpi,
  39. "Setting this to zero will disable the default scan of the interfaces identified via ACPI");
  40. #endif
  41. #ifdef CONFIG_OF
  42. module_param_named(tryopenfirmware, si_tryopenfirmware, bool, 0);
  43. MODULE_PARM_DESC(tryopenfirmware,
  44. "Setting this to zero will disable the default scan of the interfaces identified via OpenFirmware");
  45. #endif
  46. #ifdef CONFIG_DMI
  47. module_param_named(trydmi, si_trydmi, bool, 0);
  48. MODULE_PARM_DESC(trydmi,
  49. "Setting this to zero will disable the default scan of the interfaces identified via DMI");
  50. #endif
  51. #ifdef CONFIG_ACPI
  52. /* For GPE-type interrupts. */
  53. static u32 ipmi_acpi_gpe(acpi_handle gpe_device,
  54. u32 gpe_number, void *context)
  55. {
  56. struct si_sm_io *io = context;
  57. ipmi_si_irq_handler(io->irq, io->irq_handler_data);
  58. return ACPI_INTERRUPT_HANDLED;
  59. }
  60. static void acpi_gpe_irq_cleanup(struct si_sm_io *io)
  61. {
  62. if (!io->irq)
  63. return;
  64. ipmi_irq_start_cleanup(io);
  65. acpi_remove_gpe_handler(NULL, io->irq, &ipmi_acpi_gpe);
  66. }
  67. static int acpi_gpe_irq_setup(struct si_sm_io *io)
  68. {
  69. acpi_status status;
  70. if (!io->irq)
  71. return 0;
  72. status = acpi_install_gpe_handler(NULL,
  73. io->irq,
  74. ACPI_GPE_LEVEL_TRIGGERED,
  75. &ipmi_acpi_gpe,
  76. io);
  77. if (ACPI_FAILURE(status)) {
  78. dev_warn(io->dev,
  79. "Unable to claim ACPI GPE %d, running polled\n",
  80. io->irq);
  81. io->irq = 0;
  82. return -EINVAL;
  83. }
  84. io->irq_cleanup = acpi_gpe_irq_cleanup;
  85. ipmi_irq_finish_setup(io);
  86. dev_info(io->dev, "Using ACPI GPE %d\n", io->irq);
  87. return 0;
  88. }
  89. #endif
  90. static void ipmi_set_addr_data_and_space(struct resource *r, struct si_sm_io *io)
  91. {
  92. if (resource_type(r) == IORESOURCE_IO)
  93. io->addr_space = IPMI_IO_ADDR_SPACE;
  94. else
  95. io->addr_space = IPMI_MEM_ADDR_SPACE;
  96. io->addr_data = r->start;
  97. }
  98. static struct resource *
  99. ipmi_get_info_from_resources(struct platform_device *pdev,
  100. struct si_sm_io *io)
  101. {
  102. struct resource *res, *res_second;
  103. res = platform_get_mem_or_io(pdev, 0);
  104. if (!res) {
  105. dev_err(&pdev->dev, "no I/O or memory address\n");
  106. return NULL;
  107. }
  108. ipmi_set_addr_data_and_space(res, io);
  109. io->regspacing = DEFAULT_REGSPACING;
  110. res_second = platform_get_mem_or_io(pdev, 1);
  111. if (res_second && resource_type(res_second) == resource_type(res)) {
  112. if (res_second->start > io->addr_data)
  113. io->regspacing = res_second->start - io->addr_data;
  114. }
  115. return res;
  116. }
  117. static int platform_ipmi_probe(struct platform_device *pdev)
  118. {
  119. struct si_sm_io io;
  120. u8 type, slave_addr, addr_source, regsize, regshift;
  121. int rv;
  122. rv = device_property_read_u8(&pdev->dev, "addr-source", &addr_source);
  123. if (rv)
  124. addr_source = SI_PLATFORM;
  125. if (addr_source >= SI_LAST)
  126. return -EINVAL;
  127. if (addr_source == SI_SMBIOS) {
  128. if (!si_trydmi)
  129. return -ENODEV;
  130. } else if (addr_source != SI_HARDCODED) {
  131. if (!si_tryplatform)
  132. return -ENODEV;
  133. }
  134. rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type);
  135. if (rv)
  136. return -ENODEV;
  137. memset(&io, 0, sizeof(io));
  138. io.addr_source = addr_source;
  139. dev_info(&pdev->dev, "probing via %s\n",
  140. ipmi_addr_src_to_str(addr_source));
  141. switch (type) {
  142. case SI_KCS:
  143. case SI_SMIC:
  144. case SI_BT:
  145. io.si_type = type;
  146. break;
  147. case SI_TYPE_INVALID: /* User disabled this in hardcode. */
  148. return -ENODEV;
  149. default:
  150. dev_err(&pdev->dev, "ipmi-type property is invalid\n");
  151. return -EINVAL;
  152. }
  153. io.regsize = DEFAULT_REGSIZE;
  154. rv = device_property_read_u8(&pdev->dev, "reg-size", &regsize);
  155. if (!rv)
  156. io.regsize = regsize;
  157. io.regshift = 0;
  158. rv = device_property_read_u8(&pdev->dev, "reg-shift", &regshift);
  159. if (!rv)
  160. io.regshift = regshift;
  161. if (!ipmi_get_info_from_resources(pdev, &io))
  162. return -EINVAL;
  163. rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
  164. if (rv)
  165. io.slave_addr = 0x20;
  166. else
  167. io.slave_addr = slave_addr;
  168. io.irq = platform_get_irq_optional(pdev, 0);
  169. if (io.irq > 0)
  170. io.irq_setup = ipmi_std_irq_setup;
  171. else
  172. io.irq = 0;
  173. io.dev = &pdev->dev;
  174. pr_info("ipmi_si: %s: %s %#lx regsize %d spacing %d irq %d\n",
  175. ipmi_addr_src_to_str(addr_source),
  176. (io.addr_space == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
  177. io.addr_data, io.regsize, io.regspacing, io.irq);
  178. ipmi_si_add_smi(&io);
  179. return 0;
  180. }
  181. #ifdef CONFIG_OF
  182. static const struct of_device_id of_ipmi_match[] = {
  183. { .type = "ipmi", .compatible = "ipmi-kcs",
  184. .data = (void *)(unsigned long) SI_KCS },
  185. { .type = "ipmi", .compatible = "ipmi-smic",
  186. .data = (void *)(unsigned long) SI_SMIC },
  187. { .type = "ipmi", .compatible = "ipmi-bt",
  188. .data = (void *)(unsigned long) SI_BT },
  189. {},
  190. };
  191. MODULE_DEVICE_TABLE(of, of_ipmi_match);
  192. static int of_ipmi_probe(struct platform_device *pdev)
  193. {
  194. struct si_sm_io io;
  195. struct resource resource;
  196. const __be32 *regsize, *regspacing, *regshift;
  197. struct device_node *np = pdev->dev.of_node;
  198. int ret;
  199. int proplen;
  200. if (!si_tryopenfirmware)
  201. return -ENODEV;
  202. dev_info(&pdev->dev, "probing via device tree\n");
  203. if (!of_device_is_available(np))
  204. return -EINVAL;
  205. ret = of_address_to_resource(np, 0, &resource);
  206. if (ret) {
  207. dev_warn(&pdev->dev, "invalid address from OF\n");
  208. return ret;
  209. }
  210. regsize = of_get_property(np, "reg-size", &proplen);
  211. if (regsize && proplen != 4) {
  212. dev_warn(&pdev->dev, "invalid regsize from OF\n");
  213. return -EINVAL;
  214. }
  215. regspacing = of_get_property(np, "reg-spacing", &proplen);
  216. if (regspacing && proplen != 4) {
  217. dev_warn(&pdev->dev, "invalid regspacing from OF\n");
  218. return -EINVAL;
  219. }
  220. regshift = of_get_property(np, "reg-shift", &proplen);
  221. if (regshift && proplen != 4) {
  222. dev_warn(&pdev->dev, "invalid regshift from OF\n");
  223. return -EINVAL;
  224. }
  225. memset(&io, 0, sizeof(io));
  226. io.si_type = (enum si_type)device_get_match_data(&pdev->dev);
  227. io.addr_source = SI_DEVICETREE;
  228. io.irq_setup = ipmi_std_irq_setup;
  229. ipmi_set_addr_data_and_space(&resource, &io);
  230. io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
  231. io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
  232. io.regshift = regshift ? be32_to_cpup(regshift) : 0;
  233. io.irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  234. io.dev = &pdev->dev;
  235. dev_dbg(&pdev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
  236. io.addr_data, io.regsize, io.regspacing, io.irq);
  237. return ipmi_si_add_smi(&io);
  238. }
  239. #else
  240. #define of_ipmi_match NULL
  241. static int of_ipmi_probe(struct platform_device *dev)
  242. {
  243. return -ENODEV;
  244. }
  245. #endif
  246. #ifdef CONFIG_ACPI
  247. static int find_slave_address(struct si_sm_io *io, int slave_addr)
  248. {
  249. #ifdef CONFIG_IPMI_DMI_DECODE
  250. if (!slave_addr)
  251. slave_addr = ipmi_dmi_get_slave_addr(io->si_type,
  252. io->addr_space,
  253. io->addr_data);
  254. #endif
  255. return slave_addr;
  256. }
  257. static int acpi_ipmi_probe(struct platform_device *pdev)
  258. {
  259. struct device *dev = &pdev->dev;
  260. struct si_sm_io io;
  261. acpi_handle handle;
  262. acpi_status status;
  263. unsigned long long tmp;
  264. struct resource *res;
  265. if (!si_tryacpi)
  266. return -ENODEV;
  267. handle = ACPI_HANDLE(dev);
  268. if (!handle)
  269. return -ENODEV;
  270. memset(&io, 0, sizeof(io));
  271. io.addr_source = SI_ACPI;
  272. dev_info(dev, "probing via ACPI\n");
  273. io.addr_info.acpi_info.acpi_handle = handle;
  274. /* _IFT tells us the interface type: KCS, BT, etc */
  275. status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
  276. if (ACPI_FAILURE(status)) {
  277. dev_err(dev, "Could not find ACPI IPMI interface type\n");
  278. return -EINVAL;
  279. }
  280. switch (tmp) {
  281. case 1:
  282. io.si_type = SI_KCS;
  283. break;
  284. case 2:
  285. io.si_type = SI_SMIC;
  286. break;
  287. case 3:
  288. io.si_type = SI_BT;
  289. break;
  290. case 4: /* SSIF, just ignore */
  291. return -ENODEV;
  292. default:
  293. dev_info(dev, "unknown IPMI type %lld\n", tmp);
  294. return -EINVAL;
  295. }
  296. io.dev = dev;
  297. io.regsize = DEFAULT_REGSIZE;
  298. io.regshift = 0;
  299. res = ipmi_get_info_from_resources(pdev, &io);
  300. if (!res)
  301. return -EINVAL;
  302. /* If _GPE exists, use it; otherwise use standard interrupts */
  303. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  304. if (ACPI_SUCCESS(status)) {
  305. io.irq = tmp;
  306. io.irq_setup = acpi_gpe_irq_setup;
  307. } else {
  308. int irq = platform_get_irq_optional(pdev, 0);
  309. if (irq > 0) {
  310. io.irq = irq;
  311. io.irq_setup = ipmi_std_irq_setup;
  312. }
  313. }
  314. io.slave_addr = find_slave_address(&io, io.slave_addr);
  315. dev_info(dev, "%pR regsize %d spacing %d irq %d\n",
  316. res, io.regsize, io.regspacing, io.irq);
  317. request_module_nowait("acpi_ipmi");
  318. return ipmi_si_add_smi(&io);
  319. }
  320. static const struct acpi_device_id acpi_ipmi_match[] = {
  321. { "IPI0001", 0 },
  322. { },
  323. };
  324. MODULE_DEVICE_TABLE(acpi, acpi_ipmi_match);
  325. #else
  326. static int acpi_ipmi_probe(struct platform_device *dev)
  327. {
  328. return -ENODEV;
  329. }
  330. #endif
  331. static int ipmi_probe(struct platform_device *pdev)
  332. {
  333. if (pdev->dev.of_node && of_ipmi_probe(pdev) == 0)
  334. return 0;
  335. if (acpi_ipmi_probe(pdev) == 0)
  336. return 0;
  337. return platform_ipmi_probe(pdev);
  338. }
  339. static void ipmi_remove(struct platform_device *pdev)
  340. {
  341. ipmi_si_remove_by_dev(&pdev->dev);
  342. }
  343. static int pdev_match_name(struct device *dev, const void *data)
  344. {
  345. struct platform_device *pdev = to_platform_device(dev);
  346. const char *name = data;
  347. return strcmp(pdev->name, name) == 0;
  348. }
  349. void ipmi_remove_platform_device_by_name(char *name)
  350. {
  351. struct device *dev;
  352. while ((dev = bus_find_device(&platform_bus_type, NULL, name,
  353. pdev_match_name))) {
  354. struct platform_device *pdev = to_platform_device(dev);
  355. platform_device_unregister(pdev);
  356. put_device(dev);
  357. }
  358. }
  359. static const struct platform_device_id si_plat_ids[] = {
  360. { "dmi-ipmi-si", 0 },
  361. { "hardcode-ipmi-si", 0 },
  362. { "hotmod-ipmi-si", 0 },
  363. { }
  364. };
  365. struct platform_driver ipmi_platform_driver = {
  366. .driver = {
  367. .name = SI_DEVICE_NAME,
  368. .of_match_table = of_ipmi_match,
  369. .acpi_match_table = ACPI_PTR(acpi_ipmi_match),
  370. },
  371. .probe = ipmi_probe,
  372. .remove_new = ipmi_remove,
  373. .id_table = si_plat_ids
  374. };
  375. void ipmi_si_platform_init(void)
  376. {
  377. int rv = platform_driver_register(&ipmi_platform_driver);
  378. if (rv)
  379. pr_err("Unable to register driver: %d\n", rv);
  380. else
  381. platform_registered = true;
  382. }
  383. void ipmi_si_platform_shutdown(void)
  384. {
  385. if (platform_registered)
  386. platform_driver_unregister(&ipmi_platform_driver);
  387. }