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