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