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