ttm_bo_driver.h 34.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/**************************************************************************
 *
 * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/
/*
 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
 */
#ifndef _TTM_BO_DRIVER_H_
#define _TTM_BO_DRIVER_H_

33 34
#include <drm/drm_mm.h>
#include <drm/drm_global.h>
35
#include <drm/drm_vma_manager.h>
36 37 38
#include <linux/workqueue.h>
#include <linux/fs.h>
#include <linux/spinlock.h>
39
#include <linux/reservation.h>
40

41 42 43 44 45
#include "ttm_bo_api.h"
#include "ttm_memory.h"
#include "ttm_module.h"
#include "ttm_placement.h"

46
#define TTM_MAX_BO_PRIORITY	4U
47

48 49 50 51
struct ttm_backend_func {
	/**
	 * struct ttm_backend_func member bind
	 *
52
	 * @ttm: Pointer to a struct ttm_tt.
53 54 55 56 57
	 * @bo_mem: Pointer to a struct ttm_mem_reg describing the
	 * memory type and location for binding.
	 *
	 * Bind the backend pages into the aperture in the location
	 * indicated by @bo_mem. This function should be able to handle
58
	 * differences between aperture and system page sizes.
59
	 */
60
	int (*bind) (struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
61 62 63 64

	/**
	 * struct ttm_backend_func member unbind
	 *
65
	 * @ttm: Pointer to a struct ttm_tt.
66 67
	 *
	 * Unbind previously bound backend pages. This function should be
68
	 * able to handle differences between aperture and system page sizes.
69
	 */
70
	int (*unbind) (struct ttm_tt *ttm);
71 72 73 74

	/**
	 * struct ttm_backend_func member destroy
	 *
75
	 * @ttm: Pointer to a struct ttm_tt.
76
	 *
77 78
	 * Destroy the backend. This will be call back from ttm_tt_destroy so
	 * don't call ttm_tt_destroy from the callback or infinite loop.
79
	 */
80
	void (*destroy) (struct ttm_tt *ttm);
81 82 83 84
};

#define TTM_PAGE_FLAG_WRITE           (1 << 3)
#define TTM_PAGE_FLAG_SWAPPED         (1 << 4)
J
Jan Engelhardt 已提交
85
#define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)
86
#define TTM_PAGE_FLAG_ZERO_ALLOC      (1 << 6)
D
Dave Airlie 已提交
87
#define TTM_PAGE_FLAG_DMA32           (1 << 7)
88
#define TTM_PAGE_FLAG_SG              (1 << 8)
89 90 91 92 93 94 95 96 97 98

enum ttm_caching_state {
	tt_uncached,
	tt_wc,
	tt_cached
};

/**
 * struct ttm_tt
 *
99 100 101
 * @bdev: Pointer to a struct ttm_bo_device.
 * @func: Pointer to a struct ttm_backend_func that describes
 * the backend methods.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
 * @dummy_read_page: Page to map where the ttm_tt page array contains a NULL
 * pointer.
 * @pages: Array of pages backing the data.
 * @num_pages: Number of pages in the page array.
 * @bdev: Pointer to the current struct ttm_bo_device.
 * @be: Pointer to the ttm backend.
 * @swap_storage: Pointer to shmem struct file for swap storage.
 * @caching_state: The current caching state of the pages.
 * @state: The current binding state of the pages.
 *
 * This is a structure holding the pages, caching- and aperture binding
 * status for a buffer object that isn't backed by fixed (VRAM / AGP)
 * memory.
 */

struct ttm_tt {
118 119
	struct ttm_bo_device *bdev;
	struct ttm_backend_func *func;
120 121 122 123
	struct page *dummy_read_page;
	struct page **pages;
	uint32_t page_flags;
	unsigned long num_pages;
124
	struct sg_table *sg; /* for SG objects via dma-buf */
125
	struct ttm_bo_global *glob;
126 127 128 129 130 131 132
	struct file *swap_storage;
	enum ttm_caching_state caching_state;
	enum {
		tt_bound,
		tt_unbound,
		tt_unpopulated,
	} state;
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
};

/**
 * struct ttm_dma_tt
 *
 * @ttm: Base ttm_tt struct.
 * @dma_address: The DMA (bus) addresses of the pages
 * @pages_list: used by some page allocation backend
 *
 * This is a structure holding the pages, caching- and aperture binding
 * status for a buffer object that isn't backed by fixed (VRAM / AGP)
 * memory.
 */
struct ttm_dma_tt {
	struct ttm_tt ttm;
148
	dma_addr_t *dma_address;
149
	struct list_head pages_list;
150 151 152 153 154 155
};

#define TTM_MEMTYPE_FLAG_FIXED         (1 << 0)	/* Fixed (on-card) PCI memory */
#define TTM_MEMTYPE_FLAG_MAPPABLE      (1 << 1)	/* Memory mappable */
#define TTM_MEMTYPE_FLAG_CMA           (1 << 3)	/* Can't map aperture */

156 157 158
struct ttm_mem_type_manager;

struct ttm_mem_type_manager_func {
T
Thomas Hellstrom 已提交
159 160 161 162 163 164 165 166 167 168 169
	/**
	 * struct ttm_mem_type_manager member init
	 *
	 * @man: Pointer to a memory type manager.
	 * @p_size: Implementation dependent, but typically the size of the
	 * range to be managed in pages.
	 *
	 * Called to initialize a private range manager. The function is
	 * expected to initialize the man::priv member.
	 * Returns 0 on success, negative error code on failure.
	 */
170
	int  (*init)(struct ttm_mem_type_manager *man, unsigned long p_size);
T
Thomas Hellstrom 已提交
171 172 173 174 175 176 177 178 179

