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/dmub_srv_stat.h"
27 #include "dmub/inc/dmub_cmd.h"
28 
29 /**
30  * DOC: DMUB_SRV STAT Interface
31  *
32  * These interfaces are called without acquiring DAL and DC locks.
33  * Hence, there is limitations on whese interfaces can access. Only
34  * variables exclusively defined for these interfaces can be modified.
35  */
36 
37 /**
38  *****************************************************************************
39  *  Function: dmub_srv_stat_get_notification
40  *
41  *  @brief
42  *		Retrieves a dmub outbox notification, set up dmub notification
43  *		structure with message information. Also a pending bit if queue
44  *		is having more notifications
45  *
46  *  @param [in] dmub: dmub srv structure
47  *  @param [out] pnotify: dmub notification structure to be filled up
48  *
49  *  @return
50  *     dmub_status
51  *****************************************************************************
52  */
dmub_srv_stat_get_notification(struct dmub_srv * dmub,struct dmub_notification * notify)53 enum dmub_status dmub_srv_stat_get_notification(struct dmub_srv *dmub,
54 						struct dmub_notification *notify)
55 {
56 	/**
57 	 * This function is called without dal and dc locks, so
58 	 * we shall not modify any dmub variables, only dmub->outbox1_rb
59 	 * is exempted as it is exclusively accessed by this function
60 	 */
61 	union dmub_rb_out_cmd cmd = {0};
62 
63 	if (!dmub->hw_init) {
64 		notify->type = DMUB_NOTIFICATION_NO_DATA;
65 		notify->pending_notification = false;
66 		return DMUB_STATUS_INVALID;
67 	}
68 
69 	/* Get write pointer which is updated by dmub */
70 	dmub->outbox1_rb.wrpt = dmub->hw_funcs.get_outbox1_wptr(dmub);
71 
72 	if (!dmub_rb_out_front(&dmub->outbox1_rb, &cmd)) {
73 		notify->type = DMUB_NOTIFICATION_NO_DATA;
74 		notify->pending_notification = false;
75 		return DMUB_STATUS_OK;
76 	}
77 
78 	switch (cmd.cmd_common.header.type) {
79 	case DMUB_OUT_CMD__DP_AUX_REPLY:
80 		notify->type = DMUB_NOTIFICATION_AUX_REPLY;
81 		notify->link_index = cmd.dp_aux_reply.control.instance;
82 		notify->result = cmd.dp_aux_reply.control.result;
83 		dmub_memcpy((void *)&notify->aux_reply,
84 			(void *)&cmd.dp_aux_reply.reply_data, sizeof(struct aux_reply_data));
85 		break;
86 	default:
87 		notify->type = DMUB_NOTIFICATION_NO_DATA;
88 		break;
89 	}
90 
91 	/* Pop outbox1 ringbuffer and update read pointer */
92 	dmub_rb_pop_front(&dmub->outbox1_rb);
93 	dmub->hw_funcs.set_outbox1_rptr(dmub, dmub->outbox1_rb.rptr);
94 
95 	/**
96 	 * Notify dc whether dmub has a pending outbox message,
97 	 * this is to avoid one more call to dmub_srv_stat_get_notification
98 	 */
99 	if (dmub_rb_empty(&dmub->outbox1_rb))
100 		notify->pending_notification = false;
101 	else
102 		notify->pending_notification = true;
103 
104 	return DMUB_STATUS_OK;
105 }
106