Merge remote branch 'origin/nv50-compiler'
[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 struct radeon *radeon_new(int fd, unsigned device)
46 {
47 struct radeon *radeon;
48 int r, i, id, j, k;
49
50 radeon = calloc(1, sizeof(*radeon));
51 if (radeon == NULL) {
52 return NULL;
53 }
54 radeon->fd = fd;
55 radeon->device = device;
56 radeon->refcount = 1;
57 if (fd >= 0) {
58 r = radeon_get_device(radeon);
59 if (r) {
60 fprintf(stderr, "Failed to get device id\n");
61 return radeon_decref(radeon);
62 }
63 }
64 radeon->family = radeon_family_from_device(radeon->device);
65 if (radeon->family == CHIP_UNKNOWN) {
66 fprintf(stderr, "Unknown chipset 0x%04X\n", radeon->device);
67 return radeon_decref(radeon);
68 }
69 switch (radeon->family) {
70 case CHIP_R600:
71 case CHIP_RV610:
72 case CHIP_RV630:
73 case CHIP_RV670:
74 case CHIP_RV620:
75 case CHIP_RV635:
76 case CHIP_RS780:
77 case CHIP_RS880:
78 case CHIP_RV770:
79 case CHIP_RV730:
80 case CHIP_RV710:
81 case CHIP_RV740:
82 case CHIP_CEDAR:
83 case CHIP_REDWOOD:
84 case CHIP_JUNIPER:
85 case CHIP_CYPRESS:
86 case CHIP_HEMLOCK:
87 if (r600_init(radeon)) {
88 return radeon_decref(radeon);
89 }
90 break;
91 case CHIP_R100:
92 case CHIP_RV100:
93 case CHIP_RS100:
94 case CHIP_RV200:
95 case CHIP_RS200:
96 case CHIP_R200:
97 case CHIP_RV250:
98 case CHIP_RS300:
99 case CHIP_RV280:
100 case CHIP_R300:
101 case CHIP_R350:
102 case CHIP_RV350:
103 case CHIP_RV380:
104 case CHIP_R420:
105 case CHIP_R423:
106 case CHIP_RV410:
107 case CHIP_RS400:
108 case CHIP_RS480:
109 case CHIP_RS600:
110 case CHIP_RS690:
111 case CHIP_RS740:
112 case CHIP_RV515:
113 case CHIP_R520:
114 case CHIP_RV530:
115 case CHIP_RV560:
116 case CHIP_RV570:
117 case CHIP_R580:
118 default:
119 fprintf(stderr, "%s unknown or unsupported chipset 0x%04X\n",
120 __func__, radeon->device);
121 break;
122 }
123 return radeon;
124 }
125
126 struct radeon *radeon_incref(struct radeon *radeon)
127 {
128 if (radeon == NULL)
129 return NULL;
130 radeon->refcount++;
131 return radeon;
132 }
133
134 struct radeon *radeon_decref(struct radeon *radeon)
135 {
136 if (radeon == NULL)
137 return NULL;
138 if (--radeon->refcount > 0) {
139 return NULL;
140 }
141 drmClose(radeon->fd);
142 free(radeon);
143 return NULL;
144 }