blob: 145313a599685c774a0fd3037f3146fb2db13216 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Martin Rothcddd6002019-09-23 17:38:27 -06003
4/*
Patrick Rudolphffbc3b52019-06-06 15:45:51 +02005 * Place in devicetree.cb:
6 *
7 * chip drivers/ipmi
8 * device pnp ca2.0 on end # IPMI KCS
9 * end
10 */
11
12#include <console/console.h>
13#include <device/device.h>
14#include <device/pnp.h>
15#if CONFIG(HAVE_ACPI_TABLES)
16#include <arch/acpi.h>
17#include <arch/acpigen.h>
18#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"
26#include "chip.h"
27
28/* 4 bit encoding */
29static u8 ipmi_revision_major = 0x1;
30static u8 ipmi_revision_minor = 0x0;
31
32static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
33{
34 int ret;
35
36 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
37 IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
38 sizeof(*rsp));
39 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
40 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
41 __func__, ret, rsp->resp.completion_code);
42 return 1;
43 }
44 if (ret != sizeof(*rsp)) {
45 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
46 return 1;
47 }
48 return 0;
49}
50
Morgan Jang50155022019-11-06 10:24:47 +080051static int ipmi_get_bmc_self_test_result(struct device *dev, struct ipmi_selftest_rsp *rsp)
52{
53 int ret;
54
55 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
56 IPMI_BMC_GET_SELFTEST_RESULTS, NULL, 0, (u8 *)rsp,
57 sizeof(*rsp));
58
59 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
60 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
61 __func__, ret, rsp->resp.completion_code);
62 return 1;
63 }
64 if (ret != sizeof(*rsp)) {
65 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
66 return 1;
67 }
68
69 return 0;
70}
71
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020072static void ipmi_kcs_init(struct device *dev)
73{
74 struct ipmi_devid_rsp rsp;
75 uint32_t man_id = 0, prod_id = 0;
Patrick Rudolph3d41a132019-07-22 16:31:35 +020076 struct drivers_ipmi_config *conf = NULL;
Morgan Jang50155022019-11-06 10:24:47 +080077 struct ipmi_selftest_rsp selftestrsp;
78 uint8_t retry_count;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020079
80 if (!dev->enabled)
81 return;
82
Patrick Rudolph3d41a132019-07-22 16:31:35 +020083 printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);
84
85 if (dev->chip_info)
86 conf = dev->chip_info;
87
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020088 /* Get IPMI version for ACPI and SMBIOS */
Patrick Rudolph3d41a132019-07-22 16:31:35 +020089 if (conf && conf->wait_for_bmc && conf->bmc_boot_timeout) {
90 struct stopwatch sw;
91 stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
92 printk(BIOS_DEBUG, "IPMI: Waiting for BMC...\n");
93
94 while (!stopwatch_expired(&sw)) {
95 if (inb(dev->path.pnp.port) != 0xff)
96 break;
97 mdelay(100);
98 }
99 if (stopwatch_expired(&sw)) {
100 printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
101 /* Don't write tables if communication failed */
102 dev->enabled = 0;
103 return;
104 }
105 }
106
Morgan Jang50155022019-11-06 10:24:47 +0800107 printk(BIOS_INFO, "Get BMC self test result...");
108 for (retry_count = 0; retry_count < conf->bmc_boot_timeout; retry_count++) {
109 if (!ipmi_get_bmc_self_test_result(dev, &selftestrsp))
110 break;
111
112 mdelay(1000);
113 }
114
115 switch (selftestrsp.result) {
116 case IPMI_APP_SELFTEST_NO_ERROR: /* 0x55 */
117 printk(BIOS_DEBUG, "No Error\n");
118 break;
119 case IPMI_APP_SELFTEST_NOT_IMPLEMENTED: /* 0x56 */
120 printk(BIOS_DEBUG, "Function Not Implemented\n");
121 break;
122 case IPMI_APP_SELFTEST_ERROR: /* 0x57 */
123 printk(BIOS_ERR, "BMC: Corrupted or inaccessible data or device\n");
124 /* Don't write tables if communication failed */
125 dev->enabled = 0;
126 break;
127 case IPMI_APP_SELFTEST_FATAL_HW_ERROR: /* 0x58 */
128 printk(BIOS_ERR, "BMC: Fatal Hardware Error\n");
129 /* Don't write tables if communication failed */
130 dev->enabled = 0;
131 break;
132 case IPMI_APP_SELFTEST_RESERVED: /* 0xFF */
133 printk(BIOS_DEBUG, "Reserved\n");
134 break;
135
136 default: /* Other Device Specific Hardware Error */
137 printk(BIOS_ERR, "BMC: Device Specific Error\n");
138 /* Don't write tables if communication failed */
139 dev->enabled = 0;
140 break;
141 }
142
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200143 if (!ipmi_get_device_id(dev, &rsp)) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200144 /* Queried the IPMI revision from BMC */
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200145 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
146 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
147
148 memcpy(&man_id, rsp.manufacturer_id,
149 sizeof(rsp.manufacturer_id));
150
151 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
152
153 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
154 man_id, prod_id);
155
156 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
157 ipmi_revision_major, ipmi_revision_minor);
158 } else {
159 /* Don't write tables if communication failed */
160 dev->enabled = 0;
161 }
162}
163
164#if CONFIG(HAVE_ACPI_TABLES)
165static uint32_t uid_cnt = 0;
166
167static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700168ipmi_write_acpi_tables(const struct device *dev, unsigned long current,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200169 struct acpi_rsdp *rsdp)
170{
171 struct drivers_ipmi_config *conf = NULL;
172 struct acpi_spmi *spmi;
173 s8 gpe_interrupt = -1;
174 u32 apic_interrupt = 0;
175 acpi_addr_t addr = {
176 .space_id = ACPI_ADDRESS_SPACE_IO,
177 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
178 .addrl = dev->path.pnp.port,
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200179 .bit_width = 8,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200180 };
181
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200182 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
183 case 4:
184 addr.bit_offset = 32;
185 break;
186 case 16:
187 addr.bit_offset = 128;
188 break;
189 default:
190 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n");
191 /* fall through */
192 case 1:
193 addr.bit_offset = 8;
194 break;
195 }
196
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200197 current = ALIGN_UP(current, 8);
198 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
199 spmi = (struct acpi_spmi *)current;
200
201 if (dev->chip_info)
202 conf = dev->chip_info;
203
204 if (conf) {
205 if (conf->have_gpe)
206 gpe_interrupt = conf->gpe_interrupt;
207 if (conf->have_apic)
208 apic_interrupt = conf->apic_interrupt;
209 }
210
211 /* Use command to get UID from ipmi_ssdt */
212 acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
213 (ipmi_revision_minor << 4), &addr,
214 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700215 conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200216
217 acpi_add_table(rsdp, spmi);
218
219 current += spmi->header.length;
220
221 return current;
222}
223
Furquan Shaikh7536a392020-04-24 21:59:21 -0700224static void ipmi_ssdt(const struct device *dev)
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200225{
226 const char *scope = acpi_device_scope(dev);
227 struct drivers_ipmi_config *conf = NULL;
228
229 if (!scope) {
230 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
231 dev_path(dev));
232 return;
233 }
234
235 if (dev->chip_info)
236 conf = dev->chip_info;
237
238 /* Use command to pass UID to ipmi_write_acpi_tables */
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700239 conf->uid = uid_cnt++;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200240
241 /* write SPMI device */
242 acpigen_write_scope(scope);
243 acpigen_write_device("SPMI");
244 acpigen_write_name_string("_HID", "IPI0001");
Patrick Rudolph389c8272019-12-17 13:54:41 +0100245 acpigen_write_name_unicode("_STR", "IPMI_KCS");
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700246 acpigen_write_name_byte("_UID", conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200247 acpigen_write_STA(0xf);
248 acpigen_write_name("_CRS");
249 acpigen_write_resourcetemplate_header();
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200250 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1);
251 acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING,
252 dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200253
254 if (conf) {
255 // FIXME: is that correct?
256 if (conf->have_apic)
257 acpigen_write_irq(1 << conf->apic_interrupt);
258 }
259
260 acpigen_write_resourcetemplate_footer();
261
262 acpigen_write_method("_IFT", 0);
263 acpigen_write_return_byte(1); // KCS
264 acpigen_pop_len();
265
266 acpigen_write_method("_SRV", 0);
267 acpigen_write_return_integer((ipmi_revision_major << 8) |
268 (ipmi_revision_minor << 4));
269 acpigen_pop_len();
270
271 acpigen_pop_len(); /* pop device */
272 acpigen_pop_len(); /* pop scope */
273}
274#endif
275
276#if CONFIG(GENERATE_SMBIOS_TABLES)
277static int ipmi_smbios_data(struct device *dev, int *handle,
278 unsigned long *current)
279{
280 struct drivers_ipmi_config *conf = NULL;
281 u8 nv_storage = 0xff;
282 u8 i2c_address = 0;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200283 u8 register_spacing;
284
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200285 int len = 0;
286
287 if (dev->chip_info)
288 conf = dev->chip_info;
289
290 if (conf) {
291 if (conf->have_nv_storage)
292 nv_storage = conf->nv_storage_device_address;
293 i2c_address = conf->bmc_i2c_address;
294 }
295
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200296 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
297 case 4:
298 register_spacing = 1 << 6;
299 break;
300 case 16:
301 register_spacing = 2 << 6;
302 break;
303 default:
304 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n");
305 /* fall through */
306 case 1:
307 register_spacing = 0 << 6;
308 break;
309 }
310
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200311 // add IPMI Device Information
312 len += smbios_write_type38(
313 current, handle,
314 SMBIOS_BMC_INTERFACE_KCS,
315 ipmi_revision_minor | (ipmi_revision_major << 4),
316 i2c_address, // I2C address
317 nv_storage, // NV storage
318 dev->path.pnp.port | 1, // IO interface
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200319 register_spacing,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200320 0); // no IRQ
321
322 return len;
323}
324#endif
325
326static void ipmi_set_resources(struct device *dev)
327{
328 struct resource *res;
329
330 for (res = dev->resource_list; res; res = res->next) {
331 if (!(res->flags & IORESOURCE_ASSIGNED))
332 continue;
333
334 res->flags |= IORESOURCE_STORED;
335 report_resource_stored(dev, res, "");
336 }
337}
338
339static void ipmi_read_resources(struct device *dev)
340{
341 struct resource *res = new_resource(dev, 0);
342 res->base = dev->path.pnp.port;
343 res->size = 2;
344 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
345}
346
347static struct device_operations ops = {
348 .read_resources = ipmi_read_resources,
349 .set_resources = ipmi_set_resources,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200350 .init = ipmi_kcs_init,
351#if CONFIG(HAVE_ACPI_TABLES)
352 .write_acpi_tables = ipmi_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200353 .acpi_fill_ssdt = ipmi_ssdt,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200354#endif
355#if CONFIG(GENERATE_SMBIOS_TABLES)
356 .get_smbios_data = ipmi_smbios_data,
357#endif
358};
359
360static void enable_dev(struct device *dev)
361{
362 if (dev->path.type != DEVICE_PATH_PNP)
363 printk(BIOS_ERR, "%s: Unsupported device type\n",
364 dev_path(dev));
365 else if (dev->path.pnp.port & 1)
366 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
367 dev_path(dev));
368 else
369 dev->ops = &ops;
370}
371
372struct chip_operations drivers_ipmi_ops = {
373 CHIP_NAME("IPMI KCS")
374 .enable_dev = enable_dev,
375};