xref: /openbmc/linux/drivers/char/ipmi/ipmi_dmi.c (revision 0b003749)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
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.
5  */
6 
7 #define pr_fmt(fmt) "%s" fmt, "ipmi:dmi: "
8 #define dev_fmt pr_fmt
9 
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"
16 #include "ipmi_dmi.h"
17 
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
22 
23 struct ipmi_dmi_info {
24 	enum si_type si_type;
25 	u32 flags;
26 	unsigned long addr;
27 	u8 slave_addr;
28 	struct ipmi_dmi_info *next;
29 };
30 
31 static struct ipmi_dmi_info *ipmi_dmi_infos;
32 
33 static int ipmi_dmi_nr __initdata;
34 
35 static void __init dmi_add_platform_ipmi(unsigned long base_addr,
36 					 u32 flags,
37 					 u8 slave_addr,
38 					 int irq,
39 					 int offset,
40 					 int type)
41 {
42 	struct platform_device *pdev;
43 	struct resource r[4];
44 	unsigned int num_r = 1, size;
45 	struct property_entry p[5];
46 	unsigned int pidx = 0;
47 	char *name;
48 	int rv;
49 	enum si_type si_type;
50 	struct ipmi_dmi_info *info;
51 
52 	memset(p, 0, sizeof(p));
53 
54 	name = "dmi-ipmi-si";
55 	switch (type) {
56 	case IPMI_DMI_TYPE_SSIF:
57 		name = "dmi-ipmi-ssif";
58 		offset = 1;
59 		size = 1;
60 		si_type = SI_TYPE_INVALID;
61 		break;
62 	case IPMI_DMI_TYPE_BT:
63 		size = 3;
64 		si_type = SI_BT;
65 		break;
66 	case IPMI_DMI_TYPE_KCS:
67 		size = 2;
68 		si_type = SI_KCS;
69 		break;
70 	case IPMI_DMI_TYPE_SMIC:
71 		size = 2;
72 		si_type = SI_SMIC;
73 		break;
74 	default:
75 		pr_err("Invalid IPMI type: %d\n", type);
76 		return;
77 	}
78 
79 	if (si_type != SI_TYPE_INVALID)
80 		p[pidx++] = PROPERTY_ENTRY_U8("ipmi-type", si_type);
81 
82 	p[pidx++] = PROPERTY_ENTRY_U8("slave-addr", slave_addr);
83 	p[pidx++] = PROPERTY_ENTRY_U8("addr-source", SI_SMBIOS);
84 
85 	info = kmalloc(sizeof(*info), GFP_KERNEL);
86 	if (!info) {
87 		pr_warn("Could not allocate dmi info\n");
88 	} else {
89 		info->si_type = si_type;
90 		info->flags = flags;
91 		info->addr = base_addr;
92 		info->slave_addr = slave_addr;
93 		info->next = ipmi_dmi_infos;
94 		ipmi_dmi_infos = info;
95 	}
96 
97 	pdev = platform_device_alloc(name, ipmi_dmi_nr);
98 	if (!pdev) {
99 		pr_err("Error allocation IPMI platform device\n");
100 		return;
101 	}
102 
103 	if (type == IPMI_DMI_TYPE_SSIF) {
104 		p[pidx++] = PROPERTY_ENTRY_U16("i2c-addr", base_addr);
105 		goto add_properties;
106 	}
107 
108 	memset(r, 0, sizeof(r));
109 
110 	r[0].start = base_addr;
111 	r[0].end = r[0].start + offset - 1;
112 	r[0].name = "IPMI Address 1";
113 	r[0].flags = flags;
114 
115 	if (size > 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";
119 		r[1].flags = flags;
120 		num_r++;
121 	}
122 
123 	if (size > 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";
127 		r[2].flags = flags;
128 		num_r++;
129 	}
130 
131 	if (irq) {
132 		r[num_r].start = irq;
133 		r[num_r].end = irq;
134 		r[num_r].name = "IPMI IRQ";
135 		r[num_r].flags = IORESOURCE_IRQ;
136 		num_r++;
137 	}
138 
139 	rv = platform_device_add_resources(pdev, r, num_r);
140 	if (rv) {
141 		dev_err(&pdev->dev, "Unable to add resources: %d\n", rv);
142 		goto err;
143 	}
144 
145 add_properties:
146 	rv = platform_device_add_properties(pdev, p);
147 	if (rv) {
148 		dev_err(&pdev->dev, "Unable to add properties: %d\n", rv);
149 		goto err;
150 	}
151 
152 	rv = platform_device_add(pdev);
153 	if (rv) {
154 		dev_err(&pdev->dev, "Unable to add device: %d\n", rv);
155 		goto err;
156 	}
157 
158 	ipmi_dmi_nr++;
159 	return;
160 
161 err:
162 	platform_device_put(pdev);
163 }
164 
165 /*
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.
171  */
172 int ipmi_dmi_get_slave_addr(enum si_type si_type, u32 flags,
173 			    unsigned long base_addr)
174 {
175 	struct ipmi_dmi_info *info = ipmi_dmi_infos;
176 
177 	while (info) {
178 		if (info->si_type == si_type &&
179 		    info->flags == flags &&
180 		    info->addr == base_addr)
181 			return info->slave_addr;
182 		info = info->next;
183 	}
184 
185 	return 0;
186 }
187 EXPORT_SYMBOL(ipmi_dmi_get_slave_addr);
188 
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
197 
198 static void __init dmi_decode_ipmi(const struct dmi_header *dm)
199 {
200 	const u8	*data = (const u8 *) dm;
201 	u32             flags = IORESOURCE_IO;
202 	unsigned long	base_addr;
203 	u8              len = dm->length;
204 	u8              slave_addr;
205 	int             irq = 0, offset;
206 	int             type;
207 
208 	if (len < DMI_IPMI_MIN_LENGTH)
209 		return;
210 
211 	type = data[DMI_IPMI_TYPE];
212 	slave_addr = data[DMI_IPMI_SLAVEADDR];
213 
214 	memcpy(&base_addr, data + DMI_IPMI_ADDR, sizeof(unsigned long));
215 	if (!base_addr) {
216 		pr_err("Base address is zero, assuming no IPMI interface\n");
217 		return;
218 	}
219 	if (len >= DMI_IPMI_VER2_LENGTH) {
220 		if (type == IPMI_DMI_TYPE_SSIF) {
221 			offset = 0;
222 			flags = 0;
223 			base_addr = data[DMI_IPMI_ADDR] >> 1;
224 			if (base_addr == 0) {
225 				/*
226 				 * Some broken systems put the I2C address in
227 				 * the slave address field.  We try to
228 				 * accommodate them here.
229 				 */
230 				base_addr = data[DMI_IPMI_SLAVEADDR] >> 1;
231 				slave_addr = 0;
232 			}
233 		} else {
234 			if (base_addr & 1) {
235 				/* I/O */
236 				base_addr &= DMI_IPMI_IO_MASK;
237 			} else {
238 				/* Memory */
239 				flags = IORESOURCE_MEM;
240 			}
241 
242 			/*
243 			 * If bit 4 of byte 0x10 is set, then the lsb
244 			 * for the address is odd.
245 			 */
246 			base_addr |= (data[DMI_IPMI_ACCESS] >> 4) & 1;
247 
248 			irq = data[DMI_IPMI_IRQ];
249 
250 			/*
251 			 * The top two bits of byte 0x10 hold the
252 			 * register spacing.
253 			 */
254 			switch ((data[DMI_IPMI_ACCESS] >> 6) & 3) {
255 			case 0: /* Byte boundaries */
256 				offset = 1;
257 				break;
258 			case 1: /* 32-bit boundaries */
259 				offset = 4;
260 				break;
261 			case 2: /* 16-byte boundaries */
262 				offset = 16;
263 				break;
264 			default:
265 				pr_err("Invalid offset: 0\n");
266 				return;
267 			}
268 		}
269 	} else {
270 		/* Old DMI spec. */
271 		/*
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.
278 		 */
279 		base_addr = base_addr & DMI_IPMI_IO_MASK;
280 		offset = 1;
281 	}
282 
283 	dmi_add_platform_ipmi(base_addr, flags, slave_addr, irq,
284 			      offset, type);
285 }
286 
287 static int __init scan_for_dmi_ipmi(void)
288 {
289 	const struct dmi_device *dev = NULL;
290 
291 	while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
292 		dmi_decode_ipmi((const struct dmi_header *) dev->device_data);
293 
294 	return 0;
295 }
296 subsys_initcall(scan_for_dmi_ipmi);
297