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