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