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