1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
use std::{ptr, fmt};
use void::{Void, ResultVoidExt};
use sys::gpu::{self, pstate, clock, power, cooler, thermal, display};
use sys::{self, driverapi, i2c};
use types::{Kibibytes, KilohertzDelta, Kilohertz2Delta, Microvolts, Percentage, Percentage1000, RawConversion};
use thermal::CoolerLevel;
use clock::{ClockDomain, VfpMask};
use pstate::PState;

#[derive(Debug)]
pub struct PhysicalGpu(sys::handles::NvPhysicalGpuHandle);

unsafe impl Send for PhysicalGpu { }

pub use sys::gpu::{SystemType, PerformanceDecreaseReason};
pub use sys::gpu::private::{RamType, RamMaker, Foundry, VendorId as Vendor};
pub use sys::gpu::clock::ClockFrequencyType;
pub use sys::gpu::display::{ConnectedIdsFlags, DisplayIdsFlags, MonitorConnectorType};
pub type ClockFrequencies = <sys::gpu::clock::NV_GPU_CLOCK_FREQUENCIES as RawConversion>::Target;
pub type Utilizations = <pstate::NV_GPU_DYNAMIC_PSTATES_INFO_EX as RawConversion>::Target;

impl PhysicalGpu {
    pub fn handle(&self) -> &sys::handles::NvPhysicalGpuHandle {
        &self.0
    }

    pub fn enumerate() -> sys::Result<Vec<Self>> {
        trace!("gpu.enumerate()");
        let mut handles = [Default::default(); sys::types::NVAPI_MAX_PHYSICAL_GPUS];
        let mut len = 0;
        match unsafe { gpu::NvAPI_EnumPhysicalGPUs(&mut handles, &mut len) } {
            sys::status::NVAPI_NVIDIA_DEVICE_NOT_FOUND => Ok(Vec::new()),
            status => sys::status_result(status).map(move |_| handles[..len as usize].iter().cloned().map(PhysicalGpu).collect()),
        }
    }

    pub fn tachometer(&self) -> sys::Result<u32> {
        trace!("gpu.tachometer()");
        let mut out = 0;
        unsafe {
            sys::status_result(cooler::NvAPI_GPU_GetTachReading(self.0, &mut out))
                .map(move |_| out)
        }
    }

    pub fn short_name(&self) -> sys::Result<String> {
        trace!("gpu.short_name()");
        let mut str = sys::types::short_string();
        unsafe {
            sys::status_result(gpu::private::NvAPI_GPU_GetShortName(self.0, &mut str))
                .map(|_| str.convert_raw().void_unwrap())
        }
    }

    pub fn full_name(&self) -> sys::Result<String> {
        trace!("gpu.full_name()");
        let mut str = sys::types::short_string();
        unsafe {
            sys::status_result(gpu::NvAPI_GPU_GetFullName(self.0, &mut str))
                .map(|_| str.convert_raw().void_unwrap())
        }
    }

    pub fn vbios_version_string(&self) -> sys::Result<String> {
        trace!("gpu.vbios_version_string()");
        let mut str = sys::types::short_string();
        unsafe {
            sys::status_result(gpu::NvAPI_GPU_GetVbiosVersionString(self.0, &mut str))
                .map(|_| str.convert_raw().void_unwrap())
        }
    }

    pub fn driver_model(&self) -> sys::Result<DriverModel> {
        trace!("gpu.driver_model()");
        let mut value = 0;
        unsafe {
            sys::status_result(gpu::private::NvAPI_GetDriverModel(self.0, &mut value))
                .map(|_| DriverModel::new(value))
        }
    }

    pub fn gpu_id(&self) -> sys::Result<u32> {
        trace!("gpu.gpu_id()");
        let mut value = 0;
        unsafe {
            sys::status_result(gpu::private::NvAPI_GetGPUIDFromPhysicalGPU(self.0, &mut value))
                .map(|_| value)
        }
    }

    pub fn pci_identifiers(&self) -> sys::Result<PciIdentifiers> {
        trace!("gpu.pci_identifiers()");
        let mut pci = PciIdentifiers::default();
        unsafe {
            sys::status_result(gpu::NvAPI_GPU_GetPCIIdentifiers(self.0, &mut pci.device_id, &mut pci.subsystem_id, &mut pci.revision_id, &mut pci.ext_device_id))
                .map(|_| pci)
        }
    }

    pub fn system_type(&self) -> sys::Result<SystemType> {
        trace!("gpu.system_type()");
        let mut ty = gpu::NV_SYSTEM_TYPE_UNKNOWN;
        unsafe {
            sys::status_result(gpu::NvAPI_GPU_GetSystemType(self.0, &mut ty))
                .and_then(|_| gpu::SystemType::from_raw(ty).map_err(From::from))
        }
    }

