blob: f4ee43db00aaf2db1a08e0b023ffb98e839de54e [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"
35
Maarten Maathuisa5106042009-12-26 21:46:36 +010036#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Maarten Maathuisa5106042009-12-26 21:46:36 +010038
Ben Skeggs6ee73862009-12-11 19:24:15 +100039static void
40nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
41{
42 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010043 struct drm_device *dev = dev_priv->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +100044 struct nouveau_bo *nvbo = nouveau_bo(bo);
45
Ben Skeggs6ee73862009-12-11 19:24:15 +100046 if (unlikely(nvbo->gem))
47 DRM_ERROR("bo %p still attached to GEM object\n", bo);
48
Francisco Jereza5cf68b2010-10-24 16:14:41 +020049 nv10_mem_put_tile_region(dev, nvbo->tile, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +100050 kfree(nvbo);
51}
52
Francisco Jereza0af9ad2009-12-11 16:51:09 +010053static void
54nouveau_bo_fixup_align(struct drm_device *dev,
55 uint32_t tile_mode, uint32_t tile_flags,
56 int *align, int *size)
57{
58 struct drm_nouveau_private *dev_priv = dev->dev_private;
59
60 /*
61 * Some of the tile_flags have a periodic structure of N*4096 bytes,
Maarten Maathuiseb1dba02009-12-27 12:22:07 +010062 * align to to that as well as the page size. Align the size to the
63 * appropriate boundaries. This does imply that sizes are rounded up
64 * 3-7 pages, so be aware of this and do not waste memory by allocating
65 * many small buffers.
Francisco Jereza0af9ad2009-12-11 16:51:09 +010066 */
67 if (dev_priv->card_type == NV_50) {
Ben Skeggsa76fb4e2010-03-18 09:45:20 +100068 uint32_t block_size = dev_priv->vram_size >> 15;
Maarten Maathuisa5106042009-12-26 21:46:36 +010069 int i;
70
Francisco Jereza0af9ad2009-12-11 16:51:09 +010071 switch (tile_flags) {
72 case 0x1800:
73 case 0x2800:
74 case 0x4800:
75 case 0x7a00:
Maarten Maathuisa5106042009-12-26 21:46:36 +010076 if (is_power_of_2(block_size)) {
Maarten Maathuisa5106042009-12-26 21:46:36 +010077 for (i = 1; i < 10; i++) {
78 *align = 12 * i * block_size;
79 if (!(*align % 65536))
80 break;
81 }
Francisco Jereza0af9ad2009-12-11 16:51:09 +010082 } else {
Maarten Maathuisa5106042009-12-26 21:46:36 +010083 for (i = 1; i < 10; i++) {
84 *align = 8 * i * block_size;
85 if (!(*align % 65536))
86 break;
87 }
Francisco Jereza0af9ad2009-12-11 16:51:09 +010088 }
Maarten Maathuiseb1dba02009-12-27 12:22:07 +010089 *size = roundup(*size, *align);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010090 break;
91 default:
92 break;
93 }
94
95 } else {
96 if (tile_mode) {
97 if (dev_priv->chipset >= 0x40) {
98 *align = 65536;
99 *size = roundup(*size, 64 * tile_mode);
100
101 } else if (dev_priv->chipset >= 0x30) {
102 *align = 32768;
103 *size = roundup(*size, 64 * tile_mode);
104
105 } else if (dev_priv->chipset >= 0x20) {
106 *align = 16384;
107 *size = roundup(*size, 64 * tile_mode);
108
109 } else if (dev_priv->chipset >= 0x10) {
110 *align = 16384;
111 *size = roundup(*size, 32 * tile_mode);
112 }
113 }
114 }
115
Maarten Maathuis1c7059e2009-12-25 18:51:17 +0100116 /* ALIGN works only on powers of two. */
117 *size = roundup(*size, PAGE_SIZE);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100118
119 if (dev_priv->card_type == NV_50) {
Maarten Maathuis1c7059e2009-12-25 18:51:17 +0100120 *size = roundup(*size, 65536);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100121 *align = max(65536, *align);
122 }
123}
124
Ben Skeggs6ee73862009-12-11 19:24:15 +1000125int
126nouveau_bo_new(struct drm_device *dev, struct nouveau_channel *chan,
127 int size, int align, uint32_t flags, uint32_t tile_mode,
128 uint32_t tile_flags, bool no_vm, bool mappable,
129 struct nouveau_bo **pnvbo)
130{
131 struct drm_nouveau_private *dev_priv = dev->dev_private;
132 struct nouveau_bo *nvbo;
Francisco Jerez8dea4a12009-12-16 19:03:28 +0100133 int ret = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000134
135 nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
136 if (!nvbo)
137 return -ENOMEM;
138 INIT_LIST_HEAD(&nvbo->head);
139 INIT_LIST_HEAD(&nvbo->entry);
140 nvbo->mappable = mappable;
141 nvbo->no_vm = no_vm;
142 nvbo->tile_mode = tile_mode;
143 nvbo->tile_flags = tile_flags;
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200144 nvbo->bo.bdev = &dev_priv->ttm.bdev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000145
Francisco Jerezf13b3262010-10-10 06:01:08 +0200146 nouveau_bo_fixup_align(dev, tile_mode, nouveau_bo_tile_layout(nvbo),
147 &align, &size);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000148 align >>= PAGE_SHIFT;
149
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100150 nouveau_bo_placement_set(nvbo, flags, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000151
152 nvbo->channel = chan;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000153 ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
154 ttm_bo_type_device, &nvbo->placement, align, 0,
155 false, NULL, size, nouveau_bo_del_ttm);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000156 if (ret) {
157 /* ttm will call nouveau_bo_del_ttm if it fails.. */
158 return ret;
159 }
Ben Skeggs90af89b2010-04-15 14:42:34 +1000160 nvbo->channel = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000161
Ben Skeggs6ee73862009-12-11 19:24:15 +1000162 *pnvbo = nvbo;
163 return 0;
164}
165
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100166static void
167set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000168{
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100169 *n = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000170
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100171 if (type & TTM_PL_FLAG_VRAM)
172 pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
173 if (type & TTM_PL_FLAG_TT)
174 pl[(*n)++] = TTM_PL_FLAG_TT | flags;
175 if (type & TTM_PL_FLAG_SYSTEM)
176 pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
177}
Ben Skeggs37cb3e082009-12-16 16:22:42 +1000178
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200179static void
180set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
181{
182 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
183
184 if (dev_priv->card_type == NV_10 &&
185 nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM)) {
186 /*
187 * Make sure that the color and depth buffers are handled
188 * by independent memory controller units. Up to a 9x
189 * speed up when alpha-blending and depth-test are enabled
190 * at the same time.
191 */
192 int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
193
194 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
195 nvbo->placement.fpfn = vram_pages / 2;
196 nvbo->placement.lpfn = ~0;
197 } else {
198 nvbo->placement.fpfn = 0;
199 nvbo->placement.lpfn = vram_pages / 2;
200 }
201 }
202}
203
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100204void
205nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
206{
207 struct ttm_placement *pl = &nvbo->placement;
208 uint32_t flags = TTM_PL_MASK_CACHING |
209 (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
210
211 pl->placement = nvbo->placements;
212 set_placement_list(nvbo->placements, &pl->num_placement,
213 type, flags);
214
215 pl->busy_placement = nvbo->busy_placements;
216 set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
217 type | busy, flags);
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200218
219 set_placement_range(nvbo, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000220}
221
222int
223nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
224{
225 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
226 struct ttm_buffer_object *bo = &nvbo->bo;
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100227 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000228
229 if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
230 NV_ERROR(nouveau_bdev(bo->bdev)->dev,
231 "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
232 1 << bo->mem.mem_type, memtype);
233 return -EINVAL;
234 }
235
236 if (nvbo->pin_refcnt++)
237 return 0;
238
239 ret = ttm_bo_reserve(bo, false, false, false, 0);
240 if (ret)
241 goto out;
242
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100243 nouveau_bo_placement_set(nvbo, memtype, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000244
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000245 ret = ttm_bo_validate(bo, &nvbo->placement, false, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000246 if (ret == 0) {
247 switch (bo->mem.mem_type) {
248 case TTM_PL_VRAM:
249 dev_priv->fb_aper_free -= bo->mem.size;
250 break;
251 case TTM_PL_TT:
252 dev_priv->gart_info.aper_free -= bo->mem.size;
253 break;
254 default:
255 break;
256 }
257 }
258 ttm_bo_unreserve(bo);
259out:
260 if (unlikely(ret))
261 nvbo->pin_refcnt--;
262 return ret;
263}
264
265int
266nouveau_bo_unpin(struct nouveau_bo *nvbo)
267{
268 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
269 struct ttm_buffer_object *bo = &nvbo->bo;
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100270 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000271
272 if (--nvbo->pin_refcnt)
273 return 0;
274
275 ret = ttm_bo_reserve(bo, false, false, false, 0);
276 if (ret)
277 return ret;
278
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100279 nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000280
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000281 ret = ttm_bo_validate(bo, &nvbo->placement, false, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000282 if (ret == 0) {
283 switch (bo->mem.mem_type) {
284 case TTM_PL_VRAM:
285 dev_priv->fb_aper_free += bo->mem.size;
286 break;
287 case TTM_PL_TT:
288 dev_priv->gart_info.aper_free += bo->mem.size;
289 break;
290 default:
291 break;
292 }
293 }
294
295 ttm_bo_unreserve(bo);
296 return ret;
297}
298
299int
300nouveau_bo_map(struct nouveau_bo *nvbo)
301{
302 int ret;
303
304 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
305 if (ret)
306 return ret;
307
308 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
309 ttm_bo_unreserve(&nvbo->bo);
310 return ret;
311}
312
313void
314nouveau_bo_unmap(struct nouveau_bo *nvbo)
315{
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000316 if (nvbo)
317 ttm_bo_kunmap(&nvbo->kmap);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000318}
319
320u16
321nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
322{
323 bool is_iomem;
324 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
325 mem = &mem[index];
326 if (is_iomem)
327 return ioread16_native((void __force __iomem *)mem);
328 else
329 return *mem;
330}
331
332void
333nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
334{
335 bool is_iomem;
336 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
337 mem = &mem[index];
338 if (is_iomem)
339 iowrite16_native(val, (void __force __iomem *)mem);
340 else
341 *mem = val;
342}
343
344u32
345nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
346{
347 bool is_iomem;
348 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
349 mem = &mem[index];
350 if (is_iomem)
351 return ioread32_native((void __force __iomem *)mem);
352 else
353 return *mem;
354}
355
356void
357nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
358{
359 bool is_iomem;
360 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
361 mem = &mem[index];
362 if (is_iomem)
363 iowrite32_native(val, (void __force __iomem *)mem);
364 else
365 *mem = val;
366}
367
368static struct ttm_backend *
369nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
370{
371 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
372 struct drm_device *dev = dev_priv->dev;
373
374 switch (dev_priv->gart_info.type) {
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000375#if __OS_HAS_AGP
Ben Skeggs6ee73862009-12-11 19:24:15 +1000376 case NOUVEAU_GART_AGP:
377 return ttm_agp_backend_init(bdev, dev->agp->bridge);
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000378#endif
Ben Skeggs6ee73862009-12-11 19:24:15 +1000379 case NOUVEAU_GART_SGDMA:
380 return nouveau_sgdma_init_ttm(dev);
381 default:
382 NV_ERROR(dev, "Unknown GART type %d\n",
383 dev_priv->gart_info.type);
384 break;
385 }
386
387 return NULL;
388}
389
390static int
391nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
392{
393 /* We'll do this from user space. */
394 return 0;
395}
396
397static int
398nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
399 struct ttm_mem_type_manager *man)
400{
401 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
402 struct drm_device *dev = dev_priv->dev;
403
404 switch (type) {
405 case TTM_PL_SYSTEM:
406 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
407 man->available_caching = TTM_PL_MASK_CACHING;
408 man->default_caching = TTM_PL_FLAG_CACHED;
409 break;
410 case TTM_PL_VRAM:
Ben Skeggsd961db72010-08-05 10:48:18 +1000411 man->func = &ttm_bo_manager_func;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000412 man->flags = TTM_MEMTYPE_FLAG_FIXED |
Jerome Glissef32f02f2010-04-09 14:39:25 +0200413 TTM_MEMTYPE_FLAG_MAPPABLE;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000414 man->available_caching = TTM_PL_FLAG_UNCACHED |
415 TTM_PL_FLAG_WC;
416 man->default_caching = TTM_PL_FLAG_WC;
Ben Skeggsfbd28952010-09-01 15:24:34 +1000417 if (dev_priv->card_type == NV_50)
418 man->gpu_offset = 0x40000000;
419 else
420 man->gpu_offset = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000421 break;
422 case TTM_PL_TT:
Ben Skeggsd961db72010-08-05 10:48:18 +1000423 man->func = &ttm_bo_manager_func;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000424 switch (dev_priv->gart_info.type) {
425 case NOUVEAU_GART_AGP:
Jerome Glissef32f02f2010-04-09 14:39:25 +0200426 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000427 man->available_caching = TTM_PL_FLAG_UNCACHED;
428 man->default_caching = TTM_PL_FLAG_UNCACHED;
429 break;
430 case NOUVEAU_GART_SGDMA:
431 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
432 TTM_MEMTYPE_FLAG_CMA;
433 man->available_caching = TTM_PL_MASK_CACHING;
434 man->default_caching = TTM_PL_FLAG_CACHED;
435 break;
436 default:
437 NV_ERROR(dev, "Unknown GART type: %d\n",
438 dev_priv->gart_info.type);
439 return -EINVAL;
440 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000441 man->gpu_offset = dev_priv->vm_gart_base;
442 break;
443 default:
444 NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
445 return -EINVAL;
446 }
447 return 0;
448}
449
450static void
451nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
452{
453 struct nouveau_bo *nvbo = nouveau_bo(bo);
454
455 switch (bo->mem.mem_type) {
Francisco Jerez22fbd532009-12-11 18:40:17 +0100456 case TTM_PL_VRAM:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100457 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
458 TTM_PL_FLAG_SYSTEM);
Francisco Jerez22fbd532009-12-11 18:40:17 +0100459 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000460 default:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100461 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000462 break;
463 }
Francisco Jerez22fbd532009-12-11 18:40:17 +0100464
465 *pl = nvbo->placement;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000466}
467
468
469/* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
470 * TTM_PL_{VRAM,TT} directly.
471 */
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100472
Ben Skeggs6ee73862009-12-11 19:24:15 +1000473static int
474nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000475 struct nouveau_bo *nvbo, bool evict,
476 bool no_wait_reserve, bool no_wait_gpu,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000477 struct ttm_mem_reg *new_mem)
478{
479 struct nouveau_fence *fence = NULL;
480 int ret;
481
482 ret = nouveau_fence_new(chan, &fence, true);
483 if (ret)
484 return ret;
485
Francisco Jerez64798812010-09-21 19:02:01 +0200486 ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
Francisco Jerez311ab692010-07-04 12:54:23 +0200487 no_wait_reserve, no_wait_gpu, new_mem);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200488 nouveau_fence_unref(&fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000489 return ret;
490}
491
492static inline uint32_t
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000493nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
494 struct nouveau_channel *chan, struct ttm_mem_reg *mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000495{
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000496 struct nouveau_bo *nvbo = nouveau_bo(bo);
497
498 if (nvbo->no_vm) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000499 if (mem->mem_type == TTM_PL_TT)
500 return NvDmaGART;
501 return NvDmaVRAM;
502 }
503
504 if (mem->mem_type == TTM_PL_TT)
505 return chan->gart_handle;
506 return chan->vram_handle;
507}
508
509static int
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000510nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
511 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000512{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000513 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000514 struct nouveau_bo *nvbo = nouveau_bo(bo);
515 u64 length = (new_mem->num_pages << PAGE_SHIFT);
516 u64 src_offset, dst_offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000517 int ret;
518
Ben Skeggsd961db72010-08-05 10:48:18 +1000519 src_offset = old_mem->start << PAGE_SHIFT;
520 dst_offset = new_mem->start << PAGE_SHIFT;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000521 if (!nvbo->no_vm) {
522 if (old_mem->mem_type == TTM_PL_VRAM)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000523 src_offset += dev_priv->vm_vram_base;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000524 else
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000525 src_offset += dev_priv->vm_gart_base;
526
527 if (new_mem->mem_type == TTM_PL_VRAM)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000528 dst_offset += dev_priv->vm_vram_base;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000529 else
530 dst_offset += dev_priv->vm_gart_base;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000531 }
532
533 ret = RING_SPACE(chan, 3);
534 if (ret)
535 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000536
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000537 BEGIN_RING(chan, NvSubM2MF, 0x0184, 2);
538 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
539 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
540
541 while (length) {
542 u32 amount, stride, height;
543
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000544 amount = min(length, (u64)(4 * 1024 * 1024));
545 stride = 16 * 4;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000546 height = amount / stride;
547
Francisco Jerezf13b3262010-10-10 06:01:08 +0200548 if (new_mem->mem_type == TTM_PL_VRAM &&
549 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000550 ret = RING_SPACE(chan, 8);
551 if (ret)
552 return ret;
553
554 BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
555 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000556 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000557 OUT_RING (chan, stride);
558 OUT_RING (chan, height);
559 OUT_RING (chan, 1);
560 OUT_RING (chan, 0);
561 OUT_RING (chan, 0);
562 } else {
563 ret = RING_SPACE(chan, 2);
564 if (ret)
565 return ret;
566
567 BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
568 OUT_RING (chan, 1);
569 }
Francisco Jerezf13b3262010-10-10 06:01:08 +0200570 if (old_mem->mem_type == TTM_PL_VRAM &&
571 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000572 ret = RING_SPACE(chan, 8);
573 if (ret)
574 return ret;
575
576 BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
577 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000578 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000579 OUT_RING (chan, stride);
580 OUT_RING (chan, height);
581 OUT_RING (chan, 1);
582 OUT_RING (chan, 0);
583 OUT_RING (chan, 0);
584 } else {
585 ret = RING_SPACE(chan, 2);
586 if (ret)
587 return ret;
588
589 BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
590 OUT_RING (chan, 1);
591 }
592
593 ret = RING_SPACE(chan, 14);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000594 if (ret)
595 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000596
597 BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
598 OUT_RING (chan, upper_32_bits(src_offset));
599 OUT_RING (chan, upper_32_bits(dst_offset));
600 BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
601 OUT_RING (chan, lower_32_bits(src_offset));
602 OUT_RING (chan, lower_32_bits(dst_offset));
603 OUT_RING (chan, stride);
604 OUT_RING (chan, stride);
605 OUT_RING (chan, stride);
606 OUT_RING (chan, height);
607 OUT_RING (chan, 0x00000101);
608 OUT_RING (chan, 0x00000000);
609 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
610 OUT_RING (chan, 0);
611
612 length -= amount;
613 src_offset += amount;
614 dst_offset += amount;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000615 }
616
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000617 return 0;
618}
619
620static int
621nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
622 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
623{
Ben Skeggsd961db72010-08-05 10:48:18 +1000624 u32 src_offset = old_mem->start << PAGE_SHIFT;
625 u32 dst_offset = new_mem->start << PAGE_SHIFT;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000626 u32 page_count = new_mem->num_pages;
627 int ret;
628
629 ret = RING_SPACE(chan, 3);
630 if (ret)
631 return ret;
632
633 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
634 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
635 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
636
Ben Skeggs6ee73862009-12-11 19:24:15 +1000637 page_count = new_mem->num_pages;
638 while (page_count) {
639 int line_count = (page_count > 2047) ? 2047 : page_count;
640
Ben Skeggs6ee73862009-12-11 19:24:15 +1000641 ret = RING_SPACE(chan, 11);
642 if (ret)
643 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000644
Ben Skeggs6ee73862009-12-11 19:24:15 +1000645 BEGIN_RING(chan, NvSubM2MF,
646 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000647 OUT_RING (chan, src_offset);
648 OUT_RING (chan, dst_offset);
649 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
650 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
651 OUT_RING (chan, PAGE_SIZE); /* line_length */
652 OUT_RING (chan, line_count);
653 OUT_RING (chan, 0x00000101);
654 OUT_RING (chan, 0x00000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000655 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000656 OUT_RING (chan, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000657
658 page_count -= line_count;
659 src_offset += (PAGE_SIZE * line_count);
660 dst_offset += (PAGE_SIZE * line_count);
661 }
662
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000663 return 0;
664}
665
666static int
667nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
668 bool no_wait_reserve, bool no_wait_gpu,
669 struct ttm_mem_reg *new_mem)
670{
671 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
672 struct nouveau_bo *nvbo = nouveau_bo(bo);
673 struct nouveau_channel *chan;
674 int ret;
675
676 chan = nvbo->channel;
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000677 if (!chan || nvbo->no_vm) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000678 chan = dev_priv->channel;
Francisco Jereze419cf02010-10-25 23:38:59 +0200679 mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000680 }
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000681
682 if (dev_priv->card_type < NV_50)
683 ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
684 else
685 ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000686 if (ret == 0) {
687 ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
688 no_wait_reserve,
689 no_wait_gpu, new_mem);
690 }
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000691
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000692 if (chan == dev_priv->channel)
693 mutex_unlock(&chan->mutex);
694 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000695}
696
697static int
698nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000699 bool no_wait_reserve, bool no_wait_gpu,
700 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000701{
702 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
703 struct ttm_placement placement;
704 struct ttm_mem_reg tmp_mem;
705 int ret;
706
707 placement.fpfn = placement.lpfn = 0;
708 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +0100709 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000710
711 tmp_mem = *new_mem;
712 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000713 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000714 if (ret)
715 return ret;
716
717 ret = ttm_tt_bind(bo->ttm, &tmp_mem);
718 if (ret)
719 goto out;
720
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000721 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000722 if (ret)
723 goto out;
724
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000725 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000726out:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000727 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000728 return ret;
729}
730
731static int
732nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000733 bool no_wait_reserve, bool no_wait_gpu,
734 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000735{
736 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
737 struct ttm_placement placement;
738 struct ttm_mem_reg tmp_mem;
739 int ret;
740
741 placement.fpfn = placement.lpfn = 0;
742 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +0100743 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000744
745 tmp_mem = *new_mem;
746 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000747 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000748 if (ret)
749 return ret;
750
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000751 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000752 if (ret)
753 goto out;
754
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000755 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000756 if (ret)
757 goto out;
758
759out:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000760 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000761 return ret;
762}
763
764static int
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100765nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
766 struct nouveau_tile_reg **new_tile)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000767{
768 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000769 struct drm_device *dev = dev_priv->dev;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100770 struct nouveau_bo *nvbo = nouveau_bo(bo);
771 uint64_t offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000772 int ret;
773
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100774 if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) {
775 /* Nothing to do. */
776 *new_tile = NULL;
777 return 0;
778 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000779
Ben Skeggsd961db72010-08-05 10:48:18 +1000780 offset = new_mem->start << PAGE_SHIFT;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100781
782 if (dev_priv->card_type == NV_50) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000783 ret = nv50_mem_vm_bind_linear(dev,
784 offset + dev_priv->vm_vram_base,
Francisco Jerezf13b3262010-10-10 06:01:08 +0200785 new_mem->size,
786 nouveau_bo_tile_layout(nvbo),
Ben Skeggs6ee73862009-12-11 19:24:15 +1000787 offset);
788 if (ret)
789 return ret;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100790
791 } else if (dev_priv->card_type >= NV_10) {
792 *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200793 nvbo->tile_mode,
794 nvbo->tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000795 }
796
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100797 return 0;
798}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000799
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100800static void
801nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
802 struct nouveau_tile_reg *new_tile,
803 struct nouveau_tile_reg **old_tile)
804{
805 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
806 struct drm_device *dev = dev_priv->dev;
807
808 if (dev_priv->card_type >= NV_10 &&
809 dev_priv->card_type < NV_50) {
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200810 nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100811 *old_tile = new_tile;
812 }
813}
814
815static int
816nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000817 bool no_wait_reserve, bool no_wait_gpu,
818 struct ttm_mem_reg *new_mem)
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100819{
820 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
821 struct nouveau_bo *nvbo = nouveau_bo(bo);
822 struct ttm_mem_reg *old_mem = &bo->mem;
823 struct nouveau_tile_reg *new_tile = NULL;
824 int ret = 0;
825
826 ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
827 if (ret)
828 return ret;
829
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100830 /* Fake bo copy. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000831 if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
832 BUG_ON(bo->mem.mm_node != NULL);
833 bo->mem = *new_mem;
834 new_mem->mm_node = NULL;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100835 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000836 }
837
Ben Skeggsb8a6a802010-08-27 11:55:43 +1000838 /* Software copy if the card isn't up and running yet. */
839 if (!dev_priv->channel) {
840 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
841 goto out;
842 }
843
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100844 /* Hardware assisted copy. */
845 if (new_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000846 ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100847 else if (old_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000848 ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100849 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000850 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000851
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100852 if (!ret)
853 goto out;
854
855 /* Fallback to software copy. */
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000856 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100857
858out:
859 if (ret)
860 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
861 else
862 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
863
864 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000865}
866
867static int
868nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
869{
870 return 0;
871}
872
Jerome Glissef32f02f2010-04-09 14:39:25 +0200873static int
874nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
875{
876 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
877 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
878 struct drm_device *dev = dev_priv->dev;
879
880 mem->bus.addr = NULL;
881 mem->bus.offset = 0;
882 mem->bus.size = mem->num_pages << PAGE_SHIFT;
883 mem->bus.base = 0;
884 mem->bus.is_iomem = false;
885 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
886 return -EINVAL;
887 switch (mem->mem_type) {
888 case TTM_PL_SYSTEM:
889 /* System memory */
890 return 0;
891 case TTM_PL_TT:
892#if __OS_HAS_AGP
893 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000894 mem->bus.offset = mem->start << PAGE_SHIFT;
Jerome Glissef32f02f2010-04-09 14:39:25 +0200895 mem->bus.base = dev_priv->gart_info.aper_base;
896 mem->bus.is_iomem = true;
897 }
898#endif
899 break;
900 case TTM_PL_VRAM:
Ben Skeggsd961db72010-08-05 10:48:18 +1000901 mem->bus.offset = mem->start << PAGE_SHIFT;
Jordan Crouse01d73a62010-05-27 13:40:24 -0600902 mem->bus.base = pci_resource_start(dev->pdev, 1);
Jerome Glissef32f02f2010-04-09 14:39:25 +0200903 mem->bus.is_iomem = true;
904 break;
905 default:
906 return -EINVAL;
907 }
908 return 0;
909}
910
911static void
912nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
913{
914}
915
916static int
917nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
918{
Ben Skeggse1429b42010-09-10 11:12:25 +1000919 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
920 struct nouveau_bo *nvbo = nouveau_bo(bo);
921
922 /* as long as the bo isn't in vram, and isn't tiled, we've got
923 * nothing to do here.
924 */
925 if (bo->mem.mem_type != TTM_PL_VRAM) {
Francisco Jerezf13b3262010-10-10 06:01:08 +0200926 if (dev_priv->card_type < NV_50 ||
927 !nouveau_bo_tile_layout(nvbo))
Ben Skeggse1429b42010-09-10 11:12:25 +1000928 return 0;
929 }
930
931 /* make sure bo is in mappable vram */
Ben Skeggsd961db72010-08-05 10:48:18 +1000932 if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
Ben Skeggse1429b42010-09-10 11:12:25 +1000933 return 0;
934
935
936 nvbo->placement.fpfn = 0;
937 nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
938 nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
939 return ttm_bo_validate(bo, &nvbo->placement, false, true, false);
Jerome Glissef32f02f2010-04-09 14:39:25 +0200940}
941
Francisco Jerez332b2422010-10-20 23:35:40 +0200942void
943nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
944{
Francisco Jerez23c45e82010-10-28 23:10:29 +0200945 struct nouveau_fence *old_fence;
Francisco Jerez332b2422010-10-20 23:35:40 +0200946
947 if (likely(fence))
Francisco Jerez23c45e82010-10-28 23:10:29 +0200948 nouveau_fence_ref(fence);
Francisco Jerez332b2422010-10-20 23:35:40 +0200949
Francisco Jerez23c45e82010-10-28 23:10:29 +0200950 spin_lock(&nvbo->bo.bdev->fence_lock);
951 old_fence = nvbo->bo.sync_obj;
952 nvbo->bo.sync_obj = fence;
Francisco Jerez332b2422010-10-20 23:35:40 +0200953 spin_unlock(&nvbo->bo.bdev->fence_lock);
Francisco Jerez23c45e82010-10-28 23:10:29 +0200954
955 nouveau_fence_unref(&old_fence);
Francisco Jerez332b2422010-10-20 23:35:40 +0200956}
957
Ben Skeggs6ee73862009-12-11 19:24:15 +1000958struct ttm_bo_driver nouveau_bo_driver = {
959 .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
960 .invalidate_caches = nouveau_bo_invalidate_caches,
961 .init_mem_type = nouveau_bo_init_mem_type,
962 .evict_flags = nouveau_bo_evict_flags,
963 .move = nouveau_bo_move,
964 .verify_access = nouveau_bo_verify_access,
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200965 .sync_obj_signaled = __nouveau_fence_signalled,
966 .sync_obj_wait = __nouveau_fence_wait,
967 .sync_obj_flush = __nouveau_fence_flush,
968 .sync_obj_unref = __nouveau_fence_unref,
969 .sync_obj_ref = __nouveau_fence_ref,
Jerome Glissef32f02f2010-04-09 14:39:25 +0200970 .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
971 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
972 .io_mem_free = &nouveau_ttm_io_mem_free,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000973};
974