	/**
	 * struct ttm_mem_type_manager member takedown
	 *
	 * @man: Pointer to a memory type manager.
	 *
	 * Called to undo the setup done in init. All allocated resources
	 * should be freed.
	 */
180
	int  (*takedown)(struct ttm_mem_type_manager *man);
T
Thomas Hellstrom 已提交
181 182 183 184 185 186 187

	/**
	 * struct ttm_mem_type_manager member get_node
	 *
	 * @man: Pointer to a memory type manager.
	 * @bo: Pointer to the buffer object we're allocating space for.
	 * @placement: Placement details.
188
	 * @flags: Additional placement flags.
T
Thomas Hellstrom 已提交
189 190 191 192 193 194 195 196
	 * @mem: Pointer to a struct ttm_mem_reg to be filled in.
	 *
	 * This function should allocate space in the memory type managed
	 * by @man. Placement details if
	 * applicable are given by @placement. If successful,
	 * @mem::mm_node should be set to a non-null value, and
	 * @mem::start should be set to a value identifying the beginning
	 * of the range allocated, and the function should return zero.
L
Lucas De Marchi 已提交
197
	 * If the memory region accommodate the buffer object, @mem::mm_node
T
Thomas Hellstrom 已提交
198
	 * should be set to NULL, and the function should return 0.
L
Lucas De Marchi 已提交
199
	 * If a system error occurred, preventing the request to be fulfilled,
T
Thomas Hellstrom 已提交
200 201 202 203 204 205 206 207 208 209
	 * the function should return a negative error code.
	 *
	 * Note that @mem::mm_node will only be dereferenced by
	 * struct ttm_mem_type_manager functions and optionally by the driver,
	 * which has knowledge of the underlying type.
	 *
	 * This function may not be called from within atomic context, so
	 * an implementation can and must use either a mutex or a spinlock to
	 * protect any data structures managing the space.
	 */
210 211
	int  (*get_node)(struct ttm_mem_type_manager *man,
			 struct ttm_buffer_object *bo,
212
			 const struct ttm_place *place,
213
			 struct ttm_mem_reg *mem);
T
Thomas Hellstrom 已提交
214 215 216 217 218 219 220 221 222 223 224

	/**
	 * struct ttm_mem_type_manager member put_node
	 *
	 * @man: Pointer to a memory type manager.
	 * @mem: Pointer to a struct ttm_mem_reg to be filled in.
	 *
	 * This function frees memory type resources previously allocated
	 * and that are identified by @mem::mm_node and @mem::start. May not
	 * be called from within atomic context.
	 */
225 226
	void (*put_node)(struct ttm_mem_type_manager *man,
			 struct ttm_mem_reg *mem);
T
Thomas Hellstrom 已提交
227 228 229 230 231

	/**
	 * struct ttm_mem_type_manager member debug
	 *
	 * @man: Pointer to a memory type manager.
232
	 * @printer: Prefix to be used in printout to identify the caller.
T
Thomas Hellstrom 已提交
233 234 235 236 237
	 *
	 * This function is called to print out the state of the memory
	 * type manager to aid debugging of out-of-memory conditions.
	 * It may not be called from within atomic context.
	 */
238 239
	void (*debug)(struct ttm_mem_type_manager *man,
		      struct drm_printer *printer);
240 241
};

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
/**
 * struct ttm_mem_type_manager
 *
 * @has_type: The memory type has been initialized.
 * @use_type: The memory type is enabled.
 * @flags: TTM_MEMTYPE_XX flags identifying the traits of the memory
 * managed by this memory type.
 * @gpu_offset: If used, the GPU offset of the first managed page of
 * fixed memory or the first managed location in an aperture.
 * @size: Size of the managed region.
 * @available_caching: A mask of available caching types, TTM_PL_FLAG_XX,
 * as defined in ttm_placement_common.h
 * @default_caching: The default caching policy used for a buffer object
 * placed in this memory type if the user doesn't provide one.
 * @func: structure pointer implementing the range manager. See above
 * @priv: Driver private closure for @func.
 * @io_reserve_mutex: Mutex optionally protecting shared io_reserve structures
 * @use_io_reserve_lru: Use an lru list to try to unreserve io_mem_regions
 * reserved by the TTM vm system.
 * @io_reserve_lru: Optional lru list for unreserving io mem regions.
 * @io_reserve_fastpath: Only use bdev::driver::io_mem_reserve to obtain
263
 * @move_lock: lock for move fence
264 265
 * static information. bdev::driver::io_mem_free is never used.
 * @lru: The lru list for this memory type.
266
 * @move: The fence of the last pipelined move operation.
267 268 269 270 271 272 273
 *
 * This structure is used to identify and manage memory types for a device.
 * It's set up by the ttm_bo_driver::init_mem_type method.
 */



274
struct ttm_mem_type_manager {
275
	struct ttm_bo_device *bdev;
276 277 278 279 280 281 282 283