    pub fn core_count(&self) -> sys::Result<u32> {
        trace!("gpu.core_count()");
        let mut value = 0;
        unsafe {
            sys::status_result(gpu::NvAPI_GPU_GetGpuCoreCount(self.0, &mut value))
                .map(|_| value)
        }
    }

    pub fn shader_pipe_count(&self) -> sys::Result<u32> {
        trace!("gpu.shader_pipe_count()");
        let mut value = 0;
        unsafe {
            sys::status_result(gpu::private::NvAPI_GPU_GetShaderPipeCount(self.0, &mut value))
                .map(|_| value)
        }
    }

    pub fn shader_sub_pipe_count(&self) -> sys::Result<u32> {
        trace!("gpu.shader_sub_pipe_count()");
        let mut value = 0;
        unsafe {
            sys::status_result(gpu::NvAPI_GPU_GetShaderSubPipeCount(self.0, &mut value))
                .map(|_| value)
        }
    }

    pub fn ram_type(&self) -> sys::Result<RamType> {
        trace!("gpu.ram_type()");
        let mut value = gpu::private::NV_GPU_RAM_UNKNOWN;
        unsafe {
            sys::status_result(gpu::private::NvAPI_GPU_GetRamType(self.0, &mut value))
                .and_then(|_| gpu::private::RamType::from_raw(value).map_err(From::from))
        }
    }

    pub fn ram_maker(&self) -> sys::Result<RamMaker> {
        trace!("gpu.ram_maker()");
        let mut value = gpu::private::NV_GPU_RAM_MAKER_UNKNOWN;
        unsafe {
            sys::status_result(gpu::private::NvAPI_GPU_GetRamMaker(self.0, &mut value))
                .and_then(|_| gpu::private::RamMaker::from_raw(value).map_err(From::from))
        }
    }

    pub fn ram_bus_width(&self) -> sys::Result<u32> {
        trace!("gpu.ram_bus_width()");
        let mut value = 0;
        unsafe {
            sys::status_result(gpu::private::NvAPI_GPU_GetRamBusWidth(self.0, &mut value))
                .map(|_| value)
        }
    }

    pub fn ram_bank_count(&self) -> sys::Result<u32> {
        trace!("gpu.ram_bank_count()");
        let mut value = 0;
        unsafe {
            sys::status_result(gpu::private::NvAPI_GPU_GetRamBankCount(self.0, &mut value))
                .map(|_| value)
        }
    }

    pub fn ram_partition_count(&self) -> sys::Result<u32> {
        trace!("gpu.ram_partition_count()");
        let mut value = 0;
        unsafe {
            sys::status_result(gpu::private::NvAPI_GPU_GetPartitionCount(self.0, &mut value))
                .map(|_| value)
        }
    }

    pub fn foundry(&self) -> sys::Result<Foundry> {
        trace!("gpu.foundry()");
        let mut value = gpu::private::NV_GPU_FOUNDRY_UNKNOWN;
        unsafe {
            sys::status_result(gpu::private::NvAPI_GPU_GetFoundry(self.0, &mut value))
                .and_then(|_| gpu::private::Foundry::from_raw(value).map_err(From::from))
        }
    }

    pub fn memory_info(&self) -> sys::Result<MemoryInfo> {
        trace!("gpu.memory_info()");
        let mut data = driverapi::NV_DISPLAY_DRIVER_MEMORY_INFO::zeroed();
        data.version = driverapi::NV_DISPLAY_DRIVER_MEMORY_INFO_VER;

        sys::status_result(unsafe { driverapi::NvAPI_GPU_GetMemoryInfo(self.0, &mut data) })
            .map(|_| data.convert_raw().void_unwrap())
    }

    pub fn clock_frequencies(&self, clock_type: ClockFrequencyType) -> sys::Result<ClockFrequencies> {
        trace!("gpu.clock_frequencies({:?})", clock_type);
        let mut clocks = clock::NV_GPU_CLOCK_FREQUENCIES::zeroed();
        clocks.version = clock::NV_GPU_CLOCK_FREQUENCIES_VER;
        clocks.set_ClockType(clock_type.raw());

        sys::status_result(unsafe { clock::NvAPI_GPU_GetAllClockFrequencies(self.0, &mut clocks) })
            .map(|_| clocks.convert_raw().void_unwrap())
    }

