blob: fcf129d27baa517d0c4099009d8c391f8bcf7647 [file] [log] [blame]
Erik Schmauss95857632018-03-14 16:13:07 -07001// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*******************************************************************************
3 *
4 * Module Name: rscalc - Calculate stream and list lengths
5 *
6 ******************************************************************************/
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -05009#include "accommon.h"
10#include "acresrc.h"
11#include "acnamesp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012
13#define _COMPONENT ACPI_RESOURCES
Len Brown4be44fc2005-08-05 00:44:28 -040014ACPI_MODULE_NAME("rscalc")
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Robert Moorebda663d2005-09-16 16:51:15 -040016/* Local prototypes */
Robert Moorebda663d2005-09-16 16:51:15 -040017static u8 acpi_rs_count_set_bits(u16 bit_field);
18
Bob Moore08978312005-10-21 00:00:00 -040019static acpi_rs_length
Robert Moorebda663d2005-09-16 16:51:15 -040020acpi_rs_struct_option_length(struct acpi_resource_source *resource_source);
21
22static u32
23acpi_rs_stream_option_length(u32 resource_length, u32 minimum_total_length);
24
25/*******************************************************************************
26 *
27 * FUNCTION: acpi_rs_count_set_bits
28 *
29 * PARAMETERS: bit_field - Field in which to count bits
30 *
31 * RETURN: Number of bits set within the field
32 *
33 * DESCRIPTION: Count the number of bits set in a resource field. Used for
34 * (Short descriptor) interrupt and DMA lists.
35 *
36 ******************************************************************************/
37
38static u8 acpi_rs_count_set_bits(u16 bit_field)
39{
Bob Moore67a119f2008-06-10 13:42:13 +080040 u8 bits_set;
Robert Moorebda663d2005-09-16 16:51:15 -040041
42 ACPI_FUNCTION_ENTRY();
43
44 for (bits_set = 0; bit_field; bits_set++) {
Bob Moore52fc0b02006-10-02 00:00:00 -040045
Robert Moorebda663d2005-09-16 16:51:15 -040046 /* Zero the least significant bit that is set */
47
Bob Moore1d18c052008-04-10 19:06:40 +040048 bit_field &= (u16) (bit_field - 1);
Robert Moorebda663d2005-09-16 16:51:15 -040049 }
50
Lv Zheng9c0d7932012-12-19 05:37:21 +000051 return (bits_set);
Robert Moorebda663d2005-09-16 16:51:15 -040052}
53
54/*******************************************************************************
55 *
Robert Moorebda663d2005-09-16 16:51:15 -040056 * FUNCTION: acpi_rs_struct_option_length
57 *
58 * PARAMETERS: resource_source - Pointer to optional descriptor field
59 *
60 * RETURN: Status
61 *
62 * DESCRIPTION: Common code to handle optional resource_source_index and
63 * resource_source fields in some Large descriptors. Used during
64 * list-to-stream conversion
65 *
66 ******************************************************************************/
67
Bob Moore08978312005-10-21 00:00:00 -040068static acpi_rs_length
Robert Moorebda663d2005-09-16 16:51:15 -040069acpi_rs_struct_option_length(struct acpi_resource_source *resource_source)
70{
71 ACPI_FUNCTION_ENTRY();
72
73 /*
74 * If the resource_source string is valid, return the size of the string
75 * (string_length includes the NULL terminator) plus the size of the
76 * resource_source_index (1).
77 */
78 if (resource_source->string_ptr) {
Lv Zhengf5c1e1c2016-05-05 12:57:53 +080079 return ((acpi_rs_length)(resource_source->string_length + 1));
Robert Moorebda663d2005-09-16 16:51:15 -040080 }
81
82 return (0);
83}
84
85/*******************************************************************************
86 *
87 * FUNCTION: acpi_rs_stream_option_length
88 *
89 * PARAMETERS: resource_length - Length from the resource header
90 * minimum_total_length - Minimum length of this resource, before
91 * any optional fields. Includes header size
92 *
93 * RETURN: Length of optional string (0 if no string present)
94 *
95 * DESCRIPTION: Common code to handle optional resource_source_index and
96 * resource_source fields in some Large descriptors. Used during
97 * stream-to-list conversion
98 *
99 ******************************************************************************/
100
101static u32
Bob Moore50eca3e2005-09-30 19:03:00 -0400102acpi_rs_stream_option_length(u32 resource_length,
103 u32 minimum_aml_resource_length)
Robert Moorebda663d2005-09-16 16:51:15 -0400104{
105 u32 string_length = 0;
Robert Moorebda663d2005-09-16 16:51:15 -0400106
107 ACPI_FUNCTION_ENTRY();
108
109 /*
Bob Moore1fad8732015-12-29 13:54:36 +0800110 * The resource_source_index and resource_source are optional elements of
111 * some Large-type resource descriptors.
Robert Moorebda663d2005-09-16 16:51:15 -0400112 */
113
Robert Moorebda663d2005-09-16 16:51:15 -0400114 /*
Bob Moore1fad8732015-12-29 13:54:36 +0800115 * If the length of the actual resource descriptor is greater than the
116 * ACPI spec-defined minimum length, it means that a resource_source_index
117 * exists and is followed by a (required) null terminated string. The
118 * string length (including the null terminator) is the resource length
119 * minus the minimum length, minus one byte for the resource_source_index
120 * itself.
Robert Moorebda663d2005-09-16 16:51:15 -0400121 */
Bob Moore50eca3e2005-09-30 19:03:00 -0400122 if (resource_length > minimum_aml_resource_length) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400123
Robert Moorebda663d2005-09-16 16:51:15 -0400124 /* Compute the length of the optional string */
125
Bob Moore50eca3e2005-09-30 19:03:00 -0400126 string_length =
127 resource_length - minimum_aml_resource_length - 1;
Robert Moorebda663d2005-09-16 16:51:15 -0400128 }
129
Bob Mooreea936b72006-02-17 00:00:00 -0500130 /*
131 * Round the length up to a multiple of the native word in order to
132 * guarantee that the entire resource descriptor is native word aligned
133 */
134 return ((u32) ACPI_ROUND_UP_TO_NATIVE_WORD(string_length));
Robert Moorebda663d2005-09-16 16:51:15 -0400135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/*******************************************************************************
138 *
Bob Moore50eca3e2005-09-30 19:03:00 -0400139 * FUNCTION: acpi_rs_get_aml_length
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *
Bob Mooreba494be2012-07-12 09:40:10 +0800141 * PARAMETERS: resource - Pointer to the resource linked list
Lv Zheng9a0a3592013-11-21 12:17:34 +0800142 * resource_list_size - Size of the resource linked list
Robert Moorebda663d2005-09-16 16:51:15 -0400143 * size_needed - Where the required size is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 *
145 * RETURN: Status
146 *
Robert Moorebda663d2005-09-16 16:51:15 -0400147 * DESCRIPTION: Takes a linked list of internal resource descriptors and
148 * calculates the size buffer needed to hold the corresponding
149 * external resource byte stream.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
151 ******************************************************************************/
Robert Moorebda663d2005-09-16 16:51:15 -0400152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153acpi_status
Lv Zheng9a0a3592013-11-21 12:17:34 +0800154acpi_rs_get_aml_length(struct acpi_resource *resource,
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800155 acpi_size resource_list_size, acpi_size *size_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Bob Moore50eca3e2005-09-30 19:03:00 -0400157 acpi_size aml_size_needed = 0;
Lv Zheng9a0a3592013-11-21 12:17:34 +0800158 struct acpi_resource *resource_end;
Bob Moore08978312005-10-21 00:00:00 -0400159 acpi_rs_length total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Bob Mooreb229cf92006-04-21 17:15:00 -0400161 ACPI_FUNCTION_TRACE(rs_get_aml_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Robert Moorebda663d2005-09-16 16:51:15 -0400163 /* Traverse entire list of internal resource descriptors */
Robert Moore44f6c012005-04-18 22:49:35 -0400164
Lv Zheng9a0a3592013-11-21 12:17:34 +0800165 resource_end =
166 ACPI_ADD_PTR(struct acpi_resource, resource, resource_list_size);
167 while (resource < resource_end) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400168
Robert Moorebda663d2005-09-16 16:51:15 -0400169 /* Validate the descriptor type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Bob Moore50eca3e2005-09-30 19:03:00 -0400171 if (resource->type > ACPI_RESOURCE_TYPE_MAX) {
Robert Moorebda663d2005-09-16 16:51:15 -0400172 return_ACPI_STATUS(AE_AML_INVALID_RESOURCE_TYPE);
173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Bob Moorec13085e2013-03-08 09:19:38 +0000175 /* Sanity check the length. It must not be zero, or we loop forever */
176
177 if (!resource->length) {
178 return_ACPI_STATUS(AE_AML_BAD_RESOURCE_LENGTH);
179 }
180
Robert Moorebda663d2005-09-16 16:51:15 -0400181 /* Get the base size of the (external stream) resource descriptor */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Bob Moore08978312005-10-21 00:00:00 -0400183 total_size = acpi_gbl_aml_resource_sizes[resource->type];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Robert Moorebda663d2005-09-16 16:51:15 -0400185 /*
186 * Augment the base size for descriptors with optional and/or
187 * variable-length fields
188 */
189 switch (resource->type) {
Bob Moore1d5b2852008-04-10 19:06:43 +0400190 case ACPI_RESOURCE_TYPE_IRQ:
191
Bob Moore66d3ca92008-04-10 19:06:44 +0400192 /* Length can be 3 or 2 */
193
Bob Moore1d5b2852008-04-10 19:06:43 +0400194 if (resource->data.irq.descriptor_length == 2) {
195 total_size--;
196 }
197 break;
198
Bob Moore66d3ca92008-04-10 19:06:44 +0400199 case ACPI_RESOURCE_TYPE_START_DEPENDENT:
200
201 /* Length can be 1 or 0 */
202
203 if (resource->data.irq.descriptor_length == 0) {
204 total_size--;
205 }
206 break;
207
Bob Moore50eca3e2005-09-30 19:03:00 -0400208 case ACPI_RESOURCE_TYPE_VENDOR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400210 * Vendor Defined Resource:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * For a Vendor Specific resource, if the Length is between 1 and 7
212 * it will be created as a Small Resource data type, otherwise it
213 * is a Large Resource data type.
214 */
Bob Moore50eca3e2005-09-30 19:03:00 -0400215 if (resource->data.vendor.byte_length > 7) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400216
Robert Moorebda663d2005-09-16 16:51:15 -0400217 /* Base size of a Large resource descriptor */
218
Bob Moore08978312005-10-21 00:00:00 -0400219 total_size =
Bob Moore50eca3e2005-09-30 19:03:00 -0400220 sizeof(struct aml_resource_large_header);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Robert Moorebda663d2005-09-16 16:51:15 -0400222
223 /* Add the size of the vendor-specific data */
224
Bob Moore08978312005-10-21 00:00:00 -0400225 total_size = (acpi_rs_length)
226 (total_size + resource->data.vendor.byte_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
228
Bob Moore50eca3e2005-09-30 19:03:00 -0400229 case ACPI_RESOURCE_TYPE_END_TAG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400231 * End Tag:
232 * We are done -- return the accumulated total size.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 */
Bob Moore08978312005-10-21 00:00:00 -0400234 *size_needed = aml_size_needed + total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Robert Moorebda663d2005-09-16 16:51:15 -0400236 /* Normal exit */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Robert Moorebda663d2005-09-16 16:51:15 -0400238 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Bob Moore50eca3e2005-09-30 19:03:00 -0400240 case ACPI_RESOURCE_TYPE_ADDRESS16:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400242 * 16-Bit Address Resource:
243 * Add the size of the optional resource_source info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800245 total_size = (acpi_rs_length)(total_size +
246 acpi_rs_struct_option_length
247 (&resource->data.
248 address16.
249 resource_source));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251
Bob Moore50eca3e2005-09-30 19:03:00 -0400252 case ACPI_RESOURCE_TYPE_ADDRESS32:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400254 * 32-Bit Address Resource:
255 * Add the size of the optional resource_source info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800257 total_size = (acpi_rs_length)(total_size +
258 acpi_rs_struct_option_length
259 (&resource->data.
260 address32.
261 resource_source));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 break;
263
Bob Moore50eca3e2005-09-30 19:03:00 -0400264 case ACPI_RESOURCE_TYPE_ADDRESS64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400266 * 64-Bit Address Resource:
267 * Add the size of the optional resource_source info
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 */
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800269 total_size = (acpi_rs_length)(total_size +
270 acpi_rs_struct_option_length
271 (&resource->data.
272 address64.
273 resource_source));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
275
Bob Moore50eca3e2005-09-30 19:03:00 -0400276 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 /*
Robert Moorebda663d2005-09-16 16:51:15 -0400278 * Extended IRQ Resource:
279 * Add the size of each additional optional interrupt beyond the
280 * required 1 (4 bytes for each u32 interrupt number)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 */
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800282 total_size = (acpi_rs_length)(total_size +
283 ((resource->data.
Bob Moore1fad8732015-12-29 13:54:36 +0800284 extended_irq.
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800285 interrupt_count -
286 1) * 4) +
287 /* Add the size of the optional resource_source info */
288 acpi_rs_struct_option_length
289 (&resource->data.
290 extended_irq.
291 resource_source));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 break;
293
Lin Minge0fe0a82011-11-16 14:38:13 +0800294 case ACPI_RESOURCE_TYPE_GPIO:
295
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800296 total_size = (acpi_rs_length)(total_size +
297 (resource->data.gpio.
298 pin_table_length * 2) +
299 resource->data.gpio.
300 resource_source.
301 string_length +
302 resource->data.gpio.
303 vendor_length);
Lin Minge0fe0a82011-11-16 14:38:13 +0800304
305 break;
306
Mika Westerberg2b726932017-06-05 16:39:14 +0800307 case ACPI_RESOURCE_TYPE_PIN_FUNCTION:
308
309 total_size = (acpi_rs_length)(total_size +
310 (resource->data.
311 pin_function.
312 pin_table_length * 2) +
313 resource->data.
314 pin_function.
315 resource_source.
316 string_length +
317 resource->data.
318 pin_function.
319 vendor_length);
320
321 break;
322
Lin Minge0fe0a82011-11-16 14:38:13 +0800323 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
324
325 total_size =
326 acpi_gbl_aml_resource_serial_bus_sizes[resource->
327 data.
328 common_serial_bus.
329 type];
330
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800331 total_size = (acpi_rs_length)(total_size +
332 resource->data.
333 i2c_serial_bus.
334 resource_source.
335 string_length +
336 resource->data.
337 i2c_serial_bus.
338 vendor_length);
Lin Minge0fe0a82011-11-16 14:38:13 +0800339
340 break;
341
Mika Westerberg97028ce2017-06-05 16:39:19 +0800342 case ACPI_RESOURCE_TYPE_PIN_CONFIG:
343
344 total_size = (acpi_rs_length)(total_size +
345 (resource->data.
346 pin_config.
347 pin_table_length * 2) +
348 resource->data.pin_config.
349 resource_source.
350 string_length +
351 resource->data.pin_config.
352 vendor_length);
353
354 break;
355
Mika Westerbergfdaa0982017-06-05 16:39:25 +0800356 case ACPI_RESOURCE_TYPE_PIN_GROUP:
357
358 total_size = (acpi_rs_length)(total_size +
359 (resource->data.pin_group.
360 pin_table_length * 2) +
361 resource->data.pin_group.
362 resource_label.
363 string_length +
364 resource->data.pin_group.
365 vendor_length);
366
367 break;
368
Mika Westerbergf8a6c862017-06-05 16:39:31 +0800369 case ACPI_RESOURCE_TYPE_PIN_GROUP_FUNCTION:
370
371 total_size = (acpi_rs_length)(total_size +
372 resource->data.
373 pin_group_function.
374 resource_source.
375 string_length +
376 resource->data.
377 pin_group_function.
378 resource_source_label.
379 string_length +
380 resource->data.
381 pin_group_function.
382 vendor_length);
383
384 break;
385
Mika Westerberg044b7232017-06-05 16:39:37 +0800386 case ACPI_RESOURCE_TYPE_PIN_GROUP_CONFIG:
387
388 total_size = (acpi_rs_length)(total_size +
389 resource->data.
390 pin_group_config.
391 resource_source.
392 string_length +
393 resource->data.
394 pin_group_config.
395 resource_source_label.
396 string_length +
397 resource->data.
398 pin_group_config.
399 vendor_length);
400
401 break;
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 default:
Chao Guan1d1ea1b72013-06-08 00:58:14 +0000404
Robert Moorebda663d2005-09-16 16:51:15 -0400405 break;
406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Robert Moore44f6c012005-04-18 22:49:35 -0400408 /* Update the total */
409
Bob Moore08978312005-10-21 00:00:00 -0400410 aml_size_needed += total_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Robert Moore44f6c012005-04-18 22:49:35 -0400412 /* Point to the next object */
413
Bob Moore96db2552005-11-02 00:00:00 -0500414 resource =
Bob Moorec51a4de2005-11-17 13:07:00 -0500415 ACPI_ADD_PTR(struct acpi_resource, resource,
Bob Moore96db2552005-11-02 00:00:00 -0500416 resource->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Bob Moore96db2552005-11-02 00:00:00 -0500419 /* Did not find an end_tag resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -0400420
Bob Moore96db2552005-11-02 00:00:00 -0500421 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/*******************************************************************************
425 *
426 * FUNCTION: acpi_rs_get_list_length
427 *
Bob Moore50eca3e2005-09-30 19:03:00 -0400428 * PARAMETERS: aml_buffer - Pointer to the resource byte stream
429 * aml_buffer_length - Size of aml_buffer
430 * size_needed - Where the size needed is returned
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 *
432 * RETURN: Status
433 *
Robert Moorebda663d2005-09-16 16:51:15 -0400434 * DESCRIPTION: Takes an external resource byte stream and calculates the size
435 * buffer needed to hold the corresponding internal resource
436 * descriptor linked list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
438 ******************************************************************************/
439
440acpi_status
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800441acpi_rs_get_list_length(u8 *aml_buffer,
442 u32 aml_buffer_length, acpi_size *size_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Bob Moore96db2552005-11-02 00:00:00 -0500444 acpi_status status;
445 u8 *end_aml;
Robert Moorebda663d2005-09-16 16:51:15 -0400446 u8 *buffer;
Bob Mooreea936b72006-02-17 00:00:00 -0500447 u32 buffer_size;
Len Brown4be44fc2005-08-05 00:44:28 -0400448 u16 temp16;
Robert Moorebda663d2005-09-16 16:51:15 -0400449 u16 resource_length;
Robert Moorebda663d2005-09-16 16:51:15 -0400450 u32 extra_struct_bytes;
Bob Moore96db2552005-11-02 00:00:00 -0500451 u8 resource_index;
452 u8 minimum_aml_resource_length;
Lin Minge0fe0a82011-11-16 14:38:13 +0800453 union aml_resource *aml_resource;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Bob Mooreb229cf92006-04-21 17:15:00 -0400455 ACPI_FUNCTION_TRACE(rs_get_list_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Lin Minge0fe0a82011-11-16 14:38:13 +0800457 *size_needed = ACPI_RS_SIZE_MIN; /* Minimum size is one end_tag */
Bob Moore96db2552005-11-02 00:00:00 -0500458 end_aml = aml_buffer + aml_buffer_length;
Robert Moore44f6c012005-04-18 22:49:35 -0400459
Bob Moore96db2552005-11-02 00:00:00 -0500460 /* Walk the list of AML resource descriptors */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Bob Moore96db2552005-11-02 00:00:00 -0500462 while (aml_buffer < end_aml) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400463
Bob Moore96db2552005-11-02 00:00:00 -0500464 /* Validate the Resource Type and Resource Length */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Bob Moore886308e2012-12-19 05:38:07 +0000466 status =
467 acpi_ut_validate_resource(NULL, aml_buffer,
468 &resource_index);
Bob Moore96db2552005-11-02 00:00:00 -0500469 if (ACPI_FAILURE(status)) {
Lin Minge0fe0a82011-11-16 14:38:13 +0800470 /*
471 * Exit on failure. Cannot continue because the descriptor length
472 * may be bogus also.
473 */
Bob Moore96db2552005-11-02 00:00:00 -0500474 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
476
Lin Minge0fe0a82011-11-16 14:38:13 +0800477 aml_resource = (void *)aml_buffer;
478
Bob Moore96db2552005-11-02 00:00:00 -0500479 /* Get the resource length and base (minimum) AML size */
Robert Moore44f6c012005-04-18 22:49:35 -0400480
Bob Moore08978312005-10-21 00:00:00 -0400481 resource_length = acpi_ut_get_resource_length(aml_buffer);
Bob Moore96db2552005-11-02 00:00:00 -0500482 minimum_aml_resource_length =
483 acpi_gbl_resource_aml_sizes[resource_index];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Bob Moore96db2552005-11-02 00:00:00 -0500485 /*
486 * Augment the size for descriptors with optional
487 * and/or variable length fields
488 */
Robert Moorebda663d2005-09-16 16:51:15 -0400489 extra_struct_bytes = 0;
Bob Moore96db2552005-11-02 00:00:00 -0500490 buffer =
491 aml_buffer + acpi_ut_get_resource_header_length(aml_buffer);
Robert Moorebda663d2005-09-16 16:51:15 -0400492
Bob Moore96db2552005-11-02 00:00:00 -0500493 switch (acpi_ut_get_resource_type(aml_buffer)) {
494 case ACPI_RESOURCE_NAME_IRQ:
Robert Moorebda663d2005-09-16 16:51:15 -0400495 /*
Bob Moore96db2552005-11-02 00:00:00 -0500496 * IRQ Resource:
497 * Get the number of bits set in the 16-bit IRQ mask
Robert Moorebda663d2005-09-16 16:51:15 -0400498 */
Bob Moore96db2552005-11-02 00:00:00 -0500499 ACPI_MOVE_16_TO_16(&temp16, buffer);
Bob Moorec51a4de2005-11-17 13:07:00 -0500500 extra_struct_bytes = acpi_rs_count_set_bits(temp16);
Bob Moore96db2552005-11-02 00:00:00 -0500501 break;
Robert Moorebda663d2005-09-16 16:51:15 -0400502
Bob Moore96db2552005-11-02 00:00:00 -0500503 case ACPI_RESOURCE_NAME_DMA:
Robert Moorebda663d2005-09-16 16:51:15 -0400504 /*
Bob Moore96db2552005-11-02 00:00:00 -0500505 * DMA Resource:
506 * Get the number of bits set in the 8-bit DMA mask
Robert Moorebda663d2005-09-16 16:51:15 -0400507 */
Bob Moorec51a4de2005-11-17 13:07:00 -0500508 extra_struct_bytes = acpi_rs_count_set_bits(*buffer);
Bob Moore96db2552005-11-02 00:00:00 -0500509 break;
Robert Moorebda663d2005-09-16 16:51:15 -0400510
Bob Moore96db2552005-11-02 00:00:00 -0500511 case ACPI_RESOURCE_NAME_VENDOR_SMALL:
Bob Mooreea936b72006-02-17 00:00:00 -0500512 case ACPI_RESOURCE_NAME_VENDOR_LARGE:
Bob Moore96db2552005-11-02 00:00:00 -0500513 /*
514 * Vendor Resource:
Bob Mooreea936b72006-02-17 00:00:00 -0500515 * Get the number of vendor data bytes
Bob Moore96db2552005-11-02 00:00:00 -0500516 */
Bob Mooreea936b72006-02-17 00:00:00 -0500517 extra_struct_bytes = resource_length;
Feng Tangbee6dc32012-10-31 02:27:15 +0000518
519 /*
520 * There is already one byte included in the minimum
521 * descriptor size. If there are extra struct bytes,
522 * subtract one from the count.
523 */
524 if (extra_struct_bytes) {
525 extra_struct_bytes--;
526 }
Bob Moore96db2552005-11-02 00:00:00 -0500527 break;
Robert Moorebda663d2005-09-16 16:51:15 -0400528
Bob Moore96db2552005-11-02 00:00:00 -0500529 case ACPI_RESOURCE_NAME_END_TAG:
530 /*
Lin Minge0fe0a82011-11-16 14:38:13 +0800531 * End Tag: This is the normal exit
Bob Moore96db2552005-11-02 00:00:00 -0500532 */
Bob Moore96db2552005-11-02 00:00:00 -0500533 return_ACPI_STATUS(AE_OK);
Robert Moorebda663d2005-09-16 16:51:15 -0400534
Bob Moore96db2552005-11-02 00:00:00 -0500535 case ACPI_RESOURCE_NAME_ADDRESS32:
536 case ACPI_RESOURCE_NAME_ADDRESS16:
Bob Mooreea936b72006-02-17 00:00:00 -0500537 case ACPI_RESOURCE_NAME_ADDRESS64:
Bob Moore96db2552005-11-02 00:00:00 -0500538 /*
Bob Mooreea936b72006-02-17 00:00:00 -0500539 * Address Resource:
540 * Add the size of the optional resource_source
Bob Moore96db2552005-11-02 00:00:00 -0500541 */
542 extra_struct_bytes =
543 acpi_rs_stream_option_length(resource_length,
544 minimum_aml_resource_length);
545 break;
Robert Moorebda663d2005-09-16 16:51:15 -0400546
Bob Moore96db2552005-11-02 00:00:00 -0500547 case ACPI_RESOURCE_NAME_EXTENDED_IRQ:
548 /*
Bob Mooreea936b72006-02-17 00:00:00 -0500549 * Extended IRQ Resource:
550 * Using the interrupt_table_length, add 4 bytes for each additional
551 * interrupt. Note: at least one interrupt is required and is
552 * included in the minimum descriptor size (reason for the -1)
Bob Moore96db2552005-11-02 00:00:00 -0500553 */
Bob Mooreea936b72006-02-17 00:00:00 -0500554 extra_struct_bytes = (buffer[1] - 1) * sizeof(u32);
Robert Moorebda663d2005-09-16 16:51:15 -0400555
Bob Mooreea936b72006-02-17 00:00:00 -0500556 /* Add the size of the optional resource_source */
557
558 extra_struct_bytes +=
Bob Moore96db2552005-11-02 00:00:00 -0500559 acpi_rs_stream_option_length(resource_length -
560 extra_struct_bytes,
561 minimum_aml_resource_length);
562 break;
Robert Moorebda663d2005-09-16 16:51:15 -0400563
Lin Minge0fe0a82011-11-16 14:38:13 +0800564 case ACPI_RESOURCE_NAME_GPIO:
565
566 /* Vendor data is optional */
567
568 if (aml_resource->gpio.vendor_length) {
569 extra_struct_bytes +=
570 aml_resource->gpio.vendor_offset -
571 aml_resource->gpio.pin_table_offset +
572 aml_resource->gpio.vendor_length;
573 } else {
574 extra_struct_bytes +=
575 aml_resource->large_header.resource_length +
576 sizeof(struct aml_resource_large_header) -
577 aml_resource->gpio.pin_table_offset;
578 }
579 break;
580
Mika Westerberg2b726932017-06-05 16:39:14 +0800581 case ACPI_RESOURCE_NAME_PIN_FUNCTION:
582
583 /* Vendor data is optional */
584
585 if (aml_resource->pin_function.vendor_length) {
586 extra_struct_bytes +=
587 aml_resource->pin_function.vendor_offset -
588 aml_resource->pin_function.
589 pin_table_offset +
590 aml_resource->pin_function.vendor_length;
591 } else {
592 extra_struct_bytes +=
593 aml_resource->large_header.resource_length +
594 sizeof(struct aml_resource_large_header) -
595 aml_resource->pin_function.pin_table_offset;
596 }
597 break;
598
Lin Minge0fe0a82011-11-16 14:38:13 +0800599 case ACPI_RESOURCE_NAME_SERIAL_BUS:
600
601 minimum_aml_resource_length =
602 acpi_gbl_resource_aml_serial_bus_sizes
603 [aml_resource->common_serial_bus.type];
604 extra_struct_bytes +=
605 aml_resource->common_serial_bus.resource_length -
606 minimum_aml_resource_length;
607 break;
608
Mika Westerberg97028ce2017-06-05 16:39:19 +0800609 case ACPI_RESOURCE_NAME_PIN_CONFIG:
610
611 /* Vendor data is optional */
612
613 if (aml_resource->pin_config.vendor_length) {
614 extra_struct_bytes +=
615 aml_resource->pin_config.vendor_offset -
616 aml_resource->pin_config.pin_table_offset +
617 aml_resource->pin_config.vendor_length;
618 } else {
619 extra_struct_bytes +=
620 aml_resource->large_header.resource_length +
621 sizeof(struct aml_resource_large_header) -
622 aml_resource->pin_config.pin_table_offset;
623 }
624 break;
625
Mika Westerbergfdaa0982017-06-05 16:39:25 +0800626 case ACPI_RESOURCE_NAME_PIN_GROUP:
627
628 extra_struct_bytes +=
629 aml_resource->pin_group.vendor_offset -
630 aml_resource->pin_group.pin_table_offset +
631 aml_resource->pin_group.vendor_length;
632
633 break;
634
Mika Westerbergf8a6c862017-06-05 16:39:31 +0800635 case ACPI_RESOURCE_NAME_PIN_GROUP_FUNCTION:
636
637 extra_struct_bytes +=
638 aml_resource->pin_group_function.vendor_offset -
639 aml_resource->pin_group_function.res_source_offset +
640 aml_resource->pin_group_function.vendor_length;
641
642 break;
643
Mika Westerberg044b7232017-06-05 16:39:37 +0800644 case ACPI_RESOURCE_NAME_PIN_GROUP_CONFIG:
645
646 extra_struct_bytes +=
647 aml_resource->pin_group_config.vendor_offset -
648 aml_resource->pin_group_config.res_source_offset +
649 aml_resource->pin_group_config.vendor_length;
650
651 break;
652
Bob Moore96db2552005-11-02 00:00:00 -0500653 default:
Chao Guan1d1ea1b72013-06-08 00:58:14 +0000654
Bob Moore96db2552005-11-02 00:00:00 -0500655 break;
Robert Moorebda663d2005-09-16 16:51:15 -0400656 }
657
Bob Mooreea936b72006-02-17 00:00:00 -0500658 /*
659 * Update the required buffer size for the internal descriptor structs
660 *
661 * Important: Round the size up for the appropriate alignment. This
662 * is a requirement on IA64.
663 */
Lin Minge0fe0a82011-11-16 14:38:13 +0800664 if (acpi_ut_get_resource_type(aml_buffer) ==
665 ACPI_RESOURCE_NAME_SERIAL_BUS) {
666 buffer_size =
667 acpi_gbl_resource_struct_serial_bus_sizes
668 [aml_resource->common_serial_bus.type] +
669 extra_struct_bytes;
670 } else {
671 buffer_size =
672 acpi_gbl_resource_struct_sizes[resource_index] +
673 extra_struct_bytes;
674 }
Robert Moorebda663d2005-09-16 16:51:15 -0400675
Bob Moore1fad8732015-12-29 13:54:36 +0800676 buffer_size = (u32)ACPI_ROUND_UP_TO_NATIVE_WORD(buffer_size);
Bob Mooreea936b72006-02-17 00:00:00 -0500677 *size_needed += buffer_size;
678
679 ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES,
Bob Mooreb229cf92006-04-21 17:15:00 -0400680 "Type %.2X, AmlLength %.2X InternalLength %.2X\n",
Bob Mooreea936b72006-02-17 00:00:00 -0500681 acpi_ut_get_resource_type(aml_buffer),
682 acpi_ut_get_descriptor_length(aml_buffer),
683 buffer_size));
Robert Moorebda663d2005-09-16 16:51:15 -0400684
685 /*
Bob Mooreea936b72006-02-17 00:00:00 -0500686 * Point to the next resource within the AML stream using the length
687 * contained in the resource descriptor header
Robert Moorebda663d2005-09-16 16:51:15 -0400688 */
Bob Moore96db2552005-11-02 00:00:00 -0500689 aml_buffer += acpi_ut_get_descriptor_length(aml_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
691
Bob Moore96db2552005-11-02 00:00:00 -0500692 /* Did not find an end_tag resource descriptor */
Robert Moore44f6c012005-04-18 22:49:35 -0400693
Bob Moore96db2552005-11-02 00:00:00 -0500694 return_ACPI_STATUS(AE_AML_NO_RESOURCE_END_TAG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/*******************************************************************************
698 *
699 * FUNCTION: acpi_rs_get_pci_routing_table_length
700 *
701 * PARAMETERS: package_object - Pointer to the package object
702 * buffer_size_needed - u32 pointer of the size buffer
703 * needed to properly return the
704 * parsed data
705 *
706 * RETURN: Status
707 *
708 * DESCRIPTION: Given a package representing a PCI routing table, this
709 * calculates the size of the corresponding linked list of
710 * descriptions.
711 *
712 ******************************************************************************/
713
714acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400715acpi_rs_get_pci_routing_table_length(union acpi_operand_object *package_object,
Lv Zhengf5c1e1c2016-05-05 12:57:53 +0800716 acpi_size *buffer_size_needed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Len Brown4be44fc2005-08-05 00:44:28 -0400718 u32 number_of_elements;
719 acpi_size temp_size_needed = 0;
720 union acpi_operand_object **top_object_list;
721 u32 index;
722 union acpi_operand_object *package_element;
723 union acpi_operand_object **sub_object_list;
724 u8 name_found;
725 u32 table_index;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Bob Mooreb229cf92006-04-21 17:15:00 -0400727 ACPI_FUNCTION_TRACE(rs_get_pci_routing_table_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
729 number_of_elements = package_object->package.count;
730
731 /*
732 * Calculate the size of the return buffer.
733 * The base size is the number of elements * the sizes of the
Bob Moore73a30902012-10-31 02:26:55 +0000734 * structures. Additional space for the strings is added below.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 * The minus one is to subtract the size of the u8 Source[1]
736 * member because it is added below.
737 *
738 * But each PRT_ENTRY structure has a pointer to a string and
739 * the size of that string must be found.
740 */
741 top_object_list = package_object->package.elements;
742
743 for (index = 0; index < number_of_elements; index++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400744
Bob Moore0a16d122014-02-26 10:31:18 +0800745 /* Dereference the subpackage */
Robert Moore44f6c012005-04-18 22:49:35 -0400746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 package_element = *top_object_list;
748
Robert Moore53951bd2009-05-02 11:48:37 -0700749 /* We must have a valid Package object */
750
751 if (!package_element ||
752 (package_element->common.type != ACPI_TYPE_PACKAGE)) {
Bob Moore474caff2009-05-21 10:10:16 +0800753 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
Robert Moore53951bd2009-05-02 11:48:37 -0700754 }
755
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 /*
757 * The sub_object_list will now point to an array of the
758 * four IRQ elements: Address, Pin, Source and source_index
759 */
760 sub_object_list = package_element->package.elements;
761
Robert Moore44f6c012005-04-18 22:49:35 -0400762 /* Scan the irq_table_elements for the Source Name String */
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 name_found = FALSE;
765
Lv Zhengaa6329c2013-06-08 09:01:01 +0800766 for (table_index = 0;
767 table_index < package_element->package.count
768 && !name_found; table_index++) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500769 if (*sub_object_list && /* Null object allowed */
770 ((ACPI_TYPE_STRING ==
Bob Moore3371c192009-02-18 14:44:03 +0800771 (*sub_object_list)->common.type) ||
Bob Mooreb8e4d892006-01-27 16:43:00 -0500772 ((ACPI_TYPE_LOCAL_REFERENCE ==
Bob Moore3371c192009-02-18 14:44:03 +0800773 (*sub_object_list)->common.type) &&
Bob Moore1044f1f2008-09-27 11:08:41 +0800774 ((*sub_object_list)->reference.class ==
775 ACPI_REFCLASS_NAME)))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 name_found = TRUE;
Len Brown4be44fc2005-08-05 00:44:28 -0400777 } else {
Robert Moore44f6c012005-04-18 22:49:35 -0400778 /* Look at the next element */
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 sub_object_list++;
781 }
782 }
783
Len Brown4be44fc2005-08-05 00:44:28 -0400784 temp_size_needed += (sizeof(struct acpi_pci_routing_table) - 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Robert Moore44f6c012005-04-18 22:49:35 -0400786 /* Was a String type found? */
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (name_found) {
Bob Moore3371c192009-02-18 14:44:03 +0800789 if ((*sub_object_list)->common.type == ACPI_TYPE_STRING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 /*
791 * The length String.Length field does not include the
792 * terminating NULL, add 1
793 */
Robert Moore44f6c012005-04-18 22:49:35 -0400794 temp_size_needed += ((acpi_size)
Len Brown4be44fc2005-08-05 00:44:28 -0400795 (*sub_object_list)->string.
796 length + 1);
797 } else {
Lv Zheng1f86e8c2012-10-31 02:25:45 +0000798 temp_size_needed += acpi_ns_get_pathname_length((*sub_object_list)->reference.node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
Len Brown4be44fc2005-08-05 00:44:28 -0400800 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 /*
802 * If no name was found, then this is a NULL, which is
803 * translated as a u32 zero.
804 */
Len Brown4be44fc2005-08-05 00:44:28 -0400805 temp_size_needed += sizeof(u32);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807
808 /* Round up the size since each element must be aligned */
809
Bob Moore958dd242006-05-12 17:12:00 -0400810 temp_size_needed = ACPI_ROUND_UP_TO_64BIT(temp_size_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Robert Moore44f6c012005-04-18 22:49:35 -0400812 /* Point to the next union acpi_operand_object */
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 top_object_list++;
815 }
816
817 /*
Bob Mooreea936b72006-02-17 00:00:00 -0500818 * Add an extra element to the end of the list, essentially a
Robert Moore44f6c012005-04-18 22:49:35 -0400819 * NULL terminator
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Len Brown4be44fc2005-08-05 00:44:28 -0400821 *buffer_size_needed =
822 temp_size_needed + sizeof(struct acpi_pci_routing_table);
823 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}