xref: /reactos/hal/halx86/include/halp.h (revision d6eebaa4)
1 /*
2  *
3  */
4 
5 #pragma once
6 
7 #ifdef CONFIG_SMP
8 #define HAL_BUILD_TYPE (DBG ? PRCB_BUILD_DEBUG : 0)
9 #else
10 #define HAL_BUILD_TYPE ((DBG ? PRCB_BUILD_DEBUG : 0) | PRCB_BUILD_UNIPROCESSOR)
11 #endif
12 
13 /* Don't include this in freeloader */
14 #ifndef _BLDR_
15 extern KIRQL HalpIrqlSynchLevel;
16 
17 #undef SYNCH_LEVEL
18 #define SYNCH_LEVEL HalpIrqlSynchLevel
19 #endif
20 
21 typedef struct _HAL_BIOS_FRAME
22 {
23     ULONG SegSs;
24     ULONG Esp;
25     ULONG EFlags;
26     ULONG SegCs;
27     ULONG Eip;
28     PKTRAP_FRAME TrapFrame;
29     ULONG CsLimit;
30     ULONG CsBase;
31     ULONG CsFlags;
32     ULONG SsLimit;
33     ULONG SsBase;
34     ULONG SsFlags;
35     ULONG Prefix;
36 } HAL_BIOS_FRAME, *PHAL_BIOS_FRAME;
37 
38 typedef
39 VOID
40 (__cdecl *PHAL_SW_INTERRUPT_HANDLER)(
41     VOID
42 );
43 
44 typedef
45 VOID
46 (FASTCALL *PHAL_SW_INTERRUPT_HANDLER_2ND_ENTRY)(
47     IN PKTRAP_FRAME TrapFrame
48 );
49 
50 #define HAL_APC_REQUEST         0
51 #define HAL_DPC_REQUEST         1
52 
53 /* HAL profiling offsets in KeGetPcr()->HalReserved[] */
54 #define HAL_PROFILING_INTERVAL      0
55 #define HAL_PROFILING_MULTIPLIER    1
56 
57 /* Usage flags */
58 #define IDT_REGISTERED          0x01
59 #define IDT_LATCHED             0x02
60 #define IDT_READ_ONLY           0x04
61 #define IDT_INTERNAL            0x11
62 #define IDT_DEVICE              0x21
63 
64 #ifdef _M_AMD64
65 #define HALP_LOW_STUB_SIZE_IN_PAGES 5
66 #else
67 #define HALP_LOW_STUB_SIZE_IN_PAGES 3
68 #endif
69 
70 /* Conversion functions */
71 #define BCD_INT(bcd)            \
72     (((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F))
73 #define INT_BCD(int)            \
74     (UCHAR)(((int / 10) << 4) + (int % 10))
75 
76 typedef
77 BOOLEAN
78 (NTAPI *PHAL_DISMISS_INTERRUPT)(
79     IN KIRQL Irql,
80     IN ULONG Irq,
81     OUT PKIRQL OldIrql
82 );
83 
84 BOOLEAN
85 NTAPI
86 HalpDismissIrqGeneric(
87     IN KIRQL Irql,
88     IN ULONG Irq,
89     OUT PKIRQL OldIrql
90 );
91 
92 BOOLEAN
93 NTAPI
94 HalpDismissIrq15(
95     IN KIRQL Irql,
96     IN ULONG Irq,
97     OUT PKIRQL OldIrql
98 );
99 
100 BOOLEAN
101 NTAPI
102 HalpDismissIrq13(
103     IN KIRQL Irql,
104     IN ULONG Irq,
105     OUT PKIRQL OldIrql
106 );
107 
108 BOOLEAN
109 NTAPI
110 HalpDismissIrq07(
111     IN KIRQL Irql,
112     IN ULONG Irq,
113     OUT PKIRQL OldIrql
114 );
115 
116 BOOLEAN
117 NTAPI
118 HalpDismissIrqLevel(
119     IN KIRQL Irql,
120     IN ULONG Irq,
121     OUT PKIRQL OldIrql
122 );
123 
124 BOOLEAN
125 NTAPI
126 HalpDismissIrq15Level(
127     IN KIRQL Irql,
128     IN ULONG Irq,
129     OUT PKIRQL OldIrql
130 );
131 
132 BOOLEAN
133 NTAPI
134 HalpDismissIrq13Level(
135     IN KIRQL Irql,
136     IN ULONG Irq,
137     OUT PKIRQL OldIrql
138 );
139 
140 BOOLEAN
141 NTAPI
142 HalpDismissIrq07Level(
143     IN KIRQL Irql,
144     IN ULONG Irq,
145     OUT PKIRQL OldIrql
146 );
147 
148 VOID
149 __cdecl
150 HalpHardwareInterruptLevel(
151     VOID
152 );
153 
154 //
155 // Hack Flags
156 //
157 #define HALP_REVISION_FROM_HACK_FLAGS(x)    ((x) >> 24)
158 #define HALP_REVISION_HACK_FLAGS(x)         ((x) >> 12)
159 #define HALP_HACK_FLAGS(x)                  ((x) & 0xFFF)
160 
161 //
162 // Feature flags
163 //
164 #define HALP_CARD_FEATURE_FULL_DECODE   0x0001
165 
166 //
167 // Match Flags
168 //
169 #define HALP_CHECK_CARD_REVISION_ID     0x10000
170 #define HALP_CHECK_CARD_SUBVENDOR_ID    0x20000
171 #define HALP_CHECK_CARD_SUBSYSTEM_ID    0x40000
172 
173 //
174 // Mm PTE/PDE to Hal PTE/PDE
175 //
176 #define HalAddressToPde(x) (PHARDWARE_PTE)MiAddressToPde(x)
177 #define HalAddressToPte(x) (PHARDWARE_PTE)MiAddressToPte(x)
178 
179 typedef struct _IDTUsageFlags
180 {
181     UCHAR Flags;
182 } IDTUsageFlags;
183 
184 typedef struct
185 {
186     KIRQL Irql;
187     UCHAR BusReleativeVector;
188 } IDTUsage;
189 
190 typedef struct _HalAddressUsage
191 {
192     struct _HalAddressUsage *Next;
193     CM_RESOURCE_TYPE Type;
194     UCHAR Flags;
195     struct
196     {
197         ULONG Start;
198         ULONG Length;
199     } Element[];
200 } ADDRESS_USAGE, *PADDRESS_USAGE;
201 
202 /* adapter.c */
203 PADAPTER_OBJECT NTAPI HalpAllocateAdapterEx(ULONG NumberOfMapRegisters,BOOLEAN IsMaster, BOOLEAN Dma32BitAddresses);
204 
205 /* sysinfo.c */
206 CODE_SEG("INIT")
207 VOID
208 NTAPI
209 HalpRegisterVector(IN UCHAR Flags,
210                    IN ULONG BusVector,
211                    IN ULONG SystemVector,
212                    IN KIRQL Irql);
213 
214 CODE_SEG("INIT")
215 VOID
216 NTAPI
217 HalpEnableInterruptHandler(IN UCHAR Flags,
218                            IN ULONG BusVector,
219                            IN ULONG SystemVector,
220                            IN KIRQL Irql,
221                            IN PVOID Handler,
222                            IN KINTERRUPT_MODE Mode);
223 
224 /* pic.c */
225 VOID NTAPI HalpInitializePICs(IN BOOLEAN EnableInterrupts);
226 VOID __cdecl HalpApcInterrupt(VOID);
227 VOID __cdecl HalpDispatchInterrupt(VOID);
228 PHAL_SW_INTERRUPT_HANDLER __cdecl HalpDispatchInterrupt2(VOID);
229 DECLSPEC_NORETURN VOID FASTCALL HalpApcInterrupt2ndEntry(IN PKTRAP_FRAME TrapFrame);
230 DECLSPEC_NORETURN VOID FASTCALL HalpDispatchInterrupt2ndEntry(IN PKTRAP_FRAME TrapFrame);
231 
232 /* profil.c */
233 extern BOOLEAN HalpProfilingStopped;
234 
235 /* timer.c */
236 CODE_SEG("INIT") VOID NTAPI HalpInitializeClock(VOID);
237 VOID __cdecl HalpClockInterrupt(VOID);
238 VOID __cdecl HalpClockIpi(VOID);
239 VOID __cdecl HalpProfileInterrupt(VOID);
240 
241 typedef struct _HALP_ROLLOVER
242 {
243     ULONG RollOver;
244     ULONG Increment;
245 } HALP_ROLLOVER, *PHALP_ROLLOVER;
246 
247 VOID
248 NTAPI
249 HalpCalibrateStallExecution(VOID);
250 
251 /* pci.c */
252 VOID HalpInitPciBus (VOID);
253 
254 /* dma.c */
255 CODE_SEG("INIT") VOID HalpInitDma (VOID);
256 
257 /* Non-generic initialization */
258 VOID HalpInitPhase0 (PLOADER_PARAMETER_BLOCK LoaderBlock);
259 VOID HalpInitPhase1(VOID);
260 
261 VOID
262 NTAPI
263 HalpFlushTLB(VOID);
264 
265 //
266 // KD Support
267 //
268 VOID
269 NTAPI
270 HalpCheckPowerButton(
271     VOID
272 );
273 
274 CODE_SEG("INIT")
275 VOID
276 NTAPI
277 HalpRegisterKdSupportFunctions(
278     VOID
279 );
280 
281 CODE_SEG("INIT")
282 NTSTATUS
283 NTAPI
284 HalpSetupPciDeviceForDebugging(
285     IN PVOID LoaderBlock,
286     IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice
287 );
288 
289 CODE_SEG("INIT")
290 NTSTATUS
291 NTAPI
292 HalpReleasePciDeviceForDebugging(
293     IN OUT PDEBUG_DEVICE_DESCRIPTOR PciDevice
294 );
295 
296 //
297 // Memory routines
298 //
299 ULONG64
300 NTAPI
301 HalpAllocPhysicalMemory(
302     IN PLOADER_PARAMETER_BLOCK LoaderBlock,
303     IN ULONG64 MaxAddress,
304     IN PFN_NUMBER PageCount,
305     IN BOOLEAN Aligned
306 );
307 
308 PVOID
309 NTAPI
310 HalpMapPhysicalMemory64Vista(
311     IN PHYSICAL_ADDRESS PhysicalAddress,
312     IN PFN_COUNT PageCount,
313     IN BOOLEAN FlushCurrentTLB
314 );
315 
316 VOID
317 NTAPI
318 HalpUnmapVirtualAddressVista(
319     IN PVOID VirtualAddress,
320     IN PFN_COUNT NumberPages,
321     IN BOOLEAN FlushCurrentTLB
322 );
323 
324 PVOID
325 NTAPI
326 HalpMapPhysicalMemory64(
327     IN PHYSICAL_ADDRESS PhysicalAddress,
328     IN PFN_COUNT PageCount
329 );
330 
331 VOID
332 NTAPI
333 HalpUnmapVirtualAddress(
334     IN PVOID VirtualAddress,
335     IN PFN_COUNT NumberPages
336 );
337 
338 /* sysinfo.c */
339 NTSTATUS
340 NTAPI
341 HaliHandlePCIConfigSpaceAccess(
342     _In_ BOOLEAN IsRead,
343     _In_ ULONG Port,
344     _In_ ULONG Length,
345     _Inout_ PULONG Buffer
346 );
347 
348 NTSTATUS
349 NTAPI
350 HaliQuerySystemInformation(
351     IN HAL_QUERY_INFORMATION_CLASS InformationClass,
352     IN ULONG BufferSize,
353     IN OUT PVOID Buffer,
354     OUT PULONG ReturnedLength
355 );
356 
357 NTSTATUS
358 NTAPI
359 HaliSetSystemInformation(
360     IN HAL_SET_INFORMATION_CLASS InformationClass,
361     IN ULONG BufferSize,
362     IN OUT PVOID Buffer
363 );
364 
365 //
366 // BIOS Routines
367 //
368 BOOLEAN
369 NTAPI
370 HalpBiosDisplayReset(
371     VOID
372 );
373 
374 VOID
375 FASTCALL
376 HalpExitToV86(
377     PKTRAP_FRAME TrapFrame
378 );
379 
380 VOID
381 __cdecl
382 HalpRealModeStart(
383     VOID
384 );
385 
386 //
387 // Processor Halt Routine
388 //
389 VOID
390 NTAPI
391 HaliHaltSystem(
392     VOID
393 );
394 
395 //
396 // CMOS Routines
397 //
398 CODE_SEG("INIT")
399 VOID
400 NTAPI
401 HalpInitializeCmos(
402     VOID
403 );
404 
405 _Requires_lock_held_(HalpSystemHardwareLock)
406 UCHAR
407 NTAPI
408 HalpReadCmos(
409     IN UCHAR Reg
410 );
411 
412 _Requires_lock_held_(HalpSystemHardwareLock)
413 VOID
414 NTAPI
415 HalpWriteCmos(
416     IN UCHAR Reg,
417     IN UCHAR Value
418 );
419 
420 //
421 // Spinlock for protecting CMOS access
422 //
423 _Acquires_lock_(HalpSystemHardwareLock)
424 VOID
425 NTAPI
426 HalpAcquireCmosSpinLock(
427     VOID
428 );
429 
430 _Releases_lock_(HalpSystemHardwareLock)
431 VOID
432 NTAPI
433 HalpReleaseCmosSpinLock(
434     VOID
435 );
436 
437 VOID
438 NTAPI
439 HalpInitializeLegacyPICs(
440     VOID
441 );
442 
443 NTSTATUS
444 NTAPI
445 HalpOpenRegistryKey(
446     IN PHANDLE KeyHandle,
447     IN HANDLE RootKey,
448     IN PUNICODE_STRING KeyName,
449     IN ACCESS_MASK DesiredAccess,
450     IN BOOLEAN Create
451 );
452 
453 CODE_SEG("INIT")
454 VOID
455 NTAPI
456 HalpGetNMICrashFlag(
457     VOID
458 );
459 
460 CODE_SEG("INIT")
461 BOOLEAN
462 NTAPI
463 HalpGetDebugPortTable(
464     VOID
465 );
466 
467 CODE_SEG("INIT")
468 VOID
469 NTAPI
470 HalpReportSerialNumber(
471     VOID
472 );
473 
474 CODE_SEG("INIT")
475 NTSTATUS
476 NTAPI
477 HalpMarkAcpiHal(
478     VOID
479 );
480 
481 CODE_SEG("INIT")
482 VOID
483 NTAPI
484 HalpBuildAddressMap(
485     VOID
486 );
487 
488 CODE_SEG("INIT")
489 VOID
490 NTAPI
491 HalpReportResourceUsage(
492     IN PUNICODE_STRING HalName,
493     IN INTERFACE_TYPE InterfaceType
494 );
495 
496 CODE_SEG("INIT")
497 ULONG
498 NTAPI
499 HalpIs16BitPortDecodeSupported(
500     VOID
501 );
502 
503 NTSTATUS
504 NTAPI
505 HalpQueryAcpiResourceRequirements(
506     OUT PIO_RESOURCE_REQUIREMENTS_LIST *Requirements
507 );
508 
509 VOID
510 FASTCALL
511 KeUpdateSystemTime(
512     IN PKTRAP_FRAME TrapFrame,
513     IN ULONG Increment,
514     IN KIRQL OldIrql
515 );
516 
517 VOID
518 NTAPI
519 KeUpdateRunTime(
520     _In_ PKTRAP_FRAME TrapFrame,
521     _In_ KIRQL Irql);
522 
523 CODE_SEG("INIT")
524 VOID
525 NTAPI
526 HalpInitBusHandlers(
527     VOID
528 );
529 
530 NTSTATUS
531 NTAPI
532 HaliInitPnpDriver(
533     VOID
534 );
535 
536 CODE_SEG("INIT")
537 VOID
538 NTAPI
539 HalpDebugPciDumpBus(
540     IN PBUS_HANDLER BusHandler,
541     IN PCI_SLOT_NUMBER PciSlot,
542     IN ULONG i,
543     IN ULONG j,
544     IN ULONG k,
545     IN PPCI_COMMON_CONFIG PciData
546 );
547 
548 VOID
549 NTAPI
550 HalpInitProcessor(
551     IN ULONG ProcessorNumber,
552     IN PLOADER_PARAMETER_BLOCK LoaderBlock
553 );
554 
555 #if defined(SARCH_PC98)
556 BOOLEAN
557 NTAPI
558 HalpDismissIrq08(
559     _In_ KIRQL Irql,
560     _In_ ULONG Irq,
561     _Out_ PKIRQL OldIrql
562 );
563 
564 BOOLEAN
565 NTAPI
566 HalpDismissIrq08Level(
567     _In_ KIRQL Irql,
568     _In_ ULONG Irq,
569     _Out_ PKIRQL OldIrql
570 );
571 
572 VOID
573 NTAPI
574 HalpInitializeClockPc98(VOID);
575 
576 extern ULONG PIT_FREQUENCY;
577 #endif /* SARCH_PC98 */
578 
579 VOID
580 NTAPI
581 HalInitializeBios(
582     _In_ ULONG Phase,
583     _In_ PLOADER_PARAMETER_BLOCK LoaderBlock
584 );
585 
586 #ifdef _M_AMD64
587 #define KfLowerIrql KeLowerIrql
588 #define KiEnterInterruptTrap(TrapFrame) /* We do all neccessary in asm code */
589 #define KiEoiHelper(TrapFrame) return /* Just return to the caller */
590 #define HalBeginSystemInterrupt(Irql, Vector, OldIrql) ((*(OldIrql) = PASSIVE_LEVEL), TRUE)
591 #endif // _M_AMD64
592 
593 extern BOOLEAN HalpNMIInProgress;
594 
595 extern ADDRESS_USAGE HalpDefaultIoSpace;
596 
597 extern KSPIN_LOCK HalpSystemHardwareLock;
598 
599 extern PADDRESS_USAGE HalpAddressUsageList;
600 
601 extern LARGE_INTEGER HalpPerfCounter;
602 
603 extern KAFFINITY HalpActiveProcessors;
604 
605 extern BOOLEAN HalDisableFirmwareMapper;
606 extern PWCHAR HalHardwareIdString;
607 extern PWCHAR HalName;
608 
609 extern KAFFINITY HalpDefaultInterruptAffinity;
610 
611 extern IDTUsageFlags HalpIDTUsageFlags[MAXIMUM_IDTVECTOR+1];
612 
613 extern const USHORT HalpBuildType;
614