blob: 71338b48e88919ab2a668ecf2ff9f1e25a91b0fa [file] [log] [blame]
Jes Sorensenf14f75b2005-06-21 17:15:02 -07001/*
2 * Basic general purpose allocator for managing special purpose memory
3 * not managed by the regular kmalloc/kfree interface.
4 * Uses for this includes on-device special memory, uncached memory
5 * etc.
6 *
Jes Sorensenf14f75b2005-06-21 17:15:02 -07007 * Copyright 2005 (C) Jes Sorensen <jes@trained-monkey.org>
8 *
9 * This source code is licensed under the GNU General Public License,
10 * Version 2. See the file COPYING for more details.
11 */
12
13#include <linux/module.h>
Jes Sorensenf14f75b2005-06-21 17:15:02 -070014#include <linux/genalloc.h>
15
Jes Sorensenf14f75b2005-06-21 17:15:02 -070016
Dean Nelson929f9722006-06-23 02:03:21 -070017/*
18 * Create a new special memory pool.
19 *
20 * @min_alloc_order: log base 2 of number of bytes each bitmap bit represents
21 * @nid: node id of the node the pool structure should be allocated on, or -1
22 */
23struct gen_pool *gen_pool_create(int min_alloc_order, int nid)
Jes Sorensenf14f75b2005-06-21 17:15:02 -070024{
Dean Nelson929f9722006-06-23 02:03:21 -070025 struct gen_pool *pool;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070026
Dean Nelson929f9722006-06-23 02:03:21 -070027 pool = kmalloc_node(sizeof(struct gen_pool), GFP_KERNEL, nid);
28 if (pool != NULL) {
29 rwlock_init(&pool->lock);
30 INIT_LIST_HEAD(&pool->chunks);
31 pool->min_alloc_order = min_alloc_order;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070032 }
Dean Nelson929f9722006-06-23 02:03:21 -070033 return pool;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070034}
35EXPORT_SYMBOL(gen_pool_create);
36
37
38/*
Dean Nelson929f9722006-06-23 02:03:21 -070039 * Add a new chunk of memory to the specified pool.
40 *
41 * @pool: pool to add new memory chunk to
42 * @addr: starting address of memory chunk to add to pool
43 * @size: size in bytes of the memory chunk to add to pool
44 * @nid: node id of the node the chunk structure and bitmap should be
45 * allocated on, or -1
Jes Sorensenf14f75b2005-06-21 17:15:02 -070046 */
Dean Nelson929f9722006-06-23 02:03:21 -070047int gen_pool_add(struct gen_pool *pool, unsigned long addr, size_t size,
48 int nid)
Jes Sorensenf14f75b2005-06-21 17:15:02 -070049{
Dean Nelson929f9722006-06-23 02:03:21 -070050 struct gen_pool_chunk *chunk;
51 int nbits = size >> pool->min_alloc_order;
52 int nbytes = sizeof(struct gen_pool_chunk) +
53 (nbits + BITS_PER_BYTE - 1) / BITS_PER_BYTE;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070054
Dean Nelson929f9722006-06-23 02:03:21 -070055 chunk = kmalloc_node(nbytes, GFP_KERNEL, nid);
56 if (unlikely(chunk == NULL))
57 return -1;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070058
Dean Nelson929f9722006-06-23 02:03:21 -070059 memset(chunk, 0, nbytes);
60 spin_lock_init(&chunk->lock);
61 chunk->start_addr = addr;
62 chunk->end_addr = addr + size;
63
64 write_lock(&pool->lock);
65 list_add(&chunk->next_chunk, &pool->chunks);
66 write_unlock(&pool->lock);
67
68 return 0;
69}
70EXPORT_SYMBOL(gen_pool_add);
71
72
73/*
74 * Allocate the requested number of bytes from the specified pool.
75 * Uses a first-fit algorithm.
76 *
77 * @pool: pool to allocate from
78 * @size: number of bytes to allocate from the pool
79 */
80unsigned long gen_pool_alloc(struct gen_pool *pool, size_t size)
81{
82 struct list_head *_chunk;
83 struct gen_pool_chunk *chunk;
84 unsigned long addr, flags;
85 int order = pool->min_alloc_order;
86 int nbits, bit, start_bit, end_bit;
87
88 if (size == 0)
Jes Sorensenf14f75b2005-06-21 17:15:02 -070089 return 0;
90
Dean Nelson929f9722006-06-23 02:03:21 -070091 nbits = (size + (1UL << order) - 1) >> order;
Jes Sorensenf14f75b2005-06-21 17:15:02 -070092
Dean Nelson929f9722006-06-23 02:03:21 -070093 read_lock(&pool->lock);
94 list_for_each(_chunk, &pool->chunks) {
95 chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
96
97 end_bit = (chunk->end_addr - chunk->start_addr) >> order;
98 end_bit -= nbits + 1;
99
100 spin_lock_irqsave(&chunk->lock, flags);
101 bit = -1;
102 while (bit + 1 < end_bit) {
103 bit = find_next_zero_bit(chunk->bits, end_bit, bit + 1);
104 if (bit >= end_bit)
105 break;
106
107 start_bit = bit;
108 if (nbits > 1) {
109 bit = find_next_bit(chunk->bits, bit + nbits,
110 bit + 1);
111 if (bit - start_bit < nbits)
112 continue;
113 }
114
115 addr = chunk->start_addr +
116 ((unsigned long)start_bit << order);
117 while (nbits--)
118 __set_bit(start_bit++, &chunk->bits);
119 spin_unlock_irqrestore(&chunk->lock, flags);
120 read_unlock(&pool->lock);
121 return addr;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700122 }
Dean Nelson929f9722006-06-23 02:03:21 -0700123 spin_unlock_irqrestore(&chunk->lock, flags);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700124 }
Dean Nelson929f9722006-06-23 02:03:21 -0700125 read_unlock(&pool->lock);
126 return 0;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700127}
128EXPORT_SYMBOL(gen_pool_alloc);
129
130
131/*
Dean Nelson929f9722006-06-23 02:03:21 -0700132 * Free the specified memory back to the specified pool.
133 *
134 * @pool: pool to free to
135 * @addr: starting address of memory to free back to pool
136 * @size: size in bytes of memory to free
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700137 */
Dean Nelson929f9722006-06-23 02:03:21 -0700138void gen_pool_free(struct gen_pool *pool, unsigned long addr, size_t size)
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700139{
Dean Nelson929f9722006-06-23 02:03:21 -0700140 struct list_head *_chunk;
141 struct gen_pool_chunk *chunk;
142 unsigned long flags;
143 int order = pool->min_alloc_order;
144 int bit, nbits;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700145
Dean Nelson929f9722006-06-23 02:03:21 -0700146 nbits = (size + (1UL << order) - 1) >> order;
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700147
Dean Nelson929f9722006-06-23 02:03:21 -0700148 read_lock(&pool->lock);
149 list_for_each(_chunk, &pool->chunks) {
150 chunk = list_entry(_chunk, struct gen_pool_chunk, next_chunk);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700151
Dean Nelson929f9722006-06-23 02:03:21 -0700152 if (addr >= chunk->start_addr && addr < chunk->end_addr) {
153 BUG_ON(addr + size > chunk->end_addr);
154 spin_lock_irqsave(&chunk->lock, flags);
155 bit = (addr - chunk->start_addr) >> order;
156 while (nbits--)
157 __clear_bit(bit++, &chunk->bits);
158 spin_unlock_irqrestore(&chunk->lock, flags);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700159 break;
160 }
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700161 }
Dean Nelson929f9722006-06-23 02:03:21 -0700162 BUG_ON(nbits > 0);
163 read_unlock(&pool->lock);
Jes Sorensenf14f75b2005-06-21 17:15:02 -0700164}
165EXPORT_SYMBOL(gen_pool_free);