    pub fn current_pstate(&self) -> sys::Result<::pstate::PState> {
        trace!("gpu.current_pstate()");
        let mut pstate = 0;

        sys::status_result(unsafe { pstate::NvAPI_GPU_GetCurrentPstate(self.0, &mut pstate) })?;

        ::pstate::PState::from_raw(pstate).map_err(From::from)
    }

    pub fn pstates(&self) -> sys::Result<<pstate::NV_GPU_PERF_PSTATES20_INFO as RawConversion>::Target> {
        trace!("gpu.pstates()");
        let mut info = pstate::NV_GPU_PERF_PSTATES20_INFO::zeroed();
        info.version = pstate::NV_GPU_PERF_PSTATES20_INFO_VER;

        sys::status_result(unsafe { pstate::NvAPI_GPU_GetPstates20(self.0, &mut info) })
            .and_then(|_| info.convert_raw().map_err(From::from))
    }

    pub fn set_pstates<I: Iterator<Item=(PState, ClockDomain, KilohertzDelta)>>(&self, deltas: I) -> sys::Result<()> {
        trace!("gpu.set_pstates()");
        use std::collections::BTreeMap;

        let mut info = pstate::NV_GPU_PERF_PSTATES20_INFO::zeroed();
        info.version = pstate::NV_GPU_PERF_PSTATES20_INFO_VER;

        let mut map: BTreeMap<PState, (usize, usize)> = Default::default();
        for (pstate, clock, delta) in deltas {
            trace!("gpu.set_pstate({:?}, {:?}, {:?})", pstate, clock, delta);
            let pstates = map.len();
            let map = map.entry(pstate).or_insert((pstates, 0));
            let entry = &mut info.pstates[map.0];
            entry.pstateId = pstate.raw();
            let entry = &mut entry.clocks[map.1];
            entry.domainId = clock.raw();
            entry.freqDelta_kHz.value = delta.0;
            map.1 += 1;
        }
        info.numPstates = map.len() as _;
        info.numClocks = map.iter().map(|v| (v.1).1).max().unwrap_or(0) as _;

        sys::status_result(unsafe { pstate::private::NvAPI_GPU_SetPstates20(self.0, &info) })
    }

    pub fn dynamic_pstates_info(&self) -> sys::Result<Utilizations> {
        trace!("gpu.dynamic_pstates_info()");
        let mut info = pstate::NV_GPU_DYNAMIC_PSTATES_INFO_EX::zeroed();
        info.version = pstate::NV_GPU_DYNAMIC_PSTATES_INFO_EX_VER;

        sys::status_result(unsafe { pstate::NvAPI_GPU_GetDynamicPstatesInfoEx(self.0, &mut info) })
            .and_then(|_| info.convert_raw().map_err(From::from))
    }

    /// Private and deprecated, use `dynamic_pstates_info()` instead.
    pub fn usages(&self) -> sys::Result<<clock::private::NV_USAGES_INFO as RawConversion>::Target> {
        trace!("gpu.usages()");
        let mut usages = clock::private::NV_USAGES_INFO::zeroed();
        usages.version = clock::private::NV_USAGES_INFO_VER;

        sys::status_result(unsafe { clock::private::NvAPI_GPU_GetUsages(self.0, &mut usages) })
            .and_then(|_| usages.convert_raw().map_err(From::from))
    }

