]> Git Repo - linux.git/blob - include/drm/ttm/ttm_bo_api.h
mm/page_alloc: free pages in a single pass during bulk free
[linux.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_vma_manager.h>
36 #include <linux/kref.h>
37 #include <linux/list.h>
38 #include <linux/wait.h>
39 #include <linux/mutex.h>
40 #include <linux/mm.h>
41 #include <linux/bitmap.h>
42 #include <linux/dma-resv.h>
43
44 #include "ttm_resource.h"
45
46 struct ttm_global;
47
48 struct ttm_device;
49
50 struct dma_buf_map;
51
52 struct drm_mm_node;
53
54 struct ttm_placement;
55
56 struct ttm_place;
57
58 struct ttm_lru_bulk_move;
59
60 /**
61  * enum ttm_bo_type
62  *
63  * @ttm_bo_type_device: These are 'normal' buffers that can
64  * be mmapped by user space. Each of these bos occupy a slot in the
65  * device address space, that can be used for normal vm operations.
66  *
67  * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
68  * but they cannot be accessed from user-space. For kernel-only use.
69  *
70  * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
71  * driver.
72  */
73
74 enum ttm_bo_type {
75         ttm_bo_type_device,
76         ttm_bo_type_kernel,
77         ttm_bo_type_sg
78 };
79
80 struct ttm_tt;
81
82 /**
83  * struct ttm_buffer_object
84  *
85  * @base: drm_gem_object superclass data.
86  * @bdev: Pointer to the buffer object device structure.
87  * @type: The bo type.
88  * @page_alignment: Page alignment.
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         uint32_t page_alignment;
127         void (*destroy) (struct ttm_buffer_object *);
128
129         /**
130         * Members not needing protection.
131         */
132         struct kref kref;
133
134         /**
135          * Members protected by the bo::resv::reserved lock.
136          */
137
138         struct ttm_resource *resource;
139         struct ttm_tt *ttm;
140         bool deleted;
141
142         /**
143          * Members protected by the bdev::lru_lock.
144          */
145
146         struct list_head lru;
147         struct list_head ddestroy;
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_validate
268  *
269  * @bo: The buffer object.
270  * @placement: Proposed placement for the buffer object.
271  * @ctx: validation parameters.
272  *
273  * Changes placement and caching policy of the buffer object
274  * according proposed placement.
275  * Returns
276  * -EINVAL on invalid proposed placement.
277  * -ENOMEM on out-of-memory condition.
278  * -EBUSY if no_wait is true and buffer busy.
279  * -ERESTARTSYS if interrupted by a signal.
280  */
281 int ttm_bo_validate(struct ttm_buffer_object *bo,
282                     struct ttm_placement *placement,
283                     struct ttm_operation_ctx *ctx);
284
285 /**
286  * ttm_bo_put
287  *
288  * @bo: The buffer object.
289  *
290  * Unreference a buffer object.
291  */
292 void ttm_bo_put(struct ttm_buffer_object *bo);
293
294 /**
295  * ttm_bo_move_to_lru_tail
296  *
297  * @bo: The buffer object.
298  * @mem: Resource object.
299  * @bulk: optional bulk move structure to remember BO positions
300  *
301  * Move this BO to the tail of all lru lists used to lookup and reserve an
302  * object. This function must be called with struct ttm_global::lru_lock
303  * held, and is used to make a BO less likely to be considered for eviction.
304  */
305 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
306                              struct ttm_resource *mem,
307                              struct ttm_lru_bulk_move *bulk);
308
309 /**
310  * ttm_bo_bulk_move_lru_tail
311  *
312  * @bulk: bulk move structure
313  *
314  * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
315  * BO order never changes. Should be called with ttm_global::lru_lock held.
316  */
317 void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
318
319 /**
320  * ttm_bo_lock_delayed_workqueue
321  *
322  * Prevent the delayed workqueue from running.
323  * Returns
324  * True if the workqueue was queued at the time
325  */
326 int ttm_bo_lock_delayed_workqueue(struct ttm_device *bdev);
327
328 /**
329  * ttm_bo_unlock_delayed_workqueue
330  *
331  * Allows the delayed workqueue to run.
332  */
333 void ttm_bo_unlock_delayed_workqueue(struct ttm_device *bdev, int resched);
334
335 /**
336  * ttm_bo_eviction_valuable
337  *
338  * @bo: The buffer object to evict
339  * @place: the placement we need to make room for
340  *
341  * Check if it is valuable to evict the BO to make room for the given placement.
342  */
343 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
344                               const struct ttm_place *place);
345
346 /**
347  * ttm_bo_init_reserved
348  *
349  * @bdev: Pointer to a ttm_device struct.
350  * @bo: Pointer to a ttm_buffer_object to be initialized.
351  * @size: Requested size of buffer object.
352  * @type: Requested type of buffer object.
353  * @placement: Initial placement for buffer object.
354  * @page_alignment: Data alignment in pages.
355  * @ctx: TTM operation context for memory allocation.
356  * @sg: Scatter-gather table.
357  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
358  * @destroy: Destroy function. Use NULL for kfree().
359  *
360  * This function initializes a pre-allocated struct ttm_buffer_object.
361  * As this object may be part of a larger structure, this function,
362  * together with the @destroy function,
363  * enables driver-specific objects derived from a ttm_buffer_object.
364  *
365  * On successful return, the caller owns an object kref to @bo. The kref and
366  * list_kref are usually set to 1, but note that in some situations, other
367  * tasks may already be holding references to @bo as well.
368  * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
369  * and it is the caller's responsibility to call ttm_bo_unreserve.
370  *
371  * If a failure occurs, the function will call the @destroy function, or
372  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
373  * illegal and will likely cause memory corruption.
374  *
375  * Returns
376  * -ENOMEM: Out of memory.
377  * -EINVAL: Invalid placement flags.
378  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
379  */
380
381 int ttm_bo_init_reserved(struct ttm_device *bdev,
382                          struct ttm_buffer_object *bo,
383                          size_t size, enum ttm_bo_type type,
384                          struct ttm_placement *placement,
385                          uint32_t page_alignment,
386                          struct ttm_operation_ctx *ctx,
387                          struct sg_table *sg, struct dma_resv *resv,
388                          void (*destroy) (struct ttm_buffer_object *));
389
390 /**
391  * ttm_bo_init
392  *
393  * @bdev: Pointer to a ttm_device struct.
394  * @bo: Pointer to a ttm_buffer_object to be initialized.
395  * @size: Requested size of buffer object.
396  * @type: Requested type of buffer object.
397  * @placement: Initial placement for buffer object.
398  * @page_alignment: Data alignment in pages.
399  * @interruptible: If needing to sleep to wait for GPU resources,
400  * sleep interruptible.
401  * pinned in physical memory. If this behaviour is not desired, this member
402  * holds a pointer to a persistent shmem object. Typically, this would
403  * point to the shmem object backing a GEM object if TTM is used to back a
404  * GEM user interface.
405  * @sg: Scatter-gather table.
406  * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
407  * @destroy: Destroy function. Use NULL for kfree().
408  *
409  * This function initializes a pre-allocated struct ttm_buffer_object.
410  * As this object may be part of a larger structure, this function,
411  * together with the @destroy function,
412  * enables driver-specific objects derived from a ttm_buffer_object.
413  *
414  * On successful return, the caller owns an object kref to @bo. The kref and
415  * list_kref are usually set to 1, but note that in some situations, other
416  * tasks may already be holding references to @bo as well.
417  *
418  * If a failure occurs, the function will call the @destroy function, or
419  * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
420  * illegal and will likely cause memory corruption.
421  *
422  * Returns
423  * -ENOMEM: Out of memory.
424  * -EINVAL: Invalid placement flags.
425  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
426  */
427 int ttm_bo_init(struct ttm_device *bdev, struct ttm_buffer_object *bo,
428                 size_t size, enum ttm_bo_type type,
429                 struct ttm_placement *placement,
430                 uint32_t page_alignment, bool interrubtible,
431                 struct sg_table *sg, struct dma_resv *resv,
432                 void (*destroy) (struct ttm_buffer_object *));
433
434 /**
435  * ttm_kmap_obj_virtual
436  *
437  * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
438  * @is_iomem: Pointer to an integer that on return indicates 1 if the
439  * virtual map is io memory, 0 if normal memory.
440  *
441  * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
442  * If *is_iomem is 1 on return, the virtual address points to an io memory area,
443  * that should strictly be accessed by the iowriteXX() and similar functions.
444  */
445 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
446                                          bool *is_iomem)
447 {
448         *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
449         return map->virtual;
450 }
451
452 /**
453  * ttm_bo_kmap
454  *
455  * @bo: The buffer object.
456  * @start_page: The first page to map.
457  * @num_pages: Number of pages to map.
458  * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
459  *
460  * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
461  * data in the buffer object. The ttm_kmap_obj_virtual function can then be
462  * used to obtain a virtual address to the data.
463  *
464  * Returns
465  * -ENOMEM: Out of memory.
466  * -EINVAL: Invalid range.
467  */
468 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
469                 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
470
471 /**
472  * ttm_bo_kunmap
473  *
474  * @map: Object describing the map to unmap.
475  *
476  * Unmaps a kernel map set up by ttm_bo_kmap.
477  */
478 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
479
480 /**
481  * ttm_bo_vmap
482  *
483  * @bo: The buffer object.
484  * @map: pointer to a struct dma_buf_map representing the map.
485  *
486  * Sets up a kernel virtual mapping, using ioremap or vmap to the
487  * data in the buffer object. The parameter @map returns the virtual
488  * address as struct dma_buf_map. Unmap the buffer with ttm_bo_vunmap().
489  *
490  * Returns
491  * -ENOMEM: Out of memory.
492  * -EINVAL: Invalid range.
493  */
494 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
495
496 /**
497  * ttm_bo_vunmap
498  *
499  * @bo: The buffer object.
500  * @map: Object describing the map to unmap.
501  *
502  * Unmaps a kernel map set up by ttm_bo_vmap().
503  */
504 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct dma_buf_map *map);
505
506 /**
507  * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
508  *
509  * @vma:       vma as input from the fbdev mmap method.
510  * @bo:        The bo backing the address space.
511  *
512  * Maps a buffer object.
513  */
514 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
515
516 /**
517  * ttm_bo_io
518  *
519  * @bdev:      Pointer to the struct ttm_device.
520  * @filp:      Pointer to the struct file attempting to read / write.
521  * @wbuf:      User-space pointer to address of buffer to write. NULL on read.
522  * @rbuf:      User-space pointer to address of buffer to read into.
523  * Null on write.
524  * @count:     Number of bytes to read / write.
525  * @f_pos:     Pointer to current file position.
526  * @write:     1 for read, 0 for write.
527  *
528  * This function implements read / write into ttm buffer objects, and is
529  * intended to
530  * be called from the fops::read and fops::write method.
531  * Returns:
532  * See man (2) write, man(2) read. In particular,
533  * the function may return -ERESTARTSYS if
534  * interrupted by a signal.
535  */
536 ssize_t ttm_bo_io(struct ttm_device *bdev, struct file *filp,
537                   const char __user *wbuf, char __user *rbuf,
538                   size_t count, loff_t *f_pos, bool write);
539
540 int ttm_bo_swapout(struct ttm_buffer_object *bo, struct ttm_operation_ctx *ctx,
541                    gfp_t gfp_flags);
542
543 /**
544  * ttm_bo_pin - Pin the buffer object.
545  * @bo: The buffer object to pin
546  *
547  * Make sure the buffer is not evicted any more during memory pressure.
548  */
549 static inline void ttm_bo_pin(struct ttm_buffer_object *bo)
550 {
551         dma_resv_assert_held(bo->base.resv);
552         WARN_ON_ONCE(!kref_read(&bo->kref));
553         ++bo->pin_count;
554 }
555
556 /**
557  * ttm_bo_unpin - Unpin the buffer object.
558  * @bo: The buffer object to unpin
559  *
560  * Allows the buffer object to be evicted again during memory pressure.
561  */
562 static inline void ttm_bo_unpin(struct ttm_buffer_object *bo)
563 {
564         dma_resv_assert_held(bo->base.resv);
565         WARN_ON_ONCE(!kref_read(&bo->kref));
566         if (bo->pin_count)
567                 --bo->pin_count;
568         else
569                 WARN_ON_ONCE(true);
570 }
571
572 int ttm_mem_evict_first(struct ttm_device *bdev,
573                         struct ttm_resource_manager *man,
574                         const struct ttm_place *place,
575                         struct ttm_operation_ctx *ctx,
576                         struct ww_acquire_ctx *ticket);
577
578 /* Default number of pre-faulted pages in the TTM fault handler */
579 #define TTM_BO_VM_NUM_PREFAULT 16
580
581 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
582                              struct vm_fault *vmf);
583
584 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
585                                     pgprot_t prot,
586                                     pgoff_t num_prefault);
587
588 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
589
590 void ttm_bo_vm_open(struct vm_area_struct *vma);
591
592 void ttm_bo_vm_close(struct vm_area_struct *vma);
593
594 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
595                      void *buf, int len, int write);
596 bool ttm_bo_delayed_delete(struct ttm_device *bdev, bool remove_all);
597
598 vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
599
600 #endif
This page took 0.062712 seconds and 4 git commands to generate.