i915_gem_busy.c 3.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
/*
 * SPDX-License-Identifier: MIT
 *
 * Copyright © 2014-2016 Intel Corporation
 */

#include "gt/intel_engine.h"

#include "i915_gem_ioctls.h"
#include "i915_gem_object.h"

12
static __always_inline u32 __busy_read_flag(u16 id)
13
{
14
	if (id == (u16)I915_ENGINE_CLASS_INVALID)
15
16
17
18
19
20
		return 0xffff0000u;

	GEM_BUG_ON(id >= 16);
	return 0x10000u << id;
}

21
static __always_inline u32 __busy_write_id(u16 id)
22
23
24
25
26
27
28
29
30
31
{
	/*
	 * The uABI guarantees an active writer is also amongst the read
	 * engines. This would be true if we accessed the activity tracking
	 * under the lock, but as we perform the lookup of the object and
	 * its activity locklessly we can not guarantee that the last_write
	 * being active implies that we have set the same engine flag from
	 * last_read - hence we always set both read and write busy for
	 * last_write.
	 */
32
	if (id == (u16)I915_ENGINE_CLASS_INVALID)
33
34
35
36
37
38
		return 0xffffffffu;

	return (id + 1) | __busy_read_flag(id);
}

static __always_inline unsigned int
39
__busy_set_if_active(const struct dma_fence *fence, u32 (*flag)(u16 id))
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
{
	const struct i915_request *rq;

	/*
	 * We have to check the current hw status of the fence as the uABI
	 * guarantees forward progress. We could rely on the idle worker
	 * to eventually flush us, but to minimise latency just ask the
	 * hardware.
	 *
	 * Note we only report on the status of native fences.
	 */
	if (!dma_fence_is_i915(fence))
		return 0;

	/* opencode to_request() in order to avoid const warnings */
	rq = container_of(fence, const struct i915_request, fence);
	if (i915_request_completed(rq))
		return 0;

	/* Beware type-expansion follies! */
60
	BUILD_BUG_ON(!typecheck(u16, rq->engine->uabi_class));
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
	return flag(rq->engine->uabi_class);
}

static __always_inline unsigned int
busy_check_reader(const struct dma_fence *fence)
{
	return __busy_set_if_active(fence, __busy_read_flag);
}

static __always_inline unsigned int
busy_check_writer(const struct dma_fence *fence)
{
	if (!fence)
		return 0;

	return __busy_set_if_active(fence, __busy_write_id);
}

int
i915_gem_busy_ioctl(struct drm_device *dev, void *data,
		    struct drm_file *file)
{
	struct drm_i915_gem_busy *args = data;
	struct drm_i915_gem_object *obj;
85
	struct dma_resv_list *list;
86
	unsigned int seq;
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	int err;

	err = -ENOENT;
	rcu_read_lock();
	obj = i915_gem_object_lookup_rcu(file, args->handle);
	if (!obj)
		goto out;

	/*
	 * A discrepancy here is that we do not report the status of
	 * non-i915 fences, i.e. even though we may report the object as idle,
	 * a call to set-domain may still stall waiting for foreign rendering.
	 * This also means that wait-ioctl may report an object as busy,
	 * where busy-ioctl considers it idle.
	 *
	 * We trade the ability to warn of foreign fences to report on which
	 * i915 engines are active for the object.
	 *
	 * Alternatively, we can trade that extra information on read/write
	 * activity with
	 *	args->busy =
108
	 *		!dma_resv_test_signaled_rcu(obj->resv, true);
109
110
111
	 * to report the overall busyness. This is what the wait-ioctl does.
	 *
	 */
112
113
retry:
	seq = raw_read_seqcount(&obj->base.resv->seq);
114
115

	/* Translate the exclusive fence to the READ *and* WRITE engine */
116
117
	args->busy =
		busy_check_writer(rcu_dereference(obj->base.resv->fence_excl));
118
119

	/* Translate shared fences to READ set of engines */
120
121
122
	list = rcu_dereference(obj->base.resv->fence);
	if (list) {
		unsigned int shared_count = list->shared_count, i;
123

124
125
126
127
128
129
		for (i = 0; i < shared_count; ++i) {
			struct dma_fence *fence =
				rcu_dereference(list->shared[i]);

			args->busy |= busy_check_reader(fence);
		}
130
131
	}

132
133
134
	if (args->busy && read_seqcount_retry(&obj->base.resv->seq, seq))
		goto retry;

135
136
137
138
139
	err = 0;
out:
	rcu_read_unlock();
	return err;
}