Lines Matching +full:page +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
10 * the given device. It uses the dma_alloc_coherent page allocator to get
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
16 * allocated pages. Each page in the page_list is split into blocks of at
17 * least 'size' bytes. Free blocks are tracked in an unsorted singly-linked
19 * keep a count of how many are currently allocated from each page.
23 #include <linux/dma-mapping.h>
56 unsigned int size; member
75 unsigned size; in pools_show() local
77 size = sysfs_emit(buf, "poolinfo - 0.1\n"); in pools_show()
80 list_for_each_entry(pool, &dev->dma_pools, pools) { in pools_show()
81 /* per-pool info, no real statistics yet */ in pools_show()
82 size += sysfs_emit_at(buf, size, "%-16s %4zu %4zu %4u %2zu\n", in pools_show()
83 pool->name, pool->nr_active, in pools_show()
84 pool->nr_blocks, pool->size, in pools_show()
85 pool->nr_pages); in pools_show()
89 return size; in pools_show()
101 for (i = sizeof(struct dma_block); i < pool->size; i++) { in pool_check_block()
104 dev_err(pool->dev, "%s %s, %p (corrupted)\n", __func__, in pool_check_block()
105 pool->name, block); in pool_check_block()
112 data, pool->size, 1); in pool_check_block()
117 memset(block, POOL_POISON_ALLOCATED, pool->size); in pool_check_block()
122 struct dma_page *page; in pool_find_page() local
124 list_for_each_entry(page, &pool->page_list, page_list) { in pool_find_page()
125 if (dma < page->dma) in pool_find_page()
127 if ((dma - page->dma) < pool->allocation) in pool_find_page()
128 return page; in pool_find_page()
135 struct dma_block *block = pool->next_block; in pool_block_err()
136 struct dma_page *page; in pool_block_err() local
138 page = pool_find_page(pool, dma); in pool_block_err()
139 if (!page) { in pool_block_err()
140 dev_err(pool->dev, "%s %s, %p/%pad (bad dma)\n", in pool_block_err()
141 __func__, pool->name, vaddr, &dma); in pool_block_err()
147 block = block->next_block; in pool_block_err()
150 dev_err(pool->dev, "%s %s, dma %pad already free\n", in pool_block_err()
151 __func__, pool->name, &dma); in pool_block_err()
155 memset(vaddr, POOL_POISON_FREED, pool->size); in pool_block_err()
159 static void pool_init_page(struct dma_pool *pool, struct dma_page *page) in pool_init_page() argument
161 memset(page->vaddr, POOL_POISON_FREED, pool->allocation); in pool_init_page()
172 memset(vaddr, 0, pool->size); in pool_block_err()
176 static void pool_init_page(struct dma_pool *pool, struct dma_page *page) in pool_init_page() argument
183 struct dma_block *block = pool->next_block; in pool_block_pop()
186 pool->next_block = block->next_block; in pool_block_pop()
187 pool->nr_active++; in pool_block_pop()
195 block->dma = dma; in pool_block_push()
196 block->next_block = pool->next_block; in pool_block_push()
197 pool->next_block = block; in pool_block_push()
202 * dma_pool_create - Creates a pool of consistent memory blocks, for dma.
205 * @size: size of the blocks in this pool.
213 * cache flushing primitives. The actual size of blocks allocated may be
217 * cross that size boundary. This is useful for devices which have
225 size_t size, size_t align, size_t boundary) in dma_pool_create() argument
236 else if (align & (align - 1)) in dma_pool_create()
239 if (size == 0 || size > INT_MAX) in dma_pool_create()
241 if (size < sizeof(struct dma_block)) in dma_pool_create()
242 size = sizeof(struct dma_block); in dma_pool_create()
244 size = ALIGN(size, align); in dma_pool_create()
245 allocation = max_t(size_t, size, PAGE_SIZE); in dma_pool_create()
249 else if ((boundary < size) || (boundary & (boundary - 1))) in dma_pool_create()
258 strscpy(retval->name, name, sizeof(retval->name)); in dma_pool_create()
260 retval->dev = dev; in dma_pool_create()
262 INIT_LIST_HEAD(&retval->page_list); in dma_pool_create()
263 spin_lock_init(&retval->lock); in dma_pool_create()
264 retval->size = size; in dma_pool_create()
265 retval->boundary = boundary; in dma_pool_create()
266 retval->allocation = allocation; in dma_pool_create()
267 INIT_LIST_HEAD(&retval->pools); in dma_pool_create()
270 * pools_lock ensures that the ->dma_pools list does not get corrupted. in dma_pool_create()
279 empty = list_empty(&dev->dma_pools); in dma_pool_create()
280 list_add(&retval->pools, &dev->dma_pools); in dma_pool_create()
288 list_del(&retval->pools); in dma_pool_create()
300 static void pool_initialise_page(struct dma_pool *pool, struct dma_page *page) in pool_initialise_page() argument
302 unsigned int next_boundary = pool->boundary, offset = 0; in pool_initialise_page()
305 pool_init_page(pool, page); in pool_initialise_page()
306 while (offset + pool->size <= pool->allocation) { in pool_initialise_page()
307 if (offset + pool->size > next_boundary) { in pool_initialise_page()
309 next_boundary += pool->boundary; in pool_initialise_page()
313 block = page->vaddr + offset; in pool_initialise_page()
314 block->dma = page->dma + offset; in pool_initialise_page()
315 block->next_block = NULL; in pool_initialise_page()
318 last->next_block = block; in pool_initialise_page()
323 offset += pool->size; in pool_initialise_page()
324 pool->nr_blocks++; in pool_initialise_page()
327 last->next_block = pool->next_block; in pool_initialise_page()
328 pool->next_block = first; in pool_initialise_page()
330 list_add(&page->page_list, &pool->page_list); in pool_initialise_page()
331 pool->nr_pages++; in pool_initialise_page()
336 struct dma_page *page; in pool_alloc_page() local
338 page = kmalloc(sizeof(*page), mem_flags); in pool_alloc_page()
339 if (!page) in pool_alloc_page()
342 page->vaddr = dma_alloc_coherent(pool->dev, pool->allocation, in pool_alloc_page()
343 &page->dma, mem_flags); in pool_alloc_page()
344 if (!page->vaddr) { in pool_alloc_page()
345 kfree(page); in pool_alloc_page()
349 return page; in pool_alloc_page()
353 * dma_pool_destroy - destroys a pool of dma memory blocks.
362 struct dma_page *page, *tmp; in dma_pool_destroy() local
370 list_del(&pool->pools); in dma_pool_destroy()
371 empty = list_empty(&pool->dev->dma_pools); in dma_pool_destroy()
374 device_remove_file(pool->dev, &dev_attr_pools); in dma_pool_destroy()
377 if (pool->nr_active) { in dma_pool_destroy()
378 dev_err(pool->dev, "%s %s busy\n", __func__, pool->name); in dma_pool_destroy()
382 list_for_each_entry_safe(page, tmp, &pool->page_list, page_list) { in dma_pool_destroy()
384 dma_free_coherent(pool->dev, pool->allocation, in dma_pool_destroy()
385 page->vaddr, page->dma); in dma_pool_destroy()
386 list_del(&page->page_list); in dma_pool_destroy()
387 kfree(page); in dma_pool_destroy()
395 * dma_pool_alloc - get a block of consistent memory
408 struct dma_page *page; in dma_pool_alloc() local
413 spin_lock_irqsave(&pool->lock, flags); in dma_pool_alloc()
418 * &pool->lock in dma_pool_alloc()
420 spin_unlock_irqrestore(&pool->lock, flags); in dma_pool_alloc()
422 page = pool_alloc_page(pool, mem_flags & (~__GFP_ZERO)); in dma_pool_alloc()
423 if (!page) in dma_pool_alloc()
426 spin_lock_irqsave(&pool->lock, flags); in dma_pool_alloc()
427 pool_initialise_page(pool, page); in dma_pool_alloc()
430 spin_unlock_irqrestore(&pool->lock, flags); in dma_pool_alloc()
432 *handle = block->dma; in dma_pool_alloc()
435 memset(block, 0, pool->size); in dma_pool_alloc()
442 * dma_pool_free - put block back into dma pool
448 * unless it is first re-allocated.
455 spin_lock_irqsave(&pool->lock, flags); in dma_pool_free()
458 pool->nr_active--; in dma_pool_free()
460 spin_unlock_irqrestore(&pool->lock, flags); in dma_pool_free()
480 * dmam_pool_create - Managed dma_pool_create()
483 * @size: size of the blocks in this pool.
494 size_t size, size_t align, size_t allocation) in dmam_pool_create() argument
502 pool = *ptr = dma_pool_create(name, dev, size, align, allocation); in dmam_pool_create()
513 * dmam_pool_destroy - Managed dma_pool_destroy()
520 struct device *dev = pool->dev; in dmam_pool_destroy()