1 /******************************************************************************
2  *
3  * Module Name: aeregion - Handler for operation regions
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2021, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include "aecommon.h"
153 
154 #define _COMPONENT          ACPI_TOOLS
155         ACPI_MODULE_NAME    ("aeregion")
156 
157 
158 static AE_DEBUG_REGIONS     AeRegions;
159 
160 
161 /******************************************************************************
162  *
163  * FUNCTION:    AeRegionHandler
164  *
165  * PARAMETERS:  Standard region handler parameters
166  *
167  * RETURN:      Status
168  *
169  * DESCRIPTION: Test handler - Handles some dummy regions via memory that can
170  *              be manipulated in Ring 3. Simulates actual reads and writes.
171  *
172  *****************************************************************************/
173 
174 ACPI_STATUS
175 AeRegionHandler (
176     UINT32                  Function,
177     ACPI_PHYSICAL_ADDRESS   Address,
178     UINT32                  BitWidth,
179     UINT64                  *Value,
180     void                    *HandlerContext,
181     void                    *RegionContext)
182 {
183 
184     ACPI_OPERAND_OBJECT     *RegionObject = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, RegionContext);
185     UINT8                   *Buffer = ACPI_CAST_PTR (UINT8, Value);
186     UINT8                   *OldBuffer;
187     UINT8                   *NewBuffer;
188     ACPI_PHYSICAL_ADDRESS   BaseAddress;
189     ACPI_PHYSICAL_ADDRESS   BaseAddressEnd;
190     ACPI_PHYSICAL_ADDRESS   RegionAddress;
191     ACPI_PHYSICAL_ADDRESS   RegionAddressEnd;
192     UINT32                  Length;
193     UINT8                   DataLength;
194     UINT8                   *DataBuffer;
195     BOOLEAN                 BufferExists;
196     BOOLEAN                 BufferResize;
197     AE_REGION               *RegionElement;
198     void                    *BufferValue;
199     ACPI_STATUS             Status;
200     UINT32                  ByteWidth;
201     UINT32                  RegionLength;
202     UINT32                  i;
203     UINT8                   SpaceId;
204     ACPI_CONNECTION_INFO    *MyContext;
205     UINT32                  Value1;
206     UINT32                  Value2;
207     ACPI_RESOURCE           *Resource;
208     char                    Uuid[ACPI_PRM_INPUT_BUFFER_SIZE + 1];
209 
210 
211     ACPI_FUNCTION_NAME (AeRegionHandler);
212 
213 
214     /* If the object is not a region, simply return */
215 
216     if (RegionObject->Region.Type != ACPI_TYPE_REGION)
217     {
218         return (AE_OK);
219     }
220 
221     /* Check that we actually got back our context parameter */
222 
223     if (HandlerContext != &AeMyContext)
224     {
225         AcpiOsPrintf (
226             "Region handler received incorrect context %p, should be %p\n",
227             HandlerContext, &AeMyContext);
228     }
229 
230     MyContext = ACPI_CAST_PTR (ACPI_CONNECTION_INFO, HandlerContext);
231 
232     /*
233      * Find the region's address space and length before searching
234      * the linked list.
235      */
236     BaseAddress = RegionObject->Region.Address;
237     Length = RegionObject->Region.Length;
238     SpaceId = RegionObject->Region.SpaceId;
239 
240     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
241         "Operation Region request on %s at 0x%X, BitWidth 0x%X, RegionLength 0x%X\n",
242         AcpiUtGetRegionName (RegionObject->Region.SpaceId),
243         (UINT32) Address, BitWidth, (UINT32) Length));
244 
245     /*
246      * Region support can be disabled with the -do option.
247      * We use this to support dynamically loaded tables where we pass a valid
248      * address to the AML.
249      */
250     if (AcpiGbl_DbOpt_NoRegionSupport)
251     {
252         BufferValue = ACPI_TO_POINTER (Address);
253         ByteWidth = (BitWidth / 8);
254 
255         if (BitWidth % 8)
256         {
257             ByteWidth += 1;
258         }
259         goto DoFunction;
260     }
261 
262     switch (SpaceId)
263     {
264     case ACPI_ADR_SPACE_SYSTEM_IO:
265         /*
266          * For I/O space, exercise the port validation
267          * Note: ReadPort currently always returns all ones, length=BitLength
268          */
269         switch (Function & ACPI_IO_MASK)
270         {
271         case ACPI_READ:
272 
273             if (BitWidth == 64)
274             {
275                 /* Split the 64-bit request into two 32-bit requests */
276 
277                 Status = AcpiHwReadPort (Address, &Value1, 32);
278                 ACPI_CHECK_OK (AcpiHwReadPort, Status);
279                 Status = AcpiHwReadPort (Address+4, &Value2, 32);
280                 ACPI_CHECK_OK (AcpiHwReadPort, Status);
281 
282                 *Value = Value1 | ((UINT64) Value2 << 32);
283             }
284             else
285             {
286                 Status = AcpiHwReadPort (Address, &Value1, BitWidth);
287                 ACPI_CHECK_OK (AcpiHwReadPort, Status);
288                 *Value = (UINT64) Value1;
289             }
290             break;
291 
292         case ACPI_WRITE:
293 
294             if (BitWidth == 64)
295             {
296                 /* Split the 64-bit request into two 32-bit requests */
297 
298                 Status = AcpiHwWritePort (Address, ACPI_LODWORD (*Value), 32);
299                 ACPI_CHECK_OK (AcpiHwWritePort, Status);
300                 Status = AcpiHwWritePort (Address+4, ACPI_HIDWORD (*Value), 32);
301                 ACPI_CHECK_OK (AcpiHwWritePort, Status);
302             }
303             else
304             {
305                 Status = AcpiHwWritePort (Address, (UINT32) *Value, BitWidth);
306                 ACPI_CHECK_OK (AcpiHwWritePort, Status);
307             }
308             break;
309 
310         default:
311 
312             Status = AE_BAD_PARAMETER;
313             break;
314         }
315 
316         if (ACPI_FAILURE (Status))
317         {
318             return (Status);
319         }
320 
321         /* Now go ahead and simulate the hardware */
322         break;
323 
324     /*
325      * SMBus and GenericSerialBus support the various bidirectional
326      * protocols.
327      */
328     case ACPI_ADR_SPACE_SMBUS:
329     case ACPI_ADR_SPACE_GSBUS:  /* ACPI 5.0 */
330 
331         Status = AcpiExGetProtocolBufferLength ((Function >> 16), &Length);
332         if (ACPI_FAILURE (Status))
333         {
334             AcpiOsPrintf ("AcpiExec: Invalid SMbus/GSbus protocol ID: 0x%X\n",
335                 (Function >> 16));
336             return (Status);
337         }
338 
339         /* Adjust for fixed SMBus buffer size */
340 
341         if ((SpaceId == ACPI_ADR_SPACE_SMBUS) &&
342             (Length > ACPI_SMBUS_DATA_SIZE))
343         {
344             Length = ACPI_SMBUS_DATA_SIZE; /* SMBus buffer is fixed-length */
345         }
346 
347         if (AcpiGbl_DisplayRegionAccess)
348         {
349             AcpiOsPrintf ("AcpiExec: %s "
350                 "%s: Attr %X Addr %.4X BaseAddr %.4X Length %.2X BitWidth %X BufLen %X\n",
351                 AcpiUtGetRegionName (SpaceId),
352                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
353                 (UINT32) (Function >> 16),
354                 (UINT32) Address, (UINT32) BaseAddress,
355                 Length, BitWidth, Buffer[1]);
356 
357             /* GenericSerialBus has a Connection() parameter */
358 
359             if ((SpaceId == ACPI_ADR_SPACE_GSBUS) && MyContext)
360             {
361                 Status = AcpiBufferToResource (MyContext->Connection,
362                     MyContext->Length, &Resource);
363                 if (ACPI_SUCCESS (Status))
364                 {
365                     ACPI_FREE (Resource);
366                 }
367 
368                 AcpiOsPrintf (" [AccessLength %.2X Connection %p]",
369                     MyContext->AccessLength, MyContext->Connection);
370             }
371 
372             AcpiOsPrintf ("\n");
373         }
374 
375         DataBuffer = &Buffer[2];
376         DataLength = (UINT8) Length;
377 
378         /* Setup the return buffer. Note: ASLTS depends on these fill values */
379 
380         if (Length == ACPI_MAX_GSBUS_DATA_SIZE)
381         {
382             DataLength = 0x20; /* For ASLTS only */
383         }
384 
385         for (i = 0; i < Length; i++)
386         {
387             DataBuffer[i] = (UINT8) (0xA0 + i);
388         }
389 
390         Buffer[0] = 0;                  /* Return Status, OK */
391         Buffer[1] = DataLength;         /* Length of valid data */
392         return (AE_OK);
393 
394     case ACPI_ADR_SPACE_IPMI: /* ACPI 4.0 */
395 
396         if (AcpiGbl_DisplayRegionAccess)
397         {
398             AcpiOsPrintf ("AcpiExec: IPMI "
399                 "%s: Attr %X Addr %.4X BaseAddr %.4X Len %.2X Width %X BufLen %X\n",
400                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
401                 (UINT32) (Function >> 16), (UINT32) Address, (UINT32) BaseAddress,
402                 Length, BitWidth, Buffer[1]);
403         }
404 
405         /*
406          * Regardless of a READ or WRITE, this handler is passed a 66-byte
407          * buffer in which to return the IPMI status/length/data.
408          *
409          * Return some example data to show use of the bidirectional buffer
410          */
411         Buffer[0] = 0;                      /* Status byte */
412         Buffer[1] = ACPI_IPMI_DATA_SIZE;    /* Return buffer data length */
413         Buffer[2] = 0;                      /* Completion code */
414         Buffer[3] = 0;                      /* Reserved */
415 
416         /*
417          * Fill the 66-byte buffer with the return data.
418          * Note: ASLTS depends on these fill values.
419          */
420         for (i = 4; i < ACPI_IPMI_BUFFER_SIZE; i++)
421         {
422             Buffer[i] = (UINT8) (i);
423         }
424         return (AE_OK);
425 
426     /*
427      * GPIO has some special semantics:
428      * 1) Address is the pin number index into the Connection() pin list
429      * 2) BitWidth is the actual number of bits (pins) defined by the field
430      */
431     case ACPI_ADR_SPACE_GPIO: /* ACPI 5.0 */
432 
433         if (AcpiGbl_DisplayRegionAccess)
434         {
435             AcpiOsPrintf ("AcpiExec: GPIO "
436                 "%s: Address %.4X Length %X BitWidth %X Conn %p\n",
437                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
438                 (UINT32) Address, Length, BitWidth, MyContext->Connection);
439         }
440 
441         /* Now perform the "normal" SystemMemory handling, for AcpiExec only */
442         break;
443 
444     /*
445      * PCC operation region will write the entire subspace's data and expect
446      * a response from the hardware. For acpiexec, we'll fill the buffer with
447      * default values. Note: ASLTS will depend on these values.
448      */
449     case ACPI_ADR_SPACE_PLATFORM_COMM: /* ACPI 6.3 */
450 
451         if (AcpiGbl_DisplayRegionAccess)
452         {
453             AcpiOsPrintf ("AcpiExec: PCC Write : Addr %.4X Width %X\n",
454                 (UINT32) Address, BitWidth);
455         }
456         for (i = 0; i < Length; ++i)
457         {
458             Buffer[i] = (UINT8) i;
459         }
460         return (AE_OK);
461 
462     case ACPI_ADR_SPACE_PLATFORM_RT:
463 
464         AcpiOsPrintf ("Acpiexec: PRM %s invoked\n",
465             (Function & ACPI_IO_MASK) ? "Write" : "Read ");
466 
467         if ((Function & ACPI_IO_MASK) == ACPI_WRITE)
468         {
469             AcpiUtConvertUuidToString((char *) Buffer + 10, Uuid);
470             AcpiOsPrintf ("Mode: %u GUID: %s\n", Buffer[0], Uuid);
471         }
472 
473         /* Unpack the input buffer and print the contents for debug */
474 
475         break;
476 
477     default:
478         break;
479     }
480 
481     /*
482      * Search through the linked list for this region's buffer
483      */
484     BufferExists = FALSE;
485     BufferResize = FALSE;
486     RegionElement = AeRegions.RegionList;
487 
488     if (AeRegions.NumberOfRegions)
489     {
490         BaseAddressEnd = BaseAddress + Length - 1;
491         while (!BufferExists && RegionElement)
492         {
493             RegionAddress = RegionElement->Address;
494             RegionAddressEnd = RegionElement->Address + RegionElement->Length - 1;
495             RegionLength = RegionElement->Length;
496 
497             /*
498              * Overlapping Region Support
499              *
500              * While searching through the region buffer list, determine if an
501              * overlap exists between the requested buffer space and the current
502              * RegionElement space. If there is an overlap then replace the old
503              * buffer with a new buffer of increased size before continuing to
504              * do the read or write
505              */
506             if (RegionElement->SpaceId != SpaceId ||
507                 BaseAddressEnd < RegionAddress ||
508                 BaseAddress > RegionAddressEnd)
509             {
510                 /*
511                  * Requested buffer is outside of the current RegionElement
512                  * bounds
513                  */
514                 RegionElement = RegionElement->NextRegion;
515             }
516             else
517             {
518                 /*
519                  * Some amount of buffer space sharing exists. There are 4 cases
520                  * to consider:
521                  *
522                  * 1. Right overlap
523                  * 2. Left overlap
524                  * 3. Left and right overlap
525                  * 4. Fully contained - no resizing required
526                  */
527                 BufferExists = TRUE;
528 
529                 if ((BaseAddress >= RegionAddress) &&
530                     (BaseAddress <= RegionAddressEnd) &&
531                     (BaseAddressEnd > RegionAddressEnd))
532                 {
533                     /* Right overlap */
534 
535                     RegionElement->Length = (UINT32) (BaseAddress -
536                         RegionAddress + Length);
537                     BufferResize = TRUE;
538                 }
539 
540                 else if ((BaseAddressEnd >= RegionAddress) &&
541                          (BaseAddressEnd <= RegionAddressEnd) &&
542                          (BaseAddress < RegionAddress))
543                 {
544                     /* Left overlap */
545 
546                     RegionElement->Address = BaseAddress;
547                     RegionElement->Length = (UINT32) (RegionAddress -
548                         BaseAddress + RegionElement->Length);
549                     BufferResize = TRUE;
550                 }
551 
552                 else if ((BaseAddress < RegionAddress) &&
553                          (BaseAddressEnd > RegionAddressEnd))
554                 {
555                     /* Left and right overlap */
556 
557                     RegionElement->Address = BaseAddress;
558                     RegionElement->Length = Length;
559                     BufferResize = TRUE;
560                 }
561 
562                 /*
563                  * only remaining case is fully contained for which we don't
564                  * need to do anything
565                  */
566                 if (BufferResize)
567                 {
568                     NewBuffer = AcpiOsAllocate (RegionElement->Length);
569                     if (!NewBuffer)
570                     {
571                         return (AE_NO_MEMORY);
572                     }
573 
574                     OldBuffer = RegionElement->Buffer;
575                     RegionElement->Buffer = NewBuffer;
576                     NewBuffer = NULL;
577 
578                     /* Initialize the region with the default fill value */
579 
580                     memset (RegionElement->Buffer,
581                         AcpiGbl_RegionFillValue, RegionElement->Length);
582 
583                     /*
584                      * Get BufferValue to point (within the new buffer) to the
585                      * base address of the old buffer
586                      */
587                     BufferValue = (UINT8 *) RegionElement->Buffer +
588                         (UINT64) RegionAddress -
589                         (UINT64) RegionElement->Address;
590 
591                     /*
592                      * Copy the old buffer to its same location within the new
593                      * buffer
594                      */
595                     memcpy (BufferValue, OldBuffer, RegionLength);
596                     AcpiOsFree (OldBuffer);
597                 }
598             }
599         }
600     }
601 
602     /*
603      * If the Region buffer does not exist, create it now
604      */
605     if (!BufferExists)
606     {
607         /* Do the memory allocations first */
608 
609         RegionElement = AcpiOsAllocate (sizeof (AE_REGION));
610         if (!RegionElement)
611         {
612             return (AE_NO_MEMORY);
613         }
614 
615         RegionElement->Buffer = AcpiOsAllocate (Length);
616         if (!RegionElement->Buffer)
617         {
618             AcpiOsFree (RegionElement);
619             return (AE_NO_MEMORY);
620         }
621 
622         /* Initialize the region with the default fill value */
623 
624         memset (RegionElement->Buffer, AcpiGbl_RegionFillValue, Length);
625 
626         RegionElement->Address      = BaseAddress;
627         RegionElement->Length       = Length;
628         RegionElement->SpaceId      = SpaceId;
629         RegionElement->NextRegion   = NULL;
630 
631         /*
632          * Increment the number of regions and put this one
633          * at the head of the list as it will probably get accessed
634          * more often anyway.
635          */
636         AeRegions.NumberOfRegions += 1;
637 
638         if (AeRegions.RegionList)
639         {
640             RegionElement->NextRegion = AeRegions.RegionList;
641         }
642 
643         AeRegions.RegionList = RegionElement;
644     }
645 
646     /* Calculate the size of the memory copy */
647 
648     ByteWidth = (BitWidth / 8);
649     if (BitWidth % 8)
650     {
651         ByteWidth += 1;
652     }
653 
654     /*
655      * The buffer exists and is pointed to by RegionElement.
656      * We now need to verify the request is valid and perform the operation.
657      *
658      * NOTE: RegionElement->Length is in bytes, therefore it we compare against
659      * ByteWidth (see above)
660      */
661     if ((RegionObject->Region.SpaceId != ACPI_ADR_SPACE_GPIO) &&
662         ((UINT64) Address + ByteWidth) >
663         ((UINT64)(RegionElement->Address) + RegionElement->Length))
664     {
665         ACPI_WARNING ((AE_INFO,
666             "Request on [%4.4s] is beyond region limit "
667             "Req-0x%X+0x%X, Base=0x%X, Len-0x%X",
668             (RegionObject->Region.Node)->Name.Ascii, (UINT32) Address,
669             ByteWidth, (UINT32)(RegionElement->Address),
670             RegionElement->Length));
671 
672         return (AE_AML_REGION_LIMIT);
673     }
674 
675     /*
676      * Get BufferValue to point to the "address" in the buffer
677      */
678     BufferValue = ((UINT8 *) RegionElement->Buffer +
679         ((UINT64) Address - (UINT64) RegionElement->Address));
680 
681 DoFunction:
682     /*
683      * Perform a read or write to the buffer space
684      */
685     switch (Function)
686     {
687     case ACPI_READ:
688         /*
689          * Set the pointer Value to whatever is in the buffer
690          */
691         memcpy (Value, BufferValue, ByteWidth);
692         break;
693 
694     case ACPI_WRITE:
695         /*
696          * Write the contents of Value to the buffer
697          */
698         memcpy (BufferValue, Value, ByteWidth);
699         break;
700 
701     default:
702 
703         return (AE_BAD_PARAMETER);
704     }
705 
706     if (AcpiGbl_DisplayRegionAccess)
707     {
708         switch (SpaceId)
709         {
710         case ACPI_ADR_SPACE_SYSTEM_MEMORY:
711 
712             AcpiOsPrintf ("AcpiExec: SystemMemory "
713                 "%s: Val %.8X Addr %.4X BitWidth %X [REGION: BaseAddr %.4X Len %.2X]\n",
714                 (Function & ACPI_IO_MASK) ? "Write" : "Read ",
715                 (UINT32) *Value, (UINT32) Address, BitWidth, (UINT32) BaseAddress, Length);
716             break;
717 
718         case ACPI_ADR_SPACE_GSBUS:
719 
720             AcpiOsPrintf ("AcpiExec: GenericSerialBus\n");
721             break;
722 
723         case ACPI_ADR_SPACE_GPIO:   /* ACPI 5.0 */
724 
725             /* This space is required to always be ByteAcc */
726 
727             Status = AcpiBufferToResource (MyContext->Connection,
728                 MyContext->Length, &Resource);
729 
730             AcpiOsPrintf ("AcpiExec: GeneralPurposeIo "
731                 "%s: %.8X Addr %.4X BaseAddr %.4X Length %.2X "
732                 "BitWidth %X AccLen %.2X Conn %p\n",
733                 (Function & ACPI_IO_MASK) ? "Write" : "Read ", (UINT32) *Value,
734                 (UINT32) Address, (UINT32) BaseAddress, Length, BitWidth,
735                 MyContext->AccessLength, MyContext->Connection);
736             if (ACPI_SUCCESS (Status))
737             {
738                 ACPI_FREE (Resource);
739             }
740             break;
741 
742         default:
743 
744             AcpiOsPrintf ("AcpiExec: Region access on SpaceId %2.2X\n", SpaceId);
745             break;
746         }
747     }
748 
749     return (AE_OK);
750 }
751