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_add_buffer(struct r300_winsys
* winsys
,
26 struct pipe_buffer
* pbuffer
,
31 struct radeon_winsys_priv
* priv
=
32 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
33 struct radeon_cs_space_check
* sc
= priv
->sc
;
34 struct radeon_bo
* bo
= ((struct radeon_pipe_buffer
*)pbuffer
)->bo
;
36 /* Check to see if this BO is already in line for validation;
37 * find a slot for it otherwise. */
38 assert(priv
->bo_count
<= RADEON_MAX_BOS
);
39 for (i
= 0; i
< priv
->bo_count
; i
++) {
41 sc
[i
].read_domains
|= rd
;
42 sc
[i
].write_domain
|= wd
;
47 sc
[priv
->bo_count
].bo
= bo
;
48 sc
[priv
->bo_count
].read_domains
= rd
;
49 sc
[priv
->bo_count
].write_domain
= wd
;
53 static boolean
radeon_r300_validate(struct r300_winsys
* winsys
)
56 struct radeon_winsys_priv
* priv
=
57 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
58 struct radeon_cs_space_check
* sc
= priv
->sc
;
60 retval
= radeon_cs_space_check(priv
->cs
, sc
, priv
->bo_count
);
62 if (retval
== RADEON_CS_SPACE_OP_TO_BIG
) {
63 /* We might as well HCF, since this is not going to fit in the card,
65 /* XXX just drop it on the floor instead */
67 } else if (retval
== RADEON_CS_SPACE_FLUSH
) {
68 /* We must flush before more rendering can commence. */
72 /* Things are fine, we can proceed as normal. */
76 static boolean
radeon_r300_check_cs(struct r300_winsys
* winsys
, int size
)
78 /* XXX check size here, lazy ass! */
79 /* XXX also validate buffers */
83 static void radeon_r300_begin_cs(struct r300_winsys
* winsys
,
89 struct radeon_winsys_priv
* priv
=
90 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
92 radeon_cs_begin(priv
->cs
, size
, file
, function
, line
);
95 static void radeon_r300_write_cs_dword(struct r300_winsys
* winsys
,
98 struct radeon_winsys_priv
* priv
=
99 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
101 radeon_cs_write_dword(priv
->cs
, dword
);
104 static void radeon_r300_write_cs_reloc(struct r300_winsys
* winsys
,
105 struct pipe_buffer
* pbuffer
,
110 struct radeon_winsys_priv
* priv
=
111 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
113 radeon_cs_write_reloc(priv
->cs
,
114 ((struct radeon_pipe_buffer
*)pbuffer
)->bo
, rd
, wd
, flags
);
117 static void radeon_r300_end_cs(struct r300_winsys
* winsys
,
119 const char* function
,
122 struct radeon_winsys_priv
* priv
=
123 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
125 radeon_cs_end(priv
->cs
, file
, function
, line
);
128 static void radeon_r300_flush_cs(struct r300_winsys
* winsys
)
130 struct radeon_winsys_priv
* priv
=
131 (struct radeon_winsys_priv
*)winsys
->radeon_winsys
;
132 struct radeon_cs_space_check
* sc
= priv
->sc
;
136 retval
= radeon_cs_emit(priv
->cs
);
138 debug_printf("radeon: Bad CS, dumping...\n");
139 radeon_cs_print(priv
->cs
, stderr
);
141 radeon_cs_erase(priv
->cs
);
144 memset(sc
, 0, sizeof(struct radeon_cs_space_check
) * RADEON_MAX_BOS
);
148 /* Helper function to do the ioctls needed for setup and init. */
149 static void do_ioctls(struct r300_winsys
* winsys
, int fd
)
151 struct drm_radeon_gem_info gem_info
= {0};
152 drm_radeon_getparam_t gp
= {0};
153 struct drm_radeon_info info
= {0};
157 info
.value
= &target
;
160 /* First, get PCI ID */
161 info
.request
= RADEON_INFO_DEVICE_ID
;
162 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
164 fprintf(stderr
, "%s: New ioctl for PCI ID failed "
165 "(error number %d), trying classic ioctl...\n",
166 __FUNCTION__
, retval
);
167 gp
.param
= RADEON_PARAM_DEVICE_ID
;
168 retval
= drmCommandWriteRead(fd
, DRM_RADEON_GETPARAM
, &gp
,
171 fprintf(stderr
, "%s: Failed to get PCI ID, "
172 "error number %d\n", __FUNCTION__
, retval
);
176 winsys
->pci_id
= target
;
178 /* Then, retrieve MM info */
179 retval
= drmCommandWriteRead(fd
, DRM_RADEON_GEM_INFO
,
180 &gem_info
, sizeof(gem_info
));
182 fprintf(stderr
, "%s: Failed to get MM info, error number %d\n",
183 __FUNCTION__
, retval
);
186 winsys
->gart_size
= gem_info
.gart_size
;
188 winsys
->vram_size
= gem_info
.vram_visible
;
192 radeon_create_r300_winsys(int fd
, struct radeon_winsys
* old_winsys
)
194 struct r300_winsys
* winsys
= CALLOC_STRUCT(r300_winsys
);
195 struct radeon_winsys_priv
* priv
;
197 if (winsys
== NULL
) {
201 priv
= old_winsys
->priv
;
203 do_ioctls(winsys
, fd
);
205 priv
->csm
= radeon_cs_manager_gem_ctor(fd
);
207 priv
->cs
= radeon_cs_create(priv
->csm
, 1024 * 64 / 4);
208 radeon_cs_set_limit(priv
->cs
,
209 RADEON_GEM_DOMAIN_GTT
, winsys
->gart_size
);
210 radeon_cs_set_limit(priv
->cs
,
211 RADEON_GEM_DOMAIN_VRAM
, winsys
->vram_size
);
213 winsys
->add_buffer
= radeon_r300_add_buffer
;
214 winsys
->validate
= radeon_r300_validate
;
216 winsys
->check_cs
= radeon_r300_check_cs
;
217 winsys
->begin_cs
= radeon_r300_begin_cs
;
218 winsys
->write_cs_dword
= radeon_r300_write_cs_dword
;
219 winsys
->write_cs_reloc
= radeon_r300_write_cs_reloc
;
220 winsys
->end_cs
= radeon_r300_end_cs
;
221 winsys
->flush_cs
= radeon_r300_flush_cs
;
223 memcpy(winsys
, old_winsys
, sizeof(struct radeon_winsys
));