1 /**************************************************************************
3 * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
30 #ifndef _TTM_BO_DRIVER_H_
31 #define _TTM_BO_DRIVER_H_
33 #include <drm/drm_mm.h>
34 #include <drm/drm_vma_manager.h>
35 #include <linux/workqueue.h>
37 #include <linux/spinlock.h>
38 #include <linux/dma-resv.h>
40 #include "ttm_bo_api.h"
41 #include "ttm_memory.h"
42 #include "ttm_module.h"
43 #include "ttm_placement.h"
47 * struct ttm_bo_driver
49 * @create_ttm_backend_entry: Callback to create a struct ttm_backend.
50 * @evict_flags: Callback to obtain placement flags when a buffer is evicted.
51 * @move: Callback for a driver to hook in accelerated functions to
53 * If set to NULL, a potentially slow memcpy() move is used.
56 struct ttm_bo_driver {
60 * @bo: The buffer object to create the ttm for.
61 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
63 * Create a struct ttm_tt to back data with system memory pages.
64 * No pages are actually allocated.
66 * NULL: Out of memory.
68 struct ttm_tt *(*ttm_tt_create)(struct ttm_buffer_object *bo,
74 * @ttm: The struct ttm_tt to contain the backing pages.
76 * Allocate all backing pages
78 * -ENOMEM: Out of memory.
80 int (*ttm_tt_populate)(struct ttm_bo_device *bdev,
82 struct ttm_operation_ctx *ctx);
87 * @ttm: The struct ttm_tt to contain the backing pages.
89 * Free all backing page
91 void (*ttm_tt_unpopulate)(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
96 * @bdev: Pointer to a ttm device
97 * @ttm: Pointer to a struct ttm_tt.
98 * @bo_mem: Pointer to a struct ttm_resource describing the
99 * memory type and location for binding.
101 * Bind the backend pages into the aperture in the location
102 * indicated by @bo_mem. This function should be able to handle
103 * differences between aperture and system page sizes.
105 int (*ttm_tt_bind)(struct ttm_bo_device *bdev, struct ttm_tt *ttm, struct ttm_resource *bo_mem);
110 * @bdev: Pointer to a ttm device
111 * @ttm: Pointer to a struct ttm_tt.
113 * Unbind previously bound backend pages. This function should be
114 * able to handle differences between aperture and system page sizes.
116 void (*ttm_tt_unbind)(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
121 * @bdev: Pointer to a ttm device
122 * @ttm: Pointer to a struct ttm_tt.
124 * Destroy the backend. This will be call back from ttm_tt_destroy so
125 * don't call ttm_tt_destroy from the callback or infinite loop.
127 void (*ttm_tt_destroy)(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
130 * struct ttm_bo_driver member eviction_valuable
132 * @bo: the buffer object to be evicted
133 * @place: placement we need room for
135 * Check with the driver if it is valuable to evict a BO to make room
136 * for a certain placement.
138 bool (*eviction_valuable)(struct ttm_buffer_object *bo,
139 const struct ttm_place *place);
141 * struct ttm_bo_driver member evict_flags:
143 * @bo: the buffer object to be evicted
145 * Return the bo flags for a buffer which is not mapped to the hardware.
146 * These will be placed in proposed_flags so that when the move is
147 * finished, they'll end up in bo->mem.flags
150 void (*evict_flags)(struct ttm_buffer_object *bo,
151 struct ttm_placement *placement);
154 * struct ttm_bo_driver member move:
156 * @bo: the buffer to move
157 * @evict: whether this motion is evicting the buffer from
158 * the graphics address space
159 * @ctx: context for this move with parameters
160 * @new_mem: the new memory region receiving the buffer
162 * Move a buffer between two memory regions.
164 int (*move)(struct ttm_buffer_object *bo, bool evict,
165 struct ttm_operation_ctx *ctx,
166 struct ttm_resource *new_mem);
169 * struct ttm_bo_driver_member verify_access
171 * @bo: Pointer to a buffer object.
172 * @filp: Pointer to a struct file trying to access the object.
174 * Called from the map / write / read methods to verify that the
175 * caller is permitted to access the buffer object.
176 * This member may be set to NULL, which will refuse this kind of
177 * access for all buffer objects.
178 * This function should return 0 if access is granted, -EPERM otherwise.
180 int (*verify_access)(struct ttm_buffer_object *bo,
184 * Hook to notify driver about a driver move so it
185 * can do tiling things and book-keeping.
187 * @evict: whether this move is evicting the buffer from the graphics
190 void (*move_notify)(struct ttm_buffer_object *bo,
192 struct ttm_resource *new_mem);
195 * notify the driver that we're about to swap out this bo
197 void (*swap_notify)(struct ttm_buffer_object *bo);
200 * Driver callback on when mapping io memory (for bo_move_memcpy
201 * for instance). TTM will take care to call io_mem_free whenever
202 * the mapping is not use anymore. io_mem_reserve & io_mem_free
205 int (*io_mem_reserve)(struct ttm_bo_device *bdev,
206 struct ttm_resource *mem);
207 void (*io_mem_free)(struct ttm_bo_device *bdev,
208 struct ttm_resource *mem);
211 * Return the pfn for a given page_offset inside the BO.
213 * @bo: the BO to look up the pfn for
214 * @page_offset: the offset to look up
216 unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
217 unsigned long page_offset);
220 * Read/write memory buffers for ptrace access
222 * @bo: the BO to access
223 * @offset: the offset from the start of the BO
224 * @buf: pointer to source/destination buffer
225 * @len: number of bytes to copy
226 * @write: whether to read (0) from or write (non-0) to BO
228 * If successful, this function should return the number of
229 * bytes copied, -EIO otherwise. If the number of bytes
230 * returned is < len, the function may be called again with
231 * the remainder of the buffer to copy.
233 int (*access_memory)(struct ttm_buffer_object *bo, unsigned long offset,
234 void *buf, int len, int write);
237 * struct ttm_bo_driver member del_from_lru_notify
239 * @bo: the buffer object deleted from lru
241 * notify driver that a BO was deleted from LRU.
243 void (*del_from_lru_notify)(struct ttm_buffer_object *bo);
246 * Notify the driver that we're about to release a BO
248 * @bo: BO that is about to be released
250 * Gives the driver a chance to do any cleanup, including
251 * adding fences that may force a delayed delete
253 void (*release_notify)(struct ttm_buffer_object *bo);
257 * struct ttm_bo_global - Buffer object driver global data.
259 * @dummy_read_page: Pointer to a dummy page used for mapping requests
260 * of unpopulated pages.
261 * @shrink: A shrink callback object used for buffer object swap.
262 * @device_list_mutex: Mutex protecting the device list.
263 * This mutex is held while traversing the device list for pm options.
264 * @lru_lock: Spinlock protecting the bo subsystem lru lists.
265 * @device_list: List of buffer object devices.
266 * @swap_lru: Lru list of buffer objects used for swapping.
269 extern struct ttm_bo_global {
272 * Constant after init.
276 struct page *dummy_read_page;
280 * Protected by ttm_global_mutex.
282 struct list_head device_list;
285 * Protected by the lru_lock.
287 struct list_head swap_lru[TTM_MAX_BO_PRIORITY];
290 * Internal protection.
296 #define TTM_NUM_MEM_TYPES 8
299 * struct ttm_bo_device - Buffer object driver device-specific data.
301 * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
302 * @man: An array of resource_managers.
303 * @vma_manager: Address space manager (pointer)
304 * lru_lock: Spinlock that protects the buffer+device lru lists and
306 * @dev_mapping: A pointer to the struct address_space representing the
307 * device address space.
308 * @wq: Work queue structure for the delayed delete workqueue.
309 * @no_retry: Don't retry allocation if it fails
313 struct ttm_bo_device {
316 * Constant after bo device init / atomic.
318 struct list_head device_list;
319 struct ttm_bo_driver *driver;
321 * access via ttm_manager_type.
323 struct ttm_resource_manager sysman;
324 struct ttm_resource_manager *man_drv[TTM_NUM_MEM_TYPES];
326 * Protected by internal locks.
328 struct drm_vma_offset_manager *vma_manager;
331 * Protected by the global:lru lock.
333 struct list_head ddestroy;
336 * Protected by load / firstopen / lastclose /unload sync.
339 struct address_space *dev_mapping;
342 * Internal protection.
345 struct delayed_work wq;
352 static inline struct ttm_resource_manager *ttm_manager_type(struct ttm_bo_device *bdev,
355 return bdev->man_drv[mem_type];
358 static inline void ttm_set_driver_manager(struct ttm_bo_device *bdev,
360 struct ttm_resource_manager *manager)
362 bdev->man_drv[type] = manager;
366 * struct ttm_lru_bulk_move_pos
368 * @first: first BO in the bulk move range
369 * @last: last BO in the bulk move range
371 * Positions for a lru bulk move.
373 struct ttm_lru_bulk_move_pos {
374 struct ttm_buffer_object *first;
375 struct ttm_buffer_object *last;
379 * struct ttm_lru_bulk_move
381 * @tt: first/last lru entry for BOs in the TT domain
382 * @vram: first/last lru entry for BOs in the VRAM domain
383 * @swap: first/last lru entry for BOs on the swap list
385 * Helper structure for bulk moves on the LRU list.
387 struct ttm_lru_bulk_move {
388 struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY];
389 struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY];
390 struct ttm_lru_bulk_move_pos swap[TTM_MAX_BO_PRIORITY];
400 * @bo: Pointer to a struct ttm_buffer_object. the data of which
401 * we want to allocate space for.
402 * @proposed_placement: Proposed new placement for the buffer object.
403 * @mem: A struct ttm_resource.
404 * @interruptible: Sleep interruptible when sliping.
405 * @no_wait_gpu: Return immediately if the GPU is busy.
407 * Allocate memory space for the buffer object pointed to by @bo, using
408 * the placement flags in @mem, potentially evicting other idle buffer objects.
409 * This function may sleep while waiting for space to become available.
411 * -EBUSY: No space available (only if no_wait == 1).
412 * -ENOMEM: Could not allocate memory for the buffer object, either due to
413 * fragmentation or concurrent allocators.
414 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
416 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
417 struct ttm_placement *placement,
418 struct ttm_resource *mem,
419 struct ttm_operation_ctx *ctx);
421 int ttm_bo_device_release(struct ttm_bo_device *bdev);
426 * @bdev: A pointer to a struct ttm_bo_device to initialize.
427 * @glob: A pointer to an initialized struct ttm_bo_global.
428 * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
429 * @mapping: The address space to use for this bo.
430 * @vma_manager: A pointer to a vma manager.
431 * @file_page_offset: Offset into the device address space that is available
432 * for buffer data. This ensures compatibility with other users of the
435 * Initializes a struct ttm_bo_device:
439 int ttm_bo_device_init(struct ttm_bo_device *bdev,
440 struct ttm_bo_driver *driver,
441 struct address_space *mapping,
442 struct drm_vma_offset_manager *vma_manager,
446 * ttm_bo_unmap_virtual
448 * @bo: tear down the virtual mappings for this BO
450 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
453 * ttm_bo_unmap_virtual
455 * @bo: tear down the virtual mappings for this BO
457 * The caller must take ttm_mem_io_lock before calling this function.
459 void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo);
464 * @bo: A pointer to a struct ttm_buffer_object.
465 * @interruptible: Sleep interruptible if waiting.
466 * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
467 * @ticket: ticket used to acquire the ww_mutex.
469 * Locks a buffer object for validation. (Or prevents other processes from
470 * locking it for validation), while taking a number of measures to prevent
474 * -EDEADLK: The reservation may cause a deadlock.
475 * Release all buffer reservations, wait for @bo to become unreserved and
477 * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
478 * a signal. Release all buffer reservations and return to user-space.
479 * -EBUSY: The function needed to sleep, but @no_wait was true
480 * -EALREADY: Bo already reserved using @ticket. This error code will only
481 * be returned if @use_ticket is set to true.
483 static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
484 bool interruptible, bool no_wait,
485 struct ww_acquire_ctx *ticket)
494 success = dma_resv_trylock(bo->base.resv);
495 return success ? 0 : -EBUSY;
499 ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
501 ret = dma_resv_lock(bo->base.resv, ticket);
508 * ttm_bo_reserve_slowpath:
509 * @bo: A pointer to a struct ttm_buffer_object.
510 * @interruptible: Sleep interruptible if waiting.
511 * @sequence: Set (@bo)->sequence to this value after lock
513 * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
514 * from all our other reservations. Because there are no other reservations
515 * held by us, this function cannot deadlock any more.
517 static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
519 struct ww_acquire_ctx *ticket)
522 int ret = dma_resv_lock_slow_interruptible(bo->base.resv,
528 dma_resv_lock_slow(bo->base.resv, ticket);
532 static inline void ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
534 spin_lock(&ttm_bo_glob.lru_lock);
535 ttm_bo_move_to_lru_tail(bo, NULL);
536 spin_unlock(&ttm_bo_glob.lru_lock);
539 static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
540 struct ttm_resource *new_mem)
543 new_mem->mm_node = NULL;
547 * ttm_bo_move_null = assign memory for a buffer object.
548 * @bo: The bo to assign the memory to
549 * @new_mem: The memory to be assigned.
551 * Assign the memory from new_mem to the memory of the buffer object bo.
553 static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
554 struct ttm_resource *new_mem)
556 struct ttm_resource *old_mem = &bo->mem;
558 WARN_ON(old_mem->mm_node != NULL);
559 ttm_bo_assign_mem(bo, new_mem);
565 * @bo: A pointer to a struct ttm_buffer_object.
567 * Unreserve a previous reservation of @bo.
569 static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
571 ttm_bo_move_to_lru_tail_unlocked(bo);
572 dma_resv_unlock(bo->base.resv);
579 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
580 struct ttm_resource *mem);
581 void ttm_mem_io_free(struct ttm_bo_device *bdev,
582 struct ttm_resource *mem);
586 * @bo: A pointer to a struct ttm_buffer_object.
587 * @interruptible: Sleep interruptible if waiting.
588 * @no_wait_gpu: Return immediately if the GPU is busy.
589 * @new_mem: struct ttm_resource indicating where to move.
591 * Optimized move function for a buffer object with both old and
592 * new placement backed by a TTM. The function will, if successful,
593 * free any old aperture space, and set (@new_mem)->mm_node to NULL,
594 * and update the (@bo)->mem placement flags. If unsuccessful, the old
595 * data remains untouched, and it's up to the caller to free the
596 * memory space indicated by @new_mem.
601 int ttm_bo_move_ttm(struct ttm_buffer_object *bo,
602 struct ttm_operation_ctx *ctx,
603 struct ttm_resource *new_mem);
608 * @bo: A pointer to a struct ttm_buffer_object.
609 * @interruptible: Sleep interruptible if waiting.
610 * @no_wait_gpu: Return immediately if the GPU is busy.
611 * @new_mem: struct ttm_resource indicating where to move.
613 * Fallback move function for a mappable buffer object in mappable memory.
614 * The function will, if successful,
615 * free any old aperture space, and set (@new_mem)->mm_node to NULL,
616 * and update the (@bo)->mem placement flags. If unsuccessful, the old
617 * data remains untouched, and it's up to the caller to free the
618 * memory space indicated by @new_mem.
623 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
624 struct ttm_operation_ctx *ctx,
625 struct ttm_resource *new_mem);
628 * ttm_bo_free_old_node
630 * @bo: A pointer to a struct ttm_buffer_object.
632 * Utility function to free an old placement after a successful move.
634 void ttm_bo_free_old_node(struct ttm_buffer_object *bo);
637 * ttm_bo_move_accel_cleanup.
639 * @bo: A pointer to a struct ttm_buffer_object.
640 * @fence: A fence object that signals when moving is complete.
641 * @evict: This is an evict move. Don't return until the buffer is idle.
642 * @pipeline: evictions are to be pipelined.
643 * @new_mem: struct ttm_resource indicating where to move.
645 * Accelerated move function to be called when an accelerated move
646 * has been scheduled. The function will create a new temporary buffer object
647 * representing the old placement, and put the sync object on both buffer
648 * objects. After that the newly created buffer object is unref'd to be
649 * destroyed when the move is complete. This will help pipeline
652 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
653 struct dma_fence *fence, bool evict,
655 struct ttm_resource *new_mem);
658 * ttm_bo_pipeline_gutting.
660 * @bo: A pointer to a struct ttm_buffer_object.
662 * Pipelined gutting a BO of its backing store.
664 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
669 * @c_state: Caching state.
670 * @tmp: Page protection flag for a normal, cached mapping.
672 * Utility function that returns the pgprot_t that should be used for
673 * setting up a PTE with the caching model indicated by @c_state.
675 pgprot_t ttm_io_prot(uint32_t caching_flags, pgprot_t tmp);
680 * Bind the object tt to a memory resource.
682 int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem);
687 * Unbind the object tt from a memory resource.
689 void ttm_bo_tt_unbind(struct ttm_buffer_object *bo);
694 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
700 * @type: memory manager type
701 * @use_tt: if the memory manager uses tt
702 * @p_size: size of area to be managed in pages.
704 * Initialise a generic range manager for the selected memory type.
705 * The range manager is installed for this device in the type slot.
707 int ttm_range_man_init(struct ttm_bo_device *bdev,
708 unsigned type, bool use_tt,
709 unsigned long p_size);
715 * @type: memory manager type
717 * Remove the generic range manager from a slot and tear it down.
719 int ttm_range_man_fini(struct ttm_bo_device *bdev,