ttm_bo_api.h 22.8 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/**************************************************************************
 *
 * 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_API_H_
#define _TTM_BO_API_H_

#include "drm_hashtab.h"
#include <linux/kref.h>
#include <linux/list.h>
#include <linux/wait.h>
#include <linux/mutex.h>
#include <linux/mm.h>
#include <linux/rbtree.h>
#include <linux/bitmap.h>

struct ttm_bo_device;

struct drm_mm_node;

47 48 49 50 51 52

/**
 * struct ttm_placement
 *
 * @fpfn:		first valid page frame number to put the object
 * @lpfn:		last valid page frame number to put the object
L
Lucas De Marchi 已提交
53 54 55 56
 * @num_placement:	number of preferred placements
 * @placement:		preferred placements
 * @num_busy_placement:	number of preferred placements when need to evict buffer
 * @busy_placement:	preferred placements when need to evict buffer
57 58 59 60 61 62 63 64 65 66 67 68
 *
 * Structure indicating the placement you request for an object.
 */
struct ttm_placement {
	unsigned	fpfn;
	unsigned	lpfn;
	unsigned	num_placement;
	const uint32_t	*placement;
	unsigned	num_busy_placement;
	const uint32_t	*busy_placement;
};

69 70 71 72 73 74 75 76
/**
 * struct ttm_bus_placement
 *
 * @addr:		mapped virtual address
 * @base:		bus base address
 * @is_iomem:		is this io memory ?
 * @size:		size in byte
 * @offset:		offset from the base address
77 78
 * @io_reserved_vm:     The VM system has a refcount in @io_reserved_count
 * @io_reserved_count:  Refcounting the numbers of callers to ttm_mem_io_reserve
79 80 81 82 83 84 85 86 87
 *
 * Structure indicating the bus placement of an object.
 */
struct ttm_bus_placement {
	void		*addr;
	unsigned long	base;
	unsigned long	size;
	unsigned long	offset;
	bool		is_iomem;
88 89
	bool		io_reserved_vm;
	uint64_t        io_reserved_count;
90 91
};

92

93 94 95 96 97 98 99 100
/**
 * struct ttm_mem_reg
 *
 * @mm_node: Memory manager node.
 * @size: Requested size of memory region.
 * @num_pages: Actual size of memory region in pages.
 * @page_alignment: Page alignment.
 * @placement: Placement flags.
101
 * @bus: Placement on io bus accessible to the CPU
102 103 104 105 106 107
 *
 * Structure indicating the placement and space resources used by a
 * buffer object.
 */

struct ttm_mem_reg {
108 109
	void *mm_node;
	unsigned long start;
110 111 112 113 114
	unsigned long size;
	unsigned long num_pages;
	uint32_t page_alignment;
	uint32_t mem_type;
	uint32_t placement;
115
	struct ttm_bus_placement bus;
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
};

/**
 * enum ttm_bo_type
 *
 * @ttm_bo_type_device:	These are 'normal' buffers that can
 * be mmapped by user space. Each of these bos occupy a slot in the
 * device address space, that can be used for normal vm operations.
 *
 * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
 * but they cannot be accessed from user-space. For kernel-only use.
 */

enum ttm_bo_type {
	ttm_bo_type_device,
	ttm_bo_type_kernel
};

struct ttm_tt;

