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