1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2022 Intel Corporation
4 */
5
6 #include "xe_pm.h"
7
8 #include <linux/pm_runtime.h>
9
10 #include <drm/drm_managed.h>
11 #include <drm/ttm/ttm_placement.h>
12
13 #include "display/xe_display.h"
14 #include "xe_bo.h"
15 #include "xe_bo_evict.h"
16 #include "xe_device.h"
17 #include "xe_device_sysfs.h"
18 #include "xe_ggtt.h"
19 #include "xe_gt.h"
20 #include "xe_guc.h"
21 #include "xe_irq.h"
22 #include "xe_pcode.h"
23 #include "xe_trace.h"
24 #include "xe_wa.h"
25
26 /**
27 * DOC: Xe Power Management
28 *
29 * Xe PM implements the main routines for both system level suspend states and
30 * for the opportunistic runtime suspend states.
31 *
32 * System Level Suspend (S-States) - In general this is OS initiated suspend
33 * driven by ACPI for achieving S0ix (a.k.a. S2idle, freeze), S3 (suspend to ram),
34 * S4 (disk). The main functions here are `xe_pm_suspend` and `xe_pm_resume`. They
35 * are the main point for the suspend to and resume from these states.
36 *
37 * PCI Device Suspend (D-States) - This is the opportunistic PCIe device low power
38 * state D3, controlled by the PCI subsystem and ACPI with the help from the
39 * runtime_pm infrastructure.
40 * PCI D3 is special and can mean D3hot, where Vcc power is on for keeping memory
41 * alive and quicker low latency resume or D3Cold where Vcc power is off for
42 * better power savings.
43 * The Vcc control of PCI hierarchy can only be controlled at the PCI root port
44 * level, while the device driver can be behind multiple bridges/switches and
45 * paired with other devices. For this reason, the PCI subsystem cannot perform
46 * the transition towards D3Cold. The lowest runtime PM possible from the PCI
47 * subsystem is D3hot. Then, if all these paired devices in the same root port
48 * are in D3hot, ACPI will assist here and run its own methods (_PR3 and _OFF)
49 * to perform the transition from D3hot to D3cold. Xe may disallow this
50 * transition by calling pci_d3cold_disable(root_pdev) before going to runtime
51 * suspend. It will be based on runtime conditions such as VRAM usage for a
52 * quick and low latency resume for instance.
53 *
54 * Runtime PM - This infrastructure provided by the Linux kernel allows the
55 * device drivers to indicate when the can be runtime suspended, so the device
56 * could be put at D3 (if supported), or allow deeper package sleep states
57 * (PC-states), and/or other low level power states. Xe PM component provides
58 * `xe_pm_runtime_suspend` and `xe_pm_runtime_resume` functions that PCI
59 * subsystem will call before transition to/from runtime suspend.
60 *
61 * Also, Xe PM provides get and put functions that Xe driver will use to
62 * indicate activity. In order to avoid locking complications with the memory
63 * management, whenever possible, these get and put functions needs to be called
64 * from the higher/outer levels.
65 * The main cases that need to be protected from the outer levels are: IOCTL,
66 * sysfs, debugfs, dma-buf sharing, GPU execution.
67 *
68 * This component is not responsible for GT idleness (RC6) nor GT frequency
69 * management (RPS).
70 */
71
72 #ifdef CONFIG_LOCKDEP
73 static struct lockdep_map xe_pm_runtime_d3cold_map = {
74 .name = "xe_rpm_d3cold_map"
75 };
76
77 static struct lockdep_map xe_pm_runtime_nod3cold_map = {
78 .name = "xe_rpm_nod3cold_map"
79 };
80 #endif
81
82 /**
83 * xe_rpm_reclaim_safe() - Whether runtime resume can be done from reclaim context
84 * @xe: The xe device.
85 *
86 * Return: true if it is safe to runtime resume from reclaim context.
87 * false otherwise.
88 */
xe_rpm_reclaim_safe(const struct xe_device * xe)89 bool xe_rpm_reclaim_safe(const struct xe_device *xe)
90 {
91 return !xe->d3cold.capable && !xe->info.has_sriov;
92 }
93
xe_rpm_lockmap_acquire(const struct xe_device * xe)94 static void xe_rpm_lockmap_acquire(const struct xe_device *xe)
95 {
96 lock_map_acquire(xe_rpm_reclaim_safe(xe) ?
97 &xe_pm_runtime_nod3cold_map :
98 &xe_pm_runtime_d3cold_map);
99 }
100
xe_rpm_lockmap_release(const struct xe_device * xe)101 static void xe_rpm_lockmap_release(const struct xe_device *xe)
102 {
103 lock_map_release(xe_rpm_reclaim_safe(xe) ?
104 &xe_pm_runtime_nod3cold_map :
105 &xe_pm_runtime_d3cold_map);
106 }
107
108 /**
109 * xe_pm_suspend - Helper for System suspend, i.e. S0->S3 / S0->S2idle
110 * @xe: xe device instance
111 *
112 * Return: 0 on success
113 */
xe_pm_suspend(struct xe_device * xe)114 int xe_pm_suspend(struct xe_device *xe)
115 {
116 struct xe_gt *gt;
117 u8 id;
118 int err;
119
120 drm_dbg(&xe->drm, "Suspending device\n");
121 trace_xe_pm_suspend(xe, __builtin_return_address(0));
122
123 for_each_gt(gt, xe, id)
124 xe_gt_suspend_prepare(gt);
125
126 xe_display_pm_suspend(xe);
127
128 /* FIXME: Super racey... */
129 err = xe_bo_evict_all(xe);
130 if (err)
131 goto err;
132
133 for_each_gt(gt, xe, id) {
134 err = xe_gt_suspend(gt);
135 if (err) {
136 xe_display_pm_resume(xe);
137 goto err;
138 }
139 }
140
141 xe_irq_suspend(xe);
142
143 xe_display_pm_suspend_late(xe);
144
145 drm_dbg(&xe->drm, "Device suspended\n");
146 return 0;
147 err:
148 drm_dbg(&xe->drm, "Device suspend failed %d\n", err);
149 return err;
150 }
151
152 /**
153 * xe_pm_resume - Helper for System resume S3->S0 / S2idle->S0
154 * @xe: xe device instance
155 *
156 * Return: 0 on success
157 */
xe_pm_resume(struct xe_device * xe)158 int xe_pm_resume(struct xe_device *xe)
159 {
160 struct xe_tile *tile;
161 struct xe_gt *gt;
162 u8 id;
163 int err;
164
165 drm_dbg(&xe->drm, "Resuming device\n");
166 trace_xe_pm_resume(xe, __builtin_return_address(0));
167
168 for_each_tile(tile, xe, id)
169 xe_wa_apply_tile_workarounds(tile);
170
171 err = xe_pcode_ready(xe, true);
172 if (err)
173 return err;
174
175 xe_display_pm_resume_early(xe);
176
177 /*
178 * This only restores pinned memory which is the memory required for the
179 * GT(s) to resume.
180 */
181 err = xe_bo_restore_kernel(xe);
182 if (err)
183 goto err;
184
185 xe_irq_resume(xe);
186
187 for_each_gt(gt, xe, id)
188 xe_gt_resume(gt);
189
190 xe_display_pm_resume(xe);
191
192 err = xe_bo_restore_user(xe);
193 if (err)
194 goto err;
195
196 drm_dbg(&xe->drm, "Device resumed\n");
197 return 0;
198 err:
199 drm_dbg(&xe->drm, "Device resume failed %d\n", err);
200 return err;
201 }
202
xe_pm_pci_d3cold_capable(struct xe_device * xe)203 static bool xe_pm_pci_d3cold_capable(struct xe_device *xe)
204 {
205 struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
206 struct pci_dev *root_pdev;
207
208 root_pdev = pcie_find_root_port(pdev);
209 if (!root_pdev)
210 return false;
211
212 /* D3Cold requires PME capability */
213 if (!pci_pme_capable(root_pdev, PCI_D3cold)) {
214 drm_dbg(&xe->drm, "d3cold: PME# not supported\n");
215 return false;
216 }
217
218 /* D3Cold requires _PR3 power resource */
219 if (!pci_pr3_present(root_pdev)) {
220 drm_dbg(&xe->drm, "d3cold: ACPI _PR3 not present\n");
221 return false;
222 }
223
224 return true;
225 }
226
xe_pm_runtime_init(struct xe_device * xe)227 static void xe_pm_runtime_init(struct xe_device *xe)
228 {
229 struct device *dev = xe->drm.dev;
230
231 /*
232 * Disable the system suspend direct complete optimization.
233 * We need to ensure that the regular device suspend/resume functions
234 * are called since our runtime_pm cannot guarantee local memory
235 * eviction for d3cold.
236 * TODO: Check HDA audio dependencies claimed by i915, and then enforce
237 * this option to integrated graphics as well.
238 */
239 if (IS_DGFX(xe))
240 dev_pm_set_driver_flags(dev, DPM_FLAG_NO_DIRECT_COMPLETE);
241
242 pm_runtime_use_autosuspend(dev);
243 pm_runtime_set_autosuspend_delay(dev, 1000);
244 pm_runtime_set_active(dev);
245 pm_runtime_allow(dev);
246 pm_runtime_mark_last_busy(dev);
247 pm_runtime_put(dev);
248 }
249
xe_pm_init_early(struct xe_device * xe)250 int xe_pm_init_early(struct xe_device *xe)
251 {
252 int err;
253
254 INIT_LIST_HEAD(&xe->mem_access.vram_userfault.list);
255
256 err = drmm_mutex_init(&xe->drm, &xe->mem_access.vram_userfault.lock);
257 if (err)
258 return err;
259
260 err = drmm_mutex_init(&xe->drm, &xe->d3cold.lock);
261 if (err)
262 return err;
263
264 return 0;
265 }
266
vram_threshold_value(struct xe_device * xe)267 static u32 vram_threshold_value(struct xe_device *xe)
268 {
269 /* FIXME: D3Cold temporarily disabled by default on BMG */
270 if (xe->info.platform == XE_BATTLEMAGE)
271 return 0;
272
273 return DEFAULT_VRAM_THRESHOLD;
274 }
275
276 /**
277 * xe_pm_init - Initialize Xe Power Management
278 * @xe: xe device instance
279 *
280 * This component is responsible for System and Device sleep states.
281 *
282 * Returns 0 for success, negative error code otherwise.
283 */
xe_pm_init(struct xe_device * xe)284 int xe_pm_init(struct xe_device *xe)
285 {
286 u32 vram_threshold;
287 int err;
288
289 /* For now suspend/resume is only allowed with GuC */
290 if (!xe_device_uc_enabled(xe))
291 return 0;
292
293 xe->d3cold.capable = xe_pm_pci_d3cold_capable(xe);
294
295 if (xe->d3cold.capable) {
296 err = xe_device_sysfs_init(xe);
297 if (err)
298 return err;
299
300 vram_threshold = vram_threshold_value(xe);
301 err = xe_pm_set_vram_threshold(xe, vram_threshold);
302 if (err)
303 return err;
304 }
305
306 xe_pm_runtime_init(xe);
307
308 return 0;
309 }
310
311 /**
312 * xe_pm_runtime_fini - Finalize Runtime PM
313 * @xe: xe device instance
314 */
xe_pm_runtime_fini(struct xe_device * xe)315 void xe_pm_runtime_fini(struct xe_device *xe)
316 {
317 struct device *dev = xe->drm.dev;
318
319 pm_runtime_get_sync(dev);
320 pm_runtime_forbid(dev);
321 }
322
xe_pm_write_callback_task(struct xe_device * xe,struct task_struct * task)323 static void xe_pm_write_callback_task(struct xe_device *xe,
324 struct task_struct *task)
325 {
326 WRITE_ONCE(xe->pm_callback_task, task);
327
328 /*
329 * Just in case it's somehow possible for our writes to be reordered to
330 * the extent that something else re-uses the task written in
331 * pm_callback_task. For example after returning from the callback, but
332 * before the reordered write that resets pm_callback_task back to NULL.
333 */
334 smp_mb(); /* pairs with xe_pm_read_callback_task */
335 }
336
xe_pm_read_callback_task(struct xe_device * xe)337 struct task_struct *xe_pm_read_callback_task(struct xe_device *xe)
338 {
339 smp_mb(); /* pairs with xe_pm_write_callback_task */
340
341 return READ_ONCE(xe->pm_callback_task);
342 }
343
344 /**
345 * xe_pm_runtime_suspended - Check if runtime_pm state is suspended
346 * @xe: xe device instance
347 *
348 * This does not provide any guarantee that the device is going to remain
349 * suspended as it might be racing with the runtime state transitions.
350 * It can be used only as a non-reliable assertion, to ensure that we are not in
351 * the sleep state while trying to access some memory for instance.
352 *
353 * Returns true if PCI device is suspended, false otherwise.
354 */
xe_pm_runtime_suspended(struct xe_device * xe)355 bool xe_pm_runtime_suspended(struct xe_device *xe)
356 {
357 return pm_runtime_suspended(xe->drm.dev);
358 }
359
360 /**
361 * xe_pm_runtime_suspend - Prepare our device for D3hot/D3Cold
362 * @xe: xe device instance
363 *
364 * Returns 0 for success, negative error code otherwise.
365 */
xe_pm_runtime_suspend(struct xe_device * xe)366 int xe_pm_runtime_suspend(struct xe_device *xe)
367 {
368 struct xe_bo *bo, *on;
369 struct xe_gt *gt;
370 u8 id;
371 int err = 0;
372
373 trace_xe_pm_runtime_suspend(xe, __builtin_return_address(0));
374 /* Disable access_ongoing asserts and prevent recursive pm calls */
375 xe_pm_write_callback_task(xe, current);
376
377 /*
378 * The actual xe_pm_runtime_put() is always async underneath, so
379 * exactly where that is called should makes no difference to us. However
380 * we still need to be very careful with the locks that this callback
381 * acquires and the locks that are acquired and held by any callers of
382 * xe_runtime_pm_get(). We already have the matching annotation
383 * on that side, but we also need it here. For example lockdep should be
384 * able to tell us if the following scenario is in theory possible:
385 *
386 * CPU0 | CPU1 (kworker)
387 * lock(A) |
388 * | xe_pm_runtime_suspend()
389 * | lock(A)
390 * xe_pm_runtime_get() |
391 *
392 * This will clearly deadlock since rpm core needs to wait for
393 * xe_pm_runtime_suspend() to complete, but here we are holding lock(A)
394 * on CPU0 which prevents CPU1 making forward progress. With the
395 * annotation here and in xe_pm_runtime_get() lockdep will see
396 * the potential lock inversion and give us a nice splat.
397 */
398 xe_rpm_lockmap_acquire(xe);
399
400 /*
401 * Applying lock for entire list op as xe_ttm_bo_destroy and xe_bo_move_notify
402 * also checks and delets bo entry from user fault list.
403 */
404 mutex_lock(&xe->mem_access.vram_userfault.lock);
405 list_for_each_entry_safe(bo, on,
406 &xe->mem_access.vram_userfault.list, vram_userfault_link)
407 xe_bo_runtime_pm_release_mmap_offset(bo);
408 mutex_unlock(&xe->mem_access.vram_userfault.lock);
409
410 xe_display_pm_runtime_suspend(xe);
411
412 if (xe->d3cold.allowed) {
413 err = xe_bo_evict_all(xe);
414 if (err)
415 goto out;
416 }
417
418 for_each_gt(gt, xe, id) {
419 err = xe_gt_suspend(gt);
420 if (err)
421 goto out;
422 }
423
424 xe_irq_suspend(xe);
425
426 if (xe->d3cold.allowed)
427 xe_display_pm_suspend_late(xe);
428 out:
429 if (err)
430 xe_display_pm_runtime_resume(xe);
431 xe_rpm_lockmap_release(xe);
432 xe_pm_write_callback_task(xe, NULL);
433 return err;
434 }
435
436 /**
437 * xe_pm_runtime_resume - Waking up from D3hot/D3Cold
438 * @xe: xe device instance
439 *
440 * Returns 0 for success, negative error code otherwise.
441 */
xe_pm_runtime_resume(struct xe_device * xe)442 int xe_pm_runtime_resume(struct xe_device *xe)
443 {
444 struct xe_gt *gt;
445 u8 id;
446 int err = 0;
447
448 trace_xe_pm_runtime_resume(xe, __builtin_return_address(0));
449 /* Disable access_ongoing asserts and prevent recursive pm calls */
450 xe_pm_write_callback_task(xe, current);
451
452 xe_rpm_lockmap_acquire(xe);
453
454 if (xe->d3cold.allowed) {
455 err = xe_pcode_ready(xe, true);
456 if (err)
457 goto out;
458
459 xe_display_pm_resume_early(xe);
460
461 /*
462 * This only restores pinned memory which is the memory
463 * required for the GT(s) to resume.
464 */
465 err = xe_bo_restore_kernel(xe);
466 if (err)
467 goto out;
468 }
469
470 xe_irq_resume(xe);
471
472 for_each_gt(gt, xe, id)
473 xe_gt_resume(gt);
474
475 xe_display_pm_runtime_resume(xe);
476
477 if (xe->d3cold.allowed) {
478 err = xe_bo_restore_user(xe);
479 if (err)
480 goto out;
481 }
482
483 out:
484 xe_rpm_lockmap_release(xe);
485 xe_pm_write_callback_task(xe, NULL);
486 return err;
487 }
488
489 /*
490 * For places where resume is synchronous it can be quite easy to deadlock
491 * if we are not careful. Also in practice it might be quite timing
492 * sensitive to ever see the 0 -> 1 transition with the callers locks
493 * held, so deadlocks might exist but are hard for lockdep to ever see.
494 * With this in mind, help lockdep learn about the potentially scary
495 * stuff that can happen inside the runtime_resume callback by acquiring
496 * a dummy lock (it doesn't protect anything and gets compiled out on
497 * non-debug builds). Lockdep then only needs to see the
498 * xe_pm_runtime_xxx_map -> runtime_resume callback once, and then can
499 * hopefully validate all the (callers_locks) -> xe_pm_runtime_xxx_map.
500 * For example if the (callers_locks) are ever grabbed in the
501 * runtime_resume callback, lockdep should give us a nice splat.
502 */
xe_rpm_might_enter_cb(const struct xe_device * xe)503 static void xe_rpm_might_enter_cb(const struct xe_device *xe)
504 {
505 xe_rpm_lockmap_acquire(xe);
506 xe_rpm_lockmap_release(xe);
507 }
508
509 /*
510 * Prime the lockdep maps for known locking orders that need to
511 * be supported but that may not always occur on all systems.
512 */
xe_pm_runtime_lockdep_prime(void)513 static void xe_pm_runtime_lockdep_prime(void)
514 {
515 struct dma_resv lockdep_resv;
516
517 dma_resv_init(&lockdep_resv);
518 lock_map_acquire(&xe_pm_runtime_d3cold_map);
519 /* D3Cold takes the dma_resv locks to evict bos */
520 dma_resv_lock(&lockdep_resv, NULL);
521 dma_resv_unlock(&lockdep_resv);
522 lock_map_release(&xe_pm_runtime_d3cold_map);
523
524 /* Shrinkers might like to wake up the device under reclaim. */
525 fs_reclaim_acquire(GFP_KERNEL);
526 lock_map_acquire(&xe_pm_runtime_nod3cold_map);
527 lock_map_release(&xe_pm_runtime_nod3cold_map);
528 fs_reclaim_release(GFP_KERNEL);
529 }
530
531 /**
532 * xe_pm_runtime_get - Get a runtime_pm reference and resume synchronously
533 * @xe: xe device instance
534 */
xe_pm_runtime_get(struct xe_device * xe)535 void xe_pm_runtime_get(struct xe_device *xe)
536 {
537 trace_xe_pm_runtime_get(xe, __builtin_return_address(0));
538 pm_runtime_get_noresume(xe->drm.dev);
539
540 if (xe_pm_read_callback_task(xe) == current)
541 return;
542
543 xe_rpm_might_enter_cb(xe);
544 pm_runtime_resume(xe->drm.dev);
545 }
546
547 /**
548 * xe_pm_runtime_put - Put the runtime_pm reference back and mark as idle
549 * @xe: xe device instance
550 */
xe_pm_runtime_put(struct xe_device * xe)551 void xe_pm_runtime_put(struct xe_device *xe)
552 {
553 trace_xe_pm_runtime_put(xe, __builtin_return_address(0));
554 if (xe_pm_read_callback_task(xe) == current) {
555 pm_runtime_put_noidle(xe->drm.dev);
556 } else {
557 pm_runtime_mark_last_busy(xe->drm.dev);
558 pm_runtime_put(xe->drm.dev);
559 }
560 }
561
562 /**
563 * xe_pm_runtime_get_ioctl - Get a runtime_pm reference before ioctl
564 * @xe: xe device instance
565 *
566 * Returns: Any number greater than or equal to 0 for success, negative error
567 * code otherwise.
568 */
xe_pm_runtime_get_ioctl(struct xe_device * xe)569 int xe_pm_runtime_get_ioctl(struct xe_device *xe)
570 {
571 trace_xe_pm_runtime_get_ioctl(xe, __builtin_return_address(0));
572 if (WARN_ON(xe_pm_read_callback_task(xe) == current))
573 return -ELOOP;
574
575 xe_rpm_might_enter_cb(xe);
576 return pm_runtime_get_sync(xe->drm.dev);
577 }
578
579 /**
580 * xe_pm_runtime_get_if_active - Get a runtime_pm reference if device active
581 * @xe: xe device instance
582 *
583 * Return: True if device is awake (regardless the previous number of references)
584 * and a new reference was taken, false otherwise.
585 */
xe_pm_runtime_get_if_active(struct xe_device * xe)586 bool xe_pm_runtime_get_if_active(struct xe_device *xe)
587 {
588 return pm_runtime_get_if_active(xe->drm.dev) > 0;
589 }
590
591 /**
592 * xe_pm_runtime_get_if_in_use - Get a new reference if device is active with previous ref taken
593 * @xe: xe device instance
594 *
595 * Return: True if device is awake, a previous reference had been already taken,
596 * and a new reference was now taken, false otherwise.
597 */
xe_pm_runtime_get_if_in_use(struct xe_device * xe)598 bool xe_pm_runtime_get_if_in_use(struct xe_device *xe)
599 {
600 if (xe_pm_read_callback_task(xe) == current) {
601 /* The device is awake, grab the ref and move on */
602 pm_runtime_get_noresume(xe->drm.dev);
603 return true;
604 }
605
606 return pm_runtime_get_if_in_use(xe->drm.dev) > 0;
607 }
608
609 /*
610 * Very unreliable! Should only be used to suppress the false positive case
611 * in the missing outer rpm protection warning.
612 */
xe_pm_suspending_or_resuming(struct xe_device * xe)613 static bool xe_pm_suspending_or_resuming(struct xe_device *xe)
614 {
615 #ifdef CONFIG_PM
616 struct device *dev = xe->drm.dev;
617
618 return dev->power.runtime_status == RPM_SUSPENDING ||
619 dev->power.runtime_status == RPM_RESUMING;
620 #else
621 return false;
622 #endif
623 }
624
625 /**
626 * xe_pm_runtime_get_noresume - Bump runtime PM usage counter without resuming
627 * @xe: xe device instance
628 *
629 * This function should be used in inner places where it is surely already
630 * protected by outer-bound callers of `xe_pm_runtime_get`.
631 * It will warn if not protected.
632 * The reference should be put back after this function regardless, since it
633 * will always bump the usage counter, regardless.
634 */
xe_pm_runtime_get_noresume(struct xe_device * xe)635 void xe_pm_runtime_get_noresume(struct xe_device *xe)
636 {
637 bool ref;
638
639 ref = xe_pm_runtime_get_if_in_use(xe);
640
641 if (!ref) {
642 pm_runtime_get_noresume(xe->drm.dev);
643 drm_WARN(&xe->drm, !xe_pm_suspending_or_resuming(xe),
644 "Missing outer runtime PM protection\n");
645 }
646 }
647
648 /**
649 * xe_pm_runtime_resume_and_get - Resume, then get a runtime_pm ref if awake.
650 * @xe: xe device instance
651 *
652 * Returns: True if device is awake and the reference was taken, false otherwise.
653 */
xe_pm_runtime_resume_and_get(struct xe_device * xe)654 bool xe_pm_runtime_resume_and_get(struct xe_device *xe)
655 {
656 if (xe_pm_read_callback_task(xe) == current) {
657 /* The device is awake, grab the ref and move on */
658 pm_runtime_get_noresume(xe->drm.dev);
659 return true;
660 }
661
662 xe_rpm_might_enter_cb(xe);
663 return pm_runtime_resume_and_get(xe->drm.dev) >= 0;
664 }
665
666 /**
667 * xe_pm_assert_unbounded_bridge - Disable PM on unbounded pcie parent bridge
668 * @xe: xe device instance
669 */
xe_pm_assert_unbounded_bridge(struct xe_device * xe)670 void xe_pm_assert_unbounded_bridge(struct xe_device *xe)
671 {
672 struct pci_dev *pdev = to_pci_dev(xe->drm.dev);
673 struct pci_dev *bridge = pci_upstream_bridge(pdev);
674
675 if (!bridge)
676 return;
677
678 if (!bridge->driver) {
679 drm_warn(&xe->drm, "unbounded parent pci bridge, device won't support any PM support.\n");
680 device_set_pm_not_required(&pdev->dev);
681 }
682 }
683
684 /**
685 * xe_pm_set_vram_threshold - Set a VRAM threshold for allowing/blocking D3Cold
686 * @xe: xe device instance
687 * @threshold: VRAM size in MiB for the D3cold threshold
688 *
689 * Return:
690 * * 0 - success
691 * * -EINVAL - invalid argument
692 */
xe_pm_set_vram_threshold(struct xe_device * xe,u32 threshold)693 int xe_pm_set_vram_threshold(struct xe_device *xe, u32 threshold)
694 {
695 struct ttm_resource_manager *man;
696 u32 vram_total_mb = 0;
697 int i;
698
699 for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
700 man = ttm_manager_type(&xe->ttm, i);
701 if (man)
702 vram_total_mb += DIV_ROUND_UP_ULL(man->size, 1024 * 1024);
703 }
704
705 drm_dbg(&xe->drm, "Total vram %u mb\n", vram_total_mb);
706
707 if (threshold > vram_total_mb)
708 return -EINVAL;
709
710 mutex_lock(&xe->d3cold.lock);
711 xe->d3cold.vram_threshold = threshold;
712 mutex_unlock(&xe->d3cold.lock);
713
714 return 0;
715 }
716
717 /**
718 * xe_pm_d3cold_allowed_toggle - Check conditions to toggle d3cold.allowed
719 * @xe: xe device instance
720 *
721 * To be called during runtime_pm idle callback.
722 * Check for all the D3Cold conditions ahead of runtime suspend.
723 */
xe_pm_d3cold_allowed_toggle(struct xe_device * xe)724 void xe_pm_d3cold_allowed_toggle(struct xe_device *xe)
725 {
726 struct ttm_resource_manager *man;
727 u32 total_vram_used_mb = 0;
728 u64 vram_used;
729 int i;
730
731 if (!xe->d3cold.capable) {
732 xe->d3cold.allowed = false;
733 return;
734 }
735
736 for (i = XE_PL_VRAM0; i <= XE_PL_VRAM1; ++i) {
737 man = ttm_manager_type(&xe->ttm, i);
738 if (man) {
739 vram_used = ttm_resource_manager_usage(man);
740 total_vram_used_mb += DIV_ROUND_UP_ULL(vram_used, 1024 * 1024);
741 }
742 }
743
744 mutex_lock(&xe->d3cold.lock);
745
746 if (total_vram_used_mb < xe->d3cold.vram_threshold)
747 xe->d3cold.allowed = true;
748 else
749 xe->d3cold.allowed = false;
750
751 mutex_unlock(&xe->d3cold.lock);
752
753 drm_dbg(&xe->drm,
754 "d3cold: allowed=%s\n", str_yes_no(xe->d3cold.allowed));
755 }
756
757 /**
758 * xe_pm_module_init() - Perform xe_pm specific module initialization.
759 *
760 * Return: 0 on success. Currently doesn't fail.
761 */
xe_pm_module_init(void)762 int __init xe_pm_module_init(void)
763 {
764 xe_pm_runtime_lockdep_prime();
765 return 0;
766 }
767