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 | */ |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 26 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 27 | #include "nouveau_drm.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 28 | #include "nouveau_dma.h" |
Ben Skeggs | d375e7d5 | 2012-04-30 13:30:00 +1000 | [diff] [blame] | 29 | #include "nouveau_fence.h" |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 30 | #include "nouveau_abi16.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 31 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 32 | #include "nouveau_ttm.h" |
| 33 | #include "nouveau_gem.h" |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 34 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 35 | void |
| 36 | nouveau_gem_object_del(struct drm_gem_object *gem) |
| 37 | { |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 38 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
Ben Skeggs | 5cc8d53 | 2014-12-11 10:05:00 +1000 | [diff] [blame] | 39 | struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 40 | struct ttm_buffer_object *bo = &nvbo->bo; |
Ben Skeggs | 5cc8d53 | 2014-12-11 10:05:00 +1000 | [diff] [blame] | 41 | struct device *dev = drm->dev->dev; |
| 42 | int ret; |
| 43 | |
| 44 | ret = pm_runtime_get_sync(dev); |
| 45 | if (WARN_ON(ret < 0 && ret != -EACCES)) |
| 46 | return; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 47 | |
Dave Airlie | 22b33e8 | 2012-04-02 11:53:06 +0100 | [diff] [blame] | 48 | if (gem->import_attach) |
| 49 | drm_prime_gem_destroy(gem, nvbo->bo.sg); |
| 50 | |
Daniel Vetter | fd632aa | 2010-04-09 19:05:05 +0000 | [diff] [blame] | 51 | drm_gem_object_release(gem); |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 52 | |
| 53 | /* reset filp so nouveau_bo_del_ttm() can test for it */ |
| 54 | gem->filp = NULL; |
| 55 | ttm_bo_unref(&bo); |
Ben Skeggs | 5cc8d53 | 2014-12-11 10:05:00 +1000 | [diff] [blame] | 56 | |
| 57 | pm_runtime_mark_last_busy(dev); |
| 58 | pm_runtime_put_autosuspend(dev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | int |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 62 | nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv) |
| 63 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 64 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 65 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
Ben Skeggs | 5cc8d53 | 2014-12-11 10:05:00 +1000 | [diff] [blame] | 66 | struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 67 | struct nvkm_vma *vma; |
Ben Skeggs | 5cc8d53 | 2014-12-11 10:05:00 +1000 | [diff] [blame] | 68 | struct device *dev = drm->dev->dev; |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 69 | int ret; |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 70 | |
Ben Skeggs | 3ee6f5b | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 71 | if (!cli->vm) |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 72 | return 0; |
| 73 | |
Thierry Reding | ee3939e | 2014-07-21 13:15:51 +0200 | [diff] [blame] | 74 | ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 75 | if (ret) |
| 76 | return ret; |
| 77 | |
Ben Skeggs | 3ee6f5b | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 78 | vma = nouveau_bo_vma_find(nvbo, cli->vm); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 79 | if (!vma) { |
| 80 | vma = kzalloc(sizeof(*vma), GFP_KERNEL); |
| 81 | if (!vma) { |
| 82 | ret = -ENOMEM; |
| 83 | goto out; |
| 84 | } |
| 85 | |
Ben Skeggs | 5cc8d53 | 2014-12-11 10:05:00 +1000 | [diff] [blame] | 86 | ret = pm_runtime_get_sync(dev); |
Sudip Mukherjee | bad4274 | 2015-09-11 15:00:56 +0530 | [diff] [blame^] | 87 | if (ret < 0 && ret != -EACCES) { |
| 88 | kfree(vma); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 89 | goto out; |
Sudip Mukherjee | bad4274 | 2015-09-11 15:00:56 +0530 | [diff] [blame^] | 90 | } |
Ben Skeggs | 5cc8d53 | 2014-12-11 10:05:00 +1000 | [diff] [blame] | 91 | |
| 92 | ret = nouveau_bo_vma_add(nvbo, cli->vm, vma); |
| 93 | if (ret) |
| 94 | kfree(vma); |
| 95 | |
| 96 | pm_runtime_mark_last_busy(dev); |
| 97 | pm_runtime_put_autosuspend(dev); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 98 | } else { |
| 99 | vma->refcount++; |
| 100 | } |
| 101 | |
| 102 | out: |
| 103 | ttm_bo_unreserve(&nvbo->bo); |
| 104 | return ret; |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 105 | } |
| 106 | |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 107 | static void |
| 108 | nouveau_gem_object_delete(void *data) |
| 109 | { |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 110 | struct nvkm_vma *vma = data; |
| 111 | nvkm_vm_unmap(vma); |
| 112 | nvkm_vm_put(vma); |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 113 | kfree(vma); |
| 114 | } |
| 115 | |
| 116 | static void |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 117 | nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nvkm_vma *vma) |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 118 | { |
| 119 | const bool mapped = nvbo->bo.mem.mem_type != TTM_PL_SYSTEM; |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 120 | struct reservation_object *resv = nvbo->bo.resv; |
| 121 | struct reservation_object_list *fobj; |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 122 | struct fence *fence = NULL; |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 123 | |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 124 | fobj = reservation_object_get_list(resv); |
| 125 | |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 126 | list_del(&vma->head); |
| 127 | |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 128 | if (fobj && fobj->shared_count > 1) |
| 129 | ttm_bo_wait(&nvbo->bo, true, false, false); |
| 130 | else if (fobj && fobj->shared_count == 1) |
| 131 | fence = rcu_dereference_protected(fobj->shared[0], |
| 132 | reservation_object_held(resv)); |
| 133 | else |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 134 | fence = reservation_object_get_excl(nvbo->bo.resv); |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 135 | |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 136 | if (fence && mapped) { |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 137 | nouveau_fence_work(fence, nouveau_gem_object_delete, vma); |
| 138 | } else { |
| 139 | if (mapped) |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 140 | nvkm_vm_unmap(vma); |
| 141 | nvkm_vm_put(vma); |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 142 | kfree(vma); |
| 143 | } |
Ben Skeggs | c4c7044 | 2013-05-07 09:48:30 +1000 | [diff] [blame] | 144 | } |
| 145 | |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 146 | void |
| 147 | nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv) |
| 148 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 149 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 150 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
Ben Skeggs | 5cc8d53 | 2014-12-11 10:05:00 +1000 | [diff] [blame] | 151 | struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev); |
| 152 | struct device *dev = drm->dev->dev; |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 153 | struct nvkm_vma *vma; |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 154 | int ret; |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 155 | |
Ben Skeggs | 3ee6f5b | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 156 | if (!cli->vm) |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 157 | return; |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 158 | |
Thierry Reding | ee3939e | 2014-07-21 13:15:51 +0200 | [diff] [blame] | 159 | ret = ttm_bo_reserve(&nvbo->bo, false, false, false, NULL); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 160 | if (ret) |
| 161 | return; |
| 162 | |
Ben Skeggs | 3ee6f5b | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 163 | vma = nouveau_bo_vma_find(nvbo, cli->vm); |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 164 | if (vma) { |
Ben Skeggs | 5cc8d53 | 2014-12-11 10:05:00 +1000 | [diff] [blame] | 165 | if (--vma->refcount == 0) { |
| 166 | ret = pm_runtime_get_sync(dev); |
| 167 | if (!WARN_ON(ret < 0 && ret != -EACCES)) { |
| 168 | nouveau_gem_object_unmap(nvbo, vma); |
| 169 | pm_runtime_mark_last_busy(dev); |
| 170 | pm_runtime_put_autosuspend(dev); |
| 171 | } |
| 172 | } |
Ben Skeggs | 2fd3db6 | 2011-06-07 15:25:12 +1000 | [diff] [blame] | 173 | } |
| 174 | ttm_bo_unreserve(&nvbo->bo); |
Ben Skeggs | 639212d | 2011-06-03 16:18:26 +1000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | int |
Ben Skeggs | f6d4e62 | 2011-06-07 12:25:36 +1000 | [diff] [blame] | 178 | nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain, |
| 179 | uint32_t tile_mode, uint32_t tile_flags, |
| 180 | struct nouveau_bo **pnvbo) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 181 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 182 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 183 | struct nouveau_bo *nvbo; |
Ben Skeggs | 6ba9a68 | 2011-02-10 14:42:08 +1000 | [diff] [blame] | 184 | u32 flags = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 185 | int ret; |
| 186 | |
Ben Skeggs | 6ba9a68 | 2011-02-10 14:42:08 +1000 | [diff] [blame] | 187 | if (domain & NOUVEAU_GEM_DOMAIN_VRAM) |
| 188 | flags |= TTM_PL_FLAG_VRAM; |
| 189 | if (domain & NOUVEAU_GEM_DOMAIN_GART) |
| 190 | flags |= TTM_PL_FLAG_TT; |
| 191 | if (!flags || domain & NOUVEAU_GEM_DOMAIN_CPU) |
| 192 | flags |= TTM_PL_FLAG_SYSTEM; |
| 193 | |
Alexandre Courbot | 996f545 | 2015-02-26 12:44:51 +0900 | [diff] [blame] | 194 | if (domain & NOUVEAU_GEM_DOMAIN_COHERENT) |
| 195 | flags |= TTM_PL_FLAG_UNCACHED; |
| 196 | |
Ben Skeggs | 7375c95 | 2011-06-07 14:21:29 +1000 | [diff] [blame] | 197 | ret = nouveau_bo_new(dev, size, align, flags, tile_mode, |
Maarten Lankhorst | bb6178b | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 198 | tile_flags, NULL, NULL, pnvbo); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 199 | if (ret) |
| 200 | return ret; |
| 201 | nvbo = *pnvbo; |
| 202 | |
Ben Skeggs | db5c8e2 | 2011-02-10 13:41:01 +1000 | [diff] [blame] | 203 | /* we restrict allowed domains on nv50+ to only the types |
| 204 | * that were requested at creation time. not possibly on |
| 205 | * earlier chips without busting the ABI. |
| 206 | */ |
| 207 | nvbo->valid_domains = NOUVEAU_GEM_DOMAIN_VRAM | |
| 208 | NOUVEAU_GEM_DOMAIN_GART; |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 209 | if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) |
Ben Skeggs | db5c8e2 | 2011-02-10 13:41:01 +1000 | [diff] [blame] | 210 | nvbo->valid_domains &= domain; |
| 211 | |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 212 | /* Initialize the embedded gem-object. We return a single gem-reference |
| 213 | * to the caller, instead of a normal nouveau_bo ttm reference. */ |
| 214 | ret = drm_gem_object_init(dev, &nvbo->gem, nvbo->bo.mem.size); |
| 215 | if (ret) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 216 | nouveau_bo_ref(NULL, pnvbo); |
| 217 | return -ENOMEM; |
| 218 | } |
| 219 | |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 220 | nvbo->bo.persistent_swap_storage = nvbo->gem.filp; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 225 | nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem, |
| 226 | struct drm_nouveau_gem_info *rep) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 227 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 228 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 229 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
Ben Skeggs | be83cd4 | 2015-01-14 15:36:34 +1000 | [diff] [blame] | 230 | struct nvkm_vma *vma; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 231 | |
Ilia Mirkin | 04b8a4b | 2015-10-20 01:15:39 -0400 | [diff] [blame] | 232 | if (is_power_of_2(nvbo->valid_domains)) |
| 233 | rep->domain = nvbo->valid_domains; |
| 234 | else if (nvbo->bo.mem.mem_type == TTM_PL_TT) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 235 | rep->domain = NOUVEAU_GEM_DOMAIN_GART; |
| 236 | else |
| 237 | rep->domain = NOUVEAU_GEM_DOMAIN_VRAM; |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 238 | rep->offset = nvbo->bo.offset; |
Ben Skeggs | 3ee6f5b | 2014-08-10 04:10:20 +1000 | [diff] [blame] | 239 | if (cli->vm) { |
| 240 | vma = nouveau_bo_vma_find(nvbo, cli->vm); |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 241 | if (!vma) |
| 242 | return -EINVAL; |
| 243 | |
| 244 | rep->offset = vma->offset; |
| 245 | } |
| 246 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 247 | rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT; |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 248 | rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 249 | rep->tile_mode = nvbo->tile_mode; |
| 250 | rep->tile_flags = nvbo->tile_flags; |
| 251 | return 0; |
| 252 | } |
| 253 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 254 | int |
| 255 | nouveau_gem_ioctl_new(struct drm_device *dev, void *data, |
| 256 | struct drm_file *file_priv) |
| 257 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 258 | struct nouveau_drm *drm = nouveau_drm(dev); |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 259 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | b1e4553 | 2015-08-20 14:54:06 +1000 | [diff] [blame] | 260 | struct nvkm_fb *fb = nvxx_fb(&drm->device); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 261 | struct drm_nouveau_gem_new *req = data; |
| 262 | struct nouveau_bo *nvbo = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 263 | int ret = 0; |
| 264 | |
Ben Skeggs | 03c8952 | 2015-08-20 14:54:20 +1000 | [diff] [blame] | 265 | if (!nvkm_fb_memtype_valid(fb, req->info.tile_flags)) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 266 | NV_PRINTK(err, cli, "bad page flags: 0x%08x\n", req->info.tile_flags); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 267 | return -EINVAL; |
Ben Skeggs | 60d2a88 | 2010-12-06 15:28:54 +1000 | [diff] [blame] | 268 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 269 | |
Ben Skeggs | f6d4e62 | 2011-06-07 12:25:36 +1000 | [diff] [blame] | 270 | ret = nouveau_gem_new(dev, req->info.size, req->align, |
Ben Skeggs | 6ba9a68 | 2011-02-10 14:42:08 +1000 | [diff] [blame] | 271 | req->info.domain, req->info.tile_mode, |
| 272 | req->info.tile_flags, &nvbo); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 273 | if (ret) |
| 274 | return ret; |
| 275 | |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 276 | ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle); |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 277 | if (ret == 0) { |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 278 | ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info); |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 279 | if (ret) |
| 280 | drm_gem_handle_delete(file_priv, req->info.handle); |
| 281 | } |
| 282 | |
Dave Airlie | 29d08b3 | 2010-09-27 16:17:17 +1000 | [diff] [blame] | 283 | /* drop reference from allocate - handle holds it now */ |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 284 | drm_gem_object_unreference_unlocked(&nvbo->gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static int |
| 289 | nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains, |
| 290 | uint32_t write_domains, uint32_t valid_domains) |
| 291 | { |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 292 | struct nouveau_bo *nvbo = nouveau_gem_object(gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 293 | struct ttm_buffer_object *bo = &nvbo->bo; |
Ben Skeggs | db5c8e2 | 2011-02-10 13:41:01 +1000 | [diff] [blame] | 294 | uint32_t domains = valid_domains & nvbo->valid_domains & |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 295 | (write_domains ? write_domains : read_domains); |
| 296 | uint32_t pref_flags = 0, valid_flags = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 297 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 298 | if (!domains) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 299 | return -EINVAL; |
| 300 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 301 | if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) |
| 302 | valid_flags |= TTM_PL_FLAG_VRAM; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 303 | |
Francisco Jerez | 78ad0f7 | 2010-03-18 13:07:47 +0100 | [diff] [blame] | 304 | if (valid_domains & NOUVEAU_GEM_DOMAIN_GART) |
| 305 | valid_flags |= TTM_PL_FLAG_TT; |
| 306 | |
| 307 | if ((domains & NOUVEAU_GEM_DOMAIN_VRAM) && |
| 308 | bo->mem.mem_type == TTM_PL_VRAM) |
| 309 | pref_flags |= TTM_PL_FLAG_VRAM; |
| 310 | |
| 311 | else if ((domains & NOUVEAU_GEM_DOMAIN_GART) && |
| 312 | bo->mem.mem_type == TTM_PL_TT) |
| 313 | pref_flags |= TTM_PL_FLAG_TT; |
| 314 | |
| 315 | else if (domains & NOUVEAU_GEM_DOMAIN_VRAM) |
| 316 | pref_flags |= TTM_PL_FLAG_VRAM; |
| 317 | |
| 318 | else |
| 319 | pref_flags |= TTM_PL_FLAG_TT; |
| 320 | |
| 321 | nouveau_bo_placement_set(nvbo, pref_flags, valid_flags); |
| 322 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | struct validate_op { |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 327 | struct list_head list; |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 328 | struct ww_acquire_ctx ticket; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | static void |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 332 | validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence, |
| 333 | struct drm_nouveau_gem_pushbuf_bo *pbbo) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 334 | { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 335 | struct nouveau_bo *nvbo; |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 336 | struct drm_nouveau_gem_pushbuf_bo *b; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 337 | |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 338 | while (!list_empty(&op->list)) { |
| 339 | nvbo = list_entry(op->list.next, struct nouveau_bo, entry); |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 340 | b = &pbbo[nvbo->pbbo_index]; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 341 | |
Ben Skeggs | 9360bd1 | 2013-11-13 15:18:32 +1000 | [diff] [blame] | 342 | if (likely(fence)) |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 343 | nouveau_bo_fence(nvbo, fence, !!b->write_domains); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 344 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 345 | if (unlikely(nvbo->validate_mapped)) { |
| 346 | ttm_bo_kunmap(&nvbo->kmap); |
| 347 | nvbo->validate_mapped = false; |
| 348 | } |
| 349 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 350 | list_del(&nvbo->entry); |
| 351 | nvbo->reserved_by = NULL; |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 352 | ttm_bo_unreserve_ticket(&nvbo->bo, &op->ticket); |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 353 | drm_gem_object_unreference_unlocked(&nvbo->gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | static void |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 358 | validate_fini(struct validate_op *op, struct nouveau_fence *fence, |
| 359 | struct drm_nouveau_gem_pushbuf_bo *pbbo) |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 360 | { |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 361 | validate_fini_no_ticket(op, fence, pbbo); |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 362 | ww_acquire_fini(&op->ticket); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static int |
| 366 | validate_init(struct nouveau_channel *chan, struct drm_file *file_priv, |
| 367 | struct drm_nouveau_gem_pushbuf_bo *pbbo, |
| 368 | int nr_buffers, struct validate_op *op) |
| 369 | { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 370 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 371 | struct drm_device *dev = chan->drm->dev; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 372 | int trycnt = 0; |
| 373 | int ret, i; |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 374 | struct nouveau_bo *res_bo = NULL; |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 375 | LIST_HEAD(gart_list); |
| 376 | LIST_HEAD(vram_list); |
| 377 | LIST_HEAD(both_list); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 378 | |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 379 | ww_acquire_init(&op->ticket, &reservation_ww_class); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 380 | retry: |
| 381 | if (++trycnt > 100000) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 382 | NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 383 | return -EINVAL; |
| 384 | } |
| 385 | |
| 386 | for (i = 0; i < nr_buffers; i++) { |
| 387 | struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[i]; |
| 388 | struct drm_gem_object *gem; |
| 389 | struct nouveau_bo *nvbo; |
| 390 | |
| 391 | gem = drm_gem_object_lookup(dev, file_priv, b->handle); |
| 392 | if (!gem) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 393 | NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle); |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 394 | ret = -ENOENT; |
| 395 | break; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 396 | } |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 397 | nvbo = nouveau_gem_object(gem); |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 398 | if (nvbo == res_bo) { |
| 399 | res_bo = NULL; |
| 400 | drm_gem_object_unreference_unlocked(gem); |
| 401 | continue; |
| 402 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 403 | |
| 404 | if (nvbo->reserved_by && nvbo->reserved_by == file_priv) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 405 | NV_PRINTK(err, cli, "multiple instances of buffer %d on " |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 406 | "validation list\n", b->handle); |
Ben Skeggs | 5086f69 | 2012-06-25 18:04:27 +1000 | [diff] [blame] | 407 | drm_gem_object_unreference_unlocked(gem); |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 408 | ret = -EINVAL; |
| 409 | break; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 410 | } |
| 411 | |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 412 | ret = ttm_bo_reserve(&nvbo->bo, true, false, true, &op->ticket); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 413 | if (ret) { |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 414 | list_splice_tail_init(&vram_list, &op->list); |
| 415 | list_splice_tail_init(&gart_list, &op->list); |
| 416 | list_splice_tail_init(&both_list, &op->list); |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 417 | validate_fini_no_ticket(op, NULL, NULL); |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame] | 418 | if (unlikely(ret == -EDEADLK)) { |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 419 | ret = ttm_bo_reserve_slowpath(&nvbo->bo, true, |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 420 | &op->ticket); |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 421 | if (!ret) |
| 422 | res_bo = nvbo; |
| 423 | } |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 424 | if (unlikely(ret)) { |
| 425 | if (ret != -ERESTARTSYS) |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 426 | NV_PRINTK(err, cli, "fail reserve\n"); |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 427 | break; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 428 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 429 | } |
| 430 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 431 | b->user_priv = (uint64_t)(unsigned long)nvbo; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 432 | nvbo->reserved_by = file_priv; |
| 433 | nvbo->pbbo_index = i; |
| 434 | if ((b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) && |
| 435 | (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)) |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 436 | list_add_tail(&nvbo->entry, &both_list); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 437 | else |
| 438 | if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM) |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 439 | list_add_tail(&nvbo->entry, &vram_list); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 440 | else |
| 441 | if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART) |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 442 | list_add_tail(&nvbo->entry, &gart_list); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 443 | else { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 444 | NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n", |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 445 | b->valid_domains); |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 446 | list_add_tail(&nvbo->entry, &both_list); |
| 447 | ret = -EINVAL; |
| 448 | break; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 449 | } |
Maarten Lankhorst | c354c89 | 2013-01-15 14:57:20 +0100 | [diff] [blame] | 450 | if (nvbo == res_bo) |
| 451 | goto retry; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 452 | } |
| 453 | |
Maarten Lankhorst | ecff665 | 2013-06-27 13:48:17 +0200 | [diff] [blame] | 454 | ww_acquire_done(&op->ticket); |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 455 | list_splice_tail(&vram_list, &op->list); |
| 456 | list_splice_tail(&gart_list, &op->list); |
| 457 | list_splice_tail(&both_list, &op->list); |
| 458 | if (ret) |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 459 | validate_fini(op, NULL, NULL); |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 460 | return ret; |
| 461 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | static int |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 465 | validate_list(struct nouveau_channel *chan, struct nouveau_cli *cli, |
| 466 | struct list_head *list, struct drm_nouveau_gem_pushbuf_bo *pbbo, |
| 467 | uint64_t user_pbbo_ptr) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 468 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 469 | struct nouveau_drm *drm = chan->drm; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 470 | struct drm_nouveau_gem_pushbuf_bo __user *upbbo = |
| 471 | (void __force __user *)(uintptr_t)user_pbbo_ptr; |
| 472 | struct nouveau_bo *nvbo; |
| 473 | int ret, relocs = 0; |
| 474 | |
| 475 | list_for_each_entry(nvbo, list, entry) { |
| 476 | struct drm_nouveau_gem_pushbuf_bo *b = &pbbo[nvbo->pbbo_index]; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 477 | |
David Herrmann | 55fb74a | 2013-10-02 10:15:17 +0200 | [diff] [blame] | 478 | ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains, |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 479 | b->write_domains, |
| 480 | b->valid_domains); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 481 | if (unlikely(ret)) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 482 | NV_PRINTK(err, cli, "fail set_domain\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 483 | return ret; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 484 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 485 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 486 | ret = nouveau_bo_validate(nvbo, true, false); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 487 | if (unlikely(ret)) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 488 | if (ret != -ERESTARTSYS) |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 489 | NV_PRINTK(err, cli, "fail ttm_validate\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 490 | return ret; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 491 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 492 | |
Maarten Lankhorst | e3be4c2 | 2014-09-16 11:15:07 +0200 | [diff] [blame] | 493 | ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true); |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 494 | if (unlikely(ret)) { |
Maarten Lankhorst | 29ba89b | 2014-01-09 11:03:11 +0100 | [diff] [blame] | 495 | if (ret != -ERESTARTSYS) |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 496 | NV_PRINTK(err, cli, "fail post-validate sync\n"); |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 497 | return ret; |
| 498 | } |
| 499 | |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 500 | if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) { |
Ben Skeggs | a3fcd0a | 2011-06-07 11:12:39 +1000 | [diff] [blame] | 501 | if (nvbo->bo.offset == b->presumed.offset && |
| 502 | ((nvbo->bo.mem.mem_type == TTM_PL_VRAM && |
| 503 | b->presumed.domain & NOUVEAU_GEM_DOMAIN_VRAM) || |
| 504 | (nvbo->bo.mem.mem_type == TTM_PL_TT && |
| 505 | b->presumed.domain & NOUVEAU_GEM_DOMAIN_GART))) |
| 506 | continue; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 507 | |
Ben Skeggs | a3fcd0a | 2011-06-07 11:12:39 +1000 | [diff] [blame] | 508 | if (nvbo->bo.mem.mem_type == TTM_PL_TT) |
| 509 | b->presumed.domain = NOUVEAU_GEM_DOMAIN_GART; |
| 510 | else |
| 511 | b->presumed.domain = NOUVEAU_GEM_DOMAIN_VRAM; |
| 512 | b->presumed.offset = nvbo->bo.offset; |
| 513 | b->presumed.valid = 0; |
| 514 | relocs++; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 515 | |
Daniel Vetter | 1d6ac18 | 2013-12-11 11:34:44 +0100 | [diff] [blame] | 516 | if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed, |
Ben Skeggs | a3fcd0a | 2011-06-07 11:12:39 +1000 | [diff] [blame] | 517 | &b->presumed, sizeof(b->presumed))) |
| 518 | return -EFAULT; |
| 519 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | return relocs; |
| 523 | } |
| 524 | |
| 525 | static int |
| 526 | nouveau_gem_pushbuf_validate(struct nouveau_channel *chan, |
| 527 | struct drm_file *file_priv, |
| 528 | struct drm_nouveau_gem_pushbuf_bo *pbbo, |
| 529 | uint64_t user_buffers, int nr_buffers, |
| 530 | struct validate_op *op, int *apply_relocs) |
| 531 | { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 532 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 533 | int ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 534 | |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 535 | INIT_LIST_HEAD(&op->list); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 536 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 537 | if (nr_buffers == 0) |
| 538 | return 0; |
| 539 | |
| 540 | ret = validate_init(chan, file_priv, pbbo, nr_buffers, op); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 541 | if (unlikely(ret)) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 542 | if (ret != -ERESTARTSYS) |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 543 | NV_PRINTK(err, cli, "validate_init\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 544 | return ret; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 545 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 546 | |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 547 | ret = validate_list(chan, cli, &op->list, pbbo, user_buffers); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 548 | if (unlikely(ret < 0)) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 549 | if (ret != -ERESTARTSYS) |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 550 | NV_PRINTK(err, cli, "validating bo list\n"); |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 551 | validate_fini(op, NULL, NULL); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 552 | return ret; |
| 553 | } |
Maarten Lankhorst | 9242829 | 2014-04-09 16:18:58 +0200 | [diff] [blame] | 554 | *apply_relocs = ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
Maarten Lankhorst | c859074 | 2013-09-02 16:31:31 +0200 | [diff] [blame] | 558 | static inline void |
| 559 | u_free(void *addr) |
| 560 | { |
Pekka Enberg | 48a2013 | 2015-06-30 14:59:18 -0700 | [diff] [blame] | 561 | kvfree(addr); |
Maarten Lankhorst | c859074 | 2013-09-02 16:31:31 +0200 | [diff] [blame] | 562 | } |
| 563 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 564 | static inline void * |
| 565 | u_memcpya(uint64_t user, unsigned nmemb, unsigned size) |
| 566 | { |
| 567 | void *mem; |
| 568 | void __user *userptr = (void __force __user *)(uintptr_t)user; |
| 569 | |
Maarten Lankhorst | c859074 | 2013-09-02 16:31:31 +0200 | [diff] [blame] | 570 | size *= nmemb; |
| 571 | |
| 572 | mem = kmalloc(size, GFP_KERNEL | __GFP_NOWARN); |
| 573 | if (!mem) |
| 574 | mem = vmalloc(size); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 575 | if (!mem) |
| 576 | return ERR_PTR(-ENOMEM); |
| 577 | |
Daniel Vetter | 1d6ac18 | 2013-12-11 11:34:44 +0100 | [diff] [blame] | 578 | if (copy_from_user(mem, userptr, size)) { |
Maarten Lankhorst | c859074 | 2013-09-02 16:31:31 +0200 | [diff] [blame] | 579 | u_free(mem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 580 | return ERR_PTR(-EFAULT); |
| 581 | } |
| 582 | |
| 583 | return mem; |
| 584 | } |
| 585 | |
| 586 | static int |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 587 | nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli, |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 588 | struct drm_nouveau_gem_pushbuf *req, |
| 589 | struct drm_nouveau_gem_pushbuf_bo *bo) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 590 | { |
| 591 | struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL; |
Luca Barbieri | 12f735b | 2010-01-10 20:10:53 +0100 | [diff] [blame] | 592 | int ret = 0; |
| 593 | unsigned i; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 594 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 595 | reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 596 | if (IS_ERR(reloc)) |
| 597 | return PTR_ERR(reloc); |
| 598 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 599 | for (i = 0; i < req->nr_relocs; i++) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 600 | struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i]; |
| 601 | struct drm_nouveau_gem_pushbuf_bo *b; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 602 | struct nouveau_bo *nvbo; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 603 | uint32_t data; |
| 604 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 605 | if (unlikely(r->bo_index > req->nr_buffers)) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 606 | NV_PRINTK(err, cli, "reloc bo index invalid\n"); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 607 | ret = -EINVAL; |
| 608 | break; |
| 609 | } |
| 610 | |
| 611 | b = &bo[r->bo_index]; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 612 | if (b->presumed.valid) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 613 | continue; |
| 614 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 615 | if (unlikely(r->reloc_bo_index > req->nr_buffers)) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 616 | NV_PRINTK(err, cli, "reloc container bo index invalid\n"); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 617 | ret = -EINVAL; |
| 618 | break; |
| 619 | } |
| 620 | nvbo = (void *)(unsigned long)bo[r->reloc_bo_index].user_priv; |
| 621 | |
| 622 | if (unlikely(r->reloc_bo_offset + 4 > |
| 623 | nvbo->bo.mem.num_pages << PAGE_SHIFT)) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 624 | NV_PRINTK(err, cli, "reloc outside of bo\n"); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 625 | ret = -EINVAL; |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | if (!nvbo->kmap.virtual) { |
| 630 | ret = ttm_bo_kmap(&nvbo->bo, 0, nvbo->bo.mem.num_pages, |
| 631 | &nvbo->kmap); |
| 632 | if (ret) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 633 | NV_PRINTK(err, cli, "failed kmap for reloc\n"); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | nvbo->validate_mapped = true; |
| 637 | } |
| 638 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 639 | if (r->flags & NOUVEAU_GEM_RELOC_LOW) |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 640 | data = b->presumed.offset + r->data; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 641 | else |
| 642 | if (r->flags & NOUVEAU_GEM_RELOC_HIGH) |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 643 | data = (b->presumed.offset + r->data) >> 32; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 644 | else |
| 645 | data = r->data; |
| 646 | |
| 647 | if (r->flags & NOUVEAU_GEM_RELOC_OR) { |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 648 | if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART) |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 649 | data |= r->tor; |
| 650 | else |
| 651 | data |= r->vor; |
| 652 | } |
| 653 | |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 654 | ret = ttm_bo_wait(&nvbo->bo, true, false, false); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 655 | if (ret) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 656 | NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 657 | break; |
| 658 | } |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 659 | |
| 660 | nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 661 | } |
| 662 | |
Maarten Lankhorst | c859074 | 2013-09-02 16:31:31 +0200 | [diff] [blame] | 663 | u_free(reloc); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 664 | return ret; |
| 665 | } |
| 666 | |
| 667 | int |
| 668 | nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data, |
| 669 | struct drm_file *file_priv) |
| 670 | { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 671 | struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev); |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 672 | struct nouveau_cli *cli = nouveau_cli(file_priv); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 673 | struct nouveau_abi16_chan *temp; |
| 674 | struct nouveau_drm *drm = nouveau_drm(dev); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 675 | struct drm_nouveau_gem_pushbuf *req = data; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 676 | struct drm_nouveau_gem_pushbuf_push *push; |
| 677 | struct drm_nouveau_gem_pushbuf_bo *bo; |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 678 | struct nouveau_channel *chan = NULL; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 679 | struct validate_op op; |
Francisco Jerez | 6e86e04 | 2010-07-03 18:36:39 +0200 | [diff] [blame] | 680 | struct nouveau_fence *fence = NULL; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 681 | int i, j, ret = 0, do_reloc = 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 682 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 683 | if (unlikely(!abi16)) |
| 684 | return -ENOMEM; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 685 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 686 | list_for_each_entry(temp, &abi16->channels, head) { |
Ben Skeggs | fcf3f91 | 2015-09-04 14:40:32 +1000 | [diff] [blame] | 687 | if (temp->chan->chid == req->channel) { |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 688 | chan = temp->chan; |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | if (!chan) |
| 694 | return nouveau_abi16_put(abi16, -ENOENT); |
| 695 | |
| 696 | req->vram_available = drm->gem.vram_available; |
| 697 | req->gart_available = drm->gem.gart_available; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 698 | if (unlikely(req->nr_push == 0)) |
| 699 | goto out_next; |
| 700 | |
| 701 | if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 702 | NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n", |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 703 | req->nr_push, NOUVEAU_GEM_MAX_PUSH); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 704 | return nouveau_abi16_put(abi16, -EINVAL); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 705 | } |
| 706 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 707 | if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 708 | NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n", |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 709 | req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 710 | return nouveau_abi16_put(abi16, -EINVAL); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 714 | NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n", |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 715 | req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 716 | return nouveau_abi16_put(abi16, -EINVAL); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | push = u_memcpya(req->push, req->nr_push, sizeof(*push)); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 720 | if (IS_ERR(push)) |
| 721 | return nouveau_abi16_put(abi16, PTR_ERR(push)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 722 | |
| 723 | bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo)); |
| 724 | if (IS_ERR(bo)) { |
Maarten Lankhorst | c859074 | 2013-09-02 16:31:31 +0200 | [diff] [blame] | 725 | u_free(push); |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 726 | return nouveau_abi16_put(abi16, PTR_ERR(bo)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 727 | } |
| 728 | |
Ben Skeggs | accf949 | 2012-03-16 12:40:17 +1000 | [diff] [blame] | 729 | /* Ensure all push buffers are on validate list */ |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 730 | for (i = 0; i < req->nr_push; i++) { |
| 731 | if (push[i].bo_index >= req->nr_buffers) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 732 | NV_PRINTK(err, cli, "push %d buffer not in list\n", i); |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 733 | ret = -EINVAL; |
Marcin Slusarz | 7fa0cba2 | 2011-03-07 12:31:35 +0100 | [diff] [blame] | 734 | goto out_prevalid; |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 735 | } |
Ben Skeggs | 415e618 | 2010-07-23 09:06:52 +1000 | [diff] [blame] | 736 | } |
| 737 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 738 | /* Validate buffer list */ |
| 739 | ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers, |
| 740 | req->nr_buffers, &op, &do_reloc); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 741 | if (ret) { |
Ben Skeggs | 938c40e | 2010-10-12 09:54:54 +1000 | [diff] [blame] | 742 | if (ret != -ERESTARTSYS) |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 743 | NV_PRINTK(err, cli, "validate: %d\n", ret); |
Marcin Slusarz | 7fa0cba2 | 2011-03-07 12:31:35 +0100 | [diff] [blame] | 744 | goto out_prevalid; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 745 | } |
| 746 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 747 | /* Apply any relocations that are required */ |
| 748 | if (do_reloc) { |
Marcin Slusarz | a84fa1a | 2013-02-03 22:02:47 +0100 | [diff] [blame] | 749 | ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 750 | if (ret) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 751 | NV_PRINTK(err, cli, "reloc apply: %d\n", ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 752 | goto out; |
| 753 | } |
| 754 | } |
| 755 | |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 756 | if (chan->dma.ib_max) { |
Ben Skeggs | 5e120f6 | 2012-04-30 13:55:29 +1000 | [diff] [blame] | 757 | ret = nouveau_dma_wait(chan, req->nr_push + 1, 16); |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 758 | if (ret) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 759 | NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret); |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 760 | goto out; |
| 761 | } |
| 762 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 763 | for (i = 0; i < req->nr_push; i++) { |
| 764 | struct nouveau_bo *nvbo = (void *)(unsigned long) |
| 765 | bo[push[i].bo_index].user_priv; |
| 766 | |
| 767 | nv50_dma_push(chan, nvbo, push[i].offset, |
| 768 | push[i].length); |
| 769 | } |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 770 | } else |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 771 | if (drm->device.info.chipset >= 0x25) { |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 772 | ret = RING_SPACE(chan, req->nr_push * 2); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 773 | if (ret) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 774 | NV_PRINTK(err, cli, "cal_space: %d\n", ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 775 | goto out; |
| 776 | } |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 777 | |
| 778 | for (i = 0; i < req->nr_push; i++) { |
| 779 | struct nouveau_bo *nvbo = (void *)(unsigned long) |
| 780 | bo[push[i].bo_index].user_priv; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 781 | |
Ben Skeggs | 3a92d37 | 2012-08-04 05:46:01 +1000 | [diff] [blame] | 782 | OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 783 | OUT_RING(chan, 0); |
| 784 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 785 | } else { |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 786 | ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 787 | if (ret) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 788 | NV_PRINTK(err, cli, "jmp_space: %d\n", ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 789 | goto out; |
| 790 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 791 | |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 792 | for (i = 0; i < req->nr_push; i++) { |
| 793 | struct nouveau_bo *nvbo = (void *)(unsigned long) |
| 794 | bo[push[i].bo_index].user_priv; |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 795 | uint32_t cmd; |
| 796 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 797 | cmd = chan->push.vma.offset + ((chan->dma.cur + 2) << 2); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 798 | cmd |= 0x20000000; |
| 799 | if (unlikely(cmd != req->suffix0)) { |
| 800 | if (!nvbo->kmap.virtual) { |
| 801 | ret = ttm_bo_kmap(&nvbo->bo, 0, |
| 802 | nvbo->bo.mem. |
| 803 | num_pages, |
| 804 | &nvbo->kmap); |
| 805 | if (ret) { |
| 806 | WIND_RING(chan); |
| 807 | goto out; |
| 808 | } |
| 809 | nvbo->validate_mapped = true; |
| 810 | } |
| 811 | |
| 812 | nouveau_bo_wr32(nvbo, (push[i].offset + |
| 813 | push[i].length - 8) / 4, cmd); |
| 814 | } |
| 815 | |
Ben Skeggs | 3a92d37 | 2012-08-04 05:46:01 +1000 | [diff] [blame] | 816 | OUT_RING(chan, 0x20000000 | |
| 817 | (nvbo->bo.offset + push[i].offset)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 818 | OUT_RING(chan, 0); |
Ben Skeggs | a1606a9 | 2010-02-12 10:27:35 +1000 | [diff] [blame] | 819 | for (j = 0; j < NOUVEAU_DMA_SKIPS; j++) |
| 820 | OUT_RING(chan, 0); |
| 821 | } |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 822 | } |
| 823 | |
Ben Skeggs | 264ce19 | 2013-02-14 13:43:21 +1000 | [diff] [blame] | 824 | ret = nouveau_fence_new(chan, false, &fence); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 825 | if (ret) { |
Ben Skeggs | 9ad97ed | 2015-08-20 14:54:13 +1000 | [diff] [blame] | 826 | NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 827 | WIND_RING(chan); |
| 828 | goto out; |
| 829 | } |
| 830 | |
| 831 | out: |
Maarten Lankhorst | 809e944 | 2014-04-09 16:19:30 +0200 | [diff] [blame] | 832 | validate_fini(&op, fence, bo); |
Marcin Slusarz | 382d62e | 2010-10-20 21:50:24 +0200 | [diff] [blame] | 833 | nouveau_fence_unref(&fence); |
Marcin Slusarz | 7fa0cba2 | 2011-03-07 12:31:35 +0100 | [diff] [blame] | 834 | |
| 835 | out_prevalid: |
Maarten Lankhorst | c859074 | 2013-09-02 16:31:31 +0200 | [diff] [blame] | 836 | u_free(bo); |
| 837 | u_free(push); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 838 | |
| 839 | out_next: |
Ben Skeggs | 9a391ad | 2010-02-11 16:37:26 +1000 | [diff] [blame] | 840 | if (chan->dma.ib_max) { |
| 841 | req->suffix0 = 0x00000000; |
| 842 | req->suffix1 = 0x00000000; |
| 843 | } else |
Ben Skeggs | 967e7bd | 2014-08-10 04:10:22 +1000 | [diff] [blame] | 844 | if (drm->device.info.chipset >= 0x25) { |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 845 | req->suffix0 = 0x00020000; |
| 846 | req->suffix1 = 0x00000000; |
| 847 | } else { |
| 848 | req->suffix0 = 0x20000000 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 849 | (chan->push.vma.offset + ((chan->dma.cur + 2) << 2)); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 850 | req->suffix1 = 0x00000000; |
| 851 | } |
| 852 | |
Ben Skeggs | ebb945a | 2012-07-20 08:17:34 +1000 | [diff] [blame] | 853 | return nouveau_abi16_put(abi16, ret); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 854 | } |
| 855 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 856 | int |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 857 | nouveau_gem_ioctl_cpu_prep(struct drm_device *dev, void *data, |
| 858 | struct drm_file *file_priv) |
| 859 | { |
| 860 | struct drm_nouveau_gem_cpu_prep *req = data; |
| 861 | struct drm_gem_object *gem; |
| 862 | struct nouveau_bo *nvbo; |
| 863 | bool no_wait = !!(req->flags & NOUVEAU_GEM_CPU_PREP_NOWAIT); |
Maarten Lankhorst | 59701f9 | 2014-05-14 15:38:23 +0200 | [diff] [blame] | 864 | bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE); |
Maarten Lankhorst | d0b3c3b | 2014-01-21 13:00:24 +0100 | [diff] [blame] | 865 | int ret; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 866 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 867 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); |
| 868 | if (!gem) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 869 | return -ENOENT; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 870 | nvbo = nouveau_gem_object(gem); |
| 871 | |
Maarten Lankhorst | 59701f9 | 2014-05-14 15:38:23 +0200 | [diff] [blame] | 872 | if (no_wait) |
| 873 | ret = reservation_object_test_signaled_rcu(nvbo->bo.resv, write) ? 0 : -EBUSY; |
| 874 | else { |
| 875 | long lret; |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 876 | |
Maarten Lankhorst | 59701f9 | 2014-05-14 15:38:23 +0200 | [diff] [blame] | 877 | lret = reservation_object_wait_timeout_rcu(nvbo->bo.resv, write, true, 30 * HZ); |
| 878 | if (!lret) |
| 879 | ret = -EBUSY; |
| 880 | else if (lret > 0) |
| 881 | ret = 0; |
| 882 | else |
| 883 | ret = lret; |
Maarten Lankhorst | d0b3c3b | 2014-01-21 13:00:24 +0100 | [diff] [blame] | 884 | } |
Alexandre Courbot | b22870b | 2014-10-27 18:49:19 +0900 | [diff] [blame] | 885 | nouveau_bo_sync_for_cpu(nvbo); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 886 | drm_gem_object_unreference_unlocked(gem); |
Maarten Lankhorst | d0b3c3b | 2014-01-21 13:00:24 +0100 | [diff] [blame] | 887 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 888 | return ret; |
| 889 | } |
| 890 | |
| 891 | int |
| 892 | nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data, |
| 893 | struct drm_file *file_priv) |
| 894 | { |
Alexandre Courbot | b22870b | 2014-10-27 18:49:19 +0900 | [diff] [blame] | 895 | struct drm_nouveau_gem_cpu_fini *req = data; |
| 896 | struct drm_gem_object *gem; |
| 897 | struct nouveau_bo *nvbo; |
| 898 | |
| 899 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); |
| 900 | if (!gem) |
| 901 | return -ENOENT; |
| 902 | nvbo = nouveau_gem_object(gem); |
| 903 | |
| 904 | nouveau_bo_sync_for_device(nvbo); |
| 905 | drm_gem_object_unreference_unlocked(gem); |
Ben Skeggs | 21e86c1 | 2010-10-11 11:48:45 +1000 | [diff] [blame] | 906 | return 0; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 907 | } |
| 908 | |
| 909 | int |
| 910 | nouveau_gem_ioctl_info(struct drm_device *dev, void *data, |
| 911 | struct drm_file *file_priv) |
| 912 | { |
| 913 | struct drm_nouveau_gem_info *req = data; |
| 914 | struct drm_gem_object *gem; |
| 915 | int ret; |
| 916 | |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 917 | gem = drm_gem_object_lookup(dev, file_priv, req->handle); |
| 918 | if (!gem) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 919 | return -ENOENT; |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 920 | |
Ben Skeggs | e758a31 | 2011-06-07 13:28:11 +1000 | [diff] [blame] | 921 | ret = nouveau_gem_info(file_priv, gem, req); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 922 | drm_gem_object_unreference_unlocked(gem); |
Ben Skeggs | 6ee7386 | 2009-12-11 19:24:15 +1000 | [diff] [blame] | 923 | return ret; |
| 924 | } |
| 925 | |