0d4e3fccaa8aa59ee317da6e26dc2f7053604a45
[linux-2.6-microblaze.git] / include / drm / ttm / ttm_bo_api.h
1 /**************************************************************************
2  *
3  * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., 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 Hellstrom <thellstrom-at-vmware-dot-com>
29  */
30
31 #ifndef _TTM_BO_API_H_
32 #define _TTM_BO_API_H_
33
34 #include <drm/drm_gem.h>
35 #include <drm/drm_hashtab.h>
36 #include <drm/drm_vma_manager.h>
37 #include <linux/kref.h>
38 #include <linux/list.h>
39 #include <linux/wait.h>
40 #include <linux/mutex.h>
41 #include <linux/mm.h>
42 #include <linux/bitmap.h>
43 #include <linux/dma-resv.h>
44
45 #include "ttm_resource.h"
46
47 struct ttm_bo_global;
48
49 struct ttm_bo_device;
50
51 struct dma_buf_map;
52
53 struct drm_mm_node;
54
55 struct ttm_placement;
56
57 struct ttm_place;
58
59 struct ttm_lru_bulk_move;
60
61 /**
62  * enum ttm_bo_type
63  *
64  * @ttm_bo_type_device: These are 'normal' buffers that can
65  * be mmapped by user space. Each of these bos occupy a slot in the
66  * device address space, that can be used for normal vm operations.
67  *
68  * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
69  * but they cannot be accessed from user-space. For kernel-only use.
70  *
71  * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
72  * driver.
73  */
74
75 enum ttm_bo_type {
76         ttm_bo_type_device,
77         ttm_bo_type_kernel,
78         ttm_bo_type_sg
79 };
80
81 struct ttm_tt;
82
83 /**
84  * struct ttm_buffer_object
85  *
86  * @base: drm_gem_object superclass data.
87  * @bdev: Pointer to the buffer object device structure.
88  * @type: The bo type.
89  * @destroy: Destruction function. If NULL, kfree is used.
90  * @num_pages: Actual number of pages.
91  * @acc_size: Accounted size for this object.
92  * @kref: Reference count of this buffer object. When this refcount reaches
93  * zero, the object is destroyed or put on the delayed delete list.
94  * @mem: structure describing current placement.
95  * @ttm: TTM structure holding system pages.
96  * @evicted: Whether the object was evicted without user-space knowing.
97  * @deleted: True if the object is only a zombie and already deleted.
98  * @lru: List head for the lru list.
99  * @ddestroy: List head for the delayed destroy list.
100  * @swap: List head for swap LRU list.
101  * @moving: Fence set when BO is moving
102  * @offset: The current GPU offset, which can have different meanings
103  * depending on the memory type. For SYSTEM type memory, it should be 0.
104  * @cur_placement: Hint of current placement.
105  *
106  * Base class for TTM buffer object, that deals with data placement and CPU
107  * mappings. GPU mappings are really up to the driver, but for simpler GPUs
108  * the driver can usually use the placement offset @offset directly as the
109  * GPU virtual address. For drivers implementing multiple
110  * GPU memory manager contexts, the driver should manage the address space
111  * in these contexts separately and use these objects to get the correct
112  * placement and caching for these GPU maps. This makes it possible to use
113  * these objects for even quite elaborate memory management schemes.
114  * The destroy member, the API visibility of this object makes it possible
115  * to derive driver specific types.
116  */
117
118 struct ttm_buffer_object {
119         struct drm_gem_object base;
120
121         /**
122          * Members constant at init.
123          */
124
125         struct ttm_bo_device *bdev;
126         enum ttm_bo_type type;
127         void (*destroy) (struct ttm_buffer_object *);
128         size_t acc_size;
129
130         /**
131         * Members not needing protection.
132         */
133         struct kref kref;
134
135         /**
136          * Members protected by the bo::resv::reserved lock.
137          */
138
139         struct ttm_resource mem;
140         struct ttm_tt *ttm;
141         bool deleted;
142
143         /**
144          * Members protected by the bdev::lru_lock.
145          */
146
147         struct list_head lru;
148         struct list_head ddestroy;
149         struct list_head swap;
150
151         /**
152          * Members protected by a bo reservation.
153          */
154
155         struct dma_fence *moving;
156         unsigned priority;
157         unsigned pin_count;
158
159         /**
160          * Special members that are protected by the reserve lock
161          * and the bo::lock when written to. Can be read with
162          * either of these locks held.
163          */
164
165         struct sg_table *sg;
166 };
167
168 /**
169  * struct ttm_bo_kmap_obj
170  *
171  * @virtual: The current kernel virtual address.
172  * @page: The page when kmap'ing a single page.
173  * @bo_kmap_type: Type of bo_kmap.
174  *
175  * Object describing a kernel mapping. Since a TTM bo may be located
176  * in various memory types with various caching policies, the
177  * mapping can either be an ioremap, a vmap, a kmap or part of a
178  * premapped region.
179  */
180
181 #define TTM_BO_MAP_IOMEM_MASK 0x80
182 struct ttm_bo_kmap_obj {
183         void *virtual;
184         struct page *page;
185         enum {
186                 ttm_bo_map_iomap        = 1 | TTM_BO_MAP_IOMEM_MASK,
187                 ttm_bo_map_vmap         = 2,
188                 ttm_bo_map_kmap         = 3,
189                 ttm_bo_map_premapped    = 4 | TTM_BO_MAP_IOMEM_MASK,
190         } bo_kmap_type;
191         struct ttm_buffer_object *bo;
192 };
193
194 /**
195  * struct ttm_operation_ctx
196  *
197  * @interruptible: Sleep interruptible if sleeping.
198  * @no_wait_gpu: Return immediately if the GPU is busy.
199  * @gfp_retry_mayfail: Set the __GFP_RETRY_MAYFAIL when allocation pages.
200  * @allow_res_evict: Allow eviction of reserved BOs. Can be used when multiple
201  * BOs share the same reservation object.
202  * @force_alloc: Don't check the memory account during suspend or CPU page
203  * faults. Should only be used by TTM internally.
204  * @resv: Reservation object to allow reserved evictions with.
205  *
206  * Context for TTM operations like changing buffer placement or general memory
207  * allocation.
208  */
209 struct ttm_operation_ctx {
210         bool interruptible;
211         bool no_wait_gpu;
212         bool gfp_retry_mayfail;
213         bool allow_res_evict;
214         bool force_alloc;
215         struct dma_resv *resv;
216         uint64_t bytes_moved;
217 };
218
219 /**
220  * ttm_bo_get - reference a struct ttm_buffer_object
221  *
222  * @bo: The buffer object.
223  */
224 static inline void ttm_bo_get(struct ttm_buffer_object *bo)
225 {
226         kref_get(&bo->kref);
227 }
228
229 /**
230  * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless
231  * its refcount has already reached zero.
232  * @bo: The buffer object.
233  *
234  * Used to reference a TTM buffer object in lookups where the object is removed
235  * from the lookup structure during the destructor and for RCU lookups.
236  *
237  * Returns: @bo if the referencing was successful, NULL otherwise.
238  */
239 static inline __must_check struct ttm_buffer_object *
240 ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
241 {
242         if (!kref_get_unless_zero(&bo->kref))
243                 return NULL;
244         return bo;
245 }
246
247 /**
248  * ttm_bo_wait - wait for buffer idle.
249  *
250  * @bo:  The buffer object.
251  * @interruptible:  Use interruptible wait.
252  * @no_wait:  Return immediately if buffer is busy.
253  *
254  * This function must be called with the bo::mutex held, and makes
255  * sure any previous rendering to the buffer is completed.
256  * Note: It might be necessary to block validations before the
257  * wait by reserving the buffer.
258  * Returns -EBUSY if no_wait is true and the buffer is busy.
259  * Returns -ERESTARTSYS if interrupted by a signal.
260  */
261 int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait);
262
263 static inline int ttm_bo_wait_ctx(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx)
264 {
265         return ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu);
266 }
267
268 /**
269  * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
270  *
271  * @placement:  Return immediately if buffer is busy.
272  * @mem:  The struct ttm_resource indicating the region where the bo resides
273  * @new_flags: Describes compatible placement found
274  *
275  * Returns true if the placement is compatible
276  */
277 bool ttm_bo_mem_compat(struct ttm_placement *placement, struct ttm_resource *mem,
278                        uint32_t *new_flags);
279
280 /**
281  * ttm_bo_validate
282  *
283  * @bo: The buffer object.
284  * @placement: Proposed placement for the buffer object.
285  * @ctx: validation parameters.
286  *
287  * Changes placement and caching policy of the buffer object
288  * according proposed placement.
289  * Returns
290  * -EINVAL on invalid proposed placement.
291  * -ENOMEM on out-of-memory condition.
292  * -EBUSY if no_wait is true and buffer busy.
293  * -ERESTARTSYS if interrupted by a signal.
294  */
295 int ttm_bo_validate(struct ttm_buffer_object *bo,
296                     struct ttm_placement *placement,
297                     struct ttm_operation_ctx *ctx);
298
299 /**
300  * ttm_bo_put
301  *
302  * @bo: The buffer object.
303  *
304  * Unreference a buffer object.
305  */
306 void ttm_bo_put(struct ttm_buffer_object *bo);
307
308 /**
309  * ttm_bo_move_to_lru_tail
310  *
311  * @bo: The buffer object.
312  * @bulk: optional bulk move structure to remember BO positions
313  *
314  * Move this BO to the tail of all lru lists used to lookup and reserve an
315  * object. This function must be called with struct ttm_bo_global::lru_lock
316  * held, and is used to make a BO less likely to be considered for eviction.
317  */
318 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
319                              struct ttm_lru_bulk_move *bulk);
320
321 /**
322  * ttm_bo_bulk_move_lru_tail
323  *
324  * @bulk: bulk move structure
325  *
326  * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
327  * BO order never changes. Should be called with ttm_bo_global::lru_lock held.
328  */
329 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
330
331 /**
332  * ttm_bo_lock_delayed_workqueue
333  *
334  * Prevent the delayed workqueue from running.
335  * Returns
336  * True if the workqueue was queued at the time
337  */
338 int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
339
340 /**
341  * ttm_bo_unlock_delayed_workqueue
342  *
343  * Allows the delayed workqueue to run.
344  */
345 void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched);
346
347 /**
348  * ttm_bo_eviction_valuable
349  *
350  * @bo: The buffer object to evict
351  * @place: the placement we need to make room for
352  *
353  * Check if it is valuable to evict the BO to make room for the given placement.
354  */
355 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
356                               const struct ttm_place *place);
357
358 size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
359                            unsigned long bo_size,
360                            unsigned struct_size);
361
362 /**
363  * ttm_bo_init_reserved
364  *
365  * @bdev: Pointer to a ttm_bo_device struct.
366  * @bo: Pointer to a ttm_buffer_object to be initialized.
367  * @size: Requested size of buffer object.
368  * @type: Requested type of buffer object.
369  * @flags: Initial placement flags.
370  * @page_alignment: Data alignment in pages.
371  * @ctx: TTM operation context for memory allocation.
372  * @acc_size: Accounted size for this object.
373  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
374  * @destroy: Destroy function. Use NULL for kfree().
375  *
376  * This function initializes a pre-allocated struct ttm_buffer_object.
377  * As this object may be part of a larger structure, this function,
378  * together with the @destroy function,
379  * enables driver-specific objects derived from a ttm_buffer_object.
380  *
381  * On successful return, the caller owns an object kref to @bo. The kref and
382  * list_kref are usually set to 1, but note that in some situations, other
383  * tasks may already be holding references to @bo as well.
384  * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
385  * and it is the caller's responsibility to call ttm_bo_unreserve.
386  *
387  * If a failure occurs, the function will call the @destroy function, or
388  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
389  * illegal and will likely cause memory corruption.
390  *
391  * Returns
392  * -ENOMEM: Out of memory.
393  * -EINVAL: Invalid placement flags.
394  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
395  */
396
397 int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
398                          struct ttm_buffer_object *bo,
399                          size_t size, enum ttm_bo_type type,
400                          struct ttm_placement *placement,
401                          uint32_t page_alignment,
402                          struct ttm_operation_ctx *ctx,
403                          size_t acc_size, struct sg_table *sg,
404                          struct dma_resv *resv,
405                          void (*destroy) (struct ttm_buffer_object *));
406
407 /**
408  * ttm_bo_init
409  *
410  * @bdev: Pointer to a ttm_bo_device struct.
411  * @bo: Pointer to a ttm_buffer_object to be initialized.
412  * @size: Requested size of buffer object.
413  * @type: Requested type of buffer object.
414  * @flags: Initial placement flags.
415  * @page_alignment: Data alignment in pages.
416  * @interruptible: If needing to sleep to wait for GPU resources,
417  * sleep interruptible.
418  * pinned in physical memory. If this behaviour is not desired, this member
419  * holds a pointer to a persistent shmem object. Typically, this would
420  * point to the shmem object backing a GEM object if TTM is used to back a
421  * GEM user interface.
422  * @acc_size: Accounted size for this object.
423  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
424  * @destroy: Destroy function. Use NULL for kfree().
425  *
426  * This function initializes a pre-allocated struct ttm_buffer_object.
427  * As this object may be part of a larger structure, this function,
428  * together with the @destroy function,
429  * enables driver-specific objects derived from a ttm_buffer_object.
430  *
431  * On successful return, the caller owns an object kref to @bo. The kref and
432  * list_kref are usually set to 1, but note that in some situations, other
433  * tasks may already be holding references to @bo as well.
434  *
435  * If a failure occurs, the function will call the @destroy function, or
436  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
437  * illegal and will likely cause memory corruption.
438  *
439  * Returns
440  * -ENOMEM: Out of memory.
441  * -EINVAL: Invalid placement flags.
442  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
443  */
444 int ttm_bo_init(struct ttm_bo_device *bdev, struct ttm_buffer_object *bo,
445                 size_t size, enum ttm_bo_type type,
446                 struct ttm_placement *placement,
447                 uint32_t page_alignment, bool interrubtible, size_t acc_size,
448                 struct sg_table *sg, struct dma_resv *resv,
449                 void (*destroy) (struct ttm_buffer_object *));
450
451 /**
452  * ttm_kmap_obj_virtual
453  *
454  * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
455  * @is_iomem: Pointer to an integer that on return indicates 1 if the
456  * virtual map is io memory, 0 if normal memory.
457  *
458  * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
459  * If *is_iomem is 1 on return, the virtual address points to an io memory area,
460  * that should strictly be accessed by the iowriteXX() and similar functions.
461  */
462 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
463                                          bool *is_iomem)
464 {
465         *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
466         return map->virtual;
467 }
468
469 /**
470  * ttm_bo_kmap
471  *
472  * @bo: The buffer object.
473  * @start_page: The first page to map.
474  * @num_pages: Number of pages to map.
475  * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
476  *
477  * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
478  * data in the buffer object. The ttm_kmap_obj_virtual function can then be
479  * used to obtain a virtual address to the data.
480  *
481  * Returns
482  * -ENOMEM: Out of memory.
483  * -EINVAL: Invalid range.
484  */
485 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
486                 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
487
488 /**
489  * ttm_bo_kunmap
490  *
491  * @map: Object describing the map to unmap.
492  *
493  * Unmaps a kernel map set up by ttm_bo_kmap.
494  */
495 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
496
497 /**
498  * ttm_bo_vmap
499  *
500  * @bo: The buffer object.
501  * @map: pointer to a struct dma_buf_map representing the map.
502  *
503  * Sets up a kernel virtual mapping, using ioremap or vmap to the
504  * data in the buffer object. The parameter @map returns the virtual
505  * address as struct dma_buf_map. Unmap the buffer with ttm_bo_vunmap().
506  *
507  * Returns
508  * -ENOMEM: Out of memory.
509  * -EINVAL: Invalid range.
510  */
511 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
512
513 /**
514  * ttm_bo_vunmap
515  *
516  * @bo: The buffer object.
517  * @map: Object describing the map to unmap.
518  *
519  * Unmaps a kernel map set up by ttm_bo_vmap().
520  */
521 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
522
523 /**
524  * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
525  *
526  * @vma:       vma as input from the fbdev mmap method.
527  * @bo:        The bo backing the address space.
528  *
529  * Maps a buffer object.
530  */
531 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
532
533 /**
534  * ttm_bo_mmap - mmap out of the ttm device address space.
535  *
536  * @filp:      filp as input from the mmap method.
537  * @vma:       vma as input from the mmap method.
538  * @bdev:      Pointer to the ttm_bo_device with the address space manager.
539  *
540  * This function is intended to be called by the device mmap method.
541  * if the device address space is to be backed by the bo manager.
542  */
543 int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
544                 struct ttm_bo_device *bdev);
545
546 /**
547  * ttm_bo_io
548  *
549  * @bdev:      Pointer to the struct ttm_bo_device.
550  * @filp:      Pointer to the struct file attempting to read / write.
551  * @wbuf:      User-space pointer to address of buffer to write. NULL on read.
552  * @rbuf:      User-space pointer to address of buffer to read into.
553  * Null on write.
554  * @count:     Number of bytes to read / write.
555  * @f_pos:     Pointer to current file position.
556  * @write:     1 for read, 0 for write.
557  *
558  * This function implements read / write into ttm buffer objects, and is
559  * intended to
560  * be called from the fops::read and fops::write method.
561  * Returns:
562  * See man (2) write, man(2) read. In particular,
563  * the function may return -ERESTARTSYS if
564  * interrupted by a signal.
565  */
566 ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
567                   const char __user *wbuf, char __user *rbuf,
568                   size_t count, loff_t *f_pos, bool write);
569
570 int ttm_bo_swapout(struct ttm_operation_ctx *ctx);
571
572 /**
573  * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
574  * embedded drm_gem_object.
575  *
576  * Most ttm drivers are using gem too, so the embedded
577  * ttm_buffer_object.base will be initialized by the driver (before
578  * calling ttm_bo_init).  It is also possible to use ttm without gem
579  * though (vmwgfx does that).
580  *
581  * This helper will figure whenever a given ttm bo is a gem object too
582  * or not.
583  *
584  * @bo: The bo to check.
585  */
586 static inline bool ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object *bo)
587 {
588         return bo->base.dev != NULL;
589 }
590
591 /**
592  * ttm_bo_pin - Pin the buffer object.
593  * @bo: The buffer object to pin
594  *
595  * Make sure the buffer is not evicted any more during memory pressure.
596  */
597 static inline void ttm_bo_pin(struct ttm_buffer_object *bo)
598 {
599         dma_resv_assert_held(bo->base.resv);
600         WARN_ON_ONCE(!kref_read(&bo->kref));
601         ++bo->pin_count;
602 }
603
604 /**
605  * ttm_bo_unpin - Unpin the buffer object.
606  * @bo: The buffer object to unpin
607  *
608  * Allows the buffer object to be evicted again during memory pressure.
609  */
610 static inline void ttm_bo_unpin(struct ttm_buffer_object *bo)
611 {
612         dma_resv_assert_held(bo->base.resv);
613         WARN_ON_ONCE(!bo->pin_count);
614         WARN_ON_ONCE(!kref_read(&bo->kref));
615         --bo->pin_count;
616 }
617
618 int ttm_mem_evict_first(struct ttm_bo_device *bdev,
619                         struct ttm_resource_manager *man,
620                         const struct ttm_place *place,
621                         struct ttm_operation_ctx *ctx,
622                         struct ww_acquire_ctx *ticket);
623
624 /* Default number of pre-faulted pages in the TTM fault handler */
625 #define TTM_BO_VM_NUM_PREFAULT 16
626
627 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
628                              struct vm_fault *vmf);
629
630 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
631                                     pgprot_t prot,
632                                     pgoff_t num_prefault,
633                                     pgoff_t fault_page_size);
634
635 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
636
637 void ttm_bo_vm_open(struct vm_area_struct *vma);
638
639 void ttm_bo_vm_close(struct vm_area_struct *vma);
640
641 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
642                      void *buf, int len, int write);
643
644 #endif