prmt.c 11 KB

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