summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/i915/i915_fb_pin.c
blob: 1034cb767e9f9eedefae20da39f6aa19a74080ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// SPDX-License-Identifier: MIT
/*
 * Copyright © 2021 Intel Corporation
 */

#include <drm/drm_print.h>
#include <drm/intel/display_parent_interface.h>

#include "gem/i915_gem_domain.h"
#include "gem/i915_gem_object.h"

#include "i915_fb_pin.h"
#include "i915_dpt.h"
#include "i915_drv.h"
#include "i915_vma.h"

static struct i915_vma *
intel_fb_pin_to_dpt(struct drm_gem_object *_obj, struct intel_dpt *dpt,
		    const struct intel_fb_pin_params *pin_params)
{
	struct drm_i915_private *i915 = to_i915(_obj->dev);
	struct drm_i915_gem_object *obj = to_intel_bo(_obj);
	struct i915_address_space *vm = i915_dpt_to_vm(dpt);
	struct i915_gem_ww_ctx ww;
	struct i915_vma *vma;
	int ret;

	/*
	 * We are not syncing against the binding (and potential migrations)
	 * below, so this vm must never be async.
	 */
	if (drm_WARN_ON(&i915->drm, vm->bind_async_flags))
		return ERR_PTR(-EINVAL);

	if (WARN_ON(!i915_gem_object_is_framebuffer(obj)))
		return ERR_PTR(-EINVAL);

	atomic_inc(&i915->pending_fb_pin);

	for_i915_gem_ww(&ww, ret, true) {
		ret = i915_gem_object_lock(obj, &ww);
		if (ret)
			continue;

		if (HAS_LMEM(i915)) {
			unsigned int flags = obj->flags;

			/*
			 * For this type of buffer we need to able to read from the CPU
			 * the clear color value found in the buffer, hence we need to
			 * ensure it is always in the mappable part of lmem, if this is
			 * a small-bar device.
			 */
			if (pin_params->needs_cpu_lmem_access)
				flags &= ~I915_BO_ALLOC_GPU_ONLY;
			ret = __i915_gem_object_migrate(obj, &ww, INTEL_REGION_LMEM_0,
							flags);
			if (ret)
				continue;
		}

		ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
		if (ret)
			continue;

		vma = i915_vma_instance(obj, vm, pin_params->view);
		if (IS_ERR(vma)) {
			ret = PTR_ERR(vma);
			continue;
		}

		if (i915_vma_misplaced(vma, 0, pin_params->alignment, 0)) {
			ret = i915_vma_unbind(vma);
			if (ret)
				continue;
		}

		ret = i915_vma_pin_ww(vma, &ww, 0, pin_params->alignment,
				      PIN_GLOBAL);
		if (ret)
			continue;
	}
	if (ret) {
		vma = ERR_PTR(ret);
		goto err;
	}

	vma->display_alignment = max(vma->display_alignment,
				     pin_params->alignment);

	i915_gem_object_flush_if_display(obj);

	i915_vma_get(vma);

	/*
	 * The DPT object contains only one vma, and there is no VT-d
	 * guard, so the VMA's offset within the DPT is always 0.
	 */
	drm_WARN_ON(&i915->drm, i915_dpt_offset(vma));
err:
	atomic_dec(&i915->pending_fb_pin);

	return vma;
}

static struct i915_vma *
intel_fb_pin_to_ggtt(struct drm_gem_object *_obj,
		     const struct intel_fb_pin_params *pin_params,
		     int *out_fence_id)
{
	struct drm_i915_private *i915 = to_i915(_obj->dev);
	struct drm_i915_gem_object *obj = to_intel_bo(_obj);
	intel_wakeref_t wakeref;
	struct i915_gem_ww_ctx ww;
	struct i915_vma *vma;
	unsigned int pinctl;
	int ret;

	if (drm_WARN_ON(&i915->drm, !i915_gem_object_is_framebuffer(obj)))
		return ERR_PTR(-EINVAL);

	if (drm_WARN_ON(&i915->drm, pin_params->alignment &&
			!is_power_of_2(pin_params->alignment)))
		return ERR_PTR(-EINVAL);

	/*
	 * Global gtt pte registers are special registers which actually forward
	 * writes to a chunk of system memory. Which means that there is no risk
	 * that the register values disappear as soon as we call
	 * intel_runtime_pm_put(), so it is correct to wrap only the
	 * pin/unpin/fence and not more.
	 */
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);

	atomic_inc(&i915->pending_fb_pin);

	pinctl = 0;
	/* PIN_MAPPABLE limits the address to GMADR size */
	if (pin_params->needs_low_address)
		pinctl |= PIN_MAPPABLE;

	i915_gem_ww_ctx_init(&ww, true);
