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