Merge branch 'i915-pageflip'
[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.19+
86  * Kernels below 2.6.15 use nopage whereas 2.6.19 and upwards use
87  * nopfn.
88  */ 
89
90 static struct {
91         spinlock_t lock;
92         struct page *dummy_page;
93         atomic_t present;
94 } drm_np_retry = 
95 {SPIN_LOCK_UNLOCKED, NOPAGE_OOM, ATOMIC_INIT(0)};
96
97
98 static struct page *drm_bo_vm_fault(struct vm_area_struct *vma, 
99                                     struct fault_data *data);
100
101
102 struct page * get_nopage_retry(void)
103 {
104         if (atomic_read(&drm_np_retry.present) == 0) {
105                 struct page *page = alloc_page(GFP_KERNEL);
106                 if (!page)
107                         return NOPAGE_OOM;
108                 spin_lock(&drm_np_retry.lock);
109                 drm_np_retry.dummy_page = page;
110                 atomic_set(&drm_np_retry.present,1);
111                 spin_unlock(&drm_np_retry.lock);
112         }
113         get_page(drm_np_retry.dummy_page);
114         return drm_np_retry.dummy_page;
115 }
116
117 void free_nopage_retry(void)
118 {
119         if (atomic_read(&drm_np_retry.present) == 1) {
120                 spin_lock(&drm_np_retry.lock);
121                 __free_page(drm_np_retry.dummy_page);
122                 drm_np_retry.dummy_page = NULL;
123                 atomic_set(&drm_np_retry.present, 0);
124                 spin_unlock(&drm_np_retry.lock);
125         }
126 }
127
128 struct page *drm_bo_vm_nopage(struct vm_area_struct *vma,
129                                unsigned long address, 
130                                int *type)
131 {
132         struct fault_data data;
133
134         if (type)
135                 *type = VM_FAULT_MINOR;
136
137         data.address = address;
138         data.vma = vma;
139         drm_bo_vm_fault(vma, &data);
140         switch (data.type) {
141         case VM_FAULT_OOM:
142                 return NOPAGE_OOM;
143         case VM_FAULT_SIGBUS:
144                 return NOPAGE_SIGBUS;
145         default:
146                 break;
147         }
148
149         return NOPAGE_REFAULT;
150 }
151
152 #endif
153
154 #if !defined(DRM_FULL_MM_COMPAT) && \
155   ((LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)) || \
156    (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)))
157
158 static int drm_pte_is_clear(struct vm_area_struct *vma,
159                             unsigned long addr)
160 {
161         struct mm_struct *mm = vma->vm_mm;
162         int ret = 1;
163         pte_t *pte;
164         pmd_t *pmd;
165         pud_t *pud;
166         pgd_t *pgd;
167
168         spin_lock(&mm->page_table_lock);
169         pgd = pgd_offset(mm, addr);
170         if (pgd_none(*pgd))
171                 goto unlock;
172         pud = pud_offset(pgd, addr);
173         if (pud_none(*pud))
174                 goto unlock;
175         pmd = pmd_offset(pud, addr);
176         if (pmd_none(*pmd))
177                 goto unlock;
178         pte = pte_offset_map(pmd, addr);
179         if (!pte)
180                 goto unlock;
181         ret = pte_none(*pte);
182         pte_unmap(pte);
183  unlock:
184         spin_unlock(&mm->page_table_lock);
185         return ret;
186 }
187
188 static int vm_insert_pfn(struct vm_area_struct *vma, unsigned long addr,
189                   unsigned long pfn)
190 {
191         int ret;
192         if (!drm_pte_is_clear(vma, addr))
193                 return -EBUSY;
194
195         ret = io_remap_pfn_range(vma, addr, pfn, PAGE_SIZE, vma->vm_page_prot);
196         return ret;
197 }
198
199 static struct page *drm_bo_vm_fault(struct vm_area_struct *vma, 
200                                     struct fault_data *data)
201 {
202         unsigned long address = data->address;
203         drm_buffer_object_t *bo = (drm_buffer_object_t *) vma->vm_private_data;
204         unsigned long page_offset;
205         struct page *page = NULL;
206         drm_ttm_t *ttm; 
207         drm_device_t *dev;
208         unsigned long pfn;
209         int err;
210         unsigned long bus_base;
211         unsigned long bus_offset;
212         unsigned long bus_size;
213         
214
215         mutex_lock(&bo->mutex);
216
217         err = drm_bo_wait(bo, 0, 1, 0);
218         if (err) {
219                 data->type = (err == -EAGAIN) ? 
220                         VM_FAULT_MINOR : VM_FAULT_SIGBUS;
221                 goto out_unlock;
222         }
223         
224         
225         /*
226          * If buffer happens to be in a non-mappable location,
227          * move it to a mappable.
228          */
229
230         if (!(bo->mem.flags & DRM_BO_FLAG_MAPPABLE)) {
231                 unsigned long _end = jiffies + 3*DRM_HZ;
232                 uint32_t new_mask = bo->mem.mask |
233                         DRM_BO_FLAG_MAPPABLE |
234                         DRM_BO_FLAG_FORCE_MAPPABLE;
235
236                 do {
237                         err = drm_bo_move_buffer(bo, new_mask, 0, 0);
238                 } while((err == -EAGAIN) && !time_after_eq(jiffies, _end));
239
240                 if (err) {
241                         DRM_ERROR("Timeout moving buffer to mappable location.\n");
242                         data->type = VM_FAULT_SIGBUS;
243                         goto out_unlock;
244                 }
245         }
246
247         if (address > vma->vm_end) {
248                 data->type = VM_FAULT_SIGBUS;
249                 goto out_unlock;
250         }
251
252         dev = bo->dev;
253         err = drm_bo_pci_offset(dev, &bo->mem, &bus_base, &bus_offset, 
254                                 &bus_size);
255
256         if (err) {
257                 data->type = VM_FAULT_SIGBUS;
258                 goto out_unlock;
259         }
260
261         page_offset = (address - vma->vm_start) >> PAGE_SHIFT;
262
263         if (bus_size) {
264                 drm_mem_type_manager_t *man = &dev->bm.man[bo->mem.mem_type];
265
266                 pfn = ((bus_base + bus_offset) >> PAGE_SHIFT) + page_offset;
267                 vma->vm_page_prot = drm_io_prot(man->drm_bus_maptype, vma);
268         } else {
269                 ttm = bo->ttm;
270
271                 drm_ttm_fixup_caching(ttm);
272                 page = drm_ttm_get_page(ttm, page_offset);
273                 if (!page) {
274                         data->type = VM_FAULT_OOM;
275                         goto out_unlock;
276                 }
277                 pfn = page_to_pfn(page);
278                 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
279         }
280         
281         err = vm_insert_pfn(vma, address, pfn);
282
283         if (!err || err == -EBUSY) 
284                 data->type = VM_FAULT_MINOR; 
285         else
286                 data->type = VM_FAULT_OOM;
287 out_unlock:
288         mutex_unlock(&bo->mutex);
289         return NULL;
290 }
291
292 #endif
293
294 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19)) && \
295   !defined(DRM_FULL_MM_COMPAT)
296
297 /**
298  */
299
300 unsigned long drm_bo_vm_nopfn(struct vm_area_struct * vma,
301                            unsigned long address)
302 {
303         struct fault_data data;
304         data.address = address;
305
306         (void) drm_bo_vm_fault(vma, &data);
307         if (data.type == VM_FAULT_OOM)
308                 return NOPFN_OOM;
309         else if (data.type == VM_FAULT_SIGBUS)
310                 return NOPFN_SIGBUS;
311
312         /*
313          * pfn already set.
314          */
315
316         return 0;
317 }
318 #endif
319
320
321 #ifdef DRM_ODD_MM_COMPAT
322
323 /*
324  * VM compatibility code for 2.6.15-2.6.18. This code implements a complicated
325  * workaround for a single BUG statement in do_no_page in these versions. The
326  * tricky thing is that we need to take the mmap_sem in exclusive mode for _all_
327  * vmas mapping the ttm, before dev->struct_mutex is taken. The way we do this is to 
328  * check first take the dev->struct_mutex, and then trylock all mmap_sems. If this
329  * fails for a single mmap_sem, we have to release all sems and the dev->struct_mutex,
330  * release the cpu and retry. We also need to keep track of all vmas mapping the ttm.
331  * phew.
332  */
333
334 typedef struct p_mm_entry {
335         struct list_head head;
336         struct mm_struct *mm;
337         atomic_t refcount;
338         int locked;
339 } p_mm_entry_t;
340
341 typedef struct vma_entry {
342         struct list_head head;
343         struct vm_area_struct *vma;
344 } vma_entry_t;
345
346
347 struct page *drm_bo_vm_nopage(struct vm_area_struct *vma,
348                                unsigned long address, 
349                                int *type)
350 {
351         drm_buffer_object_t *bo = (drm_buffer_object_t *) vma->vm_private_data;
352         unsigned long page_offset;
353         struct page *page;
354         drm_ttm_t *ttm; 
355         drm_device_t *dev;
356
357         mutex_lock(&bo->mutex);
358
359         if (type)
360                 *type = VM_FAULT_MINOR;
361
362         if (address > vma->vm_end) {
363                 page = NOPAGE_SIGBUS;
364                 goto out_unlock;
365         }
366         
367         dev = bo->dev;
368
369         if (drm_mem_reg_is_pci(dev, &bo->mem)) {
370                 DRM_ERROR("Invalid compat nopage.\n");
371                 page = NOPAGE_SIGBUS;
372                 goto out_unlock;
373         }
374
375         ttm = bo->ttm;
376         drm_ttm_fixup_caching(ttm);
377         page_offset = (address - vma->vm_start) >> PAGE_SHIFT;
378         page = drm_ttm_get_page(ttm, page_offset);
379         if (!page) {
380                 page = NOPAGE_OOM;
381                 goto out_unlock;
382         }
383
384         get_page(page);
385 out_unlock:
386         mutex_unlock(&bo->mutex);
387         return page;
388 }
389
390
391
392
393 int drm_bo_map_bound(struct vm_area_struct *vma)
394 {
395         drm_buffer_object_t *bo = (drm_buffer_object_t *)vma->vm_private_data;
396         int ret = 0;
397         unsigned long bus_base;
398         unsigned long bus_offset;
399         unsigned long bus_size;
400         
401         ret = drm_bo_pci_offset(bo->dev, &bo->mem, &bus_base, 
402                                 &bus_offset, &bus_size);
403         BUG_ON(ret);
404
405         if (bus_size) {
406                 drm_mem_type_manager_t *man = &bo->dev->bm.man[bo->mem.mem_type];
407                 unsigned long pfn = (bus_base + bus_offset) >> PAGE_SHIFT;
408                 pgprot_t pgprot = drm_io_prot(man->drm_bus_maptype, vma);
409                 ret = io_remap_pfn_range(vma, vma->vm_start, pfn,
410                                          vma->vm_end - vma->vm_start,
411                                          pgprot);
412         }
413
414         return ret;
415 }
416         
417
418 int drm_bo_add_vma(drm_buffer_object_t * bo, struct vm_area_struct *vma)
419 {
420         p_mm_entry_t *entry, *n_entry;
421         vma_entry_t *v_entry;
422         struct mm_struct *mm = vma->vm_mm;
423
424         v_entry = drm_ctl_alloc(sizeof(*v_entry), DRM_MEM_BUFOBJ);
425         if (!v_entry) {
426                 DRM_ERROR("Allocation of vma pointer entry failed\n");
427                 return -ENOMEM;
428         }
429         v_entry->vma = vma;
430
431         list_add_tail(&v_entry->head, &bo->vma_list);
432
433         list_for_each_entry(entry, &bo->p_mm_list, head) {
434                 if (mm == entry->mm) {
435                         atomic_inc(&entry->refcount);
436                         return 0;
437                 } else if ((unsigned long)mm < (unsigned long)entry->mm) ;
438         }
439
440         n_entry = drm_ctl_alloc(sizeof(*n_entry), DRM_MEM_BUFOBJ);
441         if (!n_entry) {
442                 DRM_ERROR("Allocation of process mm pointer entry failed\n");
443                 return -ENOMEM;
444         }
445         INIT_LIST_HEAD(&n_entry->head);
446         n_entry->mm = mm;
447         n_entry->locked = 0;
448         atomic_set(&n_entry->refcount, 0);
449         list_add_tail(&n_entry->head, &entry->head);
450
451         return 0;
452 }
453
454 void drm_bo_delete_vma(drm_buffer_object_t * bo, struct vm_area_struct *vma)
455 {
456         p_mm_entry_t *entry, *n;
457         vma_entry_t *v_entry, *v_n;
458         int found = 0;
459         struct mm_struct *mm = vma->vm_mm;
460
461         list_for_each_entry_safe(v_entry, v_n, &bo->vma_list, head) {
462                 if (v_entry->vma == vma) {
463                         found = 1;
464                         list_del(&v_entry->head);
465                         drm_ctl_free(v_entry, sizeof(*v_entry), DRM_MEM_BUFOBJ);
466                         break;
467                 }
468         }
469         BUG_ON(!found);
470
471         list_for_each_entry_safe(entry, n, &bo->p_mm_list, head) {
472                 if (mm == entry->mm) {
473                         if (atomic_add_negative(-1, &entry->refcount)) {
474                                 list_del(&entry->head);
475                                 BUG_ON(entry->locked);
476                                 drm_ctl_free(entry, sizeof(*entry), DRM_MEM_BUFOBJ);
477                         }
478                         return;
479                 }
480         }
481         BUG_ON(1);
482 }
483
484
485
486 int drm_bo_lock_kmm(drm_buffer_object_t * bo)
487 {
488         p_mm_entry_t *entry;
489         int lock_ok = 1;
490         
491         list_for_each_entry(entry, &bo->p_mm_list, head) {
492                 BUG_ON(entry->locked);
493                 if (!down_write_trylock(&entry->mm->mmap_sem)) {
494                         lock_ok = 0;
495                         break;
496                 }
497                 entry->locked = 1;
498         }
499
500         if (lock_ok)
501                 return 0;
502
503         list_for_each_entry(entry, &bo->p_mm_list, head) {
504                 if (!entry->locked) 
505                         break;
506                 up_write(&entry->mm->mmap_sem);
507                 entry->locked = 0;
508         }
509
510         /*
511          * Possible deadlock. Try again. Our callers should handle this
512          * and restart.
513          */
514
515         return -EAGAIN;
516 }
517
518 void drm_bo_unlock_kmm(drm_buffer_object_t * bo)
519 {
520         p_mm_entry_t *entry;
521         
522         list_for_each_entry(entry, &bo->p_mm_list, head) {
523                 BUG_ON(!entry->locked);
524                 up_write(&entry->mm->mmap_sem);
525                 entry->locked = 0;
526         }
527 }
528
529 int drm_bo_remap_bound(drm_buffer_object_t *bo) 
530 {
531         vma_entry_t *v_entry;
532         int ret = 0;
533
534         if (drm_mem_reg_is_pci(bo->dev, &bo->mem)) {
535                 list_for_each_entry(v_entry, &bo->vma_list, head) {
536                         ret = drm_bo_map_bound(v_entry->vma);
537                         if (ret)
538                                 break;
539                 }
540         }
541
542         return ret;
543 }
544
545 void drm_bo_finish_unmap(drm_buffer_object_t *bo)
546 {
547         vma_entry_t *v_entry;
548
549         list_for_each_entry(v_entry, &bo->vma_list, head) {
550                 v_entry->vma->vm_flags &= ~VM_PFNMAP; 
551         }
552 }       
553
554 #endif
555