blob: e0fa1b012007d0171b6b79483451c755c5293ec1 [file] [log] [blame]
Patrick Rudolphffbc3b52019-06-06 15:45:51 +02001/*
2 * This file is part of the coreboot project.
3 *
Patrick Rudolphffbc3b52019-06-06 15:45:51 +02004 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; version 2 of
7 * the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Martin Rothcddd6002019-09-23 17:38:27 -060013 */
14
15/*
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020016 * Place in devicetree.cb:
17 *
18 * chip drivers/ipmi
19 * device pnp ca2.0 on end # IPMI KCS
20 * end
21 */
22
23#include <console/console.h>
24#include <device/device.h>
25#include <device/pnp.h>
26#if CONFIG(HAVE_ACPI_TABLES)
27#include <arch/acpi.h>
28#include <arch/acpigen.h>
29#endif
30#if CONFIG(GENERATE_SMBIOS_TABLES)
31#include <smbios.h>
32#endif
33#include <version.h>
34#include <delay.h>
Patrick Rudolph3d41a132019-07-22 16:31:35 +020035#include <timer.h>
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020036#include "ipmi_kcs.h"
37#include "chip.h"
38
39/* 4 bit encoding */
40static u8 ipmi_revision_major = 0x1;
41static u8 ipmi_revision_minor = 0x0;
42
43static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
44{
45 int ret;
46
47 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
48 IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
49 sizeof(*rsp));
50 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
51 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
52 __func__, ret, rsp->resp.completion_code);
53 return 1;
54 }
55 if (ret != sizeof(*rsp)) {
56 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
57 return 1;
58 }
59 return 0;
60}
61
Morgan Jang50155022019-11-06 10:24:47 +080062static int ipmi_get_bmc_self_test_result(struct device *dev, struct ipmi_selftest_rsp *rsp)
63{
64 int ret;
65
66 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
67 IPMI_BMC_GET_SELFTEST_RESULTS, NULL, 0, (u8 *)rsp,
68 sizeof(*rsp));
69
70 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
71 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
72 __func__, ret, rsp->resp.completion_code);
73 return 1;
74 }
75 if (ret != sizeof(*rsp)) {
76 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
77 return 1;
78 }
79
80 return 0;
81}
82
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020083static void ipmi_kcs_init(struct device *dev)
84{
85 struct ipmi_devid_rsp rsp;
86 uint32_t man_id = 0, prod_id = 0;
Patrick Rudolph3d41a132019-07-22 16:31:35 +020087 struct drivers_ipmi_config *conf = NULL;
Morgan Jang50155022019-11-06 10:24:47 +080088 struct ipmi_selftest_rsp selftestrsp;
89 uint8_t retry_count;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020090
91 if (!dev->enabled)
92 return;
93
Patrick Rudolph3d41a132019-07-22 16:31:35 +020094 printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);
95
96 if (dev->chip_info)
97 conf = dev->chip_info;
98
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020099 /* Get IPMI version for ACPI and SMBIOS */
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200100 if (conf && conf->wait_for_bmc && conf->bmc_boot_timeout) {
101 struct stopwatch sw;
102 stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
103 printk(BIOS_DEBUG, "IPMI: Waiting for BMC...\n");
104
105 while (!stopwatch_expired(&sw)) {
106 if (inb(dev->path.pnp.port) != 0xff)
107 break;
108 mdelay(100);
109 }
110 if (stopwatch_expired(&sw)) {
111 printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
112 /* Don't write tables if communication failed */
113 dev->enabled = 0;
114 return;
115 }
116 }
117
Morgan Jang50155022019-11-06 10:24:47 +0800118 printk(BIOS_INFO, "Get BMC self test result...");
119 for (retry_count = 0; retry_count < conf->bmc_boot_timeout; retry_count++) {
120 if (!ipmi_get_bmc_self_test_result(dev, &selftestrsp))
121 break;
122
123 mdelay(1000);
124 }
125
126 switch (selftestrsp.result) {
127 case IPMI_APP_SELFTEST_NO_ERROR: /* 0x55 */
128 printk(BIOS_DEBUG, "No Error\n");
129 break;
130 case IPMI_APP_SELFTEST_NOT_IMPLEMENTED: /* 0x56 */
131 printk(BIOS_DEBUG, "Function Not Implemented\n");
132 break;
133 case IPMI_APP_SELFTEST_ERROR: /* 0x57 */
134 printk(BIOS_ERR, "BMC: Corrupted or inaccessible data or device\n");
135 /* Don't write tables if communication failed */
136 dev->enabled = 0;
137 break;
138 case IPMI_APP_SELFTEST_FATAL_HW_ERROR: /* 0x58 */
139 printk(BIOS_ERR, "BMC: Fatal Hardware Error\n");
140 /* Don't write tables if communication failed */
141 dev->enabled = 0;
142 break;
143 case IPMI_APP_SELFTEST_RESERVED: /* 0xFF */
144 printk(BIOS_DEBUG, "Reserved\n");
145 break;
146
147 default: /* Other Device Specific Hardware Error */
148 printk(BIOS_ERR, "BMC: Device Specific Error\n");
149 /* Don't write tables if communication failed */
150 dev->enabled = 0;
151 break;
152 }
153
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200154 if (!ipmi_get_device_id(dev, &rsp)) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200155 /* Queried the IPMI revision from BMC */
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200156 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
157 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
158
159 memcpy(&man_id, rsp.manufacturer_id,
160 sizeof(rsp.manufacturer_id));
161
162 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
163
164 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
165 man_id, prod_id);
166
167 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
168 ipmi_revision_major, ipmi_revision_minor);
169 } else {
170 /* Don't write tables if communication failed */
171 dev->enabled = 0;
172 }
173}
174
175#if CONFIG(HAVE_ACPI_TABLES)
176static uint32_t uid_cnt = 0;
177
178static unsigned long
179ipmi_write_acpi_tables(struct device *dev, unsigned long current,
180 struct acpi_rsdp *rsdp)
181{
182 struct drivers_ipmi_config *conf = NULL;
183 struct acpi_spmi *spmi;
184 s8 gpe_interrupt = -1;
185 u32 apic_interrupt = 0;
186 acpi_addr_t addr = {
187 .space_id = ACPI_ADDRESS_SPACE_IO,
188 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
189 .addrl = dev->path.pnp.port,
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200190 .bit_width = 8,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200191 };
192
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200193 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
194 case 4:
195 addr.bit_offset = 32;
196 break;
197 case 16:
198 addr.bit_offset = 128;
199 break;
200 default:
201 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n");
202 /* fall through */
203 case 1:
204 addr.bit_offset = 8;
205 break;
206 }
207
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200208 current = ALIGN_UP(current, 8);
209 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
210 spmi = (struct acpi_spmi *)current;
211
212 if (dev->chip_info)
213 conf = dev->chip_info;
214
215 if (conf) {
216 if (conf->have_gpe)
217 gpe_interrupt = conf->gpe_interrupt;
218 if (conf->have_apic)
219 apic_interrupt = conf->apic_interrupt;
220 }
221
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 dev->command);
227
228 acpi_add_table(rsdp, spmi);
229
230 current += spmi->header.length;
231
232 return current;
233}
234
235static void ipmi_ssdt(struct device *dev)
236{
237 const char *scope = acpi_device_scope(dev);
238 struct drivers_ipmi_config *conf = NULL;
239
240 if (!scope) {
241 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
242 dev_path(dev));
243 return;
244 }
245
246 if (dev->chip_info)
247 conf = dev->chip_info;
248
249 /* Use command to pass UID to ipmi_write_acpi_tables */
250 dev->command = uid_cnt++;
251
252 /* write SPMI device */
253 acpigen_write_scope(scope);
254 acpigen_write_device("SPMI");
255 acpigen_write_name_string("_HID", "IPI0001");
Patrick Rudolph389c8272019-12-17 13:54:41 +0100256 acpigen_write_name_unicode("_STR", "IPMI_KCS");
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200257 acpigen_write_name_byte("_UID", dev->command);
258 acpigen_write_STA(0xf);
259 acpigen_write_name("_CRS");
260 acpigen_write_resourcetemplate_header();
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200261 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1);
262 acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING,
263 dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200264
265 if (conf) {
266 // FIXME: is that correct?
267 if (conf->have_apic)
268 acpigen_write_irq(1 << conf->apic_interrupt);
269 }
270
271 acpigen_write_resourcetemplate_footer();
272
273 acpigen_write_method("_IFT", 0);
274 acpigen_write_return_byte(1); // KCS
275 acpigen_pop_len();
276
277 acpigen_write_method("_SRV", 0);
278 acpigen_write_return_integer((ipmi_revision_major << 8) |
279 (ipmi_revision_minor << 4));
280 acpigen_pop_len();
281
282 acpigen_pop_len(); /* pop device */
283 acpigen_pop_len(); /* pop scope */
284}
285#endif
286
287#if CONFIG(GENERATE_SMBIOS_TABLES)
288static int ipmi_smbios_data(struct device *dev, int *handle,
289 unsigned long *current)
290{
291 struct drivers_ipmi_config *conf = NULL;
292 u8 nv_storage = 0xff;
293 u8 i2c_address = 0;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200294 u8 register_spacing;
295
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200296 int len = 0;
297
298 if (dev->chip_info)
299 conf = dev->chip_info;
300
301 if (conf) {
302 if (conf->have_nv_storage)
303 nv_storage = conf->nv_storage_device_address;
304 i2c_address = conf->bmc_i2c_address;
305 }
306
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200307 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
308 case 4:
309 register_spacing = 1 << 6;
310 break;
311 case 16:
312 register_spacing = 2 << 6;
313 break;
314 default:
315 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n");
316 /* fall through */
317 case 1:
318 register_spacing = 0 << 6;
319 break;
320 }
321
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200322 // add IPMI Device Information
323 len += smbios_write_type38(
324 current, handle,
325 SMBIOS_BMC_INTERFACE_KCS,
326 ipmi_revision_minor | (ipmi_revision_major << 4),
327 i2c_address, // I2C address
328 nv_storage, // NV storage
329 dev->path.pnp.port | 1, // IO interface
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200330 register_spacing,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200331 0); // no IRQ
332
333 return len;
334}
335#endif
336
337static void ipmi_set_resources(struct device *dev)
338{
339 struct resource *res;
340
341 for (res = dev->resource_list; res; res = res->next) {
342 if (!(res->flags & IORESOURCE_ASSIGNED))
343 continue;
344
345 res->flags |= IORESOURCE_STORED;
346 report_resource_stored(dev, res, "");
347 }
348}
349
350static void ipmi_read_resources(struct device *dev)
351{
352 struct resource *res = new_resource(dev, 0);
353 res->base = dev->path.pnp.port;
354 res->size = 2;
355 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
356}
357
358static struct device_operations ops = {
359 .read_resources = ipmi_read_resources,
360 .set_resources = ipmi_set_resources,
361 .enable_resources = DEVICE_NOOP,
362 .init = ipmi_kcs_init,
363#if CONFIG(HAVE_ACPI_TABLES)
364 .write_acpi_tables = ipmi_write_acpi_tables,
365 .acpi_fill_ssdt_generator = ipmi_ssdt,
366#endif
367#if CONFIG(GENERATE_SMBIOS_TABLES)
368 .get_smbios_data = ipmi_smbios_data,
369#endif
370};
371
372static void enable_dev(struct device *dev)
373{
374 if (dev->path.type != DEVICE_PATH_PNP)
375 printk(BIOS_ERR, "%s: Unsupported device type\n",
376 dev_path(dev));
377 else if (dev->path.pnp.port & 1)
378 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
379 dev_path(dev));
380 else
381 dev->ops = &ops;
382}
383
384struct chip_operations drivers_ipmi_ops = {
385 CHIP_NAME("IPMI KCS")
386 .enable_dev = enable_dev,
387};