aml_nfw.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OpRegion handler to allow AML to call native firmware
  4. *
  5. * (c) Copyright 2007 Hewlett-Packard Development Company, L.P.
  6. * Bjorn Helgaas <[email protected]>
  7. *
  8. * This driver implements HP Open Source Review Board proposal 1842,
  9. * which was approved on 9/20/2006.
  10. *
  11. * For technical documentation, see the HP SPPA Firmware EAS, Appendix F.
  12. *
  13. * ACPI does not define a mechanism for AML methods to call native firmware
  14. * interfaces such as PAL or SAL. This OpRegion handler adds such a mechanism.
  15. * After the handler is installed, an AML method can call native firmware by
  16. * storing the arguments and firmware entry point to specific offsets in the
  17. * OpRegion. When AML reads the "return value" offset from the OpRegion, this
  18. * handler loads up the arguments, makes the firmware call, and returns the
  19. * result.
  20. */
  21. #include <linux/module.h>
  22. #include <linux/acpi.h>
  23. #include <asm/sal.h>
  24. MODULE_AUTHOR("Bjorn Helgaas <[email protected]>");
  25. MODULE_LICENSE("GPL");
  26. MODULE_DESCRIPTION("ACPI opregion handler for native firmware calls");
  27. static bool force_register;
  28. module_param_named(force, force_register, bool, 0);
  29. MODULE_PARM_DESC(force, "Install opregion handler even without HPQ5001 device");
  30. #define AML_NFW_SPACE 0xA1
  31. struct ia64_pdesc {
  32. void *ip;
  33. void *gp;
  34. };
  35. /*
  36. * N.B. The layout of this structure is defined in the HP SPPA FW EAS, and
  37. * the member offsets are embedded in AML methods.
  38. */
  39. struct ia64_nfw_context {
  40. u64 arg[8];
  41. struct ia64_sal_retval ret;
  42. u64 ip;
  43. u64 gp;
  44. u64 pad[2];
  45. };
  46. static void *virt_map(u64 address)
  47. {
  48. if (address & (1UL << 63))
  49. return (void *) (__IA64_UNCACHED_OFFSET | address);
  50. return __va(address);
  51. }
  52. static void aml_nfw_execute(struct ia64_nfw_context *c)
  53. {
  54. struct ia64_pdesc virt_entry;
  55. ia64_sal_handler entry;
  56. virt_entry.ip = virt_map(c->ip);
  57. virt_entry.gp = virt_map(c->gp);
  58. entry = (ia64_sal_handler) &virt_entry;
  59. IA64_FW_CALL(entry, c->ret,
  60. c->arg[0], c->arg[1], c->arg[2], c->arg[3],
  61. c->arg[4], c->arg[5], c->arg[6], c->arg[7]);
  62. }
  63. static void aml_nfw_read_arg(u8 *offset, u32 bit_width, u64 *value)
  64. {
  65. switch (bit_width) {
  66. case 8:
  67. *value = *(u8 *)offset;
  68. break;
  69. case 16:
  70. *value = *(u16 *)offset;
  71. break;
  72. case 32:
  73. *value = *(u32 *)offset;
  74. break;
  75. case 64:
  76. *value = *(u64 *)offset;
  77. break;
  78. }
  79. }
  80. static void aml_nfw_write_arg(u8 *offset, u32 bit_width, u64 *value)
  81. {
  82. switch (bit_width) {
  83. case 8:
  84. *(u8 *) offset = *value;
  85. break;
  86. case 16:
  87. *(u16 *) offset = *value;
  88. break;
  89. case 32:
  90. *(u32 *) offset = *value;
  91. break;
  92. case 64:
  93. *(u64 *) offset = *value;
  94. break;
  95. }
  96. }
  97. static acpi_status aml_nfw_handler(u32 function, acpi_physical_address address,
  98. u32 bit_width, u64 *value, void *handler_context,
  99. void *region_context)
  100. {
  101. struct ia64_nfw_context *context = handler_context;
  102. u8 *offset = (u8 *) context + address;
  103. if (bit_width != 8 && bit_width != 16 &&
  104. bit_width != 32 && bit_width != 64)
  105. return AE_BAD_PARAMETER;
  106. if (address + (bit_width >> 3) > sizeof(struct ia64_nfw_context))
  107. return AE_BAD_PARAMETER;
  108. switch (function) {
  109. case ACPI_READ:
  110. if (address == offsetof(struct ia64_nfw_context, ret))
  111. aml_nfw_execute(context);
  112. aml_nfw_read_arg(offset, bit_width, value);
  113. break;
  114. case ACPI_WRITE:
  115. aml_nfw_write_arg(offset, bit_width, value);
  116. break;
  117. }
  118. return AE_OK;
  119. }
  120. static struct ia64_nfw_context global_context;
  121. static int global_handler_registered;
  122. static int aml_nfw_add_global_handler(void)
  123. {
  124. acpi_status status;
  125. if (global_handler_registered)
  126. return 0;
  127. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  128. AML_NFW_SPACE, aml_nfw_handler, NULL, &global_context);
  129. if (ACPI_FAILURE(status))
  130. return -ENODEV;
  131. global_handler_registered = 1;
  132. printk(KERN_INFO "Global 0x%02X opregion handler registered\n",
  133. AML_NFW_SPACE);
  134. return 0;
  135. }
  136. static int aml_nfw_remove_global_handler(void)
  137. {
  138. acpi_status status;
  139. if (!global_handler_registered)
  140. return 0;
  141. status = acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  142. AML_NFW_SPACE, aml_nfw_handler);
  143. if (ACPI_FAILURE(status))
  144. return -ENODEV;
  145. global_handler_registered = 0;
  146. printk(KERN_INFO "Global 0x%02X opregion handler removed\n",
  147. AML_NFW_SPACE);
  148. return 0;
  149. }
  150. static int aml_nfw_add(struct acpi_device *device)
  151. {
  152. /*
  153. * We would normally allocate a new context structure and install
  154. * the address space handler for the specific device we found.
  155. * But the HP-UX implementation shares a single global context
  156. * and always puts the handler at the root, so we'll do the same.
  157. */
  158. return aml_nfw_add_global_handler();
  159. }
  160. static int aml_nfw_remove(struct acpi_device *device)
  161. {
  162. return aml_nfw_remove_global_handler();
  163. }
  164. static const struct acpi_device_id aml_nfw_ids[] = {
  165. {"HPQ5001", 0},
  166. {"", 0}
  167. };
  168. static struct acpi_driver acpi_aml_nfw_driver = {
  169. .name = "native firmware",
  170. .ids = aml_nfw_ids,
  171. .ops = {
  172. .add = aml_nfw_add,
  173. .remove = aml_nfw_remove,
  174. },
  175. };
  176. static int __init aml_nfw_init(void)
  177. {
  178. int result;
  179. if (force_register)
  180. aml_nfw_add_global_handler();
  181. result = acpi_bus_register_driver(&acpi_aml_nfw_driver);
  182. if (result < 0) {
  183. aml_nfw_remove_global_handler();
  184. return result;
  185. }
  186. return 0;
  187. }
  188. static void __exit aml_nfw_exit(void)
  189. {
  190. acpi_bus_unregister_driver(&acpi_aml_nfw_driver);
  191. aml_nfw_remove_global_handler();
  192. }
  193. module_init(aml_nfw_init);
  194. module_exit(aml_nfw_exit);