167754Smsmith/*******************************************************************************
267754Smsmith *
377424Smsmith * Module Name: rsxface - Public interfaces to the resource manager
467754Smsmith *
567754Smsmith ******************************************************************************/
667754Smsmith
7217365Sjkim/*
8281075Sdim * Copyright (C) 2000 - 2015, Intel Corp.
970243Smsmith * All rights reserved.
1067754Smsmith *
11217365Sjkim * Redistribution and use in source and binary forms, with or without
12217365Sjkim * modification, are permitted provided that the following conditions
13217365Sjkim * are met:
14217365Sjkim * 1. Redistributions of source code must retain the above copyright
15217365Sjkim *    notice, this list of conditions, and the following disclaimer,
16217365Sjkim *    without modification.
17217365Sjkim * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18217365Sjkim *    substantially similar to the "NO WARRANTY" disclaimer below
19217365Sjkim *    ("Disclaimer") and any redistribution must be conditioned upon
20217365Sjkim *    including a substantially similar Disclaimer requirement for further
21217365Sjkim *    binary redistribution.
22217365Sjkim * 3. Neither the names of the above-listed copyright holders nor the names
23217365Sjkim *    of any contributors may be used to endorse or promote products derived
24217365Sjkim *    from this software without specific prior written permission.
2567754Smsmith *
26217365Sjkim * Alternatively, this software may be distributed under the terms of the
27217365Sjkim * GNU General Public License ("GPL") version 2 as published by the Free
28217365Sjkim * Software Foundation.
2967754Smsmith *
30217365Sjkim * NO WARRANTY
31217365Sjkim * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32217365Sjkim * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33217365Sjkim * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34217365Sjkim * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35217365Sjkim * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36217365Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37217365Sjkim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38217365Sjkim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39217365Sjkim * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40217365Sjkim * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41217365Sjkim * POSSIBILITY OF SUCH DAMAGES.
42217365Sjkim */
4367754Smsmith
44281075Sdim#define EXPORT_ACPI_INTERFACES
4567754Smsmith
46193341Sjkim#include <contrib/dev/acpica/include/acpi.h>
47193341Sjkim#include <contrib/dev/acpica/include/accommon.h>
48193341Sjkim#include <contrib/dev/acpica/include/acresrc.h>
49193341Sjkim#include <contrib/dev/acpica/include/acnamesp.h>
5067754Smsmith
5177424Smsmith#define _COMPONENT          ACPI_RESOURCES
5291116Smsmith        ACPI_MODULE_NAME    ("rsxface")
5367754Smsmith
54151937Sjkim/* Local macros for 16,32-bit to 64-bit conversion */
5567754Smsmith
56151937Sjkim#define ACPI_COPY_FIELD(Out, In, Field)  ((Out)->Field = (In)->Field)
57151937Sjkim#define ACPI_COPY_ADDRESS(Out, In)                      \
58151937Sjkim    ACPI_COPY_FIELD(Out, In, ResourceType);              \
59151937Sjkim    ACPI_COPY_FIELD(Out, In, ProducerConsumer);          \
60151937Sjkim    ACPI_COPY_FIELD(Out, In, Decode);                    \
61151937Sjkim    ACPI_COPY_FIELD(Out, In, MinAddressFixed);           \
62151937Sjkim    ACPI_COPY_FIELD(Out, In, MaxAddressFixed);           \
63151937Sjkim    ACPI_COPY_FIELD(Out, In, Info);                      \
64281687Sjkim    ACPI_COPY_FIELD(Out, In, Address.Granularity);       \
65281687Sjkim    ACPI_COPY_FIELD(Out, In, Address.Minimum);           \
66281687Sjkim    ACPI_COPY_FIELD(Out, In, Address.Maximum);           \
67281687Sjkim    ACPI_COPY_FIELD(Out, In, Address.TranslationOffset); \
68281687Sjkim    ACPI_COPY_FIELD(Out, In, Address.AddressLength);     \
69151937Sjkim    ACPI_COPY_FIELD(Out, In, ResourceSource);
70151937Sjkim
71151937Sjkim
72167802Sjkim/* Local prototypes */
73167802Sjkim
74167802Sjkimstatic ACPI_STATUS
75167802SjkimAcpiRsMatchVendorResource (
76167802Sjkim    ACPI_RESOURCE           *Resource,
77167802Sjkim    void                    *Context);
78167802Sjkim
79167802Sjkimstatic ACPI_STATUS
80167802SjkimAcpiRsValidateParameters (
81167802Sjkim    ACPI_HANDLE             DeviceHandle,
82167802Sjkim    ACPI_BUFFER             *Buffer,
83167802Sjkim    ACPI_NAMESPACE_NODE     **ReturnNode);
84167802Sjkim
85167802Sjkim
8667754Smsmith/*******************************************************************************
8767754Smsmith *
88167802Sjkim * FUNCTION:    AcpiRsValidateParameters
89167802Sjkim *
90167802Sjkim * PARAMETERS:  DeviceHandle    - Handle to a device
91167802Sjkim *              Buffer          - Pointer to a data buffer
92167802Sjkim *              ReturnNode      - Pointer to where the device node is returned
93167802Sjkim *
94167802Sjkim * RETURN:      Status
95167802Sjkim *
96167802Sjkim * DESCRIPTION: Common parameter validation for resource interfaces
97167802Sjkim *
98167802Sjkim ******************************************************************************/
99167802Sjkim
100167802Sjkimstatic ACPI_STATUS
101167802SjkimAcpiRsValidateParameters (
102167802Sjkim    ACPI_HANDLE             DeviceHandle,
103167802Sjkim    ACPI_BUFFER             *Buffer,
104167802Sjkim    ACPI_NAMESPACE_NODE     **ReturnNode)
105167802Sjkim{
106167802Sjkim    ACPI_STATUS             Status;
107167802Sjkim    ACPI_NAMESPACE_NODE     *Node;
108167802Sjkim
109167802Sjkim
110167802Sjkim    ACPI_FUNCTION_TRACE (RsValidateParameters);
111167802Sjkim
112167802Sjkim
113167802Sjkim    /*
114167802Sjkim     * Must have a valid handle to an ACPI device
115167802Sjkim     */
116167802Sjkim    if (!DeviceHandle)
117167802Sjkim    {
118167802Sjkim        return_ACPI_STATUS (AE_BAD_PARAMETER);
119167802Sjkim    }
120167802Sjkim
121200553Sjkim    Node = AcpiNsValidateHandle (DeviceHandle);
122167802Sjkim    if (!Node)
123167802Sjkim    {
124167802Sjkim        return_ACPI_STATUS (AE_BAD_PARAMETER);
125167802Sjkim    }
126167802Sjkim
127167802Sjkim    if (Node->Type != ACPI_TYPE_DEVICE)
128167802Sjkim    {
129167802Sjkim        return_ACPI_STATUS (AE_TYPE);
130167802Sjkim    }
131167802Sjkim
132167802Sjkim    /*
133167802Sjkim     * Validate the user buffer object
134167802Sjkim     *
135167802Sjkim     * if there is a non-zero buffer length we also need a valid pointer in
136167802Sjkim     * the buffer. If it's a zero buffer length, we'll be returning the
137167802Sjkim     * needed buffer size (later), so keep going.
138167802Sjkim     */
139167802Sjkim    Status = AcpiUtValidateBuffer (Buffer);
140167802Sjkim    if (ACPI_FAILURE (Status))
141167802Sjkim    {
142167802Sjkim        return_ACPI_STATUS (Status);
143167802Sjkim    }
144167802Sjkim
145167802Sjkim    *ReturnNode = Node;
146167802Sjkim    return_ACPI_STATUS (AE_OK);
147167802Sjkim}
148167802Sjkim
149167802Sjkim
150167802Sjkim/*******************************************************************************
151167802Sjkim *
15267754Smsmith * FUNCTION:    AcpiGetIrqRoutingTable
15367754Smsmith *
154167802Sjkim * PARAMETERS:  DeviceHandle    - Handle to the Bus device we are querying
155167802Sjkim *              RetBuffer       - Pointer to a buffer to receive the
15667754Smsmith *                                current resources for the device
15767754Smsmith *
15877424Smsmith * RETURN:      Status
15967754Smsmith *
16067754Smsmith * DESCRIPTION: This function is called to get the IRQ routing table for a
161167802Sjkim *              specific bus. The caller must first acquire a handle for the
162167802Sjkim *              desired bus. The routine table is placed in the buffer pointed
16367754Smsmith *              to by the RetBuffer variable parameter.
16467754Smsmith *
16567754Smsmith *              If the function fails an appropriate status will be returned
16667754Smsmith *              and the value of RetBuffer is undefined.
16767754Smsmith *
16867754Smsmith *              This function attempts to execute the _PRT method contained in
16967754Smsmith *              the object indicated by the passed DeviceHandle.
17067754Smsmith *
17167754Smsmith ******************************************************************************/
17267754Smsmith
17367754SmsmithACPI_STATUS
17467754SmsmithAcpiGetIrqRoutingTable  (
17567754Smsmith    ACPI_HANDLE             DeviceHandle,
17667754Smsmith    ACPI_BUFFER             *RetBuffer)
17767754Smsmith{
17867754Smsmith    ACPI_STATUS             Status;
179167802Sjkim    ACPI_NAMESPACE_NODE     *Node;
18067754Smsmith
18167754Smsmith
182167802Sjkim    ACPI_FUNCTION_TRACE (AcpiGetIrqRoutingTable);
18367754Smsmith
18477424Smsmith
185167802Sjkim    /* Validate parameters then dispatch to internal routine */
18667754Smsmith
187167802Sjkim    Status = AcpiRsValidateParameters (DeviceHandle, RetBuffer, &Node);
18891116Smsmith    if (ACPI_FAILURE (Status))
18991116Smsmith    {
19091116Smsmith        return_ACPI_STATUS (Status);
19191116Smsmith    }
19291116Smsmith
193167802Sjkim    Status = AcpiRsGetPrtMethodData (Node, RetBuffer);
19467754Smsmith    return_ACPI_STATUS (Status);
19567754Smsmith}
19667754Smsmith
197167802SjkimACPI_EXPORT_SYMBOL (AcpiGetIrqRoutingTable)
19867754Smsmith
199167802Sjkim
20067754Smsmith/*******************************************************************************
20167754Smsmith *
20267754Smsmith * FUNCTION:    AcpiGetCurrentResources
20367754Smsmith *
204167802Sjkim * PARAMETERS:  DeviceHandle    - Handle to the device object for the
20567754Smsmith *                                device we are querying
206167802Sjkim *              RetBuffer       - Pointer to a buffer to receive the
20767754Smsmith *                                current resources for the device
20867754Smsmith *
20977424Smsmith * RETURN:      Status
21067754Smsmith *
21167754Smsmith * DESCRIPTION: This function is called to get the current resources for a
212167802Sjkim *              specific device. The caller must first acquire a handle for
213167802Sjkim *              the desired device. The resource data is placed in the buffer
21467754Smsmith *              pointed to by the RetBuffer variable parameter.
21567754Smsmith *
21667754Smsmith *              If the function fails an appropriate status will be returned
21767754Smsmith *              and the value of RetBuffer is undefined.
21867754Smsmith *
21967754Smsmith *              This function attempts to execute the _CRS method contained in
22067754Smsmith *              the object indicated by the passed DeviceHandle.
22167754Smsmith *
22267754Smsmith ******************************************************************************/
22367754Smsmith
22467754SmsmithACPI_STATUS
22567754SmsmithAcpiGetCurrentResources (
22667754Smsmith    ACPI_HANDLE             DeviceHandle,
22767754Smsmith    ACPI_BUFFER             *RetBuffer)
22867754Smsmith{
22967754Smsmith    ACPI_STATUS             Status;
230167802Sjkim    ACPI_NAMESPACE_NODE     *Node;
23167754Smsmith
23267754Smsmith
233167802Sjkim    ACPI_FUNCTION_TRACE (AcpiGetCurrentResources);
23467754Smsmith
23583174Smsmith
236167802Sjkim    /* Validate parameters then dispatch to internal routine */
23767754Smsmith
238167802Sjkim    Status = AcpiRsValidateParameters (DeviceHandle, RetBuffer, &Node);
23991116Smsmith    if (ACPI_FAILURE (Status))
24091116Smsmith    {
24191116Smsmith        return_ACPI_STATUS (Status);
24291116Smsmith    }
24391116Smsmith
244167802Sjkim    Status = AcpiRsGetCrsMethodData (Node, RetBuffer);
24567754Smsmith    return_ACPI_STATUS (Status);
24667754Smsmith}
24767754Smsmith
248167802SjkimACPI_EXPORT_SYMBOL (AcpiGetCurrentResources)
24967754Smsmith
250167802Sjkim
25167754Smsmith/*******************************************************************************
25267754Smsmith *
25367754Smsmith * FUNCTION:    AcpiGetPossibleResources
25467754Smsmith *
255167802Sjkim * PARAMETERS:  DeviceHandle    - Handle to the device object for the
25667754Smsmith *                                device we are querying
257167802Sjkim *              RetBuffer       - Pointer to a buffer to receive the
25867754Smsmith *                                resources for the device
25967754Smsmith *
26077424Smsmith * RETURN:      Status
26167754Smsmith *
26267754Smsmith * DESCRIPTION: This function is called to get a list of the possible resources
263167802Sjkim *              for a specific device. The caller must first acquire a handle
264167802Sjkim *              for the desired device. The resource data is placed in the
26567754Smsmith *              buffer pointed to by the RetBuffer variable.
26667754Smsmith *
26767754Smsmith *              If the function fails an appropriate status will be returned
26867754Smsmith *              and the value of RetBuffer is undefined.
26967754Smsmith *
27067754Smsmith ******************************************************************************/
27167754Smsmith
27267754SmsmithACPI_STATUS
27367754SmsmithAcpiGetPossibleResources (
27467754Smsmith    ACPI_HANDLE             DeviceHandle,
27567754Smsmith    ACPI_BUFFER             *RetBuffer)
27667754Smsmith{
27767754Smsmith    ACPI_STATUS             Status;
278167802Sjkim    ACPI_NAMESPACE_NODE     *Node;
27967754Smsmith
28067754Smsmith
281167802Sjkim    ACPI_FUNCTION_TRACE (AcpiGetPossibleResources);
28267754Smsmith
28377424Smsmith
284167802Sjkim    /* Validate parameters then dispatch to internal routine */
28567754Smsmith
286167802Sjkim    Status = AcpiRsValidateParameters (DeviceHandle, RetBuffer, &Node);
28791116Smsmith    if (ACPI_FAILURE (Status))
28891116Smsmith    {
28991116Smsmith        return_ACPI_STATUS (Status);
29091116Smsmith    }
29191116Smsmith
292167802Sjkim    Status = AcpiRsGetPrsMethodData (Node, RetBuffer);
29367754Smsmith    return_ACPI_STATUS (Status);
29467754Smsmith}
29567754Smsmith
296167802SjkimACPI_EXPORT_SYMBOL (AcpiGetPossibleResources)
29767754Smsmith
298167802Sjkim
29967754Smsmith/*******************************************************************************
30067754Smsmith *
301167802Sjkim * FUNCTION:    AcpiSetCurrentResources
302114237Snjl *
303167802Sjkim * PARAMETERS:  DeviceHandle    - Handle to the device object for the
304167802Sjkim *                                device we are setting resources
305167802Sjkim *              InBuffer        - Pointer to a buffer containing the
306167802Sjkim *                                resources to be set for the device
307114237Snjl *
308114237Snjl * RETURN:      Status
309114237Snjl *
310167802Sjkim * DESCRIPTION: This function is called to set the current resources for a
311167802Sjkim *              specific device. The caller must first acquire a handle for
312167802Sjkim *              the desired device. The resource data is passed to the routine
313167802Sjkim *              the buffer pointed to by the InBuffer variable.
314114237Snjl *
315114237Snjl ******************************************************************************/
316114237Snjl
317114237SnjlACPI_STATUS
318167802SjkimAcpiSetCurrentResources (
319167802Sjkim    ACPI_HANDLE             DeviceHandle,
320167802Sjkim    ACPI_BUFFER             *InBuffer)
321114237Snjl{
322167802Sjkim    ACPI_STATUS             Status;
323167802Sjkim    ACPI_NAMESPACE_NODE     *Node;
324114237Snjl
325114237Snjl
326167802Sjkim    ACPI_FUNCTION_TRACE (AcpiSetCurrentResources);
327114237Snjl
328114237Snjl
329167802Sjkim    /* Validate the buffer, don't allow zero length */
330167802Sjkim
331167802Sjkim    if ((!InBuffer) ||
332167802Sjkim        (!InBuffer->Pointer) ||
333167802Sjkim        (!InBuffer->Length))
334114237Snjl    {
335114237Snjl        return_ACPI_STATUS (AE_BAD_PARAMETER);
336114237Snjl    }
337114237Snjl
338167802Sjkim    /* Validate parameters then dispatch to internal routine */
339167802Sjkim
340167802Sjkim    Status = AcpiRsValidateParameters (DeviceHandle, InBuffer, &Node);
341114237Snjl    if (ACPI_FAILURE (Status))
342114237Snjl    {
343114237Snjl        return_ACPI_STATUS (Status);
344114237Snjl    }
345114237Snjl
346167802Sjkim    Status = AcpiRsSetSrsMethodData (Node, InBuffer);
347167802Sjkim    return_ACPI_STATUS (Status);
348167802Sjkim}
349126372Snjl
350167802SjkimACPI_EXPORT_SYMBOL (AcpiSetCurrentResources)
351126372Snjl
352126372Snjl
353228110Sjkim/*******************************************************************************
354228110Sjkim *
355228110Sjkim * FUNCTION:    AcpiGetEventResources
356228110Sjkim *
357228110Sjkim * PARAMETERS:  DeviceHandle    - Handle to the device object for the
358228110Sjkim *                                device we are getting resources
359228110Sjkim *              InBuffer        - Pointer to a buffer containing the
360228110Sjkim *                                resources to be set for the device
361228110Sjkim *
362228110Sjkim * RETURN:      Status
363228110Sjkim *
364228110Sjkim * DESCRIPTION: This function is called to get the event resources for a
365228110Sjkim *              specific device. The caller must first acquire a handle for
366228110Sjkim *              the desired device. The resource data is passed to the routine
367228110Sjkim *              the buffer pointed to by the InBuffer variable. Uses the
368228110Sjkim *              _AEI method.
369228110Sjkim *
370228110Sjkim ******************************************************************************/
371228110Sjkim
372228110SjkimACPI_STATUS
373228110SjkimAcpiGetEventResources (
374228110Sjkim    ACPI_HANDLE             DeviceHandle,
375228110Sjkim    ACPI_BUFFER             *RetBuffer)
376228110Sjkim{
377228110Sjkim    ACPI_STATUS             Status;
378228110Sjkim    ACPI_NAMESPACE_NODE     *Node;
379228110Sjkim
380228110Sjkim
381228110Sjkim    ACPI_FUNCTION_TRACE (AcpiGetEventResources);
382228110Sjkim
383228110Sjkim
384228110Sjkim    /* Validate parameters then dispatch to internal routine */
385228110Sjkim
386228110Sjkim    Status = AcpiRsValidateParameters (DeviceHandle, RetBuffer, &Node);
387228110Sjkim    if (ACPI_FAILURE (Status))
388228110Sjkim    {
389228110Sjkim        return_ACPI_STATUS (Status);
390228110Sjkim    }
391228110Sjkim
392228110Sjkim    Status = AcpiRsGetAeiMethodData (Node, RetBuffer);
393228110Sjkim    return_ACPI_STATUS (Status);
394228110Sjkim}
395228110Sjkim
396228110SjkimACPI_EXPORT_SYMBOL (AcpiGetEventResources)
397228110Sjkim
398228110Sjkim
399167802Sjkim/******************************************************************************
400167802Sjkim *
401167802Sjkim * FUNCTION:    AcpiResourceToAddress64
402167802Sjkim *
403167802Sjkim * PARAMETERS:  Resource        - Pointer to a resource
404167802Sjkim *              Out             - Pointer to the users's return buffer
405167802Sjkim *                                (a struct acpi_resource_address64)
406167802Sjkim *
407167802Sjkim * RETURN:      Status
408167802Sjkim *
409167802Sjkim * DESCRIPTION: If the resource is an address16, address32, or address64,
410167802Sjkim *              copy it to the address64 return buffer. This saves the
411167802Sjkim *              caller from having to duplicate code for different-sized
412167802Sjkim *              addresses.
413167802Sjkim *
414167802Sjkim ******************************************************************************/
415167802Sjkim
416167802SjkimACPI_STATUS
417167802SjkimAcpiResourceToAddress64 (
418167802Sjkim    ACPI_RESOURCE               *Resource,
419167802Sjkim    ACPI_RESOURCE_ADDRESS64     *Out)
420167802Sjkim{
421167802Sjkim    ACPI_RESOURCE_ADDRESS16     *Address16;
422167802Sjkim    ACPI_RESOURCE_ADDRESS32     *Address32;
423167802Sjkim
424167802Sjkim
425167802Sjkim    if (!Resource || !Out)
426114237Snjl    {
427167802Sjkim        return (AE_BAD_PARAMETER);
428167802Sjkim    }
429114237Snjl
430167802Sjkim    /* Convert 16 or 32 address descriptor to 64 */
431114237Snjl
432167802Sjkim    switch (Resource->Type)
433167802Sjkim    {
434167802Sjkim    case ACPI_RESOURCE_TYPE_ADDRESS16:
435114237Snjl
436193267Sjkim        Address16 = ACPI_CAST_PTR (ACPI_RESOURCE_ADDRESS16, &Resource->Data);
437167802Sjkim        ACPI_COPY_ADDRESS (Out, Address16);
438167802Sjkim        break;
439126372Snjl
440167802Sjkim    case ACPI_RESOURCE_TYPE_ADDRESS32:
441114237Snjl
442193267Sjkim        Address32 = ACPI_CAST_PTR (ACPI_RESOURCE_ADDRESS32, &Resource->Data);
443167802Sjkim        ACPI_COPY_ADDRESS (Out, Address32);
444167802Sjkim        break;
445114237Snjl
446167802Sjkim    case ACPI_RESOURCE_TYPE_ADDRESS64:
447114237Snjl
448167802Sjkim        /* Simple copy for 64 bit source */
449114237Snjl
450167802Sjkim        ACPI_MEMCPY (Out, &Resource->Data, sizeof (ACPI_RESOURCE_ADDRESS64));
451167802Sjkim        break;
452114237Snjl
453167802Sjkim    default:
454250838Sjkim
455167802Sjkim        return (AE_BAD_PARAMETER);
456167802Sjkim    }
457114237Snjl
458167802Sjkim    return (AE_OK);
459167802Sjkim}
460114237Snjl
461167802SjkimACPI_EXPORT_SYMBOL (AcpiResourceToAddress64)
462126372Snjl
463126372Snjl
464167802Sjkim/*******************************************************************************
465167802Sjkim *
466167802Sjkim * FUNCTION:    AcpiGetVendorResource
467167802Sjkim *
468167802Sjkim * PARAMETERS:  DeviceHandle    - Handle for the parent device object
469167802Sjkim *              Name            - Method name for the parent resource
470167802Sjkim *                                (METHOD_NAME__CRS or METHOD_NAME__PRS)
471167802Sjkim *              Uuid            - Pointer to the UUID to be matched.
472167802Sjkim *                                includes both subtype and 16-byte UUID
473167802Sjkim *              RetBuffer       - Where the vendor resource is returned
474167802Sjkim *
475167802Sjkim * RETURN:      Status
476167802Sjkim *
477245582Sjkim * DESCRIPTION: Walk a resource template for the specified device to find a
478167802Sjkim *              vendor-defined resource that matches the supplied UUID and
479167802Sjkim *              UUID subtype. Returns a ACPI_RESOURCE of type Vendor.
480167802Sjkim *
481167802Sjkim ******************************************************************************/
482126372Snjl
483167802SjkimACPI_STATUS
484167802SjkimAcpiGetVendorResource (
485167802Sjkim    ACPI_HANDLE             DeviceHandle,
486167802Sjkim    char                    *Name,
487167802Sjkim    ACPI_VENDOR_UUID        *Uuid,
488167802Sjkim    ACPI_BUFFER             *RetBuffer)
489167802Sjkim{
490167802Sjkim    ACPI_VENDOR_WALK_INFO   Info;
491167802Sjkim    ACPI_STATUS             Status;
492167802Sjkim
493167802Sjkim
494167802Sjkim    /* Other parameters are validated by AcpiWalkResources */
495167802Sjkim
496167802Sjkim    if (!Uuid || !RetBuffer)
497167802Sjkim    {
498167802Sjkim        return (AE_BAD_PARAMETER);
499114237Snjl    }
500114237Snjl
501167802Sjkim    Info.Uuid = Uuid;
502167802Sjkim    Info.Buffer = RetBuffer;
503167802Sjkim    Info.Status = AE_NOT_EXIST;
504114237Snjl
505167802Sjkim    /* Walk the _CRS or _PRS resource list for this device */
506167802Sjkim
507167802Sjkim    Status = AcpiWalkResources (DeviceHandle, Name, AcpiRsMatchVendorResource,
508167802Sjkim                &Info);
509167802Sjkim    if (ACPI_FAILURE (Status))
510167802Sjkim    {
511167802Sjkim        return (Status);
512167802Sjkim    }
513167802Sjkim
514167802Sjkim    return (Info.Status);
515114237Snjl}
516114237Snjl
517167802SjkimACPI_EXPORT_SYMBOL (AcpiGetVendorResource)
518114237Snjl
519167802Sjkim
520114237Snjl/*******************************************************************************
521114237Snjl *
522167802Sjkim * FUNCTION:    AcpiRsMatchVendorResource
52367754Smsmith *
524167802Sjkim * PARAMETERS:  ACPI_WALK_RESOURCE_CALLBACK
52567754Smsmith *
52677424Smsmith * RETURN:      Status
52767754Smsmith *
528167802Sjkim * DESCRIPTION: Match a vendor resource via the ACPI 3.0 UUID
52967754Smsmith *
53067754Smsmith ******************************************************************************/
53167754Smsmith
532167802Sjkimstatic ACPI_STATUS
533167802SjkimAcpiRsMatchVendorResource (
534167802Sjkim    ACPI_RESOURCE           *Resource,
535167802Sjkim    void                    *Context)
53667754Smsmith{
537167802Sjkim    ACPI_VENDOR_WALK_INFO       *Info = Context;
538167802Sjkim    ACPI_RESOURCE_VENDOR_TYPED  *Vendor;
539167802Sjkim    ACPI_BUFFER                 *Buffer;
540167802Sjkim    ACPI_STATUS                 Status;
54167754Smsmith
54267754Smsmith
543167802Sjkim    /* Ignore all descriptors except Vendor */
54467754Smsmith
545167802Sjkim    if (Resource->Type != ACPI_RESOURCE_TYPE_VENDOR)
546167802Sjkim    {
547167802Sjkim        return (AE_OK);
548167802Sjkim    }
54977424Smsmith
550167802Sjkim    Vendor = &Resource->Data.VendorTyped;
551151937Sjkim
552167802Sjkim    /*
553167802Sjkim     * For a valid match, these conditions must hold:
554167802Sjkim     *
555167802Sjkim     * 1) Length of descriptor data must be at least as long as a UUID struct
556167802Sjkim     * 2) The UUID subtypes must match
557167802Sjkim     * 3) The UUID data must match
558167802Sjkim     */
559167802Sjkim    if ((Vendor->ByteLength < (ACPI_UUID_LENGTH + 1)) ||
560167802Sjkim        (Vendor->UuidSubtype != Info->Uuid->Subtype)  ||
561167802Sjkim        (ACPI_MEMCMP (Vendor->Uuid, Info->Uuid->Data, ACPI_UUID_LENGTH)))
56267754Smsmith    {
563167802Sjkim        return (AE_OK);
56467754Smsmith    }
56567754Smsmith
566167802Sjkim    /* Validate/Allocate/Clear caller buffer */
567167802Sjkim
568167802Sjkim    Buffer = Info->Buffer;
569167802Sjkim    Status = AcpiUtInitializeBuffer (Buffer, Resource->Length);
570167802Sjkim    if (ACPI_FAILURE (Status))
571167802Sjkim    {
572167802Sjkim        return (Status);
573167802Sjkim    }
574167802Sjkim
575167802Sjkim    /* Found the correct resource, copy and return it */
576167802Sjkim
577167802Sjkim    ACPI_MEMCPY (Buffer->Pointer, Resource, Resource->Length);
578167802Sjkim    Buffer->Length = Resource->Length;
579167802Sjkim
580167802Sjkim    /* Found the desired descriptor, terminate resource walk */
581167802Sjkim
582167802Sjkim    Info->Status = AE_OK;
583167802Sjkim    return (AE_CTRL_TERMINATE);
58467754Smsmith}
585114237Snjl
586114237Snjl
587167802Sjkim/*******************************************************************************
588114237Snjl *
589245582Sjkim * FUNCTION:    AcpiWalkResourceBuffer
590114237Snjl *
591245582Sjkim * PARAMETERS:  Buffer          - Formatted buffer returned by one of the
592245582Sjkim *                                various Get*Resource functions
593167802Sjkim *              UserFunction    - Called for each resource
594167802Sjkim *              Context         - Passed to UserFunction
595114237Snjl *
596114237Snjl * RETURN:      Status
597114237Snjl *
598245582Sjkim * DESCRIPTION: Walks the input resource template. The UserFunction is called
599245582Sjkim *              once for each resource in the list.
600114237Snjl *
601114237Snjl ******************************************************************************/
602114237Snjl
603114237SnjlACPI_STATUS
604245582SjkimAcpiWalkResourceBuffer (
605245582Sjkim    ACPI_BUFFER                 *Buffer,
606167802Sjkim    ACPI_WALK_RESOURCE_CALLBACK UserFunction,
607167802Sjkim    void                        *Context)
608114237Snjl{
609245582Sjkim    ACPI_STATUS                 Status = AE_OK;
610167802Sjkim    ACPI_RESOURCE               *Resource;
611167802Sjkim    ACPI_RESOURCE               *ResourceEnd;
612114237Snjl
613114237Snjl
614245582Sjkim    ACPI_FUNCTION_TRACE (AcpiWalkResourceBuffer);
615167802Sjkim
616167802Sjkim
617167802Sjkim    /* Parameter validation */
618167802Sjkim
619245582Sjkim    if (!Buffer || !Buffer->Pointer || !UserFunction)
620151937Sjkim    {
621167802Sjkim        return_ACPI_STATUS (AE_BAD_PARAMETER);
622167802Sjkim    }
623117521Snjl
624245582Sjkim    /* Buffer contains the resource list and length */
625114237Snjl
626245582Sjkim    Resource = ACPI_CAST_PTR (ACPI_RESOURCE, Buffer->Pointer);
627245582Sjkim    ResourceEnd = ACPI_ADD_PTR (ACPI_RESOURCE, Buffer->Pointer, Buffer->Length);
628117521Snjl
629167802Sjkim    /* Walk the resource list until the EndTag is found (or buffer end) */
630117521Snjl
631167802Sjkim    while (Resource < ResourceEnd)
632167802Sjkim    {
633246849Sjkim        /* Sanity check the resource type */
634114237Snjl
635167802Sjkim        if (Resource->Type > ACPI_RESOURCE_TYPE_MAX)
636167802Sjkim        {
637167802Sjkim            Status = AE_AML_INVALID_RESOURCE_TYPE;
638167802Sjkim            break;
639167802Sjkim        }
640167802Sjkim
641246849Sjkim        /* Sanity check the length. It must not be zero, or we loop forever */
642246849Sjkim
643246849Sjkim        if (!Resource->Length)
644246849Sjkim        {
645246849Sjkim            return_ACPI_STATUS (AE_AML_BAD_RESOURCE_LENGTH);
646246849Sjkim        }
647246849Sjkim
648167802Sjkim        /* Invoke the user function, abort on any error returned */
649167802Sjkim
650167802Sjkim        Status = UserFunction (Resource, Context);
651167802Sjkim        if (ACPI_FAILURE (Status))
652167802Sjkim        {
653167802Sjkim            if (Status == AE_CTRL_TERMINATE)
654167802Sjkim            {
655167802Sjkim                /* This is an OK termination by the user function */
656167802Sjkim
657167802Sjkim                Status = AE_OK;
658167802Sjkim            }
659167802Sjkim            break;
660167802Sjkim        }
661167802Sjkim
662167802Sjkim        /* EndTag indicates end-of-list */
663167802Sjkim
664167802Sjkim        if (Resource->Type == ACPI_RESOURCE_TYPE_END_TAG)
665167802Sjkim        {
666167802Sjkim            break;
667167802Sjkim        }
668167802Sjkim
669167802Sjkim        /* Get the next resource descriptor */
670167802Sjkim
671243347Sjkim        Resource = ACPI_NEXT_RESOURCE (Resource);
672114237Snjl    }
673114237Snjl
674245582Sjkim    return_ACPI_STATUS (Status);
675245582Sjkim}
676245582Sjkim
677245582SjkimACPI_EXPORT_SYMBOL (AcpiWalkResourceBuffer)
678245582Sjkim
679245582Sjkim
680245582Sjkim/*******************************************************************************
681245582Sjkim *
682245582Sjkim * FUNCTION:    AcpiWalkResources
683245582Sjkim *
684245582Sjkim * PARAMETERS:  DeviceHandle    - Handle to the device object for the
685245582Sjkim *                                device we are querying
686245582Sjkim *              Name            - Method name of the resources we want.
687245582Sjkim *                                (METHOD_NAME__CRS, METHOD_NAME__PRS, or
688245582Sjkim *                                METHOD_NAME__AEI)
689245582Sjkim *              UserFunction    - Called for each resource
690245582Sjkim *              Context         - Passed to UserFunction
691245582Sjkim *
692245582Sjkim * RETURN:      Status
693245582Sjkim *
694245582Sjkim * DESCRIPTION: Retrieves the current or possible resource list for the
695245582Sjkim *              specified device. The UserFunction is called once for
696245582Sjkim *              each resource in the list.
697245582Sjkim *
698245582Sjkim ******************************************************************************/
699245582Sjkim
700245582SjkimACPI_STATUS
701245582SjkimAcpiWalkResources (
702245582Sjkim    ACPI_HANDLE                 DeviceHandle,
703245582Sjkim    char                        *Name,
704245582Sjkim    ACPI_WALK_RESOURCE_CALLBACK UserFunction,
705245582Sjkim    void                        *Context)
706245582Sjkim{
707245582Sjkim    ACPI_STATUS                 Status;
708245582Sjkim    ACPI_BUFFER                 Buffer;
709245582Sjkim
710245582Sjkim
711245582Sjkim    ACPI_FUNCTION_TRACE (AcpiWalkResources);
712245582Sjkim
713245582Sjkim
714245582Sjkim    /* Parameter validation */
715245582Sjkim
716245582Sjkim    if (!DeviceHandle || !UserFunction || !Name ||
717245582Sjkim        (!ACPI_COMPARE_NAME (Name, METHOD_NAME__CRS) &&
718245582Sjkim         !ACPI_COMPARE_NAME (Name, METHOD_NAME__PRS) &&
719245582Sjkim         !ACPI_COMPARE_NAME (Name, METHOD_NAME__AEI)))
720245582Sjkim    {
721245582Sjkim        return_ACPI_STATUS (AE_BAD_PARAMETER);
722245582Sjkim    }
723245582Sjkim
724245582Sjkim    /* Get the _CRS/_PRS/_AEI resource list */
725245582Sjkim
726245582Sjkim    Buffer.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
727245582Sjkim    Status = AcpiRsGetMethodData (DeviceHandle, Name, &Buffer);
728245582Sjkim    if (ACPI_FAILURE (Status))
729245582Sjkim    {
730245582Sjkim        return_ACPI_STATUS (Status);
731245582Sjkim    }
732245582Sjkim
733245582Sjkim    /* Walk the resource list and cleanup */
734245582Sjkim
735245582Sjkim    Status = AcpiWalkResourceBuffer (&Buffer, UserFunction, Context);
736167802Sjkim    ACPI_FREE (Buffer.Pointer);
737167802Sjkim    return_ACPI_STATUS (Status);
738114237Snjl}
739167802Sjkim
740167802SjkimACPI_EXPORT_SYMBOL (AcpiWalkResources)
741