blob: b56524d343c3e84eb109badc3a763471fe95eff8 [file] [log] [blame]
Ben Skeggs6ee73862009-12-11 19:24:15 +10001/*
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 Skeggs6ee73862009-12-11 19:24:15 +100026
Ben Skeggs4dc28132016-05-20 09:22:55 +100027#include "nouveau_drv.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100028#include "nouveau_dma.h"
Ben Skeggsd375e7d52012-04-30 13:30:00 +100029#include "nouveau_fence.h"
Ben Skeggsebb945a2012-07-20 08:17:34 +100030#include "nouveau_abi16.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100031
Ben Skeggsebb945a2012-07-20 08:17:34 +100032#include "nouveau_ttm.h"
33#include "nouveau_gem.h"
Ben Skeggsd7722132017-11-01 03:56:20 +100034#include "nouveau_mem.h"
Ben Skeggs24e83752017-11-01 03:56:19 +100035#include "nouveau_vmm.h"
Ben Skeggs6ee73862009-12-11 19:24:15 +100036
Ben Skeggs96da0bc2017-11-01 03:56:20 +100037#include <nvif/class.h>
38
Ben Skeggs6ee73862009-12-11 19:24:15 +100039void
40nouveau_gem_object_del(struct drm_gem_object *gem)
41{
David Herrmann55fb74a2013-10-02 10:15:17 +020042 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggs5cc8d532014-12-11 10:05:00 +100043 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
Ben Skeggs6ee73862009-12-11 19:24:15 +100044 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggs5cc8d532014-12-11 10:05:00 +100045 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 Skeggs6ee73862009-12-11 19:24:15 +100051
Dave Airlie22b33e82012-04-02 11:53:06 +010052 if (gem->import_attach)
53 drm_prime_gem_destroy(gem, nvbo->bo.sg);
54
Daniel Vetterfd632aa2010-04-09 19:05:05 +000055 drm_gem_object_release(gem);
David Herrmann55fb74a2013-10-02 10:15:17 +020056
57 /* reset filp so nouveau_bo_del_ttm() can test for it */
58 gem->filp = NULL;
59 ttm_bo_unref(&bo);
Ben Skeggs5cc8d532014-12-11 10:05:00 +100060
61 pm_runtime_mark_last_busy(dev);
62 pm_runtime_put_autosuspend(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +100063}
64
65int
Ben Skeggs639212d2011-06-03 16:18:26 +100066nouveau_gem_object_open(struct drm_gem_object *gem, struct drm_file *file_priv)
67{
Ben Skeggsebb945a2012-07-20 08:17:34 +100068 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100069 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggs5cc8d532014-12-11 10:05:00 +100070 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
Ben Skeggs5cc8d532014-12-11 10:05:00 +100071 struct device *dev = drm->dev->dev;
Ben Skeggs24e83752017-11-01 03:56:19 +100072 struct nouveau_vma *vma;
Ben Skeggs2fd3db62011-06-07 15:25:12 +100073 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100074
Ben Skeggs96da0bc2017-11-01 03:56:20 +100075 if (cli->vmm.vmm.object.oclass < NVIF_CLASS_VMM_NV50)
Ben Skeggs639212d2011-06-03 16:18:26 +100076 return 0;
77
Christian Königdfd5e502016-04-06 11:12:03 +020078 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100079 if (ret)
80 return ret;
81
Ben Skeggs24e83752017-11-01 03:56:19 +100082 ret = pm_runtime_get_sync(dev);
83 if (ret < 0 && ret != -EACCES)
84 goto out;
Ben Skeggs2fd3db62011-06-07 15:25:12 +100085
Ben Skeggs24e83752017-11-01 03:56:19 +100086 ret = nouveau_vma_new(nvbo, &cli->vmm, &vma);
87 pm_runtime_mark_last_busy(dev);
88 pm_runtime_put_autosuspend(dev);
Ben Skeggs2fd3db62011-06-07 15:25:12 +100089out:
90 ttm_bo_unreserve(&nvbo->bo);
91 return ret;
Ben Skeggs639212d2011-06-03 16:18:26 +100092}
93
Ben Skeggs00d041d2017-11-01 03:56:20 +100094struct nouveau_gem_object_unmap {
95 struct nouveau_cli_work work;
96 struct nouveau_vma *vma;
97};
98
Ben Skeggsc4c70442013-05-07 09:48:30 +100099static void
Ben Skeggs00d041d2017-11-01 03:56:20 +1000100nouveau_gem_object_delete(struct nouveau_vma *vma)
Ben Skeggsc4c70442013-05-07 09:48:30 +1000101{
Ben Skeggs0db912a2018-05-08 20:39:47 +1000102 nouveau_fence_unref(&vma->fence);
Ben Skeggs24e83752017-11-01 03:56:19 +1000103 nouveau_vma_del(&vma);
Ben Skeggsc4c70442013-05-07 09:48:30 +1000104}
105
106static void
Ben Skeggs00d041d2017-11-01 03:56:20 +1000107nouveau_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
115static void
Ben Skeggs24e83752017-11-01 03:56:19 +1000116nouveau_gem_object_unmap(struct nouveau_bo *nvbo, struct nouveau_vma *vma)
Ben Skeggsc4c70442013-05-07 09:48:30 +1000117{
Ben Skeggs470db8b72018-05-08 20:39:47 +1000118 struct dma_fence *fence = vma->fence ? &vma->fence->base : NULL;
Ben Skeggs00d041d2017-11-01 03:56:20 +1000119 struct nouveau_gem_object_unmap *work;
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200120
Ben Skeggs24e83752017-11-01 03:56:19 +1000121 list_del_init(&vma->head);
Ben Skeggsc4c70442013-05-07 09:48:30 +1000122
Ben Skeggs470db8b72018-05-08 20:39:47 +1000123 if (!fence) {
Ben Skeggs00d041d2017-11-01 03:56:20 +1000124 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 Skeggsc4c70442013-05-07 09:48:30 +1000137}
138
Ben Skeggs639212d2011-06-03 16:18:26 +1000139void
140nouveau_gem_object_close(struct drm_gem_object *gem, struct drm_file *file_priv)
141{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000142 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000143 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggs5cc8d532014-12-11 10:05:00 +1000144 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
145 struct device *dev = drm->dev->dev;
Ben Skeggs24e83752017-11-01 03:56:19 +1000146 struct nouveau_vma *vma;
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000147 int ret;
Ben Skeggs639212d2011-06-03 16:18:26 +1000148
Ben Skeggs96da0bc2017-11-01 03:56:20 +1000149 if (cli->vmm.vmm.object.oclass < NVIF_CLASS_VMM_NV50)
Ben Skeggs639212d2011-06-03 16:18:26 +1000150 return;
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000151
Christian Königdfd5e502016-04-06 11:12:03 +0200152 ret = ttm_bo_reserve(&nvbo->bo, false, false, NULL);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000153 if (ret)
154 return;
155
Ben Skeggs24e83752017-11-01 03:56:19 +1000156 vma = nouveau_vma_find(nvbo, &cli->vmm);
Ben Skeggs2fd3db62011-06-07 15:25:12 +1000157 if (vma) {
Ben Skeggs24e83752017-11-01 03:56:19 +1000158 if (--vma->refs == 0) {
Ben Skeggs5cc8d532014-12-11 10:05:00 +1000159 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 Skeggs2fd3db62011-06-07 15:25:12 +1000166 }
167 ttm_bo_unreserve(&nvbo->bo);
Ben Skeggs639212d2011-06-03 16:18:26 +1000168}
169
170int
Ben Skeggs4d8b3d32016-05-23 12:34:49 +1000171nouveau_gem_new(struct nouveau_cli *cli, u64 size, int align, uint32_t domain,
Ben Skeggsf6d4e622011-06-07 12:25:36 +1000172 uint32_t tile_mode, uint32_t tile_flags,
173 struct nouveau_bo **pnvbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000174{
Ben Skeggse75c0912017-11-01 03:56:19 +1000175 struct nouveau_drm *drm = cli->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000176 struct nouveau_bo *nvbo;
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000177 u32 flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000178 int ret;
179
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000180 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 Courbot996f5452015-02-26 12:44:51 +0900187 if (domain & NOUVEAU_GEM_DOMAIN_COHERENT)
188 flags |= TTM_PL_FLAG_UNCACHED;
189
Ben Skeggsfc1b0a02016-05-24 17:29:55 +1000190 ret = nouveau_bo_new(cli, size, align, flags, tile_mode,
Maarten Lankhorstbb6178b2014-01-09 11:03:15 +0100191 tile_flags, NULL, NULL, pnvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000192 if (ret)
193 return ret;
194 nvbo = *pnvbo;
195
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000196 /* 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 Skeggs1167c6b2016-05-18 13:57:42 +1000202 if (drm->client.device.info.family >= NV_DEVICE_INFO_V0_TESLA)
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000203 nvbo->valid_domains &= domain;
204
David Herrmann55fb74a2013-10-02 10:15:17 +0200205 /* Initialize the embedded gem-object. We return a single gem-reference
206 * to the caller, instead of a normal nouveau_bo ttm reference. */
Ben Skeggsfc1b0a02016-05-24 17:29:55 +1000207 ret = drm_gem_object_init(drm->dev, &nvbo->gem, nvbo->bo.mem.size);
David Herrmann55fb74a2013-10-02 10:15:17 +0200208 if (ret) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000209 nouveau_bo_ref(NULL, pnvbo);
210 return -ENOMEM;
211 }
212
David Herrmann55fb74a2013-10-02 10:15:17 +0200213 nvbo->bo.persistent_swap_storage = nvbo->gem.filp;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000214 return 0;
215}
216
217static int
Ben Skeggse758a312011-06-07 13:28:11 +1000218nouveau_gem_info(struct drm_file *file_priv, struct drm_gem_object *gem,
219 struct drm_nouveau_gem_info *rep)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000220{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000221 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000222 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggs24e83752017-11-01 03:56:19 +1000223 struct nouveau_vma *vma;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000224
Ilia Mirkin04b8a4b2015-10-20 01:15:39 -0400225 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 Skeggs6ee73862009-12-11 19:24:15 +1000228 rep->domain = NOUVEAU_GEM_DOMAIN_GART;
229 else
230 rep->domain = NOUVEAU_GEM_DOMAIN_VRAM;
Ben Skeggse758a312011-06-07 13:28:11 +1000231 rep->offset = nvbo->bo.offset;
Ben Skeggs96da0bc2017-11-01 03:56:20 +1000232 if (cli->vmm.vmm.object.oclass >= NVIF_CLASS_VMM_NV50) {
Ben Skeggs24e83752017-11-01 03:56:19 +1000233 vma = nouveau_vma_find(nvbo, &cli->vmm);
Ben Skeggse758a312011-06-07 13:28:11 +1000234 if (!vma)
235 return -EINVAL;
236
Ben Skeggs24e83752017-11-01 03:56:19 +1000237 rep->offset = vma->addr;
Ben Skeggse758a312011-06-07 13:28:11 +1000238 }
239
Ben Skeggs6ee73862009-12-11 19:24:15 +1000240 rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
David Herrmann72525b32013-07-24 21:08:53 +0200241 rep->map_handle = drm_vma_node_offset_addr(&nvbo->bo.vma_node);
Ben Skeggs7760a2e2017-11-01 03:56:19 +1000242 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 Skeggs6ee73862009-12-11 19:24:15 +1000251 return 0;
252}
253
Ben Skeggs6ee73862009-12-11 19:24:15 +1000254int
255nouveau_gem_ioctl_new(struct drm_device *dev, void *data,
256 struct drm_file *file_priv)
257{
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100258 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000259 struct drm_nouveau_gem_new *req = data;
260 struct nouveau_bo *nvbo = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000261 int ret = 0;
262
Ben Skeggsfc1b0a02016-05-24 17:29:55 +1000263 ret = nouveau_gem_new(cli, req->info.size, req->align,
Ben Skeggs6ba9a682011-02-10 14:42:08 +1000264 req->info.domain, req->info.tile_mode,
265 req->info.tile_flags, &nvbo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000266 if (ret)
267 return ret;
268
David Herrmann55fb74a2013-10-02 10:15:17 +0200269 ret = drm_gem_handle_create(file_priv, &nvbo->gem, &req->info.handle);
Ben Skeggse758a312011-06-07 13:28:11 +1000270 if (ret == 0) {
David Herrmann55fb74a2013-10-02 10:15:17 +0200271 ret = nouveau_gem_info(file_priv, &nvbo->gem, &req->info);
Ben Skeggse758a312011-06-07 13:28:11 +1000272 if (ret)
273 drm_gem_handle_delete(file_priv, req->info.handle);
274 }
275
Dave Airlie29d08b32010-09-27 16:17:17 +1000276 /* drop reference from allocate - handle holds it now */
Thomas Zimmermann743e0f02018-06-18 14:53:11 +0200277 drm_gem_object_put_unlocked(&nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000278 return ret;
279}
280
281static int
282nouveau_gem_set_domain(struct drm_gem_object *gem, uint32_t read_domains,
283 uint32_t write_domains, uint32_t valid_domains)
284{
David Herrmann55fb74a2013-10-02 10:15:17 +0200285 struct nouveau_bo *nvbo = nouveau_gem_object(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000286 struct ttm_buffer_object *bo = &nvbo->bo;
Ben Skeggsdb5c8e22011-02-10 13:41:01 +1000287 uint32_t domains = valid_domains & nvbo->valid_domains &
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100288 (write_domains ? write_domains : read_domains);
289 uint32_t pref_flags = 0, valid_flags = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000290
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100291 if (!domains)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000292 return -EINVAL;
293
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100294 if (valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
295 valid_flags |= TTM_PL_FLAG_VRAM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000296
Francisco Jerez78ad0f72010-03-18 13:07:47 +0100297 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 Skeggs6ee73862009-12-11 19:24:15 +1000316 return 0;
317}
318
319struct validate_op {
Maarten Lankhorst92428292014-04-09 16:18:58 +0200320 struct list_head list;
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200321 struct ww_acquire_ctx ticket;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000322};
323
324static void
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200325validate_fini_no_ticket(struct validate_op *op, struct nouveau_fence *fence,
326 struct drm_nouveau_gem_pushbuf_bo *pbbo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000327{
Ben Skeggs6ee73862009-12-11 19:24:15 +1000328 struct nouveau_bo *nvbo;
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200329 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000330
Maarten Lankhorst92428292014-04-09 16:18:58 +0200331 while (!list_empty(&op->list)) {
332 nvbo = list_entry(op->list.next, struct nouveau_bo, entry);
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200333 b = &pbbo[nvbo->pbbo_index];
Ben Skeggs6ee73862009-12-11 19:24:15 +1000334
Ben Skeggs0db912a2018-05-08 20:39:47 +1000335 if (likely(fence)) {
336 struct nouveau_drm *drm = nouveau_bdev(nvbo->bo.bdev);
337 struct nouveau_vma *vma;
338
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200339 nouveau_bo_fence(nvbo, fence, !!b->write_domains);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000340
Ben Skeggs0db912a2018-05-08 20:39:47 +1000341 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 Skeggsa1606a92010-02-12 10:27:35 +1000349 if (unlikely(nvbo->validate_mapped)) {
350 ttm_bo_kunmap(&nvbo->kmap);
351 nvbo->validate_mapped = false;
352 }
353
Ben Skeggs6ee73862009-12-11 19:24:15 +1000354 list_del(&nvbo->entry);
355 nvbo->reserved_by = NULL;
Christian Königadd526b2017-11-08 21:06:03 +0100356 ttm_bo_unreserve(&nvbo->bo);
Thomas Zimmermann743e0f02018-06-18 14:53:11 +0200357 drm_gem_object_put_unlocked(&nvbo->gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000358 }
359}
360
361static void
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200362validate_fini(struct validate_op *op, struct nouveau_fence *fence,
363 struct drm_nouveau_gem_pushbuf_bo *pbbo)
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200364{
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200365 validate_fini_no_ticket(op, fence, pbbo);
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200366 ww_acquire_fini(&op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000367}
368
369static int
370validate_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 Slusarza84fa1a2013-02-03 22:02:47 +0100374 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000375 int trycnt = 0;
Arnd Bergmannb74c0a92016-10-24 17:30:38 +0200376 int ret = -EINVAL, i;
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100377 struct nouveau_bo *res_bo = NULL;
Maarten Lankhorst92428292014-04-09 16:18:58 +0200378 LIST_HEAD(gart_list);
379 LIST_HEAD(vram_list);
380 LIST_HEAD(both_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000381
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200382 ww_acquire_init(&op->ticket, &reservation_ww_class);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000383retry:
384 if (++trycnt > 100000) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000385 NV_PRINTK(err, cli, "%s failed and gave up.\n", __func__);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000386 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 Wilsona8ad0bd2016-05-09 11:04:54 +0100394 gem = drm_gem_object_lookup(file_priv, b->handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000395 if (!gem) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000396 NV_PRINTK(err, cli, "Unknown handle 0x%08x\n", b->handle);
Maarten Lankhorst92428292014-04-09 16:18:58 +0200397 ret = -ENOENT;
398 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000399 }
David Herrmann55fb74a2013-10-02 10:15:17 +0200400 nvbo = nouveau_gem_object(gem);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100401 if (nvbo == res_bo) {
402 res_bo = NULL;
Thomas Zimmermann743e0f02018-06-18 14:53:11 +0200403 drm_gem_object_put_unlocked(gem);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100404 continue;
405 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000406
407 if (nvbo->reserved_by && nvbo->reserved_by == file_priv) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000408 NV_PRINTK(err, cli, "multiple instances of buffer %d on "
Ben Skeggs6ee73862009-12-11 19:24:15 +1000409 "validation list\n", b->handle);
Thomas Zimmermann743e0f02018-06-18 14:53:11 +0200410 drm_gem_object_put_unlocked(gem);
Maarten Lankhorst92428292014-04-09 16:18:58 +0200411 ret = -EINVAL;
412 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000413 }
414
Christian Königdfd5e502016-04-06 11:12:03 +0200415 ret = ttm_bo_reserve(&nvbo->bo, true, false, &op->ticket);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000416 if (ret) {
Maarten Lankhorst92428292014-04-09 16:18:58 +0200417 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 Lankhorst809e9442014-04-09 16:19:30 +0200420 validate_fini_no_ticket(op, NULL, NULL);
Maarten Lankhorst5e338402013-06-27 13:48:19 +0200421 if (unlikely(ret == -EDEADLK)) {
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100422 ret = ttm_bo_reserve_slowpath(&nvbo->bo, true,
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200423 &op->ticket);
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100424 if (!ret)
425 res_bo = nvbo;
426 }
Ben Skeggs938c40e2010-10-12 09:54:54 +1000427 if (unlikely(ret)) {
428 if (ret != -ERESTARTSYS)
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000429 NV_PRINTK(err, cli, "fail reserve\n");
Maarten Lankhorst92428292014-04-09 16:18:58 +0200430 break;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000431 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000432 }
433
Ben Skeggs19ca10d2018-05-08 20:39:47 +1000434 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 Skeggs6ee73862009-12-11 19:24:15 +1000448 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 Lankhorst92428292014-04-09 16:18:58 +0200452 list_add_tail(&nvbo->entry, &both_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000453 else
454 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_VRAM)
Maarten Lankhorst92428292014-04-09 16:18:58 +0200455 list_add_tail(&nvbo->entry, &vram_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000456 else
457 if (b->valid_domains & NOUVEAU_GEM_DOMAIN_GART)
Maarten Lankhorst92428292014-04-09 16:18:58 +0200458 list_add_tail(&nvbo->entry, &gart_list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000459 else {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000460 NV_PRINTK(err, cli, "invalid valid domains: 0x%08x\n",
Ben Skeggs6ee73862009-12-11 19:24:15 +1000461 b->valid_domains);
Maarten Lankhorst92428292014-04-09 16:18:58 +0200462 list_add_tail(&nvbo->entry, &both_list);
463 ret = -EINVAL;
464 break;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000465 }
Maarten Lankhorstc354c892013-01-15 14:57:20 +0100466 if (nvbo == res_bo)
467 goto retry;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000468 }
469
Maarten Lankhorstecff6652013-06-27 13:48:17 +0200470 ww_acquire_done(&op->ticket);
Maarten Lankhorst92428292014-04-09 16:18:58 +0200471 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 Lankhorst809e9442014-04-09 16:19:30 +0200475 validate_fini(op, NULL, NULL);
Maarten Lankhorst92428292014-04-09 16:18:58 +0200476 return ret;
477
Ben Skeggs6ee73862009-12-11 19:24:15 +1000478}
479
480static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100481validate_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 Skeggs6ee73862009-12-11 19:24:15 +1000484{
Ben Skeggsebb945a2012-07-20 08:17:34 +1000485 struct nouveau_drm *drm = chan->drm;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000486 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 Skeggs6ee73862009-12-11 19:24:15 +1000493
David Herrmann55fb74a2013-10-02 10:15:17 +0200494 ret = nouveau_gem_set_domain(&nvbo->gem, b->read_domains,
Ben Skeggs6ee73862009-12-11 19:24:15 +1000495 b->write_domains,
496 b->valid_domains);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000497 if (unlikely(ret)) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000498 NV_PRINTK(err, cli, "fail set_domain\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000499 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000500 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000501
Maarten Lankhorst97a875c2012-11-28 11:25:44 +0000502 ret = nouveau_bo_validate(nvbo, true, false);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000503 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000504 if (ret != -ERESTARTSYS)
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000505 NV_PRINTK(err, cli, "fail ttm_validate\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000506 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000507 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000508
Maarten Lankhorste3be4c22014-09-16 11:15:07 +0200509 ret = nouveau_fence_sync(nvbo, chan, !!b->write_domains, true);
Ben Skeggs415e6182010-07-23 09:06:52 +1000510 if (unlikely(ret)) {
Maarten Lankhorst29ba89b2014-01-09 11:03:11 +0100511 if (ret != -ERESTARTSYS)
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000512 NV_PRINTK(err, cli, "fail post-validate sync\n");
Ben Skeggs415e6182010-07-23 09:06:52 +1000513 return ret;
514 }
515
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000516 if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000517 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 Skeggs6ee73862009-12-11 19:24:15 +1000523
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000524 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 Skeggs6ee73862009-12-11 19:24:15 +1000531
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100532 if (copy_to_user(&upbbo[nvbo->pbbo_index].presumed,
Ben Skeggsa3fcd0a2011-06-07 11:12:39 +1000533 &b->presumed, sizeof(b->presumed)))
534 return -EFAULT;
535 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000536 }
537
538 return relocs;
539}
540
541static int
542nouveau_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 Slusarza84fa1a2013-02-03 22:02:47 +0100548 struct nouveau_cli *cli = nouveau_cli(file_priv);
Maarten Lankhorst92428292014-04-09 16:18:58 +0200549 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000550
Maarten Lankhorst92428292014-04-09 16:18:58 +0200551 INIT_LIST_HEAD(&op->list);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000552
Ben Skeggs6ee73862009-12-11 19:24:15 +1000553 if (nr_buffers == 0)
554 return 0;
555
556 ret = validate_init(chan, file_priv, pbbo, nr_buffers, op);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000557 if (unlikely(ret)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000558 if (ret != -ERESTARTSYS)
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000559 NV_PRINTK(err, cli, "validate_init\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000560 return ret;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000561 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000562
Maarten Lankhorst92428292014-04-09 16:18:58 +0200563 ret = validate_list(chan, cli, &op->list, pbbo, user_buffers);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000564 if (unlikely(ret < 0)) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000565 if (ret != -ERESTARTSYS)
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000566 NV_PRINTK(err, cli, "validating bo list\n");
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200567 validate_fini(op, NULL, NULL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000568 return ret;
569 }
Maarten Lankhorst92428292014-04-09 16:18:58 +0200570 *apply_relocs = ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000571 return 0;
572}
573
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200574static inline void
575u_free(void *addr)
576{
Pekka Enberg48a20132015-06-30 14:59:18 -0700577 kvfree(addr);
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200578}
579
Ben Skeggs6ee73862009-12-11 19:24:15 +1000580static inline void *
581u_memcpya(uint64_t user, unsigned nmemb, unsigned size)
582{
583 void *mem;
584 void __user *userptr = (void __force __user *)(uintptr_t)user;
585
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200586 size *= nmemb;
587
Michal Hocko752ade62017-05-08 15:57:27 -0700588 mem = kvmalloc(size, GFP_KERNEL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000589 if (!mem)
590 return ERR_PTR(-ENOMEM);
591
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100592 if (copy_from_user(mem, userptr, size)) {
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200593 u_free(mem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000594 return ERR_PTR(-EFAULT);
595 }
596
597 return mem;
598}
599
600static int
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100601nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
Ben Skeggsa1606a92010-02-12 10:27:35 +1000602 struct drm_nouveau_gem_pushbuf *req,
603 struct drm_nouveau_gem_pushbuf_bo *bo)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000604{
605 struct drm_nouveau_gem_pushbuf_reloc *reloc = NULL;
Luca Barbieri12f735b2010-01-10 20:10:53 +0100606 int ret = 0;
607 unsigned i;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000608
Ben Skeggsa1606a92010-02-12 10:27:35 +1000609 reloc = u_memcpya(req->relocs, req->nr_relocs, sizeof(*reloc));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000610 if (IS_ERR(reloc))
611 return PTR_ERR(reloc);
612
Ben Skeggsa1606a92010-02-12 10:27:35 +1000613 for (i = 0; i < req->nr_relocs; i++) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000614 struct drm_nouveau_gem_pushbuf_reloc *r = &reloc[i];
615 struct drm_nouveau_gem_pushbuf_bo *b;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000616 struct nouveau_bo *nvbo;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000617 uint32_t data;
618
Dan Carpenter7f073d02018-07-03 15:30:56 +0300619 if (unlikely(r->bo_index >= req->nr_buffers)) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000620 NV_PRINTK(err, cli, "reloc bo index invalid\n");
Ben Skeggs6ee73862009-12-11 19:24:15 +1000621 ret = -EINVAL;
622 break;
623 }
624
625 b = &bo[r->bo_index];
Ben Skeggsa1606a92010-02-12 10:27:35 +1000626 if (b->presumed.valid)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000627 continue;
628
Dan Carpenter7f073d02018-07-03 15:30:56 +0300629 if (unlikely(r->reloc_bo_index >= req->nr_buffers)) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000630 NV_PRINTK(err, cli, "reloc container bo index invalid\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000631 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 Skeggs9ad97ed2015-08-20 14:54:13 +1000638 NV_PRINTK(err, cli, "reloc outside of bo\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000639 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 Skeggs9ad97ed2015-08-20 14:54:13 +1000647 NV_PRINTK(err, cli, "failed kmap for reloc\n");
Ben Skeggsa1606a92010-02-12 10:27:35 +1000648 break;
649 }
650 nvbo->validate_mapped = true;
651 }
652
Ben Skeggs6ee73862009-12-11 19:24:15 +1000653 if (r->flags & NOUVEAU_GEM_RELOC_LOW)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000654 data = b->presumed.offset + r->data;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000655 else
656 if (r->flags & NOUVEAU_GEM_RELOC_HIGH)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000657 data = (b->presumed.offset + r->data) >> 32;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000658 else
659 data = r->data;
660
661 if (r->flags & NOUVEAU_GEM_RELOC_OR) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000662 if (b->presumed.domain == NOUVEAU_GEM_DOMAIN_GART)
Ben Skeggs6ee73862009-12-11 19:24:15 +1000663 data |= r->tor;
664 else
665 data |= r->vor;
666 }
667
Christian König8aa6d4f2016-04-06 11:12:04 +0200668 ret = ttm_bo_wait(&nvbo->bo, false, false);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000669 if (ret) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000670 NV_PRINTK(err, cli, "reloc wait_idle failed: %d\n", ret);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000671 break;
672 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000673
674 nouveau_bo_wr32(nvbo, r->reloc_bo_offset >> 2, data);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000675 }
676
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200677 u_free(reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000678 return ret;
679}
680
681int
682nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
683 struct drm_file *file_priv)
684{
Ben Skeggs09433f22015-11-03 10:17:49 +1000685 struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv);
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100686 struct nouveau_cli *cli = nouveau_cli(file_priv);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000687 struct nouveau_abi16_chan *temp;
688 struct nouveau_drm *drm = nouveau_drm(dev);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000689 struct drm_nouveau_gem_pushbuf *req = data;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000690 struct drm_nouveau_gem_pushbuf_push *push;
691 struct drm_nouveau_gem_pushbuf_bo *bo;
Ben Skeggsebb945a2012-07-20 08:17:34 +1000692 struct nouveau_channel *chan = NULL;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000693 struct validate_op op;
Francisco Jerez6e86e042010-07-03 18:36:39 +0200694 struct nouveau_fence *fence = NULL;
Ben Skeggsa1606a92010-02-12 10:27:35 +1000695 int i, j, ret = 0, do_reloc = 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000696
Ben Skeggsebb945a2012-07-20 08:17:34 +1000697 if (unlikely(!abi16))
698 return -ENOMEM;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000699
Ben Skeggsebb945a2012-07-20 08:17:34 +1000700 list_for_each_entry(temp, &abi16->channels, head) {
Ben Skeggsfcf3f912015-09-04 14:40:32 +1000701 if (temp->chan->chid == req->channel) {
Ben Skeggsebb945a2012-07-20 08:17:34 +1000702 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 Skeggsa1606a92010-02-12 10:27:35 +1000712 if (unlikely(req->nr_push == 0))
713 goto out_next;
714
715 if (unlikely(req->nr_push > NOUVEAU_GEM_MAX_PUSH)) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000716 NV_PRINTK(err, cli, "pushbuf push count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000717 req->nr_push, NOUVEAU_GEM_MAX_PUSH);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000718 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000719 }
720
Ben Skeggsa1606a92010-02-12 10:27:35 +1000721 if (unlikely(req->nr_buffers > NOUVEAU_GEM_MAX_BUFFERS)) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000722 NV_PRINTK(err, cli, "pushbuf bo count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000723 req->nr_buffers, NOUVEAU_GEM_MAX_BUFFERS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000724 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000725 }
726
727 if (unlikely(req->nr_relocs > NOUVEAU_GEM_MAX_RELOCS)) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000728 NV_PRINTK(err, cli, "pushbuf reloc count exceeds limit: %d max %d\n",
Ben Skeggsa1606a92010-02-12 10:27:35 +1000729 req->nr_relocs, NOUVEAU_GEM_MAX_RELOCS);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000730 return nouveau_abi16_put(abi16, -EINVAL);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000731 }
732
733 push = u_memcpya(req->push, req->nr_push, sizeof(*push));
Ben Skeggsebb945a2012-07-20 08:17:34 +1000734 if (IS_ERR(push))
735 return nouveau_abi16_put(abi16, PTR_ERR(push));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000736
737 bo = u_memcpya(req->buffers, req->nr_buffers, sizeof(*bo));
738 if (IS_ERR(bo)) {
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200739 u_free(push);
Ben Skeggsebb945a2012-07-20 08:17:34 +1000740 return nouveau_abi16_put(abi16, PTR_ERR(bo));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000741 }
742
Ben Skeggsaccf9492012-03-16 12:40:17 +1000743 /* Ensure all push buffers are on validate list */
Ben Skeggs415e6182010-07-23 09:06:52 +1000744 for (i = 0; i < req->nr_push; i++) {
745 if (push[i].bo_index >= req->nr_buffers) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000746 NV_PRINTK(err, cli, "push %d buffer not in list\n", i);
Ben Skeggs415e6182010-07-23 09:06:52 +1000747 ret = -EINVAL;
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100748 goto out_prevalid;
Ben Skeggs415e6182010-07-23 09:06:52 +1000749 }
Ben Skeggs415e6182010-07-23 09:06:52 +1000750 }
751
Ben Skeggs6ee73862009-12-11 19:24:15 +1000752 /* Validate buffer list */
753 ret = nouveau_gem_pushbuf_validate(chan, file_priv, bo, req->buffers,
754 req->nr_buffers, &op, &do_reloc);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000755 if (ret) {
Ben Skeggs938c40e2010-10-12 09:54:54 +1000756 if (ret != -ERESTARTSYS)
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000757 NV_PRINTK(err, cli, "validate: %d\n", ret);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100758 goto out_prevalid;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000759 }
760
Ben Skeggs6ee73862009-12-11 19:24:15 +1000761 /* Apply any relocations that are required */
762 if (do_reloc) {
Marcin Slusarza84fa1a2013-02-03 22:02:47 +0100763 ret = nouveau_gem_pushbuf_reloc_apply(cli, req, bo);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000764 if (ret) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000765 NV_PRINTK(err, cli, "reloc apply: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000766 goto out;
767 }
768 }
769
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000770 if (chan->dma.ib_max) {
Ben Skeggs5e120f62012-04-30 13:55:29 +1000771 ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000772 if (ret) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000773 NV_PRINTK(err, cli, "nv50cal_space: %d\n", ret);
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000774 goto out;
775 }
776
Ben Skeggsa1606a92010-02-12 10:27:35 +1000777 for (i = 0; i < req->nr_push; i++) {
Ben Skeggs19ca10d2018-05-08 20:39:47 +1000778 struct nouveau_vma *vma = (void *)(unsigned long)
Ben Skeggsa1606a92010-02-12 10:27:35 +1000779 bo[push[i].bo_index].user_priv;
780
Ben Skeggs19ca10d2018-05-08 20:39:47 +1000781 nv50_dma_push(chan, vma->addr + push[i].offset,
Ben Skeggsa1606a92010-02-12 10:27:35 +1000782 push[i].length);
783 }
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000784 } else
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000785 if (drm->client.device.info.chipset >= 0x25) {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000786 ret = RING_SPACE(chan, req->nr_push * 2);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000787 if (ret) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000788 NV_PRINTK(err, cli, "cal_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000789 goto out;
790 }
Ben Skeggsa1606a92010-02-12 10:27:35 +1000791
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 Skeggsa1606a92010-02-12 10:27:35 +1000795
Ben Skeggs3a92d372012-08-04 05:46:01 +1000796 OUT_RING(chan, (nvbo->bo.offset + push[i].offset) | 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000797 OUT_RING(chan, 0);
798 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000799 } else {
Ben Skeggsa1606a92010-02-12 10:27:35 +1000800 ret = RING_SPACE(chan, req->nr_push * (2 + NOUVEAU_DMA_SKIPS));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000801 if (ret) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000802 NV_PRINTK(err, cli, "jmp_space: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000803 goto out;
804 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000805
Ben Skeggsa1606a92010-02-12 10:27:35 +1000806 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 Skeggsa1606a92010-02-12 10:27:35 +1000809 uint32_t cmd;
810
Ben Skeggs24e83752017-11-01 03:56:19 +1000811 cmd = chan->push.addr + ((chan->dma.cur + 2) << 2);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000812 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 Skeggs3a92d372012-08-04 05:46:01 +1000830 OUT_RING(chan, 0x20000000 |
831 (nvbo->bo.offset + push[i].offset));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000832 OUT_RING(chan, 0);
Ben Skeggsa1606a92010-02-12 10:27:35 +1000833 for (j = 0; j < NOUVEAU_DMA_SKIPS; j++)
834 OUT_RING(chan, 0);
835 }
Ben Skeggs6ee73862009-12-11 19:24:15 +1000836 }
837
Ben Skeggs264ce192013-02-14 13:43:21 +1000838 ret = nouveau_fence_new(chan, false, &fence);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000839 if (ret) {
Ben Skeggs9ad97ed2015-08-20 14:54:13 +1000840 NV_PRINTK(err, cli, "error fencing pushbuf: %d\n", ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000841 WIND_RING(chan);
842 goto out;
843 }
844
845out:
Maarten Lankhorst809e9442014-04-09 16:19:30 +0200846 validate_fini(&op, fence, bo);
Marcin Slusarz382d62e2010-10-20 21:50:24 +0200847 nouveau_fence_unref(&fence);
Marcin Slusarz7fa0cba22011-03-07 12:31:35 +0100848
849out_prevalid:
Maarten Lankhorstc8590742013-09-02 16:31:31 +0200850 u_free(bo);
851 u_free(push);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000852
853out_next:
Ben Skeggs9a391ad2010-02-11 16:37:26 +1000854 if (chan->dma.ib_max) {
855 req->suffix0 = 0x00000000;
856 req->suffix1 = 0x00000000;
857 } else
Ben Skeggs1167c6b2016-05-18 13:57:42 +1000858 if (drm->client.device.info.chipset >= 0x25) {
Ben Skeggs6ee73862009-12-11 19:24:15 +1000859 req->suffix0 = 0x00020000;
860 req->suffix1 = 0x00000000;
861 } else {
862 req->suffix0 = 0x20000000 |
Ben Skeggs24e83752017-11-01 03:56:19 +1000863 (chan->push.addr + ((chan->dma.cur + 2) << 2));
Ben Skeggs6ee73862009-12-11 19:24:15 +1000864 req->suffix1 = 0x00000000;
865 }
866
Ben Skeggsebb945a2012-07-20 08:17:34 +1000867 return nouveau_abi16_put(abi16, ret);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000868}
869
Ben Skeggs6ee73862009-12-11 19:24:15 +1000870int
Ben Skeggs6ee73862009-12-11 19:24:15 +1000871nouveau_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 Lankhorst59701f92014-05-14 15:38:23 +0200878 bool write = !!(req->flags & NOUVEAU_GEM_CPU_PREP_WRITE);
Chris Wilson491d8a12016-08-29 08:08:27 +0100879 long lret;
Maarten Lankhorstd0b3c3b2014-01-21 13:00:24 +0100880 int ret;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000881
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100882 gem = drm_gem_object_lookup(file_priv, req->handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000883 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100884 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000885 nvbo = nouveau_gem_object(gem);
886
Chris Wilson491d8a12016-08-29 08:08:27 +0100887 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 Lankhorstf2c24b82014-04-02 17:14:48 +0200895
Alexandre Courbotb22870b2014-10-27 18:49:19 +0900896 nouveau_bo_sync_for_cpu(nvbo);
Thomas Zimmermann743e0f02018-06-18 14:53:11 +0200897 drm_gem_object_put_unlocked(gem);
Maarten Lankhorstd0b3c3b2014-01-21 13:00:24 +0100898
Ben Skeggs6ee73862009-12-11 19:24:15 +1000899 return ret;
900}
901
902int
903nouveau_gem_ioctl_cpu_fini(struct drm_device *dev, void *data,
904 struct drm_file *file_priv)
905{
Alexandre Courbotb22870b2014-10-27 18:49:19 +0900906 struct drm_nouveau_gem_cpu_fini *req = data;
907 struct drm_gem_object *gem;
908 struct nouveau_bo *nvbo;
909
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100910 gem = drm_gem_object_lookup(file_priv, req->handle);
Alexandre Courbotb22870b2014-10-27 18:49:19 +0900911 if (!gem)
912 return -ENOENT;
913 nvbo = nouveau_gem_object(gem);
914
915 nouveau_bo_sync_for_device(nvbo);
Thomas Zimmermann743e0f02018-06-18 14:53:11 +0200916 drm_gem_object_put_unlocked(gem);
Ben Skeggs21e86c12010-10-11 11:48:45 +1000917 return 0;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000918}
919
920int
921nouveau_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 Wilsona8ad0bd2016-05-09 11:04:54 +0100928 gem = drm_gem_object_lookup(file_priv, req->handle);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000929 if (!gem)
Chris Wilsonbf79cb92010-08-04 14:19:46 +0100930 return -ENOENT;
Ben Skeggs6ee73862009-12-11 19:24:15 +1000931
Ben Skeggse758a312011-06-07 13:28:11 +1000932 ret = nouveau_gem_info(file_priv, gem, req);
Thomas Zimmermann743e0f02018-06-18 14:53:11 +0200933 drm_gem_object_put_unlocked(gem);
Ben Skeggs6ee73862009-12-11 19:24:15 +1000934 return ret;
935}
936