1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2021 Intel Corporation
4  */
5 
6 #include <drm/drm_cache.h>
7 #include <linux/string_helpers.h>
8 
9 #include "i915_drv.h"
10 #include "i915_reg.h"
11 #include "intel_guc_slpc.h"
12 #include "intel_guc_print.h"
13 #include "intel_mchbar_regs.h"
14 #include "gt/intel_gt.h"
15 #include "gt/intel_gt_regs.h"
16 #include "gt/intel_rps.h"
17 
slpc_to_guc(struct intel_guc_slpc * slpc)18 static inline struct intel_guc *slpc_to_guc(struct intel_guc_slpc *slpc)
19 {
20 	return container_of(slpc, struct intel_guc, slpc);
21 }
22 
slpc_to_gt(struct intel_guc_slpc * slpc)23 static inline struct intel_gt *slpc_to_gt(struct intel_guc_slpc *slpc)
24 {
25 	return guc_to_gt(slpc_to_guc(slpc));
26 }
27 
slpc_to_i915(struct intel_guc_slpc * slpc)28 static inline struct drm_i915_private *slpc_to_i915(struct intel_guc_slpc *slpc)
29 {
30 	return slpc_to_gt(slpc)->i915;
31 }
32 
__detect_slpc_supported(struct intel_guc * guc)33 static bool __detect_slpc_supported(struct intel_guc *guc)
34 {
35 	/* GuC SLPC is unavailable for pre-Gen12 */
36 	return guc->submission_supported &&
37 		GRAPHICS_VER(guc_to_gt(guc)->i915) >= 12;
38 }
39 
__guc_slpc_selected(struct intel_guc * guc)40 static bool __guc_slpc_selected(struct intel_guc *guc)
41 {
42 	if (!intel_guc_slpc_is_supported(guc))
43 		return false;
44 
45 	return guc->submission_selected;
46 }
47 
intel_guc_slpc_init_early(struct intel_guc_slpc * slpc)48 void intel_guc_slpc_init_early(struct intel_guc_slpc *slpc)
49 {
50 	struct intel_guc *guc = slpc_to_guc(slpc);
51 
52 	slpc->supported = __detect_slpc_supported(guc);
53 	slpc->selected = __guc_slpc_selected(guc);
54 }
55 
slpc_mem_set_param(struct slpc_shared_data * data,u32 id,u32 value)56 static void slpc_mem_set_param(struct slpc_shared_data *data,
57 			       u32 id, u32 value)
58 {
59 	GEM_BUG_ON(id >= SLPC_MAX_OVERRIDE_PARAMETERS);
60 	/*
61 	 * When the flag bit is set, corresponding value will be read
62 	 * and applied by SLPC.
63 	 */
64 	data->override_params.bits[id >> 5] |= (1 << (id % 32));
65 	data->override_params.values[id] = value;
66 }
67 
slpc_mem_set_enabled(struct slpc_shared_data * data,u8 enable_id,u8 disable_id)68 static void slpc_mem_set_enabled(struct slpc_shared_data *data,
69 				 u8 enable_id, u8 disable_id)
70 {
71 	/*
72 	 * Enabling a param involves setting the enable_id
73 	 * to 1 and disable_id to 0.
74 	 */
75 	slpc_mem_set_param(data, enable_id, 1);
76 	slpc_mem_set_param(data, disable_id, 0);
77 }
78 
slpc_mem_set_disabled(struct slpc_shared_data * data,u8 enable_id,u8 disable_id)79 static void slpc_mem_set_disabled(struct slpc_shared_data *data,
80 				  u8 enable_id, u8 disable_id)
81 {
82 	/*
83 	 * Disabling a param involves setting the enable_id
84 	 * to 0 and disable_id to 1.
85 	 */
86 	slpc_mem_set_param(data, disable_id, 1);
87 	slpc_mem_set_param(data, enable_id, 0);
88 }
89 
slpc_get_state(struct intel_guc_slpc * slpc)90 static u32 slpc_get_state(struct intel_guc_slpc *slpc)
91 {
92 	struct slpc_shared_data *data;
93 
94 	GEM_BUG_ON(!slpc->vma);
95 
96 	drm_clflush_virt_range(slpc->vaddr, sizeof(u32));
97 	data = slpc->vaddr;
98 
99 	return data->header.global_state;
100 }
101 
guc_action_slpc_set_param_nb(struct intel_guc * guc,u8 id,u32 value)102 static int guc_action_slpc_set_param_nb(struct intel_guc *guc, u8 id, u32 value)
103 {
104 	u32 request[] = {
105 		GUC_ACTION_HOST2GUC_PC_SLPC_REQUEST,
106 		SLPC_EVENT(SLPC_EVENT_PARAMETER_SET, 2),
107 		id,
108 		value,
109 	};
110 	int ret;
111 
112 	ret = intel_guc_send_nb(guc, request, ARRAY_SIZE(request), 0);
113 
114 	return ret > 0 ? -EPROTO : ret;
115 }
116 
slpc_set_param_nb(struct intel_guc_slpc * slpc,u8 id,u32 value)117 static int slpc_set_param_nb(struct intel_guc_slpc *slpc, u8 id, u32 value)
118 {
119 	struct intel_guc *guc = slpc_to_guc(slpc);
120 
121 	GEM_BUG_ON(id >= SLPC_MAX_PARAM);
122 
123 	return guc_action_slpc_set_param_nb(guc, id, value);
124 }
125 
guc_action_slpc_set_param(struct intel_guc * guc,u8 id,u32 value)126 static int guc_action_slpc_set_param(struct intel_guc *guc, u8 id, u32 value)
127 {
128 	u32 request[] = {
129 		GUC_ACTION_HOST2GUC_PC_SLPC_REQUEST,
130 		SLPC_EVENT(SLPC_EVENT_PARAMETER_SET, 2),
131 		id,
132 		value,
133 	};
134 	int ret;
135 
136 	ret = intel_guc_send(guc, request, ARRAY_SIZE(request));
137 
138 	return ret > 0 ? -EPROTO : ret;
139 }
140 
guc_action_slpc_unset_param(struct intel_guc * guc,u8 id)141 static int guc_action_slpc_unset_param(struct intel_guc *guc, u8 id)
142 {
143 	u32 request[] = {
144 		GUC_ACTION_HOST2GUC_PC_SLPC_REQUEST,
145 		SLPC_EVENT(SLPC_EVENT_PARAMETER_UNSET, 1),
146 		id,
147 	};
148 
149 	return intel_guc_send(guc, request, ARRAY_SIZE(request));
150 }
151 
slpc_is_running(struct intel_guc_slpc * slpc)152 static bool slpc_is_running(struct intel_guc_slpc *slpc)
153 {
154 	return slpc_get_state(slpc) == SLPC_GLOBAL_STATE_RUNNING;
155 }
156 
guc_action_slpc_query(struct intel_guc * guc,u32 offset)157 static int guc_action_slpc_query(struct intel_guc *guc, u32 offset)
158 {
159 	u32 request[] = {
160 		GUC_ACTION_HOST2GUC_PC_SLPC_REQUEST,
161 		SLPC_EVENT(SLPC_EVENT_QUERY_TASK_STATE, 2),
162 		offset,
163 		0,
164 	};
165 	int ret;
166 
167 	ret = intel_guc_send(guc, request, ARRAY_SIZE(request));
168 
169 	return ret > 0 ? -EPROTO : ret;
170 }
171 
slpc_query_task_state(struct intel_guc_slpc * slpc)172 static int slpc_query_task_state(struct intel_guc_slpc *slpc)
173 {
174 	struct intel_guc *guc = slpc_to_guc(slpc);
175 	u32 offset = intel_guc_ggtt_offset(guc, slpc->vma);
176 	int ret;
177 
178 	ret = guc_action_slpc_query(guc, offset);
179 	if (unlikely(ret))
180 		guc_probe_error(guc, "Failed to query task state: %pe\n", ERR_PTR(ret));
181 
182 	drm_clflush_virt_range(slpc->vaddr, SLPC_PAGE_SIZE_BYTES);
183 
184 	return ret;
185 }
186 
slpc_set_param(struct intel_guc_slpc * slpc,u8 id,u32 value)187 static int slpc_set_param(struct intel_guc_slpc *slpc, u8 id, u32 value)
188 {
189 	struct intel_guc *guc = slpc_to_guc(slpc);
190 	int ret;
191 
192 	GEM_BUG_ON(id >= SLPC_MAX_PARAM);
193 
194 	ret = guc_action_slpc_set_param(guc, id, value);
195 	if (ret)
196 		guc_probe_error(guc, "Failed to set param %d to %u: %pe\n",
197 				id, value, ERR_PTR(ret));
198 
199 	return ret;
200 }
201 
slpc_unset_param(struct intel_guc_slpc * slpc,u8 id)202 static int slpc_unset_param(struct intel_guc_slpc *slpc, u8 id)
203 {
204 	struct intel_guc *guc = slpc_to_guc(slpc);
205 
206 	GEM_BUG_ON(id >= SLPC_MAX_PARAM);
207 
208 	return guc_action_slpc_unset_param(guc, id);
209 }
210 
slpc_force_min_freq(struct intel_guc_slpc * slpc,u32 freq)211 static int slpc_force_min_freq(struct intel_guc_slpc *slpc, u32 freq)
212 {
213 	struct intel_guc *guc = slpc_to_guc(slpc);
214 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
215 	intel_wakeref_t wakeref;
216 	int ret = 0;
217 
218 	lockdep_assert_held(&slpc->lock);
219 
220 	if (!intel_guc_is_ready(guc))
221 		return -ENODEV;
222 
223 	/*
224 	 * This function is a little different as compared to
225 	 * intel_guc_slpc_set_min_freq(). Softlimit will not be updated
226 	 * here since this is used to temporarily change min freq,
227 	 * for example, during a waitboost. Caller is responsible for
228 	 * checking bounds.
229 	 */
230 
231 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
232 		/* Non-blocking request will avoid stalls */
233 		ret = slpc_set_param_nb(slpc,
234 					SLPC_PARAM_GLOBAL_MIN_GT_UNSLICE_FREQ_MHZ,
235 					freq);
236 		if (ret)
237 			guc_notice(guc, "Failed to send set_param for min freq(%d): %pe\n",
238 				   freq, ERR_PTR(ret));
239 	}
240 
241 	return ret;
242 }
243 
slpc_boost_work(struct work_struct * work)244 static void slpc_boost_work(struct work_struct *work)
245 {
246 	struct intel_guc_slpc *slpc = container_of(work, typeof(*slpc), boost_work);
247 	int err;
248 
249 	/*
250 	 * Raise min freq to boost. It's possible that
251 	 * this is greater than current max. But it will
252 	 * certainly be limited by RP0. An error setting
253 	 * the min param is not fatal.
254 	 */
255 	mutex_lock(&slpc->lock);
256 	if (atomic_read(&slpc->num_waiters)) {
257 		err = slpc_force_min_freq(slpc, slpc->boost_freq);
258 		if (!err)
259 			slpc->num_boosts++;
260 	}
261 	mutex_unlock(&slpc->lock);
262 }
263 
intel_guc_slpc_init(struct intel_guc_slpc * slpc)264 int intel_guc_slpc_init(struct intel_guc_slpc *slpc)
265 {
266 	struct intel_guc *guc = slpc_to_guc(slpc);
267 	u32 size = PAGE_ALIGN(sizeof(struct slpc_shared_data));
268 	int err;
269 
270 	GEM_BUG_ON(slpc->vma);
271 
272 	err = intel_guc_allocate_and_map_vma(guc, size, &slpc->vma, (void **)&slpc->vaddr);
273 	if (unlikely(err)) {
274 		guc_probe_error(guc, "Failed to allocate SLPC struct: %pe\n", ERR_PTR(err));
275 		return err;
276 	}
277 
278 	slpc->max_freq_softlimit = 0;
279 	slpc->min_freq_softlimit = 0;
280 	slpc->ignore_eff_freq = false;
281 	slpc->min_is_rpmax = false;
282 
283 	slpc->boost_freq = 0;
284 	atomic_set(&slpc->num_waiters, 0);
285 	slpc->num_boosts = 0;
286 	slpc->media_ratio_mode = SLPC_MEDIA_RATIO_MODE_DYNAMIC_CONTROL;
287 
288 	mutex_init(&slpc->lock);
289 	INIT_WORK(&slpc->boost_work, slpc_boost_work);
290 
291 	return err;
292 }
293 
slpc_global_state_to_string(enum slpc_global_state state)294 static const char *slpc_global_state_to_string(enum slpc_global_state state)
295 {
296 	switch (state) {
297 	case SLPC_GLOBAL_STATE_NOT_RUNNING:
298 		return "not running";
299 	case SLPC_GLOBAL_STATE_INITIALIZING:
300 		return "initializing";
301 	case SLPC_GLOBAL_STATE_RESETTING:
302 		return "resetting";
303 	case SLPC_GLOBAL_STATE_RUNNING:
304 		return "running";
305 	case SLPC_GLOBAL_STATE_SHUTTING_DOWN:
306 		return "shutting down";
307 	case SLPC_GLOBAL_STATE_ERROR:
308 		return "error";
309 	default:
310 		return "unknown";
311 	}
312 }
313 
slpc_get_state_string(struct intel_guc_slpc * slpc)314 static const char *slpc_get_state_string(struct intel_guc_slpc *slpc)
315 {
316 	return slpc_global_state_to_string(slpc_get_state(slpc));
317 }
318 
guc_action_slpc_reset(struct intel_guc * guc,u32 offset)319 static int guc_action_slpc_reset(struct intel_guc *guc, u32 offset)
320 {
321 	u32 request[] = {
322 		GUC_ACTION_HOST2GUC_PC_SLPC_REQUEST,
323 		SLPC_EVENT(SLPC_EVENT_RESET, 2),
324 		offset,
325 		0,
326 	};
327 	int ret;
328 
329 	ret = intel_guc_send(guc, request, ARRAY_SIZE(request));
330 
331 	return ret > 0 ? -EPROTO : ret;
332 }
333 
slpc_reset(struct intel_guc_slpc * slpc)334 static int slpc_reset(struct intel_guc_slpc *slpc)
335 {
336 	struct intel_guc *guc = slpc_to_guc(slpc);
337 	u32 offset = intel_guc_ggtt_offset(guc, slpc->vma);
338 	int ret;
339 
340 	ret = guc_action_slpc_reset(guc, offset);
341 
342 	if (unlikely(ret < 0)) {
343 		guc_probe_error(guc, "SLPC reset action failed: %pe\n", ERR_PTR(ret));
344 		return ret;
345 	}
346 
347 	if (!ret) {
348 		if (wait_for(slpc_is_running(slpc), SLPC_RESET_TIMEOUT_MS)) {
349 			guc_probe_error(guc, "SLPC not enabled! State = %s\n",
350 					slpc_get_state_string(slpc));
351 			return -EIO;
352 		}
353 	}
354 
355 	return 0;
356 }
357 
slpc_decode_min_freq(struct intel_guc_slpc * slpc)358 static u32 slpc_decode_min_freq(struct intel_guc_slpc *slpc)
359 {
360 	struct slpc_shared_data *data = slpc->vaddr;
361 
362 	GEM_BUG_ON(!slpc->vma);
363 
364 	return	DIV_ROUND_CLOSEST(REG_FIELD_GET(SLPC_MIN_UNSLICE_FREQ_MASK,
365 				  data->task_state_data.freq) *
366 				  GT_FREQUENCY_MULTIPLIER, GEN9_FREQ_SCALER);
367 }
368 
slpc_decode_max_freq(struct intel_guc_slpc * slpc)369 static u32 slpc_decode_max_freq(struct intel_guc_slpc *slpc)
370 {
371 	struct slpc_shared_data *data = slpc->vaddr;
372 
373 	GEM_BUG_ON(!slpc->vma);
374 
375 	return	DIV_ROUND_CLOSEST(REG_FIELD_GET(SLPC_MAX_UNSLICE_FREQ_MASK,
376 				  data->task_state_data.freq) *
377 				  GT_FREQUENCY_MULTIPLIER, GEN9_FREQ_SCALER);
378 }
379 
slpc_shared_data_reset(struct slpc_shared_data * data)380 static void slpc_shared_data_reset(struct slpc_shared_data *data)
381 {
382 	memset(data, 0, sizeof(struct slpc_shared_data));
383 
384 	data->header.size = sizeof(struct slpc_shared_data);
385 
386 	/* Enable only GTPERF task, disable others */
387 	slpc_mem_set_enabled(data, SLPC_PARAM_TASK_ENABLE_GTPERF,
388 			     SLPC_PARAM_TASK_DISABLE_GTPERF);
389 
390 	slpc_mem_set_disabled(data, SLPC_PARAM_TASK_ENABLE_BALANCER,
391 			      SLPC_PARAM_TASK_DISABLE_BALANCER);
392 
393 	slpc_mem_set_disabled(data, SLPC_PARAM_TASK_ENABLE_DCC,
394 			      SLPC_PARAM_TASK_DISABLE_DCC);
395 }
396 
397 /**
398  * intel_guc_slpc_set_max_freq() - Set max frequency limit for SLPC.
399  * @slpc: pointer to intel_guc_slpc.
400  * @val: frequency (MHz)
401  *
402  * This function will invoke GuC SLPC action to update the max frequency
403  * limit for unslice.
404  *
405  * Return: 0 on success, non-zero error code on failure.
406  */
intel_guc_slpc_set_max_freq(struct intel_guc_slpc * slpc,u32 val)407 int intel_guc_slpc_set_max_freq(struct intel_guc_slpc *slpc, u32 val)
408 {
409 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
410 	intel_wakeref_t wakeref;
411 	int ret;
412 
413 	if (val < slpc->min_freq ||
414 	    val > slpc->rp0_freq ||
415 	    val < slpc->min_freq_softlimit)
416 		return -EINVAL;
417 
418 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
419 		ret = slpc_set_param(slpc,
420 				     SLPC_PARAM_GLOBAL_MAX_GT_UNSLICE_FREQ_MHZ,
421 				     val);
422 
423 		/* Return standardized err code for sysfs calls */
424 		if (ret)
425 			ret = -EIO;
426 	}
427 
428 	if (!ret)
429 		slpc->max_freq_softlimit = val;
430 
431 	return ret;
432 }
433 
434 /**
435  * intel_guc_slpc_get_max_freq() - Get max frequency limit for SLPC.
436  * @slpc: pointer to intel_guc_slpc.
437  * @val: pointer to val which will hold max frequency (MHz)
438  *
439  * This function will invoke GuC SLPC action to read the max frequency
440  * limit for unslice.
441  *
442  * Return: 0 on success, non-zero error code on failure.
443  */
intel_guc_slpc_get_max_freq(struct intel_guc_slpc * slpc,u32 * val)444 int intel_guc_slpc_get_max_freq(struct intel_guc_slpc *slpc, u32 *val)
445 {
446 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
447 	intel_wakeref_t wakeref;
448 	int ret = 0;
449 
450 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
451 		/* Force GuC to update task data */
452 		ret = slpc_query_task_state(slpc);
453 
454 		if (!ret)
455 			*val = slpc_decode_max_freq(slpc);
456 	}
457 
458 	return ret;
459 }
460 
intel_guc_slpc_set_ignore_eff_freq(struct intel_guc_slpc * slpc,bool val)461 int intel_guc_slpc_set_ignore_eff_freq(struct intel_guc_slpc *slpc, bool val)
462 {
463 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
464 	intel_wakeref_t wakeref;
465 	int ret;
466 
467 	mutex_lock(&slpc->lock);
468 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
469 
470 	ret = slpc_set_param(slpc,
471 			     SLPC_PARAM_IGNORE_EFFICIENT_FREQUENCY,
472 			     val);
473 	if (ret) {
474 		guc_probe_error(slpc_to_guc(slpc), "Failed to set efficient freq(%d): %pe\n",
475 				val, ERR_PTR(ret));
476 	} else {
477 		slpc->ignore_eff_freq = val;
478 
479 		/* Set min to RPn when we disable efficient freq */
480 		if (val)
481 			ret = slpc_set_param(slpc,
482 					     SLPC_PARAM_GLOBAL_MIN_GT_UNSLICE_FREQ_MHZ,
483 					     slpc->min_freq);
484 	}
485 
486 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
487 	mutex_unlock(&slpc->lock);
488 	return ret;
489 }
490 
491 /**
492  * intel_guc_slpc_set_min_freq() - Set min frequency limit for SLPC.
493  * @slpc: pointer to intel_guc_slpc.
494  * @val: frequency (MHz)
495  *
496  * This function will invoke GuC SLPC action to update the min unslice
497  * frequency.
498  *
499  * Return: 0 on success, non-zero error code on failure.
500  */
intel_guc_slpc_set_min_freq(struct intel_guc_slpc * slpc,u32 val)501 int intel_guc_slpc_set_min_freq(struct intel_guc_slpc *slpc, u32 val)
502 {
503 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
504 	intel_wakeref_t wakeref;
505 	int ret;
506 
507 	if (val < slpc->min_freq ||
508 	    val > slpc->rp0_freq ||
509 	    val > slpc->max_freq_softlimit)
510 		return -EINVAL;
511 
512 	/* Need a lock now since waitboost can be modifying min as well */
513 	mutex_lock(&slpc->lock);
514 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
515 
516 	ret = slpc_set_param(slpc,
517 			     SLPC_PARAM_GLOBAL_MIN_GT_UNSLICE_FREQ_MHZ,
518 			     val);
519 
520 	if (!ret)
521 		slpc->min_freq_softlimit = val;
522 
523 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
524 	mutex_unlock(&slpc->lock);
525 
526 	/* Return standardized err code for sysfs calls */
527 	if (ret)
528 		ret = -EIO;
529 
530 	return ret;
531 }
532 
533 /**
534  * intel_guc_slpc_get_min_freq() - Get min frequency limit for SLPC.
535  * @slpc: pointer to intel_guc_slpc.
536  * @val: pointer to val which will hold min frequency (MHz)
537  *
538  * This function will invoke GuC SLPC action to read the min frequency
539  * limit for unslice.
540  *
541  * Return: 0 on success, non-zero error code on failure.
542  */
intel_guc_slpc_get_min_freq(struct intel_guc_slpc * slpc,u32 * val)543 int intel_guc_slpc_get_min_freq(struct intel_guc_slpc *slpc, u32 *val)
544 {
545 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
546 	intel_wakeref_t wakeref;
547 	int ret = 0;
548 
549 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
550 		/* Force GuC to update task data */
551 		ret = slpc_query_task_state(slpc);
552 
553 		if (!ret)
554 			*val = slpc_decode_min_freq(slpc);
555 	}
556 
557 	return ret;
558 }
559 
intel_guc_slpc_set_media_ratio_mode(struct intel_guc_slpc * slpc,u32 val)560 int intel_guc_slpc_set_media_ratio_mode(struct intel_guc_slpc *slpc, u32 val)
561 {
562 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
563 	intel_wakeref_t wakeref;
564 	int ret = 0;
565 
566 	if (!HAS_MEDIA_RATIO_MODE(i915))
567 		return -ENODEV;
568 
569 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
570 		ret = slpc_set_param(slpc,
571 				     SLPC_PARAM_MEDIA_FF_RATIO_MODE,
572 				     val);
573 	return ret;
574 }
575 
intel_guc_pm_intrmsk_enable(struct intel_gt * gt)576 void intel_guc_pm_intrmsk_enable(struct intel_gt *gt)
577 {
578 	u32 pm_intrmsk_mbz = 0;
579 
580 	/*
581 	 * Allow GuC to receive ARAT timer expiry event.
582 	 * This interrupt register is setup by RPS code
583 	 * when host based Turbo is enabled.
584 	 */
585 	pm_intrmsk_mbz |= ARAT_EXPIRED_INTRMSK;
586 
587 	intel_uncore_rmw(gt->uncore,
588 			 GEN6_PMINTRMSK, pm_intrmsk_mbz, 0);
589 }
590 
slpc_set_softlimits(struct intel_guc_slpc * slpc)591 static int slpc_set_softlimits(struct intel_guc_slpc *slpc)
592 {
593 	int ret = 0;
594 
595 	/*
596 	 * Softlimits are initially equivalent to platform limits
597 	 * unless they have deviated from defaults, in which case,
598 	 * we retain the values and set min/max accordingly.
599 	 */
600 	if (!slpc->max_freq_softlimit) {
601 		slpc->max_freq_softlimit = slpc->rp0_freq;
602 		slpc_to_gt(slpc)->defaults.max_freq = slpc->max_freq_softlimit;
603 	} else if (slpc->max_freq_softlimit != slpc->rp0_freq) {
604 		ret = intel_guc_slpc_set_max_freq(slpc,
605 						  slpc->max_freq_softlimit);
606 	}
607 
608 	if (unlikely(ret))
609 		return ret;
610 
611 	if (!slpc->min_freq_softlimit) {
612 		/* Min softlimit is initialized to RPn */
613 		slpc->min_freq_softlimit = slpc->min_freq;
614 		slpc_to_gt(slpc)->defaults.min_freq = slpc->min_freq_softlimit;
615 	} else {
616 		return intel_guc_slpc_set_min_freq(slpc,
617 						   slpc->min_freq_softlimit);
618 	}
619 
620 	return 0;
621 }
622 
is_slpc_min_freq_rpmax(struct intel_guc_slpc * slpc)623 static bool is_slpc_min_freq_rpmax(struct intel_guc_slpc *slpc)
624 {
625 	int slpc_min_freq;
626 	int ret;
627 
628 	ret = intel_guc_slpc_get_min_freq(slpc, &slpc_min_freq);
629 	if (ret) {
630 		guc_err(slpc_to_guc(slpc), "Failed to get min freq: %pe\n", ERR_PTR(ret));
631 		return false;
632 	}
633 
634 	if (slpc_min_freq == SLPC_MAX_FREQ_MHZ)
635 		return true;
636 	else
637 		return false;
638 }
639 
update_server_min_softlimit(struct intel_guc_slpc * slpc)640 static void update_server_min_softlimit(struct intel_guc_slpc *slpc)
641 {
642 	/* For server parts, SLPC min will be at RPMax.
643 	 * Use min softlimit to clamp it to RP0 instead.
644 	 */
645 	if (!slpc->min_freq_softlimit &&
646 	    is_slpc_min_freq_rpmax(slpc)) {
647 		slpc->min_is_rpmax = true;
648 		slpc->min_freq_softlimit = slpc->rp0_freq;
649 		(slpc_to_gt(slpc))->defaults.min_freq = slpc->min_freq_softlimit;
650 	}
651 }
652 
slpc_use_fused_rp0(struct intel_guc_slpc * slpc)653 static int slpc_use_fused_rp0(struct intel_guc_slpc *slpc)
654 {
655 	/* Force SLPC to used platform rp0 */
656 	return slpc_set_param(slpc,
657 			      SLPC_PARAM_GLOBAL_MAX_GT_UNSLICE_FREQ_MHZ,
658 			      slpc->rp0_freq);
659 }
660 
slpc_get_rp_values(struct intel_guc_slpc * slpc)661 static void slpc_get_rp_values(struct intel_guc_slpc *slpc)
662 {
663 	struct intel_rps *rps = &slpc_to_gt(slpc)->rps;
664 	struct intel_rps_freq_caps caps;
665 
666 	gen6_rps_get_freq_caps(rps, &caps);
667 	slpc->rp0_freq = intel_gpu_freq(rps, caps.rp0_freq);
668 	slpc->rp1_freq = intel_gpu_freq(rps, caps.rp1_freq);
669 	slpc->min_freq = intel_gpu_freq(rps, caps.min_freq);
670 
671 	if (!slpc->boost_freq)
672 		slpc->boost_freq = slpc->rp0_freq;
673 }
674 
675 /**
676  * intel_guc_slpc_override_gucrc_mode() - override GUCRC mode
677  * @slpc: pointer to intel_guc_slpc.
678  * @mode: new value of the mode.
679  *
680  * This function will override the GUCRC mode.
681  *
682  * Return: 0 on success, non-zero error code on failure.
683  */
intel_guc_slpc_override_gucrc_mode(struct intel_guc_slpc * slpc,u32 mode)684 int intel_guc_slpc_override_gucrc_mode(struct intel_guc_slpc *slpc, u32 mode)
685 {
686 	int ret;
687 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
688 	intel_wakeref_t wakeref;
689 
690 	if (mode >= SLPC_GUCRC_MODE_MAX)
691 		return -EINVAL;
692 
693 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
694 		ret = slpc_set_param(slpc, SLPC_PARAM_PWRGATE_RC_MODE, mode);
695 		if (ret)
696 			guc_err(slpc_to_guc(slpc), "Override RC mode %d failed: %pe\n",
697 				mode, ERR_PTR(ret));
698 	}
699 
700 	return ret;
701 }
702 
intel_guc_slpc_unset_gucrc_mode(struct intel_guc_slpc * slpc)703 int intel_guc_slpc_unset_gucrc_mode(struct intel_guc_slpc *slpc)
704 {
705 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
706 	intel_wakeref_t wakeref;
707 	int ret = 0;
708 
709 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
710 		ret = slpc_unset_param(slpc, SLPC_PARAM_PWRGATE_RC_MODE);
711 		if (ret)
712 			guc_err(slpc_to_guc(slpc), "Unsetting RC mode failed: %pe\n", ERR_PTR(ret));
713 	}
714 
715 	return ret;
716 }
717 
718 /*
719  * intel_guc_slpc_enable() - Start SLPC
720  * @slpc: pointer to intel_guc_slpc.
721  *
722  * SLPC is enabled by setting up the shared data structure and
723  * sending reset event to GuC SLPC. Initial data is setup in
724  * intel_guc_slpc_init. Here we send the reset event. We do
725  * not currently need a slpc_disable since this is taken care
726  * of automatically when a reset/suspend occurs and the GuC
727  * CTB is destroyed.
728  *
729  * Return: 0 on success, non-zero error code on failure.
730  */
intel_guc_slpc_enable(struct intel_guc_slpc * slpc)731 int intel_guc_slpc_enable(struct intel_guc_slpc *slpc)
732 {
733 	struct intel_guc *guc = slpc_to_guc(slpc);
734 	int ret;
735 
736 	GEM_BUG_ON(!slpc->vma);
737 
738 	slpc_shared_data_reset(slpc->vaddr);
739 
740 	ret = slpc_reset(slpc);
741 	if (unlikely(ret < 0)) {
742 		guc_probe_error(guc, "SLPC Reset event returned: %pe\n", ERR_PTR(ret));
743 		return ret;
744 	}
745 
746 	ret = slpc_query_task_state(slpc);
747 	if (unlikely(ret < 0))
748 		return ret;
749 
750 	intel_guc_pm_intrmsk_enable(slpc_to_gt(slpc));
751 
752 	slpc_get_rp_values(slpc);
753 
754 	/* Handle the case where min=max=RPmax */
755 	update_server_min_softlimit(slpc);
756 
757 	/* Set SLPC max limit to RP0 */
758 	ret = slpc_use_fused_rp0(slpc);
759 	if (unlikely(ret)) {
760 		guc_probe_error(guc, "Failed to set SLPC max to RP0: %pe\n", ERR_PTR(ret));
761 		return ret;
762 	}
763 
764 	/* Set cached value of ignore efficient freq */
765 	intel_guc_slpc_set_ignore_eff_freq(slpc, slpc->ignore_eff_freq);
766 
767 	/* Revert SLPC min/max to softlimits if necessary */
768 	ret = slpc_set_softlimits(slpc);
769 	if (unlikely(ret)) {
770 		guc_probe_error(guc, "Failed to set SLPC softlimits: %pe\n", ERR_PTR(ret));
771 		return ret;
772 	}
773 
774 	/* Set cached media freq ratio mode */
775 	intel_guc_slpc_set_media_ratio_mode(slpc, slpc->media_ratio_mode);
776 
777 	return 0;
778 }
779 
intel_guc_slpc_set_boost_freq(struct intel_guc_slpc * slpc,u32 val)780 int intel_guc_slpc_set_boost_freq(struct intel_guc_slpc *slpc, u32 val)
781 {
782 	int ret = 0;
783 
784 	if (val < slpc->min_freq || val > slpc->rp0_freq)
785 		return -EINVAL;
786 
787 	mutex_lock(&slpc->lock);
788 
789 	if (slpc->boost_freq != val) {
790 		/* Apply only if there are active waiters */
791 		if (atomic_read(&slpc->num_waiters)) {
792 			ret = slpc_force_min_freq(slpc, val);
793 			if (ret) {
794 				ret = -EIO;
795 				goto done;
796 			}
797 		}
798 
799 		slpc->boost_freq = val;
800 	}
801 
802 done:
803 	mutex_unlock(&slpc->lock);
804 	return ret;
805 }
806 
intel_guc_slpc_dec_waiters(struct intel_guc_slpc * slpc)807 void intel_guc_slpc_dec_waiters(struct intel_guc_slpc *slpc)
808 {
809 	/*
810 	 * Return min back to the softlimit.
811 	 * This is called during request retire,
812 	 * so we don't need to fail that if the
813 	 * set_param fails.
814 	 */
815 	mutex_lock(&slpc->lock);
816 	if (atomic_dec_and_test(&slpc->num_waiters))
817 		slpc_force_min_freq(slpc, slpc->min_freq_softlimit);
818 	mutex_unlock(&slpc->lock);
819 }
820 
intel_guc_slpc_print_info(struct intel_guc_slpc * slpc,struct drm_printer * p)821 int intel_guc_slpc_print_info(struct intel_guc_slpc *slpc, struct drm_printer *p)
822 {
823 	struct drm_i915_private *i915 = slpc_to_i915(slpc);
824 	struct slpc_shared_data *data = slpc->vaddr;
825 	struct slpc_task_state_data *slpc_tasks;
826 	intel_wakeref_t wakeref;
827 	int ret = 0;
828 
829 	GEM_BUG_ON(!slpc->vma);
830 
831 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
832 		ret = slpc_query_task_state(slpc);
833 
834 		if (!ret) {
835 			slpc_tasks = &data->task_state_data;
836 
837 			drm_printf(p, "\tSLPC state: %s\n", slpc_get_state_string(slpc));
838 			drm_printf(p, "\tGTPERF task active: %s\n",
839 				   str_yes_no(slpc_tasks->status & SLPC_GTPERF_TASK_ENABLED));
840 			drm_printf(p, "\tMax freq: %u MHz\n",
841 				   slpc_decode_max_freq(slpc));
842 			drm_printf(p, "\tMin freq: %u MHz\n",
843 				   slpc_decode_min_freq(slpc));
844 			drm_printf(p, "\twaitboosts: %u\n",
845 				   slpc->num_boosts);
846 			drm_printf(p, "\tBoosts outstanding: %u\n",
847 				   atomic_read(&slpc->num_waiters));
848 		}
849 	}
850 
851 	return ret;
852 }
853 
intel_guc_slpc_fini(struct intel_guc_slpc * slpc)854 void intel_guc_slpc_fini(struct intel_guc_slpc *slpc)
855 {
856 	if (!slpc->vma)
857 		return;
858 
859 	i915_vma_unpin_and_release(&slpc->vma, I915_VMA_RELEASE_MAP);
860 }
861