/**
 * struct ttm_buffer_object
 *
 * @bdev: Pointer to the buffer object device structure.
 * @buffer_start: The virtual user-space start address of ttm_bo_type_user
 * buffers.
 * @type: The bo type.
 * @destroy: Destruction function. If NULL, kfree is used.
 * @num_pages: Actual number of pages.
 * @addr_space_offset: Address space offset.
 * @acc_size: Accounted size for this object.
 * @kref: Reference count of this buffer object. When this refcount reaches
 * zero, the object is put on the delayed delete list.
 * @list_kref: List reference count of this buffer object. This member is
 * used to avoid destruction while the buffer object is still on a list.
 * Lru lists may keep one refcount, the delayed delete list, and kref != 0
 * keeps one refcount. When this refcount reaches zero,
 * the object is destroyed.
 * @event_queue: Queue for processes waiting on buffer object status change.
 * @mem: structure describing current placement.
J
Jan Engelhardt 已提交
156
 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
157
 * pinned in physical memory. If this behaviour is not desired, this member
J
Jan Engelhardt 已提交
158
 * holds a pointer to a persistent shmem object.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
 * @ttm: TTM structure holding system pages.
 * @evicted: Whether the object was evicted without user-space knowing.
 * @cpu_writes: For synchronization. Number of cpu writers.
 * @lru: List head for the lru list.
 * @ddestroy: List head for the delayed destroy list.
 * @swap: List head for swap LRU list.
 * @val_seq: Sequence of the validation holding the @reserved lock.
 * Used to avoid starvation when many processes compete to validate the
 * buffer. This member is protected by the bo_device::lru_lock.
 * @seq_valid: The value of @val_seq is valid. This value is protected by
 * the bo_device::lru_lock.
 * @reserved: Deadlock-free lock used for synchronization state transitions.
 * @sync_obj_arg: Opaque argument to synchronization object function.
 * @sync_obj: Pointer to a synchronization object.
 * @priv_flags: Flags describing buffer object internal state.
 * @vm_rb: Rb node for the vm rb tree.
 * @vm_node: Address space manager node.
 * @offset: The current GPU offset, which can have different meanings
 * depending on the memory type. For SYSTEM type memory, it should be 0.
 * @cur_placement: Hint of current placement.
 *
 * Base class for TTM buffer object, that deals with data placement and CPU
 * mappings. GPU mappings are really up to the driver, but for simpler GPUs
 * the driver can usually use the placement offset @offset directly as the
 * GPU virtual address. For drivers implementing multiple
 * GPU memory manager contexts, the driver should manage the address space
 * in these contexts separately and use these objects to get the correct
 * placement and caching for these GPU maps. This makes it possible to use
 * these objects for even quite elaborate memory management schemes.
 * The destroy member, the API visibility of this object makes it possible
 * to derive driver specific types.
 */

struct ttm_buffer_object {
	/**
	 * Members constant at init.
	 */

197
	struct ttm_bo_global *glob;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
	struct ttm_bo_device *bdev;
	unsigned long buffer_start;
	enum ttm_bo_type type;
	void (*destroy) (struct ttm_buffer_object *);
	unsigned long num_pages;
	uint64_t addr_space_offset;
	size_t acc_size;

	/**
	* Members not needing protection.
	*/

	struct kref kref;
	struct kref list_kref;
	wait_queue_head_t event_queue;

	/**
	 * Members protected by the bo::reserved lock.
	 */

	struct ttm_mem_reg mem;
J
Jan Engelhardt 已提交
219
	struct file *persistent_swap_storage;
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	struct ttm_tt *ttm;
	bool evicted;

	/**
	 * Members protected by the bo::reserved lock only when written to.
	 */

	atomic_t cpu_writers;

	/**
	 * Members protected by the bdev::lru_lock.
	 */

	struct list_head lru;
	struct list_head ddestroy;
	struct list_head swap;
236
	struct list_head io_reserve_lru;
237 238 239 240 241 242 243 244 245 246 247
	uint32_t val_seq;
	bool seq_valid;

	/**
	 * Members protected by the bdev::lru_lock
	 * only when written to.
	 */

	atomic_t reserved;

	/**
248
	 * Members protected by struct buffer_object_device::fence_lock
249 250
	 * In addition, setting sync_obj to anything else
	 * than NULL requires bo::reserved to be held. This allows for
251
	 * checking NULL while reserved but not holding the mentioned lock.
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
	 */

	void *sync_obj_arg;
	void *sync_obj;
	unsigned long priv_flags;

