xref: /freebsd/sys/contrib/dev/acpica/components/executer/exregion.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /******************************************************************************
2  *
3  * Module Name: exregion - ACPI default OpRegion (address space) handlers
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2015, 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 <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/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 = (ACPI_SIZE)
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 (Address, MapLength);
193         if (!MemInfo->MappedLogicalAddress)
194         {
195             ACPI_ERROR ((AE_INFO,
196                 "Could not map memory at 0x%8.8X%8.8X, size %u",
197                 ACPI_FORMAT_UINT64 (Address), (UINT32) MapLength));
198             MemInfo->MappedLength = 0;
199             return_ACPI_STATUS (AE_NO_MEMORY);
200         }
201 
202         /* Save the physical address and mapping size */
203 
204         MemInfo->MappedPhysicalAddress = Address;
205         MemInfo->MappedLength = MapLength;
206     }
207 
208     /*
209      * Generate a logical pointer corresponding to the address we want to
210      * access
211      */
212     LogicalAddrPtr = MemInfo->MappedLogicalAddress +
213         ((UINT64) Address - (UINT64) MemInfo->MappedPhysicalAddress);
214 
215     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
216         "System-Memory (width %u) R/W %u Address=%8.8X%8.8X\n",
217         BitWidth, Function, ACPI_FORMAT_UINT64 (Address)));
218 
219     /*
220      * Perform the memory read or write
221      *
222      * Note: For machines that do not support non-aligned transfers, the target
223      * address was checked for alignment above. We do not attempt to break the
224      * transfer up into smaller (byte-size) chunks because the AML specifically
225      * asked for a transfer width that the hardware may require.
226      */
227     switch (Function)
228     {
229     case ACPI_READ:
230 
231         *Value = 0;
232         switch (BitWidth)
233         {
234         case 8:
235 
236             *Value = (UINT64) ACPI_GET8 (LogicalAddrPtr);
237             break;
238 
239         case 16:
240 
241             *Value = (UINT64) ACPI_GET16 (LogicalAddrPtr);
242             break;
243 
244         case 32:
245 
246             *Value = (UINT64) ACPI_GET32 (LogicalAddrPtr);
247             break;
248 
249         case 64:
250 
251             *Value = (UINT64) ACPI_GET64 (LogicalAddrPtr);
252             break;
253 
254         default:
255 
256             /* BitWidth was already validated */
257 
258             break;
259         }
260         break;
261 
262     case ACPI_WRITE:
263 
264         switch (BitWidth)
265         {
266         case 8:
267 
268             ACPI_SET8 (LogicalAddrPtr, *Value);
269             break;
270 
271         case 16:
272 
273             ACPI_SET16 (LogicalAddrPtr, *Value);
274             break;
275 
276         case 32:
277 
278             ACPI_SET32 (LogicalAddrPtr, *Value);
279             break;
280 
281         case 64:
282 
283             ACPI_SET64 (LogicalAddrPtr, *Value);
284             break;
285 
286         default:
287 
288             /* BitWidth was already validated */
289 
290             break;
291         }
292         break;
293 
294     default:
295 
296         Status = AE_BAD_PARAMETER;
297         break;
298     }
299 
300     return_ACPI_STATUS (Status);
301 }
302 
303 
304 /*******************************************************************************
305  *
306  * FUNCTION:    AcpiExSystemIoSpaceHandler
307  *
308  * PARAMETERS:  Function            - Read or Write operation
309  *              Address             - Where in the space to read or write
310  *              BitWidth            - Field width in bits (8, 16, or 32)
311  *              Value               - Pointer to in or out value
312  *              HandlerContext      - Pointer to Handler's context
313  *              RegionContext       - Pointer to context specific to the
314  *                                    accessed region
315  *
316  * RETURN:      Status
317  *
318  * DESCRIPTION: Handler for the System IO address space (Op Region)
319  *
320  ******************************************************************************/
321 
322 ACPI_STATUS
323 AcpiExSystemIoSpaceHandler (
324     UINT32                  Function,
325     ACPI_PHYSICAL_ADDRESS   Address,
326     UINT32                  BitWidth,
327     UINT64                  *Value,
328     void                    *HandlerContext,
329     void                    *RegionContext)
330 {
331     ACPI_STATUS             Status = AE_OK;
332     UINT32                  Value32;
333 
334 
335     ACPI_FUNCTION_TRACE (ExSystemIoSpaceHandler);
336 
337 
338     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
339         "System-IO (width %u) R/W %u Address=%8.8X%8.8X\n",
340         BitWidth, Function, ACPI_FORMAT_UINT64 (Address)));
341 
342     /* Decode the function parameter */
343 
344     switch (Function)
345     {
346     case ACPI_READ:
347 
348         Status = AcpiHwReadPort ((ACPI_IO_ADDRESS) Address,
349                     &Value32, BitWidth);
350         *Value = Value32;
351         break;
352 
353     case ACPI_WRITE:
354 
355         Status = AcpiHwWritePort ((ACPI_IO_ADDRESS) Address,
356                     (UINT32) *Value, BitWidth);
357         break;
358 
359     default:
360 
361         Status = AE_BAD_PARAMETER;
362         break;
363     }
364 
365     return_ACPI_STATUS (Status);
366 }
367 
368 
369 /*******************************************************************************
370  *
371  * FUNCTION:    AcpiExPciConfigSpaceHandler
372  *
373  * PARAMETERS:  Function            - Read or Write operation
374  *              Address             - Where in the space to read or write
375  *              BitWidth            - Field width in bits (8, 16, or 32)
376  *              Value               - Pointer to in or out value
377  *              HandlerContext      - Pointer to Handler's context
378  *              RegionContext       - Pointer to context specific to the
379  *                                    accessed region
380  *
381  * RETURN:      Status
382  *
383  * DESCRIPTION: Handler for the PCI Config address space (Op Region)
384  *
385  ******************************************************************************/
386 
387 ACPI_STATUS
388 AcpiExPciConfigSpaceHandler (
389     UINT32                  Function,
390     ACPI_PHYSICAL_ADDRESS   Address,
391     UINT32                  BitWidth,
392     UINT64                  *Value,
393     void                    *HandlerContext,
394     void                    *RegionContext)
395 {
396     ACPI_STATUS             Status = AE_OK;
397     ACPI_PCI_ID             *PciId;
398     UINT16                  PciRegister;
399 
400 
401     ACPI_FUNCTION_TRACE (ExPciConfigSpaceHandler);
402 
403 
404     /*
405      *  The arguments to AcpiOs(Read|Write)PciConfiguration are:
406      *
407      *  PciSegment  is the PCI bus segment range 0-31
408      *  PciBus      is the PCI bus number range 0-255
409      *  PciDevice   is the PCI device number range 0-31
410      *  PciFunction is the PCI device function number
411      *  PciRegister is the Config space register range 0-255 bytes
412      *
413      *  Value - input value for write, output address for read
414      *
415      */
416     PciId       = (ACPI_PCI_ID *) RegionContext;
417     PciRegister = (UINT16) (UINT32) Address;
418 
419     ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
420         "Pci-Config %u (%u) Seg(%04x) Bus(%04x) Dev(%04x) Func(%04x) Reg(%04x)\n",
421         Function, BitWidth, PciId->Segment, PciId->Bus, PciId->Device,
422         PciId->Function, PciRegister));
423 
424     switch (Function)
425     {
426     case ACPI_READ:
427 
428         *Value = 0;
429         Status = AcpiOsReadPciConfiguration (PciId, PciRegister,
430                     Value, BitWidth);
431         break;
432 
433     case ACPI_WRITE:
434 
435         Status = AcpiOsWritePciConfiguration (PciId, PciRegister,
436                     *Value, BitWidth);
437         break;
438 
439     default:
440 
441         Status = AE_BAD_PARAMETER;
442         break;
443     }
444 
445     return_ACPI_STATUS (Status);
446 }
447 
448 
449 /*******************************************************************************
450  *
451  * FUNCTION:    AcpiExCmosSpaceHandler
452  *
453  * PARAMETERS:  Function            - Read or Write operation
454  *              Address             - Where in the space to read or write
455  *              BitWidth            - Field width in bits (8, 16, or 32)
456  *              Value               - Pointer to in or out value
457  *              HandlerContext      - Pointer to Handler's context
458  *              RegionContext       - Pointer to context specific to the
459  *                                    accessed region
460  *
461  * RETURN:      Status
462  *
463  * DESCRIPTION: Handler for the CMOS address space (Op Region)
464  *
465  ******************************************************************************/
466 
467 ACPI_STATUS
468 AcpiExCmosSpaceHandler (
469     UINT32                  Function,
470     ACPI_PHYSICAL_ADDRESS   Address,
471     UINT32                  BitWidth,
472     UINT64                  *Value,
473     void                    *HandlerContext,
474     void                    *RegionContext)
475 {
476     ACPI_STATUS             Status = AE_OK;
477 
478 
479     ACPI_FUNCTION_TRACE (ExCmosSpaceHandler);
480 
481 
482     return_ACPI_STATUS (Status);
483 }
484 
485 
486 /*******************************************************************************
487  *
488  * FUNCTION:    AcpiExPciBarSpaceHandler
489  *
490  * PARAMETERS:  Function            - Read or Write operation
491  *              Address             - Where in the space to read or write
492  *              BitWidth            - Field width in bits (8, 16, or 32)
493  *              Value               - Pointer to in or out value
494  *              HandlerContext      - Pointer to Handler's context
495  *              RegionContext       - Pointer to context specific to the
496  *                                    accessed region
497  *
498  * RETURN:      Status
499  *
500  * DESCRIPTION: Handler for the PCI BarTarget address space (Op Region)
501  *
502  ******************************************************************************/
503 
504 ACPI_STATUS
505 AcpiExPciBarSpaceHandler (
506     UINT32                  Function,
507     ACPI_PHYSICAL_ADDRESS   Address,
508     UINT32                  BitWidth,
509     UINT64                  *Value,
510     void                    *HandlerContext,
511     void                    *RegionContext)
512 {
513     ACPI_STATUS             Status = AE_OK;
514 
515 
516     ACPI_FUNCTION_TRACE (ExPciBarSpaceHandler);
517 
518 
519     return_ACPI_STATUS (Status);
520 }
521 
522 
523 /*******************************************************************************
524  *
525  * FUNCTION:    AcpiExDataTableSpaceHandler
526  *
527  * PARAMETERS:  Function            - Read or Write operation
528  *              Address             - Where in the space to read or write
529  *              BitWidth            - Field width in bits (8, 16, or 32)
530  *              Value               - Pointer to in or out value
531  *              HandlerContext      - Pointer to Handler's context
532  *              RegionContext       - Pointer to context specific to the
533  *                                    accessed region
534  *
535  * RETURN:      Status
536  *
537  * DESCRIPTION: Handler for the Data Table address space (Op Region)
538  *
539  ******************************************************************************/
540 
541 ACPI_STATUS
542 AcpiExDataTableSpaceHandler (
543     UINT32                  Function,
544     ACPI_PHYSICAL_ADDRESS   Address,
545     UINT32                  BitWidth,
546     UINT64                  *Value,
547     void                    *HandlerContext,
548     void                    *RegionContext)
549 {
550     ACPI_FUNCTION_TRACE (ExDataTableSpaceHandler);
551 
552 
553     /*
554      * Perform the memory read or write. The BitWidth was already
555      * validated.
556      */
557     switch (Function)
558     {
559     case ACPI_READ:
560 
561         memcpy (ACPI_CAST_PTR (char, Value), ACPI_PHYSADDR_TO_PTR (Address),
562             ACPI_DIV_8 (BitWidth));
563         break;
564 
565     case ACPI_WRITE:
566 
567         memcpy (ACPI_PHYSADDR_TO_PTR (Address), ACPI_CAST_PTR (char, Value),
568             ACPI_DIV_8 (BitWidth));
569         break;
570 
571     default:
572 
573         return_ACPI_STATUS (AE_BAD_PARAMETER);
574     }
575 
576     return_ACPI_STATUS (AE_OK);
577 }
578