Merge remote branch 'origin/master' into pipe-video
[mesa.git] / src / gallium / winsys / radeon / drm / radeon_drm_winsys.c
1 /*
2 * Copyright © 2009 Corbin Simpson
3 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
25 * of the Software.
26 */
27 /*
28 * Authors:
29 * Corbin Simpson <MostAwesomeDude@gmail.com>
30 * Joakim Sindholt <opensource@zhasha.com>
31 * Marek Olšák <maraeo@gmail.com>
32 */
33
34 #include "radeon_drm_bo.h"
35 #include "radeon_drm_cs.h"
36 #include "radeon_drm_public.h"
37
38 #include "pipebuffer/pb_bufmgr.h"
39 #include "util/u_memory.h"
40
41 #include <xf86drm.h>
42 #include <stdio.h>
43
44 #ifndef RADEON_INFO_WANT_HYPERZ
45 #define RADEON_INFO_WANT_HYPERZ 7
46 #endif
47 #ifndef RADEON_INFO_WANT_CMASK
48 #define RADEON_INFO_WANT_CMASK 8
49 #endif
50
51 /* Enable/disable feature access. Return TRUE on success. */
52 static boolean radeon_set_fd_access(int fd, unsigned request, boolean enable)
53 {
54 struct drm_radeon_info info = {0};
55 unsigned value = enable ? 1 : 0;
56
57 info.value = (unsigned long)&value;
58 info.request = request;
59
60 if (drmCommandWriteRead(fd, DRM_RADEON_INFO, &info, sizeof(info)) != 0)
61 return FALSE;
62
63 if (enable && !value)
64 return FALSE;
65
66 return TRUE;
67 }
68
69 /* Helper function to do the ioctls needed for setup and init. */
70 static void do_ioctls(struct radeon_drm_winsys *winsys)
71 {
72 struct drm_radeon_gem_info gem_info = {0};
73 struct drm_radeon_info info = {0};
74 int target = 0;
75 int retval;
76 drmVersionPtr version;
77
78 info.value = (unsigned long)&target;
79
80 /* We do things in a specific order here.
81 *
82 * DRM version first. We need to be sure we're running on a KMS chipset.
83 * This is also for some features.
84 *
85 * Then, the PCI ID. This is essential and should return usable numbers
86 * for all Radeons. If this fails, we probably got handed an FD for some
87 * non-Radeon card.
88 *
89 * The GB and Z pipe requests should always succeed, but they might not
90 * return sensical values for all chipsets, but that's alright because
91 * the pipe drivers already know that.
92 *
93 * The GEM info is actually bogus on the kernel side, as well as our side
94 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
95 * we don't actually use the info for anything yet. */
96
97 version = drmGetVersion(winsys->fd);
98 if (version->version_major != 2 ||
99 version->version_minor < 3) {
100 fprintf(stderr, "%s: DRM version is %d.%d.%d but this driver is "
101 "only compatible with 2.3.x (kernel 2.6.34) and later.\n",
102 __FUNCTION__,
103 version->version_major,
104 version->version_minor,
105 version->version_patchlevel);
106 drmFreeVersion(version);
107 exit(1);
108 }
109
110 winsys->drm_major = version->version_major;
111 winsys->drm_minor = version->version_minor;
112 winsys->drm_patchlevel = version->version_patchlevel;
113
114 info.request = RADEON_INFO_DEVICE_ID;
115 retval = drmCommandWriteRead(winsys->fd, DRM_RADEON_INFO, &info, sizeof(info));
116 if (retval) {
117 fprintf(stderr, "%s: Failed to get PCI ID, "
118 "error number %d\n", __FUNCTION__, retval);
119 exit(1);
120 }
121 winsys->pci_id = target;
122
123 info.request = RADEON_INFO_NUM_GB_PIPES;
124 retval = drmCommandWriteRead(winsys->fd, DRM_RADEON_INFO, &info, sizeof(info));
125 if (retval) {
126 fprintf(stderr, "%s: Failed to get GB pipe count, "
127 "error number %d\n", __FUNCTION__, retval);
128 exit(1);
129 }
130 winsys->gb_pipes = target;
131
132 info.request = RADEON_INFO_NUM_Z_PIPES;
133 retval = drmCommandWriteRead(winsys->fd, DRM_RADEON_INFO, &info, sizeof(info));
134 if (retval) {
135 fprintf(stderr, "%s: Failed to get Z pipe count, "
136 "error number %d\n", __FUNCTION__, retval);
137 exit(1);
138 }
139 winsys->z_pipes = target;
140
141 if (debug_get_bool_option("RADEON_HYPERZ", FALSE)) {
142 winsys->hyperz = radeon_set_fd_access(winsys->fd,
143 RADEON_INFO_WANT_HYPERZ, TRUE);
144 }
145
146 if (debug_get_bool_option("RADEON_CMASK", FALSE)) {
147 winsys->aacompress = radeon_set_fd_access(winsys->fd,
148 RADEON_INFO_WANT_CMASK, TRUE);
149 }
150
151 retval = drmCommandWriteRead(winsys->fd, DRM_RADEON_GEM_INFO,
152 &gem_info, sizeof(gem_info));
153 if (retval) {
154 fprintf(stderr, "%s: Failed to get MM info, error number %d\n",
155 __FUNCTION__, retval);
156 exit(1);
157 }
158 winsys->gart_size = gem_info.gart_size;
159 winsys->vram_size = gem_info.vram_size;
160
161 drmFreeVersion(version);
162
163 winsys->num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
164 }
165
166 static void radeon_winsys_destroy(struct radeon_winsys *rws)
167 {
168 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys*)rws;
169
170 ws->cman->destroy(ws->cman);
171 ws->kman->destroy(ws->kman);
172 FREE(rws);
173 }
174
175 static uint32_t radeon_get_value(struct radeon_winsys *rws,
176 enum radeon_value_id id)
177 {
178 struct radeon_drm_winsys *ws = (struct radeon_drm_winsys *)rws;
179
180 switch(id) {
181 case RADEON_VID_PCI_ID:
182 return ws->pci_id;
183 case RADEON_VID_R300_GB_PIPES:
184 return ws->gb_pipes;
185 case RADEON_VID_R300_Z_PIPES:
186 return ws->z_pipes;
187 case RADEON_VID_GART_SIZE:
188 return ws->gart_size;
189 case RADEON_VID_VRAM_SIZE:
190 return ws->vram_size;
191 case RADEON_VID_DRM_MAJOR:
192 return ws->drm_major;
193 case RADEON_VID_DRM_MINOR:
194 return ws->drm_minor;
195 case RADEON_VID_DRM_PATCHLEVEL:
196 return ws->drm_patchlevel;
197 case RADEON_VID_DRM_2_6_0:
198 return ws->drm_major*100 + ws->drm_minor >= 206;
199 case RADEON_VID_DRM_2_8_0:
200 return ws->drm_major*100 + ws->drm_minor >= 208;
201 case RADEON_VID_CAN_HYPERZ:
202 return ws->hyperz;
203 case RADEON_VID_CAN_AACOMPRESS:
204 return ws->aacompress;
205 }
206 return 0;
207 }
208
209 struct radeon_winsys *radeon_drm_winsys_create(int fd)
210 {
211 struct radeon_drm_winsys *ws = CALLOC_STRUCT(radeon_drm_winsys);
212 if (!ws) {
213 return NULL;
214 }
215
216 ws->fd = fd;
217 do_ioctls(ws);
218
219 if (!is_r3xx(ws->pci_id)) {
220 goto fail;
221 }
222
223 /* Create managers. */
224 ws->kman = radeon_bomgr_create(ws);
225 if (!ws->kman)
226 goto fail;
227 ws->cman = pb_cache_manager_create(ws->kman, 1000000);
228 if (!ws->cman)
229 goto fail;
230
231 /* Set functions. */
232 ws->base.destroy = radeon_winsys_destroy;
233 ws->base.get_value = radeon_get_value;
234
235 radeon_bomgr_init_functions(ws);
236 radeon_drm_cs_init_functions(ws);
237
238 return &ws->base;
239
240 fail:
241 if (ws->cman)
242 ws->cman->destroy(ws->cman);
243 if (ws->kman)
244 ws->kman->destroy(ws->kman);
245 FREE(ws);
246 return NULL;
247 }