Added a ton of new NVML stuff
[hashcat.git] / src / hashcat.c
index 1141815..76446bd 100644 (file)
@@ -76,7 +76,7 @@ double TARGET_MS_PROFILE[4]     = { 2, 12, 96, 480 };
 #define BITMAP_MAX              24
 #define GPU_TEMP_DISABLE        0
 #define GPU_TEMP_ABORT          90
-#define GPU_TEMP_RETAIN         70
+#define GPU_TEMP_RETAIN         0
 #define WORKLOAD_PROFILE        2
 #define KERNEL_ACCEL            0
 #define KERNEL_LOOPS            0
@@ -411,10 +411,8 @@ const char *USAGE_BIG[] =
   #ifdef HAVE_HWMON
   "     --gpu-temp-abort          | Num  | Abort if GPU temperature reaches X degrees celsius   | --gpu-temp-abort=100",
   "     --gpu-temp-retain         | Num  | Try to retain GPU temperature at X degrees celsius   | --gpu-temp-retain=95",
-  #ifdef HAVE_ADL
   "     --powertune-enable        |      | Enable automatic power tuning (AMD OverDrive 6 only) |",
   #endif
-  #endif
   "     --scrypt-tmto             | Num  | Manually override TMTO value for scrypt to X         | --scrypt-tmto=3",
   " -s, --skip                    | Num  | Skip X words from the start                          | -s 1000000",
   " -l, --limit                   | Num  | Limit X words from the start + skipped words         | -l 1000000",
@@ -1543,42 +1541,75 @@ void status_display ()
 
       if (device_param->skipped) continue;
 
-      #define HM_STR_BUF_SIZE 255
+      const int num_temperature = hm_get_temperature_with_device_id (device_id);
+      const int num_fanspeed    = hm_get_fanspeed_with_device_id    (device_id);
+      const int num_utilization = hm_get_utilization_with_device_id (device_id);
+      const int num_corespeed   = hm_get_corespeed_with_device_id   (device_id);
+      const int num_memoryspeed = hm_get_memoryspeed_with_device_id (device_id);
+      const int num_buslanes    = hm_get_buslanes_with_device_id    (device_id);
+      const int num_throttle    = hm_get_throttle_with_device_id    (device_id);
+
+      char output_buf[256] = { 0 };
 
-      if (data.hm_device[device_id].fan_supported == 1)
+      int output_len = 0;
+
+      if (num_temperature >= 0)
       {
-        char utilization[HM_STR_BUF_SIZE] = { 0 };
-        char temperature[HM_STR_BUF_SIZE] = { 0 };
-        char fanspeed[HM_STR_BUF_SIZE]    = { 0 };
-        char corespeed[HM_STR_BUF_SIZE]   = { 0 };
-        char memoryspeed[HM_STR_BUF_SIZE] = { 0 };
+        snprintf (output_buf + output_len, sizeof (output_buf) - output_len, " Temp:%3uc", num_temperature);
 
-        hm_device_val_to_str ((char *) utilization, HM_STR_BUF_SIZE,   "%", hm_get_utilization_with_device_id (device_id));
-        hm_device_val_to_str ((char *) temperature, HM_STR_BUF_SIZE,   "c", hm_get_temperature_with_device_id (device_id));
-        hm_device_val_to_str ((char *) corespeed,   HM_STR_BUF_SIZE, "Mhz", hm_get_corespeed_with_device_id   (device_id));
-        hm_device_val_to_str ((char *) memoryspeed, HM_STR_BUF_SIZE, "Mhz", hm_get_memoryspeed_with_device_id (device_id));
+        output_len = strlen (output_buf);
+      }
 
-        if (device_param->device_vendor_id == VENDOR_ID_AMD)
-        {
-          hm_device_val_to_str ((char *) fanspeed, HM_STR_BUF_SIZE, "%", hm_get_fanspeed_with_device_id (device_id));
-        }
-        else if (device_param->device_vendor_id == VENDOR_ID_NV)
-        {
-          hm_device_val_to_str ((char *) fanspeed, HM_STR_BUF_SIZE, "%", hm_get_fanspeed_with_device_id (device_id));
-        }
+      if (num_fanspeed >= 0)
+      {
+        snprintf (output_buf + output_len, sizeof (output_buf) - output_len, " Fan:%3u%%", num_fanspeed);
 
-        log_info ("HWMon.GPU.#%d...: %s Util, %s Temp, %s Fan, %s Core, %s Memory", device_id + 1, utilization, temperature, fanspeed, corespeed, memoryspeed);
+        output_len = strlen (output_buf);
       }
