ttm_bo_driver.h 25.7 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
#include <drm/drm_mm.h>
34
#include <drm/drm_vma_manager.h>
35 36 37
#include <linux/workqueue.h>
#include <linux/fs.h>
#include <linux/spinlock.h>
38
#include <linux/dma-resv.h>
39

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

46
#define TTM_MAX_BO_PRIORITY	4U
47

48 49 50
struct ttm_mem_type_manager;

struct ttm_mem_type_manager_func {
T
Thomas Hellstrom 已提交
51 52 53 54 55 56
	/**
	 * 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.
57
	 * @flags: Additional placement flags.
T
Thomas Hellstrom 已提交
58 59 60 61 62 63 64 65
	 * @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 已提交
66
	 * If the memory region accommodate the buffer object, @mem::mm_node
T
Thomas Hellstrom 已提交
67
	 * should be set to NULL, and the function should return 0.
L
Lucas De Marchi 已提交
68
	 * If a system error occurred, preventing the request to be fulfilled,
T
Thomas Hellstrom 已提交
69 70 71 72 73 74 75 76 77 78
	 * 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.
	 */
79 80
	int  (*get_node)(struct ttm_mem_type_manager *man,
			 struct ttm_buffer_object *bo,
81
			 const struct ttm_place *place,
82
			 struct ttm_mem_reg *mem);
T
Thomas Hellstrom 已提交
83 84 85 86 87 88 89 90 91 92 93

	/**
	 * 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.
	 */
94 95
	void (*put_node)(struct ttm_mem_type_manager *man,
			 struct ttm_mem_reg *mem);
T
Thomas Hellstrom 已提交
96 97 98 99 100

	/**
	 * struct ttm_mem_type_manager member debug
	 *
	 * @man: Pointer to a memory type manager.
101
	 * @printer: Prefix to be used in printout to identify the caller.
T
Thomas Hellstrom 已提交
102 103 104 105 106
	 *
	 * 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.
	 */
107 108
	void (*debug)(struct ttm_mem_type_manager *man,
		      struct drm_printer *printer);
109 110
};

111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/**
 * 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
 * @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.
130
 * @move_lock: lock for move fence
131 132
 * static information. bdev::driver::io_mem_free is never used.
 * @lru: The lru list for this memory type.
133
 * @move: The fence of the last pipelined move operation.
134 135 136 137 138 139
 *
 * This structure is used to identify and manage memory types for a device.
 */



140 141 142 143 144 145 146
struct ttm_mem_type_manager {
	/*
	 * No protection. Constant from start.
	 */

	bool has_type;
	bool use_type;
147
	bool use_tt;
148 149 150
	uint64_t size;
	uint32_t available_caching;
	uint32_t default_caching;
T
Thomas Hellstrom 已提交
151
	const struct ttm_mem_type_manager_func *func;
152 153
	struct mutex io_reserve_mutex;
	bool use_io_reserve_lru;
154
	spinlock_t move_lock;
155 156 157 158 159 160

	/*
	 * Protected by @io_reserve_mutex:
	 */

	struct list_head io_reserve_lru;
161 162

	/*
T
Thomas Hellstrom 已提交
163
	 * Protected by the global->lru_lock.
164
	 */
T
Thomas Hellstrom 已提交
165

166
	struct list_head lru[TTM_MAX_BO_PRIORITY];
167 168 169 170

	/*
	 * Protected by @move_lock.
	 */
171
	struct dma_fence *move;
172 173 174 175 176 177 178 179 180 181 182 183 184 185
};

/**
 * struct ttm_bo_driver
 *
 * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
 * @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 {
	/**
186
	 * ttm_tt_create
187
	 *
188
	 * @bo: The buffer object to create the ttm for.
189
	 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
190
	 *
191 192 193 194
	 * Create a struct ttm_tt to back data with system memory pages.
	 * No pages are actually allocated.
	 * Returns:
	 * NULL: Out of memory.
195
	 */
