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