-      else
+
+      if (num_utilization >= 0)
+      {
+        snprintf (output_buf + output_len, sizeof (output_buf) - output_len, " Util:%3u%%", num_utilization);
+
+        output_len = strlen (output_buf);
+      }
+
+      if (num_corespeed >= 0)
+      {
+        snprintf (output_buf + output_len, sizeof (output_buf) - output_len, " Core:%4uMhz", num_corespeed);
+
+        output_len = strlen (output_buf);
+      }
+
+      if (num_memoryspeed >= 0)
       {
-        char utilization[HM_STR_BUF_SIZE] = { 0 };
-        char temperature[HM_STR_BUF_SIZE] = { 0 };
+        snprintf (output_buf + output_len, sizeof (output_buf) - output_len, " Mem:%4uMhz", num_memoryspeed);
+
+        output_len = strlen (output_buf);
+      }
+
+      if (num_buslanes >= 0)
+      {
+        snprintf (output_buf + output_len, sizeof (output_buf) - output_len, " Lanes:%u", num_buslanes);
+
+        output_len = strlen (output_buf);
+      }
+
+      if (num_throttle == 1)
+      {
+        snprintf (output_buf + output_len, sizeof (output_buf) - output_len, " *Throttled*");
+
+        output_len = strlen (output_buf);
+      }
 
-        hm_device_val_to_str ((char *) utilization, HM_STR_BUF_SIZE, "%", hm_get_utilization_with_device_id (device_id));
-        hm_device_val_to_str ((char *) temperature, HM_STR_BUF_SIZE, "c", hm_get_temperature_with_device_id (device_id));
+      if (output_len == 0)
+      {
+        snprintf (output_buf + output_len, sizeof (output_buf) - output_len, " N/A");
 
-        log_info ("HWMon.GPU.#%d...: %s Util, %s Temp, N/A Fan", device_id + 1, utilization, temperature);
+        output_len = strlen (output_buf);
       }
+
+      log_info ("HWMon.Dev.#%d...:%s", device_id + 1, output_buf);
     }
 
     hc_thread_mutex_unlock (mux_adl);
@@ -2104,6 +2135,7 @@ static void check_hash (hc_device_param_t *device_param, plain_t *plain)
 
       out_fp = stdout;
     }
+
     lock_file (out_fp);
   }
   else
@@ -2932,28 +2964,31 @@ static void autotune (hc_device_param_t *device_param)
 
   const u32 kernel_power_max = device_param->device_processors * device_param->kernel_threads * kernel_accel_max;
 
-  run_kernel_memset (device_param, device_param->d_pws_buf, 7, kernel_power_max * sizeof (pw_t));
-
-  if (data.attack_exec == ATTACK_EXEC_OUTSIDE_KERNEL)
+  if (data.attack_kern == ATTACK_KERN_BF)
   {
-    run_kernel_memset (device_param, device_param->d_pws_amp_buf, 7, kernel_power_max * sizeof (pw_t));
+    run_kernel_memset (device_param, device_param->d_pws_buf, 7, kernel_power_max * sizeof (pw_t));
   }
-
-  /*
-  for (u32 i = 0; i < kernel_power_max; i++)
+  else
   {
-    device_param->pws_buf[i].i[0]   = i;
-    device_param->pws_buf[i].i[1]   = 0x01234567;
-    device_param->pws_buf[i].pw_len = 7;
-  }
+    for (u32 i = 0; i < kernel_power_max; i++)
+    {
+      device_param->pws_buf[i].i[0]   = i;
+      device_param->pws_buf[i].i[1]   = 0x01234567;
+      device_param->pws_buf[i].pw_len = 7 + (i & 7);
+    }
 
-  hc_clEnqueueWriteBuffer (data.ocl, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, 0, kernel_power_max * sizeof (pw_t), device_param->pws_buf, 0, NULL, NULL);
+    if (data.kernel_rules_cnt > 1)
+    {
+      hc_clEnqueueCopyBuffer (data.ocl, device_param->command_queue, device_param->d_rules, device_param->d_rules_c, 0, 0, kernel_loops_max * sizeof (kernel_rule_t), 0, NULL, NULL);
+    }
+
+    hc_clEnqueueWriteBuffer (data.ocl, device_param->command_queue, device_param->d_pws_buf, CL_TRUE, 0, kernel_power_max * sizeof (pw_t), device_param->pws_buf, 0, NULL, NULL);
+  }
 
   if (data.attack_exec == ATTACK_EXEC_OUTSIDE_KERNEL)
   {
     run_kernel_amp (device_param, kernel_power_max);
   }
-  */
 
   #define VERIFIER_CNT 1
 
@@ -3781,7 +3816,9 @@ static void *thread_monitor (void *p)
   #ifdef HAVE_HWMON
   uint hwmon_check   = 0;
 
-  // these variables are mainly used for fan control (AMD only)
+  int slowdown_warnings = 0;
+
+  // these variables are mainly used for fan control
 
   int *fan_speed_chgd = (int *) mycalloc (data.devices_cnt, sizeof (int));
 
