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