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 #include <errno.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "internal.h"
34
35 #include <sys/ioctl.h>
36 #include "xf86drm.h"
37 #include "libdrm_macros.h"
38
39 #include "radeon_drm.h"
40
41
42 #define ALIGNMENT 512
43
44 struct radeon_bo
45 {
46 struct kms_bo base;
47 unsigned map_count;
48 };
49
50 static int
radeon_get_prop(struct kms_driver * kms,unsigned key,unsigned * out)51 radeon_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
52 {
53 switch (key) {
54 case KMS_BO_TYPE:
55 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
56 break;
57 default:
58 return -EINVAL;
59 }
60 return 0;
61 }
62
63 static int
radeon_destroy(struct kms_driver * kms)64 radeon_destroy(struct kms_driver *kms)
65 {
66 free(kms);
67 return 0;
68 }
69
70 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)71 radeon_bo_create(struct kms_driver *kms,
72 const unsigned width, const unsigned height,
73 const enum kms_bo_type type, const unsigned *attr,
74 struct kms_bo **out)
75 {
76 struct drm_radeon_gem_create arg;
77 unsigned size, pitch;
78 struct radeon_bo *bo;
79 int i, ret;
80
81 for (i = 0; attr[i]; i += 2) {
82 switch (attr[i]) {
83 case KMS_WIDTH:
84 case KMS_HEIGHT:
85 case KMS_BO_TYPE:
86 break;
87 default:
88 return -EINVAL;
89 }
90 }
91
92 switch (type) {
93 case KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8:
94 pitch = 4 * 64;
95 size = 4 * 64 * 64;
96 break;
97 case KMS_BO_TYPE_SCANOUT_X8R8G8B8:
98 pitch = width * 4;
99 pitch = (pitch + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
100 size = pitch * height;
101 break;
102 default:
103 return -EINVAL;
104 }
105
106 bo = calloc(1, sizeof(*bo));
107 if (!bo)
108 return -ENOMEM;
109
110 memset(&arg, 0, sizeof(arg));
111 arg.size = size;
112 arg.alignment = ALIGNMENT;
113 arg.initial_domain = RADEON_GEM_DOMAIN_CPU;
114 arg.flags = 0;
115 arg.handle = 0;
116
117 ret = drmCommandWriteRead(kms->fd, DRM_RADEON_GEM_CREATE,
118 &arg, sizeof(arg));
119 if (ret)
120 goto err_free;
121
122 bo->base.kms = kms;
123 bo->base.handle = arg.handle;
124 bo->base.size = size;
125 bo->base.pitch = pitch;
126 bo->base.offset = 0;
127 bo->map_count = 0;
128
129 *out = &bo->base;
130
131 return 0;
132
133 err_free:
134 free(bo);
135 return ret;
136 }
137
138 static int
radeon_bo_get_prop(struct kms_bo * bo,unsigned key,unsigned * out)139 radeon_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
140 {
141 switch (key) {
142 default:
143 return -EINVAL;
144 }
145 }
146
147 static int
radeon_bo_map(struct kms_bo * _bo,void ** out)148 radeon_bo_map(struct kms_bo *_bo, void **out)
149 {
150 struct radeon_bo *bo = (struct radeon_bo *)_bo;
151 struct drm_radeon_gem_mmap arg;
152 void *map = NULL;
153 int ret;
154
155 if (bo->base.ptr) {
156 bo->map_count++;
157 *out = bo->base.ptr;
158 return 0;
159 }
160
161 memset(&arg, 0, sizeof(arg));
162 arg.handle = bo->base.handle;
163 arg.offset = bo->base.offset;
164 arg.size = (uint64_t)bo->base.size;
165
166 ret = drmCommandWriteRead(bo->base.kms->fd, DRM_RADEON_GEM_MMAP,
167 &arg, sizeof(arg));
168 if (ret)
169 return -errno;
170
171 map = drm_mmap(0, arg.size, PROT_READ | PROT_WRITE, MAP_SHARED,
172 bo->base.kms->fd, arg.addr_ptr);
173 if (map == MAP_FAILED)
174 return -errno;
175
176 bo->base.ptr = map;
177 bo->map_count++;
178 *out = bo->base.ptr;
179
180 return 0;
181 }
182
183 static int
radeon_bo_unmap(struct kms_bo * _bo)184 radeon_bo_unmap(struct kms_bo *_bo)
185 {
186 struct radeon_bo *bo = (struct radeon_bo *)_bo;
187 if (--bo->map_count == 0) {
188 drm_munmap(bo->base.ptr, bo->base.size);
189 bo->base.ptr = NULL;
190 }
191 return 0;
192 }
193
194 static int
radeon_bo_destroy(struct kms_bo * _bo)195 radeon_bo_destroy(struct kms_bo *_bo)
196 {
197 struct radeon_bo *bo = (struct radeon_bo *)_bo;
198 struct drm_gem_close arg;
199 int ret;
200
201 if (bo->base.ptr) {
202 /* XXX Sanity check map_count */
203 drm_munmap(bo->base.ptr, bo->base.size);
204 bo->base.ptr = NULL;
205 }
206
207 memset(&arg, 0, sizeof(arg));
208 arg.handle = bo->base.handle;
209
210 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
211 if (ret)
212 return -errno;
213
214 free(bo);
215 return 0;
216 }
217
218 drm_private int
radeon_create(int fd,struct kms_driver ** out)219 radeon_create(int fd, struct kms_driver **out)
220 {
221 struct kms_driver *kms;
222
223 kms = calloc(1, sizeof(*kms));
224 if (!kms)
225 return -ENOMEM;
226
227 kms->fd = fd;
228
229 kms->bo_create = radeon_bo_create;
230 kms->bo_map = radeon_bo_map;
231 kms->bo_unmap = radeon_bo_unmap;
232 kms->bo_get_prop = radeon_bo_get_prop;
233 kms->bo_destroy = radeon_bo_destroy;
234 kms->get_prop = radeon_get_prop;
235 kms->destroy = radeon_destroy;
236 *out = kms;
237
238 return 0;
239 }
240