@@ -3790,12 +3827,10 @@ static void *thread_monitor (void *p)
   int *temp_diff_old = (int *) mycalloc (data.devices_cnt, sizeof (int));
   int *temp_diff_sum = (int *) mycalloc (data.devices_cnt, sizeof (int));
 
-  #ifdef HAVE_ADL
   int temp_threshold = 1; // degrees celcius
 
   int fan_speed_min =  15; // in percentage
   int fan_speed_max = 100;
-  #endif // HAVE_ADL
 
   time_t last_temp_check_time;
   #endif // HAVE_HWMON
@@ -3845,8 +3880,52 @@ static void *thread_monitor (void *p)
 
     if (data.devices_status != STATUS_RUNNING) continue;
 
-
     #ifdef HAVE_HWMON
+
+    if (1)
+    {
+      hc_thread_mutex_lock (mux_adl);
+
+      for (uint device_id = 0; device_id < data.devices_cnt; device_id++)
+      {
+        hc_device_param_t *device_param = &data.devices_param[device_id];
+
+        if (device_param->skipped) continue;
+
+        if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) continue;
+
+        const int temperature = hm_get_temperature_with_device_id (device_id);
+
+        const int threshold = data.hm_device[device_id].gpu_temp_threshold_slowdown;
+
+        if (temperature >= threshold)
+        {
+          if (slowdown_warnings < 3)
+          {
+            if (data.quiet == 0) clear_prompt ();
+
+            log_info ("WARNING: Drivers temperature threshold (%dc) hit on GPU #%d, expect performance to drop...", threshold, device_id + 1);
+
+            if (slowdown_warnings == 2)
+            {
+              log_info ("");
+            }
+
+            if (data.quiet == 0) fprintf (stdout, "%s", PROMPT);
+            if (data.quiet == 0) fflush (stdout);
+
+            slowdown_warnings++;
+          }
+        }
+        else
+        {
+          slowdown_warnings = 0;
+        }
+      }
+
+      hc_thread_mutex_unlock (mux_adl);
+    }
+
     if (hwmon_check == 1)
     {
       hc_thread_mutex_lock (mux_adl);
@@ -3878,12 +3957,11 @@ static void *thread_monitor (void *p)
           break;
         }
 
-        #ifdef HAVE_ADL
         const int gpu_temp_retain = data.gpu_temp_retain;
 
-        if (gpu_temp_retain) // VENDOR_ID_AMD implied
+        if (gpu_temp_retain)
         {
-          if (data.hm_device[device_id].fan_supported == 1)
+          if (data.hm_device[device_id].fan_set_supported == 1)
           {
             int temp_cur = temperature;
 
@@ -3923,7 +4001,14 @@ static void *thread_monitor (void *p)
 
                 if ((freely_change_fan_speed == 1) || (fan_speed_must_change == 1))
                 {
-                  hm_set_fanspeed_with_device_id_amd (device_id, fan_speed_new);
+                  if (device_param->device_vendor_id == VENDOR_ID_AMD)
+                  {
+                    hm_set_fanspeed_with_device_id_amd (device_id, fan_speed_new, 1);
+                  }
+                  else if (device_param->device_vendor_id == VENDOR_ID_NV)
+                  {
+
+                  }
 
                   fan_speed_chgd[device_id] = 1;
                 }
@@ -3933,7 +4018,6 @@ static void *thread_monitor (void *p)
             }
           }
         }
-        #endif // HAVE_ADL
       }
 
       hc_thread_mutex_unlock (mux_adl);
@@ -5278,7 +5362,7 @@ static uint hlfmt_detect (FILE *fp, uint max_check)
 
 // wrapper around mymalloc for ADL
 