196
	struct ttm_tt *(*ttm_tt_create)(struct ttm_buffer_object *bo,
197
					uint32_t page_flags);
198

199 200 201 202 203 204 205 206 207
	/**
	 * ttm_tt_populate
	 *
	 * @ttm: The struct ttm_tt to contain the backing pages.
	 *
	 * Allocate all backing pages
	 * Returns:
	 * -ENOMEM: Out of memory.
	 */
208 209
	int (*ttm_tt_populate)(struct ttm_tt *ttm,
			struct ttm_operation_ctx *ctx);
210 211 212 213 214 215 216 217 218 219

	/**
	 * 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);

220 221 222 223 224 225 226 227 228 229 230
	/**
	 * 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);
231 232 233 234 235 236 237 238 239 240
	/**
	 * 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
	 */

241 242 243
	void (*evict_flags)(struct ttm_buffer_object *bo,
			    struct ttm_placement *placement);

244 245 246 247 248 249
	/**
	 * struct ttm_bo_driver member move:
	 *
	 * @bo: the buffer to move
	 * @evict: whether this motion is evicting the buffer from
	 * the graphics address space
250
	 * @ctx: context for this move with parameters
251 252 253 254
	 * @new_mem: the new memory region receiving the buffer
	 *
	 * Move a buffer between two memory regions.
	 */
255
	int (*move)(struct ttm_buffer_object *bo, bool evict,
256
		    struct ttm_operation_ctx *ctx,
257
		    struct ttm_mem_reg *new_mem);
258 259 260 261 262 263 264 265 266 267 268 269 270

	/**
	 * 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.
	 */
271 272
	int (*verify_access)(struct ttm_buffer_object *bo,
			     struct file *filp);
273

274 275 276 277 278 279 280
	/**
	 * 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
	 */
281
	void (*move_notify)(struct ttm_buffer_object *bo,
282
			    bool evict,
283 284 285
			    struct ttm_mem_reg *new_mem);
	/* notify the driver we are taking a fault on this BO
	 * and have reserved it */
286
	int (*fault_reserve_notify)(struct ttm_buffer_object *bo);
287 288 289 290

	/**
	 * notify the driver that we're about to swap out this bo
	 */
291
	void (*swap_notify)(struct ttm_buffer_object *bo);
292 293 294 295 296 297 298

	/**
	 * 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.
	 */
299 300 301 302
	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);
303 304 305 306 307 308 309 310 311

	/**
	 * 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);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

	/**
	 * 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);
329 330 331 332 333 334 335 336 337

	/**
	 * struct ttm_bo_driver member del_from_lru_notify
	 *
	 * @bo: the buffer object deleted from lru
	 *
	 * notify driver that a BO was deleted from LRU.
	 */
	void (*del_from_lru_notify)(struct ttm_buffer_object *bo);
338 339 340 341 342 343 344 345 346 347

	/**
	 * Notify the driver that we're about to release a BO
	 *
	 * @bo: BO that is about to be released
	 *
	 * Gives the driver a chance to do any cleanup, including
	 * adding fences that may force a delayed delete
	 */
	void (*release_notify)(struct ttm_buffer_object *bo);
348 349 350
};

/**
351
 * struct ttm_bo_global - Buffer object driver global data.
352 353 354
 *
 * @dummy_read_page: Pointer to a dummy page used for mapping requests
 * of unpopulated pages.
355 356 357 358 359 360 361 362
 * @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.
 */

363
extern struct ttm_bo_global {
364 365 366 367 368 369 370 371 372 373

	/**
	 * Constant after init.
	 */

	struct kobject kobj;
	struct page *dummy_read_page;
	spinlock_t lru_lock;

	/**
374
	 * Protected by ttm_global_mutex.
375 376 377 378 379 380
	 */
	struct list_head device_list;

	/**
	 * Protected by the lru_lock.
	 */
381
	struct list_head swap_lru[TTM_MAX_BO_PRIORITY];
382 383 384 385 386

	/**
	 * Internal protection.
	 */
	atomic_t bo_count;
387
} ttm_bo_glob;
388 389 390 391 392 393 394 395


