radeon_legacy_memory.c revision ad43ddac
1
2#ifdef HAVE_CONFIG_H
3#include "config.h"
4#endif
5
6/* Driver data structures */
7#include "radeon.h"
8
9/* Allocates memory, either by resizing the allocation pointed to by mem_struct,
10 * or by freeing mem_struct (if non-NULL) and allocating a new space.  The size
11 * is measured in bytes, and the offset from the beginning of card space is
12 * returned.
13 */
14uint32_t
15radeon_legacy_allocate_memory(ScrnInfoPtr pScrn,
16		       void **mem_struct,
17		       int size,
18		       int align,
19		       int domain)
20{
21    ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
22    RADEONInfoPtr info = RADEONPTR(pScrn);
23    uint32_t offset = 0;
24
25#ifdef XF86DRM_MODE
26    if (info->cs) {
27	struct radeon_bo *video_bo;
28
29	video_bo = radeon_bo_open(info->bufmgr, 0, size, RADEON_GPU_PAGE_SIZE, domain, 0);
30
31	*mem_struct = video_bo;
32
33	if (!video_bo)
34	    return 0;
35
36	return (uint32_t)-1;
37    }
38#endif
39#ifdef USE_EXA
40    if (info->useEXA) {
41	ExaOffscreenArea *area = *mem_struct;
42
43	if (area != NULL) {
44	    if (area->size >= size)
45		return area->offset;
46
47	    exaOffscreenFree(pScreen, area);
48	}
49
50	area = exaOffscreenAlloc(pScreen, size, align, TRUE,
51				 NULL, NULL);
52
53	*mem_struct = area;
54	if (area == NULL)
55	    return 0;
56	offset = area->offset;
57    }
58#endif /* USE_EXA */
59#ifdef USE_XAA
60    if (!info->useEXA) {
61	FBLinearPtr linear = *mem_struct;
62	int cpp = info->CurrentLayout.bitsPerPixel / 8;
63
64	/* XAA allocates in units of pixels at the screen bpp, so adjust size
65	 * appropriately.
66	 */
67	size = (size + cpp - 1) / cpp;
68	align = (align + cpp - 1) / cpp;
69
70	if (linear) {
71	    if(linear->size >= size)
72		return linear->offset * cpp;
73
74	    if(xf86ResizeOffscreenLinear(linear, size))
75		return linear->offset * cpp;
76
77	    xf86FreeOffscreenLinear(linear);
78	}
79
80	linear = xf86AllocateOffscreenLinear(pScreen, size, align,
81					     NULL, NULL, NULL);
82	*mem_struct = linear;
83
84	if (!linear) {
85	    int max_size;
86
87	    xf86QueryLargestOffscreenLinear(pScreen, &max_size, align,
88					    PRIORITY_EXTREME);
89
90	    if (max_size < size)
91		return 0;
92
93	    xf86PurgeUnlockedOffscreenAreas(pScreen);
94	    linear = xf86AllocateOffscreenLinear(pScreen, size, align,
95						 NULL, NULL, NULL);
96	    *mem_struct = linear;
97	    if (!linear)
98		return 0;
99	}
100	offset = linear->offset * cpp;
101    }
102#endif /* USE_XAA */
103
104    return offset;
105}
106
107void
108radeon_legacy_free_memory(ScrnInfoPtr pScrn,
109		   void *mem_struct)
110{
111    RADEONInfoPtr info = RADEONPTR(pScrn);
112
113#ifdef XF86DRM_MODE
114    if (info->cs) {
115        struct radeon_bo *bo = mem_struct;
116	radeon_bo_unref(bo);
117	return;
118    }
119#endif
120#ifdef USE_EXA
121    ScreenPtr pScreen = screenInfo.screens[pScrn->scrnIndex];
122
123    if (info->useEXA) {
124	ExaOffscreenArea *area = mem_struct;
125
126	if (area != NULL)
127	    exaOffscreenFree(pScreen, area);
128	area = NULL;
129    }
130#endif /* USE_EXA */
131#ifdef USE_XAA
132    if (!info->useEXA) {
133	FBLinearPtr linear = mem_struct;
134
135	if (linear != NULL)
136	    xf86FreeOffscreenLinear(linear);
137	linear = NULL;
138    }
139#endif /* USE_XAA */
140}
141