Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

Apr 14-17, 2025
Register
Loading...
v6.2
  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#ifndef _TTM_BO_DRIVER_H_
 31#define _TTM_BO_DRIVER_H_
 32
 33#include <drm/drm_mm.h>
 34#include <drm/drm_vma_manager.h>
 35#include <linux/workqueue.h>
 36#include <linux/fs.h>
 37#include <linux/spinlock.h>
 38#include <linux/dma-resv.h>
 39
 40#include <drm/ttm/ttm_device.h>
 41
 42#include "ttm_bo_api.h"
 43#include "ttm_kmap_iter.h"
 
 44#include "ttm_placement.h"
 45#include "ttm_tt.h"
 46#include "ttm_pool.h"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 47
 48/*
 49 * ttm_bo.c
 50 */
 51
 52/**
 53 * ttm_bo_mem_space
 54 *
 55 * @bo: Pointer to a struct ttm_buffer_object. the data of which
 56 * we want to allocate space for.
 57 * @proposed_placement: Proposed new placement for the buffer object.
 58 * @mem: A struct ttm_resource.
 59 * @interruptible: Sleep interruptible when sliping.
 60 * @no_wait_gpu: Return immediately if the GPU is busy.
 61 *
 62 * Allocate memory space for the buffer object pointed to by @bo, using
 63 * the placement flags in @mem, potentially evicting other idle buffer objects.
 64 * This function may sleep while waiting for space to become available.
 65 * Returns:
 66 * -EBUSY: No space available (only if no_wait == 1).
 67 * -ENOMEM: Could not allocate memory for the buffer object, either due to
 68 * fragmentation or concurrent allocators.
 69 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
 70 */
 71int ttm_bo_mem_space(struct ttm_buffer_object *bo,
 72		     struct ttm_placement *placement,
 73		     struct ttm_resource **mem,
 74		     struct ttm_operation_ctx *ctx);
 75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 76/**
 77 * ttm_bo_unmap_virtual
 78 *
 79 * @bo: tear down the virtual mappings for this BO
 80 */
 81void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
 82
 83/**
 84 * ttm_bo_reserve:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 85 *
 86 * @bo: A pointer to a struct ttm_buffer_object.
 87 * @interruptible: Sleep interruptible if waiting.
 88 * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
 89 * @ticket: ticket used to acquire the ww_mutex.
 90 *
 91 * Locks a buffer object for validation. (Or prevents other processes from
 92 * locking it for validation), while taking a number of measures to prevent
 93 * deadlocks.
 94 *
 95 * Returns:
 96 * -EDEADLK: The reservation may cause a deadlock.
 97 * Release all buffer reservations, wait for @bo to become unreserved and
 98 * try again.
 99 * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
100 * a signal. Release all buffer reservations and return to user-space.
101 * -EBUSY: The function needed to sleep, but @no_wait was true
102 * -EALREADY: Bo already reserved using @ticket. This error code will only
103 * be returned if @use_ticket is set to true.
104 */
105static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
106				 bool interruptible, bool no_wait,
107				 struct ww_acquire_ctx *ticket)
108{
109	int ret;
110
111	if (no_wait) {
112		bool success;
113		if (WARN_ON(ticket))
114			return -EBUSY;
115
116		success = dma_resv_trylock(bo->base.resv);
117		return success ? 0 : -EBUSY;
118	}
119
120	if (interruptible)
121		ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
122	else
123		ret = dma_resv_lock(bo->base.resv, ticket);
124	if (ret == -EINTR)
125		return -ERESTARTSYS;
126	return ret;
127}
128
129/**
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130 * ttm_bo_reserve_slowpath:
131 * @bo: A pointer to a struct ttm_buffer_object.
132 * @interruptible: Sleep interruptible if waiting.
133 * @sequence: Set (@bo)->sequence to this value after lock
134 *
135 * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
136 * from all our other reservations. Because there are no other reservations
137 * held by us, this function cannot deadlock any more.
138 */
139static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
140					  bool interruptible,
141					  struct ww_acquire_ctx *ticket)
142{
143	if (interruptible) {
144		int ret = dma_resv_lock_slow_interruptible(bo->base.resv,
145							   ticket);
146		if (ret == -EINTR)
147			ret = -ERESTARTSYS;
148		return ret;
149	}
150	dma_resv_lock_slow(bo->base.resv, ticket);
151	return 0;
152}
153
154static inline void
155ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
156{
157	spin_lock(&bo->bdev->lru_lock);
158	ttm_bo_move_to_lru_tail(bo);
159	spin_unlock(&bo->bdev->lru_lock);
160}
161
162static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
163				     struct ttm_resource *new_mem)
164{
165	WARN_ON(bo->resource);
166	bo->resource = new_mem;
167}
168
169/**
170 * ttm_bo_move_null = assign memory for a buffer object.
171 * @bo: The bo to assign the memory to
172 * @new_mem: The memory to be assigned.
173 *
174 * Assign the memory from new_mem to the memory of the buffer object bo.
175 */
176static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
177				    struct ttm_resource *new_mem)
178{
179	ttm_resource_free(bo, &bo->resource);
180	ttm_bo_assign_mem(bo, new_mem);
181}
182
183/**
184 * ttm_bo_unreserve
185 *
186 * @bo: A pointer to a struct ttm_buffer_object.
187 *
188 * Unreserve a previous reservation of @bo.
189 */
190static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
191{
192	ttm_bo_move_to_lru_tail_unlocked(bo);
 
 
193	dma_resv_unlock(bo->base.resv);
194}
195
196/*
197 * ttm_bo_util.c
198 */
199int ttm_mem_io_reserve(struct ttm_device *bdev,
200		       struct ttm_resource *mem);
201void ttm_mem_io_free(struct ttm_device *bdev,
202		     struct ttm_resource *mem);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
204/**
205 * ttm_bo_move_memcpy
206 *
207 * @bo: A pointer to a struct ttm_buffer_object.
208 * @interruptible: Sleep interruptible if waiting.
209 * @no_wait_gpu: Return immediately if the GPU is busy.
210 * @new_mem: struct ttm_resource indicating where to move.
211 *
212 * Fallback move function for a mappable buffer object in mappable memory.
213 * The function will, if successful,
214 * free any old aperture space, and set (@new_mem)->mm_node to NULL,
215 * and update the (@bo)->mem placement flags. If unsuccessful, the old
216 * data remains untouched, and it's up to the caller to free the
217 * memory space indicated by @new_mem.
218 * Returns:
219 * !0: Failure.
220 */
221
222int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
223		       struct ttm_operation_ctx *ctx,
224		       struct ttm_resource *new_mem);
 
 
 
 
 
 
 
 
 
