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/11.2/sys/x86/acpica/acpi_wakeup.c 334152 2018-05-24 13:17:24Z kib $");
   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 
   48 #include <vm/vm.h>
   49 #include <vm/pmap.h>
   50 
   51 #include <machine/clock.h>
   52 #include <machine/cpu.h>
   53 #include <machine/intr_machdep.h>
   54 #include <x86/mca.h>
   55 #include <machine/pcb.h>
   56 #include <machine/specialreg.h>
   57 #include <machine/md_var.h>
   58 
   59 #ifdef DEV_APIC
   60 #include <x86/apicreg.h>
   61 #include <x86/apicvar.h>
   62 #endif
   63 #ifdef SMP
   64 #include <machine/smp.h>
   65 #include <machine/vmparam.h>
   66 #endif
   67 
   68 #include <contrib/dev/acpica/include/acpi.h>
   69 
   70 #include <dev/acpica/acpivar.h>
   71 
   72 #include "acpi_wakecode.h"
   73 #include "acpi_wakedata.h"
   74 
   75 /* Make sure the code is less than a page and leave room for the stack. */
   76 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
   77 
   78 extern int              acpi_resume_beep;
   79 extern int              acpi_reset_video;
   80 
   81 #ifdef SMP
   82 extern struct susppcb   **susppcbs;
   83 static cpuset_t         suspcpus;
   84 #else
   85 static struct susppcb   **susppcbs;
   86 #endif
   87 
   88 static void             *acpi_alloc_wakeup_handler(void **);
   89 static void             acpi_stop_beep(void *);
   90 
   91 #ifdef SMP
   92 static int              acpi_wakeup_ap(struct acpi_softc *, int);
   93 static void             acpi_wakeup_cpus(struct acpi_softc *);
   94 #endif
   95 
   96 #ifdef __amd64__
   97 #define ACPI_WAKEPAGES  4
   98 #else
   99 #define ACPI_WAKEPAGES  1
  100 #endif
  101 
  102 #define WAKECODE_FIXUP(offset, type, val)       do {    \
  103         type    *addr;                                  \
  104         addr = (type *)(sc->acpi_wakeaddr + (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 = (sc->acpi_wakephys >> 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) = sc->acpi_wakephys >> 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 #ifdef __amd64__
  194         struct pcpu *pc;
  195         int i;
  196 #endif
  197 
  198         if (sc->acpi_wakeaddr == 0ul)
  199                 return (-1);    /* couldn't alloc wake memory */
  200 
  201 #ifdef SMP
  202         suspcpus = all_cpus;
  203         CPU_CLR(PCPU_GET(cpuid), &suspcpus);
  204 #endif
  205 
  206         if (acpi_resume_beep != 0)
  207                 timer_spkr_acquire();
  208 
  209         AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0);
  210 
  211         intr_suspend();
  212 
  213         pcb = &susppcbs[0]->sp_pcb;
  214         if (savectx(pcb)) {
  215 #ifdef __amd64__
  216                 fpususpend(susppcbs[0]->sp_fpususpend);
  217 #else
  218                 npxsuspend(susppcbs[0]->sp_fpususpend);
  219 #endif
  220 #ifdef SMP
  221                 if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
  222                         device_printf(sc->acpi_dev, "Failed to suspend APs\n");
  223                         return (0);     /* couldn't sleep */
  224                 }
  225 #endif
  226 #ifdef __amd64__
  227                 hw_ibrs_active = 0;
  228                 hw_ssb_active = 0;
  229                 cpu_stdext_feature3 = 0;
  230                 CPU_FOREACH(i) {
  231                         pc = pcpu_find(i);
  232                         pc->pc_ibpb_set = 0;
  233                 }
  234 #endif
  235 
  236                 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
  237                 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
  238 
  239 #ifdef __amd64__
  240                 WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER) &
  241                     ~(EFER_LMA));
  242 #else
  243                 WAKECODE_FIXUP(wakeup_cr4, register_t, pcb->pcb_cr4);
  244 #endif
  245                 WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
  246                 WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
  247                 WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
  248 
  249                 /* Call ACPICA to enter the desired sleep state */
  250                 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
  251                         status = AcpiEnterSleepStateS4bios();
  252                 else
  253                         status = AcpiEnterSleepState(state);
  254                 if (ACPI_FAILURE(status)) {
  255                         device_printf(sc->acpi_dev,
  256                             "AcpiEnterSleepState failed - %s\n",
  257                             AcpiFormatException(status));
  258                         return (0);     /* couldn't sleep */
  259                 }
  260 
  261                 for (;;)
  262                         ia32_pause();
  263         } else {
  264 #ifdef __amd64__
  265                 fpuresume(susppcbs[0]->sp_fpususpend);
  266 #else
  267                 npxresume(susppcbs[0]->sp_fpususpend);
  268 #endif
  269         }
  270 
  271         return (1);     /* wakeup successfully */
  272 }
  273 
  274 int
  275 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
  276     int intr_enabled)
  277 {
  278 
  279         if (sleep_result == -1)
  280                 return (sleep_result);
  281 
  282         if (!intr_enabled) {
  283                 /* Wakeup MD procedures in interrupt disabled context */
  284                 if (sleep_result == 1) {
  285                         pmap_init_pat();
  286                         initializecpu();
  287                         PCPU_SET(switchtime, 0);
  288                         PCPU_SET(switchticks, ticks);
  289 #ifdef DEV_APIC
  290                         lapic_xapic_mode();
  291 #endif
  292 #ifdef SMP
  293                         if (!CPU_EMPTY(&suspcpus))
  294                                 acpi_wakeup_cpus(sc);
  295 #endif
  296                 }
  297 
  298 #ifdef SMP
  299                 if (!CPU_EMPTY(&suspcpus))
  300                         resume_cpus(suspcpus);
  301 #endif
  302                 mca_resume();
  303 #ifdef __amd64__
  304                 if (vmm_resume_p != NULL)
  305                         vmm_resume_p();
  306 #endif
  307                 intr_resume(/*suspend_cancelled*/false);
  308 
  309                 AcpiSetFirmwareWakingVector(0, 0);
  310         } else {
  311                 /* Wakeup MD procedures in interrupt enabled context */
  312                 if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
  313                     mem_range_softc.mr_op->reinit != NULL)
  314                         mem_range_softc.mr_op->reinit(&mem_range_softc);
  315         }
  316 
  317         return (sleep_result);
  318 }
  319 
  320 static void *
  321 acpi_alloc_wakeup_handler(void *wakepages[ACPI_WAKEPAGES])
  322 {
  323         int             i;
  324 
  325         memset(wakepages, 0, ACPI_WAKEPAGES * sizeof(*wakepages));
  326 
  327         /*
  328          * Specify the region for our wakeup code.  We want it in the low 1 MB
  329          * region, excluding real mode IVT (0-0x3ff), BDA (0x400-0x4ff), EBDA
  330          * (less than 128KB, below 0xa0000, must be excluded by SMAP and DSDT),
  331          * and ROM area (0xa0000 and above).  The temporary page tables must be
  332          * page-aligned.
  333          */
  334         for (i = 0; i < ACPI_WAKEPAGES; i++) {
  335                 wakepages[i] = contigmalloc(PAGE_SIZE, M_DEVBUF, M_NOWAIT,
  336                     0x500, 0xa0000, PAGE_SIZE, 0ul);
  337                 if (wakepages[i] == NULL) {
  338                         printf("%s: can't alloc wake memory\n", __func__);
  339                         goto freepages;
  340                 }
  341         }
  342         if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
  343             EVENTHANDLER_PRI_LAST) == NULL) {
  344                 printf("%s: can't register event handler\n", __func__);
  345                 goto freepages;
  346         }
  347         susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
  348         for (i = 0; i < mp_ncpus; i++) {
  349                 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
  350                 susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
  351         }
  352 
  353         return (wakepages);
  354 
  355 freepages:
  356         for (i = 0; i < ACPI_WAKEPAGES; i++)
  357                 if (wakepages[i] != NULL)
  358                         contigfree(wakepages[i], PAGE_SIZE, M_DEVBUF);
  359         return (NULL);
  360 }
  361 
  362 void
  363 acpi_install_wakeup_handler(struct acpi_softc *sc)
  364 {
  365         static void     *wakeaddr;
  366         void            *wakepages[ACPI_WAKEPAGES];
  367 #ifdef __amd64__
  368         uint64_t        *pt4, *pt3, *pt2;
  369         vm_paddr_t      pt4pa, pt3pa, pt2pa;
  370         int             i;
  371 #endif
  372 
  373         if (wakeaddr != NULL)
  374                 return;
  375 
  376         if (acpi_alloc_wakeup_handler(wakepages) == NULL)
  377                 return;
  378 
  379         wakeaddr = wakepages[0];
  380         sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
  381         sc->acpi_wakephys = vtophys(wakeaddr);
  382 
  383 #ifdef __amd64__
  384         pt4 = wakepages[1];
  385         pt3 = wakepages[2];
  386         pt2 = wakepages[3];
  387         pt4pa = vtophys(pt4);
  388         pt3pa = vtophys(pt3);
  389         pt2pa = vtophys(pt2);
  390 #endif
  391 
  392         bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
  393 
  394         /* Patch GDT base address, ljmp targets. */
  395         WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
  396             sc->acpi_wakephys + bootgdt);
  397         WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
  398             sc->acpi_wakephys + wakeup_32);
  399 #ifdef __amd64__
  400         WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
  401             sc->acpi_wakephys + wakeup_64);
  402         WAKECODE_FIXUP(wakeup_pagetables, uint32_t, pt4pa);
  403 #endif
  404 
  405         /* Save pointers to some global data. */
  406         WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
  407 #ifndef __amd64__
  408 #if defined(PAE) || defined(PAE_TABLES)
  409         WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdpt));
  410 #else
  411         WAKECODE_FIXUP(wakeup_cr3, register_t, vtophys(kernel_pmap->pm_pdir));
  412 #endif
  413 
  414 #else /* __amd64__ */
  415         /* Create the initial 1GB replicated page tables */
  416         for (i = 0; i < 512; i++) {
  417                 /*
  418                  * Each slot of the level 4 pages points
  419                  * to the same level 3 page
  420                  */
  421                 pt4[i] = (uint64_t)pt3pa;
  422                 pt4[i] |= PG_V | PG_RW | PG_U;
  423 
  424                 /*
  425                  * Each slot of the level 3 pages points
  426                  * to the same level 2 page
  427                  */
  428                 pt3[i] = (uint64_t)pt2pa;
  429                 pt3[i] |= PG_V | PG_RW | PG_U;
  430 
  431                 /* The level 2 page slots are mapped with 2MB pages for 1GB. */
  432                 pt2[i] = i * (2 * 1024 * 1024);
  433                 pt2[i] |= PG_V | PG_RW | PG_PS | PG_U;
  434         }
  435 #endif /* !__amd64__ */
  436 
  437         if (bootverbose)
  438                 device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
  439                     (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
  440 }

Cache object: 4bd96a7d6c8dbd06354b4023133841a9


[ 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.