A minor function interface change and some memcpy bugfixing.
[profile/ivi/libdrm.git] / linux-core / drm_compat.c
1 /**************************************************************************
2  * 
3  * This kernel module is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU General Public License as
5  * published by the Free Software Foundation; either version 2 of the
6  * License, or (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16  * 
17  **************************************************************************/
18 /*
19  * This code provides access to unexported mm kernel features. It is necessary
20  * to use the new DRM memory manager code with kernels that don't support it
21  * directly.
22  *
23  * Authors: Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
24  *          Linux kernel mm subsystem authors. 
25  *          (Most code taken from there).
26  */
27
28 #include "drmP.h"
29
30 #if defined(CONFIG_X86) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
31
32 /*
33  * These have bad performance in the AGP module for the indicated kernel versions.
34  */
35
36 int drm_map_page_into_agp(struct page *page)
37 {
38         int i;
39         i = change_page_attr(page, 1, PAGE_KERNEL_NOCACHE);
40         /* Caller's responsibility to call global_flush_tlb() for
41          * performance reasons */
42         return i;
43 }
44
45 int drm_unmap_page_from_agp(struct page *page)
46 {
47         int i;
48         i = change_page_attr(page, 1, PAGE_KERNEL);
49         /* Caller's responsibility to call global_flush_tlb() for
50          * performance reasons */
51         return i;
52 }
53 #endif 
54
55
56 #if  (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
57
58 /*
59  * The protection map was exported in 2.6.19
60  */
61
62 pgprot_t vm_get_page_prot(unsigned long vm_flags)
63 {
64 #ifdef MODULE
65         static pgprot_t drm_protection_map[16] = {
66                 __P000, __P001, __P010, __P011, __P100, __P101, __P110, __P111,
67                 __S000, __S001, __S010, __S011, __S100, __S101, __S110, __S111
68         };
69
70         return drm_protection_map[vm_flags & 0x0F];
71 #else
72         extern pgprot_t protection_map[];
73         return protection_map[vm_flags & 0x0F];
74 #endif
75 };
76 #endif
77
78
79 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15))
80
81 /*
82  * vm code for kernels below 2,6,15 in which version a major vm write
83  * occured. This implement a simple straightforward 
84  * version similar to what's going to be
85  * in kernel 2.6.20+?
86  */ 
87
88 static int drm_pte_is_clear(struct vm_area_struct *vma,
89                             unsigned long addr)
90 {
91         struct mm_struct *mm = vma->vm_mm;
92         int ret = 1;
93         pte_t *pte;
94         pmd_t *pmd;
95         pud_t *pud;
96         pgd_t *pgd;
97         
98
99         spin_lock(&mm->page_table_lock);
100         pgd = pgd_offset(mm, addr);
101         if (pgd_none(*pgd))
102                 goto unlock;
103         pud = pud_offset(pgd, addr);
104         if (pud_none(*pud))
105                 goto unlock;
106         pmd = pmd_offset(pud, addr);
107         if (pmd_none(*pmd))
108                 goto unlock;
109         pte = pte_offset_map(pmd, addr);
110         if (!pte)
111                 goto unlock;
112         ret = pte_none(*pte);
113         pte_unmap(pte);
114  unlock:        
115         spin_unlock(&mm->page_table_lock);
116         return ret;
117 }
118         
119 int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr, 
120                   unsigned long pfn, pgprot_t pgprot)
121 {
122         int ret;
123         if (!drm_pte_is_clear(vma, addr))
124                 return -EBUSY;
125
126         ret = io_remap_pfn_range(vma, addr, pfn, PAGE_SIZE, pgprot);
127         return ret;
128 }
129
130 static struct {
131         spinlock_t lock;
132         struct page *dummy_page;
133         atomic_t present;
134 } drm_np_retry = 
135 {SPIN_LOCK_UNLOCKED, NOPAGE_OOM, ATOMIC_INIT(0)};
136
137 struct page * get_nopage_retry(void)
138 {
139         if (atomic_read(&drm_np_retry.present) == 0) {
140                 struct page *page = alloc_page(GFP_KERNEL);
141                 if (!page)
142                         return NOPAGE_OOM;
143                 spin_lock(&drm_np_retry.lock);
144                 drm_np_retry.dummy_page = page;
145                 atomic_set(&drm_np_retry.present,1);
146                 spin_unlock(&drm_np_retry.lock);
147         }
148         get_page(drm_np_retry.dummy_page);
149         return drm_np_retry.dummy_page;
150 }
151
152 void free_nopage_retry(void)
153 {
154         if (atomic_read(&drm_np_retry.present) == 1) {
155                 spin_lock(&drm_np_retry.lock);
156                 __free_page(drm_np_retry.dummy_page);
157                 drm_np_retry.dummy_page = NULL;
158                 atomic_set(&drm_np_retry.present, 0);
159                 spin_unlock(&drm_np_retry.lock);
160         }
161 }
162
163 struct page *drm_bo_vm_nopage(struct vm_area_struct *vma,
164                                unsigned long address, 
165                                int *type)
166 {
167         struct fault_data data;
168
169         if (type)
170                 *type = VM_FAULT_MINOR;
171
172         data.address = address;
173         data.vma = vma;
174         drm_bo_vm_fault(vma, &data);
175         switch (data.type) {
176         case VM_FAULT_OOM:
177                 return NOPAGE_OOM;
178         case VM_FAULT_SIGBUS:
179                 return NOPAGE_SIGBUS;
180         default:
181                 break;
182         }
183
184         return NOPAGE_REFAULT;
185 }
186
187 #endif
188
189 #ifdef DRM_ODD_MM_COMPAT
190
191 /*
192  * VM compatibility code for 2.6.15-2.6.19(?). This code implements a complicated
193  * workaround for a single BUG statement in do_no_page in these versions. The
194  * tricky thing is that we need to take the mmap_sem in exclusive mode for _all_
195  * vmas mapping the ttm, before dev->struct_mutex is taken. The way we do this is to 
196  * check first take the dev->struct_mutex, and then trylock all mmap_sems. If this
197  * fails for a single mmap_sem, we have to release all sems and the dev->struct_mutex,
198  * release the cpu and retry. We also need to keep track of all vmas mapping the ttm.
199  * phew.
200  */
201
202 typedef struct p_mm_entry {
203         struct list_head head;
204         struct mm_struct *mm;
205         atomic_t refcount;
206         int locked;
207 } p_mm_entry_t;
208
209 typedef struct vma_entry {
210         struct list_head head;
211         struct vm_area_struct *vma;
212 } vma_entry_t;
213
214
215 struct page *drm_bo_vm_nopage(struct vm_area_struct *vma,
216                                unsigned long address, 
217                                int *type)
218 {
219         drm_buffer_object_t *bo = (drm_buffer_object_t *) vma->vm_private_data;
220         unsigned long page_offset;
221         struct page *page;
222         drm_ttm_t *ttm; 
223         drm_device_t *dev;
224
225         mutex_lock(&bo->mutex);
226
227         if (type)
228                 *type = VM_FAULT_MINOR;
229
230         if (address > vma->vm_end) {
231                 page = NOPAGE_SIGBUS;
232                 goto out_unlock;
233         }
234         
235         dev = bo->dev;
236
237         if (drm_mem_reg_is_pci(dev, &bo->mem)) {
238                 DRM_ERROR("Invalid compat nopage.\n");
239                 page = NOPAGE_SIGBUS;
240                 goto out_unlock;
241         }
242
243         ttm = bo->ttm;
244         drm_ttm_fixup_caching(ttm);
245         page_offset = (address - vma->vm_start) >> PAGE_SHIFT;
246         page = drm_ttm_get_page(ttm, page_offset);
247         if (!page) {
248                 page = NOPAGE_OOM;
249                 goto out_unlock;
250         }
251
252         get_page(page);
253 out_unlock:
254         mutex_unlock(&bo->mutex);
255         return page;
256 }
257
258
259
260
261 int drm_bo_map_bound(struct vm_area_struct *vma)
262 {
263         drm_buffer_object_t *bo = (drm_buffer_object_t *)vma->vm_private_data;
264         int ret = 0;
265         unsigned long bus_base;
266         unsigned long bus_offset;
267         unsigned long bus_size;
268         
269         ret = drm_bo_pci_offset(bo->dev, &bo->mem, &bus_base, 
270                                 &bus_offset, &bus_size);
271         BUG_ON(ret);
272
273         if (bus_size) {
274                 unsigned long pfn = (bus_base + bus_offset) >> PAGE_SHIFT;
275                 pgprot_t pgprot = drm_io_prot(_DRM_AGP, vma);
276                 ret = io_remap_pfn_range(vma, vma->vm_start, pfn,
277                                          vma->vm_end - vma->vm_start,
278                                          pgprot);
279         }
280
281         return ret;
282 }
283         
284
285 int drm_bo_add_vma(drm_buffer_object_t * bo, struct vm_area_struct *vma)
286 {
287         p_mm_entry_t *entry, *n_entry;
288         vma_entry_t *v_entry;
289         struct mm_struct *mm = vma->vm_mm;
290
291         v_entry = drm_ctl_alloc(sizeof(*v_entry), DRM_MEM_BUFOBJ);
292         if (!v_entry) {
293                 DRM_ERROR("Allocation of vma pointer entry failed\n");
294                 return -ENOMEM;
295         }
296         v_entry->vma = vma;
297
298         list_add_tail(&v_entry->head, &bo->vma_list);
299
300         list_for_each_entry(entry, &bo->p_mm_list, head) {
301                 if (mm == entry->mm) {
302                         atomic_inc(&entry->refcount);
303                         return 0;
304                 } else if ((unsigned long)mm < (unsigned long)entry->mm) ;
305         }
306
307         n_entry = drm_ctl_alloc(sizeof(*n_entry), DRM_MEM_BUFOBJ);
308         if (!n_entry) {
309                 DRM_ERROR("Allocation of process mm pointer entry failed\n");
310                 return -ENOMEM;
311         }
312         INIT_LIST_HEAD(&n_entry->head);
313         n_entry->mm = mm;
314         n_entry->locked = 0;
315         atomic_set(&n_entry->refcount, 0);
316         list_add_tail(&n_entry->head, &entry->head);
317
318         return 0;
319 }
320
321 void drm_bo_delete_vma(drm_buffer_object_t * bo, struct vm_area_struct *vma)
322 {
323         p_mm_entry_t *entry, *n;
324         vma_entry_t *v_entry, *v_n;
325         int found = 0;
326         struct mm_struct *mm = vma->vm_mm;
327
328         list_for_each_entry_safe(v_entry, v_n, &bo->vma_list, head) {
329                 if (v_entry->vma == vma) {
330                         found = 1;
331                         list_del(&v_entry->head);
332                         drm_ctl_free(v_entry, sizeof(*v_entry), DRM_MEM_BUFOBJ);
333                         break;
334                 }
335         }
336         BUG_ON(!found);
337
338         list_for_each_entry_safe(entry, n, &bo->p_mm_list, head) {
339                 if (mm == entry->mm) {
340                         if (atomic_add_negative(-1, &entry->refcount)) {
341                                 list_del(&entry->head);
342                                 BUG_ON(entry->locked);
343                                 drm_ctl_free(entry, sizeof(*entry), DRM_MEM_BUFOBJ);
344                         }
345                         return;
346                 }
347         }
348         BUG_ON(1);
349 }
350
351
352
353 int drm_bo_lock_kmm(drm_buffer_object_t * bo)
354 {
355         p_mm_entry_t *entry;
356         int lock_ok = 1;
357         
358         list_for_each_entry(entry, &bo->p_mm_list, head) {
359                 BUG_ON(entry->locked);
360                 if (!down_write_trylock(&entry->mm->mmap_sem)) {
361                         lock_ok = 0;
362                         break;
363                 }
364                 entry->locked = 1;
365         }
366
367         if (lock_ok)
368                 return 0;
369
370         list_for_each_entry(entry, &bo->p_mm_list, head) {
371                 if (!entry->locked) 
372                         break;
373                 up_write(&entry->mm->mmap_sem);
374                 entry->locked = 0;
375         }
376
377         /*
378          * Possible deadlock. Try again. Our callers should handle this
379          * and restart.
380          */
381
382         return -EAGAIN;
383 }
384
385 void drm_bo_unlock_kmm(drm_buffer_object_t * bo)
386 {
387         p_mm_entry_t *entry;
388         
389         list_for_each_entry(entry, &bo->p_mm_list, head) {
390                 BUG_ON(!entry->locked);
391                 up_write(&entry->mm->mmap_sem);
392                 entry->locked = 0;
393         }
394 }
395
396 int drm_bo_remap_bound(drm_buffer_object_t *bo) 
397 {
398         vma_entry_t *v_entry;
399         int ret = 0;
400
401         if (drm_mem_reg_is_pci(bo->dev, &bo->mem)) {
402                 list_for_each_entry(v_entry, &bo->vma_list, head) {
403                         ret = drm_bo_map_bound(v_entry->vma);
404                         if (ret)
405                                 break;
406                 }
407         }
408
409         return ret;
410 }
411
412 void drm_bo_finish_unmap(drm_buffer_object_t *bo)
413 {
414         vma_entry_t *v_entry;
415
416         list_for_each_entry(v_entry, &bo->vma_list, head) {
417                 v_entry->vma->vm_flags &= ~VM_PFNMAP; 
418         }
419 }       
420
421 #endif
422