2 * Copyright 2015 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
26 #include <linux/string.h>
27 #include <linux/acpi.h>
28 #include <linux/i2c.h>
30 #include <drm/drm_probe_helper.h>
31 #include <drm/amdgpu_drm.h>
32 #include <drm/drm_edid.h>
34 #include "dm_services.h"
37 #include "amdgpu_dm.h"
38 #include "amdgpu_dm_irq.h"
39 #include "amdgpu_dm_mst_types.h"
41 #include "dm_helpers.h"
43 struct monitor_patch_info {
44 unsigned int manufacturer_id;
45 unsigned int product_id;
46 void (*patch_func)(struct dc_edid_caps *edid_caps, unsigned int param);
47 unsigned int patch_param;
49 static void set_max_dsc_bpp_limit(struct dc_edid_caps *edid_caps, unsigned int param);
51 static const struct monitor_patch_info monitor_patch_table[] = {
52 {0x6D1E, 0x5BBF, set_max_dsc_bpp_limit, 15},
53 {0x6D1E, 0x5B9A, set_max_dsc_bpp_limit, 15},
56 static void set_max_dsc_bpp_limit(struct dc_edid_caps *edid_caps, unsigned int param)
59 edid_caps->panel_patch.max_dsc_target_bpp_limit = param;
62 static int amdgpu_dm_patch_edid_caps(struct dc_edid_caps *edid_caps)
66 for (i = 0; i < ARRAY_SIZE(monitor_patch_table); i++)
67 if ((edid_caps->manufacturer_id == monitor_patch_table[i].manufacturer_id)
68 && (edid_caps->product_id == monitor_patch_table[i].product_id)) {
69 monitor_patch_table[i].patch_func(edid_caps, monitor_patch_table[i].patch_param);
76 /* dm_helpers_parse_edid_caps
80 * @edid: [in] pointer to edid
81 * edid_caps: [in] pointer to edid caps
85 enum dc_edid_status dm_helpers_parse_edid_caps(
86 struct dc_context *ctx,
87 const struct dc_edid *edid,
88 struct dc_edid_caps *edid_caps)
90 struct edid *edid_buf = (struct edid *) edid->raw_edid;
98 enum dc_edid_status result = EDID_OK;
100 if (!edid_caps || !edid)
101 return EDID_BAD_INPUT;
103 if (!drm_edid_is_valid(edid_buf))
104 result = EDID_BAD_CHECKSUM;
106 edid_caps->manufacturer_id = (uint16_t) edid_buf->mfg_id[0] |
107 ((uint16_t) edid_buf->mfg_id[1])<<8;
108 edid_caps->product_id = (uint16_t) edid_buf->prod_code[0] |
109 ((uint16_t) edid_buf->prod_code[1])<<8;
110 edid_caps->serial_number = edid_buf->serial;
111 edid_caps->manufacture_week = edid_buf->mfg_week;
112 edid_caps->manufacture_year = edid_buf->mfg_year;
114 /* One of the four detailed_timings stores the monitor name. It's
115 * stored in an array of length 13. */
116 for (i = 0; i < 4; i++) {
117 if (edid_buf->detailed_timings[i].data.other_data.type == 0xfc) {
118 while (j < 13 && edid_buf->detailed_timings[i].data.other_data.data.str.str[j]) {
119 if (edid_buf->detailed_timings[i].data.other_data.data.str.str[j] == '\n')
122 edid_caps->display_name[j] =
123 edid_buf->detailed_timings[i].data.other_data.data.str.str[j];
129 edid_caps->edid_hdmi = drm_detect_hdmi_monitor(
130 (struct edid *) edid->raw_edid);
132 sad_count = drm_edid_to_sad((struct edid *) edid->raw_edid, &sads);
136 edid_caps->audio_mode_count = sad_count < DC_MAX_AUDIO_DESC_COUNT ? sad_count : DC_MAX_AUDIO_DESC_COUNT;
137 for (i = 0; i < edid_caps->audio_mode_count; ++i) {
138 struct cea_sad *sad = &sads[i];
140 edid_caps->audio_modes[i].format_code = sad->format;
141 edid_caps->audio_modes[i].channel_count = sad->channels + 1;
142 edid_caps->audio_modes[i].sample_rate = sad->freq;
143 edid_caps->audio_modes[i].sample_size = sad->byte2;
146 sadb_count = drm_edid_to_speaker_allocation((struct edid *) edid->raw_edid, &sadb);
148 if (sadb_count < 0) {
149 DRM_ERROR("Couldn't read Speaker Allocation Data Block: %d\n", sadb_count);
154 edid_caps->speaker_flags = sadb[0];
156 edid_caps->speaker_flags = DEFAULT_SPEAKER_LOCATION;
161 amdgpu_dm_patch_edid_caps(edid_caps);
166 static void get_payload_table(
167 struct amdgpu_dm_connector *aconnector,
168 struct dp_mst_stream_allocation_table *proposed_table)
171 struct drm_dp_mst_topology_mgr *mst_mgr =
172 &aconnector->mst_port->mst_mgr;
174 mutex_lock(&mst_mgr->payload_lock);
176 proposed_table->stream_count = 0;
178 /* number of active streams */
179 for (i = 0; i < mst_mgr->max_payloads; i++) {
180 if (mst_mgr->payloads[i].num_slots == 0)
181 break; /* end of vcp_id table */
183 ASSERT(mst_mgr->payloads[i].payload_state !=
184 DP_PAYLOAD_DELETE_LOCAL);
186 if (mst_mgr->payloads[i].payload_state == DP_PAYLOAD_LOCAL ||
187 mst_mgr->payloads[i].payload_state ==
190 struct dp_mst_stream_allocation *sa =
191 &proposed_table->stream_allocations[
192 proposed_table->stream_count];
194 sa->slot_count = mst_mgr->payloads[i].num_slots;
195 sa->vcp_id = mst_mgr->proposed_vcpis[i]->vcpi;
196 proposed_table->stream_count++;
200 mutex_unlock(&mst_mgr->payload_lock);
203 void dm_helpers_dp_update_branch_info(
204 struct dc_context *ctx,
205 const struct dc_link *link)
209 * Writes payload allocation table in immediate downstream device.
211 bool dm_helpers_dp_mst_write_payload_allocation_table(
212 struct dc_context *ctx,
213 const struct dc_stream_state *stream,
214 struct dp_mst_stream_allocation_table *proposed_table,
217 struct amdgpu_dm_connector *aconnector;
218 struct dm_connector_state *dm_conn_state;
219 struct drm_dp_mst_topology_mgr *mst_mgr;
220 struct drm_dp_mst_port *mst_port;
223 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
224 /* Accessing the connector state is required for vcpi_slots allocation
225 * and directly relies on behaviour in commit check
226 * that blocks before commit guaranteeing that the state
227 * is not gonna be swapped while still in use in commit tail */
229 if (!aconnector || !aconnector->mst_port)
232 dm_conn_state = to_dm_connector_state(aconnector->base.state);
234 mst_mgr = &aconnector->mst_port->mst_mgr;
236 if (!mst_mgr->mst_state)
239 mst_port = aconnector->port;
243 ret = drm_dp_mst_allocate_vcpi(mst_mgr, mst_port,
245 dm_conn_state->vcpi_slots);
250 drm_dp_mst_reset_vcpi_slots(mst_mgr, mst_port);
253 /* It's OK for this to fail */
254 drm_dp_update_payload_part1(mst_mgr, 1);
256 /* mst_mgr->->payloads are VC payload notify MST branch using DPCD or
257 * AUX message. The sequence is slot 1-63 allocated sequence for each
258 * stream. AMD ASIC stream slot allocation should follow the same
259 * sequence. copy DRM MST allocation to dc */
261 get_payload_table(aconnector, proposed_table);
267 * poll pending down reply
269 void dm_helpers_dp_mst_poll_pending_down_reply(
270 struct dc_context *ctx,
271 const struct dc_link *link)
275 * Clear payload allocation table before enable MST DP link.
277 void dm_helpers_dp_mst_clear_payload_allocation_table(
278 struct dc_context *ctx,
279 const struct dc_link *link)
283 * Polls for ACT (allocation change trigger) handled and sends
284 * ALLOCATE_PAYLOAD message.
286 enum act_return_status dm_helpers_dp_mst_poll_for_allocation_change_trigger(
287 struct dc_context *ctx,
288 const struct dc_stream_state *stream)
290 struct amdgpu_dm_connector *aconnector;
291 struct drm_dp_mst_topology_mgr *mst_mgr;
294 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
296 if (!aconnector || !aconnector->mst_port)
299 mst_mgr = &aconnector->mst_port->mst_mgr;
301 if (!mst_mgr->mst_state)
304 ret = drm_dp_check_act_status(mst_mgr);
312 bool dm_helpers_dp_mst_send_payload_allocation(
313 struct dc_context *ctx,
314 const struct dc_stream_state *stream,
317 struct amdgpu_dm_connector *aconnector;
318 struct drm_dp_mst_topology_mgr *mst_mgr;
319 struct drm_dp_mst_port *mst_port;
321 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
323 if (!aconnector || !aconnector->mst_port)
326 mst_port = aconnector->port;
328 mst_mgr = &aconnector->mst_port->mst_mgr;
330 if (!mst_mgr->mst_state)
333 /* It's OK for this to fail */
334 drm_dp_update_payload_part2(mst_mgr);
337 drm_dp_mst_deallocate_vcpi(mst_mgr, mst_port);
342 void dm_dtn_log_begin(struct dc_context *ctx,
343 struct dc_log_buffer_ctx *log_ctx)
345 static const char msg[] = "[dtn begin]\n";
352 dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
356 void dm_dtn_log_append_v(struct dc_context *ctx,
357 struct dc_log_buffer_ctx *log_ctx,
358 const char *msg, ...)
365 /* No context, redirect to dmesg. */
366 struct va_format vaf;
372 pr_info("%pV", &vaf);
378 /* Measure the output. */
380 n = vsnprintf(NULL, 0, msg, args);
386 /* Reallocate the string buffer as needed. */
387 total = log_ctx->pos + n + 1;
389 if (total > log_ctx->size) {
390 char *buf = (char *)kvcalloc(total, sizeof(char), GFP_KERNEL);
393 memcpy(buf, log_ctx->buf, log_ctx->pos);
397 log_ctx->size = total;
404 /* Write the formatted string to the log buffer. */
407 log_ctx->buf + log_ctx->pos,
408 log_ctx->size - log_ctx->pos,
417 void dm_dtn_log_end(struct dc_context *ctx,
418 struct dc_log_buffer_ctx *log_ctx)
420 static const char msg[] = "[dtn end]\n";
427 dm_dtn_log_append_v(ctx, log_ctx, "%s", msg);
430 bool dm_helpers_dp_mst_start_top_mgr(
431 struct dc_context *ctx,
432 const struct dc_link *link,
435 struct amdgpu_dm_connector *aconnector = link->priv;
438 DRM_ERROR("Failed to find connector for link!");
443 DRM_INFO("DM_MST: Differing MST start on aconnector: %p [id: %d]\n",
444 aconnector, aconnector->base.base.id);
448 DRM_INFO("DM_MST: starting TM on aconnector: %p [id: %d]\n",
449 aconnector, aconnector->base.base.id);
451 return (drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, true) == 0);
454 void dm_helpers_dp_mst_stop_top_mgr(
455 struct dc_context *ctx,
456 struct dc_link *link)
458 struct amdgpu_dm_connector *aconnector = link->priv;
462 DRM_ERROR("Failed to find connector for link!");
466 DRM_INFO("DM_MST: stopping TM on aconnector: %p [id: %d]\n",
467 aconnector, aconnector->base.base.id);
469 if (aconnector->mst_mgr.mst_state == true) {
470 drm_dp_mst_topology_mgr_set_mst(&aconnector->mst_mgr, false);
472 for (i = 0; i < MAX_SINKS_PER_LINK; i++) {
473 if (link->remote_sinks[i] == NULL)
476 if (link->remote_sinks[i]->sink_signal ==
477 SIGNAL_TYPE_DISPLAY_PORT_MST) {
478 dc_link_remove_remote_sink(link, link->remote_sinks[i]);
480 if (aconnector->dc_sink) {
481 dc_sink_release(aconnector->dc_sink);
482 aconnector->dc_sink = NULL;
483 aconnector->dc_link->cur_link_settings.lane_count = 0;
490 bool dm_helpers_dp_read_dpcd(
491 struct dc_context *ctx,
492 const struct dc_link *link,
498 struct amdgpu_dm_connector *aconnector = link->priv;
501 DC_LOG_DC("Failed to find connector for link!\n");
505 return drm_dp_dpcd_read(&aconnector->dm_dp_aux.aux, address,
509 bool dm_helpers_dp_write_dpcd(
510 struct dc_context *ctx,
511 const struct dc_link *link,
516 struct amdgpu_dm_connector *aconnector = link->priv;
519 DRM_ERROR("Failed to find connector for link!");
523 return drm_dp_dpcd_write(&aconnector->dm_dp_aux.aux,
524 address, (uint8_t *)data, size) > 0;
527 bool dm_helpers_submit_i2c(
528 struct dc_context *ctx,
529 const struct dc_link *link,
530 struct i2c_command *cmd)
532 struct amdgpu_dm_connector *aconnector = link->priv;
533 struct i2c_msg *msgs;
535 int num = cmd->number_of_payloads;
539 DRM_ERROR("Failed to find connector for link!");
543 msgs = kcalloc(num, sizeof(struct i2c_msg), GFP_KERNEL);
548 for (i = 0; i < num; i++) {
549 msgs[i].flags = cmd->payloads[i].write ? 0 : I2C_M_RD;
550 msgs[i].addr = cmd->payloads[i].address;
551 msgs[i].len = cmd->payloads[i].length;
552 msgs[i].buf = cmd->payloads[i].data;
555 result = i2c_transfer(&aconnector->i2c->base, msgs, num) == num;
561 bool dm_helpers_dp_write_dsc_enable(
562 struct dc_context *ctx,
563 const struct dc_stream_state *stream,
566 uint8_t enable_dsc = enable ? 1 : 0;
567 struct amdgpu_dm_connector *aconnector;
573 if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT_MST) {
574 aconnector = (struct amdgpu_dm_connector *)stream->dm_stream_context;
576 if (!aconnector->dsc_aux)
579 ret = drm_dp_dpcd_write(aconnector->dsc_aux, DP_DSC_ENABLE, &enable_dsc, 1);
582 if (stream->signal == SIGNAL_TYPE_DISPLAY_PORT) {
583 ret = dm_helpers_dp_write_dpcd(ctx, stream->link, DP_DSC_ENABLE, &enable_dsc, 1);
584 DC_LOG_DC("Send DSC %s to sst display\n", enable_dsc ? "enable" : "disable");
590 bool dm_helpers_is_dp_sink_present(struct dc_link *link)
592 bool dp_sink_present;
593 struct amdgpu_dm_connector *aconnector = link->priv;
596 BUG_ON("Failed to find connector for link!");
600 mutex_lock(&aconnector->dm_dp_aux.aux.hw_mutex);
601 dp_sink_present = dc_link_is_dp_sink_present(link);
602 mutex_unlock(&aconnector->dm_dp_aux.aux.hw_mutex);
603 return dp_sink_present;
606 enum dc_edid_status dm_helpers_read_local_edid(
607 struct dc_context *ctx,
608 struct dc_link *link,
609 struct dc_sink *sink)
611 struct amdgpu_dm_connector *aconnector = link->priv;
612 struct drm_connector *connector = &aconnector->base;
613 struct i2c_adapter *ddc;
615 enum dc_edid_status edid_status;
619 ddc = &aconnector->dm_dp_aux.aux.ddc;
621 ddc = &aconnector->i2c->base;
623 /* some dongles read edid incorrectly the first time,
624 * do check sum and retry to make sure read correct edid.
628 edid = drm_get_edid(&aconnector->base, ddc);
630 /* DP Compliance Test 4.2.2.6 */
631 if (link->aux_mode && connector->edid_corrupt)
632 drm_dp_send_real_edid_checksum(&aconnector->dm_dp_aux.aux, connector->real_edid_checksum);
634 if (!edid && connector->edid_corrupt) {
635 connector->edid_corrupt = false;
636 return EDID_BAD_CHECKSUM;
640 return EDID_NO_RESPONSE;
642 sink->dc_edid.length = EDID_LENGTH * (edid->extensions + 1);
643 memmove(sink->dc_edid.raw_edid, (uint8_t *)edid, sink->dc_edid.length);
645 /* We don't need the original edid anymore */
648 /* connector->display_info will be parsed from EDID and saved
649 * into drm_connector->display_info from edid by call stack
651 * drm_parse_ycbcr420_deep_color_info
652 * drm_parse_hdmi_forum_vsdb
654 * drm_add_display_info
655 * drm_connector_update_edid_property
657 * drm_connector->display_info will be used by amdgpu_dm funcs,
658 * like fill_stream_properties_from_drm_display_mode
660 amdgpu_dm_update_connector_after_detect(aconnector);
662 edid_status = dm_helpers_parse_edid_caps(
667 } while (edid_status == EDID_BAD_CHECKSUM && --retry > 0);
669 if (edid_status != EDID_OK)
670 DRM_ERROR("EDID err: %d, on connector: %s",
672 aconnector->base.name);
674 /* DP Compliance Test 4.2.2.3 */
676 drm_dp_send_real_edid_checksum(&aconnector->dm_dp_aux.aux, sink->dc_edid.raw_edid[sink->dc_edid.length-1]);
680 int dm_helper_dmub_aux_transfer_sync(
681 struct dc_context *ctx,
682 const struct dc_link *link,
683 struct aux_payload *payload,
684 enum aux_return_code_type *operation_result)
686 return amdgpu_dm_process_dmub_aux_transfer_sync(true, ctx,
687 link->link_index, (void *)payload,
688 (void *)operation_result);
691 int dm_helpers_dmub_set_config_sync(struct dc_context *ctx,
692 const struct dc_link *link,
693 struct set_config_cmd_payload *payload,
694 enum set_config_status *operation_result)
696 return amdgpu_dm_process_dmub_aux_transfer_sync(false, ctx,
697 link->link_index, (void *)payload,
698 (void *)operation_result);
701 void dm_set_dcn_clocks(struct dc_context *ctx, struct dc_clocks *clks)
703 /* TODO: something */
706 void dm_helpers_smu_timeout(struct dc_context *ctx, unsigned int msg_id, unsigned int param, unsigned int timeout_us)
709 //amdgpu_device_gpu_recover(dc_context->driver-context, NULL);
712 void *dm_helpers_allocate_gpu_mem(
713 struct dc_context *ctx,
714 enum dc_gpu_mem_alloc_type type,
718 struct amdgpu_device *adev = ctx->driver_context;
719 struct dal_allocation *da;
720 u32 domain = (type == DC_MEM_ALLOC_TYPE_GART) ?
721 AMDGPU_GEM_DOMAIN_GTT : AMDGPU_GEM_DOMAIN_VRAM;
724 da = kzalloc(sizeof(struct dal_allocation), GFP_KERNEL);
728 ret = amdgpu_bo_create_kernel(adev, size, PAGE_SIZE,
730 &da->gpu_addr, &da->cpu_ptr);
732 *addr = da->gpu_addr;
739 /* add da to list in dm */
740 list_add(&da->list, &adev->dm.da_list);
745 void dm_helpers_free_gpu_mem(
746 struct dc_context *ctx,
747 enum dc_gpu_mem_alloc_type type,
750 struct amdgpu_device *adev = ctx->driver_context;
751 struct dal_allocation *da;
753 /* walk the da list in DM */
754 list_for_each_entry(da, &adev->dm.da_list, list) {
755 if (pvMem == da->cpu_ptr) {
756 amdgpu_bo_free_kernel(&da->bo, &da->gpu_addr, &da->cpu_ptr);
764 bool dm_helpers_dmub_outbox_interrupt_control(struct dc_context *ctx, bool enable)
766 enum dc_irq_source irq_source;
769 irq_source = DC_IRQ_SOURCE_DMCUB_OUTBOX;
771 ret = dc_interrupt_set(ctx->dc, irq_source, enable);
773 DRM_DEBUG_DRIVER("Dmub trace irq %sabling: r=%d\n",
774 enable ? "en" : "dis", ret);
778 void dm_helpers_mst_enable_stream_features(const struct dc_stream_state *stream)
780 /* TODO: virtual DPCD */
781 struct dc_link *link = stream->link;
782 union down_spread_ctrl old_downspread;
783 union down_spread_ctrl new_downspread;
785 if (link->aux_access_disabled)
788 if (!dm_helpers_dp_read_dpcd(link->ctx, link, DP_DOWNSPREAD_CTRL,
790 sizeof(old_downspread)))
793 new_downspread.raw = old_downspread.raw;
794 new_downspread.bits.IGNORE_MSA_TIMING_PARAM =
795 (stream->ignore_msa_timing_param) ? 1 : 0;
797 if (new_downspread.raw != old_downspread.raw)
798 dm_helpers_dp_write_dpcd(link->ctx, link, DP_DOWNSPREAD_CTRL,
800 sizeof(new_downspread));
803 #if defined(CONFIG_DRM_AMD_DC_DCN)
804 void dm_set_phyd32clk(struct dc_context *ctx, int freq_khz)
806 // FPGA programming for this clock in diags framework that
807 // needs to go through dm layer, therefore leave dummy interace here
811 void dm_helpers_enable_periodic_detection(struct dc_context *ctx, bool enable)
813 /* TODO: add peridic detection implementation */