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 #define set_prop_entry(_p_, _name_, type, val) \
34 struct property_entry *_p = &_p_; \
36 _p->length = sizeof(type); \
37 _p->is_string = false; \
38 _p->value.type##_data = val; \
41 static void __init
dmi_add_platform_ipmi(unsigned long base_addr
,
48 struct platform_device
*pdev
;
50 unsigned int num_r
= 1, size
;
51 struct property_entry p
[5];
52 unsigned int pidx
= 0;
53 char *name
, *override
;
56 struct ipmi_dmi_info
*info
;
58 memset(p
, 0, sizeof(p
));
63 case IPMI_DMI_TYPE_SSIF
:
64 name
= "dmi-ipmi-ssif";
65 override
= "ipmi_ssif";
68 si_type
= SI_TYPE_INVALID
;
70 case IPMI_DMI_TYPE_BT
:
74 case IPMI_DMI_TYPE_KCS
:
78 case IPMI_DMI_TYPE_SMIC
:
83 pr_err("ipmi:dmi: Invalid IPMI type: %d\n", type
);
87 if (si_type
!= SI_TYPE_INVALID
)
88 set_prop_entry(p
[pidx
++], "ipmi-type", u8
, si_type
);
89 set_prop_entry(p
[pidx
++], "slave-addr", u8
, slave_addr
);
90 set_prop_entry(p
[pidx
++], "addr-source", u8
, SI_SMBIOS
);
92 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
94 pr_warn("ipmi:dmi: Could not allocate dmi info\n");
96 info
->si_type
= si_type
;
98 info
->addr
= base_addr
;
99 info
->slave_addr
= slave_addr
;
100 info
->next
= ipmi_dmi_infos
;
101 ipmi_dmi_infos
= info
;
104 pdev
= platform_device_alloc(name
, ipmi_dmi_nr
);
106 pr_err("ipmi:dmi: Error allocation IPMI platform device\n");
109 pdev
->driver_override
= kasprintf(GFP_KERNEL
, "%s",
111 if (!pdev
->driver_override
)
114 if (type
== IPMI_DMI_TYPE_SSIF
) {
115 set_prop_entry(p
[pidx
++], "i2c-addr", u16
, base_addr
);
119 memset(r
, 0, sizeof(r
));
121 r
[0].start
= base_addr
;
122 r
[0].end
= r
[0].start
+ offset
- 1;
123 r
[0].name
= "IPMI Address 1";
127 r
[1].start
= r
[0].start
+ offset
;
128 r
[1].end
= r
[1].start
+ offset
- 1;
129 r
[1].name
= "IPMI Address 2";
135 r
[2].start
= r
[1].start
+ offset
;
136 r
[2].end
= r
[2].start
+ offset
- 1;
137 r
[2].name
= "IPMI Address 3";
143 r
[num_r
].start
= irq
;
145 r
[num_r
].name
= "IPMI IRQ";
146 r
[num_r
].flags
= IORESOURCE_IRQ
;
150 rv
= platform_device_add_resources(pdev
, r
, num_r
);
153 "ipmi:dmi: Unable to add resources: %d\n", rv
);
158 rv
= platform_device_add_properties(pdev
, p
);
161 "ipmi:dmi: Unable to add properties: %d\n", rv
);
165 rv
= platform_device_add(pdev
);
167 dev_err(&pdev
->dev
, "ipmi:dmi: Unable to add device: %d\n", rv
);
175 platform_device_put(pdev
);
179 * Look up the slave address for a given interface. This is here
180 * because ACPI doesn't have a slave address while SMBIOS does, but we
181 * prefer using ACPI so the ACPI code can use the IPMI namespace.
182 * This function allows an ACPI-specified IPMI device to look up the
183 * slave address from the DMI table.
185 int ipmi_dmi_get_slave_addr(enum si_type si_type
, u32 flags
,
186 unsigned long base_addr
)
188 struct ipmi_dmi_info
*info
= ipmi_dmi_infos
;
191 if (info
->si_type
== si_type
&&
192 info
->flags
== flags
&&
193 info
->addr
== base_addr
)
194 return info
->slave_addr
;
200 EXPORT_SYMBOL(ipmi_dmi_get_slave_addr
);
202 #define DMI_IPMI_MIN_LENGTH 0x10
203 #define DMI_IPMI_VER2_LENGTH 0x12
204 #define DMI_IPMI_TYPE 4
205 #define DMI_IPMI_SLAVEADDR 6
206 #define DMI_IPMI_ADDR 8
207 #define DMI_IPMI_ACCESS 0x10
208 #define DMI_IPMI_IRQ 0x11
209 #define DMI_IPMI_IO_MASK 0xfffe
211 static void __init
dmi_decode_ipmi(const struct dmi_header
*dm
)
213 const u8
*data
= (const u8
*) dm
;
214 u32 flags
= IORESOURCE_IO
;
215 unsigned long base_addr
;
221 if (len
< DMI_IPMI_MIN_LENGTH
)
224 type
= data
[DMI_IPMI_TYPE
];
225 slave_addr
= data
[DMI_IPMI_SLAVEADDR
];
227 memcpy(&base_addr
, data
+ DMI_IPMI_ADDR
, sizeof(unsigned long));
228 if (len
>= DMI_IPMI_VER2_LENGTH
) {
229 if (type
== IPMI_DMI_TYPE_SSIF
) {
232 base_addr
= data
[DMI_IPMI_ADDR
] >> 1;
233 if (base_addr
== 0) {
235 * Some broken systems put the I2C address in
236 * the slave address field. We try to
237 * accommodate them here.
239 base_addr
= data
[DMI_IPMI_SLAVEADDR
] >> 1;
245 base_addr
&= DMI_IPMI_IO_MASK
;
248 flags
= IORESOURCE_MEM
;
252 * If bit 4 of byte 0x10 is set, then the lsb
253 * for the address is odd.
255 base_addr
|= (data
[DMI_IPMI_ACCESS
] >> 4) & 1;
257 irq
= data
[DMI_IPMI_IRQ
];
260 * The top two bits of byte 0x10 hold the
263 switch ((data
[DMI_IPMI_ACCESS
] >> 6) & 3) {
264 case 0: /* Byte boundaries */
267 case 1: /* 32-bit boundaries */
270 case 2: /* 16-byte boundaries */
274 pr_err("ipmi:dmi: Invalid offset: 0\n");
281 * Note that technically, the lower bit of the base
282 * address should be 1 if the address is I/O and 0 if
283 * the address is in memory. So many systems get that
284 * wrong (and all that I have seen are I/O) so we just
285 * ignore that bit and assume I/O. Systems that use
286 * memory should use the newer spec, anyway.
288 base_addr
= base_addr
& DMI_IPMI_IO_MASK
;
292 dmi_add_platform_ipmi(base_addr
, flags
, slave_addr
, irq
,
296 static int __init
scan_for_dmi_ipmi(void)
298 const struct dmi_device
*dev
= NULL
;
300 while ((dev
= dmi_find_device(DMI_DEV_TYPE_IPMI
, NULL
, dev
)))
301 dmi_decode_ipmi((const struct dmi_header
*) dev
->device_data
);
305 subsys_initcall(scan_for_dmi_ipmi
);