ipmi_dmi.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #define pr_fmt(fmt) "%s" fmt, "ipmi:dmi: "
  7. #define dev_fmt pr_fmt
  8. #include <linux/ipmi.h>
  9. #include <linux/init.h>
  10. #include <linux/dmi.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/property.h>
  13. #include "ipmi_dmi.h"
  14. #include "ipmi_plat_data.h"
  15. #define IPMI_DMI_TYPE_KCS 0x01
  16. #define IPMI_DMI_TYPE_SMIC 0x02
  17. #define IPMI_DMI_TYPE_BT 0x03
  18. #define IPMI_DMI_TYPE_SSIF 0x04
  19. struct ipmi_dmi_info {
  20. enum si_type si_type;
  21. unsigned int space; /* addr space for si, intf# for ssif */
  22. unsigned long addr;
  23. u8 slave_addr;
  24. struct ipmi_dmi_info *next;
  25. };
  26. static struct ipmi_dmi_info *ipmi_dmi_infos;
  27. static int ipmi_dmi_nr __initdata;
  28. static void __init dmi_add_platform_ipmi(unsigned long base_addr,
  29. unsigned int space,
  30. u8 slave_addr,
  31. int irq,
  32. int offset,
  33. int type)
  34. {
  35. const char *name;
  36. struct ipmi_dmi_info *info;
  37. struct ipmi_plat_data p;
  38. memset(&p, 0, sizeof(p));
  39. name = "dmi-ipmi-si";
  40. p.iftype = IPMI_PLAT_IF_SI;
  41. switch (type) {
  42. case IPMI_DMI_TYPE_SSIF:
  43. name = "dmi-ipmi-ssif";
  44. p.iftype = IPMI_PLAT_IF_SSIF;
  45. p.type = SI_TYPE_INVALID;
  46. break;
  47. case IPMI_DMI_TYPE_BT:
  48. p.type = SI_BT;
  49. break;
  50. case IPMI_DMI_TYPE_KCS:
  51. p.type = SI_KCS;
  52. break;
  53. case IPMI_DMI_TYPE_SMIC:
  54. p.type = SI_SMIC;
  55. break;
  56. default:
  57. pr_err("Invalid IPMI type: %d\n", type);
  58. return;
  59. }
  60. p.addr = base_addr;
  61. p.space = space;
  62. p.regspacing = offset;
  63. p.irq = irq;
  64. p.slave_addr = slave_addr;
  65. p.addr_source = SI_SMBIOS;
  66. info = kmalloc(sizeof(*info), GFP_KERNEL);
  67. if (!info) {
  68. pr_warn("Could not allocate dmi info\n");
  69. } else {
  70. info->si_type = p.type;
  71. info->space = space;
  72. info->addr = base_addr;
  73. info->slave_addr = slave_addr;
  74. info->next = ipmi_dmi_infos;
  75. ipmi_dmi_infos = info;
  76. }
  77. if (ipmi_platform_add(name, ipmi_dmi_nr, &p))
  78. ipmi_dmi_nr++;
  79. }
  80. /*
  81. * Look up the slave address for a given interface. This is here
  82. * because ACPI doesn't have a slave address while SMBIOS does, but we
  83. * prefer using ACPI so the ACPI code can use the IPMI namespace.
  84. * This function allows an ACPI-specified IPMI device to look up the
  85. * slave address from the DMI table.
  86. */
  87. int ipmi_dmi_get_slave_addr(enum si_type si_type, unsigned int space,
  88. unsigned long base_addr)
  89. {
  90. struct ipmi_dmi_info *info = ipmi_dmi_infos;
  91. while (info) {
  92. if (info->si_type == si_type &&
  93. info->space == space &&
  94. info->addr == base_addr)
  95. return info->slave_addr;
  96. info = info->next;
  97. }
  98. return 0;
  99. }
  100. EXPORT_SYMBOL(ipmi_dmi_get_slave_addr);
  101. #define DMI_IPMI_MIN_LENGTH 0x10
  102. #define DMI_IPMI_VER2_LENGTH 0x12
  103. #define DMI_IPMI_TYPE 4
  104. #define DMI_IPMI_SLAVEADDR 6
  105. #define DMI_IPMI_ADDR 8
  106. #define DMI_IPMI_ACCESS 0x10
  107. #define DMI_IPMI_IRQ 0x11
  108. #define DMI_IPMI_IO_MASK 0xfffe
  109. static void __init dmi_decode_ipmi(const struct dmi_header *dm)
  110. {
  111. const u8 *data = (const u8 *) dm;
  112. int space = IPMI_IO_ADDR_SPACE;
  113. unsigned long base_addr;
  114. u8 len = dm->length;
  115. u8 slave_addr;
  116. int irq = 0, offset = 0;
  117. int type;
  118. if (len < DMI_IPMI_MIN_LENGTH)
  119. return;
  120. type = data[DMI_IPMI_TYPE];
  121. slave_addr = data[DMI_IPMI_SLAVEADDR];
  122. memcpy(&base_addr, data + DMI_IPMI_ADDR, sizeof(unsigned long));
  123. if (!base_addr) {
  124. pr_err("Base address is zero, assuming no IPMI interface\n");
  125. return;
  126. }
  127. if (len >= DMI_IPMI_VER2_LENGTH) {
  128. if (type == IPMI_DMI_TYPE_SSIF) {
  129. space = 0; /* Match I2C interface 0. */
  130. base_addr = data[DMI_IPMI_ADDR] >> 1;
  131. if (base_addr == 0) {
  132. /*
  133. * Some broken systems put the I2C address in
  134. * the slave address field. We try to
  135. * accommodate them here.
  136. */
  137. base_addr = data[DMI_IPMI_SLAVEADDR] >> 1;
  138. slave_addr = 0;
  139. }
  140. } else {
  141. if (base_addr & 1) {
  142. /* I/O */
  143. base_addr &= DMI_IPMI_IO_MASK;
  144. } else {
  145. /* Memory */
  146. space = IPMI_MEM_ADDR_SPACE;
  147. }
  148. /*
  149. * If bit 4 of byte 0x10 is set, then the lsb
  150. * for the address is odd.
  151. */
  152. base_addr |= (data[DMI_IPMI_ACCESS] >> 4) & 1;
  153. irq = data[DMI_IPMI_IRQ];
  154. /*
  155. * The top two bits of byte 0x10 hold the
  156. * register spacing.
  157. */
  158. switch ((data[DMI_IPMI_ACCESS] >> 6) & 3) {
  159. case 0: /* Byte boundaries */
  160. offset = 1;
  161. break;
  162. case 1: /* 32-bit boundaries */
  163. offset = 4;
  164. break;
  165. case 2: /* 16-byte boundaries */
  166. offset = 16;
  167. break;
  168. default:
  169. pr_err("Invalid offset: 0\n");
  170. return;
  171. }
  172. }
  173. } else {
  174. /* Old DMI spec. */
  175. /*
  176. * Note that technically, the lower bit of the base
  177. * address should be 1 if the address is I/O and 0 if
  178. * the address is in memory. So many systems get that
  179. * wrong (and all that I have seen are I/O) so we just
  180. * ignore that bit and assume I/O. Systems that use
  181. * memory should use the newer spec, anyway.
  182. */
  183. base_addr = base_addr & DMI_IPMI_IO_MASK;
  184. offset = 1;
  185. }
  186. dmi_add_platform_ipmi(base_addr, space, slave_addr, irq,
  187. offset, type);
  188. }
  189. static int __init scan_for_dmi_ipmi(void)
  190. {
  191. const struct dmi_device *dev = NULL;
  192. while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
  193. dmi_decode_ipmi((const struct dmi_header *) dev->device_data);
  194. return 0;
  195. }
  196. subsys_initcall(scan_for_dmi_ipmi);