Skip to content

Commit

Permalink
fanspeed control
Browse files Browse the repository at this point in the history
  • Loading branch information
orkblutt committed Aug 25, 2017
1 parent 84c16ce commit 7bff1ea
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 12 deletions.
40 changes: 34 additions & 6 deletions nvidiaapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ nvidiaAPI::nvidiaAPI():
NvGetPStatesInfoEx(NULL),
NvQueryIlluminationSupport(NULL),
NvGetIllumination(NULL),
NvSetIllumination(NULL)
NvSetIllumination(NULL),
NvGetCoolersSettings(NULL),
NvSetCoolerLevel(NULL)
{

NvQueryInterface = (NvAPI_QueryInterface_t)resolve("nvapi_QueryInterface");
if(NvQueryInterface)
{
Expand All @@ -50,12 +51,12 @@ nvidiaAPI::nvidiaAPI():
NvGetIllumination = (NvAPI_GPU_GetIllumination_t)NvQueryInterface(0x9A1B9365);
NvSetIllumination = (NvAPI_GPU_SetIllumination_t)NvQueryInterface(0x254A187);

NvGetCoolersSettings = (NvAPI_GPU_GetCoolersSettings_t)NvQueryInterface(0xDA141340);
NvSetCoolerLevel = (NvAPI_GPU_SetCoolerLevel_t)NvQueryInterface(0x891FA0AE);

NvAPI_Status ret = NvInit();

qDebug() << "NVAPI success " << ret;



}
}

Expand Down Expand Up @@ -162,13 +163,28 @@ unsigned int nvidiaAPI::getPowerLimit(unsigned int gpu)
if ((ret = NvClientPowerPoliciesGetStatus(_gpuHandles[gpu], &pol)) != NVAPI_OK)
{
qDebug() << "error";
return 0;
return 0;
}

return (uint8_t) (pol.entries[0].power / 1000); // in percent

}

unsigned int nvidiaAPI::getFanSpeed(unsigned int gpu)
{
NV_GPU_COOLER_SETTINGS coolerSettings;
coolerSettings.version = NV_GPU_COOLER_SETTINGS_VER;

NvAPI_Status ret = NvGetCoolersSettings(_gpuHandles[gpu], 0, &coolerSettings);
if(ret == NVAPI_OK)
{
return coolerSettings.cooler[0].currentLevel;
}
return 0;
}



int nvidiaAPI::setPowerLimitPercent(unsigned int gpu, unsigned int percent)
{
NvAPI_Status ret = NVAPI_OK;
Expand Down Expand Up @@ -242,6 +258,18 @@ int nvidiaAPI::setTempLimitOffset(unsigned int gpu, unsigned int offset)
return 0;
}

int nvidiaAPI::setFanSpeed(unsigned int gpu, unsigned int percent)
{
NV_GPU_COOLER_LEVELS coolerLvl;
coolerLvl.cooler[0].policy = 1;
coolerLvl.version = NV_GPU_COOLER_LEVELS_VER;
coolerLvl.cooler[0].level = percent;

NvAPI_Status ret = NvSetCoolerLevel(_gpuHandles[gpu], 0, &coolerLvl);

return ret;
}

