xref: /linux-6.15/drivers/acpi/prmt.c (revision caa2bd07)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Author: Erik Kaneda <[email protected]>
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 	guid_t guid;
56 	u64 handler_addr;
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[];
73 };
74 
75 static u64 efi_pa_va_lookup(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->phys_addr < pa && pa < md->phys_addr + PAGE_SIZE * md->num_pages)
83 			return pa_offset + md->virt_addr + page - md->phys_addr;
84 	}
85 
86 	return 0;
87 }
88 
89 #define get_first_handler(a) ((struct acpi_prmt_handler_info *) ((char *) (a) + a->handler_info_offset))
90 #define get_next_handler(a) ((struct acpi_prmt_handler_info *) (sizeof(struct acpi_prmt_handler_info) + (char *) a))
91 
92 static int __init
93 acpi_parse_prmt(union acpi_subtable_headers *header, const unsigned long end)
94 {
95 	struct acpi_prmt_module_info *module_info;
96 	struct acpi_prmt_handler_info *handler_info;
97 	struct prm_handler_info *th;
98 	struct prm_module_info *tm;
99 	u64 mmio_count = 0;
100 	u64 cur_handler = 0;
101 	u32 module_info_size = 0;
102 	u64 mmio_range_size = 0;
103 	void *temp_mmio;
104 
105 	module_info = (struct acpi_prmt_module_info *) header;
106 	module_info_size = struct_size(tm, handlers, module_info->handler_info_count);
107 	tm = kmalloc(module_info_size, GFP_KERNEL);
108 
109 	guid_copy(&tm->guid, (guid_t *) module_info->module_guid);
110 	tm->major_rev = module_info->major_rev;
111 	tm->minor_rev = module_info->minor_rev;
112 	tm->handler_count = module_info->handler_info_count;
113 	tm->updatable = true;
114 
115 	if (module_info->mmio_list_pointer) {
116 		/*
117 		 * Each module is associated with a list of addr
118 		 * ranges that it can use during the service
119 		 */
120 		mmio_count = *(u64 *) memremap(module_info->mmio_list_pointer, 8, MEMREMAP_WB);
121 		mmio_range_size = struct_size(tm->mmio_info, addr_ranges, mmio_count);
122 		tm->mmio_info = kmalloc(mmio_range_size, GFP_KERNEL);
123 		temp_mmio = memremap(module_info->mmio_list_pointer, mmio_range_size, MEMREMAP_WB);
124 		memmove(tm->mmio_info, temp_mmio, mmio_range_size);
125 	} else {
126 		mmio_range_size = struct_size(tm->mmio_info, addr_ranges, mmio_count);
127 		tm->mmio_info = kmalloc(mmio_range_size, GFP_KERNEL);
128 		tm->mmio_info->mmio_count = 0;
129 	}
130 
131 	INIT_LIST_HEAD(&tm->module_list);
132 	list_add(&tm->module_list, &prm_module_list);
133 
134 	handler_info = get_first_handler(module_info);
135 	do {
136 		th = &tm->handlers[cur_handler];
137 
138 		guid_copy(&th->guid, (guid_t *)handler_info->handler_guid);
139 		th->handler_addr = efi_pa_va_lookup(handler_info->handler_address);
140 		th->static_data_buffer_addr = efi_pa_va_lookup(handler_info->static_data_buffer_address);
141 		th->acpi_param_buffer_addr = efi_pa_va_lookup(handler_info->acpi_param_buffer_address);
142 	} while (++cur_handler < tm->handler_count && (handler_info = get_next_handler(handler_info)));
143 
144 	return 0;
145 }
146 
147 #define GET_MODULE	0
148 #define GET_HANDLER	1
149 
150 static void *find_guid_info(const guid_t *guid, u8 mode)
151 {
152 	struct prm_handler_info *cur_handler;
153 	struct prm_module_info *cur_module;
154 	int i = 0;
155 
156 	list_for_each_entry(cur_module, &prm_module_list, module_list) {
157 		for (i = 0; i < cur_module->handler_count; ++i) {
158 			cur_handler = &cur_module->handlers[i];
159 			if (guid_equal(guid, &cur_handler->guid)) {
160 				if (mode == GET_MODULE)
161 					return (void *)cur_module;
162 				else
163 					return (void *)cur_handler;
164 			}
165 		}
166 	}
167 
168 	return NULL;
169 }
170 
171 static struct prm_module_info *find_prm_module(const guid_t *guid)
172 {
173 	return (struct prm_module_info *)find_guid_info(guid, GET_MODULE);
174 }
175 
176 static struct prm_handler_info *find_prm_handler(const guid_t *guid)
177 {
178 	return (struct prm_handler_info *) find_guid_info(guid, GET_HANDLER);
179 }
180 
181 /* In-coming PRM commands */
182 
183 #define PRM_CMD_RUN_SERVICE		0
184 #define PRM_CMD_START_TRANSACTION	1
185 #define PRM_CMD_END_TRANSACTION		2
186 
187 /* statuses that can be passed back to ASL */
188 
189 #define PRM_HANDLER_SUCCESS 		0
190 #define PRM_HANDLER_ERROR 		1
191 #define INVALID_PRM_COMMAND 		2
192 #define PRM_HANDLER_GUID_NOT_FOUND 	3
193 #define UPDATE_LOCK_ALREADY_HELD 	4
194 #define UPDATE_UNLOCK_WITHOUT_LOCK 	5
195 
196 /*
197  * This is the PlatformRtMechanism opregion space handler.
198  * @function: indicates the read/write. In fact as the PlatformRtMechanism
199  * message is driven by command, only write is meaningful.
200  *
201  * @addr   : not used
202  * @bits   : not used.
203  * @value  : it is an in/out parameter. It points to the PRM message buffer.
204  * @handler_context: not used
205  */
206 static acpi_status acpi_platformrt_space_handler(u32 function,
207 						 acpi_physical_address addr,
208 						 u32 bits, acpi_integer *value,
209 						 void *handler_context,
210 						 void *region_context)
211 {
212 	struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value);
213 	struct prm_handler_info *handler;
214 	struct prm_module_info *module;
215 	efi_status_t status;
216 	struct prm_context_buffer context;
217 
218 	/*
219 	 * The returned acpi_status will always be AE_OK. Error values will be
220 	 * saved in the first byte of the PRM message buffer to be used by ASL.
221 	 */
222 	switch (buffer->prm_cmd) {
223 	case PRM_CMD_RUN_SERVICE:
224 
225 		handler = find_prm_handler(&buffer->handler_guid);
226 		module = find_prm_module(&buffer->handler_guid);
227 		if (!handler || !module)
228 			goto invalid_guid;
229 
230 		ACPI_COPY_NAMESEG(context.signature, "PRMC");
231 		context.revision = 0x0;
232 		context.reserved = 0x0;
233 		context.identifier = handler->guid;
234 		context.static_data_buffer = handler->static_data_buffer_addr;
235 		context.mmio_ranges = module->mmio_info;
236 
237 		status = efi_call_virt_pointer(handler, handler_addr,
238 					       handler->acpi_param_buffer_addr,
239 					       &context);
240 		if (status == EFI_SUCCESS) {
241 			buffer->prm_status = PRM_HANDLER_SUCCESS;
242 		} else {
243 			buffer->prm_status = PRM_HANDLER_ERROR;
244 			buffer->efi_status = status;
245 		}
246 		break;
247 
248 	case PRM_CMD_START_TRANSACTION:
249 
250 		module = find_prm_module(&buffer->handler_guid);
251 		if (!module)
252 			goto invalid_guid;
253 
254 		if (module->updatable)
255 			module->updatable = false;
256 		else
257 			buffer->prm_status = UPDATE_LOCK_ALREADY_HELD;
258 		break;
259 
260 	case PRM_CMD_END_TRANSACTION:
261 
262 		module = find_prm_module(&buffer->handler_guid);
263 		if (!module)
264 			goto invalid_guid;
265 
266 		if (module->updatable)
267 			buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK;
268 		else
269 			module->updatable = true;
270 		break;
271 
272 	default:
273 
274 		buffer->prm_status = INVALID_PRM_COMMAND;
275 		break;
276 	}
277 
278 	return AE_OK;
279 
280 invalid_guid:
281 	buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND;
282 	return AE_OK;
283 }
284 
285 void __init init_prmt(void)
286 {
287 	struct acpi_table_header *tbl;
288 	acpi_status status;
289 	int mc;
290 
291 	status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl);
292 	if (ACPI_FAILURE(status))
293 		return;
294 
295 	mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) +
296 					  sizeof (struct acpi_table_prmt_header),
297 					  0, acpi_parse_prmt, 0);
298 	acpi_put_table(tbl);
299 	/*
300 	 * Return immediately if PRMT table is not present or no PRM module found.
301 	 */
302 	if (mc <= 0)
303 		return;
304 
305 	pr_info("PRM: found %u modules\n", mc);
306 
307 	status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
308 						    ACPI_ADR_SPACE_PLATFORM_RT,
309 						    &acpi_platformrt_space_handler,
310 						    NULL, NULL);
311 	if (ACPI_FAILURE(status))
312 		pr_alert("PRM: OperationRegion handler could not be installed\n");
313 }
314