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
acpi_get_irq_routing_table(acpi_handle device_handle,struct acpi_buffer * ret_buffer)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
acpi_get_current_resources(acpi_handle device_handle,struct acpi_buffer * ret_buffer)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
acpi_get_possible_resources(acpi_handle device_handle,struct acpi_buffer * ret_buffer)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
acpi_walk_resources(acpi_handle device_handle,char * path,ACPI_WALK_RESOURCE_CALLBACK user_function,void * context)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 = (struct acpi_resource *) ((u8 *) buffer.pointer + buffer.length);
263 
264 	/* Walk the resource list */
265 
266 	for (;;) {
267 		if (!resource || resource->id == ACPI_RSTYPE_END_TAG) {
268 			break;
269 		}
270 
271 		status = user_function (resource, context);
272 
273 		switch (status) {
274 		case AE_OK:
275 		case AE_CTRL_DEPTH:
276 
277 			/* Just keep going */
278 
279 			status = AE_OK;
280 			break;
281 
282 		case AE_CTRL_TERMINATE:
283 
284 			/* Exit now, with OK stats */
285 
286 			status = AE_OK;
287 			goto cleanup;
288 
289 		default:
290 
291 			/* All others are valid exceptions */
292 
293 			goto cleanup;
294 		}
295 
296 		/* Get the next resource descriptor */
297 
298 		resource = ACPI_NEXT_RESOURCE (resource);
299 
300 		/* Check for end-of-buffer */
301 
302 		if (resource >= buffer_end) {
303 			goto cleanup;
304 		}
305 	}
306 
307 cleanup:
308 
309 	acpi_os_free (buffer.pointer);
310 	return_ACPI_STATUS (status);
311 }
312 
313 
314 /*******************************************************************************
315  *
316  * FUNCTION:    acpi_set_current_resources
317  *
318  * PARAMETERS:  device_handle   - a handle to the device object for the
319  *                                device we are changing the resources of
320  *              in_buffer       - a pointer to a buffer containing the
321  *                                resources to be set for the device
322  *
323  * RETURN:      Status
324  *
325  * DESCRIPTION: This function is called to set the current resources for a
326  *              specific device.  The caller must first acquire a handle for
327  *              the desired device.  The resource data is passed to the routine
328  *              the buffer pointed to by the in_buffer variable.
329  *
330  ******************************************************************************/
331 
332 acpi_status
acpi_set_current_resources(acpi_handle device_handle,struct acpi_buffer * in_buffer)333 acpi_set_current_resources (
334 	acpi_handle                     device_handle,
335 	struct acpi_buffer              *in_buffer)
336 {
337 	acpi_status                     status;
338 
339 
340 	ACPI_FUNCTION_TRACE ("acpi_set_current_resources");
341 
342 
343 	/*
344 	 * Must have a valid handle and buffer
345 	 */
346 	if ((!device_handle)      ||
347 		(!in_buffer)          ||
348 		(!in_buffer->pointer) ||
349 		(!in_buffer->length)) {
350 		return_ACPI_STATUS (AE_BAD_PARAMETER);
351 	}
352 
353 	status = acpi_rs_set_srs_method_data (device_handle, in_buffer);
354 	return_ACPI_STATUS (status);
355 }
356 
357 
358 #define ACPI_COPY_FIELD(out, in, field)  ((out)->field = (in)->field)
359 #define ACPI_COPY_ADDRESS(out, in)                      \
360 	ACPI_COPY_FIELD(out, in, resource_type);             \
361 	ACPI_COPY_FIELD(out, in, producer_consumer);         \
362 	ACPI_COPY_FIELD(out, in, decode);                    \
363 	ACPI_COPY_FIELD(out, in, min_address_fixed);         \
364 	ACPI_COPY_FIELD(out, in, max_address_fixed);         \
365 	ACPI_COPY_FIELD(out, in, attribute);                 \
366 	ACPI_COPY_FIELD(out, in, granularity);               \
367 	ACPI_COPY_FIELD(out, in, min_address_range);         \
368 	ACPI_COPY_FIELD(out, in, max_address_range);         \
369 	ACPI_COPY_FIELD(out, in, address_translation_offset); \
370 	ACPI_COPY_FIELD(out, in, address_length);            \
371 	ACPI_COPY_FIELD(out, in, resource_source);
372 
373 /******************************************************************************
374  *
375  * FUNCTION:    acpi_resource_to_address64
376  *
377  * PARAMETERS:  resource                - Pointer to a resource
378  *              out                     - Pointer to the users's return
379  *                                        buffer (a struct
380  *                                        struct acpi_resource_address64)
381  *
382  * RETURN:      Status
383  *
384  * DESCRIPTION: If the resource is an address16, address32, or address64,
385  *              copy it to the address64 return buffer.  This saves the
386  *              caller from having to duplicate code for different-sized
387  *              addresses.
388  *
389  ******************************************************************************/
390 
391 acpi_status
acpi_resource_to_address64(struct acpi_resource * resource,struct acpi_resource_address64 * out)392 acpi_resource_to_address64 (
393 	struct acpi_resource                *resource,
394 	struct acpi_resource_address64      *out)
395 {
396 	struct acpi_resource_address16      *address16;
397 	struct acpi_resource_address32      *address32;
398 
399 
400 	switch (resource->id) {
401 	case ACPI_RSTYPE_ADDRESS16:
402 
403 		address16 = (struct acpi_resource_address16 *) &resource->data;
404 		ACPI_COPY_ADDRESS(out, address16);
405 		break;
406 
407 
408 	case ACPI_RSTYPE_ADDRESS32:
409 
410 		address32 = (struct acpi_resource_address32 *) &resource->data;
411 		ACPI_COPY_ADDRESS(out, address32);
412 		break;
413 
414 
415 	case ACPI_RSTYPE_ADDRESS64:
416 
417 		/* Simple copy for 64 bit source */
418 
419 		ACPI_MEMCPY (out, &resource->data, sizeof (struct acpi_resource_address64));
420 		break;
421 
422 
423 	default:
424 		return (AE_BAD_PARAMETER);
425 	}
426 
427 	return (AE_OK);
428 }
429