xref: /linux/drivers/gpu/drm/xe/xe_force_wake.h (revision 9156bf442ee56c0f883aa4c81af9c8471eef6846)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2022 Intel Corporation
4  */
5 
6 #ifndef _XE_FORCE_WAKE_H_
7 #define _XE_FORCE_WAKE_H_
8 
9 #include "xe_assert.h"
10 #include "xe_force_wake_types.h"
11 
12 struct xe_gt;
13 
14 void xe_force_wake_init_gt(struct xe_gt *gt,
15 			   struct xe_force_wake *fw);
16 void xe_force_wake_init_engines(struct xe_gt *gt,
17 				struct xe_force_wake *fw);
18 unsigned int __must_check xe_force_wake_get(struct xe_force_wake *fw,
19 					    enum xe_force_wake_domains domains);
20 void xe_force_wake_put(struct xe_force_wake *fw, unsigned int fw_ref);
21 
22 static inline int
23 xe_force_wake_ref(struct xe_force_wake *fw,
24 		  enum xe_force_wake_domains domain)
25 {
26 	xe_gt_assert(fw->gt, domain != XE_FORCEWAKE_ALL);
27 	return fw->domains[ffs(domain) - 1].ref;
28 }
29 
30 /**
31  * xe_force_wake_assert_held - asserts domain is awake
32  * @fw : xe_force_wake structure
33  * @domain: xe_force_wake_domains apart from XE_FORCEWAKE_ALL
34  *
35  * xe_force_wake_assert_held() is designed to confirm a particular
36  * forcewake domain's wakefulness; it doesn't verify the wakefulness of
37  * multiple domains. Make sure the caller doesn't input multiple
38  * domains(XE_FORCEWAKE_ALL) as a parameter.
39  */
40 static inline void
41 xe_force_wake_assert_held(struct xe_force_wake *fw,
42 			  enum xe_force_wake_domains domain)
43 {
44 	xe_gt_assert(fw->gt, domain != XE_FORCEWAKE_ALL);
45 	xe_gt_assert(fw->gt, fw->awake_domains & domain);
46 }
47 
48 /**
49  * xe_force_wake_ref_has_domain - verifies if the domains are in fw_ref
50  * @fw_ref : the force_wake reference
51  * @domain : forcewake domain to verify
52  *
53  * This function confirms whether the @fw_ref includes a reference to the
54  * specified @domain.
55  *
56  * Return: true if domain is refcounted.
57  */
58 static inline bool
59 xe_force_wake_ref_has_domain(unsigned int fw_ref, enum xe_force_wake_domains domain)
60 {
61 	return fw_ref & domain;
62 }
63 
64 struct xe_force_wake_ref {
65 	struct xe_force_wake *fw;
66 	unsigned int domains;
67 };
68 
69 static struct xe_force_wake_ref
70 xe_force_wake_constructor(struct xe_force_wake *fw, unsigned int domains)
71 {
72 	struct xe_force_wake_ref fw_ref = { .fw = fw };
73 
74 	fw_ref.domains = xe_force_wake_get(fw, domains);
75 
76 	return fw_ref;
77 }
78 
79 DEFINE_CLASS(xe_force_wake, struct xe_force_wake_ref,
80 	     xe_force_wake_put(_T.fw, _T.domains),
81 	     xe_force_wake_constructor(fw, domains),
82 	     struct xe_force_wake *fw, unsigned int domains);
83 
84 /*
85  * Scoped helper for the forcewake class, using the same trick as scoped_guard()
86  * to bind the lifetime to the next statement/block.
87  */
88 #define __xe_with_force_wake(ref, fw, domains, done) \
89 	for (CLASS(xe_force_wake, ref)(fw, domains), *(done) = NULL; \
90 	     !(done); (done) = (void *)1)
91 
92 #define xe_with_force_wake(ref, fw, domains) \
93 	__xe_with_force_wake(ref, fw, domains, __UNIQUE_ID(done))
94 
95 /*
96  * Used when xe_force_wake_constructor() has already been called by another
97  * function and the current function is responsible for releasing the forcewake
98  * reference in all possible cases and error paths.
99  */
100 DEFINE_CLASS(xe_force_wake_release_only, struct xe_force_wake_ref,
101 	     if (_T.fw) xe_force_wake_put(_T.fw, _T.domains), fw_ref,
102 	     struct xe_force_wake_ref fw_ref);
103 
104 #endif
105