	/*
	 * No protection. Constant from start.
	 */

	bool has_type;
	bool use_type;
	uint32_t flags;
284
	uint64_t gpu_offset; /* GPU address space is independent of CPU word size */
285 286 287
	uint64_t size;
	uint32_t available_caching;
	uint32_t default_caching;
T
Thomas Hellstrom 已提交
288 289
	const struct ttm_mem_type_manager_func *func;
	void *priv;
290 291 292
	struct mutex io_reserve_mutex;
	bool use_io_reserve_lru;
	bool io_reserve_fastpath;
293
	spinlock_t move_lock;
294 295 296 297 298 299

	/*
	 * Protected by @io_reserve_mutex:
	 */

	struct list_head io_reserve_lru;
300 301

	/*
T
Thomas Hellstrom 已提交
302
	 * Protected by the global->lru_lock.
303
	 */
T
Thomas Hellstrom 已提交
304

305
	struct list_head lru[TTM_MAX_BO_PRIORITY];
306 307 308 309

	/*
	 * Protected by @move_lock.
	 */
310
	struct dma_fence *move;
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
};

/**
 * struct ttm_bo_driver
 *
 * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
 * @invalidate_caches: Callback to invalidate read caches when a buffer object
 * has been evicted.
 * @init_mem_type: Callback to initialize a struct ttm_mem_type_manager
 * structure.
 * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
 * @move: Callback for a driver to hook in accelerated functions to
 * move a buffer.
 * If set to NULL, a potentially slow memcpy() move is used.
 */

struct ttm_bo_driver {
	/**
329
	 * ttm_tt_create
330
	 *
331 332 333 334
	 * @bdev: pointer to a struct ttm_bo_device:
	 * @size: Size of the data needed backing.
	 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
	 * @dummy_read_page: See struct ttm_bo_device.
335
	 *
336 337 338 339
	 * Create a struct ttm_tt to back data with system memory pages.
	 * No pages are actually allocated.
	 * Returns:
	 * NULL: Out of memory.
340
	 */
341 342 343 344
	struct ttm_tt *(*ttm_tt_create)(struct ttm_bo_device *bdev,
					unsigned long size,
					uint32_t page_flags,
					struct page *dummy_read_page);
345

346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
	/**
	 * ttm_tt_populate
	 *
	 * @ttm: The struct ttm_tt to contain the backing pages.
	 *
	 * Allocate all backing pages
	 * Returns:
	 * -ENOMEM: Out of memory.
	 */
	int (*ttm_tt_populate)(struct ttm_tt *ttm);

	/**
	 * ttm_tt_unpopulate
	 *
	 * @ttm: The struct ttm_tt to contain the backing pages.
	 *
	 * Free all backing page
	 */
	void (*ttm_tt_unpopulate)(struct ttm_tt *ttm);

366 367 368 369 370 371 372 373 374 375 376 377
	/**
	 * struct ttm_bo_driver member invalidate_caches
	 *
	 * @bdev: the buffer object device.
	 * @flags: new placement of the rebound buffer object.
	 *
	 * A previosly evicted buffer has been rebound in a
	 * potentially new location. Tell the driver that it might
	 * consider invalidating read (texture) caches on the next command
	 * submission as a consequence.
	 */

378 379 380
	int (*invalidate_caches)(struct ttm_bo_device *bdev, uint32_t flags);
	int (*init_mem_type)(struct ttm_bo_device *bdev, uint32_t type,
			     struct ttm_mem_type_manager *man);
381 382 383 384 385 386 387 388 389 390 391 392

	/**
	 * struct ttm_bo_driver member eviction_valuable
	 *
	 * @bo: the buffer object to be evicted
	 * @place: placement we need room for
	 *
	 * Check with the driver if it is valuable to evict a BO to make room
	 * for a certain placement.
	 */
	bool (*eviction_valuable)(struct ttm_buffer_object *bo,
				  const struct ttm_place *place);
393 394 395 396 397 398 399 400 401 402
	/**
	 * struct ttm_bo_driver member evict_flags:
	 *
	 * @bo: the buffer object to be evicted
	 *
	 * Return the bo flags for a buffer which is not mapped to the hardware.
	 * These will be placed in proposed_flags so that when the move is
	 * finished, they'll end up in bo->mem.flags
	 */

403 404 405
	void (*evict_flags)(struct ttm_buffer_object *bo,
			    struct ttm_placement *placement);

406 407 408 409 410 411 412 413 414 415 416 417 418
	/**
	 * struct ttm_bo_driver member move:
	 *
	 * @bo: the buffer to move
	 * @evict: whether this motion is evicting the buffer from
	 * the graphics address space
	 * @interruptible: Use interruptible sleeps if possible when sleeping.
	 * @no_wait: whether this should give up and return -EBUSY
	 * if this move would require sleeping
	 * @new_mem: the new memory region receiving the buffer
	 *
	 * Move a buffer between two memory regions.
	 */
419 420 421
	int (*move)(struct ttm_buffer_object *bo, bool evict,
		    bool interruptible, bool no_wait_gpu,
		    struct ttm_mem_reg *new_mem);
422 423 424 425 426 427 428 429 430 431 432 433 434

