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