GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / char / ipmi / ipmi_dmi.c
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 #include <linux/ipmi.h>
8 #include <linux/init.h>
9 #include <linux/dmi.h>
10 #include <linux/platform_device.h>
11 #include <linux/property.h>
12 #include "ipmi_dmi.h"
13
14 struct ipmi_dmi_info {
15         int type;
16         u32 flags;
17         unsigned long addr;
18         u8 slave_addr;
19         struct ipmi_dmi_info *next;
20 };
21
22 static struct ipmi_dmi_info *ipmi_dmi_infos;
23
24 static int ipmi_dmi_nr __initdata;
25
26 static void __init dmi_add_platform_ipmi(unsigned long base_addr,
27                                          u32 flags,
28                                          u8 slave_addr,
29                                          int irq,
30                                          int offset,
31                                          int type)
32 {
33         struct platform_device *pdev;
34         struct resource r[4];
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),
40                 { }
41         };
42         char *name, *override;
43         int rv;
44         struct ipmi_dmi_info *info;
45
46         info = kmalloc(sizeof(*info), GFP_KERNEL);
47         if (!info) {
48                 pr_warn("ipmi:dmi: Could not allocate dmi info\n");
49         } else {
50                 info->type = type;
51                 info->flags = flags;
52                 info->addr = base_addr;
53                 info->slave_addr = slave_addr;
54                 info->next = ipmi_dmi_infos;
55                 ipmi_dmi_infos = info;
56         }
57
58         name = "dmi-ipmi-si";
59         override = "ipmi_si";
60         switch (type) {
61         case IPMI_DMI_TYPE_SSIF:
62                 name = "dmi-ipmi-ssif";
63                 override = "ipmi_ssif";
64                 offset = 1;
65                 size = 1;
66                 break;
67         case IPMI_DMI_TYPE_BT:
68                 size = 3;
69                 break;
70         case IPMI_DMI_TYPE_KCS:
71         case IPMI_DMI_TYPE_SMIC:
72                 size = 2;
73                 break;
74         default:
75                 pr_err("ipmi:dmi: Invalid IPMI type: %d", type);
76                 return;
77         }
78
79         pdev = platform_device_alloc(name, ipmi_dmi_nr);
80         if (!pdev) {
81                 pr_err("ipmi:dmi: Error allocation IPMI platform device");
82                 return;
83         }
84         pdev->driver_override = kasprintf(GFP_KERNEL, "%s",
85                                           override);
86         if (!pdev->driver_override)
87                 goto err;
88
89         if (type == IPMI_DMI_TYPE_SSIF)
90                 goto add_properties;
91
92         memset(r, 0, sizeof(r));
93
94         r[0].start = base_addr;
95         r[0].end = r[0].start + offset - 1;
96         r[0].name = "IPMI Address 1";
97         r[0].flags = flags;
98
99         if (size > 1) {
100                 r[1].start = r[0].start + offset;
101                 r[1].end = r[1].start + offset - 1;
102                 r[1].name = "IPMI Address 2";
103                 r[1].flags = flags;
104                 num_r++;
105         }
106
107         if (size > 2) {
108                 r[2].start = r[1].start + offset;
109                 r[2].end = r[2].start + offset - 1;
110                 r[2].name = "IPMI Address 3";
111                 r[2].flags = flags;
112                 num_r++;
113         }
114
115         if (irq) {
116                 r[num_r].start = irq;
117                 r[num_r].end = irq;
118                 r[num_r].name = "IPMI IRQ";
119                 r[num_r].flags = IORESOURCE_IRQ;
120                 num_r++;
121         }
122
123         rv = platform_device_add_resources(pdev, r, num_r);
124         if (rv) {
125                 dev_err(&pdev->dev,
126                         "ipmi:dmi: Unable to add resources: %d\n", rv);
127                 goto err;
128         }
129
130 add_properties:
131         rv = platform_device_add_properties(pdev, p);
132         if (rv) {
133                 dev_err(&pdev->dev,
134                         "ipmi:dmi: Unable to add properties: %d\n", rv);
135                 goto err;
136         }
137
138         rv = platform_device_add(pdev);
139         if (rv) {
140                 dev_err(&pdev->dev, "ipmi:dmi: Unable to add device: %d\n", rv);
141                 goto err;
142         }
143
144         ipmi_dmi_nr++;
145         return;
146
147 err:
148         platform_device_put(pdev);
149 }
150
151 /*
152  * Look up the slave address for a given interface.  This is here
153  * because ACPI doesn't have a slave address while SMBIOS does, but we
154  * prefer using ACPI so the ACPI code can use the IPMI namespace.
155  * This function allows an ACPI-specified IPMI device to look up the
156  * slave address from the DMI table.
157  */
158 int ipmi_dmi_get_slave_addr(int type, u32 flags, unsigned long base_addr)
159 {
160         struct ipmi_dmi_info *info = ipmi_dmi_infos;
161
162         while (info) {
163                 if (info->type == type &&
164                     info->flags == flags &&
165                     info->addr == base_addr)
166                         return info->slave_addr;
167                 info = info->next;
168         }
169
170         return 0;
171 }
172 EXPORT_SYMBOL(ipmi_dmi_get_slave_addr);
173
174 #define DMI_IPMI_MIN_LENGTH     0x10
175 #define DMI_IPMI_VER2_LENGTH    0x12
176 #define DMI_IPMI_TYPE           4
177 #define DMI_IPMI_SLAVEADDR      6
178 #define DMI_IPMI_ADDR           8
179 #define DMI_IPMI_ACCESS         0x10
180 #define DMI_IPMI_IRQ            0x11
181 #define DMI_IPMI_IO_MASK        0xfffe
182
183 static void __init dmi_decode_ipmi(const struct dmi_header *dm)
184 {
185         const u8        *data = (const u8 *) dm;
186         u32             flags = IORESOURCE_IO;
187         unsigned long   base_addr;
188         u8              len = dm->length;
189         u8              slave_addr;
190         int             irq = 0, offset;
191         int             type;
192
193         if (len < DMI_IPMI_MIN_LENGTH)
194                 return;
195
196         type = data[DMI_IPMI_TYPE];
197         slave_addr = data[DMI_IPMI_SLAVEADDR];
198
199         memcpy(&base_addr, data + DMI_IPMI_ADDR, sizeof(unsigned long));
200         if (!base_addr) {
201                 pr_err("Base address is zero, assuming no IPMI interface\n");
202                 return;
203         }
204         if (len >= DMI_IPMI_VER2_LENGTH) {
205                 if (type == IPMI_DMI_TYPE_SSIF) {
206                         offset = 0;
207                         flags = 0;
208                         base_addr = data[DMI_IPMI_ADDR] >> 1;
209                         if (base_addr == 0) {
210                                 /*
211                                  * Some broken systems put the I2C address in
212                                  * the slave address field.  We try to
213                                  * accommodate them here.
214                                  */
215                                 base_addr = data[DMI_IPMI_SLAVEADDR] >> 1;
216                                 slave_addr = 0;
217                         }
218                 } else {
219                         if (base_addr & 1) {
220                                 /* I/O */
221                                 base_addr &= DMI_IPMI_IO_MASK;
222                         } else {
223                                 /* Memory */
224                                 flags = IORESOURCE_MEM;
225                         }
226
227                         /*
228                          * If bit 4 of byte 0x10 is set, then the lsb
229                          * for the address is odd.
230                          */
231                         base_addr |= (data[DMI_IPMI_ACCESS] >> 4) & 1;
232
233                         irq = data[DMI_IPMI_IRQ];
234
235                         /*
236                          * The top two bits of byte 0x10 hold the
237                          * register spacing.
238                          */
239                         switch ((data[DMI_IPMI_ACCESS] >> 6) & 3) {
240                         case 0: /* Byte boundaries */
241                                 offset = 1;
242                                 break;
243                         case 1: /* 32-bit boundaries */
244                                 offset = 4;
245                                 break;
246                         case 2: /* 16-byte boundaries */
247                                 offset = 16;
248                                 break;
249                         default:
250                                 pr_err("ipmi:dmi: Invalid offset: 0");
251                                 return;
252                         }
253                 }
254         } else {
255                 /* Old DMI spec. */
256                 /*
257                  * Note that technically, the lower bit of the base
258                  * address should be 1 if the address is I/O and 0 if
259                  * the address is in memory.  So many systems get that
260                  * wrong (and all that I have seen are I/O) so we just
261                  * ignore that bit and assume I/O.  Systems that use
262                  * memory should use the newer spec, anyway.
263                  */
264                 base_addr = base_addr & DMI_IPMI_IO_MASK;
265                 offset = 1;
266         }
267
268         dmi_add_platform_ipmi(base_addr, flags, slave_addr, irq,
269                               offset, type);
270 }
271
272 static int __init scan_for_dmi_ipmi(void)
273 {
274         const struct dmi_device *dev = NULL;
275
276         while ((dev = dmi_find_device(DMI_DEV_TYPE_IPMI, NULL, dev)))
277                 dmi_decode_ipmi((const struct dmi_header *) dev->device_data);
278
279         return 0;
280 }
281 subsys_initcall(scan_for_dmi_ipmi);