]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/acpica/acpi_wakeup.c
Update libarchive to 3.0.3
[FreeBSD/FreeBSD.git] / sys / amd64 / 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  * 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 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/eventhandler.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/memrange.h>
39 #include <sys/smp.h>
40
41 #include <vm/vm.h>
42 #include <vm/pmap.h>
43
44 #include <machine/clock.h>
45 #include <machine/intr_machdep.h>
46 #include <x86/mca.h>
47 #include <machine/pcb.h>
48 #include <machine/pmap.h>
49 #include <machine/specialreg.h>
50 #include <machine/md_var.h>
51
52 #ifdef SMP
53 #include <x86/apicreg.h>
54 #include <machine/smp.h>
55 #include <machine/vmparam.h>
56 #endif
57
58 #include <contrib/dev/acpica/include/acpi.h>
59
60 #include <dev/acpica/acpivar.h>
61
62 #include "acpi_wakecode.h"
63 #include "acpi_wakedata.h"
64
65 /* Make sure the code is less than a page and leave room for the stack. */
66 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
67
68 extern int              acpi_resume_beep;
69 extern int              acpi_reset_video;
70
71 #ifdef SMP
72 extern struct pcb       **susppcbs;
73 extern void             **suspfpusave;
74 #else
75 static struct pcb       **susppcbs;
76 static void             **suspfpusave;
77 #endif
78
79 int                     acpi_restorecpu(uint64_t, vm_offset_t);
80
81 static void             *acpi_alloc_wakeup_handler(void);
82 static void             acpi_stop_beep(void *);
83
84 #ifdef SMP
85 static int              acpi_wakeup_ap(struct acpi_softc *, int);
86 static void             acpi_wakeup_cpus(struct acpi_softc *, const cpuset_t *);
87 #endif
88
89 #define WAKECODE_VADDR(sc)      ((sc)->acpi_wakeaddr + (3 * PAGE_SIZE))
90 #define WAKECODE_PADDR(sc)      ((sc)->acpi_wakephys + (3 * PAGE_SIZE))
91 #define WAKECODE_FIXUP(offset, type, val) do    {       \
92         type    *addr;                                  \
93         addr = (type *)(WAKECODE_VADDR(sc) + offset);   \
94         *addr = val;                                    \
95 } while (0)
96
97 static void
98 acpi_stop_beep(void *arg)
99 {
100
101         if (acpi_resume_beep != 0)
102                 timer_spkr_release();
103 }
104
105 #ifdef SMP
106 static int
107 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
108 {
109         int             vector = (WAKECODE_PADDR(sc) >> 12) & 0xff;
110         int             apic_id = cpu_apic_ids[cpu];
111         int             ms;
112
113         WAKECODE_FIXUP(wakeup_pcb, struct pcb *, susppcbs[cpu]);
114         WAKECODE_FIXUP(wakeup_fpusave, void *, suspfpusave[cpu]);
115         WAKECODE_FIXUP(wakeup_gdt, uint16_t, susppcbs[cpu]->pcb_gdt.rd_limit);
116         WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t,
117             susppcbs[cpu]->pcb_gdt.rd_base);
118         WAKECODE_FIXUP(wakeup_cpu, int, cpu);
119
120         /* do an INIT IPI: assert RESET */
121         lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
122             APIC_LEVEL_ASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, apic_id);
123
124         /* wait for pending status end */
125         lapic_ipi_wait(-1);
126
127         /* do an INIT IPI: deassert RESET */
128         lapic_ipi_raw(APIC_DEST_ALLESELF | APIC_TRIGMOD_LEVEL |
129             APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_INIT, 0);
130
131         /* wait for pending status end */
132         DELAY(10000);           /* wait ~10mS */
133         lapic_ipi_wait(-1);
134
135         /*
136          * next we do a STARTUP IPI: the previous INIT IPI might still be
137          * latched, (P5 bug) this 1st STARTUP would then terminate
138          * immediately, and the previously started INIT IPI would continue. OR
139          * the previous INIT IPI has already run. and this STARTUP IPI will
140          * run. OR the previous INIT IPI was ignored. and this STARTUP IPI
141          * will run.
142          */
143
144         /* do a STARTUP IPI */
145         lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
146             APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
147             vector, apic_id);
148         lapic_ipi_wait(-1);
149         DELAY(200);             /* wait ~200uS */
150
151         /*
152          * finally we do a 2nd STARTUP IPI: this 2nd STARTUP IPI should run IF
153          * the previous STARTUP IPI was cancelled by a latched INIT IPI. OR
154          * this STARTUP IPI will be ignored, as only ONE STARTUP IPI is
155          * recognized after hardware RESET or INIT IPI.
156          */
157
158         lapic_ipi_raw(APIC_DEST_DESTFLD | APIC_TRIGMOD_EDGE |
159             APIC_LEVEL_DEASSERT | APIC_DESTMODE_PHY | APIC_DELMODE_STARTUP |
160             vector, apic_id);
161         lapic_ipi_wait(-1);
162         DELAY(200);             /* wait ~200uS */
163
164         /* Wait up to 5 seconds for it to start. */
165         for (ms = 0; ms < 5000; ms++) {
166                 if (*(int *)(WAKECODE_VADDR(sc) + wakeup_cpu) == 0)
167                         return (1);     /* return SUCCESS */
168                 DELAY(1000);
169         }
170         return (0);             /* return FAILURE */
171 }
172
173 #define WARMBOOT_TARGET         0
174 #define WARMBOOT_OFF            (KERNBASE + 0x0467)
175 #define WARMBOOT_SEG            (KERNBASE + 0x0469)
176
177 #define CMOS_REG                (0x70)
178 #define CMOS_DATA               (0x71)
179 #define BIOS_RESET              (0x0f)
180 #define BIOS_WARM               (0x0a)
181
182 static void
183 acpi_wakeup_cpus(struct acpi_softc *sc, const cpuset_t *wakeup_cpus)
184 {
185         uint32_t        mpbioswarmvec;
186         int             cpu;
187         u_char          mpbiosreason;
188
189         /* save the current value of the warm-start vector */
190         mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
191         outb(CMOS_REG, BIOS_RESET);
192         mpbiosreason = inb(CMOS_DATA);
193
194         /* setup a vector to our boot code */
195         *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
196         *((volatile u_short *)WARMBOOT_SEG) = WAKECODE_PADDR(sc) >> 4;
197         outb(CMOS_REG, BIOS_RESET);
198         outb(CMOS_DATA, BIOS_WARM);     /* 'warm-start' */
199
200         /* Wake up each AP. */
201         for (cpu = 1; cpu < mp_ncpus; cpu++) {
202                 if (!CPU_ISSET(cpu, wakeup_cpus))
203                         continue;
204                 if (acpi_wakeup_ap(sc, cpu) == 0) {
205                         /* restore the warmstart vector */
206                         *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
207                         panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
208                             cpu, cpu_apic_ids[cpu]);
209                 }
210         }
211
212         /* restore the warmstart vector */
213         *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
214
215         outb(CMOS_REG, BIOS_RESET);
216         outb(CMOS_DATA, mpbiosreason);
217 }
218 #endif
219
220 int
221 acpi_sleep_machdep(struct acpi_softc *sc, int state)
222 {
223 #ifdef SMP
224         cpuset_t        wakeup_cpus;
225 #endif
226         ACPI_STATUS     status;
227         int             ret;
228
229         ret = -1;
230
231         if (sc->acpi_wakeaddr == 0ul)
232                 return (ret);
233
234 #ifdef SMP
235         wakeup_cpus = all_cpus;
236         CPU_CLR(PCPU_GET(cpuid), &wakeup_cpus);
237 #endif
238
239         if (acpi_resume_beep != 0)
240                 timer_spkr_acquire();
241
242         AcpiSetFirmwareWakingVector(WAKECODE_PADDR(sc));
243
244         spinlock_enter();
245         intr_suspend();
246
247         if (savectx(susppcbs[0])) {
248                 ctx_fpusave(suspfpusave[0]);
249 #ifdef SMP
250                 if (!CPU_EMPTY(&wakeup_cpus) &&
251                     suspend_cpus(wakeup_cpus) == 0) {
252                         device_printf(sc->acpi_dev, "Failed to suspend APs\n");
253                         goto out;
254                 }
255 #endif
256
257                 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
258                 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
259
260                 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, susppcbs[0]);
261                 WAKECODE_FIXUP(wakeup_fpusave, void *, suspfpusave[0]);
262                 WAKECODE_FIXUP(wakeup_gdt, uint16_t,
263                     susppcbs[0]->pcb_gdt.rd_limit);
264                 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t,
265                     susppcbs[0]->pcb_gdt.rd_base);
266                 WAKECODE_FIXUP(wakeup_cpu, int, 0);
267
268                 /* Call ACPICA to enter the desired sleep state */
269                 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
270                         status = AcpiEnterSleepStateS4bios();
271                 else
272                         status = AcpiEnterSleepState(state);
273
274                 if (status != AE_OK) {
275                         device_printf(sc->acpi_dev,
276                             "AcpiEnterSleepState failed - %s\n",
277                             AcpiFormatException(status));
278                         goto out;
279                 }
280
281                 for (;;)
282                         ia32_pause();
283         } else {
284                 pmap_init_pat();
285                 load_cr3(susppcbs[0]->pcb_cr3);
286                 PCPU_SET(switchtime, 0);
287                 PCPU_SET(switchticks, ticks);
288 #ifdef SMP
289                 if (!CPU_EMPTY(&wakeup_cpus))
290                         acpi_wakeup_cpus(sc, &wakeup_cpus);
291 #endif
292                 ret = 0;
293         }
294
295 out:
296 #ifdef SMP
297         if (!CPU_EMPTY(&wakeup_cpus))
298                 restart_cpus(wakeup_cpus);
299 #endif
300
301         mca_resume();
302         intr_resume();
303         spinlock_exit();
304
305         AcpiSetFirmwareWakingVector(0);
306
307         if (ret == 0 && mem_range_softc.mr_op != NULL &&
308             mem_range_softc.mr_op->reinit != NULL)
309                 mem_range_softc.mr_op->reinit(&mem_range_softc);
310
311         return (ret);
312 }
313
314 static void *
315 acpi_alloc_wakeup_handler(void)
316 {
317         void            *wakeaddr;
318         int             i;
319
320         /*
321          * Specify the region for our wakeup code.  We want it in the low 1 MB
322          * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
323          * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
324          * and ROM area (0xa0000 and above).  The temporary page tables must be
325          * page-aligned.
326          */
327         wakeaddr = contigmalloc(4 * PAGE_SIZE, M_DEVBUF, M_WAITOK, 0x500,
328             0xa0000, PAGE_SIZE, 0ul);
329         if (wakeaddr == NULL) {
330                 printf("%s: can't alloc wake memory\n", __func__);
331                 return (NULL);
332         }
333         if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
334             EVENTHANDLER_PRI_LAST) == NULL) {
335                 printf("%s: can't register event handler\n", __func__);
336                 contigfree(wakeaddr, 4 * PAGE_SIZE, M_DEVBUF);
337                 return (NULL);
338         }
339         susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
340         suspfpusave = malloc(mp_ncpus * sizeof(void *), M_DEVBUF, M_WAITOK);
341         for (i = 0; i < mp_ncpus; i++) {
342                 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
343                 suspfpusave[i] = alloc_fpusave(M_WAITOK);
344         }
345
346         return (wakeaddr);
347 }
348
349 void
350 acpi_install_wakeup_handler(struct acpi_softc *sc)
351 {
352         static void     *wakeaddr = NULL;
353         uint64_t        *pt4, *pt3, *pt2;
354         int             i;
355
356         if (wakeaddr != NULL)
357                 return;
358
359         wakeaddr = acpi_alloc_wakeup_handler();
360         if (wakeaddr == NULL)
361                 return;
362
363         sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
364         sc->acpi_wakephys = vtophys(wakeaddr);
365
366         bcopy(wakecode, (void *)WAKECODE_VADDR(sc), sizeof(wakecode));
367
368         /* Patch GDT base address, ljmp targets and page table base address. */
369         WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
370             WAKECODE_PADDR(sc) + bootgdt);
371         WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
372             WAKECODE_PADDR(sc) + wakeup_32);
373         WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
374             WAKECODE_PADDR(sc) + wakeup_64);
375         WAKECODE_FIXUP(wakeup_pagetables, uint32_t, sc->acpi_wakephys);
376
377         /* Save pointers to some global data. */
378         WAKECODE_FIXUP(wakeup_retaddr, void *, acpi_restorecpu);
379         WAKECODE_FIXUP(wakeup_kpml4, uint64_t, KPML4phys);
380         WAKECODE_FIXUP(wakeup_ctx, vm_offset_t,
381             WAKECODE_VADDR(sc) + wakeup_ctx);
382         WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER));
383         WAKECODE_FIXUP(wakeup_star, uint64_t, rdmsr(MSR_STAR));
384         WAKECODE_FIXUP(wakeup_lstar, uint64_t, rdmsr(MSR_LSTAR));
385         WAKECODE_FIXUP(wakeup_cstar, uint64_t, rdmsr(MSR_CSTAR));
386         WAKECODE_FIXUP(wakeup_sfmask, uint64_t, rdmsr(MSR_SF_MASK));
387         WAKECODE_FIXUP(wakeup_xsmask, uint64_t, xsave_mask);
388
389         /* Build temporary page tables below realmode code. */
390         pt4 = wakeaddr;
391         pt3 = pt4 + (PAGE_SIZE) / sizeof(uint64_t);
392         pt2 = pt3 + (PAGE_SIZE) / sizeof(uint64_t);
393
394         /* Create the initial 1GB replicated page tables */
395         for (i = 0; i < 512; i++) {
396                 /*
397                  * Each slot of the level 4 pages points
398                  * to the same level 3 page
399                  */
400                 pt4[i] = (uint64_t)(sc->acpi_wakephys + PAGE_SIZE);
401                 pt4[i] |= PG_V | PG_RW | PG_U;
402
403                 /*
404                  * Each slot of the level 3 pages points
405                  * to the same level 2 page
406                  */
407                 pt3[i] = (uint64_t)(sc->acpi_wakephys + (2 * PAGE_SIZE));
408                 pt3[i] |= PG_V | PG_RW | PG_U;
409
410                 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
411                 pt2[i] = i * (2 * 1024 * 1024);
412                 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
413         }
414
415         if (bootverbose)
416                 device_printf(sc->acpi_dev, "wakeup code va %p pa %p\n",
417                     (void *)sc->acpi_wakeaddr, (void *)sc->acpi_wakephys);
418 }