    pub fn vfp_mask(&self) -> sys::Result<<clock::private::NV_CLOCK_MASKS as RawConversion>::Target> {
        trace!("gpu.vfp_mask()");
        let mut data = clock::private::NV_CLOCK_MASKS::zeroed();
        data.version = clock::private::NV_CLOCK_MASKS_VER;

        sys::status_result(unsafe { clock::private::NvAPI_GPU_GetClockBoostMask(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn vfp_table(&self, mask: [u32; 4]) -> sys::Result<<clock::private::NV_CLOCK_TABLE as RawConversion>::Target> {
        trace!("gpu.vfp_table({:?})", mask);
        let mut data = clock::private::NV_CLOCK_TABLE::zeroed();
        data.version = clock::private::NV_CLOCK_TABLE_VER;
        data.mask = mask;

        sys::status_result(unsafe { clock::private::NvAPI_GPU_GetClockBoostTable(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn set_vfp_table<I: Iterator<Item=(usize, Kilohertz2Delta)>, M: Iterator<Item=(usize, Kilohertz2Delta)>>(&self, mask: [u32; 4], clocks: I, memory: M) -> sys::Result<()> {
        trace!("gpu.set_vfp_table({:?})", mask);
        let mut data = clock::private::NV_CLOCK_TABLE::zeroed();
        data.version = clock::private::NV_CLOCK_TABLE_VER;
        data.mask = mask;
        for (i, delta) in clocks {
            trace!("gpu.set_vfp_table({:?}, {:?})", i, delta);
            data.gpuDeltas[i].freqDeltaKHz = delta.0;
            VfpMask::set_bit(&mut data.mask, i);
        }
        for (i, delta) in memory {
            data.memFilled[i] = 1;
            data.memDeltas[i] = delta.0;
        }

        sys::status_result(unsafe { clock::private::NvAPI_GPU_SetClockBoostTable(self.0, &data) })
    }

    pub fn vfp_ranges(&self) -> sys::Result<<clock::private::NV_CLOCK_RANGES as RawConversion>::Target> {
        trace!("gpu.vfp_ranges()");
        let mut data = clock::private::NV_CLOCK_RANGES::zeroed();
        data.version = clock::private::NV_CLOCK_RANGES_VER;

        sys::status_result(unsafe { clock::private::NvAPI_GPU_GetClockBoostRanges(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn vfp_locks(&self) -> sys::Result<<clock::private::NV_CLOCK_LOCK as RawConversion>::Target> {
        trace!("gpu.vfp_locks()");
        let mut data = clock::private::NV_CLOCK_LOCK::zeroed();
        data.version = clock::private::NV_CLOCK_LOCK_VER;

        sys::status_result(unsafe { clock::private::NvAPI_GPU_GetClockBoostLock(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn set_vfp_locks<I: Iterator<Item=(usize, Option<Microvolts>)>>(&self, values: I) -> sys::Result<()> {
        trace!("gpu.set_vfp_locks()");
        use clock::ClockLockMode;

        let mut data = clock::private::NV_CLOCK_LOCK::zeroed();
        data.version = clock::private::NV_CLOCK_LOCK_VER;
        for (i, (id, voltage)) in values.enumerate() {
            trace!("gpu.set_vfp_lock({:?}, {:?})", id, voltage);
            data.count += 1;
            let entry = &mut data.entries[i];
            entry.id = id as _;
            if let Some(voltage) = voltage {
                entry.mode = ClockLockMode::Manual.raw();
                entry.voltage_uV = voltage.0;
            } else {
                // these are already 0
                //entry.mode = ClockLockMode::None.raw();
                //entry.voltage_uV = 0;
            }
        }

        sys::status_result(unsafe { clock::private::NvAPI_GPU_SetClockBoostLock(self.0, &data) })
    }

    pub fn vfp_curve(&self, mask: [u32; 4]) -> sys::Result<<power::private::NV_VFP_CURVE as RawConversion>::Target> {
        trace!("gpu.vfp_curve({:?})", mask);
        let mut data = power::private::NV_VFP_CURVE::zeroed();
        data.version = power::private::NV_VFP_CURVE_VER;
        data.mask = mask;

        sys::status_result(unsafe { power::private::NvAPI_GPU_GetVFPCurve(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn core_voltage(&self) -> sys::Result<<power::private::NV_VOLTAGE_STATUS as RawConversion>::Target> {
        trace!("gpu.core_voltage()");
        let mut data = power::private::NV_VOLTAGE_STATUS::zeroed();
        data.version = power::private::NV_VOLTAGE_STATUS_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_GetCurrentVoltage(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn core_voltage_boost(&self) -> sys::Result<<power::private::NV_VOLTAGE_BOOST_PERCENT as RawConversion>::Target> {
        trace!("gpu.core_voltage_boost()");
        let mut data = power::private::NV_VOLTAGE_BOOST_PERCENT::zeroed();
        data.version = power::private::NV_VOLTAGE_BOOST_PERCENT_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_GetCoreVoltageBoostPercent(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn set_core_voltage_boost(&self, value: Percentage) -> sys::Result<()> {
        trace!("gpu.set_core_voltage_boost({:?})", value);
        let mut data = power::private::NV_VOLTAGE_BOOST_PERCENT::zeroed();
        data.version = power::private::NV_VOLTAGE_BOOST_PERCENT_VER;
        data.percent = value.0;

        sys::status_result(unsafe { power::private::NvAPI_GPU_SetCoreVoltageBoostPercent(self.0, &data) })
    }

    pub fn power_usage(&self) -> sys::Result<<power::private::NV_GPU_POWER_TOPO as RawConversion>::Target> {
        trace!("gpu.power_usage()");
        let mut data = power::private::NV_GPU_POWER_TOPO::zeroed();
        data.version = power::private::NV_GPU_POWER_TOPO_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_ClientPowerTopologyGetStatus(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn power_limit_info(&self) -> sys::Result<<power::private::NV_GPU_POWER_INFO as RawConversion>::Target> {
        trace!("gpu.power_limit_info()");
        let mut data = power::private::NV_GPU_POWER_INFO::zeroed();
        data.version = power::private::NV_GPU_POWER_INFO_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_ClientPowerPoliciesGetInfo(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn power_limit(&self) -> sys::Result<<power::private::NV_GPU_POWER_STATUS as RawConversion>::Target> {
        trace!("gpu.power_limit()");
        let mut data = power::private::NV_GPU_POWER_STATUS::zeroed();
        data.version = power::private::NV_GPU_POWER_STATUS_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_ClientPowerPoliciesGetStatus(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn set_power_limit<I: Iterator<Item=Percentage1000>>(&self, values: I) -> sys::Result<()> {
        trace!("gpu.set_power_limit()");
        let mut data = power::private::NV_GPU_POWER_STATUS::zeroed();
        data.version = power::private::NV_GPU_POWER_STATUS_VER;
        //data.valid = 1;
        for (entry, v) in data.entries.iter_mut().zip(values) {
            trace!("gpu.set_power_limit({:?})", v);
            entry.power = v.0;
            data.count += 1;
        }

        sys::status_result(unsafe { power::private::NvAPI_GPU_ClientPowerPoliciesSetStatus(self.0, &data) })
    }

    pub fn thermal_settings(&self, index: Option<u32>) -> sys::Result<<thermal::NV_GPU_THERMAL_SETTINGS as RawConversion>::Target> {
        trace!("gpu.thermal_settings({:?})", index);
        let mut data = thermal::NV_GPU_THERMAL_SETTINGS::zeroed();
        data.version = thermal::NV_GPU_THERMAL_SETTINGS_VER;

        sys::status_result(unsafe { thermal::NvAPI_GPU_GetThermalSettings(self.0, index.unwrap_or(thermal::NVAPI_THERMAL_TARGET_ALL as _), &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn thermal_limit_info(&self) -> sys::Result<<thermal::private::NV_GPU_THERMAL_INFO as RawConversion>::Target> {
        trace!("gpu.thermal_limit_info()");
        let mut data = thermal::private::NV_GPU_THERMAL_INFO::zeroed();
        data.version = thermal::private::NV_GPU_THERMAL_INFO_VER;

        sys::status_result(unsafe { thermal::private::NvAPI_GPU_ClientThermalPoliciesGetInfo(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn thermal_limit(&self) -> sys::Result<<thermal::private::NV_GPU_THERMAL_LIMIT as RawConversion>::Target> {
        trace!("gpu.thermal_limit()");
        let mut data = thermal::private::NV_GPU_THERMAL_LIMIT::zeroed();
        data.version = thermal::private::NV_GPU_THERMAL_LIMIT_VER;

        sys::status_result(unsafe { thermal::private::NvAPI_GPU_ClientThermalPoliciesGetLimit(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn set_thermal_limit<I: Iterator<Item=::thermal::ThermalLimit>>(&self, value: I) -> sys::Result<()> {
        trace!("gpu.set_thermal_limit()");
        let mut data = thermal::private::NV_GPU_THERMAL_LIMIT::zeroed();
        data.version = thermal::private::NV_GPU_THERMAL_LIMIT_VER;
        for (entry, v) in data.entries.iter_mut().zip(value) {
            trace!("gpu.set_thermal_limit({:?})", v);
            entry.controller = v.controller.raw();
            entry.value = v.value.0 as _;
            entry.flags = v.flags;
            data.flags += 1;
        }

        sys::status_result(unsafe { thermal::private::NvAPI_GPU_ClientThermalPoliciesSetLimit(self.0, &data) })
    }

    pub fn cooler_settings(&self, index: Option<u32>) -> sys::Result<<cooler::private::NV_GPU_COOLER_SETTINGS as RawConversion>::Target> {
        trace!("gpu.cooler_settings({:?})", index);
        let mut data = cooler::private::NV_GPU_COOLER_SETTINGS::zeroed();
        data.version = cooler::private::NV_GPU_COOLER_SETTINGS_VER;

        sys::status_result(unsafe { cooler::private::NvAPI_GPU_GetCoolerSettings(self.0, index.unwrap_or(cooler::private::NVAPI_COOLER_TARGET_ALL as _), &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn set_cooler_levels<I: Iterator<Item=CoolerLevel>>(&self, index: Option<u32>, values: I) -> sys::Result<()> {
        trace!("gpu.set_cooler_levels({:?})", index);
        let mut data = cooler::private::NV_GPU_SETCOOLER_LEVEL::zeroed();
        data.version = cooler::private::NV_GPU_SETCOOLER_LEVEL_VER;
        for (entry, level) in data.cooler.iter_mut().zip(values) {
            trace!("gpu.set_cooler_level({:?})", level);
            entry.currentLevel = level.level.0;
            entry.currentPolicy = level.policy.raw();
        }

        sys::status_result(unsafe { cooler::private::NvAPI_GPU_SetCoolerLevels(self.0, index.unwrap_or(cooler::private::NVAPI_COOLER_TARGET_ALL as _), &mut data) })
    }

    pub fn restore_cooler_settings(&self, index: &[u32]) -> sys::Result<()> {
        trace!("gpu.restore_cooler_settings({:?})", index);
        let ptr = if index.is_empty() { ptr::null() } else { index.as_ptr() };
        sys::status_result(unsafe { cooler::private::NvAPI_GPU_RestoreCoolerSettings(self.0, ptr, index.len() as u32) })
    }

    pub fn cooler_policy_table(&self, index: u32, policy: ::thermal::CoolerPolicy) -> sys::Result<<cooler::private::NV_GPU_COOLER_POLICY_TABLE as RawConversion>::Target> {
        trace!("gpu.cooler_policy_table({:?})", index);
        let mut data = cooler::private::NV_GPU_COOLER_POLICY_TABLE::zeroed();
        data.version = cooler::private::NV_GPU_COOLER_POLICY_TABLE_VER;
        data.policy = policy.raw();
        let mut count = 0;

        sys::status_result(unsafe { cooler::private::NvAPI_GPU_GetCoolerPolicyTable(self.0, index, &mut data, &mut count) })
            .and_then(|_| data.convert_raw().map_err(From::from)).map(|mut c| {
                c.levels.truncate(count as usize);
                // TODO: ensure remaining levels are null?
                c
            })
    }

    pub fn set_cooler_policy_table(&self, index: u32, value: &<cooler::private::NV_GPU_COOLER_POLICY_TABLE as RawConversion>::Target) -> sys::Result<()> {
        trace!("gpu.set_cooler_policy_table({:?}, {:?})", index, value);
        let mut data = cooler::private::NV_GPU_COOLER_POLICY_TABLE::zeroed();
        data.version = cooler::private::NV_GPU_COOLER_POLICY_TABLE_VER;

        sys::status_result(unsafe { cooler::private::NvAPI_GPU_SetCoolerPolicyTable(self.0, index, &data, value.levels.len() as u32) })
    }

    pub fn restore_cooler_policy_table(&self, index: &[u32], policy: ::thermal::CoolerPolicy) -> sys::Result<()> {
        trace!("gpu.restore_cooler_policy_table({:?}, {:?})", index, policy);
        let ptr = if index.is_empty() { ptr::null() } else { index.as_ptr() };
        sys::status_result(unsafe { cooler::private::NvAPI_GPU_RestoreCoolerPolicyTable(self.0, ptr, index.len() as u32, policy.raw()) })
    }

    pub fn perf_info(&self) -> sys::Result<<power::private::NV_GPU_PERF_INFO as RawConversion>::Target> {
        trace!("gpu.perf_info()");
        let mut data = power::private::NV_GPU_PERF_INFO::zeroed();
        data.version = power::private::NV_GPU_PERF_INFO_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_PerfPoliciesGetInfo(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn perf_status(&self) -> sys::Result<<power::private::NV_GPU_PERF_STATUS as RawConversion>::Target> {
        trace!("gpu.perf_status()");
        let mut data = power::private::NV_GPU_PERF_STATUS::zeroed();
        data.version = power::private::NV_GPU_PERF_STATUS_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_PerfPoliciesGetStatus(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn voltage_domains_status(&self) -> sys::Result<<power::private::NV_VOLT_STATUS as RawConversion>::Target> {
        trace!("gpu.voltage_domains_status()");
        let mut data = power::private::NV_VOLT_STATUS::zeroed();
        data.version = power::private::NV_VOLT_STATUS_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_GetVoltageDomainsStatus(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn voltage_step(&self) -> sys::Result<<power::private::NV_VOLT_STATUS as RawConversion>::Target> {
        trace!("gpu.voltage_step()");
        let mut data = power::private::NV_VOLT_STATUS::zeroed();
        data.version = power::private::NV_VOLT_STATUS_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_GetVoltageStep(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn voltage_table(&self) -> sys::Result<<power::private::NV_VOLT_TABLE as RawConversion>::Target> {
        trace!("gpu.voltage_table()");
        let mut data = power::private::NV_VOLT_TABLE::zeroed();
        data.version = power::private::NV_VOLT_TABLE_VER;

        sys::status_result(unsafe { power::private::NvAPI_GPU_GetVoltages(self.0, &mut data) })
            .and_then(|_| data.convert_raw().map_err(From::from))
    }

    pub fn performance_decrease(&self) -> sys::Result<PerformanceDecreaseReason> {
        trace!("gpu.performance_decrease()");

        let mut data = gpu::NV_GPU_PERF_DECREASE_NONE;

        sys::status_result(unsafe { gpu::NvAPI_GPU_GetPerfDecreaseInfo(self.0, &mut data) })
            .map(|_| PerformanceDecreaseReason::from_bits_truncate(data))
    }

    pub fn display_ids_all(&self) -> sys::Result<Vec<<display::NV_GPU_DISPLAYIDS as RawConversion>::Target>> {
        trace!("gpu.display_ids_all()");
        let mut count = 0;
        sys::status_result(unsafe { display::NvAPI_GPU_GetAllDisplayIds(self.0, ptr::null_mut(), &mut count) })?;

        let mut data = display::NV_GPU_DISPLAYIDS::zeroed();
        data.version = display::NV_GPU_DISPLAYIDS_VER;
        let mut data = vec![data; count as usize];

        sys::status_result(unsafe { display::NvAPI_GPU_GetAllDisplayIds(self.0, data.as_mut_ptr(), &mut count) })
            .and_then(|_| data.into_iter().map(|v| v.convert_raw().map_err(From::from)).collect())
    }

    pub fn display_ids_connected(&self, flags: ConnectedIdsFlags) -> sys::Result<Vec<<display::NV_GPU_DISPLAYIDS as RawConversion>::Target>> {
        trace!("gpu.display_ids_connected({:?})", flags);
        let mut count = 0;
        sys::status_result(unsafe { display::NvAPI_GPU_GetConnectedDisplayIds(self.0, ptr::null_mut(), &mut count, flags.bits()) })?;

        let mut data = display::NV_GPU_DISPLAYIDS::zeroed();
        data.version = display::NV_GPU_DISPLAYIDS_VER;
        let mut data = vec![data; count as usize];

        sys::status_result(unsafe { display::NvAPI_GPU_GetConnectedDisplayIds(self.0, data.as_mut_ptr(), &mut count, flags.bits()) })
            .and_then(|_| data.into_iter().map(|v| v.convert_raw().map_err(From::from)).collect())
    }

    pub fn i2c_read(&self, display_mask: u32, port: Option<u8>, port_is_ddc: bool, address: u8, register: &[u8], bytes: &mut [u8], speed: i2c::I2cSpeed) -> sys::Result<usize> {
        trace!("i2c_read({}, {:?}, {:?}, 0x{:02x}, {:?}, {:?})", display_mask, port, port_is_ddc, address, register, speed);
        let mut data = i2c::NV_I2C_INFO::zeroed();
        data.version = i2c::NV_I2C_INFO_VER;
        data.displayMask = display_mask;
        data.bIsDDCPort = if port_is_ddc { sys::NV_TRUE } else { sys::NV_FALSE } as _;
        data.i2cDevAddress = address << 1;
        data.pbI2cRegAddress = if register.is_empty() { ptr::null_mut() } else { register.as_ptr() as *mut _ };
        data.regAddrSize = register.len() as _;
        data.pbData = bytes.as_mut_ptr();
        data.cbSize = bytes.len() as _;
        data.i2cSpeed = i2c::NVAPI_I2C_SPEED_DEPRECATED;
        data.i2cSpeedKhz = speed.raw();
        if let Some(port) = port {
            data.portId = port;
            data.bIsPortIdSet = sys::NV_TRUE as _;
        }

        sys::status_result(unsafe { i2c::NvAPI_I2CRead(self.0, &mut data) })
            .map(|_| data.cbSize as usize) // TODO: not actually sure if this ever changes?
    }

    pub fn i2c_write(&self, display_mask: u32, port: Option<u8>, port_is_ddc: bool, address: u8, register: &[u8], bytes: &[u8], speed: i2c::I2cSpeed) -> sys::Result<()> {
        trace!("i2c_write({}, {:?}, {:?}, 0x{:02x}, {:?}, {:?})", display_mask, port, port_is_ddc, address, register, speed);
        let mut data = i2c::NV_I2C_INFO::zeroed();
        data.version = i2c::NV_I2C_INFO_VER;
        data.displayMask = display_mask;
        data.bIsDDCPort = if port_is_ddc { sys::NV_TRUE } else { sys::NV_FALSE } as _;
        data.i2cDevAddress = address << 1;
        data.pbI2cRegAddress = if register.is_empty() { ptr::null_mut() } else { register.as_ptr() as *mut _ };
        data.regAddrSize = register.len() as _;
        data.pbData = bytes.as_ptr() as *mut _;
        data.cbSize = bytes.len() as _;
        data.i2cSpeed = i2c::NVAPI_I2C_SPEED_DEPRECATED;
        data.i2cSpeedKhz = speed.raw();
        if let Some(port) = port {
            data.portId = port;
            data.bIsPortIdSet = sys::NV_TRUE as _;
        }

        sys::status_result(unsafe { i2c::NvAPI_I2CWrite(self.0, &mut data) })
            .map(drop)
    }
}

#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, Default, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct PciIdentifiers {
    pub device_id: u32,
    pub subsystem_id: u32,
    pub revision_id: u32,
    pub ext_device_id: u32,
}

impl fmt::Display for PciIdentifiers {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:08x} - {:08x} - {:08x} - {:x}", self.device_id, self.subsystem_id, self.ext_device_id, self.revision_id)
    }
}

impl PciIdentifiers {
    pub fn vendor_id(&self) -> u16 {
        self.ids().0
    }

    pub fn product_id(&self) -> u16 {
        self.ids().1
    }

    pub fn ids(&self) -> (u16, u16) {
        let pid = (self.device_id >> 16) as u16;
        let vid = self.device_id as u16;
        if vid == 0x10de && self.subsystem_id != 0 {
            let spid = (self.subsystem_id >> 16) as u16;
            (
                self.subsystem_id as u16,
                if spid == 0 {
                    // Colorful and Inno3D
                    pid
                } else {
                    spid
                }
            )
        } else {
            (vid, pid)
        }
    }

    pub fn vendor(&self) -> Result<Vendor, sys::ArgumentRangeError> {
        Vendor::from_raw(self.vendor_id() as _)
    }
}

#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct MemoryInfo {
    pub dedicated: Kibibytes,
    pub dedicated_available: Kibibytes,
    pub system: Kibibytes,
    pub shared: Kibibytes,
    pub dedicated_available_current: Kibibytes,
    pub dedicated_evictions_size: Kibibytes,
    pub dedicated_evictions: u32,
}

impl RawConversion for driverapi::NV_DISPLAY_DRIVER_MEMORY_INFO {
    type Target = MemoryInfo;
    type Error = Void;

    fn convert_raw(&self) -> Result<Self::Target, Self::Error> {
        Ok(MemoryInfo {
            dedicated: Kibibytes(self.dedicatedVideoMemory),
            dedicated_available: Kibibytes(self.availableDedicatedVideoMemory),
            system: Kibibytes(self.systemVideoMemory),
            shared: Kibibytes(self.sharedSystemMemory),
            dedicated_available_current: Kibibytes(self.curAvailableDedicatedVideoMemory),
            dedicated_evictions_size: Kibibytes(self.dedicatedVideoMemoryEvictionsSize),
            dedicated_evictions: self.dedicatedVideoMemoryEvictionCount,
        })
    }
}

#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[derive(Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct DriverModel {
    pub value: u32,
}

impl DriverModel {
    pub fn new(value: u32) -> Self {
        DriverModel {
            value: value,
        }
    }

    pub fn wddm(&self) -> (u8, u8) {
        // 2.0 or 1.(value >> 8)
        let major = ((self.value >> 12) & 0xf) as u8;
        (
            major,
            if major == 2 { 0 } else { (self.value >> 8) as u8 & 0xf }
        )
    }
}

impl fmt::Display for DriverModel {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let wddm = self.wddm();
        write!(f, "WDDM {}.{:02}", wddm.0, wddm.1)
    }
}

impl fmt::Debug for DriverModel {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{} ({:08x})", self, self.value)
    }
}

#[cfg_attr(feature = "serde_derive", derive(Serialize, Deserialize))]
#[derive(Debug, Copy, Clone, PartialOrd, Ord, PartialEq, Eq, Hash)]
pub struct DisplayId {
    pub connector: MonitorConnectorType,
    pub display_id: u32,
    pub flags: DisplayIdsFlags,
}

impl RawConversion for display::NV_GPU_DISPLAYIDS {
    type Target = DisplayId;
    type Error = sys::ArgumentRangeError;

    fn convert_raw(&self) -> Result<Self::Target, Self::Error> {
        Ok(DisplayId {
            connector: MonitorConnectorType::from_raw(self.connectorType)?,
            display_id: self.displayId,
            flags: DisplayIdsFlags::from_bits_truncate(self.flags),
        })
    }
}