at commit c03c1a14
| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Memory allocator for buffers shared with the TrustZone. |
| 4 | * |
| 5 | * Copyright (C) 2023-2024 Linaro Ltd. |
| 6 | */ |
| 7 | |
| 8 | #include <linux/bug.h> |
| 9 | #include <linux/cleanup.h> |
| 10 | #include <linux/dma-mapping.h> |
| 11 | #include <linux/err.h> |
| 12 | #include <linux/firmware/qcom/qcom_tzmem.h> |
| 13 | #include <linux/genalloc.h> |
| 14 | #include <linux/gfp.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/once.h> |
| 19 | #include <linux/radix-tree.h> |
| 20 | #include <linux/slab.h> |
| 21 | #include <linux/spinlock.h> |
| 22 | #include <linux/types.h> |
| 23 | |
| 24 | #include "qcom_scm.h" |
| 25 | #include "qcom_tzmem.h" |
| 26 | |
| 27 | struct qcom_tzmem_area { |
| 28 | struct list_head list; |
| 29 | void *vaddr; |
| 30 | dma_addr_t paddr; |
| 31 | size_t size; |
| 32 | void *priv; |
| 33 | }; |
| 34 | |
| 35 | struct qcom_tzmem_pool { |
| 36 | struct gen_pool *genpool; |
| 37 | struct list_head areas; |
| 38 | enum qcom_tzmem_policy policy; |
| 39 | size_t increment; |
| 40 | size_t max_size; |
| 41 | spinlock_t lock; |
| 42 | }; |
| 43 | |
| 44 | struct qcom_tzmem_chunk { |
| 45 | size_t size; |
| 46 | struct qcom_tzmem_pool *owner; |
| 47 | }; |
| 48 | |
| 49 | static struct device *qcom_tzmem_dev; |
| 50 | static RADIX_TREE(qcom_tzmem_chunks, GFP_ATOMIC); |
| 51 | static DEFINE_SPINLOCK(qcom_tzmem_chunks_lock); |
| 52 | |
| 53 | #if IS_ENABLED(CONFIG_QCOM_TZMEM_MODE_GENERIC) |
| 54 | |
| 55 | static int qcom_tzmem_init(void) |
| 56 | { |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static int qcom_tzmem_init_area(struct qcom_tzmem_area *area) |
| 61 | { |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | static void qcom_tzmem_cleanup_area(struct qcom_tzmem_area *area) |
| 66 | { |
| 67 | |
| 68 | } |
| 69 | |
| 70 | #elif IS_ENABLED(CONFIG_QCOM_TZMEM_MODE_SHMBRIDGE) |
| 71 | |
| 72 | #include <linux/firmware/qcom/qcom_scm.h> |
| 73 | #include <linux/of.h> |
| 74 | |
| 75 | #define QCOM_SHM_BRIDGE_NUM_VM_SHIFT 9 |
| 76 | |
| 77 | static bool qcom_tzmem_using_shm_bridge; |
| 78 | |
| 79 | /* List of machines that are known to not support SHM bridge correctly. */ |
| 80 | static const char *const qcom_tzmem_blacklist[] = { |
| 81 | "qcom,sc7180", /* hang in rmtfs memory assignment */ |
| 82 | "qcom,sc8180x", |
| 83 | "qcom,sdm670", /* failure in GPU firmware loading */ |
| 84 | "qcom,sdm845", /* reset in rmtfs memory assignment */ |
| 85 | "qcom,sm7150", /* reset in rmtfs memory assignment */ |
| 86 | "qcom,sm8150", /* reset in rmtfs memory assignment */ |
| 87 | NULL |
| 88 | }; |
| 89 | |
| 90 | static int qcom_tzmem_init(void) |
| 91 | { |
| 92 | const char *const *platform; |
| 93 | int ret; |
| 94 | |
| 95 | for (platform = qcom_tzmem_blacklist; *platform; platform++) { |
| 96 | if (of_machine_is_compatible(*platform)) |
| 97 | goto notsupp; |
| 98 | } |
| 99 | |
| 100 | ret = qcom_scm_shm_bridge_enable(qcom_tzmem_dev); |
| 101 | if (ret == -EOPNOTSUPP) |
| 102 | goto notsupp; |
| 103 | |
| 104 | if (!ret) |
| 105 | qcom_tzmem_using_shm_bridge = true; |
| 106 | |
| 107 | return ret; |
| 108 | |
| 109 | notsupp: |
| 110 | dev_info(qcom_tzmem_dev, "SHM Bridge not supported\n"); |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * qcom_tzmem_shm_bridge_create() - Create a SHM bridge. |
| 116 | * @paddr: Physical address of the memory to share. |
| 117 | * @size: Size of the memory to share. |
| 118 | * @handle: Handle to the SHM bridge. |
| 119 | * |
| 120 | * On platforms that support SHM bridge, this function creates a SHM bridge |
| 121 | * for the given memory region with QTEE. The handle returned by this function |
| 122 | * must be passed to qcom_tzmem_shm_bridge_delete() to free the SHM bridge. |
| 123 | * |
| 124 | * Return: On success, returns 0; on failure, returns < 0. |
| 125 | */ |
| 126 | int qcom_tzmem_shm_bridge_create(phys_addr_t paddr, size_t size, u64 *handle) |
| 127 | { |
| 128 | u64 pfn_and_ns_perm, ipfn_and_s_perm, size_and_flags; |
| 129 | int ret; |
| 130 | |
| 131 | if (!qcom_tzmem_using_shm_bridge) |
| 132 | return 0; |
| 133 | |
| 134 | pfn_and_ns_perm = paddr | QCOM_SCM_PERM_RW; |
| 135 | ipfn_and_s_perm = paddr | QCOM_SCM_PERM_RW; |
| 136 | size_and_flags = size | (1 << QCOM_SHM_BRIDGE_NUM_VM_SHIFT); |
| 137 | |
| 138 | ret = qcom_scm_shm_bridge_create(pfn_and_ns_perm, ipfn_and_s_perm, |
| 139 | size_and_flags, QCOM_SCM_VMID_HLOS, |
| 140 | handle); |
| 141 | if (ret) { |
| 142 | dev_err(qcom_tzmem_dev, |
| 143 | "SHM Bridge failed: ret %d paddr 0x%pa, size %zu\n", |
| 144 | ret, &paddr, size); |
| 145 | |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | return 0; |
| 150 | } |
| 151 | EXPORT_SYMBOL_GPL(qcom_tzmem_shm_bridge_create); |
| 152 | |
| 153 | /** |
| 154 | * qcom_tzmem_shm_bridge_delete() - Delete a SHM bridge. |
| 155 | * @handle: Handle to the SHM bridge. |
| 156 | * |
| 157 | * On platforms that support SHM bridge, this function deletes the SHM bridge |
| 158 | * for the given memory region. The handle must be the same as the one |
| 159 | * returned by qcom_tzmem_shm_bridge_create(). |
| 160 | */ |
| 161 | void qcom_tzmem_shm_bridge_delete(u64 handle) |
| 162 | { |
| 163 | if (qcom_tzmem_using_shm_bridge) |
| 164 | qcom_scm_shm_bridge_delete(handle); |
| 165 | } |
| 166 | EXPORT_SYMBOL_GPL(qcom_tzmem_shm_bridge_delete); |
| 167 | |
| 168 | static int qcom_tzmem_init_area(struct qcom_tzmem_area *area) |
| 169 | { |
| 170 | int ret; |
| 171 | |
| 172 | u64 *handle __free(kfree) = kzalloc_obj(*handle); |
| 173 | if (!handle) |
| 174 | return -ENOMEM; |
| 175 | |
| 176 | ret = qcom_tzmem_shm_bridge_create(area->paddr, area->size, handle); |
| 177 | if (ret) |
| 178 | return ret; |
| 179 | |
| 180 | area->priv = no_free_ptr(handle); |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static void qcom_tzmem_cleanup_area(struct qcom_tzmem_area *area) |
| 186 | { |
| 187 | u64 *handle = area->priv; |
| 188 | |
| 189 | qcom_tzmem_shm_bridge_delete(*handle); |
| 190 | kfree(handle); |
| 191 | } |
| 192 | |
| 193 | #endif /* CONFIG_QCOM_TZMEM_MODE_SHMBRIDGE */ |
| 194 | |
| 195 | static int qcom_tzmem_pool_add_memory(struct qcom_tzmem_pool *pool, |
| 196 | size_t size, gfp_t gfp) |
| 197 | { |
| 198 | int ret; |
| 199 | |
| 200 | struct qcom_tzmem_area *area __free(kfree) = kzalloc_obj(*area, gfp); |
| 201 | if (!area) |
| 202 | return -ENOMEM; |
| 203 | |
| 204 | area->size = PAGE_ALIGN(size); |
| 205 | |
| 206 | area->vaddr = dma_alloc_coherent(qcom_tzmem_dev, area->size, |
| 207 | &area->paddr, gfp); |
| 208 | if (!area->vaddr) |
| 209 | return -ENOMEM; |
| 210 | |
| 211 | ret = qcom_tzmem_init_area(area); |
| 212 | if (ret) { |
| 213 | dma_free_coherent(qcom_tzmem_dev, area->size, |
| 214 | area->vaddr, area->paddr); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | ret = gen_pool_add_virt(pool->genpool, (unsigned long)area->vaddr, |
| 219 | (phys_addr_t)area->paddr, size, -1); |
| 220 | if (ret) { |
| 221 | dma_free_coherent(qcom_tzmem_dev, area->size, |
| 222 | area->vaddr, area->paddr); |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | scoped_guard(spinlock_irqsave, &pool->lock) |
| 227 | list_add_tail(&area->list, &pool->areas); |
| 228 | |
| 229 | area = NULL; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * qcom_tzmem_pool_new() - Create a new TZ memory pool. |
| 235 | * @config: Pool configuration. |
| 236 | * |
| 237 | * Create a new pool of memory suitable for sharing with the TrustZone. |
| 238 | * |
| 239 | * Must not be used in atomic context. |
| 240 | * |
| 241 | * Return: New memory pool address or ERR_PTR() on error. |
| 242 | */ |
| 243 | struct qcom_tzmem_pool * |
| 244 | qcom_tzmem_pool_new(const struct qcom_tzmem_pool_config *config) |
| 245 | { |
| 246 | int ret = -ENOMEM; |
| 247 | |
| 248 | might_sleep(); |
| 249 | |
| 250 | switch (config->policy) { |
| 251 | case QCOM_TZMEM_POLICY_STATIC: |
| 252 | if (!config->initial_size) |
| 253 | return ERR_PTR(-EINVAL); |
| 254 | break; |
| 255 | case QCOM_TZMEM_POLICY_MULTIPLIER: |
| 256 | if (!config->increment) |
| 257 | return ERR_PTR(-EINVAL); |
| 258 | break; |
| 259 | case QCOM_TZMEM_POLICY_ON_DEMAND: |
| 260 | break; |
| 261 | default: |
| 262 | return ERR_PTR(-EINVAL); |
| 263 | } |
| 264 | |
| 265 | struct qcom_tzmem_pool *pool __free(kfree) = kzalloc_obj(*pool); |
| 266 | if (!pool) |
| 267 | return ERR_PTR(-ENOMEM); |
| 268 | |
| 269 | pool->genpool = gen_pool_create(PAGE_SHIFT, -1); |
| 270 | if (!pool->genpool) |
| 271 | return ERR_PTR(-ENOMEM); |
| 272 | |
| 273 | gen_pool_set_algo(pool->genpool, gen_pool_best_fit, NULL); |
| 274 | |
| 275 | pool->policy = config->policy; |
| 276 | pool->increment = config->increment; |
| 277 | pool->max_size = config->max_size; |
| 278 | INIT_LIST_HEAD(&pool->areas); |
| 279 | spin_lock_init(&pool->lock); |
| 280 | |
| 281 | if (config->initial_size) { |
| 282 | ret = qcom_tzmem_pool_add_memory(pool, config->initial_size, |
| 283 | GFP_KERNEL); |
| 284 | if (ret) { |
| 285 | gen_pool_destroy(pool->genpool); |
| 286 | return ERR_PTR(ret); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | return_ptr(pool); |
| 291 | } |
| 292 | EXPORT_SYMBOL_GPL(qcom_tzmem_pool_new); |
| 293 | |
| 294 | /** |
| 295 | * qcom_tzmem_pool_free() - Destroy a TZ memory pool and free all resources. |
| 296 | * @pool: Memory pool to free. |
| 297 | * |
| 298 | * Must not be called if any of the allocated chunks has not been freed. |
| 299 | * Must not be used in atomic context. |
| 300 | */ |
| 301 | void qcom_tzmem_pool_free(struct qcom_tzmem_pool *pool) |
| 302 | { |
| 303 | struct qcom_tzmem_area *area, *next; |
| 304 | struct qcom_tzmem_chunk *chunk; |
| 305 | struct radix_tree_iter iter; |
| 306 | bool non_empty = false; |
| 307 | void __rcu **slot; |
| 308 | |
| 309 | might_sleep(); |
| 310 | |
| 311 | if (!pool) |
| 312 | return; |
| 313 | |
| 314 | scoped_guard(spinlock_irqsave, &qcom_tzmem_chunks_lock) { |
| 315 | radix_tree_for_each_slot(slot, &qcom_tzmem_chunks, &iter, 0) { |
| 316 | chunk = radix_tree_deref_slot_protected(slot, |
| 317 | &qcom_tzmem_chunks_lock); |
| 318 | |
| 319 | if (chunk->owner == pool) |
| 320 | non_empty = true; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | WARN(non_empty, "Freeing TZ memory pool with memory still allocated"); |
| 325 | |
| 326 | list_for_each_entry_safe(area, next, &pool->areas, list) { |
| 327 | list_del(&area->list); |
| 328 | qcom_tzmem_cleanup_area(area); |
| 329 | dma_free_coherent(qcom_tzmem_dev, area->size, |
| 330 | area->vaddr, area->paddr); |
| 331 | kfree(area); |
| 332 | } |
| 333 | |
| 334 | gen_pool_destroy(pool->genpool); |
| 335 | kfree(pool); |
| 336 | } |
| 337 | EXPORT_SYMBOL_GPL(qcom_tzmem_pool_free); |
| 338 | |
| 339 | static void devm_qcom_tzmem_pool_free(void *data) |
| 340 | { |
| 341 | struct qcom_tzmem_pool *pool = data; |
| 342 | |
| 343 | qcom_tzmem_pool_free(pool); |
| 344 | } |
| 345 | |
| 346 | /** |
| 347 | * devm_qcom_tzmem_pool_new() - Managed variant of qcom_tzmem_pool_new(). |
| 348 | * @dev: Device managing this resource. |
| 349 | * @config: Pool configuration. |
| 350 | * |
| 351 | * Must not be used in atomic context. |
| 352 | * |
| 353 | * Return: Address of the managed pool or ERR_PTR() on failure. |
| 354 | */ |
| 355 | struct qcom_tzmem_pool * |
| 356 | devm_qcom_tzmem_pool_new(struct device *dev, |
| 357 | const struct qcom_tzmem_pool_config *config) |
| 358 | { |
| 359 | struct qcom_tzmem_pool *pool; |
| 360 | int ret; |
| 361 | |
| 362 | pool = qcom_tzmem_pool_new(config); |
| 363 | if (IS_ERR(pool)) |
| 364 | return pool; |
| 365 | |
| 366 | ret = devm_add_action_or_reset(dev, devm_qcom_tzmem_pool_free, pool); |
| 367 | if (ret) |
| 368 | return ERR_PTR(ret); |
| 369 | |
| 370 | return pool; |
| 371 | } |
| 372 | EXPORT_SYMBOL_GPL(devm_qcom_tzmem_pool_new); |
| 373 | |
| 374 | static bool qcom_tzmem_try_grow_pool(struct qcom_tzmem_pool *pool, |
| 375 | size_t requested, gfp_t gfp) |
| 376 | { |
| 377 | size_t current_size = gen_pool_size(pool->genpool); |
| 378 | |
| 379 | if (pool->max_size && (current_size + requested) > pool->max_size) |
| 380 | return false; |
| 381 | |
| 382 | switch (pool->policy) { |
| 383 | case QCOM_TZMEM_POLICY_STATIC: |
| 384 | return false; |
| 385 | case QCOM_TZMEM_POLICY_MULTIPLIER: |
| 386 | requested = current_size * pool->increment; |
| 387 | break; |
| 388 | case QCOM_TZMEM_POLICY_ON_DEMAND: |
| 389 | break; |
| 390 | } |
| 391 | |
| 392 | return !qcom_tzmem_pool_add_memory(pool, requested, gfp); |
| 393 | } |
| 394 | |
| 395 | /** |
| 396 | * qcom_tzmem_alloc() - Allocate a memory chunk suitable for sharing with TZ. |
| 397 | * @pool: TZ memory pool from which to allocate memory. |
| 398 | * @size: Number of bytes to allocate. |
| 399 | * @gfp: GFP flags. |
| 400 | * |
| 401 | * Can be used in any context. |
| 402 | * |
| 403 | * Return: |
| 404 | * Address of the allocated buffer or NULL if no more memory can be allocated. |
| 405 | * The buffer must be released using qcom_tzmem_free(). |
| 406 | */ |
| 407 | void *qcom_tzmem_alloc(struct qcom_tzmem_pool *pool, size_t size, gfp_t gfp) |
| 408 | { |
| 409 | unsigned long vaddr; |
| 410 | int ret; |
| 411 | |
| 412 | if (!size) |
| 413 | return NULL; |
| 414 | |
| 415 | size = PAGE_ALIGN(size); |
| 416 | |
| 417 | struct qcom_tzmem_chunk *chunk __free(kfree) = kzalloc_obj(*chunk, gfp); |
| 418 | if (!chunk) |
| 419 | return NULL; |
| 420 | |
| 421 | again: |
| 422 | vaddr = gen_pool_alloc(pool->genpool, size); |
| 423 | if (!vaddr) { |
| 424 | if (qcom_tzmem_try_grow_pool(pool, size, gfp)) |
| 425 | goto again; |
| 426 | |
| 427 | return NULL; |
| 428 | } |
| 429 | |
| 430 | chunk->size = size; |
| 431 | chunk->owner = pool; |
| 432 | |
| 433 | scoped_guard(spinlock_irqsave, &qcom_tzmem_chunks_lock) { |
| 434 | ret = radix_tree_insert(&qcom_tzmem_chunks, vaddr, chunk); |
| 435 | if (ret) { |
| 436 | gen_pool_free(pool->genpool, vaddr, size); |
| 437 | return NULL; |
| 438 | } |
| 439 | |
| 440 | chunk = NULL; |
| 441 | } |
| 442 | |
| 443 | return (void *)vaddr; |
| 444 | } |
| 445 | EXPORT_SYMBOL_GPL(qcom_tzmem_alloc); |
| 446 | |
| 447 | /** |
| 448 | * qcom_tzmem_free() - Release a buffer allocated from a TZ memory pool. |
| 449 | * @vaddr: Virtual address of the buffer. |
| 450 | * |
| 451 | * Can be used in any context. |
| 452 | */ |
| 453 | void qcom_tzmem_free(void *vaddr) |
| 454 | { |
| 455 | struct qcom_tzmem_chunk *chunk; |
| 456 | |
| 457 | scoped_guard(spinlock_irqsave, &qcom_tzmem_chunks_lock) |
| 458 | chunk = radix_tree_delete_item(&qcom_tzmem_chunks, |
| 459 | (unsigned long)vaddr, NULL); |
| 460 | |
| 461 | if (!chunk) { |
| 462 | WARN(1, "Virtual address %p not owned by TZ memory allocator", |
| 463 | vaddr); |
| 464 | return; |
| 465 | } |
| 466 | |
| 467 | scoped_guard(spinlock_irqsave, &chunk->owner->lock) |
| 468 | gen_pool_free(chunk->owner->genpool, (unsigned long)vaddr, |
| 469 | chunk->size); |
| 470 | kfree(chunk); |
| 471 | } |
| 472 | EXPORT_SYMBOL_GPL(qcom_tzmem_free); |
| 473 | |
| 474 | /** |
| 475 | * qcom_tzmem_to_phys() - Map the virtual address of TZ memory to physical. |
| 476 | * @vaddr: Virtual address of memory allocated from a TZ memory pool. |
| 477 | * |
| 478 | * Can be used in any context. The address must point to memory allocated |
| 479 | * using qcom_tzmem_alloc(). |
| 480 | * |
| 481 | * Returns: |
| 482 | * Physical address mapped from the virtual or 0 if the mapping failed. |
| 483 | */ |
| 484 | phys_addr_t qcom_tzmem_to_phys(void *vaddr) |
| 485 | { |
| 486 | struct qcom_tzmem_chunk *chunk; |
| 487 | struct radix_tree_iter iter; |
| 488 | void __rcu **slot; |
| 489 | phys_addr_t ret; |
| 490 | |
| 491 | guard(spinlock_irqsave)(&qcom_tzmem_chunks_lock); |
| 492 | |
| 493 | radix_tree_for_each_slot(slot, &qcom_tzmem_chunks, &iter, 0) { |
| 494 | chunk = radix_tree_deref_slot_protected(slot, |
| 495 | &qcom_tzmem_chunks_lock); |
| 496 | |
| 497 | ret = gen_pool_virt_to_phys(chunk->owner->genpool, |
| 498 | (unsigned long)vaddr); |
| 499 | if (ret == -1) |
| 500 | continue; |
| 501 | |
| 502 | return ret; |
| 503 | } |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | EXPORT_SYMBOL_GPL(qcom_tzmem_to_phys); |
| 508 | |
| 509 | static void qcom_tzmem_do_init(int *result) |
| 510 | { |
| 511 | *result = qcom_tzmem_init(); |
| 512 | } |
| 513 | |
| 514 | int qcom_tzmem_enable(struct device *dev) |
| 515 | { |
| 516 | static int result; |
| 517 | |
| 518 | qcom_tzmem_dev = dev; |
| 519 | DO_ONCE(qcom_tzmem_do_init, &result); |
| 520 | return result; |
| 521 | } |
| 522 | EXPORT_SYMBOL_GPL(qcom_tzmem_enable); |
| 523 | |
| 524 | MODULE_DESCRIPTION("TrustZone memory allocator for Qualcomm firmware drivers"); |
| 525 | MODULE_AUTHOR("Bartosz Golaszewski <bartosz.golaszewski@linaro.org>"); |
| 526 | MODULE_LICENSE("GPL"); |