1 // SPDX-License-Identifier: GPL-2.0
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.
7 #include <linux/ipmi.h>
8 #include <linux/init.h>
10 #include <linux/platform_device.h>
11 #include <linux/property.h>
14 struct ipmi_dmi_info {
19 struct ipmi_dmi_info *next;
22 static struct ipmi_dmi_info *ipmi_dmi_infos;
24 static int ipmi_dmi_nr __initdata;
26 static void __init dmi_add_platform_ipmi(unsigned long base_addr,
33 struct platform_device *pdev;
35 unsigned int num_r = 1, size;
36 struct property_entry p[4] = {
37 PROPERTY_ENTRY_U8("slave-addr", slave_addr),
38 PROPERTY_ENTRY_U8("ipmi-type", type),
39 PROPERTY_ENTRY_U16("i2c-addr", base_addr),
42 char *name, *override;
44 struct ipmi_dmi_info *info;
46 info = kmalloc(sizeof(*info), GFP_KERNEL);
48 pr_warn("ipmi:dmi: Could not allocate dmi info\n");
52 info->addr = base_addr;
53 info->slave_addr = slave_addr;
54 info->next = ipmi_dmi_infos;
55 ipmi_dmi_infos = info;
61 case IPMI_DMI_TYPE_SSIF:
62 name = "dmi-ipmi-ssif";
63 override = "ipmi_ssif";
67 case IPMI_DMI_TYPE_BT:
70 case IPMI_DMI_TYPE_KCS:
71 case IPMI_DMI_TYPE_SMIC:
75 pr_err("ipmi:dmi: Invalid IPMI type: %d", type);
79 pdev = platform_device_alloc(name, ipmi_dmi_nr);
81 pr_err("ipmi:dmi: Error allocation IPMI platform device");
84 pdev->driver_override = kasprintf(GFP_KERNEL, "%s",
86 if (!pdev->driver_override)
89 if (type == IPMI_DMI_TYPE_SSIF)
92 memset(r, 0, sizeof(r));
94 r[0].start = base_addr;
95 r[0].end = r[0].start + offset - 1;
96 r[0].name = "IPMI Address 1";
100 r[1].start = r[0].start + offset;
101 r[1].end = r[1].start + offset - 1;
102 r[1].name = "IPMI Address 2";
108 r[2].start = r[1].start + offset;
109 r[2].end = r[2].start + offset - 1;
110 r[2].name = "IPMI Address 3";
116 r[num_r].start = irq;
118 r[num_r].name = "IPMI IRQ";
119 r[num_r].flags = IORESOURCE_IRQ;
123 rv = platform_device_add_resources(pdev, r, num_r);
126 "ipmi:dmi: Unable to add resources: %d\n", rv);
131 rv = platform_device_add_properties(pdev, p);
134 "ipmi:dmi: Unable to add properties: %d\n", rv);
138 rv = platform_device_add(pdev);
140 dev_err(&pdev->dev, "ipmi:dmi: Unable to add device: %d\n", rv);
148 platform_device_put(pdev);
152 * Look up the slave address for a given interface. This is here
153 * because ACPI doesn't have a slave address while SMBIOS does, but we
154 * prefer using ACPI so the ACPI code can use the IPMI namespace.
155 * This function allows an ACPI-specified IPMI device to look up the
156 * slave address from the DMI table.
158 int ipmi_dmi_get_slave_addr(int type, u32 flags, unsigned long base_addr)
160 struct ipmi_dmi_info *info = ipmi_dmi_infos;
163 if (info->type == type &&
164 info->flags == flags &&
165 info->addr == base_addr)
166 return info->slave_addr;
172 EXPORT_SYMBOL(ipmi_dmi_get_slave_addr);
174 #define DMI_IPMI_MIN_LENGTH 0x10
175 #define DMI_IPMI_VER2_LENGTH 0x12
176 #define DMI_IPMI_TYPE 4
177 #define DMI_IPMI_SLAVEADDR 6
178 #define DMI_IPMI_ADDR 8
179 #define DMI_IPMI_ACCESS 0x10
180 #define DMI_IPMI_IRQ 0x11
181 #define DMI_IPMI_IO_MASK 0xfffe
183 static void __init dmi_decode_ipmi(const struct dmi_header *dm)
185 const u8 *data = (const u8 *) dm;
186 u32 flags = IORESOURCE_IO;
187 unsigned long base_addr;
193 if (len < DMI_IPMI_MIN_LENGTH)
196 type = data[DMI_IPMI_TYPE];
197 slave_addr = data[DMI_IPMI_SLAVEADDR];
199 memcpy(&base_addr, data + DMI_IPMI_ADDR, sizeof(unsigned long));
201 pr_err("Base address is zero, assuming no IPMI interface\n");
204 if (len >= DMI_IPMI_VER2_LENGTH) {
205 if (type == IPMI_DMI_TYPE_SSIF) {
208 base_addr = data[DMI_IPMI_ADDR] >> 1;
209 if (base_addr == 0) {
211 * Some broken systems put the I2C address in
212 * the slave address field. We try to
213 * accommodate them here.
215 base_addr = data[DMI_IPMI_SLAVEADDR] >> 1;
221 base_addr &= DMI_IPMI_IO_MASK;
224 flags = IORESOURCE_MEM;
228 * If bit 4 of byte 0x10 is set, then the lsb
229 * for the address is odd.
231 base_addr |= (data[DMI_IPMI_ACCESS] >> 4) & 1;
233 irq = data[DMI_IPMI_IRQ];
236 * The top two bits of byte 0x10 hold the
239 switch ((data[DMI_IPMI_ACCESS] >> 6) & 3) {
240 case 0: /* Byte boundaries */
243 case 1: /* 32-bit boundaries */
246 case 2: /* 16-byte boundaries */
250 pr_err("ipmi:dmi: Invalid offset: 0");
257 * Note that technically, the lower bit of the base
258 * address should be 1 if the address is I/O and 0 if
259 * the address is in memory. So many systems get that
260 * wrong (and all that I have seen are I/O) so we just
261 * ignore that bit and assume I/O. Systems that use
262 * memory should use the newer spec, anyway.
264 base_addr = base_addr & DMI_IPMI_IO_MASK;
268 dmi_add_platform_ipmi(base_addr, flags, slave_addr, irq,
272 static int __init scan_for_dmi_ipmi(void)
274 const struct dmi_device *dev = NULL;
276 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
277 dmi_decode_ipmi((const struct dmi_header *) dev->device_data);
281 subsys_initcall(scan_for_dmi_ipmi);