drivers/char/Kconfig: don't mess it up for everyone else
[linux-2.6.32.60-moxart.git] / drivers / firmware / dcdbas.c
blob3abb51577a52b205218e64010588a0824c99793b
1 /*
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
7 * Dell systems.
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>
37 #include <asm/io.h>
39 #include "dcdbas.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;
57 /**
58 * smi_data_buf_free: free SMI data buffer
60 static void smi_data_buf_free(void)
62 if (!smi_data_buf)
63 return;
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,
69 smi_data_buf_handle);
70 smi_data_buf = NULL;
71 smi_data_buf_handle = 0;
72 smi_data_buf_phys_addr = 0;
73 smi_data_buf_size = 0;
76 /**
77 * smi_data_buf_realloc: grow SMI data buffer if needed
79 static int smi_data_buf_realloc(unsigned long size)
81 void *buf;
82 dma_addr_t handle;
84 if (smi_data_buf_size >= size)
85 return 0;
87 if (size > MAX_SMI_DATA_BUF_SIZE)
88 return -EINVAL;
90 /* new buffer is needed */
91 buf = dma_alloc_coherent(&dcdbas_pdev->dev, size, &handle, GFP_KERNEL);
92 if (!buf) {
93 dev_dbg(&dcdbas_pdev->dev,
94 "%s: failed to allocate memory size %lu\n",
95 __func__, size);
96 return -ENOMEM;
98 /* memory zeroed by dma_alloc_coherent */
100 if (smi_data_buf)
101 memcpy(buf, smi_data_buf, smi_data_buf_size);
103 /* free any existing buffer */
104 smi_data_buf_free();
106 /* set up new buffer for use */
107 smi_data_buf = buf;
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);
115 return 0;
118 static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
119 struct device_attribute *attr,
120 char *buf)
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,
127 char *buf)
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;
137 ssize_t ret;
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);
145 if (ret)
146 return ret;
148 return count;
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)
155 ssize_t ret;
157 mutex_lock(&smi_data_lock);
158 ret = memory_read_from_buffer(buf, count, &pos, smi_data_buf,
159 smi_data_buf_size);
160 mutex_unlock(&smi_data_lock);
161 return ret;
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)
168 ssize_t ret;
170 if ((pos + count) > MAX_SMI_DATA_BUF_SIZE)
171 return -EINVAL;
173 mutex_lock(&smi_data_lock);
175 ret = smi_data_buf_realloc(pos + count);
176 if (ret)
177 goto out;
179 memcpy(smi_data_buf + pos, buf, count);
180 ret = count;
181 out:
182 mutex_unlock(&smi_data_lock);
183 return ret;
186 static ssize_t host_control_action_show(struct device *dev,
187 struct device_attribute *attr,
188 char *buf)
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)
197 ssize_t ret;
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);
203 if (ret)
204 return ret;
206 host_control_action = simple_strtoul(buf, NULL, 10);
207 return count;
210 static ssize_t host_control_smi_type_show(struct device *dev,
211 struct device_attribute *attr,
212 char *buf)
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);
222 return count;
225 static ssize_t host_control_on_shutdown_show(struct device *dev,
226 struct device_attribute *attr,
227 char *buf)
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);
237 return count;
241 * dcdbas_smi_request: generate SMI request
243 * Called with smi_data_lock.
245 int dcdbas_smi_request(struct smi_cmd *smi_cmd)
247 cpumask_var_t old_mask;
248 int ret = 0;
250 if (smi_cmd->magic != SMI_CMD_MAGIC) {
251 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
252 __func__);
253 return -EBADR;
256 /* SMI requires CPU 0 */
257 if (!alloc_cpumask_var(&old_mask, GFP_KERNEL))
258 return -ENOMEM;
260 cpumask_copy(old_mask, &current->cpus_allowed);
261 set_cpus_allowed_ptr(current, cpumask_of(0));
262 if (smp_processor_id() != 0) {
263 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
264 __func__);
265 ret = -EBUSY;
266 goto out;
269 /* generate SMI */
270 /* inb to force posted write through and make SMI happen now */
271 asm volatile (
272 "outb %b0,%w1\n"
273 "inb %w1"
274 : /* no output args */
275 : "a" (smi_cmd->command_code),
276 "d" (smi_cmd->command_address),
277 "b" (smi_cmd->ebx),
278 "c" (smi_cmd->ecx)
279 : "memory"
282 out:
283 set_cpus_allowed_ptr(current, old_mask);
284 free_cpumask_var(old_mask);
285 return ret;
289 * smi_request_store:
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
297 * to generate SMI.
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);
305 ssize_t ret;
307 mutex_lock(&smi_data_lock);
309 if (smi_data_buf_size < sizeof(struct smi_cmd)) {
310 ret = -ENODEV;
311 goto out;
313 smi_cmd = (struct smi_cmd *)smi_data_buf;
315 switch (val) {
316 case 2:
317 /* Raw SMI */
318 ret = dcdbas_smi_request(smi_cmd);
319 if (!ret)
320 ret = count;
321 break;
322 case 1:
323 /* Calling Interface SMI */
324 smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer);
325 ret = dcdbas_smi_request(smi_cmd);
326 if (!ret)
327 ret = count;
328 break;
329 case 0:
330 memset(smi_data_buf, 0, smi_data_buf_size);
331 ret = count;
332 break;
333 default:
334 ret = -EINVAL;
335 break;
338 out:
339 mutex_unlock(&smi_data_lock);
340 return ret;
342 EXPORT_SYMBOL(dcdbas_smi_request);
345 * host_control_smi: generate host control SMI
347 * Caller must set up the host control command in smi_data_buf.
349 static int host_control_smi(void)
351 struct apm_cmd *apm_cmd;
352 u8 *data;
353 unsigned long flags;
354 u32 num_ticks;
355 s8 cmd_status;
356 u8 index;
358 apm_cmd = (struct apm_cmd *)smi_data_buf;
359 apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
361 switch (host_control_smi_type) {
362 case HC_SMITYPE_TYPE1:
363 spin_lock_irqsave(&rtc_lock, flags);
364 /* write SMI data buffer physical address */
365 data = (u8 *)&smi_data_buf_phys_addr;
366 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
367 index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
368 index++, data++) {
369 outb(index,
370 (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
371 outb(*data,
372 (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
375 /* first set status to -1 as called by spec */
376 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
377 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
379 /* generate SMM call */
380 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
381 spin_unlock_irqrestore(&rtc_lock, flags);
383 /* wait a few to see if it executed */
384 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
385 while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
386 == ESM_STATUS_CMD_UNSUCCESSFUL) {
387 num_ticks--;
388 if (num_ticks == EXPIRED_TIMER)
389 return -ETIME;
391 break;
393 case HC_SMITYPE_TYPE2:
394 case HC_SMITYPE_TYPE3:
395 spin_lock_irqsave(&rtc_lock, flags);
396 /* write SMI data buffer physical address */
397 data = (u8 *)&smi_data_buf_phys_addr;
398 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
399 index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
400 index++, data++) {
401 outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
402 outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
405 /* generate SMM call */
406 if (host_control_smi_type == HC_SMITYPE_TYPE3)
407 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
408 else
409 outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
411 /* restore RTC index pointer since it was written to above */
412 CMOS_READ(RTC_REG_C);
413 spin_unlock_irqrestore(&rtc_lock, flags);
415 /* read control port back to serialize write */
416 cmd_status = inb(PE1400_APM_CONTROL_PORT);
418 /* wait a few to see if it executed */
419 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
420 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
421 num_ticks--;
422 if (num_ticks == EXPIRED_TIMER)
423 return -ETIME;
425 break;
427 default:
428 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
429 __func__, host_control_smi_type);
430 return -ENOSYS;
433 return 0;
437 * dcdbas_host_control: initiate host control
439 * This function is called by the driver after the system has
440 * finished shutting down if the user application specified a
441 * host control action to perform on shutdown. It is safe to
442 * use smi_data_buf at this point because the system has finished
443 * shutting down and no userspace apps are running.
445 static void dcdbas_host_control(void)
447 struct apm_cmd *apm_cmd;
448 u8 action;
450 if (host_control_action == HC_ACTION_NONE)
451 return;
453 action = host_control_action;
454 host_control_action = HC_ACTION_NONE;
456 if (!smi_data_buf) {
457 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
458 return;
461 if (smi_data_buf_size < sizeof(struct apm_cmd)) {
462 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
463 __func__);
464 return;
467 apm_cmd = (struct apm_cmd *)smi_data_buf;
469 /* power off takes precedence */
470 if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
471 apm_cmd->command = ESM_APM_POWER_CYCLE;
472 apm_cmd->reserved = 0;
473 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
474 host_control_smi();
475 } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
476 apm_cmd->command = ESM_APM_POWER_CYCLE;
477 apm_cmd->reserved = 0;
478 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
479 host_control_smi();
484 * dcdbas_reboot_notify: handle reboot notification for host control
486 static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
487 void *unused)
489 switch (code) {
490 case SYS_DOWN:
491 case SYS_HALT:
492 case SYS_POWER_OFF:
493 if (host_control_on_shutdown) {
494 /* firmware is going to perform host control action */
495 printk(KERN_WARNING "Please wait for shutdown "
496 "action to complete...\n");
497 dcdbas_host_control();
499 break;
502 return NOTIFY_DONE;
505 static struct notifier_block dcdbas_reboot_nb = {
506 .notifier_call = dcdbas_reboot_notify,
507 .next = NULL,
508 .priority = INT_MIN
511 static DCDBAS_BIN_ATTR_RW(smi_data);
513 static struct bin_attribute *dcdbas_bin_attrs[] = {
514 &bin_attr_smi_data,
515 NULL
518 static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
519 static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
520 static DCDBAS_DEV_ATTR_WO(smi_request);
521 static DCDBAS_DEV_ATTR_RW(host_control_action);
522 static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
523 static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
525 static struct attribute *dcdbas_dev_attrs[] = {
526 &dev_attr_smi_data_buf_size.attr,
527 &dev_attr_smi_data_buf_phys_addr.attr,
528 &dev_attr_smi_request.attr,
529 &dev_attr_host_control_action.attr,
530 &dev_attr_host_control_smi_type.attr,
531 &dev_attr_host_control_on_shutdown.attr,
532 NULL
535 static struct attribute_group dcdbas_attr_group = {
536 .attrs = dcdbas_dev_attrs,
539 static int __devinit dcdbas_probe(struct platform_device *dev)
541 int i, error;
543 host_control_action = HC_ACTION_NONE;
544 host_control_smi_type = HC_SMITYPE_NONE;
547 * BIOS SMI calls require buffer addresses be in 32-bit address space.
548 * This is done by setting the DMA mask below.
550 dcdbas_pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
551 dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask;
553 error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
554 if (error)
555 return error;
557 for (i = 0; dcdbas_bin_attrs[i]; i++) {
558 error = sysfs_create_bin_file(&dev->dev.kobj,
559 dcdbas_bin_attrs[i]);
560 if (error) {
561 while (--i >= 0)
562 sysfs_remove_bin_file(&dev->dev.kobj,
563 dcdbas_bin_attrs[i]);
564 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
565 return error;
569 register_reboot_notifier(&dcdbas_reboot_nb);
571 dev_info(&dev->dev, "%s (version %s)\n",
572 DRIVER_DESCRIPTION, DRIVER_VERSION);
574 return 0;
577 static int __devexit dcdbas_remove(struct platform_device *dev)
579 int i;
581 unregister_reboot_notifier(&dcdbas_reboot_nb);
582 for (i = 0; dcdbas_bin_attrs[i]; i++)
583 sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]);
584 sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
586 return 0;
589 static struct platform_driver dcdbas_driver = {
590 .driver = {
591 .name = DRIVER_NAME,
592 .owner = THIS_MODULE,
594 .probe = dcdbas_probe,
595 .remove = __devexit_p(dcdbas_remove),
599 * dcdbas_init: initialize driver
601 static int __init dcdbas_init(void)
603 int error;
605 error = platform_driver_register(&dcdbas_driver);
606 if (error)
607 return error;
609 dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1);
610 if (!dcdbas_pdev) {
611 error = -ENOMEM;
612 goto err_unregister_driver;
615 error = platform_device_add(dcdbas_pdev);
616 if (error)
617 goto err_free_device;
619 return 0;
621 err_free_device:
622 platform_device_put(dcdbas_pdev);
623 err_unregister_driver:
624 platform_driver_unregister(&dcdbas_driver);
625 return error;
629 * dcdbas_exit: perform driver cleanup
631 static void __exit dcdbas_exit(void)
634 * make sure functions that use dcdbas_pdev are called
635 * before platform_device_unregister
637 unregister_reboot_notifier(&dcdbas_reboot_nb);
638 smi_data_buf_free();
639 platform_device_unregister(dcdbas_pdev);
640 platform_driver_unregister(&dcdbas_driver);
643 * We have to free the buffer here instead of dcdbas_remove
644 * because only in module exit function we can be sure that
645 * all sysfs attributes belonging to this module have been
646 * released.
648 smi_data_buf_free();
651 module_init(dcdbas_init);
652 module_exit(dcdbas_exit);
654 MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
655 MODULE_VERSION(DRIVER_VERSION);
656 MODULE_AUTHOR("Dell Inc.");
657 MODULE_LICENSE("GPL");
658 /* Any System or BIOS claiming to be by Dell */
659 MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");