| 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* |
| 3 | * Copyright (C) 2023-2024 Linaro Ltd. |
| 4 | */ |
| 5 | |
| 6 | #ifndef __QCOM_TZMEM_H |
| 7 | #define __QCOM_TZMEM_H |
| 8 | |
| 9 | #include <linux/cleanup.h> |
| 10 | #include <linux/gfp.h> |
| 11 | #include <linux/types.h> |
| 12 | |
| 13 | struct device; |
| 14 | struct qcom_tzmem_pool; |
| 15 | |
| 16 | /** |
| 17 | * enum qcom_tzmem_policy - Policy for pool growth. |
| 18 | */ |
| 19 | enum qcom_tzmem_policy { |
| 20 | /** |
| 21 | * @QCOM_TZMEM_POLICY_STATIC: Static pool, |
| 22 | * never grow above initial size. |
| 23 | */ |
| 24 | QCOM_TZMEM_POLICY_STATIC = 1, |
| 25 | /** |
| 26 | * @QCOM_TZMEM_POLICY_MULTIPLIER: When out of memory, |
| 27 | * add increment * current size of memory. |
| 28 | */ |
| 29 | QCOM_TZMEM_POLICY_MULTIPLIER, |
| 30 | /** |
| 31 | * @QCOM_TZMEM_POLICY_ON_DEMAND: When out of memory |
| 32 | * add as much as is needed until max_size. |
| 33 | */ |
| 34 | QCOM_TZMEM_POLICY_ON_DEMAND, |
| 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * struct qcom_tzmem_pool_config - TZ memory pool configuration. |
| 39 | * @initial_size: Number of bytes to allocate for the pool during its creation. |
| 40 | * @policy: Pool size growth policy. |
| 41 | * @increment: Used with policies that allow pool growth. |
| 42 | * @max_size: Size above which the pool will never grow. |
| 43 | */ |
| 44 | struct qcom_tzmem_pool_config { |
| 45 | size_t initial_size; |
| 46 | enum qcom_tzmem_policy policy; |
| 47 | size_t increment; |
| 48 | size_t max_size; |
| 49 | }; |
| 50 | |
| 51 | struct qcom_tzmem_pool * |
| 52 | qcom_tzmem_pool_new(const struct qcom_tzmem_pool_config *config); |
| 53 | void qcom_tzmem_pool_free(struct qcom_tzmem_pool *pool); |
| 54 | struct qcom_tzmem_pool * |
| 55 | devm_qcom_tzmem_pool_new(struct device *dev, |
| 56 | const struct qcom_tzmem_pool_config *config); |
| 57 | |
| 58 | void *qcom_tzmem_alloc(struct qcom_tzmem_pool *pool, size_t size, gfp_t gfp); |
| 59 | void qcom_tzmem_free(void *ptr); |
| 60 | |
| 61 | DEFINE_FREE(qcom_tzmem, void *, if (_T) qcom_tzmem_free(_T)) |
| 62 | |
| 63 | phys_addr_t qcom_tzmem_to_phys(void *ptr); |
| 64 | |
| 65 | #if IS_ENABLED(CONFIG_QCOM_TZMEM_MODE_SHMBRIDGE) |
| 66 | int qcom_tzmem_shm_bridge_create(phys_addr_t paddr, size_t size, u64 *handle); |
| 67 | void qcom_tzmem_shm_bridge_delete(u64 handle); |
| 68 | #else |
| 69 | static inline int qcom_tzmem_shm_bridge_create(phys_addr_t paddr, |
| 70 | size_t size, u64 *handle) |
| 71 | { |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static inline void qcom_tzmem_shm_bridge_delete(u64 handle) |
| 76 | { |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | #endif /* __QCOM_TZMEM */ |
| 81 | |