blob: c45c7bce9ebd23d4331c3afa992c1753e914e1a6 [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"
Jerome Glisseb1e5f172011-11-02 23:59:28 -040031#include "ttm/ttm_page_alloc.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100032
33#include "nouveau_drm.h"
34#include "nouveau_drv.h"
35#include "nouveau_dma.h"
Ben Skeggsf869ef82010-11-15 11:53:16 +100036#include "nouveau_mm.h"
37#include "nouveau_vm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100038
Maarten Maathuisa5106042009-12-26 21:46:36 +010039#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Maarten Maathuisa5106042009-12-26 21:46:36 +010041
Ben Skeggs6ee73862009-12-11 19:24:15 +100042static void
43nouveau_bo_del_ttm(struct ttm_buffer_object *bo)
44{
45 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010046 struct drm_device *dev = dev_priv->dev;
Ben Skeggs6ee73862009-12-11 19:24:15 +100047 struct nouveau_bo *nvbo = nouveau_bo(bo);
48
Ben Skeggs6ee73862009-12-11 19:24:15 +100049 if (unlikely(nvbo->gem))
50 DRM_ERROR("bo %p still attached to GEM object\n", bo);
51
Francisco Jereza5cf68b2010-10-24 16:14:41 +020052 nv10_mem_put_tile_region(dev, nvbo->tile, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +100053 kfree(nvbo);
54}
55
Francisco Jereza0af9ad2009-12-11 16:51:09 +010056static void
Ben Skeggsdb5c8e22011-02-10 13:41:01 +100057nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,
Ben Skeggsf91bac52011-06-06 14:15:46 +100058 int *align, int *size)
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 {
Ben Skeggsf91bac52011-06-06 14:15:46 +100082 *size = roundup(*size, (1 << nvbo->page_shift));
83 *align = max((1 << nvbo->page_shift), *align);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010084 }
85
Maarten Maathuis1c7059e2009-12-25 18:51:17 +010086 *size = roundup(*size, PAGE_SIZE);
Francisco Jereza0af9ad2009-12-11 16:51:09 +010087}
88
Ben Skeggs6ee73862009-12-11 19:24:15 +100089int
Ben Skeggs7375c952011-06-07 14:21:29 +100090nouveau_bo_new(struct drm_device *dev, int size, int align,
91 uint32_t flags, uint32_t tile_mode, uint32_t tile_flags,
Dave Airlie22b33e82012-04-02 11:53:06 +010092 struct sg_table *sg,
Ben Skeggs7375c952011-06-07 14:21:29 +100093 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +100094{
95 struct drm_nouveau_private *dev_priv = dev->dev_private;
96 struct nouveau_bo *nvbo;
Jerome Glisse57de4ba2011-11-11 15:42:57 -050097 size_t acc_size;
Ben Skeggsf91bac52011-06-06 14:15:46 +100098 int ret;
Dave Airlie22b33e82012-04-02 11:53:06 +010099 int type = ttm_bo_type_device;
100
101 if (sg)
102 type = ttm_bo_type_sg;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000103
104 nvbo = kzalloc(sizeof(struct nouveau_bo), GFP_KERNEL);
105 if (!nvbo)
106 return -ENOMEM;
107 INIT_LIST_HEAD(&nvbo->head);
108 INIT_LIST_HEAD(&nvbo->entry);
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000109 INIT_LIST_HEAD(&nvbo->vma_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000110 nvbo->tile_mode = tile_mode;
111 nvbo->tile_flags = tile_flags;
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200112 nvbo->bo.bdev = &dev_priv->ttm.bdev;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000113
Ben Skeggsf91bac52011-06-06 14:15:46 +1000114 nvbo->page_shift = 12;
115 if (dev_priv->bar1_vm) {
116 if (!(flags & TTM_PL_FLAG_TT) && size > 256 * 1024)
117 nvbo->page_shift = dev_priv->bar1_vm->lpg_shift;
118 }
119
120 nouveau_bo_fixup_align(nvbo, flags, &align, &size);
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000121 nvbo->bo.mem.num_pages = size >> PAGE_SHIFT;
122 nouveau_bo_placement_set(nvbo, flags, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000123
Jerome Glisse57de4ba2011-11-11 15:42:57 -0500124 acc_size = ttm_bo_dma_acc_size(&dev_priv->ttm.bdev, size,
125 sizeof(struct nouveau_bo));
126
Ben Skeggs6ee73862009-12-11 19:24:15 +1000127 ret = ttm_bo_init(&dev_priv->ttm.bdev, &nvbo->bo, size,
Dave Airlie22b33e82012-04-02 11:53:06 +0100128 type, &nvbo->placement,
129 align >> PAGE_SHIFT, 0, false, NULL, acc_size, sg,
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000130 nouveau_bo_del_ttm);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000131 if (ret) {
132 /* ttm will call nouveau_bo_del_ttm if it fails.. */
133 return ret;
134 }
135
Ben Skeggs6ee73862009-12-11 19:24:15 +1000136 *pnvbo = nvbo;
137 return 0;
138}
139
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100140static void
141set_placement_list(uint32_t *pl, unsigned *n, uint32_t type, uint32_t flags)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000142{
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100143 *n = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000144
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100145 if (type & TTM_PL_FLAG_VRAM)
146 pl[(*n)++] = TTM_PL_FLAG_VRAM | flags;
147 if (type & TTM_PL_FLAG_TT)
148 pl[(*n)++] = TTM_PL_FLAG_TT | flags;
149 if (type & TTM_PL_FLAG_SYSTEM)
150 pl[(*n)++] = TTM_PL_FLAG_SYSTEM | flags;
151}
Ben Skeggs37cb3e082009-12-16 16:22:42 +1000152
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200153static void
154set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
155{
156 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
Francisco Jerez812f2192011-02-03 01:49:33 +0100157 int vram_pages = dev_priv->vram_size >> PAGE_SHIFT;
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200158
159 if (dev_priv->card_type == NV_10 &&
Francisco Jerez812f2192011-02-03 01:49:33 +0100160 nvbo->tile_mode && (type & TTM_PL_FLAG_VRAM) &&
Francisco Jerez4beb1162011-11-06 21:21:28 +0100161 nvbo->bo.mem.num_pages < vram_pages / 4) {
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200162 /*
163 * Make sure that the color and depth buffers are handled
164 * by independent memory controller units. Up to a 9x
165 * speed up when alpha-blending and depth-test are enabled
166 * at the same time.
167 */
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200168 if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
169 nvbo->placement.fpfn = vram_pages / 2;
170 nvbo->placement.lpfn = ~0;
171 } else {
172 nvbo->placement.fpfn = 0;
173 nvbo->placement.lpfn = vram_pages / 2;
174 }
175 }
176}
177
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100178void
179nouveau_bo_placement_set(struct nouveau_bo *nvbo, uint32_t type, uint32_t busy)
180{
181 struct ttm_placement *pl = &nvbo->placement;
182 uint32_t flags = TTM_PL_MASK_CACHING |
183 (nvbo->pin_refcnt ? TTM_PL_FLAG_NO_EVICT : 0);
184
185 pl->placement = nvbo->placements;
186 set_placement_list(nvbo->placements, &pl->num_placement,
187 type, flags);
188
189 pl->busy_placement = nvbo->busy_placements;
190 set_placement_list(nvbo->busy_placements, &pl->num_busy_placement,
191 type | busy, flags);
Francisco Jerez699ddfd2010-10-10 06:07:32 +0200192
193 set_placement_range(nvbo, type);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000194}
195
196int
197nouveau_bo_pin(struct nouveau_bo *nvbo, uint32_t memtype)
198{
199 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
200 struct ttm_buffer_object *bo = &nvbo->bo;
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100201 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000202
203 if (nvbo->pin_refcnt && !(memtype & (1 << bo->mem.mem_type))) {
204 NV_ERROR(nouveau_bdev(bo->bdev)->dev,
205 "bo %p pinned elsewhere: 0x%08x vs 0x%08x\n", bo,
206 1 << bo->mem.mem_type, memtype);
207 return -EINVAL;
208 }
209
210 if (nvbo->pin_refcnt++)
211 return 0;
212
213 ret = ttm_bo_reserve(bo, false, false, false, 0);
214 if (ret)
215 goto out;
216
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100217 nouveau_bo_placement_set(nvbo, memtype, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000218
Ben Skeggs7a45d762010-11-22 08:50:27 +1000219 ret = nouveau_bo_validate(nvbo, false, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000220 if (ret == 0) {
221 switch (bo->mem.mem_type) {
222 case TTM_PL_VRAM:
223 dev_priv->fb_aper_free -= bo->mem.size;
224 break;
225 case TTM_PL_TT:
226 dev_priv->gart_info.aper_free -= bo->mem.size;
227 break;
228 default:
229 break;
230 }
231 }
232 ttm_bo_unreserve(bo);
233out:
234 if (unlikely(ret))
235 nvbo->pin_refcnt--;
236 return ret;
237}
238
239int
240nouveau_bo_unpin(struct nouveau_bo *nvbo)
241{
242 struct drm_nouveau_private *dev_priv = nouveau_bdev(nvbo->bo.bdev);
243 struct ttm_buffer_object *bo = &nvbo->bo;
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100244 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000245
246 if (--nvbo->pin_refcnt)
247 return 0;
248
249 ret = ttm_bo_reserve(bo, false, false, false, 0);
250 if (ret)
251 return ret;
252
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100253 nouveau_bo_placement_set(nvbo, bo->mem.placement, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000254
Ben Skeggs7a45d762010-11-22 08:50:27 +1000255 ret = nouveau_bo_validate(nvbo, false, false, false);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000256 if (ret == 0) {
257 switch (bo->mem.mem_type) {
258 case TTM_PL_VRAM:
259 dev_priv->fb_aper_free += bo->mem.size;
260 break;
261 case TTM_PL_TT:
262 dev_priv->gart_info.aper_free += bo->mem.size;
263 break;
264 default:
265 break;
266 }
267 }
268
269 ttm_bo_unreserve(bo);
270 return ret;
271}
272
273int
274nouveau_bo_map(struct nouveau_bo *nvbo)
275{
276 int ret;
277
278 ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0);
279 if (ret)
280 return ret;
281
282 ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, &nvbo->kmap);
283 ttm_bo_unreserve(&nvbo->bo);
284 return ret;
285}
286
287void
288nouveau_bo_unmap(struct nouveau_bo *nvbo)
289{
Ben Skeggs9d59e8a2010-08-27 13:04:41 +1000290 if (nvbo)
291 ttm_bo_kunmap(&nvbo->kmap);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000292}
293
Ben Skeggs7a45d762010-11-22 08:50:27 +1000294int
295nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
296 bool no_wait_reserve, bool no_wait_gpu)
297{
298 int ret;
299
300 ret = ttm_bo_validate(&nvbo->bo, &nvbo->placement, interruptible,
301 no_wait_reserve, no_wait_gpu);
302 if (ret)
303 return ret;
304
305 return 0;
306}
307
Ben Skeggs6ee73862009-12-11 19:24:15 +1000308u16
309nouveau_bo_rd16(struct nouveau_bo *nvbo, unsigned index)
310{
311 bool is_iomem;
312 u16 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
313 mem = &mem[index];
314 if (is_iomem)
315 return ioread16_native((void __force __iomem *)mem);
316 else
317 return *mem;
318}
319
320void
321nouveau_bo_wr16(struct nouveau_bo *nvbo, unsigned index, u16 val)
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 iowrite16_native(val, (void __force __iomem *)mem);
328 else
329 *mem = val;
330}
331
332u32
333nouveau_bo_rd32(struct nouveau_bo *nvbo, unsigned index)
334{
335 bool is_iomem;
336 u32 *mem = ttm_kmap_obj_virtual(&nvbo->kmap, &is_iomem);
337 mem = &mem[index];
338 if (is_iomem)
339 return ioread32_native((void __force __iomem *)mem);
340 else
341 return *mem;
342}
343
344void
345nouveau_bo_wr32(struct nouveau_bo *nvbo, unsigned index, u32 val)
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 iowrite32_native(val, (void __force __iomem *)mem);
352 else
353 *mem = val;
354}
355
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400356static struct ttm_tt *
357nouveau_ttm_tt_create(struct ttm_bo_device *bdev,
358 unsigned long size, uint32_t page_flags,
359 struct page *dummy_read_page)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000360{
361 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
362 struct drm_device *dev = dev_priv->dev;
363
364 switch (dev_priv->gart_info.type) {
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000365#if __OS_HAS_AGP
Ben Skeggs6ee73862009-12-11 19:24:15 +1000366 case NOUVEAU_GART_AGP:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400367 return ttm_agp_tt_create(bdev, dev->agp->bridge,
368 size, page_flags, dummy_read_page);
Ben Skeggsb694dfb2009-12-15 10:38:32 +1000369#endif
Ben Skeggs58e6c7a2011-01-11 14:10:09 +1000370 case NOUVEAU_GART_PDMA:
371 case NOUVEAU_GART_HW:
Jerome Glisse649bf3c2011-11-01 20:46:13 -0400372 return nouveau_sgdma_create_ttm(bdev, size, page_flags,
373 dummy_read_page);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000374 default:
375 NV_ERROR(dev, "Unknown GART type %d\n",
376 dev_priv->gart_info.type);
377 break;
378 }
379
380 return NULL;
381}
382
383static int
384nouveau_bo_invalidate_caches(struct ttm_bo_device *bdev, uint32_t flags)
385{
386 /* We'll do this from user space. */
387 return 0;
388}
389
390static int
391nouveau_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
392 struct ttm_mem_type_manager *man)
393{
394 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
395 struct drm_device *dev = dev_priv->dev;
396
397 switch (type) {
398 case TTM_PL_SYSTEM:
399 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
400 man->available_caching = TTM_PL_MASK_CACHING;
401 man->default_caching = TTM_PL_FLAG_CACHED;
402 break;
403 case TTM_PL_VRAM:
Ben Skeggs8984e042010-11-15 11:48:33 +1000404 if (dev_priv->card_type >= NV_50) {
Ben Skeggs573a2a32010-08-25 15:26:04 +1000405 man->func = &nouveau_vram_manager;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000406 man->io_reserve_fastpath = false;
407 man->use_io_reserve_lru = true;
408 } else {
Ben Skeggs573a2a32010-08-25 15:26:04 +1000409 man->func = &ttm_bo_manager_func;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000410 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000411 man->flags = TTM_MEMTYPE_FLAG_FIXED |
Jerome Glissef32f02f2010-04-09 14:39:25 +0200412 TTM_MEMTYPE_FLAG_MAPPABLE;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000413 man->available_caching = TTM_PL_FLAG_UNCACHED |
414 TTM_PL_FLAG_WC;
415 man->default_caching = TTM_PL_FLAG_WC;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000416 break;
417 case TTM_PL_TT:
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000418 if (dev_priv->card_type >= NV_50)
419 man->func = &nouveau_gart_manager;
420 else
421 man->func = &ttm_bo_manager_func;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000422 switch (dev_priv->gart_info.type) {
423 case NOUVEAU_GART_AGP:
Jerome Glissef32f02f2010-04-09 14:39:25 +0200424 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
Francisco Jereza3d487e2010-11-20 22:11:22 +0100425 man->available_caching = TTM_PL_FLAG_UNCACHED |
426 TTM_PL_FLAG_WC;
427 man->default_caching = TTM_PL_FLAG_WC;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000428 break;
Ben Skeggs58e6c7a2011-01-11 14:10:09 +1000429 case NOUVEAU_GART_PDMA:
430 case NOUVEAU_GART_HW:
Ben Skeggs6ee73862009-12-11 19:24:15 +1000431 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 break;
442 default:
443 NV_ERROR(dev, "Unsupported memory type %u\n", (unsigned)type);
444 return -EINVAL;
445 }
446 return 0;
447}
448
449static void
450nouveau_bo_evict_flags(struct ttm_buffer_object *bo, struct ttm_placement *pl)
451{
452 struct nouveau_bo *nvbo = nouveau_bo(bo);
453
454 switch (bo->mem.mem_type) {
Francisco Jerez22fbd532009-12-11 18:40:17 +0100455 case TTM_PL_VRAM:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100456 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_TT,
457 TTM_PL_FLAG_SYSTEM);
Francisco Jerez22fbd532009-12-11 18:40:17 +0100458 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000459 default:
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100460 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_SYSTEM, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000461 break;
462 }
Francisco Jerez22fbd532009-12-11 18:40:17 +0100463
464 *pl = nvbo->placement;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000465}
466
467
468/* GPU-assisted copy using NV_MEMORY_TO_MEMORY_FORMAT, can access
469 * TTM_PL_{VRAM,TT} directly.
470 */
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100471
Ben Skeggs6ee73862009-12-11 19:24:15 +1000472static int
473nouveau_bo_move_accel_cleanup(struct nouveau_channel *chan,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000474 struct nouveau_bo *nvbo, bool evict,
475 bool no_wait_reserve, bool no_wait_gpu,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000476 struct ttm_mem_reg *new_mem)
477{
478 struct nouveau_fence *fence = NULL;
479 int ret;
480
481 ret = nouveau_fence_new(chan, &fence, true);
482 if (ret)
483 return ret;
484
Francisco Jerez64798812010-09-21 19:02:01 +0200485 ret = ttm_bo_move_accel_cleanup(&nvbo->bo, fence, NULL, evict,
Francisco Jerez311ab692010-07-04 12:54:23 +0200486 no_wait_reserve, no_wait_gpu, new_mem);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200487 nouveau_fence_unref(&fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000488 return ret;
489}
490
Ben Skeggs6ee73862009-12-11 19:24:15 +1000491static int
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000492nve0_bo_move_copy(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
493 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
494{
495 struct nouveau_mem *node = old_mem->mm_node;
496 int ret = RING_SPACE(chan, 10);
497 if (ret == 0) {
Ben Skeggs6d597022012-04-01 21:09:13 +1000498 BEGIN_NVC0(chan, NvSubCopy, 0x0400, 8);
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000499 OUT_RING (chan, upper_32_bits(node->vma[0].offset));
500 OUT_RING (chan, lower_32_bits(node->vma[0].offset));
501 OUT_RING (chan, upper_32_bits(node->vma[1].offset));
502 OUT_RING (chan, lower_32_bits(node->vma[1].offset));
503 OUT_RING (chan, PAGE_SIZE);
504 OUT_RING (chan, PAGE_SIZE);
505 OUT_RING (chan, PAGE_SIZE);
506 OUT_RING (chan, new_mem->num_pages);
Ben Skeggs6d597022012-04-01 21:09:13 +1000507 BEGIN_IMC0(chan, NvSubCopy, 0x0300, 0x0386);
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000508 }
509 return ret;
510}
511
512static int
Ben Skeggs183720b2010-12-09 15:17:10 +1000513nvc0_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
514 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
515{
Ben Skeggsd2f966662011-06-06 20:54:42 +1000516 struct nouveau_mem *node = old_mem->mm_node;
517 u64 src_offset = node->vma[0].offset;
518 u64 dst_offset = node->vma[1].offset;
Ben Skeggs183720b2010-12-09 15:17:10 +1000519 u32 page_count = new_mem->num_pages;
520 int ret;
521
Ben Skeggs183720b2010-12-09 15:17:10 +1000522 page_count = new_mem->num_pages;
523 while (page_count) {
524 int line_count = (page_count > 2047) ? 2047 : page_count;
525
526 ret = RING_SPACE(chan, 12);
527 if (ret)
528 return ret;
529
Ben Skeggs6d597022012-04-01 21:09:13 +1000530 BEGIN_NVC0(chan, NvSubM2MF, 0x0238, 2);
Ben Skeggs183720b2010-12-09 15:17:10 +1000531 OUT_RING (chan, upper_32_bits(dst_offset));
532 OUT_RING (chan, lower_32_bits(dst_offset));
Ben Skeggs6d597022012-04-01 21:09:13 +1000533 BEGIN_NVC0(chan, NvSubM2MF, 0x030c, 6);
Ben Skeggs183720b2010-12-09 15:17:10 +1000534 OUT_RING (chan, upper_32_bits(src_offset));
535 OUT_RING (chan, lower_32_bits(src_offset));
536 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
537 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
538 OUT_RING (chan, PAGE_SIZE); /* line_length */
539 OUT_RING (chan, line_count);
Ben Skeggs6d597022012-04-01 21:09:13 +1000540 BEGIN_NVC0(chan, NvSubM2MF, 0x0300, 1);
Ben Skeggs183720b2010-12-09 15:17:10 +1000541 OUT_RING (chan, 0x00100110);
542
543 page_count -= line_count;
544 src_offset += (PAGE_SIZE * line_count);
545 dst_offset += (PAGE_SIZE * line_count);
546 }
547
548 return 0;
549}
550
551static int
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000552nv50_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
553 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000554{
Ben Skeggsd2f966662011-06-06 20:54:42 +1000555 struct nouveau_mem *node = old_mem->mm_node;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000556 struct nouveau_bo *nvbo = nouveau_bo(bo);
557 u64 length = (new_mem->num_pages << PAGE_SHIFT);
Ben Skeggsd2f966662011-06-06 20:54:42 +1000558 u64 src_offset = node->vma[0].offset;
559 u64 dst_offset = node->vma[1].offset;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000560 int ret;
561
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000562 while (length) {
563 u32 amount, stride, height;
564
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000565 amount = min(length, (u64)(4 * 1024 * 1024));
566 stride = 16 * 4;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000567 height = amount / stride;
568
Francisco Jerezf13b3262010-10-10 06:01:08 +0200569 if (new_mem->mem_type == TTM_PL_VRAM &&
570 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000571 ret = RING_SPACE(chan, 8);
572 if (ret)
573 return ret;
574
Ben Skeggs6d597022012-04-01 21:09:13 +1000575 BEGIN_NV04(chan, NvSubM2MF, 0x0200, 7);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000576 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000577 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000578 OUT_RING (chan, stride);
579 OUT_RING (chan, height);
580 OUT_RING (chan, 1);
581 OUT_RING (chan, 0);
582 OUT_RING (chan, 0);
583 } else {
584 ret = RING_SPACE(chan, 2);
585 if (ret)
586 return ret;
587
Ben Skeggs6d597022012-04-01 21:09:13 +1000588 BEGIN_NV04(chan, NvSubM2MF, 0x0200, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000589 OUT_RING (chan, 1);
590 }
Francisco Jerezf13b3262010-10-10 06:01:08 +0200591 if (old_mem->mem_type == TTM_PL_VRAM &&
592 nouveau_bo_tile_layout(nvbo)) {
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000593 ret = RING_SPACE(chan, 8);
594 if (ret)
595 return ret;
596
Ben Skeggs6d597022012-04-01 21:09:13 +1000597 BEGIN_NV04(chan, NvSubM2MF, 0x021c, 7);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000598 OUT_RING (chan, 0);
Ben Skeggs5220b3c2010-09-23 15:21:17 +1000599 OUT_RING (chan, 0);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000600 OUT_RING (chan, stride);
601 OUT_RING (chan, height);
602 OUT_RING (chan, 1);
603 OUT_RING (chan, 0);
604 OUT_RING (chan, 0);
605 } else {
606 ret = RING_SPACE(chan, 2);
607 if (ret)
608 return ret;
609
Ben Skeggs6d597022012-04-01 21:09:13 +1000610 BEGIN_NV04(chan, NvSubM2MF, 0x021c, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000611 OUT_RING (chan, 1);
612 }
613
614 ret = RING_SPACE(chan, 14);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000615 if (ret)
616 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000617
Ben Skeggs6d597022012-04-01 21:09:13 +1000618 BEGIN_NV04(chan, NvSubM2MF, 0x0238, 2);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000619 OUT_RING (chan, upper_32_bits(src_offset));
620 OUT_RING (chan, upper_32_bits(dst_offset));
Ben Skeggs6d597022012-04-01 21:09:13 +1000621 BEGIN_NV04(chan, NvSubM2MF, 0x030c, 8);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000622 OUT_RING (chan, lower_32_bits(src_offset));
623 OUT_RING (chan, lower_32_bits(dst_offset));
624 OUT_RING (chan, stride);
625 OUT_RING (chan, stride);
626 OUT_RING (chan, stride);
627 OUT_RING (chan, height);
628 OUT_RING (chan, 0x00000101);
629 OUT_RING (chan, 0x00000000);
Ben Skeggs6d597022012-04-01 21:09:13 +1000630 BEGIN_NV04(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000631 OUT_RING (chan, 0);
632
633 length -= amount;
634 src_offset += amount;
635 dst_offset += amount;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000636 }
637
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000638 return 0;
639}
640
Ben Skeggsa6704782011-02-16 09:10:20 +1000641static inline uint32_t
642nouveau_bo_mem_ctxdma(struct ttm_buffer_object *bo,
643 struct nouveau_channel *chan, struct ttm_mem_reg *mem)
644{
645 if (mem->mem_type == TTM_PL_TT)
646 return chan->gart_handle;
647 return chan->vram_handle;
648}
649
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000650static int
651nv04_bo_move_m2mf(struct nouveau_channel *chan, struct ttm_buffer_object *bo,
652 struct ttm_mem_reg *old_mem, struct ttm_mem_reg *new_mem)
653{
Ben Skeggsd961db72010-08-05 10:48:18 +1000654 u32 src_offset = old_mem->start << PAGE_SHIFT;
655 u32 dst_offset = new_mem->start << PAGE_SHIFT;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000656 u32 page_count = new_mem->num_pages;
657 int ret;
658
659 ret = RING_SPACE(chan, 3);
660 if (ret)
661 return ret;
662
Ben Skeggs6d597022012-04-01 21:09:13 +1000663 BEGIN_NV04(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_DMA_SOURCE, 2);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000664 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, old_mem));
665 OUT_RING (chan, nouveau_bo_mem_ctxdma(bo, chan, new_mem));
666
Ben Skeggs6ee73862009-12-11 19:24:15 +1000667 page_count = new_mem->num_pages;
668 while (page_count) {
669 int line_count = (page_count > 2047) ? 2047 : page_count;
670
Ben Skeggs6ee73862009-12-11 19:24:15 +1000671 ret = RING_SPACE(chan, 11);
672 if (ret)
673 return ret;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000674
Ben Skeggs6d597022012-04-01 21:09:13 +1000675 BEGIN_NV04(chan, NvSubM2MF,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000676 NV_MEMORY_TO_MEMORY_FORMAT_OFFSET_IN, 8);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000677 OUT_RING (chan, src_offset);
678 OUT_RING (chan, dst_offset);
679 OUT_RING (chan, PAGE_SIZE); /* src_pitch */
680 OUT_RING (chan, PAGE_SIZE); /* dst_pitch */
681 OUT_RING (chan, PAGE_SIZE); /* line_length */
682 OUT_RING (chan, line_count);
683 OUT_RING (chan, 0x00000101);
684 OUT_RING (chan, 0x00000000);
Ben Skeggs6d597022012-04-01 21:09:13 +1000685 BEGIN_NV04(chan, NvSubM2MF, NV_MEMORY_TO_MEMORY_FORMAT_NOP, 1);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000686 OUT_RING (chan, 0);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000687
688 page_count -= line_count;
689 src_offset += (PAGE_SIZE * line_count);
690 dst_offset += (PAGE_SIZE * line_count);
691 }
692
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000693 return 0;
694}
695
696static int
Ben Skeggsd2f966662011-06-06 20:54:42 +1000697nouveau_vma_getmap(struct nouveau_channel *chan, struct nouveau_bo *nvbo,
698 struct ttm_mem_reg *mem, struct nouveau_vma *vma)
699{
700 struct nouveau_mem *node = mem->mm_node;
701 int ret;
702
703 ret = nouveau_vm_get(chan->vm, mem->num_pages << PAGE_SHIFT,
704 node->page_shift, NV_MEM_ACCESS_RO, vma);
705 if (ret)
706 return ret;
707
708 if (mem->mem_type == TTM_PL_VRAM)
709 nouveau_vm_map(vma, node);
710 else
Ben Skeggsf7b24c42011-12-22 15:20:21 +1000711 nouveau_vm_map_sg(vma, 0, mem->num_pages << PAGE_SHIFT, node);
Ben Skeggsd2f966662011-06-06 20:54:42 +1000712
713 return 0;
714}
715
716static int
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000717nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int evict, bool intr,
718 bool no_wait_reserve, bool no_wait_gpu,
719 struct ttm_mem_reg *new_mem)
720{
721 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Ben Skeggsaccf9492012-03-16 12:40:17 +1000722 struct nouveau_channel *chan = chan = dev_priv->channel;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000723 struct nouveau_bo *nvbo = nouveau_bo(bo);
Ben Skeggs3425df42011-02-10 11:22:12 +1000724 struct ttm_mem_reg *old_mem = &bo->mem;
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000725 int ret;
726
Ben Skeggsaccf9492012-03-16 12:40:17 +1000727 mutex_lock_nested(&chan->mutex, NOUVEAU_KCHANNEL_MUTEX);
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000728
Ben Skeggsd2f966662011-06-06 20:54:42 +1000729 /* create temporary vmas for the transfer and attach them to the
730 * old nouveau_mem node, these will get cleaned up after ttm has
731 * destroyed the ttm_mem_reg
Ben Skeggs3425df42011-02-10 11:22:12 +1000732 */
Ben Skeggs26c0c9e2011-02-10 12:59:51 +1000733 if (dev_priv->card_type >= NV_50) {
Ben Skeggsd5f42392011-02-10 12:22:52 +1000734 struct nouveau_mem *node = old_mem->mm_node;
Ben Skeggs3425df42011-02-10 11:22:12 +1000735
Ben Skeggsd2f966662011-06-06 20:54:42 +1000736 ret = nouveau_vma_getmap(chan, nvbo, old_mem, &node->vma[0]);
737 if (ret)
738 goto out;
Ben Skeggs3425df42011-02-10 11:22:12 +1000739
Ben Skeggsd2f966662011-06-06 20:54:42 +1000740 ret = nouveau_vma_getmap(chan, nvbo, new_mem, &node->vma[1]);
741 if (ret)
742 goto out;
Ben Skeggs3425df42011-02-10 11:22:12 +1000743 }
744
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000745 if (dev_priv->card_type < NV_50)
746 ret = nv04_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
747 else
Ben Skeggs183720b2010-12-09 15:17:10 +1000748 if (dev_priv->card_type < NV_C0)
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000749 ret = nv50_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
Ben Skeggs183720b2010-12-09 15:17:10 +1000750 else
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000751 if (dev_priv->card_type < NV_E0)
Ben Skeggs183720b2010-12-09 15:17:10 +1000752 ret = nvc0_bo_move_m2mf(chan, bo, &bo->mem, new_mem);
Ben Skeggsc6b7e892012-03-20 14:36:04 +1000753 else
754 ret = nve0_bo_move_copy(chan, bo, &bo->mem, new_mem);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000755 if (ret == 0) {
756 ret = nouveau_bo_move_accel_cleanup(chan, nvbo, evict,
757 no_wait_reserve,
758 no_wait_gpu, new_mem);
759 }
Ben Skeggsf1ab0cc2010-08-26 11:32:01 +1000760
Ben Skeggs3425df42011-02-10 11:22:12 +1000761out:
Ben Skeggsaccf9492012-03-16 12:40:17 +1000762 mutex_unlock(&chan->mutex);
Ben Skeggs6a6b73f2010-10-05 16:53:48 +1000763 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764}
765
766static int
767nouveau_bo_move_flipd(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000768 bool no_wait_reserve, bool no_wait_gpu,
769 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000770{
771 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
772 struct ttm_placement placement;
773 struct ttm_mem_reg tmp_mem;
774 int ret;
775
776 placement.fpfn = placement.lpfn = 0;
777 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +0100778 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000779
780 tmp_mem = *new_mem;
781 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000782 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000783 if (ret)
784 return ret;
785
786 ret = ttm_tt_bind(bo->ttm, &tmp_mem);
787 if (ret)
788 goto out;
789
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000790 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000791 if (ret)
792 goto out;
793
Ben Skeggsb8884da2011-02-14 13:51:28 +1000794 ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000795out:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000796 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000797 return ret;
798}
799
800static int
801nouveau_bo_move_flips(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000802 bool no_wait_reserve, bool no_wait_gpu,
803 struct ttm_mem_reg *new_mem)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000804{
805 u32 placement_memtype = TTM_PL_FLAG_TT | TTM_PL_MASK_CACHING;
806 struct ttm_placement placement;
807 struct ttm_mem_reg tmp_mem;
808 int ret;
809
810 placement.fpfn = placement.lpfn = 0;
811 placement.num_placement = placement.num_busy_placement = 1;
Francisco Jerez77e2b5e2009-12-16 19:05:00 +0100812 placement.placement = placement.busy_placement = &placement_memtype;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000813
814 tmp_mem = *new_mem;
815 tmp_mem.mm_node = NULL;
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000816 ret = ttm_bo_mem_space(bo, &placement, &tmp_mem, intr, no_wait_reserve, no_wait_gpu);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000817 if (ret)
818 return ret;
819
Ben Skeggsb8884da2011-02-14 13:51:28 +1000820 ret = ttm_bo_move_ttm(bo, true, no_wait_reserve, no_wait_gpu, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000821 if (ret)
822 goto out;
823
Ben Skeggsb8884da2011-02-14 13:51:28 +1000824 ret = nouveau_bo_move_m2mf(bo, true, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000825 if (ret)
826 goto out;
827
828out:
Ben Skeggs42311ff2010-08-04 12:07:08 +1000829 ttm_bo_mem_put(bo, &tmp_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000830 return ret;
831}
832
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000833static void
834nouveau_bo_move_ntfy(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem)
835{
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000836 struct nouveau_bo *nvbo = nouveau_bo(bo);
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000837 struct nouveau_vma *vma;
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000838
Ben Skeggs9f1feed2012-01-25 15:34:22 +1000839 /* ttm can now (stupidly) pass the driver bos it didn't create... */
840 if (bo->destroy != nouveau_bo_del_ttm)
841 return;
842
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000843 list_for_each_entry(vma, &nvbo->vma_list, head) {
Jerome Glissedc97b342011-11-18 11:47:03 -0500844 if (new_mem && new_mem->mem_type == TTM_PL_VRAM) {
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000845 nouveau_vm_map(vma, new_mem->mm_node);
846 } else
Jerome Glissedc97b342011-11-18 11:47:03 -0500847 if (new_mem && new_mem->mem_type == TTM_PL_TT &&
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000848 nvbo->page_shift == vma->vm->spg_shift) {
Dave Airlie22b33e82012-04-02 11:53:06 +0100849 if (((struct nouveau_mem *)new_mem->mm_node)->sg)
850 nouveau_vm_map_sg_table(vma, 0, new_mem->
851 num_pages << PAGE_SHIFT,
852 new_mem->mm_node);
853 else
854 nouveau_vm_map_sg(vma, 0, new_mem->
855 num_pages << PAGE_SHIFT,
856 new_mem->mm_node);
Ben Skeggsfd2871a2011-06-06 14:07:04 +1000857 } else {
858 nouveau_vm_unmap(vma);
859 }
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000860 }
861}
862
Ben Skeggs6ee73862009-12-11 19:24:15 +1000863static int
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100864nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct ttm_mem_reg *new_mem,
865 struct nouveau_tile_reg **new_tile)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000866{
867 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000868 struct drm_device *dev = dev_priv->dev;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100869 struct nouveau_bo *nvbo = nouveau_bo(bo);
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000870 u64 offset = new_mem->start << PAGE_SHIFT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000871
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000872 *new_tile = NULL;
873 if (new_mem->mem_type != TTM_PL_VRAM)
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100874 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000875
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000876 if (dev_priv->card_type >= NV_10) {
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100877 *new_tile = nv10_mem_set_tiling(dev, offset, new_mem->size,
Francisco Jereza5cf68b2010-10-24 16:14:41 +0200878 nvbo->tile_mode,
879 nvbo->tile_flags);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000880 }
881
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100882 return 0;
883}
Ben Skeggs6ee73862009-12-11 19:24:15 +1000884
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100885static void
886nouveau_bo_vm_cleanup(struct ttm_buffer_object *bo,
887 struct nouveau_tile_reg *new_tile,
888 struct nouveau_tile_reg **old_tile)
889{
890 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
891 struct drm_device *dev = dev_priv->dev;
892
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000893 nv10_mem_put_tile_region(dev, *old_tile, bo->sync_obj);
894 *old_tile = new_tile;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100895}
896
897static int
898nouveau_bo_move(struct ttm_buffer_object *bo, bool evict, bool intr,
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000899 bool no_wait_reserve, bool no_wait_gpu,
900 struct ttm_mem_reg *new_mem)
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100901{
902 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
903 struct nouveau_bo *nvbo = nouveau_bo(bo);
904 struct ttm_mem_reg *old_mem = &bo->mem;
905 struct nouveau_tile_reg *new_tile = NULL;
906 int ret = 0;
907
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000908 if (dev_priv->card_type < NV_50) {
909 ret = nouveau_bo_vm_bind(bo, new_mem, &new_tile);
910 if (ret)
911 return ret;
912 }
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100913
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100914 /* Fake bo copy. */
Ben Skeggs6ee73862009-12-11 19:24:15 +1000915 if (old_mem->mem_type == TTM_PL_SYSTEM && !bo->ttm) {
916 BUG_ON(bo->mem.mm_node != NULL);
917 bo->mem = *new_mem;
918 new_mem->mm_node = NULL;
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100919 goto out;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000920 }
921
Ben Skeggsb8a6a802010-08-27 11:55:43 +1000922 /* Software copy if the card isn't up and running yet. */
Ben Skeggs183720b2010-12-09 15:17:10 +1000923 if (!dev_priv->channel) {
Ben Skeggsb8a6a802010-08-27 11:55:43 +1000924 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
925 goto out;
926 }
927
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100928 /* Hardware assisted copy. */
929 if (new_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000930 ret = nouveau_bo_move_flipd(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100931 else if (old_mem->mem_type == TTM_PL_SYSTEM)
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000932 ret = nouveau_bo_move_flips(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100933 else
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000934 ret = nouveau_bo_move_m2mf(bo, evict, intr, no_wait_reserve, no_wait_gpu, new_mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000935
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100936 if (!ret)
937 goto out;
938
939 /* Fallback to software copy. */
Jerome Glisse9d87fa22010-04-07 10:21:19 +0000940 ret = ttm_bo_move_memcpy(bo, evict, no_wait_reserve, no_wait_gpu, new_mem);
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100941
942out:
Ben Skeggsa4154bb2011-02-10 10:35:16 +1000943 if (dev_priv->card_type < NV_50) {
944 if (ret)
945 nouveau_bo_vm_cleanup(bo, NULL, &new_tile);
946 else
947 nouveau_bo_vm_cleanup(bo, new_tile, &nvbo->tile);
948 }
Francisco Jereza0af9ad2009-12-11 16:51:09 +0100949
950 return ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000951}
952
953static int
954nouveau_bo_verify_access(struct ttm_buffer_object *bo, struct file *filp)
955{
956 return 0;
957}
958
Jerome Glissef32f02f2010-04-09 14:39:25 +0200959static int
960nouveau_ttm_io_mem_reserve(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
961{
962 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
963 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
964 struct drm_device *dev = dev_priv->dev;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000965 int ret;
Jerome Glissef32f02f2010-04-09 14:39:25 +0200966
967 mem->bus.addr = NULL;
968 mem->bus.offset = 0;
969 mem->bus.size = mem->num_pages << PAGE_SHIFT;
970 mem->bus.base = 0;
971 mem->bus.is_iomem = false;
972 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
973 return -EINVAL;
974 switch (mem->mem_type) {
975 case TTM_PL_SYSTEM:
976 /* System memory */
977 return 0;
978 case TTM_PL_TT:
979#if __OS_HAS_AGP
980 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
Ben Skeggsd961db72010-08-05 10:48:18 +1000981 mem->bus.offset = mem->start << PAGE_SHIFT;
Jerome Glissef32f02f2010-04-09 14:39:25 +0200982 mem->bus.base = dev_priv->gart_info.aper_base;
983 mem->bus.is_iomem = true;
984 }
985#endif
986 break;
987 case TTM_PL_VRAM:
Ben Skeggsf869ef82010-11-15 11:53:16 +1000988 {
Ben Skeggsd5f42392011-02-10 12:22:52 +1000989 struct nouveau_mem *node = mem->mm_node;
Ben Skeggs8984e042010-11-15 11:48:33 +1000990 u8 page_shift;
Ben Skeggsf869ef82010-11-15 11:53:16 +1000991
992 if (!dev_priv->bar1_vm) {
993 mem->bus.offset = mem->start << PAGE_SHIFT;
994 mem->bus.base = pci_resource_start(dev->pdev, 1);
995 mem->bus.is_iomem = true;
996 break;
997 }
998
Ben Skeggs2e9733f2011-07-02 20:28:49 +1000999 if (dev_priv->card_type >= NV_C0)
Ben Skeggsd5f42392011-02-10 12:22:52 +10001000 page_shift = node->page_shift;
Ben Skeggs8984e042010-11-15 11:48:33 +10001001 else
1002 page_shift = 12;
1003
Ben Skeggs4c74eb72010-11-10 14:10:04 +10001004 ret = nouveau_vm_get(dev_priv->bar1_vm, mem->bus.size,
Ben Skeggs8984e042010-11-15 11:48:33 +10001005 page_shift, NV_MEM_ACCESS_RW,
Ben Skeggsd5f42392011-02-10 12:22:52 +10001006 &node->bar_vma);
Ben Skeggsf869ef82010-11-15 11:53:16 +10001007 if (ret)
1008 return ret;
1009
Ben Skeggsd5f42392011-02-10 12:22:52 +10001010 nouveau_vm_map(&node->bar_vma, node);
Ben Skeggsf869ef82010-11-15 11:53:16 +10001011 if (ret) {
Ben Skeggsd5f42392011-02-10 12:22:52 +10001012 nouveau_vm_put(&node->bar_vma);
Ben Skeggsf869ef82010-11-15 11:53:16 +10001013 return ret;
1014 }
1015
Ben Skeggsd5f42392011-02-10 12:22:52 +10001016 mem->bus.offset = node->bar_vma.offset;
Ben Skeggs8984e042010-11-15 11:48:33 +10001017 if (dev_priv->card_type == NV_50) /*XXX*/
1018 mem->bus.offset -= 0x0020000000ULL;
Jordan Crouse01d73a62010-05-27 13:40:24 -06001019 mem->bus.base = pci_resource_start(dev->pdev, 1);
Jerome Glissef32f02f2010-04-09 14:39:25 +02001020 mem->bus.is_iomem = true;
Ben Skeggsf869ef82010-11-15 11:53:16 +10001021 }
Jerome Glissef32f02f2010-04-09 14:39:25 +02001022 break;
1023 default:
1024 return -EINVAL;
1025 }
1026 return 0;
1027}
1028
1029static void
1030nouveau_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
1031{
Ben Skeggsf869ef82010-11-15 11:53:16 +10001032 struct drm_nouveau_private *dev_priv = nouveau_bdev(bdev);
Ben Skeggsd5f42392011-02-10 12:22:52 +10001033 struct nouveau_mem *node = mem->mm_node;
Ben Skeggsf869ef82010-11-15 11:53:16 +10001034
1035 if (!dev_priv->bar1_vm || mem->mem_type != TTM_PL_VRAM)
1036 return;
1037
Ben Skeggsd5f42392011-02-10 12:22:52 +10001038 if (!node->bar_vma.node)
Ben Skeggsf869ef82010-11-15 11:53:16 +10001039 return;
1040
Ben Skeggsd5f42392011-02-10 12:22:52 +10001041 nouveau_vm_unmap(&node->bar_vma);
1042 nouveau_vm_put(&node->bar_vma);
Jerome Glissef32f02f2010-04-09 14:39:25 +02001043}
1044
1045static int
1046nouveau_ttm_fault_reserve_notify(struct ttm_buffer_object *bo)
1047{
Ben Skeggse1429b42010-09-10 11:12:25 +10001048 struct drm_nouveau_private *dev_priv = nouveau_bdev(bo->bdev);
1049 struct nouveau_bo *nvbo = nouveau_bo(bo);
1050
1051 /* as long as the bo isn't in vram, and isn't tiled, we've got
1052 * nothing to do here.
1053 */
1054 if (bo->mem.mem_type != TTM_PL_VRAM) {
Francisco Jerezf13b3262010-10-10 06:01:08 +02001055 if (dev_priv->card_type < NV_50 ||
1056 !nouveau_bo_tile_layout(nvbo))
Ben Skeggse1429b42010-09-10 11:12:25 +10001057 return 0;
1058 }
1059
1060 /* make sure bo is in mappable vram */
Ben Skeggsd961db72010-08-05 10:48:18 +10001061 if (bo->mem.start + bo->mem.num_pages < dev_priv->fb_mappable_pages)
Ben Skeggse1429b42010-09-10 11:12:25 +10001062 return 0;
1063
1064
1065 nvbo->placement.fpfn = 0;
1066 nvbo->placement.lpfn = dev_priv->fb_mappable_pages;
Dave Airliec2848152012-05-18 15:31:12 +01001067 nouveau_bo_placement_set(nvbo, TTM_PL_FLAG_VRAM, 0);
Ben Skeggs7a45d762010-11-22 08:50:27 +10001068 return nouveau_bo_validate(nvbo, false, true, false);
Jerome Glissef32f02f2010-04-09 14:39:25 +02001069}
1070
Francisco Jerez332b2422010-10-20 23:35:40 +02001071void
1072nouveau_bo_fence(struct nouveau_bo *nvbo, struct nouveau_fence *fence)
1073{
Francisco Jerez23c45e82010-10-28 23:10:29 +02001074 struct nouveau_fence *old_fence;
Francisco Jerez332b2422010-10-20 23:35:40 +02001075
1076 if (likely(fence))
Francisco Jerez23c45e82010-10-28 23:10:29 +02001077 nouveau_fence_ref(fence);
Francisco Jerez332b2422010-10-20 23:35:40 +02001078
Francisco Jerez23c45e82010-10-28 23:10:29 +02001079 spin_lock(&nvbo->bo.bdev->fence_lock);
1080 old_fence = nvbo->bo.sync_obj;
1081 nvbo->bo.sync_obj = fence;
Francisco Jerez332b2422010-10-20 23:35:40 +02001082 spin_unlock(&nvbo->bo.bdev->fence_lock);
Francisco Jerez23c45e82010-10-28 23:10:29 +02001083
1084 nouveau_fence_unref(&old_fence);
Francisco Jerez332b2422010-10-20 23:35:40 +02001085}
1086
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001087static int
1088nouveau_ttm_tt_populate(struct ttm_tt *ttm)
1089{
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001090 struct ttm_dma_tt *ttm_dma = (void *)ttm;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001091 struct drm_nouveau_private *dev_priv;
1092 struct drm_device *dev;
1093 unsigned i;
1094 int r;
Dave Airlie22b33e82012-04-02 11:53:06 +01001095 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001096
1097 if (ttm->state != tt_unpopulated)
1098 return 0;
1099
Dave Airlie22b33e82012-04-02 11:53:06 +01001100 if (slave && ttm->sg) {
1101 /* make userspace faulting work */
1102 drm_prime_sg_to_page_addr_arrays(ttm->sg, ttm->pages,
1103 ttm_dma->dma_address, ttm->num_pages);
1104 ttm->state = tt_unbound;
1105 return 0;
1106 }
1107
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001108 dev_priv = nouveau_bdev(ttm->bdev);
1109 dev = dev_priv->dev;
1110
Jerome Glissedea7e0a2012-01-03 17:37:37 -05001111#if __OS_HAS_AGP
1112 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
1113 return ttm_agp_tt_populate(ttm);
1114 }
1115#endif
1116
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001117#ifdef CONFIG_SWIOTLB
1118 if (swiotlb_nr_tbl()) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001119 return ttm_dma_populate((void *)ttm, dev->dev);
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001120 }
1121#endif
1122
1123 r = ttm_pool_populate(ttm);
1124 if (r) {
1125 return r;
1126 }
1127
1128 for (i = 0; i < ttm->num_pages; i++) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001129 ttm_dma->dma_address[i] = pci_map_page(dev->pdev, ttm->pages[i],
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001130 0, PAGE_SIZE,
1131 PCI_DMA_BIDIRECTIONAL);
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001132 if (pci_dma_mapping_error(dev->pdev, ttm_dma->dma_address[i])) {
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001133 while (--i) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001134 pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001135 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001136 ttm_dma->dma_address[i] = 0;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001137 }
1138 ttm_pool_unpopulate(ttm);
1139 return -EFAULT;
1140 }
1141 }
1142 return 0;
1143}
1144
1145static void
1146nouveau_ttm_tt_unpopulate(struct ttm_tt *ttm)
1147{
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001148 struct ttm_dma_tt *ttm_dma = (void *)ttm;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001149 struct drm_nouveau_private *dev_priv;
1150 struct drm_device *dev;
1151 unsigned i;
Dave Airlie22b33e82012-04-02 11:53:06 +01001152 bool slave = !!(ttm->page_flags & TTM_PAGE_FLAG_SG);
1153
1154 if (slave)
1155 return;
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001156
1157 dev_priv = nouveau_bdev(ttm->bdev);
1158 dev = dev_priv->dev;
1159
Jerome Glissedea7e0a2012-01-03 17:37:37 -05001160#if __OS_HAS_AGP
1161 if (dev_priv->gart_info.type == NOUVEAU_GART_AGP) {
1162 ttm_agp_tt_unpopulate(ttm);
1163 return;
1164 }
1165#endif
1166
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001167#ifdef CONFIG_SWIOTLB
1168 if (swiotlb_nr_tbl()) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001169 ttm_dma_unpopulate((void *)ttm, dev->dev);
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001170 return;
1171 }
1172#endif
1173
1174 for (i = 0; i < ttm->num_pages; i++) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -05001175 if (ttm_dma->dma_address[i]) {
1176 pci_unmap_page(dev->pdev, ttm_dma->dma_address[i],
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001177 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
1178 }
1179 }
1180
1181 ttm_pool_unpopulate(ttm);
1182}
1183
Ben Skeggs6ee73862009-12-11 19:24:15 +10001184struct ttm_bo_driver nouveau_bo_driver = {
Jerome Glisse649bf3c2011-11-01 20:46:13 -04001185 .ttm_tt_create = &nouveau_ttm_tt_create,
Konrad Rzeszutek Wilk3230cfc2011-10-17 17:14:26 -04001186 .ttm_tt_populate = &nouveau_ttm_tt_populate,
1187 .ttm_tt_unpopulate = &nouveau_ttm_tt_unpopulate,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001188 .invalidate_caches = nouveau_bo_invalidate_caches,
1189 .init_mem_type = nouveau_bo_init_mem_type,
1190 .evict_flags = nouveau_bo_evict_flags,
Ben Skeggsa4154bb2011-02-10 10:35:16 +10001191 .move_notify = nouveau_bo_move_ntfy,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001192 .move = nouveau_bo_move,
1193 .verify_access = nouveau_bo_verify_access,
Marcin Slusarz382d62e2010-10-20 21:50:24 +02001194 .sync_obj_signaled = __nouveau_fence_signalled,
1195 .sync_obj_wait = __nouveau_fence_wait,
1196 .sync_obj_flush = __nouveau_fence_flush,
1197 .sync_obj_unref = __nouveau_fence_unref,
1198 .sync_obj_ref = __nouveau_fence_ref,
Jerome Glissef32f02f2010-04-09 14:39:25 +02001199 .fault_reserve_notify = &nouveau_ttm_fault_reserve_notify,
1200 .io_mem_reserve = &nouveau_ttm_io_mem_reserve,
1201 .io_mem_free = &nouveau_ttm_io_mem_free,
Ben Skeggs6ee73862009-12-11 19:24:15 +10001202};
1203
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001204struct nouveau_vma *
1205nouveau_bo_vma_find(struct nouveau_bo *nvbo, struct nouveau_vm *vm)
1206{
1207 struct nouveau_vma *vma;
1208 list_for_each_entry(vma, &nvbo->vma_list, head) {
1209 if (vma->vm == vm)
1210 return vma;
1211 }
1212
1213 return NULL;
1214}
1215
1216int
1217nouveau_bo_vma_add(struct nouveau_bo *nvbo, struct nouveau_vm *vm,
1218 struct nouveau_vma *vma)
1219{
1220 const u32 size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
1221 struct nouveau_mem *node = nvbo->bo.mem.mm_node;
1222 int ret;
1223
1224 ret = nouveau_vm_get(vm, size, nvbo->page_shift,
1225 NV_MEM_ACCESS_RW, vma);
1226 if (ret)
1227 return ret;
1228
1229 if (nvbo->bo.mem.mem_type == TTM_PL_VRAM)
1230 nouveau_vm_map(vma, nvbo->bo.mem.mm_node);
Dave Airlie22b33e82012-04-02 11:53:06 +01001231 else if (nvbo->bo.mem.mem_type == TTM_PL_TT) {
1232 if (node->sg)
1233 nouveau_vm_map_sg_table(vma, 0, size, node);
1234 else
1235 nouveau_vm_map_sg(vma, 0, size, node);
1236 }
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001237
1238 list_add_tail(&vma->head, &nvbo->vma_list);
Ben Skeggs2fd3db62011-06-07 15:25:12 +10001239 vma->refcount = 1;
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001240 return 0;
1241}
1242
1243void
1244nouveau_bo_vma_del(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
1245{
1246 if (vma->node) {
1247 if (nvbo->bo.mem.mem_type != TTM_PL_SYSTEM) {
1248 spin_lock(&nvbo->bo.bdev->fence_lock);
Dave Airlie1717c0e2011-10-27 18:28:37 +02001249 ttm_bo_wait(&nvbo->bo, false, false, false);
Ben Skeggsfd2871a2011-06-06 14:07:04 +10001250 spin_unlock(&nvbo->bo.bdev->fence_lock);
1251 nouveau_vm_unmap(vma);
1252 }
1253
1254 nouveau_vm_put(vma);
1255 list_del(&vma->head);
1256 }
1257}