prmt.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. efi_guid_t guid;
  50. efi_status_t (__efiapi *handler_addr)(u64, void *);
  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[] __counted_by(handler_count);
  64. };
  65. static u64 efi_pa_va_lookup(efi_guid_t *guid, 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->attribute & EFI_MEMORY_RUNTIME) &&
  72. (md->phys_addr < pa && pa < md->phys_addr + PAGE_SIZE * md->num_pages)) {
  73. return pa_offset + md->virt_addr + page - md->phys_addr;
  74. }
  75. }
  76. pr_warn("Failed to find VA for GUID: %pUL, PA: 0x%llx", guid, pa);
  77. return 0;
  78. }
  79. #define get_first_handler(a) ((struct acpi_prmt_handler_info *) ((char *) (a) + a->handler_info_offset))
  80. #define get_next_handler(a) ((struct acpi_prmt_handler_info *) (sizeof(struct acpi_prmt_handler_info) + (char *) a))
  81. static int __init
  82. acpi_parse_prmt(union acpi_subtable_headers *header, const unsigned long end)
  83. {
  84. struct acpi_prmt_module_info *module_info;
  85. struct acpi_prmt_handler_info *handler_info;
  86. struct prm_handler_info *th;
  87. struct prm_module_info *tm;
  88. u64 *mmio_count;
  89. u64 cur_handler = 0;
  90. u32 module_info_size = 0;
  91. u64 mmio_range_size = 0;
  92. void *temp_mmio;
  93. module_info = (struct acpi_prmt_module_info *) header;
  94. module_info_size = struct_size(tm, handlers, module_info->handler_info_count);
  95. tm = kmalloc(module_info_size, GFP_KERNEL);
  96. if (!tm)
  97. goto parse_prmt_out1;
  98. guid_copy(&tm->guid, (guid_t *) module_info->module_guid);
  99. tm->major_rev = module_info->major_rev;
  100. tm->minor_rev = module_info->minor_rev;
  101. tm->handler_count = module_info->handler_info_count;
  102. tm->updatable = true;
  103. if (module_info->mmio_list_pointer) {
  104. /*
  105. * Each module is associated with a list of addr
  106. * ranges that it can use during the service
  107. */
  108. mmio_count = (u64 *) memremap(module_info->mmio_list_pointer, 8, MEMREMAP_WB);
  109. if (!mmio_count)
  110. goto parse_prmt_out2;
  111. mmio_range_size = struct_size(tm->mmio_info, addr_ranges, *mmio_count);
  112. tm->mmio_info = kmalloc(mmio_range_size, GFP_KERNEL);
  113. if (!tm->mmio_info)
  114. goto parse_prmt_out3;
  115. temp_mmio = memremap(module_info->mmio_list_pointer, mmio_range_size, MEMREMAP_WB);
  116. if (!temp_mmio)
  117. goto parse_prmt_out4;
  118. memmove(tm->mmio_info, temp_mmio, mmio_range_size);
  119. } else {
  120. tm->mmio_info = kmalloc(sizeof(*tm->mmio_info), GFP_KERNEL);
  121. if (!tm->mmio_info)
  122. goto parse_prmt_out2;
  123. tm->mmio_info->mmio_count = 0;
  124. }
  125. INIT_LIST_HEAD(&tm->module_list);
  126. list_add(&tm->module_list, &prm_module_list);
  127. handler_info = get_first_handler(module_info);
  128. do {
  129. th = &tm->handlers[cur_handler];
  130. guid_copy(&th->guid, (guid_t *)handler_info->handler_guid);
  131. th->handler_addr =
  132. (void *)efi_pa_va_lookup(&th->guid, handler_info->handler_address);
  133. th->static_data_buffer_addr =
  134. efi_pa_va_lookup(&th->guid, handler_info->static_data_buffer_address);
  135. th->acpi_param_buffer_addr =
  136. efi_pa_va_lookup(&th->guid, handler_info->acpi_param_buffer_address);
  137. } while (++cur_handler < tm->handler_count && (handler_info = get_next_handler(handler_info)));
  138. return 0;
  139. parse_prmt_out4:
  140. kfree(tm->mmio_info);
  141. parse_prmt_out3:
  142. memunmap(mmio_count);
  143. parse_prmt_out2:
  144. kfree(tm);
  145. parse_prmt_out1:
  146. return -ENOMEM;
  147. }
  148. #define GET_MODULE 0
  149. #define GET_HANDLER 1
  150. static void *find_guid_info(const guid_t *guid, u8 mode)
  151. {
  152. struct prm_handler_info *cur_handler;
  153. struct prm_module_info *cur_module;
  154. int i = 0;
  155. list_for_each_entry(cur_module, &prm_module_list, module_list) {
  156. for (i = 0; i < cur_module->handler_count; ++i) {
  157. cur_handler = &cur_module->handlers[i];
  158. if (guid_equal(guid, &cur_handler->guid)) {
  159. if (mode == GET_MODULE)
  160. return (void *)cur_module;
  161. else
  162. return (void *)cur_handler;
  163. }
  164. }
  165. }
  166. return NULL;
  167. }
  168. static struct prm_module_info *find_prm_module(const guid_t *guid)
  169. {
  170. return (struct prm_module_info *)find_guid_info(guid, GET_MODULE);
  171. }
  172. static struct prm_handler_info *find_prm_handler(const guid_t *guid)
  173. {
  174. return (struct prm_handler_info *) find_guid_info(guid, GET_HANDLER);
  175. }
  176. /* In-coming PRM commands */
  177. #define PRM_CMD_RUN_SERVICE 0
  178. #define PRM_CMD_START_TRANSACTION 1
  179. #define PRM_CMD_END_TRANSACTION 2
  180. /* statuses that can be passed back to ASL */
  181. #define PRM_HANDLER_SUCCESS 0
  182. #define PRM_HANDLER_ERROR 1
  183. #define INVALID_PRM_COMMAND 2
  184. #define PRM_HANDLER_GUID_NOT_FOUND 3
  185. #define UPDATE_LOCK_ALREADY_HELD 4
  186. #define UPDATE_UNLOCK_WITHOUT_LOCK 5
  187. int acpi_call_prm_handler(guid_t handler_guid, void *param_buffer)
  188. {
  189. struct prm_handler_info *handler = find_prm_handler(&handler_guid);
  190. struct prm_module_info *module = find_prm_module(&handler_guid);
  191. struct prm_context_buffer context;
  192. efi_status_t status;
  193. if (!module || !handler)
  194. return -ENODEV;
  195. memset(&context, 0, sizeof(context));
  196. ACPI_COPY_NAMESEG(context.signature, "PRMC");
  197. context.identifier = handler->guid;
  198. context.static_data_buffer = handler->static_data_buffer_addr;
  199. context.mmio_ranges = module->mmio_info;
  200. status = efi_call_acpi_prm_handler(handler->handler_addr,
  201. (u64)param_buffer,
  202. &context);
  203. return efi_status_to_err(status);
  204. }
  205. EXPORT_SYMBOL_GPL(acpi_call_prm_handler);
  206. /*
  207. * This is the PlatformRtMechanism opregion space handler.
  208. * @function: indicates the read/write. In fact as the PlatformRtMechanism
  209. * message is driven by command, only write is meaningful.
  210. *
  211. * @addr : not used
  212. * @bits : not used.
  213. * @value : it is an in/out parameter. It points to the PRM message buffer.
  214. * @handler_context: not used
  215. */
  216. static acpi_status acpi_platformrt_space_handler(u32 function,
  217. acpi_physical_address addr,
  218. u32 bits, acpi_integer *value,
  219. void *handler_context,
  220. void *region_context)
  221. {
  222. struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value);
  223. struct prm_handler_info *handler;
  224. struct prm_module_info *module;
  225. efi_status_t status;
  226. struct prm_context_buffer context;
  227. if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
  228. pr_err_ratelimited("PRM: EFI runtime services no longer available\n");
  229. return AE_NO_HANDLER;
  230. }
  231. /*
  232. * The returned acpi_status will always be AE_OK. Error values will be
  233. * saved in the first byte of the PRM message buffer to be used by ASL.
  234. */
  235. switch (buffer->prm_cmd) {
  236. case PRM_CMD_RUN_SERVICE:
  237. handler = find_prm_handler(&buffer->handler_guid);
  238. module = find_prm_module(&buffer->handler_guid);
  239. if (!handler || !module)
  240. goto invalid_guid;
  241. if (!handler->handler_addr) {
  242. buffer->prm_status = PRM_HANDLER_ERROR;
  243. return AE_OK;
  244. }
  245. ACPI_COPY_NAMESEG(context.signature, "PRMC");
  246. context.revision = 0x0;
  247. context.reserved = 0x0;
  248. context.identifier = handler->guid;
  249. context.static_data_buffer = handler->static_data_buffer_addr;
  250. context.mmio_ranges = module->mmio_info;
  251. status = efi_call_acpi_prm_handler(handler->handler_addr,
  252. handler->acpi_param_buffer_addr,
  253. &context);
  254. if (status == EFI_SUCCESS) {
  255. buffer->prm_status = PRM_HANDLER_SUCCESS;
  256. } else {
  257. buffer->prm_status = PRM_HANDLER_ERROR;
  258. buffer->efi_status = status;
  259. }
  260. break;
  261. case PRM_CMD_START_TRANSACTION:
  262. module = find_prm_module(&buffer->handler_guid);
  263. if (!module)
  264. goto invalid_guid;
  265. if (module->updatable)
  266. module->updatable = false;
  267. else
  268. buffer->prm_status = UPDATE_LOCK_ALREADY_HELD;
  269. break;
  270. case PRM_CMD_END_TRANSACTION:
  271. module = find_prm_module(&buffer->handler_guid);
  272. if (!module)
  273. goto invalid_guid;
  274. if (module->updatable)
  275. buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK;
  276. else
  277. module->updatable = true;
  278. break;
  279. default:
  280. buffer->prm_status = INVALID_PRM_COMMAND;
  281. break;
  282. }
  283. return AE_OK;
  284. invalid_guid:
  285. buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND;
  286. return AE_OK;
  287. }
  288. void __init init_prmt(void)
  289. {
  290. struct acpi_table_header *tbl;
  291. acpi_status status;
  292. int mc;
  293. status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl);
  294. if (ACPI_FAILURE(status))
  295. return;
  296. mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) +
  297. sizeof (struct acpi_table_prmt_header),
  298. 0, acpi_parse_prmt, 0);
  299. acpi_put_table(tbl);
  300. /*
  301. * Return immediately if PRMT table is not present or no PRM module found.
  302. */
  303. if (mc <= 0)
  304. return;
  305. pr_info("PRM: found %u modules\n", mc);
  306. if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
  307. pr_err("PRM: EFI runtime services unavailable\n");
  308. return;
  309. }
  310. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  311. ACPI_ADR_SPACE_PLATFORM_RT,
  312. &acpi_platformrt_space_handler,
  313. NULL, NULL);
  314. if (ACPI_FAILURE(status))
  315. pr_alert("PRM: OperationRegion handler could not be installed\n");
  316. }