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