ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / char / drm / drm_memory.h
1 /** 
2  * \file drm_memory.h 
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/config.h>
37 #include <linux/highmem.h>
38 #include "drmP.h"
39
40 /**
41  * Cut down version of drm_memory_debug.h, which used to be called
42  * drm_memory.h.  If you want the debug functionality, change 0 to 1
43  * below.
44  */
45 #define DEBUG_MEMORY 0
46
47 #if __REALLY_HAVE_AGP
48
49 #include <linux/vmalloc.h>
50
51 #ifdef HAVE_PAGE_AGP
52 #include <asm/agp.h>
53 #else
54 # ifdef __powerpc__
55 #  define PAGE_AGP      __pgprot(_PAGE_KERNEL | _PAGE_NO_CACHE)
56 # else
57 #  define PAGE_AGP      PAGE_KERNEL
58 # endif
59 #endif
60
61 #include <asm/tlbflush.h>
62
63 /*
64  * Find the drm_map that covers the range [offset, offset+size).
65  */
66 static inline drm_map_t *
67 drm_lookup_map (unsigned long offset, unsigned long size, drm_device_t *dev)
68 {
69         struct list_head *list;
70         drm_map_list_t *r_list;
71         drm_map_t *map;
72
73         list_for_each(list, &dev->maplist->head) {
74                 r_list = (drm_map_list_t *) list;
75                 map = r_list->map;
76                 if (!map)
77                         continue;
78                 if (map->offset <= offset && (offset + size) <= (map->offset + map->size))
79                         return map;
80         }
81         return NULL;
82 }
83
84 static inline void *
85 agp_remap (unsigned long offset, unsigned long size, drm_device_t *dev)
86 {
87         unsigned long *phys_addr_map, i, num_pages = PAGE_ALIGN(size) / PAGE_SIZE;
88         struct drm_agp_mem *agpmem;
89         struct page **page_map;
90         void *addr;
91
92         size = PAGE_ALIGN(size);
93
94 #ifdef __alpha__
95         offset -= dev->hose->mem_space->start;
96 #endif
97
98         for (agpmem = dev->agp->memory; agpmem; agpmem = agpmem->next)
99                 if (agpmem->bound <= offset
100                     && (agpmem->bound + (agpmem->pages << PAGE_SHIFT)) >= (offset + size))
101                         break;
102         if (!agpmem)
103                 return NULL;
104
105         /*
106          * OK, we're mapping AGP space on a chipset/platform on which memory accesses by
107          * the CPU do not get remapped by the GART.  We fix this by using the kernel's
108          * page-table instead (that's probably faster anyhow...).
109          */
110         /* note: use vmalloc() because num_pages could be large... */
111         page_map = vmalloc(num_pages * sizeof(struct page *));
112         if (!page_map)
113                 return NULL;
114
115         phys_addr_map = agpmem->memory->memory + (offset - agpmem->bound) / PAGE_SIZE;
116         for (i = 0; i < num_pages; ++i)
117                 page_map[i] = pfn_to_page(phys_addr_map[i] >> PAGE_SHIFT);
118         addr = vmap(page_map, num_pages, VM_IOREMAP, PAGE_AGP);
119         vfree(page_map);
120
121         return addr;
122 }
123
124 static inline unsigned long
125 drm_follow_page (void *vaddr)
126 {
127         pgd_t *pgd = pgd_offset_k((unsigned long) vaddr);
128         pmd_t *pmd = pmd_offset(pgd, (unsigned long) vaddr);
129         pte_t *ptep = pte_offset_kernel(pmd, (unsigned long) vaddr);
130         return pte_pfn(*ptep) << PAGE_SHIFT;
131 }
132
133 #endif /* __REALLY_HAVE_AGP */
134
135 static inline void *drm_ioremap(unsigned long offset, unsigned long size, drm_device_t *dev)
136 {
137 #if __REALLY_HAVE_AGP
138         if (dev->agp && dev->agp->cant_use_aperture) {
139                 drm_map_t *map = drm_lookup_map(offset, size, dev);
140
141                 if (map && map->type == _DRM_AGP)
142                         return agp_remap(offset, size, dev);
143         }
144 #endif
145
146         return ioremap(offset, size);
147 }
148
149 static inline void *drm_ioremap_nocache(unsigned long offset, unsigned long size,
150                                         drm_device_t *dev)
151 {
152 #if __REALLY_HAVE_AGP
153         if (dev->agp && dev->agp->cant_use_aperture) {
154                 drm_map_t *map = drm_lookup_map(offset, size, dev);
155
156                 if (map && map->type == _DRM_AGP)
157                         return agp_remap(offset, size, dev);
158         }
159 #endif
160
161         return ioremap_nocache(offset, size);
162 }
163
164 static inline void drm_ioremapfree(void *pt, unsigned long size, drm_device_t *dev)
165 {
166 #if __REALLY_HAVE_AGP
167         /*
168          * This is a bit ugly.  It would be much cleaner if the DRM API would use separate
169          * routines for handling mappings in the AGP space.  Hopefully this can be done in
170          * a future revision of the interface...
171          */
172         if (dev->agp && dev->agp->cant_use_aperture
173             && ((unsigned long) pt >= VMALLOC_START && (unsigned long) pt < VMALLOC_END))
174         {
175                 unsigned long offset;
176                 drm_map_t *map;
177
178                 offset = drm_follow_page(pt) | ((unsigned long) pt & ~PAGE_MASK);
179                 map = drm_lookup_map(offset, size, dev);
180                 if (map && map->type == _DRM_AGP) {
181                         vunmap(pt);
182                         return;
183                 }
184         }
185 #endif
186
187         iounmap(pt);
188 }
189
190 #if DEBUG_MEMORY
191 #include "drm_memory_debug.h"
192 #else
193
194 /** No-op. */
195 void DRM(mem_init)(void)
196 {
197 }
198
199 /**
200  * Called when "/proc/dri/%dev%/mem" is read.
201  * 
202  * \param buf output buffer.
203  * \param start start of output data.
204  * \param offset requested start offset.
205  * \param len requested number of bytes.
206  * \param eof whether there is no more data to return.
207  * \param data private data.
208  * \return number of written bytes.
209  *
210  * No-op. 
211  */
212 int DRM(mem_info)(char *buf, char **start, off_t offset,
213                   int len, int *eof, void *data)
214 {
215         return 0;
216 }
217
218 /** Wrapper around kmalloc() */
219 void *DRM(alloc)(size_t size, int area)
220 {
221         return kmalloc(size, GFP_KERNEL);
222 }
223
224 /** Wrapper around kmalloc() */
225 void *DRM(calloc)(size_t size, size_t nmemb, int area)
226 {
227         void *addr;
228
229         addr = kmalloc(size * nmemb, GFP_KERNEL);
230         if (addr != NULL)
231                 memset((void *)addr, 0, size * nmemb);
232
233         return addr;
234 }
235
236 /** Wrapper around kmalloc() and kfree() */
237 void *DRM(realloc)(void *oldpt, size_t oldsize, size_t size, int area)
238 {
239         void *pt;
240
241         if (!(pt = kmalloc(size, GFP_KERNEL))) return NULL;
242         if (oldpt && oldsize) {
243                 memcpy(pt, oldpt, oldsize);
244                 kfree(oldpt);
245         }
246         return pt;
247 }
248
249 /** Wrapper around kfree() */
250 void DRM(free)(void *pt, size_t size, int area)
251 {
252         kfree(pt);
253 }
254
255 /**
256  * Allocate pages.
257  *
258  * \param order size order.
259  * \param area memory area. (Not used.)
260  * \return page address on success, or zero on failure.
261  *
262  * Allocate and reserve free pages.
263  */
264 unsigned long DRM(alloc_pages)(int order, int area)
265 {
266         unsigned long address;
267         unsigned long bytes       = PAGE_SIZE << order;
268         unsigned long addr;
269         unsigned int  sz;
270
271         address = __get_free_pages(GFP_KERNEL, order);
272         if (!address) 
273                 return 0;
274
275                                 /* Zero */
276         memset((void *)address, 0, bytes);
277
278                                 /* Reserve */
279         for (addr = address, sz = bytes;
280              sz > 0;
281              addr += PAGE_SIZE, sz -= PAGE_SIZE) {
282                 SetPageReserved(virt_to_page(addr));
283         }
284
285         return address;
286 }
287
288 /**
289  * Free pages.
290  * 
291  * \param address address of the pages to free.
292  * \param order size order.
293  * \param area memory area. (Not used.)
294  *
295  * Unreserve and free pages allocated by alloc_pages().
296  */
297 void DRM(free_pages)(unsigned long address, int order, int area)
298 {
299         unsigned long bytes = PAGE_SIZE << order;
300         unsigned long addr;
301         unsigned int  sz;
302
303         if (!address) 
304                 return;
305
306         /* Unreserve */
307         for (addr = address, sz = bytes;
308              sz > 0;
309              addr += PAGE_SIZE, sz -= PAGE_SIZE) {
310                 ClearPageReserved(virt_to_page(addr));
311         }
312
313         free_pages(address, order);
314 }
315
316 /** Wrapper around drm_ioremap() */
317 void *DRM(ioremap)(unsigned long offset, unsigned long size, drm_device_t *dev)
318 {
319         return drm_ioremap(offset, size, dev);
320 }
321
322 /** Wrapper around drm_ioremap_nocache() */
323 void *DRM(ioremap_nocache)(unsigned long offset, unsigned long size, drm_device_t *dev)
324 {
325         return drm_ioremap_nocache(offset, size, dev);
326 }
327
328 /** Wrapper around drm_iounmap() */
329 void DRM(ioremapfree)(void *pt, unsigned long size, drm_device_t *dev)
330 {
331         drm_ioremapfree(pt, size, dev);
332 }
333
334 #if __REALLY_HAVE_AGP
335 /** Wrapper around agp_allocate_memory() */
336 DRM_AGP_MEM *DRM(alloc_agp)(int pages, u32 type)
337 {
338         return DRM(agp_allocate_memory)(pages, type);
339 }
340
341 /** Wrapper around agp_free_memory() */
342 int DRM(free_agp)(DRM_AGP_MEM *handle, int pages)
343 {
344         return DRM(agp_free_memory)(handle) ? 0 : -EINVAL;
345 }
346
347 /** Wrapper around agp_bind_memory() */
348 int DRM(bind_agp)(DRM_AGP_MEM *handle, unsigned int start)
349 {
350         return DRM(agp_bind_memory)(handle, start);
351 }
352
353 /** Wrapper around agp_unbind_memory() */
354 int DRM(unbind_agp)(DRM_AGP_MEM *handle)
355 {
356         return DRM(agp_unbind_memory)(handle);
357 }
358 #endif /* agp */
359 #endif /* debug_memory */