	/**
	 * Members protected by the bdev::vm_lock
	 */

	struct rb_node vm_rb;
	struct drm_mm_node *vm_node;


	/**
	 * Special members that are protected by the reserve lock
	 * and the bo::lock when written to. Can be read with
	 * either of these locks held.
	 */

	unsigned long offset;
	uint32_t cur_placement;
};

/**
 * struct ttm_bo_kmap_obj
 *
 * @virtual: The current kernel virtual address.
 * @page: The page when kmap'ing a single page.
 * @bo_kmap_type: Type of bo_kmap.
 *
 * Object describing a kernel mapping. Since a TTM bo may be located
 * in various memory types with various caching policies, the
 * mapping can either be an ioremap, a vmap, a kmap or part of a
 * premapped region.
 */

289
#define TTM_BO_MAP_IOMEM_MASK 0x80
290 291 292 293
struct ttm_bo_kmap_obj {
	void *virtual;
	struct page *page;
	enum {
294 295 296 297
		ttm_bo_map_iomap        = 1 | TTM_BO_MAP_IOMEM_MASK,
		ttm_bo_map_vmap         = 2,
		ttm_bo_map_kmap         = 3,
		ttm_bo_map_premapped    = 4 | TTM_BO_MAP_IOMEM_MASK,
298
	} bo_kmap_type;
299
	struct ttm_buffer_object *bo;
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
};

/**
 * ttm_bo_reference - reference a struct ttm_buffer_object
 *
 * @bo: The buffer object.
 *
 * Returns a refcounted pointer to a buffer object.
 */

static inline struct ttm_buffer_object *
ttm_bo_reference(struct ttm_buffer_object *bo)
{
	kref_get(&bo->kref);
	return bo;
}

/**
 * ttm_bo_wait - wait for buffer idle.
 *
 * @bo:  The buffer object.
 * @interruptible:  Use interruptible wait.
 * @no_wait:  Return immediately if buffer is busy.
 *
 * This function must be called with the bo::mutex held, and makes
 * sure any previous rendering to the buffer is completed.
 * Note: It might be necessary to block validations before the
 * wait by reserving the buffer.
 * Returns -EBUSY if no_wait is true and the buffer is busy.
329
 * Returns -ERESTARTSYS if interrupted by a signal.
330 331 332 333
 */
extern int ttm_bo_wait(struct ttm_buffer_object *bo, bool lazy,
		       bool interruptible, bool no_wait);
/**
334
 * ttm_bo_validate
335 336
 *
 * @bo: The buffer object.
337
 * @placement: Proposed placement for the buffer object.
338
 * @interruptible: Sleep interruptible if sleeping.
339 340
 * @no_wait_reserve: Return immediately if other buffers are busy.
 * @no_wait_gpu: Return immediately if the GPU is busy.
341 342
 *
 * Changes placement and caching policy of the buffer object
343
 * according proposed placement.
344
 * Returns
345
 * -EINVAL on invalid proposed placement.
346 347
 * -ENOMEM on out-of-memory condition.
 * -EBUSY if no_wait is true and buffer busy.
348
 * -ERESTARTSYS if interrupted by a signal.
349
 */
350 351
extern int ttm_bo_validate(struct ttm_buffer_object *bo,
				struct ttm_placement *placement,
352 353
				bool interruptible, bool no_wait_reserve,
				bool no_wait_gpu);
354

355 356 357 358 359 360 361 362 363
/**
 * ttm_bo_unref
 *
 * @bo: The buffer object.
 *
 * Unreference and clear a pointer to a buffer object.
 */
extern void ttm_bo_unref(struct ttm_buffer_object **bo);

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

/**
 * ttm_bo_list_ref_sub
 *
 * @bo: The buffer object.
 * @count: The number of references with which to decrease @bo::list_kref;
 * @never_free: The refcount should not reach zero with this operation.
 *
 * Release @count lru list references to this buffer object.
 */
