blob: 362f17ac2b25250ab10bc77450c26ee6b312fc9d [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"
26#include "chip.h"
27
28/* 4 bit encoding */
29static u8 ipmi_revision_major = 0x1;
30static u8 ipmi_revision_minor = 0x0;
31
Tim Chu278ad212020-10-06 01:40:53 -070032static u8 bmc_revision_major = 0x0;
33static u8 bmc_revision_minor = 0x0;
34
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020035static int ipmi_get_device_id(struct device *dev, struct ipmi_devid_rsp *rsp)
36{
37 int ret;
38
39 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
40 IPMI_BMC_GET_DEVICE_ID, NULL, 0, (u8 *)rsp,
41 sizeof(*rsp));
42 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
43 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
44 __func__, ret, rsp->resp.completion_code);
45 return 1;
46 }
47 if (ret != sizeof(*rsp)) {
48 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
49 return 1;
50 }
51 return 0;
52}
53
Morgan Jang50155022019-11-06 10:24:47 +080054static int ipmi_get_bmc_self_test_result(struct device *dev, struct ipmi_selftest_rsp *rsp)
55{
56 int ret;
57
58 ret = ipmi_kcs_message(dev->path.pnp.port, IPMI_NETFN_APPLICATION, 0,
59 IPMI_BMC_GET_SELFTEST_RESULTS, NULL, 0, (u8 *)rsp,
60 sizeof(*rsp));
61
62 if (ret < sizeof(struct ipmi_rsp) || rsp->resp.completion_code) {
63 printk(BIOS_ERR, "IPMI: %s command failed (ret=%d resp=0x%x)\n",
64 __func__, ret, rsp->resp.completion_code);
65 return 1;
66 }
67 if (ret != sizeof(*rsp)) {
68 printk(BIOS_ERR, "IPMI: %s response truncated\n", __func__);
69 return 1;
70 }
71
72 return 0;
73}
74
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020075static void ipmi_kcs_init(struct device *dev)
76{
77 struct ipmi_devid_rsp rsp;
78 uint32_t man_id = 0, prod_id = 0;
Patrick Rudolph3d41a132019-07-22 16:31:35 +020079 struct drivers_ipmi_config *conf = NULL;
Morgan Jang50155022019-11-06 10:24:47 +080080 struct ipmi_selftest_rsp selftestrsp;
81 uint8_t retry_count;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020082
83 if (!dev->enabled)
84 return;
85
Patrick Rudolph3d41a132019-07-22 16:31:35 +020086 printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port);
87
88 if (dev->chip_info)
89 conf = dev->chip_info;
90
Patrick Rudolphffbc3b52019-06-06 15:45:51 +020091 /* Get IPMI version for ACPI and SMBIOS */
Patrick Rudolph3d41a132019-07-22 16:31:35 +020092 if (conf && conf->wait_for_bmc && conf->bmc_boot_timeout) {
93 struct stopwatch sw;
94 stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000);
Paul Menzel01c18152020-06-02 11:12:30 +020095 printk(BIOS_INFO, "IPMI: Waiting for BMC...\n");
Patrick Rudolph3d41a132019-07-22 16:31:35 +020096
97 while (!stopwatch_expired(&sw)) {
98 if (inb(dev->path.pnp.port) != 0xff)
99 break;
100 mdelay(100);
101 }
102 if (stopwatch_expired(&sw)) {
103 printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n");
104 /* Don't write tables if communication failed */
105 dev->enabled = 0;
106 return;
107 }
108 }
109
Morgan Jang50155022019-11-06 10:24:47 +0800110 printk(BIOS_INFO, "Get BMC self test result...");
111 for (retry_count = 0; retry_count < conf->bmc_boot_timeout; retry_count++) {
112 if (!ipmi_get_bmc_self_test_result(dev, &selftestrsp))
113 break;
114
115 mdelay(1000);
116 }
117
118 switch (selftestrsp.result) {
119 case IPMI_APP_SELFTEST_NO_ERROR: /* 0x55 */
120 printk(BIOS_DEBUG, "No Error\n");
121 break;
122 case IPMI_APP_SELFTEST_NOT_IMPLEMENTED: /* 0x56 */
123 printk(BIOS_DEBUG, "Function Not Implemented\n");
124 break;
125 case IPMI_APP_SELFTEST_ERROR: /* 0x57 */
126 printk(BIOS_ERR, "BMC: Corrupted or inaccessible data or device\n");
127 /* Don't write tables if communication failed */
128 dev->enabled = 0;
129 break;
130 case IPMI_APP_SELFTEST_FATAL_HW_ERROR: /* 0x58 */
131 printk(BIOS_ERR, "BMC: Fatal Hardware Error\n");
132 /* Don't write tables if communication failed */
133 dev->enabled = 0;
134 break;
135 case IPMI_APP_SELFTEST_RESERVED: /* 0xFF */
136 printk(BIOS_DEBUG, "Reserved\n");
137 break;
138
139 default: /* Other Device Specific Hardware Error */
140 printk(BIOS_ERR, "BMC: Device Specific Error\n");
141 /* Don't write tables if communication failed */
142 dev->enabled = 0;
143 break;
144 }
145
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200146 if (!ipmi_get_device_id(dev, &rsp)) {
Patrick Rudolph3d41a132019-07-22 16:31:35 +0200147 /* Queried the IPMI revision from BMC */
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200148 ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version);
149 ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version);
150
Tim Chu278ad212020-10-06 01:40:53 -0700151 bmc_revision_major = rsp.fw_rev1;
152 bmc_revision_minor = rsp.fw_rev2;
153
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200154 memcpy(&man_id, rsp.manufacturer_id,
155 sizeof(rsp.manufacturer_id));
156
157 memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id));
158
159 printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n",
160 man_id, prod_id);
161
162 printk(BIOS_INFO, "IPMI: Version %01x.%01x\n",
163 ipmi_revision_major, ipmi_revision_minor);
164 } else {
165 /* Don't write tables if communication failed */
166 dev->enabled = 0;
167 }
168}
169
170#if CONFIG(HAVE_ACPI_TABLES)
171static uint32_t uid_cnt = 0;
172
173static unsigned long
Furquan Shaikh0f007d82020-04-24 06:41:18 -0700174ipmi_write_acpi_tables(const struct device *dev, unsigned long current,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200175 struct acpi_rsdp *rsdp)
176{
177 struct drivers_ipmi_config *conf = NULL;
178 struct acpi_spmi *spmi;
179 s8 gpe_interrupt = -1;
180 u32 apic_interrupt = 0;
181 acpi_addr_t addr = {
182 .space_id = ACPI_ADDRESS_SPACE_IO,
183 .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS,
184 .addrl = dev->path.pnp.port,
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200185 .bit_width = 8,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200186 };
187
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200188 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
189 case 4:
190 addr.bit_offset = 32;
191 break;
192 case 16:
193 addr.bit_offset = 128;
194 break;
195 default:
196 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n");
197 /* fall through */
198 case 1:
199 addr.bit_offset = 8;
200 break;
201 }
202
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200203 current = ALIGN_UP(current, 8);
204 printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current);
205 spmi = (struct acpi_spmi *)current;
206
207 if (dev->chip_info)
208 conf = dev->chip_info;
209
210 if (conf) {
211 if (conf->have_gpe)
212 gpe_interrupt = conf->gpe_interrupt;
213 if (conf->have_apic)
214 apic_interrupt = conf->apic_interrupt;
215 }
216
217 /* Use command to get UID from ipmi_ssdt */
218 acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) |
219 (ipmi_revision_minor << 4), &addr,
220 IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt,
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700221 conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200222
223 acpi_add_table(rsdp, spmi);
224
225 current += spmi->header.length;
226
227 return current;
228}
229
Furquan Shaikh7536a392020-04-24 21:59:21 -0700230static void ipmi_ssdt(const struct device *dev)
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200231{
232 const char *scope = acpi_device_scope(dev);
233 struct drivers_ipmi_config *conf = NULL;
234
235 if (!scope) {
236 printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n",
237 dev_path(dev));
238 return;
239 }
240
241 if (dev->chip_info)
242 conf = dev->chip_info;
243
244 /* Use command to pass UID to ipmi_write_acpi_tables */
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700245 conf->uid = uid_cnt++;
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200246
247 /* write SPMI device */
248 acpigen_write_scope(scope);
249 acpigen_write_device("SPMI");
250 acpigen_write_name_string("_HID", "IPI0001");
Patrick Rudolph389c8272019-12-17 13:54:41 +0100251 acpigen_write_name_unicode("_STR", "IPMI_KCS");
Furquan Shaikh0f6e6522020-04-24 21:35:23 -0700252 acpigen_write_name_byte("_UID", conf->uid);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200253 acpigen_write_STA(0xf);
254 acpigen_write_name("_CRS");
255 acpigen_write_resourcetemplate_header();
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200256 acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1);
257 acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING,
258 dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1);
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200259
260 if (conf) {
261 // FIXME: is that correct?
262 if (conf->have_apic)
263 acpigen_write_irq(1 << conf->apic_interrupt);
264 }
265
266 acpigen_write_resourcetemplate_footer();
267
268 acpigen_write_method("_IFT", 0);
269 acpigen_write_return_byte(1); // KCS
270 acpigen_pop_len();
271
272 acpigen_write_method("_SRV", 0);
273 acpigen_write_return_integer((ipmi_revision_major << 8) |
274 (ipmi_revision_minor << 4));
275 acpigen_pop_len();
276
277 acpigen_pop_len(); /* pop device */
278 acpigen_pop_len(); /* pop scope */
279}
280#endif
281
Tim Chu278ad212020-10-06 01:40:53 -0700282void ipmi_bmc_version(uint8_t *ipmi_bmc_major_revision, uint8_t *ipmi_bmc_minor_revision)
283{
284 if (!bmc_revision_major || !bmc_revision_minor) {
285 printk(BIOS_ERR, "IPMI: BMC revision missing\n");
286 *ipmi_bmc_major_revision = 0;
287 *ipmi_bmc_minor_revision = 0;
288 } else {
289 *ipmi_bmc_major_revision = bmc_revision_major;
290 *ipmi_bmc_minor_revision = bmc_revision_minor;
291 }
292}
293
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200294#if CONFIG(GENERATE_SMBIOS_TABLES)
295static int ipmi_smbios_data(struct device *dev, int *handle,
296 unsigned long *current)
297{
298 struct drivers_ipmi_config *conf = NULL;
299 u8 nv_storage = 0xff;
300 u8 i2c_address = 0;
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200301 u8 register_spacing;
302
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200303 int len = 0;
304
305 if (dev->chip_info)
306 conf = dev->chip_info;
307
308 if (conf) {
309 if (conf->have_nv_storage)
310 nv_storage = conf->nv_storage_device_address;
311 i2c_address = conf->bmc_i2c_address;
312 }
313
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200314 switch (CONFIG_IPMI_KCS_REGISTER_SPACING) {
315 case 4:
316 register_spacing = 1 << 6;
317 break;
318 case 16:
319 register_spacing = 2 << 6;
320 break;
321 default:
322 printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n");
323 /* fall through */
324 case 1:
325 register_spacing = 0 << 6;
326 break;
327 }
328
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200329 // add IPMI Device Information
330 len += smbios_write_type38(
331 current, handle,
332 SMBIOS_BMC_INTERFACE_KCS,
333 ipmi_revision_minor | (ipmi_revision_major << 4),
334 i2c_address, // I2C address
335 nv_storage, // NV storage
336 dev->path.pnp.port | 1, // IO interface
Patrick Rudolpha96c4a12019-08-29 19:44:32 +0200337 register_spacing,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200338 0); // no IRQ
339
340 return len;
341}
342#endif
343
344static void ipmi_set_resources(struct device *dev)
345{
346 struct resource *res;
347
348 for (res = dev->resource_list; res; res = res->next) {
349 if (!(res->flags & IORESOURCE_ASSIGNED))
350 continue;
351
352 res->flags |= IORESOURCE_STORED;
353 report_resource_stored(dev, res, "");
354 }
355}
356
357static void ipmi_read_resources(struct device *dev)
358{
359 struct resource *res = new_resource(dev, 0);
360 res->base = dev->path.pnp.port;
361 res->size = 2;
362 res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED;
363}
364
365static struct device_operations ops = {
366 .read_resources = ipmi_read_resources,
367 .set_resources = ipmi_set_resources,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200368 .init = ipmi_kcs_init,
369#if CONFIG(HAVE_ACPI_TABLES)
370 .write_acpi_tables = ipmi_write_acpi_tables,
Nico Huber68680dd2020-03-31 17:34:52 +0200371 .acpi_fill_ssdt = ipmi_ssdt,
Patrick Rudolphffbc3b52019-06-06 15:45:51 +0200372#endif
373#if CONFIG(GENERATE_SMBIOS_TABLES)
374 .get_smbios_data = ipmi_smbios_data,
375#endif
376};
377
378static void enable_dev(struct device *dev)
379{
380 if (dev->path.type != DEVICE_PATH_PNP)
381 printk(BIOS_ERR, "%s: Unsupported device type\n",
382 dev_path(dev));
383 else if (dev->path.pnp.port & 1)
384 printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n",
385 dev_path(dev));
386 else
387 dev->ops = &ops;
388}
389
390struct chip_operations drivers_ipmi_ops = {
391 CHIP_NAME("IPMI KCS")
392 .enable_dev = enable_dev,
393};