mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-03-22 07:27:12 +08:00
Since forcewake uses a reference counting get/put model, there are many
places where we need to be careful to drop the forcewake reference when
bailing out of a function early on an error path. Add scope-based
cleanup options that can be used in place of explicit get/put to help
prevent mistakes in this area.
Examples:
CLASS(xe_force_wake, fw_ref)(gt_to_fw(gt), XE_FW_GT);
Obtain forcewake on the XE_FW_GT domain and hold it until the
end of the current block. The wakeref will be dropped
automatically when the current scope is exited by any means
(return, break, reaching the end of the block, etc.).
xe_with_force_wake(fw_ref, gt_to_fw(ss->gt), XE_FORCEWAKE_ALL) {
...
}
Hold all forcewake domains for the following block. As with the
CLASS usage, forcewake will be dropped automatically when the
block is exited by any means.
Use of these cleanup helpers should allow us to remove some ugly
goto-based error handling and help avoid mistakes in functions with lots
of early error exits.
An 'xe_force_wake_release_only' class is also added for cases where a
forcewake reference is passed in from another function and the current
function is responsible for releasing it in every flow and error path.
v2:
- Create a separate constructor that just wraps xe_force_wake_get for
use in the class. This eliminates the need to update the signature
of xe_force_wake_get(). (Michal)
v3:
- Wrap xe_with_force_wake's 'done' marker in __UNIQUE_ID. (Gustavo)
- Add a note to xe_force_wake_get()'s kerneldoc explaining that
scope-based cleanup is preferred when possible. (Gustavo)
- Add an xe_force_wake_release_only class. (Gustavo)
v4:
- Add NULL check on fw in release_only variant. (Gustavo)
Cc: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Gustavo Sousa <gustavo.sousa@intel.com>
Reviewed-by: Gustavo Sousa <gustavo.sousa@intel.com>
Link: https://patch.msgid.link/20251118164338.3572146-30-matthew.d.roper@intel.com
Signed-off-by: Matt Roper <matthew.d.roper@intel.com>
105 lines
3.0 KiB
C
105 lines
3.0 KiB
C
/* SPDX-License-Identifier: MIT */
|
|
/*
|
|
* Copyright © 2022 Intel Corporation
|
|
*/
|
|
|
|
#ifndef _XE_FORCE_WAKE_H_
|
|
#define _XE_FORCE_WAKE_H_
|
|
|
|
#include "xe_assert.h"
|
|
#include "xe_force_wake_types.h"
|
|
|
|
struct xe_gt;
|
|
|
|
void xe_force_wake_init_gt(struct xe_gt *gt,
|
|
struct xe_force_wake *fw);
|
|
void xe_force_wake_init_engines(struct xe_gt *gt,
|
|
struct xe_force_wake *fw);
|
|
unsigned int __must_check xe_force_wake_get(struct xe_force_wake *fw,
|
|
enum xe_force_wake_domains domains);
|
|
void xe_force_wake_put(struct xe_force_wake *fw, unsigned int fw_ref);
|
|
|
|
static inline int
|
|
xe_force_wake_ref(struct xe_force_wake *fw,
|
|
enum xe_force_wake_domains domain)
|
|
{
|
|
xe_gt_assert(fw->gt, domain != XE_FORCEWAKE_ALL);
|
|
return fw->domains[ffs(domain) - 1].ref;
|
|
}
|
|
|
|
/**
|
|
* xe_force_wake_assert_held - asserts domain is awake
|
|
* @fw : xe_force_wake structure
|
|
* @domain: xe_force_wake_domains apart from XE_FORCEWAKE_ALL
|
|
*
|
|
* xe_force_wake_assert_held() is designed to confirm a particular
|
|
* forcewake domain's wakefulness; it doesn't verify the wakefulness of
|
|
* multiple domains. Make sure the caller doesn't input multiple
|
|
* domains(XE_FORCEWAKE_ALL) as a parameter.
|
|
*/
|
|
static inline void
|
|
xe_force_wake_assert_held(struct xe_force_wake *fw,
|
|
enum xe_force_wake_domains domain)
|
|
{
|
|
xe_gt_assert(fw->gt, domain != XE_FORCEWAKE_ALL);
|
|
xe_gt_assert(fw->gt, fw->awake_domains & domain);
|
|
}
|
|
|
|
/**
|
|
* xe_force_wake_ref_has_domain - verifies if the domains are in fw_ref
|
|
* @fw_ref : the force_wake reference
|
|
* @domain : forcewake domain to verify
|
|
*
|
|
* This function confirms whether the @fw_ref includes a reference to the
|
|
* specified @domain.
|
|
*
|
|
* Return: true if domain is refcounted.
|
|
*/
|
|
static inline bool
|
|
xe_force_wake_ref_has_domain(unsigned int fw_ref, enum xe_force_wake_domains domain)
|
|
{
|
|
return fw_ref & domain;
|
|
}
|
|
|
|
struct xe_force_wake_ref {
|
|
struct xe_force_wake *fw;
|
|
unsigned int domains;
|
|
};
|
|
|
|
static struct xe_force_wake_ref
|
|
xe_force_wake_constructor(struct xe_force_wake *fw, unsigned int domains)
|
|
{
|
|
struct xe_force_wake_ref fw_ref = { .fw = fw };
|
|
|
|
fw_ref.domains = xe_force_wake_get(fw, domains);
|
|
|
|
return fw_ref;
|
|
}
|
|
|
|
DEFINE_CLASS(xe_force_wake, struct xe_force_wake_ref,
|
|
xe_force_wake_put(_T.fw, _T.domains),
|
|
xe_force_wake_constructor(fw, domains),
|
|
struct xe_force_wake *fw, unsigned int domains);
|
|
|
|
/*
|
|
* Scoped helper for the forcewake class, using the same trick as scoped_guard()
|
|
* to bind the lifetime to the next statement/block.
|
|
*/
|
|
#define __xe_with_force_wake(ref, fw, domains, done) \
|
|
for (CLASS(xe_force_wake, ref)(fw, domains), *(done) = NULL; \
|
|
!(done); (done) = (void *)1)
|
|
|
|
#define xe_with_force_wake(ref, fw, domains) \
|
|
__xe_with_force_wake(ref, fw, domains, __UNIQUE_ID(done))
|
|
|
|
/*
|
|
* Used when xe_force_wake_constructor() has already been called by another
|
|
* function and the current function is responsible for releasing the forcewake
|
|
* reference in all possible cases and error paths.
|
|
*/
|
|
DEFINE_CLASS(xe_force_wake_release_only, struct xe_force_wake_ref,
|
|
if (_T.fw) xe_force_wake_put(_T.fw, _T.domains), fw_ref,
|
|
struct xe_force_wake_ref fw_ref);
|
|
|
|
#endif
|