blob: 859e7a0f15c6eac932a6f4cccb52aaa275c11f1b [file] [log] [blame]
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +00001/**************************************************************************
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
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +000029#include <errno.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include "internal.h"
34
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +000035#include <sys/ioctl.h>
36#include "xf86drm.h"
Emil Velikov42465fe2015-04-05 15:51:59 +010037#include "libdrm_macros.h"
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +000038
39#include "i915_drm.h"
40
41struct intel_bo
42{
43 struct kms_bo base;
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +000044 unsigned map_count;
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +000045};
46
47static int
48intel_get_prop(struct kms_driver *kms, unsigned key, unsigned *out)
49{
50 switch (key) {
Jakob Bornecrantz44a0e0a2010-01-23 01:41:49 +000051 case KMS_BO_TYPE:
52 *out = KMS_BO_TYPE_SCANOUT_X8R8G8B8 | KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8;
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +000053 break;
54 default:
55 return -EINVAL;
56 }
57 return 0;
58}
59
60static int
61intel_destroy(struct kms_driver *kms)
62{
63 free(kms);
64 return 0;
65}
66
67static int
68intel_bo_create(struct kms_driver *kms,
69 const unsigned width, const unsigned height,
70 const enum kms_bo_type type, const unsigned *attr,
71 struct kms_bo **out)
72{
73 struct drm_i915_gem_create arg;
74 unsigned size, pitch;
75 struct intel_bo *bo;
76 int i, ret;
77
78 for (i = 0; attr[i]; i += 2) {
79 switch (attr[i]) {
80 case KMS_WIDTH:
81 case KMS_HEIGHT:
82 case KMS_BO_TYPE:
83 break;
84 default:
85 return -EINVAL;
86 }
87 }
88
89 bo = calloc(1, sizeof(*bo));
90 if (!bo)
91 return -ENOMEM;
92
Jakob Bornecrantz44a0e0a2010-01-23 01:41:49 +000093 if (type == KMS_BO_TYPE_CURSOR_64X64_A8R8G8B8) {
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +000094 pitch = 64 * 4;
95 size = 64 * 64 * 4;
Jakob Bornecrantz44a0e0a2010-01-23 01:41:49 +000096 } else if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8) {
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +000097 pitch = width * 4;
98 pitch = (pitch + 512 - 1) & ~(512 - 1);
99 size = pitch * ((height + 4 - 1) & ~(4 - 1));
100 } else {
Maxime Villard1f4b5e12013-01-02 10:55:50 -0500101 free(bo);
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +0000102 return -EINVAL;
103 }
104
105 memset(&arg, 0, sizeof(arg));
106 arg.size = size;
107
108 ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_CREATE, &arg, sizeof(arg));
109 if (ret)
110 goto err_free;
111
112 bo->base.kms = kms;
113 bo->base.handle = arg.handle;
114 bo->base.size = size;
115 bo->base.pitch = pitch;
116
117 *out = &bo->base;
Jakob Bornecrantz44a0e0a2010-01-23 01:41:49 +0000118 if (type == KMS_BO_TYPE_SCANOUT_X8R8G8B8 && pitch > 512) {
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +0000119 struct drm_i915_gem_set_tiling tile;
120
121 memset(&tile, 0, sizeof(tile));
122 tile.handle = bo->base.handle;
123 tile.tiling_mode = I915_TILING_X;
124 tile.stride = bo->base.pitch;
125
126 ret = drmCommandWriteRead(kms->fd, DRM_I915_GEM_SET_TILING, &tile, sizeof(tile));
Jakob Bornecrantz8838bb12010-01-08 15:19:44 +0000127#if 0
128 if (ret) {
129 kms_bo_destroy(out);
130 return ret;
131 }
132#endif
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +0000133 }
134
135 return 0;
136
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +0000137err_free:
138 free(bo);
139 return ret;
140}
141
142static int
143intel_bo_get_prop(struct kms_bo *bo, unsigned key, unsigned *out)
144{
145 switch (key) {
146 default:
147 return -EINVAL;
148 }
149}
150
151static int
152intel_bo_map(struct kms_bo *_bo, void **out)
153{
154 struct intel_bo *bo = (struct intel_bo *)_bo;
155 struct drm_i915_gem_mmap_gtt 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
168 ret = drmCommandWriteRead(bo->base.kms->fd, DRM_I915_GEM_MMAP_GTT, &arg, sizeof(arg));
169 if (ret)
170 return ret;
171
Emil Velikovdadc9ef2014-09-07 19:41:37 +0100172 map = drm_mmap(0, bo->base.size, PROT_READ | PROT_WRITE, MAP_SHARED, bo->base.kms->fd, arg.offset);
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +0000173 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
183static int
184intel_bo_unmap(struct kms_bo *_bo)
185{
186 struct intel_bo *bo = (struct intel_bo *)_bo;
187 bo->map_count--;
188 return 0;
189}
190
191static int
192intel_bo_destroy(struct kms_bo *_bo)
193{
194 struct intel_bo *bo = (struct intel_bo *)_bo;
195 struct drm_gem_close arg;
196 int ret;
197
Jakob Bornecrantz29592662010-01-15 20:13:20 +0000198 if (bo->base.ptr) {
199 /* XXX Sanity check map_count */
Emil Velikovdadc9ef2014-09-07 19:41:37 +0100200 drm_munmap(bo->base.ptr, bo->base.size);
Jakob Bornecrantz29592662010-01-15 20:13:20 +0000201 bo->base.ptr = NULL;
202 }
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +0000203
204 memset(&arg, 0, sizeof(arg));
205 arg.handle = bo->base.handle;
206
207 ret = drmIoctl(bo->base.kms->fd, DRM_IOCTL_GEM_CLOSE, &arg);
208 if (ret)
209 return -errno;
210
211 free(bo);
212 return 0;
213}
214
Emil Velikov983892d2015-03-23 23:24:48 +0000215drm_private int
Jakob Bornecrantzbfa44bb2010-01-08 03:00:56 +0000216intel_create(int fd, struct kms_driver **out)
217{
218 struct kms_driver *kms;
219
220 kms = calloc(1, sizeof(*kms));
221 if (!kms)
222 return -ENOMEM;
223
224 kms->fd = fd;
225
226 kms->bo_create = intel_bo_create;
227 kms->bo_map = intel_bo_map;
228 kms->bo_unmap = intel_bo_unmap;
229 kms->bo_get_prop = intel_bo_get_prop;
230 kms->bo_destroy = intel_bo_destroy;
231 kms->get_prop = intel_get_prop;
232 kms->destroy = intel_destroy;
233 *out = kms;
234
235 return 0;
236}