• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * 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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32 
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include "internal.h"
38 
39 #include <sys/ioctl.h>
40 #include "xf86drm.h"
41 #include "libdrm_macros.h"
42 
43 #include "radeon_drm.h"
44 
45 
46 #define ALIGNMENT 512
47 
48 struct radeon_bo
49 {
50 	struct kms_bo base;
51 	unsigned map_count;
52 };
53 
54 static int
radeon_get_prop(struct kms_driver * kms,unsigned key,unsigned * out)55 radeon_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
56 {
57 	switch (key) {
58 	case KMS_BO_TYPE:
59 		*out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
60 		break;
61 	default:
62 		return -EINVAL;
63 	}
64 	return 0;
65 }
66 
67 static int
radeon_destroy(struct kms_driver * kms)68 radeon_destroy(struct kms_driver *kms)
69 {
70 	free(kms);
71 	return 0;
72 }
73 
74 static int
radeon_bo_create(struct kms_driver * kms,const unsigned width,const unsigned height,const enum kms_bo_type type,const unsigned * attr,struct kms_bo ** out)75 radeon_bo_create(struct kms_driver *kms,
76 		 const unsigned width, const unsigned height,
77 		 const enum kms_bo_type type, const unsigned *attr,
78 		 struct kms_bo **out)
79 {
80 	struct drm_radeon_gem_create arg;
81 	unsigned size, pitch;
82 	struct radeon_bo *bo;
83 	int i, ret;
84 
85 	for (i = 0; attr[i]; i += 2) {
86 		switch (attr[i]) {
87 		case KMS_WIDTH:
88 		case KMS_HEIGHT:
89 		case KMS_BO_TYPE:
90 			break;
91 		default:
92 			return -EINVAL;
93 		}
94 	}
95 
96 	switch (type) {
97 	case KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8:
98 		pitch = 4 * 64;
99 		size  = 4 * 64 * 64;
100 		break;
101 	case KMS_BO_TYPE_SCANOUT_X8R8G8B8:
102 		pitch = width * 4;
103 		pitch = (pitch + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
104 		size  = pitch * height;
105 		break;
106 	default:
107 		return -EINVAL;
108 	}
109 
110 	bo = calloc(1, sizeof(*bo));
111 	if (!bo)
112 		return -ENOMEM;
113 
114 	memset(&arg, 0, sizeof(arg));
115 	arg.size = size;
116 	arg.alignment = ALIGNMENT;
117 	arg.initial_domain = RADEON_GEM_DOMAIN_CPU;
118 	arg.flags = 0;
119 	arg.handle = 0;
120 
121 	ret = drmCommandWriteRead(kms->fd, DRM_RADEON_GEM_CREATE,
122 	                          &arg, sizeof(arg));
123 	if (ret)
124 		goto err_free;
125 
126 	bo->base.kms = kms;
127 	bo->base.handle = arg.handle;
128 	bo->base.size = size;
129 	bo->base.pitch = pitch;
130 	bo->base.offset = 0;
131 	bo->map_count = 0;
132 
133 	*out = &bo->base;
134 
135 	return 0;
136 
137 err_free:
138 	free(bo);
139 	return ret;
140 }
141 
142 static int
radeon_bo_get_prop(struct kms_bo * bo,unsigned key,unsigned * out)143 radeon_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
144 {
145 	switch (key) {
146 	default:
147 		return -EINVAL;
148 	}
149 }
150 
151 static int
radeon_bo_map(struct kms_bo * _bo,void ** out)152 radeon_bo_map(struct kms_bo *_bo, void **out)
153 {
154 	struct radeon_bo *bo = (struct radeon_bo *)_bo;
155 	struct drm_radeon_gem_mmap arg;
156 	void *map = NULL;
157 	int ret;
158 
159 	if (bo->base.ptr) {
160 		bo->map_count++;
161 		*out = bo->base.ptr;
162 		return 0;
163 	}
164 
165 	memset(&arg, 0, sizeof(arg));
166 	arg.handle = bo->base.handle;
167 	arg.offset = bo->base.offset;
168 	arg.size = (uint64_t)bo->base.size;
169 
170 	ret = drmCommandWriteRead(bo->base.kms->fd, DRM_RADEON_GEM_MMAP,
171 	                        &arg, sizeof(arg));
172 	if (ret)
173 		return -errno;
174 
175 	map = drm_mmap(0, arg.size, PROT_READ | PROT_WRITE, MAP_SHARED,
176 	           bo->base.kms->fd, arg.addr_ptr);
177 	if (map == MAP_FAILED)
178 		return -errno;
179 
180 	bo->base.ptr = map;
181 	bo->map_count++;
182 	*out = bo->base.ptr;
183 
184 	return 0;
185 }
186 
187 static int
radeon_bo_unmap(struct kms_bo * _bo)188 radeon_bo_unmap(struct kms_bo *_bo)
189 {
190 	struct radeon_bo *bo = (struct radeon_bo *)_bo;
191 	if (--bo->map_count == 0) {
192 		drm_munmap(bo->base.ptr, bo->base.size);
193 		bo->base.ptr = NULL;
194 	}
195 	return 0;
196 }
197 
198 static int
radeon_bo_destroy(struct kms_bo * _bo)199 radeon_bo_destroy(struct kms_bo *_bo)
200 {
201 	struct radeon_bo *bo = (struct radeon_bo *)_bo;
202 	struct drm_gem_close arg;
203 	int ret;
204 
205 	if (bo->base.ptr) {
206 		/* XXX Sanity check map_count */
207 		drm_munmap(bo->base.ptr, bo->base.size);
208 		bo->base.ptr = NULL;
209 	}
210 
211 	memset(&arg, 0, sizeof(arg));
212 	arg.handle = bo->base.handle;
213 
214 	ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
215 	if (ret)
216 		return -errno;
217 
218 	free(bo);
219 	return 0;
220 }
221 
222 drm_private int
radeon_create(int fd,struct kms_driver ** out)223 radeon_create(int fd, struct kms_driver **out)
224 {
225 	struct kms_driver *kms;
226 
227 	kms = calloc(1, sizeof(*kms));
228 	if (!kms)
229 		return -ENOMEM;
230 
231 	kms->fd = fd;
232 
233 	kms->bo_create = radeon_bo_create;
234 	kms->bo_map = radeon_bo_map;
235 	kms->bo_unmap = radeon_bo_unmap;
236 	kms->bo_get_prop = radeon_bo_get_prop;
237 	kms->bo_destroy = radeon_bo_destroy;
238 	kms->get_prop = radeon_get_prop;
239 	kms->destroy = radeon_destroy;
240 	*out = kms;
241 
242 	return 0;
243 }
244