2 * Copyright © 2008 Nicolai Haehnle
3 * Copyright © 2008 Dave Airlie
4 * Copyright © 2008 Jérôme Glisse
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
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 COPYRIGHT HOLDERS, AUTHORS AND/OR ITS 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.
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
29 * Aapo Tahkola <aet@rasterburn.org>
30 * Nicolai Haehnle <prefect_@gmx.net>
32 * Jérôme Glisse <glisse@freedesktop.org>
41 #include <sys/ioctl.h>
44 #include "radeon_drm.h"
45 #include "radeon_bo.h"
46 #include "radeon_bo_legacy.h"
47 #include "radeon_ioctl.h"
51 struct radeon_bo base
;
52 driTextureObject tobj_base
;
58 int got_dri_texture_obj
;
61 driTextureObject dri_texture_obj
;
63 struct bo_legacy
*next
, *prev
;
64 struct bo_legacy
*pnext
, *pprev
;
67 struct bo_manager_legacy
{
68 struct radeon_bo_manager base
;
70 unsigned nfree_handles
;
71 unsigned cfree_handles
;
74 struct bo_legacy pending_bos
;
76 uint32_t texture_offset
;
77 unsigned dma_alloc_size
;
79 driTextureObject texture_swapped
;
80 driTexHeap
*texture_heap
;
81 struct radeon_screen
*screen
;
82 unsigned *free_handles
;
85 static void bo_legacy_tobj_destroy(void *data
, driTextureObject
*t
)
87 struct bo_legacy
*bo_legacy
;
89 bo_legacy
= (struct bo_legacy
*)((char*)t
)-sizeof(struct radeon_bo
);
90 bo_legacy
->got_dri_texture_obj
= 0;
91 bo_legacy
->validated
= 0;
94 static int legacy_new_handle(struct bo_manager_legacy
*bom
, uint32_t *handle
)
99 if (bom
->nhandle
== 0xFFFFFFFF) {
102 if (bom
->cfree_handles
> 0) {
103 tmp
= bom
->free_handles
[--bom
->cfree_handles
];
104 while (!bom
->free_handles
[bom
->cfree_handles
- 1]) {
105 bom
->cfree_handles
--;
106 if (bom
->cfree_handles
<= 0) {
107 bom
->cfree_handles
= 0;
111 bom
->cfree_handles
= 0;
112 tmp
= bom
->nhandle
++;
119 static int legacy_free_handle(struct bo_manager_legacy
*bom
, uint32_t handle
)
126 if (handle
== (bom
->nhandle
- 1)) {
130 for (i
= bom
->cfree_handles
- 1; i
>= 0; i
--) {
131 if (bom
->free_handles
[i
] == (bom
->nhandle
- 1)) {
133 bom
->free_handles
[i
] = 0;
136 while (!bom
->free_handles
[bom
->cfree_handles
- 1]) {
137 bom
->cfree_handles
--;
138 if (bom
->cfree_handles
<= 0) {
139 bom
->cfree_handles
= 0;
144 if (bom
->cfree_handles
< bom
->nfree_handles
) {
145 bom
->free_handles
[bom
->cfree_handles
++] = handle
;
148 bom
->nfree_handles
+= 0x100;
149 handles
= (uint32_t*)realloc(bom
->free_handles
, bom
->nfree_handles
* 4);
150 if (handles
== NULL
) {
151 bom
->nfree_handles
-= 0x100;
154 bom
->free_handles
= handles
;
155 bom
->free_handles
[bom
->cfree_handles
++] = handle
;
159 static void legacy_get_current_age(struct bo_manager_legacy
*boml
)
161 drm_radeon_getparam_t gp
;
164 gp
.param
= RADEON_PARAM_LAST_CLEAR
;
165 gp
.value
= (int *)&boml
->current_age
;
166 r
= drmCommandWriteRead(boml
->base
.fd
, DRM_RADEON_GETPARAM
,
169 fprintf(stderr
, "%s: drmRadeonGetParam: %d\n", __FUNCTION__
, r
);
174 static int legacy_is_pending(struct radeon_bo
*bo
)
176 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bo
->bom
;
177 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
179 if (bo_legacy
->is_pending
<= 0) {
180 bo_legacy
->is_pending
= 0;
183 if (boml
->current_age
>= bo_legacy
->pending
) {
184 if (boml
->pending_bos
.pprev
== bo_legacy
) {
185 boml
->pending_bos
.pprev
= bo_legacy
->pprev
;
187 bo_legacy
->pprev
->pnext
= bo_legacy
->pnext
;
188 if (bo_legacy
->pnext
) {
189 bo_legacy
->pnext
->pprev
= bo_legacy
->pprev
;
191 while (bo_legacy
->is_pending
--) {
194 bo_legacy
->is_pending
= 0;
201 static int legacy_wait_pending(struct radeon_bo
*bo
)
203 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bo
->bom
;
204 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
206 if (!bo_legacy
->is_pending
) {
209 /* FIXME: lockup and userspace busy looping that's all the folks */
210 legacy_get_current_age(boml
);
211 while (legacy_is_pending(bo
)) {
213 legacy_get_current_age(boml
);
218 static void legacy_track_pending(struct bo_manager_legacy
*boml
)
220 struct bo_legacy
*bo_legacy
;
221 struct bo_legacy
*next
;
223 legacy_get_current_age(boml
);
224 bo_legacy
= boml
->pending_bos
.pnext
;
226 next
= bo_legacy
->pnext
;
227 if (legacy_is_pending(&(bo_legacy
->base
))) {
233 static struct bo_legacy
*bo_allocate(struct bo_manager_legacy
*boml
,
238 struct bo_legacy
*bo_legacy
;
240 bo_legacy
= (struct bo_legacy
*)calloc(1, sizeof(struct bo_legacy
));
241 if (bo_legacy
== NULL
) {
244 bo_legacy
->base
.bom
= (struct radeon_bo_manager
*)boml
;
245 bo_legacy
->base
.handle
= 0;
246 bo_legacy
->base
.size
= size
;
247 bo_legacy
->base
.alignment
= alignment
;
248 bo_legacy
->base
.flags
= flags
;
249 bo_legacy
->base
.ptr
= NULL
;
250 bo_legacy
->map_count
= 0;
251 bo_legacy
->next
= NULL
;
252 bo_legacy
->prev
= NULL
;
253 bo_legacy
->got_dri_texture_obj
= 0;
254 bo_legacy
->pnext
= NULL
;
255 bo_legacy
->pprev
= NULL
;
256 bo_legacy
->next
= boml
->bos
.next
;
257 bo_legacy
->prev
= &boml
->bos
;
258 boml
->bos
.next
= bo_legacy
;
259 if (bo_legacy
->next
) {
260 bo_legacy
->next
->prev
= bo_legacy
;
265 static int bo_dma_alloc(struct radeon_bo
*bo
)
267 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bo
->bom
;
268 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
269 drm_radeon_mem_alloc_t alloc
;
274 /* align size on 4Kb */
275 size
= (((4 * 1024) - 1) + bo
->size
) & ~((4 * 1024) - 1);
276 alloc
.region
= RADEON_MEM_REGION_GART
;
277 alloc
.alignment
= bo_legacy
->base
.alignment
;
279 alloc
.region_offset
= &base_offset
;
280 r
= drmCommandWriteRead(bo
->bom
->fd
,
285 /* ptr is set to NULL if dma allocation failed */
286 bo_legacy
->ptr
= NULL
;
290 bo_legacy
->ptr
= boml
->screen
->gartTextures
.map
+ base_offset
;
291 bo_legacy
->offset
= boml
->screen
->gart_texture_offset
+ base_offset
;
293 boml
->dma_alloc_size
+= size
;
297 static int bo_dma_free(struct radeon_bo
*bo
)
299 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bo
->bom
;
300 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
301 drm_radeon_mem_free_t memfree
;
304 if (bo_legacy
->ptr
== NULL
) {
305 /* ptr is set to NULL if dma allocation failed */
308 legacy_get_current_age(boml
);
309 memfree
.region
= RADEON_MEM_REGION_GART
;
310 memfree
.region_offset
= bo_legacy
->offset
;
311 memfree
.region_offset
-= boml
->screen
->gart_texture_offset
;
312 r
= drmCommandWrite(boml
->base
.fd
,
317 fprintf(stderr
, "Failed to free bo[%p] at %08x\n",
318 &bo_legacy
->base
, memfree
.region_offset
);
319 fprintf(stderr
, "ret = %s\n", strerror(-r
));
322 boml
->dma_alloc_size
-= bo_legacy
->base
.size
;
326 static void bo_free(struct bo_legacy
*bo_legacy
)
328 struct bo_manager_legacy
*boml
;
330 if (bo_legacy
== NULL
) {
333 boml
= (struct bo_manager_legacy
*)bo_legacy
->base
.bom
;
334 bo_legacy
->prev
->next
= bo_legacy
->next
;
335 if (bo_legacy
->next
) {
336 bo_legacy
->next
->prev
= bo_legacy
->prev
;
338 if (!bo_legacy
->static_bo
) {
339 legacy_free_handle(boml
, bo_legacy
->base
.handle
);
340 if (bo_legacy
->base
.flags
& RADEON_GEM_DOMAIN_GTT
) {
342 bo_dma_free(&bo_legacy
->base
);
344 /* free backing store */
345 free(bo_legacy
->ptr
);
348 memset(bo_legacy
, 0 , sizeof(struct bo_legacy
));
352 static struct radeon_bo
*bo_open(struct radeon_bo_manager
*bom
,
358 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bom
;
359 struct bo_legacy
*bo_legacy
;
363 bo_legacy
= boml
->bos
.next
;
365 if (bo_legacy
->base
.handle
== handle
) {
366 radeon_bo_ref(&(bo_legacy
->base
));
367 return (struct radeon_bo
*)bo_legacy
;
369 bo_legacy
= bo_legacy
->next
;
374 bo_legacy
= bo_allocate(boml
, size
, alignment
, flags
);
375 bo_legacy
->static_bo
= 0;
376 r
= legacy_new_handle(boml
, &bo_legacy
->base
.handle
);
381 if (bo_legacy
->base
.flags
& RADEON_GEM_DOMAIN_GTT
) {
382 legacy_track_pending(boml
);
384 r
= bo_dma_alloc(&(bo_legacy
->base
));
386 fprintf(stderr
, "Ran out of GART memory (for %d)!\n", size
);
387 fprintf(stderr
, "Please consider adjusting GARTSize option.\n");
393 bo_legacy
->ptr
= malloc(bo_legacy
->base
.size
);
394 if (bo_legacy
->ptr
== NULL
) {
399 radeon_bo_ref(&(bo_legacy
->base
));
400 return (struct radeon_bo
*)bo_legacy
;
403 static void bo_ref(struct radeon_bo
*bo
)
407 static void bo_unref(struct radeon_bo
*bo
)
409 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
412 bo_legacy
->prev
->next
= bo_legacy
->next
;
413 if (bo_legacy
->next
) {
414 bo_legacy
->next
->prev
= bo_legacy
->prev
;
416 if (!bo_legacy
->is_pending
) {
422 static int bo_map(struct radeon_bo
*bo
, int write
)
424 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bo
->bom
;
425 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
427 legacy_wait_pending(bo
);
428 bo_legacy
->validated
= 0;
429 bo_legacy
->dirty
= 1;
430 bo_legacy
->map_count
++;
431 bo
->ptr
= bo_legacy
->ptr
;
432 /* Read the first pixel in the frame buffer. This should
433 * be a noop, right? In fact without this conform fails as reading
434 * from the framebuffer sometimes produces old results -- the
435 * on-card read cache gets mixed up and doesn't notice that the
436 * framebuffer has been updated.
438 * Note that we should probably be reading some otherwise unused
439 * region of VRAM, otherwise we might get incorrect results when
440 * reading pixels from the top left of the screen.
442 * I found this problem on an R420 with glean's texCube test.
443 * Note that the R200 span code also *writes* the first pixel in the
444 * framebuffer, but I've found this to be unnecessary.
445 * -- Nicolai Hähnle, June 2008
449 volatile int *buf
= (int*)boml
->screen
->driScreen
->pFB
;
456 static int bo_unmap(struct radeon_bo
*bo
)
458 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
460 if (--bo_legacy
->map_count
> 0) {
467 static struct radeon_bo_funcs bo_legacy_funcs
= {
475 static int bo_vram_validate(struct radeon_bo
*bo
,
479 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bo
->bom
;
480 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
483 if (!bo_legacy
->got_dri_texture_obj
) {
484 make_empty_list(&bo_legacy
->dri_texture_obj
);
485 bo_legacy
->dri_texture_obj
.totalSize
= bo
->size
;
486 r
= driAllocateTexture(&boml
->texture_heap
, 1,
487 &bo_legacy
->dri_texture_obj
);
489 uint8_t *segfault
=NULL
;
490 fprintf(stderr
, "Ouch! vram_validate failed %d\n", r
);
494 bo_legacy
->offset
= boml
->texture_offset
+
495 bo_legacy
->dri_texture_obj
.memBlock
->ofs
;
496 bo_legacy
->got_dri_texture_obj
= 1;
497 bo_legacy
->dirty
= 1;
499 if (bo_legacy
->dirty
) {
500 /* Copy to VRAM using a blit.
501 * All memory is 4K aligned. We're using 1024 pixels wide blits.
503 drm_radeon_texture_t tex
;
504 drm_radeon_tex_image_t tmp
;
507 tex
.offset
= bo_legacy
->offset
;
509 assert(!(tex
.offset
& 1023));
513 if (bo
->size
< 4096) {
514 tmp
.width
= (bo
->size
+ 3) / 4;
518 tmp
.height
= (bo
->size
+ 4095) / 4096;
520 tmp
.data
= bo_legacy
->ptr
;
521 tex
.format
= RADEON_TXFORMAT_ARGB8888
;
522 tex
.width
= tmp
.width
;
523 tex
.height
= tmp
.height
;
524 tex
.pitch
= MAX2(tmp
.width
/ 16, 1);
526 ret
= drmCommandWriteRead(bo
->bom
->fd
,
529 sizeof(drm_radeon_texture_t
));
531 if (RADEON_DEBUG
& DEBUG_IOCTL
)
532 fprintf(stderr
, "DRM_RADEON_TEXTURE: again!\n");
535 } while (ret
== -EAGAIN
);
536 bo_legacy
->dirty
= 0;
541 int radeon_bo_legacy_validate(struct radeon_bo
*bo
,
545 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
548 if (bo_legacy
->map_count
) {
549 fprintf(stderr
, "bo(%p, %d) is mapped (%d) can't valide it.\n",
550 bo
, bo
->size
, bo_legacy
->map_count
);
553 if (bo_legacy
->static_bo
|| bo_legacy
->validated
) {
554 *soffset
= bo_legacy
->offset
;
555 *eoffset
= bo_legacy
->offset
+ bo
->size
;
558 if (!(bo
->flags
& RADEON_GEM_DOMAIN_GTT
)) {
559 r
= bo_vram_validate(bo
, soffset
, eoffset
);
564 *soffset
= bo_legacy
->offset
;
565 *eoffset
= bo_legacy
->offset
+ bo
->size
;
566 bo_legacy
->validated
= 1;
570 void radeon_bo_legacy_pending(struct radeon_bo
*bo
, uint32_t pending
)
572 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bo
->bom
;
573 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
575 bo_legacy
->pending
= pending
;
576 bo_legacy
->is_pending
+= 1;
577 /* add to pending list */
579 if (bo_legacy
->is_pending
> 1) {
582 bo_legacy
->pprev
= boml
->pending_bos
.pprev
;
583 bo_legacy
->pnext
= NULL
;
584 bo_legacy
->pprev
->pnext
= bo_legacy
;
585 boml
->pending_bos
.pprev
= bo_legacy
;
589 void radeon_bo_manager_legacy_shutdown(struct radeon_bo_manager
*bom
)
591 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bom
;
592 struct bo_legacy
*bo_legacy
;
597 bo_legacy
= boml
->bos
.next
;
599 struct bo_legacy
*next
;
601 next
= bo_legacy
->next
;
605 free(boml
->free_handles
);
609 struct radeon_bo_manager
*radeon_bo_manager_legacy(struct radeon_screen
*scrn
)
611 struct bo_manager_legacy
*bom
;
612 struct bo_legacy
*bo
;
615 bom
= (struct bo_manager_legacy
*)
616 calloc(1, sizeof(struct bo_manager_legacy
));
621 bom
->texture_heap
= driCreateTextureHeap(0,
625 RADEON_NR_TEX_REGIONS
,
626 (drmTextureRegionPtr
)scrn
->sarea
->tex_list
[0],
627 &scrn
->sarea
->tex_age
[0],
628 &bom
->texture_swapped
,
629 sizeof(struct bo_legacy
),
630 &bo_legacy_tobj_destroy
);
631 bom
->texture_offset
= scrn
->texOffset
[0];
633 bom
->base
.funcs
= &bo_legacy_funcs
;
634 bom
->base
.fd
= scrn
->driScreen
->fd
;
635 bom
->bos
.next
= NULL
;
636 bom
->bos
.prev
= NULL
;
637 bom
->pending_bos
.pprev
= &bom
->pending_bos
;
638 bom
->pending_bos
.pnext
= NULL
;
640 bom
->fb_location
= scrn
->fbLocation
;
642 bom
->cfree_handles
= 0;
643 bom
->nfree_handles
= 0x400;
644 bom
->free_handles
= (uint32_t*)malloc(bom
->nfree_handles
* 4);
645 if (bom
->free_handles
== NULL
) {
646 radeon_bo_manager_legacy_shutdown((struct radeon_bo_manager
*)bom
);
650 /* biggest framebuffer size */
653 bo
= bo_allocate(bom
, size
, 0, 0);
655 radeon_bo_manager_legacy_shutdown((struct radeon_bo_manager
*)bom
);
658 if (scrn
->sarea
->tiling_enabled
) {
659 bo
->base
.flags
= RADEON_BO_FLAGS_MACRO_TILE
;
662 bo
->offset
= bom
->screen
->frontOffset
+ bom
->fb_location
;
663 bo
->base
.handle
= bo
->offset
;
664 bo
->ptr
= scrn
->driScreen
->pFB
+ bom
->screen
->frontOffset
;
665 if (bo
->base
.handle
> bom
->nhandle
) {
666 bom
->nhandle
= bo
->base
.handle
+ 1;
669 bo
= bo_allocate(bom
, size
, 0, 0);
671 radeon_bo_manager_legacy_shutdown((struct radeon_bo_manager
*)bom
);
674 if (scrn
->sarea
->tiling_enabled
) {
675 bo
->base
.flags
= RADEON_BO_FLAGS_MACRO_TILE
;
678 bo
->offset
= bom
->screen
->backOffset
+ bom
->fb_location
;
679 bo
->base
.handle
= bo
->offset
;
680 bo
->ptr
= scrn
->driScreen
->pFB
+ bom
->screen
->backOffset
;
681 if (bo
->base
.handle
> bom
->nhandle
) {
682 bom
->nhandle
= bo
->base
.handle
+ 1;
685 bo
= bo_allocate(bom
, size
, 0, 0);
687 radeon_bo_manager_legacy_shutdown((struct radeon_bo_manager
*)bom
);
691 if (scrn
->sarea
->tiling_enabled
) {
692 bo
->base
.flags
= RADEON_BO_FLAGS_MACRO_TILE
;
695 bo
->offset
= bom
->screen
->depthOffset
+ bom
->fb_location
;
696 bo
->base
.handle
= bo
->offset
;
697 bo
->ptr
= scrn
->driScreen
->pFB
+ bom
->screen
->depthOffset
;
698 if (bo
->base
.handle
> bom
->nhandle
) {
699 bom
->nhandle
= bo
->base
.handle
+ 1;
701 return (struct radeon_bo_manager
*)bom
;
704 void radeon_bo_legacy_texture_age(struct radeon_bo_manager
*bom
)
706 struct bo_manager_legacy
*boml
= (struct bo_manager_legacy
*)bom
;
707 DRI_AGE_TEXTURES(boml
->texture_heap
);
710 unsigned radeon_bo_legacy_relocs_size(struct radeon_bo
*bo
)
712 struct bo_legacy
*bo_legacy
= (struct bo_legacy
*)bo
;
714 if (bo_legacy
->static_bo
|| (bo
->flags
& RADEON_GEM_DOMAIN_GTT
)) {