Merge tag 'drm-intel-next-2019-04-04' into gvt-next
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / intel_atomic.c
1 /*
2  * Copyright © 2015 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
24 /**
25  * DOC: atomic modeset support
26  *
27  * The functions here implement the state management and hardware programming
28  * dispatch required by the atomic modeset infrastructure.
29  * See intel_atomic_plane.c for the plane-specific atomic functionality.
30  */
31
32 #include <drm/drm_atomic.h>
33 #include <drm/drm_atomic_helper.h>
34 #include <drm/drm_fourcc.h>
35 #include <drm/drm_plane_helper.h>
36
37 #include "intel_drv.h"
38
39 /**
40  * intel_digital_connector_atomic_get_property - hook for connector->atomic_get_property.
41  * @connector: Connector to get the property for.
42  * @state: Connector state to retrieve the property from.
43  * @property: Property to retrieve.
44  * @val: Return value for the property.
45  *
46  * Returns the atomic property value for a digital connector.
47  */
48 int intel_digital_connector_atomic_get_property(struct drm_connector *connector,
49                                                 const struct drm_connector_state *state,
50                                                 struct drm_property *property,
51                                                 u64 *val)
52 {
53         struct drm_device *dev = connector->dev;
54         struct drm_i915_private *dev_priv = to_i915(dev);
55         struct intel_digital_connector_state *intel_conn_state =
56                 to_intel_digital_connector_state(state);
57
58         if (property == dev_priv->force_audio_property)
59                 *val = intel_conn_state->force_audio;
60         else if (property == dev_priv->broadcast_rgb_property)
61                 *val = intel_conn_state->broadcast_rgb;
62         else {
63                 DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n",
64                                  property->base.id, property->name);
65                 return -EINVAL;
66         }
67
68         return 0;
69 }
70
71 /**
72  * intel_digital_connector_atomic_set_property - hook for connector->atomic_set_property.
73  * @connector: Connector to set the property for.
74  * @state: Connector state to set the property on.
75  * @property: Property to set.
76  * @val: New value for the property.
77  *
78  * Sets the atomic property value for a digital connector.
79  */
80 int intel_digital_connector_atomic_set_property(struct drm_connector *connector,
81                                                 struct drm_connector_state *state,
82                                                 struct drm_property *property,
83                                                 u64 val)
84 {
85         struct drm_device *dev = connector->dev;
86         struct drm_i915_private *dev_priv = to_i915(dev);
87         struct intel_digital_connector_state *intel_conn_state =
88                 to_intel_digital_connector_state(state);
89
90         if (property == dev_priv->force_audio_property) {
91                 intel_conn_state->force_audio = val;
92                 return 0;
93         }
94
95         if (property == dev_priv->broadcast_rgb_property) {
96                 intel_conn_state->broadcast_rgb = val;
97                 return 0;
98         }
99
100         DRM_DEBUG_ATOMIC("Unknown property [PROP:%d:%s]\n",
101                          property->base.id, property->name);
102         return -EINVAL;
103 }
104
105 int intel_digital_connector_atomic_check(struct drm_connector *conn,
106                                          struct drm_connector_state *new_state)
107 {
108         struct intel_digital_connector_state *new_conn_state =
109                 to_intel_digital_connector_state(new_state);
110         struct drm_connector_state *old_state =
111                 drm_atomic_get_old_connector_state(new_state->state, conn);
112         struct intel_digital_connector_state *old_conn_state =
113                 to_intel_digital_connector_state(old_state);
114         struct drm_crtc_state *crtc_state;
115
116         intel_hdcp_atomic_check(conn, old_state, new_state);
117
118         if (!new_state->crtc)
119                 return 0;
120
121         crtc_state = drm_atomic_get_new_crtc_state(new_state->state, new_state->crtc);
122
123         /*
124          * These properties are handled by fastset, and might not end
125          * up in a modeset.
126          */
127         if (new_conn_state->force_audio != old_conn_state->force_audio ||
128             new_conn_state->broadcast_rgb != old_conn_state->broadcast_rgb ||
129             new_conn_state->base.colorspace != old_conn_state->base.colorspace ||
130             new_conn_state->base.picture_aspect_ratio != old_conn_state->base.picture_aspect_ratio ||
131             new_conn_state->base.content_type != old_conn_state->base.content_type ||
132             new_conn_state->base.scaling_mode != old_conn_state->base.scaling_mode)
133                 crtc_state->mode_changed = true;
134
135         return 0;
136 }
137
138 /**
139  * intel_digital_connector_duplicate_state - duplicate connector state
140  * @connector: digital connector
141  *
142  * Allocates and returns a copy of the connector state (both common and
143  * digital connector specific) for the specified connector.
144  *
145  * Returns: The newly allocated connector state, or NULL on failure.
146  */
147 struct drm_connector_state *
148 intel_digital_connector_duplicate_state(struct drm_connector *connector)
149 {
150         struct intel_digital_connector_state *state;
151
152         state = kmemdup(connector->state, sizeof(*state), GFP_KERNEL);
153         if (!state)
154                 return NULL;
155
156         __drm_atomic_helper_connector_duplicate_state(connector, &state->base);
157         return &state->base;
158 }
159
160 /**
161  * intel_crtc_duplicate_state - duplicate crtc state
162  * @crtc: drm crtc
163  *
164  * Allocates and returns a copy of the crtc state (both common and
165  * Intel-specific) for the specified crtc.
166  *
167  * Returns: The newly allocated crtc state, or NULL on failure.
168  */
169 struct drm_crtc_state *
170 intel_crtc_duplicate_state(struct drm_crtc *crtc)
171 {
172         struct intel_crtc_state *crtc_state;
173
174         crtc_state = kmemdup(crtc->state, sizeof(*crtc_state), GFP_KERNEL);
175         if (!crtc_state)
176                 return NULL;
177
178         __drm_atomic_helper_crtc_duplicate_state(crtc, &crtc_state->base);
179
180         crtc_state->update_pipe = false;
181         crtc_state->disable_lp_wm = false;
182         crtc_state->disable_cxsr = false;
183         crtc_state->update_wm_pre = false;
184         crtc_state->update_wm_post = false;
185         crtc_state->fb_changed = false;
186         crtc_state->fifo_changed = false;
187         crtc_state->wm.need_postvbl_update = false;
188         crtc_state->fb_bits = 0;
189         crtc_state->update_planes = 0;
190
191         return &crtc_state->base;
192 }
193
194 /**
195  * intel_crtc_destroy_state - destroy crtc state
196  * @crtc: drm crtc
197  * @state: the state to destroy
198  *
199  * Destroys the crtc state (both common and Intel-specific) for the
200  * specified crtc.
201  */
202 void
203 intel_crtc_destroy_state(struct drm_crtc *crtc,
204                          struct drm_crtc_state *state)
205 {
206         drm_atomic_helper_crtc_destroy_state(crtc, state);
207 }
208
209 static void intel_atomic_setup_scaler(struct intel_crtc_scaler_state *scaler_state,
210                                       int num_scalers_need, struct intel_crtc *intel_crtc,
211                                       const char *name, int idx,
212                                       struct intel_plane_state *plane_state,
213                                       int *scaler_id)
214 {
215         struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
216         int j;
217         u32 mode;
218
219         if (*scaler_id < 0) {
220                 /* find a free scaler */
221                 for (j = 0; j < intel_crtc->num_scalers; j++) {
222                         if (scaler_state->scalers[j].in_use)
223                                 continue;
224
225                         *scaler_id = j;
226                         scaler_state->scalers[*scaler_id].in_use = 1;
227                         break;
228                 }
229         }
230
231         if (WARN(*scaler_id < 0, "Cannot find scaler for %s:%d\n", name, idx))
232                 return;
233
234         /* set scaler mode */
235         if (plane_state && plane_state->base.fb &&
236             plane_state->base.fb->format->is_yuv &&
237             plane_state->base.fb->format->num_planes > 1) {
238                 struct intel_plane *plane = to_intel_plane(plane_state->base.plane);
239                 if (IS_GEN(dev_priv, 9) &&
240                     !IS_GEMINILAKE(dev_priv)) {
241                         mode = SKL_PS_SCALER_MODE_NV12;
242                 } else if (icl_is_hdr_plane(dev_priv, plane->id)) {
243                         /*
244                          * On gen11+'s HDR planes we only use the scaler for
245                          * scaling. They have a dedicated chroma upsampler, so
246                          * we don't need the scaler to upsample the UV plane.
247                          */
248                         mode = PS_SCALER_MODE_NORMAL;
249                 } else {
250                         mode = PS_SCALER_MODE_PLANAR;
251
252                         if (plane_state->linked_plane)
253                                 mode |= PS_PLANE_Y_SEL(plane_state->linked_plane->id);
254                 }
255         } else if (INTEL_GEN(dev_priv) > 9 || IS_GEMINILAKE(dev_priv)) {
256                 mode = PS_SCALER_MODE_NORMAL;
257         } else if (num_scalers_need == 1 && intel_crtc->num_scalers > 1) {
258                 /*
259                  * when only 1 scaler is in use on a pipe with 2 scalers
260                  * scaler 0 operates in high quality (HQ) mode.
261                  * In this case use scaler 0 to take advantage of HQ mode
262                  */
263                 scaler_state->scalers[*scaler_id].in_use = 0;
264                 *scaler_id = 0;
265                 scaler_state->scalers[0].in_use = 1;
266                 mode = SKL_PS_SCALER_MODE_HQ;
267         } else {
268                 mode = SKL_PS_SCALER_MODE_DYN;
269         }
270
271         DRM_DEBUG_KMS("Attached scaler id %u.%u to %s:%d\n",
272                       intel_crtc->pipe, *scaler_id, name, idx);
273         scaler_state->scalers[*scaler_id].mode = mode;
274 }
275
276 /**
277  * intel_atomic_setup_scalers() - setup scalers for crtc per staged requests
278  * @dev_priv: i915 device
279  * @intel_crtc: intel crtc
280  * @crtc_state: incoming crtc_state to validate and setup scalers
281  *
282  * This function sets up scalers based on staged scaling requests for
283  * a @crtc and its planes. It is called from crtc level check path. If request
284  * is a supportable request, it attaches scalers to requested planes and crtc.
285  *
286  * This function takes into account the current scaler(s) in use by any planes
287  * not being part of this atomic state
288  *
289  *  Returns:
290  *         0 - scalers were setup succesfully
291  *         error code - otherwise
292  */
293 int intel_atomic_setup_scalers(struct drm_i915_private *dev_priv,
294                                struct intel_crtc *intel_crtc,
295                                struct intel_crtc_state *crtc_state)
296 {
297         struct drm_plane *plane = NULL;
298         struct intel_plane *intel_plane;
299         struct intel_plane_state *plane_state = NULL;
300         struct intel_crtc_scaler_state *scaler_state =
301                 &crtc_state->scaler_state;
302         struct drm_atomic_state *drm_state = crtc_state->base.state;
303         struct intel_atomic_state *intel_state = to_intel_atomic_state(drm_state);
304         int num_scalers_need;
305         int i;
306
307         num_scalers_need = hweight32(scaler_state->scaler_users);
308
309         /*
310          * High level flow:
311          * - staged scaler requests are already in scaler_state->scaler_users
312          * - check whether staged scaling requests can be supported
313          * - add planes using scalers that aren't in current transaction
314          * - assign scalers to requested users
315          * - as part of plane commit, scalers will be committed
316          *   (i.e., either attached or detached) to respective planes in hw
317          * - as part of crtc_commit, scaler will be either attached or detached
318          *   to crtc in hw
319          */
320
321         /* fail if required scalers > available scalers */
322         if (num_scalers_need > intel_crtc->num_scalers){
323                 DRM_DEBUG_KMS("Too many scaling requests %d > %d\n",
324                         num_scalers_need, intel_crtc->num_scalers);
325                 return -EINVAL;
326         }
327
328         /* walkthrough scaler_users bits and start assigning scalers */
329         for (i = 0; i < sizeof(scaler_state->scaler_users) * 8; i++) {
330                 int *scaler_id;
331                 const char *name;
332                 int idx;
333
334                 /* skip if scaler not required */
335                 if (!(scaler_state->scaler_users & (1 << i)))
336                         continue;
337
338                 if (i == SKL_CRTC_INDEX) {
339                         name = "CRTC";
340                         idx = intel_crtc->base.base.id;
341
342                         /* panel fitter case: assign as a crtc scaler */
343                         scaler_id = &scaler_state->scaler_id;
344                 } else {
345                         name = "PLANE";
346
347                         /* plane scaler case: assign as a plane scaler */
348                         /* find the plane that set the bit as scaler_user */
349                         plane = drm_state->planes[i].ptr;
350
351                         /*
352                          * to enable/disable hq mode, add planes that are using scaler
353                          * into this transaction
354                          */
355                         if (!plane) {
356                                 struct drm_plane_state *state;
357                                 plane = drm_plane_from_index(&dev_priv->drm, i);
358                                 state = drm_atomic_get_plane_state(drm_state, plane);
359                                 if (IS_ERR(state)) {
360                                         DRM_DEBUG_KMS("Failed to add [PLANE:%d] to drm_state\n",
361                                                 plane->base.id);
362                                         return PTR_ERR(state);
363                                 }
364
365                                 /*
366                                  * the plane is added after plane checks are run,
367                                  * but since this plane is unchanged just do the
368                                  * minimum required validation.
369                                  */
370                                 crtc_state->base.planes_changed = true;
371                         }
372
373                         intel_plane = to_intel_plane(plane);
374                         idx = plane->base.id;
375
376                         /* plane on different crtc cannot be a scaler user of this crtc */
377                         if (WARN_ON(intel_plane->pipe != intel_crtc->pipe))
378                                 continue;
379
380                         plane_state = intel_atomic_get_new_plane_state(intel_state,
381                                                                        intel_plane);
382                         scaler_id = &plane_state->scaler_id;
383                 }
384
385                 intel_atomic_setup_scaler(scaler_state, num_scalers_need,
386                                           intel_crtc, name, idx,
387                                           plane_state, scaler_id);
388         }
389
390         return 0;
391 }
392
393 struct drm_atomic_state *
394 intel_atomic_state_alloc(struct drm_device *dev)
395 {
396         struct intel_atomic_state *state = kzalloc(sizeof(*state), GFP_KERNEL);
397
398         if (!state || drm_atomic_state_init(dev, &state->base) < 0) {
399                 kfree(state);
400                 return NULL;
401         }
402
403         return &state->base;
404 }
405
406 void intel_atomic_state_clear(struct drm_atomic_state *s)
407 {
408         struct intel_atomic_state *state = to_intel_atomic_state(s);
409         drm_atomic_state_default_clear(&state->base);
410         state->dpll_set = state->modeset = false;
411 }