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