| 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * ipmi_si_platform.c |
| 4 | * |
| 5 | * Handling for platform devices in IPMI (ACPI, OF, and things |
| 6 | * coming from the platform. |
| 7 | */ |
| 8 | |
| 9 | #define pr_fmt(fmt) "ipmi_platform: " fmt |
| 10 | #define dev_fmt pr_fmt |
| 11 | |
| 12 | #include <linux/types.h> |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/of_device.h> |
| 15 | #include <linux/of_platform.h> |
| 16 | #include <linux/of_address.h> |
| 17 | #include <linux/of_irq.h> |
| 18 | #include <linux/acpi.h> |
| 19 | #include "ipmi_si.h" |
| 20 | #include "ipmi_dmi.h" |
| 21 | |
| 22 | static bool si_tryplatform = true; |
| 23 | #ifdef CONFIG_ACPI |
| 24 | static bool si_tryacpi = true; |
| 25 | #endif |
| 26 | #ifdef CONFIG_OF |
| 27 | static bool si_tryopenfirmware = true; |
| 28 | #endif |
| 29 | #ifdef CONFIG_DMI |
| 30 | static bool si_trydmi = true; |
| 31 | #else |
| 32 | static bool si_trydmi = false; |
| 33 | #endif |
| 34 | |
| 35 | module_param_named(tryplatform, si_tryplatform, bool, 0); |
| 36 | MODULE_PARM_DESC(tryplatform, "Setting this to zero will disable the" |
| 37 | " default scan of the interfaces identified via platform" |
| 38 | " interfaces besides ACPI, OpenFirmware, and DMI"); |
| 39 | #ifdef CONFIG_ACPI |
| 40 | module_param_named(tryacpi, si_tryacpi, bool, 0); |
| 41 | MODULE_PARM_DESC(tryacpi, "Setting this to zero will disable the" |
| 42 | " default scan of the interfaces identified via ACPI"); |
| 43 | #endif |
| 44 | #ifdef CONFIG_OF |
| 45 | module_param_named(tryopenfirmware, si_tryopenfirmware, bool, 0); |
| 46 | MODULE_PARM_DESC(tryopenfirmware, "Setting this to zero will disable the" |
| 47 | " default scan of the interfaces identified via OpenFirmware"); |
| 48 | #endif |
| 49 | #ifdef CONFIG_DMI |
| 50 | module_param_named(trydmi, si_trydmi, bool, 0); |
| 51 | MODULE_PARM_DESC(trydmi, "Setting this to zero will disable the" |
| 52 | " default scan of the interfaces identified via DMI"); |
| 53 | #endif |
| 54 | |
| 55 | #ifdef CONFIG_ACPI |
| 56 | /* For GPE-type interrupts. */ |
| 57 | static u32 ipmi_acpi_gpe(acpi_handle gpe_device, |
| 58 | u32 gpe_number, void *context) |
| 59 | { |
| 60 | struct si_sm_io *io = context; |
| 61 | |
| 62 | ipmi_si_irq_handler(io->irq, io->irq_handler_data); |
| 63 | return ACPI_INTERRUPT_HANDLED; |
| 64 | } |
| 65 | |
| 66 | static void acpi_gpe_irq_cleanup(struct si_sm_io *io) |
| 67 | { |
| 68 | if (!io->irq) |
| 69 | return; |
| 70 | |
| 71 | ipmi_irq_start_cleanup(io); |
| 72 | acpi_remove_gpe_handler(NULL, io->irq, &ipmi_acpi_gpe); |
| 73 | } |
| 74 | |
| 75 | static int acpi_gpe_irq_setup(struct si_sm_io *io) |
| 76 | { |
| 77 | acpi_status status; |
| 78 | |
| 79 | if (!io->irq) |
| 80 | return 0; |
| 81 | |
| 82 | status = acpi_install_gpe_handler(NULL, |
| 83 | io->irq, |
| 84 | ACPI_GPE_LEVEL_TRIGGERED, |
| 85 | &ipmi_acpi_gpe, |
| 86 | io); |
| 87 | if (status != AE_OK) { |
| 88 | dev_warn(io->dev, |
| 89 | "Unable to claim ACPI GPE %d, running polled\n", |
| 90 | io->irq); |
| 91 | io->irq = 0; |
| 92 | return -EINVAL; |
| 93 | } else { |
| 94 | io->irq_cleanup = acpi_gpe_irq_cleanup; |
| 95 | ipmi_irq_finish_setup(io); |
| 96 | dev_info(io->dev, "Using ACPI GPE %d\n", io->irq); |
| 97 | return 0; |
| 98 | } |
| 99 | } |
| 100 | #endif |
| 101 | |
| 102 | static struct resource * |
| 103 | ipmi_get_info_from_resources(struct platform_device *pdev, |
| 104 | struct si_sm_io *io) |
| 105 | { |
| 106 | struct resource *res, *res_second; |
| 107 | |
| 108 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 109 | if (res) { |
| 110 | io->addr_space = IPMI_IO_ADDR_SPACE; |
| 111 | } else { |
| 112 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 113 | if (res) |
| 114 | io->addr_space = IPMI_MEM_ADDR_SPACE; |
| 115 | } |
| 116 | if (!res) { |
| 117 | dev_err(&pdev->dev, "no I/O or memory address\n"); |
| 118 | return NULL; |
| 119 | } |
| 120 | io->addr_data = res->start; |
| 121 | |
| 122 | io->regspacing = DEFAULT_REGSPACING; |
| 123 | res_second = platform_get_resource(pdev, |
| 124 | (io->addr_space == IPMI_IO_ADDR_SPACE) ? |
| 125 | IORESOURCE_IO : IORESOURCE_MEM, |
| 126 | 1); |
| 127 | if (res_second) { |
| 128 | if (res_second->start > io->addr_data) |
| 129 | io->regspacing = res_second->start - io->addr_data; |
| 130 | } |
| 131 | |
| 132 | return res; |
| 133 | } |
| 134 | |
| 135 | static int platform_ipmi_probe(struct platform_device *pdev) |
| 136 | { |
| 137 | struct si_sm_io io; |
| 138 | u8 type, slave_addr, addr_source, regsize, regshift; |
| 139 | int rv; |
| 140 | |
| 141 | rv = device_property_read_u8(&pdev->dev, "addr-source", &addr_source); |
| 142 | if (rv) |
| 143 | addr_source = SI_PLATFORM; |
| 144 | if (addr_source >= SI_LAST) |
| 145 | return -EINVAL; |
| 146 | |
| 147 | if (addr_source == SI_SMBIOS) { |
| 148 | if (!si_trydmi) |
| 149 | return -ENODEV; |
| 150 | } else if (addr_source != SI_HARDCODED) { |
| 151 | if (!si_tryplatform) |
| 152 | return -ENODEV; |
| 153 | } |
| 154 | |
| 155 | rv = device_property_read_u8(&pdev->dev, "ipmi-type", &type); |
| 156 | if (rv) |
| 157 | return -ENODEV; |
| 158 | |
| 159 | memset(&io, 0, sizeof(io)); |
| 160 | io.addr_source = addr_source; |
| 161 | dev_info(&pdev->dev, "probing via %s\n", |
| 162 | ipmi_addr_src_to_str(addr_source)); |
| 163 | |
| 164 | switch (type) { |
| 165 | case SI_KCS: |
| 166 | case SI_SMIC: |
| 167 | case SI_BT: |
| 168 | io.si_type = type; |
| 169 | break; |
| 170 | case SI_TYPE_INVALID: /* User disabled this in hardcode. */ |
| 171 | return -ENODEV; |
| 172 | default: |
| 173 | dev_err(&pdev->dev, "ipmi-type property is invalid\n"); |
| 174 | return -EINVAL; |
| 175 | } |
| 176 | |
| 177 | io.regsize = DEFAULT_REGSIZE; |
| 178 | rv = device_property_read_u8(&pdev->dev, "reg-size", ®size); |
| 179 | if (!rv) |
| 180 | io.regsize = regsize; |
| 181 | |
| 182 | io.regshift = 0; |
| 183 | rv = device_property_read_u8(&pdev->dev, "reg-shift", ®shift); |
| 184 | if (!rv) |
| 185 | io.regshift = regshift; |
| 186 | |
| 187 | if (!ipmi_get_info_from_resources(pdev, &io)) |
| 188 | return -EINVAL; |
| 189 | |
| 190 | rv = device_property_read_u8(&pdev->dev, "slave-addr", &slave_addr); |
| 191 | if (rv) { |
| 192 | dev_warn(&pdev->dev, "device has no slave-addr property\n"); |
| 193 | io.slave_addr = 0x20; |
| 194 | } else { |
| 195 | io.slave_addr = slave_addr; |
| 196 | } |
| 197 | |
| 198 | io.irq = platform_get_irq(pdev, 0); |
| 199 | if (io.irq > 0) |
| 200 | io.irq_setup = ipmi_std_irq_setup; |
| 201 | else |
| 202 | io.irq = 0; |
| 203 | |
| 204 | io.dev = &pdev->dev; |
| 205 | |
| 206 | pr_info("ipmi_si: %s: %s %#lx regsize %d spacing %d irq %d\n", |
| 207 | ipmi_addr_src_to_str(addr_source), |
| 208 | (io.addr_space == IPMI_IO_ADDR_SPACE) ? "io" : "mem", |
| 209 | io.addr_data, io.regsize, io.regspacing, io.irq); |
| 210 | |
| 211 | ipmi_si_add_smi(&io); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | #ifdef CONFIG_OF |
| 217 | static const struct of_device_id of_ipmi_match[] = { |
| 218 | { .type = "ipmi", .compatible = "ipmi-kcs", |
| 219 | .data = (void *)(unsigned long) SI_KCS }, |
| 220 | { .type = "ipmi", .compatible = "ipmi-smic", |
| 221 | .data = (void *)(unsigned long) SI_SMIC }, |
| 222 | { .type = "ipmi", .compatible = "ipmi-bt", |
| 223 | .data = (void *)(unsigned long) SI_BT }, |
| 224 | {}, |
| 225 | }; |
| 226 | MODULE_DEVICE_TABLE(of, of_ipmi_match); |
| 227 | |
| 228 | static int of_ipmi_probe(struct platform_device *pdev) |
| 229 | { |
| 230 | const struct of_device_id *match; |
| 231 | struct si_sm_io io; |
| 232 | struct resource resource; |
| 233 | const __be32 *regsize, *regspacing, *regshift; |
| 234 | struct device_node *np = pdev->dev.of_node; |
| 235 | int ret; |
| 236 | int proplen; |
| 237 | |
| 238 | if (!si_tryopenfirmware) |
| 239 | return -ENODEV; |
| 240 | |
| 241 | dev_info(&pdev->dev, "probing via device tree\n"); |
| 242 | |
| 243 | match = of_match_device(of_ipmi_match, &pdev->dev); |
| 244 | if (!match) |
| 245 | return -ENODEV; |
| 246 | |
| 247 | if (!of_device_is_available(np)) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | ret = of_address_to_resource(np, 0, &resource); |
| 251 | if (ret) { |
| 252 | dev_warn(&pdev->dev, "invalid address from OF\n"); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | regsize = of_get_property(np, "reg-size", &proplen); |
| 257 | if (regsize && proplen != 4) { |
| 258 | dev_warn(&pdev->dev, "invalid regsize from OF\n"); |
| 259 | return -EINVAL; |
| 260 | } |
| 261 | |
| 262 | regspacing = of_get_property(np, "reg-spacing", &proplen); |
| 263 | if (regspacing && proplen != 4) { |
| 264 | dev_warn(&pdev->dev, "invalid regspacing from OF\n"); |
| 265 | return -EINVAL; |
| 266 | } |
| 267 | |
| 268 | regshift = of_get_property(np, "reg-shift", &proplen); |
| 269 | if (regshift && proplen != 4) { |
| 270 | dev_warn(&pdev->dev, "invalid regshift from OF\n"); |
| 271 | return -EINVAL; |
| 272 | } |
| 273 | |
| 274 | memset(&io, 0, sizeof(io)); |
| 275 | io.si_type = (enum si_type) match->data; |
| 276 | io.addr_source = SI_DEVICETREE; |
| 277 | io.irq_setup = ipmi_std_irq_setup; |
| 278 | |
| 279 | if (resource.flags & IORESOURCE_IO) |
| 280 | io.addr_space = IPMI_IO_ADDR_SPACE; |
| 281 | else |
| 282 | io.addr_space = IPMI_MEM_ADDR_SPACE; |
| 283 | |
| 284 | io.addr_data = resource.start; |
| 285 | |
| 286 | io.regsize = regsize ? be32_to_cpup(regsize) : DEFAULT_REGSIZE; |
| 287 | io.regspacing = regspacing ? be32_to_cpup(regspacing) : DEFAULT_REGSPACING; |
| 288 | io.regshift = regshift ? be32_to_cpup(regshift) : 0; |
| 289 | |
| 290 | io.irq = irq_of_parse_and_map(pdev->dev.of_node, 0); |
| 291 | io.dev = &pdev->dev; |
| 292 | |
| 293 | dev_dbg(&pdev->dev, "addr 0x%lx regsize %d spacing %d irq %d\n", |
| 294 | io.addr_data, io.regsize, io.regspacing, io.irq); |
| 295 | |
| 296 | return ipmi_si_add_smi(&io); |
| 297 | } |
| 298 | #else |
| 299 | #define of_ipmi_match NULL |
| 300 | static int of_ipmi_probe(struct platform_device *dev) |
| 301 | { |
| 302 | return -ENODEV; |
| 303 | } |
| 304 | #endif |
| 305 | |
| 306 | #ifdef CONFIG_ACPI |
| 307 | static int find_slave_address(struct si_sm_io *io, int slave_addr) |
| 308 | { |
| 309 | #ifdef CONFIG_IPMI_DMI_DECODE |
| 310 | if (!slave_addr) |
| 311 | slave_addr = ipmi_dmi_get_slave_addr(io->si_type, |
| 312 | io->addr_space, |
| 313 | io->addr_data); |
| 314 | #endif |
| 315 | |
| 316 | return slave_addr; |
| 317 | } |
| 318 | |
| 319 | static int acpi_ipmi_probe(struct platform_device *pdev) |
| 320 | { |
| 321 | struct si_sm_io io; |
| 322 | acpi_handle handle; |
| 323 | acpi_status status; |
| 324 | unsigned long long tmp; |
| 325 | struct resource *res; |
| 326 | int rv = -EINVAL; |
| 327 | |
| 328 | if (!si_tryacpi) |
| 329 | return -ENODEV; |
| 330 | |
| 331 | handle = ACPI_HANDLE(&pdev->dev); |
| 332 | if (!handle) |
| 333 | return -ENODEV; |
| 334 | |
| 335 | memset(&io, 0, sizeof(io)); |
| 336 | io.addr_source = SI_ACPI; |
| 337 | dev_info(&pdev->dev, "probing via ACPI\n"); |
| 338 | |
| 339 | io.addr_info.acpi_info.acpi_handle = handle; |
| 340 | |
| 341 | /* _IFT tells us the interface type: KCS, BT, etc */ |
| 342 | status = acpi_evaluate_integer(handle, "_IFT", NULL, &tmp); |
| 343 | if (ACPI_FAILURE(status)) { |
| 344 | dev_err(&pdev->dev, |
| 345 | "Could not find ACPI IPMI interface type\n"); |
| 346 | goto err_free; |
| 347 | } |
| 348 | |
| 349 | switch (tmp) { |
| 350 | case 1: |
| 351 | io.si_type = SI_KCS; |
| 352 | break; |
| 353 | case 2: |
| 354 | io.si_type = SI_SMIC; |
| 355 | break; |
| 356 | case 3: |
| 357 | io.si_type = SI_BT; |
| 358 | break; |
| 359 | case 4: /* SSIF, just ignore */ |
| 360 | rv = -ENODEV; |
| 361 | goto err_free; |
| 362 | default: |
| 363 | dev_info(&pdev->dev, "unknown IPMI type %lld\n", tmp); |
| 364 | goto err_free; |
| 365 | } |
| 366 | |
| 367 | io.regsize = DEFAULT_REGSIZE; |
| 368 | io.regshift = 0; |
| 369 | |
| 370 | res = ipmi_get_info_from_resources(pdev, &io); |
| 371 | if (!res) { |
| 372 | rv = -EINVAL; |
| 373 | goto err_free; |
| 374 | } |
| 375 | |
| 376 | /* If _GPE exists, use it; otherwise use standard interrupts */ |
| 377 | status = acpi_evaluate_integer(handle, "_GPE", NULL, &tmp); |
| 378 | if (ACPI_SUCCESS(status)) { |
| 379 | io.irq = tmp; |
| 380 | io.irq_setup = acpi_gpe_irq_setup; |
| 381 | } else { |
| 382 | int irq = platform_get_irq(pdev, 0); |
| 383 | |
| 384 | if (irq > 0) { |
| 385 | io.irq = irq; |
| 386 | io.irq_setup = ipmi_std_irq_setup; |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | io.slave_addr = find_slave_address(&io, io.slave_addr); |
| 391 | |
| 392 | io.dev = &pdev->dev; |
| 393 | |
| 394 | dev_info(io.dev, "%pR regsize %d spacing %d irq %d\n", |
| 395 | res, io.regsize, io.regspacing, io.irq); |
| 396 | |
| 397 | return ipmi_si_add_smi(&io); |
| 398 | |
| 399 | err_free: |
| 400 | return rv; |
| 401 | } |
| 402 | |
| 403 | static const struct acpi_device_id acpi_ipmi_match[] = { |
| 404 | { "IPI0001", 0 }, |
| 405 | { }, |
| 406 | }; |
| 407 | MODULE_DEVICE_TABLE(acpi, acpi_ipmi_match); |
| 408 | #else |
| 409 | static int acpi_ipmi_probe(struct platform_device *dev) |
| 410 | { |
| 411 | return -ENODEV; |
| 412 | } |
| 413 | #endif |
| 414 | |
| 415 | static int ipmi_probe(struct platform_device *pdev) |
| 416 | { |
| 417 | if (pdev->dev.of_node && of_ipmi_probe(pdev) == 0) |
| 418 | return 0; |
| 419 | |
| 420 | if (acpi_ipmi_probe(pdev) == 0) |
| 421 | return 0; |
| 422 | |
| 423 | return platform_ipmi_probe(pdev); |
| 424 | } |
| 425 | |
| 426 | static int ipmi_remove(struct platform_device *pdev) |
| 427 | { |
| 428 | return ipmi_si_remove_by_dev(&pdev->dev); |
| 429 | } |
| 430 | |
| 431 | static const struct platform_device_id si_plat_ids[] = { |
| 432 | { "dmi-ipmi-si", 0 }, |
| 433 | { "hardcode-ipmi-si", 0 }, |
| 434 | { "hotmod-ipmi-si", 0 }, |
| 435 | { } |
| 436 | }; |
| 437 | |
| 438 | struct platform_driver ipmi_platform_driver = { |
| 439 | .driver = { |
| 440 | .name = DEVICE_NAME, |
| 441 | .of_match_table = of_ipmi_match, |
| 442 | .acpi_match_table = ACPI_PTR(acpi_ipmi_match), |
| 443 | }, |
| 444 | .probe = ipmi_probe, |
| 445 | .remove = ipmi_remove, |
| 446 | .id_table = si_plat_ids |
| 447 | }; |
| 448 | |
| 449 | void ipmi_si_platform_init(void) |
| 450 | { |
| 451 | int rv = platform_driver_register(&ipmi_platform_driver); |
| 452 | if (rv) |
| 453 | pr_err("Unable to register driver: %d\n", rv); |
| 454 | } |
| 455 | |
| 456 | void ipmi_si_platform_shutdown(void) |
| 457 | { |
| 458 | platform_driver_unregister(&ipmi_platform_driver); |
| 459 | } |