1 /*
2  * Copyright 2019 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 "dc.h"
27 #include "dc_dmub_srv.h"
28 #include "../dmub/dmub_srv.h"
29 #include "dm_helpers.h"
30 
31 #define CTX dc_dmub_srv->ctx
32 #define DC_LOGGER CTX->logger
33 
34 static void dc_dmub_srv_construct(struct dc_dmub_srv *dc_srv, struct dc *dc,
35 				  struct dmub_srv *dmub)
36 {
37 	dc_srv->dmub = dmub;
38 	dc_srv->ctx = dc->ctx;
39 }
40 
41 struct dc_dmub_srv *dc_dmub_srv_create(struct dc *dc, struct dmub_srv *dmub)
42 {
43 	struct dc_dmub_srv *dc_srv =
44 		kzalloc(sizeof(struct dc_dmub_srv), GFP_KERNEL);
45 
46 	if (dc_srv == NULL) {
47 		BREAK_TO_DEBUGGER();
48 		return NULL;
49 	}
50 
51 	dc_dmub_srv_construct(dc_srv, dc, dmub);
52 
53 	return dc_srv;
54 }
55 
56 void dc_dmub_srv_destroy(struct dc_dmub_srv **dmub_srv)
57 {
58 	if (*dmub_srv) {
59 		kfree(*dmub_srv);
60 		*dmub_srv = NULL;
61 	}
62 }
63 
64 void dc_dmub_srv_cmd_queue(struct dc_dmub_srv *dc_dmub_srv,
65 			   union dmub_rb_cmd *cmd)
66 {
67 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
68 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
69 	enum dmub_status status;
70 
71 	status = dmub_srv_cmd_queue(dmub, cmd);
72 	if (status == DMUB_STATUS_OK)
73 		return;
74 
75 	if (status != DMUB_STATUS_QUEUE_FULL)
76 		goto error;
77 
78 	/* Execute and wait for queue to become empty again. */
79 	dc_dmub_srv_cmd_execute(dc_dmub_srv);
80 	dc_dmub_srv_wait_idle(dc_dmub_srv);
81 
82 	/* Requeue the command. */
83 	status = dmub_srv_cmd_queue(dmub, cmd);
84 	if (status == DMUB_STATUS_OK)
85 		return;
86 
87 error:
88 	DC_ERROR("Error queuing DMUB command: status=%d\n", status);
89 }
90 
91 void dc_dmub_srv_cmd_execute(struct dc_dmub_srv *dc_dmub_srv)
92 {
93 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
94 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
95 	enum dmub_status status;
96 
97 	status = dmub_srv_cmd_execute(dmub);
98 	if (status != DMUB_STATUS_OK)
99 		DC_ERROR("Error starting DMUB execution: status=%d\n", status);
100 }
101 
102 void dc_dmub_srv_wait_idle(struct dc_dmub_srv *dc_dmub_srv)
103 {
104 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
105 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
106 	enum dmub_status status;
107 
108 	status = dmub_srv_wait_for_idle(dmub, 100000);
109 	if (status != DMUB_STATUS_OK)
110 		DC_ERROR("Error waiting for DMUB idle: status=%d\n", status);
111 }
112 
113 bool dc_dmub_srv_cmd_with_reply_data(struct dc_dmub_srv *dc_dmub_srv, union dmub_rb_cmd *cmd)
114 {
115 	struct dmub_srv *dmub;
116 	enum dmub_status status;
117 
118 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
119 		return false;
120 
121 	dmub = dc_dmub_srv->dmub;
122 
123 	status = dmub_srv_cmd_with_reply_data(dmub, cmd);
124 	if (status != DMUB_STATUS_OK) {
125 		DC_LOG_DEBUG("No reply for DMUB command: status=%d\n", status);
126 		return false;
127 	}
128 
129 	return true;
130 }
131 
132 void dc_dmub_srv_wait_phy_init(struct dc_dmub_srv *dc_dmub_srv)
133 {
134 	struct dmub_srv *dmub = dc_dmub_srv->dmub;
135 	struct dc_context *dc_ctx = dc_dmub_srv->ctx;
136 	enum dmub_status status;
137 
138 	for (;;) {
139 		/* Wait up to a second for PHY init. */
140 		status = dmub_srv_wait_for_phy_init(dmub, 1000000);
141 		if (status == DMUB_STATUS_OK)
142 			/* Initialization OK */
143 			break;
144 
145 		DC_ERROR("DMCUB PHY init failed: status=%d\n", status);
146 		ASSERT(0);
147 
148 		if (status != DMUB_STATUS_TIMEOUT)
149 			/*
150 			 * Server likely initialized or we don't have
151 			 * DMCUB HW support - this won't end.
152 			 */
153 			break;
154 
155 		/* Continue spinning so we don't hang the ASIC. */
156 	}
157 }
158 
159 bool dc_dmub_srv_notify_stream_mask(struct dc_dmub_srv *dc_dmub_srv,
160 				    unsigned int stream_mask)
161 {
162 	struct dmub_srv *dmub;
163 	const uint32_t timeout = 30;
164 
165 	if (!dc_dmub_srv || !dc_dmub_srv->dmub)
166 		return false;
167 
168 	dmub = dc_dmub_srv->dmub;
169 
170 	return dmub_srv_send_gpint_command(
171 		       dmub, DMUB_GPINT__IDLE_OPT_NOTIFY_STREAM_MASK,
172 		       stream_mask, timeout) == DMUB_STATUS_OK;
173 }
174 
175 bool dc_dmub_srv_get_dmub_outbox0_msg(const struct dc *dc, struct dmcub_trace_buf_entry *entry)
176 {
177 	struct dmub_srv *dmub = dc->ctx->dmub_srv->dmub;
178 	return dmub_srv_get_outbox0_msg(dmub, entry);
179 }
180 
181 void dc_dmub_trace_event_control(struct dc *dc, bool enable)
182 {
183 	dm_helpers_dmub_outbox0_interrupt_control(dc->ctx, enable);
184 }
185