blob: 28a2b16634e668f7787f13cd646930c24eca7363 [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 Kwongbb27c092016-07-20 16:41:25 -040024
Alan Kwongbb27c092016-07-20 16:41:25 -040025#define to_sde_encoder_phys_wb(x) \
26 container_of(x, struct sde_encoder_phys_wb, base)
27
Lloyd Atkinson5d40d312016-09-06 08:34:13 -040028#define WBID(wb_enc) ((wb_enc) ? wb_enc->wb_dev->wb_idx : -1)
Alan Kwongbb27c092016-07-20 16:41:25 -040029
Alan Kwongd6a38602017-03-11 22:26:25 -080030#define TO_S15D16(_x_) ((_x_) << 7)
31
32/**
33 * sde_rgb2yuv_601l - rgb to yuv color space conversion matrix
34 *
35 */
36static struct sde_csc_cfg sde_encoder_phys_wb_rgb2yuv_601l = {
37 {
38 TO_S15D16(0x0083), TO_S15D16(0x0102), TO_S15D16(0x0032),
39 TO_S15D16(0x1fb5), TO_S15D16(0x1f6c), TO_S15D16(0x00e1),
40 TO_S15D16(0x00e1), TO_S15D16(0x1f45), TO_S15D16(0x1fdc)
41 },
42 { 0x00, 0x00, 0x00 },
43 { 0x0040, 0x0200, 0x0200 },
44 { 0x000, 0x3ff, 0x000, 0x3ff, 0x000, 0x3ff },
45 { 0x040, 0x3ac, 0x040, 0x3c0, 0x040, 0x3c0 },
46};
47
Alan Kwongbb27c092016-07-20 16:41:25 -040048/**
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -040049 * sde_encoder_phys_wb_is_master - report wb always as master encoder
50 */
51static bool sde_encoder_phys_wb_is_master(struct sde_encoder_phys *phys_enc)
52{
53 return true;
54}
55
56/**
Alan Kwongbb27c092016-07-20 16:41:25 -040057 * sde_encoder_phys_wb_get_intr_type - get interrupt type based on block mode
58 * @hw_wb: Pointer to h/w writeback driver
59 */
60static enum sde_intr_type sde_encoder_phys_wb_get_intr_type(
61 struct sde_hw_wb *hw_wb)
62{
63 return (hw_wb->caps->features & BIT(SDE_WB_BLOCK_MODE)) ?
64 SDE_IRQ_TYPE_WB_ROT_COMP : SDE_IRQ_TYPE_WB_WFD_COMP;
65}
66
67/**
Alan Kwong5d324e42016-07-28 22:56:18 -040068 * sde_encoder_phys_wb_set_ot_limit - set OT limit for writeback interface
69 * @phys_enc: Pointer to physical encoder
70 */
71static void sde_encoder_phys_wb_set_ot_limit(
72 struct sde_encoder_phys *phys_enc)
73{
74 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
75 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
76 struct sde_vbif_set_ot_params ot_params;
77
78 memset(&ot_params, 0, sizeof(ot_params));
79 ot_params.xin_id = hw_wb->caps->xin_id;
80 ot_params.num = hw_wb->idx - WB_0;
81 ot_params.width = wb_enc->wb_roi.w;
82 ot_params.height = wb_enc->wb_roi.h;
83 ot_params.is_wfd = true;
84 ot_params.frame_rate = phys_enc->cached_mode.vrefresh;
85 ot_params.vbif_idx = hw_wb->caps->vbif_idx;
86 ot_params.clk_ctrl = hw_wb->caps->clk_ctrl;
87 ot_params.rd = false;
88
89 sde_vbif_set_ot_limit(phys_enc->sde_kms, &ot_params);
90}
91
92/**
Alan Kwongbb27c092016-07-20 16:41:25 -040093 * sde_encoder_phys_wb_set_traffic_shaper - set traffic shaper for writeback
94 * @phys_enc: Pointer to physical encoder
95 */
96static void sde_encoder_phys_wb_set_traffic_shaper(
97 struct sde_encoder_phys *phys_enc)
98{
99 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
100 struct sde_hw_wb_cfg *wb_cfg = &wb_enc->wb_cfg;
101
102 /* traffic shaper is only enabled for rotator */
103 wb_cfg->ts_cfg.en = false;
104}
105
106/**
107 * sde_encoder_phys_setup_cdm - setup chroma down block
108 * @phys_enc: Pointer to physical encoder
109 * @fb: Pointer to output framebuffer
110 * @format: Output format
111 */
112void sde_encoder_phys_setup_cdm(struct sde_encoder_phys *phys_enc,
113 struct drm_framebuffer *fb, const struct sde_format *format,
114 struct sde_rect *wb_roi)
115{
116 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
117 struct sde_hw_cdm_cfg *cdm_cfg = &phys_enc->cdm_cfg;
118 int ret;
119
120 if (!SDE_FORMAT_IS_YUV(format)) {
121 SDE_DEBUG("[cdm_disable fmt:%x]\n",
122 format->base.pixel_format);
123
124 if (hw_cdm && hw_cdm->ops.disable)
125 hw_cdm->ops.disable(hw_cdm);
126
127 return;
128 }
129
130 memset(cdm_cfg, 0, sizeof(struct sde_hw_cdm_cfg));
131
132 cdm_cfg->output_width = wb_roi->w;
133 cdm_cfg->output_height = wb_roi->h;
134 cdm_cfg->output_fmt = format;
135 cdm_cfg->output_type = CDM_CDWN_OUTPUT_WB;
abeykun5ed42e12016-08-24 17:09:26 -0400136 cdm_cfg->output_bit_depth = SDE_FORMAT_IS_DX(format) ?
137 CDM_CDWN_OUTPUT_10BIT : CDM_CDWN_OUTPUT_8BIT;
Alan Kwongbb27c092016-07-20 16:41:25 -0400138
139 /* enable 10 bit logic */
140 switch (cdm_cfg->output_fmt->chroma_sample) {
141 case SDE_CHROMA_RGB:
142 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
143 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
144 break;
145 case SDE_CHROMA_H2V1:
146 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
147 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
148 break;
149 case SDE_CHROMA_420:
150 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
151 cdm_cfg->v_cdwn_type = CDM_CDWN_OFFSITE;
152 break;
153 case SDE_CHROMA_H1V2:
154 default:
155 SDE_ERROR("unsupported chroma sampling type\n");
156 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
157 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
158 break;
159 }
160
161 SDE_DEBUG("[cdm_enable:%d,%d,%X,%d,%d,%d,%d]\n",
162 cdm_cfg->output_width,
163 cdm_cfg->output_height,
164 cdm_cfg->output_fmt->base.pixel_format,
165 cdm_cfg->output_type,
166 cdm_cfg->output_bit_depth,
167 cdm_cfg->h_cdwn_type,
168 cdm_cfg->v_cdwn_type);
169
Alan Kwongd6a38602017-03-11 22:26:25 -0800170 if (hw_cdm && hw_cdm->ops.setup_csc_data) {
171 ret = hw_cdm->ops.setup_csc_data(hw_cdm,
172 &sde_encoder_phys_wb_rgb2yuv_601l);
173 if (ret < 0) {
174 SDE_ERROR("failed to setup CSC %d\n", ret);
175 return;
176 }
177 }
178
Alan Kwongbb27c092016-07-20 16:41:25 -0400179 if (hw_cdm && hw_cdm->ops.setup_cdwn) {
180 ret = hw_cdm->ops.setup_cdwn(hw_cdm, cdm_cfg);
181 if (ret < 0) {
182 SDE_ERROR("failed to setup CDM %d\n", ret);
183 return;
184 }
185 }
186
187 if (hw_cdm && hw_cdm->ops.enable) {
188 ret = hw_cdm->ops.enable(hw_cdm, cdm_cfg);
189 if (ret < 0) {
190 SDE_ERROR("failed to enable CDM %d\n", ret);
191 return;
192 }
193 }
194}
195
196/**
197 * sde_encoder_phys_wb_setup_fb - setup output framebuffer
198 * @phys_enc: Pointer to physical encoder
199 * @fb: Pointer to output framebuffer
200 * @wb_roi: Pointer to output region of interest
201 */
202static void sde_encoder_phys_wb_setup_fb(struct sde_encoder_phys *phys_enc,
203 struct drm_framebuffer *fb, struct sde_rect *wb_roi)
204{
205 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -0400206 struct sde_hw_wb *hw_wb;
207 struct sde_hw_wb_cfg *wb_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400208 const struct msm_format *format;
209 int ret, mmu_id;
210
Clarence Ip03521982016-08-26 10:49:47 -0400211 if (!phys_enc) {
212 SDE_ERROR("invalid encoder\n");
213 return;
214 }
215
216 hw_wb = wb_enc->hw_wb;
217 wb_cfg = &wb_enc->wb_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400218 memset(wb_cfg, 0, sizeof(struct sde_hw_wb_cfg));
219
Clarence Ip03521982016-08-26 10:49:47 -0400220 wb_cfg->intf_mode = phys_enc->intf_mode;
Alan Kwongbb27c092016-07-20 16:41:25 -0400221 wb_cfg->is_secure = (fb->flags & DRM_MODE_FB_SECURE) ? true : false;
222 mmu_id = (wb_cfg->is_secure) ?
223 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] :
224 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE];
225
226 SDE_DEBUG("[fb_secure:%d]\n", wb_cfg->is_secure);
227
228 format = msm_framebuffer_format(fb);
Dhaval Patelccbcb3d2016-08-22 11:58:14 -0700229 if (!format) {
230 SDE_DEBUG("invalid format for fb\n");
231 return;
232 }
233
Alan Kwongbb27c092016-07-20 16:41:25 -0400234 wb_cfg->dest.format = sde_get_sde_format_ext(
235 format->pixel_format,
236 fb->modifier,
237 drm_format_num_planes(fb->pixel_format));
238 if (!wb_cfg->dest.format) {
239 /* this error should be detected during atomic_check */
240 SDE_ERROR("failed to get format %x\n", format->pixel_format);
241 return;
242 }
abeykunf1539f72016-08-24 16:08:03 -0400243 wb_cfg->roi = *wb_roi;
Alan Kwongbb27c092016-07-20 16:41:25 -0400244
abeykunf1539f72016-08-24 16:08:03 -0400245 if (hw_wb->caps->features & BIT(SDE_WB_XY_ROI_OFFSET)) {
246 ret = sde_format_populate_layout(mmu_id, fb, &wb_cfg->dest);
247 if (ret) {
248 SDE_DEBUG("failed to populate layout %d\n", ret);
249 return;
250 }
251 wb_cfg->dest.width = fb->width;
252 wb_cfg->dest.height = fb->height;
253 wb_cfg->dest.num_planes = wb_cfg->dest.format->num_planes;
254 } else {
255 ret = sde_format_populate_layout_with_roi(mmu_id, fb, wb_roi,
Alan Kwongbb27c092016-07-20 16:41:25 -0400256 &wb_cfg->dest);
abeykunf1539f72016-08-24 16:08:03 -0400257 if (ret) {
258 /* this error should be detected during atomic_check */
259 SDE_DEBUG("failed to populate layout %d\n", ret);
260 return;
261 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400262 }
263
264 if ((wb_cfg->dest.format->fetch_planes == SDE_PLANE_PLANAR) &&
265 (wb_cfg->dest.format->element[0] == C1_B_Cb))
266 swap(wb_cfg->dest.plane_addr[1], wb_cfg->dest.plane_addr[2]);
267
268 SDE_DEBUG("[fb_offset:%8.8x,%8.8x,%8.8x,%8.8x]\n",
269 wb_cfg->dest.plane_addr[0],
270 wb_cfg->dest.plane_addr[1],
271 wb_cfg->dest.plane_addr[2],
272 wb_cfg->dest.plane_addr[3]);
273 SDE_DEBUG("[fb_stride:%8.8x,%8.8x,%8.8x,%8.8x]\n",
274 wb_cfg->dest.plane_pitch[0],
275 wb_cfg->dest.plane_pitch[1],
276 wb_cfg->dest.plane_pitch[2],
277 wb_cfg->dest.plane_pitch[3]);
278
abeykunf1539f72016-08-24 16:08:03 -0400279 if (hw_wb->ops.setup_roi)
280 hw_wb->ops.setup_roi(hw_wb, wb_cfg);
281
Alan Kwongbb27c092016-07-20 16:41:25 -0400282 if (hw_wb->ops.setup_outformat)
283 hw_wb->ops.setup_outformat(hw_wb, wb_cfg);
284
285 if (hw_wb->ops.setup_outaddress)
286 hw_wb->ops.setup_outaddress(hw_wb, wb_cfg);
287}
288
289/**
290 * sde_encoder_phys_wb_setup_cdp - setup chroma down prefetch block
291 * @phys_enc: Pointer to physical encoder
292 */
293static void sde_encoder_phys_wb_setup_cdp(struct sde_encoder_phys *phys_enc)
294{
295 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
296 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
297 struct sde_hw_intf_cfg *intf_cfg = &wb_enc->intf_cfg;
298
299 memset(intf_cfg, 0, sizeof(struct sde_hw_intf_cfg));
300
301 intf_cfg->intf = SDE_NONE;
302 intf_cfg->wb = hw_wb->idx;
Lloyd Atkinson55987b02016-08-16 16:57:46 -0400303 intf_cfg->mode_3d = sde_encoder_helper_get_3d_blend_mode(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400304
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400305 if (phys_enc->hw_ctl && phys_enc->hw_ctl->ops.setup_intf_cfg)
Alan Kwongbb27c092016-07-20 16:41:25 -0400306 phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl,
307 intf_cfg);
308}
309
310/**
311 * sde_encoder_phys_wb_atomic_check - verify and fixup given atomic states
312 * @phys_enc: Pointer to physical encoder
313 * @crtc_state: Pointer to CRTC atomic state
314 * @conn_state: Pointer to connector atomic state
315 */
316static int sde_encoder_phys_wb_atomic_check(
317 struct sde_encoder_phys *phys_enc,
318 struct drm_crtc_state *crtc_state,
319 struct drm_connector_state *conn_state)
320{
321 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
322 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
323 const struct sde_wb_cfg *wb_cfg = hw_wb->caps;
324 struct drm_framebuffer *fb;
325 const struct sde_format *fmt;
326 struct sde_rect wb_roi;
327 const struct drm_display_mode *mode = &crtc_state->mode;
328 int rc;
329
330 SDE_DEBUG("[atomic_check:%d,%d,\"%s\",%d,%d]\n",
331 hw_wb->idx - WB_0, mode->base.id, mode->name,
332 mode->hdisplay, mode->vdisplay);
333
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400334 if (!conn_state || !conn_state->connector) {
335 SDE_ERROR("invalid connector state\n");
336 return -EINVAL;
337 } else if (conn_state->connector->status !=
338 connector_status_connected) {
339 SDE_ERROR("connector not connected %d\n",
340 conn_state->connector->status);
341 return -EINVAL;
342 }
343
Alan Kwongbb27c092016-07-20 16:41:25 -0400344 memset(&wb_roi, 0, sizeof(struct sde_rect));
345
346 rc = sde_wb_connector_state_get_output_roi(conn_state, &wb_roi);
347 if (rc) {
348 SDE_ERROR("failed to get roi %d\n", rc);
349 return rc;
350 }
351
352 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi.x, wb_roi.y,
353 wb_roi.w, wb_roi.h);
354
355 fb = sde_wb_connector_state_get_output_fb(conn_state);
356 if (!fb) {
357 SDE_ERROR("no output framebuffer\n");
358 return -EINVAL;
359 }
360
361 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
362 fb->width, fb->height);
363
364 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
365 drm_format_num_planes(fb->pixel_format));
366 if (!fmt) {
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400367 SDE_ERROR("unsupported output pixel format:%x\n",
Alan Kwongbb27c092016-07-20 16:41:25 -0400368 fb->pixel_format);
369 return -EINVAL;
370 }
371
372 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
373 fb->modifier[0]);
374
375 if (SDE_FORMAT_IS_YUV(fmt) &&
376 !(wb_cfg->features & BIT(SDE_WB_YUV_CONFIG))) {
377 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
378 return -EINVAL;
379 }
380
381 if (SDE_FORMAT_IS_UBWC(fmt) &&
Clarence Ip32bcb002017-03-13 12:26:44 -0700382 !(wb_cfg->features & BIT(SDE_WB_UBWC))) {
Alan Kwongbb27c092016-07-20 16:41:25 -0400383 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
384 return -EINVAL;
385 }
386
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400387 if (SDE_FORMAT_IS_YUV(fmt) != !!phys_enc->hw_cdm)
388 crtc_state->mode_changed = true;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400389
Alan Kwongbb27c092016-07-20 16:41:25 -0400390 if (wb_roi.w && wb_roi.h) {
391 if (wb_roi.w != mode->hdisplay) {
392 SDE_ERROR("invalid roi w=%d, mode w=%d\n", wb_roi.w,
393 mode->hdisplay);
394 return -EINVAL;
395 } else if (wb_roi.h != mode->vdisplay) {
396 SDE_ERROR("invalid roi h=%d, mode h=%d\n", wb_roi.h,
397 mode->vdisplay);
398 return -EINVAL;
399 } else if (wb_roi.x + wb_roi.w > fb->width) {
400 SDE_ERROR("invalid roi x=%d, w=%d, fb w=%d\n",
401 wb_roi.x, wb_roi.w, fb->width);
402 return -EINVAL;
403 } else if (wb_roi.y + wb_roi.h > fb->height) {
404 SDE_ERROR("invalid roi y=%d, h=%d, fb h=%d\n",
405 wb_roi.y, wb_roi.h, fb->height);
406 return -EINVAL;
407 } else if (wb_roi.w > wb_cfg->sblk->maxlinewidth) {
408 SDE_ERROR("invalid roi w=%d, maxlinewidth=%u\n",
409 wb_roi.w, wb_cfg->sblk->maxlinewidth);
410 return -EINVAL;
411 }
412 } else {
413 if (wb_roi.x || wb_roi.y) {
414 SDE_ERROR("invalid roi x=%d, y=%d\n",
415 wb_roi.x, wb_roi.y);
416 return -EINVAL;
417 } else if (fb->width != mode->hdisplay) {
418 SDE_ERROR("invalid fb w=%d, mode w=%d\n", fb->width,
419 mode->hdisplay);
420 return -EINVAL;
421 } else if (fb->height != mode->vdisplay) {
422 SDE_ERROR("invalid fb h=%d, mode h=%d\n", fb->height,
423 mode->vdisplay);
424 return -EINVAL;
425 } else if (fb->width > wb_cfg->sblk->maxlinewidth) {
426 SDE_ERROR("invalid fb w=%d, maxlinewidth=%u\n",
427 fb->width, wb_cfg->sblk->maxlinewidth);
428 return -EINVAL;
429 }
430 }
431
432 return 0;
433}
434
435/**
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700436 * _sde_encoder_phys_wb_update_flush - flush hardware update
Alan Kwongbb27c092016-07-20 16:41:25 -0400437 * @phys_enc: Pointer to physical encoder
438 */
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700439static void _sde_encoder_phys_wb_update_flush(struct sde_encoder_phys *phys_enc)
Alan Kwongbb27c092016-07-20 16:41:25 -0400440{
441 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
442 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
443 struct sde_hw_ctl *hw_ctl = phys_enc->hw_ctl;
444 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
445 u32 flush_mask = 0;
446
447 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
448
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400449 if (!hw_ctl) {
450 SDE_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
451 return;
452 }
453
Alan Kwongbb27c092016-07-20 16:41:25 -0400454 if (hw_ctl->ops.get_bitmask_wb)
455 hw_ctl->ops.get_bitmask_wb(hw_ctl, &flush_mask, hw_wb->idx);
456
457 if (hw_ctl->ops.get_bitmask_cdm && hw_cdm)
458 hw_ctl->ops.get_bitmask_cdm(hw_ctl, &flush_mask, hw_cdm->idx);
459
460 if (hw_ctl->ops.update_pending_flush)
461 hw_ctl->ops.update_pending_flush(hw_ctl, flush_mask);
462
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700463 if (hw_ctl->ops.get_pending_flush)
464 flush_mask = hw_ctl->ops.get_pending_flush(hw_ctl);
465
466 SDE_DEBUG("Pending flush mask for CTL_%d is 0x%x, WB %d\n",
Alan Kwongbb27c092016-07-20 16:41:25 -0400467 hw_ctl->idx - CTL_0, flush_mask, hw_wb->idx - WB_0);
468}
469
470/**
471 * sde_encoder_phys_wb_setup - setup writeback encoder
472 * @phys_enc: Pointer to physical encoder
473 */
474static void sde_encoder_phys_wb_setup(
475 struct sde_encoder_phys *phys_enc)
476{
477 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
478 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
479 struct drm_display_mode mode = phys_enc->cached_mode;
480 struct drm_framebuffer *fb;
481 struct sde_rect *wb_roi = &wb_enc->wb_roi;
482
483 SDE_DEBUG("[mode_set:%d,%d,\"%s\",%d,%d]\n",
484 hw_wb->idx - WB_0, mode.base.id, mode.name,
485 mode.hdisplay, mode.vdisplay);
486
487 memset(wb_roi, 0, sizeof(struct sde_rect));
488
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700489 if (phys_enc->enable_state == SDE_ENC_DISABLING) {
490 fb = wb_enc->fb_disable;
491 wb_roi->w = 0;
492 wb_roi->h = 0;
493 } else {
494 fb = sde_wb_get_output_fb(wb_enc->wb_dev);
495 sde_wb_get_output_roi(wb_enc->wb_dev, wb_roi);
496 }
497
Alan Kwongbb27c092016-07-20 16:41:25 -0400498 if (!fb) {
499 SDE_DEBUG("no output framebuffer\n");
500 return;
501 }
502
503 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
504 fb->width, fb->height);
505
Alan Kwongbb27c092016-07-20 16:41:25 -0400506 if (wb_roi->w == 0 || wb_roi->h == 0) {
507 wb_roi->x = 0;
508 wb_roi->y = 0;
509 wb_roi->w = fb->width;
510 wb_roi->h = fb->height;
511 }
512
513 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi->x, wb_roi->y,
514 wb_roi->w, wb_roi->h);
515
516 wb_enc->wb_fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
517 drm_format_num_planes(fb->pixel_format));
518 if (!wb_enc->wb_fmt) {
519 SDE_ERROR("unsupported output pixel format: %d\n",
520 fb->pixel_format);
521 return;
522 }
523
524 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
525 fb->modifier[0]);
526
Alan Kwong5d324e42016-07-28 22:56:18 -0400527 sde_encoder_phys_wb_set_ot_limit(phys_enc);
528
Alan Kwongbb27c092016-07-20 16:41:25 -0400529 sde_encoder_phys_wb_set_traffic_shaper(phys_enc);
530
531 sde_encoder_phys_setup_cdm(phys_enc, fb, wb_enc->wb_fmt, wb_roi);
532
533 sde_encoder_phys_wb_setup_fb(phys_enc, fb, wb_roi);
534
535 sde_encoder_phys_wb_setup_cdp(phys_enc);
536}
537
538/**
539 * sde_encoder_phys_wb_unregister_irq - unregister writeback interrupt handler
540 * @phys_enc: Pointer to physical encoder
541 */
542static int sde_encoder_phys_wb_unregister_irq(
543 struct sde_encoder_phys *phys_enc)
544{
545 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
546 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
547
548 if (wb_enc->bypass_irqreg)
549 return 0;
550
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400551 sde_core_irq_disable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwonga172ef52016-09-27 00:29:10 -0400552 sde_core_irq_unregister_callback(phys_enc->sde_kms, wb_enc->irq_idx,
553 &wb_enc->irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400554
555 SDE_DEBUG("un-register IRQ for wb %d, irq_idx=%d\n",
556 hw_wb->idx - WB_0,
557 wb_enc->irq_idx);
558
559 return 0;
560}
561
562/**
563 * sde_encoder_phys_wb_done_irq - writeback interrupt handler
564 * @arg: Pointer to writeback encoder
565 * @irq_idx: interrupt index
566 */
567static void sde_encoder_phys_wb_done_irq(void *arg, int irq_idx)
568{
569 struct sde_encoder_phys_wb *wb_enc = arg;
570 struct sde_encoder_phys *phys_enc = &wb_enc->base;
571 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
572
573 SDE_DEBUG("[wb:%d,%u]\n", hw_wb->idx - WB_0,
574 wb_enc->frame_count);
575
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700576 /* don't notify upper layer for internal commit */
577 if (phys_enc->enable_state == SDE_ENC_DISABLING)
578 goto complete;
579
Alan Kwong628d19e2016-10-31 13:50:13 -0400580 if (phys_enc->parent_ops.handle_frame_done)
581 phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
582 phys_enc, SDE_ENCODER_FRAME_EVENT_DONE);
Alan Kwongbb27c092016-07-20 16:41:25 -0400583
Dhaval Patel81e87882016-10-19 21:41:56 -0700584 phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
585 phys_enc);
Alan Kwong628d19e2016-10-31 13:50:13 -0400586
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700587complete:
Alan Kwong628d19e2016-10-31 13:50:13 -0400588 complete_all(&wb_enc->wbdone_complete);
Alan Kwongbb27c092016-07-20 16:41:25 -0400589}
590
591/**
592 * sde_encoder_phys_wb_register_irq - register writeback interrupt handler
593 * @phys_enc: Pointer to physical encoder
594 */
595static int sde_encoder_phys_wb_register_irq(struct sde_encoder_phys *phys_enc)
596{
597 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
598 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwonga172ef52016-09-27 00:29:10 -0400599 struct sde_irq_callback *irq_cb = &wb_enc->irq_cb;
Alan Kwongbb27c092016-07-20 16:41:25 -0400600 enum sde_intr_type intr_type;
601 int ret = 0;
602
603 if (wb_enc->bypass_irqreg)
604 return 0;
605
606 intr_type = sde_encoder_phys_wb_get_intr_type(hw_wb);
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400607 wb_enc->irq_idx = sde_core_irq_idx_lookup(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400608 intr_type, hw_wb->idx);
609 if (wb_enc->irq_idx < 0) {
610 SDE_ERROR(
611 "failed to lookup IRQ index for WB_DONE with wb=%d\n",
612 hw_wb->idx - WB_0);
613 return -EINVAL;
614 }
615
Alan Kwonga172ef52016-09-27 00:29:10 -0400616 irq_cb->func = sde_encoder_phys_wb_done_irq;
617 irq_cb->arg = wb_enc;
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400618 ret = sde_core_irq_register_callback(phys_enc->sde_kms,
Alan Kwonga172ef52016-09-27 00:29:10 -0400619 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400620 if (ret) {
621 SDE_ERROR("failed to register IRQ callback WB_DONE\n");
622 return ret;
623 }
624
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400625 ret = sde_core_irq_enable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwongbb27c092016-07-20 16:41:25 -0400626 if (ret) {
627 SDE_ERROR(
628 "failed to enable IRQ for WB_DONE, wb %d, irq_idx=%d\n",
629 hw_wb->idx - WB_0,
630 wb_enc->irq_idx);
631 wb_enc->irq_idx = -EINVAL;
632
633 /* Unregister callback on IRQ enable failure */
Alan Kwonga172ef52016-09-27 00:29:10 -0400634 sde_core_irq_unregister_callback(phys_enc->sde_kms,
635 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400636 return ret;
637 }
638
639 SDE_DEBUG("registered IRQ for wb %d, irq_idx=%d\n",
640 hw_wb->idx - WB_0,
641 wb_enc->irq_idx);
642
643 return ret;
644}
645
646/**
647 * sde_encoder_phys_wb_mode_set - set display mode
648 * @phys_enc: Pointer to physical encoder
649 * @mode: Pointer to requested display mode
650 * @adj_mode: Pointer to adjusted display mode
651 */
652static void sde_encoder_phys_wb_mode_set(
653 struct sde_encoder_phys *phys_enc,
654 struct drm_display_mode *mode,
655 struct drm_display_mode *adj_mode)
656{
657 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400658 struct sde_rm *rm = &phys_enc->sde_kms->rm;
Alan Kwongbb27c092016-07-20 16:41:25 -0400659 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400660 struct sde_rm_hw_iter iter;
661 int i, instance;
Alan Kwongbb27c092016-07-20 16:41:25 -0400662
663 phys_enc->cached_mode = *adj_mode;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400664 instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400665
666 SDE_DEBUG("[mode_set_cache:%d,%d,\"%s\",%d,%d]\n",
667 hw_wb->idx - WB_0, mode->base.id,
668 mode->name, mode->hdisplay, mode->vdisplay);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400669
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400670 phys_enc->hw_ctl = NULL;
671 phys_enc->hw_cdm = NULL;
672
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400673 /* Retrieve previously allocated HW Resources. CTL shouldn't fail */
674 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
675 for (i = 0; i <= instance; i++) {
676 sde_rm_get_hw(rm, &iter);
677 if (i == instance)
678 phys_enc->hw_ctl = (struct sde_hw_ctl *) iter.hw;
679 }
680
681 if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
682 SDE_ERROR("failed init ctl: %ld\n", PTR_ERR(phys_enc->hw_ctl));
683 phys_enc->hw_ctl = NULL;
684 return;
685 }
686
687 /* CDM is optional */
688 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CDM);
689 for (i = 0; i <= instance; i++) {
690 sde_rm_get_hw(rm, &iter);
691 if (i == instance)
692 phys_enc->hw_cdm = (struct sde_hw_cdm *) iter.hw;
693 }
694
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400695 if (IS_ERR(phys_enc->hw_cdm)) {
696 SDE_ERROR("CDM required but not allocated: %ld\n",
697 PTR_ERR(phys_enc->hw_cdm));
698 phys_enc->hw_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400699 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400700}
701
702/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400703 * sde_encoder_phys_wb_wait_for_commit_done - wait until request is committed
704 * @phys_enc: Pointer to physical encoder
705 */
706static int sde_encoder_phys_wb_wait_for_commit_done(
707 struct sde_encoder_phys *phys_enc)
708{
709 unsigned long ret;
710 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
711 u32 irq_status;
712 u64 wb_time = 0;
713 int rc = 0;
Alan Kwong5a50a4a2017-01-24 19:12:51 -0800714 u32 timeout = max_t(u32, wb_enc->wbdone_timeout, KICKOFF_TIMEOUT_MS);
Alan Kwongbb27c092016-07-20 16:41:25 -0400715
716 /* Return EWOULDBLOCK since we know the wait isn't necessary */
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700717 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
718 SDE_ERROR("encoder already disabled\n");
Alan Kwongbb27c092016-07-20 16:41:25 -0400719 return -EWOULDBLOCK;
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700720 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400721
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400722 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400723
724 ret = wait_for_completion_timeout(&wb_enc->wbdone_complete,
Alan Kwong5a50a4a2017-01-24 19:12:51 -0800725 msecs_to_jiffies(timeout));
Alan Kwongbb27c092016-07-20 16:41:25 -0400726
727 if (!ret) {
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400728 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
729 wb_enc->frame_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400730
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400731 irq_status = sde_core_irq_read(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400732 wb_enc->irq_idx, true);
733 if (irq_status) {
734 SDE_DEBUG("wb:%d done but irq not triggered\n",
735 wb_enc->wb_dev->wb_idx - WB_0);
736 sde_encoder_phys_wb_done_irq(wb_enc, wb_enc->irq_idx);
737 } else {
738 SDE_ERROR("wb:%d kickoff timed out\n",
739 wb_enc->wb_dev->wb_idx - WB_0);
Alan Kwong628d19e2016-10-31 13:50:13 -0400740 if (phys_enc->parent_ops.handle_frame_done)
741 phys_enc->parent_ops.handle_frame_done(
742 phys_enc->parent, phys_enc,
743 SDE_ENCODER_FRAME_EVENT_ERROR);
Alan Kwongbb27c092016-07-20 16:41:25 -0400744 rc = -ETIMEDOUT;
745 }
746 }
747
748 sde_encoder_phys_wb_unregister_irq(phys_enc);
749
750 if (!rc)
751 wb_enc->end_time = ktime_get();
752
753 /* once operation is done, disable traffic shaper */
754 if (wb_enc->wb_cfg.ts_cfg.en && wb_enc->hw_wb &&
755 wb_enc->hw_wb->ops.setup_trafficshaper) {
756 wb_enc->wb_cfg.ts_cfg.en = false;
757 wb_enc->hw_wb->ops.setup_trafficshaper(
758 wb_enc->hw_wb, &wb_enc->wb_cfg);
759 }
760
761 /* remove vote for iommu/clk/bus */
762 wb_enc->frame_count++;
763
764 if (!rc) {
765 wb_time = (u64)ktime_to_us(wb_enc->end_time) -
766 (u64)ktime_to_us(wb_enc->start_time);
767 SDE_DEBUG("wb:%d took %llu us\n",
768 wb_enc->wb_dev->wb_idx - WB_0, wb_time);
769 }
770
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400771 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
772 wb_time);
Alan Kwongbb27c092016-07-20 16:41:25 -0400773
774 return rc;
775}
776
777/**
778 * sde_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
779 * @phys_enc: Pointer to physical encoder
Alan Kwong4aacd532017-02-04 18:51:33 -0800780 * @params: kickoff parameters
Alan Kwongbb27c092016-07-20 16:41:25 -0400781 */
782static void sde_encoder_phys_wb_prepare_for_kickoff(
Alan Kwong4aacd532017-02-04 18:51:33 -0800783 struct sde_encoder_phys *phys_enc,
784 struct sde_encoder_kickoff_params *params)
Alan Kwongbb27c092016-07-20 16:41:25 -0400785{
786 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
787 int ret;
788
789 SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
790 wb_enc->kickoff_count);
791
Alan Kwongbb27c092016-07-20 16:41:25 -0400792 reinit_completion(&wb_enc->wbdone_complete);
793
794 ret = sde_encoder_phys_wb_register_irq(phys_enc);
795 if (ret) {
796 SDE_ERROR("failed to register irq %d\n", ret);
797 return;
798 }
799
800 wb_enc->kickoff_count++;
801
802 /* set OT limit & enable traffic shaper */
803 sde_encoder_phys_wb_setup(phys_enc);
804
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700805 _sde_encoder_phys_wb_update_flush(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400806
807 /* vote for iommu/clk/bus */
808 wb_enc->start_time = ktime_get();
809
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500810 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->kickoff_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400811}
812
813/**
814 * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
815 * @phys_enc: Pointer to physical encoder
816 */
817static void sde_encoder_phys_wb_handle_post_kickoff(
818 struct sde_encoder_phys *phys_enc)
819{
820 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
821
822 SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
823
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400824 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc));
Alan Kwongbb27c092016-07-20 16:41:25 -0400825}
826
827/**
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700828 * _sde_encoder_phys_wb_init_internal_fb - create fb for internal commit
829 * @wb_enc: Pointer to writeback encoder
830 * @pixel_format: DRM pixel format
831 * @width: Desired fb width
832 * @height: Desired fb height
833 */
834static int _sde_encoder_phys_wb_init_internal_fb(
835 struct sde_encoder_phys_wb *wb_enc,
836 uint32_t pixel_format, uint32_t width, uint32_t height)
837{
838 struct drm_device *dev;
839 struct drm_framebuffer *fb;
840 struct drm_mode_fb_cmd2 mode_cmd;
841 uint32_t size;
842 int nplanes, i, ret;
843
844 if (!wb_enc || !wb_enc->base.parent || !wb_enc->base.sde_kms) {
845 SDE_ERROR("invalid params\n");
846 return -EINVAL;
847 }
848
849 dev = wb_enc->base.sde_kms->dev;
850 if (!dev) {
851 SDE_ERROR("invalid dev\n");
852 return -EINVAL;
853 }
854
855 memset(&mode_cmd, 0, sizeof(mode_cmd));
856 mode_cmd.pixel_format = pixel_format;
857 mode_cmd.width = width;
858 mode_cmd.height = height;
859
860 size = sde_format_get_framebuffer_size(pixel_format,
861 mode_cmd.width, mode_cmd.height, 0, 0);
862 if (!size) {
863 SDE_DEBUG("not creating zero size buffer\n");
864 return -EINVAL;
865 }
866
867 /* allocate gem tracking object */
868 nplanes = drm_format_num_planes(pixel_format);
869 if (nplanes > SDE_MAX_PLANES) {
870 SDE_ERROR("requested format has too many planes\n");
871 return -EINVAL;
872 }
873 mutex_lock(&dev->struct_mutex);
874 wb_enc->bo_disable[0] = msm_gem_new(dev, size,
875 MSM_BO_SCANOUT | MSM_BO_WC);
876 mutex_unlock(&dev->struct_mutex);
877
878 if (IS_ERR_OR_NULL(wb_enc->bo_disable[0])) {
879 ret = PTR_ERR(wb_enc->bo_disable[0]);
880 wb_enc->bo_disable[0] = NULL;
881
882 SDE_ERROR("failed to create bo, %d\n", ret);
883 return ret;
884 }
885
886 for (i = 0; i < nplanes; ++i) {
887 wb_enc->bo_disable[i] = wb_enc->bo_disable[0];
888 mode_cmd.pitches[i] = width *
889 drm_format_plane_cpp(pixel_format, i);
890 }
891
892 fb = msm_framebuffer_init(dev, &mode_cmd, wb_enc->bo_disable);
893 if (IS_ERR_OR_NULL(fb)) {
894 ret = PTR_ERR(fb);
895 drm_gem_object_unreference(wb_enc->bo_disable[0]);
896 wb_enc->bo_disable[0] = NULL;
897
898 SDE_ERROR("failed to init fb, %d\n", ret);
899 return ret;
900 }
901
902 /* prepare the backing buffer now so that it's available later */
903 ret = msm_framebuffer_prepare(fb,
904 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE]);
905 if (!ret)
906 wb_enc->fb_disable = fb;
907 return ret;
908}
909
910/**
911 * _sde_encoder_phys_wb_destroy_internal_fb - deconstruct internal fb
912 * @wb_enc: Pointer to writeback encoder
913 */
914static void _sde_encoder_phys_wb_destroy_internal_fb(
915 struct sde_encoder_phys_wb *wb_enc)
916{
917 if (!wb_enc)
918 return;
919
920 if (wb_enc->fb_disable) {
921 drm_framebuffer_unregister_private(wb_enc->fb_disable);
922 drm_framebuffer_remove(wb_enc->fb_disable);
923 wb_enc->fb_disable = NULL;
924 }
925
926 if (wb_enc->bo_disable[0]) {
927 drm_gem_object_unreference(wb_enc->bo_disable[0]);
928 wb_enc->bo_disable[0] = NULL;
929 }
930}
931
932/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400933 * sde_encoder_phys_wb_enable - enable writeback encoder
934 * @phys_enc: Pointer to physical encoder
935 */
936static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
937{
938 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
939 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwong74e8ba32016-09-30 14:25:16 -0400940 struct drm_device *dev;
Alan Kwongbb27c092016-07-20 16:41:25 -0400941 struct drm_connector *connector;
942
943 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
944
Alan Kwong74e8ba32016-09-30 14:25:16 -0400945 if (!wb_enc->base.parent || !wb_enc->base.parent->dev) {
946 SDE_ERROR("invalid drm device\n");
947 return;
948 }
949 dev = wb_enc->base.parent->dev;
950
Alan Kwongbb27c092016-07-20 16:41:25 -0400951 /* find associated writeback connector */
Clarence Ip05431f32016-11-25 16:39:38 -0500952 connector = phys_enc->connector;
Alan Kwong74e8ba32016-09-30 14:25:16 -0400953
Alan Kwongbb27c092016-07-20 16:41:25 -0400954 if (!connector || connector->encoder != phys_enc->parent) {
955 SDE_ERROR("failed to find writeback connector\n");
956 return;
957 }
958 wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
959
960 phys_enc->enable_state = SDE_ENC_ENABLED;
961}
962
963/**
964 * sde_encoder_phys_wb_disable - disable writeback encoder
965 * @phys_enc: Pointer to physical encoder
966 */
967static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
968{
969 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
970 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
971
972 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
973
974 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
975 SDE_ERROR("encoder is already disabled\n");
976 return;
977 }
978
979 if (wb_enc->frame_count != wb_enc->kickoff_count) {
980 SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
981 hw_wb->idx - WB_0, wb_enc->frame_count,
982 wb_enc->kickoff_count);
983 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
984 }
985
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700986 if (!phys_enc->hw_ctl || !phys_enc->parent ||
987 !phys_enc->sde_kms || !wb_enc->fb_disable) {
988 SDE_DEBUG("invalid enc, skipping extra commit\n");
989 goto exit;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400990 }
991
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700992 /* reset h/w before final flush */
993 if (sde_encoder_helper_hw_release(phys_enc, wb_enc->fb_disable))
994 goto exit;
995
996 phys_enc->enable_state = SDE_ENC_DISABLING;
Alan Kwong4aacd532017-02-04 18:51:33 -0800997 sde_encoder_phys_wb_prepare_for_kickoff(phys_enc, NULL);
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700998 if (phys_enc->hw_ctl->ops.trigger_flush)
999 phys_enc->hw_ctl->ops.trigger_flush(phys_enc->hw_ctl);
1000 sde_encoder_helper_trigger_start(phys_enc);
1001 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
1002exit:
Alan Kwongbb27c092016-07-20 16:41:25 -04001003 phys_enc->enable_state = SDE_ENC_DISABLED;
1004}
1005
1006/**
1007 * sde_encoder_phys_wb_get_hw_resources - get hardware resources
1008 * @phys_enc: Pointer to physical encoder
1009 * @hw_res: Pointer to encoder resources
1010 */
1011static void sde_encoder_phys_wb_get_hw_resources(
1012 struct sde_encoder_phys *phys_enc,
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001013 struct sde_encoder_hw_resources *hw_res,
1014 struct drm_connector_state *conn_state)
Alan Kwongbb27c092016-07-20 16:41:25 -04001015{
1016 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -04001017 struct sde_hw_wb *hw_wb;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001018 struct drm_framebuffer *fb;
1019 const struct sde_format *fmt;
Alan Kwongbb27c092016-07-20 16:41:25 -04001020
Clarence Ip03521982016-08-26 10:49:47 -04001021 if (!phys_enc) {
1022 SDE_ERROR("invalid encoder\n");
1023 return;
1024 }
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001025
1026 fb = sde_wb_connector_state_get_output_fb(conn_state);
1027 if (!fb) {
1028 SDE_ERROR("no output framebuffer\n");
1029 return;
1030 }
1031
1032 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
1033 drm_format_num_planes(fb->pixel_format));
1034 if (!fmt) {
1035 SDE_ERROR("unsupported output pixel format:%d\n",
1036 fb->pixel_format);
1037 return;
1038 }
1039
Clarence Ip03521982016-08-26 10:49:47 -04001040 hw_wb = wb_enc->hw_wb;
Clarence Ip03521982016-08-26 10:49:47 -04001041 hw_res->wbs[hw_wb->idx - WB_0] = phys_enc->intf_mode;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001042 hw_res->needs_cdm = SDE_FORMAT_IS_YUV(fmt);
1043 SDE_DEBUG("[wb:%d] intf_mode=%d needs_cdm=%d\n", hw_wb->idx - WB_0,
1044 hw_res->wbs[hw_wb->idx - WB_0],
1045 hw_res->needs_cdm);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001046}
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001047
Alan Kwongbb27c092016-07-20 16:41:25 -04001048#ifdef CONFIG_DEBUG_FS
1049/**
1050 * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001051 * @phys_enc: Pointer to physical encoder
1052 * @debugfs_root: Pointer to virtual encoder's debugfs_root dir
Alan Kwongbb27c092016-07-20 16:41:25 -04001053 */
1054static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001055 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001056{
1057 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1058
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001059 if (!phys_enc || !wb_enc->hw_wb || !debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001060 return -EINVAL;
1061
Alan Kwongbb27c092016-07-20 16:41:25 -04001062 if (!debugfs_create_u32("wbdone_timeout", 0644,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001063 debugfs_root, &wb_enc->wbdone_timeout)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001064 SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
1065 return -ENOMEM;
1066 }
1067
1068 if (!debugfs_create_u32("bypass_irqreg", 0644,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001069 debugfs_root, &wb_enc->bypass_irqreg)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001070 SDE_ERROR("failed to create debugfs/bypass_irqreg\n");
1071 return -ENOMEM;
1072 }
1073
1074 return 0;
1075}
Alan Kwongbb27c092016-07-20 16:41:25 -04001076#else
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001077static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001078 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001079{
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001080 return 0;
Alan Kwongbb27c092016-07-20 16:41:25 -04001081}
Alan Kwongbb27c092016-07-20 16:41:25 -04001082#endif
1083
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001084static int sde_encoder_phys_wb_late_register(struct sde_encoder_phys *phys_enc,
1085 struct dentry *debugfs_root)
1086{
1087 return sde_encoder_phys_wb_init_debugfs(phys_enc, debugfs_root);
1088}
1089
Alan Kwongbb27c092016-07-20 16:41:25 -04001090/**
1091 * sde_encoder_phys_wb_destroy - destroy writeback encoder
1092 * @phys_enc: Pointer to physical encoder
1093 */
1094static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
1095{
1096 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1097 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
1098
1099 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
1100
1101 if (!phys_enc)
1102 return;
1103
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001104 _sde_encoder_phys_wb_destroy_internal_fb(wb_enc);
1105
Alan Kwongbb27c092016-07-20 16:41:25 -04001106 kfree(wb_enc);
1107}
1108
1109/**
1110 * sde_encoder_phys_wb_init_ops - initialize writeback operations
1111 * @ops: Pointer to encoder operation table
1112 */
1113static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
1114{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001115 ops->late_register = sde_encoder_phys_wb_late_register;
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001116 ops->is_master = sde_encoder_phys_wb_is_master;
Alan Kwongbb27c092016-07-20 16:41:25 -04001117 ops->mode_set = sde_encoder_phys_wb_mode_set;
1118 ops->enable = sde_encoder_phys_wb_enable;
1119 ops->disable = sde_encoder_phys_wb_disable;
1120 ops->destroy = sde_encoder_phys_wb_destroy;
1121 ops->atomic_check = sde_encoder_phys_wb_atomic_check;
1122 ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
Alan Kwongbb27c092016-07-20 16:41:25 -04001123 ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
1124 ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
1125 ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
Clarence Ip110d15c2016-08-16 14:44:41 -04001126 ops->trigger_start = sde_encoder_helper_trigger_start;
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001127 ops->hw_reset = sde_encoder_helper_hw_reset;
Alan Kwongbb27c092016-07-20 16:41:25 -04001128}
1129
1130/**
1131 * sde_encoder_phys_wb_init - initialize writeback encoder
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001132 * @init: Pointer to init info structure with initialization params
Alan Kwongbb27c092016-07-20 16:41:25 -04001133 */
1134struct sde_encoder_phys *sde_encoder_phys_wb_init(
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001135 struct sde_enc_phys_init_params *p)
Alan Kwongbb27c092016-07-20 16:41:25 -04001136{
1137 struct sde_encoder_phys *phys_enc;
1138 struct sde_encoder_phys_wb *wb_enc;
1139 struct sde_hw_mdp *hw_mdp;
1140 int ret = 0;
1141
1142 SDE_DEBUG("\n");
1143
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001144 if (!p || !p->parent) {
1145 SDE_ERROR("invalid params\n");
1146 ret = -EINVAL;
1147 goto fail_alloc;
1148 }
1149
Alan Kwongbb27c092016-07-20 16:41:25 -04001150 wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
1151 if (!wb_enc) {
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001152 SDE_ERROR("failed to allocate wb enc\n");
Alan Kwongbb27c092016-07-20 16:41:25 -04001153 ret = -ENOMEM;
1154 goto fail_alloc;
1155 }
1156 wb_enc->irq_idx = -EINVAL;
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -05001157 wb_enc->wbdone_timeout = KICKOFF_TIMEOUT_MS;
Alan Kwongbb27c092016-07-20 16:41:25 -04001158 init_completion(&wb_enc->wbdone_complete);
1159
1160 phys_enc = &wb_enc->base;
1161
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001162 if (p->sde_kms->vbif[VBIF_NRT]) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001163 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001164 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001165 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001166 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001167 } else {
1168 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001169 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001170 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001171 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001172 }
1173
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001174 hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
Alan Kwongbb27c092016-07-20 16:41:25 -04001175 if (IS_ERR_OR_NULL(hw_mdp)) {
1176 ret = PTR_ERR(hw_mdp);
1177 SDE_ERROR("failed to init hw_top: %d\n", ret);
1178 goto fail_mdp_init;
1179 }
1180 phys_enc->hw_mdptop = hw_mdp;
1181
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001182 /**
1183 * hw_wb resource permanently assigned to this encoder
1184 * Other resources allocated at atomic commit time by use case
1185 */
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001186 if (p->wb_idx != SDE_NONE) {
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001187 struct sde_rm_hw_iter iter;
Alan Kwongbb27c092016-07-20 16:41:25 -04001188
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001189 sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
1190 while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
1191 struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
1192
1193 if (hw_wb->idx == p->wb_idx) {
1194 wb_enc->hw_wb = hw_wb;
1195 break;
1196 }
1197 }
1198
1199 if (!wb_enc->hw_wb) {
1200 ret = -EINVAL;
1201 SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
Alan Kwongbb27c092016-07-20 16:41:25 -04001202 goto fail_wb_init;
1203 }
Alan Kwongbb27c092016-07-20 16:41:25 -04001204 } else {
1205 ret = -EINVAL;
1206 SDE_ERROR("invalid wb_idx\n");
1207 goto fail_wb_check;
1208 }
1209
Alan Kwongbb27c092016-07-20 16:41:25 -04001210 sde_encoder_phys_wb_init_ops(&phys_enc->ops);
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001211 phys_enc->parent = p->parent;
1212 phys_enc->parent_ops = p->parent_ops;
1213 phys_enc->sde_kms = p->sde_kms;
1214 phys_enc->split_role = p->split_role;
Clarence Ip03521982016-08-26 10:49:47 -04001215 phys_enc->intf_mode = INTF_MODE_WB_LINE;
Dhaval Patel81e87882016-10-19 21:41:56 -07001216 phys_enc->intf_idx = p->intf_idx;
Lloyd Atkinson7d070942016-07-26 18:35:12 -04001217 phys_enc->enc_spinlock = p->enc_spinlock;
Alan Kwonga172ef52016-09-27 00:29:10 -04001218 INIT_LIST_HEAD(&wb_enc->irq_cb.list);
Alan Kwongbb27c092016-07-20 16:41:25 -04001219
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001220 /* create internal buffer for disable logic */
1221 if (_sde_encoder_phys_wb_init_internal_fb(wb_enc,
1222 DRM_FORMAT_RGB888, 2, 1)) {
1223 SDE_ERROR("failed to init internal fb\n");
1224 goto fail_wb_init;
1225 }
1226
Alan Kwongbb27c092016-07-20 16:41:25 -04001227 SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
1228 wb_enc->hw_wb->idx - WB_0);
1229
1230 return phys_enc;
1231
Alan Kwongbb27c092016-07-20 16:41:25 -04001232fail_wb_init:
1233fail_wb_check:
Alan Kwongbb27c092016-07-20 16:41:25 -04001234fail_mdp_init:
1235 kfree(wb_enc);
1236fail_alloc:
1237 return ERR_PTR(ret);
1238}
1239