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