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
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
{
20 void *cpumem
; /* dma_alloc map */
24 struct gen_pool
*gen_pool
;
28 void k3_cppi_desc_pool_destroy(struct k3_cppi_desc_pool
*pool
)
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
));
38 dma_free_coherent(pool
->dev
, pool
->mem_size
, pool
->cpumem
,
41 kfree(pool
->desc_infos
);
43 gen_pool_destroy(pool
->gen_pool
); /* frees pool->name */
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
,
54 struct k3_cppi_desc_pool
*pool
;
55 const char *pool_name
= NULL
;
58 pool
= kzalloc(sizeof(*pool
), GFP_KERNEL
);
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
),
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
);
91 ret
= gen_pool_add_virt(pool
->gen_pool
, (unsigned long)pool
->cpumem
,
92 (phys_addr_t
)pool
->dma_addr
, pool
->mem_size
,
95 dev_err(pool
->dev
, "pool add failed %d\n", ret
);
96 goto gen_pool_add_virt_fail
;
101 gen_pool_add_virt_fail
:
102 dma_free_coherent(pool
->dev
, pool
->mem_size
, pool
->cpumem
,
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
:
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
,
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
)
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
,
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");