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