HID: sensor-hub: Add quirk for Lenovo Yoga 900 with ITE Chips
[cascardo/linux.git] / drivers / gpu / drm / msm / mdp / mdp5 / mdp5_plane.c
1 /*
2  * Copyright (C) 2014-2015 The Linux Foundation. All rights reserved.
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published by
8  * the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "mdp5_kms.h"
20
21 struct mdp5_plane {
22         struct drm_plane base;
23         const char *name;
24
25         enum mdp5_pipe pipe;
26
27         spinlock_t pipe_lock;   /* protect REG_MDP5_PIPE_* registers */
28         uint32_t reg_offset;
29         uint32_t caps;
30
31         uint32_t flush_mask;    /* used to commit pipe registers */
32
33         uint32_t nformats;
34         uint32_t formats[32];
35 };
36 #define to_mdp5_plane(x) container_of(x, struct mdp5_plane, base)
37
38 static int mdp5_plane_mode_set(struct drm_plane *plane,
39                 struct drm_crtc *crtc, struct drm_framebuffer *fb,
40                 int crtc_x, int crtc_y,
41                 unsigned int crtc_w, unsigned int crtc_h,
42                 uint32_t src_x, uint32_t src_y,
43                 uint32_t src_w, uint32_t src_h);
44
45 static void set_scanout_locked(struct drm_plane *plane,
46                 struct drm_framebuffer *fb);
47
48 static struct mdp5_kms *get_kms(struct drm_plane *plane)
49 {
50         struct msm_drm_private *priv = plane->dev->dev_private;
51         return to_mdp5_kms(to_mdp_kms(priv->kms));
52 }
53
54 static bool plane_enabled(struct drm_plane_state *state)
55 {
56         return state->fb && state->crtc;
57 }
58
59 static void mdp5_plane_destroy(struct drm_plane *plane)
60 {
61         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
62
63         drm_plane_helper_disable(plane);
64         drm_plane_cleanup(plane);
65
66         kfree(mdp5_plane);
67 }
68
69 static void mdp5_plane_install_rotation_property(struct drm_device *dev,
70                 struct drm_plane *plane)
71 {
72         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
73
74         if (!(mdp5_plane->caps & MDP_PIPE_CAP_HFLIP) &&
75                 !(mdp5_plane->caps & MDP_PIPE_CAP_VFLIP))
76                 return;
77
78         if (!dev->mode_config.rotation_property)
79                 dev->mode_config.rotation_property =
80                         drm_mode_create_rotation_property(dev,
81                         BIT(DRM_REFLECT_X) | BIT(DRM_REFLECT_Y));
82
83         if (dev->mode_config.rotation_property)
84                 drm_object_attach_property(&plane->base,
85                         dev->mode_config.rotation_property,
86                         0);
87 }
88
89 /* helper to install properties which are common to planes and crtcs */
90 static void mdp5_plane_install_properties(struct drm_plane *plane,
91                 struct drm_mode_object *obj)
92 {
93         struct drm_device *dev = plane->dev;
94         struct msm_drm_private *dev_priv = dev->dev_private;
95         struct drm_property *prop;
96
97 #define INSTALL_PROPERTY(name, NAME, init_val, fnc, ...) do { \
98                 prop = dev_priv->plane_property[PLANE_PROP_##NAME]; \
99                 if (!prop) { \
100                         prop = drm_property_##fnc(dev, 0, #name, \
101                                 ##__VA_ARGS__); \
102                         if (!prop) { \
103                                 dev_warn(dev->dev, \
104                                         "Create property %s failed\n", \
105                                         #name); \
106                                 return; \
107                         } \
108                         dev_priv->plane_property[PLANE_PROP_##NAME] = prop; \
109                 } \
110                 drm_object_attach_property(&plane->base, prop, init_val); \
111         } while (0)
112
113 #define INSTALL_RANGE_PROPERTY(name, NAME, min, max, init_val) \
114                 INSTALL_PROPERTY(name, NAME, init_val, \
115                                 create_range, min, max)
116
117 #define INSTALL_ENUM_PROPERTY(name, NAME, init_val) \
118                 INSTALL_PROPERTY(name, NAME, init_val, \
119                                 create_enum, name##_prop_enum_list, \
120                                 ARRAY_SIZE(name##_prop_enum_list))
121
122         INSTALL_RANGE_PROPERTY(zpos, ZPOS, 1, 255, 1);
123
124         mdp5_plane_install_rotation_property(dev, plane);
125
126 #undef INSTALL_RANGE_PROPERTY
127 #undef INSTALL_ENUM_PROPERTY
128 #undef INSTALL_PROPERTY
129 }
130
131 static int mdp5_plane_atomic_set_property(struct drm_plane *plane,
132                 struct drm_plane_state *state, struct drm_property *property,
133                 uint64_t val)
134 {
135         struct drm_device *dev = plane->dev;
136         struct mdp5_plane_state *pstate;
137         struct msm_drm_private *dev_priv = dev->dev_private;
138         int ret = 0;
139
140         pstate = to_mdp5_plane_state(state);
141
142 #define SET_PROPERTY(name, NAME, type) do { \
143                 if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
144                         pstate->name = (type)val; \
145                         DBG("Set property %s %d", #name, (type)val); \
146                         goto done; \
147                 } \
148         } while (0)
149
150         SET_PROPERTY(zpos, ZPOS, uint8_t);
151
152         dev_err(dev->dev, "Invalid property\n");
153         ret = -EINVAL;
154 done:
155         return ret;
156 #undef SET_PROPERTY
157 }
158
159 static int mdp5_plane_atomic_get_property(struct drm_plane *plane,
160                 const struct drm_plane_state *state,
161                 struct drm_property *property, uint64_t *val)
162 {
163         struct drm_device *dev = plane->dev;
164         struct mdp5_plane_state *pstate;
165         struct msm_drm_private *dev_priv = dev->dev_private;
166         int ret = 0;
167
168         pstate = to_mdp5_plane_state(state);
169
170 #define GET_PROPERTY(name, NAME, type) do { \
171                 if (dev_priv->plane_property[PLANE_PROP_##NAME] == property) { \
172                         *val = pstate->name; \
173                         DBG("Get property %s %lld", #name, *val); \
174                         goto done; \
175                 } \
176         } while (0)
177
178         GET_PROPERTY(zpos, ZPOS, uint8_t);
179
180         dev_err(dev->dev, "Invalid property\n");
181         ret = -EINVAL;
182 done:
183         return ret;
184 #undef SET_PROPERTY
185 }
186
187 static void mdp5_plane_reset(struct drm_plane *plane)
188 {
189         struct mdp5_plane_state *mdp5_state;
190
191         if (plane->state && plane->state->fb)
192                 drm_framebuffer_unreference(plane->state->fb);
193
194         kfree(to_mdp5_plane_state(plane->state));
195         mdp5_state = kzalloc(sizeof(*mdp5_state), GFP_KERNEL);
196
197         /* assign default blend parameters */
198         mdp5_state->alpha = 255;
199         mdp5_state->premultiplied = 0;
200
201         if (plane->type == DRM_PLANE_TYPE_PRIMARY)
202                 mdp5_state->zpos = STAGE_BASE;
203         else
204                 mdp5_state->zpos = STAGE0 + drm_plane_index(plane);
205
206         mdp5_state->base.plane = plane;
207
208         plane->state = &mdp5_state->base;
209 }
210
211 static struct drm_plane_state *
212 mdp5_plane_duplicate_state(struct drm_plane *plane)
213 {
214         struct mdp5_plane_state *mdp5_state;
215
216         if (WARN_ON(!plane->state))
217                 return NULL;
218
219         mdp5_state = kmemdup(to_mdp5_plane_state(plane->state),
220                         sizeof(*mdp5_state), GFP_KERNEL);
221
222         if (mdp5_state && mdp5_state->base.fb)
223                 drm_framebuffer_reference(mdp5_state->base.fb);
224
225         mdp5_state->mode_changed = false;
226         mdp5_state->pending = false;
227
228         return &mdp5_state->base;
229 }
230
231 static void mdp5_plane_destroy_state(struct drm_plane *plane,
232                 struct drm_plane_state *state)
233 {
234         if (state->fb)
235                 drm_framebuffer_unreference(state->fb);
236
237         kfree(to_mdp5_plane_state(state));
238 }
239
240 static const struct drm_plane_funcs mdp5_plane_funcs = {
241                 .update_plane = drm_atomic_helper_update_plane,
242                 .disable_plane = drm_atomic_helper_disable_plane,
243                 .destroy = mdp5_plane_destroy,
244                 .set_property = drm_atomic_helper_plane_set_property,
245                 .atomic_set_property = mdp5_plane_atomic_set_property,
246                 .atomic_get_property = mdp5_plane_atomic_get_property,
247                 .reset = mdp5_plane_reset,
248                 .atomic_duplicate_state = mdp5_plane_duplicate_state,
249                 .atomic_destroy_state = mdp5_plane_destroy_state,
250 };
251
252 static int mdp5_plane_prepare_fb(struct drm_plane *plane,
253                 struct drm_framebuffer *fb,
254                 const struct drm_plane_state *new_state)
255 {
256         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
257         struct mdp5_kms *mdp5_kms = get_kms(plane);
258
259         DBG("%s: prepare: FB[%u]", mdp5_plane->name, fb->base.id);
260         return msm_framebuffer_prepare(fb, mdp5_kms->id);
261 }
262
263 static void mdp5_plane_cleanup_fb(struct drm_plane *plane,
264                 struct drm_framebuffer *fb,
265                 const struct drm_plane_state *old_state)
266 {
267         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
268         struct mdp5_kms *mdp5_kms = get_kms(plane);
269
270         DBG("%s: cleanup: FB[%u]", mdp5_plane->name, fb->base.id);
271         msm_framebuffer_cleanup(fb, mdp5_kms->id);
272 }
273
274 static int mdp5_plane_atomic_check(struct drm_plane *plane,
275                 struct drm_plane_state *state)
276 {
277         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
278         struct drm_plane_state *old_state = plane->state;
279         const struct mdp_format *format;
280         bool vflip, hflip;
281
282         DBG("%s: check (%d -> %d)", mdp5_plane->name,
283                         plane_enabled(old_state), plane_enabled(state));
284
285         if (plane_enabled(state)) {
286                 format = to_mdp_format(msm_framebuffer_format(state->fb));
287                 if (MDP_FORMAT_IS_YUV(format) &&
288                         !pipe_supports_yuv(mdp5_plane->caps)) {
289                         dev_err(plane->dev->dev,
290                                 "Pipe doesn't support YUV\n");
291
292                         return -EINVAL;
293                 }
294
295                 if (!(mdp5_plane->caps & MDP_PIPE_CAP_SCALE) &&
296                         (((state->src_w >> 16) != state->crtc_w) ||
297                         ((state->src_h >> 16) != state->crtc_h))) {
298                         dev_err(plane->dev->dev,
299                                 "Pipe doesn't support scaling (%dx%d -> %dx%d)\n",
300                                 state->src_w >> 16, state->src_h >> 16,
301                                 state->crtc_w, state->crtc_h);
302
303                         return -EINVAL;
304                 }
305
306                 hflip = !!(state->rotation & BIT(DRM_REFLECT_X));
307                 vflip = !!(state->rotation & BIT(DRM_REFLECT_Y));
308                 if ((vflip && !(mdp5_plane->caps & MDP_PIPE_CAP_VFLIP)) ||
309                         (hflip && !(mdp5_plane->caps & MDP_PIPE_CAP_HFLIP))) {
310                         dev_err(plane->dev->dev,
311                                 "Pipe doesn't support flip\n");
312
313                         return -EINVAL;
314                 }
315         }
316
317         if (plane_enabled(state) && plane_enabled(old_state)) {
318                 /* we cannot change SMP block configuration during scanout: */
319                 bool full_modeset = false;
320                 if (state->fb->pixel_format != old_state->fb->pixel_format) {
321                         DBG("%s: pixel_format change!", mdp5_plane->name);
322                         full_modeset = true;
323                 }
324                 if (state->src_w != old_state->src_w) {
325                         DBG("%s: src_w change!", mdp5_plane->name);
326                         full_modeset = true;
327                 }
328                 if (to_mdp5_plane_state(old_state)->pending) {
329                         DBG("%s: still pending!", mdp5_plane->name);
330                         full_modeset = true;
331                 }
332                 if (full_modeset) {
333                         struct drm_crtc_state *crtc_state =
334                                         drm_atomic_get_crtc_state(state->state, state->crtc);
335                         crtc_state->mode_changed = true;
336                         to_mdp5_plane_state(state)->mode_changed = true;
337                 }
338         } else {
339                 to_mdp5_plane_state(state)->mode_changed = true;
340         }
341
342         return 0;
343 }
344
345 static void mdp5_plane_atomic_update(struct drm_plane *plane,
346                                      struct drm_plane_state *old_state)
347 {
348         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
349         struct drm_plane_state *state = plane->state;
350
351         DBG("%s: update", mdp5_plane->name);
352
353         if (!plane_enabled(state)) {
354                 to_mdp5_plane_state(state)->pending = true;
355         } else if (to_mdp5_plane_state(state)->mode_changed) {
356                 int ret;
357                 to_mdp5_plane_state(state)->pending = true;
358                 ret = mdp5_plane_mode_set(plane,
359                                 state->crtc, state->fb,
360                                 state->crtc_x, state->crtc_y,
361                                 state->crtc_w, state->crtc_h,
362                                 state->src_x,  state->src_y,
363                                 state->src_w, state->src_h);
364                 /* atomic_check should have ensured that this doesn't fail */
365                 WARN_ON(ret < 0);
366         } else {
367                 unsigned long flags;
368                 spin_lock_irqsave(&mdp5_plane->pipe_lock, flags);
369                 set_scanout_locked(plane, state->fb);
370                 spin_unlock_irqrestore(&mdp5_plane->pipe_lock, flags);
371         }
372 }
373
374 static const struct drm_plane_helper_funcs mdp5_plane_helper_funcs = {
375                 .prepare_fb = mdp5_plane_prepare_fb,
376                 .cleanup_fb = mdp5_plane_cleanup_fb,
377                 .atomic_check = mdp5_plane_atomic_check,
378                 .atomic_update = mdp5_plane_atomic_update,
379 };
380
381 static void set_scanout_locked(struct drm_plane *plane,
382                 struct drm_framebuffer *fb)
383 {
384         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
385         struct mdp5_kms *mdp5_kms = get_kms(plane);
386         enum mdp5_pipe pipe = mdp5_plane->pipe;
387
388         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_A(pipe),
389                         MDP5_PIPE_SRC_STRIDE_A_P0(fb->pitches[0]) |
390                         MDP5_PIPE_SRC_STRIDE_A_P1(fb->pitches[1]));
391
392         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_STRIDE_B(pipe),
393                         MDP5_PIPE_SRC_STRIDE_B_P2(fb->pitches[2]) |
394                         MDP5_PIPE_SRC_STRIDE_B_P3(fb->pitches[3]));
395
396         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC0_ADDR(pipe),
397                         msm_framebuffer_iova(fb, mdp5_kms->id, 0));
398         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC1_ADDR(pipe),
399                         msm_framebuffer_iova(fb, mdp5_kms->id, 1));
400         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC2_ADDR(pipe),
401                         msm_framebuffer_iova(fb, mdp5_kms->id, 2));
402         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC3_ADDR(pipe),
403                         msm_framebuffer_iova(fb, mdp5_kms->id, 3));
404
405         plane->fb = fb;
406 }
407
408 /* Note: mdp5_plane->pipe_lock must be locked */
409 static void csc_disable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe)
410 {
411         uint32_t value = mdp5_read(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe)) &
412                          ~MDP5_PIPE_OP_MODE_CSC_1_EN;
413
414         mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), value);
415 }
416
417 /* Note: mdp5_plane->pipe_lock must be locked */
418 static void csc_enable(struct mdp5_kms *mdp5_kms, enum mdp5_pipe pipe,
419                 struct csc_cfg *csc)
420 {
421         uint32_t  i, mode = 0; /* RGB, no CSC */
422         uint32_t *matrix;
423
424         if (unlikely(!csc))
425                 return;
426
427         if ((csc->type == CSC_YUV2RGB) || (CSC_YUV2YUV == csc->type))
428                 mode |= MDP5_PIPE_OP_MODE_CSC_SRC_DATA_FORMAT(DATA_FORMAT_YUV);
429         if ((csc->type == CSC_RGB2YUV) || (CSC_YUV2YUV == csc->type))
430                 mode |= MDP5_PIPE_OP_MODE_CSC_DST_DATA_FORMAT(DATA_FORMAT_YUV);
431         mode |= MDP5_PIPE_OP_MODE_CSC_1_EN;
432         mdp5_write(mdp5_kms, REG_MDP5_PIPE_OP_MODE(pipe), mode);
433
434         matrix = csc->matrix;
435         mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_0(pipe),
436                         MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_11(matrix[0]) |
437                         MDP5_PIPE_CSC_1_MATRIX_COEFF_0_COEFF_12(matrix[1]));
438         mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_1(pipe),
439                         MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_13(matrix[2]) |
440                         MDP5_PIPE_CSC_1_MATRIX_COEFF_1_COEFF_21(matrix[3]));
441         mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_2(pipe),
442                         MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_22(matrix[4]) |
443                         MDP5_PIPE_CSC_1_MATRIX_COEFF_2_COEFF_23(matrix[5]));
444         mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_3(pipe),
445                         MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_31(matrix[6]) |
446                         MDP5_PIPE_CSC_1_MATRIX_COEFF_3_COEFF_32(matrix[7]));
447         mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_MATRIX_COEFF_4(pipe),
448                         MDP5_PIPE_CSC_1_MATRIX_COEFF_4_COEFF_33(matrix[8]));
449
450         for (i = 0; i < ARRAY_SIZE(csc->pre_bias); i++) {
451                 uint32_t *pre_clamp = csc->pre_clamp;
452                 uint32_t *post_clamp = csc->post_clamp;
453
454                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_CLAMP(pipe, i),
455                         MDP5_PIPE_CSC_1_PRE_CLAMP_REG_HIGH(pre_clamp[2*i+1]) |
456                         MDP5_PIPE_CSC_1_PRE_CLAMP_REG_LOW(pre_clamp[2*i]));
457
458                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_CLAMP(pipe, i),
459                         MDP5_PIPE_CSC_1_POST_CLAMP_REG_HIGH(post_clamp[2*i+1]) |
460                         MDP5_PIPE_CSC_1_POST_CLAMP_REG_LOW(post_clamp[2*i]));
461
462                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_PRE_BIAS(pipe, i),
463                         MDP5_PIPE_CSC_1_PRE_BIAS_REG_VALUE(csc->pre_bias[i]));
464
465                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_CSC_1_POST_BIAS(pipe, i),
466                         MDP5_PIPE_CSC_1_POST_BIAS_REG_VALUE(csc->post_bias[i]));
467         }
468 }
469
470 #define PHASE_STEP_SHIFT        21
471 #define DOWN_SCALE_RATIO_MAX    32      /* 2^(26-21) */
472
473 static int calc_phase_step(uint32_t src, uint32_t dst, uint32_t *out_phase)
474 {
475         uint32_t unit;
476
477         if (src == 0 || dst == 0)
478                 return -EINVAL;
479
480         /*
481          * PHASE_STEP_X/Y is coded on 26 bits (25:0),
482          * where 2^21 represents the unity "1" in fixed-point hardware design.
483          * This leaves 5 bits for the integer part (downscale case):
484          *      -> maximum downscale ratio = 0b1_1111 = 31
485          */
486         if (src > (dst * DOWN_SCALE_RATIO_MAX))
487                 return -EOVERFLOW;
488
489         unit = 1 << PHASE_STEP_SHIFT;
490         *out_phase = mult_frac(unit, src, dst);
491
492         return 0;
493 }
494
495 static int calc_scalex_steps(struct drm_plane *plane,
496                 uint32_t pixel_format, uint32_t src, uint32_t dest,
497                 uint32_t phasex_steps[2])
498 {
499         struct mdp5_kms *mdp5_kms = get_kms(plane);
500         struct device *dev = mdp5_kms->dev->dev;
501         uint32_t phasex_step;
502         unsigned int hsub;
503         int ret;
504
505         ret = calc_phase_step(src, dest, &phasex_step);
506         if (ret) {
507                 dev_err(dev, "X scaling (%d->%d) failed: %d\n", src, dest, ret);
508                 return ret;
509         }
510
511         hsub = drm_format_horz_chroma_subsampling(pixel_format);
512
513         phasex_steps[0] = phasex_step;
514         phasex_steps[1] = phasex_step / hsub;
515
516         return 0;
517 }
518
519 static int calc_scaley_steps(struct drm_plane *plane,
520                 uint32_t pixel_format, uint32_t src, uint32_t dest,
521                 uint32_t phasey_steps[2])
522 {
523         struct mdp5_kms *mdp5_kms = get_kms(plane);
524         struct device *dev = mdp5_kms->dev->dev;
525         uint32_t phasey_step;
526         unsigned int vsub;
527         int ret;
528
529         ret = calc_phase_step(src, dest, &phasey_step);
530         if (ret) {
531                 dev_err(dev, "Y scaling (%d->%d) failed: %d\n", src, dest, ret);
532                 return ret;
533         }
534
535         vsub = drm_format_vert_chroma_subsampling(pixel_format);
536
537         phasey_steps[0] = phasey_step;
538         phasey_steps[1] = phasey_step / vsub;
539
540         return 0;
541 }
542
543 static uint32_t get_scale_config(enum mdp_chroma_samp_type chroma_sample,
544                 uint32_t src, uint32_t dest, bool hor)
545 {
546         uint32_t y_filter =   (src <= dest) ? SCALE_FILTER_CA  : SCALE_FILTER_PCMN;
547         uint32_t y_a_filter = (src <= dest) ? SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
548         uint32_t uv_filter = ((src / 2) <= dest) ? /* 2x upsample */
549                         SCALE_FILTER_BIL : SCALE_FILTER_PCMN;
550         uint32_t value = 0;
551
552         if (chroma_sample == CHROMA_420 || chroma_sample == CHROMA_H2V1) {
553                 if (hor)
554                         value = MDP5_PIPE_SCALE_CONFIG_SCALEX_EN |
555                                 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_0(y_filter) |
556                                 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_3(y_a_filter) |
557                                 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_1_2(uv_filter);
558                 else
559                         value = MDP5_PIPE_SCALE_CONFIG_SCALEY_EN |
560                                 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_0(y_filter) |
561                                 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_3(y_a_filter) |
562                                 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_1_2(uv_filter);
563         } else if (src != dest) {
564                 if (hor)
565                         value = MDP5_PIPE_SCALE_CONFIG_SCALEX_EN |
566                                 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_0(y_a_filter) |
567                                 MDP5_PIPE_SCALE_CONFIG_SCALEX_FILTER_COMP_3(y_a_filter);
568                 else
569                         value = MDP5_PIPE_SCALE_CONFIG_SCALEY_EN |
570                                 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_0(y_a_filter) |
571                                 MDP5_PIPE_SCALE_CONFIG_SCALEY_FILTER_COMP_3(y_a_filter);
572         }
573
574         return value;
575 }
576
577 static int mdp5_plane_mode_set(struct drm_plane *plane,
578                 struct drm_crtc *crtc, struct drm_framebuffer *fb,
579                 int crtc_x, int crtc_y,
580                 unsigned int crtc_w, unsigned int crtc_h,
581                 uint32_t src_x, uint32_t src_y,
582                 uint32_t src_w, uint32_t src_h)
583 {
584         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
585         struct drm_plane_state *pstate = plane->state;
586         struct mdp5_kms *mdp5_kms = get_kms(plane);
587         enum mdp5_pipe pipe = mdp5_plane->pipe;
588         const struct mdp_format *format;
589         uint32_t nplanes, config = 0;
590         /* below array -> index 0: comp 0/3 ; index 1: comp 1/2 */
591         uint32_t phasex_step[2] = {0,}, phasey_step[2] = {0,};
592         uint32_t hdecm = 0, vdecm = 0;
593         uint32_t pix_format;
594         bool vflip, hflip;
595         unsigned long flags;
596         int ret;
597
598         nplanes = drm_format_num_planes(fb->pixel_format);
599
600         /* bad formats should already be rejected: */
601         if (WARN_ON(nplanes > pipe2nclients(pipe)))
602                 return -EINVAL;
603
604         format = to_mdp_format(msm_framebuffer_format(fb));
605         pix_format = format->base.pixel_format;
606
607         /* src values are in Q16 fixed point, convert to integer: */
608         src_x = src_x >> 16;
609         src_y = src_y >> 16;
610         src_w = src_w >> 16;
611         src_h = src_h >> 16;
612
613         DBG("%s: FB[%u] %u,%u,%u,%u -> CRTC[%u] %d,%d,%u,%u", mdp5_plane->name,
614                         fb->base.id, src_x, src_y, src_w, src_h,
615                         crtc->base.id, crtc_x, crtc_y, crtc_w, crtc_h);
616
617         /* Request some memory from the SMP: */
618         ret = mdp5_smp_request(mdp5_kms->smp,
619                         mdp5_plane->pipe, format, src_w, false);
620         if (ret)
621                 return ret;
622
623         /*
624          * Currently we update the hw for allocations/requests immediately,
625          * but once atomic modeset/pageflip is in place, the allocation
626          * would move into atomic->check_plane_state(), while updating the
627          * hw would remain here:
628          */
629         mdp5_smp_configure(mdp5_kms->smp, pipe);
630
631         ret = calc_scalex_steps(plane, pix_format, src_w, crtc_w, phasex_step);
632         if (ret)
633                 return ret;
634
635         ret = calc_scaley_steps(plane, pix_format, src_h, crtc_h, phasey_step);
636         if (ret)
637                 return ret;
638
639         /* TODO calc hdecm, vdecm */
640
641         /* SCALE is used to both scale and up-sample chroma components */
642         config |= get_scale_config(format->chroma_sample, src_w, crtc_w, true);
643         config |= get_scale_config(format->chroma_sample, src_h, crtc_h, false);
644         DBG("scale config = %x", config);
645
646         hflip = !!(pstate->rotation & BIT(DRM_REFLECT_X));
647         vflip = !!(pstate->rotation & BIT(DRM_REFLECT_Y));
648
649         spin_lock_irqsave(&mdp5_plane->pipe_lock, flags);
650
651         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_IMG_SIZE(pipe),
652                         MDP5_PIPE_SRC_IMG_SIZE_WIDTH(fb->width) |
653                         MDP5_PIPE_SRC_IMG_SIZE_HEIGHT(fb->height));
654
655         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_SIZE(pipe),
656                         MDP5_PIPE_SRC_SIZE_WIDTH(src_w) |
657                         MDP5_PIPE_SRC_SIZE_HEIGHT(src_h));
658
659         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_XY(pipe),
660                         MDP5_PIPE_SRC_XY_X(src_x) |
661                         MDP5_PIPE_SRC_XY_Y(src_y));
662
663         mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_SIZE(pipe),
664                         MDP5_PIPE_OUT_SIZE_WIDTH(crtc_w) |
665                         MDP5_PIPE_OUT_SIZE_HEIGHT(crtc_h));
666
667         mdp5_write(mdp5_kms, REG_MDP5_PIPE_OUT_XY(pipe),
668                         MDP5_PIPE_OUT_XY_X(crtc_x) |
669                         MDP5_PIPE_OUT_XY_Y(crtc_y));
670
671         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_FORMAT(pipe),
672                         MDP5_PIPE_SRC_FORMAT_A_BPC(format->bpc_a) |
673                         MDP5_PIPE_SRC_FORMAT_R_BPC(format->bpc_r) |
674                         MDP5_PIPE_SRC_FORMAT_G_BPC(format->bpc_g) |
675                         MDP5_PIPE_SRC_FORMAT_B_BPC(format->bpc_b) |
676                         COND(format->alpha_enable, MDP5_PIPE_SRC_FORMAT_ALPHA_ENABLE) |
677                         MDP5_PIPE_SRC_FORMAT_CPP(format->cpp - 1) |
678                         MDP5_PIPE_SRC_FORMAT_UNPACK_COUNT(format->unpack_count - 1) |
679                         COND(format->unpack_tight, MDP5_PIPE_SRC_FORMAT_UNPACK_TIGHT) |
680                         MDP5_PIPE_SRC_FORMAT_FETCH_TYPE(format->fetch_type) |
681                         MDP5_PIPE_SRC_FORMAT_CHROMA_SAMP(format->chroma_sample));
682
683         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_UNPACK(pipe),
684                         MDP5_PIPE_SRC_UNPACK_ELEM0(format->unpack[0]) |
685                         MDP5_PIPE_SRC_UNPACK_ELEM1(format->unpack[1]) |
686                         MDP5_PIPE_SRC_UNPACK_ELEM2(format->unpack[2]) |
687                         MDP5_PIPE_SRC_UNPACK_ELEM3(format->unpack[3]));
688
689         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_OP_MODE(pipe),
690                         (hflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_LR : 0) |
691                         (vflip ? MDP5_PIPE_SRC_OP_MODE_FLIP_UD : 0) |
692                         MDP5_PIPE_SRC_OP_MODE_BWC(BWC_LOSSLESS));
693
694         /* not using secure mode: */
695         mdp5_write(mdp5_kms, REG_MDP5_PIPE_SRC_ADDR_SW_STATUS(pipe), 0);
696
697         if (mdp5_plane->caps & MDP_PIPE_CAP_SCALE) {
698                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_X(pipe),
699                                 phasex_step[0]);
700                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_PHASE_STEP_Y(pipe),
701                                 phasey_step[0]);
702                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_X(pipe),
703                                 phasex_step[1]);
704                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CR_PHASE_STEP_Y(pipe),
705                                 phasey_step[1]);
706                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_DECIMATION(pipe),
707                                 MDP5_PIPE_DECIMATION_VERT(vdecm) |
708                                 MDP5_PIPE_DECIMATION_HORZ(hdecm));
709                 mdp5_write(mdp5_kms, REG_MDP5_PIPE_SCALE_CONFIG(pipe), config);
710         }
711
712         if (mdp5_plane->caps & MDP_PIPE_CAP_CSC) {
713                 if (MDP_FORMAT_IS_YUV(format))
714                         csc_enable(mdp5_kms, pipe,
715                                         mdp_get_default_csc_cfg(CSC_YUV2RGB));
716                 else
717                         csc_disable(mdp5_kms, pipe);
718         }
719
720         set_scanout_locked(plane, fb);
721
722         spin_unlock_irqrestore(&mdp5_plane->pipe_lock, flags);
723
724         return ret;
725 }
726
727 void mdp5_plane_complete_flip(struct drm_plane *plane)
728 {
729         struct mdp5_kms *mdp5_kms = get_kms(plane);
730         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
731         enum mdp5_pipe pipe = mdp5_plane->pipe;
732
733         DBG("%s: complete flip", mdp5_plane->name);
734
735         mdp5_smp_commit(mdp5_kms->smp, pipe);
736
737         to_mdp5_plane_state(plane->state)->pending = false;
738 }
739
740 enum mdp5_pipe mdp5_plane_pipe(struct drm_plane *plane)
741 {
742         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
743         return mdp5_plane->pipe;
744 }
745
746 uint32_t mdp5_plane_get_flush(struct drm_plane *plane)
747 {
748         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
749
750         return mdp5_plane->flush_mask;
751 }
752
753 /* called after vsync in thread context */
754 void mdp5_plane_complete_commit(struct drm_plane *plane,
755         struct drm_plane_state *state)
756 {
757         struct mdp5_kms *mdp5_kms = get_kms(plane);
758         struct mdp5_plane *mdp5_plane = to_mdp5_plane(plane);
759         enum mdp5_pipe pipe = mdp5_plane->pipe;
760
761         if (!plane_enabled(plane->state)) {
762                 DBG("%s: free SMP", mdp5_plane->name);
763                 mdp5_smp_release(mdp5_kms->smp, pipe);
764         }
765 }
766
767 /* initialize plane */
768 struct drm_plane *mdp5_plane_init(struct drm_device *dev,
769                 enum mdp5_pipe pipe, bool private_plane, uint32_t reg_offset,
770                 uint32_t caps)
771 {
772         struct drm_plane *plane = NULL;
773         struct mdp5_plane *mdp5_plane;
774         int ret;
775         enum drm_plane_type type;
776
777         mdp5_plane = kzalloc(sizeof(*mdp5_plane), GFP_KERNEL);
778         if (!mdp5_plane) {
779                 ret = -ENOMEM;
780                 goto fail;
781         }
782
783         plane = &mdp5_plane->base;
784
785         mdp5_plane->pipe = pipe;
786         mdp5_plane->name = pipe2name(pipe);
787         mdp5_plane->caps = caps;
788
789         mdp5_plane->nformats = mdp_get_formats(mdp5_plane->formats,
790                 ARRAY_SIZE(mdp5_plane->formats),
791                 !pipe_supports_yuv(mdp5_plane->caps));
792
793         mdp5_plane->flush_mask = mdp_ctl_flush_mask_pipe(pipe);
794         mdp5_plane->reg_offset = reg_offset;
795         spin_lock_init(&mdp5_plane->pipe_lock);
796
797         type = private_plane ? DRM_PLANE_TYPE_PRIMARY : DRM_PLANE_TYPE_OVERLAY;
798         ret = drm_universal_plane_init(dev, plane, 0xff, &mdp5_plane_funcs,
799                                  mdp5_plane->formats, mdp5_plane->nformats,
800                                  type);
801         if (ret)
802                 goto fail;
803
804         drm_plane_helper_add(plane, &mdp5_plane_helper_funcs);
805
806         mdp5_plane_install_properties(plane, &plane->base);
807
808         return plane;
809
810 fail:
811         if (plane)
812                 mdp5_plane_destroy(plane);
813
814         return ERR_PTR(ret);
815 }