Hardware/ATI/ATIGPU.cs
author moel.mich
Sat, 19 Mar 2011 22:55:05 +0000
changeset 265 961c07a3bd78
parent 247 6dc755f1970e
child 270 ef89cbe45b7c
permissions -rw-r--r--
Fixed Issue 156.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Globalization;
    40 
    41 namespace OpenHardwareMonitor.Hardware.ATI {
    42   internal sealed class ATIGPU : Hardware {
    43 
    44     private readonly string name;
    45     private readonly int adapterIndex;
    46     private readonly int busNumber;
    47     private readonly int deviceNumber;
    48     private readonly Sensor temperature;
    49     private readonly Sensor fan;
    50     private readonly Sensor coreClock;
    51     private readonly Sensor memoryClock;
    52     private readonly Sensor coreVoltage;
    53     private readonly Sensor coreLoad;
    54     private readonly Sensor controlSensor;
    55     private readonly Control fanControl;
    56 
    57     private bool restoreDefaultFanSpeedRequired = false;
    58     private ADLFanSpeedValue initialFanSpeedValue;    
    59 
    60     public ATIGPU(string name, int adapterIndex, int busNumber, 
    61       int deviceNumber, ISettings settings) 
    62     {
    63       this.name = name;
    64       this.adapterIndex = adapterIndex;
    65       this.busNumber = busNumber;
    66       this.deviceNumber = deviceNumber;
    67 
    68       this.temperature = new Sensor("GPU Core", 0, SensorType.Temperature, this, settings);
    69       this.fan = new Sensor("GPU Fan", 0, SensorType.Fan, this, settings);
    70       this.coreClock = new Sensor("GPU Core", 0, SensorType.Clock, this, settings);
    71       this.memoryClock = new Sensor("GPU Memory", 1, SensorType.Clock, this, settings);
    72       this.coreVoltage = new Sensor("GPU Core", 0, SensorType.Voltage, this, settings);
    73       this.coreLoad = new Sensor("GPU Core", 0, SensorType.Load, this, settings);
    74       this.controlSensor = new Sensor("GPU Fan", 0, SensorType.Control, this, settings);
    75 
    76       ADLFanSpeedInfo afsi = new ADLFanSpeedInfo();
    77       if (ADL.ADL_Overdrive5_FanSpeedInfo_Get(adapterIndex, 0, ref afsi)
    78         != ADL.ADL_OK) 
    79       {
    80         afsi.MaxPercent = 100;
    81         afsi.MinPercent = 0;
    82       }
    83 
    84       this.fanControl = new Control(controlSensor, settings, afsi.MinPercent, 
    85         afsi.MaxPercent);
    86       this.fanControl.ControlModeChanged += ControlModeChanged;
    87       this.fanControl.SoftwareControlValueChanged += 
    88         SoftwareControlValueChanged;
    89       ControlModeChanged(fanControl);
    90       this.controlSensor.Control = fanControl;
    91       Update();                   
    92     }
    93 
    94     private void SaveDefaultFanSpeed() {
    95       if (!restoreDefaultFanSpeedRequired) {        
    96         initialFanSpeedValue = new ADLFanSpeedValue();
    97         initialFanSpeedValue.SpeedType =
    98           ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
    99         restoreDefaultFanSpeedRequired = 
   100           ADL.ADL_Overdrive5_FanSpeed_Get(adapterIndex, 0,
   101           ref initialFanSpeedValue) == ADL.ADL_OK;
   102       }
   103     }
   104 
   105     private void RestoreDefaultFanSpeed() {
   106       if (restoreDefaultFanSpeedRequired) {        
   107         ADL.ADL_Overdrive5_FanSpeed_Set(adapterIndex, 0,
   108           ref this.initialFanSpeedValue);
   109         restoreDefaultFanSpeedRequired = false;
   110       }
   111     }
   112 
   113     private void SoftwareControlValueChanged(IControl control) {
   114       if (control.ControlMode == ControlMode.Software) {
   115         SaveDefaultFanSpeed();
   116         ADLFanSpeedValue adlf = new ADLFanSpeedValue();
   117         adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
   118         adlf.Flags = ADL.ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
   119         adlf.FanSpeed = (int)control.SoftwareValue;
   120         ADL.ADL_Overdrive5_FanSpeed_Set(adapterIndex, 0, ref adlf);
   121       }
   122     }
   123 
   124     private void ControlModeChanged(IControl control) {
   125       if (control.ControlMode == ControlMode.Default) {
   126         RestoreDefaultFanSpeed();     
   127       } else {
   128         SoftwareControlValueChanged(control);
   129       }
   130     }
   131 
   132     public int BusNumber { get { return busNumber; } }
   133 
   134     public int DeviceNumber { get { return deviceNumber; } }
   135 
   136     public override string Name {
   137       get { return name; }
   138     }
   139 
   140     public override Identifier Identifier {
   141       get { 
   142         return new Identifier("atigpu", 
   143           adapterIndex.ToString(CultureInfo.InvariantCulture)); 
   144       }
   145     }
   146 
   147     public override HardwareType HardwareType {
   148       get { return HardwareType.GpuAti; }
   149     }
   150 
   151     public override void Update() {
   152       ADLTemperature adlt = new ADLTemperature();
   153       if (ADL.ADL_Overdrive5_Temperature_Get(adapterIndex, 0, ref adlt)
   154         == ADL.ADL_OK) 
   155       {
   156         temperature.Value = 0.001f * adlt.Temperature;
   157         ActivateSensor(temperature);
   158       } else {
   159         temperature.Value = null;
   160       }
   161 
   162       ADLFanSpeedValue adlf = new ADLFanSpeedValue();
   163       adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_RPM;
   164       if (ADL.ADL_Overdrive5_FanSpeed_Get(adapterIndex, 0, ref adlf)
   165         == ADL.ADL_OK) 
   166       {
   167         fan.Value = adlf.FanSpeed;
   168         ActivateSensor(fan);
   169       } else {
   170         fan.Value = null;
   171       }
   172 
   173       adlf = new ADLFanSpeedValue();
   174       adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
   175       if (ADL.ADL_Overdrive5_FanSpeed_Get(adapterIndex, 0, ref adlf)
   176         == ADL.ADL_OK) {
   177         controlSensor.Value = adlf.FanSpeed;
   178         ActivateSensor(controlSensor);
   179       } else {
   180         controlSensor.Value = null;
   181       }
   182 
   183       ADLPMActivity adlp = new ADLPMActivity();
   184       if (ADL.ADL_Overdrive5_CurrentActivity_Get(adapterIndex, ref adlp)
   185         == ADL.ADL_OK) 
   186       {
   187         if (adlp.EngineClock > 0) {
   188           coreClock.Value = 0.01f * adlp.EngineClock;
   189           ActivateSensor(coreClock);
   190         }
   191 
   192         if (adlp.MemoryClock > 0) {
   193           memoryClock.Value = 0.01f * adlp.MemoryClock;
   194           ActivateSensor(memoryClock);
   195         }
   196 
   197         if (adlp.Vddc > 0) {
   198           coreVoltage.Value = 0.001f * adlp.Vddc;
   199           ActivateSensor(coreVoltage);
   200         }
   201 
   202         coreLoad.Value = Math.Min(adlp.ActivityPercent, 100);                        
   203         ActivateSensor(coreLoad);
   204       } else {
   205         coreClock.Value = null;
   206         memoryClock.Value = null;
   207         coreVoltage.Value = null;
   208         coreLoad.Value = null;
   209       }
   210     }
   211 
   212     public void Close() {
   213       this.fanControl.ControlModeChanged -= ControlModeChanged;
   214       this.fanControl.SoftwareControlValueChanged -=
   215         SoftwareControlValueChanged;
   216 
   217       RestoreDefaultFanSpeed();
   218     }
   219   }
   220 }