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 
36 #include <linux/kref.h>
37 #include <linux/list.h>
38 
39 #include "ttm_device.h"
40 
41 #include <uvm/uvm_extern.h>
42 
43 /* Default number of pre-faulted pages in the TTM fault handler */
44 #define TTM_BO_VM_NUM_PREFAULT 16
45 
46 struct iosys_map;
47 
48 struct ttm_global;
49 struct ttm_device;
50 struct ttm_placement;
51 struct ttm_place;
52 struct ttm_resource;
53 struct ttm_resource_manager;
54 struct ttm_tt;
55 
56 /**
57  * enum ttm_bo_type
58  *
59  * @ttm_bo_type_device:	These are 'normal' buffers that can
60  * be mmapped by user space. Each of these bos occupy a slot in the
61  * device address space, that can be used for normal vm operations.
62  *
63  * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
64  * but they cannot be accessed from user-space. For kernel-only use.
65  *
66  * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
67  * driver.
68  */
69 enum ttm_bo_type {
70 	ttm_bo_type_device,
71 	ttm_bo_type_kernel,
72 	ttm_bo_type_sg
73 };
74 
75 /**
76  * struct ttm_buffer_object
77  *
78  * @base: drm_gem_object superclass data.
79  * @bdev: Pointer to the buffer object device structure.
80  * @type: The bo type.
81  * @page_alignment: Page alignment.
82  * @destroy: Destruction function. If NULL, kfree is used.
83  * @kref: Reference count of this buffer object. When this refcount reaches
84  * zero, the object is destroyed or put on the delayed delete list.
85  * @resource: structure describing current placement.
86  * @ttm: TTM structure holding system pages.
87  * @deleted: True if the object is only a zombie and already deleted.
88  * @bulk_move: The bulk move object.
89  * @priority: Priority for LRU, BOs with lower priority are evicted first.
90  * @pin_count: Pin count.
91  *
92  * Base class for TTM buffer object, that deals with data placement and CPU
93  * mappings. GPU mappings are really up to the driver, but for simpler GPUs
94  * the driver can usually use the placement offset @offset directly as the
95  * GPU virtual address. For drivers implementing multiple
96  * GPU memory manager contexts, the driver should manage the address space
97  * in these contexts separately and use these objects to get the correct
98  * placement and caching for these GPU maps. This makes it possible to use
99  * these objects for even quite elaborate memory management schemes.
100  * The destroy member, the API visibility of this object makes it possible
101  * to derive driver specific types.
102  */
103 struct ttm_buffer_object {
104 	struct drm_gem_object base;
105 
106 	/*
107 	 * Members constant at init.
108 	 */
109 	struct ttm_device *bdev;
110 	enum ttm_bo_type type;
111 	uint32_t page_alignment;
112 	void (*destroy) (struct ttm_buffer_object *);
113 
114 	/*
115 	* Members not needing protection.
116 	*/
117 	struct kref kref;
118 
119 	/*
120 	 * Members protected by the bo::resv::reserved lock.
121 	 */
122 	struct ttm_resource *resource;
123 	struct ttm_tt *ttm;
124 	bool deleted;
125 	struct ttm_lru_bulk_move *bulk_move;
126 	unsigned priority;
127 	unsigned pin_count;
128 
129 	/**
130 	 * @delayed_delete: Work item used when we can't delete the BO
131 	 * immediately
132 	 */
133 	struct work_struct delayed_delete;
134 
135 	/**
136 	 * @sg: external source of pages and DMA addresses, protected by the
137 	 * reservation lock.
138 	 */
139 	struct sg_table *sg;
140 };
141 
142 #define TTM_BO_MAP_IOMEM_MASK 0x80
143 
144 /**
145  * struct ttm_bo_kmap_obj
146  *
147  * @virtual: The current kernel virtual address.
148  * @page: The page when kmap'ing a single page.
149  * @bo_kmap_type: Type of bo_kmap.
150  * @bo: The TTM BO.
151  *
152  * Object describing a kernel mapping. Since a TTM bo may be located
153  * in various memory types with various caching policies, the
154  * mapping can either be an ioremap, a vmap, a kmap or part of a
155  * premapped region.
156  */
157 struct ttm_bo_kmap_obj {
158 	void *virtual;
159 	struct vm_page *page;
160 	enum {
161 		ttm_bo_map_iomap        = 1 | TTM_BO_MAP_IOMEM_MASK,
162 		ttm_bo_map_vmap         = 2,
163 		ttm_bo_map_kmap         = 3,
164 		ttm_bo_map_premapped    = 4 | TTM_BO_MAP_IOMEM_MASK,
165 	} bo_kmap_type;
166 	struct ttm_buffer_object *bo;
167 };
168 
169 /**
170  * struct ttm_operation_ctx
171  *
172  * @interruptible: Sleep interruptible if sleeping.
173  * @no_wait_gpu: Return immediately if the GPU is busy.
174  * @gfp_retry_mayfail: Set the __GFP_RETRY_MAYFAIL when allocation pages.
175  * @allow_res_evict: Allow eviction of reserved BOs. Can be used when multiple
176  * BOs share the same reservation object.
177  * @force_alloc: Don't check the memory account during suspend or CPU page
178  * faults. Should only be used by TTM internally.
179  * @resv: Reservation object to allow reserved evictions with.
180  * @bytes_moved: Statistics on how many bytes have been moved.
181  *
182  * Context for TTM operations like changing buffer placement or general memory
183  * allocation.
184  */
185 struct ttm_operation_ctx {
186 	bool interruptible;
187 	bool no_wait_gpu;
188 	bool gfp_retry_mayfail;
189 	bool allow_res_evict;
190 	bool force_alloc;
191 	struct dma_resv *resv;
192 	uint64_t bytes_moved;
193 };
194 
195 struct ttm_lru_walk;
196 
197 /** struct ttm_lru_walk_ops - Operations for a LRU walk. */
198 struct ttm_lru_walk_ops {
199 	/**
200 	 * process_bo - Process this bo.
201 	 * @walk: struct ttm_lru_walk describing the walk.
202 	 * @bo: A locked and referenced buffer object.
203 	 *
204 	 * Return: Negative error code on error, User-defined positive value
205 	 * (typically, but not always, size of the processed bo) on success.
206 	 * On success, the returned values are summed by the walk and the
207 	 * walk exits when its target is met.
208 	 * 0 also indicates success, -EBUSY means this bo was skipped.
209 	 */
210 	s64 (*process_bo)(struct ttm_lru_walk *walk, struct ttm_buffer_object *bo);
211 };
212 
213 /**
214  * struct ttm_lru_walk - Structure describing a LRU walk.
215  */
216 struct ttm_lru_walk {
217 	/** @ops: Pointer to the ops structure. */
218 	const struct ttm_lru_walk_ops *ops;
219 	/** @ctx: Pointer to the struct ttm_operation_ctx. */
220 	struct ttm_operation_ctx *ctx;
221 	/** @ticket: The struct ww_acquire_ctx if any. */
222 	struct ww_acquire_ctx *ticket;
223 	/** @trylock_only: Only use trylock for locking. */
224 	bool trylock_only;
225 };
226 
227 s64 ttm_lru_walk_for_evict(struct ttm_lru_walk *walk, struct ttm_device *bdev,
228 			   struct ttm_resource_manager *man, s64 target);
229 
230 /**
231  * ttm_bo_get - reference a struct ttm_buffer_object
232  *
233  * @bo: The buffer object.
234  */
ttm_bo_get(struct ttm_buffer_object * bo)235 static inline void ttm_bo_get(struct ttm_buffer_object *bo)
236 {
237 	kref_get(&bo->kref);
238 }
239 
240 /**
241  * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless
242  * its refcount has already reached zero.
243  * @bo: The buffer object.
244  *
245  * Used to reference a TTM buffer object in lookups where the object is removed
246  * from the lookup structure during the destructor and for RCU lookups.
247  *
248  * Returns: @bo if the referencing was successful, NULL otherwise.
249  */
250 static inline __must_check struct ttm_buffer_object *
ttm_bo_get_unless_zero(struct ttm_buffer_object * bo)251 ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
252 {
253 	if (!kref_get_unless_zero(&bo->kref))
254 		return NULL;
255 	return bo;
256 }
257 
258 /**
259  * ttm_bo_reserve:
260  *
261  * @bo: A pointer to a struct ttm_buffer_object.
262  * @interruptible: Sleep interruptible if waiting.
263  * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
264  * @ticket: ticket used to acquire the ww_mutex.
265  *
266  * Locks a buffer object for validation. (Or prevents other processes from
267  * locking it for validation), while taking a number of measures to prevent
268  * deadlocks.
269  *
270  * Returns:
271  * -EDEADLK: The reservation may cause a deadlock.
272  * Release all buffer reservations, wait for @bo to become unreserved and
273  * try again.
274  * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
275  * a signal. Release all buffer reservations and return to user-space.
276  * -EBUSY: The function needed to sleep, but @no_wait was true
277  * -EALREADY: Bo already reserved using @ticket. This error code will only
278  * be returned if @use_ticket is set to true.
279  */
ttm_bo_reserve(struct ttm_buffer_object * bo,bool interruptible,bool no_wait,struct ww_acquire_ctx * ticket)280 static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
281 				 bool interruptible, bool no_wait,
282 				 struct ww_acquire_ctx *ticket)
283 {
284 	int ret = 0;
285 
286 	if (no_wait) {
287 		bool success;
288 
289 		if (WARN_ON(ticket))
290 			return -EBUSY;
291 
292 		success = dma_resv_trylock(bo->base.resv);
293 		return success ? 0 : -EBUSY;
294 	}
295 
296 	if (interruptible)
297 		ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
298 	else
299 		ret = dma_resv_lock(bo->base.resv, ticket);
300 	if (ret == -EINTR)
301 		return -ERESTARTSYS;
302 	return ret;
303 }
304 
305 /**
306  * ttm_bo_reserve_slowpath:
307  * @bo: A pointer to a struct ttm_buffer_object.
308  * @interruptible: Sleep interruptible if waiting.
309  * @ticket: Ticket used to acquire the ww_mutex.
310  *
311  * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
312  * from all our other reservations. Because there are no other reservations
313  * held by us, this function cannot deadlock any more.
314  */
ttm_bo_reserve_slowpath(struct ttm_buffer_object * bo,bool interruptible,struct ww_acquire_ctx * ticket)315 static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
316 					  bool interruptible,
317 					  struct ww_acquire_ctx *ticket)
318 {
319 	if (interruptible) {
320 		int ret = dma_resv_lock_slow_interruptible(bo->base.resv,
321 							   ticket);
322 		if (ret == -EINTR)
323 			ret = -ERESTARTSYS;
324 		return ret;
325 	}
326 	dma_resv_lock_slow(bo->base.resv, ticket);
327 	return 0;
328 }
329 
330 void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo);
331 
332 static inline void
ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object * bo)333 ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
334 {
335 	spin_lock(&bo->bdev->lru_lock);
336 	ttm_bo_move_to_lru_tail(bo);
337 	spin_unlock(&bo->bdev->lru_lock);
338 }
339 
ttm_bo_assign_mem(struct ttm_buffer_object * bo,struct ttm_resource * new_mem)340 static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
341 				     struct ttm_resource *new_mem)
342 {
343 	WARN_ON(bo->resource);
344 	bo->resource = new_mem;
345 }
346 
347 /**
348  * ttm_bo_move_null - assign memory for a buffer object.
349  * @bo: The bo to assign the memory to
350  * @new_mem: The memory to be assigned.
351  *
352  * Assign the memory from new_mem to the memory of the buffer object bo.
353  */
ttm_bo_move_null(struct ttm_buffer_object * bo,struct ttm_resource * new_mem)354 static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
355 				    struct ttm_resource *new_mem)
356 {
357 	ttm_resource_free(bo, &bo->resource);
358 	ttm_bo_assign_mem(bo, new_mem);
359 }
360 
361 /**
362  * ttm_bo_unreserve
363  *
364  * @bo: A pointer to a struct ttm_buffer_object.
365  *
366  * Unreserve a previous reservation of @bo.
367  */
ttm_bo_unreserve(struct ttm_buffer_object * bo)368 static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
369 {
370 	ttm_bo_move_to_lru_tail_unlocked(bo);
371 	dma_resv_unlock(bo->base.resv);
372 }
373 
374 /**
375  * ttm_kmap_obj_virtual
376  *
377  * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
378  * @is_iomem: Pointer to an integer that on return indicates 1 if the
379  * virtual map is io memory, 0 if normal memory.
380  *
381  * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
382  * If *is_iomem is 1 on return, the virtual address points to an io memory area,
383  * that should strictly be accessed by the iowriteXX() and similar functions.
384  */
ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj * map,bool * is_iomem)385 static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
386 					 bool *is_iomem)
387 {
388 	*is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
389 	return map->virtual;
390 }
391 
392 int ttm_bo_wait_ctx(struct ttm_buffer_object *bo,
393 		    struct ttm_operation_ctx *ctx);
394 int ttm_bo_validate(struct ttm_buffer_object *bo,
395 		    struct ttm_placement *placement,
396 		    struct ttm_operation_ctx *ctx);
397 void ttm_bo_put(struct ttm_buffer_object *bo);
398 void ttm_bo_set_bulk_move(struct ttm_buffer_object *bo,
399 			  struct ttm_lru_bulk_move *bulk);
400 bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
401 			      const struct ttm_place *place);
402 int ttm_bo_init_reserved(struct ttm_device *bdev, struct ttm_buffer_object *bo,
403 			 enum ttm_bo_type type, struct ttm_placement *placement,
404 			 uint32_t alignment, struct ttm_operation_ctx *ctx,
405 			 struct sg_table *sg, struct dma_resv *resv,
406 			 void (*destroy)(struct ttm_buffer_object *));
407 int ttm_bo_init_validate(struct ttm_device *bdev, struct ttm_buffer_object *bo,
408 			 enum ttm_bo_type type, struct ttm_placement *placement,
409 			 uint32_t alignment, bool interruptible,
410 			 struct sg_table *sg, struct dma_resv *resv,
411 			 void (*destroy)(struct ttm_buffer_object *));
412 int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
413 		unsigned long num_pages, struct ttm_bo_kmap_obj *map);
414 void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
415 int ttm_bo_vmap(struct ttm_buffer_object *bo, struct iosys_map *map);
416 void ttm_bo_vunmap(struct ttm_buffer_object *bo, struct iosys_map *map);
417 #ifdef __linux__
418 int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
419 #else
420 int ttm_bo_mmap_obj(struct ttm_buffer_object *bo);
421 #endif
422 s64 ttm_bo_swapout(struct ttm_device *bdev, struct ttm_operation_ctx *ctx,
423 		   struct ttm_resource_manager *man, gfp_t gfp_flags,
424 		   s64 target);
425 void ttm_bo_pin(struct ttm_buffer_object *bo);
426 void ttm_bo_unpin(struct ttm_buffer_object *bo);
427 int ttm_bo_evict_first(struct ttm_device *bdev,
428 		       struct ttm_resource_manager *man,
429 		       struct ttm_operation_ctx *ctx);
430 #ifdef __linux__
431 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
432 			     struct vm_fault *vmf);
433 vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
434 				    pgprot_t prot,
435 				    pgoff_t num_prefault);
436 vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
437 void ttm_bo_vm_open(struct vm_area_struct *vma);
438 void ttm_bo_vm_close(struct vm_area_struct *vma);
439 int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
440 		     void *buf, int len, int write);
441 vm_fault_t ttm_bo_vm_dummy_page(struct vm_fault *vmf, pgprot_t prot);
442 #else
443 vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo);
444 vm_fault_t ttm_bo_vm_fault_reserved(struct uvm_faultinfo *ufi,
445 				    vaddr_t vaddr,
446 				    pgoff_t num_prefault,
447 				    pgoff_t fault_page_size);
448 int ttm_bo_vm_fault(struct uvm_faultinfo *, vaddr_t, vm_page_t *,
449     int, int, vm_fault_t, vm_prot_t, int);
450 #endif /* !__linux__ */
451 
452 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
453 		     struct ttm_placement *placement,
454 		     struct ttm_resource **mem,
455 		     struct ttm_operation_ctx *ctx);
456 
457 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
458 /*
459  * ttm_bo_util.c
460  */
461 int ttm_mem_io_reserve(struct ttm_device *bdev,
462 		       struct ttm_resource *mem);
463 void ttm_mem_io_free(struct ttm_device *bdev,
464 		     struct ttm_resource *mem);
465 void ttm_move_memcpy(bool clear, u32 num_pages,
466 		     struct ttm_kmap_iter *dst_iter,
467 		     struct ttm_kmap_iter *src_iter,
468 		     bus_space_tag_t memt);
469 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
470 		       struct ttm_operation_ctx *ctx,
471 		       struct ttm_resource *new_mem);
472 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
473 			      struct dma_fence *fence, bool evict,
474 			      bool pipeline,
475 			      struct ttm_resource *new_mem);
476 void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo,
477 			      struct ttm_resource *new_mem);
478 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
479 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
480 		     pgprot_t tmp);
481 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
482 
483 #endif
484