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