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-2005 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/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>
37 #include <asm/semaphore.h>
41 #define DRIVER_NAME "dcdbas"
42 #define DRIVER_VERSION "5.6.0-1"
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 DECLARE_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 __FUNCTION__, 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 __FUNCTION__, 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 down(&smi_data_lock);
143 ret = smi_data_buf_realloc(buf_size);
151 static ssize_t smi_data_read(struct kobject *kobj, char *buf, loff_t pos,
157 down(&smi_data_lock);
159 if (pos >= smi_data_buf_size) {
164 max_read = smi_data_buf_size - pos;
165 ret = min(max_read, count);
166 memcpy(buf, smi_data_buf + pos, ret);
172 static ssize_t smi_data_write(struct kobject *kobj, char *buf, loff_t pos,
177 down(&smi_data_lock);
179 ret = smi_data_buf_realloc(pos + count);
183 memcpy(smi_data_buf + pos, buf, count);
190 static ssize_t host_control_action_show(struct device *dev,
191 struct device_attribute *attr,
194 return sprintf(buf, "%u\n", host_control_action);
197 static ssize_t host_control_action_store(struct device *dev,
198 struct device_attribute *attr,
199 const char *buf, size_t count)
203 /* make sure buffer is available for host control command */
204 down(&smi_data_lock);
205 ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
210 host_control_action = simple_strtoul(buf, NULL, 10);
214 static ssize_t host_control_smi_type_show(struct device *dev,
215 struct device_attribute *attr,
218 return sprintf(buf, "%u\n", host_control_smi_type);
221 static ssize_t host_control_smi_type_store(struct device *dev,
222 struct device_attribute *attr,
223 const char *buf, size_t count)
225 host_control_smi_type = simple_strtoul(buf, NULL, 10);
229 static ssize_t host_control_on_shutdown_show(struct device *dev,
230 struct device_attribute *attr,
233 return sprintf(buf, "%u\n", host_control_on_shutdown);
236 static ssize_t host_control_on_shutdown_store(struct device *dev,
237 struct device_attribute *attr,
238 const char *buf, size_t count)
240 host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
245 * smi_request: generate SMI request
247 * Called with smi_data_lock.
249 static int smi_request(struct smi_cmd *smi_cmd)
254 if (smi_cmd->magic != SMI_CMD_MAGIC) {
255 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
260 /* SMI requires CPU 0 */
261 old_mask = current->cpus_allowed;
262 set_cpus_allowed(current, cpumask_of_cpu(0));
263 if (smp_processor_id() != 0) {
264 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
273 : /* no output args */
274 : "a" (smi_cmd->command_code),
275 "d" (smi_cmd->command_address),
282 set_cpus_allowed(current, old_mask);
289 * The valid values are:
290 * 0: zero SMI data buffer
291 * 1: generate calling interface SMI
292 * 2: generate raw SMI
294 * User application writes smi_cmd to smi_data before telling driver
297 static ssize_t smi_request_store(struct device *dev,
298 struct device_attribute *attr,
299 const char *buf, size_t count)
301 struct smi_cmd *smi_cmd;
302 unsigned long val = simple_strtoul(buf, NULL, 10);
305 down(&smi_data_lock);
307 if (smi_data_buf_size < sizeof(struct smi_cmd)) {
311 smi_cmd = (struct smi_cmd *)smi_data_buf;
316 ret = smi_request(smi_cmd);
321 /* Calling Interface SMI */
322 smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer);
323 ret = smi_request(smi_cmd);
328 memset(smi_data_buf, 0, smi_data_buf_size);
342 * host_control_smi: generate host control SMI
344 * Caller must set up the host control command in smi_data_buf.
346 static int host_control_smi(void)
348 struct apm_cmd *apm_cmd;
355 apm_cmd = (struct apm_cmd *)smi_data_buf;
356 apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
358 switch (host_control_smi_type) {
359 case HC_SMITYPE_TYPE1:
360 spin_lock_irqsave(&rtc_lock, flags);
361 /* write SMI data buffer physical address */
362 data = (u8 *)&smi_data_buf_phys_addr;
363 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
364 index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
367 (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
369 (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
372 /* first set status to -1 as called by spec */
373 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
374 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
376 /* generate SMM call */
377 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
378 spin_unlock_irqrestore(&rtc_lock, flags);
380 /* wait a few to see if it executed */
381 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
382 while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
383 == ESM_STATUS_CMD_UNSUCCESSFUL) {
385 if (num_ticks == EXPIRED_TIMER)
390 case HC_SMITYPE_TYPE2:
391 case HC_SMITYPE_TYPE3:
392 spin_lock_irqsave(&rtc_lock, flags);
393 /* write SMI data buffer physical address */
394 data = (u8 *)&smi_data_buf_phys_addr;
395 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
396 index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
398 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
399 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
402 /* generate SMM call */
403 if (host_control_smi_type == HC_SMITYPE_TYPE3)
404 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
406 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
408 /* restore RTC index pointer since it was written to above */
409 CMOS_READ(RTC_REG_C);
410 spin_unlock_irqrestore(&rtc_lock, flags);
412 /* read control port back to serialize write */
413 cmd_status = inb(PE1400_APM_CONTROL_PORT);
415 /* wait a few to see if it executed */
416 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
417 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
419 if (num_ticks == EXPIRED_TIMER)
425 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
426 __FUNCTION__, host_control_smi_type);
434 * dcdbas_host_control: initiate host control
436 * This function is called by the driver after the system has
437 * finished shutting down if the user application specified a
438 * host control action to perform on shutdown. It is safe to
439 * use smi_data_buf at this point because the system has finished
440 * shutting down and no userspace apps are running.
442 static void dcdbas_host_control(void)
444 struct apm_cmd *apm_cmd;
447 if (host_control_action == HC_ACTION_NONE)
450 action = host_control_action;
451 host_control_action = HC_ACTION_NONE;
454 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __FUNCTION__);
458 if (smi_data_buf_size < sizeof(struct apm_cmd)) {
459 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
464 apm_cmd = (struct apm_cmd *)smi_data_buf;
466 /* power off takes precedence */
467 if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
468 apm_cmd->command = ESM_APM_POWER_CYCLE;
469 apm_cmd->reserved = 0;
470 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
472 } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
473 apm_cmd->command = ESM_APM_POWER_CYCLE;
474 apm_cmd->reserved = 0;
475 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
481 * dcdbas_reboot_notify: handle reboot notification for host control
483 static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
486 static unsigned int notify_cnt = 0;
492 if (host_control_on_shutdown) {
493 /* firmware is going to perform host control action */
494 if (++notify_cnt == 2) {
496 "Please wait for shutdown "
497 "action to complete...\n");
498 dcdbas_host_control();
501 * register again and initiate the host control
502 * action on the second notification to allow
503 * everyone that registered to be notified
505 register_reboot_notifier(nb);
513 static struct notifier_block dcdbas_reboot_nb = {
514 .notifier_call = dcdbas_reboot_notify,
519 static DCDBAS_BIN_ATTR_RW(smi_data);
521 static struct bin_attribute *dcdbas_bin_attrs[] = {
526 static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
527 static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
528 static DCDBAS_DEV_ATTR_WO(smi_request);
529 static DCDBAS_DEV_ATTR_RW(host_control_action);
530 static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
531 static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
533 static struct device_attribute *dcdbas_dev_attrs[] = {
534 &dev_attr_smi_data_buf_size,
535 &dev_attr_smi_data_buf_phys_addr,
536 &dev_attr_smi_request,
537 &dev_attr_host_control_action,
538 &dev_attr_host_control_smi_type,
539 &dev_attr_host_control_on_shutdown,
544 * dcdbas_init: initialize driver
546 static int __init dcdbas_init(void)
550 host_control_action = HC_ACTION_NONE;
551 host_control_smi_type = HC_SMITYPE_NONE;
553 dcdbas_pdev = platform_device_register_simple(DRIVER_NAME, -1, NULL, 0);
554 if (IS_ERR(dcdbas_pdev))
555 return PTR_ERR(dcdbas_pdev);
558 * BIOS SMI calls require buffer addresses be in 32-bit address space.
559 * This is done by setting the DMA mask below.
561 dcdbas_pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
562 dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask;
564 register_reboot_notifier(&dcdbas_reboot_nb);
566 for (i = 0; dcdbas_bin_attrs[i]; i++)
567 sysfs_create_bin_file(&dcdbas_pdev->dev.kobj,
568 dcdbas_bin_attrs[i]);
570 for (i = 0; dcdbas_dev_attrs[i]; i++)
571 device_create_file(&dcdbas_pdev->dev, dcdbas_dev_attrs[i]);
573 dev_info(&dcdbas_pdev->dev, "%s (version %s)\n",
574 DRIVER_DESCRIPTION, DRIVER_VERSION);
580 * dcdbas_exit: perform driver cleanup
582 static void __exit dcdbas_exit(void)
584 platform_device_unregister(dcdbas_pdev);
585 unregister_reboot_notifier(&dcdbas_reboot_nb);
589 module_init(dcdbas_init);
590 module_exit(dcdbas_exit);
592 MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
593 MODULE_VERSION(DRIVER_VERSION);
594 MODULE_AUTHOR("Dell Inc.");
595 MODULE_LICENSE("GPL");