#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.
396
 * @man: An array of mem_type_managers.
397
 * @vma_manager: Address space manager (pointer)
398 399 400 401 402
 * 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.
403
 * @no_retry: Don't retry allocation if it fails
404 405 406 407 408 409 410 411
 *
 */

struct ttm_bo_device {

	/*
	 * Constant after bo device init / atomic.
	 */
412
	struct list_head device_list;
413
	struct ttm_bo_driver *driver;
414 415 416
	/*
	 * access via ttm_manager_type.
	 */
417
	struct ttm_mem_type_manager sysman;
418
	struct ttm_mem_type_manager *man_drv[TTM_NUM_MEM_TYPES];
419
	/*
420
	 * Protected by internal locks.
421
	 */
422
	struct drm_vma_offset_manager *vma_manager;
423 424

	/*
425
	 * Protected by the global:lru lock.
426 427 428 429 430 431 432 433 434 435 436 437 438 439
	 */
	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 已提交
440 441

	bool need_dma32;
442 443

	bool no_retry;
444 445
};

446 447 448
static inline struct ttm_mem_type_manager *ttm_manager_type(struct ttm_bo_device *bdev,
							    int mem_type)
{
449
	return bdev->man_drv[mem_type];
450 451
}

452 453 454 455 456 457 458
static inline void ttm_set_driver_manager(struct ttm_bo_device *bdev,
					  int type,
					  struct ttm_mem_type_manager *manager)
{
	bdev->man_drv[type] = manager;
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
/**
 * struct ttm_lru_bulk_move_pos
 *
 * @first: first BO in the bulk move range
 * @last: last BO in the bulk move range
 *
 * Positions for a lru bulk move.
 */
struct ttm_lru_bulk_move_pos {
	struct ttm_buffer_object *first;
	struct ttm_buffer_object *last;
};

/**
 * struct ttm_lru_bulk_move
 *
 * @tt: first/last lru entry for BOs in the TT domain
 * @vram: first/last lru entry for BOs in the VRAM domain
 * @swap: first/last lru entry for BOs on the swap list
 *
 * Helper structure for bulk moves on the LRU list.
 */
struct ttm_lru_bulk_move {
	struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY];
	struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY];
	struct ttm_lru_bulk_move_pos swap[TTM_MAX_BO_PRIORITY];
};

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/**
 * 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;
}

/*
 * 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.
 */
517
bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem);
518 519 520 521 522 523 524 525 526

/**
 * 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.
527
 * @no_wait_gpu: Return immediately if the GPU is busy.
528 529 530 531 532 533 534 535
 *
 * 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.
536
 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
537
 */
538 539 540
int ttm_bo_mem_space(struct ttm_buffer_object *bo,
		     struct ttm_placement *placement,
		     struct ttm_mem_reg *mem,
541
		     struct ttm_operation_ctx *ctx);
542 543

void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem);
544

545
int ttm_bo_device_release(struct ttm_bo_device *bdev);
546 547 548 549 550

/**
 * ttm_bo_device_init
 *
 * @bdev: A pointer to a struct ttm_bo_device to initialize.
551
 * @glob: A pointer to an initialized struct ttm_bo_global.
552
 * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
553
 * @mapping: The address space to use for this bo.
554
 * @vma_manager: A pointer to a vma manager.
555 556 557 558 559 560 561 562
 * @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.
 */
563
int ttm_bo_device_init(struct ttm_bo_device *bdev,
564 565
		       struct ttm_bo_driver *driver,
		       struct address_space *mapping,
566
		       struct drm_vma_offset_manager *vma_manager,
567
		       bool need_dma32);
568

