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