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/i386/acpica/acpi_wakeup.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2001 Takanori Watanabe <takawata@jp.freebsd.org>
    3  * Copyright (c) 2001 Mitsuru IWASAKI <iwasaki@jp.freebsd.org>
    4  * All rights reserved.
    5  *
    6  * Redistribution and use in source and binary forms, with or without
    7  * modification, are permitted provided that the following conditions
    8  * are met:
    9  * 1. Redistributions of source code must retain the above copyright
   10  *    notice, this list of conditions and the following disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD$");
   30 
   31 #include <sys/param.h>
   32 #include <sys/systm.h>
   33 #include <sys/kernel.h>
   34 #include <sys/bus.h>
   35 #include <sys/lock.h>
   36 #include <sys/proc.h>
   37 #include <sys/sysctl.h>
   38 
   39 #include <vm/vm.h>
   40 #include <vm/pmap.h>
   41 #include <vm/vm_object.h>
   42 #include <vm/vm_page.h>
   43 #include <vm/vm_map.h>
   44 
   45 #include <machine/bus.h>
   46 #include <machine/cpufunc.h>
   47 #include <machine/intr_machdep.h>
   48 #include <machine/segments.h>
   49 
   50 #include <contrib/dev/acpica/acpi.h>
   51 #include <dev/acpica/acpivar.h>
   52 
   53 #include "acpi_wakecode.h"
   54 
   55 /* Make sure the code is less than one page and leave room for the stack. */
   56 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
   57 
   58 #ifndef _SYS_CDEFS_H_
   59 #error this file needs sys/cdefs.h as a prerequisite
   60 #endif
   61 
   62 extern uint32_t acpi_resume_beep;
   63 extern uint32_t acpi_reset_video;
   64 extern void     initializecpu(void);
   65 
   66 static struct region_descriptor saved_idt, saved_gdt, *p_gdt;
   67 static uint16_t         saved_ldt;
   68 
   69 static uint32_t         r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi,
   70                         r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr;
   71 
   72 static uint16_t         r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
   73 static uint32_t         r_esp;
   74 
   75 static void             acpi_printcpu(void);
   76 static void             acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
   77                                           int nsegs, int error);
   78 static void             acpi_alloc_wakeup_handler(void);
   79 
   80 /* XXX shut gcc up */
   81 extern int              acpi_savecpu(void);
   82 extern int              acpi_restorecpu(void);
   83 
   84 #ifdef __GNUCLIKE_ASM
   85 __asm__("                               \n\
   86         .text                           \n\
   87         .p2align 2, 0x90                \n\
   88         .type acpi_restorecpu, @function\n\
   89 acpi_restorecpu:                        \n\
   90         .align 4                        \n\
   91         movl    r_eax,%eax              \n\
   92         movl    r_ebx,%ebx              \n\
   93         movl    r_ecx,%ecx              \n\
   94         movl    r_edx,%edx              \n\
   95         movl    r_ebp,%ebp              \n\
   96         movl    r_esi,%esi              \n\
   97         movl    r_edi,%edi              \n\
   98         movl    r_esp,%esp              \n\
   99                                         \n\
  100         pushl   r_efl                   \n\
  101         popfl                           \n\
  102                                         \n\
  103         movl    ret_addr,%eax           \n\
  104         movl    %eax,(%esp)             \n\
  105         xorl    %eax,%eax               \n\
  106         ret                             \n\
  107                                         \n\
  108         .text                           \n\
  109         .p2align 2, 0x90                \n\
  110         .type acpi_savecpu, @function   \n\
  111 acpi_savecpu:                           \n\
  112         movw    %cs,r_cs                \n\
  113         movw    %ds,r_ds                \n\
  114         movw    %es,r_es                \n\
  115         movw    %fs,r_fs                \n\
  116         movw    %gs,r_gs                \n\
  117         movw    %ss,r_ss                \n\
  118                                         \n\
  119         movl    %eax,r_eax              \n\
  120         movl    %ebx,r_ebx              \n\
  121         movl    %ecx,r_ecx              \n\
  122         movl    %edx,r_edx              \n\
  123         movl    %ebp,r_ebp              \n\
  124         movl    %esi,r_esi              \n\
  125         movl    %edi,r_edi              \n\
  126                                         \n\
  127         movl    %cr0,%eax               \n\
  128         movl    %eax,r_cr0              \n\
  129         movl    %cr2,%eax               \n\
  130         movl    %eax,r_cr2              \n\
  131         movl    %cr3,%eax               \n\
  132         movl    %eax,r_cr3              \n\
  133         movl    %cr4,%eax               \n\
  134         movl    %eax,r_cr4              \n\
  135                                         \n\
  136         pushfl                          \n\
  137         popl    r_efl                   \n\
  138                                         \n\
  139         movl    %esp,r_esp              \n\
  140                                         \n\
  141         sgdt    saved_gdt               \n\
  142         sidt    saved_idt               \n\
  143         sldt    saved_ldt               \n\
  144         str     r_tr                    \n\
  145                                         \n\
  146         movl    (%esp),%eax             \n\
  147         movl    %eax,ret_addr           \n\
  148         movl    $1,%eax                 \n\
  149         ret                             \n\
  150 ");
  151 #endif /* __GNUCLIKE_ASM */
  152 
  153 static void
  154 acpi_printcpu(void)
  155 {
  156         printf("======== acpi_printcpu() debug dump ========\n");
  157         printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n",
  158                 saved_gdt.rd_limit, saved_gdt.rd_base,
  159                 saved_idt.rd_limit, saved_idt.rd_base,
  160                 saved_ldt, r_tr, r_efl);
  161         printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n",
  162                 r_eax, r_ebx, r_ecx, r_edx);
  163         printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n",
  164                 r_esi, r_edi, r_ebp, r_esp);
  165         printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n",
  166                 r_cr0, r_cr2, r_cr3, r_cr4);
  167         printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n",
  168                 r_cs, r_ds, r_es, r_fs, r_gs, r_ss);
  169 }
  170 
  171 #define WAKECODE_FIXUP(offset, type, val) do    {               \
  172         type    *addr;                                          \
  173         addr = (type *)(sc->acpi_wakeaddr + offset);            \
  174         *addr = val;                                            \
  175 } while (0)
  176 
  177 #define WAKECODE_BCOPY(offset, type, val) do    {               \
  178         void    *addr;                                          \
  179         addr = (void *)(sc->acpi_wakeaddr + offset);            \
  180         bcopy(&(val), addr, sizeof(type));                      \
  181 } while (0)
  182 
  183 /* Turn off bits 1&2 of the PIT, stopping the beep. */
  184 static void
  185 acpi_stop_beep(void *arg)
  186 {
  187         outb(0x61, inb(0x61) & ~0x3);
  188 }
  189 
  190 int
  191 acpi_sleep_machdep(struct acpi_softc *sc, int state)
  192 {
  193         ACPI_STATUS             status;
  194         struct pmap             *pm;
  195         int                     ret;
  196         uint32_t                cr3;
  197         u_long                  ef;
  198 
  199         ret = 0;
  200         if (sc->acpi_wakeaddr == 0)
  201                 return (0);
  202 
  203         AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
  204 
  205         ef = read_eflags();
  206 
  207         /*
  208          * Temporarily switch to the kernel pmap because it provides an
  209          * identity mapping (setup at boot) for the low physical memory
  210          * region containing the wakeup code.
  211          */
  212         pm = kernel_pmap;
  213         cr3 = rcr3();
  214 #ifdef PAE
  215         load_cr3(vtophys(pm->pm_pdpt));
  216 #else
  217         load_cr3(vtophys(pm->pm_pdir));
  218 #endif
  219 
  220         ret_addr = 0;
  221         ACPI_DISABLE_IRQS();
  222         if (acpi_savecpu()) {
  223                 /* Execute Sleep */
  224                 intr_suspend();
  225 
  226                 p_gdt = (struct region_descriptor *)
  227                                 (sc->acpi_wakeaddr + physical_gdt);
  228                 p_gdt->rd_limit = saved_gdt.rd_limit;
  229                 p_gdt->rd_base = vtophys(saved_gdt.rd_base);
  230 
  231                 WAKECODE_FIXUP(physical_esp, uint32_t, vtophys(r_esp));
  232                 WAKECODE_FIXUP(previous_cr0, uint32_t, r_cr0);
  233                 WAKECODE_FIXUP(previous_cr2, uint32_t, r_cr2);
  234                 WAKECODE_FIXUP(previous_cr3, uint32_t, r_cr3);
  235                 WAKECODE_FIXUP(previous_cr4, uint32_t, r_cr4);
  236 
  237                 WAKECODE_FIXUP(resume_beep, uint32_t, acpi_resume_beep);
  238                 WAKECODE_FIXUP(reset_video, uint32_t, acpi_reset_video);
  239 
  240                 WAKECODE_FIXUP(previous_tr,  uint16_t, r_tr);
  241                 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, saved_gdt);
  242                 WAKECODE_FIXUP(previous_ldt, uint16_t, saved_ldt);
  243                 WAKECODE_BCOPY(previous_idt, struct region_descriptor, saved_idt);
  244 
  245                 WAKECODE_FIXUP(where_to_recover, void *, acpi_restorecpu);
  246 
  247                 WAKECODE_FIXUP(previous_ds,  uint16_t, r_ds);
  248                 WAKECODE_FIXUP(previous_es,  uint16_t, r_es);
  249                 WAKECODE_FIXUP(previous_fs,  uint16_t, r_fs);
  250                 WAKECODE_FIXUP(previous_gs,  uint16_t, r_gs);
  251                 WAKECODE_FIXUP(previous_ss,  uint16_t, r_ss);
  252 
  253                 if (bootverbose)
  254                         acpi_printcpu();
  255 
  256                 /* Call ACPICA to enter the desired sleep state */
  257                 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
  258                         status = AcpiEnterSleepStateS4bios();
  259                 else
  260                         status = AcpiEnterSleepState(state);
  261 
  262                 if (status != AE_OK) {
  263                         device_printf(sc->acpi_dev,
  264                                 "AcpiEnterSleepState failed - %s\n",
  265                                 AcpiFormatException(status));
  266                         ret = -1;
  267                         goto out;
  268                 }
  269 
  270                 for (;;) ;
  271         } else {
  272                 /* Execute Wakeup */
  273                 intr_resume();
  274 
  275                 if (bootverbose) {
  276                         acpi_savecpu();
  277                         acpi_printcpu();
  278                 }
  279         }
  280 
  281 out:
  282         load_cr3(cr3);
  283         write_eflags(ef);
  284 
  285         /* If we beeped, turn it off after a delay. */
  286         if (acpi_resume_beep)
  287                 timeout(acpi_stop_beep, NULL, 3 * hz);
  288 
  289         return (ret);
  290 }
  291 
  292 static bus_dma_tag_t    acpi_waketag;
  293 static bus_dmamap_t     acpi_wakemap;
  294 static vm_offset_t      acpi_wakeaddr;
  295 
  296 static void
  297 acpi_alloc_wakeup_handler(void)
  298 {
  299         void *wakeaddr;
  300 
  301         if (!cold)
  302                 return;
  303 
  304         /*
  305          * Specify the region for our wakeup code.  We want it in the low 1 MB
  306          * region, excluding video memory and above (0xa0000).  We ask for
  307          * it to be page-aligned, just to be safe.
  308          */
  309         if (bus_dma_tag_create(/*parent*/ NULL,
  310             /*alignment*/ PAGE_SIZE, /*no boundary*/ 0,
  311             /*lowaddr*/ 0x9ffff, /*highaddr*/ BUS_SPACE_MAXADDR, NULL, NULL,
  312             /*maxsize*/ PAGE_SIZE, /*segments*/ 1, /*maxsegsize*/ PAGE_SIZE,
  313             0, busdma_lock_mutex, &Giant, &acpi_waketag) != 0) {
  314                 printf("acpi_alloc_wakeup_handler: can't create wake tag\n");
  315                 return;
  316         }
  317         if (bus_dmamem_alloc(acpi_waketag, &wakeaddr, BUS_DMA_NOWAIT,
  318             &acpi_wakemap) != 0) {
  319                 printf("acpi_alloc_wakeup_handler: can't alloc wake memory\n");
  320                 return;
  321         }
  322         acpi_wakeaddr = (vm_offset_t)wakeaddr;
  323 }
  324 
  325 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0)
  326 
  327 static void
  328 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
  329 {
  330         struct acpi_softc *sc;
  331         uint32_t *addr;
  332 
  333         /* Overwrite the ljmp target with the real address */
  334         sc = arg;
  335         sc->acpi_wakephys = segs[0].ds_addr;
  336         addr = (uint32_t *)&wakecode[wakeup_sw32 + 2];
  337         *addr = sc->acpi_wakephys + wakeup_32;
  338 
  339         /* Copy the wake code into our low page and save its physical addr. */
  340         bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
  341         if (bootverbose) {
  342                 device_printf(sc->acpi_dev, "wakeup code va %#x pa %#jx\n",
  343                     acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
  344         }
  345 }
  346 
  347 void
  348 acpi_install_wakeup_handler(struct acpi_softc *sc)
  349 {
  350         if (acpi_wakeaddr == 0)
  351                 return;
  352 
  353         sc->acpi_waketag = acpi_waketag;
  354         sc->acpi_wakeaddr = acpi_wakeaddr;
  355         sc->acpi_wakemap = acpi_wakemap;
  356 
  357         bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
  358             (void *)sc->acpi_wakeaddr, PAGE_SIZE, acpi_realmodeinst, sc, 0);
  359 }

Cache object: 19fb7ce3fad444b3c56f21980ac65ff6


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