extern void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
				bool never_free);

/**
 * ttm_bo_add_to_lru
 *
 * @bo: The buffer object.
 *
 * Add this bo to the relevant mem type lru and, if it's backed by
 * system pages (ttms) to the swap list.
 * This function must be called with struct ttm_bo_global::lru_lock held, and
 * is typically called immediately prior to unreserving a bo.
 */
extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);

/**
 * ttm_bo_del_from_lru
 *
 * @bo: The buffer object.
 *
 * Remove this bo from all lru lists used to lookup and reserve an object.
 * This function must be called with struct ttm_bo_global::lru_lock held,
 * and is usually called just immediately after the bo has been reserved to
 * avoid recursive reservation from lru lists.
 */
extern int ttm_bo_del_from_lru(struct ttm_buffer_object *bo);


402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
/**
 * ttm_bo_lock_delayed_workqueue
 *
 * Prevent the delayed workqueue from running.
 * Returns
 * True if the workqueue was queued at the time
 */
extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);

/**
 * ttm_bo_unlock_delayed_workqueue
 *
 * Allows the delayed workqueue to run.
 */
extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
					    int resched);

419 420 421 422 423 424 425 426 427 428 429
/**
 * ttm_bo_synccpu_write_grab
 *
 * @bo: The buffer object:
 * @no_wait: Return immediately if buffer is busy.
 *
 * Synchronizes a buffer object for CPU RW access. This means
 * blocking command submission that affects the buffer and
 * waiting for buffer idle. This lock is recursive.
 * Returns
 * -EBUSY if the buffer is busy and no_wait is true.
430
 * -ERESTARTSYS if interrupted by a signal.
431 432 433
 */
extern int
ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
434

435 436 437 438 439 440 441 442 443
/**
 * ttm_bo_synccpu_write_release:
 *
 * @bo : The buffer object.
 *
 * Releases a synccpu lock.
 */
extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
/**
 * ttm_bo_acc_size
 *
 * @bdev: Pointer to a ttm_bo_device struct.
 * @bo_size: size of the buffer object in byte.
 * @struct_size: size of the structure holding buffer object datas
 *
 * Returns size to account for a buffer object
 */
size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
		       unsigned long bo_size,
		       unsigned struct_size);
size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
			   unsigned long bo_size,
			   unsigned struct_size);

460
/**
461
 * ttm_bo_init
462 463 464 465 466 467 468 469 470 471 472
 *
 * @bdev: Pointer to a ttm_bo_device struct.
 * @bo: Pointer to a ttm_buffer_object to be initialized.
 * @size: Requested size of buffer object.
 * @type: Requested type of buffer object.
 * @flags: Initial placement flags.
 * @page_alignment: Data alignment in pages.
 * @buffer_start: Virtual address of user space data backing a
 * user buffer object.
 * @interruptible: If needing to sleep to wait for GPU resources,
 * sleep interruptible.
J
Jan Engelhardt 已提交
473
 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
474
 * pinned in physical memory. If this behaviour is not desired, this member
J
Jan Engelhardt 已提交
475
 * holds a pointer to a persistent shmem object. Typically, this would
476 477 478 479 480 481 482 483 484 485
 * point to the shmem object backing a GEM object if TTM is used to back a
 * GEM user interface.
 * @acc_size: Accounted size for this object.
 * @destroy: Destroy function. Use NULL for kfree().
 *
 * This function initializes a pre-allocated struct ttm_buffer_object.
 * As this object may be part of a larger structure, this function,
 * together with the @destroy function,
 * enables driver-specific objects derived from a ttm_buffer_object.
 * On successful return, the object kref and list_kref are set to 1.
486 487 488 489
 * If a failure occurs, the function will call the @destroy function, or
 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
 * illegal and will likely cause memory corruption.
 *
490 491 492
 * Returns
 * -ENOMEM: Out of memory.
 * -EINVAL: Invalid placement flags.
493
 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
494 495
 */

