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