Merge tag 'drm-misc-next-2021-10-14' of git://anongit.freedesktop.org/drm/drm-misc...
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / display / intel_frontbuffer.c
1 /*
2  * Copyright © 2014 Intel Corporation
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  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *      Daniel Vetter <daniel.vetter@ffwll.ch>
25  */
26
27 /**
28  * DOC: frontbuffer tracking
29  *
30  * Many features require us to track changes to the currently active
31  * frontbuffer, especially rendering targeted at the frontbuffer.
32  *
33  * To be able to do so we track frontbuffers using a bitmask for all possible
34  * frontbuffer slots through intel_frontbuffer_track(). The functions in this
35  * file are then called when the contents of the frontbuffer are invalidated,
36  * when frontbuffer rendering has stopped again to flush out all the changes
37  * and when the frontbuffer is exchanged with a flip. Subsystems interested in
38  * frontbuffer changes (e.g. PSR, FBC, DRRS) should directly put their callbacks
39  * into the relevant places and filter for the frontbuffer slots that they are
40  * interested int.
41  *
42  * On a high level there are two types of powersaving features. The first one
43  * work like a special cache (FBC and PSR) and are interested when they should
44  * stop caching and when to restart caching. This is done by placing callbacks
45  * into the invalidate and the flush functions: At invalidate the caching must
46  * be stopped and at flush time it can be restarted. And maybe they need to know
47  * when the frontbuffer changes (e.g. when the hw doesn't initiate an invalidate
48  * and flush on its own) which can be achieved with placing callbacks into the
49  * flip functions.
50  *
51  * The other type of display power saving feature only cares about busyness
52  * (e.g. DRRS). In that case all three (invalidate, flush and flip) indicate
53  * busyness. There is no direct way to detect idleness. Instead an idle timer
54  * work delayed work should be started from the flush and flip functions and
55  * cancelled as soon as busyness is detected.
56  */
57
58 #include "display/intel_dp.h"
59
60 #include "i915_drv.h"
61 #include "i915_trace.h"
62 #include "intel_display_types.h"
63 #include "intel_fbc.h"
64 #include "intel_frontbuffer.h"
65 #include "intel_drrs.h"
66 #include "intel_psr.h"
67
68 /**
69  * frontbuffer_flush - flush frontbuffer
70  * @i915: i915 device
71  * @frontbuffer_bits: frontbuffer plane tracking bits
72  * @origin: which operation caused the flush
73  *
74  * This function gets called every time rendering on the given planes has
75  * completed and frontbuffer caching can be started again. Flushes will get
76  * delayed if they're blocked by some outstanding asynchronous rendering.
77  *
78  * Can be called without any locks held.
79  */
80 static void frontbuffer_flush(struct drm_i915_private *i915,
81                               unsigned int frontbuffer_bits,
82                               enum fb_op_origin origin)
83 {
84         /* Delay flushing when rings are still busy.*/
85         spin_lock(&i915->fb_tracking.lock);
86         frontbuffer_bits &= ~i915->fb_tracking.busy_bits;
87         spin_unlock(&i915->fb_tracking.lock);
88
89         if (!frontbuffer_bits)
90                 return;
91
92         trace_intel_frontbuffer_flush(frontbuffer_bits, origin);
93
94         might_sleep();
95         intel_drrs_flush(i915, frontbuffer_bits);
96         intel_psr_flush(i915, frontbuffer_bits, origin);
97         intel_fbc_flush(i915, frontbuffer_bits, origin);
98 }
99
100 /**
101  * intel_frontbuffer_flip_prepare - prepare asynchronous frontbuffer flip
102  * @i915: i915 device
103  * @frontbuffer_bits: frontbuffer plane tracking bits
104  *
105  * This function gets called after scheduling a flip on @obj. The actual
106  * frontbuffer flushing will be delayed until completion is signalled with
107  * intel_frontbuffer_flip_complete. If an invalidate happens in between this
108  * flush will be cancelled.
109  *
110  * Can be called without any locks held.
111  */
112 void intel_frontbuffer_flip_prepare(struct drm_i915_private *i915,
113                                     unsigned frontbuffer_bits)
114 {
115         spin_lock(&i915->fb_tracking.lock);
116         i915->fb_tracking.flip_bits |= frontbuffer_bits;
117         /* Remove stale busy bits due to the old buffer. */
118         i915->fb_tracking.busy_bits &= ~frontbuffer_bits;
119         spin_unlock(&i915->fb_tracking.lock);
120 }
121
122 /**
123  * intel_frontbuffer_flip_complete - complete asynchronous frontbuffer flip
124  * @i915: i915 device
125  * @frontbuffer_bits: frontbuffer plane tracking bits
126  *
127  * This function gets called after the flip has been latched and will complete
128  * on the next vblank. It will execute the flush if it hasn't been cancelled yet.
129  *
130  * Can be called without any locks held.
131  */
132 void intel_frontbuffer_flip_complete(struct drm_i915_private *i915,
133                                      unsigned frontbuffer_bits)
134 {
135         spin_lock(&i915->fb_tracking.lock);
136         /* Mask any cancelled flips. */
137         frontbuffer_bits &= i915->fb_tracking.flip_bits;
138         i915->fb_tracking.flip_bits &= ~frontbuffer_bits;
139         spin_unlock(&i915->fb_tracking.lock);
140
141         if (frontbuffer_bits)
142                 frontbuffer_flush(i915, frontbuffer_bits, ORIGIN_FLIP);
143 }
144
145 /**
146  * intel_frontbuffer_flip - synchronous frontbuffer flip
147  * @i915: i915 device
148  * @frontbuffer_bits: frontbuffer plane tracking bits
149  *
150  * This function gets called after scheduling a flip on @obj. This is for
151  * synchronous plane updates which will happen on the next vblank and which will
152  * not get delayed by pending gpu rendering.
153  *
154  * Can be called without any locks held.
155  */
156 void intel_frontbuffer_flip(struct drm_i915_private *i915,
157                             unsigned frontbuffer_bits)
158 {
159         spin_lock(&i915->fb_tracking.lock);
160         /* Remove stale busy bits due to the old buffer. */
161         i915->fb_tracking.busy_bits &= ~frontbuffer_bits;
162         spin_unlock(&i915->fb_tracking.lock);
163
164         frontbuffer_flush(i915, frontbuffer_bits, ORIGIN_FLIP);
165 }
166
167 void __intel_fb_invalidate(struct intel_frontbuffer *front,
168                            enum fb_op_origin origin,
169                            unsigned int frontbuffer_bits)
170 {
171         struct drm_i915_private *i915 = to_i915(front->obj->base.dev);
172
173         if (origin == ORIGIN_CS) {
174                 spin_lock(&i915->fb_tracking.lock);
175                 i915->fb_tracking.busy_bits |= frontbuffer_bits;
176                 i915->fb_tracking.flip_bits &= ~frontbuffer_bits;
177                 spin_unlock(&i915->fb_tracking.lock);
178         }
179
180         trace_intel_frontbuffer_invalidate(frontbuffer_bits, origin);
181
182         might_sleep();
183         intel_psr_invalidate(i915, frontbuffer_bits, origin);
184         intel_drrs_invalidate(i915, frontbuffer_bits);
185         intel_fbc_invalidate(i915, frontbuffer_bits, origin);
186 }
187
188 void __intel_fb_flush(struct intel_frontbuffer *front,
189                       enum fb_op_origin origin,
190                       unsigned int frontbuffer_bits)
191 {
192         struct drm_i915_private *i915 = to_i915(front->obj->base.dev);
193
194         if (origin == ORIGIN_CS) {
195                 spin_lock(&i915->fb_tracking.lock);
196                 /* Filter out new bits since rendering started. */
197                 frontbuffer_bits &= i915->fb_tracking.busy_bits;
198                 i915->fb_tracking.busy_bits &= ~frontbuffer_bits;
199                 spin_unlock(&i915->fb_tracking.lock);
200         }
201
202         if (frontbuffer_bits)
203                 frontbuffer_flush(i915, frontbuffer_bits, origin);
204 }
205
206 static int frontbuffer_active(struct i915_active *ref)
207 {
208         struct intel_frontbuffer *front =
209                 container_of(ref, typeof(*front), write);
210
211         kref_get(&front->ref);
212         return 0;
213 }
214
215 static void frontbuffer_retire(struct i915_active *ref)
216 {
217         struct intel_frontbuffer *front =
218                 container_of(ref, typeof(*front), write);
219
220         intel_frontbuffer_flush(front, ORIGIN_CS);
221         intel_frontbuffer_put(front);
222 }
223
224 static void frontbuffer_release(struct kref *ref)
225         __releases(&to_i915(front->obj->base.dev)->fb_tracking.lock)
226 {
227         struct intel_frontbuffer *front =
228                 container_of(ref, typeof(*front), ref);
229         struct drm_i915_gem_object *obj = front->obj;
230         struct i915_vma *vma;
231
232         drm_WARN_ON(obj->base.dev, atomic_read(&front->bits));
233
234         spin_lock(&obj->vma.lock);
235         for_each_ggtt_vma(vma, obj) {
236                 i915_vma_clear_scanout(vma);
237                 vma->display_alignment = I915_GTT_MIN_ALIGNMENT;
238         }
239         spin_unlock(&obj->vma.lock);
240
241         RCU_INIT_POINTER(obj->frontbuffer, NULL);
242         spin_unlock(&to_i915(obj->base.dev)->fb_tracking.lock);
243
244         i915_active_fini(&front->write);
245
246         i915_gem_object_put(obj);
247         kfree_rcu(front, rcu);
248 }
249
250 struct intel_frontbuffer *
251 intel_frontbuffer_get(struct drm_i915_gem_object *obj)
252 {
253         struct drm_i915_private *i915 = to_i915(obj->base.dev);
254         struct intel_frontbuffer *front;
255
256         front = __intel_frontbuffer_get(obj);
257         if (front)
258                 return front;
259
260         front = kmalloc(sizeof(*front), GFP_KERNEL);
261         if (!front)
262                 return NULL;
263
264         front->obj = obj;
265         kref_init(&front->ref);
266         atomic_set(&front->bits, 0);
267         i915_active_init(&front->write,
268                          frontbuffer_active,
269                          frontbuffer_retire,
270                          I915_ACTIVE_RETIRE_SLEEPS);
271
272         spin_lock(&i915->fb_tracking.lock);
273         if (rcu_access_pointer(obj->frontbuffer)) {
274                 kfree(front);
275                 front = rcu_dereference_protected(obj->frontbuffer, true);
276                 kref_get(&front->ref);
277         } else {
278                 i915_gem_object_get(obj);
279                 rcu_assign_pointer(obj->frontbuffer, front);
280         }
281         spin_unlock(&i915->fb_tracking.lock);
282
283         return front;
284 }
285
286 void intel_frontbuffer_put(struct intel_frontbuffer *front)
287 {
288         kref_put_lock(&front->ref,
289                       frontbuffer_release,
290                       &to_i915(front->obj->base.dev)->fb_tracking.lock);
291 }
292
293 /**
294  * intel_frontbuffer_track - update frontbuffer tracking
295  * @old: current buffer for the frontbuffer slots
296  * @new: new buffer for the frontbuffer slots
297  * @frontbuffer_bits: bitmask of frontbuffer slots
298  *
299  * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
300  * from @old and setting them in @new. Both @old and @new can be NULL.
301  */
302 void intel_frontbuffer_track(struct intel_frontbuffer *old,
303                              struct intel_frontbuffer *new,
304                              unsigned int frontbuffer_bits)
305 {
306         /*
307          * Control of individual bits within the mask are guarded by
308          * the owning plane->mutex, i.e. we can never see concurrent
309          * manipulation of individual bits. But since the bitfield as a whole
310          * is updated using RMW, we need to use atomics in order to update
311          * the bits.
312          */
313         BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE * I915_MAX_PIPES >
314                      BITS_PER_TYPE(atomic_t));
315
316         if (old) {
317                 drm_WARN_ON(old->obj->base.dev,
318                             !(atomic_read(&old->bits) & frontbuffer_bits));
319                 atomic_andnot(frontbuffer_bits, &old->bits);
320         }
321
322         if (new) {
323                 drm_WARN_ON(new->obj->base.dev,
324                             atomic_read(&new->bits) & frontbuffer_bits);
325                 atomic_or(frontbuffer_bits, &new->bits);
326         }
327 }