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 | |
| 11 | #include <console/console.h> |
| 12 | #include <device/device.h> |
| 13 | #include <device/pnp.h> |
| 14 | #if CONFIG(HAVE_ACPI_TABLES) |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 15 | #include <acpi/acpi.h> |
| 16 | #include <acpi/acpigen.h> |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 17 | #endif |
| 18 | #if CONFIG(GENERATE_SMBIOS_TABLES) |
| 19 | #include <smbios.h> |
| 20 | #endif |
| 21 | #include <version.h> |
| 22 | #include <delay.h> |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 23 | #include <timer.h> |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 24 | #include "ipmi_kcs.h" |
| 25 | #include "chip.h" |
| 26 | |
| 27 | /* 4 bit encoding */ |
| 28 | static u8 ipmi_revision_major = 0x1; |
| 29 | static u8 ipmi_revision_minor = 0x0; |
| 30 | |
| 31 | static 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 Jang | 5015502 | 2019-11-06 10:24:47 +0800 | [diff] [blame] | 50 | static 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 71 | static void ipmi_kcs_init(struct device *dev) |
| 72 | { |
| 73 | struct ipmi_devid_rsp rsp; |
| 74 | uint32_t man_id = 0, prod_id = 0; |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 75 | struct drivers_ipmi_config *conf = NULL; |
Morgan Jang | 5015502 | 2019-11-06 10:24:47 +0800 | [diff] [blame] | 76 | struct ipmi_selftest_rsp selftestrsp; |
| 77 | uint8_t retry_count; |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 78 | |
| 79 | if (!dev->enabled) |
| 80 | return; |
| 81 | |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 82 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 87 | /* Get IPMI version for ACPI and SMBIOS */ |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 88 | 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 Jang | 5015502 | 2019-11-06 10:24:47 +0800 | [diff] [blame] | 106 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 142 | if (!ipmi_get_device_id(dev, &rsp)) { |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 143 | /* Queried the IPMI revision from BMC */ |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 144 | 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) |
| 164 | static uint32_t uid_cnt = 0; |
| 165 | |
| 166 | static unsigned long |
Furquan Shaikh | 0f007d8 | 2020-04-24 06:41:18 -0700 | [diff] [blame] | 167 | ipmi_write_acpi_tables(const struct device *dev, unsigned long current, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 168 | 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 178 | .bit_width = 8, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 179 | }; |
| 180 | |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 181 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 196 | 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 Shaikh | 0f6e652 | 2020-04-24 21:35:23 -0700 | [diff] [blame] | 214 | conf->uid); |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 215 | |
| 216 | acpi_add_table(rsdp, spmi); |
| 217 | |
| 218 | current += spmi->header.length; |
| 219 | |
| 220 | return current; |
| 221 | } |
| 222 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 223 | static void ipmi_ssdt(const struct device *dev) |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 224 | { |
| 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 Shaikh | 0f6e652 | 2020-04-24 21:35:23 -0700 | [diff] [blame] | 238 | conf->uid = uid_cnt++; |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 239 | |
| 240 | /* write SPMI device */ |
| 241 | acpigen_write_scope(scope); |
| 242 | acpigen_write_device("SPMI"); |
| 243 | acpigen_write_name_string("_HID", "IPI0001"); |
Patrick Rudolph | 389c827 | 2019-12-17 13:54:41 +0100 | [diff] [blame] | 244 | acpigen_write_name_unicode("_STR", "IPMI_KCS"); |
Furquan Shaikh | 0f6e652 | 2020-04-24 21:35:23 -0700 | [diff] [blame] | 245 | acpigen_write_name_byte("_UID", conf->uid); |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 246 | acpigen_write_STA(0xf); |
| 247 | acpigen_write_name("_CRS"); |
| 248 | acpigen_write_resourcetemplate_header(); |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 249 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 252 | |
| 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) |
| 276 | static 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 282 | u8 register_spacing; |
| 283 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 284 | 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 295 | 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 Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 310 | // 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 Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame] | 318 | register_spacing, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 319 | 0); // no IRQ |
| 320 | |
| 321 | return len; |
| 322 | } |
| 323 | #endif |
| 324 | |
| 325 | static 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 | |
| 338 | static 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 | |
| 346 | static struct device_operations ops = { |
| 347 | .read_resources = ipmi_read_resources, |
| 348 | .set_resources = ipmi_set_resources, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 349 | .init = ipmi_kcs_init, |
| 350 | #if CONFIG(HAVE_ACPI_TABLES) |
| 351 | .write_acpi_tables = ipmi_write_acpi_tables, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 352 | .acpi_fill_ssdt = ipmi_ssdt, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 353 | #endif |
| 354 | #if CONFIG(GENERATE_SMBIOS_TABLES) |
| 355 | .get_smbios_data = ipmi_smbios_data, |
| 356 | #endif |
| 357 | }; |
| 358 | |
| 359 | static 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 | |
| 371 | struct chip_operations drivers_ipmi_ops = { |
| 372 | CHIP_NAME("IPMI KCS") |
| 373 | .enable_dev = enable_dev, |
| 374 | }; |