3 * Memory management wrappers for DRM
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
10 * Created: Thu Feb 4 14:00:34 1999 by faith@valinux.com
12 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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
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.
36 #include <linux/highmem.h>
43 uint64_t low_threshold;
44 uint64_t high_threshold;
45 uint64_t emer_threshold;
47 .lock = SPIN_LOCK_UNLOCKED
50 static inline size_t drm_size_align(size_t size)
54 return PAGE_ALIGN(size);
56 while (tmpSize < size)
59 return (size_t) tmpSize;
62 int drm_alloc_memctl(size_t size)
65 unsigned long a_size = drm_size_align(size);
66 unsigned long new_used;
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;
76 * Allow small allocations from root-only processes to
77 * succeed until the emergency threshold is reached.
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))) {
88 drm_memctl.cur_used = drm_memctl.high_threshold;
89 drm_memctl.emer_used = new_used - drm_memctl.high_threshold;
91 spin_unlock(&drm_memctl.lock);
94 EXPORT_SYMBOL(drm_alloc_memctl);
97 void drm_free_memctl(size_t size)
99 unsigned long a_size = drm_size_align(size);
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;
106 drm_memctl.emer_used -= a_size;
109 drm_memctl.cur_used -= a_size;
110 spin_unlock(&drm_memctl.lock);
112 EXPORT_SYMBOL(drm_free_memctl);
114 void drm_query_memctl(uint64_t *cur_used,
116 uint64_t *low_threshold,
117 uint64_t *high_threshold,
118 uint64_t *emer_threshold)
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);
128 EXPORT_SYMBOL(drm_query_memctl);
130 void drm_init_memctl(size_t p_low_threshold,
131 size_t p_high_threshold,
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);
148 void drm_mem_init(void)
153 * Called when "/proc/dri/%dev%/mem" is read.
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.
165 int drm_mem_info(char *buf, char **start, off_t offset,
166 int len, int *eof, void *data)
171 /** Wrapper around kmalloc() */
172 void *drm_calloc(size_t nmemb, size_t size, int area)
174 return kcalloc(nmemb, size, GFP_KERNEL);
176 EXPORT_SYMBOL(drm_calloc);
178 /** Wrapper around kmalloc() and kfree() */
179 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
183 if (!(pt = kmalloc(size, GFP_KERNEL)))
185 if (oldpt && oldsize) {
186 memcpy(pt, oldpt, oldsize);
195 * \param order size order.
196 * \param area memory area. (Not used.)
197 * \return page address on success, or zero on failure.
199 * Allocate and reserve free pages.
201 unsigned long drm_alloc_pages(int order, int area)
203 unsigned long address;
204 unsigned long bytes = PAGE_SIZE << order;
208 address = __get_free_pages(GFP_KERNEL, order);
213 memset((void *)address, 0, bytes);
216 for (addr = address, sz = bytes;
217 sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
218 SetPageReserved(virt_to_page(addr));
227 * \param address address of the pages to free.
228 * \param order size order.
229 * \param area memory area. (Not used.)
231 * Unreserve and free pages allocated by alloc_pages().
233 void drm_free_pages(unsigned long address, int order, int area)
235 unsigned long bytes = PAGE_SIZE << order;
243 for (addr = address, sz = bytes;
244 sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
245 ClearPageReserved(virt_to_page(addr));
248 free_pages(address, order);
252 static void *agp_remap(unsigned long offset, unsigned long size,
253 struct drm_device * dev)
255 unsigned long *phys_addr_map, i, num_pages =
256 PAGE_ALIGN(size) / PAGE_SIZE;
257 struct drm_agp_mem *agpmem;
258 struct page **page_map;
261 size = PAGE_ALIGN(size);
264 offset -= dev->hose->mem_space->start;
267 list_for_each_entry(agpmem, &dev->agp->memory, head)
268 if (agpmem->bound <= offset
269 && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
276 * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
277 * the CPU do not get remapped by the GART. We fix this by using the kernel's
278 * page-table instead (that's probably faster anyhow...).
280 /* note: use vmalloc() because num_pages could be large... */
281 page_map = vmalloc(num_pages * sizeof(struct page *));
286 agpmem->memory->memory + (offset - agpmem->bound) / PAGE_SIZE;
287 for (i = 0; i < num_pages; ++i)
288 page_map[i] = pfn_to_page(phys_addr_map[i] >> PAGE_SHIFT);
289 addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
295 /** Wrapper around agp_allocate_memory() */
296 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11)
297 DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type)
299 return drm_agp_allocate_memory(pages, type);
302 DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type)
304 return drm_agp_allocate_memory(dev->agp->bridge, pages, type);
308 /** Wrapper around agp_free_memory() */
309 int drm_free_agp(DRM_AGP_MEM * handle, int pages)
311 return drm_agp_free_memory(handle) ? 0 : -EINVAL;
314 /** Wrapper around agp_bind_memory() */
315 int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
317 return drm_agp_bind_memory(handle, start);
320 /** Wrapper around agp_unbind_memory() */
321 int drm_unbind_agp(DRM_AGP_MEM * handle)
323 return drm_agp_unbind_memory(handle);
326 #else /* __OS_HAS_AGP*/
327 static void *agp_remap(unsigned long offset, unsigned long size,
328 struct drm_device * dev)
334 static void *agp_remap(unsigned long offset, unsigned long size,
335 struct drm_device * dev)
339 #endif /* debug_memory */
341 void drm_core_ioremap(struct drm_map *map, struct drm_device *dev)
343 if (drm_core_has_AGP(dev) &&
344 dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
345 map->handle = agp_remap(map->offset, map->size, dev);
347 map->handle = ioremap(map->offset, map->size);
349 EXPORT_SYMBOL_GPL(drm_core_ioremap);
351 void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev)
353 if (!map->handle || !map->size)
356 if (drm_core_has_AGP(dev) &&
357 dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
360 iounmap(map->handle);
362 EXPORT_SYMBOL_GPL(drm_core_ioremapfree);