gallium: Move xorg drivers to targets
[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 static struct radeon_libdrm_winsys *
45 radeon_winsys_create(int fd)
46 {
47 struct radeon_libdrm_winsys *rws;
48
49 rws = CALLOC_STRUCT(radeon_libdrm_winsys);
50 if (rws == NULL) {
51 return NULL;
52 }
53
54 rws->fd = fd;
55 return rws;
56 }
57
58 /* Helper function to do the ioctls needed for setup and init. */
59 static void do_ioctls(int fd, struct radeon_libdrm_winsys* winsys)
60 {
61 struct drm_radeon_gem_info gem_info = {0};
62 struct drm_radeon_info info = {0};
63 int target = 0;
64 int retval;
65 drmVersionPtr version;
66
67 info.value = (unsigned long)&target;
68
69 /* We do things in a specific order here.
70 *
71 * DRM version first. We need to be sure we're running on a KMS chipset.
72 * This is also for some features.
73 *
74 * Then, the PCI ID. This is essential and should return usable numbers
75 * for all Radeons. If this fails, we probably got handed an FD for some
76 * non-Radeon card.
77 *
78 * The GB and Z pipe requests should always succeed, but they might not
79 * return sensical values for all chipsets, but that's alright because
80 * the pipe drivers already know that.
81 *
82 * The GEM info is actually bogus on the kernel side, as well as our side
83 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
84 * we don't actually use the info for anything yet. */
85
86 version = drmGetVersion(fd);
87 if (version->version_major != 2) {
88 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
89 "only compatible with 2.x.x\n", __FUNCTION__,
90 version->version_major, version->version_minor,
91 version->version_patchlevel);
92 drmFreeVersion(version);
93 exit(1);
94 }
95
96 /* XXX Remove this ifdef when libdrm version 2.4.19 becomes mandatory. */
97 #ifdef RADEON_BO_FLAGS_MICRO_TILE_SQUARE
98 // Supported since 2.1.0.
99 winsys->squaretiling = version->version_major > 2 ||
100 version->version_minor >= 1;
101 #endif
102
103 info.request = RADEON_INFO_DEVICE_ID;
104 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
105 if (retval) {
106 fprintf(stderr, "%s: Failed to get PCI ID, "
107 "error number %d\n", __FUNCTION__, retval);
108 exit(1);
109 }
110 winsys->pci_id = target;
111
112 info.request = RADEON_INFO_NUM_GB_PIPES;
113 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
114 if (retval) {
115 fprintf(stderr, "%s: Failed to get GB pipe count, "
116 "error number %d\n", __FUNCTION__, retval);
117 exit(1);
118 }
119 winsys->gb_pipes = target;
120
121 info.request = RADEON_INFO_NUM_Z_PIPES;
122 retval = drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info));
123 if (retval) {
124 fprintf(stderr, "%s: Failed to get Z pipe count, "
125 "error number %d\n", __FUNCTION__, retval);
126 exit(1);
127 }
128 winsys->z_pipes = target;
129
130 retval = drmCommandWriteRead(fd, DRM_RADEON_GEM_INFO,
131 &gem_info, sizeof(gem_info));
132 if (retval) {
133 fprintf(stderr, "%s: Failed to get MM info, error number %d\n",
134 __FUNCTION__, retval);
135 exit(1);
136 }
137 winsys->gart_size = gem_info.gart_size;
138 winsys->vram_size = gem_info.vram_size;
139
140 debug_printf("radeon: Successfully grabbed chipset info from kernel!\n"
141 "radeon: DRM version: %d.%d.%d ID: 0x%04x GB: %d Z: %d\n"
142 "radeon: GART size: %d MB VRAM size: %d MB\n",
143 version->version_major, version->version_minor,
144 version->version_patchlevel, winsys->pci_id,
145 winsys->gb_pipes, winsys->z_pipes,
146 winsys->gart_size / 1024 / 1024,
147 winsys->vram_size / 1024 / 1024);
148
149 drmFreeVersion(version);
150 }
151
152 /* Create a pipe_screen. */
153 struct pipe_screen* radeon_create_screen(struct drm_api* api,
154 int drmFB,
155 struct drm_create_screen_arg *arg)
156 {
157 struct radeon_libdrm_winsys* rws;
158 boolean ret;
159
160 rws = radeon_winsys_create(drmFB);
161 if (!rws)
162 return NULL;
163
164 do_ioctls(drmFB, rws);
165
166 /* The state tracker can organize a softpipe fallback if no hw
167 * driver is found.
168 */
169 if (is_r3xx(rws->pci_id)) {
170 ret = radeon_setup_winsys(drmFB, rws);
171 if (ret == FALSE)
172 goto fail;
173 return r300_create_screen(&rws->base);
174 }
175
176 fail:
177 FREE(rws);
178 return NULL;
179 }
180
181 static void radeon_drm_api_destroy(struct drm_api *api)
182 {
183 return;
184 }
185
186 struct drm_api drm_api_hooks = {
187 .name = "radeon",
188 .driver_name = "radeon",
189 .create_screen = radeon_create_screen,
190 .destroy = radeon_drm_api_destroy,
191 };
192
193 struct drm_api* drm_api_create()
194 {
195 #ifdef DEBUG
196 return trace_drm_create(&drm_api_hooks);
197 #else
198 return &drm_api_hooks;
199 #endif
200 }