blob: 69603ba52a3accc41d71b35ac863d6d4c7498f0d [file] [log] [blame]
Erik Schmauss95857632018-03-14 16:13:07 -07001// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*******************************************************************************
3 *
4 * Module Name: hwregs - Read/write access functions for the various ACPI
5 * control and status registers.
6 *
7 ******************************************************************************/
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050010#include "accommon.h"
Len Browne2f7a772009-01-09 00:30:03 -050011#include "acevents.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#define _COMPONENT ACPI_HARDWARE
Len Brown4be44fc2005-08-05 00:44:28 -040014ACPI_MODULE_NAME("hwregs")
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Bob Moore33620c52012-02-14 18:14:27 +080016#if (!ACPI_REDUCED_HARDWARE)
Bob Moorec520aba2009-02-18 14:20:12 +080017/* Local Prototypes */
Lv Zhengb314a172016-05-05 12:58:39 +080018static u8
Lv Zheng04cf0532016-12-28 15:28:36 +080019acpi_hw_get_access_bit_width(u64 address,
20 struct acpi_generic_address *reg,
Lv Zhengb314a172016-05-05 12:58:39 +080021 u8 max_bit_width);
22
Bob Moorec520aba2009-02-18 14:20:12 +080023static acpi_status
24acpi_hw_read_multiple(u32 *value,
25 struct acpi_generic_address *register_a,
26 struct acpi_generic_address *register_b);
27
28static acpi_status
29acpi_hw_write_multiple(u32 value,
30 struct acpi_generic_address *register_a,
31 struct acpi_generic_address *register_b);
32
Bob Moore33620c52012-02-14 18:14:27 +080033#endif /* !ACPI_REDUCED_HARDWARE */
34
Bob Moorec6b57742009-06-24 09:44:06 +080035/******************************************************************************
36 *
Lv Zhengb314a172016-05-05 12:58:39 +080037 * FUNCTION: acpi_hw_get_access_bit_width
38 *
Lv Zheng04cf0532016-12-28 15:28:36 +080039 * PARAMETERS: address - GAS register address
40 * reg - GAS register structure
Lv Zhengb314a172016-05-05 12:58:39 +080041 * max_bit_width - Max bit_width supported (32 or 64)
42 *
43 * RETURN: Status
44 *
45 * DESCRIPTION: Obtain optimal access bit width
46 *
47 ******************************************************************************/
48
49static u8
Lv Zheng04cf0532016-12-28 15:28:36 +080050acpi_hw_get_access_bit_width(u64 address,
51 struct acpi_generic_address *reg, u8 max_bit_width)
Lv Zhengb314a172016-05-05 12:58:39 +080052{
Lv Zheng04cf0532016-12-28 15:28:36 +080053 u8 access_bit_width;
Lv Zheng7f9bef92016-06-01 11:03:20 +080054
Lv Zheng04cf0532016-12-28 15:28:36 +080055 /*
56 * GAS format "register", used by FADT:
57 * 1. Detected if bit_offset is 0 and bit_width is 8/16/32/64;
58 * 2. access_size field is ignored and bit_width field is used for
59 * determining the boundary of the IO accesses.
60 * GAS format "region", used by APEI registers:
61 * 1. Detected if bit_offset is not 0 or bit_width is not 8/16/32/64;
62 * 2. access_size field is used for determining the boundary of the
63 * IO accesses;
64 * 3. bit_offset/bit_width fields are used to describe the "region".
65 *
66 * Note: This algorithm assumes that the "Address" fields should always
67 * contain aligned values.
68 */
69 if (!reg->bit_offset && reg->bit_width &&
70 ACPI_IS_POWER_OF_TWO(reg->bit_width) &&
71 ACPI_IS_ALIGNED(reg->bit_width, 8)) {
72 access_bit_width = reg->bit_width;
73 } else if (reg->access_width) {
Lv Zheng4eebedd2017-08-03 14:26:19 +080074 access_bit_width = ACPI_ACCESS_BIT_WIDTH(reg->access_width);
Lv Zhengb314a172016-05-05 12:58:39 +080075 } else {
Lv Zheng04cf0532016-12-28 15:28:36 +080076 access_bit_width =
77 ACPI_ROUND_UP_POWER_OF_TWO_8(reg->bit_offset +
78 reg->bit_width);
79 if (access_bit_width <= 8) {
80 access_bit_width = 8;
81 } else {
82 while (!ACPI_IS_ALIGNED(address, access_bit_width >> 3)) {
83 access_bit_width >>= 1;
84 }
85 }
Lv Zhengb314a172016-05-05 12:58:39 +080086 }
Lv Zheng04cf0532016-12-28 15:28:36 +080087
88 /* Maximum IO port access bit width is 32 */
89
90 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
91 max_bit_width = 32;
92 }
93
94 /*
95 * Return access width according to the requested maximum access bit width,
96 * as the caller should know the format of the register and may enforce
97 * a 32-bit accesses.
98 */
99 if (access_bit_width < max_bit_width) {
100 return (access_bit_width);
101 }
102 return (max_bit_width);
Lv Zhengb314a172016-05-05 12:58:39 +0800103}
104
105/******************************************************************************
106 *
Bob Moorec6b57742009-06-24 09:44:06 +0800107 * FUNCTION: acpi_hw_validate_register
108 *
Bob Mooreba494be2012-07-12 09:40:10 +0800109 * PARAMETERS: reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800110 * max_bit_width - Max bit_width supported (32 or 64)
Bob Mooreba494be2012-07-12 09:40:10 +0800111 * address - Pointer to where the gas->address
Bob Moorec6b57742009-06-24 09:44:06 +0800112 * is returned
113 *
114 * RETURN: Status
115 *
116 * DESCRIPTION: Validate the contents of a GAS register. Checks the GAS
117 * pointer, Address, space_id, bit_width, and bit_offset.
118 *
119 ******************************************************************************/
120
121acpi_status
122acpi_hw_validate_register(struct acpi_generic_address *reg,
123 u8 max_bit_width, u64 *address)
124{
Lv Zheng920de6e2016-03-24 09:41:09 +0800125 u8 bit_width;
126 u8 access_width;
Bob Moorec6b57742009-06-24 09:44:06 +0800127
128 /* Must have a valid pointer to a GAS structure */
129
130 if (!reg) {
131 return (AE_BAD_PARAMETER);
132 }
133
134 /*
135 * Copy the target address. This handles possible alignment issues.
136 * Address must not be null. A null address also indicates an optional
137 * ACPI register that is not supported, so no error message.
138 */
139 ACPI_MOVE_64_TO_64(address, &reg->address);
140 if (!(*address)) {
141 return (AE_BAD_ADDRESS);
142 }
143
Bob Mooreba494be2012-07-12 09:40:10 +0800144 /* Validate the space_ID */
Bob Moorec6b57742009-06-24 09:44:06 +0800145
146 if ((reg->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY) &&
147 (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO)) {
148 ACPI_ERROR((AE_INFO,
149 "Unsupported address space: 0x%X", reg->space_id));
150 return (AE_SUPPORT);
151 }
152
Lv Zheng920de6e2016-03-24 09:41:09 +0800153 /* Validate the access_width */
Bob Moorec6b57742009-06-24 09:44:06 +0800154
Lv Zheng920de6e2016-03-24 09:41:09 +0800155 if (reg->access_width > 4) {
Bob Moorec6b57742009-06-24 09:44:06 +0800156 ACPI_ERROR((AE_INFO,
Lv Zheng920de6e2016-03-24 09:41:09 +0800157 "Unsupported register access width: 0x%X",
158 reg->access_width));
Bob Moorec6b57742009-06-24 09:44:06 +0800159 return (AE_SUPPORT);
160 }
161
Lv Zheng920de6e2016-03-24 09:41:09 +0800162 /* Validate the bit_width, convert access_width into number of bits */
Bob Moorec6b57742009-06-24 09:44:06 +0800163
Lv Zheng04cf0532016-12-28 15:28:36 +0800164 access_width =
165 acpi_hw_get_access_bit_width(*address, reg, max_bit_width);
Lv Zheng920de6e2016-03-24 09:41:09 +0800166 bit_width =
167 ACPI_ROUND_UP(reg->bit_offset + reg->bit_width, access_width);
168 if (max_bit_width < bit_width) {
Bob Moorec6b57742009-06-24 09:44:06 +0800169 ACPI_WARNING((AE_INFO,
Lv Zheng920de6e2016-03-24 09:41:09 +0800170 "Requested bit width 0x%X is smaller than register bit width 0x%X",
171 max_bit_width, bit_width));
172 return (AE_SUPPORT);
Bob Moorec6b57742009-06-24 09:44:06 +0800173 }
174
175 return (AE_OK);
176}
177
178/******************************************************************************
179 *
180 * FUNCTION: acpi_hw_read
181 *
Bob Mooreba494be2012-07-12 09:40:10 +0800182 * PARAMETERS: value - Where the value is returned
183 * reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800184 *
185 * RETURN: Status
186 *
Lv Zheng8381c542017-09-20 10:00:11 +0800187 * DESCRIPTION: Read from either memory or IO space. This is a 64-bit max
188 * version of acpi_read.
Bob Moorec6b57742009-06-24 09:44:06 +0800189 *
190 * LIMITATIONS: <These limitations also apply to acpi_hw_write>
Bob Mooreba494be2012-07-12 09:40:10 +0800191 * space_ID must be system_memory or system_IO.
Bob Moorec6b57742009-06-24 09:44:06 +0800192 *
193 ******************************************************************************/
194
Lv Zheng8381c542017-09-20 10:00:11 +0800195acpi_status acpi_hw_read(u64 *value, struct acpi_generic_address *reg)
Bob Moorec6b57742009-06-24 09:44:06 +0800196{
197 u64 address;
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800198 u8 access_width;
199 u32 bit_width;
200 u8 bit_offset;
Bob Moore653f4b52012-02-14 18:29:55 +0800201 u64 value64;
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800202 u32 value32;
203 u8 index;
Bob Moorec6b57742009-06-24 09:44:06 +0800204 acpi_status status;
205
206 ACPI_FUNCTION_NAME(hw_read);
207
208 /* Validate contents of the GAS register */
209
Lv Zheng8381c542017-09-20 10:00:11 +0800210 status = acpi_hw_validate_register(reg, 64, &address);
Bob Moorec6b57742009-06-24 09:44:06 +0800211 if (ACPI_FAILURE(status)) {
212 return (status);
213 }
214
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800215 /*
Lv Zheng8381c542017-09-20 10:00:11 +0800216 * Initialize entire 64-bit return value to zero, convert access_width
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800217 * into number of bits based
218 */
Bob Moorec6b57742009-06-24 09:44:06 +0800219 *value = 0;
Lv Zheng8381c542017-09-20 10:00:11 +0800220 access_width = acpi_hw_get_access_bit_width(address, reg, 64);
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800221 bit_width = reg->bit_offset + reg->bit_width;
222 bit_offset = reg->bit_offset;
Bob Moorec6b57742009-06-24 09:44:06 +0800223
224 /*
225 * Two address spaces supported: Memory or IO. PCI_Config is
226 * not supported here because the GAS structure is insufficient
227 */
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800228 index = 0;
229 while (bit_width) {
230 if (bit_offset >= access_width) {
Lv Zheng8381c542017-09-20 10:00:11 +0800231 value64 = 0;
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800232 bit_offset -= access_width;
233 } else {
234 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
235 status =
236 acpi_os_read_memory((acpi_physical_address)
237 address +
238 index *
239 ACPI_DIV_8
240 (access_width),
241 &value64, access_width);
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800242 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
Bob Moore653f4b52012-02-14 18:29:55 +0800243
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800244 status = acpi_hw_read_port((acpi_io_address)
245 address +
246 index *
247 ACPI_DIV_8
248 (access_width),
249 &value32,
250 access_width);
Lv Zheng8381c542017-09-20 10:00:11 +0800251 value64 = (u64)value32;
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800252 }
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800253 }
254
255 /*
256 * Use offset style bit writes because "Index * AccessWidth" is
Lv Zheng8381c542017-09-20 10:00:11 +0800257 * ensured to be less than 64-bits by acpi_hw_validate_register().
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800258 */
259 ACPI_SET_BITS(value, index * access_width,
Lv Zheng8381c542017-09-20 10:00:11 +0800260 ACPI_MASK_BITS_ABOVE_64(access_width), value64);
Lv Zhengc3bc26d2016-05-05 12:58:52 +0800261
262 bit_width -=
263 bit_width > access_width ? access_width : bit_width;
264 index++;
Bob Moorec6b57742009-06-24 09:44:06 +0800265 }
266
267 ACPI_DEBUG_PRINT((ACPI_DB_IO,
Lv Zheng8381c542017-09-20 10:00:11 +0800268 "Read: %8.8X%8.8X width %2d from %8.8X%8.8X (%s)\n",
269 ACPI_FORMAT_UINT64(*value), access_width,
270 ACPI_FORMAT_UINT64(address),
Bob Moorec6b57742009-06-24 09:44:06 +0800271 acpi_ut_get_region_name(reg->space_id)));
272
273 return (status);
274}
275
276/******************************************************************************
277 *
278 * FUNCTION: acpi_hw_write
279 *
Bob Mooreba494be2012-07-12 09:40:10 +0800280 * PARAMETERS: value - Value to be written
281 * reg - GAS register structure
Bob Moorec6b57742009-06-24 09:44:06 +0800282 *
283 * RETURN: Status
284 *
Lv Zheng8381c542017-09-20 10:00:11 +0800285 * DESCRIPTION: Write to either memory or IO space. This is a 64-bit max
286 * version of acpi_write.
Bob Moorec6b57742009-06-24 09:44:06 +0800287 *
288 ******************************************************************************/
289
Lv Zheng8381c542017-09-20 10:00:11 +0800290acpi_status acpi_hw_write(u64 value, struct acpi_generic_address *reg)
Bob Moorec6b57742009-06-24 09:44:06 +0800291{
292 u64 address;
Lv Zhengdc4c7372016-12-28 15:28:23 +0800293 u8 access_width;
294 u32 bit_width;
295 u8 bit_offset;
296 u64 value64;
Lv Zhengdc4c7372016-12-28 15:28:23 +0800297 u8 index;
Bob Moorec6b57742009-06-24 09:44:06 +0800298 acpi_status status;
299
300 ACPI_FUNCTION_NAME(hw_write);
301
302 /* Validate contents of the GAS register */
303
Lv Zheng8381c542017-09-20 10:00:11 +0800304 status = acpi_hw_validate_register(reg, 64, &address);
Bob Moorec6b57742009-06-24 09:44:06 +0800305 if (ACPI_FAILURE(status)) {
306 return (status);
307 }
308
Lv Zhengdc4c7372016-12-28 15:28:23 +0800309 /* Convert access_width into number of bits based */
310
Lv Zheng8381c542017-09-20 10:00:11 +0800311 access_width = acpi_hw_get_access_bit_width(address, reg, 64);
Lv Zhengdc4c7372016-12-28 15:28:23 +0800312 bit_width = reg->bit_offset + reg->bit_width;
313 bit_offset = reg->bit_offset;
314
Bob Moorec6b57742009-06-24 09:44:06 +0800315 /*
316 * Two address spaces supported: Memory or IO. PCI_Config is
317 * not supported here because the GAS structure is insufficient
318 */
Lv Zhengdc4c7372016-12-28 15:28:23 +0800319 index = 0;
320 while (bit_width) {
321 /*
322 * Use offset style bit reads because "Index * AccessWidth" is
Lv Zheng8381c542017-09-20 10:00:11 +0800323 * ensured to be less than 64-bits by acpi_hw_validate_register().
Lv Zhengdc4c7372016-12-28 15:28:23 +0800324 */
Lv Zheng8381c542017-09-20 10:00:11 +0800325 value64 = ACPI_GET_BITS(&value, index * access_width,
326 ACPI_MASK_BITS_ABOVE_64(access_width));
Bob Moorec6b57742009-06-24 09:44:06 +0800327
Lv Zhengdc4c7372016-12-28 15:28:23 +0800328 if (bit_offset >= access_width) {
329 bit_offset -= access_width;
330 } else {
331 if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
Lv Zhengdc4c7372016-12-28 15:28:23 +0800332 status =
333 acpi_os_write_memory((acpi_physical_address)
334 address +
335 index *
336 ACPI_DIV_8
337 (access_width),
338 value64, access_width);
339 } else { /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
340
341 status = acpi_hw_write_port((acpi_io_address)
342 address +
343 index *
344 ACPI_DIV_8
345 (access_width),
Lv Zheng8381c542017-09-20 10:00:11 +0800346 (u32)value64,
Lv Zhengdc4c7372016-12-28 15:28:23 +0800347 access_width);
348 }
349 }
350
351 /*
352 * Index * access_width is ensured to be less than 32-bits by
353 * acpi_hw_validate_register().
354 */
355 bit_width -=
356 bit_width > access_width ? access_width : bit_width;
357 index++;
Bob Moorec6b57742009-06-24 09:44:06 +0800358 }
359
360 ACPI_DEBUG_PRINT((ACPI_DB_IO,
Lv Zheng8381c542017-09-20 10:00:11 +0800361 "Wrote: %8.8X%8.8X width %2d to %8.8X%8.8X (%s)\n",
362 ACPI_FORMAT_UINT64(value), access_width,
363 ACPI_FORMAT_UINT64(address),
Bob Moorec6b57742009-06-24 09:44:06 +0800364 acpi_ut_get_region_name(reg->space_id)));
365
366 return (status);
367}
368
Bob Moore33620c52012-02-14 18:14:27 +0800369#if (!ACPI_REDUCED_HARDWARE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370/*******************************************************************************
371 *
372 * FUNCTION: acpi_hw_clear_acpi_status
373 *
Bob Moored8c71b62007-02-02 19:48:21 +0300374 * PARAMETERS: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 *
Bob Moore7db5d822008-12-30 11:04:48 +0800376 * RETURN: Status
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *
378 * DESCRIPTION: Clears all fixed and general purpose status bits
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 *
380 ******************************************************************************/
Bob Moorec520aba2009-02-18 14:20:12 +0800381
Bob Moored8c71b62007-02-02 19:48:21 +0300382acpi_status acpi_hw_clear_acpi_status(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Len Brown4be44fc2005-08-05 00:44:28 -0400384 acpi_status status;
Bob Moore4c90ece2006-06-08 16:29:00 -0400385 acpi_cpu_flags lock_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Bob Mooreb229cf92006-04-21 17:15:00 -0400387 ACPI_FUNCTION_TRACE(hw_clear_acpi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Bob Moore8eb7b242009-04-22 10:28:22 +0800389 ACPI_DEBUG_PRINT((ACPI_DB_IO, "About to write %04X to %8.8X%8.8X\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400390 ACPI_BITMASK_ALL_FIXED_STATUS,
Bob Moore8eb7b242009-04-22 10:28:22 +0800391 ACPI_FORMAT_UINT64(acpi_gbl_xpm1a_status.address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Steven Rostedtc57c0ad2018-04-25 16:28:27 +0200393 lock_flags = acpi_os_acquire_raw_lock(acpi_gbl_hardware_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Bob Moore227243a2009-02-18 14:24:50 +0800395 /* Clear the fixed events in PM1 A/B */
Bob Moore531c6332009-02-18 14:06:12 +0800396
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400397 status = acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
Len Brown4be44fc2005-08-05 00:44:28 -0400398 ACPI_BITMASK_ALL_FIXED_STATUS);
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600399
Steven Rostedtc57c0ad2018-04-25 16:28:27 +0200400 acpi_os_release_raw_lock(acpi_gbl_hardware_lock, lock_flags);
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600401
Lv Zheng7d3e83b2014-07-08 10:08:19 +0800402 if (ACPI_FAILURE(status)) {
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600403 goto exit;
Lv Zheng7d3e83b2014-07-08 10:08:19 +0800404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /* Clear the GPE Bits in all GPE registers in all GPE blocks */
407
Bob Mooree97d6bf2008-12-30 09:45:17 +0800408 status = acpi_ev_walk_gpe_list(acpi_hw_clear_gpe_block, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Rakib Mullickf7f71cf2011-11-06 21:18:17 +0600410exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400411 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/*******************************************************************************
415 *
Bob Moore33620c52012-02-14 18:14:27 +0800416 * FUNCTION: acpi_hw_get_bit_register_info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 *
418 * PARAMETERS: register_id - Index of ACPI Register to access
419 *
Robert Moore44f6c012005-04-18 22:49:35 -0400420 * RETURN: The bitmask to be used when accessing the register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 *
Robert Moore44f6c012005-04-18 22:49:35 -0400422 * DESCRIPTION: Map register_id into a register bitmask.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 *
424 ******************************************************************************/
Bob Moore7db5d822008-12-30 11:04:48 +0800425
Len Brown4be44fc2005-08-05 00:44:28 -0400426struct acpi_bit_register_info *acpi_hw_get_bit_register_info(u32 register_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Bob Moore4a90c7e2006-01-13 16:22:00 -0500428 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
430 if (register_id > ACPI_BITREG_MAX) {
Bob Mooref6a22b02010-03-05 17:56:40 +0800431 ACPI_ERROR((AE_INFO, "Invalid BitRegister ID: 0x%X",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500432 register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return (NULL);
434 }
435
436 return (&acpi_gbl_bit_register_info[register_id]);
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/******************************************************************************
440 *
Bob Moore32c9ef92009-02-18 14:36:05 +0800441 * FUNCTION: acpi_hw_write_pm1_control
442 *
443 * PARAMETERS: pm1a_control - Value to be written to PM1A control
444 * pm1b_control - Value to be written to PM1B control
445 *
446 * RETURN: Status
447 *
448 * DESCRIPTION: Write the PM1 A/B control registers. These registers are
449 * different than than the PM1 A/B status and enable registers
450 * in that different values can be written to the A/B registers.
451 * Most notably, the SLP_TYP bits can be different, as per the
452 * values returned from the _Sx predefined methods.
453 *
454 ******************************************************************************/
455
456acpi_status acpi_hw_write_pm1_control(u32 pm1a_control, u32 pm1b_control)
457{
458 acpi_status status;
459
460 ACPI_FUNCTION_TRACE(hw_write_pm1_control);
461
Bob Moorec6b57742009-06-24 09:44:06 +0800462 status =
463 acpi_hw_write(pm1a_control, &acpi_gbl_FADT.xpm1a_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800464 if (ACPI_FAILURE(status)) {
465 return_ACPI_STATUS(status);
466 }
467
468 if (acpi_gbl_FADT.xpm1b_control_block.address) {
469 status =
Bob Moorec6b57742009-06-24 09:44:06 +0800470 acpi_hw_write(pm1b_control,
471 &acpi_gbl_FADT.xpm1b_control_block);
Bob Moore32c9ef92009-02-18 14:36:05 +0800472 }
473 return_ACPI_STATUS(status);
474}
475
476/******************************************************************************
477 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 * FUNCTION: acpi_hw_register_read
479 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400480 * PARAMETERS: register_id - ACPI Register ID
Robert Moore44f6c012005-04-18 22:49:35 -0400481 * return_value - Where the register value is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *
483 * RETURN: Status and the value read.
484 *
Bob Moore967440e32006-06-23 17:04:00 -0400485 * DESCRIPTION: Read from the specified ACPI register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 *
487 ******************************************************************************/
Lv Zheng3e8214e2012-12-19 05:37:15 +0000488acpi_status acpi_hw_register_read(u32 register_id, u32 *return_value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
Bob Moorec520aba2009-02-18 14:20:12 +0800490 u32 value = 0;
Lv Zheng8381c542017-09-20 10:00:11 +0800491 u64 value64;
Len Brown4be44fc2005-08-05 00:44:28 -0400492 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Bob Mooreb229cf92006-04-21 17:15:00 -0400494 ACPI_FUNCTION_TRACE(hw_register_read);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800497 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Bob Moorec520aba2009-02-18 14:20:12 +0800499 status = acpi_hw_read_multiple(&value,
500 &acpi_gbl_xpm1a_status,
501 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 break;
503
Bob Moorec520aba2009-02-18 14:20:12 +0800504 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Bob Moorec520aba2009-02-18 14:20:12 +0800506 status = acpi_hw_read_multiple(&value,
507 &acpi_gbl_xpm1a_enable,
508 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 break;
510
Bob Moorec520aba2009-02-18 14:20:12 +0800511 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
Bob Moorec520aba2009-02-18 14:20:12 +0800513 status = acpi_hw_read_multiple(&value,
514 &acpi_gbl_FADT.
515 xpm1a_control_block,
516 &acpi_gbl_FADT.
517 xpm1b_control_block);
Lin Mingc3dd25f2009-03-19 09:51:01 +0800518
519 /*
520 * Zero the write-only bits. From the ACPI specification, "Hardware
521 * Write-Only Bits": "Upon reads to registers with write-only bits,
522 * software masks out all write-only bits."
523 */
524 value &= ~ACPI_PM1_CONTROL_WRITEONLY_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
526
Len Brown4be44fc2005-08-05 00:44:28 -0400527 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Bob Moorec6b57742009-06-24 09:44:06 +0800529 status =
Lv Zheng8381c542017-09-20 10:00:11 +0800530 acpi_hw_read(&value64, &acpi_gbl_FADT.xpm2_control_block);
Erik Schmaussf016b192018-08-10 14:42:55 -0700531 if (ACPI_SUCCESS(status)) {
532 value = (u32)value64;
533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 break;
535
Len Brown4be44fc2005-08-05 00:44:28 -0400536 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Lv Zheng8381c542017-09-20 10:00:11 +0800538 status = acpi_hw_read(&value64, &acpi_gbl_FADT.xpm_timer_block);
Erik Schmaussf016b192018-08-10 14:42:55 -0700539 if (ACPI_SUCCESS(status)) {
540 value = (u32)value64;
541 }
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544
Len Brown4be44fc2005-08-05 00:44:28 -0400545 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Bob Mooref3d2e782007-02-02 19:48:18 +0300547 status =
Bob Moore7f071902009-03-19 09:37:47 +0800548 acpi_hw_read_port(acpi_gbl_FADT.smi_command, &value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 break;
550
551 default:
Chao Guan1d1ea1b72013-06-08 00:58:14 +0000552
Bob Mooref6a22b02010-03-05 17:56:40 +0800553 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 status = AE_BAD_PARAMETER;
555 break;
556 }
557
Len Brown4be44fc2005-08-05 00:44:28 -0400558 if (ACPI_SUCCESS(status)) {
Lv Zheng8381c542017-09-20 10:00:11 +0800559 *return_value = (u32)value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561
Len Brown4be44fc2005-08-05 00:44:28 -0400562 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563}
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565/******************************************************************************
566 *
567 * FUNCTION: acpi_hw_register_write
568 *
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400569 * PARAMETERS: register_id - ACPI Register ID
Bob Mooreba494be2012-07-12 09:40:10 +0800570 * value - The value to write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 *
572 * RETURN: Status
573 *
Bob Moore967440e32006-06-23 17:04:00 -0400574 * DESCRIPTION: Write to the specified ACPI register
575 *
576 * NOTE: In accordance with the ACPI specification, this function automatically
577 * preserves the value of the following bits, meaning that these bits cannot be
578 * changed via this interface:
579 *
580 * PM1_CONTROL[0] = SCI_EN
581 * PM1_CONTROL[9]
582 * PM1_STATUS[11]
583 *
584 * ACPI References:
585 * 1) Hardware Ignored Bits: When software writes to a register with ignored
586 * bit fields, it preserves the ignored bit fields
587 * 2) SCI_EN: OSPM always preserves this bit position
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 *
589 ******************************************************************************/
590
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400591acpi_status acpi_hw_register_write(u32 register_id, u32 value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Len Brown4be44fc2005-08-05 00:44:28 -0400593 acpi_status status;
Bob Moore967440e32006-06-23 17:04:00 -0400594 u32 read_value;
Lv Zheng8381c542017-09-20 10:00:11 +0800595 u64 read_value64;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Bob Mooreb229cf92006-04-21 17:15:00 -0400597 ACPI_FUNCTION_TRACE(hw_register_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 switch (register_id) {
Bob Moorec520aba2009-02-18 14:20:12 +0800600 case ACPI_REGISTER_PM1_STATUS: /* PM1 A/B: 16-bit access each */
Bob Moore8636f8d2009-03-09 16:32:20 +0800601 /*
602 * Handle the "ignored" bit in PM1 Status. According to the ACPI
603 * specification, ignored bits are to be preserved when writing.
604 * Normally, this would mean a read/modify/write sequence. However,
605 * preserving a bit in the status register is different. Writing a
606 * one clears the status, and writing a zero preserves the status.
607 * Therefore, we must always write zero to the ignored bit.
608 *
609 * This behavior is clarified in the ACPI 4.0 specification.
610 */
611 value &= ~ACPI_PM1_STATUS_PRESERVED_BITS;
Bob Moore967440e32006-06-23 17:04:00 -0400612
Bob Moorec520aba2009-02-18 14:20:12 +0800613 status = acpi_hw_write_multiple(value,
614 &acpi_gbl_xpm1a_status,
615 &acpi_gbl_xpm1b_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 break;
617
Lv Zheng75c80442012-12-19 05:36:49 +0000618 case ACPI_REGISTER_PM1_ENABLE: /* PM1 A/B: 16-bit access each */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Bob Moorec520aba2009-02-18 14:20:12 +0800620 status = acpi_hw_write_multiple(value,
621 &acpi_gbl_xpm1a_enable,
622 &acpi_gbl_xpm1b_enable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 break;
624
Bob Moorec520aba2009-02-18 14:20:12 +0800625 case ACPI_REGISTER_PM1_CONTROL: /* PM1 A/B: 16-bit access each */
Bob Moore967440e32006-06-23 17:04:00 -0400626 /*
627 * Perform a read first to preserve certain bits (per ACPI spec)
Bob Moorec520aba2009-02-18 14:20:12 +0800628 * Note: This includes SCI_EN, we never want to change this bit
Bob Moore967440e32006-06-23 17:04:00 -0400629 */
Bob Moorec520aba2009-02-18 14:20:12 +0800630 status = acpi_hw_read_multiple(&read_value,
631 &acpi_gbl_FADT.
632 xpm1a_control_block,
633 &acpi_gbl_FADT.
634 xpm1b_control_block);
Bob Moore967440e32006-06-23 17:04:00 -0400635 if (ACPI_FAILURE(status)) {
Alexey Starikovskiyd30dc9ab2007-09-30 22:39:36 +0400636 goto exit;
Bob Moore967440e32006-06-23 17:04:00 -0400637 }
638
639 /* Insert the bits to be preserved */
640
641 ACPI_INSERT_BITS(value, ACPI_PM1_CONTROL_PRESERVED_BITS,
642 read_value);
643
644 /* Now we can write the data */
645
Bob Moorec520aba2009-02-18 14:20:12 +0800646 status = acpi_hw_write_multiple(value,
647 &acpi_gbl_FADT.
648 xpm1a_control_block,
649 &acpi_gbl_FADT.
650 xpm1b_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 break;
652
Len Brown4be44fc2005-08-05 00:44:28 -0400653 case ACPI_REGISTER_PM2_CONTROL: /* 8-bit access */
Bob Moore20869dc2009-03-13 09:10:46 +0800654 /*
655 * For control registers, all reserved bits must be preserved,
656 * as per the ACPI spec.
657 */
658 status =
Lv Zheng8381c542017-09-20 10:00:11 +0800659 acpi_hw_read(&read_value64,
Bob Moorec6b57742009-06-24 09:44:06 +0800660 &acpi_gbl_FADT.xpm2_control_block);
Bob Moore20869dc2009-03-13 09:10:46 +0800661 if (ACPI_FAILURE(status)) {
662 goto exit;
663 }
Lv Zheng8381c542017-09-20 10:00:11 +0800664 read_value = (u32)read_value64;
Bob Moore20869dc2009-03-13 09:10:46 +0800665
666 /* Insert the bits to be preserved */
667
668 ACPI_INSERT_BITS(value, ACPI_PM2_CONTROL_PRESERVED_BITS,
669 read_value);
670
Bob Moorec6b57742009-06-24 09:44:06 +0800671 status =
672 acpi_hw_write(value, &acpi_gbl_FADT.xpm2_control_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 break;
674
Len Brown4be44fc2005-08-05 00:44:28 -0400675 case ACPI_REGISTER_PM_TIMER: /* 32-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Bob Moorec6b57742009-06-24 09:44:06 +0800677 status = acpi_hw_write(value, &acpi_gbl_FADT.xpm_timer_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 break;
679
Len Brown4be44fc2005-08-05 00:44:28 -0400680 case ACPI_REGISTER_SMI_COMMAND_BLOCK: /* 8-bit access */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* SMI_CMD is currently always in IO space */
683
Bob Mooref3d2e782007-02-02 19:48:18 +0300684 status =
Bob Moore7f071902009-03-19 09:37:47 +0800685 acpi_hw_write_port(acpi_gbl_FADT.smi_command, value, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 break;
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 default:
Chao Guan1d1ea1b72013-06-08 00:58:14 +0000689
Bob Mooref6a22b02010-03-05 17:56:40 +0800690 ACPI_ERROR((AE_INFO, "Unknown Register ID: 0x%X", register_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 status = AE_BAD_PARAMETER;
692 break;
693 }
694
Lv Zheng10622bf2013-10-29 09:30:02 +0800695exit:
Len Brown4be44fc2005-08-05 00:44:28 -0400696 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
Bob Moorec520aba2009-02-18 14:20:12 +0800698
699/******************************************************************************
700 *
701 * FUNCTION: acpi_hw_read_multiple
702 *
Bob Mooreba494be2012-07-12 09:40:10 +0800703 * PARAMETERS: value - Where the register value is returned
Bob Moorec520aba2009-02-18 14:20:12 +0800704 * register_a - First ACPI register (required)
705 * register_b - Second ACPI register (optional)
706 *
707 * RETURN: Status
708 *
709 * DESCRIPTION: Read from the specified two-part ACPI register (such as PM1 A/B)
710 *
711 ******************************************************************************/
712
713static acpi_status
714acpi_hw_read_multiple(u32 *value,
715 struct acpi_generic_address *register_a,
716 struct acpi_generic_address *register_b)
717{
718 u32 value_a = 0;
719 u32 value_b = 0;
Lv Zheng8381c542017-09-20 10:00:11 +0800720 u64 value64;
Bob Moorec520aba2009-02-18 14:20:12 +0800721 acpi_status status;
722
723 /* The first register is always required */
724
Lv Zheng8381c542017-09-20 10:00:11 +0800725 status = acpi_hw_read(&value64, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800726 if (ACPI_FAILURE(status)) {
727 return (status);
728 }
Lv Zheng8381c542017-09-20 10:00:11 +0800729 value_a = (u32)value64;
Bob Moorec520aba2009-02-18 14:20:12 +0800730
731 /* Second register is optional */
732
733 if (register_b->address) {
Lv Zheng8381c542017-09-20 10:00:11 +0800734 status = acpi_hw_read(&value64, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800735 if (ACPI_FAILURE(status)) {
736 return (status);
737 }
Lv Zheng8381c542017-09-20 10:00:11 +0800738 value_b = (u32)value64;
Bob Moorec520aba2009-02-18 14:20:12 +0800739 }
740
Bob Mooreaefc7f92009-02-18 14:26:02 +0800741 /*
742 * OR the two return values together. No shifting or masking is necessary,
743 * because of how the PM1 registers are defined in the ACPI specification:
744 *
745 * "Although the bits can be split between the two register blocks (each
746 * register block has a unique pointer within the FADT), the bit positions
747 * are maintained. The register block with unimplemented bits (that is,
748 * those implemented in the other register block) always returns zeros,
749 * and writes have no side effects"
750 */
751 *value = (value_a | value_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800752 return (AE_OK);
753}
754
755/******************************************************************************
756 *
757 * FUNCTION: acpi_hw_write_multiple
758 *
Bob Mooreba494be2012-07-12 09:40:10 +0800759 * PARAMETERS: value - The value to write
Bob Moorec520aba2009-02-18 14:20:12 +0800760 * register_a - First ACPI register (required)
761 * register_b - Second ACPI register (optional)
762 *
763 * RETURN: Status
764 *
765 * DESCRIPTION: Write to the specified two-part ACPI register (such as PM1 A/B)
766 *
767 ******************************************************************************/
768
769static acpi_status
770acpi_hw_write_multiple(u32 value,
771 struct acpi_generic_address *register_a,
772 struct acpi_generic_address *register_b)
773{
774 acpi_status status;
775
776 /* The first register is always required */
777
Bob Moorec6b57742009-06-24 09:44:06 +0800778 status = acpi_hw_write(value, register_a);
Bob Moorec520aba2009-02-18 14:20:12 +0800779 if (ACPI_FAILURE(status)) {
780 return (status);
781 }
782
Bob Mooreaefc7f92009-02-18 14:26:02 +0800783 /*
784 * Second register is optional
785 *
786 * No bit shifting or clearing is necessary, because of how the PM1
787 * registers are defined in the ACPI specification:
788 *
789 * "Although the bits can be split between the two register blocks (each
790 * register block has a unique pointer within the FADT), the bit positions
791 * are maintained. The register block with unimplemented bits (that is,
792 * those implemented in the other register block) always returns zeros,
793 * and writes have no side effects"
794 */
Bob Moorec520aba2009-02-18 14:20:12 +0800795 if (register_b->address) {
Bob Moorec6b57742009-06-24 09:44:06 +0800796 status = acpi_hw_write(value, register_b);
Bob Moorec520aba2009-02-18 14:20:12 +0800797 }
798
799 return (status);
800}
Bob Moore33620c52012-02-14 18:14:27 +0800801
802#endif /* !ACPI_REDUCED_HARDWARE */