blob: 3cbf1a8cf55121f0af1ec7ab795b6d85d0909741 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
2 * Copyright 2007 Dave Airlied
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24/*
25 * Authors: Dave Airlied <airlied@linux.ie>
26 * Ben Skeggs <darktama@iinet.net.au>
27 * Jeremy Kolb <jkolb@brandeis.edu>
28 */
29
Ben Skeggsebb945a2012-07-20 08:17:34 +100030#include <core/engine.h>
Ben Skeggs6ee73862009-12-11 19:24:15 +100031
Ben Skeggsebb945a2012-07-20 08:17:34 +100032#include <subdev/fb.h>
33#include <subdev/vm.h>
34#include <subdev/bar.h>
35
36#include "nouveau_drm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100037#include "nouveau_dma.h"
Ben Skeggsd375e7d52012-04-30 13:30:00 +100038#include "nouveau_fence.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100039
Ben Skeggsebb945a2012-07-20 08:17:34 +100040#include "nouveau_bo.h"
41#include "nouveau_ttm.h"
42#include "nouveau_gem.h"
Maarten Maathuisa5106042009-12-26 21:46:36 +010043
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100044/*
45 * NV10-NV40 tiling helpers
46 */
47
48static void
Ben Skeggsebb945a2012-07-20 08:17:34 +100049nv10_bo_update_tile_region(struct drm_device *dev, struct nouveau_drm_tile *reg,
50 u32 addr, u32 size, u32 pitch, u32 flags)
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100051{
Ben Skeggs77145f12012-07-31 16:16:21 +100052 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +100053 int i = reg - drm->tile.reg;
54 struct nouveau_fb *pfb = nouveau_fb(drm->device);
55 struct nouveau_fb_tile *tile = &pfb->tile.region[i];
56 struct nouveau_engine *engine;
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100057
Ben Skeggsebb945a2012-07-20 08:17:34 +100058 nouveau_fence_unref(&reg->fence);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100059
60 if (tile->pitch)
Ben Skeggsebb945a2012-07-20 08:17:34 +100061 pfb->tile.fini(pfb, i, tile);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100062
63 if (pitch)
Ben Skeggsebb945a2012-07-20 08:17:34 +100064 pfb->tile.init(pfb, i, addr, size, pitch, flags, tile);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100065
Ben Skeggsebb945a2012-07-20 08:17:34 +100066 pfb->tile.prog(pfb, i, tile);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100067
Ben Skeggsebb945a2012-07-20 08:17:34 +100068 if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_GR)))
69 engine->tile_prog(engine, i);
70 if ((engine = nouveau_engine(pfb, NVDEV_ENGINE_MPEG)))
71 engine->tile_prog(engine, i);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100072}
73
Ben Skeggsebb945a2012-07-20 08:17:34 +100074static struct nouveau_drm_tile *
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100075nv10_bo_get_tile_region(struct drm_device *dev, int i)
76{
Ben Skeggs77145f12012-07-31 16:16:21 +100077 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +100078 struct nouveau_drm_tile *tile = &drm->tile.reg[i];
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100079
Ben Skeggsebb945a2012-07-20 08:17:34 +100080 spin_lock(&drm->tile.lock);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100081
82 if (!tile->used &&
83 (!tile->fence || nouveau_fence_done(tile->fence)))
84 tile->used = true;
85 else
86 tile = NULL;
87
Ben Skeggsebb945a2012-07-20 08:17:34 +100088 spin_unlock(&drm->tile.lock);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100089 return tile;
90}
91
92static void
Ben Skeggsebb945a2012-07-20 08:17:34 +100093nv10_bo_put_tile_region(struct drm_device *dev, struct nouveau_drm_tile *tile,
94 struct nouveau_fence *fence)
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100095{
Ben Skeggs77145f12012-07-31 16:16:21 +100096 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +100097
98 if (tile) {
Ben Skeggsebb945a2012-07-20 08:17:34 +100099 spin_lock(&drm->tile.lock);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +1000100 if (fence) {
101 /* Mark it as pending. */
102 tile->fence = fence;
103 nouveau_fence_ref(fence);
104 }
105
106 tile->used = false;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000107 spin_unlock(&drm->tile.lock);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +1000108 }
109}
110
Ben Skeggsebb945a2012-07-20 08:17:34 +1000111static struct nouveau_drm_tile *
112nv10_bo_set_tiling(struct drm_device *dev, u32 addr,
113 u32 size, u32 pitch, u32 flags)
Ben Skeggsbc9e7b92012-07-19 17:54:21 +1000114{
Ben Skeggs77145f12012-07-31 16:16:21 +1000115 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000116 struct nouveau_fb *pfb = nouveau_fb(drm->device);
117 struct nouveau_drm_tile *tile, *found = NULL;
Ben Skeggsbc9e7b92012-07-19 17:54:21 +1000118 int i;
119
Ben Skeggsebb945a2012-07-20 08:17:34 +1000120 for (i = 0; i < pfb->tile.regions; i++) {
Ben Skeggsbc9e7b92012-07-19 17:54:21 +1000121 tile = nv10_bo_get_tile_region(dev, i);
122
123 if (pitch && !found) {
124 found = tile;
125 continue;
126
Ben Skeggsebb945a2012-07-20 08:17:34 +1000127 } else if (tile && pfb->tile.region[i].pitch) {
Ben Skeggsbc9e7b92012-07-19 17:54:21 +1000128 /* Kill an unused tile region. */
129 nv10_bo_update_tile_region(dev, tile, 0, 0, 0, 0);
130 }
131
132 nv10_bo_put_tile_region(dev, tile, NULL);
133 }
134
135 if (found)
136 nv10_bo_update_tile_region(dev, found, addr, size,
137 pitch, flags);
138 return found;
139}
140
Ben Skeggs6ee73862009-12-11 19:24:15 +1000141static void
142nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
143{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000144 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
145 struct drm_device *dev = drm->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000146 struct nouveau_bo *nvbo = nouveau_bo(bo);
147
Ben Skeggs6ee73862009-12-11 19:24:15 +1000148 if (unlikely(nvbo->gem))
149 DRM_ERROR("bo %p still attached to GEM object\n", bo);
Ben Skeggsbc9e7b92012-07-19 17:54:21 +1000150 nv10_bo_put_tile_region(dev, nvbo->tile, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000151 kfree(nvbo);
152}
153
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100154static void
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000155nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
Ben Skeggsf91bac52011-06-06 14:15:46 +1000156 int *align, int *size)
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100157{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000158 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
159 struct nouveau_device *device = nv_device(drm->device);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100160
Ben Skeggsebb945a2012-07-20 08:17:34 +1000161 if (device->card_type < NV_50) {
Ben Skeggsbfd83ac2010-11-12 15:12:51 +1000162 if (nvbo->tile_mode) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000163 if (device->chipset >= 0x40) {
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100164 *align = 65536;
Ben Skeggsbfd83ac2010-11-12 15:12:51 +1000165 *size = roundup(*size, 64 * nvbo->tile_mode);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100166
Ben Skeggsebb945a2012-07-20 08:17:34 +1000167 } else if (device->chipset >= 0x30) {
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100168 *align = 32768;
Ben Skeggsbfd83ac2010-11-12 15:12:51 +1000169 *size = roundup(*size, 64 * nvbo->tile_mode);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100170
Ben Skeggsebb945a2012-07-20 08:17:34 +1000171 } else if (device->chipset >= 0x20) {
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100172 *align = 16384;
Ben Skeggsbfd83ac2010-11-12 15:12:51 +1000173 *size = roundup(*size, 64 * nvbo->tile_mode);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100174
Ben Skeggsebb945a2012-07-20 08:17:34 +1000175 } else if (device->chipset >= 0x10) {
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100176 *align = 16384;
Ben Skeggsbfd83ac2010-11-12 15:12:51 +1000177 *size = roundup(*size, 32 * nvbo->tile_mode);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100178 }
179 }
Ben Skeggsbfd83ac2010-11-12 15:12:51 +1000180 } else {
Ben Skeggsf91bac52011-06-06 14:15:46 +1000181 *size = roundup(*size, (1 << nvbo->page_shift));
182 *align = max((1 << nvbo->page_shift), *align);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100183 }
184
Maarten Maathuis1c7059e2009-12-25 18:51:17 +0100185 *size = roundup(*size, PAGE_SIZE);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100186}
187
Ben Skeggs6ee73862009-12-11 19:24:15 +1000188int
Ben Skeggs7375c952011-06-07 14:21:29 +1000189nouveau_bo_new(struct drm_device *dev, int size, int align,
190 uint32_t flags, uint32_t tile_mode, uint32_t tile_flags,
Dave Airlie22b33e82012-04-02 11:53:06 +0100191 struct sg_table *sg,
Ben Skeggs7375c952011-06-07 14:21:29 +1000192 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000193{
Ben Skeggs77145f12012-07-31 16:16:21 +1000194 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000195 struct nouveau_bo *nvbo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500196 size_t acc_size;
Ben Skeggsf91bac52011-06-06 14:15:46 +1000197 int ret;
Dave Airlie22b33e82012-04-02 11:53:06 +0100198 int type = ttm_bo_type_device;
199
200 if (sg)
201 type = ttm_bo_type_sg;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000202
203 nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
204 if (!nvbo)
205 return -ENOMEM;
206 INIT_LIST_HEAD(&nvbo->head);
207 INIT_LIST_HEAD(&nvbo->entry);
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000208 INIT_LIST_HEAD(&nvbo->vma_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000209 nvbo->tile_mode = tile_mode;
210 nvbo->tile_flags = tile_flags;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000211 nvbo->bo.bdev = &drm->ttm.bdev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000212
Ben Skeggsf91bac52011-06-06 14:15:46 +1000213 nvbo->page_shift = 12;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000214 if (drm->client.base.vm) {
Ben Skeggsf91bac52011-06-06 14:15:46 +1000215 if (!(flags & TTM_PL_FLAG_TT) && size > 256 * 1024)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000216 nvbo->page_shift = drm->client.base.vm->vmm->lpg_shift;
Ben Skeggsf91bac52011-06-06 14:15:46 +1000217 }
218
219 nouveau_bo_fixup_align(nvbo, flags, &align, &size);
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000220 nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
221 nouveau_bo_placement_set(nvbo, flags, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000222
Ben Skeggsebb945a2012-07-20 08:17:34 +1000223 acc_size = ttm_bo_dma_acc_size(&drm->ttm.bdev, size,
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500224 sizeof(struct nouveau_bo));
225
Ben Skeggsebb945a2012-07-20 08:17:34 +1000226 ret = ttm_bo_init(&drm->ttm.bdev, &nvbo->bo, size,
Dave Airlie22b33e82012-04-02 11:53:06 +0100227 type, &nvbo->placement,
Marcin Slusarz0b91c4a2012-11-06 21:49:51 +0000228 align >> PAGE_SHIFT, false, NULL, acc_size, sg,
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000229 nouveau_bo_del_ttm);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000230 if (ret) {
231 /* ttm will call nouveau_bo_del_ttm if it fails.. */
232 return ret;
233 }
234
Ben Skeggs6ee73862009-12-11 19:24:15 +1000235 *pnvbo = nvbo;
236 return 0;
237}
238
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100239static void
240set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000241{
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100242 *n = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000243
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100244 if (type & TTM_PL_FLAG_VRAM)
245 pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
246 if (type & TTM_PL_FLAG_TT)
247 pl[(*n)++] = TTM_PL_FLAG_TT | flags;
248 if (type & TTM_PL_FLAG_SYSTEM)
249 pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
250}
Ben Skeggs37cb3e082009-12-16 16:22:42 +1000251
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200252static void
253set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
254{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000255 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
256 struct nouveau_fb *pfb = nouveau_fb(drm->device);
257 u32 vram_pages = pfb->ram.size >> PAGE_SHIFT;
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200258
Ben Skeggsebb945a2012-07-20 08:17:34 +1000259 if (nv_device(drm->device)->card_type == NV_10 &&
Francisco Jerez812f2192011-02-03 01:49:33 +0100260 nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
Francisco Jerez4beb1162011-11-06 21:21:28 +0100261 nvbo->bo.mem.num_pages < vram_pages / 4) {
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200262 /*
263 * Make sure that the color and depth buffers are handled
264 * by independent memory controller units. Up to a 9x
265 * speed up when alpha-blending and depth-test are enabled
266 * at the same time.
267 */
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200268 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
269 nvbo->placement.fpfn = vram_pages / 2;
270 nvbo->placement.lpfn = ~0;
271 } else {
272 nvbo->placement.fpfn = 0;
273 nvbo->placement.lpfn = vram_pages / 2;
274 }
275 }
276}
277
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100278void
279nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
280{
281 struct ttm_placement *pl = &nvbo->placement;
282 uint32_t flags = TTM_PL_MASK_CACHING |
283 (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
284
285 pl->placement = nvbo->placements;
286 set_placement_list(nvbo->placements, &pl->num_placement,
287 type, flags);
288
289 pl->busy_placement = nvbo->busy_placements;
290 set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
291 type | busy, flags);
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200292
293 set_placement_range(nvbo, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000294}
295
296int
297nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
298{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000299 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000300 struct ttm_buffer_object *bo = &nvbo->bo;
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100301 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000302
303 if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000304 NV_ERROR(drm, "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000305 1 << bo->mem.mem_type, memtype);
306 return -EINVAL;
307 }
308
309 if (nvbo->pin_refcnt++)
310 return 0;
311
312 ret = ttm_bo_reserve(bo, false, false, false, 0);
313 if (ret)
314 goto out;
315
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100316 nouveau_bo_placement_set(nvbo, memtype, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000317
Ben Skeggs7a45d762010-11-22 08:50:27 +1000318 ret = nouveau_bo_validate(nvbo, false, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000319 if (ret == 0) {
320 switch (bo->mem.mem_type) {
321 case TTM_PL_VRAM:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000322 drm->gem.vram_available -= bo->mem.size;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000323 break;
324 case TTM_PL_TT:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000325 drm->gem.gart_available -= bo->mem.size;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000326 break;
327 default:
328 break;
329 }
330 }
331 ttm_bo_unreserve(bo);
332out:
333 if (unlikely(ret))
334 nvbo->pin_refcnt--;
335 return ret;
336}
337
338int
339nouveau_bo_unpin(struct nouveau_bo *nvbo)
340{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000341 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000342 struct ttm_buffer_object *bo = &nvbo->bo;
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100343 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000344
345 if (--nvbo->pin_refcnt)
346 return 0;
347
348 ret = ttm_bo_reserve(bo, false, false, false, 0);
349 if (ret)
350 return ret;
351
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100352 nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000353
Ben Skeggs7a45d762010-11-22 08:50:27 +1000354 ret = nouveau_bo_validate(nvbo, false, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000355 if (ret == 0) {
356 switch (bo->mem.mem_type) {
357 case TTM_PL_VRAM:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000358 drm->gem.vram_available += bo->mem.size;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000359 break;
360 case TTM_PL_TT:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000361 drm->gem.gart_available += bo->mem.size;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000362 break;
363 default:
364 break;
365 }
366 }
367
368 ttm_bo_unreserve(bo);
369 return ret;
370}
371
372int
373nouveau_bo_map(struct nouveau_bo *nvbo)
374{
375 int ret;
376
377 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
378 if (ret)
379 return ret;
380
381 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
382 ttm_bo_unreserve(&nvbo->bo);
383 return ret;
384}
385
386void
387nouveau_bo_unmap(struct nouveau_bo *nvbo)
388{
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000389 if (nvbo)
390 ttm_bo_kunmap(&nvbo->kmap);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000391}
392
Ben Skeggs7a45d762010-11-22 08:50:27 +1000393int
394nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
395 bool no_wait_reserve, bool no_wait_gpu)
396{
397 int ret;
398
399 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
400 no_wait_reserve, no_wait_gpu);
401 if (ret)
402 return ret;
403
404 return 0;
405}
406
Ben Skeggs6ee73862009-12-11 19:24:15 +1000407u16
408nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
409{
410 bool is_iomem;
411 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
412 mem = &mem[index];
413 if (is_iomem)
414 return ioread16_native((void __force __iomem *)mem);
415 else
416 return *mem;
417}
418
419void
420nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
421{
422 bool is_iomem;
423 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
424 mem = &mem[index];
425 if (is_iomem)
426 iowrite16_native(val, (void __force __iomem *)mem);
427 else
428 *mem = val;
429}
430
431u32
432nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
433{
434 bool is_iomem;
435 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
436 mem = &mem[index];
437 if (is_iomem)
438 return ioread32_native((void __force __iomem *)mem);
439 else
440 return *mem;
441}
442
443void
444nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
445{
446 bool is_iomem;
447 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
448 mem = &mem[index];
449 if (is_iomem)
450 iowrite32_native(val, (void __force __iomem *)mem);
451 else
452 *mem = val;
453}
454
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400455static struct ttm_tt *
Ben Skeggsebb945a2012-07-20 08:17:34 +1000456nouveau_ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size,
457 uint32_t page_flags, struct page *dummy_read)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000458{
Max Filippovdf1b4b92012-10-14 01:58:26 +0400459#if __OS_HAS_AGP
Ben Skeggsebb945a2012-07-20 08:17:34 +1000460 struct nouveau_drm *drm = nouveau_bdev(bdev);
461 struct drm_device *dev = drm->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000462
Ben Skeggsebb945a2012-07-20 08:17:34 +1000463 if (drm->agp.stat == ENABLED) {
464 return ttm_agp_tt_create(bdev, dev->agp->bridge, size,
465 page_flags, dummy_read);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000466 }
Max Filippovdf1b4b92012-10-14 01:58:26 +0400467#endif
Ben Skeggs6ee73862009-12-11 19:24:15 +1000468
Ben Skeggsebb945a2012-07-20 08:17:34 +1000469 return nouveau_sgdma_create_ttm(bdev, size, page_flags, dummy_read);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000470}
471
472static int
473nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
474{
475 /* We'll do this from user space. */
476 return 0;
477}
478
479static int
480nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
481 struct ttm_mem_type_manager *man)
482{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000483 struct nouveau_drm *drm = nouveau_bdev(bdev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000484
485 switch (type) {
486 case TTM_PL_SYSTEM:
487 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
488 man->available_caching = TTM_PL_MASK_CACHING;
489 man->default_caching = TTM_PL_FLAG_CACHED;
490 break;
491 case TTM_PL_VRAM:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000492 if (nv_device(drm->device)->card_type >= NV_50) {
Ben Skeggs573a2a32010-08-25 15:26:04 +1000493 man->func = &nouveau_vram_manager;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000494 man->io_reserve_fastpath = false;
495 man->use_io_reserve_lru = true;
496 } else {
Ben Skeggs573a2a32010-08-25 15:26:04 +1000497 man->func = &ttm_bo_manager_func;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000498 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000499 man->flags = TTM_MEMTYPE_FLAG_FIXED |
Jerome Glissef32f02f2010-04-09 14:39:25 +0200500 TTM_MEMTYPE_FLAG_MAPPABLE;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000501 man->available_caching = TTM_PL_FLAG_UNCACHED |
502 TTM_PL_FLAG_WC;
503 man->default_caching = TTM_PL_FLAG_WC;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000504 break;
505 case TTM_PL_TT:
Ben Skeggsebb945a2012-07-20 08:17:34 +1000506 if (nv_device(drm->device)->card_type >= NV_50)
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000507 man->func = &nouveau_gart_manager;
508 else
Ben Skeggsebb945a2012-07-20 08:17:34 +1000509 if (drm->agp.stat != ENABLED)
Ben Skeggs3863c9b2012-07-14 19:09:17 +1000510 man->func = &nv04_gart_manager;
511 else
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000512 man->func = &ttm_bo_manager_func;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000513
514 if (drm->agp.stat == ENABLED) {
Jerome Glissef32f02f2010-04-09 14:39:25 +0200515 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
Francisco Jereza3d487e2010-11-20 22:11:22 +0100516 man->available_caching = TTM_PL_FLAG_UNCACHED |
517 TTM_PL_FLAG_WC;
518 man->default_caching = TTM_PL_FLAG_WC;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000519 } else {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000520 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
521 TTM_MEMTYPE_FLAG_CMA;
522 man->available_caching = TTM_PL_MASK_CACHING;
523 man->default_caching = TTM_PL_FLAG_CACHED;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000524 }
Ben Skeggsebb945a2012-07-20 08:17:34 +1000525
Ben Skeggs6ee73862009-12-11 19:24:15 +1000526 break;
527 default:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000528 return -EINVAL;
529 }
530 return 0;
531}
532
533static void
534nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
535{
536 struct nouveau_bo *nvbo = nouveau_bo(bo);
537
538 switch (bo->mem.mem_type) {
Francisco Jerez22fbd532009-12-11 18:40:17 +0100539 case TTM_PL_VRAM:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100540 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
541 TTM_PL_FLAG_SYSTEM);
Francisco Jerez22fbd532009-12-11 18:40:17 +0100542 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000543 default:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100544 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000545 break;
546 }
Francisco Jerez22fbd532009-12-11 18:40:17 +0100547
548 *pl = nvbo->placement;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000549}
550
551
552/* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
553 * TTM_PL_{VRAM,TT} directly.
554 */
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100555
Ben Skeggs6ee73862009-12-11 19:24:15 +1000556static int
557nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000558 struct nouveau_bo *nvbo, bool evict,
559 bool no_wait_reserve, bool no_wait_gpu,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000560 struct ttm_mem_reg *new_mem)
561{
562 struct nouveau_fence *fence = NULL;
563 int ret;
564
Ben Skeggsd375e7d52012-04-30 13:30:00 +1000565 ret = nouveau_fence_new(chan, &fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000566 if (ret)
567 return ret;
568
Francisco Jerez64798812010-09-21 19:02:01 +0200569 ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
Francisco Jerez311ab692010-07-04 12:54:23 +0200570 no_wait_reserve, no_wait_gpu, new_mem);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200571 nouveau_fence_unref(&fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000572 return ret;
573}
574
Ben Skeggs6ee73862009-12-11 19:24:15 +1000575static int
Ben Skeggs49981042012-08-06 19:38:25 +1000576nve0_bo_move_init(struct nouveau_channel *chan, u32 handle)
577{
578 int ret = RING_SPACE(chan, 2);
579 if (ret == 0) {
580 BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
581 OUT_RING (chan, handle);
582 FIRE_RING (chan);
583 }
584 return ret;
585}
586
587static int
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000588nve0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
589 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
590{
591 struct nouveau_mem *node = old_mem->mm_node;
592 int ret = RING_SPACE(chan, 10);
593 if (ret == 0) {
Ben Skeggs6d597022012-04-01 21:09:13 +1000594 BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000595 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
596 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
597 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
598 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
599 OUT_RING (chan, PAGE_SIZE);
600 OUT_RING (chan, PAGE_SIZE);
601 OUT_RING (chan, PAGE_SIZE);
602 OUT_RING (chan, new_mem->num_pages);
Ben Skeggs6d597022012-04-01 21:09:13 +1000603 BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386);
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000604 }
605 return ret;
606}
607
608static int
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000609nvc0_bo_move_init(struct nouveau_channel *chan, u32 handle)
610{
611 int ret = RING_SPACE(chan, 2);
612 if (ret == 0) {
613 BEGIN_NVC0(chan, NvSubCopy, 0x0000, 1);
614 OUT_RING (chan, handle);
615 }
616 return ret;
617}
618
619static int
Ben Skeggs1a460982012-05-04 15:17:28 +1000620nvc0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
621 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
622{
623 struct nouveau_mem *node = old_mem->mm_node;
624 u64 src_offset = node->vma[0].offset;
625 u64 dst_offset = node->vma[1].offset;
626 u32 page_count = new_mem->num_pages;
627 int ret;
628
629 page_count = new_mem->num_pages;
630 while (page_count) {
631 int line_count = (page_count > 8191) ? 8191 : page_count;
632
633 ret = RING_SPACE(chan, 11);
634 if (ret)
635 return ret;
636
637 BEGIN_NVC0(chan, NvSubCopy, 0x030c, 8);
638 OUT_RING (chan, upper_32_bits(src_offset));
639 OUT_RING (chan, lower_32_bits(src_offset));
640 OUT_RING (chan, upper_32_bits(dst_offset));
641 OUT_RING (chan, lower_32_bits(dst_offset));
642 OUT_RING (chan, PAGE_SIZE);
643 OUT_RING (chan, PAGE_SIZE);
644 OUT_RING (chan, PAGE_SIZE);
645 OUT_RING (chan, line_count);
646 BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
647 OUT_RING (chan, 0x00000110);
648
649 page_count -= line_count;
650 src_offset += (PAGE_SIZE * line_count);
651 dst_offset += (PAGE_SIZE * line_count);
652 }
653
654 return 0;
655}
656
657static int
Ben Skeggs183720b2010-12-09 15:17:10 +1000658nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
659 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
660{
Ben Skeggsd2f966662011-06-06 20:54:42 +1000661 struct nouveau_mem *node = old_mem->mm_node;
662 u64 src_offset = node->vma[0].offset;
663 u64 dst_offset = node->vma[1].offset;
Ben Skeggs183720b2010-12-09 15:17:10 +1000664 u32 page_count = new_mem->num_pages;
665 int ret;
666
Ben Skeggs183720b2010-12-09 15:17:10 +1000667 page_count = new_mem->num_pages;
668 while (page_count) {
669 int line_count = (page_count > 2047) ? 2047 : page_count;
670
671 ret = RING_SPACE(chan, 12);
672 if (ret)
673 return ret;
674
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000675 BEGIN_NVC0(chan, NvSubCopy, 0x0238, 2);
Ben Skeggs183720b2010-12-09 15:17:10 +1000676 OUT_RING (chan, upper_32_bits(dst_offset));
677 OUT_RING (chan, lower_32_bits(dst_offset));
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000678 BEGIN_NVC0(chan, NvSubCopy, 0x030c, 6);
Ben Skeggs183720b2010-12-09 15:17:10 +1000679 OUT_RING (chan, upper_32_bits(src_offset));
680 OUT_RING (chan, lower_32_bits(src_offset));
681 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
682 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
683 OUT_RING (chan, PAGE_SIZE); /* line_length */
684 OUT_RING (chan, line_count);
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000685 BEGIN_NVC0(chan, NvSubCopy, 0x0300, 1);
Ben Skeggs183720b2010-12-09 15:17:10 +1000686 OUT_RING (chan, 0x00100110);
687
688 page_count -= line_count;
689 src_offset += (PAGE_SIZE * line_count);
690 dst_offset += (PAGE_SIZE * line_count);
691 }
692
693 return 0;
694}
695
696static int
Ben Skeggsfdf53242012-05-04 15:15:12 +1000697nva3_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
698 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
699{
700 struct nouveau_mem *node = old_mem->mm_node;
701 u64 src_offset = node->vma[0].offset;
702 u64 dst_offset = node->vma[1].offset;
703 u32 page_count = new_mem->num_pages;
704 int ret;
705
706 page_count = new_mem->num_pages;
707 while (page_count) {
708 int line_count = (page_count > 8191) ? 8191 : page_count;
709
710 ret = RING_SPACE(chan, 11);
711 if (ret)
712 return ret;
713
714 BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
715 OUT_RING (chan, upper_32_bits(src_offset));
716 OUT_RING (chan, lower_32_bits(src_offset));
717 OUT_RING (chan, upper_32_bits(dst_offset));
718 OUT_RING (chan, lower_32_bits(dst_offset));
719 OUT_RING (chan, PAGE_SIZE);
720 OUT_RING (chan, PAGE_SIZE);
721 OUT_RING (chan, PAGE_SIZE);
722 OUT_RING (chan, line_count);
723 BEGIN_NV04(chan, NvSubCopy, 0x0300, 1);
724 OUT_RING (chan, 0x00000110);
725
726 page_count -= line_count;
727 src_offset += (PAGE_SIZE * line_count);
728 dst_offset += (PAGE_SIZE * line_count);
729 }
730
731 return 0;
732}
733
734static int
Ben Skeggs5490e5d2012-05-04 14:34:16 +1000735nv98_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
736 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
737{
738 struct nouveau_mem *node = old_mem->mm_node;
739 int ret = RING_SPACE(chan, 7);
740 if (ret == 0) {
741 BEGIN_NV04(chan, NvSubCopy, 0x0320, 6);
742 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
743 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
744 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
745 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
746 OUT_RING (chan, 0x00000000 /* COPY */);
747 OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
748 }
749 return ret;
750}
751
752static int
Ben Skeggs4c193d22012-05-04 14:21:15 +1000753nv84_bo_move_exec(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
754 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
755{
756 struct nouveau_mem *node = old_mem->mm_node;
757 int ret = RING_SPACE(chan, 7);
758 if (ret == 0) {
759 BEGIN_NV04(chan, NvSubCopy, 0x0304, 6);
760 OUT_RING (chan, new_mem->num_pages << PAGE_SHIFT);
761 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
762 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
763 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
764 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
765 OUT_RING (chan, 0x00000000 /* MODE_COPY, QUERY_NONE */);
766 }
767 return ret;
768}
769
770static int
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000771nv50_bo_move_init(struct nouveau_channel *chan, u32 handle)
772{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000773 int ret = RING_SPACE(chan, 6);
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000774 if (ret == 0) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000775 BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
776 OUT_RING (chan, handle);
777 BEGIN_NV04(chan, NvSubCopy, 0x0180, 3);
778 OUT_RING (chan, NvNotify0);
779 OUT_RING (chan, NvDmaFB);
780 OUT_RING (chan, NvDmaFB);
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000781 }
782
783 return ret;
784}
785
786static int
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000787nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
788 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000789{
Ben Skeggsd2f966662011-06-06 20:54:42 +1000790 struct nouveau_mem *node = old_mem->mm_node;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000791 struct nouveau_bo *nvbo = nouveau_bo(bo);
792 u64 length = (new_mem->num_pages << PAGE_SHIFT);
Ben Skeggsd2f966662011-06-06 20:54:42 +1000793 u64 src_offset = node->vma[0].offset;
794 u64 dst_offset = node->vma[1].offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000795 int ret;
796
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000797 while (length) {
798 u32 amount, stride, height;
799
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000800 amount = min(length, (u64)(4 * 1024 * 1024));
801 stride = 16 * 4;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000802 height = amount / stride;
803
Francisco Jerezf13b3262010-10-10 06:01:08 +0200804 if (new_mem->mem_type == TTM_PL_VRAM &&
805 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000806 ret = RING_SPACE(chan, 8);
807 if (ret)
808 return ret;
809
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000810 BEGIN_NV04(chan, NvSubCopy, 0x0200, 7);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000811 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000812 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000813 OUT_RING (chan, stride);
814 OUT_RING (chan, height);
815 OUT_RING (chan, 1);
816 OUT_RING (chan, 0);
817 OUT_RING (chan, 0);
818 } else {
819 ret = RING_SPACE(chan, 2);
820 if (ret)
821 return ret;
822
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000823 BEGIN_NV04(chan, NvSubCopy, 0x0200, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000824 OUT_RING (chan, 1);
825 }
Francisco Jerezf13b3262010-10-10 06:01:08 +0200826 if (old_mem->mem_type == TTM_PL_VRAM &&
827 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000828 ret = RING_SPACE(chan, 8);
829 if (ret)
830 return ret;
831
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000832 BEGIN_NV04(chan, NvSubCopy, 0x021c, 7);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000833 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000834 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000835 OUT_RING (chan, stride);
836 OUT_RING (chan, height);
837 OUT_RING (chan, 1);
838 OUT_RING (chan, 0);
839 OUT_RING (chan, 0);
840 } else {
841 ret = RING_SPACE(chan, 2);
842 if (ret)
843 return ret;
844
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000845 BEGIN_NV04(chan, NvSubCopy, 0x021c, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000846 OUT_RING (chan, 1);
847 }
848
849 ret = RING_SPACE(chan, 14);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000850 if (ret)
851 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000852
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000853 BEGIN_NV04(chan, NvSubCopy, 0x0238, 2);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000854 OUT_RING (chan, upper_32_bits(src_offset));
855 OUT_RING (chan, upper_32_bits(dst_offset));
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000856 BEGIN_NV04(chan, NvSubCopy, 0x030c, 8);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000857 OUT_RING (chan, lower_32_bits(src_offset));
858 OUT_RING (chan, lower_32_bits(dst_offset));
859 OUT_RING (chan, stride);
860 OUT_RING (chan, stride);
861 OUT_RING (chan, stride);
862 OUT_RING (chan, height);
863 OUT_RING (chan, 0x00000101);
864 OUT_RING (chan, 0x00000000);
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000865 BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000866 OUT_RING (chan, 0);
867
868 length -= amount;
869 src_offset += amount;
870 dst_offset += amount;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000871 }
872
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000873 return 0;
874}
875
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000876static int
877nv04_bo_move_init(struct nouveau_channel *chan, u32 handle)
878{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000879 int ret = RING_SPACE(chan, 4);
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000880 if (ret == 0) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000881 BEGIN_NV04(chan, NvSubCopy, 0x0000, 1);
882 OUT_RING (chan, handle);
883 BEGIN_NV04(chan, NvSubCopy, 0x0180, 1);
884 OUT_RING (chan, NvNotify0);
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000885 }
886
887 return ret;
888}
889
Ben Skeggsa6704782011-02-16 09:10:20 +1000890static inline uint32_t
891nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
892 struct nouveau_channel *chan, struct ttm_mem_reg *mem)
893{
894 if (mem->mem_type == TTM_PL_TT)
Ben Skeggsebb945a2012-07-20 08:17:34 +1000895 return NvDmaTT;
896 return NvDmaFB;
Ben Skeggsa6704782011-02-16 09:10:20 +1000897}
898
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000899static int
900nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
901 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
902{
Ben Skeggsd961db72010-08-05 10:48:18 +1000903 u32 src_offset = old_mem->start << PAGE_SHIFT;
904 u32 dst_offset = new_mem->start << PAGE_SHIFT;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000905 u32 page_count = new_mem->num_pages;
906 int ret;
907
908 ret = RING_SPACE(chan, 3);
909 if (ret)
910 return ret;
911
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000912 BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000913 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
914 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
915
Ben Skeggs6ee73862009-12-11 19:24:15 +1000916 page_count = new_mem->num_pages;
917 while (page_count) {
918 int line_count = (page_count > 2047) ? 2047 : page_count;
919
Ben Skeggs6ee73862009-12-11 19:24:15 +1000920 ret = RING_SPACE(chan, 11);
921 if (ret)
922 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000923
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000924 BEGIN_NV04(chan, NvSubCopy,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000925 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000926 OUT_RING (chan, src_offset);
927 OUT_RING (chan, dst_offset);
928 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
929 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
930 OUT_RING (chan, PAGE_SIZE); /* line_length */
931 OUT_RING (chan, line_count);
932 OUT_RING (chan, 0x00000101);
933 OUT_RING (chan, 0x00000000);
Ben Skeggsd1b167e2012-05-04 14:01:52 +1000934 BEGIN_NV04(chan, NvSubCopy, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000935 OUT_RING (chan, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000936
937 page_count -= line_count;
938 src_offset += (PAGE_SIZE * line_count);
939 dst_offset += (PAGE_SIZE * line_count);
940 }
941
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000942 return 0;
943}
944
945static int
Ben Skeggsd2f966662011-06-06 20:54:42 +1000946nouveau_vma_getmap(struct nouveau_channel *chan, struct nouveau_bo *nvbo,
947 struct ttm_mem_reg *mem, struct nouveau_vma *vma)
948{
949 struct nouveau_mem *node = mem->mm_node;
950 int ret;
951
Ben Skeggsebb945a2012-07-20 08:17:34 +1000952 ret = nouveau_vm_get(nv_client(chan->cli)->vm, mem->num_pages <<
953 PAGE_SHIFT, node->page_shift,
954 NV_MEM_ACCESS_RW, vma);
Ben Skeggsd2f966662011-06-06 20:54:42 +1000955 if (ret)
956 return ret;
957
958 if (mem->mem_type == TTM_PL_VRAM)
959 nouveau_vm_map(vma, node);
960 else
Ben Skeggsf7b24c42011-12-22 15:20:21 +1000961 nouveau_vm_map_sg(vma, 0, mem->num_pages << PAGE_SHIFT, node);
Ben Skeggsd2f966662011-06-06 20:54:42 +1000962
963 return 0;
964}
965
966static int
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000967nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
968 bool no_wait_reserve, bool no_wait_gpu,
969 struct ttm_mem_reg *new_mem)
970{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000971 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
972 struct nouveau_channel *chan = chan = drm->channel;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000973 struct nouveau_bo *nvbo = nouveau_bo(bo);
Ben Skeggs3425df42011-02-10 11:22:12 +1000974 struct ttm_mem_reg *old_mem = &bo->mem;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000975 int ret;
976
Ben Skeggsebb945a2012-07-20 08:17:34 +1000977 mutex_lock(&chan->cli->mutex);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000978
Ben Skeggsd2f966662011-06-06 20:54:42 +1000979 /* create temporary vmas for the transfer and attach them to the
980 * old nouveau_mem node, these will get cleaned up after ttm has
981 * destroyed the ttm_mem_reg
Ben Skeggs3425df42011-02-10 11:22:12 +1000982 */
Ben Skeggsebb945a2012-07-20 08:17:34 +1000983 if (nv_device(drm->device)->card_type >= NV_50) {
Ben Skeggsd5f42392011-02-10 12:22:52 +1000984 struct nouveau_mem *node = old_mem->mm_node;
Ben Skeggs3425df42011-02-10 11:22:12 +1000985
Ben Skeggsd2f966662011-06-06 20:54:42 +1000986 ret = nouveau_vma_getmap(chan, nvbo, old_mem, &node->vma[0]);
987 if (ret)
988 goto out;
Ben Skeggs3425df42011-02-10 11:22:12 +1000989
Ben Skeggsd2f966662011-06-06 20:54:42 +1000990 ret = nouveau_vma_getmap(chan, nvbo, new_mem, &node->vma[1]);
991 if (ret)
992 goto out;
Ben Skeggs3425df42011-02-10 11:22:12 +1000993 }
994
Ben Skeggsebb945a2012-07-20 08:17:34 +1000995 ret = drm->ttm.move(chan, bo, &bo->mem, new_mem);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000996 if (ret == 0) {
997 ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
998 no_wait_reserve,
999 no_wait_gpu, new_mem);
1000 }
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +10001001
Ben Skeggs3425df42011-02-10 11:22:12 +10001002out:
Ben Skeggsebb945a2012-07-20 08:17:34 +10001003 mutex_unlock(&chan->cli->mutex);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +10001004 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001005}
1006
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001007void
Ben Skeggs49981042012-08-06 19:38:25 +10001008nouveau_bo_move_init(struct nouveau_drm *drm)
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001009{
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001010 static const struct {
1011 const char *name;
Ben Skeggs1a460982012-05-04 15:17:28 +10001012 int engine;
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001013 u32 oclass;
1014 int (*exec)(struct nouveau_channel *,
1015 struct ttm_buffer_object *,
1016 struct ttm_mem_reg *, struct ttm_mem_reg *);
1017 int (*init)(struct nouveau_channel *, u32 handle);
1018 } _methods[] = {
Ben Skeggs49981042012-08-06 19:38:25 +10001019 { "COPY", 0, 0xa0b5, nve0_bo_move_copy, nve0_bo_move_init },
1020 { "GRCE", 0, 0xa0b5, nve0_bo_move_copy, nvc0_bo_move_init },
Ben Skeggs1a460982012-05-04 15:17:28 +10001021 { "COPY1", 5, 0x90b8, nvc0_bo_move_copy, nvc0_bo_move_init },
1022 { "COPY0", 4, 0x90b5, nvc0_bo_move_copy, nvc0_bo_move_init },
1023 { "COPY", 0, 0x85b5, nva3_bo_move_copy, nv50_bo_move_init },
1024 { "CRYPT", 0, 0x74c1, nv84_bo_move_exec, nv50_bo_move_init },
1025 { "M2MF", 0, 0x9039, nvc0_bo_move_m2mf, nvc0_bo_move_init },
1026 { "M2MF", 0, 0x5039, nv50_bo_move_m2mf, nv50_bo_move_init },
1027 { "M2MF", 0, 0x0039, nv04_bo_move_m2mf, nv04_bo_move_init },
Ben Skeggs5490e5d2012-05-04 14:34:16 +10001028 {},
Ben Skeggs1a460982012-05-04 15:17:28 +10001029 { "CRYPT", 0, 0x88b4, nv98_bo_move_exec, nv50_bo_move_init },
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001030 }, *mthd = _methods;
1031 const char *name = "CPU";
1032 int ret;
1033
1034 do {
Ben Skeggsebb945a2012-07-20 08:17:34 +10001035 struct nouveau_object *object;
Ben Skeggs49981042012-08-06 19:38:25 +10001036 struct nouveau_channel *chan;
Ben Skeggs1a460982012-05-04 15:17:28 +10001037 u32 handle = (mthd->engine << 16) | mthd->oclass;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001038
Ben Skeggs49981042012-08-06 19:38:25 +10001039 if (mthd->init == nve0_bo_move_init)
1040 chan = drm->cechan;
1041 else
1042 chan = drm->channel;
1043 if (chan == NULL)
1044 continue;
1045
1046 ret = nouveau_object_new(nv_object(drm), chan->handle, handle,
Ben Skeggsebb945a2012-07-20 08:17:34 +10001047 mthd->oclass, NULL, 0, &object);
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001048 if (ret == 0) {
Ben Skeggs1a460982012-05-04 15:17:28 +10001049 ret = mthd->init(chan, handle);
Ben Skeggsebb945a2012-07-20 08:17:34 +10001050 if (ret) {
Ben Skeggs49981042012-08-06 19:38:25 +10001051 nouveau_object_del(nv_object(drm),
Ben Skeggsebb945a2012-07-20 08:17:34 +10001052 chan->handle, handle);
1053 continue;
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001054 }
Ben Skeggsebb945a2012-07-20 08:17:34 +10001055
1056 drm->ttm.move = mthd->exec;
1057 name = mthd->name;
1058 break;
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001059 }
1060 } while ((++mthd)->exec);
1061
Ben Skeggsebb945a2012-07-20 08:17:34 +10001062 NV_INFO(drm, "MM: using %s for buffer copies\n", name);
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001063}
1064
Ben Skeggs6ee73862009-12-11 19:24:15 +10001065static int
1066nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001067 bool no_wait_reserve, bool no_wait_gpu,
1068 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001069{
1070 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
1071 struct ttm_placement placement;
1072 struct ttm_mem_reg tmp_mem;
1073 int ret;
1074
1075 placement.fpfn = placement.lpfn = 0;
1076 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +01001077 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001078
1079 tmp_mem = *new_mem;
1080 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001081 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001082 if (ret)
1083 return ret;
1084
1085 ret = ttm_tt_bind(bo->ttm, &tmp_mem);
1086 if (ret)
1087 goto out;
1088
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001089 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001090 if (ret)
1091 goto out;
1092
Ben Skeggsb8884da2011-02-14 13:51:28 +10001093 ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001094out:
Ben Skeggs42311ff2010-08-04 12:07:08 +10001095 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001096 return ret;
1097}
1098
1099static int
1100nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001101 bool no_wait_reserve, bool no_wait_gpu,
1102 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001103{
1104 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
1105 struct ttm_placement placement;
1106 struct ttm_mem_reg tmp_mem;
1107 int ret;
1108
1109 placement.fpfn = placement.lpfn = 0;
1110 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +01001111 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001112
1113 tmp_mem = *new_mem;
1114 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001115 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001116 if (ret)
1117 return ret;
1118
Ben Skeggsb8884da2011-02-14 13:51:28 +10001119 ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001120 if (ret)
1121 goto out;
1122
Ben Skeggsb8884da2011-02-14 13:51:28 +10001123 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001124 if (ret)
1125 goto out;
1126
1127out:
Ben Skeggs42311ff2010-08-04 12:07:08 +10001128 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001129 return ret;
1130}
1131
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001132static void
1133nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)
1134{
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001135 struct nouveau_bo *nvbo = nouveau_bo(bo);
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001136 struct nouveau_vma *vma;
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001137
Ben Skeggs9f1feed2012-01-25 15:34:22 +10001138 /* ttm can now (stupidly) pass the driver bos it didn't create... */
1139 if (bo->destroy != nouveau_bo_del_ttm)
1140 return;
1141
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001142 list_for_each_entry(vma, &nvbo->vma_list, head) {
Jerome Glissedc97b342011-11-18 11:47:03 -05001143 if (new_mem && new_mem->mem_type == TTM_PL_VRAM) {
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001144 nouveau_vm_map(vma, new_mem->mm_node);
1145 } else
Jerome Glissedc97b342011-11-18 11:47:03 -05001146 if (new_mem && new_mem->mem_type == TTM_PL_TT &&
Ben Skeggsebb945a2012-07-20 08:17:34 +10001147 nvbo->page_shift == vma->vm->vmm->spg_shift) {
Dave Airlie22b33e82012-04-02 11:53:06 +01001148 if (((struct nouveau_mem *)new_mem->mm_node)->sg)
1149 nouveau_vm_map_sg_table(vma, 0, new_mem->
1150 num_pages << PAGE_SHIFT,
1151 new_mem->mm_node);
1152 else
1153 nouveau_vm_map_sg(vma, 0, new_mem->
1154 num_pages << PAGE_SHIFT,
1155 new_mem->mm_node);
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001156 } else {
1157 nouveau_vm_unmap(vma);
1158 }
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001159 }
1160}
1161
Ben Skeggs6ee73862009-12-11 19:24:15 +10001162static int
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001163nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
Ben Skeggsebb945a2012-07-20 08:17:34 +10001164 struct nouveau_drm_tile **new_tile)
Ben Skeggs6ee73862009-12-11 19:24:15 +10001165{
Ben Skeggsebb945a2012-07-20 08:17:34 +10001166 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1167 struct drm_device *dev = drm->dev;
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001168 struct nouveau_bo *nvbo = nouveau_bo(bo);
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001169 u64 offset = new_mem->start << PAGE_SHIFT;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001170
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001171 *new_tile = NULL;
1172 if (new_mem->mem_type != TTM_PL_VRAM)
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001173 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001174
Ben Skeggsebb945a2012-07-20 08:17:34 +10001175 if (nv_device(drm->device)->card_type >= NV_10) {
Ben Skeggsbc9e7b92012-07-19 17:54:21 +10001176 *new_tile = nv10_bo_set_tiling(dev, offset, new_mem->size,
Francisco Jereza5cf68b2010-10-24 16:14:41 +02001177 nvbo->tile_mode,
1178 nvbo->tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001179 }
1180
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001181 return 0;
1182}
Ben Skeggs6ee73862009-12-11 19:24:15 +10001183
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001184static void
1185nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
Ben Skeggsebb945a2012-07-20 08:17:34 +10001186 struct nouveau_drm_tile *new_tile,
1187 struct nouveau_drm_tile **old_tile)
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001188{
Ben Skeggsebb945a2012-07-20 08:17:34 +10001189 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
1190 struct drm_device *dev = drm->dev;
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001191
Ben Skeggsbc9e7b92012-07-19 17:54:21 +10001192 nv10_bo_put_tile_region(dev, *old_tile, bo->sync_obj);
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001193 *old_tile = new_tile;
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001194}
1195
1196static int
1197nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001198 bool no_wait_reserve, bool no_wait_gpu,
1199 struct ttm_mem_reg *new_mem)
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001200{
Ben Skeggsebb945a2012-07-20 08:17:34 +10001201 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001202 struct nouveau_bo *nvbo = nouveau_bo(bo);
1203 struct ttm_mem_reg *old_mem = &bo->mem;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001204 struct nouveau_drm_tile *new_tile = NULL;
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001205 int ret = 0;
1206
Ben Skeggsebb945a2012-07-20 08:17:34 +10001207 if (nv_device(drm->device)->card_type < NV_50) {
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001208 ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
1209 if (ret)
1210 return ret;
1211 }
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001212
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001213 /* Fake bo copy. */
Ben Skeggs6ee73862009-12-11 19:24:15 +10001214 if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
1215 BUG_ON(bo->mem.mm_node != NULL);
1216 bo->mem = *new_mem;
1217 new_mem->mm_node = NULL;
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001218 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001219 }
1220
Ben Skeggsd1b167e2012-05-04 14:01:52 +10001221 /* CPU copy if we have no accelerated method available */
Ben Skeggsebb945a2012-07-20 08:17:34 +10001222 if (!drm->ttm.move) {
Ben Skeggsb8a6a802010-08-27 11:55:43 +10001223 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
1224 goto out;
1225 }
1226
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001227 /* Hardware assisted copy. */
1228 if (new_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001229 ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001230 else if (old_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001231 ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001232 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001233 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +10001234
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001235 if (!ret)
1236 goto out;
1237
1238 /* Fallback to software copy. */
Jerome Glisse9d87fa22010-04-07 10:21:19 +00001239 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001240
1241out:
Ben Skeggsebb945a2012-07-20 08:17:34 +10001242 if (nv_device(drm->device)->card_type < NV_50) {
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001243 if (ret)
1244 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
1245 else
1246 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
1247 }
Francisco Jereza0af9ad2009-12-11 16:51:09 +01001248
1249 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +10001250}
1251
1252static int
1253nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
1254{
1255 return 0;
1256}
1257
Jerome Glissef32f02f2010-04-09 14:39:25 +02001258static int
1259nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1260{
1261 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
Ben Skeggsebb945a2012-07-20 08:17:34 +10001262 struct nouveau_drm *drm = nouveau_bdev(bdev);
1263 struct drm_device *dev = drm->dev;
Ben Skeggsf869ef82010-11-15 11:53:16 +10001264 int ret;
Jerome Glissef32f02f2010-04-09 14:39:25 +02001265
1266 mem->bus.addr = NULL;
1267 mem->bus.offset = 0;
1268 mem->bus.size = mem->num_pages << PAGE_SHIFT;
1269 mem->bus.base = 0;
1270 mem->bus.is_iomem = false;
1271 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
1272 return -EINVAL;
1273 switch (mem->mem_type) {
1274 case TTM_PL_SYSTEM:
1275 /* System memory */
1276 return 0;
1277 case TTM_PL_TT:
1278#if __OS_HAS_AGP
Ben Skeggsebb945a2012-07-20 08:17:34 +10001279 if (drm->agp.stat == ENABLED) {
Ben Skeggsd961db72010-08-05 10:48:18 +10001280 mem->bus.offset = mem->start << PAGE_SHIFT;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001281 mem->bus.base = drm->agp.base;
Jerome Glissef32f02f2010-04-09 14:39:25 +02001282 mem->bus.is_iomem = true;
1283 }
1284#endif
1285 break;
1286 case TTM_PL_VRAM:
Ben Skeggs3863c9b2012-07-14 19:09:17 +10001287 mem->bus.offset = mem->start << PAGE_SHIFT;
Jordan Crouse01d73a62010-05-27 13:40:24 -06001288 mem->bus.base = pci_resource_start(dev->pdev, 1);
Jerome Glissef32f02f2010-04-09 14:39:25 +02001289 mem->bus.is_iomem = true;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001290 if (nv_device(drm->device)->card_type >= NV_50) {
1291 struct nouveau_bar *bar = nouveau_bar(drm->device);
Ben Skeggs3863c9b2012-07-14 19:09:17 +10001292 struct nouveau_mem *node = mem->mm_node;
1293
Ben Skeggsebb945a2012-07-20 08:17:34 +10001294 ret = bar->umap(bar, node, NV_MEM_ACCESS_RW,
Ben Skeggs3863c9b2012-07-14 19:09:17 +10001295 &node->bar_vma);
1296 if (ret)
1297 return ret;
1298
1299 mem->bus.offset = node->bar_vma.offset;
1300 }
Jerome Glissef32f02f2010-04-09 14:39:25 +02001301 break;
1302 default:
1303 return -EINVAL;
1304 }
1305 return 0;
1306}
1307
1308static void
1309nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1310{
Ben Skeggsebb945a2012-07-20 08:17:34 +10001311 struct nouveau_drm *drm = nouveau_bdev(bdev);
1312 struct nouveau_bar *bar = nouveau_bar(drm->device);
Ben Skeggsd5f42392011-02-10 12:22:52 +10001313 struct nouveau_mem *node = mem->mm_node;
Ben Skeggsf869ef82010-11-15 11:53:16 +10001314
Ben Skeggsd5f42392011-02-10 12:22:52 +10001315 if (!node->bar_vma.node)
Ben Skeggsf869ef82010-11-15 11:53:16 +10001316 return;
1317
Ben Skeggsebb945a2012-07-20 08:17:34 +10001318 bar->unmap(bar, &node->bar_vma);
Jerome Glissef32f02f2010-04-09 14:39:25 +02001319}
1320
1321static int
1322nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
1323{
Ben Skeggsebb945a2012-07-20 08:17:34 +10001324 struct nouveau_drm *drm = nouveau_bdev(bo->bdev);
Ben Skeggse1429b42010-09-10 11:12:25 +10001325 struct nouveau_bo *nvbo = nouveau_bo(bo);
Ben Skeggsebb945a2012-07-20 08:17:34 +10001326 struct nouveau_device *device = nv_device(drm->device);
1327 u32 mappable = pci_resource_len(device->pdev, 1) >> PAGE_SHIFT;
Ben Skeggse1429b42010-09-10 11:12:25 +10001328
1329 /* as long as the bo isn't in vram, and isn't tiled, we've got
1330 * nothing to do here.
1331 */
1332 if (bo->mem.mem_type != TTM_PL_VRAM) {
Ben Skeggsebb945a2012-07-20 08:17:34 +10001333 if (nv_device(drm->device)->card_type < NV_50 ||
Francisco Jerezf13b3262010-10-10 06:01:08 +02001334 !nouveau_bo_tile_layout(nvbo))
Ben Skeggse1429b42010-09-10 11:12:25 +10001335 return 0;
1336 }
1337
1338 /* make sure bo is in mappable vram */
Ben Skeggsebb945a2012-07-20 08:17:34 +10001339 if (bo->mem.start + bo->mem.num_pages < mappable)
Ben Skeggse1429b42010-09-10 11:12:25 +10001340 return 0;
1341
1342
1343 nvbo->placement.fpfn = 0;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001344 nvbo->placement.lpfn = mappable;
Dave Airliec2848152012-05-18 15:31:12 +01001345 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0);
Ben Skeggs7a45d762010-11-22 08:50:27 +10001346 return nouveau_bo_validate(nvbo, false, true, false);
Jerome Glissef32f02f2010-04-09 14:39:25 +02001347}
1348
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001349static int
1350nouveau_ttm_tt_populate(struct ttm_tt *ttm)
1351{
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001352 struct ttm_dma_tt *ttm_dma = (void *)ttm;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001353 struct nouveau_drm *drm;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001354 struct drm_device *dev;
1355 unsigned i;
1356 int r;
Dave Airlie22b33e82012-04-02 11:53:06 +01001357 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001358
1359 if (ttm->state != tt_unpopulated)
1360 return 0;
1361
Dave Airlie22b33e82012-04-02 11:53:06 +01001362 if (slave && ttm->sg) {
1363 /* make userspace faulting work */
1364 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1365 ttm_dma->dma_address, ttm->num_pages);
1366 ttm->state = tt_unbound;
1367 return 0;
1368 }
1369
Ben Skeggsebb945a2012-07-20 08:17:34 +10001370 drm = nouveau_bdev(ttm->bdev);
1371 dev = drm->dev;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001372
Jerome Glissedea7e0a2012-01-03 17:37:37 -05001373#if __OS_HAS_AGP
Ben Skeggsebb945a2012-07-20 08:17:34 +10001374 if (drm->agp.stat == ENABLED) {
Jerome Glissedea7e0a2012-01-03 17:37:37 -05001375 return ttm_agp_tt_populate(ttm);
1376 }
1377#endif
1378
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001379#ifdef CONFIG_SWIOTLB
1380 if (swiotlb_nr_tbl()) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001381 return ttm_dma_populate((void *)ttm, dev->dev);
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001382 }
1383#endif
1384
1385 r = ttm_pool_populate(ttm);
1386 if (r) {
1387 return r;
1388 }
1389
1390 for (i = 0; i < ttm->num_pages; i++) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001391 ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001392 0, PAGE_SIZE,
1393 PCI_DMA_BIDIRECTIONAL);
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001394 if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001395 while (--i) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001396 pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001397 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001398 ttm_dma->dma_address[i] = 0;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001399 }
1400 ttm_pool_unpopulate(ttm);
1401 return -EFAULT;
1402 }
1403 }
1404 return 0;
1405}
1406
1407static void
1408nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
1409{
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001410 struct ttm_dma_tt *ttm_dma = (void *)ttm;
Ben Skeggsebb945a2012-07-20 08:17:34 +10001411 struct nouveau_drm *drm;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001412 struct drm_device *dev;
1413 unsigned i;
Dave Airlie22b33e82012-04-02 11:53:06 +01001414 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1415
1416 if (slave)
1417 return;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001418
Ben Skeggsebb945a2012-07-20 08:17:34 +10001419 drm = nouveau_bdev(ttm->bdev);
1420 dev = drm->dev;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001421
Jerome Glissedea7e0a2012-01-03 17:37:37 -05001422#if __OS_HAS_AGP
Ben Skeggsebb945a2012-07-20 08:17:34 +10001423 if (drm->agp.stat == ENABLED) {
Jerome Glissedea7e0a2012-01-03 17:37:37 -05001424 ttm_agp_tt_unpopulate(ttm);
1425 return;
1426 }
1427#endif
1428
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001429#ifdef CONFIG_SWIOTLB
1430 if (swiotlb_nr_tbl()) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001431 ttm_dma_unpopulate((void *)ttm, dev->dev);
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001432 return;
1433 }
1434#endif
1435
1436 for (i = 0; i < ttm->num_pages; i++) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001437 if (ttm_dma->dma_address[i]) {
1438 pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001439 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
1440 }
1441 }
1442
1443 ttm_pool_unpopulate(ttm);
1444}
1445
Ben Skeggs875ac342012-04-30 12:51:48 +10001446void
1447nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
1448{
1449 struct nouveau_fence *old_fence = NULL;
1450
1451 if (likely(fence))
1452 nouveau_fence_ref(fence);
1453
1454 spin_lock(&nvbo->bo.bdev->fence_lock);
1455 old_fence = nvbo->bo.sync_obj;
1456 nvbo->bo.sync_obj = fence;
1457 spin_unlock(&nvbo->bo.bdev->fence_lock);
1458
1459 nouveau_fence_unref(&old_fence);
1460}
1461
1462static void
1463nouveau_bo_fence_unref(void **sync_obj)
1464{
1465 nouveau_fence_unref((struct nouveau_fence **)sync_obj);
1466}
1467
1468static void *
1469nouveau_bo_fence_ref(void *sync_obj)
1470{
1471 return nouveau_fence_ref(sync_obj);
1472}
1473
1474static bool
1475nouveau_bo_fence_signalled(void *sync_obj, void *sync_arg)
1476{
Ben Skeggsd375e7d52012-04-30 13:30:00 +10001477 return nouveau_fence_done(sync_obj);
Ben Skeggs875ac342012-04-30 12:51:48 +10001478}
1479
1480static int
1481nouveau_bo_fence_wait(void *sync_obj, void *sync_arg, bool lazy, bool intr)
1482{
1483 return nouveau_fence_wait(sync_obj, lazy, intr);
1484}
1485
1486static int
1487nouveau_bo_fence_flush(void *sync_obj, void *sync_arg)
1488{
1489 return 0;
1490}
1491
Ben Skeggs6ee73862009-12-11 19:24:15 +10001492struct ttm_bo_driver nouveau_bo_driver = {
Jerome Glisse649bf3c2011-11-01 20:46:13 -04001493 .ttm_tt_create = &nouveau_ttm_tt_create,
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001494 .ttm_tt_populate = &nouveau_ttm_tt_populate,
1495 .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001496 .invalidate_caches = nouveau_bo_invalidate_caches,
1497 .init_mem_type = nouveau_bo_init_mem_type,
1498 .evict_flags = nouveau_bo_evict_flags,
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001499 .move_notify = nouveau_bo_move_ntfy,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001500 .move = nouveau_bo_move,
1501 .verify_access = nouveau_bo_verify_access,
Ben Skeggs875ac342012-04-30 12:51:48 +10001502 .sync_obj_signaled = nouveau_bo_fence_signalled,
1503 .sync_obj_wait = nouveau_bo_fence_wait,
1504 .sync_obj_flush = nouveau_bo_fence_flush,
1505 .sync_obj_unref = nouveau_bo_fence_unref,
1506 .sync_obj_ref = nouveau_bo_fence_ref,
Jerome Glissef32f02f2010-04-09 14:39:25 +02001507 .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
1508 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
1509 .io_mem_free = &nouveau_ttm_io_mem_free,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001510};
1511
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001512struct nouveau_vma *
1513nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nouveau_vm *vm)
1514{
1515 struct nouveau_vma *vma;
1516 list_for_each_entry(vma, &nvbo->vma_list, head) {
1517 if (vma->vm == vm)
1518 return vma;
1519 }
1520
1521 return NULL;
1522}
1523
1524int
1525nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm,
1526 struct nouveau_vma *vma)
1527{
1528 const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
1529 struct nouveau_mem *node = nvbo->bo.mem.mm_node;
1530 int ret;
1531
1532 ret = nouveau_vm_get(vm, size, nvbo->page_shift,
1533 NV_MEM_ACCESS_RW, vma);
1534 if (ret)
1535 return ret;
1536
1537 if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
1538 nouveau_vm_map(vma, nvbo->bo.mem.mm_node);
Dave Airlie22b33e82012-04-02 11:53:06 +01001539 else if (nvbo->bo.mem.mem_type == TTM_PL_TT) {
1540 if (node->sg)
1541 nouveau_vm_map_sg_table(vma, 0, size, node);
1542 else
1543 nouveau_vm_map_sg(vma, 0, size, node);
1544 }
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001545
1546 list_add_tail(&vma->head, &nvbo->vma_list);
Ben Skeggs2fd3db62011-06-07 15:25:12 +10001547 vma->refcount = 1;
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001548 return 0;
1549}
1550
1551void
1552nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
1553{
1554 if (vma->node) {
1555 if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM) {
1556 spin_lock(&nvbo->bo.bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001557 ttm_bo_wait(&nvbo->bo, false, false, false);
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001558 spin_unlock(&nvbo->bo.bdev->fence_lock);
1559 nouveau_vm_unmap(vma);
1560 }
1561
1562 nouveau_vm_put(vma);
1563 list_del(&vma->head);
1564 }
1565}