blob: 4766d4f7789ed5a963514d6f52743d2f35644114 [file] [log] [blame]
Alan Kwongbb27c092016-07-20 16:41:25 -04001/*
2 * Copyright (c) 2015-2016 The Linux Foundation. All rights reserved.
3 *
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
15#define pr_fmt(fmt) "sde-wb:[%s] " fmt, __func__
16
17#include <linux/jiffies.h>
18#include <linux/debugfs.h>
19
20#include "sde_encoder_phys.h"
21#include "sde_formats.h"
22#include "sde_hw_top.h"
23#include "sde_hw_interrupts.h"
24#include "sde_wb.h"
25
26/* wait for at most 2 vsync for lowest refresh rate (24hz) */
27#define WAIT_TIMEOUT_MSEC 84
28
29#define to_sde_encoder_phys_wb(x) \
30 container_of(x, struct sde_encoder_phys_wb, base)
31
32#define DEV(phy_enc) (phy_enc->parent->dev)
33
34/**
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -040035 * sde_encoder_phys_wb_is_master - report wb always as master encoder
36 */
37static bool sde_encoder_phys_wb_is_master(struct sde_encoder_phys *phys_enc)
38{
39 return true;
40}
41
42/**
Alan Kwongbb27c092016-07-20 16:41:25 -040043 * sde_encoder_phys_wb_get_intr_type - get interrupt type based on block mode
44 * @hw_wb: Pointer to h/w writeback driver
45 */
46static enum sde_intr_type sde_encoder_phys_wb_get_intr_type(
47 struct sde_hw_wb *hw_wb)
48{
49 return (hw_wb->caps->features & BIT(SDE_WB_BLOCK_MODE)) ?
50 SDE_IRQ_TYPE_WB_ROT_COMP : SDE_IRQ_TYPE_WB_WFD_COMP;
51}
52
53/**
54 * sde_encoder_phys_wb_set_traffic_shaper - set traffic shaper for writeback
55 * @phys_enc: Pointer to physical encoder
56 */
57static void sde_encoder_phys_wb_set_traffic_shaper(
58 struct sde_encoder_phys *phys_enc)
59{
60 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
61 struct sde_hw_wb_cfg *wb_cfg = &wb_enc->wb_cfg;
62
63 /* traffic shaper is only enabled for rotator */
64 wb_cfg->ts_cfg.en = false;
65}
66
67/**
68 * sde_encoder_phys_setup_cdm - setup chroma down block
69 * @phys_enc: Pointer to physical encoder
70 * @fb: Pointer to output framebuffer
71 * @format: Output format
72 */
73void sde_encoder_phys_setup_cdm(struct sde_encoder_phys *phys_enc,
74 struct drm_framebuffer *fb, const struct sde_format *format,
75 struct sde_rect *wb_roi)
76{
77 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
78 struct sde_hw_cdm_cfg *cdm_cfg = &phys_enc->cdm_cfg;
79 int ret;
80
81 if (!SDE_FORMAT_IS_YUV(format)) {
82 SDE_DEBUG("[cdm_disable fmt:%x]\n",
83 format->base.pixel_format);
84
85 if (hw_cdm && hw_cdm->ops.disable)
86 hw_cdm->ops.disable(hw_cdm);
87
88 return;
89 }
90
91 memset(cdm_cfg, 0, sizeof(struct sde_hw_cdm_cfg));
92
93 cdm_cfg->output_width = wb_roi->w;
94 cdm_cfg->output_height = wb_roi->h;
95 cdm_cfg->output_fmt = format;
96 cdm_cfg->output_type = CDM_CDWN_OUTPUT_WB;
97 cdm_cfg->output_bit_depth = CDM_CDWN_OUTPUT_8BIT;
98
99 /* enable 10 bit logic */
100 switch (cdm_cfg->output_fmt->chroma_sample) {
101 case SDE_CHROMA_RGB:
102 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
103 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
104 break;
105 case SDE_CHROMA_H2V1:
106 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
107 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
108 break;
109 case SDE_CHROMA_420:
110 cdm_cfg->h_cdwn_type = CDM_CDWN_COSITE;
111 cdm_cfg->v_cdwn_type = CDM_CDWN_OFFSITE;
112 break;
113 case SDE_CHROMA_H1V2:
114 default:
115 SDE_ERROR("unsupported chroma sampling type\n");
116 cdm_cfg->h_cdwn_type = CDM_CDWN_DISABLE;
117 cdm_cfg->v_cdwn_type = CDM_CDWN_DISABLE;
118 break;
119 }
120
121 SDE_DEBUG("[cdm_enable:%d,%d,%X,%d,%d,%d,%d]\n",
122 cdm_cfg->output_width,
123 cdm_cfg->output_height,
124 cdm_cfg->output_fmt->base.pixel_format,
125 cdm_cfg->output_type,
126 cdm_cfg->output_bit_depth,
127 cdm_cfg->h_cdwn_type,
128 cdm_cfg->v_cdwn_type);
129
130 if (hw_cdm && hw_cdm->ops.setup_cdwn) {
131 ret = hw_cdm->ops.setup_cdwn(hw_cdm, cdm_cfg);
132 if (ret < 0) {
133 SDE_ERROR("failed to setup CDM %d\n", ret);
134 return;
135 }
136 }
137
138 if (hw_cdm && hw_cdm->ops.enable) {
139 ret = hw_cdm->ops.enable(hw_cdm, cdm_cfg);
140 if (ret < 0) {
141 SDE_ERROR("failed to enable CDM %d\n", ret);
142 return;
143 }
144 }
145}
146
147/**
148 * sde_encoder_phys_wb_setup_fb - setup output framebuffer
149 * @phys_enc: Pointer to physical encoder
150 * @fb: Pointer to output framebuffer
151 * @wb_roi: Pointer to output region of interest
152 */
153static void sde_encoder_phys_wb_setup_fb(struct sde_encoder_phys *phys_enc,
154 struct drm_framebuffer *fb, struct sde_rect *wb_roi)
155{
156 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
157 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
158 struct sde_hw_wb_cfg *wb_cfg = &wb_enc->wb_cfg;
159 const struct msm_format *format;
160 int ret, mmu_id;
161
162 memset(wb_cfg, 0, sizeof(struct sde_hw_wb_cfg));
163
164 wb_cfg->intf_mode = INTF_MODE_WB_LINE;
165 wb_cfg->is_secure = (fb->flags & DRM_MODE_FB_SECURE) ? true : false;
166 mmu_id = (wb_cfg->is_secure) ?
167 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] :
168 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE];
169
170 SDE_DEBUG("[fb_secure:%d]\n", wb_cfg->is_secure);
171
172 format = msm_framebuffer_format(fb);
173 wb_cfg->dest.format = sde_get_sde_format_ext(
174 format->pixel_format,
175 fb->modifier,
176 drm_format_num_planes(fb->pixel_format));
177 if (!wb_cfg->dest.format) {
178 /* this error should be detected during atomic_check */
179 SDE_ERROR("failed to get format %x\n", format->pixel_format);
180 return;
181 }
182
183 ret = sde_format_populate_layout_with_roi(mmu_id, fb, wb_roi,
184 &wb_cfg->dest);
185 if (ret) {
186 /* this error should be detected during atomic_check */
187 SDE_DEBUG("failed to populate layout %d\n", ret);
188 return;
189 }
190
191 if ((wb_cfg->dest.format->fetch_planes == SDE_PLANE_PLANAR) &&
192 (wb_cfg->dest.format->element[0] == C1_B_Cb))
193 swap(wb_cfg->dest.plane_addr[1], wb_cfg->dest.plane_addr[2]);
194
195 SDE_DEBUG("[fb_offset:%8.8x,%8.8x,%8.8x,%8.8x]\n",
196 wb_cfg->dest.plane_addr[0],
197 wb_cfg->dest.plane_addr[1],
198 wb_cfg->dest.plane_addr[2],
199 wb_cfg->dest.plane_addr[3]);
200 SDE_DEBUG("[fb_stride:%8.8x,%8.8x,%8.8x,%8.8x]\n",
201 wb_cfg->dest.plane_pitch[0],
202 wb_cfg->dest.plane_pitch[1],
203 wb_cfg->dest.plane_pitch[2],
204 wb_cfg->dest.plane_pitch[3]);
205
206 if (hw_wb->ops.setup_outformat)
207 hw_wb->ops.setup_outformat(hw_wb, wb_cfg);
208
209 if (hw_wb->ops.setup_outaddress)
210 hw_wb->ops.setup_outaddress(hw_wb, wb_cfg);
211}
212
213/**
214 * sde_encoder_phys_wb_setup_cdp - setup chroma down prefetch block
215 * @phys_enc: Pointer to physical encoder
216 */
217static void sde_encoder_phys_wb_setup_cdp(struct sde_encoder_phys *phys_enc)
218{
219 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
220 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
221 struct sde_hw_intf_cfg *intf_cfg = &wb_enc->intf_cfg;
222
223 memset(intf_cfg, 0, sizeof(struct sde_hw_intf_cfg));
224
225 intf_cfg->intf = SDE_NONE;
226 intf_cfg->wb = hw_wb->idx;
227
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400228 if (phys_enc->hw_ctl && phys_enc->hw_ctl->ops.setup_intf_cfg)
Alan Kwongbb27c092016-07-20 16:41:25 -0400229 phys_enc->hw_ctl->ops.setup_intf_cfg(phys_enc->hw_ctl,
230 intf_cfg);
231}
232
233/**
234 * sde_encoder_phys_wb_atomic_check - verify and fixup given atomic states
235 * @phys_enc: Pointer to physical encoder
236 * @crtc_state: Pointer to CRTC atomic state
237 * @conn_state: Pointer to connector atomic state
238 */
239static int sde_encoder_phys_wb_atomic_check(
240 struct sde_encoder_phys *phys_enc,
241 struct drm_crtc_state *crtc_state,
242 struct drm_connector_state *conn_state)
243{
244 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
245 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
246 const struct sde_wb_cfg *wb_cfg = hw_wb->caps;
247 struct drm_framebuffer *fb;
248 const struct sde_format *fmt;
249 struct sde_rect wb_roi;
250 const struct drm_display_mode *mode = &crtc_state->mode;
251 int rc;
252
253 SDE_DEBUG("[atomic_check:%d,%d,\"%s\",%d,%d]\n",
254 hw_wb->idx - WB_0, mode->base.id, mode->name,
255 mode->hdisplay, mode->vdisplay);
256
257 memset(&wb_roi, 0, sizeof(struct sde_rect));
258
259 rc = sde_wb_connector_state_get_output_roi(conn_state, &wb_roi);
260 if (rc) {
261 SDE_ERROR("failed to get roi %d\n", rc);
262 return rc;
263 }
264
265 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi.x, wb_roi.y,
266 wb_roi.w, wb_roi.h);
267
268 fb = sde_wb_connector_state_get_output_fb(conn_state);
269 if (!fb) {
270 SDE_ERROR("no output framebuffer\n");
271 return -EINVAL;
272 }
273
274 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
275 fb->width, fb->height);
276
277 fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
278 drm_format_num_planes(fb->pixel_format));
279 if (!fmt) {
280 SDE_ERROR("unsupported output pixel format:%d\n",
281 fb->pixel_format);
282 return -EINVAL;
283 }
284
285 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
286 fb->modifier[0]);
287
288 if (SDE_FORMAT_IS_YUV(fmt) &&
289 !(wb_cfg->features & BIT(SDE_WB_YUV_CONFIG))) {
290 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
291 return -EINVAL;
292 }
293
294 if (SDE_FORMAT_IS_UBWC(fmt) &&
295 !(wb_cfg->features & BIT(SDE_WB_UBWC_1_0))) {
296 SDE_ERROR("invalid output format %x\n", fmt->base.pixel_format);
297 return -EINVAL;
298 }
299
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400300 phys_enc->needs_cdm = SDE_FORMAT_IS_YUV(fmt);
301
Alan Kwongbb27c092016-07-20 16:41:25 -0400302 if (wb_roi.w && wb_roi.h) {
303 if (wb_roi.w != mode->hdisplay) {
304 SDE_ERROR("invalid roi w=%d, mode w=%d\n", wb_roi.w,
305 mode->hdisplay);
306 return -EINVAL;
307 } else if (wb_roi.h != mode->vdisplay) {
308 SDE_ERROR("invalid roi h=%d, mode h=%d\n", wb_roi.h,
309 mode->vdisplay);
310 return -EINVAL;
311 } else if (wb_roi.x + wb_roi.w > fb->width) {
312 SDE_ERROR("invalid roi x=%d, w=%d, fb w=%d\n",
313 wb_roi.x, wb_roi.w, fb->width);
314 return -EINVAL;
315 } else if (wb_roi.y + wb_roi.h > fb->height) {
316 SDE_ERROR("invalid roi y=%d, h=%d, fb h=%d\n",
317 wb_roi.y, wb_roi.h, fb->height);
318 return -EINVAL;
319 } else if (wb_roi.w > wb_cfg->sblk->maxlinewidth) {
320 SDE_ERROR("invalid roi w=%d, maxlinewidth=%u\n",
321 wb_roi.w, wb_cfg->sblk->maxlinewidth);
322 return -EINVAL;
323 }
324 } else {
325 if (wb_roi.x || wb_roi.y) {
326 SDE_ERROR("invalid roi x=%d, y=%d\n",
327 wb_roi.x, wb_roi.y);
328 return -EINVAL;
329 } else if (fb->width != mode->hdisplay) {
330 SDE_ERROR("invalid fb w=%d, mode w=%d\n", fb->width,
331 mode->hdisplay);
332 return -EINVAL;
333 } else if (fb->height != mode->vdisplay) {
334 SDE_ERROR("invalid fb h=%d, mode h=%d\n", fb->height,
335 mode->vdisplay);
336 return -EINVAL;
337 } else if (fb->width > wb_cfg->sblk->maxlinewidth) {
338 SDE_ERROR("invalid fb w=%d, maxlinewidth=%u\n",
339 fb->width, wb_cfg->sblk->maxlinewidth);
340 return -EINVAL;
341 }
342 }
343
344 return 0;
345}
346
347/**
348 * sde_encoder_phys_wb_flush - flush hardware update
349 * @phys_enc: Pointer to physical encoder
350 */
351static void sde_encoder_phys_wb_flush(struct sde_encoder_phys *phys_enc)
352{
353 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
354 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
355 struct sde_hw_ctl *hw_ctl = phys_enc->hw_ctl;
356 struct sde_hw_cdm *hw_cdm = phys_enc->hw_cdm;
357 u32 flush_mask = 0;
358
359 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
360
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400361 if (!hw_ctl) {
362 SDE_DEBUG("[wb:%d] no ctl assigned\n", hw_wb->idx - WB_0);
363 return;
364 }
365
Alan Kwongbb27c092016-07-20 16:41:25 -0400366 if (hw_ctl->ops.get_bitmask_wb)
367 hw_ctl->ops.get_bitmask_wb(hw_ctl, &flush_mask, hw_wb->idx);
368
369 if (hw_ctl->ops.get_bitmask_cdm && hw_cdm)
370 hw_ctl->ops.get_bitmask_cdm(hw_ctl, &flush_mask, hw_cdm->idx);
371
372 if (hw_ctl->ops.update_pending_flush)
373 hw_ctl->ops.update_pending_flush(hw_ctl, flush_mask);
374
375 SDE_DEBUG("Flushing CTL_ID %d, flush_mask %x, WB %d\n",
376 hw_ctl->idx - CTL_0, flush_mask, hw_wb->idx - WB_0);
377}
378
379/**
380 * sde_encoder_phys_wb_setup - setup writeback encoder
381 * @phys_enc: Pointer to physical encoder
382 */
383static void sde_encoder_phys_wb_setup(
384 struct sde_encoder_phys *phys_enc)
385{
386 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
387 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
388 struct drm_display_mode mode = phys_enc->cached_mode;
389 struct drm_framebuffer *fb;
390 struct sde_rect *wb_roi = &wb_enc->wb_roi;
391
392 SDE_DEBUG("[mode_set:%d,%d,\"%s\",%d,%d]\n",
393 hw_wb->idx - WB_0, mode.base.id, mode.name,
394 mode.hdisplay, mode.vdisplay);
395
396 memset(wb_roi, 0, sizeof(struct sde_rect));
397
398 fb = sde_wb_get_output_fb(wb_enc->wb_dev);
399 if (!fb) {
400 SDE_DEBUG("no output framebuffer\n");
401 return;
402 }
403
404 SDE_DEBUG("[fb_id:%u][fb:%u,%u]\n", fb->base.id,
405 fb->width, fb->height);
406
407 sde_wb_get_output_roi(wb_enc->wb_dev, wb_roi);
408 if (wb_roi->w == 0 || wb_roi->h == 0) {
409 wb_roi->x = 0;
410 wb_roi->y = 0;
411 wb_roi->w = fb->width;
412 wb_roi->h = fb->height;
413 }
414
415 SDE_DEBUG("[roi:%u,%u,%u,%u]\n", wb_roi->x, wb_roi->y,
416 wb_roi->w, wb_roi->h);
417
418 wb_enc->wb_fmt = sde_get_sde_format_ext(fb->pixel_format, fb->modifier,
419 drm_format_num_planes(fb->pixel_format));
420 if (!wb_enc->wb_fmt) {
421 SDE_ERROR("unsupported output pixel format: %d\n",
422 fb->pixel_format);
423 return;
424 }
425
426 SDE_DEBUG("[fb_fmt:%x,%llx]\n", fb->pixel_format,
427 fb->modifier[0]);
428
429 sde_encoder_phys_wb_set_traffic_shaper(phys_enc);
430
431 sde_encoder_phys_setup_cdm(phys_enc, fb, wb_enc->wb_fmt, wb_roi);
432
433 sde_encoder_phys_wb_setup_fb(phys_enc, fb, wb_roi);
434
435 sde_encoder_phys_wb_setup_cdp(phys_enc);
436}
437
438/**
439 * sde_encoder_phys_wb_unregister_irq - unregister writeback interrupt handler
440 * @phys_enc: Pointer to physical encoder
441 */
442static int sde_encoder_phys_wb_unregister_irq(
443 struct sde_encoder_phys *phys_enc)
444{
445 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
446 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
447
448 if (wb_enc->bypass_irqreg)
449 return 0;
450
451 sde_disable_irq(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
452 sde_register_irq_callback(phys_enc->sde_kms, wb_enc->irq_idx, NULL);
453
454 SDE_DEBUG("un-register IRQ for wb %d, irq_idx=%d\n",
455 hw_wb->idx - WB_0,
456 wb_enc->irq_idx);
457
458 return 0;
459}
460
461/**
462 * sde_encoder_phys_wb_done_irq - writeback interrupt handler
463 * @arg: Pointer to writeback encoder
464 * @irq_idx: interrupt index
465 */
466static void sde_encoder_phys_wb_done_irq(void *arg, int irq_idx)
467{
468 struct sde_encoder_phys_wb *wb_enc = arg;
469 struct sde_encoder_phys *phys_enc = &wb_enc->base;
470 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
471
472 SDE_DEBUG("[wb:%d,%u]\n", hw_wb->idx - WB_0,
473 wb_enc->frame_count);
474
475 complete_all(&wb_enc->wbdone_complete);
476
477 phys_enc->parent_ops.handle_vblank_virt(phys_enc->parent);
478}
479
480/**
481 * sde_encoder_phys_wb_register_irq - register writeback interrupt handler
482 * @phys_enc: Pointer to physical encoder
483 */
484static int sde_encoder_phys_wb_register_irq(struct sde_encoder_phys *phys_enc)
485{
486 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
487 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
488 struct sde_irq_callback irq_cb;
489 enum sde_intr_type intr_type;
490 int ret = 0;
491
492 if (wb_enc->bypass_irqreg)
493 return 0;
494
495 intr_type = sde_encoder_phys_wb_get_intr_type(hw_wb);
496 wb_enc->irq_idx = sde_irq_idx_lookup(phys_enc->sde_kms,
497 intr_type, hw_wb->idx);
498 if (wb_enc->irq_idx < 0) {
499 SDE_ERROR(
500 "failed to lookup IRQ index for WB_DONE with wb=%d\n",
501 hw_wb->idx - WB_0);
502 return -EINVAL;
503 }
504
505 irq_cb.func = sde_encoder_phys_wb_done_irq;
506 irq_cb.arg = wb_enc;
507 ret = sde_register_irq_callback(phys_enc->sde_kms, wb_enc->irq_idx,
508 &irq_cb);
509 if (ret) {
510 SDE_ERROR("failed to register IRQ callback WB_DONE\n");
511 return ret;
512 }
513
514 ret = sde_enable_irq(phys_enc->sde_kms, &wb_enc->irq_idx, 1);
515 if (ret) {
516 SDE_ERROR(
517 "failed to enable IRQ for WB_DONE, wb %d, irq_idx=%d\n",
518 hw_wb->idx - WB_0,
519 wb_enc->irq_idx);
520 wb_enc->irq_idx = -EINVAL;
521
522 /* Unregister callback on IRQ enable failure */
523 sde_register_irq_callback(phys_enc->sde_kms, wb_enc->irq_idx,
524 NULL);
525 return ret;
526 }
527
528 SDE_DEBUG("registered IRQ for wb %d, irq_idx=%d\n",
529 hw_wb->idx - WB_0,
530 wb_enc->irq_idx);
531
532 return ret;
533}
534
535/**
536 * sde_encoder_phys_wb_mode_set - set display mode
537 * @phys_enc: Pointer to physical encoder
538 * @mode: Pointer to requested display mode
539 * @adj_mode: Pointer to adjusted display mode
540 */
541static void sde_encoder_phys_wb_mode_set(
542 struct sde_encoder_phys *phys_enc,
543 struct drm_display_mode *mode,
544 struct drm_display_mode *adj_mode)
545{
546 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400547 struct sde_rm *rm = &phys_enc->sde_kms->rm;
Alan Kwongbb27c092016-07-20 16:41:25 -0400548 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400549 struct sde_rm_hw_iter iter;
550 int i, instance;
Alan Kwongbb27c092016-07-20 16:41:25 -0400551
552 phys_enc->cached_mode = *adj_mode;
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400553 instance = phys_enc->split_role == ENC_ROLE_SLAVE ? 1 : 0;
Alan Kwongbb27c092016-07-20 16:41:25 -0400554
555 SDE_DEBUG("[mode_set_cache:%d,%d,\"%s\",%d,%d]\n",
556 hw_wb->idx - WB_0, mode->base.id,
557 mode->name, mode->hdisplay, mode->vdisplay);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400558
559 /* Retrieve previously allocated HW Resources. CTL shouldn't fail */
560 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CTL);
561 for (i = 0; i <= instance; i++) {
562 sde_rm_get_hw(rm, &iter);
563 if (i == instance)
564 phys_enc->hw_ctl = (struct sde_hw_ctl *) iter.hw;
565 }
566
567 if (IS_ERR_OR_NULL(phys_enc->hw_ctl)) {
568 SDE_ERROR("failed init ctl: %ld\n", PTR_ERR(phys_enc->hw_ctl));
569 phys_enc->hw_ctl = NULL;
570 return;
571 }
572
573 /* CDM is optional */
574 sde_rm_init_hw_iter(&iter, phys_enc->parent->base.id, SDE_HW_BLK_CDM);
575 for (i = 0; i <= instance; i++) {
576 sde_rm_get_hw(rm, &iter);
577 if (i == instance)
578 phys_enc->hw_cdm = (struct sde_hw_cdm *) iter.hw;
579 }
580
581 if (IS_ERR_OR_NULL(phys_enc->hw_cdm)) {
582 if (phys_enc->needs_cdm) {
583 SDE_ERROR("CDM required but not allocated: %ld\n",
584 PTR_ERR(phys_enc->hw_cdm));
585 phys_enc->hw_ctl = NULL;
586 }
587 phys_enc->hw_cdm = NULL;
588 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400589}
590
591/**
592 * sde_encoder_phys_wb_control_vblank_irq - Control vblank interrupt
593 * @phys_enc: Pointer to physical encoder
594 * @enable: Enable interrupt
595 */
596static int sde_encoder_phys_wb_control_vblank_irq(
597 struct sde_encoder_phys *phys_enc,
598 bool enable)
599{
600 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
601 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
602 int ret = 0;
603
604 SDE_DEBUG("[wb:%d,%d]\n", hw_wb->idx - WB_0, enable);
605
606 if (enable)
607 ret = sde_encoder_phys_wb_register_irq(phys_enc);
608 else
609 ret = sde_encoder_phys_wb_unregister_irq(phys_enc);
610
611 if (ret)
612 SDE_ERROR("control vblank irq error %d, enable %d\n", ret,
613 enable);
614
615 return ret;
616}
617
618/**
619 * sde_encoder_phys_wb_wait_for_commit_done - wait until request is committed
620 * @phys_enc: Pointer to physical encoder
621 */
622static int sde_encoder_phys_wb_wait_for_commit_done(
623 struct sde_encoder_phys *phys_enc)
624{
625 unsigned long ret;
626 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
627 u32 irq_status;
628 u64 wb_time = 0;
629 int rc = 0;
630
631 /* Return EWOULDBLOCK since we know the wait isn't necessary */
632 if (WARN_ON(phys_enc->enable_state != SDE_ENC_ENABLED))
633 return -EWOULDBLOCK;
634
635 MSM_EVT(DEV(phys_enc), wb_enc->frame_count, 0);
636
637 ret = wait_for_completion_timeout(&wb_enc->wbdone_complete,
638 msecs_to_jiffies(wb_enc->wbdone_timeout));
639
640 if (!ret) {
641 MSM_EVT(DEV(phys_enc), wb_enc->frame_count, 0);
642
643 irq_status = sde_read_irq(phys_enc->sde_kms,
644 wb_enc->irq_idx, true);
645 if (irq_status) {
646 SDE_DEBUG("wb:%d done but irq not triggered\n",
647 wb_enc->wb_dev->wb_idx - WB_0);
648 sde_encoder_phys_wb_done_irq(wb_enc, wb_enc->irq_idx);
649 } else {
650 SDE_ERROR("wb:%d kickoff timed out\n",
651 wb_enc->wb_dev->wb_idx - WB_0);
652 rc = -ETIMEDOUT;
653 }
654 }
655
656 sde_encoder_phys_wb_unregister_irq(phys_enc);
657
658 if (!rc)
659 wb_enc->end_time = ktime_get();
660
661 /* once operation is done, disable traffic shaper */
662 if (wb_enc->wb_cfg.ts_cfg.en && wb_enc->hw_wb &&
663 wb_enc->hw_wb->ops.setup_trafficshaper) {
664 wb_enc->wb_cfg.ts_cfg.en = false;
665 wb_enc->hw_wb->ops.setup_trafficshaper(
666 wb_enc->hw_wb, &wb_enc->wb_cfg);
667 }
668
669 /* remove vote for iommu/clk/bus */
670 wb_enc->frame_count++;
671
672 if (!rc) {
673 wb_time = (u64)ktime_to_us(wb_enc->end_time) -
674 (u64)ktime_to_us(wb_enc->start_time);
675 SDE_DEBUG("wb:%d took %llu us\n",
676 wb_enc->wb_dev->wb_idx - WB_0, wb_time);
677 }
678
679 MSM_EVT(DEV(phys_enc), wb_enc->frame_count, wb_time);
680
681 return rc;
682}
683
684/**
685 * sde_encoder_phys_wb_prepare_for_kickoff - pre-kickoff processing
686 * @phys_enc: Pointer to physical encoder
687 * @need_to_wait: Wait for next submission
688 */
689static void sde_encoder_phys_wb_prepare_for_kickoff(
690 struct sde_encoder_phys *phys_enc,
691 bool *need_to_wait)
692{
693 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
694 int ret;
695
696 SDE_DEBUG("[wb:%d,%u]\n", wb_enc->hw_wb->idx - WB_0,
697 wb_enc->kickoff_count);
698
699 *need_to_wait = false;
700
701 reinit_completion(&wb_enc->wbdone_complete);
702
703 ret = sde_encoder_phys_wb_register_irq(phys_enc);
704 if (ret) {
705 SDE_ERROR("failed to register irq %d\n", ret);
706 return;
707 }
708
709 wb_enc->kickoff_count++;
710
711 /* set OT limit & enable traffic shaper */
712 sde_encoder_phys_wb_setup(phys_enc);
713
714 sde_encoder_phys_wb_flush(phys_enc);
715
716 /* vote for iommu/clk/bus */
717 wb_enc->start_time = ktime_get();
718
719 MSM_EVT(DEV(phys_enc), *need_to_wait, wb_enc->kickoff_count);
720}
721
722/**
723 * sde_encoder_phys_wb_handle_post_kickoff - post-kickoff processing
724 * @phys_enc: Pointer to physical encoder
725 */
726static void sde_encoder_phys_wb_handle_post_kickoff(
727 struct sde_encoder_phys *phys_enc)
728{
729 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
730
731 SDE_DEBUG("[wb:%d]\n", wb_enc->hw_wb->idx - WB_0);
732
733 MSM_EVT(DEV(phys_enc), 0, 0);
734}
735
736/**
737 * sde_encoder_phys_wb_enable - enable writeback encoder
738 * @phys_enc: Pointer to physical encoder
739 */
740static void sde_encoder_phys_wb_enable(struct sde_encoder_phys *phys_enc)
741{
742 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
743 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
744 struct drm_connector *connector;
745
746 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
747
748 /* find associated writeback connector */
749 drm_for_each_connector(connector, phys_enc->parent->dev) {
750 if (connector->encoder == phys_enc->parent)
751 break;
752 }
753 if (!connector || connector->encoder != phys_enc->parent) {
754 SDE_ERROR("failed to find writeback connector\n");
755 return;
756 }
757 wb_enc->wb_dev = sde_wb_connector_get_wb(connector);
758
759 phys_enc->enable_state = SDE_ENC_ENABLED;
760}
761
762/**
763 * sde_encoder_phys_wb_disable - disable writeback encoder
764 * @phys_enc: Pointer to physical encoder
765 */
766static void sde_encoder_phys_wb_disable(struct sde_encoder_phys *phys_enc)
767{
768 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
769 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
770
771 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
772
773 if (phys_enc->enable_state == SDE_ENC_DISABLED) {
774 SDE_ERROR("encoder is already disabled\n");
775 return;
776 }
777
778 if (wb_enc->frame_count != wb_enc->kickoff_count) {
779 SDE_DEBUG("[wait_for_done: wb:%d, frame:%u, kickoff:%u]\n",
780 hw_wb->idx - WB_0, wb_enc->frame_count,
781 wb_enc->kickoff_count);
782 sde_encoder_phys_wb_wait_for_commit_done(phys_enc);
783 }
784
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400785 if (phys_enc->hw_cdm && phys_enc->hw_cdm->ops.disable) {
786 SDE_DEBUG_DRIVER("[cdm_disable]\n");
787 phys_enc->hw_cdm->ops.disable(phys_enc->hw_cdm);
788 }
789
Alan Kwongbb27c092016-07-20 16:41:25 -0400790 phys_enc->enable_state = SDE_ENC_DISABLED;
791}
792
793/**
794 * sde_encoder_phys_wb_get_hw_resources - get hardware resources
795 * @phys_enc: Pointer to physical encoder
796 * @hw_res: Pointer to encoder resources
797 */
798static void sde_encoder_phys_wb_get_hw_resources(
799 struct sde_encoder_phys *phys_enc,
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400800 struct sde_encoder_hw_resources *hw_res,
801 struct drm_connector_state *conn_state)
Alan Kwongbb27c092016-07-20 16:41:25 -0400802{
803 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
804 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
Alan Kwongbb27c092016-07-20 16:41:25 -0400805
806 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400807 hw_res->wbs[hw_wb->idx - WB_0] = INTF_MODE_WB_LINE;
808 hw_res->needs_cdm = phys_enc->needs_cdm;
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400809}
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400810
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400811/**
812 * sde_encoder_phys_wb_needs_ctl_start - Whether encoder needs ctl_start
813 * @phys_enc: Pointer to physical encoder
814 * @Return: Whether encoder needs ctl_start
815 */
816static bool sde_encoder_phys_wb_needs_ctl_start(
817 struct sde_encoder_phys *phys_enc)
818{
819 return true;
Alan Kwongbb27c092016-07-20 16:41:25 -0400820}
821
822#ifdef CONFIG_DEBUG_FS
823/**
824 * sde_encoder_phys_wb_init_debugfs - initialize writeback encoder debugfs
825 * @phys_enc: Pointer to physical encoder
826 * @sde_kms: Pointer to SDE KMS object
827 */
828static int sde_encoder_phys_wb_init_debugfs(
829 struct sde_encoder_phys *phys_enc, struct sde_kms *kms)
830{
831 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
832
833 if (!phys_enc || !kms || !wb_enc->hw_wb)
834 return -EINVAL;
835
836 snprintf(wb_enc->wb_name, ARRAY_SIZE(wb_enc->wb_name), "encoder_wb%d",
837 wb_enc->hw_wb->idx - WB_0);
838
839 wb_enc->debugfs_root =
840 debugfs_create_dir(wb_enc->wb_name,
841 sde_debugfs_get_root(kms));
842 if (!wb_enc->debugfs_root) {
843 SDE_ERROR("failed to create debugfs\n");
844 return -ENOMEM;
845 }
846
847 if (!debugfs_create_u32("wbdone_timeout", 0644,
848 wb_enc->debugfs_root, &wb_enc->wbdone_timeout)) {
849 SDE_ERROR("failed to create debugfs/wbdone_timeout\n");
850 return -ENOMEM;
851 }
852
853 if (!debugfs_create_u32("bypass_irqreg", 0644,
854 wb_enc->debugfs_root, &wb_enc->bypass_irqreg)) {
855 SDE_ERROR("failed to create debugfs/bypass_irqreg\n");
856 return -ENOMEM;
857 }
858
859 return 0;
860}
861
862/**
863 * sde_encoder_phys_wb_destroy_debugfs - destroy writeback encoder debugfs
864 * @phys_enc: Pointer to physical encoder
865 */
866static void sde_encoder_phys_wb_destroy_debugfs(
867 struct sde_encoder_phys *phys_enc)
868{
869 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
870
871 if (!phys_enc)
872 return;
873
874 debugfs_remove_recursive(wb_enc->debugfs_root);
875}
876#else
877static void sde_encoder_phys_wb_init_debugfs(
878 struct sde_encoder_phys *phys_enc, struct sde_kms *kms)
879{
880}
881static void sde_encoder_phys_wb_destroy_debugfs(
882 struct sde_encoder_phys *phys_enc)
883{
884}
885#endif
886
887/**
888 * sde_encoder_phys_wb_destroy - destroy writeback encoder
889 * @phys_enc: Pointer to physical encoder
890 */
891static void sde_encoder_phys_wb_destroy(struct sde_encoder_phys *phys_enc)
892{
893 struct sde_encoder_phys_wb *wb_enc = to_sde_encoder_phys_wb(phys_enc);
894 struct sde_hw_wb *hw_wb = wb_enc->hw_wb;
895
896 SDE_DEBUG("[wb:%d]\n", hw_wb->idx - WB_0);
897
898 if (!phys_enc)
899 return;
900
901 sde_encoder_phys_wb_destroy_debugfs(phys_enc);
902
Alan Kwongbb27c092016-07-20 16:41:25 -0400903 kfree(wb_enc);
904}
905
906/**
907 * sde_encoder_phys_wb_init_ops - initialize writeback operations
908 * @ops: Pointer to encoder operation table
909 */
910static void sde_encoder_phys_wb_init_ops(struct sde_encoder_phys_ops *ops)
911{
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400912 ops->is_master = sde_encoder_phys_wb_is_master;
Alan Kwongbb27c092016-07-20 16:41:25 -0400913 ops->mode_set = sde_encoder_phys_wb_mode_set;
914 ops->enable = sde_encoder_phys_wb_enable;
915 ops->disable = sde_encoder_phys_wb_disable;
916 ops->destroy = sde_encoder_phys_wb_destroy;
917 ops->atomic_check = sde_encoder_phys_wb_atomic_check;
918 ops->get_hw_resources = sde_encoder_phys_wb_get_hw_resources;
919 ops->control_vblank_irq = sde_encoder_phys_wb_control_vblank_irq;
920 ops->wait_for_commit_done = sde_encoder_phys_wb_wait_for_commit_done;
921 ops->prepare_for_kickoff = sde_encoder_phys_wb_prepare_for_kickoff;
922 ops->handle_post_kickoff = sde_encoder_phys_wb_handle_post_kickoff;
Lloyd Atkinsone7bcdd22016-08-11 10:53:37 -0400923 ops->needs_ctl_start = sde_encoder_phys_wb_needs_ctl_start;
Alan Kwongbb27c092016-07-20 16:41:25 -0400924}
925
926/**
927 * sde_encoder_phys_wb_init - initialize writeback encoder
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400928 * @init: Pointer to init info structure with initialization params
Alan Kwongbb27c092016-07-20 16:41:25 -0400929 */
930struct sde_encoder_phys *sde_encoder_phys_wb_init(
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400931 struct sde_enc_phys_init_params *p)
Alan Kwongbb27c092016-07-20 16:41:25 -0400932{
933 struct sde_encoder_phys *phys_enc;
934 struct sde_encoder_phys_wb *wb_enc;
935 struct sde_hw_mdp *hw_mdp;
936 int ret = 0;
937
938 SDE_DEBUG("\n");
939
940 wb_enc = kzalloc(sizeof(*wb_enc), GFP_KERNEL);
941 if (!wb_enc) {
942 ret = -ENOMEM;
943 goto fail_alloc;
944 }
945 wb_enc->irq_idx = -EINVAL;
946 wb_enc->wbdone_timeout = WAIT_TIMEOUT_MSEC;
947 init_completion(&wb_enc->wbdone_complete);
948
949 phys_enc = &wb_enc->base;
950
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400951 if (p->sde_kms->vbif[VBIF_NRT]) {
Alan Kwongbb27c092016-07-20 16:41:25 -0400952 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400953 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -0400954 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400955 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_NRT_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -0400956 } else {
957 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_UNSECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400958 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_UNSECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -0400959 wb_enc->mmu_id[SDE_IOMMU_DOMAIN_SECURE] =
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400960 p->sde_kms->mmu_id[MSM_SMMU_DOMAIN_SECURE];
Alan Kwongbb27c092016-07-20 16:41:25 -0400961 }
962
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400963 hw_mdp = sde_rm_get_mdp(&p->sde_kms->rm);
Alan Kwongbb27c092016-07-20 16:41:25 -0400964 if (IS_ERR_OR_NULL(hw_mdp)) {
965 ret = PTR_ERR(hw_mdp);
966 SDE_ERROR("failed to init hw_top: %d\n", ret);
967 goto fail_mdp_init;
968 }
969 phys_enc->hw_mdptop = hw_mdp;
970
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400971 /**
972 * hw_wb resource permanently assigned to this encoder
973 * Other resources allocated at atomic commit time by use case
974 */
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -0400975 if (p->wb_idx != SDE_NONE) {
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400976 struct sde_rm_hw_iter iter;
Alan Kwongbb27c092016-07-20 16:41:25 -0400977
Lloyd Atkinson11f34442016-08-11 11:19:52 -0400978 sde_rm_init_hw_iter(&iter, 0, SDE_HW_BLK_WB);
979 while (sde_rm_get_hw(&p->sde_kms->rm, &iter)) {
980 struct sde_hw_wb *hw_wb = (struct sde_hw_wb *)iter.hw;
981
982 if (hw_wb->idx == p->wb_idx) {
983 wb_enc->hw_wb = hw_wb;
984 break;
985 }
986 }
987
988 if (!wb_enc->hw_wb) {
989 ret = -EINVAL;
990 SDE_ERROR("failed to init hw_wb%d\n", p->wb_idx - WB_0);
Alan Kwongbb27c092016-07-20 16:41:25 -0400991 goto fail_wb_init;
992 }
Alan Kwongbb27c092016-07-20 16:41:25 -0400993 } else {
994 ret = -EINVAL;
995 SDE_ERROR("invalid wb_idx\n");
996 goto fail_wb_check;
997 }
998
Alan Kwongbb27c092016-07-20 16:41:25 -0400999 sde_encoder_phys_wb_init_ops(&phys_enc->ops);
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001000 phys_enc->parent = p->parent;
1001 phys_enc->parent_ops = p->parent_ops;
1002 phys_enc->sde_kms = p->sde_kms;
1003 phys_enc->split_role = p->split_role;
Alan Kwongbb27c092016-07-20 16:41:25 -04001004 spin_lock_init(&phys_enc->spin_lock);
1005
Lloyd Atkinson6ef6cb52016-07-06 11:49:18 -04001006 ret = sde_encoder_phys_wb_init_debugfs(phys_enc, p->sde_kms);
Alan Kwongbb27c092016-07-20 16:41:25 -04001007 if (ret) {
1008 SDE_ERROR("failed to init debugfs %d\n", ret);
1009 goto fail_debugfs_init;
1010 }
1011
1012 SDE_DEBUG("Created sde_encoder_phys_wb for wb %d\n",
1013 wb_enc->hw_wb->idx - WB_0);
1014
1015 return phys_enc;
1016
1017fail_debugfs_init:
Alan Kwongbb27c092016-07-20 16:41:25 -04001018fail_wb_init:
1019fail_wb_check:
Alan Kwongbb27c092016-07-20 16:41:25 -04001020fail_mdp_init:
1021 kfree(wb_enc);
1022fail_alloc:
1023 return ERR_PTR(ret);
1024}
1025