OSDN Git Service

drm: make sure the drawable code doesn't call malloc(0).
[android-x86/external-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         drm_u64_t cur_used;
42         drm_u64_t low_threshold;
43         drm_u64_t high_threshold;
44 } drm_memctl = {
45         .lock = SPIN_LOCK_UNLOCKED
46 };
47
48 static inline size_t drm_size_align(size_t size) {
49
50         size_t tmpSize = 4;
51         if (size > PAGE_SIZE)
52                 return PAGE_ALIGN(size);
53
54         while(tmpSize < size)
55                 tmpSize <<= 1;
56
57         return (size_t) tmpSize;
58 }
59
60 int drm_alloc_memctl(size_t size)
61 {
62         int ret;
63         unsigned long a_size = drm_size_align(size);
64
65         spin_lock(&drm_memctl.lock);
66         ret = ((drm_memctl.cur_used + a_size) > drm_memctl.high_threshold) ?
67                 -ENOMEM : 0;
68         if (!ret)
69                 drm_memctl.cur_used += a_size;
70         spin_unlock(&drm_memctl.lock);
71         return ret;
72 }
73 EXPORT_SYMBOL(drm_alloc_memctl);
74
75 void drm_free_memctl(size_t size)
76 {
77         unsigned long a_size = drm_size_align(size);
78
79         spin_lock(&drm_memctl.lock);
80         drm_memctl.cur_used -= a_size;
81         spin_unlock(&drm_memctl.lock);
82 }
83 EXPORT_SYMBOL(drm_free_memctl);
84
85 void drm_query_memctl(drm_u64_t *cur_used,
86                       drm_u64_t *low_threshold,
87                       drm_u64_t *high_threshold)
88 {
89         spin_lock(&drm_memctl.lock);
90         *cur_used = drm_memctl.cur_used;
91         *low_threshold = drm_memctl.low_threshold;
92         *high_threshold = drm_memctl.high_threshold;
93         spin_unlock(&drm_memctl.lock);
94 }
95 EXPORT_SYMBOL(drm_query_memctl);
96
97 void drm_init_memctl(size_t p_low_threshold,
98                      size_t p_high_threshold,
99                      size_t unit_size)
100 {
101         spin_lock(&drm_memctl.lock);
102         drm_memctl.cur_used = 0;
103         drm_memctl.low_threshold = p_low_threshold * unit_size;
104         drm_memctl.high_threshold = p_high_threshold * unit_size;
105         spin_unlock(&drm_memctl.lock);
106 }
107
108
109 #ifndef DEBUG_MEMORY
110
111 /** No-op. */
112 void drm_mem_init(void)
113 {
114 }
115
116 /**
117  * Called when "/proc/dri/%dev%/mem" is read.
118  *
119  * \param buf output buffer.
120  * \param start start of output data.
121  * \param offset requested start offset.
122  * \param len requested number of bytes.
123  * \param eof whether there is no more data to return.
124  * \param data private data.
125  * \return number of written bytes.
126  *
127  * No-op.
128  */
129 int drm_mem_info(char *buf, char **start, off_t offset,
130                  int len, int *eof, void *data)
131 {
132         return 0;
133 }
134
135 /** Wrapper around kmalloc() */
136 void *drm_calloc(size_t nmemb, size_t size, int area)
137 {
138         return kcalloc(nmemb, size, GFP_KERNEL);
139 }
140 EXPORT_SYMBOL(drm_calloc);
141
142 /** Wrapper around kmalloc() and kfree() */
143 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
144 {
145         void *pt;
146
147         if (!(pt = kmalloc(size, GFP_KERNEL)))
148                 return NULL;
149         if (oldpt && oldsize) {
150                 memcpy(pt, oldpt, oldsize);
151                 kfree(oldpt);
152         }
153         return pt;
154 }
155
156 /**
157  * Allocate pages.
158  *
159  * \param order size order.
160  * \param area memory area. (Not used.)
161  * \return page address on success, or zero on failure.
162  *
163  * Allocate and reserve free pages.
164  */
165 unsigned long drm_alloc_pages(int order, int area)
166 {
167         unsigned long address;
168         unsigned long bytes = PAGE_SIZE << order;
169         unsigned long addr;
170         unsigned int sz;
171
172         address = __get_free_pages(GFP_KERNEL, order);
173         if (!address)
174                 return 0;
175
176         /* Zero */
177         memset((void *)address, 0, bytes);
178
179         /* Reserve */
180         for (addr = address, sz = bytes;
181              sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
182                 SetPageReserved(virt_to_page(addr));
183         }
184
185         return address;
186 }
187
188 /**
189  * Free pages.
190  *
191  * \param address address of the pages to free.
192  * \param order size order.
193  * \param area memory area. (Not used.)
194  *
195  * Unreserve and free pages allocated by alloc_pages().
196  */
197 void drm_free_pages(unsigned long address, int order, int area)
198 {
199         unsigned long bytes = PAGE_SIZE << order;
200         unsigned long addr;
201         unsigned int sz;
202
203         if (!address)
204                 return;
205
206         /* Unreserve */
207         for (addr = address, sz = bytes;
208              sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
209                 ClearPageReserved(virt_to_page(addr));
210         }
211
212         free_pages(address, order);
213 }
214
215 #if __OS_HAS_AGP
216 static void *agp_remap(unsigned long offset, unsigned long size,
217                               drm_device_t * dev)
218 {
219         unsigned long *phys_addr_map, i, num_pages =
220             PAGE_ALIGN(size) / PAGE_SIZE;
221         struct drm_agp_mem *agpmem;
222         struct page **page_map;
223         void *addr;
224
225         size = PAGE_ALIGN(size);
226
227 #ifdef __alpha__
228         offset -= dev->hose->mem_space->start;
229 #endif
230
231         list_for_each_entry(agpmem, &dev->agp->memory, head)
232                 if (agpmem->bound <= offset
233                     && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
234                     (offset + size))
235                         break;
236         if (!agpmem)
237                 return NULL;
238
239         /*
240          * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
241          * the CPU do not get remapped by the GART.  We fix this by using the kernel's
242          * page-table instead (that's probably faster anyhow...).
243          */
244         /* note: use vmalloc() because num_pages could be large... */
245         page_map = vmalloc(num_pages * sizeof(struct page *));
246         if (!page_map)
247                 return NULL;
248
249         phys_addr_map =
250             agpmem->memory->memory + (offset - agpmem->bound) / PAGE_SIZE;
251         for (i = 0; i < num_pages; ++i)
252                 page_map[i] = pfn_to_page(phys_addr_map[i] >> PAGE_SHIFT);
253         addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
254         vfree(page_map);
255
256         return addr;
257 }
258
259 /** Wrapper around agp_allocate_memory() */
260 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
261 DRM_AGP_MEM *drm_alloc_agp(drm_device_t *dev, int pages, u32 type)
262 {
263         return drm_agp_allocate_memory(pages, type);
264 }
265 #else
266 DRM_AGP_MEM *drm_alloc_agp(drm_device_t *dev, int pages, u32 type)
267 {
268         return drm_agp_allocate_memory(dev->agp->bridge, pages, type);
269 }
270 #endif
271
272 /** Wrapper around agp_free_memory() */
273 int drm_free_agp(DRM_AGP_MEM * handle, int pages)
274 {
275         return drm_agp_free_memory(handle) ? 0 : -EINVAL;
276 }
277
278 /** Wrapper around agp_bind_memory() */
279 int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
280 {
281         return drm_agp_bind_memory(handle, start);
282 }
283
284 /** Wrapper around agp_unbind_memory() */
285 int drm_unbind_agp(DRM_AGP_MEM * handle)
286 {
287         return drm_agp_unbind_memory(handle);
288 }
289
290 #else  /* __OS_HAS_AGP*/
291 static void *agp_remap(unsigned long offset, unsigned long size,
292                        drm_device_t * dev)
293 {
294         return NULL;
295 }
296 #endif                          /* agp */
297
298 #endif                          /* debug_memory */
299
300 void drm_core_ioremap(struct drm_map *map, struct drm_device *dev)
301 {
302         if (drm_core_has_AGP(dev) &&
303             dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
304                 map->handle = agp_remap(map->offset, map->size, dev);
305         else
306                 map->handle = ioremap(map->offset, map->size);
307 }
308 EXPORT_SYMBOL_GPL(drm_core_ioremap);
309
310 void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev)
311 {
312         if (!map->handle || !map->size)
313                 return;
314
315         if (drm_core_has_AGP(dev) &&
316             dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
317                 vunmap(map->handle);
318         else
319                 iounmap(map->handle);
320 }
321 EXPORT_SYMBOL_GPL(drm_core_ioremapfree);