prmt.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Author: Erik Kaneda <erik.kaneda@intel.com>
  4. * Copyright 2020 Intel Corporation
  5. *
  6. * prmt.c
  7. *
  8. * Each PRM service is an executable that is run in a restricted environment
  9. * that is invoked by writing to the PlatformRtMechanism OperationRegion from
  10. * AML bytecode.
  11. *
  12. * init_prmt initializes the Platform Runtime Mechanism (PRM) services by
  13. * processing data in the PRMT as well as registering an ACPI OperationRegion
  14. * handler for the PlatformRtMechanism subtype.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/efi.h>
  19. #include <linux/acpi.h>
  20. #include <linux/prmt.h>
  21. #include <asm/efi.h>
  22. #pragma pack(1)
  23. struct prm_mmio_addr_range {
  24. u64 phys_addr;
  25. u64 virt_addr;
  26. u32 length;
  27. };
  28. struct prm_mmio_info {
  29. u64 mmio_count;
  30. struct prm_mmio_addr_range addr_ranges[];
  31. };
  32. struct prm_buffer {
  33. u8 prm_status;
  34. u64 efi_status;
  35. u8 prm_cmd;
  36. guid_t handler_guid;
  37. };
  38. struct prm_context_buffer {
  39. char signature[ACPI_NAMESEG_SIZE];
  40. u16 revision;
  41. u16 reserved;
  42. guid_t identifier;
  43. u64 static_data_buffer;
  44. struct prm_mmio_info *mmio_ranges;
  45. };
  46. #pragma pack()
  47. static LIST_HEAD(prm_module_list);
  48. struct prm_handler_info {
  49. guid_t guid;
  50. void *handler_addr;
  51. u64 static_data_buffer_addr;
  52. u64 acpi_param_buffer_addr;
  53. struct list_head handler_list;
  54. };
  55. struct prm_module_info {
  56. guid_t guid;
  57. u16 major_rev;
  58. u16 minor_rev;
  59. u16 handler_count;
  60. struct prm_mmio_info *mmio_info;
  61. bool updatable;
  62. struct list_head module_list;
  63. struct prm_handler_info handlers[];
  64. };
  65. static u64 efi_pa_va_lookup(u64 pa)
  66. {
  67. efi_memory_desc_t *md;
  68. u64 pa_offset = pa & ~PAGE_MASK;
  69. u64 page = pa & PAGE_MASK;
  70. for_each_efi_memory_desc(md) {
  71. if (md->phys_addr < pa && pa < md->phys_addr + PAGE_SIZE * md->num_pages)
  72. return pa_offset + md->virt_addr + page - md->phys_addr;
  73. }
  74. return 0;
  75. }
  76. #define get_first_handler(a) ((struct acpi_prmt_handler_info *) ((char *) (a) + a->handler_info_offset))
  77. #define get_next_handler(a) ((struct acpi_prmt_handler_info *) (sizeof(struct acpi_prmt_handler_info) + (char *) a))
  78. static int __init
  79. acpi_parse_prmt(union acpi_subtable_headers *header, const unsigned long end)
  80. {
  81. struct acpi_prmt_module_info *module_info;
  82. struct acpi_prmt_handler_info *handler_info;
  83. struct prm_handler_info *th;
  84. struct prm_module_info *tm;
  85. u64 *mmio_count;
  86. u64 cur_handler = 0;
  87. u32 module_info_size = 0;
  88. u64 mmio_range_size = 0;
  89. void *temp_mmio;
  90. module_info = (struct acpi_prmt_module_info *) header;
  91. module_info_size = struct_size(tm, handlers, module_info->handler_info_count);
  92. tm = kmalloc(module_info_size, GFP_KERNEL);
  93. if (!tm)
  94. goto parse_prmt_out1;
  95. guid_copy(&tm->guid, (guid_t *) module_info->module_guid);
  96. tm->major_rev = module_info->major_rev;
  97. tm->minor_rev = module_info->minor_rev;
  98. tm->handler_count = module_info->handler_info_count;
  99. tm->updatable = true;
  100. if (module_info->mmio_list_pointer) {
  101. /*
  102. * Each module is associated with a list of addr
  103. * ranges that it can use during the service
  104. */
  105. mmio_count = (u64 *) memremap(module_info->mmio_list_pointer, 8, MEMREMAP_WB);
  106. if (!mmio_count)
  107. goto parse_prmt_out2;
  108. mmio_range_size = struct_size(tm->mmio_info, addr_ranges, *mmio_count);
  109. tm->mmio_info = kmalloc(mmio_range_size, GFP_KERNEL);
  110. if (!tm->mmio_info)
  111. goto parse_prmt_out3;
  112. temp_mmio = memremap(module_info->mmio_list_pointer, mmio_range_size, MEMREMAP_WB);
  113. if (!temp_mmio)
  114. goto parse_prmt_out4;
  115. memmove(tm->mmio_info, temp_mmio, mmio_range_size);
  116. } else {
  117. tm->mmio_info = kmalloc(sizeof(*tm->mmio_info), GFP_KERNEL);
  118. if (!tm->mmio_info)
  119. goto parse_prmt_out2;
  120. tm->mmio_info->mmio_count = 0;
  121. }
  122. INIT_LIST_HEAD(&tm->module_list);
  123. list_add(&tm->module_list, &prm_module_list);
  124. handler_info = get_first_handler(module_info);
  125. do {
  126. th = &tm->handlers[cur_handler];
  127. guid_copy(&th->guid, (guid_t *)handler_info->handler_guid);
  128. th->handler_addr = (void *)efi_pa_va_lookup(handler_info->handler_address);
  129. th->static_data_buffer_addr = efi_pa_va_lookup(handler_info->static_data_buffer_address);
  130. th->acpi_param_buffer_addr = efi_pa_va_lookup(handler_info->acpi_param_buffer_address);
  131. } while (++cur_handler < tm->handler_count && (handler_info = get_next_handler(handler_info)));
  132. return 0;
  133. parse_prmt_out4:
  134. kfree(tm->mmio_info);
  135. parse_prmt_out3:
  136. memunmap(mmio_count);
  137. parse_prmt_out2:
  138. kfree(tm);
  139. parse_prmt_out1:
  140. return -ENOMEM;
  141. }
  142. #define GET_MODULE 0
  143. #define GET_HANDLER 1
  144. static void *find_guid_info(const guid_t *guid, u8 mode)
  145. {
  146. struct prm_handler_info *cur_handler;
  147. struct prm_module_info *cur_module;
  148. int i = 0;
  149. list_for_each_entry(cur_module, &prm_module_list, module_list) {
  150. for (i = 0; i < cur_module->handler_count; ++i) {
  151. cur_handler = &cur_module->handlers[i];
  152. if (guid_equal(guid, &cur_handler->guid)) {
  153. if (mode == GET_MODULE)
  154. return (void *)cur_module;
  155. else
  156. return (void *)cur_handler;
  157. }
  158. }
  159. }
  160. return NULL;
  161. }
  162. static struct prm_module_info *find_prm_module(const guid_t *guid)
  163. {
  164. return (struct prm_module_info *)find_guid_info(guid, GET_MODULE);
  165. }
  166. static struct prm_handler_info *find_prm_handler(const guid_t *guid)
  167. {
  168. return (struct prm_handler_info *) find_guid_info(guid, GET_HANDLER);
  169. }
  170. /* In-coming PRM commands */
  171. #define PRM_CMD_RUN_SERVICE 0
  172. #define PRM_CMD_START_TRANSACTION 1
  173. #define PRM_CMD_END_TRANSACTION 2
  174. /* statuses that can be passed back to ASL */
  175. #define PRM_HANDLER_SUCCESS 0
  176. #define PRM_HANDLER_ERROR 1
  177. #define INVALID_PRM_COMMAND 2
  178. #define PRM_HANDLER_GUID_NOT_FOUND 3
  179. #define UPDATE_LOCK_ALREADY_HELD 4
  180. #define UPDATE_UNLOCK_WITHOUT_LOCK 5
  181. /*
  182. * This is the PlatformRtMechanism opregion space handler.
  183. * @function: indicates the read/write. In fact as the PlatformRtMechanism
  184. * message is driven by command, only write is meaningful.
  185. *
  186. * @addr : not used
  187. * @bits : not used.
  188. * @value : it is an in/out parameter. It points to the PRM message buffer.
  189. * @handler_context: not used
  190. */
  191. static acpi_status acpi_platformrt_space_handler(u32 function,
  192. acpi_physical_address addr,
  193. u32 bits, acpi_integer *value,
  194. void *handler_context,
  195. void *region_context)
  196. {
  197. struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value);
  198. struct prm_handler_info *handler;
  199. struct prm_module_info *module;
  200. efi_status_t status;
  201. struct prm_context_buffer context;
  202. if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
  203. pr_err_ratelimited("PRM: EFI runtime services no longer available\n");
  204. return AE_NO_HANDLER;
  205. }
  206. /*
  207. * The returned acpi_status will always be AE_OK. Error values will be
  208. * saved in the first byte of the PRM message buffer to be used by ASL.
  209. */
  210. switch (buffer->prm_cmd) {
  211. case PRM_CMD_RUN_SERVICE:
  212. handler = find_prm_handler(&buffer->handler_guid);
  213. module = find_prm_module(&buffer->handler_guid);
  214. if (!handler || !module)
  215. goto invalid_guid;
  216. ACPI_COPY_NAMESEG(context.signature, "PRMC");
  217. context.revision = 0x0;
  218. context.reserved = 0x0;
  219. context.identifier = handler->guid;
  220. context.static_data_buffer = handler->static_data_buffer_addr;
  221. context.mmio_ranges = module->mmio_info;
  222. status = efi_call_virt_pointer(handler, handler_addr,
  223. handler->acpi_param_buffer_addr,
  224. &context);
  225. if (status == EFI_SUCCESS) {
  226. buffer->prm_status = PRM_HANDLER_SUCCESS;
  227. } else {
  228. buffer->prm_status = PRM_HANDLER_ERROR;
  229. buffer->efi_status = status;
  230. }
  231. break;
  232. case PRM_CMD_START_TRANSACTION:
  233. module = find_prm_module(&buffer->handler_guid);
  234. if (!module)
  235. goto invalid_guid;
  236. if (module->updatable)
  237. module->updatable = false;
  238. else
  239. buffer->prm_status = UPDATE_LOCK_ALREADY_HELD;
  240. break;
  241. case PRM_CMD_END_TRANSACTION:
  242. module = find_prm_module(&buffer->handler_guid);
  243. if (!module)
  244. goto invalid_guid;
  245. if (module->updatable)
  246. buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK;
  247. else
  248. module->updatable = true;
  249. break;
  250. default:
  251. buffer->prm_status = INVALID_PRM_COMMAND;
  252. break;
  253. }
  254. return AE_OK;
  255. invalid_guid:
  256. buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND;
  257. return AE_OK;
  258. }
  259. void __init init_prmt(void)
  260. {
  261. struct acpi_table_header *tbl;
  262. acpi_status status;
  263. int mc;
  264. status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl);
  265. if (ACPI_FAILURE(status))
  266. return;
  267. mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) +
  268. sizeof (struct acpi_table_prmt_header),
  269. 0, acpi_parse_prmt, 0);
  270. acpi_put_table(tbl);
  271. /*
  272. * Return immediately if PRMT table is not present or no PRM module found.
  273. */
  274. if (mc <= 0)
  275. return;
  276. pr_info("PRM: found %u modules\n", mc);
  277. if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
  278. pr_err("PRM: EFI runtime services unavailable\n");
  279. return;
  280. }
  281. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  282. ACPI_ADR_SPACE_PLATFORM_RT,
  283. &acpi_platformrt_space_handler,
  284. NULL, NULL);
  285. if (ACPI_FAILURE(status))
  286. pr_alert("PRM: OperationRegion handler could not be installed\n");
  287. }