LUT updates
[platform/upstream/libdrm.git] / linux-core / drm_memory.c
1 /**
2  * \file drm_memory.c
3  * Memory management wrappers for DRM
4  *
5  * \author Rickard E. (Rik) Faith <faith@valinux.com>
6  * \author Gareth Hughes <gareth@valinux.com>
7  */
8
9 /*
10  * Created: Thu Feb  4 14:00:34 1999 by faith@valinux.com
11  *
12  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14  * All Rights Reserved.
15  *
16  * Permission is hereby granted, free of charge, to any person obtaining a
17  * copy of this software and associated documentation files (the "Software"),
18  * to deal in the Software without restriction, including without limitation
19  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20  * and/or sell copies of the Software, and to permit persons to whom the
21  * Software is furnished to do so, subject to the following conditions:
22  *
23  * The above copyright notice and this permission notice (including the next
24  * paragraph) shall be included in all copies or substantial portions of the
25  * Software.
26  *
27  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
30  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33  * OTHER DEALINGS IN THE SOFTWARE.
34  */
35
36 #include <linux/highmem.h>
37 #include "drmP.h"
38
39 static struct {
40         spinlock_t lock;
41         uint64_t cur_used;
42         uint64_t emer_used;
43         uint64_t low_threshold;
44         uint64_t high_threshold;
45         uint64_t emer_threshold;
46 } drm_memctl = {
47         .lock = SPIN_LOCK_UNLOCKED
48 };
49
50 static inline size_t drm_size_align(size_t size)
51 {
52         size_t tmpSize = 4;
53         if (size > PAGE_SIZE)
54                 return PAGE_ALIGN(size);
55
56         while (tmpSize < size)
57                 tmpSize <<= 1;
58
59         return (size_t) tmpSize;
60 }
61
62 int drm_alloc_memctl(size_t size)
63 {
64         int ret = 0;
65         unsigned long a_size = drm_size_align(size);
66         unsigned long new_used;
67
68         spin_lock(&drm_memctl.lock);
69         new_used = drm_memctl.cur_used + a_size;
70         if (likely(new_used < drm_memctl.high_threshold)) {
71                 drm_memctl.cur_used = new_used;
72                 goto out;
73         }
74
75         /*
76          * Allow small allocations from root-only processes to
77          * succeed until the emergency threshold is reached.
78          */
79
80         new_used += drm_memctl.emer_used;
81         if (unlikely(!DRM_SUSER(DRM_CURPROC) ||
82                      (a_size > 16*PAGE_SIZE) ||
83                      (new_used > drm_memctl.emer_threshold))) {
84                 ret = -ENOMEM;
85                 goto out;
86         }
87
88         drm_memctl.cur_used = drm_memctl.high_threshold;
89         drm_memctl.emer_used = new_used - drm_memctl.high_threshold;
90 out:
91         spin_unlock(&drm_memctl.lock);
92         return ret;
93 }
94 EXPORT_SYMBOL(drm_alloc_memctl);
95
96
97 void drm_free_memctl(size_t size)
98 {
99         unsigned long a_size = drm_size_align(size);
100
101         spin_lock(&drm_memctl.lock);
102         if (likely(a_size >= drm_memctl.emer_used)) {
103                 a_size -= drm_memctl.emer_used;
104                 drm_memctl.emer_used = 0;
105         } else {
106                 drm_memctl.emer_used -= a_size;
107                 a_size = 0;
108         }
109         drm_memctl.cur_used -= a_size;
110         spin_unlock(&drm_memctl.lock);
111 }
112 EXPORT_SYMBOL(drm_free_memctl);
113
114 void drm_query_memctl(uint64_t *cur_used,
115                       uint64_t *emer_used,
116                       uint64_t *low_threshold,
117                       uint64_t *high_threshold,
118                       uint64_t *emer_threshold)
119 {
120         spin_lock(&drm_memctl.lock);
121         *cur_used = drm_memctl.cur_used;
122         *emer_used = drm_memctl.emer_used;
123         *low_threshold = drm_memctl.low_threshold;
124         *high_threshold = drm_memctl.high_threshold;
125         *emer_threshold = drm_memctl.emer_threshold;
126         spin_unlock(&drm_memctl.lock);
127 }
128 EXPORT_SYMBOL(drm_query_memctl);
129
130 void drm_init_memctl(size_t p_low_threshold,
131                      size_t p_high_threshold,
132                      size_t unit_size)
133 {
134         spin_lock(&drm_memctl.lock);
135         drm_memctl.emer_used = 0;
136         drm_memctl.cur_used = 0;
137         drm_memctl.low_threshold = p_low_threshold * unit_size;
138         drm_memctl.high_threshold = p_high_threshold * unit_size;
139         drm_memctl.emer_threshold = (drm_memctl.high_threshold >> 4) +
140                 drm_memctl.high_threshold;
141         spin_unlock(&drm_memctl.lock);
142 }
143
144
145 #ifndef DEBUG_MEMORY
146
147 /** No-op. */
148 void drm_mem_init(void)
149 {
150 }
151
152 /**
153  * Called when "/proc/dri/%dev%/mem" is read.
154  *
155  * \param buf output buffer.
156  * \param start start of output data.
157  * \param offset requested start offset.
158  * \param len requested number of bytes.
159  * \param eof whether there is no more data to return.
160  * \param data private data.
161  * \return number of written bytes.
162  *
163  * No-op.
164  */
165 int drm_mem_info(char *buf, char **start, off_t offset,
166                  int len, int *eof, void *data)
167 {
168         return 0;
169 }
170
171 /** Wrapper around kmalloc() */
172 void *drm_calloc(size_t nmemb, size_t size, int area)
173 {
174         return kcalloc(nmemb, size, GFP_KERNEL);
175 }
176 EXPORT_SYMBOL(drm_calloc);
177
178 /** Wrapper around kmalloc() and kfree() */
179 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
180 {
181         void *pt;
182
183         if (!(pt = kmalloc(size, GFP_KERNEL)))
184                 return NULL;
185         if (oldpt && oldsize) {
186                 memcpy(pt, oldpt, oldsize);
187                 kfree(oldpt);
188         }
189         return pt;
190 }
191 EXPORT_SYMBOL(drm_realloc);
192
193 /**
194  * Allocate pages.
195  *
196  * \param order size order.
197  * \param area memory area. (Not used.)
198  * \return page address on success, or zero on failure.
199  *
200  * Allocate and reserve free pages.
201  */
202 unsigned long drm_alloc_pages(int order, int area)
203 {
204         unsigned long address;
205         unsigned long bytes = PAGE_SIZE << order;
206         unsigned long addr;
207         unsigned int sz;
208
209         address = __get_free_pages(GFP_KERNEL, order);
210         if (!address)
211                 return 0;
212
213         /* Zero */
214         memset((void *)address, 0, bytes);
215
216         /* Reserve */
217         for (addr = address, sz = bytes;
218              sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
219                 SetPageReserved(virt_to_page(addr));
220         }
221
222         return address;
223 }
224
225 /**
226  * Free pages.
227  *
228  * \param address address of the pages to free.
229  * \param order size order.
230  * \param area memory area. (Not used.)
231  *
232  * Unreserve and free pages allocated by alloc_pages().
233  */
234 void drm_free_pages(unsigned long address, int order, int area)
235 {
236         unsigned long bytes = PAGE_SIZE << order;
237         unsigned long addr;
238         unsigned int sz;
239
240         if (!address)
241                 return;
242
243         /* Unreserve */
244         for (addr = address, sz = bytes;
245              sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
246                 ClearPageReserved(virt_to_page(addr));
247         }
248
249         free_pages(address, order);
250 }
251
252 #if __OS_HAS_AGP
253 static void *agp_remap(unsigned long offset, unsigned long size,
254                               struct drm_device * dev)
255 {
256         unsigned long *phys_addr_map, i, num_pages =
257             PAGE_ALIGN(size) / PAGE_SIZE;
258         struct drm_agp_mem *agpmem;
259         struct page **page_map;
260         void *addr;
261
262         size = PAGE_ALIGN(size);
263
264 #ifdef __alpha__
265         offset -= dev->hose->mem_space->start;
266 #endif
267
268         list_for_each_entry(agpmem, &dev->agp->memory, head)
269                 if (agpmem->bound <= offset
270                     && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
271                     (offset + size))
272                         break;
273         if (!agpmem)
274                 return NULL;
275
276         /*
277          * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
278          * the CPU do not get remapped by the GART.  We fix this by using the kernel's
279          * page-table instead (that's probably faster anyhow...).
280          */
281         /* note: use vmalloc() because num_pages could be large... */
282         page_map = vmalloc(num_pages * sizeof(struct page *));
283         if (!page_map)
284                 return NULL;
285
286         phys_addr_map =
287             agpmem->memory->memory + (offset - agpmem->bound) / PAGE_SIZE;
288         for (i = 0; i < num_pages; ++i)
289                 page_map[i] = pfn_to_page(phys_addr_map[i] >> PAGE_SHIFT);
290         addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
291         vfree(page_map);
292
293         return addr;
294 }
295
296 /** Wrapper around agp_allocate_memory() */
297 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
298 DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type)
299 {
300         return drm_agp_allocate_memory(pages, type);
301 }
302 #else
303 DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type)
304 {
305         return drm_agp_allocate_memory(dev->agp->bridge, pages, type);
306 }
307 #endif
308
309 /** Wrapper around agp_free_memory() */
310 int drm_free_agp(DRM_AGP_MEM * handle, int pages)
311 {
312         return drm_agp_free_memory(handle) ? 0 : -EINVAL;
313 }
314 EXPORT_SYMBOL(drm_free_agp);
315
316 /** Wrapper around agp_bind_memory() */
317 int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
318 {
319         return drm_agp_bind_memory(handle, start);
320 }
321
322 /** Wrapper around agp_unbind_memory() */
323 int drm_unbind_agp(DRM_AGP_MEM * handle)
324 {
325         return drm_agp_unbind_memory(handle);
326 }
327 EXPORT_SYMBOL(drm_unbind_agp);
328
329 #else  /* __OS_HAS_AGP*/
330 static void *agp_remap(unsigned long offset, unsigned long size,
331                        struct drm_device * dev)
332 {
333         return NULL;
334 }
335 #endif                          /* agp */
336 #else
337 static void *agp_remap(unsigned long offset, unsigned long size,
338                        struct drm_device * dev)
339 {
340         return NULL;
341 }
342 #endif                          /* debug_memory */
343
344 void drm_core_ioremap(struct drm_map *map, struct drm_device *dev)
345 {
346         if (drm_core_has_AGP(dev) &&
347             dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
348                 map->handle = agp_remap(map->offset, map->size, dev);
349         else
350                 map->handle = ioremap(map->offset, map->size);
351 }
352 EXPORT_SYMBOL_GPL(drm_core_ioremap);
353
354 void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev)
355 {
356         if (!map->handle || !map->size)
357                 return;
358
359         if (drm_core_has_AGP(dev) &&
360             dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
361                 vunmap(map->handle);
362         else
363                 iounmap(map->handle);
364 }
365 EXPORT_SYMBOL_GPL(drm_core_ioremapfree);