Merge with 2.3.99-pre9.
[linux-2.6/linux-mips.git] / arch / i386 / kernel / acpi.c
blob2e220bc06ec702c36949e3f1eaa263481d589ec0
1 /*
2 * acpi.c - Linux ACPI driver
4 * Copyright (C) 1999-2000 Andrew Henroid
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * See http://www.geocities.com/SiliconValley/Hardware/3165/
23 * for the user-level ACPI stuff
26 #include <linux/config.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/miscdevice.h>
31 #include <linux/sched.h>
32 #include <linux/time.h>
33 #include <linux/wait.h>
34 #include <linux/spinlock.h>
35 #include <linux/ioport.h>
36 #include <linux/slab.h>
37 #include <linux/mm.h>
38 #include <linux/pci.h>
39 #include <asm/uaccess.h>
40 #include <asm/io.h>
41 #include <linux/sysctl.h>
42 #include <linux/delay.h>
43 #include <linux/pm.h>
44 #include <linux/acpi.h>
47 * Yes, it's unfortunate that we are relying on get_cmos_time
48 * because it is slow (> 1 sec.) and i386 only. It might be better
49 * to use some of the code from drivers/char/rtc.c in the near future
51 extern unsigned long get_cmos_time(void);
53 static int acpi_do_ulong(ctl_table *ctl,
54 int write,
55 struct file *file,
56 void *buffer,
57 size_t *len);
58 static int acpi_do_table(ctl_table *ctl,
59 int write,
60 struct file *file,
61 void *buffer,
62 size_t *len);
63 static int acpi_do_event_reg(ctl_table *ctl,
64 int write,
65 struct file *file,
66 void *buffer,
67 size_t *len);
68 static int acpi_do_event(ctl_table *ctl,
69 int write,
70 struct file *file,
71 void *buffer,
72 size_t *len);
73 static int acpi_do_sleep(ctl_table *ctl,
74 int write,
75 struct file *file,
76 void *buffer,
77 size_t *len);
79 static struct ctl_table_header *acpi_sysctl = NULL;
81 // current system sleep state (S0 - S4)
82 static acpi_sstate_t acpi_sleep_state = ACPI_S0;
83 // time sleep began
84 static unsigned long acpi_sleep_start = 0;
86 static spinlock_t acpi_event_lock = SPIN_LOCK_UNLOCKED;
87 static volatile u32 acpi_pm1_status = 0;
88 static volatile u32 acpi_gpe_status = 0;
89 static volatile u32 acpi_gpe_level = 0;
90 static volatile acpi_sstate_t acpi_event_state = ACPI_S0;
91 static DECLARE_WAIT_QUEUE_HEAD(acpi_event_wait);
93 /* Make it impossible to enter C2/C3 until after we've initialized */
94 static unsigned long acpi_enter_lvl2_lat = ACPI_INFINITE_LAT;
95 static unsigned long acpi_enter_lvl3_lat = ACPI_INFINITE_LAT;
96 static unsigned long acpi_p_lvl2_lat = ACPI_INFINITE_LAT;
97 static unsigned long acpi_p_lvl3_lat = ACPI_INFINITE_LAT;
99 static unsigned long acpi_p_blk = 0;
101 static int acpi_p_lvl2_tested = 0;
102 static int acpi_p_lvl3_tested = 0;
104 enum
106 ACPI_ENABLED = 0x00000000, // use ACPI if present
107 ACPI_DISABLED = 0x00000001, // never use ACPI
108 ACPI_TABLES_ONLY = 0x00000002, // never use chipset-specific driver
109 ACPI_CHIPSET_ONLY = 0x00000004, // always use chipset-specific driver
110 ACPI_IGNORE_ERRATA = 0x00000008, // ignore any listed platform errata
111 ACPI_COPY_TABLES = 0x00000010, // copy ACPI tables before use
112 ACPI_TRUST_TABLES = 0x00000020, // use tables even after ioremap fails
113 ACPI_SCI_DISABLED = 0x00000040, // never enable ACPI (info. only)
114 ACPI_C2_DISABLED = 0x00000080, // never enter C2
115 ACPI_C3_DISABLED = 0x00000100, // never enter C3
116 ACPI_S1_DISABLED = 0x00000200, // never enter S1
117 ACPI_S5_DISABLED = 0x00000400, // never enter S5
120 struct acpi_option_info
122 const char *name;
123 unsigned long value;
126 static struct acpi_option_info acpi_options[] =
128 {"on", ACPI_ENABLED},
129 {"off", ACPI_DISABLED},
130 {"tables", ACPI_TABLES_ONLY},
131 {"chipset", ACPI_CHIPSET_ONLY},
132 {"no-errata", ACPI_IGNORE_ERRATA},
133 {"copy-tables", ACPI_COPY_TABLES},
134 {"trust-tables", ACPI_TRUST_TABLES},
135 {"no-sci", ACPI_SCI_DISABLED},
136 {"no-c2", ACPI_C2_DISABLED},
137 {"no-c3", ACPI_C3_DISABLED},
138 {"no-s1", ACPI_S1_DISABLED},
139 {"no-s5", ACPI_S5_DISABLED},
140 {NULL, 0},
143 static unsigned long acpi_opts = ACPI_ENABLED;
145 struct acpi_errata_info
147 const char *signature; // table signature (eg. "RSDT")
148 const char *oem; // OEM name
149 const char *oem_table; // OEM table identifier (optional)
150 u32 oem_rev; // OEM table revision (optional)
151 unsigned long options; // errata options
155 * We must identify systems that need ACPI_TRUST_TABLES solely from the
156 * RSDP ("RSD PTR "). All other options should be flagged from the
157 * RSDT ("RSDT") which can be better identified.
159 struct acpi_errata_info acpi_errata[] =
161 {"RSD PTR ", "AMI ", NULL, 0, ACPI_TRUST_TABLES | ACPI_COPY_TABLES},
162 {NULL, NULL, 0, 0},
165 // bits 8-15 are SLP_TYPa, bits 0-7 are SLP_TYPb
166 static unsigned long acpi_slp_typ[] =
168 ACPI_SLP_TYP_DISABLED, /* S0 */
169 ACPI_SLP_TYP_DISABLED, /* S1 */
170 ACPI_SLP_TYP_DISABLED, /* S2 */
171 ACPI_SLP_TYP_DISABLED, /* S3 */
172 ACPI_SLP_TYP_DISABLED, /* S4 */
173 ACPI_SLP_TYP_DISABLED /* S5 */
176 struct acpi_table_info
178 u32 expected_signature;
179 u32 expected_size;
181 struct acpi_table *table;
182 size_t size;
183 int mapped;
186 static struct acpi_table_info acpi_facp
187 = {ACPI_FACP_SIG, sizeof(struct acpi_facp), NULL, 0, 0};
188 static struct acpi_table_info acpi_dsdt = {ACPI_DSDT_SIG, 0, NULL, 0, 0};
189 static struct acpi_table_info acpi_facs
190 = {ACPI_FACS_SIG, sizeof(struct acpi_facs), NULL, 0, 0};
191 static rwlock_t acpi_do_table_lock = RW_LOCK_UNLOCKED;
193 static struct ctl_table acpi_table[] =
195 {ACPI_FACP, "facp", &acpi_facp, 0, 0644, NULL, &acpi_do_table},
197 {ACPI_DSDT, "dsdt", &acpi_dsdt, 0, 0644, NULL, &acpi_do_table},
199 {ACPI_PM1_ENABLE, "pm1_enable",
200 NULL, 0,
201 0600, NULL, &acpi_do_event_reg},
203 {ACPI_GPE_ENABLE, "gpe_enable",
204 NULL, 0,
205 0600, NULL, &acpi_do_event_reg},
207 {ACPI_GPE_LEVEL, "gpe_level",
208 NULL, 0,
209 0600, NULL, &acpi_do_event_reg},
211 {ACPI_EVENT, "event", NULL, 0, 0400, NULL, &acpi_do_event},
213 {ACPI_P_BLK, "p_blk",
214 &acpi_p_blk, sizeof(acpi_p_blk),
215 0600, NULL, &acpi_do_ulong},
217 {ACPI_P_LVL2_LAT, "p_lvl2_lat",
218 &acpi_p_lvl2_lat, sizeof(acpi_p_lvl2_lat),
219 0644, NULL, &acpi_do_ulong},
221 {ACPI_P_LVL3_LAT, "p_lvl3_lat",
222 &acpi_p_lvl3_lat, sizeof(acpi_p_lvl3_lat),
223 0644, NULL, &acpi_do_ulong},
225 {ACPI_ENTER_LVL2_LAT, "enter_lvl2_lat",
226 &acpi_enter_lvl2_lat, sizeof(acpi_enter_lvl2_lat),
227 0644, NULL, &acpi_do_ulong},
229 {ACPI_ENTER_LVL3_LAT, "enter_lvl3_lat",
230 &acpi_enter_lvl3_lat, sizeof(acpi_enter_lvl3_lat),
231 0644, NULL, &acpi_do_ulong},
233 {ACPI_S0_SLP_TYP, "s0_slp_typ",
234 &acpi_slp_typ[ACPI_S0], sizeof(acpi_slp_typ[ACPI_S0]),
235 0600, NULL, &acpi_do_ulong},
237 {ACPI_S1_SLP_TYP, "s1_slp_typ",
238 &acpi_slp_typ[ACPI_S1], sizeof(acpi_slp_typ[ACPI_S1]),
239 0600, NULL, &acpi_do_ulong},
241 {ACPI_S5_SLP_TYP, "s5_slp_typ",
242 &acpi_slp_typ[ACPI_S5], sizeof(acpi_slp_typ[ACPI_S5]),
243 0600, NULL, &acpi_do_ulong},
245 {ACPI_SLEEP, "sleep", NULL, 0, 0600, NULL, &acpi_do_sleep},
250 static struct ctl_table acpi_dir_table[] =
252 {CTL_ACPI, "acpi", NULL, 0, 0555, acpi_table},
256 static u32 FASTCALL(acpi_read_pm1_control(struct acpi_facp *));
257 static u32 FASTCALL(acpi_read_pm1_status(struct acpi_facp *));
258 static u32 FASTCALL(acpi_read_pm1_enable(struct acpi_facp *));
259 static u32 FASTCALL(acpi_read_gpe_status(struct acpi_facp *));
260 static u32 FASTCALL(acpi_read_gpe_enable(struct acpi_facp *));
262 static void FASTCALL(acpi_write_pm1_control(struct acpi_facp *, u32));
263 static void FASTCALL(acpi_write_pm1_status(struct acpi_facp *, u32));
264 static void FASTCALL(acpi_write_pm1_enable(struct acpi_facp *, u32));
265 static void FASTCALL(acpi_write_gpe_status(struct acpi_facp *, u32));
266 static void FASTCALL(acpi_write_gpe_enable(struct acpi_facp *, u32));
269 * Get the value of the PM1 control register (SCI_EN, ...)
271 static u32 acpi_read_pm1_control(struct acpi_facp *facp)
273 u32 value = 0;
274 if (facp->pm1a_cnt)
275 value = inw(facp->pm1a_cnt);
276 if (facp->pm1b_cnt)
277 value |= inw(facp->pm1b_cnt);
278 return value;
282 * Set the value of the PM1 control register (BM_RLD, ...)
284 static void acpi_write_pm1_control(struct acpi_facp *facp, u32 value)
286 if (facp->pm1a_cnt)
287 outw(value, facp->pm1a_cnt);
288 if (facp->pm1b_cnt)
289 outw(value, facp->pm1b_cnt);
293 * Get the value of the fixed event status register
295 static u32 acpi_read_pm1_status(struct acpi_facp *facp)
297 u32 value = 0;
298 if (facp->pm1a_evt)
299 value = inw(facp->pm1a_evt);
300 if (facp->pm1b_evt)
301 value |= inw(facp->pm1b_evt);
302 return value;
306 * Set the value of the fixed event status register (clear events)
308 static void acpi_write_pm1_status(struct acpi_facp *facp, u32 value)
310 if (facp->pm1a_evt)
311 outw(value, facp->pm1a_evt);
312 if (facp->pm1b_evt)
313 outw(value, facp->pm1b_evt);
317 * Get the value of the fixed event enable register
319 static u32 acpi_read_pm1_enable(struct acpi_facp *facp)
321 int offset = facp->pm1_evt_len >> 1;
322 u32 value = 0;
323 if (facp->pm1a_evt)
324 value = inw(facp->pm1a_evt + offset);
325 if (facp->pm1b_evt)
326 value |= inw(facp->pm1b_evt + offset);
327 return value;
331 * Set the value of the fixed event enable register (enable events)
333 static void acpi_write_pm1_enable(struct acpi_facp *facp, u32 value)
335 int offset = facp->pm1_evt_len >> 1;
336 if (facp->pm1a_evt)
337 outw(value, facp->pm1a_evt + offset);
338 if (facp->pm1b_evt)
339 outw(value, facp->pm1b_evt + offset);
343 * Get the value of the general-purpose event status register
345 static u32 acpi_read_gpe_status(struct acpi_facp *facp)
347 u32 value = 0;
348 int i, size;
350 if (facp->gpe1) {
351 size = facp->gpe1_len >> 1;
352 for (i = size - 1; i >= 0; i--)
353 value = (value << 8) | inb(facp->gpe1 + i);
355 if (facp->gpe0) {
356 size = facp->gpe0_len >> 1;
357 for (i = size - 1; i >= 0; i--)
358 value = (value << 8) | inb(facp->gpe0 + i);
360 return value;
364 * Set the value of the general-purpose event status register (clear events)
366 static void acpi_write_gpe_status(struct acpi_facp *facp, u32 value)
368 int i, size;
370 if (facp->gpe0) {
371 size = facp->gpe0_len >> 1;
372 for (i = 0; i < size; i++) {
373 outb(value & 0xff, facp->gpe0 + i);
374 value >>= 8;
377 if (facp->gpe1) {
378 size = facp->gpe1_len >> 1;
379 for (i = 0; i < size; i++) {
380 outb(value & 0xff, facp->gpe1 + i);
381 value >>= 8;
387 * Get the value of the general-purpose event enable register
389 static u32 acpi_read_gpe_enable(struct acpi_facp *facp)
391 u32 value = 0;
392 int i, size, offset;
394 offset = facp->gpe0_len >> 1;
395 if (facp->gpe1) {
396 size = facp->gpe1_len >> 1;
397 for (i = size - 1; i >= 0; i--) {
398 value = (value << 8) | inb(facp->gpe1 + offset + i);
401 if (facp->gpe0) {
402 size = facp->gpe0_len >> 1;
403 for (i = size - 1; i >= 0; i--)
404 value = (value << 8) | inb(facp->gpe0 + offset + i);
406 return value;
410 * Set the value of the general-purpose event enable register (enable events)
412 static void acpi_write_gpe_enable(struct acpi_facp *facp, u32 value)
414 int i, offset;
416 offset = facp->gpe0_len >> 1;
417 if (facp->gpe0) {
418 for (i = 0; i < offset; i++) {
419 outb(value & 0xff, facp->gpe0 + offset + i);
420 value >>= 8;
423 if (facp->gpe1) {
424 offset = facp->gpe1_len >> 1;
425 for (i = 0; i < offset; i++) {
426 outb(value & 0xff, facp->gpe1 + offset + i);
427 value >>= 8;
433 * Map an ACPI table into virtual memory
435 static struct acpi_table *__init acpi_map_table(u32 addr)
437 struct acpi_table *table = NULL;
438 if (addr) {
439 // map table header to determine size
440 table = (struct acpi_table *)
441 ioremap((unsigned long) addr,
442 sizeof(struct acpi_table));
443 if (table) {
444 unsigned long table_size = table->length;
445 iounmap(table);
446 // remap entire table
447 table = (struct acpi_table *)
448 ioremap((unsigned long) addr, table_size);
451 if (!table && addr < virt_to_phys(high_memory)) {
452 /* sometimes we see ACPI tables in low memory
453 * and not reserved by the memory map (E820) code,
454 * who is at fault for this? BIOS?
456 printk(KERN_ERR
457 "ACPI: unreserved table memory @ 0x%p!\n",
458 (void*) addr);
460 if (acpi_opts & ACPI_TRUST_TABLES) {
461 /* OK, trust that the table is there
462 * if it isn't you'll get an OOPS here
464 static u32 sig;
465 table = (struct acpi_table *)
466 phys_to_virt(addr);
467 sig = table->signature;
471 return table;
475 * Unmap an ACPI table from virtual memory
477 static void acpi_unmap_table(struct acpi_table *table)
479 // iounmap ignores addresses within physical memory
480 if (table)
481 iounmap(table);
485 * Initialize an ACPI table
487 static int acpi_init_table(struct acpi_table_info *info,
488 void *data,
489 int mapped)
491 struct acpi_table *table = (struct acpi_table*) data;
493 info->table = NULL;
494 info->size = 0;
495 info->mapped = 0;
497 if (!table || table->signature != info->expected_signature)
498 return -EINVAL;
500 if (mapped && (acpi_opts & ACPI_COPY_TABLES)) {
501 struct acpi_table *copy
502 = kmalloc(table->length, GFP_KERNEL);
503 if (!copy)
504 return -ENOMEM;
505 memcpy(copy, table, table->length);
506 table = copy;
507 mapped = 0;
510 info->table = table;
511 info->size = (size_t) table->length;
512 info->mapped = mapped;
513 return 0;
517 * Destroy an ACPI table
519 static void acpi_destroy_table(struct acpi_table_info *info)
521 if (info->table) {
522 if (info->mapped)
523 acpi_unmap_table(info->table);
524 else
525 kfree(info->table);
526 info->table = NULL;
531 * Match ACPI table and set options based on platform errata, if any
533 static int __init acpi_find_errata(struct acpi_table *table)
535 struct acpi_errata_info *info;
536 int size;
538 for (info = acpi_errata; info->signature && info->oem; info++) {
539 size = strlen(info->signature);
540 if (memcmp(&table->signature, info->signature, size))
541 continue;
542 if (strcmp(info->signature, "RSD PTR ")) {
543 // ordinary ACPI table
544 size = strlen(info->oem);
545 if (memcmp(table->oem, info->oem, size))
546 continue;
547 if (info->oem_table) {
548 size = strlen(info->oem_table);
549 if (memcmp(table->oem_table,
550 info->oem_table,
551 size))
552 continue;
554 if (info->oem_rev && table->oem_rev != info->oem_rev)
555 continue;
557 else {
558 // special handling for RSDP
559 size = strlen(info->oem);
560 if (memcmp(((struct acpi_rsdp*) table)->oem,
561 info->oem,
562 size))
563 continue;
566 printk(KERN_INFO
567 "ACPI: found platform errata 0x%08lx\n",
568 info->options);
569 acpi_opts |= info->options;
570 return 0;
572 return -1;
576 * Locate and map ACPI tables
578 static int __init acpi_find_tables(void)
580 struct acpi_rsdp *rsdp;
581 struct acpi_table *rsdt;
582 u32 *rsdt_entry;
583 int rsdt_entry_count;
584 unsigned long i;
586 // search BIOS memory for RSDP
587 for (i = ACPI_BIOS_ROM_BASE; i < ACPI_BIOS_ROM_END; i += 16) {
588 rsdp = (struct acpi_rsdp *) phys_to_virt(i);
589 if (rsdp->signature[0] == ACPI_RSDP1_SIG
590 && rsdp->signature[1] == ACPI_RSDP2_SIG) {
591 char oem[7];
592 int j;
594 // strip trailing space and print OEM identifier
595 memcpy(oem, rsdp->oem, 6);
596 oem[6] = '\0';
597 for (j = 5;
598 j > 0 && (oem[j] == '\0' || oem[j] == ' ');
599 j--) {
600 oem[j] = '\0';
602 printk(KERN_INFO "ACPI: \"%s\" found at 0x%p\n",
603 oem, (void *) i);
605 break;
608 if (i >= ACPI_BIOS_ROM_END)
609 return -ENODEV;
611 // find any errata based on the RSDP
612 if (!acpi_find_errata((struct acpi_table*) rsdp)) {
613 if (acpi_opts & ACPI_DISABLED)
614 return -EINVAL;
615 else if (acpi_opts & ACPI_CHIPSET_ONLY)
616 return -ENODEV;
619 // fetch RSDT from RSDP
620 rsdt = acpi_map_table(rsdp->rsdt);
621 if (!rsdt) {
622 printk(KERN_ERR "ACPI: missing RSDT at 0x%p\n",
623 (void*) rsdp->rsdt);
624 return -EINVAL;
626 else if (rsdt->signature != ACPI_RSDT_SIG) {
627 printk(KERN_ERR "ACPI: bad RSDT at 0x%p (%08x)\n",
628 (void*) rsdp->rsdt, (unsigned) rsdt->signature);
629 acpi_unmap_table(rsdt);
630 return -EINVAL;
633 // find any errata based on the RSDT
634 if (!acpi_find_errata(rsdt)) {
635 if (acpi_opts & ACPI_DISABLED)
636 return -EINVAL;
637 else if (acpi_opts & ACPI_CHIPSET_ONLY)
638 return -ENODEV;
641 // search RSDT for FACP
642 acpi_facp.table = NULL;
643 rsdt_entry = (u32 *) (rsdt + 1);
644 rsdt_entry_count = (int) ((rsdt->length - sizeof(*rsdt)) >> 2);
645 while (rsdt_entry_count) {
646 struct acpi_table *dt = acpi_map_table(*rsdt_entry);
647 if (!acpi_init_table(&acpi_facp, dt, 1)) {
648 struct acpi_facp *facp
649 = (struct acpi_facp*) acpi_facp.table;
651 // map DSDT if it exists
652 dt = acpi_map_table(facp->dsdt);
653 if (acpi_init_table(&acpi_dsdt, dt, 1))
654 acpi_unmap_table(dt);
656 break;
658 else {
659 acpi_unmap_table(dt);
662 rsdt_entry++;
663 rsdt_entry_count--;
666 acpi_unmap_table(rsdt);
668 if (!acpi_facp.table) {
669 printk(KERN_ERR "ACPI: missing FACP\n");
670 return -EINVAL;
672 return 0;
676 * Unmap or destroy ACPI tables
678 static void acpi_destroy_tables(void)
680 acpi_destroy_table(&acpi_facs);
681 acpi_destroy_table(&acpi_dsdt);
682 acpi_destroy_table(&acpi_facp);
686 * Init PIIX4 device, create a fake FACP
688 static int __init acpi_init_piix4(struct pci_dev *dev)
690 struct acpi_facp *facp;
691 u32 base;
692 u16 cmd;
693 u8 pmregmisc;
695 pci_read_config_word(dev, PCI_COMMAND, &cmd);
696 if (!(cmd & PCI_COMMAND_IO))
697 return -ENODEV;
699 pci_read_config_byte(dev, ACPI_PIIX4_PMREGMISC, &pmregmisc);
700 if (!(pmregmisc & ACPI_PIIX4_PMIOSE))
701 return -ENODEV;
703 base = dev->resource[PCI_BRIDGE_RESOURCES].start & PCI_BASE_ADDRESS_IO_MASK;
704 if (!base)
705 return -ENODEV;
707 printk(KERN_INFO "ACPI: found PIIX4 at 0x%04x\n", base);
709 facp = kmalloc(sizeof(struct acpi_facp), GFP_KERNEL);
710 if (!facp)
711 return -ENOMEM;
713 memset(facp, 0, sizeof(struct acpi_facp));
714 facp->hdr.signature = ACPI_FACP_SIG;
715 facp->hdr.length = sizeof(struct acpi_facp);
716 facp->int_model = ACPI_PIIX4_INT_MODEL;
717 facp->sci_int = ACPI_PIIX4_SCI_INT;
718 facp->smi_cmd = ACPI_PIIX4_SMI_CMD;
719 facp->acpi_enable = ACPI_PIIX4_ACPI_ENABLE;
720 facp->acpi_disable = ACPI_PIIX4_ACPI_DISABLE;
721 facp->s4bios_req = ACPI_PIIX4_S4BIOS_REQ;
722 facp->pm1a_evt = base + ACPI_PIIX4_PM1_EVT;
723 facp->pm1a_cnt = base + ACPI_PIIX4_PM1_CNT;
724 facp->pm2_cnt = ACPI_PIIX4_PM2_CNT;
725 facp->pm_tmr = base + ACPI_PIIX4_PM_TMR;
726 facp->gpe0 = base + ACPI_PIIX4_GPE0;
727 facp->pm1_evt_len = ACPI_PIIX4_PM1_EVT_LEN;
728 facp->pm1_cnt_len = ACPI_PIIX4_PM1_CNT_LEN;
729 facp->pm2_cnt_len = ACPI_PIIX4_PM2_CNT_LEN;
730 facp->pm_tm_len = ACPI_PIIX4_PM_TM_LEN;
731 facp->gpe0_len = ACPI_PIIX4_GPE0_LEN;
732 facp->p_lvl2_lat = (__u16) ACPI_INFINITE_LAT;
733 facp->p_lvl3_lat = (__u16) ACPI_INFINITE_LAT;
735 acpi_init_table(&acpi_facp, facp, 0);
736 acpi_init_table(&acpi_dsdt, NULL, 0);
738 acpi_p_blk = base + ACPI_PIIX4_P_BLK;
740 return 0;
744 * Init VIA ACPI device and create a fake FACP
746 static int __init acpi_init_via(struct pci_dev *dev)
748 struct acpi_facp *facp;
749 u32 base;
750 u8 tmp, irq;
752 pci_read_config_byte(dev, 0x41, &tmp);
753 if (!(tmp & 0x80))
754 return -ENODEV;
756 base = pci_resource_start(dev, PCI_BRIDGE_RESOURCES);
757 if (!base) {
758 base = pci_resource_start(dev, PCI_BASE_ADDRESS_4);
759 if (!base)
760 return -ENODEV;
762 base &= PCI_BASE_ADDRESS_IO_MASK;
764 pci_read_config_byte(dev, 0x42, &irq);
766 printk(KERN_INFO "ACPI: found %s at 0x%04x\n", dev->name, base);
768 facp = kmalloc(sizeof(struct acpi_facp), GFP_KERNEL);
769 if (!facp)
770 return -ENOMEM;
772 memset(facp, 0, sizeof(struct acpi_facp));
773 facp->hdr.signature = ACPI_FACP_SIG;
774 facp->hdr.length = sizeof(struct acpi_facp);
775 facp->int_model = ACPI_VIA_INT_MODEL;
776 facp->sci_int = irq;
777 facp->smi_cmd = base + ACPI_VIA_SMI_CMD;
778 facp->acpi_enable = ACPI_VIA_ACPI_ENABLE;
779 facp->acpi_disable = ACPI_VIA_ACPI_DISABLE;
780 facp->pm1a_evt = base + ACPI_VIA_PM1_EVT;
781 facp->pm1a_cnt = base + ACPI_VIA_PM1_CNT;
782 facp->pm_tmr = base + ACPI_VIA_PM_TMR;
783 facp->gpe0 = base + ACPI_VIA_GPE0;
785 facp->pm1_evt_len = ACPI_VIA_PM1_EVT_LEN;
786 facp->pm1_cnt_len = ACPI_VIA_PM1_CNT_LEN;
787 facp->pm_tm_len = ACPI_VIA_PM_TM_LEN;
788 facp->gpe0_len = ACPI_VIA_GPE0_LEN;
789 facp->p_lvl2_lat = (__u16) ACPI_INFINITE_LAT;
790 facp->p_lvl3_lat = (__u16) ACPI_INFINITE_LAT;
792 facp->duty_offset = ACPI_VIA_DUTY_OFFSET;
793 facp->duty_width = ACPI_VIA_DUTY_WIDTH;
795 facp->day_alarm = ACPI_VIA_DAY_ALARM;
796 facp->mon_alarm = ACPI_VIA_MON_ALARM;
797 facp->century = ACPI_VIA_CENTURY;
799 acpi_init_table(&acpi_facp, facp, 0);
800 acpi_init_table(&acpi_dsdt, NULL, 0);
802 acpi_p_blk = base + ACPI_VIA_P_BLK;
804 return 0;
807 typedef enum
809 CH_UNKNOWN = 0,
810 CH_INTEL_PIIX4,
811 CH_VIA_586,
812 CH_VIA_686A,
813 } acpi_chip_t;
815 /* indexed by value of each enum in acpi_chip_t */
816 const static struct
818 int (*chip_init)(struct pci_dev *dev);
819 } acpi_chip_info[] =
821 {NULL,},
822 {acpi_init_piix4},
823 {acpi_init_via},
824 {acpi_init_via},
827 const static struct pci_device_id acpi_pci_tbl[] =
829 {0x8086, 0x7113, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_INTEL_PIIX4},
830 {0x1106, 0x3040, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_VIA_586},
831 {0x1106, 0x3057, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_VIA_686A},
832 {0,} /* terminate list */
835 static int __init acpi_probe(struct pci_dev *dev,
836 const struct pci_device_id *id)
838 return acpi_chip_info[id->driver_data].chip_init(dev);
841 static struct pci_driver acpi_driver =
843 name: "acpi",
844 id_table: acpi_pci_tbl,
845 probe: acpi_probe,
847 static int pci_driver_registered = 0;
850 * Locate a known ACPI chipset
852 static int __init acpi_find_chipset(void)
854 if (pci_register_driver(&acpi_driver) < 1)
855 return -ENODEV;
857 pci_driver_registered = 1;
859 return 0;
863 * Handle an ACPI SCI (fixed or general purpose event)
865 static void acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
867 struct acpi_facp *facp = (struct acpi_facp*) acpi_facp.table;
868 u32 pm1_status, gpe_status, gpe_level, gpe_edge;
869 unsigned long flags;
871 // detect and clear fixed events
872 pm1_status = (acpi_read_pm1_status(facp) & acpi_read_pm1_enable(facp));
873 acpi_write_pm1_status(facp, pm1_status);
875 // detect and handle general-purpose events
876 gpe_status = (acpi_read_gpe_status(facp) & acpi_read_gpe_enable(facp));
877 gpe_level = gpe_status & acpi_gpe_level;
878 if (gpe_level) {
879 // disable level-triggered events (re-enabled after handling)
880 acpi_write_gpe_enable(facp,
881 acpi_read_gpe_enable(facp) & ~gpe_level);
883 gpe_edge = gpe_status & ~gpe_level;
884 if (gpe_edge) {
885 // clear edge-triggered events
886 while (acpi_read_gpe_status(facp) & gpe_edge)
887 acpi_write_gpe_status(facp, gpe_edge);
890 // notify process waiting on /dev/acpi
891 spin_lock_irqsave(&acpi_event_lock, flags);
892 acpi_pm1_status |= pm1_status;
893 acpi_gpe_status |= gpe_status;
894 spin_unlock_irqrestore(&acpi_event_lock, flags);
895 acpi_event_state = acpi_sleep_state;
896 wake_up_interruptible(&acpi_event_wait);
900 * Is SCI to be enabled?
902 static inline int
903 acpi_sci_enabled(void)
905 return !(acpi_opts & ACPI_SCI_DISABLED);
909 * Is ACPI enabled or not?
911 static inline int acpi_is_enabled(struct acpi_facp *facp)
913 return ((acpi_read_pm1_control(facp) & ACPI_SCI_EN) ? 1:0);
917 * Enable SCI
919 static int acpi_enable(struct acpi_facp *facp)
921 if (facp->smi_cmd && acpi_sci_enabled())
922 outb(facp->acpi_enable, facp->smi_cmd);
923 return (acpi_is_enabled(facp) ? 0:-1);
927 * Disable SCI
929 static int acpi_disable(struct acpi_facp *facp)
931 if (facp->smi_cmd && acpi_sci_enabled()) {
932 // disable and clear any pending events
933 acpi_write_gpe_enable(facp, 0);
934 while (acpi_read_gpe_status(facp)) {
935 acpi_write_gpe_status(facp,
936 acpi_read_gpe_status(facp));
938 acpi_write_pm1_enable(facp, 0);
939 acpi_write_pm1_status(facp, acpi_read_pm1_status(facp));
941 /* writing acpi_disable to smi_cmd would be appropriate
942 * here but this causes a nasty crash on many systems
946 return 0;
949 static inline int bm_activity(struct acpi_facp *facp)
951 return acpi_read_pm1_status(facp) & ACPI_BM;
954 static inline void clear_bm_activity(struct acpi_facp *facp)
956 acpi_write_pm1_status(facp, ACPI_BM);
959 static void sleep_on_busmaster(struct acpi_facp *facp)
961 u32 pm1_cntr = acpi_read_pm1_control(facp);
962 if (pm1_cntr & ACPI_BM_RLD) {
963 pm1_cntr &= ~ACPI_BM_RLD;
964 acpi_write_pm1_control(facp, pm1_cntr);
968 static void wake_on_busmaster(struct acpi_facp *facp)
970 u32 pm1_cntr = acpi_read_pm1_control(facp);
971 if (!(pm1_cntr & ACPI_BM_RLD)) {
972 pm1_cntr |= ACPI_BM_RLD;
973 acpi_write_pm1_control(facp, pm1_cntr);
975 clear_bm_activity(facp);
978 /* The ACPI timer is just the low 24 bits */
979 #define TIME_BEGIN(tmr) inl(tmr)
980 #define TIME_END(tmr, begin) ((inl(tmr) - (begin)) & 0x00ffffff)
984 * Idle loop (uniprocessor only)
986 static void acpi_idle(void)
988 static int sleep_level = 1;
989 struct acpi_facp *facp = (struct acpi_facp*) acpi_facp.table;
991 if (!facp || !facp->pm_tmr || !acpi_p_blk)
992 goto not_initialized;
995 * start from the previous sleep level..
997 if (sleep_level == 1)
998 goto sleep1;
999 if (sleep_level == 2)
1000 goto sleep2;
1001 sleep3:
1002 sleep_level = 3;
1003 if (!acpi_p_lvl3_tested) {
1004 printk(KERN_INFO "ACPI C3 works\n");
1005 acpi_p_lvl3_tested = 1;
1007 wake_on_busmaster(facp);
1008 if (facp->pm2_cnt)
1009 goto sleep3_with_arbiter;
1011 for (;;) {
1012 unsigned long time;
1013 unsigned int pm_tmr = facp->pm_tmr;
1015 __cli();
1016 if (current->need_resched)
1017 goto out;
1018 if (bm_activity(facp))
1019 goto sleep2;
1021 time = TIME_BEGIN(pm_tmr);
1022 inb(acpi_p_blk + ACPI_P_LVL3);
1023 inl(pm_tmr); /* Dummy read, force synchronization with the PMU */
1024 time = TIME_END(pm_tmr, time);
1026 __sti();
1027 if (time < acpi_p_lvl3_lat)
1028 goto sleep2;
1031 sleep3_with_arbiter:
1032 for (;;) {
1033 unsigned long time;
1034 u8 arbiter;
1035 unsigned int pm2_cntr = facp->pm2_cnt;
1036 unsigned int pm_tmr = facp->pm_tmr;
1038 __cli();
1039 if (current->need_resched)
1040 goto out;
1041 if (bm_activity(facp))
1042 goto sleep2;
1044 time = TIME_BEGIN(pm_tmr);
1045 arbiter = inb(pm2_cntr) & ~ACPI_ARB_DIS;
1046 outb(arbiter | ACPI_ARB_DIS, pm2_cntr); /* Disable arbiter, park on CPU */
1047 inb(acpi_p_blk + ACPI_P_LVL3);
1048 inl(pm_tmr); /* Dummy read, force synchronization with the PMU */
1049 time = TIME_END(pm_tmr, time);
1050 outb(arbiter, pm2_cntr); /* Enable arbiter again.. */
1052 __sti();
1053 if (time < acpi_p_lvl3_lat)
1054 goto sleep2;
1057 sleep2:
1058 sleep_level = 2;
1059 if (!acpi_p_lvl2_tested) {
1060 printk(KERN_INFO "ACPI C2 works\n");
1061 acpi_p_lvl2_tested = 1;
1063 wake_on_busmaster(facp); /* Required to track BM activity.. */
1064 for (;;) {
1065 unsigned long time;
1066 unsigned int pm_tmr = facp->pm_tmr;
1068 __cli();
1069 if (current->need_resched)
1070 goto out;
1072 time = TIME_BEGIN(pm_tmr);
1073 inb(acpi_p_blk + ACPI_P_LVL2);
1074 inl(pm_tmr); /* Dummy read, force synchronization with the PMU */
1075 time = TIME_END(pm_tmr, time);
1077 __sti();
1078 if (time < acpi_p_lvl2_lat)
1079 goto sleep1;
1080 if (bm_activity(facp)) {
1081 clear_bm_activity(facp);
1082 continue;
1084 if (time > acpi_enter_lvl3_lat)
1085 goto sleep3;
1088 sleep1:
1089 sleep_level = 1;
1090 sleep_on_busmaster(facp);
1091 for (;;) {
1092 unsigned long time;
1093 unsigned int pm_tmr = facp->pm_tmr;
1095 __cli();
1096 if (current->need_resched)
1097 goto out;
1098 time = TIME_BEGIN(pm_tmr);
1099 __asm__ __volatile__("sti ; hlt": : :"memory");
1100 time = TIME_END(pm_tmr, time);
1101 if (time > acpi_enter_lvl2_lat)
1102 goto sleep2;
1105 not_initialized:
1106 for (;;) {
1107 __cli();
1108 if (current->need_resched)
1109 goto out;
1110 __asm__ __volatile__("sti ; hlt": : :"memory");
1113 out:
1114 __sti();
1118 * Put all devices into specified D-state
1120 static int acpi_enter_dx(acpi_dstate_t state)
1122 int status = 0;
1124 if (state == ACPI_D0)
1125 status = pm_send_all(PM_RESUME, (void*) state);
1126 else
1127 status = pm_send_all(PM_SUSPEND, (void*) state);
1129 return status;
1133 * Update system time from real-time clock
1135 static void acpi_update_clock(void)
1137 if (acpi_sleep_start) {
1138 unsigned long delta;
1139 struct timeval tv;
1141 delta = get_cmos_time() - acpi_sleep_start;
1142 do_gettimeofday(&tv);
1143 tv.tv_sec += delta;
1144 do_settimeofday(&tv);
1146 acpi_sleep_start = 0;
1151 * Enter system sleep state
1153 static int acpi_enter_sx(acpi_sstate_t state)
1155 unsigned long slp_typ;
1156 u16 typa, typb, value;
1157 struct acpi_facp *facp;
1159 slp_typ = acpi_slp_typ[(int) state];
1160 if (slp_typ == ACPI_SLP_TYP_DISABLED)
1161 return -EPERM;
1163 // bits 8-15 are SLP_TYPa, bits 0-7 are SLP_TYPb
1164 typa = (slp_typ >> 8) & 0xff;
1165 typb = slp_typ & 0xff;
1167 typa = ((typa << ACPI_SLP_TYP_SHIFT) & ACPI_SLP_TYP_MASK);
1168 typb = ((typb << ACPI_SLP_TYP_SHIFT) & ACPI_SLP_TYP_MASK);
1170 acpi_sleep_start = get_cmos_time();
1171 acpi_enter_dx(ACPI_D3);
1172 acpi_sleep_state = state;
1174 facp = (struct acpi_facp*) acpi_facp.table;
1176 // clear wake status
1177 acpi_write_pm1_status(facp, ACPI_WAK);
1179 // set SLP_TYPa/b and SLP_EN
1180 if (facp->pm1a_cnt) {
1181 value = inw(facp->pm1a_cnt) & ~ACPI_SLP_TYP_MASK;
1182 outw(value | typa | ACPI_SLP_EN, facp->pm1a_cnt);
1184 if (facp->pm1b_cnt) {
1185 value = inw(facp->pm1b_cnt) & ~ACPI_SLP_TYP_MASK;
1186 outw(value | typb | ACPI_SLP_EN, facp->pm1b_cnt);
1189 // wait until S1 is entered
1190 while (!(acpi_read_pm1_status(facp) & ACPI_WAK)) ;
1191 // finished sleeping, update system time
1192 acpi_update_clock();
1193 acpi_enter_dx(ACPI_D0);
1194 acpi_sleep_state = ACPI_S0;
1196 return 0;
1200 * Enter soft-off (S5)
1202 static void acpi_power_off(void)
1204 acpi_enter_sx(ACPI_S5);
1208 * Claim I/O port if available
1210 static int acpi_claim(unsigned long start, unsigned long size)
1212 if (start && size) {
1213 if (check_region(start, size))
1214 return -EBUSY;
1215 request_region(start, size, "acpi");
1217 return 0;
1221 * Claim ACPI I/O ports
1223 static int acpi_claim_ioports(struct acpi_facp *facp)
1225 // we don't get a guarantee of contiguity for any of the ACPI registers
1226 if (acpi_claim(facp->pm1a_evt, facp->pm1_evt_len)
1227 || acpi_claim(facp->pm1b_evt, facp->pm1_evt_len)
1228 || acpi_claim(facp->pm1a_cnt, facp->pm1_cnt_len)
1229 || acpi_claim(facp->pm1b_cnt, facp->pm1_cnt_len)
1230 || acpi_claim(facp->pm_tmr, facp->pm_tm_len)
1231 || acpi_claim(facp->gpe0, facp->gpe0_len)
1232 || acpi_claim(facp->gpe1, facp->gpe1_len))
1233 return -EBUSY;
1234 return 0;
1238 * Release I/O port if claimed
1240 static void acpi_release(unsigned long start, unsigned long size)
1242 if (start && size)
1243 release_region(start, size);
1247 * Free ACPI I/O ports
1249 static int acpi_release_ioports(struct acpi_facp *facp)
1251 // we don't get a guarantee of contiguity for any of the ACPI registers
1252 acpi_release(facp->gpe1, facp->gpe1_len);
1253 acpi_release(facp->gpe0, facp->gpe0_len);
1254 acpi_release(facp->pm_tmr, facp->pm_tm_len);
1255 acpi_release(facp->pm1b_cnt, facp->pm1_cnt_len);
1256 acpi_release(facp->pm1a_cnt, facp->pm1_cnt_len);
1257 acpi_release(facp->pm1b_evt, facp->pm1_evt_len);
1258 acpi_release(facp->pm1a_evt, facp->pm1_evt_len);
1259 return 0;
1263 * Determine if modification of value is permitted
1265 static int
1266 acpi_verify_mod(int ctl_name)
1268 switch (ctl_name) {
1269 case ACPI_PM1_ENABLE:
1270 case ACPI_GPE_ENABLE:
1271 case ACPI_GPE_LEVEL:
1272 if (!acpi_sci_enabled())
1273 return -EPERM;
1274 break;
1275 case ACPI_P_LVL2_LAT:
1276 case ACPI_ENTER_LVL2_LAT:
1277 if (acpi_opts & ACPI_C2_DISABLED)
1278 return -EPERM;
1279 break;
1280 case ACPI_P_LVL3_LAT:
1281 case ACPI_ENTER_LVL3_LAT:
1282 if (acpi_opts & ACPI_C3_DISABLED)
1283 return -EPERM;
1284 break;
1285 case ACPI_S1_SLP_TYP:
1286 case ACPI_SLEEP:
1287 if (acpi_opts & ACPI_S1_DISABLED)
1288 return -EPERM;
1289 break;
1290 case ACPI_S5_SLP_TYP:
1291 if (acpi_opts & ACPI_S5_DISABLED)
1292 return -EPERM;
1293 break;
1295 return 0;
1299 * Examine/modify value
1301 static int acpi_do_ulong(ctl_table *ctl,
1302 int write,
1303 struct file *file,
1304 void *buffer,
1305 size_t *len)
1307 char str[2 * sizeof(unsigned long) + 4], *strend;
1308 unsigned long val;
1309 int size;
1311 if (!write) {
1312 if (file->f_pos) {
1313 *len = 0;
1314 return 0;
1317 val = *(unsigned long*) ctl->data;
1318 size = sprintf(str, "0x%08lx\n", val);
1319 if (*len >= size) {
1320 copy_to_user(buffer, str, size);
1321 *len = size;
1323 else
1324 *len = 0;
1326 else {
1327 if (acpi_verify_mod(ctl->ctl_name))
1328 return -EPERM;
1330 size = sizeof(str) - 1;
1331 if (size > *len)
1332 size = *len;
1333 copy_from_user(str, buffer, size);
1334 str[size] = '\0';
1335 val = simple_strtoul(str, &strend, 0);
1336 if (strend == str)
1337 return -EINVAL;
1338 *(unsigned long*) ctl->data = val;
1341 file->f_pos += *len;
1342 return 0;
1346 * Determine if user buffer contains a valid table
1348 static int acpi_verify_table(void *buffer,
1349 size_t size,
1350 struct acpi_table_info *info)
1352 if (size < sizeof(struct acpi_table))
1353 return -EINVAL;
1354 else if (verify_area(VERIFY_READ, buffer, size))
1355 return -EFAULT;
1356 else {
1357 struct acpi_table hdr;
1358 size_t table_size;
1360 copy_from_user(&hdr, buffer, sizeof(hdr));
1361 table_size = (size_t) hdr.length;
1362 if (hdr.signature != info->expected_signature
1363 || table_size < size
1364 || (info->expected_size
1365 && table_size != info->expected_size))
1366 return -EINVAL;
1368 return 0;
1372 * Examine/replace an ACPI table
1374 static int acpi_do_table(ctl_table *ctl,
1375 int write,
1376 struct file *file,
1377 void *buffer,
1378 size_t *len)
1380 struct acpi_table_info *info = (struct acpi_table_info *) ctl->data;
1381 u8 *data = NULL;
1382 size_t size = 0;
1383 int error = 0;
1385 if (!info) {
1386 *len = 0;
1387 return 0;
1390 if (!write) {
1391 // table read
1392 read_lock(&acpi_do_table_lock);
1393 if (info->table && file->f_pos < info->size) {
1394 data = (u8*) info->table + file->f_pos;
1395 size = info->size - file->f_pos;
1396 if (size > *len)
1397 size = *len;
1398 if (copy_to_user(buffer, data, size))
1399 error = -EFAULT;
1401 read_unlock(&acpi_do_table_lock);
1403 else if (file->f_pos) {
1404 // table body replacement
1405 write_lock(&acpi_do_table_lock);
1406 if (info->table && file->f_pos < info->size) {
1407 data = (u8*) info->table + file->f_pos;
1408 size = info->size - file->f_pos;
1409 if (size > *len)
1410 size = *len;
1411 if (copy_from_user(data, buffer, size))
1412 error = -EFAULT;
1414 write_unlock(&acpi_do_table_lock);
1416 else {
1417 // table header/body replacement
1418 struct acpi_table hdr;
1419 size_t table_size;
1421 // make sure we are being given a valid table
1422 error = acpi_verify_table(buffer, *len, info);
1423 if (error)
1424 return error;
1425 copy_from_user(&hdr, buffer, sizeof(hdr));
1426 table_size = (size_t) hdr.length;
1428 write_lock(&acpi_do_table_lock);
1430 data = (u8*) info->table;
1431 size = *len;
1433 if (!data || info->mapped || table_size != info->size) {
1434 // allocate a (different sized) table
1435 data = kmalloc(table_size, GFP_KERNEL);
1436 if (data) {
1437 memset(data, 0, table_size);
1438 memcpy(data, &hdr, sizeof(hdr));
1439 acpi_destroy_table(info);
1440 acpi_init_table(info, data, 0);
1442 else
1443 error = -ENOMEM;
1445 if (data)
1446 copy_from_user(data, buffer, size);
1448 write_unlock(&acpi_do_table_lock);
1451 if (error)
1452 return error;
1454 *len = size;
1455 file->f_pos += size;
1456 return 0;
1460 * Examine/modify event register
1462 static int acpi_do_event_reg(ctl_table *ctl,
1463 int write,
1464 struct file *file,
1465 void *buffer,
1466 size_t *len)
1468 struct acpi_facp *facp = (struct acpi_facp*) acpi_facp.table;
1469 char str[2 * sizeof(u32) + 4], *strend;
1470 u32 val, enabling;
1471 int size;
1473 if (!write) {
1474 if (file->f_pos) {
1475 *len = 0;
1476 return 0;
1479 val = 0;
1480 switch (ctl->ctl_name) {
1481 case ACPI_PM1_ENABLE:
1482 val = acpi_read_pm1_enable(facp);
1483 break;
1484 case ACPI_GPE_ENABLE:
1485 val = acpi_read_gpe_enable(facp);
1486 break;
1487 case ACPI_GPE_LEVEL:
1488 val = acpi_gpe_level;
1489 break;
1492 size = sprintf(str, "0x%08x\n", val);
1493 if (*len >= size) {
1494 copy_to_user(buffer, str, size);
1495 *len = size;
1497 else
1498 *len = 0;
1500 else
1502 if (acpi_verify_mod(ctl->ctl_name))
1503 return -EPERM;
1505 // fetch user value
1506 size = sizeof(str) - 1;
1507 if (size > *len)
1508 size = *len;
1509 copy_from_user(str, buffer, size);
1510 str[size] = '\0';
1511 val = (u32) simple_strtoul(str, &strend, 0);
1512 if (strend == str)
1513 return -EINVAL;
1515 // store value in register
1516 switch (ctl->ctl_name) {
1517 case ACPI_PM1_ENABLE:
1518 // clear previously disabled events
1519 enabling = (val & ~acpi_read_pm1_enable(facp));
1520 acpi_write_pm1_status(facp, enabling);
1522 if (val) {
1523 // enable ACPI unless it is already
1524 if (!acpi_is_enabled(facp))
1525 acpi_enable(facp);
1527 else if (!acpi_read_gpe_enable(facp)) {
1528 // disable ACPI unless it is already
1529 if (acpi_is_enabled(facp))
1530 acpi_disable(facp);
1533 acpi_write_pm1_enable(facp, val);
1534 break;
1535 case ACPI_GPE_ENABLE:
1536 // clear previously disabled events
1537 enabling = (val
1538 & ~acpi_read_gpe_enable(facp));
1539 while (acpi_read_gpe_status(facp) & enabling)
1540 acpi_write_gpe_status(facp, enabling);
1542 if (val) {
1543 // enable ACPI unless it is already
1544 if (!acpi_is_enabled(facp))
1545 acpi_enable(facp);
1547 else if (!acpi_read_pm1_enable(facp)) {
1548 // disable ACPI unless it is already
1549 if (acpi_is_enabled(facp))
1550 acpi_disable(facp);
1553 acpi_write_gpe_enable(facp, val);
1554 break;
1555 case ACPI_GPE_LEVEL:
1556 acpi_gpe_level = val;
1557 break;
1561 file->f_pos += *len;
1562 return 0;
1566 * Wait for next event
1568 static int acpi_do_event(ctl_table *ctl,
1569 int write,
1570 struct file *file,
1571 void *buffer,
1572 size_t *len)
1574 u32 pm1_status = 0, gpe_status = 0;
1575 acpi_sstate_t event_state = 0;
1576 char str[27];
1577 int size;
1579 if (write)
1580 return -EPERM;
1581 if (*len < sizeof(str)) {
1582 *len = 0;
1583 return 0;
1586 for (;;) {
1587 unsigned long flags;
1589 // we need an atomic exchange here
1590 spin_lock_irqsave(&acpi_event_lock, flags);
1591 pm1_status = acpi_pm1_status;
1592 acpi_pm1_status = 0;
1593 gpe_status = acpi_gpe_status;
1594 acpi_gpe_status = 0;
1595 spin_unlock_irqrestore(&acpi_event_lock, flags);
1596 event_state = acpi_event_state;
1598 if (pm1_status || gpe_status)
1599 break;
1601 // wait for an event to arrive
1602 interruptible_sleep_on(&acpi_event_wait);
1603 if (signal_pending(current))
1604 return -ERESTARTSYS;
1607 size = sprintf(str, "0x%08x 0x%08x 0x%01x\n",
1608 pm1_status,
1609 gpe_status,
1610 event_state);
1611 copy_to_user(buffer, str, size);
1612 *len = size;
1613 file->f_pos += size;
1615 return 0;
1619 * Enter system sleep state
1621 static int acpi_do_sleep(ctl_table *ctl,
1622 int write,
1623 struct file *file,
1624 void *buffer,
1625 size_t *len)
1627 if (!write) {
1628 if (file->f_pos) {
1629 *len = 0;
1630 return 0;
1633 else
1635 if (acpi_verify_mod(ctl->ctl_name) || acpi_enter_sx(ACPI_S1))
1636 return -EPERM;
1638 file->f_pos += *len;
1639 return 0;
1643 * Parse command line options
1645 static int __init acpi_setup(char *str)
1647 while (str && *str) {
1648 struct acpi_option_info *opt = acpi_options;
1649 while (opt->name) {
1650 if (!strncmp(str, opt->name, strlen(opt->name))) {
1651 acpi_opts |= opt->value;
1652 break;
1654 opt++;
1656 str = strpbrk(str, ",");
1657 if (str)
1658 str += strspn(str, ",");
1661 if (acpi_opts)
1662 printk(KERN_INFO "ACPI: options 0x%08lx\n", acpi_opts);
1664 return 1;
1668 * kernel/module command line interfaces are both "acpi=OPTION,OPTION,..."
1670 __setup("acpi=", acpi_setup);
1672 static char * __initdata acpi = NULL;
1674 MODULE_DESCRIPTION("ACPI driver");
1675 MODULE_PARM(acpi, "s");
1676 MODULE_PARM_DESC(acpi, "ACPI driver command line");
1679 * Initialize and enable ACPI
1681 int __init acpi_init(void)
1683 struct acpi_facp *facp = NULL;
1685 if (acpi)
1686 acpi_setup(acpi);
1688 if (acpi_opts & ACPI_DISABLED) {
1689 return -ENODEV;
1691 else if (acpi_opts & ACPI_TABLES_ONLY) {
1692 if (acpi_find_tables())
1693 return -ENODEV;
1695 else if (acpi_opts & ACPI_CHIPSET_ONLY) {
1696 if (acpi_find_chipset())
1697 return -ENODEV;
1699 else {
1700 switch (acpi_find_tables()) {
1701 case 0:
1702 // found valid ACPI tables
1703 break;
1704 case -ENODEV:
1705 // found no ACPI tables, try chipset-specific
1706 if (acpi_find_chipset())
1707 return -ENODEV;
1708 break;
1709 default:
1710 // found broken ACPI tables
1711 return -ENODEV;
1715 facp = (struct acpi_facp*) acpi_facp.table;
1717 if (PM_IS_ACTIVE()) {
1718 printk(KERN_NOTICE "acpi: APM is already active.\n");
1719 goto err_out;
1721 pm_active = 1;
1724 * Internally we always keep latencies in timer
1725 * ticks, which is simpler and more consistent (what is
1726 * an uS to us?). Besides, that gives people more
1727 * control in the /proc interfaces.
1729 if (facp->p_lvl2_lat
1730 && facp->p_lvl2_lat <= ACPI_MAX_P_LVL2_LAT
1731 && !acpi_verify_mod(ACPI_P_LVL2_LAT)) {
1732 acpi_p_lvl2_lat = ACPI_uS_TO_TMR_TICKS(facp->p_lvl2_lat);
1733 acpi_enter_lvl2_lat = ACPI_uS_TO_TMR_TICKS(ACPI_TMR_HZ / 1000);
1735 if (facp->p_lvl3_lat
1736 && facp->p_lvl3_lat <= ACPI_MAX_P_LVL3_LAT
1737 && !acpi_verify_mod(ACPI_P_LVL3_LAT)) {
1738 acpi_p_lvl3_lat = ACPI_uS_TO_TMR_TICKS(facp->p_lvl3_lat);
1739 acpi_enter_lvl3_lat
1740 = ACPI_uS_TO_TMR_TICKS(facp->p_lvl3_lat * 5);
1743 if (acpi_claim_ioports(facp)) {
1744 printk(KERN_ERR "ACPI: I/O port allocation failed\n");
1745 goto err_out;
1748 if (facp->sci_int
1749 && acpi_sci_enabled()
1750 && request_irq(facp->sci_int,
1751 acpi_irq,
1752 SA_INTERRUPT | SA_SHIRQ,
1753 "acpi",
1754 &acpi_facp)) {
1755 printk(KERN_ERR "ACPI: SCI (IRQ%d) allocation failed\n",
1756 facp->sci_int);
1757 goto err_out;
1760 #ifndef CONFIG_ACPI_S1_SLEEP
1761 acpi_opts |= ACPI_S1_DISABLED;
1762 #endif
1764 acpi_sysctl = register_sysctl_table(acpi_dir_table, 1);
1766 pm_power_off = acpi_power_off;
1769 * Set up the ACPI idle function. Note that we can't really
1770 * do this with multiple CPU's, we'd need a per-CPU ACPI
1771 * device..
1773 #ifdef CONFIG_SMP
1774 if (smp_num_cpus > 1)
1775 return 0;
1776 #endif
1778 if (facp->pm_tmr)
1779 pm_idle = acpi_idle;
1781 return 0;
1783 err_out:
1784 if (pci_driver_registered)
1785 pci_unregister_driver(&acpi_driver);
1786 acpi_destroy_tables();
1788 return -ENODEV;
1792 * Disable and deinitialize ACPI
1794 void __exit acpi_exit(void)
1796 struct acpi_facp *facp = (struct acpi_facp*) acpi_facp.table;
1798 pm_idle = NULL;
1799 pm_power_off = NULL;
1801 unregister_sysctl_table(acpi_sysctl);
1802 acpi_disable(facp);
1803 acpi_release_ioports(facp);
1805 if (facp->sci_int && acpi_sci_enabled())
1806 free_irq(facp->sci_int, &acpi_facp);
1808 acpi_destroy_tables();
1810 if (pci_driver_registered)
1811 pci_unregister_driver(&acpi_driver);
1813 pm_active = 0;
1816 module_init(acpi_init);
1817 module_exit(acpi_exit);