Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pmt.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 /*
24  * Timer implementation using ACPI PM timer
25  */
26 
27 #include "precompiled.h"
29 
31 
32 #include "lib/sysdep/os/win/win.h"
33 #include "lib/sysdep/acpi.h"
35 #include "lib/bits.h"
36 
37 static const u32 TMR_VAL_EXT = Bit<u32>(8); // FADT flags
38 
39 //-----------------------------------------------------------------------------
40 
41 
42 class CounterPMT : public ICounter
43 {
44 public:
46  : m_portAddress(0xFFFF)
47  {
48  }
49 
50  virtual const wchar_t* Name() const
51  {
52  return L"PMT";
53  }
54 
56  {
57  // mahaf is needed for port I/O.
58  RETURN_STATUS_IF_ERR(mahaf_Init()); // (fails without Administrator privileges)
59  // (note: it's called FADT, but the signature is "FACP")
60  const FADT* fadt = (const FADT*)acpi_GetTable("FACP");
61  if(!fadt)
62  return ERR::NOT_SUPPORTED; // NOWARN (ACPI tables might not be available)
64 
65  return INFO::OK;
66  }
67 
68  void Shutdown()
69  {
70  }
71 
72  bool IsSafe() const
73  {
74  // the PMT has one issue: "Performance counter value may unexpectedly
75  // leap forward" (Q274323). This happens on some buggy Pentium-era
76  // systems under heavy PCI bus load. We are clever and observe that
77  // the TSC implementation would be used on such systems (because it
78  // has higher precedence and is safe on P5 CPUs), so the PMT is fine
79  // in general.
80  return true;
81  }
82 
83  u64 Counter() const
84  {
86  }
87 
88  size_t CounterBits() const
89  {
90  // (see previous acpi_GetTable call)
91  const FADT* fadt = (const FADT*)acpi_GetTable("FACP");
92  ENSURE(fadt); // Activate made sure FADT is available
93  const size_t counterBits = (fadt->flags & TMR_VAL_EXT)? 32 : 24;
94  return counterBits;
95  }
96 
97  double NominalFrequency() const
98  {
99  return (double)PMT_FREQ;
100  }
101 
102  double Resolution() const
103  {
104  return 1.0 / PMT_FREQ;
105  }
106 
107 private:
109 };
110 
111 ICounter* CreateCounterPMT(void* address, size_t size)
112 {
113  ENSURE(sizeof(CounterPMT) <= size);
114  return new(address) CounterPMT();
115 }
ICounter * CreateCounterPMT(void *address, size_t size)
Definition: pmt.cpp:111
static const i64 PMT_FREQ
Definition: pmt.h:30
u32 mahaf_ReadPort32(u16 port)
Definition: mahaf.cpp:75
Status mahaf_Init()
Definition: mahaf.cpp:390
const Status OK
Definition: status.h:386
u16 m_portAddress
Definition: pmt.cpp:108
const AcpiTable * acpi_GetTable(const char *signature)
Definition: acpi.cpp:362
Status Activate()
Definition: pmt.cpp:55
CounterPMT()
Definition: pmt.cpp:45
const Status NOT_SUPPORTED
Definition: status.h:429
u32 flags
Definition: acpi.h:76
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
u32 pmTimerPortAddress
Definition: acpi.h:69
static size_t counterBits
Definition: whrt.cpp:100
double NominalFrequency() const
initial measurement of the tick rate.
Definition: pmt.cpp:97
double Resolution() const
actual resolution [s].
Definition: pmt.cpp:102
i64 Status
Error handling system.
Definition: status.h:171
virtual const wchar_t * Name() const
Definition: pmt.cpp:50
bool IsSafe() const
Definition: pmt.cpp:72
#define u16
Definition: types.h:40
#define u64
Definition: types.h:42
size_t CounterBits() const
Definition: pmt.cpp:88
#define u32
Definition: types.h:41
u64 Counter() const
Definition: pmt.cpp:83
Definition: acpi.h:65
void Shutdown()
Definition: pmt.cpp:68
u16 u16_from_larger(T x)
Definition: lib.h:146
#define RETURN_STATUS_IF_ERR(expression)
Definition: status.h:276
static const u32 TMR_VAL_EXT
Definition: pmt.cpp:37