aml_nfw.c 5.4 KB

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