2 * dcdbas.c: Dell Systems Management Base Driver
4 * The Dell Systems Management Base Driver provides a sysfs interface for
5 * systems management software to perform System Management Interrupts (SMIs)
6 * and Host Control Actions (power cycle or power off after OS shutdown) on
9 * See Documentation/dcdbas.txt for more information.
11 * Copyright (C) 1995-2006 Dell Inc.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License v2.0 as published by
15 * the Free Software Foundation.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
23 #include <linux/platform_device.h>
24 #include <linux/acpi.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/errno.h>
27 #include <linux/cpu.h>
28 #include <linux/gfp.h>
29 #include <linux/init.h>
31 #include <linux/kernel.h>
32 #include <linux/mc146818rtc.h>
33 #include <linux/module.h>
34 #include <linux/reboot.h>
35 #include <linux/sched.h>
36 #include <linux/smp.h>
37 #include <linux/spinlock.h>
38 #include <linux/string.h>
39 #include <linux/types.h>
40 #include <linux/mutex.h>
44 #define DRIVER_NAME "dcdbas"
45 #define DRIVER_VERSION "5.6.0-3.3"
46 #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
48 static struct platform_device *dcdbas_pdev;
50 static u8 *smi_data_buf;
51 static dma_addr_t smi_data_buf_handle;
52 static unsigned long smi_data_buf_size;
53 static unsigned long max_smi_data_buf_size = MAX_SMI_DATA_BUF_SIZE;
54 static u32 smi_data_buf_phys_addr;
55 static DEFINE_MUTEX(smi_data_lock);
56 static u8 *eps_buffer;
58 static unsigned int host_control_action;
59 static unsigned int host_control_smi_type;
60 static unsigned int host_control_on_shutdown;
62 static bool wsmt_enabled;
65 * smi_data_buf_free: free SMI data buffer
67 static void smi_data_buf_free(void)
69 if (!smi_data_buf || wsmt_enabled)
72 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
73 __func__, smi_data_buf_phys_addr, smi_data_buf_size);
75 dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf,
78 smi_data_buf_handle = 0;
79 smi_data_buf_phys_addr = 0;
80 smi_data_buf_size = 0;
84 * smi_data_buf_realloc: grow SMI data buffer if needed
86 static int smi_data_buf_realloc(unsigned long size)
91 if (smi_data_buf_size >= size)
94 if (size > max_smi_data_buf_size)
97 /* new buffer is needed */
98 buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL);
100 dev_dbg(&dcdbas_pdev->dev,
101 "%s: failed to allocate memory size %lu\n",
105 /* memory zeroed by dma_alloc_coherent */
108 memcpy(buf, smi_data_buf, smi_data_buf_size);
110 /* free any existing buffer */
113 /* set up new buffer for use */
115 smi_data_buf_handle = handle;
116 smi_data_buf_phys_addr = (u32) virt_to_phys(buf);
117 smi_data_buf_size = size;
119 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
120 __func__, smi_data_buf_phys_addr, smi_data_buf_size);
125 static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
126 struct device_attribute *attr,
129 return sprintf(buf, "%x\n", smi_data_buf_phys_addr);
132 static ssize_t smi_data_buf_size_show(struct device *dev,
133 struct device_attribute *attr,
136 return sprintf(buf, "%lu\n", smi_data_buf_size);
139 static ssize_t smi_data_buf_size_store(struct device *dev,
140 struct device_attribute *attr,
141 const char *buf, size_t count)
143 unsigned long buf_size;
146 buf_size = simple_strtoul(buf, NULL, 10);
148 /* make sure SMI data buffer is at least buf_size */
149 mutex_lock(&smi_data_lock);
150 ret = smi_data_buf_realloc(buf_size);
151 mutex_unlock(&smi_data_lock);
158 static ssize_t smi_data_read(struct file *filp, struct kobject *kobj,
159 struct bin_attribute *bin_attr,
160 char *buf, loff_t pos, size_t count)
164 mutex_lock(&smi_data_lock);
165 ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf,
167 mutex_unlock(&smi_data_lock);
171 static ssize_t smi_data_write(struct file *filp, struct kobject *kobj,
172 struct bin_attribute *bin_attr,
173 char *buf, loff_t pos, size_t count)
177 if ((pos + count) > max_smi_data_buf_size)
180 mutex_lock(&smi_data_lock);
182 ret = smi_data_buf_realloc(pos + count);
186 memcpy(smi_data_buf + pos, buf, count);
189 mutex_unlock(&smi_data_lock);
193 static ssize_t host_control_action_show(struct device *dev,
194 struct device_attribute *attr,
197 return sprintf(buf, "%u\n", host_control_action);
200 static ssize_t host_control_action_store(struct device *dev,
201 struct device_attribute *attr,
202 const char *buf, size_t count)
206 /* make sure buffer is available for host control command */
207 mutex_lock(&smi_data_lock);
208 ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
209 mutex_unlock(&smi_data_lock);
213 host_control_action = simple_strtoul(buf, NULL, 10);
217 static ssize_t host_control_smi_type_show(struct device *dev,
218 struct device_attribute *attr,
221 return sprintf(buf, "%u\n", host_control_smi_type);
224 static ssize_t host_control_smi_type_store(struct device *dev,
225 struct device_attribute *attr,
226 const char *buf, size_t count)
228 host_control_smi_type = simple_strtoul(buf, NULL, 10);
232 static ssize_t host_control_on_shutdown_show(struct device *dev,
233 struct device_attribute *attr,
236 return sprintf(buf, "%u\n", host_control_on_shutdown);
239 static ssize_t host_control_on_shutdown_store(struct device *dev,
240 struct device_attribute *attr,
241 const char *buf, size_t count)
243 host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
247 static int raise_smi(void *par)
249 struct smi_cmd *smi_cmd = par;
251 if (smp_processor_id() != 0) {
252 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
258 /* inb to force posted write through and make SMI happen now */
262 : /* no output args */
263 : "a" (smi_cmd->command_code),
264 "d" (smi_cmd->command_address),
273 * dcdbas_smi_request: generate SMI request
275 * Called with smi_data_lock.
277 int dcdbas_smi_request(struct smi_cmd *smi_cmd)
281 if (smi_cmd->magic != SMI_CMD_MAGIC) {
282 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
287 /* SMI requires CPU 0 */
289 ret = smp_call_on_cpu(0, raise_smi, smi_cmd, true);
298 * The valid values are:
299 * 0: zero SMI data buffer
300 * 1: generate calling interface SMI
301 * 2: generate raw SMI
303 * User application writes smi_cmd to smi_data before telling driver
306 static ssize_t smi_request_store(struct device *dev,
307 struct device_attribute *attr,
308 const char *buf, size_t count)
310 struct smi_cmd *smi_cmd;
311 unsigned long val = simple_strtoul(buf, NULL, 10);
314 mutex_lock(&smi_data_lock);
316 if (smi_data_buf_size < sizeof(struct smi_cmd)) {
320 smi_cmd = (struct smi_cmd *)smi_data_buf;
325 ret = dcdbas_smi_request(smi_cmd);
331 * Calling Interface SMI
333 * Provide physical address of command buffer field within
334 * the struct smi_cmd to BIOS.
336 * Because the address that smi_cmd (smi_data_buf) points to
337 * will be from memremap() of a non-memory address if WSMT
338 * is present, we can't use virt_to_phys() on smi_cmd, so
339 * we have to use the physical address that was saved when
340 * the virtual address for smi_cmd was received.
342 smi_cmd->ebx = smi_data_buf_phys_addr +
343 offsetof(struct smi_cmd, command_buffer);
344 ret = dcdbas_smi_request(smi_cmd);
349 memset(smi_data_buf, 0, smi_data_buf_size);
358 mutex_unlock(&smi_data_lock);
361 EXPORT_SYMBOL(dcdbas_smi_request);
364 * host_control_smi: generate host control SMI
366 * Caller must set up the host control command in smi_data_buf.
368 static int host_control_smi(void)
370 struct apm_cmd *apm_cmd;
377 apm_cmd = (struct apm_cmd *)smi_data_buf;
378 apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
380 switch (host_control_smi_type) {
381 case HC_SMITYPE_TYPE1:
382 spin_lock_irqsave(&rtc_lock, flags);
383 /* write SMI data buffer physical address */
384 data = (u8 *)&smi_data_buf_phys_addr;
385 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
386 index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
389 (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
391 (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
394 /* first set status to -1 as called by spec */
395 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
396 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
398 /* generate SMM call */
399 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
400 spin_unlock_irqrestore(&rtc_lock, flags);
402 /* wait a few to see if it executed */
403 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
404 while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
405 == ESM_STATUS_CMD_UNSUCCESSFUL) {
407 if (num_ticks == EXPIRED_TIMER)
412 case HC_SMITYPE_TYPE2:
413 case HC_SMITYPE_TYPE3:
414 spin_lock_irqsave(&rtc_lock, flags);
415 /* write SMI data buffer physical address */
416 data = (u8 *)&smi_data_buf_phys_addr;
417 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
418 index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
420 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
421 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
424 /* generate SMM call */
425 if (host_control_smi_type == HC_SMITYPE_TYPE3)
426 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
428 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
430 /* restore RTC index pointer since it was written to above */
431 CMOS_READ(RTC_REG_C);
432 spin_unlock_irqrestore(&rtc_lock, flags);
434 /* read control port back to serialize write */
435 cmd_status = inb(PE1400_APM_CONTROL_PORT);
437 /* wait a few to see if it executed */
438 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
439 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
441 if (num_ticks == EXPIRED_TIMER)
447 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
448 __func__, host_control_smi_type);
456 * dcdbas_host_control: initiate host control
458 * This function is called by the driver after the system has
459 * finished shutting down if the user application specified a
460 * host control action to perform on shutdown. It is safe to
461 * use smi_data_buf at this point because the system has finished
462 * shutting down and no userspace apps are running.
464 static void dcdbas_host_control(void)
466 struct apm_cmd *apm_cmd;
469 if (host_control_action == HC_ACTION_NONE)
472 action = host_control_action;
473 host_control_action = HC_ACTION_NONE;
476 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
480 if (smi_data_buf_size < sizeof(struct apm_cmd)) {
481 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
486 apm_cmd = (struct apm_cmd *)smi_data_buf;
488 /* power off takes precedence */
489 if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
490 apm_cmd->command = ESM_APM_POWER_CYCLE;
491 apm_cmd->reserved = 0;
492 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
494 } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
495 apm_cmd->command = ESM_APM_POWER_CYCLE;
496 apm_cmd->reserved = 0;
497 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
504 static u8 checksum(u8 *buffer, u8 length)
507 u8 *end = buffer + length;
514 static inline struct smm_eps_table *check_eps_table(u8 *addr)
516 struct smm_eps_table *eps = (struct smm_eps_table *)addr;
518 if (strncmp(eps->smm_comm_buff_anchor, SMM_EPS_SIG, 4) != 0)
521 if (checksum(addr, eps->length) != 0)
527 static int dcdbas_check_wsmt(void)
529 struct acpi_table_wsmt *wsmt = NULL;
530 struct smm_eps_table *eps = NULL;
534 acpi_get_table(ACPI_SIG_WSMT, 0, (struct acpi_table_header **)&wsmt);
538 /* Check if WSMT ACPI table shows that protection is enabled */
539 if (!(wsmt->protection_flags & ACPI_WSMT_FIXED_COMM_BUFFERS) ||
540 !(wsmt->protection_flags & ACPI_WSMT_COMM_BUFFER_NESTED_PTR_PROTECTION))
543 /* Scan for EPS (entry point structure) */
544 for (addr = (u8 *)__va(0xf0000);
545 addr < (u8 *)__va(0x100000 - sizeof(struct smm_eps_table));
547 eps = check_eps_table(addr);
553 dev_dbg(&dcdbas_pdev->dev, "found WSMT, but no EPS found\n");
558 * Get physical address of buffer and map to virtual address.
559 * Table gives size in 4K pages, regardless of actual system page size.
561 if (upper_32_bits(eps->smm_comm_buff_addr + 8)) {
562 dev_warn(&dcdbas_pdev->dev, "found WSMT, but EPS buffer address is above 4GB\n");
566 * Limit remap size to MAX_SMI_DATA_BUF_SIZE + 8 (since the first 8
567 * bytes are used for a semaphore, not the data buffer itself).
569 remap_size = eps->num_of_4k_pages * PAGE_SIZE;
570 if (remap_size > MAX_SMI_DATA_BUF_SIZE + 8)
571 remap_size = MAX_SMI_DATA_BUF_SIZE + 8;
572 eps_buffer = memremap(eps->smm_comm_buff_addr, remap_size, MEMREMAP_WB);
574 dev_warn(&dcdbas_pdev->dev, "found WSMT, but failed to map EPS buffer\n");
578 /* First 8 bytes is for a semaphore, not part of the smi_data_buf */
579 smi_data_buf_phys_addr = eps->smm_comm_buff_addr + 8;
580 smi_data_buf = eps_buffer + 8;
581 smi_data_buf_size = remap_size - 8;
582 max_smi_data_buf_size = smi_data_buf_size;
584 dev_info(&dcdbas_pdev->dev,
585 "WSMT found, using firmware-provided SMI buffer.\n");
590 * dcdbas_reboot_notify: handle reboot notification for host control
592 static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
599 if (host_control_on_shutdown) {
600 /* firmware is going to perform host control action */
601 printk(KERN_WARNING "Please wait for shutdown "
602 "action to complete...\n");
603 dcdbas_host_control();
611 static struct notifier_block dcdbas_reboot_nb = {
612 .notifier_call = dcdbas_reboot_notify,
617 static DCDBAS_BIN_ATTR_RW(smi_data);
619 static struct bin_attribute *dcdbas_bin_attrs[] = {
624 static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
625 static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
626 static DCDBAS_DEV_ATTR_WO(smi_request);
627 static DCDBAS_DEV_ATTR_RW(host_control_action);
628 static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
629 static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
631 static struct attribute *dcdbas_dev_attrs[] = {
632 &dev_attr_smi_data_buf_size.attr,
633 &dev_attr_smi_data_buf_phys_addr.attr,
634 &dev_attr_smi_request.attr,
635 &dev_attr_host_control_action.attr,
636 &dev_attr_host_control_smi_type.attr,
637 &dev_attr_host_control_on_shutdown.attr,
641 static const struct attribute_group dcdbas_attr_group = {
642 .attrs = dcdbas_dev_attrs,
643 .bin_attrs = dcdbas_bin_attrs,
646 static int dcdbas_probe(struct platform_device *dev)
650 host_control_action = HC_ACTION_NONE;
651 host_control_smi_type = HC_SMITYPE_NONE;
655 /* Check if ACPI WSMT table specifies protected SMI buffer address */
656 error = dcdbas_check_wsmt();
661 * BIOS SMI calls require buffer addresses be in 32-bit address space.
662 * This is done by setting the DMA mask below.
664 error = dma_set_coherent_mask(&dcdbas_pdev->dev, DMA_BIT_MASK(32));
668 error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
672 register_reboot_notifier(&dcdbas_reboot_nb);
674 dev_info(&dev->dev, "%s (version %s)\n",
675 DRIVER_DESCRIPTION, DRIVER_VERSION);
680 static int dcdbas_remove(struct platform_device *dev)
682 unregister_reboot_notifier(&dcdbas_reboot_nb);
683 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
688 static struct platform_driver dcdbas_driver = {
692 .probe = dcdbas_probe,
693 .remove = dcdbas_remove,
696 static const struct platform_device_info dcdbas_dev_info __initconst = {
699 .dma_mask = DMA_BIT_MASK(32),
702 static struct platform_device *dcdbas_pdev_reg;
705 * dcdbas_init: initialize driver
707 static int __init dcdbas_init(void)
711 error = platform_driver_register(&dcdbas_driver);
715 dcdbas_pdev_reg = platform_device_register_full(&dcdbas_dev_info);
716 if (IS_ERR(dcdbas_pdev_reg)) {
717 error = PTR_ERR(dcdbas_pdev_reg);
718 goto err_unregister_driver;
723 err_unregister_driver:
724 platform_driver_unregister(&dcdbas_driver);
729 * dcdbas_exit: perform driver cleanup
731 static void __exit dcdbas_exit(void)
734 * make sure functions that use dcdbas_pdev are called
735 * before platform_device_unregister
737 unregister_reboot_notifier(&dcdbas_reboot_nb);
740 * We have to free the buffer here instead of dcdbas_remove
741 * because only in module exit function we can be sure that
742 * all sysfs attributes belonging to this module have been
748 memunmap(eps_buffer);
749 platform_device_unregister(dcdbas_pdev_reg);
750 platform_driver_unregister(&dcdbas_driver);
753 subsys_initcall_sync(dcdbas_init);
754 module_exit(dcdbas_exit);
756 MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
757 MODULE_VERSION(DRIVER_VERSION);
758 MODULE_AUTHOR("Dell Inc.");
759 MODULE_LICENSE("GPL");
760 /* Any System or BIOS claiming to be by Dell */
761 MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");