On 26/06/26 18:05, Shivang Upadhyay wrote:
diff --git a/arch/powerpc/platforms/powernv/opal.c
b/arch/powerpc/platforms/powernv/opal.c
index 1946dbdc9fa1..1e9cb5271ee7 100644
--- a/arch/powerpc/platforms/powernv/opal.c
+++ b/arch/powerpc/platforms/powernv/opal.c
@@ -1125,6 +1125,36 @@ EXPORT_SYMBOL_GPL(opal_flash_read);
EXPORT_SYMBOL_GPL(opal_flash_write);
EXPORT_SYMBOL_GPL(opal_flash_erase);
EXPORT_SYMBOL_GPL(opal_prd_msg);
+
+/**
+ * opal_check_token - Check if an OPAL call token is supported
+ * @token: OPAL token number to check
+ *
+ * Returns 1 if supported, 0 if not.
+ */
+int64_t opal_check_token(uint64_t token)
+{
+ static u8 token_cache[OPAL_LAST];
+ enum {
+ SUPP_UNKNOWN = 0,
+ PRESENT,
+ ABSENT
+ };
+
+ if (token > OPAL_LAST)
+ return 0;
+
+ if (token_cache[token] == SUPP_UNKNOWN) {
Hi Shivang,
Thanks for the v2 patch. This is a great optimization and definitely
helps reduce unnecessary overhead. I have one small suggestion.
You can wrap the if (token_cache[token] == SUPP_UNKNOWN) check, as well
as the if (token > OPAL_LAST) check above it, with unlikely(), since
this function is in the hot path. Out-of-bounds tokens are very rare,
and after the cache is populated, the SUPP_UNKNOWN condition is expected
to be false in almost all cases. Wrapping these checks with unlikely()
will help the compiler optimize branch prediction.
Thanks,
~ Nikhil
+ /* Do the actual opal_call here */
+ if (opal_check_token_call(token)) {
+ token_cache[token] = PRESENT;
+ } else {
+ token_cache[token] = ABSENT;
+ }
+ }
+
+ return (token_cache[token] == PRESENT);
+}
EXPORT_SYMBOL_GPL(opal_check_token);
/* Convert a region of vmalloc memory to an opal sg list */