1*592ffb21SWarner Losh /**************************************************************************
2*592ffb21SWarner Losh *
3*592ffb21SWarner Losh * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA.
4*592ffb21SWarner Losh * All Rights Reserved.
5*592ffb21SWarner Losh *
6*592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a
7*592ffb21SWarner Losh * copy of this software and associated documentation files (the
8*592ffb21SWarner Losh * "Software"), to deal in the Software without restriction, including
9*592ffb21SWarner Losh * without limitation the rights to use, copy, modify, merge, publish,
10*592ffb21SWarner Losh * distribute, sub license, and/or sell copies of the Software, and to
11*592ffb21SWarner Losh * permit persons to whom the Software is furnished to do so, subject to
12*592ffb21SWarner Losh * the following conditions:
13*592ffb21SWarner Losh *
14*592ffb21SWarner Losh * The above copyright notice and this permission notice (including the
15*592ffb21SWarner Losh * next paragraph) shall be included in all copies or substantial portions
16*592ffb21SWarner Losh * of the Software.
17*592ffb21SWarner Losh *
18*592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21*592ffb21SWarner Losh * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22*592ffb21SWarner Losh * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23*592ffb21SWarner Losh * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24*592ffb21SWarner Losh * USE OR OTHER DEALINGS IN THE SOFTWARE.
25*592ffb21SWarner Losh *
26*592ffb21SWarner Losh *
27*592ffb21SWarner Losh **************************************************************************/
28*592ffb21SWarner Losh
29*592ffb21SWarner Losh #include <sys/cdefs.h>
30*592ffb21SWarner Losh /*
31*592ffb21SWarner Losh * Generic simple memory manager implementation. Intended to be used as a base
32*592ffb21SWarner Losh * class implementation for more advanced memory managers.
33*592ffb21SWarner Losh *
34*592ffb21SWarner Losh * Note that the algorithm used is quite simple and there might be substantial
35*592ffb21SWarner Losh * performance gains if a smarter free list is implemented. Currently it is just an
36*592ffb21SWarner Losh * unordered stack of free regions. This could easily be improved if an RB-tree
37*592ffb21SWarner Losh * is used instead. At least if we expect heavy fragmentation.
38*592ffb21SWarner Losh *
39*592ffb21SWarner Losh * Aligned allocations can also see improvement.
40*592ffb21SWarner Losh *
41*592ffb21SWarner Losh * Authors:
42*592ffb21SWarner Losh * Thomas Hellström <thomas-at-tungstengraphics-dot-com>
43*592ffb21SWarner Losh */
44*592ffb21SWarner Losh
45*592ffb21SWarner Losh #include <dev/drm2/drmP.h>
46*592ffb21SWarner Losh #include <dev/drm2/drm_mm.h>
47*592ffb21SWarner Losh
48*592ffb21SWarner Losh #define MM_UNUSED_TARGET 4
49*592ffb21SWarner Losh
drm_mm_kmalloc(struct drm_mm * mm,int atomic)50*592ffb21SWarner Losh static struct drm_mm_node *drm_mm_kmalloc(struct drm_mm *mm, int atomic)
51*592ffb21SWarner Losh {
52*592ffb21SWarner Losh struct drm_mm_node *child;
53*592ffb21SWarner Losh
54*592ffb21SWarner Losh child = malloc(sizeof(*child), DRM_MEM_MM, M_NOWAIT | M_ZERO);
55*592ffb21SWarner Losh
56*592ffb21SWarner Losh if (unlikely(child == NULL)) {
57*592ffb21SWarner Losh mtx_lock(&mm->unused_lock);
58*592ffb21SWarner Losh if (list_empty(&mm->unused_nodes))
59*592ffb21SWarner Losh child = NULL;
60*592ffb21SWarner Losh else {
61*592ffb21SWarner Losh child =
62*592ffb21SWarner Losh list_entry(mm->unused_nodes.next,
63*592ffb21SWarner Losh struct drm_mm_node, node_list);
64*592ffb21SWarner Losh list_del(&child->node_list);
65*592ffb21SWarner Losh --mm->num_unused;
66*592ffb21SWarner Losh }
67*592ffb21SWarner Losh mtx_unlock(&mm->unused_lock);
68*592ffb21SWarner Losh }
69*592ffb21SWarner Losh return child;
70*592ffb21SWarner Losh }
71*592ffb21SWarner Losh
72*592ffb21SWarner Losh /* drm_mm_pre_get() - pre allocate drm_mm_node structure
73*592ffb21SWarner Losh * drm_mm: memory manager struct we are pre-allocating for
74*592ffb21SWarner Losh *
75*592ffb21SWarner Losh * Returns 0 on success or -ENOMEM if allocation fails.
76*592ffb21SWarner Losh */
drm_mm_pre_get(struct drm_mm * mm)77*592ffb21SWarner Losh int drm_mm_pre_get(struct drm_mm *mm)
78*592ffb21SWarner Losh {
79*592ffb21SWarner Losh struct drm_mm_node *node;
80*592ffb21SWarner Losh
81*592ffb21SWarner Losh mtx_lock(&mm->unused_lock);
82*592ffb21SWarner Losh while (mm->num_unused < MM_UNUSED_TARGET) {
83*592ffb21SWarner Losh mtx_unlock(&mm->unused_lock);
84*592ffb21SWarner Losh node = malloc(sizeof(*node), DRM_MEM_MM, M_NOWAIT | M_ZERO);
85*592ffb21SWarner Losh mtx_lock(&mm->unused_lock);
86*592ffb21SWarner Losh
87*592ffb21SWarner Losh if (unlikely(node == NULL)) {
88*592ffb21SWarner Losh int ret = (mm->num_unused < 2) ? -ENOMEM : 0;
89*592ffb21SWarner Losh mtx_unlock(&mm->unused_lock);
90*592ffb21SWarner Losh return ret;
91*592ffb21SWarner Losh }
92*592ffb21SWarner Losh ++mm->num_unused;
93*592ffb21SWarner Losh list_add_tail(&node->node_list, &mm->unused_nodes);
94*592ffb21SWarner Losh }
95*592ffb21SWarner Losh mtx_unlock(&mm->unused_lock);
96*592ffb21SWarner Losh return 0;
97*592ffb21SWarner Losh }
98*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_pre_get);
99*592ffb21SWarner Losh
drm_mm_hole_node_start(struct drm_mm_node * hole_node)100*592ffb21SWarner Losh static inline unsigned long drm_mm_hole_node_start(struct drm_mm_node *hole_node)
101*592ffb21SWarner Losh {
102*592ffb21SWarner Losh return hole_node->start + hole_node->size;
103*592ffb21SWarner Losh }
104*592ffb21SWarner Losh
drm_mm_hole_node_end(struct drm_mm_node * hole_node)105*592ffb21SWarner Losh static inline unsigned long drm_mm_hole_node_end(struct drm_mm_node *hole_node)
106*592ffb21SWarner Losh {
107*592ffb21SWarner Losh struct drm_mm_node *next_node =
108*592ffb21SWarner Losh list_entry(hole_node->node_list.next, struct drm_mm_node,
109*592ffb21SWarner Losh node_list);
110*592ffb21SWarner Losh
111*592ffb21SWarner Losh return next_node->start;
112*592ffb21SWarner Losh }
113*592ffb21SWarner Losh
drm_mm_insert_helper(struct drm_mm_node * hole_node,struct drm_mm_node * node,unsigned long size,unsigned alignment,unsigned long color)114*592ffb21SWarner Losh static void drm_mm_insert_helper(struct drm_mm_node *hole_node,
115*592ffb21SWarner Losh struct drm_mm_node *node,
116*592ffb21SWarner Losh unsigned long size, unsigned alignment,
117*592ffb21SWarner Losh unsigned long color)
118*592ffb21SWarner Losh {
119*592ffb21SWarner Losh struct drm_mm *mm = hole_node->mm;
120*592ffb21SWarner Losh unsigned long hole_start = drm_mm_hole_node_start(hole_node);
121*592ffb21SWarner Losh unsigned long hole_end = drm_mm_hole_node_end(hole_node);
122*592ffb21SWarner Losh unsigned long adj_start = hole_start;
123*592ffb21SWarner Losh unsigned long adj_end = hole_end;
124*592ffb21SWarner Losh
125*592ffb21SWarner Losh BUG_ON(!hole_node->hole_follows || node->allocated);
126*592ffb21SWarner Losh
127*592ffb21SWarner Losh if (mm->color_adjust)
128*592ffb21SWarner Losh mm->color_adjust(hole_node, color, &adj_start, &adj_end);
129*592ffb21SWarner Losh
130*592ffb21SWarner Losh if (alignment) {
131*592ffb21SWarner Losh unsigned tmp = adj_start % alignment;
132*592ffb21SWarner Losh if (tmp)
133*592ffb21SWarner Losh adj_start += alignment - tmp;
134*592ffb21SWarner Losh }
135*592ffb21SWarner Losh
136*592ffb21SWarner Losh if (adj_start == hole_start) {
137*592ffb21SWarner Losh hole_node->hole_follows = 0;
138*592ffb21SWarner Losh list_del(&hole_node->hole_stack);
139*592ffb21SWarner Losh }
140*592ffb21SWarner Losh
141*592ffb21SWarner Losh node->start = adj_start;
142*592ffb21SWarner Losh node->size = size;
143*592ffb21SWarner Losh node->mm = mm;
144*592ffb21SWarner Losh node->color = color;
145*592ffb21SWarner Losh node->allocated = 1;
146*592ffb21SWarner Losh
147*592ffb21SWarner Losh INIT_LIST_HEAD(&node->hole_stack);
148*592ffb21SWarner Losh list_add(&node->node_list, &hole_node->node_list);
149*592ffb21SWarner Losh
150*592ffb21SWarner Losh BUG_ON(node->start + node->size > adj_end);
151*592ffb21SWarner Losh
152*592ffb21SWarner Losh node->hole_follows = 0;
153*592ffb21SWarner Losh if (node->start + node->size < hole_end) {
154*592ffb21SWarner Losh list_add(&node->hole_stack, &mm->hole_stack);
155*592ffb21SWarner Losh node->hole_follows = 1;
156*592ffb21SWarner Losh }
157*592ffb21SWarner Losh }
158*592ffb21SWarner Losh
drm_mm_get_block_generic(struct drm_mm_node * hole_node,unsigned long size,unsigned alignment,unsigned long color,int atomic)159*592ffb21SWarner Losh struct drm_mm_node *drm_mm_get_block_generic(struct drm_mm_node *hole_node,
160*592ffb21SWarner Losh unsigned long size,
161*592ffb21SWarner Losh unsigned alignment,
162*592ffb21SWarner Losh unsigned long color,
163*592ffb21SWarner Losh int atomic)
164*592ffb21SWarner Losh {
165*592ffb21SWarner Losh struct drm_mm_node *node;
166*592ffb21SWarner Losh
167*592ffb21SWarner Losh node = drm_mm_kmalloc(hole_node->mm, atomic);
168*592ffb21SWarner Losh if (unlikely(node == NULL))
169*592ffb21SWarner Losh return NULL;
170*592ffb21SWarner Losh
171*592ffb21SWarner Losh drm_mm_insert_helper(hole_node, node, size, alignment, color);
172*592ffb21SWarner Losh
173*592ffb21SWarner Losh return node;
174*592ffb21SWarner Losh }
175*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_get_block_generic);
176*592ffb21SWarner Losh
177*592ffb21SWarner Losh /**
178*592ffb21SWarner Losh * Search for free space and insert a preallocated memory node. Returns
179*592ffb21SWarner Losh * -ENOSPC if no suitable free area is available. The preallocated memory node
180*592ffb21SWarner Losh * must be cleared.
181*592ffb21SWarner Losh */
drm_mm_insert_node_generic(struct drm_mm * mm,struct drm_mm_node * node,unsigned long size,unsigned alignment,unsigned long color)182*592ffb21SWarner Losh int drm_mm_insert_node_generic(struct drm_mm *mm, struct drm_mm_node *node,
183*592ffb21SWarner Losh unsigned long size, unsigned alignment,
184*592ffb21SWarner Losh unsigned long color)
185*592ffb21SWarner Losh {
186*592ffb21SWarner Losh struct drm_mm_node *hole_node;
187*592ffb21SWarner Losh
188*592ffb21SWarner Losh hole_node = drm_mm_search_free_generic(mm, size, alignment,
189*592ffb21SWarner Losh color, 0);
190*592ffb21SWarner Losh if (!hole_node)
191*592ffb21SWarner Losh return -ENOSPC;
192*592ffb21SWarner Losh
193*592ffb21SWarner Losh drm_mm_insert_helper(hole_node, node, size, alignment, color);
194*592ffb21SWarner Losh return 0;
195*592ffb21SWarner Losh }
196*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_insert_node_generic);
197*592ffb21SWarner Losh
drm_mm_insert_node(struct drm_mm * mm,struct drm_mm_node * node,unsigned long size,unsigned alignment)198*592ffb21SWarner Losh int drm_mm_insert_node(struct drm_mm *mm, struct drm_mm_node *node,
199*592ffb21SWarner Losh unsigned long size, unsigned alignment)
200*592ffb21SWarner Losh {
201*592ffb21SWarner Losh return drm_mm_insert_node_generic(mm, node, size, alignment, 0);
202*592ffb21SWarner Losh }
203*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_insert_node);
204*592ffb21SWarner Losh
drm_mm_insert_helper_range(struct drm_mm_node * hole_node,struct drm_mm_node * node,unsigned long size,unsigned alignment,unsigned long color,unsigned long start,unsigned long end)205*592ffb21SWarner Losh static void drm_mm_insert_helper_range(struct drm_mm_node *hole_node,
206*592ffb21SWarner Losh struct drm_mm_node *node,
207*592ffb21SWarner Losh unsigned long size, unsigned alignment,
208*592ffb21SWarner Losh unsigned long color,
209*592ffb21SWarner Losh unsigned long start, unsigned long end)
210*592ffb21SWarner Losh {
211*592ffb21SWarner Losh struct drm_mm *mm = hole_node->mm;
212*592ffb21SWarner Losh unsigned long hole_start = drm_mm_hole_node_start(hole_node);
213*592ffb21SWarner Losh unsigned long hole_end = drm_mm_hole_node_end(hole_node);
214*592ffb21SWarner Losh unsigned long adj_start = hole_start;
215*592ffb21SWarner Losh unsigned long adj_end = hole_end;
216*592ffb21SWarner Losh
217*592ffb21SWarner Losh BUG_ON(!hole_node->hole_follows || node->allocated);
218*592ffb21SWarner Losh
219*592ffb21SWarner Losh if (adj_start < start)
220*592ffb21SWarner Losh adj_start = start;
221*592ffb21SWarner Losh if (adj_end > end)
222*592ffb21SWarner Losh adj_end = end;
223*592ffb21SWarner Losh
224*592ffb21SWarner Losh if (mm->color_adjust)
225*592ffb21SWarner Losh mm->color_adjust(hole_node, color, &adj_start, &adj_end);
226*592ffb21SWarner Losh
227*592ffb21SWarner Losh if (alignment) {
228*592ffb21SWarner Losh unsigned tmp = adj_start % alignment;
229*592ffb21SWarner Losh if (tmp)
230*592ffb21SWarner Losh adj_start += alignment - tmp;
231*592ffb21SWarner Losh }
232*592ffb21SWarner Losh
233*592ffb21SWarner Losh if (adj_start == hole_start) {
234*592ffb21SWarner Losh hole_node->hole_follows = 0;
235*592ffb21SWarner Losh list_del(&hole_node->hole_stack);
236*592ffb21SWarner Losh }
237*592ffb21SWarner Losh
238*592ffb21SWarner Losh node->start = adj_start;
239*592ffb21SWarner Losh node->size = size;
240*592ffb21SWarner Losh node->mm = mm;
241*592ffb21SWarner Losh node->color = color;
242*592ffb21SWarner Losh node->allocated = 1;
243*592ffb21SWarner Losh
244*592ffb21SWarner Losh INIT_LIST_HEAD(&node->hole_stack);
245*592ffb21SWarner Losh list_add(&node->node_list, &hole_node->node_list);
246*592ffb21SWarner Losh
247*592ffb21SWarner Losh BUG_ON(node->start + node->size > adj_end);
248*592ffb21SWarner Losh BUG_ON(node->start + node->size > end);
249*592ffb21SWarner Losh
250*592ffb21SWarner Losh node->hole_follows = 0;
251*592ffb21SWarner Losh if (node->start + node->size < hole_end) {
252*592ffb21SWarner Losh list_add(&node->hole_stack, &mm->hole_stack);
253*592ffb21SWarner Losh node->hole_follows = 1;
254*592ffb21SWarner Losh }
255*592ffb21SWarner Losh }
256*592ffb21SWarner Losh
drm_mm_get_block_range_generic(struct drm_mm_node * hole_node,unsigned long size,unsigned alignment,unsigned long color,unsigned long start,unsigned long end,int atomic)257*592ffb21SWarner Losh struct drm_mm_node *drm_mm_get_block_range_generic(struct drm_mm_node *hole_node,
258*592ffb21SWarner Losh unsigned long size,
259*592ffb21SWarner Losh unsigned alignment,
260*592ffb21SWarner Losh unsigned long color,
261*592ffb21SWarner Losh unsigned long start,
262*592ffb21SWarner Losh unsigned long end,
263*592ffb21SWarner Losh int atomic)
264*592ffb21SWarner Losh {
265*592ffb21SWarner Losh struct drm_mm_node *node;
266*592ffb21SWarner Losh
267*592ffb21SWarner Losh node = drm_mm_kmalloc(hole_node->mm, atomic);
268*592ffb21SWarner Losh if (unlikely(node == NULL))
269*592ffb21SWarner Losh return NULL;
270*592ffb21SWarner Losh
271*592ffb21SWarner Losh drm_mm_insert_helper_range(hole_node, node, size, alignment, color,
272*592ffb21SWarner Losh start, end);
273*592ffb21SWarner Losh
274*592ffb21SWarner Losh return node;
275*592ffb21SWarner Losh }
276*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_get_block_range_generic);
277*592ffb21SWarner Losh
278*592ffb21SWarner Losh /**
279*592ffb21SWarner Losh * Search for free space and insert a preallocated memory node. Returns
280*592ffb21SWarner Losh * -ENOSPC if no suitable free area is available. This is for range
281*592ffb21SWarner Losh * restricted allocations. The preallocated memory node must be cleared.
282*592ffb21SWarner Losh */
drm_mm_insert_node_in_range_generic(struct drm_mm * mm,struct drm_mm_node * node,unsigned long size,unsigned alignment,unsigned long color,unsigned long start,unsigned long end)283*592ffb21SWarner Losh int drm_mm_insert_node_in_range_generic(struct drm_mm *mm, struct drm_mm_node *node,
284*592ffb21SWarner Losh unsigned long size, unsigned alignment, unsigned long color,
285*592ffb21SWarner Losh unsigned long start, unsigned long end)
286*592ffb21SWarner Losh {
287*592ffb21SWarner Losh struct drm_mm_node *hole_node;
288*592ffb21SWarner Losh
289*592ffb21SWarner Losh hole_node = drm_mm_search_free_in_range_generic(mm,
290*592ffb21SWarner Losh size, alignment, color,
291*592ffb21SWarner Losh start, end, 0);
292*592ffb21SWarner Losh if (!hole_node)
293*592ffb21SWarner Losh return -ENOSPC;
294*592ffb21SWarner Losh
295*592ffb21SWarner Losh drm_mm_insert_helper_range(hole_node, node,
296*592ffb21SWarner Losh size, alignment, color,
297*592ffb21SWarner Losh start, end);
298*592ffb21SWarner Losh return 0;
299*592ffb21SWarner Losh }
300*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_insert_node_in_range_generic);
301*592ffb21SWarner Losh
drm_mm_insert_node_in_range(struct drm_mm * mm,struct drm_mm_node * node,unsigned long size,unsigned alignment,unsigned long start,unsigned long end)302*592ffb21SWarner Losh int drm_mm_insert_node_in_range(struct drm_mm *mm, struct drm_mm_node *node,
303*592ffb21SWarner Losh unsigned long size, unsigned alignment,
304*592ffb21SWarner Losh unsigned long start, unsigned long end)
305*592ffb21SWarner Losh {
306*592ffb21SWarner Losh return drm_mm_insert_node_in_range_generic(mm, node, size, alignment, 0, start, end);
307*592ffb21SWarner Losh }
308*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_insert_node_in_range);
309*592ffb21SWarner Losh
310*592ffb21SWarner Losh /**
311*592ffb21SWarner Losh * Remove a memory node from the allocator.
312*592ffb21SWarner Losh */
drm_mm_remove_node(struct drm_mm_node * node)313*592ffb21SWarner Losh void drm_mm_remove_node(struct drm_mm_node *node)
314*592ffb21SWarner Losh {
315*592ffb21SWarner Losh struct drm_mm *mm = node->mm;
316*592ffb21SWarner Losh struct drm_mm_node *prev_node;
317*592ffb21SWarner Losh
318*592ffb21SWarner Losh BUG_ON(node->scanned_block || node->scanned_prev_free
319*592ffb21SWarner Losh || node->scanned_next_free);
320*592ffb21SWarner Losh
321*592ffb21SWarner Losh prev_node =
322*592ffb21SWarner Losh list_entry(node->node_list.prev, struct drm_mm_node, node_list);
323*592ffb21SWarner Losh
324*592ffb21SWarner Losh if (node->hole_follows) {
325*592ffb21SWarner Losh BUG_ON(drm_mm_hole_node_start(node)
326*592ffb21SWarner Losh == drm_mm_hole_node_end(node));
327*592ffb21SWarner Losh list_del(&node->hole_stack);
328*592ffb21SWarner Losh } else
329*592ffb21SWarner Losh BUG_ON(drm_mm_hole_node_start(node)
330*592ffb21SWarner Losh != drm_mm_hole_node_end(node));
331*592ffb21SWarner Losh
332*592ffb21SWarner Losh if (!prev_node->hole_follows) {
333*592ffb21SWarner Losh prev_node->hole_follows = 1;
334*592ffb21SWarner Losh list_add(&prev_node->hole_stack, &mm->hole_stack);
335*592ffb21SWarner Losh } else
336*592ffb21SWarner Losh list_move(&prev_node->hole_stack, &mm->hole_stack);
337*592ffb21SWarner Losh
338*592ffb21SWarner Losh list_del(&node->node_list);
339*592ffb21SWarner Losh node->allocated = 0;
340*592ffb21SWarner Losh }
341*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_remove_node);
342*592ffb21SWarner Losh
343*592ffb21SWarner Losh /*
344*592ffb21SWarner Losh * Remove a memory node from the allocator and free the allocated struct
345*592ffb21SWarner Losh * drm_mm_node. Only to be used on a struct drm_mm_node obtained by one of the
346*592ffb21SWarner Losh * drm_mm_get_block functions.
347*592ffb21SWarner Losh */
drm_mm_put_block(struct drm_mm_node * node)348*592ffb21SWarner Losh void drm_mm_put_block(struct drm_mm_node *node)
349*592ffb21SWarner Losh {
350*592ffb21SWarner Losh
351*592ffb21SWarner Losh struct drm_mm *mm = node->mm;
352*592ffb21SWarner Losh
353*592ffb21SWarner Losh drm_mm_remove_node(node);
354*592ffb21SWarner Losh
355*592ffb21SWarner Losh mtx_lock(&mm->unused_lock);
356*592ffb21SWarner Losh if (mm->num_unused < MM_UNUSED_TARGET) {
357*592ffb21SWarner Losh list_add(&node->node_list, &mm->unused_nodes);
358*592ffb21SWarner Losh ++mm->num_unused;
359*592ffb21SWarner Losh } else
360*592ffb21SWarner Losh free(node, DRM_MEM_MM);
361*592ffb21SWarner Losh mtx_unlock(&mm->unused_lock);
362*592ffb21SWarner Losh }
363*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_put_block);
364*592ffb21SWarner Losh
check_free_hole(unsigned long start,unsigned long end,unsigned long size,unsigned alignment)365*592ffb21SWarner Losh static int check_free_hole(unsigned long start, unsigned long end,
366*592ffb21SWarner Losh unsigned long size, unsigned alignment)
367*592ffb21SWarner Losh {
368*592ffb21SWarner Losh if (end - start < size)
369*592ffb21SWarner Losh return 0;
370*592ffb21SWarner Losh
371*592ffb21SWarner Losh if (alignment) {
372*592ffb21SWarner Losh unsigned tmp = start % alignment;
373*592ffb21SWarner Losh if (tmp)
374*592ffb21SWarner Losh start += alignment - tmp;
375*592ffb21SWarner Losh }
376*592ffb21SWarner Losh
377*592ffb21SWarner Losh return end >= start + size;
378*592ffb21SWarner Losh }
379*592ffb21SWarner Losh
drm_mm_search_free_generic(const struct drm_mm * mm,unsigned long size,unsigned alignment,unsigned long color,bool best_match)380*592ffb21SWarner Losh struct drm_mm_node *drm_mm_search_free_generic(const struct drm_mm *mm,
381*592ffb21SWarner Losh unsigned long size,
382*592ffb21SWarner Losh unsigned alignment,
383*592ffb21SWarner Losh unsigned long color,
384*592ffb21SWarner Losh bool best_match)
385*592ffb21SWarner Losh {
386*592ffb21SWarner Losh struct drm_mm_node *entry;
387*592ffb21SWarner Losh struct drm_mm_node *best;
388*592ffb21SWarner Losh unsigned long best_size;
389*592ffb21SWarner Losh
390*592ffb21SWarner Losh BUG_ON(mm->scanned_blocks);
391*592ffb21SWarner Losh
392*592ffb21SWarner Losh best = NULL;
393*592ffb21SWarner Losh best_size = ~0UL;
394*592ffb21SWarner Losh
395*592ffb21SWarner Losh list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
396*592ffb21SWarner Losh unsigned long adj_start = drm_mm_hole_node_start(entry);
397*592ffb21SWarner Losh unsigned long adj_end = drm_mm_hole_node_end(entry);
398*592ffb21SWarner Losh
399*592ffb21SWarner Losh if (mm->color_adjust) {
400*592ffb21SWarner Losh mm->color_adjust(entry, color, &adj_start, &adj_end);
401*592ffb21SWarner Losh if (adj_end <= adj_start)
402*592ffb21SWarner Losh continue;
403*592ffb21SWarner Losh }
404*592ffb21SWarner Losh
405*592ffb21SWarner Losh BUG_ON(!entry->hole_follows);
406*592ffb21SWarner Losh if (!check_free_hole(adj_start, adj_end, size, alignment))
407*592ffb21SWarner Losh continue;
408*592ffb21SWarner Losh
409*592ffb21SWarner Losh if (!best_match)
410*592ffb21SWarner Losh return entry;
411*592ffb21SWarner Losh
412*592ffb21SWarner Losh if (entry->size < best_size) {
413*592ffb21SWarner Losh best = entry;
414*592ffb21SWarner Losh best_size = entry->size;
415*592ffb21SWarner Losh }
416*592ffb21SWarner Losh }
417*592ffb21SWarner Losh
418*592ffb21SWarner Losh return best;
419*592ffb21SWarner Losh }
420*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_search_free_generic);
421*592ffb21SWarner Losh
drm_mm_search_free_in_range_generic(const struct drm_mm * mm,unsigned long size,unsigned alignment,unsigned long color,unsigned long start,unsigned long end,bool best_match)422*592ffb21SWarner Losh struct drm_mm_node *drm_mm_search_free_in_range_generic(const struct drm_mm *mm,
423*592ffb21SWarner Losh unsigned long size,
424*592ffb21SWarner Losh unsigned alignment,
425*592ffb21SWarner Losh unsigned long color,
426*592ffb21SWarner Losh unsigned long start,
427*592ffb21SWarner Losh unsigned long end,
428*592ffb21SWarner Losh bool best_match)
429*592ffb21SWarner Losh {
430*592ffb21SWarner Losh struct drm_mm_node *entry;
431*592ffb21SWarner Losh struct drm_mm_node *best;
432*592ffb21SWarner Losh unsigned long best_size;
433*592ffb21SWarner Losh
434*592ffb21SWarner Losh BUG_ON(mm->scanned_blocks);
435*592ffb21SWarner Losh
436*592ffb21SWarner Losh best = NULL;
437*592ffb21SWarner Losh best_size = ~0UL;
438*592ffb21SWarner Losh
439*592ffb21SWarner Losh list_for_each_entry(entry, &mm->hole_stack, hole_stack) {
440*592ffb21SWarner Losh unsigned long adj_start = drm_mm_hole_node_start(entry) < start ?
441*592ffb21SWarner Losh start : drm_mm_hole_node_start(entry);
442*592ffb21SWarner Losh unsigned long adj_end = drm_mm_hole_node_end(entry) > end ?
443*592ffb21SWarner Losh end : drm_mm_hole_node_end(entry);
444*592ffb21SWarner Losh
445*592ffb21SWarner Losh BUG_ON(!entry->hole_follows);
446*592ffb21SWarner Losh
447*592ffb21SWarner Losh if (mm->color_adjust) {
448*592ffb21SWarner Losh mm->color_adjust(entry, color, &adj_start, &adj_end);
449*592ffb21SWarner Losh if (adj_end <= adj_start)
450*592ffb21SWarner Losh continue;
451*592ffb21SWarner Losh }
452*592ffb21SWarner Losh
453*592ffb21SWarner Losh if (!check_free_hole(adj_start, adj_end, size, alignment))
454*592ffb21SWarner Losh continue;
455*592ffb21SWarner Losh
456*592ffb21SWarner Losh if (!best_match)
457*592ffb21SWarner Losh return entry;
458*592ffb21SWarner Losh
459*592ffb21SWarner Losh if (entry->size < best_size) {
460*592ffb21SWarner Losh best = entry;
461*592ffb21SWarner Losh best_size = entry->size;
462*592ffb21SWarner Losh }
463*592ffb21SWarner Losh }
464*592ffb21SWarner Losh
465*592ffb21SWarner Losh return best;
466*592ffb21SWarner Losh }
467*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_search_free_in_range_generic);
468*592ffb21SWarner Losh
469*592ffb21SWarner Losh /**
470*592ffb21SWarner Losh * Moves an allocation. To be used with embedded struct drm_mm_node.
471*592ffb21SWarner Losh */
drm_mm_replace_node(struct drm_mm_node * old,struct drm_mm_node * new)472*592ffb21SWarner Losh void drm_mm_replace_node(struct drm_mm_node *old, struct drm_mm_node *new)
473*592ffb21SWarner Losh {
474*592ffb21SWarner Losh list_replace(&old->node_list, &new->node_list);
475*592ffb21SWarner Losh list_replace(&old->hole_stack, &new->hole_stack);
476*592ffb21SWarner Losh new->hole_follows = old->hole_follows;
477*592ffb21SWarner Losh new->mm = old->mm;
478*592ffb21SWarner Losh new->start = old->start;
479*592ffb21SWarner Losh new->size = old->size;
480*592ffb21SWarner Losh new->color = old->color;
481*592ffb21SWarner Losh
482*592ffb21SWarner Losh old->allocated = 0;
483*592ffb21SWarner Losh new->allocated = 1;
484*592ffb21SWarner Losh }
485*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_replace_node);
486*592ffb21SWarner Losh
487*592ffb21SWarner Losh /**
488*592ffb21SWarner Losh * Initializa lru scanning.
489*592ffb21SWarner Losh *
490*592ffb21SWarner Losh * This simply sets up the scanning routines with the parameters for the desired
491*592ffb21SWarner Losh * hole.
492*592ffb21SWarner Losh *
493*592ffb21SWarner Losh * Warning: As long as the scan list is non-empty, no other operations than
494*592ffb21SWarner Losh * adding/removing nodes to/from the scan list are allowed.
495*592ffb21SWarner Losh */
drm_mm_init_scan(struct drm_mm * mm,unsigned long size,unsigned alignment,unsigned long color)496*592ffb21SWarner Losh void drm_mm_init_scan(struct drm_mm *mm,
497*592ffb21SWarner Losh unsigned long size,
498*592ffb21SWarner Losh unsigned alignment,
499*592ffb21SWarner Losh unsigned long color)
500*592ffb21SWarner Losh {
501*592ffb21SWarner Losh mm->scan_color = color;
502*592ffb21SWarner Losh mm->scan_alignment = alignment;
503*592ffb21SWarner Losh mm->scan_size = size;
504*592ffb21SWarner Losh mm->scanned_blocks = 0;
505*592ffb21SWarner Losh mm->scan_hit_start = 0;
506*592ffb21SWarner Losh mm->scan_hit_end = 0;
507*592ffb21SWarner Losh mm->scan_check_range = 0;
508*592ffb21SWarner Losh mm->prev_scanned_node = NULL;
509*592ffb21SWarner Losh }
510*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_init_scan);
511*592ffb21SWarner Losh
512*592ffb21SWarner Losh /**
513*592ffb21SWarner Losh * Initializa lru scanning.
514*592ffb21SWarner Losh *
515*592ffb21SWarner Losh * This simply sets up the scanning routines with the parameters for the desired
516*592ffb21SWarner Losh * hole. This version is for range-restricted scans.
517*592ffb21SWarner Losh *
518*592ffb21SWarner Losh * Warning: As long as the scan list is non-empty, no other operations than
519*592ffb21SWarner Losh * adding/removing nodes to/from the scan list are allowed.
520*592ffb21SWarner Losh */
drm_mm_init_scan_with_range(struct drm_mm * mm,unsigned long size,unsigned alignment,unsigned long color,unsigned long start,unsigned long end)521*592ffb21SWarner Losh void drm_mm_init_scan_with_range(struct drm_mm *mm,
522*592ffb21SWarner Losh unsigned long size,
523*592ffb21SWarner Losh unsigned alignment,
524*592ffb21SWarner Losh unsigned long color,
525*592ffb21SWarner Losh unsigned long start,
526*592ffb21SWarner Losh unsigned long end)
527*592ffb21SWarner Losh {
528*592ffb21SWarner Losh mm->scan_color = color;
529*592ffb21SWarner Losh mm->scan_alignment = alignment;
530*592ffb21SWarner Losh mm->scan_size = size;
531*592ffb21SWarner Losh mm->scanned_blocks = 0;
532*592ffb21SWarner Losh mm->scan_hit_start = 0;
533*592ffb21SWarner Losh mm->scan_hit_end = 0;
534*592ffb21SWarner Losh mm->scan_start = start;
535*592ffb21SWarner Losh mm->scan_end = end;
536*592ffb21SWarner Losh mm->scan_check_range = 1;
537*592ffb21SWarner Losh mm->prev_scanned_node = NULL;
538*592ffb21SWarner Losh }
539*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_init_scan_with_range);
540*592ffb21SWarner Losh
541*592ffb21SWarner Losh /**
542*592ffb21SWarner Losh * Add a node to the scan list that might be freed to make space for the desired
543*592ffb21SWarner Losh * hole.
544*592ffb21SWarner Losh *
545*592ffb21SWarner Losh * Returns non-zero, if a hole has been found, zero otherwise.
546*592ffb21SWarner Losh */
drm_mm_scan_add_block(struct drm_mm_node * node)547*592ffb21SWarner Losh int drm_mm_scan_add_block(struct drm_mm_node *node)
548*592ffb21SWarner Losh {
549*592ffb21SWarner Losh struct drm_mm *mm = node->mm;
550*592ffb21SWarner Losh struct drm_mm_node *prev_node;
551*592ffb21SWarner Losh unsigned long hole_start, hole_end;
552*592ffb21SWarner Losh unsigned long adj_start, adj_end;
553*592ffb21SWarner Losh
554*592ffb21SWarner Losh mm->scanned_blocks++;
555*592ffb21SWarner Losh
556*592ffb21SWarner Losh BUG_ON(node->scanned_block);
557*592ffb21SWarner Losh node->scanned_block = 1;
558*592ffb21SWarner Losh
559*592ffb21SWarner Losh prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
560*592ffb21SWarner Losh node_list);
561*592ffb21SWarner Losh
562*592ffb21SWarner Losh node->scanned_preceeds_hole = prev_node->hole_follows;
563*592ffb21SWarner Losh prev_node->hole_follows = 1;
564*592ffb21SWarner Losh list_del(&node->node_list);
565*592ffb21SWarner Losh node->node_list.prev = &prev_node->node_list;
566*592ffb21SWarner Losh node->node_list.next = &mm->prev_scanned_node->node_list;
567*592ffb21SWarner Losh mm->prev_scanned_node = node;
568*592ffb21SWarner Losh
569*592ffb21SWarner Losh adj_start = hole_start = drm_mm_hole_node_start(prev_node);
570*592ffb21SWarner Losh adj_end = hole_end = drm_mm_hole_node_end(prev_node);
571*592ffb21SWarner Losh
572*592ffb21SWarner Losh if (mm->scan_check_range) {
573*592ffb21SWarner Losh if (adj_start < mm->scan_start)
574*592ffb21SWarner Losh adj_start = mm->scan_start;
575*592ffb21SWarner Losh if (adj_end > mm->scan_end)
576*592ffb21SWarner Losh adj_end = mm->scan_end;
577*592ffb21SWarner Losh }
578*592ffb21SWarner Losh
579*592ffb21SWarner Losh if (mm->color_adjust)
580*592ffb21SWarner Losh mm->color_adjust(prev_node, mm->scan_color,
581*592ffb21SWarner Losh &adj_start, &adj_end);
582*592ffb21SWarner Losh
583*592ffb21SWarner Losh if (check_free_hole(adj_start, adj_end,
584*592ffb21SWarner Losh mm->scan_size, mm->scan_alignment)) {
585*592ffb21SWarner Losh mm->scan_hit_start = hole_start;
586*592ffb21SWarner Losh mm->scan_hit_end = hole_end;
587*592ffb21SWarner Losh return 1;
588*592ffb21SWarner Losh }
589*592ffb21SWarner Losh
590*592ffb21SWarner Losh return 0;
591*592ffb21SWarner Losh }
592*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_scan_add_block);
593*592ffb21SWarner Losh
594*592ffb21SWarner Losh /**
595*592ffb21SWarner Losh * Remove a node from the scan list.
596*592ffb21SWarner Losh *
597*592ffb21SWarner Losh * Nodes _must_ be removed in the exact same order from the scan list as they
598*592ffb21SWarner Losh * have been added, otherwise the internal state of the memory manager will be
599*592ffb21SWarner Losh * corrupted.
600*592ffb21SWarner Losh *
601*592ffb21SWarner Losh * When the scan list is empty, the selected memory nodes can be freed. An
602*592ffb21SWarner Losh * immediately following drm_mm_search_free with best_match = 0 will then return
603*592ffb21SWarner Losh * the just freed block (because its at the top of the free_stack list).
604*592ffb21SWarner Losh *
605*592ffb21SWarner Losh * Returns one if this block should be evicted, zero otherwise. Will always
606*592ffb21SWarner Losh * return zero when no hole has been found.
607*592ffb21SWarner Losh */
drm_mm_scan_remove_block(struct drm_mm_node * node)608*592ffb21SWarner Losh int drm_mm_scan_remove_block(struct drm_mm_node *node)
609*592ffb21SWarner Losh {
610*592ffb21SWarner Losh struct drm_mm *mm = node->mm;
611*592ffb21SWarner Losh struct drm_mm_node *prev_node;
612*592ffb21SWarner Losh
613*592ffb21SWarner Losh mm->scanned_blocks--;
614*592ffb21SWarner Losh
615*592ffb21SWarner Losh BUG_ON(!node->scanned_block);
616*592ffb21SWarner Losh node->scanned_block = 0;
617*592ffb21SWarner Losh
618*592ffb21SWarner Losh prev_node = list_entry(node->node_list.prev, struct drm_mm_node,
619*592ffb21SWarner Losh node_list);
620*592ffb21SWarner Losh
621*592ffb21SWarner Losh prev_node->hole_follows = node->scanned_preceeds_hole;
622*592ffb21SWarner Losh list_add(&node->node_list, &prev_node->node_list);
623*592ffb21SWarner Losh
624*592ffb21SWarner Losh return (drm_mm_hole_node_end(node) > mm->scan_hit_start &&
625*592ffb21SWarner Losh node->start < mm->scan_hit_end);
626*592ffb21SWarner Losh }
627*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_scan_remove_block);
628*592ffb21SWarner Losh
drm_mm_clean(struct drm_mm * mm)629*592ffb21SWarner Losh int drm_mm_clean(struct drm_mm * mm)
630*592ffb21SWarner Losh {
631*592ffb21SWarner Losh struct list_head *head = &mm->head_node.node_list;
632*592ffb21SWarner Losh
633*592ffb21SWarner Losh return (head->next->next == head);
634*592ffb21SWarner Losh }
635*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_clean);
636*592ffb21SWarner Losh
drm_mm_init(struct drm_mm * mm,unsigned long start,unsigned long size)637*592ffb21SWarner Losh int drm_mm_init(struct drm_mm * mm, unsigned long start, unsigned long size)
638*592ffb21SWarner Losh {
639*592ffb21SWarner Losh INIT_LIST_HEAD(&mm->hole_stack);
640*592ffb21SWarner Losh INIT_LIST_HEAD(&mm->unused_nodes);
641*592ffb21SWarner Losh mm->num_unused = 0;
642*592ffb21SWarner Losh mm->scanned_blocks = 0;
643*592ffb21SWarner Losh mtx_init(&mm->unused_lock, "drm_unused", NULL, MTX_DEF);
644*592ffb21SWarner Losh
645*592ffb21SWarner Losh /* Clever trick to avoid a special case in the free hole tracking. */
646*592ffb21SWarner Losh INIT_LIST_HEAD(&mm->head_node.node_list);
647*592ffb21SWarner Losh INIT_LIST_HEAD(&mm->head_node.hole_stack);
648*592ffb21SWarner Losh mm->head_node.hole_follows = 1;
649*592ffb21SWarner Losh mm->head_node.scanned_block = 0;
650*592ffb21SWarner Losh mm->head_node.scanned_prev_free = 0;
651*592ffb21SWarner Losh mm->head_node.scanned_next_free = 0;
652*592ffb21SWarner Losh mm->head_node.mm = mm;
653*592ffb21SWarner Losh mm->head_node.start = start + size;
654*592ffb21SWarner Losh mm->head_node.size = start - mm->head_node.start;
655*592ffb21SWarner Losh list_add_tail(&mm->head_node.hole_stack, &mm->hole_stack);
656*592ffb21SWarner Losh
657*592ffb21SWarner Losh mm->color_adjust = NULL;
658*592ffb21SWarner Losh
659*592ffb21SWarner Losh return 0;
660*592ffb21SWarner Losh }
661*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_init);
662*592ffb21SWarner Losh
drm_mm_takedown(struct drm_mm * mm)663*592ffb21SWarner Losh void drm_mm_takedown(struct drm_mm * mm)
664*592ffb21SWarner Losh {
665*592ffb21SWarner Losh struct drm_mm_node *entry, *next;
666*592ffb21SWarner Losh
667*592ffb21SWarner Losh if (!list_empty(&mm->head_node.node_list)) {
668*592ffb21SWarner Losh DRM_ERROR("Memory manager not clean. Delaying takedown\n");
669*592ffb21SWarner Losh return;
670*592ffb21SWarner Losh }
671*592ffb21SWarner Losh
672*592ffb21SWarner Losh mtx_lock(&mm->unused_lock);
673*592ffb21SWarner Losh list_for_each_entry_safe(entry, next, &mm->unused_nodes, node_list) {
674*592ffb21SWarner Losh list_del(&entry->node_list);
675*592ffb21SWarner Losh free(entry, DRM_MEM_MM);
676*592ffb21SWarner Losh --mm->num_unused;
677*592ffb21SWarner Losh }
678*592ffb21SWarner Losh mtx_unlock(&mm->unused_lock);
679*592ffb21SWarner Losh
680*592ffb21SWarner Losh BUG_ON(mm->num_unused != 0);
681*592ffb21SWarner Losh }
682*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_takedown);
683*592ffb21SWarner Losh
drm_mm_debug_table(struct drm_mm * mm,const char * prefix)684*592ffb21SWarner Losh void drm_mm_debug_table(struct drm_mm *mm, const char *prefix)
685*592ffb21SWarner Losh {
686*592ffb21SWarner Losh struct drm_mm_node *entry;
687*592ffb21SWarner Losh unsigned long total_used = 0, total_free = 0, total = 0;
688*592ffb21SWarner Losh unsigned long hole_start, hole_end, hole_size;
689*592ffb21SWarner Losh
690*592ffb21SWarner Losh hole_start = drm_mm_hole_node_start(&mm->head_node);
691*592ffb21SWarner Losh hole_end = drm_mm_hole_node_end(&mm->head_node);
692*592ffb21SWarner Losh hole_size = hole_end - hole_start;
693*592ffb21SWarner Losh if (hole_size)
694*592ffb21SWarner Losh printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
695*592ffb21SWarner Losh prefix, hole_start, hole_end,
696*592ffb21SWarner Losh hole_size);
697*592ffb21SWarner Losh total_free += hole_size;
698*592ffb21SWarner Losh
699*592ffb21SWarner Losh drm_mm_for_each_node(entry, mm) {
700*592ffb21SWarner Losh printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: used\n",
701*592ffb21SWarner Losh prefix, entry->start, entry->start + entry->size,
702*592ffb21SWarner Losh entry->size);
703*592ffb21SWarner Losh total_used += entry->size;
704*592ffb21SWarner Losh
705*592ffb21SWarner Losh if (entry->hole_follows) {
706*592ffb21SWarner Losh hole_start = drm_mm_hole_node_start(entry);
707*592ffb21SWarner Losh hole_end = drm_mm_hole_node_end(entry);
708*592ffb21SWarner Losh hole_size = hole_end - hole_start;
709*592ffb21SWarner Losh printk(KERN_DEBUG "%s 0x%08lx-0x%08lx: %8lu: free\n",
710*592ffb21SWarner Losh prefix, hole_start, hole_end,
711*592ffb21SWarner Losh hole_size);
712*592ffb21SWarner Losh total_free += hole_size;
713*592ffb21SWarner Losh }
714*592ffb21SWarner Losh }
715*592ffb21SWarner Losh total = total_free + total_used;
716*592ffb21SWarner Losh
717*592ffb21SWarner Losh printk(KERN_DEBUG "%s total: %lu, used %lu free %lu\n", prefix, total,
718*592ffb21SWarner Losh total_used, total_free);
719*592ffb21SWarner Losh }
720*592ffb21SWarner Losh EXPORT_SYMBOL(drm_mm_debug_table);
721