r600g: move chip class to radeon common structure
[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 <pipebuffer/pb_bufmgr.h>
24 #include "xf86drm.h"
25 #include "radeon_priv.h"
26 #include "radeon_drm.h"
27
28 enum radeon_family radeon_get_family(struct radeon *radeon)
29 {
30 return radeon->family;
31 }
32
33 enum chip_class radeon_get_family_class(struct radeon *radeon)
34 {
35 return radeon->chip_class;
36 }
37
38 void radeon_set_mem_constant(struct radeon *radeon, boolean state)
39 {
40 radeon->use_mem_constant = state;
41 }
42
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 struct radeon *radeon_new(int fd, unsigned device)
57 {
58 struct radeon *radeon;
59 int r, i, id, j, k;
60
61 radeon = calloc(1, sizeof(*radeon));
62 if (radeon == NULL) {
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 case CHIP_CEDAR:
94 case CHIP_REDWOOD:
95 case CHIP_JUNIPER:
96 case CHIP_CYPRESS:
97 case CHIP_HEMLOCK:
98 if (r600_init(radeon)) {
99 return radeon_decref(radeon);
100 }
101 break;
102 case CHIP_R100:
103 case CHIP_RV100:
104 case CHIP_RS100:
105 case CHIP_RV200:
106 case CHIP_RS200:
107 case CHIP_R200:
108 case CHIP_RV250:
109 case CHIP_RS300:
110 case CHIP_RV280:
111 case CHIP_R300:
112 case CHIP_R350:
113 case CHIP_RV350:
114 case CHIP_RV380:
115 case CHIP_R420:
116 case CHIP_R423:
117 case CHIP_RV410:
118 case CHIP_RS400:
119 case CHIP_RS480:
120 case CHIP_RS600:
121 case CHIP_RS690:
122 case CHIP_RS740:
123 case CHIP_RV515:
124 case CHIP_R520:
125 case CHIP_RV530:
126 case CHIP_RV560:
127 case CHIP_RV570:
128 case CHIP_R580:
129 default:
130 fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
131 __func__, radeon->device);
132 break;
133 }
134
135 /* setup class */
136 switch (radeon->family) {
137 case CHIP_R600:
138 case CHIP_RV610:
139 case CHIP_RV630:
140 case CHIP_RV670:
141 case CHIP_RV620:
142 case CHIP_RV635:
143 case CHIP_RS780:
144 case CHIP_RS880:
145 radeon->chip_class = R600;
146 break;
147 case CHIP_RV770:
148 case CHIP_RV730:
149 case CHIP_RV710:
150 case CHIP_RV740:
151 radeon->chip_class = R700;
152 break;
153 case CHIP_CEDAR:
154 case CHIP_REDWOOD:
155 case CHIP_JUNIPER:
156 case CHIP_CYPRESS:
157 case CHIP_HEMLOCK:
158 radeon->chip_class = EVERGREEN;
159 break;
160 default:
161 fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
162 __func__, radeon->device);
163 break;
164 }
165
166 radeon->mman = pb_malloc_bufmgr_create();
167 if (!radeon->mman)
168 return NULL;
169 radeon->kman = radeon_bo_pbmgr_create(radeon);
170 if (!radeon->kman)
171 return NULL;
172 radeon->cman = pb_cache_manager_create(radeon->kman, 100000);
173 if (!radeon->cman)
174 return NULL;
175 return radeon;
176 }
177
178 struct radeon *radeon_incref(struct radeon *radeon)
179 {
180 if (radeon == NULL)
181 return NULL;
182 radeon->refcount++;
183 return radeon;
184 }
185
186 struct radeon *radeon_decref(struct radeon *radeon)
187 {
188 if (radeon == NULL)
189 return NULL;
190 if (--radeon->refcount > 0) {
191 return NULL;
192 }
193
194 radeon->mman->destroy(radeon->mman);
195 radeon->cman->destroy(radeon->cman);
196 radeon->kman->destroy(radeon->kman);
197 drmClose(radeon->fd);
198 free(radeon);
199 return NULL;
200 }