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