Fixed a problem where the ADL Main_Memory_Alloc callback delegate could be garbage collected while still in use.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
9 http://www.mozilla.org/MPL/
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.
15 The Original Code is the Open Hardware Monitor code.
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.
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.
39 using System.Globalization;
41 using System.Threading;
43 namespace OpenHardwareMonitor.Hardware.CPU {
45 internal sealed class AMD10CPU : AMDCPU {
47 private readonly Sensor coreTemperature;
48 private readonly Sensor[] coreClocks;
49 private readonly Sensor busClock;
51 private const uint COFVID_STATUS = 0xC0010071;
52 private const uint P_STATE_0 = 0xC0010064;
54 private const byte MISCELLANEOUS_CONTROL_FUNCTION = 3;
55 private const ushort MISCELLANEOUS_CONTROL_DEVICE_ID = 0x1203;
56 private const uint REPORTED_TEMPERATURE_CONTROL_REGISTER = 0xA4;
58 private readonly uint miscellaneousControlAddress;
60 public AMD10CPU(int processorIndex, CPUID[][] cpuid, ISettings settings)
61 : base(processorIndex, cpuid, settings)
63 // AMD family 10h processors support only one temperature sensor
64 coreTemperature = new Sensor(
65 "Core" + (coreCount > 1 ? " #1 - #" + coreCount : ""), 0,
66 SensorType.Temperature, this, new [] {
67 new ParameterDescription("Offset [°C]", "Temperature offset.", 0)
70 // get the pci address for the Miscellaneous Control registers
71 miscellaneousControlAddress = GetPciAddress(
72 MISCELLANEOUS_CONTROL_FUNCTION, MISCELLANEOUS_CONTROL_DEVICE_ID);
74 busClock = new Sensor("Bus Speed", 0, SensorType.Clock, this, settings);
75 coreClocks = new Sensor[coreCount];
76 for (int i = 0; i < coreClocks.Length; i++) {
77 coreClocks[i] = new Sensor(CoreString(i), i + 1, SensorType.Clock,
80 ActivateSensor(coreClocks[i]);
86 protected override uint[] GetMSRs() {
87 return new uint[] { P_STATE_0, COFVID_STATUS };
90 public override string GetReport() {
91 StringBuilder r = new StringBuilder();
92 r.Append(base.GetReport());
94 r.Append("Miscellaneous Control Address: 0x");
95 r.AppendLine((miscellaneousControlAddress).ToString("X",
96 CultureInfo.InvariantCulture));
102 private double MultiplierFromIDs(uint divisorID, uint frequencyID) {
103 return 0.5 * (frequencyID + 0x10) / (1 << (int)divisorID);
106 public override void Update() {
109 if (miscellaneousControlAddress != WinRing0.InvalidPciAddress) {
111 if (WinRing0.ReadPciConfigDwordEx(miscellaneousControlAddress,
112 REPORTED_TEMPERATURE_CONTROL_REGISTER, out value)) {
113 coreTemperature.Value = ((value >> 21) & 0x7FF) / 8.0f +
114 coreTemperature.Parameters[0].Value;
115 ActivateSensor(coreTemperature);
117 DeactivateSensor(coreTemperature);
122 double newBusClock = 0;
124 for (int i = 0; i < coreClocks.Length; i++) {
129 if (WinRing0.RdmsrTx(COFVID_STATUS, out curEax, out curEdx,
130 (UIntPtr)(1L << cpuid[i][0].Thread)) &&
131 WinRing0.RdmsrTx(P_STATE_0, out maxEax, out maxEdx,
132 (UIntPtr)(1L << cpuid[i][0].Thread)))
134 // 8:6 CpuDid: current core divisor ID
135 // 5:0 CpuFid: current core frequency ID
136 uint curCpuDid = (curEax >> 6) & 7;
137 uint curCpuFid = curEax & 0x1F;
138 double multiplier = MultiplierFromIDs(curCpuDid, curCpuFid);
140 // we assume that the max multiplier (used for the TSC)
141 // can be found in the P_STATE_0 MSR
142 uint maxCpuDid = (maxEax >> 6) & 7;
143 uint maxCpuFid = maxEax & 0x1F;
144 double maxMultiplier = MultiplierFromIDs(maxCpuDid, maxCpuFid);
146 coreClocks[i].Value = (float)(multiplier * MaxClock / maxMultiplier);
147 newBusClock = (float)(MaxClock / maxMultiplier);
149 coreClocks[i].Value = (float)MaxClock;
153 if (newBusClock > 0) {
154 this.busClock.Value = (float)newBusClock;
155 ActivateSensor(this.busClock);