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