]> git.proxmox.com Git - mirror_edk2.git/blame - Vlv2TbltDevicePkg/PlatformDxe/Observable/Observable.c
UefiCpuPkg/PiSmmCpuDxeSmm: eliminate conditional jump in IA32 SmmStartup()
[mirror_edk2.git] / Vlv2TbltDevicePkg / PlatformDxe / Observable / Observable.c
CommitLineData
3cbfba02
DW
1/*++\r
2 This file contains 'Framework Code' and is licensed as such\r
3 under the terms of your license agreement with Intel or your\r
4 vendor. This file may not be modified, except as allowed by\r
5 additional terms of your license agreement.\r
6--*/\r
7/*++\r
8\r
9Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved\r
10 \r\r
11 This program and the accompanying materials are licensed and made available under\r\r
12 the terms and conditions of the BSD License that accompanies this distribution. \r\r
13 The full text of the license may be found at \r\r
14 http://opensource.org/licenses/bsd-license.php. \r\r
15 \r\r
16 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, \r\r
17 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. \r\r
18 \r\r
19\r
20\r
21Module Name:\r
22\r
23 Observable.c\r
24\r
25Abstract:\r
26\r
27 The following contains all of the implementation for the Observable protocol. The\r
28 protocol uses the observer design pattern to provide a way to publish events and\r
29 to subscribe to those events so that a callback will be performed at the time of\r
30 the event. The observables and subscribers are maintained by the static tree,\r
31 mObservableDb. The difference between this protocol and the existing event protocol\r
32 that exists within the EFI framework is that this protocol allows for parameters\r
33 to be passed to the subscribed callbacks that can contain up to date context.\r
34\r
35--*/\r
36\r
37#include "Observable.h"\r
38\r
39static OBS_TREE* mObservableDb = NULL;\r
40static EFI_HANDLE mObservableHandle = NULL;\r
41static OBS_OBSERVABLE_PROTOCOL mObservable = {\r
42 AddObservable,\r
43 RemoveObservable,\r
44 Subscribe,\r
45 Unsubscribe,\r
46 Publish,\r
47 RemoveAllObservables\r
48};\r
49\r
50/** Install observable protocol.\r
51 *\r
52 * Install interface and initialize the observable protocol.\r
53 *\r
54 * @param VOID No parameters.\r
55 *\r
56 * @return EFI_SUCCESS Successfully installed and initialized the protocol.\r
57 **/\r
58EFI_STATUS\r
59InitializeObservableProtocol(\r
60 VOID\r
61 )\r
62{\r
63 EFI_STATUS Status;\r
64\r
65 //\r
66 // Install protocol.\r
67 //\r
68 Status = gBS->InstallProtocolInterface (\r
69 &mObservableHandle,\r
70 &gObservableProtocolGuid,\r
71 EFI_NATIVE_INTERFACE,\r
72 &mObservable\r
73 );\r
74\r
75 return Status;\r
76}\r
77\r
78/** Deletes a subscriber\r
79 *\r
80 * This function removes the subscriber pointed to by Head.\r
81 *\r
82 * @param OBS_TREE* Head Points to the current subscriber.\r
83 *\r
84 * @return OBS_TREE* Returns the tree after successfully removing the subscriber.\r
85 **/\r
86OBS_LEAF*\r
87DeleteSubscriber(\r
88 OBS_LEAF* Head\r
89 )\r
90{\r
91 OBS_LEAF* Temp;\r
92\r
93 if (Head) {\r
94 Temp = Head;\r
95 Head = Head->Next;\r
96 gBS->FreePool(Temp);\r
97 }\r
98\r
99 return Head;\r
100}\r
101\r
102/** Finds and deletes all subscribers\r
103 *\r
104 * This function iterates recursively through the existing subscribers and delets them all.\r
105 *\r
106 * @param OBS_TREE* Head Points to the current subscriber.\r
107 *\r
108 * @return OBS_TREE* Returns the tree after successfully removing the subscribers.\r
109 **/\r
110OBS_LEAF*\r
111DeleteAllSubscribers(\r
112 OBS_LEAF* Head\r
113 )\r
114{\r
115 if (Head) {\r
116 if (Head->Next) {\r
117 //\r
118 // We aren't at the end of the list yet.\r
119 //\r
120 Head->Next = DeleteAllSubscribers(Head->Next);\r
121 }\r
122\r
123 //\r
124 // At the end, so delete the subscriber.\r
125 //\r
126 Head = DeleteSubscriber(Head);\r
127 }\r
128\r
129 return Head;\r
130}\r
131\r
132/** Deletes an observable\r
133 *\r
134 * This function removes the observable pointed to by Head.\r
135 *\r
136 * @param OBS_TREE* Head Points to the current observable.\r
137 *\r
138 * @return OBS_TREE* Returns the tree after successfully removing the observable.\r
139 **/\r
140OBS_TREE*\r
141DeleteObservable(\r
142 OBS_TREE* Head\r
143 )\r
144{\r
145 OBS_TREE* Temp;\r
146\r
147 if (Head) {\r
148 Temp = Head;\r
149 Head = Head->Next;\r
150 gBS->FreePool(Temp);\r
151 }\r
152\r
153 return Head;\r
154}\r
155\r
156/** Finds and deletes all observables\r
157 *\r
158 * This function iterates recursively through the existing observables database and, starting with\r
159 * the last most observable, deletes all of its subscribers, then deletes the observable itself.\r
160 *\r
161 * @param OBS_TREE* Head Points to the current observable.\r
162 *\r
163 * @return OBS_TREE* Returns the tree after successfully removing the observables.\r
164 **/\r
165OBS_TREE*\r
166DeleteAllObservables(\r
167 OBS_TREE* Head\r
168 )\r
169{\r
170 if (Head) {\r
171 if (Head->Next) {\r
172 //\r
173 // We aren't at the end of the list yet.\r
174 //\r
175 Head->Next = DeleteAllObservables(Head->Next);\r
176 }\r
177\r
178 //\r
179 // This is the end of the list of observables.\r
180 //\r
181 Head->Leaf = DeleteAllSubscribers(Head->Leaf);\r
182\r
183 //\r
184 // Subscribers are deleted, so now delete the observable.\r
185 //\r
186 Head = DeleteObservable(Head);\r
187 }\r
188\r
189 return Head;\r
190}\r
191\r
192/** Finds and deletes observable\r
193 *\r
194 * This function iterates recursively through the existing observable database in order to find the one\r
195 * specified by ReferenceGuid so that it can be deleted. If the requested observable is found, before it\r
196 * is deleted, all of the subscribers that are listening to this observable are deleted.\r
197 *\r
198 * @param OBS_TREE* Head Points to the current observable.\r
199 * EFI_GUID ReferenceGuid Corresponds to the observable that we're looking for.\r
200 *\r
201 * @return OBS_TREE* Returns the tree after successfully removing (or not finding) the observable.\r
202 **/\r
203OBS_TREE*\r
204FindAndDeleteObservable(\r
205 OBS_TREE* Head,\r
206 EFI_GUID ReferenceGuid\r
207 )\r
208{\r
209 if (Head) {\r
210 if (CompareMem(&(Head->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
211 //\r
212 // We found the observable. Delete all of it's subscribers, first.\r
213 //\r
214 Head->Leaf = DeleteAllSubscribers(Head->Leaf);\r
215 //\r
216 // Now we can safely remove the observable.\r
217 //\r
218 Head = DeleteObservable(Head);\r
219 } else {\r
220 //\r
221 // Not found. Keep searching.\r
222 //\r
223 Head->Next = FindAndDeleteObservable(Head->Next, ReferenceGuid);\r
224 }\r
225 }\r
226\r
227 return Head;\r
228}\r
229\r
230/** Finds and deletes subscriber\r
231 *\r
232 * This function iterates recursively through the existing subscribers that are listening to the\r
233 * observable that was found when this function was called.\r
234 *\r
235 * @param OBS_TREE* Head Points to the current subscriber.\r
236 * OBS_CALLBACK CallbackInterface This is the subscriber that is requested be removed.\r
237 *\r
238 * @return OBS_TREE* Returns the tree after successfully removing (or not finding) the subscriber.\r
239 **/\r
240OBS_LEAF*\r
241_FindAndDeleteSubscriber(\r
242 OBS_LEAF* Head,\r
243 OBS_CALLBACK CallbackInterface\r
244 )\r
245{\r
246 if (Head) {\r
247 if (Head->Observer == CallbackInterface) {\r
248 //\r
249 // Found it. Now let's delete it.\r
250 //\r
251 Head = DeleteSubscriber(Head);\r
252 } else {\r
253 //\r
254 // Not found. Keep searching.\r
255 //\r
256 Head->Next = _FindAndDeleteSubscriber(Head->Next, CallbackInterface);\r
257 }\r
258 }\r
259\r
260 return Head;\r
261}\r
262\r
263/** Finds and deletes subscriber\r
264 *\r
265 * This function iterates recursively through the existing observables database until it either finds\r
266 * a matching guid or reaches the end of the list. After finding a match, it calls a helper function,\r
267 * _FindAndDeleteSubscriber. At this point, all responsibility for finding and deleting the subscriber\r
268 * lies on the helper function.\r
269 *\r
270 * @param OBS_TREE* Head Points to the current observable.\r
271 * EFI_GUID ReferenceGuid Corresponds to the observable that we're looking for.\r
272 * OBS_CALLBACK CallbackInterface This is the subscriber that is requested be removed.\r
273 *\r
274 * @return OBS_TREE* Returns the tree after successfully removing (or not finding) the subscriber.\r
275 **/\r
276OBS_TREE*\r
277FindAndDeleteSubscriber(\r
278 IN OUT OBS_TREE* Head,\r
279 IN EFI_GUID ReferenceGuid,\r
280 IN OBS_CALLBACK CallbackInterface\r
281 )\r
282{\r
283 if (Head) {\r
284 if (CompareMem(&(Head->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
285 //\r
286 // We found the observer that matches ReferenceGuid. Find and delete the subscriber that is\r
287 // listening to it.\r
288 //\r
289 Head->Leaf = _FindAndDeleteSubscriber(Head->Leaf, CallbackInterface);\r
290 } else {\r
291 //\r
292 // Not found. Keep searching.\r
293 //\r
294 Head->Next = FindAndDeleteSubscriber(Head->Next, ReferenceGuid, CallbackInterface);\r
295 }\r
296 }\r
297\r
298 return Head;\r
299}\r
300\r
301/** Remove all observables.\r
302 *\r
303 * Remove all observable guids and all interfaces subscribed to them.\r
304 *\r
305 * @param VOID No parameters.\r
306 *\r
307 * @return EFI_SUCCESS Successfully removed all observables and subscribed interfaces.\r
308 **/\r
309EFI_STATUS\r
310EFIAPI\r
311RemoveAllObservables(\r
312 VOID\r
313 )\r
314{\r
315 mObservableDb = DeleteAllObservables(mObservableDb);\r
316\r
317 return EFI_SUCCESS;\r
318}\r
319\r
320/** Subscribe an interface with an observable guid.\r
321 *\r
322 * Use this to register a callback function with a guid. The function provided by CallbackInterface will be executed\r
323 * whenever the appropriate observable instance specified by ReferenceGuid calls Publish.\r
324 *\r
325 * @param EFI_GUID ReferenceGuid The observable guid that the callback interface will subscribe to.\r
326 * OBS_CASLLBACK CallbackInterface A pointer to the function that is subscribing to the observable.\r
327 *\r
328 * @return EFI_SUCCESS Successfully subscribed the interface to the observable guid.\r
329 * EFI_NOT_FOUND No match could be found between the provided guid and existing observables.\r
330 * EFI_OUT_OF_RESOURCES Could not subscribe to this observer due to resource limitations.\r
331 * EFI_INVALID_PARAMETER Interface is already subscribed to this observer.\r
332 **/\r
333EFI_STATUS\r
334EFIAPI\r
335Subscribe (\r
336 IN EFI_GUID ReferenceGuid,\r
337 IN OBS_CALLBACK CallbackInterface\r
338 )\r
339{\r
340 EFI_STATUS Status = EFI_SUCCESS;\r
341 OBS_TREE* TempTree = NULL;\r
342 OBS_LEAF* Last = NULL;\r
343 OBS_LEAF* TempLeaf = NULL;\r
344 OBS_LEAF* NewLeaf = NULL;\r
345 BOOLEAN Found = FALSE;\r
346\r
347 if (mObservableDb != NULL) {\r
348 //\r
349 // Find the observable guid that we're looking for.\r
350 //\r
351 for (TempTree = mObservableDb; TempTree != NULL; TempTree = TempTree->Next) {\r
352 if (CompareMem(&(TempTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
353 Found = TRUE;\r
354 break;\r
355 }\r
356 }\r
357 if (Found) {\r
358 //\r
359 // Prepare to add a new leaf.\r
360 //\r
361 NewLeaf = AllocateZeroPool(sizeof(OBS_LEAF));\r
362 if (!NewLeaf) {\r
363 Status = EFI_OUT_OF_RESOURCES;\r
364 } else {\r
365 NewLeaf->Next = NULL;\r
366 NewLeaf->Observer = CallbackInterface;\r
367 //\r
368 // Go to the end of the list of observers.\r
369 //\r
370 if (TempTree->Leaf != NULL) {\r
371 //\r
372 // First check to see if this is a duplicate observer.\r
373 //\r
374 Found = FALSE;\r
375 TempLeaf = TempTree->Leaf;\r
376 do {\r
377 Last = TempLeaf;\r
378 if (TempLeaf->Observer == CallbackInterface) {\r
379 //\r
380 // It is, so let's abort this process.\r
381 //\r
382 Found = TRUE;\r
383 break;\r
384 }\r
385 TempLeaf = TempLeaf->Next;\r
386 } while (TempLeaf != NULL);\r
387 TempLeaf = Last;\r
388\r
389 //\r
390 // Check for duplicates.\r
391 //\r
392 if (Found) {\r
393 gBS->FreePool(NewLeaf);\r
394 Status = EFI_INVALID_PARAMETER;\r
395 } else {\r
396 //\r
397 // At this point, TempLeaf->Next will be the end of the list.\r
398 //\r
399 TempLeaf->Next = NewLeaf;\r
400 }\r
401 } else {\r
402 //\r
403 // There are no observers listening to this guid. Start a new list.\r
404 //\r
405 TempTree->Leaf = NewLeaf;\r
406 }\r
407 }\r
408 } else {\r
409 Status = EFI_NOT_FOUND;\r
410 }\r
411 } else {\r
412 Status = EFI_NOT_FOUND;\r
413 }\r
414\r
415 return Status;\r
416}\r
417\r
418/** Unsubscribe an interface with an observable guid.\r
419 *\r
420 * Use this to remove an interface from the callback list associated with an observable guid.\r
421 *\r
422 * @param EFI_GUID ReferenceGuid The observable guid to unsubscribe the interface from.\r
423 * OBS_NOTIFY_INTERFACE NotifyCallback A pointer to the interface that is being unsubscribed.\r
424 *\r
425 * @return EFI_SUCCESS Successfully unsubscribed the interface from the observable guid.\r
426 **/\r
427EFI_STATUS\r
428EFIAPI\r
429Unsubscribe (\r
430 IN EFI_GUID ReferenceGuid,\r
431 IN OBS_CALLBACK CallbackInterface\r
432 )\r
433{\r
434 mObservableDb = FindAndDeleteSubscriber(mObservableDb, ReferenceGuid, CallbackInterface);\r
435\r
436 return EFI_SUCCESS;\r
437}\r
438\r
439/** Notify observing functions.\r
440 *\r
441 * Use this to notify all functions who are subscribed to the guid specified by ReferenceGuid.\r
442 *\r
443 * @param EFI_GUID ReferenceGuid The observable guid that contains the the list of interfaces to be notified.\r
444 * VOID* Data Parameter context to be passed to the notification function.\r
445 *\r
446 * @return EFI_SUCCESS Successfully notified all observers listening to this guid.\r
447 * EFI_NOT_FOUND No match could be found between the provided guid and existing observables.\r
448 **/\r
449EFI_STATUS\r
450EFIAPI\r
451Publish (\r
452 IN EFI_GUID ReferenceGuid,\r
453 IN OUT VOID* Data\r
454 )\r
455{\r
456 EFI_STATUS Status = EFI_SUCCESS;\r
457 OBS_TREE* TempTree = NULL;\r
458 OBS_LEAF* TempLeaf = NULL;\r
459 BOOLEAN Found = FALSE;\r
460\r
461 if (mObservableDb != NULL) {\r
462 //\r
463 // Find the observable guid that we're looking for.\r
464 //\r
465 for (TempTree = mObservableDb; TempTree != NULL; TempTree = TempTree->Next) {\r
466 if (CompareMem(&(TempTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
467 Found = TRUE;\r
468 break;\r
469 }\r
470 }\r
471 if (Found) {\r
472 //\r
473 // Notify every listener by performing each provided callback.\r
474 //\r
475 for (TempLeaf = TempTree->Leaf; TempLeaf != NULL; TempLeaf = TempLeaf->Next) {\r
476 if (TempLeaf->Observer != NULL) {\r
477 //\r
478 // Execute the callback.\r
479 //\r
480 TempLeaf->Observer(Data);\r
481 }\r
482 }\r
483 } else {\r
484 Status = EFI_NOT_FOUND;\r
485 }\r
486 } else {\r
487 Status = EFI_NOT_FOUND;\r
488 }\r
489\r
490 return Status;\r
491}\r
492\r
493/** Creates a new observable.\r
494 *\r
495 * Create a new observable that can be observed with the use of Subscribe function.\r
496 *\r
497 * @param EFI_GUID ReferenceGuid The observable guid to add.\r
498 *\r
499 * @return EFI_SUCCESS Successfully added observable.\r
500 * EFI_INVALID_PARAMETER Observable already exists.\r
501 **/\r
502EFI_STATUS\r
503EFIAPI\r
504AddObservable (\r
505 IN EFI_GUID ReferenceGuid\r
506 )\r
507{\r
508 EFI_STATUS Status = EFI_SUCCESS;\r
509 OBS_TREE* TempTree = NULL;\r
510 OBS_TREE* Last = NULL;\r
511 OBS_TREE* NewTree = NULL;\r
512 BOOLEAN Found = FALSE;\r
513\r
514 if (mObservableDb != NULL) {\r
515 if (mObservableDb->Next != NULL) {\r
516 //\r
517 // Iterate to the end of the observable list while checking to see if we aren't creating a duplicate.\r
518 //\r
519 TempTree = mObservableDb->Next;\r
520 do {\r
521 Last = TempTree;\r
522 if (CompareMem(&(TempTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid)) == 0) {\r
523 Found = TRUE;\r
524 break;\r
525 }\r
526 TempTree = TempTree->Next;\r
527 } while (TempTree != NULL);\r
528 TempTree = Last;\r
529 } else {\r
530 TempTree = mObservableDb;\r
531 }\r
532 if (Found) {\r
533 //\r
534 // Duplicate, so reject the parameter.\r
535 //\r
536 Status = EFI_INVALID_PARAMETER;\r
537 } else {\r
538 //\r
539 // TempTree->Next is our target. Prepare to add a new tree link.\r
540 //\r
541 NewTree = AllocateZeroPool(sizeof(OBS_TREE));\r
542 if (NewTree) {\r
543 NewTree->Next = NULL;\r
544 NewTree->Leaf = NULL;\r
545 CopyMem(&(NewTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid));\r
546 TempTree->Next = NewTree;\r
547 } else {\r
548 Status = EFI_OUT_OF_RESOURCES;\r
549 }\r
550 }\r
551 } else {\r
552 //\r
553 // mObservableDb has not been created yet. Let's do that.\r
554 //\r
555 NewTree = AllocateZeroPool(sizeof(OBS_TREE));\r
556 if (NewTree) {\r
557 NewTree->Next = NULL;\r
558 NewTree->Leaf = NULL;\r
559 CopyMem(&(NewTree->ObservableGuid), &ReferenceGuid, sizeof(ReferenceGuid));\r
560 mObservableDb = NewTree;\r
561 } else {\r
562 Status = EFI_OUT_OF_RESOURCES;\r
563 }\r
564 }\r
565\r
566 return Status;\r
567}\r
568\r
569/** Remove an observable.\r
570 *\r
571 * Remove an observable so that it can no longer be subscribed to. In addition, unsubscribe any functions\r
572 * that are subscribed to this guid.\r
573 *\r
574 * @param EFI_GUID ReferenceGuid The observable guid to remove.\r
575 *\r
576 * @return EFI_SUCCESS Successfully removed observable.\r
577 **/\r
578EFI_STATUS\r
579EFIAPI\r
580RemoveObservable (\r
581 IN EFI_GUID ReferenceGuid\r
582 )\r
583{\r
584 mObservableDb = FindAndDeleteObservable(mObservableDb, ReferenceGuid);\r
585\r
586 return EFI_SUCCESS;\r
587}\r