Lines Matching +full:block +full:- +full:size
1 // SPDX-License-Identifier: GPL-2.0-only
9 * This allocator returns small blocks of a given size which are DMA-able by
11 * new pages, then splits them up into blocks of the required size.
15 * represented by the 'struct dma_pool' which keeps a doubly-linked list of
17 * least 'size' bytes. Free blocks are tracked in an unsorted singly-linked
23 #include <linux/dma-mapping.h>
56 unsigned int size;
76 unsigned size;
78 size = sysfs_emit(buf, "poolinfo - 0.1\n");
81 list_for_each_entry(pool, &dev->dma_pools, pools) {
82 /* per-pool info, no real statistics yet */
83 size += sysfs_emit_at(buf, size, "%-16s %4zu %4zu %4u %2zu\n",
84 pool->name, pool->nr_active,
85 pool->nr_blocks, pool->size,
86 pool->nr_pages);
90 return size;
96 static void pool_check_block(struct dma_pool *pool, struct dma_block *block,
99 u8 *data = (void *)block;
102 for (i = sizeof(struct dma_block); i < pool->size; i++) {
105 dev_err(pool->dev, "%s %s, %p (corrupted)\n", __func__,
106 pool->name, block);
113 data, pool->size, 1);
118 memset(block, POOL_POISON_ALLOCATED, pool->size);
125 list_for_each_entry(page, &pool->page_list, page_list) {
126 if (dma < page->dma)
128 if ((dma - page->dma) < pool->allocation)
136 struct dma_block *block = pool->next_block;
141 dev_err(pool->dev, "%s %s, %p/%pad (bad dma)\n",
142 __func__, pool->name, vaddr, &dma);
146 while (block) {
147 if (block != vaddr) {
148 block = block->next_block;
151 dev_err(pool->dev, "%s %s, dma %pad already free\n",
152 __func__, pool->name, &dma);
156 memset(vaddr, POOL_POISON_FREED, pool->size);
162 memset(page->vaddr, POOL_POISON_FREED, pool->allocation);
165 static void pool_check_block(struct dma_pool *pool, struct dma_block *block,
173 memset(vaddr, 0, pool->size);
184 struct dma_block *block = pool->next_block;
186 if (block) {
187 pool->next_block = block->next_block;
188 pool->nr_active++;
190 return block;
193 static void pool_block_push(struct dma_pool *pool, struct dma_block *block,
196 block->dma = dma;
197 block->next_block = pool->next_block;
198 pool->next_block = block;
203 * dma_pool_create_node - Creates a pool of coherent DMA memory blocks.
206 * @size: size of the blocks in this pool.
215 * cache flushing primitives. The actual size of blocks allocated may be
219 * cross that size boundary. This is useful for devices which have
227 size_t size, size_t align, size_t boundary, int node)
238 else if (align & (align - 1))
241 if (size == 0 || size > INT_MAX)
243 if (size < sizeof(struct dma_block))
244 size = sizeof(struct dma_block);
246 size = ALIGN(size, align);
247 allocation = max_t(size_t, size, PAGE_SIZE);
251 else if ((boundary < size) || (boundary & (boundary - 1)))
260 strscpy(retval->name, name, sizeof(retval->name));
262 retval->dev = dev;
264 INIT_LIST_HEAD(&retval->page_list);
265 spin_lock_init(&retval->lock);
266 retval->size = size;
267 retval->boundary = boundary;
268 retval->allocation = allocation;
269 retval->node = node;
270 INIT_LIST_HEAD(&retval->pools);
273 * pools_lock ensures that the ->dma_pools list does not get corrupted.
282 empty = list_empty(&dev->dma_pools);
283 list_add(&retval->pools, &dev->dma_pools);
291 list_del(&retval->pools);
305 unsigned int next_boundary = pool->boundary, offset = 0;
306 struct dma_block *block, *first = NULL, *last = NULL;
309 while (offset + pool->size <= pool->allocation) {
310 if (offset + pool->size > next_boundary) {
312 next_boundary += pool->boundary;
316 block = page->vaddr + offset;
317 block->dma = page->dma + offset;
318 block->next_block = NULL;
321 last->next_block = block;
323 first = block;
324 last = block;
326 offset += pool->size;
327 pool->nr_blocks++;
330 last->next_block = pool->next_block;
331 pool->next_block = first;
333 list_add(&page->page_list, &pool->page_list);
334 pool->nr_pages++;
341 page = kmalloc_node(sizeof(*page), mem_flags, pool->node);
345 page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation,
346 &page->dma, mem_flags);
347 if (!page->vaddr) {
356 * dma_pool_destroy - destroys a pool of dma memory blocks.
373 list_del(&pool->pools);
374 empty = list_empty(&pool->dev->dma_pools);
377 device_remove_file(pool->dev, &dev_attr_pools);
380 if (pool->nr_active) {
381 dev_err(pool->dev, "%s %s busy\n", __func__, pool->name);
385 list_for_each_entry_safe(page, tmp, &pool->page_list, page_list) {
387 dma_free_coherent(pool->dev, pool->allocation,
388 page->vaddr, page->dma);
389 list_del(&page->page_list);
398 * dma_pool_alloc - get a block of coherent memory
399 * @pool: dma pool that will produce the block
401 * @handle: pointer to dma address of block
403 * Return: the kernel virtual address of a currently unused block,
405 * If such a memory block can't be allocated, %NULL is returned.
410 struct dma_block *block;
416 spin_lock_irqsave(&pool->lock, flags);
417 block = pool_block_pop(pool);
418 if (!block) {
421 * &pool->lock
423 spin_unlock_irqrestore(&pool->lock, flags);
429 spin_lock_irqsave(&pool->lock, flags);
431 block = pool_block_pop(pool);
433 spin_unlock_irqrestore(&pool->lock, flags);
435 *handle = block->dma;
436 pool_check_block(pool, block, mem_flags);
438 memset(block, 0, pool->size);
440 return block;
445 * dma_pool_free - put block back into dma pool
446 * @pool: the dma pool holding the block
447 * @vaddr: virtual address of block
448 * @dma: dma address of block
450 * Caller promises neither device nor driver will again touch this block
451 * unless it is first re-allocated.
455 struct dma_block *block = vaddr;
458 spin_lock_irqsave(&pool->lock, flags);
460 pool_block_push(pool, block, dma);
461 pool->nr_active--;
463 spin_unlock_irqrestore(&pool->lock, flags);
483 * dmam_pool_create - Managed dma_pool_create()
486 * @size: size of the blocks in this pool.
497 size_t size, size_t align, size_t allocation)
505 pool = *ptr = dma_pool_create(name, dev, size, align, allocation);
516 * dmam_pool_destroy - Managed dma_pool_destroy()
523 struct device *dev = pool->dev;