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>
34 #include "radeon_bocs_wrapper.h"
35 #include "radeon_common.h"
37 struct cs_manager_legacy
{
38 struct radeon_cs_manager base
;
39 struct radeon_context
*ctx
;
40 /* hack for scratch stuff */
42 uint32_t pending_count
;
47 struct cs_reloc_legacy
{
48 struct radeon_cs_reloc base
;
54 static struct radeon_cs
*cs_create(struct radeon_cs_manager
*csm
,
59 cs
= (struct radeon_cs
*)calloc(1, sizeof(struct radeon_cs
));
64 cs
->ndw
= (ndw
+ 0x3FF) & (~0x3FF);
65 cs
->packets
= (uint32_t*)malloc(4*cs
->ndw
);
66 if (cs
->packets
== NULL
) {
70 cs
->relocs_total_size
= 0;
74 static int cs_write_reloc(struct radeon_cs
*cs
,
77 uint32_t write_domain
,
80 struct cs_reloc_legacy
*relocs
;
83 relocs
= (struct cs_reloc_legacy
*)cs
->relocs
;
85 if ((read_domain
&& write_domain
) || (!read_domain
&& !write_domain
)) {
86 /* in one CS a bo can only be in read or write domain but not
87 * in read & write domain at the same sime
91 if (read_domain
== RADEON_GEM_DOMAIN_CPU
) {
94 if (write_domain
== RADEON_GEM_DOMAIN_CPU
) {
97 /* check if bo is already referenced */
98 for(i
= 0; i
< cs
->crelocs
; i
++) {
101 if (relocs
[i
].base
.bo
->handle
== bo
->handle
) {
102 /* Check domains must be in read or write. As we check already
103 * checked that in argument one of the read or write domain was
104 * set we only need to check that if previous reloc as the read
105 * domain set then the read_domain should also be set for this
108 if (relocs
[i
].base
.read_domain
&& !read_domain
) {
111 if (relocs
[i
].base
.write_domain
&& !write_domain
) {
114 relocs
[i
].base
.read_domain
|= read_domain
;
115 relocs
[i
].base
.write_domain
|= write_domain
;
117 relocs
[i
].cindices
++;
118 indices
= (uint32_t*)realloc(relocs
[i
].indices
,
119 relocs
[i
].cindices
* 4);
120 if (indices
== NULL
) {
121 relocs
[i
].cindices
-= 1;
124 relocs
[i
].indices
= indices
;
125 relocs
[i
].indices
[relocs
[i
].cindices
- 1] = cs
->cdw
- 1;
129 /* add bo to reloc */
130 relocs
= (struct cs_reloc_legacy
*)
132 sizeof(struct cs_reloc_legacy
) * (cs
->crelocs
+ 1));
133 if (relocs
== NULL
) {
137 relocs
[cs
->crelocs
].base
.bo
= bo
;
138 relocs
[cs
->crelocs
].base
.read_domain
= read_domain
;
139 relocs
[cs
->crelocs
].base
.write_domain
= write_domain
;
140 relocs
[cs
->crelocs
].base
.flags
= flags
;
141 relocs
[cs
->crelocs
].indices
= (uint32_t*)malloc(4);
142 if (relocs
[cs
->crelocs
].indices
== NULL
) {
145 relocs
[cs
->crelocs
].indices
[0] = cs
->cdw
- 1;
146 relocs
[cs
->crelocs
].cindices
= 1;
147 cs
->relocs_total_size
+= radeon_bo_legacy_relocs_size(bo
);
153 static int cs_begin(struct radeon_cs
*cs
,
160 fprintf(stderr
, "CS already in a section(%s,%s,%d)\n",
161 cs
->section_file
, cs
->section_func
, cs
->section_line
);
162 fprintf(stderr
, "CS can't start section(%s,%s,%d)\n",
167 cs
->section_ndw
= ndw
;
169 cs
->section_file
= file
;
170 cs
->section_func
= func
;
171 cs
->section_line
= line
;
174 if (cs
->cdw
+ ndw
> cs
->ndw
) {
176 int num
= (ndw
> 0x3FF) ? ndw
: 0x3FF;
178 tmp
= (cs
->cdw
+ 1 + num
) & (~num
);
179 ptr
= (uint32_t*)realloc(cs
->packets
, 4 * tmp
);
190 static int cs_end(struct radeon_cs
*cs
,
197 fprintf(stderr
, "CS no section to end at (%s,%s,%d)\n",
202 if (cs
->section_ndw
!= cs
->section_cdw
) {
203 fprintf(stderr
, "CS section size missmatch start at (%s,%s,%d) %d vs %d\n",
204 cs
->section_file
, cs
->section_func
, cs
->section_line
, cs
->section_ndw
, cs
->section_cdw
);
205 fprintf(stderr
, "CS section end at (%s,%s,%d)\n",
212 static int cs_process_relocs(struct radeon_cs
*cs
)
214 struct cs_manager_legacy
*csm
= (struct cs_manager_legacy
*)cs
->csm
;
215 struct cs_reloc_legacy
*relocs
;
218 csm
= (struct cs_manager_legacy
*)cs
->csm
;
219 relocs
= (struct cs_reloc_legacy
*)cs
->relocs
;
221 for (i
= 0; i
< cs
->crelocs
; i
++)
223 for (j
= 0; j
< relocs
[i
].cindices
; j
++)
225 uint32_t soffset
, eoffset
;
227 r
= radeon_bo_legacy_validate(relocs
[i
].base
.bo
,
235 fprintf(stderr
, "validated %p [0x%08X, 0x%08X]\n",
236 relocs
[i
].base
.bo
, soffset
, eoffset
);
239 cs
->packets
[relocs
[i
].indices
[j
]] += soffset
;
240 if (cs
->packets
[relocs
[i
].indices
[j
]] >= eoffset
)
242 /* radeon_bo_debug(relocs[i].base.bo, 12); */
243 fprintf(stderr
, "validated %p [0x%08X, 0x%08X]\n",
244 relocs
[i
].base
.bo
, soffset
, eoffset
);
245 fprintf(stderr
, "above end: %p 0x%08X 0x%08X\n",
247 cs
->packets
[relocs
[i
].indices
[j
]],
257 static int cs_set_age(struct radeon_cs
*cs
)
259 struct cs_manager_legacy
*csm
= (struct cs_manager_legacy
*)cs
->csm
;
260 struct cs_reloc_legacy
*relocs
;
263 relocs
= (struct cs_reloc_legacy
*)cs
->relocs
;
264 for (i
= 0; i
< cs
->crelocs
; i
++) {
265 radeon_bo_legacy_pending(relocs
[i
].base
.bo
, csm
->pending_age
);
266 radeon_bo_unref(relocs
[i
].base
.bo
);
271 static int cs_emit(struct radeon_cs
*cs
)
273 struct cs_manager_legacy
*csm
= (struct cs_manager_legacy
*)cs
->csm
;
274 drm_radeon_cmd_buffer_t cmd
;
275 drm_r300_cmd_header_t age
;
279 csm
->ctx
->vtbl
.emit_cs_header(cs
, csm
->ctx
);
281 /* append buffer age */
282 if ( IS_R300_CLASS(csm
->ctx
->radeonScreen
) )
284 age
.scratch
.cmd_type
= R300_CMD_SCRATCH
;
285 /* Scratch register 2 corresponds to what radeonGetAge polls */
286 csm
->pending_age
= 0;
287 csm
->pending_count
= 1;
288 ull
= (uint64_t) (intptr_t) &csm
->pending_age
;
290 age
.scratch
.n_bufs
= 1;
291 age
.scratch
.flags
= 0;
292 radeon_cs_write_dword(cs
, age
.u
);
293 radeon_cs_write_qword(cs
, ull
);
294 radeon_cs_write_dword(cs
, 0);
297 r
= cs_process_relocs(cs
);
302 cmd
.buf
= (char *)cs
->packets
;
303 cmd
.bufsz
= cs
->cdw
* 4;
304 if (csm
->ctx
->state
.scissor
.enabled
) {
305 cmd
.nbox
= csm
->ctx
->state
.scissor
.numClipRects
;
306 cmd
.boxes
= (drm_clip_rect_t
*) csm
->ctx
->state
.scissor
.pClipRects
;
308 cmd
.nbox
= csm
->ctx
->numClipRects
;
309 cmd
.boxes
= (drm_clip_rect_t
*) csm
->ctx
->pClipRects
;
314 r
= drmCommandWrite(cs
->csm
->fd
, DRM_RADEON_CMDBUF
, &cmd
, sizeof(cmd
));
318 if ((!IS_R300_CLASS(csm
->ctx
->radeonScreen
)) &&
319 (!IS_R600_CLASS(csm
->ctx
->radeonScreen
))) { /* +r6/r7 : No irq for r6/r7 yet. */
320 drm_radeon_irq_emit_t emit_cmd
;
321 emit_cmd
.irq_seq
= (int*)&csm
->pending_age
;
322 r
= drmCommandWrite(cs
->csm
->fd
, DRM_RADEON_IRQ_EMIT
, &emit_cmd
, sizeof(emit_cmd
));
329 cs
->csm
->read_used
= 0;
330 cs
->csm
->vram_write_used
= 0;
331 cs
->csm
->gart_write_used
= 0;
335 static void inline cs_free_reloc(void *relocs_p
, int crelocs
)
337 struct cs_reloc_legacy
*relocs
= relocs_p
;
341 for (i
= 0; i
< crelocs
; i
++)
342 free(relocs
[i
].indices
);
345 static int cs_destroy(struct radeon_cs
*cs
)
347 cs_free_reloc(cs
->relocs
, cs
->crelocs
);
354 static int cs_erase(struct radeon_cs
*cs
)
356 cs_free_reloc(cs
->relocs
, cs
->crelocs
);
358 cs
->relocs_total_size
= 0;
366 static int cs_need_flush(struct radeon_cs
*cs
)
368 /* this function used to flush when the BO usage got to
369 * a certain size, now the higher levels handle this better */
373 static void cs_print(struct radeon_cs
*cs
, FILE *file
)
377 static struct radeon_cs_funcs radeon_cs_legacy_funcs
= {
389 struct radeon_cs_manager
*radeon_cs_manager_legacy_ctor(struct radeon_context
*ctx
)
391 struct cs_manager_legacy
*csm
;
393 csm
= (struct cs_manager_legacy
*)
394 calloc(1, sizeof(struct cs_manager_legacy
));
398 csm
->base
.funcs
= &radeon_cs_legacy_funcs
;
399 csm
->base
.fd
= ctx
->dri
.fd
;
401 csm
->pending_age
= 1;
402 return (struct radeon_cs_manager
*)csm
;
405 void radeon_cs_manager_legacy_dtor(struct radeon_cs_manager
*csm
)