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/driver-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/types.h>
33 #include <linux/mutex.h>
37 #define DRIVER_NAME "dcdbas"
38 #define DRIVER_VERSION "5.6.0-3.4"
39 #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
41 static struct platform_device *dcdbas_pdev;
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);
49 static u8 *bios_buffer;
51 static unsigned int host_control_action;
52 static unsigned int host_control_smi_type;
53 static unsigned int host_control_on_shutdown;
55 static bool wsmt_enabled;
58 * smi_data_buf_free: free SMI data buffer
60 static void smi_data_buf_free(void)
62 if (!smi_data_buf || wsmt_enabled)
65 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
66 __func__, smi_data_buf_phys_addr, smi_data_buf_size);
68 dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf,
71 smi_data_buf_handle = 0;
72 smi_data_buf_phys_addr = 0;
73 smi_data_buf_size = 0;
77 * smi_data_buf_realloc: grow SMI data buffer if needed
79 static int smi_data_buf_realloc(unsigned long size)
84 if (smi_data_buf_size >= size)
87 if (size > max_smi_data_buf_size)
90 /* new buffer is needed */
91 buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL);
93 dev_dbg(&dcdbas_pdev->dev,
94 "%s: failed to allocate memory size %lu\n",
98 /* memory zeroed by dma_alloc_coherent */
101 memcpy(buf, smi_data_buf, smi_data_buf_size);
103 /* free any existing buffer */
106 /* set up new buffer for use */
108 smi_data_buf_handle = handle;
109 smi_data_buf_phys_addr = (u32) virt_to_phys(buf);
110 smi_data_buf_size = size;
112 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
113 __func__, smi_data_buf_phys_addr, smi_data_buf_size);
118 static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
119 struct device_attribute *attr,
122 return sprintf(buf, "%x\n", smi_data_buf_phys_addr);
125 static ssize_t smi_data_buf_size_show(struct device *dev,
126 struct device_attribute *attr,
129 return sprintf(buf, "%lu\n", smi_data_buf_size);
132 static ssize_t smi_data_buf_size_store(struct device *dev,
133 struct device_attribute *attr,
134 const char *buf, size_t count)
136 unsigned long buf_size;
139 buf_size = simple_strtoul(buf, NULL, 10);
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);
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)
157 mutex_lock(&smi_data_lock);
158 ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf,
160 mutex_unlock(&smi_data_lock);
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)
170 if ((pos + count) > max_smi_data_buf_size)
173 mutex_lock(&smi_data_lock);
175 ret = smi_data_buf_realloc(pos + count);
179 memcpy(smi_data_buf + pos, buf, count);
182 mutex_unlock(&smi_data_lock);
186 static ssize_t host_control_action_show(struct device *dev,
187 struct device_attribute *attr,
190 return sprintf(buf, "%u\n", host_control_action);
193 static ssize_t host_control_action_store(struct device *dev,
194 struct device_attribute *attr,
195 const char *buf, size_t count)
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);
206 host_control_action = simple_strtoul(buf, NULL, 10);
210 static ssize_t host_control_smi_type_show(struct device *dev,
211 struct device_attribute *attr,
214 return sprintf(buf, "%u\n", host_control_smi_type);
217 static ssize_t host_control_smi_type_store(struct device *dev,
218 struct device_attribute *attr,
219 const char *buf, size_t count)
221 host_control_smi_type = simple_strtoul(buf, NULL, 10);
225 static ssize_t host_control_on_shutdown_show(struct device *dev,
226 struct device_attribute *attr,
229 return sprintf(buf, "%u\n", host_control_on_shutdown);
232 static ssize_t host_control_on_shutdown_store(struct device *dev,
233 struct device_attribute *attr,
234 const char *buf, size_t count)
236 host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
240 static int raise_smi(void *par)
242 struct smi_cmd *smi_cmd = par;
244 if (smp_processor_id() != 0) {
245 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
251 /* inb to force posted write through and make SMI happen now */
255 : /* no output args */
256 : "a" (smi_cmd->command_code),
257 "d" (smi_cmd->command_address),
266 * dcdbas_smi_request: generate SMI request
268 * Called with smi_data_lock.
270 int dcdbas_smi_request(struct smi_cmd *smi_cmd)
274 if (smi_cmd->magic != SMI_CMD_MAGIC) {
275 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
280 /* SMI requires CPU 0 */
282 ret = smp_call_on_cpu(0, raise_smi, smi_cmd, true);
291 * The valid values are:
292 * 0: zero SMI data buffer
293 * 1: generate calling interface SMI
294 * 2: generate raw SMI
296 * User application writes smi_cmd to smi_data before telling driver
299 static ssize_t smi_request_store(struct device *dev,
300 struct device_attribute *attr,
301 const char *buf, size_t count)
303 struct smi_cmd *smi_cmd;
304 unsigned long val = simple_strtoul(buf, NULL, 10);
307 mutex_lock(&smi_data_lock);
309 if (smi_data_buf_size < sizeof(struct smi_cmd)) {
313 smi_cmd = (struct smi_cmd *)smi_data_buf;
318 ret = dcdbas_smi_request(smi_cmd);
324 * Calling Interface SMI
326 * Provide physical address of command buffer field within
327 * the struct smi_cmd to BIOS.
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.
335 smi_cmd->ebx = smi_data_buf_phys_addr +
336 offsetof(struct smi_cmd, command_buffer);
337 ret = dcdbas_smi_request(smi_cmd);
342 memset(smi_data_buf, 0, smi_data_buf_size);
351 mutex_unlock(&smi_data_lock);
354 EXPORT_SYMBOL(dcdbas_smi_request);
357 * host_control_smi: generate host control SMI
359 * Caller must set up the host control command in smi_data_buf.
361 static int host_control_smi(void)
363 struct apm_cmd *apm_cmd;
370 apm_cmd = (struct apm_cmd *)smi_data_buf;
371 apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
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);
382 (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
384 (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
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);
391 /* generate SMM call */
392 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
393 spin_unlock_irqrestore(&rtc_lock, flags);
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) {
400 if (num_ticks == EXPIRED_TIMER)
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);
413 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
414 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
417 /* generate SMM call */
418 if (host_control_smi_type == HC_SMITYPE_TYPE3)
419 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
421 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
423 /* restore RTC index pointer since it was written to above */
424 CMOS_READ(RTC_REG_C);
425 spin_unlock_irqrestore(&rtc_lock, flags);
427 /* read control port back to serialize write */
428 cmd_status = inb(PE1400_APM_CONTROL_PORT);
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) {
434 if (num_ticks == EXPIRED_TIMER)
440 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
441 __func__, host_control_smi_type);
449 * dcdbas_host_control: initiate host control
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.
457 static void dcdbas_host_control(void)
459 struct apm_cmd *apm_cmd;
462 if (host_control_action == HC_ACTION_NONE)
465 action = host_control_action;
466 host_control_action = HC_ACTION_NONE;
469 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
473 if (smi_data_buf_size < sizeof(struct apm_cmd)) {
474 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
479 apm_cmd = (struct apm_cmd *)smi_data_buf;
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;
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;
497 static u8 checksum(u8 *buffer, u8 length)
500 u8 *end = buffer + length;
507 static inline struct smm_eps_table *check_eps_table(u8 *addr)
509 struct smm_eps_table *eps = (struct smm_eps_table *)addr;
511 if (strncmp(eps->smm_comm_buff_anchor, SMM_EPS_SIG, 4) != 0)
514 if (checksum(addr, eps->length) != 0)
520 static int dcdbas_check_wsmt(void)
522 const struct dmi_device *dev = NULL;
523 struct acpi_table_wsmt *wsmt = NULL;
524 struct smm_eps_table *eps = NULL;
529 acpi_get_table(ACPI_SIG_WSMT, 0, (struct acpi_table_header **)&wsmt);
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))
539 * BIOS could provide the address/size of the protected buffer
540 * in an SMBIOS string or in an EPS structure in 0xFxxxx.
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,
549 /* Scan for EPS (entry point structure) */
550 for (addr = (u8 *)__va(0xf0000);
551 addr < (u8 *)__va(0x100000 - sizeof(struct smm_eps_table));
553 eps = check_eps_table(addr);
559 dev_dbg(&dcdbas_pdev->dev, "found WSMT, but no firmware buffer found\n");
562 bios_buf_paddr = eps->smm_comm_buff_addr;
563 remap_size = eps->num_of_4k_pages * PAGE_SIZE;
567 * Get physical address of buffer and map to virtual address.
568 * Table gives size in 4K pages, regardless of actual system page size.
570 if (upper_32_bits(bios_buf_paddr + 8)) {
571 dev_warn(&dcdbas_pdev->dev, "found WSMT, but buffer address is above 4GB\n");
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).
578 if (remap_size > MAX_SMI_DATA_BUF_SIZE + 8)
579 remap_size = MAX_SMI_DATA_BUF_SIZE + 8;
581 bios_buffer = memremap(bios_buf_paddr, remap_size, MEMREMAP_WB);
583 dev_warn(&dcdbas_pdev->dev, "found WSMT, but failed to map buffer\n");
587 /* First 8 bytes is for a semaphore, not part of the smi_data_buf */
588 smi_data_buf_phys_addr = bios_buf_paddr + 8;
589 smi_data_buf = bios_buffer + 8;
590 smi_data_buf_size = remap_size - 8;
591 max_smi_data_buf_size = smi_data_buf_size;
593 dev_info(&dcdbas_pdev->dev,
594 "WSMT found, using firmware-provided SMI buffer.\n");
599 * dcdbas_reboot_notify: handle reboot notification for host control
601 static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
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();
620 static struct notifier_block dcdbas_reboot_nb = {
621 .notifier_call = dcdbas_reboot_notify,
626 static DCDBAS_BIN_ATTR_RW(smi_data);
628 static struct bin_attribute *dcdbas_bin_attrs[] = {
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);
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,
650 static const struct attribute_group dcdbas_attr_group = {
651 .attrs = dcdbas_dev_attrs,
652 .bin_attrs = dcdbas_bin_attrs,
655 static int dcdbas_probe(struct platform_device *dev)
659 host_control_action = HC_ACTION_NONE;
660 host_control_smi_type = HC_SMITYPE_NONE;
664 /* Check if ACPI WSMT table specifies protected SMI buffer address */
665 error = dcdbas_check_wsmt();
670 * BIOS SMI calls require buffer addresses be in 32-bit address space.
671 * This is done by setting the DMA mask below.
673 error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
677 error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
681 register_reboot_notifier(&dcdbas_reboot_nb);
683 dev_info(&dev->dev, "%s (version %s)\n",
684 DRIVER_DESCRIPTION, DRIVER_VERSION);
689 static int dcdbas_remove(struct platform_device *dev)
691 unregister_reboot_notifier(&dcdbas_reboot_nb);
692 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
697 static struct platform_driver dcdbas_driver = {
701 .probe = dcdbas_probe,
702 .remove = dcdbas_remove,
705 static const struct platform_device_info dcdbas_dev_info __initconst = {
708 .dma_mask = DMA_BIT_MASK(32),
711 static struct platform_device *dcdbas_pdev_reg;
714 * dcdbas_init: initialize driver
716 static int __init dcdbas_init(void)
720 error = platform_driver_register(&dcdbas_driver);
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;
732 err_unregister_driver:
733 platform_driver_unregister(&dcdbas_driver);
738 * dcdbas_exit: perform driver cleanup
740 static void __exit dcdbas_exit(void)
743 * make sure functions that use dcdbas_pdev are called
744 * before platform_device_unregister
746 unregister_reboot_notifier(&dcdbas_reboot_nb);
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
757 memunmap(bios_buffer);
758 platform_device_unregister(dcdbas_pdev_reg);
759 platform_driver_unregister(&dcdbas_driver);
762 subsys_initcall_sync(dcdbas_init);
763 module_exit(dcdbas_exit);
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]*:*");