Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Author: Erik Kaneda <erik.kaneda@intel.com>
0004  * Copyright 2020 Intel Corporation
0005  *
0006  * prmt.c
0007  *
0008  * Each PRM service is an executable that is run in a restricted environment
0009  * that is invoked by writing to the PlatformRtMechanism OperationRegion from
0010  * AML bytecode.
0011  *
0012  * init_prmt initializes the Platform Runtime Mechanism (PRM) services by
0013  * processing data in the PRMT as well as registering an ACPI OperationRegion
0014  * handler for the PlatformRtMechanism subtype.
0015  *
0016  */
0017 #include <linux/kernel.h>
0018 #include <linux/efi.h>
0019 #include <linux/acpi.h>
0020 #include <linux/prmt.h>
0021 #include <asm/efi.h>
0022 
0023 #pragma pack(1)
0024 struct prm_mmio_addr_range {
0025     u64 phys_addr;
0026     u64 virt_addr;
0027     u32 length;
0028 };
0029 
0030 struct prm_mmio_info {
0031     u64 mmio_count;
0032     struct prm_mmio_addr_range addr_ranges[];
0033 };
0034 
0035 struct prm_buffer {
0036     u8 prm_status;
0037     u64 efi_status;
0038     u8 prm_cmd;
0039     guid_t handler_guid;
0040 };
0041 
0042 struct prm_context_buffer {
0043     char signature[ACPI_NAMESEG_SIZE];
0044     u16 revision;
0045     u16 reserved;
0046     guid_t identifier;
0047     u64 static_data_buffer;
0048     struct prm_mmio_info *mmio_ranges;
0049 };
0050 #pragma pack()
0051 
0052 static LIST_HEAD(prm_module_list);
0053 
0054 struct prm_handler_info {
0055     guid_t guid;
0056     void *handler_addr;
0057     u64 static_data_buffer_addr;
0058     u64 acpi_param_buffer_addr;
0059 
0060     struct list_head handler_list;
0061 };
0062 
0063 struct prm_module_info {
0064     guid_t guid;
0065     u16 major_rev;
0066     u16 minor_rev;
0067     u16 handler_count;
0068     struct prm_mmio_info *mmio_info;
0069     bool updatable;
0070 
0071     struct list_head module_list;
0072     struct prm_handler_info handlers[];
0073 };
0074 
0075 static u64 efi_pa_va_lookup(u64 pa)
0076 {
0077     efi_memory_desc_t *md;
0078     u64 pa_offset = pa & ~PAGE_MASK;
0079     u64 page = pa & PAGE_MASK;
0080 
0081     for_each_efi_memory_desc(md) {
0082         if (md->phys_addr < pa && pa < md->phys_addr + PAGE_SIZE * md->num_pages)
0083             return pa_offset + md->virt_addr + page - md->phys_addr;
0084     }
0085 
0086     return 0;
0087 }
0088 
0089 #define get_first_handler(a) ((struct acpi_prmt_handler_info *) ((char *) (a) + a->handler_info_offset))
0090 #define get_next_handler(a) ((struct acpi_prmt_handler_info *) (sizeof(struct acpi_prmt_handler_info) + (char *) a))
0091 
0092 static int __init
0093 acpi_parse_prmt(union acpi_subtable_headers *header, const unsigned long end)
0094 {
0095     struct acpi_prmt_module_info *module_info;
0096     struct acpi_prmt_handler_info *handler_info;
0097     struct prm_handler_info *th;
0098     struct prm_module_info *tm;
0099     u64 *mmio_count;
0100     u64 cur_handler = 0;
0101     u32 module_info_size = 0;
0102     u64 mmio_range_size = 0;
0103     void *temp_mmio;
0104 
0105     module_info = (struct acpi_prmt_module_info *) header;
0106     module_info_size = struct_size(tm, handlers, module_info->handler_info_count);
0107     tm = kmalloc(module_info_size, GFP_KERNEL);
0108     if (!tm)
0109         goto parse_prmt_out1;
0110 
0111     guid_copy(&tm->guid, (guid_t *) module_info->module_guid);
0112     tm->major_rev = module_info->major_rev;
0113     tm->minor_rev = module_info->minor_rev;
0114     tm->handler_count = module_info->handler_info_count;
0115     tm->updatable = true;
0116 
0117     if (module_info->mmio_list_pointer) {
0118         /*
0119          * Each module is associated with a list of addr
0120          * ranges that it can use during the service
0121          */
0122         mmio_count = (u64 *) memremap(module_info->mmio_list_pointer, 8, MEMREMAP_WB);
0123         if (!mmio_count)
0124             goto parse_prmt_out2;
0125 
0126         mmio_range_size = struct_size(tm->mmio_info, addr_ranges, *mmio_count);
0127         tm->mmio_info = kmalloc(mmio_range_size, GFP_KERNEL);
0128         if (!tm->mmio_info)
0129             goto parse_prmt_out3;
0130 
0131         temp_mmio = memremap(module_info->mmio_list_pointer, mmio_range_size, MEMREMAP_WB);
0132         if (!temp_mmio)
0133             goto parse_prmt_out4;
0134         memmove(tm->mmio_info, temp_mmio, mmio_range_size);
0135     } else {
0136         tm->mmio_info = kmalloc(sizeof(*tm->mmio_info), GFP_KERNEL);
0137         if (!tm->mmio_info)
0138             goto parse_prmt_out2;
0139 
0140         tm->mmio_info->mmio_count = 0;
0141     }
0142 
0143     INIT_LIST_HEAD(&tm->module_list);
0144     list_add(&tm->module_list, &prm_module_list);
0145 
0146     handler_info = get_first_handler(module_info);
0147     do {
0148         th = &tm->handlers[cur_handler];
0149 
0150         guid_copy(&th->guid, (guid_t *)handler_info->handler_guid);
0151         th->handler_addr = (void *)efi_pa_va_lookup(handler_info->handler_address);
0152         th->static_data_buffer_addr = efi_pa_va_lookup(handler_info->static_data_buffer_address);
0153         th->acpi_param_buffer_addr = efi_pa_va_lookup(handler_info->acpi_param_buffer_address);
0154     } while (++cur_handler < tm->handler_count && (handler_info = get_next_handler(handler_info)));
0155 
0156     return 0;
0157 
0158 parse_prmt_out4:
0159     kfree(tm->mmio_info);
0160 parse_prmt_out3:
0161     memunmap(mmio_count);
0162 parse_prmt_out2:
0163     kfree(tm);
0164 parse_prmt_out1:
0165     return -ENOMEM;
0166 }
0167 
0168 #define GET_MODULE  0
0169 #define GET_HANDLER 1
0170 
0171 static void *find_guid_info(const guid_t *guid, u8 mode)
0172 {
0173     struct prm_handler_info *cur_handler;
0174     struct prm_module_info *cur_module;
0175     int i = 0;
0176 
0177     list_for_each_entry(cur_module, &prm_module_list, module_list) {
0178         for (i = 0; i < cur_module->handler_count; ++i) {
0179             cur_handler = &cur_module->handlers[i];
0180             if (guid_equal(guid, &cur_handler->guid)) {
0181                 if (mode == GET_MODULE)
0182                     return (void *)cur_module;
0183                 else
0184                     return (void *)cur_handler;
0185             }
0186         }
0187     }
0188 
0189     return NULL;
0190 }
0191 
0192 static struct prm_module_info *find_prm_module(const guid_t *guid)
0193 {
0194     return (struct prm_module_info *)find_guid_info(guid, GET_MODULE);
0195 }
0196 
0197 static struct prm_handler_info *find_prm_handler(const guid_t *guid)
0198 {
0199     return (struct prm_handler_info *) find_guid_info(guid, GET_HANDLER);
0200 }
0201 
0202 /* In-coming PRM commands */
0203 
0204 #define PRM_CMD_RUN_SERVICE     0
0205 #define PRM_CMD_START_TRANSACTION   1
0206 #define PRM_CMD_END_TRANSACTION     2
0207 
0208 /* statuses that can be passed back to ASL */
0209 
0210 #define PRM_HANDLER_SUCCESS         0
0211 #define PRM_HANDLER_ERROR       1
0212 #define INVALID_PRM_COMMAND         2
0213 #define PRM_HANDLER_GUID_NOT_FOUND  3
0214 #define UPDATE_LOCK_ALREADY_HELD    4
0215 #define UPDATE_UNLOCK_WITHOUT_LOCK  5
0216 
0217 /*
0218  * This is the PlatformRtMechanism opregion space handler.
0219  * @function: indicates the read/write. In fact as the PlatformRtMechanism
0220  * message is driven by command, only write is meaningful.
0221  *
0222  * @addr   : not used
0223  * @bits   : not used.
0224  * @value  : it is an in/out parameter. It points to the PRM message buffer.
0225  * @handler_context: not used
0226  */
0227 static acpi_status acpi_platformrt_space_handler(u32 function,
0228                          acpi_physical_address addr,
0229                          u32 bits, acpi_integer *value,
0230                          void *handler_context,
0231                          void *region_context)
0232 {
0233     struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value);
0234     struct prm_handler_info *handler;
0235     struct prm_module_info *module;
0236     efi_status_t status;
0237     struct prm_context_buffer context;
0238 
0239     /*
0240      * The returned acpi_status will always be AE_OK. Error values will be
0241      * saved in the first byte of the PRM message buffer to be used by ASL.
0242      */
0243     switch (buffer->prm_cmd) {
0244     case PRM_CMD_RUN_SERVICE:
0245 
0246         handler = find_prm_handler(&buffer->handler_guid);
0247         module = find_prm_module(&buffer->handler_guid);
0248         if (!handler || !module)
0249             goto invalid_guid;
0250 
0251         ACPI_COPY_NAMESEG(context.signature, "PRMC");
0252         context.revision = 0x0;
0253         context.reserved = 0x0;
0254         context.identifier = handler->guid;
0255         context.static_data_buffer = handler->static_data_buffer_addr;
0256         context.mmio_ranges = module->mmio_info;
0257 
0258         status = efi_call_virt_pointer(handler, handler_addr,
0259                            handler->acpi_param_buffer_addr,
0260                            &context);
0261         if (status == EFI_SUCCESS) {
0262             buffer->prm_status = PRM_HANDLER_SUCCESS;
0263         } else {
0264             buffer->prm_status = PRM_HANDLER_ERROR;
0265             buffer->efi_status = status;
0266         }
0267         break;
0268 
0269     case PRM_CMD_START_TRANSACTION:
0270 
0271         module = find_prm_module(&buffer->handler_guid);
0272         if (!module)
0273             goto invalid_guid;
0274 
0275         if (module->updatable)
0276             module->updatable = false;
0277         else
0278             buffer->prm_status = UPDATE_LOCK_ALREADY_HELD;
0279         break;
0280 
0281     case PRM_CMD_END_TRANSACTION:
0282 
0283         module = find_prm_module(&buffer->handler_guid);
0284         if (!module)
0285             goto invalid_guid;
0286 
0287         if (module->updatable)
0288             buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK;
0289         else
0290             module->updatable = true;
0291         break;
0292 
0293     default:
0294 
0295         buffer->prm_status = INVALID_PRM_COMMAND;
0296         break;
0297     }
0298 
0299     return AE_OK;
0300 
0301 invalid_guid:
0302     buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND;
0303     return AE_OK;
0304 }
0305 
0306 void __init init_prmt(void)
0307 {
0308     struct acpi_table_header *tbl;
0309     acpi_status status;
0310     int mc;
0311 
0312     status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl);
0313     if (ACPI_FAILURE(status))
0314         return;
0315 
0316     mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) +
0317                       sizeof (struct acpi_table_prmt_header),
0318                       0, acpi_parse_prmt, 0);
0319     acpi_put_table(tbl);
0320     /*
0321      * Return immediately if PRMT table is not present or no PRM module found.
0322      */
0323     if (mc <= 0)
0324         return;
0325 
0326     pr_info("PRM: found %u modules\n", mc);
0327 
0328     status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
0329                             ACPI_ADR_SPACE_PLATFORM_RT,
0330                             &acpi_platformrt_space_handler,
0331                             NULL, NULL);
0332     if (ACPI_FAILURE(status))
0333         pr_alert("PRM: OperationRegion handler could not be installed\n");
0334 }