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