blob: 60af3cda587b882429b2e368c78748ac75a452e8 [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 }
Christian König4474f3a2013-04-08 12:41:28 +020066 if (duplicate) {
Christian König16557f12011-10-24 14:59:17 +020067 p->relocs[i].handle = 0;
Christian König4474f3a2013-04-08 12:41:28 +020068 continue;
69 }
70
71 p->relocs[i].gobj = drm_gem_object_lookup(ddev, p->filp,
72 r->handle);
73 if (p->relocs[i].gobj == NULL) {
74 DRM_ERROR("gem object lookup failed 0x%x\n",
75 r->handle);
76 return -ENOENT;
77 }
78 p->relocs_ptr[i] = &p->relocs[i];
79 p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
80 p->relocs[i].lobj.bo = p->relocs[i].robj;
81 p->relocs[i].lobj.written = !!r->write_domain;
82
Christian König075bf1f2013-09-15 13:31:28 +020083 /* the first reloc of an UVD job is the msg and that must be in
84 VRAM, also but everything into VRAM on AGP cards to avoid
85 image corruptions */
86 if (p->ring == R600_RING_TYPE_UVD_INDEX &&
Alex Deucher64522302013-09-15 23:23:07 -040087 (i == 0 || drm_pci_device_is_agp(p->rdev->ddev))) {
Christian Königf2ba57b2013-04-08 12:41:29 +020088 /* TODO: is this still needed for NI+ ? */
89 p->relocs[i].lobj.domain =
90 RADEON_GEM_DOMAIN_VRAM;
91
92 p->relocs[i].lobj.alt_domain =
93 RADEON_GEM_DOMAIN_VRAM;
94
95 } else {
96 uint32_t domain = r->write_domain ?
97 r->write_domain : r->read_domains;
98
Marek Olšák9ca04cf2014-05-27 02:56:36 +020099 if (domain & RADEON_GEM_DOMAIN_CPU) {
100 DRM_ERROR("RADEON_GEM_DOMAIN_CPU is not valid "
101 "for command submission\n");
102 return -EINVAL;
103 }
104
Christian Königf2ba57b2013-04-08 12:41:29 +0200105 p->relocs[i].lobj.domain = domain;
106 if (domain == RADEON_GEM_DOMAIN_VRAM)
107 domain |= RADEON_GEM_DOMAIN_GTT;
108 p->relocs[i].lobj.alt_domain = domain;
109 }
Christian König4474f3a2013-04-08 12:41:28 +0200110
111 p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
112 p->relocs[i].handle = r->handle;
113
114 radeon_bo_list_add_object(&p->relocs[i].lobj,
115 &p->validated);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200116 }
Christian Königf2ba57b2013-04-08 12:41:29 +0200117 return radeon_bo_list_validate(&p->validated, p->ring);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200118}
119
Jerome Glisse721604a2012-01-05 22:11:05 -0500120static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
121{
122 p->priority = priority;
123
124 switch (ring) {
125 default:
126 DRM_ERROR("unknown ring id: %d\n", ring);
127 return -EINVAL;
128 case RADEON_CS_RING_GFX:
129 p->ring = RADEON_RING_TYPE_GFX_INDEX;
130 break;
131 case RADEON_CS_RING_COMPUTE:
Alex Deucher8d5ef7b2012-03-20 17:18:24 -0400132 if (p->rdev->family >= CHIP_TAHITI) {
133 if (p->priority > 0)
134 p->ring = CAYMAN_RING_TYPE_CP1_INDEX;
135 else
136 p->ring = CAYMAN_RING_TYPE_CP2_INDEX;
137 } else
138 p->ring = RADEON_RING_TYPE_GFX_INDEX;
Jerome Glisse721604a2012-01-05 22:11:05 -0500139 break;
Alex Deucher278a3342012-12-13 12:27:28 -0500140 case RADEON_CS_RING_DMA:
141 if (p->rdev->family >= CHIP_CAYMAN) {
142 if (p->priority > 0)
143 p->ring = R600_RING_TYPE_DMA_INDEX;
144 else
145 p->ring = CAYMAN_RING_TYPE_DMA1_INDEX;
146 } else if (p->rdev->family >= CHIP_R600) {
147 p->ring = R600_RING_TYPE_DMA_INDEX;
148 } else {
149 return -EINVAL;
150 }
151 break;
Christian Königf2ba57b2013-04-08 12:41:29 +0200152 case RADEON_CS_RING_UVD:
153 p->ring = R600_RING_TYPE_UVD_INDEX;
154 break;
Jerome Glisse721604a2012-01-05 22:11:05 -0500155 }
156 return 0;
157}
158
Christian König220907d2012-05-10 16:46:43 +0200159static void radeon_cs_sync_rings(struct radeon_cs_parser *p)
Christian König93504fc2012-01-05 22:11:06 -0500160{
Christian König220907d2012-05-10 16:46:43 +0200161 int i;
Christian König93504fc2012-01-05 22:11:06 -0500162
Christian Königcdac5502012-02-23 15:18:42 +0100163 for (i = 0; i < p->nrelocs; i++) {
Christian Königf82cbdd2012-08-09 16:35:36 +0200164 if (!p->relocs[i].robj)
Christian Königcdac5502012-02-23 15:18:42 +0100165 continue;
166
Alex Deucher43f12142013-02-01 17:32:42 +0100167 radeon_ib_sync_to(&p->ib, p->relocs[i].robj->tbo.sync_obj);
Christian Königcdac5502012-02-23 15:18:42 +0100168 }
Christian König93504fc2012-01-05 22:11:06 -0500169}
170
Alex Deucher9b001472012-05-30 10:09:30 -0400171/* XXX: note that this is called from the legacy UMS CS ioctl as well */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200172int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
173{
174 struct drm_radeon_cs *cs = data;
175 uint64_t *chunk_array_ptr;
Jerome Glisse721604a2012-01-05 22:11:05 -0500176 unsigned size, i;
177 u32 ring = RADEON_CS_RING_GFX;
178 s32 priority = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200179
180 if (!cs->num_chunks) {
181 return 0;
182 }
183 /* get chunks */
184 INIT_LIST_HEAD(&p->validated);
185 p->idx = 0;
Jerome Glissef2e39222012-05-09 15:35:02 +0200186 p->ib.sa_bo = NULL;
187 p->ib.semaphore = NULL;
188 p->const_ib.sa_bo = NULL;
189 p->const_ib.semaphore = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200190 p->chunk_ib_idx = -1;
191 p->chunk_relocs_idx = -1;
Jerome Glisse721604a2012-01-05 22:11:05 -0500192 p->chunk_flags_idx = -1;
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400193 p->chunk_const_ib_idx = -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200194 p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
195 if (p->chunks_array == NULL) {
196 return -ENOMEM;
197 }
198 chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
199 if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
200 sizeof(uint64_t)*cs->num_chunks)) {
201 return -EFAULT;
202 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500203 p->cs_flags = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200204 p->nchunks = cs->num_chunks;
205 p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
206 if (p->chunks == NULL) {
207 return -ENOMEM;
208 }
209 for (i = 0; i < p->nchunks; i++) {
210 struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
211 struct drm_radeon_cs_chunk user_chunk;
212 uint32_t __user *cdata;
213
214 chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
215 if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
216 sizeof(struct drm_radeon_cs_chunk))) {
217 return -EFAULT;
218 }
Dave Airlie5176fdc2009-06-30 11:47:14 +1000219 p->chunks[i].length_dw = user_chunk.length_dw;
220 p->chunks[i].kdata = NULL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200221 p->chunks[i].chunk_id = user_chunk.chunk_id;
Ilija Hadzic580f8392013-01-02 18:27:37 -0500222 p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200223 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
224 p->chunk_relocs_idx = i;
225 }
226 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
227 p->chunk_ib_idx = i;
Dave Airlie5176fdc2009-06-30 11:47:14 +1000228 /* zero length IB isn't useful */
229 if (p->chunks[i].length_dw == 0)
230 return -EINVAL;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200231 }
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400232 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_CONST_IB) {
233 p->chunk_const_ib_idx = i;
234 /* zero length CONST IB isn't useful */
235 if (p->chunks[i].length_dw == 0)
236 return -EINVAL;
237 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500238 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
239 p->chunk_flags_idx = i;
240 /* zero length flags aren't useful */
241 if (p->chunks[i].length_dw == 0)
242 return -EINVAL;
Marek Olšáke70f2242011-10-25 01:38:45 +0200243 }
Dave Airlie5176fdc2009-06-30 11:47:14 +1000244
Dave Airlie513bcb42009-09-23 16:56:27 +1000245 cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
Jerome Glisse721604a2012-01-05 22:11:05 -0500246 if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) ||
247 (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) {
Dave Airlie513bcb42009-09-23 16:56:27 +1000248 size = p->chunks[i].length_dw * sizeof(uint32_t);
249 p->chunks[i].kdata = kmalloc(size, GFP_KERNEL);
250 if (p->chunks[i].kdata == NULL) {
251 return -ENOMEM;
252 }
253 if (DRM_COPY_FROM_USER(p->chunks[i].kdata,
254 p->chunks[i].user_ptr, size)) {
255 return -EFAULT;
256 }
Marek Olšáke70f2242011-10-25 01:38:45 +0200257 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500258 p->cs_flags = p->chunks[i].kdata[0];
259 if (p->chunks[i].length_dw > 1)
260 ring = p->chunks[i].kdata[1];
261 if (p->chunks[i].length_dw > 2)
262 priority = (s32)p->chunks[i].kdata[2];
Marek Olšáke70f2242011-10-25 01:38:45 +0200263 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200264 }
265 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500266
Alex Deucher9b001472012-05-30 10:09:30 -0400267 /* these are KMS only */
268 if (p->rdev) {
269 if ((p->cs_flags & RADEON_CS_USE_VM) &&
270 !p->rdev->vm_manager.enabled) {
271 DRM_ERROR("VM not active on asic!\n");
272 return -EINVAL;
273 }
274
Alex Deucher9b001472012-05-30 10:09:30 -0400275 if (radeon_cs_get_ring(p, ring, priority))
276 return -EINVAL;
Christian König57449042013-04-08 12:41:27 +0200277
278 /* we only support VM on some SI+ rings */
279 if ((p->rdev->asic->ring[p->ring].cs_parse == NULL) &&
280 ((p->cs_flags & RADEON_CS_USE_VM) == 0)) {
281 DRM_ERROR("Ring %d requires VM!\n", p->ring);
282 return -EINVAL;
283 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200284 }
Marek Olšáke70f2242011-10-25 01:38:45 +0200285
Jerome Glisse721604a2012-01-05 22:11:05 -0500286 /* deal with non-vm */
287 if ((p->chunk_ib_idx != -1) &&
288 ((p->cs_flags & RADEON_CS_USE_VM) == 0) &&
289 (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) {
290 if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
291 DRM_ERROR("cs IB too big: %d\n",
292 p->chunks[p->chunk_ib_idx].length_dw);
293 return -EINVAL;
294 }
Ilija Hadzicff4bd082013-01-07 18:21:57 -0500295 if (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) {
Dave Airlie6a7068b2012-04-03 16:23:41 +0100296 p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
297 p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
298 if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL ||
299 p->chunks[p->chunk_ib_idx].kpage[1] == NULL) {
Ilija Hadzic25d89992013-01-07 18:21:59 -0500300 kfree(p->chunks[p->chunk_ib_idx].kpage[0]);
301 kfree(p->chunks[p->chunk_ib_idx].kpage[1]);
Ilija Hadzic1da80cf2013-01-23 13:59:05 -0500302 p->chunks[p->chunk_ib_idx].kpage[0] = NULL;
303 p->chunks[p->chunk_ib_idx].kpage[1] = NULL;
Dave Airlie6a7068b2012-04-03 16:23:41 +0100304 return -ENOMEM;
305 }
306 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500307 p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1;
308 p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1;
309 p->chunks[p->chunk_ib_idx].last_copied_page = -1;
310 p->chunks[p->chunk_ib_idx].last_page_index =
311 ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE;
312 }
313
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200314 return 0;
315}
316
317/**
318 * cs_parser_fini() - clean parser states
319 * @parser: parser structure holding parsing context.
320 * @error: error number
321 *
322 * If error is set than unvalidate buffer, otherwise just free memory
323 * used by parsing context.
324 **/
325static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error)
326{
327 unsigned i;
328
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400329 if (!error) {
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000330 ttm_eu_fence_buffer_objects(&parser->validated,
Jerome Glissef2e39222012-05-09 15:35:02 +0200331 parser->ib.fence);
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400332 } else {
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000333 ttm_eu_backoff_reservation(&parser->validated);
Jerome Glissee43b5ec2012-08-06 12:32:21 -0400334 }
Thomas Hellstrom147666f2010-11-17 12:38:32 +0000335
Pauli Nieminenfcbc4512010-03-19 07:44:33 +0000336 if (parser->relocs != NULL) {
337 for (i = 0; i < parser->nrelocs; i++) {
338 if (parser->relocs[i].gobj)
339 drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
340 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200341 }
Michel Dänzer48e113e2009-09-15 17:09:32 +0200342 kfree(parser->track);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200343 kfree(parser->relocs);
344 kfree(parser->relocs_ptr);
345 for (i = 0; i < parser->nchunks; i++) {
346 kfree(parser->chunks[i].kdata);
Dave Airlie6a7068b2012-04-03 16:23:41 +0100347 if ((parser->rdev->flags & RADEON_IS_AGP)) {
348 kfree(parser->chunks[i].kpage[0]);
349 kfree(parser->chunks[i].kpage[1]);
350 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200351 }
352 kfree(parser->chunks);
353 kfree(parser->chunks_array);
354 radeon_ib_free(parser->rdev, &parser->ib);
Jerome Glissef2e39222012-05-09 15:35:02 +0200355 radeon_ib_free(parser->rdev, &parser->const_ib);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200356}
357
Jerome Glisse721604a2012-01-05 22:11:05 -0500358static int radeon_cs_ib_chunk(struct radeon_device *rdev,
359 struct radeon_cs_parser *parser)
360{
361 struct radeon_cs_chunk *ib_chunk;
362 int r;
363
364 if (parser->chunk_ib_idx == -1)
365 return 0;
366
367 if (parser->cs_flags & RADEON_CS_USE_VM)
368 return 0;
369
370 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
371 /* Copy the packet into the IB, the parser will read from the
372 * input memory (cached) and write to the IB (which can be
373 * uncached).
374 */
375 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
Christian König4bf3dd92012-08-06 18:57:44 +0200376 NULL, ib_chunk->length_dw * 4);
Jerome Glisse721604a2012-01-05 22:11:05 -0500377 if (r) {
378 DRM_ERROR("Failed to get ib !\n");
379 return r;
380 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200381 parser->ib.length_dw = ib_chunk->length_dw;
Christian Königeb0c19c2012-02-23 15:18:44 +0100382 r = radeon_cs_parse(rdev, parser->ring, parser);
Jerome Glisse721604a2012-01-05 22:11:05 -0500383 if (r || parser->parser_error) {
384 DRM_ERROR("Invalid command stream !\n");
385 return r;
386 }
387 r = radeon_cs_finish_pages(parser);
388 if (r) {
389 DRM_ERROR("Invalid command stream !\n");
390 return r;
391 }
Christian König220907d2012-05-10 16:46:43 +0200392 radeon_cs_sync_rings(parser);
Christian König4ef72562012-07-13 13:06:00 +0200393 r = radeon_ib_schedule(rdev, &parser->ib, NULL);
Jerome Glisse721604a2012-01-05 22:11:05 -0500394 if (r) {
395 DRM_ERROR("Failed to schedule IB !\n");
396 }
Christian König93bf8882012-07-03 14:05:41 +0200397 return r;
Jerome Glisse721604a2012-01-05 22:11:05 -0500398}
399
400static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
401 struct radeon_vm *vm)
402{
Jerome Glisse3e8970f2012-08-13 12:07:33 -0400403 struct radeon_device *rdev = parser->rdev;
Jerome Glisse721604a2012-01-05 22:11:05 -0500404 struct radeon_bo_list *lobj;
405 struct radeon_bo *bo;
406 int r;
407
Jerome Glisse3e8970f2012-08-13 12:07:33 -0400408 r = radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo, &rdev->ring_tmp_bo.bo->tbo.mem);
409 if (r) {
410 return r;
411 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500412 list_for_each_entry(lobj, &parser->validated, tv.head) {
413 bo = lobj->bo;
414 r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
415 if (r) {
416 return r;
417 }
418 }
419 return 0;
420}
421
422static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
423 struct radeon_cs_parser *parser)
424{
425 struct radeon_cs_chunk *ib_chunk;
426 struct radeon_fpriv *fpriv = parser->filp->driver_priv;
427 struct radeon_vm *vm = &fpriv->vm;
428 int r;
429
430 if (parser->chunk_ib_idx == -1)
431 return 0;
Jerome Glisse721604a2012-01-05 22:11:05 -0500432 if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
433 return 0;
434
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400435 if ((rdev->family >= CHIP_TAHITI) &&
436 (parser->chunk_const_ib_idx != -1)) {
437 ib_chunk = &parser->chunks[parser->chunk_const_ib_idx];
438 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
439 DRM_ERROR("cs IB CONST too big: %d\n", ib_chunk->length_dw);
440 return -EINVAL;
441 }
442 r = radeon_ib_get(rdev, parser->ring, &parser->const_ib,
Christian König4bf3dd92012-08-06 18:57:44 +0200443 vm, ib_chunk->length_dw * 4);
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400444 if (r) {
445 DRM_ERROR("Failed to get const ib !\n");
446 return r;
447 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200448 parser->const_ib.is_const_ib = true;
449 parser->const_ib.length_dw = ib_chunk->length_dw;
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400450 /* Copy the packet into the IB */
Jerome Glissef2e39222012-05-09 15:35:02 +0200451 if (DRM_COPY_FROM_USER(parser->const_ib.ptr, ib_chunk->user_ptr,
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400452 ib_chunk->length_dw * 4)) {
453 return -EFAULT;
454 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200455 r = radeon_ring_ib_parse(rdev, parser->ring, &parser->const_ib);
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400456 if (r) {
457 return r;
458 }
459 }
460
Jerome Glisse721604a2012-01-05 22:11:05 -0500461 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
462 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
463 DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
464 return -EINVAL;
465 }
466 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
Christian König4bf3dd92012-08-06 18:57:44 +0200467 vm, ib_chunk->length_dw * 4);
Jerome Glisse721604a2012-01-05 22:11:05 -0500468 if (r) {
469 DRM_ERROR("Failed to get ib !\n");
470 return r;
471 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200472 parser->ib.length_dw = ib_chunk->length_dw;
Jerome Glisse721604a2012-01-05 22:11:05 -0500473 /* Copy the packet into the IB */
Jerome Glissef2e39222012-05-09 15:35:02 +0200474 if (DRM_COPY_FROM_USER(parser->ib.ptr, ib_chunk->user_ptr,
Jerome Glisse721604a2012-01-05 22:11:05 -0500475 ib_chunk->length_dw * 4)) {
476 return -EFAULT;
477 }
Jerome Glissef2e39222012-05-09 15:35:02 +0200478 r = radeon_ring_ib_parse(rdev, parser->ring, &parser->ib);
Jerome Glisse721604a2012-01-05 22:11:05 -0500479 if (r) {
480 return r;
481 }
482
Christian König36ff39c2012-05-09 10:07:08 +0200483 mutex_lock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500484 mutex_lock(&vm->mutex);
Christian Königddf03f52012-08-09 20:02:28 +0200485 r = radeon_vm_alloc_pt(rdev, vm);
Jerome Glisse721604a2012-01-05 22:11:05 -0500486 if (r) {
487 goto out;
488 }
489 r = radeon_bo_vm_update_pte(parser, vm);
490 if (r) {
491 goto out;
492 }
Christian König220907d2012-05-10 16:46:43 +0200493 radeon_cs_sync_rings(parser);
Alex Deucher43f12142013-02-01 17:32:42 +0100494 radeon_ib_sync_to(&parser->ib, vm->fence);
495 radeon_ib_sync_to(&parser->ib, radeon_vm_grab_id(
496 rdev, vm, parser->ring));
Christian König4ef72562012-07-13 13:06:00 +0200497
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400498 if ((rdev->family >= CHIP_TAHITI) &&
499 (parser->chunk_const_ib_idx != -1)) {
Christian König4ef72562012-07-13 13:06:00 +0200500 r = radeon_ib_schedule(rdev, &parser->ib, &parser->const_ib);
501 } else {
502 r = radeon_ib_schedule(rdev, &parser->ib, NULL);
Alex Deucherdfcf5f32012-03-20 17:18:14 -0400503 }
504
Jerome Glisse721604a2012-01-05 22:11:05 -0500505 if (!r) {
Christian Königee60e292012-08-09 16:21:08 +0200506 radeon_vm_fence(rdev, vm, parser->ib.fence);
Jerome Glisse721604a2012-01-05 22:11:05 -0500507 }
Christian Königee60e292012-08-09 16:21:08 +0200508
509out:
Christian König13e55c32012-10-09 13:31:19 +0200510 radeon_vm_add_to_lru(rdev, vm);
Christian König36ff39c2012-05-09 10:07:08 +0200511 mutex_unlock(&vm->mutex);
512 mutex_unlock(&rdev->vm_manager.lock);
Jerome Glisse721604a2012-01-05 22:11:05 -0500513 return r;
514}
515
Christian König6c6f4782012-05-02 15:11:19 +0200516static int radeon_cs_handle_lockup(struct radeon_device *rdev, int r)
517{
518 if (r == -EDEADLK) {
519 r = radeon_gpu_reset(rdev);
520 if (!r)
521 r = -EAGAIN;
522 }
523 return r;
524}
525
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200526int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
527{
528 struct radeon_device *rdev = dev->dev_private;
529 struct radeon_cs_parser parser;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200530 int r;
531
Jerome Glissedee53e72012-07-02 12:45:19 -0400532 down_read(&rdev->exclusive_lock);
Jerome Glisse6b7746e2012-02-20 17:57:20 -0500533 if (!rdev->accel_working) {
Jerome Glissedee53e72012-07-02 12:45:19 -0400534 up_read(&rdev->exclusive_lock);
Jerome Glisse6b7746e2012-02-20 17:57:20 -0500535 return -EBUSY;
536 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200537 /* initialize parser */
538 memset(&parser, 0, sizeof(struct radeon_cs_parser));
539 parser.filp = filp;
540 parser.rdev = rdev;
Jerome Glissec8c15ff2010-01-18 13:01:36 +0100541 parser.dev = rdev->dev;
Dave Airlie428c6e32011-06-08 19:58:29 +1000542 parser.family = rdev->family;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200543 r = radeon_cs_parser_init(&parser, data);
544 if (r) {
545 DRM_ERROR("Failed to initialize parser !\n");
546 radeon_cs_parser_fini(&parser, r);
Jerome Glissedee53e72012-07-02 12:45:19 -0400547 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200548 r = radeon_cs_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200549 return r;
550 }
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200551 r = radeon_cs_parser_relocs(&parser);
552 if (r) {
Dave Airlie97f23b32010-03-19 10:33:44 +1000553 if (r != -ERESTARTSYS)
554 DRM_ERROR("Failed to parse relocation %d!\n", r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200555 radeon_cs_parser_fini(&parser, r);
Jerome Glissedee53e72012-07-02 12:45:19 -0400556 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200557 r = radeon_cs_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200558 return r;
559 }
Christian König55b51c82013-04-18 15:25:59 +0200560
561 if (parser.ring == R600_RING_TYPE_UVD_INDEX)
562 radeon_uvd_note_usage(rdev);
563
Jerome Glisse721604a2012-01-05 22:11:05 -0500564 r = radeon_cs_ib_chunk(rdev, &parser);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200565 if (r) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500566 goto out;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200567 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500568 r = radeon_cs_ib_vm_chunk(rdev, &parser);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200569 if (r) {
Jerome Glisse721604a2012-01-05 22:11:05 -0500570 goto out;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200571 }
Jerome Glisse721604a2012-01-05 22:11:05 -0500572out:
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200573 radeon_cs_parser_fini(&parser, r);
Jerome Glissedee53e72012-07-02 12:45:19 -0400574 up_read(&rdev->exclusive_lock);
Christian König6c6f4782012-05-02 15:11:19 +0200575 r = radeon_cs_handle_lockup(rdev, r);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200576 return r;
577}
Dave Airlie513bcb42009-09-23 16:56:27 +1000578
579int radeon_cs_finish_pages(struct radeon_cs_parser *p)
580{
581 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
582 int i;
583 int size = PAGE_SIZE;
584
585 for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
586 if (i == ibc->last_page_index) {
587 size = (ibc->length_dw * 4) % PAGE_SIZE;
588 if (size == 0)
589 size = PAGE_SIZE;
590 }
591
Jerome Glissef2e39222012-05-09 15:35:02 +0200592 if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
Dave Airlie513bcb42009-09-23 16:56:27 +1000593 ibc->user_ptr + (i * PAGE_SIZE),
594 size))
595 return -EFAULT;
596 }
597 return 0;
598}
599
Dave Airliec4c7f312012-05-26 17:34:24 +0100600static int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
Dave Airlie513bcb42009-09-23 16:56:27 +1000601{
602 int new_page;
Dave Airlie513bcb42009-09-23 16:56:27 +1000603 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
604 int i;
605 int size = PAGE_SIZE;
Ilija Hadzicff4bd082013-01-07 18:21:57 -0500606 bool copy1 = (p->rdev && (p->rdev->flags & RADEON_IS_AGP)) ?
607 false : true;
Dave Airlie513bcb42009-09-23 16:56:27 +1000608
Dave Airliec5e617e2009-09-26 09:03:39 +1000609 for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
Jerome Glissef2e39222012-05-09 15:35:02 +0200610 if (DRM_COPY_FROM_USER(p->ib.ptr + (i * (PAGE_SIZE/4)),
Dave Airlie513bcb42009-09-23 16:56:27 +1000611 ibc->user_ptr + (i * PAGE_SIZE),
612 PAGE_SIZE)) {
613 p->parser_error = -EFAULT;
614 return 0;
615 }
616 }
617
Dave Airlie513bcb42009-09-23 16:56:27 +1000618 if (pg_idx == ibc->last_page_index) {
619 size = (ibc->length_dw * 4) % PAGE_SIZE;
Dave Airlie6a7068b2012-04-03 16:23:41 +0100620 if (size == 0)
621 size = PAGE_SIZE;
Dave Airlie513bcb42009-09-23 16:56:27 +1000622 }
623
Dave Airlie6a7068b2012-04-03 16:23:41 +0100624 new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
625 if (copy1)
Jerome Glissef2e39222012-05-09 15:35:02 +0200626 ibc->kpage[new_page] = p->ib.ptr + (pg_idx * (PAGE_SIZE / 4));
Dave Airlie6a7068b2012-04-03 16:23:41 +0100627
Dave Airlie513bcb42009-09-23 16:56:27 +1000628 if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
629 ibc->user_ptr + (pg_idx * PAGE_SIZE),
630 size)) {
631 p->parser_error = -EFAULT;
632 return 0;
633 }
634
Dave Airlie6a7068b2012-04-03 16:23:41 +0100635 /* copy to IB for non single case */
636 if (!copy1)
Jerome Glissef2e39222012-05-09 15:35:02 +0200637 memcpy((void *)(p->ib.ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
Dave Airlie513bcb42009-09-23 16:56:27 +1000638
639 ibc->last_copied_page = pg_idx;
640 ibc->kpage_idx[new_page] = pg_idx;
641
642 return new_page;
643}
Dave Airliec4c7f312012-05-26 17:34:24 +0100644
645u32 radeon_get_ib_value(struct radeon_cs_parser *p, int idx)
646{
647 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
648 u32 pg_idx, pg_offset;
649 u32 idx_value = 0;
650 int new_page;
651
652 pg_idx = (idx * 4) / PAGE_SIZE;
653 pg_offset = (idx * 4) % PAGE_SIZE;
654
655 if (ibc->kpage_idx[0] == pg_idx)
656 return ibc->kpage[0][pg_offset/4];
657 if (ibc->kpage_idx[1] == pg_idx)
658 return ibc->kpage[1][pg_offset/4];
659
660 new_page = radeon_cs_update_pages(p, pg_idx);
661 if (new_page < 0) {
662 p->parser_error = new_page;
663 return 0;
664 }
665
666 idx_value = ibc->kpage[new_page][pg_offset/4];
667 return idx_value;
668}
Ilija Hadzic4db01312013-01-02 18:27:40 -0500669
670/**
671 * radeon_cs_packet_parse() - parse cp packet and point ib index to next packet
672 * @parser: parser structure holding parsing context.
673 * @pkt: where to store packet information
674 *
675 * Assume that chunk_ib_index is properly set. Will return -EINVAL
676 * if packet is bigger than remaining ib size. or if packets is unknown.
677 **/
678int radeon_cs_packet_parse(struct radeon_cs_parser *p,
679 struct radeon_cs_packet *pkt,
680 unsigned idx)
681{
682 struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
683 struct radeon_device *rdev = p->rdev;
684 uint32_t header;
685
686 if (idx >= ib_chunk->length_dw) {
687 DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
688 idx, ib_chunk->length_dw);
689 return -EINVAL;
690 }
691 header = radeon_get_ib_value(p, idx);
692 pkt->idx = idx;
693 pkt->type = RADEON_CP_PACKET_GET_TYPE(header);
694 pkt->count = RADEON_CP_PACKET_GET_COUNT(header);
695 pkt->one_reg_wr = 0;
696 switch (pkt->type) {
697 case RADEON_PACKET_TYPE0:
698 if (rdev->family < CHIP_R600) {
699 pkt->reg = R100_CP_PACKET0_GET_REG(header);
700 pkt->one_reg_wr =
701 RADEON_CP_PACKET0_GET_ONE_REG_WR(header);
702 } else
703 pkt->reg = R600_CP_PACKET0_GET_REG(header);
704 break;
705 case RADEON_PACKET_TYPE3:
706 pkt->opcode = RADEON_CP_PACKET3_GET_OPCODE(header);
707 break;
708 case RADEON_PACKET_TYPE2:
709 pkt->count = -1;
710 break;
711 default:
712 DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
713 return -EINVAL;
714 }
715 if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
716 DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
717 pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
718 return -EINVAL;
719 }
720 return 0;
721}
Ilija Hadzic9ffb7a62013-01-02 18:27:42 -0500722
723/**
724 * radeon_cs_packet_next_is_pkt3_nop() - test if the next packet is P3 NOP
725 * @p: structure holding the parser context.
726 *
727 * Check if the next packet is NOP relocation packet3.
728 **/
729bool radeon_cs_packet_next_is_pkt3_nop(struct radeon_cs_parser *p)
730{
731 struct radeon_cs_packet p3reloc;
732 int r;
733
734 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
735 if (r)
736 return false;
737 if (p3reloc.type != RADEON_PACKET_TYPE3)
738 return false;
739 if (p3reloc.opcode != RADEON_PACKET3_NOP)
740 return false;
741 return true;
742}
Ilija Hadzicc3ad63a2013-01-02 18:27:45 -0500743
744/**
745 * radeon_cs_dump_packet() - dump raw packet context
746 * @p: structure holding the parser context.
747 * @pkt: structure holding the packet.
748 *
749 * Used mostly for debugging and error reporting.
750 **/
751void radeon_cs_dump_packet(struct radeon_cs_parser *p,
752 struct radeon_cs_packet *pkt)
753{
754 volatile uint32_t *ib;
755 unsigned i;
756 unsigned idx;
757
758 ib = p->ib.ptr;
759 idx = pkt->idx;
760 for (i = 0; i <= (pkt->count + 1); i++, idx++)
761 DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
762}
763
Ilija Hadzice9716992013-01-02 18:27:46 -0500764/**
765 * radeon_cs_packet_next_reloc() - parse next (should be reloc) packet
766 * @parser: parser structure holding parsing context.
767 * @data: pointer to relocation data
768 * @offset_start: starting offset
769 * @offset_mask: offset mask (to align start offset on)
770 * @reloc: reloc informations
771 *
772 * Check if next packet is relocation packet3, do bo validation and compute
773 * GPU offset using the provided start.
774 **/
775int radeon_cs_packet_next_reloc(struct radeon_cs_parser *p,
776 struct radeon_cs_reloc **cs_reloc,
777 int nomm)
778{
779 struct radeon_cs_chunk *relocs_chunk;
780 struct radeon_cs_packet p3reloc;
781 unsigned idx;
782 int r;
783
784 if (p->chunk_relocs_idx == -1) {
785 DRM_ERROR("No relocation chunk !\n");
786 return -EINVAL;
787 }
788 *cs_reloc = NULL;
789 relocs_chunk = &p->chunks[p->chunk_relocs_idx];
790 r = radeon_cs_packet_parse(p, &p3reloc, p->idx);
791 if (r)
792 return r;
793 p->idx += p3reloc.count + 2;
794 if (p3reloc.type != RADEON_PACKET_TYPE3 ||
795 p3reloc.opcode != RADEON_PACKET3_NOP) {
796 DRM_ERROR("No packet3 for relocation for packet at %d.\n",
797 p3reloc.idx);
798 radeon_cs_dump_packet(p, &p3reloc);
799 return -EINVAL;
800 }
801 idx = radeon_get_ib_value(p, p3reloc.idx + 1);
802 if (idx >= relocs_chunk->length_dw) {
803 DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
804 idx, relocs_chunk->length_dw);
805 radeon_cs_dump_packet(p, &p3reloc);
806 return -EINVAL;
807 }
808 /* FIXME: we assume reloc size is 4 dwords */
809 if (nomm) {
810 *cs_reloc = p->relocs;
811 (*cs_reloc)->lobj.gpu_offset =
812 (u64)relocs_chunk->kdata[idx + 3] << 32;
813 (*cs_reloc)->lobj.gpu_offset |= relocs_chunk->kdata[idx + 0];
814 } else
815 *cs_reloc = p->relocs_ptr[(idx / 4)];
816 return 0;
817}