xref: /linux/drivers/gpu/drm/msm/disp/dpu1/dpu_encoder_phys_wb.c (revision 260f6f4fda93c8485c8037865c941b42b9cba5d2)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
4  */
5 
6 #define pr_fmt(fmt)	"[drm:%s:%d] " fmt, __func__, __LINE__
7 
8 #include <linux/debugfs.h>
9 
10 #include <drm/drm_framebuffer.h>
11 #include <drm/drm_managed.h>
12 
13 #include "dpu_encoder_phys.h"
14 #include "dpu_formats.h"
15 #include "dpu_hw_top.h"
16 #include "dpu_hw_wb.h"
17 #include "dpu_hw_lm.h"
18 #include "dpu_hw_merge3d.h"
19 #include "dpu_hw_interrupts.h"
20 #include "dpu_core_irq.h"
21 #include "dpu_vbif.h"
22 #include "dpu_crtc.h"
23 #include "disp/msm_disp_snapshot.h"
24 
25 #define to_dpu_encoder_phys_wb(x) \
26 	container_of(x, struct dpu_encoder_phys_wb, base)
27 
28 /**
29  * dpu_encoder_phys_wb_is_master - report wb always as master encoder
30  * @phys_enc:	Pointer to physical encoder
31  */
dpu_encoder_phys_wb_is_master(struct dpu_encoder_phys * phys_enc)32 static bool dpu_encoder_phys_wb_is_master(struct dpu_encoder_phys *phys_enc)
33 {
34 	/* there is only one physical enc for dpu_writeback */
35 	return true;
36 }
37 
_dpu_encoder_phys_wb_clk_force_ctrl(struct dpu_hw_wb * wb,struct dpu_hw_mdp * mdp,bool enable,bool * forced_on)38 static bool _dpu_encoder_phys_wb_clk_force_ctrl(struct dpu_hw_wb *wb,
39 						struct dpu_hw_mdp *mdp,
40 						bool enable, bool *forced_on)
41 {
42 	if (wb->ops.setup_clk_force_ctrl) {
43 		*forced_on = wb->ops.setup_clk_force_ctrl(wb, enable);
44 		return true;
45 	}
46 
47 	if (mdp->ops.setup_clk_force_ctrl) {
48 		*forced_on = mdp->ops.setup_clk_force_ctrl(mdp, wb->caps->clk_ctrl, enable);
49 		return true;
50 	}
51 
52 	return false;
53 }
54 
55 /**
56  * dpu_encoder_phys_wb_set_ot_limit - set OT limit for writeback interface
57  * @phys_enc:	Pointer to physical encoder
58  */
dpu_encoder_phys_wb_set_ot_limit(struct dpu_encoder_phys * phys_enc)59 static void dpu_encoder_phys_wb_set_ot_limit(
60 		struct dpu_encoder_phys *phys_enc)
61 {
62 	struct dpu_hw_wb *hw_wb = phys_enc->hw_wb;
63 	struct dpu_vbif_set_ot_params ot_params;
64 	bool forced_on = false;
65 
66 	memset(&ot_params, 0, sizeof(ot_params));
67 	ot_params.xin_id = hw_wb->caps->xin_id;
68 	ot_params.num = hw_wb->idx - WB_0;
69 	ot_params.width = phys_enc->cached_mode.hdisplay;
70 	ot_params.height = phys_enc->cached_mode.vdisplay;
71 	ot_params.is_wfd = !dpu_encoder_helper_get_cwb_mask(phys_enc);
72 	ot_params.frame_rate = drm_mode_vrefresh(&phys_enc->cached_mode);
73 	ot_params.vbif_idx = hw_wb->caps->vbif_idx;
74 	ot_params.rd = false;
75 
76 	if (!_dpu_encoder_phys_wb_clk_force_ctrl(hw_wb, phys_enc->dpu_kms->hw_mdp,
77 						 true, &forced_on))
78 		return;
79 
80 	dpu_vbif_set_ot_limit(phys_enc->dpu_kms, &ot_params);
81 
82 	if (forced_on)
83 		_dpu_encoder_phys_wb_clk_force_ctrl(hw_wb, phys_enc->dpu_kms->hw_mdp,
84 						    false, &forced_on);
85 }
86 
87 /**
88  * dpu_encoder_phys_wb_set_qos_remap - set QoS remapper for writeback
89  * @phys_enc:	Pointer to physical encoder
90  */
dpu_encoder_phys_wb_set_qos_remap(struct dpu_encoder_phys * phys_enc)91 static void dpu_encoder_phys_wb_set_qos_remap(
92 		struct dpu_encoder_phys *phys_enc)
93 {
94 	struct dpu_hw_wb *hw_wb;
95 	struct dpu_vbif_set_qos_params qos_params;
96 	bool forced_on = false;
97 
98 	if (!phys_enc || !phys_enc->parent || !phys_enc->parent->crtc) {
99 		DPU_ERROR("invalid arguments\n");
100 		return;
101 	}
102 
103 	if (!phys_enc->hw_wb || !phys_enc->hw_wb->caps) {
104 		DPU_ERROR("invalid writeback hardware\n");
105 		return;
106 	}
107 
108 	hw_wb = phys_enc->hw_wb;
109 
110 	memset(&qos_params, 0, sizeof(qos_params));
111 	qos_params.vbif_idx = hw_wb->caps->vbif_idx;
112 	qos_params.xin_id = hw_wb->caps->xin_id;
113 	qos_params.num = hw_wb->idx - WB_0;
114 	qos_params.is_rt = dpu_encoder_helper_get_cwb_mask(phys_enc);
115 
116 	DPU_DEBUG("[qos_remap] wb:%d vbif:%d xin:%d is_rt:%d\n",
117 			qos_params.num,
118 			qos_params.vbif_idx,
119 			qos_params.xin_id, qos_params.is_rt);
120 
121 	if (!_dpu_encoder_phys_wb_clk_force_ctrl(hw_wb, phys_enc->dpu_kms->hw_mdp,
122 						 true, &forced_on))
123 		return;
124 
125 	dpu_vbif_set_qos_remap(phys_enc->dpu_kms, &qos_params);
126 
127 	if (forced_on)
128 		_dpu_encoder_phys_wb_clk_force_ctrl(hw_wb, phys_enc->dpu_kms->hw_mdp,
129 						    false, &forced_on);
130 }
131 
132 /**
133  * dpu_encoder_phys_wb_set_qos - set QoS/danger/safe LUTs for writeback
134  * @phys_enc:	Pointer to physical encoder
135  */
dpu_encoder_phys_wb_set_qos(struct dpu_encoder_phys * phys_enc)136 static void dpu_encoder_phys_wb_set_qos(struct dpu_encoder_phys *phys_enc)
137 {
138 	struct dpu_hw_wb *hw_wb;
139 	struct dpu_hw_qos_cfg qos_cfg;
140 	const struct dpu_mdss_cfg *catalog;
141 	const struct dpu_qos_lut_tbl *qos_lut_tb;
142 
143 	if (!phys_enc || !phys_enc->dpu_kms || !phys_enc->dpu_kms->catalog) {
144 		DPU_ERROR("invalid parameter(s)\n");
145 		return;
146 	}
147 
148 	catalog = phys_enc->dpu_kms->catalog;
149 
150 	hw_wb = phys_enc->hw_wb;
151 
152 	memset(&qos_cfg, 0, sizeof(struct dpu_hw_qos_cfg));
153 	qos_cfg.danger_safe_en = true;
154 	qos_cfg.danger_lut =
155 		catalog->perf->danger_lut_tbl[DPU_QOS_LUT_USAGE_NRT];
156 
157 	qos_cfg.safe_lut = catalog->perf->safe_lut_tbl[DPU_QOS_LUT_USAGE_NRT];
158 
159 	qos_lut_tb = &catalog->perf->qos_lut_tbl[DPU_QOS_LUT_USAGE_NRT];
160 	qos_cfg.creq_lut = _dpu_hw_get_qos_lut(qos_lut_tb, 0);
161 
162 	if (hw_wb->ops.setup_qos_lut)
163 		hw_wb->ops.setup_qos_lut(hw_wb, &qos_cfg);
164 }
165 
166 /**
167  * dpu_encoder_phys_wb_setup_fb - setup output framebuffer
168  * @phys_enc:	Pointer to physical encoder
169  * @format: Format of the framebuffer
170  */
dpu_encoder_phys_wb_setup_fb(struct dpu_encoder_phys * phys_enc,const struct msm_format * format)171 static void dpu_encoder_phys_wb_setup_fb(struct dpu_encoder_phys *phys_enc,
172 					 const struct msm_format *format)
173 {
174 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys_enc);
175 	struct dpu_hw_wb *hw_wb;
176 	struct dpu_hw_wb_cfg *wb_cfg;
177 	u32 cdp_usage;
178 
179 	if (!phys_enc || !phys_enc->dpu_kms || !phys_enc->dpu_kms->catalog) {
180 		DPU_ERROR("invalid encoder\n");
181 		return;
182 	}
183 
184 	hw_wb = phys_enc->hw_wb;
185 	wb_cfg = &wb_enc->wb_cfg;
186 	if (dpu_encoder_helper_get_cwb_mask(phys_enc))
187 		cdp_usage = DPU_PERF_CDP_USAGE_RT;
188 	else
189 		cdp_usage = DPU_PERF_CDP_USAGE_NRT;
190 
191 	wb_cfg->intf_mode = phys_enc->intf_mode;
192 	wb_cfg->roi.x1 = 0;
193 	wb_cfg->roi.x2 = phys_enc->cached_mode.hdisplay;
194 	wb_cfg->roi.y1 = 0;
195 	wb_cfg->roi.y2 = phys_enc->cached_mode.vdisplay;
196 
197 	if (hw_wb->ops.setup_roi)
198 		hw_wb->ops.setup_roi(hw_wb, wb_cfg);
199 
200 	if (hw_wb->ops.setup_outformat)
201 		hw_wb->ops.setup_outformat(hw_wb, wb_cfg, format);
202 
203 	if (hw_wb->ops.setup_cdp) {
204 		const struct dpu_perf_cfg *perf = phys_enc->dpu_kms->catalog->perf;
205 
206 		hw_wb->ops.setup_cdp(hw_wb, format,
207 				     perf->cdp_cfg[cdp_usage].wr_enable);
208 	}
209 
210 	if (hw_wb->ops.setup_outaddress)
211 		hw_wb->ops.setup_outaddress(hw_wb, wb_cfg);
212 }
213 
214 /**
215  * dpu_encoder_phys_wb_setup_ctl - setup wb pipeline for ctl path
216  * @phys_enc:Pointer to physical encoder
217  */
dpu_encoder_phys_wb_setup_ctl(struct dpu_encoder_phys * phys_enc)218 static void dpu_encoder_phys_wb_setup_ctl(struct dpu_encoder_phys *phys_enc)
219 {
220 	struct dpu_hw_wb *hw_wb;
221 	struct dpu_hw_cdm *hw_cdm;
222 
223 	if (!phys_enc) {
224 		DPU_ERROR("invalid encoder\n");
225 		return;
226 	}
227 
228 	hw_wb = phys_enc->hw_wb;
229 	hw_cdm = phys_enc->hw_cdm;
230 
231 	if (phys_enc->dpu_kms->catalog->mdss_ver->core_major_ver >= 5 &&
232 		(phys_enc->hw_ctl &&
233 		 phys_enc->hw_ctl->ops.setup_intf_cfg)) {
234 		struct dpu_hw_intf_cfg intf_cfg = {0};
235 		struct dpu_hw_pingpong *hw_pp = phys_enc->hw_pp;
236 		enum dpu_3d_blend_mode mode_3d;
237 
238 		mode_3d = dpu_encoder_helper_get_3d_blend_mode(phys_enc);
239 
240 		intf_cfg.intf = DPU_NONE;
241 		intf_cfg.wb = hw_wb->idx;
242 		intf_cfg.cwb = dpu_encoder_helper_get_cwb_mask(phys_enc);
243 
244 		if (mode_3d && hw_pp && hw_pp->merge_3d)
245 			intf_cfg.merge_3d = hw_pp->merge_3d->idx;
246 
247 		if (hw_cdm)
248 			intf_cfg.cdm = hw_cdm->idx;
249 
250 		if (phys_enc->hw_pp->merge_3d && phys_enc->hw_pp->merge_3d->ops.setup_3d_mode)
251 			phys_enc->hw_pp->merge_3d->ops.setup_3d_mode(phys_enc->hw_pp->merge_3d,
252 					mode_3d);
253 
254 		/* setup which pp blk will connect to this wb */
255 		if (hw_pp && phys_enc->hw_wb->ops.bind_pingpong_blk)
256 			phys_enc->hw_wb->ops.bind_pingpong_blk(phys_enc->hw_wb,
257 					phys_enc->hw_pp->idx);
258 
259 		phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl, &intf_cfg);
260 	} else if (phys_enc->hw_ctl && phys_enc->hw_ctl->ops.setup_intf_cfg) {
261 		struct dpu_hw_intf_cfg intf_cfg = {0};
262 
263 		intf_cfg.intf = DPU_NONE;
264 		intf_cfg.wb = hw_wb->idx;
265 		intf_cfg.mode_3d =
266 			dpu_encoder_helper_get_3d_blend_mode(phys_enc);
267 		phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl, &intf_cfg);
268 	}
269 }
270 
271 /**
272  * _dpu_encoder_phys_wb_update_flush - flush hardware update
273  * @phys_enc:	Pointer to physical encoder
274  */
_dpu_encoder_phys_wb_update_flush(struct dpu_encoder_phys * phys_enc)275 static void _dpu_encoder_phys_wb_update_flush(struct dpu_encoder_phys *phys_enc)
276 {
277 	struct dpu_hw_wb *hw_wb;
278 	struct dpu_hw_ctl *hw_ctl;
279 	struct dpu_hw_pingpong *hw_pp;
280 	struct dpu_hw_cdm *hw_cdm;
281 	u32 pending_flush = 0;
282 	u32 mode_3d;
283 
284 	if (!phys_enc)
285 		return;
286 
287 	hw_wb = phys_enc->hw_wb;
288 	hw_pp = phys_enc->hw_pp;
289 	hw_ctl = phys_enc->hw_ctl;
290 	hw_cdm = phys_enc->hw_cdm;
291 	mode_3d = dpu_encoder_helper_get_3d_blend_mode(phys_enc);
292 
293 	DPU_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
294 
295 	if (!hw_ctl) {
296 		DPU_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
297 		return;
298 	}
299 
300 	if (hw_ctl->ops.update_pending_flush_wb)
301 		hw_ctl->ops.update_pending_flush_wb(hw_ctl, hw_wb->idx);
302 
303 	if (mode_3d && hw_ctl->ops.update_pending_flush_merge_3d &&
304 	    hw_pp && hw_pp->merge_3d)
305 		hw_ctl->ops.update_pending_flush_merge_3d(hw_ctl,
306 				hw_pp->merge_3d->idx);
307 
308 	if (hw_cdm && hw_ctl->ops.update_pending_flush_cdm)
309 		hw_ctl->ops.update_pending_flush_cdm(hw_ctl, hw_cdm->idx);
310 
311 	if (hw_ctl->ops.get_pending_flush)
312 		pending_flush = hw_ctl->ops.get_pending_flush(hw_ctl);
313 
314 	DPU_DEBUG("Pending flush mask for CTL_%d is 0x%x, WB %d\n",
315 			hw_ctl->idx - CTL_0, pending_flush,
316 			hw_wb->idx - WB_0);
317 }
318 
319 /**
320  * dpu_encoder_phys_wb_setup - setup writeback encoder
321  * @phys_enc:	Pointer to physical encoder
322  */
dpu_encoder_phys_wb_setup(struct dpu_encoder_phys * phys_enc)323 static void dpu_encoder_phys_wb_setup(
324 		struct dpu_encoder_phys *phys_enc)
325 {
326 	struct dpu_hw_wb *hw_wb = phys_enc->hw_wb;
327 	struct drm_display_mode mode = phys_enc->cached_mode;
328 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys_enc);
329 	const struct msm_format *format;
330 
331 	format = msm_framebuffer_format(wb_enc->wb_job->fb);
332 
333 	DPU_DEBUG("[mode_set:%d, \"%s\",%d,%d]\n",
334 			hw_wb->idx - WB_0, mode.name,
335 			mode.hdisplay, mode.vdisplay);
336 
337 	dpu_encoder_phys_wb_set_ot_limit(phys_enc);
338 
339 	dpu_encoder_phys_wb_set_qos_remap(phys_enc);
340 
341 	dpu_encoder_phys_wb_set_qos(phys_enc);
342 
343 	dpu_encoder_phys_wb_setup_fb(phys_enc, format);
344 
345 	dpu_encoder_helper_phys_setup_cdm(phys_enc, format, CDM_CDWN_OUTPUT_WB);
346 
347 	dpu_encoder_helper_phys_setup_cwb(phys_enc, true);
348 
349 	dpu_encoder_phys_wb_setup_ctl(phys_enc);
350 }
351 
352 /**
353  * dpu_encoder_phys_wb_done_irq - writeback interrupt handler
354  * @arg:	Pointer to writeback encoder
355  */
dpu_encoder_phys_wb_done_irq(void * arg)356 static void dpu_encoder_phys_wb_done_irq(void *arg)
357 {
358 	struct dpu_encoder_phys *phys_enc = arg;
359 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys_enc);
360 
361 	struct dpu_hw_wb *hw_wb = phys_enc->hw_wb;
362 	unsigned long lock_flags;
363 	u32 event = DPU_ENCODER_FRAME_EVENT_DONE;
364 
365 	DPU_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
366 
367 	dpu_encoder_frame_done_callback(phys_enc->parent, phys_enc, event);
368 
369 	dpu_encoder_vblank_callback(phys_enc->parent, phys_enc);
370 
371 	spin_lock_irqsave(phys_enc->enc_spinlock, lock_flags);
372 	atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
373 	spin_unlock_irqrestore(phys_enc->enc_spinlock, lock_flags);
374 
375 	if (wb_enc->wb_conn)
376 		drm_writeback_signal_completion(wb_enc->wb_conn, 0);
377 
378 	/* Signal any waiting atomic commit thread */
379 	wake_up_all(&phys_enc->pending_kickoff_wq);
380 }
381 
382 /**
383  * dpu_encoder_phys_wb_irq_enable - irq control of WB
384  * @phys:	Pointer to physical encoder
385  */
dpu_encoder_phys_wb_irq_enable(struct dpu_encoder_phys * phys)386 static void dpu_encoder_phys_wb_irq_enable(struct dpu_encoder_phys *phys)
387 {
388 
389 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys);
390 
391 	if (atomic_inc_return(&wb_enc->wbirq_refcount) == 1)
392 		dpu_core_irq_register_callback(phys->dpu_kms,
393 					       phys->irq[INTR_IDX_WB_DONE],
394 					       dpu_encoder_phys_wb_done_irq,
395 					       phys);
396 }
397 
398 /**
399  * dpu_encoder_phys_wb_irq_disable - irq control of WB
400  * @phys:	Pointer to physical encoder
401  */
dpu_encoder_phys_wb_irq_disable(struct dpu_encoder_phys * phys)402 static void dpu_encoder_phys_wb_irq_disable(struct dpu_encoder_phys *phys)
403 {
404 
405 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys);
406 
407 	if (atomic_dec_return(&wb_enc->wbirq_refcount) == 0)
408 		dpu_core_irq_unregister_callback(phys->dpu_kms, phys->irq[INTR_IDX_WB_DONE]);
409 }
410 
dpu_encoder_phys_wb_atomic_mode_set(struct dpu_encoder_phys * phys_enc,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)411 static void dpu_encoder_phys_wb_atomic_mode_set(
412 		struct dpu_encoder_phys *phys_enc,
413 		struct drm_crtc_state *crtc_state,
414 		struct drm_connector_state *conn_state)
415 {
416 
417 	phys_enc->irq[INTR_IDX_WB_DONE] = phys_enc->hw_wb->caps->intr_wb_done;
418 }
419 
_dpu_encoder_phys_wb_handle_wbdone_timeout(struct dpu_encoder_phys * phys_enc)420 static void _dpu_encoder_phys_wb_handle_wbdone_timeout(
421 		struct dpu_encoder_phys *phys_enc)
422 {
423 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys_enc);
424 	u32 frame_event = DPU_ENCODER_FRAME_EVENT_ERROR;
425 
426 	wb_enc->wb_done_timeout_cnt++;
427 
428 	if (wb_enc->wb_done_timeout_cnt == 1)
429 		msm_disp_snapshot_state(phys_enc->parent->dev);
430 
431 	atomic_add_unless(&phys_enc->pending_kickoff_cnt, -1, 0);
432 
433 	/* request a ctl reset before the next kickoff */
434 	phys_enc->enable_state = DPU_ENC_ERR_NEEDS_HW_RESET;
435 
436 	if (wb_enc->wb_conn)
437 		drm_writeback_signal_completion(wb_enc->wb_conn, 0);
438 
439 	dpu_encoder_frame_done_callback(phys_enc->parent, phys_enc, frame_event);
440 }
441 
442 /**
443  * dpu_encoder_phys_wb_wait_for_commit_done - wait until request is committed
444  * @phys_enc:	Pointer to physical encoder
445  */
dpu_encoder_phys_wb_wait_for_commit_done(struct dpu_encoder_phys * phys_enc)446 static int dpu_encoder_phys_wb_wait_for_commit_done(
447 		struct dpu_encoder_phys *phys_enc)
448 {
449 	unsigned long ret;
450 	struct dpu_encoder_wait_info wait_info;
451 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys_enc);
452 
453 	wait_info.wq = &phys_enc->pending_kickoff_wq;
454 	wait_info.atomic_cnt = &phys_enc->pending_kickoff_cnt;
455 	wait_info.timeout_ms = KICKOFF_TIMEOUT_MS;
456 
457 	ret = dpu_encoder_helper_wait_for_irq(phys_enc,
458 			phys_enc->irq[INTR_IDX_WB_DONE],
459 			dpu_encoder_phys_wb_done_irq, &wait_info);
460 	if (ret == -ETIMEDOUT)
461 		_dpu_encoder_phys_wb_handle_wbdone_timeout(phys_enc);
462 	else if (!ret)
463 		wb_enc->wb_done_timeout_cnt = 0;
464 
465 	return ret;
466 }
467 
468 /**
469  * dpu_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
470  * @phys_enc:	Pointer to physical encoder
471  * Returns:	Zero on success
472  */
dpu_encoder_phys_wb_prepare_for_kickoff(struct dpu_encoder_phys * phys_enc)473 static void dpu_encoder_phys_wb_prepare_for_kickoff(
474 		struct dpu_encoder_phys *phys_enc)
475 {
476 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys_enc);
477 	struct drm_connector *drm_conn;
478 	struct drm_connector_state *state;
479 
480 	DPU_DEBUG("[wb:%d]\n", phys_enc->hw_wb->idx - WB_0);
481 
482 	if (!wb_enc->wb_conn || !wb_enc->wb_job) {
483 		DPU_ERROR("invalid wb_conn or wb_job\n");
484 		return;
485 	}
486 
487 	drm_conn = &wb_enc->wb_conn->base;
488 	state = drm_conn->state;
489 
490 	if (wb_enc->wb_conn && wb_enc->wb_job)
491 		drm_writeback_queue_job(wb_enc->wb_conn, state);
492 
493 	dpu_encoder_phys_wb_setup(phys_enc);
494 
495 	_dpu_encoder_phys_wb_update_flush(phys_enc);
496 }
497 
498 /**
499  * dpu_encoder_phys_wb_needs_single_flush - trigger flush processing
500  * @phys_enc:	Pointer to physical encoder
501  */
dpu_encoder_phys_wb_needs_single_flush(struct dpu_encoder_phys * phys_enc)502 static bool dpu_encoder_phys_wb_needs_single_flush(struct dpu_encoder_phys *phys_enc)
503 {
504 	DPU_DEBUG("[wb:%d]\n", phys_enc->hw_wb->idx - WB_0);
505 	return false;
506 }
507 
508 /**
509  * dpu_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
510  * @phys_enc:	Pointer to physical encoder
511  */
dpu_encoder_phys_wb_handle_post_kickoff(struct dpu_encoder_phys * phys_enc)512 static void dpu_encoder_phys_wb_handle_post_kickoff(
513 		struct dpu_encoder_phys *phys_enc)
514 {
515 	DPU_DEBUG("[wb:%d]\n", phys_enc->hw_wb->idx - WB_0);
516 
517 }
518 
519 /**
520  * dpu_encoder_phys_wb_enable - enable writeback encoder
521  * @phys_enc:	Pointer to physical encoder
522  */
dpu_encoder_phys_wb_enable(struct dpu_encoder_phys * phys_enc)523 static void dpu_encoder_phys_wb_enable(struct dpu_encoder_phys *phys_enc)
524 {
525 	DPU_DEBUG("[wb:%d]\n", phys_enc->hw_wb->idx - WB_0);
526 	phys_enc->enable_state = DPU_ENC_ENABLED;
527 }
528 /**
529  * dpu_encoder_phys_wb_disable - disable writeback encoder
530  * @phys_enc:	Pointer to physical encoder
531  */
dpu_encoder_phys_wb_disable(struct dpu_encoder_phys * phys_enc)532 static void dpu_encoder_phys_wb_disable(struct dpu_encoder_phys *phys_enc)
533 {
534 	struct dpu_hw_wb *hw_wb = phys_enc->hw_wb;
535 
536 	DPU_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
537 
538 	if (phys_enc->enable_state == DPU_ENC_DISABLED) {
539 		DPU_ERROR("encoder is already disabled\n");
540 		return;
541 	}
542 
543 	/* reset h/w before final flush */
544 	phys_enc->hw_ctl->ops.clear_pending_flush(phys_enc->hw_ctl);
545 
546 	/*
547 	 * New CTL reset sequence from 5.0 MDP onwards.
548 	 * If has_3d_merge_reset is not set, legacy reset
549 	 * sequence is executed.
550 	 *
551 	 * Legacy reset sequence has not been implemented yet.
552 	 * Any target earlier than SM8150 will need it and when
553 	 * WB support is added to those targets will need to add
554 	 * the legacy teardown sequence as well.
555 	 */
556 	if (phys_enc->dpu_kms->catalog->mdss_ver->core_major_ver >= 5)
557 		dpu_encoder_helper_phys_cleanup(phys_enc);
558 
559 	phys_enc->enable_state = DPU_ENC_DISABLED;
560 }
561 
dpu_encoder_phys_wb_prepare_wb_job(struct dpu_encoder_phys * phys_enc,struct drm_writeback_job * job)562 static void dpu_encoder_phys_wb_prepare_wb_job(struct dpu_encoder_phys *phys_enc,
563 		struct drm_writeback_job *job)
564 {
565 	const struct msm_format *format;
566 	struct dpu_hw_wb_cfg *wb_cfg;
567 	int ret;
568 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys_enc);
569 
570 	if (!job->fb)
571 		return;
572 
573 	wb_enc->wb_job = job;
574 	wb_enc->wb_conn = job->connector;
575 
576 	wb_cfg = &wb_enc->wb_cfg;
577 
578 	memset(wb_cfg, 0, sizeof(struct dpu_hw_wb_cfg));
579 
580 	ret = msm_framebuffer_prepare(job->fb, false);
581 	if (ret) {
582 		DPU_ERROR("prep fb failed, %d\n", ret);
583 		return;
584 	}
585 
586 	format = msm_framebuffer_format(job->fb);
587 
588 	ret = dpu_format_populate_plane_sizes(job->fb, &wb_cfg->dest);
589 	if (ret) {
590 		DPU_DEBUG("failed to populate plane sizes%d\n", ret);
591 		return;
592 	}
593 
594 	dpu_format_populate_addrs(job->fb, &wb_cfg->dest);
595 
596 	wb_cfg->dest.width = job->fb->width;
597 	wb_cfg->dest.height = job->fb->height;
598 	wb_cfg->dest.num_planes = format->num_planes;
599 
600 	if ((format->fetch_type == MDP_PLANE_PLANAR) &&
601 	    (format->element[0] == C1_B_Cb))
602 		swap(wb_cfg->dest.plane_addr[1], wb_cfg->dest.plane_addr[2]);
603 
604 	DPU_DEBUG("[fb_offset:%8.8x,%8.8x,%8.8x,%8.8x]\n",
605 			wb_cfg->dest.plane_addr[0], wb_cfg->dest.plane_addr[1],
606 			wb_cfg->dest.plane_addr[2], wb_cfg->dest.plane_addr[3]);
607 
608 	DPU_DEBUG("[fb_stride:%8.8x,%8.8x,%8.8x,%8.8x]\n",
609 			wb_cfg->dest.plane_pitch[0], wb_cfg->dest.plane_pitch[1],
610 			wb_cfg->dest.plane_pitch[2], wb_cfg->dest.plane_pitch[3]);
611 }
612 
dpu_encoder_phys_wb_cleanup_wb_job(struct dpu_encoder_phys * phys_enc,struct drm_writeback_job * job)613 static void dpu_encoder_phys_wb_cleanup_wb_job(struct dpu_encoder_phys *phys_enc,
614 		struct drm_writeback_job *job)
615 {
616 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys_enc);
617 
618 	if (!job->fb)
619 		return;
620 
621 	msm_framebuffer_cleanup(job->fb, false);
622 	wb_enc->wb_job = NULL;
623 	wb_enc->wb_conn = NULL;
624 }
625 
dpu_encoder_phys_wb_is_valid_for_commit(struct dpu_encoder_phys * phys_enc)626 static bool dpu_encoder_phys_wb_is_valid_for_commit(struct dpu_encoder_phys *phys_enc)
627 {
628 	struct dpu_encoder_phys_wb *wb_enc = to_dpu_encoder_phys_wb(phys_enc);
629 
630 	if (wb_enc->wb_job)
631 		return true;
632 	else
633 		return false;
634 }
635 
636 /**
637  * dpu_encoder_phys_wb_init_ops - initialize writeback operations
638  * @ops:	Pointer to encoder operation table
639  */
dpu_encoder_phys_wb_init_ops(struct dpu_encoder_phys_ops * ops)640 static void dpu_encoder_phys_wb_init_ops(struct dpu_encoder_phys_ops *ops)
641 {
642 	ops->is_master = dpu_encoder_phys_wb_is_master;
643 	ops->atomic_mode_set = dpu_encoder_phys_wb_atomic_mode_set;
644 	ops->enable = dpu_encoder_phys_wb_enable;
645 	ops->disable = dpu_encoder_phys_wb_disable;
646 	ops->wait_for_commit_done = dpu_encoder_phys_wb_wait_for_commit_done;
647 	ops->prepare_for_kickoff = dpu_encoder_phys_wb_prepare_for_kickoff;
648 	ops->handle_post_kickoff = dpu_encoder_phys_wb_handle_post_kickoff;
649 	ops->needs_single_flush = dpu_encoder_phys_wb_needs_single_flush;
650 	ops->trigger_start = dpu_encoder_helper_trigger_start;
651 	ops->prepare_wb_job = dpu_encoder_phys_wb_prepare_wb_job;
652 	ops->cleanup_wb_job = dpu_encoder_phys_wb_cleanup_wb_job;
653 	ops->irq_enable = dpu_encoder_phys_wb_irq_enable;
654 	ops->irq_disable = dpu_encoder_phys_wb_irq_disable;
655 	ops->is_valid_for_commit = dpu_encoder_phys_wb_is_valid_for_commit;
656 
657 }
658 
659 /**
660  * dpu_encoder_phys_wb_init - initialize writeback encoder
661  * @dev:  Corresponding device for devres management
662  * @p:	Pointer to init info structure with initialization params
663  */
dpu_encoder_phys_wb_init(struct drm_device * dev,struct dpu_enc_phys_init_params * p)664 struct dpu_encoder_phys *dpu_encoder_phys_wb_init(struct drm_device *dev,
665 		struct dpu_enc_phys_init_params *p)
666 {
667 	struct dpu_encoder_phys *phys_enc = NULL;
668 	struct dpu_encoder_phys_wb *wb_enc = NULL;
669 
670 	DPU_DEBUG("\n");
671 
672 	if (!p || !p->parent) {
673 		DPU_ERROR("invalid params\n");
674 		return ERR_PTR(-EINVAL);
675 	}
676 
677 	wb_enc = drmm_kzalloc(dev, sizeof(*wb_enc), GFP_KERNEL);
678 	if (!wb_enc) {
679 		DPU_ERROR("failed to allocate wb phys_enc enc\n");
680 		return ERR_PTR(-ENOMEM);
681 	}
682 
683 	phys_enc = &wb_enc->base;
684 
685 	dpu_encoder_phys_init(phys_enc, p);
686 
687 	dpu_encoder_phys_wb_init_ops(&phys_enc->ops);
688 	phys_enc->intf_mode = INTF_MODE_WB_LINE;
689 
690 	atomic_set(&wb_enc->wbirq_refcount, 0);
691 
692 	wb_enc->wb_done_timeout_cnt = 0;
693 
694 	DPU_DEBUG("Created dpu_encoder_phys for wb %d\n", phys_enc->hw_wb->idx);
695 
696 	return phys_enc;
697 }
698