blob: 1d214b66650ff5a3865b04074ee7e7ebd632f2a4 [file] [log] [blame]
Jerome Glisse771fe6b2009-06-05 14:42:42 +02001/*
2 * Copyright 2008 Jerome Glisse.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Jerome Glisse <glisse@freedesktop.org>
26 */
David Howells760285e2012-10-02 18:01:07 +010027#include <drm/drmP.h>
28#include <drm/radeon_drm.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020029#include "radeon_reg.h"
30#include "radeon.h"
31
Lauri Kasanen1109ca02012-08-31 13:43:50 -040032static int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
Jerome Glisse771fe6b2009-06-05 14:42:42 +020033{
34 struct drm_device *ddev = p->rdev->ddev;
35 struct radeon_cs_chunk *chunk;
36 unsigned i, j;
37 bool duplicate;
38
39 if (p->chunk_relocs_idx == -1) {
40 return 0;
41 }
42 chunk = &p->chunks[p->chunk_relocs_idx];
Alex Deuchercf4ccd02011-11-18 10:19:47 -050043 p->dma_reloc_idx = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020044 /* FIXME: we assume that each relocs use 4 dwords */
45 p->nrelocs = chunk->length_dw / 4;
46 p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL);
47 if (p->relocs_ptr == NULL) {
48 return -ENOMEM;
49 }
50 p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL);
51 if (p->relocs == NULL) {
52 return -ENOMEM;
53 }
54 for (i = 0; i < p->nrelocs; i++) {
55 struct drm_radeon_cs_reloc *r;
56
57 duplicate = false;
58 r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
Christian König16557f12011-10-24 14:59:17 +020059 for (j = 0; j < i; j++) {
Jerome Glisse771fe6b2009-06-05 14:42:42 +020060 if (r->handle == p->relocs[j].handle) {
61 p->relocs_ptr[i] = &p->relocs[j];
62 duplicate = true;
63 break;
64 }
65 }
66 if (!duplicate) {
67 p->relocs[i].gobj = drm_gem_object_lookup(ddev,
68 p->filp,
69 r->handle);
70 if (p->relocs[i].gobj == NULL) {
71 DRM_ERROR("gem object lookup failed 0x%x\n",
72 r->handle);
Chris Wilsonbf79cb92010-08-04 14:19:46 +010073 return -ENOENT;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020074 }
75 p->relocs_ptr[i] = &p->relocs[i];
Daniel Vetter7e4d15d2011-02-18 17:59:17 +010076 p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
Jerome Glisse4c788672009-11-20 14:29:23 +010077 p->relocs[i].lobj.bo = p->relocs[i].robj;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020078 p->relocs[i].lobj.wdomain = r->write_domain;
Thomas Hellstrom147666f2010-11-17 12:38:32 +000079 p->relocs[i].lobj.rdomain = r->read_domains;
80 p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020081 p->relocs[i].handle = r->handle;
82 p->relocs[i].flags = r->flags;
Jerome Glisse4c788672009-11-20 14:29:23 +010083 radeon_bo_list_add_object(&p->relocs[i].lobj,
Thomas Hellstrom147666f2010-11-17 12:38:32 +000084 &p->validated);
Christian König93504fc2012-01-05 22:11:06 -050085
Christian König16557f12011-10-24 14:59:17 +020086 } else
87 p->relocs[i].handle = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +020088 }
Jerome Glisse94429bb2010-02-15 21:36:33 +010089 return radeon_bo_list_validate(&p->validated);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020090}
91
Jerome Glisse721604a2012-01-05 22:11:05 -050092static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
93{
94 p->priority = priority;
95
96 switch (ring) {
97 default:
98 DRM_ERROR("unknown ring id: %d\n", ring);
99 return -EINVAL;
100 case RADEON_CS_RING_GFX:
101 p->ring = RADEON_RING_TYPE_GFX_INDEX;
102 break;
103 case RADEON_CS_RING_COMPUTE:
Alex Deucher8d5ef7b2012-03-20 17:18:24 -0400104 if (p->rdev->family >= CHIP_TAHITI) {
105 if (p->priority > 0)
106 p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
107 else
108 p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
109 } else
110 p->ring = RADEON_RING_TYPE_GFX_INDEX;
Jerome Glisse721604a2012-01-05 22:11:05 -0500111 break;
Alex Deucher278a3342012-12-13 12:27:28 -0500112 case RADEON_CS_RING_DMA:
113 if (p->rdev->family >= CHIP_CAYMAN) {
114 if (p->priority > 0)
115 p->ring = R600_RING_TYPE_DMA_INDEX;
116 else
117 p->ring = CAYMAN_RING_TYPE_DMA1_INDEX;
118 } else if (p->rdev->family >= CHIP_R600) {
119 p->ring = R600_RING_TYPE_DMA_INDEX;
120 } else {
121 return -EINVAL;
122 }
123 break;
Jerome Glisse721604a2012-01-05 22:11:05 -0500124 }
125 return 0;
126}
127
Christian Königf82cbdd2012-08-09 16:35:36 +0200128static void radeon_cs_sync_to(struct radeon_cs_parser *p,
129 struct radeon_fence *fence)
130{
131 struct radeon_fence *other;
132
133 if (!fence)
134 return;
135
136 other = p->ib.sync_to[fence->ring];
137 p->ib.sync_to[fence->ring] = radeon_fence_later(fence, other);
138}
139
Christian König220907d2012-05-10 16:46:43 +0200140static void radeon_cs_sync_rings(struct radeon_cs_parser *p)
Christian König93504fc2012-01-05 22:11:06 -0500141{
Christian König220907d2012-05-10 16:46:43 +0200142 int i;
Christian König93504fc2012-01-05 22:11:06 -0500143
Christian Königcdac5502012-02-23 15:18:42 +0100144 for (i = 0; i < p->nrelocs; i++) {
Christian Königf82cbdd2012-08-09 16:35:36 +0200145 if (!p->relocs[i].robj)
Christian Königcdac5502012-02-23 15:18:42 +0100146 continue;
147
Christian Königf82cbdd2012-08-09 16:35:36 +0200148 radeon_cs_sync_to(p, p->relocs[i].robj->tbo.sync_obj);
Christian Königcdac5502012-02-23 15:18:42 +0100149 }
Christian König93504fc2012-01-05 22:11:06 -0500150}
151
Alex Deucher9b001472012-05-30 10:09:30 -0400152/* XXX: note that this is called from the legacy UMS CS ioctl as well */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200153int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
154{
155 struct drm_radeon_cs *cs = data;
156 uint64_t *chunk_array_ptr;
Jerome Glisse721604a2012-01-05 22:11:05 -0500157 unsigned size, i;
158 u32 ring = RADEON_CS_RING_GFX;
159 s32 priority = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200160
161 if (!cs->num_chunks) {
162 return 0;
163 }
164 /* get chunks */
165 INIT_LIST_HEAD(&p->validated);
166 p->idx = 0;
Jerome Glissef2e39222012-05-09 15:35:02 +0200167 p->ib.sa_bo = NULL;
168 p->ib.semaphore = NULL;
169 p->const_ib.sa_bo = NULL;
170 p->const_ib.semaphore = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200171 p->chunk_ib_idx = -1;
172 p->chunk_relocs_idx = -1;
Jerome Glisse721604a2012-01-05 22:11:05 -0500173 p->chunk_flags_idx = -1;
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400174 p->chunk_const_ib_idx = -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200175 p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
176 if (p->chunks_array == NULL) {
177 return -ENOMEM;
178 }
179 chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
180 if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
181 sizeof(uint64_t)*cs->num_chunks)) {
182 return -EFAULT;
183 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500184 p->cs_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200185 p->nchunks = cs->num_chunks;
186 p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
187 if (p->chunks == NULL) {
188 return -ENOMEM;
189 }
190 for (i = 0; i < p->nchunks; i++) {
191 struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
192 struct drm_radeon_cs_chunk user_chunk;
193 uint32_t __user *cdata;
194
195 chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
196 if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
197 sizeof(struct drm_radeon_cs_chunk))) {
198 return -EFAULT;
199 }
Dave Airlie5176fdc2009-06-30 11:47:14 +1000200 p->chunks[i].length_dw = user_chunk.length_dw;
201 p->chunks[i].kdata = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200202 p->chunks[i].chunk_id = user_chunk.chunk_id;
Ilija Hadzic580f8392013-01-02 18:27:37 -0500203 p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200204 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
205 p->chunk_relocs_idx = i;
206 }
207 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
208 p->chunk_ib_idx = i;
Dave Airlie5176fdc2009-06-30 11:47:14 +1000209 /* zero length IB isn't useful */
210 if (p->chunks[i].length_dw == 0)
211 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200212 }
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400213 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) {
214 p->chunk_const_ib_idx = i;
215 /* zero length CONST IB isn't useful */
216 if (p->chunks[i].length_dw == 0)
217 return -EINVAL;
218 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500219 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
220 p->chunk_flags_idx = i;
221 /* zero length flags aren't useful */
222 if (p->chunks[i].length_dw == 0)
223 return -EINVAL;
Marek Olšáke70f2242011-10-25 01:38:45 +0200224 }
Dave Airlie5176fdc2009-06-30 11:47:14 +1000225
Dave Airlie513bcb42009-09-23 16:56:27 +1000226 cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
Jerome Glisse721604a2012-01-05 22:11:05 -0500227 if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) ||
228 (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) {
Dave Airlie513bcb42009-09-23 16:56:27 +1000229 size = p->chunks[i].length_dw * sizeof(uint32_t);
230 p->chunks[i].kdata = kmalloc(size, GFP_KERNEL);
231 if (p->chunks[i].kdata == NULL) {
232 return -ENOMEM;
233 }
234 if (DRM_COPY_FROM_USER(p->chunks[i].kdata,
235 p->chunks[i].user_ptr, size)) {
236 return -EFAULT;
237 }
Marek Olšáke70f2242011-10-25 01:38:45 +0200238 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500239 p->cs_flags = p->chunks[i].kdata[0];
240 if (p->chunks[i].length_dw > 1)
241 ring = p->chunks[i].kdata[1];
242 if (p->chunks[i].length_dw > 2)
243 priority = (s32)p->chunks[i].kdata[2];
Marek Olšáke70f2242011-10-25 01:38:45 +0200244 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200245 }
246 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500247
Alex Deucher9b001472012-05-30 10:09:30 -0400248 /* these are KMS only */
249 if (p->rdev) {
250 if ((p->cs_flags & RADEON_CS_USE_VM) &&
251 !p->rdev->vm_manager.enabled) {
252 DRM_ERROR("VM not active on asic!\n");
253 return -EINVAL;
254 }
255
256 /* we only support VM on SI+ */
257 if ((p->rdev->family >= CHIP_TAHITI) &&
258 ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
259 DRM_ERROR("VM required on SI+!\n");
260 return -EINVAL;
261 }
262
263 if (radeon_cs_get_ring(p, ring, priority))
264 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200265 }
Marek Olšáke70f2242011-10-25 01:38:45 +0200266
Jerome Glisse721604a2012-01-05 22:11:05 -0500267 /* deal with non-vm */
268 if ((p->chunk_ib_idx != -1) &&
269 ((p->cs_flags & RADEON_CS_USE_VM) == 0) &&
270 (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) {
271 if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
272 DRM_ERROR("cs IB too big: %d\n",
273 p->chunks[p->chunk_ib_idx].length_dw);
274 return -EINVAL;
275 }
Ilija Hadzicff4bd082013-01-07 18:21:57 -0500276 if (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) {
Dave Airlie6a7068b2012-04-03 16:23:41 +0100277 p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
278 p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
279 if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL ||
280 p->chunks[p->chunk_ib_idx].kpage[1] == NULL) {
Ilija Hadzic25d89992013-01-07 18:21:59 -0500281 kfree(p->chunks[p->chunk_ib_idx].kpage[0]);
282 kfree(p->chunks[p->chunk_ib_idx].kpage[1]);
Ilija Hadzic1da80cf2013-01-23 13:59:05 -0500283 p->chunks[p->chunk_ib_idx].kpage[0] = NULL;
284 p->chunks[p->chunk_ib_idx].kpage[1] = NULL;
Dave Airlie6a7068b2012-04-03 16:23:41 +0100285 return -ENOMEM;
286 }
287 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500288 p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1;
289 p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1;
290 p->chunks[p->chunk_ib_idx].last_copied_page = -1;
291 p->chunks[p->chunk_ib_idx].last_page_index =
292 ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE;
293 }
294
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200295 return 0;
296}
297
298/**
299 * cs_parser_fini() - clean parser states
300 * @parser: parser structure holding parsing context.
301 * @error: error number
302 *
303 * If error is set than unvalidate buffer, otherwise just free memory
304 * used by parsing context.
305 **/
306static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error)
307{
308 unsigned i;
309
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400310 if (!error) {
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000311 ttm_eu_fence_buffer_objects(&parser->validated,
Jerome Glissef2e39222012-05-09 15:35:02 +0200312 parser->ib.fence);
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400313 } else {
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000314 ttm_eu_backoff_reservation(&parser->validated);
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400315 }
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000316
Pauli Nieminenfcbc4512010-03-19 07:44:33 +0000317 if (parser->relocs != NULL) {
318 for (i = 0; i < parser->nrelocs; i++) {
319 if (parser->relocs[i].gobj)
320 drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
321 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200322 }
Michel Dänzer48e113e2009-09-15 17:09:32 +0200323 kfree(parser->track);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200324 kfree(parser->relocs);
325 kfree(parser->relocs_ptr);
326 for (i = 0; i < parser->nchunks; i++) {
327 kfree(parser->chunks[i].kdata);
Dave Airlie6a7068b2012-04-03 16:23:41 +0100328 if ((parser->rdev->flags & RADEON_IS_AGP)) {
329 kfree(parser->chunks[i].kpage[0]);
330 kfree(parser->chunks[i].kpage[1]);
331 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200332 }
333 kfree(parser->chunks);
334 kfree(parser->chunks_array);
335 radeon_ib_free(parser->rdev, &parser->ib);
Jerome Glissef2e39222012-05-09 15:35:02 +0200336 radeon_ib_free(parser->rdev, &parser->const_ib);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200337}
338
Jerome Glisse721604a2012-01-05 22:11:05 -0500339static int radeon_cs_ib_chunk(struct radeon_device *rdev,
340 struct radeon_cs_parser *parser)
341{
342 struct radeon_cs_chunk *ib_chunk;
343 int r;
344
345 if (parser->chunk_ib_idx == -1)
346 return 0;
347
348 if (parser->cs_flags & RADEON_CS_USE_VM)
349 return 0;
350
351 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
352 /* Copy the packet into the IB, the parser will read from the
353 * input memory (cached) and write to the IB (which can be
354 * uncached).
355 */
356 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
Christian König4bf3dd92012-08-06 18:57:44 +0200357 NULL, ib_chunk->length_dw * 4);
Jerome Glisse721604a2012-01-05 22:11:05 -0500358 if (r) {
359 DRM_ERROR("Failed to get ib !\n");
360 return r;
361 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200362 parser->ib.length_dw = ib_chunk->length_dw;
Christian Königeb0c19c2012-02-23 15:18:44 +0100363 r = radeon_cs_parse(rdev, parser->ring, parser);
Jerome Glisse721604a2012-01-05 22:11:05 -0500364 if (r || parser->parser_error) {
365 DRM_ERROR("Invalid command stream !\n");
366 return r;
367 }
368 r = radeon_cs_finish_pages(parser);
369 if (r) {
370 DRM_ERROR("Invalid command stream !\n");
371 return r;
372 }
Christian König220907d2012-05-10 16:46:43 +0200373 radeon_cs_sync_rings(parser);
Christian König4ef72562012-07-13 13:06:00 +0200374 r = radeon_ib_schedule(rdev, &parser->ib, NULL);
Jerome Glisse721604a2012-01-05 22:11:05 -0500375 if (r) {
376 DRM_ERROR("Failed to schedule IB !\n");
377 }
Christian König93bf8882012-07-03 14:05:41 +0200378 return r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500379}
380
381static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
382 struct radeon_vm *vm)
383{
Jerome Glisse3e8970f2012-08-13 12:07:33 -0400384 struct radeon_device *rdev = parser->rdev;
Jerome Glisse721604a2012-01-05 22:11:05 -0500385 struct radeon_bo_list *lobj;
386 struct radeon_bo *bo;
387 int r;
388
Jerome Glisse3e8970f2012-08-13 12:07:33 -0400389 r = radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo, &rdev->ring_tmp_bo.bo->tbo.mem);
390 if (r) {
391 return r;
392 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500393 list_for_each_entry(lobj, &parser->validated, tv.head) {
394 bo = lobj->bo;
395 r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
396 if (r) {
397 return r;
398 }
399 }
400 return 0;
401}
402
403static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
404 struct radeon_cs_parser *parser)
405{
406 struct radeon_cs_chunk *ib_chunk;
407 struct radeon_fpriv *fpriv = parser->filp->driver_priv;
408 struct radeon_vm *vm = &fpriv->vm;
409 int r;
410
411 if (parser->chunk_ib_idx == -1)
412 return 0;
Jerome Glisse721604a2012-01-05 22:11:05 -0500413 if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
414 return 0;
415
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400416 if ((rdev->family >= CHIP_TAHITI) &&
417 (parser->chunk_const_ib_idx != -1)) {
418 ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
419 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
420 DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
421 return -EINVAL;
422 }
423 r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
Christian König4bf3dd92012-08-06 18:57:44 +0200424 vm, ib_chunk->length_dw * 4);
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400425 if (r) {
426 DRM_ERROR("Failed to get const ib !\n");
427 return r;
428 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200429 parser->const_ib.is_const_ib = true;
430 parser->const_ib.length_dw = ib_chunk->length_dw;
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400431 /* Copy the packet into the IB */
Jerome Glissef2e39222012-05-09 15:35:02 +0200432 if (DRM_COPY_FROM_USER(parser->const_ib.ptr, ib_chunk->user_ptr,
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400433 ib_chunk->length_dw * 4)) {
434 return -EFAULT;
435 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200436 r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400437 if (r) {
438 return r;
439 }
440 }
441
Jerome Glisse721604a2012-01-05 22:11:05 -0500442 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
443 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
444 DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
445 return -EINVAL;
446 }
447 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
Christian König4bf3dd92012-08-06 18:57:44 +0200448 vm, ib_chunk->length_dw * 4);
Jerome Glisse721604a2012-01-05 22:11:05 -0500449 if (r) {
450 DRM_ERROR("Failed to get ib !\n");
451 return r;
452 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200453 parser->ib.length_dw = ib_chunk->length_dw;
Jerome Glisse721604a2012-01-05 22:11:05 -0500454 /* Copy the packet into the IB */
Jerome Glissef2e39222012-05-09 15:35:02 +0200455 if (DRM_COPY_FROM_USER(parser->ib.ptr, ib_chunk->user_ptr,
Jerome Glisse721604a2012-01-05 22:11:05 -0500456 ib_chunk->length_dw * 4)) {
457 return -EFAULT;
458 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200459 r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
Jerome Glisse721604a2012-01-05 22:11:05 -0500460 if (r) {
461 return r;
462 }
463
Christian König36ff39c2012-05-09 10:07:08 +0200464 mutex_lock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500465 mutex_lock(&vm->mutex);
Christian Königddf03f52012-08-09 20:02:28 +0200466 r = radeon_vm_alloc_pt(rdev, vm);
Jerome Glisse721604a2012-01-05 22:11:05 -0500467 if (r) {
468 goto out;
469 }
470 r = radeon_bo_vm_update_pte(parser, vm);
471 if (r) {
472 goto out;
473 }
Christian König220907d2012-05-10 16:46:43 +0200474 radeon_cs_sync_rings(parser);
Christian König1678dbc22012-09-06 13:48:07 +0200475 radeon_cs_sync_to(parser, vm->fence);
Christian Königee60e292012-08-09 16:21:08 +0200476 radeon_cs_sync_to(parser, radeon_vm_grab_id(rdev, vm, parser->ring));
Christian König4ef72562012-07-13 13:06:00 +0200477
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400478 if ((rdev->family >= CHIP_TAHITI) &&
479 (parser->chunk_const_ib_idx != -1)) {
Christian König4ef72562012-07-13 13:06:00 +0200480 r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib);
481 } else {
482 r = radeon_ib_schedule(rdev, &parser->ib, NULL);
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400483 }
484
Jerome Glisse721604a2012-01-05 22:11:05 -0500485 if (!r) {
Christian Königee60e292012-08-09 16:21:08 +0200486 radeon_vm_fence(rdev, vm, parser->ib.fence);
Jerome Glisse721604a2012-01-05 22:11:05 -0500487 }
Christian Königee60e292012-08-09 16:21:08 +0200488
489out:
Christian König13e55c32012-10-09 13:31:19 +0200490 radeon_vm_add_to_lru(rdev, vm);
Christian König36ff39c2012-05-09 10:07:08 +0200491 mutex_unlock(&vm->mutex);
492 mutex_unlock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500493 return r;
494}
495
Christian König6c6f4782012-05-02 15:11:19 +0200496static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
497{
498 if (r == -EDEADLK) {
499 r = radeon_gpu_reset(rdev);
500 if (!r)
501 r = -EAGAIN;
502 }
503 return r;
504}
505
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200506int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
507{
508 struct radeon_device *rdev = dev->dev_private;
509 struct radeon_cs_parser parser;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200510 int r;
511
Jerome Glissedee53e72012-07-02 12:45:19 -0400512 down_read(&rdev->exclusive_lock);
Jerome Glisse6b7746e2012-02-20 17:57:20 -0500513 if (!rdev->accel_working) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400514 up_read(&rdev->exclusive_lock);
Jerome Glisse6b7746e2012-02-20 17:57:20 -0500515 return -EBUSY;
516 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200517 /* initialize parser */
518 memset(&parser, 0, sizeof(struct radeon_cs_parser));
519 parser.filp = filp;
520 parser.rdev = rdev;
Jerome Glissec8c15ff2010-01-18 13:01:36 +0100521 parser.dev = rdev->dev;
Dave Airlie428c6e32011-06-08 19:58:29 +1000522 parser.family = rdev->family;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200523 r = radeon_cs_parser_init(&parser, data);
524 if (r) {
525 DRM_ERROR("Failed to initialize parser !\n");
526 radeon_cs_parser_fini(&parser, r);
Jerome Glissedee53e72012-07-02 12:45:19 -0400527 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200528 r = radeon_cs_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200529 return r;
530 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200531 r = radeon_cs_parser_relocs(&parser);
532 if (r) {
Dave Airlie97f23b32010-03-19 10:33:44 +1000533 if (r != -ERESTARTSYS)
534 DRM_ERROR("Failed to parse relocation %d!\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200535 radeon_cs_parser_fini(&parser, r);
Jerome Glissedee53e72012-07-02 12:45:19 -0400536 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200537 r = radeon_cs_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200538 return r;
539 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500540 r = radeon_cs_ib_chunk(rdev, &parser);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200541 if (r) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500542 goto out;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200543 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500544 r = radeon_cs_ib_vm_chunk(rdev, &parser);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200545 if (r) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500546 goto out;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200547 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500548out:
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200549 radeon_cs_parser_fini(&parser, r);
Jerome Glissedee53e72012-07-02 12:45:19 -0400550 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200551 r = radeon_cs_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200552 return r;
553}
Dave Airlie513bcb42009-09-23 16:56:27 +1000554
555int radeon_cs_finish_pages(struct radeon_cs_parser *p)
556{
557 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
558 int i;
559 int size = PAGE_SIZE;
560
561 for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
562 if (i == ibc->last_page_index) {
563 size = (ibc->length_dw * 4) % PAGE_SIZE;
564 if (size == 0)
565 size = PAGE_SIZE;
566 }
567
Jerome Glissef2e39222012-05-09 15:35:02 +0200568 if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
Dave Airlie513bcb42009-09-23 16:56:27 +1000569 ibc->user_ptr + (i * PAGE_SIZE),
570 size))
571 return -EFAULT;
572 }
573 return 0;
574}
575
Dave Airliec4c7f312012-05-26 17:34:24 +0100576static int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
Dave Airlie513bcb42009-09-23 16:56:27 +1000577{
578 int new_page;
Dave Airlie513bcb42009-09-23 16:56:27 +1000579 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
580 int i;
581 int size = PAGE_SIZE;
Ilija Hadzicff4bd082013-01-07 18:21:57 -0500582 bool copy1 = (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) ?
583 false : true;
Dave Airlie513bcb42009-09-23 16:56:27 +1000584
Dave Airliec5e617e2009-09-26 09:03:39 +1000585 for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
Jerome Glissef2e39222012-05-09 15:35:02 +0200586 if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
Dave Airlie513bcb42009-09-23 16:56:27 +1000587 ibc->user_ptr + (i * PAGE_SIZE),
588 PAGE_SIZE)) {
589 p->parser_error = -EFAULT;
590 return 0;
591 }
592 }
593
Dave Airlie513bcb42009-09-23 16:56:27 +1000594 if (pg_idx == ibc->last_page_index) {
595 size = (ibc->length_dw * 4) % PAGE_SIZE;
Dave Airlie6a7068b2012-04-03 16:23:41 +0100596 if (size == 0)
597 size = PAGE_SIZE;
Dave Airlie513bcb42009-09-23 16:56:27 +1000598 }
599
Dave Airlie6a7068b2012-04-03 16:23:41 +0100600 new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
601 if (copy1)
Jerome Glissef2e39222012-05-09 15:35:02 +0200602 ibc->kpage[new_page] = p->ib.ptr + (pg_idx * (PAGE_SIZE / 4));
Dave Airlie6a7068b2012-04-03 16:23:41 +0100603
Dave Airlie513bcb42009-09-23 16:56:27 +1000604 if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
605 ibc->user_ptr + (pg_idx * PAGE_SIZE),
606 size)) {
607 p->parser_error = -EFAULT;
608 return 0;
609 }
610
Dave Airlie6a7068b2012-04-03 16:23:41 +0100611 /* copy to IB for non single case */
612 if (!copy1)
Jerome Glissef2e39222012-05-09 15:35:02 +0200613 memcpy((void *)(p->ib.ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
Dave Airlie513bcb42009-09-23 16:56:27 +1000614
615 ibc->last_copied_page = pg_idx;
616 ibc->kpage_idx[new_page] = pg_idx;
617
618 return new_page;
619}
Dave Airliec4c7f312012-05-26 17:34:24 +0100620
621u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
622{
623 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
624 u32 pg_idx, pg_offset;
625 u32 idx_value = 0;
626 int new_page;
627
628 pg_idx = (idx * 4) / PAGE_SIZE;
629 pg_offset = (idx * 4) % PAGE_SIZE;
630
631 if (ibc->kpage_idx[0] == pg_idx)
632 return ibc->kpage[0][pg_offset/4];
633 if (ibc->kpage_idx[1] == pg_idx)
634 return ibc->kpage[1][pg_offset/4];
635
636 new_page = radeon_cs_update_pages(p, pg_idx);
637 if (new_page < 0) {
638 p->parser_error = new_page;
639 return 0;
640 }
641
642 idx_value = ibc->kpage[new_page][pg_offset/4];
643 return idx_value;
644}
Ilija Hadzic4db01312013-01-02 18:27:40 -0500645
646/**
647 * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet
648 * @parser: parser structure holding parsing context.
649 * @pkt: where to store packet information
650 *
651 * Assume that chunk_ib_index is properly set. Will return -EINVAL
652 * if packet is bigger than remaining ib size. or if packets is unknown.
653 **/
654int radeon_cs_packet_parse(struct radeon_cs_parser *p,
655 struct radeon_cs_packet *pkt,
656 unsigned idx)
657{
658 struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
659 struct radeon_device *rdev = p->rdev;
660 uint32_t header;
661
662 if (idx >= ib_chunk->length_dw) {
663 DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
664 idx, ib_chunk->length_dw);
665 return -EINVAL;
666 }
667 header = radeon_get_ib_value(p, idx);
668 pkt->idx = idx;
669 pkt->type = RADEON_CP_PACKET_GET_TYPE(header);
670 pkt->count = RADEON_CP_PACKET_GET_COUNT(header);
671 pkt->one_reg_wr = 0;
672 switch (pkt->type) {
673 case RADEON_PACKET_TYPE0:
674 if (rdev->family < CHIP_R600) {
675 pkt->reg = R100_CP_PACKET0_GET_REG(header);
676 pkt->one_reg_wr =
677 RADEON_CP_PACKET0_GET_ONE_REG_WR(header);
678 } else
679 pkt->reg = R600_CP_PACKET0_GET_REG(header);
680 break;
681 case RADEON_PACKET_TYPE3:
682 pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header);
683 break;
684 case RADEON_PACKET_TYPE2:
685 pkt->count = -1;
686 break;
687 default:
688 DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
689 return -EINVAL;
690 }
691 if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
692 DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
693 pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
694 return -EINVAL;
695 }
696 return 0;
697}
Ilija Hadzic9ffb7a62013-01-02 18:27:42 -0500698
699/**
700 * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP
701 * @p: structure holding the parser context.
702 *
703 * Check if the next packet is NOP relocation packet3.
704 **/
705bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
706{
707 struct radeon_cs_packet p3reloc;
708 int r;
709
710 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
711 if (r)
712 return false;
713 if (p3reloc.type != RADEON_PACKET_TYPE3)
714 return false;
715 if (p3reloc.opcode != RADEON_PACKET3_NOP)
716 return false;
717 return true;
718}
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -0500719
720/**
721 * radeon_cs_dump_packet() - dump raw packet context
722 * @p: structure holding the parser context.
723 * @pkt: structure holding the packet.
724 *
725 * Used mostly for debugging and error reporting.
726 **/
727void radeon_cs_dump_packet(struct radeon_cs_parser *p,
728 struct radeon_cs_packet *pkt)
729{
730 volatile uint32_t *ib;
731 unsigned i;
732 unsigned idx;
733
734 ib = p->ib.ptr;
735 idx = pkt->idx;
736 for (i = 0; i <= (pkt->count + 1); i++, idx++)
737 DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
738}
739
Ilija Hadzice9716992013-01-02 18:27:46 -0500740/**
741 * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet
742 * @parser: parser structure holding parsing context.
743 * @data: pointer to relocation data
744 * @offset_start: starting offset
745 * @offset_mask: offset mask (to align start offset on)
746 * @reloc: reloc informations
747 *
748 * Check if next packet is relocation packet3, do bo validation and compute
749 * GPU offset using the provided start.
750 **/
751int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
752 struct radeon_cs_reloc **cs_reloc,
753 int nomm)
754{
755 struct radeon_cs_chunk *relocs_chunk;
756 struct radeon_cs_packet p3reloc;
757 unsigned idx;
758 int r;
759
760 if (p->chunk_relocs_idx == -1) {
761 DRM_ERROR("No relocation chunk !\n");
762 return -EINVAL;
763 }
764 *cs_reloc = NULL;
765 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
766 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
767 if (r)
768 return r;
769 p->idx += p3reloc.count + 2;
770 if (p3reloc.type != RADEON_PACKET_TYPE3 ||
771 p3reloc.opcode != RADEON_PACKET3_NOP) {
772 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
773 p3reloc.idx);
774 radeon_cs_dump_packet(p, &p3reloc);
775 return -EINVAL;
776 }
777 idx = radeon_get_ib_value(p, p3reloc.idx + 1);
778 if (idx >= relocs_chunk->length_dw) {
779 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
780 idx, relocs_chunk->length_dw);
781 radeon_cs_dump_packet(p, &p3reloc);
782 return -EINVAL;
783 }
784 /* FIXME: we assume reloc size is 4 dwords */
785 if (nomm) {
786 *cs_reloc = p->relocs;
787 (*cs_reloc)->lobj.gpu_offset =
788 (u64)relocs_chunk->kdata[idx + 3] << 32;
789 (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
790 } else
791 *cs_reloc = p->relocs_ptr[(idx / 4)];
792 return 0;
793}