1 // SPDX-License-Identifier: GPL-2.0-only 2 /* Copyright (c) 2010,2015,2019 The Linux Foundation. All rights reserved. 3 * Copyright (C) 2015 Linaro Ltd. 4 */ 5 6 #include <linux/arm-smccc.h> 7 #include <linux/bitfield.h> 8 #include <linux/bits.h> 9 #include <linux/cleanup.h> 10 #include <linux/clk.h> 11 #include <linux/completion.h> 12 #include <linux/cpumask.h> 13 #include <linux/dma-mapping.h> 14 #include <linux/err.h> 15 #include <linux/export.h> 16 #include <linux/firmware/qcom/qcom_pas.h> 17 #include <linux/firmware/qcom/qcom_scm.h> 18 #include <linux/firmware/qcom/qcom_tzmem.h> 19 #include <linux/init.h> 20 #include <linux/interconnect.h> 21 #include <linux/interrupt.h> 22 #include <linux/kstrtox.h> 23 #include <linux/module.h> 24 #include <linux/of.h> 25 #include <linux/of_address.h> 26 #include <linux/of_irq.h> 27 #include <linux/of_platform.h> 28 #include <linux/of_reserved_mem.h> 29 #include <linux/platform_device.h> 30 #include <linux/reset-controller.h> 31 #include <linux/remoteproc.h> 32 #include <linux/sizes.h> 33 #include <linux/types.h> 34 35 #include <dt-bindings/interrupt-controller/arm-gic.h> 36 37 #include "qcom_pas.h" 38 #include "qcom_scm.h" 39 #include "qcom_tzmem.h" 40 41 static u32 download_mode; 42 43 #define GIC_SPI_BASE 32 44 #define GIC_MAX_SPI 1019 // SPIs in GICv3 spec range from 32..1019 45 #define GIC_ESPI_BASE 4096 46 #define GIC_MAX_ESPI 5119 // ESPIs in GICv3 spec range from 4096..5119 47 48 struct qcom_scm { 49 struct device *dev; 50 struct clk *core_clk; 51 struct clk *iface_clk; 52 struct clk *bus_clk; 53 struct icc_path *path; 54 struct completion *waitq_comps; 55 struct reset_controller_dev reset; 56 57 /* control access to the interconnect path */ 58 struct mutex scm_bw_lock; 59 int scm_vote_count; 60 61 u64 dload_mode_addr; 62 void __iomem *minidump_sram; 63 64 struct qcom_tzmem_pool *mempool; 65 unsigned int wq_cnt; 66 }; 67 68 struct qcom_scm_current_perm_info { 69 __le32 vmid; 70 __le32 perm; 71 __le64 ctx; 72 __le32 ctx_size; 73 __le32 unused; 74 }; 75 76 struct qcom_scm_mem_map_info { 77 __le64 mem_addr; 78 __le64 mem_size; 79 }; 80 81 /** 82 * struct qcom_scm_qseecom_resp - QSEECOM SCM call response. 83 * @result: Result or status of the SCM call. See &enum qcom_scm_qseecom_result. 84 * @resp_type: Type of the response. See &enum qcom_scm_qseecom_resp_type. 85 * @data: Response data. The type of this data is given in @resp_type. 86 */ 87 struct qcom_scm_qseecom_resp { 88 u64 result; 89 u64 resp_type; 90 u64 data; 91 }; 92 93 enum qcom_scm_qseecom_result { 94 QSEECOM_RESULT_SUCCESS = 0, 95 QSEECOM_RESULT_INCOMPLETE = 1, 96 QSEECOM_RESULT_BLOCKED_ON_LISTENER = 2, 97 QSEECOM_RESULT_FAILURE = 0xFFFFFFFF, 98 }; 99 100 enum qcom_scm_qseecom_resp_type { 101 QSEECOM_SCM_RES_APP_ID = 0xEE01, 102 QSEECOM_SCM_RES_QSEOS_LISTENER_ID = 0xEE02, 103 }; 104 105 enum qcom_scm_qseecom_tz_owner { 106 QSEECOM_TZ_OWNER_SIP = 2, 107 QSEECOM_TZ_OWNER_TZ_APPS = 48, 108 QSEECOM_TZ_OWNER_QSEE_OS = 50 109 }; 110 111 enum qcom_scm_qseecom_tz_svc { 112 QSEECOM_TZ_SVC_APP_ID_PLACEHOLDER = 0, 113 QSEECOM_TZ_SVC_APP_MGR = 1, 114 QSEECOM_TZ_SVC_INFO = 6, 115 }; 116 117 enum qcom_scm_qseecom_tz_cmd_app { 118 QSEECOM_TZ_CMD_APP_SEND = 1, 119 QSEECOM_TZ_CMD_APP_LOOKUP = 3, 120 }; 121 122 enum qcom_scm_qseecom_tz_cmd_info { 123 QSEECOM_TZ_CMD_INFO_VERSION = 3, 124 }; 125 126 #define RSCTABLE_BUFFER_NOT_SUFFICIENT 20 127 128 #define QSEECOM_MAX_APP_NAME_SIZE 64 129 #define SHMBRIDGE_RESULT_NOTSUPP 4 130 131 /* Each bit configures cold/warm boot address for one of the 4 CPUs */ 132 static const u8 qcom_scm_cpu_cold_bits[QCOM_SCM_BOOT_MAX_CPUS] = { 133 0, BIT(0), BIT(3), BIT(5) 134 }; 135 static const u8 qcom_scm_cpu_warm_bits[QCOM_SCM_BOOT_MAX_CPUS] = { 136 BIT(2), BIT(1), BIT(4), BIT(6) 137 }; 138 139 #define QCOM_SMC_WAITQ_FLAG_WAKE_ONE BIT(0) 140 141 #define QCOM_DLOAD_MASK GENMASK(5, 4) 142 #define QCOM_DLOAD_NODUMP 0 143 #define QCOM_DLOAD_FULLDUMP 1 144 #define QCOM_DLOAD_MINIDUMP 2 145 #define QCOM_DLOAD_BOTHDUMP 3 146 147 /* Minidump destination values written to always-on SRAM for boot firmware */ 148 #define QCOM_MINIDUMP_DEST_USB 0x0 149 #define QCOM_MINIDUMP_DEST_STORAGE 0x2 150 151 static u32 minidump_dest = QCOM_MINIDUMP_DEST_USB; 152 153 static const struct { 154 const char *name; 155 u32 val; 156 } minidump_dest_map[] = { 157 { "usb", QCOM_MINIDUMP_DEST_USB }, 158 { "storage", QCOM_MINIDUMP_DEST_STORAGE }, 159 }; 160 161 #define QCOM_SCM_DEFAULT_WAITQ_COUNT 1 162 163 static const char * const qcom_scm_convention_names[] = { 164 [SMC_CONVENTION_UNKNOWN] = "unknown", 165 [SMC_CONVENTION_ARM_32] = "smc arm 32", 166 [SMC_CONVENTION_ARM_64] = "smc arm 64", 167 [SMC_CONVENTION_LEGACY] = "smc legacy", 168 }; 169 170 static const char * const download_mode_name[] = { 171 [QCOM_DLOAD_NODUMP] = "off", 172 [QCOM_DLOAD_FULLDUMP] = "full", 173 [QCOM_DLOAD_MINIDUMP] = "mini", 174 [QCOM_DLOAD_BOTHDUMP] = "full,mini", 175 }; 176 177 static struct qcom_scm *__scm; 178 179 static int qcom_scm_clk_enable(void) 180 { 181 int ret; 182 183 ret = clk_prepare_enable(__scm->core_clk); 184 if (ret) 185 goto bail; 186 187 ret = clk_prepare_enable(__scm->iface_clk); 188 if (ret) 189 goto disable_core; 190 191 ret = clk_prepare_enable(__scm->bus_clk); 192 if (ret) 193 goto disable_iface; 194 195 return 0; 196 197 disable_iface: 198 clk_disable_unprepare(__scm->iface_clk); 199 disable_core: 200 clk_disable_unprepare(__scm->core_clk); 201 bail: 202 return ret; 203 } 204 205 static void qcom_scm_clk_disable(void) 206 { 207 clk_disable_unprepare(__scm->core_clk); 208 clk_disable_unprepare(__scm->iface_clk); 209 clk_disable_unprepare(__scm->bus_clk); 210 } 211 212 static int qcom_scm_bw_enable(void) 213 { 214 int ret = 0; 215 216 if (!__scm->path) 217 return 0; 218 219 guard(mutex)(&__scm->scm_bw_lock); 220 221 if (!__scm->scm_vote_count) { 222 ret = icc_set_bw(__scm->path, 0, UINT_MAX); 223 if (ret < 0) { 224 dev_err(__scm->dev, "failed to set bandwidth request\n"); 225 return ret; 226 } 227 } 228 __scm->scm_vote_count++; 229 230 return 0; 231 } 232 233 static void qcom_scm_bw_disable(void) 234 { 235 if (!__scm->path) 236 return; 237 238 mutex_lock(&__scm->scm_bw_lock); 239 if (__scm->scm_vote_count-- == 1) 240 icc_set_bw(__scm->path, 0, 0); 241 mutex_unlock(&__scm->scm_bw_lock); 242 } 243 244 enum qcom_scm_convention qcom_scm_convention = SMC_CONVENTION_UNKNOWN; 245 static DEFINE_SPINLOCK(scm_query_lock); 246 247 struct qcom_tzmem_pool *qcom_scm_get_tzmem_pool(void) 248 { 249 if (!qcom_scm_is_available()) 250 return NULL; 251 252 return __scm->mempool; 253 } 254 255 static enum qcom_scm_convention __get_convention(void) 256 { 257 unsigned long flags; 258 struct qcom_scm_desc desc = { 259 .svc = QCOM_SCM_SVC_INFO, 260 .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL, 261 .args[0] = SCM_SMC_FNID(QCOM_SCM_SVC_INFO, 262 QCOM_SCM_INFO_IS_CALL_AVAIL) | 263 (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT), 264 .arginfo = QCOM_SCM_ARGS(1), 265 .owner = ARM_SMCCC_OWNER_SIP, 266 }; 267 struct qcom_scm_res res; 268 enum qcom_scm_convention probed_convention; 269 int ret; 270 bool forced = false; 271 272 if (likely(qcom_scm_convention != SMC_CONVENTION_UNKNOWN)) 273 return qcom_scm_convention; 274 275 /* 276 * Per the "SMC calling convention specification", the 64-bit calling 277 * convention can only be used when the client is 64-bit, otherwise 278 * system will encounter the undefined behaviour. 279 */ 280 #if IS_ENABLED(CONFIG_ARM64) 281 /* 282 * Device isn't required as there is only one argument - no device 283 * needed to dma_map_single to secure world 284 */ 285 probed_convention = SMC_CONVENTION_ARM_64; 286 ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true); 287 if (!ret && res.result[0] == 1) 288 goto found; 289 290 /* 291 * Some SC7180 firmwares didn't implement the 292 * QCOM_SCM_INFO_IS_CALL_AVAIL call, so we fallback to forcing ARM_64 293 * calling conventions on these firmwares. Luckily we don't make any 294 * early calls into the firmware on these SoCs so the device pointer 295 * will be valid here to check if the compatible matches. 296 */ 297 if (of_device_is_compatible(__scm ? __scm->dev->of_node : NULL, "qcom,scm-sc7180")) { 298 forced = true; 299 goto found; 300 } 301 #endif 302 303 probed_convention = SMC_CONVENTION_ARM_32; 304 ret = __scm_smc_call(NULL, &desc, probed_convention, &res, true); 305 if (!ret && res.result[0] == 1) 306 goto found; 307 308 probed_convention = SMC_CONVENTION_LEGACY; 309 found: 310 spin_lock_irqsave(&scm_query_lock, flags); 311 if (probed_convention != qcom_scm_convention) { 312 qcom_scm_convention = probed_convention; 313 pr_info("qcom_scm: convention: %s%s\n", 314 qcom_scm_convention_names[qcom_scm_convention], 315 forced ? " (forced)" : ""); 316 } 317 spin_unlock_irqrestore(&scm_query_lock, flags); 318 319 return qcom_scm_convention; 320 } 321 322 /** 323 * qcom_scm_call() - Invoke a syscall in the secure world 324 * @dev: device 325 * @desc: Descriptor structure containing arguments and return values 326 * @res: Structure containing results from SMC/HVC call 327 * 328 * Sends a command to the SCM and waits for the command to finish processing. 329 * This should *only* be called in pre-emptible context. 330 */ 331 static int qcom_scm_call(struct device *dev, const struct qcom_scm_desc *desc, 332 struct qcom_scm_res *res) 333 { 334 might_sleep(); 335 switch (__get_convention()) { 336 case SMC_CONVENTION_ARM_32: 337 case SMC_CONVENTION_ARM_64: 338 return scm_smc_call(dev, desc, res, false); 339 case SMC_CONVENTION_LEGACY: 340 return scm_legacy_call(dev, desc, res); 341 default: 342 pr_err("Unknown current SCM calling convention.\n"); 343 return -EINVAL; 344 } 345 } 346 347 /** 348 * qcom_scm_call_atomic() - atomic variation of qcom_scm_call() 349 * @dev: device 350 * @desc: Descriptor structure containing arguments and return values 351 * @res: Structure containing results from SMC/HVC call 352 * 353 * Sends a command to the SCM and waits for the command to finish processing. 354 * This can be called in atomic context. 355 */ 356 static int qcom_scm_call_atomic(struct device *dev, 357 const struct qcom_scm_desc *desc, 358 struct qcom_scm_res *res) 359 { 360 switch (__get_convention()) { 361 case SMC_CONVENTION_ARM_32: 362 case SMC_CONVENTION_ARM_64: 363 return scm_smc_call(dev, desc, res, true); 364 case SMC_CONVENTION_LEGACY: 365 return scm_legacy_call_atomic(dev, desc, res); 366 default: 367 pr_err("Unknown current SCM calling convention.\n"); 368 return -EINVAL; 369 } 370 } 371 372 static bool __qcom_scm_is_call_available(struct device *dev, u32 svc_id, 373 u32 cmd_id) 374 { 375 int ret; 376 struct qcom_scm_desc desc = { 377 .svc = QCOM_SCM_SVC_INFO, 378 .cmd = QCOM_SCM_INFO_IS_CALL_AVAIL, 379 .owner = ARM_SMCCC_OWNER_SIP, 380 }; 381 struct qcom_scm_res res; 382 383 desc.arginfo = QCOM_SCM_ARGS(1); 384 switch (__get_convention()) { 385 case SMC_CONVENTION_ARM_32: 386 case SMC_CONVENTION_ARM_64: 387 desc.args[0] = SCM_SMC_FNID(svc_id, cmd_id) | 388 (ARM_SMCCC_OWNER_SIP << ARM_SMCCC_OWNER_SHIFT); 389 break; 390 case SMC_CONVENTION_LEGACY: 391 desc.args[0] = SCM_LEGACY_FNID(svc_id, cmd_id); 392 break; 393 default: 394 pr_err("Unknown SMC convention being used\n"); 395 return false; 396 } 397 398 ret = qcom_scm_call(dev, &desc, &res); 399 400 return ret ? false : !!res.result[0]; 401 } 402 403 static int qcom_scm_set_boot_addr(void *entry, const u8 *cpu_bits) 404 { 405 int cpu; 406 unsigned int flags = 0; 407 struct qcom_scm_desc desc = { 408 .svc = QCOM_SCM_SVC_BOOT, 409 .cmd = QCOM_SCM_BOOT_SET_ADDR, 410 .arginfo = QCOM_SCM_ARGS(2), 411 .owner = ARM_SMCCC_OWNER_SIP, 412 }; 413 414 for_each_present_cpu(cpu) { 415 if (cpu >= QCOM_SCM_BOOT_MAX_CPUS) 416 return -EINVAL; 417 flags |= cpu_bits[cpu]; 418 } 419 420 desc.args[0] = flags; 421 desc.args[1] = virt_to_phys(entry); 422 423 return qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL); 424 } 425 426 static int qcom_scm_set_boot_addr_mc(void *entry, unsigned int flags) 427 { 428 struct qcom_scm_desc desc = { 429 .svc = QCOM_SCM_SVC_BOOT, 430 .cmd = QCOM_SCM_BOOT_SET_ADDR_MC, 431 .owner = ARM_SMCCC_OWNER_SIP, 432 .arginfo = QCOM_SCM_ARGS(6), 433 .args = { 434 virt_to_phys(entry), 435 /* Apply to all CPUs in all affinity levels */ 436 ~0ULL, ~0ULL, ~0ULL, ~0ULL, 437 flags, 438 }, 439 }; 440 441 /* Need a device for DMA of the additional arguments */ 442 if (!__scm || __get_convention() == SMC_CONVENTION_LEGACY) 443 return -EOPNOTSUPP; 444 445 return qcom_scm_call(__scm->dev, &desc, NULL); 446 } 447 448 /** 449 * qcom_scm_set_warm_boot_addr() - Set the warm boot address for all cpus 450 * @entry: Entry point function for the cpus 451 * 452 * Set the Linux entry point for the SCM to transfer control to when coming 453 * out of a power down. CPU power down may be executed on cpuidle or hotplug. 454 */ 455 int qcom_scm_set_warm_boot_addr(void *entry) 456 { 457 if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_WARMBOOT)) 458 /* Fallback to old SCM call */ 459 return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_warm_bits); 460 return 0; 461 } 462 EXPORT_SYMBOL_GPL(qcom_scm_set_warm_boot_addr); 463 464 /** 465 * qcom_scm_set_cold_boot_addr() - Set the cold boot address for all cpus 466 * @entry: Entry point function for the cpus 467 */ 468 int qcom_scm_set_cold_boot_addr(void *entry) 469 { 470 if (qcom_scm_set_boot_addr_mc(entry, QCOM_SCM_BOOT_MC_FLAG_COLDBOOT)) 471 /* Fallback to old SCM call */ 472 return qcom_scm_set_boot_addr(entry, qcom_scm_cpu_cold_bits); 473 return 0; 474 } 475 EXPORT_SYMBOL_GPL(qcom_scm_set_cold_boot_addr); 476 477 /** 478 * qcom_scm_cpu_power_down() - Power down the cpu 479 * @flags: Flags to flush cache 480 * 481 * This is an end point to power down cpu. If there was a pending interrupt, 482 * the control would return from this function, otherwise, the cpu jumps to the 483 * warm boot entry point set for this cpu upon reset. 484 */ 485 void qcom_scm_cpu_power_down(u32 flags) 486 { 487 struct qcom_scm_desc desc = { 488 .svc = QCOM_SCM_SVC_BOOT, 489 .cmd = QCOM_SCM_BOOT_TERMINATE_PC, 490 .args[0] = flags & QCOM_SCM_FLUSH_FLAG_MASK, 491 .arginfo = QCOM_SCM_ARGS(1), 492 .owner = ARM_SMCCC_OWNER_SIP, 493 }; 494 495 qcom_scm_call_atomic(__scm ? __scm->dev : NULL, &desc, NULL); 496 } 497 EXPORT_SYMBOL_GPL(qcom_scm_cpu_power_down); 498 499 static int qcom_scm_disable_sdi(void) 500 { 501 int ret; 502 struct qcom_scm_desc desc = { 503 .svc = QCOM_SCM_SVC_BOOT, 504 .cmd = QCOM_SCM_BOOT_SDI_CONFIG, 505 .args[0] = 1, /* Disable watchdog debug */ 506 .args[1] = 0, /* Disable SDI */ 507 .arginfo = QCOM_SCM_ARGS(2), 508 .owner = ARM_SMCCC_OWNER_SIP, 509 }; 510 struct qcom_scm_res res; 511 512 ret = qcom_scm_clk_enable(); 513 if (ret) 514 return ret; 515 ret = qcom_scm_call(__scm->dev, &desc, &res); 516 517 qcom_scm_clk_disable(); 518 519 return ret ? : res.result[0]; 520 } 521 522 static int __qcom_scm_set_dload_mode(struct device *dev, bool enable) 523 { 524 struct qcom_scm_desc desc = { 525 .svc = QCOM_SCM_SVC_BOOT, 526 .cmd = QCOM_SCM_BOOT_SET_DLOAD_MODE, 527 .arginfo = QCOM_SCM_ARGS(2), 528 .args[0] = QCOM_SCM_BOOT_SET_DLOAD_MODE, 529 .owner = ARM_SMCCC_OWNER_SIP, 530 }; 531 532 desc.args[1] = enable ? QCOM_SCM_BOOT_SET_DLOAD_MODE : 0; 533 534 return qcom_scm_call_atomic(__scm->dev, &desc, NULL); 535 } 536 537 static int qcom_scm_io_rmw(phys_addr_t addr, unsigned int mask, unsigned int val) 538 { 539 unsigned int old; 540 unsigned int new; 541 int ret; 542 543 ret = qcom_scm_io_readl(addr, &old); 544 if (ret) 545 return ret; 546 547 new = (old & ~mask) | (val & mask); 548 549 return qcom_scm_io_writel(addr, new); 550 } 551 552 static void qcom_scm_set_download_mode(struct qcom_scm *scm, u32 dload_mode) 553 { 554 int ret = 0; 555 556 if (scm->dload_mode_addr) { 557 ret = qcom_scm_io_rmw(scm->dload_mode_addr, QCOM_DLOAD_MASK, 558 FIELD_PREP(QCOM_DLOAD_MASK, dload_mode)); 559 } else if (__qcom_scm_is_call_available(scm->dev, QCOM_SCM_SVC_BOOT, 560 QCOM_SCM_BOOT_SET_DLOAD_MODE)) { 561 ret = __qcom_scm_set_dload_mode(scm->dev, !!dload_mode); 562 } else if (dload_mode) { 563 dev_err(scm->dev, 564 "No available mechanism for setting download mode\n"); 565 } 566 567 if (ret) 568 dev_err(scm->dev, "failed to set download mode: %d\n", ret); 569 570 /* 571 * Write the destination into the always-on SRAM so boot firmware 572 * can read it before DDR is initialised on the next warm reset. 573 * Only written when minidump is active; 574 */ 575 if (scm->minidump_sram && (dload_mode & QCOM_DLOAD_MINIDUMP)) 576 writel_relaxed(minidump_dest, scm->minidump_sram); 577 } 578 579 static int __qcom_scm_pas_init_image(struct device *dev, u32 pas_id, 580 dma_addr_t mdata_phys, 581 struct qcom_scm_res *res) 582 { 583 struct qcom_scm_desc desc = { 584 .svc = QCOM_SCM_SVC_PIL, 585 .cmd = QCOM_SCM_PIL_PAS_INIT_IMAGE, 586 .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_VAL, QCOM_SCM_RW), 587 .args[0] = pas_id, 588 .owner = ARM_SMCCC_OWNER_SIP, 589 }; 590 int ret; 591 592 ret = qcom_scm_clk_enable(); 593 if (ret) 594 return ret; 595 596 ret = qcom_scm_bw_enable(); 597 if (ret) 598 goto disable_clk; 599 600 desc.args[1] = mdata_phys; 601 602 ret = qcom_scm_call(dev, &desc, res); 603 qcom_scm_bw_disable(); 604 605 disable_clk: 606 qcom_scm_clk_disable(); 607 608 return ret; 609 } 610 611 static int qcom_scm_pas_prep_and_init_image(struct device *dev, 612 struct qcom_pas_context *ctx, 613 const void *metadata, size_t size) 614 { 615 struct qcom_scm_res res; 616 phys_addr_t mdata_phys; 617 void *mdata_buf; 618 int ret; 619 620 mdata_buf = qcom_tzmem_alloc(__scm->mempool, size, GFP_KERNEL); 621 if (!mdata_buf) 622 return -ENOMEM; 623 624 memcpy(mdata_buf, metadata, size); 625 mdata_phys = qcom_tzmem_to_phys(mdata_buf); 626 627 ret = __qcom_scm_pas_init_image(dev, ctx->pas_id, mdata_phys, &res); 628 if (ret < 0) 629 qcom_tzmem_free(mdata_buf); 630 else 631 ctx->ptr = mdata_buf; 632 633 return ret ? : res.result[0]; 634 } 635 636 static int qcom_scm_pas_init_image(struct device *dev, u32 pas_id, 637 const void *metadata, size_t size, 638 struct qcom_pas_context *ctx) 639 { 640 struct qcom_scm_res res; 641 dma_addr_t mdata_phys; 642 void *mdata_buf; 643 int ret; 644 645 if (ctx && ctx->use_tzmem) 646 return qcom_scm_pas_prep_and_init_image(dev, ctx, metadata, size); 647 648 /* 649 * During the scm call memory protection will be enabled for the meta 650 * data blob, so make sure it's physically contiguous, 4K aligned and 651 * non-cachable to avoid XPU violations. 652 * 653 * For PIL calls the hypervisor creates SHM Bridges for the blob 654 * buffers on behalf of Linux so we must not do it ourselves hence 655 * not using the TZMem allocator here. 656 * 657 * If we pass a buffer that is already part of an SHM Bridge to this 658 * call, it will fail. 659 */ 660 mdata_buf = dma_alloc_coherent(dev, size, &mdata_phys, GFP_KERNEL); 661 if (!mdata_buf) 662 return -ENOMEM; 663 664 memcpy(mdata_buf, metadata, size); 665 666 ret = __qcom_scm_pas_init_image(dev, pas_id, mdata_phys, &res); 667 if (ret < 0 || !ctx) { 668 dma_free_coherent(dev, size, mdata_buf, mdata_phys); 669 } else if (ctx) { 670 ctx->ptr = mdata_buf; 671 ctx->phys = mdata_phys; 672 ctx->size = size; 673 } 674 675 return ret ? : res.result[0]; 676 } 677 678 static void qcom_scm_pas_metadata_release(struct device *dev, 679 struct qcom_pas_context *ctx) 680 { 681 if (ctx->use_tzmem) 682 qcom_tzmem_free(ctx->ptr); 683 else 684 dma_free_coherent(dev, ctx->size, ctx->ptr, ctx->phys); 685 686 ctx->ptr = NULL; 687 } 688 689 static int qcom_scm_pas_mem_setup(struct device *dev, u32 pas_id, 690 phys_addr_t addr, phys_addr_t size) 691 { 692 int ret; 693 struct qcom_scm_desc desc = { 694 .svc = QCOM_SCM_SVC_PIL, 695 .cmd = QCOM_SCM_PIL_PAS_MEM_SETUP, 696 .arginfo = QCOM_SCM_ARGS(3), 697 .args[0] = pas_id, 698 .args[1] = addr, 699 .args[2] = size, 700 .owner = ARM_SMCCC_OWNER_SIP, 701 }; 702 struct qcom_scm_res res; 703 704 ret = qcom_scm_clk_enable(); 705 if (ret) 706 return ret; 707 708 ret = qcom_scm_bw_enable(); 709 if (ret) 710 goto disable_clk; 711 712 ret = qcom_scm_call(dev, &desc, &res); 713 qcom_scm_bw_disable(); 714 715 disable_clk: 716 qcom_scm_clk_disable(); 717 718 return ret ? : res.result[0]; 719 } 720 721 static void *__qcom_scm_pas_get_rsc_table(struct device *dev, u32 pas_id, 722 void *input_rt_tzm, 723 size_t input_rt_size, 724 size_t *output_rt_size) 725 { 726 struct qcom_scm_desc desc = { 727 .svc = QCOM_SCM_SVC_PIL, 728 .cmd = QCOM_SCM_PIL_PAS_GET_RSCTABLE, 729 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, QCOM_SCM_RO, QCOM_SCM_VAL, 730 QCOM_SCM_RW, QCOM_SCM_VAL), 731 .args[0] = pas_id, 732 .owner = ARM_SMCCC_OWNER_SIP, 733 }; 734 struct qcom_scm_res res; 735 void *output_rt_tzm; 736 int ret; 737 738 output_rt_tzm = qcom_tzmem_alloc(__scm->mempool, *output_rt_size, GFP_KERNEL); 739 if (!output_rt_tzm) 740 return ERR_PTR(-ENOMEM); 741 742 desc.args[1] = qcom_tzmem_to_phys(input_rt_tzm); 743 desc.args[2] = input_rt_size; 744 desc.args[3] = qcom_tzmem_to_phys(output_rt_tzm); 745 desc.args[4] = *output_rt_size; 746 747 /* 748 * Whether SMC fail or pass, res.result[2] will hold actual resource table 749 * size. 750 * 751 * If passed 'output_rt_size' buffer size is not sufficient to hold the 752 * resource table TrustZone sends, response code in res.result[1] as 753 * RSCTABLE_BUFFER_NOT_SUFFICIENT so that caller can retry this SMC call 754 * with output_rt_tzm buffer with res.result[2] size however, It should not 755 * be of unresonable size. 756 */ 757 ret = qcom_scm_call(dev, &desc, &res); 758 if (!ret && res.result[2] > SZ_1G) { 759 ret = -E2BIG; 760 goto free_output_rt; 761 } 762 763 *output_rt_size = res.result[2]; 764 if (ret && res.result[1] == RSCTABLE_BUFFER_NOT_SUFFICIENT) 765 ret = -EOVERFLOW; 766 767 free_output_rt: 768 if (ret) 769 qcom_tzmem_free(output_rt_tzm); 770 771 return ret ? ERR_PTR(ret) : output_rt_tzm; 772 } 773 774 static void *qcom_scm_pas_get_rsc_table(struct device *dev, 775 struct qcom_pas_context *ctx, 776 void *input_rt, size_t input_rt_size, 777 size_t *output_rt_size) 778 { 779 struct resource_table empty_rsc = {}; 780 size_t size = SZ_16K; 781 void *output_rt_tzm; 782 void *input_rt_tzm; 783 void *tbl_ptr; 784 int ret; 785 786 ret = qcom_scm_clk_enable(); 787 if (ret) 788 return ERR_PTR(ret); 789 790 ret = qcom_scm_bw_enable(); 791 if (ret) 792 goto disable_clk; 793 794 /* 795 * TrustZone can not accept buffer as NULL value as argument hence, 796 * we need to pass a input buffer indicating that subsystem firmware 797 * does not have resource table by filling resource table structure. 798 */ 799 if (!input_rt) { 800 input_rt = &empty_rsc; 801 input_rt_size = sizeof(empty_rsc); 802 } 803 804 input_rt_tzm = qcom_tzmem_alloc(__scm->mempool, input_rt_size, GFP_KERNEL); 805 if (!input_rt_tzm) { 806 ret = -ENOMEM; 807 goto disable_scm_bw; 808 } 809 810 memcpy(input_rt_tzm, input_rt, input_rt_size); 811 812 output_rt_tzm = __qcom_scm_pas_get_rsc_table(dev, ctx->pas_id, 813 input_rt_tzm, 814 input_rt_size, &size); 815 if (PTR_ERR(output_rt_tzm) == -EOVERFLOW) { 816 /* Try again with a page-aligned size requested by the TZ. */ 817 size = PAGE_ALIGN(size); 818 output_rt_tzm = __qcom_scm_pas_get_rsc_table(dev, ctx->pas_id, 819 input_rt_tzm, 820 input_rt_size, 821 &size); 822 } 823 if (IS_ERR(output_rt_tzm)) { 824 ret = PTR_ERR(output_rt_tzm); 825 goto free_input_rt; 826 } 827 828 *output_rt_size = size; 829 830 /* A zero-sized table means that TrustZone has no resource table. */ 831 if (!size) { 832 tbl_ptr = NULL; 833 goto free_output_rt; 834 } 835 836 tbl_ptr = kmemdup(output_rt_tzm, size, GFP_KERNEL); 837 if (!tbl_ptr) { 838 qcom_tzmem_free(output_rt_tzm); 839 ret = -ENOMEM; 840 goto free_input_rt; 841 } 842 843 free_output_rt: 844 qcom_tzmem_free(output_rt_tzm); 845 846 free_input_rt: 847 qcom_tzmem_free(input_rt_tzm); 848 849 disable_scm_bw: 850 qcom_scm_bw_disable(); 851 852 disable_clk: 853 qcom_scm_clk_disable(); 854 855 return ret ? ERR_PTR(ret) : tbl_ptr; 856 } 857 858 static int qcom_scm_pas_auth_and_reset(struct device *dev, u32 pas_id) 859 { 860 int ret; 861 struct qcom_scm_desc desc = { 862 .svc = QCOM_SCM_SVC_PIL, 863 .cmd = QCOM_SCM_PIL_PAS_AUTH_AND_RESET, 864 .arginfo = QCOM_SCM_ARGS(1), 865 .args[0] = pas_id, 866 .owner = ARM_SMCCC_OWNER_SIP, 867 }; 868 struct qcom_scm_res res; 869 870 ret = qcom_scm_clk_enable(); 871 if (ret) 872 return ret; 873 874 ret = qcom_scm_bw_enable(); 875 if (ret) 876 goto disable_clk; 877 878 ret = qcom_scm_call(dev, &desc, &res); 879 qcom_scm_bw_disable(); 880 881 disable_clk: 882 qcom_scm_clk_disable(); 883 884 return ret ? : res.result[0]; 885 } 886 887 static int qcom_scm_pas_prepare_and_auth_reset(struct device *dev, 888 struct qcom_pas_context *ctx) 889 { 890 u64 handle; 891 int ret; 892 893 /* 894 * When Linux running @ EL1, Gunyah hypervisor running @ EL2 traps the 895 * auth_and_reset call and create an shmbridge on the remote subsystem 896 * memory region and then invokes a call to TrustZone to authenticate. 897 */ 898 if (!ctx->use_tzmem) 899 return qcom_scm_pas_auth_and_reset(dev, ctx->pas_id); 900 901 /* 902 * When Linux runs @ EL2 Linux must create the shmbridge itself and then 903 * subsequently call TrustZone for authenticate and reset. 904 */ 905 ret = qcom_tzmem_shm_bridge_create(ctx->mem_phys, ctx->mem_size, &handle); 906 if (ret) 907 return ret; 908 909 ret = qcom_scm_pas_auth_and_reset(dev, ctx->pas_id); 910 qcom_tzmem_shm_bridge_delete(handle); 911 912 return ret; 913 } 914 915 static int qcom_scm_pas_set_remote_state(struct device *dev, u32 state, 916 u32 pas_id) 917 { 918 struct qcom_scm_desc desc = { 919 .svc = QCOM_SCM_SVC_BOOT, 920 .cmd = QCOM_SCM_BOOT_SET_REMOTE_STATE, 921 .arginfo = QCOM_SCM_ARGS(2), 922 .args[0] = state, 923 .args[1] = pas_id, 924 .owner = ARM_SMCCC_OWNER_SIP, 925 }; 926 struct qcom_scm_res res; 927 int ret; 928 929 ret = qcom_scm_call(dev, &desc, &res); 930 931 return ret ? : res.result[0]; 932 } 933 934 static int qcom_scm_pas_shutdown(struct device *dev, u32 pas_id) 935 { 936 int ret; 937 struct qcom_scm_desc desc = { 938 .svc = QCOM_SCM_SVC_PIL, 939 .cmd = QCOM_SCM_PIL_PAS_SHUTDOWN, 940 .arginfo = QCOM_SCM_ARGS(1), 941 .args[0] = pas_id, 942 .owner = ARM_SMCCC_OWNER_SIP, 943 }; 944 struct qcom_scm_res res; 945 946 ret = qcom_scm_clk_enable(); 947 if (ret) 948 return ret; 949 950 ret = qcom_scm_bw_enable(); 951 if (ret) 952 goto disable_clk; 953 954 ret = qcom_scm_call(dev, &desc, &res); 955 qcom_scm_bw_disable(); 956 957 disable_clk: 958 qcom_scm_clk_disable(); 959 960 return ret ? : res.result[0]; 961 } 962 963 static bool qcom_scm_pas_supported(struct device *dev, u32 pas_id) 964 { 965 int ret; 966 struct qcom_scm_desc desc = { 967 .svc = QCOM_SCM_SVC_PIL, 968 .cmd = QCOM_SCM_PIL_PAS_IS_SUPPORTED, 969 .arginfo = QCOM_SCM_ARGS(1), 970 .args[0] = pas_id, 971 .owner = ARM_SMCCC_OWNER_SIP, 972 }; 973 struct qcom_scm_res res; 974 975 if (!__qcom_scm_is_call_available(dev, QCOM_SCM_SVC_PIL, 976 QCOM_SCM_PIL_PAS_IS_SUPPORTED)) 977 return false; 978 979 ret = qcom_scm_call(dev, &desc, &res); 980 981 return ret ? false : !!res.result[0]; 982 } 983 984 static struct qcom_pas_ops qcom_pas_ops_scm = { 985 .drv_name = "qcom_scm", 986 .supported = qcom_scm_pas_supported, 987 .init_image = qcom_scm_pas_init_image, 988 .mem_setup = qcom_scm_pas_mem_setup, 989 .get_rsc_table = qcom_scm_pas_get_rsc_table, 990 .auth_and_reset = qcom_scm_pas_auth_and_reset, 991 .prepare_and_auth_reset = qcom_scm_pas_prepare_and_auth_reset, 992 .set_remote_state = qcom_scm_pas_set_remote_state, 993 .shutdown = qcom_scm_pas_shutdown, 994 .metadata_release = qcom_scm_pas_metadata_release, 995 }; 996 997 /** 998 * qcom_scm_is_pas_available() - Check if the peripheral authentication service 999 * is available via SCM or not 1000 * 1001 * Returns true if PAS is available, otherwise false. 1002 */ 1003 static bool qcom_scm_is_pas_available(void) 1004 { 1005 if (!__qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_PIL, 1006 QCOM_SCM_PIL_PAS_AUTH_AND_RESET)) 1007 return false; 1008 1009 return true; 1010 } 1011 1012 static int __qcom_scm_pas_mss_reset(struct device *dev, bool reset) 1013 { 1014 struct qcom_scm_desc desc = { 1015 .svc = QCOM_SCM_SVC_PIL, 1016 .cmd = QCOM_SCM_PIL_PAS_MSS_RESET, 1017 .arginfo = QCOM_SCM_ARGS(2), 1018 .args[0] = reset, 1019 .args[1] = 0, 1020 .owner = ARM_SMCCC_OWNER_SIP, 1021 }; 1022 struct qcom_scm_res res; 1023 int ret; 1024 1025 ret = qcom_scm_call(__scm->dev, &desc, &res); 1026 1027 return ret ? : res.result[0]; 1028 } 1029 1030 static int qcom_scm_pas_reset_assert(struct reset_controller_dev *rcdev, 1031 unsigned long idx) 1032 { 1033 if (idx != 0) 1034 return -EINVAL; 1035 1036 return __qcom_scm_pas_mss_reset(__scm->dev, 1); 1037 } 1038 1039 static int qcom_scm_pas_reset_deassert(struct reset_controller_dev *rcdev, 1040 unsigned long idx) 1041 { 1042 if (idx != 0) 1043 return -EINVAL; 1044 1045 return __qcom_scm_pas_mss_reset(__scm->dev, 0); 1046 } 1047 1048 static const struct reset_control_ops qcom_scm_pas_reset_ops = { 1049 .assert = qcom_scm_pas_reset_assert, 1050 .deassert = qcom_scm_pas_reset_deassert, 1051 }; 1052 1053 int qcom_scm_io_readl(phys_addr_t addr, unsigned int *val) 1054 { 1055 struct qcom_scm_desc desc = { 1056 .svc = QCOM_SCM_SVC_IO, 1057 .cmd = QCOM_SCM_IO_READ, 1058 .arginfo = QCOM_SCM_ARGS(1), 1059 .args[0] = addr, 1060 .owner = ARM_SMCCC_OWNER_SIP, 1061 }; 1062 struct qcom_scm_res res; 1063 int ret; 1064 1065 1066 ret = qcom_scm_call_atomic(__scm->dev, &desc, &res); 1067 if (ret >= 0) 1068 *val = res.result[0]; 1069 1070 return ret < 0 ? ret : 0; 1071 } 1072 EXPORT_SYMBOL_GPL(qcom_scm_io_readl); 1073 1074 int qcom_scm_io_writel(phys_addr_t addr, unsigned int val) 1075 { 1076 struct qcom_scm_desc desc = { 1077 .svc = QCOM_SCM_SVC_IO, 1078 .cmd = QCOM_SCM_IO_WRITE, 1079 .arginfo = QCOM_SCM_ARGS(2), 1080 .args[0] = addr, 1081 .args[1] = val, 1082 .owner = ARM_SMCCC_OWNER_SIP, 1083 }; 1084 1085 return qcom_scm_call_atomic(__scm->dev, &desc, NULL); 1086 } 1087 EXPORT_SYMBOL_GPL(qcom_scm_io_writel); 1088 1089 /** 1090 * qcom_scm_restore_sec_cfg_available() - Check if secure environment 1091 * supports restore security config interface. 1092 * 1093 * Return true if restore-cfg interface is supported, false if not. 1094 */ 1095 bool qcom_scm_restore_sec_cfg_available(void) 1096 { 1097 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP, 1098 QCOM_SCM_MP_RESTORE_SEC_CFG); 1099 } 1100 EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg_available); 1101 1102 int qcom_scm_restore_sec_cfg(u32 device_id, u32 spare) 1103 { 1104 struct qcom_scm_desc desc = { 1105 .svc = QCOM_SCM_SVC_MP, 1106 .cmd = QCOM_SCM_MP_RESTORE_SEC_CFG, 1107 .arginfo = QCOM_SCM_ARGS(2), 1108 .args[0] = device_id, 1109 .args[1] = spare, 1110 .owner = ARM_SMCCC_OWNER_SIP, 1111 }; 1112 struct qcom_scm_res res; 1113 int ret; 1114 1115 ret = qcom_scm_call(__scm->dev, &desc, &res); 1116 1117 return ret ? : res.result[0]; 1118 } 1119 EXPORT_SYMBOL_GPL(qcom_scm_restore_sec_cfg); 1120 1121 #define QCOM_SCM_CP_APERTURE_CONTEXT_MASK GENMASK(7, 0) 1122 1123 bool qcom_scm_set_gpu_smmu_aperture_is_available(void) 1124 { 1125 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_MP, 1126 QCOM_SCM_MP_CP_SMMU_APERTURE_ID); 1127 } 1128 EXPORT_SYMBOL_GPL(qcom_scm_set_gpu_smmu_aperture_is_available); 1129 1130 int qcom_scm_set_gpu_smmu_aperture(unsigned int context_bank) 1131 { 1132 struct qcom_scm_desc desc = { 1133 .svc = QCOM_SCM_SVC_MP, 1134 .cmd = QCOM_SCM_MP_CP_SMMU_APERTURE_ID, 1135 .arginfo = QCOM_SCM_ARGS(4), 1136 .args[0] = 0xffff0000 | FIELD_PREP(QCOM_SCM_CP_APERTURE_CONTEXT_MASK, context_bank), 1137 .args[1] = 0xffffffff, 1138 .args[2] = 0xffffffff, 1139 .args[3] = 0xffffffff, 1140 .owner = ARM_SMCCC_OWNER_SIP 1141 }; 1142 1143 return qcom_scm_call(__scm->dev, &desc, NULL); 1144 } 1145 EXPORT_SYMBOL_GPL(qcom_scm_set_gpu_smmu_aperture); 1146 1147 int qcom_scm_iommu_secure_ptbl_size(u32 spare, size_t *size) 1148 { 1149 struct qcom_scm_desc desc = { 1150 .svc = QCOM_SCM_SVC_MP, 1151 .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_SIZE, 1152 .arginfo = QCOM_SCM_ARGS(1), 1153 .args[0] = spare, 1154 .owner = ARM_SMCCC_OWNER_SIP, 1155 }; 1156 struct qcom_scm_res res; 1157 int ret; 1158 1159 ret = qcom_scm_call(__scm->dev, &desc, &res); 1160 1161 if (size) 1162 *size = res.result[0]; 1163 1164 return ret ? : res.result[1]; 1165 } 1166 EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_size); 1167 1168 int qcom_scm_iommu_secure_ptbl_init(u64 addr, u32 size, u32 spare) 1169 { 1170 struct qcom_scm_desc desc = { 1171 .svc = QCOM_SCM_SVC_MP, 1172 .cmd = QCOM_SCM_MP_IOMMU_SECURE_PTBL_INIT, 1173 .arginfo = QCOM_SCM_ARGS(3, QCOM_SCM_RW, QCOM_SCM_VAL, 1174 QCOM_SCM_VAL), 1175 .args[0] = addr, 1176 .args[1] = size, 1177 .args[2] = spare, 1178 .owner = ARM_SMCCC_OWNER_SIP, 1179 }; 1180 int ret; 1181 1182 ret = qcom_scm_call(__scm->dev, &desc, NULL); 1183 1184 /* the pg table has been initialized already, ignore the error */ 1185 if (ret == -EPERM) 1186 ret = 0; 1187 1188 return ret; 1189 } 1190 EXPORT_SYMBOL_GPL(qcom_scm_iommu_secure_ptbl_init); 1191 1192 int qcom_scm_iommu_set_cp_pool_size(u32 spare, u32 size) 1193 { 1194 struct qcom_scm_desc desc = { 1195 .svc = QCOM_SCM_SVC_MP, 1196 .cmd = QCOM_SCM_MP_IOMMU_SET_CP_POOL_SIZE, 1197 .arginfo = QCOM_SCM_ARGS(2), 1198 .args[0] = size, 1199 .args[1] = spare, 1200 .owner = ARM_SMCCC_OWNER_SIP, 1201 }; 1202 1203 return qcom_scm_call(__scm->dev, &desc, NULL); 1204 } 1205 EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_cp_pool_size); 1206 1207 int qcom_scm_mem_protect_video_var(u32 cp_start, u32 cp_size, 1208 u32 cp_nonpixel_start, 1209 u32 cp_nonpixel_size) 1210 { 1211 int ret; 1212 struct qcom_scm_desc desc = { 1213 .svc = QCOM_SCM_SVC_MP, 1214 .cmd = QCOM_SCM_MP_VIDEO_VAR, 1215 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL, 1216 QCOM_SCM_VAL, QCOM_SCM_VAL), 1217 .args[0] = cp_start, 1218 .args[1] = cp_size, 1219 .args[2] = cp_nonpixel_start, 1220 .args[3] = cp_nonpixel_size, 1221 .owner = ARM_SMCCC_OWNER_SIP, 1222 }; 1223 struct qcom_scm_res res; 1224 1225 ret = qcom_scm_call(__scm->dev, &desc, &res); 1226 1227 return ret ? : res.result[0]; 1228 } 1229 EXPORT_SYMBOL_GPL(qcom_scm_mem_protect_video_var); 1230 1231 static int __qcom_scm_assign_mem(struct device *dev, phys_addr_t mem_region, 1232 size_t mem_sz, phys_addr_t src, size_t src_sz, 1233 phys_addr_t dest, size_t dest_sz) 1234 { 1235 int ret; 1236 struct qcom_scm_desc desc = { 1237 .svc = QCOM_SCM_SVC_MP, 1238 .cmd = QCOM_SCM_MP_ASSIGN, 1239 .arginfo = QCOM_SCM_ARGS(7, QCOM_SCM_RO, QCOM_SCM_VAL, 1240 QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_RO, 1241 QCOM_SCM_VAL, QCOM_SCM_VAL), 1242 .args[0] = mem_region, 1243 .args[1] = mem_sz, 1244 .args[2] = src, 1245 .args[3] = src_sz, 1246 .args[4] = dest, 1247 .args[5] = dest_sz, 1248 .args[6] = 0, 1249 .owner = ARM_SMCCC_OWNER_SIP, 1250 }; 1251 struct qcom_scm_res res; 1252 1253 ret = qcom_scm_call(dev, &desc, &res); 1254 1255 return ret ? : res.result[0]; 1256 } 1257 1258 /** 1259 * qcom_scm_assign_mem() - Make a secure call to reassign memory ownership 1260 * @mem_addr: mem region whose ownership need to be reassigned 1261 * @mem_sz: size of the region. 1262 * @srcvm: vmid for current set of owners, each set bit in 1263 * flag indicate a unique owner 1264 * @newvm: array having new owners and corresponding permission 1265 * flags 1266 * @dest_cnt: number of owners in next set. 1267 * 1268 * Return negative errno on failure or 0 on success with @srcvm updated. 1269 */ 1270 int qcom_scm_assign_mem(phys_addr_t mem_addr, size_t mem_sz, 1271 u64 *srcvm, 1272 const struct qcom_scm_vmperm *newvm, 1273 unsigned int dest_cnt) 1274 { 1275 struct qcom_scm_current_perm_info *destvm; 1276 struct qcom_scm_mem_map_info *mem_to_map; 1277 phys_addr_t mem_to_map_phys; 1278 phys_addr_t dest_phys; 1279 phys_addr_t ptr_phys; 1280 size_t mem_to_map_sz; 1281 size_t dest_sz; 1282 size_t src_sz; 1283 size_t ptr_sz; 1284 int next_vm; 1285 __le32 *src; 1286 int ret, i, b; 1287 u64 srcvm_bits = *srcvm; 1288 1289 src_sz = hweight64(srcvm_bits) * sizeof(*src); 1290 mem_to_map_sz = sizeof(*mem_to_map); 1291 dest_sz = dest_cnt * sizeof(*destvm); 1292 ptr_sz = ALIGN(src_sz, SZ_64) + ALIGN(mem_to_map_sz, SZ_64) + 1293 ALIGN(dest_sz, SZ_64); 1294 1295 void *ptr __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1296 ptr_sz, GFP_KERNEL); 1297 if (!ptr) 1298 return -ENOMEM; 1299 1300 ptr_phys = qcom_tzmem_to_phys(ptr); 1301 1302 /* Fill source vmid detail */ 1303 src = ptr; 1304 i = 0; 1305 for (b = 0; b < BITS_PER_TYPE(u64); b++) { 1306 if (srcvm_bits & BIT(b)) 1307 src[i++] = cpu_to_le32(b); 1308 } 1309 1310 /* Fill details of mem buff to map */ 1311 mem_to_map = ptr + ALIGN(src_sz, SZ_64); 1312 mem_to_map_phys = ptr_phys + ALIGN(src_sz, SZ_64); 1313 mem_to_map->mem_addr = cpu_to_le64(mem_addr); 1314 mem_to_map->mem_size = cpu_to_le64(mem_sz); 1315 1316 next_vm = 0; 1317 /* Fill details of next vmid detail */ 1318 destvm = ptr + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64); 1319 dest_phys = ptr_phys + ALIGN(mem_to_map_sz, SZ_64) + ALIGN(src_sz, SZ_64); 1320 for (i = 0; i < dest_cnt; i++, destvm++, newvm++) { 1321 destvm->vmid = cpu_to_le32(newvm->vmid); 1322 destvm->perm = cpu_to_le32(newvm->perm); 1323 destvm->ctx = 0; 1324 destvm->ctx_size = 0; 1325 next_vm |= BIT(newvm->vmid); 1326 } 1327 1328 ret = __qcom_scm_assign_mem(__scm->dev, mem_to_map_phys, mem_to_map_sz, 1329 ptr_phys, src_sz, dest_phys, dest_sz); 1330 if (ret) { 1331 dev_err(__scm->dev, 1332 "Assign memory protection call failed %d\n", ret); 1333 return ret; 1334 } 1335 1336 *srcvm = next_vm; 1337 return 0; 1338 } 1339 EXPORT_SYMBOL_GPL(qcom_scm_assign_mem); 1340 1341 /** 1342 * qcom_scm_ocmem_lock_available() - is OCMEM lock/unlock interface available 1343 */ 1344 bool qcom_scm_ocmem_lock_available(void) 1345 { 1346 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_OCMEM, 1347 QCOM_SCM_OCMEM_LOCK_CMD); 1348 } 1349 EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock_available); 1350 1351 /** 1352 * qcom_scm_ocmem_lock() - call OCMEM lock interface to assign an OCMEM 1353 * region to the specified initiator 1354 * 1355 * @id: tz initiator id 1356 * @offset: OCMEM offset 1357 * @size: OCMEM size 1358 * @mode: access mode (WIDE/NARROW) 1359 */ 1360 int qcom_scm_ocmem_lock(enum qcom_scm_ocmem_client id, u32 offset, u32 size, 1361 u32 mode) 1362 { 1363 struct qcom_scm_desc desc = { 1364 .svc = QCOM_SCM_SVC_OCMEM, 1365 .cmd = QCOM_SCM_OCMEM_LOCK_CMD, 1366 .args[0] = id, 1367 .args[1] = offset, 1368 .args[2] = size, 1369 .args[3] = mode, 1370 .arginfo = QCOM_SCM_ARGS(4), 1371 }; 1372 1373 return qcom_scm_call(__scm->dev, &desc, NULL); 1374 } 1375 EXPORT_SYMBOL_GPL(qcom_scm_ocmem_lock); 1376 1377 /** 1378 * qcom_scm_ocmem_unlock() - call OCMEM unlock interface to release an OCMEM 1379 * region from the specified initiator 1380 * 1381 * @id: tz initiator id 1382 * @offset: OCMEM offset 1383 * @size: OCMEM size 1384 */ 1385 int qcom_scm_ocmem_unlock(enum qcom_scm_ocmem_client id, u32 offset, u32 size) 1386 { 1387 struct qcom_scm_desc desc = { 1388 .svc = QCOM_SCM_SVC_OCMEM, 1389 .cmd = QCOM_SCM_OCMEM_UNLOCK_CMD, 1390 .args[0] = id, 1391 .args[1] = offset, 1392 .args[2] = size, 1393 .arginfo = QCOM_SCM_ARGS(3), 1394 }; 1395 1396 return qcom_scm_call(__scm->dev, &desc, NULL); 1397 } 1398 EXPORT_SYMBOL_GPL(qcom_scm_ocmem_unlock); 1399 1400 /** 1401 * qcom_scm_ice_available() - Is the ICE key programming interface available? 1402 * 1403 * Return: true iff the SCM calls wrapped by qcom_scm_ice_invalidate_key() and 1404 * qcom_scm_ice_set_key() are available. 1405 */ 1406 bool qcom_scm_ice_available(void) 1407 { 1408 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES, 1409 QCOM_SCM_ES_INVALIDATE_ICE_KEY) && 1410 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES, 1411 QCOM_SCM_ES_CONFIG_SET_ICE_KEY); 1412 } 1413 EXPORT_SYMBOL_GPL(qcom_scm_ice_available); 1414 1415 /** 1416 * qcom_scm_ice_invalidate_key() - Invalidate an inline encryption key 1417 * @index: the keyslot to invalidate 1418 * 1419 * The UFSHCI and eMMC standards define a standard way to do this, but it 1420 * doesn't work on these SoCs; only this SCM call does. 1421 * 1422 * It is assumed that the SoC has only one ICE instance being used, as this SCM 1423 * call doesn't specify which ICE instance the keyslot belongs to. 1424 * 1425 * Return: 0 on success; -errno on failure. 1426 */ 1427 int qcom_scm_ice_invalidate_key(u32 index) 1428 { 1429 struct qcom_scm_desc desc = { 1430 .svc = QCOM_SCM_SVC_ES, 1431 .cmd = QCOM_SCM_ES_INVALIDATE_ICE_KEY, 1432 .arginfo = QCOM_SCM_ARGS(1), 1433 .args[0] = index, 1434 .owner = ARM_SMCCC_OWNER_SIP, 1435 }; 1436 1437 return qcom_scm_call(__scm->dev, &desc, NULL); 1438 } 1439 EXPORT_SYMBOL_GPL(qcom_scm_ice_invalidate_key); 1440 1441 /** 1442 * qcom_scm_ice_set_key() - Set an inline encryption key 1443 * @index: the keyslot into which to set the key 1444 * @key: the key to program 1445 * @key_size: the size of the key in bytes 1446 * @cipher: the encryption algorithm the key is for 1447 * @data_unit_size: the encryption data unit size, i.e. the size of each 1448 * individual plaintext and ciphertext. Given in 512-byte 1449 * units, e.g. 1 = 512 bytes, 8 = 4096 bytes, etc. 1450 * 1451 * Program a key into a keyslot of Qualcomm ICE (Inline Crypto Engine), where it 1452 * can then be used to encrypt/decrypt UFS or eMMC I/O requests inline. 1453 * 1454 * The UFSHCI and eMMC standards define a standard way to do this, but it 1455 * doesn't work on these SoCs; only this SCM call does. 1456 * 1457 * It is assumed that the SoC has only one ICE instance being used, as this SCM 1458 * call doesn't specify which ICE instance the keyslot belongs to. 1459 * 1460 * Return: 0 on success; -errno on failure. 1461 */ 1462 int qcom_scm_ice_set_key(u32 index, const u8 *key, u32 key_size, 1463 enum qcom_scm_ice_cipher cipher, u32 data_unit_size) 1464 { 1465 struct qcom_scm_desc desc = { 1466 .svc = QCOM_SCM_SVC_ES, 1467 .cmd = QCOM_SCM_ES_CONFIG_SET_ICE_KEY, 1468 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, QCOM_SCM_RW, 1469 QCOM_SCM_VAL, QCOM_SCM_VAL, 1470 QCOM_SCM_VAL), 1471 .args[0] = index, 1472 .args[2] = key_size, 1473 .args[3] = cipher, 1474 .args[4] = data_unit_size, 1475 .owner = ARM_SMCCC_OWNER_SIP, 1476 }; 1477 1478 int ret; 1479 1480 void *keybuf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1481 key_size, 1482 GFP_KERNEL); 1483 if (!keybuf) 1484 return -ENOMEM; 1485 memcpy(keybuf, key, key_size); 1486 desc.args[1] = qcom_tzmem_to_phys(keybuf); 1487 1488 ret = qcom_scm_call(__scm->dev, &desc, NULL); 1489 1490 memzero_explicit(keybuf, key_size); 1491 1492 return ret; 1493 } 1494 EXPORT_SYMBOL_GPL(qcom_scm_ice_set_key); 1495 1496 bool qcom_scm_has_wrapped_key_support(void) 1497 { 1498 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES, 1499 QCOM_SCM_ES_DERIVE_SW_SECRET) && 1500 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES, 1501 QCOM_SCM_ES_GENERATE_ICE_KEY) && 1502 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES, 1503 QCOM_SCM_ES_PREPARE_ICE_KEY) && 1504 __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_ES, 1505 QCOM_SCM_ES_IMPORT_ICE_KEY); 1506 } 1507 EXPORT_SYMBOL_GPL(qcom_scm_has_wrapped_key_support); 1508 1509 /** 1510 * qcom_scm_derive_sw_secret() - Derive software secret from wrapped key 1511 * @eph_key: an ephemerally-wrapped key 1512 * @eph_key_size: size of @eph_key in bytes 1513 * @sw_secret: output buffer for the software secret 1514 * @sw_secret_size: size of the software secret to derive in bytes 1515 * 1516 * Derive a software secret from an ephemerally-wrapped key for software crypto 1517 * operations. This is done by calling into the secure execution environment, 1518 * which then calls into the hardware to unwrap and derive the secret. 1519 * 1520 * For more information on sw_secret, see the "Hardware-wrapped keys" section of 1521 * Documentation/block/inline-encryption.rst. 1522 * 1523 * Return: 0 on success; -errno on failure. 1524 */ 1525 int qcom_scm_derive_sw_secret(const u8 *eph_key, size_t eph_key_size, 1526 u8 *sw_secret, size_t sw_secret_size) 1527 { 1528 struct qcom_scm_desc desc = { 1529 .svc = QCOM_SCM_SVC_ES, 1530 .cmd = QCOM_SCM_ES_DERIVE_SW_SECRET, 1531 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RW, QCOM_SCM_VAL, 1532 QCOM_SCM_RW, QCOM_SCM_VAL), 1533 .owner = ARM_SMCCC_OWNER_SIP, 1534 }; 1535 int ret; 1536 1537 void *eph_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1538 eph_key_size, 1539 GFP_KERNEL); 1540 if (!eph_key_buf) 1541 return -ENOMEM; 1542 1543 void *sw_secret_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1544 sw_secret_size, 1545 GFP_KERNEL); 1546 if (!sw_secret_buf) 1547 return -ENOMEM; 1548 1549 memcpy(eph_key_buf, eph_key, eph_key_size); 1550 desc.args[0] = qcom_tzmem_to_phys(eph_key_buf); 1551 desc.args[1] = eph_key_size; 1552 desc.args[2] = qcom_tzmem_to_phys(sw_secret_buf); 1553 desc.args[3] = sw_secret_size; 1554 1555 ret = qcom_scm_call(__scm->dev, &desc, NULL); 1556 if (!ret) 1557 memcpy(sw_secret, sw_secret_buf, sw_secret_size); 1558 1559 memzero_explicit(eph_key_buf, eph_key_size); 1560 memzero_explicit(sw_secret_buf, sw_secret_size); 1561 return ret; 1562 } 1563 EXPORT_SYMBOL_GPL(qcom_scm_derive_sw_secret); 1564 1565 /** 1566 * qcom_scm_generate_ice_key() - Generate a wrapped key for storage encryption 1567 * @lt_key: output buffer for the long-term wrapped key 1568 * @lt_key_size: size of @lt_key in bytes. Must be the exact wrapped key size 1569 * used by the SoC. 1570 * 1571 * Generate a key using the built-in HW module in the SoC. The resulting key is 1572 * returned wrapped with the platform-specific Key Encryption Key. 1573 * 1574 * Return: 0 on success; -errno on failure. 1575 */ 1576 int qcom_scm_generate_ice_key(u8 *lt_key, size_t lt_key_size) 1577 { 1578 struct qcom_scm_desc desc = { 1579 .svc = QCOM_SCM_SVC_ES, 1580 .cmd = QCOM_SCM_ES_GENERATE_ICE_KEY, 1581 .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL), 1582 .owner = ARM_SMCCC_OWNER_SIP, 1583 }; 1584 int ret; 1585 1586 void *lt_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1587 lt_key_size, 1588 GFP_KERNEL); 1589 if (!lt_key_buf) 1590 return -ENOMEM; 1591 1592 desc.args[0] = qcom_tzmem_to_phys(lt_key_buf); 1593 desc.args[1] = lt_key_size; 1594 1595 ret = qcom_scm_call(__scm->dev, &desc, NULL); 1596 if (!ret) 1597 memcpy(lt_key, lt_key_buf, lt_key_size); 1598 1599 memzero_explicit(lt_key_buf, lt_key_size); 1600 return ret; 1601 } 1602 EXPORT_SYMBOL_GPL(qcom_scm_generate_ice_key); 1603 1604 /** 1605 * qcom_scm_prepare_ice_key() - Re-wrap a key with the per-boot ephemeral key 1606 * @lt_key: a long-term wrapped key 1607 * @lt_key_size: size of @lt_key in bytes 1608 * @eph_key: output buffer for the ephemerally-wrapped key 1609 * @eph_key_size: size of @eph_key in bytes. Must be the exact wrapped key size 1610 * used by the SoC. 1611 * 1612 * Given a long-term wrapped key, re-wrap it with the per-boot ephemeral key for 1613 * added protection. The resulting key will only be valid for the current boot. 1614 * 1615 * Return: 0 on success; -errno on failure. 1616 */ 1617 int qcom_scm_prepare_ice_key(const u8 *lt_key, size_t lt_key_size, 1618 u8 *eph_key, size_t eph_key_size) 1619 { 1620 struct qcom_scm_desc desc = { 1621 .svc = QCOM_SCM_SVC_ES, 1622 .cmd = QCOM_SCM_ES_PREPARE_ICE_KEY, 1623 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RO, QCOM_SCM_VAL, 1624 QCOM_SCM_RW, QCOM_SCM_VAL), 1625 .owner = ARM_SMCCC_OWNER_SIP, 1626 }; 1627 int ret; 1628 1629 void *lt_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1630 lt_key_size, 1631 GFP_KERNEL); 1632 if (!lt_key_buf) 1633 return -ENOMEM; 1634 1635 void *eph_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1636 eph_key_size, 1637 GFP_KERNEL); 1638 if (!eph_key_buf) 1639 return -ENOMEM; 1640 1641 memcpy(lt_key_buf, lt_key, lt_key_size); 1642 desc.args[0] = qcom_tzmem_to_phys(lt_key_buf); 1643 desc.args[1] = lt_key_size; 1644 desc.args[2] = qcom_tzmem_to_phys(eph_key_buf); 1645 desc.args[3] = eph_key_size; 1646 1647 ret = qcom_scm_call(__scm->dev, &desc, NULL); 1648 if (!ret) 1649 memcpy(eph_key, eph_key_buf, eph_key_size); 1650 1651 memzero_explicit(lt_key_buf, lt_key_size); 1652 memzero_explicit(eph_key_buf, eph_key_size); 1653 return ret; 1654 } 1655 EXPORT_SYMBOL_GPL(qcom_scm_prepare_ice_key); 1656 1657 /** 1658 * qcom_scm_import_ice_key() - Import key for storage encryption 1659 * @raw_key: the raw key to import 1660 * @raw_key_size: size of @raw_key in bytes 1661 * @lt_key: output buffer for the long-term wrapped key 1662 * @lt_key_size: size of @lt_key in bytes. Must be the exact wrapped key size 1663 * used by the SoC. 1664 * 1665 * Import a raw key and return a long-term wrapped key. Uses the SoC's HWKM to 1666 * wrap the raw key using the platform-specific Key Encryption Key. 1667 * 1668 * Return: 0 on success; -errno on failure. 1669 */ 1670 int qcom_scm_import_ice_key(const u8 *raw_key, size_t raw_key_size, 1671 u8 *lt_key, size_t lt_key_size) 1672 { 1673 struct qcom_scm_desc desc = { 1674 .svc = QCOM_SCM_SVC_ES, 1675 .cmd = QCOM_SCM_ES_IMPORT_ICE_KEY, 1676 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RO, QCOM_SCM_VAL, 1677 QCOM_SCM_RW, QCOM_SCM_VAL), 1678 .owner = ARM_SMCCC_OWNER_SIP, 1679 }; 1680 int ret; 1681 1682 void *raw_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1683 raw_key_size, 1684 GFP_KERNEL); 1685 if (!raw_key_buf) 1686 return -ENOMEM; 1687 1688 void *lt_key_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1689 lt_key_size, 1690 GFP_KERNEL); 1691 if (!lt_key_buf) 1692 return -ENOMEM; 1693 1694 memcpy(raw_key_buf, raw_key, raw_key_size); 1695 desc.args[0] = qcom_tzmem_to_phys(raw_key_buf); 1696 desc.args[1] = raw_key_size; 1697 desc.args[2] = qcom_tzmem_to_phys(lt_key_buf); 1698 desc.args[3] = lt_key_size; 1699 1700 ret = qcom_scm_call(__scm->dev, &desc, NULL); 1701 if (!ret) 1702 memcpy(lt_key, lt_key_buf, lt_key_size); 1703 1704 memzero_explicit(raw_key_buf, raw_key_size); 1705 memzero_explicit(lt_key_buf, lt_key_size); 1706 return ret; 1707 } 1708 EXPORT_SYMBOL_GPL(qcom_scm_import_ice_key); 1709 1710 /** 1711 * qcom_scm_hdcp_available() - Check if secure environment supports HDCP. 1712 * 1713 * Return true if HDCP is supported, false if not. 1714 */ 1715 bool qcom_scm_hdcp_available(void) 1716 { 1717 bool avail; 1718 int ret = qcom_scm_clk_enable(); 1719 1720 if (ret) 1721 return ret; 1722 1723 avail = __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_HDCP, 1724 QCOM_SCM_HDCP_INVOKE); 1725 1726 qcom_scm_clk_disable(); 1727 1728 return avail; 1729 } 1730 EXPORT_SYMBOL_GPL(qcom_scm_hdcp_available); 1731 1732 /** 1733 * qcom_scm_hdcp_req() - Send HDCP request. 1734 * @req: HDCP request array 1735 * @req_cnt: HDCP request array count 1736 * @resp: response buffer passed to SCM 1737 * 1738 * Write HDCP register(s) through SCM. 1739 */ 1740 int qcom_scm_hdcp_req(struct qcom_scm_hdcp_req *req, u32 req_cnt, u32 *resp) 1741 { 1742 int ret; 1743 struct qcom_scm_desc desc = { 1744 .svc = QCOM_SCM_SVC_HDCP, 1745 .cmd = QCOM_SCM_HDCP_INVOKE, 1746 .arginfo = QCOM_SCM_ARGS(10), 1747 .args = { 1748 req[0].addr, 1749 req[0].val, 1750 req[1].addr, 1751 req[1].val, 1752 req[2].addr, 1753 req[2].val, 1754 req[3].addr, 1755 req[3].val, 1756 req[4].addr, 1757 req[4].val 1758 }, 1759 .owner = ARM_SMCCC_OWNER_SIP, 1760 }; 1761 struct qcom_scm_res res; 1762 1763 if (req_cnt > QCOM_SCM_HDCP_MAX_REQ_CNT) 1764 return -ERANGE; 1765 1766 ret = qcom_scm_clk_enable(); 1767 if (ret) 1768 return ret; 1769 1770 ret = qcom_scm_call(__scm->dev, &desc, &res); 1771 *resp = res.result[0]; 1772 1773 qcom_scm_clk_disable(); 1774 1775 return ret; 1776 } 1777 EXPORT_SYMBOL_GPL(qcom_scm_hdcp_req); 1778 1779 int qcom_scm_iommu_set_pt_format(u32 sec_id, u32 ctx_num, u32 pt_fmt) 1780 { 1781 struct qcom_scm_desc desc = { 1782 .svc = QCOM_SCM_SVC_SMMU_PROGRAM, 1783 .cmd = QCOM_SCM_SMMU_PT_FORMAT, 1784 .arginfo = QCOM_SCM_ARGS(3), 1785 .args[0] = sec_id, 1786 .args[1] = ctx_num, 1787 .args[2] = pt_fmt, /* 0: LPAE AArch32 - 1: AArch64 */ 1788 .owner = ARM_SMCCC_OWNER_SIP, 1789 }; 1790 1791 return qcom_scm_call(__scm->dev, &desc, NULL); 1792 } 1793 EXPORT_SYMBOL_GPL(qcom_scm_iommu_set_pt_format); 1794 1795 int qcom_scm_qsmmu500_wait_safe_toggle(bool en) 1796 { 1797 struct qcom_scm_desc desc = { 1798 .svc = QCOM_SCM_SVC_SMMU_PROGRAM, 1799 .cmd = QCOM_SCM_SMMU_CONFIG_ERRATA1, 1800 .arginfo = QCOM_SCM_ARGS(2), 1801 .args[0] = QCOM_SCM_SMMU_CONFIG_ERRATA1_CLIENT_ALL, 1802 .args[1] = en, 1803 .owner = ARM_SMCCC_OWNER_SIP, 1804 }; 1805 1806 1807 return qcom_scm_call_atomic(__scm->dev, &desc, NULL); 1808 } 1809 EXPORT_SYMBOL_GPL(qcom_scm_qsmmu500_wait_safe_toggle); 1810 1811 bool qcom_scm_lmh_dcvsh_available(void) 1812 { 1813 return __qcom_scm_is_call_available(__scm->dev, QCOM_SCM_SVC_LMH, QCOM_SCM_LMH_LIMIT_DCVSH); 1814 } 1815 EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh_available); 1816 1817 /* 1818 * This is only supposed to be called once by the TZMem module. It takes the 1819 * SCM struct device as argument and uses it to pass the call as at the time 1820 * the SHM Bridge is enabled, the SCM is not yet fully set up and doesn't 1821 * accept global user calls. Don't try to use the __scm pointer here. 1822 */ 1823 int qcom_scm_shm_bridge_enable(struct device *scm_dev) 1824 { 1825 int ret; 1826 1827 struct qcom_scm_desc desc = { 1828 .svc = QCOM_SCM_SVC_MP, 1829 .cmd = QCOM_SCM_MP_SHM_BRIDGE_ENABLE, 1830 .owner = ARM_SMCCC_OWNER_SIP 1831 }; 1832 1833 struct qcom_scm_res res; 1834 1835 if (!__qcom_scm_is_call_available(scm_dev, QCOM_SCM_SVC_MP, 1836 QCOM_SCM_MP_SHM_BRIDGE_ENABLE)) 1837 return -EOPNOTSUPP; 1838 1839 ret = qcom_scm_call(scm_dev, &desc, &res); 1840 1841 if (ret) 1842 return ret; 1843 1844 if (res.result[0] == SHMBRIDGE_RESULT_NOTSUPP) 1845 return -EOPNOTSUPP; 1846 1847 return res.result[0]; 1848 } 1849 EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_enable); 1850 1851 int qcom_scm_shm_bridge_create(u64 pfn_and_ns_perm_flags, 1852 u64 ipfn_and_s_perm_flags, u64 size_and_flags, 1853 u64 ns_vmids, u64 *handle) 1854 { 1855 struct qcom_scm_desc desc = { 1856 .svc = QCOM_SCM_SVC_MP, 1857 .cmd = QCOM_SCM_MP_SHM_BRIDGE_CREATE, 1858 .owner = ARM_SMCCC_OWNER_SIP, 1859 .args[0] = pfn_and_ns_perm_flags, 1860 .args[1] = ipfn_and_s_perm_flags, 1861 .args[2] = size_and_flags, 1862 .args[3] = ns_vmids, 1863 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_VAL, QCOM_SCM_VAL, 1864 QCOM_SCM_VAL, QCOM_SCM_VAL), 1865 }; 1866 1867 struct qcom_scm_res res; 1868 int ret; 1869 1870 ret = qcom_scm_call(__scm->dev, &desc, &res); 1871 1872 if (handle && !ret) 1873 *handle = res.result[1]; 1874 1875 return ret ?: res.result[0]; 1876 } 1877 EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_create); 1878 1879 int qcom_scm_shm_bridge_delete(u64 handle) 1880 { 1881 struct qcom_scm_desc desc = { 1882 .svc = QCOM_SCM_SVC_MP, 1883 .cmd = QCOM_SCM_MP_SHM_BRIDGE_DELETE, 1884 .owner = ARM_SMCCC_OWNER_SIP, 1885 .args[0] = handle, 1886 .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL), 1887 }; 1888 1889 return qcom_scm_call(__scm->dev, &desc, NULL); 1890 } 1891 EXPORT_SYMBOL_GPL(qcom_scm_shm_bridge_delete); 1892 1893 int qcom_scm_lmh_profile_change(u32 profile_id) 1894 { 1895 struct qcom_scm_desc desc = { 1896 .svc = QCOM_SCM_SVC_LMH, 1897 .cmd = QCOM_SCM_LMH_LIMIT_PROFILE_CHANGE, 1898 .arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL), 1899 .args[0] = profile_id, 1900 .owner = ARM_SMCCC_OWNER_SIP, 1901 }; 1902 1903 return qcom_scm_call(__scm->dev, &desc, NULL); 1904 } 1905 EXPORT_SYMBOL_GPL(qcom_scm_lmh_profile_change); 1906 1907 int qcom_scm_lmh_dcvsh(u32 payload_fn, u32 payload_reg, u32 payload_val, 1908 u64 limit_node, u32 node_id, u64 version) 1909 { 1910 int ret, payload_size = 5 * sizeof(u32); 1911 1912 struct qcom_scm_desc desc = { 1913 .svc = QCOM_SCM_SVC_LMH, 1914 .cmd = QCOM_SCM_LMH_LIMIT_DCVSH, 1915 .arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_RO, QCOM_SCM_VAL, QCOM_SCM_VAL, 1916 QCOM_SCM_VAL, QCOM_SCM_VAL), 1917 .args[1] = payload_size, 1918 .args[2] = limit_node, 1919 .args[3] = node_id, 1920 .args[4] = version, 1921 .owner = ARM_SMCCC_OWNER_SIP, 1922 }; 1923 1924 u32 *payload_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 1925 payload_size, 1926 GFP_KERNEL); 1927 if (!payload_buf) 1928 return -ENOMEM; 1929 1930 payload_buf[0] = payload_fn; 1931 payload_buf[1] = 0; 1932 payload_buf[2] = payload_reg; 1933 payload_buf[3] = 1; 1934 payload_buf[4] = payload_val; 1935 1936 desc.args[0] = qcom_tzmem_to_phys(payload_buf); 1937 1938 ret = qcom_scm_call(__scm->dev, &desc, NULL); 1939 1940 return ret; 1941 } 1942 EXPORT_SYMBOL_GPL(qcom_scm_lmh_dcvsh); 1943 1944 int qcom_scm_gpu_init_regs(u32 gpu_req) 1945 { 1946 struct qcom_scm_desc desc = { 1947 .svc = QCOM_SCM_SVC_GPU, 1948 .cmd = QCOM_SCM_SVC_GPU_INIT_REGS, 1949 .arginfo = QCOM_SCM_ARGS(1), 1950 .args[0] = gpu_req, 1951 .owner = ARM_SMCCC_OWNER_SIP, 1952 }; 1953 1954 return qcom_scm_call(__scm->dev, &desc, NULL); 1955 } 1956 EXPORT_SYMBOL_GPL(qcom_scm_gpu_init_regs); 1957 1958 static int qcom_scm_map_minidump_sram(struct device *dev, void __iomem **out) 1959 { 1960 struct device_node *np = dev->of_node; 1961 struct device_node *sram_np; 1962 struct resource res; 1963 int ret; 1964 1965 sram_np = of_parse_phandle(np, "sram", 0); 1966 if (!sram_np) 1967 return 0; 1968 1969 ret = of_address_to_resource(sram_np, 0, &res); 1970 of_node_put(sram_np); 1971 if (ret) 1972 return ret; 1973 1974 *out = devm_ioremap(dev, res.start, resource_size(&res)); 1975 if (!*out) 1976 return -ENOMEM; 1977 1978 return 0; 1979 } 1980 1981 static int qcom_scm_find_dload_address(struct device *dev, u64 *addr) 1982 { 1983 struct device_node *tcsr; 1984 struct device_node *np = dev->of_node; 1985 struct resource res; 1986 u32 offset; 1987 int ret; 1988 1989 tcsr = of_parse_phandle(np, "qcom,dload-mode", 0); 1990 if (!tcsr) 1991 return 0; 1992 1993 ret = of_address_to_resource(tcsr, 0, &res); 1994 of_node_put(tcsr); 1995 if (ret) 1996 return ret; 1997 1998 ret = of_property_read_u32_index(np, "qcom,dload-mode", 1, &offset); 1999 if (ret < 0) 2000 return ret; 2001 2002 *addr = res.start + offset; 2003 2004 return 0; 2005 } 2006 2007 #ifdef CONFIG_QCOM_QSEECOM 2008 2009 /* Lock for QSEECOM SCM call executions */ 2010 static DEFINE_MUTEX(qcom_scm_qseecom_call_lock); 2011 2012 static int __qcom_scm_qseecom_call(const struct qcom_scm_desc *desc, 2013 struct qcom_scm_qseecom_resp *res) 2014 { 2015 struct qcom_scm_res scm_res = {}; 2016 int status; 2017 2018 /* 2019 * QSEECOM SCM calls should not be executed concurrently. Therefore, we 2020 * require the respective call lock to be held. 2021 */ 2022 lockdep_assert_held(&qcom_scm_qseecom_call_lock); 2023 2024 status = qcom_scm_call(__scm->dev, desc, &scm_res); 2025 2026 res->result = scm_res.result[0]; 2027 res->resp_type = scm_res.result[1]; 2028 res->data = scm_res.result[2]; 2029 2030 if (status) 2031 return status; 2032 2033 return 0; 2034 } 2035 2036 /** 2037 * qcom_scm_qseecom_call() - Perform a QSEECOM SCM call. 2038 * @desc: SCM call descriptor. 2039 * @res: SCM call response (output). 2040 * 2041 * Performs the QSEECOM SCM call described by @desc, returning the response in 2042 * @rsp. 2043 * 2044 * Return: Zero on success, nonzero on failure. 2045 */ 2046 static int qcom_scm_qseecom_call(const struct qcom_scm_desc *desc, 2047 struct qcom_scm_qseecom_resp *res) 2048 { 2049 int status; 2050 2051 /* 2052 * Note: Multiple QSEECOM SCM calls should not be executed same time, 2053 * so lock things here. This needs to be extended to callback/listener 2054 * handling when support for that is implemented. 2055 */ 2056 2057 mutex_lock(&qcom_scm_qseecom_call_lock); 2058 status = __qcom_scm_qseecom_call(desc, res); 2059 mutex_unlock(&qcom_scm_qseecom_call_lock); 2060 2061 dev_dbg(__scm->dev, "%s: owner=%x, svc=%x, cmd=%x, result=%lld, type=%llx, data=%llx\n", 2062 __func__, desc->owner, desc->svc, desc->cmd, res->result, 2063 res->resp_type, res->data); 2064 2065 if (status) { 2066 dev_err(__scm->dev, "qseecom: scm call failed with error %d\n", status); 2067 return status; 2068 } 2069 2070 /* 2071 * TODO: Handle incomplete and blocked calls: 2072 * 2073 * Incomplete and blocked calls are not supported yet. Some devices 2074 * and/or commands require those, some don't. Let's warn about them 2075 * prominently in case someone attempts to try these commands with a 2076 * device/command combination that isn't supported yet. 2077 */ 2078 WARN_ON(res->result == QSEECOM_RESULT_INCOMPLETE); 2079 WARN_ON(res->result == QSEECOM_RESULT_BLOCKED_ON_LISTENER); 2080 2081 return 0; 2082 } 2083 2084 /** 2085 * qcom_scm_qseecom_get_version() - Query the QSEECOM version. 2086 * @version: Pointer where the QSEECOM version will be stored. 2087 * 2088 * Performs the QSEECOM SCM querying the QSEECOM version currently running in 2089 * the TrustZone. 2090 * 2091 * Return: Zero on success, nonzero on failure. 2092 */ 2093 static int qcom_scm_qseecom_get_version(u32 *version) 2094 { 2095 struct qcom_scm_desc desc = {}; 2096 struct qcom_scm_qseecom_resp res = {}; 2097 u32 feature = 10; 2098 int ret; 2099 2100 desc.owner = QSEECOM_TZ_OWNER_SIP; 2101 desc.svc = QSEECOM_TZ_SVC_INFO; 2102 desc.cmd = QSEECOM_TZ_CMD_INFO_VERSION; 2103 desc.arginfo = QCOM_SCM_ARGS(1, QCOM_SCM_VAL); 2104 desc.args[0] = feature; 2105 2106 ret = qcom_scm_qseecom_call(&desc, &res); 2107 if (ret) 2108 return ret; 2109 2110 *version = res.result; 2111 return 0; 2112 } 2113 2114 /** 2115 * qcom_scm_qseecom_app_get_id() - Query the app ID for a given QSEE app name. 2116 * @app_name: The name of the app. 2117 * @app_id: The returned app ID. 2118 * 2119 * Query and return the application ID of the SEE app identified by the given 2120 * name. This returned ID is the unique identifier of the app required for 2121 * subsequent communication. 2122 * 2123 * Return: Zero on success, nonzero on failure, -ENOENT if the app has not been 2124 * loaded or could not be found. 2125 */ 2126 int qcom_scm_qseecom_app_get_id(const char *app_name, u32 *app_id) 2127 { 2128 unsigned long name_buf_size = QSEECOM_MAX_APP_NAME_SIZE; 2129 unsigned long app_name_len = strlen(app_name); 2130 struct qcom_scm_desc desc = {}; 2131 struct qcom_scm_qseecom_resp res = {}; 2132 int status; 2133 2134 if (app_name_len >= name_buf_size) 2135 return -EINVAL; 2136 2137 char *name_buf __free(qcom_tzmem) = qcom_tzmem_alloc(__scm->mempool, 2138 name_buf_size, 2139 GFP_KERNEL); 2140 if (!name_buf) 2141 return -ENOMEM; 2142 2143 memcpy(name_buf, app_name, app_name_len); 2144 2145 desc.owner = QSEECOM_TZ_OWNER_QSEE_OS; 2146 desc.svc = QSEECOM_TZ_SVC_APP_MGR; 2147 desc.cmd = QSEECOM_TZ_CMD_APP_LOOKUP; 2148 desc.arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL); 2149 desc.args[0] = qcom_tzmem_to_phys(name_buf); 2150 desc.args[1] = app_name_len; 2151 2152 status = qcom_scm_qseecom_call(&desc, &res); 2153 2154 if (status) 2155 return status; 2156 2157 if (res.result == QSEECOM_RESULT_FAILURE) 2158 return -ENOENT; 2159 2160 if (res.result != QSEECOM_RESULT_SUCCESS) 2161 return -EINVAL; 2162 2163 if (res.resp_type != QSEECOM_SCM_RES_APP_ID) 2164 return -EINVAL; 2165 2166 *app_id = res.data; 2167 return 0; 2168 } 2169 EXPORT_SYMBOL_GPL(qcom_scm_qseecom_app_get_id); 2170 2171 /** 2172 * qcom_scm_qseecom_app_send() - Send to and receive data from a given QSEE app. 2173 * @app_id: The ID of the target app. 2174 * @req: Request buffer sent to the app (must be TZ memory) 2175 * @req_size: Size of the request buffer. 2176 * @rsp: Response buffer, written to by the app (must be TZ memory) 2177 * @rsp_size: Size of the response buffer. 2178 * 2179 * Sends a request to the QSEE app associated with the given ID and read back 2180 * its response. The caller must provide two DMA memory regions, one for the 2181 * request and one for the response, and fill out the @req region with the 2182 * respective (app-specific) request data. The QSEE app reads this and returns 2183 * its response in the @rsp region. 2184 * 2185 * Return: Zero on success, nonzero on failure. 2186 */ 2187 int qcom_scm_qseecom_app_send(u32 app_id, void *req, size_t req_size, 2188 void *rsp, size_t rsp_size) 2189 { 2190 struct qcom_scm_qseecom_resp res = {}; 2191 struct qcom_scm_desc desc = {}; 2192 phys_addr_t req_phys; 2193 phys_addr_t rsp_phys; 2194 int status; 2195 2196 req_phys = qcom_tzmem_to_phys(req); 2197 rsp_phys = qcom_tzmem_to_phys(rsp); 2198 2199 desc.owner = QSEECOM_TZ_OWNER_TZ_APPS; 2200 desc.svc = QSEECOM_TZ_SVC_APP_ID_PLACEHOLDER; 2201 desc.cmd = QSEECOM_TZ_CMD_APP_SEND; 2202 desc.arginfo = QCOM_SCM_ARGS(5, QCOM_SCM_VAL, 2203 QCOM_SCM_RW, QCOM_SCM_VAL, 2204 QCOM_SCM_RW, QCOM_SCM_VAL); 2205 desc.args[0] = app_id; 2206 desc.args[1] = req_phys; 2207 desc.args[2] = req_size; 2208 desc.args[3] = rsp_phys; 2209 desc.args[4] = rsp_size; 2210 2211 status = qcom_scm_qseecom_call(&desc, &res); 2212 2213 if (status) 2214 return status; 2215 2216 if (res.result != QSEECOM_RESULT_SUCCESS) 2217 return -EIO; 2218 2219 return 0; 2220 } 2221 EXPORT_SYMBOL_GPL(qcom_scm_qseecom_app_send); 2222 2223 /* 2224 * We do not yet support re-entrant calls via the qseecom interface. To prevent 2225 + any potential issues with this, only allow validated machines for now. 2226 */ 2227 static const struct of_device_id qcom_scm_qseecom_allowlist[] __maybe_unused = { 2228 { .compatible = "asus,vivobook-s15" }, 2229 { .compatible = "asus,vivobook-s15-x1p4" }, 2230 { .compatible = "asus,zenbook-a14-ux3407na" }, 2231 { .compatible = "asus,zenbook-a14-ux3407qa" }, 2232 { .compatible = "asus,zenbook-a14-ux3407ra" }, 2233 { .compatible = "asus,zenbook-a16-ux3607oa" }, 2234 { .compatible = "dell,inspiron-14-plus-7441" }, 2235 { .compatible = "dell,latitude-7455" }, 2236 { .compatible = "dell,xps13-9345" }, 2237 { .compatible = "ecs,liva-qc710" }, 2238 { .compatible = "honor,magicbook-art-14-snapdragon" }, 2239 { .compatible = "hp,elitebook-ultra-g1q" }, 2240 { .compatible = "hp,elitebook-x-g2q" }, 2241 { .compatible = "hp,omnibook-x14" }, 2242 { .compatible = "huawei,gaokun3" }, 2243 { .compatible = "lenovo,flex-5g" }, 2244 { .compatible = "lenovo,ideacentre-mini-01q8x10" }, 2245 { .compatible = "lenovo,thinkbook-16" }, 2246 { .compatible = "lenovo,thinkpad-t14s" }, 2247 { .compatible = "lenovo,thinkpad-t14s-gen7" }, 2248 { .compatible = "lenovo,thinkpad-x13s", }, 2249 { .compatible = "lenovo,yoga-slim7x" }, 2250 { .compatible = "lenovo,yoga-slim7x-gen11" }, 2251 { .compatible = "medion,sprchrgd14s1" }, 2252 { .compatible = "microsoft,arcata", }, 2253 { .compatible = "microsoft,surface-pro-12in", }, 2254 { .compatible = "microsoft,blackrock" }, 2255 { .compatible = "microsoft,denali", }, 2256 { .compatible = "microsoft,romulus13", }, 2257 { .compatible = "microsoft,romulus15", }, 2258 { .compatible = "qcom,glymur-crd" }, 2259 { .compatible = "qcom,glymur-qcb" }, 2260 { .compatible = "qcom,hamoa-iot-evk" }, 2261 { .compatible = "qcom,kalambo-crd" }, 2262 { .compatible = "qcom,mahua-crd" }, 2263 { .compatible = "qcom,purwa-iot-evk" }, 2264 { .compatible = "qcom,sc8180x-primus" }, 2265 { .compatible = "qcom,x1e001de-devkit" }, 2266 { .compatible = "qcom,x1e80100-crd" }, 2267 { .compatible = "qcom,x1e80100-qcp" }, 2268 { .compatible = "qcom,x1p42100-crd" }, 2269 { } 2270 }; 2271 2272 static void qcom_scm_qseecom_free(void *data) 2273 { 2274 struct platform_device *qseecom_dev = data; 2275 2276 platform_device_del(qseecom_dev); 2277 platform_device_put(qseecom_dev); 2278 } 2279 2280 static int qcom_scm_qseecom_init(struct qcom_scm *scm) 2281 { 2282 struct platform_device *qseecom_dev; 2283 u32 version; 2284 int ret; 2285 2286 /* 2287 * Note: We do two steps of validation here: First, we try to query the 2288 * QSEECOM version as a check to see if the interface exists on this 2289 * device. Second, we check against known good devices due to current 2290 * driver limitations (see comment in qcom_scm_qseecom_allowlist). 2291 * 2292 * Note that we deliberately do the machine check after the version 2293 * check so that we can log potentially supported devices. This should 2294 * be safe as downstream sources indicate that the version query is 2295 * neither blocking nor reentrant. 2296 */ 2297 ret = qcom_scm_qseecom_get_version(&version); 2298 if (ret) 2299 return 0; 2300 2301 dev_info(scm->dev, "qseecom: found qseecom with version 0x%x\n", version); 2302 2303 if (!of_machine_device_match(qcom_scm_qseecom_allowlist)) { 2304 dev_info(scm->dev, "qseecom: untested machine, skipping\n"); 2305 return 0; 2306 } 2307 2308 /* 2309 * Set up QSEECOM interface device. All application clients will be 2310 * set up and managed by the corresponding driver for it. 2311 */ 2312 qseecom_dev = platform_device_alloc("qcom_qseecom", -1); 2313 if (!qseecom_dev) 2314 return -ENOMEM; 2315 2316 qseecom_dev->dev.parent = scm->dev; 2317 2318 ret = platform_device_add(qseecom_dev); 2319 if (ret) { 2320 platform_device_put(qseecom_dev); 2321 return ret; 2322 } 2323 2324 return devm_add_action_or_reset(scm->dev, qcom_scm_qseecom_free, qseecom_dev); 2325 } 2326 2327 #else /* CONFIG_QCOM_QSEECOM */ 2328 2329 static int qcom_scm_qseecom_init(struct qcom_scm *scm) 2330 { 2331 return 0; 2332 } 2333 2334 #endif /* CONFIG_QCOM_QSEECOM */ 2335 2336 /** 2337 * qcom_scm_qtee_invoke_smc() - Invoke a QTEE object. 2338 * @inbuf: start address of memory area used for inbound buffer. 2339 * @inbuf_size: size of the memory area used for inbound buffer. 2340 * @outbuf: start address of memory area used for outbound buffer. 2341 * @outbuf_size: size of the memory area used for outbound buffer. 2342 * @result: result of QTEE object invocation. 2343 * @response_type: response type returned by QTEE. 2344 * 2345 * @response_type determines how the contents of @inbuf and @outbuf 2346 * should be processed. 2347 * 2348 * Return: On success, return 0 or <0 on failure. 2349 */ 2350 int qcom_scm_qtee_invoke_smc(phys_addr_t inbuf, size_t inbuf_size, 2351 phys_addr_t outbuf, size_t outbuf_size, 2352 u64 *result, u64 *response_type) 2353 { 2354 struct qcom_scm_desc desc = { 2355 .svc = QCOM_SCM_SVC_SMCINVOKE, 2356 .cmd = QCOM_SCM_SMCINVOKE_INVOKE, 2357 .owner = ARM_SMCCC_OWNER_TRUSTED_OS, 2358 .args[0] = inbuf, 2359 .args[1] = inbuf_size, 2360 .args[2] = outbuf, 2361 .args[3] = outbuf_size, 2362 .arginfo = QCOM_SCM_ARGS(4, QCOM_SCM_RW, QCOM_SCM_VAL, 2363 QCOM_SCM_RW, QCOM_SCM_VAL), 2364 }; 2365 struct qcom_scm_res res; 2366 int ret; 2367 2368 ret = qcom_scm_call(__scm->dev, &desc, &res); 2369 if (ret) 2370 return ret; 2371 2372 if (response_type) 2373 *response_type = res.result[0]; 2374 2375 if (result) 2376 *result = res.result[1]; 2377 2378 return 0; 2379 } 2380 EXPORT_SYMBOL(qcom_scm_qtee_invoke_smc); 2381 2382 /** 2383 * qcom_scm_qtee_callback_response() - Submit response for callback request. 2384 * @buf: start address of memory area used for outbound buffer. 2385 * @buf_size: size of the memory area used for outbound buffer. 2386 * @result: Result of QTEE object invocation. 2387 * @response_type: Response type returned by QTEE. 2388 * 2389 * @response_type determines how the contents of @buf should be processed. 2390 * 2391 * Return: On success, return 0 or <0 on failure. 2392 */ 2393 int qcom_scm_qtee_callback_response(phys_addr_t buf, size_t buf_size, 2394 u64 *result, u64 *response_type) 2395 { 2396 struct qcom_scm_desc desc = { 2397 .svc = QCOM_SCM_SVC_SMCINVOKE, 2398 .cmd = QCOM_SCM_SMCINVOKE_CB_RSP, 2399 .owner = ARM_SMCCC_OWNER_TRUSTED_OS, 2400 .args[0] = buf, 2401 .args[1] = buf_size, 2402 .arginfo = QCOM_SCM_ARGS(2, QCOM_SCM_RW, QCOM_SCM_VAL), 2403 }; 2404 struct qcom_scm_res res; 2405 int ret; 2406 2407 ret = qcom_scm_call(__scm->dev, &desc, &res); 2408 if (ret) 2409 return ret; 2410 2411 if (response_type) 2412 *response_type = res.result[0]; 2413 2414 if (result) 2415 *result = res.result[1]; 2416 2417 return 0; 2418 } 2419 EXPORT_SYMBOL(qcom_scm_qtee_callback_response); 2420 2421 static void qcom_scm_gunyah_wdt_free(void *data) 2422 { 2423 struct platform_device *gunyah_wdt_dev = data; 2424 2425 platform_device_unregister(gunyah_wdt_dev); 2426 } 2427 2428 static void qcom_scm_gunyah_wdt_init(struct qcom_scm *scm) 2429 { 2430 struct platform_device *gunyah_wdt_dev; 2431 struct device_node *np; 2432 bool of_wdt_available; 2433 int i; 2434 static const uuid_t gunyah_uuid = UUID_INIT(0xc1d58fcd, 0xa453, 0x5fdb, 2435 0x92, 0x65, 0xce, 0x36, 2436 0x67, 0x3d, 0x5f, 0x14); 2437 static const char * const of_wdt_compatible[] = { 2438 "qcom,kpss-wdt", 2439 "arm,sbsa-gwdt", 2440 }; 2441 2442 /* Bail out if we are not running under Gunyah */ 2443 if (!IS_ENABLED(CONFIG_HAVE_ARM_SMCCC_DISCOVERY) || 2444 !arm_smccc_hypervisor_has_uuid(&gunyah_uuid)) 2445 return; 2446 2447 /* 2448 * Gunyah emulates either of Qualcomm watchdog or ARM SBSA watchdog on 2449 * newer platforms. Bail out if we find them in the devicetree. 2450 */ 2451 for (i = 0; i < ARRAY_SIZE(of_wdt_compatible); i++) { 2452 np = of_find_compatible_node(NULL, NULL, of_wdt_compatible[i]); 2453 of_wdt_available = of_device_is_available(np); 2454 of_node_put(np); 2455 if (of_wdt_available) 2456 return; 2457 } 2458 2459 gunyah_wdt_dev = platform_device_register_simple("gunyah-wdt", -1, 2460 NULL, 0); 2461 if (IS_ERR(gunyah_wdt_dev)) { 2462 dev_err(scm->dev, "Failed to register Gunyah watchdog device: %ld\n", 2463 PTR_ERR(gunyah_wdt_dev)); 2464 return; 2465 } 2466 2467 devm_add_action_or_reset(scm->dev, qcom_scm_gunyah_wdt_free, 2468 gunyah_wdt_dev); 2469 } 2470 2471 static void qcom_scm_qtee_free(void *data) 2472 { 2473 struct platform_device *qtee_dev = data; 2474 2475 platform_device_unregister(qtee_dev); 2476 } 2477 2478 static void qcom_scm_qtee_init(struct qcom_scm *scm) 2479 { 2480 struct platform_device *qtee_dev; 2481 u64 result, response_type; 2482 int ret; 2483 2484 /* 2485 * Probe for smcinvoke support. This will fail due to invalid buffers, 2486 * but first, it checks whether the call is supported in QTEE syscall 2487 * handler. If it is not supported, -EIO is returned. 2488 */ 2489 ret = qcom_scm_qtee_invoke_smc(0, 0, 0, 0, &result, &response_type); 2490 if (ret == -EIO) 2491 return; 2492 2493 /* Setup QTEE interface device. */ 2494 qtee_dev = platform_device_register_data(scm->dev, "qcomtee", 2495 PLATFORM_DEVID_NONE, NULL, 0); 2496 if (IS_ERR(qtee_dev)) 2497 return; 2498 2499 devm_add_action_or_reset(scm->dev, qcom_scm_qtee_free, qtee_dev); 2500 } 2501 2502 /** 2503 * qcom_scm_is_available() - Checks if SCM is available 2504 */ 2505 bool qcom_scm_is_available(void) 2506 { 2507 /* Paired with smp_store_release() in qcom_scm_probe */ 2508 return !!smp_load_acquire(&__scm); 2509 } 2510 EXPORT_SYMBOL_GPL(qcom_scm_is_available); 2511 2512 static int qcom_scm_fill_irq_fwspec_params(struct irq_fwspec *fwspec, u32 hwirq) 2513 { 2514 if (hwirq >= GIC_SPI_BASE && hwirq <= GIC_MAX_SPI) { 2515 fwspec->param[0] = GIC_SPI; 2516 fwspec->param[1] = hwirq - GIC_SPI_BASE; 2517 } else if (hwirq >= GIC_ESPI_BASE && hwirq <= GIC_MAX_ESPI) { 2518 fwspec->param[0] = GIC_ESPI; 2519 fwspec->param[1] = hwirq - GIC_ESPI_BASE; 2520 } else { 2521 WARN(1, "Unexpected hwirq: %d\n", hwirq); 2522 return -ENXIO; 2523 } 2524 2525 fwspec->param[2] = IRQ_TYPE_EDGE_RISING; 2526 fwspec->param_count = 3; 2527 2528 return 0; 2529 } 2530 2531 static int qcom_scm_query_waitq_count(struct qcom_scm *scm) 2532 { 2533 struct qcom_scm_desc desc = { 2534 .svc = QCOM_SCM_SVC_WAITQ, 2535 .cmd = QCOM_SCM_WAITQ_GET_INFO, 2536 .owner = ARM_SMCCC_OWNER_SIP 2537 }; 2538 struct qcom_scm_res res; 2539 int ret; 2540 2541 ret = qcom_scm_call_atomic(scm->dev, &desc, &res); 2542 if (ret) 2543 return ret; 2544 2545 return res.result[0] & GENMASK(7, 0); 2546 } 2547 2548 static int qcom_scm_get_waitq_irq(struct qcom_scm *scm) 2549 { 2550 struct qcom_scm_desc desc = { 2551 .svc = QCOM_SCM_SVC_WAITQ, 2552 .cmd = QCOM_SCM_WAITQ_GET_INFO, 2553 .owner = ARM_SMCCC_OWNER_SIP 2554 }; 2555 struct device_node *parent_irq_node; 2556 struct irq_fwspec fwspec; 2557 struct qcom_scm_res res; 2558 u32 hwirq; 2559 int ret; 2560 2561 ret = qcom_scm_call_atomic(scm->dev, &desc, &res); 2562 if (ret) 2563 return ret; 2564 2565 hwirq = res.result[1] & GENMASK(15, 0); 2566 ret = qcom_scm_fill_irq_fwspec_params(&fwspec, hwirq); 2567 if (ret) 2568 return ret; 2569 2570 parent_irq_node = of_irq_find_parent(scm->dev->of_node); 2571 if (!parent_irq_node) 2572 return -ENODEV; 2573 2574 fwspec.fwnode = of_fwnode_handle(parent_irq_node); 2575 2576 return irq_create_fwspec_mapping(&fwspec); 2577 } 2578 2579 static struct completion *qcom_scm_get_completion(struct qcom_scm *scm, u32 wq_ctx) 2580 { 2581 if (WARN_ON_ONCE(wq_ctx >= scm->wq_cnt)) 2582 return ERR_PTR(-EINVAL); 2583 2584 return &scm->waitq_comps[wq_ctx]; 2585 } 2586 2587 int qcom_scm_wait_for_wq_completion(struct device *dev, u32 wq_ctx) 2588 { 2589 struct qcom_scm *scm = dev_get_drvdata(dev); 2590 struct completion *wq; 2591 2592 wq = qcom_scm_get_completion(scm, wq_ctx); 2593 if (IS_ERR(wq)) 2594 return PTR_ERR(wq); 2595 2596 wait_for_completion_state(wq, TASK_IDLE); 2597 2598 return 0; 2599 } 2600 2601 static int qcom_scm_waitq_wakeup(struct qcom_scm *scm, unsigned int wq_ctx) 2602 { 2603 struct completion *wq; 2604 2605 wq = qcom_scm_get_completion(scm, wq_ctx); 2606 if (IS_ERR(wq)) 2607 return PTR_ERR(wq); 2608 2609 complete(wq); 2610 2611 return 0; 2612 } 2613 2614 static irqreturn_t qcom_scm_irq_handler(int irq, void *data) 2615 { 2616 int ret; 2617 struct qcom_scm *scm = data; 2618 u32 wq_ctx, flags, more_pending = 0; 2619 2620 do { 2621 ret = scm_get_wq_ctx(&wq_ctx, &flags, &more_pending); 2622 if (ret) { 2623 dev_err(scm->dev, "GET_WQ_CTX SMC call failed: %d\n", ret); 2624 goto out; 2625 } 2626 2627 if (flags != QCOM_SMC_WAITQ_FLAG_WAKE_ONE) { 2628 dev_err(scm->dev, "Invalid flags received for wq_ctx: %u\n", flags); 2629 goto out; 2630 } 2631 2632 ret = qcom_scm_waitq_wakeup(scm, wq_ctx); 2633 if (ret) 2634 goto out; 2635 } while (more_pending); 2636 2637 out: 2638 return IRQ_HANDLED; 2639 } 2640 2641 static int get_download_mode(char *buffer, const struct kernel_param *kp) 2642 { 2643 if (download_mode >= ARRAY_SIZE(download_mode_name)) 2644 return sysfs_emit(buffer, "unknown mode\n"); 2645 2646 return sysfs_emit(buffer, "%s\n", download_mode_name[download_mode]); 2647 } 2648 2649 static int set_download_mode(const char *val, const struct kernel_param *kp) 2650 { 2651 struct qcom_scm *scm; 2652 bool tmp; 2653 int ret; 2654 2655 ret = sysfs_match_string(download_mode_name, val); 2656 if (ret < 0) { 2657 ret = kstrtobool(val, &tmp); 2658 if (ret < 0) { 2659 pr_err("qcom_scm: err: %d\n", ret); 2660 return ret; 2661 } 2662 2663 ret = tmp ? 1 : 0; 2664 } 2665 2666 download_mode = ret; 2667 /* Pairs with smp_store_release() in qcom_scm_probe(). */ 2668 scm = smp_load_acquire(&__scm); 2669 if (scm) 2670 qcom_scm_set_download_mode(scm, download_mode); 2671 2672 return 0; 2673 } 2674 2675 static const struct kernel_param_ops download_mode_param_ops = { 2676 .get = get_download_mode, 2677 .set = set_download_mode, 2678 }; 2679 2680 module_param_cb(download_mode, &download_mode_param_ops, NULL, 0644); 2681 MODULE_PARM_DESC(download_mode, "download mode: off/0/N for no dump mode, full/on/1/Y for full dump mode, mini for minidump mode and full,mini for both full and minidump mode together are acceptable values"); 2682 2683 static int get_minidump_dest(char *buffer, const struct kernel_param *kp) 2684 { 2685 int i; 2686 2687 for (i = 0; i < ARRAY_SIZE(minidump_dest_map); i++) 2688 if (minidump_dest == minidump_dest_map[i].val) 2689 return sysfs_emit(buffer, "%s\n", minidump_dest_map[i].name); 2690 2691 return sysfs_emit(buffer, "unknown\n"); 2692 } 2693 2694 static int set_minidump_dest(const char *val, const struct kernel_param *kp) 2695 { 2696 struct qcom_scm *scm; 2697 int i; 2698 2699 for (i = 0; i < ARRAY_SIZE(minidump_dest_map); i++) 2700 if (sysfs_streq(val, minidump_dest_map[i].name)) 2701 break; 2702 2703 if (i >= ARRAY_SIZE(minidump_dest_map)) 2704 return -EINVAL; 2705 2706 minidump_dest = minidump_dest_map[i].val; 2707 2708 /* Pairs with smp_store_release() in qcom_scm_probe(). */ 2709 scm = smp_load_acquire(&__scm); 2710 if (scm && scm->minidump_sram && (download_mode & QCOM_DLOAD_MINIDUMP)) 2711 writel_relaxed(minidump_dest, scm->minidump_sram); 2712 2713 return 0; 2714 } 2715 2716 static const struct kernel_param_ops minidump_dest_param_ops = { 2717 .get = get_minidump_dest, 2718 .set = set_minidump_dest, 2719 }; 2720 2721 module_param_cb(minidump_dest, &minidump_dest_param_ops, NULL, 0644); 2722 MODULE_PARM_DESC(minidump_dest, "Minidump SRAM destination: usb (default) or storage"); 2723 2724 static int qcom_scm_probe(struct platform_device *pdev) 2725 { 2726 struct qcom_tzmem_pool_config pool_config; 2727 struct qcom_scm *scm; 2728 int irq, ret; 2729 int i; 2730 2731 scm = devm_kzalloc(&pdev->dev, sizeof(*scm), GFP_KERNEL); 2732 if (!scm) 2733 return -ENOMEM; 2734 2735 scm->dev = &pdev->dev; 2736 platform_set_drvdata(pdev, scm); 2737 ret = qcom_scm_find_dload_address(&pdev->dev, &scm->dload_mode_addr); 2738 if (ret < 0) 2739 return dev_err_probe(&pdev->dev, ret, 2740 "Failed to get download mode address\n"); 2741 2742 ret = qcom_scm_map_minidump_sram(&pdev->dev, &scm->minidump_sram); 2743 if (ret < 0) 2744 return dev_err_probe(&pdev->dev, ret, 2745 "Failed to map minidump SRAM\n"); 2746 2747 mutex_init(&scm->scm_bw_lock); 2748 2749 scm->path = devm_of_icc_get(&pdev->dev, NULL); 2750 if (IS_ERR(scm->path)) 2751 return dev_err_probe(&pdev->dev, PTR_ERR(scm->path), 2752 "failed to acquire interconnect path\n"); 2753 2754 scm->core_clk = devm_clk_get_optional(&pdev->dev, "core"); 2755 if (IS_ERR(scm->core_clk)) 2756 return PTR_ERR(scm->core_clk); 2757 2758 scm->iface_clk = devm_clk_get_optional(&pdev->dev, "iface"); 2759 if (IS_ERR(scm->iface_clk)) 2760 return PTR_ERR(scm->iface_clk); 2761 2762 scm->bus_clk = devm_clk_get_optional(&pdev->dev, "bus"); 2763 if (IS_ERR(scm->bus_clk)) 2764 return PTR_ERR(scm->bus_clk); 2765 2766 scm->reset.ops = &qcom_scm_pas_reset_ops; 2767 scm->reset.nr_resets = 1; 2768 scm->reset.of_node = pdev->dev.of_node; 2769 ret = devm_reset_controller_register(&pdev->dev, &scm->reset); 2770 if (ret) 2771 return ret; 2772 2773 /* vote for max clk rate for highest performance */ 2774 ret = clk_set_rate(scm->core_clk, INT_MAX); 2775 if (ret) 2776 return ret; 2777 2778 ret = devm_of_reserved_mem_device_init(scm->dev); 2779 if (ret && ret != -ENODEV) 2780 return dev_err_probe(scm->dev, ret, 2781 "Failed to setup the reserved memory region for TZ mem\n"); 2782 2783 ret = qcom_tzmem_enable(scm->dev); 2784 if (ret) 2785 return dev_err_probe(scm->dev, ret, 2786 "Failed to enable the TrustZone memory allocator\n"); 2787 2788 memset(&pool_config, 0, sizeof(pool_config)); 2789 pool_config.initial_size = 0; 2790 pool_config.policy = QCOM_TZMEM_POLICY_ON_DEMAND; 2791 pool_config.max_size = SZ_256K; 2792 2793 scm->mempool = devm_qcom_tzmem_pool_new(scm->dev, &pool_config); 2794 if (IS_ERR(scm->mempool)) 2795 return dev_err_probe(scm->dev, PTR_ERR(scm->mempool), 2796 "Failed to create the SCM memory pool\n"); 2797 2798 ret = qcom_scm_query_waitq_count(scm); 2799 scm->wq_cnt = ret < 0 ? QCOM_SCM_DEFAULT_WAITQ_COUNT : ret; 2800 scm->waitq_comps = devm_kcalloc(&pdev->dev, scm->wq_cnt, sizeof(*scm->waitq_comps), 2801 GFP_KERNEL); 2802 if (!scm->waitq_comps) 2803 return -ENOMEM; 2804 2805 for (i = 0; i < scm->wq_cnt; i++) 2806 init_completion(&scm->waitq_comps[i]); 2807 2808 irq = qcom_scm_get_waitq_irq(scm); 2809 if (irq < 0) 2810 irq = platform_get_irq_optional(pdev, 0); 2811 2812 if (irq < 0) { 2813 if (irq != -ENXIO) 2814 return irq; 2815 } else { 2816 ret = devm_request_threaded_irq(scm->dev, irq, NULL, qcom_scm_irq_handler, 2817 IRQF_ONESHOT, "qcom-scm", scm); 2818 if (ret < 0) 2819 return dev_err_probe(scm->dev, ret, 2820 "Failed to request qcom-scm irq\n"); 2821 } 2822 2823 /* 2824 * Paired with smp_load_acquire() in qcom_scm_is_available(). 2825 * 2826 * This marks the SCM API as ready to accept user calls and can only 2827 * be called after the TrustZone memory pool is initialized and the 2828 * waitqueue interrupt requested. 2829 */ 2830 smp_store_release(&__scm, scm); 2831 2832 __get_convention(); 2833 2834 if (qcom_scm_is_pas_available()) { 2835 qcom_pas_ops_scm.dev = scm->dev; 2836 qcom_pas_ops_register(&qcom_pas_ops_scm); 2837 } 2838 2839 /* 2840 * If "download mode" is requested, from this point on warmboot 2841 * will cause the boot stages to enter download mode, unless 2842 * disabled below by a clean shutdown/reboot. 2843 */ 2844 qcom_scm_set_download_mode(scm, download_mode); 2845 2846 /* 2847 * Disable SDI if indicated by DT that it is enabled by default. 2848 */ 2849 if (of_property_read_bool(pdev->dev.of_node, "qcom,sdi-enabled") || !download_mode) 2850 qcom_scm_disable_sdi(); 2851 2852 /* 2853 * Initialize the QSEECOM interface. 2854 * 2855 * Note: QSEECOM is fairly self-contained and this only adds the 2856 * interface device (the driver of which does most of the heavy 2857 * lifting). So any errors returned here should be either -ENOMEM or 2858 * -EINVAL (with the latter only in case there's a bug in our code). 2859 * This means that there is no need to bring down the whole SCM driver. 2860 * Just log the error instead and let SCM live. 2861 */ 2862 ret = qcom_scm_qseecom_init(scm); 2863 WARN(ret < 0, "failed to initialize qseecom: %d\n", ret); 2864 2865 /* Initialize the QTEE object interface. */ 2866 qcom_scm_qtee_init(scm); 2867 2868 /* Initialize the Gunyah watchdog platform device. */ 2869 qcom_scm_gunyah_wdt_init(scm); 2870 2871 return 0; 2872 } 2873 2874 static void qcom_scm_shutdown(struct platform_device *pdev) 2875 { 2876 /* Clean shutdown, disable download mode to allow normal restart */ 2877 qcom_scm_set_download_mode(__scm, QCOM_DLOAD_NODUMP); 2878 qcom_pas_ops_unregister(); 2879 } 2880 2881 static const struct of_device_id qcom_scm_dt_match[] = { 2882 { .compatible = "qcom,scm" }, 2883 2884 /* Legacy entries kept for backwards compatibility */ 2885 { .compatible = "qcom,scm-apq8064" }, 2886 { .compatible = "qcom,scm-apq8084" }, 2887 { .compatible = "qcom,scm-ipq4019" }, 2888 { .compatible = "qcom,scm-msm8953" }, 2889 { .compatible = "qcom,scm-msm8974" }, 2890 { .compatible = "qcom,scm-msm8996" }, 2891 {} 2892 }; 2893 MODULE_DEVICE_TABLE(of, qcom_scm_dt_match); 2894 2895 static struct platform_driver qcom_scm_driver = { 2896 .driver = { 2897 .name = "qcom_scm", 2898 .of_match_table = qcom_scm_dt_match, 2899 .suppress_bind_attrs = true, 2900 }, 2901 .probe = qcom_scm_probe, 2902 .shutdown = qcom_scm_shutdown, 2903 }; 2904 2905 static int __init qcom_scm_init(void) 2906 { 2907 return platform_driver_register(&qcom_scm_driver); 2908 } 2909 subsys_initcall(qcom_scm_init); 2910 2911 MODULE_DESCRIPTION("Qualcomm Technologies, Inc. SCM driver"); 2912 MODULE_LICENSE("GPL v2"); 2913