569 570 571 572 573
/**
 * ttm_bo_unmap_virtual
 *
 * @bo: tear down the virtual mappings for this BO
 */
574
void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
575

576 577 578 579 580 581 582
/**
 * 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.
 */
583
void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
584

585 586 587 588
int ttm_mem_io_reserve_vm(struct ttm_buffer_object *bo);
void ttm_mem_io_free_vm(struct ttm_buffer_object *bo);
int ttm_mem_io_lock(struct ttm_mem_type_manager *man, bool interruptible);
void ttm_mem_io_unlock(struct ttm_mem_type_manager *man);
589

590
/**
591
 * ttm_bo_reserve:
592 593 594 595
 *
 * @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.
596
 * @ticket: ticket used to acquire the ww_mutex.
597
 *
598 599 600
 * Locks a buffer object for validation. (Or prevents other processes from
 * locking it for validation), while taking a number of measures to prevent
 * deadlocks.
601 602 603 604
 *
 * Returns:
 * -EDEADLK: The reservation may cause a deadlock.
 * Release all buffer reservations, wait for @bo to become unreserved and
605
 * try again.
606 607 608 609 610 611
 * -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.
 */
612 613 614
static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
				 bool interruptible, bool no_wait,
				 struct ww_acquire_ctx *ticket)
615 616 617 618 619 620 621 622
{
	int ret = 0;

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

623
		success = dma_resv_trylock(bo->base.resv);
624 625 626 627
		return success ? 0 : -EBUSY;
	}

	if (interruptible)
628
		ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
629
	else
630
		ret = dma_resv_lock(bo->base.resv, ticket);
631 632 633 634
	if (ret == -EINTR)
		return -ERESTARTSYS;
	return ret;
}
635

636 637 638 639 640 641 642 643 644 645
/**
 * 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.
 */
646 647 648 649
static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
					  bool interruptible,
					  struct ww_acquire_ctx *ticket)
{
650 651 652 653 654 655 656 657 658
	if (interruptible) {
		int ret = dma_resv_lock_slow_interruptible(bo->base.resv,
							   ticket);
		if (ret == -EINTR)
			ret = -ERESTARTSYS;
		return ret;
	}
	dma_resv_lock_slow(bo->base.resv, ticket);
	return 0;
659
}
660

661 662 663 664 665 666 667
static inline void ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
{
	spin_lock(&ttm_bo_glob.lru_lock);
	ttm_bo_move_to_lru_tail(bo, NULL);
	spin_unlock(&ttm_bo_glob.lru_lock);
}

668
/**
669 670
 * ttm_bo_unreserve
 *
671 672
 * @bo: A pointer to a struct ttm_buffer_object.
 *
673
 * Unreserve a previous reservation of @bo.
674
 */
675 676
static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
{
677
	ttm_bo_move_to_lru_tail_unlocked(bo);
678
	dma_resv_unlock(bo->base.resv);
679 680
}

681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
/**
 * ttm_mem_type_manager_set_used
 *
 * @man: A memory manager object.
 * @used: usage state to set.
 *
 * Set the manager in use flag. If disabled the manager is no longer
 * used for object placement.
 */
static inline void ttm_mem_type_manager_set_used(struct ttm_mem_type_manager *man, bool used)
{
	man->has_type = true;
	man->use_type = used;
}

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
/**
 * ttm_mem_type_manager_disable.
 *
 * @man: A memory manager object.
 *
 * Indicate the manager is not to be used and deregistered. (temporary during rework).
 */
static inline void ttm_mem_type_manager_disable(struct ttm_mem_type_manager *man)
{
	man->has_type = false;
	man->use_type = false;
}

/**
 * ttm_mem_type_manager_cleanup
 *
 * @man: A memory manager object.
 *
 * Cleanup the move fences from the memory manager object.
 */
static inline void ttm_mem_type_manager_cleanup(struct ttm_mem_type_manager *man)
{
	dma_fence_put(man->move);
	man->move = NULL;
}