-#if defined(HAVE_HWMON) && defined(HAVE_ADL)
+#if defined(HAVE_HWMON)
 void *__stdcall ADL_Main_Memory_Alloc (const int iSize)
 {
   return mymalloc (iSize);
@@ -5338,7 +5422,7 @@ static uint generate_bitmaps (const uint digests_cnt, const uint dgst_size, cons
  */
 
 #ifdef _WIN
-void SetConsoleWindowSize (const int x, const int y)
+void SetConsoleWindowSize (const int x)
 {
   HANDLE h = GetStdHandle (STD_OUTPUT_HANDLE);
 
@@ -5350,18 +5434,13 @@ void SetConsoleWindowSize (const int x, const int y)
 
   SMALL_RECT *sr = &bufferInfo.srWindow;
 
-  sr->Left   = 0;
-  sr->Top    = 0;
-  sr->Right  = MAX (sr->Right,  x - 1);
-  sr->Bottom = MAX (sr->Bottom, y - 1);
+  sr->Right = MAX (sr->Right, x - 1);
 
   COORD co;
 
   co.X = sr->Right  + 1;
   co.Y = sr->Bottom + 1;
 
-  co.Y = MAX (co.Y, 1337);
-
   if (!SetConsoleScreenBufferSize (h, co)) return;
 
   if (!SetConsoleWindowInfo (h, TRUE, sr)) return;
@@ -5371,7 +5450,7 @@ void SetConsoleWindowSize (const int x, const int y)
 int main (int argc, char **argv)
 {
   #ifdef _WIN
-  SetConsoleWindowSize (132, 44);
+  SetConsoleWindowSize (132);
   #endif
 
   /**
@@ -5502,10 +5581,8 @@ int main (int argc, char **argv)
   #ifdef HAVE_HWMON
   uint  gpu_temp_abort            = GPU_TEMP_ABORT;
   uint  gpu_temp_retain           = GPU_TEMP_RETAIN;
-  #ifdef HAVE_ADL
   uint  powertune_enable          = POWERTUNE_ENABLE;
   #endif
-  #endif
   uint  logfile_disable           = LOGFILE_DISABLE;
   uint  segment_size              = SEGMENT_SIZE;
   uint  scrypt_tmto               = SCRYPT_TMTO;
@@ -5659,9 +5736,7 @@ int main (int argc, char **argv)
     #ifdef HAVE_HWMON
     {"gpu-temp-abort",            required_argument, 0, IDX_GPU_TEMP_ABORT},
     {"gpu-temp-retain",           required_argument, 0, IDX_GPU_TEMP_RETAIN},
-    #ifdef HAVE_ADL
     {"powertune-enable",          no_argument,       0, IDX_POWERTUNE_ENABLE},
-    #endif
     #endif // HAVE_HWMON
     {"logfile-disable",           no_argument,       0, IDX_LOGFILE_DISABLE},
     {"truecrypt-keyfiles",        required_argument, 0, IDX_TRUECRYPT_KEYFILES},
@@ -5885,11 +5960,6 @@ int main (int argc, char **argv)
   uint workload_profile_chgd    = 0;
   uint opencl_vector_width_chgd = 0;
 
-  #if defined(HAVE_HWMON) && defined(HAVE_ADL)
-  uint gpu_temp_retain_chgd   = 0;
-  uint gpu_temp_abort_chgd    = 0;
-  #endif
-
   optind = 1;
   optopt = 0;
   option_index = 0;
@@ -5970,19 +6040,9 @@ int main (int argc, char **argv)
                                           kernel_loops_chgd         = 1;              break;
       case IDX_GPU_TEMP_DISABLE:          gpu_temp_disable          = 1;              break;
       #ifdef HAVE_HWMON
-      case IDX_GPU_TEMP_ABORT:            gpu_temp_abort            = atoi (optarg);
-      #ifdef HAVE_ADL
-                                          gpu_temp_abort_chgd       = 1;
-      #endif
-                                                                                      break;
-      case IDX_GPU_TEMP_RETAIN:           gpu_temp_retain           = atoi (optarg);
-      #ifdef HAVE_ADL
-                                          gpu_temp_retain_chgd      = 1;
-      #endif
-                                                                                      break;
-      #ifdef HAVE_ADL
+      case IDX_GPU_TEMP_ABORT:            gpu_temp_abort            = atoi (optarg);  break;
+      case IDX_GPU_TEMP_RETAIN:           gpu_temp_retain           = atoi (optarg);  break;
       case IDX_POWERTUNE_ENABLE:          powertune_enable          = 1;              break;
-      #endif
       #endif // HAVE_HWMON
       case IDX_LOGFILE_DISABLE:           logfile_disable           = 1;              break;
       case IDX_TRUECRYPT_KEYFILES:        truecrypt_keyfiles        = optarg;         break;
@@ -6687,10 +6747,8 @@ int main (int argc, char **argv)
   data.skip                    = skip;
   data.limit                   = limit;
   #ifdef HAVE_HWMON
-  #ifdef HAVE_ADL
   data.powertune_enable        = powertune_enable;
   #endif
-  #endif
   data.logfile_disable         = logfile_disable;
   data.truecrypt_keyfiles      = truecrypt_keyfiles;
   data.veracrypt_keyfiles      = veracrypt_keyfiles;
@@ -6792,7 +6850,7 @@ int main (int argc, char **argv)
   logfile_top_uint   (outfile_format);
   logfile_top_uint   (potfile_disable);
   logfile_top_string (potfile_path);
-  #if defined(HAVE_HWMON) && defined(HAVE_ADL)
+  #if defined(HAVE_HWMON)
   logfile_top_uint   (powertune_enable);
   #endif
   logfile_top_uint   (scrypt_tmto);
@@ -6888,7 +6946,10 @@ int main (int argc, char **argv)
     potfile_disable       = 1;
     weak_hash_threshold   = 0;
     gpu_temp_disable      = 1;
+
+    #ifdef HAVE_HWMON
     powertune_enable      = 1;
+    #endif
 
     data.status_timer     = status_timer;
     data.restore_timer    = restore_timer;
@@ -13220,32 +13281,6 @@ int main (int argc, char **argv)
       }
     }
 
-    /**
-     * OpenCL platforms: For each platform check if we need to unset features that we can not use, eg: temp_retain
-     */
-
-    for (uint platform_id = 0; platform_id < platforms_cnt; platform_id++)
-    {
-      cl_platform_id platform = platforms[platform_id];
-
-      char platform_vendor[INFOSZ] = { 0 };
-
-      hc_clGetPlatformInfo (data.ocl, platform, CL_PLATFORM_VENDOR, sizeof (platform_vendor), platform_vendor, NULL);
-
-      #ifdef HAVE_HWMON
-      #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
-      if (strcmp (platform_vendor, CL_VENDOR_NV) == 0)
-      {
-        // make sure that we do not directly control the fan for NVidia
-
-        gpu_temp_retain = 0;
-
-        data.gpu_temp_retain = gpu_temp_retain;
-      }
-      #endif // HAVE_NVML || HAVE_NVAPI
-      #endif
-    }
-
     /**
      * OpenCL device types:
      *   In case the user did not specify --opencl-device-types and the user runs hashcat in a system with only a CPU only he probably want to use that CPU.
@@ -13741,12 +13776,11 @@ int main (int argc, char **argv)
           {
             if (device_param->skipped == 0)
             {
-              log_info ("Device #%u: %s, %lu/%lu MB allocatable, %dMhz, %uMCU",
+              log_info ("Device #%u: %s, %lu/%lu MB allocatable, %uMCU",
                         device_id + 1,
                         device_name,
                         (unsigned int) (device_maxmem_alloc / 1024 / 1024),
                         (unsigned int) (device_global_mem   / 1024 / 1024),
-                        (unsigned int) (device_maxclock_frequency),
                         (unsigned int)  device_processors);
             }
             else
@@ -13946,50 +13980,11 @@ int main (int argc, char **argv)
      */
 
     #ifdef HAVE_HWMON
-    #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
-    hm_attrs_t hm_adapters_nv[DEVICES_MAX]  = { { { 0 }, 0, 0 } };
-    #endif
-
-    #ifdef HAVE_ADL
-    hm_attrs_t hm_adapters_amd[DEVICES_MAX] = { { { 0 }, 0, 0 } };
-    #endif
+    hm_attrs_t hm_adapters_nv[DEVICES_MAX]  = { { { 0 }, 0, 0, 0, 0, 0 } };
+    hm_attrs_t hm_adapters_amd[DEVICES_MAX] = { { { 0 }, 0, 0, 0, 0, 0 } };
 
     if (gpu_temp_disable == 0)
     {
-      #if defined(WIN) && defined(HAVE_NVAPI)
-      NVAPI_PTR *nvapi = (NVAPI_PTR *) mymalloc (sizeof (NVAPI_PTR));
-
-      if (nvapi_init (nvapi) == 0)
-        data.hm_nv = nvapi;
-
-      if (data.hm_nv)
-      {
-        if (hm_NvAPI_Initialize (data.hm_nv) == NVAPI_OK)
-        {
-          HM_ADAPTER_NV nvGPUHandle[DEVICES_MAX] = { 0 };
-
-          int tmp_in = hm_get_adapter_index_nv (nvGPUHandle);
-
-          int tmp_out = 0;
-
-          for (int i = 0; i < tmp_in; i++)
-          {
-            hm_adapters_nv[tmp_out++].adapter_index.nv = nvGPUHandle[i];
-          }
-
-          for (int i = 0; i < tmp_out; i++)
-          {
-            NV_GPU_COOLER_SETTINGS pCoolerSettings;
-
-            pCoolerSettings.Version = GPU_COOLER_SETTINGS_VER | sizeof (NV_GPU_COOLER_SETTINGS);
-
-            if (hm_NvAPI_GPU_GetCoolerSettings (data.hm_nv, hm_adapters_nv[i].adapter_index.nv, 0, &pCoolerSettings) != NVAPI_NOT_SUPPORTED) hm_adapters_nv[i].fan_supported = 1;
-          }
-        }
-      }
-      #endif // WIN && HAVE_NVAPI
-
-      #if defined(LINUX) && defined(HAVE_NVML)
       NVML_PTR *nvml = (NVML_PTR *) mymalloc (sizeof (NVML_PTR));
 
       if (nvml_init (nvml) == 0)
@@ -14014,15 +14009,28 @@ int main (int argc, char **argv)
           {
             unsigned int speed;
 
-            if (hm_NVML_nvmlDeviceGetFanSpeed (data.hm_nv, 1, hm_adapters_nv[i].adapter_index.nv, &speed) != NVML_ERROR_NOT_SUPPORTED) hm_adapters_nv[i].fan_supported = 1;
+            if (hm_NVML_nvmlDeviceGetFanSpeed (data.hm_nv, 1, hm_adapters_nv[i].adapter_index.nv, &speed) != NVML_ERROR_NOT_SUPPORTED) hm_adapters_nv[i].fan_get_supported = 1;
+
+            hm_NVML_nvmlDeviceSetComputeMode (data.hm_nv, 1, hm_adapters_nv[i].adapter_index.nv, NVML_COMPUTEMODE_EXCLUSIVE_PROCESS);
+
+            hm_NVML_nvmlDeviceSetGpuOperationMode (data.hm_nv, 1, hm_adapters_nv[i].adapter_index.nv, NVML_GOM_ALL_ON);
+
+            unsigned int minLimit;
+            unsigned int maxLimit;
+
+            if (hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (data.hm_nv, 1, hm_adapters_nv[i].adapter_index.nv, &minLimit, &maxLimit) == NVML_SUCCESS)
+            {
+              if (maxLimit > 0)
+              {
+                hm_NVML_nvmlDeviceSetPowerManagementLimit (data.hm_nv, 1, hm_adapters_nv[i].adapter_index.nv, maxLimit);
+              }
+            }
           }
         }
       }
-      #endif // LINUX && HAVE_NVML
 
       data.hm_amd = NULL;
 
-      #ifdef HAVE_ADL
       ADL_PTR *adl = (ADL_PTR *) mymalloc (sizeof (ADL_PTR));
 
       if (adl_init (adl) == 0)
@@ -14068,7 +14076,6 @@ int main (int argc, char **argv)
           myfree (lpAdapterInfo);
         }
       }
-      #endif // HAVE_ADL
 
       if (data.hm_amd == NULL && data.hm_nv == NULL)
       {
@@ -14080,34 +14087,17 @@ int main (int argc, char **argv)
      * OpenCL devices: allocate buffer for device specific information
      */
 
-    #ifdef HAVE_HWMON
-    int *temp_retain_fanspeed_value = (int *) mycalloc (data.devices_cnt, sizeof (int));
+    int *temp_retain_fanspeed_value  = (int *) mycalloc (data.devices_cnt, sizeof (int));
+    int *temp_retain_fanpolicy_value = (int *) mycalloc (data.devices_cnt, sizeof (int));
 
-    #ifdef HAVE_ADL
     ADLOD6MemClockState *od_clock_mem_status = (ADLOD6MemClockState *) mycalloc (data.devices_cnt, sizeof (ADLOD6MemClockState));
 
     int *od_power_control_status = (int *) mycalloc (data.devices_cnt, sizeof (int));
-    #endif // ADL
-    #endif
-
-    /**
-     * enable custom signal handler(s)
-     */
-
-    if (benchmark == 0)
-    {
-      hc_signal (sigHandler_default);
-    }
-    else
-    {
-      hc_signal (sigHandler_benchmark);
-    }
 
     /**
      * User-defined GPU temp handling
      */
 
-    #ifdef HAVE_HWMON
     if (gpu_temp_disable == 1)
     {
       gpu_temp_abort  = 0;
@@ -14129,6 +14119,19 @@ int main (int argc, char **argv)
     data.gpu_temp_retain  = gpu_temp_retain;
     #endif
 
+    /**
+     * enable custom signal handler(s)
+     */
+
+    if (benchmark == 0)
+    {
+      hc_signal (sigHandler_default);
+    }
+    else
+    {
+      hc_signal (sigHandler_benchmark);
+    }
+
     /**
      * inform the user
      */
@@ -14188,6 +14191,8 @@ int main (int argc, char **argv)
       #endif
     }
 
+    #ifdef HAVE_HWMON
+
     /**
      * HM devices: copy
      */
@@ -14204,31 +14209,26 @@ int main (int argc, char **argv)
 
         const uint platform_devices_id = device_param->platform_devices_id;
 
-        #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
         if (device_param->device_vendor_id == VENDOR_ID_NV)
         {
           memcpy (&data.hm_device[device_id], &hm_adapters_nv[platform_devices_id], sizeof (hm_attrs_t));
         }
-        #endif
 
-        #ifdef HAVE_ADL
         if (device_param->device_vendor_id == VENDOR_ID_AMD)
         {
           memcpy (&data.hm_device[device_id], &hm_adapters_amd[platform_devices_id], sizeof (hm_attrs_t));
         }
-        #endif
       }
     }
 
