2 * Copyright © 2009 Corbin Simpson
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sub license, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
17 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
28 * Corbin Simpson <MostAwesomeDude@gmail.com>
29 * Joakim Sindholt <opensource@zhasha.com>
32 #include "softpipe/sp_winsys.h"
34 #include "radeon_drm.h"
36 /* Helper function to do the ioctls needed for setup and init. */
37 static void do_ioctls(int fd
, struct radeon_winsys
* winsys
)
39 struct drm_radeon_gem_info gem_info
= {0};
40 struct drm_radeon_info info
= {0};
43 drmVersionPtr version
;
45 info
.value
= (unsigned long)&target
;
47 /* We do things in a specific order here.
49 * DRM version first. We need to be sure we're running on a KMS chipset.
50 * This is also for some features.
52 * Then, the PCI ID. This is essential and should return usable numbers
53 * for all Radeons. If this fails, we probably got handed an FD for some
56 * The GB and Z pipe requests should always succeed, but they might not
57 * return sensical values for all chipsets, but that's alright because
58 * the pipe drivers already know that.
60 * The GEM info is actually bogus on the kernel side, as well as our side
61 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
62 * we don't actually use the info for anything yet. */
64 version
= drmGetVersion(fd
);
65 if (version
->version_major
!= 2) {
66 fprintf(stderr
, "%s: DRM version is %d.%d.%d but this driver is "
67 "only compatible with 2.x.x\n", __FUNCTION__
,
68 version
->version_major
, version
->version_minor
,
69 version
->version_patchlevel
);
70 drmFreeVersion(version
);
74 info
.request
= RADEON_INFO_DEVICE_ID
;
75 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
77 fprintf(stderr
, "%s: Failed to get PCI ID, "
78 "error number %d\n", __FUNCTION__
, retval
);
81 winsys
->pci_id
= target
;
83 info
.request
= RADEON_INFO_NUM_GB_PIPES
;
84 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
86 fprintf(stderr
, "%s: Failed to get GB pipe count, "
87 "error number %d\n", __FUNCTION__
, retval
);
90 winsys
->gb_pipes
= target
;
92 info
.request
= RADEON_INFO_NUM_Z_PIPES
;
93 retval
= drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
95 fprintf(stderr
, "%s: Failed to get Z pipe count, "
96 "error number %d\n", __FUNCTION__
, retval
);
99 winsys
->z_pipes
= target
;
101 retval
= drmCommandWriteRead(fd
, DRM_RADEON_GEM_INFO
,
102 &gem_info
, sizeof(gem_info
));
104 fprintf(stderr
, "%s: Failed to get MM info, error number %d\n",
105 __FUNCTION__
, retval
);
108 winsys
->gart_size
= gem_info
.gart_size
;
109 winsys
->vram_size
= gem_info
.vram_size
;
111 debug_printf("radeon: Successfully grabbed chipset info from kernel!\n"
112 "radeon: DRM version: %d.%d.%d ID: 0x%04x GB: %d Z: %d\n"
113 "radeon: GART size: %d MB VRAM size: %d MB\n",
114 version
->version_major
, version
->version_minor
,
115 version
->version_patchlevel
, winsys
->pci_id
,
116 winsys
->gb_pipes
, winsys
->z_pipes
,
117 winsys
->gart_size
/ 1024 / 1024,
118 winsys
->vram_size
/ 1024 / 1024);
120 drmFreeVersion(version
);
123 /* Create a pipe_screen. */
124 struct pipe_screen
* radeon_create_screen(struct drm_api
* api
,
126 struct drm_create_screen_arg
*arg
)
128 struct radeon_winsys
* rwinsys
= radeon_pipe_winsys(drmFB
);
129 do_ioctls(drmFB
, rwinsys
);
131 if (!is_r3xx(rwinsys
->pci_id
) ||
132 debug_get_bool_option("RADEON_SOFTPIPE", FALSE
)) {
133 return softpipe_create_screen((struct pipe_winsys
*)rwinsys
);
135 radeon_setup_winsys(drmFB
, rwinsys
);
136 return r300_create_screen(rwinsys
);
141 boolean
radeon_buffer_from_texture(struct drm_api
* api
,
142 struct pipe_screen
* screen
,
143 struct pipe_texture
* texture
,
144 struct pipe_buffer
** buffer
,
148 return r300_get_texture_buffer(screen
, texture
, buffer
, stride
);
151 /* Create a buffer from a handle. */
152 /* XXX what's up with name? */
153 struct pipe_buffer
* radeon_buffer_from_handle(struct drm_api
* api
,
154 struct pipe_screen
* screen
,
158 struct radeon_bo_manager
* bom
=
159 ((struct radeon_winsys
*)screen
->winsys
)->priv
->bom
;
160 struct radeon_pipe_buffer
* radeon_buffer
;
161 struct radeon_bo
* bo
= NULL
;
163 bo
= radeon_bo_open(bom
, handle
, 0, 0, 0, 0);
168 radeon_buffer
= CALLOC_STRUCT(radeon_pipe_buffer
);
169 if (radeon_buffer
== NULL
) {
174 pipe_reference_init(&radeon_buffer
->base
.reference
, 1);
175 radeon_buffer
->base
.screen
= screen
;
176 radeon_buffer
->base
.usage
= PIPE_BUFFER_USAGE_PIXEL
;
177 radeon_buffer
->bo
= bo
;
178 return &radeon_buffer
->base
;
181 static struct pipe_texture
*
182 radeon_texture_from_shared_handle(struct drm_api
*api
,
183 struct pipe_screen
*screen
,
184 struct pipe_texture
*templ
,
189 struct pipe_buffer
*buffer
;
190 struct pipe_texture
*blanket
;
192 buffer
= radeon_buffer_from_handle(api
, screen
, name
, handle
);
197 blanket
= screen
->texture_blanket(screen
, templ
, &stride
, buffer
);
199 pipe_buffer_reference(&buffer
, NULL
);
204 static boolean
radeon_shared_handle_from_texture(struct drm_api
*api
,
205 struct pipe_screen
*screen
,
206 struct pipe_texture
*texture
,
211 struct drm_gem_flink flink
;
212 struct radeon_pipe_buffer
* radeon_buffer
;
213 struct pipe_buffer
*buffer
= NULL
;
215 if (!radeon_buffer_from_texture(api
, screen
, texture
, &buffer
, stride
)) {
219 radeon_buffer
= (struct radeon_pipe_buffer
*)buffer
;
220 if (!radeon_buffer
->flinked
) {
221 fd
= ((struct radeon_winsys
*)screen
->winsys
)->priv
->fd
;
223 flink
.handle
= radeon_buffer
->bo
->handle
;
225 retval
= ioctl(fd
, DRM_IOCTL_GEM_FLINK
, &flink
);
227 debug_printf("radeon: DRM_IOCTL_GEM_FLINK failed, error %d\n",
232 radeon_buffer
->flink
= flink
.name
;
233 radeon_buffer
->flinked
= TRUE
;
236 *handle
= radeon_buffer
->flink
;
240 static boolean
radeon_local_handle_from_texture(struct drm_api
*api
,
241 struct pipe_screen
*screen
,
242 struct pipe_texture
*texture
,
246 struct pipe_buffer
*buffer
= NULL
;
247 if (!radeon_buffer_from_texture(api
, screen
, texture
, &buffer
, stride
)) {
251 *handle
= ((struct radeon_pipe_buffer
*)buffer
)->bo
->handle
;
253 pipe_buffer_reference(&buffer
, NULL
);
258 struct drm_api drm_api_hooks
= {
260 .driver_name
= "radeon",
261 .create_screen
= radeon_create_screen
,
262 .texture_from_shared_handle
= radeon_texture_from_shared_handle
,
263 .shared_handle_from_texture
= radeon_shared_handle_from_texture
,
264 .local_handle_from_texture
= radeon_local_handle_from_texture
,
267 struct drm_api
* drm_api_create()
270 return trace_drm_create(&drm_api_hooks
);
272 return &drm_api_hooks
;