xref: /openbmc/linux/drivers/char/ipmi/ipmi_dmi.c (revision b7019ac5)
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 #include "ipmi_plat_data.h"
18 
19 #define IPMI_DMI_TYPE_KCS	0x01
20 #define IPMI_DMI_TYPE_SMIC	0x02
21 #define IPMI_DMI_TYPE_BT	0x03
22 #define IPMI_DMI_TYPE_SSIF	0x04
23 
24 struct ipmi_dmi_info {
25 	enum si_type si_type;
26 	unsigned int space; /* addr space for si, intf# for ssif */
27 	unsigned long addr;
28 	u8 slave_addr;
29 	struct ipmi_dmi_info *next;
30 };
31 
32 static struct ipmi_dmi_info *ipmi_dmi_infos;
33 
34 static int ipmi_dmi_nr __initdata;
35 
36 static void __init dmi_add_platform_ipmi(unsigned long base_addr,
37 					 unsigned int space,
38 					 u8 slave_addr,
39 					 int irq,
40 					 int offset,
41 					 int type)
42 {
43 	const char *name;
44 	struct ipmi_dmi_info *info;
45 	struct ipmi_plat_data p;
46 
47 	memset(&p, 0, sizeof(p));
48 
49 	name = "dmi-ipmi-si";
50 	p.iftype = IPMI_PLAT_IF_SI;
51 	switch (type) {
52 	case IPMI_DMI_TYPE_SSIF:
53 		name = "dmi-ipmi-ssif";
54 		p.iftype = IPMI_PLAT_IF_SSIF;
55 		p.type = SI_TYPE_INVALID;
56 		break;
57 	case IPMI_DMI_TYPE_BT:
58 		p.type = SI_BT;
59 		break;
60 	case IPMI_DMI_TYPE_KCS:
61 		p.type = SI_KCS;
62 		break;
63 	case IPMI_DMI_TYPE_SMIC:
64 		p.type = SI_SMIC;
65 		break;
66 	default:
67 		pr_err("Invalid IPMI type: %d\n", type);
68 		return;
69 	}
70 
71 	p.addr = base_addr;
72 	p.space = space;
73 	p.regspacing = offset;
74 	p.irq = irq;
75 	p.slave_addr = slave_addr;
76 	p.addr_source = SI_SMBIOS;
77 
78 	info = kmalloc(sizeof(*info), GFP_KERNEL);
79 	if (!info) {
80 		pr_warn("Could not allocate dmi info\n");
81 	} else {
82 		info->si_type = p.type;
83 		info->space = space;
84 		info->addr = base_addr;
85 		info->slave_addr = slave_addr;
86 		info->next = ipmi_dmi_infos;
87 		ipmi_dmi_infos = info;
88 	}
89 
90 	if (ipmi_platform_add(name, ipmi_dmi_nr, &p))
91 		ipmi_dmi_nr++;
92 }
93 
94 /*
95  * Look up the slave address for a given interface.  This is here
96  * because ACPI doesn't have a slave address while SMBIOS does, but we
97  * prefer using ACPI so the ACPI code can use the IPMI namespace.
98  * This function allows an ACPI-specified IPMI device to look up the
99  * slave address from the DMI table.
100  */
101 int ipmi_dmi_get_slave_addr(enum si_type si_type, unsigned int space,
102 			    unsigned long base_addr)
103 {
104 	struct ipmi_dmi_info *info = ipmi_dmi_infos;
105 
106 	while (info) {
107 		if (info->si_type == si_type &&
108 		    info->space == space &&
109 		    info->addr == base_addr)
110 			return info->slave_addr;
111 		info = info->next;
112 	}
113 
114 	return 0;
115 }
116 EXPORT_SYMBOL(ipmi_dmi_get_slave_addr);
117 
118 #define DMI_IPMI_MIN_LENGTH	0x10
119 #define DMI_IPMI_VER2_LENGTH	0x12
120 #define DMI_IPMI_TYPE		4
121 #define DMI_IPMI_SLAVEADDR	6
122 #define DMI_IPMI_ADDR		8
123 #define DMI_IPMI_ACCESS		0x10
124 #define DMI_IPMI_IRQ		0x11
125 #define DMI_IPMI_IO_MASK	0xfffe
126 
127 static void __init dmi_decode_ipmi(const struct dmi_header *dm)
128 {
129 	const u8 *data = (const u8 *) dm;
130 	int space = IPMI_IO_ADDR_SPACE;
131 	unsigned long base_addr;
132 	u8 len = dm->length;
133 	u8 slave_addr;
134 	int irq = 0, offset = 0;
135 	int type;
136 
137 	if (len < DMI_IPMI_MIN_LENGTH)
138 		return;
139 
140 	type = data[DMI_IPMI_TYPE];
141 	slave_addr = data[DMI_IPMI_SLAVEADDR];
142 
143 	memcpy(&base_addr, data + DMI_IPMI_ADDR, sizeof(unsigned long));
144 	if (!base_addr) {
145 		pr_err("Base address is zero, assuming no IPMI interface\n");
146 		return;
147 	}
148 	if (len >= DMI_IPMI_VER2_LENGTH) {
149 		if (type == IPMI_DMI_TYPE_SSIF) {
150 			space = 0; /* Match I2C interface 0. */
151 			base_addr = data[DMI_IPMI_ADDR] >> 1;
152 			if (base_addr == 0) {
153 				/*
154 				 * Some broken systems put the I2C address in
155 				 * the slave address field.  We try to
156 				 * accommodate them here.
157 				 */
158 				base_addr = data[DMI_IPMI_SLAVEADDR] >> 1;
159 				slave_addr = 0;
160 			}
161 		} else {
162 			if (base_addr & 1) {
163 				/* I/O */
164 				base_addr &= DMI_IPMI_IO_MASK;
165 			} else {
166 				/* Memory */
167 				space = IPMI_MEM_ADDR_SPACE;
168 			}
169 
170 			/*
171 			 * If bit 4 of byte 0x10 is set, then the lsb
172 			 * for the address is odd.
173 			 */
174 			base_addr |= (data[DMI_IPMI_ACCESS] >> 4) & 1;
175 
176 			irq = data[DMI_IPMI_IRQ];
177 
178 			/*
179 			 * The top two bits of byte 0x10 hold the
180 			 * register spacing.
181 			 */
182 			switch ((data[DMI_IPMI_ACCESS] >> 6) & 3) {
183 			case 0: /* Byte boundaries */
184 				offset = 1;
185 				break;
186 			case 1: /* 32-bit boundaries */
187 				offset = 4;
188 				break;
189 			case 2: /* 16-byte boundaries */
190 				offset = 16;
191 				break;
192 			default:
193 				pr_err("Invalid offset: 0\n");
194 				return;
195 			}
196 		}
197 	} else {
198 		/* Old DMI spec. */
199 		/*
200 		 * Note that technically, the lower bit of the base
201 		 * address should be 1 if the address is I/O and 0 if
202 		 * the address is in memory.  So many systems get that
203 		 * wrong (and all that I have seen are I/O) so we just
204 		 * ignore that bit and assume I/O.  Systems that use
205 		 * memory should use the newer spec, anyway.
206 		 */
207 		base_addr = base_addr & DMI_IPMI_IO_MASK;
208 		offset = 1;
209 	}
210 
211 	dmi_add_platform_ipmi(base_addr, space, slave_addr, irq,
212 			      offset, type);
213 }
214 
215 static int __init scan_for_dmi_ipmi(void)
216 {
217 	const struct dmi_device *dev = NULL;
218 
219 	while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
220 		dmi_decode_ipmi((const struct dmi_header *) dev->device_data);
221 
222 	return 0;
223 }
224 subsys_initcall(scan_for_dmi_ipmi);
225