From c00d9744924184b394d2238284d8eadcfb0dbcaa Mon Sep 17 00:00:00 2001
From: HeYue <yue.he@intel.com>
Date: Fri, 15 Sep 2023 14:14:18 +0800
Subject: [PATCH] INTERNAL: Enable the low context priority setting

Add the mechanism in EGL and vulkan.

Tracked-On: OAM-112246
Signed-off-by: HeYue <yue.he@intel.com>
---
 src/egl/drivers/dri2/egl_dri2.c |  5 +++++
 src/intel/common/intel_check.c  | 17 +++++++++++++++++
 src/intel/common/intel_check.h  |  1 +
 src/intel/vulkan/anv_device.c   |  9 +++++++++
 4 files changed, 32 insertions(+)

diff --git a/src/egl/drivers/dri2/egl_dri2.c b/src/egl/drivers/dri2/egl_dri2.c
index f8a38635c05..c946c6f7965 100644
--- a/src/egl/drivers/dri2/egl_dri2.c
+++ b/src/egl/drivers/dri2/egl_dri2.c
@@ -73,6 +73,7 @@
 #include "util/driconf.h"
 #include "util/u_math.h"
 #include "pipe/p_screen.h"
+#include "common/intel_check.h"
 
 #define NUM_ATTRIBS 12
 
@@ -1430,6 +1431,10 @@ dri2_create_context(_EGLDisplay *disp, _EGLConfig *conf,
    else
       dri_config = NULL;
 
+   if (intel_lower_ctx_priority()) {
+      dri2_ctx->base.ContextPriority = EGL_CONTEXT_PRIORITY_LOW_IMG;
+   }
+
    if (!dri2_fill_context_attribs(dri2_ctx, dri2_dpy, ctx_attribs,
                                   &num_attribs))
       goto cleanup;
diff --git a/src/intel/common/intel_check.c b/src/intel/common/intel_check.c
index 48a52f8531c..68e49cdf4b8 100644
--- a/src/intel/common/intel_check.c
+++ b/src/intel/common/intel_check.c
@@ -76,3 +76,20 @@ bool intel_is_dgpu_render() {
    get_pid_name(process_id, process_name);
    return (use_dgpu_render(process_name) || is_target_process(process_name));
 }
+
+bool intel_lower_ctx_priority()
+{
+   pid_t process_id = getpid();
+   char process_name[BUF_SIZE];
+   get_pid_name(process_id, process_name);
+
+   char lower_pri[BUF_SIZE];
+   char vendor_buf[PROPERTY_VALUE_MAX];
+   sprintf(lower_pri, "persist.vendor.intel.lowPir.%s",process_name);
+   if (property_get(lower_pri, vendor_buf, NULL) > 0) {
+      if (vendor_buf[0] == '1') {
+         return true;
+      }
+   }
+   return false;
+}
diff --git a/src/intel/common/intel_check.h b/src/intel/common/intel_check.h
index ab2932a7967..644c06ed32c 100644
--- a/src/intel/common/intel_check.h
+++ b/src/intel/common/intel_check.h
@@ -5,6 +5,7 @@
 extern "C" {
 #endif
 bool intel_is_dgpu_render();
+bool intel_lower_ctx_priority();
 #ifdef __cplusplus
 }
 #endif
diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c
index 029fea09396..853d008ce9d 100644
--- a/src/intel/vulkan/anv_device.c
+++ b/src/intel/vulkan/anv_device.c
@@ -3245,6 +3245,15 @@ anv_device_setup_context(struct anv_device *device,
       }
    }
 
+   if (intel_lower_ctx_priority()) {
+      int ret = anv_gem_set_context_param(device->fd, device->context_id,
+                                          I915_CONTEXT_PARAM_PRIORITY,
+                                          VK_QUEUE_GLOBAL_PRIORITY_LOW_KHR);
+      if (ret != 0) {
+         goto fail_context;
+      }
+   }
+
    return result;
 
 fail_context: