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