]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/x86/acpica/acpi_wakeup.c
MFC r337715, r337751, r337754, r337758, r337813, r338354, r338687,
[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 #if defined(__amd64__)
34 #define DEV_APIC
35 #else
36 #include "opt_apic.h"
37 #endif
38
39 #include <sys/param.h>
40 #include <sys/bus.h>
41 #include <sys/eventhandler.h>
42 #include <sys/kernel.h>
43 #include <sys/malloc.h>
44 #include <sys/memrange.h>
45 #include <sys/smp.h>
46 #include <sys/systm.h>
47 #include <sys/cons.h>
48
49 #include <vm/vm.h>
50 #include <vm/pmap.h>
51
52 #include <machine/clock.h>
53 #include <machine/cpu.h>
54 #include <machine/intr_machdep.h>
55 #include <machine/md_var.h>
56 #include <x86/mca.h>
57 #include <machine/pcb.h>
58 #include <machine/specialreg.h>
59 #include <x86/ucode.h>
60
61 #ifdef DEV_APIC
62 #include <x86/apicreg.h>
63 #include <x86/apicvar.h>
64 #endif
65 #ifdef SMP
66 #include <machine/smp.h>
67 #include <machine/vmparam.h>
68 #endif
69
70 #include <contrib/dev/acpica/include/acpi.h>
71
72 #include <dev/acpica/acpivar.h>
73
74 #include "acpi_wakecode.h"
75 #include "acpi_wakedata.h"
76
77 /* Make sure the code is less than a page and leave room for the stack. */
78 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
79
80 extern int              acpi_resume_beep;
81 extern int              acpi_reset_video;
82 extern int              acpi_susp_bounce;
83
84 #ifdef SMP
85 extern struct susppcb   **susppcbs;
86 static cpuset_t         suspcpus;
87 #else
88 static struct susppcb   **susppcbs;
89 #endif
90
91 static void             *acpi_alloc_wakeup_handler(void **);
92 static void             acpi_stop_beep(void *);
93
94 #ifdef SMP
95 static int              acpi_wakeup_ap(struct acpi_softc *, int);
96 static void             acpi_wakeup_cpus(struct acpi_softc *);
97 #endif
98
99 #ifdef __amd64__
100 #define ACPI_WAKEPAGES  4
101 #else
102 #define ACPI_WAKEPAGES  1
103 #endif
104
105 #define WAKECODE_FIXUP(offset, type, val)       do {    \
106         type    *addr;                                  \
107         addr = (type *)(sc->acpi_wakeaddr + (offset));  \
108         *addr = val;                                    \
109 } while (0)
110
111 static void
112 acpi_stop_beep(void *arg)
113 {
114
115         if (acpi_resume_beep != 0)
116                 timer_spkr_release();
117 }
118
119 #ifdef SMP
120 static int
121 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
122 {
123         struct pcb *pcb;
124         int             vector = (sc->acpi_wakephys >> 12) & 0xff;
125         int             apic_id = cpu_apic_ids[cpu];
126         int             ms;
127
128         pcb = &susppcbs[cpu]->sp_pcb;
129         WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
130         WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
131         WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
132
133         ipi_startup(apic_id, vector);
134
135         /* Wait up to 5 seconds for it to resume. */
136         for (ms = 0; ms < 5000; ms++) {
137                 if (!CPU_ISSET(cpu, &suspended_cpus))
138                         return (1);     /* return SUCCESS */
139                 DELAY(1000);
140         }
141         return (0);             /* return FAILURE */
142 }
143
144 #define WARMBOOT_TARGET         0
145 #define WARMBOOT_OFF            (KERNBASE + 0x0467)
146 #define WARMBOOT_SEG            (KERNBASE + 0x0469)
147
148 #define CMOS_REG                (0x70)
149 #define CMOS_DATA               (0x71)
150 #define BIOS_RESET              (0x0f)
151 #define BIOS_WARM               (0x0a)
152
153 static void
154 acpi_wakeup_cpus(struct acpi_softc *sc)
155 {
156         uint32_t        mpbioswarmvec;
157         int             cpu;
158         u_char          mpbiosreason;
159
160         /* save the current value of the warm-start vector */
161         mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
162         outb(CMOS_REG, BIOS_RESET);
163         mpbiosreason = inb(CMOS_DATA);
164
165         /* setup a vector to our boot code */
166         *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
167         *((volatile u_short *)WARMBOOT_SEG) = sc->acpi_wakephys >> 4;
168         outb(CMOS_REG, BIOS_RESET);
169         outb(CMOS_DATA, BIOS_WARM);     /* 'warm-start' */
170
171         /* Wake up each AP. */
172         for (cpu = 1; cpu < mp_ncpus; cpu++) {
173                 if (!CPU_ISSET(cpu, &suspcpus))
174                         continue;
175                 if (acpi_wakeup_ap(sc, cpu) == 0) {
176                         /* restore the warmstart vector */
177                         *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
178                         panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
179                             cpu, cpu_apic_ids[cpu]);
180                 }
181         }
182
183         /* restore the warmstart vector */
184         *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
185
186         outb(CMOS_REG, BIOS_RESET);
187         outb(CMOS_DATA, mpbiosreason);
188 }
189 #endif
190
191 int
192 acpi_sleep_machdep(struct acpi_softc *sc, int state)
193 {
194         ACPI_STATUS     status;
195         struct pcb      *pcb;
196 #ifdef __amd64__
197         struct pcpu *pc;
198         int i;
199 #endif
200
201         if (sc->acpi_wakeaddr == 0ul)
202                 return (-1);    /* couldn't alloc wake memory */
203
204 #ifdef SMP
205         suspcpus = all_cpus;
206         CPU_CLR(PCPU_GET(cpuid), &suspcpus);
207 #endif
208
209         if (acpi_resume_beep != 0)
210                 timer_spkr_acquire();
211
212         AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0);
213
214         intr_suspend();
215
216         pcb = &susppcbs[0]->sp_pcb;
217         if (savectx(pcb)) {
218 #ifdef __amd64__
219                 fpususpend(susppcbs[0]->sp_fpususpend);
220 #else
221                 npxsuspend(susppcbs[0]->sp_fpususpend);
222 #endif
223 #ifdef SMP
224                 if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
225                         device_printf(sc->acpi_dev, "Failed to suspend APs\n");
226                         return (0);     /* couldn't sleep */
227                 }
228 #endif
229 #ifdef __amd64__
230                 hw_ibrs_active = 0;
231                 hw_ssb_active = 0;
232                 cpu_stdext_feature3 = 0;
233                 CPU_FOREACH(i) {
234                         pc = pcpu_find(i);
235                         pc->pc_ibpb_set = 0;
236                 }
237 #endif
238
239                 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
240                 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
241
242 #ifdef __amd64__
243                 WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER) &
244                     ~(EFER_LMA));
245 #else
246                 WAKECODE_FIXUP(wakeup_cr4, register_t, pcb->pcb_cr4);
247 #endif
248                 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
249                 WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
250                 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
251
252                 /* Call ACPICA to enter the desired sleep state */
253                 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
254                         status = AcpiEnterSleepStateS4bios();
255                 else
256                         status = AcpiEnterSleepState(state);
257                 if (ACPI_FAILURE(status)) {
258                         device_printf(sc->acpi_dev,
259                             "AcpiEnterSleepState failed - %s\n",
260                             AcpiFormatException(status));
261                         return (0);     /* couldn't sleep */
262                 }
263
264                 if (acpi_susp_bounce)
265                         resumectx(pcb);
266
267                 for (;;)
268                         ia32_pause();
269         } else {
270                 /*
271                  * Re-initialize console hardware as soon as possibe.
272                  * No console output (e.g. printf) is allowed before
273                  * this point.
274                  */
275                 cnresume();
276 #ifdef __amd64__
277                 fpuresume(susppcbs[0]->sp_fpususpend);
278 #else
279                 npxresume(susppcbs[0]->sp_fpususpend);
280 #endif
281         }
282
283         return (1);     /* wakeup successfully */
284 }
285
286 int
287 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
288     int intr_enabled)
289 {
290
291         if (sleep_result == -1)
292                 return (sleep_result);
293
294         if (!intr_enabled) {
295                 /* Wakeup MD procedures in interrupt disabled context */
296                 if (sleep_result == 1) {
297                         ucode_reload();
298                         pmap_init_pat();
299                         initializecpu();
300                         PCPU_SET(switchtime, 0);
301                         PCPU_SET(switchticks, ticks);
302 #ifdef DEV_APIC
303                         lapic_xapic_mode();
304 #endif
305 #ifdef SMP
306                         if (!CPU_EMPTY(&suspcpus))
307                                 acpi_wakeup_cpus(sc);
308 #endif
309                 }
310
311 #ifdef SMP
312                 if (!CPU_EMPTY(&suspcpus))
313                         resume_cpus(suspcpus);
314 #endif
315                 mca_resume();
316 #ifdef __amd64__
317                 if (vmm_resume_p != NULL)
318                         vmm_resume_p();
319 #endif
320                 intr_resume(/*suspend_cancelled*/false);
321
322                 AcpiSetFirmwareWakingVector(0, 0);
323         } else {
324                 /* Wakeup MD procedures in interrupt enabled context */
325                 if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
326                     mem_range_softc.mr_op->reinit != NULL)
327                         mem_range_softc.mr_op->reinit(&mem_range_softc);
328         }
329
330         return (sleep_result);
331 }
332
333 static void *
334 acpi_alloc_wakeup_handler(void *wakepages[ACPI_WAKEPAGES])
335 {
336         int             i;
337
338         memset(wakepages, 0, ACPI_WAKEPAGES * sizeof(*wakepages));
339
340         /*
341          * Specify the region for our wakeup code.  We want it in the low 1 MB
342          * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
343          * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
344          * and ROM area (0xa0000 and above).  The temporary page tables must be
345          * page-aligned.
346          */
347         for (i = 0; i < ACPI_WAKEPAGES; i++) {
348                 wakepages[i] = contigmalloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT,
349                     0x500, 0xa0000, PAGE_SIZE, 0ul);
350                 if (wakepages[i] == NULL) {
351                         printf("%s: can't alloc wake memory\n", __func__);
352                         goto freepages;
353                 }
354         }
355         if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
356             EVENTHANDLER_PRI_LAST) == NULL) {
357                 printf("%s: can't register event handler\n", __func__);
358                 goto freepages;
359         }
360         susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
361         for (i = 0; i < mp_ncpus; i++) {
362                 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
363                 susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
364         }
365
366         return (wakepages);
367
368 freepages:
369         for (i = 0; i < ACPI_WAKEPAGES; i++)
370                 if (wakepages[i] != NULL)
371                         contigfree(wakepages[i], PAGE_SIZE, M_DEVBUF);
372         return (NULL);
373 }
374
375 void
376 acpi_install_wakeup_handler(struct acpi_softc *sc)
377 {
378         static void     *wakeaddr;
379         void            *wakepages[ACPI_WAKEPAGES];
380 #ifdef __amd64__
381         uint64_t        *pt4, *pt3, *pt2;
382         vm_paddr_t      pt4pa, pt3pa, pt2pa;
383         int             i;
384 #endif
385
386         if (wakeaddr != NULL)
387                 return;
388
389         if (acpi_alloc_wakeup_handler(wakepages) == NULL)
390                 return;
391
392         wakeaddr = wakepages[0];
393         sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
394         sc->acpi_wakephys = vtophys(wakeaddr);
395
396 #ifdef __amd64__
397         pt4 = wakepages[1];
398         pt3 = wakepages[2];
399         pt2 = wakepages[3];
400         pt4pa = vtophys(pt4);
401         pt3pa = vtophys(pt3);
402         pt2pa = vtophys(pt2);
403 #endif
404
405         bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
406
407         /* Patch GDT base address, ljmp targets. */
408         WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
409             sc->acpi_wakephys + bootgdt);
410         WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
411             sc->acpi_wakephys + wakeup_32);
412 #ifdef __amd64__
413         WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
414             sc->acpi_wakephys + wakeup_64);
415         WAKECODE_FIXUP(wakeup_pagetables, uint32_t, pt4pa);
416 #endif
417
418         /* Save pointers to some global data. */
419         WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
420 #ifndef __amd64__
421 #if defined(PAE) || defined(PAE_TABLES)
422         WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdpt));
423 #else
424         WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdir));
425 #endif
426
427 #else /* __amd64__ */
428         /* Create the initial 1GB replicated page tables */
429         for (i = 0; i < 512; i++) {
430                 /*
431                  * Each slot of the level 4 pages points
432                  * to the same level 3 page
433                  */
434                 pt4[i] = (uint64_t)pt3pa;
435                 pt4[i] |= PG_V | PG_RW | PG_U;
436
437                 /*
438                  * Each slot of the level 3 pages points
439                  * to the same level 2 page
440                  */
441                 pt3[i] = (uint64_t)pt2pa;
442                 pt3[i] |= PG_V | PG_RW | PG_U;
443
444                 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
445                 pt2[i] = i * (2 * 1024 * 1024);
446                 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
447         }
448 #endif /* !__amd64__ */
449
450         if (bootverbose)
451                 device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
452                     (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
453 }