]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/dev/acpica/common/dmrestag.c
Update libarchive to 3.0.3
[FreeBSD/FreeBSD.git] / sys / contrib / dev / acpica / common / dmrestag.c
1 /******************************************************************************
2  *
3  * Module Name: dmrestag - Add tags to resource descriptors (Application-level)
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2012, 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
45 #include <contrib/dev/acpica/include/acpi.h>
46 #include <contrib/dev/acpica/include/accommon.h>
47 #include <contrib/dev/acpica/include/acparser.h>
48 #include <contrib/dev/acpica/include/acdisasm.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
50 #include <contrib/dev/acpica/include/amlcode.h>
51
52 /* This module used for application-level code only */
53
54 #define _COMPONENT          ACPI_CA_DISASSEMBLER
55         ACPI_MODULE_NAME    ("dmrestag")
56
57 /* Local prototypes */
58
59 static void
60 AcpiDmUpdateResourceName (
61     ACPI_NAMESPACE_NODE     *ResourceNode);
62
63 static char *
64 AcpiDmSearchTagList (
65     UINT32                  BitIndex,
66     const ACPI_RESOURCE_TAG *TagList);
67
68 static char *
69 AcpiDmGetResourceTag (
70     UINT32                  BitIndex,
71     AML_RESOURCE            *Resource,
72     UINT8                   ResourceIndex);
73
74 static char *
75 AcpiGetTagPathname (
76     ACPI_NAMESPACE_NODE     *BufferNode,
77     ACPI_NAMESPACE_NODE     *ResourceNode,
78     UINT32                  BitIndex);
79
80 static ACPI_NAMESPACE_NODE *
81 AcpiDmGetResourceNode (
82     ACPI_NAMESPACE_NODE     *BufferNode,
83     UINT32                  BitIndex);
84
85 static ACPI_STATUS
86 AcpiDmAddResourceToNamespace (
87     UINT8                   *Aml,
88     UINT32                  Length,
89     UINT32                  Offset,
90     UINT8                   ResourceIndex,
91     void                    *Context);
92
93 static void
94 AcpiDmAddResourcesToNamespace (
95     ACPI_NAMESPACE_NODE     *BufferNode,
96     ACPI_PARSE_OBJECT       *Op);
97
98
99 /******************************************************************************
100  *
101  * Resource Tag tables
102  *
103  * These are the predefined tags that refer to elements of a resource
104  * descriptor. Each name and offset is defined in the ACPI specification.
105  *
106  * Each table entry contains the bit offset of the field and the associated
107  * name.
108  *
109  ******************************************************************************/
110
111 static const ACPI_RESOURCE_TAG      AcpiDmIrqTags[] =
112 {
113     {( 1 * 8),      ACPI_RESTAG_INTERRUPT},
114     {( 3 * 8) + 0,  ACPI_RESTAG_INTERRUPTTYPE},
115     {( 3 * 8) + 3,  ACPI_RESTAG_INTERRUPTLEVEL},
116     {( 3 * 8) + 4,  ACPI_RESTAG_INTERRUPTSHARE},
117     {0,             NULL}
118 };
119
120 static const ACPI_RESOURCE_TAG      AcpiDmDmaTags[] =
121 {
122     {( 1 * 8),      ACPI_RESTAG_DMA},
123     {( 2 * 8) + 0,  ACPI_RESTAG_XFERTYPE},
124     {( 2 * 8) + 2,  ACPI_RESTAG_BUSMASTER},
125     {( 2 * 8) + 5,  ACPI_RESTAG_DMATYPE},
126     {0,             NULL}
127 };
128
129 static const ACPI_RESOURCE_TAG      AcpiDmIoTags[] =
130 {
131     {( 1 * 8) + 0,  ACPI_RESTAG_DECODE},
132     {( 2 * 8),      ACPI_RESTAG_MINADDR},
133     {( 4 * 8),      ACPI_RESTAG_MAXADDR},
134     {( 6 * 8),      ACPI_RESTAG_ALIGNMENT},
135     {( 7 * 8),      ACPI_RESTAG_LENGTH},
136     {0,             NULL}
137 };
138
139 static const ACPI_RESOURCE_TAG      AcpiDmFixedIoTags[] =
140 {
141     {( 1 * 8),      ACPI_RESTAG_BASEADDRESS},
142     {( 3 * 8),      ACPI_RESTAG_LENGTH},
143     {0,             NULL}
144 };
145
146 static const ACPI_RESOURCE_TAG      AcpiDmFixedDmaTags[] =
147 {
148     {( 1 * 8),      ACPI_RESTAG_DMA},
149     {( 3 * 8),      ACPI_RESTAG_DMATYPE},
150     {( 5 * 8),      ACPI_RESTAG_XFERTYPE},
151     {0,             NULL}
152 };
153
154 static const ACPI_RESOURCE_TAG      AcpiDmMemory24Tags[] =
155 {
156     {( 3 * 8) + 0,  ACPI_RESTAG_READWRITETYPE},
157     {( 4 * 8),      ACPI_RESTAG_MINADDR},
158     {( 6 * 8),      ACPI_RESTAG_MAXADDR},
159     {( 8 * 8),      ACPI_RESTAG_ALIGNMENT},
160     {(10 * 8),      ACPI_RESTAG_LENGTH},
161     {0,             NULL}
162 };
163
164 static const ACPI_RESOURCE_TAG      AcpiDmRegisterTags[] =
165 {
166     {( 3 * 8),      ACPI_RESTAG_ADDRESSSPACE},
167     {( 4 * 8),      ACPI_RESTAG_REGISTERBITWIDTH},
168     {( 5 * 8),      ACPI_RESTAG_REGISTERBITOFFSET},
169     {( 6 * 8),      ACPI_RESTAG_ACCESSSIZE},
170     {( 7 * 8),      ACPI_RESTAG_ADDRESS},
171     {0,             NULL}
172 };
173
174 static const ACPI_RESOURCE_TAG      AcpiDmMemory32Tags[] =
175 {
176     {( 3 * 8) + 0,  ACPI_RESTAG_READWRITETYPE},
177     {( 4 * 8),      ACPI_RESTAG_MINADDR},
178     {( 8 * 8),      ACPI_RESTAG_MAXADDR},
179     {(12 * 8),      ACPI_RESTAG_ALIGNMENT},
180     {(16 * 8),      ACPI_RESTAG_LENGTH},
181     {0,             NULL}
182 };
183
184 static const ACPI_RESOURCE_TAG      AcpiDmFixedMemory32Tags[] =
185 {
186     {( 3 * 8) + 0,  ACPI_RESTAG_READWRITETYPE},
187     {( 4 * 8),      ACPI_RESTAG_BASEADDRESS},
188     {( 8 * 8),      ACPI_RESTAG_LENGTH},
189     {0,             NULL}
190 };
191
192 static const ACPI_RESOURCE_TAG      AcpiDmInterruptTags[] =
193 {
194     {( 3 * 8) + 1,  ACPI_RESTAG_INTERRUPTTYPE},
195     {( 3 * 8) + 2,  ACPI_RESTAG_INTERRUPTLEVEL},
196     {( 3 * 8) + 3,  ACPI_RESTAG_INTERRUPTSHARE},
197     {( 5 * 8),      ACPI_RESTAG_INTERRUPT},
198     {0,             NULL}
199 };
200
201 static const ACPI_RESOURCE_TAG      AcpiDmAddress16Tags[] =
202 {
203     {( 4 * 8) + 1,  ACPI_RESTAG_DECODE},
204     {( 4 * 8) + 2,  ACPI_RESTAG_MINTYPE},
205     {( 4 * 8) + 3,  ACPI_RESTAG_MAXTYPE},
206     {( 6 * 8),      ACPI_RESTAG_GRANULARITY},
207     {( 8 * 8),      ACPI_RESTAG_MINADDR},
208     {(10 * 8),      ACPI_RESTAG_MAXADDR},
209     {(12 * 8),      ACPI_RESTAG_TRANSLATION},
210     {(14 * 8),      ACPI_RESTAG_LENGTH},
211     {0,             NULL}
212 };
213
214 static const ACPI_RESOURCE_TAG      AcpiDmAddress32Tags[] =
215 {
216     {( 4 * 8) + 1,  ACPI_RESTAG_DECODE},
217     {( 4 * 8) + 2,  ACPI_RESTAG_MINTYPE},
218     {( 4 * 8) + 3,  ACPI_RESTAG_MAXTYPE},
219     {( 6 * 8),      ACPI_RESTAG_GRANULARITY},
220     {(10 * 8),      ACPI_RESTAG_MINADDR},
221     {(14 * 8),      ACPI_RESTAG_MAXADDR},
222     {(18 * 8),      ACPI_RESTAG_TRANSLATION},
223     {(22 * 8),      ACPI_RESTAG_LENGTH},
224     {0,             NULL}
225 };
226
227 static const ACPI_RESOURCE_TAG      AcpiDmAddress64Tags[] =
228 {
229     {( 4 * 8) + 1,  ACPI_RESTAG_DECODE},
230     {( 4 * 8) + 2,  ACPI_RESTAG_MINTYPE},
231     {( 4 * 8) + 3,  ACPI_RESTAG_MAXTYPE},
232     {( 6 * 8),      ACPI_RESTAG_GRANULARITY},
233     {(14 * 8),      ACPI_RESTAG_MINADDR},
234     {(22 * 8),      ACPI_RESTAG_MAXADDR},
235     {(30 * 8),      ACPI_RESTAG_TRANSLATION},
236     {(38 * 8),      ACPI_RESTAG_LENGTH},
237     {0,             NULL}
238 };
239
240 static const ACPI_RESOURCE_TAG      AcpiDmExtendedAddressTags[] =
241 {
242     {( 4 * 8) + 1,  ACPI_RESTAG_DECODE},
243     {( 4 * 8) + 2,  ACPI_RESTAG_MINTYPE},
244     {( 4 * 8) + 3,  ACPI_RESTAG_MAXTYPE},
245     {( 8 * 8),      ACPI_RESTAG_GRANULARITY},
246     {(16 * 8),      ACPI_RESTAG_MINADDR},
247     {(24 * 8),      ACPI_RESTAG_MAXADDR},
248     {(32 * 8),      ACPI_RESTAG_TRANSLATION},
249     {(40 * 8),      ACPI_RESTAG_LENGTH},
250     {(48 * 8),      ACPI_RESTAG_TYPESPECIFICATTRIBUTES},
251     {0,             NULL}
252 };
253
254 /* Subtype tables for GPIO descriptors */
255
256 static const ACPI_RESOURCE_TAG      AcpiDmGpioIntTags[] =
257 {
258     {( 7 * 8) + 0,  ACPI_RESTAG_MODE},
259     {( 7 * 8) + 1,  ACPI_RESTAG_POLARITY},
260     {( 7 * 8) + 3,  ACPI_RESTAG_INTERRUPTSHARE},
261     {( 9 * 8),      ACPI_RESTAG_PINCONFIG},
262     {(10 * 8),      ACPI_RESTAG_DRIVESTRENGTH},
263     {(12 * 8),      ACPI_RESTAG_DEBOUNCETIME},
264     {0,             NULL}
265 };
266
267 static const ACPI_RESOURCE_TAG      AcpiDmGpioIoTags[] =
268 {
269     {( 7 * 8) + 0,  ACPI_RESTAG_IORESTRICTION},
270     {( 7 * 8) + 3,  ACPI_RESTAG_INTERRUPTSHARE},
271     {( 9 * 8),      ACPI_RESTAG_PINCONFIG},
272     {(10 * 8),      ACPI_RESTAG_DRIVESTRENGTH},
273     {(12 * 8),      ACPI_RESTAG_DEBOUNCETIME},
274     {0,             NULL}
275 };
276
277 /* Subtype tables for SerialBus descriptors */
278
279 static const ACPI_RESOURCE_TAG      AcpiDmI2cSerialBusTags[] =
280 {
281     {( 6 * 8) + 0,  ACPI_RESTAG_SLAVEMODE},
282     {( 7 * 8) + 0,  ACPI_RESTAG_MODE},
283     {(12 * 8),      ACPI_RESTAG_SPEED},
284     {(16 * 8),      ACPI_RESTAG_ADDRESS},
285     {0,             NULL}
286 };
287
288 static const ACPI_RESOURCE_TAG      AcpiDmSpiSerialBusTags[] =
289 {
290     {( 6 * 8) + 0,  ACPI_RESTAG_SLAVEMODE},
291     {( 7 * 8) + 0,  ACPI_RESTAG_MODE},
292     {( 7 * 8) + 1,  ACPI_RESTAG_DEVICEPOLARITY},
293     {(12 * 8),      ACPI_RESTAG_SPEED},
294     {(16 * 8),      ACPI_RESTAG_LENGTH},
295     {(17 * 8),      ACPI_RESTAG_PHASE},
296     {(18 * 8),      ACPI_RESTAG_POLARITY},
297     {(19 * 8),      ACPI_RESTAG_ADDRESS},
298     {0,             NULL}
299 };
300
301 static const ACPI_RESOURCE_TAG      AcpiDmUartSerialBusTags[] =
302 {
303     {( 6 * 8) + 0,  ACPI_RESTAG_SLAVEMODE}, /* Note: not part of original macro */
304     {( 7 * 8) + 0,  ACPI_RESTAG_FLOWCONTROL},
305     {( 7 * 8) + 2,  ACPI_RESTAG_STOPBITS},
306     {( 7 * 8) + 4,  ACPI_RESTAG_LENGTH},
307     {( 7 * 8) + 7,  ACPI_RESTAG_ENDIANNESS},
308     {(12 * 8),      ACPI_RESTAG_SPEED},
309     {(16 * 8),      ACPI_RESTAG_LENGTH_RX},
310     {(18 * 8),      ACPI_RESTAG_LENGTH_TX},
311     {(20 * 8),      ACPI_RESTAG_PARITY},
312     {(21 * 8),      ACPI_RESTAG_LINE},
313     {0,             NULL}
314 };
315
316 /* Subtype tables for Address descriptor type-specific flags */
317
318 static const ACPI_RESOURCE_TAG      AcpiDmMemoryFlagTags[] =
319 {
320     {( 5 * 8) + 0,  ACPI_RESTAG_READWRITETYPE},
321     {( 5 * 8) + 1,  ACPI_RESTAG_MEMTYPE},
322     {( 5 * 8) + 3,  ACPI_RESTAG_MEMATTRIBUTES},
323     {( 5 * 8) + 5,  ACPI_RESTAG_TYPE},
324     {0,             NULL}
325 };
326
327 static const ACPI_RESOURCE_TAG      AcpiDmIoFlagTags[] =
328 {
329     {( 5 * 8) + 0,  ACPI_RESTAG_RANGETYPE},
330     {( 5 * 8) + 4,  ACPI_RESTAG_TYPE},
331     {( 5 * 8) + 5,  ACPI_RESTAG_TRANSTYPE},
332     {0,             NULL}
333 };
334
335
336 /*
337  * Dispatch table used to obtain the correct tag table for a descriptor.
338  *
339  * A NULL in this table means one of three things:
340  * 1) The descriptor ID is reserved and invalid
341  * 2) The descriptor has no tags associated with it
342  * 3) The descriptor has subtypes and a separate table will be used.
343  */
344 static const ACPI_RESOURCE_TAG      *AcpiGbl_ResourceTags[] =
345 {
346     /* Small descriptors */
347
348     NULL,                           /* 0x00, Reserved */
349     NULL,                           /* 0x01, Reserved */
350     NULL,                           /* 0x02, Reserved */
351     NULL,                           /* 0x03, Reserved */
352     AcpiDmIrqTags,                  /* 0x04, ACPI_RESOURCE_NAME_IRQ_FORMAT */
353     AcpiDmDmaTags,                  /* 0x05, ACPI_RESOURCE_NAME_DMA_FORMAT */
354     NULL,                           /* 0x06, ACPI_RESOURCE_NAME_START_DEPENDENT */
355     NULL,                           /* 0x07, ACPI_RESOURCE_NAME_END_DEPENDENT */
356     AcpiDmIoTags,                   /* 0x08, ACPI_RESOURCE_NAME_IO_PORT */
357     AcpiDmFixedIoTags,              /* 0x09, ACPI_RESOURCE_NAME_FIXED_IO_PORT */
358     AcpiDmFixedDmaTags,             /* 0x0A, ACPI_RESOURCE_NAME_FIXED_DMA */
359     NULL,                           /* 0x0B, Reserved */
360     NULL,                           /* 0x0C, Reserved */
361     NULL,                           /* 0x0D, Reserved */
362     NULL,                           /* 0x0E, ACPI_RESOURCE_NAME_SMALL_VENDOR */
363     NULL,                           /* 0x0F, ACPI_RESOURCE_NAME_END_TAG (not used) */
364
365     /* Large descriptors */
366
367     NULL,                           /* 0x00, Reserved */
368     AcpiDmMemory24Tags,             /* 0x01, ACPI_RESOURCE_NAME_MEMORY_24 */
369     AcpiDmRegisterTags,             /* 0x02, ACPI_RESOURCE_NAME_GENERIC_REGISTER */
370     NULL,                           /* 0x03, Reserved */
371     NULL,                           /* 0x04, ACPI_RESOURCE_NAME_LARGE_VENDOR */
372     AcpiDmMemory32Tags,             /* 0x05, ACPI_RESOURCE_NAME_MEMORY_32 */
373     AcpiDmFixedMemory32Tags,        /* 0x06, ACPI_RESOURCE_NAME_FIXED_MEMORY_32 */
374     AcpiDmAddress32Tags,            /* 0x07, ACPI_RESOURCE_NAME_DWORD_ADDRESS_SPACE */
375     AcpiDmAddress16Tags,            /* 0x08, ACPI_RESOURCE_NAME_WORD_ADDRESS_SPACE */
376     AcpiDmInterruptTags,            /* 0x09, ACPI_RESOURCE_NAME_EXTENDED_XRUPT */
377     AcpiDmAddress64Tags,            /* 0x0A, ACPI_RESOURCE_NAME_QWORD_ADDRESS_SPACE */
378     AcpiDmExtendedAddressTags,      /* 0x0B, ACPI_RESOURCE_NAME_EXTENDED_ADDRESS_SPACE */
379     NULL,                           /* 0x0C, ACPI_RESOURCE_NAME_GPIO - Use Subtype table below */
380     NULL,                           /* 0x0D, Reserved */
381     NULL                            /* 0x0E, ACPI_RESOURCE_NAME_SERIAL_BUS - Use Subtype table below */
382 };
383
384 /* GPIO Subtypes */
385
386 static const ACPI_RESOURCE_TAG      *AcpiGbl_GpioResourceTags[] =
387 {
388     AcpiDmGpioIntTags,              /* 0x00 Interrupt Connection */
389     AcpiDmGpioIoTags                /* 0x01 I/O Connection */
390 };
391
392 /* Serial Bus Subtypes */
393
394 static const ACPI_RESOURCE_TAG      *AcpiGbl_SerialResourceTags[] =
395 {
396     NULL,                           /* 0x00 Reserved */
397     AcpiDmI2cSerialBusTags,         /* 0x01 I2C SerialBus */
398     AcpiDmSpiSerialBusTags,         /* 0x02 SPI SerialBus */
399     AcpiDmUartSerialBusTags         /* 0x03 UART SerialBus */
400 };
401
402 /*
403  * Globals used to generate unique resource descriptor names. We use names that
404  * start with underscore and a prefix letter that is not used by other ACPI
405  * reserved names. To this, we append hex 0x00 through 0xFF. These 5 prefixes
406  * allow for 5*256 = 1280 unique names, probably sufficient for any single ASL
407  * file. If this becomes too small, we can use alpha+numerals for a total
408  * of 5*36*36 = 6480.
409  */
410 #define ACPI_NUM_RES_PREFIX     5
411
412 static UINT32                   AcpiGbl_NextResourceId = 0;
413 static UINT8                    AcpiGbl_NextPrefix = 0;
414 static char                     AcpiGbl_Prefix[ACPI_NUM_RES_PREFIX] =
415                                     {'Y','Z','J','K','X'};
416
417
418 /*******************************************************************************
419  *
420  * FUNCTION:    AcpiDmCheckResourceReference
421  *
422  * PARAMETERS:  Op                  - Parse Op for the AML opcode
423  *              WalkState           - Current walk state (with valid scope)
424  *
425  * RETURN:      None
426  *
427  * DESCRIPTION: Convert a reference to a resource descriptor to a symbolic
428  *              reference if possible
429  *
430  * NOTE:        Bit index is used to transparently handle both resource bit
431  *              fields and byte fields.
432  *
433  ******************************************************************************/
434
435 void
436 AcpiDmCheckResourceReference (
437     ACPI_PARSE_OBJECT       *Op,
438     ACPI_WALK_STATE         *WalkState)
439 {
440     ACPI_STATUS             Status;
441     ACPI_PARSE_OBJECT       *BufferNameOp;
442     ACPI_PARSE_OBJECT       *IndexOp;
443     ACPI_NAMESPACE_NODE     *BufferNode;
444     ACPI_NAMESPACE_NODE     *ResourceNode;
445     const ACPI_OPCODE_INFO  *OpInfo;
446     char                    *Pathname;
447     UINT32                  BitIndex;
448
449
450     /* We are only interested in the CreateXxxxField opcodes */
451
452     OpInfo = AcpiPsGetOpcodeInfo (Op->Common.AmlOpcode);
453     if (OpInfo->Type != AML_TYPE_CREATE_FIELD)
454     {
455         return;
456     }
457
458     /* Get the buffer term operand */
459
460     BufferNameOp = AcpiPsGetDepthNext (NULL, Op);
461
462     /* Must be a named buffer, not an arg or local or method call */
463
464     if (BufferNameOp->Common.AmlOpcode != AML_INT_NAMEPATH_OP)
465     {
466         return;
467     }
468
469     /* Get the Index term, must be an integer constant to convert */
470
471     IndexOp = BufferNameOp->Common.Next;
472     OpInfo = AcpiPsGetOpcodeInfo (IndexOp->Common.AmlOpcode);
473     if (OpInfo->ObjectType != ACPI_TYPE_INTEGER)
474     {
475         return;
476     }
477
478     /* Get the bit offset of the descriptor within the buffer */
479
480     if ((Op->Common.AmlOpcode == AML_CREATE_BIT_FIELD_OP) ||
481         (Op->Common.AmlOpcode == AML_CREATE_FIELD_OP))
482     {
483         /* Index operand is a bit offset */
484
485         BitIndex = (UINT32) IndexOp->Common.Value.Integer;
486     }
487     else
488     {
489         /* Index operand is a byte offset, convert to bits */
490
491         BitIndex = (UINT32) ACPI_MUL_8 (IndexOp->Common.Value.Integer);
492     }
493
494     /* Lookup the buffer in the namespace */
495
496     Status = AcpiNsLookup (WalkState->ScopeInfo,
497                 BufferNameOp->Common.Value.String, ACPI_TYPE_BUFFER,
498                 ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT, WalkState,
499                 &BufferNode);
500     if (ACPI_FAILURE (Status))
501     {
502         return;
503     }
504
505     /* Validate object type, we must have a buffer */
506
507     if (BufferNode->Type != ACPI_TYPE_BUFFER)
508     {
509         return;
510     }
511
512     /* Find the resource descriptor node corresponding to the index */
513
514     ResourceNode = AcpiDmGetResourceNode (BufferNode, BitIndex);
515     if (!ResourceNode)
516     {
517         return;
518     }
519
520     /* Translate the Index to a resource tag pathname */
521
522     Pathname = AcpiGetTagPathname (BufferNode, ResourceNode, BitIndex);
523     if (Pathname)
524     {
525         /* Complete the conversion of the Index to a symbol */
526
527         IndexOp->Common.AmlOpcode = AML_INT_NAMEPATH_OP;
528         IndexOp->Common.Value.String = Pathname;
529     }
530 }
531
532
533 /*******************************************************************************
534  *
535  * FUNCTION:    AcpiDmGetResourceNode
536  *
537  * PARAMETERS:  BufferNode          - Node for the parent buffer
538  *              BitIndex            - Index into the resource descriptor
539  *
540  * RETURN:      Namespace node for the resource descriptor. NULL if not found
541  *
542  * DESCRIPTION: Find a resource descriptor that corresponds to the bit index
543  *
544  ******************************************************************************/
545
546 static ACPI_NAMESPACE_NODE *
547 AcpiDmGetResourceNode (
548     ACPI_NAMESPACE_NODE     *BufferNode,
549     UINT32                  BitIndex)
550 {
551     ACPI_NAMESPACE_NODE     *Node;
552     UINT32                  ByteIndex = ACPI_DIV_8 (BitIndex);
553
554
555     /*
556      * Child list contains an entry for each resource descriptor. Find
557      * the descriptor that corresponds to the Index.
558      *
559      * If there are no children, this is not a resource template
560      */
561     Node = BufferNode->Child;
562     while (Node)
563     {
564         /*
565          * Check if the Index falls within this resource.
566          *
567          * Value contains the resource offset, Object contains the resource
568          * length (both in bytes)
569          */
570         if ((ByteIndex >= Node->Value) &&
571             (ByteIndex < (Node->Value + Node->Length)))
572         {
573             return (Node);
574         }
575
576         Node = Node->Peer;
577     }
578
579     return (NULL);
580 }
581
582
583 /*******************************************************************************
584  *
585  * FUNCTION:    AcpiGetTagPathname
586  *
587  * PARAMETERS:  BufferNode          - Node for the parent buffer
588  *              ResourceNode        - Node for a resource descriptor
589  *              BitIndex            - Index into the resource descriptor
590  *
591  * RETURN:      Full pathname for a resource tag. NULL if no match.
592  *              Path is returned in AML (packed) format.
593  *
594  * DESCRIPTION: Convert a BitIndex into a symbolic resource tag (full pathname)
595  *
596  ******************************************************************************/
597
598 static char *
599 AcpiGetTagPathname (
600     ACPI_NAMESPACE_NODE     *BufferNode,
601     ACPI_NAMESPACE_NODE     *ResourceNode,
602     UINT32                  BitIndex)
603 {
604     ACPI_STATUS             Status;
605     UINT32                  ResourceBitIndex;
606     UINT8                   ResourceTableIndex;
607     ACPI_SIZE               RequiredSize;
608     char                    *Pathname;
609     AML_RESOURCE            *Aml;
610     ACPI_PARSE_OBJECT       *Op;
611     char                    *InternalPath;
612     char                    *Tag;
613
614
615     /* Get the Op that contains the actual buffer data */
616
617     Op = BufferNode->Op->Common.Value.Arg;
618     Op = Op->Common.Next;
619     if (!Op)
620     {
621         return (NULL);
622     }
623
624     /* Get the individual resource descriptor and validate it */
625
626     Aml = ACPI_CAST_PTR (AML_RESOURCE,
627             &Op->Named.Data[ResourceNode->Value]);
628
629     Status = AcpiUtValidateResource (Aml, &ResourceTableIndex);
630     if (ACPI_FAILURE (Status))
631     {
632         return (NULL);
633     }
634
635     /* Get offset into this descriptor (from offset into entire buffer) */
636
637     ResourceBitIndex = BitIndex - ACPI_MUL_8 (ResourceNode->Value);
638
639     /* Get the tag associated with this resource descriptor and offset */
640
641     Tag = AcpiDmGetResourceTag (ResourceBitIndex, Aml, ResourceTableIndex);
642     if (!Tag)
643     {
644         return (NULL);
645     }
646
647     /*
648      * Now that we know that we have a reference that can be converted to a
649      * symbol, change the name of the resource to a unique name.
650      */
651     AcpiDmUpdateResourceName (ResourceNode);
652
653     /* Get the full pathname to the parent buffer */
654
655     RequiredSize = AcpiNsGetPathnameLength (BufferNode);
656     if (!RequiredSize)
657     {
658         return (NULL);
659     }
660
661     Pathname = ACPI_ALLOCATE_ZEROED (RequiredSize + ACPI_PATH_SEGMENT_LENGTH);
662     if (!Pathname)
663     {
664         return (NULL);
665     }
666
667     Status = AcpiNsBuildExternalPath (BufferNode, RequiredSize, Pathname);
668     if (ACPI_FAILURE (Status))
669     {
670         return (NULL);
671     }
672
673     /*
674      * Create the full path to the resource and tag by: remove the buffer name,
675      * append the resource descriptor name, append a dot, append the tag name.
676      *
677      * TBD: Always using the full path is a bit brute force, the path can be
678      * often be optimized with carats (if the original buffer namepath is a
679      * single nameseg). This doesn't really matter, because these paths do not
680      * end up in the final compiled AML, it's just an appearance issue for the
681      * disassembled code.
682      */
683     Pathname[ACPI_STRLEN (Pathname) - ACPI_NAME_SIZE] = 0;
684     ACPI_STRNCAT (Pathname, ResourceNode->Name.Ascii, ACPI_NAME_SIZE);
685     ACPI_STRCAT (Pathname, ".");
686     ACPI_STRNCAT (Pathname, Tag, ACPI_NAME_SIZE);
687
688     /* Internalize the namepath to AML format */
689
690     AcpiNsInternalizeName (Pathname, &InternalPath);
691     ACPI_FREE (Pathname);
692     return (InternalPath);
693 }
694
695
696 /*******************************************************************************
697  *
698  * FUNCTION:    AcpiDmUpdateResourceName
699  *
700  * PARAMETERS:  ResourceNode        - Node for a resource descriptor
701  *
702  * RETURN:      Stores new name in the ResourceNode
703  *
704  * DESCRIPTION: Create a new, unique name for a resource descriptor. Used by
705  *              both the disassembly of the descriptor itself and any symbolic
706  *              references to the descriptor. Ignored if a unique name has
707  *              already been assigned to the resource.
708  *
709  * NOTE: Single threaded, suitable for applications only!
710  *
711  ******************************************************************************/
712
713 static void
714 AcpiDmUpdateResourceName (
715     ACPI_NAMESPACE_NODE     *ResourceNode)
716 {
717     char                    Name[ACPI_NAME_SIZE];
718
719
720     /* Ignore if a unique name has already been assigned */
721
722     if (ResourceNode->Name.Integer != ACPI_DEFAULT_RESNAME)
723     {
724         return;
725     }
726
727     /* Generate a new ACPI name for the descriptor */
728
729     Name[0] = '_';
730     Name[1] = AcpiGbl_Prefix[AcpiGbl_NextPrefix];
731     Name[2] = AcpiUtHexToAsciiChar ((UINT64) AcpiGbl_NextResourceId, 4);
732     Name[3] = AcpiUtHexToAsciiChar ((UINT64) AcpiGbl_NextResourceId, 0);
733
734     /* Update globals for next name */
735
736     AcpiGbl_NextResourceId++;
737     if (AcpiGbl_NextResourceId >= 256)
738     {
739         AcpiGbl_NextResourceId = 0;
740         AcpiGbl_NextPrefix++;
741         if (AcpiGbl_NextPrefix > ACPI_NUM_RES_PREFIX)
742         {
743             AcpiGbl_NextPrefix = 0;
744         }
745     }
746
747     /* Change the resource descriptor name */
748
749     ResourceNode->Name.Integer = *ACPI_CAST_PTR (UINT32, &Name[0]);
750 }
751
752
753 /*******************************************************************************
754  *
755  * FUNCTION:    AcpiDmGetResourceTag
756  *
757  * PARAMETERS:  BitIndex            - Index into the resource descriptor
758  *              Resource            - Pointer to the raw resource data
759  *              ResourceIndex       - Index correspoinding to the resource type
760  *
761  * RETURN:      Pointer to the resource tag (ACPI_NAME). NULL if no match.
762  *
763  * DESCRIPTION: Convert a BitIndex into a symbolic resource tag.
764  *
765  * Note: ResourceIndex should be previously validated and guaranteed to ve
766  *       valid.
767  *
768  ******************************************************************************/
769
770 static char *
771 AcpiDmGetResourceTag (
772     UINT32                  BitIndex,
773     AML_RESOURCE            *Resource,
774     UINT8                   ResourceIndex)
775 {
776     const ACPI_RESOURCE_TAG *TagList;
777     char                    *Tag = NULL;
778
779
780     /* Get the tag list for this resource descriptor type */
781
782     TagList = AcpiGbl_ResourceTags[ResourceIndex];
783
784     /*
785      * Handle descriptors that have multiple subtypes
786      */
787     switch (Resource->DescriptorType)
788     {
789     case ACPI_RESOURCE_NAME_ADDRESS16:
790     case ACPI_RESOURCE_NAME_ADDRESS32:
791     case ACPI_RESOURCE_NAME_ADDRESS64:
792     case ACPI_RESOURCE_NAME_EXTENDED_ADDRESS64:
793
794         /*
795          * Subtype differentiation is the flags.
796          * Kindof brute force, but just blindly search for an index match
797          */
798         if (Resource->Address.ResourceType == ACPI_ADDRESS_TYPE_MEMORY_RANGE)
799         {
800             Tag = AcpiDmSearchTagList (BitIndex, AcpiDmMemoryFlagTags);
801         }
802         else if (Resource->Address.ResourceType == ACPI_ADDRESS_TYPE_IO_RANGE)
803         {
804             Tag = AcpiDmSearchTagList (BitIndex, AcpiDmIoFlagTags);
805         }
806
807         /* If we found a match, all done. Else, drop to normal search below */
808
809         if (Tag)
810         {
811             return (Tag);
812         }
813         break;
814
815     case ACPI_RESOURCE_NAME_GPIO:
816
817         /* GPIO connection has 2 subtypes: Interrupt and I/O */
818
819         if (Resource->Gpio.ConnectionType > AML_RESOURCE_MAX_GPIOTYPE)
820         {
821             return (NULL);
822         }
823
824         TagList = AcpiGbl_GpioResourceTags[Resource->Gpio.ConnectionType];
825         break;
826
827     case ACPI_RESOURCE_NAME_SERIAL_BUS:
828
829         /* SerialBus has 3 subtypes: I2C, SPI, and UART */
830
831         if ((Resource->CommonSerialBus.Type == 0) ||
832             (Resource->CommonSerialBus.Type > AML_RESOURCE_MAX_SERIALBUSTYPE))
833         {
834             return (NULL);
835         }
836
837         TagList = AcpiGbl_SerialResourceTags[Resource->CommonSerialBus.Type];
838         break;
839
840     default:
841         break;
842     }
843
844     /* Search for a match against the BitIndex */
845
846     if (TagList)
847     {
848         Tag = AcpiDmSearchTagList (BitIndex, TagList);
849     }
850
851     return (Tag);
852 }
853
854
855 /*******************************************************************************
856  *
857  * FUNCTION:    AcpiDmSearchTagList
858  *
859  * PARAMETERS:  BitIndex            - Index into the resource descriptor
860  *              TagList             - List to search
861  *
862  * RETURN:      Pointer to a tag (ACPI_NAME). NULL if no match found.
863  *
864  * DESCRIPTION: Search a tag list for a match to the input BitIndex. Matches
865  *              a fixed offset to a symbolic resource tag name.
866  *
867  ******************************************************************************/
868
869 static char *
870 AcpiDmSearchTagList (
871     UINT32                  BitIndex,
872     const ACPI_RESOURCE_TAG *TagList)
873 {
874
875     /*
876      * Walk the null-terminated tag list to find a matching bit offset.
877      * We are looking for an exact match.
878      */
879     for ( ; TagList->Tag; TagList++)
880     {
881         if (BitIndex == TagList->BitIndex)
882         {
883             return (TagList->Tag);
884         }
885     }
886
887     /* A matching offset was not found */
888
889     return (NULL);
890 }
891
892
893 /*******************************************************************************
894  *
895  * FUNCTION:    AcpiDmFindResources
896  *
897  * PARAMETERS:  Root                - Root of the parse tree
898  *
899  * RETURN:      None
900  *
901  * DESCRIPTION: Add all ResourceTemplate declarations to the namespace. Each
902  *              resource descriptor in each template is given a node -- used
903  *              for later conversion of resource references to symbolic refs.
904  *
905  ******************************************************************************/
906
907 void
908 AcpiDmFindResources (
909     ACPI_PARSE_OBJECT       *Root)
910 {
911     ACPI_PARSE_OBJECT       *Op = Root;
912     ACPI_PARSE_OBJECT       *Parent;
913
914
915     /* Walk the entire parse tree */
916
917     while (Op)
918     {
919         /* We are interested in Buffer() declarations */
920
921         if (Op->Common.AmlOpcode == AML_BUFFER_OP)
922         {
923             /* And only declarations of the form Name (XXXX, Buffer()... ) */
924
925             Parent = Op->Common.Parent;
926             if (Parent->Common.AmlOpcode == AML_NAME_OP)
927             {
928                 /*
929                  * If the buffer is a resource template, add the individual
930                  * resource descriptors to the namespace, as children of the
931                  * buffer node.
932                  */
933                 if (ACPI_SUCCESS (AcpiDmIsResourceTemplate (Op)))
934                 {
935                     Op->Common.DisasmOpcode = ACPI_DASM_RESOURCE;
936                     AcpiDmAddResourcesToNamespace (Parent->Common.Node, Op);
937                 }
938             }
939         }
940
941         Op = AcpiPsGetDepthNext (Root, Op);
942     }
943 }
944
945
946 /*******************************************************************************
947  *
948  * FUNCTION:    AcpiDmAddResourcesToNamespace
949  *
950  * PARAMETERS:  BufferNode          - Node for the parent buffer
951  *              Op                  - Parse op for the buffer
952  *
953  * RETURN:      None
954  *
955  * DESCRIPTION: Add an entire resource template to the namespace. Each
956  *              resource descriptor is added as a namespace node.
957  *
958  ******************************************************************************/
959
960 static void
961 AcpiDmAddResourcesToNamespace (
962     ACPI_NAMESPACE_NODE     *BufferNode,
963     ACPI_PARSE_OBJECT       *Op)
964 {
965     ACPI_PARSE_OBJECT       *NextOp;
966
967
968     /* Get to the ByteData list */
969
970     NextOp = Op->Common.Value.Arg;
971     NextOp = NextOp->Common.Next;
972     if (!NextOp)
973     {
974         return;
975     }
976
977     /* Set Node and Op to point to each other */
978
979     BufferNode->Op = Op;
980     Op->Common.Node = BufferNode;
981
982     /*
983      * Insert each resource into the namespace
984      * NextOp contains the Aml pointer and the Aml length
985      */
986     AcpiUtWalkAmlResources ((UINT8 *) NextOp->Named.Data,
987         (ACPI_SIZE) NextOp->Common.Value.Integer,
988         AcpiDmAddResourceToNamespace, BufferNode);
989 }
990
991
992 /*******************************************************************************
993  *
994  * FUNCTION:    AcpiDmAddResourceToNamespace
995  *
996  * PARAMETERS:  ACPI_WALK_AML_CALLBACK
997  *              BufferNode              - Node for the parent buffer
998  *
999  * RETURN:      Status
1000  *
1001  * DESCRIPTION: Add one resource descriptor to the namespace as a child of the
1002  *              parent buffer. The same name is used for each descriptor. This
1003  *              is changed later to a unique name if the resource is actually
1004  *              referenced by an AML operator.
1005  *
1006  ******************************************************************************/
1007
1008 static ACPI_STATUS
1009 AcpiDmAddResourceToNamespace (
1010     UINT8                   *Aml,
1011     UINT32                  Length,
1012     UINT32                  Offset,
1013     UINT8                   ResourceIndex,
1014     void                    *Context)
1015 {
1016     ACPI_STATUS             Status;
1017     ACPI_GENERIC_STATE      ScopeInfo;
1018     ACPI_NAMESPACE_NODE     *Node;
1019
1020
1021     /* TBD: Don't need to add descriptors that have no tags defined? */
1022
1023     /* Add the resource to the namespace, as child of the buffer */
1024
1025     ScopeInfo.Scope.Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Context);
1026     Status = AcpiNsLookup (&ScopeInfo, "_TMP", ACPI_TYPE_LOCAL_RESOURCE,
1027                 ACPI_IMODE_LOAD_PASS2,
1028                 ACPI_NS_NO_UPSEARCH | ACPI_NS_DONT_OPEN_SCOPE | ACPI_NS_PREFIX_IS_SCOPE,
1029                 NULL, &Node);
1030     if (ACPI_FAILURE (Status))
1031     {
1032         return (AE_OK);
1033     }
1034
1035     /* Set the name to the default, changed later if resource is referenced */
1036
1037     Node->Name.Integer = ACPI_DEFAULT_RESNAME;
1038
1039     /* Save the offset of the descriptor (within the original buffer) */
1040
1041     Node->Value = Offset;
1042     Node->Length = Length;
1043     return (AE_OK);
1044 }
1045