blob: 38311ee7eb10116e1a4476c8f38c5aa13e91f8b3 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Rothcddd6002019-09-23 17:38:27 -06002
3/*
Patrick Rudolphffbc3b52019-06-06 15:45:51 +02004 * Place in devicetree.cb:
5 *
6 * chip drivers/ipmi
7 * device pnp ca2.0 on end # IPMI KCS
8 * end
9 */
10
Elyes HAOUAS92f46aa2020-09-15 08:42:17 +020011#include <arch/io.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020012#include <console/console.h>
13#include <device/device.h>
14#include <device/pnp.h>
15#if CONFIG(HAVE_ACPI_TABLES)
Furquan Shaikh76cedd22020-05-02 10:24:23 -070016#include <acpi/acpi.h>
17#include <acpi/acpigen.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020018#endif
19#if CONFIG(GENERATE_SMBIOS_TABLES)
20#include <smbios.h>
21#endif
22#include <version.h>
23#include <delay.h>
Patrick Rudolph3d41a132019-07-22 16:31:35 +020024#include <timer.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020025#include "ipmi_kcs.h"
Patrick Rudolph09cdeba2019-12-30 14:40:04 +010026#include "ipmi_supermicro_oem.h"
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020027#include "chip.h"
28
29/* 4 bit encoding */
30static u8 ipmi_revision_major = 0x1;
31static u8 ipmi_revision_minor = 0x0;
32
Tim Chu278ad212020-10-06 01:40:53 -070033static u8 bmc_revision_major = 0x0;
34static u8 bmc_revision_minor = 0x0;
35
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020036static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
37{
38 int ret;
39
40 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
41 IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
42 sizeof(*rsp));
43 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
44 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
45 __func__, ret, rsp->resp.completion_code);
46 return 1;
47 }
48 if (ret != sizeof(*rsp)) {
49 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
50 return 1;
51 }
52 return 0;
53}
54
Morgan Jang50155022019-11-06 10:24:47 +080055static int ipmi_get_bmc_self_test_result(struct device *dev, struct ipmi_selftest_rsp *rsp)
56{
57 int ret;
58
59 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
60 IPMI_BMC_GET_SELFTEST_RESULTS, NULL, 0, (u8 *)rsp,
61 sizeof(*rsp));
62
63 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
64 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
65 __func__, ret, rsp->resp.completion_code);
66 return 1;
67 }
68 if (ret != sizeof(*rsp)) {
69 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
70 return 1;
71 }
72
73 return 0;
74}
75
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020076static void ipmi_kcs_init(struct device *dev)
77{
78 struct ipmi_devid_rsp rsp;
79 uint32_t man_id = 0, prod_id = 0;
Bill XIEf880eb02020-11-10 15:20:24 +080080 struct drivers_ipmi_config *conf = dev->chip_info;
81 struct ipmi_selftest_rsp selftestrsp = {0};
Morgan Jang50155022019-11-06 10:24:47 +080082 uint8_t retry_count;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020083
84 if (!dev->enabled)
85 return;
86
Patrick Rudolph3d41a132019-07-22 16:31:35 +020087 printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);
88
Bill XIEf880eb02020-11-10 15:20:24 +080089 if (!conf) {
90 printk(BIOS_WARNING, "IPMI: chip_info is missing! Skip init.\n");
91 return;
92 }
Patrick Rudolph3d41a132019-07-22 16:31:35 +020093
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020094 /* Get IPMI version for ACPI and SMBIOS */
Bill XIEf880eb02020-11-10 15:20:24 +080095 if (conf->wait_for_bmc && conf->bmc_boot_timeout) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +020096 struct stopwatch sw;
97 stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
Paul Menzel01c18152020-06-02 11:12:30 +020098 printk(BIOS_INFO, "IPMI: Waiting for BMC...\n");
Patrick Rudolph3d41a132019-07-22 16:31:35 +020099
100 while (!stopwatch_expired(&sw)) {
101 if (inb(dev->path.pnp.port) != 0xff)
102 break;
103 mdelay(100);
104 }
105 if (stopwatch_expired(&sw)) {
106 printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
107 /* Don't write tables if communication failed */
108 dev->enabled = 0;
109 return;
110 }
111 }
112
Morgan Jang50155022019-11-06 10:24:47 +0800113 printk(BIOS_INFO, "Get BMC self test result...");
114 for (retry_count = 0; retry_count < conf->bmc_boot_timeout; retry_count++) {
115 if (!ipmi_get_bmc_self_test_result(dev, &selftestrsp))
116 break;
117
118 mdelay(1000);
119 }
120
121 switch (selftestrsp.result) {
122 case IPMI_APP_SELFTEST_NO_ERROR: /* 0x55 */
123 printk(BIOS_DEBUG, "No Error\n");
124 break;
125 case IPMI_APP_SELFTEST_NOT_IMPLEMENTED: /* 0x56 */
126 printk(BIOS_DEBUG, "Function Not Implemented\n");
127 break;
128 case IPMI_APP_SELFTEST_ERROR: /* 0x57 */
129 printk(BIOS_ERR, "BMC: Corrupted or inaccessible data or device\n");
130 /* Don't write tables if communication failed */
131 dev->enabled = 0;
132 break;
133 case IPMI_APP_SELFTEST_FATAL_HW_ERROR: /* 0x58 */
134 printk(BIOS_ERR, "BMC: Fatal Hardware Error\n");
135 /* Don't write tables if communication failed */
136 dev->enabled = 0;
137 break;
138 case IPMI_APP_SELFTEST_RESERVED: /* 0xFF */
139 printk(BIOS_DEBUG, "Reserved\n");
140 break;
141
142 default: /* Other Device Specific Hardware Error */
143 printk(BIOS_ERR, "BMC: Device Specific Error\n");
144 /* Don't write tables if communication failed */
145 dev->enabled = 0;
146 break;
147 }
148
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200149 if (!ipmi_get_device_id(dev, &rsp)) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200150 /* Queried the IPMI revision from BMC */
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200151 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
152 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
153
Tim Chu278ad212020-10-06 01:40:53 -0700154 bmc_revision_major = rsp.fw_rev1;
155 bmc_revision_minor = rsp.fw_rev2;
156
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200157 memcpy(&man_id, rsp.manufacturer_id,
158 sizeof(rsp.manufacturer_id));
159
160 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
161
162 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
163 man_id, prod_id);
164
165 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
166 ipmi_revision_major, ipmi_revision_minor);
167 } else {
168 /* Don't write tables if communication failed */
169 dev->enabled = 0;
170 }
Patrick Rudolph09cdeba2019-12-30 14:40:04 +0100171
172 if (!dev->enabled)
173 return;
174
175 if (CONFIG(DRIVERS_IPMI_SUPERMICRO_OEM))
176 supermicro_ipmi_oem(dev->path.pnp.port);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200177}
178
179#if CONFIG(HAVE_ACPI_TABLES)
180static uint32_t uid_cnt = 0;
181
182static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700183ipmi_write_acpi_tables(const struct device *dev, unsigned long current,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200184 struct acpi_rsdp *rsdp)
185{
Bill XIEf880eb02020-11-10 15:20:24 +0800186 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200187 struct acpi_spmi *spmi;
188 s8 gpe_interrupt = -1;
189 u32 apic_interrupt = 0;
190 acpi_addr_t addr = {
191 .space_id = ACPI_ADDRESS_SPACE_IO,
192 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
193 .addrl = dev->path.pnp.port,
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200194 .bit_width = 8,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200195 };
196
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200197 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
198 case 4:
199 addr.bit_offset = 32;
200 break;
201 case 16:
202 addr.bit_offset = 128;
203 break;
204 default:
205 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n");
206 /* fall through */
207 case 1:
208 addr.bit_offset = 8;
209 break;
210 }
211
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200212 current = ALIGN_UP(current, 8);
213 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
214 spmi = (struct acpi_spmi *)current;
215
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200216 if (conf) {
217 if (conf->have_gpe)
218 gpe_interrupt = conf->gpe_interrupt;
219 if (conf->have_apic)
220 apic_interrupt = conf->apic_interrupt;
Bill XIEf880eb02020-11-10 15:20:24 +0800221
222 /* Use command to get UID from ipmi_ssdt */
223 acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
224 (ipmi_revision_minor << 4), &addr,
225 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
226 conf->uid);
227
228 acpi_add_table(rsdp, spmi);
229
230 current += spmi->header.length;
231 } else {
232 printk(BIOS_WARNING, "IPMI: chip_info is missing!\n");
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200233 }
234
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200235 return current;
236}
237
Furquan Shaikh7536a392020-04-24 21:59:21 -0700238static void ipmi_ssdt(const struct device *dev)
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200239{
240 const char *scope = acpi_device_scope(dev);
Bill XIEf880eb02020-11-10 15:20:24 +0800241 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200242
243 if (!scope) {
244 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
245 dev_path(dev));
246 return;
247 }
248
Bill XIEf880eb02020-11-10 15:20:24 +0800249 if (!conf) {
250 printk(BIOS_WARNING, "IPMI: chip_info is missing!\n");
251 return;
252 }
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200253
254 /* Use command to pass UID to ipmi_write_acpi_tables */
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700255 conf->uid = uid_cnt++;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200256
257 /* write SPMI device */
258 acpigen_write_scope(scope);
259 acpigen_write_device("SPMI");
260 acpigen_write_name_string("_HID", "IPI0001");
Patrick Rudolph389c8272019-12-17 13:54:41 +0100261 acpigen_write_name_unicode("_STR", "IPMI_KCS");
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700262 acpigen_write_name_byte("_UID", conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200263 acpigen_write_STA(0xf);
264 acpigen_write_name("_CRS");
265 acpigen_write_resourcetemplate_header();
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200266 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1);
267 acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING,
268 dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200269
Bill XIEf880eb02020-11-10 15:20:24 +0800270 // FIXME: is that correct?
271 if (conf->have_apic)
272 acpigen_write_irq(1 << conf->apic_interrupt);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200273
274 acpigen_write_resourcetemplate_footer();
275
276 acpigen_write_method("_IFT", 0);
277 acpigen_write_return_byte(1); // KCS
278 acpigen_pop_len();
279
280 acpigen_write_method("_SRV", 0);
281 acpigen_write_return_integer((ipmi_revision_major << 8) |
282 (ipmi_revision_minor << 4));
283 acpigen_pop_len();
284
285 acpigen_pop_len(); /* pop device */
286 acpigen_pop_len(); /* pop scope */
287}
288#endif
289
Tim Chu278ad212020-10-06 01:40:53 -0700290void ipmi_bmc_version(uint8_t *ipmi_bmc_major_revision, uint8_t *ipmi_bmc_minor_revision)
291{
292 if (!bmc_revision_major || !bmc_revision_minor) {
293 printk(BIOS_ERR, "IPMI: BMC revision missing\n");
294 *ipmi_bmc_major_revision = 0;
295 *ipmi_bmc_minor_revision = 0;
296 } else {
297 *ipmi_bmc_major_revision = bmc_revision_major;
298 *ipmi_bmc_minor_revision = bmc_revision_minor;
299 }
300}
301
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200302#if CONFIG(GENERATE_SMBIOS_TABLES)
303static int ipmi_smbios_data(struct device *dev, int *handle,
304 unsigned long *current)
305{
Bill XIEf880eb02020-11-10 15:20:24 +0800306 struct drivers_ipmi_config *conf = dev->chip_info;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200307 u8 nv_storage = 0xff;
308 u8 i2c_address = 0;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200309 u8 register_spacing;
310
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200311 int len = 0;
312
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200313 if (conf) {
314 if (conf->have_nv_storage)
315 nv_storage = conf->nv_storage_device_address;
316 i2c_address = conf->bmc_i2c_address;
317 }
318
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200319 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
320 case 4:
321 register_spacing = 1 << 6;
322 break;
323 case 16:
324 register_spacing = 2 << 6;
325 break;
326 default:
327 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n");
328 /* fall through */
329 case 1:
330 register_spacing = 0 << 6;
331 break;
332 }
333
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200334 // add IPMI Device Information
335 len += smbios_write_type38(
336 current, handle,
337 SMBIOS_BMC_INTERFACE_KCS,
338 ipmi_revision_minor | (ipmi_revision_major << 4),
339 i2c_address, // I2C address
340 nv_storage, // NV storage
341 dev->path.pnp.port | 1, // IO interface
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200342 register_spacing,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200343 0); // no IRQ
344
345 return len;
346}
347#endif
348
349static void ipmi_set_resources(struct device *dev)
350{
351 struct resource *res;
352
353 for (res = dev->resource_list; res; res = res->next) {
354 if (!(res->flags & IORESOURCE_ASSIGNED))
355 continue;
356
357 res->flags |= IORESOURCE_STORED;
358 report_resource_stored(dev, res, "");
359 }
360}
361
362static void ipmi_read_resources(struct device *dev)
363{
364 struct resource *res = new_resource(dev, 0);
365 res->base = dev->path.pnp.port;
366 res->size = 2;
367 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
368}
369
370static struct device_operations ops = {
371 .read_resources = ipmi_read_resources,
372 .set_resources = ipmi_set_resources,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200373 .init = ipmi_kcs_init,
374#if CONFIG(HAVE_ACPI_TABLES)
375 .write_acpi_tables = ipmi_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200376 .acpi_fill_ssdt = ipmi_ssdt,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200377#endif
378#if CONFIG(GENERATE_SMBIOS_TABLES)
379 .get_smbios_data = ipmi_smbios_data,
380#endif
381};
382
383static void enable_dev(struct device *dev)
384{
385 if (dev->path.type != DEVICE_PATH_PNP)
386 printk(BIOS_ERR, "%s: Unsupported device type\n",
387 dev_path(dev));
388 else if (dev->path.pnp.port & 1)
389 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
390 dev_path(dev));
391 else
392 dev->ops = &ops;
393}
394
395struct chip_operations drivers_ipmi_ops = {
396 CHIP_NAME("IPMI KCS")
397 .enable_dev = enable_dev,
398};