1 /* 2 * Copyright 2020 Advanced Micro Devices, Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 * 22 * Authors: Christian König 23 */ 24 25 #include <linux/dma-buf-map.h> 26 #include <linux/io-mapping.h> 27 #include <linux/scatterlist.h> 28 29 #include <drm/ttm/ttm_resource.h> 30 #include <drm/ttm/ttm_bo_driver.h> 31 32 void ttm_resource_init(struct ttm_buffer_object *bo, 33 const struct ttm_place *place, 34 struct ttm_resource *res) 35 { 36 res->start = 0; 37 res->num_pages = PFN_UP(bo->base.size); 38 res->mem_type = place->mem_type; 39 res->placement = place->flags; 40 res->bus.addr = NULL; 41 res->bus.offset = 0; 42 res->bus.is_iomem = false; 43 res->bus.caching = ttm_cached; 44 } 45 EXPORT_SYMBOL(ttm_resource_init); 46 47 int ttm_resource_alloc(struct ttm_buffer_object *bo, 48 const struct ttm_place *place, 49 struct ttm_resource **res_ptr) 50 { 51 struct ttm_resource_manager *man = 52 ttm_manager_type(bo->bdev, place->mem_type); 53 54 return man->func->alloc(man, bo, place, res_ptr); 55 } 56 57 void ttm_resource_free(struct ttm_buffer_object *bo, struct ttm_resource **res) 58 { 59 struct ttm_resource_manager *man; 60 61 if (!*res) 62 return; 63 64 man = ttm_manager_type(bo->bdev, (*res)->mem_type); 65 man->func->free(man, *res); 66 *res = NULL; 67 } 68 EXPORT_SYMBOL(ttm_resource_free); 69 70 /** 71 * ttm_resource_manager_init 72 * 73 * @man: memory manager object to init 74 * @p_size: size managed area in pages. 75 * 76 * Initialise core parts of a manager object. 77 */ 78 void ttm_resource_manager_init(struct ttm_resource_manager *man, 79 unsigned long p_size) 80 { 81 unsigned i; 82 83 mtx_init(&man->move_lock, IPL_NONE); 84 man->size = p_size; 85 86 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) 87 INIT_LIST_HEAD(&man->lru[i]); 88 man->move = NULL; 89 } 90 EXPORT_SYMBOL(ttm_resource_manager_init); 91 92 /* 93 * ttm_resource_manager_evict_all 94 * 95 * @bdev - device to use 96 * @man - manager to use 97 * 98 * Evict all the objects out of a memory manager until it is empty. 99 * Part of memory manager cleanup sequence. 100 */ 101 int ttm_resource_manager_evict_all(struct ttm_device *bdev, 102 struct ttm_resource_manager *man) 103 { 104 struct ttm_operation_ctx ctx = { 105 .interruptible = false, 106 .no_wait_gpu = false, 107 .force_alloc = true 108 }; 109 struct dma_fence *fence; 110 int ret; 111 unsigned i; 112 113 /* 114 * Can't use standard list traversal since we're unlocking. 115 */ 116 117 spin_lock(&bdev->lru_lock); 118 for (i = 0; i < TTM_MAX_BO_PRIORITY; ++i) { 119 while (!list_empty(&man->lru[i])) { 120 spin_unlock(&bdev->lru_lock); 121 ret = ttm_mem_evict_first(bdev, man, NULL, &ctx, 122 NULL); 123 if (ret) 124 return ret; 125 spin_lock(&bdev->lru_lock); 126 } 127 } 128 spin_unlock(&bdev->lru_lock); 129 130 spin_lock(&man->move_lock); 131 fence = dma_fence_get(man->move); 132 spin_unlock(&man->move_lock); 133 134 if (fence) { 135 ret = dma_fence_wait(fence, false); 136 dma_fence_put(fence); 137 if (ret) 138 return ret; 139 } 140 141 return 0; 142 } 143 EXPORT_SYMBOL(ttm_resource_manager_evict_all); 144 145 /** 146 * ttm_resource_manager_debug 147 * 148 * @man: manager type to dump. 149 * @p: printer to use for debug. 150 */ 151 void ttm_resource_manager_debug(struct ttm_resource_manager *man, 152 struct drm_printer *p) 153 { 154 drm_printf(p, " use_type: %d\n", man->use_type); 155 drm_printf(p, " use_tt: %d\n", man->use_tt); 156 drm_printf(p, " size: %llu\n", man->size); 157 if (man->func->debug) 158 man->func->debug(man, p); 159 } 160 EXPORT_SYMBOL(ttm_resource_manager_debug); 161 162 static void ttm_kmap_iter_iomap_map_local(struct ttm_kmap_iter *iter, 163 struct dma_buf_map *dmap, 164 pgoff_t i, bus_space_tag_t bst) 165 { 166 struct ttm_kmap_iter_iomap *iter_io = 167 container_of(iter, typeof(*iter_io), base); 168 void __iomem *addr; 169 170 retry: 171 while (i >= iter_io->cache.end) { 172 iter_io->cache.sg = iter_io->cache.sg ? 173 sg_next(iter_io->cache.sg) : iter_io->st->sgl; 174 iter_io->cache.i = iter_io->cache.end; 175 iter_io->cache.end += sg_dma_len(iter_io->cache.sg) >> 176 PAGE_SHIFT; 177 iter_io->cache.offs = sg_dma_address(iter_io->cache.sg) - 178 iter_io->start; 179 } 180 181 if (i < iter_io->cache.i) { 182 iter_io->cache.end = 0; 183 iter_io->cache.sg = NULL; 184 goto retry; 185 } 186 187 #ifdef __linux__ 188 addr = io_mapping_map_local_wc(iter_io->iomap, iter_io->cache.offs + 189 (((resource_size_t)i - iter_io->cache.i) 190 << PAGE_SHIFT)); 191 #else 192 if (bus_space_map(bst, iter_io->cache.offs + 193 (((resource_size_t)i - iter_io->cache.i) << PAGE_SHIFT), 194 PAGE_SIZE, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, 195 &dmap->bsh)) { 196 printf("%s bus_space_map failed\n", __func__); 197 addr = 0; 198 } else { 199 addr = bus_space_vaddr(bst, dmap->bsh); 200 } 201 #endif 202 dma_buf_map_set_vaddr_iomem(dmap, addr); 203 } 204 205 static void ttm_kmap_iter_iomap_unmap_local(struct ttm_kmap_iter *iter, 206 struct dma_buf_map *map, bus_space_tag_t bst) 207 { 208 #ifdef notyet 209 io_mapping_unmap_local(map->vaddr_iomem); 210 #else 211 bus_space_unmap(bst, map->bsh, PAGE_SIZE); 212 #endif 213 } 214 215 static const struct ttm_kmap_iter_ops ttm_kmap_iter_io_ops = { 216 .map_local = ttm_kmap_iter_iomap_map_local, 217 .unmap_local = ttm_kmap_iter_iomap_unmap_local, 218 .maps_tt = false, 219 }; 220 221 /** 222 * ttm_kmap_iter_iomap_init - Initialize a struct ttm_kmap_iter_iomap 223 * @iter_io: The struct ttm_kmap_iter_iomap to initialize. 224 * @iomap: The struct io_mapping representing the underlying linear io_memory. 225 * @st: sg_table into @iomap, representing the memory of the struct 226 * ttm_resource. 227 * @start: Offset that needs to be subtracted from @st to make 228 * sg_dma_address(st->sgl) - @start == 0 for @iomap start. 229 * 230 * Return: Pointer to the embedded struct ttm_kmap_iter. 231 */ 232 struct ttm_kmap_iter * 233 ttm_kmap_iter_iomap_init(struct ttm_kmap_iter_iomap *iter_io, 234 struct io_mapping *iomap, 235 struct sg_table *st, 236 resource_size_t start) 237 { 238 iter_io->base.ops = &ttm_kmap_iter_io_ops; 239 iter_io->iomap = iomap; 240 iter_io->st = st; 241 iter_io->start = start; 242 memset(&iter_io->cache, 0, sizeof(iter_io->cache)); 243 244 return &iter_io->base; 245 } 246 EXPORT_SYMBOL(ttm_kmap_iter_iomap_init); 247 248 /** 249 * DOC: Linear io iterator 250 * 251 * This code should die in the not too near future. Best would be if we could 252 * make io-mapping use memremap for all io memory, and have memremap 253 * implement a kmap_local functionality. We could then strip a huge amount of 254 * code. These linear io iterators are implemented to mimic old functionality, 255 * and they don't use kmap_local semantics at all internally. Rather ioremap or 256 * friends, and at least on 32-bit they add global TLB flushes and points 257 * of failure. 258 */ 259 260 static void ttm_kmap_iter_linear_io_map_local(struct ttm_kmap_iter *iter, 261 struct dma_buf_map *dmap, 262 pgoff_t i, bus_space_tag_t bst) 263 { 264 struct ttm_kmap_iter_linear_io *iter_io = 265 container_of(iter, typeof(*iter_io), base); 266 267 *dmap = iter_io->dmap; 268 dma_buf_map_incr(dmap, i * PAGE_SIZE); 269 } 270 271 static const struct ttm_kmap_iter_ops ttm_kmap_iter_linear_io_ops = { 272 .map_local = ttm_kmap_iter_linear_io_map_local, 273 .maps_tt = false, 274 }; 275 276 /** 277 * ttm_kmap_iter_linear_io_init - Initialize an iterator for linear io memory 278 * @iter_io: The iterator to initialize 279 * @bdev: The TTM device 280 * @mem: The ttm resource representing the iomap. 281 * 282 * This function is for internal TTM use only. It sets up a memcpy kmap iterator 283 * pointing at a linear chunk of io memory. 284 * 285 * Return: A pointer to the embedded struct ttm_kmap_iter or error pointer on 286 * failure. 287 */ 288 struct ttm_kmap_iter * 289 ttm_kmap_iter_linear_io_init(struct ttm_kmap_iter_linear_io *iter_io, 290 struct ttm_device *bdev, 291 struct ttm_resource *mem) 292 { 293 int ret; 294 295 ret = ttm_mem_io_reserve(bdev, mem); 296 if (ret) 297 goto out_err; 298 if (!mem->bus.is_iomem) { 299 ret = -EINVAL; 300 goto out_io_free; 301 } 302 303 if (mem->bus.addr) { 304 dma_buf_map_set_vaddr(&iter_io->dmap, mem->bus.addr); 305 iter_io->needs_unmap = false; 306 } else { 307 size_t bus_size = (size_t)mem->num_pages << PAGE_SHIFT; 308 309 iter_io->needs_unmap = true; 310 memset(&iter_io->dmap, 0, sizeof(iter_io->dmap)); 311 if (mem->bus.caching == ttm_write_combined) { 312 #ifdef __linux__ 313 dma_buf_map_set_vaddr_iomem(&iter_io->dmap, 314 ioremap_wc(mem->bus.offset, 315 bus_size)); 316 #else 317 if (bus_space_map(bdev->memt, mem->bus.offset, 318 bus_size, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, 319 &iter_io->dmap.bsh)) { 320 ret = -ENOMEM; 321 goto out_io_free; 322 } 323 iter_io->dmap.size = bus_size; 324 dma_buf_map_set_vaddr_iomem(&iter_io->dmap, 325 bus_space_vaddr(bdev->memt, iter_io->dmap.bsh)); 326 #endif 327 } 328 else if (mem->bus.caching == ttm_cached) { 329 #ifdef __linux__ 330 dma_buf_map_set_vaddr(&iter_io->dmap, 331 memremap(mem->bus.offset, bus_size, 332 MEMREMAP_WB | 333 MEMREMAP_WT | 334 MEMREMAP_WC)); 335 #else 336 if (bus_space_map(bdev->memt, mem->bus.offset, 337 bus_size, BUS_SPACE_MAP_LINEAR | BUS_SPACE_MAP_PREFETCHABLE, 338 &iter_io->dmap.bsh)) { 339 ret = -ENOMEM; 340 goto out_io_free; 341 } 342 iter_io->dmap.size = bus_size; 343 dma_buf_map_set_vaddr(&iter_io->dmap, 344 bus_space_vaddr(bdev->memt, iter_io->dmap.bsh)); 345 #endif 346 } 347 348 /* If uncached requested or if mapping cached or wc failed */ 349 if (dma_buf_map_is_null(&iter_io->dmap)) { 350 #ifdef __linux__ 351 dma_buf_map_set_vaddr_iomem(&iter_io->dmap, 352 ioremap(mem->bus.offset, 353 bus_size)); 354 #else 355 if (bus_space_map(bdev->memt, mem->bus.offset, 356 bus_size, BUS_SPACE_MAP_LINEAR, &iter_io->dmap.bsh)) { 357 ret = -ENOMEM; 358 goto out_io_free; 359 } 360 iter_io->dmap.size = bus_size; 361 dma_buf_map_set_vaddr_iomem(&iter_io->dmap, 362 bus_space_vaddr(bdev->memt, iter_io->dmap.bsh)); 363 #endif 364 } 365 366 if (dma_buf_map_is_null(&iter_io->dmap)) { 367 ret = -ENOMEM; 368 goto out_io_free; 369 } 370 } 371 372 iter_io->base.ops = &ttm_kmap_iter_linear_io_ops; 373 return &iter_io->base; 374 375 out_io_free: 376 ttm_mem_io_free(bdev, mem); 377 out_err: 378 return ERR_PTR(ret); 379 } 380 381 /** 382 * ttm_kmap_iter_linear_io_fini - Clean up an iterator for linear io memory 383 * @iter_io: The iterator to initialize 384 * @bdev: The TTM device 385 * @mem: The ttm resource representing the iomap. 386 * 387 * This function is for internal TTM use only. It cleans up a memcpy kmap 388 * iterator initialized by ttm_kmap_iter_linear_io_init. 389 */ 390 void 391 ttm_kmap_iter_linear_io_fini(struct ttm_kmap_iter_linear_io *iter_io, 392 struct ttm_device *bdev, 393 struct ttm_resource *mem) 394 { 395 if (iter_io->needs_unmap && dma_buf_map_is_set(&iter_io->dmap)) { 396 #ifdef __linux__ 397 if (iter_io->dmap.is_iomem) 398 iounmap(iter_io->dmap.vaddr_iomem); 399 else 400 memunmap(iter_io->dmap.vaddr); 401 #else 402 bus_space_unmap(bdev->memt, iter_io->dmap.bsh, 403 iter_io->dmap.size); 404 #endif 405 } 406 407 ttm_mem_io_free(bdev, mem); 408 } 409