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-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 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/malloc.h>
   37 #include <sys/memrange.h>
   38 #include <sys/proc.h>
   39 #include <sys/sysctl.h>
   40 
   41 #include <vm/vm.h>
   42 #include <vm/pmap.h>
   43 #include <vm/vm_object.h>
   44 #include <vm/vm_page.h>
   45 #include <vm/vm_map.h>
   46 
   47 #include <machine/bus.h>
   48 #include <machine/cpufunc.h>
   49 #include <machine/intr_machdep.h>
   50 #include <machine/mca.h>
   51 #include <machine/segments.h>
   52 
   53 #include <contrib/dev/acpica/include/acpi.h>
   54 
   55 #include <dev/acpica/acpivar.h>
   56 
   57 #include "acpi_wakecode.h"
   58 
   59 /* Make sure the code is less than one page and leave room for the stack. */
   60 CTASSERT(sizeof(wakecode) < PAGE_SIZE - 1024);
   61 
   62 #ifndef _SYS_CDEFS_H_
   63 #error this file needs sys/cdefs.h as a prerequisite
   64 #endif
   65 
   66 extern uint32_t acpi_resume_beep;
   67 extern uint32_t acpi_reset_video;
   68 extern void     initializecpu(void);
   69 
   70 static struct region_descriptor __used  saved_idt, saved_gdt;
   71 static struct region_descriptor *p_gdt;
   72 static uint16_t __used  saved_ldt;
   73 
   74 static uint32_t __used  r_eax, r_ebx, r_ecx, r_edx, r_ebp, r_esi, r_edi,
   75                         r_efl, r_cr0, r_cr2, r_cr3, r_cr4, ret_addr;
   76 
   77 static uint16_t __used  r_cs, r_ds, r_es, r_fs, r_gs, r_ss, r_tr;
   78 static uint32_t __used  r_esp;
   79 
   80 static void             acpi_printcpu(void);
   81 static void             acpi_realmodeinst(void *arg, bus_dma_segment_t *segs,
   82                                           int nsegs, int error);
   83 static void             acpi_alloc_wakeup_handler(void);
   84 
   85 /* XXX shut gcc up */
   86 extern int              acpi_savecpu(void);
   87 extern int              acpi_restorecpu(void);
   88 
   89 #ifdef __GNUCLIKE_ASM
   90 __asm__("                               \n\
   91         .text                           \n\
   92         .p2align 2, 0x90                \n\
   93         .type acpi_restorecpu, @function\n\
   94 acpi_restorecpu:                        \n\
   95         .align 4                        \n\
   96         movl    r_eax,%eax              \n\
   97         movl    r_ebx,%ebx              \n\
   98         movl    r_ecx,%ecx              \n\
   99         movl    r_edx,%edx              \n\
  100         movl    r_ebp,%ebp              \n\
  101         movl    r_esi,%esi              \n\
  102         movl    r_edi,%edi              \n\
  103         movl    r_esp,%esp              \n\
  104                                         \n\
  105         pushl   r_efl                   \n\
  106         popfl                           \n\
  107                                         \n\
  108         movl    ret_addr,%eax           \n\
  109         movl    %eax,(%esp)             \n\
  110         xorl    %eax,%eax               \n\
  111         ret                             \n\
  112                                         \n\
  113         .text                           \n\
  114         .p2align 2, 0x90                \n\
  115         .type acpi_savecpu, @function   \n\
  116 acpi_savecpu:                           \n\
  117         movw    %cs,r_cs                \n\
  118         movw    %ds,r_ds                \n\
  119         movw    %es,r_es                \n\
  120         movw    %fs,r_fs                \n\
  121         movw    %gs,r_gs                \n\
  122         movw    %ss,r_ss                \n\
  123                                         \n\
  124         movl    %eax,r_eax              \n\
  125         movl    %ebx,r_ebx              \n\
  126         movl    %ecx,r_ecx              \n\
  127         movl    %edx,r_edx              \n\
  128         movl    %ebp,r_ebp              \n\
  129         movl    %esi,r_esi              \n\
  130         movl    %edi,r_edi              \n\
  131                                         \n\
  132         movl    %cr0,%eax               \n\
  133         movl    %eax,r_cr0              \n\
  134         movl    %cr2,%eax               \n\
  135         movl    %eax,r_cr2              \n\
  136         movl    %cr3,%eax               \n\
  137         movl    %eax,r_cr3              \n\
  138         movl    %cr4,%eax               \n\
  139         movl    %eax,r_cr4              \n\
  140                                         \n\
  141         pushfl                          \n\
  142         popl    r_efl                   \n\
  143                                         \n\
  144         movl    %esp,r_esp              \n\
  145                                         \n\
  146         sgdt    saved_gdt               \n\
  147         sidt    saved_idt               \n\
  148         sldt    saved_ldt               \n\
  149         str     r_tr                    \n\
  150                                         \n\
  151         movl    (%esp),%eax             \n\
  152         movl    %eax,ret_addr           \n\
  153         movl    $1,%eax                 \n\
  154         ret                             \n\
  155 ");
  156 #endif /* __GNUCLIKE_ASM */
  157 
  158 static void
  159 acpi_printcpu(void)
  160 {
  161         printf("======== acpi_printcpu() debug dump ========\n");
  162         printf("gdt[%04x:%08x] idt[%04x:%08x] ldt[%04x] tr[%04x] efl[%08x]\n",
  163                 saved_gdt.rd_limit, saved_gdt.rd_base,
  164                 saved_idt.rd_limit, saved_idt.rd_base,
  165                 saved_ldt, r_tr, r_efl);
  166         printf("eax[%08x] ebx[%08x] ecx[%08x] edx[%08x]\n",
  167                 r_eax, r_ebx, r_ecx, r_edx);
  168         printf("esi[%08x] edi[%08x] ebp[%08x] esp[%08x]\n",
  169                 r_esi, r_edi, r_ebp, r_esp);
  170         printf("cr0[%08x] cr2[%08x] cr3[%08x] cr4[%08x]\n",
  171                 r_cr0, r_cr2, r_cr3, r_cr4);
  172         printf("cs[%04x] ds[%04x] es[%04x] fs[%04x] gs[%04x] ss[%04x]\n",
  173                 r_cs, r_ds, r_es, r_fs, r_gs, r_ss);
  174 }
  175 
  176 #define WAKECODE_FIXUP(offset, type, val) do    {               \
  177         type    *addr;                                          \
  178         addr = (type *)(sc->acpi_wakeaddr + offset);            \
  179         *addr = val;                                            \
  180 } while (0)
  181 
  182 #define WAKECODE_BCOPY(offset, type, val) do    {               \
  183         void    *addr;                                          \
  184         addr = (void *)(sc->acpi_wakeaddr + offset);            \
  185         bcopy(&(val), addr, sizeof(type));                      \
  186 } while (0)
  187 
  188 /* Turn off bits 1&2 of the PIT, stopping the beep. */
  189 static void
  190 acpi_stop_beep(void *arg)
  191 {
  192         outb(0x61, inb(0x61) & ~0x3);
  193 }
  194 
  195 int
  196 acpi_sleep_machdep(struct acpi_softc *sc, int state)
  197 {
  198         ACPI_STATUS             status;
  199         struct pmap             *pm;
  200         int                     ret;
  201         uint32_t                cr3;
  202         u_long                  ef;
  203 
  204         ret = -1;
  205         if (sc->acpi_wakeaddr == 0)
  206                 return (ret);
  207 
  208         AcpiSetFirmwareWakingVector(sc->acpi_wakephys);
  209 
  210         ef = intr_disable();
  211         intr_suspend();
  212 
  213         /*
  214          * Temporarily switch to the kernel pmap because it provides an
  215          * identity mapping (setup at boot) for the low physical memory
  216          * region containing the wakeup code.
  217          */
  218         pm = kernel_pmap;
  219         cr3 = rcr3();
  220 #ifdef PAE
  221         load_cr3(vtophys(pm->pm_pdpt));
  222 #else
  223         load_cr3(vtophys(pm->pm_pdir));
  224 #endif
  225 
  226         ret_addr = 0;
  227         if (acpi_savecpu()) {
  228                 /* Execute Sleep */
  229 
  230                 p_gdt = (struct region_descriptor *)
  231                                 (sc->acpi_wakeaddr + physical_gdt);
  232                 p_gdt->rd_limit = saved_gdt.rd_limit;
  233                 p_gdt->rd_base = vtophys(saved_gdt.rd_base);
  234 
  235                 WAKECODE_FIXUP(physical_esp, uint32_t, vtophys(r_esp));
  236                 WAKECODE_FIXUP(previous_cr0, uint32_t, r_cr0);
  237                 WAKECODE_FIXUP(previous_cr2, uint32_t, r_cr2);
  238                 WAKECODE_FIXUP(previous_cr3, uint32_t, r_cr3);
  239                 WAKECODE_FIXUP(previous_cr4, uint32_t, r_cr4);
  240 
  241                 WAKECODE_FIXUP(resume_beep, uint32_t, acpi_resume_beep);
  242                 WAKECODE_FIXUP(reset_video, uint32_t, acpi_reset_video);
  243 
  244                 WAKECODE_FIXUP(previous_tr,  uint16_t, r_tr);
  245                 WAKECODE_BCOPY(previous_gdt, struct region_descriptor, saved_gdt);
  246                 WAKECODE_FIXUP(previous_ldt, uint16_t, saved_ldt);
  247                 WAKECODE_BCOPY(previous_idt, struct region_descriptor, saved_idt);
  248 
  249                 WAKECODE_FIXUP(where_to_recover, void *, acpi_restorecpu);
  250 
  251                 WAKECODE_FIXUP(previous_ds,  uint16_t, r_ds);
  252                 WAKECODE_FIXUP(previous_es,  uint16_t, r_es);
  253                 WAKECODE_FIXUP(previous_fs,  uint16_t, r_fs);
  254                 WAKECODE_FIXUP(previous_gs,  uint16_t, r_gs);
  255                 WAKECODE_FIXUP(previous_ss,  uint16_t, r_ss);
  256 
  257                 if (bootverbose)
  258                         acpi_printcpu();
  259 
  260                 /* Call ACPICA to enter the desired sleep state */
  261                 if (state == ACPI_STATE_S4 && sc->acpi_s4bios)
  262                         status = AcpiEnterSleepStateS4bios();
  263                 else
  264                         status = AcpiEnterSleepState(state);
  265 
  266                 if (status != AE_OK) {
  267                         device_printf(sc->acpi_dev,
  268                                 "AcpiEnterSleepState failed - %s\n",
  269                                 AcpiFormatException(status));
  270                         goto out;
  271                 }
  272 
  273                 for (;;)
  274                         ia32_pause();
  275         } else {
  276                 pmap_init_pat();
  277                 PCPU_SET(switchtime, 0);
  278                 PCPU_SET(switchticks, ticks);
  279                 if (bootverbose) {
  280                         acpi_savecpu();
  281                         acpi_printcpu();
  282                 }
  283                 ret = 0;
  284         }
  285 
  286 out:
  287         load_cr3(cr3);
  288         mca_resume();
  289         intr_resume();
  290         intr_restore(ef);
  291 
  292         if (ret == 0 && mem_range_softc.mr_op != NULL &&
  293             mem_range_softc.mr_op->reinit != NULL)
  294                 mem_range_softc.mr_op->reinit(&mem_range_softc);
  295 
  296         /* If we beeped, turn it off after a delay. */
  297         if (acpi_resume_beep)
  298                 timeout(acpi_stop_beep, NULL, 3 * hz);
  299 
  300         return (ret);
  301 }
  302 
  303 static bus_dma_tag_t    acpi_waketag;
  304 static bus_dmamap_t     acpi_wakemap;
  305 static vm_offset_t      acpi_wakeaddr;
  306 
  307 static void
  308 acpi_alloc_wakeup_handler(void)
  309 {
  310         void *wakeaddr;
  311 
  312         if (!cold)
  313                 return;
  314 
  315         /*
  316          * Specify the region for our wakeup code.  We want it in the low 1 MB
  317          * region, excluding video memory and above (0xa0000).  We ask for
  318          * it to be page-aligned, just to be safe.
  319          */
  320         if (bus_dma_tag_create(/*parent*/ NULL,
  321             /*alignment*/ PAGE_SIZE, /*no boundary*/ 0,
  322             /*lowaddr*/ 0x9ffff, /*highaddr*/ BUS_SPACE_MAXADDR, NULL, NULL,
  323             /*maxsize*/ PAGE_SIZE, /*segments*/ 1, /*maxsegsize*/ PAGE_SIZE,
  324             0, busdma_lock_mutex, &Giant, &acpi_waketag) != 0) {
  325                 printf("acpi_alloc_wakeup_handler: can't create wake tag\n");
  326                 return;
  327         }
  328         if (bus_dmamem_alloc(acpi_waketag, &wakeaddr, BUS_DMA_NOWAIT,
  329             &acpi_wakemap) != 0) {
  330                 printf("acpi_alloc_wakeup_handler: can't alloc wake memory\n");
  331                 return;
  332         }
  333         acpi_wakeaddr = (vm_offset_t)wakeaddr;
  334 }
  335 
  336 SYSINIT(acpiwakeup, SI_SUB_KMEM, SI_ORDER_ANY, acpi_alloc_wakeup_handler, 0);
  337 
  338 static void
  339 acpi_realmodeinst(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
  340 {
  341         struct acpi_softc *sc;
  342         uint32_t *addr;
  343 
  344         /* Overwrite the ljmp target with the real address */
  345         sc = arg;
  346         sc->acpi_wakephys = segs[0].ds_addr;
  347         addr = (uint32_t *)&wakecode[wakeup_sw32 + 2];
  348         *addr = sc->acpi_wakephys + wakeup_32;
  349 
  350         /* Copy the wake code into our low page and save its physical addr. */
  351         bcopy(wakecode, (void *)sc->acpi_wakeaddr, sizeof(wakecode));
  352         if (bootverbose) {
  353                 device_printf(sc->acpi_dev, "wakeup code va %#x pa %#jx\n",
  354                     acpi_wakeaddr, (uintmax_t)sc->acpi_wakephys);
  355         }
  356 }
  357 
  358 void
  359 acpi_install_wakeup_handler(struct acpi_softc *sc)
  360 {
  361         if (acpi_wakeaddr == 0)
  362                 return;
  363 
  364         sc->acpi_waketag = acpi_waketag;
  365         sc->acpi_wakeaddr = acpi_wakeaddr;
  366         sc->acpi_wakemap = acpi_wakemap;
  367 
  368         bus_dmamap_load(sc->acpi_waketag, sc->acpi_wakemap,
  369             (void *)sc->acpi_wakeaddr, PAGE_SIZE, acpi_realmodeinst, sc, 0);
  370 }

Cache object: 942345a0c2df12631837d41bf4f99d14


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