Hardware/ATI/ATIGPU.cs
author moel.mich
Thu, 20 Jan 2011 21:31:54 +0000
changeset 247 6dc755f1970e
parent 195 0ee888c485d5
child 248 045eb5c1ec32
permissions -rw-r--r--
Added a minimal control interface to allow manual fan control and implemented the interface for ATI GPUs.
     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 ADLFanSpeedValue initialFanSpeedValue;
    58 
    59     public ATIGPU(string name, int adapterIndex, int busNumber, 
    60       int deviceNumber, ISettings settings) 
    61     {
    62       this.name = name;
    63       this.adapterIndex = adapterIndex;
    64       this.busNumber = busNumber;
    65       this.deviceNumber = deviceNumber;
    66 
    67       this.initialFanSpeedValue = new ADLFanSpeedValue();
    68       this.initialFanSpeedValue.SpeedType = 
    69         ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
    70       ADL.ADL_Overdrive5_FanSpeed_Get(adapterIndex, 0, 
    71         ref this.initialFanSpeedValue);
    72 
    73       this.temperature = new Sensor("GPU Core", 0, SensorType.Temperature, this, settings);
    74       this.fan = new Sensor("GPU Fan", 0, SensorType.Fan, this, settings);
    75       this.coreClock = new Sensor("GPU Core", 0, SensorType.Clock, this, settings);
    76       this.memoryClock = new Sensor("GPU Memory", 1, SensorType.Clock, this, settings);
    77       this.coreVoltage = new Sensor("GPU Core", 0, SensorType.Voltage, this, settings);
    78       this.coreLoad = new Sensor("GPU Core", 0, SensorType.Load, this, settings);
    79       this.controlSensor = new Sensor("GPU Fan", 0, SensorType.Control, this, settings);
    80 
    81       ADLFanSpeedInfo afsi = new ADLFanSpeedInfo();
    82       if (ADL.ADL_Overdrive5_FanSpeedInfo_Get(adapterIndex, 0, ref afsi)
    83         != ADL.ADL_OK) 
    84       {
    85         afsi.MaxPercent = 100;
    86         afsi.MinPercent = 0;
    87       }
    88 
    89       this.fanControl = new Control(controlSensor, settings, afsi.MinPercent, 
    90         afsi.MaxPercent);
    91       this.fanControl.ControlModeChanged += ControlModeChanged;
    92       this.fanControl.SoftwareControlValueChanged += 
    93         SoftwareControlValueChanged;
    94       ControlModeChanged(fanControl);
    95       this.controlSensor.Control = fanControl;
    96       Update();                   
    97     }
    98 
    99     private void SoftwareControlValueChanged(IControl control) {
   100       if (control.ControlMode == ControlMode.Software) {
   101         ADLFanSpeedValue adlf = new ADLFanSpeedValue();
   102         adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
   103         adlf.Flags = ADL.ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
   104         adlf.FanSpeed = (int)control.SoftwareValue;
   105         ADL.ADL_Overdrive5_FanSpeed_Set(adapterIndex, 0, ref adlf);
   106       }
   107     }
   108 
   109     private void ControlModeChanged(IControl control) {
   110       if (control.ControlMode == ControlMode.Default) {
   111         ADL.ADL_Overdrive5_FanSpeed_Set(adapterIndex, 0,
   112           ref this.initialFanSpeedValue);
   113       } else {
   114         ADLFanSpeedValue adlf = new ADLFanSpeedValue();
   115         adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
   116         adlf.Flags = ADL.ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED;
   117         adlf.FanSpeed = (int)control.SoftwareValue;
   118         ADL.ADL_Overdrive5_FanSpeed_Set(adapterIndex, 0, ref adlf);
   119       }
   120     }
   121 
   122     public int BusNumber { get { return busNumber; } }
   123 
   124     public int DeviceNumber { get { return deviceNumber; } }
   125 
   126     public override string Name {
   127       get { return name; }
   128     }
   129 
   130     public override Identifier Identifier {
   131       get { 
   132         return new Identifier("atigpu", 
   133           adapterIndex.ToString(CultureInfo.InvariantCulture)); 
   134       }
   135     }
   136 
   137     public override HardwareType HardwareType {
   138       get { return HardwareType.GpuAti; }
   139     }
   140 
   141     public override void Update() {
   142       ADLTemperature adlt = new ADLTemperature();
   143       if (ADL.ADL_Overdrive5_Temperature_Get(adapterIndex, 0, ref adlt)
   144         == ADL.ADL_OK) 
   145       {
   146         temperature.Value = 0.001f * adlt.Temperature;
   147         ActivateSensor(temperature);
   148       } else {
   149         temperature.Value = null;
   150       }
   151 
   152       ADLFanSpeedValue adlf = new ADLFanSpeedValue();
   153       adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_RPM;
   154       if (ADL.ADL_Overdrive5_FanSpeed_Get(adapterIndex, 0, ref adlf)
   155         == ADL.ADL_OK) 
   156       {
   157         fan.Value = adlf.FanSpeed;
   158         ActivateSensor(fan);
   159       } else {
   160         fan.Value = null;
   161       }
   162 
   163       adlf = new ADLFanSpeedValue();
   164       adlf.SpeedType = ADL.ADL_DL_FANCTRL_SPEED_TYPE_PERCENT;
   165       if (ADL.ADL_Overdrive5_FanSpeed_Get(adapterIndex, 0, ref adlf)
   166         == ADL.ADL_OK) {
   167         controlSensor.Value = adlf.FanSpeed;
   168         ActivateSensor(controlSensor);
   169       } else {
   170         controlSensor.Value = null;
   171       }
   172 
   173       ADLPMActivity adlp = new ADLPMActivity();
   174       if (ADL.ADL_Overdrive5_CurrentActivity_Get(adapterIndex, ref adlp)
   175         == ADL.ADL_OK) 
   176       {
   177         if (adlp.EngineClock > 0) {
   178           coreClock.Value = 0.01f * adlp.EngineClock;
   179           ActivateSensor(coreClock);
   180         }
   181 
   182         if (adlp.MemoryClock > 0) {
   183           memoryClock.Value = 0.01f * adlp.MemoryClock;
   184           ActivateSensor(memoryClock);
   185         }
   186 
   187         if (adlp.Vddc > 0) {
   188           coreVoltage.Value = 0.001f * adlp.Vddc;
   189           ActivateSensor(coreVoltage);
   190         }
   191 
   192         coreLoad.Value = Math.Min(adlp.ActivityPercent, 100);                        
   193         ActivateSensor(coreLoad);
   194       } else {
   195         coreClock.Value = null;
   196         memoryClock.Value = null;
   197         coreVoltage.Value = null;
   198         coreLoad.Value = null;
   199       }
   200     }
   201 
   202     public void Close() {
   203       this.fanControl.ControlModeChanged -= ControlModeChanged;
   204       this.fanControl.SoftwareControlValueChanged -=
   205         SoftwareControlValueChanged;
   206 
   207       ADL.ADL_Overdrive5_FanSpeed_Set(adapterIndex, 0,
   208         ref this.initialFanSpeedValue);
   209     }
   210   }
   211 }