| 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 |
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_obj(*tm->mmio_info); |
| 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 |
bool acpi_prm_handler_available(const guid_t *guid) |
| 248 |
{ |
| 249 |
return find_prm_handler(guid) && find_prm_module(guid); |
| 250 |
} |
| 251 |
EXPORT_SYMBOL_GPL(acpi_prm_handler_available); |
| 252 |
|
| 253 |
/* In-coming PRM commands */ |
| 254 |
|
| 255 |
#define PRM_CMD_RUN_SERVICE 0 |
| 256 |
#define PRM_CMD_START_TRANSACTION 1 |
| 257 |
#define PRM_CMD_END_TRANSACTION 2 |
| 258 |
|
| 259 |
/* statuses that can be passed back to ASL */ |
| 260 |
|
| 261 |
#define PRM_HANDLER_SUCCESS 0 |
| 262 |
#define PRM_HANDLER_ERROR 1 |
| 263 |
#define INVALID_PRM_COMMAND 2 |
| 264 |
#define PRM_HANDLER_GUID_NOT_FOUND 3 |
| 265 |
#define UPDATE_LOCK_ALREADY_HELD 4 |
| 266 |
#define UPDATE_UNLOCK_WITHOUT_LOCK 5 |
| 267 |
|
| 268 |
int acpi_call_prm_handler(guid_t handler_guid, void *param_buffer) |
| 269 |
{ |
| 270 |
struct prm_handler_info *handler = find_prm_handler(&handler_guid); |
| 271 |
struct prm_module_info *module = find_prm_module(&handler_guid); |
| 272 |
struct prm_context_buffer context; |
| 273 |
efi_status_t status; |
| 274 |
|
| 275 |
if (!module || !handler) |
| 276 |
return -ENODEV; |
| 277 |
|
| 278 |
memset(&context, 0, sizeof(context)); |
| 279 |
ACPI_COPY_NAMESEG(context.signature, "PRMC"); |
| 280 |
context.identifier = handler->guid; |
| 281 |
context.static_data_buffer = handler->static_data_buffer_addr; |
| 282 |
context.mmio_ranges = module->mmio_info; |
| 283 |
|
| 284 |
status = efi_call_acpi_prm_handler(handler->handler_addr, |
| 285 |
(u64)param_buffer, |
| 286 |
&context); |
| 287 |
|
| 288 |
return efi_status_to_err(status); |
| 289 |
} |
| 290 |
EXPORT_SYMBOL_GPL(acpi_call_prm_handler); |
| 291 |
|
| 292 |
/* |
| 293 |
* This is the PlatformRtMechanism opregion space handler. |
| 294 |
* @function: indicates the read/write. In fact as the PlatformRtMechanism |
| 295 |
* message is driven by command, only write is meaningful. |
| 296 |
* |
| 297 |
* @addr : not used |
| 298 |
* @bits : not used. |
| 299 |
* @value : it is an in/out parameter. It points to the PRM message buffer. |
| 300 |
* @handler_context: not used |
| 301 |
*/ |
| 302 |
static acpi_status acpi_platformrt_space_handler(u32 function, |
| 303 |
acpi_physical_address addr, |
| 304 |
u32 bits, acpi_integer *value, |
| 305 |
void *handler_context, |
| 306 |
void *region_context) |
| 307 |
{ |
| 308 |
struct prm_buffer *buffer = ACPI_CAST_PTR(struct prm_buffer, value); |
| 309 |
struct prm_handler_info *handler; |
| 310 |
struct prm_module_info *module; |
| 311 |
efi_status_t status; |
| 312 |
struct prm_context_buffer context; |
| 313 |
|
| 314 |
if (!efi_enabled(EFI_RUNTIME_SERVICES)) { |
| 315 |
pr_err_ratelimited("PRM: EFI runtime services no longer available\n"); |
| 316 |
return AE_NO_HANDLER; |
| 317 |
} |
| 318 |
|
| 319 |
/* |
| 320 |
* The returned acpi_status will always be AE_OK. Error values will be |
| 321 |
* saved in the first byte of the PRM message buffer to be used by ASL. |
| 322 |
*/ |
| 323 |
switch (buffer->prm_cmd) { |
| 324 |
case PRM_CMD_RUN_SERVICE: |
| 325 |
|
| 326 |
handler = find_prm_handler(&buffer->handler_guid); |
| 327 |
module = find_prm_module(&buffer->handler_guid); |
| 328 |
if (!handler || !module) |
| 329 |
goto invalid_guid; |
| 330 |
|
| 331 |
if (!handler->handler_addr) { |
| 332 |
buffer->prm_status = PRM_HANDLER_ERROR; |
| 333 |
return AE_OK; |
| 334 |
} |
| 335 |
|
| 336 |
ACPI_COPY_NAMESEG(context.signature, "PRMC"); |
| 337 |
context.revision = 0x0; |
| 338 |
context.reserved = 0x0; |
| 339 |
context.identifier = handler->guid; |
| 340 |
context.static_data_buffer = handler->static_data_buffer_addr; |
| 341 |
context.mmio_ranges = module->mmio_info; |
| 342 |
|
| 343 |
status = efi_call_acpi_prm_handler(handler->handler_addr, |
| 344 |
handler->acpi_param_buffer_addr, |
| 345 |
&context); |
| 346 |
if (status == EFI_SUCCESS) { |
| 347 |
buffer->prm_status = PRM_HANDLER_SUCCESS; |
| 348 |
} else { |
| 349 |
buffer->prm_status = PRM_HANDLER_ERROR; |
| 350 |
buffer->efi_status = status; |
| 351 |
} |
| 352 |
break; |
| 353 |
|
| 354 |
case PRM_CMD_START_TRANSACTION: |
| 355 |
|
| 356 |
module = find_prm_module(&buffer->handler_guid); |
| 357 |
if (!module) |
| 358 |
goto invalid_guid; |
| 359 |
|
| 360 |
if (module->updatable) |
| 361 |
module->updatable = false; |
| 362 |
else |
| 363 |
buffer->prm_status = UPDATE_LOCK_ALREADY_HELD; |
| 364 |
break; |
| 365 |
|
| 366 |
case PRM_CMD_END_TRANSACTION: |
| 367 |
|
| 368 |
module = find_prm_module(&buffer->handler_guid); |
| 369 |
if (!module) |
| 370 |
goto invalid_guid; |
| 371 |
|
| 372 |
if (module->updatable) |
| 373 |
buffer->prm_status = UPDATE_UNLOCK_WITHOUT_LOCK; |
| 374 |
else |
| 375 |
module->updatable = true; |
| 376 |
break; |
| 377 |
|
| 378 |
default: |
| 379 |
|
| 380 |
buffer->prm_status = INVALID_PRM_COMMAND; |
| 381 |
break; |
| 382 |
} |
| 383 |
|
| 384 |
return AE_OK; |
| 385 |
|
| 386 |
invalid_guid: |
| 387 |
buffer->prm_status = PRM_HANDLER_GUID_NOT_FOUND; |
| 388 |
return AE_OK; |
| 389 |
} |
| 390 |
|
| 391 |
void __init init_prmt(void) |
| 392 |
{ |
| 393 |
struct acpi_table_header *tbl; |
| 394 |
acpi_status status; |
| 395 |
int mc; |
| 396 |
|
| 397 |
status = acpi_get_table(ACPI_SIG_PRMT, 0, &tbl); |
| 398 |
if (ACPI_FAILURE(status)) |
| 399 |
return; |
| 400 |
|
| 401 |
mc = acpi_table_parse_entries(ACPI_SIG_PRMT, sizeof(struct acpi_table_prmt) + |
| 402 |
sizeof (struct acpi_table_prmt_header), |
| 403 |
0, acpi_parse_prmt, 0); |
| 404 |
acpi_put_table(tbl); |
| 405 |
/* |
| 406 |
* Return immediately if PRMT table is not present or no PRM module found. |
| 407 |
*/ |
| 408 |
if (mc <= 0) |
| 409 |
return; |
| 410 |
|
| 411 |
pr_info("PRM: found %u modules\n", mc); |
| 412 |
|
| 413 |
if (!efi_enabled(EFI_RUNTIME_SERVICES)) { |
| 414 |
pr_err("PRM: EFI runtime services unavailable\n"); |
| 415 |
return; |
| 416 |
} |
| 417 |
|
| 418 |
status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT, |
| 419 |
ACPI_ADR_SPACE_PLATFORM_RT, |
| 420 |
&acpi_platformrt_space_handler, |
| 421 |
NULL, NULL); |
| 422 |
if (ACPI_FAILURE(status)) |
| 423 |
pr_alert("PRM: OperationRegion handler could not be installed\n"); |
| 424 |
} |