-   /*
-    * Temporary fix:
-    * with AMD r9 295x cards it seems that we need to set the powertune value just AFTER the ocl init stuff
-    * otherwise after hc_clCreateContext () etc, powertune value was set back to "normal" and cards unfortunately
-    * were not working @ full speed (setting hm_ADL_Overdrive_PowerControl_Set () here seems to fix the problem)
-    * Driver / ADL bug?
-    */
+    /**
+     * Temporary fix:
+     * with AMD r9 295x cards it seems that we need to set the powertune value just AFTER the ocl init stuff
+     * otherwise after hc_clCreateContext () etc, powertune value was set back to "normal" and cards unfortunately
+     * were not working @ full speed (setting hm_ADL_Overdrive_PowerControl_Set () here seems to fix the problem)
+     * Driver / ADL bug?
+     */
 
-    #ifdef HAVE_ADL
     if (powertune_enable == 1)
     {
       hc_thread_mutex_lock (mux_adl);
@@ -14278,7 +14278,7 @@ int main (int argc, char **argv)
 
       hc_thread_mutex_unlock (mux_adl);
     }
-    #endif // HAVE_ADK
+
     #endif // HAVE_HWMON
 
     #ifdef DEBUG
@@ -15566,59 +15566,73 @@ int main (int argc, char **argv)
         run_kernel_bzero (device_param, device_param->d_markov_css_buf, size_markov_css);
       }
 
