xref: /linux/drivers/gpu/drm/xe/xe_pcode.c (revision a9a95523c84957b7863796b5d1df2f3f5dca4519)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #include "xe_pcode.h"
7 
8 #include <linux/delay.h>
9 #include <linux/errno.h>
10 
11 #include <drm/drm_managed.h>
12 
13 #include "xe_gt.h"
14 #include "xe_mmio.h"
15 #include "xe_pcode_api.h"
16 
17 /**
18  * DOC: PCODE
19  *
20  * Xe PCODE is the component responsible for interfacing with the PCODE
21  * firmware.
22  * It shall provide a very simple ABI to other Xe components, but be the
23  * single and consolidated place that will communicate with PCODE. All read
24  * and write operations to PCODE will be internal and private to this component.
25  *
26  * What's next:
27  * - PCODE hw metrics
28  * - PCODE for display operations
29  */
30 
31 static int pcode_mailbox_status(struct xe_gt *gt)
32 {
33 	u32 err;
34 	static const struct pcode_err_decode err_decode[] = {
35 		[PCODE_ILLEGAL_CMD] = {-ENXIO, "Illegal Command"},
36 		[PCODE_TIMEOUT] = {-ETIMEDOUT, "Timed out"},
37 		[PCODE_ILLEGAL_DATA] = {-EINVAL, "Illegal Data"},
38 		[PCODE_ILLEGAL_SUBCOMMAND] = {-ENXIO, "Illegal Subcommand"},
39 		[PCODE_LOCKED] = {-EBUSY, "PCODE Locked"},
40 		[PCODE_GT_RATIO_OUT_OF_RANGE] = {-EOVERFLOW,
41 			"GT ratio out of range"},
42 		[PCODE_REJECTED] = {-EACCES, "PCODE Rejected"},
43 		[PCODE_ERROR_MASK] = {-EPROTO, "Unknown"},
44 	};
45 
46 	lockdep_assert_held(&gt->pcode.lock);
47 
48 	err = xe_mmio_read32(gt, PCODE_MAILBOX) & PCODE_ERROR_MASK;
49 	if (err) {
50 		drm_err(&gt_to_xe(gt)->drm, "PCODE Mailbox failed: %d %s", err,
51 			err_decode[err].str ?: "Unknown");
52 		return err_decode[err].errno ?: -EPROTO;
53 	}
54 
55 	return 0;
56 }
57 
58 static int pcode_mailbox_rw(struct xe_gt *gt, u32 mbox, u32 *data0, u32 *data1,
59 			    unsigned int timeout_ms, bool return_data,
60 			    bool atomic)
61 {
62 	int err;
63 
64 	lockdep_assert_held(&gt->pcode.lock);
65 
66 	if ((xe_mmio_read32(gt, PCODE_MAILBOX) & PCODE_READY) != 0)
67 		return -EAGAIN;
68 
69 	xe_mmio_write32(gt, PCODE_DATA0, *data0);
70 	xe_mmio_write32(gt, PCODE_DATA1, data1 ? *data1 : 0);
71 	xe_mmio_write32(gt, PCODE_MAILBOX, PCODE_READY | mbox);
72 
73 	err = xe_mmio_wait32(gt, PCODE_MAILBOX, PCODE_READY, 0,
74 			     timeout_ms * 1000, NULL, atomic);
75 	if (err)
76 		return err;
77 
78 	if (return_data) {
79 		*data0 = xe_mmio_read32(gt, PCODE_DATA0);
80 		if (data1)
81 			*data1 = xe_mmio_read32(gt, PCODE_DATA1);
82 	}
83 
84 	return pcode_mailbox_status(gt);
85 }
86 
87 int xe_pcode_write_timeout(struct xe_gt *gt, u32 mbox, u32 data, int timeout)
88 {
89 	int err;
90 
91 	mutex_lock(&gt->pcode.lock);
92 	err = pcode_mailbox_rw(gt, mbox, &data, NULL, timeout, false, false);
93 	mutex_unlock(&gt->pcode.lock);
94 
95 	return err;
96 }
97 
98 int xe_pcode_read(struct xe_gt *gt, u32 mbox, u32 *val, u32 *val1)
99 {
100 	int err;
101 
102 	mutex_lock(&gt->pcode.lock);
103 	err = pcode_mailbox_rw(gt, mbox, val, val1, 1, true, false);
104 	mutex_unlock(&gt->pcode.lock);
105 
106 	return err;
107 }
108 
109 static int xe_pcode_try_request(struct xe_gt *gt, u32 mbox,
110 				u32 request, u32 reply_mask, u32 reply,
111 				u32 *status, bool atomic, int timeout_us)
112 {
113 	int slept, wait = 10;
114 
115 	for (slept = 0; slept < timeout_us; slept += wait) {
116 		*status = pcode_mailbox_rw(gt, mbox, &request, NULL, 1, true,
117 					   atomic);
118 		if ((*status == 0) && ((request & reply_mask) == reply))
119 			return 0;
120 
121 		if (atomic)
122 			udelay(wait);
123 		else
124 			usleep_range(wait, wait << 1);
125 		wait <<= 1;
126 	}
127 
128 	return -ETIMEDOUT;
129 }
130 
131 /**
132  * xe_pcode_request - send PCODE request until acknowledgment
133  * @gt: gt
134  * @mbox: PCODE mailbox ID the request is targeted for
135  * @request: request ID
136  * @reply_mask: mask used to check for request acknowledgment
137  * @reply: value used to check for request acknowledgment
138  * @timeout_base_ms: timeout for polling with preemption enabled
139  *
140  * Keep resending the @request to @mbox until PCODE acknowledges it, PCODE
141  * reports an error or an overall timeout of @timeout_base_ms+50 ms expires.
142  * The request is acknowledged once the PCODE reply dword equals @reply after
143  * applying @reply_mask. Polling is first attempted with preemption enabled
144  * for @timeout_base_ms and if this times out for another 50 ms with
145  * preemption disabled.
146  *
147  * Returns 0 on success, %-ETIMEDOUT in case of a timeout, <0 in case of some
148  * other error as reported by PCODE.
149  */
150 int xe_pcode_request(struct xe_gt *gt, u32 mbox, u32 request,
151 		      u32 reply_mask, u32 reply, int timeout_base_ms)
152 {
153 	u32 status;
154 	int ret;
155 
156 	mutex_lock(&gt->pcode.lock);
157 
158 	ret = xe_pcode_try_request(gt, mbox, request, reply_mask, reply, &status,
159 				   false, timeout_base_ms * 1000);
160 	if (!ret)
161 		goto out;
162 
163 	/*
164 	 * The above can time out if the number of requests was low (2 in the
165 	 * worst case) _and_ PCODE was busy for some reason even after a
166 	 * (queued) request and @timeout_base_ms delay. As a workaround retry
167 	 * the poll with preemption disabled to maximize the number of
168 	 * requests. Increase the timeout from @timeout_base_ms to 50ms to
169 	 * account for interrupts that could reduce the number of these
170 	 * requests, and for any quirks of the PCODE firmware that delays
171 	 * the request completion.
172 	 */
173 	drm_err(&gt_to_xe(gt)->drm,
174 		"PCODE timeout, retrying with preemption disabled\n");
175 	drm_WARN_ON_ONCE(&gt_to_xe(gt)->drm, timeout_base_ms > 1);
176 	preempt_disable();
177 	ret = xe_pcode_try_request(gt, mbox, request, reply_mask, reply, &status,
178 				   true, timeout_base_ms * 1000);
179 	preempt_enable();
180 
181 out:
182 	mutex_unlock(&gt->pcode.lock);
183 	return status ? status : ret;
184 }
185 /**
186  * xe_pcode_init_min_freq_table - Initialize PCODE's QOS frequency table
187  * @gt: gt instance
188  * @min_gt_freq: Minimal (RPn) GT frequency in units of 50MHz.
189  * @max_gt_freq: Maximal (RP0) GT frequency in units of 50MHz.
190  *
191  * This function initialize PCODE's QOS frequency table for a proper minimal
192  * frequency/power steering decision, depending on the current requested GT
193  * frequency. For older platforms this was a more complete table including
194  * the IA freq. However for the latest platforms this table become a simple
195  * 1-1 Ring vs GT frequency. Even though, without setting it, PCODE might
196  * not take the right decisions for some memory frequencies and affect latency.
197  *
198  * It returns 0 on success, and -ERROR number on failure, -EINVAL if max
199  * frequency is higher then the minimal, and other errors directly translated
200  * from the PCODE Error returs:
201  * - -ENXIO: "Illegal Command"
202  * - -ETIMEDOUT: "Timed out"
203  * - -EINVAL: "Illegal Data"
204  * - -ENXIO, "Illegal Subcommand"
205  * - -EBUSY: "PCODE Locked"
206  * - -EOVERFLOW, "GT ratio out of range"
207  * - -EACCES, "PCODE Rejected"
208  * - -EPROTO, "Unknown"
209  */
210 int xe_pcode_init_min_freq_table(struct xe_gt *gt, u32 min_gt_freq,
211 				 u32 max_gt_freq)
212 {
213 	int ret;
214 	u32 freq;
215 
216 	if (!gt_to_xe(gt)->info.has_llc)
217 		return 0;
218 
219 	if (max_gt_freq <= min_gt_freq)
220 		return -EINVAL;
221 
222 	mutex_lock(&gt->pcode.lock);
223 	for (freq = min_gt_freq; freq <= max_gt_freq; freq++) {
224 		u32 data = freq << PCODE_FREQ_RING_RATIO_SHIFT | freq;
225 
226 		ret = pcode_mailbox_rw(gt, PCODE_WRITE_MIN_FREQ_TABLE,
227 				       &data, NULL, 1, false, false);
228 		if (ret)
229 			goto unlock;
230 	}
231 
232 unlock:
233 	mutex_unlock(&gt->pcode.lock);
234 	return ret;
235 }
236 
237 /**
238  * xe_pcode_init - Ensure PCODE is initialized
239  * @gt: gt instance
240  *
241  * This function ensures that PCODE is properly initialized. To be called during
242  * probe and resume paths.
243  *
244  * It returns 0 on success, and -error number on failure.
245  */
246 int xe_pcode_init(struct xe_gt *gt)
247 {
248 	u32 status, request = DGFX_GET_INIT_STATUS;
249 	int timeout_us = 180000000; /* 3 min */
250 	int ret;
251 
252 	if (!IS_DGFX(gt_to_xe(gt)))
253 		return 0;
254 
255 	mutex_lock(&gt->pcode.lock);
256 	ret = xe_pcode_try_request(gt, DGFX_PCODE_STATUS, request,
257 				   DGFX_INIT_STATUS_COMPLETE,
258 				   DGFX_INIT_STATUS_COMPLETE,
259 				   &status, false, timeout_us);
260 	mutex_unlock(&gt->pcode.lock);
261 
262 	if (ret)
263 		drm_err(&gt_to_xe(gt)->drm,
264 			"PCODE initialization timedout after: 3 min\n");
265 
266 	return ret;
267 }
268 
269 /**
270  * xe_pcode_probe - Prepare xe_pcode and also ensure PCODE is initialized.
271  * @gt: gt instance
272  *
273  * This function initializes the xe_pcode component, and when needed, it ensures
274  * that PCODE has properly performed its initialization and it is really ready
275  * to go. To be called once only during probe.
276  *
277  * It returns 0 on success, and -error number on failure.
278  */
279 int xe_pcode_probe(struct xe_gt *gt)
280 {
281 	drmm_mutex_init(&gt_to_xe(gt)->drm, &gt->pcode.lock);
282 
283 	if (!IS_DGFX(gt_to_xe(gt)))
284 		return 0;
285 
286 	return xe_pcode_init(gt);
287 }
288