ipmi_dmi.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * A hack to create a platform device from a DMI entry. This will
  4. * allow autoloading of the IPMI drive based on SMBIOS entries.
  5. */
  6. #include <linux/ipmi.h>
  7. #include <linux/init.h>
  8. #include <linux/dmi.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/property.h>
  11. #include "ipmi_si_sm.h"
  12. #include "ipmi_dmi.h"
  13. #define IPMI_DMI_TYPE_KCS 0x01
  14. #define IPMI_DMI_TYPE_SMIC 0x02
  15. #define IPMI_DMI_TYPE_BT 0x03
  16. #define IPMI_DMI_TYPE_SSIF 0x04
  17. struct ipmi_dmi_info {
  18. enum si_type si_type;
  19. u32 flags;
  20. unsigned long addr;
  21. u8 slave_addr;
  22. struct ipmi_dmi_info *next;
  23. };
  24. static struct ipmi_dmi_info *ipmi_dmi_infos;
  25. static int ipmi_dmi_nr __initdata;
  26. static void __init dmi_add_platform_ipmi(unsigned long base_addr,
  27. u32 flags,
  28. u8 slave_addr,
  29. int irq,
  30. int offset,
  31. int type)
  32. {
  33. struct platform_device *pdev;
  34. struct resource r[4];
  35. unsigned int num_r = 1, size;
  36. struct property_entry p[5];
  37. unsigned int pidx = 0;
  38. char *name, *override;
  39. int rv;
  40. enum si_type si_type;
  41. struct ipmi_dmi_info *info;
  42. memset(p, 0, sizeof(p));
  43. name = "dmi-ipmi-si";
  44. override = "ipmi_si";
  45. switch (type) {
  46. case IPMI_DMI_TYPE_SSIF:
  47. name = "dmi-ipmi-ssif";
  48. override = "ipmi_ssif";
  49. offset = 1;
  50. size = 1;
  51. si_type = SI_TYPE_INVALID;
  52. break;
  53. case IPMI_DMI_TYPE_BT:
  54. size = 3;
  55. si_type = SI_BT;
  56. break;
  57. case IPMI_DMI_TYPE_KCS:
  58. size = 2;
  59. si_type = SI_KCS;
  60. break;
  61. case IPMI_DMI_TYPE_SMIC:
  62. size = 2;
  63. si_type = SI_SMIC;
  64. break;
  65. default:
  66. pr_err("ipmi:dmi: Invalid IPMI type: %d\n", type);
  67. return;
  68. }
  69. if (si_type != SI_TYPE_INVALID)
  70. p[pidx++] = PROPERTY_ENTRY_U8("ipmi-type", si_type);
  71. p[pidx++] = PROPERTY_ENTRY_U8("slave-addr", slave_addr);
  72. p[pidx++] = PROPERTY_ENTRY_U8("addr-source", SI_SMBIOS);
  73. info = kmalloc(sizeof(*info), GFP_KERNEL);
  74. if (!info) {
  75. pr_warn("ipmi:dmi: Could not allocate dmi info\n");
  76. } else {
  77. info->si_type = si_type;
  78. info->flags = flags;
  79. info->addr = base_addr;
  80. info->slave_addr = slave_addr;
  81. info->next = ipmi_dmi_infos;
  82. ipmi_dmi_infos = info;
  83. }
  84. pdev = platform_device_alloc(name, ipmi_dmi_nr);
  85. if (!pdev) {
  86. pr_err("ipmi:dmi: Error allocation IPMI platform device\n");
  87. return;
  88. }
  89. pdev->driver_override = kasprintf(GFP_KERNEL, "%s",
  90. override);
  91. if (!pdev->driver_override)
  92. goto err;
  93. if (type == IPMI_DMI_TYPE_SSIF) {
  94. p[pidx++] = PROPERTY_ENTRY_U16("i2c-addr", base_addr);
  95. goto add_properties;
  96. }
  97. memset(r, 0, sizeof(r));
  98. r[0].start = base_addr;
  99. r[0].end = r[0].start + offset - 1;
  100. r[0].name = "IPMI Address 1";
  101. r[0].flags = flags;
  102. if (size > 1) {
  103. r[1].start = r[0].start + offset;
  104. r[1].end = r[1].start + offset - 1;
  105. r[1].name = "IPMI Address 2";
  106. r[1].flags = flags;
  107. num_r++;
  108. }
  109. if (size > 2) {
  110. r[2].start = r[1].start + offset;
  111. r[2].end = r[2].start + offset - 1;
  112. r[2].name = "IPMI Address 3";
  113. r[2].flags = flags;
  114. num_r++;
  115. }
  116. if (irq) {
  117. r[num_r].start = irq;
  118. r[num_r].end = irq;
  119. r[num_r].name = "IPMI IRQ";
  120. r[num_r].flags = IORESOURCE_IRQ;
  121. num_r++;
  122. }
  123. rv = platform_device_add_resources(pdev, r, num_r);
  124. if (rv) {
  125. dev_err(&pdev->dev,
  126. "ipmi:dmi: Unable to add resources: %d\n", rv);
  127. goto err;
  128. }
  129. add_properties:
  130. rv = platform_device_add_properties(pdev, p);
  131. if (rv) {
  132. dev_err(&pdev->dev,
  133. "ipmi:dmi: Unable to add properties: %d\n", rv);
  134. goto err;
  135. }
  136. rv = platform_device_add(pdev);
  137. if (rv) {
  138. dev_err(&pdev->dev, "ipmi:dmi: Unable to add device: %d\n", rv);
  139. goto err;
  140. }
  141. ipmi_dmi_nr++;
  142. return;
  143. err:
  144. platform_device_put(pdev);
  145. }
  146. /*
  147. * Look up the slave address for a given interface. This is here
  148. * because ACPI doesn't have a slave address while SMBIOS does, but we
  149. * prefer using ACPI so the ACPI code can use the IPMI namespace.
  150. * This function allows an ACPI-specified IPMI device to look up the
  151. * slave address from the DMI table.
  152. */
  153. int ipmi_dmi_get_slave_addr(enum si_type si_type, u32 flags,
  154. unsigned long base_addr)
  155. {
  156. struct ipmi_dmi_info *info = ipmi_dmi_infos;
  157. while (info) {
  158. if (info->si_type == si_type &&
  159. info->flags == flags &&
  160. info->addr == base_addr)
  161. return info->slave_addr;
  162. info = info->next;
  163. }
  164. return 0;
  165. }
  166. EXPORT_SYMBOL(ipmi_dmi_get_slave_addr);
  167. #define DMI_IPMI_MIN_LENGTH 0x10
  168. #define DMI_IPMI_VER2_LENGTH 0x12
  169. #define DMI_IPMI_TYPE 4
  170. #define DMI_IPMI_SLAVEADDR 6
  171. #define DMI_IPMI_ADDR 8
  172. #define DMI_IPMI_ACCESS 0x10
  173. #define DMI_IPMI_IRQ 0x11
  174. #define DMI_IPMI_IO_MASK 0xfffe
  175. static void __init dmi_decode_ipmi(const struct dmi_header *dm)
  176. {
  177. const u8 *data = (const u8 *) dm;
  178. u32 flags = IORESOURCE_IO;
  179. unsigned long base_addr;
  180. u8 len = dm->length;
  181. u8 slave_addr;
  182. int irq = 0, offset;
  183. int type;
  184. if (len < DMI_IPMI_MIN_LENGTH)
  185. return;
  186. type = data[DMI_IPMI_TYPE];
  187. slave_addr = data[DMI_IPMI_SLAVEADDR];
  188. memcpy(&base_addr, data + DMI_IPMI_ADDR, sizeof(unsigned long));
  189. if (len >= DMI_IPMI_VER2_LENGTH) {
  190. if (type == IPMI_DMI_TYPE_SSIF) {
  191. offset = 0;
  192. flags = 0;
  193. base_addr = data[DMI_IPMI_ADDR] >> 1;
  194. if (base_addr == 0) {
  195. /*
  196. * Some broken systems put the I2C address in
  197. * the slave address field. We try to
  198. * accommodate them here.
  199. */
  200. base_addr = data[DMI_IPMI_SLAVEADDR] >> 1;
  201. slave_addr = 0;
  202. }
  203. } else {
  204. if (base_addr & 1) {
  205. /* I/O */
  206. base_addr &= DMI_IPMI_IO_MASK;
  207. } else {
  208. /* Memory */
  209. flags = IORESOURCE_MEM;
  210. }
  211. /*
  212. * If bit 4 of byte 0x10 is set, then the lsb
  213. * for the address is odd.
  214. */
  215. base_addr |= (data[DMI_IPMI_ACCESS] >> 4) & 1;
  216. irq = data[DMI_IPMI_IRQ];
  217. /*
  218. * The top two bits of byte 0x10 hold the
  219. * register spacing.
  220. */
  221. switch ((data[DMI_IPMI_ACCESS] >> 6) & 3) {
  222. case 0: /* Byte boundaries */
  223. offset = 1;
  224. break;
  225. case 1: /* 32-bit boundaries */
  226. offset = 4;
  227. break;
  228. case 2: /* 16-byte boundaries */
  229. offset = 16;
  230. break;
  231. default:
  232. pr_err("ipmi:dmi: Invalid offset: 0\n");
  233. return;
  234. }
  235. }
  236. } else {
  237. /* Old DMI spec. */
  238. /*
  239. * Note that technically, the lower bit of the base
  240. * address should be 1 if the address is I/O and 0 if
  241. * the address is in memory. So many systems get that
  242. * wrong (and all that I have seen are I/O) so we just
  243. * ignore that bit and assume I/O. Systems that use
  244. * memory should use the newer spec, anyway.
  245. */
  246. base_addr = base_addr & DMI_IPMI_IO_MASK;
  247. offset = 1;
  248. }
  249. dmi_add_platform_ipmi(base_addr, flags, slave_addr, irq,
  250. offset, type);
  251. }
  252. static int __init scan_for_dmi_ipmi(void)
  253. {
  254. const struct dmi_device *dev = NULL;
  255. while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
  256. dmi_decode_ipmi((const struct dmi_header *) dev->device_data);
  257. return 0;
  258. }
  259. subsys_initcall(scan_for_dmi_ipmi);