225
226/**
227 * ttm_bo_move_accel_cleanup.
228 *
229 * @bo: A pointer to a struct ttm_buffer_object.
230 * @fence: A fence object that signals when moving is complete.
231 * @evict: This is an evict move. Don't return until the buffer is idle.
232 * @pipeline: evictions are to be pipelined.
233 * @new_mem: struct ttm_resource indicating where to move.
234 *
235 * Accelerated move function to be called when an accelerated move
236 * has been scheduled. The function will create a new temporary buffer object
237 * representing the old placement, and put the sync object on both buffer
238 * objects. After that the newly created buffer object is unref'd to be
239 * destroyed when the move is complete. This will help pipeline
240 * buffer moves.
241 */
242int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
243			      struct dma_fence *fence, bool evict,
244			      bool pipeline,
245			      struct ttm_resource *new_mem);
246
247/**
248 * ttm_bo_move_sync_cleanup.
249 *
250 * @bo: A pointer to a struct ttm_buffer_object.
251 * @new_mem: struct ttm_resource indicating where to move.
 
 
252 *
253 * Special case of ttm_bo_move_accel_cleanup where the bo is guaranteed
254 * by the caller to be idle. Typically used after memcpy buffer moves.
255 */
256void ttm_bo_move_sync_cleanup(struct ttm_buffer_object *bo,
257			      struct ttm_resource *new_mem);
 
