| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 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_hashtab.h" | 
 | 35 | #include <linux/kref.h> | 
 | 36 | #include <linux/list.h> | 
 | 37 | #include <linux/wait.h> | 
 | 38 | #include <linux/mutex.h> | 
 | 39 | #include <linux/mm.h> | 
 | 40 | #include <linux/rbtree.h> | 
 | 41 | #include <linux/bitmap.h> | 
 | 42 |  | 
 | 43 | struct ttm_bo_device; | 
 | 44 |  | 
 | 45 | struct drm_mm_node; | 
 | 46 |  | 
| Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 47 |  | 
 | 48 | /** | 
 | 49 |  * struct ttm_placement | 
 | 50 |  * | 
 | 51 |  * @fpfn:		first valid page frame number to put the object | 
 | 52 |  * @lpfn:		last valid page frame number to put the object | 
 | 53 |  * @num_placement:	number of prefered placements | 
 | 54 |  * @placement:		prefered placements | 
 | 55 |  * @num_busy_placement:	number of prefered placements when need to evict buffer | 
 | 56 |  * @busy_placement:	prefered placements when need to evict buffer | 
 | 57 |  * | 
 | 58 |  * Structure indicating the placement you request for an object. | 
 | 59 |  */ | 
 | 60 | struct ttm_placement { | 
 | 61 | 	unsigned	fpfn; | 
 | 62 | 	unsigned	lpfn; | 
 | 63 | 	unsigned	num_placement; | 
 | 64 | 	const uint32_t	*placement; | 
 | 65 | 	unsigned	num_busy_placement; | 
 | 66 | 	const uint32_t	*busy_placement; | 
 | 67 | }; | 
 | 68 |  | 
| Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 69 | /** | 
 | 70 |  * struct ttm_bus_placement | 
 | 71 |  * | 
 | 72 |  * @addr:		mapped virtual address | 
 | 73 |  * @base:		bus base address | 
 | 74 |  * @is_iomem:		is this io memory ? | 
 | 75 |  * @size:		size in byte | 
 | 76 |  * @offset:		offset from the base address | 
 | 77 |  * | 
 | 78 |  * Structure indicating the bus placement of an object. | 
 | 79 |  */ | 
 | 80 | struct ttm_bus_placement { | 
 | 81 | 	void		*addr; | 
 | 82 | 	unsigned long	base; | 
 | 83 | 	unsigned long	size; | 
 | 84 | 	unsigned long	offset; | 
 | 85 | 	bool		is_iomem; | 
 | 86 | 	bool		io_reserved; | 
 | 87 | }; | 
 | 88 |  | 
| Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 89 |  | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 90 | /** | 
 | 91 |  * struct ttm_mem_reg | 
 | 92 |  * | 
 | 93 |  * @mm_node: Memory manager node. | 
 | 94 |  * @size: Requested size of memory region. | 
 | 95 |  * @num_pages: Actual size of memory region in pages. | 
 | 96 |  * @page_alignment: Page alignment. | 
 | 97 |  * @placement: Placement flags. | 
| Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 98 |  * @bus: Placement on io bus accessible to the CPU | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 99 |  * | 
 | 100 |  * Structure indicating the placement and space resources used by a | 
 | 101 |  * buffer object. | 
 | 102 |  */ | 
 | 103 |  | 
 | 104 | struct ttm_mem_reg { | 
| Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 105 | 	void *mm_node; | 
 | 106 | 	unsigned long start; | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 107 | 	unsigned long size; | 
 | 108 | 	unsigned long num_pages; | 
 | 109 | 	uint32_t page_alignment; | 
 | 110 | 	uint32_t mem_type; | 
 | 111 | 	uint32_t placement; | 
| Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 112 | 	struct ttm_bus_placement bus; | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 113 | }; | 
 | 114 |  | 
 | 115 | /** | 
 | 116 |  * enum ttm_bo_type | 
 | 117 |  * | 
 | 118 |  * @ttm_bo_type_device:	These are 'normal' buffers that can | 
 | 119 |  * be mmapped by user space. Each of these bos occupy a slot in the | 
 | 120 |  * device address space, that can be used for normal vm operations. | 
 | 121 |  * | 
 | 122 |  * @ttm_bo_type_user: These are user-space memory areas that are made | 
 | 123 |  * available to the GPU by mapping the buffer pages into the GPU aperture | 
 | 124 |  * space. These buffers cannot be mmaped from the device address space. | 
 | 125 |  * | 
 | 126 |  * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers, | 
 | 127 |  * but they cannot be accessed from user-space. For kernel-only use. | 
 | 128 |  */ | 
 | 129 |  | 
 | 130 | enum ttm_bo_type { | 
 | 131 | 	ttm_bo_type_device, | 
 | 132 | 	ttm_bo_type_user, | 
 | 133 | 	ttm_bo_type_kernel | 
 | 134 | }; | 
 | 135 |  | 
 | 136 | struct ttm_tt; | 
 | 137 |  | 
 | 138 | /** | 
 | 139 |  * struct ttm_buffer_object | 
 | 140 |  * | 
 | 141 |  * @bdev: Pointer to the buffer object device structure. | 
 | 142 |  * @buffer_start: The virtual user-space start address of ttm_bo_type_user | 
 | 143 |  * buffers. | 
 | 144 |  * @type: The bo type. | 
 | 145 |  * @destroy: Destruction function. If NULL, kfree is used. | 
 | 146 |  * @num_pages: Actual number of pages. | 
 | 147 |  * @addr_space_offset: Address space offset. | 
 | 148 |  * @acc_size: Accounted size for this object. | 
 | 149 |  * @kref: Reference count of this buffer object. When this refcount reaches | 
 | 150 |  * zero, the object is put on the delayed delete list. | 
 | 151 |  * @list_kref: List reference count of this buffer object. This member is | 
 | 152 |  * used to avoid destruction while the buffer object is still on a list. | 
 | 153 |  * Lru lists may keep one refcount, the delayed delete list, and kref != 0 | 
 | 154 |  * keeps one refcount. When this refcount reaches zero, | 
 | 155 |  * the object is destroyed. | 
 | 156 |  * @event_queue: Queue for processes waiting on buffer object status change. | 
 | 157 |  * @lock: spinlock protecting mostly synchronization members. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 158 |  * @mem: structure describing current placement. | 
 | 159 |  * @persistant_swap_storage: Usually the swap storage is deleted for buffers | 
 | 160 |  * pinned in physical memory. If this behaviour is not desired, this member | 
 | 161 |  * holds a pointer to a persistant shmem object. | 
 | 162 |  * @ttm: TTM structure holding system pages. | 
 | 163 |  * @evicted: Whether the object was evicted without user-space knowing. | 
 | 164 |  * @cpu_writes: For synchronization. Number of cpu writers. | 
 | 165 |  * @lru: List head for the lru list. | 
 | 166 |  * @ddestroy: List head for the delayed destroy list. | 
 | 167 |  * @swap: List head for swap LRU list. | 
 | 168 |  * @val_seq: Sequence of the validation holding the @reserved lock. | 
 | 169 |  * Used to avoid starvation when many processes compete to validate the | 
 | 170 |  * buffer. This member is protected by the bo_device::lru_lock. | 
 | 171 |  * @seq_valid: The value of @val_seq is valid. This value is protected by | 
 | 172 |  * the bo_device::lru_lock. | 
 | 173 |  * @reserved: Deadlock-free lock used for synchronization state transitions. | 
 | 174 |  * @sync_obj_arg: Opaque argument to synchronization object function. | 
 | 175 |  * @sync_obj: Pointer to a synchronization object. | 
 | 176 |  * @priv_flags: Flags describing buffer object internal state. | 
 | 177 |  * @vm_rb: Rb node for the vm rb tree. | 
 | 178 |  * @vm_node: Address space manager node. | 
 | 179 |  * @offset: The current GPU offset, which can have different meanings | 
 | 180 |  * depending on the memory type. For SYSTEM type memory, it should be 0. | 
 | 181 |  * @cur_placement: Hint of current placement. | 
 | 182 |  * | 
 | 183 |  * Base class for TTM buffer object, that deals with data placement and CPU | 
 | 184 |  * mappings. GPU mappings are really up to the driver, but for simpler GPUs | 
 | 185 |  * the driver can usually use the placement offset @offset directly as the | 
 | 186 |  * GPU virtual address. For drivers implementing multiple | 
 | 187 |  * GPU memory manager contexts, the driver should manage the address space | 
 | 188 |  * in these contexts separately and use these objects to get the correct | 
 | 189 |  * placement and caching for these GPU maps. This makes it possible to use | 
 | 190 |  * these objects for even quite elaborate memory management schemes. | 
 | 191 |  * The destroy member, the API visibility of this object makes it possible | 
 | 192 |  * to derive driver specific types. | 
 | 193 |  */ | 
 | 194 |  | 
 | 195 | struct ttm_buffer_object { | 
 | 196 | 	/** | 
 | 197 | 	 * Members constant at init. | 
 | 198 | 	 */ | 
 | 199 |  | 
| Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 200 | 	struct ttm_bo_global *glob; | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 201 | 	struct ttm_bo_device *bdev; | 
 | 202 | 	unsigned long buffer_start; | 
 | 203 | 	enum ttm_bo_type type; | 
 | 204 | 	void (*destroy) (struct ttm_buffer_object *); | 
 | 205 | 	unsigned long num_pages; | 
 | 206 | 	uint64_t addr_space_offset; | 
 | 207 | 	size_t acc_size; | 
 | 208 |  | 
 | 209 | 	/** | 
 | 210 | 	* Members not needing protection. | 
 | 211 | 	*/ | 
 | 212 |  | 
 | 213 | 	struct kref kref; | 
 | 214 | 	struct kref list_kref; | 
 | 215 | 	wait_queue_head_t event_queue; | 
 | 216 | 	spinlock_t lock; | 
 | 217 |  | 
 | 218 | 	/** | 
 | 219 | 	 * Members protected by the bo::reserved lock. | 
 | 220 | 	 */ | 
 | 221 |  | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 222 | 	struct ttm_mem_reg mem; | 
 | 223 | 	struct file *persistant_swap_storage; | 
 | 224 | 	struct ttm_tt *ttm; | 
 | 225 | 	bool evicted; | 
 | 226 |  | 
 | 227 | 	/** | 
 | 228 | 	 * Members protected by the bo::reserved lock only when written to. | 
 | 229 | 	 */ | 
 | 230 |  | 
 | 231 | 	atomic_t cpu_writers; | 
 | 232 |  | 
 | 233 | 	/** | 
 | 234 | 	 * Members protected by the bdev::lru_lock. | 
 | 235 | 	 */ | 
 | 236 |  | 
 | 237 | 	struct list_head lru; | 
 | 238 | 	struct list_head ddestroy; | 
 | 239 | 	struct list_head swap; | 
 | 240 | 	uint32_t val_seq; | 
 | 241 | 	bool seq_valid; | 
 | 242 |  | 
 | 243 | 	/** | 
 | 244 | 	 * Members protected by the bdev::lru_lock | 
 | 245 | 	 * only when written to. | 
 | 246 | 	 */ | 
 | 247 |  | 
 | 248 | 	atomic_t reserved; | 
 | 249 |  | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 250 | 	/** | 
 | 251 | 	 * Members protected by the bo::lock | 
| Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 252 | 	 * In addition, setting sync_obj to anything else | 
 | 253 | 	 * than NULL requires bo::reserved to be held. This allows for | 
 | 254 | 	 * checking NULL while reserved but not holding bo::lock. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 255 | 	 */ | 
 | 256 |  | 
 | 257 | 	void *sync_obj_arg; | 
 | 258 | 	void *sync_obj; | 
 | 259 | 	unsigned long priv_flags; | 
 | 260 |  | 
 | 261 | 	/** | 
 | 262 | 	 * Members protected by the bdev::vm_lock | 
 | 263 | 	 */ | 
 | 264 |  | 
 | 265 | 	struct rb_node vm_rb; | 
 | 266 | 	struct drm_mm_node *vm_node; | 
 | 267 |  | 
 | 268 |  | 
 | 269 | 	/** | 
 | 270 | 	 * Special members that are protected by the reserve lock | 
 | 271 | 	 * and the bo::lock when written to. Can be read with | 
 | 272 | 	 * either of these locks held. | 
 | 273 | 	 */ | 
 | 274 |  | 
 | 275 | 	unsigned long offset; | 
 | 276 | 	uint32_t cur_placement; | 
 | 277 | }; | 
 | 278 |  | 
 | 279 | /** | 
 | 280 |  * struct ttm_bo_kmap_obj | 
 | 281 |  * | 
 | 282 |  * @virtual: The current kernel virtual address. | 
 | 283 |  * @page: The page when kmap'ing a single page. | 
 | 284 |  * @bo_kmap_type: Type of bo_kmap. | 
 | 285 |  * | 
 | 286 |  * Object describing a kernel mapping. Since a TTM bo may be located | 
 | 287 |  * in various memory types with various caching policies, the | 
 | 288 |  * mapping can either be an ioremap, a vmap, a kmap or part of a | 
 | 289 |  * premapped region. | 
 | 290 |  */ | 
 | 291 |  | 