void nvidiaAPI::setAllLED(int color)
{
for(unsigned int i = 0; i < _gpuCount; i++)
Expand Down
50 changes: 48 additions & 2 deletions nvidiaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,49 @@ typedef struct {
} NVAPI_COOLER_LEVEL;
#define NVAPI_COOLER_LEVEL_VER MAKE_NVAPI_VERSION(NVAPI_COOLER_LEVEL, 1)

#define NVAPI_MAX_COOLERS_PER_GPU 20


class nvidiaAPI : QLibrary
typedef struct {
NvU32 version;
NvU32 count;
struct {
NvS32 type;
NvS32 controller;
NvS32 defaultMin;
NvS32 defaultMax;
NvS32 currentMin;
NvS32 currentMax;
NvS32 currentLevel;
NvS32 defaultPolicy;
NvS32 currentPolicy;
NvS32 target;
NvS32 controlType;
NvS32 active;
} cooler[NVAPI_MAX_COOLERS_PER_GPU];
} NV_GPU_COOLER_SETTINGS_V2;
typedef NV_GPU_COOLER_SETTINGS_V2 NV_GPU_COOLER_SETTINGS;

#define NV_GPU_COOLER_SETTINGS_VER_2 MAKE_NVAPI_VERSION(NV_GPU_COOLER_SETTINGS_V2,2)
#define NV_GPU_COOLER_SETTINGS_VER NV_GPU_COOLER_SETTINGS_VER_2


typedef struct {
NvU32 version;
struct {
NvS32 level;
NvS32 policy;
} cooler[NVAPI_MAX_COOLERS_PER_GPU];
} NV_GPU_COOLER_LEVELS_V1;
typedef NV_GPU_COOLER_LEVELS_V1 NV_GPU_COOLER_LEVELS;


#define NV_GPU_COOLER_LEVELS_VER_1 MAKE_NVAPI_VERSION(NV_GPU_COOLER_LEVELS_V1,1)
#define NV_GPU_COOLER_LEVELS_VER NV_GPU_COOLER_LEVELS_VER_1



class nvidiaAPI : public QLibrary
{
public:
nvidiaAPI();
Expand All @@ -283,11 +323,14 @@ class nvidiaAPI : QLibrary

unsigned int getGpuClock(unsigned int gpu);
unsigned int getPowerLimit(unsigned int gpu);
unsigned int getFanSpeed(unsigned int gpu);


int setMemClockOffset(unsigned int gpu, int clock);
int setGPUOffset(unsigned int gpu, int offset);
int setPowerLimitPercent(unsigned int gpu, unsigned int percent);
int setTempLimitOffset(unsigned int gpu, unsigned int offset);
int setFanSpeed(unsigned int gpu, unsigned int percent);

void setAllLED(int color);

Expand All @@ -313,6 +356,8 @@ class nvidiaAPI : QLibrary
typedef NvAPI_Status (*NvAPI_DLL_ClientPowerPoliciesGetStatus_t)(NvPhysicalGpuHandle hPhysicalGpu, NVAPI_GPU_POWER_STATUS* pPolicies);
typedef NvAPI_Status (*NvAPI_DLL_ClientPowerPoliciesGetInfo_t)(NvPhysicalGpuHandle hPhysicalGpu, NVAPI_GPU_POWER_INFO* pInfo);
typedef NvAPI_Status (*NvAPI_DLL_ClientPowerPoliciesSetStatus_t)(NvPhysicalGpuHandle handle, NVAPI_GPU_POWER_STATUS* pPolicies);
typedef NvAPI_Status (*NvAPI_GPU_GetCoolersSettings_t)(NvPhysicalGpuHandle hPhysicalGpu, NvU32 coolerIndex, NV_GPU_COOLER_SETTINGS* coolerSettings);
typedef NvAPI_Status (*NvAPI_GPU_SetCoolerLevel_t)(NvPhysicalGpuHandle hPhysicalGpu, NvU32 coolerIndex, NV_GPU_COOLER_LEVELS* coolerLevel);

NvAPI_QueryInterface_t NvQueryInterface;
NvAPI_Initialize_t NvInit;
Expand All @@ -333,6 +378,8 @@ class nvidiaAPI : QLibrary
NvAPI_DLL_ClientPowerPoliciesGetStatus_t NvClientPowerPoliciesGetStatus;
NvAPI_DLL_ClientPowerPoliciesGetInfo_t NvClientPowerPoliciesGetInfo;
NvAPI_DLL_ClientPowerPoliciesSetStatus_t NvClientPowerPoliciesSetStatus;
NvAPI_GPU_GetCoolersSettings_t NvGetCoolersSettings;
NvAPI_GPU_SetCoolerLevel_t NvSetCoolerLevel;

private:

Expand All @@ -342,7 +389,6 @@ class nvidiaAPI : QLibrary

bool _libLoaded;


};

#endif // NVIDIAAPI_H
16 changes: 15 additions & 1 deletion nvocdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ nvOCDialog::nvOCDialog(nvidiaAPI *nvapi, QSettings *settings, QWidget *parent) :
nvCard card;
card.gpuOffset = 0;
card.memOffset = 0;
card.powerOffset = 0;
card.powerOffset = 100;
card.fanSpeed = 50;

_cardList << card;
ui->comboBoxDevice->addItem(QString("device " + QString::number(i)));
Expand Down Expand Up @@ -57,6 +58,11 @@ void nvOCDialog::on_horizontalSliderMemOffset_valueChanged(int value)
_cardList[_gpuIndex].memOffset = value;
}

