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