2 * Copyright © 2009 Corbin Simpson
3 * Copyright © 2011 Marek Olšák <maraeo@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT HOLDERS, AUTHORS
18 * AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * The above copyright notice and this permission notice (including the
24 * next paragraph) shall be included in all copies or substantial portions
29 * Corbin Simpson <MostAwesomeDude@gmail.com>
30 * Joakim Sindholt <opensource@zhasha.com>
31 * Marek Olšák <maraeo@gmail.com>
34 #include "radeon_drm_bo.h"
35 #include "radeon_drm_cs.h"
36 #include "radeon_drm_public.h"
38 #include "pipebuffer/pb_bufmgr.h"
39 #include "util/u_memory.h"
44 #ifndef RADEON_INFO_WANT_HYPERZ
45 #define RADEON_INFO_WANT_HYPERZ 7
47 #ifndef RADEON_INFO_WANT_CMASK
48 #define RADEON_INFO_WANT_CMASK 8
51 /* Enable/disable feature access. Return TRUE on success. */
52 static boolean
radeon_set_fd_access(int fd
, unsigned request
, boolean enable
)
54 struct drm_radeon_info info
= {0};
55 unsigned value
= enable
? 1 : 0;
57 info
.value
= (unsigned long)&value
;
58 info
.request
= request
;
60 if (drmCommandWriteRead(fd
, DRM_RADEON_INFO
, &info
, sizeof(info
)) != 0)
69 /* Helper function to do the ioctls needed for setup and init. */
70 static void do_ioctls(struct radeon_drm_winsys
*winsys
)
72 struct drm_radeon_gem_info gem_info
= {0};
73 struct drm_radeon_info info
= {0};
76 drmVersionPtr version
;
78 info
.value
= (unsigned long)&target
;
80 /* We do things in a specific order here.
82 * DRM version first. We need to be sure we're running on a KMS chipset.
83 * This is also for some features.
85 * Then, the PCI ID. This is essential and should return usable numbers
86 * for all Radeons. If this fails, we probably got handed an FD for some
89 * The GB and Z pipe requests should always succeed, but they might not
90 * return sensical values for all chipsets, but that's alright because
91 * the pipe drivers already know that.
93 * The GEM info is actually bogus on the kernel side, as well as our side
94 * (see radeon_gem_info_ioctl in radeon_gem.c) but that's alright because
95 * we don't actually use the info for anything yet. */
97 version
= drmGetVersion(winsys
->fd
);
98 if (version
->version_major
!= 2 ||
99 version
->version_minor
< 3) {
100 fprintf(stderr
, "%s: DRM version is %d.%d.%d but this driver is "
101 "only compatible with 2.3.x (kernel 2.6.34) and later.\n",
103 version
->version_major
,
104 version
->version_minor
,
105 version
->version_patchlevel
);
106 drmFreeVersion(version
);
110 winsys
->drm_major
= version
->version_major
;
111 winsys
->drm_minor
= version
->version_minor
;
112 winsys
->drm_patchlevel
= version
->version_patchlevel
;
114 info
.request
= RADEON_INFO_DEVICE_ID
;
115 retval
= drmCommandWriteRead(winsys
->fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
117 fprintf(stderr
, "%s: Failed to get PCI ID, "
118 "error number %d\n", __FUNCTION__
, retval
);
121 winsys
->pci_id
= target
;
123 info
.request
= RADEON_INFO_NUM_GB_PIPES
;
124 retval
= drmCommandWriteRead(winsys
->fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
126 fprintf(stderr
, "%s: Failed to get GB pipe count, "
127 "error number %d\n", __FUNCTION__
, retval
);
130 winsys
->gb_pipes
= target
;
132 info
.request
= RADEON_INFO_NUM_Z_PIPES
;
133 retval
= drmCommandWriteRead(winsys
->fd
, DRM_RADEON_INFO
, &info
, sizeof(info
));
135 fprintf(stderr
, "%s: Failed to get Z pipe count, "
136 "error number %d\n", __FUNCTION__
, retval
);
139 winsys
->z_pipes
= target
;
141 if (debug_get_bool_option("RADEON_HYPERZ", FALSE
)) {
142 winsys
->hyperz
= radeon_set_fd_access(winsys
->fd
,
143 RADEON_INFO_WANT_HYPERZ
, TRUE
);
146 if (debug_get_bool_option("RADEON_CMASK", FALSE
)) {
147 winsys
->aacompress
= radeon_set_fd_access(winsys
->fd
,
148 RADEON_INFO_WANT_CMASK
, TRUE
);
151 retval
= drmCommandWriteRead(winsys
->fd
, DRM_RADEON_GEM_INFO
,
152 &gem_info
, sizeof(gem_info
));
154 fprintf(stderr
, "%s: Failed to get MM info, error number %d\n",
155 __FUNCTION__
, retval
);
158 winsys
->gart_size
= gem_info
.gart_size
;
159 winsys
->vram_size
= gem_info
.vram_size
;
161 drmFreeVersion(version
);
163 winsys
->num_cpus
= sysconf(_SC_NPROCESSORS_ONLN
);
166 static void radeon_winsys_destroy(struct radeon_winsys
*rws
)
168 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
170 ws
->cman
->destroy(ws
->cman
);
171 ws
->kman
->destroy(ws
->kman
);
175 static uint32_t radeon_get_value(struct radeon_winsys
*rws
,
176 enum radeon_value_id id
)
178 struct radeon_drm_winsys
*ws
= (struct radeon_drm_winsys
*)rws
;
181 case RADEON_VID_PCI_ID
:
183 case RADEON_VID_R300_GB_PIPES
:
185 case RADEON_VID_R300_Z_PIPES
:
187 case RADEON_VID_GART_SIZE
:
188 return ws
->gart_size
;
189 case RADEON_VID_VRAM_SIZE
:
190 return ws
->vram_size
;
191 case RADEON_VID_DRM_MAJOR
:
192 return ws
->drm_major
;
193 case RADEON_VID_DRM_MINOR
:
194 return ws
->drm_minor
;
195 case RADEON_VID_DRM_PATCHLEVEL
:
196 return ws
->drm_patchlevel
;
197 case RADEON_VID_DRM_2_6_0
:
198 return ws
->drm_major
*100 + ws
->drm_minor
>= 206;
199 case RADEON_VID_DRM_2_8_0
:
200 return ws
->drm_major
*100 + ws
->drm_minor
>= 208;
201 case RADEON_VID_CAN_HYPERZ
:
203 case RADEON_VID_CAN_AACOMPRESS
:
204 return ws
->aacompress
;
209 struct radeon_winsys
*radeon_drm_winsys_create(int fd
)
211 struct radeon_drm_winsys
*ws
= CALLOC_STRUCT(radeon_drm_winsys
);
219 if (!is_r3xx(ws
->pci_id
)) {
223 /* Create managers. */
224 ws
->kman
= radeon_bomgr_create(ws
);
227 ws
->cman
= pb_cache_manager_create(ws
->kman
, 1000000);
232 ws
->base
.destroy
= radeon_winsys_destroy
;
233 ws
->base
.get_value
= radeon_get_value
;
235 radeon_bomgr_init_functions(ws
);
236 radeon_drm_cs_init_functions(ws
);
242 ws
->cman
->destroy(ws
->cman
);
244 ws
->kman
->destroy(ws
->kman
);