Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Martin Roth | cddd600 | 2019-09-23 17:38:27 -0600 | [diff] [blame] | 2 | |
| 3 | /* |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 4 | * Place in devicetree.cb: |
| 5 | * |
| 6 | * chip drivers/ipmi |
| 7 | * device pnp ca2.0 on end # IPMI KCS |
| 8 | * end |
| 9 | */ |
| 10 | |
Elyes HAOUAS | 92f46aa | 2020-09-15 08:42:17 +0200 | [diff] [blame] | 11 | #include <arch/io.h> |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 12 | #include <console/console.h> |
| 13 | #include <device/device.h> |
| 14 | #include <device/pnp.h> |
| 15 | #if CONFIG(HAVE_ACPI_TABLES) |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 16 | #include <acpi/acpi.h> |
| 17 | #include <acpi/acpigen.h> |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 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 | |
Tim Chu | 278ad21 | 2020-10-06 01:40:53 -0700 | [diff] [blame] | 32 | static u8 bmc_revision_major = 0x0; |
| 33 | static u8 bmc_revision_minor = 0x0; |
| 34 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 35 | static 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 Jang | 5015502 | 2019-11-06 10:24:47 +0800 | [diff] [blame] | 54 | static 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 75 | static void ipmi_kcs_init(struct device *dev) |
| 76 | { |
| 77 | struct ipmi_devid_rsp rsp; |
| 78 | uint32_t man_id = 0, prod_id = 0; |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 79 | struct drivers_ipmi_config *conf = NULL; |
Morgan Jang | 5015502 | 2019-11-06 10:24:47 +0800 | [diff] [blame] | 80 | struct ipmi_selftest_rsp selftestrsp; |
| 81 | uint8_t retry_count; |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 82 | |
| 83 | if (!dev->enabled) |
| 84 | return; |
| 85 | |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 86 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 91 | /* Get IPMI version for ACPI and SMBIOS */ |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 92 | 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 Menzel | 01c1815 | 2020-06-02 11:12:30 +0200 | [diff] [blame] | 95 | printk(BIOS_INFO, "IPMI: Waiting for BMC...\n"); |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 96 | |
| 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 Jang | 5015502 | 2019-11-06 10:24:47 +0800 | [diff] [blame] | 110 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 146 | if (!ipmi_get_device_id(dev, &rsp)) { |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 147 | /* Queried the IPMI revision from BMC */ |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 148 | ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version); |
| 149 | ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version); |
| 150 | |
Tim Chu | 278ad21 | 2020-10-06 01:40:53 -0700 | [diff] [blame] | 151 | bmc_revision_major = rsp.fw_rev1; |
| 152 | bmc_revision_minor = rsp.fw_rev2; |
| 153 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 154 | 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) |
| 171 | static uint32_t uid_cnt = 0; |
| 172 | |
| 173 | static unsigned long |
Furquan Shaikh | 0f007d8 | 2020-04-24 06:41:18 -0700 | [diff] [blame] | 174 | ipmi_write_acpi_tables(const struct device *dev, unsigned long current, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 175 | 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 185 | .bit_width = 8, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 186 | }; |
| 187 | |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 188 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 203 | 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 Shaikh | 0f6e652 | 2020-04-24 21:35:23 -0700 | [diff] [blame] | 221 | conf->uid); |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 222 | |
| 223 | acpi_add_table(rsdp, spmi); |
| 224 | |
| 225 | current += spmi->header.length; |
| 226 | |
| 227 | return current; |
| 228 | } |
| 229 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 230 | static void ipmi_ssdt(const struct device *dev) |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 231 | { |
| 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 Shaikh | 0f6e652 | 2020-04-24 21:35:23 -0700 | [diff] [blame] | 245 | conf->uid = uid_cnt++; |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 246 | |
| 247 | /* write SPMI device */ |
| 248 | acpigen_write_scope(scope); |
| 249 | acpigen_write_device("SPMI"); |
| 250 | acpigen_write_name_string("_HID", "IPI0001"); |
Patrick Rudolph | 389c827 | 2019-12-17 13:54:41 +0100 | [diff] [blame] | 251 | acpigen_write_name_unicode("_STR", "IPMI_KCS"); |
Furquan Shaikh | 0f6e652 | 2020-04-24 21:35:23 -0700 | [diff] [blame] | 252 | acpigen_write_name_byte("_UID", conf->uid); |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 253 | acpigen_write_STA(0xf); |
| 254 | acpigen_write_name("_CRS"); |
| 255 | acpigen_write_resourcetemplate_header(); |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 256 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 259 | |
| 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 Chu | 278ad21 | 2020-10-06 01:40:53 -0700 | [diff] [blame] | 282 | void 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 294 | #if CONFIG(GENERATE_SMBIOS_TABLES) |
| 295 | static 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 301 | u8 register_spacing; |
| 302 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 303 | 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 314 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 329 | // 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 337 | register_spacing, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 338 | 0); // no IRQ |
| 339 | |
| 340 | return len; |
| 341 | } |
| 342 | #endif |
| 343 | |
| 344 | static 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 | |
| 357 | static 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 | |
| 365 | static struct device_operations ops = { |
| 366 | .read_resources = ipmi_read_resources, |
| 367 | .set_resources = ipmi_set_resources, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 368 | .init = ipmi_kcs_init, |
| 369 | #if CONFIG(HAVE_ACPI_TABLES) |
| 370 | .write_acpi_tables = ipmi_write_acpi_tables, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 371 | .acpi_fill_ssdt = ipmi_ssdt, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 372 | #endif |
| 373 | #if CONFIG(GENERATE_SMBIOS_TABLES) |
| 374 | .get_smbios_data = ipmi_smbios_data, |
| 375 | #endif |
| 376 | }; |
| 377 | |
| 378 | static 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 | |
| 390 | struct chip_operations drivers_ipmi_ops = { |
| 391 | CHIP_NAME("IPMI KCS") |
| 392 | .enable_dev = enable_dev, |
| 393 | }; |