]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - include/drm/ttm/ttm_bo_driver.h
72f106b335e94555888c433e96f769964ba6d810
[mirror_ubuntu-jammy-kernel.git] / include / drm / ttm / ttm_bo_driver.h
1 /**************************************************************************
2 *
3 * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27 /*
28 * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29 */
30 #ifndef _TTM_BO_DRIVER_H_
31 #define _TTM_BO_DRIVER_H_
32
33 #include <drm/drm_mm.h>
34 #include <drm/drm_vma_manager.h>
35 #include <linux/workqueue.h>
36 #include <linux/fs.h>
37 #include <linux/spinlock.h>
38 #include <linux/dma-resv.h>
39
40 #include "ttm_bo_api.h"
41 #include "ttm_memory.h"
42 #include "ttm_module.h"
43 #include "ttm_placement.h"
44 #include "ttm_tt.h"
45
46 /**
47 * struct ttm_bo_driver
48 *
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
52 * move a buffer.
53 * If set to NULL, a potentially slow memcpy() move is used.
54 */
55
56 struct ttm_bo_driver {
57 /**
58 * ttm_tt_create
59 *
60 * @bo: The buffer object to create the ttm for.
61 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
62 *
63 * Create a struct ttm_tt to back data with system memory pages.
64 * No pages are actually allocated.
65 * Returns:
66 * NULL: Out of memory.
67 */
68 struct ttm_tt *(*ttm_tt_create)(struct ttm_buffer_object *bo,
69 uint32_t page_flags);
70
71 /**
72 * ttm_tt_populate
73 *
74 * @ttm: The struct ttm_tt to contain the backing pages.
75 *
76 * Allocate all backing pages
77 * Returns:
78 * -ENOMEM: Out of memory.
79 */
80 int (*ttm_tt_populate)(struct ttm_bo_device *bdev,
81 struct ttm_tt *ttm,
82 struct ttm_operation_ctx *ctx);
83
84 /**
85 * ttm_tt_unpopulate
86 *
87 * @ttm: The struct ttm_tt to contain the backing pages.
88 *
89 * Free all backing page
90 */
91 void (*ttm_tt_unpopulate)(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
92
93 /**
94 * ttm_tt_destroy
95 *
96 * @bdev: Pointer to a ttm device
97 * @ttm: Pointer to a struct ttm_tt.
98 *
99 * Destroy the backend. This will be call back from ttm_tt_destroy so
100 * don't call ttm_tt_destroy from the callback or infinite loop.
101 */
102 void (*ttm_tt_destroy)(struct ttm_bo_device *bdev, struct ttm_tt *ttm);
103
104 /**
105 * struct ttm_bo_driver member eviction_valuable
106 *
107 * @bo: the buffer object to be evicted
108 * @place: placement we need room for
109 *
110 * Check with the driver if it is valuable to evict a BO to make room
111 * for a certain placement.
112 */
113 bool (*eviction_valuable)(struct ttm_buffer_object *bo,
114 const struct ttm_place *place);
115 /**
116 * struct ttm_bo_driver member evict_flags:
117 *
118 * @bo: the buffer object to be evicted
119 *
120 * Return the bo flags for a buffer which is not mapped to the hardware.
121 * These will be placed in proposed_flags so that when the move is
122 * finished, they'll end up in bo->mem.flags
123 */
124
125 void (*evict_flags)(struct ttm_buffer_object *bo,
126 struct ttm_placement *placement);
127
128 /**
129 * struct ttm_bo_driver member move:
130 *
131 * @bo: the buffer to move
132 * @evict: whether this motion is evicting the buffer from
133 * the graphics address space
134 * @ctx: context for this move with parameters
135 * @new_mem: the new memory region receiving the buffer
136 *
137 * Move a buffer between two memory regions.
138 */
139 int (*move)(struct ttm_buffer_object *bo, bool evict,
140 struct ttm_operation_ctx *ctx,
141 struct ttm_resource *new_mem);
142
143 /**
144 * struct ttm_bo_driver_member verify_access
145 *
146 * @bo: Pointer to a buffer object.
147 * @filp: Pointer to a struct file trying to access the object.
148 *
149 * Called from the map / write / read methods to verify that the
150 * caller is permitted to access the buffer object.
151 * This member may be set to NULL, which will refuse this kind of
152 * access for all buffer objects.
153 * This function should return 0 if access is granted, -EPERM otherwise.
154 */
155 int (*verify_access)(struct ttm_buffer_object *bo,
156 struct file *filp);
157
158 /**
159 * Hook to notify driver about a driver move so it
160 * can do tiling things and book-keeping.
161 *
162 * @evict: whether this move is evicting the buffer from the graphics
163 * address space
164 */
165 void (*move_notify)(struct ttm_buffer_object *bo,
166 bool evict,
167 struct ttm_resource *new_mem);
168
169 /**
170 * notify the driver that we're about to swap out this bo
171 */
172 void (*swap_notify)(struct ttm_buffer_object *bo);
173
174 /**
175 * Driver callback on when mapping io memory (for bo_move_memcpy
176 * for instance). TTM will take care to call io_mem_free whenever
177 * the mapping is not use anymore. io_mem_reserve & io_mem_free
178 * are balanced.
179 */
180 int (*io_mem_reserve)(struct ttm_bo_device *bdev,
181 struct ttm_resource *mem);
182 void (*io_mem_free)(struct ttm_bo_device *bdev,
183 struct ttm_resource *mem);
184
185 /**
186 * Return the pfn for a given page_offset inside the BO.
187 *
188 * @bo: the BO to look up the pfn for
189 * @page_offset: the offset to look up
190 */
191 unsigned long (*io_mem_pfn)(struct ttm_buffer_object *bo,
192 unsigned long page_offset);
193
194 /**
195 * Read/write memory buffers for ptrace access
196 *
197 * @bo: the BO to access
198 * @offset: the offset from the start of the BO
199 * @buf: pointer to source/destination buffer
200 * @len: number of bytes to copy
201 * @write: whether to read (0) from or write (non-0) to BO
202 *
203 * If successful, this function should return the number of
204 * bytes copied, -EIO otherwise. If the number of bytes
205 * returned is < len, the function may be called again with
206 * the remainder of the buffer to copy.
207 */
208 int (*access_memory)(struct ttm_buffer_object *bo, unsigned long offset,
209 void *buf, int len, int write);
210
211 /**
212 * struct ttm_bo_driver member del_from_lru_notify
213 *
214 * @bo: the buffer object deleted from lru
215 *
216 * notify driver that a BO was deleted from LRU.
217 */
218 void (*del_from_lru_notify)(struct ttm_buffer_object *bo);
219
220 /**
221 * Notify the driver that we're about to release a BO
222 *
223 * @bo: BO that is about to be released
224 *
225 * Gives the driver a chance to do any cleanup, including
226 * adding fences that may force a delayed delete
227 */
228 void (*release_notify)(struct ttm_buffer_object *bo);
229 };
230
231 /**
232 * struct ttm_bo_global - Buffer object driver global data.
233 *
234 * @dummy_read_page: Pointer to a dummy page used for mapping requests
235 * of unpopulated pages.
236 * @shrink: A shrink callback object used for buffer object swap.
237 * @device_list_mutex: Mutex protecting the device list.
238 * This mutex is held while traversing the device list for pm options.
239 * @lru_lock: Spinlock protecting the bo subsystem lru lists.
240 * @device_list: List of buffer object devices.
241 * @swap_lru: Lru list of buffer objects used for swapping.
242 */
243
244 extern struct ttm_bo_global {
245
246 /**
247 * Constant after init.
248 */
249
250 struct kobject kobj;
251 struct page *dummy_read_page;
252 spinlock_t lru_lock;
253
254 /**
255 * Protected by ttm_global_mutex.
256 */
257 struct list_head device_list;
258
259 /**
260 * Protected by the lru_lock.
261 */
262 struct list_head swap_lru[TTM_MAX_BO_PRIORITY];
263
264 /**
265 * Internal protection.
266 */
267 atomic_t bo_count;
268 } ttm_bo_glob;
269
270
271 #define TTM_NUM_MEM_TYPES 8
272
273 /**
274 * struct ttm_bo_device - Buffer object driver device-specific data.
275 *
276 * @driver: Pointer to a struct ttm_bo_driver struct setup by the driver.
277 * @man: An array of resource_managers.
278 * @vma_manager: Address space manager (pointer)
279 * lru_lock: Spinlock that protects the buffer+device lru lists and
280 * ddestroy lists.
281 * @dev_mapping: A pointer to the struct address_space representing the
282 * device address space.
283 * @wq: Work queue structure for the delayed delete workqueue.
284 * @no_retry: Don't retry allocation if it fails
285 *
286 */
287
288 struct ttm_bo_device {
289
290 /*
291 * Constant after bo device init / atomic.
292 */
293 struct list_head device_list;
294 struct ttm_bo_driver *driver;
295 /*
296 * access via ttm_manager_type.
297 */
298 struct ttm_resource_manager sysman;
299 struct ttm_resource_manager *man_drv[TTM_NUM_MEM_TYPES];
300 /*
301 * Protected by internal locks.
302 */
303 struct drm_vma_offset_manager *vma_manager;
304
305 /*
306 * Protected by the global:lru lock.
307 */
308 struct list_head ddestroy;
309
310 /*
311 * Protected by load / firstopen / lastclose /unload sync.
312 */
313
314 struct address_space *dev_mapping;
315
316 /*
317 * Internal protection.
318 */
319
320 struct delayed_work wq;
321
322 bool need_dma32;
323
324 bool no_retry;
325 };
326
327 static inline struct ttm_resource_manager *ttm_manager_type(struct ttm_bo_device *bdev,
328 int mem_type)
329 {
330 return bdev->man_drv[mem_type];
331 }
332
333 static inline void ttm_set_driver_manager(struct ttm_bo_device *bdev,
334 int type,
335 struct ttm_resource_manager *manager)
336 {
337 bdev->man_drv[type] = manager;
338 }
339
340 /**
341 * struct ttm_lru_bulk_move_pos
342 *
343 * @first: first BO in the bulk move range
344 * @last: last BO in the bulk move range
345 *
346 * Positions for a lru bulk move.
347 */
348 struct ttm_lru_bulk_move_pos {
349 struct ttm_buffer_object *first;
350 struct ttm_buffer_object *last;
351 };
352
353 /**
354 * struct ttm_lru_bulk_move
355 *
356 * @tt: first/last lru entry for BOs in the TT domain
357 * @vram: first/last lru entry for BOs in the VRAM domain
358 * @swap: first/last lru entry for BOs on the swap list
359 *
360 * Helper structure for bulk moves on the LRU list.
361 */
362 struct ttm_lru_bulk_move {
363 struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY];
364 struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY];
365 struct ttm_lru_bulk_move_pos swap[TTM_MAX_BO_PRIORITY];
366 };
367
368 /*
369 * ttm_bo.c
370 */
371
372 /**
373 * ttm_bo_mem_space
374 *
375 * @bo: Pointer to a struct ttm_buffer_object. the data of which
376 * we want to allocate space for.
377 * @proposed_placement: Proposed new placement for the buffer object.
378 * @mem: A struct ttm_resource.
379 * @interruptible: Sleep interruptible when sliping.
380 * @no_wait_gpu: Return immediately if the GPU is busy.
381 *
382 * Allocate memory space for the buffer object pointed to by @bo, using
383 * the placement flags in @mem, potentially evicting other idle buffer objects.
384 * This function may sleep while waiting for space to become available.
385 * Returns:
386 * -EBUSY: No space available (only if no_wait == 1).
387 * -ENOMEM: Could not allocate memory for the buffer object, either due to
388 * fragmentation or concurrent allocators.
389 * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
390 */
391 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
392 struct ttm_placement *placement,
393 struct ttm_resource *mem,
394 struct ttm_operation_ctx *ctx);
395
396 int ttm_bo_device_release(struct ttm_bo_device *bdev);
397
398 /**
399 * ttm_bo_device_init
400 *
401 * @bdev: A pointer to a struct ttm_bo_device to initialize.
402 * @glob: A pointer to an initialized struct ttm_bo_global.
403 * @driver: A pointer to a struct ttm_bo_driver set up by the caller.
404 * @mapping: The address space to use for this bo.
405 * @vma_manager: A pointer to a vma manager.
406 * @file_page_offset: Offset into the device address space that is available
407 * for buffer data. This ensures compatibility with other users of the
408 * address space.
409 *
410 * Initializes a struct ttm_bo_device:
411 * Returns:
412 * !0: Failure.
413 */
414 int ttm_bo_device_init(struct ttm_bo_device *bdev,
415 struct ttm_bo_driver *driver,
416 struct address_space *mapping,
417 struct drm_vma_offset_manager *vma_manager,
418 bool need_dma32);
419
420 /**
421 * ttm_bo_unmap_virtual
422 *
423 * @bo: tear down the virtual mappings for this BO
424 */
425 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
426
427 /**
428 * ttm_bo_reserve:
429 *
430 * @bo: A pointer to a struct ttm_buffer_object.
431 * @interruptible: Sleep interruptible if waiting.
432 * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
433 * @ticket: ticket used to acquire the ww_mutex.
434 *
435 * Locks a buffer object for validation. (Or prevents other processes from
436 * locking it for validation), while taking a number of measures to prevent
437 * deadlocks.
438 *
439 * Returns:
440 * -EDEADLK: The reservation may cause a deadlock.
441 * Release all buffer reservations, wait for @bo to become unreserved and
442 * try again.
443 * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
444 * a signal. Release all buffer reservations and return to user-space.
445 * -EBUSY: The function needed to sleep, but @no_wait was true
446 * -EALREADY: Bo already reserved using @ticket. This error code will only
447 * be returned if @use_ticket is set to true.
448 */
449 static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
450 bool interruptible, bool no_wait,
451 struct ww_acquire_ctx *ticket)
452 {
453 int ret = 0;
454
455 if (no_wait) {
456 bool success;
457 if (WARN_ON(ticket))
458 return -EBUSY;
459
460 success = dma_resv_trylock(bo->base.resv);
461 return success ? 0 : -EBUSY;
462 }
463
464 if (interruptible)
465 ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
466 else
467 ret = dma_resv_lock(bo->base.resv, ticket);
468 if (ret == -EINTR)
469 return -ERESTARTSYS;
470 return ret;
471 }
472
473 /**
474 * ttm_bo_reserve_slowpath:
475 * @bo: A pointer to a struct ttm_buffer_object.
476 * @interruptible: Sleep interruptible if waiting.
477 * @sequence: Set (@bo)->sequence to this value after lock
478 *
479 * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
480 * from all our other reservations. Because there are no other reservations
481 * held by us, this function cannot deadlock any more.
482 */
483 static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
484 bool interruptible,
485 struct ww_acquire_ctx *ticket)
486 {
487 if (interruptible) {
488 int ret = dma_resv_lock_slow_interruptible(bo->base.resv,
489 ticket);
490 if (ret == -EINTR)
491 ret = -ERESTARTSYS;
492 return ret;
493 }
494 dma_resv_lock_slow(bo->base.resv, ticket);
495 return 0;
496 }
497
498 static inline void ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
499 {
500 spin_lock(&ttm_bo_glob.lru_lock);
501 ttm_bo_move_to_lru_tail(bo, NULL);
502 spin_unlock(&ttm_bo_glob.lru_lock);
503 }
504
505 static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
506 struct ttm_resource *new_mem)
507 {
508 bo->mem = *new_mem;
509 new_mem->mm_node = NULL;
510 }
511
512 /**
513 * ttm_bo_move_null = assign memory for a buffer object.
514 * @bo: The bo to assign the memory to
515 * @new_mem: The memory to be assigned.
516 *
517 * Assign the memory from new_mem to the memory of the buffer object bo.
518 */
519 static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
520 struct ttm_resource *new_mem)
521 {
522 struct ttm_resource *old_mem = &bo->mem;
523
524 WARN_ON(old_mem->mm_node != NULL);
525 ttm_bo_assign_mem(bo, new_mem);
526 }
527
528 /**
529 * ttm_bo_unreserve
530 *
531 * @bo: A pointer to a struct ttm_buffer_object.
532 *
533 * Unreserve a previous reservation of @bo.
534 */
535 static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
536 {
537 ttm_bo_move_to_lru_tail_unlocked(bo);
538 dma_resv_unlock(bo->base.resv);
539 }
540
541 /*
542 * ttm_bo_util.c
543 */
544 int ttm_mem_io_reserve(struct ttm_bo_device *bdev,
545 struct ttm_resource *mem);
546 void ttm_mem_io_free(struct ttm_bo_device *bdev,
547 struct ttm_resource *mem);
548
549 /**
550 * ttm_bo_move_memcpy
551 *
552 * @bo: A pointer to a struct ttm_buffer_object.
553 * @interruptible: Sleep interruptible if waiting.
554 * @no_wait_gpu: Return immediately if the GPU is busy.
555 * @new_mem: struct ttm_resource indicating where to move.
556 *
557 * Fallback move function for a mappable buffer object in mappable memory.
558 * The function will, if successful,
559 * free any old aperture space, and set (@new_mem)->mm_node to NULL,
560 * and update the (@bo)->mem placement flags. If unsuccessful, the old
561 * data remains untouched, and it's up to the caller to free the
562 * memory space indicated by @new_mem.
563 * Returns:
564 * !0: Failure.
565 */
566
567 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
568 struct ttm_operation_ctx *ctx,
569 struct ttm_resource *new_mem);
570
571 /**
572 * ttm_bo_move_accel_cleanup.
573 *
574 * @bo: A pointer to a struct ttm_buffer_object.
575 * @fence: A fence object that signals when moving is complete.
576 * @evict: This is an evict move. Don't return until the buffer is idle.
577 * @pipeline: evictions are to be pipelined.
578 * @new_mem: struct ttm_resource indicating where to move.
579 *
580 * Accelerated move function to be called when an accelerated move
581 * has been scheduled. The function will create a new temporary buffer object
582 * representing the old placement, and put the sync object on both buffer
583 * objects. After that the newly created buffer object is unref'd to be
584 * destroyed when the move is complete. This will help pipeline
585 * buffer moves.
586 */
587 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
588 struct dma_fence *fence, bool evict,
589 bool pipeline,
590 struct ttm_resource *new_mem);
591
592 /**
593 * ttm_bo_pipeline_gutting.
594 *
595 * @bo: A pointer to a struct ttm_buffer_object.
596 *
597 * Pipelined gutting a BO of its backing store.
598 */
599 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
600
601 /**
602 * ttm_io_prot
603 *
604 * bo: ttm buffer object
605 * res: ttm resource object
606 * @tmp: Page protection flag for a normal, cached mapping.
607 *
608 * Utility function that returns the pgprot_t that should be used for
609 * setting up a PTE with the caching model indicated by @c_state.
610 */
611 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
612 pgprot_t tmp);
613
614 /**
615 * ttm_bo_tt_bind
616 *
617 * Bind the object tt to a memory resource.
618 */
619 int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem);
620
621 /**
622 * ttm_bo_tt_destroy.
623 */
624 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
625
626 /**
627 * ttm_range_man_init
628 *
629 * @bdev: ttm device
630 * @type: memory manager type
631 * @use_tt: if the memory manager uses tt
632 * @p_size: size of area to be managed in pages.
633 *
634 * Initialise a generic range manager for the selected memory type.
635 * The range manager is installed for this device in the type slot.
636 */
637 int ttm_range_man_init(struct ttm_bo_device *bdev,
638 unsigned type, bool use_tt,
639 unsigned long p_size);
640
641 /**
642 * ttm_range_man_fini
643 *
644 * @bdev: ttm device
645 * @type: memory manager type
646 *
647 * Remove the generic range manager from a slot and tear it down.
648 */
649 int ttm_range_man_fini(struct ttm_bo_device *bdev,
650 unsigned type);
651
652 #endif