Merge branch 'sched/for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/tip...
[linux-2.6] / drivers / firmware / dcdbas.c
1 /*
2  *  dcdbas.c: Dell Systems Management Base Driver
3  *
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.
8  *
9  *  See Documentation/dcdbas.txt for more information.
10  *
11  *  Copyright (C) 1995-2006 Dell Inc.
12  *
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.
16  *
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.
21  */
22
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>
38
39 #include "dcdbas.h"
40
41 #define DRIVER_NAME             "dcdbas"
42 #define DRIVER_VERSION          "5.6.0-3.2"
43 #define DRIVER_DESCRIPTION      "Dell Systems Management Base Driver"
44
45 static struct platform_device *dcdbas_pdev;
46
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);
52
53 static unsigned int host_control_action;
54 static unsigned int host_control_smi_type;
55 static unsigned int host_control_on_shutdown;
56
57 /**
58  * smi_data_buf_free: free SMI data buffer
59  */
60 static void smi_data_buf_free(void)
61 {
62         if (!smi_data_buf)
63                 return;
64
65         dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
66                 __func__, smi_data_buf_phys_addr, smi_data_buf_size);
67
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;
74 }
75
76 /**
77  * smi_data_buf_realloc: grow SMI data buffer if needed
78  */
79 static int smi_data_buf_realloc(unsigned long size)
80 {
81         void *buf;
82         dma_addr_t handle;
83
84         if (smi_data_buf_size >= size)
85                 return 0;
86
87         if (size > MAX_SMI_DATA_BUF_SIZE)
88                 return -EINVAL;
89
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;
97         }
98         /* memory zeroed by dma_alloc_coherent */
99
100         if (smi_data_buf)
101                 memcpy(buf, smi_data_buf, smi_data_buf_size);
102
103         /* free any existing buffer */
104         smi_data_buf_free();
105
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;
111
112         dev_dbg(&dcdbas_pdev->dev, "%s: phys: %x size: %lu\n",
113                 __func__, smi_data_buf_phys_addr, smi_data_buf_size);
114
115         return 0;
116 }
117
118 static ssize_t smi_data_buf_phys_addr_show(struct device *dev,
119                                            struct device_attribute *attr,
120                                            char *buf)
121 {
122         return sprintf(buf, "%x\n", smi_data_buf_phys_addr);
123 }
124
125 static ssize_t smi_data_buf_size_show(struct device *dev,
126                                       struct device_attribute *attr,
127                                       char *buf)
128 {
129         return sprintf(buf, "%lu\n", smi_data_buf_size);
130 }
131
132 static ssize_t smi_data_buf_size_store(struct device *dev,
133                                        struct device_attribute *attr,
134                                        const char *buf, size_t count)
135 {
136         unsigned long buf_size;
137         ssize_t ret;
138
139         buf_size = simple_strtoul(buf, NULL, 10);
140
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;
147
148         return count;
149 }
150
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)
154 {
155         size_t max_read;
156         ssize_t ret;
157
158         mutex_lock(&smi_data_lock);
159
160         if (pos >= smi_data_buf_size) {
161                 ret = 0;
162                 goto out;
163         }
164
165         max_read = smi_data_buf_size - pos;
166         ret = min(max_read, count);
167         memcpy(buf, smi_data_buf + pos, ret);
168 out:
169         mutex_unlock(&smi_data_lock);
170         return ret;
171 }
172
173 static ssize_t smi_data_write(struct kobject *kobj,
174                               struct bin_attribute *bin_attr,
175                               char *buf, loff_t pos, size_t count)
176 {
177         ssize_t ret;
178
179         if ((pos + count) > MAX_SMI_DATA_BUF_SIZE)
180                 return -EINVAL;
181
182         mutex_lock(&smi_data_lock);
183
184         ret = smi_data_buf_realloc(pos + count);
185         if (ret)
186                 goto out;
187
188         memcpy(smi_data_buf + pos, buf, count);
189         ret = count;
190 out:
191         mutex_unlock(&smi_data_lock);
192         return ret;
193 }
194
195 static ssize_t host_control_action_show(struct device *dev,
196                                         struct device_attribute *attr,
197                                         char *buf)
198 {
199         return sprintf(buf, "%u\n", host_control_action);
200 }
201
202 static ssize_t host_control_action_store(struct device *dev,
203                                          struct device_attribute *attr,
204                                          const char *buf, size_t count)
205 {
206         ssize_t ret;
207
208         /* make sure buffer is available for host control command */
209         mutex_lock(&smi_data_lock);
210         ret = smi_data_buf_realloc(sizeof(struct apm_cmd));
211         mutex_unlock(&smi_data_lock);
212         if (ret)
213                 return ret;
214
215         host_control_action = simple_strtoul(buf, NULL, 10);
216         return count;
217 }
218
219 static ssize_t host_control_smi_type_show(struct device *dev,
220                                           struct device_attribute *attr,
221                                           char *buf)
222 {
223         return sprintf(buf, "%u\n", host_control_smi_type);
224 }
225
226 static ssize_t host_control_smi_type_store(struct device *dev,
227                                            struct device_attribute *attr,
228                                            const char *buf, size_t count)
229 {
230         host_control_smi_type = simple_strtoul(buf, NULL, 10);
231         return count;
232 }
233
234 static ssize_t host_control_on_shutdown_show(struct device *dev,
235                                              struct device_attribute *attr,
236                                              char *buf)
237 {
238         return sprintf(buf, "%u\n", host_control_on_shutdown);
239 }
240
241 static ssize_t host_control_on_shutdown_store(struct device *dev,
242                                               struct device_attribute *attr,
243                                               const char *buf, size_t count)
244 {
245         host_control_on_shutdown = simple_strtoul(buf, NULL, 10);
246         return count;
247 }
248
249 /**
250  * smi_request: generate SMI request
251  *
252  * Called with smi_data_lock.
253  */
254 static int smi_request(struct smi_cmd *smi_cmd)
255 {
256         cpumask_t old_mask;
257         cpumask_of_cpu_ptr(new_mask, 0);
258         int ret = 0;
259
260         if (smi_cmd->magic != SMI_CMD_MAGIC) {
261                 dev_info(&dcdbas_pdev->dev, "%s: invalid magic value\n",
262                          __func__);
263                 return -EBADR;
264         }
265
266         /* SMI requires CPU 0 */
267         old_mask = current->cpus_allowed;
268         set_cpus_allowed_ptr(current, new_mask);
269         if (smp_processor_id() != 0) {
270                 dev_dbg(&dcdbas_pdev->dev, "%s: failed to get CPU 0\n",
271                         __func__);
272                 ret = -EBUSY;
273                 goto out;
274         }
275
276         /* generate SMI */
277         asm volatile (
278                 "outb %b0,%w1"
279                 : /* no output args */
280                 : "a" (smi_cmd->command_code),
281                   "d" (smi_cmd->command_address),
282                   "b" (smi_cmd->ebx),
283                   "c" (smi_cmd->ecx)
284                 : "memory"
285         );
286
287 out:
288         set_cpus_allowed_ptr(current, &old_mask);
289         return ret;
290 }
291
292 /**
293  * smi_request_store:
294  *
295  * The valid values are:
296  * 0: zero SMI data buffer
297  * 1: generate calling interface SMI
298  * 2: generate raw SMI
299  *
300  * User application writes smi_cmd to smi_data before telling driver
301  * to generate SMI.
302  */
303 static ssize_t smi_request_store(struct device *dev,
304                                  struct device_attribute *attr,
305                                  const char *buf, size_t count)
306 {
307         struct smi_cmd *smi_cmd;
308         unsigned long val = simple_strtoul(buf, NULL, 10);
309         ssize_t ret;
310
311         mutex_lock(&smi_data_lock);
312
313         if (smi_data_buf_size < sizeof(struct smi_cmd)) {
314                 ret = -ENODEV;
315                 goto out;
316         }
317         smi_cmd = (struct smi_cmd *)smi_data_buf;
318
319         switch (val) {
320         case 2:
321                 /* Raw SMI */
322                 ret = smi_request(smi_cmd);
323                 if (!ret)
324                         ret = count;
325                 break;
326         case 1:
327                 /* Calling Interface SMI */
328                 smi_cmd->ebx = (u32) virt_to_phys(smi_cmd->command_buffer);
329                 ret = smi_request(smi_cmd);
330                 if (!ret)
331                         ret = count;
332                 break;
333         case 0:
334                 memset(smi_data_buf, 0, smi_data_buf_size);
335                 ret = count;
336                 break;
337         default:
338                 ret = -EINVAL;
339                 break;
340         }
341
342 out:
343         mutex_unlock(&smi_data_lock);
344         return ret;
345 }
346
347 /**
348  * host_control_smi: generate host control SMI
349  *
350  * Caller must set up the host control command in smi_data_buf.
351  */
352 static int host_control_smi(void)
353 {
354         struct apm_cmd *apm_cmd;
355         u8 *data;
356         unsigned long flags;
357         u32 num_ticks;
358         s8 cmd_status;
359         u8 index;
360
361         apm_cmd = (struct apm_cmd *)smi_data_buf;
362         apm_cmd->status = ESM_STATUS_CMD_UNSUCCESSFUL;
363
364         switch (host_control_smi_type) {
365         case HC_SMITYPE_TYPE1:
366                 spin_lock_irqsave(&rtc_lock, flags);
367                 /* write SMI data buffer physical address */
368                 data = (u8 *)&smi_data_buf_phys_addr;
369                 for (index = PE1300_CMOS_CMD_STRUCT_PTR;
370                      index < (PE1300_CMOS_CMD_STRUCT_PTR + 4);
371                      index++, data++) {
372                         outb(index,
373                              (CMOS_BASE_PORT + CMOS_PAGE2_INDEX_PORT_PIIX4));
374                         outb(*data,
375                              (CMOS_BASE_PORT + CMOS_PAGE2_DATA_PORT_PIIX4));
376                 }
377
378                 /* first set status to -1 as called by spec */
379                 cmd_status = ESM_STATUS_CMD_UNSUCCESSFUL;
380                 outb((u8) cmd_status, PCAT_APM_STATUS_PORT);
381
382                 /* generate SMM call */
383                 outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
384                 spin_unlock_irqrestore(&rtc_lock, flags);
385
386                 /* wait a few to see if it executed */
387                 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
388                 while ((cmd_status = inb(PCAT_APM_STATUS_PORT))
389                        == ESM_STATUS_CMD_UNSUCCESSFUL) {
390                         num_ticks--;
391                         if (num_ticks == EXPIRED_TIMER)
392                                 return -ETIME;
393                 }
394                 break;
395
396         case HC_SMITYPE_TYPE2:
397         case HC_SMITYPE_TYPE3:
398                 spin_lock_irqsave(&rtc_lock, flags);
399                 /* write SMI data buffer physical address */
400                 data = (u8 *)&smi_data_buf_phys_addr;
401                 for (index = PE1400_CMOS_CMD_STRUCT_PTR;
402                      index < (PE1400_CMOS_CMD_STRUCT_PTR + 4);
403                      index++, data++) {
404                         outb(index, (CMOS_BASE_PORT + CMOS_PAGE1_INDEX_PORT));
405                         outb(*data, (CMOS_BASE_PORT + CMOS_PAGE1_DATA_PORT));
406                 }
407
408                 /* generate SMM call */
409                 if (host_control_smi_type == HC_SMITYPE_TYPE3)
410                         outb(ESM_APM_CMD, PCAT_APM_CONTROL_PORT);
411                 else
412                         outb(ESM_APM_CMD, PE1400_APM_CONTROL_PORT);
413
414                 /* restore RTC index pointer since it was written to above */
415                 CMOS_READ(RTC_REG_C);
416                 spin_unlock_irqrestore(&rtc_lock, flags);
417
418                 /* read control port back to serialize write */
419                 cmd_status = inb(PE1400_APM_CONTROL_PORT);
420
421                 /* wait a few to see if it executed */
422                 num_ticks = TIMEOUT_USEC_SHORT_SEMA_BLOCKING;
423                 while (apm_cmd->status == ESM_STATUS_CMD_UNSUCCESSFUL) {
424                         num_ticks--;
425                         if (num_ticks == EXPIRED_TIMER)
426                                 return -ETIME;
427                 }
428                 break;
429
430         default:
431                 dev_dbg(&dcdbas_pdev->dev, "%s: invalid SMI type %u\n",
432                         __func__, host_control_smi_type);
433                 return -ENOSYS;
434         }
435
436         return 0;
437 }
438
439 /**
440  * dcdbas_host_control: initiate host control
441  *
442  * This function is called by the driver after the system has
443  * finished shutting down if the user application specified a
444  * host control action to perform on shutdown.  It is safe to
445  * use smi_data_buf at this point because the system has finished
446  * shutting down and no userspace apps are running.
447  */
448 static void dcdbas_host_control(void)
449 {
450         struct apm_cmd *apm_cmd;
451         u8 action;
452
453         if (host_control_action == HC_ACTION_NONE)
454                 return;
455
456         action = host_control_action;
457         host_control_action = HC_ACTION_NONE;
458
459         if (!smi_data_buf) {
460                 dev_dbg(&dcdbas_pdev->dev, "%s: no SMI buffer\n", __func__);
461                 return;
462         }
463
464         if (smi_data_buf_size < sizeof(struct apm_cmd)) {
465                 dev_dbg(&dcdbas_pdev->dev, "%s: SMI buffer too small\n",
466                         __func__);
467                 return;
468         }
469
470         apm_cmd = (struct apm_cmd *)smi_data_buf;
471
472         /* power off takes precedence */
473         if (action & HC_ACTION_HOST_CONTROL_POWEROFF) {
474                 apm_cmd->command = ESM_APM_POWER_CYCLE;
475                 apm_cmd->reserved = 0;
476                 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 0;
477                 host_control_smi();
478         } else if (action & HC_ACTION_HOST_CONTROL_POWERCYCLE) {
479                 apm_cmd->command = ESM_APM_POWER_CYCLE;
480                 apm_cmd->reserved = 0;
481                 *((s16 *)&apm_cmd->parameters.shortreq.parm[0]) = (s16) 20;
482                 host_control_smi();
483         }
484 }
485
486 /**
487  * dcdbas_reboot_notify: handle reboot notification for host control
488  */
489 static int dcdbas_reboot_notify(struct notifier_block *nb, unsigned long code,
490                                 void *unused)
491 {
492         switch (code) {
493         case SYS_DOWN:
494         case SYS_HALT:
495         case SYS_POWER_OFF:
496                 if (host_control_on_shutdown) {
497                         /* firmware is going to perform host control action */
498                         printk(KERN_WARNING "Please wait for shutdown "
499                                "action to complete...\n");
500                         dcdbas_host_control();
501                 }
502                 break;
503         }
504
505         return NOTIFY_DONE;
506 }
507
508 static struct notifier_block dcdbas_reboot_nb = {
509         .notifier_call = dcdbas_reboot_notify,
510         .next = NULL,
511         .priority = INT_MIN
512 };
513
514 static DCDBAS_BIN_ATTR_RW(smi_data);
515
516 static struct bin_attribute *dcdbas_bin_attrs[] = {
517         &bin_attr_smi_data,
518         NULL
519 };
520
521 static DCDBAS_DEV_ATTR_RW(smi_data_buf_size);
522 static DCDBAS_DEV_ATTR_RO(smi_data_buf_phys_addr);
523 static DCDBAS_DEV_ATTR_WO(smi_request);
524 static DCDBAS_DEV_ATTR_RW(host_control_action);
525 static DCDBAS_DEV_ATTR_RW(host_control_smi_type);
526 static DCDBAS_DEV_ATTR_RW(host_control_on_shutdown);
527
528 static struct attribute *dcdbas_dev_attrs[] = {
529         &dev_attr_smi_data_buf_size.attr,
530         &dev_attr_smi_data_buf_phys_addr.attr,
531         &dev_attr_smi_request.attr,
532         &dev_attr_host_control_action.attr,
533         &dev_attr_host_control_smi_type.attr,
534         &dev_attr_host_control_on_shutdown.attr,
535         NULL
536 };
537
538 static struct attribute_group dcdbas_attr_group = {
539         .attrs = dcdbas_dev_attrs,
540 };
541
542 static int __devinit dcdbas_probe(struct platform_device *dev)
543 {
544         int i, error;
545
546         host_control_action = HC_ACTION_NONE;
547         host_control_smi_type = HC_SMITYPE_NONE;
548
549         /*
550          * BIOS SMI calls require buffer addresses be in 32-bit address space.
551          * This is done by setting the DMA mask below.
552          */
553         dcdbas_pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
554         dcdbas_pdev->dev.dma_mask = &dcdbas_pdev->dev.coherent_dma_mask;
555
556         error = sysfs_create_group(&dev->dev.kobj, &dcdbas_attr_group);
557         if (error)
558                 return error;
559
560         for (i = 0; dcdbas_bin_attrs[i]; i++) {
561                 error = sysfs_create_bin_file(&dev->dev.kobj,
562                                               dcdbas_bin_attrs[i]);
563                 if (error) {
564                         while (--i >= 0)
565                                 sysfs_remove_bin_file(&dev->dev.kobj,
566                                                       dcdbas_bin_attrs[i]);
567                         sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
568                         return error;
569                 }
570         }
571
572         register_reboot_notifier(&dcdbas_reboot_nb);
573
574         dev_info(&dev->dev, "%s (version %s)\n",
575                  DRIVER_DESCRIPTION, DRIVER_VERSION);
576
577         return 0;
578 }
579
580 static int __devexit dcdbas_remove(struct platform_device *dev)
581 {
582         int i;
583
584         unregister_reboot_notifier(&dcdbas_reboot_nb);
585         for (i = 0; dcdbas_bin_attrs[i]; i++)
586                 sysfs_remove_bin_file(&dev->dev.kobj, dcdbas_bin_attrs[i]);
587         sysfs_remove_group(&dev->dev.kobj, &dcdbas_attr_group);
588
589         return 0;
590 }
591
592 static struct platform_driver dcdbas_driver = {
593         .driver         = {
594                 .name   = DRIVER_NAME,
595                 .owner  = THIS_MODULE,
596         },
597         .probe          = dcdbas_probe,
598         .remove         = __devexit_p(dcdbas_remove),
599 };
600
601 /**
602  * dcdbas_init: initialize driver
603  */
604 static int __init dcdbas_init(void)
605 {
606         int error;
607
608         error = platform_driver_register(&dcdbas_driver);
609         if (error)
610                 return error;
611
612         dcdbas_pdev = platform_device_alloc(DRIVER_NAME, -1);
613         if (!dcdbas_pdev) {
614                 error = -ENOMEM;
615                 goto err_unregister_driver;
616         }
617
618         error = platform_device_add(dcdbas_pdev);
619         if (error)
620                 goto err_free_device;
621
622         return 0;
623
624  err_free_device:
625         platform_device_put(dcdbas_pdev);
626  err_unregister_driver:
627         platform_driver_unregister(&dcdbas_driver);
628         return error;
629 }
630
631 /**
632  * dcdbas_exit: perform driver cleanup
633  */
634 static void __exit dcdbas_exit(void)
635 {
636         /*
637          * make sure functions that use dcdbas_pdev are called
638          * before platform_device_unregister
639          */
640         unregister_reboot_notifier(&dcdbas_reboot_nb);
641         smi_data_buf_free();
642         platform_device_unregister(dcdbas_pdev);
643         platform_driver_unregister(&dcdbas_driver);
644
645         /*
646          * We have to free the buffer here instead of dcdbas_remove
647          * because only in module exit function we can be sure that
648          * all sysfs attributes belonging to this module have been
649          * released.
650          */
651         smi_data_buf_free();
652 }
653
654 module_init(dcdbas_init);
655 module_exit(dcdbas_exit);
656
657 MODULE_DESCRIPTION(DRIVER_DESCRIPTION " (version " DRIVER_VERSION ")");
658 MODULE_VERSION(DRIVER_VERSION);
659 MODULE_AUTHOR("Dell Inc.");
660 MODULE_LICENSE("GPL");
661 /* Any System or BIOS claiming to be by Dell */
662 MODULE_ALIAS("dmi:*:[bs]vnD[Ee][Ll][Ll]*:*");