blob: 9a90075d8993e27d0cb8312d0620bf30aa41252d [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>
Alan Kwong03b89842017-08-17 16:32:45 -040016#include <uapi/drm/sde_drm.h>
Alan Kwongbb27c092016-07-20 16:41:25 -040017
18#include "sde_encoder_phys.h"
19#include "sde_formats.h"
20#include "sde_hw_top.h"
21#include "sde_hw_interrupts.h"
Alan Kwongf5dd86c2016-08-09 18:08:17 -040022#include "sde_core_irq.h"
Alan Kwongbb27c092016-07-20 16:41:25 -040023#include "sde_wb.h"
Lloyd Atkinson8772e202016-09-26 17:52:16 -040024#include "sde_vbif.h"
Alan Kwonga62eeb82017-04-19 08:57:55 -070025#include "sde_crtc.h"
Alan Kwongbb27c092016-07-20 16:41:25 -040026
Alan Kwongbb27c092016-07-20 16:41:25 -040027#define to_sde_encoder_phys_wb(x) \
28 container_of(x, struct sde_encoder_phys_wb, base)
29
Alan Kwong1124f1f2017-11-10 18:14:39 -050030#define WBID(wb_enc) \
31 ((wb_enc && wb_enc->wb_dev) ? wb_enc->wb_dev->wb_idx - WB_0 : -1)
Alan Kwongbb27c092016-07-20 16:41:25 -040032
Alan Kwongd6a38602017-03-11 22:26:25 -080033#define TO_S15D16(_x_) ((_x_) << 7)
34
35/**
36 * sde_rgb2yuv_601l - rgb to yuv color space conversion matrix
37 *
38 */
39static struct sde_csc_cfg sde_encoder_phys_wb_rgb2yuv_601l = {
40 {
41 TO_S15D16(0x0083), TO_S15D16(0x0102), TO_S15D16(0x0032),
42 TO_S15D16(0x1fb5), TO_S15D16(0x1f6c), TO_S15D16(0x00e1),
43 TO_S15D16(0x00e1), TO_S15D16(0x1f45), TO_S15D16(0x1fdc)
44 },
45 { 0x00, 0x00, 0x00 },
46 { 0x0040, 0x0200, 0x0200 },
47 { 0x000, 0x3ff, 0x000, 0x3ff, 0x000, 0x3ff },
48 { 0x040, 0x3ac, 0x040, 0x3c0, 0x040, 0x3c0 },
49};
50
Alan Kwongbb27c092016-07-20 16:41:25 -040051/**
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -040052 * sde_encoder_phys_wb_is_master - report wb always as master encoder
53 */
54static bool sde_encoder_phys_wb_is_master(struct sde_encoder_phys *phys_enc)
55{
56 return true;
57}
58
59/**
Alan Kwongbb27c092016-07-20 16:41:25 -040060 * sde_encoder_phys_wb_get_intr_type - get interrupt type based on block mode
61 * @hw_wb: Pointer to h/w writeback driver
62 */
63static enum sde_intr_type sde_encoder_phys_wb_get_intr_type(
64 struct sde_hw_wb *hw_wb)
65{
66 return (hw_wb->caps->features & BIT(SDE_WB_BLOCK_MODE)) ?
67 SDE_IRQ_TYPE_WB_ROT_COMP : SDE_IRQ_TYPE_WB_WFD_COMP;
68}
69
70/**
Alan Kwong5d324e42016-07-28 22:56:18 -040071 * sde_encoder_phys_wb_set_ot_limit - set OT limit for writeback interface
72 * @phys_enc: Pointer to physical encoder
73 */
74static void sde_encoder_phys_wb_set_ot_limit(
75 struct sde_encoder_phys *phys_enc)
76{
77 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
78 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
79 struct sde_vbif_set_ot_params ot_params;
80
81 memset(&ot_params, 0, sizeof(ot_params));
82 ot_params.xin_id = hw_wb->caps->xin_id;
83 ot_params.num = hw_wb->idx - WB_0;
84 ot_params.width = wb_enc->wb_roi.w;
85 ot_params.height = wb_enc->wb_roi.h;
86 ot_params.is_wfd = true;
87 ot_params.frame_rate = phys_enc->cached_mode.vrefresh;
88 ot_params.vbif_idx = hw_wb->caps->vbif_idx;
89 ot_params.clk_ctrl = hw_wb->caps->clk_ctrl;
90 ot_params.rd = false;
91
92 sde_vbif_set_ot_limit(phys_enc->sde_kms, &ot_params);
93}
94
95/**
Alan Kwongbb27c092016-07-20 16:41:25 -040096 * sde_encoder_phys_wb_set_traffic_shaper - set traffic shaper for writeback
97 * @phys_enc: Pointer to physical encoder
98 */
99static void sde_encoder_phys_wb_set_traffic_shaper(
100 struct sde_encoder_phys *phys_enc)
101{
102 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
103 struct sde_hw_wb_cfg *wb_cfg = &wb_enc->wb_cfg;
104
105 /* traffic shaper is only enabled for rotator */
106 wb_cfg->ts_cfg.en = false;
107}
108
109/**
Alan Kwonga62eeb82017-04-19 08:57:55 -0700110 * sde_encoder_phys_wb_set_qos_remap - set QoS remapper for writeback
111 * @phys_enc: Pointer to physical encoder
112 */
113static void sde_encoder_phys_wb_set_qos_remap(
114 struct sde_encoder_phys *phys_enc)
115{
116 struct sde_encoder_phys_wb *wb_enc;
117 struct sde_hw_wb *hw_wb;
118 struct drm_crtc *crtc;
119 struct sde_vbif_set_qos_params qos_params;
120
121 if (!phys_enc || !phys_enc->parent || !phys_enc->parent->crtc) {
122 SDE_ERROR("invalid arguments\n");
123 return;
124 }
125
126 wb_enc = to_sde_encoder_phys_wb(phys_enc);
Harsh Sahufae4d812017-11-29 15:08:59 -0800127 if (!wb_enc->crtc) {
128 SDE_ERROR("invalid crtc");
129 return;
130 }
131
132 crtc = wb_enc->crtc;
Alan Kwonga62eeb82017-04-19 08:57:55 -0700133
134 if (!wb_enc->hw_wb || !wb_enc->hw_wb->caps) {
135 SDE_ERROR("invalid writeback hardware\n");
136 return;
137 }
138
139 hw_wb = wb_enc->hw_wb;
140
141 memset(&qos_params, 0, sizeof(qos_params));
142 qos_params.vbif_idx = hw_wb->caps->vbif_idx;
143 qos_params.xin_id = hw_wb->caps->xin_id;
144 qos_params.clk_ctrl = hw_wb->caps->clk_ctrl;
145 qos_params.num = hw_wb->idx - WB_0;
146 qos_params.is_rt = sde_crtc_get_client_type(crtc) != NRT_CLIENT;
147
148 SDE_DEBUG("[qos_remap] wb:%d vbif:%d xin:%d rt:%d\n",
149 qos_params.num,
150 qos_params.vbif_idx,
151 qos_params.xin_id, qos_params.is_rt);
152
153 sde_vbif_set_qos_remap(phys_enc->sde_kms, &qos_params);
154}
155
156/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400157 * sde_encoder_phys_setup_cdm - setup chroma down block
158 * @phys_enc: Pointer to physical encoder
159 * @fb: Pointer to output framebuffer
160 * @format: Output format
161 */
162void sde_encoder_phys_setup_cdm(struct sde_encoder_phys *phys_enc,
163 struct drm_framebuffer *fb, const struct sde_format *format,
164 struct sde_rect *wb_roi)
165{
Lloyd Atkinson6a5359d2017-06-21 10:18:08 -0400166 struct sde_hw_cdm *hw_cdm;
167 struct sde_hw_cdm_cfg *cdm_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400168 int ret;
169
Lloyd Atkinson6a5359d2017-06-21 10:18:08 -0400170 if (!phys_enc || !format)
171 return;
172
173 cdm_cfg = &phys_enc->cdm_cfg;
174 hw_cdm = phys_enc->hw_cdm;
175 if (!hw_cdm)
176 return;
177
Alan Kwongbb27c092016-07-20 16:41:25 -0400178 if (!SDE_FORMAT_IS_YUV(format)) {
179 SDE_DEBUG("[cdm_disable fmt:%x]\n",
180 format->base.pixel_format);
181
182 if (hw_cdm && hw_cdm->ops.disable)
183 hw_cdm->ops.disable(hw_cdm);
184
185 return;
186 }
187
188 memset(cdm_cfg, 0, sizeof(struct sde_hw_cdm_cfg));
189
Lloyd Atkinson6a5359d2017-06-21 10:18:08 -0400190 if (!wb_roi)
191 return;
192
Alan Kwongbb27c092016-07-20 16:41:25 -0400193 cdm_cfg->output_width = wb_roi->w;
194 cdm_cfg->output_height = wb_roi->h;
195 cdm_cfg->output_fmt = format;
196 cdm_cfg->output_type = CDM_CDWN_OUTPUT_WB;
abeykun5ed42e12016-08-24 17:09:26 -0400197 cdm_cfg->output_bit_depth = SDE_FORMAT_IS_DX(format) ?
198 CDM_CDWN_OUTPUT_10BIT : CDM_CDWN_OUTPUT_8BIT;
Alan Kwongbb27c092016-07-20 16:41:25 -0400199
200 /* enable 10 bit logic */
201 switch (cdm_cfg->output_fmt->chroma_sample) {
202 case SDE_CHROMA_RGB:
203 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
204 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
205 break;
206 case SDE_CHROMA_H2V1:
207 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
208 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
209 break;
210 case SDE_CHROMA_420:
211 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
212 cdm_cfg->v_cdwn_type = CDM_CDWN_OFFSITE;
213 break;
214 case SDE_CHROMA_H1V2:
215 default:
216 SDE_ERROR("unsupported chroma sampling type\n");
217 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
218 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
219 break;
220 }
221
222 SDE_DEBUG("[cdm_enable:%d,%d,%X,%d,%d,%d,%d]\n",
223 cdm_cfg->output_width,
224 cdm_cfg->output_height,
225 cdm_cfg->output_fmt->base.pixel_format,
226 cdm_cfg->output_type,
227 cdm_cfg->output_bit_depth,
228 cdm_cfg->h_cdwn_type,
229 cdm_cfg->v_cdwn_type);
230
Alan Kwongd6a38602017-03-11 22:26:25 -0800231 if (hw_cdm && hw_cdm->ops.setup_csc_data) {
232 ret = hw_cdm->ops.setup_csc_data(hw_cdm,
233 &sde_encoder_phys_wb_rgb2yuv_601l);
234 if (ret < 0) {
235 SDE_ERROR("failed to setup CSC %d\n", ret);
236 return;
237 }
238 }
239
Alan Kwongbb27c092016-07-20 16:41:25 -0400240 if (hw_cdm && hw_cdm->ops.setup_cdwn) {
241 ret = hw_cdm->ops.setup_cdwn(hw_cdm, cdm_cfg);
242 if (ret < 0) {
243 SDE_ERROR("failed to setup CDM %d\n", ret);
244 return;
245 }
246 }
247
248 if (hw_cdm && hw_cdm->ops.enable) {
249 ret = hw_cdm->ops.enable(hw_cdm, cdm_cfg);
250 if (ret < 0) {
251 SDE_ERROR("failed to enable CDM %d\n", ret);
252 return;
253 }
254 }
255}
256
257/**
258 * sde_encoder_phys_wb_setup_fb - setup output framebuffer
259 * @phys_enc: Pointer to physical encoder
260 * @fb: Pointer to output framebuffer
261 * @wb_roi: Pointer to output region of interest
262 */
263static void sde_encoder_phys_wb_setup_fb(struct sde_encoder_phys *phys_enc,
264 struct drm_framebuffer *fb, struct sde_rect *wb_roi)
265{
266 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -0400267 struct sde_hw_wb *hw_wb;
268 struct sde_hw_wb_cfg *wb_cfg;
Alan Kwong143f50c2017-04-28 07:34:28 -0700269 struct sde_hw_wb_cdp_cfg *cdp_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400270 const struct msm_format *format;
Jordan Croused8e96522017-02-13 10:14:16 -0700271 int ret;
272 struct msm_gem_address_space *aspace;
Alan Kwong03b89842017-08-17 16:32:45 -0400273 u32 fb_mode;
Alan Kwongbb27c092016-07-20 16:41:25 -0400274
Alan Kwong03b89842017-08-17 16:32:45 -0400275 if (!phys_enc || !phys_enc->sde_kms || !phys_enc->sde_kms->catalog ||
276 !phys_enc->connector) {
Clarence Ip03521982016-08-26 10:49:47 -0400277 SDE_ERROR("invalid encoder\n");
278 return;
279 }
280
281 hw_wb = wb_enc->hw_wb;
282 wb_cfg = &wb_enc->wb_cfg;
Alan Kwong143f50c2017-04-28 07:34:28 -0700283 cdp_cfg = &wb_enc->cdp_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400284 memset(wb_cfg, 0, sizeof(struct sde_hw_wb_cfg));
285
Clarence Ip03521982016-08-26 10:49:47 -0400286 wb_cfg->intf_mode = phys_enc->intf_mode;
Alan Kwong03b89842017-08-17 16:32:45 -0400287
288 fb_mode = sde_connector_get_property(phys_enc->connector->state,
289 CONNECTOR_PROP_FB_TRANSLATION_MODE);
290 if (phys_enc->enable_state == SDE_ENC_DISABLING)
291 wb_cfg->is_secure = false;
292 else if (fb_mode == SDE_DRM_FB_SEC)
293 wb_cfg->is_secure = true;
294 else
295 wb_cfg->is_secure = false;
296
Jordan Croused8e96522017-02-13 10:14:16 -0700297 aspace = (wb_cfg->is_secure) ?
298 wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] :
299 wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -0400300
301 SDE_DEBUG("[fb_secure:%d]\n", wb_cfg->is_secure);
302
Alan Kwong03b89842017-08-17 16:32:45 -0400303 ret = msm_framebuffer_prepare(fb, aspace);
304 if (ret) {
305 SDE_ERROR("prep fb failed, %d\n", ret);
306 return;
307 }
308
309 /* cache framebuffer for cleanup in writeback done */
310 wb_enc->wb_fb = fb;
311 wb_enc->wb_aspace = aspace;
312
Alan Kwongbb27c092016-07-20 16:41:25 -0400313 format = msm_framebuffer_format(fb);
Dhaval Patelccbcb3d2016-08-22 11:58:14 -0700314 if (!format) {
315 SDE_DEBUG("invalid format for fb\n");
316 return;
317 }
318
Alan Kwongbb27c092016-07-20 16:41:25 -0400319 wb_cfg->dest.format = sde_get_sde_format_ext(
320 format->pixel_format,
321 fb->modifier,
322 drm_format_num_planes(fb->pixel_format));
323 if (!wb_cfg->dest.format) {
324 /* this error should be detected during atomic_check */
325 SDE_ERROR("failed to get format %x\n", format->pixel_format);
326 return;
327 }
abeykunf1539f72016-08-24 16:08:03 -0400328 wb_cfg->roi = *wb_roi;
Alan Kwongbb27c092016-07-20 16:41:25 -0400329
abeykunf1539f72016-08-24 16:08:03 -0400330 if (hw_wb->caps->features & BIT(SDE_WB_XY_ROI_OFFSET)) {
Jordan Croused8e96522017-02-13 10:14:16 -0700331 ret = sde_format_populate_layout(aspace, fb, &wb_cfg->dest);
abeykunf1539f72016-08-24 16:08:03 -0400332 if (ret) {
333 SDE_DEBUG("failed to populate layout %d\n", ret);
334 return;
335 }
336 wb_cfg->dest.width = fb->width;
337 wb_cfg->dest.height = fb->height;
338 wb_cfg->dest.num_planes = wb_cfg->dest.format->num_planes;
339 } else {
Jordan Croused8e96522017-02-13 10:14:16 -0700340 ret = sde_format_populate_layout_with_roi(aspace, fb, wb_roi,
Alan Kwongbb27c092016-07-20 16:41:25 -0400341 &wb_cfg->dest);
abeykunf1539f72016-08-24 16:08:03 -0400342 if (ret) {
343 /* this error should be detected during atomic_check */
344 SDE_DEBUG("failed to populate layout %d\n", ret);
345 return;
346 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400347 }
348
349 if ((wb_cfg->dest.format->fetch_planes == SDE_PLANE_PLANAR) &&
350 (wb_cfg->dest.format->element[0] == C1_B_Cb))
351 swap(wb_cfg->dest.plane_addr[1], wb_cfg->dest.plane_addr[2]);
352
353 SDE_DEBUG("[fb_offset:%8.8x,%8.8x,%8.8x,%8.8x]\n",
354 wb_cfg->dest.plane_addr[0],
355 wb_cfg->dest.plane_addr[1],
356 wb_cfg->dest.plane_addr[2],
357 wb_cfg->dest.plane_addr[3]);
358 SDE_DEBUG("[fb_stride:%8.8x,%8.8x,%8.8x,%8.8x]\n",
359 wb_cfg->dest.plane_pitch[0],
360 wb_cfg->dest.plane_pitch[1],
361 wb_cfg->dest.plane_pitch[2],
362 wb_cfg->dest.plane_pitch[3]);
363
abeykunf1539f72016-08-24 16:08:03 -0400364 if (hw_wb->ops.setup_roi)
365 hw_wb->ops.setup_roi(hw_wb, wb_cfg);
366
Alan Kwongbb27c092016-07-20 16:41:25 -0400367 if (hw_wb->ops.setup_outformat)
368 hw_wb->ops.setup_outformat(hw_wb, wb_cfg);
369
Alan Kwong143f50c2017-04-28 07:34:28 -0700370 if (hw_wb->ops.setup_cdp) {
371 memset(cdp_cfg, 0, sizeof(struct sde_hw_wb_cdp_cfg));
372
373 cdp_cfg->enable = phys_enc->sde_kms->catalog->perf.cdp_cfg
374 [SDE_PERF_CDP_USAGE_NRT].wr_enable;
375 cdp_cfg->ubwc_meta_enable =
376 SDE_FORMAT_IS_UBWC(wb_cfg->dest.format);
377 cdp_cfg->tile_amortize_enable =
378 SDE_FORMAT_IS_UBWC(wb_cfg->dest.format) ||
379 SDE_FORMAT_IS_TILE(wb_cfg->dest.format);
380 cdp_cfg->preload_ahead = SDE_WB_CDP_PRELOAD_AHEAD_64;
381
382 hw_wb->ops.setup_cdp(hw_wb, cdp_cfg);
383 }
384
Alan Kwongc16e0922017-05-11 14:50:46 -0700385 if (hw_wb->ops.setup_outaddress) {
386 SDE_EVT32(hw_wb->idx,
387 wb_cfg->dest.width,
388 wb_cfg->dest.height,
389 wb_cfg->dest.plane_addr[0],
390 wb_cfg->dest.plane_size[0],
391 wb_cfg->dest.plane_addr[1],
392 wb_cfg->dest.plane_size[1],
393 wb_cfg->dest.plane_addr[2],
394 wb_cfg->dest.plane_size[2],
395 wb_cfg->dest.plane_addr[3],
396 wb_cfg->dest.plane_size[3]);
Alan Kwongbb27c092016-07-20 16:41:25 -0400397 hw_wb->ops.setup_outaddress(hw_wb, wb_cfg);
Alan Kwongc16e0922017-05-11 14:50:46 -0700398 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400399}
400
401/**
402 * sde_encoder_phys_wb_setup_cdp - setup chroma down prefetch block
403 * @phys_enc: Pointer to physical encoder
404 */
405static void sde_encoder_phys_wb_setup_cdp(struct sde_encoder_phys *phys_enc)
406{
407 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
408 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
409 struct sde_hw_intf_cfg *intf_cfg = &wb_enc->intf_cfg;
410
411 memset(intf_cfg, 0, sizeof(struct sde_hw_intf_cfg));
412
413 intf_cfg->intf = SDE_NONE;
414 intf_cfg->wb = hw_wb->idx;
Lloyd Atkinson55987b02016-08-16 16:57:46 -0400415 intf_cfg->mode_3d = sde_encoder_helper_get_3d_blend_mode(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400416
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400417 if (phys_enc->hw_ctl && phys_enc->hw_ctl->ops.setup_intf_cfg)
Alan Kwongbb27c092016-07-20 16:41:25 -0400418 phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl,
419 intf_cfg);
420}
421
422/**
423 * sde_encoder_phys_wb_atomic_check - verify and fixup given atomic states
424 * @phys_enc: Pointer to physical encoder
425 * @crtc_state: Pointer to CRTC atomic state
426 * @conn_state: Pointer to connector atomic state
427 */
428static int sde_encoder_phys_wb_atomic_check(
429 struct sde_encoder_phys *phys_enc,
430 struct drm_crtc_state *crtc_state,
431 struct drm_connector_state *conn_state)
432{
433 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
434 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
435 const struct sde_wb_cfg *wb_cfg = hw_wb->caps;
436 struct drm_framebuffer *fb;
437 const struct sde_format *fmt;
438 struct sde_rect wb_roi;
439 const struct drm_display_mode *mode = &crtc_state->mode;
440 int rc;
441
442 SDE_DEBUG("[atomic_check:%d,%d,\"%s\",%d,%d]\n",
443 hw_wb->idx - WB_0, mode->base.id, mode->name,
444 mode->hdisplay, mode->vdisplay);
445
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400446 if (!conn_state || !conn_state->connector) {
447 SDE_ERROR("invalid connector state\n");
448 return -EINVAL;
449 } else if (conn_state->connector->status !=
450 connector_status_connected) {
451 SDE_ERROR("connector not connected %d\n",
452 conn_state->connector->status);
453 return -EINVAL;
454 }
455
Alan Kwongbb27c092016-07-20 16:41:25 -0400456 memset(&wb_roi, 0, sizeof(struct sde_rect));
457
458 rc = sde_wb_connector_state_get_output_roi(conn_state, &wb_roi);
459 if (rc) {
460 SDE_ERROR("failed to get roi %d\n", rc);
461 return rc;
462 }
463
464 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi.x, wb_roi.y,
465 wb_roi.w, wb_roi.h);
466
Alan Kwong4212dd42017-09-19 17:22:33 -0400467 /* bypass check if commit with no framebuffer */
Alan Kwongbb27c092016-07-20 16:41:25 -0400468 fb = sde_wb_connector_state_get_output_fb(conn_state);
469 if (!fb) {
Alan Kwong4212dd42017-09-19 17:22:33 -0400470 SDE_DEBUG("no output framebuffer\n");
471 return 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400472 }
473
474 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
475 fb->width, fb->height);
476
477 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
478 drm_format_num_planes(fb->pixel_format));
479 if (!fmt) {
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400480 SDE_ERROR("unsupported output pixel format:%x\n",
Alan Kwongbb27c092016-07-20 16:41:25 -0400481 fb->pixel_format);
482 return -EINVAL;
483 }
484
485 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
486 fb->modifier[0]);
487
488 if (SDE_FORMAT_IS_YUV(fmt) &&
489 !(wb_cfg->features & BIT(SDE_WB_YUV_CONFIG))) {
490 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
491 return -EINVAL;
492 }
493
494 if (SDE_FORMAT_IS_UBWC(fmt) &&
Clarence Ip32bcb002017-03-13 12:26:44 -0700495 !(wb_cfg->features & BIT(SDE_WB_UBWC))) {
Alan Kwongbb27c092016-07-20 16:41:25 -0400496 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
497 return -EINVAL;
498 }
499
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400500 if (SDE_FORMAT_IS_YUV(fmt) != !!phys_enc->hw_cdm)
501 crtc_state->mode_changed = true;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400502
Alan Kwongbb27c092016-07-20 16:41:25 -0400503 if (wb_roi.w && wb_roi.h) {
504 if (wb_roi.w != mode->hdisplay) {
505 SDE_ERROR("invalid roi w=%d, mode w=%d\n", wb_roi.w,
506 mode->hdisplay);
507 return -EINVAL;
508 } else if (wb_roi.h != mode->vdisplay) {
509 SDE_ERROR("invalid roi h=%d, mode h=%d\n", wb_roi.h,
510 mode->vdisplay);
511 return -EINVAL;
512 } else if (wb_roi.x + wb_roi.w > fb->width) {
513 SDE_ERROR("invalid roi x=%d, w=%d, fb w=%d\n",
514 wb_roi.x, wb_roi.w, fb->width);
515 return -EINVAL;
516 } else if (wb_roi.y + wb_roi.h > fb->height) {
517 SDE_ERROR("invalid roi y=%d, h=%d, fb h=%d\n",
518 wb_roi.y, wb_roi.h, fb->height);
519 return -EINVAL;
520 } else if (wb_roi.w > wb_cfg->sblk->maxlinewidth) {
521 SDE_ERROR("invalid roi w=%d, maxlinewidth=%u\n",
522 wb_roi.w, wb_cfg->sblk->maxlinewidth);
523 return -EINVAL;
524 }
525 } else {
526 if (wb_roi.x || wb_roi.y) {
527 SDE_ERROR("invalid roi x=%d, y=%d\n",
528 wb_roi.x, wb_roi.y);
529 return -EINVAL;
530 } else if (fb->width != mode->hdisplay) {
531 SDE_ERROR("invalid fb w=%d, mode w=%d\n", fb->width,
532 mode->hdisplay);
533 return -EINVAL;
534 } else if (fb->height != mode->vdisplay) {
535 SDE_ERROR("invalid fb h=%d, mode h=%d\n", fb->height,
536 mode->vdisplay);
537 return -EINVAL;
538 } else if (fb->width > wb_cfg->sblk->maxlinewidth) {
539 SDE_ERROR("invalid fb w=%d, maxlinewidth=%u\n",
540 fb->width, wb_cfg->sblk->maxlinewidth);
541 return -EINVAL;
542 }
543 }
544
545 return 0;
546}
547
548/**
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700549 * _sde_encoder_phys_wb_update_flush - flush hardware update
Alan Kwongbb27c092016-07-20 16:41:25 -0400550 * @phys_enc: Pointer to physical encoder
551 */
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700552static void _sde_encoder_phys_wb_update_flush(struct sde_encoder_phys *phys_enc)
Alan Kwongbb27c092016-07-20 16:41:25 -0400553{
554 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Lloyd Atkinson6a5359d2017-06-21 10:18:08 -0400555 struct sde_hw_wb *hw_wb;
556 struct sde_hw_ctl *hw_ctl;
557 struct sde_hw_cdm *hw_cdm;
Alan Kwongbb27c092016-07-20 16:41:25 -0400558 u32 flush_mask = 0;
559
Lloyd Atkinson6a5359d2017-06-21 10:18:08 -0400560 if (!phys_enc)
561 return;
562
563 hw_wb = wb_enc->hw_wb;
564 hw_ctl = phys_enc->hw_ctl;
565 hw_cdm = phys_enc->hw_cdm;
566
Alan Kwongbb27c092016-07-20 16:41:25 -0400567 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
568
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400569 if (!hw_ctl) {
570 SDE_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
571 return;
572 }
573
Alan Kwongbb27c092016-07-20 16:41:25 -0400574 if (hw_ctl->ops.get_bitmask_wb)
575 hw_ctl->ops.get_bitmask_wb(hw_ctl, &flush_mask, hw_wb->idx);
576
577 if (hw_ctl->ops.get_bitmask_cdm && hw_cdm)
578 hw_ctl->ops.get_bitmask_cdm(hw_ctl, &flush_mask, hw_cdm->idx);
579
580 if (hw_ctl->ops.update_pending_flush)
581 hw_ctl->ops.update_pending_flush(hw_ctl, flush_mask);
582
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700583 if (hw_ctl->ops.get_pending_flush)
584 flush_mask = hw_ctl->ops.get_pending_flush(hw_ctl);
585
586 SDE_DEBUG("Pending flush mask for CTL_%d is 0x%x, WB %d\n",
Alan Kwongbb27c092016-07-20 16:41:25 -0400587 hw_ctl->idx - CTL_0, flush_mask, hw_wb->idx - WB_0);
588}
589
590/**
591 * sde_encoder_phys_wb_setup - setup writeback encoder
592 * @phys_enc: Pointer to physical encoder
593 */
594static void sde_encoder_phys_wb_setup(
595 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;
599 struct drm_display_mode mode = phys_enc->cached_mode;
600 struct drm_framebuffer *fb;
601 struct sde_rect *wb_roi = &wb_enc->wb_roi;
602
603 SDE_DEBUG("[mode_set:%d,%d,\"%s\",%d,%d]\n",
604 hw_wb->idx - WB_0, mode.base.id, mode.name,
605 mode.hdisplay, mode.vdisplay);
606
607 memset(wb_roi, 0, sizeof(struct sde_rect));
608
Alan Kwong03b89842017-08-17 16:32:45 -0400609 /* clear writeback framebuffer - will be updated in setup_fb */
610 wb_enc->wb_fb = NULL;
611 wb_enc->wb_aspace = NULL;
612
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700613 if (phys_enc->enable_state == SDE_ENC_DISABLING) {
614 fb = wb_enc->fb_disable;
615 wb_roi->w = 0;
616 wb_roi->h = 0;
617 } else {
618 fb = sde_wb_get_output_fb(wb_enc->wb_dev);
619 sde_wb_get_output_roi(wb_enc->wb_dev, wb_roi);
620 }
621
Alan Kwongbb27c092016-07-20 16:41:25 -0400622 if (!fb) {
623 SDE_DEBUG("no output framebuffer\n");
624 return;
625 }
626
627 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
628 fb->width, fb->height);
629
Alan Kwongbb27c092016-07-20 16:41:25 -0400630 if (wb_roi->w == 0 || wb_roi->h == 0) {
631 wb_roi->x = 0;
632 wb_roi->y = 0;
633 wb_roi->w = fb->width;
634 wb_roi->h = fb->height;
635 }
636
637 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi->x, wb_roi->y,
638 wb_roi->w, wb_roi->h);
639
640 wb_enc->wb_fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
641 drm_format_num_planes(fb->pixel_format));
642 if (!wb_enc->wb_fmt) {
643 SDE_ERROR("unsupported output pixel format: %d\n",
644 fb->pixel_format);
645 return;
646 }
647
648 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
649 fb->modifier[0]);
650
Alan Kwong5d324e42016-07-28 22:56:18 -0400651 sde_encoder_phys_wb_set_ot_limit(phys_enc);
652
Alan Kwongbb27c092016-07-20 16:41:25 -0400653 sde_encoder_phys_wb_set_traffic_shaper(phys_enc);
654
Alan Kwonga62eeb82017-04-19 08:57:55 -0700655 sde_encoder_phys_wb_set_qos_remap(phys_enc);
656
Alan Kwongbb27c092016-07-20 16:41:25 -0400657 sde_encoder_phys_setup_cdm(phys_enc, fb, wb_enc->wb_fmt, wb_roi);
658
659 sde_encoder_phys_wb_setup_fb(phys_enc, fb, wb_roi);
660
661 sde_encoder_phys_wb_setup_cdp(phys_enc);
662}
663
664/**
665 * sde_encoder_phys_wb_unregister_irq - unregister writeback interrupt handler
666 * @phys_enc: Pointer to physical encoder
667 */
668static int sde_encoder_phys_wb_unregister_irq(
669 struct sde_encoder_phys *phys_enc)
670{
671 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
672 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
673
674 if (wb_enc->bypass_irqreg)
675 return 0;
676
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400677 sde_core_irq_disable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwonga172ef52016-09-27 00:29:10 -0400678 sde_core_irq_unregister_callback(phys_enc->sde_kms, wb_enc->irq_idx,
679 &wb_enc->irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400680
681 SDE_DEBUG("un-register IRQ for wb %d, irq_idx=%d\n",
682 hw_wb->idx - WB_0,
683 wb_enc->irq_idx);
684
685 return 0;
686}
687
688/**
689 * sde_encoder_phys_wb_done_irq - writeback interrupt handler
690 * @arg: Pointer to writeback encoder
691 * @irq_idx: interrupt index
692 */
693static void sde_encoder_phys_wb_done_irq(void *arg, int irq_idx)
694{
695 struct sde_encoder_phys_wb *wb_enc = arg;
696 struct sde_encoder_phys *phys_enc = &wb_enc->base;
697 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -0700698 u32 event = 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400699
700 SDE_DEBUG("[wb:%d,%u]\n", hw_wb->idx - WB_0,
701 wb_enc->frame_count);
702
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700703 /* don't notify upper layer for internal commit */
704 if (phys_enc->enable_state == SDE_ENC_DISABLING)
705 goto complete;
706
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -0700707 event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE
708 | SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE
709 | SDE_ENCODER_FRAME_EVENT_DONE;
710
711 atomic_add_unless(&phys_enc->pending_retire_fence_cnt, -1, 0);
Alan Kwong628d19e2016-10-31 13:50:13 -0400712 if (phys_enc->parent_ops.handle_frame_done)
713 phys_enc->parent_ops.handle_frame_done(phys_enc->parent,
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -0700714 phys_enc, event);
Alan Kwongbb27c092016-07-20 16:41:25 -0400715
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -0700716 if (phys_enc->parent_ops.handle_vblank_virt)
717 phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
718 phys_enc);
719
720 SDE_EVT32_IRQ(DRMID(phys_enc->parent), hw_wb->idx - WB_0, event);
Alan Kwong628d19e2016-10-31 13:50:13 -0400721
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700722complete:
Alan Kwong628d19e2016-10-31 13:50:13 -0400723 complete_all(&wb_enc->wbdone_complete);
Alan Kwongbb27c092016-07-20 16:41:25 -0400724}
725
726/**
727 * sde_encoder_phys_wb_register_irq - register writeback interrupt handler
728 * @phys_enc: Pointer to physical encoder
729 */
730static int sde_encoder_phys_wb_register_irq(struct sde_encoder_phys *phys_enc)
731{
732 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
733 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwonga172ef52016-09-27 00:29:10 -0400734 struct sde_irq_callback *irq_cb = &wb_enc->irq_cb;
Alan Kwongbb27c092016-07-20 16:41:25 -0400735 enum sde_intr_type intr_type;
736 int ret = 0;
737
738 if (wb_enc->bypass_irqreg)
739 return 0;
740
741 intr_type = sde_encoder_phys_wb_get_intr_type(hw_wb);
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400742 wb_enc->irq_idx = sde_core_irq_idx_lookup(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400743 intr_type, hw_wb->idx);
744 if (wb_enc->irq_idx < 0) {
745 SDE_ERROR(
746 "failed to lookup IRQ index for WB_DONE with wb=%d\n",
747 hw_wb->idx - WB_0);
748 return -EINVAL;
749 }
750
Alan Kwonga172ef52016-09-27 00:29:10 -0400751 irq_cb->func = sde_encoder_phys_wb_done_irq;
752 irq_cb->arg = wb_enc;
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400753 ret = sde_core_irq_register_callback(phys_enc->sde_kms,
Alan Kwonga172ef52016-09-27 00:29:10 -0400754 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400755 if (ret) {
756 SDE_ERROR("failed to register IRQ callback WB_DONE\n");
757 return ret;
758 }
759
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400760 ret = sde_core_irq_enable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwongbb27c092016-07-20 16:41:25 -0400761 if (ret) {
762 SDE_ERROR(
763 "failed to enable IRQ for WB_DONE, wb %d, irq_idx=%d\n",
764 hw_wb->idx - WB_0,
765 wb_enc->irq_idx);
766 wb_enc->irq_idx = -EINVAL;
767
768 /* Unregister callback on IRQ enable failure */
Alan Kwonga172ef52016-09-27 00:29:10 -0400769 sde_core_irq_unregister_callback(phys_enc->sde_kms,
770 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400771 return ret;
772 }
773
774 SDE_DEBUG("registered IRQ for wb %d, irq_idx=%d\n",
775 hw_wb->idx - WB_0,
776 wb_enc->irq_idx);
777
778 return ret;
779}
780
781/**
782 * sde_encoder_phys_wb_mode_set - set display mode
783 * @phys_enc: Pointer to physical encoder
784 * @mode: Pointer to requested display mode
785 * @adj_mode: Pointer to adjusted display mode
786 */
787static void sde_encoder_phys_wb_mode_set(
788 struct sde_encoder_phys *phys_enc,
789 struct drm_display_mode *mode,
790 struct drm_display_mode *adj_mode)
791{
792 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400793 struct sde_rm *rm = &phys_enc->sde_kms->rm;
Alan Kwongbb27c092016-07-20 16:41:25 -0400794 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400795 struct sde_rm_hw_iter iter;
796 int i, instance;
Alan Kwongbb27c092016-07-20 16:41:25 -0400797
798 phys_enc->cached_mode = *adj_mode;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400799 instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400800
801 SDE_DEBUG("[mode_set_cache:%d,%d,\"%s\",%d,%d]\n",
802 hw_wb->idx - WB_0, mode->base.id,
803 mode->name, mode->hdisplay, mode->vdisplay);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400804
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400805 phys_enc->hw_ctl = NULL;
806 phys_enc->hw_cdm = NULL;
807
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400808 /* Retrieve previously allocated HW Resources. CTL shouldn't fail */
809 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
810 for (i = 0; i <= instance; i++) {
811 sde_rm_get_hw(rm, &iter);
812 if (i == instance)
813 phys_enc->hw_ctl = (struct sde_hw_ctl *) iter.hw;
814 }
815
816 if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
817 SDE_ERROR("failed init ctl: %ld\n", PTR_ERR(phys_enc->hw_ctl));
818 phys_enc->hw_ctl = NULL;
819 return;
820 }
821
822 /* CDM is optional */
823 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CDM);
824 for (i = 0; i <= instance; i++) {
825 sde_rm_get_hw(rm, &iter);
826 if (i == instance)
827 phys_enc->hw_cdm = (struct sde_hw_cdm *) iter.hw;
828 }
829
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400830 if (IS_ERR(phys_enc->hw_cdm)) {
831 SDE_ERROR("CDM required but not allocated: %ld\n",
832 PTR_ERR(phys_enc->hw_cdm));
833 phys_enc->hw_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400834 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400835}
836
837/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400838 * sde_encoder_phys_wb_wait_for_commit_done - wait until request is committed
839 * @phys_enc: Pointer to physical encoder
840 */
841static int sde_encoder_phys_wb_wait_for_commit_done(
842 struct sde_encoder_phys *phys_enc)
843{
844 unsigned long ret;
845 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -0700846 u32 irq_status, event = 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400847 u64 wb_time = 0;
848 int rc = 0;
Alan Kwong5a50a4a2017-01-24 19:12:51 -0800849 u32 timeout = max_t(u32, wb_enc->wbdone_timeout, KICKOFF_TIMEOUT_MS);
Alan Kwongbb27c092016-07-20 16:41:25 -0400850
851 /* Return EWOULDBLOCK since we know the wait isn't necessary */
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700852 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
853 SDE_ERROR("encoder already disabled\n");
Alan Kwongbb27c092016-07-20 16:41:25 -0400854 return -EWOULDBLOCK;
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700855 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400856
Alan Kwong4212dd42017-09-19 17:22:33 -0400857 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
858 !!wb_enc->wb_fb);
859
860 /* signal completion if commit with no framebuffer */
861 if (!wb_enc->wb_fb) {
862 SDE_DEBUG("no output framebuffer\n");
863 sde_encoder_phys_wb_done_irq(wb_enc, wb_enc->irq_idx);
864 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400865
866 ret = wait_for_completion_timeout(&wb_enc->wbdone_complete,
Alan Kwong5a50a4a2017-01-24 19:12:51 -0800867 msecs_to_jiffies(timeout));
Alan Kwongbb27c092016-07-20 16:41:25 -0400868
869 if (!ret) {
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400870 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
871 wb_enc->frame_count);
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400872 irq_status = sde_core_irq_read(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400873 wb_enc->irq_idx, true);
874 if (irq_status) {
875 SDE_DEBUG("wb:%d done but irq not triggered\n",
Alan Kwong1124f1f2017-11-10 18:14:39 -0500876 WBID(wb_enc));
Alan Kwongbb27c092016-07-20 16:41:25 -0400877 sde_encoder_phys_wb_done_irq(wb_enc, wb_enc->irq_idx);
878 } else {
879 SDE_ERROR("wb:%d kickoff timed out\n",
Alan Kwong1124f1f2017-11-10 18:14:39 -0500880 WBID(wb_enc));
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -0700881 atomic_add_unless(
882 &phys_enc->pending_retire_fence_cnt, -1, 0);
883
884 event = SDE_ENCODER_FRAME_EVENT_SIGNAL_RELEASE_FENCE
885 | SDE_ENCODER_FRAME_EVENT_SIGNAL_RETIRE_FENCE
886 | SDE_ENCODER_FRAME_EVENT_ERROR;
Alan Kwong628d19e2016-10-31 13:50:13 -0400887 if (phys_enc->parent_ops.handle_frame_done)
888 phys_enc->parent_ops.handle_frame_done(
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -0700889 phys_enc->parent, phys_enc, event);
Alan Kwongbb27c092016-07-20 16:41:25 -0400890 rc = -ETIMEDOUT;
891 }
892 }
893
894 sde_encoder_phys_wb_unregister_irq(phys_enc);
895
896 if (!rc)
897 wb_enc->end_time = ktime_get();
898
899 /* once operation is done, disable traffic shaper */
900 if (wb_enc->wb_cfg.ts_cfg.en && wb_enc->hw_wb &&
901 wb_enc->hw_wb->ops.setup_trafficshaper) {
902 wb_enc->wb_cfg.ts_cfg.en = false;
903 wb_enc->hw_wb->ops.setup_trafficshaper(
904 wb_enc->hw_wb, &wb_enc->wb_cfg);
905 }
906
907 /* remove vote for iommu/clk/bus */
908 wb_enc->frame_count++;
909
910 if (!rc) {
911 wb_time = (u64)ktime_to_us(wb_enc->end_time) -
912 (u64)ktime_to_us(wb_enc->start_time);
Alan Kwong1124f1f2017-11-10 18:14:39 -0500913 SDE_DEBUG("wb:%d took %llu us\n", WBID(wb_enc), wb_time);
Alan Kwongbb27c092016-07-20 16:41:25 -0400914 }
915
Alan Kwong03b89842017-08-17 16:32:45 -0400916 /* cleanup writeback framebuffer */
917 if (wb_enc->wb_fb && wb_enc->wb_aspace) {
918 msm_framebuffer_cleanup(wb_enc->wb_fb, wb_enc->wb_aspace);
919 wb_enc->wb_fb = NULL;
920 wb_enc->wb_aspace = NULL;
921 }
922
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400923 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -0700924 wb_time, event, rc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400925
926 return rc;
927}
928
929/**
930 * sde_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
931 * @phys_enc: Pointer to physical encoder
Alan Kwong4aacd532017-02-04 18:51:33 -0800932 * @params: kickoff parameters
Clarence Ip85f4f4532017-10-04 12:10:13 -0400933 * Returns: Zero on success
Alan Kwongbb27c092016-07-20 16:41:25 -0400934 */
Clarence Ip85f4f4532017-10-04 12:10:13 -0400935static int sde_encoder_phys_wb_prepare_for_kickoff(
Alan Kwong4aacd532017-02-04 18:51:33 -0800936 struct sde_encoder_phys *phys_enc,
937 struct sde_encoder_kickoff_params *params)
Alan Kwongbb27c092016-07-20 16:41:25 -0400938{
939 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
940 int ret;
941
942 SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
943 wb_enc->kickoff_count);
944
Alan Kwongbb27c092016-07-20 16:41:25 -0400945 reinit_completion(&wb_enc->wbdone_complete);
946
947 ret = sde_encoder_phys_wb_register_irq(phys_enc);
948 if (ret) {
949 SDE_ERROR("failed to register irq %d\n", ret);
Clarence Ip85f4f4532017-10-04 12:10:13 -0400950 return ret;
Alan Kwongbb27c092016-07-20 16:41:25 -0400951 }
952
953 wb_enc->kickoff_count++;
954
955 /* set OT limit & enable traffic shaper */
956 sde_encoder_phys_wb_setup(phys_enc);
957
Clarence Ip9c65f7b2017-03-20 06:48:15 -0700958 _sde_encoder_phys_wb_update_flush(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400959
960 /* vote for iommu/clk/bus */
961 wb_enc->start_time = ktime_get();
962
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500963 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->kickoff_count);
Clarence Ip85f4f4532017-10-04 12:10:13 -0400964 return 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400965}
966
967/**
Alan Kwong4212dd42017-09-19 17:22:33 -0400968 * sde_encoder_phys_wb_trigger_flush - trigger flush processing
969 * @phys_enc: Pointer to physical encoder
970 */
971static void sde_encoder_phys_wb_trigger_flush(struct sde_encoder_phys *phys_enc)
972{
973 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
974
975 if (!phys_enc || !wb_enc->hw_wb) {
976 SDE_ERROR("invalid encoder\n");
977 return;
978 }
979
980 SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
981
982 /* clear pending flush if commit with no framebuffer */
983 if (!wb_enc->wb_fb) {
984 SDE_DEBUG("no output framebuffer\n");
985 return;
986 }
987
988 sde_encoder_helper_trigger_flush(phys_enc);
989}
990
991/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400992 * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
993 * @phys_enc: Pointer to physical encoder
994 */
995static void sde_encoder_phys_wb_handle_post_kickoff(
996 struct sde_encoder_phys *phys_enc)
997{
998 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
999
1000 SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
1001
Lloyd Atkinson5d40d312016-09-06 08:34:13 -04001002 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc));
Alan Kwongbb27c092016-07-20 16:41:25 -04001003}
1004
1005/**
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001006 * _sde_encoder_phys_wb_init_internal_fb - create fb for internal commit
1007 * @wb_enc: Pointer to writeback encoder
1008 * @pixel_format: DRM pixel format
1009 * @width: Desired fb width
1010 * @height: Desired fb height
Narendra Muppalla58a64e22017-07-24 10:54:47 -07001011 * @pitch: Desired fb pitch
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001012 */
1013static int _sde_encoder_phys_wb_init_internal_fb(
1014 struct sde_encoder_phys_wb *wb_enc,
Narendra Muppalla58a64e22017-07-24 10:54:47 -07001015 uint32_t pixel_format, uint32_t width,
1016 uint32_t height, uint32_t pitch)
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001017{
1018 struct drm_device *dev;
1019 struct drm_framebuffer *fb;
1020 struct drm_mode_fb_cmd2 mode_cmd;
1021 uint32_t size;
1022 int nplanes, i, ret;
Jordan Croused8e96522017-02-13 10:14:16 -07001023 struct msm_gem_address_space *aspace;
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001024
1025 if (!wb_enc || !wb_enc->base.parent || !wb_enc->base.sde_kms) {
1026 SDE_ERROR("invalid params\n");
1027 return -EINVAL;
1028 }
1029
Jordan Croused8e96522017-02-13 10:14:16 -07001030 aspace = wb_enc->base.sde_kms->aspace[SDE_IOMMU_DOMAIN_UNSECURE];
1031 if (!aspace) {
1032 SDE_ERROR("invalid address space\n");
1033 return -EINVAL;
1034 }
1035
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001036 dev = wb_enc->base.sde_kms->dev;
1037 if (!dev) {
1038 SDE_ERROR("invalid dev\n");
1039 return -EINVAL;
1040 }
1041
1042 memset(&mode_cmd, 0, sizeof(mode_cmd));
1043 mode_cmd.pixel_format = pixel_format;
1044 mode_cmd.width = width;
1045 mode_cmd.height = height;
Narendra Muppalla58a64e22017-07-24 10:54:47 -07001046 mode_cmd.pitches[0] = pitch;
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001047
1048 size = sde_format_get_framebuffer_size(pixel_format,
Narendra Muppalla58a64e22017-07-24 10:54:47 -07001049 mode_cmd.width, mode_cmd.height,
1050 mode_cmd.pitches, NULL, 0);
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001051 if (!size) {
1052 SDE_DEBUG("not creating zero size buffer\n");
1053 return -EINVAL;
1054 }
1055
1056 /* allocate gem tracking object */
1057 nplanes = drm_format_num_planes(pixel_format);
1058 if (nplanes > SDE_MAX_PLANES) {
1059 SDE_ERROR("requested format has too many planes\n");
1060 return -EINVAL;
1061 }
1062 mutex_lock(&dev->struct_mutex);
1063 wb_enc->bo_disable[0] = msm_gem_new(dev, size,
1064 MSM_BO_SCANOUT | MSM_BO_WC);
1065 mutex_unlock(&dev->struct_mutex);
1066
1067 if (IS_ERR_OR_NULL(wb_enc->bo_disable[0])) {
1068 ret = PTR_ERR(wb_enc->bo_disable[0]);
1069 wb_enc->bo_disable[0] = NULL;
1070
1071 SDE_ERROR("failed to create bo, %d\n", ret);
1072 return ret;
1073 }
1074
1075 for (i = 0; i < nplanes; ++i) {
1076 wb_enc->bo_disable[i] = wb_enc->bo_disable[0];
1077 mode_cmd.pitches[i] = width *
1078 drm_format_plane_cpp(pixel_format, i);
1079 }
1080
1081 fb = msm_framebuffer_init(dev, &mode_cmd, wb_enc->bo_disable);
1082 if (IS_ERR_OR_NULL(fb)) {
1083 ret = PTR_ERR(fb);
1084 drm_gem_object_unreference(wb_enc->bo_disable[0]);
1085 wb_enc->bo_disable[0] = NULL;
1086
1087 SDE_ERROR("failed to init fb, %d\n", ret);
1088 return ret;
1089 }
1090
1091 /* prepare the backing buffer now so that it's available later */
Jordan Croused8e96522017-02-13 10:14:16 -07001092 ret = msm_framebuffer_prepare(fb, aspace);
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001093 if (!ret)
1094 wb_enc->fb_disable = fb;
1095 return ret;
1096}
1097
1098/**
1099 * _sde_encoder_phys_wb_destroy_internal_fb - deconstruct internal fb
1100 * @wb_enc: Pointer to writeback encoder
1101 */
1102static void _sde_encoder_phys_wb_destroy_internal_fb(
1103 struct sde_encoder_phys_wb *wb_enc)
1104{
1105 if (!wb_enc)
1106 return;
1107
1108 if (wb_enc->fb_disable) {
1109 drm_framebuffer_unregister_private(wb_enc->fb_disable);
1110 drm_framebuffer_remove(wb_enc->fb_disable);
1111 wb_enc->fb_disable = NULL;
1112 }
1113
1114 if (wb_enc->bo_disable[0]) {
1115 drm_gem_object_unreference(wb_enc->bo_disable[0]);
1116 wb_enc->bo_disable[0] = NULL;
1117 }
1118}
1119
1120/**
Alan Kwongbb27c092016-07-20 16:41:25 -04001121 * sde_encoder_phys_wb_enable - enable writeback encoder
1122 * @phys_enc: Pointer to physical encoder
1123 */
1124static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
1125{
1126 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1127 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwong74e8ba32016-09-30 14:25:16 -04001128 struct drm_device *dev;
Alan Kwongbb27c092016-07-20 16:41:25 -04001129 struct drm_connector *connector;
1130
1131 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
1132
Alan Kwong74e8ba32016-09-30 14:25:16 -04001133 if (!wb_enc->base.parent || !wb_enc->base.parent->dev) {
1134 SDE_ERROR("invalid drm device\n");
1135 return;
1136 }
1137 dev = wb_enc->base.parent->dev;
1138
Alan Kwongbb27c092016-07-20 16:41:25 -04001139 /* find associated writeback connector */
Clarence Ip05431f32016-11-25 16:39:38 -05001140 connector = phys_enc->connector;
Alan Kwong74e8ba32016-09-30 14:25:16 -04001141
Alan Kwongbb27c092016-07-20 16:41:25 -04001142 if (!connector || connector->encoder != phys_enc->parent) {
1143 SDE_ERROR("failed to find writeback connector\n");
1144 return;
1145 }
1146 wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
1147
1148 phys_enc->enable_state = SDE_ENC_ENABLED;
Harsh Sahufae4d812017-11-29 15:08:59 -08001149
1150 /*
1151 * cache the crtc in wb_enc on enable for duration of use case
1152 * for correctly servicing asynchronous irq events and timers
1153 */
1154 wb_enc->crtc = phys_enc->parent->crtc;
Alan Kwongbb27c092016-07-20 16:41:25 -04001155}
1156
1157/**
1158 * sde_encoder_phys_wb_disable - disable writeback encoder
1159 * @phys_enc: Pointer to physical encoder
1160 */
1161static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
1162{
1163 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1164 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
1165
1166 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
1167
1168 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
1169 SDE_ERROR("encoder is already disabled\n");
1170 return;
1171 }
1172
1173 if (wb_enc->frame_count != wb_enc->kickoff_count) {
1174 SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
1175 hw_wb->idx - WB_0, wb_enc->frame_count,
1176 wb_enc->kickoff_count);
1177 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
1178 }
1179
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001180 if (!phys_enc->hw_ctl || !phys_enc->parent ||
1181 !phys_enc->sde_kms || !wb_enc->fb_disable) {
1182 SDE_DEBUG("invalid enc, skipping extra commit\n");
1183 goto exit;
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001184 }
1185
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001186 /* reset h/w before final flush */
Clarence Ip662698e2017-09-12 18:34:16 -04001187 if (phys_enc->hw_ctl->ops.clear_pending_flush)
1188 phys_enc->hw_ctl->ops.clear_pending_flush(phys_enc->hw_ctl);
1189 if (sde_encoder_helper_reset_mixers(phys_enc, wb_enc->fb_disable))
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001190 goto exit;
1191
1192 phys_enc->enable_state = SDE_ENC_DISABLING;
Alan Kwong4aacd532017-02-04 18:51:33 -08001193 sde_encoder_phys_wb_prepare_for_kickoff(phys_enc, NULL);
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001194 if (phys_enc->hw_ctl->ops.trigger_flush)
1195 phys_enc->hw_ctl->ops.trigger_flush(phys_enc->hw_ctl);
1196 sde_encoder_helper_trigger_start(phys_enc);
1197 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
1198exit:
Alan Kwongbb27c092016-07-20 16:41:25 -04001199 phys_enc->enable_state = SDE_ENC_DISABLED;
Harsh Sahufae4d812017-11-29 15:08:59 -08001200 wb_enc->crtc = NULL;
Alan Kwongbb27c092016-07-20 16:41:25 -04001201}
1202
1203/**
1204 * sde_encoder_phys_wb_get_hw_resources - get hardware resources
1205 * @phys_enc: Pointer to physical encoder
1206 * @hw_res: Pointer to encoder resources
1207 */
1208static void sde_encoder_phys_wb_get_hw_resources(
1209 struct sde_encoder_phys *phys_enc,
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001210 struct sde_encoder_hw_resources *hw_res,
1211 struct drm_connector_state *conn_state)
Alan Kwongbb27c092016-07-20 16:41:25 -04001212{
1213 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -04001214 struct sde_hw_wb *hw_wb;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001215 struct drm_framebuffer *fb;
Alan Kwong4212dd42017-09-19 17:22:33 -04001216 const struct sde_format *fmt = NULL;
Alan Kwongbb27c092016-07-20 16:41:25 -04001217
Clarence Ip03521982016-08-26 10:49:47 -04001218 if (!phys_enc) {
1219 SDE_ERROR("invalid encoder\n");
1220 return;
1221 }
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001222
1223 fb = sde_wb_connector_state_get_output_fb(conn_state);
Alan Kwong4212dd42017-09-19 17:22:33 -04001224 if (fb) {
1225 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
1226 drm_format_num_planes(fb->pixel_format));
1227 if (!fmt) {
1228 SDE_ERROR("unsupported output pixel format:%d\n",
1229 fb->pixel_format);
1230 return;
1231 }
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001232 }
1233
Clarence Ip03521982016-08-26 10:49:47 -04001234 hw_wb = wb_enc->hw_wb;
Clarence Ip03521982016-08-26 10:49:47 -04001235 hw_res->wbs[hw_wb->idx - WB_0] = phys_enc->intf_mode;
Alan Kwong4212dd42017-09-19 17:22:33 -04001236 hw_res->needs_cdm = fmt ? SDE_FORMAT_IS_YUV(fmt) : false;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -04001237 SDE_DEBUG("[wb:%d] intf_mode=%d needs_cdm=%d\n", hw_wb->idx - WB_0,
1238 hw_res->wbs[hw_wb->idx - WB_0],
1239 hw_res->needs_cdm);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001240}
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001241
Alan Kwongbb27c092016-07-20 16:41:25 -04001242#ifdef CONFIG_DEBUG_FS
1243/**
1244 * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001245 * @phys_enc: Pointer to physical encoder
1246 * @debugfs_root: Pointer to virtual encoder's debugfs_root dir
Alan Kwongbb27c092016-07-20 16:41:25 -04001247 */
1248static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001249 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001250{
1251 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1252
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001253 if (!phys_enc || !wb_enc->hw_wb || !debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001254 return -EINVAL;
1255
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04001256 if (!debugfs_create_u32("wbdone_timeout", 0600,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001257 debugfs_root, &wb_enc->wbdone_timeout)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001258 SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
1259 return -ENOMEM;
1260 }
1261
Lloyd Atkinson8de415a2017-05-23 11:31:16 -04001262 if (!debugfs_create_u32("bypass_irqreg", 0600,
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001263 debugfs_root, &wb_enc->bypass_irqreg)) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001264 SDE_ERROR("failed to create debugfs/bypass_irqreg\n");
1265 return -ENOMEM;
1266 }
1267
1268 return 0;
1269}
Alan Kwongbb27c092016-07-20 16:41:25 -04001270#else
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001271static int sde_encoder_phys_wb_init_debugfs(
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001272 struct sde_encoder_phys *phys_enc, struct dentry *debugfs_root)
Alan Kwongbb27c092016-07-20 16:41:25 -04001273{
Dhaval Patel48f2d0f2016-09-27 16:39:12 -07001274 return 0;
Alan Kwongbb27c092016-07-20 16:41:25 -04001275}
Alan Kwongbb27c092016-07-20 16:41:25 -04001276#endif
1277
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001278static int sde_encoder_phys_wb_late_register(struct sde_encoder_phys *phys_enc,
1279 struct dentry *debugfs_root)
1280{
1281 return sde_encoder_phys_wb_init_debugfs(phys_enc, debugfs_root);
1282}
1283
Alan Kwongbb27c092016-07-20 16:41:25 -04001284/**
1285 * sde_encoder_phys_wb_destroy - destroy writeback encoder
1286 * @phys_enc: Pointer to physical encoder
1287 */
1288static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
1289{
1290 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
1291 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
1292
1293 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
1294
1295 if (!phys_enc)
1296 return;
1297
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001298 _sde_encoder_phys_wb_destroy_internal_fb(wb_enc);
1299
Alan Kwongbb27c092016-07-20 16:41:25 -04001300 kfree(wb_enc);
1301}
1302
1303/**
1304 * sde_encoder_phys_wb_init_ops - initialize writeback operations
1305 * @ops: Pointer to encoder operation table
1306 */
1307static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
1308{
Lloyd Atkinsonb020e0f2017-03-14 08:05:18 -07001309 ops->late_register = sde_encoder_phys_wb_late_register;
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -04001310 ops->is_master = sde_encoder_phys_wb_is_master;
Alan Kwongbb27c092016-07-20 16:41:25 -04001311 ops->mode_set = sde_encoder_phys_wb_mode_set;
1312 ops->enable = sde_encoder_phys_wb_enable;
1313 ops->disable = sde_encoder_phys_wb_disable;
1314 ops->destroy = sde_encoder_phys_wb_destroy;
1315 ops->atomic_check = sde_encoder_phys_wb_atomic_check;
1316 ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
Alan Kwongbb27c092016-07-20 16:41:25 -04001317 ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
1318 ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
1319 ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
Alan Kwong4212dd42017-09-19 17:22:33 -04001320 ops->trigger_flush = sde_encoder_phys_wb_trigger_flush;
Clarence Ip110d15c2016-08-16 14:44:41 -04001321 ops->trigger_start = sde_encoder_helper_trigger_start;
Lloyd Atkinson8c49c582016-11-18 14:23:54 -05001322 ops->hw_reset = sde_encoder_helper_hw_reset;
Alan Kwongbb27c092016-07-20 16:41:25 -04001323}
1324
1325/**
1326 * sde_encoder_phys_wb_init - initialize writeback encoder
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001327 * @init: Pointer to init info structure with initialization params
Alan Kwongbb27c092016-07-20 16:41:25 -04001328 */
1329struct sde_encoder_phys *sde_encoder_phys_wb_init(
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001330 struct sde_enc_phys_init_params *p)
Alan Kwongbb27c092016-07-20 16:41:25 -04001331{
1332 struct sde_encoder_phys *phys_enc;
1333 struct sde_encoder_phys_wb *wb_enc;
1334 struct sde_hw_mdp *hw_mdp;
1335 int ret = 0;
1336
1337 SDE_DEBUG("\n");
1338
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001339 if (!p || !p->parent) {
1340 SDE_ERROR("invalid params\n");
1341 ret = -EINVAL;
1342 goto fail_alloc;
1343 }
1344
Alan Kwongbb27c092016-07-20 16:41:25 -04001345 wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
1346 if (!wb_enc) {
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001347 SDE_ERROR("failed to allocate wb enc\n");
Alan Kwongbb27c092016-07-20 16:41:25 -04001348 ret = -ENOMEM;
1349 goto fail_alloc;
1350 }
1351 wb_enc->irq_idx = -EINVAL;
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -05001352 wb_enc->wbdone_timeout = KICKOFF_TIMEOUT_MS;
Alan Kwongbb27c092016-07-20 16:41:25 -04001353 init_completion(&wb_enc->wbdone_complete);
1354
1355 phys_enc = &wb_enc->base;
1356
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001357 if (p->sde_kms->vbif[VBIF_NRT]) {
Jordan Croused8e96522017-02-13 10:14:16 -07001358 wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE] =
1359 p->sde_kms->aspace[MSM_SMMU_DOMAIN_NRT_UNSECURE];
1360 wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] =
1361 p->sde_kms->aspace[MSM_SMMU_DOMAIN_NRT_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001362 } else {
Jordan Croused8e96522017-02-13 10:14:16 -07001363 wb_enc->aspace[SDE_IOMMU_DOMAIN_UNSECURE] =
1364 p->sde_kms->aspace[MSM_SMMU_DOMAIN_UNSECURE];
1365 wb_enc->aspace[SDE_IOMMU_DOMAIN_SECURE] =
1366 p->sde_kms->aspace[MSM_SMMU_DOMAIN_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001367 }
1368
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001369 hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
Alan Kwongbb27c092016-07-20 16:41:25 -04001370 if (IS_ERR_OR_NULL(hw_mdp)) {
1371 ret = PTR_ERR(hw_mdp);
1372 SDE_ERROR("failed to init hw_top: %d\n", ret);
1373 goto fail_mdp_init;
1374 }
1375 phys_enc->hw_mdptop = hw_mdp;
1376
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001377 /**
1378 * hw_wb resource permanently assigned to this encoder
1379 * Other resources allocated at atomic commit time by use case
1380 */
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001381 if (p->wb_idx != SDE_NONE) {
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001382 struct sde_rm_hw_iter iter;
Alan Kwongbb27c092016-07-20 16:41:25 -04001383
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001384 sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
1385 while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
1386 struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
1387
1388 if (hw_wb->idx == p->wb_idx) {
1389 wb_enc->hw_wb = hw_wb;
1390 break;
1391 }
1392 }
1393
1394 if (!wb_enc->hw_wb) {
1395 ret = -EINVAL;
1396 SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
Alan Kwongbb27c092016-07-20 16:41:25 -04001397 goto fail_wb_init;
1398 }
Alan Kwongbb27c092016-07-20 16:41:25 -04001399 } else {
1400 ret = -EINVAL;
1401 SDE_ERROR("invalid wb_idx\n");
1402 goto fail_wb_check;
1403 }
1404
Alan Kwongbb27c092016-07-20 16:41:25 -04001405 sde_encoder_phys_wb_init_ops(&phys_enc->ops);
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001406 phys_enc->parent = p->parent;
1407 phys_enc->parent_ops = p->parent_ops;
1408 phys_enc->sde_kms = p->sde_kms;
1409 phys_enc->split_role = p->split_role;
Clarence Ip03521982016-08-26 10:49:47 -04001410 phys_enc->intf_mode = INTF_MODE_WB_LINE;
Dhaval Patel81e87882016-10-19 21:41:56 -07001411 phys_enc->intf_idx = p->intf_idx;
Lloyd Atkinson7d070942016-07-26 18:35:12 -04001412 phys_enc->enc_spinlock = p->enc_spinlock;
Veera Sundaram Sankaran675ff622017-06-21 21:44:46 -07001413 atomic_set(&phys_enc->pending_retire_fence_cnt, 0);
Alan Kwonga172ef52016-09-27 00:29:10 -04001414 INIT_LIST_HEAD(&wb_enc->irq_cb.list);
Alan Kwongbb27c092016-07-20 16:41:25 -04001415
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001416 /* create internal buffer for disable logic */
1417 if (_sde_encoder_phys_wb_init_internal_fb(wb_enc,
Narendra Muppalla58a64e22017-07-24 10:54:47 -07001418 DRM_FORMAT_RGB888, 2, 1, 6)) {
Clarence Ip9c65f7b2017-03-20 06:48:15 -07001419 SDE_ERROR("failed to init internal fb\n");
1420 goto fail_wb_init;
1421 }
1422
Alan Kwongbb27c092016-07-20 16:41:25 -04001423 SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
1424 wb_enc->hw_wb->idx - WB_0);
1425
1426 return phys_enc;
1427
Alan Kwongbb27c092016-07-20 16:41:25 -04001428fail_wb_init:
1429fail_wb_check:
Alan Kwongbb27c092016-07-20 16:41:25 -04001430fail_mdp_init:
1431 kfree(wb_enc);
1432fail_alloc:
1433 return ERR_PTR(ret);
1434}
1435