	/**
	 * struct ttm_bo_driver_member verify_access
	 *
	 * @bo: Pointer to a buffer object.
	 * @filp: Pointer to a struct file trying to access the object.
	 *
	 * Called from the map / write / read methods to verify that the
	 * caller is permitted to access the buffer object.
	 * This member may be set to NULL, which will refuse this kind of
	 * access for all buffer objects.
	 * This function should return 0 if access is granted, -EPERM otherwise.
	 */
435 436
	int (*verify_access)(struct ttm_buffer_object *bo,
			     struct file *filp);
437

438 439 440 441 442 443 444
	/**
	 * Hook to notify driver about a driver move so it
	 * can do tiling things and book-keeping.
	 *
	 * @evict: whether this move is evicting the buffer from the graphics
	 * address space
	 */
445
	void (*move_notify)(struct ttm_buffer_object *bo,
446
			    bool evict,
447 448 449
			    struct ttm_mem_reg *new_mem);
	/* notify the driver we are taking a fault on this BO
	 * and have reserved it */
450
	int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
451 452 453 454

	/**
	 * notify the driver that we're about to swap out this bo
	 */
455
	void (*swap_notify)(struct ttm_buffer_object *bo);
456 457 458 459 460 461 462

	/**
	 * Driver callback on when mapping io memory (for bo_move_memcpy
	 * for instance). TTM will take care to call io_mem_free whenever
	 * the mapping is not use anymore. io_mem_reserve & io_mem_free
	 * are balanced.
	 */
463 464 465 466
	int (*io_mem_reserve)(struct ttm_bo_device *bdev,
			      struct ttm_mem_reg *mem);
	void (*io_mem_free)(struct ttm_bo_device *bdev,
			    struct ttm_mem_reg *mem);
467 468 469 470 471 472 473 474 475

	/**
	 * Return the pfn for a given page_offset inside the BO.
	 *
	 * @bo: the BO to look up the pfn for
	 * @page_offset: the offset to look up
	 */
	unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
				    unsigned long page_offset);
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492

	/**
	 * Read/write memory buffers for ptrace access
	 *
	 * @bo: the BO to access
	 * @offset: the offset from the start of the BO
	 * @buf: pointer to source/destination buffer
	 * @len: number of bytes to copy
	 * @write: whether to read (0) from or write (non-0) to BO
	 *
	 * If successful, this function should return the number of
	 * bytes copied, -EIO otherwise. If the number of bytes
	 * returned is < len, the function may be called again with
	 * the remainder of the buffer to copy.
	 */
	int (*access_memory)(struct ttm_buffer_object *bo, unsigned long offset,
			     void *buf, int len, int write);
493 494
};

495 496 497 498 499
/**
 * struct ttm_bo_global_ref - Argument to initialize a struct ttm_bo_global.
 */

struct ttm_bo_global_ref {
500
	struct drm_global_reference ref;
501 502
	struct ttm_mem_global *mem_glob;
};
503 504

/**
505
 * struct ttm_bo_global - Buffer object driver global data.
506 507 508 509
 *
 * @mem_glob: Pointer to a struct ttm_mem_global object for accounting.
 * @dummy_read_page: Pointer to a dummy page used for mapping requests
 * of unpopulated pages.
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
 * @shrink: A shrink callback object used for buffer object swap.
 * @device_list_mutex: Mutex protecting the device list.
 * This mutex is held while traversing the device list for pm options.
 * @lru_lock: Spinlock protecting the bo subsystem lru lists.
 * @device_list: List of buffer object devices.
 * @swap_lru: Lru list of buffer objects used for swapping.
 */

struct ttm_bo_global {

	/**
	 * Constant after init.
	 */

	struct kobject kobj;
	struct ttm_mem_global *mem_glob;
	struct page *dummy_read_page;
	struct ttm_mem_shrink shrink;
	struct mutex device_list_mutex;
	spinlock_t lru_lock;

	/**
	 * Protected by device_list_mutex.
	 */
	struct list_head device_list;

	/**
	 * Protected by the lru_lock.
	 */
539
	struct list_head swap_lru[TTM_MAX_BO_PRIORITY];
540 541 542 543 544 545 546 547 548 549 550 551 552 553

	/**
	 * Internal protection.
	 */
	atomic_t bo_count;
};


#define TTM_NUM_MEM_TYPES 8

