2 * Copyright © 2008 Nicolai Haehnle
3 * Copyright © 2008 Jérôme Glisse
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
28 * Aapo Tahkola <aet@rasterburn.org>
29 * Nicolai Haehnle <prefect_@gmx.net>
30 * Jérôme Glisse <glisse@freedesktop.org>
36 #include "radeon_drm.h"
38 #include "radeon_bocs_wrapper.h"
39 #include "radeon_common.h"
40 #ifdef HAVE_LIBDRM_RADEON
41 #include "radeon_cs_int.h"
43 #include "radeon_cs_int_drm.h"
45 struct cs_manager_legacy
{
46 struct radeon_cs_manager base
;
47 struct radeon_context
*ctx
;
48 /* hack for scratch stuff */
50 uint32_t pending_count
;
55 struct cs_reloc_legacy
{
56 struct radeon_cs_reloc base
;
62 static struct radeon_cs_int
*cs_create(struct radeon_cs_manager
*csm
,
65 struct radeon_cs_int
*csi
;
67 csi
= (struct radeon_cs_int
*)calloc(1, sizeof(struct radeon_cs_int
));
72 csi
->ndw
= (ndw
+ 0x3FF) & (~0x3FF);
73 csi
->packets
= (uint32_t*)malloc(4*csi
->ndw
);
74 if (csi
->packets
== NULL
) {
78 csi
->relocs_total_size
= 0;
82 static int cs_write_reloc(struct radeon_cs_int
*cs
,
85 uint32_t write_domain
,
88 struct cs_reloc_legacy
*relocs
;
91 relocs
= (struct cs_reloc_legacy
*)cs
->relocs
;
93 if ((read_domain
&& write_domain
) || (!read_domain
&& !write_domain
)) {
94 /* in one CS a bo can only be in read or write domain but not
95 * in read & write domain at the same sime
99 if (read_domain
== RADEON_GEM_DOMAIN_CPU
) {
102 if (write_domain
== RADEON_GEM_DOMAIN_CPU
) {
105 /* check if bo is already referenced */
106 for(i
= 0; i
< cs
->crelocs
; i
++) {
109 if (relocs
[i
].base
.bo
->handle
== bo
->handle
) {
110 /* Check domains must be in read or write. As we check already
111 * checked that in argument one of the read or write domain was
112 * set we only need to check that if previous reloc as the read
113 * domain set then the read_domain should also be set for this
116 if (relocs
[i
].base
.read_domain
&& !read_domain
) {
119 if (relocs
[i
].base
.write_domain
&& !write_domain
) {
122 relocs
[i
].base
.read_domain
|= read_domain
;
123 relocs
[i
].base
.write_domain
|= write_domain
;
125 relocs
[i
].cindices
++;
126 indices
= (uint32_t*)realloc(relocs
[i
].indices
,
127 relocs
[i
].cindices
* 4);
128 if (indices
== NULL
) {
129 relocs
[i
].cindices
-= 1;
132 relocs
[i
].indices
= indices
;
133 relocs
[i
].indices
[relocs
[i
].cindices
- 1] = cs
->cdw
- 1;
137 /* add bo to reloc */
138 relocs
= (struct cs_reloc_legacy
*)
140 sizeof(struct cs_reloc_legacy
) * (cs
->crelocs
+ 1));
141 if (relocs
== NULL
) {
145 relocs
[cs
->crelocs
].base
.bo
= bo
;
146 relocs
[cs
->crelocs
].base
.read_domain
= read_domain
;
147 relocs
[cs
->crelocs
].base
.write_domain
= write_domain
;
148 relocs
[cs
->crelocs
].base
.flags
= flags
;
149 relocs
[cs
->crelocs
].indices
= (uint32_t*)malloc(4);
150 if (relocs
[cs
->crelocs
].indices
== NULL
) {
153 relocs
[cs
->crelocs
].indices
[0] = cs
->cdw
- 1;
154 relocs
[cs
->crelocs
].cindices
= 1;
155 cs
->relocs_total_size
+= radeon_bo_legacy_relocs_size(bo
);
161 static int cs_begin(struct radeon_cs_int
*cs
,
167 if (cs
->section_ndw
) {
168 fprintf(stderr
, "CS already in a section(%s,%s,%d)\n",
169 cs
->section_file
, cs
->section_func
, cs
->section_line
);
170 fprintf(stderr
, "CS can't start section(%s,%s,%d)\n",
174 cs
->section_ndw
= ndw
;
176 cs
->section_file
= file
;
177 cs
->section_func
= func
;
178 cs
->section_line
= line
;
181 if (cs
->cdw
+ ndw
> cs
->ndw
) {
183 int num
= (ndw
> 0x3FF) ? ndw
: 0x3FF;
185 tmp
= (cs
->cdw
+ ndw
+ 0x3ff) & (~0x3ff);
186 ptr
= (uint32_t*)realloc(cs
->packets
, 4 * tmp
);
197 static int cs_end(struct radeon_cs_int
*cs
,
203 if (!cs
->section_ndw
) {
204 fprintf(stderr
, "CS no section to end at (%s,%s,%d)\n",
208 if (cs
->section_ndw
!= cs
->section_cdw
) {
209 fprintf(stderr
, "CS section size missmatch start at (%s,%s,%d) %d vs %d\n",
210 cs
->section_file
, cs
->section_func
, cs
->section_line
, cs
->section_ndw
, cs
->section_cdw
);
211 fprintf(stderr
, "CS section end at (%s,%s,%d)\n",
220 static int cs_process_relocs(struct radeon_cs_int
*cs
)
222 struct cs_manager_legacy
*csm
= (struct cs_manager_legacy
*)cs
->csm
;
223 struct cs_reloc_legacy
*relocs
;
226 csm
= (struct cs_manager_legacy
*)cs
->csm
;
227 relocs
= (struct cs_reloc_legacy
*)cs
->relocs
;
229 for (i
= 0; i
< cs
->crelocs
; i
++)
231 for (j
= 0; j
< relocs
[i
].cindices
; j
++)
233 uint32_t soffset
, eoffset
;
235 r
= radeon_bo_legacy_validate(relocs
[i
].base
.bo
,
243 fprintf(stderr
, "validated %p [0x%08X, 0x%08X]\n",
244 relocs
[i
].base
.bo
, soffset
, eoffset
);
247 cs
->packets
[relocs
[i
].indices
[j
]] += soffset
;
248 if (cs
->packets
[relocs
[i
].indices
[j
]] >= eoffset
)
250 /* radeon_bo_debug(relocs[i].base.bo, 12); */
251 fprintf(stderr
, "validated %p [0x%08X, 0x%08X]\n",
252 relocs
[i
].base
.bo
, soffset
, eoffset
);
253 fprintf(stderr
, "above end: %p 0x%08X 0x%08X\n",
255 cs
->packets
[relocs
[i
].indices
[j
]],
265 static int cs_set_age(struct radeon_cs_int
*cs
)
267 struct cs_manager_legacy
*csm
= (struct cs_manager_legacy
*)cs
->csm
;
268 struct cs_reloc_legacy
*relocs
;
271 relocs
= (struct cs_reloc_legacy
*)cs
->relocs
;
272 for (i
= 0; i
< cs
->crelocs
; i
++) {
273 radeon_bo_legacy_pending(relocs
[i
].base
.bo
, csm
->pending_age
);
274 radeon_bo_unref(relocs
[i
].base
.bo
);
279 static int cs_emit(struct radeon_cs_int
*cs
)
281 struct cs_manager_legacy
*csm
= (struct cs_manager_legacy
*)cs
->csm
;
282 drm_radeon_cmd_buffer_t cmd
;
283 drm_r300_cmd_header_t age
;
287 csm
->ctx
->vtbl
.emit_cs_header((struct radeon_cs
*)cs
, csm
->ctx
);
289 /* append buffer age */
290 if ( IS_R300_CLASS(csm
->ctx
->radeonScreen
) )
292 age
.scratch
.cmd_type
= R300_CMD_SCRATCH
;
293 /* Scratch register 2 corresponds to what radeonGetAge polls */
294 csm
->pending_age
= 0;
295 csm
->pending_count
= 1;
296 ull
= (uint64_t) (intptr_t) &csm
->pending_age
;
298 age
.scratch
.n_bufs
= 1;
299 age
.scratch
.flags
= 0;
300 radeon_cs_write_dword((struct radeon_cs
*)cs
, age
.u
);
301 radeon_cs_write_qword((struct radeon_cs
*)cs
, ull
);
302 radeon_cs_write_dword((struct radeon_cs
*)cs
, 0);
305 r
= cs_process_relocs(cs
);
310 cmd
.buf
= (char *)cs
->packets
;
311 cmd
.bufsz
= cs
->cdw
* 4;
312 if (csm
->ctx
->state
.scissor
.enabled
) {
313 cmd
.nbox
= csm
->ctx
->state
.scissor
.numClipRects
;
314 cmd
.boxes
= (drm_clip_rect_t
*) csm
->ctx
->state
.scissor
.pClipRects
;
316 cmd
.nbox
= csm
->ctx
->numClipRects
;
317 cmd
.boxes
= (drm_clip_rect_t
*) csm
->ctx
->pClipRects
;
322 r
= drmCommandWrite(cs
->csm
->fd
, DRM_RADEON_CMDBUF
, &cmd
, sizeof(cmd
));
326 if ((!IS_R300_CLASS(csm
->ctx
->radeonScreen
)) &&
327 (!IS_R600_CLASS(csm
->ctx
->radeonScreen
))) { /* +r6/r7 : No irq for r6/r7 yet. */
328 drm_radeon_irq_emit_t emit_cmd
;
329 emit_cmd
.irq_seq
= (int*)&csm
->pending_age
;
330 r
= drmCommandWrite(cs
->csm
->fd
, DRM_RADEON_IRQ_EMIT
, &emit_cmd
, sizeof(emit_cmd
));
337 cs
->csm
->read_used
= 0;
338 cs
->csm
->vram_write_used
= 0;
339 cs
->csm
->gart_write_used
= 0;
343 static void inline cs_free_reloc(void *relocs_p
, int crelocs
)
345 struct cs_reloc_legacy
*relocs
= relocs_p
;
349 for (i
= 0; i
< crelocs
; i
++)
350 free(relocs
[i
].indices
);
353 static int cs_destroy(struct radeon_cs_int
*cs
)
355 cs_free_reloc(cs
->relocs
, cs
->crelocs
);
362 static int cs_erase(struct radeon_cs_int
*cs
)
364 cs_free_reloc(cs
->relocs
, cs
->crelocs
);
366 cs
->relocs_total_size
= 0;
374 static int cs_need_flush(struct radeon_cs_int
*cs
)
376 /* this function used to flush when the BO usage got to
377 * a certain size, now the higher levels handle this better */
381 static void cs_print(struct radeon_cs_int
*cs
, FILE *file
)
385 static struct radeon_cs_funcs radeon_cs_legacy_funcs
= {
397 struct radeon_cs_manager
*radeon_cs_manager_legacy_ctor(struct radeon_context
*ctx
)
399 struct cs_manager_legacy
*csm
;
401 csm
= (struct cs_manager_legacy
*)
402 calloc(1, sizeof(struct cs_manager_legacy
));
406 csm
->base
.funcs
= &radeon_cs_legacy_funcs
;
407 csm
->base
.fd
= ctx
->dri
.fd
;
409 csm
->pending_age
= 1;
410 return (struct radeon_cs_manager
*)csm
;
413 void radeon_cs_manager_legacy_dtor(struct radeon_cs_manager
*csm
)