Merge remote-tracking branches 'asoc/topic/wm8753', 'asoc/topic/wm8770', 'asoc/topic...
[linux-block.git] / drivers / gpu / drm / msm / mdp / mdp5 / mdp5_kms.c
CommitLineData
06c0dd96 1/*
2e362e17 2 * Copyright (c) 2014, The Linux Foundation. All rights reserved.
06c0dd96
RC
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
aec095ec 19#include <linux/of_irq.h>
06c0dd96
RC
20
21#include "msm_drv.h"
667ce33e 22#include "msm_gem.h"
06c0dd96
RC
23#include "msm_mmu.h"
24#include "mdp5_kms.h"
25
87e956e9
SV
26static const char *iommu_ports[] = {
27 "mdp_0",
28};
29
3d47fd47
SV
30static int mdp5_hw_init(struct msm_kms *kms)
31{
32 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
d68fe15b 33 struct device *dev = &mdp5_kms->pdev->dev;
0deed25b 34 unsigned long flags;
3d47fd47 35
d68fe15b 36 pm_runtime_get_sync(dev);
3d47fd47 37
06c0dd96
RC
38 /* Magic unknown register writes:
39 *
40 * W VBIF:0x004 00000001 (mdss_mdp.c:839)
41 * W MDP5:0x2e0 0xe9 (mdss_mdp.c:839)
42 * W MDP5:0x2e4 0x55 (mdss_mdp.c:839)
43 * W MDP5:0x3ac 0xc0000ccc (mdss_mdp.c:839)
44 * W MDP5:0x3b4 0xc0000ccc (mdss_mdp.c:839)
45 * W MDP5:0x3bc 0xcccccc (mdss_mdp.c:839)
46 * W MDP5:0x4a8 0xcccc0c0 (mdss_mdp.c:839)
47 * W MDP5:0x4b0 0xccccc0c0 (mdss_mdp.c:839)
48 * W MDP5:0x4b8 0xccccc000 (mdss_mdp.c:839)
49 *
50 * Downstream fbdev driver gets these register offsets/values
51 * from DT.. not really sure what these registers are or if
52 * different values for different boards/SoC's, etc. I guess
53 * they are the golden registers.
54 *
55 * Not setting these does not seem to cause any problem. But
56 * we may be getting lucky with the bootloader initializing
57 * them for us. OTOH, if we can always count on the bootloader
58 * setting the golden registers, then perhaps we don't need to
59 * care.
60 */
61
0deed25b 62 spin_lock_irqsave(&mdp5_kms->resource_lock, flags);
7b59c7e4 63 mdp5_write(mdp5_kms, REG_MDP5_DISP_INTF_SEL, 0);
0deed25b 64 spin_unlock_irqrestore(&mdp5_kms->resource_lock, flags);
06c0dd96 65
42238da8 66 mdp5_ctlm_hw_reset(mdp5_kms->ctlm);
3d47fd47 67
d68fe15b 68 pm_runtime_put_sync(dev);
06c0dd96 69
3d47fd47 70 return 0;
06c0dd96
RC
71}
72
ac2a3fd3
RC
73struct mdp5_state *mdp5_get_state(struct drm_atomic_state *s)
74{
75 struct msm_drm_private *priv = s->dev->dev_private;
76 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(priv->kms));
77 struct msm_kms_state *state = to_kms_state(s);
78 struct mdp5_state *new_state;
79 int ret;
80
81 if (state->state)
82 return state->state;
83
84 ret = drm_modeset_lock(&mdp5_kms->state_lock, s->acquire_ctx);
85 if (ret)
86 return ERR_PTR(ret);
87
88 new_state = kmalloc(sizeof(*mdp5_kms->state), GFP_KERNEL);
89 if (!new_state)
90 return ERR_PTR(-ENOMEM);
91
92 /* Copy state: */
4a0f012d 93 new_state->hwpipe = mdp5_kms->state->hwpipe;
894558ec 94 new_state->hwmixer = mdp5_kms->state->hwmixer;
49ec5b2e
RC
95 if (mdp5_kms->smp)
96 new_state->smp = mdp5_kms->state->smp;
ac2a3fd3
RC
97
98 state->state = new_state;
99
100 return new_state;
101}
102
103static void mdp5_swap_state(struct msm_kms *kms, struct drm_atomic_state *state)
104{
105 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
106 swap(to_kms_state(state)->state, mdp5_kms->state);
107}
108
0b776d45
RC
109static void mdp5_prepare_commit(struct msm_kms *kms, struct drm_atomic_state *state)
110{
111 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
d68fe15b 112 struct device *dev = &mdp5_kms->pdev->dev;
49ec5b2e 113
d68fe15b 114 pm_runtime_get_sync(dev);
49ec5b2e
RC
115
116 if (mdp5_kms->smp)
117 mdp5_smp_prepare_commit(mdp5_kms->smp, &mdp5_kms->state->smp);
0b776d45
RC
118}
119
120static void mdp5_complete_commit(struct msm_kms *kms, struct drm_atomic_state *state)
121{
122 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
d68fe15b 123 struct device *dev = &mdp5_kms->pdev->dev;
657c63f0 124
49ec5b2e
RC
125 if (mdp5_kms->smp)
126 mdp5_smp_complete_commit(mdp5_kms->smp, &mdp5_kms->state->smp);
127
3c352b66 128 pm_runtime_put_sync(dev);
0b776d45
RC
129}
130
0a5c9aad
HL
131static void mdp5_wait_for_crtc_commit_done(struct msm_kms *kms,
132 struct drm_crtc *crtc)
133{
134 mdp5_crtc_wait_for_commit_done(crtc);
135}
136
06c0dd96
RC
137static long mdp5_round_pixclk(struct msm_kms *kms, unsigned long rate,
138 struct drm_encoder *encoder)
139{
140 return rate;
141}
142
d5af49c9
HL
143static int mdp5_set_split_display(struct msm_kms *kms,
144 struct drm_encoder *encoder,
145 struct drm_encoder *slave_encoder,
146 bool is_cmd_mode)
147{
148 if (is_cmd_mode)
149 return mdp5_cmd_encoder_set_split_display(encoder,
150 slave_encoder);
151 else
df8a71d2
AT
152 return mdp5_vid_encoder_set_split_display(encoder,
153 slave_encoder);
d5af49c9
HL
154}
155
9c9f6f8d
AT
156static void mdp5_set_encoder_mode(struct msm_kms *kms,
157 struct drm_encoder *encoder,
158 bool cmd_mode)
159{
160 mdp5_encoder_set_intf_mode(encoder, cmd_mode);
161}
162
1dd0a0b1 163static void mdp5_kms_destroy(struct msm_kms *kms)
06c0dd96
RC
164{
165 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
f59f62d5 166 struct msm_gem_address_space *aspace = kms->aspace;
c056b55d
RC
167 int i;
168
6803c606
AT
169 for (i = 0; i < mdp5_kms->num_hwmixers; i++)
170 mdp5_mixer_destroy(mdp5_kms->hwmixers[i]);
171
c056b55d
RC
172 for (i = 0; i < mdp5_kms->num_hwpipes; i++)
173 mdp5_pipe_destroy(mdp5_kms->hwpipes[i]);
87e956e9 174
667ce33e
RC
175 if (aspace) {
176 aspace->mmu->funcs->detach(aspace->mmu,
177 iommu_ports, ARRAY_SIZE(iommu_ports));
ee546cd3 178 msm_gem_address_space_put(aspace);
87e956e9 179 }
aec095ec
AT
180}
181
bc5289ee
RC
182#ifdef CONFIG_DEBUG_FS
183static int smp_show(struct seq_file *m, void *arg)
184{
185 struct drm_info_node *node = (struct drm_info_node *) m->private;
186 struct drm_device *dev = node->minor->dev;
187 struct msm_drm_private *priv = dev->dev_private;
188 struct mdp5_kms *mdp5_kms = to_mdp5_kms(to_mdp_kms(priv->kms));
189 struct drm_printer p = drm_seq_file_printer(m);
190
191 if (!mdp5_kms->smp) {
192 drm_printf(&p, "no SMP pool\n");
193 return 0;
194 }
195
196 mdp5_smp_dump(mdp5_kms->smp, &p);
197
198 return 0;
199}
200
201static struct drm_info_list mdp5_debugfs_list[] = {
202 {"smp", smp_show },
203};
204
205static int mdp5_kms_debugfs_init(struct msm_kms *kms, struct drm_minor *minor)
206{
207 struct drm_device *dev = minor->dev;
208 int ret;
209
210 ret = drm_debugfs_create_files(mdp5_debugfs_list,
211 ARRAY_SIZE(mdp5_debugfs_list),
212 minor->debugfs_root, minor);
213
214 if (ret) {
215 dev_err(dev->dev, "could not install mdp5_debugfs_list\n");
216 return ret;
217 }
218
219 return 0;
220}
bc5289ee
RC
221#endif
222
06c0dd96
RC
223static const struct mdp_kms_funcs kms_funcs = {
224 .base = {
225 .hw_init = mdp5_hw_init,
226 .irq_preinstall = mdp5_irq_preinstall,
227 .irq_postinstall = mdp5_irq_postinstall,
228 .irq_uninstall = mdp5_irq_uninstall,
229 .irq = mdp5_irq,
230 .enable_vblank = mdp5_enable_vblank,
231 .disable_vblank = mdp5_disable_vblank,
ac2a3fd3 232 .swap_state = mdp5_swap_state,
0b776d45
RC
233 .prepare_commit = mdp5_prepare_commit,
234 .complete_commit = mdp5_complete_commit,
0a5c9aad 235 .wait_for_crtc_commit_done = mdp5_wait_for_crtc_commit_done,
06c0dd96
RC
236 .get_format = mdp_get_format,
237 .round_pixclk = mdp5_round_pixclk,
d5af49c9 238 .set_split_display = mdp5_set_split_display,
9c9f6f8d 239 .set_encoder_mode = mdp5_set_encoder_mode,
392ae6e0 240 .destroy = mdp5_kms_destroy,
bc5289ee
RC
241#ifdef CONFIG_DEBUG_FS
242 .debugfs_init = mdp5_kms_debugfs_init,
bc5289ee 243#endif
06c0dd96
RC
244 },
245 .set_irqmask = mdp5_set_irqmask,
246};
247
248int mdp5_disable(struct mdp5_kms *mdp5_kms)
249{
250 DBG("");
251
a7d3bb00
RC
252 mdp5_kms->enable_count--;
253 WARN_ON(mdp5_kms->enable_count < 0);
254
06c0dd96
RC
255 clk_disable_unprepare(mdp5_kms->ahb_clk);
256 clk_disable_unprepare(mdp5_kms->axi_clk);
257 clk_disable_unprepare(mdp5_kms->core_clk);
3a84f846
SV
258 if (mdp5_kms->lut_clk)
259 clk_disable_unprepare(mdp5_kms->lut_clk);
06c0dd96
RC
260
261 return 0;
262}
263
264int mdp5_enable(struct mdp5_kms *mdp5_kms)
265{
266 DBG("");
267
a7d3bb00
RC
268 mdp5_kms->enable_count++;
269
06c0dd96
RC
270 clk_prepare_enable(mdp5_kms->ahb_clk);
271 clk_prepare_enable(mdp5_kms->axi_clk);
272 clk_prepare_enable(mdp5_kms->core_clk);
3a84f846
SV
273 if (mdp5_kms->lut_clk)
274 clk_prepare_enable(mdp5_kms->lut_clk);
06c0dd96
RC
275
276 return 0;
277}
278
5722a9e3 279static struct drm_encoder *construct_encoder(struct mdp5_kms *mdp5_kms,
36d1364a
AT
280 struct mdp5_interface *intf,
281 struct mdp5_ctl *ctl)
67ac0a2d
SV
282{
283 struct drm_device *dev = mdp5_kms->dev;
284 struct msm_drm_private *priv = dev->dev_private;
285 struct drm_encoder *encoder;
67ac0a2d 286
36d1364a 287 encoder = mdp5_encoder_init(dev, intf, ctl);
67ac0a2d 288 if (IS_ERR(encoder)) {
5722a9e3
HL
289 dev_err(dev->dev, "failed to construct encoder\n");
290 return encoder;
67ac0a2d
SV
291 }
292
67ac0a2d
SV
293 priv->encoders[priv->num_encoders++] = encoder;
294
5722a9e3
HL
295 return encoder;
296}
297
d5af49c9
HL
298static int get_dsi_id_from_intf(const struct mdp5_cfg_hw *hw_cfg, int intf_num)
299{
fe34464d
SV
300 const enum mdp5_intf_type *intfs = hw_cfg->intf.connect;
301 const int intf_cnt = ARRAY_SIZE(hw_cfg->intf.connect);
d5af49c9
HL
302 int id = 0, i;
303
304 for (i = 0; i < intf_cnt; i++) {
305 if (intfs[i] == INTF_DSI) {
306 if (intf_num == i)
307 return id;
308
309 id++;
310 }
311 }
312
313 return -EINVAL;
314}
315
36d1364a
AT
316static int modeset_init_intf(struct mdp5_kms *mdp5_kms,
317 struct mdp5_interface *intf)
5722a9e3
HL
318{
319 struct drm_device *dev = mdp5_kms->dev;
320 struct msm_drm_private *priv = dev->dev_private;
c71716b1
HL
321 struct mdp5_ctl_manager *ctlm = mdp5_kms->ctlm;
322 struct mdp5_ctl *ctl;
5722a9e3
HL
323 struct drm_encoder *encoder;
324 int ret = 0;
325
36d1364a 326 switch (intf->type) {
5722a9e3
HL
327 case INTF_eDP:
328 if (!priv->edp)
329 break;
330
36d1364a 331 ctl = mdp5_ctlm_request(ctlm, intf->num);
c71716b1
HL
332 if (!ctl) {
333 ret = -EINVAL;
334 break;
335 }
336
36d1364a 337 encoder = construct_encoder(mdp5_kms, intf, ctl);
5722a9e3
HL
338 if (IS_ERR(encoder)) {
339 ret = PTR_ERR(encoder);
340 break;
341 }
67ac0a2d 342
67ac0a2d 343 ret = msm_edp_modeset_init(priv->edp, dev, encoder);
5722a9e3
HL
344 break;
345 case INTF_HDMI:
346 if (!priv->hdmi)
347 break;
348
36d1364a 349 ctl = mdp5_ctlm_request(ctlm, intf->num);
c71716b1
HL
350 if (!ctl) {
351 ret = -EINVAL;
352 break;
353 }
354
36d1364a 355 encoder = construct_encoder(mdp5_kms, intf, ctl);
5722a9e3
HL
356 if (IS_ERR(encoder)) {
357 ret = PTR_ERR(encoder);
358 break;
359 }
360
fcda50c8 361 ret = msm_hdmi_modeset_init(priv->hdmi, dev, encoder);
5722a9e3 362 break;
d5af49c9
HL
363 case INTF_DSI:
364 {
36d1364a
AT
365 const struct mdp5_cfg_hw *hw_cfg =
366 mdp5_cfg_get_hw_config(mdp5_kms->cfg);
367 int dsi_id = get_dsi_id_from_intf(hw_cfg, intf->num);
d5af49c9
HL
368
369 if ((dsi_id >= ARRAY_SIZE(priv->dsi)) || (dsi_id < 0)) {
370 dev_err(dev->dev, "failed to find dsi from intf %d\n",
36d1364a 371 intf->num);
d5af49c9
HL
372 ret = -EINVAL;
373 break;
374 }
375
376 if (!priv->dsi[dsi_id])
377 break;
378
36d1364a 379 ctl = mdp5_ctlm_request(ctlm, intf->num);
c71716b1
HL
380 if (!ctl) {
381 ret = -EINVAL;
382 break;
383 }
384
36d1364a 385 encoder = construct_encoder(mdp5_kms, intf, ctl);
97e00119
AT
386 if (IS_ERR(encoder)) {
387 ret = PTR_ERR(encoder);
388 break;
d5af49c9
HL
389 }
390
97e00119 391 ret = msm_dsi_modeset_init(priv->dsi[dsi_id], dev, encoder);
d5af49c9
HL
392 break;
393 }
5722a9e3 394 default:
36d1364a 395 dev_err(dev->dev, "unknown intf: %d\n", intf->type);
5722a9e3
HL
396 ret = -EINVAL;
397 break;
67ac0a2d
SV
398 }
399
400 return ret;
401}
402
06c0dd96
RC
403static int modeset_init(struct mdp5_kms *mdp5_kms)
404{
06c0dd96
RC
405 struct drm_device *dev = mdp5_kms->dev;
406 struct msm_drm_private *priv = dev->dev_private;
2e362e17 407 const struct mdp5_cfg_hw *hw_cfg;
e5366ffe 408 unsigned int num_crtcs;
bff8fba4
AT
409 int i, ret, pi = 0, ci = 0;
410 struct drm_plane *primary[MAX_BASES] = { NULL };
411 struct drm_plane *cursor[MAX_BASES] = { NULL };
06c0dd96 412
42238da8 413 hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
2e362e17 414
e5366ffe
AT
415 /*
416 * Construct encoders and modeset initialize connector devices
417 * for each external display interface.
418 */
36d1364a
AT
419 for (i = 0; i < mdp5_kms->num_intfs; i++) {
420 ret = modeset_init_intf(mdp5_kms, mdp5_kms->intfs[i]);
e5366ffe
AT
421 if (ret)
422 goto fail;
423 }
424
425 /*
426 * We should ideally have less number of encoders (set up by parsing
427 * the MDP5 interfaces) than the number of layer mixers present in HW,
428 * but let's be safe here anyway
429 */
adfc0e63 430 num_crtcs = min(priv->num_encoders, mdp5_kms->num_hwmixers);
e5366ffe
AT
431
432 /*
433 * Construct planes equaling the number of hw pipes, and CRTCs for the
434 * N encoders set up by the driver. The first N planes become primary
c056b55d
RC
435 * planes for the CRTCs, with the remainder as overlay planes:
436 */
437 for (i = 0; i < mdp5_kms->num_hwpipes; i++) {
bff8fba4 438 struct mdp5_hw_pipe *hwpipe = mdp5_kms->hwpipes[i];
06c0dd96 439 struct drm_plane *plane;
5798c8e0 440 enum drm_plane_type type;
06c0dd96 441
bff8fba4 442 if (i < num_crtcs)
5798c8e0 443 type = DRM_PLANE_TYPE_PRIMARY;
bff8fba4
AT
444 else if (hwpipe->caps & MDP_PIPE_CAP_CURSOR)
445 type = DRM_PLANE_TYPE_CURSOR;
5798c8e0
AT
446 else
447 type = DRM_PLANE_TYPE_OVERLAY;
448
449 plane = mdp5_plane_init(dev, type);
06c0dd96
RC
450 if (IS_ERR(plane)) {
451 ret = PTR_ERR(plane);
c056b55d 452 dev_err(dev->dev, "failed to construct plane %d (%d)\n", i, ret);
06c0dd96
RC
453 goto fail;
454 }
bc5289ee 455 priv->planes[priv->num_planes++] = plane;
06c0dd96 456
bff8fba4
AT
457 if (type == DRM_PLANE_TYPE_PRIMARY)
458 primary[pi++] = plane;
459 if (type == DRM_PLANE_TYPE_CURSOR)
460 cursor[ci++] = plane;
461 }
462
463 for (i = 0; i < num_crtcs; i++) {
464 struct drm_crtc *crtc;
c056b55d 465
bff8fba4 466 crtc = mdp5_crtc_init(dev, primary[i], cursor[i], i);
06c0dd96
RC
467 if (IS_ERR(crtc)) {
468 ret = PTR_ERR(crtc);
c056b55d 469 dev_err(dev->dev, "failed to construct crtc %d (%d)\n", i, ret);
06c0dd96
RC
470 goto fail;
471 }
472 priv->crtcs[priv->num_crtcs++] = crtc;
473 }
474
e5366ffe
AT
475 /*
476 * Now that we know the number of crtcs we've created, set the possible
477 * crtcs for the encoders
5722a9e3 478 */
e5366ffe
AT
479 for (i = 0; i < priv->num_encoders; i++) {
480 struct drm_encoder *encoder = priv->encoders[i];
481
482 encoder->possible_crtcs = (1 << priv->num_crtcs) - 1;
00453981
HL
483 }
484
06c0dd96
RC
485 return 0;
486
487fail:
488 return ret;
489}
490
1dd0a0b1
AT
491static void read_mdp_hw_revision(struct mdp5_kms *mdp5_kms,
492 u32 *major, u32 *minor)
493{
d68fe15b 494 struct device *dev = &mdp5_kms->pdev->dev;
1dd0a0b1
AT
495 u32 version;
496
d68fe15b 497 pm_runtime_get_sync(dev);
7b59c7e4 498 version = mdp5_read(mdp5_kms, REG_MDP5_HW_VERSION);
3c352b66 499 pm_runtime_put_sync(dev);
1dd0a0b1 500
7b59c7e4
AT
501 *major = FIELD(version, MDP5_HW_VERSION_MAJOR);
502 *minor = FIELD(version, MDP5_HW_VERSION_MINOR);
1dd0a0b1 503
e9acce2b 504 dev_info(dev, "MDP5 version v%d.%d", *major, *minor);
1dd0a0b1
AT
505}
506
06c0dd96 507static int get_clk(struct platform_device *pdev, struct clk **clkp,
d40325b4 508 const char *name, bool mandatory)
06c0dd96
RC
509{
510 struct device *dev = &pdev->dev;
d0538f50 511 struct clk *clk = msm_clk_get(pdev, name);
d40325b4 512 if (IS_ERR(clk) && mandatory) {
06c0dd96
RC
513 dev_err(dev, "failed to get %s (%ld)\n", name, PTR_ERR(clk));
514 return PTR_ERR(clk);
515 }
d40325b4
SV
516 if (IS_ERR(clk))
517 DBG("skipping %s", name);
518 else
519 *clkp = clk;
520
06c0dd96
RC
521 return 0;
522}
523
e2dd9f9f
AT
524static struct drm_encoder *get_encoder_from_crtc(struct drm_crtc *crtc)
525{
526 struct drm_device *dev = crtc->dev;
527 struct drm_encoder *encoder;
528
529 drm_for_each_encoder(encoder, dev)
530 if (encoder->crtc == crtc)
531 return encoder;
532
533 return NULL;
534}
535
1bf6ad62
DV
536static bool mdp5_get_scanoutpos(struct drm_device *dev, unsigned int pipe,
537 bool in_vblank_irq, int *vpos, int *hpos,
538 ktime_t *stime, ktime_t *etime,
539 const struct drm_display_mode *mode)
e2dd9f9f
AT
540{
541 struct msm_drm_private *priv = dev->dev_private;
542 struct drm_crtc *crtc;
543 struct drm_encoder *encoder;
544 int line, vsw, vbp, vactive_start, vactive_end, vfp_end;
e2dd9f9f
AT
545
546 crtc = priv->crtcs[pipe];
547 if (!crtc) {
548 DRM_ERROR("Invalid crtc %d\n", pipe);
1bf6ad62 549 return false;
e2dd9f9f
AT
550 }
551
552 encoder = get_encoder_from_crtc(crtc);
553 if (!encoder) {
554 DRM_ERROR("no encoder found for crtc %d\n", pipe);
1bf6ad62 555 return false;
e2dd9f9f
AT
556 }
557
e2dd9f9f
AT
558 vsw = mode->crtc_vsync_end - mode->crtc_vsync_start;
559 vbp = mode->crtc_vtotal - mode->crtc_vsync_end;
560
561 /*
562 * the line counter is 1 at the start of the VSYNC pulse and VTOTAL at
563 * the end of VFP. Translate the porch values relative to the line
564 * counter positions.
565 */
566
567 vactive_start = vsw + vbp + 1;
568
569 vactive_end = vactive_start + mode->crtc_vdisplay;
570
571 /* last scan line before VSYNC */
572 vfp_end = mode->crtc_vtotal;
573
574 if (stime)
575 *stime = ktime_get();
576
577 line = mdp5_encoder_get_linecount(encoder);
578
579 if (line < vactive_start) {
580 line -= vactive_start;
e2dd9f9f
AT
581 } else if (line > vactive_end) {
582 line = line - vfp_end - vactive_start;
e2dd9f9f
AT
583 } else {
584 line -= vactive_start;
585 }
586
587 *vpos = line;
588 *hpos = 0;
589
590 if (etime)
591 *etime = ktime_get();
592
1bf6ad62 593 return true;
e2dd9f9f
AT
594}
595
596static u32 mdp5_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
597{
598 struct msm_drm_private *priv = dev->dev_private;
599 struct drm_crtc *crtc;
600 struct drm_encoder *encoder;
601
0c17151a 602 if (pipe >= priv->num_crtcs)
e2dd9f9f
AT
603 return 0;
604
605 crtc = priv->crtcs[pipe];
606 if (!crtc)
607 return 0;
608
609 encoder = get_encoder_from_crtc(crtc);
610 if (!encoder)
611 return 0;
612
613 return mdp5_encoder_get_framecount(encoder);
614}
615
06c0dd96 616struct msm_kms *mdp5_kms_init(struct drm_device *dev)
aec095ec
AT
617{
618 struct msm_drm_private *priv = dev->dev_private;
619 struct platform_device *pdev;
620 struct mdp5_kms *mdp5_kms;
621 struct mdp5_cfg *config;
622 struct msm_kms *kms;
667ce33e 623 struct msm_gem_address_space *aspace;
aec095ec
AT
624 int irq, i, ret;
625
626 /* priv->kms would have been populated by the MDP5 driver */
627 kms = priv->kms;
628 if (!kms)
629 return NULL;
630
631 mdp5_kms = to_mdp5_kms(to_mdp_kms(kms));
632
633 mdp_kms_init(&mdp5_kms->base, &kms_funcs);
634
635 pdev = mdp5_kms->pdev;
636
637 irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
638 if (irq < 0) {
639 ret = irq;
640 dev_err(&pdev->dev, "failed to get irq: %d\n", ret);
641 goto fail;
642 }
643
644 kms->irq = irq;
645
646 config = mdp5_cfg_get_config(mdp5_kms->cfg);
647
648 /* make sure things are off before attaching iommu (bootloader could
649 * have left things on, in which case we'll start getting faults if
650 * we don't disable):
651 */
d68fe15b 652 pm_runtime_get_sync(&pdev->dev);
aec095ec
AT
653 for (i = 0; i < MDP5_INTF_NUM_MAX; i++) {
654 if (mdp5_cfg_intf_is_virtual(config->hw->intf.connect[i]) ||
655 !config->hw->intf.base[i])
656 continue;
657 mdp5_write(mdp5_kms, REG_MDP5_INTF_TIMING_ENGINE_EN(i), 0);
658
659 mdp5_write(mdp5_kms, REG_MDP5_INTF_FRAME_LINE_COUNT_EN(i), 0x3);
660 }
aec095ec
AT
661 mdelay(16);
662
663 if (config->platform.iommu) {
667ce33e
RC
664 aspace = msm_gem_address_space_create(&pdev->dev,
665 config->platform.iommu, "mdp5");
666 if (IS_ERR(aspace)) {
667 ret = PTR_ERR(aspace);
aec095ec
AT
668 goto fail;
669 }
670
f59f62d5 671 kms->aspace = aspace;
667ce33e
RC
672
673 ret = aspace->mmu->funcs->attach(aspace->mmu, iommu_ports,
aec095ec
AT
674 ARRAY_SIZE(iommu_ports));
675 if (ret) {
676 dev_err(&pdev->dev, "failed to attach iommu: %d\n",
677 ret);
aec095ec
AT
678 goto fail;
679 }
680 } else {
681 dev_info(&pdev->dev,
682 "no iommu, fallback to phys contig buffers for scanout\n");
ed7158ba 683 aspace = NULL;
aec095ec 684 }
aec095ec 685
3c352b66 686 pm_runtime_put_sync(&pdev->dev);
d68fe15b 687
aec095ec
AT
688 ret = modeset_init(mdp5_kms);
689 if (ret) {
690 dev_err(&pdev->dev, "modeset_init failed: %d\n", ret);
691 goto fail;
692 }
693
694 dev->mode_config.min_width = 0;
695 dev->mode_config.min_height = 0;
9708ebbe
RC
696 dev->mode_config.max_width = 0xffff;
697 dev->mode_config.max_height = 0xffff;
aec095ec 698
1bf6ad62 699 dev->driver->get_vblank_timestamp = drm_calc_vbltimestamp_from_scanoutpos;
aec095ec
AT
700 dev->driver->get_scanout_position = mdp5_get_scanoutpos;
701 dev->driver->get_vblank_counter = mdp5_get_vblank_counter;
702 dev->max_vblank_count = 0xffffffff;
703 dev->vblank_disable_immediate = true;
704
705 return kms;
706fail:
707 if (kms)
392ae6e0 708 mdp5_kms_destroy(kms);
aec095ec
AT
709 return ERR_PTR(ret);
710}
711
1dd0a0b1
AT
712static void mdp5_destroy(struct platform_device *pdev)
713{
714 struct mdp5_kms *mdp5_kms = platform_get_drvdata(pdev);
36d1364a 715 int i;
1dd0a0b1
AT
716
717 if (mdp5_kms->ctlm)
718 mdp5_ctlm_destroy(mdp5_kms->ctlm);
719 if (mdp5_kms->smp)
720 mdp5_smp_destroy(mdp5_kms->smp);
721 if (mdp5_kms->cfg)
722 mdp5_cfg_destroy(mdp5_kms->cfg);
cd792726 723
36d1364a
AT
724 for (i = 0; i < mdp5_kms->num_intfs; i++)
725 kfree(mdp5_kms->intfs[i]);
726
cd792726
AT
727 if (mdp5_kms->rpm_enabled)
728 pm_runtime_disable(&pdev->dev);
ac2a3fd3
RC
729
730 kfree(mdp5_kms->state);
1dd0a0b1
AT
731}
732
c056b55d
RC
733static int construct_pipes(struct mdp5_kms *mdp5_kms, int cnt,
734 const enum mdp5_pipe *pipes, const uint32_t *offsets,
735 uint32_t caps)
736{
737 struct drm_device *dev = mdp5_kms->dev;
738 int i, ret;
739
740 for (i = 0; i < cnt; i++) {
741 struct mdp5_hw_pipe *hwpipe;
742
743 hwpipe = mdp5_pipe_init(pipes[i], offsets[i], caps);
744 if (IS_ERR(hwpipe)) {
745 ret = PTR_ERR(hwpipe);
746 dev_err(dev->dev, "failed to construct pipe for %s (%d)\n",
747 pipe2name(pipes[i]), ret);
748 return ret;
749 }
750 hwpipe->idx = mdp5_kms->num_hwpipes;
751 mdp5_kms->hwpipes[mdp5_kms->num_hwpipes++] = hwpipe;
752 }
753
754 return 0;
755}
756
757static int hwpipe_init(struct mdp5_kms *mdp5_kms)
758{
759 static const enum mdp5_pipe rgb_planes[] = {
760 SSPP_RGB0, SSPP_RGB1, SSPP_RGB2, SSPP_RGB3,
761 };
762 static const enum mdp5_pipe vig_planes[] = {
763 SSPP_VIG0, SSPP_VIG1, SSPP_VIG2, SSPP_VIG3,
764 };
765 static const enum mdp5_pipe dma_planes[] = {
766 SSPP_DMA0, SSPP_DMA1,
767 };
bff8fba4
AT
768 static const enum mdp5_pipe cursor_planes[] = {
769 SSPP_CURSOR0, SSPP_CURSOR1,
770 };
c056b55d
RC
771 const struct mdp5_cfg_hw *hw_cfg;
772 int ret;
773
774 hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
775
776 /* Construct RGB pipes: */
777 ret = construct_pipes(mdp5_kms, hw_cfg->pipe_rgb.count, rgb_planes,
778 hw_cfg->pipe_rgb.base, hw_cfg->pipe_rgb.caps);
779 if (ret)
780 return ret;
781
782 /* Construct video (VIG) pipes: */
783 ret = construct_pipes(mdp5_kms, hw_cfg->pipe_vig.count, vig_planes,
784 hw_cfg->pipe_vig.base, hw_cfg->pipe_vig.caps);
785 if (ret)
786 return ret;
787
788 /* Construct DMA pipes: */
789 ret = construct_pipes(mdp5_kms, hw_cfg->pipe_dma.count, dma_planes,
790 hw_cfg->pipe_dma.base, hw_cfg->pipe_dma.caps);
791 if (ret)
792 return ret;
793
bff8fba4
AT
794 /* Construct cursor pipes: */
795 ret = construct_pipes(mdp5_kms, hw_cfg->pipe_cursor.count,
796 cursor_planes, hw_cfg->pipe_cursor.base,
797 hw_cfg->pipe_cursor.caps);
798 if (ret)
799 return ret;
800
c056b55d
RC
801 return 0;
802}
803
6803c606
AT
804static int hwmixer_init(struct mdp5_kms *mdp5_kms)
805{
806 struct drm_device *dev = mdp5_kms->dev;
807 const struct mdp5_cfg_hw *hw_cfg;
808 int i, ret;
809
810 hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
811
812 for (i = 0; i < hw_cfg->lm.count; i++) {
813 struct mdp5_hw_mixer *mixer;
814
815 mixer = mdp5_mixer_init(&hw_cfg->lm.instances[i]);
816 if (IS_ERR(mixer)) {
817 ret = PTR_ERR(mixer);
818 dev_err(dev->dev, "failed to construct LM%d (%d)\n",
819 i, ret);
820 return ret;
821 }
822
823 mixer->idx = mdp5_kms->num_hwmixers;
824 mdp5_kms->hwmixers[mdp5_kms->num_hwmixers++] = mixer;
825 }
826
827 return 0;
828}
829
36d1364a
AT
830static int interface_init(struct mdp5_kms *mdp5_kms)
831{
832 struct drm_device *dev = mdp5_kms->dev;
833 const struct mdp5_cfg_hw *hw_cfg;
834 const enum mdp5_intf_type *intf_types;
835 int i;
836
837 hw_cfg = mdp5_cfg_get_hw_config(mdp5_kms->cfg);
838 intf_types = hw_cfg->intf.connect;
839
840 for (i = 0; i < ARRAY_SIZE(hw_cfg->intf.connect); i++) {
841 struct mdp5_interface *intf;
842
843 if (intf_types[i] == INTF_DISABLED)
844 continue;
845
846 intf = kzalloc(sizeof(*intf), GFP_KERNEL);
847 if (!intf) {
848 dev_err(dev->dev, "failed to construct INTF%d\n", i);
849 return -ENOMEM;
850 }
851
852 intf->num = i;
853 intf->type = intf_types[i];
854 intf->mode = MDP5_INTF_MODE_NONE;
855 intf->idx = mdp5_kms->num_intfs;
856 mdp5_kms->intfs[mdp5_kms->num_intfs++] = intf;
857 }
858
859 return 0;
860}
861
1dd0a0b1
AT
862static int mdp5_init(struct platform_device *pdev, struct drm_device *dev)
863{
864 struct msm_drm_private *priv = dev->dev_private;
865 struct mdp5_kms *mdp5_kms;
866 struct mdp5_cfg *config;
867 u32 major, minor;
868 int ret;
869
870 mdp5_kms = devm_kzalloc(&pdev->dev, sizeof(*mdp5_kms), GFP_KERNEL);
871 if (!mdp5_kms) {
872 ret = -ENOMEM;
873 goto fail;
874 }
875
876 platform_set_drvdata(pdev, mdp5_kms);
877
878 spin_lock_init(&mdp5_kms->resource_lock);
879
880 mdp5_kms->dev = dev;
881 mdp5_kms->pdev = pdev;
882
ac2a3fd3
RC
883 drm_modeset_lock_init(&mdp5_kms->state_lock);
884 mdp5_kms->state = kzalloc(sizeof(*mdp5_kms->state), GFP_KERNEL);
885 if (!mdp5_kms->state) {
886 ret = -ENOMEM;
887 goto fail;
888 }
889
1dd0a0b1
AT
890 mdp5_kms->mmio = msm_ioremap(pdev, "mdp_phys", "MDP5");
891 if (IS_ERR(mdp5_kms->mmio)) {
892 ret = PTR_ERR(mdp5_kms->mmio);
893 goto fail;
894 }
895
896 /* mandatory clocks: */
d0538f50 897 ret = get_clk(pdev, &mdp5_kms->axi_clk, "bus", true);
1dd0a0b1
AT
898 if (ret)
899 goto fail;
d0538f50 900 ret = get_clk(pdev, &mdp5_kms->ahb_clk, "iface", true);
1dd0a0b1
AT
901 if (ret)
902 goto fail;
d0538f50 903 ret = get_clk(pdev, &mdp5_kms->core_clk, "core", true);
1dd0a0b1
AT
904 if (ret)
905 goto fail;
d0538f50 906 ret = get_clk(pdev, &mdp5_kms->vsync_clk, "vsync", true);
1dd0a0b1
AT
907 if (ret)
908 goto fail;
909
910 /* optional clocks: */
d0538f50 911 get_clk(pdev, &mdp5_kms->lut_clk, "lut", false);
1dd0a0b1
AT
912
913 /* we need to set a default rate before enabling. Set a safe
914 * rate first, then figure out hw revision, and then set a
915 * more optimal rate:
916 */
917 clk_set_rate(mdp5_kms->core_clk, 200000000);
918
cd792726
AT
919 pm_runtime_enable(&pdev->dev);
920 mdp5_kms->rpm_enabled = true;
921
1dd0a0b1
AT
922 read_mdp_hw_revision(mdp5_kms, &major, &minor);
923
924 mdp5_kms->cfg = mdp5_cfg_init(mdp5_kms, major, minor);
925 if (IS_ERR(mdp5_kms->cfg)) {
926 ret = PTR_ERR(mdp5_kms->cfg);
927 mdp5_kms->cfg = NULL;
928 goto fail;
929 }
930
931 config = mdp5_cfg_get_config(mdp5_kms->cfg);
932 mdp5_kms->caps = config->hw->mdp.caps;
933
934 /* TODO: compute core clock rate at runtime */
935 clk_set_rate(mdp5_kms->core_clk, config->hw->max_clk);
936
937 /*
938 * Some chipsets have a Shared Memory Pool (SMP), while others
939 * have dedicated latency buffering per source pipe instead;
940 * this section initializes the SMP:
941 */
942 if (mdp5_kms->caps & MDP_CAP_SMP) {
49ec5b2e 943 mdp5_kms->smp = mdp5_smp_init(mdp5_kms, &config->hw->smp);
1dd0a0b1
AT
944 if (IS_ERR(mdp5_kms->smp)) {
945 ret = PTR_ERR(mdp5_kms->smp);
946 mdp5_kms->smp = NULL;
947 goto fail;
948 }
949 }
950
951 mdp5_kms->ctlm = mdp5_ctlm_init(dev, mdp5_kms->mmio, mdp5_kms->cfg);
952 if (IS_ERR(mdp5_kms->ctlm)) {
953 ret = PTR_ERR(mdp5_kms->ctlm);
954 mdp5_kms->ctlm = NULL;
955 goto fail;
956 }
957
c056b55d
RC
958 ret = hwpipe_init(mdp5_kms);
959 if (ret)
960 goto fail;
961
6803c606
AT
962 ret = hwmixer_init(mdp5_kms);
963 if (ret)
964 goto fail;
965
36d1364a
AT
966 ret = interface_init(mdp5_kms);
967 if (ret)
968 goto fail;
969
1dd0a0b1
AT
970 /* set uninit-ed kms */
971 priv->kms = &mdp5_kms->base.base;
972
973 return 0;
974fail:
975 mdp5_destroy(pdev);
976 return ret;
977}
978
979static int mdp5_bind(struct device *dev, struct device *master, void *data)
980{
981 struct drm_device *ddev = dev_get_drvdata(master);
982 struct platform_device *pdev = to_platform_device(dev);
983
984 DBG("");
985
986 return mdp5_init(pdev, ddev);
987}
988
989static void mdp5_unbind(struct device *dev, struct device *master,
990 void *data)
991{
992 struct platform_device *pdev = to_platform_device(dev);
993
994 mdp5_destroy(pdev);
995}
996
997static const struct component_ops mdp5_ops = {
998 .bind = mdp5_bind,
999 .unbind = mdp5_unbind,
1000};
1001
1002static int mdp5_dev_probe(struct platform_device *pdev)
1003{
1004 DBG("");
1005 return component_add(&pdev->dev, &mdp5_ops);
1006}
1007
1008static int mdp5_dev_remove(struct platform_device *pdev)
1009{
1010 DBG("");
1011 component_del(&pdev->dev, &mdp5_ops);
1012 return 0;
1013}
1014
d1f08d82 1015static __maybe_unused int mdp5_runtime_suspend(struct device *dev)
d68fe15b
AT
1016{
1017 struct platform_device *pdev = to_platform_device(dev);
1018 struct mdp5_kms *mdp5_kms = platform_get_drvdata(pdev);
1019
1020 DBG("");
1021
1022 return mdp5_disable(mdp5_kms);
1023}
1024
d1f08d82 1025static __maybe_unused int mdp5_runtime_resume(struct device *dev)
d68fe15b
AT
1026{
1027 struct platform_device *pdev = to_platform_device(dev);
1028 struct mdp5_kms *mdp5_kms = platform_get_drvdata(pdev);
1029
1030 DBG("");
1031
1032 return mdp5_enable(mdp5_kms);
1033}
1034
1035static const struct dev_pm_ops mdp5_pm_ops = {
1036 SET_RUNTIME_PM_OPS(mdp5_runtime_suspend, mdp5_runtime_resume, NULL)
1037};
1038
96a611b5
AT
1039static const struct of_device_id mdp5_dt_match[] = {
1040 { .compatible = "qcom,mdp5", },
1041 /* to support downstream DT files */
1042 { .compatible = "qcom,mdss_mdp", },
1043 {}
1044};
1045MODULE_DEVICE_TABLE(of, mdp5_dt_match);
1046
1dd0a0b1
AT
1047static struct platform_driver mdp5_driver = {
1048 .probe = mdp5_dev_probe,
1049 .remove = mdp5_dev_remove,
1050 .driver = {
1051 .name = "msm_mdp",
96a611b5 1052 .of_match_table = mdp5_dt_match,
d68fe15b 1053 .pm = &mdp5_pm_ops,
1dd0a0b1
AT
1054 },
1055};
1056
1057void __init msm_mdp_register(void)
1058{
1059 DBG("");
1060 platform_driver_register(&mdp5_driver);
1061}
1062
1063void __exit msm_mdp_unregister(void)
1064{
1065 DBG("");
1066 platform_driver_unregister(&mdp5_driver);
1067}