1 /* $NetBSD: ttm_tt.h,v 1.7 2021/12/19 12:29:16 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 const 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 #ifdef __NetBSD__ 115 struct uvm_object *swap_storage; 116 #else 117 struct file *swap_storage; 118 #endif 119 enum ttm_caching_state caching_state; 120 enum { 121 tt_bound, 122 tt_unbound, 123 tt_unpopulated, 124 } state; 125 }; 126 127 /** 128 * struct ttm_dma_tt 129 * 130 * @ttm: Base ttm_tt struct. 131 * @dma_address: The DMA (bus) addresses of the pages 132 * @pages_list: used by some page allocation backend 133 * 134 * This is a structure holding the pages, caching- and aperture binding 135 * status for a buffer object that isn't backed by fixed (VRAM / AGP) 136 * memory. 137 */ 138 struct ttm_dma_tt { 139 struct ttm_tt ttm; 140 #ifdef __NetBSD__ 141 bus_dmamap_t dma_address; 142 #else 143 dma_addr_t *dma_address; 144 #endif 145 struct list_head pages_list; 146 }; 147 148 /** 149 * ttm_tt_create 150 * 151 * @bo: pointer to a struct ttm_buffer_object 152 * @zero_alloc: true if allocated pages needs to be zeroed 153 * 154 * Make sure we have a TTM structure allocated for the given BO. 155 * No pages are actually allocated. 156 */ 157 int ttm_tt_create(struct ttm_buffer_object *bo, bool zero_alloc); 158 159 /** 160 * ttm_tt_init 161 * 162 * @ttm: The struct ttm_tt. 163 * @bo: The buffer object we create the ttm for. 164 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags. 165 * 166 * Create a struct ttm_tt to back data with system memory pages. 167 * No pages are actually allocated. 168 * Returns: 169 * NULL: Out of memory. 170 */ 171 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_buffer_object *bo, 172 uint32_t page_flags); 173 int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo, 174 uint32_t page_flags); 175 int ttm_sg_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_buffer_object *bo, 176 uint32_t page_flags); 177 178 /** 179 * ttm_tt_fini 180 * 181 * @ttm: the ttm_tt structure. 182 * 183 * Free memory of ttm_tt structure 184 */ 185 void ttm_tt_fini(struct ttm_tt *ttm); 186 void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma); 187 188 /** 189 * ttm_ttm_bind: 190 * 191 * @ttm: The struct ttm_tt containing backing pages. 192 * @bo_mem: The struct ttm_mem_reg identifying the binding location. 193 * 194 * Bind the pages of @ttm to an aperture location identified by @bo_mem 195 */ 196 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem, 197 struct ttm_operation_ctx *ctx); 198 199 /** 200 * ttm_ttm_destroy: 201 * 202 * @ttm: The struct ttm_tt. 203 * 204 * Unbind, unpopulate and destroy common struct ttm_tt. 205 */ 206 void ttm_tt_destroy(struct ttm_tt *ttm); 207 208 /** 209 * ttm_ttm_unbind: 210 * 211 * @ttm: The struct ttm_tt. 212 * 213 * Unbind a struct ttm_tt. 214 */ 215 void ttm_tt_unbind(struct ttm_tt *ttm); 216 217 #ifdef __NetBSD__ 218 /** 219 * ttm_tt_wire 220 * 221 * @ttm The struct ttm_tt. 222 * 223 * Wire the pages of a ttm_tt, allocating pages for it if necessary. 224 */ 225 extern int ttm_tt_wire(struct ttm_tt *ttm); 226 227 /** 228 * ttm_tt_unwire 229 * 230 * @ttm The struct ttm_tt. 231 * 232 * Unwire the pages of a ttm_tt. 233 */ 234 extern void ttm_tt_unwire(struct ttm_tt *ttm); 235 #else 236 /** 237 * ttm_tt_swapin: 238 * 239 * @ttm: The struct ttm_tt. 240 * 241 * Swap in a previously swap out ttm_tt. 242 */ 243 int ttm_tt_swapin(struct ttm_tt *ttm); 244 #endif 245 246 /** 247 * ttm_tt_set_placement_caching: 248 * 249 * @ttm A struct ttm_tt the backing pages of which will change caching policy. 250 * @placement: Flag indicating the desired caching policy. 251 * 252 * This function will change caching policy of any default kernel mappings of 253 * the pages backing @ttm. If changing from cached to uncached or 254 * write-combined, 255 * all CPU caches will first be flushed to make sure the data of the pages 256 * hit RAM. This function may be very costly as it involves global TLB 257 * and cache flushes and potential page splitting / combining. 258 */ 259 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement); 260 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage); 261 262 /** 263 * ttm_tt_populate - allocate pages for a ttm 264 * 265 * @ttm: Pointer to the ttm_tt structure 266 * 267 * Calls the driver method to allocate pages for a ttm 268 */ 269 int ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx); 270 271 /** 272 * ttm_tt_unpopulate - free pages from a ttm 273 * 274 * @ttm: Pointer to the ttm_tt structure 275 * 276 * Calls the driver method to free all pages from a ttm 277 */ 278 void ttm_tt_unpopulate(struct ttm_tt *ttm); 279 280 #if IS_ENABLED(CONFIG_AGP) 281 #include <linux/agp_backend.h> 282 283 /** 284 * ttm_agp_tt_create 285 * 286 * @bo: Buffer object we allocate the ttm for. 287 * @bridge: The agp bridge this device is sitting on. 288 * @page_flags: Page flags as identified by TTM_PAGE_FLAG_XX flags. 289 * 290 * 291 * Create a TTM backend that uses the indicated AGP bridge as an aperture 292 * for TT memory. This function uses the linux agpgart interface to 293 * bind and unbind memory backing a ttm_tt. 294 */ 295 struct ttm_tt *ttm_agp_tt_create(struct ttm_buffer_object *bo, 296 struct agp_bridge_data *bridge, 297 uint32_t page_flags); 298 int ttm_agp_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx); 299 void ttm_agp_tt_unpopulate(struct ttm_tt *ttm); 300 #endif 301 302 #endif 303