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