/**
 * struct ttm_bo_device - Buffer object driver device-specific data.
 *
 * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
554
 * @man: An array of mem_type_managers.
555
 * @vma_manager: Address space manager
556 557 558 559 560 561 562 563 564 565 566 567 568
 * lru_lock: Spinlock that protects the buffer+device lru lists and
 * ddestroy lists.
 * @dev_mapping: A pointer to the struct address_space representing the
 * device address space.
 * @wq: Work queue structure for the delayed delete workqueue.
 *
 */

struct ttm_bo_device {

	/*
	 * Constant after bo device init / atomic.
	 */
569 570
	struct list_head device_list;
	struct ttm_bo_global *glob;
571
	struct ttm_bo_driver *driver;
572
	struct ttm_mem_type_manager man[TTM_NUM_MEM_TYPES];
573

574
	/*
575
	 * Protected by internal locks.
576
	 */
577
	struct drm_vma_offset_manager vma_manager;
578 579

	/*
580
	 * Protected by the global:lru lock.
581 582 583 584 585 586 587 588 589 590 591 592 593 594
	 */
	struct list_head ddestroy;

	/*
	 * Protected by load / firstopen / lastclose /unload sync.
	 */

	struct address_space *dev_mapping;

	/*
	 * Internal protection.
	 */

	struct delayed_work wq;
D
Dave Airlie 已提交
595 596

	bool need_dma32;
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
};

/**
 * ttm_flag_masked
 *
 * @old: Pointer to the result and original value.
 * @new: New value of bits.
 * @mask: Mask of bits to change.
 *
 * Convenience function to change a number of bits identified by a mask.
 */

static inline uint32_t
ttm_flag_masked(uint32_t *old, uint32_t new, uint32_t mask)
{
	*old ^= (*old ^ new) & mask;
	return *old;
}

/**
617
 * ttm_tt_init
618
 *
619
 * @ttm: The struct ttm_tt.
620 621 622 623 624 625 626 627 628 629
 * @bdev: pointer to a struct ttm_bo_device:
 * @size: Size of the data needed backing.
 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
 * @dummy_read_page: See struct ttm_bo_device.
 *
 * Create a struct ttm_tt to back data with system memory pages.
 * No pages are actually allocated.
 * Returns:
 * NULL: Out of memory.
 */
630 631 632
extern int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
			unsigned long size, uint32_t page_flags,
			struct page *dummy_read_page);
633 634 635 636 637 638 639 640 641 642 643 644 645
extern int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
			   unsigned long size, uint32_t page_flags,
			   struct page *dummy_read_page);

/**
 * ttm_tt_fini
 *
 * @ttm: the ttm_tt structure.
 *
 * Free memory of ttm_tt structure
 */
extern void ttm_tt_fini(struct ttm_tt *ttm);
extern void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

/**
 * ttm_ttm_bind:
 *
 * @ttm: The struct ttm_tt containing backing pages.
 * @bo_mem: The struct ttm_mem_reg identifying the binding location.
 *
 * Bind the pages of @ttm to an aperture location identified by @bo_mem
 */
extern int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);

/**
 * ttm_ttm_destroy:
 *
 * @ttm: The struct ttm_tt.
 *
662
 * Unbind, unpopulate and destroy common struct ttm_tt.
663 664 665
 */
extern void ttm_tt_destroy(struct ttm_tt *ttm);

666 667 668 669 670 671 672 673 674
/**
 * ttm_ttm_unbind:
 *
 * @ttm: The struct ttm_tt.
 *
 * Unbind a struct ttm_tt.
 */
extern void ttm_tt_unbind(struct ttm_tt *ttm);

675
/**
676
 * ttm_tt_swapin:
677 678 679
 *
 * @ttm: The struct ttm_tt.
 *
680
 * Swap in a previously swap out ttm_tt.
681
 */
682
extern int ttm_tt_swapin(struct ttm_tt *ttm);
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698

/**
 * ttm_tt_set_placement_caching:
 *
 * @ttm A struct ttm_tt the backing pages of which will change caching policy.
 * @placement: Flag indicating the desired caching policy.
 *
 * This function will change caching policy of any default kernel mappings of
 * the pages backing @ttm. If changing from cached to uncached or
 * write-combined,
 * all CPU caches will first be flushed to make sure the data of the pages
 * hit RAM. This function may be very costly as it involves global TLB
 * and cache flushes and potential page splitting / combining.
 */
extern int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
extern int ttm_tt_swapout(struct ttm_tt *ttm,
J
Jan Engelhardt 已提交
699
			  struct file *persistent_swap_storage);
700

701 702 703 704 705 706 707 708 709
/**
 * ttm_tt_unpopulate - free pages from a ttm
 *
 * @ttm: Pointer to the ttm_tt structure
 *
 * Calls the driver method to free all pages from a ttm
 */
extern void ttm_tt_unpopulate(struct ttm_tt *ttm);

710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
/*
 * ttm_bo.c
 */

/**
 * ttm_mem_reg_is_pci
 *
 * @bdev: Pointer to a struct ttm_bo_device.
 * @mem: A valid struct ttm_mem_reg.
 *
 * Returns true if the memory described by @mem is PCI memory,
 * false otherwise.
 */
extern bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev,
				   struct ttm_mem_reg *mem);

