]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/acpi/acpica/hwxface.c
Merge remote-tracking branch 'wireless-next/master'
[karo-tx-linux.git] / drivers / acpi / acpica / hwxface.c
1 /******************************************************************************
2  *
3  * Module Name: hwxface - Public ACPICA hardware interfaces
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2013, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <linux/export.h>
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47 #include "acnamesp.h"
48
49 #define _COMPONENT          ACPI_HARDWARE
50 ACPI_MODULE_NAME("hwxface")
51
52 /******************************************************************************
53  *
54  * 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  ******************************************************************************/
65 acpi_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         if (reset_reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
82                 /*
83                  * For I/O space, write directly to the OSL. This bypasses the port
84                  * validation mechanism, which may block a valid write to the reset
85                  * register.
86                  * Spec section 4.7.3.6 requires register width to be 8.
87                  */
88                 status =
89                     acpi_os_write_port((acpi_io_address) reset_reg->address,
90                                        acpi_gbl_FADT.reset_value, 8);
91         } else {
92                 /* Write the reset value to the reset register */
93
94                 status = acpi_hw_write(acpi_gbl_FADT.reset_value, reset_reg);
95         }
96
97         return_ACPI_STATUS(status);
98 }
99
100 ACPI_EXPORT_SYMBOL(acpi_reset)
101
102 /******************************************************************************
103  *
104  * FUNCTION:    acpi_read
105  *
106  * PARAMETERS:  value               - Where the value is returned
107  *              reg                 - GAS register structure
108  *
109  * RETURN:      Status
110  *
111  * DESCRIPTION: Read from either memory or IO space.
112  *
113  * LIMITATIONS: <These limitations also apply to acpi_write>
114  *      bit_width must be exactly 8, 16, 32, or 64.
115  *      space_ID must be system_memory or system_IO.
116  *      bit_offset and access_width are currently ignored, as there has
117  *          not been a need to implement these.
118  *
119  ******************************************************************************/
120 acpi_status acpi_read(u64 *return_value, struct acpi_generic_address *reg)
121 {
122         u32 value_lo;
123         u32 value_hi;
124         u32 width;
125         u64 address;
126         acpi_status status;
127
128         ACPI_FUNCTION_NAME(acpi_read);
129
130         if (!return_value) {
131                 return (AE_BAD_PARAMETER);
132         }
133
134         /* Validate contents of the GAS register. Allow 64-bit transfers */
135
136         status = acpi_hw_validate_register(reg, 64, &address);
137         if (ACPI_FAILURE(status)) {
138                 return (status);
139         }
140
141         /*
142          * Two address spaces supported: Memory or I/O. PCI_Config is
143          * not supported here because the GAS structure is insufficient
144          */
145         if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
146                 status = acpi_os_read_memory((acpi_physical_address)
147                                              address, return_value,
148                                              reg->bit_width);
149                 if (ACPI_FAILURE(status)) {
150                         return (status);
151                 }
152         } else {                /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
153
154                 value_lo = 0;
155                 value_hi = 0;
156
157                 width = reg->bit_width;
158                 if (width == 64) {
159                         width = 32;     /* Break into two 32-bit transfers */
160                 }
161
162                 status = acpi_hw_read_port((acpi_io_address)
163                                            address, &value_lo, width);
164                 if (ACPI_FAILURE(status)) {
165                         return (status);
166                 }
167
168                 if (reg->bit_width == 64) {
169
170                         /* Read the top 32 bits */
171
172                         status = acpi_hw_read_port((acpi_io_address)
173                                                    (address + 4), &value_hi,
174                                                    32);
175                         if (ACPI_FAILURE(status)) {
176                                 return (status);
177                         }
178                 }
179
180                 /* Set the return value only if status is AE_OK */
181
182                 *return_value = (value_lo | ((u64)value_hi << 32));
183         }
184
185         ACPI_DEBUG_PRINT((ACPI_DB_IO,
186                           "Read:  %8.8X%8.8X width %2d from %8.8X%8.8X (%s)\n",
187                           ACPI_FORMAT_UINT64(*return_value), reg->bit_width,
188                           ACPI_FORMAT_UINT64(address),
189                           acpi_ut_get_region_name(reg->space_id)));
190
191         return (AE_OK);
192 }
193
194 ACPI_EXPORT_SYMBOL(acpi_read)
195
196 /******************************************************************************
197  *
198  * FUNCTION:    acpi_write
199  *
200  * PARAMETERS:  value               - Value to be written
201  *              reg                 - GAS register structure
202  *
203  * RETURN:      Status
204  *
205  * DESCRIPTION: Write to either memory or IO space.
206  *
207  ******************************************************************************/
208 acpi_status acpi_write(u64 value, struct acpi_generic_address *reg)
209 {
210         u32 width;
211         u64 address;
212         acpi_status status;
213
214         ACPI_FUNCTION_NAME(acpi_write);
215
216         /* Validate contents of the GAS register. Allow 64-bit transfers */
217
218         status = acpi_hw_validate_register(reg, 64, &address);
219         if (ACPI_FAILURE(status)) {
220                 return (status);
221         }
222
223         /*
224          * Two address spaces supported: Memory or IO. PCI_Config is
225          * not supported here because the GAS structure is insufficient
226          */
227         if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
228                 status = acpi_os_write_memory((acpi_physical_address)
229                                               address, value, reg->bit_width);
230                 if (ACPI_FAILURE(status)) {
231                         return (status);
232                 }
233         } else {                /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
234
235                 width = reg->bit_width;
236                 if (width == 64) {
237                         width = 32;     /* Break into two 32-bit transfers */
238                 }
239
240                 status = acpi_hw_write_port((acpi_io_address)
241                                             address, ACPI_LODWORD(value),
242                                             width);
243                 if (ACPI_FAILURE(status)) {
244                         return (status);
245                 }
246
247                 if (reg->bit_width == 64) {
248                         status = acpi_hw_write_port((acpi_io_address)
249                                                     (address + 4),
250                                                     ACPI_HIDWORD(value), 32);
251                         if (ACPI_FAILURE(status)) {
252                                 return (status);
253                         }
254                 }
255         }
256
257         ACPI_DEBUG_PRINT((ACPI_DB_IO,
258                           "Wrote: %8.8X%8.8X width %2d   to %8.8X%8.8X (%s)\n",
259                           ACPI_FORMAT_UINT64(value), reg->bit_width,
260                           ACPI_FORMAT_UINT64(address),
261                           acpi_ut_get_region_name(reg->space_id)));
262
263         return (status);
264 }
265
266 ACPI_EXPORT_SYMBOL(acpi_write)
267
268 #if (!ACPI_REDUCED_HARDWARE)
269 /*******************************************************************************
270  *
271  * FUNCTION:    acpi_read_bit_register
272  *
273  * PARAMETERS:  register_id     - ID of ACPI Bit Register to access
274  *              return_value    - Value that was read from the register,
275  *                                normalized to bit position zero.
276  *
277  * RETURN:      Status and the value read from the specified Register. Value
278  *              returned is normalized to bit0 (is shifted all the way right)
279  *
280  * DESCRIPTION: ACPI bit_register read function. Does not acquire the HW lock.
281  *
282  * SUPPORTS:    Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
283  *              PM2 Control.
284  *
285  * Note: The hardware lock is not required when reading the ACPI bit registers
286  *       since almost all of them are single bit and it does not matter that
287  *       the parent hardware register can be split across two physical
288  *       registers. The only multi-bit field is SLP_TYP in the PM1 control
289  *       register, but this field does not cross an 8-bit boundary (nor does
290  *       it make much sense to actually read this field.)
291  *
292  ******************************************************************************/
293 acpi_status acpi_read_bit_register(u32 register_id, u32 *return_value)
294 {
295         struct acpi_bit_register_info *bit_reg_info;
296         u32 register_value;
297         u32 value;
298         acpi_status status;
299
300         ACPI_FUNCTION_TRACE_U32(acpi_read_bit_register, register_id);
301
302         /* Get the info structure corresponding to the requested ACPI Register */
303
304         bit_reg_info = acpi_hw_get_bit_register_info(register_id);
305         if (!bit_reg_info) {
306                 return_ACPI_STATUS(AE_BAD_PARAMETER);
307         }
308
309         /* Read the entire parent register */
310
311         status = acpi_hw_register_read(bit_reg_info->parent_register,
312                                        &register_value);
313         if (ACPI_FAILURE(status)) {
314                 return_ACPI_STATUS(status);
315         }
316
317         /* Normalize the value that was read, mask off other bits */
318
319         value = ((register_value & bit_reg_info->access_bit_mask)
320                  >> bit_reg_info->bit_position);
321
322         ACPI_DEBUG_PRINT((ACPI_DB_IO,
323                           "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
324                           register_id, bit_reg_info->parent_register,
325                           register_value, value));
326
327         *return_value = value;
328         return_ACPI_STATUS(AE_OK);
329 }
330
331 ACPI_EXPORT_SYMBOL(acpi_read_bit_register)
332
333 /*******************************************************************************
334  *
335  * FUNCTION:    acpi_write_bit_register
336  *
337  * PARAMETERS:  register_id     - ID of ACPI Bit Register to access
338  *              value           - Value to write to the register, in bit
339  *                                position zero. The bit is automatically
340  *                                shifted to the correct position.
341  *
342  * RETURN:      Status
343  *
344  * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
345  *              since most operations require a read/modify/write sequence.
346  *
347  * SUPPORTS:    Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
348  *              PM2 Control.
349  *
350  * Note that at this level, the fact that there may be actually two
351  * hardware registers (A and B - and B may not exist) is abstracted.
352  *
353  ******************************************************************************/
354 acpi_status acpi_write_bit_register(u32 register_id, u32 value)
355 {
356         struct acpi_bit_register_info *bit_reg_info;
357         acpi_cpu_flags lock_flags;
358         u32 register_value;
359         acpi_status status = AE_OK;
360
361         ACPI_FUNCTION_TRACE_U32(acpi_write_bit_register, register_id);
362
363         /* Get the info structure corresponding to the requested ACPI Register */
364
365         bit_reg_info = acpi_hw_get_bit_register_info(register_id);
366         if (!bit_reg_info) {
367                 return_ACPI_STATUS(AE_BAD_PARAMETER);
368         }
369
370         lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
371
372         /*
373          * At this point, we know that the parent register is one of the
374          * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
375          */
376         if (bit_reg_info->parent_register != ACPI_REGISTER_PM1_STATUS) {
377                 /*
378                  * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
379                  *
380                  * Perform a register read to preserve the bits that we are not
381                  * interested in
382                  */
383                 status = acpi_hw_register_read(bit_reg_info->parent_register,
384                                                &register_value);
385                 if (ACPI_FAILURE(status)) {
386                         goto unlock_and_exit;
387                 }
388
389                 /*
390                  * Insert the input bit into the value that was just read
391                  * and write the register
392                  */
393                 ACPI_REGISTER_INSERT_VALUE(register_value,
394                                            bit_reg_info->bit_position,
395                                            bit_reg_info->access_bit_mask,
396                                            value);
397
398                 status = acpi_hw_register_write(bit_reg_info->parent_register,
399                                                 register_value);
400         } else {
401                 /*
402                  * 2) Case for PM1 Status
403                  *
404                  * The Status register is different from the rest. Clear an event
405                  * by writing 1, writing 0 has no effect. So, the only relevant
406                  * information is the single bit we're interested in, all others
407                  * should be written as 0 so they will be left unchanged.
408                  */
409                 register_value = ACPI_REGISTER_PREPARE_BITS(value,
410                                                             bit_reg_info->
411                                                             bit_position,
412                                                             bit_reg_info->
413                                                             access_bit_mask);
414
415                 /* No need to write the register if value is all zeros */
416
417                 if (register_value) {
418                         status =
419                             acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
420                                                    register_value);
421                 }
422         }
423
424         ACPI_DEBUG_PRINT((ACPI_DB_IO,
425                           "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
426                           register_id, bit_reg_info->parent_register, value,
427                           register_value));
428
429 unlock_and_exit:
430
431         acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
432         return_ACPI_STATUS(status);
433 }
434
435 ACPI_EXPORT_SYMBOL(acpi_write_bit_register)
436 #endif                          /* !ACPI_REDUCED_HARDWARE */
437 /*******************************************************************************
438  *
439  * FUNCTION:    acpi_get_sleep_type_data
440  *
441  * PARAMETERS:  sleep_state         - Numeric sleep state
442  *              *sleep_type_a        - Where SLP_TYPa is returned
443  *              *sleep_type_b        - Where SLP_TYPb is returned
444  *
445  * RETURN:      Status
446  *
447  * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested
448  *              sleep state via the appropriate \_Sx object.
449  *
450  *  The sleep state package returned from the corresponding \_Sx_ object
451  *  must contain at least one integer.
452  *
453  *  March 2005:
454  *  Added support for a package that contains two integers. This
455  *  goes against the ACPI specification which defines this object as a
456  *  package with one encoded DWORD integer. However, existing practice
457  *  by many BIOS vendors is to return a package with 2 or more integer
458  *  elements, at least one per sleep type (A/B).
459  *
460  *  January 2013:
461  *  Therefore, we must be prepared to accept a package with either a
462  *  single integer or multiple integers.
463  *
464  *  The single integer DWORD format is as follows:
465  *      BYTE 0 - Value for the PM1A SLP_TYP register
466  *      BYTE 1 - Value for the PM1B SLP_TYP register
467  *      BYTE 2-3 - Reserved
468  *
469  *  The dual integer format is as follows:
470  *      Integer 0 - Value for the PM1A SLP_TYP register
471  *      Integer 1 - Value for the PM1A SLP_TYP register
472  *
473  ******************************************************************************/
474 acpi_status
475 acpi_get_sleep_type_data(u8 sleep_state, u8 *sleep_type_a, u8 *sleep_type_b)
476 {
477         acpi_status status;
478         struct acpi_evaluate_info *info;
479         union acpi_operand_object **elements;
480
481         ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
482
483         /* Validate parameters */
484
485         if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
486                 return_ACPI_STATUS(AE_BAD_PARAMETER);
487         }
488
489         /* Allocate the evaluation information block */
490
491         info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
492         if (!info) {
493                 return_ACPI_STATUS(AE_NO_MEMORY);
494         }
495
496         /*
497          * Evaluate the \_Sx namespace object containing the register values
498          * for this state
499          */
500         info->relative_pathname =
501             ACPI_CAST_PTR(char, acpi_gbl_sleep_state_names[sleep_state]);
502         status = acpi_ns_evaluate(info);
503         if (ACPI_FAILURE(status)) {
504                 goto cleanup;
505         }
506
507         /* Must have a return object */
508
509         if (!info->return_object) {
510                 ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
511                             info->relative_pathname));
512                 status = AE_AML_NO_RETURN_VALUE;
513                 goto cleanup;
514         }
515
516         /* Return object must be of type Package */
517
518         if (info->return_object->common.type != ACPI_TYPE_PACKAGE) {
519                 ACPI_ERROR((AE_INFO,
520                             "Sleep State return object is not a Package"));
521                 status = AE_AML_OPERAND_TYPE;
522                 goto cleanup1;
523         }
524
525         /*
526          * Any warnings about the package length or the object types have
527          * already been issued by the predefined name module -- there is no
528          * need to repeat them here.
529          */
530         elements = info->return_object->package.elements;
531         switch (info->return_object->package.count) {
532         case 0:
533
534                 status = AE_AML_PACKAGE_LIMIT;
535                 break;
536
537         case 1:
538
539                 if (elements[0]->common.type != ACPI_TYPE_INTEGER) {
540                         status = AE_AML_OPERAND_TYPE;
541                         break;
542                 }
543
544                 /* A valid _Sx_ package with one integer */
545
546                 *sleep_type_a = (u8)elements[0]->integer.value;
547                 *sleep_type_b = (u8)(elements[0]->integer.value >> 8);
548                 break;
549
550         case 2:
551         default:
552
553                 if ((elements[0]->common.type != ACPI_TYPE_INTEGER) ||
554                     (elements[1]->common.type != ACPI_TYPE_INTEGER)) {
555                         status = AE_AML_OPERAND_TYPE;
556                         break;
557                 }
558
559                 /* A valid _Sx_ package with two integers */
560
561                 *sleep_type_a = (u8)elements[0]->integer.value;
562                 *sleep_type_b = (u8)elements[1]->integer.value;
563                 break;
564         }
565
566       cleanup1:
567         acpi_ut_remove_reference(info->return_object);
568
569       cleanup:
570         if (ACPI_FAILURE(status)) {
571                 ACPI_EXCEPTION((AE_INFO, status,
572                                 "While evaluating Sleep State [%s]",
573                                 info->relative_pathname));
574         }
575
576         ACPI_FREE(info);
577         return_ACPI_STATUS(status);
578 }
579
580 ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)