xref: /linux/drivers/acpi/prmt.c (revision 9bbf8e17d8521211c5c5516ed5ec78d7581aacff)
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 
23 #pragma pack(1)
24 struct prm_mmio_addr_range {
25 	u64 phys_addr;
26 	u64 virt_addr;
27 	u32 length;
28 };
29 
30 struct prm_mmio_info {
31 	u64 mmio_count;
32 	struct prm_mmio_addr_range addr_ranges[];
33 };
34 
35 struct prm_buffer {
36 	u8 prm_status;
37 	u64 efi_status;
38 	u8 prm_cmd;
39 	guid_t handler_guid;
40 };
41 
42 struct prm_context_buffer {
43 	char signature[ACPI_NAMESEG_SIZE];
44 	u16 revision;
45 	u16 reserved;
46 	guid_t identifier;
47 	u64 static_data_buffer;
48 	struct prm_mmio_info *mmio_ranges;
49 };
50 #pragma pack()
51 
52 static LIST_HEAD(prm_module_list);
53 
54 struct prm_handler_info {
55 	efi_guid_t guid;
56 	efi_status_t (__efiapi *handler_addr)(u64, void *);
57 	u64 static_data_buffer_addr;
58 	u64 acpi_param_buffer_addr;
59 
60 	struct list_head handler_list;
61 };
62 
63 struct prm_module_info {
64 	guid_t guid;
65 	u16 major_rev;
66 	u16 minor_rev;
67 	u16 handler_count;
68 	struct prm_mmio_info *mmio_info;
69 	bool updatable;
70 
71 	struct list_head module_list;
72 	struct prm_handler_info handlers[] __counted_by(handler_count);
73 };
74 
efi_pa_va_lookup(efi_guid_t * guid,u64 pa)75 static u64 efi_pa_va_lookup(efi_guid_t *guid, u64 pa)
76 {
77 	efi_memory_desc_t *md;
78 	u64 pa_offset = pa & ~PAGE_MASK;
79 	u64 page = pa & PAGE_MASK;
80 
81 	for_each_efi_memory_desc(md) {
82 		if ((md->attribute & EFI_MEMORY_RUNTIME) &&
83 		    (md->phys_addr < pa && pa < md->phys_addr + PAGE_SIZE * md->num_pages)) {
84 			return pa_offset + md->virt_addr + page - md->phys_addr;
85 		}
86 	}
87 
88 	return 0;
89 }
90 
91 #define get_first_handler(a) ((struct acpi_prmt_handler_info *) ((char *) (a) + a->handler_info_offset))
92 #define get_next_handler(a) ((struct acpi_prmt_handler_info *) (sizeof(struct acpi_prmt_handler_info) + (char *) a))
93 
94 static int __init
acpi_parse_prmt(union acpi_subtable_headers * header,const unsigned long end)95 acpi_parse_prmt(union acpi_subtable_headers *header, const unsigned long end)
96 {
97 	struct acpi_prmt_module_info *module_info;
98 	struct acpi_prmt_handler_info *handler_info;
99 	struct prm_handler_info *th;
100 	struct prm_module_info *tm;
101 	u64 *mmio_count;
102 	u64 cur_handler = 0;
103 	u32 module_info_size = 0;
104 	u64 mmio_range_size = 0;
105 	void *temp_mmio;
106 
107 	module_info = (struct acpi_prmt_module_info *) header;
108 	module_info_size = struct_size(tm, handlers, module_info->handler_info_count);
109 	tm = kmalloc(module_info_size, GFP_KERNEL);
110 	if (!tm)
111 		goto parse_prmt_out1;
112 
113 	guid_copy(&tm->guid, (guid_t *) module_info->module_guid);
114 	tm->major_rev = module_info->major_rev;
115 	tm->minor_rev = module_info->minor_rev;
116 	tm->handler_count = module_info->handler_info_count;
117 	tm->updatable = true;
118 
119 	if (module_info->mmio_list_pointer) {
120 		/*
121 		 * Each module is associated with a list of addr
122 		 * ranges that it can use during the service
123 		 */
124 		mmio_count = (u64 *) memremap(module_info->mmio_list_pointer, 8, MEMREMAP_WB);
125 		if (!mmio_count)
126 			goto parse_prmt_out2;
127 
128 		mmio_range_size = struct_size(tm->mmio_info, addr_ranges, *mmio_count);
129 		tm->mmio_info = kmalloc(mmio_range_size, GFP_KERNEL);
130 		if (!tm->mmio_info)
131 			goto parse_prmt_out3;
132 
133 		temp_mmio = memremap(module_info->mmio_list_pointer, mmio_range_size, MEMREMAP_WB);
134 		if (!temp_mmio)
135 			goto parse_prmt_out4;
136 		memmove(tm->mmio_info, temp_mmio, mmio_range_size);
137 	} else {
138 		tm->mmio_info = kmalloc(sizeof(*tm->mmio_info), GFP_KERNEL);
139 		if (!tm->mmio_info)
140 			goto parse_prmt_out2;
141 
142 		tm->mmio_info->mmio_count = 0;
143 	}
144 
145 	INIT_LIST_HEAD(&tm->module_list);
146 	list_add(&tm->module_list, &prm_module_list);
147 
148 	handler_info = get_first_handler(module_info);
149 	do {
150 		th = &tm->handlers[cur_handler];
151 
152 		guid_copy(&th->guid, (guid_t *)handler_info->handler_guid);
153 		th->handler_addr =
154 			(void *)efi_pa_va_lookup(&th->guid, handler_info->handler_address);
155 		/*
156 		 * Print a warning message if handler_addr is zero which is not expected to
157 		 * ever happen.
158 		 */
159 		if (unlikely(!th->handler_addr))
160 			pr_warn("Failed to find VA of handler for GUID: %pUL, PA: 0x%llx",
161 				&th->guid, handler_info->handler_address);
162 
163 		th->static_data_buffer_addr =
164 			efi_pa_va_lookup(&th->guid, handler_info->static_data_buffer_address);
165 		/*
166 		 * According to the PRM specification, static_data_buffer_address can be zero,
167 		 * so avoid printing a warning message in that case.  Otherwise, if the
168 		 * return value of efi_pa_va_lookup() is zero, print the message.
169 		 */
170 		if (unlikely(!th->static_data_buffer_addr && handler_info->static_data_buffer_address))
171 			pr_warn("Failed to find VA of static data buffer for GUID: %pUL, PA: 0x%llx",
172 				&th->guid, handler_info->static_data_buffer_address);
173 
174 		th->acpi_param_buffer_addr =
175 			efi_pa_va_lookup(&th->guid, handler_info->acpi_param_buffer_address);
176 
177 		/*
178 		 * According to the PRM specification, acpi_param_buffer_address can be zero,
179 		 * so avoid printing a warning message in that case.  Otherwise, if the
180 		 * return value of efi_pa_va_lookup() is zero, print the message.
181 		 */
182 		if (unlikely(!th->acpi_param_buffer_addr && handler_info->acpi_param_buffer_address))
183 			pr_warn("Failed to find VA of acpi param buffer for GUID: %pUL, PA: 0x%llx",
184 				&th->guid, handler_info->acpi_param_buffer_address);
185 
186 	} while (++cur_handler < tm->handler_count && (handler_info = get_next_handler(handler_info)));
187 
188 	return 0;
189 
190 parse_prmt_out4:
191 	kfree(tm->mmio_info);
192 parse_prmt_out3:
193 	memunmap(mmio_count);
194 parse_prmt_out2:
195 	kfree(tm);
196 parse_prmt_out1:
197 	return -ENOMEM;
198 }
199 
200 #define GET_MODULE	0
201 #define GET_HANDLER	1
202 
find_guid_info(const guid_t * guid,u8 mode)203 static void *find_guid_info(const guid_t *guid, u8 mode)
204 {
205 	struct prm_handler_info *cur_handler;
206 	struct prm_module_info *cur_module;
207 	int i = 0;
208 
209 	list_for_each_entry(cur_module, &prm_module_list, module_list) {
210 		for (i = 0; i < cur_module->handler_count; ++i) {
211 			cur_handler = &cur_module->handlers[i];
212 			if (guid_equal(guid, &cur_handler->guid)) {
213 				if (mode == GET_MODULE)
214 					return (void *)cur_module;
215 				else
216 					return (void *)cur_handler;
217 			}
218 		}
219 	}
220 
221 	return NULL;
222 }
223 
find_prm_module(const guid_t * guid)224 static struct prm_module_info *find_prm_module(const guid_t *guid)
225 {
226 	return (struct prm_module_info *)find_guid_info(guid, GET_MODULE);
227 }
228 
find_prm_handler(const guid_t * guid)229 static struct prm_handler_info *find_prm_handler(const guid_t *guid)
230 {
231 	return (struct prm_handler_info *) find_guid_info(guid, GET_HANDLER);
232 }
233 
234 /* In-coming PRM commands */
235 
236 #define PRM_CMD_RUN_SERVICE		0
237 #define PRM_CMD_START_TRANSACTION	1
238 #define PRM_CMD_END_TRANSACTION		2
239 
240 /* statuses that can be passed back to ASL */
241 
242 #define PRM_HANDLER_SUCCESS 		0
243 #define PRM_HANDLER_ERROR 		1
244 #define INVALID_PRM_COMMAND 		2
245 #define PRM_HANDLER_GUID_NOT_FOUND 	3
246 #define UPDATE_LOCK_ALREADY_HELD 	4
247 #define UPDATE_UNLOCK_WITHOUT_LOCK 	5
248 
acpi_call_prm_handler(guid_t handler_guid,void * param_buffer)249 int acpi_call_prm_handler(guid_t handler_guid, void *param_buffer)
250 {
251 	struct prm_handler_info *handler = find_prm_handler(&handler_guid);
252 	struct prm_module_info *module = find_prm_module(&handler_guid);
253 	struct prm_context_buffer context;
254 	efi_status_t status;
255 
256 	if (!module || !handler)
257 		return -ENODEV;
258 
259 	memset(&context, 0, sizeof(context));
260 	ACPI_COPY_NAMESEG(context.signature, "PRMC");
261 	context.identifier         = handler->guid;
262 	context.static_data_buffer = handler->static_data_buffer_addr;
263 	context.mmio_ranges        = module->mmio_info;
264 
265 	status = efi_call_acpi_prm_handler(handler->handler_addr,
266 					   (u64)param_buffer,
267 					   &context);
268 
269 	return efi_status_to_err(status);
270 }
271 EXPORT_SYMBOL_GPL(acpi_call_prm_handler);
272 
273 /*
274  * This is the PlatformRtMechanism opregion space handler.
275  * @function: indicates the read/write. In fact as the PlatformRtMechanism
276  * message is driven by command, only write is meaningful.
277  *
278  * @addr   : not used
279  * @bits   : not used.
280  * @value  : it is an in/out parameter. It points to the PRM message buffer.
281  * @handler_context: not used
282  */
acpi_platformrt_space_handler(u32 function,acpi_physical_address addr,u32 bits,acpi_integer * value,void * handler_context,void * region_context)283 static acpi_status acpi_platformrt_space_handler(u32 function,
284 						 acpi_physical_address addr,
285 						 u32 bits, acpi_integer *value,
286 						 void *handler_context,
287 						 void *region_context)
288 {
289 	struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value);
290 	struct prm_handler_info *handler;
291 	struct prm_module_info *module;
292 	efi_status_t status;
293 	struct prm_context_buffer context;
294 
295 	if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
296 		pr_err_ratelimited("PRM: EFI runtime services no longer available\n");
297 		return AE_NO_HANDLER;
298 	}
299 
300 	/*
301 	 * The returned acpi_status will always be AE_OK. Error values will be
302 	 * saved in the first byte of the PRM message buffer to be used by ASL.
303 	 */
304 	switch (buffer->prm_cmd) {
305 	case PRM_CMD_RUN_SERVICE:
306 
307 		handler = find_prm_handler(&buffer->handler_guid);
308 		module = find_prm_module(&buffer->handler_guid);
309 		if (!handler || !module)
310 			goto invalid_guid;
311 
312 		if (!handler->handler_addr) {
313 			buffer->prm_status = PRM_HANDLER_ERROR;
314 			return AE_OK;
315 		}
316 
317 		ACPI_COPY_NAMESEG(context.signature, "PRMC");
318 		context.revision = 0x0;
319 		context.reserved = 0x0;
320 		context.identifier = handler->guid;
321 		context.static_data_buffer = handler->static_data_buffer_addr;
322 		context.mmio_ranges = module->mmio_info;
323 
324 		status = efi_call_acpi_prm_handler(handler->handler_addr,
325 						   handler->acpi_param_buffer_addr,
326 						   &context);
327 		if (status == EFI_SUCCESS) {
328 			buffer->prm_status = PRM_HANDLER_SUCCESS;
329 		} else {
330 			buffer->prm_status = PRM_HANDLER_ERROR;
331 			buffer->efi_status = status;
332 		}
333 		break;
334 
335 	case PRM_CMD_START_TRANSACTION:
336 
337 		module = find_prm_module(&buffer->handler_guid);
338 		if (!module)
339 			goto invalid_guid;
340 
341 		if (module->updatable)
342 			module->updatable = false;
343 		else
344 			buffer->prm_status = UPDATE_LOCK_ALREADY_HELD;
345 		break;
346 
347 	case PRM_CMD_END_TRANSACTION:
348 
349 		module = find_prm_module(&buffer->handler_guid);
350 		if (!module)
351 			goto invalid_guid;
352 
353 		if (module->updatable)
354 			buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK;
355 		else
356 			module->updatable = true;
357 		break;
358 
359 	default:
360 
361 		buffer->prm_status = INVALID_PRM_COMMAND;
362 		break;
363 	}
364 
365 	return AE_OK;
366 
367 invalid_guid:
368 	buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND;
369 	return AE_OK;
370 }
371 
init_prmt(void)372 void __init init_prmt(void)
373 {
374 	struct acpi_table_header *tbl;
375 	acpi_status status;
376 	int mc;
377 
378 	status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl);
379 	if (ACPI_FAILURE(status))
380 		return;
381 
382 	mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) +
383 					  sizeof (struct acpi_table_prmt_header),
384 					  0, acpi_parse_prmt, 0);
385 	acpi_put_table(tbl);
386 	/*
387 	 * Return immediately if PRMT table is not present or no PRM module found.
388 	 */
389 	if (mc <= 0)
390 		return;
391 
392 	pr_info("PRM: found %u modules\n", mc);
393 
394 	if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
395 		pr_err("PRM: EFI runtime services unavailable\n");
396 		return;
397 	}
398 
399 	status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
400 						    ACPI_ADR_SPACE_PLATFORM_RT,
401 						    &acpi_platformrt_space_handler,
402 						    NULL, NULL);
403 	if (ACPI_FAILURE(status))
404 		pr_alert("PRM: OperationRegion handler could not be installed\n");
405 }
406