Merge branch 'mesa_7_5_branch' into mesa_7_6_branch
[mesa.git] / src / gallium / winsys / drm / radeon / core / radeon_r300.c
1 /*
2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 *
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:
10 *
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
13 * Software.
14 *
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. */
22
23 #include "radeon_r300.h"
24
25 static boolean radeon_r300_add_buffer(struct r300_winsys* winsys,
26 struct pipe_buffer* pbuffer,
27 uint32_t rd,
28 uint32_t wd)
29 {
30 struct radeon_winsys_priv* priv =
31 (struct radeon_winsys_priv*)winsys->radeon_winsys;
32 struct radeon_bo* bo = ((struct radeon_pipe_buffer*)pbuffer)->bo;
33
34 radeon_cs_space_add_persistent_bo(priv->cs, bo, rd, wd);
35 return TRUE;
36 }
37
38 static boolean radeon_r300_validate(struct r300_winsys* winsys)
39 {
40 struct radeon_winsys_priv* priv =
41 (struct radeon_winsys_priv*)winsys->radeon_winsys;
42
43 if (radeon_cs_space_check(priv->cs) < 0) {
44 return FALSE;
45 }
46
47 /* Things are fine, we can proceed as normal. */
48 return TRUE;
49 }
50
51 static boolean radeon_r300_check_cs(struct r300_winsys* winsys, int size)
52 {
53 /* XXX check size here, lazy ass! */
54 /* XXX also validate buffers */
55 return TRUE;
56 }
57
58 static void radeon_r300_begin_cs(struct r300_winsys* winsys,
59 int size,
60 const char* file,
61 const char* function,
62 int line)
63 {
64 struct radeon_winsys_priv* priv =
65 (struct radeon_winsys_priv*)winsys->radeon_winsys;
66
67 radeon_cs_begin(priv->cs, size, file, function, line);
68 }
69
70 static void radeon_r300_write_cs_dword(struct r300_winsys* winsys,
71 uint32_t dword)
72 {
73 struct radeon_winsys_priv* priv =
74 (struct radeon_winsys_priv*)winsys->radeon_winsys;
75
76 radeon_cs_write_dword(priv->cs, dword);
77 }
78
79 static void radeon_r300_write_cs_reloc(struct r300_winsys* winsys,
80 struct pipe_buffer* pbuffer,
81 uint32_t rd,
82 uint32_t wd,
83 uint32_t flags)
84 {
85 struct radeon_winsys_priv* priv =
86 (struct radeon_winsys_priv*)winsys->radeon_winsys;
87 int retval = 0;
88
89 retval = radeon_cs_write_reloc(priv->cs,
90 ((struct radeon_pipe_buffer*)pbuffer)->bo, rd, wd, flags);
91
92 if (retval) {
93 debug_printf("radeon: Relocation of %p (%d, %d, %d) failed!\n",
94 pbuffer, rd, wd, flags);
95 }
96 }
97
98 static void radeon_r300_end_cs(struct r300_winsys* winsys,
99 const char* file,
100 const char* function,
101 int line)
102 {
103 struct radeon_winsys_priv* priv =
104 (struct radeon_winsys_priv*)winsys->radeon_winsys;
105
106 radeon_cs_end(priv->cs, file, function, line);
107 }
108
109 static void radeon_r300_flush_cs(struct r300_winsys* winsys)
110 {
111 struct radeon_winsys_priv* priv =
112 (struct radeon_winsys_priv*)winsys->radeon_winsys;
113 int retval;
114
115 /* Emit the CS. */
116 retval = radeon_cs_emit(priv->cs);
117 if (retval) {
118 debug_printf("radeon: Bad CS, dumping...\n");
119 radeon_cs_print(priv->cs, stderr);
120 }
121
122 /* Clean out BOs. */
123 radeon_cs_space_reset_bos(priv->cs);
124
125 /* Reset CS.
126 * Someday, when we care about performance, we should really find a way
127 * to rotate between two or three CS objects so that the GPU can be
128 * spinning through one CS while another one is being filled. */
129 radeon_cs_erase(priv->cs);
130 }
131
132 /* Helper function to do the ioctls needed for setup and init. */
133 static void do_ioctls(struct r300_winsys* winsys, int fd)
134 {
135 struct drm_radeon_gem_info gem_info = {0};
136 struct drm_radeon_info info = {0};
137 int target = 0;
138 int retval;
139
140 info.value = &target;
141
142 /* First, get the number of pixel pipes */
143 info.request = RADEON_INFO_NUM_GB_PIPES;
144 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
145 if (retval) {
146 fprintf(stderr, "%s: Failed to get GB pipe count, "
147 "error number %d\n", __FUNCTION__, retval);
148 exit(1);
149 }
150 winsys->gb_pipes = target;
151
152 /* Then, get PCI ID */
153 info.request = RADEON_INFO_DEVICE_ID;
154 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
155 if (retval) {
156 fprintf(stderr, "%s: Failed to get PCI ID, "
157 "error number %d\n", __FUNCTION__, retval);
158 exit(1);
159 }
160 winsys->pci_id = target;
161
162 /* Finally, retrieve MM info */
163 retval = drmCommandWriteRead(fd, DRM_RADEON_GEM_INFO,
164 &gem_info, sizeof(gem_info));
165 if (retval) {
166 fprintf(stderr, "%s: Failed to get MM info, error number %d\n",
167 __FUNCTION__, retval);
168 exit(1);
169 }
170 winsys->gart_size = gem_info.gart_size;
171 /* XXX */
172 winsys->vram_size = gem_info.vram_visible;
173 }
174
175 struct r300_winsys*
176 radeon_create_r300_winsys(int fd, struct radeon_winsys* old_winsys)
177 {
178 struct r300_winsys* winsys = CALLOC_STRUCT(r300_winsys);
179 struct radeon_winsys_priv* priv;
180
181 if (winsys == NULL) {
182 return NULL;
183 }
184
185 priv = old_winsys->priv;
186
187 do_ioctls(winsys, fd);
188
189 priv->csm = radeon_cs_manager_gem_ctor(fd);
190
191 priv->cs = radeon_cs_create(priv->csm, 1024 * 64 / 4);
192 radeon_cs_set_limit(priv->cs,
193 RADEON_GEM_DOMAIN_GTT, winsys->gart_size);
194 radeon_cs_set_limit(priv->cs,
195 RADEON_GEM_DOMAIN_VRAM, winsys->vram_size);
196
197 winsys->add_buffer = radeon_r300_add_buffer;
198 winsys->validate = radeon_r300_validate;
199
200 winsys->check_cs = radeon_r300_check_cs;
201 winsys->begin_cs = radeon_r300_begin_cs;
202 winsys->write_cs_dword = radeon_r300_write_cs_dword;
203 winsys->write_cs_reloc = radeon_r300_write_cs_reloc;
204 winsys->end_cs = radeon_r300_end_cs;
205 winsys->flush_cs = radeon_r300_flush_cs;
206
207 memcpy(winsys, old_winsys, sizeof(struct radeon_winsys));
208
209 return winsys;
210 }