Merge from vendor branch LIBEVENT:
[dragonfly.git] / sys / platform / pc32 / acpica5 / acpi_wakeup.c
1 /*-
2  * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3  * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/i386/acpica/acpi_wakeup.c,v 1.33 2004/05/06 02:18:58 njl Exp $
28  * $DragonFly: src/sys/platform/pc32/acpica5/acpi_wakeup.c,v 1.15 2007/05/28 18:55:41 dillon Exp $
29  */
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/bus.h>
35 #include <sys/lock.h>
36 #include <sys/proc.h>
37 #include <sys/sysctl.h>
38
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 #include <vm/vm_object.h>
42 #include <vm/vm_page.h>
43 #include <vm/vm_map.h>
44
45 #include <machine/cpufunc.h>
46 #include <machine/segments.h>
47 #include <machine_base/isa/intr_machdep.h>
48
49 #include "acpi.h"
50 #include <dev/acpica5/acpivar.h>
51
52 #include "acpi_wakecode.h"
53
54 #if __FreeBSD_version < 500000
55 #define vm_page_lock_queues()
56 #define vm_page_unlock_queues()
57 #endif
58
59 extern uint32_t acpi_reset_video;
60 extern void     initializecpu(void);
61
62 static struct region_descriptor r_idt, r_gdt, *p_gdt;
63 static uint16_t         r_ldt;
64
65 static uint32_t         r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi,
66                         r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr;
67
68 static uint16_t         r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
69 static uint32_t         r_esp;
70
71 static void             acpi_printcpu(void);
72 static void             acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
73                                           int nsegs, int error);
74 static void             acpi_alloc_wakeup_handler(void);
75
76 /* XXX shut gcc up */
77 extern int              acpi_savecpu(void);
78 extern int              acpi_restorecpu(void);
79
80 #if defined(__GNUC__) || defined(__INTEL_COMPILER)
81 __asm__("                               \n\
82         .text                           \n\
83         .p2align 2, 0x90                \n\
84         .type acpi_restorecpu, @function\n\
85 acpi_restorecpu:                        \n\
86         .align 4                        \n\
87         movl    r_eax,%eax              \n\
88         movl    r_ebx,%ebx              \n\
89         movl    r_ecx,%ecx              \n\
90         movl    r_edx,%edx              \n\
91         movl    r_ebp,%ebp              \n\
92         movl    r_esi,%esi              \n\
93         movl    r_edi,%edi              \n\
94         movl    r_esp,%esp              \n\
95                                         \n\
96         pushl   r_efl                   \n\
97         popfl                           \n\
98                                         \n\
99         movl    ret_addr,%eax           \n\
100         movl    %eax,(%esp)             \n\
101         xorl    %eax,%eax               \n\
102         ret                             \n\
103                                         \n\
104         .text                           \n\
105         .p2align 2, 0x90                \n\
106         .type acpi_savecpu, @function   \n\
107 acpi_savecpu:                           \n\
108         movw    %cs,r_cs                \n\
109         movw    %ds,r_ds                \n\
110         movw    %es,r_es                \n\
111         movw    %fs,r_fs                \n\
112         movw    %gs,r_gs                \n\
113         movw    %ss,r_ss                \n\
114                                         \n\
115         movl    %eax,r_eax              \n\
116         movl    %ebx,r_ebx              \n\
117         movl    %ecx,r_ecx              \n\
118         movl    %edx,r_edx              \n\
119         movl    %ebp,r_ebp              \n\
120         movl    %esi,r_esi              \n\
121         movl    %edi,r_edi              \n\
122                                         \n\
123         movl    %cr0,%eax               \n\
124         movl    %eax,r_cr0              \n\
125         movl    %cr2,%eax               \n\
126         movl    %eax,r_cr2              \n\
127         movl    %cr3,%eax               \n\
128         movl    %eax,r_cr3              \n\
129         movl    %cr4,%eax               \n\
130         movl    %eax,r_cr4              \n\
131                                         \n\
132         pushfl                          \n\
133         popl    r_efl                   \n\
134                                         \n\
135         movl    %esp,r_esp              \n\
136                                         \n\
137         sgdt    r_gdt                   \n\
138         sidt    r_idt                   \n\
139         sldt    r_ldt                   \n\
140         str     r_tr                    \n\
141                                         \n\
142         movl    (%esp),%eax             \n\
143         movl    %eax,ret_addr           \n\
144         movl    $1,%eax                 \n\
145         ret                             \n\
146 ");
147 #endif /* __GNUC__ || __INTEL_COMPILER */
148
149 static void
150 acpi_printcpu(void)
151 {
152         kprintf("======== acpi_printcpu() debug dump ========\n");
153         kprintf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n",
154                 r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base,
155                 r_ldt, r_tr, r_efl);
156         kprintf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n",
157                 r_eax, r_ebx, r_ecx, r_edx);
158         kprintf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n",
159                 r_esi, r_edi, r_ebp, r_esp);
160         kprintf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n",
161                 r_cr0, r_cr2, r_cr3, r_cr4);
162         kprintf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n",
163                 r_cs, r_ds, r_es, r_fs, r_gs, r_ss);
164 }
165
166 #define WAKECODE_FIXUP(offset, type, val) do    {               \
167         type    *addr;                                          \
168         addr = (type *)(sc->acpi_wakeaddr + offset);            \
169         *addr = val;                                            \
170 } while (0)
171
172 #define WAKECODE_BCOPY(offset, type, val) do    {               \
173         void    *addr;                                          \
174         addr = (void *)(sc->acpi_wakeaddr + offset);            \
175         bcopy(&(val), addr, sizeof(type));                      \
176 } while (0)
177
178 int
179 acpi_sleep_machdep(struct acpi_softc *sc, int state)
180 {
181         ACPI_STATUS             status;
182         vm_paddr_t              oldphys;
183         struct pmap             *pm;
184         vm_page_t               page;
185         static vm_page_t        opage = NULL;
186         int                     ret = 0;
187         int                     pteobj_allocated = 0;
188         uint32_t                cr3;
189         u_long                  ef;
190         struct proc             *p;
191
192         if (sc->acpi_wakeaddr == 0)
193                 return (0);
194
195         AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
196
197         ef = read_eflags();
198         ACPI_DISABLE_IRQS();
199
200         /* Create Identity Mapping */
201         if ((p = curproc) == NULL)
202                 p = &proc0;
203         pm = vmspace_pmap(p->p_vmspace);
204         cr3 = rcr3();
205 #ifdef PAE
206         load_cr3(vtophys(pm->pm_pdpt));
207 #else
208         load_cr3(vtophys(pm->pm_pdir));
209 #endif
210         /*
211          * note: DragonFly still uses the VM object (FreeBSD-5 no longer uses
212          * the VM object).
213          */
214         if (pm->pm_pteobj == NULL) {
215                 pm->pm_pteobj = vm_object_allocate(OBJT_DEFAULT, PTDPTDI + 1);
216                 pteobj_allocated = 1;
217         }
218
219         oldphys = pmap_extract(pm, sc->acpi_wakephys);
220         if (oldphys)
221                 opage = PHYS_TO_VM_PAGE(oldphys);
222         page = PHYS_TO_VM_PAGE(sc->acpi_wakephys);
223         pmap_enter(pm, sc->acpi_wakephys, page,
224                    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1);
225
226         ret_addr = 0;
227         if (acpi_savecpu()) {
228                 /* Execute Sleep */
229                 cpu_disable_intr();
230
231                 p_gdt = (struct region_descriptor *)
232                                 (sc->acpi_wakeaddr + physical_gdt);
233                 p_gdt->rd_limit = r_gdt.rd_limit;
234                 p_gdt->rd_base = vtophys(r_gdt.rd_base);
235
236                 WAKECODE_FIXUP(physical_esp, uint32_t, vtophys(r_esp));
237                 WAKECODE_FIXUP(previous_cr0, uint32_t, r_cr0);
238                 WAKECODE_FIXUP(previous_cr2, uint32_t, r_cr2);
239                 WAKECODE_FIXUP(previous_cr3, uint32_t, r_cr3);
240                 WAKECODE_FIXUP(previous_cr4, uint32_t, r_cr4);
241
242                 WAKECODE_FIXUP(reset_video, uint32_t, acpi_reset_video);
243
244                 WAKECODE_FIXUP(previous_tr,  uint16_t, r_tr);
245                 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, r_gdt);
246                 WAKECODE_FIXUP(previous_ldt, uint16_t, r_ldt);
247                 WAKECODE_BCOPY(previous_idt, struct region_descriptor, r_idt);
248
249                 WAKECODE_FIXUP(where_to_recover, void *, acpi_restorecpu);
250
251                 WAKECODE_FIXUP(previous_ds,  uint16_t, r_ds);
252                 WAKECODE_FIXUP(previous_es,  uint16_t, r_es);
253                 WAKECODE_FIXUP(previous_fs,  uint16_t, r_fs);
254                 WAKECODE_FIXUP(previous_gs,  uint16_t, r_gs);
255                 WAKECODE_FIXUP(previous_ss,  uint16_t, r_ss);
256
257                 if (bootverbose)
258                         acpi_printcpu();
259
260                 /* Call ACPICA to enter the desired sleep state */
261                 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
262                         status = AcpiEnterSleepStateS4bios();
263                 else
264                         status = AcpiEnterSleepState(state);
265
266                 if (status != AE_OK) {
267                         device_printf(sc->acpi_dev,
268                                 "AcpiEnterSleepState failed - %s\n",
269                                 AcpiFormatException(status));
270                         ret = -1;
271                         goto out;
272                 }
273
274                 for (;;) ;
275         } else {
276                 /* Execute Wakeup */
277 #if 0
278                 initializecpu();
279 #endif
280                 icu_reinit();
281                 cpu_enable_intr();
282
283                 if (bootverbose) {
284                         acpi_savecpu();
285                         acpi_printcpu();
286                 }
287         }
288
289 out:
290         vm_page_lock_queues();
291         pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE);
292         vm_page_unlock_queues();
293         if (opage) {
294                 pmap_enter(pm, sc->acpi_wakephys, page,
295                            VM_PROT_READ | VM_PROT_WRITE, 0);
296         }
297
298         if (pteobj_allocated) {
299                 vm_object_deallocate(pm->pm_pteobj);
300                 pm->pm_pteobj = NULL;
301         }
302         load_cr3(cr3);
303
304         write_eflags(ef);
305
306         return (ret);
307 }
308
309 static bus_dma_tag_t    acpi_waketag;
310 static bus_dmamap_t     acpi_wakemap;
311 static vm_offset_t      acpi_wakeaddr = 0;
312
313 static void
314 acpi_alloc_wakeup_handler(void)
315 {
316         if (!cold)
317                 return;
318
319         if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0,
320                                /* lowaddr below 1MB */ 0x9ffff,
321                                /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL,
322                                 PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) {
323                 kprintf("acpi_alloc_wakeup_handler: can't create wake tag\n");
324                 return;
325         }
326
327         if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr,
328                              BUS_DMA_NOWAIT, &acpi_wakemap)) {
329                 kprintf("acpi_alloc_wakeup_handler: can't alloc wake memory\n");
330                 return;
331         }
332 }
333
334 SYSINIT(acpiwakeup, SI_BOOT1_POST, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0)
335
336 static void
337 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
338 {
339         struct acpi_softc       *sc = arg;
340         uint32_t                *addr;
341
342         addr = (uint32_t *)&wakecode[wakeup_sw32 + 2];
343         *addr = segs[0].ds_addr + wakeup_32;
344         bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
345         sc->acpi_wakephys = segs[0].ds_addr;
346 }
347
348 void
349 acpi_install_wakeup_handler(struct acpi_softc *sc)
350 {
351         if (acpi_wakeaddr == 0)
352                 return;
353
354         sc->acpi_waketag = acpi_waketag;
355         sc->acpi_wakeaddr = acpi_wakeaddr;
356         sc->acpi_wakemap = acpi_wakemap;
357
358         bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
359                         (void *)sc->acpi_wakeaddr, PAGE_SIZE,
360                         acpi_realmodeinst, sc, 0);
361 }