1*4882a593Smuzhiyun /**************************************************************************
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4*4882a593Smuzhiyun * All Rights Reserved.
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
7*4882a593Smuzhiyun * copy of this software and associated documentation files (the
8*4882a593Smuzhiyun * "Software"), to deal in the Software without restriction, including
9*4882a593Smuzhiyun * without limitation the rights to use, copy, modify, merge, publish,
10*4882a593Smuzhiyun * distribute, sub license, and/or sell copies of the Software, and to
11*4882a593Smuzhiyun * permit persons to whom the Software is furnished to do so, subject to
12*4882a593Smuzhiyun * the following conditions:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the
15*4882a593Smuzhiyun * next paragraph) shall be included in all copies or substantial portions
16*4882a593Smuzhiyun * of the Software.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21*4882a593Smuzhiyun * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22*4882a593Smuzhiyun * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23*4882a593Smuzhiyun * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24*4882a593Smuzhiyun * USE OR OTHER DEALINGS IN THE SOFTWARE.
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun **************************************************************************/
27*4882a593Smuzhiyun /*
28*4882a593Smuzhiyun * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29*4882a593Smuzhiyun */
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun #ifndef _TTM_BO_API_H_
32*4882a593Smuzhiyun #define _TTM_BO_API_H_
33*4882a593Smuzhiyun
34*4882a593Smuzhiyun #include <drm/drm_gem.h>
35*4882a593Smuzhiyun #include <drm/drm_hashtab.h>
36*4882a593Smuzhiyun #include <drm/drm_vma_manager.h>
37*4882a593Smuzhiyun #include <linux/kref.h>
38*4882a593Smuzhiyun #include <linux/list.h>
39*4882a593Smuzhiyun #include <linux/wait.h>
40*4882a593Smuzhiyun #include <linux/mutex.h>
41*4882a593Smuzhiyun #include <linux/mm.h>
42*4882a593Smuzhiyun #include <linux/bitmap.h>
43*4882a593Smuzhiyun #include <linux/dma-resv.h>
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun #include "ttm_resource.h"
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun struct ttm_bo_global;
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun struct ttm_bo_device;
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun struct drm_mm_node;
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun struct ttm_placement;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun struct ttm_place;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun struct ttm_lru_bulk_move;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /**
60*4882a593Smuzhiyun * enum ttm_bo_type
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * @ttm_bo_type_device: These are 'normal' buffers that can
63*4882a593Smuzhiyun * be mmapped by user space. Each of these bos occupy a slot in the
64*4882a593Smuzhiyun * device address space, that can be used for normal vm operations.
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
67*4882a593Smuzhiyun * but they cannot be accessed from user-space. For kernel-only use.
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
70*4882a593Smuzhiyun * driver.
71*4882a593Smuzhiyun */
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun enum ttm_bo_type {
74*4882a593Smuzhiyun ttm_bo_type_device,
75*4882a593Smuzhiyun ttm_bo_type_kernel,
76*4882a593Smuzhiyun ttm_bo_type_sg
77*4882a593Smuzhiyun };
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun struct ttm_tt;
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /**
82*4882a593Smuzhiyun * struct ttm_buffer_object
83*4882a593Smuzhiyun *
84*4882a593Smuzhiyun * @base: drm_gem_object superclass data.
85*4882a593Smuzhiyun * @bdev: Pointer to the buffer object device structure.
86*4882a593Smuzhiyun * @type: The bo type.
87*4882a593Smuzhiyun * @destroy: Destruction function. If NULL, kfree is used.
88*4882a593Smuzhiyun * @num_pages: Actual number of pages.
89*4882a593Smuzhiyun * @acc_size: Accounted size for this object.
90*4882a593Smuzhiyun * @kref: Reference count of this buffer object. When this refcount reaches
91*4882a593Smuzhiyun * zero, the object is destroyed or put on the delayed delete list.
92*4882a593Smuzhiyun * @mem: structure describing current placement.
93*4882a593Smuzhiyun * @persistent_swap_storage: Usually the swap storage is deleted for buffers
94*4882a593Smuzhiyun * pinned in physical memory. If this behaviour is not desired, this member
95*4882a593Smuzhiyun * holds a pointer to a persistent shmem object.
96*4882a593Smuzhiyun * @ttm: TTM structure holding system pages.
97*4882a593Smuzhiyun * @evicted: Whether the object was evicted without user-space knowing.
98*4882a593Smuzhiyun * @deleted: True if the object is only a zombie and already deleted.
99*4882a593Smuzhiyun * @lru: List head for the lru list.
100*4882a593Smuzhiyun * @ddestroy: List head for the delayed destroy list.
101*4882a593Smuzhiyun * @swap: List head for swap LRU list.
102*4882a593Smuzhiyun * @moving: Fence set when BO is moving
103*4882a593Smuzhiyun * @offset: The current GPU offset, which can have different meanings
104*4882a593Smuzhiyun * depending on the memory type. For SYSTEM type memory, it should be 0.
105*4882a593Smuzhiyun * @cur_placement: Hint of current placement.
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * Base class for TTM buffer object, that deals with data placement and CPU
108*4882a593Smuzhiyun * mappings. GPU mappings are really up to the driver, but for simpler GPUs
109*4882a593Smuzhiyun * the driver can usually use the placement offset @offset directly as the
110*4882a593Smuzhiyun * GPU virtual address. For drivers implementing multiple
111*4882a593Smuzhiyun * GPU memory manager contexts, the driver should manage the address space
112*4882a593Smuzhiyun * in these contexts separately and use these objects to get the correct
113*4882a593Smuzhiyun * placement and caching for these GPU maps. This makes it possible to use
114*4882a593Smuzhiyun * these objects for even quite elaborate memory management schemes.
115*4882a593Smuzhiyun * The destroy member, the API visibility of this object makes it possible
116*4882a593Smuzhiyun * to derive driver specific types.
117*4882a593Smuzhiyun */
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun struct ttm_buffer_object {
120*4882a593Smuzhiyun struct drm_gem_object base;
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /**
123*4882a593Smuzhiyun * Members constant at init.
124*4882a593Smuzhiyun */
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun struct ttm_bo_device *bdev;
127*4882a593Smuzhiyun enum ttm_bo_type type;
128*4882a593Smuzhiyun void (*destroy) (struct ttm_buffer_object *);
129*4882a593Smuzhiyun unsigned long num_pages;
130*4882a593Smuzhiyun size_t acc_size;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /**
133*4882a593Smuzhiyun * Members not needing protection.
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun struct kref kref;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /**
138*4882a593Smuzhiyun * Members protected by the bo::resv::reserved lock.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun struct ttm_resource mem;
142*4882a593Smuzhiyun struct file *persistent_swap_storage;
143*4882a593Smuzhiyun struct ttm_tt *ttm;
144*4882a593Smuzhiyun bool deleted;
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun /**
147*4882a593Smuzhiyun * Members protected by the bdev::lru_lock.
148*4882a593Smuzhiyun */
149*4882a593Smuzhiyun
150*4882a593Smuzhiyun struct list_head lru;
151*4882a593Smuzhiyun struct list_head ddestroy;
152*4882a593Smuzhiyun struct list_head swap;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun /**
155*4882a593Smuzhiyun * Members protected by a bo reservation.
156*4882a593Smuzhiyun */
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun struct dma_fence *moving;
159*4882a593Smuzhiyun unsigned priority;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /**
162*4882a593Smuzhiyun * Special members that are protected by the reserve lock
163*4882a593Smuzhiyun * and the bo::lock when written to. Can be read with
164*4882a593Smuzhiyun * either of these locks held.
165*4882a593Smuzhiyun */
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun struct sg_table *sg;
168*4882a593Smuzhiyun };
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun /**
171*4882a593Smuzhiyun * struct ttm_bo_kmap_obj
172*4882a593Smuzhiyun *
173*4882a593Smuzhiyun * @virtual: The current kernel virtual address.
174*4882a593Smuzhiyun * @page: The page when kmap'ing a single page.
175*4882a593Smuzhiyun * @bo_kmap_type: Type of bo_kmap.
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * Object describing a kernel mapping. Since a TTM bo may be located
178*4882a593Smuzhiyun * in various memory types with various caching policies, the
179*4882a593Smuzhiyun * mapping can either be an ioremap, a vmap, a kmap or part of a
180*4882a593Smuzhiyun * premapped region.
181*4882a593Smuzhiyun */
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun #define TTM_BO_MAP_IOMEM_MASK 0x80
184*4882a593Smuzhiyun struct ttm_bo_kmap_obj {
185*4882a593Smuzhiyun void *virtual;
186*4882a593Smuzhiyun struct page *page;
187*4882a593Smuzhiyun enum {
188*4882a593Smuzhiyun ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
189*4882a593Smuzhiyun ttm_bo_map_vmap = 2,
190*4882a593Smuzhiyun ttm_bo_map_kmap = 3,
191*4882a593Smuzhiyun ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
192*4882a593Smuzhiyun } bo_kmap_type;
193*4882a593Smuzhiyun struct ttm_buffer_object *bo;
194*4882a593Smuzhiyun };
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /**
197*4882a593Smuzhiyun * struct ttm_operation_ctx
198*4882a593Smuzhiyun *
199*4882a593Smuzhiyun * @interruptible: Sleep interruptible if sleeping.
200*4882a593Smuzhiyun * @no_wait_gpu: Return immediately if the GPU is busy.
201*4882a593Smuzhiyun * @resv: Reservation object to allow reserved evictions with.
202*4882a593Smuzhiyun * @flags: Including the following flags
203*4882a593Smuzhiyun *
204*4882a593Smuzhiyun * Context for TTM operations like changing buffer placement or general memory
205*4882a593Smuzhiyun * allocation.
206*4882a593Smuzhiyun */
207*4882a593Smuzhiyun struct ttm_operation_ctx {
208*4882a593Smuzhiyun bool interruptible;
209*4882a593Smuzhiyun bool no_wait_gpu;
210*4882a593Smuzhiyun struct dma_resv *resv;
211*4882a593Smuzhiyun uint64_t bytes_moved;
212*4882a593Smuzhiyun uint32_t flags;
213*4882a593Smuzhiyun };
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun /* Allow eviction of reserved BOs */
216*4882a593Smuzhiyun #define TTM_OPT_FLAG_ALLOW_RES_EVICT 0x1
217*4882a593Smuzhiyun /* when serving page fault or suspend, allow alloc anyway */
218*4882a593Smuzhiyun #define TTM_OPT_FLAG_FORCE_ALLOC 0x2
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun /**
221*4882a593Smuzhiyun * ttm_bo_get - reference a struct ttm_buffer_object
222*4882a593Smuzhiyun *
223*4882a593Smuzhiyun * @bo: The buffer object.
224*4882a593Smuzhiyun */
ttm_bo_get(struct ttm_buffer_object * bo)225*4882a593Smuzhiyun static inline void ttm_bo_get(struct ttm_buffer_object *bo)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun kref_get(&bo->kref);
228*4882a593Smuzhiyun }
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /**
231*4882a593Smuzhiyun * ttm_bo_get_unless_zero - reference a struct ttm_buffer_object unless
232*4882a593Smuzhiyun * its refcount has already reached zero.
233*4882a593Smuzhiyun * @bo: The buffer object.
234*4882a593Smuzhiyun *
235*4882a593Smuzhiyun * Used to reference a TTM buffer object in lookups where the object is removed
236*4882a593Smuzhiyun * from the lookup structure during the destructor and for RCU lookups.
237*4882a593Smuzhiyun *
238*4882a593Smuzhiyun * Returns: @bo if the referencing was successful, NULL otherwise.
239*4882a593Smuzhiyun */
240*4882a593Smuzhiyun static inline __must_check struct ttm_buffer_object *
ttm_bo_get_unless_zero(struct ttm_buffer_object * bo)241*4882a593Smuzhiyun ttm_bo_get_unless_zero(struct ttm_buffer_object *bo)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun if (!kref_get_unless_zero(&bo->kref))
244*4882a593Smuzhiyun return NULL;
245*4882a593Smuzhiyun return bo;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun /**
249*4882a593Smuzhiyun * ttm_bo_wait - wait for buffer idle.
250*4882a593Smuzhiyun *
251*4882a593Smuzhiyun * @bo: The buffer object.
252*4882a593Smuzhiyun * @interruptible: Use interruptible wait.
253*4882a593Smuzhiyun * @no_wait: Return immediately if buffer is busy.
254*4882a593Smuzhiyun *
255*4882a593Smuzhiyun * This function must be called with the bo::mutex held, and makes
256*4882a593Smuzhiyun * sure any previous rendering to the buffer is completed.
257*4882a593Smuzhiyun * Note: It might be necessary to block validations before the
258*4882a593Smuzhiyun * wait by reserving the buffer.
259*4882a593Smuzhiyun * Returns -EBUSY if no_wait is true and the buffer is busy.
260*4882a593Smuzhiyun * Returns -ERESTARTSYS if interrupted by a signal.
261*4882a593Smuzhiyun */
262*4882a593Smuzhiyun int ttm_bo_wait(struct ttm_buffer_object *bo, bool interruptible, bool no_wait);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /**
265*4882a593Smuzhiyun * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * @placement: Return immediately if buffer is busy.
268*4882a593Smuzhiyun * @mem: The struct ttm_resource indicating the region where the bo resides
269*4882a593Smuzhiyun * @new_flags: Describes compatible placement found
270*4882a593Smuzhiyun *
271*4882a593Smuzhiyun * Returns true if the placement is compatible
272*4882a593Smuzhiyun */
273*4882a593Smuzhiyun bool ttm_bo_mem_compat(struct ttm_placement *placement, struct ttm_resource *mem,
274*4882a593Smuzhiyun uint32_t *new_flags);
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun /**
277*4882a593Smuzhiyun * ttm_bo_validate
278*4882a593Smuzhiyun *
279*4882a593Smuzhiyun * @bo: The buffer object.
280*4882a593Smuzhiyun * @placement: Proposed placement for the buffer object.
281*4882a593Smuzhiyun * @ctx: validation parameters.
282*4882a593Smuzhiyun *
283*4882a593Smuzhiyun * Changes placement and caching policy of the buffer object
284*4882a593Smuzhiyun * according proposed placement.
285*4882a593Smuzhiyun * Returns
286*4882a593Smuzhiyun * -EINVAL on invalid proposed placement.
287*4882a593Smuzhiyun * -ENOMEM on out-of-memory condition.
288*4882a593Smuzhiyun * -EBUSY if no_wait is true and buffer busy.
289*4882a593Smuzhiyun * -ERESTARTSYS if interrupted by a signal.
290*4882a593Smuzhiyun */
291*4882a593Smuzhiyun int ttm_bo_validate(struct ttm_buffer_object *bo,
292*4882a593Smuzhiyun struct ttm_placement *placement,
293*4882a593Smuzhiyun struct ttm_operation_ctx *ctx);
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /**
296*4882a593Smuzhiyun * ttm_bo_put
297*4882a593Smuzhiyun *
298*4882a593Smuzhiyun * @bo: The buffer object.
299*4882a593Smuzhiyun *
300*4882a593Smuzhiyun * Unreference a buffer object.
301*4882a593Smuzhiyun */
302*4882a593Smuzhiyun void ttm_bo_put(struct ttm_buffer_object *bo);
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun /**
305*4882a593Smuzhiyun * ttm_bo_move_to_lru_tail
306*4882a593Smuzhiyun *
307*4882a593Smuzhiyun * @bo: The buffer object.
308*4882a593Smuzhiyun * @bulk: optional bulk move structure to remember BO positions
309*4882a593Smuzhiyun *
310*4882a593Smuzhiyun * Move this BO to the tail of all lru lists used to lookup and reserve an
311*4882a593Smuzhiyun * object. This function must be called with struct ttm_bo_global::lru_lock
312*4882a593Smuzhiyun * held, and is used to make a BO less likely to be considered for eviction.
313*4882a593Smuzhiyun */
314*4882a593Smuzhiyun void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo,
315*4882a593Smuzhiyun struct ttm_lru_bulk_move *bulk);
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun /**
318*4882a593Smuzhiyun * ttm_bo_bulk_move_lru_tail
319*4882a593Smuzhiyun *
320*4882a593Smuzhiyun * @bulk: bulk move structure
321*4882a593Smuzhiyun *
322*4882a593Smuzhiyun * Bulk move BOs to the LRU tail, only valid to use when driver makes sure that
323*4882a593Smuzhiyun * BO order never changes. Should be called with ttm_bo_global::lru_lock held.
324*4882a593Smuzhiyun */
325*4882a593Smuzhiyun void ttm_bo_bulk_move_lru_tail(struct ttm_lru_bulk_move *bulk);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun /**
328*4882a593Smuzhiyun * ttm_bo_lock_delayed_workqueue
329*4882a593Smuzhiyun *
330*4882a593Smuzhiyun * Prevent the delayed workqueue from running.
331*4882a593Smuzhiyun * Returns
332*4882a593Smuzhiyun * True if the workqueue was queued at the time
333*4882a593Smuzhiyun */
334*4882a593Smuzhiyun int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun /**
337*4882a593Smuzhiyun * ttm_bo_unlock_delayed_workqueue
338*4882a593Smuzhiyun *
339*4882a593Smuzhiyun * Allows the delayed workqueue to run.
340*4882a593Smuzhiyun */
341*4882a593Smuzhiyun void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched);
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun /**
344*4882a593Smuzhiyun * ttm_bo_eviction_valuable
345*4882a593Smuzhiyun *
346*4882a593Smuzhiyun * @bo: The buffer object to evict
347*4882a593Smuzhiyun * @place: the placement we need to make room for
348*4882a593Smuzhiyun *
349*4882a593Smuzhiyun * Check if it is valuable to evict the BO to make room for the given placement.
350*4882a593Smuzhiyun */
351*4882a593Smuzhiyun bool ttm_bo_eviction_valuable(struct ttm_buffer_object *bo,
352*4882a593Smuzhiyun const struct ttm_place *place);
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
355*4882a593Smuzhiyun unsigned long bo_size,
356*4882a593Smuzhiyun unsigned struct_size);
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /**
359*4882a593Smuzhiyun * ttm_bo_init_reserved
360*4882a593Smuzhiyun *
361*4882a593Smuzhiyun * @bdev: Pointer to a ttm_bo_device struct.
362*4882a593Smuzhiyun * @bo: Pointer to a ttm_buffer_object to be initialized.
363*4882a593Smuzhiyun * @size: Requested size of buffer object.
364*4882a593Smuzhiyun * @type: Requested type of buffer object.
365*4882a593Smuzhiyun * @flags: Initial placement flags.
366*4882a593Smuzhiyun * @page_alignment: Data alignment in pages.
367*4882a593Smuzhiyun * @ctx: TTM operation context for memory allocation.
368*4882a593Smuzhiyun * @acc_size: Accounted size for this object.
369*4882a593Smuzhiyun * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
370*4882a593Smuzhiyun * @destroy: Destroy function. Use NULL for kfree().
371*4882a593Smuzhiyun *
372*4882a593Smuzhiyun * This function initializes a pre-allocated struct ttm_buffer_object.
373*4882a593Smuzhiyun * As this object may be part of a larger structure, this function,
374*4882a593Smuzhiyun * together with the @destroy function,
375*4882a593Smuzhiyun * enables driver-specific objects derived from a ttm_buffer_object.
376*4882a593Smuzhiyun *
377*4882a593Smuzhiyun * On successful return, the caller owns an object kref to @bo. The kref and
378*4882a593Smuzhiyun * list_kref are usually set to 1, but note that in some situations, other
379*4882a593Smuzhiyun * tasks may already be holding references to @bo as well.
380*4882a593Smuzhiyun * Furthermore, if resv == NULL, the buffer's reservation lock will be held,
381*4882a593Smuzhiyun * and it is the caller's responsibility to call ttm_bo_unreserve.
382*4882a593Smuzhiyun *
383*4882a593Smuzhiyun * If a failure occurs, the function will call the @destroy function, or
384*4882a593Smuzhiyun * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
385*4882a593Smuzhiyun * illegal and will likely cause memory corruption.
386*4882a593Smuzhiyun *
387*4882a593Smuzhiyun * Returns
388*4882a593Smuzhiyun * -ENOMEM: Out of memory.
389*4882a593Smuzhiyun * -EINVAL: Invalid placement flags.
390*4882a593Smuzhiyun * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
391*4882a593Smuzhiyun */
392*4882a593Smuzhiyun
393*4882a593Smuzhiyun int ttm_bo_init_reserved(struct ttm_bo_device *bdev,
394*4882a593Smuzhiyun struct ttm_buffer_object *bo,
395*4882a593Smuzhiyun unsigned long size,
396*4882a593Smuzhiyun enum ttm_bo_type type,
397*4882a593Smuzhiyun struct ttm_placement *placement,
398*4882a593Smuzhiyun uint32_t page_alignment,
399*4882a593Smuzhiyun struct ttm_operation_ctx *ctx,
400*4882a593Smuzhiyun size_t acc_size,
401*4882a593Smuzhiyun struct sg_table *sg,
402*4882a593Smuzhiyun struct dma_resv *resv,
403*4882a593Smuzhiyun void (*destroy) (struct ttm_buffer_object *));
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun /**
406*4882a593Smuzhiyun * ttm_bo_init
407*4882a593Smuzhiyun *
408*4882a593Smuzhiyun * @bdev: Pointer to a ttm_bo_device struct.
409*4882a593Smuzhiyun * @bo: Pointer to a ttm_buffer_object to be initialized.
410*4882a593Smuzhiyun * @size: Requested size of buffer object.
411*4882a593Smuzhiyun * @type: Requested type of buffer object.
412*4882a593Smuzhiyun * @flags: Initial placement flags.
413*4882a593Smuzhiyun * @page_alignment: Data alignment in pages.
414*4882a593Smuzhiyun * @interruptible: If needing to sleep to wait for GPU resources,
415*4882a593Smuzhiyun * sleep interruptible.
416*4882a593Smuzhiyun * pinned in physical memory. If this behaviour is not desired, this member
417*4882a593Smuzhiyun * holds a pointer to a persistent shmem object. Typically, this would
418*4882a593Smuzhiyun * point to the shmem object backing a GEM object if TTM is used to back a
419*4882a593Smuzhiyun * GEM user interface.
420*4882a593Smuzhiyun * @acc_size: Accounted size for this object.
421*4882a593Smuzhiyun * @resv: Pointer to a dma_resv, or NULL to let ttm allocate one.
422*4882a593Smuzhiyun * @destroy: Destroy function. Use NULL for kfree().
423*4882a593Smuzhiyun *
424*4882a593Smuzhiyun * This function initializes a pre-allocated struct ttm_buffer_object.
425*4882a593Smuzhiyun * As this object may be part of a larger structure, this function,
426*4882a593Smuzhiyun * together with the @destroy function,
427*4882a593Smuzhiyun * enables driver-specific objects derived from a ttm_buffer_object.
428*4882a593Smuzhiyun *
429*4882a593Smuzhiyun * On successful return, the caller owns an object kref to @bo. The kref and
430*4882a593Smuzhiyun * list_kref are usually set to 1, but note that in some situations, other
431*4882a593Smuzhiyun * tasks may already be holding references to @bo as well.
432*4882a593Smuzhiyun *
433*4882a593Smuzhiyun * If a failure occurs, the function will call the @destroy function, or
434*4882a593Smuzhiyun * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
435*4882a593Smuzhiyun * illegal and will likely cause memory corruption.
436*4882a593Smuzhiyun *
437*4882a593Smuzhiyun * Returns
438*4882a593Smuzhiyun * -ENOMEM: Out of memory.
439*4882a593Smuzhiyun * -EINVAL: Invalid placement flags.
440*4882a593Smuzhiyun * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
441*4882a593Smuzhiyun */
442*4882a593Smuzhiyun int ttm_bo_init(struct ttm_bo_device *bdev, struct ttm_buffer_object *bo,
443*4882a593Smuzhiyun unsigned long size, enum ttm_bo_type type,
444*4882a593Smuzhiyun struct ttm_placement *placement,
445*4882a593Smuzhiyun uint32_t page_alignment, bool interrubtible, size_t acc_size,
446*4882a593Smuzhiyun struct sg_table *sg, struct dma_resv *resv,
447*4882a593Smuzhiyun void (*destroy) (struct ttm_buffer_object *));
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /**
450*4882a593Smuzhiyun * ttm_bo_create
451*4882a593Smuzhiyun *
452*4882a593Smuzhiyun * @bdev: Pointer to a ttm_bo_device struct.
453*4882a593Smuzhiyun * @size: Requested size of buffer object.
454*4882a593Smuzhiyun * @type: Requested type of buffer object.
455*4882a593Smuzhiyun * @placement: Initial placement.
456*4882a593Smuzhiyun * @page_alignment: Data alignment in pages.
457*4882a593Smuzhiyun * @interruptible: If needing to sleep while waiting for GPU resources,
458*4882a593Smuzhiyun * sleep interruptible.
459*4882a593Smuzhiyun * @p_bo: On successful completion *p_bo points to the created object.
460*4882a593Smuzhiyun *
461*4882a593Smuzhiyun * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
462*4882a593Smuzhiyun * on that object. The destroy function is set to kfree().
463*4882a593Smuzhiyun * Returns
464*4882a593Smuzhiyun * -ENOMEM: Out of memory.
465*4882a593Smuzhiyun * -EINVAL: Invalid placement flags.
466*4882a593Smuzhiyun * -ERESTARTSYS: Interrupted by signal while waiting for resources.
467*4882a593Smuzhiyun */
468*4882a593Smuzhiyun int ttm_bo_create(struct ttm_bo_device *bdev, unsigned long size,
469*4882a593Smuzhiyun enum ttm_bo_type type, struct ttm_placement *placement,
470*4882a593Smuzhiyun uint32_t page_alignment, bool interruptible,
471*4882a593Smuzhiyun struct ttm_buffer_object **p_bo);
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun /**
474*4882a593Smuzhiyun * ttm_bo_evict_mm
475*4882a593Smuzhiyun *
476*4882a593Smuzhiyun * @bdev: Pointer to a ttm_bo_device struct.
477*4882a593Smuzhiyun * @mem_type: The memory type.
478*4882a593Smuzhiyun *
479*4882a593Smuzhiyun * Evicts all buffers on the lru list of the memory type.
480*4882a593Smuzhiyun * This is normally part of a VT switch or an
481*4882a593Smuzhiyun * out-of-memory-space-due-to-fragmentation handler.
482*4882a593Smuzhiyun * The caller must make sure that there are no other processes
483*4882a593Smuzhiyun * currently validating buffers, and can do that by taking the
484*4882a593Smuzhiyun * struct ttm_bo_device::ttm_lock in write mode.
485*4882a593Smuzhiyun *
486*4882a593Smuzhiyun * Returns:
487*4882a593Smuzhiyun * -EINVAL: Invalid or uninitialized memory type.
488*4882a593Smuzhiyun * -ERESTARTSYS: The call was interrupted by a signal while waiting to
489*4882a593Smuzhiyun * evict a buffer.
490*4882a593Smuzhiyun */
491*4882a593Smuzhiyun int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun /**
494*4882a593Smuzhiyun * ttm_kmap_obj_virtual
495*4882a593Smuzhiyun *
496*4882a593Smuzhiyun * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
497*4882a593Smuzhiyun * @is_iomem: Pointer to an integer that on return indicates 1 if the
498*4882a593Smuzhiyun * virtual map is io memory, 0 if normal memory.
499*4882a593Smuzhiyun *
500*4882a593Smuzhiyun * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
501*4882a593Smuzhiyun * If *is_iomem is 1 on return, the virtual address points to an io memory area,
502*4882a593Smuzhiyun * that should strictly be accessed by the iowriteXX() and similar functions.
503*4882a593Smuzhiyun */
ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj * map,bool * is_iomem)504*4882a593Smuzhiyun static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
505*4882a593Smuzhiyun bool *is_iomem)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
508*4882a593Smuzhiyun return map->virtual;
509*4882a593Smuzhiyun }
510*4882a593Smuzhiyun
511*4882a593Smuzhiyun /**
512*4882a593Smuzhiyun * ttm_bo_kmap
513*4882a593Smuzhiyun *
514*4882a593Smuzhiyun * @bo: The buffer object.
515*4882a593Smuzhiyun * @start_page: The first page to map.
516*4882a593Smuzhiyun * @num_pages: Number of pages to map.
517*4882a593Smuzhiyun * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
518*4882a593Smuzhiyun *
519*4882a593Smuzhiyun * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
520*4882a593Smuzhiyun * data in the buffer object. The ttm_kmap_obj_virtual function can then be
521*4882a593Smuzhiyun * used to obtain a virtual address to the data.
522*4882a593Smuzhiyun *
523*4882a593Smuzhiyun * Returns
524*4882a593Smuzhiyun * -ENOMEM: Out of memory.
525*4882a593Smuzhiyun * -EINVAL: Invalid range.
526*4882a593Smuzhiyun */
527*4882a593Smuzhiyun int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
528*4882a593Smuzhiyun unsigned long num_pages, struct ttm_bo_kmap_obj *map);
529*4882a593Smuzhiyun
530*4882a593Smuzhiyun /**
531*4882a593Smuzhiyun * ttm_bo_kunmap
532*4882a593Smuzhiyun *
533*4882a593Smuzhiyun * @map: Object describing the map to unmap.
534*4882a593Smuzhiyun *
535*4882a593Smuzhiyun * Unmaps a kernel map set up by ttm_bo_kmap.
536*4882a593Smuzhiyun */
537*4882a593Smuzhiyun void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun /**
540*4882a593Smuzhiyun * ttm_bo_mmap_obj - mmap memory backed by a ttm buffer object.
541*4882a593Smuzhiyun *
542*4882a593Smuzhiyun * @vma: vma as input from the fbdev mmap method.
543*4882a593Smuzhiyun * @bo: The bo backing the address space.
544*4882a593Smuzhiyun *
545*4882a593Smuzhiyun * Maps a buffer object.
546*4882a593Smuzhiyun */
547*4882a593Smuzhiyun int ttm_bo_mmap_obj(struct vm_area_struct *vma, struct ttm_buffer_object *bo);
548*4882a593Smuzhiyun
549*4882a593Smuzhiyun /**
550*4882a593Smuzhiyun * ttm_bo_mmap - mmap out of the ttm device address space.
551*4882a593Smuzhiyun *
552*4882a593Smuzhiyun * @filp: filp as input from the mmap method.
553*4882a593Smuzhiyun * @vma: vma as input from the mmap method.
554*4882a593Smuzhiyun * @bdev: Pointer to the ttm_bo_device with the address space manager.
555*4882a593Smuzhiyun *
556*4882a593Smuzhiyun * This function is intended to be called by the device mmap method.
557*4882a593Smuzhiyun * if the device address space is to be backed by the bo manager.
558*4882a593Smuzhiyun */
559*4882a593Smuzhiyun int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
560*4882a593Smuzhiyun struct ttm_bo_device *bdev);
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun /**
563*4882a593Smuzhiyun * ttm_bo_io
564*4882a593Smuzhiyun *
565*4882a593Smuzhiyun * @bdev: Pointer to the struct ttm_bo_device.
566*4882a593Smuzhiyun * @filp: Pointer to the struct file attempting to read / write.
567*4882a593Smuzhiyun * @wbuf: User-space pointer to address of buffer to write. NULL on read.
568*4882a593Smuzhiyun * @rbuf: User-space pointer to address of buffer to read into.
569*4882a593Smuzhiyun * Null on write.
570*4882a593Smuzhiyun * @count: Number of bytes to read / write.
571*4882a593Smuzhiyun * @f_pos: Pointer to current file position.
572*4882a593Smuzhiyun * @write: 1 for read, 0 for write.
573*4882a593Smuzhiyun *
574*4882a593Smuzhiyun * This function implements read / write into ttm buffer objects, and is
575*4882a593Smuzhiyun * intended to
576*4882a593Smuzhiyun * be called from the fops::read and fops::write method.
577*4882a593Smuzhiyun * Returns:
578*4882a593Smuzhiyun * See man (2) write, man(2) read. In particular,
579*4882a593Smuzhiyun * the function may return -ERESTARTSYS if
580*4882a593Smuzhiyun * interrupted by a signal.
581*4882a593Smuzhiyun */
582*4882a593Smuzhiyun ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
583*4882a593Smuzhiyun const char __user *wbuf, char __user *rbuf,
584*4882a593Smuzhiyun size_t count, loff_t *f_pos, bool write);
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun int ttm_bo_swapout(struct ttm_bo_global *glob,
587*4882a593Smuzhiyun struct ttm_operation_ctx *ctx);
588*4882a593Smuzhiyun void ttm_bo_swapout_all(void);
589*4882a593Smuzhiyun
590*4882a593Smuzhiyun /**
591*4882a593Smuzhiyun * ttm_bo_uses_embedded_gem_object - check if the given bo uses the
592*4882a593Smuzhiyun * embedded drm_gem_object.
593*4882a593Smuzhiyun *
594*4882a593Smuzhiyun * Most ttm drivers are using gem too, so the embedded
595*4882a593Smuzhiyun * ttm_buffer_object.base will be initialized by the driver (before
596*4882a593Smuzhiyun * calling ttm_bo_init). It is also possible to use ttm without gem
597*4882a593Smuzhiyun * though (vmwgfx does that).
598*4882a593Smuzhiyun *
599*4882a593Smuzhiyun * This helper will figure whenever a given ttm bo is a gem object too
600*4882a593Smuzhiyun * or not.
601*4882a593Smuzhiyun *
602*4882a593Smuzhiyun * @bo: The bo to check.
603*4882a593Smuzhiyun */
ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object * bo)604*4882a593Smuzhiyun static inline bool ttm_bo_uses_embedded_gem_object(struct ttm_buffer_object *bo)
605*4882a593Smuzhiyun {
606*4882a593Smuzhiyun return bo->base.dev != NULL;
607*4882a593Smuzhiyun }
608*4882a593Smuzhiyun
609*4882a593Smuzhiyun int ttm_mem_evict_first(struct ttm_bo_device *bdev,
610*4882a593Smuzhiyun struct ttm_resource_manager *man,
611*4882a593Smuzhiyun const struct ttm_place *place,
612*4882a593Smuzhiyun struct ttm_operation_ctx *ctx,
613*4882a593Smuzhiyun struct ww_acquire_ctx *ticket);
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun /* Default number of pre-faulted pages in the TTM fault handler */
616*4882a593Smuzhiyun #define TTM_BO_VM_NUM_PREFAULT 16
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun vm_fault_t ttm_bo_vm_reserve(struct ttm_buffer_object *bo,
619*4882a593Smuzhiyun struct vm_fault *vmf);
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun vm_fault_t ttm_bo_vm_fault_reserved(struct vm_fault *vmf,
622*4882a593Smuzhiyun pgprot_t prot,
623*4882a593Smuzhiyun pgoff_t num_prefault,
624*4882a593Smuzhiyun pgoff_t fault_page_size);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun vm_fault_t ttm_bo_vm_fault(struct vm_fault *vmf);
627*4882a593Smuzhiyun
628*4882a593Smuzhiyun void ttm_bo_vm_open(struct vm_area_struct *vma);
629*4882a593Smuzhiyun
630*4882a593Smuzhiyun void ttm_bo_vm_close(struct vm_area_struct *vma);
631*4882a593Smuzhiyun
632*4882a593Smuzhiyun int ttm_bo_vm_access(struct vm_area_struct *vma, unsigned long addr,
633*4882a593Smuzhiyun void *buf, int len, int write);
634*4882a593Smuzhiyun
635*4882a593Smuzhiyun #endif
636