blob: 385c61060ae44956d9856216f9d07d76905269a3 [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.
Alan Kwongbb27c092016-07-20 16:41:25 -040012 */
13
Alan Kwong4c3cf4c2016-09-25 20:08:09 -040014#define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
Alan Kwongbb27c092016-07-20 16:41:25 -040015#include <linux/debugfs.h>
16
17#include "sde_encoder_phys.h"
18#include "sde_formats.h"
19#include "sde_hw_top.h"
20#include "sde_hw_interrupts.h"
Alan Kwongf5dd86c2016-08-09 18:08:17 -040021#include "sde_core_irq.h"
Alan Kwongbb27c092016-07-20 16:41:25 -040022#include "sde_wb.h"
Lloyd Atkinson8772e202016-09-26 17:52:16 -040023#include "sde_vbif.h"
Alan Kwonga62eeb82017-04-19 08:57:55 -070024#include "sde_crtc.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/**
Alan Kwonga62eeb82017-04-19 08:57:55 -0700108 * sde_encoder_phys_wb_set_qos_remap - set QoS remapper for writeback
109 * @phys_enc: Pointer to physical encoder
110 */
111static void sde_encoder_phys_wb_set_qos_remap(
112 struct sde_encoder_phys *phys_enc)
113{
114 struct sde_encoder_phys_wb *wb_enc;
115 struct sde_hw_wb *hw_wb;
116 struct drm_crtc *crtc;
117 struct sde_vbif_set_qos_params qos_params;
118
119 if (!phys_enc || !phys_enc->parent || !phys_enc->parent->crtc) {
120 SDE_ERROR("invalid arguments\n");
121 return;
122 }
123
124 wb_enc = to_sde_encoder_phys_wb(phys_enc);
125 crtc = phys_enc->parent->crtc;
126
127 if (!wb_enc->hw_wb || !wb_enc->hw_wb->caps) {
128 SDE_ERROR("invalid writeback hardware\n");
129 return;
130 }
131
132 hw_wb = wb_enc->hw_wb;
133
134 memset(&qos_params, 0, sizeof(qos_params));
135 qos_params.vbif_idx = hw_wb->caps->vbif_idx;
136 qos_params.xin_id = hw_wb->caps->xin_id;
137 qos_params.clk_ctrl = hw_wb->caps->clk_ctrl;
138 qos_params.num = hw_wb->idx - WB_0;
139 qos_params.is_rt = sde_crtc_get_client_type(crtc) != NRT_CLIENT;
140
141 SDE_DEBUG("[qos_remap] wb:%d vbif:%d xin:%d rt:%d\n",
142 qos_params.num,
143 qos_params.vbif_idx,
144 qos_params.xin_id, qos_params.is_rt);
145
146 sde_vbif_set_qos_remap(phys_enc->sde_kms, &qos_params);
147}
148
149/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400150 * sde_encoder_phys_setup_cdm - setup chroma down block
151 * @phys_enc: Pointer to physical encoder
152 * @fb: Pointer to output framebuffer
153 * @format: Output format
154 */
155void sde_encoder_phys_setup_cdm(struct sde_encoder_phys *phys_enc,
156 struct drm_framebuffer *fb, const struct sde_format *format,
157 struct sde_rect *wb_roi)
158{
159 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
160 struct sde_hw_cdm_cfg *cdm_cfg = &phys_enc->cdm_cfg;
161 int ret;
162
163 if (!SDE_FORMAT_IS_YUV(format)) {
164 SDE_DEBUG("[cdm_disable fmt:%x]\n",
165 format->base.pixel_format);
166
167 if (hw_cdm && hw_cdm->ops.disable)
168 hw_cdm->ops.disable(hw_cdm);
169
170 return;
171 }
172
173 memset(cdm_cfg, 0, sizeof(struct sde_hw_cdm_cfg));
174
175 cdm_cfg->output_width = wb_roi->w;
176 cdm_cfg->output_height = wb_roi->h;
177 cdm_cfg->output_fmt = format;
178 cdm_cfg->output_type = CDM_CDWN_OUTPUT_WB;
abeykun5ed42e12016-08-24 17:09:26 -0400179 cdm_cfg->output_bit_depth = SDE_FORMAT_IS_DX(format) ?
180 CDM_CDWN_OUTPUT_10BIT : CDM_CDWN_OUTPUT_8BIT;
Alan Kwongbb27c092016-07-20 16:41:25 -0400181
182 /* enable 10 bit logic */
183 switch (cdm_cfg->output_fmt->chroma_sample) {
184 case SDE_CHROMA_RGB:
185 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
186 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
187 break;
188 case SDE_CHROMA_H2V1:
189 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
190 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
191 break;
192 case SDE_CHROMA_420:
193 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
194 cdm_cfg->v_cdwn_type = CDM_CDWN_OFFSITE;
195 break;
196 case SDE_CHROMA_H1V2:
197 default:
198 SDE_ERROR("unsupported chroma sampling type\n");
199 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
200 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
201 break;
202 }
203
204 SDE_DEBUG("[cdm_enable:%d,%d,%X,%d,%d,%d,%d]\n",
205 cdm_cfg->output_width,
206 cdm_cfg->output_height,
207 cdm_cfg->output_fmt->base.pixel_format,
208 cdm_cfg->output_type,
209 cdm_cfg->output_bit_depth,
210 cdm_cfg->h_cdwn_type,
211 cdm_cfg->v_cdwn_type);
212
Alan Kwongd6a38602017-03-11 22:26:25 -0800213 if (hw_cdm && hw_cdm->ops.setup_csc_data) {
214 ret = hw_cdm->ops.setup_csc_data(hw_cdm,
215 &sde_encoder_phys_wb_rgb2yuv_601l);
216 if (ret < 0) {
217 SDE_ERROR("failed to setup CSC %d\n", ret);
218 return;
219 }
220 }
221
Alan Kwongbb27c092016-07-20 16:41:25 -0400222 if (hw_cdm && hw_cdm->ops.setup_cdwn) {
223 ret = hw_cdm->ops.setup_cdwn(hw_cdm, cdm_cfg);
224 if (ret < 0) {
225 SDE_ERROR("failed to setup CDM %d\n", ret);
226 return;
227 }
228 }
229
230 if (hw_cdm && hw_cdm->ops.enable) {
231 ret = hw_cdm->ops.enable(hw_cdm, cdm_cfg);
232 if (ret < 0) {
233 SDE_ERROR("failed to enable CDM %d\n", ret);
234 return;
235 }
236 }
237}
238
239/**
240 * sde_encoder_phys_wb_setup_fb - setup output framebuffer
241 * @phys_enc: Pointer to physical encoder
242 * @fb: Pointer to output framebuffer
243 * @wb_roi: Pointer to output region of interest
244 */
245static void sde_encoder_phys_wb_setup_fb(struct sde_encoder_phys *phys_enc,
246 struct drm_framebuffer *fb, struct sde_rect *wb_roi)
247{
248 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -0400249 struct sde_hw_wb *hw_wb;
250 struct sde_hw_wb_cfg *wb_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400251 const struct msm_format *format;
252 int ret, mmu_id;
253
Clarence Ip03521982016-08-26 10:49:47 -0400254 if (!phys_enc) {
255 SDE_ERROR("invalid encoder\n");
256 return;
257 }
258
259 hw_wb = wb_enc->hw_wb;
260 wb_cfg = &wb_enc->wb_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400261 memset(wb_cfg, 0, sizeof(struct sde_hw_wb_cfg));
262
Clarence Ip03521982016-08-26 10:49:47 -0400263 wb_cfg->intf_mode = phys_enc->intf_mode;
Alan Kwongbb27c092016-07-20 16:41:25 -0400264 wb_cfg->is_secure = (fb->flags & DRM_MODE_FB_SECURE) ? true : false;
265 mmu_id = (wb_cfg->is_secure) ?
266 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] :
267 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE];
268
269 SDE_DEBUG("[fb_secure:%d]\n", wb_cfg->is_secure);
270
271 format = msm_framebuffer_format(fb);
Dhaval Patelccbcb3d2016-08-22 11:58:14 -0700272 if (!format) {
273 SDE_DEBUG("invalid format for fb\n");
274 return;
275 }
276
Alan Kwongbb27c092016-07-20 16:41:25 -0400277 wb_cfg->dest.format = sde_get_sde_format_ext(
278 format->pixel_format,
279 fb->modifier,
280 drm_format_num_planes(fb->pixel_format));
281 if (!wb_cfg->dest.format) {
282 /* this error should be detected during atomic_check */
283 SDE_ERROR("failed to get format %x\n", format->pixel_format);
284 return;
285 }
abeykunf1539f72016-08-24 16:08:03 -0400286 wb_cfg->roi = *wb_roi;
Alan Kwongbb27c092016-07-20 16:41:25 -0400287
abeykunf1539f72016-08-24 16:08:03 -0400288 if (hw_wb->caps->features & BIT(SDE_WB_XY_ROI_OFFSET)) {
289 ret = sde_format_populate_layout(mmu_id, fb, &wb_cfg->dest);
290 if (ret) {
291 SDE_DEBUG("failed to populate layout %d\n", ret);
292 return;
293 }
294 wb_cfg->dest.width = fb->width;
295 wb_cfg->dest.height = fb->height;
296 wb_cfg->dest.num_planes = wb_cfg->dest.format->num_planes;
297 } else {
298 ret = sde_format_populate_layout_with_roi(mmu_id, fb, wb_roi,
Alan Kwongbb27c092016-07-20 16:41:25 -0400299 &wb_cfg->dest);
abeykunf1539f72016-08-24 16:08:03 -0400300 if (ret) {
301 /* this error should be detected during atomic_check */
302 SDE_DEBUG("failed to populate layout %d\n", ret);
303 return;
304 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400305 }
306
307 if ((wb_cfg->dest.format->fetch_planes == SDE_PLANE_PLANAR) &&
308 (wb_cfg->dest.format->element[0] == C1_B_Cb))
309 swap(wb_cfg->dest.plane_addr[1], wb_cfg->dest.plane_addr[2]);
310
311 SDE_DEBUG("[fb_offset:%8.8x,%8.8x,%8.8x,%8.8x]\n",
312 wb_cfg->dest.plane_addr[0],
313 wb_cfg->dest.plane_addr[1],
314 wb_cfg->dest.plane_addr[2],
315 wb_cfg->dest.plane_addr[3]);
316 SDE_DEBUG("[fb_stride:%8.8x,%8.8x,%8.8x,%8.8x]\n",
317 wb_cfg->dest.plane_pitch[0],
318 wb_cfg->dest.plane_pitch[1],
319 wb_cfg->dest.plane_pitch[2],
320 wb_cfg->dest.plane_pitch[3]);
321
abeykunf1539f72016-08-24 16:08:03 -0400322 if (hw_wb->ops.setup_roi)
323 hw_wb->ops.setup_roi(hw_wb, wb_cfg);
324
Alan Kwongbb27c092016-07-20 16:41:25 -0400325 if (hw_wb->ops.setup_outformat)
326 hw_wb->ops.setup_outformat(hw_wb, wb_cfg);
327
328 if (hw_wb->ops.setup_outaddress)
329 hw_wb->ops.setup_outaddress(hw_wb, wb_cfg);
330}
331
332/**
333 * sde_encoder_phys_wb_setup_cdp - setup chroma down prefetch block
334 * @phys_enc: Pointer to physical encoder
335 */
336static void sde_encoder_phys_wb_setup_cdp(struct sde_encoder_phys *phys_enc)
337{
338 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
339 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
340 struct sde_hw_intf_cfg *intf_cfg = &wb_enc->intf_cfg;
341
342 memset(intf_cfg, 0, sizeof(struct sde_hw_intf_cfg));
343
344 intf_cfg->intf = SDE_NONE;
345 intf_cfg->wb = hw_wb->idx;
Lloyd Atkinson55987b02016-08-16 16:57:46 -0400346 intf_cfg->mode_3d = sde_encoder_helper_get_3d_blend_mode(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400347
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400348 if (phys_enc->hw_ctl && phys_enc->hw_ctl->ops.setup_intf_cfg)
Alan Kwongbb27c092016-07-20 16:41:25 -0400349 phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl,
350 intf_cfg);
351}
352
353/**
354 * sde_encoder_phys_wb_atomic_check - verify and fixup given atomic states
355 * @phys_enc: Pointer to physical encoder
356 * @crtc_state: Pointer to CRTC atomic state
357 * @conn_state: Pointer to connector atomic state
358 */
359static int sde_encoder_phys_wb_atomic_check(
360 struct sde_encoder_phys *phys_enc,
361 struct drm_crtc_state *crtc_state,
362 struct drm_connector_state *conn_state)
363{
364 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
365 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
366 const struct sde_wb_cfg *wb_cfg = hw_wb->caps;
367 struct drm_framebuffer *fb;
368 const struct sde_format *fmt;
369 struct sde_rect wb_roi;
370 const struct drm_display_mode *mode = &crtc_state->mode;
371 int rc;
372
373 SDE_DEBUG("[atomic_check:%d,%d,\"%s\",%d,%d]\n",
374 hw_wb->idx - WB_0, mode->base.id, mode->name,
375 mode->hdisplay, mode->vdisplay);
376
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400377 if (!conn_state || !conn_state->connector) {
378 SDE_ERROR("invalid connector state\n");
379 return -EINVAL;
380 } else if (conn_state->connector->status !=
381 connector_status_connected) {
382 SDE_ERROR("connector not connected %d\n",
383 conn_state->connector->status);
384 return -EINVAL;
385 }
386
Alan Kwongbb27c092016-07-20 16:41:25 -0400387 memset(&wb_roi, 0, sizeof(struct sde_rect));
388
389 rc = sde_wb_connector_state_get_output_roi(conn_state, &wb_roi);
390 if (rc) {
391 SDE_ERROR("failed to get roi %d\n", rc);
392 return rc;
393 }
394
395 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi.x, wb_roi.y,
396 wb_roi.w, wb_roi.h);
397
398 fb = sde_wb_connector_state_get_output_fb(conn_state);
399 if (!fb) {
400 SDE_ERROR("no output framebuffer\n");
401 return -EINVAL;
402 }
403
404 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
405 fb->width, fb->height);
406
407 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
408 drm_format_num_planes(fb->pixel_format));
409 if (!fmt) {
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400410 SDE_ERROR("unsupported output pixel format:%x\n",
Alan Kwongbb27c092016-07-20 16:41:25 -0400411 fb->pixel_format);
412 return -EINVAL;
413 }
414
415 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
416 fb->modifier[0]);
417
418 if (SDE_FORMAT_IS_YUV(fmt) &&
419 !(wb_cfg->features & BIT(SDE_WB_YUV_CONFIG))) {
420 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
421 return -EINVAL;
422 }
423
424 if (SDE_FORMAT_IS_UBWC(fmt) &&
Clarence Ip32bcb002017-03-13 12:26:44 -0700425 !(wb_cfg->features & BIT(SDE_WB_UBWC))) {
Alan Kwongbb27c092016-07-20 16:41:25 -0400426 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
427 return -EINVAL;
428 }
429
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400430 if (SDE_FORMAT_IS_YUV(fmt) != !!phys_enc->hw_cdm)
431 crtc_state->mode_changed = true;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400432
Alan Kwongbb27c092016-07-20 16:41:25 -0400433 if (wb_roi.w && wb_roi.h) {
434 if (wb_roi.w != mode->hdisplay) {
435 SDE_ERROR("invalid roi w=%d, mode w=%d\n", wb_roi.w,
436 mode->hdisplay);
437 return -EINVAL;
438 } else if (wb_roi.h != mode->vdisplay) {
439 SDE_ERROR("invalid roi h=%d, mode h=%d\n", wb_roi.h,
440 mode->vdisplay);
441 return -EINVAL;
442 } else if (wb_roi.x + wb_roi.w > fb->width) {
443 SDE_ERROR("invalid roi x=%d, w=%d, fb w=%d\n",
444 wb_roi.x, wb_roi.w, fb->width);
445 return -EINVAL;
446 } else if (wb_roi.y + wb_roi.h > fb->height) {
447 SDE_ERROR("invalid roi y=%d, h=%d, fb h=%d\n",
448 wb_roi.y, wb_roi.h, fb->height);
449 return -EINVAL;
450 } else if (wb_roi.w > wb_cfg->sblk->maxlinewidth) {
451 SDE_ERROR("invalid roi w=%d, maxlinewidth=%u\n",
452 wb_roi.w, wb_cfg->sblk->maxlinewidth);
453 return -EINVAL;
454 }
455 } else {
456 if (wb_roi.x || wb_roi.y) {
457 SDE_ERROR("invalid roi x=%d, y=%d\n",
458 wb_roi.x, wb_roi.y);
459 return -EINVAL;
460 } else if (fb->width != mode->hdisplay) {
461 SDE_ERROR("invalid fb w=%d, mode w=%d\n", fb->width,
462 mode->hdisplay);
463 return -EINVAL;
464 } else if (fb->height != mode->vdisplay) {
465 SDE_ERROR("invalid fb h=%d, mode h=%d\n", fb->height,
466 mode->vdisplay);
467 return -EINVAL;
468 } else if (fb->width > wb_cfg->sblk->maxlinewidth) {
469 SDE_ERROR("invalid fb w=%d, maxlinewidth=%u\n",
470 fb->width, wb_cfg->sblk->maxlinewidth);
471 return -EINVAL;
472 }
473 }
474
475 return 0;
476}
477
478/**
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700479 * _sde_encoder_phys_wb_update_flush - flush hardware update
Alan Kwongbb27c092016-07-20 16:41:25 -0400480 * @phys_enc: Pointer to physical encoder
481 */
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700482static void _sde_encoder_phys_wb_update_flush(struct sde_encoder_phys *phys_enc)
Alan Kwongbb27c092016-07-20 16:41:25 -0400483{
484 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
485 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
486 struct sde_hw_ctl *hw_ctl = phys_enc->hw_ctl;
487 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
488 u32 flush_mask = 0;
489
490 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
491
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400492 if (!hw_ctl) {
493 SDE_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
494 return;
495 }
496
Alan Kwongbb27c092016-07-20 16:41:25 -0400497 if (hw_ctl->ops.get_bitmask_wb)
498 hw_ctl->ops.get_bitmask_wb(hw_ctl, &flush_mask, hw_wb->idx);
499
500 if (hw_ctl->ops.get_bitmask_cdm && hw_cdm)
501 hw_ctl->ops.get_bitmask_cdm(hw_ctl, &flush_mask, hw_cdm->idx);
502
503 if (hw_ctl->ops.update_pending_flush)
504 hw_ctl->ops.update_pending_flush(hw_ctl, flush_mask);
505
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700506 if (hw_ctl->ops.get_pending_flush)
507 flush_mask = hw_ctl->ops.get_pending_flush(hw_ctl);
508
509 SDE_DEBUG("Pending flush mask for CTL_%d is 0x%x, WB %d\n",
Alan Kwongbb27c092016-07-20 16:41:25 -0400510 hw_ctl->idx - CTL_0, flush_mask, hw_wb->idx - WB_0);
511}
512
513/**
514 * sde_encoder_phys_wb_setup - setup writeback encoder
515 * @phys_enc: Pointer to physical encoder
516 */
517static void sde_encoder_phys_wb_setup(
518 struct sde_encoder_phys *phys_enc)
519{
520 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
521 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
522 struct drm_display_mode mode = phys_enc->cached_mode;
523 struct drm_framebuffer *fb;
524 struct sde_rect *wb_roi = &wb_enc->wb_roi;
525
526 SDE_DEBUG("[mode_set:%d,%d,\"%s\",%d,%d]\n",
527 hw_wb->idx - WB_0, mode.base.id, mode.name,
528 mode.hdisplay, mode.vdisplay);
529
530 memset(wb_roi, 0, sizeof(struct sde_rect));
531
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700532 if (phys_enc->enable_state == SDE_ENC_DISABLING) {
533 fb = wb_enc->fb_disable;
534 wb_roi->w = 0;
535 wb_roi->h = 0;
536 } else {
537 fb = sde_wb_get_output_fb(wb_enc->wb_dev);
538 sde_wb_get_output_roi(wb_enc->wb_dev, wb_roi);
539 }
540
Alan Kwongbb27c092016-07-20 16:41:25 -0400541 if (!fb) {
542 SDE_DEBUG("no output framebuffer\n");
543 return;
544 }
545
546 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
547 fb->width, fb->height);
548
Alan Kwongbb27c092016-07-20 16:41:25 -0400549 if (wb_roi->w == 0 || wb_roi->h == 0) {
550 wb_roi->x = 0;
551 wb_roi->y = 0;
552 wb_roi->w = fb->width;
553 wb_roi->h = fb->height;
554 }
555
556 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi->x, wb_roi->y,
557 wb_roi->w, wb_roi->h);
558
559 wb_enc->wb_fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
560 drm_format_num_planes(fb->pixel_format));
561 if (!wb_enc->wb_fmt) {
562 SDE_ERROR("unsupported output pixel format: %d\n",
563 fb->pixel_format);
564 return;
565 }
566
567 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
568 fb->modifier[0]);
569
Alan Kwong5d324e42016-07-28 22:56:18 -0400570 sde_encoder_phys_wb_set_ot_limit(phys_enc);
571
Alan Kwongbb27c092016-07-20 16:41:25 -0400572 sde_encoder_phys_wb_set_traffic_shaper(phys_enc);
573
Alan Kwonga62eeb82017-04-19 08:57:55 -0700574 sde_encoder_phys_wb_set_qos_remap(phys_enc);
575
Alan Kwongbb27c092016-07-20 16:41:25 -0400576 sde_encoder_phys_setup_cdm(phys_enc, fb, wb_enc->wb_fmt, wb_roi);
577
578 sde_encoder_phys_wb_setup_fb(phys_enc, fb, wb_roi);
579
580 sde_encoder_phys_wb_setup_cdp(phys_enc);
581}
582
583/**
584 * sde_encoder_phys_wb_unregister_irq - unregister writeback interrupt handler
585 * @phys_enc: Pointer to physical encoder
586 */
587static int sde_encoder_phys_wb_unregister_irq(
588 struct sde_encoder_phys *phys_enc)
589{
590 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
591 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
592
593 if (wb_enc->bypass_irqreg)
594 return 0;
595
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400596 sde_core_irq_disable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwonga172ef52016-09-27 00:29:10 -0400597 sde_core_irq_unregister_callback(phys_enc->sde_kms, wb_enc->irq_idx,
598 &wb_enc->irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400599
600 SDE_DEBUG("un-register IRQ for wb %d, irq_idx=%d\n",
601 hw_wb->idx - WB_0,
602 wb_enc->irq_idx);
603
604 return 0;
605}
606
607/**
608 * sde_encoder_phys_wb_done_irq - writeback interrupt handler
609 * @arg: Pointer to writeback encoder
610 * @irq_idx: interrupt index
611 */
612static void sde_encoder_phys_wb_done_irq(void *arg, int irq_idx)
613{
614 struct sde_encoder_phys_wb *wb_enc = arg;
615 struct sde_encoder_phys *phys_enc = &wb_enc->base;
616 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
617
618 SDE_DEBUG("[wb:%d,%u]\n", hw_wb->idx - WB_0,
619 wb_enc->frame_count);
620
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700621 /* don't notify upper layer for internal commit */
622 if (phys_enc->enable_state == SDE_ENC_DISABLING)
623 goto complete;
624
Alan Kwong628d19e2016-10-31 13:50:13 -0400625 if (phys_enc->parent_ops.handle_frame_done)
626 phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
627 phys_enc, SDE_ENCODER_FRAME_EVENT_DONE);
Alan Kwongbb27c092016-07-20 16:41:25 -0400628
Dhaval Patel81e87882016-10-19 21:41:56 -0700629 phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
630 phys_enc);
Alan Kwong628d19e2016-10-31 13:50:13 -0400631
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700632complete:
Alan Kwong628d19e2016-10-31 13:50:13 -0400633 complete_all(&wb_enc->wbdone_complete);
Alan Kwongbb27c092016-07-20 16:41:25 -0400634}
635
636/**
637 * sde_encoder_phys_wb_register_irq - register writeback interrupt handler
638 * @phys_enc: Pointer to physical encoder
639 */
640static int sde_encoder_phys_wb_register_irq(struct sde_encoder_phys *phys_enc)
641{
642 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
643 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwonga172ef52016-09-27 00:29:10 -0400644 struct sde_irq_callback *irq_cb = &wb_enc->irq_cb;
Alan Kwongbb27c092016-07-20 16:41:25 -0400645 enum sde_intr_type intr_type;
646 int ret = 0;
647
648 if (wb_enc->bypass_irqreg)
649 return 0;
650
651 intr_type = sde_encoder_phys_wb_get_intr_type(hw_wb);
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400652 wb_enc->irq_idx = sde_core_irq_idx_lookup(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400653 intr_type, hw_wb->idx);
654 if (wb_enc->irq_idx < 0) {
655 SDE_ERROR(
656 "failed to lookup IRQ index for WB_DONE with wb=%d\n",
657 hw_wb->idx - WB_0);
658 return -EINVAL;
659 }
660
Alan Kwonga172ef52016-09-27 00:29:10 -0400661 irq_cb->func = sde_encoder_phys_wb_done_irq;
662 irq_cb->arg = wb_enc;
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400663 ret = sde_core_irq_register_callback(phys_enc->sde_kms,
Alan Kwonga172ef52016-09-27 00:29:10 -0400664 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400665 if (ret) {
666 SDE_ERROR("failed to register IRQ callback WB_DONE\n");
667 return ret;
668 }
669
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400670 ret = sde_core_irq_enable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwongbb27c092016-07-20 16:41:25 -0400671 if (ret) {
672 SDE_ERROR(
673 "failed to enable IRQ for WB_DONE, wb %d, irq_idx=%d\n",
674 hw_wb->idx - WB_0,
675 wb_enc->irq_idx);
676 wb_enc->irq_idx = -EINVAL;
677
678 /* Unregister callback on IRQ enable failure */
Alan Kwonga172ef52016-09-27 00:29:10 -0400679 sde_core_irq_unregister_callback(phys_enc->sde_kms,
680 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400681 return ret;
682 }
683
684 SDE_DEBUG("registered IRQ for wb %d, irq_idx=%d\n",
685 hw_wb->idx - WB_0,
686 wb_enc->irq_idx);
687
688 return ret;
689}
690
691/**
692 * sde_encoder_phys_wb_mode_set - set display mode
693 * @phys_enc: Pointer to physical encoder
694 * @mode: Pointer to requested display mode
695 * @adj_mode: Pointer to adjusted display mode
696 */
697static void sde_encoder_phys_wb_mode_set(
698 struct sde_encoder_phys *phys_enc,
699 struct drm_display_mode *mode,
700 struct drm_display_mode *adj_mode)
701{
702 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400703 struct sde_rm *rm = &phys_enc->sde_kms->rm;
Alan Kwongbb27c092016-07-20 16:41:25 -0400704 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400705 struct sde_rm_hw_iter iter;
706 int i, instance;
Alan Kwongbb27c092016-07-20 16:41:25 -0400707
708 phys_enc->cached_mode = *adj_mode;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400709 instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400710
711 SDE_DEBUG("[mode_set_cache:%d,%d,\"%s\",%d,%d]\n",
712 hw_wb->idx - WB_0, mode->base.id,
713 mode->name, mode->hdisplay, mode->vdisplay);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400714
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400715 phys_enc->hw_ctl = NULL;
716 phys_enc->hw_cdm = NULL;
717
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400718 /* Retrieve previously allocated HW Resources. CTL shouldn't fail */
719 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
720 for (i = 0; i <= instance; i++) {
721 sde_rm_get_hw(rm, &iter);
722 if (i == instance)
723 phys_enc->hw_ctl = (struct sde_hw_ctl *) iter.hw;
724 }
725
726 if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
727 SDE_ERROR("failed init ctl: %ld\n", PTR_ERR(phys_enc->hw_ctl));
728 phys_enc->hw_ctl = NULL;
729 return;
730 }
731
732 /* CDM is optional */
733 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CDM);
734 for (i = 0; i <= instance; i++) {
735 sde_rm_get_hw(rm, &iter);
736 if (i == instance)
737 phys_enc->hw_cdm = (struct sde_hw_cdm *) iter.hw;
738 }
739
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400740 if (IS_ERR(phys_enc->hw_cdm)) {
741 SDE_ERROR("CDM required but not allocated: %ld\n",
742 PTR_ERR(phys_enc->hw_cdm));
743 phys_enc->hw_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400744 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400745}
746
747/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400748 * sde_encoder_phys_wb_wait_for_commit_done - wait until request is committed
749 * @phys_enc: Pointer to physical encoder
750 */
751static int sde_encoder_phys_wb_wait_for_commit_done(
752 struct sde_encoder_phys *phys_enc)
753{
754 unsigned long ret;
755 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
756 u32 irq_status;
757 u64 wb_time = 0;
758 int rc = 0;
Alan Kwong5a50a4a2017-01-24 19:12:51 -0800759 u32 timeout = max_t(u32, wb_enc->wbdone_timeout, KICKOFF_TIMEOUT_MS);
Alan Kwongbb27c092016-07-20 16:41:25 -0400760
761 /* Return EWOULDBLOCK since we know the wait isn't necessary */
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700762 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
763 SDE_ERROR("encoder already disabled\n");
Alan Kwongbb27c092016-07-20 16:41:25 -0400764 return -EWOULDBLOCK;
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700765 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400766
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400767 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400768
769 ret = wait_for_completion_timeout(&wb_enc->wbdone_complete,
Alan Kwong5a50a4a2017-01-24 19:12:51 -0800770 msecs_to_jiffies(timeout));
Alan Kwongbb27c092016-07-20 16:41:25 -0400771
772 if (!ret) {
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400773 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
774 wb_enc->frame_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400775
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400776 irq_status = sde_core_irq_read(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400777 wb_enc->irq_idx, true);
778 if (irq_status) {
779 SDE_DEBUG("wb:%d done but irq not triggered\n",
780 wb_enc->wb_dev->wb_idx - WB_0);
781 sde_encoder_phys_wb_done_irq(wb_enc, wb_enc->irq_idx);
782 } else {
783 SDE_ERROR("wb:%d kickoff timed out\n",
784 wb_enc->wb_dev->wb_idx - WB_0);
Alan Kwong628d19e2016-10-31 13:50:13 -0400785 if (phys_enc->parent_ops.handle_frame_done)
786 phys_enc->parent_ops.handle_frame_done(
787 phys_enc->parent, phys_enc,
788 SDE_ENCODER_FRAME_EVENT_ERROR);
Alan Kwongbb27c092016-07-20 16:41:25 -0400789 rc = -ETIMEDOUT;
790 }
791 }
792
793 sde_encoder_phys_wb_unregister_irq(phys_enc);
794
795 if (!rc)
796 wb_enc->end_time = ktime_get();
797
798 /* once operation is done, disable traffic shaper */
799 if (wb_enc->wb_cfg.ts_cfg.en && wb_enc->hw_wb &&
800 wb_enc->hw_wb->ops.setup_trafficshaper) {
801 wb_enc->wb_cfg.ts_cfg.en = false;
802 wb_enc->hw_wb->ops.setup_trafficshaper(
803 wb_enc->hw_wb, &wb_enc->wb_cfg);
804 }
805
806 /* remove vote for iommu/clk/bus */
807 wb_enc->frame_count++;
808
809 if (!rc) {
810 wb_time = (u64)ktime_to_us(wb_enc->end_time) -
811 (u64)ktime_to_us(wb_enc->start_time);
812 SDE_DEBUG("wb:%d took %llu us\n",
813 wb_enc->wb_dev->wb_idx - WB_0, wb_time);
814 }
815
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400816 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
817 wb_time);
Alan Kwongbb27c092016-07-20 16:41:25 -0400818
819 return rc;
820}
821
822/**
823 * sde_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
824 * @phys_enc: Pointer to physical encoder
Alan Kwong4aacd532017-02-04 18:51:33 -0800825 * @params: kickoff parameters
Alan Kwongbb27c092016-07-20 16:41:25 -0400826 */
827static void sde_encoder_phys_wb_prepare_for_kickoff(
Alan Kwong4aacd532017-02-04 18:51:33 -0800828 struct sde_encoder_phys *phys_enc,
829 struct sde_encoder_kickoff_params *params)
Alan Kwongbb27c092016-07-20 16:41:25 -0400830{
831 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
832 int ret;
833
834 SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
835 wb_enc->kickoff_count);
836
Alan Kwongbb27c092016-07-20 16:41:25 -0400837 reinit_completion(&wb_enc->wbdone_complete);
838
839 ret = sde_encoder_phys_wb_register_irq(phys_enc);
840 if (ret) {
841 SDE_ERROR("failed to register irq %d\n", ret);
842 return;
843 }
844
845 wb_enc->kickoff_count++;
846
847 /* set OT limit & enable traffic shaper */
848 sde_encoder_phys_wb_setup(phys_enc);
849
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700850 _sde_encoder_phys_wb_update_flush(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400851
852 /* vote for iommu/clk/bus */
853 wb_enc->start_time = ktime_get();
854
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500855 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->kickoff_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400856}
857
858/**
859 * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
860 * @phys_enc: Pointer to physical encoder
861 */
862static void sde_encoder_phys_wb_handle_post_kickoff(
863 struct sde_encoder_phys *phys_enc)
864{
865 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
866
867 SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
868
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400869 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc));
Alan Kwongbb27c092016-07-20 16:41:25 -0400870}
871
872/**
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700873 * _sde_encoder_phys_wb_init_internal_fb - create fb for internal commit
874 * @wb_enc: Pointer to writeback encoder
875 * @pixel_format: DRM pixel format
876 * @width: Desired fb width
877 * @height: Desired fb height
878 */
879static int _sde_encoder_phys_wb_init_internal_fb(
880 struct sde_encoder_phys_wb *wb_enc,
881 uint32_t pixel_format, uint32_t width, uint32_t height)
882{
883 struct drm_device *dev;
884 struct drm_framebuffer *fb;
885 struct drm_mode_fb_cmd2 mode_cmd;
886 uint32_t size;
887 int nplanes, i, ret;
888
889 if (!wb_enc || !wb_enc->base.parent || !wb_enc->base.sde_kms) {
890 SDE_ERROR("invalid params\n");
891 return -EINVAL;
892 }
893
894 dev = wb_enc->base.sde_kms->dev;
895 if (!dev) {
896 SDE_ERROR("invalid dev\n");
897 return -EINVAL;
898 }
899
900 memset(&mode_cmd, 0, sizeof(mode_cmd));
901 mode_cmd.pixel_format = pixel_format;
902 mode_cmd.width = width;
903 mode_cmd.height = height;
904
905 size = sde_format_get_framebuffer_size(pixel_format,
906 mode_cmd.width, mode_cmd.height, 0, 0);
907 if (!size) {
908 SDE_DEBUG("not creating zero size buffer\n");
909 return -EINVAL;
910 }
911
912 /* allocate gem tracking object */
913 nplanes = drm_format_num_planes(pixel_format);
914 if (nplanes > SDE_MAX_PLANES) {
915 SDE_ERROR("requested format has too many planes\n");
916 return -EINVAL;
917 }
918 mutex_lock(&dev->struct_mutex);
919 wb_enc->bo_disable[0] = msm_gem_new(dev, size,
920 MSM_BO_SCANOUT | MSM_BO_WC);
921 mutex_unlock(&dev->struct_mutex);
922
923 if (IS_ERR_OR_NULL(wb_enc->bo_disable[0])) {
924 ret = PTR_ERR(wb_enc->bo_disable[0]);
925 wb_enc->bo_disable[0] = NULL;
926
927 SDE_ERROR("failed to create bo, %d\n", ret);
928 return ret;
929 }
930
931 for (i = 0; i < nplanes; ++i) {
932 wb_enc->bo_disable[i] = wb_enc->bo_disable[0];
933 mode_cmd.pitches[i] = width *
934 drm_format_plane_cpp(pixel_format, i);
935 }
936
937 fb = msm_framebuffer_init(dev, &mode_cmd, wb_enc->bo_disable);
938 if (IS_ERR_OR_NULL(fb)) {
939 ret = PTR_ERR(fb);
940 drm_gem_object_unreference(wb_enc->bo_disable[0]);
941 wb_enc->bo_disable[0] = NULL;
942
943 SDE_ERROR("failed to init fb, %d\n", ret);
944 return ret;
945 }
946
947 /* prepare the backing buffer now so that it's available later */
948 ret = msm_framebuffer_prepare(fb,
949 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE]);
950 if (!ret)
951 wb_enc->fb_disable = fb;
952 return ret;
953}
954
955/**
956 * _sde_encoder_phys_wb_destroy_internal_fb - deconstruct internal fb
957 * @wb_enc: Pointer to writeback encoder
958 */
959static void _sde_encoder_phys_wb_destroy_internal_fb(
960 struct sde_encoder_phys_wb *wb_enc)
961{
962 if (!wb_enc)
963 return;
964
965 if (wb_enc->fb_disable) {
966 drm_framebuffer_unregister_private(wb_enc->fb_disable);
967 drm_framebuffer_remove(wb_enc->fb_disable);
968 wb_enc->fb_disable = NULL;
969 }
970
971 if (wb_enc->bo_disable[0]) {
972 drm_gem_object_unreference(wb_enc->bo_disable[0]);
973 wb_enc->bo_disable[0] = NULL;
974 }
975}
976
977/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400978 * sde_encoder_phys_wb_enable - enable writeback encoder
979 * @phys_enc: Pointer to physical encoder
980 */
981static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
982{
983 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
984 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwong74e8ba32016-09-30 14:25:16 -0400985 struct drm_device *dev;
Alan Kwongbb27c092016-07-20 16:41:25 -0400986 struct drm_connector *connector;
987
988 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
989
Alan Kwong74e8ba32016-09-30 14:25:16 -0400990 if (!wb_enc->base.parent || !wb_enc->base.parent->dev) {
991 SDE_ERROR("invalid drm device\n");
992 return;
993 }
994 dev = wb_enc->base.parent->dev;
995
Alan Kwongbb27c092016-07-20 16:41:25 -0400996 /* find associated writeback connector */
Clarence Ip05431f32016-11-25 16:39:38 -0500997 connector = phys_enc->connector;
Alan Kwong74e8ba32016-09-30 14:25:16 -0400998
Alan Kwongbb27c092016-07-20 16:41:25 -0400999 if (!connector || connector->encoder != phys_enc->parent) {
1000 SDE_ERROR("failed to find writeback connector\n");
1001 return;
1002 }
1003 wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
1004
1005 phys_enc->enable_state = SDE_ENC_ENABLED;
1006}
1007
1008/**
1009 * sde_encoder_phys_wb_disable - disable writeback encoder
1010 * @phys_enc: Pointer to physical encoder
1011 */
1012static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
1013{
1014 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1015 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
1016
1017 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
1018
1019 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
1020 SDE_ERROR("encoder is already disabled\n");
1021 return;
1022 }
1023
1024 if (wb_enc->frame_count != wb_enc->kickoff_count) {
1025 SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
1026 hw_wb->idx - WB_0, wb_enc->frame_count,
1027 wb_enc->kickoff_count);
1028 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
1029 }
1030
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001031 if (!phys_enc->hw_ctl || !phys_enc->parent ||
1032 !phys_enc->sde_kms || !wb_enc->fb_disable) {
1033 SDE_DEBUG("invalid enc, skipping extra commit\n");
1034 goto exit;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001035 }
1036
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001037 /* reset h/w before final flush */
1038 if (sde_encoder_helper_hw_release(phys_enc, wb_enc->fb_disable))
1039 goto exit;
1040
1041 phys_enc->enable_state = SDE_ENC_DISABLING;
Alan Kwong4aacd532017-02-04 18:51:33 -08001042 sde_encoder_phys_wb_prepare_for_kickoff(phys_enc, NULL);
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001043 if (phys_enc->hw_ctl->ops.trigger_flush)
1044 phys_enc->hw_ctl->ops.trigger_flush(phys_enc->hw_ctl);
1045 sde_encoder_helper_trigger_start(phys_enc);
1046 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
1047exit:
Alan Kwongbb27c092016-07-20 16:41:25 -04001048 phys_enc->enable_state = SDE_ENC_DISABLED;
1049}
1050
1051/**
1052 * sde_encoder_phys_wb_get_hw_resources - get hardware resources
1053 * @phys_enc: Pointer to physical encoder
1054 * @hw_res: Pointer to encoder resources
1055 */
1056static void sde_encoder_phys_wb_get_hw_resources(
1057 struct sde_encoder_phys *phys_enc,
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001058 struct sde_encoder_hw_resources *hw_res,
1059 struct drm_connector_state *conn_state)
Alan Kwongbb27c092016-07-20 16:41:25 -04001060{
1061 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -04001062 struct sde_hw_wb *hw_wb;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001063 struct drm_framebuffer *fb;
1064 const struct sde_format *fmt;
Alan Kwongbb27c092016-07-20 16:41:25 -04001065
Clarence Ip03521982016-08-26 10:49:47 -04001066 if (!phys_enc) {
1067 SDE_ERROR("invalid encoder\n");
1068 return;
1069 }
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001070
1071 fb = sde_wb_connector_state_get_output_fb(conn_state);
1072 if (!fb) {
1073 SDE_ERROR("no output framebuffer\n");
1074 return;
1075 }
1076
1077 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
1078 drm_format_num_planes(fb->pixel_format));
1079 if (!fmt) {
1080 SDE_ERROR("unsupported output pixel format:%d\n",
1081 fb->pixel_format);
1082 return;
1083 }
1084
Clarence Ip03521982016-08-26 10:49:47 -04001085 hw_wb = wb_enc->hw_wb;
Clarence Ip03521982016-08-26 10:49:47 -04001086 hw_res->wbs[hw_wb->idx - WB_0] = phys_enc->intf_mode;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001087 hw_res->needs_cdm = SDE_FORMAT_IS_YUV(fmt);
1088 SDE_DEBUG("[wb:%d] intf_mode=%d needs_cdm=%d\n", hw_wb->idx - WB_0,
1089 hw_res->wbs[hw_wb->idx - WB_0],
1090 hw_res->needs_cdm);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001091}
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001092
Alan Kwongbb27c092016-07-20 16:41:25 -04001093#ifdef CONFIG_DEBUG_FS
1094/**
1095 * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001096 * @phys_enc: Pointer to physical encoder
1097 * @debugfs_root: Pointer to virtual encoder's debugfs_root dir
Alan Kwongbb27c092016-07-20 16:41:25 -04001098 */
1099static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001100 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001101{
1102 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1103
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001104 if (!phys_enc || !wb_enc->hw_wb || !debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001105 return -EINVAL;
1106
Alan Kwongbb27c092016-07-20 16:41:25 -04001107 if (!debugfs_create_u32("wbdone_timeout", 0644,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001108 debugfs_root, &wb_enc->wbdone_timeout)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001109 SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
1110 return -ENOMEM;
1111 }
1112
1113 if (!debugfs_create_u32("bypass_irqreg", 0644,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001114 debugfs_root, &wb_enc->bypass_irqreg)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001115 SDE_ERROR("failed to create debugfs/bypass_irqreg\n");
1116 return -ENOMEM;
1117 }
1118
1119 return 0;
1120}
Alan Kwongbb27c092016-07-20 16:41:25 -04001121#else
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001122static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001123 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001124{
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001125 return 0;
Alan Kwongbb27c092016-07-20 16:41:25 -04001126}
Alan Kwongbb27c092016-07-20 16:41:25 -04001127#endif
1128
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001129static int sde_encoder_phys_wb_late_register(struct sde_encoder_phys *phys_enc,
1130 struct dentry *debugfs_root)
1131{
1132 return sde_encoder_phys_wb_init_debugfs(phys_enc, debugfs_root);
1133}
1134
Alan Kwongbb27c092016-07-20 16:41:25 -04001135/**
1136 * sde_encoder_phys_wb_destroy - destroy writeback encoder
1137 * @phys_enc: Pointer to physical encoder
1138 */
1139static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
1140{
1141 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1142 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
1143
1144 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
1145
1146 if (!phys_enc)
1147 return;
1148
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001149 _sde_encoder_phys_wb_destroy_internal_fb(wb_enc);
1150
Alan Kwongbb27c092016-07-20 16:41:25 -04001151 kfree(wb_enc);
1152}
1153
1154/**
1155 * sde_encoder_phys_wb_init_ops - initialize writeback operations
1156 * @ops: Pointer to encoder operation table
1157 */
1158static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
1159{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001160 ops->late_register = sde_encoder_phys_wb_late_register;
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001161 ops->is_master = sde_encoder_phys_wb_is_master;
Alan Kwongbb27c092016-07-20 16:41:25 -04001162 ops->mode_set = sde_encoder_phys_wb_mode_set;
1163 ops->enable = sde_encoder_phys_wb_enable;
1164 ops->disable = sde_encoder_phys_wb_disable;
1165 ops->destroy = sde_encoder_phys_wb_destroy;
1166 ops->atomic_check = sde_encoder_phys_wb_atomic_check;
1167 ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
Alan Kwongbb27c092016-07-20 16:41:25 -04001168 ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
1169 ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
1170 ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
Clarence Ip110d15c2016-08-16 14:44:41 -04001171 ops->trigger_start = sde_encoder_helper_trigger_start;
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001172 ops->hw_reset = sde_encoder_helper_hw_reset;
Alan Kwongbb27c092016-07-20 16:41:25 -04001173}
1174
1175/**
1176 * sde_encoder_phys_wb_init - initialize writeback encoder
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001177 * @init: Pointer to init info structure with initialization params
Alan Kwongbb27c092016-07-20 16:41:25 -04001178 */
1179struct sde_encoder_phys *sde_encoder_phys_wb_init(
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001180 struct sde_enc_phys_init_params *p)
Alan Kwongbb27c092016-07-20 16:41:25 -04001181{
1182 struct sde_encoder_phys *phys_enc;
1183 struct sde_encoder_phys_wb *wb_enc;
1184 struct sde_hw_mdp *hw_mdp;
1185 int ret = 0;
1186
1187 SDE_DEBUG("\n");
1188
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001189 if (!p || !p->parent) {
1190 SDE_ERROR("invalid params\n");
1191 ret = -EINVAL;
1192 goto fail_alloc;
1193 }
1194
Alan Kwongbb27c092016-07-20 16:41:25 -04001195 wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
1196 if (!wb_enc) {
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001197 SDE_ERROR("failed to allocate wb enc\n");
Alan Kwongbb27c092016-07-20 16:41:25 -04001198 ret = -ENOMEM;
1199 goto fail_alloc;
1200 }
1201 wb_enc->irq_idx = -EINVAL;
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -05001202 wb_enc->wbdone_timeout = KICKOFF_TIMEOUT_MS;
Alan Kwongbb27c092016-07-20 16:41:25 -04001203 init_completion(&wb_enc->wbdone_complete);
1204
1205 phys_enc = &wb_enc->base;
1206
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001207 if (p->sde_kms->vbif[VBIF_NRT]) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001208 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001209 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001210 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001211 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001212 } else {
1213 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001214 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001215 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001216 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001217 }
1218
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001219 hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
Alan Kwongbb27c092016-07-20 16:41:25 -04001220 if (IS_ERR_OR_NULL(hw_mdp)) {
1221 ret = PTR_ERR(hw_mdp);
1222 SDE_ERROR("failed to init hw_top: %d\n", ret);
1223 goto fail_mdp_init;
1224 }
1225 phys_enc->hw_mdptop = hw_mdp;
1226
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001227 /**
1228 * hw_wb resource permanently assigned to this encoder
1229 * Other resources allocated at atomic commit time by use case
1230 */
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001231 if (p->wb_idx != SDE_NONE) {
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001232 struct sde_rm_hw_iter iter;
Alan Kwongbb27c092016-07-20 16:41:25 -04001233
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001234 sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
1235 while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
1236 struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
1237
1238 if (hw_wb->idx == p->wb_idx) {
1239 wb_enc->hw_wb = hw_wb;
1240 break;
1241 }
1242 }
1243
1244 if (!wb_enc->hw_wb) {
1245 ret = -EINVAL;
1246 SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
Alan Kwongbb27c092016-07-20 16:41:25 -04001247 goto fail_wb_init;
1248 }
Alan Kwongbb27c092016-07-20 16:41:25 -04001249 } else {
1250 ret = -EINVAL;
1251 SDE_ERROR("invalid wb_idx\n");
1252 goto fail_wb_check;
1253 }
1254
Alan Kwongbb27c092016-07-20 16:41:25 -04001255 sde_encoder_phys_wb_init_ops(&phys_enc->ops);
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001256 phys_enc->parent = p->parent;
1257 phys_enc->parent_ops = p->parent_ops;
1258 phys_enc->sde_kms = p->sde_kms;
1259 phys_enc->split_role = p->split_role;
Clarence Ip03521982016-08-26 10:49:47 -04001260 phys_enc->intf_mode = INTF_MODE_WB_LINE;
Dhaval Patel81e87882016-10-19 21:41:56 -07001261 phys_enc->intf_idx = p->intf_idx;
Lloyd Atkinson7d070942016-07-26 18:35:12 -04001262 phys_enc->enc_spinlock = p->enc_spinlock;
Alan Kwonga172ef52016-09-27 00:29:10 -04001263 INIT_LIST_HEAD(&wb_enc->irq_cb.list);
Alan Kwongbb27c092016-07-20 16:41:25 -04001264
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001265 /* create internal buffer for disable logic */
1266 if (_sde_encoder_phys_wb_init_internal_fb(wb_enc,
1267 DRM_FORMAT_RGB888, 2, 1)) {
1268 SDE_ERROR("failed to init internal fb\n");
1269 goto fail_wb_init;
1270 }
1271
Alan Kwongbb27c092016-07-20 16:41:25 -04001272 SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
1273 wb_enc->hw_wb->idx - WB_0);
1274
1275 return phys_enc;
1276
Alan Kwongbb27c092016-07-20 16:41:25 -04001277fail_wb_init:
1278fail_wb_check:
Alan Kwongbb27c092016-07-20 16:41:25 -04001279fail_mdp_init:
1280 kfree(wb_enc);
1281fail_alloc:
1282 return ERR_PTR(ret);
1283}
1284