summaryrefslogtreecommitdiffstats
path: root/private/ntos/nthals/halalpha/perf8254.c
blob: 7a216040815dd008687092bd19fd4d01dd48dfbc (plain) (blame)
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
/*++

Copyright (c) 1992, 1993  Digital Equipment Corporation

Module Name:

    perf82454.c

Abstract:

    This module implements the interfaces that access the system
    performance counter and the calibrated stall for systems that
    use an external 8254 timer chip to implement the performance counter.
    This module is suitable for use in multiprocessor systems.

Author:

    Joe Notarangelo  14-Mar-1994

Environment:

    Kernel mode

Revision History:

--*/

#include "halp.h"
#include "eisa.h"

//
// Declare the global that contains the current value of the performance
// counter.
//

ULONG HalpLastTimer;
ULONGLONG HalpTimerWrapCount;

//
// Declare globals used to control the access to and initialization of
// the performance counter.
//

BOOLEAN HalpPerformanceCounterInitialized = FALSE;
ULONG HalpPerformanceCounterFrequency;
KSPIN_LOCK HalpPerformanceCounterSpinLock;

#define TIMER_START_VALUE (0xffff)

//
// Define local routine prototypes.
//

VOID
HalpStartTimer(
    VOID
    );

ULONG
HalpReadTimerValue(
    VOID
    );

VOID
HalCalibratePerformanceCounter (
    IN volatile PLONG Number
    )

/*++

Routine Description:

    This routine is responsible for synchronizing the performance
    counter across all processors in the system configuration.
    For an 8254-based performance counter all that is necessary is that
    the counter be initialized.

Arguments:

    Number - Supplies a pointer to count of the number of processors in
    the configuration.

Return Value:

    None.

--*/

{
    PKPRCB Prcb = PCR->Prcb;

    //
    // If this isn't the primary processor, then return.
    //

    if( Prcb->Number != HAL_PRIMARY_PROCESSOR ){
        return;
    }

    //
    // If the counter has already been initialized then simply return.
    //

    if( HalpPerformanceCounterInitialized == TRUE ){
        return;
    }

    //
    // Initialize the spinlock for controlling access to the counter.
    //

    KeInitializeSpinLock( &HalpPerformanceCounterSpinLock );

    //
    // Set the frequency of the counter.
    //

    HalpPerformanceCounterFrequency = TIMER_CLOCK_IN;

    //
    // Initialize the wrap count.
    //

    HalpTimerWrapCount = 0;

    //
    // Initialize the counter and start it.
    //

    HalpStartTimer();

    HalpLastTimer = HalpReadTimerValue();

    //
    // Mark the counter as initialized.
    //

    HalpPerformanceCounterInitialized = TRUE;

}


LARGE_INTEGER
KeQueryPerformanceCounter (
    OUT PLARGE_INTEGER Frequency OPTIONAL
    )

/*++

Routine Description:

    This routine returns the current performance counter value and the
    performance counter frequency.

Arguments:

    Frequency - Supplies an optional pointer to a variable which receives
        the performance counter frequency in Hertz.

Return Value:

    The current performance counter value is returned as the function
    value.

--*/

{

    LARGE_INTEGER LocalPerformanceCount;
    KIRQL OldIrql;
    ULONG TimerValue;

    //
    // Return 0 if the performance counter has not been initialized yet.
    //

    if( HalpPerformanceCounterInitialized == FALSE ){
        LocalPerformanceCount.QuadPart = 0;
        return LocalPerformanceCount;
    }

    //
    // Synchronize the calculation of the performance counter with the
    // clock routine executing on the boot processor.
    //

    KeRaiseIrql( CLOCK_LEVEL, &OldIrql );
    KiAcquireSpinLock( &HalpPerformanceCounterSpinLock );

    //
    // Read the current value of the timer count.
    //

    TimerValue = HalpReadTimerValue();

    //
    // If the timer is greater than the last timer value then the timer
    // has wrapped since the last time we have read it.
    //

    if( TimerValue > HalpLastTimer ){

        HalpTimerWrapCount += (1 << 15);
    }

    HalpLastTimer = TimerValue;

    LocalPerformanceCount.QuadPart = HalpTimerWrapCount + 
                                     (TIMER_START_VALUE - TimerValue)/2;

    //
    // Once the value is calculated synchronization is no longer
    // required.
    //

    KiReleaseSpinLock( &HalpPerformanceCounterSpinLock );
    KeLowerIrql( OldIrql );

    //
    // If the frequency parameter is specified, then return the frequency
    // of the 8254 counter.
    //

    if (ARGUMENT_PRESENT(Frequency) != FALSE) {
        Frequency->LowPart = HalpPerformanceCounterFrequency;
        Frequency->HighPart = 0;
    }

    //
    // Return the calculated performance count.
    //

    return LocalPerformanceCount;

}


