2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
29 #include "radeon_priv.h"
30 #include "radeon_drm.h"
33 int radeon_ctx_set_bo_new(struct radeon_ctx
*ctx
, struct radeon_bo
*bo
)
37 ptr
= realloc(ctx
->bo
, sizeof(struct radeon_bo
) * (ctx
->nbo
+ 1));
42 ctx
->bo
[ctx
->nbo
] = bo
;
47 struct radeon_bo
*radeon_ctx_get_bo(struct radeon_ctx
*ctx
, unsigned reloc
)
49 struct radeon_cs_reloc
*greloc
;
52 greloc
= (void *)(((u8
*)ctx
->reloc
) + reloc
* 4);
53 for (i
= 0; i
< ctx
->nbo
; i
++) {
54 if (ctx
->bo
[i
]->handle
== greloc
->handle
) {
55 return radeon_bo_incref(ctx
->radeon
, ctx
->bo
[i
]);
58 fprintf(stderr
, "%s no bo for reloc[%d 0x%08X] %d\n", __func__
, reloc
, greloc
->handle
, ctx
->nbo
);
62 void radeon_ctx_get_placement(struct radeon_ctx
*ctx
, unsigned reloc
, u32
*placement
)
64 struct radeon_cs_reloc
*greloc
;
69 greloc
= (void *)(((u8
*)ctx
->reloc
) + reloc
* 4);
70 for (i
= 0; i
< ctx
->nbo
; i
++) {
71 if (ctx
->bo
[i
]->handle
== greloc
->handle
) {
72 placement
[0] = greloc
->read_domain
| greloc
->write_domain
;
73 placement
[1] = placement
[0];
79 struct radeon_ctx
*radeon_ctx(struct radeon
*radeon
)
81 struct radeon_ctx
*ctx
;
85 ctx
= calloc(1, sizeof(*ctx
));
88 ctx
->radeon
= radeon_incref(radeon
);
92 struct radeon_ctx
*radeon_ctx_incref(struct radeon_ctx
*ctx
)
98 struct radeon_ctx
*radeon_ctx_decref(struct radeon_ctx
*ctx
)
104 if (--ctx
->refcount
> 0) {
108 for (i
= 0; i
< ctx
->ndraw
; i
++) {
109 ctx
->draw
[i
] = radeon_draw_decref(ctx
->draw
[i
]);
111 for (i
= 0; i
< ctx
->nbo
; i
++) {
112 ctx
->bo
[i
] = radeon_bo_decref(ctx
->radeon
, ctx
->bo
[i
]);
114 ctx
->radeon
= radeon_decref(ctx
->radeon
);
120 memset(ctx
, 0, sizeof(*ctx
));
125 static int radeon_ctx_state_bo(struct radeon_ctx
*ctx
, struct radeon_state
*state
)
132 for (i
= 0; i
< state
->nbo
; i
++) {
133 for (j
= 0; j
< ctx
->nbo
; j
++) {
134 if (state
->bo
[i
] == ctx
->bo
[j
])
138 radeon_bo_incref(ctx
->radeon
, state
->bo
[i
]);
139 r
= radeon_ctx_set_bo_new(ctx
, state
->bo
[i
]);
148 int radeon_ctx_submit(struct radeon_ctx
*ctx
)
150 struct drm_radeon_cs drmib
;
151 struct drm_radeon_cs_chunk chunks
[2];
152 uint64_t chunk_array
[2];
158 for (r
= 0; r
< ctx
->cpm4
; r
++) {
159 fprintf(stderr
, "0x%08X\n", ctx
->pm4
[r
]);
162 drmib
.num_chunks
= 2;
163 drmib
.chunks
= (uint64_t)(uintptr_t)chunk_array
;
164 chunks
[0].chunk_id
= RADEON_CHUNK_ID_IB
;
165 chunks
[0].length_dw
= ctx
->cpm4
;
166 chunks
[0].chunk_data
= (uint64_t)(uintptr_t)ctx
->pm4
;
167 chunks
[1].chunk_id
= RADEON_CHUNK_ID_RELOCS
;
168 chunks
[1].length_dw
= ctx
->nreloc
* sizeof(struct radeon_cs_reloc
) / 4;
169 chunks
[1].chunk_data
= (uint64_t)(uintptr_t)ctx
->reloc
;
170 chunk_array
[0] = (uint64_t)(uintptr_t)&chunks
[0];
171 chunk_array
[1] = (uint64_t)(uintptr_t)&chunks
[1];
173 r
= drmCommandWriteRead(ctx
->radeon
->fd
, DRM_RADEON_CS
, &drmib
,
174 sizeof(struct drm_radeon_cs
));
179 static int radeon_ctx_reloc(struct radeon_ctx
*ctx
, struct radeon_bo
*bo
,
180 unsigned id
, unsigned *placement
)
183 struct radeon_cs_reloc
*ptr
;
185 for (i
= 0; i
< ctx
->nreloc
; i
++) {
186 if (ctx
->reloc
[i
].handle
== bo
->handle
) {
187 ctx
->pm4
[id
] = i
* sizeof(struct radeon_cs_reloc
) / 4;
191 ptr
= realloc(ctx
->reloc
, sizeof(struct radeon_cs_reloc
) * (ctx
->nreloc
+ 1));
195 ptr
[ctx
->nreloc
].handle
= bo
->handle
;
196 ptr
[ctx
->nreloc
].read_domain
= placement
[0] | placement
[1];
197 ptr
[ctx
->nreloc
].write_domain
= placement
[0] | placement
[1];
198 ptr
[ctx
->nreloc
].flags
= 0;
199 ctx
->pm4
[id
] = ctx
->nreloc
* sizeof(struct radeon_cs_reloc
) / 4;
204 static int radeon_ctx_state_schedule(struct radeon_ctx
*ctx
, struct radeon_state
*state
)
206 unsigned i
, rid
, bid
, cid
;
211 memcpy(&ctx
->pm4
[ctx
->id
], state
->pm4
, state
->cpm4
* 4);
212 for (i
= 0; i
< state
->nreloc
; i
++) {
213 rid
= state
->reloc_pm4_id
[i
];
214 bid
= state
->reloc_bo_id
[i
];
216 r
= radeon_ctx_reloc(ctx
, state
->bo
[bid
], cid
,
217 &state
->placement
[bid
* 2]);
219 fprintf(stderr
, "%s state %d failed to reloc\n", __func__
, state
->stype
->stype
);
223 ctx
->id
+= state
->cpm4
;
227 int radeon_ctx_set_query_state(struct radeon_ctx
*ctx
, struct radeon_state
*state
)
232 /* !!! ONLY ACCEPT QUERY STATE HERE !!! */
233 if (state
->stype
->stype
!= R600_STATE_QUERY_BEGIN
&& state
->stype
->stype
!= R600_STATE_QUERY_END
) {
236 r
= radeon_state_pm4(state
);
239 if ((ctx
->draw_cpm4
+ state
->cpm4
) > RADEON_CTX_MAX_PM4
) {
243 if (state
->cpm4
>= RADEON_CTX_MAX_PM4
) {
244 fprintf(stderr
, "%s single state too big %d, max %d\n",
245 __func__
, state
->cpm4
, RADEON_CTX_MAX_PM4
);
248 tmp
= realloc(ctx
->state
, (ctx
->nstate
+ 1) * sizeof(void*));
252 ctx
->state
[ctx
->nstate
++] = radeon_state_incref(state
);
253 /* BEGIN/END query are balanced in the same cs so account for END
254 * END query when scheduling BEGIN query
256 if (state
->stype
->stype
== R600_STATE_QUERY_BEGIN
) {
257 ctx
->draw_cpm4
+= state
->cpm4
* 2;
262 int radeon_ctx_set_draw_new(struct radeon_ctx
*ctx
, struct radeon_draw
*draw
)
264 struct radeon_draw
*pdraw
= NULL
;
265 struct radeon_draw
**ndraw
;
266 struct radeon_state
*nstate
, *ostate
;
267 unsigned cpm4
, i
, cstate
;
271 ndraw
= realloc(ctx
->draw
, sizeof(void*) * (ctx
->ndraw
+ 1));
275 for (i
= 0; i
< draw
->nstate
; i
++) {
276 r
= radeon_ctx_state_bo(ctx
, draw
->state
[i
]);
280 r
= radeon_draw_check(draw
);
283 if (draw
->cpm4
>= RADEON_CTX_MAX_PM4
) {
284 fprintf(stderr
, "%s single draw too big %d, max %d\n",
285 __func__
, draw
->cpm4
, RADEON_CTX_MAX_PM4
);
288 tmp
= realloc(ctx
->state
, (ctx
->nstate
+ draw
->nstate
) * sizeof(void*));
293 for (i
= 0, cpm4
= 0, cstate
= ctx
->nstate
; i
< draw
->nstate
- 1; i
++) {
294 nstate
= draw
->state
[i
];
296 if (pdraw
&& pdraw
->state
[i
]) {
297 ostate
= pdraw
->state
[i
];
298 if (ostate
->pm4_crc
!= nstate
->pm4_crc
) {
299 ctx
->state
[cstate
++] = nstate
;
300 cpm4
+= nstate
->cpm4
;
303 ctx
->state
[cstate
++] = nstate
;
304 cpm4
+= nstate
->cpm4
;
308 /* The last state is the draw state always add it */
309 if (draw
->state
[i
] == NULL
) {
310 fprintf(stderr
, "%s no draw command\n", __func__
);
313 ctx
->state
[cstate
++] = draw
->state
[i
];
314 cpm4
+= draw
->state
[i
]->cpm4
;
315 if ((ctx
->draw_cpm4
+ cpm4
) > RADEON_CTX_MAX_PM4
) {
319 ctx
->draw_cpm4
+= cpm4
;
320 ctx
->nstate
= cstate
;
321 ctx
->draw
[ctx
->ndraw
++] = draw
;
326 int radeon_ctx_set_draw(struct radeon_ctx
*ctx
, struct radeon_draw
*draw
)
330 radeon_draw_incref(draw
);
331 r
= radeon_ctx_set_draw_new(ctx
, draw
);
333 radeon_draw_decref(draw
);
337 int radeon_ctx_pm4(struct radeon_ctx
*ctx
)
344 ctx
->pm4
= malloc(ctx
->draw_cpm4
* 4);
345 if (ctx
->pm4
== NULL
)
347 for (i
= 0, ctx
->id
= 0; i
< ctx
->nstate
; i
++) {
348 r
= radeon_ctx_state_schedule(ctx
, ctx
->state
[i
]);
352 if (ctx
->id
!= ctx
->draw_cpm4
) {
353 fprintf(stderr
, "%s miss predicted pm4 size %d for %d\n",
354 __func__
, ctx
->draw_cpm4
, ctx
->id
);
357 ctx
->cpm4
= ctx
->draw_cpm4
;
361 void radeon_ctx_dump_bof(struct radeon_ctx
*ctx
, const char *file
)
363 bof_t
*bcs
, *blob
, *array
, *bo
, *size
, *handle
, *device_id
, *root
;
366 root
= device_id
= bcs
= blob
= array
= bo
= size
= handle
= NULL
;
370 device_id
= bof_int32(ctx
->radeon
->device
);
371 if (device_id
== NULL
)
373 if (bof_object_set(root
, "device_id", device_id
))
375 bof_decref(device_id
);
378 printf("%d relocs\n", ctx
->nreloc
);
379 blob
= bof_blob(ctx
->nreloc
* 16, ctx
->reloc
);
382 if (bof_object_set(root
, "reloc", blob
))
387 printf("%d pm4\n", ctx
->cpm4
);
388 blob
= bof_blob(ctx
->cpm4
* 4, ctx
->pm4
);
391 if (bof_object_set(root
, "pm4", blob
))
399 for (i
= 0; i
< ctx
->nbo
; i
++) {
403 size
= bof_int32(ctx
->bo
[i
]->size
);
406 if (bof_object_set(bo
, "size", size
))
410 handle
= bof_int32(ctx
->bo
[i
]->handle
);
413 if (bof_object_set(bo
, "handle", handle
))
417 radeon_bo_map(ctx
->radeon
, ctx
->bo
[i
]);
418 blob
= bof_blob(ctx
->bo
[i
]->size
, ctx
->bo
[i
]->data
);
419 radeon_bo_unmap(ctx
->radeon
, ctx
->bo
[i
]);
422 if (bof_object_set(bo
, "data", blob
))
426 if (bof_array_append(array
, bo
))
431 if (bof_object_set(root
, "bo", array
))
433 bof_dump_file(root
, file
);
434 printf("done dump\n");
441 bof_decref(device_id
);