Bring in the FreeBSD-5 ACPICA code as a module. Note: not hooked up yet,
[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.30 2003/11/03 22:18:57 jhb Exp $
28  * $DragonFly: src/sys/platform/pc32/acpica5/acpi_wakeup.c,v 1.1 2004/02/21 06:48:05 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/vm_param.h>
41 #include <vm/pmap.h>
42 #include <vm/vm_object.h>
43 #include <vm/vm_page.h>
44 #include <vm/vm_map.h>
45 #include <vm/vm_kern.h>
46 #include <vm/vm_extern.h>
47
48 #include <machine/bus.h>
49 #include <machine/cpufunc.h>
50 #include <machine/segments.h>
51
52 #include "acpi.h"
53 #include <dev/acpica/acpivar.h>
54
55 #include "acpi_wakecode.h"
56
57 #if __FreeBSD_version < 500000
58 #define vm_page_lock_queues()
59 #define vm_page_unlock_queues()
60 #endif
61
62 extern uint32_t acpi_reset_video;
63 extern void     initializecpu(void);
64
65 static struct region_descriptor r_idt, r_gdt, *p_gdt;
66 static u_int16_t        r_ldt;
67
68 static u_int32_t        r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi,
69                         r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr;
70
71 static u_int16_t        r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
72 static u_int32_t        r_esp = 0;
73
74 static void             acpi_printcpu(void);
75 static void             acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
76                                           int nsegs, int error);
77 static void             acpi_alloc_wakeup_handler(void);
78
79 /* XXX shut gcc up */
80 extern int              acpi_savecpu(void);
81 extern int              acpi_restorecpu(void);
82
83 #ifdef __GNUC__
84 __asm__("                               \n\
85         .text                           \n\
86         .p2align 2, 0x90                \n\
87         .type acpi_restorecpu, @function\n\
88 acpi_restorecpu:                        \n\
89         .align 4                        \n\
90         movl    r_eax,%eax              \n\
91         movl    r_ebx,%ebx              \n\
92         movl    r_ecx,%ecx              \n\
93         movl    r_edx,%edx              \n\
94         movl    r_ebp,%ebp              \n\
95         movl    r_esi,%esi              \n\
96         movl    r_edi,%edi              \n\
97         movl    r_esp,%esp              \n\
98                                         \n\
99         pushl   r_efl                   \n\
100         popfl                           \n\
101                                         \n\
102         movl    ret_addr,%eax           \n\
103         movl    %eax,(%esp)             \n\
104         xorl    %eax,%eax               \n\
105         ret                             \n\
106                                         \n\
107         .text                           \n\
108         .p2align 2, 0x90                \n\
109         .type acpi_savecpu, @function   \n\
110 acpi_savecpu:                           \n\
111         movw    %cs,r_cs                \n\
112         movw    %ds,r_ds                \n\
113         movw    %es,r_es                \n\
114         movw    %fs,r_fs                \n\
115         movw    %gs,r_gs                \n\
116         movw    %ss,r_ss                \n\
117                                         \n\
118         movl    %eax,r_eax              \n\
119         movl    %ebx,r_ebx              \n\
120         movl    %ecx,r_ecx              \n\
121         movl    %edx,r_edx              \n\
122         movl    %ebp,r_ebp              \n\
123         movl    %esi,r_esi              \n\
124         movl    %edi,r_edi              \n\
125                                         \n\
126         movl    %cr0,%eax               \n\
127         movl    %eax,r_cr0              \n\
128         movl    %cr2,%eax               \n\
129         movl    %eax,r_cr2              \n\
130         movl    %cr3,%eax               \n\
131         movl    %eax,r_cr3              \n\
132         movl    %cr4,%eax               \n\
133         movl    %eax,r_cr4              \n\
134                                         \n\
135         pushfl                          \n\
136         popl    r_efl                   \n\
137                                         \n\
138         movl    %esp,r_esp              \n\
139                                         \n\
140         sgdt    r_gdt                   \n\
141         sidt    r_idt                   \n\
142         sldt    r_ldt                   \n\
143         str     r_tr                    \n\
144                                         \n\
145         movl    (%esp),%eax             \n\
146         movl    %eax,ret_addr           \n\
147         movl    $1,%eax                 \n\
148         ret                             \n\
149 ");
150 #endif /* __GNUC__ */
151
152 static void
153 acpi_printcpu(void)
154 {
155         printf("======== acpi_printcpu() debug dump ========\n");
156         printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n",
157                 r_gdt.rd_limit, r_gdt.rd_base, r_idt.rd_limit, r_idt.rd_base,
158                 r_ldt, r_tr, r_efl);
159         printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n",
160                 r_eax, r_ebx, r_ecx, r_edx);
161         printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n",
162                 r_esi, r_edi, r_ebp, r_esp);
163         printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n",
164                 r_cr0, r_cr2, r_cr3, r_cr4);
165         printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n",
166                 r_cs, r_ds, r_es, r_fs, r_gs, r_ss);
167 }
168
169 #define WAKECODE_FIXUP(offset, type, val) do    {               \
170         void    **addr;                                         \
171         addr = (void **)(sc->acpi_wakeaddr + offset);           \
172         (type *)*addr = val;                                    \
173 } while (0)
174
175 #define WAKECODE_BCOPY(offset, type, val) do    {               \
176         void    **addr;                                         \
177         addr = (void **)(sc->acpi_wakeaddr + offset);           \
178         bcopy(&(val), addr, sizeof(type));                      \
179 } while (0)
180
181 int
182 acpi_sleep_machdep(struct acpi_softc *sc, int state)
183 {
184         ACPI_STATUS             status;
185         vm_paddr_t              oldphys;
186         struct pmap             *pm;
187         vm_page_t               page;
188         static vm_page_t        opage = NULL;
189         int                     ret = 0;
190         u_int32_t               cr3;
191         u_long                  ef;
192         struct proc             *p;
193
194         if (sc->acpi_wakeaddr == 0)
195                 return (0);
196
197         AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
198
199         ef = read_eflags();
200         ACPI_DISABLE_IRQS();
201
202         /* Create Identity Mapping */
203         if ((p = curproc) == NULL)
204                 p = &proc0;
205         pm = vmspace_pmap(p->p_vmspace);
206         cr3 = rcr3();
207 #ifdef PAE
208         load_cr3(vtophys(pm->pm_pdpt));
209 #else
210         load_cr3(vtophys(pm->pm_pdir));
211 #endif
212
213         oldphys = pmap_extract(pm, sc->acpi_wakephys);
214         if (oldphys)
215                 opage = PHYS_TO_VM_PAGE(oldphys);
216         page = PHYS_TO_VM_PAGE(sc->acpi_wakephys);
217         pmap_enter(pm, sc->acpi_wakephys, page,
218                    VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE, 1);
219
220         ret_addr = 0;
221         if (acpi_savecpu()) {
222                 /* Execute Sleep */
223                 cpu_disable_intr();
224
225                 p_gdt = (struct region_descriptor *)
226                                 (sc->acpi_wakeaddr + physical_gdt);
227                 p_gdt->rd_limit = r_gdt.rd_limit;
228                 p_gdt->rd_base = vtophys(r_gdt.rd_base);
229
230                 WAKECODE_FIXUP(physical_esp, u_int32_t, vtophys(r_esp));
231                 WAKECODE_FIXUP(previous_cr0, u_int32_t, r_cr0);
232                 WAKECODE_FIXUP(previous_cr2, u_int32_t, r_cr2);
233                 WAKECODE_FIXUP(previous_cr3, u_int32_t, r_cr3);
234                 WAKECODE_FIXUP(previous_cr4, u_int32_t, r_cr4);
235
236                 WAKECODE_FIXUP(reset_video, u_int32_t, acpi_reset_video);
237
238                 WAKECODE_FIXUP(previous_tr,  u_int16_t, r_tr);
239                 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, r_gdt);
240                 WAKECODE_FIXUP(previous_ldt, u_int16_t, r_ldt);
241                 WAKECODE_BCOPY(previous_idt, struct region_descriptor, r_idt);
242
243                 WAKECODE_FIXUP(where_to_recover, void, acpi_restorecpu);
244
245                 WAKECODE_FIXUP(previous_ds,  u_int16_t, r_ds);
246                 WAKECODE_FIXUP(previous_es,  u_int16_t, r_es);
247                 WAKECODE_FIXUP(previous_fs,  u_int16_t, r_fs);
248                 WAKECODE_FIXUP(previous_gs,  u_int16_t, r_gs);
249                 WAKECODE_FIXUP(previous_ss,  u_int16_t, r_ss);
250
251                 if (acpi_get_verbose(sc))
252                         acpi_printcpu();
253
254                 /* Call ACPICA to enter the desired sleep state */
255                 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
256                         status = AcpiEnterSleepStateS4bios();
257                 else
258                         status = AcpiEnterSleepState(state);
259
260                 if (status != AE_OK) {
261                         device_printf(sc->acpi_dev,
262                                 "AcpiEnterSleepState failed - %s\n",
263                                 AcpiFormatException(status));
264                         ret = -1;
265                         goto out;
266                 }
267
268                 for (;;) ;
269         } else {
270                 /* Execute Wakeup */
271 #if 0
272                 initializecpu();
273 #endif
274                 cpu_enable_intr();
275
276                 if (acpi_get_verbose(sc)) {
277                         acpi_savecpu();
278                         acpi_printcpu();
279                 }
280         }
281
282 out:
283         vm_page_lock_queues();
284         pmap_remove(pm, sc->acpi_wakephys, sc->acpi_wakephys + PAGE_SIZE);
285         vm_page_unlock_queues();
286         if (opage) {
287                 pmap_enter(pm, sc->acpi_wakephys, page,
288                            VM_PROT_READ | VM_PROT_WRITE, 0);
289         }
290
291         load_cr3(cr3);
292
293         write_eflags(ef);
294
295         return (ret);
296 }
297
298 static bus_dma_tag_t    acpi_waketag;
299 static bus_dmamap_t     acpi_wakemap;
300 static vm_offset_t      acpi_wakeaddr = 0;
301
302 static void
303 acpi_alloc_wakeup_handler(void)
304 {
305         if (!cold)
306                 return;
307
308         if (bus_dma_tag_create(/* parent */ NULL, /* alignment */ 2, 0,
309                                /* lowaddr below 1MB */ 0x9ffff,
310                                /* highaddr */ BUS_SPACE_MAXADDR, NULL, NULL,
311                                 PAGE_SIZE, 1, PAGE_SIZE, 0, &acpi_waketag) != 0) {
312                 printf("acpi_alloc_wakeup_handler: can't create wake tag\n");
313                 return;
314         }
315
316         if (bus_dmamem_alloc(acpi_waketag, (void **)&acpi_wakeaddr,
317                              BUS_DMA_NOWAIT, &acpi_wakemap)) {
318                 printf("acpi_alloc_wakeup_handler: can't alloc wake memory\n");
319                 return;
320         }
321 }
322
323 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0)
324
325 static void
326 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
327 {
328         struct acpi_softc       *sc = arg;
329         u_int32_t               *addr;
330
331         addr = (u_int32_t *)&wakecode[wakeup_sw32 + 2];
332         *addr = segs[0].ds_addr + wakeup_32;
333         bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
334         sc->acpi_wakephys = segs[0].ds_addr;
335 }
336
337 void
338 acpi_install_wakeup_handler(struct acpi_softc *sc)
339 {
340         if (acpi_wakeaddr == 0)
341                 return;
342
343         sc->acpi_waketag = acpi_waketag;
344         sc->acpi_wakeaddr = acpi_wakeaddr;
345         sc->acpi_wakemap = acpi_wakemap;
346
347         bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
348                         (void *)sc->acpi_wakeaddr, PAGE_SIZE,
349                         acpi_realmodeinst, sc, 0);
350 }