Revert "r600g: simplify states"
[mesa.git] / src / gallium / winsys / r600 / drm / radeon.c
1 /*
2 * Copyright © 2009 Jerome Glisse <glisse@freedesktop.org>
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
16 */
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdint.h>
20 #include <unistd.h>
21 #include <string.h>
22 #include <errno.h>
23 #include "xf86drm.h"
24 #include "radeon_priv.h"
25 #include "radeon_drm.h"
26
27 enum radeon_family radeon_get_family(struct radeon *radeon)
28 {
29 return radeon->family;
30 }
31
32 static int radeon_get_device(struct radeon *radeon)
33 {
34 struct drm_radeon_info info;
35 int r;
36
37 radeon->device = 0;
38 info.request = RADEON_INFO_DEVICE_ID;
39 info.value = (uintptr_t)&radeon->device;
40 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
41 sizeof(struct drm_radeon_info));
42 return r;
43 }
44
45 /* symbol missing drove me crazy hack to get symbol exported */
46 static void fake(void)
47 {
48 struct radeon_ctx *ctx;
49 struct radeon_draw *draw;
50
51 ctx = radeon_ctx(NULL);
52 draw = radeon_draw(NULL);
53 }
54
55 struct radeon *radeon_new(int fd, unsigned device)
56 {
57 struct radeon *radeon;
58 int r;
59
60 radeon = calloc(1, sizeof(*radeon));
61 if (radeon == NULL) {
62 fake();
63 return NULL;
64 }
65 radeon->fd = fd;
66 radeon->device = device;
67 radeon->refcount = 1;
68 if (fd >= 0) {
69 r = radeon_get_device(radeon);
70 if (r) {
71 fprintf(stderr, "Failed to get device id\n");
72 return radeon_decref(radeon);
73 }
74 }
75 radeon->family = radeon_family_from_device(radeon->device);
76 if (radeon->family == CHIP_UNKNOWN) {
77 fprintf(stderr, "Unknown chipset 0x%04X\n", radeon->device);
78 return radeon_decref(radeon);
79 }
80 switch (radeon->family) {
81 case CHIP_R600:
82 case CHIP_RV610:
83 case CHIP_RV630:
84 case CHIP_RV670:
85 case CHIP_RV620:
86 case CHIP_RV635:
87 case CHIP_RS780:
88 case CHIP_RS880:
89 case CHIP_RV770:
90 case CHIP_RV730:
91 case CHIP_RV710:
92 case CHIP_RV740:
93 if (r600_init(radeon)) {
94 return radeon_decref(radeon);
95 }
96 break;
97 case CHIP_R100:
98 case CHIP_RV100:
99 case CHIP_RS100:
100 case CHIP_RV200:
101 case CHIP_RS200:
102 case CHIP_R200:
103 case CHIP_RV250:
104 case CHIP_RS300:
105 case CHIP_RV280:
106 case CHIP_R300:
107 case CHIP_R350:
108 case CHIP_RV350:
109 case CHIP_RV380:
110 case CHIP_R420:
111 case CHIP_R423:
112 case CHIP_RV410:
113 case CHIP_RS400:
114 case CHIP_RS480:
115 case CHIP_RS600:
116 case CHIP_RS690:
117 case CHIP_RS740:
118 case CHIP_RV515:
119 case CHIP_R520:
120 case CHIP_RV530:
121 case CHIP_RV560:
122 case CHIP_RV570:
123 case CHIP_R580:
124 case CHIP_CEDAR:
125 case CHIP_REDWOOD:
126 case CHIP_JUNIPER:
127 case CHIP_CYPRESS:
128 case CHIP_HEMLOCK:
129 default:
130 fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
131 __func__, radeon->device);
132 break;
133 }
134 return radeon;
135 }
136
137 struct radeon *radeon_incref(struct radeon *radeon)
138 {
139 if (radeon == NULL)
140 return NULL;
141 radeon->refcount++;
142 return radeon;
143 }
144
145 struct radeon *radeon_decref(struct radeon *radeon)
146 {
147 if (radeon == NULL)
148 return NULL;
149 if (--radeon->refcount > 0) {
150 return NULL;
151 }
152 drmClose(radeon->fd);
153 free(radeon);
154 return NULL;
155 }
156
157 int radeon_reg_id(struct radeon *radeon, unsigned offset, unsigned *typeid, unsigned *stateid, unsigned *id)
158 {
159 unsigned i, j;
160
161 for (i = 0; i < radeon->ntype; i++) {
162 if (radeon->type[i].range_start) {
163 if (offset >= radeon->type[i].range_start && offset < radeon->type[i].range_end) {
164 *typeid = i;
165 j = offset - radeon->type[i].range_start;
166 j /= radeon->type[i].stride;
167 *stateid = radeon->type[i].id + j;
168 *id = (offset - radeon->type[i].range_start - radeon->type[i].stride * j) / 4;
169 return 0;
170 }
171 } else {
172 for (j = 0; j < radeon->type[i].nstates; j++) {
173 if (radeon->type[i].regs[j].offset == offset) {
174 *typeid = i;
175 *stateid = radeon->type[i].id;
176 *id = j;
177 return 0;
178 }
179 }
180 }
181 }
182 fprintf(stderr, "%s unknown register 0x%08X\n", __func__, offset);
183 return -EINVAL;
184 }
185
186 unsigned radeon_type_from_id(struct radeon *radeon, unsigned id)
187 {
188 unsigned i;
189
190 for (i = 0; i < radeon->ntype - 1; i++) {
191 if (radeon->type[i].id == id)
192 return i;
193 if (id > radeon->type[i].id && id < radeon->type[i + 1].id)
194 return i;
195 }
196 if (radeon->type[i].id == id)
197 return i;
198 return -1;
199 }