void nvOCDialog::on_horizontalSliderFanSpeed_valueChanged(int value)
{
ui->lcdNumberFanSpeed->display(value);
_cardList[_gpuIndex].fanSpeed = value;
}

void nvOCDialog::on_comboBoxDevice_activated(int index)
{
Expand All @@ -69,15 +75,18 @@ void nvOCDialog::updateSliders(unsigned int gpu)
int plimit = _nvapi->getPowerLimit(gpu);
int gpuoffset = _nvapi->getGPUOffset(gpu);
int memoffset = _nvapi->getMemOffset(gpu);
int fanspeed = _nvapi->getFanSpeed(gpu);

ui->horizontalSliderPowerPercent->setValue(plimit);
ui->horizontalSliderGpuOffset->setValue(gpuoffset);
ui->horizontalSliderMemOffset->setValue(memoffset);
ui->horizontalSliderFanSpeed->setValue(fanspeed);
}

void nvOCDialog::saveConfig()
{
_settings->beginGroup("nvoc");

_settings->setValue("nvoc_applyall", ui->checkBoxAllDevices->isChecked());
_settings->setValue("nvoc_applyonstart", ui->checkBoxOCMinerStart->isChecked());

Expand All @@ -86,6 +95,7 @@ void nvOCDialog::saveConfig()
_settings->setValue(QString("powerlimitoffset" + QString::number(i)), _cardList.at(i).powerOffset);
_settings->setValue(QString("gpuoffset" + QString::number(i)), _cardList.at(i).gpuOffset);
_settings->setValue(QString("memoffset" + QString::number(i)), _cardList.at(i).memOffset);
_settings->setValue(QString("fanspeed" + QString::number(i)), _cardList.at(i).fanSpeed);
}

_settings->endGroup();
Expand All @@ -104,15 +114,19 @@ void nvOCDialog::on_buttonBox_clicked(QAbstractButton *button)
_nvapi->setPowerLimitPercent(i, ui->horizontalSliderPowerPercent->value());
_nvapi->setGPUOffset(i, ui->horizontalSliderGpuOffset->value());
_nvapi->setMemClockOffset(i, ui->horizontalSliderMemOffset->value());
_nvapi->setFanSpeed(i, ui->horizontalSliderFanSpeed->value());
}
}
else
{
_nvapi->setPowerLimitPercent(gpu, ui->horizontalSliderPowerPercent->value());
_nvapi->setGPUOffset(gpu, ui->horizontalSliderGpuOffset->value());
_nvapi->setMemClockOffset(gpu, ui->horizontalSliderMemOffset->value());
_nvapi->setFanSpeed(gpu, ui->horizontalSliderFanSpeed->value());
}

saveConfig();
}
}


3 changes: 3 additions & 0 deletions nvocdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct nvCard
int powerOffset;
int memOffset;
int gpuOffset;
int fanSpeed;
};

namespace Ui {
Expand All @@ -35,6 +36,8 @@ private slots:

void on_horizontalSliderMemOffset_valueChanged(int value);

void on_horizontalSliderFanSpeed_valueChanged(int value);

void on_comboBoxDevice_activated(int index);

void on_buttonBox_clicked(QAbstractButton *button);
Expand Down
19 changes: 16 additions & 3 deletions nvocdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<string>Core Clock (MHz) :</string>
</property>
</widget>
<widget class="QSlider" name="horizontalSlider_4">
<widget class="QSlider" name="horizontalSliderFanSpeed">
<property name="geometry">
<rect>
<x>136</x>
Expand All @@ -47,6 +47,9 @@
<height>22</height>
</rect>
</property>
<property name="maximum">
<number>100</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
Expand Down Expand Up @@ -236,8 +239,8 @@
<widget class="QCheckBox" name="checkBoxAutoSpeedFan">
<property name="geometry">
<rect>
<x>610</x>
<y>220</y>
<x>590</x>
<y>190</y>
<width>53</width>
<height>20</height>
</rect>
Expand Down Expand Up @@ -288,6 +291,16 @@
<set>QDialogButtonBox::Apply|QDialogButtonBox::Close</set>
</property>
</widget>
<widget class="QLCDNumber" name="lcdNumberFanSpeed">
<property name="geometry">
<rect>
<x>590</x>
<y>220</y>
<width>64</width>
<height>23</height>
</rect>
</property>
</widget>
</widget>
<resources/>
<connections>
Expand Down

0 comments on commit 7bff1ea

Please sign in to comment.