Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 Ben Skeggs. |
| 3 | * All Rights Reserved. |
| 4 | * |
| 5 | * Permission is hereby granted, free of charge, to any person obtaining |
| 6 | * a copy of this software and associated documentation files (the |
| 7 | * "Software"), to deal in the Software without restriction, including |
| 8 | * without limitation the rights to use, copy, modify, merge, publish, |
| 9 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 10 | * permit persons to whom the Software is furnished to do so, subject to |
| 11 | * the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice (including the |
| 14 | * next paragraph) shall be included in all copies or substantial |
| 15 | * portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 | * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE |
| 21 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 22 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 23 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 24 | * |
| 25 | */ |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 26 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 27 | #include <subdev/fb.h> |
| 28 | |
| 29 | #include "nouveau_drm.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 30 | #include "nouveau_dma.h" |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 31 | #include "nouveau_fence.h" |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 32 | #include "nouveau_abi16.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 33 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 34 | #include "nouveau_ttm.h" |
| 35 | #include "nouveau_gem.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 36 | |
| 37 | int |
| 38 | nouveau_gem_object_new(struct drm_gem_object *gem) |
| 39 | { |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | void |
| 44 | nouveau_gem_object_del(struct drm_gem_object *gem) |
| 45 | { |
| 46 | struct nouveau_bo *nvbo = gem->driver_private; |
| 47 | struct ttm_buffer_object *bo = &nvbo->bo; |
| 48 | |
| 49 | if (!nvbo) |
| 50 | return; |
| 51 | nvbo->gem = NULL; |
| 52 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 53 | if (unlikely(nvbo->pin_refcnt)) { |
| 54 | nvbo->pin_refcnt = 1; |
| 55 | nouveau_bo_unpin(nvbo); |
| 56 | } |
| 57 | |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 58 | if (gem->import_attach) |
| 59 | drm_prime_gem_destroy(gem, nvbo->bo.sg); |
| 60 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 61 | ttm_bo_unref(&bo); |
Daniel Vetter | fd632aa | 2010-04-09 19:05:05 +0000 | [diff] [blame] | 62 | |
| 63 | drm_gem_object_release(gem); |
| 64 | kfree(gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | int |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 68 | nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv) |
| 69 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 70 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 71 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
| 72 | struct nouveau_vma *vma; |
| 73 | int ret; |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 74 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 75 | if (!cli->base.vm) |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 76 | return 0; |
| 77 | |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 78 | ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0); |
| 79 | if (ret) |
| 80 | return ret; |
| 81 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 82 | vma = nouveau_bo_vma_find(nvbo, cli->base.vm); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 83 | if (!vma) { |
| 84 | vma = kzalloc(sizeof(*vma), GFP_KERNEL); |
| 85 | if (!vma) { |
| 86 | ret = -ENOMEM; |
| 87 | goto out; |
| 88 | } |
| 89 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 90 | ret = nouveau_bo_vma_add(nvbo, cli->base.vm, vma); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 91 | if (ret) { |
| 92 | kfree(vma); |
| 93 | goto out; |
| 94 | } |
| 95 | } else { |
| 96 | vma->refcount++; |
| 97 | } |
| 98 | |
| 99 | out: |
| 100 | ttm_bo_unreserve(&nvbo->bo); |
| 101 | return ret; |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 102 | } |
| 103 | |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame^] | 104 | static void |
| 105 | nouveau_gem_object_delete(void *data) |
| 106 | { |
| 107 | struct nouveau_vma *vma = data; |
| 108 | nouveau_vm_unmap(vma); |
| 109 | nouveau_vm_put(vma); |
| 110 | kfree(vma); |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma) |
| 115 | { |
| 116 | const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM; |
| 117 | struct nouveau_fence *fence = NULL; |
| 118 | |
| 119 | list_del(&vma->head); |
| 120 | |
| 121 | if (mapped) { |
| 122 | spin_lock(&nvbo->bo.bdev->fence_lock); |
| 123 | if (nvbo->bo.sync_obj) |
| 124 | fence = nouveau_fence_ref(nvbo->bo.sync_obj); |
| 125 | spin_unlock(&nvbo->bo.bdev->fence_lock); |
| 126 | } |
| 127 | |
| 128 | if (fence) { |
| 129 | nouveau_fence_work(fence, nouveau_gem_object_delete, vma); |
| 130 | } else { |
| 131 | if (mapped) |
| 132 | nouveau_vm_unmap(vma); |
| 133 | nouveau_vm_put(vma); |
| 134 | kfree(vma); |
| 135 | } |
| 136 | nouveau_fence_unref(&fence); |
| 137 | } |
| 138 | |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 139 | void |
| 140 | nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv) |
| 141 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 142 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 143 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
| 144 | struct nouveau_vma *vma; |
| 145 | int ret; |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 146 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 147 | if (!cli->base.vm) |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 148 | return; |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 149 | |
| 150 | ret = ttm_bo_reserve(&nvbo->bo, false, false, false, 0); |
| 151 | if (ret) |
| 152 | return; |
| 153 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 154 | vma = nouveau_bo_vma_find(nvbo, cli->base.vm); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 155 | if (vma) { |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame^] | 156 | if (--vma->refcount == 0) |
| 157 | nouveau_gem_object_unmap(nvbo, vma); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 158 | } |
| 159 | ttm_bo_unreserve(&nvbo->bo); |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | int |
Ben Skeggs | f6d4e62 | 2011-06-07 12:25:36 +1000 | [diff] [blame] | 163 | nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain, |
| 164 | uint32_t tile_mode, uint32_t tile_flags, |
| 165 | struct nouveau_bo **pnvbo) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 166 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 167 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 168 | struct nouveau_bo *nvbo; |
Ben Skeggs | 6ba9a68 | 2011-02-10 14:42:08 +1000 | [diff] [blame] | 169 | u32 flags = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 170 | int ret; |
| 171 | |
Ben Skeggs | 6ba9a68 | 2011-02-10 14:42:08 +1000 | [diff] [blame] | 172 | if (domain & NOUVEAU_GEM_DOMAIN_VRAM) |
| 173 | flags |= TTM_PL_FLAG_VRAM; |
| 174 | if (domain & NOUVEAU_GEM_DOMAIN_GART) |
| 175 | flags |= TTM_PL_FLAG_TT; |
| 176 | if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU) |
| 177 | flags |= TTM_PL_FLAG_SYSTEM; |
| 178 | |
Ben Skeggs | 7375c95 | 2011-06-07 14:21:29 +1000 | [diff] [blame] | 179 | ret = nouveau_bo_new(dev, size, align, flags, tile_mode, |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 180 | tile_flags, NULL, pnvbo); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 181 | if (ret) |
| 182 | return ret; |
| 183 | nvbo = *pnvbo; |
| 184 | |
Ben Skeggs | db5c8e2 | 2011-02-10 13:41:01 +1000 | [diff] [blame] | 185 | /* we restrict allowed domains on nv50+ to only the types |
| 186 | * that were requested at creation time. not possibly on |
| 187 | * earlier chips without busting the ABI. |
| 188 | */ |
| 189 | nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM | |
| 190 | NOUVEAU_GEM_DOMAIN_GART; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 191 | if (nv_device(drm->device)->card_type >= NV_50) |
Ben Skeggs | db5c8e2 | 2011-02-10 13:41:01 +1000 | [diff] [blame] | 192 | nvbo->valid_domains &= domain; |
| 193 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 194 | nvbo->gem = drm_gem_object_alloc(dev, nvbo->bo.mem.size); |
| 195 | if (!nvbo->gem) { |
| 196 | nouveau_bo_ref(NULL, pnvbo); |
| 197 | return -ENOMEM; |
| 198 | } |
| 199 | |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 200 | nvbo->bo.persistent_swap_storage = nvbo->gem->filp; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 201 | nvbo->gem->driver_private = nvbo; |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 206 | nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem, |
| 207 | struct drm_nouveau_gem_info *rep) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 208 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 209 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 210 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 211 | struct nouveau_vma *vma; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 212 | |
| 213 | if (nvbo->bo.mem.mem_type == TTM_PL_TT) |
| 214 | rep->domain = NOUVEAU_GEM_DOMAIN_GART; |
| 215 | else |
| 216 | rep->domain = NOUVEAU_GEM_DOMAIN_VRAM; |
| 217 | |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 218 | rep->offset = nvbo->bo.offset; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 219 | if (cli->base.vm) { |
| 220 | vma = nouveau_bo_vma_find(nvbo, cli->base.vm); |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 221 | if (!vma) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | rep->offset = vma->offset; |
| 225 | } |
| 226 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 227 | rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT; |
Ben Skeggs | d550c41 | 2011-02-16 08:41:56 +1000 | [diff] [blame] | 228 | rep->map_handle = nvbo->bo.addr_space_offset; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 229 | rep->tile_mode = nvbo->tile_mode; |
| 230 | rep->tile_flags = nvbo->tile_flags; |
| 231 | return 0; |
| 232 | } |
| 233 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 234 | int |
| 235 | nouveau_gem_ioctl_new(struct drm_device *dev, void *data, |
| 236 | struct drm_file *file_priv) |
| 237 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 238 | struct nouveau_drm *drm = nouveau_drm(dev); |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 239 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 240 | struct nouveau_fb *pfb = nouveau_fb(drm->device); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 241 | struct drm_nouveau_gem_new *req = data; |
| 242 | struct nouveau_bo *nvbo = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 243 | int ret = 0; |
| 244 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 245 | drm->ttm.bdev.dev_mapping = drm->dev->dev_mapping; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 246 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 247 | if (!pfb->memtype_valid(pfb, req->info.tile_flags)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 248 | NV_ERROR(cli, "bad page flags: 0x%08x\n", req->info.tile_flags); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 249 | return -EINVAL; |
Ben Skeggs | 60d2a88 | 2010-12-06 15:28:54 +1000 | [diff] [blame] | 250 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 251 | |
Ben Skeggs | f6d4e62 | 2011-06-07 12:25:36 +1000 | [diff] [blame] | 252 | ret = nouveau_gem_new(dev, req->info.size, req->align, |
Ben Skeggs | 6ba9a68 | 2011-02-10 14:42:08 +1000 | [diff] [blame] | 253 | req->info.domain, req->info.tile_mode, |
| 254 | req->info.tile_flags, &nvbo); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 255 | if (ret) |
| 256 | return ret; |
| 257 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 258 | ret = drm_gem_handle_create(file_priv, nvbo->gem, &req->info.handle); |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 259 | if (ret == 0) { |
| 260 | ret = nouveau_gem_info(file_priv, nvbo->gem, &req->info); |
| 261 | if (ret) |
| 262 | drm_gem_handle_delete(file_priv, req->info.handle); |
| 263 | } |
| 264 | |
Dave Airlie | 29d08b3 | 2010-09-27 16:17:17 +1000 | [diff] [blame] | 265 | /* drop reference from allocate - handle holds it now */ |
| 266 | drm_gem_object_unreference_unlocked(nvbo->gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | static int |
| 271 | nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains, |
| 272 | uint32_t write_domains, uint32_t valid_domains) |
| 273 | { |
| 274 | struct nouveau_bo *nvbo = gem->driver_private; |
| 275 | struct ttm_buffer_object *bo = &nvbo->bo; |
Ben Skeggs | db5c8e2 | 2011-02-10 13:41:01 +1000 | [diff] [blame] | 276 | uint32_t domains = valid_domains & nvbo->valid_domains & |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 277 | (write_domains ? write_domains : read_domains); |
| 278 | uint32_t pref_flags = 0, valid_flags = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 279 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 280 | if (!domains) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 281 | return -EINVAL; |
| 282 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 283 | if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) |
| 284 | valid_flags |= TTM_PL_FLAG_VRAM; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 285 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 286 | if (valid_domains & NOUVEAU_GEM_DOMAIN_GART) |
| 287 | valid_flags |= TTM_PL_FLAG_TT; |
| 288 | |
| 289 | if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) && |
| 290 | bo->mem.mem_type == TTM_PL_VRAM) |
| 291 | pref_flags |= TTM_PL_FLAG_VRAM; |
| 292 | |
| 293 | else if ((domains & NOUVEAU_GEM_DOMAIN_GART) && |
| 294 | bo->mem.mem_type == TTM_PL_TT) |
| 295 | pref_flags |= TTM_PL_FLAG_TT; |
| 296 | |
| 297 | else if (domains & NOUVEAU_GEM_DOMAIN_VRAM) |
| 298 | pref_flags |= TTM_PL_FLAG_VRAM; |
| 299 | |
| 300 | else |
| 301 | pref_flags |= TTM_PL_FLAG_TT; |
| 302 | |
| 303 | nouveau_bo_placement_set(nvbo, pref_flags, valid_flags); |
| 304 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | struct validate_op { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 309 | struct list_head vram_list; |
| 310 | struct list_head gart_list; |
| 311 | struct list_head both_list; |
| 312 | }; |
| 313 | |
| 314 | static void |
| 315 | validate_fini_list(struct list_head *list, struct nouveau_fence *fence) |
| 316 | { |
| 317 | struct list_head *entry, *tmp; |
| 318 | struct nouveau_bo *nvbo; |
| 319 | |
| 320 | list_for_each_safe(entry, tmp, list) { |
| 321 | nvbo = list_entry(entry, struct nouveau_bo, entry); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 322 | |
Francisco Jerez | 332b242 | 2010-10-20 23:35:40 +0200 | [diff] [blame] | 323 | nouveau_bo_fence(nvbo, fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 324 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 325 | if (unlikely(nvbo->validate_mapped)) { |
| 326 | ttm_bo_kunmap(&nvbo->kmap); |
| 327 | nvbo->validate_mapped = false; |
| 328 | } |
| 329 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 330 | list_del(&nvbo->entry); |
| 331 | nvbo->reserved_by = NULL; |
| 332 | ttm_bo_unreserve(&nvbo->bo); |
Francisco Jerez | 374c3af | 2010-08-29 12:21:16 +0200 | [diff] [blame] | 333 | drm_gem_object_unreference_unlocked(nvbo->gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | static void |
Luca Barbieri | 234896a | 2010-01-06 04:02:45 +0100 | [diff] [blame] | 338 | validate_fini(struct validate_op *op, struct nouveau_fence* fence) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 339 | { |
Luca Barbieri | 234896a | 2010-01-06 04:02:45 +0100 | [diff] [blame] | 340 | validate_fini_list(&op->vram_list, fence); |
| 341 | validate_fini_list(&op->gart_list, fence); |
| 342 | validate_fini_list(&op->both_list, fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static int |
| 346 | validate_init(struct nouveau_channel *chan, struct drm_file *file_priv, |
| 347 | struct drm_nouveau_gem_pushbuf_bo *pbbo, |
| 348 | int nr_buffers, struct validate_op *op) |
| 349 | { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 350 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 351 | struct drm_device *dev = chan->drm->dev; |
| 352 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 353 | uint32_t sequence; |
| 354 | int trycnt = 0; |
| 355 | int ret, i; |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 356 | struct nouveau_bo *res_bo = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 357 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 358 | sequence = atomic_add_return(1, &drm->ttm.validate_sequence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 359 | retry: |
| 360 | if (++trycnt > 100000) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 361 | NV_ERROR(cli, "%s failed and gave up.\n", __func__); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 362 | return -EINVAL; |
| 363 | } |
| 364 | |
| 365 | for (i = 0; i < nr_buffers; i++) { |
| 366 | struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i]; |
| 367 | struct drm_gem_object *gem; |
| 368 | struct nouveau_bo *nvbo; |
| 369 | |
| 370 | gem = drm_gem_object_lookup(dev, file_priv, b->handle); |
| 371 | if (!gem) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 372 | NV_ERROR(cli, "Unknown handle 0x%08x\n", b->handle); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 373 | validate_fini(op, NULL); |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 374 | return -ENOENT; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 375 | } |
| 376 | nvbo = gem->driver_private; |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 377 | if (nvbo == res_bo) { |
| 378 | res_bo = NULL; |
| 379 | drm_gem_object_unreference_unlocked(gem); |
| 380 | continue; |
| 381 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 382 | |
| 383 | if (nvbo->reserved_by && nvbo->reserved_by == file_priv) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 384 | NV_ERROR(cli, "multiple instances of buffer %d on " |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 385 | "validation list\n", b->handle); |
Ben Skeggs | 5086f69 | 2012-06-25 18:04:27 +1000 | [diff] [blame] | 386 | drm_gem_object_unreference_unlocked(gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 387 | validate_fini(op, NULL); |
| 388 | return -EINVAL; |
| 389 | } |
| 390 | |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 391 | ret = ttm_bo_reserve(&nvbo->bo, true, false, true, sequence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 392 | if (ret) { |
| 393 | validate_fini(op, NULL); |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 394 | if (unlikely(ret == -EAGAIN)) { |
| 395 | sequence = atomic_add_return(1, &drm->ttm.validate_sequence); |
| 396 | ret = ttm_bo_reserve_slowpath(&nvbo->bo, true, |
| 397 | sequence); |
| 398 | if (!ret) |
| 399 | res_bo = nvbo; |
| 400 | } |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 401 | if (unlikely(ret)) { |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 402 | drm_gem_object_unreference_unlocked(gem); |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 403 | if (ret != -ERESTARTSYS) |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 404 | NV_ERROR(cli, "fail reserve\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 405 | return ret; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 406 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 407 | } |
| 408 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 409 | b->user_priv = (uint64_t)(unsigned long)nvbo; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 410 | nvbo->reserved_by = file_priv; |
| 411 | nvbo->pbbo_index = i; |
| 412 | if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) && |
| 413 | (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)) |
| 414 | list_add_tail(&nvbo->entry, &op->both_list); |
| 415 | else |
| 416 | if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) |
| 417 | list_add_tail(&nvbo->entry, &op->vram_list); |
| 418 | else |
| 419 | if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART) |
| 420 | list_add_tail(&nvbo->entry, &op->gart_list); |
| 421 | else { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 422 | NV_ERROR(cli, "invalid valid domains: 0x%08x\n", |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 423 | b->valid_domains); |
Ben Skeggs | 0208843 | 2010-01-21 15:03:23 +1000 | [diff] [blame] | 424 | list_add_tail(&nvbo->entry, &op->both_list); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 425 | validate_fini(op, NULL); |
| 426 | return -EINVAL; |
| 427 | } |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 428 | if (nvbo == res_bo) |
| 429 | goto retry; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static int |
Ben Skeggs | 525895b | 2012-01-10 10:18:28 +1000 | [diff] [blame] | 436 | validate_sync(struct nouveau_channel *chan, struct nouveau_bo *nvbo) |
| 437 | { |
| 438 | struct nouveau_fence *fence = NULL; |
| 439 | int ret = 0; |
| 440 | |
| 441 | spin_lock(&nvbo->bo.bdev->fence_lock); |
| 442 | if (nvbo->bo.sync_obj) |
| 443 | fence = nouveau_fence_ref(nvbo->bo.sync_obj); |
| 444 | spin_unlock(&nvbo->bo.bdev->fence_lock); |
| 445 | |
| 446 | if (fence) { |
| 447 | ret = nouveau_fence_sync(fence, chan); |
| 448 | nouveau_fence_unref(&fence); |
| 449 | } |
| 450 | |
| 451 | return ret; |
| 452 | } |
| 453 | |
| 454 | static int |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 455 | validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli, |
| 456 | struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo, |
| 457 | uint64_t user_pbbo_ptr) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 458 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 459 | struct nouveau_drm *drm = chan->drm; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 460 | struct drm_nouveau_gem_pushbuf_bo __user *upbbo = |
| 461 | (void __force __user *)(uintptr_t)user_pbbo_ptr; |
| 462 | struct nouveau_bo *nvbo; |
| 463 | int ret, relocs = 0; |
| 464 | |
| 465 | list_for_each_entry(nvbo, list, entry) { |
| 466 | struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index]; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 467 | |
Ben Skeggs | 525895b | 2012-01-10 10:18:28 +1000 | [diff] [blame] | 468 | ret = validate_sync(chan, nvbo); |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 469 | if (unlikely(ret)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 470 | NV_ERROR(cli, "fail pre-validate sync\n"); |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 471 | return ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | ret = nouveau_gem_set_domain(nvbo->gem, b->read_domains, |
| 475 | b->write_domains, |
| 476 | b->valid_domains); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 477 | if (unlikely(ret)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 478 | NV_ERROR(cli, "fail set_domain\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 479 | return ret; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 480 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 481 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 482 | ret = nouveau_bo_validate(nvbo, true, false); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 483 | if (unlikely(ret)) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 484 | if (ret != -ERESTARTSYS) |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 485 | NV_ERROR(cli, "fail ttm_validate\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 486 | return ret; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 487 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 488 | |
Ben Skeggs | 525895b | 2012-01-10 10:18:28 +1000 | [diff] [blame] | 489 | ret = validate_sync(chan, nvbo); |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 490 | if (unlikely(ret)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 491 | NV_ERROR(cli, "fail post-validate sync\n"); |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 492 | return ret; |
| 493 | } |
| 494 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 495 | if (nv_device(drm->device)->card_type < NV_50) { |
Ben Skeggs | a3fcd0a | 2011-06-07 11:12:39 +1000 | [diff] [blame] | 496 | if (nvbo->bo.offset == b->presumed.offset && |
| 497 | ((nvbo->bo.mem.mem_type == TTM_PL_VRAM && |
| 498 | b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) || |
| 499 | (nvbo->bo.mem.mem_type == TTM_PL_TT && |
| 500 | b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART))) |
| 501 | continue; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 502 | |
Ben Skeggs | a3fcd0a | 2011-06-07 11:12:39 +1000 | [diff] [blame] | 503 | if (nvbo->bo.mem.mem_type == TTM_PL_TT) |
| 504 | b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART; |
| 505 | else |
| 506 | b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM; |
| 507 | b->presumed.offset = nvbo->bo.offset; |
| 508 | b->presumed.valid = 0; |
| 509 | relocs++; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 510 | |
Ben Skeggs | a3fcd0a | 2011-06-07 11:12:39 +1000 | [diff] [blame] | 511 | if (DRM_COPY_TO_USER(&upbbo[nvbo->pbbo_index].presumed, |
| 512 | &b->presumed, sizeof(b->presumed))) |
| 513 | return -EFAULT; |
| 514 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | return relocs; |
| 518 | } |
| 519 | |
| 520 | static int |
| 521 | nouveau_gem_pushbuf_validate(struct nouveau_channel *chan, |
| 522 | struct drm_file *file_priv, |
| 523 | struct drm_nouveau_gem_pushbuf_bo *pbbo, |
| 524 | uint64_t user_buffers, int nr_buffers, |
| 525 | struct validate_op *op, int *apply_relocs) |
| 526 | { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 527 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 528 | int ret, relocs = 0; |
| 529 | |
| 530 | INIT_LIST_HEAD(&op->vram_list); |
| 531 | INIT_LIST_HEAD(&op->gart_list); |
| 532 | INIT_LIST_HEAD(&op->both_list); |
| 533 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 534 | if (nr_buffers == 0) |
| 535 | return 0; |
| 536 | |
| 537 | ret = validate_init(chan, file_priv, pbbo, nr_buffers, op); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 538 | if (unlikely(ret)) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 539 | if (ret != -ERESTARTSYS) |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 540 | NV_ERROR(cli, "validate_init\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 541 | return ret; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 542 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 543 | |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 544 | ret = validate_list(chan, cli, &op->vram_list, pbbo, user_buffers); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 545 | if (unlikely(ret < 0)) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 546 | if (ret != -ERESTARTSYS) |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 547 | NV_ERROR(cli, "validate vram_list\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 548 | validate_fini(op, NULL); |
| 549 | return ret; |
| 550 | } |
| 551 | relocs += ret; |
| 552 | |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 553 | ret = validate_list(chan, cli, &op->gart_list, pbbo, user_buffers); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 554 | if (unlikely(ret < 0)) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 555 | if (ret != -ERESTARTSYS) |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 556 | NV_ERROR(cli, "validate gart_list\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 557 | validate_fini(op, NULL); |
| 558 | return ret; |
| 559 | } |
| 560 | relocs += ret; |
| 561 | |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 562 | ret = validate_list(chan, cli, &op->both_list, pbbo, user_buffers); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 563 | if (unlikely(ret < 0)) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 564 | if (ret != -ERESTARTSYS) |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 565 | NV_ERROR(cli, "validate both_list\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 566 | validate_fini(op, NULL); |
| 567 | return ret; |
| 568 | } |
| 569 | relocs += ret; |
| 570 | |
| 571 | *apply_relocs = relocs; |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | static inline void * |
| 576 | u_memcpya(uint64_t user, unsigned nmemb, unsigned size) |
| 577 | { |
| 578 | void *mem; |
| 579 | void __user *userptr = (void __force __user *)(uintptr_t)user; |
| 580 | |
| 581 | mem = kmalloc(nmemb * size, GFP_KERNEL); |
| 582 | if (!mem) |
| 583 | return ERR_PTR(-ENOMEM); |
| 584 | |
| 585 | if (DRM_COPY_FROM_USER(mem, userptr, nmemb * size)) { |
| 586 | kfree(mem); |
| 587 | return ERR_PTR(-EFAULT); |
| 588 | } |
| 589 | |
| 590 | return mem; |
| 591 | } |
| 592 | |
| 593 | static int |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 594 | nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli, |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 595 | struct drm_nouveau_gem_pushbuf *req, |
| 596 | struct drm_nouveau_gem_pushbuf_bo *bo) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 597 | { |
| 598 | struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; |
Luca Barbieri | 12f735b | 2010-01-10 20:10:53 +0100 | [diff] [blame] | 599 | int ret = 0; |
| 600 | unsigned i; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 601 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 602 | reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 603 | if (IS_ERR(reloc)) |
| 604 | return PTR_ERR(reloc); |
| 605 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 606 | for (i = 0; i < req->nr_relocs; i++) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 607 | struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i]; |
| 608 | struct drm_nouveau_gem_pushbuf_bo *b; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 609 | struct nouveau_bo *nvbo; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 610 | uint32_t data; |
| 611 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 612 | if (unlikely(r->bo_index > req->nr_buffers)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 613 | NV_ERROR(cli, "reloc bo index invalid\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 614 | ret = -EINVAL; |
| 615 | break; |
| 616 | } |
| 617 | |
| 618 | b = &bo[r->bo_index]; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 619 | if (b->presumed.valid) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 620 | continue; |
| 621 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 622 | if (unlikely(r->reloc_bo_index > req->nr_buffers)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 623 | NV_ERROR(cli, "reloc container bo index invalid\n"); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 624 | ret = -EINVAL; |
| 625 | break; |
| 626 | } |
| 627 | nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv; |
| 628 | |
| 629 | if (unlikely(r->reloc_bo_offset + 4 > |
| 630 | nvbo->bo.mem.num_pages << PAGE_SHIFT)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 631 | NV_ERROR(cli, "reloc outside of bo\n"); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 632 | ret = -EINVAL; |
| 633 | break; |
| 634 | } |
| 635 | |
| 636 | if (!nvbo->kmap.virtual) { |
| 637 | ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, |
| 638 | &nvbo->kmap); |
| 639 | if (ret) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 640 | NV_ERROR(cli, "failed kmap for reloc\n"); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 641 | break; |
| 642 | } |
| 643 | nvbo->validate_mapped = true; |
| 644 | } |
| 645 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 646 | if (r->flags & NOUVEAU_GEM_RELOC_LOW) |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 647 | data = b->presumed.offset + r->data; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 648 | else |
| 649 | if (r->flags & NOUVEAU_GEM_RELOC_HIGH) |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 650 | data = (b->presumed.offset + r->data) >> 32; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 651 | else |
| 652 | data = r->data; |
| 653 | |
| 654 | if (r->flags & NOUVEAU_GEM_RELOC_OR) { |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 655 | if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 656 | data |= r->tor; |
| 657 | else |
| 658 | data |= r->vor; |
| 659 | } |
| 660 | |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 661 | spin_lock(&nvbo->bo.bdev->fence_lock); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 662 | ret = ttm_bo_wait(&nvbo->bo, false, false, false); |
Thomas Hellstrom | 702adba | 2010-11-17 12:28:29 +0000 | [diff] [blame] | 663 | spin_unlock(&nvbo->bo.bdev->fence_lock); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 664 | if (ret) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 665 | NV_ERROR(cli, "reloc wait_idle failed: %d\n", ret); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 666 | break; |
| 667 | } |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 668 | |
| 669 | nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | kfree(reloc); |
| 673 | return ret; |
| 674 | } |
| 675 | |
| 676 | int |
| 677 | nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, |
| 678 | struct drm_file *file_priv) |
| 679 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 680 | struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev); |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 681 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 682 | struct nouveau_abi16_chan *temp; |
| 683 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 684 | struct drm_nouveau_gem_pushbuf *req = data; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 685 | struct drm_nouveau_gem_pushbuf_push *push; |
| 686 | struct drm_nouveau_gem_pushbuf_bo *bo; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 687 | struct nouveau_channel *chan = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 688 | struct validate_op op; |
Francisco Jerez | 6e86e04 | 2010-07-03 18:36:39 +0200 | [diff] [blame] | 689 | struct nouveau_fence *fence = NULL; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 690 | int i, j, ret = 0, do_reloc = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 691 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 692 | if (unlikely(!abi16)) |
| 693 | return -ENOMEM; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 694 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 695 | list_for_each_entry(temp, &abi16->channels, head) { |
| 696 | if (temp->chan->handle == (NVDRM_CHAN | req->channel)) { |
| 697 | chan = temp->chan; |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | if (!chan) |
| 703 | return nouveau_abi16_put(abi16, -ENOENT); |
| 704 | |
| 705 | req->vram_available = drm->gem.vram_available; |
| 706 | req->gart_available = drm->gem.gart_available; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 707 | if (unlikely(req->nr_push == 0)) |
| 708 | goto out_next; |
| 709 | |
| 710 | if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 711 | NV_ERROR(cli, "pushbuf push count exceeds limit: %d max %d\n", |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 712 | req->nr_push, NOUVEAU_GEM_MAX_PUSH); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 713 | return nouveau_abi16_put(abi16, -EINVAL); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 714 | } |
| 715 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 716 | if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 717 | NV_ERROR(cli, "pushbuf bo count exceeds limit: %d max %d\n", |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 718 | req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 719 | return nouveau_abi16_put(abi16, -EINVAL); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 723 | NV_ERROR(cli, "pushbuf reloc count exceeds limit: %d max %d\n", |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 724 | req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 725 | return nouveau_abi16_put(abi16, -EINVAL); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | push = u_memcpya(req->push, req->nr_push, sizeof(*push)); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 729 | if (IS_ERR(push)) |
| 730 | return nouveau_abi16_put(abi16, PTR_ERR(push)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 731 | |
| 732 | bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo)); |
| 733 | if (IS_ERR(bo)) { |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 734 | kfree(push); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 735 | return nouveau_abi16_put(abi16, PTR_ERR(bo)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 736 | } |
| 737 | |
Ben Skeggs | accf949 | 2012-03-16 12:40:17 +1000 | [diff] [blame] | 738 | /* Ensure all push buffers are on validate list */ |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 739 | for (i = 0; i < req->nr_push; i++) { |
| 740 | if (push[i].bo_index >= req->nr_buffers) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 741 | NV_ERROR(cli, "push %d buffer not in list\n", i); |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 742 | ret = -EINVAL; |
Marcin Slusarz | 7fa0cba2 | 2011-03-07 12:31:35 +0100 | [diff] [blame] | 743 | goto out_prevalid; |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 744 | } |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 745 | } |
| 746 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 747 | /* Validate buffer list */ |
| 748 | ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers, |
| 749 | req->nr_buffers, &op, &do_reloc); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 750 | if (ret) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 751 | if (ret != -ERESTARTSYS) |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 752 | NV_ERROR(cli, "validate: %d\n", ret); |
Marcin Slusarz | 7fa0cba2 | 2011-03-07 12:31:35 +0100 | [diff] [blame] | 753 | goto out_prevalid; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 754 | } |
| 755 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 756 | /* Apply any relocations that are required */ |
| 757 | if (do_reloc) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 758 | ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 759 | if (ret) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 760 | NV_ERROR(cli, "reloc apply: %d\n", ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 761 | goto out; |
| 762 | } |
| 763 | } |
| 764 | |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 765 | if (chan->dma.ib_max) { |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 766 | ret = nouveau_dma_wait(chan, req->nr_push + 1, 16); |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 767 | if (ret) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 768 | NV_ERROR(cli, "nv50cal_space: %d\n", ret); |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 769 | goto out; |
| 770 | } |
| 771 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 772 | for (i = 0; i < req->nr_push; i++) { |
| 773 | struct nouveau_bo *nvbo = (void *)(unsigned long) |
| 774 | bo[push[i].bo_index].user_priv; |
| 775 | |
| 776 | nv50_dma_push(chan, nvbo, push[i].offset, |
| 777 | push[i].length); |
| 778 | } |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 779 | } else |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 780 | if (nv_device(drm->device)->chipset >= 0x25) { |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 781 | ret = RING_SPACE(chan, req->nr_push * 2); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 782 | if (ret) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 783 | NV_ERROR(cli, "cal_space: %d\n", ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 784 | goto out; |
| 785 | } |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 786 | |
| 787 | for (i = 0; i < req->nr_push; i++) { |
| 788 | struct nouveau_bo *nvbo = (void *)(unsigned long) |
| 789 | bo[push[i].bo_index].user_priv; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 790 | |
Ben Skeggs | 3a92d37 | 2012-08-04 05:46:01 +1000 | [diff] [blame] | 791 | OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 792 | OUT_RING(chan, 0); |
| 793 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 794 | } else { |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 795 | ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 796 | if (ret) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 797 | NV_ERROR(cli, "jmp_space: %d\n", ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 798 | goto out; |
| 799 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 800 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 801 | for (i = 0; i < req->nr_push; i++) { |
| 802 | struct nouveau_bo *nvbo = (void *)(unsigned long) |
| 803 | bo[push[i].bo_index].user_priv; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 804 | uint32_t cmd; |
| 805 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 806 | cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 807 | cmd |= 0x20000000; |
| 808 | if (unlikely(cmd != req->suffix0)) { |
| 809 | if (!nvbo->kmap.virtual) { |
| 810 | ret = ttm_bo_kmap(&nvbo->bo, 0, |
| 811 | nvbo->bo.mem. |
| 812 | num_pages, |
| 813 | &nvbo->kmap); |
| 814 | if (ret) { |
| 815 | WIND_RING(chan); |
| 816 | goto out; |
| 817 | } |
| 818 | nvbo->validate_mapped = true; |
| 819 | } |
| 820 | |
| 821 | nouveau_bo_wr32(nvbo, (push[i].offset + |
| 822 | push[i].length - 8) / 4, cmd); |
| 823 | } |
| 824 | |
Ben Skeggs | 3a92d37 | 2012-08-04 05:46:01 +1000 | [diff] [blame] | 825 | OUT_RING(chan, 0x20000000 | |
| 826 | (nvbo->bo.offset + push[i].offset)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 827 | OUT_RING(chan, 0); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 828 | for (j = 0; j < NOUVEAU_DMA_SKIPS; j++) |
| 829 | OUT_RING(chan, 0); |
| 830 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 831 | } |
| 832 | |
Ben Skeggs | 264ce19 | 2013-02-14 13:43:21 +1000 | [diff] [blame] | 833 | ret = nouveau_fence_new(chan, false, &fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 834 | if (ret) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 835 | NV_ERROR(cli, "error fencing pushbuf: %d\n", ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 836 | WIND_RING(chan); |
| 837 | goto out; |
| 838 | } |
| 839 | |
| 840 | out: |
Luca Barbieri | 234896a | 2010-01-06 04:02:45 +0100 | [diff] [blame] | 841 | validate_fini(&op, fence); |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 842 | nouveau_fence_unref(&fence); |
Marcin Slusarz | 7fa0cba2 | 2011-03-07 12:31:35 +0100 | [diff] [blame] | 843 | |
| 844 | out_prevalid: |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 845 | kfree(bo); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 846 | kfree(push); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 847 | |
| 848 | out_next: |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 849 | if (chan->dma.ib_max) { |
| 850 | req->suffix0 = 0x00000000; |
| 851 | req->suffix1 = 0x00000000; |
| 852 | } else |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 853 | if (nv_device(drm->device)->chipset >= 0x25) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 854 | req->suffix0 = 0x00020000; |
| 855 | req->suffix1 = 0x00000000; |
| 856 | } else { |
| 857 | req->suffix0 = 0x20000000 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 858 | (chan->push.vma.offset + ((chan->dma.cur + 2) << 2)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 859 | req->suffix1 = 0x00000000; |
| 860 | } |
| 861 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 862 | return nouveau_abi16_put(abi16, ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 863 | } |
| 864 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 865 | static inline uint32_t |
| 866 | domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain) |
| 867 | { |
| 868 | uint32_t flags = 0; |
| 869 | |
| 870 | if (domain & NOUVEAU_GEM_DOMAIN_VRAM) |
| 871 | flags |= TTM_PL_FLAG_VRAM; |
| 872 | if (domain & NOUVEAU_GEM_DOMAIN_GART) |
| 873 | flags |= TTM_PL_FLAG_TT; |
| 874 | |
| 875 | return flags; |
| 876 | } |
| 877 | |
| 878 | int |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 879 | nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data, |
| 880 | struct drm_file *file_priv) |
| 881 | { |
| 882 | struct drm_nouveau_gem_cpu_prep *req = data; |
| 883 | struct drm_gem_object *gem; |
| 884 | struct nouveau_bo *nvbo; |
| 885 | bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT); |
| 886 | int ret = -EINVAL; |
| 887 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 888 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); |
| 889 | if (!gem) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 890 | return -ENOENT; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 891 | nvbo = nouveau_gem_object(gem); |
| 892 | |
Ben Skeggs | 21e86c1 | 2010-10-11 11:48:45 +1000 | [diff] [blame] | 893 | spin_lock(&nvbo->bo.bdev->fence_lock); |
| 894 | ret = ttm_bo_wait(&nvbo->bo, true, true, no_wait); |
| 895 | spin_unlock(&nvbo->bo.bdev->fence_lock); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 896 | drm_gem_object_unreference_unlocked(gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 897 | return ret; |
| 898 | } |
| 899 | |
| 900 | int |
| 901 | nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data, |
| 902 | struct drm_file *file_priv) |
| 903 | { |
Ben Skeggs | 21e86c1 | 2010-10-11 11:48:45 +1000 | [diff] [blame] | 904 | return 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | int |
| 908 | nouveau_gem_ioctl_info(struct drm_device *dev, void *data, |
| 909 | struct drm_file *file_priv) |
| 910 | { |
| 911 | struct drm_nouveau_gem_info *req = data; |
| 912 | struct drm_gem_object *gem; |
| 913 | int ret; |
| 914 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 915 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); |
| 916 | if (!gem) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 917 | return -ENOENT; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 918 | |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 919 | ret = nouveau_gem_info(file_priv, gem, req); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 920 | drm_gem_object_unreference_unlocked(gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 921 | return ret; |
| 922 | } |
| 923 | |