722 723 724 725 726 727 728 729 730 731 732 733
/*
 * ttm_mem_type_manager_force_list_clean
 *
 * @bdev - device to use
 * @man - manager to use
 *
 * Force all the objects out of a memory manager until clean.
 * Part of memory manager cleanup sequence.
 */
int ttm_mem_type_manager_force_list_clean(struct ttm_bo_device *bdev,
					  struct ttm_mem_type_manager *man);

734 735 736 737
/*
 * ttm_bo_util.c
 */

738 739 740 741
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);
742 743 744 745
/**
 * ttm_bo_move_ttm
 *
 * @bo: A pointer to a struct ttm_buffer_object.
746
 * @interruptible: Sleep interruptible if waiting.
747
 * @no_wait_gpu: Return immediately if the GPU is busy.
748 749 750 751 752 753 754 755 756 757 758 759
 * @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.
 */

760
int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
761
		    struct ttm_operation_ctx *ctx,
762
		    struct ttm_mem_reg *new_mem);
763 764 765 766 767

/**
 * ttm_bo_move_memcpy
 *
 * @bo: A pointer to a struct ttm_buffer_object.
768
 * @interruptible: Sleep interruptible if waiting.
769
 * @no_wait_gpu: Return immediately if the GPU is busy.
770 771 772 773 774 775 776 777 778 779 780 781
 * @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.
 */

782
int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
783
		       struct ttm_operation_ctx *ctx,
784
		       struct ttm_mem_reg *new_mem);
785 786 787 788 789 790 791 792

/**
 * 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.
 */
793
void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
794 795 796 797 798

/**
 * ttm_bo_move_accel_cleanup.
 *
 * @bo: A pointer to a struct ttm_buffer_object.
799
 * @fence: A fence object that signals when moving is complete.
800 801 802 803 804 805 806 807 808 809
 * @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.
 */
810 811 812
int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
			      struct dma_fence *fence, bool evict,
			      struct ttm_mem_reg *new_mem);
813 814 815 816 817 818 819 820 821 822 823 824 825

/**
 * 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,
826
			 struct dma_fence *fence, bool evict,
827 828
			 struct ttm_mem_reg *new_mem);

829 830 831 832 833
/**
 * ttm_bo_pipeline_gutting.
 *
 * @bo: A pointer to a struct ttm_buffer_object.
 *
834
 * Pipelined gutting a BO of its backing store.
835 836 837
 */
int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);

838 839 840 841 842 843 844 845 846
/**
 * 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.
 */
847
pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
848

849 850 851 852
/**
 * ttm_range_man_init
 *
 * @bdev: ttm device
853 854 855 856
 * @type: memory manager type
 * @available_caching: TTM_PL_FLAG_* for allowed caching modes
 * @default_caching: default caching mode
 * @use_tt: if the memory manager uses tt
857 858 859 860 861 862
 * @p_size: size of area to be managed in pages.
 *
 * Initialise a generic range manager for the selected memory type.
 * The range manager is installed for this device in the type slot.
 */
int ttm_range_man_init(struct ttm_bo_device *bdev,
863 864 865 866
		       unsigned type,
		       uint32_t available_caching,
		       uint32_t default_caching,
		       bool use_tt,
867 868
		       unsigned long p_size);

869 870 871 872 873 874 875 876 877
/**
 * ttm_range_man_fini
 *
 * @bdev: ttm device
 * @type: memory manager type
 *
 * Remove the generic range manager from a slot and tear it down.
 */
int ttm_range_man_fini(struct ttm_bo_device *bdev,
878
		       unsigned type);
879

880 881 882 883 884 885 886 887
/**
 * ttm_mem_type_manager_debug
 *
 * @man: manager type to dump.
 * @p: printer to use for debug.
 */
void ttm_mem_type_manager_debug(struct ttm_mem_type_manager *man,
				struct drm_printer *p);
888

889
#endif