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