2 /******************************************************************************
 
   4  * Module Name: exregion - ACPI default op_region (address space) handlers
 
   6  *****************************************************************************/
 
   9  * Copyright (C) 2000 - 2006, R. Byron Moore
 
  10  * All rights reserved.
 
  12  * Redistribution and use in source and binary forms, with or without
 
  13  * modification, are permitted provided that the following conditions
 
  15  * 1. Redistributions of source code must retain the above copyright
 
  16  *    notice, this list of conditions, and the following disclaimer,
 
  17  *    without modification.
 
  18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 
  19  *    substantially similar to the "NO WARRANTY" disclaimer below
 
  20  *    ("Disclaimer") and any redistribution must be conditioned upon
 
  21  *    including a substantially similar Disclaimer requirement for further
 
  22  *    binary redistribution.
 
  23  * 3. Neither the names of the above-listed copyright holders nor the names
 
  24  *    of any contributors may be used to endorse or promote products derived
 
  25  *    from this software without specific prior written permission.
 
  27  * Alternatively, this software may be distributed under the terms of the
 
  28  * GNU General Public License ("GPL") version 2 as published by the Free
 
  29  * Software Foundation.
 
  32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 
  33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 
  34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 
  35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 
  36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 
  37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 
  38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 
  39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 
  40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 
  41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
  42  * POSSIBILITY OF SUCH DAMAGES.
 
  45 #include <acpi/acpi.h>
 
  46 #include <acpi/acinterp.h>
 
  48 #define _COMPONENT          ACPI_EXECUTER
 
  49 ACPI_MODULE_NAME("exregion")
 
  51 /*******************************************************************************
 
  53  * FUNCTION:    acpi_ex_system_memory_space_handler
 
  55  * PARAMETERS:  Function            - Read or Write operation
 
  56  *              Address             - Where in the space to read or write
 
  57  *              bit_width           - Field width in bits (8, 16, or 32)
 
  58  *              Value               - Pointer to in or out value
 
  59  *              handler_context     - Pointer to Handler's context
 
  60  *              region_context      - Pointer to context specific to the
 
  65  * DESCRIPTION: Handler for the System Memory address space (Op Region)
 
  67  ******************************************************************************/
 
  69 acpi_ex_system_memory_space_handler(u32 function,
 
  70                                     acpi_physical_address address,
 
  73                                     void *handler_context, void *region_context)
 
  75         acpi_status status = AE_OK;
 
  76         void *logical_addr_ptr = NULL;
 
  77         struct acpi_mem_space_context *mem_info = region_context;
 
  79         acpi_size window_size;
 
  80 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
 
  84         ACPI_FUNCTION_TRACE(ex_system_memory_space_handler);
 
  86         /* Validate and translate the bit width */
 
 106                 ACPI_ERROR((AE_INFO, "Invalid SystemMemory width %d",
 
 108                 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
 
 111 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
 
 113          * Hardware does not support non-aligned data transfers, we must verify
 
 116         (void)acpi_ut_short_divide((acpi_integer) address, length, NULL,
 
 118         if (remainder != 0) {
 
 119                 return_ACPI_STATUS(AE_AML_ALIGNMENT);
 
 124          * Does the request fit into the cached memory mapping?
 
 125          * Is 1) Address below the current mapping? OR
 
 126          *    2) Address beyond the current mapping?
 
 128         if ((address < mem_info->mapped_physical_address) ||
 
 129             (((acpi_integer) address + length) > ((acpi_integer)
 
 131                                                   mapped_physical_address +
 
 132                                                   mem_info->mapped_length))) {
 
 134                  * The request cannot be resolved by the current memory mapping;
 
 135                  * Delete the existing mapping and create a new one.
 
 137                 if (mem_info->mapped_length) {
 
 139                         /* Valid mapping, delete it */
 
 141                         acpi_os_unmap_memory(mem_info->mapped_logical_address,
 
 142                                              mem_info->mapped_length);
 
 146                  * Don't attempt to map memory beyond the end of the region, and
 
 147                  * constrain the maximum mapping size to something reasonable.
 
 149                 window_size = (acpi_size)
 
 150                     ((mem_info->address + mem_info->length) - address);
 
 152                 if (window_size > ACPI_SYSMEM_REGION_WINDOW_SIZE) {
 
 153                         window_size = ACPI_SYSMEM_REGION_WINDOW_SIZE;
 
 156                 /* Create a new mapping starting at the address given */
 
 158                 status = acpi_os_map_memory(address, window_size,
 
 160                                             mapped_logical_address);
 
 161                 if (ACPI_FAILURE(status)) {
 
 163                                     "Could not map memory at %8.8X%8.8X, size %X",
 
 164                                     ACPI_FORMAT_UINT64(address),
 
 166                         mem_info->mapped_length = 0;
 
 167                         return_ACPI_STATUS(status);
 
 170                 /* Save the physical address and mapping size */
 
 172                 mem_info->mapped_physical_address = address;
 
 173                 mem_info->mapped_length = window_size;
 
 177          * Generate a logical pointer corresponding to the address we want to
 
 180         logical_addr_ptr = mem_info->mapped_logical_address +
 
 181             ((acpi_integer) address -
 
 182              (acpi_integer) mem_info->mapped_physical_address);
 
 184         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 185                           "System-Memory (width %d) R/W %d Address=%8.8X%8.8X\n",
 
 186                           bit_width, function, ACPI_FORMAT_UINT64(address)));
 
 189          * Perform the memory read or write
 
 191          * Note: For machines that do not support non-aligned transfers, the target
 
 192          * address was checked for alignment above.  We do not attempt to break the
 
 193          * transfer up into smaller (byte-size) chunks because the AML specifically
 
 194          * asked for a transfer width that the hardware may require.
 
 202                         *value = (acpi_integer) ACPI_GET8(logical_addr_ptr);
 
 206                         *value = (acpi_integer) ACPI_GET16(logical_addr_ptr);
 
 210                         *value = (acpi_integer) ACPI_GET32(logical_addr_ptr);
 
 213 #if ACPI_MACHINE_WIDTH != 16
 
 215                         *value = (acpi_integer) ACPI_GET64(logical_addr_ptr);
 
 219                         /* bit_width was already validated */
 
 228                         ACPI_SET8(logical_addr_ptr) = (u8) * value;
 
 232                         ACPI_SET16(logical_addr_ptr) = (u16) * value;
 
 236                         ACPI_SET32(logical_addr_ptr) = (u32) * value;
 
 239 #if ACPI_MACHINE_WIDTH != 16
 
 241                         ACPI_SET64(logical_addr_ptr) = (u64) * value;
 
 246                         /* bit_width was already validated */
 
 252                 status = AE_BAD_PARAMETER;
 
 256         return_ACPI_STATUS(status);
 
 259 /*******************************************************************************
 
 261  * FUNCTION:    acpi_ex_system_io_space_handler
 
 263  * PARAMETERS:  Function            - Read or Write operation
 
 264  *              Address             - Where in the space to read or write
 
 265  *              bit_width           - Field width in bits (8, 16, or 32)
 
 266  *              Value               - Pointer to in or out value
 
 267  *              handler_context     - Pointer to Handler's context
 
 268  *              region_context      - Pointer to context specific to the
 
 273  * DESCRIPTION: Handler for the System IO address space (Op Region)
 
 275  ******************************************************************************/
 
 278 acpi_ex_system_io_space_handler(u32 function,
 
 279                                 acpi_physical_address address,
 
 281                                 acpi_integer * value,
 
 282                                 void *handler_context, void *region_context)
 
 284         acpi_status status = AE_OK;
 
 287         ACPI_FUNCTION_TRACE(ex_system_io_space_handler);
 
 289         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 290                           "System-IO (width %d) R/W %d Address=%8.8X%8.8X\n",
 
 291                           bit_width, function, ACPI_FORMAT_UINT64(address)));
 
 293         /* Decode the function parameter */
 
 298                 status = acpi_os_read_port((acpi_io_address) address,
 
 299                                            &value32, bit_width);
 
 305                 status = acpi_os_write_port((acpi_io_address) address,
 
 306                                             (u32) * value, bit_width);
 
 310                 status = AE_BAD_PARAMETER;
 
 314         return_ACPI_STATUS(status);
 
 317 /*******************************************************************************
 
 319  * FUNCTION:    acpi_ex_pci_config_space_handler
 
 321  * PARAMETERS:  Function            - Read or Write operation
 
 322  *              Address             - Where in the space to read or write
 
 323  *              bit_width           - Field width in bits (8, 16, or 32)
 
 324  *              Value               - Pointer to in or out value
 
 325  *              handler_context     - Pointer to Handler's context
 
 326  *              region_context      - Pointer to context specific to the
 
 331  * DESCRIPTION: Handler for the PCI Config address space (Op Region)
 
 333  ******************************************************************************/
 
 336 acpi_ex_pci_config_space_handler(u32 function,
 
 337                                  acpi_physical_address address,
 
 339                                  acpi_integer * value,
 
 340                                  void *handler_context, void *region_context)
 
 342         acpi_status status = AE_OK;
 
 343         struct acpi_pci_id *pci_id;
 
 346         ACPI_FUNCTION_TRACE(ex_pci_config_space_handler);
 
 349          *  The arguments to acpi_os(Read|Write)pci_configuration are:
 
 351          *  pci_segment is the PCI bus segment range 0-31
 
 352          *  pci_bus     is the PCI bus number range 0-255
 
 353          *  pci_device  is the PCI device number range 0-31
 
 354          *  pci_function is the PCI device function number
 
 355          *  pci_register is the Config space register range 0-255 bytes
 
 357          *  Value - input value for write, output address for read
 
 360         pci_id = (struct acpi_pci_id *)region_context;
 
 361         pci_register = (u16) (u32) address;
 
 363         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
 
 364                           "Pci-Config %d (%d) Seg(%04x) Bus(%04x) Dev(%04x) Func(%04x) Reg(%04x)\n",
 
 365                           function, bit_width, pci_id->segment, pci_id->bus,
 
 366                           pci_id->device, pci_id->function, pci_register));
 
 372                 status = acpi_os_read_pci_configuration(pci_id, pci_register,
 
 378                 status = acpi_os_write_pci_configuration(pci_id, pci_register,
 
 384                 status = AE_BAD_PARAMETER;
 
 388         return_ACPI_STATUS(status);
 
 391 /*******************************************************************************
 
 393  * FUNCTION:    acpi_ex_cmos_space_handler
 
 395  * PARAMETERS:  Function            - Read or Write operation
 
 396  *              Address             - Where in the space to read or write
 
 397  *              bit_width           - Field width in bits (8, 16, or 32)
 
 398  *              Value               - Pointer to in or out value
 
 399  *              handler_context     - Pointer to Handler's context
 
 400  *              region_context      - Pointer to context specific to the
 
 405  * DESCRIPTION: Handler for the CMOS address space (Op Region)
 
 407  ******************************************************************************/
 
 410 acpi_ex_cmos_space_handler(u32 function,
 
 411                            acpi_physical_address address,
 
 413                            acpi_integer * value,
 
 414                            void *handler_context, void *region_context)
 
 416         acpi_status status = AE_OK;
 
 418         ACPI_FUNCTION_TRACE(ex_cmos_space_handler);
 
 420         return_ACPI_STATUS(status);
 
 423 /*******************************************************************************
 
 425  * FUNCTION:    acpi_ex_pci_bar_space_handler
 
 427  * PARAMETERS:  Function            - Read or Write operation
 
 428  *              Address             - Where in the space to read or write
 
 429  *              bit_width           - Field width in bits (8, 16, or 32)
 
 430  *              Value               - Pointer to in or out value
 
 431  *              handler_context     - Pointer to Handler's context
 
 432  *              region_context      - Pointer to context specific to the
 
 437  * DESCRIPTION: Handler for the PCI bar_target address space (Op Region)
 
 439  ******************************************************************************/
 
 442 acpi_ex_pci_bar_space_handler(u32 function,
 
 443                               acpi_physical_address address,
 
 445                               acpi_integer * value,
 
 446                               void *handler_context, void *region_context)
 
 448         acpi_status status = AE_OK;
 
 450         ACPI_FUNCTION_TRACE(ex_pci_bar_space_handler);
 
 452         return_ACPI_STATUS(status);
 
 455 /*******************************************************************************
 
 457  * FUNCTION:    acpi_ex_data_table_space_handler
 
 459  * PARAMETERS:  Function            - Read or Write operation
 
 460  *              Address             - Where in the space to read or write
 
 461  *              bit_width           - Field width in bits (8, 16, or 32)
 
 462  *              Value               - Pointer to in or out value
 
 463  *              handler_context     - Pointer to Handler's context
 
 464  *              region_context      - Pointer to context specific to the
 
 469  * DESCRIPTION: Handler for the Data Table address space (Op Region)
 
 471  ******************************************************************************/
 
 474 acpi_ex_data_table_space_handler(u32 function,
 
 475                                  acpi_physical_address address,
 
 477                                  acpi_integer * value,
 
 478                                  void *handler_context, void *region_context)
 
 480         ACPI_FUNCTION_TRACE(ex_data_table_space_handler);
 
 482         /* Perform the memory read or write */
 
 487                 ACPI_MEMCPY(ACPI_CAST_PTR(char, value),
 
 488                             ACPI_PHYSADDR_TO_PTR(address),
 
 489                             ACPI_DIV_8(bit_width));
 
 495                 return_ACPI_STATUS(AE_SUPPORT);
 
 498         return_ACPI_STATUS(AE_OK);