1 // SPDX-License-Identifier: GPL-2.0-only
3 * Author: Erik Kaneda <erik.kaneda@intel.com>
4 * Copyright 2020 Intel Corporation
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
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.
17 #include <linux/kernel.h>
18 #include <linux/efi.h>
19 #include <linux/acpi.h>
20 #include <linux/prmt.h>
24 struct prm_mmio_addr_range {
30 struct prm_mmio_info {
32 struct prm_mmio_addr_range addr_ranges[];
42 struct prm_context_buffer {
43 char signature[ACPI_NAMESEG_SIZE];
47 u64 static_data_buffer;
48 struct prm_mmio_info *mmio_ranges;
53 static LIST_HEAD(prm_module_list);
55 struct prm_handler_info {
58 u64 static_data_buffer_addr;
59 u64 acpi_param_buffer_addr;
61 struct list_head handler_list;
64 struct prm_module_info {
69 struct prm_mmio_info *mmio_info;
72 struct list_head module_list;
73 struct prm_handler_info handlers[];
77 static u64 efi_pa_va_lookup(u64 pa)
79 efi_memory_desc_t *md;
80 u64 pa_offset = pa & ~PAGE_MASK;
81 u64 page = pa & PAGE_MASK;
83 for_each_efi_memory_desc(md) {
84 if (md->phys_addr < pa && pa < md->phys_addr + PAGE_SIZE * md->num_pages)
85 return pa_offset + md->virt_addr + page - md->phys_addr;
92 #define get_first_handler(a) ((struct acpi_prmt_handler_info *) ((char *) (a) + a->handler_info_offset))
93 #define get_next_handler(a) ((struct acpi_prmt_handler_info *) (sizeof(struct acpi_prmt_handler_info) + (char *) a))
96 acpi_parse_prmt(union acpi_subtable_headers *header, const unsigned long end)
98 struct acpi_prmt_module_info *module_info;
99 struct acpi_prmt_handler_info *handler_info;
100 struct prm_handler_info *th;
101 struct prm_module_info *tm;
104 u32 module_info_size = 0;
105 u64 mmio_range_size = 0;
108 module_info = (struct acpi_prmt_module_info *) header;
109 module_info_size = struct_size(tm, handlers, module_info->handler_info_count);
110 tm = kmalloc(module_info_size, GFP_KERNEL);
112 guid_copy(&tm->guid, (guid_t *) module_info->module_guid);
113 tm->major_rev = module_info->major_rev;
114 tm->minor_rev = module_info->minor_rev;
115 tm->handler_count = module_info->handler_info_count;
116 tm->updatable = true;
118 if (module_info->mmio_list_pointer) {
120 * Each module is associated with a list of addr
121 * ranges that it can use during the service
123 mmio_count = *(u64 *) memremap(module_info->mmio_list_pointer, 8, MEMREMAP_WB);
124 mmio_range_size = struct_size(tm->mmio_info, addr_ranges, mmio_count);
125 tm->mmio_info = kmalloc(mmio_range_size, GFP_KERNEL);
126 temp_mmio = memremap(module_info->mmio_list_pointer, mmio_range_size, MEMREMAP_WB);
127 memmove(tm->mmio_info, temp_mmio, mmio_range_size);
129 mmio_range_size = struct_size(tm->mmio_info, addr_ranges, mmio_count);
130 tm->mmio_info = kmalloc(mmio_range_size, GFP_KERNEL);
131 tm->mmio_info->mmio_count = 0;
134 INIT_LIST_HEAD(&tm->module_list);
135 list_add(&tm->module_list, &prm_module_list);
137 handler_info = get_first_handler(module_info);
139 th = &tm->handlers[cur_handler];
141 guid_copy(&th->guid, (guid_t *)handler_info->handler_guid);
142 th->handler_addr = efi_pa_va_lookup(handler_info->handler_address);
143 th->static_data_buffer_addr = efi_pa_va_lookup(handler_info->static_data_buffer_address);
144 th->acpi_param_buffer_addr = efi_pa_va_lookup(handler_info->acpi_param_buffer_address);
145 } while (++cur_handler < tm->handler_count && (handler_info = get_next_handler(handler_info)));
151 #define GET_HANDLER 1
153 static void *find_guid_info(const guid_t *guid, u8 mode)
155 struct prm_handler_info *cur_handler;
156 struct prm_module_info *cur_module;
159 list_for_each_entry(cur_module, &prm_module_list, module_list) {
160 for (i = 0; i < cur_module->handler_count; ++i) {
161 cur_handler = &cur_module->handlers[i];
162 if (guid_equal(guid, &cur_handler->guid)) {
163 if (mode == GET_MODULE)
164 return (void *)cur_module;
166 return (void *)cur_handler;
175 static struct prm_module_info *find_prm_module(const guid_t *guid)
177 return (struct prm_module_info *)find_guid_info(guid, GET_MODULE);
180 static struct prm_handler_info *find_prm_handler(const guid_t *guid)
182 return (struct prm_handler_info *) find_guid_info(guid, GET_HANDLER);
185 /* In-coming PRM commands */
187 #define PRM_CMD_RUN_SERVICE 0
188 #define PRM_CMD_START_TRANSACTION 1
189 #define PRM_CMD_END_TRANSACTION 2
191 /* statuses that can be passed back to ASL */
193 #define PRM_HANDLER_SUCCESS 0
194 #define PRM_HANDLER_ERROR 1
195 #define INVALID_PRM_COMMAND 2
196 #define PRM_HANDLER_GUID_NOT_FOUND 3
197 #define UPDATE_LOCK_ALREADY_HELD 4
198 #define UPDATE_UNLOCK_WITHOUT_LOCK 5
201 * This is the PlatformRtMechanism opregion space handler.
202 * @function: indicates the read/write. In fact as the PlatformRtMechanism
203 * message is driven by command, only write is meaningful.
207 * @value : it is an in/out parameter. It points to the PRM message buffer.
208 * @handler_context: not used
210 static acpi_status acpi_platformrt_space_handler(u32 function,
211 acpi_physical_address addr,
212 u32 bits, acpi_integer *value,
213 void *handler_context,
214 void *region_context)
216 struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value);
217 struct prm_handler_info *handler;
218 struct prm_module_info *module;
220 struct prm_context_buffer context;
223 * The returned acpi_status will always be AE_OK. Error values will be
224 * saved in the first byte of the PRM message buffer to be used by ASL.
226 switch (buffer->prm_cmd) {
227 case PRM_CMD_RUN_SERVICE:
229 handler = find_prm_handler(&buffer->handler_guid);
230 module = find_prm_module(&buffer->handler_guid);
231 if (!handler || !module)
234 ACPI_COPY_NAMESEG(context.signature, "PRMC");
235 context.revision = 0x0;
236 context.reserved = 0x0;
237 context.identifier = handler->guid;
238 context.static_data_buffer = handler->static_data_buffer_addr;
239 context.mmio_ranges = module->mmio_info;
241 status = efi_call_virt_pointer(handler, handler_addr,
242 handler->acpi_param_buffer_addr,
244 if (status == EFI_SUCCESS) {
245 buffer->prm_status = PRM_HANDLER_SUCCESS;
247 buffer->prm_status = PRM_HANDLER_ERROR;
248 buffer->efi_status = status;
252 case PRM_CMD_START_TRANSACTION:
254 module = find_prm_module(&buffer->handler_guid);
258 if (module->updatable)
259 module->updatable = false;
261 buffer->prm_status = UPDATE_LOCK_ALREADY_HELD;
264 case PRM_CMD_END_TRANSACTION:
266 module = find_prm_module(&buffer->handler_guid);
270 if (module->updatable)
271 buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK;
273 module->updatable = true;
278 buffer->prm_status = INVALID_PRM_COMMAND;
285 buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND;
289 void __init init_prmt(void)
291 struct acpi_table_header *tbl;
295 status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl);
296 if (ACPI_FAILURE(status))
299 mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) +
300 sizeof (struct acpi_table_prmt_header),
301 0, acpi_parse_prmt, 0);
304 * Return immediately if PRMT table is not present or no PRM module found.
309 pr_info("PRM: found %u modules\n", mc);
311 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
312 ACPI_ADR_SPACE_PLATFORM_RT,
313 &acpi_platformrt_space_handler,
315 if (ACPI_FAILURE(status))
316 pr_alert("PRM: OperationRegion handler could not be installed\n");