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
= override
;
86 if (type
== IPMI_DMI_TYPE_SSIF
)
89 memset(r
, 0, sizeof(r
));
91 r
[0].start
= base_addr
;
92 r
[0].end
= r
[0].start
+ offset
- 1;
93 r
[0].name
= "IPMI Address 1";
97 r
[1].start
= r
[0].start
+ offset
;
98 r
[1].end
= r
[1].start
+ offset
- 1;
99 r
[1].name
= "IPMI Address 2";
105 r
[2].start
= r
[1].start
+ offset
;
106 r
[2].end
= r
[2].start
+ offset
- 1;
107 r
[2].name
= "IPMI Address 3";
113 r
[num_r
].start
= irq
;
115 r
[num_r
].name
= "IPMI IRQ";
116 r
[num_r
].flags
= IORESOURCE_IRQ
;
120 rv
= platform_device_add_resources(pdev
, r
, num_r
);
123 "ipmi:dmi: Unable to add resources: %d\n", rv
);
128 rv
= platform_device_add_properties(pdev
, p
);
131 "ipmi:dmi: Unable to add properties: %d\n", rv
);
135 rv
= platform_device_add(pdev
);
137 dev_err(&pdev
->dev
, "ipmi:dmi: Unable to add device: %d\n", rv
);
145 platform_device_put(pdev
);
149 * Look up the slave address for a given interface. This is here
150 * because ACPI doesn't have a slave address while SMBIOS does, but we
151 * prefer using ACPI so the ACPI code can use the IPMI namespace.
152 * This function allows an ACPI-specified IPMI device to look up the
153 * slave address from the DMI table.
155 int ipmi_dmi_get_slave_addr(int type
, u32 flags
, unsigned long base_addr
)
157 struct ipmi_dmi_info
*info
= ipmi_dmi_infos
;
160 if (info
->type
== type
&&
161 info
->flags
== flags
&&
162 info
->addr
== base_addr
)
163 return info
->slave_addr
;
169 EXPORT_SYMBOL(ipmi_dmi_get_slave_addr
);
171 #define DMI_IPMI_MIN_LENGTH 0x10
172 #define DMI_IPMI_VER2_LENGTH 0x12
173 #define DMI_IPMI_TYPE 4
174 #define DMI_IPMI_SLAVEADDR 6
175 #define DMI_IPMI_ADDR 8
176 #define DMI_IPMI_ACCESS 0x10
177 #define DMI_IPMI_IRQ 0x11
178 #define DMI_IPMI_IO_MASK 0xfffe
180 static void __init
dmi_decode_ipmi(const struct dmi_header
*dm
)
182 const u8
*data
= (const u8
*) dm
;
183 u32 flags
= IORESOURCE_IO
;
184 unsigned long base_addr
;
190 if (len
< DMI_IPMI_MIN_LENGTH
)
193 type
= data
[DMI_IPMI_TYPE
];
194 slave_addr
= data
[DMI_IPMI_SLAVEADDR
];
196 memcpy(&base_addr
, data
+ DMI_IPMI_ADDR
, sizeof(unsigned long));
197 if (len
>= DMI_IPMI_VER2_LENGTH
) {
198 if (type
== IPMI_DMI_TYPE_SSIF
) {
201 base_addr
= data
[DMI_IPMI_ADDR
] >> 1;
202 if (base_addr
== 0) {
204 * Some broken systems put the I2C address in
205 * the slave address field. We try to
206 * accommodate them here.
208 base_addr
= data
[DMI_IPMI_SLAVEADDR
] >> 1;
214 base_addr
&= DMI_IPMI_IO_MASK
;
217 flags
= IORESOURCE_MEM
;
221 * If bit 4 of byte 0x10 is set, then the lsb
222 * for the address is odd.
224 base_addr
|= (data
[DMI_IPMI_ACCESS
] >> 4) & 1;
226 irq
= data
[DMI_IPMI_IRQ
];
229 * The top two bits of byte 0x10 hold the
232 switch ((data
[DMI_IPMI_ACCESS
] >> 6) & 3) {
233 case 0: /* Byte boundaries */
236 case 1: /* 32-bit boundaries */
239 case 2: /* 16-byte boundaries */
243 pr_err("ipmi:dmi: Invalid offset: 0");
250 * Note that technically, the lower bit of the base
251 * address should be 1 if the address is I/O and 0 if
252 * the address is in memory. So many systems get that
253 * wrong (and all that I have seen are I/O) so we just
254 * ignore that bit and assume I/O. Systems that use
255 * memory should use the newer spec, anyway.
257 base_addr
= base_addr
& DMI_IPMI_IO_MASK
;
261 dmi_add_platform_ipmi(base_addr
, flags
, slave_addr
, irq
,
265 static int __init
scan_for_dmi_ipmi(void)
267 const struct dmi_device
*dev
= NULL
;
269 while ((dev
= dmi_find_device(DMI_DEV_TYPE_IPMI
, NULL
, dev
)))
270 dmi_decode_ipmi((const struct dmi_header
*) dev
->device_data
);
274 subsys_initcall(scan_for_dmi_ipmi
);