1 /*******************************************************************************
3 * Module Name: hwregs - Read/write access functions for the various ACPI
4 * control and status registers.
6 ******************************************************************************/
9 * Copyright (C) 2000 - 2016, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
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.
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.
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.
45 #include <acpi/acpi.h>
49 #define _COMPONENT ACPI_HARDWARE
50 ACPI_MODULE_NAME("hwregs")
52 #if (!ACPI_REDUCED_HARDWARE)
53 /* Local Prototypes */
55 acpi_hw_read_multiple(u32 *value,
56 struct acpi_generic_address *register_a,
57 struct acpi_generic_address *register_b);
60 acpi_hw_write_multiple(u32 value,
61 struct acpi_generic_address *register_a,
62 struct acpi_generic_address *register_b);
64 #endif /* !ACPI_REDUCED_HARDWARE */
66 /******************************************************************************
68 * FUNCTION: acpi_hw_validate_register
70 * PARAMETERS: reg - GAS register structure
71 * max_bit_width - Max bit_width supported (32 or 64)
72 * address - Pointer to where the gas->address
77 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
78 * pointer, Address, space_id, bit_width, and bit_offset.
80 ******************************************************************************/
83 acpi_hw_validate_register(struct acpi_generic_address *reg,
84 u8 max_bit_width, u64 *address)
89 /* Must have a valid pointer to a GAS structure */
92 return (AE_BAD_PARAMETER);
96 * Copy the target address. This handles possible alignment issues.
97 * Address must not be null. A null address also indicates an optional
98 * ACPI register that is not supported, so no error message.
100 ACPI_MOVE_64_TO_64(address, ®->address);
102 return (AE_BAD_ADDRESS);
105 /* Validate the space_ID */
107 if ((reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
108 (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
110 "Unsupported address space: 0x%X", reg->space_id));
114 /* Validate the access_width */
116 if (reg->access_width > 4) {
118 "Unsupported register access width: 0x%X",
123 /* Validate the bit_width, convert access_width into number of bits */
125 access_width = reg->access_width ? reg->access_width : 1;
126 access_width = 1 << (access_width + 2);
128 ACPI_ROUND_UP(reg->bit_offset + reg->bit_width, access_width);
129 if (max_bit_width < bit_width) {
130 ACPI_WARNING((AE_INFO,
131 "Requested bit width 0x%X is smaller than register bit width 0x%X",
132 max_bit_width, bit_width));
139 /******************************************************************************
141 * FUNCTION: acpi_hw_read
143 * PARAMETERS: value - Where the value is returned
144 * reg - GAS register structure
148 * DESCRIPTION: Read from either memory or IO space. This is a 32-bit max
149 * version of acpi_read, used internally since the overhead of
150 * 64-bit values is not needed.
152 * LIMITATIONS: <These limitations also apply to acpi_hw_write>
153 * bit_width must be exactly 8, 16, or 32.
154 * space_ID must be system_memory or system_IO.
155 * bit_offset and access_width are currently ignored, as there has
156 * not been a need to implement these.
158 ******************************************************************************/
160 acpi_status acpi_hw_read(u32 *value, struct acpi_generic_address *reg)
166 ACPI_FUNCTION_NAME(hw_read);
168 /* Validate contents of the GAS register */
170 status = acpi_hw_validate_register(reg, 32, &address);
171 if (ACPI_FAILURE(status)) {
175 /* Initialize entire 32-bit return value to zero */
180 * Two address spaces supported: Memory or IO. PCI_Config is
181 * not supported here because the GAS structure is insufficient
183 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
184 status = acpi_os_read_memory((acpi_physical_address)
185 address, &value64, reg->bit_width);
187 *value = (u32)value64;
188 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
190 status = acpi_hw_read_port((acpi_io_address)
191 address, value, reg->bit_width);
194 ACPI_DEBUG_PRINT((ACPI_DB_IO,
195 "Read: %8.8X width %2d from %8.8X%8.8X (%s)\n",
196 *value, reg->bit_width, ACPI_FORMAT_UINT64(address),
197 acpi_ut_get_region_name(reg->space_id)));
202 /******************************************************************************
204 * FUNCTION: acpi_hw_write
206 * PARAMETERS: value - Value to be written
207 * reg - GAS register structure
211 * DESCRIPTION: Write to either memory or IO space. This is a 32-bit max
212 * version of acpi_write, used internally since the overhead of
213 * 64-bit values is not needed.
215 ******************************************************************************/
217 acpi_status acpi_hw_write(u32 value, struct acpi_generic_address *reg)
222 ACPI_FUNCTION_NAME(hw_write);
224 /* Validate contents of the GAS register */
226 status = acpi_hw_validate_register(reg, 32, &address);
227 if (ACPI_FAILURE(status)) {
232 * Two address spaces supported: Memory or IO. PCI_Config is
233 * not supported here because the GAS structure is insufficient
235 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
236 status = acpi_os_write_memory((acpi_physical_address)
239 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
241 status = acpi_hw_write_port((acpi_io_address)
242 address, value, reg->bit_width);
245 ACPI_DEBUG_PRINT((ACPI_DB_IO,
246 "Wrote: %8.8X width %2d to %8.8X%8.8X (%s)\n",
247 value, reg->bit_width, ACPI_FORMAT_UINT64(address),
248 acpi_ut_get_region_name(reg->space_id)));
253 #if (!ACPI_REDUCED_HARDWARE)
254 /*******************************************************************************
256 * FUNCTION: acpi_hw_clear_acpi_status
262 * DESCRIPTION: Clears all fixed and general purpose status bits
264 ******************************************************************************/
266 acpi_status acpi_hw_clear_acpi_status(void)
269 acpi_cpu_flags lock_flags = 0;
271 ACPI_FUNCTION_TRACE(hw_clear_acpi_status);
273 ACPI_DEBUG_PRINT((ACPI_DB_IO, "About to write %04X to %8.8X%8.8X\n",
274 ACPI_BITMASK_ALL_FIXED_STATUS,
275 ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
277 lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
279 /* Clear the fixed events in PM1 A/B */
281 status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
282 ACPI_BITMASK_ALL_FIXED_STATUS);
284 acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
286 if (ACPI_FAILURE(status)) {
290 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
292 status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block, NULL);
295 return_ACPI_STATUS(status);
298 /*******************************************************************************
300 * FUNCTION: acpi_hw_get_bit_register_info
302 * PARAMETERS: register_id - Index of ACPI Register to access
304 * RETURN: The bitmask to be used when accessing the register
306 * DESCRIPTION: Map register_id into a register bitmask.
308 ******************************************************************************/
310 struct acpi_bit_register_info *acpi_hw_get_bit_register_info(u32 register_id)
312 ACPI_FUNCTION_ENTRY();
314 if (register_id > ACPI_BITREG_MAX) {
315 ACPI_ERROR((AE_INFO, "Invalid BitRegister ID: 0x%X",
320 return (&acpi_gbl_bit_register_info[register_id]);
323 /******************************************************************************
325 * FUNCTION: acpi_hw_write_pm1_control
327 * PARAMETERS: pm1a_control - Value to be written to PM1A control
328 * pm1b_control - Value to be written to PM1B control
332 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
333 * different than than the PM1 A/B status and enable registers
334 * in that different values can be written to the A/B registers.
335 * Most notably, the SLP_TYP bits can be different, as per the
336 * values returned from the _Sx predefined methods.
338 ******************************************************************************/
340 acpi_status acpi_hw_write_pm1_control(u32 pm1a_control, u32 pm1b_control)
344 ACPI_FUNCTION_TRACE(hw_write_pm1_control);
347 acpi_hw_write(pm1a_control, &acpi_gbl_FADT.xpm1a_control_block);
348 if (ACPI_FAILURE(status)) {
349 return_ACPI_STATUS(status);
352 if (acpi_gbl_FADT.xpm1b_control_block.address) {
354 acpi_hw_write(pm1b_control,
355 &acpi_gbl_FADT.xpm1b_control_block);
357 return_ACPI_STATUS(status);
360 /******************************************************************************
362 * FUNCTION: acpi_hw_register_read
364 * PARAMETERS: register_id - ACPI Register ID
365 * return_value - Where the register value is returned
367 * RETURN: Status and the value read.
369 * DESCRIPTION: Read from the specified ACPI register
371 ******************************************************************************/
372 acpi_status acpi_hw_register_read(u32 register_id, u32 *return_value)
377 ACPI_FUNCTION_TRACE(hw_register_read);
379 switch (register_id) {
380 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
382 status = acpi_hw_read_multiple(&value,
383 &acpi_gbl_xpm1a_status,
384 &acpi_gbl_xpm1b_status);
387 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
389 status = acpi_hw_read_multiple(&value,
390 &acpi_gbl_xpm1a_enable,
391 &acpi_gbl_xpm1b_enable);
394 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
396 status = acpi_hw_read_multiple(&value,
400 xpm1b_control_block);
403 * Zero the write-only bits. From the ACPI specification, "Hardware
404 * Write-Only Bits": "Upon reads to registers with write-only bits,
405 * software masks out all write-only bits."
407 value &= ~ACPI_PM1_CONTROL_WRITEONLY_BITS;
410 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
413 acpi_hw_read(&value, &acpi_gbl_FADT.xpm2_control_block);
416 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
418 status = acpi_hw_read(&value, &acpi_gbl_FADT.xpm_timer_block);
421 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
424 acpi_hw_read_port(acpi_gbl_FADT.smi_command, &value, 8);
429 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
430 status = AE_BAD_PARAMETER;
434 if (ACPI_SUCCESS(status)) {
435 *return_value = value;
438 return_ACPI_STATUS(status);
441 /******************************************************************************
443 * FUNCTION: acpi_hw_register_write
445 * PARAMETERS: register_id - ACPI Register ID
446 * value - The value to write
450 * DESCRIPTION: Write to the specified ACPI register
452 * NOTE: In accordance with the ACPI specification, this function automatically
453 * preserves the value of the following bits, meaning that these bits cannot be
454 * changed via this interface:
456 * PM1_CONTROL[0] = SCI_EN
461 * 1) Hardware Ignored Bits: When software writes to a register with ignored
462 * bit fields, it preserves the ignored bit fields
463 * 2) SCI_EN: OSPM always preserves this bit position
465 ******************************************************************************/
467 acpi_status acpi_hw_register_write(u32 register_id, u32 value)
472 ACPI_FUNCTION_TRACE(hw_register_write);
474 switch (register_id) {
475 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
477 * Handle the "ignored" bit in PM1 Status. According to the ACPI
478 * specification, ignored bits are to be preserved when writing.
479 * Normally, this would mean a read/modify/write sequence. However,
480 * preserving a bit in the status register is different. Writing a
481 * one clears the status, and writing a zero preserves the status.
482 * Therefore, we must always write zero to the ignored bit.
484 * This behavior is clarified in the ACPI 4.0 specification.
486 value &= ~ACPI_PM1_STATUS_PRESERVED_BITS;
488 status = acpi_hw_write_multiple(value,
489 &acpi_gbl_xpm1a_status,
490 &acpi_gbl_xpm1b_status);
493 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
495 status = acpi_hw_write_multiple(value,
496 &acpi_gbl_xpm1a_enable,
497 &acpi_gbl_xpm1b_enable);
500 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
502 * Perform a read first to preserve certain bits (per ACPI spec)
503 * Note: This includes SCI_EN, we never want to change this bit
505 status = acpi_hw_read_multiple(&read_value,
509 xpm1b_control_block);
510 if (ACPI_FAILURE(status)) {
514 /* Insert the bits to be preserved */
516 ACPI_INSERT_BITS(value, ACPI_PM1_CONTROL_PRESERVED_BITS,
519 /* Now we can write the data */
521 status = acpi_hw_write_multiple(value,
525 xpm1b_control_block);
528 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
530 * For control registers, all reserved bits must be preserved,
531 * as per the ACPI spec.
534 acpi_hw_read(&read_value,
535 &acpi_gbl_FADT.xpm2_control_block);
536 if (ACPI_FAILURE(status)) {
540 /* Insert the bits to be preserved */
542 ACPI_INSERT_BITS(value, ACPI_PM2_CONTROL_PRESERVED_BITS,
546 acpi_hw_write(value, &acpi_gbl_FADT.xpm2_control_block);
549 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
551 status = acpi_hw_write(value, &acpi_gbl_FADT.xpm_timer_block);
554 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
556 /* SMI_CMD is currently always in IO space */
559 acpi_hw_write_port(acpi_gbl_FADT.smi_command, value, 8);
564 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
565 status = AE_BAD_PARAMETER;
570 return_ACPI_STATUS(status);
573 /******************************************************************************
575 * FUNCTION: acpi_hw_read_multiple
577 * PARAMETERS: value - Where the register value is returned
578 * register_a - First ACPI register (required)
579 * register_b - Second ACPI register (optional)
583 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
585 ******************************************************************************/
588 acpi_hw_read_multiple(u32 *value,
589 struct acpi_generic_address *register_a,
590 struct acpi_generic_address *register_b)
596 /* The first register is always required */
598 status = acpi_hw_read(&value_a, register_a);
599 if (ACPI_FAILURE(status)) {
603 /* Second register is optional */
605 if (register_b->address) {
606 status = acpi_hw_read(&value_b, register_b);
607 if (ACPI_FAILURE(status)) {
613 * OR the two return values together. No shifting or masking is necessary,
614 * because of how the PM1 registers are defined in the ACPI specification:
616 * "Although the bits can be split between the two register blocks (each
617 * register block has a unique pointer within the FADT), the bit positions
618 * are maintained. The register block with unimplemented bits (that is,
619 * those implemented in the other register block) always returns zeros,
620 * and writes have no side effects"
622 *value = (value_a | value_b);
626 /******************************************************************************
628 * FUNCTION: acpi_hw_write_multiple
630 * PARAMETERS: value - The value to write
631 * register_a - First ACPI register (required)
632 * register_b - Second ACPI register (optional)
636 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
638 ******************************************************************************/
641 acpi_hw_write_multiple(u32 value,
642 struct acpi_generic_address *register_a,
643 struct acpi_generic_address *register_b)
647 /* The first register is always required */
649 status = acpi_hw_write(value, register_a);
650 if (ACPI_FAILURE(status)) {
655 * Second register is optional
657 * No bit shifting or clearing is necessary, because of how the PM1
658 * registers are defined in the ACPI specification:
660 * "Although the bits can be split between the two register blocks (each
661 * register block has a unique pointer within the FADT), the bit positions
662 * are maintained. The register block with unimplemented bits (that is,
663 * those implemented in the other register block) always returns zeros,
664 * and writes have no side effects"
666 if (register_b->address) {
667 status = acpi_hw_write(value, register_b);
673 #endif /* !ACPI_REDUCED_HARDWARE */