acpi_adxl.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Address translation interface via ACPI DSM.
  4. * Copyright (C) 2018 Intel Corporation
  5. *
  6. * Specification for this interface is available at:
  7. *
  8. * https://cdrdv2.intel.com/v1/dl/getContent/603354
  9. */
  10. #include <linux/acpi.h>
  11. #include <linux/adxl.h>
  12. #define ADXL_REVISION 0x1
  13. #define ADXL_IDX_GET_ADDR_PARAMS 0x1
  14. #define ADXL_IDX_FORWARD_TRANSLATE 0x2
  15. #define ACPI_ADXL_PATH "\\_SB.ADXL"
  16. /*
  17. * The specification doesn't provide a limit on how many
  18. * components are in a memory address. But since we allocate
  19. * memory based on the number the BIOS tells us, we should
  20. * defend against insane values.
  21. */
  22. #define ADXL_MAX_COMPONENTS 500
  23. #undef pr_fmt
  24. #define pr_fmt(fmt) "ADXL: " fmt
  25. static acpi_handle handle;
  26. static union acpi_object *params;
  27. static const guid_t adxl_guid =
  28. GUID_INIT(0xAA3C050A, 0x7EA4, 0x4C1F,
  29. 0xAF, 0xDA, 0x12, 0x67, 0xDF, 0xD3, 0xD4, 0x8D);
  30. static int adxl_count;
  31. static char **adxl_component_names;
  32. static union acpi_object *adxl_dsm(int cmd, union acpi_object argv[])
  33. {
  34. union acpi_object *obj, *o;
  35. obj = acpi_evaluate_dsm_typed(handle, &adxl_guid, ADXL_REVISION,
  36. cmd, argv, ACPI_TYPE_PACKAGE);
  37. if (!obj) {
  38. pr_info("DSM call failed for cmd=%d\n", cmd);
  39. return NULL;
  40. }
  41. if (obj->package.count != 2) {
  42. pr_info("Bad pkg count %d\n", obj->package.count);
  43. goto err;
  44. }
  45. o = obj->package.elements;
  46. if (o->type != ACPI_TYPE_INTEGER) {
  47. pr_info("Bad 1st element type %d\n", o->type);
  48. goto err;
  49. }
  50. if (o->integer.value) {
  51. pr_info("Bad ret val %llu\n", o->integer.value);
  52. goto err;
  53. }
  54. o = obj->package.elements + 1;
  55. if (o->type != ACPI_TYPE_PACKAGE) {
  56. pr_info("Bad 2nd element type %d\n", o->type);
  57. goto err;
  58. }
  59. return obj;
  60. err:
  61. ACPI_FREE(obj);
  62. return NULL;
  63. }
  64. /**
  65. * adxl_get_component_names - get list of memory component names
  66. * Returns NULL terminated list of string names
  67. *
  68. * Give the caller a pointer to the list of memory component names
  69. * e.g. { "SystemAddress", "ProcessorSocketId", "ChannelId", ... NULL }
  70. * Caller should count how many strings in order to allocate a buffer
  71. * for the return from adxl_decode().
  72. */
  73. const char * const *adxl_get_component_names(void)
  74. {
  75. return (const char * const *)adxl_component_names;
  76. }
  77. EXPORT_SYMBOL_GPL(adxl_get_component_names);
  78. /**
  79. * adxl_decode - ask BIOS to decode a system address to memory address
  80. * @addr: the address to decode
  81. * @component_values: pointer to array of values for each component
  82. * Returns 0 on success, negative error code otherwise
  83. *
  84. * The index of each value returned in the array matches the index of
  85. * each component name returned by adxl_get_component_names().
  86. * Components that are not defined for this address translation (e.g.
  87. * mirror channel number for a non-mirrored address) are set to ~0ull.
  88. */
  89. int adxl_decode(u64 addr, u64 component_values[])
  90. {
  91. union acpi_object argv4[2], *results, *r;
  92. int i, cnt;
  93. if (!adxl_component_names)
  94. return -EOPNOTSUPP;
  95. argv4[0].type = ACPI_TYPE_PACKAGE;
  96. argv4[0].package.count = 1;
  97. argv4[0].package.elements = &argv4[1];
  98. argv4[1].integer.type = ACPI_TYPE_INTEGER;
  99. argv4[1].integer.value = addr;
  100. results = adxl_dsm(ADXL_IDX_FORWARD_TRANSLATE, argv4);
  101. if (!results)
  102. return -EINVAL;
  103. r = results->package.elements + 1;
  104. cnt = r->package.count;
  105. if (cnt != adxl_count) {
  106. ACPI_FREE(results);
  107. return -EINVAL;
  108. }
  109. r = r->package.elements;
  110. for (i = 0; i < cnt; i++)
  111. component_values[i] = r[i].integer.value;
  112. ACPI_FREE(results);
  113. return 0;
  114. }
  115. EXPORT_SYMBOL_GPL(adxl_decode);
  116. static int __init adxl_init(void)
  117. {
  118. char *path = ACPI_ADXL_PATH;
  119. union acpi_object *p;
  120. acpi_status status;
  121. int i;
  122. status = acpi_get_handle(NULL, path, &handle);
  123. if (ACPI_FAILURE(status)) {
  124. pr_debug("No ACPI handle for path %s\n", path);
  125. return -ENODEV;
  126. }
  127. if (!acpi_has_method(handle, "_DSM")) {
  128. pr_info("No DSM method\n");
  129. return -ENODEV;
  130. }
  131. if (!acpi_check_dsm(handle, &adxl_guid, ADXL_REVISION,
  132. ADXL_IDX_GET_ADDR_PARAMS |
  133. ADXL_IDX_FORWARD_TRANSLATE)) {
  134. pr_info("DSM method does not support forward translate\n");
  135. return -ENODEV;
  136. }
  137. params = adxl_dsm(ADXL_IDX_GET_ADDR_PARAMS, NULL);
  138. if (!params) {
  139. pr_info("Failed to get component names\n");
  140. return -ENODEV;
  141. }
  142. p = params->package.elements + 1;
  143. adxl_count = p->package.count;
  144. if (adxl_count > ADXL_MAX_COMPONENTS) {
  145. pr_info("Insane number of address component names %d\n", adxl_count);
  146. ACPI_FREE(params);
  147. return -ENODEV;
  148. }
  149. p = p->package.elements;
  150. /*
  151. * Allocate one extra for NULL termination.
  152. */
  153. adxl_component_names = kcalloc(adxl_count + 1, sizeof(char *), GFP_KERNEL);
  154. if (!adxl_component_names) {
  155. ACPI_FREE(params);
  156. return -ENOMEM;
  157. }
  158. for (i = 0; i < adxl_count; i++)
  159. adxl_component_names[i] = p[i].string.pointer;
  160. return 0;
  161. }
  162. subsys_initcall(adxl_init);