Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2019 9elements Agency GmbH |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; version 2 of |
| 9 | * the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * 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 Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 35 | #include <timer.h> |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 36 | #include "ipmi_kcs.h" |
| 37 | #include "chip.h" |
| 38 | |
| 39 | /* 4 bit encoding */ |
| 40 | static u8 ipmi_revision_major = 0x1; |
| 41 | static u8 ipmi_revision_minor = 0x0; |
| 42 | |
| 43 | static 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 | |
| 62 | static void ipmi_kcs_init(struct device *dev) |
| 63 | { |
| 64 | struct ipmi_devid_rsp rsp; |
| 65 | uint32_t man_id = 0, prod_id = 0; |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 66 | struct drivers_ipmi_config *conf = NULL; |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 67 | |
| 68 | if (!dev->enabled) |
| 69 | return; |
| 70 | |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 71 | printk(BIOS_DEBUG, "IPMI: PNP KCS 0x%x\n", dev->path.pnp.port); |
| 72 | |
| 73 | if (dev->chip_info) |
| 74 | conf = dev->chip_info; |
| 75 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 76 | /* Get IPMI version for ACPI and SMBIOS */ |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 77 | if (conf && conf->wait_for_bmc && conf->bmc_boot_timeout) { |
| 78 | struct stopwatch sw; |
| 79 | stopwatch_init_msecs_expire(&sw, conf->bmc_boot_timeout * 1000); |
| 80 | printk(BIOS_DEBUG, "IPMI: Waiting for BMC...\n"); |
| 81 | |
| 82 | while (!stopwatch_expired(&sw)) { |
| 83 | if (inb(dev->path.pnp.port) != 0xff) |
| 84 | break; |
| 85 | mdelay(100); |
| 86 | } |
| 87 | if (stopwatch_expired(&sw)) { |
| 88 | printk(BIOS_INFO, "IPMI: Waiting for BMC timed out\n"); |
| 89 | /* Don't write tables if communication failed */ |
| 90 | dev->enabled = 0; |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 95 | if (!ipmi_get_device_id(dev, &rsp)) { |
Patrick Rudolph | 3d41a13 | 2019-07-22 16:31:35 +0200 | [diff] [blame] | 96 | /* Queried the IPMI revision from BMC */ |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 97 | ipmi_revision_minor = IPMI_IPMI_VERSION_MINOR(rsp.ipmi_version); |
| 98 | ipmi_revision_major = IPMI_IPMI_VERSION_MAJOR(rsp.ipmi_version); |
| 99 | |
| 100 | memcpy(&man_id, rsp.manufacturer_id, |
| 101 | sizeof(rsp.manufacturer_id)); |
| 102 | |
| 103 | memcpy(&prod_id, rsp.product_id, sizeof(rsp.product_id)); |
| 104 | |
| 105 | printk(BIOS_INFO, "IPMI: Found man_id 0x%06x, prod_id 0x%04x\n", |
| 106 | man_id, prod_id); |
| 107 | |
| 108 | printk(BIOS_INFO, "IPMI: Version %01x.%01x\n", |
| 109 | ipmi_revision_major, ipmi_revision_minor); |
| 110 | } else { |
| 111 | /* Don't write tables if communication failed */ |
| 112 | dev->enabled = 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #if CONFIG(HAVE_ACPI_TABLES) |
| 117 | static uint32_t uid_cnt = 0; |
| 118 | |
| 119 | static unsigned long |
| 120 | ipmi_write_acpi_tables(struct device *dev, unsigned long current, |
| 121 | struct acpi_rsdp *rsdp) |
| 122 | { |
| 123 | struct drivers_ipmi_config *conf = NULL; |
| 124 | struct acpi_spmi *spmi; |
| 125 | s8 gpe_interrupt = -1; |
| 126 | u32 apic_interrupt = 0; |
| 127 | acpi_addr_t addr = { |
| 128 | .space_id = ACPI_ADDRESS_SPACE_IO, |
| 129 | .access_size = ACPI_ACCESS_SIZE_BYTE_ACCESS, |
| 130 | .addrl = dev->path.pnp.port, |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame^] | 131 | .bit_width = 8, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 132 | }; |
| 133 | |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame^] | 134 | switch (CONFIG_IPMI_KCS_REGISTER_SPACING) { |
| 135 | case 4: |
| 136 | addr.bit_offset = 32; |
| 137 | break; |
| 138 | case 16: |
| 139 | addr.bit_offset = 128; |
| 140 | break; |
| 141 | default: |
| 142 | printk(BIOS_ERR, "IPMI: Unsupported register spacing for SPMI\n"); |
| 143 | /* fall through */ |
| 144 | case 1: |
| 145 | addr.bit_offset = 8; |
| 146 | break; |
| 147 | } |
| 148 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 149 | current = ALIGN_UP(current, 8); |
| 150 | printk(BIOS_DEBUG, "ACPI: * SPMI at %lx\n", current); |
| 151 | spmi = (struct acpi_spmi *)current; |
| 152 | |
| 153 | if (dev->chip_info) |
| 154 | conf = dev->chip_info; |
| 155 | |
| 156 | if (conf) { |
| 157 | if (conf->have_gpe) |
| 158 | gpe_interrupt = conf->gpe_interrupt; |
| 159 | if (conf->have_apic) |
| 160 | apic_interrupt = conf->apic_interrupt; |
| 161 | } |
| 162 | |
| 163 | /* Use command to get UID from ipmi_ssdt */ |
| 164 | acpi_create_ipmi(dev, spmi, (ipmi_revision_major << 8) | |
| 165 | (ipmi_revision_minor << 4), &addr, |
| 166 | IPMI_INTERFACE_KCS, gpe_interrupt, apic_interrupt, |
| 167 | dev->command); |
| 168 | |
| 169 | acpi_add_table(rsdp, spmi); |
| 170 | |
| 171 | current += spmi->header.length; |
| 172 | |
| 173 | return current; |
| 174 | } |
| 175 | |
| 176 | static void ipmi_ssdt(struct device *dev) |
| 177 | { |
| 178 | const char *scope = acpi_device_scope(dev); |
| 179 | struct drivers_ipmi_config *conf = NULL; |
| 180 | |
| 181 | if (!scope) { |
| 182 | printk(BIOS_ERR, "IPMI: Missing ACPI scope for %s\n", |
| 183 | dev_path(dev)); |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (dev->chip_info) |
| 188 | conf = dev->chip_info; |
| 189 | |
| 190 | /* Use command to pass UID to ipmi_write_acpi_tables */ |
| 191 | dev->command = uid_cnt++; |
| 192 | |
| 193 | /* write SPMI device */ |
| 194 | acpigen_write_scope(scope); |
| 195 | acpigen_write_device("SPMI"); |
| 196 | acpigen_write_name_string("_HID", "IPI0001"); |
| 197 | acpigen_write_name_string("_STR", "IPMI_KCS"); |
| 198 | acpigen_write_name_byte("_UID", dev->command); |
| 199 | acpigen_write_STA(0xf); |
| 200 | acpigen_write_name("_CRS"); |
| 201 | acpigen_write_resourcetemplate_header(); |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame^] | 202 | acpigen_write_io16(dev->path.pnp.port, dev->path.pnp.port, 1, 1, 1); |
| 203 | acpigen_write_io16(dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, |
| 204 | dev->path.pnp.port + CONFIG_IPMI_KCS_REGISTER_SPACING, 1, 1, 1); |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 205 | |
| 206 | if (conf) { |
| 207 | // FIXME: is that correct? |
| 208 | if (conf->have_apic) |
| 209 | acpigen_write_irq(1 << conf->apic_interrupt); |
| 210 | } |
| 211 | |
| 212 | acpigen_write_resourcetemplate_footer(); |
| 213 | |
| 214 | acpigen_write_method("_IFT", 0); |
| 215 | acpigen_write_return_byte(1); // KCS |
| 216 | acpigen_pop_len(); |
| 217 | |
| 218 | acpigen_write_method("_SRV", 0); |
| 219 | acpigen_write_return_integer((ipmi_revision_major << 8) | |
| 220 | (ipmi_revision_minor << 4)); |
| 221 | acpigen_pop_len(); |
| 222 | |
| 223 | acpigen_pop_len(); /* pop device */ |
| 224 | acpigen_pop_len(); /* pop scope */ |
| 225 | } |
| 226 | #endif |
| 227 | |
| 228 | #if CONFIG(GENERATE_SMBIOS_TABLES) |
| 229 | static int ipmi_smbios_data(struct device *dev, int *handle, |
| 230 | unsigned long *current) |
| 231 | { |
| 232 | struct drivers_ipmi_config *conf = NULL; |
| 233 | u8 nv_storage = 0xff; |
| 234 | u8 i2c_address = 0; |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame^] | 235 | u8 register_spacing; |
| 236 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 237 | int len = 0; |
| 238 | |
| 239 | if (dev->chip_info) |
| 240 | conf = dev->chip_info; |
| 241 | |
| 242 | if (conf) { |
| 243 | if (conf->have_nv_storage) |
| 244 | nv_storage = conf->nv_storage_device_address; |
| 245 | i2c_address = conf->bmc_i2c_address; |
| 246 | } |
| 247 | |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame^] | 248 | switch (CONFIG_IPMI_KCS_REGISTER_SPACING) { |
| 249 | case 4: |
| 250 | register_spacing = 1 << 6; |
| 251 | break; |
| 252 | case 16: |
| 253 | register_spacing = 2 << 6; |
| 254 | break; |
| 255 | default: |
| 256 | printk(BIOS_ERR, "IPMI: Unsupported register spacing for SMBIOS\n"); |
| 257 | /* fall through */ |
| 258 | case 1: |
| 259 | register_spacing = 0 << 6; |
| 260 | break; |
| 261 | } |
| 262 | |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 263 | // add IPMI Device Information |
| 264 | len += smbios_write_type38( |
| 265 | current, handle, |
| 266 | SMBIOS_BMC_INTERFACE_KCS, |
| 267 | ipmi_revision_minor | (ipmi_revision_major << 4), |
| 268 | i2c_address, // I2C address |
| 269 | nv_storage, // NV storage |
| 270 | dev->path.pnp.port | 1, // IO interface |
Patrick Rudolph | a96c4a1 | 2019-08-29 19:44:32 +0200 | [diff] [blame^] | 271 | register_spacing, |
Patrick Rudolph | ffbc3b5 | 2019-06-06 15:45:51 +0200 | [diff] [blame] | 272 | 0); // no IRQ |
| 273 | |
| 274 | return len; |
| 275 | } |
| 276 | #endif |
| 277 | |
| 278 | static void ipmi_set_resources(struct device *dev) |
| 279 | { |
| 280 | struct resource *res; |
| 281 | |
| 282 | for (res = dev->resource_list; res; res = res->next) { |
| 283 | if (!(res->flags & IORESOURCE_ASSIGNED)) |
| 284 | continue; |
| 285 | |
| 286 | res->flags |= IORESOURCE_STORED; |
| 287 | report_resource_stored(dev, res, ""); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static void ipmi_read_resources(struct device *dev) |
| 292 | { |
| 293 | struct resource *res = new_resource(dev, 0); |
| 294 | res->base = dev->path.pnp.port; |
| 295 | res->size = 2; |
| 296 | res->flags = IORESOURCE_IO | IORESOURCE_ASSIGNED | IORESOURCE_FIXED; |
| 297 | } |
| 298 | |
| 299 | static struct device_operations ops = { |
| 300 | .read_resources = ipmi_read_resources, |
| 301 | .set_resources = ipmi_set_resources, |
| 302 | .enable_resources = DEVICE_NOOP, |
| 303 | .init = ipmi_kcs_init, |
| 304 | #if CONFIG(HAVE_ACPI_TABLES) |
| 305 | .write_acpi_tables = ipmi_write_acpi_tables, |
| 306 | .acpi_fill_ssdt_generator = ipmi_ssdt, |
| 307 | #endif |
| 308 | #if CONFIG(GENERATE_SMBIOS_TABLES) |
| 309 | .get_smbios_data = ipmi_smbios_data, |
| 310 | #endif |
| 311 | }; |
| 312 | |
| 313 | static void enable_dev(struct device *dev) |
| 314 | { |
| 315 | if (dev->path.type != DEVICE_PATH_PNP) |
| 316 | printk(BIOS_ERR, "%s: Unsupported device type\n", |
| 317 | dev_path(dev)); |
| 318 | else if (dev->path.pnp.port & 1) |
| 319 | printk(BIOS_ERR, "%s: Base address needs to be aligned to 2\n", |
| 320 | dev_path(dev)); |
| 321 | else |
| 322 | dev->ops = &ops; |
| 323 | } |
| 324 | |
| 325 | struct chip_operations drivers_ipmi_ops = { |
| 326 | CHIP_NAME("IPMI KCS") |
| 327 | .enable_dev = enable_dev, |
| 328 | }; |