blob: cd83f050cf3e7fc0a0ee2c8cdf88fed97580eb2f [file] [log] [blame]
Dave Airlief64122c2013-02-25 14:47:55 +10001/*
2 * Copyright 2011 Red Hat, Inc.
3 *
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:
10 *
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
13 * Software.
14 *
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.
21 */
22#include "qxl_drv.h"
23#include "qxl_object.h"
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +020024#include <trace/events/fence.h>
Dave Airlief64122c2013-02-25 14:47:55 +100025
26/*
27 * drawable cmd cache - allocate a bunch of VRAM pages, suballocate
28 * into 256 byte chunks for now - gives 16 cmds per page.
29 *
30 * use an ida to index into the chunks?
31 */
32/* manage releaseables */
33/* stack them 16 high for now -drawable object is 191 */
34#define RELEASE_SIZE 256
35#define RELEASES_PER_BO (4096 / RELEASE_SIZE)
36/* put an alloc/dealloc surface cmd into one bo and round up to 128 */
37#define SURFACE_RELEASE_SIZE 128
38#define SURFACE_RELEASES_PER_BO (4096 / SURFACE_RELEASE_SIZE)
39
40static const int release_size_per_bo[] = { RELEASE_SIZE, SURFACE_RELEASE_SIZE, RELEASE_SIZE };
41static const int releases_per_bo[] = { RELEASES_PER_BO, SURFACE_RELEASES_PER_BO, RELEASES_PER_BO };
Dave Airlie8002db62013-07-23 14:16:42 +100042
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +020043static const char *qxl_get_driver_name(struct fence *fence)
44{
45 return "qxl";
46}
47
48static const char *qxl_get_timeline_name(struct fence *fence)
49{
50 return "release";
51}
52
53static bool qxl_nop_signaling(struct fence *fence)
54{
55 /* fences are always automatically signaled, so just pretend we did this.. */
56 return true;
57}
58
59static long qxl_fence_wait(struct fence *fence, bool intr, signed long timeout)
60{
61 struct qxl_device *qdev;
62 struct qxl_release *release;
63 int count = 0, sc = 0;
64 bool have_drawable_releases;
65 unsigned long cur, end = jiffies + timeout;
66
67 qdev = container_of(fence->lock, struct qxl_device, release_lock);
68 release = container_of(fence, struct qxl_release, base);
69 have_drawable_releases = release->type == QXL_RELEASE_DRAWABLE;
70
71retry:
72 sc++;
73
Maarten Lankhorstd7e4d672014-09-03 16:59:41 +020074 if (fence_is_signaled(fence))
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +020075 goto signaled;
76
77 qxl_io_notify_oom(qdev);
78
79 for (count = 0; count < 11; count++) {
80 if (!qxl_queue_garbage_collect(qdev, true))
81 break;
82
Maarten Lankhorstd7e4d672014-09-03 16:59:41 +020083 if (fence_is_signaled(fence))
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +020084 goto signaled;
85 }
86
Maarten Lankhorstd7e4d672014-09-03 16:59:41 +020087 if (fence_is_signaled(fence))
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +020088 goto signaled;
89
90 if (have_drawable_releases || sc < 4) {
91 if (sc > 2)
92 /* back off */
93 usleep_range(500, 1000);
94
95 if (time_after(jiffies, end))
96 return 0;
97
98 if (have_drawable_releases && sc > 300) {
Christian König76bf0db2016-06-01 15:10:02 +020099 FENCE_WARN(fence, "failed to wait on release %llu "
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200100 "after spincount %d\n",
101 fence->context & ~0xf0000000, sc);
102 goto signaled;
103 }
104 goto retry;
105 }
106 /*
107 * yeah, original sync_obj_wait gave up after 3 spins when
108 * have_drawable_releases is not set.
109 */
110
111signaled:
112 cur = jiffies;
113 if (time_after(cur, end))
114 return 0;
115 return end - cur;
116}
117
118static const struct fence_ops qxl_fence_ops = {
119 .get_driver_name = qxl_get_driver_name,
120 .get_timeline_name = qxl_get_timeline_name,
121 .enable_signaling = qxl_nop_signaling,
122 .wait = qxl_fence_wait,
123};
124
Frediano Ziglio6ecf5c22015-06-03 12:09:14 +0100125static int
Dave Airlief64122c2013-02-25 14:47:55 +1000126qxl_release_alloc(struct qxl_device *qdev, int type,
127 struct qxl_release **ret)
128{
129 struct qxl_release *release;
Dave Airlie307b9c02013-05-03 10:37:20 +1000130 int handle;
Dave Airlief64122c2013-02-25 14:47:55 +1000131 size_t size = sizeof(*release);
Dave Airlief64122c2013-02-25 14:47:55 +1000132
133 release = kmalloc(size, GFP_KERNEL);
134 if (!release) {
135 DRM_ERROR("Out of memory\n");
136 return 0;
137 }
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200138 release->base.ops = NULL;
Dave Airlief64122c2013-02-25 14:47:55 +1000139 release->type = type;
Dave Airlief64122c2013-02-25 14:47:55 +1000140 release->release_offset = 0;
141 release->surface_release_id = 0;
Dave Airlie8002db62013-07-23 14:16:42 +1000142 INIT_LIST_HEAD(&release->bos);
Dave Airlie307b9c02013-05-03 10:37:20 +1000143
144 idr_preload(GFP_KERNEL);
Dave Airlief64122c2013-02-25 14:47:55 +1000145 spin_lock(&qdev->release_idr_lock);
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200146 handle = idr_alloc(&qdev->release_idr, release, 1, 0, GFP_NOWAIT);
147 release->base.seqno = ++qdev->release_seqno;
Dave Airlief64122c2013-02-25 14:47:55 +1000148 spin_unlock(&qdev->release_idr_lock);
Dave Airlie307b9c02013-05-03 10:37:20 +1000149 idr_preload_end();
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200150 if (handle < 0) {
151 kfree(release);
152 *ret = NULL;
153 return handle;
154 }
Dave Airlie307b9c02013-05-03 10:37:20 +1000155 *ret = release;
Frediano Ziglio72ec5652015-06-03 12:09:16 +0100156 QXL_INFO(qdev, "allocated release %d\n", handle);
Dave Airlief64122c2013-02-25 14:47:55 +1000157 release->id = handle;
Dave Airlief64122c2013-02-25 14:47:55 +1000158 return handle;
159}
160
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200161static void
162qxl_release_free_list(struct qxl_release *release)
163{
164 while (!list_empty(&release->bos)) {
Maarten Lankhorstb75402c2014-09-03 17:01:28 +0200165 struct qxl_bo_list *entry;
166 struct qxl_bo *bo;
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200167
168 entry = container_of(release->bos.next,
Maarten Lankhorstb75402c2014-09-03 17:01:28 +0200169 struct qxl_bo_list, tv.head);
170 bo = to_qxl_bo(entry->tv.bo);
171 qxl_bo_unref(&bo);
172 list_del(&entry->tv.head);
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200173 kfree(entry);
174 }
175}
176
Dave Airlief64122c2013-02-25 14:47:55 +1000177void
178qxl_release_free(struct qxl_device *qdev,
179 struct qxl_release *release)
180{
Dave Airlie8002db62013-07-23 14:16:42 +1000181 QXL_INFO(qdev, "release %d, type %d\n", release->id,
182 release->type);
Dave Airlief64122c2013-02-25 14:47:55 +1000183
184 if (release->surface_release_id)
185 qxl_surface_id_dealloc(qdev, release->surface_release_id);
186
Dave Airlief64122c2013-02-25 14:47:55 +1000187 spin_lock(&qdev->release_idr_lock);
188 idr_remove(&qdev->release_idr, release->id);
189 spin_unlock(&qdev->release_idr_lock);
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200190
191 if (release->base.ops) {
192 WARN_ON(list_empty(&release->bos));
193 qxl_release_free_list(release);
194
195 fence_signal(&release->base);
196 fence_put(&release->base);
197 } else {
198 qxl_release_free_list(release);
199 kfree(release);
200 }
Dave Airlief64122c2013-02-25 14:47:55 +1000201}
202
Dave Airlie6d01f1f2013-04-16 13:24:25 +1000203static int qxl_release_bo_alloc(struct qxl_device *qdev,
204 struct qxl_bo **bo)
Dave Airlief64122c2013-02-25 14:47:55 +1000205{
Dave Airlie4f49ec92013-07-23 14:06:07 +1000206 /* pin releases bo's they are too messy to evict */
Masahiro Yamada96c594b2016-09-14 23:39:11 +0900207 return qxl_bo_create(qdev, PAGE_SIZE, false, true,
208 QXL_GEM_DOMAIN_VRAM, NULL, bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000209}
210
Dave Airlie8002db62013-07-23 14:16:42 +1000211int qxl_release_list_add(struct qxl_release *release, struct qxl_bo *bo)
212{
213 struct qxl_bo_list *entry;
214
215 list_for_each_entry(entry, &release->bos, tv.head) {
216 if (entry->tv.bo == &bo->tbo)
217 return 0;
218 }
219
220 entry = kmalloc(sizeof(struct qxl_bo_list), GFP_KERNEL);
221 if (!entry)
222 return -ENOMEM;
223
224 qxl_bo_ref(bo);
225 entry->tv.bo = &bo->tbo;
Christian Königae9c0af2014-09-04 20:01:52 +0200226 entry->tv.shared = false;
Dave Airlie8002db62013-07-23 14:16:42 +1000227 list_add_tail(&entry->tv.head, &release->bos);
228 return 0;
229}
230
231static int qxl_release_validate_bo(struct qxl_bo *bo)
Dave Airlief64122c2013-02-25 14:47:55 +1000232{
233 int ret;
Dave Airlie8002db62013-07-23 14:16:42 +1000234
235 if (!bo->pin_count) {
236 qxl_ttm_placement_from_domain(bo, bo->type, false);
237 ret = ttm_bo_validate(&bo->tbo, &bo->placement,
238 true, false);
Dave Airlief64122c2013-02-25 14:47:55 +1000239 if (ret)
240 return ret;
241 }
Dave Airlie8002db62013-07-23 14:16:42 +1000242
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200243 ret = reservation_object_reserve_shared(bo->tbo.resv);
244 if (ret)
245 return ret;
246
Dave Airlie8002db62013-07-23 14:16:42 +1000247 /* allocate a surface for reserved + validated buffers */
248 ret = qxl_bo_check_id(bo->gem_base.dev->dev_private, bo);
249 if (ret)
250 return ret;
251 return 0;
252}
253
254int qxl_release_reserve_list(struct qxl_release *release, bool no_intr)
255{
256 int ret;
257 struct qxl_bo_list *entry;
258
259 /* if only one object on the release its the release itself
260 since these objects are pinned no need to reserve */
261 if (list_is_singular(&release->bos))
262 return 0;
263
Christian Königaa350712014-12-03 15:46:48 +0100264 ret = ttm_eu_reserve_buffers(&release->ticket, &release->bos,
265 !no_intr, NULL);
Dave Airlie8002db62013-07-23 14:16:42 +1000266 if (ret)
267 return ret;
268
269 list_for_each_entry(entry, &release->bos, tv.head) {
270 struct qxl_bo *bo = to_qxl_bo(entry->tv.bo);
271
272 ret = qxl_release_validate_bo(bo);
273 if (ret) {
274 ttm_eu_backoff_reservation(&release->ticket, &release->bos);
275 return ret;
276 }
277 }
Dave Airlief64122c2013-02-25 14:47:55 +1000278 return 0;
279}
280
Dave Airlie8002db62013-07-23 14:16:42 +1000281void qxl_release_backoff_reserve_list(struct qxl_release *release)
Dave Airlief64122c2013-02-25 14:47:55 +1000282{
Dave Airlie8002db62013-07-23 14:16:42 +1000283 /* if only one object on the release its the release itself
284 since these objects are pinned no need to reserve */
285 if (list_is_singular(&release->bos))
286 return;
287
288 ttm_eu_backoff_reservation(&release->ticket, &release->bos);
Dave Airlief64122c2013-02-25 14:47:55 +1000289}
290
Dave Airlie8002db62013-07-23 14:16:42 +1000291
Dave Airlief64122c2013-02-25 14:47:55 +1000292int qxl_alloc_surface_release_reserved(struct qxl_device *qdev,
293 enum qxl_surface_cmd_type surface_cmd_type,
294 struct qxl_release *create_rel,
295 struct qxl_release **release)
296{
Dave Airlief64122c2013-02-25 14:47:55 +1000297 if (surface_cmd_type == QXL_SURFACE_CMD_DESTROY && create_rel) {
298 int idr_ret;
Dave Airlie8002db62013-07-23 14:16:42 +1000299 struct qxl_bo_list *entry = list_first_entry(&create_rel->bos, struct qxl_bo_list, tv.head);
Dave Airlief64122c2013-02-25 14:47:55 +1000300 struct qxl_bo *bo;
301 union qxl_release_info *info;
302
303 /* stash the release after the create command */
304 idr_ret = qxl_release_alloc(qdev, QXL_RELEASE_SURFACE_CMD, release);
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200305 if (idr_ret < 0)
306 return idr_ret;
Frediano Ziglio266424b2015-09-24 14:18:22 +0100307 bo = to_qxl_bo(entry->tv.bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000308
309 (*release)->release_offset = create_rel->release_offset + 64;
310
Dave Airlie8002db62013-07-23 14:16:42 +1000311 qxl_release_list_add(*release, bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000312
Dave Airlief64122c2013-02-25 14:47:55 +1000313 info = qxl_release_map(qdev, *release);
314 info->id = idr_ret;
315 qxl_release_unmap(qdev, *release, info);
Dave Airlie8002db62013-07-23 14:16:42 +1000316 return 0;
Dave Airlief64122c2013-02-25 14:47:55 +1000317 }
318
319 return qxl_alloc_release_reserved(qdev, sizeof(struct qxl_surface_cmd),
320 QXL_RELEASE_SURFACE_CMD, release, NULL);
321}
322
323int qxl_alloc_release_reserved(struct qxl_device *qdev, unsigned long size,
324 int type, struct qxl_release **release,
325 struct qxl_bo **rbo)
326{
327 struct qxl_bo *bo;
328 int idr_ret;
Dave Airlie8002db62013-07-23 14:16:42 +1000329 int ret = 0;
Dave Airlief64122c2013-02-25 14:47:55 +1000330 union qxl_release_info *info;
331 int cur_idx;
332
333 if (type == QXL_RELEASE_DRAWABLE)
334 cur_idx = 0;
335 else if (type == QXL_RELEASE_SURFACE_CMD)
336 cur_idx = 1;
337 else if (type == QXL_RELEASE_CURSOR_CMD)
338 cur_idx = 2;
339 else {
340 DRM_ERROR("got illegal type: %d\n", type);
341 return -EINVAL;
342 }
343
344 idr_ret = qxl_release_alloc(qdev, type, release);
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200345 if (idr_ret < 0) {
346 if (rbo)
347 *rbo = NULL;
348 return idr_ret;
349 }
Dave Airlief64122c2013-02-25 14:47:55 +1000350
351 mutex_lock(&qdev->release_mutex);
352 if (qdev->current_release_bo_offset[cur_idx] + 1 >= releases_per_bo[cur_idx]) {
353 qxl_bo_unref(&qdev->current_release_bo[cur_idx]);
354 qdev->current_release_bo_offset[cur_idx] = 0;
355 qdev->current_release_bo[cur_idx] = NULL;
356 }
357 if (!qdev->current_release_bo[cur_idx]) {
358 ret = qxl_release_bo_alloc(qdev, &qdev->current_release_bo[cur_idx]);
359 if (ret) {
360 mutex_unlock(&qdev->release_mutex);
Frediano Zigliode0b5232015-06-03 12:09:15 +0100361 qxl_release_free(qdev, *release);
Dave Airlief64122c2013-02-25 14:47:55 +1000362 return ret;
363 }
Dave Airlief64122c2013-02-25 14:47:55 +1000364 }
365
366 bo = qxl_bo_ref(qdev->current_release_bo[cur_idx]);
367
368 (*release)->release_offset = qdev->current_release_bo_offset[cur_idx] * release_size_per_bo[cur_idx];
369 qdev->current_release_bo_offset[cur_idx]++;
370
371 if (rbo)
372 *rbo = bo;
373
Dave Airlief64122c2013-02-25 14:47:55 +1000374 mutex_unlock(&qdev->release_mutex);
Dave Airlie8002db62013-07-23 14:16:42 +1000375
Frediano Zigliode0b5232015-06-03 12:09:15 +0100376 ret = qxl_release_list_add(*release, bo);
377 qxl_bo_unref(&bo);
378 if (ret) {
379 qxl_release_free(qdev, *release);
380 return ret;
381 }
Dave Airlief64122c2013-02-25 14:47:55 +1000382
383 info = qxl_release_map(qdev, *release);
384 info->id = idr_ret;
385 qxl_release_unmap(qdev, *release, info);
386
Dave Airlief64122c2013-02-25 14:47:55 +1000387 return ret;
388}
389
Dave Airlief64122c2013-02-25 14:47:55 +1000390struct qxl_release *qxl_release_from_id_locked(struct qxl_device *qdev,
391 uint64_t id)
392{
393 struct qxl_release *release;
394
395 spin_lock(&qdev->release_idr_lock);
396 release = idr_find(&qdev->release_idr, id);
397 spin_unlock(&qdev->release_idr_lock);
398 if (!release) {
399 DRM_ERROR("failed to find id in release_idr\n");
400 return NULL;
401 }
Dave Airlie8002db62013-07-23 14:16:42 +1000402
Dave Airlief64122c2013-02-25 14:47:55 +1000403 return release;
404}
405
406union qxl_release_info *qxl_release_map(struct qxl_device *qdev,
407 struct qxl_release *release)
408{
409 void *ptr;
410 union qxl_release_info *info;
Dave Airlie8002db62013-07-23 14:16:42 +1000411 struct qxl_bo_list *entry = list_first_entry(&release->bos, struct qxl_bo_list, tv.head);
412 struct qxl_bo *bo = to_qxl_bo(entry->tv.bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000413
414 ptr = qxl_bo_kmap_atomic_page(qdev, bo, release->release_offset & PAGE_SIZE);
Dave Airlie8002db62013-07-23 14:16:42 +1000415 if (!ptr)
416 return NULL;
Dave Airlief64122c2013-02-25 14:47:55 +1000417 info = ptr + (release->release_offset & ~PAGE_SIZE);
418 return info;
419}
420
421void qxl_release_unmap(struct qxl_device *qdev,
422 struct qxl_release *release,
423 union qxl_release_info *info)
424{
Dave Airlie8002db62013-07-23 14:16:42 +1000425 struct qxl_bo_list *entry = list_first_entry(&release->bos, struct qxl_bo_list, tv.head);
426 struct qxl_bo *bo = to_qxl_bo(entry->tv.bo);
Dave Airlief64122c2013-02-25 14:47:55 +1000427 void *ptr;
428
429 ptr = ((void *)info) - (release->release_offset & ~PAGE_SIZE);
430 qxl_bo_kunmap_atomic_page(qdev, bo, ptr);
431}
Dave Airlie8002db62013-07-23 14:16:42 +1000432
433void qxl_release_fence_buffer_objects(struct qxl_release *release)
434{
Dave Airlie8002db62013-07-23 14:16:42 +1000435 struct ttm_buffer_object *bo;
436 struct ttm_bo_global *glob;
437 struct ttm_bo_device *bdev;
438 struct ttm_bo_driver *driver;
439 struct qxl_bo *qbo;
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200440 struct ttm_validate_buffer *entry;
441 struct qxl_device *qdev;
Dave Airlie8002db62013-07-23 14:16:42 +1000442
443 /* if only one object on the release its the release itself
444 since these objects are pinned no need to reserve */
Maarten Lankhorst58b21c22014-09-03 17:02:44 +0200445 if (list_is_singular(&release->bos) || list_empty(&release->bos))
Dave Airlie8002db62013-07-23 14:16:42 +1000446 return;
447
448 bo = list_first_entry(&release->bos, struct ttm_validate_buffer, head)->bo;
449 bdev = bo->bdev;
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200450 qdev = container_of(bdev, struct qxl_device, mman.bdev);
451
452 /*
453 * Since we never really allocated a context and we don't want to conflict,
454 * set the highest bits. This will break if we really allow exporting of dma-bufs.
455 */
456 fence_init(&release->base, &qxl_fence_ops, &qdev->release_lock,
457 release->id | 0xf0000000, release->base.seqno);
458 trace_fence_emit(&release->base);
459
Dave Airlie8002db62013-07-23 14:16:42 +1000460 driver = bdev->driver;
461 glob = bo->glob;
462
463 spin_lock(&glob->lru_lock);
Dave Airlie8002db62013-07-23 14:16:42 +1000464
465 list_for_each_entry(entry, &release->bos, head) {
466 bo = entry->bo;
467 qbo = to_qxl_bo(bo);
468
Maarten Lankhorst2f453ed2014-04-02 12:40:05 +0200469 reservation_object_add_shared_fence(bo->resv, &release->base);
Dave Airlie8002db62013-07-23 14:16:42 +1000470 ttm_bo_add_to_lru(bo);
Thomas Hellstromc7523082014-02-20 11:36:25 +0100471 __ttm_bo_unreserve(bo);
Dave Airlie8002db62013-07-23 14:16:42 +1000472 }
Dave Airlie8002db62013-07-23 14:16:42 +1000473 spin_unlock(&glob->lru_lock);
474 ww_acquire_fini(&release->ticket);
475}
476