]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/amd64/acpica/acpi_machdep.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / amd64 / acpica / acpi_machdep.c
1 /*-
2  * Copyright (c) 2001 Mitsuru IWASAKI
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/sysctl.h>
35 #include <vm/vm.h>
36 #include <vm/pmap.h>
37
38 #include <contrib/dev/acpica/include/acpi.h>
39 #include <contrib/dev/acpica/include/accommon.h>
40 #include <contrib/dev/acpica/include/actables.h>
41
42 #include <dev/acpica/acpivar.h>
43
44 #include <machine/nexusvar.h>
45
46 SYSCTL_DECL(_debug_acpi);
47
48 int acpi_resume_beep;
49 TUNABLE_INT("debug.acpi.resume_beep", &acpi_resume_beep);
50 SYSCTL_INT(_debug_acpi, OID_AUTO, resume_beep, CTLFLAG_RW, &acpi_resume_beep,
51     0, "Beep the PC speaker when resuming");
52
53 int acpi_reset_video;
54 TUNABLE_INT("hw.acpi.reset_video", &acpi_reset_video);
55
56 static int intr_model = ACPI_INTR_PIC;
57 static struct apm_clone_data acpi_clone;
58
59 int
60 acpi_machdep_init(device_t dev)
61 {
62         struct acpi_softc       *sc;
63
64         sc = devclass_get_softc(devclass_find("acpi"), 0);
65
66         /* Create a fake clone for /dev/acpi. */
67         STAILQ_INIT(&sc->apm_cdevs);
68         acpi_clone.cdev = sc->acpi_dev_t;
69         acpi_clone.acpi_sc = sc;
70         ACPI_LOCK(acpi);
71         STAILQ_INSERT_TAIL(&sc->apm_cdevs, &acpi_clone, entries);
72         ACPI_UNLOCK(acpi);
73         sc->acpi_clone = &acpi_clone;
74         acpi_install_wakeup_handler(sc);
75
76         if (intr_model != ACPI_INTR_PIC)
77                 acpi_SetIntrModel(intr_model);
78
79         SYSCTL_ADD_UINT(&sc->acpi_sysctl_ctx,
80             SYSCTL_CHILDREN(sc->acpi_sysctl_tree), OID_AUTO,
81             "reset_video", CTLFLAG_RW, &acpi_reset_video, 0,
82             "Call the VESA reset BIOS vector on the resume path");
83
84         return (0);
85 }
86
87 void
88 acpi_SetDefaultIntrModel(int model)
89 {
90
91         intr_model = model;
92 }
93
94 int
95 acpi_machdep_quirks(int *quirks)
96 {
97         return (0);
98 }
99
100 void
101 acpi_cpu_c1()
102 {
103         __asm __volatile("sti; hlt");
104 }
105
106 /*
107  * Support for mapping ACPI tables during early boot.  Currently this
108  * uses the crashdump map to map each table.  However, the crashdump
109  * map is created in pmap_bootstrap() right after the direct map, so
110  * we should be able to just use pmap_mapbios() here instead.
111  *
112  * This makes the following assumptions about how we use this KVA:
113  * pages 0 and 1 are used to map in the header of each table found via
114  * the RSDT or XSDT and pages 2 to n are used to map in the RSDT or
115  * XSDT.  This has to use 2 pages for the table headers in case a
116  * header spans a page boundary.
117  *
118  * XXX: We don't ensure the table fits in the available address space
119  * in the crashdump map.
120  */
121
122 /*
123  * Map some memory using the crashdump map.  'offset' is an offset in
124  * pages into the crashdump map to use for the start of the mapping.
125  */
126 static void *
127 table_map(vm_paddr_t pa, int offset, vm_offset_t length)
128 {
129         vm_offset_t va, off;
130         void *data;
131
132         off = pa & PAGE_MASK;
133         length = roundup(length + off, PAGE_SIZE);
134         pa = pa & PG_FRAME;
135         va = (vm_offset_t)pmap_kenter_temporary(pa, offset) +
136             (offset * PAGE_SIZE);
137         data = (void *)(va + off);
138         length -= PAGE_SIZE;
139         while (length > 0) {
140                 va += PAGE_SIZE;
141                 pa += PAGE_SIZE;
142                 length -= PAGE_SIZE;
143                 pmap_kenter(va, pa);
144                 invlpg(va);
145         }
146         return (data);
147 }
148
149 /* Unmap memory previously mapped with table_map(). */
150 static void
151 table_unmap(void *data, vm_offset_t length)
152 {
153         vm_offset_t va, off;
154
155         va = (vm_offset_t)data;
156         off = va & PAGE_MASK;
157         length = roundup(length + off, PAGE_SIZE);
158         va &= ~PAGE_MASK;
159         while (length > 0) {
160                 pmap_kremove(va);
161                 invlpg(va);
162                 va += PAGE_SIZE;
163                 length -= PAGE_SIZE;
164         }
165 }
166
167 /*
168  * Map a table at a given offset into the crashdump map.  It first
169  * maps the header to determine the table length and then maps the
170  * entire table.
171  */
172 static void *
173 map_table(vm_paddr_t pa, int offset, const char *sig)
174 {
175         ACPI_TABLE_HEADER *header;
176         vm_offset_t length;
177         void *table;
178
179         header = table_map(pa, offset, sizeof(ACPI_TABLE_HEADER));
180         if (strncmp(header->Signature, sig, ACPI_NAME_SIZE) != 0) {
181                 table_unmap(header, sizeof(ACPI_TABLE_HEADER));
182                 return (NULL);
183         }
184         length = header->Length;
185         table_unmap(header, sizeof(ACPI_TABLE_HEADER));
186         table = table_map(pa, offset, length);
187         if (ACPI_FAILURE(AcpiTbChecksum(table, length))) {
188                 if (bootverbose)
189                         printf("ACPI: Failed checksum for table %s\n", sig);
190 #if (ACPI_CHECKSUM_ABORT)
191                 table_unmap(table, length);
192                 return (NULL);
193 #endif
194         }
195         return (table);
196 }
197
198 /*
199  * See if a given ACPI table is the requested table.  Returns the
200  * length of the able if it matches or zero on failure.
201  */
202 static int
203 probe_table(vm_paddr_t address, const char *sig)
204 {
205         ACPI_TABLE_HEADER *table;
206
207         table = table_map(address, 0, sizeof(ACPI_TABLE_HEADER));
208         if (table == NULL) {
209                 if (bootverbose)
210                         printf("ACPI: Failed to map table at 0x%jx\n",
211                             (uintmax_t)address);
212                 return (0);
213         }
214         if (bootverbose)
215                 printf("Table '%.4s' at 0x%jx\n", table->Signature,
216                     (uintmax_t)address);
217
218         if (strncmp(table->Signature, sig, ACPI_NAME_SIZE) != 0) {
219                 table_unmap(table, sizeof(ACPI_TABLE_HEADER));
220                 return (0);
221         }
222         table_unmap(table, sizeof(ACPI_TABLE_HEADER));
223         return (1);
224 }
225
226 /*
227  * Try to map a table at a given physical address previously returned
228  * by acpi_find_table().
229  */
230 void *
231 acpi_map_table(vm_paddr_t pa, const char *sig)
232 {
233
234         return (map_table(pa, 0, sig));
235 }
236
237 /* Unmap a table previously mapped via acpi_map_table(). */
238 void
239 acpi_unmap_table(void *table)
240 {
241         ACPI_TABLE_HEADER *header;
242
243         header = (ACPI_TABLE_HEADER *)table;
244         table_unmap(table, header->Length);
245 }
246
247 /*
248  * Return the physical address of the requested table or zero if one
249  * is not found.
250  */
251 vm_paddr_t
252 acpi_find_table(const char *sig)
253 {
254         ACPI_PHYSICAL_ADDRESS rsdp_ptr;
255         ACPI_TABLE_RSDP *rsdp;
256         ACPI_TABLE_RSDT *rsdt;
257         ACPI_TABLE_XSDT *xsdt;
258         ACPI_TABLE_HEADER *table;
259         vm_paddr_t addr;
260         int i, count;
261
262         if (resource_disabled("acpi", 0))
263                 return (0);
264
265         /*
266          * Map in the RSDP.  Since ACPI uses AcpiOsMapMemory() which in turn
267          * calls pmap_mapbios() to find the RSDP, we assume that we can use
268          * pmap_mapbios() to map the RSDP.
269          */
270         if ((rsdp_ptr = AcpiOsGetRootPointer()) == 0)
271                 return (0);
272         rsdp = pmap_mapbios(rsdp_ptr, sizeof(ACPI_TABLE_RSDP));
273         if (rsdp == NULL) {
274                 if (bootverbose)
275                         printf("ACPI: Failed to map RSDP\n");
276                 return (0);
277         }
278
279         /*
280          * For ACPI >= 2.0, use the XSDT if it is available.
281          * Otherwise, use the RSDT.  We map the XSDT or RSDT at page 2
282          * in the crashdump area.  Pages 0 and 1 are used to map in the
283          * headers of candidate ACPI tables.
284          */
285         addr = 0;
286         if (rsdp->Revision >= 2 && rsdp->XsdtPhysicalAddress != 0) {
287                 /*
288                  * AcpiOsGetRootPointer only verifies the checksum for
289                  * the version 1.0 portion of the RSDP.  Version 2.0 has
290                  * an additional checksum that we verify first.
291                  */
292                 if (AcpiTbChecksum((UINT8 *)rsdp, ACPI_RSDP_XCHECKSUM_LENGTH)) {
293                         if (bootverbose)
294                                 printf("ACPI: RSDP failed extended checksum\n");
295                         return (0);
296                 }
297                 xsdt = map_table(rsdp->XsdtPhysicalAddress, 2, ACPI_SIG_XSDT);
298                 if (xsdt == NULL) {
299                         if (bootverbose)
300                                 printf("ACPI: Failed to map XSDT\n");
301                         return (0);
302                 }
303                 count = (xsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
304                     sizeof(UINT64);
305                 for (i = 0; i < count; i++)
306                         if (probe_table(xsdt->TableOffsetEntry[i], sig)) {
307                                 addr = xsdt->TableOffsetEntry[i];
308                                 break;
309                         }
310                 acpi_unmap_table(xsdt);
311         } else {
312                 rsdt = map_table(rsdp->RsdtPhysicalAddress, 2, ACPI_SIG_RSDT);
313                 if (rsdt == NULL) {
314                         if (bootverbose)
315                                 printf("ACPI: Failed to map RSDT\n");
316                         return (0);
317                 }
318                 count = (rsdt->Header.Length - sizeof(ACPI_TABLE_HEADER)) /
319                     sizeof(UINT32);
320                 for (i = 0; i < count; i++)
321                         if (probe_table(rsdt->TableOffsetEntry[i], sig)) {
322                                 addr = rsdt->TableOffsetEntry[i];
323                                 break;
324                         }
325                 acpi_unmap_table(rsdt);
326         }
327         pmap_unmapbios((vm_offset_t)rsdp, sizeof(ACPI_TABLE_RSDP));
328         if (addr == 0) {
329                 if (bootverbose)
330                         printf("ACPI: No %s table found\n", sig);
331                 return (0);
332         }
333         if (bootverbose)
334                 printf("%s: Found table at 0x%jx\n", sig, (uintmax_t)addr);
335
336         /*
337          * Verify that we can map the full table and that its checksum is
338          * correct, etc.
339          */
340         table = map_table(addr, 0, sig);
341         if (table == NULL)
342                 return (0);
343         acpi_unmap_table(table);
344
345         return (addr);
346 }
347
348 /*
349  * ACPI nexus(4) driver.
350  */
351 static int
352 nexus_acpi_probe(device_t dev)
353 {
354         int error;
355
356         error = acpi_identify();
357         if (error)
358                 return (error);
359
360         return (BUS_PROBE_DEFAULT);
361 }
362
363 static int
364 nexus_acpi_attach(device_t dev)
365 {
366
367         nexus_init_resources();
368         bus_generic_probe(dev);
369         if (BUS_ADD_CHILD(dev, 10, "acpi", 0) == NULL)
370                 panic("failed to add acpi0 device");
371
372         return (bus_generic_attach(dev));
373 }
374
375 static device_method_t nexus_acpi_methods[] = {
376         /* Device interface */
377         DEVMETHOD(device_probe,         nexus_acpi_probe),
378         DEVMETHOD(device_attach,        nexus_acpi_attach),
379
380         { 0, 0 }
381 };
382
383 DEFINE_CLASS_1(nexus, nexus_acpi_driver, nexus_acpi_methods, 1, nexus_driver);
384 static devclass_t nexus_devclass;
385
386 DRIVER_MODULE(nexus_acpi, root, nexus_acpi_driver, nexus_devclass, 0, 0);