9579fe4423ce39427fbd8c2bd8bcd5c9225ae08c
[dragonfly.git] / sys / contrib / dev / acpica / source / compiler / aslrestype2.c
1 /******************************************************************************
2  *
3  * Module Name: aslrestype2 - Miscellaneous Large resource descriptors
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 "aslcompiler.h"
45 #include "aslcompiler.y.h"
46 #include "amlcode.h"
47
48 #define _COMPONENT          ACPI_COMPILER
49         ACPI_MODULE_NAME    ("aslrestype2")
50
51 /*
52  * This module contains miscellaneous large resource descriptors:
53  *
54  * Register
55  * Interrupt
56  * VendorLong
57  */
58
59 /*******************************************************************************
60  *
61  * FUNCTION:    RsDoGeneralRegisterDescriptor
62  *
63  * PARAMETERS:  Op                  - Parent resource descriptor parse node
64  *              CurrentByteOffset   - Offset into the resource template AML
65  *                                    buffer (to track references to the desc)
66  *
67  * RETURN:      Completed resource node
68  *
69  * DESCRIPTION: Construct a long "Register" descriptor
70  *
71  ******************************************************************************/
72
73 ASL_RESOURCE_NODE *
74 RsDoGeneralRegisterDescriptor (
75     ACPI_PARSE_OBJECT       *Op,
76     UINT32                  CurrentByteOffset)
77 {
78     AML_RESOURCE            *Descriptor;
79     ACPI_PARSE_OBJECT       *InitializerOp;
80     ASL_RESOURCE_NODE       *Rnode;
81     UINT32                  i;
82
83
84     InitializerOp = Op->Asl.Child;
85     Rnode = RsAllocateResourceNode (sizeof (AML_RESOURCE_GENERIC_REGISTER));
86
87     Descriptor = Rnode->Buffer;
88     Descriptor->GenericReg.DescriptorType = ACPI_RESOURCE_NAME_GENERIC_REGISTER;
89     Descriptor->GenericReg.ResourceLength = 12;
90
91     /* Process all child initialization nodes */
92
93     for (i = 0; InitializerOp; i++)
94     {
95         switch (i)
96         {
97         case 0: /* Address space */
98
99             Descriptor->GenericReg.AddressSpaceId = (UINT8) InitializerOp->Asl.Value.Integer;
100             RsCreateByteField (InitializerOp, ACPI_RESTAG_ADDRESSSPACE,
101                 CurrentByteOffset + ASL_RESDESC_OFFSET (GenericReg.AddressSpaceId));
102            break;
103
104         case 1: /* Register Bit Width */
105
106             Descriptor->GenericReg.BitWidth = (UINT8) InitializerOp->Asl.Value.Integer;
107             RsCreateByteField (InitializerOp, ACPI_RESTAG_REGISTERBITWIDTH,
108                 CurrentByteOffset + ASL_RESDESC_OFFSET (GenericReg.BitWidth));
109             break;
110
111         case 2: /* Register Bit Offset */
112
113             Descriptor->GenericReg.BitOffset = (UINT8) InitializerOp->Asl.Value.Integer;
114             RsCreateByteField (InitializerOp, ACPI_RESTAG_REGISTERBITOFFSET,
115                 CurrentByteOffset + ASL_RESDESC_OFFSET (GenericReg.BitOffset));
116             break;
117
118         case 3: /* Register Address */
119
120             Descriptor->GenericReg.Address = InitializerOp->Asl.Value.Integer;
121             RsCreateQwordField (InitializerOp, ACPI_RESTAG_ADDRESS,
122                 CurrentByteOffset + ASL_RESDESC_OFFSET (GenericReg.Address));
123             break;
124
125         case 4: /* Access Size (ACPI 3.0) */
126
127             Descriptor->GenericReg.AccessSize = (UINT8) InitializerOp->Asl.Value.Integer;
128             RsCreateByteField (InitializerOp, ACPI_RESTAG_ACCESSSIZE,
129                 CurrentByteOffset + ASL_RESDESC_OFFSET (GenericReg.AccessSize));
130
131             if (Descriptor->GenericReg.AccessSize > AML_FIELD_ACCESS_QWORD)
132             {
133                 AslError (ASL_ERROR, ASL_MSG_INVALID_ACCESS_SIZE,
134                     InitializerOp, NULL);
135             }
136             break;
137
138         case 5: /* ResourceTag (ACPI 3.0b) */
139
140             UtAttachNamepathToOwner (Op, InitializerOp);
141             break;
142
143         default:
144
145             AslError (ASL_ERROR, ASL_MSG_RESOURCE_LIST, InitializerOp, NULL);
146             break;
147         }
148
149         InitializerOp = RsCompleteNodeAndGetNext (InitializerOp);
150     }
151     return (Rnode);
152 }
153
154
155 /*******************************************************************************
156  *
157  * FUNCTION:    RsDoInterruptDescriptor
158  *
159  * PARAMETERS:  Op                  - Parent resource descriptor parse node
160  *              CurrentByteOffset   - Offset into the resource template AML
161  *                                    buffer (to track references to the desc)
162  *
163  * RETURN:      Completed resource node
164  *
165  * DESCRIPTION: Construct a long "Interrupt" descriptor
166  *
167  ******************************************************************************/
168
169 ASL_RESOURCE_NODE *
170 RsDoInterruptDescriptor (
171     ACPI_PARSE_OBJECT       *Op,
172     UINT32                  CurrentByteOffset)
173 {
174     AML_RESOURCE            *Descriptor;
175     AML_RESOURCE            *Rover = NULL;
176     ACPI_PARSE_OBJECT       *InitializerOp;
177     ASL_RESOURCE_NODE       *Rnode;
178     UINT16                  StringLength = 0;
179     UINT32                  OptionIndex = 0;
180     UINT32                  i;
181     BOOLEAN                 HasResSourceIndex = FALSE;
182     UINT8                   ResSourceIndex = 0;
183     UINT8                   *ResSourceString = NULL;
184
185
186     InitializerOp = Op->Asl.Child;
187     StringLength = RsGetStringDataLength (InitializerOp);
188
189     /* Count the interrupt numbers */
190
191     for (i = 0; InitializerOp; i++)
192     {
193         InitializerOp = ASL_GET_PEER_NODE (InitializerOp);
194
195         if (i <= 6)
196         {
197             if (i == 3 &&
198                 InitializerOp->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG)
199             {
200                 /*
201                  * ResourceSourceIndex was specified, always make room for
202                  * it, even if the ResourceSource was omitted.
203                  */
204                 OptionIndex++;
205             }
206
207             continue;
208         }
209
210         OptionIndex += 4;
211     }
212
213     InitializerOp = Op->Asl.Child;
214     Rnode = RsAllocateResourceNode (sizeof (AML_RESOURCE_EXTENDED_IRQ) +
215                 1 + OptionIndex + StringLength);
216
217     Descriptor = Rnode->Buffer;
218     Descriptor->ExtendedIrq.DescriptorType  = ACPI_RESOURCE_NAME_EXTENDED_IRQ;
219
220     /*
221      * Initial descriptor length -- may be enlarged if there are
222      * optional fields present
223      */
224     Descriptor->ExtendedIrq.ResourceLength  = 2;  /* Flags and table length byte */
225     Descriptor->ExtendedIrq.InterruptCount  = 0;
226
227     Rover = ACPI_CAST_PTR (AML_RESOURCE,
228                 (&(Descriptor->ExtendedIrq.Interrupts[0])));
229
230     /* Process all child initialization nodes */
231
232     for (i = 0; InitializerOp; i++)
233     {
234         switch (i)
235         {
236         case 0: /* Resource Usage (Default: consumer (1) */
237
238             RsSetFlagBits (&Descriptor->ExtendedIrq.Flags, InitializerOp, 0, 1);
239             break;
240
241         case 1: /* Interrupt Type (or Mode - edge/level) */
242
243             RsSetFlagBits (&Descriptor->ExtendedIrq.Flags, InitializerOp, 1, 0);
244             RsCreateBitField (InitializerOp, ACPI_RESTAG_INTERRUPTTYPE,
245                 CurrentByteOffset + ASL_RESDESC_OFFSET (ExtendedIrq.Flags), 1);
246             break;
247
248         case 2: /* Interrupt Level (or Polarity - Active high/low) */
249
250             RsSetFlagBits (&Descriptor->ExtendedIrq.Flags, InitializerOp, 2, 0);
251             RsCreateBitField (InitializerOp, ACPI_RESTAG_INTERRUPTLEVEL,
252                 CurrentByteOffset + ASL_RESDESC_OFFSET (ExtendedIrq.Flags), 2);
253             break;
254
255         case 3: /* Share Type - Default: exclusive (0) */
256
257             RsSetFlagBits (&Descriptor->ExtendedIrq.Flags, InitializerOp, 3, 0);
258             RsCreateBitField (InitializerOp, ACPI_RESTAG_INTERRUPTSHARE,
259                 CurrentByteOffset + ASL_RESDESC_OFFSET (ExtendedIrq.Flags), 3);
260             break;
261
262         case 4: /* ResSourceIndex [Optional Field - BYTE] */
263
264             if (InitializerOp->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG)
265             {
266                 HasResSourceIndex = TRUE;
267                 ResSourceIndex = (UINT8) InitializerOp->Asl.Value.Integer;
268             }
269             break;
270
271         case 5: /* ResSource [Optional Field - STRING] */
272
273             if ((InitializerOp->Asl.ParseOpcode != PARSEOP_DEFAULT_ARG) &&
274                 (InitializerOp->Asl.Value.String))
275             {
276                 if (StringLength)
277                 {
278                     ResSourceString = (UINT8 *) InitializerOp->Asl.Value.String;
279                 }
280
281                 /* ResourceSourceIndex must also be valid */
282
283                 if (!HasResSourceIndex)
284                 {
285                     AslError (ASL_ERROR, ASL_MSG_RESOURCE_INDEX,
286                         InitializerOp, NULL);
287                 }
288             }
289
290 #if 0
291             /*
292              * Not a valid ResourceSource, ResourceSourceIndex must also
293              * be invalid
294              */
295             else if (HasResSourceIndex)
296             {
297                 AslError (ASL_ERROR, ASL_MSG_RESOURCE_SOURCE,
298                     InitializerOp, NULL);
299             }
300 #endif
301             break;
302
303         case 6: /* ResourceTag */
304
305             UtAttachNamepathToOwner (Op, InitializerOp);
306             break;
307
308         default:
309             /*
310              * Interrupt Numbers come through here, repeatedly
311              */
312
313             /* Maximum 255 interrupts allowed for this descriptor */
314
315             if (Descriptor->ExtendedIrq.InterruptCount == 255)
316             {
317                 AslError (ASL_ERROR, ASL_MSG_EX_INTERRUPT_LIST,
318                     InitializerOp, NULL);
319                 return (Rnode);
320             }
321
322             /* Each interrupt number must be a 32-bit value */
323
324             if (InitializerOp->Asl.Value.Integer > ACPI_UINT32_MAX)
325             {
326                 AslError (ASL_ERROR, ASL_MSG_EX_INTERRUPT_NUMBER,
327                     InitializerOp, NULL);
328             }
329
330             /* Save the integer and move pointer to the next one */
331
332             Rover->DwordItem = (UINT32) InitializerOp->Asl.Value.Integer;
333             Rover = ACPI_ADD_PTR (AML_RESOURCE, &(Rover->DwordItem), 4);
334             Descriptor->ExtendedIrq.InterruptCount++;
335             Descriptor->ExtendedIrq.ResourceLength += 4;
336
337             /* Case 7: First interrupt number in list */
338
339             if (i == 7)
340             {
341                 if (InitializerOp->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
342                 {
343                     /* Must be at least one interrupt */
344
345                     AslError (ASL_ERROR, ASL_MSG_EX_INTERRUPT_LIST_MIN,
346                         InitializerOp, NULL);
347                 }
348
349                 /* Check now for duplicates in list */
350
351                 RsCheckListForDuplicates (InitializerOp);
352
353                 /* Create a named field at the start of the list */
354
355                 RsCreateDwordField (InitializerOp, ACPI_RESTAG_INTERRUPT,
356                     CurrentByteOffset +
357                     ASL_RESDESC_OFFSET (ExtendedIrq.Interrupts[0]));
358             }
359         }
360
361         InitializerOp = RsCompleteNodeAndGetNext (InitializerOp);
362     }
363
364
365     /* Add optional ResSourceIndex if present */
366
367     if (HasResSourceIndex)
368     {
369         Rover->ByteItem = ResSourceIndex;
370         Rover = ACPI_ADD_PTR (AML_RESOURCE, &(Rover->ByteItem), 1);
371         Descriptor->ExtendedIrq.ResourceLength += 1;
372     }
373
374     /* Add optional ResSource string if present */
375
376     if (StringLength && ResSourceString)
377     {
378
379         strcpy ((char *) Rover, (char *) ResSourceString);
380         Rover = ACPI_ADD_PTR (
381                     AML_RESOURCE, &(Rover->ByteItem), StringLength);
382
383         Descriptor->ExtendedIrq.ResourceLength = (UINT16)
384             (Descriptor->ExtendedIrq.ResourceLength + StringLength);
385     }
386
387     Rnode->BufferLength = (ASL_RESDESC_OFFSET (ExtendedIrq.Interrupts[0]) -
388                            ASL_RESDESC_OFFSET (ExtendedIrq.DescriptorType))
389                            + OptionIndex + StringLength;
390     return (Rnode);
391 }
392
393
394 /*******************************************************************************
395  *
396  * FUNCTION:    RsDoVendorLargeDescriptor
397  *
398  * PARAMETERS:  Op                  - Parent resource descriptor parse node
399  *              CurrentByteOffset   - Offset into the resource template AML
400  *                                    buffer (to track references to the desc)
401  *
402  * RETURN:      Completed resource node
403  *
404  * DESCRIPTION: Construct a long "VendorLong" descriptor
405  *
406  ******************************************************************************/
407
408 ASL_RESOURCE_NODE *
409 RsDoVendorLargeDescriptor (
410     ACPI_PARSE_OBJECT       *Op,
411     UINT32                  CurrentByteOffset)
412 {
413     AML_RESOURCE            *Descriptor;
414     ACPI_PARSE_OBJECT       *InitializerOp;
415     ASL_RESOURCE_NODE       *Rnode;
416     UINT8                   *VendorData;
417     UINT32                  i;
418
419
420     /* Count the number of data bytes */
421
422     InitializerOp = Op->Asl.Child;
423     InitializerOp = RsCompleteNodeAndGetNext (InitializerOp);
424
425     for (i = 0; InitializerOp; i++)
426     {
427         if (InitializerOp->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
428         {
429             break;
430         }
431         InitializerOp = InitializerOp->Asl.Next;
432     }
433
434     InitializerOp = Op->Asl.Child;
435     InitializerOp = RsCompleteNodeAndGetNext (InitializerOp);
436     Rnode = RsAllocateResourceNode (sizeof (AML_RESOURCE_VENDOR_LARGE) + i);
437
438     Descriptor = Rnode->Buffer;
439     Descriptor->VendorLarge.DescriptorType  = ACPI_RESOURCE_NAME_VENDOR_LARGE;
440     Descriptor->VendorLarge.ResourceLength = (UINT16) i;
441
442     /* Point to end-of-descriptor for vendor data */
443
444     VendorData = ((UINT8 *) Descriptor) + sizeof (AML_RESOURCE_LARGE_HEADER);
445
446     /* Process all child initialization nodes */
447
448     for (i = 0; InitializerOp; i++)
449     {
450         if (InitializerOp->Asl.ParseOpcode == PARSEOP_DEFAULT_ARG)
451         {
452             break;
453         }
454
455         VendorData[i] = (UINT8) InitializerOp->Asl.Value.Integer;
456         InitializerOp = RsCompleteNodeAndGetNext (InitializerOp);
457     }
458
459     return (Rnode);
460 }