drm/radeon: fix panel scaling with eDP and LVDS bridges
[deliverable/linux.git] / drivers / gpu / drm / radeon / radeon_cs.c
CommitLineData
771fe6b9
JG
1/*
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 */
760285e7
DH
27#include <drm/drmP.h>
28#include <drm/radeon_drm.h>
771fe6b9
JG
29#include "radeon_reg.h"
30#include "radeon.h"
860024e5 31#include "radeon_trace.h"
771fe6b9 32
1109ca09 33static int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
771fe6b9
JG
34{
35 struct drm_device *ddev = p->rdev->ddev;
36 struct radeon_cs_chunk *chunk;
37 unsigned i, j;
38 bool duplicate;
39
40 if (p->chunk_relocs_idx == -1) {
41 return 0;
42 }
43 chunk = &p->chunks[p->chunk_relocs_idx];
cf4ccd01 44 p->dma_reloc_idx = 0;
771fe6b9
JG
45 /* FIXME: we assume that each relocs use 4 dwords */
46 p->nrelocs = chunk->length_dw / 4;
47 p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL);
48 if (p->relocs_ptr == NULL) {
49 return -ENOMEM;
50 }
51 p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL);
52 if (p->relocs == NULL) {
53 return -ENOMEM;
54 }
55 for (i = 0; i < p->nrelocs; i++) {
56 struct drm_radeon_cs_reloc *r;
57
58 duplicate = false;
59 r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
16557f1e 60 for (j = 0; j < i; j++) {
771fe6b9
JG
61 if (r->handle == p->relocs[j].handle) {
62 p->relocs_ptr[i] = &p->relocs[j];
63 duplicate = true;
64 break;
65 }
66 }
4474f3a9 67 if (duplicate) {
16557f1e 68 p->relocs[i].handle = 0;
4474f3a9
CK
69 continue;
70 }
71
72 p->relocs[i].gobj = drm_gem_object_lookup(ddev, p->filp,
73 r->handle);
74 if (p->relocs[i].gobj == NULL) {
75 DRM_ERROR("gem object lookup failed 0x%x\n",
76 r->handle);
77 return -ENOENT;
78 }
79 p->relocs_ptr[i] = &p->relocs[i];
80 p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
81 p->relocs[i].lobj.bo = p->relocs[i].robj;
82 p->relocs[i].lobj.written = !!r->write_domain;
83
f2ba57b5
CK
84 /* the first reloc of an UVD job is the
85 msg and that must be in VRAM */
86 if (p->ring == R600_RING_TYPE_UVD_INDEX && i == 0) {
87 /* TODO: is this still needed for NI+ ? */
88 p->relocs[i].lobj.domain =
89 RADEON_GEM_DOMAIN_VRAM;
90
91 p->relocs[i].lobj.alt_domain =
92 RADEON_GEM_DOMAIN_VRAM;
93
94 } else {
95 uint32_t domain = r->write_domain ?
96 r->write_domain : r->read_domains;
97
98 p->relocs[i].lobj.domain = domain;
99 if (domain == RADEON_GEM_DOMAIN_VRAM)
100 domain |= RADEON_GEM_DOMAIN_GTT;
101 p->relocs[i].lobj.alt_domain = domain;
102 }
4474f3a9
CK
103
104 p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
105 p->relocs[i].handle = r->handle;
106
107 radeon_bo_list_add_object(&p->relocs[i].lobj,
108 &p->validated);
771fe6b9 109 }
ecff665f 110 return radeon_bo_list_validate(&p->ticket, &p->validated, p->ring);
771fe6b9
JG
111}
112
721604a1
JG
113static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
114{
115 p->priority = priority;
116
117 switch (ring) {
118 default:
119 DRM_ERROR("unknown ring id: %d\n", ring);
120 return -EINVAL;
121 case RADEON_CS_RING_GFX:
122 p->ring = RADEON_RING_TYPE_GFX_INDEX;
123 break;
124 case RADEON_CS_RING_COMPUTE:
963e81f9 125 if (p->rdev->family >= CHIP_TAHITI) {
8d5ef7b1
AD
126 if (p->priority > 0)
127 p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
128 else
129 p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
130 } else
131 p->ring = RADEON_RING_TYPE_GFX_INDEX;
721604a1 132 break;
278a334c
AD
133 case RADEON_CS_RING_DMA:
134 if (p->rdev->family >= CHIP_CAYMAN) {
135 if (p->priority > 0)
136 p->ring = R600_RING_TYPE_DMA_INDEX;
137 else
138 p->ring = CAYMAN_RING_TYPE_DMA1_INDEX;
139 } else if (p->rdev->family >= CHIP_R600) {
140 p->ring = R600_RING_TYPE_DMA_INDEX;
141 } else {
142 return -EINVAL;
143 }
144 break;
f2ba57b5
CK
145 case RADEON_CS_RING_UVD:
146 p->ring = R600_RING_TYPE_UVD_INDEX;
147 break;
721604a1
JG
148 }
149 return 0;
150}
151
220907d9 152static void radeon_cs_sync_rings(struct radeon_cs_parser *p)
93504fce 153{
220907d9 154 int i;
93504fce 155
cdac5504 156 for (i = 0; i < p->nrelocs; i++) {
f82cbddd 157 if (!p->relocs[i].robj)
cdac5504
CK
158 continue;
159
43f1214a 160 radeon_ib_sync_to(&p->ib, p->relocs[i].robj->tbo.sync_obj);
8f676c4c 161 }
93504fce
CK
162}
163
9b00147d 164/* XXX: note that this is called from the legacy UMS CS ioctl as well */
771fe6b9
JG
165int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
166{
167 struct drm_radeon_cs *cs = data;
168 uint64_t *chunk_array_ptr;
721604a1
JG
169 unsigned size, i;
170 u32 ring = RADEON_CS_RING_GFX;
171 s32 priority = 0;
771fe6b9
JG
172
173 if (!cs->num_chunks) {
174 return 0;
175 }
176 /* get chunks */
177 INIT_LIST_HEAD(&p->validated);
178 p->idx = 0;
f2e39221
JG
179 p->ib.sa_bo = NULL;
180 p->ib.semaphore = NULL;
181 p->const_ib.sa_bo = NULL;
182 p->const_ib.semaphore = NULL;
771fe6b9
JG
183 p->chunk_ib_idx = -1;
184 p->chunk_relocs_idx = -1;
721604a1 185 p->chunk_flags_idx = -1;
dfcf5f36 186 p->chunk_const_ib_idx = -1;
771fe6b9
JG
187 p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
188 if (p->chunks_array == NULL) {
189 return -ENOMEM;
190 }
191 chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
192 if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
193 sizeof(uint64_t)*cs->num_chunks)) {
194 return -EFAULT;
195 }
721604a1 196 p->cs_flags = 0;
771fe6b9
JG
197 p->nchunks = cs->num_chunks;
198 p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
199 if (p->chunks == NULL) {
200 return -ENOMEM;
201 }
202 for (i = 0; i < p->nchunks; i++) {
203 struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
204 struct drm_radeon_cs_chunk user_chunk;
205 uint32_t __user *cdata;
206
207 chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
208 if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
209 sizeof(struct drm_radeon_cs_chunk))) {
210 return -EFAULT;
211 }
5176fdc4
DA
212 p->chunks[i].length_dw = user_chunk.length_dw;
213 p->chunks[i].kdata = NULL;
771fe6b9 214 p->chunks[i].chunk_id = user_chunk.chunk_id;
580f8398 215 p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
771fe6b9
JG
216 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
217 p->chunk_relocs_idx = i;
218 }
219 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
220 p->chunk_ib_idx = i;
5176fdc4
DA
221 /* zero length IB isn't useful */
222 if (p->chunks[i].length_dw == 0)
223 return -EINVAL;
771fe6b9 224 }
dfcf5f36
AD
225 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) {
226 p->chunk_const_ib_idx = i;
227 /* zero length CONST IB isn't useful */
228 if (p->chunks[i].length_dw == 0)
229 return -EINVAL;
230 }
721604a1
JG
231 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
232 p->chunk_flags_idx = i;
233 /* zero length flags aren't useful */
234 if (p->chunks[i].length_dw == 0)
235 return -EINVAL;
e70f224c 236 }
5176fdc4 237
513bcb46 238 cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
721604a1
JG
239 if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) ||
240 (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) {
513bcb46
DA
241 size = p->chunks[i].length_dw * sizeof(uint32_t);
242 p->chunks[i].kdata = kmalloc(size, GFP_KERNEL);
243 if (p->chunks[i].kdata == NULL) {
244 return -ENOMEM;
245 }
246 if (DRM_COPY_FROM_USER(p->chunks[i].kdata,
247 p->chunks[i].user_ptr, size)) {
248 return -EFAULT;
249 }
e70f224c 250 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
721604a1
JG
251 p->cs_flags = p->chunks[i].kdata[0];
252 if (p->chunks[i].length_dw > 1)
253 ring = p->chunks[i].kdata[1];
254 if (p->chunks[i].length_dw > 2)
255 priority = (s32)p->chunks[i].kdata[2];
e70f224c 256 }
771fe6b9
JG
257 }
258 }
721604a1 259
9b00147d
AD
260 /* these are KMS only */
261 if (p->rdev) {
262 if ((p->cs_flags & RADEON_CS_USE_VM) &&
263 !p->rdev->vm_manager.enabled) {
264 DRM_ERROR("VM not active on asic!\n");
265 return -EINVAL;
266 }
1b5475db 267
57449040 268 if (radeon_cs_get_ring(p, ring, priority))
9b00147d 269 return -EINVAL;
721604a1 270
57449040 271 /* we only support VM on some SI+ rings */
76a0df85 272 if ((p->rdev->asic->ring[p->ring]->cs_parse == NULL) &&
57449040
CK
273 ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
274 DRM_ERROR("Ring %d requires VM!\n", p->ring);
9b00147d 275 return -EINVAL;
57449040 276 }
9b00147d 277 }
721604a1
JG
278
279 /* deal with non-vm */
280 if ((p->chunk_ib_idx != -1) &&
281 ((p->cs_flags & RADEON_CS_USE_VM) == 0) &&
282 (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) {
283 if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
284 DRM_ERROR("cs IB too big: %d\n",
285 p->chunks[p->chunk_ib_idx].length_dw);
286 return -EINVAL;
287 }
ff4bd082 288 if (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) {
6a7068b4
DA
289 p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
290 p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
291 if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL ||
292 p->chunks[p->chunk_ib_idx].kpage[1] == NULL) {
25d89997
IH
293 kfree(p->chunks[p->chunk_ib_idx].kpage[0]);
294 kfree(p->chunks[p->chunk_ib_idx].kpage[1]);
1da80cfa
IH
295 p->chunks[p->chunk_ib_idx].kpage[0] = NULL;
296 p->chunks[p->chunk_ib_idx].kpage[1] = NULL;
6a7068b4
DA
297 return -ENOMEM;
298 }
299 }
721604a1
JG
300 p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1;
301 p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1;
302 p->chunks[p->chunk_ib_idx].last_copied_page = -1;
303 p->chunks[p->chunk_ib_idx].last_page_index =
304 ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE;
305 }
306
771fe6b9
JG
307 return 0;
308}
309
310/**
311 * cs_parser_fini() - clean parser states
312 * @parser: parser structure holding parsing context.
313 * @error: error number
314 *
315 * If error is set than unvalidate buffer, otherwise just free memory
316 * used by parsing context.
317 **/
ecff665f 318static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error, bool backoff)
771fe6b9
JG
319{
320 unsigned i;
321
e43b5ec0 322 if (!error) {
ecff665f
ML
323 ttm_eu_fence_buffer_objects(&parser->ticket,
324 &parser->validated,
f2e39221 325 parser->ib.fence);
ecff665f
ML
326 } else if (backoff) {
327 ttm_eu_backoff_reservation(&parser->ticket,
328 &parser->validated);
e43b5ec0 329 }
147666fb 330
fcbc451b
PN
331 if (parser->relocs != NULL) {
332 for (i = 0; i < parser->nrelocs; i++) {
333 if (parser->relocs[i].gobj)
334 drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
335 }
771fe6b9 336 }
48e113e5 337 kfree(parser->track);
771fe6b9
JG
338 kfree(parser->relocs);
339 kfree(parser->relocs_ptr);
340 for (i = 0; i < parser->nchunks; i++) {
341 kfree(parser->chunks[i].kdata);
6a7068b4
DA
342 if ((parser->rdev->flags & RADEON_IS_AGP)) {
343 kfree(parser->chunks[i].kpage[0]);
344 kfree(parser->chunks[i].kpage[1]);
345 }
771fe6b9
JG
346 }
347 kfree(parser->chunks);
348 kfree(parser->chunks_array);
349 radeon_ib_free(parser->rdev, &parser->ib);
f2e39221 350 radeon_ib_free(parser->rdev, &parser->const_ib);
771fe6b9
JG
351}
352
721604a1
JG
353static int radeon_cs_ib_chunk(struct radeon_device *rdev,
354 struct radeon_cs_parser *parser)
355{
356 struct radeon_cs_chunk *ib_chunk;
357 int r;
358
359 if (parser->chunk_ib_idx == -1)
360 return 0;
361
362 if (parser->cs_flags & RADEON_CS_USE_VM)
363 return 0;
364
365 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
366 /* Copy the packet into the IB, the parser will read from the
367 * input memory (cached) and write to the IB (which can be
368 * uncached).
369 */
370 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
4bf3dd92 371 NULL, ib_chunk->length_dw * 4);
721604a1
JG
372 if (r) {
373 DRM_ERROR("Failed to get ib !\n");
374 return r;
375 }
f2e39221 376 parser->ib.length_dw = ib_chunk->length_dw;
eb0c19c5 377 r = radeon_cs_parse(rdev, parser->ring, parser);
721604a1
JG
378 if (r || parser->parser_error) {
379 DRM_ERROR("Invalid command stream !\n");
380 return r;
381 }
382 r = radeon_cs_finish_pages(parser);
383 if (r) {
384 DRM_ERROR("Invalid command stream !\n");
385 return r;
386 }
ce3537d5
AD
387
388 if (parser->ring == R600_RING_TYPE_UVD_INDEX)
389 radeon_uvd_note_usage(rdev);
390
220907d9 391 radeon_cs_sync_rings(parser);
4ef72566 392 r = radeon_ib_schedule(rdev, &parser->ib, NULL);
721604a1
JG
393 if (r) {
394 DRM_ERROR("Failed to schedule IB !\n");
395 }
93bf888c 396 return r;
721604a1
JG
397}
398
399static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
400 struct radeon_vm *vm)
401{
3e8970f9 402 struct radeon_device *rdev = parser->rdev;
721604a1
JG
403 struct radeon_bo_list *lobj;
404 struct radeon_bo *bo;
405 int r;
406
3e8970f9
JG
407 r = radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo, &rdev->ring_tmp_bo.bo->tbo.mem);
408 if (r) {
409 return r;
410 }
721604a1
JG
411 list_for_each_entry(lobj, &parser->validated, tv.head) {
412 bo = lobj->bo;
413 r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
414 if (r) {
415 return r;
416 }
417 }
418 return 0;
419}
420
421static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
422 struct radeon_cs_parser *parser)
423{
424 struct radeon_cs_chunk *ib_chunk;
425 struct radeon_fpriv *fpriv = parser->filp->driver_priv;
426 struct radeon_vm *vm = &fpriv->vm;
427 int r;
428
429 if (parser->chunk_ib_idx == -1)
430 return 0;
721604a1
JG
431 if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
432 return 0;
433
dfcf5f36
AD
434 if ((rdev->family >= CHIP_TAHITI) &&
435 (parser->chunk_const_ib_idx != -1)) {
436 ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
437 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
438 DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
439 return -EINVAL;
440 }
441 r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
4bf3dd92 442 vm, ib_chunk->length_dw * 4);
dfcf5f36
AD
443 if (r) {
444 DRM_ERROR("Failed to get const ib !\n");
445 return r;
446 }
f2e39221
JG
447 parser->const_ib.is_const_ib = true;
448 parser->const_ib.length_dw = ib_chunk->length_dw;
dfcf5f36 449 /* Copy the packet into the IB */
f2e39221 450 if (DRM_COPY_FROM_USER(parser->const_ib.ptr, ib_chunk->user_ptr,
dfcf5f36
AD
451 ib_chunk->length_dw * 4)) {
452 return -EFAULT;
453 }
f2e39221 454 r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
dfcf5f36
AD
455 if (r) {
456 return r;
457 }
458 }
459
721604a1
JG
460 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
461 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
462 DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
463 return -EINVAL;
464 }
465 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
4bf3dd92 466 vm, ib_chunk->length_dw * 4);
721604a1
JG
467 if (r) {
468 DRM_ERROR("Failed to get ib !\n");
469 return r;
470 }
f2e39221 471 parser->ib.length_dw = ib_chunk->length_dw;
721604a1 472 /* Copy the packet into the IB */
f2e39221 473 if (DRM_COPY_FROM_USER(parser->ib.ptr, ib_chunk->user_ptr,
721604a1
JG
474 ib_chunk->length_dw * 4)) {
475 return -EFAULT;
476 }
f2e39221 477 r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
721604a1
JG
478 if (r) {
479 return r;
480 }
481
ce3537d5
AD
482 if (parser->ring == R600_RING_TYPE_UVD_INDEX)
483 radeon_uvd_note_usage(rdev);
484
36ff39c4 485 mutex_lock(&rdev->vm_manager.lock);
721604a1 486 mutex_lock(&vm->mutex);
ddf03f5c 487 r = radeon_vm_alloc_pt(rdev, vm);
721604a1
JG
488 if (r) {
489 goto out;
490 }
491 r = radeon_bo_vm_update_pte(parser, vm);
492 if (r) {
493 goto out;
494 }
220907d9 495 radeon_cs_sync_rings(parser);
43f1214a
AD
496 radeon_ib_sync_to(&parser->ib, vm->fence);
497 radeon_ib_sync_to(&parser->ib, radeon_vm_grab_id(
498 rdev, vm, parser->ring));
4ef72566 499
dfcf5f36
AD
500 if ((rdev->family >= CHIP_TAHITI) &&
501 (parser->chunk_const_ib_idx != -1)) {
4ef72566
CK
502 r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib);
503 } else {
504 r = radeon_ib_schedule(rdev, &parser->ib, NULL);
dfcf5f36
AD
505 }
506
721604a1 507 if (!r) {
ee60e29f 508 radeon_vm_fence(rdev, vm, parser->ib.fence);
721604a1 509 }
ee60e29f
CK
510
511out:
13e55c38 512 radeon_vm_add_to_lru(rdev, vm);
36ff39c4
CK
513 mutex_unlock(&vm->mutex);
514 mutex_unlock(&rdev->vm_manager.lock);
721604a1
JG
515 return r;
516}
517
6c6f4783
CK
518static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
519{
520 if (r == -EDEADLK) {
521 r = radeon_gpu_reset(rdev);
522 if (!r)
523 r = -EAGAIN;
524 }
525 return r;
526}
527
771fe6b9
JG
528int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
529{
530 struct radeon_device *rdev = dev->dev_private;
531 struct radeon_cs_parser parser;
771fe6b9
JG
532 int r;
533
dee53e7f 534 down_read(&rdev->exclusive_lock);
6b7746e8 535 if (!rdev->accel_working) {
dee53e7f 536 up_read(&rdev->exclusive_lock);
6b7746e8
JG
537 return -EBUSY;
538 }
771fe6b9
JG
539 /* initialize parser */
540 memset(&parser, 0, sizeof(struct radeon_cs_parser));
541 parser.filp = filp;
542 parser.rdev = rdev;
c8c15ff1 543 parser.dev = rdev->dev;
428c6e36 544 parser.family = rdev->family;
771fe6b9
JG
545 r = radeon_cs_parser_init(&parser, data);
546 if (r) {
547 DRM_ERROR("Failed to initialize parser !\n");
ecff665f 548 radeon_cs_parser_fini(&parser, r, false);
dee53e7f 549 up_read(&rdev->exclusive_lock);
6c6f4783 550 r = radeon_cs_handle_lockup(rdev, r);
771fe6b9
JG
551 return r;
552 }
771fe6b9
JG
553 r = radeon_cs_parser_relocs(&parser);
554 if (r) {
97f23b3d
DA
555 if (r != -ERESTARTSYS)
556 DRM_ERROR("Failed to parse relocation %d!\n", r);
ecff665f 557 radeon_cs_parser_fini(&parser, r, false);
dee53e7f 558 up_read(&rdev->exclusive_lock);
6c6f4783 559 r = radeon_cs_handle_lockup(rdev, r);
771fe6b9
JG
560 return r;
561 }
55b51c88 562
860024e5
CK
563 trace_radeon_cs(&parser);
564
721604a1 565 r = radeon_cs_ib_chunk(rdev, &parser);
771fe6b9 566 if (r) {
721604a1 567 goto out;
771fe6b9 568 }
721604a1 569 r = radeon_cs_ib_vm_chunk(rdev, &parser);
771fe6b9 570 if (r) {
721604a1 571 goto out;
771fe6b9 572 }
721604a1 573out:
ecff665f 574 radeon_cs_parser_fini(&parser, r, true);
dee53e7f 575 up_read(&rdev->exclusive_lock);
6c6f4783 576 r = radeon_cs_handle_lockup(rdev, r);
771fe6b9
JG
577 return r;
578}
513bcb46
DA
579
580int radeon_cs_finish_pages(struct radeon_cs_parser *p)
581{
582 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
583 int i;
584 int size = PAGE_SIZE;
585
586 for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
587 if (i == ibc->last_page_index) {
588 size = (ibc->length_dw * 4) % PAGE_SIZE;
589 if (size == 0)
590 size = PAGE_SIZE;
591 }
592
f2e39221 593 if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
513bcb46
DA
594 ibc->user_ptr + (i * PAGE_SIZE),
595 size))
596 return -EFAULT;
597 }
598 return 0;
599}
600
c4c7f314 601static int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
513bcb46
DA
602{
603 int new_page;
513bcb46
DA
604 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
605 int i;
606 int size = PAGE_SIZE;
ff4bd082
IH
607 bool copy1 = (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) ?
608 false : true;
513bcb46 609
c5e617e2 610 for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
f2e39221 611 if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
513bcb46
DA
612 ibc->user_ptr + (i * PAGE_SIZE),
613 PAGE_SIZE)) {
614 p->parser_error = -EFAULT;
615 return 0;
616 }
617 }
618
513bcb46
DA
619 if (pg_idx == ibc->last_page_index) {
620 size = (ibc->length_dw * 4) % PAGE_SIZE;
6a7068b4
DA
621 if (size == 0)
622 size = PAGE_SIZE;
513bcb46
DA
623 }
624
6a7068b4
DA
625 new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
626 if (copy1)
f2e39221 627 ibc->kpage[new_page] = p->ib.ptr + (pg_idx * (PAGE_SIZE / 4));
6a7068b4 628
513bcb46
DA
629 if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
630 ibc->user_ptr + (pg_idx * PAGE_SIZE),
631 size)) {
632 p->parser_error = -EFAULT;
633 return 0;
634 }
635
6a7068b4
DA
636 /* copy to IB for non single case */
637 if (!copy1)
f2e39221 638 memcpy((void *)(p->ib.ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
513bcb46
DA
639
640 ibc->last_copied_page = pg_idx;
641 ibc->kpage_idx[new_page] = pg_idx;
642
643 return new_page;
644}
c4c7f314
DA
645
646u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
647{
648 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
649 u32 pg_idx, pg_offset;
650 u32 idx_value = 0;
651 int new_page;
652
653 pg_idx = (idx * 4) / PAGE_SIZE;
654 pg_offset = (idx * 4) % PAGE_SIZE;
655
656 if (ibc->kpage_idx[0] == pg_idx)
657 return ibc->kpage[0][pg_offset/4];
658 if (ibc->kpage_idx[1] == pg_idx)
659 return ibc->kpage[1][pg_offset/4];
660
661 new_page = radeon_cs_update_pages(p, pg_idx);
662 if (new_page < 0) {
663 p->parser_error = new_page;
664 return 0;
665 }
666
667 idx_value = ibc->kpage[new_page][pg_offset/4];
668 return idx_value;
669}
4db01311
IH
670
671/**
672 * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet
673 * @parser: parser structure holding parsing context.
674 * @pkt: where to store packet information
675 *
676 * Assume that chunk_ib_index is properly set. Will return -EINVAL
677 * if packet is bigger than remaining ib size. or if packets is unknown.
678 **/
679int radeon_cs_packet_parse(struct radeon_cs_parser *p,
680 struct radeon_cs_packet *pkt,
681 unsigned idx)
682{
683 struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
684 struct radeon_device *rdev = p->rdev;
685 uint32_t header;
686
687 if (idx >= ib_chunk->length_dw) {
688 DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
689 idx, ib_chunk->length_dw);
690 return -EINVAL;
691 }
692 header = radeon_get_ib_value(p, idx);
693 pkt->idx = idx;
694 pkt->type = RADEON_CP_PACKET_GET_TYPE(header);
695 pkt->count = RADEON_CP_PACKET_GET_COUNT(header);
696 pkt->one_reg_wr = 0;
697 switch (pkt->type) {
698 case RADEON_PACKET_TYPE0:
699 if (rdev->family < CHIP_R600) {
700 pkt->reg = R100_CP_PACKET0_GET_REG(header);
701 pkt->one_reg_wr =
702 RADEON_CP_PACKET0_GET_ONE_REG_WR(header);
703 } else
704 pkt->reg = R600_CP_PACKET0_GET_REG(header);
705 break;
706 case RADEON_PACKET_TYPE3:
707 pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header);
708 break;
709 case RADEON_PACKET_TYPE2:
710 pkt->count = -1;
711 break;
712 default:
713 DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
714 return -EINVAL;
715 }
716 if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
717 DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
718 pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
719 return -EINVAL;
720 }
721 return 0;
722}
9ffb7a6d
IH
723
724/**
725 * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP
726 * @p: structure holding the parser context.
727 *
728 * Check if the next packet is NOP relocation packet3.
729 **/
730bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
731{
732 struct radeon_cs_packet p3reloc;
733 int r;
734
735 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
736 if (r)
737 return false;
738 if (p3reloc.type != RADEON_PACKET_TYPE3)
739 return false;
740 if (p3reloc.opcode != RADEON_PACKET3_NOP)
741 return false;
742 return true;
743}
c3ad63af
IH
744
745/**
746 * radeon_cs_dump_packet() - dump raw packet context
747 * @p: structure holding the parser context.
748 * @pkt: structure holding the packet.
749 *
750 * Used mostly for debugging and error reporting.
751 **/
752void radeon_cs_dump_packet(struct radeon_cs_parser *p,
753 struct radeon_cs_packet *pkt)
754{
755 volatile uint32_t *ib;
756 unsigned i;
757 unsigned idx;
758
759 ib = p->ib.ptr;
760 idx = pkt->idx;
761 for (i = 0; i <= (pkt->count + 1); i++, idx++)
762 DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
763}
764
e9716993
IH
765/**
766 * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet
767 * @parser: parser structure holding parsing context.
768 * @data: pointer to relocation data
769 * @offset_start: starting offset
770 * @offset_mask: offset mask (to align start offset on)
771 * @reloc: reloc informations
772 *
773 * Check if next packet is relocation packet3, do bo validation and compute
774 * GPU offset using the provided start.
775 **/
776int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
777 struct radeon_cs_reloc **cs_reloc,
778 int nomm)
779{
780 struct radeon_cs_chunk *relocs_chunk;
781 struct radeon_cs_packet p3reloc;
782 unsigned idx;
783 int r;
784
785 if (p->chunk_relocs_idx == -1) {
786 DRM_ERROR("No relocation chunk !\n");
787 return -EINVAL;
788 }
789 *cs_reloc = NULL;
790 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
791 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
792 if (r)
793 return r;
794 p->idx += p3reloc.count + 2;
795 if (p3reloc.type != RADEON_PACKET_TYPE3 ||
796 p3reloc.opcode != RADEON_PACKET3_NOP) {
797 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
798 p3reloc.idx);
799 radeon_cs_dump_packet(p, &p3reloc);
800 return -EINVAL;
801 }
802 idx = radeon_get_ib_value(p, p3reloc.idx + 1);
803 if (idx >= relocs_chunk->length_dw) {
804 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
805 idx, relocs_chunk->length_dw);
806 radeon_cs_dump_packet(p, &p3reloc);
807 return -EINVAL;
808 }
809 /* FIXME: we assume reloc size is 4 dwords */
810 if (nomm) {
811 *cs_reloc = p->relocs;
812 (*cs_reloc)->lobj.gpu_offset =
813 (u64)relocs_chunk->kdata[idx + 3] << 32;
814 (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
815 } else
816 *cs_reloc = p->relocs_ptr[(idx / 4)];
817 return 0;
818}
This page took 0.28027 seconds and 5 git commands to generate.