2 * Copyright 2011 Red Hat, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 #include "qxl_object.h"
26 * drawable cmd cache - allocate a bunch of VRAM pages, suballocate
27 * into 256 byte chunks for now - gives 16 cmds per page.
29 * use an ida to index into the chunks?
31 /* manage releaseables */
32 /* stack them 16 high for now -drawable object is 191 */
33 #define RELEASE_SIZE 256
34 #define RELEASES_PER_BO (4096 / RELEASE_SIZE)
35 /* put an alloc/dealloc surface cmd into one bo and round up to 128 */
36 #define SURFACE_RELEASE_SIZE 128
37 #define SURFACE_RELEASES_PER_BO (4096 / SURFACE_RELEASE_SIZE)
39 static const int release_size_per_bo[] = { RELEASE_SIZE, SURFACE_RELEASE_SIZE, RELEASE_SIZE };
40 static const int releases_per_bo[] = { RELEASES_PER_BO, SURFACE_RELEASES_PER_BO, RELEASES_PER_BO };
42 qxl_release_alloc(struct qxl_device *qdev, int type,
43 struct qxl_release **ret)
45 struct qxl_release *release;
47 size_t size = sizeof(*release);
50 release = kmalloc(size, GFP_KERNEL);
52 DRM_ERROR("Out of memory\n");
56 release->bo_count = 0;
57 release->release_offset = 0;
58 release->surface_release_id = 0;
60 idr_preload(GFP_KERNEL);
61 spin_lock(&qdev->release_idr_lock);
62 idr_ret = idr_alloc(&qdev->release_idr, release, 1, 0, GFP_NOWAIT);
63 spin_unlock(&qdev->release_idr_lock);
69 QXL_INFO(qdev, "allocated release %lld\n", handle);
77 qxl_release_free(struct qxl_device *qdev,
78 struct qxl_release *release)
82 QXL_INFO(qdev, "release %d, type %d, %d bos\n", release->id,
83 release->type, release->bo_count);
85 if (release->surface_release_id)
86 qxl_surface_id_dealloc(qdev, release->surface_release_id);
88 for (i = 0 ; i < release->bo_count; ++i) {
89 QXL_INFO(qdev, "release %llx\n",
90 release->bos[i]->tbo.addr_space_offset
92 qxl_fence_remove_release(&release->bos[i]->fence, release->id);
93 qxl_bo_unref(&release->bos[i]);
95 spin_lock(&qdev->release_idr_lock);
96 idr_remove(&qdev->release_idr, release->id);
97 spin_unlock(&qdev->release_idr_lock);
102 qxl_release_add_res(struct qxl_device *qdev, struct qxl_release *release,
106 for (i = 0; i < release->bo_count; i++)
107 if (release->bos[i] == bo)
110 if (release->bo_count >= QXL_MAX_RES) {
111 DRM_ERROR("exceeded max resource on a qxl_release item\n");
114 release->bos[release->bo_count++] = qxl_bo_ref(bo);
117 static int qxl_release_bo_alloc(struct qxl_device *qdev,
121 ret = qxl_bo_create(qdev, PAGE_SIZE, false, QXL_GEM_DOMAIN_VRAM, NULL,
126 int qxl_release_reserve(struct qxl_device *qdev,
127 struct qxl_release *release, bool no_wait)
130 if (atomic_inc_return(&release->bos[0]->reserve_count) == 1) {
131 ret = qxl_bo_reserve(release->bos[0], no_wait);
138 void qxl_release_unreserve(struct qxl_device *qdev,
139 struct qxl_release *release)
141 if (atomic_dec_and_test(&release->bos[0]->reserve_count))
142 qxl_bo_unreserve(release->bos[0]);
145 int qxl_alloc_surface_release_reserved(struct qxl_device *qdev,
146 enum qxl_surface_cmd_type surface_cmd_type,
147 struct qxl_release *create_rel,
148 struct qxl_release **release)
152 if (surface_cmd_type == QXL_SURFACE_CMD_DESTROY && create_rel) {
155 union qxl_release_info *info;
157 /* stash the release after the create command */
158 idr_ret = qxl_release_alloc(qdev, QXL_RELEASE_SURFACE_CMD, release);
159 bo = qxl_bo_ref(create_rel->bos[0]);
161 (*release)->release_offset = create_rel->release_offset + 64;
163 qxl_release_add_res(qdev, *release, bo);
165 ret = qxl_release_reserve(qdev, *release, false);
167 DRM_ERROR("release reserve failed\n");
170 info = qxl_release_map(qdev, *release);
172 qxl_release_unmap(qdev, *release, info);
180 return qxl_alloc_release_reserved(qdev, sizeof(struct qxl_surface_cmd),
181 QXL_RELEASE_SURFACE_CMD, release, NULL);
184 int qxl_alloc_release_reserved(struct qxl_device *qdev, unsigned long size,
185 int type, struct qxl_release **release,
191 union qxl_release_info *info;
194 if (type == QXL_RELEASE_DRAWABLE)
196 else if (type == QXL_RELEASE_SURFACE_CMD)
198 else if (type == QXL_RELEASE_CURSOR_CMD)
201 DRM_ERROR("got illegal type: %d\n", type);
205 idr_ret = qxl_release_alloc(qdev, type, release);
207 mutex_lock(&qdev->release_mutex);
208 if (qdev->current_release_bo_offset[cur_idx] + 1 >= releases_per_bo[cur_idx]) {
209 qxl_bo_unref(&qdev->current_release_bo[cur_idx]);
210 qdev->current_release_bo_offset[cur_idx] = 0;
211 qdev->current_release_bo[cur_idx] = NULL;
213 if (!qdev->current_release_bo[cur_idx]) {
214 ret = qxl_release_bo_alloc(qdev, &qdev->current_release_bo[cur_idx]);
216 mutex_unlock(&qdev->release_mutex);
220 /* pin releases bo's they are too messy to evict */
221 ret = qxl_bo_reserve(qdev->current_release_bo[cur_idx], false);
222 qxl_bo_pin(qdev->current_release_bo[cur_idx], QXL_GEM_DOMAIN_VRAM, NULL);
223 qxl_bo_unreserve(qdev->current_release_bo[cur_idx]);
226 bo = qxl_bo_ref(qdev->current_release_bo[cur_idx]);
228 (*release)->release_offset = qdev->current_release_bo_offset[cur_idx] * release_size_per_bo[cur_idx];
229 qdev->current_release_bo_offset[cur_idx]++;
234 qxl_release_add_res(qdev, *release, bo);
236 ret = qxl_release_reserve(qdev, *release, false);
237 mutex_unlock(&qdev->release_mutex);
241 info = qxl_release_map(qdev, *release);
243 qxl_release_unmap(qdev, *release, info);
250 int qxl_fence_releaseable(struct qxl_device *qdev,
251 struct qxl_release *release)
254 for (i = 0; i < release->bo_count; i++) {
255 if (!release->bos[i]->tbo.sync_obj)
256 release->bos[i]->tbo.sync_obj = &release->bos[i]->fence;
257 ret = qxl_fence_add_release(&release->bos[i]->fence, release->id);
264 struct qxl_release *qxl_release_from_id_locked(struct qxl_device *qdev,
267 struct qxl_release *release;
269 spin_lock(&qdev->release_idr_lock);
270 release = idr_find(&qdev->release_idr, id);
271 spin_unlock(&qdev->release_idr_lock);
273 DRM_ERROR("failed to find id in release_idr\n");
276 if (release->bo_count < 1) {
277 DRM_ERROR("read a released resource with 0 bos\n");
283 union qxl_release_info *qxl_release_map(struct qxl_device *qdev,
284 struct qxl_release *release)
287 union qxl_release_info *info;
288 struct qxl_bo *bo = release->bos[0];
290 ptr = qxl_bo_kmap_atomic_page(qdev, bo, release->release_offset & PAGE_SIZE);
291 info = ptr + (release->release_offset & ~PAGE_SIZE);
295 void qxl_release_unmap(struct qxl_device *qdev,
296 struct qxl_release *release,
297 union qxl_release_info *info)
299 struct qxl_bo *bo = release->bos[0];
302 ptr = ((void *)info) - (release->release_offset & ~PAGE_SIZE);
303 qxl_bo_kunmap_atomic_page(qdev, bo, ptr);