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