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