David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * dcdbas.c: Dell Systems Management Base Driver |
| 4 | * |
| 5 | * The Dell Systems Management Base Driver provides a sysfs interface for |
| 6 | * systems management software to perform System Management Interrupts (SMIs) |
| 7 | * and Host Control Actions (power cycle or power off after OS shutdown) on |
| 8 | * Dell systems. |
| 9 | * |
| 10 | * See Documentation/driver-api/dcdbas.rst for more information. |
| 11 | * |
| 12 | * Copyright (C) 1995-2006 Dell Inc. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/acpi.h> |
| 17 | #include <linux/dma-mapping.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 18 | #include <linux/dmi.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 19 | #include <linux/errno.h> |
| 20 | #include <linux/cpu.h> |
| 21 | #include <linux/gfp.h> |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/io.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/mc146818rtc.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/reboot.h> |
| 28 | #include <linux/sched.h> |
| 29 | #include <linux/smp.h> |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/string.h> |
| 32 | #include <linux/types.h> |
| 33 | #include <linux/mutex.h> |
| 34 | |
| 35 | #include "dcdbas.h" |
| 36 | |
| 37 | #define DRIVER_NAME "dcdbas" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 38 | #define DRIVER_VERSION "5.6.0-3.4" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 39 | #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver" |
| 40 | |
| 41 | static struct platform_device *dcdbas_pdev; |
| 42 | |
| 43 | static u8 *smi_data_buf; |
| 44 | static dma_addr_t smi_data_buf_handle; |
| 45 | static unsigned long smi_data_buf_size; |
| 46 | static unsigned long max_smi_data_buf_size = MAX_SMI_DATA_BUF_SIZE; |
| 47 | static u32 smi_data_buf_phys_addr; |
| 48 | static DEFINE_MUTEX(smi_data_lock); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 49 | static u8 *bios_buffer; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 50 | |
| 51 | static unsigned int host_control_action; |
| 52 | static unsigned int host_control_smi_type; |
| 53 | static unsigned int host_control_on_shutdown; |
| 54 | |
| 55 | static bool wsmt_enabled; |
| 56 | |
| 57 | /** |
| 58 | * smi_data_buf_free: free SMI data buffer |
| 59 | */ |
| 60 | static void smi_data_buf_free(void) |
| 61 | { |
| 62 | if (!smi_data_buf || wsmt_enabled) |
| 63 | return; |
| 64 | |
| 65 | dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n", |
| 66 | __func__, smi_data_buf_phys_addr, smi_data_buf_size); |
| 67 | |
| 68 | dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf, |
| 69 | smi_data_buf_handle); |
| 70 | smi_data_buf = NULL; |
| 71 | smi_data_buf_handle = 0; |
| 72 | smi_data_buf_phys_addr = 0; |
| 73 | smi_data_buf_size = 0; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * smi_data_buf_realloc: grow SMI data buffer if needed |
| 78 | */ |
| 79 | static int smi_data_buf_realloc(unsigned long size) |
| 80 | { |
| 81 | void *buf; |
| 82 | dma_addr_t handle; |
| 83 | |
| 84 | if (smi_data_buf_size >= size) |
| 85 | return 0; |
| 86 | |
| 87 | if (size > max_smi_data_buf_size) |
| 88 | return -EINVAL; |
| 89 | |
| 90 | /* new buffer is needed */ |
| 91 | buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL); |
| 92 | if (!buf) { |
| 93 | dev_dbg(&dcdbas_pdev->dev, |
| 94 | "%s: failed to allocate memory size %lu\n", |
| 95 | __func__, size); |
| 96 | return -ENOMEM; |
| 97 | } |
| 98 | /* memory zeroed by dma_alloc_coherent */ |
| 99 | |
| 100 | if (smi_data_buf) |
| 101 | memcpy(buf, smi_data_buf, smi_data_buf_size); |
| 102 | |
| 103 | /* free any existing buffer */ |
| 104 | smi_data_buf_free(); |
| 105 | |
| 106 | /* set up new buffer for use */ |
| 107 | smi_data_buf = buf; |
| 108 | smi_data_buf_handle = handle; |
| 109 | smi_data_buf_phys_addr = (u32) virt_to_phys(buf); |
| 110 | smi_data_buf_size = size; |
| 111 | |
| 112 | dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n", |
| 113 | __func__, smi_data_buf_phys_addr, smi_data_buf_size); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static ssize_t smi_data_buf_phys_addr_show(struct device *dev, |
| 119 | struct device_attribute *attr, |
| 120 | char *buf) |
| 121 | { |
| 122 | return sprintf(buf, "%x\n", smi_data_buf_phys_addr); |
| 123 | } |
| 124 | |
| 125 | static ssize_t smi_data_buf_size_show(struct device *dev, |
| 126 | struct device_attribute *attr, |
| 127 | char *buf) |
| 128 | { |
| 129 | return sprintf(buf, "%lu\n", smi_data_buf_size); |
| 130 | } |
| 131 | |
| 132 | static ssize_t smi_data_buf_size_store(struct device *dev, |
| 133 | struct device_attribute *attr, |
| 134 | const char *buf, size_t count) |
| 135 | { |
| 136 | unsigned long buf_size; |
| 137 | ssize_t ret; |
| 138 | |
| 139 | buf_size = simple_strtoul(buf, NULL, 10); |
| 140 | |
| 141 | /* make sure SMI data buffer is at least buf_size */ |
| 142 | mutex_lock(&smi_data_lock); |
| 143 | ret = smi_data_buf_realloc(buf_size); |
| 144 | mutex_unlock(&smi_data_lock); |
| 145 | if (ret) |
| 146 | return ret; |
| 147 | |
| 148 | return count; |
| 149 | } |
| 150 | |
| 151 | static ssize_t smi_data_read(struct file *filp, struct kobject *kobj, |
| 152 | struct bin_attribute *bin_attr, |
| 153 | char *buf, loff_t pos, size_t count) |
| 154 | { |
| 155 | ssize_t ret; |
| 156 | |
| 157 | mutex_lock(&smi_data_lock); |
| 158 | ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf, |
| 159 | smi_data_buf_size); |
| 160 | mutex_unlock(&smi_data_lock); |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | static ssize_t smi_data_write(struct file *filp, struct kobject *kobj, |
| 165 | struct bin_attribute *bin_attr, |
| 166 | char *buf, loff_t pos, size_t count) |
| 167 | { |
| 168 | ssize_t ret; |
| 169 | |
| 170 | if ((pos + count) > max_smi_data_buf_size) |
| 171 | return -EINVAL; |
| 172 | |
| 173 | mutex_lock(&smi_data_lock); |
| 174 | |
| 175 | ret = smi_data_buf_realloc(pos + count); |
| 176 | if (ret) |
| 177 | goto out; |
| 178 | |
| 179 | memcpy(smi_data_buf + pos, buf, count); |
| 180 | ret = count; |
| 181 | out: |
| 182 | mutex_unlock(&smi_data_lock); |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | static ssize_t host_control_action_show(struct device *dev, |
| 187 | struct device_attribute *attr, |
| 188 | char *buf) |
| 189 | { |
| 190 | return sprintf(buf, "%u\n", host_control_action); |
| 191 | } |
| 192 | |
| 193 | static ssize_t host_control_action_store(struct device *dev, |
| 194 | struct device_attribute *attr, |
| 195 | const char *buf, size_t count) |
| 196 | { |
| 197 | ssize_t ret; |
| 198 | |
| 199 | /* make sure buffer is available for host control command */ |
| 200 | mutex_lock(&smi_data_lock); |
| 201 | ret = smi_data_buf_realloc(sizeof(struct apm_cmd)); |
| 202 | mutex_unlock(&smi_data_lock); |
| 203 | if (ret) |
| 204 | return ret; |
| 205 | |
| 206 | host_control_action = simple_strtoul(buf, NULL, 10); |
| 207 | return count; |
| 208 | } |
| 209 | |
| 210 | static ssize_t host_control_smi_type_show(struct device *dev, |
| 211 | struct device_attribute *attr, |
| 212 | char *buf) |
| 213 | { |
| 214 | return sprintf(buf, "%u\n", host_control_smi_type); |
| 215 | } |
| 216 | |
| 217 | static ssize_t host_control_smi_type_store(struct device *dev, |
| 218 | struct device_attribute *attr, |
| 219 | const char *buf, size_t count) |
| 220 | { |
| 221 | host_control_smi_type = simple_strtoul(buf, NULL, 10); |
| 222 | return count; |
| 223 | } |
| 224 | |
| 225 | static ssize_t host_control_on_shutdown_show(struct device *dev, |
| 226 | struct device_attribute *attr, |
| 227 | char *buf) |
| 228 | { |
| 229 | return sprintf(buf, "%u\n", host_control_on_shutdown); |
| 230 | } |
| 231 | |
| 232 | static ssize_t host_control_on_shutdown_store(struct device *dev, |
| 233 | struct device_attribute *attr, |
| 234 | const char *buf, size_t count) |
| 235 | { |
| 236 | host_control_on_shutdown = simple_strtoul(buf, NULL, 10); |
| 237 | return count; |
| 238 | } |
| 239 | |
| 240 | static int raise_smi(void *par) |
| 241 | { |
| 242 | struct smi_cmd *smi_cmd = par; |
| 243 | |
| 244 | if (smp_processor_id() != 0) { |
| 245 | dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n", |
| 246 | __func__); |
| 247 | return -EBUSY; |
| 248 | } |
| 249 | |
| 250 | /* generate SMI */ |
| 251 | /* inb to force posted write through and make SMI happen now */ |
| 252 | asm volatile ( |
| 253 | "outb %b0,%w1\n" |
| 254 | "inb %w1" |
| 255 | : /* no output args */ |
| 256 | : "a" (smi_cmd->command_code), |
| 257 | "d" (smi_cmd->command_address), |
| 258 | "b" (smi_cmd->ebx), |
| 259 | "c" (smi_cmd->ecx) |
| 260 | : "memory" |
| 261 | ); |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | /** |
| 266 | * dcdbas_smi_request: generate SMI request |
| 267 | * |
| 268 | * Called with smi_data_lock. |
| 269 | */ |
| 270 | int dcdbas_smi_request(struct smi_cmd *smi_cmd) |
| 271 | { |
| 272 | int ret; |
| 273 | |
| 274 | if (smi_cmd->magic != SMI_CMD_MAGIC) { |
| 275 | dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n", |
| 276 | __func__); |
| 277 | return -EBADR; |
| 278 | } |
| 279 | |
| 280 | /* SMI requires CPU 0 */ |
| 281 | get_online_cpus(); |
| 282 | ret = smp_call_on_cpu(0, raise_smi, smi_cmd, true); |
| 283 | put_online_cpus(); |
| 284 | |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * smi_request_store: |
| 290 | * |
| 291 | * The valid values are: |
| 292 | * 0: zero SMI data buffer |
| 293 | * 1: generate calling interface SMI |
| 294 | * 2: generate raw SMI |
| 295 | * |
| 296 | * User application writes smi_cmd to smi_data before telling driver |
| 297 | * to generate SMI. |
| 298 | */ |
| 299 | static ssize_t smi_request_store(struct device *dev, |
| 300 | struct device_attribute *attr, |
| 301 | const char *buf, size_t count) |
| 302 | { |
| 303 | struct smi_cmd *smi_cmd; |
| 304 | unsigned long val = simple_strtoul(buf, NULL, 10); |
| 305 | ssize_t ret; |
| 306 | |
| 307 | mutex_lock(&smi_data_lock); |
| 308 | |
| 309 | if (smi_data_buf_size < sizeof(struct smi_cmd)) { |
| 310 | ret = -ENODEV; |
| 311 | goto out; |
| 312 | } |
| 313 | smi_cmd = (struct smi_cmd *)smi_data_buf; |
| 314 | |
| 315 | switch (val) { |
| 316 | case 2: |
| 317 | /* Raw SMI */ |
| 318 | ret = dcdbas_smi_request(smi_cmd); |
| 319 | if (!ret) |
| 320 | ret = count; |
| 321 | break; |
| 322 | case 1: |
| 323 | /* |
| 324 | * Calling Interface SMI |
| 325 | * |
| 326 | * Provide physical address of command buffer field within |
| 327 | * the struct smi_cmd to BIOS. |
| 328 | * |
| 329 | * Because the address that smi_cmd (smi_data_buf) points to |
| 330 | * will be from memremap() of a non-memory address if WSMT |
| 331 | * is present, we can't use virt_to_phys() on smi_cmd, so |
| 332 | * we have to use the physical address that was saved when |
| 333 | * the virtual address for smi_cmd was received. |
| 334 | */ |
| 335 | smi_cmd->ebx = smi_data_buf_phys_addr + |
| 336 | offsetof(struct smi_cmd, command_buffer); |
| 337 | ret = dcdbas_smi_request(smi_cmd); |
| 338 | if (!ret) |
| 339 | ret = count; |
| 340 | break; |
| 341 | case 0: |
| 342 | memset(smi_data_buf, 0, smi_data_buf_size); |
| 343 | ret = count; |
| 344 | break; |
| 345 | default: |
| 346 | ret = -EINVAL; |
| 347 | break; |
| 348 | } |
| 349 | |
| 350 | out: |
| 351 | mutex_unlock(&smi_data_lock); |
| 352 | return ret; |
| 353 | } |
| 354 | EXPORT_SYMBOL(dcdbas_smi_request); |
| 355 | |
| 356 | /** |
| 357 | * host_control_smi: generate host control SMI |
| 358 | * |
| 359 | * Caller must set up the host control command in smi_data_buf. |
| 360 | */ |
| 361 | static int host_control_smi(void) |
| 362 | { |
| 363 | struct apm_cmd *apm_cmd; |
| 364 | u8 *data; |
| 365 | unsigned long flags; |
| 366 | u32 num_ticks; |
| 367 | s8 cmd_status; |
| 368 | u8 index; |
| 369 | |
| 370 | apm_cmd = (struct apm_cmd *)smi_data_buf; |
| 371 | apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL; |
| 372 | |
| 373 | switch (host_control_smi_type) { |
| 374 | case HC_SMITYPE_TYPE1: |
| 375 | spin_lock_irqsave(&rtc_lock, flags); |
| 376 | /* write SMI data buffer physical address */ |
| 377 | data = (u8 *)&smi_data_buf_phys_addr; |
| 378 | for (index = PE1300_CMOS_CMD_STRUCT_PTR; |
| 379 | index < (PE1300_CMOS_CMD_STRUCT_PTR + 4); |
| 380 | index++, data++) { |
| 381 | outb(index, |
| 382 | (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4)); |
| 383 | outb(*data, |
| 384 | (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4)); |
| 385 | } |
| 386 | |
| 387 | /* first set status to -1 as called by spec */ |
| 388 | cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL; |
| 389 | outb((u8) cmd_status, PCAT_APM_STATUS_PORT); |
| 390 | |
| 391 | /* generate SMM call */ |
| 392 | outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT); |
| 393 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 394 | |
| 395 | /* wait a few to see if it executed */ |
| 396 | num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING; |
| 397 | while ((cmd_status = inb(PCAT_APM_STATUS_PORT)) |
| 398 | == ESM_STATUS_CMD_UNSUCCESSFUL) { |
| 399 | num_ticks--; |
| 400 | if (num_ticks == EXPIRED_TIMER) |
| 401 | return -ETIME; |
| 402 | } |
| 403 | break; |
| 404 | |
| 405 | case HC_SMITYPE_TYPE2: |
| 406 | case HC_SMITYPE_TYPE3: |
| 407 | spin_lock_irqsave(&rtc_lock, flags); |
| 408 | /* write SMI data buffer physical address */ |
| 409 | data = (u8 *)&smi_data_buf_phys_addr; |
| 410 | for (index = PE1400_CMOS_CMD_STRUCT_PTR; |
| 411 | index < (PE1400_CMOS_CMD_STRUCT_PTR + 4); |
| 412 | index++, data++) { |
| 413 | outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT)); |
| 414 | outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT)); |
| 415 | } |
| 416 | |
| 417 | /* generate SMM call */ |
| 418 | if (host_control_smi_type == HC_SMITYPE_TYPE3) |
| 419 | outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT); |
| 420 | else |
| 421 | outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT); |
| 422 | |
| 423 | /* restore RTC index pointer since it was written to above */ |
| 424 | CMOS_READ(RTC_REG_C); |
| 425 | spin_unlock_irqrestore(&rtc_lock, flags); |
| 426 | |
| 427 | /* read control port back to serialize write */ |
| 428 | cmd_status = inb(PE1400_APM_CONTROL_PORT); |
| 429 | |
| 430 | /* wait a few to see if it executed */ |
| 431 | num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING; |
| 432 | while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) { |
| 433 | num_ticks--; |
| 434 | if (num_ticks == EXPIRED_TIMER) |
| 435 | return -ETIME; |
| 436 | } |
| 437 | break; |
| 438 | |
| 439 | default: |
| 440 | dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n", |
| 441 | __func__, host_control_smi_type); |
| 442 | return -ENOSYS; |
| 443 | } |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * dcdbas_host_control: initiate host control |
| 450 | * |
| 451 | * This function is called by the driver after the system has |
| 452 | * finished shutting down if the user application specified a |
| 453 | * host control action to perform on shutdown. It is safe to |
| 454 | * use smi_data_buf at this point because the system has finished |
| 455 | * shutting down and no userspace apps are running. |
| 456 | */ |
| 457 | static void dcdbas_host_control(void) |
| 458 | { |
| 459 | struct apm_cmd *apm_cmd; |
| 460 | u8 action; |
| 461 | |
| 462 | if (host_control_action == HC_ACTION_NONE) |
| 463 | return; |
| 464 | |
| 465 | action = host_control_action; |
| 466 | host_control_action = HC_ACTION_NONE; |
| 467 | |
| 468 | if (!smi_data_buf) { |
| 469 | dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__); |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | if (smi_data_buf_size < sizeof(struct apm_cmd)) { |
| 474 | dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n", |
| 475 | __func__); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | apm_cmd = (struct apm_cmd *)smi_data_buf; |
| 480 | |
| 481 | /* power off takes precedence */ |
| 482 | if (action & HC_ACTION_HOST_CONTROL_POWEROFF) { |
| 483 | apm_cmd->command = ESM_APM_POWER_CYCLE; |
| 484 | apm_cmd->reserved = 0; |
| 485 | *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0; |
| 486 | host_control_smi(); |
| 487 | } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) { |
| 488 | apm_cmd->command = ESM_APM_POWER_CYCLE; |
| 489 | apm_cmd->reserved = 0; |
| 490 | *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20; |
| 491 | host_control_smi(); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /* WSMT */ |
| 496 | |
| 497 | static u8 checksum(u8 *buffer, u8 length) |
| 498 | { |
| 499 | u8 sum = 0; |
| 500 | u8 *end = buffer + length; |
| 501 | |
| 502 | while (buffer < end) |
| 503 | sum += *buffer++; |
| 504 | return sum; |
| 505 | } |
| 506 | |
| 507 | static inline struct smm_eps_table *check_eps_table(u8 *addr) |
| 508 | { |
| 509 | struct smm_eps_table *eps = (struct smm_eps_table *)addr; |
| 510 | |
| 511 | if (strncmp(eps->smm_comm_buff_anchor, SMM_EPS_SIG, 4) != 0) |
| 512 | return NULL; |
| 513 | |
| 514 | if (checksum(addr, eps->length) != 0) |
| 515 | return NULL; |
| 516 | |
| 517 | return eps; |
| 518 | } |
| 519 | |
| 520 | static int dcdbas_check_wsmt(void) |
| 521 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 522 | const struct dmi_device *dev = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 523 | struct acpi_table_wsmt *wsmt = NULL; |
| 524 | struct smm_eps_table *eps = NULL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 525 | u64 bios_buf_paddr; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 526 | u64 remap_size; |
| 527 | u8 *addr; |
| 528 | |
| 529 | acpi_get_table(ACPI_SIG_WSMT, 0, (struct acpi_table_header **)&wsmt); |
| 530 | if (!wsmt) |
| 531 | return 0; |
| 532 | |
| 533 | /* Check if WSMT ACPI table shows that protection is enabled */ |
| 534 | if (!(wsmt->protection_flags & ACPI_WSMT_FIXED_COMM_BUFFERS) || |
| 535 | !(wsmt->protection_flags & ACPI_WSMT_COMM_BUFFER_NESTED_PTR_PROTECTION)) |
| 536 | return 0; |
| 537 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 538 | /* |
| 539 | * BIOS could provide the address/size of the protected buffer |
| 540 | * in an SMBIOS string or in an EPS structure in 0xFxxxx. |
| 541 | */ |
| 542 | |
| 543 | /* Check SMBIOS for buffer address */ |
| 544 | while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev))) |
| 545 | if (sscanf(dev->name, "30[%16llx;%8llx]", &bios_buf_paddr, |
| 546 | &remap_size) == 2) |
| 547 | goto remap; |
| 548 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 549 | /* Scan for EPS (entry point structure) */ |
| 550 | for (addr = (u8 *)__va(0xf0000); |
| 551 | addr < (u8 *)__va(0x100000 - sizeof(struct smm_eps_table)); |
| 552 | addr += 16) { |
| 553 | eps = check_eps_table(addr); |
| 554 | if (eps) |
| 555 | break; |
| 556 | } |
| 557 | |
| 558 | if (!eps) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 559 | dev_dbg(&dcdbas_pdev->dev, "found WSMT, but no firmware buffer found\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 560 | return -ENODEV; |
| 561 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 562 | bios_buf_paddr = eps->smm_comm_buff_addr; |
| 563 | remap_size = eps->num_of_4k_pages * PAGE_SIZE; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 564 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 565 | remap: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 566 | /* |
| 567 | * Get physical address of buffer and map to virtual address. |
| 568 | * Table gives size in 4K pages, regardless of actual system page size. |
| 569 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 570 | if (upper_32_bits(bios_buf_paddr + 8)) { |
| 571 | dev_warn(&dcdbas_pdev->dev, "found WSMT, but buffer address is above 4GB\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 572 | return -EINVAL; |
| 573 | } |
| 574 | /* |
| 575 | * Limit remap size to MAX_SMI_DATA_BUF_SIZE + 8 (since the first 8 |
| 576 | * bytes are used for a semaphore, not the data buffer itself). |
| 577 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 578 | if (remap_size > MAX_SMI_DATA_BUF_SIZE + 8) |
| 579 | remap_size = MAX_SMI_DATA_BUF_SIZE + 8; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 580 | |
| 581 | bios_buffer = memremap(bios_buf_paddr, remap_size, MEMREMAP_WB); |
| 582 | if (!bios_buffer) { |
| 583 | dev_warn(&dcdbas_pdev->dev, "found WSMT, but failed to map buffer\n"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 584 | return -ENOMEM; |
| 585 | } |
| 586 | |
| 587 | /* First 8 bytes is for a semaphore, not part of the smi_data_buf */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 588 | smi_data_buf_phys_addr = bios_buf_paddr + 8; |
| 589 | smi_data_buf = bios_buffer + 8; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 590 | smi_data_buf_size = remap_size - 8; |
| 591 | max_smi_data_buf_size = smi_data_buf_size; |
| 592 | wsmt_enabled = true; |
| 593 | dev_info(&dcdbas_pdev->dev, |
| 594 | "WSMT found, using firmware-provided SMI buffer.\n"); |
| 595 | return 1; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * dcdbas_reboot_notify: handle reboot notification for host control |
| 600 | */ |
| 601 | static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code, |
| 602 | void *unused) |
| 603 | { |
| 604 | switch (code) { |
| 605 | case SYS_DOWN: |
| 606 | case SYS_HALT: |
| 607 | case SYS_POWER_OFF: |
| 608 | if (host_control_on_shutdown) { |
| 609 | /* firmware is going to perform host control action */ |
| 610 | printk(KERN_WARNING "Please wait for shutdown " |
| 611 | "action to complete...\n"); |
| 612 | dcdbas_host_control(); |
| 613 | } |
| 614 | break; |
| 615 | } |
| 616 | |
| 617 | return NOTIFY_DONE; |
| 618 | } |
| 619 | |
| 620 | static struct notifier_block dcdbas_reboot_nb = { |
| 621 | .notifier_call = dcdbas_reboot_notify, |
| 622 | .next = NULL, |
| 623 | .priority = INT_MIN |
| 624 | }; |
| 625 | |
| 626 | static DCDBAS_BIN_ATTR_RW(smi_data); |
| 627 | |
| 628 | static struct bin_attribute *dcdbas_bin_attrs[] = { |
| 629 | &bin_attr_smi_data, |
| 630 | NULL |
| 631 | }; |
| 632 | |
| 633 | static DCDBAS_DEV_ATTR_RW(smi_data_buf_size); |
| 634 | static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr); |
| 635 | static DCDBAS_DEV_ATTR_WO(smi_request); |
| 636 | static DCDBAS_DEV_ATTR_RW(host_control_action); |
| 637 | static DCDBAS_DEV_ATTR_RW(host_control_smi_type); |
| 638 | static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown); |
| 639 | |
| 640 | static struct attribute *dcdbas_dev_attrs[] = { |
| 641 | &dev_attr_smi_data_buf_size.attr, |
| 642 | &dev_attr_smi_data_buf_phys_addr.attr, |
| 643 | &dev_attr_smi_request.attr, |
| 644 | &dev_attr_host_control_action.attr, |
| 645 | &dev_attr_host_control_smi_type.attr, |
| 646 | &dev_attr_host_control_on_shutdown.attr, |
| 647 | NULL |
| 648 | }; |
| 649 | |
| 650 | static const struct attribute_group dcdbas_attr_group = { |
| 651 | .attrs = dcdbas_dev_attrs, |
| 652 | .bin_attrs = dcdbas_bin_attrs, |
| 653 | }; |
| 654 | |
| 655 | static int dcdbas_probe(struct platform_device *dev) |
| 656 | { |
| 657 | int error; |
| 658 | |
| 659 | host_control_action = HC_ACTION_NONE; |
| 660 | host_control_smi_type = HC_SMITYPE_NONE; |
| 661 | |
| 662 | dcdbas_pdev = dev; |
| 663 | |
| 664 | /* Check if ACPI WSMT table specifies protected SMI buffer address */ |
| 665 | error = dcdbas_check_wsmt(); |
| 666 | if (error < 0) |
| 667 | return error; |
| 668 | |
| 669 | /* |
| 670 | * BIOS SMI calls require buffer addresses be in 32-bit address space. |
| 671 | * This is done by setting the DMA mask below. |
| 672 | */ |
| 673 | error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32)); |
| 674 | if (error) |
| 675 | return error; |
| 676 | |
| 677 | error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group); |
| 678 | if (error) |
| 679 | return error; |
| 680 | |
| 681 | register_reboot_notifier(&dcdbas_reboot_nb); |
| 682 | |
| 683 | dev_info(&dev->dev, "%s (version %s)\n", |
| 684 | DRIVER_DESCRIPTION, DRIVER_VERSION); |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | static int dcdbas_remove(struct platform_device *dev) |
| 690 | { |
| 691 | unregister_reboot_notifier(&dcdbas_reboot_nb); |
| 692 | sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group); |
| 693 | |
| 694 | return 0; |
| 695 | } |
| 696 | |
| 697 | static struct platform_driver dcdbas_driver = { |
| 698 | .driver = { |
| 699 | .name = DRIVER_NAME, |
| 700 | }, |
| 701 | .probe = dcdbas_probe, |
| 702 | .remove = dcdbas_remove, |
| 703 | }; |
| 704 | |
| 705 | static const struct platform_device_info dcdbas_dev_info __initconst = { |
| 706 | .name = DRIVER_NAME, |
| 707 | .id = -1, |
| 708 | .dma_mask = DMA_BIT_MASK(32), |
| 709 | }; |
| 710 | |
| 711 | static struct platform_device *dcdbas_pdev_reg; |
| 712 | |
| 713 | /** |
| 714 | * dcdbas_init: initialize driver |
| 715 | */ |
| 716 | static int __init dcdbas_init(void) |
| 717 | { |
| 718 | int error; |
| 719 | |
| 720 | error = platform_driver_register(&dcdbas_driver); |
| 721 | if (error) |
| 722 | return error; |
| 723 | |
| 724 | dcdbas_pdev_reg = platform_device_register_full(&dcdbas_dev_info); |
| 725 | if (IS_ERR(dcdbas_pdev_reg)) { |
| 726 | error = PTR_ERR(dcdbas_pdev_reg); |
| 727 | goto err_unregister_driver; |
| 728 | } |
| 729 | |
| 730 | return 0; |
| 731 | |
| 732 | err_unregister_driver: |
| 733 | platform_driver_unregister(&dcdbas_driver); |
| 734 | return error; |
| 735 | } |
| 736 | |
| 737 | /** |
| 738 | * dcdbas_exit: perform driver cleanup |
| 739 | */ |
| 740 | static void __exit dcdbas_exit(void) |
| 741 | { |
| 742 | /* |
| 743 | * make sure functions that use dcdbas_pdev are called |
| 744 | * before platform_device_unregister |
| 745 | */ |
| 746 | unregister_reboot_notifier(&dcdbas_reboot_nb); |
| 747 | |
| 748 | /* |
| 749 | * We have to free the buffer here instead of dcdbas_remove |
| 750 | * because only in module exit function we can be sure that |
| 751 | * all sysfs attributes belonging to this module have been |
| 752 | * released. |
| 753 | */ |
| 754 | if (dcdbas_pdev) |
| 755 | smi_data_buf_free(); |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 756 | if (bios_buffer) |
| 757 | memunmap(bios_buffer); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 758 | platform_device_unregister(dcdbas_pdev_reg); |
| 759 | platform_driver_unregister(&dcdbas_driver); |
| 760 | } |
| 761 | |
| 762 | subsys_initcall_sync(dcdbas_init); |
| 763 | module_exit(dcdbas_exit); |
| 764 | |
| 765 | MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")"); |
| 766 | MODULE_VERSION(DRIVER_VERSION); |
| 767 | MODULE_AUTHOR("Dell Inc."); |
| 768 | MODULE_LICENSE("GPL"); |
| 769 | /* Any System or BIOS claiming to be by Dell */ |
| 770 | MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*"); |