Merge remote-tracking branch 'asoc/topic/pcm512x' into asoc-next
[sfrench/cifs-2.6.git] / drivers / gpu / drm / amd / display / dc / core / dc_stream.c
1 /*
2  * Copyright 2012-15 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: AMD
23  *
24  */
25
26 #include "dm_services.h"
27 #include "dc.h"
28 #include "core_types.h"
29 #include "resource.h"
30 #include "ipp.h"
31 #include "timing_generator.h"
32
33 /*******************************************************************************
34  * Private functions
35  ******************************************************************************/
36 #define TMDS_MAX_PIXEL_CLOCK_IN_KHZ_UPMOST 297000
37 static void update_stream_signal(struct dc_stream_state *stream)
38 {
39         if (stream->output_signal == SIGNAL_TYPE_NONE) {
40                 struct dc_sink *dc_sink = stream->sink;
41
42                 if (dc_sink->sink_signal == SIGNAL_TYPE_NONE)
43                         stream->signal = stream->sink->link->connector_signal;
44                 else
45                         stream->signal = dc_sink->sink_signal;
46         } else {
47                 stream->signal = stream->output_signal;
48         }
49
50         if (dc_is_dvi_signal(stream->signal)) {
51                 if (stream->timing.pix_clk_khz > TMDS_MAX_PIXEL_CLOCK_IN_KHZ_UPMOST &&
52                         stream->sink->sink_signal != SIGNAL_TYPE_DVI_SINGLE_LINK)
53                         stream->signal = SIGNAL_TYPE_DVI_DUAL_LINK;
54                 else
55                         stream->signal = SIGNAL_TYPE_DVI_SINGLE_LINK;
56         }
57 }
58
59 static void construct(struct dc_stream_state *stream,
60         struct dc_sink *dc_sink_data)
61 {
62         uint32_t i = 0;
63
64         stream->sink = dc_sink_data;
65         stream->ctx = stream->sink->ctx;
66
67         dc_sink_retain(dc_sink_data);
68
69         /* Copy audio modes */
70         /* TODO - Remove this translation */
71         for (i = 0; i < (dc_sink_data->edid_caps.audio_mode_count); i++)
72         {
73                 stream->audio_info.modes[i].channel_count = dc_sink_data->edid_caps.audio_modes[i].channel_count;
74                 stream->audio_info.modes[i].format_code = dc_sink_data->edid_caps.audio_modes[i].format_code;
75                 stream->audio_info.modes[i].sample_rates.all = dc_sink_data->edid_caps.audio_modes[i].sample_rate;
76                 stream->audio_info.modes[i].sample_size = dc_sink_data->edid_caps.audio_modes[i].sample_size;
77         }
78         stream->audio_info.mode_count = dc_sink_data->edid_caps.audio_mode_count;
79         stream->audio_info.audio_latency = dc_sink_data->edid_caps.audio_latency;
80         stream->audio_info.video_latency = dc_sink_data->edid_caps.video_latency;
81         memmove(
82                 stream->audio_info.display_name,
83                 dc_sink_data->edid_caps.display_name,
84                 AUDIO_INFO_DISPLAY_NAME_SIZE_IN_CHARS);
85         stream->audio_info.manufacture_id = dc_sink_data->edid_caps.manufacturer_id;
86         stream->audio_info.product_id = dc_sink_data->edid_caps.product_id;
87         stream->audio_info.flags.all = dc_sink_data->edid_caps.speaker_flags;
88
89         if (dc_sink_data->dc_container_id != NULL) {
90                 struct dc_container_id *dc_container_id = dc_sink_data->dc_container_id;
91
92                 stream->audio_info.port_id[0] = dc_container_id->portId[0];
93                 stream->audio_info.port_id[1] = dc_container_id->portId[1];
94         } else {
95                 /* TODO - WindowDM has implemented,
96                 other DMs need Unhardcode port_id */
97                 stream->audio_info.port_id[0] = 0x5558859e;
98                 stream->audio_info.port_id[1] = 0xd989449;
99         }
100
101         /* EDID CAP translation for HDMI 2.0 */
102         stream->timing.flags.LTE_340MCSC_SCRAMBLE = dc_sink_data->edid_caps.lte_340mcsc_scramble;
103
104         stream->status.link = stream->sink->link;
105
106         update_stream_signal(stream);
107 }
108
109 static void destruct(struct dc_stream_state *stream)
110 {
111         dc_sink_release(stream->sink);
112         if (stream->out_transfer_func != NULL) {
113                 dc_transfer_func_release(
114                                 stream->out_transfer_func);
115                 stream->out_transfer_func = NULL;
116         }
117 }
118
119 void dc_stream_retain(struct dc_stream_state *stream)
120 {
121         kref_get(&stream->refcount);
122 }
123
124 static void dc_stream_free(struct kref *kref)
125 {
126         struct dc_stream_state *stream = container_of(kref, struct dc_stream_state, refcount);
127
128         destruct(stream);
129         kfree(stream);
130 }
131
132 void dc_stream_release(struct dc_stream_state *stream)
133 {
134         if (stream != NULL) {
135                 kref_put(&stream->refcount, dc_stream_free);
136         }
137 }
138
139 struct dc_stream_state *dc_create_stream_for_sink(
140                 struct dc_sink *sink)
141 {
142         struct dc_stream_state *stream;
143
144         if (sink == NULL)
145                 return NULL;
146
147         stream = kzalloc(sizeof(struct dc_stream_state), GFP_KERNEL);
148         if (stream == NULL)
149                 return NULL;
150
151         construct(stream, sink);
152
153         kref_init(&stream->refcount);
154
155         return stream;
156 }
157
158 struct dc_stream_status *dc_stream_get_status(
159         struct dc_stream_state *stream)
160 {
161         uint8_t i;
162         struct dc  *dc = stream->ctx->dc;
163
164         for (i = 0; i < dc->current_state->stream_count; i++) {
165                 if (stream == dc->current_state->streams[i])
166                         return &dc->current_state->stream_status[i];
167         }
168
169         return NULL;
170 }
171
172 /**
173  * Update the cursor attributes and set cursor surface address
174  */
175 bool dc_stream_set_cursor_attributes(
176         struct dc_stream_state *stream,
177         const struct dc_cursor_attributes *attributes)
178 {
179         int i;
180         struct dc  *core_dc;
181         struct resource_context *res_ctx;
182
183         if (NULL == stream) {
184                 dm_error("DC: dc_stream is NULL!\n");
185                 return false;
186         }
187         if (NULL == attributes) {
188                 dm_error("DC: attributes is NULL!\n");
189                 return false;
190         }
191
192         if (attributes->address.quad_part == 0) {
193                 dm_output_to_console("DC: Cursor address is 0!\n");
194                 return false;
195         }
196
197         core_dc = stream->ctx->dc;
198         res_ctx = &core_dc->current_state->res_ctx;
199
200         for (i = 0; i < MAX_PIPES; i++) {
201                 struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
202
203                 if (pipe_ctx->stream != stream || (!pipe_ctx->plane_res.xfm && !pipe_ctx->plane_res.dpp))
204                         continue;
205                 if (pipe_ctx->top_pipe && pipe_ctx->plane_state != pipe_ctx->top_pipe->plane_state)
206                         continue;
207
208
209                 if (pipe_ctx->plane_res.ipp->funcs->ipp_cursor_set_attributes != NULL)
210                         pipe_ctx->plane_res.ipp->funcs->ipp_cursor_set_attributes(
211                                                 pipe_ctx->plane_res.ipp, attributes);
212
213                 if (pipe_ctx->plane_res.hubp != NULL &&
214                                 pipe_ctx->plane_res.hubp->funcs->set_cursor_attributes != NULL)
215                         pipe_ctx->plane_res.hubp->funcs->set_cursor_attributes(
216                                         pipe_ctx->plane_res.hubp, attributes);
217
218                 if (pipe_ctx->plane_res.mi != NULL &&
219                                 pipe_ctx->plane_res.mi->funcs->set_cursor_attributes != NULL)
220                         pipe_ctx->plane_res.mi->funcs->set_cursor_attributes(
221                                         pipe_ctx->plane_res.mi, attributes);
222
223
224                 if (pipe_ctx->plane_res.xfm != NULL &&
225                                 pipe_ctx->plane_res.xfm->funcs->set_cursor_attributes != NULL)
226                         pipe_ctx->plane_res.xfm->funcs->set_cursor_attributes(
227                                 pipe_ctx->plane_res.xfm, attributes);
228
229                 if (pipe_ctx->plane_res.dpp != NULL &&
230                                 pipe_ctx->plane_res.dpp->funcs->set_cursor_attributes != NULL)
231                         pipe_ctx->plane_res.dpp->funcs->set_cursor_attributes(
232                                 pipe_ctx->plane_res.dpp, attributes);
233         }
234
235         stream->cursor_attributes = *attributes;
236
237         return true;
238 }
239
240 bool dc_stream_set_cursor_position(
241         struct dc_stream_state *stream,
242         const struct dc_cursor_position *position)
243 {
244         int i;
245         struct dc  *core_dc;
246         struct resource_context *res_ctx;
247
248         if (NULL == stream) {
249                 dm_error("DC: dc_stream is NULL!\n");
250                 return false;
251         }
252
253         if (NULL == position) {
254                 dm_error("DC: cursor position is NULL!\n");
255                 return false;
256         }
257
258         core_dc = stream->ctx->dc;
259         res_ctx = &core_dc->current_state->res_ctx;
260
261         for (i = 0; i < MAX_PIPES; i++) {
262                 struct pipe_ctx *pipe_ctx = &res_ctx->pipe_ctx[i];
263                 struct input_pixel_processor *ipp = pipe_ctx->plane_res.ipp;
264                 struct mem_input *mi = pipe_ctx->plane_res.mi;
265                 struct hubp *hubp = pipe_ctx->plane_res.hubp;
266                 struct dpp *dpp = pipe_ctx->plane_res.dpp;
267                 struct dc_cursor_position pos_cpy = *position;
268                 struct dc_cursor_mi_param param = {
269                         .pixel_clk_khz = stream->timing.pix_clk_khz,
270                         .ref_clk_khz = core_dc->res_pool->ref_clock_inKhz,
271                         .viewport_x_start = pipe_ctx->plane_res.scl_data.viewport.x,
272                         .viewport_width = pipe_ctx->plane_res.scl_data.viewport.width,
273                         .h_scale_ratio = pipe_ctx->plane_res.scl_data.ratios.horz
274                 };
275
276                 if (pipe_ctx->stream != stream ||
277                                 (!pipe_ctx->plane_res.mi  && !pipe_ctx->plane_res.hubp) ||
278                                 !pipe_ctx->plane_state ||
279                                 (!pipe_ctx->plane_res.xfm && !pipe_ctx->plane_res.dpp))
280                         continue;
281
282                 if (pipe_ctx->plane_state->address.type
283                                 == PLN_ADDR_TYPE_VIDEO_PROGRESSIVE)
284                         pos_cpy.enable = false;
285
286                 if (pipe_ctx->top_pipe && pipe_ctx->plane_state != pipe_ctx->top_pipe->plane_state)
287                         pos_cpy.enable = false;
288
289
290                 if (ipp != NULL && ipp->funcs->ipp_cursor_set_position != NULL)
291                         ipp->funcs->ipp_cursor_set_position(ipp, &pos_cpy, &param);
292
293                 if (mi != NULL && mi->funcs->set_cursor_position != NULL)
294                         mi->funcs->set_cursor_position(mi, &pos_cpy, &param);
295
296                 if (!hubp)
297                         continue;
298
299                 if (hubp->funcs->set_cursor_position != NULL)
300                         hubp->funcs->set_cursor_position(hubp, &pos_cpy, &param);
301
302                 if (dpp != NULL && dpp->funcs->set_cursor_position != NULL)
303                         dpp->funcs->set_cursor_position(dpp, &pos_cpy, &param, hubp->curs_attr.width);
304
305         }
306
307         return true;
308 }
309
310 uint32_t dc_stream_get_vblank_counter(const struct dc_stream_state *stream)
311 {
312         uint8_t i;
313         struct dc  *core_dc = stream->ctx->dc;
314         struct resource_context *res_ctx =
315                 &core_dc->current_state->res_ctx;
316
317         for (i = 0; i < MAX_PIPES; i++) {
318                 struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
319
320                 if (res_ctx->pipe_ctx[i].stream != stream)
321                         continue;
322
323                 return tg->funcs->get_frame_count(tg);
324         }
325
326         return 0;
327 }
328
329 bool dc_stream_get_scanoutpos(const struct dc_stream_state *stream,
330                                   uint32_t *v_blank_start,
331                                   uint32_t *v_blank_end,
332                                   uint32_t *h_position,
333                                   uint32_t *v_position)
334 {
335         uint8_t i;
336         bool ret = false;
337         struct dc  *core_dc = stream->ctx->dc;
338         struct resource_context *res_ctx =
339                 &core_dc->current_state->res_ctx;
340
341         for (i = 0; i < MAX_PIPES; i++) {
342                 struct timing_generator *tg = res_ctx->pipe_ctx[i].stream_res.tg;
343
344                 if (res_ctx->pipe_ctx[i].stream != stream)
345                         continue;
346
347                 tg->funcs->get_scanoutpos(tg,
348                                           v_blank_start,
349                                           v_blank_end,
350                                           h_position,
351                                           v_position);
352
353                 ret = true;
354                 break;
355         }
356
357         return ret;
358 }
359
360
361 void dc_stream_log(
362         const struct dc_stream_state *stream,
363         struct dal_logger *dm_logger,
364         enum dc_log_type log_type)
365 {
366
367         dm_logger_write(dm_logger,
368                         log_type,
369                         "core_stream 0x%x: src: %d, %d, %d, %d; dst: %d, %d, %d, %d, colorSpace:%d\n",
370                         stream,
371                         stream->src.x,
372                         stream->src.y,
373                         stream->src.width,
374                         stream->src.height,
375                         stream->dst.x,
376                         stream->dst.y,
377                         stream->dst.width,
378                         stream->dst.height,
379                         stream->output_color_space);
380         dm_logger_write(dm_logger,
381                         log_type,
382                         "\tpix_clk_khz: %d, h_total: %d, v_total: %d, pixelencoder:%d, displaycolorDepth:%d\n",
383                         stream->timing.pix_clk_khz,
384                         stream->timing.h_total,
385                         stream->timing.v_total,
386                         stream->timing.pixel_encoding,
387                         stream->timing.display_color_depth);
388         dm_logger_write(dm_logger,
389                         log_type,
390                         "\tsink name: %s, serial: %d\n",
391                         stream->sink->edid_caps.display_name,
392                         stream->sink->edid_caps.serial_number);
393         dm_logger_write(dm_logger,
394                         log_type,
395                         "\tlink: %d\n",
396                         stream->sink->link->link_index);
397 }