r600g: remove useless switch statements
[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 "r600.h"
34 #include "r600_priv.h"
35 #include "r600_drm_public.h"
36 #include "xf86drm.h"
37 #include "radeon_drm.h"
38
39 #ifndef RADEON_INFO_TILING_CONFIG
40 #define RADEON_INFO_TILING_CONFIG 0x6
41 #endif
42
43 static struct radeon *radeon_new(int fd, unsigned device);
44
45 static int radeon_get_device(struct radeon *radeon)
46 {
47 struct drm_radeon_info info;
48 int r;
49
50 radeon->device = 0;
51 info.request = RADEON_INFO_DEVICE_ID;
52 info.value = (uintptr_t)&radeon->device;
53 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
54 sizeof(struct drm_radeon_info));
55 return r;
56 }
57
58 static int radeon_drm_get_tiling(struct radeon *radeon)
59 {
60 struct drm_radeon_info info;
61 int r;
62 uint32_t tiling_config;
63
64 info.request = RADEON_INFO_TILING_CONFIG;
65 info.value = (uintptr_t)&tiling_config;
66 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
67 sizeof(struct drm_radeon_info));
68
69 if (r)
70 return 0;
71
72 switch ((tiling_config & 0xe) >> 1) {
73 case 0:
74 radeon->tiling_info.num_channels = 1;
75 break;
76 case 1:
77 radeon->tiling_info.num_channels = 2;
78 break;
79 case 2:
80 radeon->tiling_info.num_channels = 4;
81 break;
82 case 3:
83 radeon->tiling_info.num_channels = 8;
84 break;
85 default:
86 return -EINVAL;
87 }
88
89 switch ((tiling_config & 0x30) >> 4) {
90 case 0:
91 radeon->tiling_info.num_banks = 4;
92 break;
93 case 1:
94 radeon->tiling_info.num_banks = 8;
95 break;
96 default:
97 return -EINVAL;
98
99 }
100 switch ((tiling_config & 0xc0) >> 6) {
101 case 0:
102 radeon->tiling_info.group_bytes = 256;
103 break;
104 case 1:
105 radeon->tiling_info.group_bytes = 512;
106 break;
107 default:
108 return -EINVAL;
109 }
110 return 0;
111 }
112
113 static struct radeon *radeon_new(int fd, unsigned device)
114 {
115 struct radeon *radeon;
116 int r;
117
118 radeon = calloc(1, sizeof(*radeon));
119 if (radeon == NULL) {
120 return NULL;
121 }
122 radeon->fd = fd;
123 radeon->device = device;
124 radeon->refcount = 1;
125 if (fd >= 0) {
126 r = radeon_get_device(radeon);
127 if (r) {
128 fprintf(stderr, "Failed to get device id\n");
129 return radeon_decref(radeon);
130 }
131 }
132 radeon->family = radeon_family_from_device(radeon->device);
133 if (radeon->family == CHIP_UNKNOWN) {
134 fprintf(stderr, "Unknown chipset 0x%04X\n", radeon->device);
135 return radeon_decref(radeon);
136 }
137 /* setup class */
138 switch (radeon->family) {
139 case CHIP_R600:
140 case CHIP_RV610:
141 case CHIP_RV630:
142 case CHIP_RV670:
143 case CHIP_RV620:
144 case CHIP_RV635:
145 case CHIP_RS780:
146 case CHIP_RS880:
147 radeon->chip_class = R600;
148 /* set default group bytes, overridden by tiling info ioctl */
149 radeon->tiling_info.group_bytes = 256;
150 break;
151 case CHIP_RV770:
152 case CHIP_RV730:
153 case CHIP_RV710:
154 case CHIP_RV740:
155 radeon->chip_class = R700;
156 /* set default group bytes, overridden by tiling info ioctl */
157 radeon->tiling_info.group_bytes = 256;
158 break;
159 case CHIP_CEDAR:
160 case CHIP_REDWOOD:
161 case CHIP_JUNIPER:
162 case CHIP_CYPRESS:
163 case CHIP_HEMLOCK:
164 case CHIP_PALM:
165 radeon->chip_class = EVERGREEN;
166 /* set default group bytes, overridden by tiling info ioctl */
167 radeon->tiling_info.group_bytes = 512;
168 break;
169 default:
170 fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
171 __func__, radeon->device);
172 break;
173 }
174
175 if (radeon->chip_class == R600 || radeon->chip_class == R700) {
176 if (radeon_drm_get_tiling(radeon))
177 return NULL;
178 }
179 radeon->bomgr = r600_bomgr_create(radeon, 1000000);
180 if (radeon->bomgr == NULL) {
181 return NULL;
182 }
183 return radeon;
184 }
185
186 struct radeon *r600_drm_winsys_create(int drmfd)
187 {
188 return radeon_new(drmfd, 0);
189 }
190
191 struct radeon *radeon_decref(struct radeon *radeon)
192 {
193 if (radeon == NULL)
194 return NULL;
195 if (--radeon->refcount > 0) {
196 return NULL;
197 }
198
199 if (radeon->bomgr)
200 r600_bomgr_destroy(radeon->bomgr);
201
202 if (radeon->fd >= 0)
203 drmClose(radeon->fd);
204
205 free(radeon);
206 return NULL;
207 }