1 // SPDX-License-Identifier: GPL-2.0-only
3 * dcdbas.c: Dell Systems Management Base Driver
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
10 * See Documentation/userspace-api/dcdbas.rst for more information.
12 * Copyright (C) 1995-2006 Dell Inc.
15 #include <linux/platform_device.h>
16 #include <linux/acpi.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/dmi.h>
19 #include <linux/errno.h>
20 #include <linux/cpu.h>
21 #include <linux/gfp.h>
22 #include <linux/init.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/sysfs.h>
33 #include <linux/types.h>
34 #include <linux/mutex.h>
38 #define DRIVER_NAME "dcdbas"
39 #define DRIVER_VERSION "5.6.0-3.4"
40 #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
42 static struct platform_device *dcdbas_pdev;
44 static unsigned long max_smi_data_buf_size = MAX_SMI_DATA_BUF_SIZE;
45 static DEFINE_MUTEX(smi_data_lock);
46 static u8 *bios_buffer;
47 static struct smi_buffer smi_buf;
49 static unsigned int host_control_action;
50 static unsigned int host_control_smi_type;
51 static unsigned int host_control_on_shutdown;
53 static bool wsmt_enabled;
55 int dcdbas_smi_alloc(struct smi_buffer *smi_buffer, unsigned long size)
57 smi_buffer->virt = dma_alloc_coherent(&dcdbas_pdev->dev, size,
58 &smi_buffer->dma, GFP_KERNEL);
59 if (!smi_buffer->virt) {
60 dev_dbg(&dcdbas_pdev->dev,
61 "%s: failed to allocate memory size %lu\n",
65 smi_buffer->size = size;
67 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
68 __func__, (u32)smi_buffer->dma, smi_buffer->size);
72 EXPORT_SYMBOL_GPL(dcdbas_smi_alloc);
74 void dcdbas_smi_free(struct smi_buffer *smi_buffer)
76 if (!smi_buffer->virt)
79 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
80 __func__, (u32)smi_buffer->dma, smi_buffer->size);
81 dma_free_coherent(&dcdbas_pdev->dev, smi_buffer->size,
82 smi_buffer->virt, smi_buffer->dma);
83 smi_buffer->virt = NULL;
87 EXPORT_SYMBOL_GPL(dcdbas_smi_free);
90 * smi_data_buf_free: free SMI data buffer
92 static void smi_data_buf_free(void)
94 if (!smi_buf.virt || wsmt_enabled)
97 dcdbas_smi_free(&smi_buf);
101 * smi_data_buf_realloc: grow SMI data buffer if needed
103 static int smi_data_buf_realloc(unsigned long size)
105 struct smi_buffer tmp;
108 if (smi_buf.size >= size)
111 if (size > max_smi_data_buf_size)
114 /* new buffer is needed */
115 ret = dcdbas_smi_alloc(&tmp, size);
119 /* memory zeroed by dma_alloc_coherent */
121 memcpy(tmp.virt, smi_buf.virt, smi_buf.size);
123 /* free any existing buffer */
126 /* set up new buffer for use */
132 static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
133 struct device_attribute *attr,
136 return sysfs_emit(buf, "%x\n", (u32)smi_buf.dma);
139 static ssize_t smi_data_buf_size_show(struct device *dev,
140 struct device_attribute *attr,
143 return sysfs_emit(buf, "%lu\n", smi_buf.size);
146 static ssize_t smi_data_buf_size_store(struct device *dev,
147 struct device_attribute *attr,
148 const char *buf, size_t count)
150 unsigned long buf_size;
153 buf_size = simple_strtoul(buf, NULL, 10);
155 /* make sure SMI data buffer is at least buf_size */
156 mutex_lock(&smi_data_lock);
157 ret = smi_data_buf_realloc(buf_size);
158 mutex_unlock(&smi_data_lock);
165 static ssize_t smi_data_read(struct file *filp, struct kobject *kobj,
166 const struct bin_attribute *bin_attr,
167 char *buf, loff_t pos, size_t count)
171 mutex_lock(&smi_data_lock);
172 ret = memory_read_from_buffer(buf, count, &pos, smi_buf.virt,
174 mutex_unlock(&smi_data_lock);
178 static ssize_t smi_data_write(struct file *filp, struct kobject *kobj,
179 const struct bin_attribute *bin_attr,
180 char *buf, loff_t pos, size_t count)
184 if ((pos + count) > max_smi_data_buf_size)
187 mutex_lock(&smi_data_lock);
189 ret = smi_data_buf_realloc(pos + count);
193 memcpy(smi_buf.virt + pos, buf, count);
196 mutex_unlock(&smi_data_lock);
200 static ssize_t host_control_action_show(struct device *dev,
201 struct device_attribute *attr,
204 return sysfs_emit(buf, "%u\n", host_control_action);
207 static ssize_t host_control_action_store(struct device *dev,
208 struct device_attribute *attr,
209 const char *buf, size_t count)
213 /* make sure buffer is available for host control command */
214 mutex_lock(&smi_data_lock);
215 ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
216 mutex_unlock(&smi_data_lock);
220 host_control_action = simple_strtoul(buf, NULL, 10);
224 static ssize_t host_control_smi_type_show(struct device *dev,
225 struct device_attribute *attr,
228 return sysfs_emit(buf, "%u\n", host_control_smi_type);
231 static ssize_t host_control_smi_type_store(struct device *dev,
232 struct device_attribute *attr,
233 const char *buf, size_t count)
235 host_control_smi_type = simple_strtoul(buf, NULL, 10);
239 static ssize_t host_control_on_shutdown_show(struct device *dev,
240 struct device_attribute *attr,
243 return sysfs_emit(buf, "%u\n", host_control_on_shutdown);
246 static ssize_t host_control_on_shutdown_store(struct device *dev,
247 struct device_attribute *attr,
248 const char *buf, size_t count)
250 host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
254 static int raise_smi(void *par)
256 struct smi_cmd *smi_cmd = par;
258 if (smp_processor_id() != 0) {
259 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
265 /* inb to force posted write through and make SMI happen now */
269 : /* no output args */
270 : "a" (smi_cmd->command_code),
271 "d" (smi_cmd->command_address),
280 * dcdbas_smi_request: generate SMI request
282 * Called with smi_data_lock.
284 int dcdbas_smi_request(struct smi_cmd *smi_cmd)
288 if (smi_cmd->magic != SMI_CMD_MAGIC) {
289 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
294 /* SMI requires CPU 0 */
296 ret = smp_call_on_cpu(0, raise_smi, smi_cmd, true);
301 EXPORT_SYMBOL(dcdbas_smi_request);
306 * The valid values are:
307 * 0: zero SMI data buffer
308 * 1: generate calling interface SMI
309 * 2: generate raw SMI
311 * User application writes smi_cmd to smi_data before telling driver
314 static ssize_t smi_request_store(struct device *dev,
315 struct device_attribute *attr,
316 const char *buf, size_t count)
318 struct smi_cmd *smi_cmd;
319 unsigned long val = simple_strtoul(buf, NULL, 10);
322 mutex_lock(&smi_data_lock);
324 if (smi_buf.size < sizeof(struct smi_cmd)) {
328 smi_cmd = (struct smi_cmd *)smi_buf.virt;
333 ret = dcdbas_smi_request(smi_cmd);
339 * Calling Interface SMI
341 * Provide physical address of command buffer field within
342 * the struct smi_cmd to BIOS.
344 * Because the address that smi_cmd (smi_buf.virt) points to
345 * will be from memremap() of a non-memory address if WSMT
346 * is present, we can't use virt_to_phys() on smi_cmd, so
347 * we have to use the physical address that was saved when
348 * the virtual address for smi_cmd was received.
350 smi_cmd->ebx = (u32)smi_buf.dma +
351 offsetof(struct smi_cmd, command_buffer);
352 ret = dcdbas_smi_request(smi_cmd);
357 memset(smi_buf.virt, 0, smi_buf.size);
366 mutex_unlock(&smi_data_lock);
371 * host_control_smi: generate host control SMI
373 * Caller must set up the host control command in smi_buf.virt.
375 static int host_control_smi(void)
377 struct apm_cmd *apm_cmd;
384 apm_cmd = (struct apm_cmd *)smi_buf.virt;
385 apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
387 switch (host_control_smi_type) {
388 case HC_SMITYPE_TYPE1:
389 spin_lock_irqsave(&rtc_lock, flags);
390 /* write SMI data buffer physical address */
391 data = (u8 *)&smi_buf.dma;
392 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
393 index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
396 (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
398 (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
401 /* first set status to -1 as called by spec */
402 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
403 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
405 /* generate SMM call */
406 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
407 spin_unlock_irqrestore(&rtc_lock, flags);
409 /* wait a few to see if it executed */
410 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
411 while ((s8)inb(PCAT_APM_STATUS_PORT) == ESM_STATUS_CMD_UNSUCCESSFUL) {
413 if (num_ticks == EXPIRED_TIMER)
418 case HC_SMITYPE_TYPE2:
419 case HC_SMITYPE_TYPE3:
420 spin_lock_irqsave(&rtc_lock, flags);
421 /* write SMI data buffer physical address */
422 data = (u8 *)&smi_buf.dma;
423 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
424 index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
426 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
427 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
430 /* generate SMM call */
431 if (host_control_smi_type == HC_SMITYPE_TYPE3)
432 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
434 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
436 /* restore RTC index pointer since it was written to above */
437 CMOS_READ(RTC_REG_C);
438 spin_unlock_irqrestore(&rtc_lock, flags);
440 /* read control port back to serialize write */
441 cmd_status = inb(PE1400_APM_CONTROL_PORT);
443 /* wait a few to see if it executed */
444 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
445 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
447 if (num_ticks == EXPIRED_TIMER)
453 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
454 __func__, host_control_smi_type);
462 * dcdbas_host_control: initiate host control
464 * This function is called by the driver after the system has
465 * finished shutting down if the user application specified a
466 * host control action to perform on shutdown. It is safe to
467 * use smi_buf.virt at this point because the system has finished
468 * shutting down and no userspace apps are running.
470 static void dcdbas_host_control(void)
472 struct apm_cmd *apm_cmd;
475 if (host_control_action == HC_ACTION_NONE)
478 action = host_control_action;
479 host_control_action = HC_ACTION_NONE;
482 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
486 if (smi_buf.size < sizeof(struct apm_cmd)) {
487 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
492 apm_cmd = (struct apm_cmd *)smi_buf.virt;
494 /* power off takes precedence */
495 if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
496 apm_cmd->command = ESM_APM_POWER_CYCLE;
497 apm_cmd->reserved = 0;
498 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
500 } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
501 apm_cmd->command = ESM_APM_POWER_CYCLE;
502 apm_cmd->reserved = 0;
503 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
510 static u8 checksum(u8 *buffer, u8 length)
513 u8 *end = buffer + length;
520 static inline struct smm_eps_table *check_eps_table(u8 *addr)
522 struct smm_eps_table *eps = (struct smm_eps_table *)addr;
524 if (strncmp(eps->smm_comm_buff_anchor, SMM_EPS_SIG, 4) != 0)
527 if (checksum(addr, eps->length) != 0)
533 static int dcdbas_check_wsmt(void)
535 const struct dmi_device *dev = NULL;
536 struct acpi_table_wsmt *wsmt = NULL;
537 struct smm_eps_table *eps = NULL;
542 acpi_get_table(ACPI_SIG_WSMT, 0, (struct acpi_table_header **)&wsmt);
546 /* Check if WSMT ACPI table shows that protection is enabled */
547 if (!(wsmt->protection_flags & ACPI_WSMT_FIXED_COMM_BUFFERS) ||
548 !(wsmt->protection_flags & ACPI_WSMT_COMM_BUFFER_NESTED_PTR_PROTECTION))
552 * BIOS could provide the address/size of the protected buffer
553 * in an SMBIOS string or in an EPS structure in 0xFxxxx.
556 /* Check SMBIOS for buffer address */
557 while ((dev = dmi_find_device(DMI_DEV_TYPE_OEM_STRING, NULL, dev)))
558 if (sscanf(dev->name, "30[%16llx;%8llx]", &bios_buf_paddr,
562 /* Scan for EPS (entry point structure) */
563 for (addr = (u8 *)__va(0xf0000);
564 addr < (u8 *)__va(0x100000 - sizeof(struct smm_eps_table));
566 eps = check_eps_table(addr);
572 dev_dbg(&dcdbas_pdev->dev, "found WSMT, but no firmware buffer found\n");
575 bios_buf_paddr = eps->smm_comm_buff_addr;
576 remap_size = eps->num_of_4k_pages * PAGE_SIZE;
580 * Get physical address of buffer and map to virtual address.
581 * Table gives size in 4K pages, regardless of actual system page size.
583 if (upper_32_bits(bios_buf_paddr + 8)) {
584 dev_warn(&dcdbas_pdev->dev, "found WSMT, but buffer address is above 4GB\n");
588 * Limit remap size to MAX_SMI_DATA_BUF_SIZE + 8 (since the first 8
589 * bytes are used for a semaphore, not the data buffer itself).
591 if (remap_size > MAX_SMI_DATA_BUF_SIZE + 8)
592 remap_size = MAX_SMI_DATA_BUF_SIZE + 8;
594 bios_buffer = memremap(bios_buf_paddr, remap_size, MEMREMAP_WB);
596 dev_warn(&dcdbas_pdev->dev, "found WSMT, but failed to map buffer\n");
600 /* First 8 bytes is for a semaphore, not part of the smi_buf.virt */
601 smi_buf.dma = bios_buf_paddr + 8;
602 smi_buf.virt = bios_buffer + 8;
603 smi_buf.size = remap_size - 8;
604 max_smi_data_buf_size = smi_buf.size;
606 dev_info(&dcdbas_pdev->dev,
607 "WSMT found, using firmware-provided SMI buffer.\n");
612 * dcdbas_reboot_notify: handle reboot notification for host control
614 static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
621 if (host_control_on_shutdown) {
622 /* firmware is going to perform host control action */
623 printk(KERN_WARNING "Please wait for shutdown "
624 "action to complete...\n");
625 dcdbas_host_control();
633 static struct notifier_block dcdbas_reboot_nb = {
634 .notifier_call = dcdbas_reboot_notify,
639 static const BIN_ATTR_ADMIN_RW(smi_data, 0);
641 static const struct bin_attribute *const dcdbas_bin_attrs[] = {
646 static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
647 static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
648 static DCDBAS_DEV_ATTR_WO(smi_request);
649 static DCDBAS_DEV_ATTR_RW(host_control_action);
650 static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
651 static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
653 static struct attribute *dcdbas_dev_attrs[] = {
654 &dev_attr_smi_data_buf_size.attr,
655 &dev_attr_smi_data_buf_phys_addr.attr,
656 &dev_attr_smi_request.attr,
657 &dev_attr_host_control_action.attr,
658 &dev_attr_host_control_smi_type.attr,
659 &dev_attr_host_control_on_shutdown.attr,
663 static const struct attribute_group dcdbas_attr_group = {
664 .attrs = dcdbas_dev_attrs,
665 .bin_attrs_new = dcdbas_bin_attrs,
668 static int dcdbas_probe(struct platform_device *dev)
672 host_control_action = HC_ACTION_NONE;
673 host_control_smi_type = HC_SMITYPE_NONE;
677 /* Check if ACPI WSMT table specifies protected SMI buffer address */
678 error = dcdbas_check_wsmt();
683 * BIOS SMI calls require buffer addresses be in 32-bit address space.
684 * This is done by setting the DMA mask below.
686 error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
690 error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
694 register_reboot_notifier(&dcdbas_reboot_nb);
696 dev_info(&dev->dev, "%s (version %s)\n",
697 DRIVER_DESCRIPTION, DRIVER_VERSION);
702 static void dcdbas_remove(struct platform_device *dev)
704 unregister_reboot_notifier(&dcdbas_reboot_nb);
705 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
708 static struct platform_driver dcdbas_driver = {
712 .probe = dcdbas_probe,
713 .remove = dcdbas_remove,
716 static const struct platform_device_info dcdbas_dev_info __initconst = {
718 .id = PLATFORM_DEVID_NONE,
719 .dma_mask = DMA_BIT_MASK(32),
722 static struct platform_device *dcdbas_pdev_reg;
725 * dcdbas_init: initialize driver
727 static int __init dcdbas_init(void)
731 error = platform_driver_register(&dcdbas_driver);
735 dcdbas_pdev_reg = platform_device_register_full(&dcdbas_dev_info);
736 if (IS_ERR(dcdbas_pdev_reg)) {
737 error = PTR_ERR(dcdbas_pdev_reg);
738 goto err_unregister_driver;
743 err_unregister_driver:
744 platform_driver_unregister(&dcdbas_driver);
749 * dcdbas_exit: perform driver cleanup
751 static void __exit dcdbas_exit(void)
754 * make sure functions that use dcdbas_pdev are called
755 * before platform_device_unregister
757 unregister_reboot_notifier(&dcdbas_reboot_nb);
760 * We have to free the buffer here instead of dcdbas_remove
761 * because only in module exit function we can be sure that
762 * all sysfs attributes belonging to this module have been
768 memunmap(bios_buffer);
769 platform_device_unregister(dcdbas_pdev_reg);
770 platform_driver_unregister(&dcdbas_driver);
773 subsys_initcall_sync(dcdbas_init);
774 module_exit(dcdbas_exit);
776 MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
777 MODULE_VERSION(DRIVER_VERSION);
778 MODULE_AUTHOR("Dell Inc.");
779 MODULE_LICENSE("GPL");
780 /* Any System or BIOS claiming to be by Dell */
781 MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");