258
259/**
260 * ttm_bo_pipeline_gutting.
261 *
262 * @bo: A pointer to a struct ttm_buffer_object.
263 *
264 * Pipelined gutting a BO of its backing store.
265 */
266int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
267
268/**
269 * ttm_io_prot
270 *
271 * bo: ttm buffer object
272 * res: ttm resource object
273 * @tmp: Page protection flag for a normal, cached mapping.
274 *
275 * Utility function that returns the pgprot_t that should be used for
276 * setting up a PTE with the caching model indicated by @c_state.
277 */
278pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
279		     pgprot_t tmp);
280
281/**
282 * ttm_bo_tt_bind
283 *
284 * Bind the object tt to a memory resource.
285 */
286int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem);
287
288/**
289 * ttm_bo_tt_destroy.
290 */
291void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
292
293void ttm_move_memcpy(bool clear,
294		     u32 num_pages,
295		     struct ttm_kmap_iter *dst_iter,
296		     struct ttm_kmap_iter *src_iter);
297
298struct ttm_kmap_iter *
299ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io,
300			 struct io_mapping *iomap,
301			 struct sg_table *st,
302			 resource_size_t start);
303#endif
v5.9
  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#ifndef _TTM_BO_DRIVER_H_
 31#define _TTM_BO_DRIVER_H_
 32
 33#include <drm/drm_mm.h>
 34#include <drm/drm_vma_manager.h>
 35#include <linux/workqueue.h>
 36#include <linux/fs.h>
 37#include <linux/spinlock.h>
 38#include <linux/dma-resv.h>
 39
 
 
 40#include "ttm_bo_api.h"
 41#include "ttm_memory.h"
 42#include "ttm_module.h"
 43#include "ttm_placement.h"
 44#include "ttm_tt.h"
 45
 46#define TTM_MAX_BO_PRIORITY	4U
 47
 48#define TTM_MEMTYPE_FLAG_FIXED         (1 << 0)	/* Fixed (on-card) PCI memory */
 49#define TTM_MEMTYPE_FLAG_MAPPABLE      (1 << 1)	/* Memory mappable */
 50
 51struct ttm_mem_type_manager;
 52
 53struct ttm_mem_type_manager_func {
 54	/**
 55	 * struct ttm_mem_type_manager member init
 56	 *
 57	 * @man: Pointer to a memory type manager.
 58	 * @p_size: Implementation dependent, but typically the size of the
 59	 * range to be managed in pages.
 60	 *
 61	 * Called to initialize a private range manager. The function is
 62	 * expected to initialize the man::priv member.
 63	 * Returns 0 on success, negative error code on failure.
 64	 */
 65	int  (*init)(struct ttm_mem_type_manager *man, unsigned long p_size);
 66
 67	/**
 68	 * struct ttm_mem_type_manager member takedown
 69	 *
 70	 * @man: Pointer to a memory type manager.
 71	 *
 72	 * Called to undo the setup done in init. All allocated resources
 73	 * should be freed.
 74	 */
 75	int  (*takedown)(struct ttm_mem_type_manager *man);
 76
 77	/**
 78	 * struct ttm_mem_type_manager member get_node
 79	 *
 80	 * @man: Pointer to a memory type manager.
 81	 * @bo: Pointer to the buffer object we're allocating space for.
 82	 * @placement: Placement details.
 83	 * @flags: Additional placement flags.
 84	 * @mem: Pointer to a struct ttm_mem_reg to be filled in.
 85	 *
 86	 * This function should allocate space in the memory type managed
 87	 * by @man. Placement details if
 88	 * applicable are given by @placement. If successful,
 89	 * @mem::mm_node should be set to a non-null value, and
 90	 * @mem::start should be set to a value identifying the beginning
 91	 * of the range allocated, and the function should return zero.
 92	 * If the memory region accommodate the buffer object, @mem::mm_node
 93	 * should be set to NULL, and the function should return 0.
 94	 * If a system error occurred, preventing the request to be fulfilled,
 95	 * the function should return a negative error code.
 96	 *
 97	 * Note that @mem::mm_node will only be dereferenced by
 98	 * struct ttm_mem_type_manager functions and optionally by the driver,
 99	 * which has knowledge of the underlying type.
100	 *
101	 * This function may not be called from within atomic context, so
102	 * an implementation can and must use either a mutex or a spinlock to
103	 * protect any data structures managing the space.
104	 */
105	int  (*get_node)(struct ttm_mem_type_manager *man,
106			 struct ttm_buffer_object *bo,
107			 const struct ttm_place *place,
108			 struct ttm_mem_reg *mem);
109
110	/**
111	 * struct ttm_mem_type_manager member put_node
112	 *
113	 * @man: Pointer to a memory type manager.
114	 * @mem: Pointer to a struct ttm_mem_reg to be filled in.
115	 *
116	 * This function frees memory type resources previously allocated
117	 * and that are identified by @mem::mm_node and @mem::start. May not
118	 * be called from within atomic context.
119	 */
120	void (*put_node)(struct ttm_mem_type_manager *man,
121			 struct ttm_mem_reg *mem);
122
123	/**
124	 * struct ttm_mem_type_manager member debug
125	 *
126	 * @man: Pointer to a memory type manager.
127	 * @printer: Prefix to be used in printout to identify the caller.
128	 *
129	 * This function is called to print out the state of the memory
130	 * type manager to aid debugging of out-of-memory conditions.
131	 * It may not be called from within atomic context.
132	 */
133	void (*debug)(struct ttm_mem_type_manager *man,
134		      struct drm_printer *printer);
135};
136
137/**
138 * struct ttm_mem_type_manager
139 *
140 * @has_type: The memory type has been initialized.
141 * @use_type: The memory type is enabled.
142 * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
143 * managed by this memory type.
144 * @gpu_offset: If used, the GPU offset of the first managed page of
145 * fixed memory or the first managed location in an aperture.
146 * @size: Size of the managed region.
147 * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
148 * as defined in ttm_placement_common.h
149 * @default_caching: The default caching policy used for a buffer object
150 * placed in this memory type if the user doesn't provide one.
151 * @func: structure pointer implementing the range manager. See above
152 * @priv: Driver private closure for @func.
153 * @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures
154 * @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions
155 * reserved by the TTM vm system.
156 * @io_reserve_lru: Optional lru list for unreserving io mem regions.
157 * @move_lock: lock for move fence
158 * static information. bdev::driver::io_mem_free is never used.
159 * @lru: The lru list for this memory type.
160 * @move: The fence of the last pipelined move operation.
161 *
162 * This structure is used to identify and manage memory types for a device.
163 * It's set up by the ttm_bo_driver::init_mem_type method.
164 */
165
166
167
168struct ttm_mem_type_manager {
169	struct ttm_bo_device *bdev;
170
171	/*
172	 * No protection. Constant from start.
173	 */
174
175	bool has_type;
176	bool use_type;
177	uint32_t flags;
178	uint64_t size;
179	uint32_t available_caching;
180	uint32_t default_caching;
181	const struct ttm_mem_type_manager_func *func;
182	void *priv;
183	struct mutex io_reserve_mutex;
184	bool use_io_reserve_lru;
185	spinlock_t move_lock;
186
187	/*
188	 * Protected by @io_reserve_mutex:
189	 */
190
191	struct list_head io_reserve_lru;
192
193	/*
194	 * Protected by the global->lru_lock.
195	 */
196
197	struct list_head lru[TTM_MAX_BO_PRIORITY];
198
199	/*
200	 * Protected by @move_lock.
201	 */
202	struct dma_fence *move;
203};
204
205/**
206 * struct ttm_bo_driver
207 *
208 * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
209 * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
210 * structure.
211 * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
212 * @move: Callback for a driver to hook in accelerated functions to
213 * move a buffer.
214 * If set to NULL, a potentially slow memcpy() move is used.
215 */
216
217struct ttm_bo_driver {
218	/**
219	 * ttm_tt_create
220	 *
221	 * @bo: The buffer object to create the ttm for.
222	 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
223	 *
224	 * Create a struct ttm_tt to back data with system memory pages.
225	 * No pages are actually allocated.
226	 * Returns:
227	 * NULL: Out of memory.
228	 */
229	struct ttm_tt *(*ttm_tt_create)(struct ttm_buffer_object *bo,
230					uint32_t page_flags);
231
232	/**
233	 * ttm_tt_populate
234	 *
235	 * @ttm: The struct ttm_tt to contain the backing pages.
236	 *
237	 * Allocate all backing pages
238	 * Returns:
239	 * -ENOMEM: Out of memory.
240	 */
241	int (*ttm_tt_populate)(struct ttm_tt *ttm,
242			struct ttm_operation_ctx *ctx);
243
244	/**
245	 * ttm_tt_unpopulate
246	 *
247	 * @ttm: The struct ttm_tt to contain the backing pages.
248	 *
249	 * Free all backing page
250	 */
251	void (*ttm_tt_unpopulate)(struct ttm_tt *ttm);
252
253	int (*init_mem_type)(struct ttm_bo_device *bdev, uint32_t type,
254			     struct ttm_mem_type_manager *man);
255
256	/**
257	 * struct ttm_bo_driver member eviction_valuable
258	 *
259	 * @bo: the buffer object to be evicted
260	 * @place: placement we need room for
261	 *
262	 * Check with the driver if it is valuable to evict a BO to make room
263	 * for a certain placement.
264	 */
265	bool (*eviction_valuable)(struct ttm_buffer_object *bo,
266				  const struct ttm_place *place);
267	/**
268	 * struct ttm_bo_driver member evict_flags:
269	 *
270	 * @bo: the buffer object to be evicted
271	 *
272	 * Return the bo flags for a buffer which is not mapped to the hardware.
273	 * These will be placed in proposed_flags so that when the move is
274	 * finished, they'll end up in bo->mem.flags
275	 */
276
277	void (*evict_flags)(struct ttm_buffer_object *bo,
278			    struct ttm_placement *placement);
279
280	/**
281	 * struct ttm_bo_driver member move:
282	 *
283	 * @bo: the buffer to move
284	 * @evict: whether this motion is evicting the buffer from
285	 * the graphics address space
286	 * @ctx: context for this move with parameters
287	 * @new_mem: the new memory region receiving the buffer
288	 *
289	 * Move a buffer between two memory regions.
290	 */
291	int (*move)(struct ttm_buffer_object *bo, bool evict,
292		    struct ttm_operation_ctx *ctx,
293		    struct ttm_mem_reg *new_mem);
294
295	/**
296	 * struct ttm_bo_driver_member verify_access
297	 *
298	 * @bo: Pointer to a buffer object.
299	 * @filp: Pointer to a struct file trying to access the object.
300	 *
301	 * Called from the map / write / read methods to verify that the
302	 * caller is permitted to access the buffer object.
303	 * This member may be set to NULL, which will refuse this kind of
304	 * access for all buffer objects.
305	 * This function should return 0 if access is granted, -EPERM otherwise.
306	 */
307	int (*verify_access)(struct ttm_buffer_object *bo,
308			     struct file *filp);
309
310	/**
311	 * Hook to notify driver about a driver move so it
312	 * can do tiling things and book-keeping.
313	 *
314	 * @evict: whether this move is evicting the buffer from the graphics
315	 * address space
316	 */
317	void (*move_notify)(struct ttm_buffer_object *bo,
318			    bool evict,
319			    struct ttm_mem_reg *new_mem);
320	/* notify the driver we are taking a fault on this BO
321	 * and have reserved it */
322	int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
323
324	/**
325	 * notify the driver that we're about to swap out this bo
326	 */
327	void (*swap_notify)(struct ttm_buffer_object *bo);
328
329	/**
330	 * Driver callback on when mapping io memory (for bo_move_memcpy
331	 * for instance). TTM will take care to call io_mem_free whenever
332	 * the mapping is not use anymore. io_mem_reserve & io_mem_free
333	 * are balanced.
334	 */
335	int (*io_mem_reserve)(struct ttm_bo_device *bdev,
336			      struct ttm_mem_reg *mem);
337	void (*io_mem_free)(struct ttm_bo_device *bdev,
338			    struct ttm_mem_reg *mem);
339
340	/**
341	 * Return the pfn for a given page_offset inside the BO.
342	 *
343	 * @bo: the BO to look up the pfn for
344	 * @page_offset: the offset to look up
345	 */
346	unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
347				    unsigned long page_offset);
348
349	/**
350	 * Read/write memory buffers for ptrace access
351	 *
352	 * @bo: the BO to access
353	 * @offset: the offset from the start of the BO
354	 * @buf: pointer to source/destination buffer
355	 * @len: number of bytes to copy
356	 * @write: whether to read (0) from or write (non-0) to BO
357	 *
358	 * If successful, this function should return the number of
359	 * bytes copied, -EIO otherwise. If the number of bytes
360	 * returned is < len, the function may be called again with
361	 * the remainder of the buffer to copy.
362	 */
363	int (*access_memory)(struct ttm_buffer_object *bo, unsigned long offset,
364			     void *buf, int len, int write);
365
366	/**
367	 * struct ttm_bo_driver member del_from_lru_notify
368	 *
369	 * @bo: the buffer object deleted from lru
370	 *
371	 * notify driver that a BO was deleted from LRU.
372	 */
373	void (*del_from_lru_notify)(struct ttm_buffer_object *bo);
374
375	/**
376	 * Notify the driver that we're about to release a BO
377	 *
378	 * @bo: BO that is about to be released
379	 *
380	 * Gives the driver a chance to do any cleanup, including
381	 * adding fences that may force a delayed delete
382	 */
383	void (*release_notify)(struct ttm_buffer_object *bo);
384};
385
386/**
387 * struct ttm_bo_global - Buffer object driver global data.
388 *
389 * @dummy_read_page: Pointer to a dummy page used for mapping requests
390 * of unpopulated pages.
391 * @shrink: A shrink callback object used for buffer object swap.
392 * @device_list_mutex: Mutex protecting the device list.
393 * This mutex is held while traversing the device list for pm options.
394 * @lru_lock: Spinlock protecting the bo subsystem lru lists.
395 * @device_list: List of buffer object devices.
396 * @swap_lru: Lru list of buffer objects used for swapping.
397 */
398
399extern struct ttm_bo_global {
400
401	/**
402	 * Constant after init.
403	 */
404
405	struct kobject kobj;
406	struct page *dummy_read_page;
407	spinlock_t lru_lock;
408
409	/**
410	 * Protected by ttm_global_mutex.
411	 */
412	struct list_head device_list;
413
414	/**
415	 * Protected by the lru_lock.
416	 */
417	struct list_head swap_lru[TTM_MAX_BO_PRIORITY];
418
419	/**
420	 * Internal protection.
421	 */
422	atomic_t bo_count;
423} ttm_bo_glob;
424
425
426#define TTM_NUM_MEM_TYPES 8
427
428/**
429 * struct ttm_bo_device - Buffer object driver device-specific data.
430 *
431 * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
432 * @man: An array of mem_type_managers.
433 * @vma_manager: Address space manager (pointer)
434 * lru_lock: Spinlock that protects the buffer+device lru lists and
435 * ddestroy lists.
436 * @dev_mapping: A pointer to the struct address_space representing the
437 * device address space.
438 * @wq: Work queue structure for the delayed delete workqueue.
439 * @no_retry: Don't retry allocation if it fails
440 *
441 */
442
443struct ttm_bo_device {
444
445	/*
446	 * Constant after bo device init / atomic.
447	 */
448	struct list_head device_list;
449	struct ttm_bo_driver *driver;
450	struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
451
452	/*
453	 * Protected by internal locks.
454	 */
455	struct drm_vma_offset_manager *vma_manager;
456
457	/*
458	 * Protected by the global:lru lock.
459	 */
460	struct list_head ddestroy;
461
462	/*
463	 * Protected by load / firstopen / lastclose /unload sync.
464	 */
465
466	struct address_space *dev_mapping;
467
468	/*
469	 * Internal protection.
470	 */
471
472	struct delayed_work wq;
473
474	bool need_dma32;
475
476	bool no_retry;
477};
478
479/**
480 * struct ttm_lru_bulk_move_pos
481 *
482 * @first: first BO in the bulk move range
483 * @last: last BO in the bulk move range
484 *
485 * Positions for a lru bulk move.
486 */
487struct ttm_lru_bulk_move_pos {
488	struct ttm_buffer_object *first;
489	struct ttm_buffer_object *last;
490};
491
492/**
493 * struct ttm_lru_bulk_move
494 *
495 * @tt: first/last lru entry for BOs in the TT domain
496 * @vram: first/last lru entry for BOs in the VRAM domain
497 * @swap: first/last lru entry for BOs on the swap list
498 *
499 * Helper structure for bulk moves on the LRU list.
500 */
501struct ttm_lru_bulk_move {
502	struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY];
503	struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY];
504	struct ttm_lru_bulk_move_pos swap[TTM_MAX_BO_PRIORITY];
505};
506
507/**
508 * ttm_flag_masked
509 *
510 * @old: Pointer to the result and original value.
511 * @new: New value of bits.
512 * @mask: Mask of bits to change.
513 *
514 * Convenience function to change a number of bits identified by a mask.
515 */
516
517static inline uint32_t
518ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
519{
520	*old ^= (*old ^ new) & mask;
521	return *old;
522}
523
524/*
525 * ttm_bo.c
526 */
527
528/**
529 * ttm_bo_mem_space
530 *
531 * @bo: Pointer to a struct ttm_buffer_object. the data of which
532 * we want to allocate space for.
533 * @proposed_placement: Proposed new placement for the buffer object.
534 * @mem: A struct ttm_mem_reg.
535 * @interruptible: Sleep interruptible when sliping.
536 * @no_wait_gpu: Return immediately if the GPU is busy.
537 *
538 * Allocate memory space for the buffer object pointed to by @bo, using
539 * the placement flags in @mem, potentially evicting other idle buffer objects.
540 * This function may sleep while waiting for space to become available.
541 * Returns:
542 * -EBUSY: No space available (only if no_wait == 1).
543 * -ENOMEM: Could not allocate memory for the buffer object, either due to
544 * fragmentation or concurrent allocators.
545 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
546 */
547int ttm_bo_mem_space(struct ttm_buffer_object *bo,
548		     struct ttm_placement *placement,
549		     struct ttm_mem_reg *mem,
550		     struct ttm_operation_ctx *ctx);
551
552void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem);
553
554int ttm_bo_device_release(struct ttm_bo_device *bdev);
555
556/**
557 * ttm_bo_device_init
558 *
559 * @bdev: A pointer to a struct ttm_bo_device to initialize.
560 * @glob: A pointer to an initialized struct ttm_bo_global.
561 * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
562 * @mapping: The address space to use for this bo.
563 * @vma_manager: A pointer to a vma manager.
564 * @file_page_offset: Offset into the device address space that is available
565 * for buffer data. This ensures compatibility with other users of the
566 * address space.
567 *
568 * Initializes a struct ttm_bo_device:
569 * Returns:
570 * !0: Failure.
571 */
572int ttm_bo_device_init(struct ttm_bo_device *bdev,
573		       struct ttm_bo_driver *driver,
574		       struct address_space *mapping,
575		       struct drm_vma_offset_manager *vma_manager,
576		       bool need_dma32);
577
578/**
579 * ttm_bo_unmap_virtual
580 *
581 * @bo: tear down the virtual mappings for this BO
582 */
583void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
584
585/**
586 * ttm_bo_unmap_virtual
587 *
588 * @bo: tear down the virtual mappings for this BO
589 *
590 * The caller must take ttm_mem_io_lock before calling this function.
591 */
592void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
593
594int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);
595void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);
596int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible);
597void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
598
599/**
600 * __ttm_bo_reserve:
601 *
602 * @bo: A pointer to a struct ttm_buffer_object.
603 * @interruptible: Sleep interruptible if waiting.
604 * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
605 * @ticket: ticket used to acquire the ww_mutex.
606 *
607 * Will not remove reserved buffers from the lru lists.
608 * Otherwise identical to ttm_bo_reserve.
 
609 *
610 * Returns:
611 * -EDEADLK: The reservation may cause a deadlock.
612 * Release all buffer reservations, wait for @bo to become unreserved and
613 * try again. (only if use_sequence == 1).
614 * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
615 * a signal. Release all buffer reservations and return to user-space.
616 * -EBUSY: The function needed to sleep, but @no_wait was true
617 * -EALREADY: Bo already reserved using @ticket. This error code will only
618 * be returned if @use_ticket is set to true.
619 */
620static inline int __ttm_bo_reserve(struct ttm_buffer_object *bo,
621				   bool interruptible, bool no_wait,
622				   struct ww_acquire_ctx *ticket)
623{
624	int ret = 0;
625
626	if (no_wait) {
627		bool success;
628		if (WARN_ON(ticket))
629			return -EBUSY;
630
631		success = dma_resv_trylock(bo->base.resv);
632		return success ? 0 : -EBUSY;
633	}
634
635	if (interruptible)
636		ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
637	else
638		ret = dma_resv_lock(bo->base.resv, ticket);
639	if (ret == -EINTR)
640		return -ERESTARTSYS;
641	return ret;
642}
643
644/**
645 * ttm_bo_reserve:
646 *
647 * @bo: A pointer to a struct ttm_buffer_object.
648 * @interruptible: Sleep interruptible if waiting.
649 * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
650 * @ticket: ticket used to acquire the ww_mutex.
651 *
652 * Locks a buffer object for validation. (Or prevents other processes from
653 * locking it for validation) and removes it from lru lists, while taking
654 * a number of measures to prevent deadlocks.
655 *
656 * Deadlocks may occur when two processes try to reserve multiple buffers in
657 * different order, either by will or as a result of a buffer being evicted
658 * to make room for a buffer already reserved. (Buffers are reserved before
659 * they are evicted). The following algorithm prevents such deadlocks from
660 * occurring:
661 * Processes attempting to reserve multiple buffers other than for eviction,
662 * (typically execbuf), should first obtain a unique 32-bit
663 * validation sequence number,
664 * and call this function with @use_ticket == 1 and @ticket->stamp == the unique
665 * sequence number. If upon call of this function, the buffer object is already
666 * reserved, the validation sequence is checked against the validation
667 * sequence of the process currently reserving the buffer,
668 * and if the current validation sequence is greater than that of the process
669 * holding the reservation, the function returns -EDEADLK. Otherwise it sleeps
670 * waiting for the buffer to become unreserved, after which it retries
671 * reserving.
672 * The caller should, when receiving an -EDEADLK error
673 * release all its buffer reservations, wait for @bo to become unreserved, and
674 * then rerun the validation with the same validation sequence. This procedure
675 * will always guarantee that the process with the lowest validation sequence
676 * will eventually succeed, preventing both deadlocks and starvation.
677 *
678 * Returns:
679 * -EDEADLK: The reservation may cause a deadlock.
680 * Release all buffer reservations, wait for @bo to become unreserved and
681 * try again. (only if use_sequence == 1).
682 * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
683 * a signal. Release all buffer reservations and return to user-space.
684 * -EBUSY: The function needed to sleep, but @no_wait was true
685 * -EALREADY: Bo already reserved using @ticket. This error code will only
686 * be returned if @use_ticket is set to true.
687 */
688static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
689				 bool interruptible, bool no_wait,
690				 struct ww_acquire_ctx *ticket)
691{
692	WARN_ON(!kref_read(&bo->kref));
693
694	return __ttm_bo_reserve(bo, interruptible, no_wait, ticket);
695}
696
697/**
698 * ttm_bo_reserve_slowpath:
699 * @bo: A pointer to a struct ttm_buffer_object.
700 * @interruptible: Sleep interruptible if waiting.
701 * @sequence: Set (@bo)->sequence to this value after lock
702 *
703 * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
704 * from all our other reservations. Because there are no other reservations
705 * held by us, this function cannot deadlock any more.
706 */
707static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
708					  bool interruptible,
709					  struct ww_acquire_ctx *ticket)
710{
711	int ret = 0;
 
 
 
 
 
 
 
 
 
712
713	WARN_ON(!kref_read(&bo->kref));
 
 
 
 
 
 
714
715	if (interruptible)
716		ret = dma_resv_lock_slow_interruptible(bo->base.resv,
717								 ticket);
718	else
719		dma_resv_lock_slow(bo->base.resv, ticket);
 
720
721	if (ret == -EINTR)
722		ret = -ERESTARTSYS;
723
724	return ret;
 
 
 
 
 
 
 
 
725}
726
727/**
728 * ttm_bo_unreserve
729 *
730 * @bo: A pointer to a struct ttm_buffer_object.
731 *
732 * Unreserve a previous reservation of @bo.
733 */
734static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
735{
736	spin_lock(&ttm_bo_glob.lru_lock);
737	ttm_bo_move_to_lru_tail(bo, NULL);
738	spin_unlock(&ttm_bo_glob.lru_lock);
739	dma_resv_unlock(bo->base.resv);
740}
741
742/*
743 * ttm_bo_util.c
744 */
745
746int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
747		       struct ttm_mem_reg *mem);
748void ttm_mem_io_free(struct ttm_bo_device *bdev,
749		     struct ttm_mem_reg *mem);
750/**
751 * ttm_bo_move_ttm
752 *
753 * @bo: A pointer to a struct ttm_buffer_object.
754 * @interruptible: Sleep interruptible if waiting.
755 * @no_wait_gpu: Return immediately if the GPU is busy.
756 * @new_mem: struct ttm_mem_reg indicating where to move.
757 *
758 * Optimized move function for a buffer object with both old and
759 * new placement backed by a TTM. The function will, if successful,
760 * free any old aperture space, and set (@new_mem)->mm_node to NULL,
761 * and update the (@bo)->mem placement flags. If unsuccessful, the old
762 * data remains untouched, and it's up to the caller to free the
763 * memory space indicated by @new_mem.
764 * Returns:
765 * !0: Failure.
766 */
767
768int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
769		    struct ttm_operation_ctx *ctx,
770		    struct ttm_mem_reg *new_mem);
771
772/**
773 * ttm_bo_move_memcpy
774 *
775 * @bo: A pointer to a struct ttm_buffer_object.
776 * @interruptible: Sleep interruptible if waiting.
777 * @no_wait_gpu: Return immediately if the GPU is busy.
778 * @new_mem: struct ttm_mem_reg indicating where to move.
779 *
780 * Fallback move function for a mappable buffer object in mappable memory.
781 * The function will, if successful,
782 * free any old aperture space, and set (@new_mem)->mm_node to NULL,
783 * and update the (@bo)->mem placement flags. If unsuccessful, the old
784 * data remains untouched, and it's up to the caller to free the
785 * memory space indicated by @new_mem.
786 * Returns:
787 * !0: Failure.
788 */
789
790int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
791		       struct ttm_operation_ctx *ctx,
792		       struct ttm_mem_reg *new_mem);
793
794/**
795 * ttm_bo_free_old_node
796 *
797 * @bo: A pointer to a struct ttm_buffer_object.
798 *
799 * Utility function to free an old placement after a successful move.
800 */
801void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
802
803/**
804 * ttm_bo_move_accel_cleanup.
805 *
806 * @bo: A pointer to a struct ttm_buffer_object.
807 * @fence: A fence object that signals when moving is complete.
808 * @evict: This is an evict move. Don't return until the buffer is idle.
809 * @new_mem: struct ttm_mem_reg indicating where to move.
 
810 *
811 * Accelerated move function to be called when an accelerated move
812 * has been scheduled. The function will create a new temporary buffer object
813 * representing the old placement, and put the sync object on both buffer
814 * objects. After that the newly created buffer object is unref'd to be
815 * destroyed when the move is complete. This will help pipeline
816 * buffer moves.
817 */
818int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
819			      struct dma_fence *fence, bool evict,
820			      struct ttm_mem_reg *new_mem);
 
821
822/**
823 * ttm_bo_pipeline_move.
824 *
825 * @bo: A pointer to a struct ttm_buffer_object.
826 * @fence: A fence object that signals when moving is complete.
827 * @evict: This is an evict move. Don't return until the buffer is idle.
828 * @new_mem: struct ttm_mem_reg indicating where to move.
829 *
830 * Function for pipelining accelerated moves. Either free the memory
831 * immediately or hang it on a temporary buffer object.
832 */
833int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
834			 struct dma_fence *fence, bool evict,
835			 struct ttm_mem_reg *new_mem);
836
837/**
838 * ttm_bo_pipeline_gutting.
839 *
840 * @bo: A pointer to a struct ttm_buffer_object.
841 *
842 * Pipelined gutting a BO of its backing store.
843 */
844int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
845
846/**
847 * ttm_io_prot
848 *
849 * @c_state: Caching state.
 
850 * @tmp: Page protection flag for a normal, cached mapping.
851 *
852 * Utility function that returns the pgprot_t that should be used for
853 * setting up a PTE with the caching model indicated by @c_state.
854 */
855pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
 
 
 
 
 
 
 
 
 
 
 
 
 
856
857extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;
 
 
 
858
 
 
 
 
 
859#endif