Merge tag 'nfsd-5.13-1' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux
[linux-2.6-microblaze.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 <drm/ttm/ttm_device.h>
41
42 #include "ttm_bo_api.h"
43 #include "ttm_placement.h"
44 #include "ttm_tt.h"
45 #include "ttm_pool.h"
46
47 /**
48  * struct ttm_lru_bulk_move_pos
49  *
50  * @first: first BO in the bulk move range
51  * @last: last BO in the bulk move range
52  *
53  * Positions for a lru bulk move.
54  */
55 struct ttm_lru_bulk_move_pos {
56         struct ttm_buffer_object *first;
57         struct ttm_buffer_object *last;
58 };
59
60 /**
61  * struct ttm_lru_bulk_move
62  *
63  * @tt: first/last lru entry for BOs in the TT domain
64  * @vram: first/last lru entry for BOs in the VRAM domain
65  * @swap: first/last lru entry for BOs on the swap list
66  *
67  * Helper structure for bulk moves on the LRU list.
68  */
69 struct ttm_lru_bulk_move {
70         struct ttm_lru_bulk_move_pos tt[TTM_MAX_BO_PRIORITY];
71         struct ttm_lru_bulk_move_pos vram[TTM_MAX_BO_PRIORITY];
72 };
73
74 /*
75  * ttm_bo.c
76  */
77
78 /**
79  * ttm_bo_mem_space
80  *
81  * @bo: Pointer to a struct ttm_buffer_object. the data of which
82  * we want to allocate space for.
83  * @proposed_placement: Proposed new placement for the buffer object.
84  * @mem: A struct ttm_resource.
85  * @interruptible: Sleep interruptible when sliping.
86  * @no_wait_gpu: Return immediately if the GPU is busy.
87  *
88  * Allocate memory space for the buffer object pointed to by @bo, using
89  * the placement flags in @mem, potentially evicting other idle buffer objects.
90  * This function may sleep while waiting for space to become available.
91  * Returns:
92  * -EBUSY: No space available (only if no_wait == 1).
93  * -ENOMEM: Could not allocate memory for the buffer object, either due to
94  * fragmentation or concurrent allocators.
95  * -ERESTARTSYS: An interruptible sleep was interrupted by a signal.
96  */
97 int ttm_bo_mem_space(struct ttm_buffer_object *bo,
98                      struct ttm_placement *placement,
99                      struct ttm_resource *mem,
100                      struct ttm_operation_ctx *ctx);
101
102 /**
103  * ttm_bo_unmap_virtual
104  *
105  * @bo: tear down the virtual mappings for this BO
106  */
107 void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo);
108
109 /**
110  * ttm_bo_reserve:
111  *
112  * @bo: A pointer to a struct ttm_buffer_object.
113  * @interruptible: Sleep interruptible if waiting.
114  * @no_wait: Don't sleep while trying to reserve, rather return -EBUSY.
115  * @ticket: ticket used to acquire the ww_mutex.
116  *
117  * Locks a buffer object for validation. (Or prevents other processes from
118  * locking it for validation), while taking a number of measures to prevent
119  * deadlocks.
120  *
121  * Returns:
122  * -EDEADLK: The reservation may cause a deadlock.
123  * Release all buffer reservations, wait for @bo to become unreserved and
124  * try again.
125  * -ERESTARTSYS: A wait for the buffer to become unreserved was interrupted by
126  * a signal. Release all buffer reservations and return to user-space.
127  * -EBUSY: The function needed to sleep, but @no_wait was true
128  * -EALREADY: Bo already reserved using @ticket. This error code will only
129  * be returned if @use_ticket is set to true.
130  */
131 static inline int ttm_bo_reserve(struct ttm_buffer_object *bo,
132                                  bool interruptible, bool no_wait,
133                                  struct ww_acquire_ctx *ticket)
134 {
135         int ret = 0;
136
137         if (no_wait) {
138                 bool success;
139                 if (WARN_ON(ticket))
140                         return -EBUSY;
141
142                 success = dma_resv_trylock(bo->base.resv);
143                 return success ? 0 : -EBUSY;
144         }
145
146         if (interruptible)
147                 ret = dma_resv_lock_interruptible(bo->base.resv, ticket);
148         else
149                 ret = dma_resv_lock(bo->base.resv, ticket);
150         if (ret == -EINTR)
151                 return -ERESTARTSYS;
152         return ret;
153 }
154
155 /**
156  * ttm_bo_reserve_slowpath:
157  * @bo: A pointer to a struct ttm_buffer_object.
158  * @interruptible: Sleep interruptible if waiting.
159  * @sequence: Set (@bo)->sequence to this value after lock
160  *
161  * This is called after ttm_bo_reserve returns -EAGAIN and we backed off
162  * from all our other reservations. Because there are no other reservations
163  * held by us, this function cannot deadlock any more.
164  */
165 static inline int ttm_bo_reserve_slowpath(struct ttm_buffer_object *bo,
166                                           bool interruptible,
167                                           struct ww_acquire_ctx *ticket)
168 {
169         if (interruptible) {
170                 int ret = dma_resv_lock_slow_interruptible(bo->base.resv,
171                                                            ticket);
172                 if (ret == -EINTR)
173                         ret = -ERESTARTSYS;
174                 return ret;
175         }
176         dma_resv_lock_slow(bo->base.resv, ticket);
177         return 0;
178 }
179
180 static inline void
181 ttm_bo_move_to_lru_tail_unlocked(struct ttm_buffer_object *bo)
182 {
183         spin_lock(&bo->bdev->lru_lock);
184         ttm_bo_move_to_lru_tail(bo, &bo->mem, NULL);
185         spin_unlock(&bo->bdev->lru_lock);
186 }
187
188 static inline void ttm_bo_assign_mem(struct ttm_buffer_object *bo,
189                                      struct ttm_resource *new_mem)
190 {
191         bo->mem = *new_mem;
192         new_mem->mm_node = NULL;
193 }
194
195 /**
196  * ttm_bo_move_null = assign memory for a buffer object.
197  * @bo: The bo to assign the memory to
198  * @new_mem: The memory to be assigned.
199  *
200  * Assign the memory from new_mem to the memory of the buffer object bo.
201  */
202 static inline void ttm_bo_move_null(struct ttm_buffer_object *bo,
203                                     struct ttm_resource *new_mem)
204 {
205         struct ttm_resource *old_mem = &bo->mem;
206
207         WARN_ON(old_mem->mm_node != NULL);
208         ttm_bo_assign_mem(bo, new_mem);
209 }
210
211 /**
212  * ttm_bo_unreserve
213  *
214  * @bo: A pointer to a struct ttm_buffer_object.
215  *
216  * Unreserve a previous reservation of @bo.
217  */
218 static inline void ttm_bo_unreserve(struct ttm_buffer_object *bo)
219 {
220         ttm_bo_move_to_lru_tail_unlocked(bo);
221         dma_resv_unlock(bo->base.resv);
222 }
223
224 /*
225  * ttm_bo_util.c
226  */
227 int ttm_mem_io_reserve(struct ttm_device *bdev,
228                        struct ttm_resource *mem);
229 void ttm_mem_io_free(struct ttm_device *bdev,
230                      struct ttm_resource *mem);
231
232 /**
233  * ttm_bo_move_memcpy
234  *
235  * @bo: A pointer to a struct ttm_buffer_object.
236  * @interruptible: Sleep interruptible if waiting.
237  * @no_wait_gpu: Return immediately if the GPU is busy.
238  * @new_mem: struct ttm_resource indicating where to move.
239  *
240  * Fallback move function for a mappable buffer object in mappable memory.
241  * The function will, if successful,
242  * free any old aperture space, and set (@new_mem)->mm_node to NULL,
243  * and update the (@bo)->mem placement flags. If unsuccessful, the old
244  * data remains untouched, and it's up to the caller to free the
245  * memory space indicated by @new_mem.
246  * Returns:
247  * !0: Failure.
248  */
249
250 int ttm_bo_move_memcpy(struct ttm_buffer_object *bo,
251                        struct ttm_operation_ctx *ctx,
252                        struct ttm_resource *new_mem);
253
254 /**
255  * ttm_bo_move_accel_cleanup.
256  *
257  * @bo: A pointer to a struct ttm_buffer_object.
258  * @fence: A fence object that signals when moving is complete.
259  * @evict: This is an evict move. Don't return until the buffer is idle.
260  * @pipeline: evictions are to be pipelined.
261  * @new_mem: struct ttm_resource indicating where to move.
262  *
263  * Accelerated move function to be called when an accelerated move
264  * has been scheduled. The function will create a new temporary buffer object
265  * representing the old placement, and put the sync object on both buffer
266  * objects. After that the newly created buffer object is unref'd to be
267  * destroyed when the move is complete. This will help pipeline
268  * buffer moves.
269  */
270 int ttm_bo_move_accel_cleanup(struct ttm_buffer_object *bo,
271                               struct dma_fence *fence, bool evict,
272                               bool pipeline,
273                               struct ttm_resource *new_mem);
274
275 /**
276  * ttm_bo_pipeline_gutting.
277  *
278  * @bo: A pointer to a struct ttm_buffer_object.
279  *
280  * Pipelined gutting a BO of its backing store.
281  */
282 int ttm_bo_pipeline_gutting(struct ttm_buffer_object *bo);
283
284 /**
285  * ttm_io_prot
286  *
287  * bo: ttm buffer object
288  * res: ttm resource object
289  * @tmp: Page protection flag for a normal, cached mapping.
290  *
291  * Utility function that returns the pgprot_t that should be used for
292  * setting up a PTE with the caching model indicated by @c_state.
293  */
294 pgprot_t ttm_io_prot(struct ttm_buffer_object *bo, struct ttm_resource *res,
295                      pgprot_t tmp);
296
297 /**
298  * ttm_bo_tt_bind
299  *
300  * Bind the object tt to a memory resource.
301  */
302 int ttm_bo_tt_bind(struct ttm_buffer_object *bo, struct ttm_resource *mem);
303
304 /**
305  * ttm_bo_tt_destroy.
306  */
307 void ttm_bo_tt_destroy(struct ttm_buffer_object *bo);
308
309 /**
310  * ttm_range_man_init
311  *
312  * @bdev: ttm device
313  * @type: memory manager type
314  * @use_tt: if the memory manager uses tt
315  * @p_size: size of area to be managed in pages.
316  *
317  * Initialise a generic range manager for the selected memory type.
318  * The range manager is installed for this device in the type slot.
319  */
320 int ttm_range_man_init(struct ttm_device *bdev,
321                        unsigned type, bool use_tt,
322                        unsigned long p_size);
323
324 /**
325  * ttm_range_man_fini
326  *
327  * @bdev: ttm device
328  * @type: memory manager type
329  *
330  * Remove the generic range manager from a slot and tear it down.
331  */
332 int ttm_range_man_fini(struct ttm_device *bdev,
333                        unsigned type);
334
335 #endif