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 "../dmub_srv.h"
27 #include "dmub_dcn20.h"
28 #include "dmub_dcn21.h"
29 #include "dmub_cmd.h"
30 #include "dmub_dcn30.h"
31 #include "dmub_dcn301.h"
32 #include "dmub_dcn302.h"
33 #include "os_types.h"
34 /*
35  * Note: the DMUB service is standalone. No additional headers should be
36  * added below or above this line unless they reside within the DMUB
37  * folder.
38  */
39 
40 /* Alignment for framebuffer memory. */
41 #define DMUB_FB_ALIGNMENT (1024 * 1024)
42 
43 /* Stack size. */
44 #define DMUB_STACK_SIZE (128 * 1024)
45 
46 /* Context size. */
47 #define DMUB_CONTEXT_SIZE (512 * 1024)
48 
49 /* Mailbox size : Ring buffers are required for both inbox and outbox */
50 #define DMUB_MAILBOX_SIZE ((2 * DMUB_RB_SIZE))
51 
52 /* Default state size if meta is absent. */
53 #define DMUB_FW_STATE_SIZE (64 * 1024)
54 
55 /* Default tracebuffer size if meta is absent. */
56 #define DMUB_TRACE_BUFFER_SIZE (64 * 1024)
57 
58 
59 /* Default scratch mem size. */
60 #define DMUB_SCRATCH_MEM_SIZE (256)
61 
62 /* Number of windows in use. */
63 #define DMUB_NUM_WINDOWS (DMUB_WINDOW_TOTAL)
64 /* Base addresses. */
65 
66 #define DMUB_CW0_BASE (0x60000000)
67 #define DMUB_CW1_BASE (0x61000000)
68 #define DMUB_CW3_BASE (0x63000000)
69 #define DMUB_CW4_BASE (0x64000000)
70 #define DMUB_CW5_BASE (0x65000000)
71 #define DMUB_CW6_BASE (0x66000000)
72 
73 #define DMUB_REGION5_BASE (0xA0000000)
74 
75 static inline uint32_t dmub_align(uint32_t val, uint32_t factor)
76 {
77 	return (val + factor - 1) / factor * factor;
78 }
79 
80 void dmub_flush_buffer_mem(const struct dmub_fb *fb)
81 {
82 	const uint8_t *base = (const uint8_t *)fb->cpu_addr;
83 	uint8_t buf[64];
84 	uint32_t pos, end;
85 
86 	/**
87 	 * Read 64-byte chunks since we don't want to store a
88 	 * large temporary buffer for this purpose.
89 	 */
90 	end = fb->size / sizeof(buf) * sizeof(buf);
91 
92 	for (pos = 0; pos < end; pos += sizeof(buf))
93 		dmub_memcpy(buf, base + pos, sizeof(buf));
94 
95 	/* Read anything leftover into the buffer. */
96 	if (end < fb->size)
97 		dmub_memcpy(buf, base + pos, fb->size - end);
98 }
99 
100 static const struct dmub_fw_meta_info *
101 dmub_get_fw_meta_info(const struct dmub_srv_region_params *params)
102 {
103 	const union dmub_fw_meta *meta;
104 	const uint8_t *blob = NULL;
105 	uint32_t blob_size = 0;
106 	uint32_t meta_offset = 0;
107 
108 	if (params->fw_bss_data && params->bss_data_size) {
109 		/* Legacy metadata region. */
110 		blob = params->fw_bss_data;
111 		blob_size = params->bss_data_size;
112 		meta_offset = DMUB_FW_META_OFFSET;
113 	} else if (params->fw_inst_const && params->inst_const_size) {
114 		/* Combined metadata region. */
115 		blob = params->fw_inst_const;
116 		blob_size = params->inst_const_size;
117 		meta_offset = 0;
118 	}
119 
120 	if (!blob || !blob_size)
121 		return NULL;
122 
123 	if (blob_size < sizeof(union dmub_fw_meta) + meta_offset)
124 		return NULL;
125 
126 	meta = (const union dmub_fw_meta *)(blob + blob_size - meta_offset -
127 					    sizeof(union dmub_fw_meta));
128 
129 	if (meta->info.magic_value != DMUB_FW_META_MAGIC)
130 		return NULL;
131 
132 	return &meta->info;
133 }
134 
135 static bool dmub_srv_hw_setup(struct dmub_srv *dmub, enum dmub_asic asic)
136 {
137 	struct dmub_srv_hw_funcs *funcs = &dmub->hw_funcs;
138 
139 	switch (asic) {
140 	case DMUB_ASIC_DCN20:
141 	case DMUB_ASIC_DCN21:
142 	case DMUB_ASIC_DCN30:
143 	case DMUB_ASIC_DCN301:
144 	case DMUB_ASIC_DCN302:
145 		dmub->regs = &dmub_srv_dcn20_regs;
146 
147 		funcs->reset = dmub_dcn20_reset;
148 		funcs->reset_release = dmub_dcn20_reset_release;
149 		funcs->backdoor_load = dmub_dcn20_backdoor_load;
150 		funcs->setup_windows = dmub_dcn20_setup_windows;
151 		funcs->setup_mailbox = dmub_dcn20_setup_mailbox;
152 		funcs->get_inbox1_rptr = dmub_dcn20_get_inbox1_rptr;
153 		funcs->set_inbox1_wptr = dmub_dcn20_set_inbox1_wptr;
154 		funcs->is_supported = dmub_dcn20_is_supported;
155 		funcs->is_hw_init = dmub_dcn20_is_hw_init;
156 		funcs->set_gpint = dmub_dcn20_set_gpint;
157 		funcs->is_gpint_acked = dmub_dcn20_is_gpint_acked;
158 		funcs->get_gpint_response = dmub_dcn20_get_gpint_response;
159 		funcs->get_fw_status = dmub_dcn20_get_fw_boot_status;
160 		funcs->enable_dmub_boot_options = dmub_dcn20_enable_dmub_boot_options;
161 		funcs->skip_dmub_panel_power_sequence = dmub_dcn20_skip_dmub_panel_power_sequence;
162 
163 		// Out mailbox register access functions for RN and above
164 		funcs->setup_out_mailbox = dmub_dcn20_setup_out_mailbox;
165 		funcs->get_outbox1_wptr = dmub_dcn20_get_outbox1_wptr;
166 		funcs->set_outbox1_rptr = dmub_dcn20_set_outbox1_rptr;
167 
168 		//outbox0 call stacks
169 		funcs->setup_outbox0 = dmub_dcn20_setup_outbox0;
170 		funcs->get_outbox0_wptr = dmub_dcn20_get_outbox0_wptr;
171 		funcs->set_outbox0_rptr = dmub_dcn20_set_outbox0_rptr;
172 
173 		if (asic == DMUB_ASIC_DCN21) {
174 			dmub->regs = &dmub_srv_dcn21_regs;
175 
176 			funcs->is_phy_init = dmub_dcn21_is_phy_init;
177 		}
178 		if (asic == DMUB_ASIC_DCN30) {
179 			dmub->regs = &dmub_srv_dcn30_regs;
180 
181 			funcs->backdoor_load = dmub_dcn30_backdoor_load;
182 			funcs->setup_windows = dmub_dcn30_setup_windows;
183 		}
184 		if (asic == DMUB_ASIC_DCN301) {
185 			dmub->regs = &dmub_srv_dcn301_regs;
186 
187 			funcs->backdoor_load = dmub_dcn30_backdoor_load;
188 			funcs->setup_windows = dmub_dcn30_setup_windows;
189 		}
190 		if (asic == DMUB_ASIC_DCN302) {
191 			dmub->regs = &dmub_srv_dcn302_regs;
192 
193 			funcs->backdoor_load = dmub_dcn30_backdoor_load;
194 			funcs->setup_windows = dmub_dcn30_setup_windows;
195 		}
196 		break;
197 
198 	default:
199 		return false;
200 	}
201 
202 	return true;
203 }
204 
205 enum dmub_status dmub_srv_create(struct dmub_srv *dmub,
206 				 const struct dmub_srv_create_params *params)
207 {
208 	enum dmub_status status = DMUB_STATUS_OK;
209 
210 	dmub_memset(dmub, 0, sizeof(*dmub));
211 
212 	dmub->funcs = params->funcs;
213 	dmub->user_ctx = params->user_ctx;
214 	dmub->asic = params->asic;
215 	dmub->fw_version = params->fw_version;
216 	dmub->is_virtual = params->is_virtual;
217 
218 	/* Setup asic dependent hardware funcs. */
219 	if (!dmub_srv_hw_setup(dmub, params->asic)) {
220 		status = DMUB_STATUS_INVALID;
221 		goto cleanup;
222 	}
223 
224 	/* Override (some) hardware funcs based on user params. */
225 	if (params->hw_funcs) {
226 		if (params->hw_funcs->emul_get_inbox1_rptr)
227 			dmub->hw_funcs.emul_get_inbox1_rptr =
228 				params->hw_funcs->emul_get_inbox1_rptr;
229 
230 		if (params->hw_funcs->emul_set_inbox1_wptr)
231 			dmub->hw_funcs.emul_set_inbox1_wptr =
232 				params->hw_funcs->emul_set_inbox1_wptr;
233 
234 		if (params->hw_funcs->is_supported)
235 			dmub->hw_funcs.is_supported =
236 				params->hw_funcs->is_supported;
237 	}
238 
239 	/* Sanity checks for required hw func pointers. */
240 	if (!dmub->hw_funcs.get_inbox1_rptr ||
241 	    !dmub->hw_funcs.set_inbox1_wptr) {
242 		status = DMUB_STATUS_INVALID;
243 		goto cleanup;
244 	}
245 
246 cleanup:
247 	if (status == DMUB_STATUS_OK)
248 		dmub->sw_init = true;
249 	else
250 		dmub_srv_destroy(dmub);
251 
252 	return status;
253 }
254 
255 void dmub_srv_destroy(struct dmub_srv *dmub)
256 {
257 	dmub_memset(dmub, 0, sizeof(*dmub));
258 }
259 
260 enum dmub_status
261 dmub_srv_calc_region_info(struct dmub_srv *dmub,
262 			  const struct dmub_srv_region_params *params,
263 			  struct dmub_srv_region_info *out)
264 {
265 	struct dmub_region *inst = &out->regions[DMUB_WINDOW_0_INST_CONST];
266 	struct dmub_region *stack = &out->regions[DMUB_WINDOW_1_STACK];
267 	struct dmub_region *data = &out->regions[DMUB_WINDOW_2_BSS_DATA];
268 	struct dmub_region *bios = &out->regions[DMUB_WINDOW_3_VBIOS];
269 	struct dmub_region *mail = &out->regions[DMUB_WINDOW_4_MAILBOX];
270 	struct dmub_region *trace_buff = &out->regions[DMUB_WINDOW_5_TRACEBUFF];
271 	struct dmub_region *fw_state = &out->regions[DMUB_WINDOW_6_FW_STATE];
272 	struct dmub_region *scratch_mem = &out->regions[DMUB_WINDOW_7_SCRATCH_MEM];
273 	const struct dmub_fw_meta_info *fw_info;
274 	uint32_t fw_state_size = DMUB_FW_STATE_SIZE;
275 	uint32_t trace_buffer_size = DMUB_TRACE_BUFFER_SIZE;
276 	uint32_t scratch_mem_size = DMUB_SCRATCH_MEM_SIZE;
277 
278 	if (!dmub->sw_init)
279 		return DMUB_STATUS_INVALID;
280 
281 	memset(out, 0, sizeof(*out));
282 
283 	out->num_regions = DMUB_NUM_WINDOWS;
284 
285 	inst->base = 0x0;
286 	inst->top = inst->base + params->inst_const_size;
287 
288 	data->base = dmub_align(inst->top, 256);
289 	data->top = data->base + params->bss_data_size;
290 
291 	/*
292 	 * All cache windows below should be aligned to the size
293 	 * of the DMCUB cache line, 64 bytes.
294 	 */
295 
296 	stack->base = dmub_align(data->top, 256);
297 	stack->top = stack->base + DMUB_STACK_SIZE + DMUB_CONTEXT_SIZE;
298 
299 	bios->base = dmub_align(stack->top, 256);
300 	bios->top = bios->base + params->vbios_size;
301 
302 	mail->base = dmub_align(bios->top, 256);
303 	mail->top = mail->base + DMUB_MAILBOX_SIZE;
304 
305 	fw_info = dmub_get_fw_meta_info(params);
306 
307 	if (fw_info) {
308 		fw_state_size = fw_info->fw_region_size;
309 		trace_buffer_size = fw_info->trace_buffer_size;
310 
311 		/**
312 		 * If DM didn't fill in a version, then fill it in based on
313 		 * the firmware meta now that we have it.
314 		 *
315 		 * TODO: Make it easier for driver to extract this out to
316 		 * pass during creation.
317 		 */
318 		if (dmub->fw_version == 0)
319 			dmub->fw_version = fw_info->fw_version;
320 	}
321 
322 	trace_buff->base = dmub_align(mail->top, 256);
323 	trace_buff->top = trace_buff->base + dmub_align(trace_buffer_size, 64);
324 
325 	fw_state->base = dmub_align(trace_buff->top, 256);
326 	fw_state->top = fw_state->base + dmub_align(fw_state_size, 64);
327 
328 	scratch_mem->base = dmub_align(fw_state->top, 256);
329 	scratch_mem->top = scratch_mem->base + dmub_align(scratch_mem_size, 64);
330 
331 	out->fb_size = dmub_align(scratch_mem->top, 4096);
332 
333 	return DMUB_STATUS_OK;
334 }
335 
336 enum dmub_status dmub_srv_calc_fb_info(struct dmub_srv *dmub,
337 				       const struct dmub_srv_fb_params *params,
338 				       struct dmub_srv_fb_info *out)
339 {
340 	uint8_t *cpu_base;
341 	uint64_t gpu_base;
342 	uint32_t i;
343 
344 	if (!dmub->sw_init)
345 		return DMUB_STATUS_INVALID;
346 
347 	memset(out, 0, sizeof(*out));
348 
349 	if (params->region_info->num_regions != DMUB_NUM_WINDOWS)
350 		return DMUB_STATUS_INVALID;
351 
352 	cpu_base = (uint8_t *)params->cpu_addr;
353 	gpu_base = params->gpu_addr;
354 
355 	for (i = 0; i < DMUB_NUM_WINDOWS; ++i) {
356 		const struct dmub_region *reg =
357 			&params->region_info->regions[i];
358 
359 		out->fb[i].cpu_addr = cpu_base + reg->base;
360 		out->fb[i].gpu_addr = gpu_base + reg->base;
361 		out->fb[i].size = reg->top - reg->base;
362 	}
363 
364 	out->num_fb = DMUB_NUM_WINDOWS;
365 
366 	return DMUB_STATUS_OK;
367 }
368 
369 enum dmub_status dmub_srv_has_hw_support(struct dmub_srv *dmub,
370 					 bool *is_supported)
371 {
372 	*is_supported = false;
373 
374 	if (!dmub->sw_init)
375 		return DMUB_STATUS_INVALID;
376 
377 	if (dmub->hw_funcs.is_supported)
378 		*is_supported = dmub->hw_funcs.is_supported(dmub);
379 
380 	return DMUB_STATUS_OK;
381 }
382 
383 enum dmub_status dmub_srv_is_hw_init(struct dmub_srv *dmub, bool *is_hw_init)
384 {
385 	*is_hw_init = false;
386 
387 	if (!dmub->sw_init)
388 		return DMUB_STATUS_INVALID;
389 
390 	if (!dmub->hw_init)
391 		return DMUB_STATUS_OK;
392 
393 	if (dmub->hw_funcs.is_hw_init)
394 		*is_hw_init = dmub->hw_funcs.is_hw_init(dmub);
395 
396 	return DMUB_STATUS_OK;
397 }
398 
399 enum dmub_status dmub_srv_hw_init(struct dmub_srv *dmub,
400 				  const struct dmub_srv_hw_params *params)
401 {
402 	struct dmub_fb *inst_fb = params->fb[DMUB_WINDOW_0_INST_CONST];
403 	struct dmub_fb *stack_fb = params->fb[DMUB_WINDOW_1_STACK];
404 	struct dmub_fb *data_fb = params->fb[DMUB_WINDOW_2_BSS_DATA];
405 	struct dmub_fb *bios_fb = params->fb[DMUB_WINDOW_3_VBIOS];
406 	struct dmub_fb *mail_fb = params->fb[DMUB_WINDOW_4_MAILBOX];
407 	struct dmub_fb *tracebuff_fb = params->fb[DMUB_WINDOW_5_TRACEBUFF];
408 	struct dmub_fb *fw_state_fb = params->fb[DMUB_WINDOW_6_FW_STATE];
409 	struct dmub_fb *scratch_mem_fb = params->fb[DMUB_WINDOW_7_SCRATCH_MEM];
410 
411 	struct dmub_rb_init_params rb_params, outbox0_rb_params;
412 	struct dmub_window cw0, cw1, cw2, cw3, cw4, cw5, cw6;
413 	struct dmub_region inbox1, outbox1, outbox0;
414 
415 	if (!dmub->sw_init)
416 		return DMUB_STATUS_INVALID;
417 
418 	if (!inst_fb || !stack_fb || !data_fb || !bios_fb || !mail_fb ||
419 		!tracebuff_fb || !fw_state_fb || !scratch_mem_fb) {
420 		ASSERT(0);
421 		return DMUB_STATUS_INVALID;
422 	}
423 
424 	dmub->fb_base = params->fb_base;
425 	dmub->fb_offset = params->fb_offset;
426 	dmub->psp_version = params->psp_version;
427 
428 	if (dmub->hw_funcs.reset)
429 		dmub->hw_funcs.reset(dmub);
430 
431 	cw0.offset.quad_part = inst_fb->gpu_addr;
432 	cw0.region.base = DMUB_CW0_BASE;
433 	cw0.region.top = cw0.region.base + inst_fb->size - 1;
434 
435 	cw1.offset.quad_part = stack_fb->gpu_addr;
436 	cw1.region.base = DMUB_CW1_BASE;
437 	cw1.region.top = cw1.region.base + stack_fb->size - 1;
438 
439 	if (params->load_inst_const && dmub->hw_funcs.backdoor_load) {
440 		/**
441 		 * Read back all the instruction memory so we don't hang the
442 		 * DMCUB when backdoor loading if the write from x86 hasn't been
443 		 * flushed yet. This only occurs in backdoor loading.
444 		 */
445 		dmub_flush_buffer_mem(inst_fb);
446 		dmub->hw_funcs.backdoor_load(dmub, &cw0, &cw1);
447 	}
448 
449 	cw2.offset.quad_part = data_fb->gpu_addr;
450 	cw2.region.base = DMUB_CW0_BASE + inst_fb->size;
451 	cw2.region.top = cw2.region.base + data_fb->size;
452 
453 	cw3.offset.quad_part = bios_fb->gpu_addr;
454 	cw3.region.base = DMUB_CW3_BASE;
455 	cw3.region.top = cw3.region.base + bios_fb->size;
456 
457 	cw4.offset.quad_part = mail_fb->gpu_addr;
458 	cw4.region.base = DMUB_CW4_BASE;
459 	cw4.region.top = cw4.region.base + mail_fb->size;
460 
461 	/**
462 	 * Doubled the mailbox region to accomodate inbox and outbox.
463 	 * Note: Currently, currently total mailbox size is 16KB. It is split
464 	 * equally into 8KB between inbox and outbox. If this config is
465 	 * changed, then uncached base address configuration of outbox1
466 	 * has to be updated in funcs->setup_out_mailbox.
467 	 */
468 	inbox1.base = cw4.region.base;
469 	inbox1.top = cw4.region.base + DMUB_RB_SIZE;
470 	outbox1.base = inbox1.top;
471 	outbox1.top = cw4.region.top;
472 
473 	cw5.offset.quad_part = tracebuff_fb->gpu_addr;
474 	cw5.region.base = DMUB_CW5_BASE;
475 	cw5.region.top = cw5.region.base + tracebuff_fb->size;
476 
477 	outbox0.base = DMUB_REGION5_BASE + TRACE_BUFFER_ENTRY_OFFSET;
478 	outbox0.top = outbox0.base + tracebuff_fb->size - TRACE_BUFFER_ENTRY_OFFSET;
479 
480 	cw6.offset.quad_part = fw_state_fb->gpu_addr;
481 	cw6.region.base = DMUB_CW6_BASE;
482 	cw6.region.top = cw6.region.base + fw_state_fb->size;
483 
484 	dmub->fw_state = fw_state_fb->cpu_addr;
485 
486 	dmub->scratch_mem_fb = *scratch_mem_fb;
487 
488 	if (dmub->hw_funcs.setup_windows)
489 		dmub->hw_funcs.setup_windows(dmub, &cw2, &cw3, &cw4, &cw5, &cw6);
490 
491 	if (dmub->hw_funcs.setup_outbox0)
492 		dmub->hw_funcs.setup_outbox0(dmub, &outbox0);
493 
494 	if (dmub->hw_funcs.setup_mailbox)
495 		dmub->hw_funcs.setup_mailbox(dmub, &inbox1);
496 	if (dmub->hw_funcs.setup_out_mailbox)
497 		dmub->hw_funcs.setup_out_mailbox(dmub, &outbox1);
498 
499 	dmub_memset(&rb_params, 0, sizeof(rb_params));
500 	rb_params.ctx = dmub;
501 	rb_params.base_address = mail_fb->cpu_addr;
502 	rb_params.capacity = DMUB_RB_SIZE;
503 	dmub_rb_init(&dmub->inbox1_rb, &rb_params);
504 
505 	// Initialize outbox1 ring buffer
506 	rb_params.ctx = dmub;
507 	rb_params.base_address = (void *) ((uint8_t *) (mail_fb->cpu_addr) + DMUB_RB_SIZE);
508 	rb_params.capacity = DMUB_RB_SIZE;
509 	dmub_rb_init(&dmub->outbox1_rb, &rb_params);
510 
511 	dmub_memset(&outbox0_rb_params, 0, sizeof(outbox0_rb_params));
512 	outbox0_rb_params.ctx = dmub;
513 	outbox0_rb_params.base_address = (void *)((uintptr_t)(tracebuff_fb->cpu_addr) + TRACE_BUFFER_ENTRY_OFFSET);
514 	outbox0_rb_params.capacity = tracebuff_fb->size - dmub_align(TRACE_BUFFER_ENTRY_OFFSET, 64);
515 	dmub_rb_init(&dmub->outbox0_rb, &outbox0_rb_params);
516 
517 	if (dmub->hw_funcs.reset_release)
518 		dmub->hw_funcs.reset_release(dmub);
519 
520 	dmub->hw_init = true;
521 
522 	return DMUB_STATUS_OK;
523 }
524 
525 enum dmub_status dmub_srv_hw_reset(struct dmub_srv *dmub)
526 {
527 	if (!dmub->sw_init)
528 		return DMUB_STATUS_INVALID;
529 
530 	if (dmub->hw_funcs.reset)
531 		dmub->hw_funcs.reset(dmub);
532 
533 	dmub->hw_init = false;
534 
535 	return DMUB_STATUS_OK;
536 }
537 
538 enum dmub_status dmub_srv_cmd_queue(struct dmub_srv *dmub,
539 				    const union dmub_rb_cmd *cmd)
540 {
541 	if (!dmub->hw_init)
542 		return DMUB_STATUS_INVALID;
543 
544 	if (dmub_rb_push_front(&dmub->inbox1_rb, cmd))
545 		return DMUB_STATUS_OK;
546 
547 	return DMUB_STATUS_QUEUE_FULL;
548 }
549 
550 enum dmub_status dmub_srv_cmd_execute(struct dmub_srv *dmub)
551 {
552 	if (!dmub->hw_init)
553 		return DMUB_STATUS_INVALID;
554 
555 	/**
556 	 * Read back all the queued commands to ensure that they've
557 	 * been flushed to framebuffer memory. Otherwise DMCUB might
558 	 * read back stale, fully invalid or partially invalid data.
559 	 */
560 	dmub_rb_flush_pending(&dmub->inbox1_rb);
561 
562 		dmub->hw_funcs.set_inbox1_wptr(dmub, dmub->inbox1_rb.wrpt);
563 	return DMUB_STATUS_OK;
564 }
565 
566 enum dmub_status dmub_srv_wait_for_auto_load(struct dmub_srv *dmub,
567 					     uint32_t timeout_us)
568 {
569 	uint32_t i;
570 
571 	if (!dmub->hw_init)
572 		return DMUB_STATUS_INVALID;
573 
574 	for (i = 0; i <= timeout_us; i += 100) {
575 		union dmub_fw_boot_status status = dmub->hw_funcs.get_fw_status(dmub);
576 
577 		if (status.bits.dal_fw && status.bits.mailbox_rdy)
578 			return DMUB_STATUS_OK;
579 
580 		udelay(100);
581 	}
582 
583 	return DMUB_STATUS_TIMEOUT;
584 }
585 
586 enum dmub_status dmub_srv_wait_for_phy_init(struct dmub_srv *dmub,
587 					    uint32_t timeout_us)
588 {
589 	uint32_t i = 0;
590 
591 	if (!dmub->hw_init)
592 		return DMUB_STATUS_INVALID;
593 
594 	if (!dmub->hw_funcs.is_phy_init)
595 		return DMUB_STATUS_OK;
596 
597 	for (i = 0; i <= timeout_us; i += 10) {
598 		if (dmub->hw_funcs.is_phy_init(dmub))
599 			return DMUB_STATUS_OK;
600 
601 		udelay(10);
602 	}
603 
604 	return DMUB_STATUS_TIMEOUT;
605 }
606 
607 enum dmub_status dmub_srv_wait_for_idle(struct dmub_srv *dmub,
608 					uint32_t timeout_us)
609 {
610 	uint32_t i;
611 
612 	if (!dmub->hw_init)
613 		return DMUB_STATUS_INVALID;
614 
615 	for (i = 0; i <= timeout_us; ++i) {
616 			dmub->inbox1_rb.rptr = dmub->hw_funcs.get_inbox1_rptr(dmub);
617 		if (dmub_rb_empty(&dmub->inbox1_rb))
618 			return DMUB_STATUS_OK;
619 
620 		udelay(1);
621 	}
622 
623 	return DMUB_STATUS_TIMEOUT;
624 }
625 
626 enum dmub_status
627 dmub_srv_send_gpint_command(struct dmub_srv *dmub,
628 			    enum dmub_gpint_command command_code,
629 			    uint16_t param, uint32_t timeout_us)
630 {
631 	union dmub_gpint_data_register reg;
632 	uint32_t i;
633 
634 	if (!dmub->sw_init)
635 		return DMUB_STATUS_INVALID;
636 
637 	if (!dmub->hw_funcs.set_gpint)
638 		return DMUB_STATUS_INVALID;
639 
640 	if (!dmub->hw_funcs.is_gpint_acked)
641 		return DMUB_STATUS_INVALID;
642 
643 	reg.bits.status = 1;
644 	reg.bits.command_code = command_code;
645 	reg.bits.param = param;
646 
647 	dmub->hw_funcs.set_gpint(dmub, reg);
648 
649 	for (i = 0; i < timeout_us; ++i) {
650 		udelay(1);
651 
652 		if (dmub->hw_funcs.is_gpint_acked(dmub, reg))
653 			return DMUB_STATUS_OK;
654 	}
655 
656 	return DMUB_STATUS_TIMEOUT;
657 }
658 
659 enum dmub_status dmub_srv_get_gpint_response(struct dmub_srv *dmub,
660 					     uint32_t *response)
661 {
662 	*response = 0;
663 
664 	if (!dmub->sw_init)
665 		return DMUB_STATUS_INVALID;
666 
667 	if (!dmub->hw_funcs.get_gpint_response)
668 		return DMUB_STATUS_INVALID;
669 
670 	*response = dmub->hw_funcs.get_gpint_response(dmub);
671 
672 	return DMUB_STATUS_OK;
673 }
674 
675 enum dmub_status dmub_srv_get_fw_boot_status(struct dmub_srv *dmub,
676 					     union dmub_fw_boot_status *status)
677 {
678 	status->all = 0;
679 
680 	if (!dmub->sw_init)
681 		return DMUB_STATUS_INVALID;
682 
683 	if (dmub->hw_funcs.get_fw_status)
684 		*status = dmub->hw_funcs.get_fw_status(dmub);
685 
686 	return DMUB_STATUS_OK;
687 }
688 
689 enum dmub_status dmub_srv_cmd_with_reply_data(struct dmub_srv *dmub,
690 					      union dmub_rb_cmd *cmd)
691 {
692 	enum dmub_status status = DMUB_STATUS_OK;
693 
694 	// Queue command
695 	status = dmub_srv_cmd_queue(dmub, cmd);
696 
697 	if (status != DMUB_STATUS_OK)
698 		return status;
699 
700 	// Execute command
701 	status = dmub_srv_cmd_execute(dmub);
702 
703 	if (status != DMUB_STATUS_OK)
704 		return status;
705 
706 	// Wait for DMUB to process command
707 	status = dmub_srv_wait_for_idle(dmub, 100000);
708 
709 	if (status != DMUB_STATUS_OK)
710 		return status;
711 
712 	// Copy data back from ring buffer into command
713 	dmub_rb_get_return_data(&dmub->inbox1_rb, cmd);
714 
715 	return status;
716 }
717 
718 static inline bool dmub_rb_out_trace_buffer_front(struct dmub_rb *rb,
719 				 void *entry)
720 {
721 	const uint64_t *src = (const uint64_t *)(rb->base_address) + rb->rptr / sizeof(uint64_t);
722 	uint64_t *dst = (uint64_t *)entry;
723 	uint8_t i;
724 	uint8_t loop_count;
725 
726 	if (rb->rptr == rb->wrpt)
727 		return false;
728 
729 	loop_count = sizeof(struct dmcub_trace_buf_entry) / sizeof(uint64_t);
730 	// copying data
731 	for (i = 0; i < loop_count; i++)
732 		*dst++ = *src++;
733 
734 	rb->rptr += sizeof(struct dmcub_trace_buf_entry);
735 
736 	rb->rptr %= rb->capacity;
737 
738 	return true;
739 }
740 
741 bool dmub_srv_get_outbox0_msg(struct dmub_srv *dmub, struct dmcub_trace_buf_entry *entry)
742 {
743 	dmub->outbox0_rb.wrpt = dmub->hw_funcs.get_outbox0_wptr(dmub);
744 
745 	return dmub_rb_out_trace_buffer_front(&dmub->outbox0_rb, (void *)entry);
746 }
747