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>
12 #include "ipmi_si_sm.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
20 struct ipmi_dmi_info {
25 struct ipmi_dmi_info *next;
28 static struct ipmi_dmi_info *ipmi_dmi_infos;
30 static int ipmi_dmi_nr __initdata;
32 static void __init dmi_add_platform_ipmi(unsigned long base_addr,
39 struct platform_device *pdev;
41 unsigned int num_r = 1, size;
42 struct property_entry p[5];
43 unsigned int pidx = 0;
44 char *name, *override;
47 struct ipmi_dmi_info *info;
49 memset(p, 0, sizeof(p));
54 case IPMI_DMI_TYPE_SSIF:
55 name = "dmi-ipmi-ssif";
56 override = "ipmi_ssif";
59 si_type = SI_TYPE_INVALID;
61 case IPMI_DMI_TYPE_BT:
65 case IPMI_DMI_TYPE_KCS:
69 case IPMI_DMI_TYPE_SMIC:
74 pr_err("ipmi:dmi: Invalid IPMI type: %d\n", type);
78 if (si_type != SI_TYPE_INVALID)
79 p[pidx++] = PROPERTY_ENTRY_U8("ipmi-type", si_type);
81 p[pidx++] = PROPERTY_ENTRY_U8("slave-addr", slave_addr);
82 p[pidx++] = PROPERTY_ENTRY_U8("addr-source", SI_SMBIOS);
84 info = kmalloc(sizeof(*info), GFP_KERNEL);
86 pr_warn("ipmi:dmi: Could not allocate dmi info\n");
88 info->si_type = si_type;
90 info->addr = base_addr;
91 info->slave_addr = slave_addr;
92 info->next = ipmi_dmi_infos;
93 ipmi_dmi_infos = info;
96 pdev = platform_device_alloc(name, ipmi_dmi_nr);
98 pr_err("ipmi:dmi: Error allocation IPMI platform device\n");
101 pdev->driver_override = kasprintf(GFP_KERNEL, "%s",
103 if (!pdev->driver_override)
106 if (type == IPMI_DMI_TYPE_SSIF) {
107 p[pidx++] = PROPERTY_ENTRY_U16("i2c-addr", base_addr);
111 memset(r, 0, sizeof(r));
113 r[0].start = base_addr;
114 r[0].end = r[0].start + offset - 1;
115 r[0].name = "IPMI Address 1";
119 r[1].start = r[0].start + offset;
120 r[1].end = r[1].start + offset - 1;
121 r[1].name = "IPMI Address 2";
127 r[2].start = r[1].start + offset;
128 r[2].end = r[2].start + offset - 1;
129 r[2].name = "IPMI Address 3";
135 r[num_r].start = irq;
137 r[num_r].name = "IPMI IRQ";
138 r[num_r].flags = IORESOURCE_IRQ;
142 rv = platform_device_add_resources(pdev, r, num_r);
145 "ipmi:dmi: Unable to add resources: %d\n", rv);
150 rv = platform_device_add_properties(pdev, p);
153 "ipmi:dmi: Unable to add properties: %d\n", rv);
157 rv = platform_device_add(pdev);
159 dev_err(&pdev->dev, "ipmi:dmi: Unable to add device: %d\n", rv);
167 platform_device_put(pdev);
171 * Look up the slave address for a given interface. This is here
172 * because ACPI doesn't have a slave address while SMBIOS does, but we
173 * prefer using ACPI so the ACPI code can use the IPMI namespace.
174 * This function allows an ACPI-specified IPMI device to look up the
175 * slave address from the DMI table.
177 int ipmi_dmi_get_slave_addr(enum si_type si_type, u32 flags,
178 unsigned long base_addr)
180 struct ipmi_dmi_info *info = ipmi_dmi_infos;
183 if (info->si_type == si_type &&
184 info->flags == flags &&
185 info->addr == base_addr)
186 return info->slave_addr;
192 EXPORT_SYMBOL(ipmi_dmi_get_slave_addr);
194 #define DMI_IPMI_MIN_LENGTH 0x10
195 #define DMI_IPMI_VER2_LENGTH 0x12
196 #define DMI_IPMI_TYPE 4
197 #define DMI_IPMI_SLAVEADDR 6
198 #define DMI_IPMI_ADDR 8
199 #define DMI_IPMI_ACCESS 0x10
200 #define DMI_IPMI_IRQ 0x11
201 #define DMI_IPMI_IO_MASK 0xfffe
203 static void __init dmi_decode_ipmi(const struct dmi_header *dm)
205 const u8 *data = (const u8 *) dm;
206 u32 flags = IORESOURCE_IO;
207 unsigned long base_addr;
213 if (len < DMI_IPMI_MIN_LENGTH)
216 type = data[DMI_IPMI_TYPE];
217 slave_addr = data[DMI_IPMI_SLAVEADDR];
219 memcpy(&base_addr, data + DMI_IPMI_ADDR, sizeof(unsigned long));
220 if (len >= DMI_IPMI_VER2_LENGTH) {
221 if (type == IPMI_DMI_TYPE_SSIF) {
224 base_addr = data[DMI_IPMI_ADDR] >> 1;
225 if (base_addr == 0) {
227 * Some broken systems put the I2C address in
228 * the slave address field. We try to
229 * accommodate them here.
231 base_addr = data[DMI_IPMI_SLAVEADDR] >> 1;
237 base_addr &= DMI_IPMI_IO_MASK;
240 flags = IORESOURCE_MEM;
244 * If bit 4 of byte 0x10 is set, then the lsb
245 * for the address is odd.
247 base_addr |= (data[DMI_IPMI_ACCESS] >> 4) & 1;
249 irq = data[DMI_IPMI_IRQ];
252 * The top two bits of byte 0x10 hold the
255 switch ((data[DMI_IPMI_ACCESS] >> 6) & 3) {
256 case 0: /* Byte boundaries */
259 case 1: /* 32-bit boundaries */
262 case 2: /* 16-byte boundaries */
266 pr_err("ipmi:dmi: Invalid offset: 0\n");
273 * Note that technically, the lower bit of the base
274 * address should be 1 if the address is I/O and 0 if
275 * the address is in memory. So many systems get that
276 * wrong (and all that I have seen are I/O) so we just
277 * ignore that bit and assume I/O. Systems that use
278 * memory should use the newer spec, anyway.
280 base_addr = base_addr & DMI_IPMI_IO_MASK;
284 dmi_add_platform_ipmi(base_addr, flags, slave_addr, irq,
288 static int __init scan_for_dmi_ipmi(void)
290 const struct dmi_device *dev = NULL;
292 while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
293 dmi_decode_ipmi((const struct dmi_header *) dev->device_data);
297 subsys_initcall(scan_for_dmi_ipmi);