[intel] Quirk away MSI support on 945G/GM.
[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
192 /**
193  * Allocate pages.
194  *
195  * \param order size order.
196  * \param area memory area. (Not used.)
197  * \return page address on success, or zero on failure.
198  *
199  * Allocate and reserve free pages.
200  */
201 unsigned long drm_alloc_pages(int order, int area)
202 {
203         unsigned long address;
204         unsigned long bytes = PAGE_SIZE << order;
205         unsigned long addr;
206         unsigned int sz;
207
208         address = __get_free_pages(GFP_KERNEL, order);
209         if (!address)
210                 return 0;
211
212         /* Zero */
213         memset((void *)address, 0, bytes);
214
215         /* Reserve */
216         for (addr = address, sz = bytes;
217              sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
218                 SetPageReserved(virt_to_page(addr));
219         }
220
221         return address;
222 }
223
224 /**
225  * Free pages.
226  *
227  * \param address address of the pages to free.
228  * \param order size order.
229  * \param area memory area. (Not used.)
230  *
231  * Unreserve and free pages allocated by alloc_pages().
232  */
233 void drm_free_pages(unsigned long address, int order, int area)
234 {
235         unsigned long bytes = PAGE_SIZE << order;
236         unsigned long addr;
237         unsigned int sz;
238
239         if (!address)
240                 return;
241
242         /* Unreserve */
243         for (addr = address, sz = bytes;
244              sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
245                 ClearPageReserved(virt_to_page(addr));
246         }
247
248         free_pages(address, order);
249 }
250
251 #if __OS_HAS_AGP
252 static void *agp_remap(unsigned long offset, unsigned long size,
253                               struct drm_device * dev)
254 {
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;
259         void *addr;
260
261         size = PAGE_ALIGN(size);
262
263 #ifdef __alpha__
264         offset -= dev->hose->mem_space->start;
265 #endif
266
267         list_for_each_entry(agpmem, &dev->agp->memory, head)
268                 if (agpmem->bound <= offset
269                     && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >=
270                     (offset + size))
271                         break;
272         if (!agpmem)
273                 return NULL;
274
275         /*
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...).
279          */
280         /* note: use vmalloc() because num_pages could be large... */
281         page_map = vmalloc(num_pages * sizeof(struct page *));
282         if (!page_map)
283                 return NULL;
284
285         phys_addr_map =
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);
290         vfree(page_map);
291
292         return addr;
293 }
294
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)
298 {
299         return drm_agp_allocate_memory(pages, type);
300 }
301 #else
302 DRM_AGP_MEM *drm_alloc_agp(struct drm_device *dev, int pages, u32 type)
303 {
304         return drm_agp_allocate_memory(dev->agp->bridge, pages, type);
305 }
306 #endif
307
308 /** Wrapper around agp_free_memory() */
309 int drm_free_agp(DRM_AGP_MEM * handle, int pages)
310 {
311         return drm_agp_free_memory(handle) ? 0 : -EINVAL;
312 }
313 EXPORT_SYMBOL(drm_free_agp);
314
315 /** Wrapper around agp_bind_memory() */
316 int drm_bind_agp(DRM_AGP_MEM * handle, unsigned int start)
317 {
318         return drm_agp_bind_memory(handle, start);
319 }
320
321 /** Wrapper around agp_unbind_memory() */
322 int drm_unbind_agp(DRM_AGP_MEM * handle)
323 {
324         return drm_agp_unbind_memory(handle);
325 }
326 EXPORT_SYMBOL(drm_unbind_agp);
327
328 #else  /* __OS_HAS_AGP*/
329 static void *agp_remap(unsigned long offset, unsigned long size,
330                        struct drm_device * dev)
331 {
332         return NULL;
333 }
334 #endif                          /* agp */
335 #else
336 static void *agp_remap(unsigned long offset, unsigned long size,
337                        struct drm_device * dev)
338 {
339         return NULL;
340 }
341 #endif                          /* debug_memory */
342
343 void drm_core_ioremap(struct drm_map *map, struct drm_device *dev)
344 {
345         if (drm_core_has_AGP(dev) &&
346             dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
347                 map->handle = agp_remap(map->offset, map->size, dev);
348         else
349                 map->handle = ioremap(map->offset, map->size);
350 }
351 EXPORT_SYMBOL_GPL(drm_core_ioremap);
352
353 void drm_core_ioremapfree(struct drm_map *map, struct drm_device *dev)
354 {
355         if (!map->handle || !map->size)
356                 return;
357
358         if (drm_core_has_AGP(dev) &&
359             dev->agp && dev->agp->cant_use_aperture && map->type == _DRM_AGP)
360                 vunmap(map->handle);
361         else
362                 iounmap(map->handle);
363 }
364 EXPORT_SYMBOL_GPL(drm_core_ioremapfree);