retry:
	ret = i915_gem_object_lock(obj, &ww);
	if (!ret && pin_params->needs_physical)
		ret = i915_gem_object_attach_phys(obj, pin_params->phys_alignment);
	else if (!ret && HAS_LMEM(i915))
		ret = i915_gem_object_migrate(obj, &ww, INTEL_REGION_LMEM_0);
	if (!ret)
		ret = i915_gem_object_pin_pages(obj);
	if (ret)
		goto err;

	vma = i915_gem_object_pin_to_display_plane(obj, &ww,
						   pin_params->alignment,
						   pin_params->vtd_guard,
						   pin_params->view, pinctl);
	if (IS_ERR(vma)) {
		ret = PTR_ERR(vma);
		goto err_unpin;
	}

	if (out_fence_id)
		*out_fence_id = -1;

	if (out_fence_id && i915_vma_is_map_and_fenceable(vma)) {
		/*
		 * Install a fence for tiled scan-out. Pre-i965 always needs a
		 * fence, whereas 965+ only requires a fence if using
		 * framebuffer compression.  For simplicity, we always, when
		 * possible, install a fence as the cost is not that onerous.
		 *
		 * If we fail to fence the tiled scanout, then either the
		 * modeset will reject the change (which is highly unlikely as
		 * the affected systems, all but one, do not have unmappable
		 * space) or we will not be able to enable full powersaving
		 * techniques (also likely not to apply due to various limits
		 * FBC and the like impose on the size of the buffer, which
		 * presumably we violated anyway with this unmappable buffer).
		 * Anyway, it is presumably better to stumble onwards with
		 * something and try to run the system in a "less than optimal"
		 * mode that matches the user configuration.
		 */
		ret = i915_vma_pin_fence(vma);
		if (ret != 0 && pin_params->needs_fence) {
			i915_vma_unpin(vma);
			goto err_unpin;
		}
		ret = 0;

		if (vma->fence)
			*out_fence_id = vma->fence->id;
	}

	i915_vma_get(vma);

err_unpin:
	i915_gem_object_unpin_pages(obj);
err:
	if (ret == -EDEADLK) {
		ret = i915_gem_ww_ctx_backoff(&ww);
		if (!ret)
			goto retry;
	}
	i915_gem_ww_ctx_fini(&ww);
	if (ret)
		vma = ERR_PTR(ret);

	atomic_dec(&i915->pending_fb_pin);
	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
	return vma;
}

static void intel_fb_unpin_vma(struct i915_vma *vma, int fence_id)
{
	if (fence_id >= 0)
		i915_vma_unpin_fence(vma);
	i915_vma_unpin(vma);
	i915_vma_put(vma);
}

static int i915_fb_pin_ggtt_pin(struct drm_gem_object *obj,
				const struct intel_fb_pin_params *pin_params,
				struct i915_vma **out_ggtt_vma,
				u32 *out_offset,
				int *out_fence_id)
{
	struct i915_vma *ggtt_vma;

	ggtt_vma = intel_fb_pin_to_ggtt(obj, pin_params, out_fence_id);
	if (IS_ERR(ggtt_vma))
		return PTR_ERR(ggtt_vma);

	*out_ggtt_vma = ggtt_vma;

	/*
	 * Pre-populate the dma address before we enter the vblank
	 * evade critical section as i915_gem_object_get_dma_address()
	 * will trigger might_sleep() even if it won't actually sleep,
	 * which is the case when the fb has already been pinned.
	 */
	if (pin_params->needs_physical)
		*out_offset = i915_gem_object_get_dma_address(to_intel_bo(obj), 0);
	else
		*out_offset = i915_ggtt_offset(ggtt_vma);

	return 0;
}

static void i915_fb_pin_ggtt_unpin(struct i915_vma *ggtt_vma,
				   int fence_id)
{
	if (ggtt_vma)
		intel_fb_unpin_vma(ggtt_vma, fence_id);
}

static int i915_fb_pin_dpt_pin(struct drm_gem_object *obj, struct intel_dpt *dpt,
			       const struct intel_fb_pin_params *pin_params,
			       struct i915_vma **out_dpt_vma,
			       struct i915_vma **out_ggtt_vma,
			       u32 *out_offset)
{
	struct i915_vma *ggtt_vma, *dpt_vma;

	WARN_ON(!dpt);

	ggtt_vma = i915_dpt_pin_to_ggtt(dpt, pin_params->alignment / 512);
	if (IS_ERR(ggtt_vma))
		return PTR_ERR(ggtt_vma);

	dpt_vma = intel_fb_pin_to_dpt(obj, dpt, pin_params);
	if (IS_ERR(dpt_vma)) {
		i915_dpt_unpin_from_ggtt(dpt);
		return PTR_ERR(dpt_vma);
	}

	drm_WARN_ON(obj->dev, ggtt_vma == dpt_vma);

	*out_ggtt_vma = ggtt_vma;
	*out_dpt_vma = dpt_vma;

	*out_offset = i915_ggtt_offset(ggtt_vma);

	return 0;
}

static void i915_fb_pin_dpt_unpin(struct intel_dpt *dpt,
				  struct i915_vma *dpt_vma,
				  struct i915_vma *ggtt_vma)
{
	WARN_ON(!dpt);
	WARN_ON(!!dpt_vma != !!ggtt_vma);

	if (dpt_vma)
		intel_fb_unpin_vma(dpt_vma, -1);
	if (ggtt_vma)
		i915_dpt_unpin_from_ggtt(dpt);
}

static void i915_fb_pin_get_map(struct i915_vma *vma, struct iosys_map *map)
{
	iosys_map_set_vaddr_iomem(map, i915_vma_get_iomap(vma));
}

const struct intel_display_fb_pin_interface i915_display_fb_pin_interface = {
	.ggtt_pin = i915_fb_pin_ggtt_pin,
	.ggtt_unpin = i915_fb_pin_ggtt_unpin,
	.dpt_pin = i915_fb_pin_dpt_pin,
	.dpt_unpin = i915_fb_pin_dpt_unpin,
	.get_map = i915_fb_pin_get_map,
};