496 497 498 499 500 501 502 503
extern int ttm_bo_init(struct ttm_bo_device *bdev,
			struct ttm_buffer_object *bo,
			unsigned long size,
			enum ttm_bo_type type,
			struct ttm_placement *placement,
			uint32_t page_alignment,
			unsigned long buffer_start,
			bool interrubtible,
J
Jan Engelhardt 已提交
504
			struct file *persistent_swap_storage,
505 506
			size_t acc_size,
			void (*destroy) (struct ttm_buffer_object *));
507

508 509 510 511 512 513 514 515 516 517 518 519 520
/**
 * ttm_bo_synccpu_object_init
 *
 * @bdev: Pointer to a ttm_bo_device struct.
 * @bo: Pointer to a ttm_buffer_object to be initialized.
 * @size: Requested size of buffer object.
 * @type: Requested type of buffer object.
 * @flags: Initial placement flags.
 * @page_alignment: Data alignment in pages.
 * @buffer_start: Virtual address of user space data backing a
 * user buffer object.
 * @interruptible: If needing to sleep while waiting for GPU resources,
 * sleep interruptible.
J
Jan Engelhardt 已提交
521
 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
522
 * pinned in physical memory. If this behaviour is not desired, this member
J
Jan Engelhardt 已提交
523
 * holds a pointer to a persistent shmem object. Typically, this would
524 525 526 527
 * point to the shmem object backing a GEM object if TTM is used to back a
 * GEM user interface.
 * @p_bo: On successful completion *p_bo points to the created object.
 *
528 529
 * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
 * on that object. The destroy function is set to kfree().
530 531 532
 * Returns
 * -ENOMEM: Out of memory.
 * -EINVAL: Invalid placement flags.
533
 * -ERESTARTSYS: Interrupted by signal while waiting for resources.
534 535
 */

536 537 538 539 540 541 542
extern int ttm_bo_create(struct ttm_bo_device *bdev,
				unsigned long size,
				enum ttm_bo_type type,
				struct ttm_placement *placement,
				uint32_t page_alignment,
				unsigned long buffer_start,
				bool interruptible,
J
Jan Engelhardt 已提交
543
				struct file *persistent_swap_storage,
544
				struct ttm_buffer_object **p_bo);
545 546 547 548

/**
 * ttm_bo_check_placement
 *
549 550
 * @bo:		the buffer object.
 * @placement:	placements
551 552 553 554 555 556 557
 *
 * Performs minimal validity checking on an intended change of
 * placement flags.
 * Returns
 * -EINVAL: Intended change is invalid or not allowed.
 */
extern int ttm_bo_check_placement(struct ttm_buffer_object *bo,
558
					struct ttm_placement *placement);
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576

/**
 * ttm_bo_init_mm
 *
 * @bdev: Pointer to a ttm_bo_device struct.
 * @mem_type: The memory type.
 * @p_size: size managed area in pages.
 *
 * Initialize a manager for a given memory type.
 * Note: if part of driver firstopen, it must be protected from a
 * potentially racing lastclose.
 * Returns:
 * -EINVAL: invalid size or memory type.
 * -ENOMEM: Not enough memory.
 * May also return driver-specified errors.
 */

extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
577
				unsigned long p_size);
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
/**
 * ttm_bo_clean_mm
 *
 * @bdev: Pointer to a ttm_bo_device struct.
 * @mem_type: The memory type.
 *
 * Take down a manager for a given memory type after first walking
 * the LRU list to evict any buffers left alive.
 *
 * Normally, this function is part of lastclose() or unload(), and at that
 * point there shouldn't be any buffers left created by user-space, since
 * there should've been removed by the file descriptor release() method.
 * However, before this function is run, make sure to signal all sync objects,
 * and verify that the delayed delete queue is empty. The driver must also
 * make sure that there are no NO_EVICT buffers present in this memory type
 * when the call is made.
 *
 * If this function is part of a VT switch, the caller must make sure that
 * there are no appications currently validating buffers before this
 * function is called. The caller can do that by first taking the
 * struct ttm_bo_device::ttm_lock in write mode.
 *
 * Returns:
 * -EINVAL: invalid or uninitialized memory type.
 * -EBUSY: There are still buffers left in this memory type.
 */

extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);

/**
 * ttm_bo_evict_mm
 *
 * @bdev: Pointer to a ttm_bo_device struct.
 * @mem_type: The memory type.
 *
 * Evicts all buffers on the lru list of the memory type.
 * This is normally part of a VT switch or an
 * out-of-memory-space-due-to-fragmentation handler.
 * The caller must make sure that there are no other processes
 * currently validating buffers, and can do that by taking the
 * struct ttm_bo_device::ttm_lock in write mode.
 *
 * Returns:
 * -EINVAL: Invalid or uninitialized memory type.
622
 * -ERESTARTSYS: The call was interrupted by a signal while waiting to
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
 * evict a buffer.
 */

extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);

/**
 * ttm_kmap_obj_virtual
 *
 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
 * @is_iomem: Pointer to an integer that on return indicates 1 if the
 * virtual map is io memory, 0 if normal memory.
 *
 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
 * that should strictly be accessed by the iowriteXX() and similar functions.
 */

static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
					 bool *is_iomem)
{
643
	*is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 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
	return map->virtual;
}

/**
 * ttm_bo_kmap
 *
 * @bo: The buffer object.
 * @start_page: The first page to map.
 * @num_pages: Number of pages to map.
 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
 *
 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
 * used to obtain a virtual address to the data.
 *
 * Returns
 * -ENOMEM: Out of memory.
 * -EINVAL: Invalid range.
 */

extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
		       unsigned long num_pages, struct ttm_bo_kmap_obj *map);

/**
 * ttm_bo_kunmap
 *
 * @map: Object describing the map to unmap.
 *
 * Unmaps a kernel map set up by ttm_bo_kmap.
 */

extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);

/**
 * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object.
 *
 * @vma:       vma as input from the fbdev mmap method.
 * @bo:        The bo backing the address space. The address space will
 * have the same size as the bo, and start at offset 0.
 *
 * This function is intended to be called by the fbdev mmap method
 * if the fbdev address space is to be backed by a bo.
 */

extern int ttm_fbdev_mmap(struct vm_area_struct *vma,
			  struct ttm_buffer_object *bo);

/**
 * ttm_bo_mmap - mmap out of the ttm device address space.
 *
 * @filp:      filp as input from the mmap method.
 * @vma:       vma as input from the mmap method.
 * @bdev:      Pointer to the ttm_bo_device with the address space manager.
 *
 * This function is intended to be called by the device mmap method.
 * if the device address space is to be backed by the bo manager.
 */

extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
		       struct ttm_bo_device *bdev);

/**
 * ttm_bo_io
 *
 * @bdev:      Pointer to the struct ttm_bo_device.
 * @filp:      Pointer to the struct file attempting to read / write.
 * @wbuf:      User-space pointer to address of buffer to write. NULL on read.
 * @rbuf:      User-space pointer to address of buffer to read into.
 * Null on write.
 * @count:     Number of bytes to read / write.
 * @f_pos:     Pointer to current file position.
 * @write:     1 for read, 0 for write.
 *
 * This function implements read / write into ttm buffer objects, and is
 * intended to
 * be called from the fops::read and fops::write method.
 * Returns:
 * See man (2) write, man(2) read. In particular,
722
 * the function may return -ERESTARTSYS if
723 724 725 726 727 728 729 730 731 732
 * interrupted by a signal.
 */

extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
			 const char __user *wbuf, char __user *rbuf,
			 size_t count, loff_t *f_pos, bool write);

extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev);

#endif