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