1cefc7ca4SErik Kaneda // SPDX-License-Identifier: GPL-2.0-only 2cefc7ca4SErik Kaneda /* 3cefc7ca4SErik Kaneda * Author: Erik Kaneda <erik.kaneda@intel.com> 4cefc7ca4SErik Kaneda * Copyright 2020 Intel Corporation 5cefc7ca4SErik Kaneda * 6cefc7ca4SErik Kaneda * prmt.c 7cefc7ca4SErik Kaneda * 8cefc7ca4SErik Kaneda * Each PRM service is an executable that is run in a restricted environment 9cefc7ca4SErik Kaneda * that is invoked by writing to the PlatformRtMechanism OperationRegion from 10cefc7ca4SErik Kaneda * AML bytecode. 11cefc7ca4SErik Kaneda * 12cefc7ca4SErik Kaneda * init_prmt initializes the Platform Runtime Mechanism (PRM) services by 13cefc7ca4SErik Kaneda * processing data in the PRMT as well as registering an ACPI OperationRegion 14cefc7ca4SErik Kaneda * handler for the PlatformRtMechanism subtype. 15cefc7ca4SErik Kaneda * 16cefc7ca4SErik Kaneda */ 17cefc7ca4SErik Kaneda #include <linux/kernel.h> 18cefc7ca4SErik Kaneda #include <linux/efi.h> 19cefc7ca4SErik Kaneda #include <linux/acpi.h> 20cefc7ca4SErik Kaneda #include <linux/prmt.h> 21cefc7ca4SErik Kaneda #include <asm/efi.h> 22cefc7ca4SErik Kaneda 23cefc7ca4SErik Kaneda #pragma pack(1) 24cefc7ca4SErik Kaneda struct prm_mmio_addr_range { 25cefc7ca4SErik Kaneda u64 phys_addr; 26cefc7ca4SErik Kaneda u64 virt_addr; 27cefc7ca4SErik Kaneda u32 length; 28cefc7ca4SErik Kaneda }; 29cefc7ca4SErik Kaneda 30cefc7ca4SErik Kaneda struct prm_mmio_info { 31cefc7ca4SErik Kaneda u64 mmio_count; 32cefc7ca4SErik Kaneda struct prm_mmio_addr_range addr_ranges[]; 33cefc7ca4SErik Kaneda }; 34cefc7ca4SErik Kaneda 35cefc7ca4SErik Kaneda struct prm_buffer { 36cefc7ca4SErik Kaneda u8 prm_status; 37cefc7ca4SErik Kaneda u64 efi_status; 38cefc7ca4SErik Kaneda u8 prm_cmd; 39cefc7ca4SErik Kaneda guid_t handler_guid; 40cefc7ca4SErik Kaneda }; 41cefc7ca4SErik Kaneda 42cefc7ca4SErik Kaneda struct prm_context_buffer { 43cefc7ca4SErik Kaneda char signature[ACPI_NAMESEG_SIZE]; 44cefc7ca4SErik Kaneda u16 revision; 45cefc7ca4SErik Kaneda u16 reserved; 46cefc7ca4SErik Kaneda guid_t identifier; 47cefc7ca4SErik Kaneda u64 static_data_buffer; 48cefc7ca4SErik Kaneda struct prm_mmio_info *mmio_ranges; 49cefc7ca4SErik Kaneda }; 50cefc7ca4SErik Kaneda #pragma pack() 51cefc7ca4SErik Kaneda 52d7a188bbSWei Yongjun static LIST_HEAD(prm_module_list); 53cefc7ca4SErik Kaneda 54cefc7ca4SErik Kaneda struct prm_handler_info { 55cefc7ca4SErik Kaneda guid_t guid; 56e38abdabSArd Biesheuvel efi_status_t (__efiapi *handler_addr)(u64, void *); 57cefc7ca4SErik Kaneda u64 static_data_buffer_addr; 58cefc7ca4SErik Kaneda u64 acpi_param_buffer_addr; 59cefc7ca4SErik Kaneda 60cefc7ca4SErik Kaneda struct list_head handler_list; 61cefc7ca4SErik Kaneda }; 62cefc7ca4SErik Kaneda 63cefc7ca4SErik Kaneda struct prm_module_info { 64cefc7ca4SErik Kaneda guid_t guid; 65cefc7ca4SErik Kaneda u16 major_rev; 66cefc7ca4SErik Kaneda u16 minor_rev; 67cefc7ca4SErik Kaneda u16 handler_count; 68cefc7ca4SErik Kaneda struct prm_mmio_info *mmio_info; 69cefc7ca4SErik Kaneda bool updatable; 70cefc7ca4SErik Kaneda 71cefc7ca4SErik Kaneda struct list_head module_list; 72*2e893457SKees Cook struct prm_handler_info handlers[] __counted_by(handler_count); 73cefc7ca4SErik Kaneda }; 74cefc7ca4SErik Kaneda 75cefc7ca4SErik Kaneda static u64 efi_pa_va_lookup(u64 pa) 76cefc7ca4SErik Kaneda { 77cefc7ca4SErik Kaneda efi_memory_desc_t *md; 78cefc7ca4SErik Kaneda u64 pa_offset = pa & ~PAGE_MASK; 79cefc7ca4SErik Kaneda u64 page = pa & PAGE_MASK; 80cefc7ca4SErik Kaneda 81cefc7ca4SErik Kaneda for_each_efi_memory_desc(md) { 82cefc7ca4SErik Kaneda if (md->phys_addr < pa && pa < md->phys_addr + PAGE_SIZE * md->num_pages) 83cefc7ca4SErik Kaneda return pa_offset + md->virt_addr + page - md->phys_addr; 84cefc7ca4SErik Kaneda } 85cefc7ca4SErik Kaneda 86cefc7ca4SErik Kaneda return 0; 87cefc7ca4SErik Kaneda } 88cefc7ca4SErik Kaneda 89cefc7ca4SErik Kaneda #define get_first_handler(a) ((struct acpi_prmt_handler_info *) ((char *) (a) + a->handler_info_offset)) 90cefc7ca4SErik Kaneda #define get_next_handler(a) ((struct acpi_prmt_handler_info *) (sizeof(struct acpi_prmt_handler_info) + (char *) a)) 91cefc7ca4SErik Kaneda 92cefc7ca4SErik Kaneda static int __init 93cefc7ca4SErik Kaneda acpi_parse_prmt(union acpi_subtable_headers *header, const unsigned long end) 94cefc7ca4SErik Kaneda { 95cefc7ca4SErik Kaneda struct acpi_prmt_module_info *module_info; 96cefc7ca4SErik Kaneda struct acpi_prmt_handler_info *handler_info; 97cefc7ca4SErik Kaneda struct prm_handler_info *th; 98cefc7ca4SErik Kaneda struct prm_module_info *tm; 99c52ca713SAubrey Li u64 *mmio_count; 100cefc7ca4SErik Kaneda u64 cur_handler = 0; 101cefc7ca4SErik Kaneda u32 module_info_size = 0; 102cefc7ca4SErik Kaneda u64 mmio_range_size = 0; 103cefc7ca4SErik Kaneda void *temp_mmio; 104cefc7ca4SErik Kaneda 105cefc7ca4SErik Kaneda module_info = (struct acpi_prmt_module_info *) header; 106cefc7ca4SErik Kaneda module_info_size = struct_size(tm, handlers, module_info->handler_info_count); 107cefc7ca4SErik Kaneda tm = kmalloc(module_info_size, GFP_KERNEL); 108c52ca713SAubrey Li if (!tm) 109c52ca713SAubrey Li goto parse_prmt_out1; 110cefc7ca4SErik Kaneda 111cefc7ca4SErik Kaneda guid_copy(&tm->guid, (guid_t *) module_info->module_guid); 112cefc7ca4SErik Kaneda tm->major_rev = module_info->major_rev; 113cefc7ca4SErik Kaneda tm->minor_rev = module_info->minor_rev; 114cefc7ca4SErik Kaneda tm->handler_count = module_info->handler_info_count; 115cefc7ca4SErik Kaneda tm->updatable = true; 116cefc7ca4SErik Kaneda 117cefc7ca4SErik Kaneda if (module_info->mmio_list_pointer) { 118cefc7ca4SErik Kaneda /* 119cefc7ca4SErik Kaneda * Each module is associated with a list of addr 120cefc7ca4SErik Kaneda * ranges that it can use during the service 121cefc7ca4SErik Kaneda */ 122c52ca713SAubrey Li mmio_count = (u64 *) memremap(module_info->mmio_list_pointer, 8, MEMREMAP_WB); 123c52ca713SAubrey Li if (!mmio_count) 124c52ca713SAubrey Li goto parse_prmt_out2; 125c52ca713SAubrey Li 126c52ca713SAubrey Li mmio_range_size = struct_size(tm->mmio_info, addr_ranges, *mmio_count); 127cefc7ca4SErik Kaneda tm->mmio_info = kmalloc(mmio_range_size, GFP_KERNEL); 128c52ca713SAubrey Li if (!tm->mmio_info) 129c52ca713SAubrey Li goto parse_prmt_out3; 130c52ca713SAubrey Li 131cefc7ca4SErik Kaneda temp_mmio = memremap(module_info->mmio_list_pointer, mmio_range_size, MEMREMAP_WB); 132c52ca713SAubrey Li if (!temp_mmio) 133c52ca713SAubrey Li goto parse_prmt_out4; 134cefc7ca4SErik Kaneda memmove(tm->mmio_info, temp_mmio, mmio_range_size); 135cefc7ca4SErik Kaneda } else { 136c52ca713SAubrey Li tm->mmio_info = kmalloc(sizeof(*tm->mmio_info), GFP_KERNEL); 137c52ca713SAubrey Li if (!tm->mmio_info) 138c52ca713SAubrey Li goto parse_prmt_out2; 139c52ca713SAubrey Li 140cefc7ca4SErik Kaneda tm->mmio_info->mmio_count = 0; 141cefc7ca4SErik Kaneda } 142cefc7ca4SErik Kaneda 143cefc7ca4SErik Kaneda INIT_LIST_HEAD(&tm->module_list); 144cefc7ca4SErik Kaneda list_add(&tm->module_list, &prm_module_list); 145cefc7ca4SErik Kaneda 146cefc7ca4SErik Kaneda handler_info = get_first_handler(module_info); 147cefc7ca4SErik Kaneda do { 148cefc7ca4SErik Kaneda th = &tm->handlers[cur_handler]; 149cefc7ca4SErik Kaneda 150cefc7ca4SErik Kaneda guid_copy(&th->guid, (guid_t *)handler_info->handler_guid); 151353efd5eSSudeep Holla th->handler_addr = (void *)efi_pa_va_lookup(handler_info->handler_address); 152cefc7ca4SErik Kaneda th->static_data_buffer_addr = efi_pa_va_lookup(handler_info->static_data_buffer_address); 153cefc7ca4SErik Kaneda th->acpi_param_buffer_addr = efi_pa_va_lookup(handler_info->acpi_param_buffer_address); 154cefc7ca4SErik Kaneda } while (++cur_handler < tm->handler_count && (handler_info = get_next_handler(handler_info))); 155cefc7ca4SErik Kaneda 156cefc7ca4SErik Kaneda return 0; 157c52ca713SAubrey Li 158c52ca713SAubrey Li parse_prmt_out4: 159c52ca713SAubrey Li kfree(tm->mmio_info); 160c52ca713SAubrey Li parse_prmt_out3: 161c52ca713SAubrey Li memunmap(mmio_count); 162c52ca713SAubrey Li parse_prmt_out2: 163c52ca713SAubrey Li kfree(tm); 164c52ca713SAubrey Li parse_prmt_out1: 165c52ca713SAubrey Li return -ENOMEM; 166cefc7ca4SErik Kaneda } 167cefc7ca4SErik Kaneda 168cefc7ca4SErik Kaneda #define GET_MODULE 0 169cefc7ca4SErik Kaneda #define GET_HANDLER 1 170cefc7ca4SErik Kaneda 171cefc7ca4SErik Kaneda static void *find_guid_info(const guid_t *guid, u8 mode) 172cefc7ca4SErik Kaneda { 173cefc7ca4SErik Kaneda struct prm_handler_info *cur_handler; 174cefc7ca4SErik Kaneda struct prm_module_info *cur_module; 175cefc7ca4SErik Kaneda int i = 0; 176cefc7ca4SErik Kaneda 177cefc7ca4SErik Kaneda list_for_each_entry(cur_module, &prm_module_list, module_list) { 178cefc7ca4SErik Kaneda for (i = 0; i < cur_module->handler_count; ++i) { 179cefc7ca4SErik Kaneda cur_handler = &cur_module->handlers[i]; 180cefc7ca4SErik Kaneda if (guid_equal(guid, &cur_handler->guid)) { 181cefc7ca4SErik Kaneda if (mode == GET_MODULE) 182cefc7ca4SErik Kaneda return (void *)cur_module; 183cefc7ca4SErik Kaneda else 184cefc7ca4SErik Kaneda return (void *)cur_handler; 185cefc7ca4SErik Kaneda } 186cefc7ca4SErik Kaneda } 187cefc7ca4SErik Kaneda } 188cefc7ca4SErik Kaneda 189cefc7ca4SErik Kaneda return NULL; 190cefc7ca4SErik Kaneda } 191cefc7ca4SErik Kaneda 192cefc7ca4SErik Kaneda static struct prm_module_info *find_prm_module(const guid_t *guid) 193cefc7ca4SErik Kaneda { 194cefc7ca4SErik Kaneda return (struct prm_module_info *)find_guid_info(guid, GET_MODULE); 195cefc7ca4SErik Kaneda } 196cefc7ca4SErik Kaneda 197cefc7ca4SErik Kaneda static struct prm_handler_info *find_prm_handler(const guid_t *guid) 198cefc7ca4SErik Kaneda { 199cefc7ca4SErik Kaneda return (struct prm_handler_info *) find_guid_info(guid, GET_HANDLER); 200cefc7ca4SErik Kaneda } 201cefc7ca4SErik Kaneda 202cefc7ca4SErik Kaneda /* In-coming PRM commands */ 203cefc7ca4SErik Kaneda 204cefc7ca4SErik Kaneda #define PRM_CMD_RUN_SERVICE 0 205cefc7ca4SErik Kaneda #define PRM_CMD_START_TRANSACTION 1 206cefc7ca4SErik Kaneda #define PRM_CMD_END_TRANSACTION 2 207cefc7ca4SErik Kaneda 208cefc7ca4SErik Kaneda /* statuses that can be passed back to ASL */ 209cefc7ca4SErik Kaneda 210cefc7ca4SErik Kaneda #define PRM_HANDLER_SUCCESS 0 211cefc7ca4SErik Kaneda #define PRM_HANDLER_ERROR 1 212cefc7ca4SErik Kaneda #define INVALID_PRM_COMMAND 2 213cefc7ca4SErik Kaneda #define PRM_HANDLER_GUID_NOT_FOUND 3 214cefc7ca4SErik Kaneda #define UPDATE_LOCK_ALREADY_HELD 4 215cefc7ca4SErik Kaneda #define UPDATE_UNLOCK_WITHOUT_LOCK 5 216cefc7ca4SErik Kaneda 217cefc7ca4SErik Kaneda /* 218cefc7ca4SErik Kaneda * This is the PlatformRtMechanism opregion space handler. 219cefc7ca4SErik Kaneda * @function: indicates the read/write. In fact as the PlatformRtMechanism 220cefc7ca4SErik Kaneda * message is driven by command, only write is meaningful. 221cefc7ca4SErik Kaneda * 222cefc7ca4SErik Kaneda * @addr : not used 223cefc7ca4SErik Kaneda * @bits : not used. 224cefc7ca4SErik Kaneda * @value : it is an in/out parameter. It points to the PRM message buffer. 225cefc7ca4SErik Kaneda * @handler_context: not used 226cefc7ca4SErik Kaneda */ 227cefc7ca4SErik Kaneda static acpi_status acpi_platformrt_space_handler(u32 function, 228cefc7ca4SErik Kaneda acpi_physical_address addr, 229cefc7ca4SErik Kaneda u32 bits, acpi_integer *value, 230cefc7ca4SErik Kaneda void *handler_context, 231cefc7ca4SErik Kaneda void *region_context) 232cefc7ca4SErik Kaneda { 233cefc7ca4SErik Kaneda struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value); 234cefc7ca4SErik Kaneda struct prm_handler_info *handler; 235cefc7ca4SErik Kaneda struct prm_module_info *module; 236cefc7ca4SErik Kaneda efi_status_t status; 237cefc7ca4SErik Kaneda struct prm_context_buffer context; 238cefc7ca4SErik Kaneda 239182da6f2SArd Biesheuvel if (!efi_enabled(EFI_RUNTIME_SERVICES)) { 240182da6f2SArd Biesheuvel pr_err_ratelimited("PRM: EFI runtime services no longer available\n"); 241182da6f2SArd Biesheuvel return AE_NO_HANDLER; 242182da6f2SArd Biesheuvel } 243182da6f2SArd Biesheuvel 244cefc7ca4SErik Kaneda /* 245cefc7ca4SErik Kaneda * The returned acpi_status will always be AE_OK. Error values will be 246cefc7ca4SErik Kaneda * saved in the first byte of the PRM message buffer to be used by ASL. 247cefc7ca4SErik Kaneda */ 248cefc7ca4SErik Kaneda switch (buffer->prm_cmd) { 249cefc7ca4SErik Kaneda case PRM_CMD_RUN_SERVICE: 250cefc7ca4SErik Kaneda 251cefc7ca4SErik Kaneda handler = find_prm_handler(&buffer->handler_guid); 252cefc7ca4SErik Kaneda module = find_prm_module(&buffer->handler_guid); 253cefc7ca4SErik Kaneda if (!handler || !module) 254cefc7ca4SErik Kaneda goto invalid_guid; 255cefc7ca4SErik Kaneda 256cefc7ca4SErik Kaneda ACPI_COPY_NAMESEG(context.signature, "PRMC"); 257cefc7ca4SErik Kaneda context.revision = 0x0; 258cefc7ca4SErik Kaneda context.reserved = 0x0; 259cefc7ca4SErik Kaneda context.identifier = handler->guid; 260cefc7ca4SErik Kaneda context.static_data_buffer = handler->static_data_buffer_addr; 261cefc7ca4SErik Kaneda context.mmio_ranges = module->mmio_info; 262cefc7ca4SErik Kaneda 2635894cf57SArd Biesheuvel status = efi_call_acpi_prm_handler(handler->handler_addr, 264cefc7ca4SErik Kaneda handler->acpi_param_buffer_addr, 265cefc7ca4SErik Kaneda &context); 266cefc7ca4SErik Kaneda if (status == EFI_SUCCESS) { 267cefc7ca4SErik Kaneda buffer->prm_status = PRM_HANDLER_SUCCESS; 268cefc7ca4SErik Kaneda } else { 269cefc7ca4SErik Kaneda buffer->prm_status = PRM_HANDLER_ERROR; 270cefc7ca4SErik Kaneda buffer->efi_status = status; 271cefc7ca4SErik Kaneda } 272cefc7ca4SErik Kaneda break; 273cefc7ca4SErik Kaneda 274cefc7ca4SErik Kaneda case PRM_CMD_START_TRANSACTION: 275cefc7ca4SErik Kaneda 276cefc7ca4SErik Kaneda module = find_prm_module(&buffer->handler_guid); 277cefc7ca4SErik Kaneda if (!module) 278cefc7ca4SErik Kaneda goto invalid_guid; 279cefc7ca4SErik Kaneda 280cefc7ca4SErik Kaneda if (module->updatable) 281cefc7ca4SErik Kaneda module->updatable = false; 282cefc7ca4SErik Kaneda else 283cefc7ca4SErik Kaneda buffer->prm_status = UPDATE_LOCK_ALREADY_HELD; 284cefc7ca4SErik Kaneda break; 285cefc7ca4SErik Kaneda 286cefc7ca4SErik Kaneda case PRM_CMD_END_TRANSACTION: 287cefc7ca4SErik Kaneda 288cefc7ca4SErik Kaneda module = find_prm_module(&buffer->handler_guid); 289cefc7ca4SErik Kaneda if (!module) 290cefc7ca4SErik Kaneda goto invalid_guid; 291cefc7ca4SErik Kaneda 292cefc7ca4SErik Kaneda if (module->updatable) 293cefc7ca4SErik Kaneda buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK; 294cefc7ca4SErik Kaneda else 295cefc7ca4SErik Kaneda module->updatable = true; 296cefc7ca4SErik Kaneda break; 297cefc7ca4SErik Kaneda 298cefc7ca4SErik Kaneda default: 299cefc7ca4SErik Kaneda 300cefc7ca4SErik Kaneda buffer->prm_status = INVALID_PRM_COMMAND; 301cefc7ca4SErik Kaneda break; 302cefc7ca4SErik Kaneda } 303cefc7ca4SErik Kaneda 304cefc7ca4SErik Kaneda return AE_OK; 305cefc7ca4SErik Kaneda 306cefc7ca4SErik Kaneda invalid_guid: 307cefc7ca4SErik Kaneda buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND; 308cefc7ca4SErik Kaneda return AE_OK; 309cefc7ca4SErik Kaneda } 310cefc7ca4SErik Kaneda 311cefc7ca4SErik Kaneda void __init init_prmt(void) 312cefc7ca4SErik Kaneda { 3133265cc3eSAubrey Li struct acpi_table_header *tbl; 314cefc7ca4SErik Kaneda acpi_status status; 3153265cc3eSAubrey Li int mc; 3163265cc3eSAubrey Li 3173265cc3eSAubrey Li status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl); 3183265cc3eSAubrey Li if (ACPI_FAILURE(status)) 3193265cc3eSAubrey Li return; 3203265cc3eSAubrey Li 3213265cc3eSAubrey Li mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) + 322cefc7ca4SErik Kaneda sizeof (struct acpi_table_prmt_header), 323cefc7ca4SErik Kaneda 0, acpi_parse_prmt, 0); 3243265cc3eSAubrey Li acpi_put_table(tbl); 3252bbfa0adSAubrey Li /* 3262bbfa0adSAubrey Li * Return immediately if PRMT table is not present or no PRM module found. 3272bbfa0adSAubrey Li */ 3282bbfa0adSAubrey Li if (mc <= 0) 3292bbfa0adSAubrey Li return; 3302bbfa0adSAubrey Li 331cefc7ca4SErik Kaneda pr_info("PRM: found %u modules\n", mc); 332cefc7ca4SErik Kaneda 333182da6f2SArd Biesheuvel if (!efi_enabled(EFI_RUNTIME_SERVICES)) { 334182da6f2SArd Biesheuvel pr_err("PRM: EFI runtime services unavailable\n"); 335182da6f2SArd Biesheuvel return; 336182da6f2SArd Biesheuvel } 337182da6f2SArd Biesheuvel 338cefc7ca4SErik Kaneda status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT, 339cefc7ca4SErik Kaneda ACPI_ADR_SPACE_PLATFORM_RT, 340cefc7ca4SErik Kaneda &acpi_platformrt_space_handler, 341cefc7ca4SErik Kaneda NULL, NULL); 342cefc7ca4SErik Kaneda if (ACPI_FAILURE(status)) 343cefc7ca4SErik Kaneda pr_alert("PRM: OperationRegion handler could not be installed\n"); 344cefc7ca4SErik Kaneda } 345