memblock: memblock should be able to handle zero length operations
[~shefty/rdma-dev.git] / drivers / acpi / acpica / hwxface.c
1
2 /******************************************************************************
3  *
4  * Module Name: hwxface - Public ACPICA hardware interfaces
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2012, 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 <linux/export.h>
46 #include <acpi/acpi.h>
47 #include "accommon.h"
48 #include "acnamesp.h"
49
50 #define _COMPONENT          ACPI_HARDWARE
51 ACPI_MODULE_NAME("hwxface")
52
53 /******************************************************************************
54  *
55  * FUNCTION:    acpi_reset
56  *
57  * PARAMETERS:  None
58  *
59  * RETURN:      Status
60  *
61  * DESCRIPTION: Set reset register in memory or IO space. Note: Does not
62  *              support reset register in PCI config space, this must be
63  *              handled separately.
64  *
65  ******************************************************************************/
66 acpi_status acpi_reset(void)
67 {
68         struct acpi_generic_address *reset_reg;
69         acpi_status status;
70
71         ACPI_FUNCTION_TRACE(acpi_reset);
72
73         reset_reg = &acpi_gbl_FADT.reset_register;
74
75         /* Check if the reset register is supported */
76
77         if (!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
84                  * bypasses the port validation mechanism, which may
85                  * block a valid write to the reset register. Spec
86                  * 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;
123         u32 width;
124         u64 address;
125         acpi_status status;
126
127         ACPI_FUNCTION_NAME(acpi_read);
128
129         if (!return_value) {
130                 return (AE_BAD_PARAMETER);
131         }
132
133         /* Validate contents of the GAS register. Allow 64-bit transfers */
134
135         status = acpi_hw_validate_register(reg, 64, &address);
136         if (ACPI_FAILURE(status)) {
137                 return (status);
138         }
139
140         /* Initialize entire 64-bit return value to zero */
141
142         *return_value = 0;
143         value = 0;
144
145         /*
146          * Two address spaces supported: Memory or IO. PCI_Config is
147          * not supported here because the GAS structure is insufficient
148          */
149         if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
150                 status = acpi_os_read_memory((acpi_physical_address)
151                                              address, return_value,
152                                              reg->bit_width);
153                 if (ACPI_FAILURE(status)) {
154                         return (status);
155                 }
156         } else {                /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
157
158                 width = reg->bit_width;
159                 if (width == 64) {
160                         width = 32;     /* Break into two 32-bit transfers */
161                 }
162
163                 status = acpi_hw_read_port((acpi_io_address)
164                                            address, &value, width);
165                 if (ACPI_FAILURE(status)) {
166                         return (status);
167                 }
168                 *return_value = value;
169
170                 if (reg->bit_width == 64) {
171
172                         /* Read the top 32 bits */
173
174                         status = acpi_hw_read_port((acpi_io_address)
175                                                    (address + 4), &value, 32);
176                         if (ACPI_FAILURE(status)) {
177                                 return (status);
178                         }
179                         *return_value |= ((u64)value << 32);
180                 }
181         }
182
183         ACPI_DEBUG_PRINT((ACPI_DB_IO,
184                           "Read:  %8.8X%8.8X width %2d from %8.8X%8.8X (%s)\n",
185                           ACPI_FORMAT_UINT64(*return_value), reg->bit_width,
186                           ACPI_FORMAT_UINT64(address),
187                           acpi_ut_get_region_name(reg->space_id)));
188
189         return (status);
190 }
191
192 ACPI_EXPORT_SYMBOL(acpi_read)
193
194 /******************************************************************************
195  *
196  * FUNCTION:    acpi_write
197  *
198  * PARAMETERS:  Value               - Value to be written
199  *              Reg                 - GAS register structure
200  *
201  * RETURN:      Status
202  *
203  * DESCRIPTION: Write to either memory or IO space.
204  *
205  ******************************************************************************/
206 acpi_status acpi_write(u64 value, struct acpi_generic_address *reg)
207 {
208         u32 width;
209         u64 address;
210         acpi_status status;
211
212         ACPI_FUNCTION_NAME(acpi_write);
213
214         /* Validate contents of the GAS register. Allow 64-bit transfers */
215
216         status = acpi_hw_validate_register(reg, 64, &address);
217         if (ACPI_FAILURE(status)) {
218                 return (status);
219         }
220
221         /*
222          * Two address spaces supported: Memory or IO. PCI_Config is
223          * not supported here because the GAS structure is insufficient
224          */
225         if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_MEMORY) {
226                 status = acpi_os_write_memory((acpi_physical_address)
227                                               address, value, reg->bit_width);
228                 if (ACPI_FAILURE(status)) {
229                         return (status);
230                 }
231         } else {                /* ACPI_ADR_SPACE_SYSTEM_IO, validated earlier */
232
233                 width = reg->bit_width;
234                 if (width == 64) {
235                         width = 32;     /* Break into two 32-bit transfers */
236                 }
237
238                 status = acpi_hw_write_port((acpi_io_address)
239                                             address, ACPI_LODWORD(value),
240                                             width);
241                 if (ACPI_FAILURE(status)) {
242                         return (status);
243                 }
244
245                 if (reg->bit_width == 64) {
246                         status = acpi_hw_write_port((acpi_io_address)
247                                                     (address + 4),
248                                                     ACPI_HIDWORD(value), 32);
249                         if (ACPI_FAILURE(status)) {
250                                 return (status);
251                         }
252                 }
253         }
254
255         ACPI_DEBUG_PRINT((ACPI_DB_IO,
256                           "Wrote: %8.8X%8.8X width %2d   to %8.8X%8.8X (%s)\n",
257                           ACPI_FORMAT_UINT64(value), reg->bit_width,
258                           ACPI_FORMAT_UINT64(address),
259                           acpi_ut_get_region_name(reg->space_id)));
260
261         return (status);
262 }
263
264 ACPI_EXPORT_SYMBOL(acpi_write)
265
266 #if (!ACPI_REDUCED_HARDWARE)
267 /*******************************************************************************
268  *
269  * FUNCTION:    acpi_read_bit_register
270  *
271  * PARAMETERS:  register_id     - ID of ACPI Bit Register to access
272  *              return_value    - Value that was read from the register,
273  *                                normalized to bit position zero.
274  *
275  * RETURN:      Status and the value read from the specified Register. Value
276  *              returned is normalized to bit0 (is shifted all the way right)
277  *
278  * DESCRIPTION: ACPI bit_register read function. Does not acquire the HW lock.
279  *
280  * SUPPORTS:    Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
281  *              PM2 Control.
282  *
283  * Note: The hardware lock is not required when reading the ACPI bit registers
284  *       since almost all of them are single bit and it does not matter that
285  *       the parent hardware register can be split across two physical
286  *       registers. The only multi-bit field is SLP_TYP in the PM1 control
287  *       register, but this field does not cross an 8-bit boundary (nor does
288  *       it make much sense to actually read this field.)
289  *
290  ******************************************************************************/
291 acpi_status acpi_read_bit_register(u32 register_id, u32 *return_value)
292 {
293         struct acpi_bit_register_info *bit_reg_info;
294         u32 register_value;
295         u32 value;
296         acpi_status status;
297
298         ACPI_FUNCTION_TRACE_U32(acpi_read_bit_register, register_id);
299
300         /* Get the info structure corresponding to the requested ACPI Register */
301
302         bit_reg_info = acpi_hw_get_bit_register_info(register_id);
303         if (!bit_reg_info) {
304                 return_ACPI_STATUS(AE_BAD_PARAMETER);
305         }
306
307         /* Read the entire parent register */
308
309         status = acpi_hw_register_read(bit_reg_info->parent_register,
310                                        &register_value);
311         if (ACPI_FAILURE(status)) {
312                 return_ACPI_STATUS(status);
313         }
314
315         /* Normalize the value that was read, mask off other bits */
316
317         value = ((register_value & bit_reg_info->access_bit_mask)
318                  >> bit_reg_info->bit_position);
319
320         ACPI_DEBUG_PRINT((ACPI_DB_IO,
321                           "BitReg %X, ParentReg %X, Actual %8.8X, ReturnValue %8.8X\n",
322                           register_id, bit_reg_info->parent_register,
323                           register_value, value));
324
325         *return_value = value;
326         return_ACPI_STATUS(AE_OK);
327 }
328
329 ACPI_EXPORT_SYMBOL(acpi_read_bit_register)
330
331 /*******************************************************************************
332  *
333  * FUNCTION:    acpi_write_bit_register
334  *
335  * PARAMETERS:  register_id     - ID of ACPI Bit Register to access
336  *              Value           - Value to write to the register, in bit
337  *                                position zero. The bit is automatically
338  *                                shifted to the correct position.
339  *
340  * RETURN:      Status
341  *
342  * DESCRIPTION: ACPI Bit Register write function. Acquires the hardware lock
343  *              since most operations require a read/modify/write sequence.
344  *
345  * SUPPORTS:    Bit fields in PM1 Status, PM1 Enable, PM1 Control, and
346  *              PM2 Control.
347  *
348  * Note that at this level, the fact that there may be actually two
349  * hardware registers (A and B - and B may not exist) is abstracted.
350  *
351  ******************************************************************************/
352 acpi_status acpi_write_bit_register(u32 register_id, u32 value)
353 {
354         struct acpi_bit_register_info *bit_reg_info;
355         acpi_cpu_flags lock_flags;
356         u32 register_value;
357         acpi_status status = AE_OK;
358
359         ACPI_FUNCTION_TRACE_U32(acpi_write_bit_register, register_id);
360
361         /* Get the info structure corresponding to the requested ACPI Register */
362
363         bit_reg_info = acpi_hw_get_bit_register_info(register_id);
364         if (!bit_reg_info) {
365                 return_ACPI_STATUS(AE_BAD_PARAMETER);
366         }
367
368         lock_flags = acpi_os_acquire_lock(acpi_gbl_hardware_lock);
369
370         /*
371          * At this point, we know that the parent register is one of the
372          * following: PM1 Status, PM1 Enable, PM1 Control, or PM2 Control
373          */
374         if (bit_reg_info->parent_register != ACPI_REGISTER_PM1_STATUS) {
375                 /*
376                  * 1) Case for PM1 Enable, PM1 Control, and PM2 Control
377                  *
378                  * Perform a register read to preserve the bits that we are not
379                  * interested in
380                  */
381                 status = acpi_hw_register_read(bit_reg_info->parent_register,
382                                                &register_value);
383                 if (ACPI_FAILURE(status)) {
384                         goto unlock_and_exit;
385                 }
386
387                 /*
388                  * Insert the input bit into the value that was just read
389                  * and write the register
390                  */
391                 ACPI_REGISTER_INSERT_VALUE(register_value,
392                                            bit_reg_info->bit_position,
393                                            bit_reg_info->access_bit_mask,
394                                            value);
395
396                 status = acpi_hw_register_write(bit_reg_info->parent_register,
397                                                 register_value);
398         } else {
399                 /*
400                  * 2) Case for PM1 Status
401                  *
402                  * The Status register is different from the rest. Clear an event
403                  * by writing 1, writing 0 has no effect. So, the only relevant
404                  * information is the single bit we're interested in, all others
405                  * should be written as 0 so they will be left unchanged.
406                  */
407                 register_value = ACPI_REGISTER_PREPARE_BITS(value,
408                                                             bit_reg_info->
409                                                             bit_position,
410                                                             bit_reg_info->
411                                                             access_bit_mask);
412
413                 /* No need to write the register if value is all zeros */
414
415                 if (register_value) {
416                         status =
417                             acpi_hw_register_write(ACPI_REGISTER_PM1_STATUS,
418                                                    register_value);
419                 }
420         }
421
422         ACPI_DEBUG_PRINT((ACPI_DB_IO,
423                           "BitReg %X, ParentReg %X, Value %8.8X, Actual %8.8X\n",
424                           register_id, bit_reg_info->parent_register, value,
425                           register_value));
426
427 unlock_and_exit:
428
429         acpi_os_release_lock(acpi_gbl_hardware_lock, lock_flags);
430         return_ACPI_STATUS(status);
431 }
432
433 ACPI_EXPORT_SYMBOL(acpi_write_bit_register)
434 #endif                          /* !ACPI_REDUCED_HARDWARE */
435 /*******************************************************************************
436  *
437  * FUNCTION:    acpi_get_sleep_type_data
438  *
439  * PARAMETERS:  sleep_state         - Numeric sleep state
440  *              *sleep_type_a        - Where SLP_TYPa is returned
441  *              *sleep_type_b        - Where SLP_TYPb is returned
442  *
443  * RETURN:      Status - ACPI status
444  *
445  * DESCRIPTION: Obtain the SLP_TYPa and SLP_TYPb values for the requested sleep
446  *              state.
447  *
448  ******************************************************************************/
449 acpi_status
450 acpi_get_sleep_type_data(u8 sleep_state, u8 *sleep_type_a, u8 *sleep_type_b)
451 {
452         acpi_status status = AE_OK;
453         struct acpi_evaluate_info *info;
454
455         ACPI_FUNCTION_TRACE(acpi_get_sleep_type_data);
456
457         /* Validate parameters */
458
459         if ((sleep_state > ACPI_S_STATES_MAX) || !sleep_type_a || !sleep_type_b) {
460                 return_ACPI_STATUS(AE_BAD_PARAMETER);
461         }
462
463         /* Allocate the evaluation information block */
464
465         info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
466         if (!info) {
467                 return_ACPI_STATUS(AE_NO_MEMORY);
468         }
469
470         info->pathname =
471             ACPI_CAST_PTR(char, acpi_gbl_sleep_state_names[sleep_state]);
472
473         /* Evaluate the namespace object containing the values for this state */
474
475         status = acpi_ns_evaluate(info);
476         if (ACPI_FAILURE(status)) {
477                 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
478                                   "%s while evaluating SleepState [%s]\n",
479                                   acpi_format_exception(status),
480                                   info->pathname));
481
482                 goto cleanup;
483         }
484
485         /* Must have a return object */
486
487         if (!info->return_object) {
488                 ACPI_ERROR((AE_INFO, "No Sleep State object returned from [%s]",
489                             info->pathname));
490                 status = AE_NOT_EXIST;
491         }
492
493         /* It must be of type Package */
494
495         else if (info->return_object->common.type != ACPI_TYPE_PACKAGE) {
496                 ACPI_ERROR((AE_INFO,
497                             "Sleep State return object is not a Package"));
498                 status = AE_AML_OPERAND_TYPE;
499         }
500
501         /*
502          * The package must have at least two elements. NOTE (March 2005): This
503          * goes against the current ACPI spec which defines this object as a
504          * package with one encoded DWORD element. However, existing practice
505          * by BIOS vendors seems to be to have 2 or more elements, at least
506          * one per sleep type (A/B).
507          */
508         else if (info->return_object->package.count < 2) {
509                 ACPI_ERROR((AE_INFO,
510                             "Sleep State return package does not have at least two elements"));
511                 status = AE_AML_NO_OPERAND;
512         }
513
514         /* The first two elements must both be of type Integer */
515
516         else if (((info->return_object->package.elements[0])->common.type
517                   != ACPI_TYPE_INTEGER) ||
518                  ((info->return_object->package.elements[1])->common.type
519                   != ACPI_TYPE_INTEGER)) {
520                 ACPI_ERROR((AE_INFO,
521                             "Sleep State return package elements are not both Integers "
522                             "(%s, %s)",
523                             acpi_ut_get_object_type_name(info->return_object->
524                                                          package.elements[0]),
525                             acpi_ut_get_object_type_name(info->return_object->
526                                                          package.elements[1])));
527                 status = AE_AML_OPERAND_TYPE;
528         } else {
529                 /* Valid _Sx_ package size, type, and value */
530
531                 *sleep_type_a = (u8)
532                     (info->return_object->package.elements[0])->integer.value;
533                 *sleep_type_b = (u8)
534                     (info->return_object->package.elements[1])->integer.value;
535         }
536
537         if (ACPI_FAILURE(status)) {
538                 ACPI_EXCEPTION((AE_INFO, status,
539                                 "While evaluating SleepState [%s], bad Sleep object %p type %s",
540                                 info->pathname, info->return_object,
541                                 acpi_ut_get_object_type_name(info->
542                                                              return_object)));
543         }
544
545         acpi_ut_remove_reference(info->return_object);
546
547       cleanup:
548         ACPI_FREE(info);
549         return_ACPI_STATUS(status);
550 }
551
552 ACPI_EXPORT_SYMBOL(acpi_get_sleep_type_data)