vserver 1.9.3
[linux-2.6.git] / drivers / acpi / resources / rsxface.c
1 /*******************************************************************************
2  *
3  * Module Name: rsxface - Public interfaces to the resource manager
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2004, R. Byron Moore
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 <acpi/acpi.h>
46 #include <acpi/acresrc.h>
47
48 #define _COMPONENT          ACPI_RESOURCES
49          ACPI_MODULE_NAME    ("rsxface")
50
51
52 /*******************************************************************************
53  *
54  * FUNCTION:    acpi_get_irq_routing_table
55  *
56  * PARAMETERS:  device_handle   - a handle to the Bus device we are querying
57  *              ret_buffer      - a pointer to a buffer to receive the
58  *                                current resources for the device
59  *
60  * RETURN:      Status
61  *
62  * DESCRIPTION: This function is called to get the IRQ routing table for a
63  *              specific bus.  The caller must first acquire a handle for the
64  *              desired bus.  The routine table is placed in the buffer pointed
65  *              to by the ret_buffer variable parameter.
66  *
67  *              If the function fails an appropriate status will be returned
68  *              and the value of ret_buffer is undefined.
69  *
70  *              This function attempts to execute the _PRT method contained in
71  *              the object indicated by the passed device_handle.
72  *
73  ******************************************************************************/
74
75 acpi_status
76 acpi_get_irq_routing_table (
77         acpi_handle                     device_handle,
78         struct acpi_buffer              *ret_buffer)
79 {
80         acpi_status                     status;
81
82
83         ACPI_FUNCTION_TRACE ("acpi_get_irq_routing_table ");
84
85
86         /*
87          * Must have a valid handle and buffer, So we have to have a handle
88          * and a return buffer structure, and if there is a non-zero buffer length
89          * we also need a valid pointer in the buffer. If it's a zero buffer length,
90          * we'll be returning the needed buffer size, so keep going.
91          */
92         if (!device_handle) {
93                 return_ACPI_STATUS (AE_BAD_PARAMETER);
94         }
95
96         status = acpi_ut_validate_buffer (ret_buffer);
97         if (ACPI_FAILURE (status)) {
98                 return_ACPI_STATUS (status);
99         }
100
101         status = acpi_rs_get_prt_method_data (device_handle, ret_buffer);
102         return_ACPI_STATUS (status);
103 }
104
105
106 /*******************************************************************************
107  *
108  * FUNCTION:    acpi_get_current_resources
109  *
110  * PARAMETERS:  device_handle   - a handle to the device object for the
111  *                                device we are querying
112  *              ret_buffer      - a pointer to a buffer to receive the
113  *                                current resources for the device
114  *
115  * RETURN:      Status
116  *
117  * DESCRIPTION: This function is called to get the current resources for a
118  *              specific device.  The caller must first acquire a handle for
119  *              the desired device.  The resource data is placed in the buffer
120  *              pointed to by the ret_buffer variable parameter.
121  *
122  *              If the function fails an appropriate status will be returned
123  *              and the value of ret_buffer is undefined.
124  *
125  *              This function attempts to execute the _CRS method contained in
126  *              the object indicated by the passed device_handle.
127  *
128  ******************************************************************************/
129
130 acpi_status
131 acpi_get_current_resources (
132         acpi_handle                     device_handle,
133         struct acpi_buffer              *ret_buffer)
134 {
135         acpi_status                     status;
136
137
138         ACPI_FUNCTION_TRACE ("acpi_get_current_resources");
139
140
141         /*
142          * Must have a valid handle and buffer, So we have to have a handle
143          * and a return buffer structure, and if there is a non-zero buffer length
144          * we also need a valid pointer in the buffer. If it's a zero buffer length,
145          * we'll be returning the needed buffer size, so keep going.
146          */
147         if (!device_handle) {
148                 return_ACPI_STATUS (AE_BAD_PARAMETER);
149         }
150
151         status = acpi_ut_validate_buffer (ret_buffer);
152         if (ACPI_FAILURE (status)) {
153                 return_ACPI_STATUS (status);
154         }
155
156         status = acpi_rs_get_crs_method_data (device_handle, ret_buffer);
157         return_ACPI_STATUS (status);
158 }
159
160
161 /*******************************************************************************
162  *
163  * FUNCTION:    acpi_get_possible_resources
164  *
165  * PARAMETERS:  device_handle   - a handle to the device object for the
166  *                                device we are querying
167  *              ret_buffer      - a pointer to a buffer to receive the
168  *                                resources for the device
169  *
170  * RETURN:      Status
171  *
172  * DESCRIPTION: This function is called to get a list of the possible resources
173  *              for a specific device.  The caller must first acquire a handle
174  *              for the desired device.  The resource data is placed in the
175  *              buffer pointed to by the ret_buffer variable.
176  *
177  *              If the function fails an appropriate status will be returned
178  *              and the value of ret_buffer is undefined.
179  *
180  ******************************************************************************/
181
182 acpi_status
183 acpi_get_possible_resources (
184         acpi_handle                     device_handle,
185         struct acpi_buffer              *ret_buffer)
186 {
187         acpi_status                     status;
188
189
190         ACPI_FUNCTION_TRACE ("acpi_get_possible_resources");
191
192
193         /*
194          * Must have a valid handle and buffer, So we have to have a handle
195          * and a return buffer structure, and if there is a non-zero buffer length
196          * we also need a valid pointer in the buffer. If it's a zero buffer length,
197          * we'll be returning the needed buffer size, so keep going.
198          */
199         if (!device_handle) {
200                 return_ACPI_STATUS (AE_BAD_PARAMETER);
201         }
202
203         status = acpi_ut_validate_buffer (ret_buffer);
204         if (ACPI_FAILURE (status)) {
205                 return_ACPI_STATUS (status);
206         }
207
208         status = acpi_rs_get_prs_method_data (device_handle, ret_buffer);
209         return_ACPI_STATUS (status);
210 }
211
212
213 /*******************************************************************************
214  *
215  * FUNCTION:    acpi_walk_resources
216  *
217  * PARAMETERS:  device_handle   - a handle to the device object for the
218  *                                device we are querying
219  *              Path            - method name of the resources we want
220  *                                (METHOD_NAME__CRS or METHOD_NAME__PRS)
221  *              user_function   - called for each resource
222  *              Context         - passed to user_function
223  *
224  * RETURN:      Status
225  *
226  * DESCRIPTION: Retrieves the current or possible resource list for the
227  *              specified device.  The user_function is called once for
228  *              each resource in the list.
229  *
230  ******************************************************************************/
231
232 acpi_status
233 acpi_walk_resources (
234         acpi_handle                             device_handle,
235         char                                    *path,
236         ACPI_WALK_RESOURCE_CALLBACK     user_function,
237         void                                    *context)
238 {
239         acpi_status                         status;
240         struct acpi_buffer                  buffer = {ACPI_ALLOCATE_BUFFER, NULL};
241         struct acpi_resource                *resource;
242         struct acpi_resource                *buffer_end;
243
244
245         ACPI_FUNCTION_TRACE ("acpi_walk_resources");
246
247
248         if (!device_handle ||
249                 (ACPI_STRNCMP (path, METHOD_NAME__CRS, sizeof (METHOD_NAME__CRS)) &&
250                  ACPI_STRNCMP (path, METHOD_NAME__PRS, sizeof (METHOD_NAME__PRS)))) {
251                 return_ACPI_STATUS (AE_BAD_PARAMETER);
252         }
253
254         status = acpi_rs_get_method_data (device_handle, path, &buffer);
255         if (ACPI_FAILURE (status)) {
256                 return_ACPI_STATUS (status);
257         }
258
259         /* Setup pointers */
260
261         resource  = (struct acpi_resource *) buffer.pointer;
262         buffer_end = ACPI_CAST_PTR (struct acpi_resource,
263                           ((u8 *) buffer.pointer + buffer.length));
264
265         /* Walk the resource list */
266
267         for (;;) {
268                 if (!resource || resource->id == ACPI_RSTYPE_END_TAG) {
269                         break;
270                 }
271
272                 status = user_function (resource, context);
273
274                 switch (status) {
275                 case AE_OK:
276                 case AE_CTRL_DEPTH:
277
278                         /* Just keep going */
279
280                         status = AE_OK;
281                         break;
282
283                 case AE_CTRL_TERMINATE:
284
285                         /* Exit now, with OK stats */
286
287                         status = AE_OK;
288                         goto cleanup;
289
290                 default:
291
292                         /* All others are valid exceptions */
293
294                         goto cleanup;
295                 }
296
297                 /* Get the next resource descriptor */
298
299                 resource = ACPI_NEXT_RESOURCE (resource);
300
301                 /* Check for end-of-buffer */
302
303                 if (resource >= buffer_end) {
304                         goto cleanup;
305                 }
306         }
307
308 cleanup:
309
310         acpi_os_free (buffer.pointer);
311         return_ACPI_STATUS (status);
312 }
313
314
315 /*******************************************************************************
316  *
317  * FUNCTION:    acpi_set_current_resources
318  *
319  * PARAMETERS:  device_handle   - a handle to the device object for the
320  *                                device we are changing the resources of
321  *              in_buffer       - a pointer to a buffer containing the
322  *                                resources to be set for the device
323  *
324  * RETURN:      Status
325  *
326  * DESCRIPTION: This function is called to set the current resources for a
327  *              specific device.  The caller must first acquire a handle for
328  *              the desired device.  The resource data is passed to the routine
329  *              the buffer pointed to by the in_buffer variable.
330  *
331  ******************************************************************************/
332
333 acpi_status
334 acpi_set_current_resources (
335         acpi_handle                     device_handle,
336         struct acpi_buffer              *in_buffer)
337 {
338         acpi_status                     status;
339
340
341         ACPI_FUNCTION_TRACE ("acpi_set_current_resources");
342
343
344         /*
345          * Must have a valid handle and buffer
346          */
347         if ((!device_handle)      ||
348                 (!in_buffer)          ||
349                 (!in_buffer->pointer) ||
350                 (!in_buffer->length)) {
351                 return_ACPI_STATUS (AE_BAD_PARAMETER);
352         }
353
354         status = acpi_rs_set_srs_method_data (device_handle, in_buffer);
355         return_ACPI_STATUS (status);
356 }
357
358
359 #define ACPI_COPY_FIELD(out, in, field)  ((out)->field = (in)->field)
360 #define ACPI_COPY_ADDRESS(out, in)                      \
361         ACPI_COPY_FIELD(out, in, resource_type);             \
362         ACPI_COPY_FIELD(out, in, producer_consumer);         \
363         ACPI_COPY_FIELD(out, in, decode);                    \
364         ACPI_COPY_FIELD(out, in, min_address_fixed);         \
365         ACPI_COPY_FIELD(out, in, max_address_fixed);         \
366         ACPI_COPY_FIELD(out, in, attribute);                 \
367         ACPI_COPY_FIELD(out, in, granularity);               \
368         ACPI_COPY_FIELD(out, in, min_address_range);         \
369         ACPI_COPY_FIELD(out, in, max_address_range);         \
370         ACPI_COPY_FIELD(out, in, address_translation_offset); \
371         ACPI_COPY_FIELD(out, in, address_length);            \
372         ACPI_COPY_FIELD(out, in, resource_source);
373
374 /******************************************************************************
375  *
376  * FUNCTION:    acpi_resource_to_address64
377  *
378  * PARAMETERS:  resource                - Pointer to a resource
379  *              out                     - Pointer to the users's return
380  *                                        buffer (a struct
381  *                                        struct acpi_resource_address64)
382  *
383  * RETURN:      Status
384  *
385  * DESCRIPTION: If the resource is an address16, address32, or address64,
386  *              copy it to the address64 return buffer.  This saves the
387  *              caller from having to duplicate code for different-sized
388  *              addresses.
389  *
390  ******************************************************************************/
391
392 acpi_status
393 acpi_resource_to_address64 (
394         struct acpi_resource                *resource,
395         struct acpi_resource_address64      *out)
396 {
397         struct acpi_resource_address16      *address16;
398         struct acpi_resource_address32      *address32;
399
400
401         switch (resource->id) {
402         case ACPI_RSTYPE_ADDRESS16:
403
404                 address16 = (struct acpi_resource_address16 *) &resource->data;
405                 ACPI_COPY_ADDRESS(out, address16);
406                 break;
407
408
409         case ACPI_RSTYPE_ADDRESS32:
410
411                 address32 = (struct acpi_resource_address32 *) &resource->data;
412                 ACPI_COPY_ADDRESS(out, address32);
413                 break;
414
415
416         case ACPI_RSTYPE_ADDRESS64:
417
418                 /* Simple copy for 64 bit source */
419
420                 ACPI_MEMCPY (out, &resource->data, sizeof (struct acpi_resource_address64));
421                 break;
422
423
424         default:
425                 return (AE_BAD_PARAMETER);
426         }
427
428         return (AE_OK);
429 }