27158dd0f877344623689a2a24212f222cc74bba
[sfrench/cifs-2.6.git] / drivers / acpi / utilities / utresrc.c
1 /*******************************************************************************
2  *
3  * Module Name: utresrc - Resource managment utilities
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2006, 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 #include <acpi/acpi.h>
45 #include <acpi/amlresrc.h>
46
47 #define _COMPONENT          ACPI_UTILITIES
48 ACPI_MODULE_NAME("utmisc")
49
50 #if defined(ACPI_DISASSEMBLER) || defined (ACPI_DEBUGGER)
51 /*
52  * Strings used to decode resource descriptors.
53  * Used by both the disasssembler and the debugger resource dump routines
54  */
55 const char *acpi_gbl_BMdecode[2] = {
56         "not_bus_master",
57         "bus_master"
58 };
59
60 const char *acpi_gbl_config_decode[4] = {
61         "0 - Good Configuration",
62         "1 - Acceptable Configuration",
63         "2 - Suboptimal Configuration",
64         "3 - ***Invalid Configuration***",
65 };
66
67 const char *acpi_gbl_consume_decode[2] = {
68         "resource_producer",
69         "resource_consumer"
70 };
71
72 const char *acpi_gbl_DECdecode[2] = {
73         "pos_decode",
74         "sub_decode"
75 };
76
77 const char *acpi_gbl_HEdecode[2] = {
78         "Level",
79         "Edge"
80 };
81
82 const char *acpi_gbl_io_decode[2] = {
83         "Decode10",
84         "Decode16"
85 };
86
87 const char *acpi_gbl_LLdecode[2] = {
88         "active_high",
89         "active_low"
90 };
91
92 const char *acpi_gbl_max_decode[2] = {
93         "max_not_fixed",
94         "max_fixed"
95 };
96
97 const char *acpi_gbl_MEMdecode[4] = {
98         "non_cacheable",
99         "Cacheable",
100         "write_combining",
101         "Prefetchable"
102 };
103
104 const char *acpi_gbl_min_decode[2] = {
105         "min_not_fixed",
106         "min_fixed"
107 };
108
109 const char *acpi_gbl_MTPdecode[4] = {
110         "address_range_memory",
111         "address_range_reserved",
112         "address_range_aCPI",
113         "address_range_nVS"
114 };
115
116 const char *acpi_gbl_RNGdecode[4] = {
117         "invalid_ranges",
118         "non_iSAonly_ranges",
119         "ISAonly_ranges",
120         "entire_range"
121 };
122
123 const char *acpi_gbl_RWdecode[2] = {
124         "read_only",
125         "read_write"
126 };
127
128 const char *acpi_gbl_SHRdecode[2] = {
129         "Exclusive",
130         "Shared"
131 };
132
133 const char *acpi_gbl_SIZdecode[4] = {
134         "Transfer8",
135         "Transfer8_16",
136         "Transfer16",
137         "invalid_size"
138 };
139
140 const char *acpi_gbl_TRSdecode[2] = {
141         "dense_translation",
142         "sparse_translation"
143 };
144
145 const char *acpi_gbl_TTPdecode[2] = {
146         "type_static",
147         "type_translation"
148 };
149
150 const char *acpi_gbl_TYPdecode[4] = {
151         "Compatibility",
152         "type_a",
153         "type_b",
154         "type_f"
155 };
156
157 #endif
158
159 /*
160  * Base sizes of the raw AML resource descriptors, indexed by resource type.
161  * Zero indicates a reserved (and therefore invalid) resource type.
162  */
163 const u8 acpi_gbl_resource_aml_sizes[] = {
164         /* Small descriptors */
165
166         0,
167         0,
168         0,
169         0,
170         ACPI_AML_SIZE_SMALL(struct aml_resource_irq),
171         ACPI_AML_SIZE_SMALL(struct aml_resource_dma),
172         ACPI_AML_SIZE_SMALL(struct aml_resource_start_dependent),
173         ACPI_AML_SIZE_SMALL(struct aml_resource_end_dependent),
174         ACPI_AML_SIZE_SMALL(struct aml_resource_io),
175         ACPI_AML_SIZE_SMALL(struct aml_resource_fixed_io),
176         0,
177         0,
178         0,
179         0,
180         ACPI_AML_SIZE_SMALL(struct aml_resource_vendor_small),
181         ACPI_AML_SIZE_SMALL(struct aml_resource_end_tag),
182
183         /* Large descriptors */
184
185         0,
186         ACPI_AML_SIZE_LARGE(struct aml_resource_memory24),
187         ACPI_AML_SIZE_LARGE(struct aml_resource_generic_register),
188         0,
189         ACPI_AML_SIZE_LARGE(struct aml_resource_vendor_large),
190         ACPI_AML_SIZE_LARGE(struct aml_resource_memory32),
191         ACPI_AML_SIZE_LARGE(struct aml_resource_fixed_memory32),
192         ACPI_AML_SIZE_LARGE(struct aml_resource_address32),
193         ACPI_AML_SIZE_LARGE(struct aml_resource_address16),
194         ACPI_AML_SIZE_LARGE(struct aml_resource_extended_irq),
195         ACPI_AML_SIZE_LARGE(struct aml_resource_address64),
196         ACPI_AML_SIZE_LARGE(struct aml_resource_extended_address64)
197 };
198
199 /*
200  * Resource types, used to validate the resource length field.
201  * The length of fixed-length types must match exactly, variable
202  * lengths must meet the minimum required length, etc.
203  * Zero indicates a reserved (and therefore invalid) resource type.
204  */
205 static const u8 acpi_gbl_resource_types[] = {
206         /* Small descriptors */
207
208         0,
209         0,
210         0,
211         0,
212         ACPI_SMALL_VARIABLE_LENGTH,
213         ACPI_FIXED_LENGTH,
214         ACPI_SMALL_VARIABLE_LENGTH,
215         ACPI_FIXED_LENGTH,
216         ACPI_FIXED_LENGTH,
217         ACPI_FIXED_LENGTH,
218         0,
219         0,
220         0,
221         0,
222         ACPI_VARIABLE_LENGTH,
223         ACPI_FIXED_LENGTH,
224
225         /* Large descriptors */
226
227         0,
228         ACPI_FIXED_LENGTH,
229         ACPI_FIXED_LENGTH,
230         0,
231         ACPI_VARIABLE_LENGTH,
232         ACPI_FIXED_LENGTH,
233         ACPI_FIXED_LENGTH,
234         ACPI_VARIABLE_LENGTH,
235         ACPI_VARIABLE_LENGTH,
236         ACPI_VARIABLE_LENGTH,
237         ACPI_VARIABLE_LENGTH,
238         ACPI_FIXED_LENGTH
239 };
240
241 /*******************************************************************************
242  *
243  * FUNCTION:    acpi_ut_validate_resource
244  *
245  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
246  *              return_index    - Where the resource index is returned. NULL
247  *                                if the index is not required.
248  *
249  * RETURN:      Status, and optionally the Index into the global resource tables
250  *
251  * DESCRIPTION: Validate an AML resource descriptor by checking the Resource
252  *              Type and Resource Length. Returns an index into the global
253  *              resource information/dispatch tables for later use.
254  *
255  ******************************************************************************/
256
257 acpi_status acpi_ut_validate_resource(void *aml, u8 * return_index)
258 {
259         u8 resource_type;
260         u8 resource_index;
261         acpi_rs_length resource_length;
262         acpi_rs_length minimum_resource_length;
263
264         ACPI_FUNCTION_ENTRY();
265
266         /*
267          * 1) Validate the resource_type field (Byte 0)
268          */
269         resource_type = ACPI_GET8(aml);
270
271         /*
272          * Byte 0 contains the descriptor name (Resource Type)
273          * Examine the large/small bit in the resource header
274          */
275         if (resource_type & ACPI_RESOURCE_NAME_LARGE) {
276
277                 /* Verify the large resource type (name) against the max */
278
279                 if (resource_type > ACPI_RESOURCE_NAME_LARGE_MAX) {
280                         return (AE_AML_INVALID_RESOURCE_TYPE);
281                 }
282
283                 /*
284                  * Large Resource Type -- bits 6:0 contain the name
285                  * Translate range 0x80-0x8B to index range 0x10-0x1B
286                  */
287                 resource_index = (u8) (resource_type - 0x70);
288         } else {
289                 /*
290                  * Small Resource Type -- bits 6:3 contain the name
291                  * Shift range to index range 0x00-0x0F
292                  */
293                 resource_index = (u8)
294                     ((resource_type & ACPI_RESOURCE_NAME_SMALL_MASK) >> 3);
295         }
296
297         /* Check validity of the resource type, zero indicates name is invalid */
298
299         if (!acpi_gbl_resource_types[resource_index]) {
300                 return (AE_AML_INVALID_RESOURCE_TYPE);
301         }
302
303         /*
304          * 2) Validate the resource_length field. This ensures that the length
305          *    is at least reasonable, and guarantees that it is non-zero.
306          */
307         resource_length = acpi_ut_get_resource_length(aml);
308         minimum_resource_length = acpi_gbl_resource_aml_sizes[resource_index];
309
310         /* Validate based upon the type of resource - fixed length or variable */
311
312         switch (acpi_gbl_resource_types[resource_index]) {
313         case ACPI_FIXED_LENGTH:
314
315                 /* Fixed length resource, length must match exactly */
316
317                 if (resource_length != minimum_resource_length) {
318                         return (AE_AML_BAD_RESOURCE_LENGTH);
319                 }
320                 break;
321
322         case ACPI_VARIABLE_LENGTH:
323
324                 /* Variable length resource, length must be at least the minimum */
325
326                 if (resource_length < minimum_resource_length) {
327                         return (AE_AML_BAD_RESOURCE_LENGTH);
328                 }
329                 break;
330
331         case ACPI_SMALL_VARIABLE_LENGTH:
332
333                 /* Small variable length resource, length can be (Min) or (Min-1) */
334
335                 if ((resource_length > minimum_resource_length) ||
336                     (resource_length < (minimum_resource_length - 1))) {
337                         return (AE_AML_BAD_RESOURCE_LENGTH);
338                 }
339                 break;
340
341         default:
342
343                 /* Shouldn't happen (because of validation earlier), but be sure */
344
345                 return (AE_AML_INVALID_RESOURCE_TYPE);
346         }
347
348         /* Optionally return the resource table index */
349
350         if (return_index) {
351                 *return_index = resource_index;
352         }
353
354         return (AE_OK);
355 }
356
357 /*******************************************************************************
358  *
359  * FUNCTION:    acpi_ut_get_resource_type
360  *
361  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
362  *
363  * RETURN:      The Resource Type with no extraneous bits (except the
364  *              Large/Small descriptor bit -- this is left alone)
365  *
366  * DESCRIPTION: Extract the Resource Type/Name from the first byte of
367  *              a resource descriptor.
368  *
369  ******************************************************************************/
370
371 u8 acpi_ut_get_resource_type(void *aml)
372 {
373         ACPI_FUNCTION_ENTRY();
374
375         /*
376          * Byte 0 contains the descriptor name (Resource Type)
377          * Examine the large/small bit in the resource header
378          */
379         if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
380
381                 /* Large Resource Type -- bits 6:0 contain the name */
382
383                 return (ACPI_GET8(aml));
384         } else {
385                 /* Small Resource Type -- bits 6:3 contain the name */
386
387                 return ((u8) (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_SMALL_MASK));
388         }
389 }
390
391 /*******************************************************************************
392  *
393  * FUNCTION:    acpi_ut_get_resource_length
394  *
395  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
396  *
397  * RETURN:      Byte Length
398  *
399  * DESCRIPTION: Get the "Resource Length" of a raw AML descriptor. By
400  *              definition, this does not include the size of the descriptor
401  *              header or the length field itself.
402  *
403  ******************************************************************************/
404
405 u16 acpi_ut_get_resource_length(void *aml)
406 {
407         acpi_rs_length resource_length;
408
409         ACPI_FUNCTION_ENTRY();
410
411         /*
412          * Byte 0 contains the descriptor name (Resource Type)
413          * Examine the large/small bit in the resource header
414          */
415         if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
416
417                 /* Large Resource type -- bytes 1-2 contain the 16-bit length */
418
419                 ACPI_MOVE_16_TO_16(&resource_length, ACPI_ADD_PTR(u8, aml, 1));
420
421         } else {
422                 /* Small Resource type -- bits 2:0 of byte 0 contain the length */
423
424                 resource_length = (u16) (ACPI_GET8(aml) &
425                                          ACPI_RESOURCE_NAME_SMALL_LENGTH_MASK);
426         }
427
428         return (resource_length);
429 }
430
431 /*******************************************************************************
432  *
433  * FUNCTION:    acpi_ut_get_resource_header_length
434  *
435  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
436  *
437  * RETURN:      Length of the AML header (depends on large/small descriptor)
438  *
439  * DESCRIPTION: Get the length of the header for this resource.
440  *
441  ******************************************************************************/
442
443 u8 acpi_ut_get_resource_header_length(void *aml)
444 {
445         ACPI_FUNCTION_ENTRY();
446
447         /* Examine the large/small bit in the resource header */
448
449         if (ACPI_GET8(aml) & ACPI_RESOURCE_NAME_LARGE) {
450                 return (sizeof(struct aml_resource_large_header));
451         } else {
452                 return (sizeof(struct aml_resource_small_header));
453         }
454 }
455
456 /*******************************************************************************
457  *
458  * FUNCTION:    acpi_ut_get_descriptor_length
459  *
460  * PARAMETERS:  Aml             - Pointer to the raw AML resource descriptor
461  *
462  * RETURN:      Byte length
463  *
464  * DESCRIPTION: Get the total byte length of a raw AML descriptor, including the
465  *              length of the descriptor header and the length field itself.
466  *              Used to walk descriptor lists.
467  *
468  ******************************************************************************/
469
470 u32 acpi_ut_get_descriptor_length(void *aml)
471 {
472         ACPI_FUNCTION_ENTRY();
473
474         /*
475          * Get the Resource Length (does not include header length) and add
476          * the header length (depends on if this is a small or large resource)
477          */
478         return (acpi_ut_get_resource_length(aml) +
479                 acpi_ut_get_resource_header_length(aml));
480 }
481
482 /*******************************************************************************
483  *
484  * FUNCTION:    acpi_ut_get_resource_end_tag
485  *
486  * PARAMETERS:  obj_desc        - The resource template buffer object
487  *              end_tag         - Where the pointer to the end_tag is returned
488  *
489  * RETURN:      Status, pointer to the end tag
490  *
491  * DESCRIPTION: Find the end_tag resource descriptor in an AML resource template
492  *              Note: allows a buffer length of zero.
493  *
494  ******************************************************************************/
495
496 acpi_status
497 acpi_ut_get_resource_end_tag(union acpi_operand_object * obj_desc,
498                              u8 ** end_tag)
499 {
500         acpi_status status;
501         u8 *aml;
502         u8 *end_aml;
503
504         ACPI_FUNCTION_TRACE("ut_get_resource_end_tag");
505
506         /* Get start and end pointers */
507
508         aml = obj_desc->buffer.pointer;
509         end_aml = aml + obj_desc->buffer.length;
510
511         /* Allow a buffer length of zero */
512
513         if (!obj_desc->buffer.length) {
514                 *end_tag = aml;
515                 return_ACPI_STATUS(AE_OK);
516         }
517
518         /* Walk the resource template, one descriptor per iteration */
519
520         while (aml < end_aml) {
521
522                 /* Validate the Resource Type and Resource Length */
523
524                 status = acpi_ut_validate_resource(aml, NULL);
525                 if (ACPI_FAILURE(status)) {
526                         return_ACPI_STATUS(status);
527                 }
528
529                 /* end_tag resource indicates the end of the resource template */
530
531                 if (acpi_ut_get_resource_type(aml) ==
532                     ACPI_RESOURCE_NAME_END_TAG) {
533                         /*
534                          * There must be at least one more byte in the buffer for
535                          * the 2nd byte of the end_tag
536                          */
537                         if ((aml + 1) >= end_aml) {
538                                 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
539                         }
540
541                         /* Return the pointer to the end_tag */
542
543                         *end_tag = aml;
544                         return_ACPI_STATUS(AE_OK);
545                 }
546
547                 /*
548                  * Point to the next resource descriptor in the AML buffer. The
549                  * descriptor length is guaranteed to be non-zero by resource
550                  * validation above.
551                  */
552                 aml += acpi_ut_get_descriptor_length(aml);
553         }
554
555         /* Did not find an end_tag resource descriptor */
556
557         return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
558 }