Merge branch 'llvm-cliptest-viewport'
[mesa.git] / src / gallium / winsys / r600 / drm / r600_drm.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Jerome Glisse
25 * Corbin Simpson <MostAwesomeDude@gmail.com>
26 * Joakim Sindholt <opensource@zhasha.com>
27 */
28 #include <stdio.h>
29 #include <errno.h>
30 #include <sys/ioctl.h>
31 #include "util/u_inlines.h"
32 #include "util/u_debug.h"
33 #include <pipebuffer/pb_bufmgr.h>
34 #include "r600.h"
35 #include "r600_priv.h"
36 #include "r600_drm_public.h"
37 #include "xf86drm.h"
38 #include "radeon_drm.h"
39
40 #ifndef RADEON_INFO_TILING_CONFIG
41 #define RADEON_INFO_TILING_CONFIG 0x6
42 #endif
43 static int radeon_get_device(struct radeon *radeon)
44 {
45 struct drm_radeon_info info;
46 int r;
47
48 radeon->device = 0;
49 info.request = RADEON_INFO_DEVICE_ID;
50 info.value = (uintptr_t)&radeon->device;
51 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
52 sizeof(struct drm_radeon_info));
53 return r;
54 }
55
56 static int radeon_drm_get_tiling(struct radeon *radeon)
57 {
58 struct drm_radeon_info info;
59 int r;
60 uint32_t tiling_config;
61
62 info.request = RADEON_INFO_TILING_CONFIG;
63 info.value = (uintptr_t)&tiling_config;
64 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
65 sizeof(struct drm_radeon_info));
66
67 if (r)
68 return r;
69
70 switch ((tiling_config & 0xe) >> 1) {
71 case 0:
72 radeon->tiling_info.num_channels = 1;
73 break;
74 case 1:
75 radeon->tiling_info.num_channels = 2;
76 break;
77 case 2:
78 radeon->tiling_info.num_channels = 4;
79 break;
80 case 3:
81 radeon->tiling_info.num_channels = 8;
82 break;
83 default:
84 return -EINVAL;
85 }
86
87 switch ((tiling_config & 0x30) >> 4) {
88 case 0:
89 radeon->tiling_info.num_banks = 4;
90 break;
91 case 1:
92 radeon->tiling_info.num_banks = 8;
93 break;
94 default:
95 return -EINVAL;
96
97 }
98 switch ((tiling_config & 0xc0) >> 6) {
99 case 0:
100 radeon->tiling_info.group_bytes = 256;
101 break;
102 case 1:
103 radeon->tiling_info.group_bytes = 512;
104 break;
105 default:
106 return -EINVAL;
107 }
108 return 0;
109 }
110
111 struct radeon *radeon_new(int fd, unsigned device)
112 {
113 struct radeon *radeon;
114 int r;
115
116 radeon = calloc(1, sizeof(*radeon));
117 if (radeon == NULL) {
118 return NULL;
119 }
120 radeon->fd = fd;
121 radeon->device = device;
122 radeon->refcount = 1;
123 if (fd >= 0) {
124 r = radeon_get_device(radeon);
125 if (r) {
126 fprintf(stderr, "Failed to get device id\n");
127 return radeon_decref(radeon);
128 }
129 }
130 radeon->family = radeon_family_from_device(radeon->device);
131 if (radeon->family == CHIP_UNKNOWN) {
132 fprintf(stderr, "Unknown chipset 0x%04X\n", radeon->device);
133 return radeon_decref(radeon);
134 }
135 switch (radeon->family) {
136 case CHIP_R600:
137 case CHIP_RV610:
138 case CHIP_RV630:
139 case CHIP_RV670:
140 case CHIP_RV620:
141 case CHIP_RV635:
142 case CHIP_RS780:
143 case CHIP_RS880:
144 case CHIP_RV770:
145 case CHIP_RV730:
146 case CHIP_RV710:
147 case CHIP_RV740:
148 case CHIP_CEDAR:
149 case CHIP_REDWOOD:
150 case CHIP_JUNIPER:
151 case CHIP_CYPRESS:
152 case CHIP_HEMLOCK:
153 break;
154 case CHIP_R100:
155 case CHIP_RV100:
156 case CHIP_RS100:
157 case CHIP_RV200:
158 case CHIP_RS200:
159 case CHIP_R200:
160 case CHIP_RV250:
161 case CHIP_RS300:
162 case CHIP_RV280:
163 case CHIP_R300:
164 case CHIP_R350:
165 case CHIP_RV350:
166 case CHIP_RV380:
167 case CHIP_R420:
168 case CHIP_R423:
169 case CHIP_RV410:
170 case CHIP_RS400:
171 case CHIP_RS480:
172 case CHIP_RS600:
173 case CHIP_RS690:
174 case CHIP_RS740:
175 case CHIP_RV515:
176 case CHIP_R520:
177 case CHIP_RV530:
178 case CHIP_RV560:
179 case CHIP_RV570:
180 case CHIP_R580:
181 default:
182 fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
183 __func__, radeon->device);
184 break;
185 }
186
187 /* setup class */
188 switch (radeon->family) {
189 case CHIP_R600:
190 case CHIP_RV610:
191 case CHIP_RV630:
192 case CHIP_RV670:
193 case CHIP_RV620:
194 case CHIP_RV635:
195 case CHIP_RS780:
196 case CHIP_RS880:
197 radeon->chip_class = R600;
198 break;
199 case CHIP_RV770:
200 case CHIP_RV730:
201 case CHIP_RV710:
202 case CHIP_RV740:
203 radeon->chip_class = R700;
204 break;
205 case CHIP_CEDAR:
206 case CHIP_REDWOOD:
207 case CHIP_JUNIPER:
208 case CHIP_CYPRESS:
209 case CHIP_HEMLOCK:
210 radeon->chip_class = EVERGREEN;
211 break;
212 default:
213 fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
214 __func__, radeon->device);
215 break;
216 }
217
218 if (radeon->chip_class == R600 || radeon->chip_class == R700) {
219 if (radeon_drm_get_tiling(radeon))
220 return NULL;
221 }
222 radeon->kman = radeon_bo_pbmgr_create(radeon);
223 if (!radeon->kman)
224 return NULL;
225 radeon->cman = pb_cache_manager_create(radeon->kman, 100000);
226 if (!radeon->cman)
227 return NULL;
228 return radeon;
229 }
230
231 struct radeon *r600_drm_winsys_create(int drmfd)
232 {
233 return radeon_new(drmfd, 0);
234 }
235
236 struct radeon *radeon_decref(struct radeon *radeon)
237 {
238 if (radeon == NULL)
239 return NULL;
240 if (--radeon->refcount > 0) {
241 return NULL;
242 }
243
244 if (radeon->cman)
245 radeon->cman->destroy(radeon->cman);
246
247 if (radeon->kman)
248 radeon->kman->destroy(radeon->kman);
249
250 if (radeon->fd >= 0)
251 drmClose(radeon->fd);
252
253 free(radeon);
254 return NULL;
255 }