The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/x86/acpica/acpi_wakeup.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 66c9882cb37b3d75a4ffa03e2a922422


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.