2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
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:
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
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. */
23 #include "radeon_r300.h"
25 static void radeon_r300_set_flush_cb(struct r300_winsys
*winsys
,
26 void (*flush_cb
)(void *),
29 struct radeon_winsys_priv
* priv
=
30 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
32 radeon_cs_space_set_flush(priv
->cs
, flush_cb
,
36 static boolean
radeon_r300_add_buffer(struct r300_winsys
* winsys
,
37 struct pipe_buffer
* pbuffer
,
41 struct radeon_winsys_priv
* priv
=
42 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
43 struct radeon_bo
* bo
= ((struct radeon_pipe_buffer
*)pbuffer
)->bo
;
45 radeon_cs_space_add_persistent_bo(priv
->cs
, bo
, rd
, wd
);
49 static boolean
radeon_r300_validate(struct r300_winsys
* winsys
)
51 struct radeon_winsys_priv
* priv
=
52 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
54 if (radeon_cs_space_check(priv
->cs
) < 0) {
58 /* Things are fine, we can proceed as normal. */
62 static boolean
radeon_r300_check_cs(struct r300_winsys
* winsys
, int size
)
64 /* XXX check size here, lazy ass! */
65 /* XXX also validate buffers */
69 static void radeon_r300_begin_cs(struct r300_winsys
* winsys
,
75 struct radeon_winsys_priv
* priv
=
76 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
78 radeon_cs_begin(priv
->cs
, size
, file
, function
, line
);
81 static void radeon_r300_write_cs_dword(struct r300_winsys
* winsys
,
84 struct radeon_winsys_priv
* priv
=
85 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
87 radeon_cs_write_dword(priv
->cs
, dword
);
90 static void radeon_r300_write_cs_reloc(struct r300_winsys
* winsys
,
91 struct pipe_buffer
* pbuffer
,
96 struct radeon_winsys_priv
* priv
=
97 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
100 retval
= radeon_cs_write_reloc(priv
->cs
,
101 ((struct radeon_pipe_buffer
*)pbuffer
)->bo
, rd
, wd
, flags
);
104 debug_printf("radeon: Relocation of %p (%d, %d, %d) failed!\n",
105 pbuffer
, rd
, wd
, flags
);
109 static void radeon_r300_reset_bos(struct r300_winsys
*winsys
)
111 struct radeon_winsys_priv
* priv
=
112 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
113 radeon_cs_space_reset_bos(priv
->cs
);
116 static void radeon_r300_end_cs(struct r300_winsys
* winsys
,
118 const char* function
,
121 struct radeon_winsys_priv
* priv
=
122 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
124 radeon_cs_end(priv
->cs
, file
, function
, line
);
127 static void radeon_r300_flush_cs(struct r300_winsys
* winsys
)
129 struct radeon_winsys_priv
* priv
=
130 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
134 retval
= radeon_cs_emit(priv
->cs
);
136 debug_printf("radeon: Bad CS, dumping...\n");
137 radeon_cs_print(priv
->cs
, stderr
);
141 * Someday, when we care about performance, we should really find a way
142 * to rotate between two or three CS objects so that the GPU can be
143 * spinning through one CS while another one is being filled. */
144 radeon_cs_erase(priv
->cs
);
147 /* Helper function to do the ioctls needed for setup and init. */
148 static void do_ioctls(struct r300_winsys
* winsys
, int fd
)
150 struct drm_radeon_gem_info gem_info
= {0};
151 struct drm_radeon_info info
= {0};
155 info
.value
= (unsigned long)&target
;
157 /* First, get the number of pixel pipes */
158 info
.request
= RADEON_INFO_NUM_GB_PIPES
;
159 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
161 fprintf(stderr
, "%s: Failed to get GB pipe count, "
162 "error number %d\n", __FUNCTION__
, retval
);
165 winsys
->gb_pipes
= target
;
168 info
.request
= RADEON_INFO_NUM_Z_PIPES
;
169 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
171 fprintf(stderr
, "%s: Failed to get GB pipe count, "
172 "error number %d\n", __FUNCTION__
, retval
);
175 winsys
->z_pipes
= target
;
177 /* Then, get PCI ID */
178 info
.request
= RADEON_INFO_DEVICE_ID
;
179 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
181 fprintf(stderr
, "%s: Failed to get PCI ID, "
182 "error number %d\n", __FUNCTION__
, retval
);
185 winsys
->pci_id
= target
;
187 /* Finally, retrieve MM info */
188 retval
= drmCommandWriteRead(fd
, DRM_RADEON_GEM_INFO
,
189 &gem_info
, sizeof(gem_info
));
191 fprintf(stderr
, "%s: Failed to get MM info, error number %d\n",
192 __FUNCTION__
, retval
);
195 winsys
->gart_size
= gem_info
.gart_size
;
197 winsys
->vram_size
= gem_info
.vram_visible
;
201 radeon_create_r300_winsys(int fd
, struct radeon_winsys
* old_winsys
)
203 struct r300_winsys
* winsys
= CALLOC_STRUCT(r300_winsys
);
204 struct radeon_winsys_priv
* priv
;
206 if (winsys
== NULL
) {
210 priv
= old_winsys
->priv
;
212 do_ioctls(winsys
, fd
);
214 priv
->csm
= radeon_cs_manager_gem_ctor(fd
);
216 priv
->cs
= radeon_cs_create(priv
->csm
, 1024 * 64 / 4);
217 radeon_cs_set_limit(priv
->cs
,
218 RADEON_GEM_DOMAIN_GTT
, winsys
->gart_size
);
219 radeon_cs_set_limit(priv
->cs
,
220 RADEON_GEM_DOMAIN_VRAM
, winsys
->vram_size
);
222 winsys
->add_buffer
= radeon_r300_add_buffer
;
223 winsys
->validate
= radeon_r300_validate
;
225 winsys
->check_cs
= radeon_r300_check_cs
;
226 winsys
->begin_cs
= radeon_r300_begin_cs
;
227 winsys
->write_cs_dword
= radeon_r300_write_cs_dword
;
228 winsys
->write_cs_reloc
= radeon_r300_write_cs_reloc
;
229 winsys
->end_cs
= radeon_r300_end_cs
;
230 winsys
->flush_cs
= radeon_r300_flush_cs
;
231 winsys
->reset_bos
= radeon_r300_reset_bos
;
232 winsys
->set_flush_cb
= radeon_r300_set_flush_cb
;
234 memcpy(winsys
, old_winsys
, sizeof(struct radeon_winsys
));