1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2013 Red Hat
4 * Copyright (c) 2014-2018, 2020-2021 The Linux Foundation. All rights reserved.
5 * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
6 *
7 * Author: Rob Clark <robdclark@gmail.com>
8 */
9
10 #define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
11 #include <linux/debugfs.h>
12 #include <linux/kthread.h>
13 #include <linux/seq_file.h>
14
15 #include <drm/drm_atomic.h>
16 #include <drm/drm_crtc.h>
17 #include <drm/drm_file.h>
18 #include <drm/drm_probe_helper.h>
19 #include <drm/drm_framebuffer.h>
20
21 #include "msm_drv.h"
22 #include "dpu_kms.h"
23 #include "dpu_hwio.h"
24 #include "dpu_hw_catalog.h"
25 #include "dpu_hw_intf.h"
26 #include "dpu_hw_ctl.h"
27 #include "dpu_hw_cwb.h"
28 #include "dpu_hw_dspp.h"
29 #include "dpu_hw_dsc.h"
30 #include "dpu_hw_merge3d.h"
31 #include "dpu_hw_cdm.h"
32 #include "dpu_formats.h"
33 #include "dpu_encoder_phys.h"
34 #include "dpu_crtc.h"
35 #include "dpu_trace.h"
36 #include "dpu_core_irq.h"
37 #include "disp/msm_disp_snapshot.h"
38
39 #define DPU_DEBUG_ENC(e, fmt, ...) DRM_DEBUG_ATOMIC("enc%d " fmt,\
40 (e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
41
42 #define DPU_ERROR_ENC(e, fmt, ...) DPU_ERROR("enc%d " fmt,\
43 (e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
44
45 #define DPU_ERROR_ENC_RATELIMITED(e, fmt, ...) DPU_ERROR_RATELIMITED("enc%d " fmt,\
46 (e) ? (e)->base.base.id : -1, ##__VA_ARGS__)
47
48 /*
49 * Two to anticipate panels that can do cmd/vid dynamic switching
50 * plan is to create all possible physical encoder types, and switch between
51 * them at runtime
52 */
53 #define NUM_PHYS_ENCODER_TYPES 2
54
55 #define MAX_PHYS_ENCODERS_PER_VIRTUAL \
56 (MAX_H_TILES_PER_DISPLAY * NUM_PHYS_ENCODER_TYPES)
57
58 #define MAX_CHANNELS_PER_ENC 2
59
60 #define IDLE_SHORT_TIMEOUT 1
61
62 /* timeout in frames waiting for frame done */
63 #define DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES 5
64
65 /**
66 * enum dpu_enc_rc_events - events for resource control state machine
67 * @DPU_ENC_RC_EVENT_KICKOFF:
68 * This event happens at NORMAL priority.
69 * Event that signals the start of the transfer. When this event is
70 * received, enable MDP/DSI core clocks. Regardless of the previous
71 * state, the resource should be in ON state at the end of this event.
72 * @DPU_ENC_RC_EVENT_FRAME_DONE:
73 * This event happens at INTERRUPT level.
74 * Event signals the end of the data transfer after the PP FRAME_DONE
75 * event. At the end of this event, a delayed work is scheduled to go to
76 * IDLE_PC state after IDLE_TIMEOUT time.
77 * @DPU_ENC_RC_EVENT_PRE_STOP:
78 * This event happens at NORMAL priority.
79 * This event, when received during the ON state, leave the RC STATE
80 * in the PRE_OFF state. It should be followed by the STOP event as
81 * part of encoder disable.
82 * If received during IDLE or OFF states, it will do nothing.
83 * @DPU_ENC_RC_EVENT_STOP:
84 * This event happens at NORMAL priority.
85 * When this event is received, disable all the MDP/DSI core clocks, and
86 * disable IRQs. It should be called from the PRE_OFF or IDLE states.
87 * IDLE is expected when IDLE_PC has run, and PRE_OFF did nothing.
88 * PRE_OFF is expected when PRE_STOP was executed during the ON state.
89 * Resource state should be in OFF at the end of the event.
90 * @DPU_ENC_RC_EVENT_ENTER_IDLE:
91 * This event happens at NORMAL priority from a work item.
92 * Event signals that there were no frame updates for IDLE_TIMEOUT time.
93 * This would disable MDP/DSI core clocks and change the resource state
94 * to IDLE.
95 */
96 enum dpu_enc_rc_events {
97 DPU_ENC_RC_EVENT_KICKOFF = 1,
98 DPU_ENC_RC_EVENT_FRAME_DONE,
99 DPU_ENC_RC_EVENT_PRE_STOP,
100 DPU_ENC_RC_EVENT_STOP,
101 DPU_ENC_RC_EVENT_ENTER_IDLE
102 };
103
104 /*
105 * enum dpu_enc_rc_states - states that the resource control maintains
106 * @DPU_ENC_RC_STATE_OFF: Resource is in OFF state
107 * @DPU_ENC_RC_STATE_PRE_OFF: Resource is transitioning to OFF state
108 * @DPU_ENC_RC_STATE_ON: Resource is in ON state
109 * @DPU_ENC_RC_STATE_MODESET: Resource is in modeset state
110 * @DPU_ENC_RC_STATE_IDLE: Resource is in IDLE state
111 */
112 enum dpu_enc_rc_states {
113 DPU_ENC_RC_STATE_OFF,
114 DPU_ENC_RC_STATE_PRE_OFF,
115 DPU_ENC_RC_STATE_ON,
116 DPU_ENC_RC_STATE_IDLE
117 };
118
119 /**
120 * struct dpu_encoder_virt - virtual encoder. Container of one or more physical
121 * encoders. Virtual encoder manages one "logical" display. Physical
122 * encoders manage one intf block, tied to a specific panel/sub-panel.
123 * Virtual encoder defers as much as possible to the physical encoders.
124 * Virtual encoder registers itself with the DRM Framework as the encoder.
125 * @base: drm_encoder base class for registration with DRM
126 * @enc_spinlock: Virtual-Encoder-Wide Spin Lock for IRQ purposes
127 * @enabled: True if the encoder is active, protected by enc_lock
128 * @commit_done_timedout: True if there has been a timeout on commit after
129 * enabling the encoder.
130 * @num_phys_encs: Actual number of physical encoders contained.
131 * @phys_encs: Container of physical encoders managed.
132 * @cur_master: Pointer to the current master in this mode. Optimization
133 * Only valid after enable. Cleared as disable.
134 * @cur_slave: As above but for the slave encoder.
135 * @hw_pp: Handle to the pingpong blocks used for the display. No.
136 * pingpong blocks can be different than num_phys_encs.
137 * @hw_cwb: Handle to the CWB muxes used for concurrent writeback
138 * display. Number of CWB muxes can be different than
139 * num_phys_encs.
140 * @hw_dsc: Handle to the DSC blocks used for the display.
141 * @dsc_mask: Bitmask of used DSC blocks.
142 * @cwb_mask: Bitmask of used CWB muxes
143 * @intfs_swapped: Whether or not the phys_enc interfaces have been swapped
144 * for partial update right-only cases, such as pingpong
145 * split where virtual pingpong does not generate IRQs
146 * @crtc: Pointer to the currently assigned crtc. Normally you
147 * would use crtc->state->encoder_mask to determine the
148 * link between encoder/crtc. However in this case we need
149 * to track crtc in the disable() hook which is called
150 * _after_ encoder_mask is cleared.
151 * @connector: If a mode is set, cached pointer to the active connector
152 * @enc_lock: Lock around physical encoder
153 * create/destroy/enable/disable
154 * @frame_busy_mask: Bitmask tracking which phys_enc we are still
155 * busy processing current command.
156 * Bit0 = phys_encs[0] etc.
157 * @frame_done_timeout_ms: frame done timeout in ms
158 * @frame_done_timeout_cnt: atomic counter tracking the number of frame
159 * done timeouts
160 * @frame_done_timer: watchdog timer for frame done event
161 * @disp_info: local copy of msm_display_info struct
162 * @idle_pc_supported: indicate if idle power collaps is supported
163 * @rc_lock: resource control mutex lock to protect
164 * virt encoder over various state changes
165 * @rc_state: resource controller state
166 * @delayed_off_work: delayed worker to schedule disabling of
167 * clks and resources after IDLE_TIMEOUT time.
168 * @topology: topology of the display
169 * @idle_timeout: idle timeout duration in milliseconds
170 * @wide_bus_en: wide bus is enabled on this interface
171 * @dsc: drm_dsc_config pointer, for DSC-enabled encoders
172 */
173 struct dpu_encoder_virt {
174 struct drm_encoder base;
175 spinlock_t enc_spinlock;
176
177 bool enabled;
178 bool commit_done_timedout;
179
180 unsigned int num_phys_encs;
181 struct dpu_encoder_phys *phys_encs[MAX_PHYS_ENCODERS_PER_VIRTUAL];
182 struct dpu_encoder_phys *cur_master;
183 struct dpu_encoder_phys *cur_slave;
184 struct dpu_hw_pingpong *hw_pp[MAX_CHANNELS_PER_ENC];
185 struct dpu_hw_cwb *hw_cwb[MAX_CHANNELS_PER_ENC];
186 struct dpu_hw_dsc *hw_dsc[MAX_CHANNELS_PER_ENC];
187
188 unsigned int dsc_mask;
189 unsigned int cwb_mask;
190
191 bool intfs_swapped;
192
193 struct drm_crtc *crtc;
194 struct drm_connector *connector;
195
196 struct mutex enc_lock;
197 DECLARE_BITMAP(frame_busy_mask, MAX_PHYS_ENCODERS_PER_VIRTUAL);
198
199 atomic_t frame_done_timeout_ms;
200 atomic_t frame_done_timeout_cnt;
201 struct timer_list frame_done_timer;
202
203 struct msm_display_info disp_info;
204
205 bool idle_pc_supported;
206 struct mutex rc_lock;
207 enum dpu_enc_rc_states rc_state;
208 struct delayed_work delayed_off_work;
209 struct msm_display_topology topology;
210
211 u32 idle_timeout;
212
213 bool wide_bus_en;
214
215 /* DSC configuration */
216 struct drm_dsc_config *dsc;
217 };
218
219 #define to_dpu_encoder_virt(x) container_of(x, struct dpu_encoder_virt, base)
220
221 static u32 dither_matrix[DITHER_MATRIX_SZ] = {
222 15, 7, 13, 5, 3, 11, 1, 9, 12, 4, 14, 6, 0, 8, 2, 10
223 };
224
225 /**
226 * dpu_encoder_get_drm_fmt - return DRM fourcc format
227 * @phys_enc: Pointer to physical encoder structure
228 */
dpu_encoder_get_drm_fmt(struct dpu_encoder_phys * phys_enc)229 u32 dpu_encoder_get_drm_fmt(struct dpu_encoder_phys *phys_enc)
230 {
231 struct drm_encoder *drm_enc;
232 struct dpu_encoder_virt *dpu_enc;
233 struct drm_display_info *info;
234 struct drm_display_mode *mode;
235
236 drm_enc = phys_enc->parent;
237 dpu_enc = to_dpu_encoder_virt(drm_enc);
238 info = &dpu_enc->connector->display_info;
239 mode = &phys_enc->cached_mode;
240
241 if (drm_mode_is_420_only(info, mode))
242 return DRM_FORMAT_YUV420;
243
244 return DRM_FORMAT_RGB888;
245 }
246
247 /**
248 * dpu_encoder_needs_periph_flush - return true if physical encoder requires
249 * peripheral flush
250 * @phys_enc: Pointer to physical encoder structure
251 */
dpu_encoder_needs_periph_flush(struct dpu_encoder_phys * phys_enc)252 bool dpu_encoder_needs_periph_flush(struct dpu_encoder_phys *phys_enc)
253 {
254 struct drm_encoder *drm_enc;
255 struct dpu_encoder_virt *dpu_enc;
256 struct msm_display_info *disp_info;
257 struct msm_drm_private *priv;
258 struct drm_display_mode *mode;
259
260 drm_enc = phys_enc->parent;
261 dpu_enc = to_dpu_encoder_virt(drm_enc);
262 disp_info = &dpu_enc->disp_info;
263 priv = drm_enc->dev->dev_private;
264 mode = &phys_enc->cached_mode;
265
266 return phys_enc->hw_intf->cap->type == INTF_DP &&
267 msm_dp_needs_periph_flush(priv->dp[disp_info->h_tile_instance[0]], mode);
268 }
269
270 /**
271 * dpu_encoder_is_widebus_enabled - return bool value if widebus is enabled
272 * @drm_enc: Pointer to previously created drm encoder structure
273 */
dpu_encoder_is_widebus_enabled(const struct drm_encoder * drm_enc)274 bool dpu_encoder_is_widebus_enabled(const struct drm_encoder *drm_enc)
275 {
276 const struct dpu_encoder_virt *dpu_enc;
277 struct msm_drm_private *priv = drm_enc->dev->dev_private;
278 const struct msm_display_info *disp_info;
279 int index;
280
281 dpu_enc = to_dpu_encoder_virt(drm_enc);
282 disp_info = &dpu_enc->disp_info;
283 index = disp_info->h_tile_instance[0];
284
285 if (disp_info->intf_type == INTF_DP)
286 return msm_dp_wide_bus_available(priv->dp[index]);
287 else if (disp_info->intf_type == INTF_DSI)
288 return msm_dsi_wide_bus_enabled(priv->dsi[index]);
289
290 return false;
291 }
292
293 /**
294 * dpu_encoder_is_dsc_enabled - indicate whether dsc is enabled
295 * for the encoder.
296 * @drm_enc: Pointer to previously created drm encoder structure
297 */
dpu_encoder_is_dsc_enabled(const struct drm_encoder * drm_enc)298 bool dpu_encoder_is_dsc_enabled(const struct drm_encoder *drm_enc)
299 {
300 const struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
301
302 return dpu_enc->dsc ? true : false;
303 }
304
305 /**
306 * dpu_encoder_get_crc_values_cnt - get number of physical encoders contained
307 * in virtual encoder that can collect CRC values
308 * @drm_enc: Pointer to previously created drm encoder structure
309 * Returns: Number of physical encoders for given drm encoder
310 */
dpu_encoder_get_crc_values_cnt(const struct drm_encoder * drm_enc)311 int dpu_encoder_get_crc_values_cnt(const struct drm_encoder *drm_enc)
312 {
313 struct dpu_encoder_virt *dpu_enc;
314 int i, num_intf = 0;
315
316 dpu_enc = to_dpu_encoder_virt(drm_enc);
317
318 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
319 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
320
321 if (phys->hw_intf && phys->hw_intf->ops.setup_misr
322 && phys->hw_intf->ops.collect_misr)
323 num_intf++;
324 }
325
326 return num_intf;
327 }
328
329 /**
330 * dpu_encoder_setup_misr - enable misr calculations
331 * @drm_enc: Pointer to previously created drm encoder structure
332 */
dpu_encoder_setup_misr(const struct drm_encoder * drm_enc)333 void dpu_encoder_setup_misr(const struct drm_encoder *drm_enc)
334 {
335 struct dpu_encoder_virt *dpu_enc;
336
337 int i;
338
339 dpu_enc = to_dpu_encoder_virt(drm_enc);
340
341 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
342 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
343
344 if (!phys->hw_intf || !phys->hw_intf->ops.setup_misr)
345 continue;
346
347 phys->hw_intf->ops.setup_misr(phys->hw_intf);
348 }
349 }
350
351 /**
352 * dpu_encoder_get_crc - get the crc value from interface blocks
353 * @drm_enc: Pointer to previously created drm encoder structure
354 * @crcs: array to fill with CRC data
355 * @pos: offset into the @crcs array
356 * Returns: 0 on success, error otherwise
357 */
dpu_encoder_get_crc(const struct drm_encoder * drm_enc,u32 * crcs,int pos)358 int dpu_encoder_get_crc(const struct drm_encoder *drm_enc, u32 *crcs, int pos)
359 {
360 struct dpu_encoder_virt *dpu_enc;
361
362 int i, rc = 0, entries_added = 0;
363
364 if (!drm_enc->crtc) {
365 DRM_ERROR("no crtc found for encoder %d\n", drm_enc->index);
366 return -EINVAL;
367 }
368
369 dpu_enc = to_dpu_encoder_virt(drm_enc);
370
371 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
372 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
373
374 if (!phys->hw_intf || !phys->hw_intf->ops.collect_misr)
375 continue;
376
377 rc = phys->hw_intf->ops.collect_misr(phys->hw_intf, &crcs[pos + entries_added]);
378 if (rc)
379 return rc;
380 entries_added++;
381 }
382
383 return entries_added;
384 }
385
_dpu_encoder_setup_dither(struct dpu_hw_pingpong * hw_pp,unsigned bpc)386 static void _dpu_encoder_setup_dither(struct dpu_hw_pingpong *hw_pp, unsigned bpc)
387 {
388 struct dpu_hw_dither_cfg dither_cfg = { 0 };
389
390 if (!hw_pp->ops.setup_dither)
391 return;
392
393 switch (bpc) {
394 case 6:
395 dither_cfg.c0_bitdepth = 6;
396 dither_cfg.c1_bitdepth = 6;
397 dither_cfg.c2_bitdepth = 6;
398 dither_cfg.c3_bitdepth = 6;
399 dither_cfg.temporal_en = 0;
400 break;
401 default:
402 hw_pp->ops.setup_dither(hw_pp, NULL);
403 return;
404 }
405
406 memcpy(&dither_cfg.matrix, dither_matrix,
407 sizeof(u32) * DITHER_MATRIX_SZ);
408
409 hw_pp->ops.setup_dither(hw_pp, &dither_cfg);
410 }
411
dpu_encoder_helper_get_intf_type(enum dpu_intf_mode intf_mode)412 static char *dpu_encoder_helper_get_intf_type(enum dpu_intf_mode intf_mode)
413 {
414 switch (intf_mode) {
415 case INTF_MODE_VIDEO:
416 return "INTF_MODE_VIDEO";
417 case INTF_MODE_CMD:
418 return "INTF_MODE_CMD";
419 case INTF_MODE_WB_BLOCK:
420 return "INTF_MODE_WB_BLOCK";
421 case INTF_MODE_WB_LINE:
422 return "INTF_MODE_WB_LINE";
423 default:
424 return "INTF_MODE_UNKNOWN";
425 }
426 }
427
428 /**
429 * dpu_encoder_helper_report_irq_timeout - utility to report error that irq has
430 * timed out, including reporting frame error event to crtc and debug dump
431 * @phys_enc: Pointer to physical encoder structure
432 * @intr_idx: Failing interrupt index
433 */
dpu_encoder_helper_report_irq_timeout(struct dpu_encoder_phys * phys_enc,enum dpu_intr_idx intr_idx)434 void dpu_encoder_helper_report_irq_timeout(struct dpu_encoder_phys *phys_enc,
435 enum dpu_intr_idx intr_idx)
436 {
437 DRM_ERROR("irq timeout id=%u, intf_mode=%s intf=%d wb=%d, pp=%d, intr=%d\n",
438 DRMID(phys_enc->parent),
439 dpu_encoder_helper_get_intf_type(phys_enc->intf_mode),
440 phys_enc->hw_intf ? phys_enc->hw_intf->idx - INTF_0 : -1,
441 phys_enc->hw_wb ? phys_enc->hw_wb->idx - WB_0 : -1,
442 phys_enc->hw_pp->idx - PINGPONG_0, intr_idx);
443
444 dpu_encoder_frame_done_callback(phys_enc->parent, phys_enc,
445 DPU_ENCODER_FRAME_EVENT_ERROR);
446 }
447
448 static int dpu_encoder_helper_wait_event_timeout(int32_t drm_id,
449 u32 irq_idx, struct dpu_encoder_wait_info *info);
450
451 /**
452 * dpu_encoder_helper_wait_for_irq - utility to wait on an irq.
453 * note: will call dpu_encoder_helper_wait_for_irq on timeout
454 * @phys_enc: Pointer to physical encoder structure
455 * @irq_idx: IRQ index
456 * @func: IRQ callback to be called in case of timeout
457 * @wait_info: wait info struct
458 * @return: 0 or -ERROR
459 */
dpu_encoder_helper_wait_for_irq(struct dpu_encoder_phys * phys_enc,unsigned int irq_idx,void (* func)(void * arg),struct dpu_encoder_wait_info * wait_info)460 int dpu_encoder_helper_wait_for_irq(struct dpu_encoder_phys *phys_enc,
461 unsigned int irq_idx,
462 void (*func)(void *arg),
463 struct dpu_encoder_wait_info *wait_info)
464 {
465 u32 irq_status;
466 int ret;
467
468 if (!wait_info) {
469 DPU_ERROR("invalid params\n");
470 return -EINVAL;
471 }
472 /* note: do master / slave checking outside */
473
474 /* return EWOULDBLOCK since we know the wait isn't necessary */
475 if (phys_enc->enable_state == DPU_ENC_DISABLED) {
476 DRM_ERROR("encoder is disabled id=%u, callback=%ps, IRQ=[%d, %d]\n",
477 DRMID(phys_enc->parent), func,
478 DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx));
479 return -EWOULDBLOCK;
480 }
481
482 if (irq_idx == 0) {
483 DRM_DEBUG_KMS("skip irq wait id=%u, callback=%ps\n",
484 DRMID(phys_enc->parent), func);
485 return 0;
486 }
487
488 DRM_DEBUG_KMS("id=%u, callback=%ps, IRQ=[%d, %d], pp=%d, pending_cnt=%d\n",
489 DRMID(phys_enc->parent), func,
490 DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx), phys_enc->hw_pp->idx - PINGPONG_0,
491 atomic_read(wait_info->atomic_cnt));
492
493 ret = dpu_encoder_helper_wait_event_timeout(
494 DRMID(phys_enc->parent),
495 irq_idx,
496 wait_info);
497
498 if (ret <= 0) {
499 irq_status = dpu_core_irq_read(phys_enc->dpu_kms, irq_idx);
500 if (irq_status) {
501 unsigned long flags;
502
503 DRM_DEBUG_KMS("IRQ=[%d, %d] not triggered id=%u, callback=%ps, pp=%d, atomic_cnt=%d\n",
504 DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx),
505 DRMID(phys_enc->parent), func,
506 phys_enc->hw_pp->idx - PINGPONG_0,
507 atomic_read(wait_info->atomic_cnt));
508 local_irq_save(flags);
509 func(phys_enc);
510 local_irq_restore(flags);
511 ret = 0;
512 } else {
513 ret = -ETIMEDOUT;
514 DRM_DEBUG_KMS("IRQ=[%d, %d] timeout id=%u, callback=%ps, pp=%d, atomic_cnt=%d\n",
515 DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx),
516 DRMID(phys_enc->parent), func,
517 phys_enc->hw_pp->idx - PINGPONG_0,
518 atomic_read(wait_info->atomic_cnt));
519 }
520 } else {
521 ret = 0;
522 trace_dpu_enc_irq_wait_success(DRMID(phys_enc->parent),
523 func, DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx),
524 phys_enc->hw_pp->idx - PINGPONG_0,
525 atomic_read(wait_info->atomic_cnt));
526 }
527
528 return ret;
529 }
530
531 /**
532 * dpu_encoder_get_vsync_count - get vsync count for the encoder.
533 * @drm_enc: Pointer to previously created drm encoder structure
534 */
dpu_encoder_get_vsync_count(struct drm_encoder * drm_enc)535 int dpu_encoder_get_vsync_count(struct drm_encoder *drm_enc)
536 {
537 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
538 struct dpu_encoder_phys *phys = dpu_enc ? dpu_enc->cur_master : NULL;
539 return phys ? atomic_read(&phys->vsync_cnt) : 0;
540 }
541
542 /**
543 * dpu_encoder_get_linecount - get interface line count for the encoder.
544 * @drm_enc: Pointer to previously created drm encoder structure
545 */
dpu_encoder_get_linecount(struct drm_encoder * drm_enc)546 int dpu_encoder_get_linecount(struct drm_encoder *drm_enc)
547 {
548 struct dpu_encoder_virt *dpu_enc;
549 struct dpu_encoder_phys *phys;
550 int linecount = 0;
551
552 dpu_enc = to_dpu_encoder_virt(drm_enc);
553 phys = dpu_enc ? dpu_enc->cur_master : NULL;
554
555 if (phys && phys->ops.get_line_count)
556 linecount = phys->ops.get_line_count(phys);
557
558 return linecount;
559 }
560
561 /**
562 * dpu_encoder_helper_split_config - split display configuration helper function
563 * This helper function may be used by physical encoders to configure
564 * the split display related registers.
565 * @phys_enc: Pointer to physical encoder structure
566 * @interface: enum dpu_intf setting
567 */
dpu_encoder_helper_split_config(struct dpu_encoder_phys * phys_enc,enum dpu_intf interface)568 void dpu_encoder_helper_split_config(
569 struct dpu_encoder_phys *phys_enc,
570 enum dpu_intf interface)
571 {
572 struct dpu_encoder_virt *dpu_enc;
573 struct split_pipe_cfg cfg = { 0 };
574 struct dpu_hw_mdp *hw_mdptop;
575 struct msm_display_info *disp_info;
576
577 if (!phys_enc->hw_mdptop || !phys_enc->parent) {
578 DPU_ERROR("invalid arg(s), encoder %d\n", phys_enc != NULL);
579 return;
580 }
581
582 dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
583 hw_mdptop = phys_enc->hw_mdptop;
584 disp_info = &dpu_enc->disp_info;
585
586 if (disp_info->intf_type != INTF_DSI)
587 return;
588
589 /**
590 * disable split modes since encoder will be operating in as the only
591 * encoder, either for the entire use case in the case of, for example,
592 * single DSI, or for this frame in the case of left/right only partial
593 * update.
594 */
595 if (phys_enc->split_role == ENC_ROLE_SOLO) {
596 if (hw_mdptop->ops.setup_split_pipe)
597 hw_mdptop->ops.setup_split_pipe(hw_mdptop, &cfg);
598 return;
599 }
600
601 cfg.en = true;
602 cfg.mode = phys_enc->intf_mode;
603 cfg.intf = interface;
604
605 if (cfg.en && phys_enc->ops.needs_single_flush &&
606 phys_enc->ops.needs_single_flush(phys_enc))
607 cfg.split_flush_en = true;
608
609 if (phys_enc->split_role == ENC_ROLE_MASTER) {
610 DPU_DEBUG_ENC(dpu_enc, "enable %d\n", cfg.en);
611
612 if (hw_mdptop->ops.setup_split_pipe)
613 hw_mdptop->ops.setup_split_pipe(hw_mdptop, &cfg);
614 }
615 }
616
617 /**
618 * dpu_encoder_use_dsc_merge - returns true if the encoder uses DSC merge topology.
619 * @drm_enc: Pointer to previously created drm encoder structure
620 */
dpu_encoder_use_dsc_merge(struct drm_encoder * drm_enc)621 bool dpu_encoder_use_dsc_merge(struct drm_encoder *drm_enc)
622 {
623 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
624 int i, intf_count = 0, num_dsc = 0;
625
626 for (i = 0; i < MAX_PHYS_ENCODERS_PER_VIRTUAL; i++)
627 if (dpu_enc->phys_encs[i])
628 intf_count++;
629
630 for (i = 0; i < MAX_CHANNELS_PER_ENC; i++)
631 if (dpu_enc->hw_dsc[i])
632 num_dsc++;
633
634 return (num_dsc > 0) && (num_dsc > intf_count);
635 }
636
637 /**
638 * dpu_encoder_get_dsc_config - get DSC config for the DPU encoder
639 * This helper function is used by physical encoder to get DSC config
640 * used for this encoder.
641 * @drm_enc: Pointer to encoder structure
642 */
dpu_encoder_get_dsc_config(struct drm_encoder * drm_enc)643 struct drm_dsc_config *dpu_encoder_get_dsc_config(struct drm_encoder *drm_enc)
644 {
645 struct msm_drm_private *priv = drm_enc->dev->dev_private;
646 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
647 int index = dpu_enc->disp_info.h_tile_instance[0];
648
649 if (dpu_enc->disp_info.intf_type == INTF_DSI)
650 return msm_dsi_get_dsc_config(priv->dsi[index]);
651
652 return NULL;
653 }
654
dpu_encoder_update_topology(struct drm_encoder * drm_enc,struct msm_display_topology * topology,struct drm_atomic_state * state,const struct drm_display_mode * adj_mode)655 void dpu_encoder_update_topology(struct drm_encoder *drm_enc,
656 struct msm_display_topology *topology,
657 struct drm_atomic_state *state,
658 const struct drm_display_mode *adj_mode)
659 {
660 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
661 struct msm_drm_private *priv = dpu_enc->base.dev->dev_private;
662 struct msm_display_info *disp_info = &dpu_enc->disp_info;
663 struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
664 struct drm_connector *connector;
665 struct drm_connector_state *conn_state;
666 struct drm_framebuffer *fb;
667 struct drm_dsc_config *dsc;
668
669 int i;
670
671 for (i = 0; i < MAX_PHYS_ENCODERS_PER_VIRTUAL; i++)
672 if (dpu_enc->phys_encs[i])
673 topology->num_intf++;
674
675 dsc = dpu_encoder_get_dsc_config(drm_enc);
676
677 /* We only support 2 DSC mode (with 2 LM and 1 INTF) */
678 if (dsc) {
679 /*
680 * Use 2 DSC encoders, 2 layer mixers and 1 or 2 interfaces
681 * when Display Stream Compression (DSC) is enabled,
682 * and when enough DSC blocks are available.
683 * This is power-optimal and can drive up to (including) 4k
684 * screens.
685 */
686 WARN(topology->num_intf > 2,
687 "DSC topology cannot support more than 2 interfaces\n");
688 if (topology->num_intf >= 2 || dpu_kms->catalog->dsc_count >= 2)
689 topology->num_dsc = 2;
690 else
691 topology->num_dsc = 1;
692 }
693
694 connector = drm_atomic_get_new_connector_for_encoder(state, drm_enc);
695 if (!connector)
696 return;
697 conn_state = drm_atomic_get_new_connector_state(state, connector);
698 if (!conn_state)
699 return;
700
701 /*
702 * Use CDM only for writeback or DP at the moment as other interfaces cannot handle it.
703 * If writeback itself cannot handle cdm for some reason it will fail in its atomic_check()
704 * earlier.
705 */
706 if (disp_info->intf_type == INTF_WB && conn_state->writeback_job) {
707 fb = conn_state->writeback_job->fb;
708
709 if (fb && MSM_FORMAT_IS_YUV(msm_framebuffer_format(fb)))
710 topology->num_cdm++;
711 } else if (disp_info->intf_type == INTF_DP) {
712 if (msm_dp_is_yuv_420_enabled(priv->dp[disp_info->h_tile_instance[0]], adj_mode))
713 topology->num_cdm++;
714 }
715 }
716
dpu_encoder_needs_modeset(struct drm_encoder * drm_enc,struct drm_atomic_state * state)717 bool dpu_encoder_needs_modeset(struct drm_encoder *drm_enc, struct drm_atomic_state *state)
718 {
719 struct drm_connector *connector;
720 struct drm_connector_state *conn_state;
721 struct drm_framebuffer *fb;
722 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
723
724 if (!drm_enc || !state)
725 return false;
726
727 connector = drm_atomic_get_new_connector_for_encoder(state, drm_enc);
728 if (!connector)
729 return false;
730
731 conn_state = drm_atomic_get_new_connector_state(state, connector);
732
733 /**
734 * These checks are duplicated from dpu_encoder_update_topology() since
735 * CRTC and encoder don't hold topology information
736 */
737 if (dpu_enc->disp_info.intf_type == INTF_WB && conn_state->writeback_job) {
738 fb = conn_state->writeback_job->fb;
739 if (fb && MSM_FORMAT_IS_YUV(msm_framebuffer_format(fb))) {
740 if (!dpu_enc->cur_master->hw_cdm)
741 return true;
742 } else {
743 if (dpu_enc->cur_master->hw_cdm)
744 return true;
745 }
746 }
747
748 return false;
749 }
750
_dpu_encoder_update_vsync_source(struct dpu_encoder_virt * dpu_enc,struct msm_display_info * disp_info)751 static void _dpu_encoder_update_vsync_source(struct dpu_encoder_virt *dpu_enc,
752 struct msm_display_info *disp_info)
753 {
754 struct dpu_vsync_source_cfg vsync_cfg = { 0 };
755 struct msm_drm_private *priv;
756 struct dpu_kms *dpu_kms;
757 struct dpu_hw_mdp *hw_mdptop;
758 struct drm_encoder *drm_enc;
759 struct dpu_encoder_phys *phys_enc;
760 int i;
761
762 if (!dpu_enc || !disp_info) {
763 DPU_ERROR("invalid param dpu_enc:%d or disp_info:%d\n",
764 dpu_enc != NULL, disp_info != NULL);
765 return;
766 } else if (dpu_enc->num_phys_encs > ARRAY_SIZE(dpu_enc->hw_pp)) {
767 DPU_ERROR("invalid num phys enc %d/%d\n",
768 dpu_enc->num_phys_encs,
769 (int) ARRAY_SIZE(dpu_enc->hw_pp));
770 return;
771 }
772
773 drm_enc = &dpu_enc->base;
774 /* this pointers are checked in virt_enable_helper */
775 priv = drm_enc->dev->dev_private;
776
777 dpu_kms = to_dpu_kms(priv->kms);
778 hw_mdptop = dpu_kms->hw_mdp;
779 if (!hw_mdptop) {
780 DPU_ERROR("invalid mdptop\n");
781 return;
782 }
783
784 if (hw_mdptop->ops.setup_vsync_source) {
785 for (i = 0; i < dpu_enc->num_phys_encs; i++)
786 vsync_cfg.ppnumber[i] = dpu_enc->hw_pp[i]->idx;
787
788 vsync_cfg.pp_count = dpu_enc->num_phys_encs;
789 vsync_cfg.frame_rate = drm_mode_vrefresh(&dpu_enc->base.crtc->state->adjusted_mode);
790
791 vsync_cfg.vsync_source = disp_info->vsync_source;
792
793 hw_mdptop->ops.setup_vsync_source(hw_mdptop, &vsync_cfg);
794
795 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
796 phys_enc = dpu_enc->phys_encs[i];
797
798 if (phys_enc->has_intf_te && phys_enc->hw_intf->ops.vsync_sel)
799 phys_enc->hw_intf->ops.vsync_sel(phys_enc->hw_intf,
800 vsync_cfg.vsync_source);
801 }
802 }
803 }
804
_dpu_encoder_irq_enable(struct drm_encoder * drm_enc)805 static void _dpu_encoder_irq_enable(struct drm_encoder *drm_enc)
806 {
807 struct dpu_encoder_virt *dpu_enc;
808 int i;
809
810 if (!drm_enc) {
811 DPU_ERROR("invalid encoder\n");
812 return;
813 }
814
815 dpu_enc = to_dpu_encoder_virt(drm_enc);
816
817 DPU_DEBUG_ENC(dpu_enc, "\n");
818 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
819 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
820
821 phys->ops.irq_enable(phys);
822 }
823 }
824
_dpu_encoder_irq_disable(struct drm_encoder * drm_enc)825 static void _dpu_encoder_irq_disable(struct drm_encoder *drm_enc)
826 {
827 struct dpu_encoder_virt *dpu_enc;
828 int i;
829
830 if (!drm_enc) {
831 DPU_ERROR("invalid encoder\n");
832 return;
833 }
834
835 dpu_enc = to_dpu_encoder_virt(drm_enc);
836
837 DPU_DEBUG_ENC(dpu_enc, "\n");
838 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
839 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
840
841 phys->ops.irq_disable(phys);
842 }
843 }
844
_dpu_encoder_resource_enable(struct drm_encoder * drm_enc)845 static void _dpu_encoder_resource_enable(struct drm_encoder *drm_enc)
846 {
847 struct msm_drm_private *priv;
848 struct dpu_kms *dpu_kms;
849 struct dpu_encoder_virt *dpu_enc;
850
851 dpu_enc = to_dpu_encoder_virt(drm_enc);
852 priv = drm_enc->dev->dev_private;
853 dpu_kms = to_dpu_kms(priv->kms);
854
855 trace_dpu_enc_rc_enable(DRMID(drm_enc));
856
857 if (!dpu_enc->cur_master) {
858 DPU_ERROR("encoder master not set\n");
859 return;
860 }
861
862 /* enable DPU core clks */
863 pm_runtime_get_sync(&dpu_kms->pdev->dev);
864
865 /* enable all the irq */
866 _dpu_encoder_irq_enable(drm_enc);
867 }
868
_dpu_encoder_resource_disable(struct drm_encoder * drm_enc)869 static void _dpu_encoder_resource_disable(struct drm_encoder *drm_enc)
870 {
871 struct msm_drm_private *priv;
872 struct dpu_kms *dpu_kms;
873 struct dpu_encoder_virt *dpu_enc;
874
875 dpu_enc = to_dpu_encoder_virt(drm_enc);
876 priv = drm_enc->dev->dev_private;
877 dpu_kms = to_dpu_kms(priv->kms);
878
879 trace_dpu_enc_rc_disable(DRMID(drm_enc));
880
881 if (!dpu_enc->cur_master) {
882 DPU_ERROR("encoder master not set\n");
883 return;
884 }
885
886 /* disable all the irq */
887 _dpu_encoder_irq_disable(drm_enc);
888
889 /* disable DPU core clks */
890 pm_runtime_put_sync(&dpu_kms->pdev->dev);
891 }
892
dpu_encoder_resource_control(struct drm_encoder * drm_enc,u32 sw_event)893 static int dpu_encoder_resource_control(struct drm_encoder *drm_enc,
894 u32 sw_event)
895 {
896 struct dpu_encoder_virt *dpu_enc;
897 struct msm_drm_private *priv;
898 bool is_vid_mode = false;
899
900 if (!drm_enc || !drm_enc->dev || !drm_enc->crtc) {
901 DPU_ERROR("invalid parameters\n");
902 return -EINVAL;
903 }
904 dpu_enc = to_dpu_encoder_virt(drm_enc);
905 priv = drm_enc->dev->dev_private;
906 is_vid_mode = !dpu_enc->disp_info.is_cmd_mode;
907
908 /*
909 * when idle_pc is not supported, process only KICKOFF, STOP and MODESET
910 * events and return early for other events (ie wb display).
911 */
912 if (!dpu_enc->idle_pc_supported &&
913 (sw_event != DPU_ENC_RC_EVENT_KICKOFF &&
914 sw_event != DPU_ENC_RC_EVENT_STOP &&
915 sw_event != DPU_ENC_RC_EVENT_PRE_STOP))
916 return 0;
917
918 trace_dpu_enc_rc(DRMID(drm_enc), sw_event, dpu_enc->idle_pc_supported,
919 dpu_enc->rc_state, "begin");
920
921 switch (sw_event) {
922 case DPU_ENC_RC_EVENT_KICKOFF:
923 /* cancel delayed off work, if any */
924 if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work))
925 DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n",
926 sw_event);
927
928 mutex_lock(&dpu_enc->rc_lock);
929
930 /* return if the resource control is already in ON state */
931 if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) {
932 DRM_DEBUG_ATOMIC("id;%u, sw_event:%d, rc in ON state\n",
933 DRMID(drm_enc), sw_event);
934 mutex_unlock(&dpu_enc->rc_lock);
935 return 0;
936 } else if (dpu_enc->rc_state != DPU_ENC_RC_STATE_OFF &&
937 dpu_enc->rc_state != DPU_ENC_RC_STATE_IDLE) {
938 DRM_DEBUG_ATOMIC("id;%u, sw_event:%d, rc in state %d\n",
939 DRMID(drm_enc), sw_event,
940 dpu_enc->rc_state);
941 mutex_unlock(&dpu_enc->rc_lock);
942 return -EINVAL;
943 }
944
945 if (is_vid_mode && dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE)
946 _dpu_encoder_irq_enable(drm_enc);
947 else
948 _dpu_encoder_resource_enable(drm_enc);
949
950 dpu_enc->rc_state = DPU_ENC_RC_STATE_ON;
951
952 trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
953 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
954 "kickoff");
955
956 mutex_unlock(&dpu_enc->rc_lock);
957 break;
958
959 case DPU_ENC_RC_EVENT_FRAME_DONE:
960 /*
961 * mutex lock is not used as this event happens at interrupt
962 * context. And locking is not required as, the other events
963 * like KICKOFF and STOP does a wait-for-idle before executing
964 * the resource_control
965 */
966 if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) {
967 DRM_DEBUG_KMS("id:%d, sw_event:%d,rc:%d-unexpected\n",
968 DRMID(drm_enc), sw_event,
969 dpu_enc->rc_state);
970 return -EINVAL;
971 }
972
973 /*
974 * schedule off work item only when there are no
975 * frames pending
976 */
977 if (dpu_crtc_frame_pending(drm_enc->crtc) > 1) {
978 DRM_DEBUG_KMS("id:%d skip schedule work\n",
979 DRMID(drm_enc));
980 return 0;
981 }
982
983 queue_delayed_work(priv->wq, &dpu_enc->delayed_off_work,
984 msecs_to_jiffies(dpu_enc->idle_timeout));
985
986 trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
987 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
988 "frame done");
989 break;
990
991 case DPU_ENC_RC_EVENT_PRE_STOP:
992 /* cancel delayed off work, if any */
993 if (cancel_delayed_work_sync(&dpu_enc->delayed_off_work))
994 DPU_DEBUG_ENC(dpu_enc, "sw_event:%d, work cancelled\n",
995 sw_event);
996
997 mutex_lock(&dpu_enc->rc_lock);
998
999 if (is_vid_mode &&
1000 dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) {
1001 _dpu_encoder_irq_enable(drm_enc);
1002 }
1003 /* skip if is already OFF or IDLE, resources are off already */
1004 else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF ||
1005 dpu_enc->rc_state == DPU_ENC_RC_STATE_IDLE) {
1006 DRM_DEBUG_KMS("id:%u, sw_event:%d, rc in %d state\n",
1007 DRMID(drm_enc), sw_event,
1008 dpu_enc->rc_state);
1009 mutex_unlock(&dpu_enc->rc_lock);
1010 return 0;
1011 }
1012
1013 dpu_enc->rc_state = DPU_ENC_RC_STATE_PRE_OFF;
1014
1015 trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1016 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1017 "pre stop");
1018
1019 mutex_unlock(&dpu_enc->rc_lock);
1020 break;
1021
1022 case DPU_ENC_RC_EVENT_STOP:
1023 mutex_lock(&dpu_enc->rc_lock);
1024
1025 /* return if the resource control is already in OFF state */
1026 if (dpu_enc->rc_state == DPU_ENC_RC_STATE_OFF) {
1027 DRM_DEBUG_KMS("id: %u, sw_event:%d, rc in OFF state\n",
1028 DRMID(drm_enc), sw_event);
1029 mutex_unlock(&dpu_enc->rc_lock);
1030 return 0;
1031 } else if (dpu_enc->rc_state == DPU_ENC_RC_STATE_ON) {
1032 DRM_ERROR("id: %u, sw_event:%d, rc in state %d\n",
1033 DRMID(drm_enc), sw_event, dpu_enc->rc_state);
1034 mutex_unlock(&dpu_enc->rc_lock);
1035 return -EINVAL;
1036 }
1037
1038 /**
1039 * expect to arrive here only if in either idle state or pre-off
1040 * and in IDLE state the resources are already disabled
1041 */
1042 if (dpu_enc->rc_state == DPU_ENC_RC_STATE_PRE_OFF)
1043 _dpu_encoder_resource_disable(drm_enc);
1044
1045 dpu_enc->rc_state = DPU_ENC_RC_STATE_OFF;
1046
1047 trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1048 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1049 "stop");
1050
1051 mutex_unlock(&dpu_enc->rc_lock);
1052 break;
1053
1054 case DPU_ENC_RC_EVENT_ENTER_IDLE:
1055 mutex_lock(&dpu_enc->rc_lock);
1056
1057 if (dpu_enc->rc_state != DPU_ENC_RC_STATE_ON) {
1058 DRM_ERROR("id: %u, sw_event:%d, rc:%d !ON state\n",
1059 DRMID(drm_enc), sw_event, dpu_enc->rc_state);
1060 mutex_unlock(&dpu_enc->rc_lock);
1061 return 0;
1062 }
1063
1064 /*
1065 * if we are in ON but a frame was just kicked off,
1066 * ignore the IDLE event, it's probably a stale timer event
1067 */
1068 if (dpu_enc->frame_busy_mask[0]) {
1069 DRM_ERROR("id:%u, sw_event:%d, rc:%d frame pending\n",
1070 DRMID(drm_enc), sw_event, dpu_enc->rc_state);
1071 mutex_unlock(&dpu_enc->rc_lock);
1072 return 0;
1073 }
1074
1075 if (is_vid_mode)
1076 _dpu_encoder_irq_disable(drm_enc);
1077 else
1078 _dpu_encoder_resource_disable(drm_enc);
1079
1080 dpu_enc->rc_state = DPU_ENC_RC_STATE_IDLE;
1081
1082 trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1083 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1084 "idle");
1085
1086 mutex_unlock(&dpu_enc->rc_lock);
1087 break;
1088
1089 default:
1090 DRM_ERROR("id:%u, unexpected sw_event: %d\n", DRMID(drm_enc),
1091 sw_event);
1092 trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1093 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1094 "error");
1095 break;
1096 }
1097
1098 trace_dpu_enc_rc(DRMID(drm_enc), sw_event,
1099 dpu_enc->idle_pc_supported, dpu_enc->rc_state,
1100 "end");
1101 return 0;
1102 }
1103
1104 /**
1105 * dpu_encoder_prepare_wb_job - prepare writeback job for the encoder.
1106 * @drm_enc: Pointer to previously created drm encoder structure
1107 * @job: Pointer to the current drm writeback job
1108 */
dpu_encoder_prepare_wb_job(struct drm_encoder * drm_enc,struct drm_writeback_job * job)1109 void dpu_encoder_prepare_wb_job(struct drm_encoder *drm_enc,
1110 struct drm_writeback_job *job)
1111 {
1112 struct dpu_encoder_virt *dpu_enc;
1113 int i;
1114
1115 dpu_enc = to_dpu_encoder_virt(drm_enc);
1116
1117 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1118 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1119
1120 if (phys->ops.prepare_wb_job)
1121 phys->ops.prepare_wb_job(phys, job);
1122
1123 }
1124 }
1125
1126 /**
1127 * dpu_encoder_cleanup_wb_job - cleanup writeback job for the encoder.
1128 * @drm_enc: Pointer to previously created drm encoder structure
1129 * @job: Pointer to the current drm writeback job
1130 */
dpu_encoder_cleanup_wb_job(struct drm_encoder * drm_enc,struct drm_writeback_job * job)1131 void dpu_encoder_cleanup_wb_job(struct drm_encoder *drm_enc,
1132 struct drm_writeback_job *job)
1133 {
1134 struct dpu_encoder_virt *dpu_enc;
1135 int i;
1136
1137 dpu_enc = to_dpu_encoder_virt(drm_enc);
1138
1139 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1140 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1141
1142 if (phys->ops.cleanup_wb_job)
1143 phys->ops.cleanup_wb_job(phys, job);
1144
1145 }
1146 }
1147
dpu_encoder_virt_atomic_mode_set(struct drm_encoder * drm_enc,struct drm_crtc_state * crtc_state,struct drm_connector_state * conn_state)1148 static void dpu_encoder_virt_atomic_mode_set(struct drm_encoder *drm_enc,
1149 struct drm_crtc_state *crtc_state,
1150 struct drm_connector_state *conn_state)
1151 {
1152 struct dpu_encoder_virt *dpu_enc;
1153 struct msm_drm_private *priv;
1154 struct dpu_kms *dpu_kms;
1155 struct dpu_global_state *global_state;
1156 struct dpu_hw_blk *hw_pp[MAX_CHANNELS_PER_ENC];
1157 struct dpu_hw_blk *hw_ctl[MAX_CHANNELS_PER_ENC];
1158 struct dpu_hw_blk *hw_dsc[MAX_CHANNELS_PER_ENC];
1159 struct dpu_hw_blk *hw_cwb[MAX_CHANNELS_PER_ENC];
1160 int num_ctl, num_pp, num_dsc;
1161 int num_cwb = 0;
1162 bool is_cwb_encoder;
1163 unsigned int dsc_mask = 0;
1164 unsigned int cwb_mask = 0;
1165 int i;
1166
1167 if (!drm_enc) {
1168 DPU_ERROR("invalid encoder\n");
1169 return;
1170 }
1171
1172 dpu_enc = to_dpu_encoder_virt(drm_enc);
1173 DPU_DEBUG_ENC(dpu_enc, "\n");
1174
1175 priv = drm_enc->dev->dev_private;
1176 dpu_kms = to_dpu_kms(priv->kms);
1177 is_cwb_encoder = drm_crtc_in_clone_mode(crtc_state) &&
1178 dpu_enc->disp_info.intf_type == INTF_WB;
1179
1180 global_state = dpu_kms_get_existing_global_state(dpu_kms);
1181 if (IS_ERR_OR_NULL(global_state)) {
1182 DPU_ERROR("Failed to get global state");
1183 return;
1184 }
1185
1186 trace_dpu_enc_mode_set(DRMID(drm_enc));
1187
1188 /* Query resource that have been reserved in atomic check step. */
1189 if (is_cwb_encoder) {
1190 num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1191 drm_enc->crtc,
1192 DPU_HW_BLK_DCWB_PINGPONG,
1193 hw_pp, ARRAY_SIZE(hw_pp));
1194 num_cwb = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1195 drm_enc->crtc,
1196 DPU_HW_BLK_CWB,
1197 hw_cwb, ARRAY_SIZE(hw_cwb));
1198 } else {
1199 num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1200 drm_enc->crtc,
1201 DPU_HW_BLK_PINGPONG, hw_pp,
1202 ARRAY_SIZE(hw_pp));
1203 }
1204
1205 for (i = 0; i < num_cwb; i++) {
1206 dpu_enc->hw_cwb[i] = to_dpu_hw_cwb(hw_cwb[i]);
1207 cwb_mask |= BIT(dpu_enc->hw_cwb[i]->idx - CWB_0);
1208 }
1209
1210 dpu_enc->cwb_mask = cwb_mask;
1211
1212 num_ctl = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1213 drm_enc->crtc, DPU_HW_BLK_CTL, hw_ctl, ARRAY_SIZE(hw_ctl));
1214
1215 for (i = 0; i < MAX_CHANNELS_PER_ENC; i++)
1216 dpu_enc->hw_pp[i] = i < num_pp ? to_dpu_hw_pingpong(hw_pp[i])
1217 : NULL;
1218
1219 num_dsc = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1220 drm_enc->crtc, DPU_HW_BLK_DSC,
1221 hw_dsc, ARRAY_SIZE(hw_dsc));
1222 for (i = 0; i < num_dsc; i++) {
1223 dpu_enc->hw_dsc[i] = to_dpu_hw_dsc(hw_dsc[i]);
1224 dsc_mask |= BIT(dpu_enc->hw_dsc[i]->idx - DSC_0);
1225 }
1226
1227 dpu_enc->dsc_mask = dsc_mask;
1228
1229 if ((dpu_enc->disp_info.intf_type == INTF_WB && conn_state->writeback_job) ||
1230 dpu_enc->disp_info.intf_type == INTF_DP) {
1231 struct dpu_hw_blk *hw_cdm = NULL;
1232
1233 dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
1234 drm_enc->crtc, DPU_HW_BLK_CDM,
1235 &hw_cdm, 1);
1236 dpu_enc->cur_master->hw_cdm = hw_cdm ? to_dpu_hw_cdm(hw_cdm) : NULL;
1237 }
1238
1239 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1240 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1241
1242 phys->hw_pp = dpu_enc->hw_pp[i];
1243 if (!phys->hw_pp) {
1244 DPU_ERROR_ENC(dpu_enc,
1245 "no pp block assigned at idx: %d\n", i);
1246 return;
1247 }
1248
1249 phys->hw_ctl = i < num_ctl ? to_dpu_hw_ctl(hw_ctl[i]) : NULL;
1250 if (!phys->hw_ctl) {
1251 DPU_ERROR_ENC(dpu_enc,
1252 "no ctl block assigned at idx: %d\n", i);
1253 return;
1254 }
1255
1256 phys->cached_mode = crtc_state->adjusted_mode;
1257 if (phys->ops.atomic_mode_set)
1258 phys->ops.atomic_mode_set(phys, crtc_state, conn_state);
1259 }
1260 }
1261
_dpu_encoder_virt_enable_helper(struct drm_encoder * drm_enc)1262 static void _dpu_encoder_virt_enable_helper(struct drm_encoder *drm_enc)
1263 {
1264 struct dpu_encoder_virt *dpu_enc = NULL;
1265 int i;
1266
1267 if (!drm_enc || !drm_enc->dev) {
1268 DPU_ERROR("invalid parameters\n");
1269 return;
1270 }
1271
1272 dpu_enc = to_dpu_encoder_virt(drm_enc);
1273 if (!dpu_enc || !dpu_enc->cur_master) {
1274 DPU_ERROR("invalid dpu encoder/master\n");
1275 return;
1276 }
1277
1278
1279 if (dpu_enc->disp_info.intf_type == INTF_DP &&
1280 dpu_enc->cur_master->hw_mdptop &&
1281 dpu_enc->cur_master->hw_mdptop->ops.intf_audio_select)
1282 dpu_enc->cur_master->hw_mdptop->ops.intf_audio_select(
1283 dpu_enc->cur_master->hw_mdptop);
1284
1285 if (dpu_enc->disp_info.is_cmd_mode)
1286 _dpu_encoder_update_vsync_source(dpu_enc, &dpu_enc->disp_info);
1287
1288 if (dpu_enc->disp_info.intf_type == INTF_DSI &&
1289 !WARN_ON(dpu_enc->num_phys_encs == 0)) {
1290 unsigned bpc = dpu_enc->connector->display_info.bpc;
1291 for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
1292 if (!dpu_enc->hw_pp[i])
1293 continue;
1294 _dpu_encoder_setup_dither(dpu_enc->hw_pp[i], bpc);
1295 }
1296 }
1297 }
1298
1299 /**
1300 * dpu_encoder_virt_runtime_resume - pm runtime resume the encoder configs
1301 * @drm_enc: encoder pointer
1302 */
dpu_encoder_virt_runtime_resume(struct drm_encoder * drm_enc)1303 void dpu_encoder_virt_runtime_resume(struct drm_encoder *drm_enc)
1304 {
1305 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1306
1307 mutex_lock(&dpu_enc->enc_lock);
1308
1309 if (!dpu_enc->enabled)
1310 goto out;
1311
1312 if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.restore)
1313 dpu_enc->cur_slave->ops.restore(dpu_enc->cur_slave);
1314 if (dpu_enc->cur_master && dpu_enc->cur_master->ops.restore)
1315 dpu_enc->cur_master->ops.restore(dpu_enc->cur_master);
1316
1317 _dpu_encoder_virt_enable_helper(drm_enc);
1318
1319 out:
1320 mutex_unlock(&dpu_enc->enc_lock);
1321 }
1322
dpu_encoder_virt_atomic_enable(struct drm_encoder * drm_enc,struct drm_atomic_state * state)1323 static void dpu_encoder_virt_atomic_enable(struct drm_encoder *drm_enc,
1324 struct drm_atomic_state *state)
1325 {
1326 struct dpu_encoder_virt *dpu_enc = NULL;
1327 int ret = 0;
1328 struct drm_display_mode *cur_mode = NULL;
1329
1330 dpu_enc = to_dpu_encoder_virt(drm_enc);
1331 dpu_enc->dsc = dpu_encoder_get_dsc_config(drm_enc);
1332
1333 atomic_set(&dpu_enc->frame_done_timeout_cnt, 0);
1334
1335 mutex_lock(&dpu_enc->enc_lock);
1336
1337 dpu_enc->commit_done_timedout = false;
1338
1339 dpu_enc->connector = drm_atomic_get_new_connector_for_encoder(state, drm_enc);
1340
1341 cur_mode = &dpu_enc->base.crtc->state->adjusted_mode;
1342
1343 dpu_enc->wide_bus_en = dpu_encoder_is_widebus_enabled(drm_enc);
1344
1345 trace_dpu_enc_enable(DRMID(drm_enc), cur_mode->hdisplay,
1346 cur_mode->vdisplay);
1347
1348 /* always enable slave encoder before master */
1349 if (dpu_enc->cur_slave && dpu_enc->cur_slave->ops.enable)
1350 dpu_enc->cur_slave->ops.enable(dpu_enc->cur_slave);
1351
1352 if (dpu_enc->cur_master && dpu_enc->cur_master->ops.enable)
1353 dpu_enc->cur_master->ops.enable(dpu_enc->cur_master);
1354
1355 ret = dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF);
1356 if (ret) {
1357 DPU_ERROR_ENC(dpu_enc, "dpu resource control failed: %d\n",
1358 ret);
1359 goto out;
1360 }
1361
1362 _dpu_encoder_virt_enable_helper(drm_enc);
1363
1364 dpu_enc->enabled = true;
1365
1366 out:
1367 mutex_unlock(&dpu_enc->enc_lock);
1368 }
1369
dpu_encoder_virt_atomic_disable(struct drm_encoder * drm_enc,struct drm_atomic_state * state)1370 static void dpu_encoder_virt_atomic_disable(struct drm_encoder *drm_enc,
1371 struct drm_atomic_state *state)
1372 {
1373 struct dpu_encoder_virt *dpu_enc = NULL;
1374 struct drm_crtc *crtc;
1375 struct drm_crtc_state *old_state = NULL;
1376 int i = 0;
1377
1378 dpu_enc = to_dpu_encoder_virt(drm_enc);
1379 DPU_DEBUG_ENC(dpu_enc, "\n");
1380
1381 crtc = drm_atomic_get_old_crtc_for_encoder(state, drm_enc);
1382 if (crtc)
1383 old_state = drm_atomic_get_old_crtc_state(state, crtc);
1384
1385 /*
1386 * The encoder is already disabled if self refresh mode was set earlier,
1387 * in the old_state for the corresponding crtc.
1388 */
1389 if (old_state && old_state->self_refresh_active)
1390 return;
1391
1392 mutex_lock(&dpu_enc->enc_lock);
1393 dpu_enc->enabled = false;
1394
1395 trace_dpu_enc_disable(DRMID(drm_enc));
1396
1397 /* wait for idle */
1398 dpu_encoder_wait_for_tx_complete(drm_enc);
1399
1400 dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_PRE_STOP);
1401
1402 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1403 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1404
1405 if (phys->ops.disable)
1406 phys->ops.disable(phys);
1407 }
1408
1409
1410 /* after phys waits for frame-done, should be no more frames pending */
1411 if (atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) {
1412 DPU_ERROR("enc%d timeout pending\n", drm_enc->base.id);
1413 del_timer_sync(&dpu_enc->frame_done_timer);
1414 }
1415
1416 dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_STOP);
1417
1418 dpu_enc->connector = NULL;
1419
1420 DPU_DEBUG_ENC(dpu_enc, "encoder disabled\n");
1421
1422 mutex_unlock(&dpu_enc->enc_lock);
1423 }
1424
dpu_encoder_get_intf(const struct dpu_mdss_cfg * catalog,struct dpu_rm * dpu_rm,enum dpu_intf_type type,u32 controller_id)1425 static struct dpu_hw_intf *dpu_encoder_get_intf(const struct dpu_mdss_cfg *catalog,
1426 struct dpu_rm *dpu_rm,
1427 enum dpu_intf_type type, u32 controller_id)
1428 {
1429 int i = 0;
1430
1431 if (type == INTF_WB)
1432 return NULL;
1433
1434 for (i = 0; i < catalog->intf_count; i++) {
1435 if (catalog->intf[i].type == type
1436 && catalog->intf[i].controller_id == controller_id) {
1437 return dpu_rm_get_intf(dpu_rm, catalog->intf[i].id);
1438 }
1439 }
1440
1441 return NULL;
1442 }
1443
1444 /**
1445 * dpu_encoder_vblank_callback - Notify virtual encoder of vblank IRQ reception
1446 * @drm_enc: Pointer to drm encoder structure
1447 * @phy_enc: Pointer to physical encoder
1448 * Note: This is called from IRQ handler context.
1449 */
dpu_encoder_vblank_callback(struct drm_encoder * drm_enc,struct dpu_encoder_phys * phy_enc)1450 void dpu_encoder_vblank_callback(struct drm_encoder *drm_enc,
1451 struct dpu_encoder_phys *phy_enc)
1452 {
1453 struct dpu_encoder_virt *dpu_enc = NULL;
1454 unsigned long lock_flags;
1455
1456 if (!drm_enc || !phy_enc)
1457 return;
1458
1459 DPU_ATRACE_BEGIN("encoder_vblank_callback");
1460 dpu_enc = to_dpu_encoder_virt(drm_enc);
1461
1462 atomic_inc(&phy_enc->vsync_cnt);
1463
1464 spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1465 if (dpu_enc->crtc)
1466 dpu_crtc_vblank_callback(dpu_enc->crtc);
1467 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1468
1469 DPU_ATRACE_END("encoder_vblank_callback");
1470 }
1471
1472 /**
1473 * dpu_encoder_underrun_callback - Notify virtual encoder of underrun IRQ reception
1474 * @drm_enc: Pointer to drm encoder structure
1475 * @phy_enc: Pointer to physical encoder
1476 * Note: This is called from IRQ handler context.
1477 */
dpu_encoder_underrun_callback(struct drm_encoder * drm_enc,struct dpu_encoder_phys * phy_enc)1478 void dpu_encoder_underrun_callback(struct drm_encoder *drm_enc,
1479 struct dpu_encoder_phys *phy_enc)
1480 {
1481 if (!phy_enc)
1482 return;
1483
1484 DPU_ATRACE_BEGIN("encoder_underrun_callback");
1485 atomic_inc(&phy_enc->underrun_cnt);
1486
1487 /* trigger dump only on the first underrun */
1488 if (atomic_read(&phy_enc->underrun_cnt) == 1)
1489 msm_disp_snapshot_state(drm_enc->dev);
1490
1491 trace_dpu_enc_underrun_cb(DRMID(drm_enc),
1492 atomic_read(&phy_enc->underrun_cnt));
1493 DPU_ATRACE_END("encoder_underrun_callback");
1494 }
1495
1496 /**
1497 * dpu_encoder_assign_crtc - Link the encoder to the crtc it's assigned to
1498 * @drm_enc: encoder pointer
1499 * @crtc: crtc pointer
1500 */
dpu_encoder_assign_crtc(struct drm_encoder * drm_enc,struct drm_crtc * crtc)1501 void dpu_encoder_assign_crtc(struct drm_encoder *drm_enc, struct drm_crtc *crtc)
1502 {
1503 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1504 unsigned long lock_flags;
1505
1506 spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1507 /* crtc should always be cleared before re-assigning */
1508 WARN_ON(crtc && dpu_enc->crtc);
1509 dpu_enc->crtc = crtc;
1510 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1511 }
1512
1513 /**
1514 * dpu_encoder_toggle_vblank_for_crtc - Toggles vblank interrupts on or off if
1515 * the encoder is assigned to the given crtc
1516 * @drm_enc: encoder pointer
1517 * @crtc: crtc pointer
1518 * @enable: true if vblank should be enabled
1519 */
dpu_encoder_toggle_vblank_for_crtc(struct drm_encoder * drm_enc,struct drm_crtc * crtc,bool enable)1520 void dpu_encoder_toggle_vblank_for_crtc(struct drm_encoder *drm_enc,
1521 struct drm_crtc *crtc, bool enable)
1522 {
1523 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1524 unsigned long lock_flags;
1525 int i;
1526
1527 trace_dpu_enc_vblank_cb(DRMID(drm_enc), enable);
1528
1529 spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1530 if (dpu_enc->crtc != crtc) {
1531 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1532 return;
1533 }
1534 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1535
1536 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1537 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1538
1539 if (phys->ops.control_vblank_irq)
1540 phys->ops.control_vblank_irq(phys, enable);
1541 }
1542 }
1543
1544 /**
1545 * dpu_encoder_frame_done_callback - Notify virtual encoder that this phys
1546 * encoder completes last request frame
1547 * @drm_enc: Pointer to drm encoder structure
1548 * @ready_phys: Pointer to physical encoder
1549 * @event: Event to process
1550 */
dpu_encoder_frame_done_callback(struct drm_encoder * drm_enc,struct dpu_encoder_phys * ready_phys,u32 event)1551 void dpu_encoder_frame_done_callback(
1552 struct drm_encoder *drm_enc,
1553 struct dpu_encoder_phys *ready_phys, u32 event)
1554 {
1555 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1556 unsigned int i;
1557
1558 if (event & (DPU_ENCODER_FRAME_EVENT_DONE
1559 | DPU_ENCODER_FRAME_EVENT_ERROR
1560 | DPU_ENCODER_FRAME_EVENT_PANEL_DEAD)) {
1561
1562 if (!dpu_enc->frame_busy_mask[0]) {
1563 /**
1564 * suppress frame_done without waiter,
1565 * likely autorefresh
1566 */
1567 trace_dpu_enc_frame_done_cb_not_busy(DRMID(drm_enc), event,
1568 dpu_encoder_helper_get_intf_type(ready_phys->intf_mode),
1569 ready_phys->hw_intf ? ready_phys->hw_intf->idx : -1,
1570 ready_phys->hw_wb ? ready_phys->hw_wb->idx : -1);
1571 return;
1572 }
1573
1574 /* One of the physical encoders has become idle */
1575 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1576 if (dpu_enc->phys_encs[i] == ready_phys) {
1577 trace_dpu_enc_frame_done_cb(DRMID(drm_enc), i,
1578 dpu_enc->frame_busy_mask[0]);
1579 clear_bit(i, dpu_enc->frame_busy_mask);
1580 }
1581 }
1582
1583 if (!dpu_enc->frame_busy_mask[0]) {
1584 atomic_set(&dpu_enc->frame_done_timeout_ms, 0);
1585 del_timer(&dpu_enc->frame_done_timer);
1586
1587 dpu_encoder_resource_control(drm_enc,
1588 DPU_ENC_RC_EVENT_FRAME_DONE);
1589
1590 if (dpu_enc->crtc)
1591 dpu_crtc_frame_event_cb(dpu_enc->crtc, event);
1592 }
1593 } else {
1594 if (dpu_enc->crtc)
1595 dpu_crtc_frame_event_cb(dpu_enc->crtc, event);
1596 }
1597 }
1598
dpu_encoder_off_work(struct work_struct * work)1599 static void dpu_encoder_off_work(struct work_struct *work)
1600 {
1601 struct dpu_encoder_virt *dpu_enc = container_of(work,
1602 struct dpu_encoder_virt, delayed_off_work.work);
1603
1604 dpu_encoder_resource_control(&dpu_enc->base,
1605 DPU_ENC_RC_EVENT_ENTER_IDLE);
1606
1607 dpu_encoder_frame_done_callback(&dpu_enc->base, NULL,
1608 DPU_ENCODER_FRAME_EVENT_IDLE);
1609 }
1610
1611 /**
1612 * _dpu_encoder_trigger_flush - trigger flush for a physical encoder
1613 * @drm_enc: Pointer to drm encoder structure
1614 * @phys: Pointer to physical encoder structure
1615 * @extra_flush_bits: Additional bit mask to include in flush trigger
1616 */
_dpu_encoder_trigger_flush(struct drm_encoder * drm_enc,struct dpu_encoder_phys * phys,uint32_t extra_flush_bits)1617 static void _dpu_encoder_trigger_flush(struct drm_encoder *drm_enc,
1618 struct dpu_encoder_phys *phys, uint32_t extra_flush_bits)
1619 {
1620 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
1621 struct dpu_hw_ctl *ctl;
1622 int pending_kickoff_cnt;
1623 u32 ret = UINT_MAX;
1624
1625 if (!phys->hw_pp) {
1626 DPU_ERROR("invalid pingpong hw\n");
1627 return;
1628 }
1629
1630 ctl = phys->hw_ctl;
1631 if (!ctl->ops.trigger_flush) {
1632 DPU_ERROR("missing trigger cb\n");
1633 return;
1634 }
1635
1636 pending_kickoff_cnt = dpu_encoder_phys_inc_pending(phys);
1637
1638 /* Return early if encoder is writeback and in clone mode */
1639 if (drm_enc->encoder_type == DRM_MODE_ENCODER_VIRTUAL &&
1640 dpu_enc->cwb_mask) {
1641 DPU_DEBUG("encoder %d skip flush for concurrent writeback encoder\n",
1642 DRMID(drm_enc));
1643 return;
1644 }
1645
1646
1647 if (extra_flush_bits && ctl->ops.update_pending_flush)
1648 ctl->ops.update_pending_flush(ctl, extra_flush_bits);
1649
1650 ctl->ops.trigger_flush(ctl);
1651
1652 if (ctl->ops.get_pending_flush)
1653 ret = ctl->ops.get_pending_flush(ctl);
1654
1655 trace_dpu_enc_trigger_flush(DRMID(drm_enc),
1656 dpu_encoder_helper_get_intf_type(phys->intf_mode),
1657 phys->hw_intf ? phys->hw_intf->idx : -1,
1658 phys->hw_wb ? phys->hw_wb->idx : -1,
1659 pending_kickoff_cnt, ctl->idx,
1660 extra_flush_bits, ret);
1661 }
1662
1663 /**
1664 * _dpu_encoder_trigger_start - trigger start for a physical encoder
1665 * @phys: Pointer to physical encoder structure
1666 */
_dpu_encoder_trigger_start(struct dpu_encoder_phys * phys)1667 static void _dpu_encoder_trigger_start(struct dpu_encoder_phys *phys)
1668 {
1669 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(phys->parent);
1670
1671 if (!phys) {
1672 DPU_ERROR("invalid argument(s)\n");
1673 return;
1674 }
1675
1676 if (!phys->hw_pp) {
1677 DPU_ERROR("invalid pingpong hw\n");
1678 return;
1679 }
1680
1681 if (phys->parent->encoder_type == DRM_MODE_ENCODER_VIRTUAL &&
1682 dpu_enc->cwb_mask) {
1683 DPU_DEBUG("encoder %d CWB enabled, skipping\n", DRMID(phys->parent));
1684 return;
1685 }
1686
1687 if (phys->ops.trigger_start && phys->enable_state != DPU_ENC_DISABLED)
1688 phys->ops.trigger_start(phys);
1689 }
1690
1691 /**
1692 * dpu_encoder_helper_trigger_start - control start helper function
1693 * This helper function may be optionally specified by physical
1694 * encoders if they require ctl_start triggering.
1695 * @phys_enc: Pointer to physical encoder structure
1696 */
dpu_encoder_helper_trigger_start(struct dpu_encoder_phys * phys_enc)1697 void dpu_encoder_helper_trigger_start(struct dpu_encoder_phys *phys_enc)
1698 {
1699 struct dpu_hw_ctl *ctl;
1700
1701 ctl = phys_enc->hw_ctl;
1702 if (ctl->ops.trigger_start) {
1703 ctl->ops.trigger_start(ctl);
1704 trace_dpu_enc_trigger_start(DRMID(phys_enc->parent), ctl->idx);
1705 }
1706 }
1707
dpu_encoder_helper_wait_event_timeout(int32_t drm_id,unsigned int irq_idx,struct dpu_encoder_wait_info * info)1708 static int dpu_encoder_helper_wait_event_timeout(
1709 int32_t drm_id,
1710 unsigned int irq_idx,
1711 struct dpu_encoder_wait_info *info)
1712 {
1713 int rc = 0;
1714 s64 expected_time = ktime_to_ms(ktime_get()) + info->timeout_ms;
1715 s64 jiffies = msecs_to_jiffies(info->timeout_ms);
1716 s64 time;
1717
1718 do {
1719 rc = wait_event_timeout(*(info->wq),
1720 atomic_read(info->atomic_cnt) == 0, jiffies);
1721 time = ktime_to_ms(ktime_get());
1722
1723 trace_dpu_enc_wait_event_timeout(drm_id,
1724 DPU_IRQ_REG(irq_idx), DPU_IRQ_BIT(irq_idx),
1725 rc, time,
1726 expected_time,
1727 atomic_read(info->atomic_cnt));
1728 /* If we timed out, counter is valid and time is less, wait again */
1729 } while (atomic_read(info->atomic_cnt) && (rc == 0) &&
1730 (time < expected_time));
1731
1732 return rc;
1733 }
1734
dpu_encoder_helper_hw_reset(struct dpu_encoder_phys * phys_enc)1735 static void dpu_encoder_helper_hw_reset(struct dpu_encoder_phys *phys_enc)
1736 {
1737 struct dpu_encoder_virt *dpu_enc;
1738 struct dpu_hw_ctl *ctl;
1739 int rc;
1740 struct drm_encoder *drm_enc;
1741
1742 dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
1743 ctl = phys_enc->hw_ctl;
1744 drm_enc = phys_enc->parent;
1745
1746 if (!ctl->ops.reset)
1747 return;
1748
1749 DRM_DEBUG_KMS("id:%u ctl %d reset\n", DRMID(drm_enc),
1750 ctl->idx);
1751
1752 rc = ctl->ops.reset(ctl);
1753 if (rc) {
1754 DPU_ERROR_ENC(dpu_enc, "ctl %d reset failure\n", ctl->idx);
1755 msm_disp_snapshot_state(drm_enc->dev);
1756 }
1757
1758 phys_enc->enable_state = DPU_ENC_ENABLED;
1759 }
1760
1761 /**
1762 * _dpu_encoder_kickoff_phys - handle physical encoder kickoff
1763 * Iterate through the physical encoders and perform consolidated flush
1764 * and/or control start triggering as needed. This is done in the virtual
1765 * encoder rather than the individual physical ones in order to handle
1766 * use cases that require visibility into multiple physical encoders at
1767 * a time.
1768 * @dpu_enc: Pointer to virtual encoder structure
1769 */
_dpu_encoder_kickoff_phys(struct dpu_encoder_virt * dpu_enc)1770 static void _dpu_encoder_kickoff_phys(struct dpu_encoder_virt *dpu_enc)
1771 {
1772 struct dpu_hw_ctl *ctl;
1773 uint32_t i, pending_flush;
1774 unsigned long lock_flags;
1775
1776 pending_flush = 0x0;
1777
1778 /* update pending counts and trigger kickoff ctl flush atomically */
1779 spin_lock_irqsave(&dpu_enc->enc_spinlock, lock_flags);
1780
1781 /* don't perform flush/start operations for slave encoders */
1782 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1783 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
1784
1785 if (phys->enable_state == DPU_ENC_DISABLED)
1786 continue;
1787
1788 ctl = phys->hw_ctl;
1789
1790 /*
1791 * This is cleared in frame_done worker, which isn't invoked
1792 * for async commits. So don't set this for async, since it'll
1793 * roll over to the next commit.
1794 */
1795 if (phys->split_role != ENC_ROLE_SLAVE)
1796 set_bit(i, dpu_enc->frame_busy_mask);
1797
1798 if (!phys->ops.needs_single_flush ||
1799 !phys->ops.needs_single_flush(phys))
1800 _dpu_encoder_trigger_flush(&dpu_enc->base, phys, 0x0);
1801 else if (ctl->ops.get_pending_flush)
1802 pending_flush |= ctl->ops.get_pending_flush(ctl);
1803 }
1804
1805 /* for split flush, combine pending flush masks and send to master */
1806 if (pending_flush && dpu_enc->cur_master) {
1807 _dpu_encoder_trigger_flush(
1808 &dpu_enc->base,
1809 dpu_enc->cur_master,
1810 pending_flush);
1811 }
1812
1813 _dpu_encoder_trigger_start(dpu_enc->cur_master);
1814
1815 spin_unlock_irqrestore(&dpu_enc->enc_spinlock, lock_flags);
1816 }
1817
1818 /**
1819 * dpu_encoder_trigger_kickoff_pending - Clear the flush bits from previous
1820 * kickoff and trigger the ctl prepare progress for command mode display.
1821 * @drm_enc: encoder pointer
1822 */
dpu_encoder_trigger_kickoff_pending(struct drm_encoder * drm_enc)1823 void dpu_encoder_trigger_kickoff_pending(struct drm_encoder *drm_enc)
1824 {
1825 struct dpu_encoder_virt *dpu_enc;
1826 struct dpu_encoder_phys *phys;
1827 unsigned int i;
1828 struct dpu_hw_ctl *ctl;
1829 struct msm_display_info *disp_info;
1830
1831 if (!drm_enc) {
1832 DPU_ERROR("invalid encoder\n");
1833 return;
1834 }
1835 dpu_enc = to_dpu_encoder_virt(drm_enc);
1836 disp_info = &dpu_enc->disp_info;
1837
1838 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
1839 phys = dpu_enc->phys_encs[i];
1840
1841 ctl = phys->hw_ctl;
1842 ctl->ops.clear_pending_flush(ctl);
1843
1844 /* update only for command mode primary ctl */
1845 if ((phys == dpu_enc->cur_master) &&
1846 disp_info->is_cmd_mode
1847 && ctl->ops.trigger_pending)
1848 ctl->ops.trigger_pending(ctl);
1849 }
1850 }
1851
_dpu_encoder_calculate_linetime(struct dpu_encoder_virt * dpu_enc,struct drm_display_mode * mode)1852 static u32 _dpu_encoder_calculate_linetime(struct dpu_encoder_virt *dpu_enc,
1853 struct drm_display_mode *mode)
1854 {
1855 u64 pclk_rate;
1856 u32 pclk_period;
1857 u32 line_time;
1858
1859 /*
1860 * For linetime calculation, only operate on master encoder.
1861 */
1862 if (!dpu_enc->cur_master)
1863 return 0;
1864
1865 if (!dpu_enc->cur_master->ops.get_line_count) {
1866 DPU_ERROR("get_line_count function not defined\n");
1867 return 0;
1868 }
1869
1870 pclk_rate = mode->clock; /* pixel clock in kHz */
1871 if (pclk_rate == 0) {
1872 DPU_ERROR("pclk is 0, cannot calculate line time\n");
1873 return 0;
1874 }
1875
1876 pclk_period = DIV_ROUND_UP_ULL(1000000000ull, pclk_rate);
1877 if (pclk_period == 0) {
1878 DPU_ERROR("pclk period is 0\n");
1879 return 0;
1880 }
1881
1882 /*
1883 * Line time calculation based on Pixel clock and HTOTAL.
1884 * Final unit is in ns.
1885 */
1886 line_time = (pclk_period * mode->htotal) / 1000;
1887 if (line_time == 0) {
1888 DPU_ERROR("line time calculation is 0\n");
1889 return 0;
1890 }
1891
1892 DPU_DEBUG_ENC(dpu_enc,
1893 "clk_rate=%lldkHz, clk_period=%d, linetime=%dns\n",
1894 pclk_rate, pclk_period, line_time);
1895
1896 return line_time;
1897 }
1898
1899 /**
1900 * dpu_encoder_vsync_time - get the time of the next vsync
1901 * @drm_enc: encoder pointer
1902 * @wakeup_time: pointer to ktime_t to write the vsync time to
1903 */
dpu_encoder_vsync_time(struct drm_encoder * drm_enc,ktime_t * wakeup_time)1904 int dpu_encoder_vsync_time(struct drm_encoder *drm_enc, ktime_t *wakeup_time)
1905 {
1906 struct drm_display_mode *mode;
1907 struct dpu_encoder_virt *dpu_enc;
1908 u32 cur_line;
1909 u32 line_time;
1910 u32 vtotal, time_to_vsync;
1911 ktime_t cur_time;
1912
1913 dpu_enc = to_dpu_encoder_virt(drm_enc);
1914
1915 if (!drm_enc->crtc || !drm_enc->crtc->state) {
1916 DPU_ERROR("crtc/crtc state object is NULL\n");
1917 return -EINVAL;
1918 }
1919 mode = &drm_enc->crtc->state->adjusted_mode;
1920
1921 line_time = _dpu_encoder_calculate_linetime(dpu_enc, mode);
1922 if (!line_time)
1923 return -EINVAL;
1924
1925 cur_line = dpu_enc->cur_master->ops.get_line_count(dpu_enc->cur_master);
1926
1927 vtotal = mode->vtotal;
1928 if (cur_line >= vtotal)
1929 time_to_vsync = line_time * vtotal;
1930 else
1931 time_to_vsync = line_time * (vtotal - cur_line);
1932
1933 if (time_to_vsync == 0) {
1934 DPU_ERROR("time to vsync should not be zero, vtotal=%d\n",
1935 vtotal);
1936 return -EINVAL;
1937 }
1938
1939 cur_time = ktime_get();
1940 *wakeup_time = ktime_add_ns(cur_time, time_to_vsync);
1941
1942 DPU_DEBUG_ENC(dpu_enc,
1943 "cur_line=%u vtotal=%u time_to_vsync=%u, cur_time=%lld, wakeup_time=%lld\n",
1944 cur_line, vtotal, time_to_vsync,
1945 ktime_to_ms(cur_time),
1946 ktime_to_ms(*wakeup_time));
1947 return 0;
1948 }
1949
1950 static u32
dpu_encoder_dsc_initial_line_calc(struct drm_dsc_config * dsc,u32 enc_ip_width)1951 dpu_encoder_dsc_initial_line_calc(struct drm_dsc_config *dsc,
1952 u32 enc_ip_width)
1953 {
1954 int ssm_delay, total_pixels, soft_slice_per_enc;
1955
1956 soft_slice_per_enc = enc_ip_width / dsc->slice_width;
1957
1958 /*
1959 * minimum number of initial line pixels is a sum of:
1960 * 1. sub-stream multiplexer delay (83 groups for 8bpc,
1961 * 91 for 10 bpc) * 3
1962 * 2. for two soft slice cases, add extra sub-stream multiplexer * 3
1963 * 3. the initial xmit delay
1964 * 4. total pipeline delay through the "lock step" of encoder (47)
1965 * 5. 6 additional pixels as the output of the rate buffer is
1966 * 48 bits wide
1967 */
1968 ssm_delay = ((dsc->bits_per_component < 10) ? 84 : 92);
1969 total_pixels = ssm_delay * 3 + dsc->initial_xmit_delay + 47;
1970 if (soft_slice_per_enc > 1)
1971 total_pixels += (ssm_delay * 3);
1972 return DIV_ROUND_UP(total_pixels, dsc->slice_width);
1973 }
1974
dpu_encoder_dsc_pipe_cfg(struct dpu_hw_ctl * ctl,struct dpu_hw_dsc * hw_dsc,struct dpu_hw_pingpong * hw_pp,struct drm_dsc_config * dsc,u32 common_mode,u32 initial_lines)1975 static void dpu_encoder_dsc_pipe_cfg(struct dpu_hw_ctl *ctl,
1976 struct dpu_hw_dsc *hw_dsc,
1977 struct dpu_hw_pingpong *hw_pp,
1978 struct drm_dsc_config *dsc,
1979 u32 common_mode,
1980 u32 initial_lines)
1981 {
1982 if (hw_dsc->ops.dsc_config)
1983 hw_dsc->ops.dsc_config(hw_dsc, dsc, common_mode, initial_lines);
1984
1985 if (hw_dsc->ops.dsc_config_thresh)
1986 hw_dsc->ops.dsc_config_thresh(hw_dsc, dsc);
1987
1988 if (hw_pp->ops.setup_dsc)
1989 hw_pp->ops.setup_dsc(hw_pp);
1990
1991 if (hw_dsc->ops.dsc_bind_pingpong_blk)
1992 hw_dsc->ops.dsc_bind_pingpong_blk(hw_dsc, hw_pp->idx);
1993
1994 if (hw_pp->ops.enable_dsc)
1995 hw_pp->ops.enable_dsc(hw_pp);
1996
1997 if (ctl->ops.update_pending_flush_dsc)
1998 ctl->ops.update_pending_flush_dsc(ctl, hw_dsc->idx);
1999 }
2000
dpu_encoder_prep_dsc(struct dpu_encoder_virt * dpu_enc,struct drm_dsc_config * dsc)2001 static void dpu_encoder_prep_dsc(struct dpu_encoder_virt *dpu_enc,
2002 struct drm_dsc_config *dsc)
2003 {
2004 struct dpu_encoder_phys *enc_master = dpu_enc->cur_master;
2005 struct dpu_hw_ctl *ctl = enc_master->hw_ctl;
2006 struct dpu_hw_dsc *hw_dsc[MAX_CHANNELS_PER_ENC];
2007 struct dpu_hw_pingpong *hw_pp[MAX_CHANNELS_PER_ENC];
2008 int this_frame_slices;
2009 int intf_ip_w, enc_ip_w;
2010 int dsc_common_mode;
2011 int pic_width;
2012 u32 initial_lines;
2013 int num_dsc = 0;
2014 int i;
2015
2016 for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
2017 hw_pp[i] = dpu_enc->hw_pp[i];
2018 hw_dsc[i] = dpu_enc->hw_dsc[i];
2019
2020 if (!hw_pp[i] || !hw_dsc[i])
2021 break;
2022
2023 num_dsc++;
2024 }
2025
2026 pic_width = dsc->pic_width;
2027
2028 dsc_common_mode = 0;
2029 if (num_dsc > 1)
2030 dsc_common_mode |= DSC_MODE_SPLIT_PANEL;
2031 if (dpu_encoder_use_dsc_merge(enc_master->parent))
2032 dsc_common_mode |= DSC_MODE_MULTIPLEX;
2033 if (enc_master->intf_mode == INTF_MODE_VIDEO)
2034 dsc_common_mode |= DSC_MODE_VIDEO;
2035
2036 this_frame_slices = pic_width / dsc->slice_width;
2037 intf_ip_w = this_frame_slices * dsc->slice_width;
2038
2039 enc_ip_w = intf_ip_w / num_dsc;
2040 initial_lines = dpu_encoder_dsc_initial_line_calc(dsc, enc_ip_w);
2041
2042 for (i = 0; i < num_dsc; i++)
2043 dpu_encoder_dsc_pipe_cfg(ctl, hw_dsc[i], hw_pp[i],
2044 dsc, dsc_common_mode, initial_lines);
2045 }
2046
2047 /**
2048 * dpu_encoder_prepare_for_kickoff - schedule double buffer flip of the ctl
2049 * path (i.e. ctl flush and start) at next appropriate time.
2050 * Immediately: if no previous commit is outstanding.
2051 * Delayed: Block until next trigger can be issued.
2052 * @drm_enc: encoder pointer
2053 */
dpu_encoder_prepare_for_kickoff(struct drm_encoder * drm_enc)2054 void dpu_encoder_prepare_for_kickoff(struct drm_encoder *drm_enc)
2055 {
2056 struct dpu_encoder_virt *dpu_enc;
2057 struct dpu_encoder_phys *phys;
2058 bool needs_hw_reset = false;
2059 unsigned int i;
2060
2061 dpu_enc = to_dpu_encoder_virt(drm_enc);
2062
2063 trace_dpu_enc_prepare_kickoff(DRMID(drm_enc));
2064
2065 /* prepare for next kickoff, may include waiting on previous kickoff */
2066 DPU_ATRACE_BEGIN("enc_prepare_for_kickoff");
2067 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2068 phys = dpu_enc->phys_encs[i];
2069 if (phys->ops.prepare_for_kickoff)
2070 phys->ops.prepare_for_kickoff(phys);
2071 if (phys->enable_state == DPU_ENC_ERR_NEEDS_HW_RESET)
2072 needs_hw_reset = true;
2073 }
2074 DPU_ATRACE_END("enc_prepare_for_kickoff");
2075
2076 dpu_encoder_resource_control(drm_enc, DPU_ENC_RC_EVENT_KICKOFF);
2077
2078 /* if any phys needs reset, reset all phys, in-order */
2079 if (needs_hw_reset) {
2080 trace_dpu_enc_prepare_kickoff_reset(DRMID(drm_enc));
2081 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2082 dpu_encoder_helper_hw_reset(dpu_enc->phys_encs[i]);
2083 }
2084 }
2085
2086 if (dpu_enc->dsc)
2087 dpu_encoder_prep_dsc(dpu_enc, dpu_enc->dsc);
2088 }
2089
2090 /**
2091 * dpu_encoder_is_valid_for_commit - check if encode has valid parameters for commit.
2092 * @drm_enc: Pointer to drm encoder structure
2093 */
dpu_encoder_is_valid_for_commit(struct drm_encoder * drm_enc)2094 bool dpu_encoder_is_valid_for_commit(struct drm_encoder *drm_enc)
2095 {
2096 struct dpu_encoder_virt *dpu_enc;
2097 unsigned int i;
2098 struct dpu_encoder_phys *phys;
2099
2100 dpu_enc = to_dpu_encoder_virt(drm_enc);
2101
2102 if (drm_enc->encoder_type == DRM_MODE_ENCODER_VIRTUAL) {
2103 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2104 phys = dpu_enc->phys_encs[i];
2105 if (phys->ops.is_valid_for_commit && !phys->ops.is_valid_for_commit(phys)) {
2106 DPU_DEBUG("invalid FB not kicking off\n");
2107 return false;
2108 }
2109 }
2110 }
2111
2112 return true;
2113 }
2114
2115 /**
2116 * dpu_encoder_start_frame_done_timer - Start the encoder frame done timer
2117 * @drm_enc: Pointer to drm encoder structure
2118 */
dpu_encoder_start_frame_done_timer(struct drm_encoder * drm_enc)2119 void dpu_encoder_start_frame_done_timer(struct drm_encoder *drm_enc)
2120 {
2121 struct dpu_encoder_virt *dpu_enc;
2122 unsigned long timeout_ms;
2123
2124 dpu_enc = to_dpu_encoder_virt(drm_enc);
2125 timeout_ms = DPU_ENCODER_FRAME_DONE_TIMEOUT_FRAMES * 1000 /
2126 drm_mode_vrefresh(&drm_enc->crtc->state->adjusted_mode);
2127
2128 atomic_set(&dpu_enc->frame_done_timeout_ms, timeout_ms);
2129 mod_timer(&dpu_enc->frame_done_timer,
2130 jiffies + msecs_to_jiffies(timeout_ms));
2131
2132 }
2133
2134 /**
2135 * dpu_encoder_kickoff - trigger a double buffer flip of the ctl path
2136 * (i.e. ctl flush and start) immediately.
2137 * @drm_enc: encoder pointer
2138 */
dpu_encoder_kickoff(struct drm_encoder * drm_enc)2139 void dpu_encoder_kickoff(struct drm_encoder *drm_enc)
2140 {
2141 struct dpu_encoder_virt *dpu_enc;
2142 struct dpu_encoder_phys *phys;
2143 unsigned int i;
2144
2145 DPU_ATRACE_BEGIN("encoder_kickoff");
2146 dpu_enc = to_dpu_encoder_virt(drm_enc);
2147
2148 trace_dpu_enc_kickoff(DRMID(drm_enc));
2149
2150 /* All phys encs are ready to go, trigger the kickoff */
2151 _dpu_encoder_kickoff_phys(dpu_enc);
2152
2153 /* allow phys encs to handle any post-kickoff business */
2154 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2155 phys = dpu_enc->phys_encs[i];
2156 if (phys->ops.handle_post_kickoff)
2157 phys->ops.handle_post_kickoff(phys);
2158 }
2159
2160 DPU_ATRACE_END("encoder_kickoff");
2161 }
2162
dpu_encoder_helper_reset_mixers(struct dpu_encoder_phys * phys_enc)2163 static void dpu_encoder_helper_reset_mixers(struct dpu_encoder_phys *phys_enc)
2164 {
2165 struct dpu_hw_mixer_cfg mixer;
2166 int i, num_lm;
2167 struct dpu_global_state *global_state;
2168 struct dpu_hw_blk *hw_lm[2];
2169 struct dpu_hw_mixer *hw_mixer[2];
2170 struct dpu_hw_ctl *ctl = phys_enc->hw_ctl;
2171
2172 memset(&mixer, 0, sizeof(mixer));
2173
2174 /* reset all mixers for this encoder */
2175 if (ctl->ops.clear_all_blendstages)
2176 ctl->ops.clear_all_blendstages(ctl);
2177
2178 global_state = dpu_kms_get_existing_global_state(phys_enc->dpu_kms);
2179
2180 num_lm = dpu_rm_get_assigned_resources(&phys_enc->dpu_kms->rm, global_state,
2181 phys_enc->parent->crtc, DPU_HW_BLK_LM, hw_lm, ARRAY_SIZE(hw_lm));
2182
2183 for (i = 0; i < num_lm; i++) {
2184 hw_mixer[i] = to_dpu_hw_mixer(hw_lm[i]);
2185 if (ctl->ops.update_pending_flush_mixer)
2186 ctl->ops.update_pending_flush_mixer(ctl, hw_mixer[i]->idx);
2187
2188 /* clear all blendstages */
2189 if (ctl->ops.setup_blendstage)
2190 ctl->ops.setup_blendstage(ctl, hw_mixer[i]->idx, NULL);
2191 }
2192 }
2193
dpu_encoder_dsc_pipe_clr(struct dpu_hw_ctl * ctl,struct dpu_hw_dsc * hw_dsc,struct dpu_hw_pingpong * hw_pp)2194 static void dpu_encoder_dsc_pipe_clr(struct dpu_hw_ctl *ctl,
2195 struct dpu_hw_dsc *hw_dsc,
2196 struct dpu_hw_pingpong *hw_pp)
2197 {
2198 if (hw_dsc->ops.dsc_disable)
2199 hw_dsc->ops.dsc_disable(hw_dsc);
2200
2201 if (hw_pp->ops.disable_dsc)
2202 hw_pp->ops.disable_dsc(hw_pp);
2203
2204 if (hw_dsc->ops.dsc_bind_pingpong_blk)
2205 hw_dsc->ops.dsc_bind_pingpong_blk(hw_dsc, PINGPONG_NONE);
2206
2207 if (ctl->ops.update_pending_flush_dsc)
2208 ctl->ops.update_pending_flush_dsc(ctl, hw_dsc->idx);
2209 }
2210
dpu_encoder_unprep_dsc(struct dpu_encoder_virt * dpu_enc)2211 static void dpu_encoder_unprep_dsc(struct dpu_encoder_virt *dpu_enc)
2212 {
2213 /* coding only for 2LM, 2enc, 1 dsc config */
2214 struct dpu_encoder_phys *enc_master = dpu_enc->cur_master;
2215 struct dpu_hw_ctl *ctl = enc_master->hw_ctl;
2216 struct dpu_hw_dsc *hw_dsc[MAX_CHANNELS_PER_ENC];
2217 struct dpu_hw_pingpong *hw_pp[MAX_CHANNELS_PER_ENC];
2218 int i;
2219
2220 for (i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
2221 hw_pp[i] = dpu_enc->hw_pp[i];
2222 hw_dsc[i] = dpu_enc->hw_dsc[i];
2223
2224 if (hw_pp[i] && hw_dsc[i])
2225 dpu_encoder_dsc_pipe_clr(ctl, hw_dsc[i], hw_pp[i]);
2226 }
2227 }
2228
2229 /**
2230 * dpu_encoder_helper_phys_cleanup - helper to cleanup dpu pipeline
2231 * @phys_enc: Pointer to physical encoder structure
2232 */
dpu_encoder_helper_phys_cleanup(struct dpu_encoder_phys * phys_enc)2233 void dpu_encoder_helper_phys_cleanup(struct dpu_encoder_phys *phys_enc)
2234 {
2235 struct dpu_hw_ctl *ctl = phys_enc->hw_ctl;
2236 struct dpu_hw_intf_cfg intf_cfg = { 0 };
2237 int i;
2238 struct dpu_encoder_virt *dpu_enc;
2239
2240 dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
2241
2242 ctl->ops.reset(ctl);
2243
2244 dpu_encoder_helper_reset_mixers(phys_enc);
2245
2246 /*
2247 * TODO: move the once-only operation like CTL flush/trigger
2248 * into dpu_encoder_virt_disable() and all operations which need
2249 * to be done per phys encoder into the phys_disable() op.
2250 */
2251 if (phys_enc->hw_wb) {
2252 /* disable the PP block */
2253 if (phys_enc->hw_wb->ops.bind_pingpong_blk)
2254 phys_enc->hw_wb->ops.bind_pingpong_blk(phys_enc->hw_wb, PINGPONG_NONE);
2255
2256 /* mark WB flush as pending */
2257 if (ctl->ops.update_pending_flush_wb)
2258 ctl->ops.update_pending_flush_wb(ctl, phys_enc->hw_wb->idx);
2259 } else {
2260 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2261 if (dpu_enc->phys_encs[i] && phys_enc->hw_intf->ops.bind_pingpong_blk)
2262 phys_enc->hw_intf->ops.bind_pingpong_blk(
2263 dpu_enc->phys_encs[i]->hw_intf,
2264 PINGPONG_NONE);
2265
2266 /* mark INTF flush as pending */
2267 if (ctl->ops.update_pending_flush_intf)
2268 ctl->ops.update_pending_flush_intf(ctl,
2269 dpu_enc->phys_encs[i]->hw_intf->idx);
2270 }
2271 }
2272
2273 if (phys_enc->hw_pp && phys_enc->hw_pp->ops.setup_dither)
2274 phys_enc->hw_pp->ops.setup_dither(phys_enc->hw_pp, NULL);
2275
2276 if (dpu_enc->cwb_mask)
2277 dpu_encoder_helper_phys_setup_cwb(phys_enc, false);
2278
2279 /* reset the merge 3D HW block */
2280 if (phys_enc->hw_pp && phys_enc->hw_pp->merge_3d) {
2281 phys_enc->hw_pp->merge_3d->ops.setup_3d_mode(phys_enc->hw_pp->merge_3d,
2282 BLEND_3D_NONE);
2283 if (ctl->ops.update_pending_flush_merge_3d)
2284 ctl->ops.update_pending_flush_merge_3d(ctl,
2285 phys_enc->hw_pp->merge_3d->idx);
2286 }
2287
2288 if (phys_enc->hw_cdm) {
2289 if (phys_enc->hw_cdm->ops.bind_pingpong_blk && phys_enc->hw_pp)
2290 phys_enc->hw_cdm->ops.bind_pingpong_blk(phys_enc->hw_cdm,
2291 PINGPONG_NONE);
2292 if (ctl->ops.update_pending_flush_cdm)
2293 ctl->ops.update_pending_flush_cdm(ctl,
2294 phys_enc->hw_cdm->idx);
2295 }
2296
2297 if (dpu_enc->dsc) {
2298 dpu_encoder_unprep_dsc(dpu_enc);
2299 dpu_enc->dsc = NULL;
2300 }
2301
2302 intf_cfg.stream_sel = 0; /* Don't care value for video mode */
2303 intf_cfg.mode_3d = dpu_encoder_helper_get_3d_blend_mode(phys_enc);
2304 intf_cfg.dsc = dpu_encoder_helper_get_dsc(phys_enc);
2305 intf_cfg.cwb = dpu_enc->cwb_mask;
2306
2307 if (phys_enc->hw_intf)
2308 intf_cfg.intf = phys_enc->hw_intf->idx;
2309 if (phys_enc->hw_wb)
2310 intf_cfg.wb = phys_enc->hw_wb->idx;
2311
2312 if (phys_enc->hw_pp && phys_enc->hw_pp->merge_3d)
2313 intf_cfg.merge_3d = phys_enc->hw_pp->merge_3d->idx;
2314
2315 if (ctl->ops.reset_intf_cfg)
2316 ctl->ops.reset_intf_cfg(ctl, &intf_cfg);
2317
2318 ctl->ops.trigger_flush(ctl);
2319 ctl->ops.trigger_start(ctl);
2320 ctl->ops.clear_pending_flush(ctl);
2321 }
2322
dpu_encoder_helper_phys_setup_cwb(struct dpu_encoder_phys * phys_enc,bool enable)2323 void dpu_encoder_helper_phys_setup_cwb(struct dpu_encoder_phys *phys_enc,
2324 bool enable)
2325 {
2326 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(phys_enc->parent);
2327 struct dpu_hw_cwb *hw_cwb;
2328 struct dpu_hw_ctl *hw_ctl;
2329 struct dpu_hw_cwb_setup_cfg cwb_cfg;
2330
2331 struct dpu_kms *dpu_kms;
2332 struct dpu_global_state *global_state;
2333 struct dpu_hw_blk *rt_pp_list[MAX_CHANNELS_PER_ENC];
2334 int num_pp;
2335
2336 if (!phys_enc->hw_wb)
2337 return;
2338
2339 hw_ctl = phys_enc->hw_ctl;
2340
2341 if (!phys_enc->hw_ctl) {
2342 DPU_DEBUG("[wb:%d] no ctl assigned\n",
2343 phys_enc->hw_wb->idx - WB_0);
2344 return;
2345 }
2346
2347 dpu_kms = phys_enc->dpu_kms;
2348 global_state = dpu_kms_get_existing_global_state(dpu_kms);
2349 num_pp = dpu_rm_get_assigned_resources(&dpu_kms->rm, global_state,
2350 phys_enc->parent->crtc,
2351 DPU_HW_BLK_PINGPONG, rt_pp_list,
2352 ARRAY_SIZE(rt_pp_list));
2353
2354 if (num_pp == 0 || num_pp > MAX_CHANNELS_PER_ENC) {
2355 DPU_DEBUG_ENC(dpu_enc, "invalid num_pp %d\n", num_pp);
2356 return;
2357 }
2358
2359 /*
2360 * The CWB mux supports using LM or DSPP as tap points. For now,
2361 * always use LM tap point
2362 */
2363 cwb_cfg.input = INPUT_MODE_LM_OUT;
2364
2365 for (int i = 0; i < MAX_CHANNELS_PER_ENC; i++) {
2366 hw_cwb = dpu_enc->hw_cwb[i];
2367 if (!hw_cwb)
2368 continue;
2369
2370 if (enable) {
2371 struct dpu_hw_pingpong *hw_pp =
2372 to_dpu_hw_pingpong(rt_pp_list[i]);
2373 cwb_cfg.pp_idx = hw_pp->idx;
2374 } else {
2375 cwb_cfg.pp_idx = PINGPONG_NONE;
2376 }
2377
2378 hw_cwb->ops.config_cwb(hw_cwb, &cwb_cfg);
2379
2380 if (hw_ctl->ops.update_pending_flush_cwb)
2381 hw_ctl->ops.update_pending_flush_cwb(hw_ctl, hw_cwb->idx);
2382 }
2383 }
2384
2385 /**
2386 * dpu_encoder_helper_phys_setup_cdm - setup chroma down sampling block
2387 * @phys_enc: Pointer to physical encoder
2388 * @dpu_fmt: Pinter to the format description
2389 * @output_type: HDMI/WB
2390 */
dpu_encoder_helper_phys_setup_cdm(struct dpu_encoder_phys * phys_enc,const struct msm_format * dpu_fmt,u32 output_type)2391 void dpu_encoder_helper_phys_setup_cdm(struct dpu_encoder_phys *phys_enc,
2392 const struct msm_format *dpu_fmt,
2393 u32 output_type)
2394 {
2395 struct dpu_hw_cdm *hw_cdm;
2396 struct dpu_hw_cdm_cfg *cdm_cfg;
2397 struct dpu_hw_pingpong *hw_pp;
2398 int ret;
2399
2400 if (!phys_enc)
2401 return;
2402
2403 cdm_cfg = &phys_enc->cdm_cfg;
2404 hw_pp = phys_enc->hw_pp;
2405 hw_cdm = phys_enc->hw_cdm;
2406
2407 if (!hw_cdm)
2408 return;
2409
2410 if (!MSM_FORMAT_IS_YUV(dpu_fmt)) {
2411 DPU_DEBUG("[enc:%d] cdm_disable fmt:%p4cc\n", DRMID(phys_enc->parent),
2412 &dpu_fmt->pixel_format);
2413 if (hw_cdm->ops.bind_pingpong_blk)
2414 hw_cdm->ops.bind_pingpong_blk(hw_cdm, PINGPONG_NONE);
2415
2416 return;
2417 }
2418
2419 memset(cdm_cfg, 0, sizeof(struct dpu_hw_cdm_cfg));
2420
2421 cdm_cfg->output_width = phys_enc->cached_mode.hdisplay;
2422 cdm_cfg->output_height = phys_enc->cached_mode.vdisplay;
2423 cdm_cfg->output_fmt = dpu_fmt;
2424 cdm_cfg->output_type = output_type;
2425 cdm_cfg->output_bit_depth = MSM_FORMAT_IS_DX(dpu_fmt) ?
2426 CDM_CDWN_OUTPUT_10BIT : CDM_CDWN_OUTPUT_8BIT;
2427 cdm_cfg->csc_cfg = &dpu_csc10_rgb2yuv_601l;
2428
2429 /* enable 10 bit logic */
2430 switch (cdm_cfg->output_fmt->chroma_sample) {
2431 case CHROMA_FULL:
2432 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
2433 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
2434 break;
2435 case CHROMA_H2V1:
2436 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
2437 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
2438 break;
2439 case CHROMA_420:
2440 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
2441 cdm_cfg->v_cdwn_type = CDM_CDWN_OFFSITE;
2442 break;
2443 case CHROMA_H1V2:
2444 default:
2445 DPU_ERROR("[enc:%d] unsupported chroma sampling type\n",
2446 DRMID(phys_enc->parent));
2447 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
2448 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
2449 break;
2450 }
2451
2452 DPU_DEBUG("[enc:%d] cdm_enable:%d,%d,%p4cc,%d,%d,%d,%d]\n",
2453 DRMID(phys_enc->parent), cdm_cfg->output_width,
2454 cdm_cfg->output_height, &cdm_cfg->output_fmt->pixel_format,
2455 cdm_cfg->output_type, cdm_cfg->output_bit_depth,
2456 cdm_cfg->h_cdwn_type, cdm_cfg->v_cdwn_type);
2457
2458 if (hw_cdm->ops.enable) {
2459 cdm_cfg->pp_id = hw_pp->idx;
2460 ret = hw_cdm->ops.enable(hw_cdm, cdm_cfg);
2461 if (ret < 0) {
2462 DPU_ERROR("[enc:%d] failed to enable CDM; ret:%d\n",
2463 DRMID(phys_enc->parent), ret);
2464 return;
2465 }
2466 }
2467 }
2468
2469 #ifdef CONFIG_DEBUG_FS
_dpu_encoder_status_show(struct seq_file * s,void * data)2470 static int _dpu_encoder_status_show(struct seq_file *s, void *data)
2471 {
2472 struct drm_encoder *drm_enc = s->private;
2473 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(drm_enc);
2474 int i;
2475
2476 mutex_lock(&dpu_enc->enc_lock);
2477 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2478 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2479
2480 seq_printf(s, "intf:%d wb:%d vsync:%8d underrun:%8d frame_done_cnt:%d",
2481 phys->hw_intf ? phys->hw_intf->idx - INTF_0 : -1,
2482 phys->hw_wb ? phys->hw_wb->idx - WB_0 : -1,
2483 atomic_read(&phys->vsync_cnt),
2484 atomic_read(&phys->underrun_cnt),
2485 atomic_read(&dpu_enc->frame_done_timeout_cnt));
2486
2487 seq_printf(s, "mode: %s\n", dpu_encoder_helper_get_intf_type(phys->intf_mode));
2488 }
2489 mutex_unlock(&dpu_enc->enc_lock);
2490
2491 return 0;
2492 }
2493
2494 DEFINE_SHOW_ATTRIBUTE(_dpu_encoder_status);
2495
dpu_encoder_debugfs_init(struct drm_encoder * drm_enc,struct dentry * root)2496 static void dpu_encoder_debugfs_init(struct drm_encoder *drm_enc, struct dentry *root)
2497 {
2498 /* don't error check these */
2499 debugfs_create_file("status", 0600,
2500 root, drm_enc, &_dpu_encoder_status_fops);
2501 }
2502 #else
2503 #define dpu_encoder_debugfs_init NULL
2504 #endif
2505
dpu_encoder_virt_add_phys_encs(struct drm_device * dev,struct msm_display_info * disp_info,struct dpu_encoder_virt * dpu_enc,struct dpu_enc_phys_init_params * params)2506 static int dpu_encoder_virt_add_phys_encs(
2507 struct drm_device *dev,
2508 struct msm_display_info *disp_info,
2509 struct dpu_encoder_virt *dpu_enc,
2510 struct dpu_enc_phys_init_params *params)
2511 {
2512 struct dpu_encoder_phys *enc = NULL;
2513
2514 DPU_DEBUG_ENC(dpu_enc, "\n");
2515
2516 /*
2517 * We may create up to NUM_PHYS_ENCODER_TYPES physical encoder types
2518 * in this function, check up-front.
2519 */
2520 if (dpu_enc->num_phys_encs + NUM_PHYS_ENCODER_TYPES >=
2521 ARRAY_SIZE(dpu_enc->phys_encs)) {
2522 DPU_ERROR_ENC(dpu_enc, "too many physical encoders %d\n",
2523 dpu_enc->num_phys_encs);
2524 return -EINVAL;
2525 }
2526
2527
2528 if (disp_info->intf_type == INTF_WB) {
2529 enc = dpu_encoder_phys_wb_init(dev, params);
2530
2531 if (IS_ERR(enc)) {
2532 DPU_ERROR_ENC(dpu_enc, "failed to init wb enc: %ld\n",
2533 PTR_ERR(enc));
2534 return PTR_ERR(enc);
2535 }
2536
2537 dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
2538 ++dpu_enc->num_phys_encs;
2539 } else if (disp_info->is_cmd_mode) {
2540 enc = dpu_encoder_phys_cmd_init(dev, params);
2541
2542 if (IS_ERR(enc)) {
2543 DPU_ERROR_ENC(dpu_enc, "failed to init cmd enc: %ld\n",
2544 PTR_ERR(enc));
2545 return PTR_ERR(enc);
2546 }
2547
2548 dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
2549 ++dpu_enc->num_phys_encs;
2550 } else {
2551 enc = dpu_encoder_phys_vid_init(dev, params);
2552
2553 if (IS_ERR(enc)) {
2554 DPU_ERROR_ENC(dpu_enc, "failed to init vid enc: %ld\n",
2555 PTR_ERR(enc));
2556 return PTR_ERR(enc);
2557 }
2558
2559 dpu_enc->phys_encs[dpu_enc->num_phys_encs] = enc;
2560 ++dpu_enc->num_phys_encs;
2561 }
2562
2563 if (params->split_role == ENC_ROLE_SLAVE)
2564 dpu_enc->cur_slave = enc;
2565 else
2566 dpu_enc->cur_master = enc;
2567
2568 return 0;
2569 }
2570
2571 /**
2572 * dpu_encoder_get_clones - Calculate the possible_clones for DPU encoder
2573 * @drm_enc: DRM encoder pointer
2574 * Returns: possible_clones mask
2575 */
dpu_encoder_get_clones(struct drm_encoder * drm_enc)2576 uint32_t dpu_encoder_get_clones(struct drm_encoder *drm_enc)
2577 {
2578 struct drm_encoder *curr;
2579 int type = drm_enc->encoder_type;
2580 uint32_t clone_mask = drm_encoder_mask(drm_enc);
2581
2582 /*
2583 * Set writeback as possible clones of real-time DSI encoders and vice
2584 * versa
2585 *
2586 * Writeback encoders can't be clones of each other and DSI
2587 * encoders can't be clones of each other.
2588 *
2589 * TODO: Add DP encoders as valid possible clones for writeback encoders
2590 * (and vice versa) once concurrent writeback has been validated for DP
2591 */
2592 drm_for_each_encoder(curr, drm_enc->dev) {
2593 if ((type == DRM_MODE_ENCODER_VIRTUAL &&
2594 curr->encoder_type == DRM_MODE_ENCODER_DSI) ||
2595 (type == DRM_MODE_ENCODER_DSI &&
2596 curr->encoder_type == DRM_MODE_ENCODER_VIRTUAL))
2597 clone_mask |= drm_encoder_mask(curr);
2598 }
2599
2600 return clone_mask;
2601 }
2602
dpu_encoder_setup_display(struct dpu_encoder_virt * dpu_enc,struct dpu_kms * dpu_kms,struct msm_display_info * disp_info)2603 static int dpu_encoder_setup_display(struct dpu_encoder_virt *dpu_enc,
2604 struct dpu_kms *dpu_kms,
2605 struct msm_display_info *disp_info)
2606 {
2607 int ret = 0;
2608 int i = 0;
2609 struct dpu_enc_phys_init_params phys_params;
2610
2611 if (!dpu_enc) {
2612 DPU_ERROR("invalid arg(s), enc %d\n", dpu_enc != NULL);
2613 return -EINVAL;
2614 }
2615
2616 dpu_enc->cur_master = NULL;
2617
2618 memset(&phys_params, 0, sizeof(phys_params));
2619 phys_params.dpu_kms = dpu_kms;
2620 phys_params.parent = &dpu_enc->base;
2621 phys_params.enc_spinlock = &dpu_enc->enc_spinlock;
2622
2623 WARN_ON(disp_info->num_of_h_tiles < 1);
2624
2625 DPU_DEBUG("dsi_info->num_of_h_tiles %d\n", disp_info->num_of_h_tiles);
2626
2627 if (disp_info->intf_type != INTF_WB)
2628 dpu_enc->idle_pc_supported =
2629 dpu_kms->catalog->caps->has_idle_pc;
2630
2631 mutex_lock(&dpu_enc->enc_lock);
2632 for (i = 0; i < disp_info->num_of_h_tiles && !ret; i++) {
2633 /*
2634 * Left-most tile is at index 0, content is controller id
2635 * h_tile_instance_ids[2] = {0, 1}; DSI0 = left, DSI1 = right
2636 * h_tile_instance_ids[2] = {1, 0}; DSI1 = left, DSI0 = right
2637 */
2638 u32 controller_id = disp_info->h_tile_instance[i];
2639
2640 if (disp_info->num_of_h_tiles > 1) {
2641 if (i == 0)
2642 phys_params.split_role = ENC_ROLE_MASTER;
2643 else
2644 phys_params.split_role = ENC_ROLE_SLAVE;
2645 } else {
2646 phys_params.split_role = ENC_ROLE_SOLO;
2647 }
2648
2649 DPU_DEBUG("h_tile_instance %d = %d, split_role %d\n",
2650 i, controller_id, phys_params.split_role);
2651
2652 phys_params.hw_intf = dpu_encoder_get_intf(dpu_kms->catalog, &dpu_kms->rm,
2653 disp_info->intf_type,
2654 controller_id);
2655
2656 if (disp_info->intf_type == INTF_WB && controller_id < WB_MAX)
2657 phys_params.hw_wb = dpu_rm_get_wb(&dpu_kms->rm, controller_id);
2658
2659 if (!phys_params.hw_intf && !phys_params.hw_wb) {
2660 DPU_ERROR_ENC(dpu_enc, "no intf or wb block assigned at idx: %d\n", i);
2661 ret = -EINVAL;
2662 break;
2663 }
2664
2665 if (phys_params.hw_intf && phys_params.hw_wb) {
2666 DPU_ERROR_ENC(dpu_enc,
2667 "invalid phys both intf and wb block at idx: %d\n", i);
2668 ret = -EINVAL;
2669 break;
2670 }
2671
2672 ret = dpu_encoder_virt_add_phys_encs(dpu_kms->dev, disp_info,
2673 dpu_enc, &phys_params);
2674 if (ret) {
2675 DPU_ERROR_ENC(dpu_enc, "failed to add phys encs\n");
2676 break;
2677 }
2678 }
2679
2680 mutex_unlock(&dpu_enc->enc_lock);
2681
2682 return ret;
2683 }
2684
dpu_encoder_frame_done_timeout(struct timer_list * t)2685 static void dpu_encoder_frame_done_timeout(struct timer_list *t)
2686 {
2687 struct dpu_encoder_virt *dpu_enc = from_timer(dpu_enc, t,
2688 frame_done_timer);
2689 struct drm_encoder *drm_enc = &dpu_enc->base;
2690 u32 event;
2691
2692 if (!drm_enc->dev) {
2693 DPU_ERROR("invalid parameters\n");
2694 return;
2695 }
2696
2697 if (!dpu_enc->frame_busy_mask[0] || !dpu_enc->crtc) {
2698 DRM_DEBUG_KMS("id:%u invalid timeout frame_busy_mask=%lu\n",
2699 DRMID(drm_enc), dpu_enc->frame_busy_mask[0]);
2700 return;
2701 } else if (!atomic_xchg(&dpu_enc->frame_done_timeout_ms, 0)) {
2702 DRM_DEBUG_KMS("id:%u invalid timeout\n", DRMID(drm_enc));
2703 return;
2704 }
2705
2706 DPU_ERROR_ENC_RATELIMITED(dpu_enc, "frame done timeout\n");
2707
2708 if (atomic_inc_return(&dpu_enc->frame_done_timeout_cnt) == 1)
2709 msm_disp_snapshot_state(drm_enc->dev);
2710
2711 event = DPU_ENCODER_FRAME_EVENT_ERROR;
2712 trace_dpu_enc_frame_done_timeout(DRMID(drm_enc), event);
2713 dpu_crtc_frame_event_cb(dpu_enc->crtc, event);
2714 }
2715
2716 static const struct drm_encoder_helper_funcs dpu_encoder_helper_funcs = {
2717 .atomic_mode_set = dpu_encoder_virt_atomic_mode_set,
2718 .atomic_disable = dpu_encoder_virt_atomic_disable,
2719 .atomic_enable = dpu_encoder_virt_atomic_enable,
2720 };
2721
2722 static const struct drm_encoder_funcs dpu_encoder_funcs = {
2723 .debugfs_init = dpu_encoder_debugfs_init,
2724 };
2725
2726 /**
2727 * dpu_encoder_init - initialize virtual encoder object
2728 * @dev: Pointer to drm device structure
2729 * @drm_enc_mode: corresponding DRM_MODE_ENCODER_* constant
2730 * @disp_info: Pointer to display information structure
2731 * Returns: Pointer to newly created drm encoder
2732 */
dpu_encoder_init(struct drm_device * dev,int drm_enc_mode,struct msm_display_info * disp_info)2733 struct drm_encoder *dpu_encoder_init(struct drm_device *dev,
2734 int drm_enc_mode,
2735 struct msm_display_info *disp_info)
2736 {
2737 struct msm_drm_private *priv = dev->dev_private;
2738 struct dpu_kms *dpu_kms = to_dpu_kms(priv->kms);
2739 struct dpu_encoder_virt *dpu_enc;
2740 int ret;
2741
2742 dpu_enc = drmm_encoder_alloc(dev, struct dpu_encoder_virt, base,
2743 &dpu_encoder_funcs, drm_enc_mode, NULL);
2744 if (IS_ERR(dpu_enc))
2745 return ERR_CAST(dpu_enc);
2746
2747 drm_encoder_helper_add(&dpu_enc->base, &dpu_encoder_helper_funcs);
2748
2749 spin_lock_init(&dpu_enc->enc_spinlock);
2750 dpu_enc->enabled = false;
2751 mutex_init(&dpu_enc->enc_lock);
2752 mutex_init(&dpu_enc->rc_lock);
2753
2754 ret = dpu_encoder_setup_display(dpu_enc, dpu_kms, disp_info);
2755 if (ret) {
2756 DPU_ERROR("failed to setup encoder\n");
2757 return ERR_PTR(-ENOMEM);
2758 }
2759
2760 atomic_set(&dpu_enc->frame_done_timeout_ms, 0);
2761 atomic_set(&dpu_enc->frame_done_timeout_cnt, 0);
2762 timer_setup(&dpu_enc->frame_done_timer,
2763 dpu_encoder_frame_done_timeout, 0);
2764
2765 INIT_DELAYED_WORK(&dpu_enc->delayed_off_work,
2766 dpu_encoder_off_work);
2767 dpu_enc->idle_timeout = IDLE_TIMEOUT;
2768
2769 memcpy(&dpu_enc->disp_info, disp_info, sizeof(*disp_info));
2770
2771 DPU_DEBUG_ENC(dpu_enc, "created\n");
2772
2773 return &dpu_enc->base;
2774 }
2775
2776 /**
2777 * dpu_encoder_wait_for_commit_done() - Wait for encoder to flush pending state
2778 * @drm_enc: encoder pointer
2779 *
2780 * Wait for hardware to have flushed the current pending changes to hardware at
2781 * a vblank or CTL_START. Physical encoders will map this differently depending
2782 * on the type: vid mode -> vsync_irq, cmd mode -> CTL_START.
2783 *
2784 * Return: 0 on success, -EWOULDBLOCK if already signaled, error otherwise
2785 */
dpu_encoder_wait_for_commit_done(struct drm_encoder * drm_enc)2786 int dpu_encoder_wait_for_commit_done(struct drm_encoder *drm_enc)
2787 {
2788 struct dpu_encoder_virt *dpu_enc = NULL;
2789 int i, ret = 0;
2790
2791 if (!drm_enc) {
2792 DPU_ERROR("invalid encoder\n");
2793 return -EINVAL;
2794 }
2795 dpu_enc = to_dpu_encoder_virt(drm_enc);
2796 DPU_DEBUG_ENC(dpu_enc, "\n");
2797
2798 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2799 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2800
2801 if (phys->ops.wait_for_commit_done) {
2802 DPU_ATRACE_BEGIN("wait_for_commit_done");
2803 ret = phys->ops.wait_for_commit_done(phys);
2804 DPU_ATRACE_END("wait_for_commit_done");
2805 if (ret == -ETIMEDOUT && !dpu_enc->commit_done_timedout) {
2806 dpu_enc->commit_done_timedout = true;
2807 msm_disp_snapshot_state(drm_enc->dev);
2808 }
2809 if (ret)
2810 return ret;
2811 }
2812 }
2813
2814 return ret;
2815 }
2816
2817 /**
2818 * dpu_encoder_wait_for_tx_complete() - Wait for encoder to transfer pixels to panel
2819 * @drm_enc: encoder pointer
2820 *
2821 * Wait for the hardware to transfer all the pixels to the panel. Physical
2822 * encoders will map this differently depending on the type: vid mode -> vsync_irq,
2823 * cmd mode -> pp_done.
2824 *
2825 * Return: 0 on success, -EWOULDBLOCK if already signaled, error otherwise
2826 */
dpu_encoder_wait_for_tx_complete(struct drm_encoder * drm_enc)2827 int dpu_encoder_wait_for_tx_complete(struct drm_encoder *drm_enc)
2828 {
2829 struct dpu_encoder_virt *dpu_enc = NULL;
2830 int i, ret = 0;
2831
2832 if (!drm_enc) {
2833 DPU_ERROR("invalid encoder\n");
2834 return -EINVAL;
2835 }
2836 dpu_enc = to_dpu_encoder_virt(drm_enc);
2837 DPU_DEBUG_ENC(dpu_enc, "\n");
2838
2839 for (i = 0; i < dpu_enc->num_phys_encs; i++) {
2840 struct dpu_encoder_phys *phys = dpu_enc->phys_encs[i];
2841
2842 if (phys->ops.wait_for_tx_complete) {
2843 DPU_ATRACE_BEGIN("wait_for_tx_complete");
2844 ret = phys->ops.wait_for_tx_complete(phys);
2845 DPU_ATRACE_END("wait_for_tx_complete");
2846 if (ret)
2847 return ret;
2848 }
2849 }
2850
2851 return ret;
2852 }
2853
2854 /**
2855 * dpu_encoder_get_intf_mode - get interface mode of the given encoder
2856 * @encoder: Pointer to drm encoder object
2857 */
dpu_encoder_get_intf_mode(struct drm_encoder * encoder)2858 enum dpu_intf_mode dpu_encoder_get_intf_mode(struct drm_encoder *encoder)
2859 {
2860 struct dpu_encoder_virt *dpu_enc = NULL;
2861
2862 if (!encoder) {
2863 DPU_ERROR("invalid encoder\n");
2864 return INTF_MODE_NONE;
2865 }
2866 dpu_enc = to_dpu_encoder_virt(encoder);
2867
2868 if (dpu_enc->cur_master)
2869 return dpu_enc->cur_master->intf_mode;
2870
2871 if (dpu_enc->num_phys_encs)
2872 return dpu_enc->phys_encs[0]->intf_mode;
2873
2874 return INTF_MODE_NONE;
2875 }
2876
2877 /**
2878 * dpu_encoder_helper_get_cwb_mask - get CWB blocks mask for the DPU encoder
2879 * @phys_enc: Pointer to physical encoder structure
2880 */
dpu_encoder_helper_get_cwb_mask(struct dpu_encoder_phys * phys_enc)2881 unsigned int dpu_encoder_helper_get_cwb_mask(struct dpu_encoder_phys *phys_enc)
2882 {
2883 struct drm_encoder *encoder = phys_enc->parent;
2884 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(encoder);
2885
2886 return dpu_enc->cwb_mask;
2887 }
2888
2889 /**
2890 * dpu_encoder_helper_get_dsc - get DSC blocks mask for the DPU encoder
2891 * This helper function is used by physical encoder to get DSC blocks mask
2892 * used for this encoder.
2893 * @phys_enc: Pointer to physical encoder structure
2894 */
dpu_encoder_helper_get_dsc(struct dpu_encoder_phys * phys_enc)2895 unsigned int dpu_encoder_helper_get_dsc(struct dpu_encoder_phys *phys_enc)
2896 {
2897 struct drm_encoder *encoder = phys_enc->parent;
2898 struct dpu_encoder_virt *dpu_enc = to_dpu_encoder_virt(encoder);
2899
2900 return dpu_enc->dsc_mask;
2901 }
2902
dpu_encoder_phys_init(struct dpu_encoder_phys * phys_enc,struct dpu_enc_phys_init_params * p)2903 void dpu_encoder_phys_init(struct dpu_encoder_phys *phys_enc,
2904 struct dpu_enc_phys_init_params *p)
2905 {
2906 phys_enc->hw_mdptop = p->dpu_kms->hw_mdp;
2907 phys_enc->hw_intf = p->hw_intf;
2908 phys_enc->hw_wb = p->hw_wb;
2909 phys_enc->parent = p->parent;
2910 phys_enc->dpu_kms = p->dpu_kms;
2911 phys_enc->split_role = p->split_role;
2912 phys_enc->enc_spinlock = p->enc_spinlock;
2913 phys_enc->enable_state = DPU_ENC_DISABLED;
2914
2915 atomic_set(&phys_enc->pending_kickoff_cnt, 0);
2916 atomic_set(&phys_enc->pending_ctlstart_cnt, 0);
2917
2918 atomic_set(&phys_enc->vsync_cnt, 0);
2919 atomic_set(&phys_enc->underrun_cnt, 0);
2920
2921 init_waitqueue_head(&phys_enc->pending_kickoff_wq);
2922 }
2923