Skip to content

Instantly share code, notes, and snippets.

@Randl
Created March 21, 2017 21:20
Show Gist options
  • Save Randl/a055a45cb6960019e0324130c96123ff to your computer and use it in GitHub Desktop.
Save Randl/a055a45cb6960019e0324130c96123ff to your computer and use it in GitHub Desktop.
Overclocking tool for Nvidia GPUs
/*
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>, All rights reserved.
Copyright (C) 2017 Evgeniy Zheltonozhskiy <zheltonozhskiy@gmail.com>, All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/* https://1vwjbxf1wko0yhnr.wordpress.com/2015/08/10/overclocking-tools-for-nvidia-gpus-suck-i-made-my-own/ */
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
typedef unsigned long NvU32;
/* data structures we need from the NvAPI headers */
typedef struct {
NvU32 version;
NvU32 ClockType:2;
NvU32 reserved:22;
NvU32 reserved1:8;
struct {
NvU32 bIsPresent:1;
NvU32 reserved:31;
NvU32 frequency;
} domain[32];
} NV_GPU_CLOCK_FREQUENCIES_V2;
typedef struct {
int value;
struct {
int mindelta;
int maxdelta;
} valueRange;
} NV_GPU_PERF_PSTATES20_PARAM_DELTA;
typedef struct {
NvU32 domainId;
NvU32 typeId;
NvU32 bIsEditable:1;
NvU32 reserved:31;
NV_GPU_PERF_PSTATES20_PARAM_DELTA freqDelta_kHz;
union {
struct {
NvU32 freq_kHz;
} single;
struct {
NvU32 minFreq_kHz;
NvU32 maxFreq_kHz;
NvU32 domainId;
NvU32 minVoltage_uV;
NvU32 maxVoltage_uV;
} range;
} data;
} NV_GPU_PSTATE20_CLOCK_ENTRY_V1;
typedef struct {
NvU32 domainId;
NvU32 bIsEditable:1;
NvU32 reserved:31;
NvU32 volt_uV;
int voltDelta_uV;
} NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY_V1;
typedef struct {
NvU32 version;
NvU32 bIsEditable:1;
NvU32 reserved:31;
NvU32 numPstates;
NvU32 numClocks;
NvU32 numBaseVoltages;
struct {
NvU32 pstateId;
NvU32 bIsEditable:1;
NvU32 reserved:31;
NV_GPU_PSTATE20_CLOCK_ENTRY_V1 clocks[8];
NV_GPU_PSTATE20_BASE_VOLTAGE_ENTRY_V1 baseVoltages[4];
} pstates[16];
} NV_GPU_PERF_PSTATES20_INFO_V1;
/* prototypes for the functions we’ll use. */
typedef void *(*NvAPI_QueryInterface_t)(unsigned int offset);
typedef int (*NvAPI_Initialize_t)();
typedef int (*NvAPI_Unload_t)();
typedef int (*NvAPI_EnumPhysicalGPUs_t)(int **handles, int *count);
typedef int (*NvAPI_GPU_GetSystemType_t)(int *handle, int *systype);
typedef int (*NvAPI_GPU_GetFullName_t)(int *handle, char *sysname);
typedef int (*NvAPI_GPU_GetPhysicalFrameBufferSize_t)(int *handle, int *memsize);
typedef int (*NvAPI_GPU_GetRamType_t)(int *handle, int *memtype);
typedef int (*NvAPI_GPU_GetVbiosVersionString_t)(int *handle, char *biosname);
typedef int (*NvAPI_GPU_GetAllClockFrequencies_t)(int *handle, NV_GPU_PERF_PSTATES20_INFO_V1 *pstates_info);
typedef int (*NvAPI_GPU_GetPstates20_t)(int *handle, NV_GPU_PERF_PSTATES20_INFO_V1 *pstates_info);
typedef int (*NvAPI_GPU_SetPstates20_t)(int *handle, int *pstates_info);
NvAPI_QueryInterface_t NvQueryInterface = 0;
NvAPI_Initialize_t NvInit = 0;
NvAPI_Unload_t NvUnload = 0;
NvAPI_EnumPhysicalGPUs_t NvEnumGPUs = 0;
NvAPI_GPU_GetSystemType_t NvGetSysType = 0;
NvAPI_GPU_GetFullName_t NvGetName = 0;
NvAPI_GPU_GetPhysicalFrameBufferSize_t NvGetMemSize = 0;
NvAPI_GPU_GetRamType_t NvGetMemType = 0;
NvAPI_GPU_GetVbiosVersionString_t NvGetBiosName = 0;
NvAPI_GPU_GetAllClockFrequencies_t NvGetFreq = 0;
NvAPI_GPU_GetPstates20_t NvGetPstates = 0;
NvAPI_GPU_SetPstates20_t NvSetPstates = 0;
int main(int argc, char **argv) {
int nGPU = 0, userfreq = 0, systype = 0, memsize = 0, memtype = 0;
int *hdlGPU[64] = {0}, *buf = 0;
char sysname[64] = {0}, biosname[64] = {0};
NV_GPU_PERF_PSTATES20_INFO_V1 pstates_info;
pstates_info.version = 0x11c94;
if (!(NvQueryInterface = (void *) GetProcAddress(LoadLibrary("nvapi.dll"), "nvapi_QueryInterface"))
&& !(NvQueryInterface = (void *) GetProcAddress(LoadLibrary("nvapi64.dll"), "nvapi_QueryInterface"))) {
fprintf(stderr, "Can't load nvapi.dll or nvapi64.dll. Error %d", GetLastError());
return -1;
}
NvInit = NvQueryInterface(0x0150E828);
NvUnload = NvQueryInterface(0xD22BDD7E);
NvEnumGPUs = NvQueryInterface(0xE5AC921F);
NvGetSysType = NvQueryInterface(0xBAAABFCC);
NvGetName = NvQueryInterface(0xCEEE8E9F);
NvGetMemSize = NvQueryInterface(0x46FBEB03);
NvGetMemType = NvQueryInterface(0x57F7CAAC);
NvGetBiosName = NvQueryInterface(0xA561FD7D);
NvGetFreq = NvQueryInterface(0xDCB616C3);
NvGetPstates = NvQueryInterface(0x6FF81213);
NvSetPstates = NvQueryInterface(0x0F4DAE6B);
NvInit();
NvEnumGPUs(hdlGPU, &nGPU);
NvGetSysType(hdlGPU[0], &systype);
NvGetName(hdlGPU[0], sysname);
NvGetMemSize(hdlGPU[0], &memsize);
NvGetMemType(hdlGPU[0], &memtype);
NvGetBiosName(hdlGPU[0], biosname);
NvGetPstates(hdlGPU[0], &pstates_info);
switch (systype) {
case 1: printf("\nType: Laptop\n");
break;
case 2: printf("\nType: Desktop\n");
break;
default: printf("\nType: Unknown\n");
break;
}
printf("Name: %s\n", sysname);
printf("VRAM: %dMB GDDR%d\n", memsize / 1024, memtype <= 7 ? 3 : 5);
printf("BIOS: %s\n", biosname);
printf("\nGPU: %dMHz\n", (int) ((pstates_info.pstates[0].clocks[0]).data.range.maxFreq_kHz) / 1000);
printf("RAM: %dMHz\n", (int) ((pstates_info.pstates[0].clocks[1]).data.single.freq_kHz) / 1000);
printf("\nCurrent GPU OC: %dMHz\n", (int) ((pstates_info.pstates[0].clocks[0]).freqDelta_kHz.value) / 1000);
printf("Current RAM OC: %dMHz\n", (int) ((pstates_info.pstates[0].clocks[1]).freqDelta_kHz.value) / 1000);
if (argc > 1) {
userfreq = atoi(argv[1]) * 1000;
if (-250000 <= userfreq && userfreq <= 250000) {
buf = malloc(0x1c94);
memset(buf, 0, 0x1c94);
buf[0] = 0x11c94;
buf[2] = 1;
buf[3] = 1;
buf[10] = userfreq;
NvSetPstates(hdlGPU[0], buf) ? printf("\nGPU OC failed!\n") : printf("\nGPU OC OK: %d MHz\n", userfreq / 1000);
free(buf);
} else {
printf("\nGPU Frequency not in safe range (-250MHz to +250MHz).\n");
return 1;
}
}
if (argc > 2) {
userfreq = atoi(argv[2]) * 1000;
if (-250000 <= userfreq && userfreq <= 250000) {
buf = malloc(0x1c94);
memset(buf, 0, 0x1c94);
buf[0] = 0x11c94;
buf[2] = 1;
buf[3] = 1;
buf[7] = 4;
buf[10] = memtype <= 7 ? userfreq : userfreq * 2;
NvSetPstates(hdlGPU[0], buf) ? printf("VRAM OC failed!\n") : printf("RAM OC OK: %d MHz\n", userfreq / 1000);
free(buf);
} else {
printf("\nRAM Frequency not in safe range (-250MHz to +250MHz).\n");
return 1;
}
}
NvUnload();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment