1 // SPDX-License-Identifier: GPL-2.0 2 /* TI K3 CPPI5 descriptors pool API 3 * 4 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/device.h> 10 #include <linux/dma-mapping.h> 11 #include <linux/err.h> 12 #include <linux/genalloc.h> 13 #include <linux/kernel.h> 14 15 #include "k3-cppi-desc-pool.h" 16 17 struct k3_cppi_desc_pool { 18 struct device *dev; 19 dma_addr_t dma_addr; 20 void *cpumem; /* dma_alloc map */ 21 size_t desc_size; 22 size_t mem_size; 23 size_t num_desc; 24 struct gen_pool *gen_pool; 25 void **desc_infos; 26 }; 27 28 void k3_cppi_desc_pool_destroy(struct k3_cppi_desc_pool *pool) 29 { 30 if (!pool) 31 return; 32 33 WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool), 34 "k3_knav_desc_pool size %zu != avail %zu", 35 gen_pool_size(pool->gen_pool), 36 gen_pool_avail(pool->gen_pool)); 37 if (pool->cpumem) 38 dma_free_coherent(pool->dev, pool->mem_size, pool->cpumem, 39 pool->dma_addr); 40 41 kfree(pool->desc_infos); 42 43 gen_pool_destroy(pool->gen_pool); /* frees pool->name */ 44 45 kfree(pool); 46 } 47 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_destroy); 48 49 struct k3_cppi_desc_pool * 50 k3_cppi_desc_pool_create_name(struct device *dev, size_t size, 51 size_t desc_size, 52 const char *name) 53 { 54 struct k3_cppi_desc_pool *pool; 55 const char *pool_name = NULL; 56 int ret = -ENOMEM; 57 58 pool = kzalloc(sizeof(*pool), GFP_KERNEL); 59 if (!pool) 60 return ERR_PTR(ret); 61 62 pool->dev = dev; 63 pool->desc_size = roundup_pow_of_two(desc_size); 64 pool->num_desc = size; 65 pool->mem_size = pool->num_desc * pool->desc_size; 66 67 pool_name = kstrdup_const(name ? name : dev_name(pool->dev), 68 GFP_KERNEL); 69 if (!pool_name) 70 goto gen_pool_create_fail; 71 72 pool->gen_pool = gen_pool_create(ilog2(pool->desc_size), -1); 73 if (!pool->gen_pool) { 74 kfree_const(pool_name); 75 goto gen_pool_create_fail; 76 } 77 78 pool->gen_pool->name = pool_name; 79 80 pool->desc_infos = kcalloc(pool->num_desc, 81 sizeof(*pool->desc_infos), GFP_KERNEL); 82 if (!pool->desc_infos) 83 goto gen_pool_desc_infos_alloc_fail; 84 85 pool->cpumem = dma_alloc_coherent(pool->dev, pool->mem_size, 86 &pool->dma_addr, GFP_KERNEL); 87 88 if (!pool->cpumem) 89 goto dma_alloc_fail; 90 91 ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->cpumem, 92 (phys_addr_t)pool->dma_addr, pool->mem_size, 93 -1); 94 if (ret < 0) { 95 dev_err(pool->dev, "pool add failed %d\n", ret); 96 goto gen_pool_add_virt_fail; 97 } 98 99 return pool; 100 101 gen_pool_add_virt_fail: 102 dma_free_coherent(pool->dev, pool->mem_size, pool->cpumem, 103 pool->dma_addr); 104 dma_alloc_fail: 105 kfree(pool->desc_infos); 106 gen_pool_desc_infos_alloc_fail: 107 gen_pool_destroy(pool->gen_pool); /* frees pool->name */ 108 gen_pool_create_fail: 109 kfree(pool); 110 return ERR_PTR(ret); 111 } 112 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_create_name); 113 114 dma_addr_t k3_cppi_desc_pool_virt2dma(struct k3_cppi_desc_pool *pool, 115 void *addr) 116 { 117 return addr ? pool->dma_addr + (addr - pool->cpumem) : 0; 118 } 119 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_virt2dma); 120 121 void *k3_cppi_desc_pool_dma2virt(struct k3_cppi_desc_pool *pool, dma_addr_t dma) 122 { 123 return dma ? pool->cpumem + (dma - pool->dma_addr) : NULL; 124 } 125 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_dma2virt); 126 127 void *k3_cppi_desc_pool_alloc(struct k3_cppi_desc_pool *pool) 128 { 129 return (void *)gen_pool_alloc(pool->gen_pool, pool->desc_size); 130 } 131 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_alloc); 132 133 void k3_cppi_desc_pool_free(struct k3_cppi_desc_pool *pool, void *addr) 134 { 135 gen_pool_free(pool->gen_pool, (unsigned long)addr, pool->desc_size); 136 } 137 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_free); 138 139 size_t k3_cppi_desc_pool_avail(struct k3_cppi_desc_pool *pool) 140 { 141 return gen_pool_avail(pool->gen_pool) / pool->desc_size; 142 } 143 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_avail); 144 145 size_t k3_cppi_desc_pool_desc_size(const struct k3_cppi_desc_pool *pool) 146 { 147 return pool->desc_size; 148 } 149 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_desc_size); 150 151 void *k3_cppi_desc_pool_cpuaddr(const struct k3_cppi_desc_pool *pool) 152 { 153 return pool->cpumem; 154 } 155 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_cpuaddr); 156 157 void k3_cppi_desc_pool_desc_info_set(struct k3_cppi_desc_pool *pool, 158 int desc_idx, void *info) 159 { 160 pool->desc_infos[desc_idx] = info; 161 } 162 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_desc_info_set); 163 164 void *k3_cppi_desc_pool_desc_info(const struct k3_cppi_desc_pool *pool, 165 int desc_idx) 166 { 167 return pool->desc_infos[desc_idx]; 168 } 169 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_desc_info); 170 171 MODULE_LICENSE("GPL"); 172 MODULE_DESCRIPTION("TI K3 CPPI5 descriptors pool API"); 173