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