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>
38 #include <asm/semaphore.h>
42 #define DRIVER_NAME "dcdbas"
43 #define DRIVER_VERSION "5.6.0-3.2"
44 #define DRIVER_DESCRIPTION "Dell Systems Management Base Driver"
46 static struct platform_device *dcdbas_pdev;
48 static u8 *smi_data_buf;
49 static dma_addr_t smi_data_buf_handle;
50 static unsigned long smi_data_buf_size;
51 static u32 smi_data_buf_phys_addr;
52 static DEFINE_MUTEX(smi_data_lock);
54 static unsigned int host_control_action;
55 static unsigned int host_control_smi_type;
56 static unsigned int host_control_on_shutdown;
59 * smi_data_buf_free: free SMI data buffer
61 static void smi_data_buf_free(void)
66 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
67 __FUNCTION__, smi_data_buf_phys_addr, smi_data_buf_size);
69 dma_free_coherent(&dcdbas_pdev->dev, smi_data_buf_size, smi_data_buf,
72 smi_data_buf_handle = 0;
73 smi_data_buf_phys_addr = 0;
74 smi_data_buf_size = 0;
78 * smi_data_buf_realloc: grow SMI data buffer if needed
80 static int smi_data_buf_realloc(unsigned long size)
85 if (smi_data_buf_size >= size)
88 if (size > MAX_SMI_DATA_BUF_SIZE)
91 /* new buffer is needed */
92 buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL);
94 dev_dbg(&dcdbas_pdev->dev,
95 "%s: failed to allocate memory size %lu\n",
99 /* memory zeroed by dma_alloc_coherent */
102 memcpy(buf, smi_data_buf, smi_data_buf_size);
104 /* free any existing buffer */
107 /* set up new buffer for use */
109 smi_data_buf_handle = handle;
110 smi_data_buf_phys_addr = (u32) virt_to_phys(buf);
111 smi_data_buf_size = size;
113 dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
114 __FUNCTION__, smi_data_buf_phys_addr, smi_data_buf_size);
119 static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
120 struct device_attribute *attr,
123 return sprintf(buf, "%x\n", smi_data_buf_phys_addr);
126 static ssize_t smi_data_buf_size_show(struct device *dev,
127 struct device_attribute *attr,
130 return sprintf(buf, "%lu\n", smi_data_buf_size);
133 static ssize_t smi_data_buf_size_store(struct device *dev,
134 struct device_attribute *attr,
135 const char *buf, size_t count)
137 unsigned long buf_size;
140 buf_size = simple_strtoul(buf, NULL, 10);
142 /* make sure SMI data buffer is at least buf_size */
143 mutex_lock(&smi_data_lock);
144 ret = smi_data_buf_realloc(buf_size);
145 mutex_unlock(&smi_data_lock);
152 static ssize_t smi_data_read(struct kobject *kobj, char *buf, loff_t pos,
158 mutex_lock(&smi_data_lock);
160 if (pos >= smi_data_buf_size) {
165 max_read = smi_data_buf_size - pos;
166 ret = min(max_read, count);
167 memcpy(buf, smi_data_buf + pos, ret);
169 mutex_unlock(&smi_data_lock);
173 static ssize_t smi_data_write(struct kobject *kobj, char *buf, loff_t pos,
178 if ((pos + count) > MAX_SMI_DATA_BUF_SIZE)
181 mutex_lock(&smi_data_lock);
183 ret = smi_data_buf_realloc(pos + count);
187 memcpy(smi_data_buf + pos, buf, count);
190 mutex_unlock(&smi_data_lock);
194 static ssize_t host_control_action_show(struct device *dev,
195 struct device_attribute *attr,
198 return sprintf(buf, "%u\n", host_control_action);
201 static ssize_t host_control_action_store(struct device *dev,
202 struct device_attribute *attr,
203 const char *buf, size_t count)
207 /* make sure buffer is available for host control command */
208 mutex_lock(&smi_data_lock);
209 ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
210 mutex_unlock(&smi_data_lock);
214 host_control_action = simple_strtoul(buf, NULL, 10);
218 static ssize_t host_control_smi_type_show(struct device *dev,
219 struct device_attribute *attr,
222 return sprintf(buf, "%u\n", host_control_smi_type);
225 static ssize_t host_control_smi_type_store(struct device *dev,
226 struct device_attribute *attr,
227 const char *buf, size_t count)
229 host_control_smi_type = simple_strtoul(buf, NULL, 10);
233 static ssize_t host_control_on_shutdown_show(struct device *dev,
234 struct device_attribute *attr,
237 return sprintf(buf, "%u\n", host_control_on_shutdown);
240 static ssize_t host_control_on_shutdown_store(struct device *dev,
241 struct device_attribute *attr,
242 const char *buf, size_t count)
244 host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
249 * smi_request: generate SMI request
251 * Called with smi_data_lock.
253 static int smi_request(struct smi_cmd *smi_cmd)
258 if (smi_cmd->magic != SMI_CMD_MAGIC) {
259 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
264 /* SMI requires CPU 0 */
265 old_mask = current->cpus_allowed;
266 set_cpus_allowed(current, cpumask_of_cpu(0));
267 if (smp_processor_id() != 0) {
268 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
277 : /* no output args */
278 : "a" (smi_cmd->command_code),
279 "d" (smi_cmd->command_address),
286 set_cpus_allowed(current, old_mask);
293 * The valid values are:
294 * 0: zero SMI data buffer
295 * 1: generate calling interface SMI
296 * 2: generate raw SMI
298 * User application writes smi_cmd to smi_data before telling driver
301 static ssize_t smi_request_store(struct device *dev,
302 struct device_attribute *attr,
303 const char *buf, size_t count)
305 struct smi_cmd *smi_cmd;
306 unsigned long val = simple_strtoul(buf, NULL, 10);
309 mutex_lock(&smi_data_lock);
311 if (smi_data_buf_size < sizeof(struct smi_cmd)) {
315 smi_cmd = (struct smi_cmd *)smi_data_buf;
320 ret = smi_request(smi_cmd);
325 /* Calling Interface SMI */
326 smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer);
327 ret = smi_request(smi_cmd);
332 memset(smi_data_buf, 0, smi_data_buf_size);
341 mutex_unlock(&smi_data_lock);
346 * host_control_smi: generate host control SMI
348 * Caller must set up the host control command in smi_data_buf.
350 static int host_control_smi(void)
352 struct apm_cmd *apm_cmd;
359 apm_cmd = (struct apm_cmd *)smi_data_buf;
360 apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
362 switch (host_control_smi_type) {
363 case HC_SMITYPE_TYPE1:
364 spin_lock_irqsave(&rtc_lock, flags);
365 /* write SMI data buffer physical address */
366 data = (u8 *)&smi_data_buf_phys_addr;
367 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
368 index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
371 (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
373 (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
376 /* first set status to -1 as called by spec */
377 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
378 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
380 /* generate SMM call */
381 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
382 spin_unlock_irqrestore(&rtc_lock, flags);
384 /* wait a few to see if it executed */
385 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
386 while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
387 == ESM_STATUS_CMD_UNSUCCESSFUL) {
389 if (num_ticks == EXPIRED_TIMER)
394 case HC_SMITYPE_TYPE2:
395 case HC_SMITYPE_TYPE3:
396 spin_lock_irqsave(&rtc_lock, flags);
397 /* write SMI data buffer physical address */
398 data = (u8 *)&smi_data_buf_phys_addr;
399 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
400 index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
402 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
403 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
406 /* generate SMM call */
407 if (host_control_smi_type == HC_SMITYPE_TYPE3)
408 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
410 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
412 /* restore RTC index pointer since it was written to above */
413 CMOS_READ(RTC_REG_C);
414 spin_unlock_irqrestore(&rtc_lock, flags);
416 /* read control port back to serialize write */
417 cmd_status = inb(PE1400_APM_CONTROL_PORT);
419 /* wait a few to see if it executed */
420 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
421 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
423 if (num_ticks == EXPIRED_TIMER)
429 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
430 __FUNCTION__, host_control_smi_type);
438 * dcdbas_host_control: initiate host control
440 * This function is called by the driver after the system has
441 * finished shutting down if the user application specified a
442 * host control action to perform on shutdown. It is safe to
443 * use smi_data_buf at this point because the system has finished
444 * shutting down and no userspace apps are running.
446 static void dcdbas_host_control(void)
448 struct apm_cmd *apm_cmd;
451 if (host_control_action == HC_ACTION_NONE)
454 action = host_control_action;
455 host_control_action = HC_ACTION_NONE;
458 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __FUNCTION__);
462 if (smi_data_buf_size < sizeof(struct apm_cmd)) {
463 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
468 apm_cmd = (struct apm_cmd *)smi_data_buf;
470 /* power off takes precedence */
471 if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
472 apm_cmd->command = ESM_APM_POWER_CYCLE;
473 apm_cmd->reserved = 0;
474 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
476 } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
477 apm_cmd->command = ESM_APM_POWER_CYCLE;
478 apm_cmd->reserved = 0;
479 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
485 * dcdbas_reboot_notify: handle reboot notification for host control
487 static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
494 if (host_control_on_shutdown) {
495 /* firmware is going to perform host control action */
496 printk(KERN_WARNING "Please wait for shutdown "
497 "action to complete...\n");
498 dcdbas_host_control();
506 static struct notifier_block dcdbas_reboot_nb = {
507 .notifier_call = dcdbas_reboot_notify,
512 static DCDBAS_BIN_ATTR_RW(smi_data);
514 static struct bin_attribute *dcdbas_bin_attrs[] = {
519 static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
520 static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
521 static DCDBAS_DEV_ATTR_WO(smi_request);
522 static DCDBAS_DEV_ATTR_RW(host_control_action);
523 static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
524 static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
526 static struct attribute *dcdbas_dev_attrs[] = {
527 &dev_attr_smi_data_buf_size.attr,
528 &dev_attr_smi_data_buf_phys_addr.attr,
529 &dev_attr_smi_request.attr,
530 &dev_attr_host_control_action.attr,
531 &dev_attr_host_control_smi_type.attr,
532 &dev_attr_host_control_on_shutdown.attr,
536 static struct attribute_group dcdbas_attr_group = {
537 .attrs = dcdbas_dev_attrs,
540 static int __devinit dcdbas_probe(struct platform_device *dev)
544 host_control_action = HC_ACTION_NONE;
545 host_control_smi_type = HC_SMITYPE_NONE;
548 * BIOS SMI calls require buffer addresses be in 32-bit address space.
549 * This is done by setting the DMA mask below.
551 dcdbas_pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
552 dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask;
554 error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
558 for (i = 0; dcdbas_bin_attrs[i]; i++) {
559 error = sysfs_create_bin_file(&dev->dev.kobj,
560 dcdbas_bin_attrs[i]);
563 sysfs_remove_bin_file(&dev->dev.kobj,
564 dcdbas_bin_attrs[i]);
565 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
570 register_reboot_notifier(&dcdbas_reboot_nb);
572 dev_info(&dev->dev, "%s (version %s)\n",
573 DRIVER_DESCRIPTION, DRIVER_VERSION);
578 static int __devexit dcdbas_remove(struct platform_device *dev)
582 unregister_reboot_notifier(&dcdbas_reboot_nb);
583 for (i = 0; dcdbas_bin_attrs[i]; i++)
584 sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]);
585 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
590 static struct platform_driver dcdbas_driver = {
593 .owner = THIS_MODULE,
595 .probe = dcdbas_probe,
596 .remove = __devexit_p(dcdbas_remove),
600 * dcdbas_init: initialize driver
602 static int __init dcdbas_init(void)
606 error = platform_driver_register(&dcdbas_driver);
610 dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1);
613 goto err_unregister_driver;
616 error = platform_device_add(dcdbas_pdev);
618 goto err_free_device;
623 platform_device_put(dcdbas_pdev);
624 err_unregister_driver:
625 platform_driver_unregister(&dcdbas_driver);
630 * dcdbas_exit: perform driver cleanup
632 static void __exit dcdbas_exit(void)
635 * make sure functions that use dcdbas_pdev are called
636 * before platform_device_unregister
638 unregister_reboot_notifier(&dcdbas_reboot_nb);
640 platform_device_unregister(dcdbas_pdev);
641 platform_driver_unregister(&dcdbas_driver);
644 * We have to free the buffer here instead of dcdbas_remove
645 * because only in module exit function we can be sure that
646 * all sysfs attributes belonging to this module have been
652 module_init(dcdbas_init);
653 module_exit(dcdbas_exit);
655 MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
656 MODULE_VERSION(DRIVER_VERSION);
657 MODULE_AUTHOR("Dell Inc.");
658 MODULE_LICENSE("GPL");