]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Universal/DataHubDxe/DataHub.c
This patch includes:
[mirror_edk2.git] / IntelFrameworkModulePkg / Universal / DataHubDxe / DataHub.c
1 /** @file
2 This code produces the Data Hub protocol. It preloads the data hub
3 with status information copied in from PEI HOBs.
4
5 Copyright (c) 2006 - 2009, Intel Corporation
6 All rights reserved. This program and the accompanying materials
7 are licensed and made available under the terms and conditions of the BSD License
8 which accompanies this distribution. The full text of the license may be found at
9 http://opensource.org/licenses/bsd-license.php
10
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
13
14 **/
15
16 #include "DataHub.h"
17
18 CONST EFI_GUID gZeroGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
19
20 //
21 // Since this driver will only ever produce one instance of the Logging Hub
22 // protocol you are not required to dynamically allocate the PrivateData.
23 //
24 DATA_HUB_INSTANCE mPrivateData;
25
26 //
27 // Worker functions private to this file
28 //
29 DATA_HUB_FILTER_DRIVER *
30 FindFilterDriverByEvent (
31 IN LIST_ENTRY *Head,
32 IN EFI_EVENT Event
33 );
34
35 EFI_DATA_RECORD_HEADER *
36 GetNextDataRecord (
37 IN LIST_ENTRY *Head,
38 IN UINT64 ClassFilter,
39 IN OUT UINT64 *PtrCurrentMTC
40 );
41
42 /**
43 Log data record into the data logging hub
44
45 @param This Protocol instance structure
46 @param DataRecordGuid GUID that defines record contents
47 @param ProducerName GUID that defines the name of the producer of the data
48 @param DataRecordClass Class that defines generic record type
49 @param RawData Data Log record as defined by DataRecordGuid
50 @param RawDataSize Size of Data Log data in bytes
51
52 @retval EFI_SUCCESS If data was logged
53 @retval EFI_OUT_OF_RESOURCES If data was not logged due to lack of system
54 resources.
55 **/
56 EFI_STATUS
57 EFIAPI
58 DataHubLogData (
59 IN EFI_DATA_HUB_PROTOCOL *This,
60 IN EFI_GUID *DataRecordGuid,
61 IN EFI_GUID *ProducerName,
62 IN UINT64 DataRecordClass,
63 IN VOID *RawData,
64 IN UINT32 RawDataSize
65 )
66 {
67 EFI_STATUS Status;
68 DATA_HUB_INSTANCE *Private;
69 EFI_DATA_ENTRY *LogEntry;
70 UINT32 TotalSize;
71 UINT32 RecordSize;
72 EFI_DATA_RECORD_HEADER *Record;
73 VOID *Raw;
74 DATA_HUB_FILTER_DRIVER *FilterEntry;
75 LIST_ENTRY *Link;
76 LIST_ENTRY *Head;
77
78 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
79
80 //
81 // Combine the storage for the internal structs and a copy of the log record.
82 // Record follows PrivateLogEntry. The consumer will be returned a pointer
83 // to Record so we don't what it to be the thing that was allocated from
84 // pool, so the consumer can't free an data record by mistake.
85 //
86 RecordSize = sizeof (EFI_DATA_RECORD_HEADER) + RawDataSize;
87 TotalSize = sizeof (EFI_DATA_ENTRY) + RecordSize;
88
89 //
90 // The Logging action is the critical section, so it is locked.
91 // The MTC asignment & update, time, and logging must be an
92 // atomic operation, so use the lock.
93 //
94 Status = EfiAcquireLockOrFail (&Private->DataLock);
95 if (EFI_ERROR (Status)) {
96 //
97 // Reentrancy detected so exit!
98 //
99 return Status;
100 }
101
102 LogEntry = AllocatePool (TotalSize);
103
104 if (LogEntry == NULL) {
105 EfiReleaseLock (&Private->DataLock);
106 return EFI_OUT_OF_RESOURCES;
107 }
108
109 ZeroMem (LogEntry, TotalSize);
110
111 Record = (EFI_DATA_RECORD_HEADER *) (LogEntry + 1);
112 Raw = (VOID *) (Record + 1);
113
114 //
115 // Build Standard Log Header
116 //
117 Record->Version = EFI_DATA_RECORD_HEADER_VERSION;
118 Record->HeaderSize = sizeof (EFI_DATA_RECORD_HEADER);
119 Record->RecordSize = RecordSize;
120 CopyMem (&Record->DataRecordGuid, DataRecordGuid, sizeof (EFI_GUID));
121 CopyMem (&Record->ProducerName, ProducerName, sizeof (EFI_GUID));
122 Record->DataRecordClass = DataRecordClass;
123
124 //
125 // Ensure LogMonotonicCount is not zero
126 //
127 Record->LogMonotonicCount = ++Private->GlobalMonotonicCount;
128
129 gRT->GetTime (&Record->LogTime, NULL);
130
131 //
132 // Insert log into the internal linked list.
133 //
134 LogEntry->Signature = EFI_DATA_ENTRY_SIGNATURE;
135 LogEntry->Record = Record;
136 LogEntry->RecordSize = sizeof (EFI_DATA_ENTRY) + RawDataSize;
137 InsertTailList (&Private->DataListHead, &LogEntry->Link);
138
139 CopyMem (Raw, RawData, RawDataSize);
140
141 EfiReleaseLock (&Private->DataLock);
142
143 //
144 // Send Signal to all the filter drivers which are interested
145 // in the record's class and guid.
146 //
147 Head = &Private->FilterDriverListHead;
148 for (Link = GetFirstNode(Head); Link != Head; Link = GetNextNode(Head, Link)) {
149 FilterEntry = FILTER_ENTRY_FROM_LINK (Link);
150 if (((FilterEntry->ClassFilter & DataRecordClass) != 0) &&
151 (CompareGuid (&FilterEntry->FilterDataRecordGuid, &gZeroGuid) ||
152 CompareGuid (&FilterEntry->FilterDataRecordGuid, DataRecordGuid))) {
153 gBS->SignalEvent (FilterEntry->Event);
154 }
155 }
156
157 return EFI_SUCCESS;
158 }
159
160 /**
161
162 Get a previously logged data record and the MonotonicCount for the next
163 availible Record. This allows all records or all records later
164 than a give MonotonicCount to be returned. If an optional FilterDriverEvent
165 is passed in with a MonotonicCout of zero return the first record
166 not yet read by the filter driver. If FilterDriverEvent is NULL and
167 MonotonicCount is zero return the first data record.
168
169 @param This Pointer to the EFI_DATA_HUB_PROTOCOL instance.
170 @param MonotonicCount Specifies the Record to return. On input, zero means
171 return the first record. On output, contains the next
172 record to availible. Zero indicates no more records.
173 @param FilterDriverEvent If FilterDriverEvent is not passed in a MonotonicCount
174 of zero, it means to return the first data record.
175 If FilterDriverEvent is passed in, then a MonotonicCount
176 of zero means to return the first data not yet read by
177 FilterDriverEvent.
178 @param Record Returns a dynamically allocated memory buffer with a data
179 record that matches MonotonicCount.
180
181 @retval EFI_SUCCESS Data was returned in Record.
182 @retval EFI_INVALID_PARAMETER FilterDriverEvent was passed in but does not exist.
183 @retval EFI_NOT_FOUND MonotonicCount does not match any data record in the
184 system. If a MonotonicCount of zero was passed in, then
185 no data records exist in the system.
186 @retval EFI_OUT_OF_RESOURCES Record was not returned due to lack of system resources.
187
188 **/
189 EFI_STATUS
190 EFIAPI
191 DataHubGetNextRecord (
192 IN EFI_DATA_HUB_PROTOCOL *This,
193 IN OUT UINT64 *MonotonicCount,
194 IN EFI_EVENT *FilterDriverEvent, OPTIONAL
195 OUT EFI_DATA_RECORD_HEADER **Record
196 )
197 {
198 DATA_HUB_INSTANCE *Private;
199 DATA_HUB_FILTER_DRIVER *FilterDriver;
200 UINT64 ClassFilter;
201 UINT64 FilterMonotonicCount;
202
203 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
204
205 FilterDriver = NULL;
206 FilterMonotonicCount = 0;
207 ClassFilter = EFI_DATA_RECORD_CLASS_DEBUG |
208 EFI_DATA_RECORD_CLASS_ERROR |
209 EFI_DATA_RECORD_CLASS_DATA |
210 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
211
212 if (FilterDriverEvent != NULL) {
213 //
214 // For events the beginning is the last unread record. This info is
215 // stored in the instance structure, so we must look up the event
216 // to get the data.
217 //
218 FilterDriver = FindFilterDriverByEvent (
219 &Private->FilterDriverListHead,
220 *FilterDriverEvent
221 );
222 if (FilterDriver == NULL) {
223 return EFI_INVALID_PARAMETER;
224 }
225 //
226 // Use the Class filter the event was created with.
227 //
228 ClassFilter = FilterDriver->ClassFilter;
229
230 if (*MonotonicCount == 0) {
231 //
232 // Use the MTC from the Filter Driver.
233 //
234 FilterMonotonicCount = FilterDriver->GetNextMonotonicCount;
235
236 //
237 // The GetNextMonotonicCount field remembers the last value from the previous time.
238 // But we already processed this vaule, so we need to find the next one.
239 //
240 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, &FilterMonotonicCount);
241 if (FilterMonotonicCount != 0) {
242 *MonotonicCount = FilterMonotonicCount;
243 }
244
245 if ((FilterDriver->GetNextMonotonicCount != 0) && (FilterMonotonicCount == 0)) {
246 //
247 // If there is no new record to get exit now.
248 //
249 *MonotonicCount = 0;
250 return EFI_NOT_FOUND;
251 }
252 }
253 }
254 //
255 // Return the record
256 //
257 *Record = GetNextDataRecord (&Private->DataListHead, ClassFilter, MonotonicCount);
258 if (*Record == NULL) {
259 return EFI_NOT_FOUND;
260 }
261
262 if (FilterDriver != NULL) {
263 //
264 // If we have a filter driver update the records that have been read.
265 // If MonotonicCount is zero No more reacords left.
266 //
267 if (*MonotonicCount == 0) {
268 //
269 // Save the current Record MonotonicCount.
270 //
271 FilterDriver->GetNextMonotonicCount = (*Record)->LogMonotonicCount;
272 } else {
273 //
274 // Point to next undread record
275 //
276 FilterDriver->GetNextMonotonicCount = *MonotonicCount;
277 }
278 }
279
280 return EFI_SUCCESS;
281 }
282
283 /**
284 This function registers the data hub filter driver that is represented
285 by FilterEvent. Only one instance of each FilterEvent can be registered.
286 After the FilterEvent is registered, it will be signaled so it can sync
287 with data records that have been recorded prior to the FilterEvent being
288 registered.
289
290 @param This Pointer to The EFI_DATA_HUB_PROTOCOL instance.
291 @param FilterEvent The EFI_EVENT to signal whenever data that matches
292 FilterClass is logged in the system.
293 @param FilterTpl The maximum EFI_TPL at which FilterEvent can be
294 signaled. It is strongly recommended that you use the
295 lowest EFI_TPL possible.
296 @param FilterClass FilterEvent will be signaled whenever a bit in
297 EFI_DATA_RECORD_HEADER.DataRecordClass is also set in
298 FilterClass. If FilterClass is zero, no class-based
299 filtering will be performed.
300 @param FilterDataRecordGuid FilterEvent will be signaled whenever FilterDataRecordGuid
301 matches EFI_DATA_RECORD_HEADER.DataRecordGuid. If
302 FilterDataRecordGuid is NULL, then no GUID-based filtering
303 will be performed.
304
305 @retval EFI_SUCCESS The filter driver event was registered.
306 @retval EFI_ALREADY_STARTED FilterEvent was previously registered and cannot be
307 registered again.
308 @retval EFI_OUT_OF_RESOURCES The filter driver event was not registered due to lack of
309 system resources.
310
311 **/
312 EFI_STATUS
313 EFIAPI
314 DataHubRegisterFilterDriver (
315 IN EFI_DATA_HUB_PROTOCOL * This,
316 IN EFI_EVENT FilterEvent,
317 IN EFI_TPL FilterTpl,
318 IN UINT64 FilterClass,
319 IN EFI_GUID * FilterDataRecordGuid OPTIONAL
320 )
321
322 {
323 DATA_HUB_INSTANCE *Private;
324 DATA_HUB_FILTER_DRIVER *FilterDriver;
325
326 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
327
328 FilterDriver = (DATA_HUB_FILTER_DRIVER *) AllocateZeroPool (sizeof (DATA_HUB_FILTER_DRIVER));
329 if (FilterDriver == NULL) {
330 return EFI_OUT_OF_RESOURCES;
331 }
332 //
333 // Initialize filter driver info
334 //
335 FilterDriver->Signature = EFI_DATA_HUB_FILTER_DRIVER_SIGNATURE;
336 FilterDriver->Event = FilterEvent;
337 FilterDriver->Tpl = FilterTpl;
338 FilterDriver->GetNextMonotonicCount = 0;
339 if (FilterClass == 0) {
340 FilterDriver->ClassFilter = EFI_DATA_RECORD_CLASS_DEBUG |
341 EFI_DATA_RECORD_CLASS_ERROR |
342 EFI_DATA_RECORD_CLASS_DATA |
343 EFI_DATA_RECORD_CLASS_PROGRESS_CODE;
344 } else {
345 FilterDriver->ClassFilter = FilterClass;
346 }
347
348 if (FilterDataRecordGuid != NULL) {
349 CopyMem (&FilterDriver->FilterDataRecordGuid, FilterDataRecordGuid, sizeof (EFI_GUID));
350 }
351 //
352 // Search for duplicate entries
353 //
354 if (FindFilterDriverByEvent (&Private->FilterDriverListHead, FilterEvent) != NULL) {
355 FreePool (FilterDriver);
356 return EFI_ALREADY_STARTED;
357 }
358 //
359 // Make insertion an atomic operation with the lock.
360 //
361 EfiAcquireLock (&Private->DataLock);
362 InsertTailList (&Private->FilterDriverListHead, &FilterDriver->Link);
363 EfiReleaseLock (&Private->DataLock);
364
365 //
366 // Signal the Filter driver we just loaded so they will recieve all the
367 // previous history. If we did not signal here we would have to wait until
368 // the next data was logged to get the history. In a case where no next
369 // data was logged we would never get synced up.
370 //
371 gBS->SignalEvent (FilterEvent);
372
373 return EFI_SUCCESS;
374 }
375
376 /**
377 Remove a Filter Driver, so it no longer gets called when data
378 information is logged.
379
380 @param This Protocol instance structure
381
382 @param FilterEvent Event that represents a filter driver that is to be
383 Unregistered.
384
385 @retval EFI_SUCCESS If FilterEvent was unregistered
386 @retval EFI_NOT_FOUND If FilterEvent does not exist
387 **/
388 EFI_STATUS
389 EFIAPI
390 DataHubUnregisterFilterDriver (
391 IN EFI_DATA_HUB_PROTOCOL *This,
392 IN EFI_EVENT FilterEvent
393 )
394 {
395 DATA_HUB_INSTANCE *Private;
396 DATA_HUB_FILTER_DRIVER *FilterDriver;
397
398 Private = DATA_HUB_INSTANCE_FROM_THIS (This);
399
400 //
401 // Search for duplicate entries
402 //
403 FilterDriver = FindFilterDriverByEvent (
404 &Private->FilterDriverListHead,
405 FilterEvent
406 );
407 if (FilterDriver == NULL) {
408 return EFI_NOT_FOUND;
409 }
410 //
411 // Make removal an atomic operation with the lock
412 //
413 EfiAcquireLock (&Private->DataLock);
414 RemoveEntryList (&FilterDriver->Link);
415 EfiReleaseLock (&Private->DataLock);
416
417 return EFI_SUCCESS;
418 }
419
420 /**
421 Search the Head list for a EFI_DATA_HUB_FILTER_DRIVER member that
422 represents Event and return it.
423
424 @param Head Pointer to head of dual linked list of EFI_DATA_HUB_FILTER_DRIVER structures.
425 @param Event Event to be search for in the Head list.
426
427 @retval EFI_DATA_HUB_FILTER_DRIVER Returned if Event stored in the Head doubly linked list.
428 @retval NULL If Event is not in the list
429
430 **/
431 DATA_HUB_FILTER_DRIVER *
432 FindFilterDriverByEvent (
433 IN LIST_ENTRY *Head,
434 IN EFI_EVENT Event
435 )
436 {
437 DATA_HUB_FILTER_DRIVER *FilterEntry;
438 LIST_ENTRY *Link;
439
440 for (Link = GetFirstNode(Head); Link != Head; Link = GetNextNode(Head, Link)) {
441 FilterEntry = FILTER_ENTRY_FROM_LINK (Link);
442 if (FilterEntry->Event == Event) {
443 return FilterEntry;
444 }
445 }
446
447 return NULL;
448 }
449
450 /**
451 Search the Head doubly linked list for the passed in MTC. Return the
452 matching element in Head and the MTC on the next entry.
453
454 @param Head Head of Data Log linked list.
455 @param ClassFilter Only match the MTC if it is in the same Class as the
456 ClassFilter.
457 @param PtrCurrentMTC On IN contians MTC to search for. On OUT contians next
458 MTC in the data log list or zero if at end of the list.
459
460 @retval EFI_DATA_LOG_ENTRY Return pointer to data log data from Head list.
461 @retval NULL If no data record exists.
462
463 **/
464 EFI_DATA_RECORD_HEADER *
465 GetNextDataRecord (
466 IN LIST_ENTRY *Head,
467 IN UINT64 ClassFilter,
468 IN OUT UINT64 *PtrCurrentMTC
469 )
470
471 {
472 EFI_DATA_ENTRY *LogEntry;
473 LIST_ENTRY *Link;
474 BOOLEAN ReturnFirstEntry;
475 EFI_DATA_RECORD_HEADER *Record;
476 EFI_DATA_ENTRY *NextLogEntry;
477
478 //
479 // If MonotonicCount == 0 just return the first one
480 //
481 ReturnFirstEntry = (BOOLEAN) (*PtrCurrentMTC == 0);
482
483 Record = NULL;
484 for (Link = GetFirstNode(Head); Link != Head; Link = GetNextNode(Head, Link)) {
485 LogEntry = DATA_ENTRY_FROM_LINK (Link);
486 if ((LogEntry->Record->DataRecordClass & ClassFilter) == 0) {
487 //
488 // Skip any entry that does not have the correct ClassFilter
489 //
490 continue;
491 }
492
493 if ((LogEntry->Record->LogMonotonicCount == *PtrCurrentMTC) || ReturnFirstEntry) {
494 //
495 // Return record to the user
496 //
497 Record = LogEntry->Record;
498
499 //
500 // Calculate the next MTC value. If there is no next entry set
501 // MTC to zero.
502 //
503 *PtrCurrentMTC = 0;
504 for (Link = GetNextNode(Head, Link); Link != Head; Link = GetNextNode(Head, Link)) {
505 NextLogEntry = DATA_ENTRY_FROM_LINK (Link);
506 if ((NextLogEntry->Record->DataRecordClass & ClassFilter) != 0) {
507 //
508 // Return the MTC of the next thing to search for if found
509 //
510 *PtrCurrentMTC = NextLogEntry->Record->LogMonotonicCount;
511 break;
512 }
513 }
514 //
515 // Record found exit loop and return
516 //
517 break;
518 }
519 }
520
521 return Record;
522 }
523
524 /**
525 Driver's Entry point routine that install Driver to produce Data Hub protocol.
526
527 @param ImageHandle Module's image handle
528 @param SystemTable Pointer of EFI_SYSTEM_TABLE
529
530 @retval EFI_SUCCESS Logging Hub protocol installed
531 @retval Other No protocol installed, unload driver.
532
533 **/
534 EFI_STATUS
535 EFIAPI
536 DataHubInstall (
537 IN EFI_HANDLE ImageHandle,
538 IN EFI_SYSTEM_TABLE *SystemTable
539 )
540 {
541 EFI_STATUS Status;
542 UINT32 HighMontonicCount;
543
544 mPrivateData.Signature = DATA_HUB_INSTANCE_SIGNATURE;
545 mPrivateData.DataHub.LogData = DataHubLogData;
546 mPrivateData.DataHub.GetNextRecord = DataHubGetNextRecord;
547 mPrivateData.DataHub.RegisterFilterDriver = DataHubRegisterFilterDriver;
548 mPrivateData.DataHub.UnregisterFilterDriver = DataHubUnregisterFilterDriver;
549
550 //
551 // Initialize Private Data in CORE_LOGGING_HUB_INSTANCE that is
552 // required by this protocol
553 //
554 InitializeListHead (&mPrivateData.DataListHead);
555 InitializeListHead (&mPrivateData.FilterDriverListHead);
556
557 EfiInitializeLock (&mPrivateData.DataLock, TPL_NOTIFY);
558
559 //
560 // Make sure we get a bigger MTC number on every boot!
561 //
562 Status = gRT->GetNextHighMonotonicCount (&HighMontonicCount);
563 if (EFI_ERROR (Status)) {
564 //
565 // if system service fails pick a sane value.
566 //
567 mPrivateData.GlobalMonotonicCount = 0;
568 } else {
569 mPrivateData.GlobalMonotonicCount = LShiftU64 ((UINT64) HighMontonicCount, 32);
570 }
571 //
572 // Make a new handle and install the protocol
573 //
574 mPrivateData.Handle = NULL;
575 Status = gBS->InstallProtocolInterface (
576 &mPrivateData.Handle,
577 &gEfiDataHubProtocolGuid,
578 EFI_NATIVE_INTERFACE,
579 &mPrivateData.DataHub
580 );
581 return Status;
582 }
583