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

Cache object: 074430279864b15ea3a30c530460e9ab


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