ipmi_si_platform.c 10 KB

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