OSDN Git Service

Pass lock data like linux and open.
[android-x86/external-libdrm.git] / linux-core / drm_bo_move.c
1 /**************************************************************************
2  *
3  * Copyright (c) 2007 Tungsten Graphics, Inc., Cedar Park, TX., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 /*
28  * Authors: Thomas Hellström <thomas-at-tungstengraphics-dot-com>
29  */
30
31 #include "drmP.h"
32
33 /**
34  * Free the old memory node unless it's a pinned region and we
35  * have not been requested to free also pinned regions.
36  */
37
38 static void drm_bo_free_old_node(struct drm_buffer_object *bo)
39 {
40         struct drm_bo_mem_reg *old_mem = &bo->mem;
41
42         if (old_mem->mm_node && (old_mem->mm_node != bo->pinned_node)) {
43                 mutex_lock(&bo->dev->struct_mutex);
44                 drm_mm_put_block(old_mem->mm_node);
45                 mutex_unlock(&bo->dev->struct_mutex);
46         }
47         old_mem->mm_node = NULL;
48 }
49
50 int drm_bo_move_ttm(struct drm_buffer_object *bo,
51                     int evict, int no_wait, struct drm_bo_mem_reg *new_mem)
52 {
53         struct drm_ttm *ttm = bo->ttm;
54         struct drm_bo_mem_reg *old_mem = &bo->mem;
55         uint64_t save_flags = old_mem->flags;
56         uint64_t save_proposed_flags = old_mem->proposed_flags;
57         int ret;
58
59         if (old_mem->mem_type != DRM_BO_MEM_LOCAL) {
60                 if (evict)
61                         drm_ttm_evict(ttm);
62                 else
63                         drm_ttm_unbind(ttm);
64
65                 drm_bo_free_old_node(bo);
66                 DRM_FLAG_MASKED(old_mem->flags,
67                                 DRM_BO_FLAG_CACHED | DRM_BO_FLAG_MAPPABLE |
68                                 DRM_BO_FLAG_MEM_LOCAL, DRM_BO_MASK_MEMTYPE);
69                 old_mem->mem_type = DRM_BO_MEM_LOCAL;
70                 save_flags = old_mem->flags;
71         }
72         if (new_mem->mem_type != DRM_BO_MEM_LOCAL) {
73                 ret = drm_ttm_bind(ttm, new_mem);
74                 if (ret)
75                         return ret;
76         }
77
78         *old_mem = *new_mem;
79         new_mem->mm_node = NULL;
80         old_mem->proposed_flags = save_proposed_flags;
81         DRM_FLAG_MASKED(save_flags, new_mem->flags, DRM_BO_MASK_MEMTYPE);
82         return 0;
83 }
84 EXPORT_SYMBOL(drm_bo_move_ttm);
85
86 /**
87  * \c Return a kernel virtual address to the buffer object PCI memory.
88  *
89  * \param bo The buffer object.
90  * \return Failure indication.
91  *
92  * Returns -EINVAL if the buffer object is currently not mappable.
93  * Returns -ENOMEM if the ioremap operation failed.
94  * Otherwise returns zero.
95  *
96  * After a successfull call, bo->iomap contains the virtual address, or NULL
97  * if the buffer object content is not accessible through PCI space.
98  * Call bo->mutex locked.
99  */
100
101 int drm_mem_reg_ioremap(struct drm_device *dev, struct drm_bo_mem_reg *mem,
102                         void **virtual)
103 {
104         struct drm_buffer_manager *bm = &dev->bm;
105         struct drm_mem_type_manager *man = &bm->man[mem->mem_type];
106         unsigned long bus_offset;
107         unsigned long bus_size;
108         unsigned long bus_base;
109         int ret;
110         void *addr;
111
112         *virtual = NULL;
113         ret = drm_bo_pci_offset(dev, mem, &bus_base, &bus_offset, &bus_size);
114         if (ret || bus_size == 0)
115                 return ret;
116
117         if (!(man->flags & _DRM_FLAG_NEEDS_IOREMAP))
118                 addr = (void *)(((u8 *) man->io_addr) + bus_offset);
119         else {
120                 addr = ioremap_nocache(bus_base + bus_offset, bus_size);
121                 if (!addr)
122                         return -ENOMEM;
123         }
124         *virtual = addr;
125         return 0;
126 }
127 EXPORT_SYMBOL(drm_mem_reg_ioremap);
128
129 /**
130  * \c Unmap mapping obtained using drm_bo_ioremap
131  *
132  * \param bo The buffer object.
133  *
134  * Call bo->mutex locked.
135  */
136
137 void drm_mem_reg_iounmap(struct drm_device *dev, struct drm_bo_mem_reg *mem,
138                          void *virtual)
139 {
140         struct drm_buffer_manager *bm;
141         struct drm_mem_type_manager *man;
142
143         bm = &dev->bm;
144         man = &bm->man[mem->mem_type];
145
146         if (virtual && (man->flags & _DRM_FLAG_NEEDS_IOREMAP))
147                 iounmap(virtual);
148 }
149
150 static int drm_copy_io_page(void *dst, void *src, unsigned long page)
151 {
152         uint32_t *dstP =
153             (uint32_t *) ((unsigned long)dst + (page << PAGE_SHIFT));
154         uint32_t *srcP =
155             (uint32_t *) ((unsigned long)src + (page << PAGE_SHIFT));
156
157         int i;
158         for (i = 0; i < PAGE_SIZE / sizeof(uint32_t); ++i)
159                 iowrite32(ioread32(srcP++), dstP++);
160         return 0;
161 }
162
163 static int drm_copy_io_ttm_page(struct drm_ttm *ttm, void *src,
164                                 unsigned long page)
165 {
166         struct page *d = drm_ttm_get_page(ttm, page);
167         void *dst;
168
169         if (!d)
170                 return -ENOMEM;
171
172         src = (void *)((unsigned long)src + (page << PAGE_SHIFT));
173         dst = kmap(d);
174         if (!dst)
175                 return -ENOMEM;
176
177         memcpy_fromio(dst, src, PAGE_SIZE);
178         kunmap(d);
179         return 0;
180 }
181
182 static int drm_copy_ttm_io_page(struct drm_ttm *ttm, void *dst, unsigned long page)
183 {
184         struct page *s = drm_ttm_get_page(ttm, page);
185         void *src;
186
187         if (!s)
188                 return -ENOMEM;
189
190         dst = (void *)((unsigned long)dst + (page << PAGE_SHIFT));
191         src = kmap(s);
192         if (!src)
193                 return -ENOMEM;
194
195         memcpy_toio(dst, src, PAGE_SIZE);
196         kunmap(s);
197         return 0;
198 }
199
200 int drm_bo_move_memcpy(struct drm_buffer_object *bo,
201                        int evict, int no_wait, struct drm_bo_mem_reg *new_mem)
202 {
203         struct drm_device *dev = bo->dev;
204         struct drm_mem_type_manager *man = &dev->bm.man[new_mem->mem_type];
205         struct drm_ttm *ttm = bo->ttm;
206         struct drm_bo_mem_reg *old_mem = &bo->mem;
207         struct drm_bo_mem_reg old_copy = *old_mem;
208         void *old_iomap;
209         void *new_iomap;
210         int ret;
211         uint64_t save_flags = old_mem->flags;
212         uint64_t save_proposed_flags = old_mem->proposed_flags;
213         unsigned long i;
214         unsigned long page;
215         unsigned long add = 0;
216         int dir;
217
218         ret = drm_mem_reg_ioremap(dev, old_mem, &old_iomap);
219         if (ret)
220                 return ret;
221         ret = drm_mem_reg_ioremap(dev, new_mem, &new_iomap);
222         if (ret)
223                 goto out;
224
225         if (old_iomap == NULL && new_iomap == NULL)
226                 goto out2;
227         if (old_iomap == NULL && ttm == NULL)
228                 goto out2;
229
230         add = 0;
231         dir = 1;
232
233         if ((old_mem->mem_type == new_mem->mem_type) &&
234             (new_mem->mm_node->start <
235              old_mem->mm_node->start + old_mem->mm_node->size)) {
236                 dir = -1;
237                 add = new_mem->num_pages - 1;
238         }
239
240         for (i = 0; i < new_mem->num_pages; ++i) {
241                 page = i * dir + add;
242                 if (old_iomap == NULL)
243                         ret = drm_copy_ttm_io_page(ttm, new_iomap, page);
244                 else if (new_iomap == NULL)
245                         ret = drm_copy_io_ttm_page(ttm, old_iomap, page);
246                 else
247                         ret = drm_copy_io_page(new_iomap, old_iomap, page);
248                 if (ret)
249                         goto out1;
250         }
251         mb();
252 out2:
253         drm_bo_free_old_node(bo);
254
255         *old_mem = *new_mem;
256         new_mem->mm_node = NULL;
257         old_mem->proposed_flags = save_proposed_flags;
258         DRM_FLAG_MASKED(save_flags, new_mem->flags, DRM_BO_MASK_MEMTYPE);
259
260         if ((man->flags & _DRM_FLAG_MEMTYPE_FIXED) && (ttm != NULL)) {
261                 drm_ttm_unbind(ttm);
262                 drm_ttm_destroy(ttm);
263                 bo->ttm = NULL;
264         }
265
266 out1:
267         drm_mem_reg_iounmap(dev, new_mem, new_iomap);
268 out:
269         drm_mem_reg_iounmap(dev, &old_copy, old_iomap);
270         return ret;
271 }
272 EXPORT_SYMBOL(drm_bo_move_memcpy);
273
274 /*
275  * Transfer a buffer object's memory and LRU status to a newly
276  * created object. User-space references remains with the old
277  * object. Call bo->mutex locked.
278  */
279
280 int drm_buffer_object_transfer(struct drm_buffer_object *bo,
281                                struct drm_buffer_object **new_obj)
282 {
283         struct drm_buffer_object *fbo;
284         struct drm_device *dev = bo->dev;
285         struct drm_buffer_manager *bm = &dev->bm;
286
287         fbo = drm_ctl_calloc(1, sizeof(*fbo), DRM_MEM_BUFOBJ);
288         if (!fbo)
289                 return -ENOMEM;
290
291         *fbo = *bo;
292         mutex_init(&fbo->mutex);
293         mutex_lock(&fbo->mutex);
294         mutex_lock(&dev->struct_mutex);
295
296         DRM_INIT_WAITQUEUE(&bo->event_queue);
297         INIT_LIST_HEAD(&fbo->ddestroy);
298         INIT_LIST_HEAD(&fbo->lru);
299         INIT_LIST_HEAD(&fbo->pinned_lru);
300 #ifdef DRM_ODD_MM_COMPAT
301         INIT_LIST_HEAD(&fbo->vma_list);
302         INIT_LIST_HEAD(&fbo->p_mm_list);
303 #endif
304
305         fbo->fence = drm_fence_reference_locked(bo->fence);
306         fbo->pinned_node = NULL;
307         fbo->mem.mm_node->private = (void *)fbo;
308         atomic_set(&fbo->usage, 1);
309         atomic_inc(&bm->count);
310         mutex_unlock(&dev->struct_mutex);
311         mutex_unlock(&fbo->mutex);
312
313         *new_obj = fbo;
314         return 0;
315 }
316
317 /*
318  * Since move is underway, we need to block signals in this function.
319  * We cannot restart until it has finished.
320  */
321
322 int drm_bo_move_accel_cleanup(struct drm_buffer_object *bo,
323                               int evict, int no_wait, uint32_t fence_class,
324                               uint32_t fence_type, uint32_t fence_flags,
325                               struct drm_bo_mem_reg *new_mem)
326 {
327         struct drm_device *dev = bo->dev;
328         struct drm_mem_type_manager *man = &dev->bm.man[new_mem->mem_type];
329         struct drm_bo_mem_reg *old_mem = &bo->mem;
330         int ret;
331         uint64_t save_flags = old_mem->flags;
332         uint64_t save_proposed_flags = old_mem->proposed_flags;
333         struct drm_buffer_object *old_obj;
334
335         if (bo->fence)
336                 drm_fence_usage_deref_unlocked(&bo->fence);
337         ret = drm_fence_object_create(dev, fence_class, fence_type,
338                                       fence_flags | DRM_FENCE_FLAG_EMIT,
339                                       &bo->fence);
340         bo->fence_type = fence_type;
341         if (ret)
342                 return ret;
343
344 #ifdef DRM_ODD_MM_COMPAT
345         /*
346          * In this mode, we don't allow pipelining a copy blit,
347          * since the buffer will be accessible from user space
348          * the moment we return and rebuild the page tables.
349          *
350          * With normal vm operation, page tables are rebuilt
351          * on demand using fault(), which waits for buffer idle.
352          */
353         if (1)
354 #else
355         if (evict || ((bo->mem.mm_node == bo->pinned_node) &&
356                       bo->mem.mm_node != NULL))
357 #endif
358         {
359                 if (bo->fence) {
360                         (void) drm_fence_object_wait(bo->fence, 0, 1,
361                                                     bo->fence_type);
362                         drm_fence_usage_deref_unlocked(&bo->fence);
363                 }
364                 drm_bo_free_old_node(bo);
365
366                 if ((man->flags & _DRM_FLAG_MEMTYPE_FIXED) && (bo->ttm != NULL)) {
367                         drm_ttm_unbind(bo->ttm);
368                         drm_ttm_destroy(bo->ttm);
369                         bo->ttm = NULL;
370                 }
371         } else {
372
373                 /* This should help pipeline ordinary buffer moves.
374                  *
375                  * Hang old buffer memory on a new buffer object,
376                  * and leave it to be released when the GPU
377                  * operation has completed.
378                  */
379
380                 ret = drm_buffer_object_transfer(bo, &old_obj);
381
382                 if (ret)
383                         return ret;
384
385                 if (!(man->flags & _DRM_FLAG_MEMTYPE_FIXED))
386                         old_obj->ttm = NULL;
387                 else
388                         bo->ttm = NULL;
389
390                 mutex_lock(&dev->struct_mutex);
391                 list_del_init(&old_obj->lru);
392                 DRM_FLAG_MASKED(bo->priv_flags, 0, _DRM_BO_FLAG_UNFENCED);
393                 drm_bo_add_to_lru(old_obj);
394
395                 drm_bo_usage_deref_locked(&old_obj);
396                 mutex_unlock(&dev->struct_mutex);
397
398         }
399
400         *old_mem = *new_mem;
401         new_mem->mm_node = NULL;
402         old_mem->proposed_flags = save_proposed_flags;
403         DRM_FLAG_MASKED(save_flags, new_mem->flags, DRM_BO_MASK_MEMTYPE);
404         return 0;
405 }
406 EXPORT_SYMBOL(drm_bo_move_accel_cleanup);
407
408 int drm_bo_same_page(unsigned long offset,
409                      unsigned long offset2)
410 {
411         return (offset & PAGE_MASK) == (offset2 & PAGE_MASK);
412 }
413 EXPORT_SYMBOL(drm_bo_same_page);
414
415 unsigned long drm_bo_offset_end(unsigned long offset,
416                                 unsigned long end)
417 {
418         offset = (offset + PAGE_SIZE) & PAGE_MASK;
419         return (end < offset) ? end : offset;
420 }
421 EXPORT_SYMBOL(drm_bo_offset_end);
422
423 static pgprot_t drm_kernel_io_prot(uint32_t map_type)
424 {
425         pgprot_t tmp = PAGE_KERNEL;
426
427 #if defined(__i386__) || defined(__x86_64__)
428 #ifdef USE_PAT_WC
429 #warning using pat
430         if (drm_use_pat() && map_type == _DRM_TTM) {
431                 pgprot_val(tmp) |= _PAGE_PAT;
432                 return tmp;
433         }
434 #endif
435         if (boot_cpu_data.x86 > 3 && map_type != _DRM_AGP) {
436                 pgprot_val(tmp) |= _PAGE_PCD;
437                 pgprot_val(tmp) &= ~_PAGE_PWT;
438         }
439 #elif defined(__powerpc__)
440         pgprot_val(tmp) |= _PAGE_NO_CACHE;
441         if (map_type == _DRM_REGISTERS)
442                 pgprot_val(tmp) |= _PAGE_GUARDED;
443 #endif
444 #if defined(__ia64__)
445         if (map_type == _DRM_TTM)
446                 tmp = pgprot_writecombine(tmp);
447         else
448                 tmp = pgprot_noncached(tmp);
449 #endif
450         return tmp;
451 }
452
453 static int drm_bo_ioremap(struct drm_buffer_object *bo, unsigned long bus_base,
454                           unsigned long bus_offset, unsigned long bus_size,
455                           struct drm_bo_kmap_obj *map)
456 {
457         struct drm_device *dev = bo->dev;
458         struct drm_bo_mem_reg *mem = &bo->mem;
459         struct drm_mem_type_manager *man = &dev->bm.man[mem->mem_type];
460
461         if (!(man->flags & _DRM_FLAG_NEEDS_IOREMAP)) {
462                 map->bo_kmap_type = bo_map_premapped;
463                 map->virtual = (void *)(((u8 *) man->io_addr) + bus_offset);
464         } else {
465                 map->bo_kmap_type = bo_map_iomap;
466                 map->virtual = ioremap_nocache(bus_base + bus_offset, bus_size);
467         }
468         return (!map->virtual) ? -ENOMEM : 0;
469 }
470
471 static int drm_bo_kmap_ttm(struct drm_buffer_object *bo,
472                            unsigned long start_page, unsigned long num_pages,
473                            struct drm_bo_kmap_obj *map)
474 {
475         struct drm_device *dev = bo->dev;
476         struct drm_bo_mem_reg *mem = &bo->mem;
477         struct drm_mem_type_manager *man = &dev->bm.man[mem->mem_type];
478         pgprot_t prot;
479         struct drm_ttm *ttm = bo->ttm;
480         struct page *d;
481         int i;
482
483         BUG_ON(!ttm);
484
485         if (num_pages == 1 && (mem->flags & DRM_BO_FLAG_CACHED)) {
486
487                 /*
488                  * We're mapping a single page, and the desired
489                  * page protection is consistent with the bo.
490                  */
491
492                 map->bo_kmap_type = bo_map_kmap;
493                 map->page = drm_ttm_get_page(ttm, start_page);
494                 map->virtual = kmap(map->page);
495         } else {
496                 /*
497                  * Populate the part we're mapping;
498                  */
499
500                 for (i = start_page; i < start_page + num_pages; ++i) {
501                         d = drm_ttm_get_page(ttm, i);
502                         if (!d)
503                                 return -ENOMEM;
504                 }
505
506                 /*
507                  * We need to use vmap to get the desired page protection
508                  * or to make the buffer object look contigous.
509                  */
510
511                 prot = (mem->flags & DRM_BO_FLAG_CACHED) ?
512                         PAGE_KERNEL :
513                         drm_kernel_io_prot(man->drm_bus_maptype);
514                 map->bo_kmap_type = bo_map_vmap;
515                 map->virtual = vmap(ttm->pages + start_page,
516                                     num_pages, 0, prot);
517         }
518         return (!map->virtual) ? -ENOMEM : 0;
519 }
520
521 /*
522  * This function is to be used for kernel mapping of buffer objects.
523  * It chooses the appropriate mapping method depending on the memory type
524  * and caching policy the buffer currently has.
525  * Mapping multiple pages or buffers that live in io memory is a bit slow and
526  * consumes vmalloc space. Be restrictive with such mappings.
527  * Mapping single pages usually returns the logical kernel address,
528  * (which is fast)
529  * BUG may use slower temporary mappings for high memory pages or
530  * uncached / write-combined pages.
531  *
532  * The function fills in a drm_bo_kmap_obj which can be used to return the
533  * kernel virtual address of the buffer.
534  *
535  * Code servicing a non-priviliged user request is only allowed to map one
536  * page at a time. We might need to implement a better scheme to stop such
537  * processes from consuming all vmalloc space.
538  */
539
540 int drm_bo_kmap(struct drm_buffer_object *bo, unsigned long start_page,
541                 unsigned long num_pages, struct drm_bo_kmap_obj *map)
542 {
543         int ret;
544         unsigned long bus_base;
545         unsigned long bus_offset;
546         unsigned long bus_size;
547
548         map->virtual = NULL;
549
550         if (num_pages > bo->num_pages)
551                 return -EINVAL;
552         if (start_page > bo->num_pages)
553                 return -EINVAL;
554 #if 0
555         if (num_pages > 1 && !DRM_SUSER(DRM_CURPROC))
556                 return -EPERM;
557 #endif
558         ret = drm_bo_pci_offset(bo->dev, &bo->mem, &bus_base,
559                                 &bus_offset, &bus_size);
560
561         if (ret)
562                 return ret;
563
564         if (bus_size == 0) {
565                 return drm_bo_kmap_ttm(bo, start_page, num_pages, map);
566         } else {
567                 bus_offset += start_page << PAGE_SHIFT;
568                 bus_size = num_pages << PAGE_SHIFT;
569                 return drm_bo_ioremap(bo, bus_base, bus_offset, bus_size, map);
570         }
571 }
572 EXPORT_SYMBOL(drm_bo_kmap);
573
574 void drm_bo_kunmap(struct drm_bo_kmap_obj *map)
575 {
576         if (!map->virtual)
577                 return;
578
579         switch (map->bo_kmap_type) {
580         case bo_map_iomap:
581                 iounmap(map->virtual);
582                 break;
583         case bo_map_vmap:
584                 vunmap(map->virtual);
585                 break;
586         case bo_map_kmap:
587                 kunmap(map->page);
588                 break;
589         case bo_map_premapped:
590                 break;
591         default:
592                 BUG();
593         }
594         map->virtual = NULL;
595         map->page = NULL;
596 }
597 EXPORT_SYMBOL(drm_bo_kunmap);
598
599 int drm_bo_pfn_prot(struct drm_buffer_object *bo,
600                     unsigned long dst_offset,
601                     unsigned long *pfn,
602                     pgprot_t *prot)
603 {
604         struct drm_bo_mem_reg *mem = &bo->mem;
605         struct drm_device *dev = bo->dev;
606         unsigned long bus_offset;
607         unsigned long bus_size;
608         unsigned long bus_base;
609         struct drm_mem_type_manager *man = &dev->bm.man[mem->mem_type];
610         int ret;
611
612         ret = drm_bo_pci_offset(dev, mem, &bus_base, &bus_offset,
613                                 &bus_size);
614         if (ret)
615                 return -EINVAL;
616
617         if (bus_size != 0)
618                 *pfn = (bus_base + bus_offset + dst_offset) >> PAGE_SHIFT;
619         else if (!bo->ttm)
620                 return -EINVAL;
621         else
622                 *pfn = page_to_pfn(drm_ttm_get_page(bo->ttm, dst_offset >> PAGE_SHIFT));
623
624         *prot = (mem->flags & DRM_BO_FLAG_CACHED) ?
625                 PAGE_KERNEL : drm_kernel_io_prot(man->drm_bus_maptype);
626
627         return 0;
628 }
629 EXPORT_SYMBOL(drm_bo_pfn_prot);
630