blob: a26126f481a9454cb014f05ca4897f5820278ca1 [file] [log] [blame]
Alan Kwongbb27c092016-07-20 16:41:25 -04001/*
Lloyd Atkinson8772e202016-09-26 17:52:16 -04002 * Copyright (c) 2015-2017 The Linux Foundation. All rights reserved.
Alan Kwongbb27c092016-07-20 16:41:25 -04003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
Alan Kwong4c3cf4c2016-09-25 20:08:09 -040015#define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
Alan Kwongbb27c092016-07-20 16:41:25 -040016#include <linux/debugfs.h>
17
18#include "sde_encoder_phys.h"
19#include "sde_formats.h"
20#include "sde_hw_top.h"
21#include "sde_hw_interrupts.h"
Alan Kwongf5dd86c2016-08-09 18:08:17 -040022#include "sde_core_irq.h"
Alan Kwongbb27c092016-07-20 16:41:25 -040023#include "sde_wb.h"
Lloyd Atkinson8772e202016-09-26 17:52:16 -040024#include "sde_vbif.h"
Alan Kwongbb27c092016-07-20 16:41:25 -040025
Alan Kwongbb27c092016-07-20 16:41:25 -040026#define to_sde_encoder_phys_wb(x) \
27 container_of(x, struct sde_encoder_phys_wb, base)
28
Lloyd Atkinson5d40d312016-09-06 08:34:13 -040029#define WBID(wb_enc) ((wb_enc) ? wb_enc->wb_dev->wb_idx : -1)
Alan Kwongbb27c092016-07-20 16:41:25 -040030
Alan Kwongd6a38602017-03-11 22:26:25 -080031#define TO_S15D16(_x_) ((_x_) << 7)
32
33/**
34 * sde_rgb2yuv_601l - rgb to yuv color space conversion matrix
35 *
36 */
37static struct sde_csc_cfg sde_encoder_phys_wb_rgb2yuv_601l = {
38 {
39 TO_S15D16(0x0083), TO_S15D16(0x0102), TO_S15D16(0x0032),
40 TO_S15D16(0x1fb5), TO_S15D16(0x1f6c), TO_S15D16(0x00e1),
41 TO_S15D16(0x00e1), TO_S15D16(0x1f45), TO_S15D16(0x1fdc)
42 },
43 { 0x00, 0x00, 0x00 },
44 { 0x0040, 0x0200, 0x0200 },
45 { 0x000, 0x3ff, 0x000, 0x3ff, 0x000, 0x3ff },
46 { 0x040, 0x3ac, 0x040, 0x3c0, 0x040, 0x3c0 },
47};
48
Alan Kwongbb27c092016-07-20 16:41:25 -040049/**
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -040050 * sde_encoder_phys_wb_is_master - report wb always as master encoder
51 */
52static bool sde_encoder_phys_wb_is_master(struct sde_encoder_phys *phys_enc)
53{
54 return true;
55}
56
57/**
Alan Kwongbb27c092016-07-20 16:41:25 -040058 * sde_encoder_phys_wb_get_intr_type - get interrupt type based on block mode
59 * @hw_wb: Pointer to h/w writeback driver
60 */
61static enum sde_intr_type sde_encoder_phys_wb_get_intr_type(
62 struct sde_hw_wb *hw_wb)
63{
64 return (hw_wb->caps->features & BIT(SDE_WB_BLOCK_MODE)) ?
65 SDE_IRQ_TYPE_WB_ROT_COMP : SDE_IRQ_TYPE_WB_WFD_COMP;
66}
67
68/**
Alan Kwong5d324e42016-07-28 22:56:18 -040069 * sde_encoder_phys_wb_set_ot_limit - set OT limit for writeback interface
70 * @phys_enc: Pointer to physical encoder
71 */
72static void sde_encoder_phys_wb_set_ot_limit(
73 struct sde_encoder_phys *phys_enc)
74{
75 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
76 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
77 struct sde_vbif_set_ot_params ot_params;
78
79 memset(&ot_params, 0, sizeof(ot_params));
80 ot_params.xin_id = hw_wb->caps->xin_id;
81 ot_params.num = hw_wb->idx - WB_0;
82 ot_params.width = wb_enc->wb_roi.w;
83 ot_params.height = wb_enc->wb_roi.h;
84 ot_params.is_wfd = true;
85 ot_params.frame_rate = phys_enc->cached_mode.vrefresh;
86 ot_params.vbif_idx = hw_wb->caps->vbif_idx;
87 ot_params.clk_ctrl = hw_wb->caps->clk_ctrl;
88 ot_params.rd = false;
89
90 sde_vbif_set_ot_limit(phys_enc->sde_kms, &ot_params);
91}
92
93/**
Alan Kwongbb27c092016-07-20 16:41:25 -040094 * sde_encoder_phys_wb_set_traffic_shaper - set traffic shaper for writeback
95 * @phys_enc: Pointer to physical encoder
96 */
97static void sde_encoder_phys_wb_set_traffic_shaper(
98 struct sde_encoder_phys *phys_enc)
99{
100 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
101 struct sde_hw_wb_cfg *wb_cfg = &wb_enc->wb_cfg;
102
103 /* traffic shaper is only enabled for rotator */
104 wb_cfg->ts_cfg.en = false;
105}
106
107/**
108 * sde_encoder_phys_setup_cdm - setup chroma down block
109 * @phys_enc: Pointer to physical encoder
110 * @fb: Pointer to output framebuffer
111 * @format: Output format
112 */
113void sde_encoder_phys_setup_cdm(struct sde_encoder_phys *phys_enc,
114 struct drm_framebuffer *fb, const struct sde_format *format,
115 struct sde_rect *wb_roi)
116{
117 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
118 struct sde_hw_cdm_cfg *cdm_cfg = &phys_enc->cdm_cfg;
119 int ret;
120
121 if (!SDE_FORMAT_IS_YUV(format)) {
122 SDE_DEBUG("[cdm_disable fmt:%x]\n",
123 format->base.pixel_format);
124
125 if (hw_cdm && hw_cdm->ops.disable)
126 hw_cdm->ops.disable(hw_cdm);
127
128 return;
129 }
130
131 memset(cdm_cfg, 0, sizeof(struct sde_hw_cdm_cfg));
132
133 cdm_cfg->output_width = wb_roi->w;
134 cdm_cfg->output_height = wb_roi->h;
135 cdm_cfg->output_fmt = format;
136 cdm_cfg->output_type = CDM_CDWN_OUTPUT_WB;
abeykun5ed42e12016-08-24 17:09:26 -0400137 cdm_cfg->output_bit_depth = SDE_FORMAT_IS_DX(format) ?
138 CDM_CDWN_OUTPUT_10BIT : CDM_CDWN_OUTPUT_8BIT;
Alan Kwongbb27c092016-07-20 16:41:25 -0400139
140 /* enable 10 bit logic */
141 switch (cdm_cfg->output_fmt->chroma_sample) {
142 case SDE_CHROMA_RGB:
143 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
144 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
145 break;
146 case SDE_CHROMA_H2V1:
147 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
148 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
149 break;
150 case SDE_CHROMA_420:
151 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
152 cdm_cfg->v_cdwn_type = CDM_CDWN_OFFSITE;
153 break;
154 case SDE_CHROMA_H1V2:
155 default:
156 SDE_ERROR("unsupported chroma sampling type\n");
157 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
158 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
159 break;
160 }
161
162 SDE_DEBUG("[cdm_enable:%d,%d,%X,%d,%d,%d,%d]\n",
163 cdm_cfg->output_width,
164 cdm_cfg->output_height,
165 cdm_cfg->output_fmt->base.pixel_format,
166 cdm_cfg->output_type,
167 cdm_cfg->output_bit_depth,
168 cdm_cfg->h_cdwn_type,
169 cdm_cfg->v_cdwn_type);
170
Alan Kwongd6a38602017-03-11 22:26:25 -0800171 if (hw_cdm && hw_cdm->ops.setup_csc_data) {
172 ret = hw_cdm->ops.setup_csc_data(hw_cdm,
173 &sde_encoder_phys_wb_rgb2yuv_601l);
174 if (ret < 0) {
175 SDE_ERROR("failed to setup CSC %d\n", ret);
176 return;
177 }
178 }
179
Alan Kwongbb27c092016-07-20 16:41:25 -0400180 if (hw_cdm && hw_cdm->ops.setup_cdwn) {
181 ret = hw_cdm->ops.setup_cdwn(hw_cdm, cdm_cfg);
182 if (ret < 0) {
183 SDE_ERROR("failed to setup CDM %d\n", ret);
184 return;
185 }
186 }
187
188 if (hw_cdm && hw_cdm->ops.enable) {
189 ret = hw_cdm->ops.enable(hw_cdm, cdm_cfg);
190 if (ret < 0) {
191 SDE_ERROR("failed to enable CDM %d\n", ret);
192 return;
193 }
194 }
195}
196
197/**
198 * sde_encoder_phys_wb_setup_fb - setup output framebuffer
199 * @phys_enc: Pointer to physical encoder
200 * @fb: Pointer to output framebuffer
201 * @wb_roi: Pointer to output region of interest
202 */
203static void sde_encoder_phys_wb_setup_fb(struct sde_encoder_phys *phys_enc,
204 struct drm_framebuffer *fb, struct sde_rect *wb_roi)
205{
206 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -0400207 struct sde_hw_wb *hw_wb;
208 struct sde_hw_wb_cfg *wb_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400209 const struct msm_format *format;
210 int ret, mmu_id;
211
Clarence Ip03521982016-08-26 10:49:47 -0400212 if (!phys_enc) {
213 SDE_ERROR("invalid encoder\n");
214 return;
215 }
216
217 hw_wb = wb_enc->hw_wb;
218 wb_cfg = &wb_enc->wb_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400219 memset(wb_cfg, 0, sizeof(struct sde_hw_wb_cfg));
220
Clarence Ip03521982016-08-26 10:49:47 -0400221 wb_cfg->intf_mode = phys_enc->intf_mode;
Alan Kwongbb27c092016-07-20 16:41:25 -0400222 wb_cfg->is_secure = (fb->flags & DRM_MODE_FB_SECURE) ? true : false;
223 mmu_id = (wb_cfg->is_secure) ?
224 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] :
225 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE];
226
227 SDE_DEBUG("[fb_secure:%d]\n", wb_cfg->is_secure);
228
229 format = msm_framebuffer_format(fb);
Dhaval Patelccbcb3d2016-08-22 11:58:14 -0700230 if (!format) {
231 SDE_DEBUG("invalid format for fb\n");
232 return;
233 }
234
Alan Kwongbb27c092016-07-20 16:41:25 -0400235 wb_cfg->dest.format = sde_get_sde_format_ext(
236 format->pixel_format,
237 fb->modifier,
238 drm_format_num_planes(fb->pixel_format));
239 if (!wb_cfg->dest.format) {
240 /* this error should be detected during atomic_check */
241 SDE_ERROR("failed to get format %x\n", format->pixel_format);
242 return;
243 }
abeykunf1539f72016-08-24 16:08:03 -0400244 wb_cfg->roi = *wb_roi;
Alan Kwongbb27c092016-07-20 16:41:25 -0400245
abeykunf1539f72016-08-24 16:08:03 -0400246 if (hw_wb->caps->features & BIT(SDE_WB_XY_ROI_OFFSET)) {
247 ret = sde_format_populate_layout(mmu_id, fb, &wb_cfg->dest);
248 if (ret) {
249 SDE_DEBUG("failed to populate layout %d\n", ret);
250 return;
251 }
252 wb_cfg->dest.width = fb->width;
253 wb_cfg->dest.height = fb->height;
254 wb_cfg->dest.num_planes = wb_cfg->dest.format->num_planes;
255 } else {
256 ret = sde_format_populate_layout_with_roi(mmu_id, fb, wb_roi,
Alan Kwongbb27c092016-07-20 16:41:25 -0400257 &wb_cfg->dest);
abeykunf1539f72016-08-24 16:08:03 -0400258 if (ret) {
259 /* this error should be detected during atomic_check */
260 SDE_DEBUG("failed to populate layout %d\n", ret);
261 return;
262 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400263 }
264
265 if ((wb_cfg->dest.format->fetch_planes == SDE_PLANE_PLANAR) &&
266 (wb_cfg->dest.format->element[0] == C1_B_Cb))
267 swap(wb_cfg->dest.plane_addr[1], wb_cfg->dest.plane_addr[2]);
268
269 SDE_DEBUG("[fb_offset:%8.8x,%8.8x,%8.8x,%8.8x]\n",
270 wb_cfg->dest.plane_addr[0],
271 wb_cfg->dest.plane_addr[1],
272 wb_cfg->dest.plane_addr[2],
273 wb_cfg->dest.plane_addr[3]);
274 SDE_DEBUG("[fb_stride:%8.8x,%8.8x,%8.8x,%8.8x]\n",
275 wb_cfg->dest.plane_pitch[0],
276 wb_cfg->dest.plane_pitch[1],
277 wb_cfg->dest.plane_pitch[2],
278 wb_cfg->dest.plane_pitch[3]);
279
abeykunf1539f72016-08-24 16:08:03 -0400280 if (hw_wb->ops.setup_roi)
281 hw_wb->ops.setup_roi(hw_wb, wb_cfg);
282
Alan Kwongbb27c092016-07-20 16:41:25 -0400283 if (hw_wb->ops.setup_outformat)
284 hw_wb->ops.setup_outformat(hw_wb, wb_cfg);
285
286 if (hw_wb->ops.setup_outaddress)
287 hw_wb->ops.setup_outaddress(hw_wb, wb_cfg);
288}
289
290/**
291 * sde_encoder_phys_wb_setup_cdp - setup chroma down prefetch block
292 * @phys_enc: Pointer to physical encoder
293 */
294static void sde_encoder_phys_wb_setup_cdp(struct sde_encoder_phys *phys_enc)
295{
296 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
297 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
298 struct sde_hw_intf_cfg *intf_cfg = &wb_enc->intf_cfg;
299
300 memset(intf_cfg, 0, sizeof(struct sde_hw_intf_cfg));
301
302 intf_cfg->intf = SDE_NONE;
303 intf_cfg->wb = hw_wb->idx;
Lloyd Atkinson55987b02016-08-16 16:57:46 -0400304 intf_cfg->mode_3d = sde_encoder_helper_get_3d_blend_mode(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400305
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400306 if (phys_enc->hw_ctl && phys_enc->hw_ctl->ops.setup_intf_cfg)
Alan Kwongbb27c092016-07-20 16:41:25 -0400307 phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl,
308 intf_cfg);
309}
310
311/**
312 * sde_encoder_phys_wb_atomic_check - verify and fixup given atomic states
313 * @phys_enc: Pointer to physical encoder
314 * @crtc_state: Pointer to CRTC atomic state
315 * @conn_state: Pointer to connector atomic state
316 */
317static int sde_encoder_phys_wb_atomic_check(
318 struct sde_encoder_phys *phys_enc,
319 struct drm_crtc_state *crtc_state,
320 struct drm_connector_state *conn_state)
321{
322 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
323 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
324 const struct sde_wb_cfg *wb_cfg = hw_wb->caps;
325 struct drm_framebuffer *fb;
326 const struct sde_format *fmt;
327 struct sde_rect wb_roi;
328 const struct drm_display_mode *mode = &crtc_state->mode;
329 int rc;
330
331 SDE_DEBUG("[atomic_check:%d,%d,\"%s\",%d,%d]\n",
332 hw_wb->idx - WB_0, mode->base.id, mode->name,
333 mode->hdisplay, mode->vdisplay);
334
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400335 if (!conn_state || !conn_state->connector) {
336 SDE_ERROR("invalid connector state\n");
337 return -EINVAL;
338 } else if (conn_state->connector->status !=
339 connector_status_connected) {
340 SDE_ERROR("connector not connected %d\n",
341 conn_state->connector->status);
342 return -EINVAL;
343 }
344
Alan Kwongbb27c092016-07-20 16:41:25 -0400345 memset(&wb_roi, 0, sizeof(struct sde_rect));
346
347 rc = sde_wb_connector_state_get_output_roi(conn_state, &wb_roi);
348 if (rc) {
349 SDE_ERROR("failed to get roi %d\n", rc);
350 return rc;
351 }
352
353 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi.x, wb_roi.y,
354 wb_roi.w, wb_roi.h);
355
356 fb = sde_wb_connector_state_get_output_fb(conn_state);
357 if (!fb) {
358 SDE_ERROR("no output framebuffer\n");
359 return -EINVAL;
360 }
361
362 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
363 fb->width, fb->height);
364
365 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
366 drm_format_num_planes(fb->pixel_format));
367 if (!fmt) {
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400368 SDE_ERROR("unsupported output pixel format:%x\n",
Alan Kwongbb27c092016-07-20 16:41:25 -0400369 fb->pixel_format);
370 return -EINVAL;
371 }
372
373 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
374 fb->modifier[0]);
375
376 if (SDE_FORMAT_IS_YUV(fmt) &&
377 !(wb_cfg->features & BIT(SDE_WB_YUV_CONFIG))) {
378 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
379 return -EINVAL;
380 }
381
382 if (SDE_FORMAT_IS_UBWC(fmt) &&
383 !(wb_cfg->features & BIT(SDE_WB_UBWC_1_0))) {
384 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
385 return -EINVAL;
386 }
387
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400388 if (SDE_FORMAT_IS_YUV(fmt) != !!phys_enc->hw_cdm)
389 crtc_state->mode_changed = true;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400390
Alan Kwongbb27c092016-07-20 16:41:25 -0400391 if (wb_roi.w && wb_roi.h) {
392 if (wb_roi.w != mode->hdisplay) {
393 SDE_ERROR("invalid roi w=%d, mode w=%d\n", wb_roi.w,
394 mode->hdisplay);
395 return -EINVAL;
396 } else if (wb_roi.h != mode->vdisplay) {
397 SDE_ERROR("invalid roi h=%d, mode h=%d\n", wb_roi.h,
398 mode->vdisplay);
399 return -EINVAL;
400 } else if (wb_roi.x + wb_roi.w > fb->width) {
401 SDE_ERROR("invalid roi x=%d, w=%d, fb w=%d\n",
402 wb_roi.x, wb_roi.w, fb->width);
403 return -EINVAL;
404 } else if (wb_roi.y + wb_roi.h > fb->height) {
405 SDE_ERROR("invalid roi y=%d, h=%d, fb h=%d\n",
406 wb_roi.y, wb_roi.h, fb->height);
407 return -EINVAL;
408 } else if (wb_roi.w > wb_cfg->sblk->maxlinewidth) {
409 SDE_ERROR("invalid roi w=%d, maxlinewidth=%u\n",
410 wb_roi.w, wb_cfg->sblk->maxlinewidth);
411 return -EINVAL;
412 }
413 } else {
414 if (wb_roi.x || wb_roi.y) {
415 SDE_ERROR("invalid roi x=%d, y=%d\n",
416 wb_roi.x, wb_roi.y);
417 return -EINVAL;
418 } else if (fb->width != mode->hdisplay) {
419 SDE_ERROR("invalid fb w=%d, mode w=%d\n", fb->width,
420 mode->hdisplay);
421 return -EINVAL;
422 } else if (fb->height != mode->vdisplay) {
423 SDE_ERROR("invalid fb h=%d, mode h=%d\n", fb->height,
424 mode->vdisplay);
425 return -EINVAL;
426 } else if (fb->width > wb_cfg->sblk->maxlinewidth) {
427 SDE_ERROR("invalid fb w=%d, maxlinewidth=%u\n",
428 fb->width, wb_cfg->sblk->maxlinewidth);
429 return -EINVAL;
430 }
431 }
432
433 return 0;
434}
435
436/**
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700437 * _sde_encoder_phys_wb_update_flush - flush hardware update
Alan Kwongbb27c092016-07-20 16:41:25 -0400438 * @phys_enc: Pointer to physical encoder
439 */
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700440static void _sde_encoder_phys_wb_update_flush(struct sde_encoder_phys *phys_enc)
Alan Kwongbb27c092016-07-20 16:41:25 -0400441{
442 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
443 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
444 struct sde_hw_ctl *hw_ctl = phys_enc->hw_ctl;
445 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
446 u32 flush_mask = 0;
447
448 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
449
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400450 if (!hw_ctl) {
451 SDE_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
452 return;
453 }
454
Alan Kwongbb27c092016-07-20 16:41:25 -0400455 if (hw_ctl->ops.get_bitmask_wb)
456 hw_ctl->ops.get_bitmask_wb(hw_ctl, &flush_mask, hw_wb->idx);
457
458 if (hw_ctl->ops.get_bitmask_cdm && hw_cdm)
459 hw_ctl->ops.get_bitmask_cdm(hw_ctl, &flush_mask, hw_cdm->idx);
460
461 if (hw_ctl->ops.update_pending_flush)
462 hw_ctl->ops.update_pending_flush(hw_ctl, flush_mask);
463
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700464 if (hw_ctl->ops.get_pending_flush)
465 flush_mask = hw_ctl->ops.get_pending_flush(hw_ctl);
466
467 SDE_DEBUG("Pending flush mask for CTL_%d is 0x%x, WB %d\n",
Alan Kwongbb27c092016-07-20 16:41:25 -0400468 hw_ctl->idx - CTL_0, flush_mask, hw_wb->idx - WB_0);
469}
470
471/**
472 * sde_encoder_phys_wb_setup - setup writeback encoder
473 * @phys_enc: Pointer to physical encoder
474 */
475static void sde_encoder_phys_wb_setup(
476 struct sde_encoder_phys *phys_enc)
477{
478 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
479 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
480 struct drm_display_mode mode = phys_enc->cached_mode;
481 struct drm_framebuffer *fb;
482 struct sde_rect *wb_roi = &wb_enc->wb_roi;
483
484 SDE_DEBUG("[mode_set:%d,%d,\"%s\",%d,%d]\n",
485 hw_wb->idx - WB_0, mode.base.id, mode.name,
486 mode.hdisplay, mode.vdisplay);
487
488 memset(wb_roi, 0, sizeof(struct sde_rect));
489
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700490 if (phys_enc->enable_state == SDE_ENC_DISABLING) {
491 fb = wb_enc->fb_disable;
492 wb_roi->w = 0;
493 wb_roi->h = 0;
494 } else {
495 fb = sde_wb_get_output_fb(wb_enc->wb_dev);
496 sde_wb_get_output_roi(wb_enc->wb_dev, wb_roi);
497 }
498
Alan Kwongbb27c092016-07-20 16:41:25 -0400499 if (!fb) {
500 SDE_DEBUG("no output framebuffer\n");
501 return;
502 }
503
504 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
505 fb->width, fb->height);
506
Alan Kwongbb27c092016-07-20 16:41:25 -0400507 if (wb_roi->w == 0 || wb_roi->h == 0) {
508 wb_roi->x = 0;
509 wb_roi->y = 0;
510 wb_roi->w = fb->width;
511 wb_roi->h = fb->height;
512 }
513
514 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi->x, wb_roi->y,
515 wb_roi->w, wb_roi->h);
516
517 wb_enc->wb_fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
518 drm_format_num_planes(fb->pixel_format));
519 if (!wb_enc->wb_fmt) {
520 SDE_ERROR("unsupported output pixel format: %d\n",
521 fb->pixel_format);
522 return;
523 }
524
525 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
526 fb->modifier[0]);
527
Alan Kwong5d324e42016-07-28 22:56:18 -0400528 sde_encoder_phys_wb_set_ot_limit(phys_enc);
529
Alan Kwongbb27c092016-07-20 16:41:25 -0400530 sde_encoder_phys_wb_set_traffic_shaper(phys_enc);
531
532 sde_encoder_phys_setup_cdm(phys_enc, fb, wb_enc->wb_fmt, wb_roi);
533
534 sde_encoder_phys_wb_setup_fb(phys_enc, fb, wb_roi);
535
536 sde_encoder_phys_wb_setup_cdp(phys_enc);
537}
538
539/**
540 * sde_encoder_phys_wb_unregister_irq - unregister writeback interrupt handler
541 * @phys_enc: Pointer to physical encoder
542 */
543static int sde_encoder_phys_wb_unregister_irq(
544 struct sde_encoder_phys *phys_enc)
545{
546 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
547 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
548
549 if (wb_enc->bypass_irqreg)
550 return 0;
551
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400552 sde_core_irq_disable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwonga172ef52016-09-27 00:29:10 -0400553 sde_core_irq_unregister_callback(phys_enc->sde_kms, wb_enc->irq_idx,
554 &wb_enc->irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400555
556 SDE_DEBUG("un-register IRQ for wb %d, irq_idx=%d\n",
557 hw_wb->idx - WB_0,
558 wb_enc->irq_idx);
559
560 return 0;
561}
562
563/**
564 * sde_encoder_phys_wb_done_irq - writeback interrupt handler
565 * @arg: Pointer to writeback encoder
566 * @irq_idx: interrupt index
567 */
568static void sde_encoder_phys_wb_done_irq(void *arg, int irq_idx)
569{
570 struct sde_encoder_phys_wb *wb_enc = arg;
571 struct sde_encoder_phys *phys_enc = &wb_enc->base;
572 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
573
574 SDE_DEBUG("[wb:%d,%u]\n", hw_wb->idx - WB_0,
575 wb_enc->frame_count);
576
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700577 /* don't notify upper layer for internal commit */
578 if (phys_enc->enable_state == SDE_ENC_DISABLING)
579 goto complete;
580
Alan Kwong628d19e2016-10-31 13:50:13 -0400581 if (phys_enc->parent_ops.handle_frame_done)
582 phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
583 phys_enc, SDE_ENCODER_FRAME_EVENT_DONE);
Alan Kwongbb27c092016-07-20 16:41:25 -0400584
Dhaval Patel81e87882016-10-19 21:41:56 -0700585 phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
586 phys_enc);
Alan Kwong628d19e2016-10-31 13:50:13 -0400587
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700588complete:
Alan Kwong628d19e2016-10-31 13:50:13 -0400589 complete_all(&wb_enc->wbdone_complete);
Alan Kwongbb27c092016-07-20 16:41:25 -0400590}
591
592/**
593 * sde_encoder_phys_wb_register_irq - register writeback interrupt handler
594 * @phys_enc: Pointer to physical encoder
595 */
596static int sde_encoder_phys_wb_register_irq(struct sde_encoder_phys *phys_enc)
597{
598 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
599 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwonga172ef52016-09-27 00:29:10 -0400600 struct sde_irq_callback *irq_cb = &wb_enc->irq_cb;
Alan Kwongbb27c092016-07-20 16:41:25 -0400601 enum sde_intr_type intr_type;
602 int ret = 0;
603
604 if (wb_enc->bypass_irqreg)
605 return 0;
606
607 intr_type = sde_encoder_phys_wb_get_intr_type(hw_wb);
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400608 wb_enc->irq_idx = sde_core_irq_idx_lookup(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400609 intr_type, hw_wb->idx);
610 if (wb_enc->irq_idx < 0) {
611 SDE_ERROR(
612 "failed to lookup IRQ index for WB_DONE with wb=%d\n",
613 hw_wb->idx - WB_0);
614 return -EINVAL;
615 }
616
Alan Kwonga172ef52016-09-27 00:29:10 -0400617 irq_cb->func = sde_encoder_phys_wb_done_irq;
618 irq_cb->arg = wb_enc;
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400619 ret = sde_core_irq_register_callback(phys_enc->sde_kms,
Alan Kwonga172ef52016-09-27 00:29:10 -0400620 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400621 if (ret) {
622 SDE_ERROR("failed to register IRQ callback WB_DONE\n");
623 return ret;
624 }
625
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400626 ret = sde_core_irq_enable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwongbb27c092016-07-20 16:41:25 -0400627 if (ret) {
628 SDE_ERROR(
629 "failed to enable IRQ for WB_DONE, wb %d, irq_idx=%d\n",
630 hw_wb->idx - WB_0,
631 wb_enc->irq_idx);
632 wb_enc->irq_idx = -EINVAL;
633
634 /* Unregister callback on IRQ enable failure */
Alan Kwonga172ef52016-09-27 00:29:10 -0400635 sde_core_irq_unregister_callback(phys_enc->sde_kms,
636 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400637 return ret;
638 }
639
640 SDE_DEBUG("registered IRQ for wb %d, irq_idx=%d\n",
641 hw_wb->idx - WB_0,
642 wb_enc->irq_idx);
643
644 return ret;
645}
646
647/**
648 * sde_encoder_phys_wb_mode_set - set display mode
649 * @phys_enc: Pointer to physical encoder
650 * @mode: Pointer to requested display mode
651 * @adj_mode: Pointer to adjusted display mode
652 */
653static void sde_encoder_phys_wb_mode_set(
654 struct sde_encoder_phys *phys_enc,
655 struct drm_display_mode *mode,
656 struct drm_display_mode *adj_mode)
657{
658 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400659 struct sde_rm *rm = &phys_enc->sde_kms->rm;
Alan Kwongbb27c092016-07-20 16:41:25 -0400660 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400661 struct sde_rm_hw_iter iter;
662 int i, instance;
Alan Kwongbb27c092016-07-20 16:41:25 -0400663
664 phys_enc->cached_mode = *adj_mode;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400665 instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400666
667 SDE_DEBUG("[mode_set_cache:%d,%d,\"%s\",%d,%d]\n",
668 hw_wb->idx - WB_0, mode->base.id,
669 mode->name, mode->hdisplay, mode->vdisplay);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400670
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400671 phys_enc->hw_ctl = NULL;
672 phys_enc->hw_cdm = NULL;
673
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400674 /* Retrieve previously allocated HW Resources. CTL shouldn't fail */
675 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
676 for (i = 0; i <= instance; i++) {
677 sde_rm_get_hw(rm, &iter);
678 if (i == instance)
679 phys_enc->hw_ctl = (struct sde_hw_ctl *) iter.hw;
680 }
681
682 if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
683 SDE_ERROR("failed init ctl: %ld\n", PTR_ERR(phys_enc->hw_ctl));
684 phys_enc->hw_ctl = NULL;
685 return;
686 }
687
688 /* CDM is optional */
689 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CDM);
690 for (i = 0; i <= instance; i++) {
691 sde_rm_get_hw(rm, &iter);
692 if (i == instance)
693 phys_enc->hw_cdm = (struct sde_hw_cdm *) iter.hw;
694 }
695
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400696 if (IS_ERR(phys_enc->hw_cdm)) {
697 SDE_ERROR("CDM required but not allocated: %ld\n",
698 PTR_ERR(phys_enc->hw_cdm));
699 phys_enc->hw_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400700 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400701}
702
703/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400704 * sde_encoder_phys_wb_wait_for_commit_done - wait until request is committed
705 * @phys_enc: Pointer to physical encoder
706 */
707static int sde_encoder_phys_wb_wait_for_commit_done(
708 struct sde_encoder_phys *phys_enc)
709{
710 unsigned long ret;
711 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
712 u32 irq_status;
713 u64 wb_time = 0;
714 int rc = 0;
Alan Kwong5a50a4a2017-01-24 19:12:51 -0800715 u32 timeout = max_t(u32, wb_enc->wbdone_timeout, KICKOFF_TIMEOUT_MS);
Alan Kwongbb27c092016-07-20 16:41:25 -0400716
717 /* Return EWOULDBLOCK since we know the wait isn't necessary */
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700718 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
719 SDE_ERROR("encoder already disabled\n");
Alan Kwongbb27c092016-07-20 16:41:25 -0400720 return -EWOULDBLOCK;
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700721 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400722
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400723 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400724
725 ret = wait_for_completion_timeout(&wb_enc->wbdone_complete,
Alan Kwong5a50a4a2017-01-24 19:12:51 -0800726 msecs_to_jiffies(timeout));
Alan Kwongbb27c092016-07-20 16:41:25 -0400727
728 if (!ret) {
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400729 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
730 wb_enc->frame_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400731
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400732 irq_status = sde_core_irq_read(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400733 wb_enc->irq_idx, true);
734 if (irq_status) {
735 SDE_DEBUG("wb:%d done but irq not triggered\n",
736 wb_enc->wb_dev->wb_idx - WB_0);
737 sde_encoder_phys_wb_done_irq(wb_enc, wb_enc->irq_idx);
738 } else {
739 SDE_ERROR("wb:%d kickoff timed out\n",
740 wb_enc->wb_dev->wb_idx - WB_0);
Alan Kwong628d19e2016-10-31 13:50:13 -0400741 if (phys_enc->parent_ops.handle_frame_done)
742 phys_enc->parent_ops.handle_frame_done(
743 phys_enc->parent, phys_enc,
744 SDE_ENCODER_FRAME_EVENT_ERROR);
Alan Kwongbb27c092016-07-20 16:41:25 -0400745 rc = -ETIMEDOUT;
746 }
747 }
748
749 sde_encoder_phys_wb_unregister_irq(phys_enc);
750
751 if (!rc)
752 wb_enc->end_time = ktime_get();
753
754 /* once operation is done, disable traffic shaper */
755 if (wb_enc->wb_cfg.ts_cfg.en && wb_enc->hw_wb &&
756 wb_enc->hw_wb->ops.setup_trafficshaper) {
757 wb_enc->wb_cfg.ts_cfg.en = false;
758 wb_enc->hw_wb->ops.setup_trafficshaper(
759 wb_enc->hw_wb, &wb_enc->wb_cfg);
760 }
761
762 /* remove vote for iommu/clk/bus */
763 wb_enc->frame_count++;
764
765 if (!rc) {
766 wb_time = (u64)ktime_to_us(wb_enc->end_time) -
767 (u64)ktime_to_us(wb_enc->start_time);
768 SDE_DEBUG("wb:%d took %llu us\n",
769 wb_enc->wb_dev->wb_idx - WB_0, wb_time);
770 }
771
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400772 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
773 wb_time);
Alan Kwongbb27c092016-07-20 16:41:25 -0400774
775 return rc;
776}
777
778/**
779 * sde_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
780 * @phys_enc: Pointer to physical encoder
Alan Kwongbb27c092016-07-20 16:41:25 -0400781 */
782static void sde_encoder_phys_wb_prepare_for_kickoff(
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500783 struct sde_encoder_phys *phys_enc)
Alan Kwongbb27c092016-07-20 16:41:25 -0400784{
785 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
786 int ret;
787
788 SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
789 wb_enc->kickoff_count);
790
Alan Kwongbb27c092016-07-20 16:41:25 -0400791 reinit_completion(&wb_enc->wbdone_complete);
792
793 ret = sde_encoder_phys_wb_register_irq(phys_enc);
794 if (ret) {
795 SDE_ERROR("failed to register irq %d\n", ret);
796 return;
797 }
798
799 wb_enc->kickoff_count++;
800
801 /* set OT limit & enable traffic shaper */
802 sde_encoder_phys_wb_setup(phys_enc);
803
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700804 _sde_encoder_phys_wb_update_flush(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400805
806 /* vote for iommu/clk/bus */
807 wb_enc->start_time = ktime_get();
808
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500809 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->kickoff_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400810}
811
812/**
813 * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
814 * @phys_enc: Pointer to physical encoder
815 */
816static void sde_encoder_phys_wb_handle_post_kickoff(
817 struct sde_encoder_phys *phys_enc)
818{
819 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
820
821 SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
822
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400823 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc));
Alan Kwongbb27c092016-07-20 16:41:25 -0400824}
825
826/**
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700827 * _sde_encoder_phys_wb_init_internal_fb - create fb for internal commit
828 * @wb_enc: Pointer to writeback encoder
829 * @pixel_format: DRM pixel format
830 * @width: Desired fb width
831 * @height: Desired fb height
832 */
833static int _sde_encoder_phys_wb_init_internal_fb(
834 struct sde_encoder_phys_wb *wb_enc,
835 uint32_t pixel_format, uint32_t width, uint32_t height)
836{
837 struct drm_device *dev;
838 struct drm_framebuffer *fb;
839 struct drm_mode_fb_cmd2 mode_cmd;
840 uint32_t size;
841 int nplanes, i, ret;
842
843 if (!wb_enc || !wb_enc->base.parent || !wb_enc->base.sde_kms) {
844 SDE_ERROR("invalid params\n");
845 return -EINVAL;
846 }
847
848 dev = wb_enc->base.sde_kms->dev;
849 if (!dev) {
850 SDE_ERROR("invalid dev\n");
851 return -EINVAL;
852 }
853
854 memset(&mode_cmd, 0, sizeof(mode_cmd));
855 mode_cmd.pixel_format = pixel_format;
856 mode_cmd.width = width;
857 mode_cmd.height = height;
858
859 size = sde_format_get_framebuffer_size(pixel_format,
860 mode_cmd.width, mode_cmd.height, 0, 0);
861 if (!size) {
862 SDE_DEBUG("not creating zero size buffer\n");
863 return -EINVAL;
864 }
865
866 /* allocate gem tracking object */
867 nplanes = drm_format_num_planes(pixel_format);
868 if (nplanes > SDE_MAX_PLANES) {
869 SDE_ERROR("requested format has too many planes\n");
870 return -EINVAL;
871 }
872 mutex_lock(&dev->struct_mutex);
873 wb_enc->bo_disable[0] = msm_gem_new(dev, size,
874 MSM_BO_SCANOUT | MSM_BO_WC);
875 mutex_unlock(&dev->struct_mutex);
876
877 if (IS_ERR_OR_NULL(wb_enc->bo_disable[0])) {
878 ret = PTR_ERR(wb_enc->bo_disable[0]);
879 wb_enc->bo_disable[0] = NULL;
880
881 SDE_ERROR("failed to create bo, %d\n", ret);
882 return ret;
883 }
884
885 for (i = 0; i < nplanes; ++i) {
886 wb_enc->bo_disable[i] = wb_enc->bo_disable[0];
887 mode_cmd.pitches[i] = width *
888 drm_format_plane_cpp(pixel_format, i);
889 }
890
891 fb = msm_framebuffer_init(dev, &mode_cmd, wb_enc->bo_disable);
892 if (IS_ERR_OR_NULL(fb)) {
893 ret = PTR_ERR(fb);
894 drm_gem_object_unreference(wb_enc->bo_disable[0]);
895 wb_enc->bo_disable[0] = NULL;
896
897 SDE_ERROR("failed to init fb, %d\n", ret);
898 return ret;
899 }
900
901 /* prepare the backing buffer now so that it's available later */
902 ret = msm_framebuffer_prepare(fb,
903 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE]);
904 if (!ret)
905 wb_enc->fb_disable = fb;
906 return ret;
907}
908
909/**
910 * _sde_encoder_phys_wb_destroy_internal_fb - deconstruct internal fb
911 * @wb_enc: Pointer to writeback encoder
912 */
913static void _sde_encoder_phys_wb_destroy_internal_fb(
914 struct sde_encoder_phys_wb *wb_enc)
915{
916 if (!wb_enc)
917 return;
918
919 if (wb_enc->fb_disable) {
920 drm_framebuffer_unregister_private(wb_enc->fb_disable);
921 drm_framebuffer_remove(wb_enc->fb_disable);
922 wb_enc->fb_disable = NULL;
923 }
924
925 if (wb_enc->bo_disable[0]) {
926 drm_gem_object_unreference(wb_enc->bo_disable[0]);
927 wb_enc->bo_disable[0] = NULL;
928 }
929}
930
931/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400932 * sde_encoder_phys_wb_enable - enable writeback encoder
933 * @phys_enc: Pointer to physical encoder
934 */
935static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
936{
937 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
938 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwong74e8ba32016-09-30 14:25:16 -0400939 struct drm_device *dev;
Alan Kwongbb27c092016-07-20 16:41:25 -0400940 struct drm_connector *connector;
941
942 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
943
Alan Kwong74e8ba32016-09-30 14:25:16 -0400944 if (!wb_enc->base.parent || !wb_enc->base.parent->dev) {
945 SDE_ERROR("invalid drm device\n");
946 return;
947 }
948 dev = wb_enc->base.parent->dev;
949
Alan Kwongbb27c092016-07-20 16:41:25 -0400950 /* find associated writeback connector */
Alan Kwong74e8ba32016-09-30 14:25:16 -0400951 mutex_lock(&dev->mode_config.mutex);
Alan Kwongbb27c092016-07-20 16:41:25 -0400952 drm_for_each_connector(connector, phys_enc->parent->dev) {
953 if (connector->encoder == phys_enc->parent)
954 break;
955 }
Alan Kwong74e8ba32016-09-30 14:25:16 -0400956 mutex_unlock(&dev->mode_config.mutex);
957
Alan Kwongbb27c092016-07-20 16:41:25 -0400958 if (!connector || connector->encoder != phys_enc->parent) {
959 SDE_ERROR("failed to find writeback connector\n");
960 return;
961 }
962 wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
963
964 phys_enc->enable_state = SDE_ENC_ENABLED;
965}
966
967/**
968 * sde_encoder_phys_wb_disable - disable writeback encoder
969 * @phys_enc: Pointer to physical encoder
970 */
971static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
972{
973 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
974 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
975
976 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
977
978 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
979 SDE_ERROR("encoder is already disabled\n");
980 return;
981 }
982
983 if (wb_enc->frame_count != wb_enc->kickoff_count) {
984 SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
985 hw_wb->idx - WB_0, wb_enc->frame_count,
986 wb_enc->kickoff_count);
987 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
988 }
989
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700990 if (!phys_enc->hw_ctl || !phys_enc->parent ||
991 !phys_enc->sde_kms || !wb_enc->fb_disable) {
992 SDE_DEBUG("invalid enc, skipping extra commit\n");
993 goto exit;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400994 }
995
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700996 /* reset h/w before final flush */
997 if (sde_encoder_helper_hw_release(phys_enc, wb_enc->fb_disable))
998 goto exit;
999
1000 phys_enc->enable_state = SDE_ENC_DISABLING;
1001 sde_encoder_phys_wb_prepare_for_kickoff(phys_enc);
1002 if (phys_enc->hw_ctl->ops.trigger_flush)
1003 phys_enc->hw_ctl->ops.trigger_flush(phys_enc->hw_ctl);
1004 sde_encoder_helper_trigger_start(phys_enc);
1005 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
1006exit:
Alan Kwongbb27c092016-07-20 16:41:25 -04001007 phys_enc->enable_state = SDE_ENC_DISABLED;
1008}
1009
1010/**
1011 * sde_encoder_phys_wb_get_hw_resources - get hardware resources
1012 * @phys_enc: Pointer to physical encoder
1013 * @hw_res: Pointer to encoder resources
1014 */
1015static void sde_encoder_phys_wb_get_hw_resources(
1016 struct sde_encoder_phys *phys_enc,
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001017 struct sde_encoder_hw_resources *hw_res,
1018 struct drm_connector_state *conn_state)
Alan Kwongbb27c092016-07-20 16:41:25 -04001019{
1020 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -04001021 struct sde_hw_wb *hw_wb;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001022 struct drm_framebuffer *fb;
1023 const struct sde_format *fmt;
Alan Kwongbb27c092016-07-20 16:41:25 -04001024
Clarence Ip03521982016-08-26 10:49:47 -04001025 if (!phys_enc) {
1026 SDE_ERROR("invalid encoder\n");
1027 return;
1028 }
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001029
1030 fb = sde_wb_connector_state_get_output_fb(conn_state);
1031 if (!fb) {
1032 SDE_ERROR("no output framebuffer\n");
1033 return;
1034 }
1035
1036 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
1037 drm_format_num_planes(fb->pixel_format));
1038 if (!fmt) {
1039 SDE_ERROR("unsupported output pixel format:%d\n",
1040 fb->pixel_format);
1041 return;
1042 }
1043
Clarence Ip03521982016-08-26 10:49:47 -04001044 hw_wb = wb_enc->hw_wb;
Clarence Ip03521982016-08-26 10:49:47 -04001045 hw_res->wbs[hw_wb->idx - WB_0] = phys_enc->intf_mode;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001046 hw_res->needs_cdm = SDE_FORMAT_IS_YUV(fmt);
1047 SDE_DEBUG("[wb:%d] intf_mode=%d needs_cdm=%d\n", hw_wb->idx - WB_0,
1048 hw_res->wbs[hw_wb->idx - WB_0],
1049 hw_res->needs_cdm);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001050}
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001051
Alan Kwongbb27c092016-07-20 16:41:25 -04001052#ifdef CONFIG_DEBUG_FS
1053/**
1054 * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001055 * @phys_enc: Pointer to physical encoder
1056 * @debugfs_root: Pointer to virtual encoder's debugfs_root dir
Alan Kwongbb27c092016-07-20 16:41:25 -04001057 */
1058static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001059 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001060{
1061 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1062
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001063 if (!phys_enc || !wb_enc->hw_wb || !debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001064 return -EINVAL;
1065
Alan Kwongbb27c092016-07-20 16:41:25 -04001066 if (!debugfs_create_u32("wbdone_timeout", 0644,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001067 debugfs_root, &wb_enc->wbdone_timeout)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001068 SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
1069 return -ENOMEM;
1070 }
1071
1072 if (!debugfs_create_u32("bypass_irqreg", 0644,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001073 debugfs_root, &wb_enc->bypass_irqreg)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001074 SDE_ERROR("failed to create debugfs/bypass_irqreg\n");
1075 return -ENOMEM;
1076 }
1077
1078 return 0;
1079}
Alan Kwongbb27c092016-07-20 16:41:25 -04001080#else
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001081static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001082 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001083{
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001084 return 0;
Alan Kwongbb27c092016-07-20 16:41:25 -04001085}
Alan Kwongbb27c092016-07-20 16:41:25 -04001086#endif
1087
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001088static int sde_encoder_phys_wb_late_register(struct sde_encoder_phys *phys_enc,
1089 struct dentry *debugfs_root)
1090{
1091 return sde_encoder_phys_wb_init_debugfs(phys_enc, debugfs_root);
1092}
1093
Alan Kwongbb27c092016-07-20 16:41:25 -04001094/**
1095 * sde_encoder_phys_wb_destroy - destroy writeback encoder
1096 * @phys_enc: Pointer to physical encoder
1097 */
1098static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
1099{
1100 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1101 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
1102
1103 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
1104
1105 if (!phys_enc)
1106 return;
1107
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001108 _sde_encoder_phys_wb_destroy_internal_fb(wb_enc);
1109
Alan Kwongbb27c092016-07-20 16:41:25 -04001110 kfree(wb_enc);
1111}
1112
1113/**
1114 * sde_encoder_phys_wb_init_ops - initialize writeback operations
1115 * @ops: Pointer to encoder operation table
1116 */
1117static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
1118{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001119 ops->late_register = sde_encoder_phys_wb_late_register;
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001120 ops->is_master = sde_encoder_phys_wb_is_master;
Alan Kwongbb27c092016-07-20 16:41:25 -04001121 ops->mode_set = sde_encoder_phys_wb_mode_set;
1122 ops->enable = sde_encoder_phys_wb_enable;
1123 ops->disable = sde_encoder_phys_wb_disable;
1124 ops->destroy = sde_encoder_phys_wb_destroy;
1125 ops->atomic_check = sde_encoder_phys_wb_atomic_check;
1126 ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
Alan Kwongbb27c092016-07-20 16:41:25 -04001127 ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
1128 ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
1129 ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
Clarence Ip110d15c2016-08-16 14:44:41 -04001130 ops->trigger_start = sde_encoder_helper_trigger_start;
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001131 ops->hw_reset = sde_encoder_helper_hw_reset;
Alan Kwongbb27c092016-07-20 16:41:25 -04001132}
1133
1134/**
1135 * sde_encoder_phys_wb_init - initialize writeback encoder
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001136 * @init: Pointer to init info structure with initialization params
Alan Kwongbb27c092016-07-20 16:41:25 -04001137 */
1138struct sde_encoder_phys *sde_encoder_phys_wb_init(
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001139 struct sde_enc_phys_init_params *p)
Alan Kwongbb27c092016-07-20 16:41:25 -04001140{
1141 struct sde_encoder_phys *phys_enc;
1142 struct sde_encoder_phys_wb *wb_enc;
1143 struct sde_hw_mdp *hw_mdp;
1144 int ret = 0;
1145
1146 SDE_DEBUG("\n");
1147
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001148 if (!p || !p->parent) {
1149 SDE_ERROR("invalid params\n");
1150 ret = -EINVAL;
1151 goto fail_alloc;
1152 }
1153
Alan Kwongbb27c092016-07-20 16:41:25 -04001154 wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
1155 if (!wb_enc) {
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001156 SDE_ERROR("failed to allocate wb enc\n");
Alan Kwongbb27c092016-07-20 16:41:25 -04001157 ret = -ENOMEM;
1158 goto fail_alloc;
1159 }
1160 wb_enc->irq_idx = -EINVAL;
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -05001161 wb_enc->wbdone_timeout = KICKOFF_TIMEOUT_MS;
Alan Kwongbb27c092016-07-20 16:41:25 -04001162 init_completion(&wb_enc->wbdone_complete);
1163
1164 phys_enc = &wb_enc->base;
1165
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001166 if (p->sde_kms->vbif[VBIF_NRT]) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001167 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001168 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001169 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001170 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001171 } else {
1172 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001173 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001174 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001175 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001176 }
1177
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001178 hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
Alan Kwongbb27c092016-07-20 16:41:25 -04001179 if (IS_ERR_OR_NULL(hw_mdp)) {
1180 ret = PTR_ERR(hw_mdp);
1181 SDE_ERROR("failed to init hw_top: %d\n", ret);
1182 goto fail_mdp_init;
1183 }
1184 phys_enc->hw_mdptop = hw_mdp;
1185
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001186 /**
1187 * hw_wb resource permanently assigned to this encoder
1188 * Other resources allocated at atomic commit time by use case
1189 */
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001190 if (p->wb_idx != SDE_NONE) {
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001191 struct sde_rm_hw_iter iter;
Alan Kwongbb27c092016-07-20 16:41:25 -04001192
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001193 sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
1194 while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
1195 struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
1196
1197 if (hw_wb->idx == p->wb_idx) {
1198 wb_enc->hw_wb = hw_wb;
1199 break;
1200 }
1201 }
1202
1203 if (!wb_enc->hw_wb) {
1204 ret = -EINVAL;
1205 SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
Alan Kwongbb27c092016-07-20 16:41:25 -04001206 goto fail_wb_init;
1207 }
Alan Kwongbb27c092016-07-20 16:41:25 -04001208 } else {
1209 ret = -EINVAL;
1210 SDE_ERROR("invalid wb_idx\n");
1211 goto fail_wb_check;
1212 }
1213
Alan Kwongbb27c092016-07-20 16:41:25 -04001214 sde_encoder_phys_wb_init_ops(&phys_enc->ops);
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001215 phys_enc->parent = p->parent;
1216 phys_enc->parent_ops = p->parent_ops;
1217 phys_enc->sde_kms = p->sde_kms;
1218 phys_enc->split_role = p->split_role;
Clarence Ip03521982016-08-26 10:49:47 -04001219 phys_enc->intf_mode = INTF_MODE_WB_LINE;
Dhaval Patel81e87882016-10-19 21:41:56 -07001220 phys_enc->intf_idx = p->intf_idx;
Lloyd Atkinson7d070942016-07-26 18:35:12 -04001221 phys_enc->enc_spinlock = p->enc_spinlock;
Alan Kwonga172ef52016-09-27 00:29:10 -04001222 INIT_LIST_HEAD(&wb_enc->irq_cb.list);
Alan Kwongbb27c092016-07-20 16:41:25 -04001223
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001224 /* create internal buffer for disable logic */
1225 if (_sde_encoder_phys_wb_init_internal_fb(wb_enc,
1226 DRM_FORMAT_RGB888, 2, 1)) {
1227 SDE_ERROR("failed to init internal fb\n");
1228 goto fail_wb_init;
1229 }
1230
Alan Kwongbb27c092016-07-20 16:41:25 -04001231 SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
1232 wb_enc->hw_wb->idx - WB_0);
1233
1234 return phys_enc;
1235
Alan Kwongbb27c092016-07-20 16:41:25 -04001236fail_wb_init:
1237fail_wb_check:
Alan Kwongbb27c092016-07-20 16:41:25 -04001238fail_mdp_init:
1239 kfree(wb_enc);
1240fail_alloc:
1241 return ERR_PTR(ret);
1242}
1243