prmt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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(sizeof(*tm->mmio_info), GFP_KERNEL);
  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. th->handler_addr =
  131. (void *)efi_pa_va_lookup(&th->guid, handler_info->handler_address);
  132. /*
  133. * Print a warning message if handler_addr is zero which is not expected to
  134. * ever happen.
  135. */
  136. if (unlikely(!th->handler_addr))
  137. pr_warn("Failed to find VA of handler for GUID: %pUL, PA: 0x%llx",
  138. &th->guid, handler_info->handler_address);
  139. th->static_data_buffer_addr =
  140. efi_pa_va_lookup(&th->guid, handler_info->static_data_buffer_address);
  141. /*
  142. * According to the PRM specification, static_data_buffer_address can be zero,
  143. * so avoid printing a warning message in that case. Otherwise, if the
  144. * return value of efi_pa_va_lookup() is zero, print the message.
  145. */
  146. if (unlikely(!th->static_data_buffer_addr && handler_info->static_data_buffer_address))
  147. pr_warn("Failed to find VA of static data buffer for GUID: %pUL, PA: 0x%llx",
  148. &th->guid, handler_info->static_data_buffer_address);
  149. th->acpi_param_buffer_addr =
  150. efi_pa_va_lookup(&th->guid, handler_info->acpi_param_buffer_address);
  151. /*
  152. * According to the PRM specification, acpi_param_buffer_address can be zero,
  153. * so avoid printing a warning message in that case. Otherwise, if the
  154. * return value of efi_pa_va_lookup() is zero, print the message.
  155. */
  156. if (unlikely(!th->acpi_param_buffer_addr && handler_info->acpi_param_buffer_address))
  157. pr_warn("Failed to find VA of acpi param buffer for GUID: %pUL, PA: 0x%llx",
  158. &th->guid, handler_info->acpi_param_buffer_address);
  159. } while (++cur_handler < tm->handler_count && (handler_info = get_next_handler(handler_info)));
  160. return 0;
  161. parse_prmt_out4:
  162. kfree(tm->mmio_info);
  163. parse_prmt_out3:
  164. memunmap(mmio_count);
  165. parse_prmt_out2:
  166. kfree(tm);
  167. parse_prmt_out1:
  168. return -ENOMEM;
  169. }
  170. #define GET_MODULE 0
  171. #define GET_HANDLER 1
  172. static void *find_guid_info(const guid_t *guid, u8 mode)
  173. {
  174. struct prm_handler_info *cur_handler;
  175. struct prm_module_info *cur_module;
  176. int i = 0;
  177. list_for_each_entry(cur_module, &prm_module_list, module_list) {
  178. for (i = 0; i < cur_module->handler_count; ++i) {
  179. cur_handler = &cur_module->handlers[i];
  180. if (guid_equal(guid, &cur_handler->guid)) {
  181. if (mode == GET_MODULE)
  182. return (void *)cur_module;
  183. else
  184. return (void *)cur_handler;
  185. }
  186. }
  187. }
  188. return NULL;
  189. }
  190. static struct prm_module_info *find_prm_module(const guid_t *guid)
  191. {
  192. return (struct prm_module_info *)find_guid_info(guid, GET_MODULE);
  193. }
  194. static struct prm_handler_info *find_prm_handler(const guid_t *guid)
  195. {
  196. return (struct prm_handler_info *) find_guid_info(guid, GET_HANDLER);
  197. }
  198. /* In-coming PRM commands */
  199. #define PRM_CMD_RUN_SERVICE 0
  200. #define PRM_CMD_START_TRANSACTION 1
  201. #define PRM_CMD_END_TRANSACTION 2
  202. /* statuses that can be passed back to ASL */
  203. #define PRM_HANDLER_SUCCESS 0
  204. #define PRM_HANDLER_ERROR 1
  205. #define INVALID_PRM_COMMAND 2
  206. #define PRM_HANDLER_GUID_NOT_FOUND 3
  207. #define UPDATE_LOCK_ALREADY_HELD 4
  208. #define UPDATE_UNLOCK_WITHOUT_LOCK 5
  209. int acpi_call_prm_handler(guid_t handler_guid, void *param_buffer)
  210. {
  211. struct prm_handler_info *handler = find_prm_handler(&handler_guid);
  212. struct prm_module_info *module = find_prm_module(&handler_guid);
  213. struct prm_context_buffer context;
  214. efi_status_t status;
  215. if (!module || !handler)
  216. return -ENODEV;
  217. memset(&context, 0, sizeof(context));
  218. ACPI_COPY_NAMESEG(context.signature, "PRMC");
  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_acpi_prm_handler(handler->handler_addr,
  223. (u64)param_buffer,
  224. &context);
  225. return efi_status_to_err(status);
  226. }
  227. EXPORT_SYMBOL_GPL(acpi_call_prm_handler);
  228. /*
  229. * This is the PlatformRtMechanism opregion space handler.
  230. * @function: indicates the read/write. In fact as the PlatformRtMechanism
  231. * message is driven by command, only write is meaningful.
  232. *
  233. * @addr : not used
  234. * @bits : not used.
  235. * @value : it is an in/out parameter. It points to the PRM message buffer.
  236. * @handler_context: not used
  237. */
  238. static acpi_status acpi_platformrt_space_handler(u32 function,
  239. acpi_physical_address addr,
  240. u32 bits, acpi_integer *value,
  241. void *handler_context,
  242. void *region_context)
  243. {
  244. struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value);
  245. struct prm_handler_info *handler;
  246. struct prm_module_info *module;
  247. efi_status_t status;
  248. struct prm_context_buffer context;
  249. if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
  250. pr_err_ratelimited("PRM: EFI runtime services no longer available\n");
  251. return AE_NO_HANDLER;
  252. }
  253. /*
  254. * The returned acpi_status will always be AE_OK. Error values will be
  255. * saved in the first byte of the PRM message buffer to be used by ASL.
  256. */
  257. switch (buffer->prm_cmd) {
  258. case PRM_CMD_RUN_SERVICE:
  259. handler = find_prm_handler(&buffer->handler_guid);
  260. module = find_prm_module(&buffer->handler_guid);
  261. if (!handler || !module)
  262. goto invalid_guid;
  263. if (!handler->handler_addr) {
  264. buffer->prm_status = PRM_HANDLER_ERROR;
  265. return AE_OK;
  266. }
  267. ACPI_COPY_NAMESEG(context.signature, "PRMC");
  268. context.revision = 0x0;
  269. context.reserved = 0x0;
  270. context.identifier = handler->guid;
  271. context.static_data_buffer = handler->static_data_buffer_addr;
  272. context.mmio_ranges = module->mmio_info;
  273. status = efi_call_acpi_prm_handler(handler->handler_addr,
  274. handler->acpi_param_buffer_addr,
  275. &context);
  276. if (status == EFI_SUCCESS) {
  277. buffer->prm_status = PRM_HANDLER_SUCCESS;
  278. } else {
  279. buffer->prm_status = PRM_HANDLER_ERROR;
  280. buffer->efi_status = status;
  281. }
  282. break;
  283. case PRM_CMD_START_TRANSACTION:
  284. module = find_prm_module(&buffer->handler_guid);
  285. if (!module)
  286. goto invalid_guid;
  287. if (module->updatable)
  288. module->updatable = false;
  289. else
  290. buffer->prm_status = UPDATE_LOCK_ALREADY_HELD;
  291. break;
  292. case PRM_CMD_END_TRANSACTION:
  293. module = find_prm_module(&buffer->handler_guid);
  294. if (!module)
  295. goto invalid_guid;
  296. if (module->updatable)
  297. buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK;
  298. else
  299. module->updatable = true;
  300. break;
  301. default:
  302. buffer->prm_status = INVALID_PRM_COMMAND;
  303. break;
  304. }
  305. return AE_OK;
  306. invalid_guid:
  307. buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND;
  308. return AE_OK;
  309. }
  310. void __init init_prmt(void)
  311. {
  312. struct acpi_table_header *tbl;
  313. acpi_status status;
  314. int mc;
  315. status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl);
  316. if (ACPI_FAILURE(status))
  317. return;
  318. mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) +
  319. sizeof (struct acpi_table_prmt_header),
  320. 0, acpi_parse_prmt, 0);
  321. acpi_put_table(tbl);
  322. /*
  323. * Return immediately if PRMT table is not present or no PRM module found.
  324. */
  325. if (mc <= 0)
  326. return;
  327. pr_info("PRM: found %u modules\n", mc);
  328. if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
  329. pr_err("PRM: EFI runtime services unavailable\n");
  330. return;
  331. }
  332. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  333. ACPI_ADR_SPACE_PLATFORM_RT,
  334. &acpi_platformrt_space_handler,
  335. NULL, NULL);
  336. if (ACPI_FAILURE(status))
  337. pr_alert("PRM: OperationRegion handler could not be installed\n");
  338. }