Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / net / ethernet / ti / k3-cppi-desc-pool.c
blob739bae8e11ee9297379d18a20a3e63dbc420b085
1 // SPDX-License-Identifier: GPL-2.0
2 /* TI K3 CPPI5 descriptors pool API
4 * Copyright (C) 2020 Texas Instruments Incorporated - http://www.ti.com
5 */
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
15 #include "k3-cppi-desc-pool.h"
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;
28 void k3_cppi_desc_pool_destroy(struct k3_cppi_desc_pool *pool)
30 if (!pool)
31 return;
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);
41 kfree(pool->desc_infos);
43 gen_pool_destroy(pool->gen_pool); /* frees pool->name */
45 kfree(pool);
47 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_destroy);
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)
54 struct k3_cppi_desc_pool *pool;
55 const char *pool_name = NULL;
56 int ret = -ENOMEM;
58 pool = kzalloc(sizeof(*pool), GFP_KERNEL);
59 if (!pool)
60 return ERR_PTR(ret);
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;
67 pool_name = kstrdup_const(name ? name : dev_name(pool->dev),
68 GFP_KERNEL);
69 if (!pool_name)
70 goto gen_pool_create_fail;
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;
78 pool->gen_pool->name = pool_name;
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;
85 pool->cpumem = dma_alloc_coherent(pool->dev, pool->mem_size,
86 &pool->dma_addr, GFP_KERNEL);
88 if (!pool->cpumem)
89 goto dma_alloc_fail;
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;
99 return pool;
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);
112 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_create_name);
114 dma_addr_t k3_cppi_desc_pool_virt2dma(struct k3_cppi_desc_pool *pool,
115 void *addr)
117 return addr ? pool->dma_addr + (addr - pool->cpumem) : 0;
119 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_virt2dma);
121 void *k3_cppi_desc_pool_dma2virt(struct k3_cppi_desc_pool *pool, dma_addr_t dma)
123 return dma ? pool->cpumem + (dma - pool->dma_addr) : NULL;
125 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_dma2virt);
127 void *k3_cppi_desc_pool_alloc(struct k3_cppi_desc_pool *pool)
129 return (void *)gen_pool_alloc(pool->gen_pool, pool->desc_size);
131 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_alloc);
133 void k3_cppi_desc_pool_free(struct k3_cppi_desc_pool *pool, void *addr)
135 gen_pool_free(pool->gen_pool, (unsigned long)addr, pool->desc_size);
137 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_free);
139 size_t k3_cppi_desc_pool_avail(struct k3_cppi_desc_pool *pool)
141 return gen_pool_avail(pool->gen_pool) / pool->desc_size;
143 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_avail);
145 size_t k3_cppi_desc_pool_desc_size(const struct k3_cppi_desc_pool *pool)
147 return pool->desc_size;
149 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_desc_size);
151 void *k3_cppi_desc_pool_cpuaddr(const struct k3_cppi_desc_pool *pool)
153 return pool->cpumem;
155 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_cpuaddr);
157 void k3_cppi_desc_pool_desc_info_set(struct k3_cppi_desc_pool *pool,
158 int desc_idx, void *info)
160 pool->desc_infos[desc_idx] = info;
162 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_desc_info_set);
164 void *k3_cppi_desc_pool_desc_info(const struct k3_cppi_desc_pool *pool,
165 int desc_idx)
167 return pool->desc_infos[desc_idx];
169 EXPORT_SYMBOL_GPL(k3_cppi_desc_pool_desc_info);
171 MODULE_LICENSE("GPL");
172 MODULE_DESCRIPTION("TI K3 CPPI5 descriptors pool API");