]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/amd/display/amdgpu_dm/amdgpu_dm_helpers.c
drm/amd/display: fix dereference of pointer aconnector when it is null
[linux.git] / drivers / gpu / drm / amd / display / amdgpu_dm / amdgpu_dm_helpers.c
1 /*
2  * Copyright 2015 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 <linux/string.h>
27 #include <linux/acpi.h>
28 #include <linux/version.h>
29 #include <linux/i2c.h>
30
31 #include <drm/drm_probe_helper.h>
32 #include <drm/amdgpu_drm.h>
33 #include <drm/drm_edid.h>
34
35 #include "dm_services.h"
36 #include "amdgpu.h"
37 #include "dc.h"
38 #include "amdgpu_dm.h"
39 #include "amdgpu_dm_irq.h"
40
41 #include "dm_helpers.h"
42
43 /* dm_helpers_parse_edid_caps
44  *
45  * Parse edid caps
46  *
47  * @edid:       [in] pointer to edid
48  *  edid_caps:  [in] pointer to edid caps
49  * @return
50  *      void
51  * */
52 enum dc_edid_status dm_helpers_parse_edid_caps(
53                 struct dc_context *ctx,
54                 const struct dc_edid *edid,
55                 struct dc_edid_caps *edid_caps)
56 {
57         struct edid *edid_buf = (struct edid *) edid->raw_edid;
58         struct cea_sad *sads;
59         int sad_count = -1;
60         int sadb_count = -1;
61         int i = 0;
62         int j = 0;
63         uint8_t *sadb = NULL;
64
65         enum dc_edid_status result = EDID_OK;
66
67         if (!edid_caps || !edid)
68                 return EDID_BAD_INPUT;
69
70         if (!drm_edid_is_valid(edid_buf))
71                 result = EDID_BAD_CHECKSUM;
72
73         edid_caps->manufacturer_id = (uint16_t) edid_buf->mfg_id[0] |
74                                         ((uint16_t) edid_buf->mfg_id[1])<<8;
75         edid_caps->product_id = (uint16_t) edid_buf->prod_code[0] |
76                                         ((uint16_t) edid_buf->prod_code[1])<<8;
77         edid_caps->serial_number = edid_buf->serial;
78         edid_caps->manufacture_week = edid_buf->mfg_week;
79         edid_caps->manufacture_year = edid_buf->mfg_year;
80
81         /* One of the four detailed_timings stores the monitor name. It's
82          * stored in an array of length 13. */
83         for (i = 0; i < 4; i++) {
84                 if (edid_buf->detailed_timings[i].data.other_data.type == 0xfc) {
85                         while (j < 13 && edid_buf->detailed_timings[i].data.other_data.data.str.str[j]) {
86                                 if (edid_buf->detailed_timings[i].data.other_data.data.str.str[j] == '\n')
87                                         break;
88
89                                 edid_caps->display_name[j] =
90                                         edid_buf->detailed_timings[i].data.other_data.data.str.str[j];
91                                 j++;
92                         }
93                 }
94         }
95
96         edid_caps->edid_hdmi = drm_detect_hdmi_monitor(
97                         (struct edid *) edid->raw_edid);
98
99         sad_count = drm_edid_to_sad((struct edid *) edid->raw_edid, &sads);
100         if (sad_count < 0)
101                 DRM_ERROR("Couldn't read SADs: %d\n", sad_count);
102         if (sad_count <= 0)
103                 return result;
104
105         edid_caps->audio_mode_count = sad_count < DC_MAX_AUDIO_DESC_COUNT ? sad_count : DC_MAX_AUDIO_DESC_COUNT;
106         for (i = 0; i < edid_caps->audio_mode_count; ++i) {
107                 struct cea_sad *sad = &sads[i];
108
109                 edid_caps->audio_modes[i].format_code = sad->format;
110                 edid_caps->audio_modes[i].channel_count = sad->channels + 1;
111                 edid_caps->audio_modes[i].sample_rate = sad->freq;
112                 edid_caps->audio_modes[i].sample_size = sad->byte2;
113         }
114
115         sadb_count = drm_edid_to_speaker_allocation((struct edid *) edid->raw_edid, &sadb);
116
117         if (sadb_count < 0) {
118                 DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sadb_count);
119                 sadb_count = 0;
120         }
121
122         if (sadb_count)
123                 edid_caps->speaker_flags = sadb[0];
124         else
125                 edid_caps->speaker_flags = DEFAULT_SPEAKER_LOCATION;
126
127         kfree(sads);
128         kfree(sadb);
129
130         return result;
131 }
132
133 static void get_payload_table(
134                 struct amdgpu_dm_connector *aconnector,
135                 struct dp_mst_stream_allocation_table *proposed_table)
136 {
137         int i;
138         struct drm_dp_mst_topology_mgr *mst_mgr =
139                         &aconnector->mst_port->mst_mgr;
140
141         mutex_lock(&mst_mgr->payload_lock);
142
143         proposed_table->stream_count = 0;
144
145         /* number of active streams */
146         for (i = 0; i < mst_mgr->max_payloads; i++) {
147                 if (mst_mgr->payloads[i].num_slots == 0)
148                         break; /* end of vcp_id table */
149
150                 ASSERT(mst_mgr->payloads[i].payload_state !=
151                                 DP_PAYLOAD_DELETE_LOCAL);
152
153                 if (mst_mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL ||
154                         mst_mgr->payloads[i].payload_state ==
155                                         DP_PAYLOAD_REMOTE) {
156
157                         struct dp_mst_stream_allocation *sa =
158                                         &proposed_table->stream_allocations[
159                                                 proposed_table->stream_count];
160
161                         sa->slot_count = mst_mgr->payloads[i].num_slots;
162                         sa->vcp_id = mst_mgr->proposed_vcpis[i]->vcpi;
163                         proposed_table->stream_count++;
164                 }
165         }
166
167         mutex_unlock(&mst_mgr->payload_lock);
168 }
169
170 void dm_helpers_dp_update_branch_info(
171         struct dc_context *ctx,
172         const struct dc_link *link)
173 {}
174
175 /*
176  * Writes payload allocation table in immediate downstream device.
177  */
178 bool dm_helpers_dp_mst_write_payload_allocation_table(
179                 struct dc_context *ctx,
180                 const struct dc_stream_state *stream,
181                 struct dp_mst_stream_allocation_table *proposed_table,
182                 bool enable)
183 {
184         struct amdgpu_dm_connector *aconnector;
185         struct dm_connector_state *dm_conn_state;
186         struct drm_dp_mst_topology_mgr *mst_mgr;
187         struct drm_dp_mst_port *mst_port;
188         bool ret;
189
190         aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
191         /* Accessing the connector state is required for vcpi_slots allocation
192          * and directly relies on behaviour in commit check
193          * that blocks before commit guaranteeing that the state
194          * is not gonna be swapped while still in use in commit tail */
195
196         if (!aconnector || !aconnector->mst_port)
197                 return false;
198
199         dm_conn_state = to_dm_connector_state(aconnector->base.state);
200
201         mst_mgr = &aconnector->mst_port->mst_mgr;
202
203         if (!mst_mgr->mst_state)
204                 return false;
205
206         mst_port = aconnector->port;
207
208         if (enable) {
209
210                 ret = drm_dp_mst_allocate_vcpi(mst_mgr, mst_port,
211                                                dm_conn_state->pbn,
212                                                dm_conn_state->vcpi_slots);
213                 if (!ret)
214                         return false;
215
216         } else {
217                 drm_dp_mst_reset_vcpi_slots(mst_mgr, mst_port);
218         }
219
220         ret = drm_dp_update_payload_part1(mst_mgr);
221
222         /* mst_mgr->->payloads are VC payload notify MST branch using DPCD or
223          * AUX message. The sequence is slot 1-63 allocated sequence for each
224          * stream. AMD ASIC stream slot allocation should follow the same
225          * sequence. copy DRM MST allocation to dc */
226
227         get_payload_table(aconnector, proposed_table);
228
229         if (ret)
230                 return false;
231
232         return true;
233 }
234
235 /*
236  * poll pending down reply
237  */
238 void dm_helpers_dp_mst_poll_pending_down_reply(
239         struct dc_context *ctx,
240         const struct dc_link *link)
241 {}
242
243 /*
244  * Clear payload allocation table before enable MST DP link.
245  */
246 void dm_helpers_dp_mst_clear_payload_allocation_table(
247         struct dc_context *ctx,
248         const struct dc_link *link)
249 {}
250
251 /*
252  * Polls for ACT (allocation change trigger) handled and sends
253  * ALLOCATE_PAYLOAD message.
254  */
255 enum act_return_status dm_helpers_dp_mst_poll_for_allocation_change_trigger(
256                 struct dc_context *ctx,
257                 const struct dc_stream_state *stream)
258 {
259         struct amdgpu_dm_connector *aconnector;
260         struct drm_dp_mst_topology_mgr *mst_mgr;
261         int ret;
262
263         aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
264
265         if (!aconnector || !aconnector->mst_port)
266                 return ACT_FAILED;
267
268         mst_mgr = &aconnector->mst_port->mst_mgr;
269
270         if (!mst_mgr->mst_state)
271                 return ACT_FAILED;
272
273         ret = drm_dp_check_act_status(mst_mgr);
274
275         if (ret)
276                 return ACT_FAILED;
277
278         return ACT_SUCCESS;
279 }
280
281 bool dm_helpers_dp_mst_send_payload_allocation(
282                 struct dc_context *ctx,
283                 const struct dc_stream_state *stream,
284                 bool enable)
285 {
286         struct amdgpu_dm_connector *aconnector;
287         struct drm_dp_mst_topology_mgr *mst_mgr;
288         struct drm_dp_mst_port *mst_port;
289         int ret;
290
291         aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
292
293         if (!aconnector || !aconnector->mst_port)
294                 return false;
295
296         mst_port = aconnector->port;
297
298         mst_mgr = &aconnector->mst_port->mst_mgr;
299
300         if (!mst_mgr->mst_state)
301                 return false;
302
303         ret = drm_dp_update_payload_part2(mst_mgr);
304
305         if (ret)
306                 return false;
307
308         if (!enable)
309                 drm_dp_mst_deallocate_vcpi(mst_mgr, mst_port);
310
311         return true;
312 }
313
314 void dm_dtn_log_begin(struct dc_context *ctx,
315         struct dc_log_buffer_ctx *log_ctx)
316 {
317         static const char msg[] = "[dtn begin]\n";
318
319         if (!log_ctx) {
320                 pr_info("%s", msg);
321                 return;
322         }
323
324         dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
325 }
326
327 void dm_dtn_log_append_v(struct dc_context *ctx,
328         struct dc_log_buffer_ctx *log_ctx,
329         const char *msg, ...)
330 {
331         va_list args;
332         size_t total;
333         int n;
334
335         if (!log_ctx) {
336                 /* No context, redirect to dmesg. */
337                 struct va_format vaf;
338
339                 vaf.fmt = msg;
340                 vaf.va = &args;
341
342                 va_start(args, msg);
343                 pr_info("%pV", &vaf);
344                 va_end(args);
345
346                 return;
347         }
348
349         /* Measure the output. */
350         va_start(args, msg);
351         n = vsnprintf(NULL, 0, msg, args);
352         va_end(args);
353
354         if (n <= 0)
355                 return;
356
357         /* Reallocate the string buffer as needed. */
358         total = log_ctx->pos + n + 1;
359
360         if (total > log_ctx->size) {
361                 char *buf = (char *)kvcalloc(total, sizeof(char), GFP_KERNEL);
362
363                 if (buf) {
364                         memcpy(buf, log_ctx->buf, log_ctx->pos);
365                         kfree(log_ctx->buf);
366
367                         log_ctx->buf = buf;
368                         log_ctx->size = total;
369                 }
370         }
371
372         if (!log_ctx->buf)
373                 return;
374
375         /* Write the formatted string to the log buffer. */
376         va_start(args, msg);
377         n = vscnprintf(
378                 log_ctx->buf + log_ctx->pos,
379                 log_ctx->size - log_ctx->pos,
380                 msg,
381                 args);
382         va_end(args);
383
384         if (n > 0)
385                 log_ctx->pos += n;
386 }
387
388 void dm_dtn_log_end(struct dc_context *ctx,
389         struct dc_log_buffer_ctx *log_ctx)
390 {
391         static const char msg[] = "[dtn end]\n";
392
393         if (!log_ctx) {
394                 pr_info("%s", msg);
395                 return;
396         }
397
398         dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
399 }
400
401 bool dm_helpers_dp_mst_start_top_mgr(
402                 struct dc_context *ctx,
403                 const struct dc_link *link,
404                 bool boot)
405 {
406         struct amdgpu_dm_connector *aconnector = link->priv;
407
408         if (!aconnector) {
409                         DRM_ERROR("Failed to found connector for link!");
410                         return false;
411         }
412
413         if (boot) {
414                 DRM_INFO("DM_MST: Differing MST start on aconnector: %p [id: %d]\n",
415                                         aconnector, aconnector->base.base.id);
416                 return true;
417         }
418
419         DRM_INFO("DM_MST: starting TM on aconnector: %p [id: %d]\n",
420                         aconnector, aconnector->base.base.id);
421
422         return (drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true) == 0);
423 }
424
425 void dm_helpers_dp_mst_stop_top_mgr(
426                 struct dc_context *ctx,
427                 const struct dc_link *link)
428 {
429         struct amdgpu_dm_connector *aconnector = link->priv;
430
431         if (!aconnector) {
432                         DRM_ERROR("Failed to found connector for link!");
433                         return;
434         }
435
436         DRM_INFO("DM_MST: stopping TM on aconnector: %p [id: %d]\n",
437                         aconnector, aconnector->base.base.id);
438
439         if (aconnector->mst_mgr.mst_state == true)
440                 drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, false);
441 }
442
443 bool dm_helpers_dp_read_dpcd(
444                 struct dc_context *ctx,
445                 const struct dc_link *link,
446                 uint32_t address,
447                 uint8_t *data,
448                 uint32_t size)
449 {
450
451         struct amdgpu_dm_connector *aconnector = link->priv;
452
453         if (!aconnector) {
454                 DRM_ERROR("Failed to found connector for link!");
455                 return false;
456         }
457
458         return drm_dp_dpcd_read(&aconnector->dm_dp_aux.aux, address,
459                         data, size) > 0;
460 }
461
462 bool dm_helpers_dp_write_dpcd(
463                 struct dc_context *ctx,
464                 const struct dc_link *link,
465                 uint32_t address,
466                 const uint8_t *data,
467                 uint32_t size)
468 {
469         struct amdgpu_dm_connector *aconnector = link->priv;
470
471         if (!aconnector) {
472                 DRM_ERROR("Failed to found connector for link!");
473                 return false;
474         }
475
476         return drm_dp_dpcd_write(&aconnector->dm_dp_aux.aux,
477                         address, (uint8_t *)data, size) > 0;
478 }
479
480 bool dm_helpers_submit_i2c(
481                 struct dc_context *ctx,
482                 const struct dc_link *link,
483                 struct i2c_command *cmd)
484 {
485         struct amdgpu_dm_connector *aconnector = link->priv;
486         struct i2c_msg *msgs;
487         int i = 0;
488         int num = cmd->number_of_payloads;
489         bool result;
490
491         if (!aconnector) {
492                 DRM_ERROR("Failed to found connector for link!");
493                 return false;
494         }
495
496         msgs = kcalloc(num, sizeof(struct i2c_msg), GFP_KERNEL);
497
498         if (!msgs)
499                 return false;
500
501         for (i = 0; i < num; i++) {
502                 msgs[i].flags = cmd->payloads[i].write ? 0 : I2C_M_RD;
503                 msgs[i].addr = cmd->payloads[i].address;
504                 msgs[i].len = cmd->payloads[i].length;
505                 msgs[i].buf = cmd->payloads[i].data;
506         }
507
508         result = i2c_transfer(&aconnector->i2c->base, msgs, num) == num;
509
510         kfree(msgs);
511
512         return result;
513 }
514 #ifdef CONFIG_DRM_AMD_DC_DSC_SUPPORT
515 bool dm_helpers_dp_write_dsc_enable(
516                 struct dc_context *ctx,
517                 const struct dc_stream_state *stream,
518                 bool enable
519 )
520 {
521         uint8_t enable_dsc = enable ? 1 : 0;
522
523         return dm_helpers_dp_write_dpcd(ctx, stream->sink->link, DP_DSC_ENABLE, &enable_dsc, 1);
524 }
525 #endif
526
527 bool dm_helpers_is_dp_sink_present(struct dc_link *link)
528 {
529         bool dp_sink_present;
530         struct amdgpu_dm_connector *aconnector = link->priv;
531
532         if (!aconnector) {
533                 BUG_ON("Failed to found connector for link!");
534                 return true;
535         }
536
537         mutex_lock(&aconnector->dm_dp_aux.aux.hw_mutex);
538         dp_sink_present = dc_link_is_dp_sink_present(link);
539         mutex_unlock(&aconnector->dm_dp_aux.aux.hw_mutex);
540         return dp_sink_present;
541 }
542
543 enum dc_edid_status dm_helpers_read_local_edid(
544                 struct dc_context *ctx,
545                 struct dc_link *link,
546                 struct dc_sink *sink)
547 {
548         struct amdgpu_dm_connector *aconnector = link->priv;
549         struct i2c_adapter *ddc;
550         int retry = 3;
551         enum dc_edid_status edid_status;
552         struct edid *edid;
553
554         if (link->aux_mode)
555                 ddc = &aconnector->dm_dp_aux.aux.ddc;
556         else
557                 ddc = &aconnector->i2c->base;
558
559         /* some dongles read edid incorrectly the first time,
560          * do check sum and retry to make sure read correct edid.
561          */
562         do {
563
564                 edid = drm_get_edid(&aconnector->base, ddc);
565
566                 if (!edid)
567                         return EDID_NO_RESPONSE;
568
569                 sink->dc_edid.length = EDID_LENGTH * (edid->extensions + 1);
570                 memmove(sink->dc_edid.raw_edid, (uint8_t *)edid, sink->dc_edid.length);
571
572                 /* We don't need the original edid anymore */
573                 kfree(edid);
574
575                 edid_status = dm_helpers_parse_edid_caps(
576                                                 ctx,
577                                                 &sink->dc_edid,
578                                                 &sink->edid_caps);
579
580         } while (edid_status == EDID_BAD_CHECKSUM && --retry > 0);
581
582         if (edid_status != EDID_OK)
583                 DRM_ERROR("EDID err: %d, on connector: %s",
584                                 edid_status,
585                                 aconnector->base.name);
586         if (link->aux_mode) {
587                 union test_request test_request = { {0} };
588                 union test_response test_response = { {0} };
589
590                 dm_helpers_dp_read_dpcd(ctx,
591                                         link,
592                                         DP_TEST_REQUEST,
593                                         &test_request.raw,
594                                         sizeof(union test_request));
595
596                 if (!test_request.bits.EDID_READ)
597                         return edid_status;
598
599                 test_response.bits.EDID_CHECKSUM_WRITE = 1;
600
601                 dm_helpers_dp_write_dpcd(ctx,
602                                         link,
603                                         DP_TEST_EDID_CHECKSUM,
604                                         &sink->dc_edid.raw_edid[sink->dc_edid.length-1],
605                                         1);
606
607                 dm_helpers_dp_write_dpcd(ctx,
608                                         link,
609                                         DP_TEST_RESPONSE,
610                                         &test_response.raw,
611                                         sizeof(test_response));
612
613         }
614
615         return edid_status;
616 }
617
618 void dm_set_dcn_clocks(struct dc_context *ctx, struct dc_clocks *clks)
619 {
620         /* TODO: something */
621 }