blob: 5187627534f90bfcacf503d1c1edff4d5af63ca1 [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 Kwongbb27c092016-07-20 16:41:25 -0400780 */
781static void sde_encoder_phys_wb_prepare_for_kickoff(
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500782 struct sde_encoder_phys *phys_enc)
Alan Kwongbb27c092016-07-20 16:41:25 -0400783{
784 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
785 int ret;
786
787 SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
788 wb_enc->kickoff_count);
789
Alan Kwongbb27c092016-07-20 16:41:25 -0400790 reinit_completion(&wb_enc->wbdone_complete);
791
792 ret = sde_encoder_phys_wb_register_irq(phys_enc);
793 if (ret) {
794 SDE_ERROR("failed to register irq %d\n", ret);
795 return;
796 }
797
798 wb_enc->kickoff_count++;
799
800 /* set OT limit & enable traffic shaper */
801 sde_encoder_phys_wb_setup(phys_enc);
802
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700803 _sde_encoder_phys_wb_update_flush(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400804
805 /* vote for iommu/clk/bus */
806 wb_enc->start_time = ktime_get();
807
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500808 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->kickoff_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400809}
810
811/**
812 * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
813 * @phys_enc: Pointer to physical encoder
814 */
815static void sde_encoder_phys_wb_handle_post_kickoff(
816 struct sde_encoder_phys *phys_enc)
817{
818 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
819
820 SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
821
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400822 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc));
Alan Kwongbb27c092016-07-20 16:41:25 -0400823}
824
825/**
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700826 * _sde_encoder_phys_wb_init_internal_fb - create fb for internal commit
827 * @wb_enc: Pointer to writeback encoder
828 * @pixel_format: DRM pixel format
829 * @width: Desired fb width
830 * @height: Desired fb height
831 */
832static int _sde_encoder_phys_wb_init_internal_fb(
833 struct sde_encoder_phys_wb *wb_enc,
834 uint32_t pixel_format, uint32_t width, uint32_t height)
835{
836 struct drm_device *dev;
837 struct drm_framebuffer *fb;
838 struct drm_mode_fb_cmd2 mode_cmd;
839 uint32_t size;
840 int nplanes, i, ret;
841
842 if (!wb_enc || !wb_enc->base.parent || !wb_enc->base.sde_kms) {
843 SDE_ERROR("invalid params\n");
844 return -EINVAL;
845 }
846
847 dev = wb_enc->base.sde_kms->dev;
848 if (!dev) {
849 SDE_ERROR("invalid dev\n");
850 return -EINVAL;
851 }
852
853 memset(&mode_cmd, 0, sizeof(mode_cmd));
854 mode_cmd.pixel_format = pixel_format;
855 mode_cmd.width = width;
856 mode_cmd.height = height;
857
858 size = sde_format_get_framebuffer_size(pixel_format,
859 mode_cmd.width, mode_cmd.height, 0, 0);
860 if (!size) {
861 SDE_DEBUG("not creating zero size buffer\n");
862 return -EINVAL;
863 }
864
865 /* allocate gem tracking object */
866 nplanes = drm_format_num_planes(pixel_format);
867 if (nplanes > SDE_MAX_PLANES) {
868 SDE_ERROR("requested format has too many planes\n");
869 return -EINVAL;
870 }
871 mutex_lock(&dev->struct_mutex);
872 wb_enc->bo_disable[0] = msm_gem_new(dev, size,
873 MSM_BO_SCANOUT | MSM_BO_WC);
874 mutex_unlock(&dev->struct_mutex);
875
876 if (IS_ERR_OR_NULL(wb_enc->bo_disable[0])) {
877 ret = PTR_ERR(wb_enc->bo_disable[0]);
878 wb_enc->bo_disable[0] = NULL;
879
880 SDE_ERROR("failed to create bo, %d\n", ret);
881 return ret;
882 }
883
884 for (i = 0; i < nplanes; ++i) {
885 wb_enc->bo_disable[i] = wb_enc->bo_disable[0];
886 mode_cmd.pitches[i] = width *
887 drm_format_plane_cpp(pixel_format, i);
888 }
889
890 fb = msm_framebuffer_init(dev, &mode_cmd, wb_enc->bo_disable);
891 if (IS_ERR_OR_NULL(fb)) {
892 ret = PTR_ERR(fb);
893 drm_gem_object_unreference(wb_enc->bo_disable[0]);
894 wb_enc->bo_disable[0] = NULL;
895
896 SDE_ERROR("failed to init fb, %d\n", ret);
897 return ret;
898 }
899
900 /* prepare the backing buffer now so that it's available later */
901 ret = msm_framebuffer_prepare(fb,
902 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE]);
903 if (!ret)
904 wb_enc->fb_disable = fb;
905 return ret;
906}
907
908/**
909 * _sde_encoder_phys_wb_destroy_internal_fb - deconstruct internal fb
910 * @wb_enc: Pointer to writeback encoder
911 */
912static void _sde_encoder_phys_wb_destroy_internal_fb(
913 struct sde_encoder_phys_wb *wb_enc)
914{
915 if (!wb_enc)
916 return;
917
918 if (wb_enc->fb_disable) {
919 drm_framebuffer_unregister_private(wb_enc->fb_disable);
920 drm_framebuffer_remove(wb_enc->fb_disable);
921 wb_enc->fb_disable = NULL;
922 }
923
924 if (wb_enc->bo_disable[0]) {
925 drm_gem_object_unreference(wb_enc->bo_disable[0]);
926 wb_enc->bo_disable[0] = NULL;
927 }
928}
929
930/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400931 * sde_encoder_phys_wb_enable - enable writeback encoder
932 * @phys_enc: Pointer to physical encoder
933 */
934static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
935{
936 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
937 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwong74e8ba32016-09-30 14:25:16 -0400938 struct drm_device *dev;
Alan Kwongbb27c092016-07-20 16:41:25 -0400939 struct drm_connector *connector;
940
941 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
942
Alan Kwong74e8ba32016-09-30 14:25:16 -0400943 if (!wb_enc->base.parent || !wb_enc->base.parent->dev) {
944 SDE_ERROR("invalid drm device\n");
945 return;
946 }
947 dev = wb_enc->base.parent->dev;
948
Alan Kwongbb27c092016-07-20 16:41:25 -0400949 /* find associated writeback connector */
Clarence Ip05431f32016-11-25 16:39:38 -0500950 connector = phys_enc->connector;
Alan Kwong74e8ba32016-09-30 14:25:16 -0400951
Alan Kwongbb27c092016-07-20 16:41:25 -0400952 if (!connector || connector->encoder != phys_enc->parent) {
953 SDE_ERROR("failed to find writeback connector\n");
954 return;
955 }
956 wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
957
958 phys_enc->enable_state = SDE_ENC_ENABLED;
959}
960
961/**
962 * sde_encoder_phys_wb_disable - disable writeback encoder
963 * @phys_enc: Pointer to physical encoder
964 */
965static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
966{
967 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
968 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
969
970 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
971
972 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
973 SDE_ERROR("encoder is already disabled\n");
974 return;
975 }
976
977 if (wb_enc->frame_count != wb_enc->kickoff_count) {
978 SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
979 hw_wb->idx - WB_0, wb_enc->frame_count,
980 wb_enc->kickoff_count);
981 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
982 }
983
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700984 if (!phys_enc->hw_ctl || !phys_enc->parent ||
985 !phys_enc->sde_kms || !wb_enc->fb_disable) {
986 SDE_DEBUG("invalid enc, skipping extra commit\n");
987 goto exit;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400988 }
989
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700990 /* reset h/w before final flush */
991 if (sde_encoder_helper_hw_release(phys_enc, wb_enc->fb_disable))
992 goto exit;
993
994 phys_enc->enable_state = SDE_ENC_DISABLING;
995 sde_encoder_phys_wb_prepare_for_kickoff(phys_enc);
996 if (phys_enc->hw_ctl->ops.trigger_flush)
997 phys_enc->hw_ctl->ops.trigger_flush(phys_enc->hw_ctl);
998 sde_encoder_helper_trigger_start(phys_enc);
999 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
1000exit:
Alan Kwongbb27c092016-07-20 16:41:25 -04001001 phys_enc->enable_state = SDE_ENC_DISABLED;
1002}
1003
1004/**
1005 * sde_encoder_phys_wb_get_hw_resources - get hardware resources
1006 * @phys_enc: Pointer to physical encoder
1007 * @hw_res: Pointer to encoder resources
1008 */
1009static void sde_encoder_phys_wb_get_hw_resources(
1010 struct sde_encoder_phys *phys_enc,
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001011 struct sde_encoder_hw_resources *hw_res,
1012 struct drm_connector_state *conn_state)
Alan Kwongbb27c092016-07-20 16:41:25 -04001013{
1014 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -04001015 struct sde_hw_wb *hw_wb;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001016 struct drm_framebuffer *fb;
1017 const struct sde_format *fmt;
Alan Kwongbb27c092016-07-20 16:41:25 -04001018
Clarence Ip03521982016-08-26 10:49:47 -04001019 if (!phys_enc) {
1020 SDE_ERROR("invalid encoder\n");
1021 return;
1022 }
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001023
1024 fb = sde_wb_connector_state_get_output_fb(conn_state);
1025 if (!fb) {
1026 SDE_ERROR("no output framebuffer\n");
1027 return;
1028 }
1029
1030 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
1031 drm_format_num_planes(fb->pixel_format));
1032 if (!fmt) {
1033 SDE_ERROR("unsupported output pixel format:%d\n",
1034 fb->pixel_format);
1035 return;
1036 }
1037
Clarence Ip03521982016-08-26 10:49:47 -04001038 hw_wb = wb_enc->hw_wb;
Clarence Ip03521982016-08-26 10:49:47 -04001039 hw_res->wbs[hw_wb->idx - WB_0] = phys_enc->intf_mode;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001040 hw_res->needs_cdm = SDE_FORMAT_IS_YUV(fmt);
1041 SDE_DEBUG("[wb:%d] intf_mode=%d needs_cdm=%d\n", hw_wb->idx - WB_0,
1042 hw_res->wbs[hw_wb->idx - WB_0],
1043 hw_res->needs_cdm);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001044}
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001045
Alan Kwongbb27c092016-07-20 16:41:25 -04001046#ifdef CONFIG_DEBUG_FS
1047/**
1048 * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001049 * @phys_enc: Pointer to physical encoder
1050 * @debugfs_root: Pointer to virtual encoder's debugfs_root dir
Alan Kwongbb27c092016-07-20 16:41:25 -04001051 */
1052static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001053 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001054{
1055 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1056
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001057 if (!phys_enc || !wb_enc->hw_wb || !debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001058 return -EINVAL;
1059
Alan Kwongbb27c092016-07-20 16:41:25 -04001060 if (!debugfs_create_u32("wbdone_timeout", 0644,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001061 debugfs_root, &wb_enc->wbdone_timeout)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001062 SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
1063 return -ENOMEM;
1064 }
1065
1066 if (!debugfs_create_u32("bypass_irqreg", 0644,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001067 debugfs_root, &wb_enc->bypass_irqreg)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001068 SDE_ERROR("failed to create debugfs/bypass_irqreg\n");
1069 return -ENOMEM;
1070 }
1071
1072 return 0;
1073}
Alan Kwongbb27c092016-07-20 16:41:25 -04001074#else
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001075static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001076 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001077{
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001078 return 0;
Alan Kwongbb27c092016-07-20 16:41:25 -04001079}
Alan Kwongbb27c092016-07-20 16:41:25 -04001080#endif
1081
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001082static int sde_encoder_phys_wb_late_register(struct sde_encoder_phys *phys_enc,
1083 struct dentry *debugfs_root)
1084{
1085 return sde_encoder_phys_wb_init_debugfs(phys_enc, debugfs_root);
1086}
1087
Alan Kwongbb27c092016-07-20 16:41:25 -04001088/**
1089 * sde_encoder_phys_wb_destroy - destroy writeback encoder
1090 * @phys_enc: Pointer to physical encoder
1091 */
1092static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
1093{
1094 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1095 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
1096
1097 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
1098
1099 if (!phys_enc)
1100 return;
1101
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001102 _sde_encoder_phys_wb_destroy_internal_fb(wb_enc);
1103
Alan Kwongbb27c092016-07-20 16:41:25 -04001104 kfree(wb_enc);
1105}
1106
1107/**
1108 * sde_encoder_phys_wb_init_ops - initialize writeback operations
1109 * @ops: Pointer to encoder operation table
1110 */
1111static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
1112{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001113 ops->late_register = sde_encoder_phys_wb_late_register;
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001114 ops->is_master = sde_encoder_phys_wb_is_master;
Alan Kwongbb27c092016-07-20 16:41:25 -04001115 ops->mode_set = sde_encoder_phys_wb_mode_set;
1116 ops->enable = sde_encoder_phys_wb_enable;
1117 ops->disable = sde_encoder_phys_wb_disable;
1118 ops->destroy = sde_encoder_phys_wb_destroy;
1119 ops->atomic_check = sde_encoder_phys_wb_atomic_check;
1120 ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
Alan Kwongbb27c092016-07-20 16:41:25 -04001121 ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
1122 ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
1123 ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
Clarence Ip110d15c2016-08-16 14:44:41 -04001124 ops->trigger_start = sde_encoder_helper_trigger_start;
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001125 ops->hw_reset = sde_encoder_helper_hw_reset;
Alan Kwongbb27c092016-07-20 16:41:25 -04001126}
1127
1128/**
1129 * sde_encoder_phys_wb_init - initialize writeback encoder
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001130 * @init: Pointer to init info structure with initialization params
Alan Kwongbb27c092016-07-20 16:41:25 -04001131 */
1132struct sde_encoder_phys *sde_encoder_phys_wb_init(
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001133 struct sde_enc_phys_init_params *p)
Alan Kwongbb27c092016-07-20 16:41:25 -04001134{
1135 struct sde_encoder_phys *phys_enc;
1136 struct sde_encoder_phys_wb *wb_enc;
1137 struct sde_hw_mdp *hw_mdp;
1138 int ret = 0;
1139
1140 SDE_DEBUG("\n");
1141
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001142 if (!p || !p->parent) {
1143 SDE_ERROR("invalid params\n");
1144 ret = -EINVAL;
1145 goto fail_alloc;
1146 }
1147
Alan Kwongbb27c092016-07-20 16:41:25 -04001148 wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
1149 if (!wb_enc) {
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001150 SDE_ERROR("failed to allocate wb enc\n");
Alan Kwongbb27c092016-07-20 16:41:25 -04001151 ret = -ENOMEM;
1152 goto fail_alloc;
1153 }
1154 wb_enc->irq_idx = -EINVAL;
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -05001155 wb_enc->wbdone_timeout = KICKOFF_TIMEOUT_MS;
Alan Kwongbb27c092016-07-20 16:41:25 -04001156 init_completion(&wb_enc->wbdone_complete);
1157
1158 phys_enc = &wb_enc->base;
1159
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001160 if (p->sde_kms->vbif[VBIF_NRT]) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001161 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001162 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001163 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001164 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001165 } else {
1166 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001167 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001168 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001169 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001170 }
1171
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001172 hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
Alan Kwongbb27c092016-07-20 16:41:25 -04001173 if (IS_ERR_OR_NULL(hw_mdp)) {
1174 ret = PTR_ERR(hw_mdp);
1175 SDE_ERROR("failed to init hw_top: %d\n", ret);
1176 goto fail_mdp_init;
1177 }
1178 phys_enc->hw_mdptop = hw_mdp;
1179
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001180 /**
1181 * hw_wb resource permanently assigned to this encoder
1182 * Other resources allocated at atomic commit time by use case
1183 */
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001184 if (p->wb_idx != SDE_NONE) {
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001185 struct sde_rm_hw_iter iter;
Alan Kwongbb27c092016-07-20 16:41:25 -04001186
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001187 sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
1188 while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
1189 struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
1190
1191 if (hw_wb->idx == p->wb_idx) {
1192 wb_enc->hw_wb = hw_wb;
1193 break;
1194 }
1195 }
1196
1197 if (!wb_enc->hw_wb) {
1198 ret = -EINVAL;
1199 SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
Alan Kwongbb27c092016-07-20 16:41:25 -04001200 goto fail_wb_init;
1201 }
Alan Kwongbb27c092016-07-20 16:41:25 -04001202 } else {
1203 ret = -EINVAL;
1204 SDE_ERROR("invalid wb_idx\n");
1205 goto fail_wb_check;
1206 }
1207
Alan Kwongbb27c092016-07-20 16:41:25 -04001208 sde_encoder_phys_wb_init_ops(&phys_enc->ops);
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001209 phys_enc->parent = p->parent;
1210 phys_enc->parent_ops = p->parent_ops;
1211 phys_enc->sde_kms = p->sde_kms;
1212 phys_enc->split_role = p->split_role;
Clarence Ip03521982016-08-26 10:49:47 -04001213 phys_enc->intf_mode = INTF_MODE_WB_LINE;
Dhaval Patel81e87882016-10-19 21:41:56 -07001214 phys_enc->intf_idx = p->intf_idx;
Lloyd Atkinson7d070942016-07-26 18:35:12 -04001215 phys_enc->enc_spinlock = p->enc_spinlock;
Alan Kwonga172ef52016-09-27 00:29:10 -04001216 INIT_LIST_HEAD(&wb_enc->irq_cb.list);
Alan Kwongbb27c092016-07-20 16:41:25 -04001217
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001218 /* create internal buffer for disable logic */
1219 if (_sde_encoder_phys_wb_init_internal_fb(wb_enc,
1220 DRM_FORMAT_RGB888, 2, 1)) {
1221 SDE_ERROR("failed to init internal fb\n");
1222 goto fail_wb_init;
1223 }
1224
Alan Kwongbb27c092016-07-20 16:41:25 -04001225 SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
1226 wb_enc->hw_wb->idx - WB_0);
1227
1228 return phys_enc;
1229
Alan Kwongbb27c092016-07-20 16:41:25 -04001230fail_wb_init:
1231fail_wb_check:
Alan Kwongbb27c092016-07-20 16:41:25 -04001232fail_mdp_init:
1233 kfree(wb_enc);
1234fail_alloc:
1235 return ERR_PTR(ret);
1236}
1237