Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1 | #include <linux/pagemap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 2 | #include <linux/slab.h> |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 3 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 4 | #include "nouveau_drm.h" |
| 5 | #include "nouveau_ttm.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 6 | |
| 7 | struct nouveau_sgdma_be { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 8 | /* this has to be the first field so populate/unpopulated in |
| 9 | * nouve_bo.c works properly, otherwise have to move them here |
| 10 | */ |
| 11 | struct ttm_dma_tt ttm; |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 12 | struct nvkm_mem *node; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 13 | }; |
| 14 | |
Ben Skeggs | efa58db | 2011-01-10 16:24:00 +1000 | [diff] [blame] | 15 | static void |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 16 | nouveau_sgdma_destroy(struct ttm_tt *ttm) |
Ben Skeggs | efa58db | 2011-01-10 16:24:00 +1000 | [diff] [blame] | 17 | { |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 18 | struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm; |
Ben Skeggs | efa58db | 2011-01-10 16:24:00 +1000 | [diff] [blame] | 19 | |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 20 | if (ttm) { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 21 | ttm_dma_tt_fini(&nvbe->ttm); |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 22 | kfree(nvbe); |
Ben Skeggs | efa58db | 2011-01-10 16:24:00 +1000 | [diff] [blame] | 23 | } |
| 24 | } |
| 25 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 26 | static int |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 27 | nv04_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 28 | { |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 29 | struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm; |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 30 | struct nvkm_mem *node = mem->mm_node; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 31 | |
Ben Skeggs | 3863c9b | 2012-07-14 19:09:17 +1000 | [diff] [blame] | 32 | if (ttm->sg) { |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 33 | node->sg = ttm->sg; |
| 34 | node->pages = NULL; |
Ben Skeggs | 3863c9b | 2012-07-14 19:09:17 +1000 | [diff] [blame] | 35 | } else { |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 36 | node->sg = NULL; |
Ben Skeggs | 3863c9b | 2012-07-14 19:09:17 +1000 | [diff] [blame] | 37 | node->pages = nvbe->ttm.dma_address; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 38 | } |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 39 | node->size = (mem->num_pages << PAGE_SHIFT) >> 12; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 40 | |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 41 | nvkm_vm_map(&node->vma[0], node); |
Ben Skeggs | 3863c9b | 2012-07-14 19:09:17 +1000 | [diff] [blame] | 42 | nvbe->node = node; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | static int |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 47 | nv04_sgdma_unbind(struct ttm_tt *ttm) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 48 | { |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 49 | struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm; |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 50 | nvkm_vm_unmap(&nvbe->node->vma[0]); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 51 | return 0; |
| 52 | } |
| 53 | |
Ben Skeggs | efa58db | 2011-01-10 16:24:00 +1000 | [diff] [blame] | 54 | static struct ttm_backend_func nv04_sgdma_backend = { |
Ben Skeggs | efa58db | 2011-01-10 16:24:00 +1000 | [diff] [blame] | 55 | .bind = nv04_sgdma_bind, |
| 56 | .unbind = nv04_sgdma_unbind, |
| 57 | .destroy = nouveau_sgdma_destroy |
| 58 | }; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 59 | |
Ben Skeggs | b571fe2 | 2010-11-16 10:13:05 +1000 | [diff] [blame] | 60 | static int |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 61 | nv50_sgdma_bind(struct ttm_tt *ttm, struct ttm_mem_reg *mem) |
Ben Skeggs | b571fe2 | 2010-11-16 10:13:05 +1000 | [diff] [blame] | 62 | { |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 63 | struct nouveau_sgdma_be *nvbe = (struct nouveau_sgdma_be *)ttm; |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 64 | struct nvkm_mem *node = mem->mm_node; |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 65 | |
Ben Skeggs | 26c0c9e | 2011-02-10 12:59:51 +1000 | [diff] [blame] | 66 | /* noop: bound in move_notify() */ |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 67 | if (ttm->sg) { |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 68 | node->sg = ttm->sg; |
| 69 | node->pages = NULL; |
| 70 | } else { |
| 71 | node->sg = NULL; |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 72 | node->pages = nvbe->ttm.dma_address; |
Ben Skeggs | 2e2cfbe | 2013-11-15 11:56:49 +1000 | [diff] [blame] | 73 | } |
| 74 | node->size = (mem->num_pages << PAGE_SHIFT) >> 12; |
Ben Skeggs | b571fe2 | 2010-11-16 10:13:05 +1000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 79 | nv50_sgdma_unbind(struct ttm_tt *ttm) |
Ben Skeggs | b571fe2 | 2010-11-16 10:13:05 +1000 | [diff] [blame] | 80 | { |
Ben Skeggs | 26c0c9e | 2011-02-10 12:59:51 +1000 | [diff] [blame] | 81 | /* noop: unbound in move_notify() */ |
Ben Skeggs | b571fe2 | 2010-11-16 10:13:05 +1000 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
Ben Skeggs | b571fe2 | 2010-11-16 10:13:05 +1000 | [diff] [blame] | 85 | static struct ttm_backend_func nv50_sgdma_backend = { |
Ben Skeggs | b571fe2 | 2010-11-16 10:13:05 +1000 | [diff] [blame] | 86 | .bind = nv50_sgdma_bind, |
| 87 | .unbind = nv50_sgdma_unbind, |
| 88 | .destroy = nouveau_sgdma_destroy |
| 89 | }; |
| 90 | |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 91 | struct ttm_tt * |
| 92 | nouveau_sgdma_create_ttm(struct ttm_bo_device *bdev, |
| 93 | unsigned long size, uint32_t page_flags, |
| 94 | struct page *dummy_read_page) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 95 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 96 | struct nouveau_drm *drm = nouveau_bdev(bdev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 97 | struct nouveau_sgdma_be *nvbe; |
| 98 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 99 | nvbe = kzalloc(sizeof(*nvbe), GFP_KERNEL); |
| 100 | if (!nvbe) |
| 101 | return NULL; |
| 102 | |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 103 | if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 104 | nvbe->ttm.ttm.func = &nv04_sgdma_backend; |
| 105 | else |
| 106 | nvbe->ttm.ttm.func = &nv50_sgdma_backend; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 107 | |
Ben Skeggs | 7a59cc3 | 2013-09-17 14:13:32 +1000 | [diff] [blame] | 108 | if (ttm_dma_tt_init(&nvbe->ttm, bdev, size, page_flags, dummy_read_page)) |
Alexandre Courbot | 495b217 | 2014-12-11 04:07:20 +0900 | [diff] [blame] | 109 | /* |
| 110 | * A failing ttm_dma_tt_init() will call ttm_tt_destroy() |
| 111 | * and thus our nouveau_sgdma_destroy() hook, so we don't need |
| 112 | * to free nvbe here. |
| 113 | */ |
Jerome Glisse | 649bf3c | 2011-11-01 20:46:13 -0400 | [diff] [blame] | 114 | return NULL; |
Jerome Glisse | 8e7e705 | 2011-11-09 17:15:26 -0500 | [diff] [blame] | 115 | return &nvbe->ttm.ttm; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 116 | } |