Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Martin Roth | cddd600 | 2019-09-23 17:38:27 -0600 | [diff] [blame] | 3 | |
| 4 | /* |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 5 | * 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 Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 24 | #include <timer.h> |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 25 | #include "ipmi_kcs.h" |
| 26 | #include "chip.h" |
| 27 | |
| 28 | /* 4 bit encoding */ |
| 29 | static u8 ipmi_revision_major = 0x1; |
| 30 | static u8 ipmi_revision_minor = 0x0; |
| 31 | |
| 32 | static 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 Jang | 5015502 | 2019-11-06 10:24:47 +0800 | [diff] [blame] | 51 | static 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 72 | static void ipmi_kcs_init(struct device *dev) |
| 73 | { |
| 74 | struct ipmi_devid_rsp rsp; |
| 75 | uint32_t man_id = 0, prod_id = 0; |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 76 | struct drivers_ipmi_config *conf = NULL; |
Morgan Jang | 5015502 | 2019-11-06 10:24:47 +0800 | [diff] [blame] | 77 | struct ipmi_selftest_rsp selftestrsp; |
| 78 | uint8_t retry_count; |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 79 | |
| 80 | if (!dev->enabled) |
| 81 | return; |
| 82 | |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 83 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 88 | /* Get IPMI version for ACPI and SMBIOS */ |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 89 | 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 Jang | 5015502 | 2019-11-06 10:24:47 +0800 | [diff] [blame] | 107 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 143 | if (!ipmi_get_device_id(dev, &rsp)) { |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 144 | /* Queried the IPMI revision from BMC */ |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 145 | 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) |
| 165 | static uint32_t uid_cnt = 0; |
| 166 | |
| 167 | static unsigned long |
Furquan Shaikh | 0f007d8 | 2020-04-24 06:41:18 -0700 | [diff] [blame] | 168 | ipmi_write_acpi_tables(const struct device *dev, unsigned long current, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 169 | 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 179 | .bit_width = 8, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 180 | }; |
| 181 | |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 182 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 197 | 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 Shaikh | 0f6e652 | 2020-04-24 21:35:23 -0700 | [diff] [blame] | 215 | conf->uid); |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 216 | |
| 217 | acpi_add_table(rsdp, spmi); |
| 218 | |
| 219 | current += spmi->header.length; |
| 220 | |
| 221 | return current; |
| 222 | } |
| 223 | |
| 224 | static void ipmi_ssdt(struct device *dev) |
| 225 | { |
| 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 Shaikh | 0f6e652 | 2020-04-24 21:35:23 -0700 | [diff] [blame] | 239 | conf->uid = uid_cnt++; |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 240 | |
| 241 | /* write SPMI device */ |
| 242 | acpigen_write_scope(scope); |
| 243 | acpigen_write_device("SPMI"); |
| 244 | acpigen_write_name_string("_HID", "IPI0001"); |
Patrick Rudolph | 389c827 | 2019-12-17 13:54:41 +0100 | [diff] [blame] | 245 | acpigen_write_name_unicode("_STR", "IPMI_KCS"); |
Furquan Shaikh | 0f6e652 | 2020-04-24 21:35:23 -0700 | [diff] [blame] | 246 | acpigen_write_name_byte("_UID", conf->uid); |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 247 | acpigen_write_STA(0xf); |
| 248 | acpigen_write_name("_CRS"); |
| 249 | acpigen_write_resourcetemplate_header(); |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 250 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 253 | |
| 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) |
| 277 | static 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 283 | u8 register_spacing; |
| 284 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 285 | 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 296 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 311 | // 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 319 | register_spacing, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 320 | 0); // no IRQ |
| 321 | |
| 322 | return len; |
| 323 | } |
| 324 | #endif |
| 325 | |
| 326 | static 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 | |
| 339 | static 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 | |
| 347 | static struct device_operations ops = { |
| 348 | .read_resources = ipmi_read_resources, |
| 349 | .set_resources = ipmi_set_resources, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 350 | .init = ipmi_kcs_init, |
| 351 | #if CONFIG(HAVE_ACPI_TABLES) |
| 352 | .write_acpi_tables = ipmi_write_acpi_tables, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 353 | .acpi_fill_ssdt = ipmi_ssdt, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 354 | #endif |
| 355 | #if CONFIG(GENERATE_SMBIOS_TABLES) |
| 356 | .get_smbios_data = ipmi_smbios_data, |
| 357 | #endif |
| 358 | }; |
| 359 | |
| 360 | static 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 | |
| 372 | struct chip_operations drivers_ipmi_ops = { |
| 373 | CHIP_NAME("IPMI KCS") |
| 374 | .enable_dev = enable_dev, |
| 375 | }; |