This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
PL011device.cpp
1396 lines (1167 loc) · 39.2 KB
/
PL011device.cpp
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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Module Name:
//
// PL011device.cpp
//
// Abstract:
//
// This module contains the ARM PL011 UART controller's PNP device functions.
// This controller driver uses the Serial WDF class extension (SerCx2).
//
// Environment:
//
// kernel-mode only
//
#include "precomp.h"
#pragma hdrstop
#define _PL011_DEVICE_CPP_
// Logging header files
#include "PL011logging.h"
#include "PL011device.tmh"
// Common driver header files
#include "PL011common.h"
// Module specific header files
#include "PL011rx.h"
#include "PL011tx.h"
#include "PL011interrupt.h"
#include "PL011driver.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, PL011EvtDeviceAdd)
#pragma alloc_text(PAGE, PL011EvtDevicePrepareHardware)
#pragma alloc_text(PAGE, PL011EvtDeviceReleaseHardware)
#pragma alloc_text(PAGE, PL011EvtSerCx2ApplyConfig)
#pragma alloc_text(PAGE, PL011pDeviceExtensionInit)
#pragma alloc_text(PAGE, PL011pDeviceSerCx2Init)
#pragma alloc_text(PAGE, PL011pDeviceParseResources)
#pragma alloc_text(PAGE, PL011pDeviceMapResources)
#pragma alloc_text(PAGE, PL011pDeviceCreateDeviceInterface)
#pragma alloc_text(PAGE, PL011pDeviceReserveFunctionConfigResource)
#pragma alloc_text(PAGE, PL011pDeviceGetSupportedFeatures)
#endif // ALLOC_PRAGMA
//
// This is exported from the kernel. It is used to point
// to the address that the kernel debugger is using.
//
extern "C" extern PUCHAR* KdComPortInUse;
//
// Routine Description:
//
// PL011EvtDeviceAdd is called by the framework in response to AddDevice
// call from the PnP manager.
// The function creates and initializes a new PL011 WDF device, and registers it
// with the SerCx framework.
//
// Arguments:
//
// DeviceInitPtr - Pointer to a framework-allocated WDFDEVICE_INIT structure.
//
// Return Value:
//
// Device creation and SerCx2 registration status.
//
_Use_decl_annotations_
NTSTATUS
PL011EvtDeviceAdd(
WDFDRIVER WdfDriver,
PWDFDEVICE_INIT DeviceInitPtr
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(WdfDriver);
//
// Set P&P and Power callbacks
//
{
WDF_PNPPOWER_EVENT_CALLBACKS wdfPnppowerEventCallbacks;
WDF_PNPPOWER_EVENT_CALLBACKS_INIT(&wdfPnppowerEventCallbacks);
wdfPnppowerEventCallbacks.EvtDevicePrepareHardware = PL011EvtDevicePrepareHardware;
wdfPnppowerEventCallbacks.EvtDeviceReleaseHardware = PL011EvtDeviceReleaseHardware;
WdfDeviceInitSetPnpPowerEventCallbacks(DeviceInitPtr, &wdfPnppowerEventCallbacks);
} // P&P and Power
//
// Call SerCx2InitializeDeviceInit() to attach the SerCX2 to the
// WDF pipeline.
//
// Note:
// SerCx2InitializeDeviceInit MUST be called before calling
// WdfDeviceCreate()!
//
NTSTATUS status = SerCx2InitializeDeviceInit(DeviceInitPtr);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"SerCx2InitializeDeviceInit failed, (status = %!STATUS!)", status
);
return status;
}
DECLARE_CONST_UNICODE_STRING(
SDDL_DEVOBJ_SERCX_SYS_ALL_ADM_ALL_UMDF_ALL_USERS_RDWR,
L"D:P(A;;GA;;;SY)(A;;GA;;;BA)(A;;GA;;;UD)(A;;GRGW;;;BU)"
);
status = WdfDeviceInitAssignSDDLString(
DeviceInitPtr,
&SDDL_DEVOBJ_SERCX_SYS_ALL_ADM_ALL_UMDF_ALL_USERS_RDWR
);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"WdfDeviceInitAssignSDDLString failed. (status = %!STATUS!, SDDL_DEVOBJ_SERCX_SYS_ALL_ADM_ALL_UMDF_ALL_USERS_RDWR = %wZ)",
status,
&SDDL_DEVOBJ_SERCX_SYS_ALL_ADM_ALL_UMDF_ALL_USERS_RDWR
);
return status;
}
//
// Create and initialize the WDF device.
//
WDFDEVICE wdfDevice;
{
WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(&attributes, PL011_DEVICE_EXTENSION);
status = WdfDeviceCreate(&DeviceInitPtr, &attributes, &wdfDevice);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"WdfDeviceCreate failed, (status = %!STATUS!)", status
);
return status;
}
status = PL011pDeviceExtensionInit(wdfDevice);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"PL011DeviceExtensionInit failed, (status = %!STATUS!)", status
);
return status;
}
} // Create the WDF device
//
// Initialize and register the device with SerCx2 class extension.
//
status = PL011pDeviceSerCx2Init(wdfDevice);
if (!NT_SUCCESS(status)) {
return status;
}
return status;
}
//
// Routine Description:
//
// PL011EvtDevicePrepareHardware() is called by the framework when a PL011
// device is coming online, after being it's resources have been negotiated and
// translated.
// The routine reads and map the device resources and initializes the device.
//
// Arguments:
//
// WdfDevice - The WdfDevice object the represent the PL011 this instance of
// the PL011 controller.
//
// ResourcesRaw - Hardware resource list.
//
// ResourcesTranslated - Hardware translated resource list.
//
// Return Value:
//
// Resources mapping status and controller initialization completion code.
//
_Use_decl_annotations_
NTSTATUS
PL011EvtDevicePrepareHardware(
WDFDEVICE WdfDevice,
WDFCMRESLIST ResourcesRaw,
WDFCMRESLIST ResourcesTranslated
)
{
PAGED_CODE();
UNREFERENCED_PARAMETER(ResourcesRaw);
NTSTATUS status = PL011pDeviceParseResources(
WdfDevice,
ResourcesTranslated
);
if (!NT_SUCCESS(status)) {
return status;
}
//
// Check if we are conflicting with the debugger, and if so, do not
// create a device interface or initialize hardware.
//
PL011_DEVICE_EXTENSION* devExtPtr = PL011DeviceGetExtension(WdfDevice);
if (devExtPtr->IsDebuggerConflict) {
//
// If we don't have an FunctionConfig() resource that we need to hold
// on to, fail the load of the driver.
//
if (devExtPtr->PL011ResourceData.FunctionConfigConnectionId.QuadPart == 0LL) {
PL011_LOG_ERROR("Detected conflict with kernel debugger, failing load.");
// Tell WDF not to attempt it's usual retry loop
WdfDeviceSetFailed(WdfDevice, WdfDeviceFailedNoRestart);
return STATUS_UNSUCCESSFUL;
}
//
// Reserve the FunctionConfig() resource so that another client can't
// steal the pins away from the kernel debugger.
//
status = PL011pDeviceReserveFunctionConfigResource(devExtPtr);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"PL011pDeviceReserveFunctionConfigResource(...) failed. (status = %!STATUS!, FunctionConfigConnectionId = %llx)",
status,
devExtPtr->PL011ResourceData.FunctionConfigConnectionId.QuadPart
);
return status;
}
return STATUS_SUCCESS;
}
status = PL011pDeviceMapResources(
WdfDevice,
ResourcesRaw,
ResourcesTranslated
);
if (!NT_SUCCESS(status)) {
return status;
}
//
// If we received a UartSerialBus resource, make device accessible
// to usermode.
//
if (devExtPtr->PL011ResourceData.ConnectionId.QuadPart != 0LL) {
status = PL011pDeviceCreateDeviceInterface(
WdfDevice,
devExtPtr->PL011ResourceData.ConnectionId
);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"PL011pDeviceCreateDeviceInterface failed. (status = %!STATUS!, devExtPtr->PL011ResourceData.ConnectionId = %llx)",
status,
devExtPtr->PL011ResourceData.ConnectionId.QuadPart
);
return status;
}
} else {
PL011_LOG_INFORMATION("Skipping creation of device interface due to absence of UartSerialBus() descriptor.");
}
return PL011HwInitController(WdfDevice);
}
//
// Routine Description:
//
// PL011EvtDeviceReleaseHardware() is called by the framework when a PL011
// device is offline and not accessible anymore.
// The routine just releases device resources.
//
// Arguments:
//
// WdfDevice - The WdfDevice object the represent the PL011 this instance of
// the PL011 controller.
//
// ResourcesTranslated - Hardware translated resource list.
//
// Return Value:
//
// STATUS_SUCCESS
//
_Use_decl_annotations_
NTSTATUS
PL011EvtDeviceReleaseHardware(
WDFDEVICE WdfDevice,
WDFCMRESLIST ResourcesTranslated
)
{
PAGED_CODE();
PL011_DEVICE_EXTENSION* devExtPtr = PL011DeviceGetExtension(WdfDevice);
PPL011_RESOURCE_DATA resourceDataPtr = &devExtPtr->PL011ResourceData;
UNREFERENCED_PARAMETER(ResourcesTranslated);
if (devExtPtr->PL011RegsPtr != nullptr) {
PL011_ASSERT(resourceDataPtr->RegsSpan == ULONG(PL011_REG_FILE::REG_FILE_SIZE));
MmUnmapIoSpace(
PVOID(devExtPtr->PL011RegsPtr),
resourceDataPtr->RegsSpan
);
}
//
// Disconnecting from interrupt will automatically be done
// by the framework....
//
//
// Clear resource information
//
RtlZeroMemory(resourceDataPtr, sizeof(PL011_RESOURCE_DATA));
devExtPtr->PL011RegsPtr = nullptr;
devExtPtr->WdfUartInterrupt = NULL;
return STATUS_SUCCESS;
}
//
// Routine Description:
//
// PL011DeviceExtensionInit is called by translate received PL011 errors
// into SERIAL_ERROR_??? codes, and record those on the device context, to be picked
// up when through IOCTL_SERIAL_GET_COMMSTATUS.
//
// Arguments:
//
// WdfDevice - The WdfDevice object the represent the PL011 this instance of
// the PL011 controller.
//
// PL011ErrorEventsMask - The new PL011 errors events mask
//
// Return Value:
//
_Use_decl_annotations_
VOID
PL011DeviceRecordErrors(
PL011_DEVICE_EXTENSION* DevExtPtr,
ULONG PL011ErrorEventsMask
)
{
LONG newErrorTypes = 0;
if ((PL011ErrorEventsMask & UARTIMSC_OEIM) != 0) {
newErrorTypes |= SERIAL_ERROR_OVERRUN;
}
if ((PL011ErrorEventsMask & UARTIMSC_FEIM) != 0) {
newErrorTypes |= SERIAL_ERROR_FRAMING;
}
if ((PL011ErrorEventsMask & UARTIMSC_PEIM) != 0) {
newErrorTypes |= SERIAL_ERROR_PARITY;
}
if ((PL011ErrorEventsMask & UARTIMSC_BEIM) != 0) {
newErrorTypes |= SERIAL_ERROR_BREAK;
}
InterlockedOr(
reinterpret_cast<volatile LONG*>(&DevExtPtr->UartErrorTypes),
newErrorTypes
);
}
//
// Routine Description:
//
// PL011DeviceNotifyEvents is called by translate received PL011 events
// into SERIAL_EV_??? codes, and notify the framework if the new events
// correspond to the 'wait event mask' previously set by the framework
// through EvtSerCx2SetWaitMask() (IOCTL_SERIAL_SET_WAIT_MASK).
//
// Arguments:
//
// WdfDevice - The WdfDevice object the represent the PL011 this instance of
// the PL011 controller.
//
// PL011EventsMask - The new PL011 events mask.
//
// Return Value:
//
_Use_decl_annotations_
VOID
PL011DeviceNotifyEvents(
PL011_DEVICE_EXTENSION* DevExtPtr,
ULONG PL011EventsMask
)
{
//
// Translate received events to SERIAL_EV_??? so we
// can notify the framework if needed.
//
ULONG waitEvents = 0;
if ((PL011EventsMask & UART_INTERUPPTS_ERRORS) != 0) {
waitEvents |= SERIAL_EV_ERR;
}
if ((PL011EventsMask & UARTRIS_BEIS) != 0) {
waitEvents |= SERIAL_EV_BREAK;
}
//
// Modem status interrupt
//
if ((PL011EventsMask & UARTRIS_CTSMIS) != 0) {
waitEvents |= SERIAL_EV_CTS;
}
if ((PL011EventsMask & UARTRIS_DSRMIS) != 0) {
waitEvents |= SERIAL_EV_DSR;
}
//
// Complete 'wait events', if any...
//
{
KIRQL oldIrql = ExAcquireSpinLockShared(&DevExtPtr->ConfigLock);
ULONG currentWaitMask = DevExtPtr->WaitEventMask;
if ((waitEvents != 0) && ((waitEvents & currentWaitMask) != 0)) {
SerCx2CompleteWait(DevExtPtr->WdfDevice, waitEvents);
}
ExReleaseSpinLockShared(&DevExtPtr->ConfigLock, oldIrql);
} // Complete 'wait events', if any...
}
//
// Routine Description:
//
// PL011DeviceExtensionInit is called by PL011EvtDeviceAdd to initialize the
// WDF device extension.
// The routine initializes context objects and registers the driver ISR/DPC.
//
// Arguments:
//
// WdfDevice - The WdfDevice object the represent the PL011 this instance of
// the PL011 controller.
//
// Return Value:
//
// STATUS_SUCCESS, or STATUS_NOT_SUPPORTED if board does not support the
// features mask that was set in INF file.
//
_Use_decl_annotations_
NTSTATUS
PL011pDeviceExtensionInit(
WDFDEVICE WdfDevice
)
{
PAGED_CODE();
const PL011_DRIVER_EXTENSION* drvExtPtr = PL011DriverGetExtension(WdfGetDriver());
PL011_DEVICE_EXTENSION* devExtPtr = PL011DeviceGetExtension(WdfDevice);
devExtPtr->WdfDevice = WdfDevice;
devExtPtr->OpenCount = 0;
devExtPtr->ConfigLock = 0;
devExtPtr->UartSupportedControlsMask = PL011_DEFAULT_SUPPORTED_CONTROLS;
devExtPtr->CurrentConfiguration.UartClockHz = drvExtPtr->UartClockHz;
devExtPtr->CurrentConfiguration.MaxBaudRateBPS = drvExtPtr->MaxBaudRateBPS;
KeInitializeSpinLock(&devExtPtr->Lock);
KeInitializeSpinLock(&devExtPtr->RegsLock);
//
// Get the features supported by this board
//
return PL011pDeviceGetSupportedFeatures(WdfDevice);
}
//
// Routine Description:
//
// PL011DeviceSerCx2Init is called by PL011EvtDeviceAdd to initialize the
// SerCx2 class extension with the device.
// The routine registers the driver callbacks with SerCx2 class extension.
//
// Arguments:
//
// WdfDevice - The WdfDevice object the represent the PL011 this instance of
// the PL011 controller.
//
// Return Value:
//
// SerCxe initialization status.
//
_Use_decl_annotations_
NTSTATUS
PL011pDeviceSerCx2Init(
WDFDEVICE WdfDevice
)
{
PAGED_CODE();
NTSTATUS status;
PL011_DEVICE_EXTENSION* devExtPtr = PL011DeviceGetExtension(WdfDevice);
//
// Initialize SerCx2 class extension.
//
{
SERCX2_CONFIG serCx2Config;
SERCX2_CONFIG_INIT(
&serCx2Config,
PL011EvtSerCx2ApplyConfig,
PL011EvtSerCx2Control,
PL011EvtSerCx2PurgeFifos
);
serCx2Config.EvtSerCx2SetWaitMask = PL011EvtSerCx2SetWaitMask;
serCx2Config.EvtSerCx2FileOpen = PL011EvtSerCx2FileOpen;
serCx2Config.EvtSerCx2FileClose = PL011EvtSerCx2FileClose;
status = SerCx2InitializeDevice(WdfDevice, &serCx2Config);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"SerCx2InitializeDevice failed, (status = %!STATUS!)", status
);
return status;
}
} // Initialize SerCx2 class extension.
//
// Configure receive PIO contexts and callbacks
//
{
SERCX2_PIO_RECEIVE_CONFIG serCx2PioReceiveConfig;
SERCX2_PIO_RECEIVE_CONFIG_INIT(
&serCx2PioReceiveConfig,
PL011SerCx2EvtPioReceiveReadBuffer,
PL011SerCx2EvtPioReceiveEnableReadyNotification,
PL011SerCx2EvtPioReceiveCancelReadyNotification
);
WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(
&attributes,
PL011_SERCXPIORECEIVE_CONTEXT
);
//
// Create the SerCx2 RX object
//
status = SerCx2PioReceiveCreate(
WdfDevice,
&serCx2PioReceiveConfig,
&attributes,
&devExtPtr->SerCx2PioReceive
);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"SerCx2PioTransmitCreate failed, (status = %!STATUS!)", status
);
return status;
}
//
// Initialize the SerCx2 RX object context
//
status = PL011RxPioReceiveInit(WdfDevice, devExtPtr->SerCx2PioReceive);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"PL011RxPioReceiveInit failed, (status = %!STATUS!)", status
);
return status;
}
} // Configure receive PIO
//
// Configure transmit PIO contexts and callbacks
//
{
SERCX2_PIO_TRANSMIT_CONFIG serCx2PioTransmitConfig;
SERCX2_PIO_TRANSMIT_CONFIG_INIT(
&serCx2PioTransmitConfig,
PL011SerCx2EvtPioTransmitWriteBuffer,
PL011SerCx2EvtPioTransmitEnableReadyNotification,
PL011SerCx2EvtPioTransmitCancelReadyNotification
);
serCx2PioTransmitConfig.EvtSerCx2PioTransmitDrainFifo =
PL011SerCx2EvtPioTransmitDrainFifo;
serCx2PioTransmitConfig.EvtSerCx2PioTransmitCancelDrainFifo =
PL011SerCx2EvtPioTransmitCancelDrainFifo;
serCx2PioTransmitConfig.EvtSerCx2PioTransmitPurgeFifo =
PL011SerCx2EvtPioTransmitPurgeFifo;
WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(
&attributes,
PL011_SERCXPIOTRANSMIT_CONTEXT
);
//
// Create the SerCx2 TX object
//
status = SerCx2PioTransmitCreate(
WdfDevice,
&serCx2PioTransmitConfig,
&attributes,
&devExtPtr->SerCx2PioTransmit
);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"SerCx2PioTransmitCreate failed, (status = %!STATUS!)", status
);
return status;
}
//
// Initialize the SerCx2 TX object context
//
status = PL011TxPioTransmitInit(WdfDevice, devExtPtr->SerCx2PioTransmit);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"PL011RxPioReceiveInit failed, (status = %!STATUS!)", status
);
return status;
}
} // Configure transmit PIO
return status;
}
//
// Routine Description:
//
// PL011pDeviceMapResources() is called by PL011EvtDevicePrepareHardware() to
// parse the PL011 hardware resources. The routine verifies the resource
// list and saves the resource information in the device context.
//
// Arguments:
//
// WdfDevice - The WdfDevice object the represent the PL011 this instance of
// the PL011 controller.
//
// ResourcesTranslated - Hardware translated resource list.
//
// Return Value:
//
// STATUS_SUCCESS - Valid resources were parsed and mapped, or
// STATUS_ACPI_INVALID_DATA - Missing resources or invalid resources
// were parsed.
//
_Use_decl_annotations_
NTSTATUS
PL011pDeviceParseResources(
WDFDEVICE WdfDevice,
WDFCMRESLIST ResourcesTranslated
)
{
PAGED_CODE();
PL011_DEVICE_EXTENSION* devExtPtr = PL011DeviceGetExtension(WdfDevice);
PPL011_RESOURCE_DATA resourceDataPtr = &devExtPtr->PL011ResourceData;
ULONG numResourses = WdfCmResourceListGetCount(ResourcesTranslated);
ULONG numMemResourcesFound = 0;
ULONG numIntResourcesFound = 0;
ULONG numDmaResourcesFound = 0;
ULONG numSerialConnectionResourcesFound = 0;
ULONG numFunctionConfigResourcesFound = 0;
for (ULONG resInx = 0; resInx < numResourses; ++resInx) {
PCM_PARTIAL_RESOURCE_DESCRIPTOR resDescPtr =
WdfCmResourceListGetDescriptor(ResourcesTranslated, resInx);
switch (resDescPtr->Type) {
case CmResourceTypeMemory:
++numMemResourcesFound;
PL011_ASSERT(numMemResourcesFound == 1);
if (resDescPtr->u.Memory.Length == ULONG(PL011_REG_FILE::REG_FILE_SIZE)) {
//
// Make sure the debugger is not configured to the
// same port as this PL011 device.
//
if (PL011IsDebuggerPresent()) {
PHYSICAL_ADDRESS kdComPA =
MmGetPhysicalAddress(*KdComPortInUse);
if (kdComPA.QuadPart ==
resDescPtr->u.Memory.Start.QuadPart) {
PL011_LOG_ERROR("Kernel debugger is in use!");
#if !IS_DONT_CHANGE_HW
//
// We cannot fail the driver loading, thus we mark it,
// and let the device to load. We will block any open request.
//
devExtPtr->IsDebuggerConflict = TRUE;
#endif //!IS_DONT_CHANGE_HW
} // Debugger resource conflict
} // Make sure debugger is not using the port
resourceDataPtr->RegsResInx = resInx;
resourceDataPtr->RegsPA = resDescPtr->u.Memory.Start;
resourceDataPtr->RegsSpan = resDescPtr->u.Memory.Length;
} else {
PL011_LOG_ERROR(
"Invalid PL011 register file span (%lu)!",
resDescPtr->u.Memory.Length
);
return STATUS_ACPI_INVALID_DATA;
}
break;
case CmResourceTypeInterrupt:
++numIntResourcesFound;
PL011_ASSERT(numIntResourcesFound == 1);
resourceDataPtr->IntResInx = resInx;
resourceDataPtr->IntVector = resDescPtr->u.Interrupt.Vector;
resourceDataPtr->IntLevel = resDescPtr->u.Interrupt.Level;
resourceDataPtr->Intffinity = resDescPtr->u.Interrupt.Affinity;
break;
case CmResourceTypeDma:
++numDmaResourcesFound;
PL011_ASSERT(numDmaResourcesFound <= 2);
//
// To be implemented...
//
break;
case CmResourceTypeConnection:
switch (resDescPtr->u.Connection.Class) {
case CM_RESOURCE_CONNECTION_CLASS_SERIAL:
switch (resDescPtr->u.Connection.Type) {
case CM_RESOURCE_CONNECTION_TYPE_SERIAL_UART:
++numSerialConnectionResourcesFound;
PL011_ASSERT(numSerialConnectionResourcesFound == 1);
resourceDataPtr->ConnectionId.LowPart =
resDescPtr->u.Connection.IdLowPart;
resourceDataPtr->ConnectionId.HighPart =
resDescPtr->u.Connection.IdHighPart;
PL011_ASSERT(resourceDataPtr->ConnectionId.QuadPart != 0LL);
break; // CM_RESOURCE_CONNECTION_TYPE_SERIAL_UART
} // switch (resDescPtr->u.Connection.Type)
break; // CM_RESOURCE_CONNECTION_CLASS_SERIAL
case CM_RESOURCE_CONNECTION_CLASS_FUNCTION_CONFIG:
switch (resDescPtr->u.Connection.Type) {
case CM_RESOURCE_CONNECTION_TYPE_FUNCTION_CONFIG:
++numFunctionConfigResourcesFound;
PL011_ASSERT(numFunctionConfigResourcesFound == 1);
resourceDataPtr->FunctionConfigConnectionId.LowPart =
resDescPtr->u.Connection.IdLowPart;
resourceDataPtr->FunctionConfigConnectionId.HighPart =
resDescPtr->u.Connection.IdHighPart;
PL011_ASSERT(resourceDataPtr->FunctionConfigConnectionId.QuadPart != 0LL);
break; // CM_RESOURCE_CONNECTION_TYPE_FUNCTION_CONFIG
} // switch (resDescPtr->u.Connection.Type)
break; // CM_RESOURCE_CONNECTION_CLASS_FUNCTION_CONFIG
} // switch (resDescPtr->u.Connection.Class)
break;
default:
PL011_ASSERT(FALSE);
break;
} // switch
} // for (resource list)
//
// Make sure we got everything we need...
//
if (numMemResourcesFound != 1) {
PL011_LOG_ERROR("Invalid or no memory resource!");
return STATUS_ACPI_INVALID_DATA;
}
if (numIntResourcesFound != 1) {
PL011_LOG_ERROR("Invalid or not interrupt resource!");
return STATUS_ACPI_INVALID_DATA;
}
//
// (DMA is optional)
//
if ((numDmaResourcesFound != 2) && (numDmaResourcesFound != 0)) {
PL011_LOG_ERROR(
"Invalid number of DMA channels found (%lu)!",
numDmaResourcesFound
);
return STATUS_ACPI_INVALID_DATA;
}
return STATUS_SUCCESS;
}
//
// Routine Description:
//
// PL011pDeviceMapResources() is called by PL011EvtDevicePrepareHardware() to
// map the PL011 hardware resources. The routine maps the required resources.
//
// Arguments:
//
// WdfDevice - The WdfDevice object the represent the PL011 this instance of
// the PL011 controller.
//
// ResourcesRaw - Hardware resource list.
//
// ResourcesTranslated - Hardware translated resource list.
//
// Return Value:
//
// STATUS_SUCCESS - Valid resources were parsed and mapped, or
// STATUS_ACPI_INVALID_DATA - Missing resources or invalid resources
// were parsed.
// STATUS_INSUFFICIENT_RESOURCES - Failed to map PL011 register file into
// virtual memory
//
_Use_decl_annotations_
NTSTATUS
PL011pDeviceMapResources(
WDFDEVICE WdfDevice,
WDFCMRESLIST ResourcesRaw,
WDFCMRESLIST ResourcesTranslated
)
{
PAGED_CODE();
NTSTATUS status = STATUS_UNSUCCESSFUL;
PL011_DEVICE_EXTENSION* devExtPtr = PL011DeviceGetExtension(WdfDevice);
PPL011_RESOURCE_DATA resourceDataPtr = &devExtPtr->PL011ResourceData;
PL011_ASSERT(devExtPtr->PL011RegsPtr == nullptr);
//
// Map device registers into virtual memory...
//
devExtPtr->PL011RegsPtr = static_cast<volatile ULONG*>(MmMapIoSpaceEx(
resourceDataPtr->RegsPA,
resourceDataPtr->RegsSpan,
PAGE_READWRITE | PAGE_NOCACHE
));
if (devExtPtr->PL011RegsPtr == nullptr) {
PL011_LOG_ERROR(
"Failed to map PL011 regs, span %lu bytes!",
resourceDataPtr->RegsSpan
);
return STATUS_INSUFFICIENT_RESOURCES;
}
PL011_LOG_TRACE(
"PL011 regs: PA 0x%08X, span %lu bytes, VA %p",
resourceDataPtr->RegsPA.LowPart,
resourceDataPtr->RegsSpan,
PVOID(devExtPtr->PL011RegsPtr)
);
//
// Setup interrupt handling
//
{
//
// Create a spin lock for the interrupt object
//
WDF_OBJECT_ATTRIBUTES attributes;
WDF_OBJECT_ATTRIBUTES_INIT(&attributes);
attributes.ParentObject = WdfDevice;
WDFSPINLOCK interruptSpinLock;
status = WdfSpinLockCreate(&attributes, &interruptSpinLock);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"WdfSpinLockCreate failed for interrupt lock, "
"(status = %!STATUS!)",
status
);
return status;
}
//
// Configure and create the interrupt object
//
PCM_PARTIAL_RESOURCE_DESCRIPTOR resRawDescPtr =
WdfCmResourceListGetDescriptor(
ResourcesRaw,
resourceDataPtr->IntResInx
);
PCM_PARTIAL_RESOURCE_DESCRIPTOR resTranslatedDescPtr =
WdfCmResourceListGetDescriptor(
ResourcesTranslated,
resourceDataPtr->IntResInx
);
WDF_INTERRUPT_CONFIG wdfInterruptConfig;
WDF_INTERRUPT_CONFIG_INIT(
&wdfInterruptConfig,
PL011EvtInterruptIsr,
PL011EvtInterruptDpc
);
wdfInterruptConfig.SpinLock = interruptSpinLock;
wdfInterruptConfig.InterruptRaw = resRawDescPtr;
wdfInterruptConfig.InterruptTranslated = resTranslatedDescPtr;
status = WdfInterruptCreate(
WdfDevice,
&wdfInterruptConfig,
WDF_NO_OBJECT_ATTRIBUTES,
&devExtPtr->WdfUartInterrupt
);
if (!NT_SUCCESS(status)) {
PL011_LOG_ERROR(
"WdfInterruptCreate failed , (status = %!STATUS!)", status
);
return status;
}
} // Setup interrupt handling
return STATUS_SUCCESS;
}
//
// Routine Description: