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