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