blob: bc28aeada8c306ddbab0fc7363ce4581af3cadac [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
30#include "drmP.h"
31
32#include "nouveau_drm.h"
33#include "nouveau_drv.h"
34#include "nouveau_dma.h"
Ben Skeggsf869ef82010-11-15 11:53:16 +100035#include "nouveau_mm.h"
36#include "nouveau_vm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100037
Maarten Maathuisa5106042009-12-26 21:46:36 +010038#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.h>
Maarten Maathuisa5106042009-12-26 21:46:36 +010040
Ben Skeggs6ee73862009-12-11 19:24:15 +100041static void
42nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
43{
44 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010045 struct drm_device *dev = dev_priv->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +100046 struct nouveau_bo *nvbo = nouveau_bo(bo);
47
Ben Skeggs6ee73862009-12-11 19:24:15 +100048 if (unlikely(nvbo->gem))
49 DRM_ERROR("bo %p still attached to GEM object\n", bo);
50
Francisco Jereza5cf68b2010-10-24 16:14:41 +020051 nv10_mem_put_tile_region(dev, nvbo->tile, NULL);
Ben Skeggs4c136142010-11-15 11:54:21 +100052 nouveau_vm_put(&nvbo->vma);
Ben Skeggs6ee73862009-12-11 19:24:15 +100053 kfree(nvbo);
54}
55
Francisco Jereza0af9ad2009-12-11 16:51:09 +010056static void
Ben Skeggsbfd83ac2010-11-12 15:12:51 +100057nouveau_bo_fixup_align(struct nouveau_bo *nvbo, int *align, int *size,
58 int *page_shift)
Francisco Jereza0af9ad2009-12-11 16:51:09 +010059{
Ben Skeggsbfd83ac2010-11-12 15:12:51 +100060 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010061
Ben Skeggs573a2a32010-08-25 15:26:04 +100062 if (dev_priv->card_type < NV_50) {
Ben Skeggsbfd83ac2010-11-12 15:12:51 +100063 if (nvbo->tile_mode) {
Francisco Jereza0af9ad2009-12-11 16:51:09 +010064 if (dev_priv->chipset >= 0x40) {
65 *align = 65536;
Ben Skeggsbfd83ac2010-11-12 15:12:51 +100066 *size = roundup(*size, 64 * nvbo->tile_mode);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010067
68 } else if (dev_priv->chipset >= 0x30) {
69 *align = 32768;
Ben Skeggsbfd83ac2010-11-12 15:12:51 +100070 *size = roundup(*size, 64 * nvbo->tile_mode);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010071
72 } else if (dev_priv->chipset >= 0x20) {
73 *align = 16384;
Ben Skeggsbfd83ac2010-11-12 15:12:51 +100074 *size = roundup(*size, 64 * nvbo->tile_mode);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010075
76 } else if (dev_priv->chipset >= 0x10) {
77 *align = 16384;
Ben Skeggsbfd83ac2010-11-12 15:12:51 +100078 *size = roundup(*size, 32 * nvbo->tile_mode);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010079 }
80 }
Ben Skeggsbfd83ac2010-11-12 15:12:51 +100081 } else {
82 if (likely(dev_priv->chan_vm)) {
83 if (*size > 256 * 1024)
84 *page_shift = dev_priv->chan_vm->lpg_shift;
85 else
86 *page_shift = dev_priv->chan_vm->spg_shift;
87 } else {
88 *page_shift = 12;
89 }
90
91 *size = roundup(*size, (1 << *page_shift));
92 *align = max((1 << *page_shift), *align);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010093 }
94
Maarten Maathuis1c7059e2009-12-25 18:51:17 +010095 *size = roundup(*size, PAGE_SIZE);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010096}
97
Ben Skeggs6ee73862009-12-11 19:24:15 +100098int
99nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
100 int size, int align, uint32_t flags, uint32_t tile_mode,
101 uint32_t tile_flags, bool no_vm, bool mappable,
102 struct nouveau_bo **pnvbo)
103{
104 struct drm_nouveau_private *dev_priv = dev->dev_private;
105 struct nouveau_bo *nvbo;
Ben Skeggsbfd83ac2010-11-12 15:12:51 +1000106 int ret = 0, page_shift = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000107
108 nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
109 if (!nvbo)
110 return -ENOMEM;
111 INIT_LIST_HEAD(&nvbo->head);
112 INIT_LIST_HEAD(&nvbo->entry);
113 nvbo->mappable = mappable;
114 nvbo->no_vm = no_vm;
115 nvbo->tile_mode = tile_mode;
116 nvbo->tile_flags = tile_flags;
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200117 nvbo->bo.bdev = &dev_priv->ttm.bdev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000118
Ben Skeggsbfd83ac2010-11-12 15:12:51 +1000119 nouveau_bo_fixup_align(nvbo, &align, &size, &page_shift);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000120 align >>= PAGE_SHIFT;
121
Ben Skeggs4c136142010-11-15 11:54:21 +1000122 if (!nvbo->no_vm && dev_priv->chan_vm) {
Ben Skeggsbfd83ac2010-11-12 15:12:51 +1000123 ret = nouveau_vm_get(dev_priv->chan_vm, size, page_shift,
Ben Skeggs4c136142010-11-15 11:54:21 +1000124 NV_MEM_ACCESS_RW, &nvbo->vma);
125 if (ret) {
126 kfree(nvbo);
127 return ret;
128 }
129 }
130
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100131 nouveau_bo_placement_set(nvbo, flags, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000132
133 nvbo->channel = chan;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000134 ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
135 ttm_bo_type_device, &nvbo->placement, align, 0,
136 false, NULL, size, nouveau_bo_del_ttm);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000137 if (ret) {
138 /* ttm will call nouveau_bo_del_ttm if it fails.. */
139 return ret;
140 }
Ben Skeggs90af89b2010-04-15 14:42:34 +1000141 nvbo->channel = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000142
Ben Skeggs4c136142010-11-15 11:54:21 +1000143 if (nvbo->vma.node) {
144 if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
145 nvbo->bo.offset = nvbo->vma.offset;
146 }
147
Ben Skeggs6ee73862009-12-11 19:24:15 +1000148 *pnvbo = nvbo;
149 return 0;
150}
151
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100152static void
153set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000154{
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100155 *n = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000156
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100157 if (type & TTM_PL_FLAG_VRAM)
158 pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
159 if (type & TTM_PL_FLAG_TT)
160 pl[(*n)++] = TTM_PL_FLAG_TT | flags;
161 if (type & TTM_PL_FLAG_SYSTEM)
162 pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
163}
Ben Skeggs37cb3e082009-12-16 16:22:42 +1000164
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200165static void
166set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
167{
168 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
169
170 if (dev_priv->card_type == NV_10 &&
171 nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM)) {
172 /*
173 * Make sure that the color and depth buffers are handled
174 * by independent memory controller units. Up to a 9x
175 * speed up when alpha-blending and depth-test are enabled
176 * at the same time.
177 */
178 int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
179
180 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
181 nvbo->placement.fpfn = vram_pages / 2;
182 nvbo->placement.lpfn = ~0;
183 } else {
184 nvbo->placement.fpfn = 0;
185 nvbo->placement.lpfn = vram_pages / 2;
186 }
187 }
188}
189
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100190void
191nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
192{
193 struct ttm_placement *pl = &nvbo->placement;
194 uint32_t flags = TTM_PL_MASK_CACHING |
195 (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
196
197 pl->placement = nvbo->placements;
198 set_placement_list(nvbo->placements, &pl->num_placement,
199 type, flags);
200
201 pl->busy_placement = nvbo->busy_placements;
202 set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
203 type | busy, flags);
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200204
205 set_placement_range(nvbo, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000206}
207
208int
209nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
210{
211 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
212 struct ttm_buffer_object *bo = &nvbo->bo;
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100213 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000214
215 if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
216 NV_ERROR(nouveau_bdev(bo->bdev)->dev,
217 "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
218 1 << bo->mem.mem_type, memtype);
219 return -EINVAL;
220 }
221
222 if (nvbo->pin_refcnt++)
223 return 0;
224
225 ret = ttm_bo_reserve(bo, false, false, false, 0);
226 if (ret)
227 goto out;
228
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100229 nouveau_bo_placement_set(nvbo, memtype, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000230
Ben Skeggs7a45d762010-11-22 08:50:27 +1000231 ret = nouveau_bo_validate(nvbo, false, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000232 if (ret == 0) {
233 switch (bo->mem.mem_type) {
234 case TTM_PL_VRAM:
235 dev_priv->fb_aper_free -= bo->mem.size;
236 break;
237 case TTM_PL_TT:
238 dev_priv->gart_info.aper_free -= bo->mem.size;
239 break;
240 default:
241 break;
242 }
243 }
244 ttm_bo_unreserve(bo);
245out:
246 if (unlikely(ret))
247 nvbo->pin_refcnt--;
248 return ret;
249}
250
251int
252nouveau_bo_unpin(struct nouveau_bo *nvbo)
253{
254 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
255 struct ttm_buffer_object *bo = &nvbo->bo;
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100256 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000257
258 if (--nvbo->pin_refcnt)
259 return 0;
260
261 ret = ttm_bo_reserve(bo, false, false, false, 0);
262 if (ret)
263 return ret;
264
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100265 nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000266
Ben Skeggs7a45d762010-11-22 08:50:27 +1000267 ret = nouveau_bo_validate(nvbo, false, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000268 if (ret == 0) {
269 switch (bo->mem.mem_type) {
270 case TTM_PL_VRAM:
271 dev_priv->fb_aper_free += bo->mem.size;
272 break;
273 case TTM_PL_TT:
274 dev_priv->gart_info.aper_free += bo->mem.size;
275 break;
276 default:
277 break;
278 }
279 }
280
281 ttm_bo_unreserve(bo);
282 return ret;
283}
284
285int
286nouveau_bo_map(struct nouveau_bo *nvbo)
287{
288 int ret;
289
290 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
291 if (ret)
292 return ret;
293
294 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
295 ttm_bo_unreserve(&nvbo->bo);
296 return ret;
297}
298
299void
300nouveau_bo_unmap(struct nouveau_bo *nvbo)
301{
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000302 if (nvbo)
303 ttm_bo_kunmap(&nvbo->kmap);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000304}
305
Ben Skeggs7a45d762010-11-22 08:50:27 +1000306int
307nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
308 bool no_wait_reserve, bool no_wait_gpu)
309{
310 int ret;
311
312 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
313 no_wait_reserve, no_wait_gpu);
314 if (ret)
315 return ret;
316
Ben Skeggs4c136142010-11-15 11:54:21 +1000317 if (nvbo->vma.node) {
318 if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
319 nvbo->bo.offset = nvbo->vma.offset;
320 }
321
Ben Skeggs7a45d762010-11-22 08:50:27 +1000322 return 0;
323}
324
Ben Skeggs6ee73862009-12-11 19:24:15 +1000325u16
326nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
327{
328 bool is_iomem;
329 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
330 mem = &mem[index];
331 if (is_iomem)
332 return ioread16_native((void __force __iomem *)mem);
333 else
334 return *mem;
335}
336
337void
338nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
339{
340 bool is_iomem;
341 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
342 mem = &mem[index];
343 if (is_iomem)
344 iowrite16_native(val, (void __force __iomem *)mem);
345 else
346 *mem = val;
347}
348
349u32
350nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
351{
352 bool is_iomem;
353 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
354 mem = &mem[index];
355 if (is_iomem)
356 return ioread32_native((void __force __iomem *)mem);
357 else
358 return *mem;
359}
360
361void
362nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
363{
364 bool is_iomem;
365 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
366 mem = &mem[index];
367 if (is_iomem)
368 iowrite32_native(val, (void __force __iomem *)mem);
369 else
370 *mem = val;
371}
372
373static struct ttm_backend *
374nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
375{
376 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
377 struct drm_device *dev = dev_priv->dev;
378
379 switch (dev_priv->gart_info.type) {
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000380#if __OS_HAS_AGP
Ben Skeggs6ee73862009-12-11 19:24:15 +1000381 case NOUVEAU_GART_AGP:
382 return ttm_agp_backend_init(bdev, dev->agp->bridge);
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000383#endif
Ben Skeggs6ee73862009-12-11 19:24:15 +1000384 case NOUVEAU_GART_SGDMA:
385 return nouveau_sgdma_init_ttm(dev);
386 default:
387 NV_ERROR(dev, "Unknown GART type %d\n",
388 dev_priv->gart_info.type);
389 break;
390 }
391
392 return NULL;
393}
394
395static int
396nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
397{
398 /* We'll do this from user space. */
399 return 0;
400}
401
402static int
403nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
404 struct ttm_mem_type_manager *man)
405{
406 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
407 struct drm_device *dev = dev_priv->dev;
408
409 switch (type) {
410 case TTM_PL_SYSTEM:
411 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
412 man->available_caching = TTM_PL_MASK_CACHING;
413 man->default_caching = TTM_PL_FLAG_CACHED;
414 break;
415 case TTM_PL_VRAM:
Ben Skeggsf869ef82010-11-15 11:53:16 +1000416 if (dev_priv->card_type == NV_50) {
Ben Skeggs573a2a32010-08-25 15:26:04 +1000417 man->func = &nouveau_vram_manager;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000418 man->io_reserve_fastpath = false;
419 man->use_io_reserve_lru = true;
420 } else {
Ben Skeggs573a2a32010-08-25 15:26:04 +1000421 man->func = &ttm_bo_manager_func;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000422 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000423 man->flags = TTM_MEMTYPE_FLAG_FIXED |
Jerome Glissef32f02f2010-04-09 14:39:25 +0200424 TTM_MEMTYPE_FLAG_MAPPABLE;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000425 man->available_caching = TTM_PL_FLAG_UNCACHED |
426 TTM_PL_FLAG_WC;
427 man->default_caching = TTM_PL_FLAG_WC;
Ben Skeggs4c136142010-11-15 11:54:21 +1000428 man->gpu_offset = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000429 break;
430 case TTM_PL_TT:
Ben Skeggsd961db72010-08-05 10:48:18 +1000431 man->func = &ttm_bo_manager_func;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000432 switch (dev_priv->gart_info.type) {
433 case NOUVEAU_GART_AGP:
Jerome Glissef32f02f2010-04-09 14:39:25 +0200434 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
Francisco Jereza3d487e2010-11-20 22:11:22 +0100435 man->available_caching = TTM_PL_FLAG_UNCACHED |
436 TTM_PL_FLAG_WC;
437 man->default_caching = TTM_PL_FLAG_WC;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000438 break;
439 case NOUVEAU_GART_SGDMA:
440 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
441 TTM_MEMTYPE_FLAG_CMA;
442 man->available_caching = TTM_PL_MASK_CACHING;
443 man->default_caching = TTM_PL_FLAG_CACHED;
444 break;
445 default:
446 NV_ERROR(dev, "Unknown GART type: %d\n",
447 dev_priv->gart_info.type);
448 return -EINVAL;
449 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000450 man->gpu_offset = dev_priv->vm_gart_base;
451 break;
452 default:
453 NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
454 return -EINVAL;
455 }
456 return 0;
457}
458
459static void
460nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
461{
462 struct nouveau_bo *nvbo = nouveau_bo(bo);
463
464 switch (bo->mem.mem_type) {
Francisco Jerez22fbd532009-12-11 18:40:17 +0100465 case TTM_PL_VRAM:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100466 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
467 TTM_PL_FLAG_SYSTEM);
Francisco Jerez22fbd532009-12-11 18:40:17 +0100468 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000469 default:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100470 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000471 break;
472 }
Francisco Jerez22fbd532009-12-11 18:40:17 +0100473
474 *pl = nvbo->placement;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000475}
476
477
478/* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
479 * TTM_PL_{VRAM,TT} directly.
480 */
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100481
Ben Skeggs6ee73862009-12-11 19:24:15 +1000482static int
483nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000484 struct nouveau_bo *nvbo, bool evict,
485 bool no_wait_reserve, bool no_wait_gpu,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000486 struct ttm_mem_reg *new_mem)
487{
488 struct nouveau_fence *fence = NULL;
489 int ret;
490
491 ret = nouveau_fence_new(chan, &fence, true);
492 if (ret)
493 return ret;
494
Francisco Jerez64798812010-09-21 19:02:01 +0200495 ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
Francisco Jerez311ab692010-07-04 12:54:23 +0200496 no_wait_reserve, no_wait_gpu, new_mem);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200497 nouveau_fence_unref(&fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000498 return ret;
499}
500
501static inline uint32_t
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000502nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
503 struct nouveau_channel *chan, struct ttm_mem_reg *mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000504{
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000505 struct nouveau_bo *nvbo = nouveau_bo(bo);
506
507 if (nvbo->no_vm) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000508 if (mem->mem_type == TTM_PL_TT)
509 return NvDmaGART;
510 return NvDmaVRAM;
511 }
512
513 if (mem->mem_type == TTM_PL_TT)
514 return chan->gart_handle;
515 return chan->vram_handle;
516}
517
518static int
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000519nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
520 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000521{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000522 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000523 struct nouveau_bo *nvbo = nouveau_bo(bo);
524 u64 length = (new_mem->num_pages << PAGE_SHIFT);
525 u64 src_offset, dst_offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000526 int ret;
527
Ben Skeggsd961db72010-08-05 10:48:18 +1000528 src_offset = old_mem->start << PAGE_SHIFT;
529 dst_offset = new_mem->start << PAGE_SHIFT;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000530 if (!nvbo->no_vm) {
531 if (old_mem->mem_type == TTM_PL_VRAM)
Ben Skeggs4c136142010-11-15 11:54:21 +1000532 src_offset = nvbo->vma.offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000533 else
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000534 src_offset += dev_priv->vm_gart_base;
535
536 if (new_mem->mem_type == TTM_PL_VRAM)
Ben Skeggs4c136142010-11-15 11:54:21 +1000537 dst_offset = nvbo->vma.offset;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000538 else
539 dst_offset += dev_priv->vm_gart_base;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000540 }
541
542 ret = RING_SPACE(chan, 3);
543 if (ret)
544 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000545
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000546 BEGIN_RING(chan, NvSubM2MF, 0x0184, 2);
547 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
548 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
549
550 while (length) {
551 u32 amount, stride, height;
552
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000553 amount = min(length, (u64)(4 * 1024 * 1024));
554 stride = 16 * 4;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000555 height = amount / stride;
556
Francisco Jerezf13b3262010-10-10 06:01:08 +0200557 if (new_mem->mem_type == TTM_PL_VRAM &&
558 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000559 ret = RING_SPACE(chan, 8);
560 if (ret)
561 return ret;
562
563 BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
564 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000565 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000566 OUT_RING (chan, stride);
567 OUT_RING (chan, height);
568 OUT_RING (chan, 1);
569 OUT_RING (chan, 0);
570 OUT_RING (chan, 0);
571 } else {
572 ret = RING_SPACE(chan, 2);
573 if (ret)
574 return ret;
575
576 BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
577 OUT_RING (chan, 1);
578 }
Francisco Jerezf13b3262010-10-10 06:01:08 +0200579 if (old_mem->mem_type == TTM_PL_VRAM &&
580 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000581 ret = RING_SPACE(chan, 8);
582 if (ret)
583 return ret;
584
585 BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
586 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000587 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000588 OUT_RING (chan, stride);
589 OUT_RING (chan, height);
590 OUT_RING (chan, 1);
591 OUT_RING (chan, 0);
592 OUT_RING (chan, 0);
593 } else {
594 ret = RING_SPACE(chan, 2);
595 if (ret)
596 return ret;
597
598 BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
599 OUT_RING (chan, 1);
600 }
601
602 ret = RING_SPACE(chan, 14);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000603 if (ret)
604 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000605
606 BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
607 OUT_RING (chan, upper_32_bits(src_offset));
608 OUT_RING (chan, upper_32_bits(dst_offset));
609 BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
610 OUT_RING (chan, lower_32_bits(src_offset));
611 OUT_RING (chan, lower_32_bits(dst_offset));
612 OUT_RING (chan, stride);
613 OUT_RING (chan, stride);
614 OUT_RING (chan, stride);
615 OUT_RING (chan, height);
616 OUT_RING (chan, 0x00000101);
617 OUT_RING (chan, 0x00000000);
618 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
619 OUT_RING (chan, 0);
620
621 length -= amount;
622 src_offset += amount;
623 dst_offset += amount;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000624 }
625
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000626 return 0;
627}
628
629static int
630nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
631 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
632{
Ben Skeggsd961db72010-08-05 10:48:18 +1000633 u32 src_offset = old_mem->start << PAGE_SHIFT;
634 u32 dst_offset = new_mem->start << PAGE_SHIFT;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000635 u32 page_count = new_mem->num_pages;
636 int ret;
637
638 ret = RING_SPACE(chan, 3);
639 if (ret)
640 return ret;
641
642 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
643 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
644 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
645
Ben Skeggs6ee73862009-12-11 19:24:15 +1000646 page_count = new_mem->num_pages;
647 while (page_count) {
648 int line_count = (page_count > 2047) ? 2047 : page_count;
649
Ben Skeggs6ee73862009-12-11 19:24:15 +1000650 ret = RING_SPACE(chan, 11);
651 if (ret)
652 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000653
Ben Skeggs6ee73862009-12-11 19:24:15 +1000654 BEGIN_RING(chan, NvSubM2MF,
655 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000656 OUT_RING (chan, src_offset);
657 OUT_RING (chan, dst_offset);
658 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
659 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
660 OUT_RING (chan, PAGE_SIZE); /* line_length */
661 OUT_RING (chan, line_count);
662 OUT_RING (chan, 0x00000101);
663 OUT_RING (chan, 0x00000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000664 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000665 OUT_RING (chan, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000666
667 page_count -= line_count;
668 src_offset += (PAGE_SIZE * line_count);
669 dst_offset += (PAGE_SIZE * line_count);
670 }
671
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000672 return 0;
673}
674
675static int
676nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
677 bool no_wait_reserve, bool no_wait_gpu,
678 struct ttm_mem_reg *new_mem)
679{
680 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
681 struct nouveau_bo *nvbo = nouveau_bo(bo);
682 struct nouveau_channel *chan;
683 int ret;
684
685 chan = nvbo->channel;
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000686 if (!chan || nvbo->no_vm) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000687 chan = dev_priv->channel;
Francisco Jereze419cf02010-10-25 23:38:59 +0200688 mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000689 }
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000690
691 if (dev_priv->card_type < NV_50)
692 ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
693 else
694 ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000695 if (ret == 0) {
696 ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
697 no_wait_reserve,
698 no_wait_gpu, new_mem);
699 }
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000700
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000701 if (chan == dev_priv->channel)
702 mutex_unlock(&chan->mutex);
703 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000704}
705
706static int
707nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000708 bool no_wait_reserve, bool no_wait_gpu,
709 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000710{
711 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
712 struct ttm_placement placement;
713 struct ttm_mem_reg tmp_mem;
714 int ret;
715
716 placement.fpfn = placement.lpfn = 0;
717 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +0100718 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000719
720 tmp_mem = *new_mem;
721 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000722 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000723 if (ret)
724 return ret;
725
726 ret = ttm_tt_bind(bo->ttm, &tmp_mem);
727 if (ret)
728 goto out;
729
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000730 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000731 if (ret)
732 goto out;
733
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000734 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000735out:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000736 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000737 return ret;
738}
739
740static int
741nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000742 bool no_wait_reserve, bool no_wait_gpu,
743 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000744{
745 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
746 struct ttm_placement placement;
747 struct ttm_mem_reg tmp_mem;
748 int ret;
749
750 placement.fpfn = placement.lpfn = 0;
751 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +0100752 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000753
754 tmp_mem = *new_mem;
755 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000756 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000757 if (ret)
758 return ret;
759
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000760 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000761 if (ret)
762 goto out;
763
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000764 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000765 if (ret)
766 goto out;
767
768out:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000769 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000770 return ret;
771}
772
773static int
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100774nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
775 struct nouveau_tile_reg **new_tile)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000776{
777 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000778 struct drm_device *dev = dev_priv->dev;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100779 struct nouveau_bo *nvbo = nouveau_bo(bo);
780 uint64_t offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000781
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100782 if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) {
783 /* Nothing to do. */
784 *new_tile = NULL;
785 return 0;
786 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000787
Ben Skeggsd961db72010-08-05 10:48:18 +1000788 offset = new_mem->start << PAGE_SHIFT;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100789
Ben Skeggs4c136142010-11-15 11:54:21 +1000790 if (dev_priv->chan_vm) {
791 nouveau_vm_map(&nvbo->vma, new_mem->mm_node);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100792 } else if (dev_priv->card_type >= NV_10) {
793 *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200794 nvbo->tile_mode,
795 nvbo->tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000796 }
797
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100798 return 0;
799}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000800
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100801static void
802nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
803 struct nouveau_tile_reg *new_tile,
804 struct nouveau_tile_reg **old_tile)
805{
806 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
807 struct drm_device *dev = dev_priv->dev;
808
809 if (dev_priv->card_type >= NV_10 &&
810 dev_priv->card_type < NV_50) {
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200811 nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100812 *old_tile = new_tile;
813 }
814}
815
816static int
817nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000818 bool no_wait_reserve, bool no_wait_gpu,
819 struct ttm_mem_reg *new_mem)
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100820{
821 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
822 struct nouveau_bo *nvbo = nouveau_bo(bo);
823 struct ttm_mem_reg *old_mem = &bo->mem;
824 struct nouveau_tile_reg *new_tile = NULL;
825 int ret = 0;
826
827 ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
828 if (ret)
829 return ret;
830
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100831 /* Fake bo copy. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000832 if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
833 BUG_ON(bo->mem.mm_node != NULL);
834 bo->mem = *new_mem;
835 new_mem->mm_node = NULL;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100836 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000837 }
838
Ben Skeggsb8a6a802010-08-27 11:55:43 +1000839 /* Software copy if the card isn't up and running yet. */
840 if (!dev_priv->channel) {
841 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
842 goto out;
843 }
844
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100845 /* Hardware assisted copy. */
846 if (new_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000847 ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100848 else if (old_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000849 ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100850 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000851 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000852
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100853 if (!ret)
854 goto out;
855
856 /* Fallback to software copy. */
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000857 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100858
859out:
860 if (ret)
861 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
862 else
863 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
864
865 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000866}
867
868static int
869nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
870{
871 return 0;
872}
873
Jerome Glissef32f02f2010-04-09 14:39:25 +0200874static int
875nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
876{
877 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
878 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
879 struct drm_device *dev = dev_priv->dev;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000880 int ret;
Jerome Glissef32f02f2010-04-09 14:39:25 +0200881
882 mem->bus.addr = NULL;
883 mem->bus.offset = 0;
884 mem->bus.size = mem->num_pages << PAGE_SHIFT;
885 mem->bus.base = 0;
886 mem->bus.is_iomem = false;
887 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
888 return -EINVAL;
889 switch (mem->mem_type) {
890 case TTM_PL_SYSTEM:
891 /* System memory */
892 return 0;
893 case TTM_PL_TT:
894#if __OS_HAS_AGP
895 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000896 mem->bus.offset = mem->start << PAGE_SHIFT;
Jerome Glissef32f02f2010-04-09 14:39:25 +0200897 mem->bus.base = dev_priv->gart_info.aper_base;
898 mem->bus.is_iomem = true;
899 }
900#endif
901 break;
902 case TTM_PL_VRAM:
Ben Skeggsf869ef82010-11-15 11:53:16 +1000903 {
904 struct nouveau_vram *vram = mem->mm_node;
905
906 if (!dev_priv->bar1_vm) {
907 mem->bus.offset = mem->start << PAGE_SHIFT;
908 mem->bus.base = pci_resource_start(dev->pdev, 1);
909 mem->bus.is_iomem = true;
910 break;
911 }
912
913 ret = nouveau_vm_get(dev_priv->bar1_vm, mem->bus.size, 12,
914 NV_MEM_ACCESS_RW, &vram->bar_vma);
915 if (ret)
916 return ret;
917
918 nouveau_vm_map(&vram->bar_vma, vram);
919 if (ret) {
920 nouveau_vm_put(&vram->bar_vma);
921 return ret;
922 }
923
924 mem->bus.offset = vram->bar_vma.offset;
925 mem->bus.offset -= 0x0020000000ULL;
Jordan Crouse01d73a62010-05-27 13:40:24 -0600926 mem->bus.base = pci_resource_start(dev->pdev, 1);
Jerome Glissef32f02f2010-04-09 14:39:25 +0200927 mem->bus.is_iomem = true;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000928 }
Jerome Glissef32f02f2010-04-09 14:39:25 +0200929 break;
930 default:
931 return -EINVAL;
932 }
933 return 0;
934}
935
936static void
937nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
938{
Ben Skeggsf869ef82010-11-15 11:53:16 +1000939 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
940 struct nouveau_vram *vram = mem->mm_node;
941
942 if (!dev_priv->bar1_vm || mem->mem_type != TTM_PL_VRAM)
943 return;
944
945 if (!vram->bar_vma.node)
946 return;
947
948 nouveau_vm_unmap(&vram->bar_vma);
949 nouveau_vm_put(&vram->bar_vma);
Jerome Glissef32f02f2010-04-09 14:39:25 +0200950}
951
952static int
953nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
954{
Ben Skeggse1429b42010-09-10 11:12:25 +1000955 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
956 struct nouveau_bo *nvbo = nouveau_bo(bo);
957
958 /* as long as the bo isn't in vram, and isn't tiled, we've got
959 * nothing to do here.
960 */
961 if (bo->mem.mem_type != TTM_PL_VRAM) {
Francisco Jerezf13b3262010-10-10 06:01:08 +0200962 if (dev_priv->card_type < NV_50 ||
963 !nouveau_bo_tile_layout(nvbo))
Ben Skeggse1429b42010-09-10 11:12:25 +1000964 return 0;
965 }
966
967 /* make sure bo is in mappable vram */
Ben Skeggsd961db72010-08-05 10:48:18 +1000968 if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
Ben Skeggse1429b42010-09-10 11:12:25 +1000969 return 0;
970
971
972 nvbo->placement.fpfn = 0;
973 nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
974 nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
Ben Skeggs7a45d762010-11-22 08:50:27 +1000975 return nouveau_bo_validate(nvbo, false, true, false);
Jerome Glissef32f02f2010-04-09 14:39:25 +0200976}
977
Francisco Jerez332b2422010-10-20 23:35:40 +0200978void
979nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
980{
Francisco Jerez23c45e82010-10-28 23:10:29 +0200981 struct nouveau_fence *old_fence;
Francisco Jerez332b2422010-10-20 23:35:40 +0200982
983 if (likely(fence))
Francisco Jerez23c45e82010-10-28 23:10:29 +0200984 nouveau_fence_ref(fence);
Francisco Jerez332b2422010-10-20 23:35:40 +0200985
Francisco Jerez23c45e82010-10-28 23:10:29 +0200986 spin_lock(&nvbo->bo.bdev->fence_lock);
987 old_fence = nvbo->bo.sync_obj;
988 nvbo->bo.sync_obj = fence;
Francisco Jerez332b2422010-10-20 23:35:40 +0200989 spin_unlock(&nvbo->bo.bdev->fence_lock);
Francisco Jerez23c45e82010-10-28 23:10:29 +0200990
991 nouveau_fence_unref(&old_fence);
Francisco Jerez332b2422010-10-20 23:35:40 +0200992}
993
Ben Skeggs6ee73862009-12-11 19:24:15 +1000994struct ttm_bo_driver nouveau_bo_driver = {
995 .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
996 .invalidate_caches = nouveau_bo_invalidate_caches,
997 .init_mem_type = nouveau_bo_init_mem_type,
998 .evict_flags = nouveau_bo_evict_flags,
999 .move = nouveau_bo_move,
1000 .verify_access = nouveau_bo_verify_access,
Marcin Slusarz382d62e2010-10-20 21:50:24 +02001001 .sync_obj_signaled = __nouveau_fence_signalled,
1002 .sync_obj_wait = __nouveau_fence_wait,
1003 .sync_obj_flush = __nouveau_fence_flush,
1004 .sync_obj_unref = __nouveau_fence_unref,
1005 .sync_obj_ref = __nouveau_fence_ref,
Jerome Glissef32f02f2010-04-09 14:39:25 +02001006 .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
1007 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
1008 .io_mem_free = &nouveau_ttm_io_mem_free,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001009};
1010