1 | /************************************************************************** |
---|
2 | |
---|
3 | Copyright (c) 2007, 2008 Chelsio Inc. |
---|
4 | All rights reserved. |
---|
5 | |
---|
6 | Redistribution and use in source and binary forms, with or without |
---|
7 | modification, are permitted provided that the following conditions are met: |
---|
8 | |
---|
9 | 1. Redistributions of source code must retain the above copyright notice, |
---|
10 | this list of conditions and the following disclaimer. |
---|
11 | |
---|
12 | 2. Neither the name of the Chelsio Corporation nor the names of its |
---|
13 | contributors may be used to endorse or promote products derived from |
---|
14 | this software without specific prior written permission. |
---|
15 | |
---|
16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
17 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
23 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
24 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
25 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
26 | POSSIBILITY OF SUCH DAMAGE. |
---|
27 | ***************************************************************************/ |
---|
28 | |
---|
29 | #ifndef _LINUX_GENALLOC_H |
---|
30 | #define _LINUX_GENALLOC_H |
---|
31 | |
---|
32 | #include <sys/blist.h> |
---|
33 | #include <sys/malloc.h> |
---|
34 | |
---|
35 | struct gen_pool { |
---|
36 | blist_t gen_list; |
---|
37 | daddr_t gen_base; |
---|
38 | int gen_chunk_shift; |
---|
39 | struct mtx gen_lock; |
---|
40 | }; |
---|
41 | |
---|
42 | static inline struct gen_pool * |
---|
43 | gen_pool_create(int min_alloc_order, int nid) |
---|
44 | { |
---|
45 | struct gen_pool *gp; |
---|
46 | |
---|
47 | gp = malloc(sizeof(*gp), M_DEVBUF, M_NOWAIT | M_ZERO); |
---|
48 | if (gp == NULL) |
---|
49 | return (NULL); |
---|
50 | |
---|
51 | gp->gen_chunk_shift = min_alloc_order; |
---|
52 | mtx_init(&gp->gen_lock, "genpool", NULL, MTX_DEF); |
---|
53 | return (gp); |
---|
54 | } |
---|
55 | |
---|
56 | static inline int |
---|
57 | gen_pool_add_virt(struct gen_pool *gp, daddr_t virt, daddr_t phys, |
---|
58 | size_t size, int nid) |
---|
59 | { |
---|
60 | |
---|
61 | (void)phys; |
---|
62 | (void)nid; |
---|
63 | |
---|
64 | if (gp->gen_base != 0) |
---|
65 | return (-ENOMEM); |
---|
66 | |
---|
67 | gp->gen_list = blist_create(size >> gp->gen_chunk_shift, M_NOWAIT); |
---|
68 | if (gp->gen_list == NULL) |
---|
69 | return (-ENOMEM); |
---|
70 | |
---|
71 | gp->gen_base = virt; |
---|
72 | blist_free(gp->gen_list, 0, size >> gp->gen_chunk_shift); |
---|
73 | return (0); |
---|
74 | } |
---|
75 | |
---|
76 | static inline int |
---|
77 | gen_pool_add(struct gen_pool *gp, daddr_t addr, size_t size, int nid) |
---|
78 | { |
---|
79 | |
---|
80 | return (gen_pool_add_virt(gp, addr, -1, size, nid)); |
---|
81 | } |
---|
82 | |
---|
83 | static inline daddr_t |
---|
84 | gen_pool_alloc(struct gen_pool *gp, size_t size) |
---|
85 | { |
---|
86 | int chunks; |
---|
87 | daddr_t blkno; |
---|
88 | |
---|
89 | chunks = (size + (1 << gp->gen_chunk_shift) - 1) >> gp->gen_chunk_shift; |
---|
90 | mtx_lock(&gp->gen_lock); |
---|
91 | blkno = blist_alloc(gp->gen_list, chunks); |
---|
92 | mtx_unlock(&gp->gen_lock); |
---|
93 | |
---|
94 | if (blkno == SWAPBLK_NONE) |
---|
95 | return (0); |
---|
96 | |
---|
97 | return (gp->gen_base + ((1 << gp->gen_chunk_shift) * blkno)); |
---|
98 | } |
---|
99 | |
---|
100 | static inline void |
---|
101 | gen_pool_free(struct gen_pool *gp, daddr_t address, size_t size) |
---|
102 | { |
---|
103 | int chunks; |
---|
104 | daddr_t blkno; |
---|
105 | |
---|
106 | chunks = (size + (1<<gp->gen_chunk_shift) - 1) >> gp->gen_chunk_shift; |
---|
107 | blkno = (address - gp->gen_base) / (1 << gp->gen_chunk_shift); |
---|
108 | mtx_lock(&gp->gen_lock); |
---|
109 | blist_free(gp->gen_list, blkno, chunks); |
---|
110 | mtx_unlock(&gp->gen_lock); |
---|
111 | } |
---|
112 | |
---|
113 | static __inline void |
---|
114 | gen_pool_destroy(struct gen_pool *gp) |
---|
115 | { |
---|
116 | blist_destroy(gp->gen_list); |
---|
117 | free(gp, M_DEVBUF); |
---|
118 | } |
---|
119 | |
---|
120 | static inline struct gen_pool * |
---|
121 | devm_gen_pool_create(struct device *dev, int min_alloc_order, int nid, |
---|
122 | const char *name) |
---|
123 | { |
---|
124 | |
---|
125 | (void)dev; |
---|
126 | (void)name; |
---|
127 | return (gen_pool_create(min_alloc_order, nid)); |
---|
128 | } |
---|
129 | |
---|
130 | #endif /* _LINUX_GENALLOC_H */ |
---|