xref: /linux/drivers/gpu/drm/msm/disp/dpu1/dpu_writeback.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  */
5 
6 #include <drm/drm_edid.h>
7 #include <drm/drm_framebuffer.h>
8 
9 #include "dpu_writeback.h"
10 
11 static int dpu_wb_conn_get_modes(struct drm_connector *connector)
12 {
13 	struct drm_device *dev = connector->dev;
14 	struct msm_drm_private *priv = dev->dev_private;
15 	struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
16 
17 	/*
18 	 * We should ideally be limiting the modes only to the maxlinewidth but
19 	 * on some chipsets this will allow even 4k modes to be added which will
20 	 * fail the per SSPP bandwidth checks. So, till we have dual-SSPP support
21 	 * and source split support added lets limit the modes based on max_mixer_width
22 	 * as 4K modes can then be supported.
23 	 */
24 	return drm_add_modes_noedid(connector, dpu_kms->catalog->caps->max_mixer_width,
25 			dev->mode_config.max_height);
26 }
27 
28 static int dpu_wb_conn_atomic_check(struct drm_connector *connector,
29 				    struct drm_atomic_state *state)
30 {
31 	struct drm_writeback_connector *wb_conn = drm_connector_to_writeback(connector);
32 	struct dpu_wb_connector *dpu_wb_conn = to_dpu_wb_conn(wb_conn);
33 	struct drm_connector_state *conn_state =
34 		drm_atomic_get_new_connector_state(state, connector);
35 	struct drm_crtc *crtc;
36 	struct drm_crtc_state *crtc_state;
37 	const struct drm_display_mode *mode;
38 	struct drm_framebuffer *fb;
39 
40 	DPU_DEBUG("[atomic_check:%d]\n", connector->base.id);
41 
42 	if (!conn_state || !conn_state->connector) {
43 		DPU_ERROR("invalid connector state\n");
44 		return -EINVAL;
45 	} else if (conn_state->connector->status != connector_status_connected) {
46 		DPU_ERROR("connector not connected %d\n", conn_state->connector->status);
47 		return -EINVAL;
48 	}
49 
50 	crtc = conn_state->crtc;
51 	if (!crtc)
52 		return 0;
53 
54 	if (!conn_state->writeback_job || !conn_state->writeback_job->fb)
55 		return 0;
56 
57 	crtc_state = drm_atomic_get_crtc_state(state, crtc);
58 	if (IS_ERR(crtc_state))
59 		return PTR_ERR(crtc_state);
60 
61 	mode = &crtc_state->mode;
62 
63 	fb = conn_state->writeback_job->fb;
64 
65 	DPU_DEBUG("[fb_id:%u][fb:%u,%u][mode:\"%s\":%ux%u]\n", fb->base.id, fb->width, fb->height,
66 		  mode->name, mode->hdisplay, mode->vdisplay);
67 
68 	if (fb->width != mode->hdisplay) {
69 		DPU_ERROR("invalid fb w=%d, mode w=%d\n", fb->width, mode->hdisplay);
70 		return -EINVAL;
71 	} else if (fb->height != mode->vdisplay) {
72 		DPU_ERROR("invalid fb h=%d, mode h=%d\n", fb->height, mode->vdisplay);
73 		return -EINVAL;
74 	} else if (fb->width > dpu_wb_conn->maxlinewidth) {
75 		DPU_ERROR("invalid fb w=%d, maxlinewidth=%u\n",
76 			  fb->width, dpu_wb_conn->maxlinewidth);
77 		return -EINVAL;
78 	}
79 
80 	return drm_atomic_helper_check_wb_connector_state(conn_state->connector, conn_state->state);
81 }
82 
83 static const struct drm_connector_funcs dpu_wb_conn_funcs = {
84 	.reset = drm_atomic_helper_connector_reset,
85 	.fill_modes = drm_helper_probe_single_connector_modes,
86 	.destroy = drm_connector_cleanup,
87 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
88 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
89 };
90 
91 static int dpu_wb_conn_prepare_job(struct drm_writeback_connector *connector,
92 		struct drm_writeback_job *job)
93 {
94 
95 	struct dpu_wb_connector *dpu_wb_conn = to_dpu_wb_conn(connector);
96 
97 	if (!job->fb)
98 		return 0;
99 
100 	dpu_encoder_prepare_wb_job(dpu_wb_conn->wb_enc, job);
101 
102 	return 0;
103 }
104 
105 static void dpu_wb_conn_cleanup_job(struct drm_writeback_connector *connector,
106 		struct drm_writeback_job *job)
107 {
108 	struct dpu_wb_connector *dpu_wb_conn = to_dpu_wb_conn(connector);
109 
110 	if (!job->fb)
111 		return;
112 
113 	dpu_encoder_cleanup_wb_job(dpu_wb_conn->wb_enc, job);
114 }
115 
116 static const struct drm_connector_helper_funcs dpu_wb_conn_helper_funcs = {
117 	.get_modes = dpu_wb_conn_get_modes,
118 	.atomic_check = dpu_wb_conn_atomic_check,
119 	.prepare_writeback_job = dpu_wb_conn_prepare_job,
120 	.cleanup_writeback_job = dpu_wb_conn_cleanup_job,
121 };
122 
123 int dpu_writeback_init(struct drm_device *dev, struct drm_encoder *enc,
124 		const u32 *format_list, u32 num_formats, u32 maxlinewidth)
125 {
126 	struct dpu_wb_connector *dpu_wb_conn;
127 	int rc = 0;
128 
129 	dpu_wb_conn = devm_kzalloc(dev->dev, sizeof(*dpu_wb_conn), GFP_KERNEL);
130 	if (!dpu_wb_conn)
131 		return -ENOMEM;
132 
133 	dpu_wb_conn->maxlinewidth = maxlinewidth;
134 
135 	drm_connector_helper_add(&dpu_wb_conn->base.base, &dpu_wb_conn_helper_funcs);
136 
137 	/* DPU initializes the encoder and sets it up completely for writeback
138 	 * cases and hence should use the new API drm_writeback_connector_init_with_encoder
139 	 * to initialize the writeback connector
140 	 */
141 	rc = drm_writeback_connector_init_with_encoder(dev, &dpu_wb_conn->base, enc,
142 			&dpu_wb_conn_funcs, format_list, num_formats);
143 
144 	if (!rc)
145 		dpu_wb_conn->wb_enc = enc;
146 
147 	return rc;
148 }
149