blob: 1e53099c31b4e92fec41ee210ace24f962e02f9c [file] [log] [blame]
Dave Airlie2fa2db12009-06-17 17:47:42 +10001/*
2 * Copyright © 2008 Jérôme Glisse
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26/*
27 * Authors:
28 * Aapo Tahkola <aet@rasterburn.org>
29 * Nicolai Haehnle <prefect_@gmx.net>
30 * Jérôme Glisse <glisse@freedesktop.org>
31 */
32#include <assert.h>
33#include <errno.h>
34#include <stdlib.h>
35#include <sys/mman.h>
36#include <sys/ioctl.h>
37#include "radeon_cs.h"
Dave Airlie125994a2009-12-17 14:11:55 +100038#include "radeon_cs_int.h"
39#include "radeon_bo_int.h"
Dave Airlie2fa2db12009-06-17 17:47:42 +100040#include "radeon_cs_gem.h"
41#include "radeon_bo_gem.h"
42#include "drm.h"
43#include "xf86drm.h"
44#include "radeon_drm.h"
45
46#pragma pack(1)
47struct cs_reloc_gem {
48 uint32_t handle;
49 uint32_t read_domain;
50 uint32_t write_domain;
51 uint32_t flags;
52};
53
54#pragma pack()
55#define RELOC_SIZE (sizeof(struct cs_reloc_gem) / sizeof(uint32_t))
56
57struct cs_gem {
Dave Airlie125994a2009-12-17 14:11:55 +100058 struct radeon_cs_int base;
Dave Airlie2fa2db12009-06-17 17:47:42 +100059 struct drm_radeon_cs cs;
60 struct drm_radeon_cs_chunk chunks[2];
61 unsigned nrelocs;
62 uint32_t *relocs;
Dave Airlie125994a2009-12-17 14:11:55 +100063 struct radeon_bo_int **relocs_bo;
Dave Airlie2fa2db12009-06-17 17:47:42 +100064};
65
Dave Airlie125994a2009-12-17 14:11:55 +100066static struct radeon_cs_int *cs_gem_create(struct radeon_cs_manager *csm,
Dave Airlie2fa2db12009-06-17 17:47:42 +100067 uint32_t ndw)
68{
69 struct cs_gem *csg;
70
71 /* max cmd buffer size is 64Kb */
72 if (ndw > (64 * 1024 / 4)) {
73 return NULL;
74 }
75 csg = (struct cs_gem*)calloc(1, sizeof(struct cs_gem));
76 if (csg == NULL) {
77 return NULL;
78 }
79 csg->base.csm = csm;
80 csg->base.ndw = 64 * 1024 / 4;
81 csg->base.packets = (uint32_t*)calloc(1, 64 * 1024);
82 if (csg->base.packets == NULL) {
83 free(csg);
84 return NULL;
85 }
86 csg->base.relocs_total_size = 0;
87 csg->base.crelocs = 0;
88 csg->nrelocs = 4096 / (4 * 4) ;
Dave Airlie125994a2009-12-17 14:11:55 +100089 csg->relocs_bo = (struct radeon_bo_int**)calloc(1,
Dave Airlie2fa2db12009-06-17 17:47:42 +100090 csg->nrelocs*sizeof(void*));
91 if (csg->relocs_bo == NULL) {
92 free(csg->base.packets);
93 free(csg);
94 return NULL;
95 }
96 csg->base.relocs = csg->relocs = (uint32_t*)calloc(1, 4096);
97 if (csg->relocs == NULL) {
98 free(csg->relocs_bo);
99 free(csg->base.packets);
100 free(csg);
101 return NULL;
102 }
103 csg->chunks[0].chunk_id = RADEON_CHUNK_ID_IB;
104 csg->chunks[0].length_dw = 0;
Dave Airliecdd325b2009-09-15 07:29:02 +1000105 csg->chunks[0].chunk_data = (uint64_t)(uintptr_t)csg->base.packets;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000106 csg->chunks[1].chunk_id = RADEON_CHUNK_ID_RELOCS;
107 csg->chunks[1].length_dw = 0;
Dave Airliecdd325b2009-09-15 07:29:02 +1000108 csg->chunks[1].chunk_data = (uint64_t)(uintptr_t)csg->relocs;
Dave Airlie125994a2009-12-17 14:11:55 +1000109 return (struct radeon_cs_int*)csg;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000110}
111
Dave Airlie125994a2009-12-17 14:11:55 +1000112static int cs_gem_write_reloc(struct radeon_cs_int *cs,
Dave Airlie2fa2db12009-06-17 17:47:42 +1000113 struct radeon_bo *bo,
114 uint32_t read_domain,
115 uint32_t write_domain,
116 uint32_t flags)
117{
Dave Airlie125994a2009-12-17 14:11:55 +1000118 struct radeon_bo_int *boi = (struct radeon_bo_int *)bo;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000119 struct cs_gem *csg = (struct cs_gem*)cs;
120 struct cs_reloc_gem *reloc;
121 uint32_t idx;
122 unsigned i;
123
Dave Airlie125994a2009-12-17 14:11:55 +1000124 assert(boi->space_accounted);
Dave Airlie2fa2db12009-06-17 17:47:42 +1000125
126 /* check domains */
127 if ((read_domain && write_domain) || (!read_domain && !write_domain)) {
128 /* in one CS a bo can only be in read or write domain but not
129 * in read & write domain at the same sime
130 */
131 return -EINVAL;
132 }
133 if (read_domain == RADEON_GEM_DOMAIN_CPU) {
134 return -EINVAL;
135 }
136 if (write_domain == RADEON_GEM_DOMAIN_CPU) {
137 return -EINVAL;
138 }
139 /* check if bo is already referenced */
140 for(i = 0; i < cs->crelocs; i++) {
141 idx = i * RELOC_SIZE;
142 reloc = (struct cs_reloc_gem*)&csg->relocs[idx];
143 if (reloc->handle == bo->handle) {
144 /* Check domains must be in read or write. As we check already
145 * checked that in argument one of the read or write domain was
146 * set we only need to check that if previous reloc as the read
147 * domain set then the read_domain should also be set for this
148 * new relocation.
149 */
Dave Airliede1ed012009-06-30 12:19:28 +1000150 /* the DDX expects to read and write from same pixmap */
151 if (write_domain && (reloc->read_domain & write_domain)) {
152 reloc->read_domain = 0;
153 reloc->write_domain = write_domain;
154 } else if (read_domain & reloc->write_domain) {
155 reloc->read_domain = 0;
156 } else {
157 if (write_domain != reloc->write_domain)
158 return -EINVAL;
159 if (read_domain != reloc->read_domain)
160 return -EINVAL;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000161 }
Dave Airliede1ed012009-06-30 12:19:28 +1000162
Dave Airlie2fa2db12009-06-17 17:47:42 +1000163 reloc->read_domain |= read_domain;
164 reloc->write_domain |= write_domain;
165 /* update flags */
166 reloc->flags |= (flags & reloc->flags);
167 /* write relocation packet */
Dave Airlie125994a2009-12-17 14:11:55 +1000168 radeon_cs_write_dword((struct radeon_cs *)cs, 0xc0001000);
169 radeon_cs_write_dword((struct radeon_cs *)cs, idx);
Dave Airlie2fa2db12009-06-17 17:47:42 +1000170 return 0;
171 }
172 }
173 /* new relocation */
174 if (csg->base.crelocs >= csg->nrelocs) {
175 /* allocate more memory (TODO: should use a slab allocatore maybe) */
176 uint32_t *tmp, size;
177 size = ((csg->nrelocs + 1) * sizeof(struct radeon_bo*));
178 tmp = (uint32_t*)realloc(csg->relocs_bo, size);
179 if (tmp == NULL) {
180 return -ENOMEM;
181 }
Dave Airlie125994a2009-12-17 14:11:55 +1000182 csg->relocs_bo = (struct radeon_bo_int **)tmp;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000183 size = ((csg->nrelocs + 1) * RELOC_SIZE * 4);
184 tmp = (uint32_t*)realloc(csg->relocs, size);
185 if (tmp == NULL) {
186 return -ENOMEM;
187 }
188 cs->relocs = csg->relocs = tmp;
189 csg->nrelocs += 1;
Dave Airliecdd325b2009-09-15 07:29:02 +1000190 csg->chunks[1].chunk_data = (uint64_t)(uintptr_t)csg->relocs;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000191 }
Dave Airlie125994a2009-12-17 14:11:55 +1000192 csg->relocs_bo[csg->base.crelocs] = boi;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000193 idx = (csg->base.crelocs++) * RELOC_SIZE;
194 reloc = (struct cs_reloc_gem*)&csg->relocs[idx];
195 reloc->handle = bo->handle;
196 reloc->read_domain = read_domain;
197 reloc->write_domain = write_domain;
198 reloc->flags = flags;
199 csg->chunks[1].length_dw += RELOC_SIZE;
200 radeon_bo_ref(bo);
Dave Airlie125994a2009-12-17 14:11:55 +1000201 cs->relocs_total_size += boi->size;
202 radeon_cs_write_dword((struct radeon_cs *)cs, 0xc0001000);
203 radeon_cs_write_dword((struct radeon_cs *)cs, idx);
Dave Airlie2fa2db12009-06-17 17:47:42 +1000204 return 0;
205}
206
Dave Airlie125994a2009-12-17 14:11:55 +1000207static int cs_gem_begin(struct radeon_cs_int *cs,
Dave Airlie2fa2db12009-06-17 17:47:42 +1000208 uint32_t ndw,
209 const char *file,
210 const char *func,
211 int line)
212{
213
Dave Airlie125994a2009-12-17 14:11:55 +1000214 if (cs->section_ndw) {
Dave Airlie2fa2db12009-06-17 17:47:42 +1000215 fprintf(stderr, "CS already in a section(%s,%s,%d)\n",
216 cs->section_file, cs->section_func, cs->section_line);
217 fprintf(stderr, "CS can't start section(%s,%s,%d)\n",
218 file, func, line);
219 return -EPIPE;
220 }
Dave Airlie2fa2db12009-06-17 17:47:42 +1000221 cs->section_ndw = ndw;
222 cs->section_cdw = 0;
223 cs->section_file = file;
224 cs->section_func = func;
225 cs->section_line = line;
226
Dave Airlie2fa2db12009-06-17 17:47:42 +1000227 if (cs->cdw + ndw > cs->ndw) {
228 uint32_t tmp, *ptr;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000229
Mathias Fröhlich6eafd1c2009-11-03 11:41:26 -0500230 /* round up the required size to a multiple of 1024 */
231 tmp = (cs->cdw + ndw + 0x3FF) & (~0x3FF);
Dave Airlie2fa2db12009-06-17 17:47:42 +1000232 ptr = (uint32_t*)realloc(cs->packets, 4 * tmp);
233 if (ptr == NULL) {
234 return -ENOMEM;
235 }
236 cs->packets = ptr;
237 cs->ndw = tmp;
238 }
Dave Airlie2fa2db12009-06-17 17:47:42 +1000239 return 0;
240}
241
Dave Airlie125994a2009-12-17 14:11:55 +1000242static int cs_gem_end(struct radeon_cs_int *cs,
Dave Airlie2fa2db12009-06-17 17:47:42 +1000243 const char *file,
244 const char *func,
245 int line)
246
247{
Dave Airlie125994a2009-12-17 14:11:55 +1000248 if (!cs->section_ndw) {
Dave Airlie2fa2db12009-06-17 17:47:42 +1000249 fprintf(stderr, "CS no section to end at (%s,%s,%d)\n",
250 file, func, line);
251 return -EPIPE;
252 }
Dave Airlie2fa2db12009-06-17 17:47:42 +1000253 if (cs->section_ndw != cs->section_cdw) {
254 fprintf(stderr, "CS section size missmatch start at (%s,%s,%d) %d vs %d\n",
255 cs->section_file, cs->section_func, cs->section_line, cs->section_ndw, cs->section_cdw);
256 fprintf(stderr, "CS section end at (%s,%s,%d)\n",
257 file, func, line);
258 return -EPIPE;
259 }
Dave Airlie125994a2009-12-17 14:11:55 +1000260 cs->section_ndw = 0;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000261 return 0;
262}
263
Dave Airlie125994a2009-12-17 14:11:55 +1000264static int cs_gem_emit(struct radeon_cs_int *cs)
Dave Airlie2fa2db12009-06-17 17:47:42 +1000265{
266 struct cs_gem *csg = (struct cs_gem*)cs;
267 uint64_t chunk_array[2];
268 unsigned i;
269 int r;
270
271 csg->chunks[0].length_dw = cs->cdw;
272
Dave Airliecdd325b2009-09-15 07:29:02 +1000273 chunk_array[0] = (uint64_t)(uintptr_t)&csg->chunks[0];
274 chunk_array[1] = (uint64_t)(uintptr_t)&csg->chunks[1];
Dave Airlie2fa2db12009-06-17 17:47:42 +1000275
276 csg->cs.num_chunks = 2;
Dave Airliecdd325b2009-09-15 07:29:02 +1000277 csg->cs.chunks = (uint64_t)(uintptr_t)chunk_array;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000278
279 r = drmCommandWriteRead(cs->csm->fd, DRM_RADEON_CS,
280 &csg->cs, sizeof(struct drm_radeon_cs));
281 for (i = 0; i < csg->base.crelocs; i++) {
282 csg->relocs_bo[i]->space_accounted = 0;
Dave Airlie125994a2009-12-17 14:11:55 +1000283 radeon_bo_unref((struct radeon_bo *)csg->relocs_bo[i]);
Dave Airlie2fa2db12009-06-17 17:47:42 +1000284 csg->relocs_bo[i] = NULL;
285 }
286
287 cs->csm->read_used = 0;
288 cs->csm->vram_write_used = 0;
289 cs->csm->gart_write_used = 0;
290 return r;
291}
292
Dave Airlie125994a2009-12-17 14:11:55 +1000293static int cs_gem_destroy(struct radeon_cs_int *cs)
Dave Airlie2fa2db12009-06-17 17:47:42 +1000294{
295 struct cs_gem *csg = (struct cs_gem*)cs;
296
297 free(csg->relocs_bo);
298 free(cs->relocs);
299 free(cs->packets);
300 free(cs);
301 return 0;
302}
303
Dave Airlie125994a2009-12-17 14:11:55 +1000304static int cs_gem_erase(struct radeon_cs_int *cs)
Dave Airlie2fa2db12009-06-17 17:47:42 +1000305{
306 struct cs_gem *csg = (struct cs_gem*)cs;
307 unsigned i;
308
309 if (csg->relocs_bo) {
310 for (i = 0; i < csg->base.crelocs; i++) {
311 if (csg->relocs_bo[i]) {
Dave Airlie125994a2009-12-17 14:11:55 +1000312 radeon_bo_unref((struct radeon_bo *)csg->relocs_bo[i]);
Dave Airlie2fa2db12009-06-17 17:47:42 +1000313 csg->relocs_bo[i] = NULL;
314 }
315 }
316 }
317 cs->relocs_total_size = 0;
318 cs->cdw = 0;
Dave Airlie125994a2009-12-17 14:11:55 +1000319 cs->section_ndw = 0;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000320 cs->crelocs = 0;
321 csg->chunks[0].length_dw = 0;
322 csg->chunks[1].length_dw = 0;
323 return 0;
324}
325
Dave Airlie125994a2009-12-17 14:11:55 +1000326static int cs_gem_need_flush(struct radeon_cs_int *cs)
Dave Airlie2fa2db12009-06-17 17:47:42 +1000327{
328 return 0; //(cs->relocs_total_size > (32*1024*1024));
329}
330
331#define PACKET_TYPE0 0
332#define PACKET_TYPE1 1
333#define PACKET_TYPE2 2
334#define PACKET_TYPE3 3
335
336#define PACKET3_NOP 0x10
337#define PACKET3_SET_SCISSORS 0x1E
338#define PACKET3_3D_DRAW_VBUF 0x28
339#define PACKET3_3D_DRAW_IMMD 0x29
340#define PACKET3_3D_DRAW_INDX 0x2A
341#define PACKET3_3D_LOAD_VBPNTR 0x2F
342#define PACKET3_INDX_BUFFER 0x33
343#define PACKET3_3D_DRAW_VBUF_2 0x34
344#define PACKET3_3D_DRAW_IMMD_2 0x35
345#define PACKET3_3D_DRAW_INDX_2 0x36
346
347#define CP_PACKET_GET_TYPE(h) (((h) >> 30) & 3)
348#define CP_PACKET_GET_COUNT(h) (((h) >> 16) & 0x3FFF)
349#define CP_PACKET0_GET_REG(h) (((h) & 0x1FFF) << 2)
350#define CP_PACKET0_GET_ONE_REG_WR(h) (((h) >> 15) & 1)
351#define CP_PACKET3_GET_OPCODE(h) (((h) >> 8) & 0xFF)
352
Dave Airlie125994a2009-12-17 14:11:55 +1000353static void cs_gem_print(struct radeon_cs_int *cs, FILE *file)
Dave Airlie2fa2db12009-06-17 17:47:42 +1000354{
355 unsigned opcode;
356 unsigned reg;
357 unsigned cnt;
Pauli Nieminena474fd92009-08-18 18:51:37 +0300358 unsigned int i, j;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000359
360 for (i = 0; i < cs->cdw;) {
Pauli Nieminena474fd92009-08-18 18:51:37 +0300361 cnt = CP_PACKET_GET_COUNT(cs->packets[i]) + 1;
Dave Airlie2fa2db12009-06-17 17:47:42 +1000362 switch (CP_PACKET_GET_TYPE(cs->packets[i])) {
363 case PACKET_TYPE0:
Pauli Nieminena474fd92009-08-18 18:51:37 +0300364 fprintf(file, "Pkt0 at %d (%d dwords):\n", i, cnt);
Dave Airlie2fa2db12009-06-17 17:47:42 +1000365 reg = CP_PACKET0_GET_REG(cs->packets[i]);
366 if (CP_PACKET0_GET_ONE_REG_WR(cs->packets[i++])) {
Pauli Nieminena474fd92009-08-18 18:51:37 +0300367 for (j = 0; j < cnt; j++) {
Dave Airlie2fa2db12009-06-17 17:47:42 +1000368 fprintf(file, " 0x%08X -> 0x%04X\n",
369 cs->packets[i++], reg);
370 }
371 } else {
Pauli Nieminena474fd92009-08-18 18:51:37 +0300372 for (j = 0; j < cnt; j++) {
Dave Airlie2fa2db12009-06-17 17:47:42 +1000373 fprintf(file, " 0x%08X -> 0x%04X\n",
374 cs->packets[i++], reg);
375 reg += 4;
376 }
377 }
378 break;
379 case PACKET_TYPE3:
380 fprintf(file, "Pkt3 at %d :\n", i);
381 opcode = CP_PACKET3_GET_OPCODE(cs->packets[i++]);
382 switch (opcode) {
383 case PACKET3_NOP:
384 fprintf(file, " PACKET3_NOP:\n");
385 break;
386 case PACKET3_3D_DRAW_VBUF:
387 fprintf(file, " PACKET3_3D_DRAW_VBUF:\n");
388 break;
389 case PACKET3_3D_DRAW_IMMD:
390 fprintf(file, " PACKET3_3D_DRAW_IMMD:\n");
391 break;
392 case PACKET3_3D_DRAW_INDX:
393 fprintf(file, " PACKET3_3D_DRAW_INDX:\n");
394 break;
395 case PACKET3_3D_LOAD_VBPNTR:
396 fprintf(file, " PACKET3_3D_LOAD_VBPNTR:\n");
397 break;
398 case PACKET3_INDX_BUFFER:
399 fprintf(file, " PACKET3_INDX_BUFFER:\n");
400 break;
401 case PACKET3_3D_DRAW_VBUF_2:
402 fprintf(file, " PACKET3_3D_DRAW_VBUF_2:\n");
403 break;
404 case PACKET3_3D_DRAW_IMMD_2:
405 fprintf(file, " PACKET3_3D_DRAW_IMMD_2:\n");
406 break;
407 case PACKET3_3D_DRAW_INDX_2:
408 fprintf(file, " PACKET3_3D_DRAW_INDX_2:\n");
409 break;
410 default:
411 fprintf(file, "Unknow opcode 0x%02X at %d\n", opcode, i);
412 return;
413 }
Pauli Nieminena474fd92009-08-18 18:51:37 +0300414 for (j = 0; j < cnt; j++) {
Dave Airlie2fa2db12009-06-17 17:47:42 +1000415 fprintf(file, " 0x%08X\n", cs->packets[i++]);
416 }
417 break;
418 case PACKET_TYPE1:
419 case PACKET_TYPE2:
420 default:
421 fprintf(file, "Unknow packet 0x%08X at %d\n", cs->packets[i], i);
422 return;
423 }
424 }
425}
426
Dave Airlie2fa2db12009-06-17 17:47:42 +1000427
Dave Airlie2fa2db12009-06-17 17:47:42 +1000428
429static struct radeon_cs_funcs radeon_cs_gem_funcs = {
430 cs_gem_create,
431 cs_gem_write_reloc,
432 cs_gem_begin,
433 cs_gem_end,
434 cs_gem_emit,
435 cs_gem_destroy,
436 cs_gem_erase,
437 cs_gem_need_flush,
438 cs_gem_print,
Dave Airlie2fa2db12009-06-17 17:47:42 +1000439};
440
441struct radeon_cs_manager *radeon_cs_manager_gem_ctor(int fd)
442{
443 struct radeon_cs_manager *csm;
444
445 csm = (struct radeon_cs_manager*)calloc(1,
446 sizeof(struct radeon_cs_manager));
447 if (csm == NULL) {
448 return NULL;
449 }
450 csm->funcs = &radeon_cs_gem_funcs;
451 csm->fd = fd;
452 return csm;
453}
454
455void radeon_cs_manager_gem_dtor(struct radeon_cs_manager *csm)
456{
457 free(csm);
458}