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