aml_nfw.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 <acpi/acpi_bus.h>
  26. #include <acpi/acpi_drivers.h>
  27. #include <asm/sal.h>
  28. MODULE_AUTHOR("Bjorn Helgaas <bjorn.helgaas@hp.com>");
  29. MODULE_LICENSE("GPL");
  30. MODULE_DESCRIPTION("ACPI opregion handler for native firmware calls");
  31. static int force_register;
  32. module_param_named(force, force_register, bool, 0);
  33. MODULE_PARM_DESC(force, "Install opregion handler even without HPQ5001 device");
  34. #define AML_NFW_SPACE 0xA1
  35. struct ia64_pdesc {
  36. void *ip;
  37. void *gp;
  38. };
  39. /*
  40. * N.B. The layout of this structure is defined in the HP SPPA FW EAS, and
  41. * the member offsets are embedded in AML methods.
  42. */
  43. struct ia64_nfw_context {
  44. u64 arg[8];
  45. struct ia64_sal_retval ret;
  46. u64 ip;
  47. u64 gp;
  48. u64 pad[2];
  49. };
  50. static void *virt_map(u64 address)
  51. {
  52. if (address & (1UL << 63))
  53. return (void *) (__IA64_UNCACHED_OFFSET | address);
  54. return __va(address);
  55. }
  56. static void aml_nfw_execute(struct ia64_nfw_context *c)
  57. {
  58. struct ia64_pdesc virt_entry;
  59. ia64_sal_handler entry;
  60. virt_entry.ip = virt_map(c->ip);
  61. virt_entry.gp = virt_map(c->gp);
  62. entry = (ia64_sal_handler) &virt_entry;
  63. IA64_FW_CALL(entry, c->ret,
  64. c->arg[0], c->arg[1], c->arg[2], c->arg[3],
  65. c->arg[4], c->arg[5], c->arg[6], c->arg[7]);
  66. }
  67. static void aml_nfw_read_arg(u8 *offset, u32 bit_width, acpi_integer *value)
  68. {
  69. switch (bit_width) {
  70. case 8:
  71. *value = *(u8 *)offset;
  72. break;
  73. case 16:
  74. *value = *(u16 *)offset;
  75. break;
  76. case 32:
  77. *value = *(u32 *)offset;
  78. break;
  79. case 64:
  80. *value = *(u64 *)offset;
  81. break;
  82. }
  83. }
  84. static void aml_nfw_write_arg(u8 *offset, u32 bit_width, acpi_integer *value)
  85. {
  86. switch (bit_width) {
  87. case 8:
  88. *(u8 *) offset = *value;
  89. break;
  90. case 16:
  91. *(u16 *) offset = *value;
  92. break;
  93. case 32:
  94. *(u32 *) offset = *value;
  95. break;
  96. case 64:
  97. *(u64 *) offset = *value;
  98. break;
  99. }
  100. }
  101. static acpi_status aml_nfw_handler(u32 function, acpi_physical_address address,
  102. u32 bit_width, acpi_integer *value, void *handler_context,
  103. void *region_context)
  104. {
  105. struct ia64_nfw_context *context = handler_context;
  106. u8 *offset = (u8 *) context + address;
  107. if (bit_width != 8 && bit_width != 16 &&
  108. bit_width != 32 && bit_width != 64)
  109. return AE_BAD_PARAMETER;
  110. if (address + (bit_width >> 3) > sizeof(struct ia64_nfw_context))
  111. return AE_BAD_PARAMETER;
  112. switch (function) {
  113. case ACPI_READ:
  114. if (address == offsetof(struct ia64_nfw_context, ret))
  115. aml_nfw_execute(context);
  116. aml_nfw_read_arg(offset, bit_width, value);
  117. break;
  118. case ACPI_WRITE:
  119. aml_nfw_write_arg(offset, bit_width, value);
  120. break;
  121. }
  122. return AE_OK;
  123. }
  124. static struct ia64_nfw_context global_context;
  125. static int global_handler_registered;
  126. static int aml_nfw_add_global_handler(void)
  127. {
  128. acpi_status status;
  129. if (global_handler_registered)
  130. return 0;
  131. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  132. AML_NFW_SPACE, aml_nfw_handler, NULL, &global_context);
  133. if (ACPI_FAILURE(status))
  134. return -ENODEV;
  135. global_handler_registered = 1;
  136. printk(KERN_INFO "Global 0x%02X opregion handler registered\n",
  137. AML_NFW_SPACE);
  138. return 0;
  139. }
  140. static int aml_nfw_remove_global_handler(void)
  141. {
  142. acpi_status status;
  143. if (!global_handler_registered)
  144. return 0;
  145. status = acpi_remove_address_space_handler(ACPI_ROOT_OBJECT,
  146. AML_NFW_SPACE, aml_nfw_handler);
  147. if (ACPI_FAILURE(status))
  148. return -ENODEV;
  149. global_handler_registered = 0;
  150. printk(KERN_INFO "Global 0x%02X opregion handler removed\n",
  151. AML_NFW_SPACE);
  152. return 0;
  153. }
  154. static int aml_nfw_add(struct acpi_device *device)
  155. {
  156. /*
  157. * We would normally allocate a new context structure and install
  158. * the address space handler for the specific device we found.
  159. * But the HP-UX implementation shares a single global context
  160. * and always puts the handler at the root, so we'll do the same.
  161. */
  162. return aml_nfw_add_global_handler();
  163. }
  164. static int aml_nfw_remove(struct acpi_device *device, int type)
  165. {
  166. return aml_nfw_remove_global_handler();
  167. }
  168. static const struct acpi_device_id aml_nfw_ids[] = {
  169. {"HPQ5001", 0},
  170. {"", 0}
  171. };
  172. static struct acpi_driver acpi_aml_nfw_driver = {
  173. .name = "native firmware",
  174. .ids = aml_nfw_ids,
  175. .ops = {
  176. .add = aml_nfw_add,
  177. .remove = aml_nfw_remove,
  178. },
  179. };
  180. static int __init aml_nfw_init(void)
  181. {
  182. int result;
  183. if (force_register)
  184. aml_nfw_add_global_handler();
  185. result = acpi_bus_register_driver(&acpi_aml_nfw_driver);
  186. if (result < 0) {
  187. aml_nfw_remove_global_handler();
  188. return result;
  189. }
  190. return 0;
  191. }
  192. static void __exit aml_nfw_exit(void)
  193. {
  194. acpi_bus_unregister_driver(&acpi_aml_nfw_driver);
  195. aml_nfw_remove_global_handler();
  196. }
  197. module_init(aml_nfw_init);
  198. module_exit(aml_nfw_exit);