/**
 * ttm_bo_mem_space
 *
 * @bo: Pointer to a struct ttm_buffer_object. the data of which
 * we want to allocate space for.
 * @proposed_placement: Proposed new placement for the buffer object.
 * @mem: A struct ttm_mem_reg.
 * @interruptible: Sleep interruptible when sliping.
734
 * @no_wait_gpu: Return immediately if the GPU is busy.
735 736 737 738 739 740 741 742
 *
 * Allocate memory space for the buffer object pointed to by @bo, using
 * the placement flags in @mem, potentially evicting other idle buffer objects.
 * This function may sleep while waiting for space to become available.
 * Returns:
 * -EBUSY: No space available (only if no_wait == 1).
 * -ENOMEM: Could not allocate memory for the buffer object, either due to
 * fragmentation or concurrent allocators.
743
 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
744 745
 */
extern int ttm_bo_mem_space(struct ttm_buffer_object *bo,
746 747
				struct ttm_placement *placement,
				struct ttm_mem_reg *mem,
748
				bool interruptible,
749
				bool no_wait_gpu);
750 751 752

extern void ttm_bo_mem_put(struct ttm_buffer_object *bo,
			   struct ttm_mem_reg *mem);
753 754
extern void ttm_bo_mem_put_locked(struct ttm_buffer_object *bo,
				  struct ttm_mem_reg *mem);
755

756 757
extern void ttm_bo_global_release(struct drm_global_reference *ref);
extern int ttm_bo_global_init(struct drm_global_reference *ref);
758

759 760 761 762 763 764
extern int ttm_bo_device_release(struct ttm_bo_device *bdev);

/**
 * ttm_bo_device_init
 *
 * @bdev: A pointer to a struct ttm_bo_device to initialize.
765
 * @glob: A pointer to an initialized struct ttm_bo_global.
766
 * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
767
 * @mapping: The address space to use for this bo.
768 769 770 771 772 773 774 775 776
 * @file_page_offset: Offset into the device address space that is available
 * for buffer data. This ensures compatibility with other users of the
 * address space.
 *
 * Initializes a struct ttm_bo_device:
 * Returns:
 * !0: Failure.
 */
extern int ttm_bo_device_init(struct ttm_bo_device *bdev,
777
			      struct ttm_bo_global *glob,
778
			      struct ttm_bo_driver *driver,
779
			      struct address_space *mapping,
D
Dave Airlie 已提交
780
			      uint64_t file_page_offset, bool need_dma32);
781

782 783 784 785 786 787
/**
 * ttm_bo_unmap_virtual
 *
 * @bo: tear down the virtual mappings for this BO
 */
extern void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
788

789 790 791 792 793 794 795 796 797 798 799 800 801 802 803
/**
 * ttm_bo_unmap_virtual
 *
 * @bo: tear down the virtual mappings for this BO
 *
 * The caller must take ttm_mem_io_lock before calling this function.
 */
extern void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);

extern int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);
extern void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);
extern int ttm_mem_io_lock(struct ttm_mem_type_manager *man,
			   bool interruptible);
extern void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);

804 805 806 807
extern void ttm_bo_del_sub_from_lru(struct ttm_buffer_object *bo);
extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);

/**
808
 * __ttm_bo_reserve:
809 810 811 812
 *
 * @bo: A pointer to a struct ttm_buffer_object.
 * @interruptible: Sleep interruptible if waiting.
 * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
813
 * @ticket: ticket used to acquire the ww_mutex.
814 815 816 817 818 819 820 821 822 823 824 825 826 827
 *
 * Will not remove reserved buffers from the lru lists.
 * Otherwise identical to ttm_bo_reserve.
 *
 * Returns:
 * -EDEADLK: The reservation may cause a deadlock.
 * Release all buffer reservations, wait for @bo to become unreserved and
 * try again. (only if use_sequence == 1).
 * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
 * a signal. Release all buffer reservations and return to user-space.
 * -EBUSY: The function needed to sleep, but @no_wait was true
 * -EALREADY: Bo already reserved using @ticket. This error code will only
 * be returned if @use_ticket is set to true.
 */
828
static inline int __ttm_bo_reserve(struct ttm_buffer_object *bo,
829
				   bool interruptible, bool no_wait,
830
				   struct ww_acquire_ctx *ticket)
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850
{
	int ret = 0;

	if (no_wait) {
		bool success;
		if (WARN_ON(ticket))
			return -EBUSY;

		success = ww_mutex_trylock(&bo->resv->lock);
		return success ? 0 : -EBUSY;
	}

	if (interruptible)
		ret = ww_mutex_lock_interruptible(&bo->resv->lock, ticket);
	else
		ret = ww_mutex_lock(&bo->resv->lock, ticket);
	if (ret == -EINTR)
		return -ERESTARTSYS;
	return ret;
}
851

