apple.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * apple.c - Apple ACPI quirks
  4. * Copyright (C) 2017 Lukas Wunner <lukas@wunner.de>
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/bitmap.h>
  8. #include <linux/platform_data/x86/apple.h>
  9. #include <linux/uuid.h>
  10. /* Apple _DSM device properties GUID */
  11. static const guid_t apple_prp_guid =
  12. GUID_INIT(0xa0b5b7c6, 0x1318, 0x441c,
  13. 0xb0, 0xc9, 0xfe, 0x69, 0x5e, 0xaf, 0x94, 0x9b);
  14. /**
  15. * acpi_extract_apple_properties - retrieve and convert Apple _DSM properties
  16. * @adev: ACPI device for which to retrieve the properties
  17. *
  18. * Invoke Apple's custom _DSM once to check the protocol version and once more
  19. * to retrieve the properties. They are marshalled up in a single package as
  20. * alternating key/value elements, unlike _DSD which stores them as a package
  21. * of 2-element packages. Convert to _DSD format and make them available under
  22. * the primary fwnode.
  23. */
  24. void acpi_extract_apple_properties(struct acpi_device *adev)
  25. {
  26. unsigned int i, j = 0, newsize = 0, numprops, numvalid;
  27. union acpi_object *props, *newprops;
  28. unsigned long *valid = NULL;
  29. void *free_space;
  30. if (!x86_apple_machine)
  31. return;
  32. props = acpi_evaluate_dsm_typed(adev->handle, &apple_prp_guid, 1, 0,
  33. NULL, ACPI_TYPE_BUFFER);
  34. if (!props)
  35. return;
  36. if (!props->buffer.length)
  37. goto out_free;
  38. if (props->buffer.pointer[0] != 3) {
  39. acpi_handle_info(adev->handle, FW_INFO
  40. "unsupported properties version %*ph\n",
  41. props->buffer.length, props->buffer.pointer);
  42. goto out_free;
  43. }
  44. ACPI_FREE(props);
  45. props = acpi_evaluate_dsm_typed(adev->handle, &apple_prp_guid, 1, 1,
  46. NULL, ACPI_TYPE_PACKAGE);
  47. if (!props)
  48. return;
  49. numprops = props->package.count / 2;
  50. if (!numprops)
  51. goto out_free;
  52. valid = bitmap_zalloc(numprops, GFP_KERNEL);
  53. if (!valid)
  54. goto out_free;
  55. /* newsize = key length + value length of each tuple */
  56. for (i = 0; i < numprops; i++) {
  57. union acpi_object *key = &props->package.elements[i * 2];
  58. union acpi_object *val = &props->package.elements[i * 2 + 1];
  59. if ( key->type != ACPI_TYPE_STRING ||
  60. (val->type != ACPI_TYPE_INTEGER &&
  61. val->type != ACPI_TYPE_BUFFER))
  62. continue; /* skip invalid properties */
  63. __set_bit(i, valid);
  64. newsize += key->string.length + 1;
  65. if ( val->type == ACPI_TYPE_BUFFER)
  66. newsize += val->buffer.length;
  67. }
  68. numvalid = bitmap_weight(valid, numprops);
  69. if (numprops > numvalid)
  70. acpi_handle_info(adev->handle, FW_INFO
  71. "skipped %u properties: wrong type\n",
  72. numprops - numvalid);
  73. if (numvalid == 0)
  74. goto out_free;
  75. /* newsize += top-level package + 3 objects for each key/value tuple */
  76. newsize += (1 + 3 * numvalid) * sizeof(union acpi_object);
  77. newprops = ACPI_ALLOCATE_ZEROED(newsize);
  78. if (!newprops)
  79. goto out_free;
  80. /* layout: top-level package | packages | key/value tuples | strings */
  81. newprops->type = ACPI_TYPE_PACKAGE;
  82. newprops->package.count = numvalid;
  83. newprops->package.elements = &newprops[1];
  84. free_space = &newprops[1 + 3 * numvalid];
  85. for_each_set_bit(i, valid, numprops) {
  86. union acpi_object *key = &props->package.elements[i * 2];
  87. union acpi_object *val = &props->package.elements[i * 2 + 1];
  88. unsigned int k = 1 + numvalid + j * 2; /* index into newprops */
  89. unsigned int v = k + 1;
  90. newprops[1 + j].type = ACPI_TYPE_PACKAGE;
  91. newprops[1 + j].package.count = 2;
  92. newprops[1 + j].package.elements = &newprops[k];
  93. newprops[k].type = ACPI_TYPE_STRING;
  94. newprops[k].string.length = key->string.length;
  95. newprops[k].string.pointer = free_space;
  96. memcpy(free_space, key->string.pointer, key->string.length);
  97. free_space += key->string.length + 1;
  98. newprops[v].type = val->type;
  99. if (val->type == ACPI_TYPE_INTEGER) {
  100. newprops[v].integer.value = val->integer.value;
  101. } else {
  102. newprops[v].buffer.length = val->buffer.length;
  103. newprops[v].buffer.pointer = free_space;
  104. memcpy(free_space, val->buffer.pointer,
  105. val->buffer.length);
  106. free_space += val->buffer.length;
  107. }
  108. j++; /* count valid properties */
  109. }
  110. WARN_ON(free_space != (void *)newprops + newsize);
  111. adev->data.pointer = newprops;
  112. acpi_data_add_props(&adev->data, &apple_prp_guid, newprops);
  113. out_free:
  114. ACPI_FREE(props);
  115. bitmap_free(valid);
  116. }