Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 1 | |
| 2 | #include "drmP.h" |
| 3 | #include "drm.h" |
| 4 | |
| 5 | #include "nouveau_drv.h" |
| 6 | #include "nouveau_drm.h" |
| 7 | #include "nouveau_dma.h" |
| 8 | |
| 9 | #include <linux/dma-buf.h> |
| 10 | |
| 11 | static struct sg_table *nouveau_gem_map_dma_buf(struct dma_buf_attachment *attachment, |
| 12 | enum dma_data_direction dir) |
| 13 | { |
| 14 | struct nouveau_bo *nvbo = attachment->dmabuf->priv; |
| 15 | struct drm_device *dev = nvbo->gem->dev; |
| 16 | int npages = nvbo->bo.num_pages; |
| 17 | struct sg_table *sg; |
| 18 | int nents; |
| 19 | |
| 20 | mutex_lock(&dev->struct_mutex); |
| 21 | sg = drm_prime_pages_to_sg(nvbo->bo.ttm->pages, npages); |
| 22 | nents = dma_map_sg(attachment->dev, sg->sgl, sg->nents, dir); |
| 23 | mutex_unlock(&dev->struct_mutex); |
| 24 | return sg; |
| 25 | } |
| 26 | |
| 27 | static void nouveau_gem_unmap_dma_buf(struct dma_buf_attachment *attachment, |
| 28 | struct sg_table *sg, enum dma_data_direction dir) |
| 29 | { |
| 30 | dma_unmap_sg(attachment->dev, sg->sgl, sg->nents, dir); |
| 31 | sg_free_table(sg); |
| 32 | kfree(sg); |
| 33 | } |
| 34 | |
| 35 | static void nouveau_gem_dmabuf_release(struct dma_buf *dma_buf) |
| 36 | { |
| 37 | struct nouveau_bo *nvbo = dma_buf->priv; |
| 38 | |
| 39 | if (nvbo->gem->export_dma_buf == dma_buf) { |
| 40 | nvbo->gem->export_dma_buf = NULL; |
| 41 | drm_gem_object_unreference_unlocked(nvbo->gem); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | static void *nouveau_gem_kmap_atomic(struct dma_buf *dma_buf, unsigned long page_num) |
| 46 | { |
| 47 | return NULL; |
| 48 | } |
| 49 | |
| 50 | static void nouveau_gem_kunmap_atomic(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 51 | { |
| 52 | |
| 53 | } |
| 54 | static void *nouveau_gem_kmap(struct dma_buf *dma_buf, unsigned long page_num) |
| 55 | { |
| 56 | return NULL; |
| 57 | } |
| 58 | |
| 59 | static void nouveau_gem_kunmap(struct dma_buf *dma_buf, unsigned long page_num, void *addr) |
| 60 | { |
| 61 | |
| 62 | } |
| 63 | |
Dave Airlie | e1bbc4b | 2012-05-29 15:11:55 +0100 | [diff] [blame] | 64 | static int nouveau_gem_prime_mmap(struct dma_buf *dma_buf, struct vm_area_struct *vma) |
| 65 | { |
| 66 | return -EINVAL; |
| 67 | } |
| 68 | |
Dave Airlie | 35916ac | 2012-05-31 13:52:17 +0100 | [diff] [blame] | 69 | static void *nouveau_gem_prime_vmap(struct dma_buf *dma_buf) |
| 70 | { |
| 71 | struct nouveau_bo *nvbo = dma_buf->priv; |
| 72 | struct drm_device *dev = nvbo->gem->dev; |
| 73 | int ret; |
| 74 | |
| 75 | mutex_lock(&dev->struct_mutex); |
| 76 | if (nvbo->vmapping_count) { |
| 77 | nvbo->vmapping_count++; |
| 78 | goto out_unlock; |
| 79 | } |
| 80 | |
| 81 | ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.num_pages, |
| 82 | &nvbo->dma_buf_vmap); |
| 83 | if (ret) { |
| 84 | mutex_unlock(&dev->struct_mutex); |
| 85 | return ERR_PTR(ret); |
| 86 | } |
| 87 | nvbo->vmapping_count = 1; |
| 88 | out_unlock: |
| 89 | mutex_unlock(&dev->struct_mutex); |
| 90 | return nvbo->dma_buf_vmap.virtual; |
| 91 | } |
| 92 | |
| 93 | static void nouveau_gem_prime_vunmap(struct dma_buf *dma_buf, void *vaddr) |
| 94 | { |
| 95 | struct nouveau_bo *nvbo = dma_buf->priv; |
| 96 | struct drm_device *dev = nvbo->gem->dev; |
| 97 | |
| 98 | mutex_lock(&dev->struct_mutex); |
| 99 | nvbo->vmapping_count--; |
| 100 | if (nvbo->vmapping_count == 0) { |
| 101 | ttm_bo_kunmap(&nvbo->dma_buf_vmap); |
| 102 | } |
| 103 | mutex_unlock(&dev->struct_mutex); |
| 104 | } |
| 105 | |
Dave Airlie | 41ceeeb | 2012-05-23 14:10:27 +0100 | [diff] [blame] | 106 | static const struct dma_buf_ops nouveau_dmabuf_ops = { |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 107 | .map_dma_buf = nouveau_gem_map_dma_buf, |
| 108 | .unmap_dma_buf = nouveau_gem_unmap_dma_buf, |
| 109 | .release = nouveau_gem_dmabuf_release, |
| 110 | .kmap = nouveau_gem_kmap, |
| 111 | .kmap_atomic = nouveau_gem_kmap_atomic, |
| 112 | .kunmap = nouveau_gem_kunmap, |
| 113 | .kunmap_atomic = nouveau_gem_kunmap_atomic, |
Dave Airlie | e1bbc4b | 2012-05-29 15:11:55 +0100 | [diff] [blame] | 114 | .mmap = nouveau_gem_prime_mmap, |
Dave Airlie | 35916ac | 2012-05-31 13:52:17 +0100 | [diff] [blame] | 115 | .vmap = nouveau_gem_prime_vmap, |
| 116 | .vunmap = nouveau_gem_prime_vunmap, |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | static int |
| 120 | nouveau_prime_new(struct drm_device *dev, |
| 121 | size_t size, |
| 122 | struct sg_table *sg, |
| 123 | struct nouveau_bo **pnvbo) |
| 124 | { |
| 125 | struct nouveau_bo *nvbo; |
| 126 | u32 flags = 0; |
| 127 | int ret; |
| 128 | |
| 129 | flags = TTM_PL_FLAG_TT; |
| 130 | |
| 131 | ret = nouveau_bo_new(dev, size, 0, flags, 0, 0, |
| 132 | sg, pnvbo); |
| 133 | if (ret) |
| 134 | return ret; |
| 135 | nvbo = *pnvbo; |
| 136 | |
| 137 | /* we restrict allowed domains on nv50+ to only the types |
| 138 | * that were requested at creation time. not possibly on |
| 139 | * earlier chips without busting the ABI. |
| 140 | */ |
| 141 | nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_GART; |
| 142 | nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size); |
| 143 | if (!nvbo->gem) { |
| 144 | nouveau_bo_ref(NULL, pnvbo); |
| 145 | return -ENOMEM; |
| 146 | } |
| 147 | |
| 148 | nvbo->gem->driver_private = nvbo; |
| 149 | return 0; |
| 150 | } |
| 151 | |
| 152 | struct dma_buf *nouveau_gem_prime_export(struct drm_device *dev, |
| 153 | struct drm_gem_object *obj, int flags) |
| 154 | { |
| 155 | struct nouveau_bo *nvbo = nouveau_gem_object(obj); |
| 156 | int ret = 0; |
| 157 | |
| 158 | /* pin buffer into GTT */ |
| 159 | ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_TT); |
| 160 | if (ret) |
| 161 | return ERR_PTR(-EINVAL); |
| 162 | |
| 163 | return dma_buf_export(nvbo, &nouveau_dmabuf_ops, obj->size, flags); |
| 164 | } |
| 165 | |
| 166 | struct drm_gem_object *nouveau_gem_prime_import(struct drm_device *dev, |
| 167 | struct dma_buf *dma_buf) |
| 168 | { |
| 169 | struct dma_buf_attachment *attach; |
| 170 | struct sg_table *sg; |
| 171 | struct nouveau_bo *nvbo; |
| 172 | int ret; |
| 173 | |
| 174 | if (dma_buf->ops == &nouveau_dmabuf_ops) { |
| 175 | nvbo = dma_buf->priv; |
| 176 | if (nvbo->gem) { |
| 177 | if (nvbo->gem->dev == dev) { |
| 178 | drm_gem_object_reference(nvbo->gem); |
| 179 | return nvbo->gem; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | /* need to attach */ |
| 184 | attach = dma_buf_attach(dma_buf, dev->dev); |
| 185 | if (IS_ERR(attach)) |
| 186 | return ERR_PTR(PTR_ERR(attach)); |
| 187 | |
| 188 | sg = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); |
| 189 | if (IS_ERR(sg)) { |
| 190 | ret = PTR_ERR(sg); |
| 191 | goto fail_detach; |
| 192 | } |
| 193 | |
| 194 | ret = nouveau_prime_new(dev, dma_buf->size, sg, &nvbo); |
| 195 | if (ret) |
| 196 | goto fail_unmap; |
| 197 | |
| 198 | nvbo->gem->import_attach = attach; |
| 199 | |
| 200 | return nvbo->gem; |
| 201 | |
| 202 | fail_unmap: |
| 203 | dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL); |
| 204 | fail_detach: |
| 205 | dma_buf_detach(dma_buf, attach); |
| 206 | return ERR_PTR(ret); |
| 207 | } |
| 208 | |