xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/executer/exregion.c (revision 776c58b2b56d8bcf33638a2ecb6c697f95a1cbf3)
1 /******************************************************************************
2  *
3  * Module Name: exregion - ACPI default OpRegion (address space) handlers
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2012, Intel Corp.
12  * All rights reserved.
13  *
14  * 2. License
15  *
16  * 2.1. This is your license from Intel Corp. under its intellectual property
17  * rights. You may have additional license terms from the party that provided
18  * you this software, covering your right to use that party's intellectual
19  * property rights.
20  *
21  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22  * copy of the source code appearing in this file ("Covered Code") an
23  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24  * base code distributed originally by Intel ("Original Intel Code") to copy,
25  * make derivatives, distribute, use and display any portion of the Covered
26  * Code in any form, with the right to sublicense such rights; and
27  *
28  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29  * license (with the right to sublicense), under only those claims of Intel
30  * patents that are infringed by the Original Intel Code, to make, use, sell,
31  * offer to sell, and import the Covered Code and derivative works thereof
32  * solely to the minimum extent necessary to exercise the above copyright
33  * license, and in no event shall the patent license extend to any additions
34  * to or modifications of the Original Intel Code. No other license or right
35  * is granted directly or by implication, estoppel or otherwise;
36  *
37  * The above copyright and patent license is granted only if the following
38  * conditions are met:
39  *
40  * 3. Conditions
41  *
42  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43  * Redistribution of source code of any substantial portion of the Covered
44  * Code or modification with rights to further distribute source must include
45  * the above Copyright Notice, the above License, this list of Conditions,
46  * and the following Disclaimer and Export Compliance provision. In addition,
47  * Licensee must cause all Covered Code to which Licensee contributes to
48  * contain a file documenting the changes Licensee made to create that Covered
49  * Code and the date of any change. Licensee must include in that file the
50  * documentation of any changes made by any predecessor Licensee. Licensee
51  * must include a prominent statement that the modification is derived,
52  * directly or indirectly, from Original Intel Code.
53  *
54  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55  * Redistribution of source code of any substantial portion of the Covered
56  * Code or modification without rights to further distribute source must
57  * include the following Disclaimer and Export Compliance provision in the
58  * documentation and/or other materials provided with distribution. In
59  * addition, Licensee may not authorize further sublicense of source of any
60  * portion of the Covered Code, and must include terms to the effect that the
61  * license from Licensee to its licensee is limited to the intellectual
62  * property embodied in the software Licensee provides to its licensee, and
63  * not to intellectual property embodied in modifications its licensee may
64  * make.
65  *
66  * 3.3. Redistribution of Executable. Redistribution in executable form of any
67  * substantial portion of the Covered Code or modification must reproduce the
68  * above Copyright Notice, and the following Disclaimer and Export Compliance
69  * provision in the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3.4. Intel retains all right, title, and interest in and to the Original
73  * Intel Code.
74  *
75  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76  * Intel shall be used in advertising or otherwise to promote the sale, use or
77  * other dealings in products derived from or relating to the Covered Code
78  * without prior written authorization from Intel.
79  *
80  * 4. Disclaimer and Export Compliance
81  *
82  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88  * PARTICULAR PURPOSE.
89  *
90  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97  * LIMITED REMEDY.
98  *
99  * 4.3. Licensee shall not export, either directly or indirectly, any of this
100  * software or system incorporating such software without first obtaining any
101  * required license or other approval from the U. S. Department of Commerce or
102  * any other agency or department of the United States Government. In the
103  * event Licensee exports any such software from the United States or
104  * re-exports any such software from a foreign destination, Licensee shall
105  * ensure that the distribution and export/re-export of the software is in
106  * compliance with all laws, regulations, orders, or other restrictions of the
107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108  * any of its subsidiaries will export/re-export any technical data, process,
109  * software, or service, directly or indirectly, to any country for which the
110  * United States government or any agency thereof requires an export license,
111  * other governmental approval, or letter of assurance, without first obtaining
112  * such license, approval or letter.
113  *
114  *****************************************************************************/
115 
116 
117 #define __EXREGION_C__
118 
119 #include "acpi.h"
120 #include "accommon.h"
121 #include "acinterp.h"
122 
123 
124 #define _COMPONENT          ACPI_EXECUTER
125         ACPI_MODULE_NAME    ("exregion")
126 
127 
128 /*******************************************************************************
129  *
130  * FUNCTION:    AcpiExSystemMemorySpaceHandler
131  *
132  * PARAMETERS:  Function            - Read or Write operation
133  *              Address             - Where in the space to read or write
134  *              BitWidth            - Field width in bits (8, 16, or 32)
135  *              Value               - Pointer to in or out value
136  *              HandlerContext      - Pointer to Handler's context
137  *              RegionContext       - Pointer to context specific to the
138  *                                    accessed region
139  *
140  * RETURN:      Status
141  *
142  * DESCRIPTION: Handler for the System Memory address space (Op Region)
143  *
144  ******************************************************************************/
145 
146 ACPI_STATUS
147 AcpiExSystemMemorySpaceHandler (
148     UINT32                  Function,
149     ACPI_PHYSICAL_ADDRESS   Address,
150     UINT32                  BitWidth,
151     UINT64                  *Value,
152     void                    *HandlerContext,
153     void                    *RegionContext)
154 {
155     ACPI_STATUS             Status = AE_OK;
156     void                    *LogicalAddrPtr = NULL;
157     ACPI_MEM_SPACE_CONTEXT  *MemInfo = RegionContext;
158     UINT32                  Length;
159     ACPI_SIZE               MapLength;
160     ACPI_SIZE               PageBoundaryMapLength;
161 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
162     UINT32                  Remainder;
163 #endif
164 
165 
166     ACPI_FUNCTION_TRACE (ExSystemMemorySpaceHandler);
167 
168 
169     /* Validate and translate the bit width */
170 
171     switch (BitWidth)
172     {
173     case 8:
174         Length = 1;
175         break;
176 
177     case 16:
178         Length = 2;
179         break;
180 
181     case 32:
182         Length = 4;
183         break;
184 
185     case 64:
186         Length = 8;
187         break;
188 
189     default:
190         ACPI_ERROR ((AE_INFO, "Invalid SystemMemory width %u",
191             BitWidth));
192         return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
193     }
194 
195 #ifdef ACPI_MISALIGNMENT_NOT_SUPPORTED
196     /*
197      * Hardware does not support non-aligned data transfers, we must verify
198      * the request.
199      */
200     (void) AcpiUtShortDivide ((UINT64) Address, Length, NULL, &Remainder);
201     if (Remainder != 0)
202     {
203         return_ACPI_STATUS (AE_AML_ALIGNMENT);
204     }
205 #endif
206 
207     /*
208      * Does the request fit into the cached memory mapping?
209      * Is 1) Address below the current mapping? OR
210      *    2) Address beyond the current mapping?
211      */
212     if ((Address < MemInfo->MappedPhysicalAddress) ||
213         (((UINT64) Address + Length) >
214             ((UINT64)
215             MemInfo->MappedPhysicalAddress + MemInfo->MappedLength)))
216     {
217         /*
218          * The request cannot be resolved by the current memory mapping;
219          * Delete the existing mapping and create a new one.
220          */
221         if (MemInfo->MappedLength)
222         {
223             /* Valid mapping, delete it */
224 
225             AcpiOsUnmapMemory (MemInfo->MappedLogicalAddress,
226                 MemInfo->MappedLength);
227         }
228 
229         /*
230          * October 2009: Attempt to map from the requested address to the
231          * end of the region. However, we will never map more than one
232          * page, nor will we cross a page boundary.
233          */
234         MapLength = (ACPI_SIZE)
235             ((MemInfo->Address + MemInfo->Length) - Address);
236 
237         /*
238          * If mapping the entire remaining portion of the region will cross
239          * a page boundary, just map up to the page boundary, do not cross.
240          * On some systems, crossing a page boundary while mapping regions
241          * can cause warnings if the pages have different attributes
242          * due to resource management.
243          *
244          * This has the added benefit of constraining a single mapping to
245          * one page, which is similar to the original code that used a 4k
246          * maximum window.
247          */
248         PageBoundaryMapLength =
249             ACPI_ROUND_UP (Address, ACPI_DEFAULT_PAGE_SIZE) - Address;
250         if (PageBoundaryMapLength == 0)
251         {
252             PageBoundaryMapLength = ACPI_DEFAULT_PAGE_SIZE;
253         }
254 
255         if (MapLength > PageBoundaryMapLength)
256         {
257             MapLength = PageBoundaryMapLength;
258         }
259 
260         /* Create a new mapping starting at the address given */
261 
262         MemInfo->MappedLogicalAddress = AcpiOsMapMemory (
263             (ACPI_PHYSICAL_ADDRESS) Address, MapLength);
264         if (!MemInfo->MappedLogicalAddress)
265         {
266             ACPI_ERROR ((AE_INFO,
267                 "Could not map memory at 0x%8.8X%8.8X, size %u",
268                 ACPI_FORMAT_NATIVE_UINT (Address), (UINT32) MapLength));
269             MemInfo->MappedLength = 0;
270             return_ACPI_STATUS (AE_NO_MEMORY);
271         }
272 
273         /* Save the physical address and mapping size */
274 
275         MemInfo->MappedPhysicalAddress = Address;
276         MemInfo->MappedLength = MapLength;
277     }
278 
279     /*
280      * Generate a logical pointer corresponding to the address we want to
281      * access
282      */
283     LogicalAddrPtr = MemInfo->MappedLogicalAddress +
284         ((UINT64) Address - (UINT64) MemInfo->MappedPhysicalAddress);
285 
286     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
287         "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n",
288         BitWidth, Function, ACPI_FORMAT_NATIVE_UINT (Address)));
289 
290     /*
291      * Perform the memory read or write
292      *
293      * Note: For machines that do not support non-aligned transfers, the target
294      * address was checked for alignment above. We do not attempt to break the
295      * transfer up into smaller (byte-size) chunks because the AML specifically
296      * asked for a transfer width that the hardware may require.
297      */
298     switch (Function)
299     {
300     case ACPI_READ:
301 
302         *Value = 0;
303         switch (BitWidth)
304         {
305         case 8:
306             *Value = (UINT64) ACPI_GET8 (LogicalAddrPtr);
307             break;
308 
309         case 16:
310             *Value = (UINT64) ACPI_GET16 (LogicalAddrPtr);
311             break;
312 
313         case 32:
314             *Value = (UINT64) ACPI_GET32 (LogicalAddrPtr);
315             break;
316 
317         case 64:
318             *Value = (UINT64) ACPI_GET64 (LogicalAddrPtr);
319             break;
320 
321         default:
322             /* BitWidth was already validated */
323             break;
324         }
325         break;
326 
327     case ACPI_WRITE:
328 
329         switch (BitWidth)
330         {
331         case 8:
332             ACPI_SET8 (LogicalAddrPtr) = (UINT8) *Value;
333             break;
334 
335         case 16:
336             ACPI_SET16 (LogicalAddrPtr) = (UINT16) *Value;
337             break;
338 
339         case 32:
340             ACPI_SET32 ( LogicalAddrPtr) = (UINT32) *Value;
341             break;
342 
343         case 64:
344             ACPI_SET64 (LogicalAddrPtr) = (UINT64) *Value;
345             break;
346 
347         default:
348             /* BitWidth was already validated */
349             break;
350         }
351         break;
352 
353     default:
354         Status = AE_BAD_PARAMETER;
355         break;
356     }
357 
358     return_ACPI_STATUS (Status);
359 }
360 
361 
362 /*******************************************************************************
363  *
364  * FUNCTION:    AcpiExSystemIoSpaceHandler
365  *
366  * PARAMETERS:  Function            - Read or Write operation
367  *              Address             - Where in the space to read or write
368  *              BitWidth            - Field width in bits (8, 16, or 32)
369  *              Value               - Pointer to in or out value
370  *              HandlerContext      - Pointer to Handler's context
371  *              RegionContext       - Pointer to context specific to the
372  *                                    accessed region
373  *
374  * RETURN:      Status
375  *
376  * DESCRIPTION: Handler for the System IO address space (Op Region)
377  *
378  ******************************************************************************/
379 
380 ACPI_STATUS
381 AcpiExSystemIoSpaceHandler (
382     UINT32                  Function,
383     ACPI_PHYSICAL_ADDRESS   Address,
384     UINT32                  BitWidth,
385     UINT64                  *Value,
386     void                    *HandlerContext,
387     void                    *RegionContext)
388 {
389     ACPI_STATUS             Status = AE_OK;
390     UINT32                  Value32;
391 
392 
393     ACPI_FUNCTION_TRACE (ExSystemIoSpaceHandler);
394 
395 
396     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
397         "System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",
398         BitWidth, Function, ACPI_FORMAT_NATIVE_UINT (Address)));
399 
400     /* Decode the function parameter */
401 
402     switch (Function)
403     {
404     case ACPI_READ:
405 
406         Status = AcpiHwReadPort ((ACPI_IO_ADDRESS) Address,
407                     &Value32, BitWidth);
408         *Value = Value32;
409         break;
410 
411     case ACPI_WRITE:
412 
413         Status = AcpiHwWritePort ((ACPI_IO_ADDRESS) Address,
414                     (UINT32) *Value, BitWidth);
415         break;
416 
417     default:
418         Status = AE_BAD_PARAMETER;
419         break;
420     }
421 
422     return_ACPI_STATUS (Status);
423 }
424 
425 
426 /*******************************************************************************
427  *
428  * FUNCTION:    AcpiExPciConfigSpaceHandler
429  *
430  * PARAMETERS:  Function            - Read or Write operation
431  *              Address             - Where in the space to read or write
432  *              BitWidth            - Field width in bits (8, 16, or 32)
433  *              Value               - Pointer to in or out value
434  *              HandlerContext      - Pointer to Handler's context
435  *              RegionContext       - Pointer to context specific to the
436  *                                    accessed region
437  *
438  * RETURN:      Status
439  *
440  * DESCRIPTION: Handler for the PCI Config address space (Op Region)
441  *
442  ******************************************************************************/
443 
444 ACPI_STATUS
445 AcpiExPciConfigSpaceHandler (
446     UINT32                  Function,
447     ACPI_PHYSICAL_ADDRESS   Address,
448     UINT32                  BitWidth,
449     UINT64                  *Value,
450     void                    *HandlerContext,
451     void                    *RegionContext)
452 {
453     ACPI_STATUS             Status = AE_OK;
454     ACPI_PCI_ID             *PciId;
455     UINT16                  PciRegister;
456 
457 
458     ACPI_FUNCTION_TRACE (ExPciConfigSpaceHandler);
459 
460 
461     /*
462      *  The arguments to AcpiOs(Read|Write)PciConfiguration are:
463      *
464      *  PciSegment  is the PCI bus segment range 0-31
465      *  PciBus      is the PCI bus number range 0-255
466      *  PciDevice   is the PCI device number range 0-31
467      *  PciFunction is the PCI device function number
468      *  PciRegister is the Config space register range 0-255 bytes
469      *
470      *  Value - input value for write, output address for read
471      *
472      */
473     PciId       = (ACPI_PCI_ID *) RegionContext;
474     PciRegister = (UINT16) (UINT32) Address;
475 
476     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
477         "Pci-Config %u (%u) Seg(%04x) Bus(%04x) Dev(%04x) Func(%04x) Reg(%04x)\n",
478         Function, BitWidth, PciId->Segment, PciId->Bus, PciId->Device,
479         PciId->Function, PciRegister));
480 
481     switch (Function)
482     {
483     case ACPI_READ:
484 
485         *Value = 0;
486         Status = AcpiOsReadPciConfiguration (PciId, PciRegister,
487                     Value, BitWidth);
488         break;
489 
490     case ACPI_WRITE:
491 
492         Status = AcpiOsWritePciConfiguration (PciId, PciRegister,
493                     *Value, BitWidth);
494         break;
495 
496     default:
497 
498         Status = AE_BAD_PARAMETER;
499         break;
500     }
501 
502     return_ACPI_STATUS (Status);
503 }
504 
505 
506 /*******************************************************************************
507  *
508  * FUNCTION:    AcpiExCmosSpaceHandler
509  *
510  * PARAMETERS:  Function            - Read or Write operation
511  *              Address             - Where in the space to read or write
512  *              BitWidth            - Field width in bits (8, 16, or 32)
513  *              Value               - Pointer to in or out value
514  *              HandlerContext      - Pointer to Handler's context
515  *              RegionContext       - Pointer to context specific to the
516  *                                    accessed region
517  *
518  * RETURN:      Status
519  *
520  * DESCRIPTION: Handler for the CMOS address space (Op Region)
521  *
522  ******************************************************************************/
523 
524 ACPI_STATUS
525 AcpiExCmosSpaceHandler (
526     UINT32                  Function,
527     ACPI_PHYSICAL_ADDRESS   Address,
528     UINT32                  BitWidth,
529     UINT64                  *Value,
530     void                    *HandlerContext,
531     void                    *RegionContext)
532 {
533     ACPI_STATUS             Status = AE_OK;
534 
535 
536     ACPI_FUNCTION_TRACE (ExCmosSpaceHandler);
537 
538 
539     return_ACPI_STATUS (Status);
540 }
541 
542 
543 /*******************************************************************************
544  *
545  * FUNCTION:    AcpiExPciBarSpaceHandler
546  *
547  * PARAMETERS:  Function            - Read or Write operation
548  *              Address             - Where in the space to read or write
549  *              BitWidth            - Field width in bits (8, 16, or 32)
550  *              Value               - Pointer to in or out value
551  *              HandlerContext      - Pointer to Handler's context
552  *              RegionContext       - Pointer to context specific to the
553  *                                    accessed region
554  *
555  * RETURN:      Status
556  *
557  * DESCRIPTION: Handler for the PCI BarTarget address space (Op Region)
558  *
559  ******************************************************************************/
560 
561 ACPI_STATUS
562 AcpiExPciBarSpaceHandler (
563     UINT32                  Function,
564     ACPI_PHYSICAL_ADDRESS   Address,
565     UINT32                  BitWidth,
566     UINT64                  *Value,
567     void                    *HandlerContext,
568     void                    *RegionContext)
569 {
570     ACPI_STATUS             Status = AE_OK;
571 
572 
573     ACPI_FUNCTION_TRACE (ExPciBarSpaceHandler);
574 
575 
576     return_ACPI_STATUS (Status);
577 }
578 
579 
580 /*******************************************************************************
581  *
582  * FUNCTION:    AcpiExDataTableSpaceHandler
583  *
584  * PARAMETERS:  Function            - Read or Write operation
585  *              Address             - Where in the space to read or write
586  *              BitWidth            - Field width in bits (8, 16, or 32)
587  *              Value               - Pointer to in or out value
588  *              HandlerContext      - Pointer to Handler's context
589  *              RegionContext       - Pointer to context specific to the
590  *                                    accessed region
591  *
592  * RETURN:      Status
593  *
594  * DESCRIPTION: Handler for the Data Table address space (Op Region)
595  *
596  ******************************************************************************/
597 
598 ACPI_STATUS
599 AcpiExDataTableSpaceHandler (
600     UINT32                  Function,
601     ACPI_PHYSICAL_ADDRESS   Address,
602     UINT32                  BitWidth,
603     UINT64                  *Value,
604     void                    *HandlerContext,
605     void                    *RegionContext)
606 {
607     ACPI_FUNCTION_TRACE (ExDataTableSpaceHandler);
608 
609 
610     /*
611      * Perform the memory read or write. The BitWidth was already
612      * validated.
613      */
614     switch (Function)
615     {
616     case ACPI_READ:
617 
618         ACPI_MEMCPY (ACPI_CAST_PTR (char, Value), ACPI_PHYSADDR_TO_PTR (Address),
619             ACPI_DIV_8 (BitWidth));
620         break;
621 
622     case ACPI_WRITE:
623 
624         ACPI_MEMCPY (ACPI_PHYSADDR_TO_PTR (Address), ACPI_CAST_PTR (char, Value),
625             ACPI_DIV_8 (BitWidth));
626         break;
627 
628     default:
629 
630         return_ACPI_STATUS (AE_BAD_PARAMETER);
631     }
632 
633     return_ACPI_STATUS (AE_OK);
634 }
635