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 static int radeon_ctx_set_bo_new(struct radeon_ctx
*ctx
, struct radeon_bo
*bo
)
35 if (ctx
->nbo
>= RADEON_CTX_MAX_PM4
)
37 ctx
->bo
[ctx
->nbo
] = bo
;
42 static struct radeon_bo
*radeon_ctx_get_bo(struct radeon_ctx
*ctx
, unsigned reloc
)
44 struct radeon_cs_reloc
*greloc
;
47 greloc
= (void *)(((u8
*)ctx
->reloc
) + reloc
* 4);
48 for (i
= 0; i
< ctx
->nbo
; i
++) {
49 if (ctx
->bo
[i
]->handle
== greloc
->handle
) {
50 return radeon_bo_incref(ctx
->radeon
, ctx
->bo
[i
]);
53 fprintf(stderr
, "%s no bo for reloc[%d 0x%08X] %d\n", __func__
, reloc
, greloc
->handle
, ctx
->nbo
);
57 static void radeon_ctx_get_placement(struct radeon_ctx
*ctx
, unsigned reloc
, u32
*placement
)
59 struct radeon_cs_reloc
*greloc
;
64 greloc
= (void *)(((u8
*)ctx
->reloc
) + reloc
* 4);
65 for (i
= 0; i
< ctx
->nbo
; i
++) {
66 if (ctx
->bo
[i
]->handle
== greloc
->handle
) {
67 placement
[0] = greloc
->read_domain
| greloc
->write_domain
;
68 placement
[1] = placement
[0];
74 void radeon_ctx_clear(struct radeon_ctx
*ctx
)
76 for (int i
= 0; i
< ctx
->nbo
; i
++) {
77 ctx
->bo
[i
] = radeon_bo_decref(ctx
->radeon
, ctx
->bo
[i
]);
79 ctx
->ndwords
= RADEON_CTX_MAX_PM4
;
85 int radeon_ctx_init(struct radeon_ctx
*ctx
, struct radeon
*radeon
)
89 memset(ctx
, 0, sizeof(struct radeon_ctx
));
90 ctx
->radeon
= radeon_incref(radeon
);
91 radeon_ctx_clear(ctx
);
92 ctx
->pm4
= malloc(RADEON_CTX_MAX_PM4
* 4);
93 if (ctx
->pm4
== NULL
) {
97 ctx
->reloc
= malloc(sizeof(struct radeon_cs_reloc
) * RADEON_CTX_MAX_PM4
);
98 if (ctx
->reloc
== NULL
) {
102 ctx
->bo
= malloc(sizeof(void *) * RADEON_CTX_MAX_PM4
);
103 if (ctx
->bo
== NULL
) {
104 radeon_ctx_fini(ctx
);
110 void radeon_ctx_fini(struct radeon_ctx
*ctx
)
117 for (i
= 0; i
< ctx
->nbo
; i
++) {
118 ctx
->bo
[i
] = radeon_bo_decref(ctx
->radeon
, ctx
->bo
[i
]);
120 ctx
->radeon
= radeon_decref(ctx
->radeon
);
124 memset(ctx
, 0, sizeof(struct radeon_ctx
));
127 static int radeon_ctx_state_bo(struct radeon_ctx
*ctx
, struct radeon_state
*state
)
134 for (i
= 0; i
< state
->nbo
; i
++) {
135 for (j
= 0; j
< ctx
->nbo
; j
++) {
136 if (state
->bo
[i
] == ctx
->bo
[j
])
140 radeon_bo_incref(ctx
->radeon
, state
->bo
[i
]);
141 r
= radeon_ctx_set_bo_new(ctx
, state
->bo
[i
]);
150 int radeon_ctx_submit(struct radeon_ctx
*ctx
)
152 struct drm_radeon_cs drmib
;
153 struct drm_radeon_cs_chunk chunks
[2];
154 uint64_t chunk_array
[2];
160 for (r
= 0; r
< ctx
->cdwords
; r
++) {
161 fprintf(stderr
, "0x%08X\n", ctx
->pm4
[r
]);
164 drmib
.num_chunks
= 2;
165 drmib
.chunks
= (uint64_t)(uintptr_t)chunk_array
;
166 chunks
[0].chunk_id
= RADEON_CHUNK_ID_IB
;
167 chunks
[0].length_dw
= ctx
->cdwords
;
168 chunks
[0].chunk_data
= (uint64_t)(uintptr_t)ctx
->pm4
;
169 chunks
[1].chunk_id
= RADEON_CHUNK_ID_RELOCS
;
170 chunks
[1].length_dw
= ctx
->nreloc
* sizeof(struct radeon_cs_reloc
) / 4;
171 chunks
[1].chunk_data
= (uint64_t)(uintptr_t)ctx
->reloc
;
172 chunk_array
[0] = (uint64_t)(uintptr_t)&chunks
[0];
173 chunk_array
[1] = (uint64_t)(uintptr_t)&chunks
[1];
175 r
= drmCommandWriteRead(ctx
->radeon
->fd
, DRM_RADEON_CS
, &drmib
,
176 sizeof(struct drm_radeon_cs
));
181 static int radeon_ctx_reloc(struct radeon_ctx
*ctx
, struct radeon_bo
*bo
,
182 unsigned id
, unsigned *placement
)
186 for (i
= 0; i
< ctx
->nreloc
; i
++) {
187 if (ctx
->reloc
[i
].handle
== bo
->handle
) {
188 ctx
->pm4
[id
] = i
* sizeof(struct radeon_cs_reloc
) / 4;
192 if (ctx
->nreloc
>= RADEON_CTX_MAX_PM4
) {
195 ctx
->reloc
[ctx
->nreloc
].handle
= bo
->handle
;
196 ctx
->reloc
[ctx
->nreloc
].read_domain
= placement
[0] | placement
[1];
197 ctx
->reloc
[ctx
->nreloc
].write_domain
= placement
[0] | placement
[1];
198 ctx
->reloc
[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 if (state
->cpm4
> ctx
->ndwords
) {
214 memcpy(&ctx
->pm4
[ctx
->cdwords
], state
->pm4
, state
->cpm4
* 4);
215 for (i
= 0; i
< state
->nreloc
; i
++) {
216 rid
= state
->reloc_pm4_id
[i
];
217 bid
= state
->reloc_bo_id
[i
];
218 cid
= ctx
->cdwords
+ rid
;
219 r
= radeon_ctx_reloc(ctx
, state
->bo
[bid
], cid
,
220 &state
->placement
[bid
* 2]);
222 fprintf(stderr
, "%s state %d failed to reloc\n", __func__
, state
->stype
->stype
);
226 ctx
->cdwords
+= state
->cpm4
;
227 ctx
->ndwords
-= state
->cpm4
;
231 int radeon_ctx_set_query_state(struct radeon_ctx
*ctx
, struct radeon_state
*state
)
235 /* !!! ONLY ACCEPT QUERY STATE HERE !!! */
236 r
= radeon_state_pm4(state
);
239 /* BEGIN/END query are balanced in the same cs so account for END
240 * END query when scheduling BEGIN query
242 switch (state
->stype
->stype
) {
243 case R600_STATE_QUERY_BEGIN
:
244 /* is there enough place for begin & end */
245 if ((state
->cpm4
* 2) > ctx
->ndwords
)
247 ctx
->ndwords
-= state
->cpm4
;
249 case R600_STATE_QUERY_END
:
250 ctx
->ndwords
+= state
->cpm4
;
255 return radeon_ctx_state_schedule(ctx
, state
);
258 int radeon_ctx_set_draw(struct radeon_ctx
*ctx
, struct radeon_draw
*draw
)
260 unsigned previous_cdwords
;
263 for (int i
= 0; i
< (ctx
->radeon
->nstate_per_shader
* R600_SHADER_MAX
); i
++) {
264 r
= radeon_ctx_state_bo(ctx
, draw
->state
[i
]);
268 previous_cdwords
= ctx
->cdwords
;
269 for (int i
= 0, id
= 0; i
< ctx
->radeon
->nstate_per_shader
; i
++) {
270 for (int j
= 0; j
< R600_SHADER_MAX
; j
++) {
271 id
= j
* ctx
->radeon
->nstate_per_shader
+ i
;
272 if (draw
->state
[id
]) {
273 r
= radeon_ctx_state_schedule(ctx
, draw
->state
[id
]);
275 ctx
->cdwords
= previous_cdwords
;
285 int radeon_ctx_pm4(struct radeon_ctx
*ctx
)
292 ctx
->pm4
= malloc(ctx
->draw_cpm4
* 4);
293 if (ctx
->pm4
== NULL
)
295 for (i
= 0, ctx
->id
= 0; i
< ctx
->nstate
; i
++) {
297 if (ctx
->id
!= ctx
->draw_cpm4
) {
298 fprintf(stderr
, "%s miss predicted pm4 size %d for %d\n",
299 __func__
, ctx
->draw_cpm4
, ctx
->id
);
302 ctx
->cpm4
= ctx
->draw_cpm4
;
307 void radeon_ctx_dump_bof(struct radeon_ctx
*ctx
, const char *file
)
309 bof_t
*bcs
, *blob
, *array
, *bo
, *size
, *handle
, *device_id
, *root
;
312 root
= device_id
= bcs
= blob
= array
= bo
= size
= handle
= NULL
;
316 device_id
= bof_int32(ctx
->radeon
->device
);
317 if (device_id
== NULL
)
319 if (bof_object_set(root
, "device_id", device_id
))
321 bof_decref(device_id
);
324 printf("%d relocs\n", ctx
->nreloc
);
325 blob
= bof_blob(ctx
->nreloc
* 16, ctx
->reloc
);
328 if (bof_object_set(root
, "reloc", blob
))
333 printf("%d pm4\n", ctx
->cdwords
);
334 blob
= bof_blob(ctx
->cdwords
* 4, ctx
->pm4
);
337 if (bof_object_set(root
, "pm4", blob
))
345 for (i
= 0; i
< ctx
->nbo
; i
++) {
349 size
= bof_int32(ctx
->bo
[i
]->size
);
352 if (bof_object_set(bo
, "size", size
))
356 handle
= bof_int32(ctx
->bo
[i
]->handle
);
359 if (bof_object_set(bo
, "handle", handle
))
363 radeon_bo_map(ctx
->radeon
, ctx
->bo
[i
]);
364 blob
= bof_blob(ctx
->bo
[i
]->size
, ctx
->bo
[i
]->data
);
365 radeon_bo_unmap(ctx
->radeon
, ctx
->bo
[i
]);
368 if (bof_object_set(bo
, "data", blob
))
372 if (bof_array_append(array
, bo
))
377 if (bof_object_set(root
, "bo", array
))
379 bof_dump_file(root
, file
);
380 printf("done dump\n");
387 bof_decref(device_id
);