7e6566980640c28db5afdcec37a3de142db1fd08
[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 #include "r600d.h"
27
28 enum radeon_family radeon_get_family(struct radeon *radeon)
29 {
30 return radeon->family;
31 }
32
33 static int radeon_get_device(struct radeon *radeon)
34 {
35 struct drm_radeon_info info;
36 int r;
37
38 radeon->device = 0;
39 info.request = RADEON_INFO_DEVICE_ID;
40 info.value = (uintptr_t)&radeon->device;
41 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_INFO, &info,
42 sizeof(struct drm_radeon_info));
43 return r;
44 }
45
46 /* symbol missing drove me crazy hack to get symbol exported */
47 static void fake(void)
48 {
49 struct radeon_ctx *ctx;
50 struct radeon_draw *draw;
51
52 ctx = radeon_ctx(NULL);
53 draw = radeon_draw(NULL);
54 }
55
56 struct radeon *radeon_new(int fd, unsigned device)
57 {
58 struct radeon *radeon;
59 int r;
60
61 radeon = calloc(1, sizeof(*radeon));
62 if (radeon == NULL) {
63 fake();
64 return NULL;
65 }
66 radeon->fd = fd;
67 radeon->device = device;
68 radeon->refcount = 1;
69 if (fd >= 0) {
70 r = radeon_get_device(radeon);
71 if (r) {
72 fprintf(stderr, "Failed to get device id\n");
73 return radeon_decref(radeon);
74 }
75 }
76 radeon->family = radeon_family_from_device(radeon->device);
77 if (radeon->family == CHIP_UNKNOWN) {
78 fprintf(stderr, "Unknown chipset 0x%04X\n", radeon->device);
79 return radeon_decref(radeon);
80 }
81 switch (radeon->family) {
82 case CHIP_R600:
83 case CHIP_RV610:
84 case CHIP_RV630:
85 case CHIP_RV670:
86 case CHIP_RV620:
87 case CHIP_RV635:
88 case CHIP_RS780:
89 case CHIP_RS880:
90 case CHIP_RV770:
91 case CHIP_RV730:
92 case CHIP_RV710:
93 case CHIP_RV740:
94 if (r600_init(radeon)) {
95 return radeon_decref(radeon);
96 }
97 break;
98 case CHIP_R100:
99 case CHIP_RV100:
100 case CHIP_RS100:
101 case CHIP_RV200:
102 case CHIP_RS200:
103 case CHIP_R200:
104 case CHIP_RV250:
105 case CHIP_RS300:
106 case CHIP_RV280:
107 case CHIP_R300:
108 case CHIP_R350:
109 case CHIP_RV350:
110 case CHIP_RV380:
111 case CHIP_R420:
112 case CHIP_R423:
113 case CHIP_RV410:
114 case CHIP_RS400:
115 case CHIP_RS480:
116 case CHIP_RS600:
117 case CHIP_RS690:
118 case CHIP_RS740:
119 case CHIP_RV515:
120 case CHIP_R520:
121 case CHIP_RV530:
122 case CHIP_RV560:
123 case CHIP_RV570:
124 case CHIP_R580:
125 case CHIP_CEDAR:
126 case CHIP_REDWOOD:
127 case CHIP_JUNIPER:
128 case CHIP_CYPRESS:
129 case CHIP_HEMLOCK:
130 default:
131 fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
132 __func__, radeon->device);
133 break;
134 }
135 return radeon;
136 }
137
138 struct radeon *radeon_incref(struct radeon *radeon)
139 {
140 if (radeon == NULL)
141 return NULL;
142 radeon->refcount++;
143 return radeon;
144 }
145
146 struct radeon *radeon_decref(struct radeon *radeon)
147 {
148 if (radeon == NULL)
149 return NULL;
150 if (--radeon->refcount > 0) {
151 return NULL;
152 }
153 drmClose(radeon->fd);
154 free(radeon);
155 return NULL;
156 }
157
158 int radeon_reg_id(struct radeon *radeon, unsigned offset, unsigned *typeid, unsigned *stateid, unsigned *id)
159 {
160 unsigned i, j;
161
162 for (i = 0; i < radeon->ntype; i++) {
163 if (radeon->type[i].range_start) {
164 if (offset >= radeon->type[i].range_start && offset < radeon->type[i].range_end) {
165 *typeid = i;
166 j = offset - radeon->type[i].range_start;
167 j /= radeon->type[i].stride;
168 *stateid = radeon->type[i].id + j;
169 *id = (offset - radeon->type[i].range_start - radeon->type[i].stride * j) / 4;
170 return 0;
171 }
172 } else {
173 for (j = 0; j < radeon->type[i].nstates; j++) {
174 if (radeon->type[i].regs[j].offset == offset) {
175 *typeid = i;
176 *stateid = radeon->type[i].id;
177 *id = j;
178 return 0;
179 }
180 }
181 }
182 }
183 fprintf(stderr, "%s unknown register 0x%08X\n", __func__, offset);
184 return -EINVAL;
185 }
186
187 unsigned radeon_type_from_id(struct radeon *radeon, unsigned id)
188 {
189 unsigned i;
190
191 for (i = 0; i < radeon->ntype - 1; i++) {
192 if (radeon->type[i].id == id)
193 return i;
194 if (id > radeon->type[i].id && id < radeon->type[i + 1].id)
195 return i;
196 }
197 if (radeon->type[i].id == id)
198 return i;
199 return -1;
200 }