blob: 26e66427f4ff41ded4c017cf13331be05d1aa013 [file] [log] [blame]
Bob Moore7db5d822008-12-30 11:04:48 +08001
2/******************************************************************************
3 *
4 * Module Name: hwxface - Public ACPICA hardware interfaces
5 *
6 *****************************************************************************/
7
8/*
9 * Copyright (C) 2000 - 2008, Intel Corp.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
30 *
31 * NO WARRANTY
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
43 */
44
45#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050046#include "accommon.h"
47#include "acnamesp.h"
Bob Moore7db5d822008-12-30 11:04:48 +080048
49#define _COMPONENT ACPI_HARDWARE
50ACPI_MODULE_NAME("hwxface")
51
52/******************************************************************************
53 *
Bob Moored3fd9022008-12-30 11:11:57 +080054 * FUNCTION: acpi_reset
55 *
56 * PARAMETERS: None
57 *
58 * RETURN: Status
59 *
60 * DESCRIPTION: Set reset register in memory or IO space. Note: Does not
61 * support reset register in PCI config space, this must be
62 * handled separately.
63 *
64 ******************************************************************************/
65acpi_status acpi_reset(void)
66{
67 struct acpi_generic_address *reset_reg;
68 acpi_status status;
69
70 ACPI_FUNCTION_TRACE(acpi_reset);
71
72 reset_reg = &acpi_gbl_FADT.reset_register;
73
74 /* Check if the reset register is supported */
75
76 if (!(acpi_gbl_FADT.flags & ACPI_FADT_RESET_REGISTER) ||
77 !reset_reg->address) {
78 return_ACPI_STATUS(AE_NOT_EXIST);
79 }
80
81 /* Write the reset value to the reset register */
82
83 status = acpi_write(acpi_gbl_FADT.reset_value, reset_reg);
84 return_ACPI_STATUS(status);
85}
86
87ACPI_EXPORT_SYMBOL(acpi_reset)
88
89/******************************************************************************
90 *
Bob Moore7db5d822008-12-30 11:04:48 +080091 * FUNCTION: acpi_read
92 *
93 * PARAMETERS: Value - Where the value is returned
94 * Reg - GAS register structure
95 *
96 * RETURN: Status
97 *
98 * DESCRIPTION: Read from either memory or IO space.
99 *
100 ******************************************************************************/
101acpi_status acpi_read(u32 *value, struct acpi_generic_address *reg)
102{
103 u32 width;
104 u64 address;
105 acpi_status status;
106
107 ACPI_FUNCTION_NAME(acpi_read);
108
109 /*
Bob Mooreac0c8452009-02-18 14:28:02 +0800110 * Must have a valid pointer to a GAS structure, and a non-zero address
111 * within.
Bob Moore7db5d822008-12-30 11:04:48 +0800112 */
113 if (!reg) {
Bob Mooreac0c8452009-02-18 14:28:02 +0800114 return (AE_BAD_PARAMETER);
Bob Moore7db5d822008-12-30 11:04:48 +0800115 }
116
117 /* Get a local copy of the address. Handles possible alignment issues */
118
119 ACPI_MOVE_64_TO_64(&address, &reg->address);
120 if (!address) {
Bob Mooreac0c8452009-02-18 14:28:02 +0800121 return (AE_BAD_ADDRESS);
Bob Moore7db5d822008-12-30 11:04:48 +0800122 }
123
124 /* Supported widths are 8/16/32 */
125
126 width = reg->bit_width;
127 if ((width != 8) && (width != 16) && (width != 32)) {
128 return (AE_SUPPORT);
129 }
130
131 /* Initialize entire 32-bit return value to zero */
132
133 *value = 0;
134
135 /*
Bob Moore88dcb042009-02-18 16:01:04 +0800136 * Two address spaces supported: Memory or IO. PCI_Config is
137 * not supported here because the GAS structure is insufficient
Bob Moore7db5d822008-12-30 11:04:48 +0800138 */
139 switch (reg->space_id) {
140 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
141
142 status = acpi_os_read_memory((acpi_physical_address) address,
143 value, width);
144 break;
145
146 case ACPI_ADR_SPACE_SYSTEM_IO:
147
148 status =
149 acpi_os_read_port((acpi_io_address) address, value, width);
150 break;
151
152 default:
153 ACPI_ERROR((AE_INFO,
154 "Unsupported address space: %X", reg->space_id));
155 return (AE_BAD_PARAMETER);
156 }
157
158 ACPI_DEBUG_PRINT((ACPI_DB_IO,
159 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
160 *value, width, ACPI_FORMAT_UINT64(address),
161 acpi_ut_get_region_name(reg->space_id)));
162
163 return (status);
164}
165
166ACPI_EXPORT_SYMBOL(acpi_read)
167
168/******************************************************************************
169 *
170 * FUNCTION: acpi_write
171 *
172 * PARAMETERS: Value - To be written
173 * Reg - GAS register structure
174 *
175 * RETURN: Status
176 *
177 * DESCRIPTION: Write to either memory or IO space.
178 *
179 ******************************************************************************/
180acpi_status acpi_write(u32 value, struct acpi_generic_address *reg)
181{
182 u32 width;
183 u64 address;
184 acpi_status status;
185
186 ACPI_FUNCTION_NAME(acpi_write);
187
188 /*
Bob Mooreac0c8452009-02-18 14:28:02 +0800189 * Must have a valid pointer to a GAS structure, and a non-zero address
190 * within.
Bob Moore7db5d822008-12-30 11:04:48 +0800191 */
192 if (!reg) {
Bob Mooreac0c8452009-02-18 14:28:02 +0800193 return (AE_BAD_PARAMETER);
Bob Moore7db5d822008-12-30 11:04:48 +0800194 }
195
196 /* Get a local copy of the address. Handles possible alignment issues */
197
198 ACPI_MOVE_64_TO_64(&address, &reg->address);
199 if (!address) {
Bob Mooreac0c8452009-02-18 14:28:02 +0800200 return (AE_BAD_ADDRESS);
Bob Moore7db5d822008-12-30 11:04:48 +0800201 }
202
203 /* Supported widths are 8/16/32 */
204
205 width = reg->bit_width;
206 if ((width != 8) && (width != 16) && (width != 32)) {
207 return (AE_SUPPORT);
208 }
209
210 /*
211 * Two address spaces supported: Memory or IO.
212 * PCI_Config is not supported here because the GAS struct is insufficient
213 */
214 switch (reg->space_id) {
215 case ACPI_ADR_SPACE_SYSTEM_MEMORY:
216
217 status = acpi_os_write_memory((acpi_physical_address) address,
218 value, width);
219 break;
220
221 case ACPI_ADR_SPACE_SYSTEM_IO:
222
223 status = acpi_os_write_port((acpi_io_address) address, value,
224 width);
225 break;
226
227 default:
228 ACPI_ERROR((AE_INFO,
229 "Unsupported address space: %X", reg->space_id));
230 return (AE_BAD_PARAMETER);
231 }
232
233 ACPI_DEBUG_PRINT((ACPI_DB_IO,
234 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
235 value, width, ACPI_FORMAT_UINT64(address),
236 acpi_ut_get_region_name(reg->space_id)));
237
238 return (status);
239}
240
241ACPI_EXPORT_SYMBOL(acpi_write)
242
243/*******************************************************************************
244 *
Bob Moore50ffba12009-02-23 15:02:07 +0800245 * FUNCTION: acpi_read_bit_register
Bob Moore7db5d822008-12-30 11:04:48 +0800246 *
Bob Moore9892dd22009-02-18 15:10:07 +0800247 * PARAMETERS: register_id - ID of ACPI Bit Register to access
248 * return_value - Value that was read from the register,
249 * normalized to bit position zero.
Bob Moore7db5d822008-12-30 11:04:48 +0800250 *
Bob Moore9892dd22009-02-18 15:10:07 +0800251 * RETURN: Status and the value read from the specified Register. Value
Bob Moore7db5d822008-12-30 11:04:48 +0800252 * returned is normalized to bit0 (is shifted all the way right)
253 *
254 * DESCRIPTION: ACPI bit_register read function. Does not acquire the HW lock.
255 *
Bob Moore9892dd22009-02-18 15:10:07 +0800256 * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
257 * PM2 Control.
258 *
259 * Note: The hardware lock is not required when reading the ACPI bit registers
260 * since almost all of them are single bit and it does not matter that
261 * the parent hardware register can be split across two physical
262 * registers. The only multi-bit field is SLP_TYP in the PM1 control
263 * register, but this field does not cross an 8-bit boundary (nor does
264 * it make much sense to actually read this field.)
265 *
Bob Moore7db5d822008-12-30 11:04:48 +0800266 ******************************************************************************/
Bob Moore50ffba12009-02-23 15:02:07 +0800267acpi_status acpi_read_bit_register(u32 register_id, u32 *return_value)
Bob Moore7db5d822008-12-30 11:04:48 +0800268{
Bob Moore7db5d822008-12-30 11:04:48 +0800269 struct acpi_bit_register_info *bit_reg_info;
Bob Moore88dcb042009-02-18 16:01:04 +0800270 u32 register_value;
271 u32 value;
Bob Moore7db5d822008-12-30 11:04:48 +0800272 acpi_status status;
273
Bob Moore88dcb042009-02-18 16:01:04 +0800274 ACPI_FUNCTION_TRACE_U32(acpi_read_bit_register, register_id);
Bob Moore7db5d822008-12-30 11:04:48 +0800275
276 /* Get the info structure corresponding to the requested ACPI Register */
277
278 bit_reg_info = acpi_hw_get_bit_register_info(register_id);
279 if (!bit_reg_info) {
280 return_ACPI_STATUS(AE_BAD_PARAMETER);
281 }
282
Bob Moore9892dd22009-02-18 15:10:07 +0800283 /* Read the entire parent register */
Bob Moore7db5d822008-12-30 11:04:48 +0800284
285 status = acpi_hw_register_read(bit_reg_info->parent_register,
286 &register_value);
Bob Moore88dcb042009-02-18 16:01:04 +0800287 if (ACPI_FAILURE(status)) {
288 return_ACPI_STATUS(status);
Bob Moore7db5d822008-12-30 11:04:48 +0800289 }
290
Bob Moore88dcb042009-02-18 16:01:04 +0800291 /* Normalize the value that was read, mask off other bits */
292
293 value = ((register_value & bit_reg_info->access_bit_mask)
294 >> bit_reg_info->bit_position);
295
296 ACPI_DEBUG_PRINT((ACPI_DB_IO,
297 "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
298 register_id, bit_reg_info->parent_register,
299 register_value, value));
300
301 *return_value = value;
302 return_ACPI_STATUS(AE_OK);
Bob Moore7db5d822008-12-30 11:04:48 +0800303}
304
Bob Moore50ffba12009-02-23 15:02:07 +0800305ACPI_EXPORT_SYMBOL(acpi_read_bit_register)
Bob Moore7db5d822008-12-30 11:04:48 +0800306
307/*******************************************************************************
308 *
Bob Moore50ffba12009-02-23 15:02:07 +0800309 * FUNCTION: acpi_write_bit_register
Bob Moore7db5d822008-12-30 11:04:48 +0800310 *
Bob Moore9892dd22009-02-18 15:10:07 +0800311 * PARAMETERS: register_id - ID of ACPI Bit Register to access
312 * Value - Value to write to the register, in bit
313 * position zero. The bit is automaticallly
314 * shifted to the correct position.
Bob Moore7db5d822008-12-30 11:04:48 +0800315 *
316 * RETURN: Status
317 *
Bob Moore9892dd22009-02-18 15:10:07 +0800318 * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
319 * since most operations require a read/modify/write sequence.
320 *
321 * SUPPORTS: Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
322 * PM2 Control.
Bob Moore7db5d822008-12-30 11:04:48 +0800323 *
Bob Moore88dcb042009-02-18 16:01:04 +0800324 * Note that at this level, the fact that there may be actually two
325 * hardware registers (A and B - and B may not exist) is abstracted.
326 *
Bob Moore7db5d822008-12-30 11:04:48 +0800327 ******************************************************************************/
Bob Moore50ffba12009-02-23 15:02:07 +0800328acpi_status acpi_write_bit_register(u32 register_id, u32 value)
Bob Moore7db5d822008-12-30 11:04:48 +0800329{
Bob Moore7db5d822008-12-30 11:04:48 +0800330 struct acpi_bit_register_info *bit_reg_info;
Bob Moore7db5d822008-12-30 11:04:48 +0800331 acpi_cpu_flags lock_flags;
Bob Moore88dcb042009-02-18 16:01:04 +0800332 u32 register_value;
333 acpi_status status = AE_OK;
Bob Moore7db5d822008-12-30 11:04:48 +0800334
Bob Moore50ffba12009-02-23 15:02:07 +0800335 ACPI_FUNCTION_TRACE_U32(acpi_write_bit_register, register_id);
Bob Moore7db5d822008-12-30 11:04:48 +0800336
337 /* Get the info structure corresponding to the requested ACPI Register */
338
339 bit_reg_info = acpi_hw_get_bit_register_info(register_id);
340 if (!bit_reg_info) {
Bob Moore7db5d822008-12-30 11:04:48 +0800341 return_ACPI_STATUS(AE_BAD_PARAMETER);
342 }
343
344 lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
345
Bob Moore7db5d822008-12-30 11:04:48 +0800346 /*
Bob Moore88dcb042009-02-18 16:01:04 +0800347 * At this point, we know that the parent register is one of the
348 * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
Bob Moore7db5d822008-12-30 11:04:48 +0800349 */
Bob Moore88dcb042009-02-18 16:01:04 +0800350 if (bit_reg_info->parent_register != ACPI_REGISTER_PM1_STATUS) {
Bob Moore7db5d822008-12-30 11:04:48 +0800351 /*
Bob Moore88dcb042009-02-18 16:01:04 +0800352 * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
353 *
354 * Perform a register read to preserve the bits that we are not
355 * interested in
Bob Moore7db5d822008-12-30 11:04:48 +0800356 */
Bob Moore88dcb042009-02-18 16:01:04 +0800357 status = acpi_hw_register_read(bit_reg_info->parent_register,
Bob Moore7db5d822008-12-30 11:04:48 +0800358 &register_value);
359 if (ACPI_FAILURE(status)) {
360 goto unlock_and_exit;
361 }
362
Bob Moore88dcb042009-02-18 16:01:04 +0800363 /*
364 * Insert the input bit into the value that was just read
365 * and write the register
366 */
Bob Moore7db5d822008-12-30 11:04:48 +0800367 ACPI_REGISTER_INSERT_VALUE(register_value,
368 bit_reg_info->bit_position,
369 bit_reg_info->access_bit_mask,
370 value);
371
Bob Moore88dcb042009-02-18 16:01:04 +0800372 status = acpi_hw_register_write(bit_reg_info->parent_register,
373 register_value);
374 } else {
375 /*
376 * 2) Case for PM1 Status
377 *
378 * The Status register is different from the rest. Clear an event
379 * by writing 1, writing 0 has no effect. So, the only relevant
380 * information is the single bit we're interested in, all others
381 * should be written as 0 so they will be left unchanged.
382 */
383 register_value = ACPI_REGISTER_PREPARE_BITS(value,
384 bit_reg_info->
385 bit_position,
386 bit_reg_info->
387 access_bit_mask);
Bob Moore7db5d822008-12-30 11:04:48 +0800388
Bob Moore88dcb042009-02-18 16:01:04 +0800389 /* No need to write the register if value is all zeros */
Bob Moore7db5d822008-12-30 11:04:48 +0800390
Bob Moore88dcb042009-02-18 16:01:04 +0800391 if (register_value) {
392 status =
393 acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
394 register_value);
395 }
Bob Moore7db5d822008-12-30 11:04:48 +0800396 }
397
Bob Moore88dcb042009-02-18 16:01:04 +0800398 ACPI_DEBUG_PRINT((ACPI_DB_IO,
399 "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
400 register_id, bit_reg_info->parent_register, value,
401 register_value));
402
403unlock_and_exit:
Bob Moore7db5d822008-12-30 11:04:48 +0800404
405 acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
Bob Moore7db5d822008-12-30 11:04:48 +0800406 return_ACPI_STATUS(status);
407}
408
Bob Moore50ffba12009-02-23 15:02:07 +0800409ACPI_EXPORT_SYMBOL(acpi_write_bit_register)
Bob Moore7db5d822008-12-30 11:04:48 +0800410
411/*******************************************************************************
412 *
413 * FUNCTION: acpi_get_sleep_type_data
414 *
415 * PARAMETERS: sleep_state - Numeric sleep state
416 * *sleep_type_a - Where SLP_TYPa is returned
417 * *sleep_type_b - Where SLP_TYPb is returned
418 *
419 * RETURN: Status - ACPI status
420 *
421 * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested sleep
422 * state.
423 *
424 ******************************************************************************/
425acpi_status
426acpi_get_sleep_type_data(u8 sleep_state, u8 *sleep_type_a, u8 *sleep_type_b)
427{
428 acpi_status status = AE_OK;
429 struct acpi_evaluate_info *info;
430
431 ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
432
433 /* Validate parameters */
434
435 if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
436 return_ACPI_STATUS(AE_BAD_PARAMETER);
437 }
438
439 /* Allocate the evaluation information block */
440
441 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
442 if (!info) {
443 return_ACPI_STATUS(AE_NO_MEMORY);
444 }
445
446 info->pathname =
447 ACPI_CAST_PTR(char, acpi_gbl_sleep_state_names[sleep_state]);
448
449 /* Evaluate the namespace object containing the values for this state */
450
451 status = acpi_ns_evaluate(info);
452 if (ACPI_FAILURE(status)) {
453 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
454 "%s while evaluating SleepState [%s]\n",
455 acpi_format_exception(status),
456 info->pathname));
457
458 goto cleanup;
459 }
460
461 /* Must have a return object */
462
463 if (!info->return_object) {
464 ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
465 info->pathname));
466 status = AE_NOT_EXIST;
467 }
468
469 /* It must be of type Package */
470
Bob Moore3371c192009-02-18 14:44:03 +0800471 else if (info->return_object->common.type != ACPI_TYPE_PACKAGE) {
Bob Moore7db5d822008-12-30 11:04:48 +0800472 ACPI_ERROR((AE_INFO,
473 "Sleep State return object is not a Package"));
474 status = AE_AML_OPERAND_TYPE;
475 }
476
477 /*
478 * The package must have at least two elements. NOTE (March 2005): This
479 * goes against the current ACPI spec which defines this object as a
480 * package with one encoded DWORD element. However, existing practice
481 * by BIOS vendors seems to be to have 2 or more elements, at least
482 * one per sleep type (A/B).
483 */
484 else if (info->return_object->package.count < 2) {
485 ACPI_ERROR((AE_INFO,
486 "Sleep State return package does not have at least two elements"));
487 status = AE_AML_NO_OPERAND;
488 }
489
490 /* The first two elements must both be of type Integer */
491
Bob Moore3371c192009-02-18 14:44:03 +0800492 else if (((info->return_object->package.elements[0])->common.type
Bob Moore7db5d822008-12-30 11:04:48 +0800493 != ACPI_TYPE_INTEGER) ||
Bob Moore3371c192009-02-18 14:44:03 +0800494 ((info->return_object->package.elements[1])->common.type
Bob Moore7db5d822008-12-30 11:04:48 +0800495 != ACPI_TYPE_INTEGER)) {
496 ACPI_ERROR((AE_INFO,
Bob Moored4913dc2009-03-06 10:05:18 +0800497 "Sleep State return package elements are not both Integers "
498 "(%s, %s)",
Bob Moore7db5d822008-12-30 11:04:48 +0800499 acpi_ut_get_object_type_name(info->return_object->
500 package.elements[0]),
501 acpi_ut_get_object_type_name(info->return_object->
502 package.elements[1])));
503 status = AE_AML_OPERAND_TYPE;
504 } else {
505 /* Valid _Sx_ package size, type, and value */
506
507 *sleep_type_a = (u8)
508 (info->return_object->package.elements[0])->integer.value;
509 *sleep_type_b = (u8)
510 (info->return_object->package.elements[1])->integer.value;
511 }
512
513 if (ACPI_FAILURE(status)) {
514 ACPI_EXCEPTION((AE_INFO, status,
515 "While evaluating SleepState [%s], bad Sleep object %p type %s",
516 info->pathname, info->return_object,
517 acpi_ut_get_object_type_name(info->
518 return_object)));
519 }
520
521 acpi_ut_remove_reference(info->return_object);
522
523 cleanup:
524 ACPI_FREE(info);
525 return_ACPI_STATUS(status);
526}
527
528ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)