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