xref: /openbmc/linux/drivers/gpu/drm/msm/disp/dpu1/dpu_kms.c (revision 1931dc14)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Copyright (c) 2014-2018, The Linux Foundation. All rights reserved.
5  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
6  *
7  * Author: Rob Clark <robdclark@gmail.com>
8  */
9 
10 #define pr_fmt(fmt)	"[drm:%s:%d] " fmt, __func__, __LINE__
11 
12 #include <linux/debugfs.h>
13 #include <linux/dma-buf.h>
14 #include <linux/of_irq.h>
15 #include <linux/pm_opp.h>
16 
17 #include <drm/drm_crtc.h>
18 #include <drm/drm_file.h>
19 #include <drm/drm_framebuffer.h>
20 #include <drm/drm_vblank.h>
21 #include <drm/drm_writeback.h>
22 
23 #include "msm_drv.h"
24 #include "msm_mmu.h"
25 #include "msm_mdss.h"
26 #include "msm_gem.h"
27 #include "disp/msm_disp_snapshot.h"
28 
29 #include "dpu_core_irq.h"
30 #include "dpu_crtc.h"
31 #include "dpu_encoder.h"
32 #include "dpu_formats.h"
33 #include "dpu_hw_vbif.h"
34 #include "dpu_kms.h"
35 #include "dpu_plane.h"
36 #include "dpu_vbif.h"
37 #include "dpu_writeback.h"
38 
39 #define CREATE_TRACE_POINTS
40 #include "dpu_trace.h"
41 
42 /*
43  * To enable overall DRM driver logging
44  * # echo 0x2 > /sys/module/drm/parameters/debug
45  *
46  * To enable DRM driver h/w logging
47  * # echo <mask> > /sys/kernel/debug/dri/0/debug/hw_log_mask
48  *
49  * See dpu_hw_mdss.h for h/w logging mask definitions (search for DPU_DBG_MASK_)
50  */
51 #define DPU_DEBUGFS_DIR "msm_dpu"
52 #define DPU_DEBUGFS_HWMASKNAME "hw_log_mask"
53 
54 static int dpu_kms_hw_init(struct msm_kms *kms);
55 static void _dpu_kms_mmu_destroy(struct dpu_kms *dpu_kms);
56 
57 #ifdef CONFIG_DEBUG_FS
58 static int _dpu_danger_signal_status(struct seq_file *s,
59 		bool danger_status)
60 {
61 	struct dpu_danger_safe_status status;
62 	struct dpu_kms *kms = s->private;
63 	int i;
64 
65 	if (!kms->hw_mdp) {
66 		DPU_ERROR("invalid arg(s)\n");
67 		return 0;
68 	}
69 
70 	memset(&status, 0, sizeof(struct dpu_danger_safe_status));
71 
72 	pm_runtime_get_sync(&kms->pdev->dev);
73 	if (danger_status) {
74 		seq_puts(s, "\nDanger signal status:\n");
75 		if (kms->hw_mdp->ops.get_danger_status)
76 			kms->hw_mdp->ops.get_danger_status(kms->hw_mdp,
77 					&status);
78 	} else {
79 		seq_puts(s, "\nSafe signal status:\n");
80 		if (kms->hw_mdp->ops.get_safe_status)
81 			kms->hw_mdp->ops.get_safe_status(kms->hw_mdp,
82 					&status);
83 	}
84 	pm_runtime_put_sync(&kms->pdev->dev);
85 
86 	seq_printf(s, "MDP     :  0x%x\n", status.mdp);
87 
88 	for (i = SSPP_VIG0; i < SSPP_MAX; i++)
89 		seq_printf(s, "SSPP%d   :  0x%x  \n", i - SSPP_VIG0,
90 				status.sspp[i]);
91 	seq_puts(s, "\n");
92 
93 	return 0;
94 }
95 
96 static int dpu_debugfs_danger_stats_show(struct seq_file *s, void *v)
97 {
98 	return _dpu_danger_signal_status(s, true);
99 }
100 DEFINE_SHOW_ATTRIBUTE(dpu_debugfs_danger_stats);
101 
102 static int dpu_debugfs_safe_stats_show(struct seq_file *s, void *v)
103 {
104 	return _dpu_danger_signal_status(s, false);
105 }
106 DEFINE_SHOW_ATTRIBUTE(dpu_debugfs_safe_stats);
107 
108 static ssize_t _dpu_plane_danger_read(struct file *file,
109 			char __user *buff, size_t count, loff_t *ppos)
110 {
111 	struct dpu_kms *kms = file->private_data;
112 	int len;
113 	char buf[40];
114 
115 	len = scnprintf(buf, sizeof(buf), "%d\n", !kms->has_danger_ctrl);
116 
117 	return simple_read_from_buffer(buff, count, ppos, buf, len);
118 }
119 
120 static void _dpu_plane_set_danger_state(struct dpu_kms *kms, bool enable)
121 {
122 	struct drm_plane *plane;
123 
124 	drm_for_each_plane(plane, kms->dev) {
125 		if (plane->fb && plane->state) {
126 			dpu_plane_danger_signal_ctrl(plane, enable);
127 			DPU_DEBUG("plane:%d img:%dx%d ",
128 				plane->base.id, plane->fb->width,
129 				plane->fb->height);
130 			DPU_DEBUG("src[%d,%d,%d,%d] dst[%d,%d,%d,%d]\n",
131 				plane->state->src_x >> 16,
132 				plane->state->src_y >> 16,
133 				plane->state->src_w >> 16,
134 				plane->state->src_h >> 16,
135 				plane->state->crtc_x, plane->state->crtc_y,
136 				plane->state->crtc_w, plane->state->crtc_h);
137 		} else {
138 			DPU_DEBUG("Inactive plane:%d\n", plane->base.id);
139 		}
140 	}
141 }
142 
143 static ssize_t _dpu_plane_danger_write(struct file *file,
144 		    const char __user *user_buf, size_t count, loff_t *ppos)
145 {
146 	struct dpu_kms *kms = file->private_data;
147 	int disable_panic;
148 	int ret;
149 
150 	ret = kstrtouint_from_user(user_buf, count, 0, &disable_panic);
151 	if (ret)
152 		return ret;
153 
154 	if (disable_panic) {
155 		/* Disable panic signal for all active pipes */
156 		DPU_DEBUG("Disabling danger:\n");
157 		_dpu_plane_set_danger_state(kms, false);
158 		kms->has_danger_ctrl = false;
159 	} else {
160 		/* Enable panic signal for all active pipes */
161 		DPU_DEBUG("Enabling danger:\n");
162 		kms->has_danger_ctrl = true;
163 		_dpu_plane_set_danger_state(kms, true);
164 	}
165 
166 	return count;
167 }
168 
169 static const struct file_operations dpu_plane_danger_enable = {
170 	.open = simple_open,
171 	.read = _dpu_plane_danger_read,
172 	.write = _dpu_plane_danger_write,
173 };
174 
175 static void dpu_debugfs_danger_init(struct dpu_kms *dpu_kms,
176 		struct dentry *parent)
177 {
178 	struct dentry *entry = debugfs_create_dir("danger", parent);
179 
180 	debugfs_create_file("danger_status", 0600, entry,
181 			dpu_kms, &dpu_debugfs_danger_stats_fops);
182 	debugfs_create_file("safe_status", 0600, entry,
183 			dpu_kms, &dpu_debugfs_safe_stats_fops);
184 	debugfs_create_file("disable_danger", 0600, entry,
185 			dpu_kms, &dpu_plane_danger_enable);
186 
187 }
188 
189 /*
190  * Companion structure for dpu_debugfs_create_regset32.
191  */
192 struct dpu_debugfs_regset32 {
193 	uint32_t offset;
194 	uint32_t blk_len;
195 	struct dpu_kms *dpu_kms;
196 };
197 
198 static int dpu_regset32_show(struct seq_file *s, void *data)
199 {
200 	struct dpu_debugfs_regset32 *regset = s->private;
201 	struct dpu_kms *dpu_kms = regset->dpu_kms;
202 	void __iomem *base;
203 	uint32_t i, addr;
204 
205 	if (!dpu_kms->mmio)
206 		return 0;
207 
208 	base = dpu_kms->mmio + regset->offset;
209 
210 	/* insert padding spaces, if needed */
211 	if (regset->offset & 0xF) {
212 		seq_printf(s, "[%x]", regset->offset & ~0xF);
213 		for (i = 0; i < (regset->offset & 0xF); i += 4)
214 			seq_puts(s, "         ");
215 	}
216 
217 	pm_runtime_get_sync(&dpu_kms->pdev->dev);
218 
219 	/* main register output */
220 	for (i = 0; i < regset->blk_len; i += 4) {
221 		addr = regset->offset + i;
222 		if ((addr & 0xF) == 0x0)
223 			seq_printf(s, i ? "\n[%x]" : "[%x]", addr);
224 		seq_printf(s, " %08x", readl_relaxed(base + i));
225 	}
226 	seq_puts(s, "\n");
227 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
228 
229 	return 0;
230 }
231 DEFINE_SHOW_ATTRIBUTE(dpu_regset32);
232 
233 void dpu_debugfs_create_regset32(const char *name, umode_t mode,
234 		void *parent,
235 		uint32_t offset, uint32_t length, struct dpu_kms *dpu_kms)
236 {
237 	struct dpu_debugfs_regset32 *regset;
238 
239 	if (WARN_ON(!name || !dpu_kms || !length))
240 		return;
241 
242 	regset = devm_kzalloc(&dpu_kms->pdev->dev, sizeof(*regset), GFP_KERNEL);
243 	if (!regset)
244 		return;
245 
246 	/* make sure offset is a multiple of 4 */
247 	regset->offset = round_down(offset, 4);
248 	regset->blk_len = length;
249 	regset->dpu_kms = dpu_kms;
250 
251 	debugfs_create_file(name, mode, parent, regset, &dpu_regset32_fops);
252 }
253 
254 static void dpu_debugfs_sspp_init(struct dpu_kms *dpu_kms, struct dentry *debugfs_root)
255 {
256 	struct dentry *entry = debugfs_create_dir("sspp", debugfs_root);
257 	int i;
258 
259 	if (IS_ERR(entry))
260 		return;
261 
262 	for (i = SSPP_NONE; i < SSPP_MAX; i++) {
263 		struct dpu_hw_sspp *hw = dpu_rm_get_sspp(&dpu_kms->rm, i);
264 
265 		if (!hw)
266 			continue;
267 
268 		_dpu_hw_sspp_init_debugfs(hw, dpu_kms, entry);
269 	}
270 }
271 
272 static int dpu_kms_debugfs_init(struct msm_kms *kms, struct drm_minor *minor)
273 {
274 	struct dpu_kms *dpu_kms = to_dpu_kms(kms);
275 	void *p = dpu_hw_util_get_log_mask_ptr();
276 	struct dentry *entry;
277 	struct drm_device *dev;
278 	struct msm_drm_private *priv;
279 	int i;
280 
281 	if (!p)
282 		return -EINVAL;
283 
284 	/* Only create a set of debugfs for the primary node, ignore render nodes */
285 	if (minor->type != DRM_MINOR_PRIMARY)
286 		return 0;
287 
288 	dev = dpu_kms->dev;
289 	priv = dev->dev_private;
290 
291 	entry = debugfs_create_dir("debug", minor->debugfs_root);
292 
293 	debugfs_create_x32(DPU_DEBUGFS_HWMASKNAME, 0600, entry, p);
294 
295 	dpu_debugfs_danger_init(dpu_kms, entry);
296 	dpu_debugfs_vbif_init(dpu_kms, entry);
297 	dpu_debugfs_core_irq_init(dpu_kms, entry);
298 	dpu_debugfs_sspp_init(dpu_kms, entry);
299 
300 	for (i = 0; i < ARRAY_SIZE(priv->dp); i++) {
301 		if (priv->dp[i])
302 			msm_dp_debugfs_init(priv->dp[i], minor);
303 	}
304 
305 	return dpu_core_perf_debugfs_init(dpu_kms, entry);
306 }
307 #endif
308 
309 /* Global/shared object state funcs */
310 
311 /*
312  * This is a helper that returns the private state currently in operation.
313  * Note that this would return the "old_state" if called in the atomic check
314  * path, and the "new_state" after the atomic swap has been done.
315  */
316 struct dpu_global_state *
317 dpu_kms_get_existing_global_state(struct dpu_kms *dpu_kms)
318 {
319 	return to_dpu_global_state(dpu_kms->global_state.state);
320 }
321 
322 /*
323  * This acquires the modeset lock set aside for global state, creates
324  * a new duplicated private object state.
325  */
326 struct dpu_global_state *dpu_kms_get_global_state(struct drm_atomic_state *s)
327 {
328 	struct msm_drm_private *priv = s->dev->dev_private;
329 	struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
330 	struct drm_private_state *priv_state;
331 	int ret;
332 
333 	ret = drm_modeset_lock(&dpu_kms->global_state_lock, s->acquire_ctx);
334 	if (ret)
335 		return ERR_PTR(ret);
336 
337 	priv_state = drm_atomic_get_private_obj_state(s,
338 						&dpu_kms->global_state);
339 	if (IS_ERR(priv_state))
340 		return ERR_CAST(priv_state);
341 
342 	return to_dpu_global_state(priv_state);
343 }
344 
345 static struct drm_private_state *
346 dpu_kms_global_duplicate_state(struct drm_private_obj *obj)
347 {
348 	struct dpu_global_state *state;
349 
350 	state = kmemdup(obj->state, sizeof(*state), GFP_KERNEL);
351 	if (!state)
352 		return NULL;
353 
354 	__drm_atomic_helper_private_obj_duplicate_state(obj, &state->base);
355 
356 	return &state->base;
357 }
358 
359 static void dpu_kms_global_destroy_state(struct drm_private_obj *obj,
360 				      struct drm_private_state *state)
361 {
362 	struct dpu_global_state *dpu_state = to_dpu_global_state(state);
363 
364 	kfree(dpu_state);
365 }
366 
367 static const struct drm_private_state_funcs dpu_kms_global_state_funcs = {
368 	.atomic_duplicate_state = dpu_kms_global_duplicate_state,
369 	.atomic_destroy_state = dpu_kms_global_destroy_state,
370 };
371 
372 static int dpu_kms_global_obj_init(struct dpu_kms *dpu_kms)
373 {
374 	struct dpu_global_state *state;
375 
376 	drm_modeset_lock_init(&dpu_kms->global_state_lock);
377 
378 	state = kzalloc(sizeof(*state), GFP_KERNEL);
379 	if (!state)
380 		return -ENOMEM;
381 
382 	drm_atomic_private_obj_init(dpu_kms->dev, &dpu_kms->global_state,
383 				    &state->base,
384 				    &dpu_kms_global_state_funcs);
385 	return 0;
386 }
387 
388 static int dpu_kms_parse_data_bus_icc_path(struct dpu_kms *dpu_kms)
389 {
390 	struct icc_path *path0;
391 	struct icc_path *path1;
392 	struct drm_device *dev = dpu_kms->dev;
393 	struct device *dpu_dev = dev->dev;
394 
395 	path0 = msm_icc_get(dpu_dev, "mdp0-mem");
396 	path1 = msm_icc_get(dpu_dev, "mdp1-mem");
397 
398 	if (IS_ERR_OR_NULL(path0))
399 		return PTR_ERR_OR_ZERO(path0);
400 
401 	dpu_kms->path[0] = path0;
402 	dpu_kms->num_paths = 1;
403 
404 	if (!IS_ERR_OR_NULL(path1)) {
405 		dpu_kms->path[1] = path1;
406 		dpu_kms->num_paths++;
407 	}
408 	return 0;
409 }
410 
411 static int dpu_kms_enable_vblank(struct msm_kms *kms, struct drm_crtc *crtc)
412 {
413 	return dpu_crtc_vblank(crtc, true);
414 }
415 
416 static void dpu_kms_disable_vblank(struct msm_kms *kms, struct drm_crtc *crtc)
417 {
418 	dpu_crtc_vblank(crtc, false);
419 }
420 
421 static void dpu_kms_enable_commit(struct msm_kms *kms)
422 {
423 	struct dpu_kms *dpu_kms = to_dpu_kms(kms);
424 	pm_runtime_get_sync(&dpu_kms->pdev->dev);
425 }
426 
427 static void dpu_kms_disable_commit(struct msm_kms *kms)
428 {
429 	struct dpu_kms *dpu_kms = to_dpu_kms(kms);
430 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
431 }
432 
433 static void dpu_kms_flush_commit(struct msm_kms *kms, unsigned crtc_mask)
434 {
435 	struct dpu_kms *dpu_kms = to_dpu_kms(kms);
436 	struct drm_crtc *crtc;
437 
438 	for_each_crtc_mask(dpu_kms->dev, crtc, crtc_mask) {
439 		if (!crtc->state->active)
440 			continue;
441 
442 		trace_dpu_kms_commit(DRMID(crtc));
443 		dpu_crtc_commit_kickoff(crtc);
444 	}
445 }
446 
447 static void dpu_kms_complete_commit(struct msm_kms *kms, unsigned crtc_mask)
448 {
449 	struct dpu_kms *dpu_kms = to_dpu_kms(kms);
450 	struct drm_crtc *crtc;
451 
452 	DPU_ATRACE_BEGIN("kms_complete_commit");
453 
454 	for_each_crtc_mask(dpu_kms->dev, crtc, crtc_mask)
455 		dpu_crtc_complete_commit(crtc);
456 
457 	DPU_ATRACE_END("kms_complete_commit");
458 }
459 
460 static void dpu_kms_wait_for_commit_done(struct msm_kms *kms,
461 		struct drm_crtc *crtc)
462 {
463 	struct drm_encoder *encoder;
464 	struct drm_device *dev;
465 	int ret;
466 
467 	if (!kms || !crtc || !crtc->state) {
468 		DPU_ERROR("invalid params\n");
469 		return;
470 	}
471 
472 	dev = crtc->dev;
473 
474 	if (!crtc->state->enable) {
475 		DPU_DEBUG("[crtc:%d] not enable\n", crtc->base.id);
476 		return;
477 	}
478 
479 	if (!drm_atomic_crtc_effectively_active(crtc->state)) {
480 		DPU_DEBUG("[crtc:%d] not active\n", crtc->base.id);
481 		return;
482 	}
483 
484 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
485 		if (encoder->crtc != crtc)
486 			continue;
487 		/*
488 		 * Wait for post-flush if necessary to delay before
489 		 * plane_cleanup. For example, wait for vsync in case of video
490 		 * mode panels. This may be a no-op for command mode panels.
491 		 */
492 		trace_dpu_kms_wait_for_commit_done(DRMID(crtc));
493 		ret = dpu_encoder_wait_for_commit_done(encoder);
494 		if (ret && ret != -EWOULDBLOCK) {
495 			DPU_ERROR("wait for commit done returned %d\n", ret);
496 			break;
497 		}
498 	}
499 }
500 
501 static void dpu_kms_wait_flush(struct msm_kms *kms, unsigned crtc_mask)
502 {
503 	struct dpu_kms *dpu_kms = to_dpu_kms(kms);
504 	struct drm_crtc *crtc;
505 
506 	for_each_crtc_mask(dpu_kms->dev, crtc, crtc_mask)
507 		dpu_kms_wait_for_commit_done(kms, crtc);
508 }
509 
510 static int _dpu_kms_initialize_dsi(struct drm_device *dev,
511 				    struct msm_drm_private *priv,
512 				    struct dpu_kms *dpu_kms)
513 {
514 	struct drm_encoder *encoder = NULL;
515 	struct msm_display_info info;
516 	int i, rc = 0;
517 
518 	if (!(priv->dsi[0] || priv->dsi[1]))
519 		return rc;
520 
521 	/*
522 	 * We support following confiurations:
523 	 * - Single DSI host (dsi0 or dsi1)
524 	 * - Two independent DSI hosts
525 	 * - Bonded DSI0 and DSI1 hosts
526 	 *
527 	 * TODO: Support swapping DSI0 and DSI1 in the bonded setup.
528 	 */
529 	for (i = 0; i < ARRAY_SIZE(priv->dsi); i++) {
530 		int other = (i + 1) % 2;
531 
532 		if (!priv->dsi[i])
533 			continue;
534 
535 		if (msm_dsi_is_bonded_dsi(priv->dsi[i]) &&
536 		    !msm_dsi_is_master_dsi(priv->dsi[i]))
537 			continue;
538 
539 		memset(&info, 0, sizeof(info));
540 		info.intf_type = INTF_DSI;
541 
542 		info.h_tile_instance[info.num_of_h_tiles++] = i;
543 		if (msm_dsi_is_bonded_dsi(priv->dsi[i]))
544 			info.h_tile_instance[info.num_of_h_tiles++] = other;
545 
546 		info.is_cmd_mode = msm_dsi_is_cmd_mode(priv->dsi[i]);
547 
548 		encoder = dpu_encoder_init(dev, DRM_MODE_ENCODER_DSI, &info);
549 		if (IS_ERR(encoder)) {
550 			DPU_ERROR("encoder init failed for dsi display\n");
551 			return PTR_ERR(encoder);
552 		}
553 
554 		rc = msm_dsi_modeset_init(priv->dsi[i], dev, encoder);
555 		if (rc) {
556 			DPU_ERROR("modeset_init failed for dsi[%d], rc = %d\n",
557 				i, rc);
558 			break;
559 		}
560 
561 		if (msm_dsi_is_bonded_dsi(priv->dsi[i]) && priv->dsi[other]) {
562 			rc = msm_dsi_modeset_init(priv->dsi[other], dev, encoder);
563 			if (rc) {
564 				DPU_ERROR("modeset_init failed for dsi[%d], rc = %d\n",
565 					other, rc);
566 				break;
567 			}
568 		}
569 	}
570 
571 	return rc;
572 }
573 
574 static int _dpu_kms_initialize_displayport(struct drm_device *dev,
575 					    struct msm_drm_private *priv,
576 					    struct dpu_kms *dpu_kms)
577 {
578 	struct drm_encoder *encoder = NULL;
579 	struct msm_display_info info;
580 	int rc;
581 	int i;
582 
583 	for (i = 0; i < ARRAY_SIZE(priv->dp); i++) {
584 		if (!priv->dp[i])
585 			continue;
586 
587 		memset(&info, 0, sizeof(info));
588 		info.num_of_h_tiles = 1;
589 		info.h_tile_instance[0] = i;
590 		info.intf_type = INTF_DP;
591 
592 		encoder = dpu_encoder_init(dev, DRM_MODE_ENCODER_TMDS, &info);
593 		if (IS_ERR(encoder)) {
594 			DPU_ERROR("encoder init failed for dsi display\n");
595 			return PTR_ERR(encoder);
596 		}
597 
598 		rc = msm_dp_modeset_init(priv->dp[i], dev, encoder);
599 		if (rc) {
600 			DPU_ERROR("modeset_init failed for DP, rc = %d\n", rc);
601 			drm_encoder_cleanup(encoder);
602 			return rc;
603 		}
604 	}
605 
606 	return 0;
607 }
608 
609 static int _dpu_kms_initialize_hdmi(struct drm_device *dev,
610 				    struct msm_drm_private *priv,
611 				    struct dpu_kms *dpu_kms)
612 {
613 	struct drm_encoder *encoder = NULL;
614 	struct msm_display_info info;
615 	int rc;
616 
617 	if (!priv->hdmi)
618 		return 0;
619 
620 	memset(&info, 0, sizeof(info));
621 	info.num_of_h_tiles = 1;
622 	info.h_tile_instance[0] = 0;
623 	info.intf_type = INTF_HDMI;
624 
625 	encoder = dpu_encoder_init(dev, DRM_MODE_ENCODER_TMDS, &info);
626 	if (IS_ERR(encoder)) {
627 		DPU_ERROR("encoder init failed for HDMI display\n");
628 		return PTR_ERR(encoder);
629 	}
630 
631 	rc = msm_hdmi_modeset_init(priv->hdmi, dev, encoder);
632 	if (rc) {
633 		DPU_ERROR("modeset_init failed for DP, rc = %d\n", rc);
634 		drm_encoder_cleanup(encoder);
635 		return rc;
636 	}
637 
638 	return 0;
639 }
640 
641 static int _dpu_kms_initialize_writeback(struct drm_device *dev,
642 		struct msm_drm_private *priv, struct dpu_kms *dpu_kms,
643 		const u32 *wb_formats, int n_formats)
644 {
645 	struct drm_encoder *encoder = NULL;
646 	struct msm_display_info info;
647 	int rc;
648 
649 	memset(&info, 0, sizeof(info));
650 
651 	info.num_of_h_tiles = 1;
652 	/* use only WB idx 2 instance for DPU */
653 	info.h_tile_instance[0] = WB_2;
654 	info.intf_type = INTF_WB;
655 
656 	encoder = dpu_encoder_init(dev, DRM_MODE_ENCODER_VIRTUAL, &info);
657 	if (IS_ERR(encoder)) {
658 		DPU_ERROR("encoder init failed for dsi display\n");
659 		return PTR_ERR(encoder);
660 	}
661 
662 	rc = dpu_writeback_init(dev, encoder, wb_formats,
663 			n_formats);
664 	if (rc) {
665 		DPU_ERROR("dpu_writeback_init, rc = %d\n", rc);
666 		drm_encoder_cleanup(encoder);
667 		return rc;
668 	}
669 
670 	return 0;
671 }
672 
673 /**
674  * _dpu_kms_setup_displays - create encoders, bridges and connectors
675  *                           for underlying displays
676  * @dev:        Pointer to drm device structure
677  * @priv:       Pointer to private drm device data
678  * @dpu_kms:    Pointer to dpu kms structure
679  * Returns:     Zero on success
680  */
681 static int _dpu_kms_setup_displays(struct drm_device *dev,
682 				    struct msm_drm_private *priv,
683 				    struct dpu_kms *dpu_kms)
684 {
685 	int rc = 0;
686 	int i;
687 
688 	rc = _dpu_kms_initialize_dsi(dev, priv, dpu_kms);
689 	if (rc) {
690 		DPU_ERROR("initialize_dsi failed, rc = %d\n", rc);
691 		return rc;
692 	}
693 
694 	rc = _dpu_kms_initialize_displayport(dev, priv, dpu_kms);
695 	if (rc) {
696 		DPU_ERROR("initialize_DP failed, rc = %d\n", rc);
697 		return rc;
698 	}
699 
700 	rc = _dpu_kms_initialize_hdmi(dev, priv, dpu_kms);
701 	if (rc) {
702 		DPU_ERROR("initialize HDMI failed, rc = %d\n", rc);
703 		return rc;
704 	}
705 
706 	/* Since WB isn't a driver check the catalog before initializing */
707 	if (dpu_kms->catalog->wb_count) {
708 		for (i = 0; i < dpu_kms->catalog->wb_count; i++) {
709 			if (dpu_kms->catalog->wb[i].id == WB_2) {
710 				rc = _dpu_kms_initialize_writeback(dev, priv, dpu_kms,
711 						dpu_kms->catalog->wb[i].format_list,
712 						dpu_kms->catalog->wb[i].num_formats);
713 				if (rc) {
714 					DPU_ERROR("initialize_WB failed, rc = %d\n", rc);
715 					return rc;
716 				}
717 			}
718 		}
719 	}
720 
721 	return rc;
722 }
723 
724 #define MAX_PLANES 20
725 static int _dpu_kms_drm_obj_init(struct dpu_kms *dpu_kms)
726 {
727 	struct drm_device *dev;
728 	struct drm_plane *primary_planes[MAX_PLANES], *plane;
729 	struct drm_plane *cursor_planes[MAX_PLANES] = { NULL };
730 	struct drm_crtc *crtc;
731 	struct drm_encoder *encoder;
732 	unsigned int num_encoders;
733 
734 	struct msm_drm_private *priv;
735 	const struct dpu_mdss_cfg *catalog;
736 
737 	int primary_planes_idx = 0, cursor_planes_idx = 0, i, ret;
738 	int max_crtc_count;
739 	dev = dpu_kms->dev;
740 	priv = dev->dev_private;
741 	catalog = dpu_kms->catalog;
742 
743 	/*
744 	 * Create encoder and query display drivers to create
745 	 * bridges and connectors
746 	 */
747 	ret = _dpu_kms_setup_displays(dev, priv, dpu_kms);
748 	if (ret)
749 		return ret;
750 
751 	num_encoders = 0;
752 	drm_for_each_encoder(encoder, dev)
753 		num_encoders++;
754 
755 	max_crtc_count = min(catalog->mixer_count, num_encoders);
756 
757 	/* Create the planes, keeping track of one primary/cursor per crtc */
758 	for (i = 0; i < catalog->sspp_count; i++) {
759 		enum drm_plane_type type;
760 
761 		if ((catalog->sspp[i].features & BIT(DPU_SSPP_CURSOR))
762 			&& cursor_planes_idx < max_crtc_count)
763 			type = DRM_PLANE_TYPE_CURSOR;
764 		else if (primary_planes_idx < max_crtc_count)
765 			type = DRM_PLANE_TYPE_PRIMARY;
766 		else
767 			type = DRM_PLANE_TYPE_OVERLAY;
768 
769 		DPU_DEBUG("Create plane type %d with features %lx (cur %lx)\n",
770 			  type, catalog->sspp[i].features,
771 			  catalog->sspp[i].features & BIT(DPU_SSPP_CURSOR));
772 
773 		plane = dpu_plane_init(dev, catalog->sspp[i].id, type,
774 				       (1UL << max_crtc_count) - 1);
775 		if (IS_ERR(plane)) {
776 			DPU_ERROR("dpu_plane_init failed\n");
777 			ret = PTR_ERR(plane);
778 			return ret;
779 		}
780 
781 		if (type == DRM_PLANE_TYPE_CURSOR)
782 			cursor_planes[cursor_planes_idx++] = plane;
783 		else if (type == DRM_PLANE_TYPE_PRIMARY)
784 			primary_planes[primary_planes_idx++] = plane;
785 	}
786 
787 	max_crtc_count = min(max_crtc_count, primary_planes_idx);
788 
789 	/* Create one CRTC per encoder */
790 	for (i = 0; i < max_crtc_count; i++) {
791 		crtc = dpu_crtc_init(dev, primary_planes[i], cursor_planes[i]);
792 		if (IS_ERR(crtc)) {
793 			ret = PTR_ERR(crtc);
794 			return ret;
795 		}
796 		priv->num_crtcs++;
797 	}
798 
799 	/* All CRTCs are compatible with all encoders */
800 	drm_for_each_encoder(encoder, dev)
801 		encoder->possible_crtcs = (1 << priv->num_crtcs) - 1;
802 
803 	return 0;
804 }
805 
806 static void _dpu_kms_hw_destroy(struct dpu_kms *dpu_kms)
807 {
808 	int i;
809 
810 	if (dpu_kms->hw_intr)
811 		dpu_hw_intr_destroy(dpu_kms->hw_intr);
812 	dpu_kms->hw_intr = NULL;
813 
814 	/* safe to call these more than once during shutdown */
815 	_dpu_kms_mmu_destroy(dpu_kms);
816 
817 	if (dpu_kms->catalog) {
818 		for (i = 0; i < ARRAY_SIZE(dpu_kms->hw_vbif); i++) {
819 			if (dpu_kms->hw_vbif[i]) {
820 				dpu_hw_vbif_destroy(dpu_kms->hw_vbif[i]);
821 				dpu_kms->hw_vbif[i] = NULL;
822 			}
823 		}
824 	}
825 
826 	if (dpu_kms->rm_init)
827 		dpu_rm_destroy(&dpu_kms->rm);
828 	dpu_kms->rm_init = false;
829 
830 	dpu_kms->catalog = NULL;
831 
832 	if (dpu_kms->vbif[VBIF_NRT])
833 		devm_iounmap(&dpu_kms->pdev->dev, dpu_kms->vbif[VBIF_NRT]);
834 	dpu_kms->vbif[VBIF_NRT] = NULL;
835 
836 	if (dpu_kms->vbif[VBIF_RT])
837 		devm_iounmap(&dpu_kms->pdev->dev, dpu_kms->vbif[VBIF_RT]);
838 	dpu_kms->vbif[VBIF_RT] = NULL;
839 
840 	if (dpu_kms->hw_mdp)
841 		dpu_hw_mdp_destroy(dpu_kms->hw_mdp);
842 	dpu_kms->hw_mdp = NULL;
843 
844 	if (dpu_kms->mmio)
845 		devm_iounmap(&dpu_kms->pdev->dev, dpu_kms->mmio);
846 	dpu_kms->mmio = NULL;
847 }
848 
849 static void dpu_kms_destroy(struct msm_kms *kms)
850 {
851 	struct dpu_kms *dpu_kms;
852 
853 	if (!kms) {
854 		DPU_ERROR("invalid kms\n");
855 		return;
856 	}
857 
858 	dpu_kms = to_dpu_kms(kms);
859 
860 	_dpu_kms_hw_destroy(dpu_kms);
861 
862 	msm_kms_destroy(&dpu_kms->base);
863 
864 	if (dpu_kms->rpm_enabled)
865 		pm_runtime_disable(&dpu_kms->pdev->dev);
866 }
867 
868 static int dpu_irq_postinstall(struct msm_kms *kms)
869 {
870 	struct msm_drm_private *priv;
871 	struct dpu_kms *dpu_kms = to_dpu_kms(kms);
872 	int i;
873 
874 	if (!dpu_kms || !dpu_kms->dev)
875 		return -EINVAL;
876 
877 	priv = dpu_kms->dev->dev_private;
878 	if (!priv)
879 		return -EINVAL;
880 
881 	for (i = 0; i < ARRAY_SIZE(priv->dp); i++)
882 		msm_dp_irq_postinstall(priv->dp[i]);
883 
884 	return 0;
885 }
886 
887 static void dpu_kms_mdp_snapshot(struct msm_disp_state *disp_state, struct msm_kms *kms)
888 {
889 	int i;
890 	struct dpu_kms *dpu_kms;
891 	const struct dpu_mdss_cfg *cat;
892 	void __iomem *base;
893 
894 	dpu_kms = to_dpu_kms(kms);
895 
896 	cat = dpu_kms->catalog;
897 
898 	pm_runtime_get_sync(&dpu_kms->pdev->dev);
899 
900 	/* dump CTL sub-blocks HW regs info */
901 	for (i = 0; i < cat->ctl_count; i++)
902 		msm_disp_snapshot_add_block(disp_state, cat->ctl[i].len,
903 				dpu_kms->mmio + cat->ctl[i].base, cat->ctl[i].name);
904 
905 	/* dump DSPP sub-blocks HW regs info */
906 	for (i = 0; i < cat->dspp_count; i++) {
907 		base = dpu_kms->mmio + cat->dspp[i].base;
908 		msm_disp_snapshot_add_block(disp_state, cat->dspp[i].len, base, cat->dspp[i].name);
909 
910 		if (cat->dspp[i].sblk && cat->dspp[i].sblk->pcc.len > 0)
911 			msm_disp_snapshot_add_block(disp_state, cat->dspp[i].sblk->pcc.len,
912 						    base + cat->dspp[i].sblk->pcc.base, "%s_%s",
913 						    cat->dspp[i].name,
914 						    cat->dspp[i].sblk->pcc.name);
915 	}
916 
917 	/* dump INTF sub-blocks HW regs info */
918 	for (i = 0; i < cat->intf_count; i++)
919 		msm_disp_snapshot_add_block(disp_state, cat->intf[i].len,
920 				dpu_kms->mmio + cat->intf[i].base, cat->intf[i].name);
921 
922 	/* dump PP sub-blocks HW regs info */
923 	for (i = 0; i < cat->pingpong_count; i++) {
924 		base = dpu_kms->mmio + cat->pingpong[i].base;
925 		msm_disp_snapshot_add_block(disp_state, cat->pingpong[i].len, base,
926 					    cat->pingpong[i].name);
927 
928 		/* TE2 sub-block has length of 0, so will not print it */
929 
930 		if (cat->pingpong[i].sblk && cat->pingpong[i].sblk->dither.len > 0)
931 			msm_disp_snapshot_add_block(disp_state, cat->pingpong[i].sblk->dither.len,
932 						    base + cat->pingpong[i].sblk->dither.base,
933 						    "%s_%s", cat->pingpong[i].name,
934 						    cat->pingpong[i].sblk->dither.name);
935 	}
936 
937 	/* dump SSPP sub-blocks HW regs info */
938 	for (i = 0; i < cat->sspp_count; i++) {
939 		base = dpu_kms->mmio + cat->sspp[i].base;
940 		msm_disp_snapshot_add_block(disp_state, cat->sspp[i].len, base, cat->sspp[i].name);
941 
942 		if (cat->sspp[i].sblk && cat->sspp[i].sblk->scaler_blk.len > 0)
943 			msm_disp_snapshot_add_block(disp_state, cat->sspp[i].sblk->scaler_blk.len,
944 						    base + cat->sspp[i].sblk->scaler_blk.base,
945 						    "%s_%s", cat->sspp[i].name,
946 						    cat->sspp[i].sblk->scaler_blk.name);
947 
948 		if (cat->sspp[i].sblk && cat->sspp[i].sblk->csc_blk.len > 0)
949 			msm_disp_snapshot_add_block(disp_state, cat->sspp[i].sblk->csc_blk.len,
950 						    base + cat->sspp[i].sblk->csc_blk.base,
951 						    "%s_%s", cat->sspp[i].name,
952 						    cat->sspp[i].sblk->csc_blk.name);
953 	}
954 
955 	/* dump LM sub-blocks HW regs info */
956 	for (i = 0; i < cat->mixer_count; i++)
957 		msm_disp_snapshot_add_block(disp_state, cat->mixer[i].len,
958 				dpu_kms->mmio + cat->mixer[i].base, cat->mixer[i].name);
959 
960 	/* dump WB sub-blocks HW regs info */
961 	for (i = 0; i < cat->wb_count; i++)
962 		msm_disp_snapshot_add_block(disp_state, cat->wb[i].len,
963 				dpu_kms->mmio + cat->wb[i].base, cat->wb[i].name);
964 
965 	if (cat->mdp[0].features & BIT(DPU_MDP_PERIPH_0_REMOVED)) {
966 		msm_disp_snapshot_add_block(disp_state, MDP_PERIPH_TOP0,
967 				dpu_kms->mmio + cat->mdp[0].base, "top");
968 		msm_disp_snapshot_add_block(disp_state, cat->mdp[0].len - MDP_PERIPH_TOP0_END,
969 				dpu_kms->mmio + cat->mdp[0].base + MDP_PERIPH_TOP0_END, "top_2");
970 	} else {
971 		msm_disp_snapshot_add_block(disp_state, cat->mdp[0].len,
972 				dpu_kms->mmio + cat->mdp[0].base, "top");
973 	}
974 
975 	/* dump DSC sub-blocks HW regs info */
976 	for (i = 0; i < cat->dsc_count; i++) {
977 		base = dpu_kms->mmio + cat->dsc[i].base;
978 		msm_disp_snapshot_add_block(disp_state, cat->dsc[i].len, base, cat->dsc[i].name);
979 
980 		if (cat->dsc[i].features & BIT(DPU_DSC_HW_REV_1_2)) {
981 			struct dpu_dsc_blk enc = cat->dsc[i].sblk->enc;
982 			struct dpu_dsc_blk ctl = cat->dsc[i].sblk->ctl;
983 
984 			msm_disp_snapshot_add_block(disp_state, enc.len, base + enc.base, "%s_%s",
985 						    cat->dsc[i].name, enc.name);
986 			msm_disp_snapshot_add_block(disp_state, ctl.len, base + ctl.base, "%s_%s",
987 						    cat->dsc[i].name, ctl.name);
988 		}
989 	}
990 
991 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
992 }
993 
994 static const struct msm_kms_funcs kms_funcs = {
995 	.hw_init         = dpu_kms_hw_init,
996 	.irq_preinstall  = dpu_core_irq_preinstall,
997 	.irq_postinstall = dpu_irq_postinstall,
998 	.irq_uninstall   = dpu_core_irq_uninstall,
999 	.irq             = dpu_core_irq,
1000 	.enable_commit   = dpu_kms_enable_commit,
1001 	.disable_commit  = dpu_kms_disable_commit,
1002 	.flush_commit    = dpu_kms_flush_commit,
1003 	.wait_flush      = dpu_kms_wait_flush,
1004 	.complete_commit = dpu_kms_complete_commit,
1005 	.enable_vblank   = dpu_kms_enable_vblank,
1006 	.disable_vblank  = dpu_kms_disable_vblank,
1007 	.check_modified_format = dpu_format_check_modified_format,
1008 	.get_format      = dpu_get_msm_format,
1009 	.destroy         = dpu_kms_destroy,
1010 	.snapshot        = dpu_kms_mdp_snapshot,
1011 #ifdef CONFIG_DEBUG_FS
1012 	.debugfs_init    = dpu_kms_debugfs_init,
1013 #endif
1014 };
1015 
1016 static void _dpu_kms_mmu_destroy(struct dpu_kms *dpu_kms)
1017 {
1018 	struct msm_mmu *mmu;
1019 
1020 	if (!dpu_kms->base.aspace)
1021 		return;
1022 
1023 	mmu = dpu_kms->base.aspace->mmu;
1024 
1025 	mmu->funcs->detach(mmu);
1026 	msm_gem_address_space_put(dpu_kms->base.aspace);
1027 
1028 	dpu_kms->base.aspace = NULL;
1029 }
1030 
1031 static int _dpu_kms_mmu_init(struct dpu_kms *dpu_kms)
1032 {
1033 	struct msm_gem_address_space *aspace;
1034 
1035 	aspace = msm_kms_init_aspace(dpu_kms->dev);
1036 	if (IS_ERR(aspace))
1037 		return PTR_ERR(aspace);
1038 
1039 	dpu_kms->base.aspace = aspace;
1040 
1041 	return 0;
1042 }
1043 
1044 unsigned long dpu_kms_get_clk_rate(struct dpu_kms *dpu_kms, char *clock_name)
1045 {
1046 	struct clk *clk;
1047 
1048 	clk = msm_clk_bulk_get_clock(dpu_kms->clocks, dpu_kms->num_clocks, clock_name);
1049 	if (!clk)
1050 		return 0;
1051 
1052 	return clk_get_rate(clk);
1053 }
1054 
1055 #define	DPU_PERF_DEFAULT_MAX_CORE_CLK_RATE	412500000
1056 
1057 static int dpu_kms_hw_init(struct msm_kms *kms)
1058 {
1059 	struct dpu_kms *dpu_kms;
1060 	struct drm_device *dev;
1061 	int i, rc = -EINVAL;
1062 	unsigned long max_core_clk_rate;
1063 	u32 core_rev;
1064 
1065 	if (!kms) {
1066 		DPU_ERROR("invalid kms\n");
1067 		return rc;
1068 	}
1069 
1070 	dpu_kms = to_dpu_kms(kms);
1071 	dev = dpu_kms->dev;
1072 
1073 	dev->mode_config.cursor_width = 512;
1074 	dev->mode_config.cursor_height = 512;
1075 
1076 	rc = dpu_kms_global_obj_init(dpu_kms);
1077 	if (rc)
1078 		return rc;
1079 
1080 	atomic_set(&dpu_kms->bandwidth_ref, 0);
1081 
1082 	dpu_kms->mmio = msm_ioremap(dpu_kms->pdev, "mdp");
1083 	if (IS_ERR(dpu_kms->mmio)) {
1084 		rc = PTR_ERR(dpu_kms->mmio);
1085 		DPU_ERROR("mdp register memory map failed: %d\n", rc);
1086 		dpu_kms->mmio = NULL;
1087 		goto error;
1088 	}
1089 	DRM_DEBUG("mapped dpu address space @%pK\n", dpu_kms->mmio);
1090 
1091 	dpu_kms->vbif[VBIF_RT] = msm_ioremap(dpu_kms->pdev, "vbif");
1092 	if (IS_ERR(dpu_kms->vbif[VBIF_RT])) {
1093 		rc = PTR_ERR(dpu_kms->vbif[VBIF_RT]);
1094 		DPU_ERROR("vbif register memory map failed: %d\n", rc);
1095 		dpu_kms->vbif[VBIF_RT] = NULL;
1096 		goto error;
1097 	}
1098 	dpu_kms->vbif[VBIF_NRT] = msm_ioremap_quiet(dpu_kms->pdev, "vbif_nrt");
1099 	if (IS_ERR(dpu_kms->vbif[VBIF_NRT])) {
1100 		dpu_kms->vbif[VBIF_NRT] = NULL;
1101 		DPU_DEBUG("VBIF NRT is not defined");
1102 	}
1103 
1104 	dpu_kms_parse_data_bus_icc_path(dpu_kms);
1105 
1106 	rc = pm_runtime_resume_and_get(&dpu_kms->pdev->dev);
1107 	if (rc < 0)
1108 		goto error;
1109 
1110 	core_rev = readl_relaxed(dpu_kms->mmio + 0x0);
1111 
1112 	pr_info("dpu hardware revision:0x%x\n", core_rev);
1113 
1114 	dpu_kms->catalog = of_device_get_match_data(dev->dev);
1115 	if (!dpu_kms->catalog) {
1116 		DPU_ERROR("device config not known!\n");
1117 		rc = -EINVAL;
1118 		goto power_error;
1119 	}
1120 
1121 	/*
1122 	 * Now we need to read the HW catalog and initialize resources such as
1123 	 * clocks, regulators, GDSC/MMAGIC, ioremap the register ranges etc
1124 	 */
1125 	rc = _dpu_kms_mmu_init(dpu_kms);
1126 	if (rc) {
1127 		DPU_ERROR("dpu_kms_mmu_init failed: %d\n", rc);
1128 		goto power_error;
1129 	}
1130 
1131 	dpu_kms->mdss = msm_mdss_get_mdss_data(dpu_kms->pdev->dev.parent);
1132 	if (IS_ERR(dpu_kms->mdss)) {
1133 		rc = PTR_ERR(dpu_kms->mdss);
1134 		DPU_ERROR("failed to get MDSS data: %d\n", rc);
1135 		goto power_error;
1136 	}
1137 
1138 	if (!dpu_kms->mdss) {
1139 		rc = -EINVAL;
1140 		DPU_ERROR("NULL MDSS data\n");
1141 		goto power_error;
1142 	}
1143 
1144 	rc = dpu_rm_init(&dpu_kms->rm, dpu_kms->catalog, dpu_kms->mdss, dpu_kms->mmio);
1145 	if (rc) {
1146 		DPU_ERROR("rm init failed: %d\n", rc);
1147 		goto power_error;
1148 	}
1149 
1150 	dpu_kms->rm_init = true;
1151 
1152 	dpu_kms->hw_mdp = dpu_hw_mdptop_init(dpu_kms->catalog->mdp,
1153 					     dpu_kms->mmio,
1154 					     dpu_kms->catalog);
1155 	if (IS_ERR(dpu_kms->hw_mdp)) {
1156 		rc = PTR_ERR(dpu_kms->hw_mdp);
1157 		DPU_ERROR("failed to get hw_mdp: %d\n", rc);
1158 		dpu_kms->hw_mdp = NULL;
1159 		goto power_error;
1160 	}
1161 
1162 	for (i = 0; i < dpu_kms->catalog->vbif_count; i++) {
1163 		struct dpu_hw_vbif *hw;
1164 		const struct dpu_vbif_cfg *vbif = &dpu_kms->catalog->vbif[i];
1165 
1166 		hw = dpu_hw_vbif_init(vbif, dpu_kms->vbif[vbif->id]);
1167 		if (IS_ERR(hw)) {
1168 			rc = PTR_ERR(hw);
1169 			DPU_ERROR("failed to init vbif %d: %d\n", vbif->id, rc);
1170 			goto power_error;
1171 		}
1172 
1173 		dpu_kms->hw_vbif[vbif->id] = hw;
1174 	}
1175 
1176 	/* TODO: use the same max_freq as in dpu_kms_hw_init */
1177 	max_core_clk_rate = dpu_kms_get_clk_rate(dpu_kms, "core");
1178 	if (!max_core_clk_rate) {
1179 		DPU_DEBUG("max core clk rate not determined, using default\n");
1180 		max_core_clk_rate = DPU_PERF_DEFAULT_MAX_CORE_CLK_RATE;
1181 	}
1182 
1183 	rc = dpu_core_perf_init(&dpu_kms->perf, dpu_kms->catalog->perf, max_core_clk_rate);
1184 	if (rc) {
1185 		DPU_ERROR("failed to init perf %d\n", rc);
1186 		goto perf_err;
1187 	}
1188 
1189 	dpu_kms->hw_intr = dpu_hw_intr_init(dpu_kms->mmio, dpu_kms->catalog);
1190 	if (IS_ERR_OR_NULL(dpu_kms->hw_intr)) {
1191 		rc = PTR_ERR(dpu_kms->hw_intr);
1192 		DPU_ERROR("hw_intr init failed: %d\n", rc);
1193 		dpu_kms->hw_intr = NULL;
1194 		goto hw_intr_init_err;
1195 	}
1196 
1197 	dev->mode_config.min_width = 0;
1198 	dev->mode_config.min_height = 0;
1199 
1200 	/*
1201 	 * max crtc width is equal to the max mixer width * 2 and max height is
1202 	 * is 4K
1203 	 */
1204 	dev->mode_config.max_width =
1205 			dpu_kms->catalog->caps->max_mixer_width * 2;
1206 	dev->mode_config.max_height = 4096;
1207 
1208 	dev->max_vblank_count = 0xffffffff;
1209 	/* Disable vblank irqs aggressively for power-saving */
1210 	dev->vblank_disable_immediate = true;
1211 
1212 	/*
1213 	 * _dpu_kms_drm_obj_init should create the DRM related objects
1214 	 * i.e. CRTCs, planes, encoders, connectors and so forth
1215 	 */
1216 	rc = _dpu_kms_drm_obj_init(dpu_kms);
1217 	if (rc) {
1218 		DPU_ERROR("modeset init failed: %d\n", rc);
1219 		goto drm_obj_init_err;
1220 	}
1221 
1222 	dpu_vbif_init_memtypes(dpu_kms);
1223 
1224 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
1225 
1226 	return 0;
1227 
1228 drm_obj_init_err:
1229 hw_intr_init_err:
1230 perf_err:
1231 power_error:
1232 	pm_runtime_put_sync(&dpu_kms->pdev->dev);
1233 error:
1234 	_dpu_kms_hw_destroy(dpu_kms);
1235 
1236 	return rc;
1237 }
1238 
1239 static int dpu_kms_init(struct drm_device *ddev)
1240 {
1241 	struct msm_drm_private *priv = ddev->dev_private;
1242 	struct device *dev = ddev->dev;
1243 	struct platform_device *pdev = to_platform_device(dev);
1244 	struct dpu_kms *dpu_kms;
1245 	int irq;
1246 	struct dev_pm_opp *opp;
1247 	int ret = 0;
1248 	unsigned long max_freq = ULONG_MAX;
1249 
1250 	dpu_kms = devm_kzalloc(&pdev->dev, sizeof(*dpu_kms), GFP_KERNEL);
1251 	if (!dpu_kms)
1252 		return -ENOMEM;
1253 
1254 	ret = devm_pm_opp_set_clkname(dev, "core");
1255 	if (ret)
1256 		return ret;
1257 	/* OPP table is optional */
1258 	ret = devm_pm_opp_of_add_table(dev);
1259 	if (ret && ret != -ENODEV) {
1260 		dev_err(dev, "invalid OPP table in device tree\n");
1261 		return ret;
1262 	}
1263 
1264 	ret = devm_clk_bulk_get_all(&pdev->dev, &dpu_kms->clocks);
1265 	if (ret < 0) {
1266 		DPU_ERROR("failed to parse clocks, ret=%d\n", ret);
1267 		return ret;
1268 	}
1269 	dpu_kms->num_clocks = ret;
1270 
1271 	opp = dev_pm_opp_find_freq_floor(dev, &max_freq);
1272 	if (!IS_ERR(opp))
1273 		dev_pm_opp_put(opp);
1274 
1275 	dev_pm_opp_set_rate(dev, max_freq);
1276 
1277 	ret = msm_kms_init(&dpu_kms->base, &kms_funcs);
1278 	if (ret) {
1279 		DPU_ERROR("failed to init kms, ret=%d\n", ret);
1280 		return ret;
1281 	}
1282 	dpu_kms->dev = ddev;
1283 	dpu_kms->pdev = pdev;
1284 
1285 	pm_runtime_enable(&pdev->dev);
1286 	dpu_kms->rpm_enabled = true;
1287 
1288 	priv->kms = &dpu_kms->base;
1289 
1290 	irq = irq_of_parse_and_map(dpu_kms->pdev->dev.of_node, 0);
1291 	if (!irq) {
1292 		DPU_ERROR("failed to get irq\n");
1293 		return -EINVAL;
1294 	}
1295 	dpu_kms->base.irq = irq;
1296 
1297 	return 0;
1298 }
1299 
1300 static int dpu_dev_probe(struct platform_device *pdev)
1301 {
1302 	return msm_drv_probe(&pdev->dev, dpu_kms_init);
1303 }
1304 
1305 static int dpu_dev_remove(struct platform_device *pdev)
1306 {
1307 	component_master_del(&pdev->dev, &msm_drm_ops);
1308 
1309 	return 0;
1310 }
1311 
1312 static int __maybe_unused dpu_runtime_suspend(struct device *dev)
1313 {
1314 	int i;
1315 	struct platform_device *pdev = to_platform_device(dev);
1316 	struct msm_drm_private *priv = platform_get_drvdata(pdev);
1317 	struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
1318 
1319 	/* Drop the performance state vote */
1320 	dev_pm_opp_set_rate(dev, 0);
1321 	clk_bulk_disable_unprepare(dpu_kms->num_clocks, dpu_kms->clocks);
1322 
1323 	for (i = 0; i < dpu_kms->num_paths; i++)
1324 		icc_set_bw(dpu_kms->path[i], 0, 0);
1325 
1326 	return 0;
1327 }
1328 
1329 static int __maybe_unused dpu_runtime_resume(struct device *dev)
1330 {
1331 	int rc = -1;
1332 	struct platform_device *pdev = to_platform_device(dev);
1333 	struct msm_drm_private *priv = platform_get_drvdata(pdev);
1334 	struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
1335 	struct drm_encoder *encoder;
1336 	struct drm_device *ddev;
1337 
1338 	ddev = dpu_kms->dev;
1339 
1340 	rc = clk_bulk_prepare_enable(dpu_kms->num_clocks, dpu_kms->clocks);
1341 	if (rc) {
1342 		DPU_ERROR("clock enable failed rc:%d\n", rc);
1343 		return rc;
1344 	}
1345 
1346 	dpu_vbif_init_memtypes(dpu_kms);
1347 
1348 	drm_for_each_encoder(encoder, ddev)
1349 		dpu_encoder_virt_runtime_resume(encoder);
1350 
1351 	return rc;
1352 }
1353 
1354 static const struct dev_pm_ops dpu_pm_ops = {
1355 	SET_RUNTIME_PM_OPS(dpu_runtime_suspend, dpu_runtime_resume, NULL)
1356 	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1357 				pm_runtime_force_resume)
1358 	.prepare = msm_pm_prepare,
1359 	.complete = msm_pm_complete,
1360 };
1361 
1362 static const struct of_device_id dpu_dt_match[] = {
1363 	{ .compatible = "qcom,msm8998-dpu", .data = &dpu_msm8998_cfg, },
1364 	{ .compatible = "qcom,qcm2290-dpu", .data = &dpu_qcm2290_cfg, },
1365 	{ .compatible = "qcom,sdm845-dpu", .data = &dpu_sdm845_cfg, },
1366 	{ .compatible = "qcom,sc7180-dpu", .data = &dpu_sc7180_cfg, },
1367 	{ .compatible = "qcom,sc7280-dpu", .data = &dpu_sc7280_cfg, },
1368 	{ .compatible = "qcom,sc8180x-dpu", .data = &dpu_sc8180x_cfg, },
1369 	{ .compatible = "qcom,sc8280xp-dpu", .data = &dpu_sc8280xp_cfg, },
1370 	{ .compatible = "qcom,sm6115-dpu", .data = &dpu_sm6115_cfg, },
1371 	{ .compatible = "qcom,sm6125-dpu", .data = &dpu_sm6125_cfg, },
1372 	{ .compatible = "qcom,sm6350-dpu", .data = &dpu_sm6350_cfg, },
1373 	{ .compatible = "qcom,sm6375-dpu", .data = &dpu_sm6375_cfg, },
1374 	{ .compatible = "qcom,sm8150-dpu", .data = &dpu_sm8150_cfg, },
1375 	{ .compatible = "qcom,sm8250-dpu", .data = &dpu_sm8250_cfg, },
1376 	{ .compatible = "qcom,sm8350-dpu", .data = &dpu_sm8350_cfg, },
1377 	{ .compatible = "qcom,sm8450-dpu", .data = &dpu_sm8450_cfg, },
1378 	{ .compatible = "qcom,sm8550-dpu", .data = &dpu_sm8550_cfg, },
1379 	{}
1380 };
1381 MODULE_DEVICE_TABLE(of, dpu_dt_match);
1382 
1383 static struct platform_driver dpu_driver = {
1384 	.probe = dpu_dev_probe,
1385 	.remove = dpu_dev_remove,
1386 	.shutdown = msm_drv_shutdown,
1387 	.driver = {
1388 		.name = "msm_dpu",
1389 		.of_match_table = dpu_dt_match,
1390 		.pm = &dpu_pm_ops,
1391 	},
1392 };
1393 
1394 void __init msm_dpu_register(void)
1395 {
1396 	platform_driver_register(&dpu_driver);
1397 }
1398 
1399 void __exit msm_dpu_unregister(void)
1400 {
1401 	platform_driver_unregister(&dpu_driver);
1402 }
1403