VOID
HalpCheckPerformanceCounter(
    VOID
    )
/*++

Routine Description:

    This function is called every system clock interrupt in order to
    check for wrap of the performance counter.  The function must handle
    a wrap if it is detected.

    N.B. - This function must be called at CLOCK_LEVEL.

Arguments:

    None.

Return Value:

    None.

--*/
{

    ULONG TimerValue;

    //
    // Synchronize the performance counter check with any possible
    // readers.
    //

    KiAcquireSpinLock( &HalpPerformanceCounterSpinLock );

    //
    // Read the current value of the timer count.
    //

    TimerValue = HalpReadTimerValue();

    //
    // If the timer is greater than the last timer value then the timer
    // has wrapped since the last time we have read it.
    //

    if( TimerValue > HalpLastTimer ){

        HalpTimerWrapCount += (1 << 15);
    }

    HalpLastTimer = TimerValue;

    KiReleaseSpinLock( &HalpPerformanceCounterSpinLock );

    return;

}


VOID
KeStallExecutionProcessor (
    IN ULONG MicroSeconds
    )

/*++

Routine Description:

    This function stalll execution of the current processor for the specified
    number of microseconds.

Arguments:

    MicroSeconds - Supplies the number of microseconds that execution is to
                   be stalled.

Return Value:

    None.

--*/

{

    LONG StallCyclesRemaining;               // signed value
    ULONG PreviousRpcc, CurrentRpcc;

    //
    // Get the value of the RPCC as soon as we enter
    //

    PreviousRpcc = HalpRpcc();

    //
    // Compute the number of cycles to stall
    //

    StallCyclesRemaining = MicroSeconds * HalpClockMegaHertz;

    //
    // Wait while there are stall cycles remaining.
    // The accuracy of this routine is limited by the
    // length of this while loop.
    //

    while (StallCyclesRemaining > 0) {

        CurrentRpcc = HalpRpcc();

        //
        // The subtraction always works because the Rpcc
        // is a wrapping long-word.  If it wraps, we still
        // get the positive number we want.
        //

        StallCyclesRemaining -= (CurrentRpcc - PreviousRpcc);

        //
        // remember this RPCC value
        //

        PreviousRpcc = CurrentRpcc;
    }

}


VOID
HalpStartTimer(
    VOID
    )
/*++

Routine Description:

    Start the timer used to maintain the performance count.  The timer used
    is counter 0 in timer 1 - it is an 8254-compatible timer.

Arguments:

    None.

Return Value:

    None.

--*/
{
    TIMER_CONTROL TimerControl;

    //
    // Set the timer for counter 0 in binary mode.  Write the counter
    // with the LSB then MSB of the TIMER_START_VALUE.
    //

    TimerControl.BcdMode = 0;
    TimerControl.Mode = TM_SQUARE_WAVE;
    TimerControl.SelectByte = SB_LSB_THEN_MSB;
    TimerControl.SelectCounter = SELECT_COUNTER_0;

    WRITE_PORT_UCHAR( &((PEISA_CONTROL)HalpEisaControlBase)->CommandMode1,
                      *(PUCHAR)&TimerControl );

    WRITE_PORT_UCHAR( &((PEISA_CONTROL)HalpEisaControlBase)->Timer1,
                      TIMER_START_VALUE & 0xff );

    WRITE_PORT_UCHAR( &((PEISA_CONTROL)HalpEisaControlBase)->Timer1,
                      (TIMER_START_VALUE >> 8) & 0xff );


    return;

}


ULONG
HalpReadTimerValue(
    VOID
    )
/*++

Routine Description:

    Read the current value from the timer used to maintain the performance
    count.  The timer used is counter 0 in timer - it is an 8254-compatible
    timer.

Arguments:

    None.

Return Value:

    The current count value of the timer is returned.

--*/
{
    UCHAR Lsb;
    UCHAR Msb;
    TIMER_CONTROL TimerControl;

    //
    // Set the counter for a latched read, read it Lsb then Msb.
    //

    TimerControl.BcdMode = 0;
    TimerControl.Mode = 0;
    TimerControl.SelectByte = SB_COUNTER_LATCH;
    TimerControl.SelectCounter = SELECT_COUNTER_0;

    WRITE_PORT_UCHAR( &((PEISA_CONTROL)HalpEisaControlBase)->CommandMode1,
                      *(PUCHAR)&TimerControl );

    Lsb = READ_PORT_UCHAR( &((PEISA_CONTROL)HalpEisaControlBase)->Timer1 );

    Msb = READ_PORT_UCHAR( &((PEISA_CONTROL)HalpEisaControlBase)->Timer1 );

    return (ULONG)( (Msb << 8) | Lsb );

}