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 #define pr_fmt(fmt) "%s" fmt, "ipmi:dmi: "
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"
18 #define IPMI_DMI_TYPE_KCS 0x01
19 #define IPMI_DMI_TYPE_SMIC 0x02
20 #define IPMI_DMI_TYPE_BT 0x03
21 #define IPMI_DMI_TYPE_SSIF 0x04
23 struct ipmi_dmi_info
{
28 struct ipmi_dmi_info
*next
;
31 static struct ipmi_dmi_info
*ipmi_dmi_infos
;
33 static int ipmi_dmi_nr __initdata
;
35 static void __init
dmi_add_platform_ipmi(unsigned long base_addr
,
42 struct platform_device
*pdev
;
44 unsigned int num_r
= 1, size
;
45 struct property_entry p
[5];
46 unsigned int pidx
= 0;
50 struct ipmi_dmi_info
*info
;
52 memset(p
, 0, sizeof(p
));
56 case IPMI_DMI_TYPE_SSIF
:
57 name
= "dmi-ipmi-ssif";
60 si_type
= SI_TYPE_INVALID
;
62 case IPMI_DMI_TYPE_BT
:
66 case IPMI_DMI_TYPE_KCS
:
70 case IPMI_DMI_TYPE_SMIC
:
75 pr_err("Invalid IPMI type: %d\n", type
);
79 if (si_type
!= SI_TYPE_INVALID
)
80 p
[pidx
++] = PROPERTY_ENTRY_U8("ipmi-type", si_type
);
82 p
[pidx
++] = PROPERTY_ENTRY_U8("slave-addr", slave_addr
);
83 p
[pidx
++] = PROPERTY_ENTRY_U8("addr-source", SI_SMBIOS
);
85 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
87 pr_warn("Could not allocate dmi info\n");
89 info
->si_type
= si_type
;
91 info
->addr
= base_addr
;
92 info
->slave_addr
= slave_addr
;
93 info
->next
= ipmi_dmi_infos
;
94 ipmi_dmi_infos
= info
;
97 pdev
= platform_device_alloc(name
, ipmi_dmi_nr
);
99 pr_err("Error allocation IPMI platform device\n");
103 if (type
== IPMI_DMI_TYPE_SSIF
) {
104 p
[pidx
++] = PROPERTY_ENTRY_U16("i2c-addr", base_addr
);
108 memset(r
, 0, sizeof(r
));
110 r
[0].start
= base_addr
;
111 r
[0].end
= r
[0].start
+ offset
- 1;
112 r
[0].name
= "IPMI Address 1";
116 r
[1].start
= r
[0].start
+ offset
;
117 r
[1].end
= r
[1].start
+ offset
- 1;
118 r
[1].name
= "IPMI Address 2";
124 r
[2].start
= r
[1].start
+ offset
;
125 r
[2].end
= r
[2].start
+ offset
- 1;
126 r
[2].name
= "IPMI Address 3";
132 r
[num_r
].start
= irq
;
134 r
[num_r
].name
= "IPMI IRQ";
135 r
[num_r
].flags
= IORESOURCE_IRQ
;
139 rv
= platform_device_add_resources(pdev
, r
, num_r
);
141 dev_err(&pdev
->dev
, "Unable to add resources: %d\n", rv
);
146 rv
= platform_device_add_properties(pdev
, p
);
148 dev_err(&pdev
->dev
, "Unable to add properties: %d\n", rv
);
152 rv
= platform_device_add(pdev
);
154 dev_err(&pdev
->dev
, "Unable to add device: %d\n", rv
);
162 platform_device_put(pdev
);
166 * Look up the slave address for a given interface. This is here
167 * because ACPI doesn't have a slave address while SMBIOS does, but we
168 * prefer using ACPI so the ACPI code can use the IPMI namespace.
169 * This function allows an ACPI-specified IPMI device to look up the
170 * slave address from the DMI table.
172 int ipmi_dmi_get_slave_addr(enum si_type si_type
, u32 flags
,
173 unsigned long base_addr
)
175 struct ipmi_dmi_info
*info
= ipmi_dmi_infos
;
178 if (info
->si_type
== si_type
&&
179 info
->flags
== flags
&&
180 info
->addr
== base_addr
)
181 return info
->slave_addr
;
187 EXPORT_SYMBOL(ipmi_dmi_get_slave_addr
);
189 #define DMI_IPMI_MIN_LENGTH 0x10
190 #define DMI_IPMI_VER2_LENGTH 0x12
191 #define DMI_IPMI_TYPE 4
192 #define DMI_IPMI_SLAVEADDR 6
193 #define DMI_IPMI_ADDR 8
194 #define DMI_IPMI_ACCESS 0x10
195 #define DMI_IPMI_IRQ 0x11
196 #define DMI_IPMI_IO_MASK 0xfffe
198 static void __init
dmi_decode_ipmi(const struct dmi_header
*dm
)
200 const u8
*data
= (const u8
*) dm
;
201 u32 flags
= IORESOURCE_IO
;
202 unsigned long base_addr
;
208 if (len
< DMI_IPMI_MIN_LENGTH
)
211 type
= data
[DMI_IPMI_TYPE
];
212 slave_addr
= data
[DMI_IPMI_SLAVEADDR
];
214 memcpy(&base_addr
, data
+ DMI_IPMI_ADDR
, sizeof(unsigned long));
216 pr_err("Base address is zero, assuming no IPMI interface\n");
219 if (len
>= DMI_IPMI_VER2_LENGTH
) {
220 if (type
== IPMI_DMI_TYPE_SSIF
) {
223 base_addr
= data
[DMI_IPMI_ADDR
] >> 1;
224 if (base_addr
== 0) {
226 * Some broken systems put the I2C address in
227 * the slave address field. We try to
228 * accommodate them here.
230 base_addr
= data
[DMI_IPMI_SLAVEADDR
] >> 1;
236 base_addr
&= DMI_IPMI_IO_MASK
;
239 flags
= IORESOURCE_MEM
;
243 * If bit 4 of byte 0x10 is set, then the lsb
244 * for the address is odd.
246 base_addr
|= (data
[DMI_IPMI_ACCESS
] >> 4) & 1;
248 irq
= data
[DMI_IPMI_IRQ
];
251 * The top two bits of byte 0x10 hold the
254 switch ((data
[DMI_IPMI_ACCESS
] >> 6) & 3) {
255 case 0: /* Byte boundaries */
258 case 1: /* 32-bit boundaries */
261 case 2: /* 16-byte boundaries */
265 pr_err("Invalid offset: 0\n");
272 * Note that technically, the lower bit of the base
273 * address should be 1 if the address is I/O and 0 if
274 * the address is in memory. So many systems get that
275 * wrong (and all that I have seen are I/O) so we just
276 * ignore that bit and assume I/O. Systems that use
277 * memory should use the newer spec, anyway.
279 base_addr
= base_addr
& DMI_IPMI_IO_MASK
;
283 dmi_add_platform_ipmi(base_addr
, flags
, slave_addr
, irq
,
287 static int __init
scan_for_dmi_ipmi(void)
289 const struct dmi_device
*dev
= NULL
;
291 while ((dev
= dmi_find_device(DMI_DEV_TYPE_IPMI
, NULL
, dev
)))
292 dmi_decode_ipmi((const struct dmi_header
*) dev
->device_data
);
296 subsys_initcall(scan_for_dmi_ipmi
);