+      #if defined(HAVE_HWMON)
+
       /**
-       * Store initial fanspeed if gpu_temp_retain is enabled
+       * Store thermal target temperature so we can send a notice to user
        */
 
-      #if defined(HAVE_HWMON) && defined(HAVE_ADL)
-      int gpu_temp_retain_set = 0;
+      if (gpu_temp_disable == 0)
+      {
+        const int gpu_temp_threshold_slowdown = hm_get_threshold_slowdown_with_device_id (device_id);
+        const int gpu_temp_threshold_shutdown = hm_get_threshold_slowdown_with_device_id (device_id);
+
+        data.hm_device[device_id].gpu_temp_threshold_slowdown = (gpu_temp_threshold_slowdown > 0) ? gpu_temp_threshold_slowdown : 10000;
+        data.hm_device[device_id].gpu_temp_threshold_shutdown = (gpu_temp_threshold_shutdown > 0) ? gpu_temp_threshold_shutdown : 10000;
+
+        // we could use those numbers for gpu_temp_retain and gpu_temp_abort, too
+      }
+
+      /**
+       * Store initial fanspeed if gpu_temp_retain is enabled
+       */
 
       if (gpu_temp_disable == 0)
       {
-        if (gpu_temp_retain != 0) // VENDOR_ID_AMD implied
+        if (gpu_temp_retain != 0)
         {
           hc_thread_mutex_lock (mux_adl);
 
-          if (data.hm_device[device_id].fan_supported == 1)
+          if (data.hm_device[device_id].fan_get_supported == 1)
           {
-            if (gpu_temp_retain_chgd == 0)
-            {
-              uint cur_temp = 0;
-              uint default_temp = 0;
+            const int fanspeed  = hm_get_fanspeed_with_device_id  (device_id);
+            const int fanpolicy = hm_get_fanpolicy_with_device_id (device_id);
 
-              int ADL_rc = hm_ADL_Overdrive6_TargetTemperatureData_Get (data.hm_amd, data.hm_device[device_id].adapter_index.amd, (int *) &cur_temp, (int *) &default_temp);
-
-              if (ADL_rc == ADL_OK)
-              {
-                #define GPU_TEMP_RETAIN_ABORT_DIFF 15
+            temp_retain_fanspeed_value[device_id]  = fanspeed;
+            temp_retain_fanpolicy_value[device_id] = fanpolicy;
 
-                const uint gpu_temp_retain_target = default_temp - GPU_TEMP_RETAIN_ABORT_DIFF;
+            // we also set it to tell the OS we take control over the fan and it's automatic controller
+            // if it was set to automatic. we do not control user-defined fanspeeds.
 
-                // special case with multi gpu setups: always use minimum retain
+            if (fanpolicy == 1)
+            {
+              data.hm_device[device_id].fan_set_supported = 1;
 
-                if (gpu_temp_retain_set == 0)
-                {
-                  gpu_temp_retain = gpu_temp_retain_target;
-                  gpu_temp_retain_set = 1;
-                }
-                else
-                {
-                  gpu_temp_retain = MIN (gpu_temp_retain, gpu_temp_retain_target);
-                }
+              int rc = -1;
 
-                if (gpu_temp_abort_chgd == 0) gpu_temp_abort = gpu_temp_retain + GPU_TEMP_RETAIN_ABORT_DIFF;
+              if (device_param->device_vendor_id == VENDOR_ID_AMD)
+              {
+                rc = hm_set_fanspeed_with_device_id_amd (device_id, fanspeed, 1);
               }
-            }
+              else if (device_param->device_vendor_id == VENDOR_ID_NV)
+              {
 
-            const int fan_speed = hm_get_fanspeed_with_device_id (device_id);
+              }
 
-            temp_retain_fanspeed_value[device_id] = fan_speed;
+              if (rc == 0)
+              {
+                data.hm_device[device_id].fan_set_supported = 1;
+              }
+              else
+              {
+                log_info ("WARNING: Failed to set initial fan speed for device #%u", device_id + 1);
 
-            if (fan_speed == -1)
+                data.hm_device[device_id].fan_set_supported = 0;
+              }
+            }
+            else
             {
-              log_info ("WARNING: Failed to get current fan speed settings for gpu number: %i:", device_id + 1);
-
-              temp_retain_fanspeed_value[device_id] = 0;
+              data.hm_device[device_id].fan_set_supported = 0;
             }
           }
 
@@ -15741,7 +15755,8 @@ int main (int argc, char **argv)
 
         hc_thread_mutex_unlock (mux_adl);
       }
-      #endif // HAVE_HWMON && HAVE_ADL
+
+      #endif // HAVE_HWMON
     }
 
     data.kernel_power_all = kernel_power_all;
@@ -17854,7 +17869,6 @@ int main (int argc, char **argv)
     #ifdef HAVE_HWMON
     if (gpu_temp_disable == 0)
     {
-      #ifdef HAVE_ADL
       if (gpu_temp_retain != 0) // VENDOR_ID_AMD is implied here
       {
         hc_thread_mutex_lock (mux_adl);
@@ -17865,24 +17879,33 @@ int main (int argc, char **argv)
 
           if (device_param->skipped) continue;
 
-          if (data.hm_device[device_id].fan_supported == 1)
+          if (data.hm_device[device_id].fan_set_supported == 1)
           {
-            int fanspeed = temp_retain_fanspeed_value[device_id];
+            int fanspeed  = temp_retain_fanspeed_value[device_id];
+            int fanpolicy = temp_retain_fanpolicy_value[device_id];
 
-            if (fanspeed == -1) continue;
+            if (fanpolicy == 1)
+            {
+              int rc = -1;
 
-            int rc = hm_set_fanspeed_with_device_id_amd (device_id, fanspeed);
+              if (device_param->device_vendor_id == VENDOR_ID_AMD)
+              {
+                rc = hm_set_fanspeed_with_device_id_amd (device_id, fanspeed, 0);
+              }
+              else if (device_param->device_vendor_id == VENDOR_ID_NV)
+              {
+
+              }
 
-            if (rc == -1) log_info ("WARNING: Failed to restore default fan speed for gpu number: %i:", device_id);
+              if (rc == -1) log_info ("WARNING: Failed to restore default fan speed and policy for device #%", device_id + 1);
+            }
           }
         }
 
         hc_thread_mutex_unlock (mux_adl);
       }
-      #endif // HAVE_ADL
     }
 
