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/amd64/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  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
    5  * Copyright (c) 2001-2012 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
    6  * Copyright (c) 2003 Peter Wemm
    7  * Copyright (c) 2008-2012 Jung-uk Kim <jkim@FreeBSD.org>
    8  * All rights reserved.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 #include <sys/param.h>
   36 #include <sys/bus.h>
   37 #include <sys/eventhandler.h>
   38 #include <sys/kernel.h>
   39 #include <sys/malloc.h>
   40 #include <sys/memrange.h>
   41 #include <sys/smp.h>
   42 #include <sys/systm.h>
   43 #include <sys/cons.h>
   44 
   45 #include <vm/vm.h>
   46 #include <vm/pmap.h>
   47 #include <vm/vm_page.h>
   48 
   49 #include <machine/clock.h>
   50 #include <machine/cpu.h>
   51 #include <machine/intr_machdep.h>
   52 #include <machine/md_var.h>
   53 #include <x86/mca.h>
   54 #include <machine/pcb.h>
   55 #include <machine/specialreg.h>
   56 #include <x86/ucode.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 extern int              acpi_susp_bounce;
   78 
   79 #ifdef SMP
   80 extern struct susppcb   **susppcbs;
   81 static cpuset_t         suspcpus;
   82 #else
   83 static struct susppcb   **susppcbs;
   84 #endif
   85 
   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 #define ACPI_WAKEPT_PAGES       7
   94 
   95 #define WAKECODE_FIXUP(offset, type, val)       do {    \
   96         type    *addr;                                  \
   97         addr = (type *)(sc->acpi_wakeaddr + (offset));  \
   98         *addr = val;                                    \
   99 } while (0)
  100 
  101 static void
  102 acpi_stop_beep(void *arg)
  103 {
  104 
  105         if (acpi_resume_beep != 0)
  106                 timer_spkr_release();
  107 }
  108 
  109 #ifdef SMP
  110 static int
  111 acpi_wakeup_ap(struct acpi_softc *sc, int cpu)
  112 {
  113         struct pcb *pcb;
  114         int             vector = (sc->acpi_wakephys >> 12) & 0xff;
  115         int             apic_id = cpu_apic_ids[cpu];
  116         int             ms;
  117 
  118         pcb = &susppcbs[cpu]->sp_pcb;
  119         WAKECODE_FIXUP(wakeup_pcb, struct pcb *, pcb);
  120         WAKECODE_FIXUP(wakeup_gdt, uint16_t, pcb->pcb_gdt.rd_limit);
  121         WAKECODE_FIXUP(wakeup_gdt + 2, uint64_t, pcb->pcb_gdt.rd_base);
  122 
  123         ipi_startup(apic_id, vector);
  124 
  125         /* Wait up to 5 seconds for it to resume. */
  126         for (ms = 0; ms < 5000; ms++) {
  127                 if (!CPU_ISSET(cpu, &suspended_cpus))
  128                         return (1);     /* return SUCCESS */
  129                 DELAY(1000);
  130         }
  131         return (0);             /* return FAILURE */
  132 }
  133 
  134 #define WARMBOOT_TARGET         0
  135 #define WARMBOOT_OFF            (KERNBASE + 0x0467)
  136 #define WARMBOOT_SEG            (KERNBASE + 0x0469)
  137 
  138 #define CMOS_REG                (0x70)
  139 #define CMOS_DATA               (0x71)
  140 #define BIOS_RESET              (0x0f)
  141 #define BIOS_WARM               (0x0a)
  142 
  143 static void
  144 acpi_wakeup_cpus(struct acpi_softc *sc)
  145 {
  146         uint32_t        mpbioswarmvec;
  147         int             cpu;
  148         u_char          mpbiosreason;
  149 
  150         if (!efi_boot) {
  151                 /* save the current value of the warm-start vector */
  152                 mpbioswarmvec = *((uint32_t *)WARMBOOT_OFF);
  153                 outb(CMOS_REG, BIOS_RESET);
  154                 mpbiosreason = inb(CMOS_DATA);
  155 
  156                 /* setup a vector to our boot code */
  157                 *((volatile u_short *)WARMBOOT_OFF) = WARMBOOT_TARGET;
  158                 *((volatile u_short *)WARMBOOT_SEG) = sc->acpi_wakephys >> 4;
  159                 outb(CMOS_REG, BIOS_RESET);
  160                 outb(CMOS_DATA, BIOS_WARM);     /* 'warm-start' */
  161         }
  162 
  163         /* Wake up each AP. */
  164         for (cpu = 1; cpu < mp_ncpus; cpu++) {
  165                 if (!CPU_ISSET(cpu, &suspcpus))
  166                         continue;
  167                 if (acpi_wakeup_ap(sc, cpu) == 0) {
  168                         /* restore the warmstart vector */
  169                         *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
  170                         panic("acpi_wakeup: failed to resume AP #%d (PHY #%d)",
  171                             cpu, cpu_apic_ids[cpu]);
  172                 }
  173         }
  174 
  175         if (!efi_boot) {
  176                 /* restore the warmstart vector */
  177                 *(uint32_t *)WARMBOOT_OFF = mpbioswarmvec;
  178 
  179                 outb(CMOS_REG, BIOS_RESET);
  180                 outb(CMOS_DATA, mpbiosreason);
  181         }
  182 }
  183 #endif
  184 
  185 int
  186 acpi_sleep_machdep(struct acpi_softc *sc, int state)
  187 {
  188         ACPI_STATUS     status;
  189         struct pcb      *pcb;
  190         struct pcpu *pc;
  191         int i;
  192 
  193         if (sc->acpi_wakeaddr == 0ul)
  194                 return (-1);    /* couldn't alloc wake memory */
  195 
  196 #ifdef SMP
  197         suspcpus = all_cpus;
  198         CPU_CLR(PCPU_GET(cpuid), &suspcpus);
  199 #endif
  200 
  201         if (acpi_resume_beep != 0)
  202                 timer_spkr_acquire();
  203 
  204         AcpiSetFirmwareWakingVector(sc->acpi_wakephys, 0);
  205 
  206         intr_suspend();
  207 
  208         pcb = &susppcbs[0]->sp_pcb;
  209         if (savectx(pcb)) {
  210                 fpususpend(susppcbs[0]->sp_fpususpend);
  211 #ifdef SMP
  212                 if (!CPU_EMPTY(&suspcpus) && suspend_cpus(suspcpus) == 0) {
  213                         device_printf(sc->acpi_dev, "Failed to suspend APs\n");
  214                         return (0);     /* couldn't sleep */
  215                 }
  216 #endif
  217                 hw_ibrs_ibpb_active = 0;
  218                 hw_ssb_active = 0;
  219                 cpu_stdext_feature3 = 0;
  220                 CPU_FOREACH(i) {
  221                         pc = pcpu_find(i);
  222                         pc->pc_ibpb_set = 0;
  223                 }
  224 
  225                 WAKECODE_FIXUP(resume_beep, uint8_t, (acpi_resume_beep != 0));
  226                 WAKECODE_FIXUP(reset_video, uint8_t, (acpi_reset_video != 0));
  227 
  228                 WAKECODE_FIXUP(wakeup_efer, uint64_t, rdmsr(MSR_EFER) &
  229                     ~(EFER_LMA));
  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                 if (acpi_susp_bounce)
  247                         resumectx(pcb);
  248 
  249                 for (;;)
  250                         ia32_pause();
  251         } else {
  252                 /*
  253                  * Re-initialize console hardware as soon as possibe.
  254                  * No console output (e.g. printf) is allowed before
  255                  * this point.
  256                  */
  257                 cnresume();
  258                 fpuresume(susppcbs[0]->sp_fpususpend);
  259         }
  260 
  261         return (1);     /* wakeup successfully */
  262 }
  263 
  264 int
  265 acpi_wakeup_machdep(struct acpi_softc *sc, int state, int sleep_result,
  266     int intr_enabled)
  267 {
  268 
  269         if (sleep_result == -1)
  270                 return (sleep_result);
  271 
  272         if (!intr_enabled) {
  273                 /* Wakeup MD procedures in interrupt disabled context */
  274                 if (sleep_result == 1) {
  275                         ucode_reload();
  276                         pmap_init_pat();
  277                         initializecpu();
  278                         PCPU_SET(switchtime, 0);
  279                         PCPU_SET(switchticks, ticks);
  280                         lapic_xapic_mode();
  281 #ifdef SMP
  282                         if (!CPU_EMPTY(&suspcpus))
  283                                 acpi_wakeup_cpus(sc);
  284 #endif
  285                 }
  286 
  287 #ifdef SMP
  288                 if (!CPU_EMPTY(&suspcpus))
  289                         resume_cpus(suspcpus);
  290 #endif
  291                 mca_resume();
  292                 if (vmm_resume_p != NULL)
  293                         vmm_resume_p();
  294                 intr_resume(/*suspend_cancelled*/false);
  295 
  296                 AcpiSetFirmwareWakingVector(0, 0);
  297         } else {
  298                 /* Wakeup MD procedures in interrupt enabled context */
  299                 if (sleep_result == 1 && mem_range_softc.mr_op != NULL &&
  300                     mem_range_softc.mr_op->reinit != NULL)
  301                         mem_range_softc.mr_op->reinit(&mem_range_softc);
  302         }
  303 
  304         return (sleep_result);
  305 }
  306 
  307 static void
  308 acpi_alloc_wakeup_handler(void **wakeaddr,
  309     void *wakept_pages[ACPI_WAKEPT_PAGES])
  310 {
  311         vm_page_t wakept_m[ACPI_WAKEPT_PAGES];
  312         int i;
  313 
  314         *wakeaddr = NULL;
  315         memset(wakept_pages, 0, ACPI_WAKEPT_PAGES * sizeof(*wakept_pages));
  316         memset(wakept_m, 0, ACPI_WAKEPT_PAGES * sizeof(*wakept_m));
  317 
  318         /*
  319          * Specify the region for our wakeup code.  We want it in the
  320          * low 1 MB region, excluding real mode IVT (0-0x3ff), BDA
  321          * (0x400-0x4ff), EBDA (less than 128KB, below 0xa0000, must
  322          * be excluded by SMAP and DSDT), and ROM area (0xa0000 and
  323          * above).
  324          */
  325         *wakeaddr = contigmalloc(PAGE_SIZE, M_DEVBUF,
  326             M_NOWAIT, 0x500, 0xa0000, PAGE_SIZE, 0ul);
  327         if (*wakeaddr == NULL) {
  328                 printf("%s: can't alloc wake memory\n", __func__);
  329                 goto freepages;
  330         }
  331 
  332         for (i = 0; i < ACPI_WAKEPT_PAGES - (la57 ? 0 : 1); i++) {
  333                 wakept_m[i] = pmap_page_alloc_below_4g(true);
  334                 wakept_pages[i] = (void *)PHYS_TO_DMAP(VM_PAGE_TO_PHYS(
  335                     wakept_m[i]));
  336         }
  337         if (EVENTHANDLER_REGISTER(power_resume, acpi_stop_beep, NULL,
  338             EVENTHANDLER_PRI_LAST) == NULL) {
  339                 printf("%s: can't register event handler\n", __func__);
  340                 goto freepages;
  341         }
  342         susppcbs = malloc(mp_ncpus * sizeof(*susppcbs), M_DEVBUF, M_WAITOK);
  343         for (i = 0; i < mp_ncpus; i++) {
  344                 susppcbs[i] = malloc(sizeof(**susppcbs), M_DEVBUF, M_WAITOK);
  345                 susppcbs[i]->sp_fpususpend = alloc_fpusave(M_WAITOK);
  346         }
  347         return;
  348 
  349 freepages:
  350         if (*wakeaddr != NULL)
  351                 contigfree(*wakeaddr, PAGE_SIZE, M_DEVBUF);
  352         for (i = 0; i < ACPI_WAKEPT_PAGES; i++) {
  353                 if (wakept_m[i] != NULL)
  354                         vm_page_free(wakept_m[i]);
  355         }
  356         *wakeaddr = NULL;
  357 }
  358 
  359 void
  360 acpi_install_wakeup_handler(struct acpi_softc *sc)
  361 {
  362         static void *wakeaddr;
  363         void *wakept_pages[ACPI_WAKEPT_PAGES];
  364         uint64_t *pt5, *pt4, *pt3, *pt2_0, *pt2_1, *pt2_2, *pt2_3;
  365         vm_paddr_t pt5pa, pt4pa, pt3pa, pt2_0pa, pt2_1pa, pt2_2pa, pt2_3pa;
  366         int i;
  367 
  368         if (wakeaddr != NULL)
  369                 return;
  370         acpi_alloc_wakeup_handler(&wakeaddr, wakept_pages);
  371         if (wakeaddr == NULL)
  372                 return;
  373 
  374         sc->acpi_wakeaddr = (vm_offset_t)wakeaddr;
  375         sc->acpi_wakephys = vtophys(wakeaddr);
  376 
  377         if (la57) {
  378                 pt5 = wakept_pages[6];
  379                 pt5pa = vtophys(pt5);
  380         }
  381         pt4 = wakept_pages[0];
  382         pt3 = wakept_pages[1];
  383         pt2_0 = wakept_pages[2];
  384         pt2_1 = wakept_pages[3];
  385         pt2_2 = wakept_pages[4];
  386         pt2_3 = wakept_pages[5];
  387         pt4pa = vtophys(pt4);
  388         pt3pa = vtophys(pt3);
  389         pt2_0pa = vtophys(pt2_0);
  390         pt2_1pa = vtophys(pt2_1);
  391         pt2_2pa = vtophys(pt2_2);
  392         pt2_3pa = vtophys(pt2_3);
  393 
  394         bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
  395 
  396         /* Patch GDT base address, ljmp targets. */
  397         WAKECODE_FIXUP((bootgdtdesc + 2), uint32_t,
  398             sc->acpi_wakephys + bootgdt);
  399         WAKECODE_FIXUP((wakeup_sw32 + 2), uint32_t,
  400             sc->acpi_wakephys + wakeup_32);
  401         WAKECODE_FIXUP((wakeup_sw64 + 1), uint32_t,
  402             sc->acpi_wakephys + wakeup_64);
  403         WAKECODE_FIXUP(wakeup_pagetables, uint32_t, la57 ? (pt5pa | 0x1) :
  404             pt4pa);
  405 
  406         /* Save pointers to some global data. */
  407         WAKECODE_FIXUP(wakeup_ret, void *, resumectx);
  408         /* Create 1:1 mapping for the low 4G */
  409         if (la57) {
  410                 bcopy(kernel_pmap->pm_pmltop, pt5, PAGE_SIZE);
  411                 pt5[0] = (uint64_t)pt4pa;
  412                 pt5[0] |= PG_V | PG_RW | PG_U;
  413         } else {
  414                 bcopy(kernel_pmap->pm_pmltop, pt4, PAGE_SIZE);
  415         }
  416 
  417         pt4[0] = (uint64_t)pt3pa;
  418         pt4[0] |= PG_V | PG_RW | PG_U;
  419 
  420         pt3[0] = (uint64_t)pt2_0pa;
  421         pt3[0] |= PG_V | PG_RW | PG_U;
  422         pt3[1] = (uint64_t)pt2_1pa;
  423         pt3[1] |= PG_V | PG_RW | PG_U;
  424         pt3[2] = (uint64_t)pt2_2pa;
  425         pt3[2] |= PG_V | PG_RW | PG_U;
  426         pt3[3] = (uint64_t)pt2_3pa;
  427         pt3[3] |= PG_V | PG_RW | PG_U;
  428 
  429         for (i = 0; i < NPDEPG; i++) {
  430                 pt2_0[i] = (pd_entry_t)i * NBPDR;
  431                 pt2_0[i] |= PG_V | PG_RW | PG_PS | PG_U;
  432         }
  433         for (i = 0; i < NPDEPG; i++) {
  434                 pt2_1[i] = (pd_entry_t)NBPDP + i * NBPDR;
  435                 pt2_1[i] |= PG_V | PG_RW | PG_PS | PG_U;
  436         }
  437         for (i = 0; i < NPDEPG; i++) {
  438                 pt2_2[i] = (pd_entry_t)2 * NBPDP + i * NBPDR;
  439                 pt2_2[i] |= PG_V | PG_RW | PG_PS | PG_U;
  440         }
  441         for (i = 0; i < NPDEPG; i++) {
  442                 pt2_3[i] = (pd_entry_t)3 * NBPDP + i * NBPDR;
  443                 pt2_3[i] |= PG_V | PG_RW | PG_PS | PG_U;
  444         }
  445 
  446         if (bootverbose)
  447                 device_printf(sc->acpi_dev, "wakeup code va %#jx pa %#jx\n",
  448                     (uintmax_t)sc->acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
  449 }

Cache object: 7a5597cccd09ba99610aa601304ee42c


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