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