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 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 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 154 /* 155 * Print an error message if handler_address is NULL, the parse of VA also 156 * can be skipped. 157 */ 158 if (unlikely(!handler_info->handler_address)) { 159 pr_info("Skipping handler with NULL address for GUID: %pUL", 160 (guid_t *)handler_info->handler_guid); 161 continue; 162 } 163 164 th->handler_addr = 165 (void *)efi_pa_va_lookup(&th->guid, handler_info->handler_address); 166 /* 167 * Print a warning message and skip the parse of VA if handler_addr is zero 168 * which is not expected to ever happen. 169 */ 170 if (unlikely(!th->handler_addr)) { 171 pr_warn("Failed to find VA of handler for GUID: %pUL, PA: 0x%llx", 172 &th->guid, handler_info->handler_address); 173 continue; 174 } 175 176 th->static_data_buffer_addr = 177 efi_pa_va_lookup(&th->guid, handler_info->static_data_buffer_address); 178 /* 179 * According to the PRM specification, static_data_buffer_address can be zero, 180 * so avoid printing a warning message in that case. Otherwise, if the 181 * return value of efi_pa_va_lookup() is zero, print the message. 182 */ 183 if (unlikely(!th->static_data_buffer_addr && handler_info->static_data_buffer_address)) 184 pr_warn("Failed to find VA of static data buffer for GUID: %pUL, PA: 0x%llx", 185 &th->guid, handler_info->static_data_buffer_address); 186 187 th->acpi_param_buffer_addr = 188 efi_pa_va_lookup(&th->guid, handler_info->acpi_param_buffer_address); 189 190 /* 191 * According to the PRM specification, acpi_param_buffer_address can be zero, 192 * so avoid printing a warning message in that case. Otherwise, if the 193 * return value of efi_pa_va_lookup() is zero, print the message. 194 */ 195 if (unlikely(!th->acpi_param_buffer_addr && handler_info->acpi_param_buffer_address)) 196 pr_warn("Failed to find VA of acpi param buffer for GUID: %pUL, PA: 0x%llx", 197 &th->guid, handler_info->acpi_param_buffer_address); 198 199 } while (++cur_handler < tm->handler_count && (handler_info = get_next_handler(handler_info))); 200 201 return 0; 202 203 parse_prmt_out4: 204 kfree(tm->mmio_info); 205 parse_prmt_out3: 206 memunmap(mmio_count); 207 parse_prmt_out2: 208 kfree(tm); 209 parse_prmt_out1: 210 return -ENOMEM; 211 } 212 213 #define GET_MODULE 0 214 #define GET_HANDLER 1 215 216 static void *find_guid_info(const guid_t *guid, u8 mode) 217 { 218 struct prm_handler_info *cur_handler; 219 struct prm_module_info *cur_module; 220 int i = 0; 221 222 list_for_each_entry(cur_module, &prm_module_list, module_list) { 223 for (i = 0; i < cur_module->handler_count; ++i) { 224 cur_handler = &cur_module->handlers[i]; 225 if (guid_equal(guid, &cur_handler->guid)) { 226 if (mode == GET_MODULE) 227 return (void *)cur_module; 228 else 229 return (void *)cur_handler; 230 } 231 } 232 } 233 234 return NULL; 235 } 236 237 static struct prm_module_info *find_prm_module(const guid_t *guid) 238 { 239 return (struct prm_module_info *)find_guid_info(guid, GET_MODULE); 240 } 241 242 static struct prm_handler_info *find_prm_handler(const guid_t *guid) 243 { 244 return (struct prm_handler_info *) find_guid_info(guid, GET_HANDLER); 245 } 246 247 /* In-coming PRM commands */ 248 249 #define PRM_CMD_RUN_SERVICE 0 250 #define PRM_CMD_START_TRANSACTION 1 251 #define PRM_CMD_END_TRANSACTION 2 252 253 /* statuses that can be passed back to ASL */ 254 255 #define PRM_HANDLER_SUCCESS 0 256 #define PRM_HANDLER_ERROR 1 257 #define INVALID_PRM_COMMAND 2 258 #define PRM_HANDLER_GUID_NOT_FOUND 3 259 #define UPDATE_LOCK_ALREADY_HELD 4 260 #define UPDATE_UNLOCK_WITHOUT_LOCK 5 261 262 int acpi_call_prm_handler(guid_t handler_guid, void *param_buffer) 263 { 264 struct prm_handler_info *handler = find_prm_handler(&handler_guid); 265 struct prm_module_info *module = find_prm_module(&handler_guid); 266 struct prm_context_buffer context; 267 efi_status_t status; 268 269 if (!module || !handler) 270 return -ENODEV; 271 272 memset(&context, 0, sizeof(context)); 273 ACPI_COPY_NAMESEG(context.signature, "PRMC"); 274 context.identifier = handler->guid; 275 context.static_data_buffer = handler->static_data_buffer_addr; 276 context.mmio_ranges = module->mmio_info; 277 278 status = efi_call_acpi_prm_handler(handler->handler_addr, 279 (u64)param_buffer, 280 &context); 281 282 return efi_status_to_err(status); 283 } 284 EXPORT_SYMBOL_GPL(acpi_call_prm_handler); 285 286 /* 287 * This is the PlatformRtMechanism opregion space handler. 288 * @function: indicates the read/write. In fact as the PlatformRtMechanism 289 * message is driven by command, only write is meaningful. 290 * 291 * @addr : not used 292 * @bits : not used. 293 * @value : it is an in/out parameter. It points to the PRM message buffer. 294 * @handler_context: not used 295 */ 296 static acpi_status acpi_platformrt_space_handler(u32 function, 297 acpi_physical_address addr, 298 u32 bits, acpi_integer *value, 299 void *handler_context, 300 void *region_context) 301 { 302 struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value); 303 struct prm_handler_info *handler; 304 struct prm_module_info *module; 305 efi_status_t status; 306 struct prm_context_buffer context; 307 308 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { 309 pr_err_ratelimited("PRM: EFI runtime services no longer available\n"); 310 return AE_NO_HANDLER; 311 } 312 313 /* 314 * The returned acpi_status will always be AE_OK. Error values will be 315 * saved in the first byte of the PRM message buffer to be used by ASL. 316 */ 317 switch (buffer->prm_cmd) { 318 case PRM_CMD_RUN_SERVICE: 319 320 handler = find_prm_handler(&buffer->handler_guid); 321 module = find_prm_module(&buffer->handler_guid); 322 if (!handler || !module) 323 goto invalid_guid; 324 325 if (!handler->handler_addr) { 326 buffer->prm_status = PRM_HANDLER_ERROR; 327 return AE_OK; 328 } 329 330 ACPI_COPY_NAMESEG(context.signature, "PRMC"); 331 context.revision = 0x0; 332 context.reserved = 0x0; 333 context.identifier = handler->guid; 334 context.static_data_buffer = handler->static_data_buffer_addr; 335 context.mmio_ranges = module->mmio_info; 336 337 status = efi_call_acpi_prm_handler(handler->handler_addr, 338 handler->acpi_param_buffer_addr, 339 &context); 340 if (status == EFI_SUCCESS) { 341 buffer->prm_status = PRM_HANDLER_SUCCESS; 342 } else { 343 buffer->prm_status = PRM_HANDLER_ERROR; 344 buffer->efi_status = status; 345 } 346 break; 347 348 case PRM_CMD_START_TRANSACTION: 349 350 module = find_prm_module(&buffer->handler_guid); 351 if (!module) 352 goto invalid_guid; 353 354 if (module->updatable) 355 module->updatable = false; 356 else 357 buffer->prm_status = UPDATE_LOCK_ALREADY_HELD; 358 break; 359 360 case PRM_CMD_END_TRANSACTION: 361 362 module = find_prm_module(&buffer->handler_guid); 363 if (!module) 364 goto invalid_guid; 365 366 if (module->updatable) 367 buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK; 368 else 369 module->updatable = true; 370 break; 371 372 default: 373 374 buffer->prm_status = INVALID_PRM_COMMAND; 375 break; 376 } 377 378 return AE_OK; 379 380 invalid_guid: 381 buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND; 382 return AE_OK; 383 } 384 385 void __init init_prmt(void) 386 { 387 struct acpi_table_header *tbl; 388 acpi_status status; 389 int mc; 390 391 status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl); 392 if (ACPI_FAILURE(status)) 393 return; 394 395 mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) + 396 sizeof (struct acpi_table_prmt_header), 397 0, acpi_parse_prmt, 0); 398 acpi_put_table(tbl); 399 /* 400 * Return immediately if PRMT table is not present or no PRM module found. 401 */ 402 if (mc <= 0) 403 return; 404 405 pr_info("PRM: found %u modules\n", mc); 406 407 if (!efi_enabled(EFI_RUNTIME_SERVICES)) { 408 pr_err("PRM: EFI runtime services unavailable\n"); 409 return; 410 } 411 412 status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT, 413 ACPI_ADR_SPACE_PLATFORM_RT, 414 &acpi_platformrt_space_handler, 415 NULL, NULL); 416 if (ACPI_FAILURE(status)) 417 pr_alert("PRM: OperationRegion handler could not be installed\n"); 418 } 419