Hardware/ATI/ATIGPU.cs
author moel.mich
Sat, 11 Aug 2012 21:48:54 +0000
changeset 379 7af3aaeb42e9
parent 298 96263190189a
permissions -rw-r--r--
Added a few checks and delays to the driver loading code to increase the chance of loading the driver.
moel@1
     1
/*
moel@1
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@1
     6
 
moel@344
     7
  Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@1
     9
*/
moel@1
    10
moel@1
    11
using System;
moel@166
    12
using System.Globalization;
moel@1
    13
moel@1
    14
namespace OpenHardwareMonitor.Hardware.ATI {
moel@166
    15
  internal sealed class ATIGPU : Hardware {
moel@1
    16
moel@195
    17
    private readonly int adapterIndex;
moel@195
    18
    private readonly int busNumber;
moel@195
    19
    private readonly int deviceNumber;
moel@195
    20
    private readonly Sensor temperature;
moel@195
    21
    private readonly Sensor fan;
moel@195
    22
    private readonly Sensor coreClock;
moel@195
    23
    private readonly Sensor memoryClock;
moel@195
    24
    private readonly Sensor coreVoltage;
moel@195
    25
    private readonly Sensor coreLoad;
moel@247
    26
    private readonly Sensor controlSensor;
moel@247
    27
    private readonly Control fanControl;
moel@247
    28
moel@248
    29
    private bool restoreDefaultFanSpeedRequired = false;
moel@248
    30
    private ADLFanSpeedValue initialFanSpeedValue;    
moel@1
    31
moel@3
    32
    public ATIGPU(string name, int adapterIndex, int busNumber, 
moel@165
    33
      int deviceNumber, ISettings settings) 
moel@275
    34
      : base(name, new Identifier("atigpu", 
moel@275
    35
        adapterIndex.ToString(CultureInfo.InvariantCulture)), settings)
moel@3
    36
    {
moel@1
    37
      this.adapterIndex = adapterIndex;
moel@3
    38
      this.busNumber = busNumber;
moel@3
    39
      this.deviceNumber = deviceNumber;
moel@1
    40
moel@165
    41
      this.temperature = new Sensor("GPU Core", 0, SensorType.Temperature, this, settings);
moel@165
    42
      this.fan = new Sensor("GPU Fan", 0, SensorType.Fan, this, settings);
moel@165
    43
      this.coreClock = new Sensor("GPU Core", 0, SensorType.Clock, this, settings);
moel@165
    44
      this.memoryClock = new Sensor("GPU Memory", 1, SensorType.Clock, this, settings);
moel@165
    45
      this.coreVoltage = new Sensor("GPU Core", 0, SensorType.Voltage, this, settings);
moel@165
    46
      this.coreLoad = new Sensor("GPU Core", 0, SensorType.Load, this, settings);
moel@247
    47
      this.controlSensor = new Sensor("GPU Fan", 0, SensorType.Control, this, settings);
moel@247
    48
moel@247
    49
      ADLFanSpeedInfo afsi = new ADLFanSpeedInfo();
moel@247
    50
      if (ADL.ADL_Overdrive5_FanSpeedInfo_Get(adapterIndex, 0, ref afsi)
moel@247
    51
        != ADL.ADL_OK) 
moel@247
    52
      {
moel@247
    53
        afsi.MaxPercent = 100;
moel@247
    54
        afsi.MinPercent = 0;
moel@247
    55
      }
moel@247
    56
moel@247
    57
      this.fanControl = new Control(controlSensor, settings, afsi.MinPercent, 
moel@247
    58
        afsi.MaxPercent);
moel@247
    59
      this.fanControl.ControlModeChanged += ControlModeChanged;
moel@247
    60
      this.fanControl.SoftwareControlValueChanged += 
moel@247
    61
        SoftwareControlValueChanged;
moel@247
    62
      ControlModeChanged(fanControl);
moel@247
    63
      this.controlSensor.Control = fanControl;
moel@1
    64
      Update();                   
moel@1
    65
    }
moel@1
    66
moel@248
    67
    private void SaveDefaultFanSpeed() {
moel@248
    68
      if (!restoreDefaultFanSpeedRequired) {        
moel@248
    69
        initialFanSpeedValue = new ADLFanSpeedValue();
moel@248
    70
        initialFanSpeedValue.SpeedType =
moel@248
    71
          ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
moel@248
    72
        restoreDefaultFanSpeedRequired = 
moel@248
    73
          ADL.ADL_Overdrive5_FanSpeed_Get(adapterIndex, 0,
moel@248
    74
          ref initialFanSpeedValue) == ADL.ADL_OK;
moel@248
    75
      }
moel@248
    76
    }
moel@248
    77
moel@248
    78
    private void RestoreDefaultFanSpeed() {
moel@248
    79
      if (restoreDefaultFanSpeedRequired) {        
moel@248
    80
        ADL.ADL_Overdrive5_FanSpeed_Set(adapterIndex, 0,
moel@248
    81
          ref this.initialFanSpeedValue);
moel@280
    82
        if ((initialFanSpeedValue.Flags &
moel@280
    83
          ADL.ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED) == 0)
moel@280
    84
          ADL.ADL_Overdrive5_FanSpeedToDefault_Set(adapterIndex, 0);
moel@248
    85
        restoreDefaultFanSpeedRequired = false;
moel@248
    86
      }
moel@248
    87
    }
moel@248
    88
moel@247
    89
    private void SoftwareControlValueChanged(IControl control) {
moel@247
    90
      if (control.ControlMode == ControlMode.Software) {
moel@248
    91
        SaveDefaultFanSpeed();
moel@247
    92
        ADLFanSpeedValue adlf = new ADLFanSpeedValue();
moel@247
    93
        adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
moel@247
    94
        adlf.Flags = ADL.ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
moel@247
    95
        adlf.FanSpeed = (int)control.SoftwareValue;
moel@247
    96
        ADL.ADL_Overdrive5_FanSpeed_Set(adapterIndex, 0, ref adlf);
moel@247
    97
      }
moel@247
    98
    }
moel@247
    99
moel@247
   100
    private void ControlModeChanged(IControl control) {
moel@247
   101
      if (control.ControlMode == ControlMode.Default) {
moel@248
   102
        RestoreDefaultFanSpeed();     
moel@247
   103
      } else {
moel@248
   104
        SoftwareControlValueChanged(control);
moel@247
   105
      }
moel@247
   106
    }
moel@247
   107
moel@3
   108
    public int BusNumber { get { return busNumber; } }
moel@3
   109
moel@3
   110
    public int DeviceNumber { get { return deviceNumber; } }
moel@3
   111
moel@1
   112
moel@165
   113
    public override HardwareType HardwareType {
moel@176
   114
      get { return HardwareType.GpuAti; }
moel@1
   115
    }
moel@1
   116
moel@110
   117
    public override void Update() {
moel@1
   118
      ADLTemperature adlt = new ADLTemperature();
moel@15
   119
      if (ADL.ADL_Overdrive5_Temperature_Get(adapterIndex, 0, ref adlt)
moel@15
   120
        == ADL.ADL_OK) 
moel@15
   121
      {
moel@15
   122
        temperature.Value = 0.001f * adlt.Temperature;
moel@15
   123
        ActivateSensor(temperature);
moel@15
   124
      } else {
moel@164
   125
        temperature.Value = null;
moel@15
   126
      }
moel@1
   127
moel@1
   128
      ADLFanSpeedValue adlf = new ADLFanSpeedValue();
moel@1
   129
      adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_RPM;
moel@15
   130
      if (ADL.ADL_Overdrive5_FanSpeed_Get(adapterIndex, 0, ref adlf)
moel@15
   131
        == ADL.ADL_OK) 
moel@15
   132
      {
moel@15
   133
        fan.Value = adlf.FanSpeed;
moel@15
   134
        ActivateSensor(fan);
moel@15
   135
      } else {
moel@164
   136
        fan.Value = null;
moel@164
   137
      }
moel@164
   138
moel@164
   139
      adlf = new ADLFanSpeedValue();
moel@164
   140
      adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
moel@164
   141
      if (ADL.ADL_Overdrive5_FanSpeed_Get(adapterIndex, 0, ref adlf)
moel@164
   142
        == ADL.ADL_OK) {
moel@247
   143
        controlSensor.Value = adlf.FanSpeed;
moel@247
   144
        ActivateSensor(controlSensor);
moel@164
   145
      } else {
moel@247
   146
        controlSensor.Value = null;
moel@15
   147
      }
moel@1
   148
moel@1
   149
      ADLPMActivity adlp = new ADLPMActivity();
moel@15
   150
      if (ADL.ADL_Overdrive5_CurrentActivity_Get(adapterIndex, ref adlp)
moel@15
   151
        == ADL.ADL_OK) 
moel@15
   152
      {
moel@80
   153
        if (adlp.EngineClock > 0) {
moel@80
   154
          coreClock.Value = 0.01f * adlp.EngineClock;
moel@80
   155
          ActivateSensor(coreClock);
moel@270
   156
        } else {
moel@270
   157
          coreClock.Value = null;
moel@80
   158
        }
moel@80
   159
moel@80
   160
        if (adlp.MemoryClock > 0) {
moel@80
   161
          memoryClock.Value = 0.01f * adlp.MemoryClock;
moel@80
   162
          ActivateSensor(memoryClock);
moel@270
   163
        } else {
moel@270
   164
          memoryClock.Value = null;
moel@80
   165
        }
moel@80
   166
moel@80
   167
        if (adlp.Vddc > 0) {
moel@80
   168
          coreVoltage.Value = 0.001f * adlp.Vddc;
moel@80
   169
          ActivateSensor(coreVoltage);
moel@270
   170
        } else {
moel@270
   171
          coreVoltage.Value = null;
moel@80
   172
        }
moel@80
   173
moel@126
   174
        coreLoad.Value = Math.Min(adlp.ActivityPercent, 100);                        
moel@24
   175
        ActivateSensor(coreLoad);
moel@15
   176
      } else {
moel@164
   177
        coreClock.Value = null;
moel@164
   178
        memoryClock.Value = null;
moel@164
   179
        coreVoltage.Value = null;
moel@164
   180
        coreLoad.Value = null;
moel@15
   181
      }
moel@1
   182
    }
moel@247
   183
moel@298
   184
    public override void Close() {
moel@247
   185
      this.fanControl.ControlModeChanged -= ControlModeChanged;
moel@247
   186
      this.fanControl.SoftwareControlValueChanged -=
moel@247
   187
        SoftwareControlValueChanged;
moel@247
   188
moel@248
   189
      RestoreDefaultFanSpeed();
moel@298
   190
      base.Close();
moel@247
   191
    }
moel@1
   192
  }
moel@1
   193
}