blob: a7883e7db3444a4d559bed4a4f33b6e97c6f77cc [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
Ben Skeggs7a45d762010-11-22 08:50:27 +1000245 ret = nouveau_bo_validate(nvbo, 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
Ben Skeggs7a45d762010-11-22 08:50:27 +1000281 ret = nouveau_bo_validate(nvbo, 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
Ben Skeggs7a45d762010-11-22 08:50:27 +1000320int
321nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
322 bool no_wait_reserve, bool no_wait_gpu)
323{
324 int ret;
325
326 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
327 no_wait_reserve, no_wait_gpu);
328 if (ret)
329 return ret;
330
331 return 0;
332}
333
Ben Skeggs6ee73862009-12-11 19:24:15 +1000334u16
335nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
336{
337 bool is_iomem;
338 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
339 mem = &mem[index];
340 if (is_iomem)
341 return ioread16_native((void __force __iomem *)mem);
342 else
343 return *mem;
344}
345
346void
347nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
348{
349 bool is_iomem;
350 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
351 mem = &mem[index];
352 if (is_iomem)
353 iowrite16_native(val, (void __force __iomem *)mem);
354 else
355 *mem = val;
356}
357
358u32
359nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
360{
361 bool is_iomem;
362 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
363 mem = &mem[index];
364 if (is_iomem)
365 return ioread32_native((void __force __iomem *)mem);
366 else
367 return *mem;
368}
369
370void
371nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
372{
373 bool is_iomem;
374 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
375 mem = &mem[index];
376 if (is_iomem)
377 iowrite32_native(val, (void __force __iomem *)mem);
378 else
379 *mem = val;
380}
381
382static struct ttm_backend *
383nouveau_bo_create_ttm_backend_entry(struct ttm_bo_device *bdev)
384{
385 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
386 struct drm_device *dev = dev_priv->dev;
387
388 switch (dev_priv->gart_info.type) {
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000389#if __OS_HAS_AGP
Ben Skeggs6ee73862009-12-11 19:24:15 +1000390 case NOUVEAU_GART_AGP:
391 return ttm_agp_backend_init(bdev, dev->agp->bridge);
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000392#endif
Ben Skeggs6ee73862009-12-11 19:24:15 +1000393 case NOUVEAU_GART_SGDMA:
394 return nouveau_sgdma_init_ttm(dev);
395 default:
396 NV_ERROR(dev, "Unknown GART type %d\n",
397 dev_priv->gart_info.type);
398 break;
399 }
400
401 return NULL;
402}
403
404static int
405nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
406{
407 /* We'll do this from user space. */
408 return 0;
409}
410
411static int
412nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
413 struct ttm_mem_type_manager *man)
414{
415 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
416 struct drm_device *dev = dev_priv->dev;
417
418 switch (type) {
419 case TTM_PL_SYSTEM:
420 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
421 man->available_caching = TTM_PL_MASK_CACHING;
422 man->default_caching = TTM_PL_FLAG_CACHED;
423 break;
424 case TTM_PL_VRAM:
Ben Skeggsd961db72010-08-05 10:48:18 +1000425 man->func = &ttm_bo_manager_func;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000426 man->flags = TTM_MEMTYPE_FLAG_FIXED |
Jerome Glissef32f02f2010-04-09 14:39:25 +0200427 TTM_MEMTYPE_FLAG_MAPPABLE;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000428 man->available_caching = TTM_PL_FLAG_UNCACHED |
429 TTM_PL_FLAG_WC;
430 man->default_caching = TTM_PL_FLAG_WC;
Ben Skeggsfbd28952010-09-01 15:24:34 +1000431 if (dev_priv->card_type == NV_50)
432 man->gpu_offset = 0x40000000;
433 else
434 man->gpu_offset = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000435 break;
436 case TTM_PL_TT:
Ben Skeggsd961db72010-08-05 10:48:18 +1000437 man->func = &ttm_bo_manager_func;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000438 switch (dev_priv->gart_info.type) {
439 case NOUVEAU_GART_AGP:
Jerome Glissef32f02f2010-04-09 14:39:25 +0200440 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
Francisco Jereza3d487e2010-11-20 22:11:22 +0100441 man->available_caching = TTM_PL_FLAG_UNCACHED |
442 TTM_PL_FLAG_WC;
443 man->default_caching = TTM_PL_FLAG_WC;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000444 break;
445 case NOUVEAU_GART_SGDMA:
446 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE |
447 TTM_MEMTYPE_FLAG_CMA;
448 man->available_caching = TTM_PL_MASK_CACHING;
449 man->default_caching = TTM_PL_FLAG_CACHED;
450 break;
451 default:
452 NV_ERROR(dev, "Unknown GART type: %d\n",
453 dev_priv->gart_info.type);
454 return -EINVAL;
455 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000456 man->gpu_offset = dev_priv->vm_gart_base;
457 break;
458 default:
459 NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
460 return -EINVAL;
461 }
462 return 0;
463}
464
465static void
466nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
467{
468 struct nouveau_bo *nvbo = nouveau_bo(bo);
469
470 switch (bo->mem.mem_type) {
Francisco Jerez22fbd532009-12-11 18:40:17 +0100471 case TTM_PL_VRAM:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100472 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
473 TTM_PL_FLAG_SYSTEM);
Francisco Jerez22fbd532009-12-11 18:40:17 +0100474 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000475 default:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100476 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000477 break;
478 }
Francisco Jerez22fbd532009-12-11 18:40:17 +0100479
480 *pl = nvbo->placement;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000481}
482
483
484/* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
485 * TTM_PL_{VRAM,TT} directly.
486 */
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100487
Ben Skeggs6ee73862009-12-11 19:24:15 +1000488static int
489nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000490 struct nouveau_bo *nvbo, bool evict,
491 bool no_wait_reserve, bool no_wait_gpu,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000492 struct ttm_mem_reg *new_mem)
493{
494 struct nouveau_fence *fence = NULL;
495 int ret;
496
497 ret = nouveau_fence_new(chan, &fence, true);
498 if (ret)
499 return ret;
500
Francisco Jerez64798812010-09-21 19:02:01 +0200501 ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
Francisco Jerez311ab692010-07-04 12:54:23 +0200502 no_wait_reserve, no_wait_gpu, new_mem);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200503 nouveau_fence_unref(&fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000504 return ret;
505}
506
507static inline uint32_t
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000508nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
509 struct nouveau_channel *chan, struct ttm_mem_reg *mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000510{
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000511 struct nouveau_bo *nvbo = nouveau_bo(bo);
512
513 if (nvbo->no_vm) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000514 if (mem->mem_type == TTM_PL_TT)
515 return NvDmaGART;
516 return NvDmaVRAM;
517 }
518
519 if (mem->mem_type == TTM_PL_TT)
520 return chan->gart_handle;
521 return chan->vram_handle;
522}
523
524static int
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000525nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
526 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000527{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000528 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000529 struct nouveau_bo *nvbo = nouveau_bo(bo);
530 u64 length = (new_mem->num_pages << PAGE_SHIFT);
531 u64 src_offset, dst_offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000532 int ret;
533
Ben Skeggsd961db72010-08-05 10:48:18 +1000534 src_offset = old_mem->start << PAGE_SHIFT;
535 dst_offset = new_mem->start << PAGE_SHIFT;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000536 if (!nvbo->no_vm) {
537 if (old_mem->mem_type == TTM_PL_VRAM)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000538 src_offset += dev_priv->vm_vram_base;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000539 else
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000540 src_offset += dev_priv->vm_gart_base;
541
542 if (new_mem->mem_type == TTM_PL_VRAM)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000543 dst_offset += dev_priv->vm_vram_base;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000544 else
545 dst_offset += dev_priv->vm_gart_base;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000546 }
547
548 ret = RING_SPACE(chan, 3);
549 if (ret)
550 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000551
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000552 BEGIN_RING(chan, NvSubM2MF, 0x0184, 2);
553 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
554 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
555
556 while (length) {
557 u32 amount, stride, height;
558
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000559 amount = min(length, (u64)(4 * 1024 * 1024));
560 stride = 16 * 4;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000561 height = amount / stride;
562
Francisco Jerezf13b3262010-10-10 06:01:08 +0200563 if (new_mem->mem_type == TTM_PL_VRAM &&
564 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000565 ret = RING_SPACE(chan, 8);
566 if (ret)
567 return ret;
568
569 BEGIN_RING(chan, NvSubM2MF, 0x0200, 7);
570 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000571 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000572 OUT_RING (chan, stride);
573 OUT_RING (chan, height);
574 OUT_RING (chan, 1);
575 OUT_RING (chan, 0);
576 OUT_RING (chan, 0);
577 } else {
578 ret = RING_SPACE(chan, 2);
579 if (ret)
580 return ret;
581
582 BEGIN_RING(chan, NvSubM2MF, 0x0200, 1);
583 OUT_RING (chan, 1);
584 }
Francisco Jerezf13b3262010-10-10 06:01:08 +0200585 if (old_mem->mem_type == TTM_PL_VRAM &&
586 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000587 ret = RING_SPACE(chan, 8);
588 if (ret)
589 return ret;
590
591 BEGIN_RING(chan, NvSubM2MF, 0x021c, 7);
592 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000593 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000594 OUT_RING (chan, stride);
595 OUT_RING (chan, height);
596 OUT_RING (chan, 1);
597 OUT_RING (chan, 0);
598 OUT_RING (chan, 0);
599 } else {
600 ret = RING_SPACE(chan, 2);
601 if (ret)
602 return ret;
603
604 BEGIN_RING(chan, NvSubM2MF, 0x021c, 1);
605 OUT_RING (chan, 1);
606 }
607
608 ret = RING_SPACE(chan, 14);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000609 if (ret)
610 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000611
612 BEGIN_RING(chan, NvSubM2MF, 0x0238, 2);
613 OUT_RING (chan, upper_32_bits(src_offset));
614 OUT_RING (chan, upper_32_bits(dst_offset));
615 BEGIN_RING(chan, NvSubM2MF, 0x030c, 8);
616 OUT_RING (chan, lower_32_bits(src_offset));
617 OUT_RING (chan, lower_32_bits(dst_offset));
618 OUT_RING (chan, stride);
619 OUT_RING (chan, stride);
620 OUT_RING (chan, stride);
621 OUT_RING (chan, height);
622 OUT_RING (chan, 0x00000101);
623 OUT_RING (chan, 0x00000000);
624 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
625 OUT_RING (chan, 0);
626
627 length -= amount;
628 src_offset += amount;
629 dst_offset += amount;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000630 }
631
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000632 return 0;
633}
634
635static int
636nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
637 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
638{
Ben Skeggsd961db72010-08-05 10:48:18 +1000639 u32 src_offset = old_mem->start << PAGE_SHIFT;
640 u32 dst_offset = new_mem->start << PAGE_SHIFT;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000641 u32 page_count = new_mem->num_pages;
642 int ret;
643
644 ret = RING_SPACE(chan, 3);
645 if (ret)
646 return ret;
647
648 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
649 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
650 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
651
Ben Skeggs6ee73862009-12-11 19:24:15 +1000652 page_count = new_mem->num_pages;
653 while (page_count) {
654 int line_count = (page_count > 2047) ? 2047 : page_count;
655
Ben Skeggs6ee73862009-12-11 19:24:15 +1000656 ret = RING_SPACE(chan, 11);
657 if (ret)
658 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000659
Ben Skeggs6ee73862009-12-11 19:24:15 +1000660 BEGIN_RING(chan, NvSubM2MF,
661 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000662 OUT_RING (chan, src_offset);
663 OUT_RING (chan, dst_offset);
664 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
665 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
666 OUT_RING (chan, PAGE_SIZE); /* line_length */
667 OUT_RING (chan, line_count);
668 OUT_RING (chan, 0x00000101);
669 OUT_RING (chan, 0x00000000);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000670 BEGIN_RING(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000671 OUT_RING (chan, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000672
673 page_count -= line_count;
674 src_offset += (PAGE_SIZE * line_count);
675 dst_offset += (PAGE_SIZE * line_count);
676 }
677
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000678 return 0;
679}
680
681static int
682nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
683 bool no_wait_reserve, bool no_wait_gpu,
684 struct ttm_mem_reg *new_mem)
685{
686 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
687 struct nouveau_bo *nvbo = nouveau_bo(bo);
688 struct nouveau_channel *chan;
689 int ret;
690
691 chan = nvbo->channel;
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000692 if (!chan || nvbo->no_vm) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000693 chan = dev_priv->channel;
Francisco Jereze419cf02010-10-25 23:38:59 +0200694 mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000695 }
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000696
697 if (dev_priv->card_type < NV_50)
698 ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
699 else
700 ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000701 if (ret == 0) {
702 ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
703 no_wait_reserve,
704 no_wait_gpu, new_mem);
705 }
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000706
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000707 if (chan == dev_priv->channel)
708 mutex_unlock(&chan->mutex);
709 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000710}
711
712static int
713nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000714 bool no_wait_reserve, bool no_wait_gpu,
715 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000716{
717 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
718 struct ttm_placement placement;
719 struct ttm_mem_reg tmp_mem;
720 int ret;
721
722 placement.fpfn = placement.lpfn = 0;
723 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +0100724 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000725
726 tmp_mem = *new_mem;
727 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000728 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000729 if (ret)
730 return ret;
731
732 ret = ttm_tt_bind(bo->ttm, &tmp_mem);
733 if (ret)
734 goto out;
735
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000736 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000737 if (ret)
738 goto out;
739
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000740 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000741out:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000742 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000743 return ret;
744}
745
746static int
747nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000748 bool no_wait_reserve, bool no_wait_gpu,
749 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000750{
751 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
752 struct ttm_placement placement;
753 struct ttm_mem_reg tmp_mem;
754 int ret;
755
756 placement.fpfn = placement.lpfn = 0;
757 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +0100758 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000759
760 tmp_mem = *new_mem;
761 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000762 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000763 if (ret)
764 return ret;
765
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000766 ret = ttm_bo_move_ttm(bo, evict, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000767 if (ret)
768 goto out;
769
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000770 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000771 if (ret)
772 goto out;
773
774out:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000775 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000776 return ret;
777}
778
779static int
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100780nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
781 struct nouveau_tile_reg **new_tile)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000782{
783 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000784 struct drm_device *dev = dev_priv->dev;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100785 struct nouveau_bo *nvbo = nouveau_bo(bo);
786 uint64_t offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000787 int ret;
788
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100789 if (nvbo->no_vm || new_mem->mem_type != TTM_PL_VRAM) {
790 /* Nothing to do. */
791 *new_tile = NULL;
792 return 0;
793 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000794
Ben Skeggsd961db72010-08-05 10:48:18 +1000795 offset = new_mem->start << PAGE_SHIFT;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100796
797 if (dev_priv->card_type == NV_50) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000798 ret = nv50_mem_vm_bind_linear(dev,
799 offset + dev_priv->vm_vram_base,
Francisco Jerezf13b3262010-10-10 06:01:08 +0200800 new_mem->size,
801 nouveau_bo_tile_layout(nvbo),
Ben Skeggs6ee73862009-12-11 19:24:15 +1000802 offset);
803 if (ret)
804 return ret;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100805
806 } else if (dev_priv->card_type >= NV_10) {
807 *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200808 nvbo->tile_mode,
809 nvbo->tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000810 }
811
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100812 return 0;
813}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000814
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100815static void
816nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
817 struct nouveau_tile_reg *new_tile,
818 struct nouveau_tile_reg **old_tile)
819{
820 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
821 struct drm_device *dev = dev_priv->dev;
822
823 if (dev_priv->card_type >= NV_10 &&
824 dev_priv->card_type < NV_50) {
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200825 nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100826 *old_tile = new_tile;
827 }
828}
829
830static int
831nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000832 bool no_wait_reserve, bool no_wait_gpu,
833 struct ttm_mem_reg *new_mem)
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100834{
835 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
836 struct nouveau_bo *nvbo = nouveau_bo(bo);
837 struct ttm_mem_reg *old_mem = &bo->mem;
838 struct nouveau_tile_reg *new_tile = NULL;
839 int ret = 0;
840
841 ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
842 if (ret)
843 return ret;
844
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100845 /* Fake bo copy. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000846 if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
847 BUG_ON(bo->mem.mm_node != NULL);
848 bo->mem = *new_mem;
849 new_mem->mm_node = NULL;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100850 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000851 }
852
Ben Skeggsb8a6a802010-08-27 11:55:43 +1000853 /* Software copy if the card isn't up and running yet. */
854 if (!dev_priv->channel) {
855 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
856 goto out;
857 }
858
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100859 /* Hardware assisted copy. */
860 if (new_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000861 ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100862 else if (old_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000863 ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100864 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000865 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000866
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100867 if (!ret)
868 goto out;
869
870 /* Fallback to software copy. */
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000871 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100872
873out:
874 if (ret)
875 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
876 else
877 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
878
879 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000880}
881
882static int
883nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
884{
885 return 0;
886}
887
Jerome Glissef32f02f2010-04-09 14:39:25 +0200888static int
889nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
890{
891 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
892 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
893 struct drm_device *dev = dev_priv->dev;
894
895 mem->bus.addr = NULL;
896 mem->bus.offset = 0;
897 mem->bus.size = mem->num_pages << PAGE_SHIFT;
898 mem->bus.base = 0;
899 mem->bus.is_iomem = false;
900 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
901 return -EINVAL;
902 switch (mem->mem_type) {
903 case TTM_PL_SYSTEM:
904 /* System memory */
905 return 0;
906 case TTM_PL_TT:
907#if __OS_HAS_AGP
908 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000909 mem->bus.offset = mem->start << PAGE_SHIFT;
Jerome Glissef32f02f2010-04-09 14:39:25 +0200910 mem->bus.base = dev_priv->gart_info.aper_base;
911 mem->bus.is_iomem = true;
912 }
913#endif
914 break;
915 case TTM_PL_VRAM:
Ben Skeggsd961db72010-08-05 10:48:18 +1000916 mem->bus.offset = mem->start << PAGE_SHIFT;
Jordan Crouse01d73a62010-05-27 13:40:24 -0600917 mem->bus.base = pci_resource_start(dev->pdev, 1);
Jerome Glissef32f02f2010-04-09 14:39:25 +0200918 mem->bus.is_iomem = true;
919 break;
920 default:
921 return -EINVAL;
922 }
923 return 0;
924}
925
926static void
927nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
928{
929}
930
931static int
932nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
933{
Ben Skeggse1429b42010-09-10 11:12:25 +1000934 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
935 struct nouveau_bo *nvbo = nouveau_bo(bo);
936
937 /* as long as the bo isn't in vram, and isn't tiled, we've got
938 * nothing to do here.
939 */
940 if (bo->mem.mem_type != TTM_PL_VRAM) {
Francisco Jerezf13b3262010-10-10 06:01:08 +0200941 if (dev_priv->card_type < NV_50 ||
942 !nouveau_bo_tile_layout(nvbo))
Ben Skeggse1429b42010-09-10 11:12:25 +1000943 return 0;
944 }
945
946 /* make sure bo is in mappable vram */
Ben Skeggsd961db72010-08-05 10:48:18 +1000947 if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
Ben Skeggse1429b42010-09-10 11:12:25 +1000948 return 0;
949
950
951 nvbo->placement.fpfn = 0;
952 nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
953 nouveau_bo_placement_set(nvbo, TTM_PL_VRAM, 0);
Ben Skeggs7a45d762010-11-22 08:50:27 +1000954 return nouveau_bo_validate(nvbo, false, true, false);
Jerome Glissef32f02f2010-04-09 14:39:25 +0200955}
956
Francisco Jerez332b2422010-10-20 23:35:40 +0200957void
958nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
959{
Francisco Jerez23c45e82010-10-28 23:10:29 +0200960 struct nouveau_fence *old_fence;
Francisco Jerez332b2422010-10-20 23:35:40 +0200961
962 if (likely(fence))
Francisco Jerez23c45e82010-10-28 23:10:29 +0200963 nouveau_fence_ref(fence);
Francisco Jerez332b2422010-10-20 23:35:40 +0200964
Francisco Jerez23c45e82010-10-28 23:10:29 +0200965 spin_lock(&nvbo->bo.bdev->fence_lock);
966 old_fence = nvbo->bo.sync_obj;
967 nvbo->bo.sync_obj = fence;
Francisco Jerez332b2422010-10-20 23:35:40 +0200968 spin_unlock(&nvbo->bo.bdev->fence_lock);
Francisco Jerez23c45e82010-10-28 23:10:29 +0200969
970 nouveau_fence_unref(&old_fence);
Francisco Jerez332b2422010-10-20 23:35:40 +0200971}
972
Ben Skeggs6ee73862009-12-11 19:24:15 +1000973struct ttm_bo_driver nouveau_bo_driver = {
974 .create_ttm_backend_entry = nouveau_bo_create_ttm_backend_entry,
975 .invalidate_caches = nouveau_bo_invalidate_caches,
976 .init_mem_type = nouveau_bo_init_mem_type,
977 .evict_flags = nouveau_bo_evict_flags,
978 .move = nouveau_bo_move,
979 .verify_access = nouveau_bo_verify_access,
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200980 .sync_obj_signaled = __nouveau_fence_signalled,
981 .sync_obj_wait = __nouveau_fence_wait,
982 .sync_obj_flush = __nouveau_fence_flush,
983 .sync_obj_unref = __nouveau_fence_unref,
984 .sync_obj_ref = __nouveau_fence_ref,
Jerome Glissef32f02f2010-04-09 14:39:25 +0200985 .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
986 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
987 .io_mem_free = &nouveau_ttm_io_mem_free,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000988};
989