1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #ifndef _XE_GUC_CT_TYPES_H_
7 #define _XE_GUC_CT_TYPES_H_
8 
9 #include <linux/interrupt.h>
10 #include <linux/iosys-map.h>
11 #include <linux/spinlock_types.h>
12 #include <linux/wait.h>
13 #include <linux/xarray.h>
14 
15 #include "abi/guc_communication_ctb_abi.h"
16 
17 struct xe_bo;
18 
19 /**
20  * struct guc_ctb_info - GuC command transport buffer (CTB) info
21  */
22 struct guc_ctb_info {
23 	/** @size: size of CTB commands (DW) */
24 	u32 size;
25 	/** @resv_space: reserved space of CTB commands (DW) */
26 	u32 resv_space;
27 	/** @head: head of CTB commands (DW) */
28 	u32 head;
29 	/** @tail: tail of CTB commands (DW) */
30 	u32 tail;
31 	/** @space: space in CTB commands (DW) */
32 	u32 space;
33 	/** @broken: channel broken */
34 	bool broken;
35 };
36 
37 /**
38  * struct guc_ctb - GuC command transport buffer (CTB)
39  */
40 struct guc_ctb {
41 	/** @desc: dma buffer map for CTB descriptor */
42 	struct iosys_map desc;
43 	/** @cmds: dma buffer map for CTB commands */
44 	struct iosys_map cmds;
45 	/** @info: CTB info */
46 	struct guc_ctb_info info;
47 };
48 
49 /**
50  * struct guc_ctb_snapshot - GuC command transport buffer (CTB) snapshot
51  */
52 struct guc_ctb_snapshot {
53 	/** @desc: snapshot of the CTB descriptor */
54 	struct guc_ct_buffer_desc desc;
55 	/** @info: snapshot of the CTB info */
56 	struct guc_ctb_info info;
57 };
58 
59 /**
60  * struct xe_guc_ct_snapshot - GuC command transport (CT) snapshot
61  */
62 struct xe_guc_ct_snapshot {
63 	/** @ct_enabled: CT enabled info at capture time. */
64 	bool ct_enabled;
65 	/** @g2h_outstanding: G2H outstanding info at the capture time */
66 	u32 g2h_outstanding;
67 	/** @g2h: G2H CTB snapshot */
68 	struct guc_ctb_snapshot g2h;
69 	/** @h2g: H2G CTB snapshot */
70 	struct guc_ctb_snapshot h2g;
71 	/** @ctb_size: size of the snapshot of the CTB */
72 	size_t ctb_size;
73 	/** @ctb: snapshot of the entire CTB */
74 	u32 *ctb;
75 };
76 
77 /**
78  * enum xe_guc_ct_state - CT state
79  * @XE_GUC_CT_STATE_NOT_INITIALIZED: CT not initialized, messages not expected in this state
80  * @XE_GUC_CT_STATE_DISABLED: CT disabled, messages not expected in this state
81  * @XE_GUC_CT_STATE_STOPPED: CT stopped, drop messages without errors
82  * @XE_GUC_CT_STATE_ENABLED: CT enabled, messages sent / received in this state
83  */
84 enum xe_guc_ct_state {
85 	XE_GUC_CT_STATE_NOT_INITIALIZED = 0,
86 	XE_GUC_CT_STATE_DISABLED,
87 	XE_GUC_CT_STATE_STOPPED,
88 	XE_GUC_CT_STATE_ENABLED,
89 };
90 
91 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG)
92 /** struct xe_dead_ct - Information for debugging a dead CT */
93 struct xe_dead_ct {
94 	/** @lock: protects memory allocation/free operations, and @reason updates */
95 	spinlock_t lock;
96 	/** @reason: bit mask of CT_DEAD_* reason codes */
97 	unsigned int reason;
98 	/** @reported: for preventing multiple dumps per error sequence */
99 	bool reported;
100 	/** @worker: worker thread to get out of interrupt context before dumping */
101 	struct work_struct worker;
102 	/** snapshot_ct: copy of CT state and CTB content at point of error */
103 	struct xe_guc_ct_snapshot *snapshot_ct;
104 	/** snapshot_log: copy of GuC log at point of error */
105 	struct xe_guc_log_snapshot *snapshot_log;
106 };
107 #endif
108 
109 /**
110  * struct xe_guc_ct - GuC command transport (CT) layer
111  *
112  * Includes a pair of CT buffers for bi-directional communication and tracking
113  * for the H2G and G2H requests sent and received through the buffers.
114  */
115 struct xe_guc_ct {
116 	/** @bo: XE BO for CT */
117 	struct xe_bo *bo;
118 	/** @lock: protects everything in CT layer */
119 	struct mutex lock;
120 	/** @fast_lock: protects G2H channel and credits */
121 	spinlock_t fast_lock;
122 	/** @ctbs: buffers for sending and receiving commands */
123 	struct {
124 		/** @ctbs.send: Host to GuC (H2G, send) channel */
125 		struct guc_ctb h2g;
126 		/** @ctbs.recv: GuC to Host (G2H, receive) channel */
127 		struct guc_ctb g2h;
128 	} ctbs;
129 	/** @g2h_outstanding: number of outstanding G2H */
130 	u32 g2h_outstanding;
131 	/** @g2h_worker: worker to process G2H messages */
132 	struct work_struct g2h_worker;
133 	/** @safe_mode_worker: worker to check G2H messages with IRQ disabled */
134 	struct delayed_work safe_mode_worker;
135 	/** @state: CT state */
136 	enum xe_guc_ct_state state;
137 	/** @fence_seqno: G2H fence seqno - 16 bits used by CT */
138 	u32 fence_seqno;
139 	/** @fence_lookup: G2H fence lookup */
140 	struct xarray fence_lookup;
141 	/** @wq: wait queue used for reliable CT sends and freeing G2H credits */
142 	wait_queue_head_t wq;
143 	/** @g2h_fence_wq: wait queue used for G2H fencing */
144 	wait_queue_head_t g2h_fence_wq;
145 	/** @g2h_wq: used to process G2H */
146 	struct workqueue_struct *g2h_wq;
147 	/** @msg: Message buffer */
148 	u32 msg[GUC_CTB_MSG_MAX_LEN];
149 	/** @fast_msg: Message buffer */
150 	u32 fast_msg[GUC_CTB_MSG_MAX_LEN];
151 
152 #if IS_ENABLED(CONFIG_DRM_XE_DEBUG)
153 	/** @dead: information for debugging dead CTs */
154 	struct xe_dead_ct dead;
155 #endif
156 };
157 
158 #endif
159