blob: 768f59ca7f99c1a210d4d242992a46f029ac310e [file] [log] [blame]
Alan Kwongbb27c092016-07-20 16:41:25 -04001/*
Lloyd Atkinson8772e202016-09-26 17:52:16 -04002 * Copyright (c) 2015-2017 The Linux Foundation. All rights reserved.
Alan Kwongbb27c092016-07-20 16:41:25 -04003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 */
14
Alan Kwong4c3cf4c2016-09-25 20:08:09 -040015#define pr_fmt(fmt) "[drm:%s:%d] " fmt, __func__, __LINE__
Alan Kwongbb27c092016-07-20 16:41:25 -040016#include <linux/debugfs.h>
17
18#include "sde_encoder_phys.h"
19#include "sde_formats.h"
20#include "sde_hw_top.h"
21#include "sde_hw_interrupts.h"
Alan Kwongf5dd86c2016-08-09 18:08:17 -040022#include "sde_core_irq.h"
Alan Kwongbb27c092016-07-20 16:41:25 -040023#include "sde_wb.h"
Lloyd Atkinson8772e202016-09-26 17:52:16 -040024#include "sde_vbif.h"
Alan Kwongbb27c092016-07-20 16:41:25 -040025
Alan Kwongbb27c092016-07-20 16:41:25 -040026#define to_sde_encoder_phys_wb(x) \
27 container_of(x, struct sde_encoder_phys_wb, base)
28
Lloyd Atkinson5d40d312016-09-06 08:34:13 -040029#define WBID(wb_enc) ((wb_enc) ? wb_enc->wb_dev->wb_idx : -1)
Alan Kwongbb27c092016-07-20 16:41:25 -040030
31/**
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -040032 * sde_encoder_phys_wb_is_master - report wb always as master encoder
33 */
34static bool sde_encoder_phys_wb_is_master(struct sde_encoder_phys *phys_enc)
35{
36 return true;
37}
38
39/**
Alan Kwongbb27c092016-07-20 16:41:25 -040040 * sde_encoder_phys_wb_get_intr_type - get interrupt type based on block mode
41 * @hw_wb: Pointer to h/w writeback driver
42 */
43static enum sde_intr_type sde_encoder_phys_wb_get_intr_type(
44 struct sde_hw_wb *hw_wb)
45{
46 return (hw_wb->caps->features & BIT(SDE_WB_BLOCK_MODE)) ?
47 SDE_IRQ_TYPE_WB_ROT_COMP : SDE_IRQ_TYPE_WB_WFD_COMP;
48}
49
50/**
Alan Kwong5d324e42016-07-28 22:56:18 -040051 * sde_encoder_phys_wb_set_ot_limit - set OT limit for writeback interface
52 * @phys_enc: Pointer to physical encoder
53 */
54static void sde_encoder_phys_wb_set_ot_limit(
55 struct sde_encoder_phys *phys_enc)
56{
57 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
58 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
59 struct sde_vbif_set_ot_params ot_params;
60
61 memset(&ot_params, 0, sizeof(ot_params));
62 ot_params.xin_id = hw_wb->caps->xin_id;
63 ot_params.num = hw_wb->idx - WB_0;
64 ot_params.width = wb_enc->wb_roi.w;
65 ot_params.height = wb_enc->wb_roi.h;
66 ot_params.is_wfd = true;
67 ot_params.frame_rate = phys_enc->cached_mode.vrefresh;
68 ot_params.vbif_idx = hw_wb->caps->vbif_idx;
69 ot_params.clk_ctrl = hw_wb->caps->clk_ctrl;
70 ot_params.rd = false;
71
72 sde_vbif_set_ot_limit(phys_enc->sde_kms, &ot_params);
73}
74
75/**
Alan Kwongbb27c092016-07-20 16:41:25 -040076 * sde_encoder_phys_wb_set_traffic_shaper - set traffic shaper for writeback
77 * @phys_enc: Pointer to physical encoder
78 */
79static void sde_encoder_phys_wb_set_traffic_shaper(
80 struct sde_encoder_phys *phys_enc)
81{
82 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
83 struct sde_hw_wb_cfg *wb_cfg = &wb_enc->wb_cfg;
84
85 /* traffic shaper is only enabled for rotator */
86 wb_cfg->ts_cfg.en = false;
87}
88
89/**
90 * sde_encoder_phys_setup_cdm - setup chroma down block
91 * @phys_enc: Pointer to physical encoder
92 * @fb: Pointer to output framebuffer
93 * @format: Output format
94 */
95void sde_encoder_phys_setup_cdm(struct sde_encoder_phys *phys_enc,
96 struct drm_framebuffer *fb, const struct sde_format *format,
97 struct sde_rect *wb_roi)
98{
99 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
100 struct sde_hw_cdm_cfg *cdm_cfg = &phys_enc->cdm_cfg;
101 int ret;
102
103 if (!SDE_FORMAT_IS_YUV(format)) {
104 SDE_DEBUG("[cdm_disable fmt:%x]\n",
105 format->base.pixel_format);
106
107 if (hw_cdm && hw_cdm->ops.disable)
108 hw_cdm->ops.disable(hw_cdm);
109
110 return;
111 }
112
113 memset(cdm_cfg, 0, sizeof(struct sde_hw_cdm_cfg));
114
115 cdm_cfg->output_width = wb_roi->w;
116 cdm_cfg->output_height = wb_roi->h;
117 cdm_cfg->output_fmt = format;
118 cdm_cfg->output_type = CDM_CDWN_OUTPUT_WB;
abeykun5ed42e12016-08-24 17:09:26 -0400119 cdm_cfg->output_bit_depth = SDE_FORMAT_IS_DX(format) ?
120 CDM_CDWN_OUTPUT_10BIT : CDM_CDWN_OUTPUT_8BIT;
Alan Kwongbb27c092016-07-20 16:41:25 -0400121
122 /* enable 10 bit logic */
123 switch (cdm_cfg->output_fmt->chroma_sample) {
124 case SDE_CHROMA_RGB:
125 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
126 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
127 break;
128 case SDE_CHROMA_H2V1:
129 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
130 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
131 break;
132 case SDE_CHROMA_420:
133 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
134 cdm_cfg->v_cdwn_type = CDM_CDWN_OFFSITE;
135 break;
136 case SDE_CHROMA_H1V2:
137 default:
138 SDE_ERROR("unsupported chroma sampling type\n");
139 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
140 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
141 break;
142 }
143
144 SDE_DEBUG("[cdm_enable:%d,%d,%X,%d,%d,%d,%d]\n",
145 cdm_cfg->output_width,
146 cdm_cfg->output_height,
147 cdm_cfg->output_fmt->base.pixel_format,
148 cdm_cfg->output_type,
149 cdm_cfg->output_bit_depth,
150 cdm_cfg->h_cdwn_type,
151 cdm_cfg->v_cdwn_type);
152
153 if (hw_cdm && hw_cdm->ops.setup_cdwn) {
154 ret = hw_cdm->ops.setup_cdwn(hw_cdm, cdm_cfg);
155 if (ret < 0) {
156 SDE_ERROR("failed to setup CDM %d\n", ret);
157 return;
158 }
159 }
160
161 if (hw_cdm && hw_cdm->ops.enable) {
162 ret = hw_cdm->ops.enable(hw_cdm, cdm_cfg);
163 if (ret < 0) {
164 SDE_ERROR("failed to enable CDM %d\n", ret);
165 return;
166 }
167 }
168}
169
170/**
171 * sde_encoder_phys_wb_setup_fb - setup output framebuffer
172 * @phys_enc: Pointer to physical encoder
173 * @fb: Pointer to output framebuffer
174 * @wb_roi: Pointer to output region of interest
175 */
176static void sde_encoder_phys_wb_setup_fb(struct sde_encoder_phys *phys_enc,
177 struct drm_framebuffer *fb, struct sde_rect *wb_roi)
178{
179 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -0400180 struct sde_hw_wb *hw_wb;
181 struct sde_hw_wb_cfg *wb_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400182 const struct msm_format *format;
183 int ret, mmu_id;
184
Clarence Ip03521982016-08-26 10:49:47 -0400185 if (!phys_enc) {
186 SDE_ERROR("invalid encoder\n");
187 return;
188 }
189
190 hw_wb = wb_enc->hw_wb;
191 wb_cfg = &wb_enc->wb_cfg;
Alan Kwongbb27c092016-07-20 16:41:25 -0400192 memset(wb_cfg, 0, sizeof(struct sde_hw_wb_cfg));
193
Clarence Ip03521982016-08-26 10:49:47 -0400194 wb_cfg->intf_mode = phys_enc->intf_mode;
Alan Kwongbb27c092016-07-20 16:41:25 -0400195 wb_cfg->is_secure = (fb->flags & DRM_MODE_FB_SECURE) ? true : false;
196 mmu_id = (wb_cfg->is_secure) ?
197 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] :
198 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE];
199
200 SDE_DEBUG("[fb_secure:%d]\n", wb_cfg->is_secure);
201
202 format = msm_framebuffer_format(fb);
Dhaval Patelccbcb3d2016-08-22 11:58:14 -0700203 if (!format) {
204 SDE_DEBUG("invalid format for fb\n");
205 return;
206 }
207
Alan Kwongbb27c092016-07-20 16:41:25 -0400208 wb_cfg->dest.format = sde_get_sde_format_ext(
209 format->pixel_format,
210 fb->modifier,
211 drm_format_num_planes(fb->pixel_format));
212 if (!wb_cfg->dest.format) {
213 /* this error should be detected during atomic_check */
214 SDE_ERROR("failed to get format %x\n", format->pixel_format);
215 return;
216 }
abeykunf1539f72016-08-24 16:08:03 -0400217 wb_cfg->roi = *wb_roi;
Alan Kwongbb27c092016-07-20 16:41:25 -0400218
abeykunf1539f72016-08-24 16:08:03 -0400219 if (hw_wb->caps->features & BIT(SDE_WB_XY_ROI_OFFSET)) {
220 ret = sde_format_populate_layout(mmu_id, fb, &wb_cfg->dest);
221 if (ret) {
222 SDE_DEBUG("failed to populate layout %d\n", ret);
223 return;
224 }
225 wb_cfg->dest.width = fb->width;
226 wb_cfg->dest.height = fb->height;
227 wb_cfg->dest.num_planes = wb_cfg->dest.format->num_planes;
228 } else {
229 ret = sde_format_populate_layout_with_roi(mmu_id, fb, wb_roi,
Alan Kwongbb27c092016-07-20 16:41:25 -0400230 &wb_cfg->dest);
abeykunf1539f72016-08-24 16:08:03 -0400231 if (ret) {
232 /* this error should be detected during atomic_check */
233 SDE_DEBUG("failed to populate layout %d\n", ret);
234 return;
235 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400236 }
237
238 if ((wb_cfg->dest.format->fetch_planes == SDE_PLANE_PLANAR) &&
239 (wb_cfg->dest.format->element[0] == C1_B_Cb))
240 swap(wb_cfg->dest.plane_addr[1], wb_cfg->dest.plane_addr[2]);
241
242 SDE_DEBUG("[fb_offset:%8.8x,%8.8x,%8.8x,%8.8x]\n",
243 wb_cfg->dest.plane_addr[0],
244 wb_cfg->dest.plane_addr[1],
245 wb_cfg->dest.plane_addr[2],
246 wb_cfg->dest.plane_addr[3]);
247 SDE_DEBUG("[fb_stride:%8.8x,%8.8x,%8.8x,%8.8x]\n",
248 wb_cfg->dest.plane_pitch[0],
249 wb_cfg->dest.plane_pitch[1],
250 wb_cfg->dest.plane_pitch[2],
251 wb_cfg->dest.plane_pitch[3]);
252
abeykunf1539f72016-08-24 16:08:03 -0400253 if (hw_wb->ops.setup_roi)
254 hw_wb->ops.setup_roi(hw_wb, wb_cfg);
255
Alan Kwongbb27c092016-07-20 16:41:25 -0400256 if (hw_wb->ops.setup_outformat)
257 hw_wb->ops.setup_outformat(hw_wb, wb_cfg);
258
259 if (hw_wb->ops.setup_outaddress)
260 hw_wb->ops.setup_outaddress(hw_wb, wb_cfg);
261}
262
263/**
264 * sde_encoder_phys_wb_setup_cdp - setup chroma down prefetch block
265 * @phys_enc: Pointer to physical encoder
266 */
267static void sde_encoder_phys_wb_setup_cdp(struct sde_encoder_phys *phys_enc)
268{
269 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
270 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
271 struct sde_hw_intf_cfg *intf_cfg = &wb_enc->intf_cfg;
272
273 memset(intf_cfg, 0, sizeof(struct sde_hw_intf_cfg));
274
275 intf_cfg->intf = SDE_NONE;
276 intf_cfg->wb = hw_wb->idx;
Lloyd Atkinson55987b02016-08-16 16:57:46 -0400277 intf_cfg->mode_3d = sde_encoder_helper_get_3d_blend_mode(phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400278
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400279 if (phys_enc->hw_ctl && phys_enc->hw_ctl->ops.setup_intf_cfg)
Alan Kwongbb27c092016-07-20 16:41:25 -0400280 phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl,
281 intf_cfg);
282}
283
284/**
285 * sde_encoder_phys_wb_atomic_check - verify and fixup given atomic states
286 * @phys_enc: Pointer to physical encoder
287 * @crtc_state: Pointer to CRTC atomic state
288 * @conn_state: Pointer to connector atomic state
289 */
290static int sde_encoder_phys_wb_atomic_check(
291 struct sde_encoder_phys *phys_enc,
292 struct drm_crtc_state *crtc_state,
293 struct drm_connector_state *conn_state)
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 const struct sde_wb_cfg *wb_cfg = hw_wb->caps;
298 struct drm_framebuffer *fb;
299 const struct sde_format *fmt;
300 struct sde_rect wb_roi;
301 const struct drm_display_mode *mode = &crtc_state->mode;
302 int rc;
303
304 SDE_DEBUG("[atomic_check:%d,%d,\"%s\",%d,%d]\n",
305 hw_wb->idx - WB_0, mode->base.id, mode->name,
306 mode->hdisplay, mode->vdisplay);
307
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400308 if (!conn_state || !conn_state->connector) {
309 SDE_ERROR("invalid connector state\n");
310 return -EINVAL;
311 } else if (conn_state->connector->status !=
312 connector_status_connected) {
313 SDE_ERROR("connector not connected %d\n",
314 conn_state->connector->status);
315 return -EINVAL;
316 }
317
Alan Kwongbb27c092016-07-20 16:41:25 -0400318 memset(&wb_roi, 0, sizeof(struct sde_rect));
319
320 rc = sde_wb_connector_state_get_output_roi(conn_state, &wb_roi);
321 if (rc) {
322 SDE_ERROR("failed to get roi %d\n", rc);
323 return rc;
324 }
325
326 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi.x, wb_roi.y,
327 wb_roi.w, wb_roi.h);
328
329 fb = sde_wb_connector_state_get_output_fb(conn_state);
330 if (!fb) {
331 SDE_ERROR("no output framebuffer\n");
332 return -EINVAL;
333 }
334
335 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
336 fb->width, fb->height);
337
338 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
339 drm_format_num_planes(fb->pixel_format));
340 if (!fmt) {
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400341 SDE_ERROR("unsupported output pixel format:%x\n",
Alan Kwongbb27c092016-07-20 16:41:25 -0400342 fb->pixel_format);
343 return -EINVAL;
344 }
345
346 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
347 fb->modifier[0]);
348
349 if (SDE_FORMAT_IS_YUV(fmt) &&
350 !(wb_cfg->features & BIT(SDE_WB_YUV_CONFIG))) {
351 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
352 return -EINVAL;
353 }
354
355 if (SDE_FORMAT_IS_UBWC(fmt) &&
356 !(wb_cfg->features & BIT(SDE_WB_UBWC_1_0))) {
357 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
358 return -EINVAL;
359 }
360
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400361 if (SDE_FORMAT_IS_YUV(fmt) != !!phys_enc->hw_cdm)
362 crtc_state->mode_changed = true;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400363
Alan Kwongbb27c092016-07-20 16:41:25 -0400364 if (wb_roi.w && wb_roi.h) {
365 if (wb_roi.w != mode->hdisplay) {
366 SDE_ERROR("invalid roi w=%d, mode w=%d\n", wb_roi.w,
367 mode->hdisplay);
368 return -EINVAL;
369 } else if (wb_roi.h != mode->vdisplay) {
370 SDE_ERROR("invalid roi h=%d, mode h=%d\n", wb_roi.h,
371 mode->vdisplay);
372 return -EINVAL;
373 } else if (wb_roi.x + wb_roi.w > fb->width) {
374 SDE_ERROR("invalid roi x=%d, w=%d, fb w=%d\n",
375 wb_roi.x, wb_roi.w, fb->width);
376 return -EINVAL;
377 } else if (wb_roi.y + wb_roi.h > fb->height) {
378 SDE_ERROR("invalid roi y=%d, h=%d, fb h=%d\n",
379 wb_roi.y, wb_roi.h, fb->height);
380 return -EINVAL;
381 } else if (wb_roi.w > wb_cfg->sblk->maxlinewidth) {
382 SDE_ERROR("invalid roi w=%d, maxlinewidth=%u\n",
383 wb_roi.w, wb_cfg->sblk->maxlinewidth);
384 return -EINVAL;
385 }
386 } else {
387 if (wb_roi.x || wb_roi.y) {
388 SDE_ERROR("invalid roi x=%d, y=%d\n",
389 wb_roi.x, wb_roi.y);
390 return -EINVAL;
391 } else if (fb->width != mode->hdisplay) {
392 SDE_ERROR("invalid fb w=%d, mode w=%d\n", fb->width,
393 mode->hdisplay);
394 return -EINVAL;
395 } else if (fb->height != mode->vdisplay) {
396 SDE_ERROR("invalid fb h=%d, mode h=%d\n", fb->height,
397 mode->vdisplay);
398 return -EINVAL;
399 } else if (fb->width > wb_cfg->sblk->maxlinewidth) {
400 SDE_ERROR("invalid fb w=%d, maxlinewidth=%u\n",
401 fb->width, wb_cfg->sblk->maxlinewidth);
402 return -EINVAL;
403 }
404 }
405
406 return 0;
407}
408
409/**
410 * sde_encoder_phys_wb_flush - flush hardware update
411 * @phys_enc: Pointer to physical encoder
412 */
413static void sde_encoder_phys_wb_flush(struct sde_encoder_phys *phys_enc)
414{
415 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
416 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
417 struct sde_hw_ctl *hw_ctl = phys_enc->hw_ctl;
418 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
419 u32 flush_mask = 0;
420
421 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
422
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400423 if (!hw_ctl) {
424 SDE_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
425 return;
426 }
427
Alan Kwongbb27c092016-07-20 16:41:25 -0400428 if (hw_ctl->ops.get_bitmask_wb)
429 hw_ctl->ops.get_bitmask_wb(hw_ctl, &flush_mask, hw_wb->idx);
430
431 if (hw_ctl->ops.get_bitmask_cdm && hw_cdm)
432 hw_ctl->ops.get_bitmask_cdm(hw_ctl, &flush_mask, hw_cdm->idx);
433
434 if (hw_ctl->ops.update_pending_flush)
435 hw_ctl->ops.update_pending_flush(hw_ctl, flush_mask);
436
437 SDE_DEBUG("Flushing CTL_ID %d, flush_mask %x, WB %d\n",
438 hw_ctl->idx - CTL_0, flush_mask, hw_wb->idx - WB_0);
439}
440
441/**
442 * sde_encoder_phys_wb_setup - setup writeback encoder
443 * @phys_enc: Pointer to physical encoder
444 */
445static void sde_encoder_phys_wb_setup(
446 struct sde_encoder_phys *phys_enc)
447{
448 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
449 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
450 struct drm_display_mode mode = phys_enc->cached_mode;
451 struct drm_framebuffer *fb;
452 struct sde_rect *wb_roi = &wb_enc->wb_roi;
453
454 SDE_DEBUG("[mode_set:%d,%d,\"%s\",%d,%d]\n",
455 hw_wb->idx - WB_0, mode.base.id, mode.name,
456 mode.hdisplay, mode.vdisplay);
457
458 memset(wb_roi, 0, sizeof(struct sde_rect));
459
460 fb = sde_wb_get_output_fb(wb_enc->wb_dev);
461 if (!fb) {
462 SDE_DEBUG("no output framebuffer\n");
463 return;
464 }
465
466 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
467 fb->width, fb->height);
468
469 sde_wb_get_output_roi(wb_enc->wb_dev, wb_roi);
470 if (wb_roi->w == 0 || wb_roi->h == 0) {
471 wb_roi->x = 0;
472 wb_roi->y = 0;
473 wb_roi->w = fb->width;
474 wb_roi->h = fb->height;
475 }
476
477 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi->x, wb_roi->y,
478 wb_roi->w, wb_roi->h);
479
480 wb_enc->wb_fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
481 drm_format_num_planes(fb->pixel_format));
482 if (!wb_enc->wb_fmt) {
483 SDE_ERROR("unsupported output pixel format: %d\n",
484 fb->pixel_format);
485 return;
486 }
487
488 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
489 fb->modifier[0]);
490
Alan Kwong5d324e42016-07-28 22:56:18 -0400491 sde_encoder_phys_wb_set_ot_limit(phys_enc);
492
Alan Kwongbb27c092016-07-20 16:41:25 -0400493 sde_encoder_phys_wb_set_traffic_shaper(phys_enc);
494
495 sde_encoder_phys_setup_cdm(phys_enc, fb, wb_enc->wb_fmt, wb_roi);
496
497 sde_encoder_phys_wb_setup_fb(phys_enc, fb, wb_roi);
498
499 sde_encoder_phys_wb_setup_cdp(phys_enc);
500}
501
502/**
503 * sde_encoder_phys_wb_unregister_irq - unregister writeback interrupt handler
504 * @phys_enc: Pointer to physical encoder
505 */
506static int sde_encoder_phys_wb_unregister_irq(
507 struct sde_encoder_phys *phys_enc)
508{
509 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
510 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
511
512 if (wb_enc->bypass_irqreg)
513 return 0;
514
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400515 sde_core_irq_disable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwonga172ef52016-09-27 00:29:10 -0400516 sde_core_irq_unregister_callback(phys_enc->sde_kms, wb_enc->irq_idx,
517 &wb_enc->irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400518
519 SDE_DEBUG("un-register IRQ for wb %d, irq_idx=%d\n",
520 hw_wb->idx - WB_0,
521 wb_enc->irq_idx);
522
523 return 0;
524}
525
526/**
527 * sde_encoder_phys_wb_done_irq - writeback interrupt handler
528 * @arg: Pointer to writeback encoder
529 * @irq_idx: interrupt index
530 */
531static void sde_encoder_phys_wb_done_irq(void *arg, int irq_idx)
532{
533 struct sde_encoder_phys_wb *wb_enc = arg;
534 struct sde_encoder_phys *phys_enc = &wb_enc->base;
535 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
536
537 SDE_DEBUG("[wb:%d,%u]\n", hw_wb->idx - WB_0,
538 wb_enc->frame_count);
539
540 complete_all(&wb_enc->wbdone_complete);
541
Dhaval Patel81e87882016-10-19 21:41:56 -0700542 phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent,
543 phys_enc);
Alan Kwongbb27c092016-07-20 16:41:25 -0400544}
545
546/**
547 * sde_encoder_phys_wb_register_irq - register writeback interrupt handler
548 * @phys_enc: Pointer to physical encoder
549 */
550static int sde_encoder_phys_wb_register_irq(struct sde_encoder_phys *phys_enc)
551{
552 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
553 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwonga172ef52016-09-27 00:29:10 -0400554 struct sde_irq_callback *irq_cb = &wb_enc->irq_cb;
Alan Kwongbb27c092016-07-20 16:41:25 -0400555 enum sde_intr_type intr_type;
556 int ret = 0;
557
558 if (wb_enc->bypass_irqreg)
559 return 0;
560
561 intr_type = sde_encoder_phys_wb_get_intr_type(hw_wb);
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400562 wb_enc->irq_idx = sde_core_irq_idx_lookup(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400563 intr_type, hw_wb->idx);
564 if (wb_enc->irq_idx < 0) {
565 SDE_ERROR(
566 "failed to lookup IRQ index for WB_DONE with wb=%d\n",
567 hw_wb->idx - WB_0);
568 return -EINVAL;
569 }
570
Alan Kwonga172ef52016-09-27 00:29:10 -0400571 irq_cb->func = sde_encoder_phys_wb_done_irq;
572 irq_cb->arg = wb_enc;
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400573 ret = sde_core_irq_register_callback(phys_enc->sde_kms,
Alan Kwonga172ef52016-09-27 00:29:10 -0400574 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400575 if (ret) {
576 SDE_ERROR("failed to register IRQ callback WB_DONE\n");
577 return ret;
578 }
579
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400580 ret = sde_core_irq_enable(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
Alan Kwongbb27c092016-07-20 16:41:25 -0400581 if (ret) {
582 SDE_ERROR(
583 "failed to enable IRQ for WB_DONE, wb %d, irq_idx=%d\n",
584 hw_wb->idx - WB_0,
585 wb_enc->irq_idx);
586 wb_enc->irq_idx = -EINVAL;
587
588 /* Unregister callback on IRQ enable failure */
Alan Kwonga172ef52016-09-27 00:29:10 -0400589 sde_core_irq_unregister_callback(phys_enc->sde_kms,
590 wb_enc->irq_idx, irq_cb);
Alan Kwongbb27c092016-07-20 16:41:25 -0400591 return ret;
592 }
593
594 SDE_DEBUG("registered IRQ for wb %d, irq_idx=%d\n",
595 hw_wb->idx - WB_0,
596 wb_enc->irq_idx);
597
598 return ret;
599}
600
601/**
602 * sde_encoder_phys_wb_mode_set - set display mode
603 * @phys_enc: Pointer to physical encoder
604 * @mode: Pointer to requested display mode
605 * @adj_mode: Pointer to adjusted display mode
606 */
607static void sde_encoder_phys_wb_mode_set(
608 struct sde_encoder_phys *phys_enc,
609 struct drm_display_mode *mode,
610 struct drm_display_mode *adj_mode)
611{
612 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400613 struct sde_rm *rm = &phys_enc->sde_kms->rm;
Alan Kwongbb27c092016-07-20 16:41:25 -0400614 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400615 struct sde_rm_hw_iter iter;
616 int i, instance;
Alan Kwongbb27c092016-07-20 16:41:25 -0400617
618 phys_enc->cached_mode = *adj_mode;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400619 instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400620
621 SDE_DEBUG("[mode_set_cache:%d,%d,\"%s\",%d,%d]\n",
622 hw_wb->idx - WB_0, mode->base.id,
623 mode->name, mode->hdisplay, mode->vdisplay);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400624
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400625 phys_enc->hw_ctl = NULL;
626 phys_enc->hw_cdm = NULL;
627
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400628 /* Retrieve previously allocated HW Resources. CTL shouldn't fail */
629 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
630 for (i = 0; i <= instance; i++) {
631 sde_rm_get_hw(rm, &iter);
632 if (i == instance)
633 phys_enc->hw_ctl = (struct sde_hw_ctl *) iter.hw;
634 }
635
636 if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
637 SDE_ERROR("failed init ctl: %ld\n", PTR_ERR(phys_enc->hw_ctl));
638 phys_enc->hw_ctl = NULL;
639 return;
640 }
641
642 /* CDM is optional */
643 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CDM);
644 for (i = 0; i <= instance; i++) {
645 sde_rm_get_hw(rm, &iter);
646 if (i == instance)
647 phys_enc->hw_cdm = (struct sde_hw_cdm *) iter.hw;
648 }
649
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400650 if (IS_ERR(phys_enc->hw_cdm)) {
651 SDE_ERROR("CDM required but not allocated: %ld\n",
652 PTR_ERR(phys_enc->hw_cdm));
653 phys_enc->hw_ctl = NULL;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400654 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400655}
656
657/**
Alan Kwongbb27c092016-07-20 16:41:25 -0400658 * sde_encoder_phys_wb_wait_for_commit_done - wait until request is committed
659 * @phys_enc: Pointer to physical encoder
660 */
661static int sde_encoder_phys_wb_wait_for_commit_done(
662 struct sde_encoder_phys *phys_enc)
663{
664 unsigned long ret;
665 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
666 u32 irq_status;
667 u64 wb_time = 0;
668 int rc = 0;
669
670 /* Return EWOULDBLOCK since we know the wait isn't necessary */
671 if (WARN_ON(phys_enc->enable_state != SDE_ENC_ENABLED))
672 return -EWOULDBLOCK;
673
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400674 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400675
676 ret = wait_for_completion_timeout(&wb_enc->wbdone_complete,
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500677 KICKOFF_TIMEOUT_JIFFIES);
Alan Kwongbb27c092016-07-20 16:41:25 -0400678
679 if (!ret) {
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400680 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc),
681 wb_enc->frame_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400682
Alan Kwongf5dd86c2016-08-09 18:08:17 -0400683 irq_status = sde_core_irq_read(phys_enc->sde_kms,
Alan Kwongbb27c092016-07-20 16:41:25 -0400684 wb_enc->irq_idx, true);
685 if (irq_status) {
686 SDE_DEBUG("wb:%d done but irq not triggered\n",
687 wb_enc->wb_dev->wb_idx - WB_0);
688 sde_encoder_phys_wb_done_irq(wb_enc, wb_enc->irq_idx);
689 } else {
690 SDE_ERROR("wb:%d kickoff timed out\n",
691 wb_enc->wb_dev->wb_idx - WB_0);
692 rc = -ETIMEDOUT;
693 }
694 }
695
696 sde_encoder_phys_wb_unregister_irq(phys_enc);
697
698 if (!rc)
699 wb_enc->end_time = ktime_get();
700
701 /* once operation is done, disable traffic shaper */
702 if (wb_enc->wb_cfg.ts_cfg.en && wb_enc->hw_wb &&
703 wb_enc->hw_wb->ops.setup_trafficshaper) {
704 wb_enc->wb_cfg.ts_cfg.en = false;
705 wb_enc->hw_wb->ops.setup_trafficshaper(
706 wb_enc->hw_wb, &wb_enc->wb_cfg);
707 }
708
709 /* remove vote for iommu/clk/bus */
710 wb_enc->frame_count++;
711
712 if (!rc) {
713 wb_time = (u64)ktime_to_us(wb_enc->end_time) -
714 (u64)ktime_to_us(wb_enc->start_time);
715 SDE_DEBUG("wb:%d took %llu us\n",
716 wb_enc->wb_dev->wb_idx - WB_0, wb_time);
717 }
718
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400719 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->frame_count,
720 wb_time);
Alan Kwongbb27c092016-07-20 16:41:25 -0400721
722 return rc;
723}
724
725/**
726 * sde_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
727 * @phys_enc: Pointer to physical encoder
Alan Kwongbb27c092016-07-20 16:41:25 -0400728 */
729static void sde_encoder_phys_wb_prepare_for_kickoff(
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500730 struct sde_encoder_phys *phys_enc)
Alan Kwongbb27c092016-07-20 16:41:25 -0400731{
732 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
733 int ret;
734
735 SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
736 wb_enc->kickoff_count);
737
Alan Kwongbb27c092016-07-20 16:41:25 -0400738 reinit_completion(&wb_enc->wbdone_complete);
739
740 ret = sde_encoder_phys_wb_register_irq(phys_enc);
741 if (ret) {
742 SDE_ERROR("failed to register irq %d\n", ret);
743 return;
744 }
745
746 wb_enc->kickoff_count++;
747
748 /* set OT limit & enable traffic shaper */
749 sde_encoder_phys_wb_setup(phys_enc);
750
751 sde_encoder_phys_wb_flush(phys_enc);
752
753 /* vote for iommu/clk/bus */
754 wb_enc->start_time = ktime_get();
755
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -0500756 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc), wb_enc->kickoff_count);
Alan Kwongbb27c092016-07-20 16:41:25 -0400757}
758
759/**
760 * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
761 * @phys_enc: Pointer to physical encoder
762 */
763static void sde_encoder_phys_wb_handle_post_kickoff(
764 struct sde_encoder_phys *phys_enc)
765{
766 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
767
768 SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
769
Lloyd Atkinson5d40d312016-09-06 08:34:13 -0400770 SDE_EVT32(DRMID(phys_enc->parent), WBID(wb_enc));
Alan Kwongbb27c092016-07-20 16:41:25 -0400771}
772
773/**
774 * sde_encoder_phys_wb_enable - enable writeback encoder
775 * @phys_enc: Pointer to physical encoder
776 */
777static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
778{
779 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
780 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwong74e8ba32016-09-30 14:25:16 -0400781 struct drm_device *dev;
Alan Kwongbb27c092016-07-20 16:41:25 -0400782 struct drm_connector *connector;
783
784 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
785
Alan Kwong74e8ba32016-09-30 14:25:16 -0400786 if (!wb_enc->base.parent || !wb_enc->base.parent->dev) {
787 SDE_ERROR("invalid drm device\n");
788 return;
789 }
790 dev = wb_enc->base.parent->dev;
791
Alan Kwongbb27c092016-07-20 16:41:25 -0400792 /* find associated writeback connector */
Alan Kwong74e8ba32016-09-30 14:25:16 -0400793 mutex_lock(&dev->mode_config.mutex);
Alan Kwongbb27c092016-07-20 16:41:25 -0400794 drm_for_each_connector(connector, phys_enc->parent->dev) {
795 if (connector->encoder == phys_enc->parent)
796 break;
797 }
Alan Kwong74e8ba32016-09-30 14:25:16 -0400798 mutex_unlock(&dev->mode_config.mutex);
799
Alan Kwongbb27c092016-07-20 16:41:25 -0400800 if (!connector || connector->encoder != phys_enc->parent) {
801 SDE_ERROR("failed to find writeback connector\n");
802 return;
803 }
804 wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
805
806 phys_enc->enable_state = SDE_ENC_ENABLED;
807}
808
809/**
810 * sde_encoder_phys_wb_disable - disable writeback encoder
811 * @phys_enc: Pointer to physical encoder
812 */
813static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
814{
815 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
816 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
817
818 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
819
820 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
821 SDE_ERROR("encoder is already disabled\n");
822 return;
823 }
824
825 if (wb_enc->frame_count != wb_enc->kickoff_count) {
826 SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
827 hw_wb->idx - WB_0, wb_enc->frame_count,
828 wb_enc->kickoff_count);
829 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
830 }
831
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400832 if (phys_enc->hw_cdm && phys_enc->hw_cdm->ops.disable) {
833 SDE_DEBUG_DRIVER("[cdm_disable]\n");
834 phys_enc->hw_cdm->ops.disable(phys_enc->hw_cdm);
835 }
836
Alan Kwongbb27c092016-07-20 16:41:25 -0400837 phys_enc->enable_state = SDE_ENC_DISABLED;
838}
839
840/**
841 * sde_encoder_phys_wb_get_hw_resources - get hardware resources
842 * @phys_enc: Pointer to physical encoder
843 * @hw_res: Pointer to encoder resources
844 */
845static void sde_encoder_phys_wb_get_hw_resources(
846 struct sde_encoder_phys *phys_enc,
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400847 struct sde_encoder_hw_resources *hw_res,
848 struct drm_connector_state *conn_state)
Alan Kwongbb27c092016-07-20 16:41:25 -0400849{
850 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Clarence Ip03521982016-08-26 10:49:47 -0400851 struct sde_hw_wb *hw_wb;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400852 struct drm_framebuffer *fb;
853 const struct sde_format *fmt;
Alan Kwongbb27c092016-07-20 16:41:25 -0400854
Clarence Ip03521982016-08-26 10:49:47 -0400855 if (!phys_enc) {
856 SDE_ERROR("invalid encoder\n");
857 return;
858 }
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400859
860 fb = sde_wb_connector_state_get_output_fb(conn_state);
861 if (!fb) {
862 SDE_ERROR("no output framebuffer\n");
863 return;
864 }
865
866 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
867 drm_format_num_planes(fb->pixel_format));
868 if (!fmt) {
869 SDE_ERROR("unsupported output pixel format:%d\n",
870 fb->pixel_format);
871 return;
872 }
873
Clarence Ip03521982016-08-26 10:49:47 -0400874 hw_wb = wb_enc->hw_wb;
Clarence Ip03521982016-08-26 10:49:47 -0400875 hw_res->wbs[hw_wb->idx - WB_0] = phys_enc->intf_mode;
Alan Kwong4c3cf4c2016-09-25 20:08:09 -0400876 hw_res->needs_cdm = SDE_FORMAT_IS_YUV(fmt);
877 SDE_DEBUG("[wb:%d] intf_mode=%d needs_cdm=%d\n", hw_wb->idx - WB_0,
878 hw_res->wbs[hw_wb->idx - WB_0],
879 hw_res->needs_cdm);
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400880}
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400881
Alan Kwongbb27c092016-07-20 16:41:25 -0400882#ifdef CONFIG_DEBUG_FS
883/**
884 * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
885 * @phys_enc: Pointer to physical encoder
886 * @sde_kms: Pointer to SDE KMS object
887 */
888static int sde_encoder_phys_wb_init_debugfs(
889 struct sde_encoder_phys *phys_enc, struct sde_kms *kms)
890{
891 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
892
893 if (!phys_enc || !kms || !wb_enc->hw_wb)
894 return -EINVAL;
895
896 snprintf(wb_enc->wb_name, ARRAY_SIZE(wb_enc->wb_name), "encoder_wb%d",
897 wb_enc->hw_wb->idx - WB_0);
898
899 wb_enc->debugfs_root =
900 debugfs_create_dir(wb_enc->wb_name,
901 sde_debugfs_get_root(kms));
902 if (!wb_enc->debugfs_root) {
903 SDE_ERROR("failed to create debugfs\n");
904 return -ENOMEM;
905 }
906
907 if (!debugfs_create_u32("wbdone_timeout", 0644,
908 wb_enc->debugfs_root, &wb_enc->wbdone_timeout)) {
909 SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
910 return -ENOMEM;
911 }
912
913 if (!debugfs_create_u32("bypass_irqreg", 0644,
914 wb_enc->debugfs_root, &wb_enc->bypass_irqreg)) {
915 SDE_ERROR("failed to create debugfs/bypass_irqreg\n");
916 return -ENOMEM;
917 }
918
919 return 0;
920}
921
922/**
923 * sde_encoder_phys_wb_destroy_debugfs - destroy writeback encoder debugfs
924 * @phys_enc: Pointer to physical encoder
925 */
926static void sde_encoder_phys_wb_destroy_debugfs(
927 struct sde_encoder_phys *phys_enc)
928{
929 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
930
931 if (!phys_enc)
932 return;
933
934 debugfs_remove_recursive(wb_enc->debugfs_root);
935}
936#else
Dhaval Patel48f2d0f2016-09-27 16:39:12 -0700937static int sde_encoder_phys_wb_init_debugfs(
Alan Kwongbb27c092016-07-20 16:41:25 -0400938 struct sde_encoder_phys *phys_enc, struct sde_kms *kms)
939{
Dhaval Patel48f2d0f2016-09-27 16:39:12 -0700940 return 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400941}
942static void sde_encoder_phys_wb_destroy_debugfs(
943 struct sde_encoder_phys *phys_enc)
944{
945}
946#endif
947
948/**
949 * sde_encoder_phys_wb_destroy - destroy writeback encoder
950 * @phys_enc: Pointer to physical encoder
951 */
952static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
953{
954 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
955 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
956
957 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
958
959 if (!phys_enc)
960 return;
961
962 sde_encoder_phys_wb_destroy_debugfs(phys_enc);
963
Alan Kwongbb27c092016-07-20 16:41:25 -0400964 kfree(wb_enc);
965}
966
967/**
968 * sde_encoder_phys_wb_init_ops - initialize writeback operations
969 * @ops: Pointer to encoder operation table
970 */
971static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
972{
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400973 ops->is_master = sde_encoder_phys_wb_is_master;
Alan Kwongbb27c092016-07-20 16:41:25 -0400974 ops->mode_set = sde_encoder_phys_wb_mode_set;
975 ops->enable = sde_encoder_phys_wb_enable;
976 ops->disable = sde_encoder_phys_wb_disable;
977 ops->destroy = sde_encoder_phys_wb_destroy;
978 ops->atomic_check = sde_encoder_phys_wb_atomic_check;
979 ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
Alan Kwongbb27c092016-07-20 16:41:25 -0400980 ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
981 ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
982 ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
Clarence Ip110d15c2016-08-16 14:44:41 -0400983 ops->trigger_start = sde_encoder_helper_trigger_start;
Alan Kwongbb27c092016-07-20 16:41:25 -0400984}
985
986/**
987 * sde_encoder_phys_wb_init - initialize writeback encoder
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400988 * @init: Pointer to init info structure with initialization params
Alan Kwongbb27c092016-07-20 16:41:25 -0400989 */
990struct sde_encoder_phys *sde_encoder_phys_wb_init(
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400991 struct sde_enc_phys_init_params *p)
Alan Kwongbb27c092016-07-20 16:41:25 -0400992{
993 struct sde_encoder_phys *phys_enc;
994 struct sde_encoder_phys_wb *wb_enc;
995 struct sde_hw_mdp *hw_mdp;
996 int ret = 0;
997
998 SDE_DEBUG("\n");
999
1000 wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
1001 if (!wb_enc) {
1002 ret = -ENOMEM;
1003 goto fail_alloc;
1004 }
1005 wb_enc->irq_idx = -EINVAL;
Lloyd Atkinsonaa0dce92016-11-23 20:16:47 -05001006 wb_enc->wbdone_timeout = KICKOFF_TIMEOUT_MS;
Alan Kwongbb27c092016-07-20 16:41:25 -04001007 init_completion(&wb_enc->wbdone_complete);
1008
1009 phys_enc = &wb_enc->base;
1010
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001011 if (p->sde_kms->vbif[VBIF_NRT]) {
Alan Kwongbb27c092016-07-20 16:41:25 -04001012 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001013 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001014 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001015 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001016 } else {
1017 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001018 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001019 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001020 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -04001021 }
1022
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001023 hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
Alan Kwongbb27c092016-07-20 16:41:25 -04001024 if (IS_ERR_OR_NULL(hw_mdp)) {
1025 ret = PTR_ERR(hw_mdp);
1026 SDE_ERROR("failed to init hw_top: %d\n", ret);
1027 goto fail_mdp_init;
1028 }
1029 phys_enc->hw_mdptop = hw_mdp;
1030
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001031 /**
1032 * hw_wb resource permanently assigned to this encoder
1033 * Other resources allocated at atomic commit time by use case
1034 */
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001035 if (p->wb_idx != SDE_NONE) {
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001036 struct sde_rm_hw_iter iter;
Alan Kwongbb27c092016-07-20 16:41:25 -04001037
Lloyd Atkinson11f34442016-08-11 11:19:52 -04001038 sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
1039 while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
1040 struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
1041
1042 if (hw_wb->idx == p->wb_idx) {
1043 wb_enc->hw_wb = hw_wb;
1044 break;
1045 }
1046 }
1047
1048 if (!wb_enc->hw_wb) {
1049 ret = -EINVAL;
1050 SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
Alan Kwongbb27c092016-07-20 16:41:25 -04001051 goto fail_wb_init;
1052 }
Alan Kwongbb27c092016-07-20 16:41:25 -04001053 } else {
1054 ret = -EINVAL;
1055 SDE_ERROR("invalid wb_idx\n");
1056 goto fail_wb_check;
1057 }
1058
Alan Kwongbb27c092016-07-20 16:41:25 -04001059 sde_encoder_phys_wb_init_ops(&phys_enc->ops);
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001060 phys_enc->parent = p->parent;
1061 phys_enc->parent_ops = p->parent_ops;
1062 phys_enc->sde_kms = p->sde_kms;
1063 phys_enc->split_role = p->split_role;
Clarence Ip03521982016-08-26 10:49:47 -04001064 phys_enc->intf_mode = INTF_MODE_WB_LINE;
Dhaval Patel81e87882016-10-19 21:41:56 -07001065 phys_enc->intf_idx = p->intf_idx;
Lloyd Atkinson7d070942016-07-26 18:35:12 -04001066 phys_enc->enc_spinlock = p->enc_spinlock;
Alan Kwonga172ef52016-09-27 00:29:10 -04001067 INIT_LIST_HEAD(&wb_enc->irq_cb.list);
Alan Kwongbb27c092016-07-20 16:41:25 -04001068
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001069 ret = sde_encoder_phys_wb_init_debugfs(phys_enc, p->sde_kms);
Alan Kwongbb27c092016-07-20 16:41:25 -04001070 if (ret) {
1071 SDE_ERROR("failed to init debugfs %d\n", ret);
1072 goto fail_debugfs_init;
1073 }
1074
1075 SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
1076 wb_enc->hw_wb->idx - WB_0);
1077
1078 return phys_enc;
1079
1080fail_debugfs_init:
Alan Kwongbb27c092016-07-20 16:41:25 -04001081fail_wb_init:
1082fail_wb_check:
Alan Kwongbb27c092016-07-20 16:41:25 -04001083fail_mdp_init:
1084 kfree(wb_enc);
1085fail_alloc:
1086 return ERR_PTR(ret);
1087}
1088