1 /**************************************************************************
2  *
3  * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 #ifndef _TTM_TT_H_
28 #define _TTM_TT_H_
29 
30 #include <linux/pagemap.h>
31 #include <linux/types.h>
32 #include <drm/ttm/ttm_caching.h>
33 #include <drm/ttm/ttm_kmap_iter.h>
34 
35 struct ttm_backup;
36 struct ttm_device;
37 struct ttm_tt;
38 struct ttm_resource;
39 struct ttm_buffer_object;
40 struct ttm_operation_ctx;
41 struct ttm_pool_tt_restore;
42 
43 /**
44  * struct ttm_tt - This is a structure holding the pages, caching- and aperture
45  * binding status for a buffer object that isn't backed by fixed (VRAM / AGP)
46  * memory.
47  */
48 struct ttm_tt {
49 	/** @pages: Array of pages backing the data. */
50 	struct page **pages;
51 	/**
52 	 * @page_flags: The page flags.
53 	 *
54 	 * Supported values:
55 	 *
56 	 * TTM_TT_FLAG_SWAPPED: Set by TTM when the pages have been unpopulated
57 	 * and swapped out by TTM.  Calling ttm_tt_populate() will then swap the
58 	 * pages back in, and unset the flag. Drivers should in general never
59 	 * need to touch this.
60 	 *
61 	 * TTM_TT_FLAG_ZERO_ALLOC: Set if the pages will be zeroed on
62 	 * allocation.
63 	 *
64 	 * TTM_TT_FLAG_EXTERNAL: Set if the underlying pages were allocated
65 	 * externally, like with dma-buf or userptr. This effectively disables
66 	 * TTM swapping out such pages.  Also important is to prevent TTM from
67 	 * ever directly mapping these pages.
68 	 *
69 	 * Note that enum ttm_bo_type.ttm_bo_type_sg objects will always enable
70 	 * this flag.
71 	 *
72 	 * TTM_TT_FLAG_EXTERNAL_MAPPABLE: Same behaviour as
73 	 * TTM_TT_FLAG_EXTERNAL, but with the reduced restriction that it is
74 	 * still valid to use TTM to map the pages directly. This is useful when
75 	 * implementing a ttm_tt backend which still allocates driver owned
76 	 * pages underneath(say with shmem).
77 	 *
78 	 * Note that since this also implies TTM_TT_FLAG_EXTERNAL, the usage
79 	 * here should always be:
80 	 *
81 	 *   page_flags = TTM_TT_FLAG_EXTERNAL |
82 	 *		  TTM_TT_FLAG_EXTERNAL_MAPPABLE;
83 	 *
84 	 * TTM_TT_FLAG_DECRYPTED: The mapped ttm pages should be marked as
85 	 * not encrypted. The framework will try to match what the dma layer
86 	 * is doing, but note that it is a little fragile because ttm page
87 	 * fault handling abuses the DMA api a bit and dma_map_attrs can't be
88 	 * used to assure pgprot always matches.
89 	 *
90 	 * TTM_TT_FLAG_BACKED_UP: TTM internal only. This is set if the
91 	 * struct ttm_tt has been (possibly partially) backed up.
92 	 *
93 	 * TTM_TT_FLAG_PRIV_POPULATED: TTM internal only. DO NOT USE. This is
94 	 * set by TTM after ttm_tt_populate() has successfully returned, and is
95 	 * then unset when TTM calls ttm_tt_unpopulate().
96 	 *
97 	 */
98 #define TTM_TT_FLAG_SWAPPED		BIT(0)
99 #define TTM_TT_FLAG_ZERO_ALLOC		BIT(1)
100 #define TTM_TT_FLAG_EXTERNAL		BIT(2)
101 #define TTM_TT_FLAG_EXTERNAL_MAPPABLE	BIT(3)
102 #define TTM_TT_FLAG_DECRYPTED		BIT(4)
103 #define TTM_TT_FLAG_BACKED_UP	        BIT(5)
104 
105 #define TTM_TT_FLAG_PRIV_POPULATED	BIT(6)
106 	uint32_t page_flags;
107 	/** @num_pages: Number of pages in the page array. */
108 	uint32_t num_pages;
109 	/** @sg: for SG objects via dma-buf. */
110 	struct sg_table *sg;
111 	/** @dma_address: The DMA (bus) addresses of the pages. */
112 	dma_addr_t *dma_address;
113 	/** @swap_storage: Pointer to shmem struct file for swap storage. */
114 	struct file *swap_storage;
115 	/**
116 	 * @backup: Pointer to backup struct for backed up tts.
117 	 * Could be unified with @swap_storage. Meanwhile, the driver's
118 	 * ttm_tt_create() callback is responsible for assigning
119 	 * this field.
120 	 */
121 	struct file *backup;
122 	/**
123 	 * @caching: The current caching state of the pages, see enum
124 	 * ttm_caching.
125 	 */
126 	enum ttm_caching caching;
127 	/** @restore: Partial restoration from backup state. TTM private */
128 	struct ttm_pool_tt_restore *restore;
129 };
130 
131 /**
132  * struct ttm_kmap_iter_tt - Specialization of a mappig iterator for a tt.
133  * @base: Embedded struct ttm_kmap_iter providing the usage interface
134  * @tt: Cached struct ttm_tt.
135  * @prot: Cached page protection for mapping.
136  */
137 struct ttm_kmap_iter_tt {
138 	struct ttm_kmap_iter base;
139 	struct ttm_tt *tt;
140 	pgprot_t prot;
141 };
142 
143 static inline bool ttm_tt_is_populated(struct ttm_tt *tt)
144 {
145 	return tt->page_flags & TTM_TT_FLAG_PRIV_POPULATED;
146 }
147 
148 /**
149  * ttm_tt_is_swapped() - Whether the ttm_tt is swapped out or backed up
150  * @tt: The struct ttm_tt.
151  *
152  * Return: true if swapped or backed up, false otherwise.
153  */
154 static inline bool ttm_tt_is_swapped(const struct ttm_tt *tt)
155 {
156 	return tt->page_flags & (TTM_TT_FLAG_SWAPPED | TTM_TT_FLAG_BACKED_UP);
157 }
158 
159 /**
160  * ttm_tt_is_backed_up() - Whether the ttm_tt backed up
161  * @tt: The struct ttm_tt.
162  *
163  * Return: true if swapped or backed up, false otherwise.
164  */
165 static inline bool ttm_tt_is_backed_up(const struct ttm_tt *tt)
166 {
167 	return tt->page_flags & TTM_TT_FLAG_BACKED_UP;
168 }
169 
170 /**
171  * ttm_tt_clear_backed_up() - Clear the ttm_tt backed-up status
172  * @tt: The struct ttm_tt.
173  *
174  * Drivers can use this functionto clear the backed-up status,
175  * for example before destroying or re-validating a purged tt.
176  */
177 static inline void ttm_tt_clear_backed_up(struct ttm_tt *tt)
178 {
179 	tt->page_flags &= ~TTM_TT_FLAG_BACKED_UP;
180 }
181 
182 /**
183  * ttm_tt_create
184  *
185  * @bo: pointer to a struct ttm_buffer_object
186  * @zero_alloc: true if allocated pages needs to be zeroed
187  *
188  * Make sure we have a TTM structure allocated for the given BO.
189  * No pages are actually allocated.
190  */
191 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc);
192 
193 /**
194  * ttm_tt_init
195  *
196  * @ttm: The struct ttm_tt.
197  * @bo: The buffer object we create the ttm for.
198  * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.
199  * @caching: the desired caching state of the pages
200  * @extra_pages: Extra pages needed for the driver.
201  *
202  * Create a struct ttm_tt to back data with system memory pages.
203  * No pages are actually allocated.
204  * Returns:
205  * NULL: Out of memory.
206  */
207 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
208 		uint32_t page_flags, enum ttm_caching caching,
209 		unsigned long extra_pages);
210 int ttm_sg_tt_init(struct ttm_tt *ttm_dma, struct ttm_buffer_object *bo,
211 		   uint32_t page_flags, enum ttm_caching caching);
212 
213 /**
214  * ttm_tt_fini
215  *
216  * @ttm: the ttm_tt structure.
217  *
218  * Free memory of ttm_tt structure
219  */
220 void ttm_tt_fini(struct ttm_tt *ttm);
221 
222 /**
223  * ttm_tt_destroy:
224  *
225  * @bdev: the ttm_device this object belongs to
226  * @ttm: The struct ttm_tt.
227  *
228  * Unbind, unpopulate and destroy common struct ttm_tt.
229  */
230 void ttm_tt_destroy(struct ttm_device *bdev, struct ttm_tt *ttm);
231 
232 /**
233  * ttm_tt_swapin:
234  *
235  * @ttm: The struct ttm_tt.
236  *
237  * Swap in a previously swap out ttm_tt.
238  */
239 int ttm_tt_swapin(struct ttm_tt *ttm);
240 int ttm_tt_swapout(struct ttm_device *bdev, struct ttm_tt *ttm,
241 		   gfp_t gfp_flags);
242 
243 /**
244  * ttm_tt_populate - allocate pages for a ttm
245  *
246  * @bdev: the ttm_device this object belongs to
247  * @ttm: Pointer to the ttm_tt structure
248  * @ctx: operation context for populating the tt object.
249  *
250  * Calls the driver method to allocate pages for a ttm
251  */
252 int ttm_tt_populate(struct ttm_device *bdev, struct ttm_tt *ttm,
253 		    struct ttm_operation_ctx *ctx);
254 
255 /**
256  * ttm_tt_unpopulate - free pages from a ttm
257  *
258  * @bdev: the ttm_device this object belongs to
259  * @ttm: Pointer to the ttm_tt structure
260  *
261  * Calls the driver method to free all pages from a ttm
262  */
263 void ttm_tt_unpopulate(struct ttm_device *bdev, struct ttm_tt *ttm);
264 
265 /**
266  * ttm_tt_mark_for_clear - Mark pages for clearing on populate.
267  *
268  * @ttm: Pointer to the ttm_tt structure
269  *
270  * Marks pages for clearing so that the next time the page vector is
271  * populated, the pages will be cleared.
272  */
273 static inline void ttm_tt_mark_for_clear(struct ttm_tt *ttm)
274 {
275 	ttm->page_flags |= TTM_TT_FLAG_ZERO_ALLOC;
276 }
277 
278 void ttm_tt_mgr_init(unsigned long num_pages, unsigned long num_dma32_pages);
279 
280 struct ttm_kmap_iter *ttm_kmap_iter_tt_init(struct ttm_kmap_iter_tt *iter_tt,
281 					    struct ttm_tt *tt);
282 unsigned long ttm_tt_pages_limit(void);
283 
284 /**
285  * struct ttm_backup_flags - Flags to govern backup behaviour.
286  * @purge: Free pages without backing up. Bypass pools.
287  * @writeback: Attempt to copy contents directly to swap space, even
288  * if that means blocking on writes to external memory.
289  */
290 struct ttm_backup_flags {
291 	u32 purge : 1;
292 	u32 writeback : 1;
293 };
294 
295 long ttm_tt_backup(struct ttm_device *bdev, struct ttm_tt *tt,
296 		   const struct ttm_backup_flags flags);
297 
298 int ttm_tt_restore(struct ttm_device *bdev, struct ttm_tt *tt,
299 		   const struct ttm_operation_ctx *ctx);
300 
301 int ttm_tt_setup_backup(struct ttm_tt *tt);
302 
303 #if IS_ENABLED(CONFIG_AGP)
304 #include <linux/agp_backend.h>
305 
306 /**
307  * ttm_agp_tt_create
308  *
309  * @bo: Buffer object we allocate the ttm for.
310  * @bridge: The agp bridge this device is sitting on.
311  * @page_flags: Page flags as identified by TTM_TT_FLAG_XX flags.
312  *
313  *
314  * Create a TTM backend that uses the indicated AGP bridge as an aperture
315  * for TT memory. This function uses the linux agpgart interface to
316  * bind and unbind memory backing a ttm_tt.
317  */
318 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
319 				 struct agp_bridge_data *bridge,
320 				 uint32_t page_flags);
321 int ttm_agp_bind(struct ttm_tt *ttm, struct ttm_resource *bo_mem);
322 void ttm_agp_unbind(struct ttm_tt *ttm);
323 void ttm_agp_destroy(struct ttm_tt *ttm);
324 bool ttm_agp_is_bound(struct ttm_tt *ttm);
325 #endif
326 
327 #endif
328