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