1 1.20 joe /* $NetBSD: nouveau_bo.c,v 1.20 2025/03/23 17:04:09 joe Exp $ */ 2 1.2 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2007 Dave Airlied 5 1.1 riastrad * All Rights Reserved. 6 1.1 riastrad * 7 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 8 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 9 1.1 riastrad * to deal in the Software without restriction, including without limitation 10 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 12 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 13 1.1 riastrad * 14 1.1 riastrad * The above copyright notice and this permission notice (including the next 15 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the 16 1.1 riastrad * Software. 17 1.1 riastrad * 18 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21 1.1 riastrad * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 22 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 24 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 25 1.1 riastrad */ 26 1.1 riastrad /* 27 1.1 riastrad * Authors: Dave Airlied <airlied (at) linux.ie> 28 1.1 riastrad * Ben Skeggs <darktama (at) iinet.net.au> 29 1.1 riastrad * Jeremy Kolb <jkolb (at) brandeis.edu> 30 1.1 riastrad */ 31 1.1 riastrad 32 1.2 riastrad #include <sys/cdefs.h> 33 1.20 joe __KERNEL_RCSID(0, "$NetBSD: nouveau_bo.c,v 1.20 2025/03/23 17:04:09 joe Exp $"); 34 1.2 riastrad 35 1.8 riastrad #include <linux/dma-mapping.h> 36 1.1 riastrad #include <linux/swiotlb.h> 37 1.1 riastrad 38 1.16 riastrad #include "nouveau_drv.h" 39 1.1 riastrad #include "nouveau_dma.h" 40 1.1 riastrad #include "nouveau_fence.h" 41 1.1 riastrad 42 1.1 riastrad #include "nouveau_bo.h" 43 1.1 riastrad #include "nouveau_ttm.h" 44 1.1 riastrad #include "nouveau_gem.h" 45 1.16 riastrad #include "nouveau_mem.h" 46 1.16 riastrad #include "nouveau_vmm.h" 47 1.16 riastrad 48 1.16 riastrad #include <nvif/class.h> 49 1.16 riastrad #include <nvif/if500b.h> 50 1.16 riastrad #include <nvif/if900b.h> 51 1.1 riastrad 52 1.1 riastrad /* 53 1.1 riastrad * NV10-NV40 tiling helpers 54 1.1 riastrad */ 55 1.1 riastrad 56 1.1 riastrad static void 57 1.1 riastrad nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg, 58 1.1 riastrad u32 addr, u32 size, u32 pitch, u32 flags) 59 1.1 riastrad { 60 1.1 riastrad struct nouveau_drm *drm = nouveau_drm(dev); 61 1.1 riastrad int i = reg - drm->tile.reg; 62 1.16 riastrad struct nvkm_fb *fb = nvxx_fb(&drm->client.device); 63 1.8 riastrad struct nvkm_fb_tile *tile = &fb->tile.region[i]; 64 1.1 riastrad 65 1.1 riastrad nouveau_fence_unref(®->fence); 66 1.1 riastrad 67 1.1 riastrad if (tile->pitch) 68 1.8 riastrad nvkm_fb_tile_fini(fb, i, tile); 69 1.1 riastrad 70 1.1 riastrad if (pitch) 71 1.8 riastrad nvkm_fb_tile_init(fb, i, addr, size, pitch, flags, tile); 72 1.1 riastrad 73 1.8 riastrad nvkm_fb_tile_prog(fb, i, tile); 74 1.1 riastrad } 75 1.1 riastrad 76 1.1 riastrad static struct nouveau_drm_tile * 77 1.1 riastrad nv10_bo_get_tile_region(struct drm_device *dev, int i) 78 1.1 riastrad { 79 1.1 riastrad struct nouveau_drm *drm = nouveau_drm(dev); 80 1.1 riastrad struct nouveau_drm_tile *tile = &drm->tile.reg[i]; 81 1.1 riastrad 82 1.1 riastrad spin_lock(&drm->tile.lock); 83 1.1 riastrad 84 1.1 riastrad if (!tile->used && 85 1.1 riastrad (!tile->fence || nouveau_fence_done(tile->fence))) 86 1.1 riastrad tile->used = true; 87 1.1 riastrad else 88 1.1 riastrad tile = NULL; 89 1.1 riastrad 90 1.1 riastrad spin_unlock(&drm->tile.lock); 91 1.1 riastrad return tile; 92 1.1 riastrad } 93 1.1 riastrad 94 1.1 riastrad static void 95 1.1 riastrad nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile, 96 1.16 riastrad struct dma_fence *fence) 97 1.1 riastrad { 98 1.1 riastrad struct nouveau_drm *drm = nouveau_drm(dev); 99 1.1 riastrad 100 1.1 riastrad if (tile) { 101 1.1 riastrad spin_lock(&drm->tile.lock); 102 1.16 riastrad tile->fence = (struct nouveau_fence *)dma_fence_get(fence); 103 1.1 riastrad tile->used = false; 104 1.1 riastrad spin_unlock(&drm->tile.lock); 105 1.1 riastrad } 106 1.1 riastrad } 107 1.1 riastrad 108 1.1 riastrad static struct nouveau_drm_tile * 109 1.1 riastrad nv10_bo_set_tiling(struct drm_device *dev, u32 addr, 110 1.16 riastrad u32 size, u32 pitch, u32 zeta) 111 1.1 riastrad { 112 1.1 riastrad struct nouveau_drm *drm = nouveau_drm(dev); 113 1.16 riastrad struct nvkm_fb *fb = nvxx_fb(&drm->client.device); 114 1.1 riastrad struct nouveau_drm_tile *tile, *found = NULL; 115 1.1 riastrad int i; 116 1.1 riastrad 117 1.8 riastrad for (i = 0; i < fb->tile.regions; i++) { 118 1.1 riastrad tile = nv10_bo_get_tile_region(dev, i); 119 1.1 riastrad 120 1.1 riastrad if (pitch && !found) { 121 1.1 riastrad found = tile; 122 1.1 riastrad continue; 123 1.1 riastrad 124 1.8 riastrad } else if (tile && fb->tile.region[i].pitch) { 125 1.1 riastrad /* Kill an unused tile region. */ 126 1.1 riastrad nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0); 127 1.1 riastrad } 128 1.1 riastrad 129 1.1 riastrad nv10_bo_put_tile_region(dev, tile, NULL); 130 1.1 riastrad } 131 1.1 riastrad 132 1.1 riastrad if (found) 133 1.16 riastrad nv10_bo_update_tile_region(dev, found, addr, size, pitch, zeta); 134 1.1 riastrad return found; 135 1.1 riastrad } 136 1.1 riastrad 137 1.1 riastrad static void 138 1.1 riastrad nouveau_bo_del_ttm(struct ttm_buffer_object *bo) 139 1.1 riastrad { 140 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 141 1.1 riastrad struct drm_device *dev = drm->dev; 142 1.1 riastrad struct nouveau_bo *nvbo = nouveau_bo(bo); 143 1.1 riastrad 144 1.1 riastrad WARN_ON(nvbo->pin_refcnt > 0); 145 1.1 riastrad nv10_bo_put_tile_region(dev, nvbo->tile, NULL); 146 1.16 riastrad 147 1.16 riastrad /* 148 1.16 riastrad * If nouveau_bo_new() allocated this buffer, the GEM object was never 149 1.16 riastrad * initialized, so don't attempt to release it. 150 1.16 riastrad */ 151 1.16 riastrad if (bo->base.dev) 152 1.16 riastrad drm_gem_object_release(&bo->base); 153 1.16 riastrad 154 1.1 riastrad kfree(nvbo); 155 1.1 riastrad } 156 1.1 riastrad 157 1.16 riastrad static inline u64 158 1.16 riastrad roundup_64(u64 x, u32 y) 159 1.16 riastrad { 160 1.16 riastrad x += y - 1; 161 1.16 riastrad do_div(x, y); 162 1.16 riastrad return x * y; 163 1.16 riastrad } 164 1.16 riastrad 165 1.1 riastrad static void 166 1.1 riastrad nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags, 167 1.16 riastrad int *align, u64 *size) 168 1.1 riastrad { 169 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); 170 1.16 riastrad struct nvif_device *device = &drm->client.device; 171 1.1 riastrad 172 1.8 riastrad if (device->info.family < NV_DEVICE_INFO_V0_TESLA) { 173 1.16 riastrad if (nvbo->mode) { 174 1.8 riastrad if (device->info.chipset >= 0x40) { 175 1.1 riastrad *align = 65536; 176 1.16 riastrad *size = roundup_64(*size, 64 * nvbo->mode); 177 1.1 riastrad 178 1.8 riastrad } else if (device->info.chipset >= 0x30) { 179 1.1 riastrad *align = 32768; 180 1.16 riastrad *size = roundup_64(*size, 64 * nvbo->mode); 181 1.1 riastrad 182 1.8 riastrad } else if (device->info.chipset >= 0x20) { 183 1.1 riastrad *align = 16384; 184 1.16 riastrad *size = roundup_64(*size, 64 * nvbo->mode); 185 1.1 riastrad 186 1.8 riastrad } else if (device->info.chipset >= 0x10) { 187 1.1 riastrad *align = 16384; 188 1.16 riastrad *size = roundup_64(*size, 32 * nvbo->mode); 189 1.1 riastrad } 190 1.1 riastrad } 191 1.1 riastrad } else { 192 1.16 riastrad *size = roundup_64(*size, (1 << nvbo->page)); 193 1.16 riastrad *align = max((1 << nvbo->page), *align); 194 1.1 riastrad } 195 1.1 riastrad 196 1.16 riastrad *size = roundup_64(*size, PAGE_SIZE); 197 1.1 riastrad } 198 1.1 riastrad 199 1.16 riastrad struct nouveau_bo * 200 1.16 riastrad nouveau_bo_alloc(struct nouveau_cli *cli, u64 *size, int *align, u32 flags, 201 1.16 riastrad u32 tile_mode, u32 tile_flags) 202 1.1 riastrad { 203 1.16 riastrad struct nouveau_drm *drm = cli->drm; 204 1.1 riastrad struct nouveau_bo *nvbo; 205 1.16 riastrad struct nvif_mmu *mmu = &cli->mmu; 206 1.16 riastrad struct nvif_vmm *vmm = cli->svm.cli ? &cli->svm.vmm : &cli->vmm.vmm; 207 1.16 riastrad int i, pi = -1; 208 1.16 riastrad 209 1.16 riastrad if (!*size) { 210 1.18 riastrad NV_WARN(drm, "skipped size %016"PRIx64"\n", *size); 211 1.16 riastrad return ERR_PTR(-EINVAL); 212 1.1 riastrad } 213 1.1 riastrad 214 1.1 riastrad nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL); 215 1.1 riastrad if (!nvbo) 216 1.16 riastrad return ERR_PTR(-ENOMEM); 217 1.1 riastrad INIT_LIST_HEAD(&nvbo->head); 218 1.1 riastrad INIT_LIST_HEAD(&nvbo->entry); 219 1.1 riastrad INIT_LIST_HEAD(&nvbo->vma_list); 220 1.1 riastrad nvbo->bo.bdev = &drm->ttm.bdev; 221 1.1 riastrad 222 1.16 riastrad /* This is confusing, and doesn't actually mean we want an uncached 223 1.16 riastrad * mapping, but is what NOUVEAU_GEM_DOMAIN_COHERENT gets translated 224 1.16 riastrad * into in nouveau_gem_new(). 225 1.16 riastrad */ 226 1.16 riastrad if (flags & TTM_PL_FLAG_UNCACHED) { 227 1.16 riastrad /* Determine if we can get a cache-coherent map, forcing 228 1.16 riastrad * uncached mapping if we can't. 229 1.16 riastrad */ 230 1.16 riastrad if (!nouveau_drm_use_coherent_gpu_mapping(drm)) 231 1.16 riastrad nvbo->force_coherent = true; 232 1.16 riastrad } 233 1.16 riastrad 234 1.16 riastrad if (cli->device.info.family >= NV_DEVICE_INFO_V0_FERMI) { 235 1.16 riastrad nvbo->kind = (tile_flags & 0x0000ff00) >> 8; 236 1.16 riastrad if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) { 237 1.20 joe goto err; 238 1.16 riastrad } 239 1.16 riastrad 240 1.16 riastrad nvbo->comp = mmu->kind[nvbo->kind] != nvbo->kind; 241 1.16 riastrad } else 242 1.16 riastrad if (cli->device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 243 1.16 riastrad nvbo->kind = (tile_flags & 0x00007f00) >> 8; 244 1.16 riastrad nvbo->comp = (tile_flags & 0x00030000) >> 16; 245 1.16 riastrad if (!nvif_mmu_kind_valid(mmu, nvbo->kind)) { 246 1.20 joe goto err; 247 1.16 riastrad } 248 1.16 riastrad } else { 249 1.16 riastrad nvbo->zeta = (tile_flags & 0x00000007); 250 1.16 riastrad } 251 1.16 riastrad nvbo->mode = tile_mode; 252 1.16 riastrad nvbo->contig = !(tile_flags & NOUVEAU_GEM_TILE_NONCONTIG); 253 1.16 riastrad 254 1.16 riastrad /* Determine the desirable target GPU page size for the buffer. */ 255 1.16 riastrad for (i = 0; i < vmm->page_nr; i++) { 256 1.16 riastrad /* Because we cannot currently allow VMM maps to fail 257 1.16 riastrad * during buffer migration, we need to determine page 258 1.16 riastrad * size for the buffer up-front, and pre-allocate its 259 1.16 riastrad * page tables. 260 1.16 riastrad * 261 1.16 riastrad * Skip page sizes that can't support needed domains. 262 1.16 riastrad */ 263 1.16 riastrad if (cli->device.info.family > NV_DEVICE_INFO_V0_CURIE && 264 1.16 riastrad (flags & TTM_PL_FLAG_VRAM) && !vmm->page[i].vram) 265 1.16 riastrad continue; 266 1.16 riastrad if ((flags & TTM_PL_FLAG_TT) && 267 1.16 riastrad (!vmm->page[i].host || vmm->page[i].shift > PAGE_SHIFT)) 268 1.16 riastrad continue; 269 1.16 riastrad 270 1.16 riastrad /* Select this page size if it's the first that supports 271 1.16 riastrad * the potential memory domains, or when it's compatible 272 1.16 riastrad * with the requested compression settings. 273 1.16 riastrad */ 274 1.16 riastrad if (pi < 0 || !nvbo->comp || vmm->page[i].comp) 275 1.16 riastrad pi = i; 276 1.16 riastrad 277 1.16 riastrad /* Stop once the buffer is larger than the current page size. */ 278 1.16 riastrad if (*size >= 1ULL << vmm->page[i].shift) 279 1.16 riastrad break; 280 1.16 riastrad } 281 1.16 riastrad 282 1.16 riastrad if (WARN_ON(pi < 0)) 283 1.20 joe goto err; 284 1.8 riastrad 285 1.16 riastrad /* Disable compression if suitable settings couldn't be found. */ 286 1.16 riastrad if (nvbo->comp && !vmm->page[pi].comp) { 287 1.16 riastrad if (mmu->object.oclass >= NVIF_CLASS_MMU_GF100) 288 1.16 riastrad nvbo->kind = mmu->kind[nvbo->kind]; 289 1.16 riastrad nvbo->comp = 0; 290 1.1 riastrad } 291 1.16 riastrad nvbo->page = vmm->page[pi].shift; 292 1.16 riastrad 293 1.16 riastrad nouveau_bo_fixup_align(nvbo, flags, align, size); 294 1.16 riastrad 295 1.16 riastrad return nvbo; 296 1.20 joe 297 1.20 joe err: 298 1.20 joe kfree(nvbo); 299 1.20 joe return ERR_PTR(-EINVAL); 300 1.16 riastrad } 301 1.16 riastrad 302 1.16 riastrad int 303 1.16 riastrad nouveau_bo_init(struct nouveau_bo *nvbo, u64 size, int align, u32 flags, 304 1.16 riastrad struct sg_table *sg, struct dma_resv *robj) 305 1.16 riastrad { 306 1.16 riastrad int type = sg ? ttm_bo_type_sg : ttm_bo_type_device; 307 1.16 riastrad size_t acc_size; 308 1.16 riastrad int ret; 309 1.16 riastrad 310 1.16 riastrad acc_size = ttm_bo_dma_acc_size(nvbo->bo.bdev, size, sizeof(*nvbo)); 311 1.1 riastrad 312 1.1 riastrad nvbo->bo.mem.num_pages = size >> PAGE_SHIFT; 313 1.1 riastrad nouveau_bo_placement_set(nvbo, flags, 0); 314 1.1 riastrad 315 1.16 riastrad ret = ttm_bo_init(nvbo->bo.bdev, &nvbo->bo, size, type, 316 1.16 riastrad &nvbo->placement, align >> PAGE_SHIFT, false, 317 1.16 riastrad acc_size, sg, robj, nouveau_bo_del_ttm); 318 1.1 riastrad if (ret) { 319 1.1 riastrad /* ttm will call nouveau_bo_del_ttm if it fails.. */ 320 1.1 riastrad return ret; 321 1.1 riastrad } 322 1.1 riastrad 323 1.16 riastrad return 0; 324 1.16 riastrad } 325 1.16 riastrad 326 1.16 riastrad int 327 1.16 riastrad nouveau_bo_new(struct nouveau_cli *cli, u64 size, int align, 328 1.16 riastrad uint32_t flags, uint32_t tile_mode, uint32_t tile_flags, 329 1.16 riastrad struct sg_table *sg, struct dma_resv *robj, 330 1.16 riastrad struct nouveau_bo **pnvbo) 331 1.16 riastrad { 332 1.16 riastrad struct nouveau_bo *nvbo; 333 1.16 riastrad int ret; 334 1.16 riastrad 335 1.16 riastrad nvbo = nouveau_bo_alloc(cli, &size, &align, flags, tile_mode, 336 1.16 riastrad tile_flags); 337 1.16 riastrad if (IS_ERR(nvbo)) 338 1.16 riastrad return PTR_ERR(nvbo); 339 1.16 riastrad 340 1.16 riastrad ret = nouveau_bo_init(nvbo, size, align, flags, sg, robj); 341 1.16 riastrad if (ret) 342 1.16 riastrad return ret; 343 1.16 riastrad 344 1.1 riastrad *pnvbo = nvbo; 345 1.1 riastrad return 0; 346 1.1 riastrad } 347 1.1 riastrad 348 1.1 riastrad static void 349 1.8 riastrad set_placement_list(struct ttm_place *pl, unsigned *n, uint32_t type, uint32_t flags) 350 1.1 riastrad { 351 1.1 riastrad *n = 0; 352 1.1 riastrad 353 1.1 riastrad if (type & TTM_PL_FLAG_VRAM) 354 1.8 riastrad pl[(*n)++].flags = TTM_PL_FLAG_VRAM | flags; 355 1.1 riastrad if (type & TTM_PL_FLAG_TT) 356 1.8 riastrad pl[(*n)++].flags = TTM_PL_FLAG_TT | flags; 357 1.1 riastrad if (type & TTM_PL_FLAG_SYSTEM) 358 1.8 riastrad pl[(*n)++].flags = TTM_PL_FLAG_SYSTEM | flags; 359 1.1 riastrad } 360 1.1 riastrad 361 1.1 riastrad static void 362 1.1 riastrad set_placement_range(struct nouveau_bo *nvbo, uint32_t type) 363 1.1 riastrad { 364 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); 365 1.16 riastrad u32 vram_pages = drm->client.device.info.ram_size >> PAGE_SHIFT; 366 1.8 riastrad unsigned i, fpfn, lpfn; 367 1.1 riastrad 368 1.16 riastrad if (drm->client.device.info.family == NV_DEVICE_INFO_V0_CELSIUS && 369 1.16 riastrad nvbo->mode && (type & TTM_PL_FLAG_VRAM) && 370 1.1 riastrad nvbo->bo.mem.num_pages < vram_pages / 4) { 371 1.1 riastrad /* 372 1.1 riastrad * Make sure that the color and depth buffers are handled 373 1.1 riastrad * by independent memory controller units. Up to a 9x 374 1.1 riastrad * speed up when alpha-blending and depth-test are enabled 375 1.1 riastrad * at the same time. 376 1.1 riastrad */ 377 1.16 riastrad if (nvbo->zeta) { 378 1.8 riastrad fpfn = vram_pages / 2; 379 1.8 riastrad lpfn = ~0; 380 1.1 riastrad } else { 381 1.8 riastrad fpfn = 0; 382 1.8 riastrad lpfn = vram_pages / 2; 383 1.8 riastrad } 384 1.8 riastrad for (i = 0; i < nvbo->placement.num_placement; ++i) { 385 1.8 riastrad nvbo->placements[i].fpfn = fpfn; 386 1.8 riastrad nvbo->placements[i].lpfn = lpfn; 387 1.8 riastrad } 388 1.8 riastrad for (i = 0; i < nvbo->placement.num_busy_placement; ++i) { 389 1.8 riastrad nvbo->busy_placements[i].fpfn = fpfn; 390 1.8 riastrad nvbo->busy_placements[i].lpfn = lpfn; 391 1.1 riastrad } 392 1.1 riastrad } 393 1.1 riastrad } 394 1.1 riastrad 395 1.1 riastrad void 396 1.1 riastrad nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy) 397 1.1 riastrad { 398 1.1 riastrad struct ttm_placement *pl = &nvbo->placement; 399 1.8 riastrad uint32_t flags = (nvbo->force_coherent ? TTM_PL_FLAG_UNCACHED : 400 1.8 riastrad TTM_PL_MASK_CACHING) | 401 1.8 riastrad (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0); 402 1.1 riastrad 403 1.1 riastrad pl->placement = nvbo->placements; 404 1.1 riastrad set_placement_list(nvbo->placements, &pl->num_placement, 405 1.1 riastrad type, flags); 406 1.1 riastrad 407 1.1 riastrad pl->busy_placement = nvbo->busy_placements; 408 1.1 riastrad set_placement_list(nvbo->busy_placements, &pl->num_busy_placement, 409 1.1 riastrad type | busy, flags); 410 1.1 riastrad 411 1.1 riastrad set_placement_range(nvbo, type); 412 1.1 riastrad } 413 1.1 riastrad 414 1.1 riastrad int 415 1.8 riastrad nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype, bool contig) 416 1.1 riastrad { 417 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); 418 1.1 riastrad struct ttm_buffer_object *bo = &nvbo->bo; 419 1.8 riastrad bool force = false, evict = false; 420 1.1 riastrad int ret; 421 1.1 riastrad 422 1.16 riastrad ret = ttm_bo_reserve(bo, false, false, NULL); 423 1.1 riastrad if (ret) 424 1.8 riastrad return ret; 425 1.8 riastrad 426 1.16 riastrad if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA && 427 1.8 riastrad memtype == TTM_PL_FLAG_VRAM && contig) { 428 1.16 riastrad if (!nvbo->contig) { 429 1.16 riastrad nvbo->contig = true; 430 1.8 riastrad force = true; 431 1.16 riastrad evict = true; 432 1.8 riastrad } 433 1.8 riastrad } 434 1.1 riastrad 435 1.8 riastrad if (nvbo->pin_refcnt) { 436 1.8 riastrad if (!(memtype & (1 << bo->mem.mem_type)) || evict) { 437 1.8 riastrad NV_ERROR(drm, "bo %p pinned elsewhere: " 438 1.8 riastrad "0x%08x vs 0x%08x\n", bo, 439 1.8 riastrad 1 << bo->mem.mem_type, memtype); 440 1.8 riastrad ret = -EBUSY; 441 1.8 riastrad } 442 1.8 riastrad nvbo->pin_refcnt++; 443 1.1 riastrad goto out; 444 1.1 riastrad } 445 1.1 riastrad 446 1.8 riastrad if (evict) { 447 1.8 riastrad nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT, 0); 448 1.8 riastrad ret = nouveau_bo_validate(nvbo, false, false); 449 1.8 riastrad if (ret) 450 1.8 riastrad goto out; 451 1.8 riastrad } 452 1.1 riastrad 453 1.8 riastrad nvbo->pin_refcnt++; 454 1.1 riastrad nouveau_bo_placement_set(nvbo, memtype, 0); 455 1.1 riastrad 456 1.8 riastrad /* drop pin_refcnt temporarily, so we don't trip the assertion 457 1.8 riastrad * in nouveau_bo_move() that makes sure we're not trying to 458 1.8 riastrad * move a pinned buffer 459 1.8 riastrad */ 460 1.8 riastrad nvbo->pin_refcnt--; 461 1.1 riastrad ret = nouveau_bo_validate(nvbo, false, false); 462 1.8 riastrad if (ret) 463 1.8 riastrad goto out; 464 1.8 riastrad nvbo->pin_refcnt++; 465 1.8 riastrad 466 1.8 riastrad switch (bo->mem.mem_type) { 467 1.8 riastrad case TTM_PL_VRAM: 468 1.8 riastrad drm->gem.vram_available -= bo->mem.size; 469 1.8 riastrad break; 470 1.8 riastrad case TTM_PL_TT: 471 1.8 riastrad drm->gem.gart_available -= bo->mem.size; 472 1.8 riastrad break; 473 1.8 riastrad default: 474 1.8 riastrad break; 475 1.1 riastrad } 476 1.8 riastrad 477 1.1 riastrad out: 478 1.8 riastrad if (force && ret) 479 1.16 riastrad nvbo->contig = false; 480 1.1 riastrad ttm_bo_unreserve(bo); 481 1.1 riastrad return ret; 482 1.1 riastrad } 483 1.1 riastrad 484 1.1 riastrad int 485 1.1 riastrad nouveau_bo_unpin(struct nouveau_bo *nvbo) 486 1.1 riastrad { 487 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); 488 1.1 riastrad struct ttm_buffer_object *bo = &nvbo->bo; 489 1.1 riastrad int ret, ref; 490 1.1 riastrad 491 1.16 riastrad ret = ttm_bo_reserve(bo, false, false, NULL); 492 1.1 riastrad if (ret) 493 1.1 riastrad return ret; 494 1.1 riastrad 495 1.1 riastrad ref = --nvbo->pin_refcnt; 496 1.1 riastrad WARN_ON_ONCE(ref < 0); 497 1.1 riastrad if (ref) 498 1.1 riastrad goto out; 499 1.1 riastrad 500 1.1 riastrad nouveau_bo_placement_set(nvbo, bo->mem.placement, 0); 501 1.1 riastrad 502 1.1 riastrad ret = nouveau_bo_validate(nvbo, false, false); 503 1.1 riastrad if (ret == 0) { 504 1.1 riastrad switch (bo->mem.mem_type) { 505 1.1 riastrad case TTM_PL_VRAM: 506 1.1 riastrad drm->gem.vram_available += bo->mem.size; 507 1.1 riastrad break; 508 1.1 riastrad case TTM_PL_TT: 509 1.1 riastrad drm->gem.gart_available += bo->mem.size; 510 1.1 riastrad break; 511 1.1 riastrad default: 512 1.1 riastrad break; 513 1.1 riastrad } 514 1.1 riastrad } 515 1.1 riastrad 516 1.1 riastrad out: 517 1.1 riastrad ttm_bo_unreserve(bo); 518 1.1 riastrad return ret; 519 1.1 riastrad } 520 1.1 riastrad 521 1.1 riastrad int 522 1.1 riastrad nouveau_bo_map(struct nouveau_bo *nvbo) 523 1.1 riastrad { 524 1.1 riastrad int ret; 525 1.1 riastrad 526 1.16 riastrad ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL); 527 1.1 riastrad if (ret) 528 1.1 riastrad return ret; 529 1.1 riastrad 530 1.16 riastrad ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap); 531 1.8 riastrad 532 1.1 riastrad ttm_bo_unreserve(&nvbo->bo); 533 1.1 riastrad return ret; 534 1.1 riastrad } 535 1.1 riastrad 536 1.1 riastrad void 537 1.1 riastrad nouveau_bo_unmap(struct nouveau_bo *nvbo) 538 1.1 riastrad { 539 1.8 riastrad if (!nvbo) 540 1.8 riastrad return; 541 1.8 riastrad 542 1.16 riastrad ttm_bo_kunmap(&nvbo->kmap); 543 1.1 riastrad } 544 1.1 riastrad 545 1.8 riastrad void 546 1.8 riastrad nouveau_bo_sync_for_device(struct nouveau_bo *nvbo) 547 1.8 riastrad { 548 1.8 riastrad struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); 549 1.8 riastrad struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm; 550 1.10 riastrad #ifndef __NetBSD__ 551 1.8 riastrad int i; 552 1.10 riastrad #endif 553 1.8 riastrad 554 1.8 riastrad if (!ttm_dma) 555 1.8 riastrad return; 556 1.8 riastrad 557 1.8 riastrad /* Don't waste time looping if the object is coherent */ 558 1.8 riastrad if (nvbo->force_coherent) 559 1.8 riastrad return; 560 1.8 riastrad 561 1.10 riastrad #ifdef __NetBSD__ 562 1.18 riastrad bus_dma_tag_t dmat = drm->dev->dmat; 563 1.10 riastrad bus_dmamap_sync(dmat, ttm_dma->dma_address, 0, 564 1.10 riastrad PAGE_SIZE*ttm_dma->ttm.num_pages, 565 1.10 riastrad BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE); 566 1.10 riastrad #else 567 1.8 riastrad for (i = 0; i < ttm_dma->ttm.num_pages; i++) 568 1.16 riastrad dma_sync_single_for_device(drm->dev->dev, 569 1.16 riastrad ttm_dma->dma_address[i], 570 1.8 riastrad PAGE_SIZE, DMA_TO_DEVICE); 571 1.10 riastrad #endif 572 1.8 riastrad } 573 1.8 riastrad 574 1.8 riastrad void 575 1.8 riastrad nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo) 576 1.8 riastrad { 577 1.8 riastrad struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); 578 1.8 riastrad struct ttm_dma_tt *ttm_dma = (struct ttm_dma_tt *)nvbo->bo.ttm; 579 1.10 riastrad #ifndef __NetBSD__ 580 1.8 riastrad int i; 581 1.10 riastrad #endif 582 1.8 riastrad 583 1.8 riastrad if (!ttm_dma) 584 1.8 riastrad return; 585 1.8 riastrad 586 1.8 riastrad /* Don't waste time looping if the object is coherent */ 587 1.8 riastrad if (nvbo->force_coherent) 588 1.8 riastrad return; 589 1.8 riastrad 590 1.10 riastrad #ifdef __NetBSD__ 591 1.18 riastrad bus_dma_tag_t dmat = drm->dev->dmat; 592 1.10 riastrad bus_dmamap_sync(dmat, ttm_dma->dma_address, 0, 593 1.10 riastrad PAGE_SIZE*ttm_dma->ttm.num_pages, 594 1.10 riastrad BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 595 1.10 riastrad #else 596 1.8 riastrad for (i = 0; i < ttm_dma->ttm.num_pages; i++) 597 1.16 riastrad dma_sync_single_for_cpu(drm->dev->dev, ttm_dma->dma_address[i], 598 1.8 riastrad PAGE_SIZE, DMA_FROM_DEVICE); 599 1.10 riastrad #endif 600 1.8 riastrad } 601 1.8 riastrad 602 1.1 riastrad int 603 1.1 riastrad nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible, 604 1.1 riastrad bool no_wait_gpu) 605 1.1 riastrad { 606 1.16 riastrad struct ttm_operation_ctx ctx = { interruptible, no_wait_gpu }; 607 1.1 riastrad int ret; 608 1.1 riastrad 609 1.16 riastrad ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, &ctx); 610 1.1 riastrad if (ret) 611 1.1 riastrad return ret; 612 1.1 riastrad 613 1.8 riastrad nouveau_bo_sync_for_device(nvbo); 614 1.8 riastrad 615 1.1 riastrad return 0; 616 1.1 riastrad } 617 1.1 riastrad 618 1.2 riastrad #ifdef __NetBSD__ 619 1.2 riastrad /* 620 1.2 riastrad * XXX Can't use bus_space here because this is all mapped through the 621 1.2 riastrad * radeon_bo abstraction. Can't assume we're x86 because this is 622 1.2 riastrad * Nouveau, not Intel. 623 1.2 riastrad */ 624 1.2 riastrad 625 1.2 riastrad # define __iomem volatile 626 1.4 riastrad # define __force 627 1.2 riastrad # define ioread16_native fake_ioread16_native 628 1.2 riastrad # define ioread32_native fake_ioread32_native 629 1.2 riastrad # define iowrite16_native fake_iowrite16_native 630 1.2 riastrad # define iowrite32_native fake_iowrite32_native 631 1.2 riastrad 632 1.13 christos #ifdef notdef 633 1.2 riastrad static inline uint16_t 634 1.2 riastrad ioread16_native(const void __iomem *ptr) 635 1.2 riastrad { 636 1.2 riastrad uint16_t v; 637 1.2 riastrad 638 1.2 riastrad v = *(const uint16_t __iomem *)ptr; 639 1.2 riastrad membar_consumer(); 640 1.2 riastrad 641 1.11 riastrad return v; 642 1.2 riastrad } 643 1.13 christos #endif 644 1.2 riastrad 645 1.2 riastrad static inline uint32_t 646 1.2 riastrad ioread32_native(const void __iomem *ptr) 647 1.2 riastrad { 648 1.2 riastrad uint32_t v; 649 1.2 riastrad 650 1.2 riastrad v = *(const uint32_t __iomem *)ptr; 651 1.2 riastrad membar_consumer(); 652 1.2 riastrad 653 1.11 riastrad return v; 654 1.2 riastrad } 655 1.2 riastrad 656 1.4 riastrad static inline void 657 1.4 riastrad iowrite16_native(uint16_t v, void __iomem *ptr) 658 1.2 riastrad { 659 1.2 riastrad 660 1.2 riastrad membar_producer(); 661 1.11 riastrad *(uint16_t __iomem *)ptr = v; 662 1.2 riastrad } 663 1.2 riastrad 664 1.4 riastrad static inline void 665 1.4 riastrad iowrite32_native(uint32_t v, void __iomem *ptr) 666 1.2 riastrad { 667 1.2 riastrad 668 1.2 riastrad membar_producer(); 669 1.11 riastrad *(uint32_t __iomem *)ptr = v; 670 1.2 riastrad } 671 1.2 riastrad #endif 672 1.2 riastrad 673 1.1 riastrad void 674 1.1 riastrad nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val) 675 1.1 riastrad { 676 1.1 riastrad bool is_iomem; 677 1.1 riastrad u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); 678 1.8 riastrad 679 1.16 riastrad mem += index; 680 1.8 riastrad 681 1.1 riastrad if (is_iomem) 682 1.1 riastrad iowrite16_native(val, (void __force __iomem *)mem); 683 1.1 riastrad else 684 1.1 riastrad *mem = val; 685 1.1 riastrad } 686 1.1 riastrad 687 1.1 riastrad u32 688 1.1 riastrad nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index) 689 1.1 riastrad { 690 1.1 riastrad bool is_iomem; 691 1.1 riastrad u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); 692 1.8 riastrad 693 1.16 riastrad mem += index; 694 1.8 riastrad 695 1.1 riastrad if (is_iomem) 696 1.1 riastrad return ioread32_native((void __force __iomem *)mem); 697 1.1 riastrad else 698 1.1 riastrad return *mem; 699 1.1 riastrad } 700 1.1 riastrad 701 1.1 riastrad void 702 1.1 riastrad nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val) 703 1.1 riastrad { 704 1.1 riastrad bool is_iomem; 705 1.1 riastrad u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem); 706 1.8 riastrad 707 1.16 riastrad mem += index; 708 1.8 riastrad 709 1.1 riastrad if (is_iomem) 710 1.1 riastrad iowrite32_native(val, (void __force __iomem *)mem); 711 1.1 riastrad else 712 1.1 riastrad *mem = val; 713 1.1 riastrad } 714 1.1 riastrad 715 1.2 riastrad #ifdef __NetBSD__ 716 1.2 riastrad # undef __iomem 717 1.4 riastrad # undef __force 718 1.2 riastrad # undef ioread16_native 719 1.2 riastrad # undef ioread32_native 720 1.2 riastrad # undef iowrite16_native 721 1.2 riastrad # undef iowrite32_native 722 1.2 riastrad #endif 723 1.2 riastrad 724 1.1 riastrad static struct ttm_tt * 725 1.16 riastrad nouveau_ttm_tt_create(struct ttm_buffer_object *bo, uint32_t page_flags) 726 1.1 riastrad { 727 1.8 riastrad #if IS_ENABLED(CONFIG_AGP) 728 1.16 riastrad struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 729 1.1 riastrad 730 1.8 riastrad if (drm->agp.bridge) { 731 1.16 riastrad return ttm_agp_tt_create(bo, drm->agp.bridge, page_flags); 732 1.1 riastrad } 733 1.1 riastrad #endif 734 1.1 riastrad 735 1.16 riastrad return nouveau_sgdma_create_ttm(bo, page_flags); 736 1.1 riastrad } 737 1.1 riastrad 738 1.1 riastrad static int 739 1.1 riastrad nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags) 740 1.1 riastrad { 741 1.1 riastrad /* We'll do this from user space. */ 742 1.1 riastrad return 0; 743 1.1 riastrad } 744 1.1 riastrad 745 1.1 riastrad static int 746 1.1 riastrad nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type, 747 1.1 riastrad struct ttm_mem_type_manager *man) 748 1.1 riastrad { 749 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(bdev); 750 1.16 riastrad struct nvif_mmu *mmu = &drm->client.mmu; 751 1.1 riastrad 752 1.1 riastrad switch (type) { 753 1.1 riastrad case TTM_PL_SYSTEM: 754 1.1 riastrad man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; 755 1.1 riastrad man->available_caching = TTM_PL_MASK_CACHING; 756 1.1 riastrad man->default_caching = TTM_PL_FLAG_CACHED; 757 1.1 riastrad break; 758 1.1 riastrad case TTM_PL_VRAM: 759 1.8 riastrad man->flags = TTM_MEMTYPE_FLAG_FIXED | 760 1.8 riastrad TTM_MEMTYPE_FLAG_MAPPABLE; 761 1.8 riastrad man->available_caching = TTM_PL_FLAG_UNCACHED | 762 1.8 riastrad TTM_PL_FLAG_WC; 763 1.8 riastrad man->default_caching = TTM_PL_FLAG_WC; 764 1.8 riastrad 765 1.16 riastrad if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 766 1.8 riastrad /* Some BARs do not support being ioremapped WC */ 767 1.16 riastrad const u8 type = mmu->type[drm->ttm.type_vram].type; 768 1.16 riastrad if (type & NVIF_MEM_UNCACHED) { 769 1.8 riastrad man->available_caching = TTM_PL_FLAG_UNCACHED; 770 1.8 riastrad man->default_caching = TTM_PL_FLAG_UNCACHED; 771 1.8 riastrad } 772 1.8 riastrad 773 1.1 riastrad man->func = &nouveau_vram_manager; 774 1.1 riastrad man->io_reserve_fastpath = false; 775 1.1 riastrad man->use_io_reserve_lru = true; 776 1.1 riastrad } else { 777 1.1 riastrad man->func = &ttm_bo_manager_func; 778 1.1 riastrad } 779 1.1 riastrad break; 780 1.1 riastrad case TTM_PL_TT: 781 1.16 riastrad if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) 782 1.1 riastrad man->func = &nouveau_gart_manager; 783 1.1 riastrad else 784 1.8 riastrad if (!drm->agp.bridge) 785 1.1 riastrad man->func = &nv04_gart_manager; 786 1.1 riastrad else 787 1.1 riastrad man->func = &ttm_bo_manager_func; 788 1.1 riastrad 789 1.8 riastrad if (drm->agp.bridge) { 790 1.1 riastrad man->flags = TTM_MEMTYPE_FLAG_MAPPABLE; 791 1.1 riastrad man->available_caching = TTM_PL_FLAG_UNCACHED | 792 1.1 riastrad TTM_PL_FLAG_WC; 793 1.1 riastrad man->default_caching = TTM_PL_FLAG_WC; 794 1.1 riastrad } else { 795 1.1 riastrad man->flags = TTM_MEMTYPE_FLAG_MAPPABLE | 796 1.1 riastrad TTM_MEMTYPE_FLAG_CMA; 797 1.1 riastrad man->available_caching = TTM_PL_MASK_CACHING; 798 1.1 riastrad man->default_caching = TTM_PL_FLAG_CACHED; 799 1.1 riastrad } 800 1.1 riastrad 801 1.1 riastrad break; 802 1.1 riastrad default: 803 1.1 riastrad return -EINVAL; 804 1.1 riastrad } 805 1.1 riastrad return 0; 806 1.1 riastrad } 807 1.1 riastrad 808 1.1 riastrad static void 809 1.1 riastrad nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl) 810 1.1 riastrad { 811 1.1 riastrad struct nouveau_bo *nvbo = nouveau_bo(bo); 812 1.1 riastrad 813 1.1 riastrad switch (bo->mem.mem_type) { 814 1.1 riastrad case TTM_PL_VRAM: 815 1.1 riastrad nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT, 816 1.1 riastrad TTM_PL_FLAG_SYSTEM); 817 1.1 riastrad break; 818 1.1 riastrad default: 819 1.1 riastrad nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0); 820 1.1 riastrad break; 821 1.1 riastrad } 822 1.1 riastrad 823 1.1 riastrad *pl = nvbo->placement; 824 1.1 riastrad } 825 1.1 riastrad 826 1.1 riastrad 827 1.1 riastrad static int 828 1.1 riastrad nve0_bo_move_init(struct nouveau_channel *chan, u32 handle) 829 1.1 riastrad { 830 1.1 riastrad int ret = RING_SPACE(chan, 2); 831 1.1 riastrad if (ret == 0) { 832 1.1 riastrad BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1); 833 1.1 riastrad OUT_RING (chan, handle & 0x0000ffff); 834 1.1 riastrad FIRE_RING (chan); 835 1.1 riastrad } 836 1.1 riastrad return ret; 837 1.1 riastrad } 838 1.1 riastrad 839 1.1 riastrad static int 840 1.1 riastrad nve0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo, 841 1.16 riastrad struct ttm_mem_reg *old_reg, struct ttm_mem_reg *new_reg) 842 1.1 riastrad { 843 1.16 riastrad struct nouveau_mem *mem = nouveau_mem(old_reg); 844 1.1 riastrad int ret = RING_SPACE(chan, 10); 845 1.1 riastrad if (ret == 0) { 846 1.1 riastrad BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8); 847 1.16 riastrad OUT_RING (chan, upper_32_bits(mem->vma[0].addr)); 848 1.16 riastrad OUT_RING (chan, lower_32_bits(mem->vma[0].addr)); 849 1.16 riastrad OUT_RING (chan, upper_32_bits(mem->vma[1].addr)); 850 1.16 riastrad OUT_RING (chan, lower_32_bits(mem->vma[1].addr)); 851 1.1 riastrad OUT_RING (chan, PAGE_SIZE); 852 1.1 riastrad OUT_RING (chan, PAGE_SIZE); 853 1.1 riastrad OUT_RING (chan, PAGE_SIZE); 854 1.16 riastrad OUT_RING (chan, new_reg->num_pages); 855 1.1 riastrad BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386); 856 1.1 riastrad } 857 1.1 riastrad return ret; 858 1.1 riastrad } 859 1.1 riastrad 860 1.1 riastrad static int 861 1.1 riastrad nvc0_bo_move_init(struct nouveau_channel *chan, u32 handle) 862 1.1 riastrad { 863 1.1 riastrad int ret = RING_SPACE(chan, 2); 864 1.1 riastrad if (ret == 0) { 865 1.1 riastrad BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1); 866 1.1 riastrad OUT_RING (chan, handle); 867 1.1 riastrad } 868 1.1 riastrad return ret; 869 1.1 riastrad } 870 1.1 riastrad 871 1.1 riastrad static int 872 1.1 riastrad nvc0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo, 873 1.16 riastrad struct ttm_mem_reg *old_reg, struct ttm_mem_reg *new_reg) 874 1.1 riastrad { 875 1.16 riastrad struct nouveau_mem *mem = nouveau_mem(old_reg); 876 1.16 riastrad u64 src_offset = mem->vma[0].addr; 877 1.16 riastrad u64 dst_offset = mem->vma[1].addr; 878 1.16 riastrad u32 page_count = new_reg->num_pages; 879 1.1 riastrad int ret; 880 1.1 riastrad 881 1.16 riastrad page_count = new_reg->num_pages; 882 1.1 riastrad while (page_count) { 883 1.1 riastrad int line_count = (page_count > 8191) ? 8191 : page_count; 884 1.1 riastrad 885 1.1 riastrad ret = RING_SPACE(chan, 11); 886 1.1 riastrad if (ret) 887 1.1 riastrad return ret; 888 1.1 riastrad 889 1.1 riastrad BEGIN_NVC0(chan, NvSubCopy, 0x030c, 8); 890 1.1 riastrad OUT_RING (chan, upper_32_bits(src_offset)); 891 1.1 riastrad OUT_RING (chan, lower_32_bits(src_offset)); 892 1.1 riastrad OUT_RING (chan, upper_32_bits(dst_offset)); 893 1.1 riastrad OUT_RING (chan, lower_32_bits(dst_offset)); 894 1.1 riastrad OUT_RING (chan, PAGE_SIZE); 895 1.1 riastrad OUT_RING (chan, PAGE_SIZE); 896 1.1 riastrad OUT_RING (chan, PAGE_SIZE); 897 1.1 riastrad OUT_RING (chan, line_count); 898 1.1 riastrad BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1); 899 1.1 riastrad OUT_RING (chan, 0x00000110); 900 1.1 riastrad 901 1.1 riastrad page_count -= line_count; 902 1.1 riastrad src_offset += (PAGE_SIZE * line_count); 903 1.1 riastrad dst_offset += (PAGE_SIZE * line_count); 904 1.1 riastrad } 905 1.1 riastrad 906 1.1 riastrad return 0; 907 1.1 riastrad } 908 1.1 riastrad 909 1.1 riastrad static int 910 1.1 riastrad nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo, 911 1.16 riastrad struct ttm_mem_reg *old_reg, struct ttm_mem_reg *new_reg) 912 1.1 riastrad { 913 1.16 riastrad struct nouveau_mem *mem = nouveau_mem(old_reg); 914 1.16 riastrad u64 src_offset = mem->vma[0].addr; 915 1.16 riastrad u64 dst_offset = mem->vma[1].addr; 916 1.16 riastrad u32 page_count = new_reg->num_pages; 917 1.1 riastrad int ret; 918 1.1 riastrad 919 1.16 riastrad page_count = new_reg->num_pages; 920 1.1 riastrad while (page_count) { 921 1.1 riastrad int line_count = (page_count > 2047) ? 2047 : page_count; 922 1.1 riastrad 923 1.1 riastrad ret = RING_SPACE(chan, 12); 924 1.1 riastrad if (ret) 925 1.1 riastrad return ret; 926 1.1 riastrad 927 1.1 riastrad BEGIN_NVC0(chan, NvSubCopy, 0x0238, 2); 928 1.1 riastrad OUT_RING (chan, upper_32_bits(dst_offset)); 929 1.1 riastrad OUT_RING (chan, lower_32_bits(dst_offset)); 930 1.1 riastrad BEGIN_NVC0(chan, NvSubCopy, 0x030c, 6); 931 1.1 riastrad OUT_RING (chan, upper_32_bits(src_offset)); 932 1.1 riastrad OUT_RING (chan, lower_32_bits(src_offset)); 933 1.1 riastrad OUT_RING (chan, PAGE_SIZE); /* src_pitch */ 934 1.1 riastrad OUT_RING (chan, PAGE_SIZE); /* dst_pitch */ 935 1.1 riastrad OUT_RING (chan, PAGE_SIZE); /* line_length */ 936 1.1 riastrad OUT_RING (chan, line_count); 937 1.1 riastrad BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1); 938 1.1 riastrad OUT_RING (chan, 0x00100110); 939 1.1 riastrad 940 1.1 riastrad page_count -= line_count; 941 1.1 riastrad src_offset += (PAGE_SIZE * line_count); 942 1.1 riastrad dst_offset += (PAGE_SIZE * line_count); 943 1.1 riastrad } 944 1.1 riastrad 945 1.1 riastrad return 0; 946 1.1 riastrad } 947 1.1 riastrad 948 1.1 riastrad static int 949 1.1 riastrad nva3_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo, 950 1.16 riastrad struct ttm_mem_reg *old_reg, struct ttm_mem_reg *new_reg) 951 1.1 riastrad { 952 1.16 riastrad struct nouveau_mem *mem = nouveau_mem(old_reg); 953 1.16 riastrad u64 src_offset = mem->vma[0].addr; 954 1.16 riastrad u64 dst_offset = mem->vma[1].addr; 955 1.16 riastrad u32 page_count = new_reg->num_pages; 956 1.1 riastrad int ret; 957 1.1 riastrad 958 1.16 riastrad page_count = new_reg->num_pages; 959 1.1 riastrad while (page_count) { 960 1.1 riastrad int line_count = (page_count > 8191) ? 8191 : page_count; 961 1.1 riastrad 962 1.1 riastrad ret = RING_SPACE(chan, 11); 963 1.1 riastrad if (ret) 964 1.1 riastrad return ret; 965 1.1 riastrad 966 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x030c, 8); 967 1.1 riastrad OUT_RING (chan, upper_32_bits(src_offset)); 968 1.1 riastrad OUT_RING (chan, lower_32_bits(src_offset)); 969 1.1 riastrad OUT_RING (chan, upper_32_bits(dst_offset)); 970 1.1 riastrad OUT_RING (chan, lower_32_bits(dst_offset)); 971 1.1 riastrad OUT_RING (chan, PAGE_SIZE); 972 1.1 riastrad OUT_RING (chan, PAGE_SIZE); 973 1.1 riastrad OUT_RING (chan, PAGE_SIZE); 974 1.1 riastrad OUT_RING (chan, line_count); 975 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0300, 1); 976 1.1 riastrad OUT_RING (chan, 0x00000110); 977 1.1 riastrad 978 1.1 riastrad page_count -= line_count; 979 1.1 riastrad src_offset += (PAGE_SIZE * line_count); 980 1.1 riastrad dst_offset += (PAGE_SIZE * line_count); 981 1.1 riastrad } 982 1.1 riastrad 983 1.1 riastrad return 0; 984 1.1 riastrad } 985 1.1 riastrad 986 1.1 riastrad static int 987 1.1 riastrad nv98_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo, 988 1.16 riastrad struct ttm_mem_reg *old_reg, struct ttm_mem_reg *new_reg) 989 1.1 riastrad { 990 1.16 riastrad struct nouveau_mem *mem = nouveau_mem(old_reg); 991 1.1 riastrad int ret = RING_SPACE(chan, 7); 992 1.1 riastrad if (ret == 0) { 993 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0320, 6); 994 1.16 riastrad OUT_RING (chan, upper_32_bits(mem->vma[0].addr)); 995 1.16 riastrad OUT_RING (chan, lower_32_bits(mem->vma[0].addr)); 996 1.16 riastrad OUT_RING (chan, upper_32_bits(mem->vma[1].addr)); 997 1.16 riastrad OUT_RING (chan, lower_32_bits(mem->vma[1].addr)); 998 1.1 riastrad OUT_RING (chan, 0x00000000 /* COPY */); 999 1.16 riastrad OUT_RING (chan, new_reg->num_pages << PAGE_SHIFT); 1000 1.1 riastrad } 1001 1.1 riastrad return ret; 1002 1.1 riastrad } 1003 1.1 riastrad 1004 1.1 riastrad static int 1005 1.1 riastrad nv84_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo, 1006 1.16 riastrad struct ttm_mem_reg *old_reg, struct ttm_mem_reg *new_reg) 1007 1.1 riastrad { 1008 1.16 riastrad struct nouveau_mem *mem = nouveau_mem(old_reg); 1009 1.1 riastrad int ret = RING_SPACE(chan, 7); 1010 1.1 riastrad if (ret == 0) { 1011 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0304, 6); 1012 1.16 riastrad OUT_RING (chan, new_reg->num_pages << PAGE_SHIFT); 1013 1.16 riastrad OUT_RING (chan, upper_32_bits(mem->vma[0].addr)); 1014 1.16 riastrad OUT_RING (chan, lower_32_bits(mem->vma[0].addr)); 1015 1.16 riastrad OUT_RING (chan, upper_32_bits(mem->vma[1].addr)); 1016 1.16 riastrad OUT_RING (chan, lower_32_bits(mem->vma[1].addr)); 1017 1.1 riastrad OUT_RING (chan, 0x00000000 /* MODE_COPY, QUERY_NONE */); 1018 1.1 riastrad } 1019 1.1 riastrad return ret; 1020 1.1 riastrad } 1021 1.1 riastrad 1022 1.1 riastrad static int 1023 1.1 riastrad nv50_bo_move_init(struct nouveau_channel *chan, u32 handle) 1024 1.1 riastrad { 1025 1.1 riastrad int ret = RING_SPACE(chan, 6); 1026 1.1 riastrad if (ret == 0) { 1027 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0000, 1); 1028 1.1 riastrad OUT_RING (chan, handle); 1029 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0180, 3); 1030 1.8 riastrad OUT_RING (chan, chan->drm->ntfy.handle); 1031 1.8 riastrad OUT_RING (chan, chan->vram.handle); 1032 1.8 riastrad OUT_RING (chan, chan->vram.handle); 1033 1.1 riastrad } 1034 1.1 riastrad 1035 1.1 riastrad return ret; 1036 1.1 riastrad } 1037 1.1 riastrad 1038 1.1 riastrad static int 1039 1.1 riastrad nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo, 1040 1.16 riastrad struct ttm_mem_reg *old_reg, struct ttm_mem_reg *new_reg) 1041 1.1 riastrad { 1042 1.16 riastrad struct nouveau_mem *mem = nouveau_mem(old_reg); 1043 1.16 riastrad u64 length = (new_reg->num_pages << PAGE_SHIFT); 1044 1.16 riastrad u64 src_offset = mem->vma[0].addr; 1045 1.16 riastrad u64 dst_offset = mem->vma[1].addr; 1046 1.16 riastrad int src_tiled = !!mem->kind; 1047 1.16 riastrad int dst_tiled = !!nouveau_mem(new_reg)->kind; 1048 1.1 riastrad int ret; 1049 1.1 riastrad 1050 1.1 riastrad while (length) { 1051 1.1 riastrad u32 amount, stride, height; 1052 1.1 riastrad 1053 1.1 riastrad ret = RING_SPACE(chan, 18 + 6 * (src_tiled + dst_tiled)); 1054 1.1 riastrad if (ret) 1055 1.1 riastrad return ret; 1056 1.1 riastrad 1057 1.1 riastrad amount = min(length, (u64)(4 * 1024 * 1024)); 1058 1.1 riastrad stride = 16 * 4; 1059 1.1 riastrad height = amount / stride; 1060 1.1 riastrad 1061 1.1 riastrad if (src_tiled) { 1062 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0200, 7); 1063 1.1 riastrad OUT_RING (chan, 0); 1064 1.1 riastrad OUT_RING (chan, 0); 1065 1.1 riastrad OUT_RING (chan, stride); 1066 1.1 riastrad OUT_RING (chan, height); 1067 1.1 riastrad OUT_RING (chan, 1); 1068 1.1 riastrad OUT_RING (chan, 0); 1069 1.1 riastrad OUT_RING (chan, 0); 1070 1.1 riastrad } else { 1071 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0200, 1); 1072 1.1 riastrad OUT_RING (chan, 1); 1073 1.1 riastrad } 1074 1.1 riastrad if (dst_tiled) { 1075 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x021c, 7); 1076 1.1 riastrad OUT_RING (chan, 0); 1077 1.1 riastrad OUT_RING (chan, 0); 1078 1.1 riastrad OUT_RING (chan, stride); 1079 1.1 riastrad OUT_RING (chan, height); 1080 1.1 riastrad OUT_RING (chan, 1); 1081 1.1 riastrad OUT_RING (chan, 0); 1082 1.1 riastrad OUT_RING (chan, 0); 1083 1.1 riastrad } else { 1084 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x021c, 1); 1085 1.1 riastrad OUT_RING (chan, 1); 1086 1.1 riastrad } 1087 1.1 riastrad 1088 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0238, 2); 1089 1.1 riastrad OUT_RING (chan, upper_32_bits(src_offset)); 1090 1.1 riastrad OUT_RING (chan, upper_32_bits(dst_offset)); 1091 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x030c, 8); 1092 1.1 riastrad OUT_RING (chan, lower_32_bits(src_offset)); 1093 1.1 riastrad OUT_RING (chan, lower_32_bits(dst_offset)); 1094 1.1 riastrad OUT_RING (chan, stride); 1095 1.1 riastrad OUT_RING (chan, stride); 1096 1.1 riastrad OUT_RING (chan, stride); 1097 1.1 riastrad OUT_RING (chan, height); 1098 1.1 riastrad OUT_RING (chan, 0x00000101); 1099 1.1 riastrad OUT_RING (chan, 0x00000000); 1100 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1); 1101 1.1 riastrad OUT_RING (chan, 0); 1102 1.1 riastrad 1103 1.1 riastrad length -= amount; 1104 1.1 riastrad src_offset += amount; 1105 1.1 riastrad dst_offset += amount; 1106 1.1 riastrad } 1107 1.1 riastrad 1108 1.1 riastrad return 0; 1109 1.1 riastrad } 1110 1.1 riastrad 1111 1.1 riastrad static int 1112 1.1 riastrad nv04_bo_move_init(struct nouveau_channel *chan, u32 handle) 1113 1.1 riastrad { 1114 1.1 riastrad int ret = RING_SPACE(chan, 4); 1115 1.1 riastrad if (ret == 0) { 1116 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0000, 1); 1117 1.1 riastrad OUT_RING (chan, handle); 1118 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 0x0180, 1); 1119 1.8 riastrad OUT_RING (chan, chan->drm->ntfy.handle); 1120 1.1 riastrad } 1121 1.1 riastrad 1122 1.1 riastrad return ret; 1123 1.1 riastrad } 1124 1.1 riastrad 1125 1.1 riastrad static inline uint32_t 1126 1.1 riastrad nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo, 1127 1.16 riastrad struct nouveau_channel *chan, struct ttm_mem_reg *reg) 1128 1.1 riastrad { 1129 1.16 riastrad if (reg->mem_type == TTM_PL_TT) 1130 1.1 riastrad return NvDmaTT; 1131 1.8 riastrad return chan->vram.handle; 1132 1.1 riastrad } 1133 1.1 riastrad 1134 1.1 riastrad static int 1135 1.1 riastrad nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo, 1136 1.16 riastrad struct ttm_mem_reg *old_reg, struct ttm_mem_reg *new_reg) 1137 1.1 riastrad { 1138 1.16 riastrad u32 src_offset = old_reg->start << PAGE_SHIFT; 1139 1.16 riastrad u32 dst_offset = new_reg->start << PAGE_SHIFT; 1140 1.16 riastrad u32 page_count = new_reg->num_pages; 1141 1.1 riastrad int ret; 1142 1.1 riastrad 1143 1.1 riastrad ret = RING_SPACE(chan, 3); 1144 1.1 riastrad if (ret) 1145 1.1 riastrad return ret; 1146 1.1 riastrad 1147 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2); 1148 1.16 riastrad OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_reg)); 1149 1.16 riastrad OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_reg)); 1150 1.1 riastrad 1151 1.16 riastrad page_count = new_reg->num_pages; 1152 1.1 riastrad while (page_count) { 1153 1.1 riastrad int line_count = (page_count > 2047) ? 2047 : page_count; 1154 1.1 riastrad 1155 1.1 riastrad ret = RING_SPACE(chan, 11); 1156 1.1 riastrad if (ret) 1157 1.1 riastrad return ret; 1158 1.1 riastrad 1159 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, 1160 1.1 riastrad NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8); 1161 1.1 riastrad OUT_RING (chan, src_offset); 1162 1.1 riastrad OUT_RING (chan, dst_offset); 1163 1.1 riastrad OUT_RING (chan, PAGE_SIZE); /* src_pitch */ 1164 1.1 riastrad OUT_RING (chan, PAGE_SIZE); /* dst_pitch */ 1165 1.1 riastrad OUT_RING (chan, PAGE_SIZE); /* line_length */ 1166 1.1 riastrad OUT_RING (chan, line_count); 1167 1.1 riastrad OUT_RING (chan, 0x00000101); 1168 1.1 riastrad OUT_RING (chan, 0x00000000); 1169 1.1 riastrad BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1); 1170 1.1 riastrad OUT_RING (chan, 0); 1171 1.1 riastrad 1172 1.1 riastrad page_count -= line_count; 1173 1.1 riastrad src_offset += (PAGE_SIZE * line_count); 1174 1.1 riastrad dst_offset += (PAGE_SIZE * line_count); 1175 1.1 riastrad } 1176 1.1 riastrad 1177 1.1 riastrad return 0; 1178 1.1 riastrad } 1179 1.1 riastrad 1180 1.1 riastrad static int 1181 1.1 riastrad nouveau_bo_move_prep(struct nouveau_drm *drm, struct ttm_buffer_object *bo, 1182 1.16 riastrad struct ttm_mem_reg *reg) 1183 1.1 riastrad { 1184 1.16 riastrad struct nouveau_mem *old_mem = nouveau_mem(&bo->mem); 1185 1.16 riastrad struct nouveau_mem *new_mem = nouveau_mem(reg); 1186 1.16 riastrad struct nvif_vmm *vmm = &drm->client.vmm.vmm; 1187 1.1 riastrad int ret; 1188 1.1 riastrad 1189 1.16 riastrad ret = nvif_vmm_get(vmm, LAZY, false, old_mem->mem.page, 0, 1190 1.16 riastrad old_mem->mem.size, &old_mem->vma[0]); 1191 1.1 riastrad if (ret) 1192 1.1 riastrad return ret; 1193 1.1 riastrad 1194 1.16 riastrad ret = nvif_vmm_get(vmm, LAZY, false, new_mem->mem.page, 0, 1195 1.16 riastrad new_mem->mem.size, &old_mem->vma[1]); 1196 1.16 riastrad if (ret) 1197 1.16 riastrad goto done; 1198 1.16 riastrad 1199 1.16 riastrad ret = nouveau_mem_map(old_mem, vmm, &old_mem->vma[0]); 1200 1.16 riastrad if (ret) 1201 1.16 riastrad goto done; 1202 1.16 riastrad 1203 1.16 riastrad ret = nouveau_mem_map(new_mem, vmm, &old_mem->vma[1]); 1204 1.16 riastrad done: 1205 1.1 riastrad if (ret) { 1206 1.16 riastrad nvif_vmm_put(vmm, &old_mem->vma[1]); 1207 1.16 riastrad nvif_vmm_put(vmm, &old_mem->vma[0]); 1208 1.1 riastrad } 1209 1.1 riastrad return 0; 1210 1.1 riastrad } 1211 1.1 riastrad 1212 1.1 riastrad static int 1213 1.1 riastrad nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr, 1214 1.16 riastrad bool no_wait_gpu, struct ttm_mem_reg *new_reg) 1215 1.1 riastrad { 1216 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 1217 1.1 riastrad struct nouveau_channel *chan = drm->ttm.chan; 1218 1.8 riastrad struct nouveau_cli *cli = (void *)chan->user.client; 1219 1.1 riastrad struct nouveau_fence *fence; 1220 1.1 riastrad int ret; 1221 1.1 riastrad 1222 1.1 riastrad /* create temporary vmas for the transfer and attach them to the 1223 1.8 riastrad * old nvkm_mem node, these will get cleaned up after ttm has 1224 1.1 riastrad * destroyed the ttm_mem_reg 1225 1.1 riastrad */ 1226 1.16 riastrad if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA) { 1227 1.16 riastrad ret = nouveau_bo_move_prep(drm, bo, new_reg); 1228 1.1 riastrad if (ret) 1229 1.1 riastrad return ret; 1230 1.1 riastrad } 1231 1.1 riastrad 1232 1.8 riastrad mutex_lock_nested(&cli->mutex, SINGLE_DEPTH_NESTING); 1233 1.8 riastrad ret = nouveau_fence_sync(nouveau_bo(bo), chan, true, intr); 1234 1.1 riastrad if (ret == 0) { 1235 1.16 riastrad ret = drm->ttm.move(chan, bo, &bo->mem, new_reg); 1236 1.1 riastrad if (ret == 0) { 1237 1.1 riastrad ret = nouveau_fence_new(chan, false, &fence); 1238 1.1 riastrad if (ret == 0) { 1239 1.8 riastrad ret = ttm_bo_move_accel_cleanup(bo, 1240 1.8 riastrad &fence->base, 1241 1.1 riastrad evict, 1242 1.16 riastrad new_reg); 1243 1.1 riastrad nouveau_fence_unref(&fence); 1244 1.1 riastrad } 1245 1.1 riastrad } 1246 1.1 riastrad } 1247 1.8 riastrad mutex_unlock(&cli->mutex); 1248 1.1 riastrad return ret; 1249 1.1 riastrad } 1250 1.1 riastrad 1251 1.1 riastrad void 1252 1.1 riastrad nouveau_bo_move_init(struct nouveau_drm *drm) 1253 1.1 riastrad { 1254 1.16 riastrad static const struct _method_table { 1255 1.1 riastrad const char *name; 1256 1.1 riastrad int engine; 1257 1.8 riastrad s32 oclass; 1258 1.1 riastrad int (*exec)(struct nouveau_channel *, 1259 1.1 riastrad struct ttm_buffer_object *, 1260 1.1 riastrad struct ttm_mem_reg *, struct ttm_mem_reg *); 1261 1.1 riastrad int (*init)(struct nouveau_channel *, u32 handle); 1262 1.1 riastrad } _methods[] = { 1263 1.16 riastrad { "COPY", 4, 0xc5b5, nve0_bo_move_copy, nve0_bo_move_init }, 1264 1.16 riastrad { "GRCE", 0, 0xc5b5, nve0_bo_move_copy, nvc0_bo_move_init }, 1265 1.16 riastrad { "COPY", 4, 0xc3b5, nve0_bo_move_copy, nve0_bo_move_init }, 1266 1.16 riastrad { "GRCE", 0, 0xc3b5, nve0_bo_move_copy, nvc0_bo_move_init }, 1267 1.16 riastrad { "COPY", 4, 0xc1b5, nve0_bo_move_copy, nve0_bo_move_init }, 1268 1.16 riastrad { "GRCE", 0, 0xc1b5, nve0_bo_move_copy, nvc0_bo_move_init }, 1269 1.16 riastrad { "COPY", 4, 0xc0b5, nve0_bo_move_copy, nve0_bo_move_init }, 1270 1.16 riastrad { "GRCE", 0, 0xc0b5, nve0_bo_move_copy, nvc0_bo_move_init }, 1271 1.8 riastrad { "COPY", 4, 0xb0b5, nve0_bo_move_copy, nve0_bo_move_init }, 1272 1.8 riastrad { "GRCE", 0, 0xb0b5, nve0_bo_move_copy, nvc0_bo_move_init }, 1273 1.1 riastrad { "COPY", 4, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init }, 1274 1.1 riastrad { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init }, 1275 1.1 riastrad { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init }, 1276 1.1 riastrad { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init }, 1277 1.1 riastrad { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init }, 1278 1.1 riastrad { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init }, 1279 1.1 riastrad { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init }, 1280 1.1 riastrad { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init }, 1281 1.1 riastrad { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init }, 1282 1.1 riastrad {}, 1283 1.1 riastrad { "CRYPT", 0, 0x88b4, nv98_bo_move_exec, nv50_bo_move_init }, 1284 1.16 riastrad }; 1285 1.16 riastrad const struct _method_table *mthd = _methods; 1286 1.1 riastrad const char *name = "CPU"; 1287 1.1 riastrad int ret; 1288 1.1 riastrad 1289 1.1 riastrad do { 1290 1.1 riastrad struct nouveau_channel *chan; 1291 1.1 riastrad 1292 1.1 riastrad if (mthd->engine) 1293 1.1 riastrad chan = drm->cechan; 1294 1.1 riastrad else 1295 1.1 riastrad chan = drm->channel; 1296 1.1 riastrad if (chan == NULL) 1297 1.1 riastrad continue; 1298 1.1 riastrad 1299 1.8 riastrad ret = nvif_object_init(&chan->user, 1300 1.8 riastrad mthd->oclass | (mthd->engine << 16), 1301 1.8 riastrad mthd->oclass, NULL, 0, 1302 1.8 riastrad &drm->ttm.copy); 1303 1.1 riastrad if (ret == 0) { 1304 1.8 riastrad ret = mthd->init(chan, drm->ttm.copy.handle); 1305 1.1 riastrad if (ret) { 1306 1.8 riastrad nvif_object_fini(&drm->ttm.copy); 1307 1.1 riastrad continue; 1308 1.1 riastrad } 1309 1.1 riastrad 1310 1.1 riastrad drm->ttm.move = mthd->exec; 1311 1.1 riastrad drm->ttm.chan = chan; 1312 1.1 riastrad name = mthd->name; 1313 1.1 riastrad break; 1314 1.1 riastrad } 1315 1.1 riastrad } while ((++mthd)->exec); 1316 1.1 riastrad 1317 1.1 riastrad NV_INFO(drm, "MM: using %s for buffer copies\n", name); 1318 1.1 riastrad } 1319 1.1 riastrad 1320 1.1 riastrad static int 1321 1.1 riastrad nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr, 1322 1.16 riastrad bool no_wait_gpu, struct ttm_mem_reg *new_reg) 1323 1.1 riastrad { 1324 1.16 riastrad struct ttm_operation_ctx ctx = { intr, no_wait_gpu }; 1325 1.8 riastrad struct ttm_place placement_memtype = { 1326 1.8 riastrad .fpfn = 0, 1327 1.8 riastrad .lpfn = 0, 1328 1.8 riastrad .flags = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING 1329 1.8 riastrad }; 1330 1.1 riastrad struct ttm_placement placement; 1331 1.16 riastrad struct ttm_mem_reg tmp_reg; 1332 1.1 riastrad int ret; 1333 1.1 riastrad 1334 1.1 riastrad placement.num_placement = placement.num_busy_placement = 1; 1335 1.1 riastrad placement.placement = placement.busy_placement = &placement_memtype; 1336 1.1 riastrad 1337 1.16 riastrad tmp_reg = *new_reg; 1338 1.16 riastrad tmp_reg.mm_node = NULL; 1339 1.16 riastrad ret = ttm_bo_mem_space(bo, &placement, &tmp_reg, &ctx); 1340 1.1 riastrad if (ret) 1341 1.1 riastrad return ret; 1342 1.1 riastrad 1343 1.16 riastrad ret = ttm_tt_bind(bo->ttm, &tmp_reg, &ctx); 1344 1.1 riastrad if (ret) 1345 1.1 riastrad goto out; 1346 1.1 riastrad 1347 1.16 riastrad ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, &tmp_reg); 1348 1.1 riastrad if (ret) 1349 1.1 riastrad goto out; 1350 1.1 riastrad 1351 1.16 riastrad ret = ttm_bo_move_ttm(bo, &ctx, new_reg); 1352 1.1 riastrad out: 1353 1.16 riastrad ttm_bo_mem_put(bo, &tmp_reg); 1354 1.1 riastrad return ret; 1355 1.1 riastrad } 1356 1.1 riastrad 1357 1.1 riastrad static int 1358 1.1 riastrad nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr, 1359 1.16 riastrad bool no_wait_gpu, struct ttm_mem_reg *new_reg) 1360 1.1 riastrad { 1361 1.16 riastrad struct ttm_operation_ctx ctx = { intr, no_wait_gpu }; 1362 1.8 riastrad struct ttm_place placement_memtype = { 1363 1.8 riastrad .fpfn = 0, 1364 1.8 riastrad .lpfn = 0, 1365 1.8 riastrad .flags = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING 1366 1.8 riastrad }; 1367 1.1 riastrad struct ttm_placement placement; 1368 1.16 riastrad struct ttm_mem_reg tmp_reg; 1369 1.1 riastrad int ret; 1370 1.1 riastrad 1371 1.1 riastrad placement.num_placement = placement.num_busy_placement = 1; 1372 1.1 riastrad placement.placement = placement.busy_placement = &placement_memtype; 1373 1.1 riastrad 1374 1.16 riastrad tmp_reg = *new_reg; 1375 1.16 riastrad tmp_reg.mm_node = NULL; 1376 1.16 riastrad ret = ttm_bo_mem_space(bo, &placement, &tmp_reg, &ctx); 1377 1.1 riastrad if (ret) 1378 1.1 riastrad return ret; 1379 1.1 riastrad 1380 1.16 riastrad ret = ttm_bo_move_ttm(bo, &ctx, &tmp_reg); 1381 1.1 riastrad if (ret) 1382 1.1 riastrad goto out; 1383 1.1 riastrad 1384 1.16 riastrad ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_gpu, new_reg); 1385 1.1 riastrad if (ret) 1386 1.1 riastrad goto out; 1387 1.1 riastrad 1388 1.1 riastrad out: 1389 1.16 riastrad ttm_bo_mem_put(bo, &tmp_reg); 1390 1.1 riastrad return ret; 1391 1.1 riastrad } 1392 1.1 riastrad 1393 1.1 riastrad static void 1394 1.16 riastrad nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, bool evict, 1395 1.16 riastrad struct ttm_mem_reg *new_reg) 1396 1.1 riastrad { 1397 1.16 riastrad struct nouveau_mem *mem = new_reg ? nouveau_mem(new_reg) : NULL; 1398 1.1 riastrad struct nouveau_bo *nvbo = nouveau_bo(bo); 1399 1.16 riastrad struct nouveau_vma *vma; 1400 1.1 riastrad 1401 1.1 riastrad /* ttm can now (stupidly) pass the driver bos it didn't create... */ 1402 1.1 riastrad if (bo->destroy != nouveau_bo_del_ttm) 1403 1.1 riastrad return; 1404 1.1 riastrad 1405 1.16 riastrad if (mem && new_reg->mem_type != TTM_PL_SYSTEM && 1406 1.16 riastrad mem->mem.page == nvbo->page) { 1407 1.16 riastrad list_for_each_entry(vma, &nvbo->vma_list, head) { 1408 1.16 riastrad nouveau_vma_map(vma, mem); 1409 1.16 riastrad } 1410 1.16 riastrad } else { 1411 1.16 riastrad list_for_each_entry(vma, &nvbo->vma_list, head) { 1412 1.16 riastrad WARN_ON(ttm_bo_wait(bo, false, false)); 1413 1.16 riastrad nouveau_vma_unmap(vma); 1414 1.1 riastrad } 1415 1.1 riastrad } 1416 1.1 riastrad } 1417 1.1 riastrad 1418 1.1 riastrad static int 1419 1.16 riastrad nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_reg, 1420 1.1 riastrad struct nouveau_drm_tile **new_tile) 1421 1.1 riastrad { 1422 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 1423 1.1 riastrad struct drm_device *dev = drm->dev; 1424 1.1 riastrad struct nouveau_bo *nvbo = nouveau_bo(bo); 1425 1.16 riastrad u64 offset = new_reg->start << PAGE_SHIFT; 1426 1.1 riastrad 1427 1.1 riastrad *new_tile = NULL; 1428 1.16 riastrad if (new_reg->mem_type != TTM_PL_VRAM) 1429 1.1 riastrad return 0; 1430 1.1 riastrad 1431 1.16 riastrad if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) { 1432 1.16 riastrad *new_tile = nv10_bo_set_tiling(dev, offset, new_reg->size, 1433 1.16 riastrad nvbo->mode, nvbo->zeta); 1434 1.1 riastrad } 1435 1.1 riastrad 1436 1.1 riastrad return 0; 1437 1.1 riastrad } 1438 1.1 riastrad 1439 1.1 riastrad static void 1440 1.1 riastrad nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo, 1441 1.1 riastrad struct nouveau_drm_tile *new_tile, 1442 1.1 riastrad struct nouveau_drm_tile **old_tile) 1443 1.1 riastrad { 1444 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 1445 1.1 riastrad struct drm_device *dev = drm->dev; 1446 1.16 riastrad struct dma_fence *fence = dma_resv_get_excl(bo->base.resv); 1447 1.1 riastrad 1448 1.8 riastrad nv10_bo_put_tile_region(dev, *old_tile, fence); 1449 1.1 riastrad *old_tile = new_tile; 1450 1.1 riastrad } 1451 1.1 riastrad 1452 1.1 riastrad static int 1453 1.16 riastrad nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, 1454 1.16 riastrad struct ttm_operation_ctx *ctx, 1455 1.16 riastrad struct ttm_mem_reg *new_reg) 1456 1.1 riastrad { 1457 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 1458 1.1 riastrad struct nouveau_bo *nvbo = nouveau_bo(bo); 1459 1.16 riastrad struct ttm_mem_reg *old_reg = &bo->mem; 1460 1.1 riastrad struct nouveau_drm_tile *new_tile = NULL; 1461 1.1 riastrad int ret = 0; 1462 1.1 riastrad 1463 1.16 riastrad ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu); 1464 1.16 riastrad if (ret) 1465 1.16 riastrad return ret; 1466 1.16 riastrad 1467 1.8 riastrad if (nvbo->pin_refcnt) 1468 1.8 riastrad NV_WARN(drm, "Moving pinned object %p!\n", nvbo); 1469 1.8 riastrad 1470 1.16 riastrad if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) { 1471 1.16 riastrad ret = nouveau_bo_vm_bind(bo, new_reg, &new_tile); 1472 1.1 riastrad if (ret) 1473 1.1 riastrad return ret; 1474 1.1 riastrad } 1475 1.1 riastrad 1476 1.1 riastrad /* Fake bo copy. */ 1477 1.16 riastrad if (old_reg->mem_type == TTM_PL_SYSTEM && !bo->ttm) { 1478 1.1 riastrad BUG_ON(bo->mem.mm_node != NULL); 1479 1.16 riastrad bo->mem = *new_reg; 1480 1.16 riastrad new_reg->mm_node = NULL; 1481 1.1 riastrad goto out; 1482 1.1 riastrad } 1483 1.1 riastrad 1484 1.1 riastrad /* Hardware assisted copy. */ 1485 1.1 riastrad if (drm->ttm.move) { 1486 1.16 riastrad if (new_reg->mem_type == TTM_PL_SYSTEM) 1487 1.16 riastrad ret = nouveau_bo_move_flipd(bo, evict, 1488 1.16 riastrad ctx->interruptible, 1489 1.16 riastrad ctx->no_wait_gpu, new_reg); 1490 1.16 riastrad else if (old_reg->mem_type == TTM_PL_SYSTEM) 1491 1.16 riastrad ret = nouveau_bo_move_flips(bo, evict, 1492 1.16 riastrad ctx->interruptible, 1493 1.16 riastrad ctx->no_wait_gpu, new_reg); 1494 1.1 riastrad else 1495 1.16 riastrad ret = nouveau_bo_move_m2mf(bo, evict, 1496 1.16 riastrad ctx->interruptible, 1497 1.16 riastrad ctx->no_wait_gpu, new_reg); 1498 1.1 riastrad if (!ret) 1499 1.1 riastrad goto out; 1500 1.1 riastrad } 1501 1.1 riastrad 1502 1.1 riastrad /* Fallback to software copy. */ 1503 1.16 riastrad ret = ttm_bo_wait(bo, ctx->interruptible, ctx->no_wait_gpu); 1504 1.1 riastrad if (ret == 0) 1505 1.16 riastrad ret = ttm_bo_move_memcpy(bo, ctx, new_reg); 1506 1.1 riastrad 1507 1.1 riastrad out: 1508 1.16 riastrad if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) { 1509 1.1 riastrad if (ret) 1510 1.1 riastrad nouveau_bo_vm_cleanup(bo, NULL, &new_tile); 1511 1.1 riastrad else 1512 1.1 riastrad nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile); 1513 1.1 riastrad } 1514 1.1 riastrad 1515 1.1 riastrad return ret; 1516 1.1 riastrad } 1517 1.1 riastrad 1518 1.1 riastrad static int 1519 1.1 riastrad nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp) 1520 1.1 riastrad { 1521 1.18 riastrad #ifdef __NetBSD__ 1522 1.18 riastrad struct drm_file *file = filp->f_data; 1523 1.18 riastrad #else 1524 1.18 riastrad struct drm_file *file = filp->private_data; 1525 1.18 riastrad #endif 1526 1.1 riastrad struct nouveau_bo *nvbo = nouveau_bo(bo); 1527 1.1 riastrad 1528 1.18 riastrad return drm_vma_node_verify_access(&nvbo->bo.base.vma_node, file); 1529 1.1 riastrad } 1530 1.1 riastrad 1531 1.1 riastrad static int 1532 1.16 riastrad nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *reg) 1533 1.1 riastrad { 1534 1.16 riastrad struct ttm_mem_type_manager *man = &bdev->man[reg->mem_type]; 1535 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(bdev); 1536 1.16 riastrad struct nvkm_device *device = nvxx_device(&drm->client.device); 1537 1.16 riastrad struct nouveau_mem *mem = nouveau_mem(reg); 1538 1.1 riastrad 1539 1.16 riastrad reg->bus.addr = NULL; 1540 1.16 riastrad reg->bus.offset = 0; 1541 1.16 riastrad reg->bus.size = reg->num_pages << PAGE_SHIFT; 1542 1.16 riastrad reg->bus.base = 0; 1543 1.16 riastrad reg->bus.is_iomem = false; 1544 1.1 riastrad if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) 1545 1.1 riastrad return -EINVAL; 1546 1.16 riastrad switch (reg->mem_type) { 1547 1.1 riastrad case TTM_PL_SYSTEM: 1548 1.1 riastrad /* System memory */ 1549 1.1 riastrad return 0; 1550 1.1 riastrad case TTM_PL_TT: 1551 1.8 riastrad #if IS_ENABLED(CONFIG_AGP) 1552 1.8 riastrad if (drm->agp.bridge) { 1553 1.16 riastrad reg->bus.offset = reg->start << PAGE_SHIFT; 1554 1.16 riastrad reg->bus.base = drm->agp.base; 1555 1.16 riastrad reg->bus.is_iomem = !drm->agp.cma; 1556 1.1 riastrad } 1557 1.1 riastrad #endif 1558 1.16 riastrad if (drm->client.mem->oclass < NVIF_CLASS_MEM_NV50 || !mem->kind) 1559 1.1 riastrad /* untiled */ 1560 1.1 riastrad break; 1561 1.16 riastrad /* fall through - tiled memory */ 1562 1.1 riastrad case TTM_PL_VRAM: 1563 1.16 riastrad reg->bus.offset = reg->start << PAGE_SHIFT; 1564 1.16 riastrad reg->bus.base = device->func->resource_addr(device, 1); 1565 1.16 riastrad reg->bus.is_iomem = true; 1566 1.16 riastrad if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) { 1567 1.16 riastrad union { 1568 1.16 riastrad struct nv50_mem_map_v0 nv50; 1569 1.16 riastrad struct gf100_mem_map_v0 gf100; 1570 1.16 riastrad } args; 1571 1.16 riastrad u64 handle, length; 1572 1.16 riastrad u32 argc = 0; 1573 1.16 riastrad int ret; 1574 1.16 riastrad 1575 1.16 riastrad switch (mem->mem.object.oclass) { 1576 1.16 riastrad case NVIF_CLASS_MEM_NV50: 1577 1.16 riastrad args.nv50.version = 0; 1578 1.16 riastrad args.nv50.ro = 0; 1579 1.16 riastrad args.nv50.kind = mem->kind; 1580 1.16 riastrad args.nv50.comp = mem->comp; 1581 1.16 riastrad argc = sizeof(args.nv50); 1582 1.16 riastrad break; 1583 1.16 riastrad case NVIF_CLASS_MEM_GF100: 1584 1.16 riastrad args.gf100.version = 0; 1585 1.16 riastrad args.gf100.ro = 0; 1586 1.16 riastrad args.gf100.kind = mem->kind; 1587 1.16 riastrad argc = sizeof(args.gf100); 1588 1.16 riastrad break; 1589 1.16 riastrad default: 1590 1.16 riastrad WARN_ON(1); 1591 1.16 riastrad break; 1592 1.16 riastrad } 1593 1.1 riastrad 1594 1.16 riastrad ret = nvif_object_map_handle(&mem->mem.object, 1595 1.16 riastrad &args, argc, 1596 1.19 riastrad #ifdef __NetBSD__ 1597 1.19 riastrad NULL, 1598 1.19 riastrad #endif 1599 1.16 riastrad &handle, &length); 1600 1.16 riastrad if (ret != 1) 1601 1.16 riastrad return ret ? ret : -EINVAL; 1602 1.1 riastrad 1603 1.16 riastrad reg->bus.base = 0; 1604 1.16 riastrad reg->bus.offset = handle; 1605 1.1 riastrad } 1606 1.1 riastrad break; 1607 1.1 riastrad default: 1608 1.1 riastrad return -EINVAL; 1609 1.1 riastrad } 1610 1.1 riastrad return 0; 1611 1.1 riastrad } 1612 1.1 riastrad 1613 1.1 riastrad static void 1614 1.16 riastrad nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *reg) 1615 1.1 riastrad { 1616 1.16 riastrad struct nouveau_drm *drm = nouveau_bdev(bdev); 1617 1.16 riastrad struct nouveau_mem *mem = nouveau_mem(reg); 1618 1.1 riastrad 1619 1.16 riastrad if (drm->client.mem->oclass >= NVIF_CLASS_MEM_NV50) { 1620 1.16 riastrad switch (reg->mem_type) { 1621 1.16 riastrad case TTM_PL_TT: 1622 1.16 riastrad if (mem->kind) 1623 1.16 riastrad nvif_object_unmap_handle(&mem->mem.object); 1624 1.16 riastrad break; 1625 1.16 riastrad case TTM_PL_VRAM: 1626 1.16 riastrad nvif_object_unmap_handle(&mem->mem.object); 1627 1.16 riastrad break; 1628 1.16 riastrad default: 1629 1.16 riastrad break; 1630 1.16 riastrad } 1631 1.16 riastrad } 1632 1.1 riastrad } 1633 1.1 riastrad 1634 1.1 riastrad static int 1635 1.1 riastrad nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo) 1636 1.1 riastrad { 1637 1.1 riastrad struct nouveau_drm *drm = nouveau_bdev(bo->bdev); 1638 1.1 riastrad struct nouveau_bo *nvbo = nouveau_bo(bo); 1639 1.16 riastrad struct nvkm_device *device = nvxx_device(&drm->client.device); 1640 1.8 riastrad u32 mappable = device->func->resource_size(device, 1) >> PAGE_SHIFT; 1641 1.8 riastrad int i, ret; 1642 1.1 riastrad 1643 1.1 riastrad /* as long as the bo isn't in vram, and isn't tiled, we've got 1644 1.1 riastrad * nothing to do here. 1645 1.1 riastrad */ 1646 1.1 riastrad if (bo->mem.mem_type != TTM_PL_VRAM) { 1647 1.16 riastrad if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA || 1648 1.16 riastrad !nvbo->kind) 1649 1.1 riastrad return 0; 1650 1.1 riastrad 1651 1.1 riastrad if (bo->mem.mem_type == TTM_PL_SYSTEM) { 1652 1.1 riastrad nouveau_bo_placement_set(nvbo, TTM_PL_TT, 0); 1653 1.1 riastrad 1654 1.1 riastrad ret = nouveau_bo_validate(nvbo, false, false); 1655 1.1 riastrad if (ret) 1656 1.1 riastrad return ret; 1657 1.1 riastrad } 1658 1.1 riastrad return 0; 1659 1.1 riastrad } 1660 1.1 riastrad 1661 1.1 riastrad /* make sure bo is in mappable vram */ 1662 1.16 riastrad if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA || 1663 1.1 riastrad bo->mem.start + bo->mem.num_pages < mappable) 1664 1.1 riastrad return 0; 1665 1.1 riastrad 1666 1.8 riastrad for (i = 0; i < nvbo->placement.num_placement; ++i) { 1667 1.8 riastrad nvbo->placements[i].fpfn = 0; 1668 1.8 riastrad nvbo->placements[i].lpfn = mappable; 1669 1.8 riastrad } 1670 1.8 riastrad 1671 1.8 riastrad for (i = 0; i < nvbo->placement.num_busy_placement; ++i) { 1672 1.8 riastrad nvbo->busy_placements[i].fpfn = 0; 1673 1.8 riastrad nvbo->busy_placements[i].lpfn = mappable; 1674 1.8 riastrad } 1675 1.1 riastrad 1676 1.1 riastrad nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0); 1677 1.1 riastrad return nouveau_bo_validate(nvbo, false, false); 1678 1.1 riastrad } 1679 1.1 riastrad 1680 1.1 riastrad static int 1681 1.16 riastrad nouveau_ttm_tt_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) 1682 1.1 riastrad { 1683 1.1 riastrad struct ttm_dma_tt *ttm_dma = (void *)ttm; 1684 1.5 riastrad struct nouveau_drm *drm; 1685 1.16 riastrad struct device *dev; 1686 1.1 riastrad unsigned i; 1687 1.1 riastrad int r; 1688 1.1 riastrad bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); 1689 1.1 riastrad 1690 1.1 riastrad if (ttm->state != tt_unpopulated) 1691 1.1 riastrad return 0; 1692 1.1 riastrad 1693 1.1 riastrad if (slave && ttm->sg) { 1694 1.1 riastrad /* make userspace faulting work */ 1695 1.12 riastrad #ifdef __NetBSD__ 1696 1.12 riastrad r = drm_prime_bus_dmamap_load_sgt(ttm->bdev->dmat, 1697 1.12 riastrad ttm_dma->dma_address, ttm->sg); 1698 1.12 riastrad if (r) 1699 1.12 riastrad return r; 1700 1.12 riastrad #else 1701 1.1 riastrad drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages, 1702 1.1 riastrad ttm_dma->dma_address, ttm->num_pages); 1703 1.12 riastrad #endif 1704 1.1 riastrad ttm->state = tt_unbound; 1705 1.1 riastrad return 0; 1706 1.1 riastrad } 1707 1.1 riastrad 1708 1.5 riastrad drm = nouveau_bdev(ttm->bdev); 1709 1.16 riastrad dev = drm->dev->dev; 1710 1.8 riastrad 1711 1.8 riastrad #if IS_ENABLED(CONFIG_AGP) 1712 1.8 riastrad if (drm->agp.bridge) { 1713 1.16 riastrad return ttm_agp_tt_populate(ttm, ctx); 1714 1.1 riastrad } 1715 1.1 riastrad #endif 1716 1.1 riastrad 1717 1.18 riastrad #ifdef __NetBSD__ 1718 1.18 riastrad __USE(i); 1719 1.18 riastrad __USE(dev); 1720 1.18 riastrad return ttm_bus_dma_populate(ttm_dma); 1721 1.18 riastrad #else 1722 1.16 riastrad #if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86) 1723 1.16 riastrad if (swiotlb_nr_tbl()) { 1724 1.16 riastrad return ttm_dma_populate((void *)ttm, dev, ctx); 1725 1.1 riastrad } 1726 1.1 riastrad #endif 1727 1.1 riastrad 1728 1.16 riastrad r = ttm_pool_populate(ttm, ctx); 1729 1.1 riastrad if (r) { 1730 1.1 riastrad return r; 1731 1.1 riastrad } 1732 1.1 riastrad 1733 1.1 riastrad for (i = 0; i < ttm->num_pages; i++) { 1734 1.8 riastrad dma_addr_t addr; 1735 1.8 riastrad 1736 1.16 riastrad addr = dma_map_page(dev, ttm->pages[i], 0, PAGE_SIZE, 1737 1.8 riastrad DMA_BIDIRECTIONAL); 1738 1.8 riastrad 1739 1.16 riastrad if (dma_mapping_error(dev, addr)) { 1740 1.16 riastrad while (i--) { 1741 1.16 riastrad dma_unmap_page(dev, ttm_dma->dma_address[i], 1742 1.8 riastrad PAGE_SIZE, DMA_BIDIRECTIONAL); 1743 1.1 riastrad ttm_dma->dma_address[i] = 0; 1744 1.1 riastrad } 1745 1.1 riastrad ttm_pool_unpopulate(ttm); 1746 1.1 riastrad return -EFAULT; 1747 1.1 riastrad } 1748 1.8 riastrad 1749 1.8 riastrad ttm_dma->dma_address[i] = addr; 1750 1.1 riastrad } 1751 1.1 riastrad return 0; 1752 1.18 riastrad #endif 1753 1.1 riastrad } 1754 1.1 riastrad 1755 1.1 riastrad static void 1756 1.1 riastrad nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm) 1757 1.1 riastrad { 1758 1.1 riastrad struct ttm_dma_tt *ttm_dma = (void *)ttm; 1759 1.5 riastrad struct nouveau_drm *drm; 1760 1.16 riastrad struct device *dev; 1761 1.1 riastrad unsigned i; 1762 1.1 riastrad bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG); 1763 1.1 riastrad 1764 1.1 riastrad if (slave) 1765 1.1 riastrad return; 1766 1.1 riastrad 1767 1.5 riastrad drm = nouveau_bdev(ttm->bdev); 1768 1.16 riastrad dev = drm->dev->dev; 1769 1.8 riastrad 1770 1.8 riastrad #if IS_ENABLED(CONFIG_AGP) 1771 1.8 riastrad if (drm->agp.bridge) { 1772 1.1 riastrad ttm_agp_tt_unpopulate(ttm); 1773 1.1 riastrad return; 1774 1.1 riastrad } 1775 1.1 riastrad #endif 1776 1.1 riastrad 1777 1.18 riastrad #ifdef __NetBSD__ 1778 1.18 riastrad __USE(i); 1779 1.18 riastrad __USE(dev); 1780 1.18 riastrad ttm_bus_dma_unpopulate(ttm_dma); 1781 1.18 riastrad #else 1782 1.16 riastrad #if IS_ENABLED(CONFIG_SWIOTLB) && IS_ENABLED(CONFIG_X86) 1783 1.16 riastrad if (swiotlb_nr_tbl()) { 1784 1.16 riastrad ttm_dma_unpopulate((void *)ttm, dev); 1785 1.16 riastrad #endif 1786 1.1 riastrad return; 1787 1.1 riastrad } 1788 1.1 riastrad 1789 1.1 riastrad for (i = 0; i < ttm->num_pages; i++) { 1790 1.1 riastrad if (ttm_dma->dma_address[i]) { 1791 1.16 riastrad dma_unmap_page(dev, ttm_dma->dma_address[i], PAGE_SIZE, 1792 1.8 riastrad DMA_BIDIRECTIONAL); 1793 1.1 riastrad } 1794 1.1 riastrad } 1795 1.1 riastrad 1796 1.1 riastrad ttm_pool_unpopulate(ttm); 1797 1.18 riastrad #endif 1798 1.1 riastrad } 1799 1.1 riastrad 1800 1.7 riastrad #ifdef __NetBSD__ 1801 1.7 riastrad static void 1802 1.7 riastrad nouveau_ttm_tt_swapout(struct ttm_tt *ttm) 1803 1.7 riastrad { 1804 1.7 riastrad struct ttm_dma_tt *ttm_dma = container_of(ttm, struct ttm_dma_tt, ttm); 1805 1.7 riastrad 1806 1.7 riastrad ttm_bus_dma_swapout(ttm_dma); 1807 1.7 riastrad } 1808 1.7 riastrad #endif 1809 1.7 riastrad 1810 1.1 riastrad void 1811 1.8 riastrad nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence, bool exclusive) 1812 1.1 riastrad { 1813 1.16 riastrad struct dma_resv *resv = nvbo->bo.base.resv; 1814 1.1 riastrad 1815 1.8 riastrad if (exclusive) 1816 1.16 riastrad dma_resv_add_excl_fence(resv, &fence->base); 1817 1.8 riastrad else if (fence) 1818 1.16 riastrad dma_resv_add_shared_fence(resv, &fence->base); 1819 1.1 riastrad } 1820 1.1 riastrad 1821 1.6 mrg #ifdef __NetBSD__ 1822 1.6 mrg static const struct uvm_pagerops nouveau_uvm_ops = { 1823 1.6 mrg .pgo_reference = &ttm_bo_uvm_reference, 1824 1.6 mrg .pgo_detach = &ttm_bo_uvm_detach, 1825 1.6 mrg .pgo_fault = &ttm_bo_uvm_fault, 1826 1.6 mrg }; 1827 1.6 mrg #endif 1828 1.6 mrg 1829 1.1 riastrad struct ttm_bo_driver nouveau_bo_driver = { 1830 1.1 riastrad .ttm_tt_create = &nouveau_ttm_tt_create, 1831 1.1 riastrad .ttm_tt_populate = &nouveau_ttm_tt_populate, 1832 1.1 riastrad .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate, 1833 1.6 mrg #ifdef __NetBSD__ 1834 1.7 riastrad .ttm_tt_swapout = &nouveau_ttm_tt_swapout, 1835 1.6 mrg .ttm_uvm_ops = &nouveau_uvm_ops, 1836 1.6 mrg #endif 1837 1.1 riastrad .invalidate_caches = nouveau_bo_invalidate_caches, 1838 1.1 riastrad .init_mem_type = nouveau_bo_init_mem_type, 1839 1.16 riastrad .eviction_valuable = ttm_bo_eviction_valuable, 1840 1.1 riastrad .evict_flags = nouveau_bo_evict_flags, 1841 1.1 riastrad .move_notify = nouveau_bo_move_ntfy, 1842 1.1 riastrad .move = nouveau_bo_move, 1843 1.1 riastrad .verify_access = nouveau_bo_verify_access, 1844 1.1 riastrad .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify, 1845 1.1 riastrad .io_mem_reserve = &nouveau_ttm_io_mem_reserve, 1846 1.1 riastrad .io_mem_free = &nouveau_ttm_io_mem_free, 1847 1.1 riastrad }; 1848