]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/acpica/acpi_wakeup.c
Merging ^/head r278916 through r279022.
[FreeBSD/FreeBSD.git] / sys / x86 / acpica / acpi_wakeup.c
1 /*-
2  * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
3  * Copyright (c) 2001-2012 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
4  * Copyright (c) 2003 Peter Wemm
5  * Copyright (c) 2008-2012 Jung-uk Kim <jkim@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #ifdef __i386__
34 #include "opt_npx.h"
35 #endif
36
37 #include <sys/param.h>
38 #include <sys/bus.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/malloc.h>
42 #include <sys/memrange.h>
43 #include <sys/smp.h>
44 #include <sys/systm.h>
45
46 #include <vm/vm.h>
47 #include <vm/pmap.h>
48
49 #include <machine/clock.h>
50 #include <machine/cpu.h>
51 #include <machine/intr_machdep.h>
52 #include <x86/mca.h>
53 #include <machine/pcb.h>
54 #include <machine/pmap.h>
55 #include <machine/specialreg.h>
56 #include <machine/md_var.h>
57
58 #include <x86/apicreg.h>
59 #include <x86/apicvar.h>
60 #ifdef SMP
61 #include <machine/smp.h>
62 #include <machine/vmparam.h>
63 #endif
64
65 #include <contrib/dev/acpica/include/acpi.h>
66
67 #include <dev/acpica/acpivar.h>
68
69 #include "acpi_wakecode.h"
70 #include "acpi_wakedata.h"
71
72 /* Make sure the code is less than a page and leave room for the stack. */
73 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
74
75 extern int              acpi_resume_beep;
76 extern int              acpi_reset_video;
77
78 #ifdef SMP
79 extern struct susppcb   **susppcbs;
80 static cpuset_t         suspcpus;
81 #else
82 static struct susppcb   **susppcbs;
83 #endif
84
85 static void             *acpi_alloc_wakeup_handler(void);
86 static void             acpi_stop_beep(void *);
87
88 #ifdef SMP
89 static int              acpi_wakeup_ap(struct acpi_softc *, int);
90 static void             acpi_wakeup_cpus(struct acpi_softc *);
91 #endif
92
93 #ifdef __amd64__
94 #define ACPI_PAGETABLES 3
95 #else
96 #define ACPI_PAGETABLES 0
97 #endif
98
99 #define WAKECODE_VADDR(sc)                              \
100     ((sc)->acpi_wakeaddr + (ACPI_PAGETABLES * PAGE_SIZE))
101 #define WAKECODE_PADDR(sc)                              \
102     ((sc)->acpi_wakephys + (ACPI_PAGETABLES * PAGE_SIZE))
103 #define WAKECODE_FIXUP(offset, type, val)       do {    \
104         type    *addr;                                  \
105         addr = (type *)(WAKECODE_VADDR(sc) + offset);   \
106         *addr = val;                                    \
107 } while (0)
108
109 static void
110 acpi_stop_beep(void *arg)
111 {
112
113         if (acpi_resume_beep != 0)
114                 timer_spkr_release();
115 }
116
117 #ifdef SMP
118 static int
119 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
120 {
121         struct pcb *pcb;
122         int             vector = (WAKECODE_PADDR(sc) >> 12) & 0xff;
123         int             apic_id = cpu_apic_ids[cpu];
124         int             ms;
125
126         pcb = &susppcbs[cpu]->sp_pcb;
127         WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
128         WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
129         WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
130
131         ipi_startup(apic_id, vector);
132
133         /* Wait up to 5 seconds for it to resume. */
134         for (ms = 0; ms < 5000; ms++) {
135                 if (!CPU_ISSET(cpu, &suspended_cpus))
136                         return (1);     /* return SUCCESS */
137                 DELAY(1000);
138         }
139         return (0);             /* return FAILURE */
140 }
141
142 #define WARMBOOT_TARGET         0
143 #define WARMBOOT_OFF            (KERNBASE + 0x0467)
144 #define WARMBOOT_SEG            (KERNBASE + 0x0469)
145
146 #define CMOS_REG                (0x70)
147 #define CMOS_DATA               (0x71)
148 #define BIOS_RESET              (0x0f)
149 #define BIOS_WARM               (0x0a)
150
151 static void
152 acpi_wakeup_cpus(struct acpi_softc *sc)
153 {
154         uint32_t        mpbioswarmvec;
155         int             cpu;
156         u_char          mpbiosreason;
157
158         /* save the current value of the warm-start vector */
159         mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
160         outb(CMOS_REG, BIOS_RESET);
161         mpbiosreason = inb(CMOS_DATA);
162
163         /* setup a vector to our boot code */
164         *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
165         *((volatile u_short *)WARMBOOT_SEG) = WAKECODE_PADDR(sc) >> 4;
166         outb(CMOS_REG, BIOS_RESET);
167         outb(CMOS_DATA, BIOS_WARM);     /* 'warm-start' */
168
169         /* Wake up each AP. */
170         for (cpu = 1; cpu < mp_ncpus; cpu++) {
171                 if (!CPU_ISSET(cpu, &suspcpus))
172                         continue;
173                 if (acpi_wakeup_ap(sc, cpu) == 0) {
174                         /* restore the warmstart vector */
175                         *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
176                         panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
177                             cpu, cpu_apic_ids[cpu]);
178                 }
179         }
180
181         /* restore the warmstart vector */
182         *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
183
184         outb(CMOS_REG, BIOS_RESET);
185         outb(CMOS_DATA, mpbiosreason);
186 }
187 #endif
188
189 int
190 acpi_sleep_machdep(struct acpi_softc *sc, int state)
191 {
192         ACPI_STATUS     status;
193         struct pcb      *pcb;
194
195         if (sc->acpi_wakeaddr == 0ul)
196                 return (-1);    /* couldn't alloc wake memory */
197
198 #ifdef SMP
199         suspcpus = all_cpus;
200         CPU_CLR(PCPU_GET(cpuid), &suspcpus);
201 #endif
202
203         if (acpi_resume_beep != 0)
204                 timer_spkr_acquire();
205
206         AcpiSetFirmwareWakingVector(WAKECODE_PADDR(sc));
207
208         intr_suspend();
209
210         pcb = &susppcbs[0]->sp_pcb;
211         if (savectx(pcb)) {
212 #ifdef __amd64__
213                 fpususpend(susppcbs[0]->sp_fpususpend);
214 #elif defined(DEV_NPX)
215                 npxsuspend(susppcbs[0]->sp_fpususpend);
216 #endif
217 #ifdef SMP
218                 if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
219                         device_printf(sc->acpi_dev, "Failed to suspend APs\n");
220                         return (0);     /* couldn't sleep */
221                 }
222 #endif
223
224                 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
225                 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
226
227 #ifndef __amd64__
228                 WAKECODE_FIXUP(wakeup_cr4, register_t, pcb->pcb_cr4);
229 #endif
230                 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
231                 WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
232                 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
233
234                 /* Call ACPICA to enter the desired sleep state */
235                 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
236                         status = AcpiEnterSleepStateS4bios();
237                 else
238                         status = AcpiEnterSleepState(state);
239                 if (ACPI_FAILURE(status)) {
240                         device_printf(sc->acpi_dev,
241                             "AcpiEnterSleepState failed - %s\n",
242                             AcpiFormatException(status));
243                         return (0);     /* couldn't sleep */
244                 }
245
246                 for (;;)
247                         ia32_pause();
248         } else {
249 #ifdef __amd64__
250                 fpuresume(susppcbs[0]->sp_fpususpend);
251 #elif defined(DEV_NPX)
252                 npxresume(susppcbs[0]->sp_fpususpend);
253 #endif
254         }
255
256         return (1);     /* wakeup successfully */
257 }
258
259 int
260 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
261     int intr_enabled)
262 {
263
264         if (sleep_result == -1)
265                 return (sleep_result);
266
267         if (!intr_enabled) {
268                 /* Wakeup MD procedures in interrupt disabled context */
269                 if (sleep_result == 1) {
270                         pmap_init_pat();
271                         initializecpu();
272                         PCPU_SET(switchtime, 0);
273                         PCPU_SET(switchticks, ticks);
274                         lapic_xapic_mode();
275 #ifdef SMP
276                         if (!CPU_EMPTY(&suspcpus))
277                                 acpi_wakeup_cpus(sc);
278 #endif
279                 }
280
281 #ifdef SMP
282                 if (!CPU_EMPTY(&suspcpus))
283                         restart_cpus(suspcpus);
284 #endif
285                 mca_resume();
286 #ifdef __amd64__
287                 if (vmm_resume_p != NULL)
288                         vmm_resume_p();
289 #endif
290                 intr_resume(/*suspend_cancelled*/false);
291
292                 AcpiSetFirmwareWakingVector(0);
293         } else {
294                 /* Wakeup MD procedures in interrupt enabled context */
295                 if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
296                     mem_range_softc.mr_op->reinit != NULL)
297                         mem_range_softc.mr_op->reinit(&mem_range_softc);
298         }
299
300         return (sleep_result);
301 }
302
303 static void *
304 acpi_alloc_wakeup_handler(void)
305 {
306         void            *wakeaddr;
307         int             i;
308
309         /*
310          * Specify the region for our wakeup code.  We want it in the low 1 MB
311          * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
312          * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
313          * and ROM area (0xa0000 and above).  The temporary page tables must be
314          * page-aligned.
315          */
316         wakeaddr = contigmalloc((ACPI_PAGETABLES + 1) * PAGE_SIZE, M_DEVBUF,
317             M_WAITOK, 0x500, 0xa0000, PAGE_SIZE, 0ul);
318         if (wakeaddr == NULL) {
319                 printf("%s: can't alloc wake memory\n", __func__);
320                 return (NULL);
321         }
322         if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
323             EVENTHANDLER_PRI_LAST) == NULL) {
324                 printf("%s: can't register event handler\n", __func__);
325                 contigfree(wakeaddr, (ACPI_PAGETABLES + 1) * PAGE_SIZE,
326                     M_DEVBUF);
327                 return (NULL);
328         }
329         susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
330         for (i = 0; i < mp_ncpus; i++) {
331                 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
332                 susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
333         }
334
335         return (wakeaddr);
336 }
337
338 void
339 acpi_install_wakeup_handler(struct acpi_softc *sc)
340 {
341         static void     *wakeaddr = NULL;
342 #ifdef __amd64__
343         uint64_t        *pt4, *pt3, *pt2;
344         int             i;
345 #endif
346
347         if (wakeaddr != NULL)
348                 return;
349
350         wakeaddr = acpi_alloc_wakeup_handler();
351         if (wakeaddr == NULL)
352                 return;
353
354         sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
355         sc->acpi_wakephys = vtophys(wakeaddr);
356
357         bcopy(wakecode, (void *)WAKECODE_VADDR(sc), sizeof(wakecode));
358
359         /* Patch GDT base address, ljmp targets. */
360         WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
361             WAKECODE_PADDR(sc) + bootgdt);
362         WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
363             WAKECODE_PADDR(sc) + wakeup_32);
364 #ifdef __amd64__
365         WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
366             WAKECODE_PADDR(sc) + wakeup_64);
367         WAKECODE_FIXUP(wakeup_pagetables, uint32_t, sc->acpi_wakephys);
368 #endif
369
370         /* Save pointers to some global data. */
371         WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
372 #ifndef __amd64__
373 #ifdef PAE
374         WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdpt));
375 #else
376         WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdir));
377 #endif
378
379 #else
380         /* Build temporary page tables below realmode code. */
381         pt4 = wakeaddr;
382         pt3 = pt4 + (PAGE_SIZE) / sizeof(uint64_t);
383         pt2 = pt3 + (PAGE_SIZE) / sizeof(uint64_t);
384
385         /* Create the initial 1GB replicated page tables */
386         for (i = 0; i < 512; i++) {
387                 /*
388                  * Each slot of the level 4 pages points
389                  * to the same level 3 page
390                  */
391                 pt4[i] = (uint64_t)(sc->acpi_wakephys + PAGE_SIZE);
392                 pt4[i] |= PG_V | PG_RW | PG_U;
393
394                 /*
395                  * Each slot of the level 3 pages points
396                  * to the same level 2 page
397                  */
398                 pt3[i] = (uint64_t)(sc->acpi_wakephys + (2 * PAGE_SIZE));
399                 pt3[i] |= PG_V | PG_RW | PG_U;
400
401                 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
402                 pt2[i] = i * (2 * 1024 * 1024);
403                 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
404         }
405 #endif
406
407         if (bootverbose)
408                 device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
409                     (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
410 }