blob: 0b90fb298e75c2f78686937b0175144a60a5371c [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
11#include <console/console.h>
12#include <device/device.h>
13#include <device/pnp.h>
14#if CONFIG(HAVE_ACPI_TABLES)
Furquan Shaikh76cedd22020-05-02 10:24:23 -070015#include <acpi/acpi.h>
16#include <acpi/acpigen.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020017#endif
18#if CONFIG(GENERATE_SMBIOS_TABLES)
19#include <smbios.h>
20#endif
21#include <version.h>
22#include <delay.h>
Patrick Rudolph3d41a132019-07-22 16:31:35 +020023#include <timer.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020024#include "ipmi_kcs.h"
25#include "chip.h"
26
27/* 4 bit encoding */
28static u8 ipmi_revision_major = 0x1;
29static u8 ipmi_revision_minor = 0x0;
30
31static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
32{
33 int ret;
34
35 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
36 IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
37 sizeof(*rsp));
38 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
39 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
40 __func__, ret, rsp->resp.completion_code);
41 return 1;
42 }
43 if (ret != sizeof(*rsp)) {
44 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
45 return 1;
46 }
47 return 0;
48}
49
Morgan Jang50155022019-11-06 10:24:47 +080050static int ipmi_get_bmc_self_test_result(struct device *dev, struct ipmi_selftest_rsp *rsp)
51{
52 int ret;
53
54 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
55 IPMI_BMC_GET_SELFTEST_RESULTS, NULL, 0, (u8 *)rsp,
56 sizeof(*rsp));
57
58 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
59 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
60 __func__, ret, rsp->resp.completion_code);
61 return 1;
62 }
63 if (ret != sizeof(*rsp)) {
64 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
65 return 1;
66 }
67
68 return 0;
69}
70
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020071static void ipmi_kcs_init(struct device *dev)
72{
73 struct ipmi_devid_rsp rsp;
74 uint32_t man_id = 0, prod_id = 0;
Patrick Rudolph3d41a132019-07-22 16:31:35 +020075 struct drivers_ipmi_config *conf = NULL;
Morgan Jang50155022019-11-06 10:24:47 +080076 struct ipmi_selftest_rsp selftestrsp;
77 uint8_t retry_count;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020078
79 if (!dev->enabled)
80 return;
81
Patrick Rudolph3d41a132019-07-22 16:31:35 +020082 printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);
83
84 if (dev->chip_info)
85 conf = dev->chip_info;
86
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020087 /* Get IPMI version for ACPI and SMBIOS */
Patrick Rudolph3d41a132019-07-22 16:31:35 +020088 if (conf && conf->wait_for_bmc && conf->bmc_boot_timeout) {
89 struct stopwatch sw;
90 stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
91 printk(BIOS_DEBUG, "IPMI: Waiting for BMC...\n");
92
93 while (!stopwatch_expired(&sw)) {
94 if (inb(dev->path.pnp.port) != 0xff)
95 break;
96 mdelay(100);
97 }
98 if (stopwatch_expired(&sw)) {
99 printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
100 /* Don't write tables if communication failed */
101 dev->enabled = 0;
102 return;
103 }
104 }
105
Morgan Jang50155022019-11-06 10:24:47 +0800106 printk(BIOS_INFO, "Get BMC self test result...");
107 for (retry_count = 0; retry_count < conf->bmc_boot_timeout; retry_count++) {
108 if (!ipmi_get_bmc_self_test_result(dev, &selftestrsp))
109 break;
110
111 mdelay(1000);
112 }
113
114 switch (selftestrsp.result) {
115 case IPMI_APP_SELFTEST_NO_ERROR: /* 0x55 */
116 printk(BIOS_DEBUG, "No Error\n");
117 break;
118 case IPMI_APP_SELFTEST_NOT_IMPLEMENTED: /* 0x56 */
119 printk(BIOS_DEBUG, "Function Not Implemented\n");
120 break;
121 case IPMI_APP_SELFTEST_ERROR: /* 0x57 */
122 printk(BIOS_ERR, "BMC: Corrupted or inaccessible data or device\n");
123 /* Don't write tables if communication failed */
124 dev->enabled = 0;
125 break;
126 case IPMI_APP_SELFTEST_FATAL_HW_ERROR: /* 0x58 */
127 printk(BIOS_ERR, "BMC: Fatal Hardware Error\n");
128 /* Don't write tables if communication failed */
129 dev->enabled = 0;
130 break;
131 case IPMI_APP_SELFTEST_RESERVED: /* 0xFF */
132 printk(BIOS_DEBUG, "Reserved\n");
133 break;
134
135 default: /* Other Device Specific Hardware Error */
136 printk(BIOS_ERR, "BMC: Device Specific Error\n");
137 /* Don't write tables if communication failed */
138 dev->enabled = 0;
139 break;
140 }
141
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200142 if (!ipmi_get_device_id(dev, &rsp)) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200143 /* Queried the IPMI revision from BMC */
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200144 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
145 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
146
147 memcpy(&man_id, rsp.manufacturer_id,
148 sizeof(rsp.manufacturer_id));
149
150 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
151
152 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
153 man_id, prod_id);
154
155 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
156 ipmi_revision_major, ipmi_revision_minor);
157 } else {
158 /* Don't write tables if communication failed */
159 dev->enabled = 0;
160 }
161}
162
163#if CONFIG(HAVE_ACPI_TABLES)
164static uint32_t uid_cnt = 0;
165
166static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700167ipmi_write_acpi_tables(const struct device *dev, unsigned long current,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200168 struct acpi_rsdp *rsdp)
169{
170 struct drivers_ipmi_config *conf = NULL;
171 struct acpi_spmi *spmi;
172 s8 gpe_interrupt = -1;
173 u32 apic_interrupt = 0;
174 acpi_addr_t addr = {
175 .space_id = ACPI_ADDRESS_SPACE_IO,
176 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
177 .addrl = dev->path.pnp.port,
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200178 .bit_width = 8,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200179 };
180
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200181 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
182 case 4:
183 addr.bit_offset = 32;
184 break;
185 case 16:
186 addr.bit_offset = 128;
187 break;
188 default:
189 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n");
190 /* fall through */
191 case 1:
192 addr.bit_offset = 8;
193 break;
194 }
195
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200196 current = ALIGN_UP(current, 8);
197 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
198 spmi = (struct acpi_spmi *)current;
199
200 if (dev->chip_info)
201 conf = dev->chip_info;
202
203 if (conf) {
204 if (conf->have_gpe)
205 gpe_interrupt = conf->gpe_interrupt;
206 if (conf->have_apic)
207 apic_interrupt = conf->apic_interrupt;
208 }
209
210 /* Use command to get UID from ipmi_ssdt */
211 acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
212 (ipmi_revision_minor << 4), &addr,
213 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700214 conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200215
216 acpi_add_table(rsdp, spmi);
217
218 current += spmi->header.length;
219
220 return current;
221}
222
Furquan Shaikh7536a392020-04-24 21:59:21 -0700223static void ipmi_ssdt(const struct device *dev)
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200224{
225 const char *scope = acpi_device_scope(dev);
226 struct drivers_ipmi_config *conf = NULL;
227
228 if (!scope) {
229 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
230 dev_path(dev));
231 return;
232 }
233
234 if (dev->chip_info)
235 conf = dev->chip_info;
236
237 /* Use command to pass UID to ipmi_write_acpi_tables */
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700238 conf->uid = uid_cnt++;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200239
240 /* write SPMI device */
241 acpigen_write_scope(scope);
242 acpigen_write_device("SPMI");
243 acpigen_write_name_string("_HID", "IPI0001");
Patrick Rudolph389c8272019-12-17 13:54:41 +0100244 acpigen_write_name_unicode("_STR", "IPMI_KCS");
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700245 acpigen_write_name_byte("_UID", conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200246 acpigen_write_STA(0xf);
247 acpigen_write_name("_CRS");
248 acpigen_write_resourcetemplate_header();
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200249 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1);
250 acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING,
251 dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200252
253 if (conf) {
254 // FIXME: is that correct?
255 if (conf->have_apic)
256 acpigen_write_irq(1 << conf->apic_interrupt);
257 }
258
259 acpigen_write_resourcetemplate_footer();
260
261 acpigen_write_method("_IFT", 0);
262 acpigen_write_return_byte(1); // KCS
263 acpigen_pop_len();
264
265 acpigen_write_method("_SRV", 0);
266 acpigen_write_return_integer((ipmi_revision_major << 8) |
267 (ipmi_revision_minor << 4));
268 acpigen_pop_len();
269
270 acpigen_pop_len(); /* pop device */
271 acpigen_pop_len(); /* pop scope */
272}
273#endif
274
275#if CONFIG(GENERATE_SMBIOS_TABLES)
276static int ipmi_smbios_data(struct device *dev, int *handle,
277 unsigned long *current)
278{
279 struct drivers_ipmi_config *conf = NULL;
280 u8 nv_storage = 0xff;
281 u8 i2c_address = 0;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200282 u8 register_spacing;
283
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200284 int len = 0;
285
286 if (dev->chip_info)
287 conf = dev->chip_info;
288
289 if (conf) {
290 if (conf->have_nv_storage)
291 nv_storage = conf->nv_storage_device_address;
292 i2c_address = conf->bmc_i2c_address;
293 }
294
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200295 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
296 case 4:
297 register_spacing = 1 << 6;
298 break;
299 case 16:
300 register_spacing = 2 << 6;
301 break;
302 default:
303 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n");
304 /* fall through */
305 case 1:
306 register_spacing = 0 << 6;
307 break;
308 }
309
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200310 // add IPMI Device Information
311 len += smbios_write_type38(
312 current, handle,
313 SMBIOS_BMC_INTERFACE_KCS,
314 ipmi_revision_minor | (ipmi_revision_major << 4),
315 i2c_address, // I2C address
316 nv_storage, // NV storage
317 dev->path.pnp.port | 1, // IO interface
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200318 register_spacing,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200319 0); // no IRQ
320
321 return len;
322}
323#endif
324
325static void ipmi_set_resources(struct device *dev)
326{
327 struct resource *res;
328
329 for (res = dev->resource_list; res; res = res->next) {
330 if (!(res->flags & IORESOURCE_ASSIGNED))
331 continue;
332
333 res->flags |= IORESOURCE_STORED;
334 report_resource_stored(dev, res, "");
335 }
336}
337
338static void ipmi_read_resources(struct device *dev)
339{
340 struct resource *res = new_resource(dev, 0);
341 res->base = dev->path.pnp.port;
342 res->size = 2;
343 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
344}
345
346static struct device_operations ops = {
347 .read_resources = ipmi_read_resources,
348 .set_resources = ipmi_set_resources,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200349 .init = ipmi_kcs_init,
350#if CONFIG(HAVE_ACPI_TABLES)
351 .write_acpi_tables = ipmi_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200352 .acpi_fill_ssdt = ipmi_ssdt,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200353#endif
354#if CONFIG(GENERATE_SMBIOS_TABLES)
355 .get_smbios_data = ipmi_smbios_data,
356#endif
357};
358
359static void enable_dev(struct device *dev)
360{
361 if (dev->path.type != DEVICE_PATH_PNP)
362 printk(BIOS_ERR, "%s: Unsupported device type\n",
363 dev_path(dev));
364 else if (dev->path.pnp.port & 1)
365 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
366 dev_path(dev));
367 else
368 dev->ops = &ops;
369}
370
371struct chip_operations drivers_ipmi_ops = {
372 CHIP_NAME("IPMI KCS")
373 .enable_dev = enable_dev,
374};