1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * ACPI device specific properties support. 4 * 5 * Copyright (C) 2014 - 2023, Intel Corporation 6 * All rights reserved. 7 * 8 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com> 9 * Darren Hart <dvhart@linux.intel.com> 10 * Rafael J. Wysocki <rafael.j.wysocki@intel.com> 11 * Sakari Ailus <sakari.ailus@linux.intel.com> 12 */ 13 14 #define pr_fmt(fmt) "ACPI: " fmt 15 16 #include <linux/acpi.h> 17 #include <linux/device.h> 18 #include <linux/export.h> 19 20 #include "internal.h" 21 22 static int acpi_data_get_property_array(const struct acpi_device_data *data, 23 const char *name, 24 acpi_object_type type, 25 const union acpi_object **obj); 26 27 /* 28 * The GUIDs here are made equivalent to each other in order to avoid extra 29 * complexity in the properties handling code, with the caveat that the 30 * kernel will accept certain combinations of GUID and properties that are 31 * not defined without a warning. For instance if any of the properties 32 * from different GUID appear in a property list of another, it will be 33 * accepted by the kernel. Firmware validation tools should catch these. 34 * 35 * References: 36 * 37 * [1] UEFI DSD Guide. 38 * https://github.com/UEFI/DSD-Guide/blob/main/src/dsd-guide.adoc 39 */ 40 static const guid_t prp_guids[] = { 41 /* ACPI _DSD device properties GUID [1]: daffd814-6eba-4d8c-8a91-bc9bbf4aa301 */ 42 GUID_INIT(0xdaffd814, 0x6eba, 0x4d8c, 43 0x8a, 0x91, 0xbc, 0x9b, 0xbf, 0x4a, 0xa3, 0x01), 44 /* Hotplug in D3 GUID: 6211e2c0-58a3-4af3-90e1-927a4e0c55a4 */ 45 GUID_INIT(0x6211e2c0, 0x58a3, 0x4af3, 46 0x90, 0xe1, 0x92, 0x7a, 0x4e, 0x0c, 0x55, 0xa4), 47 /* External facing port GUID: efcc06cc-73ac-4bc3-bff0-76143807c389 */ 48 GUID_INIT(0xefcc06cc, 0x73ac, 0x4bc3, 49 0xbf, 0xf0, 0x76, 0x14, 0x38, 0x07, 0xc3, 0x89), 50 /* Thunderbolt GUID for IMR_VALID: c44d002f-69f9-4e7d-a904-a7baabdf43f7 */ 51 GUID_INIT(0xc44d002f, 0x69f9, 0x4e7d, 52 0xa9, 0x04, 0xa7, 0xba, 0xab, 0xdf, 0x43, 0xf7), 53 /* Thunderbolt GUID for WAKE_SUPPORTED: 6c501103-c189-4296-ba72-9bf5a26ebe5d */ 54 GUID_INIT(0x6c501103, 0xc189, 0x4296, 55 0xba, 0x72, 0x9b, 0xf5, 0xa2, 0x6e, 0xbe, 0x5d), 56 /* Storage device needs D3 GUID: 5025030f-842f-4ab4-a561-99a5189762d0 */ 57 GUID_INIT(0x5025030f, 0x842f, 0x4ab4, 58 0xa5, 0x61, 0x99, 0xa5, 0x18, 0x97, 0x62, 0xd0), 59 }; 60 61 /* ACPI _DSD data subnodes GUID [1]: dbb8e3e6-5886-4ba6-8795-1319f52a966b */ 62 static const guid_t ads_guid = 63 GUID_INIT(0xdbb8e3e6, 0x5886, 0x4ba6, 64 0x87, 0x95, 0x13, 0x19, 0xf5, 0x2a, 0x96, 0x6b); 65 66 /* ACPI _DSD data buffer GUID [1]: edb12dd0-363d-4085-a3d2-49522ca160c4 */ 67 static const guid_t buffer_prop_guid = 68 GUID_INIT(0xedb12dd0, 0x363d, 0x4085, 69 0xa3, 0xd2, 0x49, 0x52, 0x2c, 0xa1, 0x60, 0xc4); 70 71 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope, 72 union acpi_object *desc, 73 struct acpi_device_data *data, 74 struct fwnode_handle *parent); 75 static bool acpi_extract_properties(acpi_handle handle, 76 union acpi_object *desc, 77 struct acpi_device_data *data); 78 79 static bool acpi_nondev_subnode_extract(union acpi_object *desc, 80 acpi_handle handle, 81 const union acpi_object *link, 82 struct list_head *list, 83 struct fwnode_handle *parent) 84 { 85 struct acpi_data_node *dn; 86 acpi_handle scope = NULL; 87 bool result; 88 89 if (acpi_graph_ignore_port(handle)) 90 return false; 91 92 dn = kzalloc(sizeof(*dn), GFP_KERNEL); 93 if (!dn) 94 return false; 95 96 dn->name = link->package.elements[0].string.pointer; 97 fwnode_init(&dn->fwnode, &acpi_data_fwnode_ops); 98 dn->parent = parent; 99 INIT_LIST_HEAD(&dn->data.properties); 100 INIT_LIST_HEAD(&dn->data.subnodes); 101 102 /* 103 * The scope for the completion of relative pathname segments and 104 * subnode object lookup is the one of the namespace node (device) 105 * containing the object that has returned the package. That is, it's 106 * the scope of that object's parent device. 107 */ 108 if (handle) 109 acpi_get_parent(handle, &scope); 110 111 /* 112 * Extract properties from the _DSD-equivalent package pointed to by 113 * desc and use scope (if not NULL) for the completion of relative 114 * pathname segments. 115 * 116 * The extracted properties will be held in the new data node dn. 117 */ 118 result = acpi_extract_properties(scope, desc, &dn->data); 119 /* 120 * Look for subnodes in the _DSD-equivalent package pointed to by desc 121 * and create child nodes of dn if there are any. 122 */ 123 if (acpi_enumerate_nondev_subnodes(scope, desc, &dn->data, &dn->fwnode)) 124 result = true; 125 126 if (!result) { 127 kfree(dn); 128 acpi_handle_debug(handle, "Invalid properties/subnodes data, skipping\n"); 129 return false; 130 } 131 132 /* 133 * This will be NULL if the desc package is embedded in an outer 134 * _DSD-equivalent package and its scope cannot be determined. 135 */ 136 dn->handle = handle; 137 dn->data.pointer = desc; 138 list_add_tail(&dn->sibling, list); 139 140 return true; 141 } 142 143 static bool acpi_nondev_subnode_ok(acpi_handle scope, 144 const union acpi_object *link, 145 struct list_head *list, 146 struct fwnode_handle *parent) 147 { 148 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 149 acpi_handle handle; 150 acpi_status status; 151 152 /* 153 * If the scope is unknown, the _DSD-equivalent package being parsed 154 * was embedded in an outer _DSD-equivalent package as a result of 155 * direct evaluation of an object pointed to by a reference. In that 156 * case, using a pathname as the target object pointer is invalid. 157 */ 158 if (!scope) 159 return false; 160 161 status = acpi_get_handle(scope, link->package.elements[1].string.pointer, 162 &handle); 163 if (ACPI_FAILURE(status)) 164 return false; 165 166 status = acpi_evaluate_object_typed(handle, NULL, NULL, &buf, 167 ACPI_TYPE_PACKAGE); 168 if (ACPI_FAILURE(status)) 169 return false; 170 171 if (acpi_nondev_subnode_extract(buf.pointer, handle, link, list, 172 parent)) 173 return true; 174 175 ACPI_FREE(buf.pointer); 176 return false; 177 } 178 179 static bool acpi_add_nondev_subnodes(acpi_handle scope, 180 union acpi_object *links, 181 struct list_head *list, 182 struct fwnode_handle *parent) 183 { 184 bool ret = false; 185 int i; 186 187 /* 188 * Every element in the links package is expected to represent a link 189 * to a non-device node in a tree containing device-specific data. 190 */ 191 for (i = 0; i < links->package.count; i++) { 192 union acpi_object *link, *desc; 193 bool result; 194 195 link = &links->package.elements[i]; 196 /* Only two elements allowed. */ 197 if (link->package.count != 2) 198 continue; 199 200 /* The first one (the key) must be a string. */ 201 if (link->package.elements[0].type != ACPI_TYPE_STRING) 202 continue; 203 204 /* The second one (the target) may be a string or a package. */ 205 switch (link->package.elements[1].type) { 206 case ACPI_TYPE_STRING: 207 /* 208 * The string is expected to be a full pathname or a 209 * pathname segment relative to the given scope. That 210 * pathname is expected to point to an object returning 211 * a package that contains _DSD-equivalent information. 212 */ 213 result = acpi_nondev_subnode_ok(scope, link, list, 214 parent); 215 break; 216 case ACPI_TYPE_PACKAGE: 217 /* 218 * This happens when a reference is used in AML to 219 * point to the target. Since the target is expected 220 * to be a named object, a reference to it will cause it 221 * to be avaluated in place and its return package will 222 * be embedded in the links package at the location of 223 * the reference. 224 * 225 * The target package is expected to contain _DSD- 226 * equivalent information, but the scope in which it 227 * is located in the original AML is unknown. Thus 228 * it cannot contain pathname segments represented as 229 * strings because there is no way to build full 230 * pathnames out of them. 231 */ 232 acpi_handle_debug(scope, "subnode %s: Unknown scope\n", 233 link->package.elements[0].string.pointer); 234 desc = &link->package.elements[1]; 235 result = acpi_nondev_subnode_extract(desc, NULL, link, 236 list, parent); 237 break; 238 case ACPI_TYPE_LOCAL_REFERENCE: 239 /* 240 * It is not expected to see any local references in 241 * the links package because referencing a named object 242 * should cause it to be evaluated in place. 243 */ 244 acpi_handle_info(scope, "subnode %s: Unexpected reference\n", 245 link->package.elements[0].string.pointer); 246 fallthrough; 247 default: 248 result = false; 249 break; 250 } 251 ret = ret || result; 252 } 253 254 return ret; 255 } 256 257 static bool acpi_enumerate_nondev_subnodes(acpi_handle scope, 258 union acpi_object *desc, 259 struct acpi_device_data *data, 260 struct fwnode_handle *parent) 261 { 262 int i; 263 264 /* Look for the ACPI data subnodes GUID. */ 265 for (i = 0; i < desc->package.count; i += 2) { 266 const union acpi_object *guid; 267 union acpi_object *links; 268 269 guid = &desc->package.elements[i]; 270 links = &desc->package.elements[i + 1]; 271 272 /* 273 * The first element must be a GUID and the second one must be 274 * a package. 275 */ 276 if (guid->type != ACPI_TYPE_BUFFER || 277 guid->buffer.length != 16 || 278 links->type != ACPI_TYPE_PACKAGE) 279 break; 280 281 if (!guid_equal((guid_t *)guid->buffer.pointer, &ads_guid)) 282 continue; 283 284 return acpi_add_nondev_subnodes(scope, links, &data->subnodes, 285 parent); 286 } 287 288 return false; 289 } 290 291 static bool acpi_property_value_ok(const union acpi_object *value) 292 { 293 int j; 294 295 /* 296 * The value must be an integer, a string, a reference, or a package 297 * whose every element must be an integer, a string, or a reference. 298 */ 299 switch (value->type) { 300 case ACPI_TYPE_INTEGER: 301 case ACPI_TYPE_STRING: 302 case ACPI_TYPE_LOCAL_REFERENCE: 303 return true; 304 305 case ACPI_TYPE_PACKAGE: 306 for (j = 0; j < value->package.count; j++) 307 switch (value->package.elements[j].type) { 308 case ACPI_TYPE_INTEGER: 309 case ACPI_TYPE_STRING: 310 case ACPI_TYPE_LOCAL_REFERENCE: 311 continue; 312 313 default: 314 return false; 315 } 316 317 return true; 318 } 319 return false; 320 } 321 322 static bool acpi_properties_format_valid(const union acpi_object *properties) 323 { 324 int i; 325 326 for (i = 0; i < properties->package.count; i++) { 327 const union acpi_object *property; 328 329 property = &properties->package.elements[i]; 330 /* 331 * Only two elements allowed, the first one must be a string and 332 * the second one has to satisfy certain conditions. 333 */ 334 if (property->package.count != 2 335 || property->package.elements[0].type != ACPI_TYPE_STRING 336 || !acpi_property_value_ok(&property->package.elements[1])) 337 return false; 338 } 339 return true; 340 } 341 342 static void acpi_init_of_compatible(struct acpi_device *adev) 343 { 344 const union acpi_object *of_compatible; 345 int ret; 346 347 ret = acpi_data_get_property_array(&adev->data, "compatible", 348 ACPI_TYPE_STRING, &of_compatible); 349 if (ret) { 350 ret = acpi_dev_get_property(adev, "compatible", 351 ACPI_TYPE_STRING, &of_compatible); 352 if (ret) { 353 struct acpi_device *parent; 354 355 parent = acpi_dev_parent(adev); 356 if (parent && parent->flags.of_compatible_ok) 357 goto out; 358 359 return; 360 } 361 } 362 adev->data.of_compatible = of_compatible; 363 364 out: 365 adev->flags.of_compatible_ok = 1; 366 } 367 368 static bool acpi_is_property_guid(const guid_t *guid) 369 { 370 int i; 371 372 for (i = 0; i < ARRAY_SIZE(prp_guids); i++) { 373 if (guid_equal(guid, &prp_guids[i])) 374 return true; 375 } 376 377 return false; 378 } 379 380 struct acpi_device_properties * 381 acpi_data_add_props(struct acpi_device_data *data, const guid_t *guid, 382 union acpi_object *properties) 383 { 384 struct acpi_device_properties *props; 385 386 props = kzalloc(sizeof(*props), GFP_KERNEL); 387 if (props) { 388 INIT_LIST_HEAD(&props->list); 389 props->guid = guid; 390 props->properties = properties; 391 list_add_tail(&props->list, &data->properties); 392 } 393 394 return props; 395 } 396 397 static void acpi_nondev_subnode_tag(acpi_handle handle, void *context) 398 { 399 } 400 401 static void acpi_untie_nondev_subnodes(struct acpi_device_data *data) 402 { 403 struct acpi_data_node *dn; 404 405 list_for_each_entry(dn, &data->subnodes, sibling) { 406 if (!dn->handle) 407 continue; 408 409 acpi_detach_data(dn->handle, acpi_nondev_subnode_tag); 410 411 acpi_untie_nondev_subnodes(&dn->data); 412 } 413 } 414 415 static bool acpi_tie_nondev_subnodes(struct acpi_device_data *data) 416 { 417 struct acpi_data_node *dn; 418 419 list_for_each_entry(dn, &data->subnodes, sibling) { 420 acpi_status status; 421 bool ret; 422 423 if (!dn->handle) 424 continue; 425 426 status = acpi_attach_data(dn->handle, acpi_nondev_subnode_tag, dn); 427 if (ACPI_FAILURE(status) && status != AE_ALREADY_EXISTS) { 428 acpi_handle_err(dn->handle, "Can't tag data node\n"); 429 return false; 430 } 431 432 ret = acpi_tie_nondev_subnodes(&dn->data); 433 if (!ret) 434 return ret; 435 } 436 437 return true; 438 } 439 440 static void acpi_data_add_buffer_props(acpi_handle handle, 441 struct acpi_device_data *data, 442 union acpi_object *properties) 443 { 444 struct acpi_device_properties *props; 445 union acpi_object *package; 446 size_t alloc_size; 447 unsigned int i; 448 u32 *count; 449 450 if (check_mul_overflow((size_t)properties->package.count, 451 sizeof(*package) + sizeof(void *), 452 &alloc_size) || 453 check_add_overflow(sizeof(*props) + sizeof(*package), alloc_size, 454 &alloc_size)) { 455 acpi_handle_warn(handle, 456 "can't allocate memory for %u buffer props", 457 properties->package.count); 458 return; 459 } 460 461 props = kvzalloc(alloc_size, GFP_KERNEL); 462 if (!props) 463 return; 464 465 props->guid = &buffer_prop_guid; 466 props->bufs = (void *)(props + 1); 467 props->properties = (void *)(props->bufs + properties->package.count); 468 469 /* Outer package */ 470 package = props->properties; 471 package->type = ACPI_TYPE_PACKAGE; 472 package->package.elements = package + 1; 473 count = &package->package.count; 474 *count = 0; 475 476 /* Inner packages */ 477 package++; 478 479 for (i = 0; i < properties->package.count; i++) { 480 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 481 union acpi_object *property = &properties->package.elements[i]; 482 union acpi_object *prop, *obj, *buf_obj; 483 acpi_status status; 484 485 if (property->type != ACPI_TYPE_PACKAGE || 486 property->package.count != 2) { 487 acpi_handle_warn(handle, 488 "buffer property %u has %u entries\n", 489 i, property->package.count); 490 continue; 491 } 492 493 prop = &property->package.elements[0]; 494 obj = &property->package.elements[1]; 495 496 if (prop->type != ACPI_TYPE_STRING || 497 obj->type != ACPI_TYPE_STRING) { 498 acpi_handle_warn(handle, 499 "wrong object types %u and %u\n", 500 prop->type, obj->type); 501 continue; 502 } 503 504 status = acpi_evaluate_object_typed(handle, obj->string.pointer, 505 NULL, &buf, 506 ACPI_TYPE_BUFFER); 507 if (ACPI_FAILURE(status)) { 508 acpi_handle_warn(handle, 509 "can't evaluate \"%*pE\" as buffer\n", 510 obj->string.length, 511 obj->string.pointer); 512 continue; 513 } 514 515 package->type = ACPI_TYPE_PACKAGE; 516 package->package.elements = prop; 517 package->package.count = 2; 518 519 buf_obj = buf.pointer; 520 521 /* Replace the string object with a buffer object */ 522 obj->type = ACPI_TYPE_BUFFER; 523 obj->buffer.length = buf_obj->buffer.length; 524 obj->buffer.pointer = buf_obj->buffer.pointer; 525 526 props->bufs[i] = buf.pointer; 527 package++; 528 (*count)++; 529 } 530 531 if (*count) 532 list_add(&props->list, &data->properties); 533 else 534 kvfree(props); 535 } 536 537 static bool acpi_extract_properties(acpi_handle scope, union acpi_object *desc, 538 struct acpi_device_data *data) 539 { 540 int i; 541 542 if (desc->package.count % 2) 543 return false; 544 545 /* Look for the device properties GUID. */ 546 for (i = 0; i < desc->package.count; i += 2) { 547 const union acpi_object *guid; 548 union acpi_object *properties; 549 550 guid = &desc->package.elements[i]; 551 properties = &desc->package.elements[i + 1]; 552 553 /* 554 * The first element must be a GUID and the second one must be 555 * a package. 556 */ 557 if (guid->type != ACPI_TYPE_BUFFER || 558 guid->buffer.length != 16 || 559 properties->type != ACPI_TYPE_PACKAGE) 560 break; 561 562 if (guid_equal((guid_t *)guid->buffer.pointer, 563 &buffer_prop_guid)) { 564 acpi_data_add_buffer_props(scope, data, properties); 565 continue; 566 } 567 568 if (!acpi_is_property_guid((guid_t *)guid->buffer.pointer)) 569 continue; 570 571 /* 572 * We found the matching GUID. Now validate the format of the 573 * package immediately following it. 574 */ 575 if (!acpi_properties_format_valid(properties)) 576 continue; 577 578 acpi_data_add_props(data, (const guid_t *)guid->buffer.pointer, 579 properties); 580 } 581 582 return !list_empty(&data->properties); 583 } 584 585 void acpi_init_properties(struct acpi_device *adev) 586 { 587 struct acpi_buffer buf = { ACPI_ALLOCATE_BUFFER }; 588 struct acpi_hardware_id *hwid; 589 acpi_status status; 590 bool acpi_of = false; 591 592 INIT_LIST_HEAD(&adev->data.properties); 593 INIT_LIST_HEAD(&adev->data.subnodes); 594 595 if (!adev->handle) 596 return; 597 598 /* 599 * Check if ACPI_DT_NAMESPACE_HID is present and inthat case we fill in 600 * Device Tree compatible properties for this device. 601 */ 602 list_for_each_entry(hwid, &adev->pnp.ids, list) { 603 if (!strcmp(hwid->id, ACPI_DT_NAMESPACE_HID)) { 604 acpi_of = true; 605 break; 606 } 607 } 608 609 status = acpi_evaluate_object_typed(adev->handle, "_DSD", NULL, &buf, 610 ACPI_TYPE_PACKAGE); 611 if (ACPI_FAILURE(status)) 612 goto out; 613 614 if (acpi_extract_properties(adev->handle, buf.pointer, &adev->data)) { 615 adev->data.pointer = buf.pointer; 616 if (acpi_of) 617 acpi_init_of_compatible(adev); 618 } 619 if (acpi_enumerate_nondev_subnodes(adev->handle, buf.pointer, 620 &adev->data, acpi_fwnode_handle(adev))) 621 adev->data.pointer = buf.pointer; 622 623 if (!adev->data.pointer) { 624 acpi_handle_debug(adev->handle, "Invalid _DSD data, skipping\n"); 625 ACPI_FREE(buf.pointer); 626 } else { 627 if (!acpi_tie_nondev_subnodes(&adev->data)) 628 acpi_untie_nondev_subnodes(&adev->data); 629 } 630 631 out: 632 if (acpi_of && !adev->flags.of_compatible_ok) 633 acpi_handle_info(adev->handle, 634 ACPI_DT_NAMESPACE_HID " requires 'compatible' property\n"); 635 636 if (!adev->data.pointer) 637 acpi_extract_apple_properties(adev); 638 } 639 640 static void acpi_free_device_properties(struct list_head *list) 641 { 642 struct acpi_device_properties *props, *tmp; 643 644 list_for_each_entry_safe(props, tmp, list, list) { 645 u32 i; 646 647 list_del(&props->list); 648 /* Buffer data properties were separately allocated */ 649 if (props->bufs) 650 for (i = 0; i < props->properties->package.count; i++) 651 ACPI_FREE(props->bufs[i]); 652 kvfree(props); 653 } 654 } 655 656 static void acpi_destroy_nondev_subnodes(struct list_head *list) 657 { 658 struct acpi_data_node *dn, *next; 659 660 if (list_empty(list)) 661 return; 662 663 list_for_each_entry_safe_reverse(dn, next, list, sibling) { 664 acpi_destroy_nondev_subnodes(&dn->data.subnodes); 665 wait_for_completion(&dn->kobj_done); 666 list_del(&dn->sibling); 667 ACPI_FREE((void *)dn->data.pointer); 668 acpi_free_device_properties(&dn->data.properties); 669 kfree(dn); 670 } 671 } 672 673 void acpi_free_properties(struct acpi_device *adev) 674 { 675 acpi_untie_nondev_subnodes(&adev->data); 676 acpi_destroy_nondev_subnodes(&adev->data.subnodes); 677 ACPI_FREE((void *)adev->data.pointer); 678 adev->data.of_compatible = NULL; 679 adev->data.pointer = NULL; 680 acpi_free_device_properties(&adev->data.properties); 681 } 682 683 /** 684 * acpi_data_get_property - return an ACPI property with given name 685 * @data: ACPI device deta object to get the property from 686 * @name: Name of the property 687 * @type: Expected property type 688 * @obj: Location to store the property value (if not %NULL) 689 * 690 * Look up a property with @name and store a pointer to the resulting ACPI 691 * object at the location pointed to by @obj if found. 692 * 693 * Callers must not attempt to free the returned objects. These objects will be 694 * freed by the ACPI core automatically during the removal of @data. 695 * 696 * Return: %0 if property with @name has been found (success), 697 * %-EINVAL if the arguments are invalid, 698 * %-EINVAL if the property doesn't exist, 699 * %-EPROTO if the property value type doesn't match @type. 700 */ 701 static int acpi_data_get_property(const struct acpi_device_data *data, 702 const char *name, acpi_object_type type, 703 const union acpi_object **obj) 704 { 705 const struct acpi_device_properties *props; 706 707 if (!data || !name) 708 return -EINVAL; 709 710 if (!data->pointer || list_empty(&data->properties)) 711 return -EINVAL; 712 713 list_for_each_entry(props, &data->properties, list) { 714 const union acpi_object *properties; 715 unsigned int i; 716 717 properties = props->properties; 718 for (i = 0; i < properties->package.count; i++) { 719 const union acpi_object *propname, *propvalue; 720 const union acpi_object *property; 721 722 property = &properties->package.elements[i]; 723 724 propname = &property->package.elements[0]; 725 propvalue = &property->package.elements[1]; 726 727 if (!strcmp(name, propname->string.pointer)) { 728 if (type != ACPI_TYPE_ANY && 729 propvalue->type != type) 730 return -EPROTO; 731 if (obj) 732 *obj = propvalue; 733 734 return 0; 735 } 736 } 737 } 738 return -EINVAL; 739 } 740 741 /** 742 * acpi_dev_get_property - return an ACPI property with given name. 743 * @adev: ACPI device to get the property from. 744 * @name: Name of the property. 745 * @type: Expected property type. 746 * @obj: Location to store the property value (if not %NULL). 747 */ 748 int acpi_dev_get_property(const struct acpi_device *adev, const char *name, 749 acpi_object_type type, const union acpi_object **obj) 750 { 751 return adev ? acpi_data_get_property(&adev->data, name, type, obj) : -EINVAL; 752 } 753 EXPORT_SYMBOL_GPL(acpi_dev_get_property); 754 755 static const struct acpi_device_data * 756 acpi_device_data_of_node(const struct fwnode_handle *fwnode) 757 { 758 if (is_acpi_device_node(fwnode)) { 759 const struct acpi_device *adev = to_acpi_device_node(fwnode); 760 return &adev->data; 761 } 762 if (is_acpi_data_node(fwnode)) { 763 const struct acpi_data_node *dn = to_acpi_data_node(fwnode); 764 return &dn->data; 765 } 766 return NULL; 767 } 768 769 /** 770 * acpi_node_prop_get - return an ACPI property with given name. 771 * @fwnode: Firmware node to get the property from. 772 * @propname: Name of the property. 773 * @valptr: Location to store a pointer to the property value (if not %NULL). 774 */ 775 int acpi_node_prop_get(const struct fwnode_handle *fwnode, 776 const char *propname, void **valptr) 777 { 778 return acpi_data_get_property(acpi_device_data_of_node(fwnode), 779 propname, ACPI_TYPE_ANY, 780 (const union acpi_object **)valptr); 781 } 782 783 /** 784 * acpi_data_get_property_array - return an ACPI array property with given name 785 * @data: ACPI data object to get the property from 786 * @name: Name of the property 787 * @type: Expected type of array elements 788 * @obj: Location to store a pointer to the property value (if not NULL) 789 * 790 * Look up an array property with @name and store a pointer to the resulting 791 * ACPI object at the location pointed to by @obj if found. 792 * 793 * Callers must not attempt to free the returned objects. Those objects will be 794 * freed by the ACPI core automatically during the removal of @data. 795 * 796 * Return: %0 if array property (package) with @name has been found (success), 797 * %-EINVAL if the arguments are invalid, 798 * %-EINVAL if the property doesn't exist, 799 * %-EPROTO if the property is not a package or the type of its elements 800 * doesn't match @type. 801 */ 802 static int acpi_data_get_property_array(const struct acpi_device_data *data, 803 const char *name, 804 acpi_object_type type, 805 const union acpi_object **obj) 806 { 807 const union acpi_object *prop; 808 int ret, i; 809 810 ret = acpi_data_get_property(data, name, ACPI_TYPE_PACKAGE, &prop); 811 if (ret) 812 return ret; 813 814 if (type != ACPI_TYPE_ANY) { 815 /* Check that all elements are of correct type. */ 816 for (i = 0; i < prop->package.count; i++) 817 if (prop->package.elements[i].type != type) 818 return -EPROTO; 819 } 820 if (obj) 821 *obj = prop; 822 823 return 0; 824 } 825 826 static struct fwnode_handle * 827 acpi_fwnode_get_named_child_node(const struct fwnode_handle *fwnode, 828 const char *childname) 829 { 830 struct fwnode_handle *child; 831 832 fwnode_for_each_child_node(fwnode, child) { 833 if (is_acpi_data_node(child)) { 834 if (acpi_data_node_match(child, childname)) 835 return child; 836 continue; 837 } 838 839 if (!strncmp(acpi_device_bid(to_acpi_device_node(child)), 840 childname, ACPI_NAMESEG_SIZE)) 841 return child; 842 } 843 844 return NULL; 845 } 846 847 static int acpi_get_ref_args(struct fwnode_reference_args *args, 848 struct fwnode_handle *ref_fwnode, 849 const union acpi_object **element, 850 const union acpi_object *end, size_t num_args) 851 { 852 u32 nargs = 0, i; 853 854 /* 855 * Assume the following integer elements are all args. Stop counting on 856 * the first reference (possibly represented as a string) or end of the 857 * package arguments. In case of neither reference, nor integer, return 858 * an error, we can't parse it. 859 */ 860 for (i = 0; (*element) + i < end && i < num_args; i++) { 861 acpi_object_type type = (*element)[i].type; 862 863 if (type == ACPI_TYPE_LOCAL_REFERENCE || type == ACPI_TYPE_STRING) 864 break; 865 866 if (type == ACPI_TYPE_INTEGER) 867 nargs++; 868 else 869 return -EINVAL; 870 } 871 872 if (nargs > NR_FWNODE_REFERENCE_ARGS) 873 return -EINVAL; 874 875 if (args) { 876 args->fwnode = ref_fwnode; 877 args->nargs = nargs; 878 for (i = 0; i < nargs; i++) 879 args->args[i] = (*element)[i].integer.value; 880 } 881 882 (*element) += nargs; 883 884 return 0; 885 } 886 887 static struct fwnode_handle *acpi_parse_string_ref(const struct fwnode_handle *fwnode, 888 const char *refstring) 889 { 890 acpi_handle scope, handle; 891 struct acpi_data_node *dn; 892 struct acpi_device *device; 893 acpi_status status; 894 895 if (is_acpi_device_node(fwnode)) { 896 scope = to_acpi_device_node(fwnode)->handle; 897 } else if (is_acpi_data_node(fwnode)) { 898 scope = to_acpi_data_node(fwnode)->handle; 899 } else { 900 pr_debug("Bad node type for node %pfw\n", fwnode); 901 return NULL; 902 } 903 904 status = acpi_get_handle(scope, refstring, &handle); 905 if (ACPI_FAILURE(status)) { 906 acpi_handle_debug(scope, "Unable to get an ACPI handle for %s\n", 907 refstring); 908 return NULL; 909 } 910 911 device = acpi_fetch_acpi_dev(handle); 912 if (device) 913 return acpi_fwnode_handle(device); 914 915 status = acpi_get_data_full(handle, acpi_nondev_subnode_tag, 916 (void **)&dn, NULL); 917 if (ACPI_FAILURE(status) || !dn) { 918 acpi_handle_debug(handle, "Subnode not found\n"); 919 return NULL; 920 } 921 922 return &dn->fwnode; 923 } 924 925 /** 926 * __acpi_node_get_property_reference - returns handle to the referenced object 927 * @fwnode: Firmware node to get the property from 928 * @propname: Name of the property 929 * @index: Index of the reference to return 930 * @num_args: Maximum number of arguments after each reference 931 * @args: Location to store the returned reference with optional arguments 932 * (may be NULL) 933 * 934 * Find property with @name, verifify that it is a package containing at least 935 * one object reference and if so, store the ACPI device object pointer to the 936 * target object in @args->adev. If the reference includes arguments, store 937 * them in the @args->args[] array. 938 * 939 * If there's more than one reference in the property value package, @index is 940 * used to select the one to return. 941 * 942 * It is possible to leave holes in the property value set like in the 943 * example below: 944 * 945 * Package () { 946 * "cs-gpios", 947 * Package () { 948 * ^GPIO, 19, 0, 0, 949 * ^GPIO, 20, 0, 0, 950 * 0, 951 * ^GPIO, 21, 0, 0, 952 * } 953 * } 954 * 955 * Calling this function with index %2 or index %3 return %-ENOENT. If the 956 * property does not contain any more values %-ENOENT is returned. The NULL 957 * entry must be single integer and preferably contain value %0. 958 * 959 * Return: %0 on success, negative error code on failure. 960 */ 961 int __acpi_node_get_property_reference(const struct fwnode_handle *fwnode, 962 const char *propname, size_t index, size_t num_args, 963 struct fwnode_reference_args *args) 964 { 965 const union acpi_object *element, *end; 966 const union acpi_object *obj; 967 const struct acpi_device_data *data; 968 struct fwnode_handle *ref_fwnode; 969 struct acpi_device *device; 970 int ret, idx = 0; 971 972 data = acpi_device_data_of_node(fwnode); 973 if (!data) 974 return -ENOENT; 975 976 ret = acpi_data_get_property(data, propname, ACPI_TYPE_ANY, &obj); 977 if (ret) 978 return ret == -EINVAL ? -ENOENT : -EINVAL; 979 980 switch (obj->type) { 981 case ACPI_TYPE_LOCAL_REFERENCE: 982 /* Plain single reference without arguments. */ 983 if (index) 984 return -ENOENT; 985 986 device = acpi_fetch_acpi_dev(obj->reference.handle); 987 if (!device) 988 return -EINVAL; 989 990 if (!args) 991 return 0; 992 993 args->fwnode = acpi_fwnode_handle(device); 994 args->nargs = 0; 995 996 return 0; 997 case ACPI_TYPE_STRING: 998 if (index) 999 return -ENOENT; 1000 1001 ref_fwnode = acpi_parse_string_ref(fwnode, obj->string.pointer); 1002 if (!ref_fwnode) 1003 return -EINVAL; 1004 1005 args->fwnode = ref_fwnode; 1006 args->nargs = 0; 1007 1008 return 0; 1009 case ACPI_TYPE_PACKAGE: 1010 /* 1011 * If it is not a single reference, then it is a package of 1012 * references, followed by number of ints as follows: 1013 * 1014 * Package () { REF, INT, REF, INT, INT } 1015 * 1016 * Here, REF may be either a local reference or a string. The 1017 * index argument is then used to determine which reference the 1018 * caller wants (along with the arguments). 1019 */ 1020 break; 1021 default: 1022 return -EINVAL; 1023 } 1024 1025 if (index >= obj->package.count) 1026 return -ENOENT; 1027 1028 element = obj->package.elements; 1029 end = element + obj->package.count; 1030 1031 while (element < end) { 1032 switch (element->type) { 1033 case ACPI_TYPE_LOCAL_REFERENCE: 1034 device = acpi_fetch_acpi_dev(element->reference.handle); 1035 if (!device) 1036 return -EINVAL; 1037 1038 element++; 1039 1040 ret = acpi_get_ref_args(idx == index ? args : NULL, 1041 acpi_fwnode_handle(device), 1042 &element, end, num_args); 1043 if (ret < 0) 1044 return ret; 1045 1046 if (idx == index) 1047 return 0; 1048 1049 break; 1050 case ACPI_TYPE_STRING: 1051 ref_fwnode = acpi_parse_string_ref(fwnode, 1052 element->string.pointer); 1053 if (!ref_fwnode) 1054 return -EINVAL; 1055 1056 element++; 1057 1058 ret = acpi_get_ref_args(idx == index ? args : NULL, 1059 ref_fwnode, &element, end, 1060 num_args); 1061 if (ret < 0) 1062 return ret; 1063 1064 if (idx == index) 1065 return 0; 1066 1067 break; 1068 case ACPI_TYPE_INTEGER: 1069 if (idx == index) 1070 return -ENOENT; 1071 element++; 1072 break; 1073 default: 1074 return -EINVAL; 1075 } 1076 1077 idx++; 1078 } 1079 1080 return -ENOENT; 1081 } 1082 EXPORT_SYMBOL_GPL(__acpi_node_get_property_reference); 1083 1084 static int acpi_data_prop_read_single(const struct acpi_device_data *data, 1085 const char *propname, 1086 enum dev_prop_type proptype, void *val) 1087 { 1088 const union acpi_object *obj; 1089 int ret = 0; 1090 1091 if (proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) 1092 ret = acpi_data_get_property(data, propname, ACPI_TYPE_INTEGER, &obj); 1093 else if (proptype == DEV_PROP_STRING) 1094 ret = acpi_data_get_property(data, propname, ACPI_TYPE_STRING, &obj); 1095 if (ret) 1096 return ret; 1097 1098 switch (proptype) { 1099 case DEV_PROP_U8: 1100 if (obj->integer.value > U8_MAX) 1101 return -EOVERFLOW; 1102 if (val) 1103 *(u8 *)val = obj->integer.value; 1104 break; 1105 case DEV_PROP_U16: 1106 if (obj->integer.value > U16_MAX) 1107 return -EOVERFLOW; 1108 if (val) 1109 *(u16 *)val = obj->integer.value; 1110 break; 1111 case DEV_PROP_U32: 1112 if (obj->integer.value > U32_MAX) 1113 return -EOVERFLOW; 1114 if (val) 1115 *(u32 *)val = obj->integer.value; 1116 break; 1117 case DEV_PROP_U64: 1118 if (val) 1119 *(u64 *)val = obj->integer.value; 1120 break; 1121 case DEV_PROP_STRING: 1122 if (val) 1123 *(char **)val = obj->string.pointer; 1124 return 1; 1125 default: 1126 return -EINVAL; 1127 } 1128 1129 /* When no storage provided return number of available values */ 1130 return val ? 0 : 1; 1131 } 1132 1133 #define acpi_copy_property_array_uint(items, val, nval) \ 1134 ({ \ 1135 typeof(items) __items = items; \ 1136 typeof(val) __val = val; \ 1137 typeof(nval) __nval = nval; \ 1138 size_t i; \ 1139 int ret = 0; \ 1140 \ 1141 for (i = 0; i < __nval; i++) { \ 1142 if (__items->type == ACPI_TYPE_BUFFER) { \ 1143 __val[i] = __items->buffer.pointer[i]; \ 1144 continue; \ 1145 } \ 1146 if (__items[i].type != ACPI_TYPE_INTEGER) { \ 1147 ret = -EPROTO; \ 1148 break; \ 1149 } \ 1150 if (__items[i].integer.value > _Generic(__val, \ 1151 u8 *: U8_MAX, \ 1152 u16 *: U16_MAX, \ 1153 u32 *: U32_MAX, \ 1154 u64 *: U64_MAX)) { \ 1155 ret = -EOVERFLOW; \ 1156 break; \ 1157 } \ 1158 \ 1159 __val[i] = __items[i].integer.value; \ 1160 } \ 1161 ret; \ 1162 }) 1163 1164 static int acpi_copy_property_array_string(const union acpi_object *items, 1165 char **val, size_t nval) 1166 { 1167 int i; 1168 1169 for (i = 0; i < nval; i++) { 1170 if (items[i].type != ACPI_TYPE_STRING) 1171 return -EPROTO; 1172 1173 val[i] = items[i].string.pointer; 1174 } 1175 return nval; 1176 } 1177 1178 static int acpi_data_prop_read(const struct acpi_device_data *data, 1179 const char *propname, 1180 enum dev_prop_type proptype, 1181 void *val, size_t nval) 1182 { 1183 const union acpi_object *obj; 1184 const union acpi_object *items; 1185 int ret; 1186 1187 if (nval == 1 || !val) { 1188 ret = acpi_data_prop_read_single(data, propname, proptype, val); 1189 /* 1190 * The overflow error means that the property is there and it is 1191 * single-value, but its type does not match, so return. 1192 */ 1193 if (ret >= 0 || ret == -EOVERFLOW) 1194 return ret; 1195 1196 /* 1197 * Reading this property as a single-value one failed, but its 1198 * value may still be represented as one-element array, so 1199 * continue. 1200 */ 1201 } 1202 1203 ret = acpi_data_get_property_array(data, propname, ACPI_TYPE_ANY, &obj); 1204 if (ret && proptype >= DEV_PROP_U8 && proptype <= DEV_PROP_U64) 1205 ret = acpi_data_get_property(data, propname, ACPI_TYPE_BUFFER, 1206 &obj); 1207 if (ret) 1208 return ret; 1209 1210 if (!val) { 1211 if (obj->type == ACPI_TYPE_BUFFER) 1212 return obj->buffer.length; 1213 1214 return obj->package.count; 1215 } 1216 1217 switch (proptype) { 1218 case DEV_PROP_STRING: 1219 break; 1220 default: 1221 if (obj->type == ACPI_TYPE_BUFFER) { 1222 if (nval > obj->buffer.length) 1223 return -EOVERFLOW; 1224 } else { 1225 if (nval > obj->package.count) 1226 return -EOVERFLOW; 1227 } 1228 break; 1229 } 1230 1231 if (obj->type == ACPI_TYPE_BUFFER) { 1232 if (proptype != DEV_PROP_U8) 1233 return -EPROTO; 1234 items = obj; 1235 } else { 1236 items = obj->package.elements; 1237 } 1238 1239 switch (proptype) { 1240 case DEV_PROP_U8: 1241 ret = acpi_copy_property_array_uint(items, (u8 *)val, nval); 1242 break; 1243 case DEV_PROP_U16: 1244 ret = acpi_copy_property_array_uint(items, (u16 *)val, nval); 1245 break; 1246 case DEV_PROP_U32: 1247 ret = acpi_copy_property_array_uint(items, (u32 *)val, nval); 1248 break; 1249 case DEV_PROP_U64: 1250 ret = acpi_copy_property_array_uint(items, (u64 *)val, nval); 1251 break; 1252 case DEV_PROP_STRING: 1253 nval = min_t(u32, nval, obj->package.count); 1254 if (nval == 0) 1255 return -ENODATA; 1256 1257 ret = acpi_copy_property_array_string(items, (char **)val, nval); 1258 break; 1259 default: 1260 ret = -EINVAL; 1261 break; 1262 } 1263 return ret; 1264 } 1265 1266 /** 1267 * acpi_node_prop_read - retrieve the value of an ACPI property with given name. 1268 * @fwnode: Firmware node to get the property from. 1269 * @propname: Name of the property. 1270 * @proptype: Expected property type. 1271 * @val: Location to store the property value (if not %NULL). 1272 * @nval: Size of the array pointed to by @val. 1273 * 1274 * If @val is %NULL, return the number of array elements comprising the value 1275 * of the property. Otherwise, read at most @nval values to the array at the 1276 * location pointed to by @val. 1277 */ 1278 static int acpi_node_prop_read(const struct fwnode_handle *fwnode, 1279 const char *propname, enum dev_prop_type proptype, 1280 void *val, size_t nval) 1281 { 1282 return acpi_data_prop_read(acpi_device_data_of_node(fwnode), 1283 propname, proptype, val, nval); 1284 } 1285 1286 static int stop_on_next(struct acpi_device *adev, void *data) 1287 { 1288 struct acpi_device **ret_p = data; 1289 1290 if (!*ret_p) { 1291 *ret_p = adev; 1292 return 1; 1293 } 1294 1295 /* Skip until the "previous" object is found. */ 1296 if (*ret_p == adev) 1297 *ret_p = NULL; 1298 1299 return 0; 1300 } 1301 1302 /** 1303 * acpi_get_next_subnode - Return the next child node handle for a fwnode 1304 * @fwnode: Firmware node to find the next child node for. 1305 * @child: Handle to one of the device's child nodes or a null handle. 1306 */ 1307 struct fwnode_handle *acpi_get_next_subnode(const struct fwnode_handle *fwnode, 1308 struct fwnode_handle *child) 1309 { 1310 struct acpi_device *adev = to_acpi_device_node(fwnode); 1311 1312 if ((!child || is_acpi_device_node(child)) && adev) { 1313 struct acpi_device *child_adev = to_acpi_device_node(child); 1314 1315 acpi_dev_for_each_child(adev, stop_on_next, &child_adev); 1316 if (child_adev) 1317 return acpi_fwnode_handle(child_adev); 1318 1319 child = NULL; 1320 } 1321 1322 if (!child || is_acpi_data_node(child)) { 1323 const struct acpi_data_node *data = to_acpi_data_node(fwnode); 1324 const struct list_head *head; 1325 struct list_head *next; 1326 struct acpi_data_node *dn; 1327 1328 /* 1329 * We can have a combination of device and data nodes, e.g. with 1330 * hierarchical _DSD properties. Make sure the adev pointer is 1331 * restored before going through data nodes, otherwise we will 1332 * be looking for data_nodes below the last device found instead 1333 * of the common fwnode shared by device_nodes and data_nodes. 1334 */ 1335 adev = to_acpi_device_node(fwnode); 1336 if (adev) 1337 head = &adev->data.subnodes; 1338 else if (data) 1339 head = &data->data.subnodes; 1340 else 1341 return NULL; 1342 1343 if (list_empty(head)) 1344 return NULL; 1345 1346 if (child) { 1347 dn = to_acpi_data_node(child); 1348 next = dn->sibling.next; 1349 if (next == head) 1350 return NULL; 1351 1352 dn = list_entry(next, struct acpi_data_node, sibling); 1353 } else { 1354 dn = list_first_entry(head, struct acpi_data_node, sibling); 1355 } 1356 return &dn->fwnode; 1357 } 1358 return NULL; 1359 } 1360 1361 /** 1362 * acpi_node_get_parent - Return parent fwnode of this fwnode 1363 * @fwnode: Firmware node whose parent to get 1364 * 1365 * Returns parent node of an ACPI device or data firmware node or %NULL if 1366 * not available. 1367 */ 1368 static struct fwnode_handle * 1369 acpi_node_get_parent(const struct fwnode_handle *fwnode) 1370 { 1371 if (is_acpi_data_node(fwnode)) { 1372 /* All data nodes have parent pointer so just return that */ 1373 return to_acpi_data_node(fwnode)->parent; 1374 } 1375 if (is_acpi_device_node(fwnode)) { 1376 struct acpi_device *parent; 1377 1378 parent = acpi_dev_parent(to_acpi_device_node(fwnode)); 1379 if (parent) 1380 return acpi_fwnode_handle(parent); 1381 } 1382 1383 return NULL; 1384 } 1385 1386 /* 1387 * Return true if the node is an ACPI graph node. Called on either ports 1388 * or endpoints. 1389 */ 1390 static bool is_acpi_graph_node(struct fwnode_handle *fwnode, 1391 const char *str) 1392 { 1393 unsigned int len = strlen(str); 1394 const char *name; 1395 1396 if (!len || !is_acpi_data_node(fwnode)) 1397 return false; 1398 1399 name = to_acpi_data_node(fwnode)->name; 1400 1401 return (fwnode_property_present(fwnode, "reg") && 1402 !strncmp(name, str, len) && name[len] == '@') || 1403 fwnode_property_present(fwnode, str); 1404 } 1405 1406 /** 1407 * acpi_graph_get_next_endpoint - Get next endpoint ACPI firmware node 1408 * @fwnode: Pointer to the parent firmware node 1409 * @prev: Previous endpoint node or %NULL to get the first 1410 * 1411 * Looks up next endpoint ACPI firmware node below a given @fwnode. Returns 1412 * %NULL if there is no next endpoint or in case of error. In case of success 1413 * the next endpoint is returned. 1414 */ 1415 static struct fwnode_handle *acpi_graph_get_next_endpoint( 1416 const struct fwnode_handle *fwnode, struct fwnode_handle *prev) 1417 { 1418 struct fwnode_handle *port = NULL; 1419 struct fwnode_handle *endpoint; 1420 1421 if (!prev) { 1422 do { 1423 port = fwnode_get_next_child_node(fwnode, port); 1424 /* 1425 * The names of the port nodes begin with "port@" 1426 * followed by the number of the port node and they also 1427 * have a "reg" property that also has the number of the 1428 * port node. For compatibility reasons a node is also 1429 * recognised as a port node from the "port" property. 1430 */ 1431 if (is_acpi_graph_node(port, "port")) 1432 break; 1433 } while (port); 1434 } else { 1435 port = fwnode_get_parent(prev); 1436 } 1437 1438 if (!port) 1439 return NULL; 1440 1441 endpoint = fwnode_get_next_child_node(port, prev); 1442 while (!endpoint) { 1443 port = fwnode_get_next_child_node(fwnode, port); 1444 if (!port) 1445 break; 1446 if (is_acpi_graph_node(port, "port")) 1447 endpoint = fwnode_get_next_child_node(port, NULL); 1448 } 1449 1450 /* 1451 * The names of the endpoint nodes begin with "endpoint@" followed by 1452 * the number of the endpoint node and they also have a "reg" property 1453 * that also has the number of the endpoint node. For compatibility 1454 * reasons a node is also recognised as an endpoint node from the 1455 * "endpoint" property. 1456 */ 1457 if (!is_acpi_graph_node(endpoint, "endpoint")) 1458 return NULL; 1459 1460 return endpoint; 1461 } 1462 1463 /** 1464 * acpi_graph_get_child_prop_value - Return a child with a given property value 1465 * @fwnode: device fwnode 1466 * @prop_name: The name of the property to look for 1467 * @val: the desired property value 1468 * 1469 * Return the port node corresponding to a given port number. Returns 1470 * the child node on success, NULL otherwise. 1471 */ 1472 static struct fwnode_handle *acpi_graph_get_child_prop_value( 1473 const struct fwnode_handle *fwnode, const char *prop_name, 1474 unsigned int val) 1475 { 1476 struct fwnode_handle *child; 1477 1478 fwnode_for_each_child_node(fwnode, child) { 1479 u32 nr; 1480 1481 if (fwnode_property_read_u32(child, prop_name, &nr)) 1482 continue; 1483 1484 if (val == nr) 1485 return child; 1486 } 1487 1488 return NULL; 1489 } 1490 1491 1492 /** 1493 * acpi_graph_get_remote_endpoint - Parses and returns remote end of an endpoint 1494 * @__fwnode: Endpoint firmware node pointing to a remote device 1495 * 1496 * Returns the remote endpoint corresponding to @__fwnode. NULL on error. 1497 */ 1498 static struct fwnode_handle * 1499 acpi_graph_get_remote_endpoint(const struct fwnode_handle *__fwnode) 1500 { 1501 struct fwnode_handle *fwnode; 1502 unsigned int port_nr, endpoint_nr; 1503 struct fwnode_reference_args args; 1504 int ret; 1505 1506 memset(&args, 0, sizeof(args)); 1507 ret = acpi_node_get_property_reference(__fwnode, "remote-endpoint", 0, 1508 &args); 1509 if (ret) 1510 return NULL; 1511 1512 /* Direct endpoint reference? */ 1513 if (!is_acpi_device_node(args.fwnode)) 1514 return args.nargs ? NULL : args.fwnode; 1515 1516 /* 1517 * Always require two arguments with the reference: port and 1518 * endpoint indices. 1519 */ 1520 if (args.nargs != 2) 1521 return NULL; 1522 1523 fwnode = args.fwnode; 1524 port_nr = args.args[0]; 1525 endpoint_nr = args.args[1]; 1526 1527 fwnode = acpi_graph_get_child_prop_value(fwnode, "port", port_nr); 1528 1529 return acpi_graph_get_child_prop_value(fwnode, "endpoint", endpoint_nr); 1530 } 1531 1532 static bool acpi_fwnode_device_is_available(const struct fwnode_handle *fwnode) 1533 { 1534 if (!is_acpi_device_node(fwnode)) 1535 return true; 1536 1537 return acpi_device_is_present(to_acpi_device_node(fwnode)); 1538 } 1539 1540 static const void * 1541 acpi_fwnode_device_get_match_data(const struct fwnode_handle *fwnode, 1542 const struct device *dev) 1543 { 1544 return acpi_device_get_match_data(dev); 1545 } 1546 1547 static bool acpi_fwnode_device_dma_supported(const struct fwnode_handle *fwnode) 1548 { 1549 return acpi_dma_supported(to_acpi_device_node(fwnode)); 1550 } 1551 1552 static enum dev_dma_attr 1553 acpi_fwnode_device_get_dma_attr(const struct fwnode_handle *fwnode) 1554 { 1555 return acpi_get_dma_attr(to_acpi_device_node(fwnode)); 1556 } 1557 1558 static bool acpi_fwnode_property_present(const struct fwnode_handle *fwnode, 1559 const char *propname) 1560 { 1561 return !acpi_node_prop_get(fwnode, propname, NULL); 1562 } 1563 1564 static int 1565 acpi_fwnode_property_read_int_array(const struct fwnode_handle *fwnode, 1566 const char *propname, 1567 unsigned int elem_size, void *val, 1568 size_t nval) 1569 { 1570 enum dev_prop_type type; 1571 1572 switch (elem_size) { 1573 case sizeof(u8): 1574 type = DEV_PROP_U8; 1575 break; 1576 case sizeof(u16): 1577 type = DEV_PROP_U16; 1578 break; 1579 case sizeof(u32): 1580 type = DEV_PROP_U32; 1581 break; 1582 case sizeof(u64): 1583 type = DEV_PROP_U64; 1584 break; 1585 default: 1586 return -ENXIO; 1587 } 1588 1589 return acpi_node_prop_read(fwnode, propname, type, val, nval); 1590 } 1591 1592 static int 1593 acpi_fwnode_property_read_string_array(const struct fwnode_handle *fwnode, 1594 const char *propname, const char **val, 1595 size_t nval) 1596 { 1597 return acpi_node_prop_read(fwnode, propname, DEV_PROP_STRING, 1598 val, nval); 1599 } 1600 1601 static int 1602 acpi_fwnode_get_reference_args(const struct fwnode_handle *fwnode, 1603 const char *prop, const char *nargs_prop, 1604 unsigned int args_count, unsigned int index, 1605 struct fwnode_reference_args *args) 1606 { 1607 return __acpi_node_get_property_reference(fwnode, prop, index, 1608 args_count, args); 1609 } 1610 1611 static const char *acpi_fwnode_get_name(const struct fwnode_handle *fwnode) 1612 { 1613 const struct acpi_device *adev; 1614 struct fwnode_handle *parent; 1615 1616 /* Is this the root node? */ 1617 parent = fwnode_get_parent(fwnode); 1618 if (!parent) 1619 return "\\"; 1620 1621 fwnode_handle_put(parent); 1622 1623 if (is_acpi_data_node(fwnode)) { 1624 const struct acpi_data_node *dn = to_acpi_data_node(fwnode); 1625 1626 return dn->name; 1627 } 1628 1629 adev = to_acpi_device_node(fwnode); 1630 if (WARN_ON(!adev)) 1631 return NULL; 1632 1633 return acpi_device_bid(adev); 1634 } 1635 1636 static const char * 1637 acpi_fwnode_get_name_prefix(const struct fwnode_handle *fwnode) 1638 { 1639 struct fwnode_handle *parent; 1640 1641 /* Is this the root node? */ 1642 parent = fwnode_get_parent(fwnode); 1643 if (!parent) 1644 return ""; 1645 1646 /* Is this 2nd node from the root? */ 1647 parent = fwnode_get_next_parent(parent); 1648 if (!parent) 1649 return ""; 1650 1651 fwnode_handle_put(parent); 1652 1653 /* ACPI device or data node. */ 1654 return "."; 1655 } 1656 1657 static struct fwnode_handle * 1658 acpi_fwnode_get_parent(struct fwnode_handle *fwnode) 1659 { 1660 return acpi_node_get_parent(fwnode); 1661 } 1662 1663 static int acpi_fwnode_graph_parse_endpoint(const struct fwnode_handle *fwnode, 1664 struct fwnode_endpoint *endpoint) 1665 { 1666 struct fwnode_handle *port_fwnode = fwnode_get_parent(fwnode); 1667 1668 endpoint->local_fwnode = fwnode; 1669 1670 if (fwnode_property_read_u32(port_fwnode, "reg", &endpoint->port)) 1671 fwnode_property_read_u32(port_fwnode, "port", &endpoint->port); 1672 if (fwnode_property_read_u32(fwnode, "reg", &endpoint->id)) 1673 fwnode_property_read_u32(fwnode, "endpoint", &endpoint->id); 1674 1675 return 0; 1676 } 1677 1678 static int acpi_fwnode_irq_get(const struct fwnode_handle *fwnode, 1679 unsigned int index) 1680 { 1681 struct resource res; 1682 int ret; 1683 1684 ret = acpi_irq_get(ACPI_HANDLE_FWNODE(fwnode), index, &res); 1685 if (ret) 1686 return ret; 1687 1688 return res.start; 1689 } 1690 1691 #define DECLARE_ACPI_FWNODE_OPS(ops) \ 1692 const struct fwnode_operations ops = { \ 1693 .device_is_available = acpi_fwnode_device_is_available, \ 1694 .device_get_match_data = acpi_fwnode_device_get_match_data, \ 1695 .device_dma_supported = \ 1696 acpi_fwnode_device_dma_supported, \ 1697 .device_get_dma_attr = acpi_fwnode_device_get_dma_attr, \ 1698 .property_present = acpi_fwnode_property_present, \ 1699 .property_read_bool = acpi_fwnode_property_present, \ 1700 .property_read_int_array = \ 1701 acpi_fwnode_property_read_int_array, \ 1702 .property_read_string_array = \ 1703 acpi_fwnode_property_read_string_array, \ 1704 .get_parent = acpi_node_get_parent, \ 1705 .get_next_child_node = acpi_get_next_subnode, \ 1706 .get_named_child_node = acpi_fwnode_get_named_child_node, \ 1707 .get_name = acpi_fwnode_get_name, \ 1708 .get_name_prefix = acpi_fwnode_get_name_prefix, \ 1709 .get_reference_args = acpi_fwnode_get_reference_args, \ 1710 .graph_get_next_endpoint = \ 1711 acpi_graph_get_next_endpoint, \ 1712 .graph_get_remote_endpoint = \ 1713 acpi_graph_get_remote_endpoint, \ 1714 .graph_get_port_parent = acpi_fwnode_get_parent, \ 1715 .graph_parse_endpoint = acpi_fwnode_graph_parse_endpoint, \ 1716 .irq_get = acpi_fwnode_irq_get, \ 1717 }; \ 1718 EXPORT_SYMBOL_GPL(ops) 1719 1720 DECLARE_ACPI_FWNODE_OPS(acpi_device_fwnode_ops); 1721 DECLARE_ACPI_FWNODE_OPS(acpi_data_fwnode_ops); 1722 const struct fwnode_operations acpi_static_fwnode_ops; 1723 1724 bool is_acpi_device_node(const struct fwnode_handle *fwnode) 1725 { 1726 return !IS_ERR_OR_NULL(fwnode) && 1727 fwnode->ops == &acpi_device_fwnode_ops; 1728 } 1729 EXPORT_SYMBOL(is_acpi_device_node); 1730 1731 bool is_acpi_data_node(const struct fwnode_handle *fwnode) 1732 { 1733 return !IS_ERR_OR_NULL(fwnode) && fwnode->ops == &acpi_data_fwnode_ops; 1734 } 1735 EXPORT_SYMBOL(is_acpi_data_node); 1736