572c5df458e7e5d406c20dcb1c1a365956190193
[mesa.git] / src / gallium / winsys / drm / radeon / core / radeon_drm.c
1 /*
2 * Copyright © 2009 Corbin Simpson
3 * All Rights Reserved.
4 *
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:
12 *
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.
21 *
22 * The above copyright notice and this permission notice (including the
23 * next paragraph) shall be included in all copies or substantial portions
24 * of the Software.
25 */
26 /*
27 * Authors:
28 * Corbin Simpson <MostAwesomeDude@gmail.com>
29 * Joakim Sindholt <opensource@zhasha.com>
30 */
31
32 #include "softpipe/sp_winsys.h"
33
34 #include "radeon_drm.h"
35
36 /* Helper function to do the ioctls needed for setup and init. */
37 static void do_ioctls(int fd, struct radeon_winsys* winsys)
38 {
39 struct drm_radeon_gem_info gem_info = {0};
40 struct drm_radeon_info info = {0};
41 int target = 0;
42 int retval;
43 drmVersionPtr version;
44
45 info.value = (unsigned long)&target;
46
47 /* We do things in a specific order here.
48 *
49 * DRM version first. We need to be sure we're running on a KMS chipset.
50 * This is also for some features.
51 *
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
54 * non-Radeon card.
55 *
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.
59 *
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. */
63
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);
71 exit(1);
72 }
73
74 info.request = RADEON_INFO_DEVICE_ID;
75 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
76 if (retval) {
77 fprintf(stderr, "%s: Failed to get PCI ID, "
78 "error number %d\n", __FUNCTION__, retval);
79 exit(1);
80 }
81 winsys->pci_id = target;
82
83 info.request = RADEON_INFO_NUM_GB_PIPES;
84 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
85 if (retval) {
86 fprintf(stderr, "%s: Failed to get GB pipe count, "
87 "error number %d\n", __FUNCTION__, retval);
88 exit(1);
89 }
90 winsys->gb_pipes = target;
91
92 info.request = RADEON_INFO_NUM_Z_PIPES;
93 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
94 if (retval) {
95 fprintf(stderr, "%s: Failed to get Z pipe count, "
96 "error number %d\n", __FUNCTION__, retval);
97 exit(1);
98 }
99 winsys->z_pipes = target;
100
101 retval = drmCommandWriteRead(fd, DRM_RADEON_GEM_INFO,
102 &gem_info, sizeof(gem_info));
103 if (retval) {
104 fprintf(stderr, "%s: Failed to get MM info, error number %d\n",
105 __FUNCTION__, retval);
106 exit(1);
107 }
108 winsys->gart_size = gem_info.gart_size;
109 winsys->vram_size = gem_info.vram_size;
110
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);
119
120 drmFreeVersion(version);
121 }
122
123 /* Guess at whether this chipset should use r300g.
124 *
125 * I believe that this check is valid, but I haven't been exhaustive. */
126 static boolean is_r3xx(int pciid)
127 {
128 return (pciid > 0x3150) && (pciid < 0x796f);
129 }
130
131 /* Create a pipe_screen. */
132 struct pipe_screen* radeon_create_screen(struct drm_api* api,
133 int drmFB,
134 struct drm_create_screen_arg *arg)
135 {
136 struct radeon_winsys* rwinsys = radeon_pipe_winsys(drmFB);
137 do_ioctls(drmFB, rwinsys);
138
139 if (!is_r3xx(rwinsys->pci_id) ||
140 debug_get_bool_option("RADEON_SOFTPIPE", FALSE)) {
141 return softpipe_create_screen((struct pipe_winsys*)rwinsys);
142 } else {
143 radeon_setup_winsys(drmFB, rwinsys);
144 return r300_create_screen(rwinsys);
145 }
146 }
147
148 /* Create a pipe_context. */
149 struct pipe_context* radeon_create_context(struct drm_api* api,
150 struct pipe_screen* screen)
151 {
152 struct radeon_winsys* rwinsys = (struct radeon_winsys*)screen->winsys;
153
154 if (!is_r3xx(rwinsys->pci_id) ||
155 debug_get_bool_option("RADEON_SOFTPIPE", FALSE)) {
156 return softpipe_create(screen);
157 } else {
158 return r300_create_context(screen, rwinsys);
159 }
160 }
161
162 boolean radeon_buffer_from_texture(struct drm_api* api,
163 struct pipe_screen* screen,
164 struct pipe_texture* texture,
165 struct pipe_buffer** buffer,
166 unsigned* stride)
167 {
168 /* XXX fix this */
169 return r300_get_texture_buffer(screen, texture, buffer, stride);
170 }
171
172 /* Create a buffer from a handle. */
173 /* XXX what's up with name? */
174 struct pipe_buffer* radeon_buffer_from_handle(struct drm_api* api,
175 struct pipe_screen* screen,
176 const char* name,
177 unsigned handle)
178 {
179 struct radeon_bo_manager* bom =
180 ((struct radeon_winsys*)screen->winsys)->priv->bom;
181 struct radeon_pipe_buffer* radeon_buffer;
182 struct radeon_bo* bo = NULL;
183
184 bo = radeon_bo_open(bom, handle, 0, 0, 0, 0);
185 if (bo == NULL) {
186 return NULL;
187 }
188
189 radeon_buffer = CALLOC_STRUCT(radeon_pipe_buffer);
190 if (radeon_buffer == NULL) {
191 radeon_bo_unref(bo);
192 return NULL;
193 }
194
195 pipe_reference_init(&radeon_buffer->base.reference, 1);
196 radeon_buffer->base.screen = screen;
197 radeon_buffer->base.usage = PIPE_BUFFER_USAGE_PIXEL;
198 radeon_buffer->bo = bo;
199 return &radeon_buffer->base;
200 }
201
202 static struct pipe_texture*
203 radeon_texture_from_shared_handle(struct drm_api *api,
204 struct pipe_screen *screen,
205 struct pipe_texture *templ,
206 const char *name,
207 unsigned stride,
208 unsigned handle)
209 {
210 struct pipe_buffer *buffer;
211 struct pipe_texture *blanket;
212
213 buffer = radeon_buffer_from_handle(api, screen, name, handle);
214 if (!buffer) {
215 return NULL;
216 }
217
218 blanket = screen->texture_blanket(screen, templ, &stride, buffer);
219
220 pipe_buffer_reference(&buffer, NULL);
221
222 return blanket;
223 }
224
225 static boolean radeon_shared_handle_from_texture(struct drm_api *api,
226 struct pipe_screen *screen,
227 struct pipe_texture *texture,
228 unsigned *stride,
229 unsigned *handle)
230 {
231 int retval, fd;
232 struct drm_gem_flink flink;
233 struct radeon_pipe_buffer* radeon_buffer;
234 struct pipe_buffer *buffer = NULL;
235
236 if (!radeon_buffer_from_texture(api, screen, texture, &buffer, stride)) {
237 return FALSE;
238 }
239
240 radeon_buffer = (struct radeon_pipe_buffer*)buffer;
241 if (!radeon_buffer->flinked) {
242 fd = ((struct radeon_winsys*)screen->winsys)->priv->fd;
243
244 flink.handle = radeon_buffer->bo->handle;
245
246 retval = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
247 if (retval) {
248 debug_printf("radeon: DRM_IOCTL_GEM_FLINK failed, error %d\n",
249 retval);
250 return FALSE;
251 }
252
253 radeon_buffer->flink = flink.name;
254 radeon_buffer->flinked = TRUE;
255 }
256
257 *handle = radeon_buffer->flink;
258 return TRUE;
259 }
260
261 static boolean radeon_local_handle_from_texture(struct drm_api *api,
262 struct pipe_screen *screen,
263 struct pipe_texture *texture,
264 unsigned *stride,
265 unsigned *handle)
266 {
267 struct pipe_buffer *buffer = NULL;
268 if (!radeon_buffer_from_texture(api, screen, texture, &buffer, stride)) {
269 return FALSE;
270 }
271
272 *handle = ((struct radeon_pipe_buffer*)buffer)->bo->handle;
273
274 pipe_buffer_reference(&buffer, NULL);
275
276 return TRUE;
277 }
278
279 struct drm_api drm_api_hooks = {
280 .name = "radeon",
281 .create_screen = radeon_create_screen,
282 .create_context = radeon_create_context,
283 .texture_from_shared_handle = radeon_texture_from_shared_handle,
284 .shared_handle_from_texture = radeon_shared_handle_from_texture,
285 .local_handle_from_texture = radeon_local_handle_from_texture,
286 };
287
288 struct drm_api* drm_api_create()
289 {
290 #ifdef DEBUG
291 return trace_drm_create(&drm_api_hooks);
292 #else
293 return &drm_api_hooks;
294 #endif
295 }