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_powerres.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 Michael Smith
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  *
   14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   24  * SUCH DAMAGE.
   25  *
   26  * $FreeBSD: src/sys/dev/acpica/acpi_powerres.c,v 1.30.8.1 2009/04/15 03:14:26 kensmith Exp $
   27  */
   28 
   29 #include "opt_acpi.h"
   30 #include <sys/param.h>
   31 #include <sys/kernel.h>
   32 #include <sys/malloc.h>
   33 #include <sys/bus.h>
   34 
   35 #include "acpi.h"
   36 #include <dev/acpica/acpivar.h>
   37 
   38 /*
   39  * ACPI power resource management.
   40  *
   41  * Power resource behaviour is slightly complicated by the fact that
   42  * a single power resource may provide power for more than one device.
   43  * Thus, we must track the device(s) being powered by a given power
   44  * resource, and only deactivate it when there are no powered devices.
   45  *
   46  * Note that this only manages resources for known devices.  There is an
   47  * ugly case where we may turn of power to a device which is in use because
   48  * we don't know that it depends on a given resource.  We should perhaps
   49  * try to be smarter about this, but a more complete solution would involve
   50  * scanning all of the ACPI namespace to find devices we're not currently
   51  * aware of, and this raises questions about whether they should be left 
   52  * on, turned off, etc.
   53  */
   54 
   55 MALLOC_DEFINE(M_ACPIPWR, "acpipwr", "ACPI power resources");
   56 
   57 /* Hooks for the ACPI CA debugging infrastructure */
   58 #define _COMPONENT      ACPI_POWERRES
   59 ACPI_MODULE_NAME("POWERRES")
   60 
   61 /* Return values from _STA on a power resource */
   62 #define ACPI_PWR_OFF    0
   63 #define ACPI_PWR_ON     1
   64 #define ACPI_PWR_UNK    (-1)
   65 
   66 /* A relationship between a power resource and a consumer. */
   67 struct acpi_powerreference {
   68     struct acpi_powerconsumer           *ar_consumer;
   69     struct acpi_powerresource           *ar_resource;
   70     TAILQ_ENTRY(acpi_powerreference)    ar_rlink; /* link on resource list */
   71     TAILQ_ENTRY(acpi_powerreference)    ar_clink; /* link on consumer */
   72 };
   73     
   74 /* A power-managed device. */
   75 struct acpi_powerconsumer {
   76     /* Device which is powered */
   77     ACPI_HANDLE                         ac_consumer;
   78     int                                 ac_state;
   79     TAILQ_ENTRY(acpi_powerconsumer)     ac_link;
   80     TAILQ_HEAD(,acpi_powerreference)    ac_references;
   81 };
   82 
   83 /* A power resource. */
   84 struct acpi_powerresource {
   85     TAILQ_ENTRY(acpi_powerresource)     ap_link;
   86     TAILQ_HEAD(,acpi_powerreference)    ap_references;
   87     ACPI_HANDLE                         ap_resource;
   88     ACPI_INTEGER                        ap_systemlevel;
   89     ACPI_INTEGER                        ap_order;
   90     int                                 ap_state;
   91 };
   92 
   93 static TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource)
   94         acpi_powerresources;
   95 static TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer)
   96         acpi_powerconsumers;
   97 ACPI_SERIAL_DECL(powerres, "ACPI power resources");
   98 
   99 static ACPI_STATUS      acpi_pwr_register_consumer(ACPI_HANDLE consumer);
  100 #ifdef notyet
  101 static ACPI_STATUS      acpi_pwr_deregister_consumer(ACPI_HANDLE consumer);
  102 #endif /* notyet */
  103 static ACPI_STATUS      acpi_pwr_register_resource(ACPI_HANDLE res);
  104 #ifdef notyet
  105 static ACPI_STATUS      acpi_pwr_deregister_resource(ACPI_HANDLE res);
  106 #endif /* notyet */
  107 static void             acpi_pwr_reference_resource(ACPI_OBJECT *obj,
  108                                                     void *arg);
  109 static int              acpi_pwr_dereference_resource(struct acpi_powerconsumer
  110                             *pc);
  111 static ACPI_STATUS      acpi_pwr_switch_power(void);
  112 static struct acpi_powerresource
  113                         *acpi_pwr_find_resource(ACPI_HANDLE res);
  114 static struct acpi_powerconsumer
  115                         *acpi_pwr_find_consumer(ACPI_HANDLE consumer);
  116 
  117 /* Initialise our lists. */    
  118 static void
  119 acpi_pwr_init(void *junk)
  120 {
  121     ACPI_SERIAL_INIT(powerres);
  122     TAILQ_INIT(&acpi_powerresources);
  123     TAILQ_INIT(&acpi_powerconsumers);
  124 }
  125 SYSINIT(acpi_powerresource, SI_BOOT1_TUNABLES, SI_ORDER_ANY, acpi_pwr_init, NULL);
  126 
  127 /*
  128  * Register a power resource.
  129  *
  130  * It's OK to call this if we already know about the resource.
  131  */
  132 static ACPI_STATUS
  133 acpi_pwr_register_resource(ACPI_HANDLE res)
  134 {
  135     ACPI_STATUS                 status;
  136     ACPI_BUFFER                 buf;
  137     ACPI_OBJECT                 *obj;
  138     struct acpi_powerresource   *rp, *srp;
  139 
  140     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  141     ACPI_SERIAL_ASSERT(powerres);
  142 
  143     rp = NULL;
  144     buf.Pointer = NULL;
  145     
  146     /* Look to see if we know about this resource */
  147     if (acpi_pwr_find_resource(res) != NULL)
  148         return_ACPI_STATUS (AE_OK);             /* already know about it */
  149 
  150     /* Allocate a new resource */
  151     if ((rp = kmalloc(sizeof(*rp), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) {
  152         status = AE_NO_MEMORY;
  153         goto out;
  154     }
  155     TAILQ_INIT(&rp->ap_references);
  156     rp->ap_resource = res;
  157 
  158     /* Get the Power Resource object */
  159     buf.Length = ACPI_ALLOCATE_BUFFER;
  160     if (ACPI_FAILURE(status = AcpiEvaluateObject(res, NULL, NULL, &buf))) {
  161         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "no power resource object\n"));
  162         goto out;
  163     }
  164     obj = buf.Pointer;
  165     if (obj->Type != ACPI_TYPE_POWER) {
  166         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  167                          "questionable power resource object %s\n",
  168                          acpi_name(res)));
  169         status = AE_TYPE;
  170         goto out;
  171     }
  172     rp->ap_systemlevel = obj->PowerResource.SystemLevel;
  173     rp->ap_order = obj->PowerResource.ResourceOrder;
  174     rp->ap_state = ACPI_PWR_UNK;
  175     
  176     /* Sort the resource into the list */
  177     status = AE_OK;
  178     srp = TAILQ_FIRST(&acpi_powerresources);
  179     if (srp == NULL || rp->ap_order < srp->ap_order) {
  180         TAILQ_INSERT_HEAD(&acpi_powerresources, rp, ap_link);
  181         goto done;
  182     }
  183     TAILQ_FOREACH(srp, &acpi_powerresources, ap_link) {
  184         if (rp->ap_order < srp->ap_order) {
  185             TAILQ_INSERT_BEFORE(srp, rp, ap_link);
  186             goto done;
  187         }
  188     }
  189     TAILQ_INSERT_TAIL(&acpi_powerresources, rp, ap_link);
  190 
  191  done:
  192     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  193                      "registered power resource %s\n", acpi_name(res)));
  194 
  195  out:
  196     if (buf.Pointer != NULL)
  197         AcpiOsFree(buf.Pointer);
  198     if (ACPI_FAILURE(status) && rp != NULL)
  199         kfree(rp, M_ACPIPWR);
  200     return_ACPI_STATUS (status);
  201 }
  202 
  203 #ifdef notyet
  204 /*
  205  * Deregister a power resource.
  206  */
  207 static ACPI_STATUS
  208 acpi_pwr_deregister_resource(ACPI_HANDLE res)
  209 {
  210     struct acpi_powerresource   *rp;
  211 
  212     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  213     ACPI_SERIAL_ASSERT(powerres);
  214 
  215     rp = NULL;
  216     
  217     /* Find the resource */
  218     if ((rp = acpi_pwr_find_resource(res)) == NULL)
  219         return_ACPI_STATUS (AE_BAD_PARAMETER);
  220 
  221     /* Check that there are no consumers referencing this resource */
  222     if (TAILQ_FIRST(&rp->ap_references) != NULL)
  223         return_ACPI_STATUS (AE_BAD_PARAMETER);
  224 
  225     /* Pull it off the list and kfree it */
  226     TAILQ_REMOVE(&acpi_powerresources, rp, ap_link);
  227     kfree(rp, M_ACPIPWR);
  228 
  229     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n",
  230                      acpi_name(res)));
  231 
  232     return_ACPI_STATUS (AE_OK);
  233 }
  234 #endif /* notyet */
  235 
  236 /*
  237  * Register a power consumer.  
  238  *
  239  * It's OK to call this if we already know about the consumer.
  240  */
  241 static ACPI_STATUS
  242 acpi_pwr_register_consumer(ACPI_HANDLE consumer)
  243 {
  244     struct acpi_powerconsumer   *pc;
  245     
  246     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  247     ACPI_SERIAL_ASSERT(powerres);
  248 
  249     /* Check to see whether we know about this consumer already */
  250     if (acpi_pwr_find_consumer(consumer) != NULL)
  251         return_ACPI_STATUS (AE_OK);
  252     
  253     /* Allocate a new power consumer */
  254     if ((pc = kmalloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL)
  255         return_ACPI_STATUS (AE_NO_MEMORY);
  256     TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link);
  257     TAILQ_INIT(&pc->ac_references);
  258     pc->ac_consumer = consumer;
  259 
  260     /* XXX we should try to find its current state */
  261     pc->ac_state = ACPI_STATE_UNKNOWN;
  262 
  263     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n",
  264                      acpi_name(consumer)));
  265     
  266     return_ACPI_STATUS (AE_OK);
  267 }
  268 
  269 #ifdef notyet
  270 /*
  271  * Deregister a power consumer.
  272  *
  273  * This should only be done once the consumer has been powered off.
  274  * (XXX is this correct?  Check once implemented)
  275  */
  276 static ACPI_STATUS
  277 acpi_pwr_deregister_consumer(ACPI_HANDLE consumer)
  278 {
  279     struct acpi_powerconsumer   *pc;
  280     
  281     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  282     ACPI_SERIAL_ASSERT(powerres);
  283 
  284     /* Find the consumer */
  285     if ((pc = acpi_pwr_find_consumer(consumer)) == NULL)
  286         return_ACPI_STATUS (AE_BAD_PARAMETER);
  287     
  288     /* Make sure the consumer's not referencing anything right now */
  289     if (TAILQ_FIRST(&pc->ac_references) != NULL)
  290         return_ACPI_STATUS (AE_BAD_PARAMETER);
  291 
  292     /* Pull the consumer off the list and kfree it */
  293     TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link);
  294     kfree(pc, M_ACPIPWR);
  295 
  296     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n",
  297                      acpi_name(consumer)));
  298 
  299     return_ACPI_STATUS (AE_OK);
  300 }
  301 #endif /* notyet */
  302 
  303 /*
  304  * Set a power consumer to a particular power state.
  305  */
  306 ACPI_STATUS
  307 acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state)
  308 {
  309     struct acpi_powerconsumer   *pc;
  310     ACPI_HANDLE                 method_handle, reslist_handle, pr0_handle;
  311     ACPI_BUFFER                 reslist_buffer;
  312     ACPI_OBJECT                 *reslist_object;
  313     ACPI_STATUS                 status;
  314     char                        *method_name, *reslist_name;
  315 
  316     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  317 
  318     /* It's never ok to switch a non-existent consumer. */
  319     if (consumer == NULL)
  320         return_ACPI_STATUS (AE_NOT_FOUND);
  321     reslist_buffer.Pointer = NULL;
  322     reslist_object = NULL;
  323     ACPI_SERIAL_BEGIN(powerres);
  324 
  325     /* Find the consumer */
  326     if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
  327         if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer)))
  328             goto out;
  329         if ((pc = acpi_pwr_find_consumer(consumer)) == NULL)
  330             panic("acpi added power consumer but can't find it");
  331     }
  332 
  333     /* Check for valid transitions.  We can only go to D0 from D3. */
  334     status = AE_BAD_PARAMETER;
  335     if (pc->ac_state == ACPI_STATE_D3 && state != ACPI_STATE_D0)
  336         goto out;
  337 
  338     /* Find transition mechanism(s) */
  339     switch (state) {
  340     case ACPI_STATE_D0:
  341         method_name = "_PS0";
  342         reslist_name = "_PR0";
  343         break;
  344     case ACPI_STATE_D1:
  345         method_name = "_PS1";
  346         reslist_name = "_PR1";
  347         break;
  348     case ACPI_STATE_D2:
  349         method_name = "_PS2";
  350         reslist_name = "_PR2";
  351         break;
  352     case ACPI_STATE_D3:
  353         method_name = "_PS3";
  354         reslist_name = "_PR3";
  355         break;
  356     default:
  357         goto out;
  358     }
  359     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "setup to switch %s D%d -> D%d\n",
  360                      acpi_name(consumer), pc->ac_state, state));
  361 
  362     /*
  363      * Verify that this state is supported, ie. one of method or
  364      * reslist must be present.  We need to do this before we go 
  365      * dereferencing resources (since we might be trying to go to
  366      * a state we don't support).
  367      *
  368      * Note that if any states are supported, the device has to
  369      * support D0 and D3.  It's never an error to try to go to
  370      * D0.
  371      */
  372     if (ACPI_FAILURE(AcpiGetHandle(consumer, method_name, &method_handle)))
  373         method_handle = NULL;
  374     if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle)))
  375         reslist_handle = NULL;
  376     if (reslist_handle == NULL && method_handle == NULL) {
  377         if (state == ACPI_STATE_D0) {
  378             pc->ac_state = ACPI_STATE_D0;
  379             status = AE_OK;
  380             goto out;
  381         }
  382         if (state != ACPI_STATE_D3) {
  383             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  384                 "attempt to set unsupported state D%d\n", state));
  385             goto out;
  386         }
  387 
  388         /*
  389          * Turn off the resources listed in _PR0 to go to D3.  If there is
  390          * no _PR0 method, this object doesn't support ACPI power states.
  391          */
  392         if (ACPI_FAILURE(AcpiGetHandle(consumer, "_PR0", &pr0_handle))) {
  393             status = AE_NOT_FOUND;
  394             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  395                 "device missing _PR0 (desired state was D%d)\n", state));
  396             goto out;
  397         }
  398         reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
  399         status = AcpiEvaluateObject(pr0_handle, NULL, NULL, &reslist_buffer);
  400         if (ACPI_FAILURE(status)) {
  401             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  402                 "can't evaluate _PR0 for device %s, state D%d\n",
  403                 acpi_name(consumer), state));
  404             goto out;
  405         }
  406         reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
  407         if (!ACPI_PKG_VALID(reslist_object, 1)) {
  408             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  409                 "invalid package object for state D%d\n", state));
  410             status = AE_TYPE;
  411             goto out;
  412         }
  413         AcpiOsFree(reslist_buffer.Pointer);
  414         reslist_buffer.Pointer = NULL;
  415         reslist_object = NULL;
  416     }
  417 
  418     /*
  419      * Check that we can actually fetch the list of power resources
  420      */
  421     if (reslist_handle != NULL) {
  422         reslist_buffer.Length = ACPI_ALLOCATE_BUFFER;
  423         status = AcpiEvaluateObject(reslist_handle, NULL, NULL,
  424                                     &reslist_buffer);
  425         if (ACPI_FAILURE(status)) {
  426             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  427                              "can't evaluate resource list %s\n",
  428                              acpi_name(reslist_handle)));
  429             goto out;
  430         }
  431         reslist_object = (ACPI_OBJECT *)reslist_buffer.Pointer;
  432         if (reslist_object->Type != ACPI_TYPE_PACKAGE) {
  433             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  434                              "resource list is not ACPI_TYPE_PACKAGE (%d)\n",
  435                              reslist_object->Type));
  436             status = AE_TYPE;
  437             goto out;
  438         }
  439     }
  440 
  441     /*
  442      * Now we are ready to switch, so kill off any current power
  443      * resource references.
  444      */
  445     acpi_pwr_dereference_resource(pc);
  446 
  447     /*
  448      * Add new power resource references, if we have any.  Traverse the
  449      * package that we got from evaluating reslist_handle, and look up each
  450      * of the resources that are referenced.
  451      */
  452     if (reslist_object != NULL) {
  453         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "referencing %d new resources\n", 
  454                           reslist_object->Package.Count));
  455         acpi_ForeachPackageObject(reslist_object, acpi_pwr_reference_resource,
  456                                   pc);
  457     }
  458 
  459     /*
  460      * If we changed anything in the resource list, we need to run a switch
  461      * pass now.
  462      */
  463     if (ACPI_FAILURE(status = acpi_pwr_switch_power())) {
  464         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  465                          "failed to switch resources from %s to D%d\n",
  466                           acpi_name(consumer), state));
  467 
  468         /* XXX is this appropriate?  Should we return to previous state? */
  469         goto out;
  470     }
  471 
  472     /* Invoke power state switch method (if present) */
  473     if (method_handle != NULL) {
  474         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  475                          "invoking state transition method %s\n",
  476                          acpi_name(method_handle)));
  477         status = AcpiEvaluateObject(method_handle, NULL, NULL, NULL);
  478         if (ACPI_FAILURE(status)) {
  479                 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "failed to set state - %s\n",
  480                                  AcpiFormatException(status)));
  481                 pc->ac_state = ACPI_STATE_UNKNOWN;
  482 
  483                 /* XXX Should we return to previous state? */
  484                 goto out;
  485         }
  486     }
  487 
  488     /* Transition was successful */
  489     pc->ac_state = state;
  490     status = AE_OK;
  491 
  492 out:
  493     ACPI_SERIAL_END(powerres);
  494     if (reslist_buffer.Pointer != NULL)
  495         AcpiOsFree(reslist_buffer.Pointer);
  496     return_ACPI_STATUS (status);
  497 }
  498 
  499 /* Enable or disable a power resource for wake */
  500 ACPI_STATUS
  501 acpi_pwr_wake_enable(ACPI_HANDLE consumer, int enable)
  502 {
  503     ACPI_STATUS status;
  504     struct acpi_powerconsumer *pc;
  505     struct acpi_prw_data prw;
  506     int i;
  507 
  508     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  509 
  510     if (consumer == NULL)
  511         return (AE_BAD_PARAMETER);
  512 
  513     ACPI_SERIAL_BEGIN(powerres);
  514     if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) {
  515         if (ACPI_FAILURE(status = acpi_pwr_register_consumer(consumer)))
  516             goto out;
  517         if ((pc = acpi_pwr_find_consumer(consumer)) == NULL)
  518             panic("acpi wake added power consumer but can't find it");
  519     }
  520 
  521     status = AE_OK;
  522     if (acpi_parse_prw(consumer, &prw) != 0)
  523         goto out;
  524     for (i = 0; i < prw.power_res_count; i++)
  525         if (enable)
  526             acpi_pwr_reference_resource(&prw.power_res[i], pc);
  527         else
  528             acpi_pwr_dereference_resource(pc);
  529 
  530     if (prw.power_res_count > 0)
  531         acpi_pwr_switch_power();
  532 
  533 out:
  534     ACPI_SERIAL_END(powerres);
  535     return (status);
  536 }
  537 
  538 /*
  539  * Called to create a reference between a power consumer and a power resource
  540  * identified in the object.
  541  */
  542 static void
  543 acpi_pwr_reference_resource(ACPI_OBJECT *obj, void *arg)
  544 {
  545     struct acpi_powerconsumer   *pc = (struct acpi_powerconsumer *)arg;
  546     struct acpi_powerreference  *pr;
  547     struct acpi_powerresource   *rp;
  548     ACPI_HANDLE                 res;
  549     ACPI_STATUS                 status;
  550 
  551     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  552     ACPI_SERIAL_ASSERT(powerres);
  553 
  554     res = acpi_GetReference(NULL, obj);
  555     if (res == NULL) {
  556         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  557                          "can't create a power reference for object type %d\n",
  558                          obj->Type));
  559         return_VOID;
  560     }
  561 
  562     /* Create/look up the resource */
  563     if (ACPI_FAILURE(status = acpi_pwr_register_resource(res))) {
  564         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  565                          "couldn't register power resource %s - %s\n",
  566                          obj->String.Pointer, AcpiFormatException(status)));
  567         return_VOID;
  568     }
  569     if ((rp = acpi_pwr_find_resource(res)) == NULL) {
  570         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "power resource list corrupted\n"));
  571         return_VOID;
  572     }
  573     ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "found power resource %s\n",
  574                      acpi_name(rp->ap_resource)));
  575 
  576     /* Create a reference between the consumer and resource */
  577     if ((pr = kmalloc(sizeof(*pr), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) {
  578         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  579                          "allocation failed for a power consumer reference\n"));
  580         return_VOID;
  581     }
  582     pr->ar_consumer = pc;
  583     pr->ar_resource = rp;
  584     TAILQ_INSERT_TAIL(&pc->ac_references, pr, ar_clink);
  585     TAILQ_INSERT_TAIL(&rp->ap_references, pr, ar_rlink);
  586 
  587     return_VOID;
  588 }
  589 
  590 static int
  591 acpi_pwr_dereference_resource(struct acpi_powerconsumer *pc)
  592 {
  593     struct acpi_powerreference *pr;
  594     int changed;
  595 
  596     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  597     ACPI_SERIAL_ASSERT(powerres);
  598 
  599     changed = 0;
  600     while ((pr = TAILQ_FIRST(&pc->ac_references)) != NULL) {
  601         ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "removing reference to %s\n",
  602                          acpi_name(pr->ar_resource->ap_resource)));
  603         TAILQ_REMOVE(&pr->ar_resource->ap_references, pr, ar_rlink);
  604         TAILQ_REMOVE(&pc->ac_references, pr, ar_clink);
  605         kfree(pr, M_ACPIPWR);
  606         changed = 1;
  607     }
  608 
  609     return (changed);
  610 }
  611 
  612 /*
  613  * Switch power resources to conform to the desired state.
  614  *
  615  * Consumers may have modified the power resource list in an arbitrary
  616  * fashion; we sweep it in sequence order.
  617  */
  618 static ACPI_STATUS
  619 acpi_pwr_switch_power(void)
  620 {
  621     struct acpi_powerresource   *rp;
  622     ACPI_STATUS                 status;
  623     int                         cur;
  624 
  625     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  626     ACPI_SERIAL_ASSERT(powerres);
  627 
  628     /*
  629      * Sweep the list forwards turning things on.
  630      */
  631     TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
  632         if (TAILQ_FIRST(&rp->ap_references) == NULL) {
  633             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  634                              "%s has no references, not turning on\n",
  635                              acpi_name(rp->ap_resource)));
  636             continue;
  637         }
  638 
  639         /* We could cache this if we trusted it not to change under us */
  640         status = acpi_GetInteger(rp->ap_resource, "_STA", &cur);
  641         if (ACPI_FAILURE(status)) {
  642             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
  643                               acpi_name(rp->ap_resource), status));
  644             /* XXX is this correct?  Always switch if in doubt? */
  645             continue;
  646         } else if (rp->ap_state == ACPI_PWR_UNK)
  647             rp->ap_state = cur;
  648 
  649         /*
  650          * Switch if required.  Note that we ignore the result of the switch
  651          * effort; we don't know what to do if it fails, so checking wouldn't
  652          * help much.
  653          */
  654         if (rp->ap_state != ACPI_PWR_ON) {
  655             status = AcpiEvaluateObject(rp->ap_resource, "_ON", NULL, NULL);
  656             if (ACPI_FAILURE(status)) {
  657                 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  658                                  "failed to switch %s on - %s\n", 
  659                                  acpi_name(rp->ap_resource),
  660                                  AcpiFormatException(status)));
  661             } else {
  662                 rp->ap_state = ACPI_PWR_ON;
  663                 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s on\n",
  664                                  acpi_name(rp->ap_resource)));
  665             }
  666         } else {
  667             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already on\n",
  668                              acpi_name(rp->ap_resource)));
  669         }
  670     }
  671     
  672     /* Sweep the list backwards turning things off. */
  673     TAILQ_FOREACH_REVERSE(rp, &acpi_powerresources, acpi_powerresource_list,
  674         ap_link) {
  675 
  676         if (TAILQ_FIRST(&rp->ap_references) != NULL) {
  677             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  678                              "%s has references, not turning off\n",
  679                              acpi_name(rp->ap_resource)));
  680             continue;
  681         }
  682 
  683         /* We could cache this if we trusted it not to change under us */
  684         status = acpi_GetInteger(rp->ap_resource, "_STA", &cur);
  685         if (ACPI_FAILURE(status)) {
  686             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "can't get status of %s - %d\n",
  687                               acpi_name(rp->ap_resource), status));
  688             /* XXX is this correct?  Always switch if in doubt? */
  689             continue;
  690         } else if (rp->ap_state == ACPI_PWR_UNK)
  691             rp->ap_state = cur;
  692 
  693         /*
  694          * Switch if required.  Note that we ignore the result of the switch
  695          * effort; we don't know what to do if it fails, so checking wouldn't
  696          * help much.
  697          */
  698         if (rp->ap_state != ACPI_PWR_OFF) {
  699             status = AcpiEvaluateObject(rp->ap_resource, "_OFF", NULL, NULL);
  700             if (ACPI_FAILURE(status)) {
  701                 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS,
  702                                  "failed to switch %s off - %s\n", 
  703                                  acpi_name(rp->ap_resource),
  704                                  AcpiFormatException(status)));
  705             } else {
  706                 rp->ap_state = ACPI_PWR_OFF;
  707                 ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "switched %s off\n",
  708                                  acpi_name(rp->ap_resource)));
  709             }
  710         } else {
  711             ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "%s is already off\n",
  712                              acpi_name(rp->ap_resource)));
  713         }
  714     }
  715 
  716     return_ACPI_STATUS (AE_OK);
  717 }
  718 
  719 /*
  720  * Find a power resource's control structure.
  721  */
  722 static struct acpi_powerresource *
  723 acpi_pwr_find_resource(ACPI_HANDLE res)
  724 {
  725     struct acpi_powerresource   *rp;
  726     
  727     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  728     ACPI_SERIAL_ASSERT(powerres);
  729 
  730     TAILQ_FOREACH(rp, &acpi_powerresources, ap_link) {
  731         if (rp->ap_resource == res)
  732             break;
  733     }
  734 
  735     return_PTR (rp);
  736 }
  737 
  738 /*
  739  * Find a power consumer's control structure.
  740  */
  741 static struct acpi_powerconsumer *
  742 acpi_pwr_find_consumer(ACPI_HANDLE consumer)
  743 {
  744     struct acpi_powerconsumer   *pc;
  745     
  746     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  747     ACPI_SERIAL_ASSERT(powerres);
  748 
  749     TAILQ_FOREACH(pc, &acpi_powerconsumers, ac_link) {
  750         if (pc->ac_consumer == consumer)
  751             break;
  752     }
  753 
  754     return_PTR (pc);
  755 }

Cache object: 00f4d4a48808b76a166da113ab6e307a


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