pandora: defconfig: update
[pandora-kernel.git] / drivers / acpi / acpica / exregion.c
1
2 /******************************************************************************
3  *
4  * Module Name: exregion - ACPI default op_region (address space) handlers
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2011, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45 #include <acpi/acpi.h>
46 #include "accommon.h"
47 #include "acinterp.h"
48
49 #define _COMPONENT          ACPI_EXECUTER
50 ACPI_MODULE_NAME("exregion")
51
52 /*******************************************************************************
53  *
54  * FUNCTION:    acpi_ex_system_memory_space_handler
55  *
56  * PARAMETERS:  Function            - Read or Write operation
57  *              Address             - Where in the space to read or write
58  *              bit_width           - Field width in bits (8, 16, or 32)
59  *              Value               - Pointer to in or out value
60  *              handler_context     - Pointer to Handler's context
61  *              region_context      - Pointer to context specific to the
62  *                                    accessed region
63  *
64  * RETURN:      Status
65  *
66  * DESCRIPTION: Handler for the System Memory address space (Op Region)
67  *
68  ******************************************************************************/
69 acpi_status
70 acpi_ex_system_memory_space_handler(u32 function,
71                                     acpi_physical_address address,
72                                     u32 bit_width,
73                                     u64 *value,
74                                     void *handler_context, void *region_context)
75 {
76         acpi_status status = AE_OK;
77         void *logical_addr_ptr = NULL;
78         struct acpi_mem_space_context *mem_info = region_context;
79         u32 length;
80         acpi_size map_length;
81         acpi_size page_boundary_map_length;
82 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
83         u32 remainder;
84 #endif
85
86         ACPI_FUNCTION_TRACE(ex_system_memory_space_handler);
87
88         /* Validate and translate the bit width */
89
90         switch (bit_width) {
91         case 8:
92                 length = 1;
93                 break;
94
95         case 16:
96                 length = 2;
97                 break;
98
99         case 32:
100                 length = 4;
101                 break;
102
103         case 64:
104                 length = 8;
105                 break;
106
107         default:
108                 ACPI_ERROR((AE_INFO, "Invalid SystemMemory width %u",
109                             bit_width));
110                 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
111         }
112
113 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
114         /*
115          * Hardware does not support non-aligned data transfers, we must verify
116          * the request.
117          */
118         (void)acpi_ut_short_divide((u64) address, length, NULL, &remainder);
119         if (remainder != 0) {
120                 return_ACPI_STATUS(AE_AML_ALIGNMENT);
121         }
122 #endif
123
124         /*
125          * Does the request fit into the cached memory mapping?
126          * Is 1) Address below the current mapping? OR
127          *    2) Address beyond the current mapping?
128          */
129         if ((address < mem_info->mapped_physical_address) ||
130             (((u64) address + length) > ((u64)
131                                          mem_info->mapped_physical_address +
132                                          mem_info->mapped_length))) {
133                 /*
134                  * The request cannot be resolved by the current memory mapping;
135                  * Delete the existing mapping and create a new one.
136                  */
137                 if (mem_info->mapped_length) {
138
139                         /* Valid mapping, delete it */
140
141                         acpi_os_unmap_memory(mem_info->mapped_logical_address,
142                                              mem_info->mapped_length);
143                 }
144
145                 /*
146                  * Attempt to map from the requested address to the end of the region.
147                  * However, we will never map more than one page, nor will we cross
148                  * a page boundary.
149                  */
150                 map_length = (acpi_size)
151                     ((mem_info->address + mem_info->length) - address);
152
153                 /*
154                  * If mapping the entire remaining portion of the region will cross
155                  * a page boundary, just map up to the page boundary, do not cross.
156                  * On some systems, crossing a page boundary while mapping regions
157                  * can cause warnings if the pages have different attributes
158                  * due to resource management
159                  */
160                 page_boundary_map_length =
161                     ACPI_ROUND_UP(address, ACPI_DEFAULT_PAGE_SIZE) - address;
162
163                 if (!page_boundary_map_length) {
164                         page_boundary_map_length = ACPI_DEFAULT_PAGE_SIZE;
165                 }
166
167                 if (map_length > page_boundary_map_length) {
168                         map_length = page_boundary_map_length;
169                 }
170
171                 /* Create a new mapping starting at the address given */
172
173                 mem_info->mapped_logical_address = acpi_os_map_memory((acpi_physical_address) address, map_length);
174                 if (!mem_info->mapped_logical_address) {
175                         ACPI_ERROR((AE_INFO,
176                                     "Could not map memory at 0x%8.8X%8.8X, size %u",
177                                     ACPI_FORMAT_UINT64(address),
178                                     (u32) map_length));
179                         mem_info->mapped_length = 0;
180                         return_ACPI_STATUS(AE_NO_MEMORY);
181                 }
182
183                 /* Save the physical address and mapping size */
184
185                 mem_info->mapped_physical_address = address;
186                 mem_info->mapped_length = map_length;
187         }
188
189         /*
190          * Generate a logical pointer corresponding to the address we want to
191          * access
192          */
193         logical_addr_ptr = mem_info->mapped_logical_address +
194             ((u64) address - (u64) mem_info->mapped_physical_address);
195
196         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
197                           "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n",
198                           bit_width, function, ACPI_FORMAT_UINT64(address)));
199
200         /*
201          * Perform the memory read or write
202          *
203          * Note: For machines that do not support non-aligned transfers, the target
204          * address was checked for alignment above.  We do not attempt to break the
205          * transfer up into smaller (byte-size) chunks because the AML specifically
206          * asked for a transfer width that the hardware may require.
207          */
208         switch (function) {
209         case ACPI_READ:
210
211                 *value = 0;
212                 switch (bit_width) {
213                 case 8:
214                         *value = (u64) ACPI_GET8(logical_addr_ptr);
215                         break;
216
217                 case 16:
218                         *value = (u64) ACPI_GET16(logical_addr_ptr);
219                         break;
220
221                 case 32:
222                         *value = (u64) ACPI_GET32(logical_addr_ptr);
223                         break;
224
225                 case 64:
226                         *value = (u64) ACPI_GET64(logical_addr_ptr);
227                         break;
228
229                 default:
230                         /* bit_width was already validated */
231                         break;
232                 }
233                 break;
234
235         case ACPI_WRITE:
236
237                 switch (bit_width) {
238                 case 8:
239                         ACPI_SET8(logical_addr_ptr) = (u8) * value;
240                         break;
241
242                 case 16:
243                         ACPI_SET16(logical_addr_ptr) = (u16) * value;
244                         break;
245
246                 case 32:
247                         ACPI_SET32(logical_addr_ptr) = (u32) * value;
248                         break;
249
250                 case 64:
251                         ACPI_SET64(logical_addr_ptr) = (u64) * value;
252                         break;
253
254                 default:
255                         /* bit_width was already validated */
256                         break;
257                 }
258                 break;
259
260         default:
261                 status = AE_BAD_PARAMETER;
262                 break;
263         }
264
265         return_ACPI_STATUS(status);
266 }
267
268 /*******************************************************************************
269  *
270  * FUNCTION:    acpi_ex_system_io_space_handler
271  *
272  * PARAMETERS:  Function            - Read or Write operation
273  *              Address             - Where in the space to read or write
274  *              bit_width           - Field width in bits (8, 16, or 32)
275  *              Value               - Pointer to in or out value
276  *              handler_context     - Pointer to Handler's context
277  *              region_context      - Pointer to context specific to the
278  *                                    accessed region
279  *
280  * RETURN:      Status
281  *
282  * DESCRIPTION: Handler for the System IO address space (Op Region)
283  *
284  ******************************************************************************/
285
286 acpi_status
287 acpi_ex_system_io_space_handler(u32 function,
288                                 acpi_physical_address address,
289                                 u32 bit_width,
290                                 u64 *value,
291                                 void *handler_context, void *region_context)
292 {
293         acpi_status status = AE_OK;
294         u32 value32;
295
296         ACPI_FUNCTION_TRACE(ex_system_io_space_handler);
297
298         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
299                           "System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",
300                           bit_width, function, ACPI_FORMAT_UINT64(address)));
301
302         /* Decode the function parameter */
303
304         switch (function) {
305         case ACPI_READ:
306
307                 status = acpi_hw_read_port((acpi_io_address) address,
308                                            &value32, bit_width);
309                 *value = value32;
310                 break;
311
312         case ACPI_WRITE:
313
314                 status = acpi_hw_write_port((acpi_io_address) address,
315                                             (u32) * value, bit_width);
316                 break;
317
318         default:
319                 status = AE_BAD_PARAMETER;
320                 break;
321         }
322
323         return_ACPI_STATUS(status);
324 }
325
326 /*******************************************************************************
327  *
328  * FUNCTION:    acpi_ex_pci_config_space_handler
329  *
330  * PARAMETERS:  Function            - Read or Write operation
331  *              Address             - Where in the space to read or write
332  *              bit_width           - Field width in bits (8, 16, or 32)
333  *              Value               - Pointer to in or out value
334  *              handler_context     - Pointer to Handler's context
335  *              region_context      - Pointer to context specific to the
336  *                                    accessed region
337  *
338  * RETURN:      Status
339  *
340  * DESCRIPTION: Handler for the PCI Config address space (Op Region)
341  *
342  ******************************************************************************/
343
344 acpi_status
345 acpi_ex_pci_config_space_handler(u32 function,
346                                  acpi_physical_address address,
347                                  u32 bit_width,
348                                  u64 *value,
349                                  void *handler_context, void *region_context)
350 {
351         acpi_status status = AE_OK;
352         struct acpi_pci_id *pci_id;
353         u16 pci_register;
354
355         ACPI_FUNCTION_TRACE(ex_pci_config_space_handler);
356
357         /*
358          *  The arguments to acpi_os(Read|Write)pci_configuration are:
359          *
360          *  pci_segment is the PCI bus segment range 0-31
361          *  pci_bus     is the PCI bus number range 0-255
362          *  pci_device  is the PCI device number range 0-31
363          *  pci_function is the PCI device function number
364          *  pci_register is the Config space register range 0-255 bytes
365          *
366          *  Value - input value for write, output address for read
367          *
368          */
369         pci_id = (struct acpi_pci_id *)region_context;
370         pci_register = (u16) (u32) address;
371
372         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
373                           "Pci-Config %u (%u) Seg(%04x) Bus(%04x) Dev(%04x) Func(%04x) Reg(%04x)\n",
374                           function, bit_width, pci_id->segment, pci_id->bus,
375                           pci_id->device, pci_id->function, pci_register));
376
377         switch (function) {
378         case ACPI_READ:
379
380                 status = acpi_os_read_pci_configuration(pci_id, pci_register,
381                                                         value, bit_width);
382                 break;
383
384         case ACPI_WRITE:
385
386                 status = acpi_os_write_pci_configuration(pci_id, pci_register,
387                                                          *value, bit_width);
388                 break;
389
390         default:
391
392                 status = AE_BAD_PARAMETER;
393                 break;
394         }
395
396         return_ACPI_STATUS(status);
397 }
398
399 /*******************************************************************************
400  *
401  * FUNCTION:    acpi_ex_cmos_space_handler
402  *
403  * PARAMETERS:  Function            - Read or Write operation
404  *              Address             - Where in the space to read or write
405  *              bit_width           - Field width in bits (8, 16, or 32)
406  *              Value               - Pointer to in or out value
407  *              handler_context     - Pointer to Handler's context
408  *              region_context      - Pointer to context specific to the
409  *                                    accessed region
410  *
411  * RETURN:      Status
412  *
413  * DESCRIPTION: Handler for the CMOS address space (Op Region)
414  *
415  ******************************************************************************/
416
417 acpi_status
418 acpi_ex_cmos_space_handler(u32 function,
419                            acpi_physical_address address,
420                            u32 bit_width,
421                            u64 *value,
422                            void *handler_context, void *region_context)
423 {
424         acpi_status status = AE_OK;
425
426         ACPI_FUNCTION_TRACE(ex_cmos_space_handler);
427
428         return_ACPI_STATUS(status);
429 }
430
431 /*******************************************************************************
432  *
433  * FUNCTION:    acpi_ex_pci_bar_space_handler
434  *
435  * PARAMETERS:  Function            - Read or Write operation
436  *              Address             - Where in the space to read or write
437  *              bit_width           - Field width in bits (8, 16, or 32)
438  *              Value               - Pointer to in or out value
439  *              handler_context     - Pointer to Handler's context
440  *              region_context      - Pointer to context specific to the
441  *                                    accessed region
442  *
443  * RETURN:      Status
444  *
445  * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
446  *
447  ******************************************************************************/
448
449 acpi_status
450 acpi_ex_pci_bar_space_handler(u32 function,
451                               acpi_physical_address address,
452                               u32 bit_width,
453                               u64 *value,
454                               void *handler_context, void *region_context)
455 {
456         acpi_status status = AE_OK;
457
458         ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler);
459
460         return_ACPI_STATUS(status);
461 }
462
463 /*******************************************************************************
464  *
465  * FUNCTION:    acpi_ex_data_table_space_handler
466  *
467  * PARAMETERS:  Function            - Read or Write operation
468  *              Address             - Where in the space to read or write
469  *              bit_width           - Field width in bits (8, 16, or 32)
470  *              Value               - Pointer to in or out value
471  *              handler_context     - Pointer to Handler's context
472  *              region_context      - Pointer to context specific to the
473  *                                    accessed region
474  *
475  * RETURN:      Status
476  *
477  * DESCRIPTION: Handler for the Data Table address space (Op Region)
478  *
479  ******************************************************************************/
480
481 acpi_status
482 acpi_ex_data_table_space_handler(u32 function,
483                                  acpi_physical_address address,
484                                  u32 bit_width,
485                                  u64 *value,
486                                  void *handler_context, void *region_context)
487 {
488         ACPI_FUNCTION_TRACE(ex_data_table_space_handler);
489
490         /*
491          * Perform the memory read or write. The bit_width was already
492          * validated.
493          */
494         switch (function) {
495         case ACPI_READ:
496
497                 ACPI_MEMCPY(ACPI_CAST_PTR(char, value),
498                             ACPI_PHYSADDR_TO_PTR(address),
499                             ACPI_DIV_8(bit_width));
500                 break;
501
502         case ACPI_WRITE:
503
504                 ACPI_MEMCPY(ACPI_PHYSADDR_TO_PTR(address),
505                             ACPI_CAST_PTR(char, value), ACPI_DIV_8(bit_width));
506                 break;
507
508         default:
509
510                 return_ACPI_STATUS(AE_BAD_PARAMETER);
511         }
512
513         return_ACPI_STATUS(AE_OK);
514 }