- Peter Anvin: more P4 configuration parsing
[davej-history.git] / arch / i386 / kernel / acpi.c
blob98079f060fd8476f8692c510336fa93124179d44
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
25 * Changes:
26 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 2000/08/31
27 * - check copy*user return
28 * - get rid of check_region
29 * - get rid of verify_area
30 * Arnaldo Carvalho de Melo <acme@conectiva.com.br> - 2000/09/28
31 * - do proper release on failure in acpi_claim_ioports and acpi_init
34 #include <linux/config.h>
35 #include <linux/module.h>
36 #include <linux/init.h>
37 #include <linux/string.h>
38 #include <linux/miscdevice.h>
39 #include <linux/sched.h>
40 #include <linux/time.h>
41 #include <linux/wait.h>
42 #include <linux/spinlock.h>
43 #include <linux/ioport.h>
44 #include <linux/slab.h>
45 #include <linux/mm.h>
46 #include <linux/pci.h>
47 #include <asm/uaccess.h>
48 #include <asm/io.h>
49 #include <linux/sysctl.h>
50 #include <linux/delay.h>
51 #include <linux/pm.h>
52 #include <linux/acpi.h>
55 * Yes, it's unfortunate that we are relying on get_cmos_time
56 * because it is slow (> 1 sec.) and i386 only. It might be better
57 * to use some of the code from drivers/char/rtc.c in the near future
59 extern unsigned long get_cmos_time(void);
61 static int acpi_do_ulong(ctl_table *ctl,
62 int write,
63 struct file *file,
64 void *buffer,
65 size_t *len);
66 static int acpi_do_table(ctl_table *ctl,
67 int write,
68 struct file *file,
69 void *buffer,
70 size_t *len);
71 static int acpi_do_event_reg(ctl_table *ctl,
72 int write,
73 struct file *file,
74 void *buffer,
75 size_t *len);
76 static int acpi_do_event(ctl_table *ctl,
77 int write,
78 struct file *file,
79 void *buffer,
80 size_t *len);
81 static int acpi_do_sleep(ctl_table *ctl,
82 int write,
83 struct file *file,
84 void *buffer,
85 size_t *len);
86 static void acpi_release(unsigned long start, unsigned long size);
88 static struct ctl_table_header *acpi_sysctl = NULL;
90 // current system sleep state (S0 - S4)
91 static acpi_sstate_t acpi_sleep_state = ACPI_S0;
92 // time sleep began
93 static unsigned long acpi_sleep_start = 0;
95 static spinlock_t acpi_event_lock = SPIN_LOCK_UNLOCKED;
96 static volatile u32 acpi_pm1_status = 0;
97 static volatile u32 acpi_gpe_status = 0;
98 static volatile u32 acpi_gpe_level = 0;
99 static volatile acpi_sstate_t acpi_event_state = ACPI_S0;
100 static DECLARE_WAIT_QUEUE_HEAD(acpi_event_wait);
102 /* Make it impossible to enter C2/C3 until after we've initialized */
103 static unsigned long acpi_enter_lvl2_lat = ACPI_INFINITE_LAT;
104 static unsigned long acpi_enter_lvl3_lat = ACPI_INFINITE_LAT;
105 static unsigned long acpi_p_lvl2_lat = ACPI_INFINITE_LAT;
106 static unsigned long acpi_p_lvl3_lat = ACPI_INFINITE_LAT;
108 /* Statistics.. */
109 struct Cx_stat_struct {
110 unsigned long time;
111 unsigned long min;
112 unsigned long max;
113 unsigned long avg;
114 } Cx_stat[3];
116 static int acpi_do_stat(ctl_table *ctl,
117 int write,
118 struct file *file,
119 void *buffer,
120 size_t *len)
122 int size;
123 char str[4*10];
124 struct Cx_stat_struct *stat = (struct Cx_stat_struct *)ctl->data;
126 if (write) {
127 stat->time = 0;
128 stat->min = 0;
129 stat->max = 0;
130 stat->avg = 0;
131 return 0;
134 if (file->f_pos) {
135 *len = 0;
136 return 0;
138 size = sprintf(str, "%9lu %9lu %9lu %9lu",
139 stat->time,
140 stat->min,
141 stat->max,
142 stat->avg);
143 if (*len < size) {
144 *len = 0;
145 return 0;
147 return copy_to_user(buffer, str, size) ? -EFAULT : 0;
150 static void cx_statistics(unsigned int x, unsigned long time)
152 struct Cx_stat_struct *stat = Cx_stat + (x-1);
154 stat->time += time;
155 if (time <= stat->min-1)
156 stat->min = time;
157 if (time > stat->max)
158 stat->max = time;
159 stat->avg = time + (stat->avg >> 1);
162 static unsigned long acpi_p_blk = 0;
164 static int acpi_p_lvl2_tested = 0;
165 static int acpi_p_lvl3_tested = 0;
167 enum
169 ACPI_ENABLED = 0x00000000, // use ACPI if present
170 ACPI_DISABLED = 0x00000001, // never use ACPI
171 ACPI_TABLES_ONLY = 0x00000002, // never use chipset-specific driver
172 ACPI_CHIPSET_ONLY = 0x00000004, // always use chipset-specific driver
173 ACPI_IGNORE_ERRATA = 0x00000008, // ignore any listed platform errata
174 ACPI_COPY_TABLES = 0x00000010, // copy ACPI tables before use
175 ACPI_TRUST_TABLES = 0x00000020, // use tables even after ioremap fails
176 ACPI_SCI_DISABLED = 0x00000040, // never enable ACPI (info. only)
177 ACPI_C2_DISABLED = 0x00000080, // never enter C2
178 ACPI_C3_DISABLED = 0x00000100, // never enter C3
179 ACPI_S1_DISABLED = 0x00000200, // never enter S1
180 ACPI_S5_DISABLED = 0x00000400, // never enter S5
183 struct acpi_option_info
185 const char *name;
186 unsigned long value;
189 static struct acpi_option_info acpi_options[] =
191 {"on", ACPI_ENABLED},
192 {"off", ACPI_DISABLED},
193 {"tables", ACPI_TABLES_ONLY},
194 {"chipset", ACPI_CHIPSET_ONLY},
195 {"no-errata", ACPI_IGNORE_ERRATA},
196 {"copy-tables", ACPI_COPY_TABLES},
197 {"trust-tables", ACPI_TRUST_TABLES},
198 {"no-sci", ACPI_SCI_DISABLED},
199 {"no-c2", ACPI_C2_DISABLED},
200 {"no-c3", ACPI_C3_DISABLED},
201 {"no-s1", ACPI_S1_DISABLED},
202 {"no-s5", ACPI_S5_DISABLED},
203 {NULL, 0},
206 static unsigned long acpi_opts = ACPI_ENABLED;
208 struct acpi_errata_info
210 const char *signature; // table signature (eg. "RSDT")
211 const char *oem; // OEM name
212 const char *oem_table; // OEM table identifier (optional)
213 u32 oem_rev; // OEM table revision (optional)
214 unsigned long options; // errata options
218 * We must identify systems that need ACPI_TRUST_TABLES solely from the
219 * RSDP ("RSD PTR "). All other options should be flagged from the
220 * RSDT ("RSDT") which can be better identified.
222 struct acpi_errata_info acpi_errata[] =
224 {"RSD PTR ", "AMI ", NULL, 0, ACPI_TRUST_TABLES | ACPI_COPY_TABLES},
225 {NULL, NULL, 0, 0},
228 // bits 8-15 are SLP_TYPa, bits 0-7 are SLP_TYPb
229 static unsigned long acpi_slp_typ[] =
231 ACPI_SLP_TYP_DISABLED, /* S0 */
232 ACPI_SLP_TYP_DISABLED, /* S1 */
233 ACPI_SLP_TYP_DISABLED, /* S2 */
234 ACPI_SLP_TYP_DISABLED, /* S3 */
235 ACPI_SLP_TYP_DISABLED, /* S4 */
236 ACPI_SLP_TYP_DISABLED /* S5 */
239 struct acpi_table_info
241 u32 expected_signature;
242 u32 expected_size;
244 struct acpi_table *table;
245 size_t size;
246 int mapped;
249 static struct acpi_table_info acpi_facp
250 = {ACPI_FACP_SIG, sizeof(struct acpi_facp), NULL, 0, 0};
251 static struct acpi_table_info acpi_dsdt = {ACPI_DSDT_SIG, 0, NULL, 0, 0};
252 static struct acpi_table_info acpi_facs
253 = {ACPI_FACS_SIG, sizeof(struct acpi_facs), NULL, 0, 0};
254 static rwlock_t acpi_do_table_lock = RW_LOCK_UNLOCKED;
256 static struct ctl_table acpi_table[] =
258 {ACPI_FACP, "facp", &acpi_facp, 0, 0644, NULL, &acpi_do_table},
260 {ACPI_DSDT, "dsdt", &acpi_dsdt, 0, 0644, NULL, &acpi_do_table},
262 {ACPI_FACS, "facs", &acpi_facs, 0, 0644, NULL, &acpi_do_table},
264 {ACPI_PM1_ENABLE, "pm1_enable",
265 NULL, 0,
266 0600, NULL, &acpi_do_event_reg},
268 {ACPI_GPE_ENABLE, "gpe_enable",
269 NULL, 0,
270 0600, NULL, &acpi_do_event_reg},
272 {ACPI_GPE_LEVEL, "gpe_level",
273 NULL, 0,
274 0600, NULL, &acpi_do_event_reg},
276 {ACPI_EVENT, "event", NULL, 0, 0400, NULL, &acpi_do_event},
278 {ACPI_P_BLK, "p_blk",
279 &acpi_p_blk, sizeof(acpi_p_blk),
280 0600, NULL, &acpi_do_ulong},
282 {ACPI_P_LVL2_LAT, "p_lvl2_lat",
283 &acpi_p_lvl2_lat, sizeof(acpi_p_lvl2_lat),
284 0644, NULL, &acpi_do_ulong},
286 {ACPI_P_LVL3_LAT, "p_lvl3_lat",
287 &acpi_p_lvl3_lat, sizeof(acpi_p_lvl3_lat),
288 0644, NULL, &acpi_do_ulong},
290 {ACPI_ENTER_LVL2_LAT, "enter_lvl2_lat",
291 &acpi_enter_lvl2_lat, sizeof(acpi_enter_lvl2_lat),
292 0644, NULL, &acpi_do_ulong},
294 {ACPI_ENTER_LVL3_LAT, "enter_lvl3_lat",
295 &acpi_enter_lvl3_lat, sizeof(acpi_enter_lvl3_lat),
296 0644, NULL, &acpi_do_ulong},
298 {ACPI_C1_TIME, "c1_time",
299 Cx_stat+0, sizeof(struct Cx_stat_struct),
300 0644, NULL, &acpi_do_stat},
302 {ACPI_C2_TIME, "c2_time",
303 Cx_stat+1, sizeof(struct Cx_stat_struct),
304 0644, NULL, &acpi_do_stat},
306 {ACPI_C3_TIME, "c3_time",
307 Cx_stat+2, sizeof(struct Cx_stat_struct),
308 0644, NULL, &acpi_do_stat},
310 {ACPI_S0_SLP_TYP, "s0_slp_typ",
311 &acpi_slp_typ[ACPI_S0], sizeof(acpi_slp_typ[ACPI_S0]),
312 0600, NULL, &acpi_do_ulong},
314 {ACPI_S1_SLP_TYP, "s1_slp_typ",
315 &acpi_slp_typ[ACPI_S1], sizeof(acpi_slp_typ[ACPI_S1]),
316 0600, NULL, &acpi_do_ulong},
318 {ACPI_S5_SLP_TYP, "s5_slp_typ",
319 &acpi_slp_typ[ACPI_S5], sizeof(acpi_slp_typ[ACPI_S5]),
320 0600, NULL, &acpi_do_ulong},
322 {ACPI_SLEEP, "sleep", NULL, 0, 0600, NULL, &acpi_do_sleep},
327 static struct ctl_table acpi_dir_table[] =
329 {CTL_ACPI, "acpi", NULL, 0, 0555, acpi_table},
333 static u32 FASTCALL(acpi_read_pm1_control(struct acpi_facp *));
334 static u32 FASTCALL(acpi_read_pm1_status(struct acpi_facp *));
335 static u32 FASTCALL(acpi_read_pm1_enable(struct acpi_facp *));
336 static u32 FASTCALL(acpi_read_gpe_status(struct acpi_facp *));
337 static u32 FASTCALL(acpi_read_gpe_enable(struct acpi_facp *));
339 static void FASTCALL(acpi_write_pm1_control(struct acpi_facp *, u32));
340 static void FASTCALL(acpi_write_pm1_status(struct acpi_facp *, u32));
341 static void FASTCALL(acpi_write_pm1_enable(struct acpi_facp *, u32));
342 static void FASTCALL(acpi_write_gpe_status(struct acpi_facp *, u32));
343 static void FASTCALL(acpi_write_gpe_enable(struct acpi_facp *, u32));
346 * Get the value of the PM1 control register (SCI_EN, ...)
348 static u32 acpi_read_pm1_control(struct acpi_facp *facp)
350 u32 value = 0;
351 if (facp->pm1a_cnt)
352 value = inw(facp->pm1a_cnt);
353 if (facp->pm1b_cnt)
354 value |= inw(facp->pm1b_cnt);
355 return value;
359 * Set the value of the PM1 control register (BM_RLD, ...)
361 static void acpi_write_pm1_control(struct acpi_facp *facp, u32 value)
363 if (facp->pm1a_cnt)
364 outw(value, facp->pm1a_cnt);
365 if (facp->pm1b_cnt)
366 outw(value, facp->pm1b_cnt);
370 * Get the value of the fixed event status register
372 static u32 acpi_read_pm1_status(struct acpi_facp *facp)
374 u32 value = 0;
375 if (facp->pm1a_evt)
376 value = inw(facp->pm1a_evt);
377 if (facp->pm1b_evt)
378 value |= inw(facp->pm1b_evt);
379 return value;
383 * Set the value of the fixed event status register (clear events)
385 static void acpi_write_pm1_status(struct acpi_facp *facp, u32 value)
387 if (facp->pm1a_evt)
388 outw(value, facp->pm1a_evt);
389 if (facp->pm1b_evt)
390 outw(value, facp->pm1b_evt);
394 * Get the value of the fixed event enable register
396 static u32 acpi_read_pm1_enable(struct acpi_facp *facp)
398 int offset = facp->pm1_evt_len >> 1;
399 u32 value = 0;
400 if (facp->pm1a_evt)
401 value = inw(facp->pm1a_evt + offset);
402 if (facp->pm1b_evt)
403 value |= inw(facp->pm1b_evt + offset);
404 return value;
408 * Set the value of the fixed event enable register (enable events)
410 static void acpi_write_pm1_enable(struct acpi_facp *facp, u32 value)
412 int offset = facp->pm1_evt_len >> 1;
413 if (facp->pm1a_evt)
414 outw(value, facp->pm1a_evt + offset);
415 if (facp->pm1b_evt)
416 outw(value, facp->pm1b_evt + offset);
420 * Get the value of the general-purpose event status register
422 static u32 acpi_read_gpe_status(struct acpi_facp *facp)
424 u32 value = 0;
425 int i, size;
427 if (facp->gpe1) {
428 size = facp->gpe1_len >> 1;
429 for (i = size - 1; i >= 0; i--)
430 value = (value << 8) | inb(facp->gpe1 + i);
432 if (facp->gpe0) {
433 size = facp->gpe0_len >> 1;
434 for (i = size - 1; i >= 0; i--)
435 value = (value << 8) | inb(facp->gpe0 + i);
437 return value;
441 * Set the value of the general-purpose event status register (clear events)
443 static void acpi_write_gpe_status(struct acpi_facp *facp, u32 value)
445 int i, size;
447 if (facp->gpe0) {
448 size = facp->gpe0_len >> 1;
449 for (i = 0; i < size; i++) {
450 outb(value & 0xff, facp->gpe0 + i);
451 value >>= 8;
454 if (facp->gpe1) {
455 size = facp->gpe1_len >> 1;
456 for (i = 0; i < size; i++) {
457 outb(value & 0xff, facp->gpe1 + i);
458 value >>= 8;
464 * Get the value of the general-purpose event enable register
466 static u32 acpi_read_gpe_enable(struct acpi_facp *facp)
468 u32 value = 0;
469 int i, size, offset;
471 offset = facp->gpe0_len >> 1;
472 if (facp->gpe1) {
473 size = facp->gpe1_len >> 1;
474 for (i = size - 1; i >= 0; i--) {
475 value = (value << 8) | inb(facp->gpe1 + offset + i);
478 if (facp->gpe0) {
479 size = facp->gpe0_len >> 1;
480 for (i = size - 1; i >= 0; i--)
481 value = (value << 8) | inb(facp->gpe0 + offset + i);
483 return value;
487 * Set the value of the general-purpose event enable register (enable events)
489 static void acpi_write_gpe_enable(struct acpi_facp *facp, u32 value)
491 int i, offset;
493 offset = facp->gpe0_len >> 1;
494 if (facp->gpe0) {
495 for (i = 0; i < offset; i++) {
496 outb(value & 0xff, facp->gpe0 + offset + i);
497 value >>= 8;
500 if (facp->gpe1) {
501 offset = facp->gpe1_len >> 1;
502 for (i = 0; i < offset; i++) {
503 outb(value & 0xff, facp->gpe1 + offset + i);
504 value >>= 8;
510 * Map an ACPI table into virtual memory
512 static struct acpi_table *__init acpi_map_table(u32 addr)
514 struct acpi_table *table = NULL;
515 if (addr) {
516 // map table header to determine size
517 table = (struct acpi_table *)
518 ioremap((unsigned long) addr,
519 sizeof(struct acpi_table));
520 if (table) {
521 unsigned long table_size = table->length;
522 iounmap(table);
523 // remap entire table
524 table = (struct acpi_table *)
525 ioremap((unsigned long) addr, table_size);
528 if (!table && addr < virt_to_phys(high_memory)) {
529 /* sometimes we see ACPI tables in low memory
530 * and not reserved by the memory map (E820) code,
531 * who is at fault for this? BIOS?
533 printk(KERN_ERR
534 "ACPI: unreserved table memory @ 0x%p!\n",
535 (void*) addr);
537 if (acpi_opts & ACPI_TRUST_TABLES) {
538 /* OK, trust that the table is there
539 * if it isn't you'll get an OOPS here
541 static u32 sig;
542 table = (struct acpi_table *)
543 phys_to_virt(addr);
544 sig = table->signature;
548 return table;
552 * Unmap an ACPI table from virtual memory
554 static void acpi_unmap_table(struct acpi_table *table)
556 // iounmap ignores addresses within physical memory
557 if (table)
558 iounmap(table);
562 * Initialize an ACPI table
564 static int acpi_init_table(struct acpi_table_info *info,
565 void *data,
566 int mapped)
568 struct acpi_table *table = (struct acpi_table*) data;
570 info->table = NULL;
571 info->size = 0;
572 info->mapped = 0;
574 if (!table || table->signature != info->expected_signature)
575 return -EINVAL;
577 if (mapped && (acpi_opts & ACPI_COPY_TABLES)) {
578 struct acpi_table *copy
579 = kmalloc(table->length, GFP_KERNEL);
580 if (!copy)
581 return -ENOMEM;
582 memcpy(copy, table, table->length);
583 table = copy;
584 mapped = 0;
587 info->table = table;
588 info->size = (size_t) table->length;
589 info->mapped = mapped;
590 return 0;
594 * Destroy an ACPI table
596 static void acpi_destroy_table(struct acpi_table_info *info)
598 if (info->table) {
599 if (info->mapped)
600 acpi_unmap_table(info->table);
601 else
602 kfree(info->table);
603 info->table = NULL;
608 * Match ACPI table and set options based on platform errata, if any
610 static int __init acpi_find_errata(struct acpi_table *table)
612 struct acpi_errata_info *info;
613 int size;
615 for (info = acpi_errata; info->signature && info->oem; info++) {
616 size = strlen(info->signature);
617 if (memcmp(&table->signature, info->signature, size))
618 continue;
619 if (strcmp(info->signature, "RSD PTR ")) {
620 // ordinary ACPI table
621 size = strlen(info->oem);
622 if (memcmp(table->oem, info->oem, size))
623 continue;
624 if (info->oem_table) {
625 size = strlen(info->oem_table);
626 if (memcmp(table->oem_table,
627 info->oem_table,
628 size))
629 continue;
631 if (info->oem_rev && table->oem_rev != info->oem_rev)
632 continue;
634 else {
635 // special handling for RSDP
636 size = strlen(info->oem);
637 if (memcmp(((struct acpi_rsdp*) table)->oem,
638 info->oem,
639 size))
640 continue;
643 printk(KERN_INFO
644 "ACPI: found platform errata 0x%08lx\n",
645 info->options);
646 acpi_opts |= info->options;
647 return 0;
649 return -1;
653 * Locate and map ACPI tables
655 static int __init acpi_find_tables(void)
657 struct acpi_rsdp *rsdp;
658 struct acpi_table *rsdt;
659 u32 *rsdt_entry;
660 int rsdt_entry_count;
661 unsigned long i;
663 // search BIOS memory for RSDP
664 for (i = ACPI_BIOS_ROM_BASE; i < ACPI_BIOS_ROM_END; i += 16) {
665 rsdp = (struct acpi_rsdp *) phys_to_virt(i);
666 if (rsdp->signature[0] == ACPI_RSDP1_SIG
667 && rsdp->signature[1] == ACPI_RSDP2_SIG) {
668 char oem[7];
669 int j;
671 // strip trailing space and print OEM identifier
672 memcpy(oem, rsdp->oem, 6);
673 oem[6] = '\0';
674 for (j = 5;
675 j > 0 && (oem[j] == '\0' || oem[j] == ' ');
676 j--) {
677 oem[j] = '\0';
679 printk(KERN_INFO "ACPI: \"%s\" found at 0x%p\n",
680 oem, (void *) i);
682 break;
685 if (i >= ACPI_BIOS_ROM_END)
686 return -ENODEV;
688 // find any errata based on the RSDP
689 if (!acpi_find_errata((struct acpi_table*) rsdp)) {
690 if (acpi_opts & ACPI_DISABLED)
691 return -EINVAL;
692 else if (acpi_opts & ACPI_CHIPSET_ONLY)
693 return -ENODEV;
696 // fetch RSDT from RSDP
697 rsdt = acpi_map_table(rsdp->rsdt);
698 if (!rsdt) {
699 printk(KERN_ERR "ACPI: missing RSDT at 0x%p\n",
700 (void*) rsdp->rsdt);
701 return -EINVAL;
703 else if (rsdt->signature != ACPI_RSDT_SIG) {
704 printk(KERN_ERR "ACPI: bad RSDT at 0x%p (%08x)\n",
705 (void*) rsdp->rsdt, (unsigned) rsdt->signature);
706 acpi_unmap_table(rsdt);
707 return -EINVAL;
710 // find any errata based on the RSDT
711 if (!acpi_find_errata(rsdt)) {
712 if (acpi_opts & ACPI_DISABLED)
713 return -EINVAL;
714 else if (acpi_opts & ACPI_CHIPSET_ONLY)
715 return -ENODEV;
718 // search RSDT for FACP
719 acpi_facp.table = NULL;
720 rsdt_entry = (u32 *) (rsdt + 1);
721 rsdt_entry_count = (int) ((rsdt->length - sizeof(*rsdt)) >> 2);
722 while (rsdt_entry_count) {
723 struct acpi_table *dt = acpi_map_table(*rsdt_entry);
724 if (!acpi_init_table(&acpi_facp, dt, 1)) {
725 struct acpi_facp *facp
726 = (struct acpi_facp*) acpi_facp.table;
727 struct acpi_table *facs;
729 // map DSDT if it exists
730 if ((dt = acpi_map_table(facp->dsdt))) {
731 if (acpi_init_table(&acpi_dsdt, dt, 1))
732 acpi_unmap_table(dt);
736 * map FACS if it exists
738 if ((facs = acpi_map_table(facp->facs))) {
739 if (acpi_init_table(&acpi_facs,facs,1))
740 acpi_unmap_table(facs);
742 break;
744 else {
745 acpi_unmap_table(dt);
748 rsdt_entry++;
749 rsdt_entry_count--;
752 acpi_unmap_table(rsdt);
754 if (!acpi_facp.table) {
755 printk(KERN_ERR "ACPI: missing FACP\n");
756 return -EINVAL;
758 return 0;
762 * Unmap or destroy ACPI tables
764 static void acpi_destroy_tables(void)
766 acpi_destroy_table(&acpi_facs);
767 acpi_destroy_table(&acpi_dsdt);
768 acpi_destroy_table(&acpi_facp);
772 * Init PIIX4 device, create a fake FACP
774 static int __init acpi_init_piix4(struct pci_dev *dev)
776 struct acpi_facp *facp;
777 u32 base;
778 u16 cmd;
779 u8 pmregmisc;
781 pci_read_config_word(dev, PCI_COMMAND, &cmd);
782 if (!(cmd & PCI_COMMAND_IO))
783 return -ENODEV;
785 pci_read_config_byte(dev, ACPI_PIIX4_PMREGMISC, &pmregmisc);
786 if (!(pmregmisc & ACPI_PIIX4_PMIOSE))
787 return -ENODEV;
789 base = pci_resource_start (dev, PCI_BRIDGE_RESOURCES);
790 if (!base)
791 return -ENODEV;
793 printk(KERN_INFO "ACPI: found PIIX4 at 0x%04x\n", base);
795 facp = kmalloc(sizeof(struct acpi_facp), GFP_KERNEL);
796 if (!facp)
797 return -ENOMEM;
799 memset(facp, 0, sizeof(struct acpi_facp));
800 facp->hdr.signature = ACPI_FACP_SIG;
801 facp->hdr.length = sizeof(struct acpi_facp);
802 facp->int_model = ACPI_PIIX4_INT_MODEL;
803 facp->sci_int = ACPI_PIIX4_SCI_INT;
804 facp->smi_cmd = ACPI_PIIX4_SMI_CMD;
805 facp->acpi_enable = ACPI_PIIX4_ACPI_ENABLE;
806 facp->acpi_disable = ACPI_PIIX4_ACPI_DISABLE;
807 facp->s4bios_req = ACPI_PIIX4_S4BIOS_REQ;
808 facp->pm1a_evt = base + ACPI_PIIX4_PM1_EVT;
809 facp->pm1a_cnt = base + ACPI_PIIX4_PM1_CNT;
810 facp->pm2_cnt = ACPI_PIIX4_PM2_CNT;
811 facp->pm_tmr = base + ACPI_PIIX4_PM_TMR;
812 facp->gpe0 = base + ACPI_PIIX4_GPE0;
813 facp->pm1_evt_len = ACPI_PIIX4_PM1_EVT_LEN;
814 facp->pm1_cnt_len = ACPI_PIIX4_PM1_CNT_LEN;
815 facp->pm2_cnt_len = ACPI_PIIX4_PM2_CNT_LEN;
816 facp->pm_tm_len = ACPI_PIIX4_PM_TM_LEN;
817 facp->gpe0_len = ACPI_PIIX4_GPE0_LEN;
818 facp->p_lvl2_lat = (__u16) ACPI_INFINITE_LAT;
819 facp->p_lvl3_lat = (__u16) ACPI_INFINITE_LAT;
821 acpi_init_table(&acpi_facp, facp, 0);
822 acpi_init_table(&acpi_dsdt, NULL, 0);
824 acpi_p_blk = base + ACPI_PIIX4_P_BLK;
826 return 0;
830 * Init VIA ACPI device and create a fake FACP
832 static int __init acpi_init_via(struct pci_dev *dev)
834 struct acpi_facp *facp;
835 u32 base;
836 u8 tmp, irq;
838 pci_read_config_byte(dev, 0x41, &tmp);
839 if (!(tmp & 0x80))
840 return -ENODEV;
842 base = pci_resource_start(dev, PCI_BRIDGE_RESOURCES);
843 if (!base) {
844 base = pci_resource_start(dev, PCI_BASE_ADDRESS_4);
845 if (!base)
846 return -ENODEV;
849 pci_read_config_byte(dev, 0x42, &irq);
851 printk(KERN_INFO "ACPI: found %s at 0x%04x\n", dev->name, base);
853 facp = kmalloc(sizeof(struct acpi_facp), GFP_KERNEL);
854 if (!facp)
855 return -ENOMEM;
857 memset(facp, 0, sizeof(struct acpi_facp));
858 facp->hdr.signature = ACPI_FACP_SIG;
859 facp->hdr.length = sizeof(struct acpi_facp);
860 facp->int_model = ACPI_VIA_INT_MODEL;
861 facp->sci_int = irq;
862 facp->smi_cmd = base + ACPI_VIA_SMI_CMD;
863 facp->acpi_enable = ACPI_VIA_ACPI_ENABLE;
864 facp->acpi_disable = ACPI_VIA_ACPI_DISABLE;
865 facp->pm1a_evt = base + ACPI_VIA_PM1_EVT;
866 facp->pm1a_cnt = base + ACPI_VIA_PM1_CNT;
867 facp->pm_tmr = base + ACPI_VIA_PM_TMR;
868 facp->gpe0 = base + ACPI_VIA_GPE0;
870 facp->pm1_evt_len = ACPI_VIA_PM1_EVT_LEN;
871 facp->pm1_cnt_len = ACPI_VIA_PM1_CNT_LEN;
872 facp->pm_tm_len = ACPI_VIA_PM_TM_LEN;
873 facp->gpe0_len = ACPI_VIA_GPE0_LEN;
874 facp->p_lvl2_lat = (__u16) ACPI_INFINITE_LAT;
875 facp->p_lvl3_lat = (__u16) ACPI_INFINITE_LAT;
877 facp->duty_offset = ACPI_VIA_DUTY_OFFSET;
878 facp->duty_width = ACPI_VIA_DUTY_WIDTH;
880 facp->day_alarm = ACPI_VIA_DAY_ALARM;
881 facp->mon_alarm = ACPI_VIA_MON_ALARM;
882 facp->century = ACPI_VIA_CENTURY;
884 acpi_init_table(&acpi_facp, facp, 0);
885 acpi_init_table(&acpi_dsdt, NULL, 0);
887 acpi_p_blk = base + ACPI_VIA_P_BLK;
889 return 0;
892 typedef enum
894 CH_UNKNOWN = 0,
895 CH_INTEL_PIIX4,
896 CH_VIA_586,
897 CH_VIA_686A,
898 } acpi_chip_t;
900 /* indexed by value of each enum in acpi_chip_t */
901 const static struct
903 int (*chip_init)(struct pci_dev *dev);
904 } acpi_chip_info[] =
906 {NULL,},
907 {acpi_init_piix4},
908 {acpi_init_via},
909 {acpi_init_via},
912 static struct pci_device_id acpi_pci_tbl[] __initdata =
914 {0x8086, 0x7113, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_INTEL_PIIX4},
915 {0x1106, 0x3040, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_VIA_586},
916 {0x1106, 0x3057, PCI_ANY_ID, PCI_ANY_ID, 0, 0, CH_VIA_686A},
917 {0,} /* terminate list */
920 static int __init acpi_probe(struct pci_dev *dev,
921 const struct pci_device_id *id)
923 return acpi_chip_info[id->driver_data].chip_init(dev);
926 static struct pci_driver acpi_driver =
928 name: "acpi",
929 id_table: acpi_pci_tbl,
930 probe: acpi_probe,
932 static int pci_driver_registered = 0;
935 * Locate a known ACPI chipset
937 static int __init acpi_find_chipset(void)
939 if (pci_register_driver(&acpi_driver) < 1)
940 return -ENODEV;
942 pci_driver_registered = 1;
944 return 0;
948 * Handle an ACPI SCI (fixed or general purpose event)
950 static void acpi_irq(int irq, void *dev_id, struct pt_regs *regs)
952 struct acpi_facp *facp = (struct acpi_facp*) acpi_facp.table;
953 u32 pm1_status, gpe_status, gpe_level, gpe_edge;
954 unsigned long flags;
956 // detect and clear fixed events
957 pm1_status = (acpi_read_pm1_status(facp) & acpi_read_pm1_enable(facp));
958 acpi_write_pm1_status(facp, pm1_status);
960 // detect and handle general-purpose events
961 gpe_status = (acpi_read_gpe_status(facp) & acpi_read_gpe_enable(facp));
962 gpe_level = gpe_status & acpi_gpe_level;
963 if (gpe_level) {
964 // disable level-triggered events (re-enabled after handling)
965 acpi_write_gpe_enable(facp,
966 acpi_read_gpe_enable(facp) & ~gpe_level);
968 gpe_edge = gpe_status & ~gpe_level;
969 if (gpe_edge) {
970 // clear edge-triggered events
971 while (acpi_read_gpe_status(facp) & gpe_edge)
972 acpi_write_gpe_status(facp, gpe_edge);
975 // notify process waiting on /dev/acpi
976 spin_lock_irqsave(&acpi_event_lock, flags);
977 acpi_pm1_status |= pm1_status;
978 acpi_gpe_status |= gpe_status;
979 spin_unlock_irqrestore(&acpi_event_lock, flags);
980 acpi_event_state = acpi_sleep_state;
981 wake_up_interruptible(&acpi_event_wait);
985 * Is SCI to be enabled?
987 static inline int
988 acpi_sci_enabled(void)
990 return !(acpi_opts & ACPI_SCI_DISABLED);
994 * Is ACPI enabled or not?
996 static inline int acpi_is_enabled(struct acpi_facp *facp)
998 return ((acpi_read_pm1_control(facp) & ACPI_SCI_EN) ? 1:0);
1002 * Enable SCI
1004 static int acpi_enable(struct acpi_facp *facp)
1006 if (facp->smi_cmd && acpi_sci_enabled())
1007 outb(facp->acpi_enable, facp->smi_cmd);
1008 return (acpi_is_enabled(facp) ? 0:-1);
1012 * Disable SCI
1014 static int acpi_disable(struct acpi_facp *facp)
1016 if (facp->smi_cmd && acpi_sci_enabled()) {
1017 // disable and clear any pending events
1018 acpi_write_gpe_enable(facp, 0);
1019 while (acpi_read_gpe_status(facp)) {
1020 acpi_write_gpe_status(facp,
1021 acpi_read_gpe_status(facp));
1023 acpi_write_pm1_enable(facp, 0);
1024 acpi_write_pm1_status(facp, acpi_read_pm1_status(facp));
1026 /* writing acpi_disable to smi_cmd would be appropriate
1027 * here but this causes a nasty crash on many systems
1031 return 0;
1034 static inline int bm_activity(struct acpi_facp *facp)
1036 return acpi_read_pm1_status(facp) & ACPI_BM;
1039 static inline void clear_bm_activity(struct acpi_facp *facp)
1041 acpi_write_pm1_status(facp, ACPI_BM);
1044 static void sleep_on_busmaster(struct acpi_facp *facp)
1046 u32 pm1_cntr = acpi_read_pm1_control(facp);
1047 if (pm1_cntr & ACPI_BM_RLD) {
1048 pm1_cntr &= ~ACPI_BM_RLD;
1049 acpi_write_pm1_control(facp, pm1_cntr);
1053 static void wake_on_busmaster(struct acpi_facp *facp)
1055 u32 pm1_cntr = acpi_read_pm1_control(facp);
1056 if (!(pm1_cntr & ACPI_BM_RLD)) {
1057 pm1_cntr |= ACPI_BM_RLD;
1058 acpi_write_pm1_control(facp, pm1_cntr);
1060 clear_bm_activity(facp);
1063 /* The ACPI timer is just the low 24 bits */
1064 #define TIME_BEGIN(tmr) inl(tmr)
1065 #define TIME_END(tmr, begin) ((inl(tmr) - (begin)) & 0x00ffffff)
1069 * Idle loop (uniprocessor only)
1071 static void acpi_idle(void)
1073 static int sleep_level = 1;
1074 struct acpi_facp *facp = (struct acpi_facp*) acpi_facp.table;
1076 if (!facp || !facp->pm_tmr || !acpi_p_blk)
1077 goto not_initialized;
1080 * start from the previous sleep level..
1082 if (sleep_level == 1)
1083 goto sleep1;
1084 if (sleep_level == 2)
1085 goto sleep2;
1086 sleep3:
1087 sleep_level = 3;
1088 if (!acpi_p_lvl3_tested) {
1089 printk(KERN_INFO "ACPI C3 works\n");
1090 acpi_p_lvl3_tested = 1;
1092 wake_on_busmaster(facp);
1093 if (facp->pm2_cnt)
1094 goto sleep3_with_arbiter;
1096 for (;;) {
1097 unsigned long time;
1098 unsigned int pm_tmr = facp->pm_tmr;
1100 __cli();
1101 if (current->need_resched)
1102 goto out;
1103 if (bm_activity(facp))
1104 goto sleep2;
1106 time = TIME_BEGIN(pm_tmr);
1107 inb(acpi_p_blk + ACPI_P_LVL3);
1108 inl(pm_tmr); /* Dummy read, force synchronization with the PMU */
1109 time = TIME_END(pm_tmr, time);
1111 __sti();
1112 cx_statistics(3, time);
1113 if (time < acpi_p_lvl3_lat)
1114 goto sleep2;
1117 sleep3_with_arbiter:
1118 for (;;) {
1119 unsigned long time;
1120 u8 arbiter;
1121 unsigned int pm2_cntr = facp->pm2_cnt;
1122 unsigned int pm_tmr = facp->pm_tmr;
1124 __cli();
1125 if (current->need_resched)
1126 goto out;
1127 if (bm_activity(facp))
1128 goto sleep2;
1130 time = TIME_BEGIN(pm_tmr);
1131 arbiter = inb(pm2_cntr) & ~ACPI_ARB_DIS;
1132 outb(arbiter | ACPI_ARB_DIS, pm2_cntr); /* Disable arbiter, park on CPU */
1133 inb(acpi_p_blk + ACPI_P_LVL3);
1134 inl(pm_tmr); /* Dummy read, force synchronization with the PMU */
1135 time = TIME_END(pm_tmr, time);
1136 outb(arbiter, pm2_cntr); /* Enable arbiter again.. */
1138 __sti();
1139 cx_statistics(3, time);
1140 if (time < acpi_p_lvl3_lat)
1141 goto sleep2;
1144 sleep2:
1145 sleep_level = 2;
1146 if (!acpi_p_lvl2_tested) {
1147 printk(KERN_INFO "ACPI C2 works\n");
1148 acpi_p_lvl2_tested = 1;
1150 wake_on_busmaster(facp); /* Required to track BM activity.. */
1151 for (;;) {
1152 unsigned long time;
1153 unsigned int pm_tmr = facp->pm_tmr;
1155 __cli();
1156 if (current->need_resched)
1157 goto out;
1159 time = TIME_BEGIN(pm_tmr);
1160 inb(acpi_p_blk + ACPI_P_LVL2);
1161 inl(pm_tmr); /* Dummy read, force synchronization with the PMU */
1162 time = TIME_END(pm_tmr, time);
1164 __sti();
1165 cx_statistics(2, time);
1166 if (time < acpi_p_lvl2_lat)
1167 goto sleep1;
1168 if (bm_activity(facp)) {
1169 clear_bm_activity(facp);
1170 continue;
1172 if (time > acpi_enter_lvl3_lat)
1173 goto sleep3;
1176 sleep1:
1177 sleep_level = 1;
1178 sleep_on_busmaster(facp);
1179 for (;;) {
1180 unsigned long time;
1181 unsigned int pm_tmr = facp->pm_tmr;
1183 __cli();
1184 if (current->need_resched)
1185 goto out;
1186 time = TIME_BEGIN(pm_tmr);
1187 __asm__ __volatile__("sti ; hlt": : :"memory");
1188 time = TIME_END(pm_tmr, time);
1189 cx_statistics(1, time);
1190 if (time > acpi_enter_lvl2_lat)
1191 goto sleep2;
1194 not_initialized:
1195 for (;;) {
1196 __cli();
1197 if (current->need_resched)
1198 goto out;
1199 __asm__ __volatile__("sti ; hlt": : :"memory");
1202 out:
1203 __sti();
1207 * Put all devices into specified D-state
1209 static int acpi_enter_dx(acpi_dstate_t state)
1211 int status = 0;
1213 if (state == ACPI_D0)
1214 status = pm_send_all(PM_RESUME, (void*) state);
1215 else
1216 status = pm_send_all(PM_SUSPEND, (void*) state);
1218 return status;
1222 * Update system time from real-time clock
1224 static void acpi_update_clock(void)
1226 if (acpi_sleep_start) {
1227 unsigned long delta;
1228 struct timeval tv;
1230 delta = get_cmos_time() - acpi_sleep_start;
1231 do_gettimeofday(&tv);
1232 tv.tv_sec += delta;
1233 do_settimeofday(&tv);
1235 acpi_sleep_start = 0;
1240 * Enter system sleep state
1242 static int acpi_enter_sx(acpi_sstate_t state)
1244 unsigned long slp_typ;
1245 u16 typa, typb, value;
1246 struct acpi_facp *facp;
1248 slp_typ = acpi_slp_typ[(int) state];
1249 if (slp_typ == ACPI_SLP_TYP_DISABLED)
1250 return -EPERM;
1252 // bits 8-15 are SLP_TYPa, bits 0-7 are SLP_TYPb
1253 typa = (slp_typ >> 8) & 0xff;
1254 typb = slp_typ & 0xff;
1256 typa = ((typa << ACPI_SLP_TYP_SHIFT) & ACPI_SLP_TYP_MASK);
1257 typb = ((typb << ACPI_SLP_TYP_SHIFT) & ACPI_SLP_TYP_MASK);
1259 acpi_sleep_start = get_cmos_time();
1260 acpi_enter_dx(ACPI_D3);
1261 // disable interrupts globally while suspended
1262 cli();
1263 acpi_sleep_state = state;
1265 facp = (struct acpi_facp*) acpi_facp.table;
1267 // clear wake status
1268 acpi_write_pm1_status(facp, ACPI_WAK);
1270 // set SLP_TYPa/b and SLP_EN
1271 if (facp->pm1a_cnt) {
1272 value = inw(facp->pm1a_cnt) & ~ACPI_SLP_TYP_MASK;
1273 outw(value | typa | ACPI_SLP_EN, facp->pm1a_cnt);
1275 if (facp->pm1b_cnt) {
1276 value = inw(facp->pm1b_cnt) & ~ACPI_SLP_TYP_MASK;
1277 outw(value | typb | ACPI_SLP_EN, facp->pm1b_cnt);
1280 // wait until S1 is entered
1281 while (!(acpi_read_pm1_status(facp) & ACPI_WAK)) ;
1282 // finished sleeping, update system time
1283 acpi_update_clock();
1284 acpi_enter_dx(ACPI_D0);
1285 // reenable interrupts globally after resume
1286 sti();
1287 acpi_sleep_state = ACPI_S0;
1289 return 0;
1293 * Enter soft-off (S5)
1295 static void acpi_power_off(void)
1297 acpi_enter_sx(ACPI_S5);
1301 * Claim I/O port if available
1303 static int acpi_claim(unsigned long start, unsigned long size)
1305 if (start && size)
1306 if (!request_region(start, size, "acpi"))
1307 return -EBUSY;
1308 return 0;
1312 * Claim ACPI I/O ports
1314 static int acpi_claim_ioports(struct acpi_facp *facp)
1316 // we don't get a guarantee of contiguity for any of the ACPI registers
1317 if (acpi_claim(facp->pm1a_evt, facp->pm1_evt_len))
1318 goto return_ebusy;
1319 if (acpi_claim(facp->pm1b_evt, facp->pm1_evt_len))
1320 goto release_pm1a_evt;
1321 if (acpi_claim(facp->pm1a_cnt, facp->pm1_cnt_len))
1322 goto release_pm1b_evt;
1323 if (acpi_claim(facp->pm1b_cnt, facp->pm1_cnt_len))
1324 goto release_pm1a_cnt;
1325 if (acpi_claim(facp->pm_tmr, facp->pm_tm_len))
1326 goto release_pm1b_cnt;
1327 if (acpi_claim(facp->gpe0, facp->gpe0_len))
1328 goto release_pm_tmr;
1329 if (acpi_claim(facp->gpe1, facp->gpe1_len))
1330 goto release_gpe0;
1331 return 0;
1332 release_gpe0: acpi_release(facp->gpe0, facp->gpe0_len);
1333 release_pm_tmr: acpi_release(facp->pm_tmr, facp->pm_tm_len);
1334 release_pm1b_cnt: acpi_release(facp->pm1b_cnt, facp->pm1_cnt_len);
1335 release_pm1a_cnt: acpi_release(facp->pm1a_cnt, facp->pm1_cnt_len);
1336 release_pm1b_evt: acpi_release(facp->pm1b_evt, facp->pm1_evt_len);
1337 release_pm1a_evt: acpi_release(facp->pm1a_evt, facp->pm1_evt_len);
1338 return_ebusy: return -EBUSY;
1342 * Release I/O port if claimed
1344 static void acpi_release(unsigned long start, unsigned long size)
1346 if (start && size)
1347 release_region(start, size);
1351 * Free ACPI I/O ports
1353 static int acpi_release_ioports(struct acpi_facp *facp)
1355 // we don't get a guarantee of contiguity for any of the ACPI registers
1356 acpi_release(facp->gpe1, facp->gpe1_len);
1357 acpi_release(facp->gpe0, facp->gpe0_len);
1358 acpi_release(facp->pm_tmr, facp->pm_tm_len);
1359 acpi_release(facp->pm1b_cnt, facp->pm1_cnt_len);
1360 acpi_release(facp->pm1a_cnt, facp->pm1_cnt_len);
1361 acpi_release(facp->pm1b_evt, facp->pm1_evt_len);
1362 acpi_release(facp->pm1a_evt, facp->pm1_evt_len);
1363 return 0;
1367 * Determine if modification of value is permitted
1369 static int
1370 acpi_verify_mod(int ctl_name)
1372 switch (ctl_name) {
1373 case ACPI_PM1_ENABLE:
1374 case ACPI_GPE_ENABLE:
1375 case ACPI_GPE_LEVEL:
1376 if (!acpi_sci_enabled())
1377 return -EPERM;
1378 break;
1379 case ACPI_P_LVL2_LAT:
1380 case ACPI_ENTER_LVL2_LAT:
1381 if (acpi_opts & ACPI_C2_DISABLED)
1382 return -EPERM;
1383 break;
1384 case ACPI_P_LVL3_LAT:
1385 case ACPI_ENTER_LVL3_LAT:
1386 if (acpi_opts & ACPI_C3_DISABLED)
1387 return -EPERM;
1388 break;
1389 case ACPI_S1_SLP_TYP:
1390 case ACPI_SLEEP:
1391 if (acpi_opts & ACPI_S1_DISABLED)
1392 return -EPERM;
1393 break;
1394 case ACPI_S5_SLP_TYP:
1395 if (acpi_opts & ACPI_S5_DISABLED)
1396 return -EPERM;
1397 break;
1399 return 0;
1403 * Examine/modify value
1405 static int acpi_do_ulong(ctl_table *ctl,
1406 int write,
1407 struct file *file,
1408 void *buffer,
1409 size_t *len)
1411 char str[2 * sizeof(unsigned long) + 4], *strend;
1412 unsigned long val;
1413 int size;
1415 if (!write) {
1416 if (file->f_pos) {
1417 *len = 0;
1418 return 0;
1421 val = *(unsigned long*) ctl->data;
1422 size = sprintf(str, "0x%08lx\n", val);
1423 if (*len >= size) {
1424 if (copy_to_user(buffer, str, size))
1425 return -EFAULT;
1426 *len = size;
1428 else
1429 *len = 0;
1431 else {
1432 if (acpi_verify_mod(ctl->ctl_name))
1433 return -EPERM;
1435 size = sizeof(str) - 1;
1436 if (size > *len)
1437 size = *len;
1438 if (copy_from_user(str, buffer, size))
1439 return -EFAULT;
1440 str[size] = '\0';
1441 val = simple_strtoul(str, &strend, 0);
1442 if (strend == str)
1443 return -EINVAL;
1444 *(unsigned long*) ctl->data = val;
1447 file->f_pos += *len;
1448 return 0;
1452 * Determine if user buffer contains a valid table
1454 static int acpi_verify_table(void *buffer,
1455 size_t size,
1456 struct acpi_table_info *info)
1458 struct acpi_table hdr;
1459 size_t table_size;
1461 if (size < sizeof(struct acpi_table))
1462 return -EINVAL;
1464 if (copy_from_user(&hdr, buffer, sizeof(hdr)))
1465 return -EFAULT;
1467 table_size = (size_t) hdr.length;
1468 if (hdr.signature != info->expected_signature
1469 || table_size < size
1470 || (info->expected_size
1471 && table_size != info->expected_size))
1472 return -EINVAL;
1474 return 0;
1478 * Examine/replace an ACPI table
1480 static int acpi_do_table(ctl_table *ctl,
1481 int write,
1482 struct file *file,
1483 void *buffer,
1484 size_t *len)
1486 struct acpi_table_info *info = (struct acpi_table_info *) ctl->data;
1487 u8 *data = NULL;
1488 size_t size = 0;
1489 int error = 0;
1491 if (!info) {
1492 *len = 0;
1493 return 0;
1496 if (!write) {
1497 // table read
1498 read_lock(&acpi_do_table_lock);
1499 if (info->table && file->f_pos < info->size) {
1500 data = (u8*) info->table + file->f_pos;
1501 size = info->size - file->f_pos;
1502 if (size > *len)
1503 size = *len;
1504 if (copy_to_user(buffer, data, size))
1505 error = -EFAULT;
1507 read_unlock(&acpi_do_table_lock);
1509 else if (file->f_pos) {
1510 // table body replacement
1511 write_lock(&acpi_do_table_lock);
1512 if (info->table && file->f_pos < info->size) {
1513 data = (u8*) info->table + file->f_pos;
1514 size = info->size - file->f_pos;
1515 if (size > *len)
1516 size = *len;
1517 if (copy_from_user(data, buffer, size))
1518 error = -EFAULT;
1520 write_unlock(&acpi_do_table_lock);
1522 else {
1523 // table header/body replacement
1524 struct acpi_table hdr;
1525 size_t table_size;
1527 // make sure we are being given a valid table
1528 error = acpi_verify_table(buffer, *len, info);
1529 if (error)
1530 return error;
1531 if (copy_from_user(&hdr, buffer, sizeof(hdr)))
1532 return -EFAULT;
1533 table_size = (size_t) hdr.length;
1535 write_lock(&acpi_do_table_lock);
1537 data = (u8*) info->table;
1538 size = *len;
1540 if (!data || info->mapped || table_size != info->size) {
1541 // allocate a (different sized) table
1542 data = kmalloc(table_size, GFP_KERNEL);
1543 if (data) {
1544 memset(data, 0, table_size);
1545 memcpy(data, &hdr, sizeof(hdr));
1546 acpi_destroy_table(info);
1547 acpi_init_table(info, data, 0);
1549 else
1550 error = -ENOMEM;
1552 if (data)
1553 if (copy_from_user(data, buffer, size)) {
1554 acpi_destroy_table(info);
1555 error = -EFAULT;
1558 write_unlock(&acpi_do_table_lock);
1561 if (error)
1562 return error;
1564 *len = size;
1565 file->f_pos += size;
1566 return 0;
1570 * Examine/modify event register
1572 static int acpi_do_event_reg(ctl_table *ctl,
1573 int write,
1574 struct file *file,
1575 void *buffer,
1576 size_t *len)
1578 struct acpi_facp *facp = (struct acpi_facp*) acpi_facp.table;
1579 char str[2 * sizeof(u32) + 4], *strend;
1580 u32 val, enabling;
1581 int size;
1583 if (!write) {
1584 if (file->f_pos) {
1585 *len = 0;
1586 return 0;
1589 val = 0;
1590 switch (ctl->ctl_name) {
1591 case ACPI_PM1_ENABLE:
1592 val = acpi_read_pm1_enable(facp);
1593 break;
1594 case ACPI_GPE_ENABLE:
1595 val = acpi_read_gpe_enable(facp);
1596 break;
1597 case ACPI_GPE_LEVEL:
1598 val = acpi_gpe_level;
1599 break;
1602 size = sprintf(str, "0x%08x\n", val);
1603 if (*len >= size) {
1604 if (copy_to_user(buffer, str, size))
1605 return -EFAULT;
1606 *len = size;
1608 else
1609 *len = 0;
1611 else
1613 if (acpi_verify_mod(ctl->ctl_name))
1614 return -EPERM;
1616 // fetch user value
1617 size = sizeof(str) - 1;
1618 if (size > *len)
1619 size = *len;
1620 if (copy_from_user(str, buffer, size))
1621 return -EFAULT;
1622 str[size] = '\0';
1623 val = (u32) simple_strtoul(str, &strend, 0);
1624 if (strend == str)
1625 return -EINVAL;
1627 // store value in register
1628 switch (ctl->ctl_name) {
1629 case ACPI_PM1_ENABLE:
1630 // clear previously disabled events
1631 enabling = (val & ~acpi_read_pm1_enable(facp));
1632 acpi_write_pm1_status(facp, enabling);
1634 if (val) {
1635 // enable ACPI unless it is already
1636 if (!acpi_is_enabled(facp))
1637 acpi_enable(facp);
1639 else if (!acpi_read_gpe_enable(facp)) {
1640 // disable ACPI unless it is already
1641 if (acpi_is_enabled(facp))
1642 acpi_disable(facp);
1645 acpi_write_pm1_enable(facp, val);
1646 break;
1647 case ACPI_GPE_ENABLE:
1648 // clear previously disabled events
1649 enabling = (val
1650 & ~acpi_read_gpe_enable(facp));
1651 while (acpi_read_gpe_status(facp) & enabling)
1652 acpi_write_gpe_status(facp, enabling);
1654 if (val) {
1655 // enable ACPI unless it is already
1656 if (!acpi_is_enabled(facp))
1657 acpi_enable(facp);
1659 else if (!acpi_read_pm1_enable(facp)) {
1660 // disable ACPI unless it is already
1661 if (acpi_is_enabled(facp))
1662 acpi_disable(facp);
1665 acpi_write_gpe_enable(facp, val);
1666 break;
1667 case ACPI_GPE_LEVEL:
1668 acpi_gpe_level = val;
1669 break;
1673 file->f_pos += *len;
1674 return 0;
1678 * Wait for next event
1680 static int acpi_do_event(ctl_table *ctl,
1681 int write,
1682 struct file *file,
1683 void *buffer,
1684 size_t *len)
1686 u32 pm1_status = 0, gpe_status = 0;
1687 acpi_sstate_t event_state = 0;
1688 char str[27];
1689 int size;
1691 if (write)
1692 return -EPERM;
1693 if (*len < sizeof(str)) {
1694 *len = 0;
1695 return 0;
1698 for (;;) {
1699 unsigned long flags;
1701 // we need an atomic exchange here
1702 spin_lock_irqsave(&acpi_event_lock, flags);
1703 pm1_status = acpi_pm1_status;
1704 acpi_pm1_status = 0;
1705 gpe_status = acpi_gpe_status;
1706 acpi_gpe_status = 0;
1707 spin_unlock_irqrestore(&acpi_event_lock, flags);
1708 event_state = acpi_event_state;
1710 if (pm1_status || gpe_status)
1711 break;
1713 // wait for an event to arrive
1714 interruptible_sleep_on(&acpi_event_wait);
1715 if (signal_pending(current))
1716 return -ERESTARTSYS;
1719 size = sprintf(str, "0x%08x 0x%08x 0x%01x\n",
1720 pm1_status,
1721 gpe_status,
1722 event_state);
1723 if (copy_to_user(buffer, str, size))
1724 return -EFAULT;
1725 *len = size;
1726 file->f_pos += size;
1728 return 0;
1732 * Enter system sleep state
1734 static int acpi_do_sleep(ctl_table *ctl,
1735 int write,
1736 struct file *file,
1737 void *buffer,
1738 size_t *len)
1740 if (!write) {
1741 if (file->f_pos) {
1742 *len = 0;
1743 return 0;
1746 else
1748 if (acpi_verify_mod(ctl->ctl_name) || acpi_enter_sx(ACPI_S1))
1749 return -EPERM;
1751 file->f_pos += *len;
1752 return 0;
1756 * Parse command line options
1758 static int __init acpi_setup(char *str)
1760 while (str && *str) {
1761 struct acpi_option_info *opt = acpi_options;
1762 while (opt->name) {
1763 if (!strncmp(str, opt->name, strlen(opt->name))) {
1764 acpi_opts |= opt->value;
1765 break;
1767 opt++;
1769 str = strpbrk(str, ",");
1770 if (str)
1771 str += strspn(str, ",");
1774 if (acpi_opts)
1775 printk(KERN_INFO "ACPI: options 0x%08lx\n", acpi_opts);
1777 return 1;
1781 * kernel/module command line interfaces are both "acpi=OPTION,OPTION,..."
1783 __setup("acpi=", acpi_setup);
1785 static char * __initdata acpi = NULL;
1787 MODULE_DESCRIPTION("ACPI driver");
1788 MODULE_PARM(acpi, "s");
1789 MODULE_PARM_DESC(acpi, "ACPI driver command line");
1792 * Initialize and enable ACPI
1794 int __init acpi_init(void)
1796 struct acpi_facp *facp = NULL;
1798 if (acpi)
1799 acpi_setup(acpi);
1801 if (acpi_opts & ACPI_DISABLED) {
1802 return -ENODEV;
1804 else if (acpi_opts & ACPI_TABLES_ONLY) {
1805 if (acpi_find_tables())
1806 return -ENODEV;
1808 else if (acpi_opts & ACPI_CHIPSET_ONLY) {
1809 if (acpi_find_chipset())
1810 return -ENODEV;
1812 else {
1813 switch (acpi_find_tables()) {
1814 case 0:
1815 // found valid ACPI tables
1816 break;
1817 case -ENODEV:
1818 // found no ACPI tables, try chipset-specific
1819 if (acpi_find_chipset())
1820 return -ENODEV;
1821 break;
1822 default:
1823 // found broken ACPI tables
1824 return -ENODEV;
1828 facp = (struct acpi_facp*) acpi_facp.table;
1830 if (PM_IS_ACTIVE()) {
1831 printk(KERN_NOTICE "acpi: APM is already active.\n");
1832 goto err_out;
1834 pm_active = 1;
1837 * Internally we always keep latencies in timer
1838 * ticks, which is simpler and more consistent (what is
1839 * an uS to us?). Besides, that gives people more
1840 * control in the /proc interfaces.
1842 if (facp->p_lvl2_lat
1843 && facp->p_lvl2_lat <= ACPI_MAX_P_LVL2_LAT
1844 && !acpi_verify_mod(ACPI_P_LVL2_LAT)) {
1845 acpi_p_lvl2_lat = ACPI_uS_TO_TMR_TICKS(facp->p_lvl2_lat);
1846 acpi_enter_lvl2_lat = ACPI_uS_TO_TMR_TICKS(ACPI_TMR_HZ / 1000);
1848 if (facp->p_lvl3_lat
1849 && facp->p_lvl3_lat <= ACPI_MAX_P_LVL3_LAT
1850 && !acpi_verify_mod(ACPI_P_LVL3_LAT)) {
1851 acpi_p_lvl3_lat = ACPI_uS_TO_TMR_TICKS(facp->p_lvl3_lat);
1852 acpi_enter_lvl3_lat
1853 = ACPI_uS_TO_TMR_TICKS(facp->p_lvl3_lat * 5);
1856 if (acpi_claim_ioports(facp)) {
1857 printk(KERN_ERR "ACPI: I/O port allocation failed\n");
1858 goto err_out;
1861 if (facp->sci_int
1862 && acpi_sci_enabled()
1863 && request_irq(facp->sci_int,
1864 acpi_irq,
1865 SA_INTERRUPT | SA_SHIRQ,
1866 "acpi",
1867 &acpi_facp)) {
1868 printk(KERN_ERR "ACPI: SCI (IRQ%d) allocation failed\n",
1869 facp->sci_int);
1870 goto cleanup_ioports;
1873 #ifndef CONFIG_ACPI_S1_SLEEP
1874 acpi_opts |= ACPI_S1_DISABLED;
1875 #endif
1877 acpi_sysctl = register_sysctl_table(acpi_dir_table, 1);
1878 if (!acpi_sysctl)
1879 goto cleanup_irq;
1881 pm_power_off = acpi_power_off;
1884 * Set up the ACPI idle function. Note that we can't really
1885 * do this with multiple CPU's, we'd need a per-CPU ACPI
1886 * device..
1888 #ifdef CONFIG_SMP
1889 if (smp_num_cpus > 1)
1890 return 0;
1891 #endif
1893 if (facp->pm_tmr)
1894 pm_idle = acpi_idle;
1896 return 0;
1897 cleanup_irq:
1898 free_irq(facp->sci_int, &acpi_facp);
1899 cleanup_ioports:
1900 acpi_release_ioports(facp);
1901 err_out:
1902 if (pci_driver_registered)
1903 pci_unregister_driver(&acpi_driver);
1904 acpi_destroy_tables();
1906 return -ENODEV;
1910 * Disable and deinitialize ACPI
1912 void __exit acpi_exit(void)
1914 struct acpi_facp *facp = (struct acpi_facp*) acpi_facp.table;
1916 pm_idle = NULL;
1917 pm_power_off = NULL;
1919 unregister_sysctl_table(acpi_sysctl);
1920 acpi_disable(facp);
1921 acpi_release_ioports(facp);
1923 if (facp->sci_int && acpi_sci_enabled())
1924 free_irq(facp->sci_int, &acpi_facp);
1926 acpi_destroy_tables();
1928 if (pci_driver_registered)
1929 pci_unregister_driver(&acpi_driver);
1931 pm_active = 0;
1934 module_init(acpi_init);
1935 module_exit(acpi_exit);