ttm_tt.h revision 1.3 1 /* $NetBSD: ttm_tt.h,v 1.3 2021/12/19 09:57:09 riastradh Exp $ */
2
3 /**************************************************************************
4 *
5 * Copyright (c) 2006-2009 Vmware, Inc., Palo Alto, CA., USA
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
23 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
24 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
25 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
26 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29 #ifndef _TTM_TT_H_
30 #define _TTM_TT_H_
31
32 #include <linux/types.h>
33
34 struct ttm_tt;
35 struct ttm_mem_reg;
36 struct ttm_buffer_object;
37 struct ttm_operation_ctx;
38
39 #define TTM_PAGE_FLAG_WRITE (1 << 3)
40 #define TTM_PAGE_FLAG_SWAPPED (1 << 4)
41 #define TTM_PAGE_FLAG_PERSISTENT_SWAP (1 << 5)
42 #define TTM_PAGE_FLAG_ZERO_ALLOC (1 << 6)
43 #define TTM_PAGE_FLAG_DMA32 (1 << 7)
44 #define TTM_PAGE_FLAG_SG (1 << 8)
45 #define TTM_PAGE_FLAG_NO_RETRY (1 << 9)
46
47 enum ttm_caching_state {
48 tt_uncached,
49 tt_wc,
50 tt_cached
51 };
52
53 struct ttm_backend_func {
54 /**
55 * struct ttm_backend_func member bind
56 *
57 * @ttm: Pointer to a struct ttm_tt.
58 * @bo_mem: Pointer to a struct ttm_mem_reg describing the
59 * memory type and location for binding.
60 *
61 * Bind the backend pages into the aperture in the location
62 * indicated by @bo_mem. This function should be able to handle
63 * differences between aperture and system page sizes.
64 */
65 int (*bind) (struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem);
66
67 /**
68 * struct ttm_backend_func member unbind
69 *
70 * @ttm: Pointer to a struct ttm_tt.
71 *
72 * Unbind previously bound backend pages. This function should be
73 * able to handle differences between aperture and system page sizes.
74 */
75 int (*unbind) (struct ttm_tt *ttm);
76
77 /**
78 * struct ttm_backend_func member destroy
79 *
80 * @ttm: Pointer to a struct ttm_tt.
81 *
82 * Destroy the backend. This will be call back from ttm_tt_destroy so
83 * don't call ttm_tt_destroy from the callback or infinite loop.
84 */
85 void (*destroy) (struct ttm_tt *ttm);
86 };
87
88 /**
89 * struct ttm_tt
90 *
91 * @bdev: Pointer to a struct ttm_bo_device.
92 * @func: Pointer to a struct ttm_backend_func that describes
93 * the backend methods.
94 * pointer.
95 * @pages: Array of pages backing the data.
96 * @num_pages: Number of pages in the page array.
97 * @bdev: Pointer to the current struct ttm_bo_device.
98 * @be: Pointer to the ttm backend.
99 * @swap_storage: Pointer to shmem struct file for swap storage.
100 * @caching_state: The current caching state of the pages.
101 * @state: The current binding state of the pages.
102 *
103 * This is a structure holding the pages, caching- and aperture binding
104 * status for a buffer object that isn't backed by fixed (VRAM / AGP)
105 * memory.
106 */
107 struct ttm_tt {
108 struct ttm_bo_device *bdev;
109 struct ttm_backend_func *func;
110 struct page **pages;
111 uint32_t page_flags;
112 unsigned long num_pages;
113 struct sg_table *sg; /* for SG objects via dma-buf */
114 struct file *swap_storage;
115 enum ttm_caching_state caching_state;
116 enum {
117 tt_bound,
118 tt_unbound,
119 tt_unpopulated,
120 } state;
121 };
122
123 /**
124 * struct ttm_dma_tt
125 *
126 * @ttm: Base ttm_tt struct.
127 * @dma_address: The DMA (bus) addresses of the pages
128 * @pages_list: used by some page allocation backend
129 *
130 * This is a structure holding the pages, caching- and aperture binding
131 * status for a buffer object that isn't backed by fixed (VRAM / AGP)
132 * memory.
133 */
134 struct ttm_dma_tt {
135 struct ttm_tt ttm;
136 #ifdef __NetBSD__
137 bus_dma_segment_t *dma_segs;
138 bus_dmamap_t dma_address;
139 #else
140 dma_addr_t *dma_address;
141 #endif
142 struct list_head pages_list;
143 };
144
145 /**
146 * ttm_tt_create
147 *
148 * @bo: pointer to a struct ttm_buffer_object
149 * @zero_alloc: true if allocated pages needs to be zeroed
150 *
151 * Make sure we have a TTM structure allocated for the given BO.
152 * No pages are actually allocated.
153 */
154 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc);
155
156 /**
157 * ttm_tt_init
158 *
159 * @ttm: The struct ttm_tt.
160 * @bo: The buffer object we create the ttm for.
161 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
162 *
163 * Create a struct ttm_tt to back data with system memory pages.
164 * No pages are actually allocated.
165 * Returns:
166 * NULL: Out of memory.
167 */
168 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo,
169 uint32_t page_flags);
170 int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
171 uint32_t page_flags);
172 int ttm_sg_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo,
173 uint32_t page_flags);
174
175 /**
176 * ttm_tt_fini
177 *
178 * @ttm: the ttm_tt structure.
179 *
180 * Free memory of ttm_tt structure
181 */
182 void ttm_tt_fini(struct ttm_tt *ttm);
183 void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma);
184
185 /**
186 * ttm_ttm_bind:
187 *
188 * @ttm: The struct ttm_tt containing backing pages.
189 * @bo_mem: The struct ttm_mem_reg identifying the binding location.
190 *
191 * Bind the pages of @ttm to an aperture location identified by @bo_mem
192 */
193 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem,
194 struct ttm_operation_ctx *ctx);
195
196 /**
197 * ttm_ttm_destroy:
198 *
199 * @ttm: The struct ttm_tt.
200 *
201 * Unbind, unpopulate and destroy common struct ttm_tt.
202 */
203 void ttm_tt_destroy(struct ttm_tt *ttm);
204
205 /**
206 * ttm_ttm_unbind:
207 *
208 * @ttm: The struct ttm_tt.
209 *
210 * Unbind a struct ttm_tt.
211 */
212 void ttm_tt_unbind(struct ttm_tt *ttm);
213
214 /**
215 * ttm_tt_swapin:
216 *
217 * @ttm: The struct ttm_tt.
218 *
219 * Swap in a previously swap out ttm_tt.
220 */
221 int ttm_tt_swapin(struct ttm_tt *ttm);
222
223 /**
224 * ttm_tt_set_placement_caching:
225 *
226 * @ttm A struct ttm_tt the backing pages of which will change caching policy.
227 * @placement: Flag indicating the desired caching policy.
228 *
229 * This function will change caching policy of any default kernel mappings of
230 * the pages backing @ttm. If changing from cached to uncached or
231 * write-combined,
232 * all CPU caches will first be flushed to make sure the data of the pages
233 * hit RAM. This function may be very costly as it involves global TLB
234 * and cache flushes and potential page splitting / combining.
235 */
236 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement);
237 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage);
238
239 /**
240 * ttm_tt_populate - allocate pages for a ttm
241 *
242 * @ttm: Pointer to the ttm_tt structure
243 *
244 * Calls the driver method to allocate pages for a ttm
245 */
246 int ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx);
247
248 /**
249 * ttm_tt_unpopulate - free pages from a ttm
250 *
251 * @ttm: Pointer to the ttm_tt structure
252 *
253 * Calls the driver method to free all pages from a ttm
254 */
255 void ttm_tt_unpopulate(struct ttm_tt *ttm);
256
257 #if IS_ENABLED(CONFIG_AGP)
258 #include <linux/agp_backend.h>
259
260 /**
261 * ttm_agp_tt_create
262 *
263 * @bo: Buffer object we allocate the ttm for.
264 * @bridge: The agp bridge this device is sitting on.
265 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags.
266 *
267 *
268 * Create a TTM backend that uses the indicated AGP bridge as an aperture
269 * for TT memory. This function uses the linux agpgart interface to
270 * bind and unbind memory backing a ttm_tt.
271 */
272 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo,
273 struct agp_bridge_data *bridge,
274 uint32_t page_flags);
275 int ttm_agp_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx);
276 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm);
277 #endif
278
279 #endif
280