852 853 854 855 856 857
/**
 * ttm_bo_reserve:
 *
 * @bo: A pointer to a struct ttm_buffer_object.
 * @interruptible: Sleep interruptible if waiting.
 * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
858
 * @ticket: ticket used to acquire the ww_mutex.
859 860 861 862 863 864 865 866 867
 *
 * Locks a buffer object for validation. (Or prevents other processes from
 * locking it for validation) and removes it from lru lists, while taking
 * a number of measures to prevent deadlocks.
 *
 * Deadlocks may occur when two processes try to reserve multiple buffers in
 * different order, either by will or as a result of a buffer being evicted
 * to make room for a buffer already reserved. (Buffers are reserved before
 * they are evicted). The following algorithm prevents such deadlocks from
L
Lucas De Marchi 已提交
868
 * occurring:
869
 * Processes attempting to reserve multiple buffers other than for eviction,
870 871
 * (typically execbuf), should first obtain a unique 32-bit
 * validation sequence number,
872
 * and call this function with @use_ticket == 1 and @ticket->stamp == the unique
873 874 875 876
 * sequence number. If upon call of this function, the buffer object is already
 * reserved, the validation sequence is checked against the validation
 * sequence of the process currently reserving the buffer,
 * and if the current validation sequence is greater than that of the process
877
 * holding the reservation, the function returns -EDEADLK. Otherwise it sleeps
878 879
 * waiting for the buffer to become unreserved, after which it retries
 * reserving.
880
 * The caller should, when receiving an -EDEADLK error
881 882 883 884 885 886
 * release all its buffer reservations, wait for @bo to become unreserved, and
 * then rerun the validation with the same validation sequence. This procedure
 * will always guarantee that the process with the lowest validation sequence
 * will eventually succeed, preventing both deadlocks and starvation.
 *
 * Returns:
887
 * -EDEADLK: The reservation may cause a deadlock.
888 889
 * Release all buffer reservations, wait for @bo to become unreserved and
 * try again. (only if use_sequence == 1).
890
 * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
891
 * a signal. Release all buffer reservations and return to user-space.
892
 * -EBUSY: The function needed to sleep, but @no_wait was true
893 894
 * -EALREADY: Bo already reserved using @ticket. This error code will only
 * be returned if @use_ticket is set to true.
895
 */
896
static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
897
				 bool interruptible, bool no_wait,
898 899 900
				 struct ww_acquire_ctx *ticket)
{
	int ret;
901

902
	WARN_ON(!kref_read(&bo->kref));
903

904
	ret = __ttm_bo_reserve(bo, interruptible, no_wait, ticket);
905 906
	if (likely(ret == 0))
		ttm_bo_del_sub_from_lru(bo);
907

908 909
	return ret;
}
910 911 912 913 914 915 916 917 918 919 920

/**
 * ttm_bo_reserve_slowpath:
 * @bo: A pointer to a struct ttm_buffer_object.
 * @interruptible: Sleep interruptible if waiting.
 * @sequence: Set (@bo)->sequence to this value after lock
 *
 * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
 * from all our other reservations. Because there are no other reservations
 * held by us, this function cannot deadlock any more.
 */
921 922 923 924 925
static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
					  bool interruptible,
					  struct ww_acquire_ctx *ticket)
{
	int ret = 0;
926

927
	WARN_ON(!kref_read(&bo->kref));
928

929 930 931 932 933 934 935 936 937 938 939 940 941
	if (interruptible)
		ret = ww_mutex_lock_slow_interruptible(&bo->resv->lock,
						       ticket);
	else
		ww_mutex_lock_slow(&bo->resv->lock, ticket);

	if (likely(ret == 0))
		ttm_bo_del_sub_from_lru(bo);
	else if (ret == -EINTR)
		ret = -ERESTARTSYS;

	return ret;
}
942

943
/**
944
 * __ttm_bo_unreserve
945
 * @bo: A pointer to a struct ttm_buffer_object.
946
 *
947 948
 * Unreserve a previous reservation of @bo where the buffer object is
 * already on lru lists.
949
 */
950
static inline void __ttm_bo_unreserve(struct ttm_buffer_object *bo)
951 952 953
{
	ww_mutex_unlock(&bo->resv->lock);
}
954 955

/**
956 957
 * ttm_bo_unreserve
 *
958 959
 * @bo: A pointer to a struct ttm_buffer_object.
 *
960
 * Unreserve a previous reservation of @bo.
961
 */
962 963
static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
{
964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
	if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) {
		spin_lock(&bo->glob->lru_lock);
		ttm_bo_add_to_lru(bo);
		spin_unlock(&bo->glob->lru_lock);
	}
	__ttm_bo_unreserve(bo);
}

/**
 * ttm_bo_unreserve_ticket
 * @bo: A pointer to a struct ttm_buffer_object.
 * @ticket: ww_acquire_ctx used for reserving
 *
 * Unreserve a previous reservation of @bo made with @ticket.
 */
static inline void ttm_bo_unreserve_ticket(struct ttm_buffer_object *bo,
					   struct ww_acquire_ctx *t)
{
	ttm_bo_unreserve(bo);
983
}
984

985 986 987 988
/*
 * ttm_bo_util.c
 */

989 990 991 992
int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
		       struct ttm_mem_reg *mem);
void ttm_mem_io_free(struct ttm_bo_device *bdev,
		     struct ttm_mem_reg *mem);
