ipmi_si_platform.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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. io.si_info = &ipmi_kcs_si_info;
  144. break;
  145. case SI_SMIC:
  146. io.si_info = &ipmi_smic_si_info;
  147. break;
  148. case SI_BT:
  149. io.si_info = &ipmi_bt_si_info;
  150. break;
  151. case SI_TYPE_INVALID: /* User disabled this in hardcode. */
  152. return -ENODEV;
  153. default:
  154. dev_err(&pdev->dev, "ipmi-type property is invalid\n");
  155. return -EINVAL;
  156. }
  157. io.regsize = DEFAULT_REGSIZE;
  158. rv = device_property_read_u8(&pdev->dev, "reg-size", &regsize);
  159. if (!rv)
  160. io.regsize = regsize;
  161. io.regshift = 0;
  162. rv = device_property_read_u8(&pdev->dev, "reg-shift", &regshift);
  163. if (!rv)
  164. io.regshift = regshift;
  165. if (!ipmi_get_info_from_resources(pdev, &io))
  166. return -EINVAL;
  167. rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr);
  168. if (rv)
  169. io.slave_addr = 0x20;
  170. else
  171. io.slave_addr = slave_addr;
  172. io.irq = platform_get_irq_optional(pdev, 0);
  173. if (io.irq > 0)
  174. io.irq_setup = ipmi_std_irq_setup;
  175. else
  176. io.irq = 0;
  177. io.dev = &pdev->dev;
  178. pr_info("ipmi_si: %s: %s %#lx regsize %d spacing %d irq %d\n",
  179. ipmi_addr_src_to_str(addr_source),
  180. (io.addr_space == IPMI_IO_ADDR_SPACE) ? "io" : "mem",
  181. io.addr_data, io.regsize, io.regspacing, io.irq);
  182. ipmi_si_add_smi(&io);
  183. return 0;
  184. }
  185. #ifdef CONFIG_OF
  186. static const struct of_device_id of_ipmi_match[] = {
  187. { .type = "ipmi", .compatible = "ipmi-kcs", .data = &ipmi_kcs_si_info },
  188. { .type = "ipmi", .compatible = "ipmi-smic", .data = &ipmi_smic_si_info },
  189. { .type = "ipmi", .compatible = "ipmi-bt", .data = &ipmi_bt_si_info },
  190. {}
  191. };
  192. MODULE_DEVICE_TABLE(of, of_ipmi_match);
  193. static int of_ipmi_probe(struct platform_device *pdev)
  194. {
  195. struct si_sm_io io;
  196. struct resource resource;
  197. const __be32 *regsize, *regspacing, *regshift;
  198. struct device_node *np = pdev->dev.of_node;
  199. int ret;
  200. int proplen;
  201. if (!si_tryopenfirmware)
  202. return -ENODEV;
  203. dev_info(&pdev->dev, "probing via device tree\n");
  204. if (!of_device_is_available(np))
  205. return -EINVAL;
  206. ret = of_address_to_resource(np, 0, &resource);
  207. if (ret) {
  208. dev_warn(&pdev->dev, "invalid address from OF\n");
  209. return ret;
  210. }
  211. regsize = of_get_property(np, "reg-size", &proplen);
  212. if (regsize && proplen != 4) {
  213. dev_warn(&pdev->dev, "invalid regsize from OF\n");
  214. return -EINVAL;
  215. }
  216. regspacing = of_get_property(np, "reg-spacing", &proplen);
  217. if (regspacing && proplen != 4) {
  218. dev_warn(&pdev->dev, "invalid regspacing from OF\n");
  219. return -EINVAL;
  220. }
  221. regshift = of_get_property(np, "reg-shift", &proplen);
  222. if (regshift && proplen != 4) {
  223. dev_warn(&pdev->dev, "invalid regshift from OF\n");
  224. return -EINVAL;
  225. }
  226. memset(&io, 0, sizeof(io));
  227. io.si_info = device_get_match_data(&pdev->dev);
  228. io.addr_source = SI_DEVICETREE;
  229. io.irq_setup = ipmi_std_irq_setup;
  230. ipmi_set_addr_data_and_space(&resource, &io);
  231. io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE;
  232. io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING;
  233. io.regshift = regshift ? be32_to_cpup(regshift) : 0;
  234. io.irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  235. io.dev = &pdev->dev;
  236. dev_dbg(&pdev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n",
  237. io.addr_data, io.regsize, io.regspacing, io.irq);
  238. return ipmi_si_add_smi(&io);
  239. }
  240. #else
  241. #define of_ipmi_match NULL
  242. static int of_ipmi_probe(struct platform_device *dev)
  243. {
  244. return -ENODEV;
  245. }
  246. #endif
  247. #ifdef CONFIG_ACPI
  248. static int find_slave_address(struct si_sm_io *io, int slave_addr)
  249. {
  250. #ifdef CONFIG_IPMI_DMI_DECODE
  251. if (!slave_addr)
  252. slave_addr = ipmi_dmi_get_slave_addr(io->si_info->type,
  253. io->addr_space,
  254. io->addr_data);
  255. #endif
  256. return slave_addr;
  257. }
  258. static int acpi_ipmi_probe(struct platform_device *pdev)
  259. {
  260. struct device *dev = &pdev->dev;
  261. struct si_sm_io io;
  262. acpi_handle handle;
  263. acpi_status status;
  264. unsigned long long tmp;
  265. struct resource *res;
  266. if (!si_tryacpi)
  267. return -ENODEV;
  268. handle = ACPI_HANDLE(dev);
  269. if (!handle)
  270. return -ENODEV;
  271. memset(&io, 0, sizeof(io));
  272. io.addr_source = SI_ACPI;
  273. dev_info(dev, "probing via ACPI\n");
  274. io.addr_info.acpi_info.acpi_handle = handle;
  275. /* _IFT tells us the interface type: KCS, BT, etc */
  276. status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp);
  277. if (ACPI_FAILURE(status)) {
  278. dev_err(dev, "Could not find ACPI IPMI interface type\n");
  279. return -EINVAL;
  280. }
  281. switch (tmp) {
  282. case 1:
  283. io.si_info = &ipmi_kcs_si_info;
  284. break;
  285. case 2:
  286. io.si_info = &ipmi_smic_si_info;
  287. break;
  288. case 3:
  289. io.si_info = &ipmi_bt_si_info;
  290. break;
  291. case 4: /* SSIF, just ignore */
  292. return -ENODEV;
  293. default:
  294. dev_info(dev, "unknown IPMI type %lld\n", tmp);
  295. return -EINVAL;
  296. }
  297. io.dev = dev;
  298. io.regsize = DEFAULT_REGSIZE;
  299. io.regshift = 0;
  300. res = ipmi_get_info_from_resources(pdev, &io);
  301. if (!res)
  302. return -EINVAL;
  303. /* If _GPE exists, use it; otherwise use standard interrupts */
  304. status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp);
  305. if (ACPI_SUCCESS(status)) {
  306. io.irq = tmp;
  307. io.irq_setup = acpi_gpe_irq_setup;
  308. } else {
  309. int irq = platform_get_irq_optional(pdev, 0);
  310. if (irq > 0) {
  311. io.irq = irq;
  312. io.irq_setup = ipmi_std_irq_setup;
  313. }
  314. }
  315. io.slave_addr = find_slave_address(&io, io.slave_addr);
  316. dev_info(dev, "%pR regsize %d spacing %d irq %d\n",
  317. res, io.regsize, io.regspacing, io.irq);
  318. request_module_nowait("acpi_ipmi");
  319. return ipmi_si_add_smi(&io);
  320. }
  321. static const struct acpi_device_id acpi_ipmi_match[] = {
  322. { "IPI0001", 0 },
  323. { },
  324. };
  325. MODULE_DEVICE_TABLE(acpi, acpi_ipmi_match);
  326. #else
  327. static int acpi_ipmi_probe(struct platform_device *dev)
  328. {
  329. return -ENODEV;
  330. }
  331. #endif
  332. static int ipmi_probe(struct platform_device *pdev)
  333. {
  334. if (pdev->dev.of_node && of_ipmi_probe(pdev) == 0)
  335. return 0;
  336. if (acpi_ipmi_probe(pdev) == 0)
  337. return 0;
  338. return platform_ipmi_probe(pdev);
  339. }
  340. static void ipmi_remove(struct platform_device *pdev)
  341. {
  342. ipmi_si_remove_by_dev(&pdev->dev);
  343. }
  344. static int pdev_match_name(struct device *dev, const void *data)
  345. {
  346. struct platform_device *pdev = to_platform_device(dev);
  347. const char *name = data;
  348. return strcmp(pdev->name, name) == 0;
  349. }
  350. void ipmi_remove_platform_device_by_name(char *name)
  351. {
  352. struct device *dev;
  353. while ((dev = bus_find_device(&platform_bus_type, NULL, name,
  354. pdev_match_name))) {
  355. struct platform_device *pdev = to_platform_device(dev);
  356. platform_device_unregister(pdev);
  357. put_device(dev);
  358. }
  359. }
  360. static const struct platform_device_id si_plat_ids[] = {
  361. { "dmi-ipmi-si", 0 },
  362. { "hardcode-ipmi-si", 0 },
  363. { "hotmod-ipmi-si", 0 },
  364. { }
  365. };
  366. struct platform_driver ipmi_platform_driver = {
  367. .driver = {
  368. .name = SI_DEVICE_NAME,
  369. .of_match_table = of_ipmi_match,
  370. .acpi_match_table = ACPI_PTR(acpi_ipmi_match),
  371. },
  372. .probe = ipmi_probe,
  373. .remove = ipmi_remove,
  374. .id_table = si_plat_ids
  375. };
  376. void ipmi_si_platform_init(void)
  377. {
  378. int rv = platform_driver_register(&ipmi_platform_driver);
  379. if (rv)
  380. pr_err("Unable to register driver: %d\n", rv);
  381. else
  382. platform_registered = true;
  383. }
  384. void ipmi_si_platform_shutdown(void)
  385. {
  386. if (platform_registered)
  387. platform_driver_unregister(&ipmi_platform_driver);
  388. }