Hardware/Nvidia/NVAPI.cs
changeset 1 361e324a0ed4
child 38 0e09d845eb00
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Hardware/Nvidia/NVAPI.cs	Tue Jan 26 22:37:48 2010 +0000
     1.3 @@ -0,0 +1,243 @@
     1.4 +/*
     1.5 +  
     1.6 +  Version: MPL 1.1/GPL 2.0/LGPL 2.1
     1.7 +
     1.8 +  The contents of this file are subject to the Mozilla Public License Version
     1.9 +  1.1 (the "License"); you may not use this file except in compliance with
    1.10 +  the License. You may obtain a copy of the License at
    1.11 + 
    1.12 +  http://www.mozilla.org/MPL/
    1.13 +
    1.14 +  Software distributed under the License is distributed on an "AS IS" basis,
    1.15 +  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    1.16 +  for the specific language governing rights and limitations under the License.
    1.17 +
    1.18 +  The Original Code is the Open Hardware Monitor code.
    1.19 +
    1.20 +  The Initial Developer of the Original Code is 
    1.21 +  Michael Möller <m.moeller@gmx.ch>.
    1.22 +  Portions created by the Initial Developer are Copyright (C) 2009-2010
    1.23 +  the Initial Developer. All Rights Reserved.
    1.24 +
    1.25 +  Contributor(s):
    1.26 +
    1.27 +  Alternatively, the contents of this file may be used under the terms of
    1.28 +  either the GNU General Public License Version 2 or later (the "GPL"), or
    1.29 +  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    1.30 +  in which case the provisions of the GPL or the LGPL are applicable instead
    1.31 +  of those above. If you wish to allow use of your version of this file only
    1.32 +  under the terms of either the GPL or the LGPL, and not to allow others to
    1.33 +  use your version of this file under the terms of the MPL, indicate your
    1.34 +  decision by deleting the provisions above and replace them with the notice
    1.35 +  and other provisions required by the GPL or the LGPL. If you do not delete
    1.36 +  the provisions above, a recipient may use your version of this file under
    1.37 +  the terms of any one of the MPL, the GPL or the LGPL.
    1.38 + 
    1.39 +*/
    1.40 +
    1.41 +using System;
    1.42 +using System.Collections.Generic;
    1.43 +using System.Runtime.InteropServices;
    1.44 +using System.Text;
    1.45 +
    1.46 +namespace OpenHardwareMonitor.Hardware.Nvidia {
    1.47 +
    1.48 +  public enum NvStatus {
    1.49 +    OK = 0,
    1.50 +    ERROR = -1,
    1.51 +    LIBRARY_NOT_FOUND = -2,
    1.52 +    NO_IMPLEMENTATION = -3,
    1.53 +    API_NOT_INTIALIZED = -4,
    1.54 +    INVALID_ARGUMENT = -5,
    1.55 +    NVIDIA_DEVICE_NOT_FOUND = -6,
    1.56 +    END_ENUMERATION = -7,
    1.57 +    INVALID_HANDLE = -8,
    1.58 +    INCOMPATIBLE_STRUCT_VERSION = -9,
    1.59 +    HANDLE_INVALIDATED = -10,
    1.60 +    OPENGL_CONTEXT_NOT_CURRENT = -11,
    1.61 +    NO_GL_EXPERT = -12,
    1.62 +    INSTRUMENTATION_DISABLED = -13,
    1.63 +    EXPECTED_LOGICAL_GPU_HANDLE = -100,
    1.64 +    EXPECTED_PHYSICAL_GPU_HANDLE = -101,
    1.65 +    EXPECTED_DISPLAY_HANDLE = -102,
    1.66 +    INVALID_COMBINATION = -103,
    1.67 +    NOT_SUPPORTED = -104,
    1.68 +    PORTID_NOT_FOUND = -105,
    1.69 +    EXPECTED_UNATTACHED_DISPLAY_HANDLE = -106,
    1.70 +    INVALID_PERF_LEVEL = -107,
    1.71 +    DEVICE_BUSY = -108,
    1.72 +    NV_PERSIST_FILE_NOT_FOUND = -109,
    1.73 +    PERSIST_DATA_NOT_FOUND = -110,
    1.74 +    EXPECTED_TV_DISPLAY = -111,
    1.75 +    EXPECTED_TV_DISPLAY_ON_DCONNECTOR = -112,
    1.76 +    NO_ACTIVE_SLI_TOPOLOGY = -113,
    1.77 +    SLI_RENDERING_MODE_NOTALLOWED = -114,
    1.78 +    EXPECTED_DIGITAL_FLAT_PANEL = -115,
    1.79 +    ARGUMENT_EXCEED_MAX_SIZE = -116,
    1.80 +    DEVICE_SWITCHING_NOT_ALLOWED = -117,
    1.81 +    TESTING_CLOCKS_NOT_SUPPORTED = -118,
    1.82 +    UNKNOWN_UNDERSCAN_CONFIG = -119,
    1.83 +    TIMEOUT_RECONFIGURING_GPU_TOPO = -120,
    1.84 +    DATA_NOT_FOUND = -121,
    1.85 +    EXPECTED_ANALOG_DISPLAY = -122,
    1.86 +    NO_VIDLINK = -123,
    1.87 +    REQUIRES_REBOOT = -124,
    1.88 +    INVALID_HYBRID_MODE = -125,
    1.89 +    MIXED_TARGET_TYPES = -126,
    1.90 +    SYSWOW64_NOT_SUPPORTED = -127,
    1.91 +    IMPLICIT_SET_GPU_TOPOLOGY_CHANGE_NOT_ALLOWED = -128,
    1.92 +    REQUEST_USER_TO_CLOSE_NON_MIGRATABLE_APPS = -129,
    1.93 +    OUT_OF_MEMORY = -130,
    1.94 +    WAS_STILL_DRAWING = -131,
    1.95 +    FILE_NOT_FOUND = -132,
    1.96 +    TOO_MANY_UNIQUE_STATE_OBJECTS = -133,
    1.97 +    INVALID_CALL = -134,
    1.98 +    D3D10_1_LIBRARY_NOT_FOUND = -135,
    1.99 +    FUNCTION_NOT_FOUND = -136
   1.100 +  }  
   1.101 +
   1.102 +  public enum NvThermalController {
   1.103 +    NONE = 0,
   1.104 +    GPU_INTERNAL,  
   1.105 +    ADM1032,
   1.106 +    MAX6649,       
   1.107 +    MAX1617,      
   1.108 +    LM99,      
   1.109 +    LM89,         
   1.110 +    LM64,         
   1.111 +    ADT7473,
   1.112 +    SBMAX6649,
   1.113 +    VBIOSEVT,  
   1.114 +    OS,    
   1.115 +    UNKNOWN = -1,
   1.116 +  } 
   1.117 +
   1.118 +  public enum NvThermalTarget {
   1.119 +    NONE = 0,
   1.120 +    GPU = 1,
   1.121 +    MEMORY = 2,
   1.122 +    POWER_SUPPLY = 4,
   1.123 +    BOARD = 8,
   1.124 +    ALL = 15,
   1.125 +    UNKNOWN = -1
   1.126 +  };
   1.127 +
   1.128 +  [StructLayout(LayoutKind.Sequential)]
   1.129 +  public struct NvSensor {
   1.130 +    public NvThermalController Controller;
   1.131 +    public int DefaultMinTemp;
   1.132 +    public int DefaultMaxTemp;
   1.133 +    public int CurrentTemp;
   1.134 +    public NvThermalTarget Target;     
   1.135 +  }
   1.136 +
   1.137 +  [StructLayout(LayoutKind.Sequential)]
   1.138 +  public struct NvGPUThermalSettings {
   1.139 +    public int Version;
   1.140 +    public int Count;
   1.141 +    [MarshalAs(UnmanagedType.ByValArray, 
   1.142 +      SizeConst = NVAPI.MAX_THERMAL_SENSORS_PER_GPU)]
   1.143 +    public NvSensor[] Sensor;
   1.144 +  }
   1.145 +
   1.146 +  [StructLayout(LayoutKind.Sequential)]
   1.147 +  public struct NvDisplayHandle {
   1.148 +    private int handle;
   1.149 +  }
   1.150 +
   1.151 +  [StructLayout(LayoutKind.Sequential)]
   1.152 +  public struct NvPhysicalGpuHandle {
   1.153 +    private int handle;
   1.154 +  }
   1.155 +
   1.156 +  public class NVAPI {
   1.157 +          
   1.158 +    private const int SHORT_STRING_MAX = 64;
   1.159 +
   1.160 +    public const int MAX_THERMAL_SENSORS_PER_GPU = 3;
   1.161 +    public const int MAX_PHYSICAL_GPUS = 64;
   1.162 +    public static readonly int GPU_THERMAL_SETTINGS_VER =
   1.163 +      Marshal.SizeOf(typeof(NvGPUThermalSettings)) | 0x10000;
   1.164 +            
   1.165 +    private delegate IntPtr nvapi_QueryInterfaceDelegate(uint id);
   1.166 +    private delegate NvStatus NvAPI_InitializeDelegate();
   1.167 +    private delegate NvStatus NvAPI_GPU_GetFullNameDelegate(
   1.168 +      NvPhysicalGpuHandle gpuHandle, StringBuilder name);
   1.169 +
   1.170 +    public delegate NvStatus NvAPI_GPU_GetThermalSettingsDelegate(
   1.171 +      NvPhysicalGpuHandle gpuHandle, int sensorIndex, 
   1.172 +      ref NvGPUThermalSettings nvGPUThermalSettings);
   1.173 +    public delegate NvStatus NvAPI_EnumNvidiaDisplayHandleDelegate(int thisEnum,
   1.174 +      ref NvDisplayHandle displayHandle);
   1.175 +    public delegate NvStatus NvAPI_GetPhysicalGPUsFromDisplayDelegate(
   1.176 +      NvDisplayHandle displayHandle, [Out] NvPhysicalGpuHandle[] gpuHandles, 
   1.177 +      out int gpuCount);
   1.178 +    public delegate NvStatus NvAPI_EnumPhysicalGPUsDelegate(
   1.179 +      [Out] NvPhysicalGpuHandle[] gpuHandles, out int gpuCount);
   1.180 +
   1.181 +    private static bool available = false;
   1.182 +    private static nvapi_QueryInterfaceDelegate nvapi_QueryInterface;
   1.183 +    private static NvAPI_InitializeDelegate NvAPI_Initialize;
   1.184 +    private static NvAPI_GPU_GetFullNameDelegate _NvAPI_GPU_GetFullName;
   1.185 +
   1.186 +    public static NvAPI_GPU_GetThermalSettingsDelegate 
   1.187 +      NvAPI_GPU_GetThermalSettings;
   1.188 +    public static NvAPI_EnumNvidiaDisplayHandleDelegate
   1.189 +      NvAPI_EnumNvidiaDisplayHandle;
   1.190 +    public static NvAPI_GetPhysicalGPUsFromDisplayDelegate
   1.191 +      NvAPI_GetPhysicalGPUsFromDisplay;
   1.192 +    public static NvAPI_EnumPhysicalGPUsDelegate
   1.193 +      NvAPI_EnumPhysicalGPUs;
   1.194 +
   1.195 +    public static NvStatus NvAPI_GPU_GetFullName(NvPhysicalGpuHandle gpuHandle,
   1.196 +      out string name) {
   1.197 +      StringBuilder builder = new StringBuilder(SHORT_STRING_MAX);
   1.198 +      NvStatus status = _NvAPI_GPU_GetFullName(gpuHandle, builder);
   1.199 +      name = builder.ToString();
   1.200 +      return status;
   1.201 +    }
   1.202 +
   1.203 +    private static string GetDllName() {
   1.204 +      if (IntPtr.Size == 4) {
   1.205 +        return "nvapi.dll";
   1.206 +      } else {
   1.207 +        return "nvapi64.dll";
   1.208 +      }
   1.209 +    }
   1.210 +
   1.211 +    private static void GetDelegate<T>(uint id, out T newDelegate) 
   1.212 +      where T : class 
   1.213 +    {
   1.214 +      IntPtr ptr = nvapi_QueryInterface(id);
   1.215 +      newDelegate = Marshal.GetDelegateForFunctionPointer(ptr, typeof(T)) as T;      
   1.216 +    }
   1.217 +
   1.218 +    static NVAPI() { 
   1.219 +      DllImportAttribute attribute = new DllImportAttribute(GetDllName());
   1.220 +      attribute.CallingConvention = CallingConvention.Cdecl;
   1.221 +      attribute.PreserveSig = true;
   1.222 +      attribute.EntryPoint = "nvapi_QueryInterface";
   1.223 +      PInvokeDelegateFactory.CreateDelegate(attribute,
   1.224 +        out nvapi_QueryInterface);
   1.225 +
   1.226 +      try {
   1.227 +        GetDelegate(0x0150E828, out NvAPI_Initialize);
   1.228 +      } catch (DllNotFoundException) { return; } 
   1.229 +        catch (ArgumentNullException) { return; }
   1.230 +
   1.231 +      if (NvAPI_Initialize() == NvStatus.OK) {
   1.232 +        GetDelegate(0xE3640A56, out NvAPI_GPU_GetThermalSettings);
   1.233 +        GetDelegate(0xCEEE8E9F, out _NvAPI_GPU_GetFullName);
   1.234 +        GetDelegate(0x9ABDD40D, out NvAPI_EnumNvidiaDisplayHandle);
   1.235 +        GetDelegate(0x34EF9506, out NvAPI_GetPhysicalGPUsFromDisplay);
   1.236 +        GetDelegate(0xE5AC921F, out NvAPI_EnumPhysicalGPUs);
   1.237 +        available = true;
   1.238 +      }
   1.239 +    }
   1.240 +
   1.241 +    public static bool IsAvailable {
   1.242 +      get { return available; }
   1.243 +    }
   1.244 +
   1.245 +  }
   1.246 +}