Revert "mesa: Remove pointless comparison of unsigned integer with a negative constant."
[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 "radeon_drm.h"
33 #include "radeon_r300.h"
34 #include "radeon_buffer.h"
35
36 #include "r300_winsys.h"
37 #include "trace/tr_drm.h"
38
39 #include "util/u_memory.h"
40
41 #include "xf86drm.h"
42 #include <sys/ioctl.h>
43
44 /* Helper function to do the ioctls needed for setup and init. */
45 static void do_ioctls(int fd, struct radeon_winsys* winsys)
46 {
47 struct drm_radeon_gem_info gem_info = {0};
48 struct drm_radeon_info info = {0};
49 int target = 0;
50 int retval;
51 drmVersionPtr version;
52
53 info.value = (unsigned long)&target;
54
55 /* We do things in a specific order here.
56 *
57 * DRM version first. We need to be sure we're running on a KMS chipset.
58 * This is also for some features.
59 *
60 * Then, the PCI ID. This is essential and should return usable numbers
61 * for all Radeons. If this fails, we probably got handed an FD for some
62 * non-Radeon card.
63 *
64 * The GB and Z pipe requests should always succeed, but they might not
65 * return sensical values for all chipsets, but that's alright because
66 * the pipe drivers already know that.
67 *
68 * The GEM info is actually bogus on the kernel side, as well as our side
69 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
70 * we don't actually use the info for anything yet. */
71
72 version = drmGetVersion(fd);
73 if (version->version_major != 2) {
74 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
75 "only compatible with 2.x.x\n", __FUNCTION__,
76 version->version_major, version->version_minor,
77 version->version_patchlevel);
78 drmFreeVersion(version);
79 exit(1);
80 }
81
82 info.request = RADEON_INFO_DEVICE_ID;
83 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
84 if (retval) {
85 fprintf(stderr, "%s: Failed to get PCI ID, "
86 "error number %d\n", __FUNCTION__, retval);
87 exit(1);
88 }
89 winsys->pci_id = target;
90
91 info.request = RADEON_INFO_NUM_GB_PIPES;
92 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
93 if (retval) {
94 fprintf(stderr, "%s: Failed to get GB pipe count, "
95 "error number %d\n", __FUNCTION__, retval);
96 exit(1);
97 }
98 winsys->gb_pipes = target;
99
100 info.request = RADEON_INFO_NUM_Z_PIPES;
101 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
102 if (retval) {
103 fprintf(stderr, "%s: Failed to get Z pipe count, "
104 "error number %d\n", __FUNCTION__, retval);
105 exit(1);
106 }
107 winsys->z_pipes = target;
108
109 retval = drmCommandWriteRead(fd, DRM_RADEON_GEM_INFO,
110 &gem_info, sizeof(gem_info));
111 if (retval) {
112 fprintf(stderr, "%s: Failed to get MM info, error number %d\n",
113 __FUNCTION__, retval);
114 exit(1);
115 }
116 winsys->gart_size = gem_info.gart_size;
117 winsys->vram_size = gem_info.vram_size;
118
119 debug_printf("radeon: Successfully grabbed chipset info from kernel!\n"
120 "radeon: DRM version: %d.%d.%d ID: 0x%04x GB: %d Z: %d\n"
121 "radeon: GART size: %d MB VRAM size: %d MB\n",
122 version->version_major, version->version_minor,
123 version->version_patchlevel, winsys->pci_id,
124 winsys->gb_pipes, winsys->z_pipes,
125 winsys->gart_size / 1024 / 1024,
126 winsys->vram_size / 1024 / 1024);
127
128 drmFreeVersion(version);
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 /* The state tracker can organize a softpipe fallback if no hw
140 * driver is found.
141 */
142 if (is_r3xx(rwinsys->pci_id)) {
143 radeon_setup_winsys(drmFB, rwinsys);
144 return r300_create_screen(rwinsys);
145 } else {
146 FREE(rwinsys);
147 return NULL;
148 }
149 }
150
151
152 boolean radeon_buffer_from_texture(struct drm_api* api,
153 struct pipe_screen* screen,
154 struct pipe_texture* texture,
155 struct pipe_buffer** buffer,
156 unsigned* stride)
157 {
158 /* XXX fix this */
159 return r300_get_texture_buffer(screen, texture, buffer, stride);
160 }
161
162 /* Create a buffer from a handle. */
163 /* XXX what's up with name? */
164 struct pipe_buffer* radeon_buffer_from_handle(struct drm_api* api,
165 struct pipe_screen* screen,
166 const char* name,
167 unsigned handle)
168 {
169 struct radeon_bo_manager* bom =
170 ((struct radeon_winsys*)screen->winsys)->priv->bom;
171 struct radeon_pipe_buffer* radeon_buffer;
172 struct radeon_bo* bo = NULL;
173
174 bo = radeon_bo_open(bom, handle, 0, 0, 0, 0);
175 if (bo == NULL) {
176 return NULL;
177 }
178
179 radeon_buffer = CALLOC_STRUCT(radeon_pipe_buffer);
180 if (radeon_buffer == NULL) {
181 radeon_bo_unref(bo);
182 return NULL;
183 }
184
185 pipe_reference_init(&radeon_buffer->base.reference, 1);
186 radeon_buffer->base.screen = screen;
187 radeon_buffer->base.usage = PIPE_BUFFER_USAGE_PIXEL;
188 radeon_buffer->bo = bo;
189 return &radeon_buffer->base;
190 }
191
192 static struct pipe_texture*
193 radeon_texture_from_shared_handle(struct drm_api *api,
194 struct pipe_screen *screen,
195 struct pipe_texture *templ,
196 const char *name,
197 unsigned stride,
198 unsigned handle)
199 {
200 struct pipe_buffer *buffer;
201 struct pipe_texture *blanket;
202
203 buffer = radeon_buffer_from_handle(api, screen, name, handle);
204 if (!buffer) {
205 return NULL;
206 }
207
208 blanket = screen->texture_blanket(screen, templ, &stride, buffer);
209
210 pipe_buffer_reference(&buffer, NULL);
211
212 return blanket;
213 }
214
215 static boolean radeon_shared_handle_from_texture(struct drm_api *api,
216 struct pipe_screen *screen,
217 struct pipe_texture *texture,
218 unsigned *stride,
219 unsigned *handle)
220 {
221 int retval, fd;
222 struct drm_gem_flink flink;
223 struct radeon_pipe_buffer* radeon_buffer;
224 struct pipe_buffer *buffer = NULL;
225
226 if (!radeon_buffer_from_texture(api, screen, texture, &buffer, stride)) {
227 return FALSE;
228 }
229
230 radeon_buffer = (struct radeon_pipe_buffer*)buffer;
231 if (!radeon_buffer->flinked) {
232 fd = ((struct radeon_winsys*)screen->winsys)->priv->fd;
233
234 flink.handle = radeon_buffer->bo->handle;
235
236 retval = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
237 if (retval) {
238 debug_printf("radeon: DRM_IOCTL_GEM_FLINK failed, error %d\n",
239 retval);
240 return FALSE;
241 }
242
243 radeon_buffer->flink = flink.name;
244 radeon_buffer->flinked = TRUE;
245 }
246
247 *handle = radeon_buffer->flink;
248 return TRUE;
249 }
250
251 static boolean radeon_local_handle_from_texture(struct drm_api *api,
252 struct pipe_screen *screen,
253 struct pipe_texture *texture,
254 unsigned *stride,
255 unsigned *handle)
256 {
257 struct pipe_buffer *buffer = NULL;
258 if (!radeon_buffer_from_texture(api, screen, texture, &buffer, stride)) {
259 return FALSE;
260 }
261
262 *handle = ((struct radeon_pipe_buffer*)buffer)->bo->handle;
263
264 pipe_buffer_reference(&buffer, NULL);
265
266 return TRUE;
267 }
268
269 static void radeon_drm_api_destroy(struct drm_api *api)
270 {
271 return;
272 }
273
274 struct drm_api drm_api_hooks = {
275 .name = "radeon",
276 .driver_name = "radeon",
277 .create_screen = radeon_create_screen,
278 .texture_from_shared_handle = radeon_texture_from_shared_handle,
279 .shared_handle_from_texture = radeon_shared_handle_from_texture,
280 .local_handle_from_texture = radeon_local_handle_from_texture,
281 .destroy = radeon_drm_api_destroy,
282 };
283
284 struct drm_api* drm_api_create()
285 {
286 #ifdef DEBUG
287 return trace_drm_create(&drm_api_hooks);
288 #else
289 return &drm_api_hooks;
290 #endif
291 }