blob: 9eb940d6755feba2163526cefc2ef230a6d4ea69 [file] [log] [blame]
Thomas Hellstromba4e7d92009-06-10 15:20:19 +02001/**************************************************************************
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
31#ifndef _TTM_BO_API_H_
32#define _TTM_BO_API_H_
33
David Howellsa1ce3922012-10-02 18:01:25 +010034#include <drm/drm_hashtab.h>
David Herrmann72525b32013-07-24 21:08:53 +020035#include <drm/drm_vma_manager.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020036#include <linux/kref.h>
37#include <linux/list.h>
38#include <linux/wait.h>
39#include <linux/mutex.h>
40#include <linux/mm.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020041#include <linux/bitmap.h>
Maarten Lankhorst5e338402013-06-27 13:48:19 +020042#include <linux/reservation.h>
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020043
44struct ttm_bo_device;
45
46struct drm_mm_node;
47
Christian König7c732ea2016-09-08 15:40:38 +020048struct ttm_placement;
Jerome Glisseca262a9992009-12-08 15:33:32 +010049
Jerome Glisse82c5da62010-04-09 14:39:23 +020050/**
51 * struct ttm_bus_placement
52 *
53 * @addr: mapped virtual address
54 * @base: bus base address
55 * @is_iomem: is this io memory ?
56 * @size: size in byte
57 * @offset: offset from the base address
Thomas Hellstromeba67092010-11-11 09:41:57 +010058 * @io_reserved_vm: The VM system has a refcount in @io_reserved_count
59 * @io_reserved_count: Refcounting the numbers of callers to ttm_mem_io_reserve
Jerome Glisse82c5da62010-04-09 14:39:23 +020060 *
61 * Structure indicating the bus placement of an object.
62 */
63struct ttm_bus_placement {
64 void *addr;
Alex Deucher749b48f2016-04-01 10:12:49 -040065 phys_addr_t base;
Jerome Glisse82c5da62010-04-09 14:39:23 +020066 unsigned long size;
67 unsigned long offset;
68 bool is_iomem;
Thomas Hellstromeba67092010-11-11 09:41:57 +010069 bool io_reserved_vm;
70 uint64_t io_reserved_count;
Jerome Glisse82c5da62010-04-09 14:39:23 +020071};
72
Jerome Glisseca262a9992009-12-08 15:33:32 +010073
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020074/**
75 * struct ttm_mem_reg
76 *
77 * @mm_node: Memory manager node.
78 * @size: Requested size of memory region.
79 * @num_pages: Actual size of memory region in pages.
80 * @page_alignment: Page alignment.
81 * @placement: Placement flags.
Jerome Glisse82c5da62010-04-09 14:39:23 +020082 * @bus: Placement on io bus accessible to the CPU
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020083 *
84 * Structure indicating the placement and space resources used by a
85 * buffer object.
86 */
87
88struct ttm_mem_reg {
Ben Skeggsd961db72010-08-05 10:48:18 +100089 void *mm_node;
90 unsigned long start;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020091 unsigned long size;
92 unsigned long num_pages;
93 uint32_t page_alignment;
94 uint32_t mem_type;
95 uint32_t placement;
Jerome Glisse82c5da62010-04-09 14:39:23 +020096 struct ttm_bus_placement bus;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +020097};
98
99/**
100 * enum ttm_bo_type
101 *
102 * @ttm_bo_type_device: These are 'normal' buffers that can
103 * be mmapped by user space. Each of these bos occupy a slot in the
104 * device address space, that can be used for normal vm operations.
105 *
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200106 * @ttm_bo_type_kernel: These buffers are like ttm_bo_type_device buffers,
107 * but they cannot be accessed from user-space. For kernel-only use.
Dave Airlie129b78b2012-04-02 11:46:06 +0100108 *
109 * @ttm_bo_type_sg: Buffer made from dmabuf sg table shared with another
110 * driver.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200111 */
112
113enum ttm_bo_type {
114 ttm_bo_type_device,
Dave Airlie129b78b2012-04-02 11:46:06 +0100115 ttm_bo_type_kernel,
116 ttm_bo_type_sg
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200117};
118
119struct ttm_tt;
120
121/**
122 * struct ttm_buffer_object
123 *
124 * @bdev: Pointer to the buffer object device structure.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200125 * @type: The bo type.
126 * @destroy: Destruction function. If NULL, kfree is used.
127 * @num_pages: Actual number of pages.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200128 * @acc_size: Accounted size for this object.
129 * @kref: Reference count of this buffer object. When this refcount reaches
130 * zero, the object is put on the delayed delete list.
131 * @list_kref: List reference count of this buffer object. This member is
132 * used to avoid destruction while the buffer object is still on a list.
133 * Lru lists may keep one refcount, the delayed delete list, and kref != 0
134 * keeps one refcount. When this refcount reaches zero,
135 * the object is destroyed.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200136 * @mem: structure describing current placement.
Jan Engelhardt5df23972011-04-04 01:25:18 +0200137 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200138 * pinned in physical memory. If this behaviour is not desired, this member
Jan Engelhardt5df23972011-04-04 01:25:18 +0200139 * holds a pointer to a persistent shmem object.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200140 * @ttm: TTM structure holding system pages.
141 * @evicted: Whether the object was evicted without user-space knowing.
142 * @cpu_writes: For synchronization. Number of cpu writers.
143 * @lru: List head for the lru list.
144 * @ddestroy: List head for the delayed destroy list.
145 * @swap: List head for swap LRU list.
Christian König5bc73062016-06-15 13:44:01 +0200146 * @moving: Fence set when BO is moving
David Herrmann72525b32013-07-24 21:08:53 +0200147 * @vma_node: Address space manager node.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200148 * @offset: The current GPU offset, which can have different meanings
149 * depending on the memory type. For SYSTEM type memory, it should be 0.
150 * @cur_placement: Hint of current placement.
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800151 * @wu_mutex: Wait unreserved mutex.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200152 *
153 * Base class for TTM buffer object, that deals with data placement and CPU
154 * mappings. GPU mappings are really up to the driver, but for simpler GPUs
155 * the driver can usually use the placement offset @offset directly as the
156 * GPU virtual address. For drivers implementing multiple
157 * GPU memory manager contexts, the driver should manage the address space
158 * in these contexts separately and use these objects to get the correct
159 * placement and caching for these GPU maps. This makes it possible to use
160 * these objects for even quite elaborate memory management schemes.
161 * The destroy member, the API visibility of this object makes it possible
162 * to derive driver specific types.
163 */
164
165struct ttm_buffer_object {
166 /**
167 * Members constant at init.
168 */
169
Thomas Hellstroma987fca2009-08-18 16:51:56 +0200170 struct ttm_bo_global *glob;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200171 struct ttm_bo_device *bdev;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200172 enum ttm_bo_type type;
173 void (*destroy) (struct ttm_buffer_object *);
174 unsigned long num_pages;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200175 size_t acc_size;
176
177 /**
178 * Members not needing protection.
179 */
180
181 struct kref kref;
182 struct kref list_kref;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200183
184 /**
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200185 * Members protected by the bo::resv::reserved lock.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200186 */
187
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200188 struct ttm_mem_reg mem;
Jan Engelhardt5df23972011-04-04 01:25:18 +0200189 struct file *persistent_swap_storage;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200190 struct ttm_tt *ttm;
191 bool evicted;
192
193 /**
194 * Members protected by the bo::reserved lock only when written to.
195 */
196
197 atomic_t cpu_writers;
198
199 /**
200 * Members protected by the bdev::lru_lock.
201 */
202
203 struct list_head lru;
204 struct list_head ddestroy;
205 struct list_head swap;
Thomas Hellstromeba67092010-11-11 09:41:57 +0100206 struct list_head io_reserve_lru;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200207
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200208 /**
Maarten Lankhorstdd7cfd62014-01-21 13:07:31 +0100209 * Members protected by a bo reservation.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200210 */
211
Christian König5bc73062016-06-15 13:44:01 +0200212 struct fence *moving;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200213
David Herrmann72525b32013-07-24 21:08:53 +0200214 struct drm_vma_offset_node vma_node;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200215
216 /**
217 * Special members that are protected by the reserve lock
218 * and the bo::lock when written to. Can be read with
219 * either of these locks held.
220 */
221
Alex Deucher54c4cd62015-03-04 00:18:38 -0500222 uint64_t offset; /* GPU address space is independent of CPU word size */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200223 uint32_t cur_placement;
Dave Airlie129b78b2012-04-02 11:46:06 +0100224
225 struct sg_table *sg;
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200226
227 struct reservation_object *resv;
228 struct reservation_object ttm_resv;
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800229 struct mutex wu_mutex;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200230};
231
232/**
233 * struct ttm_bo_kmap_obj
234 *
235 * @virtual: The current kernel virtual address.
236 * @page: The page when kmap'ing a single page.
237 * @bo_kmap_type: Type of bo_kmap.
238 *
239 * Object describing a kernel mapping. Since a TTM bo may be located
240 * in various memory types with various caching policies, the
241 * mapping can either be an ioremap, a vmap, a kmap or part of a
242 * premapped region.
243 */
244
Pekka Paalanena0724fc2009-08-17 01:18:38 +0300245#define TTM_BO_MAP_IOMEM_MASK 0x80
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200246struct ttm_bo_kmap_obj {
247 void *virtual;
248 struct page *page;
249 enum {
Pekka Paalanena0724fc2009-08-17 01:18:38 +0300250 ttm_bo_map_iomap = 1 | TTM_BO_MAP_IOMEM_MASK,
251 ttm_bo_map_vmap = 2,
252 ttm_bo_map_kmap = 3,
253 ttm_bo_map_premapped = 4 | TTM_BO_MAP_IOMEM_MASK,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200254 } bo_kmap_type;
Jerome Glisse82c5da62010-04-09 14:39:23 +0200255 struct ttm_buffer_object *bo;
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200256};
257
258/**
259 * ttm_bo_reference - reference a struct ttm_buffer_object
260 *
261 * @bo: The buffer object.
262 *
263 * Returns a refcounted pointer to a buffer object.
264 */
265
266static inline struct ttm_buffer_object *
267ttm_bo_reference(struct ttm_buffer_object *bo)
268{
269 kref_get(&bo->kref);
270 return bo;
271}
272
273/**
274 * ttm_bo_wait - wait for buffer idle.
275 *
276 * @bo: The buffer object.
277 * @interruptible: Use interruptible wait.
278 * @no_wait: Return immediately if buffer is busy.
279 *
280 * This function must be called with the bo::mutex held, and makes
281 * sure any previous rendering to the buffer is completed.
282 * Note: It might be necessary to block validations before the
283 * wait by reserving the buffer.
284 * Returns -EBUSY if no_wait is true and the buffer is busy.
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100285 * Returns -ERESTARTSYS if interrupted by a signal.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200286 */
Christian König8aa6d4f2016-04-06 11:12:04 +0200287extern int ttm_bo_wait(struct ttm_buffer_object *bo,
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200288 bool interruptible, bool no_wait);
Sinclair Yeh94477bf2016-06-29 12:58:49 -0700289
290/**
291 * ttm_bo_mem_compat - Check if proposed placement is compatible with a bo
292 *
293 * @placement: Return immediately if buffer is busy.
294 * @mem: The struct ttm_mem_reg indicating the region where the bo resides
295 * @new_flags: Describes compatible placement found
296 *
297 * Returns true if the placement is compatible
298 */
299extern bool ttm_bo_mem_compat(struct ttm_placement *placement,
300 struct ttm_mem_reg *mem,
301 uint32_t *new_flags);
302
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200303/**
Jerome Glisse09855ac2009-12-10 17:16:27 +0100304 * ttm_bo_validate
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200305 *
306 * @bo: The buffer object.
Jerome Glisseca262a9992009-12-08 15:33:32 +0100307 * @placement: Proposed placement for the buffer object.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200308 * @interruptible: Sleep interruptible if sleeping.
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000309 * @no_wait_gpu: Return immediately if the GPU is busy.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200310 *
311 * Changes placement and caching policy of the buffer object
Jerome Glisseca262a9992009-12-08 15:33:32 +0100312 * according proposed placement.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200313 * Returns
Jerome Glisseca262a9992009-12-08 15:33:32 +0100314 * -EINVAL on invalid proposed placement.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200315 * -ENOMEM on out-of-memory condition.
316 * -EBUSY if no_wait is true and buffer busy.
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100317 * -ERESTARTSYS if interrupted by a signal.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200318 */
Jerome Glisse09855ac2009-12-10 17:16:27 +0100319extern int ttm_bo_validate(struct ttm_buffer_object *bo,
320 struct ttm_placement *placement,
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000321 bool interruptible,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000322 bool no_wait_gpu);
Jerome Glisseca262a9992009-12-08 15:33:32 +0100323
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200324/**
325 * ttm_bo_unref
326 *
327 * @bo: The buffer object.
328 *
329 * Unreference and clear a pointer to a buffer object.
330 */
331extern void ttm_bo_unref(struct ttm_buffer_object **bo);
332
Dave Airlied6ea8882010-11-22 13:24:40 +1000333
334/**
335 * ttm_bo_list_ref_sub
336 *
337 * @bo: The buffer object.
338 * @count: The number of references with which to decrease @bo::list_kref;
339 * @never_free: The refcount should not reach zero with this operation.
340 *
341 * Release @count lru list references to this buffer object.
342 */
343extern void ttm_bo_list_ref_sub(struct ttm_buffer_object *bo, int count,
344 bool never_free);
345
346/**
347 * ttm_bo_add_to_lru
348 *
349 * @bo: The buffer object.
350 *
351 * Add this bo to the relevant mem type lru and, if it's backed by
352 * system pages (ttms) to the swap list.
353 * This function must be called with struct ttm_bo_global::lru_lock held, and
354 * is typically called immediately prior to unreserving a bo.
355 */
356extern void ttm_bo_add_to_lru(struct ttm_buffer_object *bo);
357
358/**
359 * ttm_bo_del_from_lru
360 *
361 * @bo: The buffer object.
362 *
363 * Remove this bo from all lru lists used to lookup and reserve an object.
364 * This function must be called with struct ttm_bo_global::lru_lock held,
365 * and is usually called just immediately after the bo has been reserved to
366 * avoid recursive reservation from lru lists.
367 */
368extern int ttm_bo_del_from_lru(struct ttm_buffer_object *bo);
369
Christian Königab749612016-01-11 15:35:20 +0100370/**
371 * ttm_bo_move_to_lru_tail
372 *
373 * @bo: The buffer object.
374 *
375 * Move this BO to the tail of all lru lists used to lookup and reserve an
376 * object. This function must be called with struct ttm_bo_global::lru_lock
377 * held, and is used to make a BO less likely to be considered for eviction.
378 */
379extern void ttm_bo_move_to_lru_tail(struct ttm_buffer_object *bo);
Dave Airlied6ea8882010-11-22 13:24:40 +1000380
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200381/**
Matthew Garrett7c5ee532010-04-26 16:00:09 -0400382 * ttm_bo_lock_delayed_workqueue
383 *
384 * Prevent the delayed workqueue from running.
385 * Returns
386 * True if the workqueue was queued at the time
387 */
388extern int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev);
389
390/**
391 * ttm_bo_unlock_delayed_workqueue
392 *
393 * Allows the delayed workqueue to run.
394 */
395extern void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev,
396 int resched);
397
398/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200399 * ttm_bo_synccpu_write_grab
400 *
401 * @bo: The buffer object:
402 * @no_wait: Return immediately if buffer is busy.
403 *
404 * Synchronizes a buffer object for CPU RW access. This means
Maarten Lankhorst654aa7922012-11-06 14:39:43 +0100405 * command submission that affects the buffer will return -EBUSY
406 * until ttm_bo_synccpu_write_release is called.
407 *
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200408 * Returns
409 * -EBUSY if the buffer is busy and no_wait is true.
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100410 * -ERESTARTSYS if interrupted by a signal.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200411 */
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200412extern int
413ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait);
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500414
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200415/**
416 * ttm_bo_synccpu_write_release:
417 *
418 * @bo : The buffer object.
419 *
420 * Releases a synccpu lock.
421 */
422extern void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo);
423
424/**
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500425 * ttm_bo_acc_size
426 *
427 * @bdev: Pointer to a ttm_bo_device struct.
428 * @bo_size: size of the buffer object in byte.
429 * @struct_size: size of the structure holding buffer object datas
430 *
431 * Returns size to account for a buffer object
432 */
433size_t ttm_bo_acc_size(struct ttm_bo_device *bdev,
434 unsigned long bo_size,
435 unsigned struct_size);
436size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev,
437 unsigned long bo_size,
438 unsigned struct_size);
439
440/**
Jerome Glisse09855ac2009-12-10 17:16:27 +0100441 * ttm_bo_init
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200442 *
443 * @bdev: Pointer to a ttm_bo_device struct.
444 * @bo: Pointer to a ttm_buffer_object to be initialized.
445 * @size: Requested size of buffer object.
446 * @type: Requested type of buffer object.
447 * @flags: Initial placement flags.
448 * @page_alignment: Data alignment in pages.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200449 * @interruptible: If needing to sleep to wait for GPU resources,
450 * sleep interruptible.
Jan Engelhardt5df23972011-04-04 01:25:18 +0200451 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200452 * pinned in physical memory. If this behaviour is not desired, this member
Jan Engelhardt5df23972011-04-04 01:25:18 +0200453 * holds a pointer to a persistent shmem object. Typically, this would
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200454 * point to the shmem object backing a GEM object if TTM is used to back a
455 * GEM user interface.
456 * @acc_size: Accounted size for this object.
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +0100457 * @resv: Pointer to a reservation_object, or NULL to let ttm allocate one.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200458 * @destroy: Destroy function. Use NULL for kfree().
459 *
460 * This function initializes a pre-allocated struct ttm_buffer_object.
461 * As this object may be part of a larger structure, this function,
462 * together with the @destroy function,
463 * enables driver-specific objects derived from a ttm_buffer_object.
464 * On successful return, the object kref and list_kref are set to 1.
Thomas Hellstrom7dfbbdc2010-11-09 21:31:44 +0100465 * If a failure occurs, the function will call the @destroy function, or
466 * kfree() if @destroy is NULL. Thus, after a failure, dereferencing @bo is
467 * illegal and will likely cause memory corruption.
468 *
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200469 * Returns
470 * -ENOMEM: Out of memory.
471 * -EINVAL: Invalid placement flags.
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100472 * -ERESTARTSYS: Interrupted by signal while sleeping waiting for resources.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200473 */
474
Jerome Glisse09855ac2009-12-10 17:16:27 +0100475extern int ttm_bo_init(struct ttm_bo_device *bdev,
476 struct ttm_buffer_object *bo,
477 unsigned long size,
478 enum ttm_bo_type type,
479 struct ttm_placement *placement,
480 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +0100481 bool interrubtible,
Jan Engelhardt5df23972011-04-04 01:25:18 +0200482 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +0100483 size_t acc_size,
Dave Airlie129b78b2012-04-02 11:46:06 +0100484 struct sg_table *sg,
Maarten Lankhorstf4f4e3e2014-01-09 11:03:15 +0100485 struct reservation_object *resv,
Jerome Glisse09855ac2009-12-10 17:16:27 +0100486 void (*destroy) (struct ttm_buffer_object *));
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500487
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200488/**
Alexandre Courbot59201052014-05-23 12:58:06 +0900489 * ttm_bo_create
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200490 *
491 * @bdev: Pointer to a ttm_bo_device struct.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200492 * @size: Requested size of buffer object.
493 * @type: Requested type of buffer object.
Alexandre Courbot59201052014-05-23 12:58:06 +0900494 * @placement: Initial placement.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200495 * @page_alignment: Data alignment in pages.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200496 * @interruptible: If needing to sleep while waiting for GPU resources,
497 * sleep interruptible.
Jan Engelhardt5df23972011-04-04 01:25:18 +0200498 * @persistent_swap_storage: Usually the swap storage is deleted for buffers
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200499 * pinned in physical memory. If this behaviour is not desired, this member
Jan Engelhardt5df23972011-04-04 01:25:18 +0200500 * holds a pointer to a persistent shmem object. Typically, this would
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200501 * point to the shmem object backing a GEM object if TTM is used to back a
502 * GEM user interface.
503 * @p_bo: On successful completion *p_bo points to the created object.
504 *
Jerome Glisse09855ac2009-12-10 17:16:27 +0100505 * This function allocates a ttm_buffer_object, and then calls ttm_bo_init
506 * on that object. The destroy function is set to kfree().
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200507 * Returns
508 * -ENOMEM: Out of memory.
509 * -EINVAL: Invalid placement flags.
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100510 * -ERESTARTSYS: Interrupted by signal while waiting for resources.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200511 */
512
Jerome Glisse09855ac2009-12-10 17:16:27 +0100513extern int ttm_bo_create(struct ttm_bo_device *bdev,
514 unsigned long size,
515 enum ttm_bo_type type,
516 struct ttm_placement *placement,
517 uint32_t page_alignment,
Jerome Glisse09855ac2009-12-10 17:16:27 +0100518 bool interruptible,
Jan Engelhardt5df23972011-04-04 01:25:18 +0200519 struct file *persistent_swap_storage,
Jerome Glisse09855ac2009-12-10 17:16:27 +0100520 struct ttm_buffer_object **p_bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200521
522/**
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200523 * ttm_bo_init_mm
524 *
525 * @bdev: Pointer to a ttm_bo_device struct.
526 * @mem_type: The memory type.
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200527 * @p_size: size managed area in pages.
528 *
529 * Initialize a manager for a given memory type.
530 * Note: if part of driver firstopen, it must be protected from a
531 * potentially racing lastclose.
532 * Returns:
533 * -EINVAL: invalid size or memory type.
534 * -ENOMEM: Not enough memory.
535 * May also return driver-specified errors.
536 */
537
538extern int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type,
Jerome Glisseca262a9992009-12-08 15:33:32 +0100539 unsigned long p_size);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200540/**
541 * ttm_bo_clean_mm
542 *
543 * @bdev: Pointer to a ttm_bo_device struct.
544 * @mem_type: The memory type.
545 *
546 * Take down a manager for a given memory type after first walking
547 * the LRU list to evict any buffers left alive.
548 *
549 * Normally, this function is part of lastclose() or unload(), and at that
550 * point there shouldn't be any buffers left created by user-space, since
551 * there should've been removed by the file descriptor release() method.
552 * However, before this function is run, make sure to signal all sync objects,
553 * and verify that the delayed delete queue is empty. The driver must also
554 * make sure that there are no NO_EVICT buffers present in this memory type
555 * when the call is made.
556 *
557 * If this function is part of a VT switch, the caller must make sure that
558 * there are no appications currently validating buffers before this
559 * function is called. The caller can do that by first taking the
560 * struct ttm_bo_device::ttm_lock in write mode.
561 *
562 * Returns:
563 * -EINVAL: invalid or uninitialized memory type.
564 * -EBUSY: There are still buffers left in this memory type.
565 */
566
567extern int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type);
568
569/**
570 * ttm_bo_evict_mm
571 *
572 * @bdev: Pointer to a ttm_bo_device struct.
573 * @mem_type: The memory type.
574 *
575 * Evicts all buffers on the lru list of the memory type.
576 * This is normally part of a VT switch or an
577 * out-of-memory-space-due-to-fragmentation handler.
578 * The caller must make sure that there are no other processes
579 * currently validating buffers, and can do that by taking the
580 * struct ttm_bo_device::ttm_lock in write mode.
581 *
582 * Returns:
583 * -EINVAL: Invalid or uninitialized memory type.
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100584 * -ERESTARTSYS: The call was interrupted by a signal while waiting to
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200585 * evict a buffer.
586 */
587
588extern int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type);
589
590/**
591 * ttm_kmap_obj_virtual
592 *
593 * @map: A struct ttm_bo_kmap_obj returned from ttm_bo_kmap.
594 * @is_iomem: Pointer to an integer that on return indicates 1 if the
595 * virtual map is io memory, 0 if normal memory.
596 *
597 * Returns the virtual address of a buffer object area mapped by ttm_bo_kmap.
598 * If *is_iomem is 1 on return, the virtual address points to an io memory area,
599 * that should strictly be accessed by the iowriteXX() and similar functions.
600 */
601
602static inline void *ttm_kmap_obj_virtual(struct ttm_bo_kmap_obj *map,
603 bool *is_iomem)
604{
Pekka Paalanena0724fc2009-08-17 01:18:38 +0300605 *is_iomem = !!(map->bo_kmap_type & TTM_BO_MAP_IOMEM_MASK);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200606 return map->virtual;
607}
608
609/**
610 * ttm_bo_kmap
611 *
612 * @bo: The buffer object.
613 * @start_page: The first page to map.
614 * @num_pages: Number of pages to map.
615 * @map: pointer to a struct ttm_bo_kmap_obj representing the map.
616 *
617 * Sets up a kernel virtual mapping, using ioremap, vmap or kmap to the
618 * data in the buffer object. The ttm_kmap_obj_virtual function can then be
619 * used to obtain a virtual address to the data.
620 *
621 * Returns
622 * -ENOMEM: Out of memory.
623 * -EINVAL: Invalid range.
624 */
625
626extern int ttm_bo_kmap(struct ttm_buffer_object *bo, unsigned long start_page,
627 unsigned long num_pages, struct ttm_bo_kmap_obj *map);
628
629/**
630 * ttm_bo_kunmap
631 *
632 * @map: Object describing the map to unmap.
633 *
634 * Unmaps a kernel map set up by ttm_bo_kmap.
635 */
636
637extern void ttm_bo_kunmap(struct ttm_bo_kmap_obj *map);
638
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200639/**
640 * ttm_fbdev_mmap - mmap fbdev memory backed by a ttm buffer object.
641 *
642 * @vma: vma as input from the fbdev mmap method.
643 * @bo: The bo backing the address space. The address space will
644 * have the same size as the bo, and start at offset 0.
645 *
646 * This function is intended to be called by the fbdev mmap method
647 * if the fbdev address space is to be backed by a bo.
648 */
649
650extern int ttm_fbdev_mmap(struct vm_area_struct *vma,
651 struct ttm_buffer_object *bo);
652
653/**
654 * ttm_bo_mmap - mmap out of the ttm device address space.
655 *
656 * @filp: filp as input from the mmap method.
657 * @vma: vma as input from the mmap method.
658 * @bdev: Pointer to the ttm_bo_device with the address space manager.
659 *
660 * This function is intended to be called by the device mmap method.
661 * if the device address space is to be backed by the bo manager.
662 */
663
664extern int ttm_bo_mmap(struct file *filp, struct vm_area_struct *vma,
665 struct ttm_bo_device *bdev);
666
667/**
668 * ttm_bo_io
669 *
670 * @bdev: Pointer to the struct ttm_bo_device.
671 * @filp: Pointer to the struct file attempting to read / write.
672 * @wbuf: User-space pointer to address of buffer to write. NULL on read.
673 * @rbuf: User-space pointer to address of buffer to read into.
674 * Null on write.
675 * @count: Number of bytes to read / write.
676 * @f_pos: Pointer to current file position.
677 * @write: 1 for read, 0 for write.
678 *
679 * This function implements read / write into ttm buffer objects, and is
680 * intended to
681 * be called from the fops::read and fops::write method.
682 * Returns:
683 * See man (2) write, man(2) read. In particular,
Thomas Hellstrom98ffc4152009-12-07 18:36:18 +0100684 * the function may return -ERESTARTSYS if
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200685 * interrupted by a signal.
686 */
687
688extern ssize_t ttm_bo_io(struct ttm_bo_device *bdev, struct file *filp,
689 const char __user *wbuf, char __user *rbuf,
690 size_t count, loff_t *f_pos, bool write);
691
692extern void ttm_bo_swapout_all(struct ttm_bo_device *bdev);
Thomas Hellstromc58f0092013-11-14 10:49:05 -0800693extern int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo);
Thomas Hellstromba4e7d92009-06-10 15:20:19 +0200694#endif