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/dma-mapping.h>
25 #include <linux/errno.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/mc146818rtc.h>
29 #include <linux/module.h>
30 #include <linux/reboot.h>
31 #include <linux/sched.h>
32 #include <linux/smp.h>
33 #include <linux/spinlock.h>
34 #include <linux/string.h>
35 #include <linux/types.h>
36 #include <linux/mutex.h>
41 #define DRIVER_NAME "dcdbas"
42 #define DRIVER_VERSION "5.6.0-3.2"
43 #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
45 static struct platform_device *dcdbas_pdev;
47 static u8 *smi_data_buf;
48 static dma_addr_t smi_data_buf_handle;
49 static unsigned long smi_data_buf_size;
50 static u32 smi_data_buf_phys_addr;
51 static DEFINE_MUTEX(smi_data_lock);
53 static unsigned int host_control_action;
54 static unsigned int host_control_smi_type;
55 static unsigned int host_control_on_shutdown;
58 * smi_data_buf_free: free SMI data buffer
60 static void smi_data_buf_free(void)
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 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 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);
241 * smi_request: generate SMI request
243 * Called with smi_data_lock.
245 static int smi_request(struct smi_cmd *smi_cmd)
248 cpumask_of_cpu_ptr(new_mask, 0);
251 if (smi_cmd->magic != SMI_CMD_MAGIC) {
252 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
257 /* SMI requires CPU 0 */
258 old_mask = current->cpus_allowed;
259 set_cpus_allowed_ptr(current, new_mask);
260 if (smp_processor_id() != 0) {
261 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
270 : /* no output args */
271 : "a" (smi_cmd->command_code),
272 "d" (smi_cmd->command_address),
279 set_cpus_allowed_ptr(current, &old_mask);
286 * The valid values are:
287 * 0: zero SMI data buffer
288 * 1: generate calling interface SMI
289 * 2: generate raw SMI
291 * User application writes smi_cmd to smi_data before telling driver
294 static ssize_t smi_request_store(struct device *dev,
295 struct device_attribute *attr,
296 const char *buf, size_t count)
298 struct smi_cmd *smi_cmd;
299 unsigned long val = simple_strtoul(buf, NULL, 10);
302 mutex_lock(&smi_data_lock);
304 if (smi_data_buf_size < sizeof(struct smi_cmd)) {
308 smi_cmd = (struct smi_cmd *)smi_data_buf;
313 ret = smi_request(smi_cmd);
318 /* Calling Interface SMI */
319 smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer);
320 ret = smi_request(smi_cmd);
325 memset(smi_data_buf, 0, smi_data_buf_size);
334 mutex_unlock(&smi_data_lock);
339 * host_control_smi: generate host control SMI
341 * Caller must set up the host control command in smi_data_buf.
343 static int host_control_smi(void)
345 struct apm_cmd *apm_cmd;
352 apm_cmd = (struct apm_cmd *)smi_data_buf;
353 apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
355 switch (host_control_smi_type) {
356 case HC_SMITYPE_TYPE1:
357 spin_lock_irqsave(&rtc_lock, flags);
358 /* write SMI data buffer physical address */
359 data = (u8 *)&smi_data_buf_phys_addr;
360 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
361 index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
364 (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
366 (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
369 /* first set status to -1 as called by spec */
370 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
371 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
373 /* generate SMM call */
374 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
375 spin_unlock_irqrestore(&rtc_lock, flags);
377 /* wait a few to see if it executed */
378 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
379 while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
380 == ESM_STATUS_CMD_UNSUCCESSFUL) {
382 if (num_ticks == EXPIRED_TIMER)
387 case HC_SMITYPE_TYPE2:
388 case HC_SMITYPE_TYPE3:
389 spin_lock_irqsave(&rtc_lock, flags);
390 /* write SMI data buffer physical address */
391 data = (u8 *)&smi_data_buf_phys_addr;
392 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
393 index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
395 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
396 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
399 /* generate SMM call */
400 if (host_control_smi_type == HC_SMITYPE_TYPE3)
401 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
403 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
405 /* restore RTC index pointer since it was written to above */
406 CMOS_READ(RTC_REG_C);
407 spin_unlock_irqrestore(&rtc_lock, flags);
409 /* read control port back to serialize write */
410 cmd_status = inb(PE1400_APM_CONTROL_PORT);
412 /* wait a few to see if it executed */
413 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
414 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
416 if (num_ticks == EXPIRED_TIMER)
422 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
423 __func__, host_control_smi_type);
431 * dcdbas_host_control: initiate host control
433 * This function is called by the driver after the system has
434 * finished shutting down if the user application specified a
435 * host control action to perform on shutdown. It is safe to
436 * use smi_data_buf at this point because the system has finished
437 * shutting down and no userspace apps are running.
439 static void dcdbas_host_control(void)
441 struct apm_cmd *apm_cmd;
444 if (host_control_action == HC_ACTION_NONE)
447 action = host_control_action;
448 host_control_action = HC_ACTION_NONE;
451 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
455 if (smi_data_buf_size < sizeof(struct apm_cmd)) {
456 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
461 apm_cmd = (struct apm_cmd *)smi_data_buf;
463 /* power off takes precedence */
464 if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
465 apm_cmd->command = ESM_APM_POWER_CYCLE;
466 apm_cmd->reserved = 0;
467 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
469 } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
470 apm_cmd->command = ESM_APM_POWER_CYCLE;
471 apm_cmd->reserved = 0;
472 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
478 * dcdbas_reboot_notify: handle reboot notification for host control
480 static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
487 if (host_control_on_shutdown) {
488 /* firmware is going to perform host control action */
489 printk(KERN_WARNING "Please wait for shutdown "
490 "action to complete...\n");
491 dcdbas_host_control();
499 static struct notifier_block dcdbas_reboot_nb = {
500 .notifier_call = dcdbas_reboot_notify,
505 static DCDBAS_BIN_ATTR_RW(smi_data);
507 static struct bin_attribute *dcdbas_bin_attrs[] = {
512 static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
513 static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
514 static DCDBAS_DEV_ATTR_WO(smi_request);
515 static DCDBAS_DEV_ATTR_RW(host_control_action);
516 static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
517 static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
519 static struct attribute *dcdbas_dev_attrs[] = {
520 &dev_attr_smi_data_buf_size.attr,
521 &dev_attr_smi_data_buf_phys_addr.attr,
522 &dev_attr_smi_request.attr,
523 &dev_attr_host_control_action.attr,
524 &dev_attr_host_control_smi_type.attr,
525 &dev_attr_host_control_on_shutdown.attr,
529 static struct attribute_group dcdbas_attr_group = {
530 .attrs = dcdbas_dev_attrs,
533 static int __devinit dcdbas_probe(struct platform_device *dev)
537 host_control_action = HC_ACTION_NONE;
538 host_control_smi_type = HC_SMITYPE_NONE;
541 * BIOS SMI calls require buffer addresses be in 32-bit address space.
542 * This is done by setting the DMA mask below.
544 dcdbas_pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
545 dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask;
547 error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
551 for (i = 0; dcdbas_bin_attrs[i]; i++) {
552 error = sysfs_create_bin_file(&dev->dev.kobj,
553 dcdbas_bin_attrs[i]);
556 sysfs_remove_bin_file(&dev->dev.kobj,
557 dcdbas_bin_attrs[i]);
558 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
563 register_reboot_notifier(&dcdbas_reboot_nb);
565 dev_info(&dev->dev, "%s (version %s)\n",
566 DRIVER_DESCRIPTION, DRIVER_VERSION);
571 static int __devexit dcdbas_remove(struct platform_device *dev)
575 unregister_reboot_notifier(&dcdbas_reboot_nb);
576 for (i = 0; dcdbas_bin_attrs[i]; i++)
577 sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]);
578 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
583 static struct platform_driver dcdbas_driver = {
586 .owner = THIS_MODULE,
588 .probe = dcdbas_probe,
589 .remove = __devexit_p(dcdbas_remove),
593 * dcdbas_init: initialize driver
595 static int __init dcdbas_init(void)
599 error = platform_driver_register(&dcdbas_driver);
603 dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1);
606 goto err_unregister_driver;
609 error = platform_device_add(dcdbas_pdev);
611 goto err_free_device;
616 platform_device_put(dcdbas_pdev);
617 err_unregister_driver:
618 platform_driver_unregister(&dcdbas_driver);
623 * dcdbas_exit: perform driver cleanup
625 static void __exit dcdbas_exit(void)
628 * make sure functions that use dcdbas_pdev are called
629 * before platform_device_unregister
631 unregister_reboot_notifier(&dcdbas_reboot_nb);
633 platform_device_unregister(dcdbas_pdev);
634 platform_driver_unregister(&dcdbas_driver);
637 * We have to free the buffer here instead of dcdbas_remove
638 * because only in module exit function we can be sure that
639 * all sysfs attributes belonging to this module have been
645 module_init(dcdbas_init);
646 module_exit(dcdbas_exit);
648 MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
649 MODULE_VERSION(DRIVER_VERSION);
650 MODULE_AUTHOR("Dell Inc.");
651 MODULE_LICENSE("GPL");
652 /* Any System or BIOS claiming to be by Dell */
653 MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");