-    #ifdef HAVE_ADL
     // reset power tuning
 
     if (powertune_enable == 1) // VENDOR_ID_AMD is implied here
@@ -17944,40 +17967,26 @@ int main (int argc, char **argv)
 
       hc_thread_mutex_unlock (mux_adl);
     }
-    #endif // HAVE_ADL
 
     if (gpu_temp_disable == 0)
     {
-      #if defined(HAVE_NVML) || defined(HAVE_NVAPI)
       if (data.hm_nv)
       {
-        #if defined(LINUX) && defined(HAVE_NVML)
-
         hm_NVML_nvmlShutdown (data.hm_nv);
 
         nvml_close (data.hm_nv);
 
-        #elif defined(WIN) && (HAVE_NVAPI)
-
-        hm_NvAPI_Unload (data.hm_nv);
-
-        nvapi_close (data.hm_nv);
-
-        #endif
-
         data.hm_nv = NULL;
       }
-      #endif
 
-      #ifdef HAVE_ADL
       if (data.hm_amd)
       {
         hm_ADL_Main_Control_Destroy (data.hm_amd);
 
         adl_close (data.hm_amd);
+
         data.hm_amd = NULL;
       }
-      #endif
     }
     #endif // HAVE_HWMON
 
@@ -18020,10 +18029,8 @@ int main (int argc, char **argv)
 
     #ifdef HAVE_HWMON
     local_free (temp_retain_fanspeed_value);
-    #ifdef HAVE_ADL
     local_free (od_clock_mem_status);
     local_free (od_power_control_status);
-    #endif // ADL
     #endif
 
     global_free (devices_param);