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/dev/acpica/acpi_cpu.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) 2003-2005 Nate Lawson (SDG)
    3  * Copyright (c) 2001 Michael Smith
    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/8.4/sys/dev/acpica/acpi_cpu.c 244632 2012-12-23 16:11:43Z avg $");
   30 
   31 #include "opt_acpi.h"
   32 #include <sys/param.h>
   33 #include <sys/bus.h>
   34 #include <sys/cpu.h>
   35 #include <sys/kernel.h>
   36 #include <sys/malloc.h>
   37 #include <sys/module.h>
   38 #include <sys/pcpu.h>
   39 #include <sys/power.h>
   40 #include <sys/proc.h>
   41 #include <sys/sched.h>
   42 #include <sys/sbuf.h>
   43 #include <sys/smp.h>
   44 
   45 #include <dev/pci/pcivar.h>
   46 #include <machine/atomic.h>
   47 #include <machine/bus.h>
   48 #include <sys/rman.h>
   49 
   50 #include <contrib/dev/acpica/include/acpi.h>
   51 #include <contrib/dev/acpica/include/accommon.h>
   52 
   53 #include <dev/acpica/acpivar.h>
   54 
   55 /*
   56  * Support for ACPI Processor devices, including C[1-3] sleep states.
   57  */
   58 
   59 /* Hooks for the ACPI CA debugging infrastructure */
   60 #define _COMPONENT      ACPI_PROCESSOR
   61 ACPI_MODULE_NAME("PROCESSOR")
   62 
   63 struct acpi_cx {
   64     struct resource     *p_lvlx;        /* Register to read to enter state. */
   65     uint32_t             type;          /* C1-3 (C4 and up treated as C3). */
   66     uint32_t             trans_lat;     /* Transition latency (usec). */
   67     uint32_t             power;         /* Power consumed (mW). */
   68     int                  res_type;      /* Resource type for p_lvlx. */
   69     int                  res_rid;       /* Resource ID for p_lvlx. */
   70 };
   71 #define MAX_CX_STATES    8
   72 
   73 struct acpi_cpu_softc {
   74     device_t             cpu_dev;
   75     ACPI_HANDLE          cpu_handle;
   76     struct pcpu         *cpu_pcpu;
   77     uint32_t             cpu_acpi_id;   /* ACPI processor id */
   78     uint32_t             cpu_p_blk;     /* ACPI P_BLK location */
   79     uint32_t             cpu_p_blk_len; /* P_BLK length (must be 6). */
   80     struct acpi_cx       cpu_cx_states[MAX_CX_STATES];
   81     int                  cpu_cx_count;  /* Number of valid Cx states. */
   82     int                  cpu_prev_sleep;/* Last idle sleep duration. */
   83     int                  cpu_features;  /* Child driver supported features. */
   84     /* Runtime state. */
   85     int                  cpu_non_c3;    /* Index of lowest non-C3 state. */
   86     u_int                cpu_cx_stats[MAX_CX_STATES];/* Cx usage history. */
   87     /* Values for sysctl. */
   88     struct sysctl_ctx_list cpu_sysctl_ctx;
   89     struct sysctl_oid   *cpu_sysctl_tree;
   90     int                  cpu_cx_lowest;
   91     int                  cpu_cx_lowest_lim;
   92     int                  cpu_disable_idle; /* Disable entry to idle function */
   93     char                 cpu_cx_supported[64];
   94 };
   95 
   96 struct acpi_cpu_device {
   97     struct resource_list        ad_rl;
   98 };
   99 
  100 #define CPU_GET_REG(reg, width)                                         \
  101     (bus_space_read_ ## width(rman_get_bustag((reg)),                   \
  102                       rman_get_bushandle((reg)), 0))
  103 #define CPU_SET_REG(reg, width, val)                                    \
  104     (bus_space_write_ ## width(rman_get_bustag((reg)),                  \
  105                        rman_get_bushandle((reg)), 0, (val)))
  106 
  107 #define PM_USEC(x)       ((x) >> 2)     /* ~4 clocks per usec (3.57955 Mhz) */
  108 
  109 #define ACPI_NOTIFY_CX_STATES   0x81    /* _CST changed. */
  110 
  111 #define CPU_QUIRK_NO_C3         (1<<0)  /* C3-type states are not usable. */
  112 #define CPU_QUIRK_NO_BM_CTRL    (1<<2)  /* No bus mastering control. */
  113 
  114 #define PCI_VENDOR_INTEL        0x8086
  115 #define PCI_DEVICE_82371AB_3    0x7113  /* PIIX4 chipset for quirks. */
  116 #define PCI_REVISION_A_STEP     0
  117 #define PCI_REVISION_B_STEP     1
  118 #define PCI_REVISION_4E         2
  119 #define PCI_REVISION_4M         3
  120 #define PIIX4_DEVACTB_REG       0x58
  121 #define PIIX4_BRLD_EN_IRQ0      (1<<0)
  122 #define PIIX4_BRLD_EN_IRQ       (1<<1)
  123 #define PIIX4_BRLD_EN_IRQ8      (1<<5)
  124 #define PIIX4_STOP_BREAK_MASK   (PIIX4_BRLD_EN_IRQ0 | PIIX4_BRLD_EN_IRQ | PIIX4_BRLD_EN_IRQ8)
  125 #define PIIX4_PCNTRL_BST_EN     (1<<10)
  126 
  127 /* Allow users to ignore processor orders in MADT. */
  128 static int cpu_unordered;
  129 TUNABLE_INT("debug.acpi.cpu_unordered", &cpu_unordered);
  130 SYSCTL_INT(_debug_acpi, OID_AUTO, cpu_unordered, CTLFLAG_RDTUN,
  131     &cpu_unordered, 0,
  132     "Do not use the MADT to match ACPI Processor objects to CPUs.");
  133 
  134 /* Platform hardware resource information. */
  135 static uint32_t          cpu_smi_cmd;   /* Value to write to SMI_CMD. */
  136 static uint8_t           cpu_cst_cnt;   /* Indicate we are _CST aware. */
  137 static int               cpu_quirks;    /* Indicate any hardware bugs. */
  138 
  139 /* Values for sysctl. */
  140 static struct sysctl_ctx_list cpu_sysctl_ctx;
  141 static struct sysctl_oid *cpu_sysctl_tree;
  142 static int               cpu_cx_generic;
  143 static int               cpu_cx_lowest_lim;
  144 
  145 static device_t         *cpu_devices;
  146 static int               cpu_ndevices;
  147 static struct acpi_cpu_softc **cpu_softc;
  148 ACPI_SERIAL_DECL(cpu, "ACPI CPU");
  149 
  150 static int      acpi_cpu_probe(device_t dev);
  151 static int      acpi_cpu_attach(device_t dev);
  152 static int      acpi_cpu_suspend(device_t dev);
  153 static int      acpi_cpu_resume(device_t dev);
  154 static int      acpi_pcpu_get_id(device_t dev, uint32_t *acpi_id,
  155                     uint32_t *cpu_id);
  156 static struct resource_list *acpi_cpu_get_rlist(device_t dev, device_t child);
  157 static device_t acpi_cpu_add_child(device_t dev, u_int order, const char *name,
  158                     int unit);
  159 static int      acpi_cpu_read_ivar(device_t dev, device_t child, int index,
  160                     uintptr_t *result);
  161 static int      acpi_cpu_shutdown(device_t dev);
  162 static void     acpi_cpu_cx_probe(struct acpi_cpu_softc *sc);
  163 static void     acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc);
  164 static int      acpi_cpu_cx_cst(struct acpi_cpu_softc *sc);
  165 static void     acpi_cpu_startup(void *arg);
  166 static void     acpi_cpu_startup_cx(struct acpi_cpu_softc *sc);
  167 static void     acpi_cpu_cx_list(struct acpi_cpu_softc *sc);
  168 static void     acpi_cpu_idle(void);
  169 static void     acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context);
  170 static int      acpi_cpu_quirks(void);
  171 static int      acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS);
  172 static int      acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc);
  173 static int      acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
  174 static int      acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS);
  175 
  176 static device_method_t acpi_cpu_methods[] = {
  177     /* Device interface */
  178     DEVMETHOD(device_probe,     acpi_cpu_probe),
  179     DEVMETHOD(device_attach,    acpi_cpu_attach),
  180     DEVMETHOD(device_detach,    bus_generic_detach),
  181     DEVMETHOD(device_shutdown,  acpi_cpu_shutdown),
  182     DEVMETHOD(device_suspend,   acpi_cpu_suspend),
  183     DEVMETHOD(device_resume,    acpi_cpu_resume),
  184 
  185     /* Bus interface */
  186     DEVMETHOD(bus_add_child,    acpi_cpu_add_child),
  187     DEVMETHOD(bus_read_ivar,    acpi_cpu_read_ivar),
  188     DEVMETHOD(bus_get_resource_list, acpi_cpu_get_rlist),
  189     DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource),
  190     DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource),
  191     DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource),
  192     DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource),
  193     DEVMETHOD(bus_activate_resource, bus_generic_activate_resource),
  194     DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
  195     DEVMETHOD(bus_setup_intr,   bus_generic_setup_intr),
  196     DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
  197 
  198     DEVMETHOD_END
  199 };
  200 
  201 static driver_t acpi_cpu_driver = {
  202     "cpu",
  203     acpi_cpu_methods,
  204     sizeof(struct acpi_cpu_softc),
  205 };
  206 
  207 static devclass_t acpi_cpu_devclass;
  208 DRIVER_MODULE(cpu, acpi, acpi_cpu_driver, acpi_cpu_devclass, 0, 0);
  209 MODULE_DEPEND(cpu, acpi, 1, 1, 1);
  210 
  211 static int
  212 acpi_cpu_probe(device_t dev)
  213 {
  214     int                    acpi_id, cpu_id;
  215     ACPI_BUFFER            buf;
  216     ACPI_HANDLE            handle;
  217     ACPI_OBJECT            *obj;
  218     ACPI_STATUS            status;
  219 
  220     if (acpi_disabled("cpu") || acpi_get_type(dev) != ACPI_TYPE_PROCESSOR)
  221         return (ENXIO);
  222 
  223     handle = acpi_get_handle(dev);
  224     if (cpu_softc == NULL)
  225         cpu_softc = malloc(sizeof(struct acpi_cpu_softc *) *
  226             (mp_maxid + 1), M_TEMP /* XXX */, M_WAITOK | M_ZERO);
  227 
  228     /* Get our Processor object. */
  229     buf.Pointer = NULL;
  230     buf.Length = ACPI_ALLOCATE_BUFFER;
  231     status = AcpiEvaluateObject(handle, NULL, NULL, &buf);
  232     if (ACPI_FAILURE(status)) {
  233         device_printf(dev, "probe failed to get Processor obj - %s\n",
  234                       AcpiFormatException(status));
  235         return (ENXIO);
  236     }
  237     obj = (ACPI_OBJECT *)buf.Pointer;
  238     if (obj->Type != ACPI_TYPE_PROCESSOR) {
  239         device_printf(dev, "Processor object has bad type %d\n", obj->Type);
  240         AcpiOsFree(obj);
  241         return (ENXIO);
  242     }
  243 
  244     /*
  245      * Find the processor associated with our unit.  We could use the
  246      * ProcId as a key, however, some boxes do not have the same values
  247      * in their Processor object as the ProcId values in the MADT.
  248      */
  249     acpi_id = obj->Processor.ProcId;
  250     AcpiOsFree(obj);
  251     if (acpi_pcpu_get_id(dev, &acpi_id, &cpu_id) != 0)
  252         return (ENXIO);
  253 
  254     /*
  255      * Check if we already probed this processor.  We scan the bus twice
  256      * so it's possible we've already seen this one.
  257      */
  258     if (cpu_softc[cpu_id] != NULL)
  259         return (ENXIO);
  260 
  261     /* Mark this processor as in-use and save our derived id for attach. */
  262     cpu_softc[cpu_id] = (void *)1;
  263     acpi_set_private(dev, (void*)(intptr_t)cpu_id);
  264     device_set_desc(dev, "ACPI CPU");
  265 
  266     return (0);
  267 }
  268 
  269 static int
  270 acpi_cpu_attach(device_t dev)
  271 {
  272     ACPI_BUFFER            buf;
  273     ACPI_OBJECT            arg[4], *obj;
  274     ACPI_OBJECT_LIST       arglist;
  275     struct pcpu            *pcpu_data;
  276     struct acpi_cpu_softc *sc;
  277     struct acpi_softc     *acpi_sc;
  278     ACPI_STATUS            status;
  279     u_int                  features;
  280     int                    cpu_id, drv_count, i;
  281     driver_t              **drivers;
  282     uint32_t               cap_set[3];
  283 
  284     /* UUID needed by _OSC evaluation */
  285     static uint8_t cpu_oscuuid[16] = { 0x16, 0xA6, 0x77, 0x40, 0x0C, 0x29,
  286                                        0xBE, 0x47, 0x9E, 0xBD, 0xD8, 0x70,
  287                                        0x58, 0x71, 0x39, 0x53 };
  288 
  289     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  290 
  291     sc = device_get_softc(dev);
  292     sc->cpu_dev = dev;
  293     sc->cpu_handle = acpi_get_handle(dev);
  294     cpu_id = (int)(intptr_t)acpi_get_private(dev);
  295     cpu_softc[cpu_id] = sc;
  296     pcpu_data = pcpu_find(cpu_id);
  297     pcpu_data->pc_device = dev;
  298     sc->cpu_pcpu = pcpu_data;
  299     cpu_smi_cmd = AcpiGbl_FADT.SmiCommand;
  300     cpu_cst_cnt = AcpiGbl_FADT.CstControl;
  301 
  302     buf.Pointer = NULL;
  303     buf.Length = ACPI_ALLOCATE_BUFFER;
  304     status = AcpiEvaluateObject(sc->cpu_handle, NULL, NULL, &buf);
  305     if (ACPI_FAILURE(status)) {
  306         device_printf(dev, "attach failed to get Processor obj - %s\n",
  307                       AcpiFormatException(status));
  308         return (ENXIO);
  309     }
  310     obj = (ACPI_OBJECT *)buf.Pointer;
  311     sc->cpu_p_blk = obj->Processor.PblkAddress;
  312     sc->cpu_p_blk_len = obj->Processor.PblkLength;
  313     sc->cpu_acpi_id = obj->Processor.ProcId;
  314     AcpiOsFree(obj);
  315     ACPI_DEBUG_PRINT((ACPI_DB_INFO, "acpi_cpu%d: P_BLK at %#x/%d\n",
  316                      device_get_unit(dev), sc->cpu_p_blk, sc->cpu_p_blk_len));
  317 
  318     /*
  319      * If this is the first cpu we attach, create and initialize the generic
  320      * resources that will be used by all acpi cpu devices.
  321      */
  322     if (device_get_unit(dev) == 0) {
  323         /* Assume we won't be using generic Cx mode by default */
  324         cpu_cx_generic = FALSE;
  325 
  326         /* Install hw.acpi.cpu sysctl tree */
  327         acpi_sc = acpi_device_get_parent_softc(dev);
  328         sysctl_ctx_init(&cpu_sysctl_ctx);
  329         cpu_sysctl_tree = SYSCTL_ADD_NODE(&cpu_sysctl_ctx,
  330             SYSCTL_CHILDREN(acpi_sc->acpi_sysctl_tree), OID_AUTO, "cpu",
  331             CTLFLAG_RD, 0, "node for CPU children");
  332 
  333         /* Queue post cpu-probing task handler */
  334         AcpiOsExecute(OSL_NOTIFY_HANDLER, acpi_cpu_startup, NULL);
  335     }
  336 
  337     /*
  338      * Before calling any CPU methods, collect child driver feature hints
  339      * and notify ACPI of them.  We support unified SMP power control
  340      * so advertise this ourselves.  Note this is not the same as independent
  341      * SMP control where each CPU can have different settings.
  342      */
  343     sc->cpu_features = ACPI_CAP_SMP_SAME | ACPI_CAP_SMP_SAME_C3;
  344     if (devclass_get_drivers(acpi_cpu_devclass, &drivers, &drv_count) == 0) {
  345         for (i = 0; i < drv_count; i++) {
  346             if (ACPI_GET_FEATURES(drivers[i], &features) == 0)
  347                 sc->cpu_features |= features;
  348         }
  349         free(drivers, M_TEMP);
  350     }
  351 
  352     /*
  353      * CPU capabilities are specified in
  354      * Intel Processor Vendor-Specific ACPI Interface Specification.
  355      */
  356     if (sc->cpu_features) {
  357         arglist.Pointer = arg;
  358         arglist.Count = 4;
  359         arg[0].Type = ACPI_TYPE_BUFFER;
  360         arg[0].Buffer.Length = sizeof(cpu_oscuuid);
  361         arg[0].Buffer.Pointer = cpu_oscuuid;    /* UUID */
  362         arg[1].Type = ACPI_TYPE_INTEGER;
  363         arg[1].Integer.Value = 1;               /* revision */
  364         arg[2].Type = ACPI_TYPE_INTEGER;
  365         arg[2].Integer.Value = 1;               /* count */
  366         arg[3].Type = ACPI_TYPE_BUFFER;
  367         arg[3].Buffer.Length = sizeof(cap_set); /* Capabilities buffer */
  368         arg[3].Buffer.Pointer = (uint8_t *)cap_set;
  369         cap_set[0] = 0;                         /* status */
  370         cap_set[1] = sc->cpu_features;
  371         status = AcpiEvaluateObject(sc->cpu_handle, "_OSC", &arglist, NULL);
  372         if (ACPI_SUCCESS(status)) {
  373             if (cap_set[0] != 0)
  374                 device_printf(dev, "_OSC returned status %#x\n", cap_set[0]);
  375         }
  376         else {
  377             arglist.Pointer = arg;
  378             arglist.Count = 1;
  379             arg[0].Type = ACPI_TYPE_BUFFER;
  380             arg[0].Buffer.Length = sizeof(cap_set);
  381             arg[0].Buffer.Pointer = (uint8_t *)cap_set;
  382             cap_set[0] = 1; /* revision */
  383             cap_set[1] = 1; /* number of capabilities integers */
  384             cap_set[2] = sc->cpu_features;
  385             AcpiEvaluateObject(sc->cpu_handle, "_PDC", &arglist, NULL);
  386         }
  387     }
  388 
  389     /* Probe for Cx state support. */
  390     acpi_cpu_cx_probe(sc);
  391 
  392     return (0);
  393 }
  394 
  395 static void
  396 acpi_cpu_postattach(void *unused __unused)
  397 {
  398     device_t *devices;
  399     int err;
  400     int i, n;
  401 
  402     err = devclass_get_devices(acpi_cpu_devclass, &devices, &n);
  403     if (err != 0) {
  404         printf("devclass_get_devices(acpi_cpu_devclass) failed\n");
  405         return;
  406     }
  407     for (i = 0; i < n; i++)
  408         bus_generic_probe(devices[i]);
  409     for (i = 0; i < n; i++)
  410         bus_generic_attach(devices[i]);
  411     free(devices, M_TEMP);
  412 }
  413 
  414 SYSINIT(acpi_cpu, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE,
  415     acpi_cpu_postattach, NULL);
  416 
  417 static void
  418 disable_idle(struct acpi_cpu_softc *sc)
  419 {
  420     cpumask_t cpuset;
  421 
  422     cpuset = sc->cpu_pcpu->pc_cpumask;
  423     sc->cpu_disable_idle = TRUE;
  424 
  425     /*
  426      * Ensure that the CPU is not in idle state or in acpi_cpu_idle().
  427      * Note that this code depends on the fact that the rendezvous IPI
  428      * can not penetrate context where interrupts are disabled and acpi_cpu_idle
  429      * is called and executed in such a context with interrupts being re-enabled
  430      * right before return.
  431      */
  432     smp_rendezvous_cpus(cpuset, smp_no_rendevous_barrier, NULL,
  433         smp_no_rendevous_barrier, NULL);
  434 }
  435 
  436 static void
  437 enable_idle(struct acpi_cpu_softc *sc)
  438 {
  439 
  440     sc->cpu_disable_idle = FALSE;
  441 }
  442 
  443 static int
  444 is_idle_disabled(struct acpi_cpu_softc *sc)
  445 {
  446 
  447     return (sc->cpu_disable_idle);
  448 }
  449 
  450 /*
  451  * Disable any entry to the idle function during suspend and re-enable it
  452  * during resume.
  453  */
  454 static int
  455 acpi_cpu_suspend(device_t dev)
  456 {
  457     int error;
  458 
  459     error = bus_generic_suspend(dev);
  460     if (error)
  461         return (error);
  462     disable_idle(device_get_softc(dev));
  463     return (0);
  464 }
  465 
  466 static int
  467 acpi_cpu_resume(device_t dev)
  468 {
  469 
  470     enable_idle(device_get_softc(dev));
  471     return (bus_generic_resume(dev));
  472 }
  473 
  474 /*
  475  * Find the processor associated with a given ACPI ID.  By default,
  476  * use the MADT to map ACPI IDs to APIC IDs and use that to locate a
  477  * processor.  Some systems have inconsistent ASL and MADT however.
  478  * For these systems the cpu_unordered tunable can be set in which
  479  * case we assume that Processor objects are listed in the same order
  480  * in both the MADT and ASL.
  481  */
  482 static int
  483 acpi_pcpu_get_id(device_t dev, uint32_t *acpi_id, uint32_t *cpu_id)
  484 {
  485     struct pcpu *pc;
  486     uint32_t     i, idx;
  487 
  488     KASSERT(acpi_id != NULL, ("Null acpi_id"));
  489     KASSERT(cpu_id != NULL, ("Null cpu_id"));
  490     idx = device_get_unit(dev);
  491 
  492     /*
  493      * If pc_acpi_id for CPU 0 is not initialized (e.g. a non-APIC
  494      * UP box) use the ACPI ID from the first processor we find.
  495      */
  496     if (idx == 0 && mp_ncpus == 1) {
  497         pc = pcpu_find(0);
  498         if (pc->pc_acpi_id == 0xffffffff)
  499             pc->pc_acpi_id = *acpi_id;
  500         *cpu_id = 0;
  501         return (0);
  502     }
  503 
  504     CPU_FOREACH(i) {
  505         pc = pcpu_find(i);
  506         KASSERT(pc != NULL, ("no pcpu data for %d", i));
  507         if (cpu_unordered) {
  508             if (idx-- == 0) {
  509                 /*
  510                  * If pc_acpi_id doesn't match the ACPI ID from the
  511                  * ASL, prefer the MADT-derived value.
  512                  */
  513                 if (pc->pc_acpi_id != *acpi_id)
  514                     *acpi_id = pc->pc_acpi_id;
  515                 *cpu_id = pc->pc_cpuid;
  516                 return (0);
  517             }
  518         } else {
  519             if (pc->pc_acpi_id == *acpi_id) {
  520                 if (bootverbose)
  521                     device_printf(dev,
  522                         "Processor %s (ACPI ID %u) -> APIC ID %d\n",
  523                         acpi_name(acpi_get_handle(dev)), *acpi_id,
  524                         pc->pc_cpuid);
  525                 *cpu_id = pc->pc_cpuid;
  526                 return (0);
  527             }
  528         }
  529     }
  530 
  531     if (bootverbose)
  532         printf("ACPI: Processor %s (ACPI ID %u) ignored\n",
  533             acpi_name(acpi_get_handle(dev)), *acpi_id);
  534 
  535     return (ESRCH);
  536 }
  537 
  538 static struct resource_list *
  539 acpi_cpu_get_rlist(device_t dev, device_t child)
  540 {
  541     struct acpi_cpu_device *ad;
  542 
  543     ad = device_get_ivars(child);
  544     if (ad == NULL)
  545         return (NULL);
  546     return (&ad->ad_rl);
  547 }
  548 
  549 static device_t
  550 acpi_cpu_add_child(device_t dev, u_int order, const char *name, int unit)
  551 {
  552     struct acpi_cpu_device *ad;
  553     device_t child;
  554 
  555     if ((ad = malloc(sizeof(*ad), M_TEMP, M_NOWAIT | M_ZERO)) == NULL)
  556         return (NULL);
  557 
  558     resource_list_init(&ad->ad_rl);
  559     
  560     child = device_add_child_ordered(dev, order, name, unit);
  561     if (child != NULL)
  562         device_set_ivars(child, ad);
  563     else
  564         free(ad, M_TEMP);
  565     return (child);
  566 }
  567 
  568 static int
  569 acpi_cpu_read_ivar(device_t dev, device_t child, int index, uintptr_t *result)
  570 {
  571     struct acpi_cpu_softc *sc;
  572 
  573     sc = device_get_softc(dev);
  574     switch (index) {
  575     case ACPI_IVAR_HANDLE:
  576         *result = (uintptr_t)sc->cpu_handle;
  577         break;
  578     case CPU_IVAR_PCPU:
  579         *result = (uintptr_t)sc->cpu_pcpu;
  580         break;
  581     default:
  582         return (ENOENT);
  583     }
  584     return (0);
  585 }
  586 
  587 static int
  588 acpi_cpu_shutdown(device_t dev)
  589 {
  590     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  591 
  592     /* Allow children to shutdown first. */
  593     bus_generic_shutdown(dev);
  594 
  595     /*
  596      * Disable any entry to the idle function.
  597      */
  598     disable_idle(device_get_softc(dev));
  599 
  600     /*
  601      * CPU devices are not truely detached and remain referenced,
  602      * so their resources are not freed.
  603      */
  604 
  605     return_VALUE (0);
  606 }
  607 
  608 static void
  609 acpi_cpu_cx_probe(struct acpi_cpu_softc *sc)
  610 {
  611     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  612 
  613     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
  614     sc->cpu_prev_sleep = 1000000;
  615     sc->cpu_cx_lowest = 0;
  616     sc->cpu_cx_lowest_lim = 0;
  617 
  618     /*
  619      * Check for the ACPI 2.0 _CST sleep states object. If we can't find
  620      * any, we'll revert to generic FADT/P_BLK Cx control method which will
  621      * be handled by acpi_cpu_startup. We need to defer to after having
  622      * probed all the cpus in the system before probing for generic Cx
  623      * states as we may already have found cpus with valid _CST packages
  624      */
  625     if (!cpu_cx_generic && acpi_cpu_cx_cst(sc) != 0) {
  626         /*
  627          * We were unable to find a _CST package for this cpu or there
  628          * was an error parsing it. Switch back to generic mode.
  629          */
  630         cpu_cx_generic = TRUE;
  631         if (bootverbose)
  632             device_printf(sc->cpu_dev, "switching to generic Cx mode\n");
  633     }
  634 
  635     /*
  636      * TODO: _CSD Package should be checked here.
  637      */
  638 }
  639 
  640 static void
  641 acpi_cpu_generic_cx_probe(struct acpi_cpu_softc *sc)
  642 {
  643     ACPI_GENERIC_ADDRESS         gas;
  644     struct acpi_cx              *cx_ptr;
  645 
  646     sc->cpu_cx_count = 0;
  647     cx_ptr = sc->cpu_cx_states;
  648 
  649     /* Use initial sleep value of 1 sec. to start with lowest idle state. */
  650     sc->cpu_prev_sleep = 1000000;
  651 
  652     /* C1 has been required since just after ACPI 1.0 */
  653     cx_ptr->type = ACPI_STATE_C1;
  654     cx_ptr->trans_lat = 0;
  655     cx_ptr++;
  656     sc->cpu_non_c3 = sc->cpu_cx_count;
  657     sc->cpu_cx_count++;
  658 
  659     /* 
  660      * The spec says P_BLK must be 6 bytes long.  However, some systems
  661      * use it to indicate a fractional set of features present so we
  662      * take 5 as C2.  Some may also have a value of 7 to indicate
  663      * another C3 but most use _CST for this (as required) and having
  664      * "only" C1-C3 is not a hardship.
  665      */
  666     if (sc->cpu_p_blk_len < 5)
  667         return; 
  668 
  669     /* Validate and allocate resources for C2 (P_LVL2). */
  670     gas.SpaceId = ACPI_ADR_SPACE_SYSTEM_IO;
  671     gas.BitWidth = 8;
  672     if (AcpiGbl_FADT.C2Latency <= 100) {
  673         gas.Address = sc->cpu_p_blk + 4;
  674         cx_ptr->res_rid = 0;
  675         acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &cx_ptr->res_rid,
  676             &gas, &cx_ptr->p_lvlx, RF_SHAREABLE);
  677         if (cx_ptr->p_lvlx != NULL) {
  678             cx_ptr->type = ACPI_STATE_C2;
  679             cx_ptr->trans_lat = AcpiGbl_FADT.C2Latency;
  680             cx_ptr++;
  681             sc->cpu_non_c3 = sc->cpu_cx_count;
  682             sc->cpu_cx_count++;
  683         }
  684     }
  685     if (sc->cpu_p_blk_len < 6)
  686         return;
  687 
  688     /* Validate and allocate resources for C3 (P_LVL3). */
  689     if (AcpiGbl_FADT.C3Latency <= 1000 && !(cpu_quirks & CPU_QUIRK_NO_C3)) {
  690         gas.Address = sc->cpu_p_blk + 5;
  691         cx_ptr->res_rid = 1;
  692         acpi_bus_alloc_gas(sc->cpu_dev, &cx_ptr->res_type, &cx_ptr->res_rid,
  693             &gas, &cx_ptr->p_lvlx, RF_SHAREABLE);
  694         if (cx_ptr->p_lvlx != NULL) {
  695             cx_ptr->type = ACPI_STATE_C3;
  696             cx_ptr->trans_lat = AcpiGbl_FADT.C3Latency;
  697             cx_ptr++;
  698             sc->cpu_cx_count++;
  699         }
  700     }
  701 }
  702 
  703 /*
  704  * Parse a _CST package and set up its Cx states.  Since the _CST object
  705  * can change dynamically, our notify handler may call this function
  706  * to clean up and probe the new _CST package.
  707  */
  708 static int
  709 acpi_cpu_cx_cst(struct acpi_cpu_softc *sc)
  710 {
  711     struct       acpi_cx *cx_ptr;
  712     ACPI_STATUS  status;
  713     ACPI_BUFFER  buf;
  714     ACPI_OBJECT *top;
  715     ACPI_OBJECT *pkg;
  716     uint32_t     count;
  717     int          i;
  718 
  719     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  720 
  721     buf.Pointer = NULL;
  722     buf.Length = ACPI_ALLOCATE_BUFFER;
  723     status = AcpiEvaluateObject(sc->cpu_handle, "_CST", NULL, &buf);
  724     if (ACPI_FAILURE(status))
  725         return (ENXIO);
  726 
  727     /* _CST is a package with a count and at least one Cx package. */
  728     top = (ACPI_OBJECT *)buf.Pointer;
  729     if (!ACPI_PKG_VALID(top, 2) || acpi_PkgInt32(top, 0, &count) != 0) {
  730         device_printf(sc->cpu_dev, "invalid _CST package\n");
  731         AcpiOsFree(buf.Pointer);
  732         return (ENXIO);
  733     }
  734     if (count != top->Package.Count - 1) {
  735         device_printf(sc->cpu_dev, "invalid _CST state count (%d != %d)\n",
  736                count, top->Package.Count - 1);
  737         count = top->Package.Count - 1;
  738     }
  739     if (count > MAX_CX_STATES) {
  740         device_printf(sc->cpu_dev, "_CST has too many states (%d)\n", count);
  741         count = MAX_CX_STATES;
  742     }
  743 
  744     sc->cpu_non_c3 = 0;
  745     sc->cpu_cx_count = 0;
  746     cx_ptr = sc->cpu_cx_states;
  747 
  748     /*
  749      * C1 has been required since just after ACPI 1.0.
  750      * Reserve the first slot for it.
  751      */
  752     cx_ptr->type = ACPI_STATE_C0;
  753     cx_ptr++;
  754     sc->cpu_cx_count++;
  755 
  756     /* Set up all valid states. */
  757     for (i = 0; i < count; i++) {
  758         pkg = &top->Package.Elements[i + 1];
  759         if (!ACPI_PKG_VALID(pkg, 4) ||
  760             acpi_PkgInt32(pkg, 1, &cx_ptr->type) != 0 ||
  761             acpi_PkgInt32(pkg, 2, &cx_ptr->trans_lat) != 0 ||
  762             acpi_PkgInt32(pkg, 3, &cx_ptr->power) != 0) {
  763 
  764             device_printf(sc->cpu_dev, "skipping invalid Cx state package\n");
  765             continue;
  766         }
  767 
  768         /* Validate the state to see if we should use it. */
  769         switch (cx_ptr->type) {
  770         case ACPI_STATE_C1:
  771             if (sc->cpu_cx_states[0].type == ACPI_STATE_C0) {
  772                 /* This is the first C1 state.  Use the reserved slot. */
  773                 sc->cpu_cx_states[0] = *cx_ptr;
  774             } else {
  775                 sc->cpu_non_c3 = sc->cpu_cx_count;
  776                 cx_ptr++;
  777                 sc->cpu_cx_count++;
  778             }
  779             continue;
  780         case ACPI_STATE_C2:
  781             sc->cpu_non_c3 = sc->cpu_cx_count;
  782             break;
  783         case ACPI_STATE_C3:
  784         default:
  785             if ((cpu_quirks & CPU_QUIRK_NO_C3) != 0) {
  786                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  787                                  "acpi_cpu%d: C3[%d] not available.\n",
  788                                  device_get_unit(sc->cpu_dev), i));
  789                 continue;
  790             }
  791             break;
  792         }
  793 
  794         /* Free up any previous register. */
  795         if (cx_ptr->p_lvlx != NULL) {
  796             bus_release_resource(sc->cpu_dev, cx_ptr->res_type, cx_ptr->res_rid,
  797                 cx_ptr->p_lvlx);
  798             cx_ptr->p_lvlx = NULL;
  799         }
  800 
  801         /* Allocate the control register for C2 or C3. */
  802         cx_ptr->res_rid = sc->cpu_cx_count;
  803         acpi_PkgGas(sc->cpu_dev, pkg, 0, &cx_ptr->res_type, &cx_ptr->res_rid,
  804             &cx_ptr->p_lvlx, RF_SHAREABLE);
  805         if (cx_ptr->p_lvlx) {
  806             ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  807                              "acpi_cpu%d: Got C%d - %d latency\n",
  808                              device_get_unit(sc->cpu_dev), cx_ptr->type,
  809                              cx_ptr->trans_lat));
  810             cx_ptr++;
  811             sc->cpu_cx_count++;
  812         }
  813     }
  814     AcpiOsFree(buf.Pointer);
  815 
  816     /* If C1 state was not found, we need one now. */
  817     cx_ptr = sc->cpu_cx_states;
  818     if (cx_ptr->type == ACPI_STATE_C0) {
  819         cx_ptr->type = ACPI_STATE_C1;
  820         cx_ptr->trans_lat = 0;
  821     }
  822 
  823     return (0);
  824 }
  825 
  826 /*
  827  * Call this *after* all CPUs have been attached.
  828  */
  829 static void
  830 acpi_cpu_startup(void *arg)
  831 {
  832     struct acpi_cpu_softc *sc;
  833     int i;
  834 
  835     /* Get set of CPU devices */
  836     devclass_get_devices(acpi_cpu_devclass, &cpu_devices, &cpu_ndevices);
  837 
  838     /*
  839      * Setup any quirks that might necessary now that we have probed
  840      * all the CPUs
  841      */
  842     acpi_cpu_quirks();
  843 
  844     if (cpu_cx_generic) {
  845         /*
  846          * We are using generic Cx mode, probe for available Cx states
  847          * for all processors.
  848          */
  849         for (i = 0; i < cpu_ndevices; i++) {
  850             sc = device_get_softc(cpu_devices[i]);
  851             acpi_cpu_generic_cx_probe(sc);
  852         }
  853     } else {
  854         /*
  855          * We are using _CST mode, remove C3 state if necessary.
  856          * As we now know for sure that we will be using _CST mode
  857          * install our notify handler.
  858          */
  859         for (i = 0; i < cpu_ndevices; i++) {
  860             sc = device_get_softc(cpu_devices[i]);
  861             if (cpu_quirks & CPU_QUIRK_NO_C3) {
  862                 sc->cpu_cx_count = sc->cpu_non_c3 + 1;
  863             }
  864             AcpiInstallNotifyHandler(sc->cpu_handle, ACPI_DEVICE_NOTIFY,
  865                 acpi_cpu_notify, sc);
  866         }
  867     }
  868 
  869     /* Perform Cx final initialization. */
  870     for (i = 0; i < cpu_ndevices; i++) {
  871         sc = device_get_softc(cpu_devices[i]);
  872         acpi_cpu_startup_cx(sc);
  873     }
  874 
  875     /* Add a sysctl handler to handle global Cx lowest setting */
  876     SYSCTL_ADD_PROC(&cpu_sysctl_ctx, SYSCTL_CHILDREN(cpu_sysctl_tree),
  877         OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
  878         NULL, 0, acpi_cpu_global_cx_lowest_sysctl, "A",
  879         "Global lowest Cx sleep state to use");
  880 
  881     /* Take over idling from cpu_idle_default(). */
  882     cpu_cx_lowest_lim = 0;
  883     for (i = 0; i < cpu_ndevices; i++) {
  884         sc = device_get_softc(cpu_devices[i]);
  885         enable_idle(sc);
  886     }
  887     cpu_idle_hook = acpi_cpu_idle;
  888 }
  889 
  890 static void
  891 acpi_cpu_cx_list(struct acpi_cpu_softc *sc)
  892 {
  893     struct sbuf sb;
  894     int i;
  895 
  896     /*
  897      * Set up the list of Cx states
  898      */
  899     sc->cpu_non_c3 = 0;
  900     sbuf_new(&sb, sc->cpu_cx_supported, sizeof(sc->cpu_cx_supported),
  901         SBUF_FIXEDLEN);
  902     for (i = 0; i < sc->cpu_cx_count; i++) {
  903         sbuf_printf(&sb, "C%d/%d/%d ", i + 1, sc->cpu_cx_states[i].type,
  904             sc->cpu_cx_states[i].trans_lat);
  905         if (sc->cpu_cx_states[i].type < ACPI_STATE_C3)
  906             sc->cpu_non_c3 = i;
  907     }
  908     sbuf_trim(&sb);
  909     sbuf_finish(&sb);
  910 }       
  911 
  912 static void
  913 acpi_cpu_startup_cx(struct acpi_cpu_softc *sc)
  914 {
  915     acpi_cpu_cx_list(sc);
  916     
  917     SYSCTL_ADD_STRING(&sc->cpu_sysctl_ctx,
  918                       SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
  919                       OID_AUTO, "cx_supported", CTLFLAG_RD,
  920                       sc->cpu_cx_supported, 0,
  921                       "Cx/microsecond values for supported Cx states");
  922     SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
  923                     SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
  924                     OID_AUTO, "cx_lowest", CTLTYPE_STRING | CTLFLAG_RW,
  925                     (void *)sc, 0, acpi_cpu_cx_lowest_sysctl, "A",
  926                     "lowest Cx sleep state to use");
  927     SYSCTL_ADD_PROC(&sc->cpu_sysctl_ctx,
  928                     SYSCTL_CHILDREN(device_get_sysctl_tree(sc->cpu_dev)),
  929                     OID_AUTO, "cx_usage", CTLTYPE_STRING | CTLFLAG_RD,
  930                     (void *)sc, 0, acpi_cpu_usage_sysctl, "A",
  931                     "percent usage for each Cx state");
  932 
  933     /* Signal platform that we can handle _CST notification. */
  934     if (!cpu_cx_generic && cpu_cst_cnt != 0) {
  935         ACPI_LOCK(acpi);
  936         AcpiOsWritePort(cpu_smi_cmd, cpu_cst_cnt, 8);
  937         ACPI_UNLOCK(acpi);
  938     }
  939 }
  940 
  941 /*
  942  * Idle the CPU in the lowest state possible.  This function is called with
  943  * interrupts disabled.  Note that once it re-enables interrupts, a task
  944  * switch can occur so do not access shared data (i.e. the softc) after
  945  * interrupts are re-enabled.
  946  */
  947 static void
  948 acpi_cpu_idle()
  949 {
  950     struct      acpi_cpu_softc *sc;
  951     struct      acpi_cx *cx_next;
  952     uint64_t    cputicks;
  953     uint32_t    start_time, end_time;
  954     int         bm_active, cx_next_idx, i;
  955 
  956     /*
  957      * Look up our CPU id to get our softc.  If it's NULL, we'll use C1
  958      * since there is no ACPI processor object for this CPU.  This occurs
  959      * for logical CPUs in the HTT case.
  960      */
  961     sc = cpu_softc[PCPU_GET(cpuid)];
  962     if (sc == NULL) {
  963         acpi_cpu_c1();
  964         return;
  965     }
  966 
  967     /* If disabled, take the safe path. */
  968     if (is_idle_disabled(sc)) {
  969         acpi_cpu_c1();
  970         return;
  971     }
  972 
  973     /* Find the lowest state that has small enough latency. */
  974     cx_next_idx = 0;
  975     for (i = sc->cpu_cx_lowest; i >= 0; i--) {
  976         if (sc->cpu_cx_states[i].trans_lat * 3 <= sc->cpu_prev_sleep) {
  977             cx_next_idx = i;
  978             break;
  979         }
  980     }
  981 
  982     /*
  983      * Check for bus master activity.  If there was activity, clear
  984      * the bit and use the lowest non-C3 state.  Note that the USB
  985      * driver polling for new devices keeps this bit set all the
  986      * time if USB is loaded.
  987      */
  988     if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0 &&
  989         cx_next_idx > sc->cpu_non_c3) {
  990         AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, &bm_active);
  991         if (bm_active != 0) {
  992             AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  993             cx_next_idx = sc->cpu_non_c3;
  994         }
  995     }
  996 
  997     /* Select the next state and update statistics. */
  998     cx_next = &sc->cpu_cx_states[cx_next_idx];
  999     sc->cpu_cx_stats[cx_next_idx]++;
 1000     KASSERT(cx_next->type != ACPI_STATE_C0, ("acpi_cpu_idle: C0 sleep"));
 1001 
 1002     /*
 1003      * Execute HLT (or equivalent) and wait for an interrupt.  We can't
 1004      * precisely calculate the time spent in C1 since the place we wake up
 1005      * is an ISR.  Assume we slept no more then half of quantum.
 1006      */
 1007     if (cx_next->type == ACPI_STATE_C1) {
 1008         cputicks = cpu_ticks();
 1009         acpi_cpu_c1();
 1010         end_time = ((cpu_ticks() - cputicks) << 20) / cpu_tickrate();
 1011         if (curthread->td_critnest == 0)
 1012                 end_time = min(end_time, 500000 / hz);
 1013         sc->cpu_prev_sleep = (sc->cpu_prev_sleep * 3 + end_time) / 4;
 1014         return;
 1015     }
 1016 
 1017     /*
 1018      * For C3, disable bus master arbitration and enable bus master wake
 1019      * if BM control is available, otherwise flush the CPU cache.
 1020      */
 1021     if (cx_next->type == ACPI_STATE_C3) {
 1022         if ((cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
 1023             AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 1);
 1024             AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 1);
 1025         } else
 1026             ACPI_FLUSH_CPU_CACHE();
 1027     }
 1028 
 1029     /*
 1030      * Read from P_LVLx to enter C2(+), checking time spent asleep.
 1031      * Use the ACPI timer for measuring sleep time.  Since we need to
 1032      * get the time very close to the CPU start/stop clock logic, this
 1033      * is the only reliable time source.
 1034      */
 1035     if (cx_next->type == ACPI_STATE_C3) {
 1036         AcpiHwRead(&start_time, &AcpiGbl_FADT.XPmTimerBlock);
 1037         cputicks = 0;
 1038     } else {
 1039         start_time = 0;
 1040         cputicks = cpu_ticks();
 1041     }
 1042     CPU_GET_REG(cx_next->p_lvlx, 1);
 1043 
 1044     /*
 1045      * Read the end time twice.  Since it may take an arbitrary time
 1046      * to enter the idle state, the first read may be executed before
 1047      * the processor has stopped.  Doing it again provides enough
 1048      * margin that we are certain to have a correct value.
 1049      */
 1050     AcpiHwRead(&end_time, &AcpiGbl_FADT.XPmTimerBlock);
 1051     if (cx_next->type == ACPI_STATE_C3) {
 1052         AcpiHwRead(&end_time, &AcpiGbl_FADT.XPmTimerBlock);
 1053         end_time = acpi_TimerDelta(end_time, start_time);
 1054     } else
 1055         end_time = ((cpu_ticks() - cputicks) << 20) / cpu_tickrate();
 1056 
 1057     /* Enable bus master arbitration and disable bus master wakeup. */
 1058     if (cx_next->type == ACPI_STATE_C3 &&
 1059         (cpu_quirks & CPU_QUIRK_NO_BM_CTRL) == 0) {
 1060         AcpiWriteBitRegister(ACPI_BITREG_ARB_DISABLE, 0);
 1061         AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
 1062     }
 1063     ACPI_ENABLE_IRQS();
 1064 
 1065     sc->cpu_prev_sleep = (sc->cpu_prev_sleep * 3 + PM_USEC(end_time)) / 4;
 1066 }
 1067 
 1068 /*
 1069  * Re-evaluate the _CST object when we are notified that it changed.
 1070  */
 1071 static void
 1072 acpi_cpu_notify(ACPI_HANDLE h, UINT32 notify, void *context)
 1073 {
 1074     struct acpi_cpu_softc *sc = (struct acpi_cpu_softc *)context;
 1075 
 1076     if (notify != ACPI_NOTIFY_CX_STATES)
 1077         return;
 1078 
 1079     /*
 1080      * C-state data for target CPU is going to be in flux while we execute
 1081      * acpi_cpu_cx_cst, so disable entering acpi_cpu_idle.
 1082      * Also, it may happen that multiple ACPI taskqueues may concurrently
 1083      * execute notifications for the same CPU.  ACPI_SERIAL is used to
 1084      * protect against that.
 1085      */
 1086     ACPI_SERIAL_BEGIN(cpu);
 1087     disable_idle(sc);
 1088 
 1089     /* Update the list of Cx states. */
 1090     acpi_cpu_cx_cst(sc);
 1091     acpi_cpu_cx_list(sc);
 1092     acpi_cpu_set_cx_lowest(sc);
 1093 
 1094     enable_idle(sc);
 1095     ACPI_SERIAL_END(cpu);
 1096 
 1097     acpi_UserNotify("PROCESSOR", sc->cpu_handle, notify);
 1098 }
 1099 
 1100 static int
 1101 acpi_cpu_quirks(void)
 1102 {
 1103     device_t acpi_dev;
 1104     uint32_t val;
 1105 
 1106     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
 1107 
 1108     /*
 1109      * Bus mastering arbitration control is needed to keep caches coherent
 1110      * while sleeping in C3.  If it's not present but a working flush cache
 1111      * instruction is present, flush the caches before entering C3 instead.
 1112      * Otherwise, just disable C3 completely.
 1113      */
 1114     if (AcpiGbl_FADT.Pm2ControlBlock == 0 ||
 1115         AcpiGbl_FADT.Pm2ControlLength == 0) {
 1116         if ((AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD) &&
 1117             (AcpiGbl_FADT.Flags & ACPI_FADT_WBINVD_FLUSH) == 0) {
 1118             cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
 1119             ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 1120                 "acpi_cpu: no BM control, using flush cache method\n"));
 1121         } else {
 1122             cpu_quirks |= CPU_QUIRK_NO_C3;
 1123             ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 1124                 "acpi_cpu: no BM control, C3 not available\n"));
 1125         }
 1126     }
 1127 
 1128     /*
 1129      * If we are using generic Cx mode, C3 on multiple CPUs requires using
 1130      * the expensive flush cache instruction.
 1131      */
 1132     if (cpu_cx_generic && mp_ncpus > 1) {
 1133         cpu_quirks |= CPU_QUIRK_NO_BM_CTRL;
 1134         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 1135             "acpi_cpu: SMP, using flush cache mode for C3\n"));
 1136     }
 1137 
 1138     /* Look for various quirks of the PIIX4 part. */
 1139     acpi_dev = pci_find_device(PCI_VENDOR_INTEL, PCI_DEVICE_82371AB_3);
 1140     if (acpi_dev != NULL) {
 1141         switch (pci_get_revid(acpi_dev)) {
 1142         /*
 1143          * Disable C3 support for all PIIX4 chipsets.  Some of these parts
 1144          * do not report the BMIDE status to the BM status register and
 1145          * others have a livelock bug if Type-F DMA is enabled.  Linux
 1146          * works around the BMIDE bug by reading the BM status directly
 1147          * but we take the simpler approach of disabling C3 for these
 1148          * parts.
 1149          *
 1150          * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
 1151          * Livelock") from the January 2002 PIIX4 specification update.
 1152          * Applies to all PIIX4 models.
 1153          *
 1154          * Also, make sure that all interrupts cause a "Stop Break"
 1155          * event to exit from C2 state.
 1156          * Also, BRLD_EN_BM (ACPI_BITREG_BUS_MASTER_RLD in ACPI-speak)
 1157          * should be set to zero, otherwise it causes C2 to short-sleep.
 1158          * PIIX4 doesn't properly support C3 and bus master activity
 1159          * need not break out of C2.
 1160          */
 1161         case PCI_REVISION_A_STEP:
 1162         case PCI_REVISION_B_STEP:
 1163         case PCI_REVISION_4E:
 1164         case PCI_REVISION_4M:
 1165             cpu_quirks |= CPU_QUIRK_NO_C3;
 1166             ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 1167                 "acpi_cpu: working around PIIX4 bug, disabling C3\n"));
 1168 
 1169             val = pci_read_config(acpi_dev, PIIX4_DEVACTB_REG, 4);
 1170             if ((val & PIIX4_STOP_BREAK_MASK) != PIIX4_STOP_BREAK_MASK) {
 1171                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 1172                     "acpi_cpu: PIIX4: enabling IRQs to generate Stop Break\n"));
 1173                 val |= PIIX4_STOP_BREAK_MASK;
 1174                 pci_write_config(acpi_dev, PIIX4_DEVACTB_REG, val, 4);
 1175             }
 1176             AcpiReadBitRegister(ACPI_BITREG_BUS_MASTER_RLD, &val);
 1177             if (val) {
 1178                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 1179                     "acpi_cpu: PIIX4: reset BRLD_EN_BM\n"));
 1180                 AcpiWriteBitRegister(ACPI_BITREG_BUS_MASTER_RLD, 0);
 1181             }
 1182             break;
 1183         default:
 1184             break;
 1185         }
 1186     }
 1187 
 1188     return (0);
 1189 }
 1190 
 1191 static int
 1192 acpi_cpu_usage_sysctl(SYSCTL_HANDLER_ARGS)
 1193 {
 1194     struct acpi_cpu_softc *sc;
 1195     struct sbuf  sb;
 1196     char         buf[128];
 1197     int          i;
 1198     uintmax_t    fract, sum, whole;
 1199 
 1200     sc = (struct acpi_cpu_softc *) arg1;
 1201     sum = 0;
 1202     for (i = 0; i < sc->cpu_cx_count; i++)
 1203         sum += sc->cpu_cx_stats[i];
 1204     sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
 1205     for (i = 0; i < sc->cpu_cx_count; i++) {
 1206         if (sum > 0) {
 1207             whole = (uintmax_t)sc->cpu_cx_stats[i] * 100;
 1208             fract = (whole % sum) * 100;
 1209             sbuf_printf(&sb, "%u.%02u%% ", (u_int)(whole / sum),
 1210                 (u_int)(fract / sum));
 1211         } else
 1212             sbuf_printf(&sb, "0.00%% ");
 1213     }
 1214     sbuf_printf(&sb, "last %dus", sc->cpu_prev_sleep);
 1215     sbuf_trim(&sb);
 1216     sbuf_finish(&sb);
 1217     sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req);
 1218     sbuf_delete(&sb);
 1219 
 1220     return (0);
 1221 }
 1222 
 1223 static int
 1224 acpi_cpu_set_cx_lowest(struct acpi_cpu_softc *sc)
 1225 {
 1226     int i;
 1227 
 1228     ACPI_SERIAL_ASSERT(cpu);
 1229     sc->cpu_cx_lowest = min(sc->cpu_cx_lowest_lim, sc->cpu_cx_count - 1);
 1230 
 1231     /* If not disabling, cache the new lowest non-C3 state. */
 1232     sc->cpu_non_c3 = 0;
 1233     for (i = sc->cpu_cx_lowest; i >= 0; i--) {
 1234         if (sc->cpu_cx_states[i].type < ACPI_STATE_C3) {
 1235             sc->cpu_non_c3 = i;
 1236             break;
 1237         }
 1238     }
 1239 
 1240     /* Reset the statistics counters. */
 1241     bzero(sc->cpu_cx_stats, sizeof(sc->cpu_cx_stats));
 1242     return (0);
 1243 }
 1244 
 1245 static int
 1246 acpi_cpu_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
 1247 {
 1248     struct       acpi_cpu_softc *sc;
 1249     char         state[8];
 1250     int          val, error;
 1251 
 1252     sc = (struct acpi_cpu_softc *) arg1;
 1253     snprintf(state, sizeof(state), "C%d", sc->cpu_cx_lowest_lim + 1);
 1254     error = sysctl_handle_string(oidp, state, sizeof(state), req);
 1255     if (error != 0 || req->newptr == NULL)
 1256         return (error);
 1257     if (strlen(state) < 2 || toupper(state[0]) != 'C')
 1258         return (EINVAL);
 1259     if (strcasecmp(state, "Cmax") == 0)
 1260         val = MAX_CX_STATES;
 1261     else {
 1262         val = (int) strtol(state + 1, NULL, 10);
 1263         if (val < 1 || val > MAX_CX_STATES)
 1264             return (EINVAL);
 1265     }
 1266 
 1267     ACPI_SERIAL_BEGIN(cpu);
 1268     sc->cpu_cx_lowest_lim = val - 1;
 1269     acpi_cpu_set_cx_lowest(sc);
 1270     ACPI_SERIAL_END(cpu);
 1271 
 1272     return (0);
 1273 }
 1274 
 1275 static int
 1276 acpi_cpu_global_cx_lowest_sysctl(SYSCTL_HANDLER_ARGS)
 1277 {
 1278     struct      acpi_cpu_softc *sc;
 1279     char        state[8];
 1280     int         val, error, i;
 1281 
 1282     snprintf(state, sizeof(state), "C%d", cpu_cx_lowest_lim + 1);
 1283     error = sysctl_handle_string(oidp, state, sizeof(state), req);
 1284     if (error != 0 || req->newptr == NULL)
 1285         return (error);
 1286     if (strlen(state) < 2 || toupper(state[0]) != 'C')
 1287         return (EINVAL);
 1288     if (strcasecmp(state, "Cmax") == 0)
 1289         val = MAX_CX_STATES;
 1290     else {
 1291         val = (int) strtol(state + 1, NULL, 10);
 1292         if (val < 1 || val > MAX_CX_STATES)
 1293             return (EINVAL);
 1294     }
 1295 
 1296     /* Update the new lowest useable Cx state for all CPUs. */
 1297     ACPI_SERIAL_BEGIN(cpu);
 1298     cpu_cx_lowest_lim = val - 1;
 1299     for (i = 0; i < cpu_ndevices; i++) {
 1300         sc = device_get_softc(cpu_devices[i]);
 1301         sc->cpu_cx_lowest_lim = cpu_cx_lowest_lim;
 1302         acpi_cpu_set_cx_lowest(sc);
 1303     }
 1304     ACPI_SERIAL_END(cpu);
 1305 
 1306     return (0);
 1307 }

Cache object: 7d835072ed3e106b6ebfdf4814bcf1bd


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