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