2 * Copyright 2008 Jerome Glisse.
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:
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
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.
25 * Jerome Glisse <glisse@freedesktop.org>
28 #include "radeon_drm.h"
29 #include "radeon_reg.h"
32 void r100_cs_dump_packet(struct radeon_cs_parser *p,
33 struct radeon_cs_packet *pkt);
35 int radeon_cs_parser_relocs(struct radeon_cs_parser *p)
37 struct drm_device *ddev = p->rdev->ddev;
38 struct radeon_cs_chunk *chunk;
42 if (p->chunk_relocs_idx == -1) {
45 chunk = &p->chunks[p->chunk_relocs_idx];
46 /* FIXME: we assume that each relocs use 4 dwords */
47 p->nrelocs = chunk->length_dw / 4;
48 p->relocs_ptr = kcalloc(p->nrelocs, sizeof(void *), GFP_KERNEL);
49 if (p->relocs_ptr == NULL) {
52 p->relocs = kcalloc(p->nrelocs, sizeof(struct radeon_cs_reloc), GFP_KERNEL);
53 if (p->relocs == NULL) {
56 for (i = 0; i < p->nrelocs; i++) {
57 struct drm_radeon_cs_reloc *r;
60 r = (struct drm_radeon_cs_reloc *)&chunk->kdata[i*4];
61 for (j = 0; j < i; j++) {
62 if (r->handle == p->relocs[j].handle) {
63 p->relocs_ptr[i] = &p->relocs[j];
69 p->relocs[i].gobj = drm_gem_object_lookup(ddev,
72 if (p->relocs[i].gobj == NULL) {
73 DRM_ERROR("gem object lookup failed 0x%x\n",
77 p->relocs_ptr[i] = &p->relocs[i];
78 p->relocs[i].robj = gem_to_radeon_bo(p->relocs[i].gobj);
79 p->relocs[i].lobj.bo = p->relocs[i].robj;
80 p->relocs[i].lobj.wdomain = r->write_domain;
81 p->relocs[i].lobj.rdomain = r->read_domains;
82 p->relocs[i].lobj.tv.bo = &p->relocs[i].robj->tbo;
83 p->relocs[i].handle = r->handle;
84 p->relocs[i].flags = r->flags;
85 radeon_bo_list_add_object(&p->relocs[i].lobj,
88 p->relocs[i].handle = 0;
90 return radeon_bo_list_validate(&p->validated);
93 static int radeon_cs_get_ring(struct radeon_cs_parser *p, u32 ring, s32 priority)
95 p->priority = priority;
99 DRM_ERROR("unknown ring id: %d\n", ring);
101 case RADEON_CS_RING_GFX:
102 p->ring = RADEON_RING_TYPE_GFX_INDEX;
104 case RADEON_CS_RING_COMPUTE:
106 p->ring = RADEON_RING_TYPE_GFX_INDEX;
112 int radeon_cs_parser_init(struct radeon_cs_parser *p, void *data)
114 struct drm_radeon_cs *cs = data;
115 uint64_t *chunk_array_ptr;
117 u32 ring = RADEON_CS_RING_GFX;
120 if (!cs->num_chunks) {
124 INIT_LIST_HEAD(&p->validated);
126 p->chunk_ib_idx = -1;
127 p->chunk_relocs_idx = -1;
128 p->chunk_flags_idx = -1;
129 p->chunks_array = kcalloc(cs->num_chunks, sizeof(uint64_t), GFP_KERNEL);
130 if (p->chunks_array == NULL) {
133 chunk_array_ptr = (uint64_t *)(unsigned long)(cs->chunks);
134 if (DRM_COPY_FROM_USER(p->chunks_array, chunk_array_ptr,
135 sizeof(uint64_t)*cs->num_chunks)) {
139 p->nchunks = cs->num_chunks;
140 p->chunks = kcalloc(p->nchunks, sizeof(struct radeon_cs_chunk), GFP_KERNEL);
141 if (p->chunks == NULL) {
144 for (i = 0; i < p->nchunks; i++) {
145 struct drm_radeon_cs_chunk __user **chunk_ptr = NULL;
146 struct drm_radeon_cs_chunk user_chunk;
147 uint32_t __user *cdata;
149 chunk_ptr = (void __user*)(unsigned long)p->chunks_array[i];
150 if (DRM_COPY_FROM_USER(&user_chunk, chunk_ptr,
151 sizeof(struct drm_radeon_cs_chunk))) {
154 p->chunks[i].length_dw = user_chunk.length_dw;
155 p->chunks[i].kdata = NULL;
156 p->chunks[i].chunk_id = user_chunk.chunk_id;
158 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) {
159 p->chunk_relocs_idx = i;
161 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_IB) {
163 /* zero length IB isn't useful */
164 if (p->chunks[i].length_dw == 0)
167 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
168 p->chunk_flags_idx = i;
169 /* zero length flags aren't useful */
170 if (p->chunks[i].length_dw == 0)
174 p->chunks[i].length_dw = user_chunk.length_dw;
175 p->chunks[i].user_ptr = (void __user *)(unsigned long)user_chunk.chunk_data;
177 cdata = (uint32_t *)(unsigned long)user_chunk.chunk_data;
178 if ((p->chunks[i].chunk_id == RADEON_CHUNK_ID_RELOCS) ||
179 (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS)) {
180 size = p->chunks[i].length_dw * sizeof(uint32_t);
181 p->chunks[i].kdata = kmalloc(size, GFP_KERNEL);
182 if (p->chunks[i].kdata == NULL) {
185 if (DRM_COPY_FROM_USER(p->chunks[i].kdata,
186 p->chunks[i].user_ptr, size)) {
189 if (p->chunks[i].chunk_id == RADEON_CHUNK_ID_FLAGS) {
190 p->cs_flags = p->chunks[i].kdata[0];
191 if (p->chunks[i].length_dw > 1)
192 ring = p->chunks[i].kdata[1];
193 if (p->chunks[i].length_dw > 2)
194 priority = (s32)p->chunks[i].kdata[2];
199 if ((p->cs_flags & RADEON_CS_USE_VM) &&
200 (p->rdev->family < CHIP_CAYMAN)) {
201 DRM_ERROR("VM not supported on asic!\n");
202 if (p->chunk_relocs_idx != -1)
203 kfree(p->chunks[p->chunk_relocs_idx].kdata);
204 if (p->chunk_flags_idx != -1)
205 kfree(p->chunks[p->chunk_flags_idx].kdata);
209 if (radeon_cs_get_ring(p, ring, priority)) {
210 if (p->chunk_relocs_idx != -1)
211 kfree(p->chunks[p->chunk_relocs_idx].kdata);
212 if (p->chunk_flags_idx != -1)
213 kfree(p->chunks[p->chunk_flags_idx].kdata);
218 /* deal with non-vm */
219 if ((p->chunk_ib_idx != -1) &&
220 ((p->cs_flags & RADEON_CS_USE_VM) == 0) &&
221 (p->chunks[p->chunk_ib_idx].chunk_id == RADEON_CHUNK_ID_IB)) {
222 if (p->chunks[p->chunk_ib_idx].length_dw > (16 * 1024)) {
223 DRM_ERROR("cs IB too big: %d\n",
224 p->chunks[p->chunk_ib_idx].length_dw);
227 p->chunks[p->chunk_ib_idx].kpage[0] = kmalloc(PAGE_SIZE, GFP_KERNEL);
228 p->chunks[p->chunk_ib_idx].kpage[1] = kmalloc(PAGE_SIZE, GFP_KERNEL);
229 if (p->chunks[p->chunk_ib_idx].kpage[0] == NULL ||
230 p->chunks[p->chunk_ib_idx].kpage[1] == NULL) {
231 kfree(p->chunks[p->chunk_ib_idx].kpage[0]);
232 kfree(p->chunks[p->chunk_ib_idx].kpage[1]);
235 p->chunks[p->chunk_ib_idx].kpage_idx[0] = -1;
236 p->chunks[p->chunk_ib_idx].kpage_idx[1] = -1;
237 p->chunks[p->chunk_ib_idx].last_copied_page = -1;
238 p->chunks[p->chunk_ib_idx].last_page_index =
239 ((p->chunks[p->chunk_ib_idx].length_dw * 4) - 1) / PAGE_SIZE;
246 * cs_parser_fini() - clean parser states
247 * @parser: parser structure holding parsing context.
248 * @error: error number
250 * If error is set than unvalidate buffer, otherwise just free memory
251 * used by parsing context.
253 static void radeon_cs_parser_fini(struct radeon_cs_parser *parser, int error)
258 if (!error && parser->ib)
259 ttm_eu_fence_buffer_objects(&parser->validated,
262 ttm_eu_backoff_reservation(&parser->validated);
264 if (parser->relocs != NULL) {
265 for (i = 0; i < parser->nrelocs; i++) {
266 if (parser->relocs[i].gobj)
267 drm_gem_object_unreference_unlocked(parser->relocs[i].gobj);
270 kfree(parser->track);
271 kfree(parser->relocs);
272 kfree(parser->relocs_ptr);
273 for (i = 0; i < parser->nchunks; i++) {
274 kfree(parser->chunks[i].kdata);
275 kfree(parser->chunks[i].kpage[0]);
276 kfree(parser->chunks[i].kpage[1]);
278 kfree(parser->chunks);
279 kfree(parser->chunks_array);
280 radeon_ib_free(parser->rdev, &parser->ib);
283 static int radeon_cs_ib_chunk(struct radeon_device *rdev,
284 struct radeon_cs_parser *parser)
286 struct radeon_cs_chunk *ib_chunk;
289 if (parser->chunk_ib_idx == -1)
292 if (parser->cs_flags & RADEON_CS_USE_VM)
295 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
296 /* Copy the packet into the IB, the parser will read from the
297 * input memory (cached) and write to the IB (which can be
300 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
301 ib_chunk->length_dw * 4);
303 DRM_ERROR("Failed to get ib !\n");
306 parser->ib->length_dw = ib_chunk->length_dw;
307 r = radeon_cs_parse(parser);
308 if (r || parser->parser_error) {
309 DRM_ERROR("Invalid command stream !\n");
312 r = radeon_cs_finish_pages(parser);
314 DRM_ERROR("Invalid command stream !\n");
317 parser->ib->vm_id = 0;
318 r = radeon_ib_schedule(rdev, parser->ib);
320 DRM_ERROR("Failed to schedule IB !\n");
325 static int radeon_bo_vm_update_pte(struct radeon_cs_parser *parser,
326 struct radeon_vm *vm)
328 struct radeon_bo_list *lobj;
329 struct radeon_bo *bo;
332 list_for_each_entry(lobj, &parser->validated, tv.head) {
334 r = radeon_vm_bo_update_pte(parser->rdev, vm, bo, &bo->tbo.mem);
342 static int radeon_cs_ib_vm_chunk(struct radeon_device *rdev,
343 struct radeon_cs_parser *parser)
345 struct radeon_cs_chunk *ib_chunk;
346 struct radeon_fpriv *fpriv = parser->filp->driver_priv;
347 struct radeon_vm *vm = &fpriv->vm;
350 if (parser->chunk_ib_idx == -1)
353 if ((parser->cs_flags & RADEON_CS_USE_VM) == 0)
356 ib_chunk = &parser->chunks[parser->chunk_ib_idx];
357 if (ib_chunk->length_dw > RADEON_IB_VM_MAX_SIZE) {
358 DRM_ERROR("cs IB too big: %d\n", ib_chunk->length_dw);
361 r = radeon_ib_get(rdev, parser->ring, &parser->ib,
362 ib_chunk->length_dw * 4);
364 DRM_ERROR("Failed to get ib !\n");
367 parser->ib->length_dw = ib_chunk->length_dw;
368 /* Copy the packet into the IB */
369 if (DRM_COPY_FROM_USER(parser->ib->ptr, ib_chunk->user_ptr,
370 ib_chunk->length_dw * 4)) {
373 r = radeon_ring_ib_parse(rdev, parser->ring, parser->ib);
378 mutex_lock(&vm->mutex);
379 r = radeon_vm_bind(rdev, vm);
383 r = radeon_bo_vm_update_pte(parser, vm);
387 parser->ib->vm_id = vm->id;
388 /* ib pool is bind at 0 in virtual address space to gpu_addr is the
389 * offset inside the pool bo
391 parser->ib->gpu_addr = parser->ib->sa_bo.offset;
392 r = radeon_ib_schedule(rdev, parser->ib);
396 radeon_fence_unref(&vm->fence);
398 vm->fence = radeon_fence_ref(parser->ib->fence);
400 mutex_unlock(&fpriv->vm.mutex);
404 int radeon_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
406 struct radeon_device *rdev = dev->dev_private;
407 struct radeon_cs_parser parser;
410 radeon_mutex_lock(&rdev->cs_mutex);
411 /* initialize parser */
412 memset(&parser, 0, sizeof(struct radeon_cs_parser));
415 parser.dev = rdev->dev;
416 parser.family = rdev->family;
417 r = radeon_cs_parser_init(&parser, data);
419 DRM_ERROR("Failed to initialize parser !\n");
420 radeon_cs_parser_fini(&parser, r);
421 radeon_mutex_unlock(&rdev->cs_mutex);
424 r = radeon_cs_parser_relocs(&parser);
426 if (r != -ERESTARTSYS)
427 DRM_ERROR("Failed to parse relocation %d!\n", r);
428 radeon_cs_parser_fini(&parser, r);
429 radeon_mutex_unlock(&rdev->cs_mutex);
432 r = radeon_cs_ib_chunk(rdev, &parser);
436 r = radeon_cs_ib_vm_chunk(rdev, &parser);
441 radeon_cs_parser_fini(&parser, r);
442 radeon_mutex_unlock(&rdev->cs_mutex);
446 int radeon_cs_finish_pages(struct radeon_cs_parser *p)
448 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
450 int size = PAGE_SIZE;
452 for (i = ibc->last_copied_page + 1; i <= ibc->last_page_index; i++) {
453 if (i == ibc->last_page_index) {
454 size = (ibc->length_dw * 4) % PAGE_SIZE;
459 if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)),
460 ibc->user_ptr + (i * PAGE_SIZE),
467 int radeon_cs_update_pages(struct radeon_cs_parser *p, int pg_idx)
470 struct radeon_cs_chunk *ibc = &p->chunks[p->chunk_ib_idx];
472 int size = PAGE_SIZE;
474 for (i = ibc->last_copied_page + 1; i < pg_idx; i++) {
475 if (DRM_COPY_FROM_USER(p->ib->ptr + (i * (PAGE_SIZE/4)),
476 ibc->user_ptr + (i * PAGE_SIZE),
478 p->parser_error = -EFAULT;
483 new_page = ibc->kpage_idx[0] < ibc->kpage_idx[1] ? 0 : 1;
485 if (pg_idx == ibc->last_page_index) {
486 size = (ibc->length_dw * 4) % PAGE_SIZE;
491 if (DRM_COPY_FROM_USER(ibc->kpage[new_page],
492 ibc->user_ptr + (pg_idx * PAGE_SIZE),
494 p->parser_error = -EFAULT;
498 /* copy to IB here */
499 memcpy((void *)(p->ib->ptr+(pg_idx*(PAGE_SIZE/4))), ibc->kpage[new_page], size);
501 ibc->last_copied_page = pg_idx;
502 ibc->kpage_idx[new_page] = pg_idx;