993 994 995 996
/**
 * ttm_bo_move_ttm
 *
 * @bo: A pointer to a struct ttm_buffer_object.
997
 * @interruptible: Sleep interruptible if waiting.
998
 * @no_wait_gpu: Return immediately if the GPU is busy.
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
 * @new_mem: struct ttm_mem_reg indicating where to move.
 *
 * Optimized move function for a buffer object with both old and
 * new placement backed by a TTM. The function will, if successful,
 * free any old aperture space, and set (@new_mem)->mm_node to NULL,
 * and update the (@bo)->mem placement flags. If unsuccessful, the old
 * data remains untouched, and it's up to the caller to free the
 * memory space indicated by @new_mem.
 * Returns:
 * !0: Failure.
 */

extern int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
1012
			   bool interruptible, bool no_wait_gpu,
1013
			   struct ttm_mem_reg *new_mem);
1014 1015 1016 1017 1018

/**
 * ttm_bo_move_memcpy
 *
 * @bo: A pointer to a struct ttm_buffer_object.
1019
 * @interruptible: Sleep interruptible if waiting.
1020
 * @no_wait_gpu: Return immediately if the GPU is busy.
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
 * @new_mem: struct ttm_mem_reg indicating where to move.
 *
 * Fallback move function for a mappable buffer object in mappable memory.
 * The function will, if successful,
 * free any old aperture space, and set (@new_mem)->mm_node to NULL,
 * and update the (@bo)->mem placement flags. If unsuccessful, the old
 * data remains untouched, and it's up to the caller to free the
 * memory space indicated by @new_mem.
 * Returns:
 * !0: Failure.
 */

extern int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
1034
			      bool interruptible, bool no_wait_gpu,
1035
			      struct ttm_mem_reg *new_mem);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049

/**
 * ttm_bo_free_old_node
 *
 * @bo: A pointer to a struct ttm_buffer_object.
 *
 * Utility function to free an old placement after a successful move.
 */
extern void ttm_bo_free_old_node(struct ttm_buffer_object *bo);

/**
 * ttm_bo_move_accel_cleanup.
 *
 * @bo: A pointer to a struct ttm_buffer_object.
1050
 * @fence: A fence object that signals when moving is complete.
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
 * @evict: This is an evict move. Don't return until the buffer is idle.
 * @new_mem: struct ttm_mem_reg indicating where to move.
 *
 * Accelerated move function to be called when an accelerated move
 * has been scheduled. The function will create a new temporary buffer object
 * representing the old placement, and put the sync object on both buffer
 * objects. After that the newly created buffer object is unref'd to be
 * destroyed when the move is complete. This will help pipeline
 * buffer moves.
 */

extern int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
1063
				     struct dma_fence *fence, bool evict,
1064
				     struct ttm_mem_reg *new_mem);
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

/**
 * ttm_bo_pipeline_move.
 *
 * @bo: A pointer to a struct ttm_buffer_object.
 * @fence: A fence object that signals when moving is complete.
 * @evict: This is an evict move. Don't return until the buffer is idle.
 * @new_mem: struct ttm_mem_reg indicating where to move.
 *
 * Function for pipelining accelerated moves. Either free the memory
 * immediately or hang it on a temporary buffer object.
 */
int ttm_bo_pipeline_move(struct ttm_buffer_object *bo,
1078
			 struct dma_fence *fence, bool evict,
1079 1080
			 struct ttm_mem_reg *new_mem);

1081 1082 1083 1084 1085 1086 1087 1088 1089
/**
 * ttm_io_prot
 *
 * @c_state: Caching state.
 * @tmp: Page protection flag for a normal, cached mapping.
 *
 * Utility function that returns the pgprot_t that should be used for
 * setting up a PTE with the caching model indicated by @c_state.
 */
1090
extern pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
1091

1092 1093
extern const struct ttm_mem_type_manager_func ttm_bo_manager_func;

D
Daniel Vetter 已提交
1094
#if IS_ENABLED(CONFIG_AGP)
1095 1096 1097
#include <linux/agp_backend.h>

/**
1098
 * ttm_agp_tt_create
1099 1100 1101
 *
 * @bdev: Pointer to a struct ttm_bo_device.
 * @bridge: The agp bridge this device is sitting on.
1102 1103 1104 1105
 * @size: Size of the data needed backing.
 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
 * @dummy_read_page: See struct ttm_bo_device.
 *
1106 1107 1108 1109 1110
 *
 * Create a TTM backend that uses the indicated AGP bridge as an aperture
 * for TT memory. This function uses the linux agpgart interface to
 * bind and unbind memory backing a ttm_tt.
 */
1111 1112 1113 1114
extern struct ttm_tt *ttm_agp_tt_create(struct ttm_bo_device *bdev,
					struct agp_bridge_data *bridge,
					unsigned long size, uint32_t page_flags,
					struct page *dummy_read_page);
J
Jerome Glisse 已提交
1115 1116
int ttm_agp_tt_populate(struct ttm_tt *ttm);
void ttm_agp_tt_unpopulate(struct ttm_tt *ttm);
1117 1118 1119
#endif

#endif