1*592ffb21SWarner Losh /**************************************************************************
2*592ffb21SWarner Losh *
3*592ffb21SWarner Losh * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA
4*592ffb21SWarner Losh * All Rights Reserved.
5*592ffb21SWarner Losh *
6*592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a
7*592ffb21SWarner Losh * copy of this software and associated documentation files (the
8*592ffb21SWarner Losh * "Software"), to deal in the Software without restriction, including
9*592ffb21SWarner Losh * without limitation the rights to use, copy, modify, merge, publish,
10*592ffb21SWarner Losh * distribute, sub license, and/or sell copies of the Software, and to
11*592ffb21SWarner Losh * permit persons to whom the Software is furnished to do so, subject to
12*592ffb21SWarner Losh * the following conditions:
13*592ffb21SWarner Losh *
14*592ffb21SWarner Losh * The above copyright notice and this permission notice (including the
15*592ffb21SWarner Losh * next paragraph) shall be included in all copies or substantial portions
16*592ffb21SWarner Losh * of the Software.
17*592ffb21SWarner Losh *
18*592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19*592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20*592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21*592ffb21SWarner Losh * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22*592ffb21SWarner Losh * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23*592ffb21SWarner Losh * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24*592ffb21SWarner Losh * USE OR OTHER DEALINGS IN THE SOFTWARE.
25*592ffb21SWarner Losh *
26*592ffb21SWarner Losh **************************************************************************/
27*592ffb21SWarner Losh /*
28*592ffb21SWarner Losh * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com>
29*592ffb21SWarner Losh */
30*592ffb21SWarner Losh
31*592ffb21SWarner Losh #ifndef _TTM_BO_API_H_
32*592ffb21SWarner Losh #define _TTM_BO_API_H_
33*592ffb21SWarner Losh
34*592ffb21SWarner Losh #include <dev/drm2/drmP.h>
35*592ffb21SWarner Losh
36*592ffb21SWarner Losh struct ttm_bo_device;
37*592ffb21SWarner Losh
38*592ffb21SWarner Losh struct drm_mm_node;
39*592ffb21SWarner Losh
40*592ffb21SWarner Losh
41*592ffb21SWarner Losh /**
42*592ffb21SWarner Losh * struct ttm_placement
43*592ffb21SWarner Losh *
44*592ffb21SWarner Losh * @fpfn: first valid page frame number to put the object
45*592ffb21SWarner Losh * @lpfn: last valid page frame number to put the object
46*592ffb21SWarner Losh * @num_placement: number of preferred placements
47*592ffb21SWarner Losh * @placement: preferred placements
48*592ffb21SWarner Losh * @num_busy_placement: number of preferred placements when need to evict buffer
49*592ffb21SWarner Losh * @busy_placement: preferred placements when need to evict buffer
50*592ffb21SWarner Losh *
51*592ffb21SWarner Losh * Structure indicating the placement you request for an object.
52*592ffb21SWarner Losh */
53*592ffb21SWarner Losh struct ttm_placement {
54*592ffb21SWarner Losh unsigned fpfn;
55*592ffb21SWarner Losh unsigned lpfn;
56*592ffb21SWarner Losh unsigned num_placement;
57*592ffb21SWarner Losh const uint32_t *placement;
58*592ffb21SWarner Losh unsigned num_busy_placement;
59*592ffb21SWarner Losh const uint32_t *busy_placement;
60*592ffb21SWarner Losh };
61*592ffb21SWarner Losh
62*592ffb21SWarner Losh /**
63*592ffb21SWarner Losh * struct ttm_bus_placement
64*592ffb21SWarner Losh *
65*592ffb21SWarner Losh * @addr: mapped virtual address
66*592ffb21SWarner Losh * @base: bus base address
67*592ffb21SWarner Losh * @is_iomem: is this io memory ?
68*592ffb21SWarner Losh * @size: size in byte
69*592ffb21SWarner Losh * @offset: offset from the base address
70*592ffb21SWarner Losh * @io_reserved_vm: The VM system has a refcount in @io_reserved_count
71*592ffb21SWarner Losh * @io_reserved_count: Refcounting the numbers of callers to ttm_mem_io_reserve
72*592ffb21SWarner Losh *
73*592ffb21SWarner Losh * Structure indicating the bus placement of an object.
74*592ffb21SWarner Losh */
75*592ffb21SWarner Losh struct ttm_bus_placement {
76*592ffb21SWarner Losh void *addr;
77*592ffb21SWarner Losh unsigned long base;
78*592ffb21SWarner Losh unsigned long size;
79*592ffb21SWarner Losh unsigned long offset;
80*592ffb21SWarner Losh bool is_iomem;
81*592ffb21SWarner Losh bool io_reserved_vm;
82*592ffb21SWarner Losh uint64_t io_reserved_count;
83*592ffb21SWarner Losh };
84*592ffb21SWarner Losh
85*592ffb21SWarner Losh
86*592ffb21SWarner Losh /**
87*592ffb21SWarner Losh * struct ttm_mem_reg
88*592ffb21SWarner Losh *
89*592ffb21SWarner Losh * @mm_node: Memory manager node.
90*592ffb21SWarner Losh * @size: Requested size of memory region.
91*592ffb21SWarner Losh * @num_pages: Actual size of memory region in pages.
92*592ffb21SWarner Losh * @page_alignment: Page alignment.
93*592ffb21SWarner Losh * @placement: Placement flags.
94*592ffb21SWarner Losh * @bus: Placement on io bus accessible to the CPU
95*592ffb21SWarner Losh *
96*592ffb21SWarner Losh * Structure indicating the placement and space resources used by a
97*592ffb21SWarner Losh * buffer object.
98*592ffb21SWarner Losh */
99*592ffb21SWarner Losh
100*592ffb21SWarner Losh struct ttm_mem_reg {
101*592ffb21SWarner Losh void *mm_node;
102*592ffb21SWarner Losh unsigned long start;
103*592ffb21SWarner Losh unsigned long size;
104*592ffb21SWarner Losh unsigned long num_pages;
105*592ffb21SWarner Losh uint32_t page_alignment;
106*592ffb21SWarner Losh uint32_t mem_type;
107*592ffb21SWarner Losh uint32_t placement;
108*592ffb21SWarner Losh struct ttm_bus_placement bus;
109*592ffb21SWarner Losh };
110*592ffb21SWarner Losh
111*592ffb21SWarner Losh /**
112*592ffb21SWarner Losh * enum ttm_bo_type
113*592ffb21SWarner Losh *
114*592ffb21SWarner Losh * @ttm_bo_type_device: These are 'normal' buffers that can
115*592ffb21SWarner Losh * be mmapped by user space. Each of these bos occupy a slot in the
116*592ffb21SWarner Losh * device address space, that can be used for normal vm operations.
117*592ffb21SWarner Losh *
118*592ffb21SWarner Losh * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
119*592ffb21SWarner Losh * but they cannot be accessed from user-space. For kernel-only use.
120*592ffb21SWarner Losh *
121*592ffb21SWarner Losh * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
122*592ffb21SWarner Losh * driver.
123*592ffb21SWarner Losh */
124*592ffb21SWarner Losh
125*592ffb21SWarner Losh enum ttm_bo_type {
126*592ffb21SWarner Losh ttm_bo_type_device,
127*592ffb21SWarner Losh ttm_bo_type_kernel,
128*592ffb21SWarner Losh ttm_bo_type_sg
129*592ffb21SWarner Losh };
130*592ffb21SWarner Losh
131*592ffb21SWarner Losh struct ttm_tt;
132*592ffb21SWarner Losh
133*592ffb21SWarner Losh /**
134*592ffb21SWarner Losh * struct ttm_buffer_object
135*592ffb21SWarner Losh *
136*592ffb21SWarner Losh * @bdev: Pointer to the buffer object device structure.
137*592ffb21SWarner Losh * @type: The bo type.
138*592ffb21SWarner Losh * @destroy: Destruction function. If NULL, kfree is used.
139*592ffb21SWarner Losh * @num_pages: Actual number of pages.
140*592ffb21SWarner Losh * @addr_space_offset: Address space offset.
141*592ffb21SWarner Losh * @acc_size: Accounted size for this object.
142*592ffb21SWarner Losh * @kref: Reference count of this buffer object. When this refcount reaches
143*592ffb21SWarner Losh * zero, the object is put on the delayed delete list.
144*592ffb21SWarner Losh * @list_kref: List reference count of this buffer object. This member is
145*592ffb21SWarner Losh * used to avoid destruction while the buffer object is still on a list.
146*592ffb21SWarner Losh * Lru lists may keep one refcount, the delayed delete list, and kref != 0
147*592ffb21SWarner Losh * keeps one refcount. When this refcount reaches zero,
148*592ffb21SWarner Losh * the object is destroyed.
149*592ffb21SWarner Losh * @event_queue: Queue for processes waiting on buffer object status change.
150*592ffb21SWarner Losh * @mem: structure describing current placement.
151*592ffb21SWarner Losh * @persistent_swap_storage: Usually the swap storage is deleted for buffers
152*592ffb21SWarner Losh * pinned in physical memory. If this behaviour is not desired, this member
153*592ffb21SWarner Losh * holds a pointer to a persistent shmem object.
154*592ffb21SWarner Losh * @ttm: TTM structure holding system pages.
155*592ffb21SWarner Losh * @evicted: Whether the object was evicted without user-space knowing.
156*592ffb21SWarner Losh * @cpu_writes: For synchronization. Number of cpu writers.
157*592ffb21SWarner Losh * @lru: List head for the lru list.
158*592ffb21SWarner Losh * @ddestroy: List head for the delayed destroy list.
159*592ffb21SWarner Losh * @swap: List head for swap LRU list.
160*592ffb21SWarner Losh * @val_seq: Sequence of the validation holding the @reserved lock.
161*592ffb21SWarner Losh * Used to avoid starvation when many processes compete to validate the
162*592ffb21SWarner Losh * buffer. This member is protected by the bo_device::lru_lock.
163*592ffb21SWarner Losh * @seq_valid: The value of @val_seq is valid. This value is protected by
164*592ffb21SWarner Losh * the bo_device::lru_lock.
165*592ffb21SWarner Losh * @reserved: Deadlock-free lock used for synchronization state transitions.
166*592ffb21SWarner Losh * @sync_obj: Pointer to a synchronization object.
167*592ffb21SWarner Losh * @priv_flags: Flags describing buffer object internal state.
168*592ffb21SWarner Losh * @vm_rb: Rb node for the vm rb tree.
169*592ffb21SWarner Losh * @vm_node: Address space manager node.
170*592ffb21SWarner Losh * @offset: The current GPU offset, which can have different meanings
171*592ffb21SWarner Losh * depending on the memory type. For SYSTEM type memory, it should be 0.
172*592ffb21SWarner Losh * @cur_placement: Hint of current placement.
173*592ffb21SWarner Losh *
174*592ffb21SWarner Losh * Base class for TTM buffer object, that deals with data placement and CPU
175*592ffb21SWarner Losh * mappings. GPU mappings are really up to the driver, but for simpler GPUs
176*592ffb21SWarner Losh * the driver can usually use the placement offset @offset directly as the
177*592ffb21SWarner Losh * GPU virtual address. For drivers implementing multiple
178*592ffb21SWarner Losh * GPU memory manager contexts, the driver should manage the address space
179*592ffb21SWarner Losh * in these contexts separately and use these objects to get the correct
180*592ffb21SWarner Losh * placement and caching for these GPU maps. This makes it possible to use
181*592ffb21SWarner Losh * these objects for even quite elaborate memory management schemes.
182*592ffb21SWarner Losh * The destroy member, the API visibility of this object makes it possible
183*592ffb21SWarner Losh * to derive driver specific types.
184*592ffb21SWarner Losh */
185*592ffb21SWarner Losh
186*592ffb21SWarner Losh struct ttm_buffer_object {
187*592ffb21SWarner Losh /**
188*592ffb21SWarner Losh * Members constant at init.
189*592ffb21SWarner Losh */
190*592ffb21SWarner Losh
191*592ffb21SWarner Losh struct ttm_bo_global *glob;
192*592ffb21SWarner Losh struct ttm_bo_device *bdev;
193*592ffb21SWarner Losh enum ttm_bo_type type;
194*592ffb21SWarner Losh void (*destroy) (struct ttm_buffer_object *);
195*592ffb21SWarner Losh unsigned long num_pages;
196*592ffb21SWarner Losh uint64_t addr_space_offset;
197*592ffb21SWarner Losh size_t acc_size;
198*592ffb21SWarner Losh
199*592ffb21SWarner Losh /**
200*592ffb21SWarner Losh * Members not needing protection.
201*592ffb21SWarner Losh */
202*592ffb21SWarner Losh
203*592ffb21SWarner Losh u_int kref;
204*592ffb21SWarner Losh u_int list_kref;
205*592ffb21SWarner Losh /* wait_queue_head_t event_queue; */
206*592ffb21SWarner Losh
207*592ffb21SWarner Losh /**
208*592ffb21SWarner Losh * Members protected by the bo::reserved lock.
209*592ffb21SWarner Losh */
210*592ffb21SWarner Losh
211*592ffb21SWarner Losh struct ttm_mem_reg mem;
212*592ffb21SWarner Losh struct vm_object *persistent_swap_storage;
213*592ffb21SWarner Losh struct ttm_tt *ttm;
214*592ffb21SWarner Losh bool evicted;
215*592ffb21SWarner Losh
216*592ffb21SWarner Losh /**
217*592ffb21SWarner Losh * Members protected by the bo::reserved lock only when written to.
218*592ffb21SWarner Losh */
219*592ffb21SWarner Losh
220*592ffb21SWarner Losh atomic_t cpu_writers;
221*592ffb21SWarner Losh
222*592ffb21SWarner Losh /**
223*592ffb21SWarner Losh * Members protected by the bdev::lru_lock.
224*592ffb21SWarner Losh */
225*592ffb21SWarner Losh
226*592ffb21SWarner Losh struct list_head lru;
227*592ffb21SWarner Losh struct list_head ddestroy;
228*592ffb21SWarner Losh struct list_head swap;
229*592ffb21SWarner Losh struct list_head io_reserve_lru;
230*592ffb21SWarner Losh uint32_t val_seq;
231*592ffb21SWarner Losh bool seq_valid;
232*592ffb21SWarner Losh
233*592ffb21SWarner Losh /**
234*592ffb21SWarner Losh * Members protected by the bdev::lru_lock
235*592ffb21SWarner Losh * only when written to.
236*592ffb21SWarner Losh */
237*592ffb21SWarner Losh
238*592ffb21SWarner Losh atomic_t reserved;
239*592ffb21SWarner Losh
240*592ffb21SWarner Losh /**
241*592ffb21SWarner Losh * Members protected by struct buffer_object_device::fence_lock
242*592ffb21SWarner Losh * In addition, setting sync_obj to anything else
243*592ffb21SWarner Losh * than NULL requires bo::reserved to be held. This allows for
244*592ffb21SWarner Losh * checking NULL while reserved but not holding the mentioned lock.
245*592ffb21SWarner Losh */
246*592ffb21SWarner Losh
247*592ffb21SWarner Losh void *sync_obj;
248*592ffb21SWarner Losh unsigned long priv_flags;
249*592ffb21SWarner Losh
250*592ffb21SWarner Losh /**
251*592ffb21SWarner Losh * Members protected by the bdev::vm_lock
252*592ffb21SWarner Losh */
253*592ffb21SWarner Losh
254*592ffb21SWarner Losh RB_ENTRY(ttm_buffer_object) vm_rb;
255*592ffb21SWarner Losh struct drm_mm_node *vm_node;
256*592ffb21SWarner Losh
257*592ffb21SWarner Losh
258*592ffb21SWarner Losh /**
259*592ffb21SWarner Losh * Special members that are protected by the reserve lock
260*592ffb21SWarner Losh * and the bo::lock when written to. Can be read with
261*592ffb21SWarner Losh * either of these locks held.
262*592ffb21SWarner Losh */
263*592ffb21SWarner Losh
264*592ffb21SWarner Losh unsigned long offset;
265*592ffb21SWarner Losh uint32_t cur_placement;
266*592ffb21SWarner Losh
267*592ffb21SWarner Losh struct sg_table *sg;
268*592ffb21SWarner Losh };
269*592ffb21SWarner Losh
270*592ffb21SWarner Losh /**
271*592ffb21SWarner Losh * struct ttm_bo_kmap_obj
272*592ffb21SWarner Losh *
273*592ffb21SWarner Losh * @virtual: The current kernel virtual address.
274*592ffb21SWarner Losh * @page: The page when kmap'ing a single page.
275*592ffb21SWarner Losh * @bo_kmap_type: Type of bo_kmap.
276*592ffb21SWarner Losh *
277*592ffb21SWarner Losh * Object describing a kernel mapping. Since a TTM bo may be located
278*592ffb21SWarner Losh * in various memory types with various caching policies, the
279*592ffb21SWarner Losh * mapping can either be an ioremap, a vmap, a kmap or part of a
280*592ffb21SWarner Losh * premapped region.
281*592ffb21SWarner Losh */
282*592ffb21SWarner Losh
283*592ffb21SWarner Losh #define TTM_BO_MAP_IOMEM_MASK 0x80
284*592ffb21SWarner Losh struct ttm_bo_kmap_obj {
285*592ffb21SWarner Losh void *virtual;
286*592ffb21SWarner Losh struct vm_page *page;
287*592ffb21SWarner Losh struct sf_buf *sf;
288*592ffb21SWarner Losh int num_pages;
289*592ffb21SWarner Losh unsigned long size;
290*592ffb21SWarner Losh enum {
291*592ffb21SWarner Losh ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
292*592ffb21SWarner Losh ttm_bo_map_vmap = 2,
293*592ffb21SWarner Losh ttm_bo_map_kmap = 3,
294*592ffb21SWarner Losh ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
295*592ffb21SWarner Losh } bo_kmap_type;
296*592ffb21SWarner Losh struct ttm_buffer_object *bo;
297*592ffb21SWarner Losh };
298*592ffb21SWarner Losh
299*592ffb21SWarner Losh /**
300*592ffb21SWarner Losh * ttm_bo_reference - reference a struct ttm_buffer_object
301*592ffb21SWarner Losh *
302*592ffb21SWarner Losh * @bo: The buffer object.
303*592ffb21SWarner Losh *
304*592ffb21SWarner Losh * Returns a refcounted pointer to a buffer object.
305*592ffb21SWarner Losh */
306*592ffb21SWarner Losh
307*592ffb21SWarner Losh static inline struct ttm_buffer_object *
ttm_bo_reference(struct ttm_buffer_object * bo)308*592ffb21SWarner Losh ttm_bo_reference(struct ttm_buffer_object *bo)
309*592ffb21SWarner Losh {
310*592ffb21SWarner Losh refcount_acquire(&bo->kref);
311*592ffb21SWarner Losh return bo;
312*592ffb21SWarner Losh }
313*592ffb21SWarner Losh
314*592ffb21SWarner Losh /**
315*592ffb21SWarner Losh * ttm_bo_wait - wait for buffer idle.
316*592ffb21SWarner Losh *
317*592ffb21SWarner Losh * @bo: The buffer object.
318*592ffb21SWarner Losh * @interruptible: Use interruptible wait.
319*592ffb21SWarner Losh * @no_wait: Return immediately if buffer is busy.
320*592ffb21SWarner Losh *
321*592ffb21SWarner Losh * This function must be called with the bo::mutex held, and makes
322*592ffb21SWarner Losh * sure any previous rendering to the buffer is completed.
323*592ffb21SWarner Losh * Note: It might be necessary to block validations before the
324*592ffb21SWarner Losh * wait by reserving the buffer.
325*592ffb21SWarner Losh * Returns -EBUSY if no_wait is true and the buffer is busy.
326*592ffb21SWarner Losh * Returns -ERESTARTSYS if interrupted by a signal.
327*592ffb21SWarner Losh */
328*592ffb21SWarner Losh extern int ttm_bo_wait(struct ttm_buffer_object *bo, bool lazy,
329*592ffb21SWarner Losh bool interruptible, bool no_wait);
330*592ffb21SWarner Losh /**
331*592ffb21SWarner Losh * ttm_bo_validate
332*592ffb21SWarner Losh *
333*592ffb21SWarner Losh * @bo: The buffer object.
334*592ffb21SWarner Losh * @placement: Proposed placement for the buffer object.
335*592ffb21SWarner Losh * @interruptible: Sleep interruptible if sleeping.
336*592ffb21SWarner Losh * @no_wait_gpu: Return immediately if the GPU is busy.
337*592ffb21SWarner Losh *
338*592ffb21SWarner Losh * Changes placement and caching policy of the buffer object
339*592ffb21SWarner Losh * according proposed placement.
340*592ffb21SWarner Losh * Returns
341*592ffb21SWarner Losh * -EINVAL on invalid proposed placement.
342*592ffb21SWarner Losh * -ENOMEM on out-of-memory condition.
343*592ffb21SWarner Losh * -EBUSY if no_wait is true and buffer busy.
344*592ffb21SWarner Losh * -ERESTARTSYS if interrupted by a signal.
345*592ffb21SWarner Losh */
346*592ffb21SWarner Losh extern int ttm_bo_validate(struct ttm_buffer_object *bo,
347*592ffb21SWarner Losh struct ttm_placement *placement,
348*592ffb21SWarner Losh bool interruptible,
349*592ffb21SWarner Losh bool no_wait_gpu);
350*592ffb21SWarner Losh
351*592ffb21SWarner Losh /**
352*592ffb21SWarner Losh * ttm_bo_unref
353*592ffb21SWarner Losh *
354*592ffb21SWarner Losh * @bo: The buffer object.
355*592ffb21SWarner Losh *
356*592ffb21SWarner Losh * Unreference and clear a pointer to a buffer object.
357*592ffb21SWarner Losh */
358*592ffb21SWarner Losh extern void ttm_bo_unref(struct ttm_buffer_object **bo);
359*592ffb21SWarner Losh
360*592ffb21SWarner Losh
361*592ffb21SWarner Losh /**
362*592ffb21SWarner Losh * ttm_bo_list_ref_sub
363*592ffb21SWarner Losh *
364*592ffb21SWarner Losh * @bo: The buffer object.
365*592ffb21SWarner Losh * @count: The number of references with which to decrease @bo::list_kref;
366*592ffb21SWarner Losh * @never_free: The refcount should not reach zero with this operation.
367*592ffb21SWarner Losh *
368*592ffb21SWarner Losh * Release @count lru list references to this buffer object.
369*592ffb21SWarner Losh */
370*592ffb21SWarner Losh extern void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
371*592ffb21SWarner Losh bool never_free);
372*592ffb21SWarner Losh
373*592ffb21SWarner Losh /**
374*592ffb21SWarner Losh * ttm_bo_add_to_lru
375*592ffb21SWarner Losh *
376*592ffb21SWarner Losh * @bo: The buffer object.
377*592ffb21SWarner Losh *
378*592ffb21SWarner Losh * Add this bo to the relevant mem type lru and, if it's backed by
379*592ffb21SWarner Losh * system pages (ttms) to the swap list.
380*592ffb21SWarner Losh * This function must be called with struct ttm_bo_global::lru_lock held, and
381*592ffb21SWarner Losh * is typically called immediately prior to unreserving a bo.
382*592ffb21SWarner Losh */
383*592ffb21SWarner Losh extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
384*592ffb21SWarner Losh
385*592ffb21SWarner Losh /**
386*592ffb21SWarner Losh * ttm_bo_del_from_lru
387*592ffb21SWarner Losh *
388*592ffb21SWarner Losh * @bo: The buffer object.
389*592ffb21SWarner Losh *
390*592ffb21SWarner Losh * Remove this bo from all lru lists used to lookup and reserve an object.
391*592ffb21SWarner Losh * This function must be called with struct ttm_bo_global::lru_lock held,
392*592ffb21SWarner Losh * and is usually called just immediately after the bo has been reserved to
393*592ffb21SWarner Losh * avoid recursive reservation from lru lists.
394*592ffb21SWarner Losh */
395*592ffb21SWarner Losh extern int ttm_bo_del_from_lru(struct ttm_buffer_object *bo);
396*592ffb21SWarner Losh
397*592ffb21SWarner Losh
398*592ffb21SWarner Losh /**
399*592ffb21SWarner Losh * ttm_bo_lock_delayed_workqueue
400*592ffb21SWarner Losh *
401*592ffb21SWarner Losh * Prevent the delayed workqueue from running.
402*592ffb21SWarner Losh * Returns
403*592ffb21SWarner Losh * True if the workqueue was queued at the time
404*592ffb21SWarner Losh */
405*592ffb21SWarner Losh extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
406*592ffb21SWarner Losh
407*592ffb21SWarner Losh /**
408*592ffb21SWarner Losh * ttm_bo_unlock_delayed_workqueue
409*592ffb21SWarner Losh *
410*592ffb21SWarner Losh * Allows the delayed workqueue to run.
411*592ffb21SWarner Losh */
412*592ffb21SWarner Losh extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
413*592ffb21SWarner Losh int resched);
414*592ffb21SWarner Losh
415*592ffb21SWarner Losh /**
416*592ffb21SWarner Losh * ttm_bo_synccpu_write_grab
417*592ffb21SWarner Losh *
418*592ffb21SWarner Losh * @bo: The buffer object:
419*592ffb21SWarner Losh * @no_wait: Return immediately if buffer is busy.
420*592ffb21SWarner Losh *
421*592ffb21SWarner Losh * Synchronizes a buffer object for CPU RW access. This means
422*592ffb21SWarner Losh * command submission that affects the buffer will return -EBUSY
423*592ffb21SWarner Losh * until ttm_bo_synccpu_write_release is called.
424*592ffb21SWarner Losh *
425*592ffb21SWarner Losh * Returns
426*592ffb21SWarner Losh * -EBUSY if the buffer is busy and no_wait is true.
427*592ffb21SWarner Losh * -ERESTARTSYS if interrupted by a signal.
428*592ffb21SWarner Losh */
429*592ffb21SWarner Losh extern int
430*592ffb21SWarner Losh ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
431*592ffb21SWarner Losh
432*592ffb21SWarner Losh /**
433*592ffb21SWarner Losh * ttm_bo_synccpu_write_release:
434*592ffb21SWarner Losh *
435*592ffb21SWarner Losh * @bo : The buffer object.
436*592ffb21SWarner Losh *
437*592ffb21SWarner Losh * Releases a synccpu lock.
438*592ffb21SWarner Losh */
439*592ffb21SWarner Losh extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
440*592ffb21SWarner Losh
441*592ffb21SWarner Losh /**
442*592ffb21SWarner Losh * ttm_bo_acc_size
443*592ffb21SWarner Losh *
444*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct.
445*592ffb21SWarner Losh * @bo_size: size of the buffer object in byte.
446*592ffb21SWarner Losh * @struct_size: size of the structure holding buffer object datas
447*592ffb21SWarner Losh *
448*592ffb21SWarner Losh * Returns size to account for a buffer object
449*592ffb21SWarner Losh */
450*592ffb21SWarner Losh size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
451*592ffb21SWarner Losh unsigned long bo_size,
452*592ffb21SWarner Losh unsigned struct_size);
453*592ffb21SWarner Losh size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
454*592ffb21SWarner Losh unsigned long bo_size,
455*592ffb21SWarner Losh unsigned struct_size);
456*592ffb21SWarner Losh
457*592ffb21SWarner Losh /**
458*592ffb21SWarner Losh * ttm_bo_init
459*592ffb21SWarner Losh *
460*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct.
461*592ffb21SWarner Losh * @bo: Pointer to a ttm_buffer_object to be initialized.
462*592ffb21SWarner Losh * @size: Requested size of buffer object.
463*592ffb21SWarner Losh * @type: Requested type of buffer object.
464*592ffb21SWarner Losh * @flags: Initial placement flags.
465*592ffb21SWarner Losh * @page_alignment: Data alignment in pages.
466*592ffb21SWarner Losh * @interruptible: If needing to sleep to wait for GPU resources,
467*592ffb21SWarner Losh * sleep interruptible.
468*592ffb21SWarner Losh * @persistent_swap_storage: Usually the swap storage is deleted for buffers
469*592ffb21SWarner Losh * pinned in physical memory. If this behaviour is not desired, this member
470*592ffb21SWarner Losh * holds a pointer to a persistent shmem object. Typically, this would
471*592ffb21SWarner Losh * point to the shmem object backing a GEM object if TTM is used to back a
472*592ffb21SWarner Losh * GEM user interface.
473*592ffb21SWarner Losh * @acc_size: Accounted size for this object.
474*592ffb21SWarner Losh * @destroy: Destroy function. Use NULL for kfree().
475*592ffb21SWarner Losh *
476*592ffb21SWarner Losh * This function initializes a pre-allocated struct ttm_buffer_object.
477*592ffb21SWarner Losh * As this object may be part of a larger structure, this function,
478*592ffb21SWarner Losh * together with the @destroy function,
479*592ffb21SWarner Losh * enables driver-specific objects derived from a ttm_buffer_object.
480*592ffb21SWarner Losh * On successful return, the object kref and list_kref are set to 1.
481*592ffb21SWarner Losh * If a failure occurs, the function will call the @destroy function, or
482*592ffb21SWarner Losh * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
483*592ffb21SWarner Losh * illegal and will likely cause memory corruption.
484*592ffb21SWarner Losh *
485*592ffb21SWarner Losh * Returns
486*592ffb21SWarner Losh * -ENOMEM: Out of memory.
487*592ffb21SWarner Losh * -EINVAL: Invalid placement flags.
488*592ffb21SWarner Losh * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
489*592ffb21SWarner Losh */
490*592ffb21SWarner Losh
491*592ffb21SWarner Losh extern int ttm_bo_init(struct ttm_bo_device *bdev,
492*592ffb21SWarner Losh struct ttm_buffer_object *bo,
493*592ffb21SWarner Losh unsigned long size,
494*592ffb21SWarner Losh enum ttm_bo_type type,
495*592ffb21SWarner Losh struct ttm_placement *placement,
496*592ffb21SWarner Losh uint32_t page_alignment,
497*592ffb21SWarner Losh bool interrubtible,
498*592ffb21SWarner Losh struct vm_object *persistent_swap_storage,
499*592ffb21SWarner Losh size_t acc_size,
500*592ffb21SWarner Losh struct sg_table *sg,
501*592ffb21SWarner Losh void (*destroy) (struct ttm_buffer_object *));
502*592ffb21SWarner Losh
503*592ffb21SWarner Losh /**
504*592ffb21SWarner Losh * ttm_bo_synccpu_object_init
505*592ffb21SWarner Losh *
506*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct.
507*592ffb21SWarner Losh * @bo: Pointer to a ttm_buffer_object to be initialized.
508*592ffb21SWarner Losh * @size: Requested size of buffer object.
509*592ffb21SWarner Losh * @type: Requested type of buffer object.
510*592ffb21SWarner Losh * @flags: Initial placement flags.
511*592ffb21SWarner Losh * @page_alignment: Data alignment in pages.
512*592ffb21SWarner Losh * @interruptible: If needing to sleep while waiting for GPU resources,
513*592ffb21SWarner Losh * sleep interruptible.
514*592ffb21SWarner Losh * @persistent_swap_storage: Usually the swap storage is deleted for buffers
515*592ffb21SWarner Losh * pinned in physical memory. If this behaviour is not desired, this member
516*592ffb21SWarner Losh * holds a pointer to a persistent shmem object. Typically, this would
517*592ffb21SWarner Losh * point to the shmem object backing a GEM object if TTM is used to back a
518*592ffb21SWarner Losh * GEM user interface.
519*592ffb21SWarner Losh * @p_bo: On successful completion *p_bo points to the created object.
520*592ffb21SWarner Losh *
521*592ffb21SWarner Losh * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
522*592ffb21SWarner Losh * on that object. The destroy function is set to kfree().
523*592ffb21SWarner Losh * Returns
524*592ffb21SWarner Losh * -ENOMEM: Out of memory.
525*592ffb21SWarner Losh * -EINVAL: Invalid placement flags.
526*592ffb21SWarner Losh * -ERESTARTSYS: Interrupted by signal while waiting for resources.
527*592ffb21SWarner Losh */
528*592ffb21SWarner Losh
529*592ffb21SWarner Losh extern int ttm_bo_create(struct ttm_bo_device *bdev,
530*592ffb21SWarner Losh unsigned long size,
531*592ffb21SWarner Losh enum ttm_bo_type type,
532*592ffb21SWarner Losh struct ttm_placement *placement,
533*592ffb21SWarner Losh uint32_t page_alignment,
534*592ffb21SWarner Losh bool interruptible,
535*592ffb21SWarner Losh struct vm_object *persistent_swap_storage,
536*592ffb21SWarner Losh struct ttm_buffer_object **p_bo);
537*592ffb21SWarner Losh
538*592ffb21SWarner Losh /**
539*592ffb21SWarner Losh * ttm_bo_check_placement
540*592ffb21SWarner Losh *
541*592ffb21SWarner Losh * @bo: the buffer object.
542*592ffb21SWarner Losh * @placement: placements
543*592ffb21SWarner Losh *
544*592ffb21SWarner Losh * Performs minimal validity checking on an intended change of
545*592ffb21SWarner Losh * placement flags.
546*592ffb21SWarner Losh * Returns
547*592ffb21SWarner Losh * -EINVAL: Intended change is invalid or not allowed.
548*592ffb21SWarner Losh */
549*592ffb21SWarner Losh extern int ttm_bo_check_placement(struct ttm_buffer_object *bo,
550*592ffb21SWarner Losh struct ttm_placement *placement);
551*592ffb21SWarner Losh
552*592ffb21SWarner Losh /**
553*592ffb21SWarner Losh * ttm_bo_init_mm
554*592ffb21SWarner Losh *
555*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct.
556*592ffb21SWarner Losh * @mem_type: The memory type.
557*592ffb21SWarner Losh * @p_size: size managed area in pages.
558*592ffb21SWarner Losh *
559*592ffb21SWarner Losh * Initialize a manager for a given memory type.
560*592ffb21SWarner Losh * Note: if part of driver firstopen, it must be protected from a
561*592ffb21SWarner Losh * potentially racing lastclose.
562*592ffb21SWarner Losh * Returns:
563*592ffb21SWarner Losh * -EINVAL: invalid size or memory type.
564*592ffb21SWarner Losh * -ENOMEM: Not enough memory.
565*592ffb21SWarner Losh * May also return driver-specified errors.
566*592ffb21SWarner Losh */
567*592ffb21SWarner Losh
568*592ffb21SWarner Losh extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
569*592ffb21SWarner Losh unsigned long p_size);
570*592ffb21SWarner Losh /**
571*592ffb21SWarner Losh * ttm_bo_clean_mm
572*592ffb21SWarner Losh *
573*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct.
574*592ffb21SWarner Losh * @mem_type: The memory type.
575*592ffb21SWarner Losh *
576*592ffb21SWarner Losh * Take down a manager for a given memory type after first walking
577*592ffb21SWarner Losh * the LRU list to evict any buffers left alive.
578*592ffb21SWarner Losh *
579*592ffb21SWarner Losh * Normally, this function is part of lastclose() or unload(), and at that
580*592ffb21SWarner Losh * point there shouldn't be any buffers left created by user-space, since
581*592ffb21SWarner Losh * there should've been removed by the file descriptor release() method.
582*592ffb21SWarner Losh * However, before this function is run, make sure to signal all sync objects,
583*592ffb21SWarner Losh * and verify that the delayed delete queue is empty. The driver must also
584*592ffb21SWarner Losh * make sure that there are no NO_EVICT buffers present in this memory type
585*592ffb21SWarner Losh * when the call is made.
586*592ffb21SWarner Losh *
587*592ffb21SWarner Losh * If this function is part of a VT switch, the caller must make sure that
588*592ffb21SWarner Losh * there are no appications currently validating buffers before this
589*592ffb21SWarner Losh * function is called. The caller can do that by first taking the
590*592ffb21SWarner Losh * struct ttm_bo_device::ttm_lock in write mode.
591*592ffb21SWarner Losh *
592*592ffb21SWarner Losh * Returns:
593*592ffb21SWarner Losh * -EINVAL: invalid or uninitialized memory type.
594*592ffb21SWarner Losh * -EBUSY: There are still buffers left in this memory type.
595*592ffb21SWarner Losh */
596*592ffb21SWarner Losh
597*592ffb21SWarner Losh extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);
598*592ffb21SWarner Losh
599*592ffb21SWarner Losh /**
600*592ffb21SWarner Losh * ttm_bo_evict_mm
601*592ffb21SWarner Losh *
602*592ffb21SWarner Losh * @bdev: Pointer to a ttm_bo_device struct.
603*592ffb21SWarner Losh * @mem_type: The memory type.
604*592ffb21SWarner Losh *
605*592ffb21SWarner Losh * Evicts all buffers on the lru list of the memory type.
606*592ffb21SWarner Losh * This is normally part of a VT switch or an
607*592ffb21SWarner Losh * out-of-memory-space-due-to-fragmentation handler.
608*592ffb21SWarner Losh * The caller must make sure that there are no other processes
609*592ffb21SWarner Losh * currently validating buffers, and can do that by taking the
610*592ffb21SWarner Losh * struct ttm_bo_device::ttm_lock in write mode.
611*592ffb21SWarner Losh *
612*592ffb21SWarner Losh * Returns:
613*592ffb21SWarner Losh * -EINVAL: Invalid or uninitialized memory type.
614*592ffb21SWarner Losh * -ERESTARTSYS: The call was interrupted by a signal while waiting to
615*592ffb21SWarner Losh * evict a buffer.
616*592ffb21SWarner Losh */
617*592ffb21SWarner Losh
618*592ffb21SWarner Losh extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
619*592ffb21SWarner Losh
620*592ffb21SWarner Losh /**
621*592ffb21SWarner Losh * ttm_kmap_obj_virtual
622*592ffb21SWarner Losh *
623*592ffb21SWarner Losh * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
624*592ffb21SWarner Losh * @is_iomem: Pointer to an integer that on return indicates 1 if the
625*592ffb21SWarner Losh * virtual map is io memory, 0 if normal memory.
626*592ffb21SWarner Losh *
627*592ffb21SWarner Losh * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
628*592ffb21SWarner Losh * If *is_iomem is 1 on return, the virtual address points to an io memory area,
629*592ffb21SWarner Losh * that should strictly be accessed by the iowriteXX() and similar functions.
630*592ffb21SWarner Losh */
631*592ffb21SWarner Losh
ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj * map,bool * is_iomem)632*592ffb21SWarner Losh static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
633*592ffb21SWarner Losh bool *is_iomem)
634*592ffb21SWarner Losh {
635*592ffb21SWarner Losh *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
636*592ffb21SWarner Losh return map->virtual;
637*592ffb21SWarner Losh }
638*592ffb21SWarner Losh
639*592ffb21SWarner Losh /**
640*592ffb21SWarner Losh * ttm_bo_kmap
641*592ffb21SWarner Losh *
642*592ffb21SWarner Losh * @bo: The buffer object.
643*592ffb21SWarner Losh * @start_page: The first page to map.
644*592ffb21SWarner Losh * @num_pages: Number of pages to map.
645*592ffb21SWarner Losh * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
646*592ffb21SWarner Losh *
647*592ffb21SWarner Losh * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
648*592ffb21SWarner Losh * data in the buffer object. The ttm_kmap_obj_virtual function can then be
649*592ffb21SWarner Losh * used to obtain a virtual address to the data.
650*592ffb21SWarner Losh *
651*592ffb21SWarner Losh * Returns
652*592ffb21SWarner Losh * -ENOMEM: Out of memory.
653*592ffb21SWarner Losh * -EINVAL: Invalid range.
654*592ffb21SWarner Losh */
655*592ffb21SWarner Losh
656*592ffb21SWarner Losh extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
657*592ffb21SWarner Losh unsigned long num_pages, struct ttm_bo_kmap_obj *map);
658*592ffb21SWarner Losh
659*592ffb21SWarner Losh /**
660*592ffb21SWarner Losh * ttm_bo_kunmap
661*592ffb21SWarner Losh *
662*592ffb21SWarner Losh * @map: Object describing the map to unmap.
663*592ffb21SWarner Losh *
664*592ffb21SWarner Losh * Unmaps a kernel map set up by ttm_bo_kmap.
665*592ffb21SWarner Losh */
666*592ffb21SWarner Losh
667*592ffb21SWarner Losh extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
668*592ffb21SWarner Losh
669*592ffb21SWarner Losh /**
670*592ffb21SWarner Losh * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object.
671*592ffb21SWarner Losh *
672*592ffb21SWarner Losh * @vma: vma as input from the fbdev mmap method.
673*592ffb21SWarner Losh * @bo: The bo backing the address space. The address space will
674*592ffb21SWarner Losh * have the same size as the bo, and start at offset 0.
675*592ffb21SWarner Losh *
676*592ffb21SWarner Losh * This function is intended to be called by the fbdev mmap method
677*592ffb21SWarner Losh * if the fbdev address space is to be backed by a bo.
678*592ffb21SWarner Losh */
679*592ffb21SWarner Losh
680*592ffb21SWarner Losh /* XXXKIB
681*592ffb21SWarner Losh extern int ttm_fbdev_mmap(struct vm_area_struct *vma,
682*592ffb21SWarner Losh struct ttm_buffer_object *bo);
683*592ffb21SWarner Losh */
684*592ffb21SWarner Losh /**
685*592ffb21SWarner Losh * ttm_bo_mmap - mmap out of the ttm device address space.
686*592ffb21SWarner Losh *
687*592ffb21SWarner Losh * @filp: filp as input from the mmap method.
688*592ffb21SWarner Losh * @vma: vma as input from the mmap method.
689*592ffb21SWarner Losh * @bdev: Pointer to the ttm_bo_device with the address space manager.
690*592ffb21SWarner Losh *
691*592ffb21SWarner Losh * This function is intended to be called by the device mmap method.
692*592ffb21SWarner Losh * if the device address space is to be backed by the bo manager.
693*592ffb21SWarner Losh */
694*592ffb21SWarner Losh /* XXXKIB
695*592ffb21SWarner Losh extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
696*592ffb21SWarner Losh struct ttm_bo_device *bdev);
697*592ffb21SWarner Losh */
698*592ffb21SWarner Losh /**
699*592ffb21SWarner Losh * ttm_bo_io
700*592ffb21SWarner Losh *
701*592ffb21SWarner Losh * @bdev: Pointer to the struct ttm_bo_device.
702*592ffb21SWarner Losh * @filp: Pointer to the struct file attempting to read / write.
703*592ffb21SWarner Losh * @wbuf: User-space pointer to address of buffer to write. NULL on read.
704*592ffb21SWarner Losh * @rbuf: User-space pointer to address of buffer to read into.
705*592ffb21SWarner Losh * Null on write.
706*592ffb21SWarner Losh * @count: Number of bytes to read / write.
707*592ffb21SWarner Losh * @f_pos: Pointer to current file position.
708*592ffb21SWarner Losh * @write: 1 for read, 0 for write.
709*592ffb21SWarner Losh *
710*592ffb21SWarner Losh * This function implements read / write into ttm buffer objects, and is
711*592ffb21SWarner Losh * intended to
712*592ffb21SWarner Losh * be called from the fops::read and fops::write method.
713*592ffb21SWarner Losh * Returns:
714*592ffb21SWarner Losh * See man (2) write, man(2) read. In particular,
715*592ffb21SWarner Losh * the function may return -ERESTARTSYS if
716*592ffb21SWarner Losh * interrupted by a signal.
717*592ffb21SWarner Losh */
718*592ffb21SWarner Losh
719*592ffb21SWarner Losh extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
720*592ffb21SWarner Losh const char *wbuf, char *rbuf,
721*592ffb21SWarner Losh size_t count, off_t *f_pos, bool write);
722*592ffb21SWarner Losh
723*592ffb21SWarner Losh extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev);
724*592ffb21SWarner Losh
725*592ffb21SWarner Losh /**
726*592ffb21SWarner Losh * ttm_bo_is_reserved - return an indication if a ttm buffer object is reserved
727*592ffb21SWarner Losh *
728*592ffb21SWarner Losh * @bo: The buffer object to check.
729*592ffb21SWarner Losh *
730*592ffb21SWarner Losh * This function returns an indication if a bo is reserved or not, and should
731*592ffb21SWarner Losh * only be used to print an error when it is not from incorrect api usage, since
732*592ffb21SWarner Losh * there's no guarantee that it is the caller that is holding the reservation.
733*592ffb21SWarner Losh */
ttm_bo_is_reserved(struct ttm_buffer_object * bo)734*592ffb21SWarner Losh static inline bool ttm_bo_is_reserved(struct ttm_buffer_object *bo)
735*592ffb21SWarner Losh {
736*592ffb21SWarner Losh return atomic_read(&bo->reserved);
737*592ffb21SWarner Losh }
738*592ffb21SWarner Losh
739*592ffb21SWarner Losh #endif
740