Changeset View
Standalone View
sys/dev/acpica/acpi_powerres.c
| Show First 20 Lines • Show All 70 Lines • ▼ Show 20 Lines | struct acpi_powerreference { | ||||
| TAILQ_ENTRY(acpi_powerreference) ar_clink; /* link on consumer */ | TAILQ_ENTRY(acpi_powerreference) ar_clink; /* link on consumer */ | ||||
| }; | }; | ||||
| /* A power-managed device. */ | /* A power-managed device. */ | ||||
| struct acpi_powerconsumer { | struct acpi_powerconsumer { | ||||
| /* Device which is powered */ | /* Device which is powered */ | ||||
| ACPI_HANDLE ac_consumer; | ACPI_HANDLE ac_consumer; | ||||
| int ac_state; | int ac_state; | ||||
| struct { | |||||
| bool prx_has; | |||||
| size_t prx_count; | |||||
| ACPI_HANDLE *prx_deps; | |||||
| } ac_prx[ACPI_D_STATE_COUNT]; | |||||
| TAILQ_ENTRY(acpi_powerconsumer) ac_link; | TAILQ_ENTRY(acpi_powerconsumer) ac_link; | ||||
| TAILQ_HEAD(,acpi_powerreference) ac_references; | TAILQ_HEAD(,acpi_powerreference) ac_references; | ||||
| }; | }; | ||||
| /* A power resource. */ | /* A power resource. */ | ||||
| struct acpi_powerresource { | struct acpi_powerresource { | ||||
| TAILQ_ENTRY(acpi_powerresource) ap_link; | TAILQ_ENTRY(acpi_powerresource) ap_link; | ||||
| TAILQ_HEAD(,acpi_powerreference) ap_references; | TAILQ_HEAD(,acpi_powerreference) ap_references; | ||||
| ACPI_HANDLE ap_resource; | ACPI_HANDLE ap_resource; | ||||
| UINT64 ap_systemlevel; | UINT64 ap_systemlevel; | ||||
| UINT64 ap_order; | UINT64 ap_order; | ||||
| }; | }; | ||||
| static TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource) | static TAILQ_HEAD(acpi_powerresource_list, acpi_powerresource) | ||||
| acpi_powerresources = TAILQ_HEAD_INITIALIZER(acpi_powerresources); | acpi_powerresources = TAILQ_HEAD_INITIALIZER(acpi_powerresources); | ||||
| static TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer) | static TAILQ_HEAD(acpi_powerconsumer_list, acpi_powerconsumer) | ||||
| acpi_powerconsumers = TAILQ_HEAD_INITIALIZER(acpi_powerconsumers); | acpi_powerconsumers = TAILQ_HEAD_INITIALIZER(acpi_powerconsumers); | ||||
| ACPI_SERIAL_DECL(powerres, "ACPI power resources"); | ACPI_SERIAL_DECL(powerres, "ACPI power resources"); | ||||
| static ACPI_STATUS acpi_pwr_register_consumer(ACPI_HANDLE consumer); | static ACPI_STATUS acpi_pwr_register_consumer(ACPI_HANDLE consumer); | ||||
| #ifdef notyet | |||||
| static ACPI_STATUS acpi_pwr_deregister_consumer(ACPI_HANDLE consumer); | static ACPI_STATUS acpi_pwr_deregister_consumer(ACPI_HANDLE consumer); | ||||
| #endif /* notyet */ | |||||
| static ACPI_STATUS acpi_pwr_register_resource(ACPI_HANDLE res); | static ACPI_STATUS acpi_pwr_register_resource(ACPI_HANDLE res); | ||||
| #ifdef notyet | #ifdef notyet | ||||
| static ACPI_STATUS acpi_pwr_deregister_resource(ACPI_HANDLE res); | static ACPI_STATUS acpi_pwr_deregister_resource(ACPI_HANDLE res); | ||||
| #endif /* notyet */ | #endif /* notyet */ | ||||
| static void acpi_pwr_reference_resource(ACPI_OBJECT *obj, | static void acpi_pwr_reference_resource(ACPI_OBJECT *obj, | ||||
| void *arg); | void *arg); | ||||
| static int acpi_pwr_dereference_resource(struct acpi_powerconsumer | static int acpi_pwr_dereference_resource(struct acpi_powerconsumer | ||||
| *pc); | *pc); | ||||
| ▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | return_ACPI_STATUS (AE_BAD_PARAMETER); | ||||
| free(rp, M_ACPIPWR); | free(rp, M_ACPIPWR); | ||||
| ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n", | ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power resource %s\n", | ||||
| acpi_name(res))); | acpi_name(res))); | ||||
| return_ACPI_STATUS (AE_OK); | return_ACPI_STATUS (AE_OK); | ||||
| } | } | ||||
| #endif /* notyet */ | #endif /* notyet */ | ||||
| /* | |||||
| * Evaluate the _PRx (power resources each D-state depends on). This also | |||||
| * populates the acpi_powerresources queue with the power resources discovered | |||||
| * during this step. | |||||
| * | |||||
| * ACPI 7.3.8 - 7.3.11 guarantee that _PRx will return the same data each | |||||
| * time they are evaluated. | |||||
| * | |||||
| * If this function fails, acpi_pwr_deregister_consumer() must be called on the | |||||
| * power consumer to free already allocated memory. | |||||
| */ | |||||
| static ACPI_STATUS | |||||
| acpi_pwr_get_power_resources(ACPI_HANDLE consumer, struct acpi_powerconsumer *pc) | |||||
| { | |||||
| ACPI_INTEGER status; | |||||
| ACPI_STRING reslist_name; | |||||
| ACPI_HANDLE reslist_handle; | |||||
| ACPI_STRING reslist_names[] = {"_PR0", "_PR1", "_PR2", "_PR3"}; | |||||
| ACPI_BUFFER reslist; | |||||
| ACPI_OBJECT *reslist_object; | |||||
| ACPI_OBJECT *dep; | |||||
| ACPI_HANDLE *res; | |||||
| ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); | |||||
| ACPI_SERIAL_ASSERT(powerres); | |||||
| MPASS(consumer != NULL); | |||||
| for (int state = ACPI_STATE_D0; state <= ACPI_STATE_D3_HOT; state++) { | |||||
| pc->ac_prx[state].prx_has = false; | |||||
| pc->ac_prx[state].prx_count = 0; | |||||
| pc->ac_prx[state].prx_deps = NULL; | |||||
obiwac: I guess this is technically redundant as we're allocating this memory with `M_ZERO` now. Should… | |||||
Done Inline ActionsI think having the explicit initialization is useful for readability. markj: I think having the explicit initialization is useful for readability. | |||||
| reslist_name = reslist_names[state - ACPI_STATE_D0]; | |||||
| if (ACPI_FAILURE(AcpiGetHandle(consumer, reslist_name, &reslist_handle))) | |||||
| continue; | |||||
| reslist.Pointer = NULL; | |||||
| reslist.Length = ACPI_ALLOCATE_BUFFER; | |||||
| status = AcpiEvaluateObjectTyped(reslist_handle, NULL, NULL, &reslist, | |||||
| ACPI_TYPE_PACKAGE); | |||||
| if (ACPI_FAILURE(status) || reslist.Pointer == NULL) | |||||
| /* | |||||
| * ACPI_ALLOCATE_BUFFER entails everything will be freed on error | |||||
| * by AcpiEvaluateObjectTyped. | |||||
| */ | |||||
| continue; | |||||
Done Inline Actionsreslist_object leaked imp: reslist_object leaked | |||||
Done Inline Actionsthanks :) obiwac: thanks :) | |||||
| reslist_object = (ACPI_OBJECT *)reslist.Pointer; | |||||
| pc->ac_prx[state].prx_has = true; | |||||
| pc->ac_prx[state].prx_count = reslist_object->Package.Count; | |||||
Not Done Inline ActionsStyle: 4 space indent, not open paren indent, here and elsewhere. imp: Style: 4 space indent, not open paren indent, here and elsewhere. | |||||
Not Done Inline Actionselsewhere in the file uses this style. Should I make this 4 spaces anyways? obiwac: elsewhere in the file uses this style. Should I make this 4 spaces anyways? | |||||
| if (reslist_object->Package.Count == 0) { | |||||
| AcpiOsFree(reslist_object); | |||||
| continue; | |||||
| } | |||||
| pc->ac_prx[state].prx_deps = mallocarray(pc->ac_prx[state].prx_count, | |||||
| sizeof(*pc->ac_prx[state].prx_deps), M_ACPIPWR, M_NOWAIT); | |||||
Done Inline ActionsShouldn't this be allocating an array of size pc->ac_prx[state].prx_count? You can use mallocarray() here. markj: Shouldn't this be allocating an array of size `pc->ac_prx[state].prx_count`? You can use… | |||||
Done Inline Actionsabsolutely, thanks for catching that I'm not sure how to handle the styling of the wrapping here since aligning the next line to the paren (like in the rest of this file) crosses 80 chars, so I've just done 4 spaces. obiwac: absolutely, thanks for catching that
I'm not sure how to handle the styling of the wrapping… | |||||
| if (pc->ac_prx[state].prx_deps == NULL) { | |||||
| AcpiOsFree(reslist_object); | |||||
| return_ACPI_STATUS (AE_NO_MEMORY); | |||||
| } | |||||
| for (size_t i = 0; i < reslist_object->Package.Count; i++) { | |||||
| dep = &reslist_object->Package.Elements[i]; | |||||
| res = dep->Reference.Handle; | |||||
| pc->ac_prx[state].prx_deps[i] = res; | |||||
| /* It's fine to attempt to register the same resource twice. */ | |||||
| acpi_pwr_register_resource(res); | |||||
| } | |||||
| AcpiOsFree(reslist_object); | |||||
| } | |||||
| return_ACPI_STATUS (AE_OK); | |||||
| } | |||||
| /* | /* | ||||
| * Register a power consumer. | * Register a power consumer. | ||||
| * | * | ||||
| * It's OK to call this if we already know about the consumer. | * It's OK to call this if we already know about the consumer. | ||||
| */ | */ | ||||
| static ACPI_STATUS | static ACPI_STATUS | ||||
| acpi_pwr_register_consumer(ACPI_HANDLE consumer) | acpi_pwr_register_consumer(ACPI_HANDLE consumer) | ||||
| { | { | ||||
| ACPI_INTEGER status; | |||||
| struct acpi_powerconsumer *pc; | struct acpi_powerconsumer *pc; | ||||
| ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); | ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); | ||||
| ACPI_SERIAL_ASSERT(powerres); | ACPI_SERIAL_ASSERT(powerres); | ||||
| /* Check to see whether we know about this consumer already */ | /* Check to see whether we know about this consumer already */ | ||||
| if (acpi_pwr_find_consumer(consumer) != NULL) | if (acpi_pwr_find_consumer(consumer) != NULL) | ||||
| return_ACPI_STATUS (AE_OK); | return_ACPI_STATUS (AE_OK); | ||||
| /* Allocate a new power consumer */ | /* Allocate a new power consumer */ | ||||
| if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT)) == NULL) | if ((pc = malloc(sizeof(*pc), M_ACPIPWR, M_NOWAIT | M_ZERO)) == NULL) | ||||
| return_ACPI_STATUS (AE_NO_MEMORY); | return_ACPI_STATUS (AE_NO_MEMORY); | ||||
| TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); | TAILQ_INSERT_HEAD(&acpi_powerconsumers, pc, ac_link); | ||||
| TAILQ_INIT(&pc->ac_references); | TAILQ_INIT(&pc->ac_references); | ||||
| pc->ac_consumer = consumer; | pc->ac_consumer = consumer; | ||||
| /* | |||||
| * Get all its power resource dependencies, if it has _PRx. We do this now | |||||
| * as an opportunity to populate the acpi_powerresources queue. | |||||
| * | |||||
| * If this fails, immediately deregister it. | |||||
Done Inline ActionsYou're ignoring errors here. Why have them returned? imp: You're ignoring errors here. Why have them returned?
And when you can't allocate memory or… | |||||
Done Inline Actionsnow deregistering power consumer on error obiwac: now deregistering power consumer on error | |||||
| */ | |||||
| status = acpi_pwr_get_power_resources(consumer, pc); | |||||
| if (ACPI_FAILURE(status)) { | |||||
| ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, | |||||
| "failed to get power resources for %s\n", | |||||
| acpi_name(consumer))); | |||||
| acpi_pwr_deregister_consumer(consumer); | |||||
| return_ACPI_STATUS (status); | |||||
| } | |||||
| /* XXX we should try to find its current state */ | /* XXX we should try to find its current state */ | ||||
| pc->ac_state = ACPI_STATE_UNKNOWN; | pc->ac_state = ACPI_STATE_UNKNOWN; | ||||
| ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n", | ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "registered power consumer %s\n", | ||||
| acpi_name(consumer))); | acpi_name(consumer))); | ||||
| return_ACPI_STATUS (AE_OK); | return_ACPI_STATUS (AE_OK); | ||||
| } | } | ||||
| #ifdef notyet | |||||
| /* | /* | ||||
| * Deregister a power consumer. | * Deregister a power consumer. | ||||
| * | * | ||||
| * This should only be done once the consumer has been powered off. | * This should only be done once the consumer has been powered off. | ||||
| * (XXX is this correct? Check once implemented) | * (XXX is this correct? Check once implemented) | ||||
Done Inline ActionsCan we remove this XXX comment? markj: Can we remove this XXX comment? | |||||
Done Inline ActionsWe're not yet doing this once the consumer has been powered off (only in acpi_pwr_register_consumer when acpi_pwr_get_power_resources fails). I haven't looked into calling this anywhere else yet. obiwac: We're not yet doing this once the consumer has been powered off (only in… | |||||
| */ | */ | ||||
| static ACPI_STATUS | static ACPI_STATUS | ||||
| acpi_pwr_deregister_consumer(ACPI_HANDLE consumer) | acpi_pwr_deregister_consumer(ACPI_HANDLE consumer) | ||||
| { | { | ||||
| struct acpi_powerconsumer *pc; | struct acpi_powerconsumer *pc; | ||||
| ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); | ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); | ||||
| ACPI_SERIAL_ASSERT(powerres); | ACPI_SERIAL_ASSERT(powerres); | ||||
| /* Find the consumer */ | /* Find the consumer */ | ||||
| if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) | if ((pc = acpi_pwr_find_consumer(consumer)) == NULL) | ||||
| return_ACPI_STATUS (AE_BAD_PARAMETER); | return_ACPI_STATUS (AE_BAD_PARAMETER); | ||||
| /* Make sure the consumer's not referencing anything right now */ | /* Make sure the consumer's not referencing anything right now */ | ||||
| if (TAILQ_FIRST(&pc->ac_references) != NULL) | if (TAILQ_FIRST(&pc->ac_references) != NULL) | ||||
| return_ACPI_STATUS (AE_BAD_PARAMETER); | return_ACPI_STATUS (AE_BAD_PARAMETER); | ||||
| /* Pull the consumer off the list and free it */ | /* Pull the consumer off the list and free it */ | ||||
| TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link); | TAILQ_REMOVE(&acpi_powerconsumers, pc, ac_link); | ||||
| for (size_t i = 0; i < sizeof(pc->ac_prx) / sizeof(*pc->ac_prx); i++) | |||||
| if (pc->ac_prx[i].prx_deps != NULL) | |||||
| free(pc->ac_prx[i].prx_deps, M_ACPIPWR); | |||||
| free(pc, M_ACPIPWR); | free(pc, M_ACPIPWR); | ||||
| ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n", | ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "deregistered power consumer %s\n", | ||||
| acpi_name(consumer))); | acpi_name(consumer))); | ||||
| return_ACPI_STATUS (AE_OK); | return_ACPI_STATUS (AE_OK); | ||||
| } | } | ||||
| #endif /* notyet */ | |||||
| /* | /* | ||||
| * Set a power consumer to a particular power state. | * Set a power consumer to a particular power state. | ||||
| */ | */ | ||||
| ACPI_STATUS | ACPI_STATUS | ||||
| acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state) | acpi_pwr_switch_consumer(ACPI_HANDLE consumer, int state) | ||||
| { | { | ||||
| struct acpi_powerconsumer *pc; | struct acpi_powerconsumer *pc; | ||||
| ▲ Show 20 Lines • Show All 467 Lines • Show Last 20 Lines | |||||
I guess this is technically redundant as we're allocating this memory with M_ZERO now. Should I keep this explicit anyway?