| Pekka Paalanen | a0724fc | 2009-08-17 01:18:38 +0300 | [diff] [blame] | 292 | #define TTM_BO_MAP_IOMEM_MASK 0x80 | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 293 | struct ttm_bo_kmap_obj { | 
 | 294 | 	void *virtual; | 
 | 295 | 	struct page *page; | 
 | 296 | 	enum { | 
| Pekka Paalanen | a0724fc | 2009-08-17 01:18:38 +0300 | [diff] [blame] | 297 | 		ttm_bo_map_iomap        = 1 | TTM_BO_MAP_IOMEM_MASK, | 
 | 298 | 		ttm_bo_map_vmap         = 2, | 
 | 299 | 		ttm_bo_map_kmap         = 3, | 
 | 300 | 		ttm_bo_map_premapped    = 4 | TTM_BO_MAP_IOMEM_MASK, | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 301 | 	} bo_kmap_type; | 
| Jerome Glisse | 82c5da6 | 2010-04-09 14:39:23 +0200 | [diff] [blame] | 302 | 	struct ttm_buffer_object *bo; | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 303 | }; | 
 | 304 |  | 
 | 305 | /** | 
 | 306 |  * ttm_bo_reference - reference a struct ttm_buffer_object | 
 | 307 |  * | 
 | 308 |  * @bo: The buffer object. | 
 | 309 |  * | 
 | 310 |  * Returns a refcounted pointer to a buffer object. | 
 | 311 |  */ | 
 | 312 |  | 
 | 313 | static inline struct ttm_buffer_object * | 
 | 314 | ttm_bo_reference(struct ttm_buffer_object *bo) | 
 | 315 | { | 
 | 316 | 	kref_get(&bo->kref); | 
 | 317 | 	return bo; | 
 | 318 | } | 
 | 319 |  | 
 | 320 | /** | 
 | 321 |  * ttm_bo_wait - wait for buffer idle. | 
 | 322 |  * | 
 | 323 |  * @bo:  The buffer object. | 
 | 324 |  * @interruptible:  Use interruptible wait. | 
 | 325 |  * @no_wait:  Return immediately if buffer is busy. | 
 | 326 |  * | 
 | 327 |  * This function must be called with the bo::mutex held, and makes | 
 | 328 |  * sure any previous rendering to the buffer is completed. | 
 | 329 |  * Note: It might be necessary to block validations before the | 
 | 330 |  * wait by reserving the buffer. | 
 | 331 |  * Returns -EBUSY if no_wait is true and the buffer is busy. | 
| Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 332 |  * Returns -ERESTARTSYS if interrupted by a signal. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 333 |  */ | 
 | 334 | extern int ttm_bo_wait(struct ttm_buffer_object *bo, bool lazy, | 
 | 335 | 		       bool interruptible, bool no_wait); | 
 | 336 | /** | 
| Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 337 |  * ttm_bo_validate | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 338 |  * | 
 | 339 |  * @bo: The buffer object. | 
| Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 340 |  * @placement: Proposed placement for the buffer object. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 341 |  * @interruptible: Sleep interruptible if sleeping. | 
| Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 342 |  * @no_wait_reserve: Return immediately if other buffers are busy. | 
 | 343 |  * @no_wait_gpu: Return immediately if the GPU is busy. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 344 |  * | 
 | 345 |  * Changes placement and caching policy of the buffer object | 
| Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 346 |  * according proposed placement. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 347 |  * Returns | 
| Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 348 |  * -EINVAL on invalid proposed placement. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 349 |  * -ENOMEM on out-of-memory condition. | 
 | 350 |  * -EBUSY if no_wait is true and buffer busy. | 
| Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 351 |  * -ERESTARTSYS if interrupted by a signal. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 352 |  */ | 
| Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 353 | extern int ttm_bo_validate(struct ttm_buffer_object *bo, | 
 | 354 | 				struct ttm_placement *placement, | 
| Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 355 | 				bool interruptible, bool no_wait_reserve, | 
 | 356 | 				bool no_wait_gpu); | 
| Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 357 |  | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 358 | /** | 
 | 359 |  * ttm_bo_unref | 
 | 360 |  * | 
 | 361 |  * @bo: The buffer object. | 
 | 362 |  * | 
 | 363 |  * Unreference and clear a pointer to a buffer object. | 
 | 364 |  */ | 
 | 365 | extern void ttm_bo_unref(struct ttm_buffer_object **bo); | 
 | 366 |  | 
 | 367 | /** | 
| Matthew Garrett | 7c5ee53 | 2010-04-26 16:00:09 -0400 | [diff] [blame] | 368 |  * ttm_bo_lock_delayed_workqueue | 
 | 369 |  * | 
 | 370 |  * Prevent the delayed workqueue from running. | 
 | 371 |  * Returns | 
 | 372 |  * True if the workqueue was queued at the time | 
 | 373 |  */ | 
 | 374 | extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev); | 
 | 375 |  | 
 | 376 | /** | 
 | 377 |  * ttm_bo_unlock_delayed_workqueue | 
 | 378 |  * | 
 | 379 |  * Allows the delayed workqueue to run. | 
 | 380 |  */ | 
 | 381 | extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, | 
 | 382 | 					    int resched); | 
 | 383 |  | 
 | 384 | /** | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 385 |  * ttm_bo_synccpu_write_grab | 
 | 386 |  * | 
 | 387 |  * @bo: The buffer object: | 
 | 388 |  * @no_wait: Return immediately if buffer is busy. | 
 | 389 |  * | 
 | 390 |  * Synchronizes a buffer object for CPU RW access. This means | 
 | 391 |  * blocking command submission that affects the buffer and | 
 | 392 |  * waiting for buffer idle. This lock is recursive. | 
 | 393 |  * Returns | 
 | 394 |  * -EBUSY if the buffer is busy and no_wait is true. | 
| Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 395 |  * -ERESTARTSYS if interrupted by a signal. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 396 |  */ | 
 | 397 |  | 
 | 398 | extern int | 
 | 399 | ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait); | 
 | 400 | /** | 
 | 401 |  * ttm_bo_synccpu_write_release: | 
 | 402 |  * | 
 | 403 |  * @bo : The buffer object. | 
 | 404 |  * | 
 | 405 |  * Releases a synccpu lock. | 
 | 406 |  */ | 
 | 407 | extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo); | 
 | 408 |  | 
 | 409 | /** | 
| Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 410 |  * ttm_bo_init | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 411 |  * | 
 | 412 |  * @bdev: Pointer to a ttm_bo_device struct. | 
 | 413 |  * @bo: Pointer to a ttm_buffer_object to be initialized. | 
 | 414 |  * @size: Requested size of buffer object. | 
 | 415 |  * @type: Requested type of buffer object. | 
 | 416 |  * @flags: Initial placement flags. | 
 | 417 |  * @page_alignment: Data alignment in pages. | 
 | 418 |  * @buffer_start: Virtual address of user space data backing a | 
 | 419 |  * user buffer object. | 
 | 420 |  * @interruptible: If needing to sleep to wait for GPU resources, | 
 | 421 |  * sleep interruptible. | 
 | 422 |  * @persistant_swap_storage: Usually the swap storage is deleted for buffers | 
 | 423 |  * pinned in physical memory. If this behaviour is not desired, this member | 
 | 424 |  * holds a pointer to a persistant shmem object. Typically, this would | 
 | 425 |  * point to the shmem object backing a GEM object if TTM is used to back a | 
 | 426 |  * GEM user interface. | 
 | 427 |  * @acc_size: Accounted size for this object. | 
 | 428 |  * @destroy: Destroy function. Use NULL for kfree(). | 
 | 429 |  * | 
 | 430 |  * This function initializes a pre-allocated struct ttm_buffer_object. | 
 | 431 |  * As this object may be part of a larger structure, this function, | 
 | 432 |  * together with the @destroy function, | 
 | 433 |  * enables driver-specific objects derived from a ttm_buffer_object. | 
 | 434 |  * On successful return, the object kref and list_kref are set to 1. | 
 | 435 |  * Returns | 
 | 436 |  * -ENOMEM: Out of memory. | 
 | 437 |  * -EINVAL: Invalid placement flags. | 
| Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 438 |  * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 439 |  */ | 
 | 440 |  | 
| Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 441 | extern int ttm_bo_init(struct ttm_bo_device *bdev, | 
 | 442 | 			struct ttm_buffer_object *bo, | 
 | 443 | 			unsigned long size, | 
 | 444 | 			enum ttm_bo_type type, | 
 | 445 | 			struct ttm_placement *placement, | 
 | 446 | 			uint32_t page_alignment, | 
 | 447 | 			unsigned long buffer_start, | 
 | 448 | 			bool interrubtible, | 
 | 449 | 			struct file *persistant_swap_storage, | 
 | 450 | 			size_t acc_size, | 
 | 451 | 			void (*destroy) (struct ttm_buffer_object *)); | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 452 | /** | 
 | 453 |  * ttm_bo_synccpu_object_init | 
 | 454 |  * | 
 | 455 |  * @bdev: Pointer to a ttm_bo_device struct. | 
 | 456 |  * @bo: Pointer to a ttm_buffer_object to be initialized. | 
 | 457 |  * @size: Requested size of buffer object. | 
 | 458 |  * @type: Requested type of buffer object. | 
 | 459 |  * @flags: Initial placement flags. | 
 | 460 |  * @page_alignment: Data alignment in pages. | 
 | 461 |  * @buffer_start: Virtual address of user space data backing a | 
 | 462 |  * user buffer object. | 
 | 463 |  * @interruptible: If needing to sleep while waiting for GPU resources, | 
 | 464 |  * sleep interruptible. | 
 | 465 |  * @persistant_swap_storage: Usually the swap storage is deleted for buffers | 
 | 466 |  * pinned in physical memory. If this behaviour is not desired, this member | 
 | 467 |  * holds a pointer to a persistant shmem object. Typically, this would | 
 | 468 |  * point to the shmem object backing a GEM object if TTM is used to back a | 
 | 469 |  * GEM user interface. | 
 | 470 |  * @p_bo: On successful completion *p_bo points to the created object. | 
 | 471 |  * | 
| Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 472 |  * This function allocates a ttm_buffer_object, and then calls ttm_bo_init | 
 | 473 |  * on that object. The destroy function is set to kfree(). | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 474 |  * Returns | 
 | 475 |  * -ENOMEM: Out of memory. | 
 | 476 |  * -EINVAL: Invalid placement flags. | 
| Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 477 |  * -ERESTARTSYS: Interrupted by signal while waiting for resources. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 478 |  */ | 
 | 479 |  | 
| Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 480 | extern int ttm_bo_create(struct ttm_bo_device *bdev, | 
 | 481 | 				unsigned long size, | 
 | 482 | 				enum ttm_bo_type type, | 
 | 483 | 				struct ttm_placement *placement, | 
 | 484 | 				uint32_t page_alignment, | 
 | 485 | 				unsigned long buffer_start, | 
 | 486 | 				bool interruptible, | 
 | 487 | 				struct file *persistant_swap_storage, | 
 | 488 | 				struct ttm_buffer_object **p_bo); | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 489 |  | 
 | 490 | /** | 
 | 491 |  * ttm_bo_check_placement | 
 | 492 |  * | 
| Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 493 |  * @bo:		the buffer object. | 
 | 494 |  * @placement:	placements | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 495 |  * | 
 | 496 |  * Performs minimal validity checking on an intended change of | 
 | 497 |  * placement flags. | 
 | 498 |  * Returns | 
 | 499 |  * -EINVAL: Intended change is invalid or not allowed. | 
 | 500 |  */ | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 501 | extern int ttm_bo_check_placement(struct ttm_buffer_object *bo, | 
| Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 502 | 					struct ttm_placement *placement); | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 503 |  | 
 | 504 | /** | 
 | 505 |  * ttm_bo_init_mm | 
 | 506 |  * | 
 | 507 |  * @bdev: Pointer to a ttm_bo_device struct. | 
 | 508 |  * @mem_type: The memory type. | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 509 |  * @p_size: size managed area in pages. | 
 | 510 |  * | 
 | 511 |  * Initialize a manager for a given memory type. | 
 | 512 |  * Note: if part of driver firstopen, it must be protected from a | 
 | 513 |  * potentially racing lastclose. | 
 | 514 |  * Returns: | 
 | 515 |  * -EINVAL: invalid size or memory type. | 
 | 516 |  * -ENOMEM: Not enough memory. | 
 | 517 |  * May also return driver-specified errors. | 
 | 518 |  */ | 
 | 519 |  | 
 | 520 | extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, | 
| Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 521 | 				unsigned long p_size); | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 522 | /** | 
 | 523 |  * ttm_bo_clean_mm | 
 | 524 |  * | 
 | 525 |  * @bdev: Pointer to a ttm_bo_device struct. | 
 | 526 |  * @mem_type: The memory type. | 
 | 527 |  * | 
 | 528 |  * Take down a manager for a given memory type after first walking | 
 | 529 |  * the LRU list to evict any buffers left alive. | 
 | 530 |  * | 
 | 531 |  * Normally, this function is part of lastclose() or unload(), and at that | 
 | 532 |  * point there shouldn't be any buffers left created by user-space, since | 
 | 533 |  * there should've been removed by the file descriptor release() method. | 
 | 534 |  * However, before this function is run, make sure to signal all sync objects, | 
 | 535 |  * and verify that the delayed delete queue is empty. The driver must also | 
 | 536 |  * make sure that there are no NO_EVICT buffers present in this memory type | 
 | 537 |  * when the call is made. | 
 | 538 |  * | 
 | 539 |  * If this function is part of a VT switch, the caller must make sure that | 
 | 540 |  * there are no appications currently validating buffers before this | 
 | 541 |  * function is called. The caller can do that by first taking the | 
 | 542 |  * struct ttm_bo_device::ttm_lock in write mode. | 
 | 543 |  * | 
 | 544 |  * Returns: | 
 | 545 |  * -EINVAL: invalid or uninitialized memory type. | 
 | 546 |  * -EBUSY: There are still buffers left in this memory type. | 
 | 547 |  */ | 
 | 548 |  | 
 | 549 | extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type); | 
 | 550 |  | 
 | 551 | /** | 
 | 552 |  * ttm_bo_evict_mm | 
 | 553 |  * | 
 | 554 |  * @bdev: Pointer to a ttm_bo_device struct. | 
 | 555 |  * @mem_type: The memory type. | 
 | 556 |  * | 
 | 557 |  * Evicts all buffers on the lru list of the memory type. | 
 | 558 |  * This is normally part of a VT switch or an | 
 | 559 |  * out-of-memory-space-due-to-fragmentation handler. | 
 | 560 |  * The caller must make sure that there are no other processes | 
 | 561 |  * currently validating buffers, and can do that by taking the | 
 | 562 |  * struct ttm_bo_device::ttm_lock in write mode. | 
 | 563 |  * | 
 | 564 |  * Returns: | 
 | 565 |  * -EINVAL: Invalid or uninitialized memory type. | 
| Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 566 |  * -ERESTARTSYS: The call was interrupted by a signal while waiting to | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 567 |  * evict a buffer. | 
 | 568 |  */ | 
 | 569 |  | 
 | 570 | extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type); | 
 | 571 |  | 
 | 572 | /** | 
 | 573 |  * ttm_kmap_obj_virtual | 
 | 574 |  * | 
 | 575 |  * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap. | 
 | 576 |  * @is_iomem: Pointer to an integer that on return indicates 1 if the | 
 | 577 |  * virtual map is io memory, 0 if normal memory. | 
 | 578 |  * | 
 | 579 |  * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap. | 
 | 580 |  * If *is_iomem is 1 on return, the virtual address points to an io memory area, | 
 | 581 |  * that should strictly be accessed by the iowriteXX() and similar functions. | 
 | 582 |  */ | 
 | 583 |  | 
 | 584 | static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map, | 
 | 585 | 					 bool *is_iomem) | 
 | 586 | { | 
| Pekka Paalanen | a0724fc | 2009-08-17 01:18:38 +0300 | [diff] [blame] | 587 | 	*is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK); | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 588 | 	return map->virtual; | 
 | 589 | } | 
 | 590 |  | 
 | 591 | /** | 
 | 592 |  * ttm_bo_kmap | 
 | 593 |  * | 
 | 594 |  * @bo: The buffer object. | 
 | 595 |  * @start_page: The first page to map. | 
 | 596 |  * @num_pages: Number of pages to map. | 
 | 597 |  * @map: pointer to a struct ttm_bo_kmap_obj representing the map. | 
 | 598 |  * | 
 | 599 |  * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the | 
 | 600 |  * data in the buffer object. The ttm_kmap_obj_virtual function can then be | 
 | 601 |  * used to obtain a virtual address to the data. | 
 | 602 |  * | 
 | 603 |  * Returns | 
 | 604 |  * -ENOMEM: Out of memory. | 
 | 605 |  * -EINVAL: Invalid range. | 
 | 606 |  */ | 
 | 607 |  | 
 | 608 | extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page, | 
 | 609 | 		       unsigned long num_pages, struct ttm_bo_kmap_obj *map); | 
 | 610 |  | 
 | 611 | /** | 
 | 612 |  * ttm_bo_kunmap | 
 | 613 |  * | 
 | 614 |  * @map: Object describing the map to unmap. | 
 | 615 |  * | 
 | 616 |  * Unmaps a kernel map set up by ttm_bo_kmap. | 
 | 617 |  */ | 
 | 618 |  | 
 | 619 | extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map); | 
 | 620 |  | 
 | 621 | #if 0 | 
 | 622 | #endif | 
 | 623 |  | 
 | 624 | /** | 
 | 625 |  * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object. | 
 | 626 |  * | 
 | 627 |  * @vma:       vma as input from the fbdev mmap method. | 
 | 628 |  * @bo:        The bo backing the address space. The address space will | 
 | 629 |  * have the same size as the bo, and start at offset 0. | 
 | 630 |  * | 
 | 631 |  * This function is intended to be called by the fbdev mmap method | 
 | 632 |  * if the fbdev address space is to be backed by a bo. | 
 | 633 |  */ | 
 | 634 |  | 
 | 635 | extern int ttm_fbdev_mmap(struct vm_area_struct *vma, | 
 | 636 | 			  struct ttm_buffer_object *bo); | 
 | 637 |  | 
 | 638 | /** | 
 | 639 |  * ttm_bo_mmap - mmap out of the ttm device address space. | 
 | 640 |  * | 
 | 641 |  * @filp:      filp as input from the mmap method. | 
 | 642 |  * @vma:       vma as input from the mmap method. | 
 | 643 |  * @bdev:      Pointer to the ttm_bo_device with the address space manager. | 
 | 644 |  * | 
 | 645 |  * This function is intended to be called by the device mmap method. | 
 | 646 |  * if the device address space is to be backed by the bo manager. | 
 | 647 |  */ | 
 | 648 |  | 
 | 649 | extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma, | 
 | 650 | 		       struct ttm_bo_device *bdev); | 
 | 651 |  | 
 | 652 | /** | 
 | 653 |  * ttm_bo_io | 
 | 654 |  * | 
 | 655 |  * @bdev:      Pointer to the struct ttm_bo_device. | 
 | 656 |  * @filp:      Pointer to the struct file attempting to read / write. | 
 | 657 |  * @wbuf:      User-space pointer to address of buffer to write. NULL on read. | 
 | 658 |  * @rbuf:      User-space pointer to address of buffer to read into. | 
 | 659 |  * Null on write. | 
 | 660 |  * @count:     Number of bytes to read / write. | 
 | 661 |  * @f_pos:     Pointer to current file position. | 
 | 662 |  * @write:     1 for read, 0 for write. | 
 | 663 |  * | 
 | 664 |  * This function implements read / write into ttm buffer objects, and is | 
 | 665 |  * intended to | 
 | 666 |  * be called from the fops::read and fops::write method. | 
 | 667 |  * Returns: | 
 | 668 |  * See man (2) write, man(2) read. In particular, | 
| Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 669 |  * the function may return -ERESTARTSYS if | 
| Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 670 |  * interrupted by a signal. | 
 | 671 |  */ | 
 | 672 |  | 
 | 673 | extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp, | 
 | 674 | 			 const char __user *wbuf, char __user *rbuf, | 
 | 675 | 			 size_t count, loff_t *f_pos, bool write); | 
 | 676 |  | 
 | 677 | extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev); | 
 | 678 |  | 
 | 679 | #endif |