]>
Commit | Line | Data |
---|---|---|
36ee91ca | 1 | /** @file\r |
2 | \r | |
d1102dba | 3 | Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR>\r |
9d510e61 | 4 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
36ee91ca | 5 | \r |
6 | Module Name:\r | |
7 | \r | |
8 | Dpc.c\r | |
9 | \r | |
10 | Abstract:\r | |
11 | \r | |
12 | \r | |
13 | **/\r | |
14 | \r | |
15 | #include "Dpc.h"\r | |
16 | \r | |
17 | //\r | |
18 | // Handle for the EFI_DPC_PROTOCOL instance\r | |
19 | //\r | |
20 | EFI_HANDLE mDpcHandle = NULL;\r | |
21 | \r | |
22 | //\r | |
23 | // The EFI_DPC_PROTOCOL instances that is installed onto mDpcHandle\r | |
24 | //\r | |
25 | EFI_DPC_PROTOCOL mDpc = {\r | |
26 | DpcQueueDpc,\r | |
27 | DpcDispatchDpc\r | |
28 | };\r | |
29 | \r | |
30 | //\r | |
d80c3d6e | 31 | // Global variables used to measure the DPC Queue Depths\r |
36ee91ca | 32 | //\r |
33 | UINTN mDpcQueueDepth = 0;\r | |
34 | UINTN mMaxDpcQueueDepth = 0;\r | |
35 | \r | |
36 | //\r | |
37 | // Free list of DPC entries. As DPCs are queued, entries are removed from this\r | |
38 | // free list. As DPC entries are dispatched, DPC entries are added to the free list.\r | |
39 | // If the free list is empty and a DPC is queued, the free list is grown by allocating\r | |
40 | // an additional set of DPC entries.\r | |
41 | //\r | |
e48e37fc | 42 | LIST_ENTRY mDpcEntryFreeList = INITIALIZE_LIST_HEAD_VARIABLE(mDpcEntryFreeList);\r |
36ee91ca | 43 | \r |
44 | //\r | |
d80c3d6e | 45 | // An array of DPC queues. A DPC queue is allocated for every level EFI_TPL value.\r |
36ee91ca | 46 | // As DPCs are queued, they are added to the end of the linked list.\r |
47 | // As DPCs are dispatched, they are removed from the beginning of the linked list.\r | |
48 | //\r | |
e48e37fc | 49 | LIST_ENTRY mDpcQueue[TPL_HIGH_LEVEL + 1];\r |
36ee91ca | 50 | \r |
51 | /**\r | |
52 | Add a Deferred Procedure Call to the end of the DPC queue.\r | |
53 | \r | |
54 | @param This Protocol instance pointer.\r | |
55 | @param DpcTpl The EFI_TPL that the DPC should be invoked.\r | |
56 | @param DpcProcedure Pointer to the DPC's function.\r | |
57 | @param DpcContext Pointer to the DPC's context. Passed to DpcProcedure\r | |
58 | when DpcProcedure is invoked.\r | |
59 | \r | |
60 | @retval EFI_SUCCESS The DPC was queued.\r | |
61 | @retval EFI_INVALID_PARAMETER DpcTpl is not a valid EFI_TPL.\r | |
62 | @retval EFI_INVALID_PARAMETER DpcProcedure is NULL.\r | |
63 | @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r | |
64 | add the DPC to the queue.\r | |
65 | \r | |
66 | **/\r | |
67 | EFI_STATUS\r | |
68 | EFIAPI\r | |
69 | DpcQueueDpc (\r | |
70 | IN EFI_DPC_PROTOCOL *This,\r | |
71 | IN EFI_TPL DpcTpl,\r | |
72 | IN EFI_DPC_PROCEDURE DpcProcedure,\r | |
73 | IN VOID *DpcContext OPTIONAL\r | |
74 | )\r | |
75 | {\r | |
76 | EFI_STATUS ReturnStatus;\r | |
77 | EFI_TPL OriginalTpl;\r | |
78 | DPC_ENTRY *DpcEntry;\r | |
79 | UINTN Index;\r | |
80 | \r | |
81 | //\r | |
82 | // Make sure DpcTpl is valid\r | |
83 | //\r | |
84 | if (DpcTpl < TPL_APPLICATION || DpcTpl > TPL_HIGH_LEVEL) {\r | |
85 | return EFI_INVALID_PARAMETER;\r | |
86 | }\r | |
87 | \r | |
88 | //\r | |
89 | // Make sure DpcProcedure is valid\r | |
90 | //\r | |
91 | if (DpcProcedure == NULL) {\r | |
92 | return EFI_INVALID_PARAMETER;\r | |
93 | }\r | |
94 | \r | |
95 | //\r | |
96 | // Assume this function will succeed\r | |
97 | //\r | |
98 | ReturnStatus = EFI_SUCCESS;\r | |
99 | \r | |
100 | //\r | |
101 | // Raise the TPL level to TPL_HIGH_LEVEL for DPC list operation and save the\r | |
102 | // current TPL value so it can be restored when this function returns.\r | |
103 | //\r | |
104 | OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
105 | \r | |
106 | //\r | |
107 | // Check to see if there are any entries in the DPC free list\r | |
108 | //\r | |
109 | if (IsListEmpty (&mDpcEntryFreeList)) {\r | |
110 | //\r | |
111 | // If the current TPL is greater than TPL_NOTIFY, then memory allocations\r | |
112 | // can not be performed, so the free list can not be expanded. In this case\r | |
113 | // return EFI_OUT_OF_RESOURCES.\r | |
114 | //\r | |
115 | if (OriginalTpl > TPL_NOTIFY) {\r | |
116 | ReturnStatus = EFI_OUT_OF_RESOURCES;\r | |
117 | goto Done;\r | |
118 | }\r | |
119 | \r | |
120 | //\r | |
121 | // Add 64 DPC entries to the free list\r | |
122 | //\r | |
123 | for (Index = 0; Index < 64; Index++) {\r | |
124 | //\r | |
125 | // Lower the TPL level to perform a memory allocation\r | |
126 | //\r | |
127 | gBS->RestoreTPL (OriginalTpl);\r | |
128 | \r | |
129 | //\r | |
130 | // Allocate a new DPC entry\r | |
131 | //\r | |
132 | DpcEntry = AllocatePool (sizeof (DPC_ENTRY));\r | |
133 | \r | |
134 | //\r | |
135 | // Raise the TPL level back to TPL_HIGH_LEVEL for DPC list operations\r | |
136 | //\r | |
137 | gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
138 | \r | |
139 | //\r | |
140 | // If the allocation of a DPC entry fails, and the free list is empty,\r | |
141 | // then return EFI_OUT_OF_RESOURCES.\r | |
142 | //\r | |
143 | if (DpcEntry == NULL) {\r | |
144 | if (IsListEmpty (&mDpcEntryFreeList)) {\r | |
145 | ReturnStatus = EFI_OUT_OF_RESOURCES;\r | |
146 | goto Done;\r | |
147 | }\r | |
148 | }\r | |
149 | \r | |
150 | //\r | |
151 | // Add the newly allocated DPC entry to the DPC free list\r | |
152 | //\r | |
153 | InsertTailList (&mDpcEntryFreeList, &DpcEntry->ListEntry);\r | |
154 | }\r | |
155 | }\r | |
156 | \r | |
157 | //\r | |
158 | // Retrieve the first node from the free list of DPCs\r | |
159 | //\r | |
160 | DpcEntry = (DPC_ENTRY *)(GetFirstNode (&mDpcEntryFreeList));\r | |
161 | \r | |
162 | //\r | |
163 | // Remove the first node from the free list of DPCs\r | |
164 | //\r | |
165 | RemoveEntryList (&DpcEntry->ListEntry);\r | |
166 | \r | |
167 | //\r | |
168 | // Fill in the DPC entry with the DpcProcedure and DpcContext\r | |
169 | //\r | |
170 | DpcEntry->DpcProcedure = DpcProcedure;\r | |
171 | DpcEntry->DpcContext = DpcContext;\r | |
172 | \r | |
173 | //\r | |
174 | // Add the DPC entry to the end of the list for the specified DplTpl.\r | |
175 | //\r | |
176 | InsertTailList (&mDpcQueue[DpcTpl], &DpcEntry->ListEntry);\r | |
177 | \r | |
178 | //\r | |
179 | // Increment the measured DPC queue depth across all TPLs\r | |
180 | //\r | |
181 | mDpcQueueDepth++;\r | |
182 | \r | |
183 | //\r | |
184 | // Measure the maximum DPC queue depth across all TPLs\r | |
185 | //\r | |
186 | if (mDpcQueueDepth > mMaxDpcQueueDepth) {\r | |
187 | mMaxDpcQueueDepth = mDpcQueueDepth;\r | |
188 | }\r | |
189 | \r | |
190 | Done:\r | |
191 | //\r | |
192 | // Restore the original TPL level when this function was called\r | |
193 | //\r | |
194 | gBS->RestoreTPL (OriginalTpl);\r | |
195 | \r | |
196 | return ReturnStatus;\r | |
197 | }\r | |
198 | \r | |
199 | /**\r | |
200 | Dispatch the queue of DPCs. ALL DPCs that have been queued with a DpcTpl\r | |
201 | value greater than or equal to the current TPL are invoked in the order that\r | |
202 | they were queued. DPCs with higher DpcTpl values are invoked before DPCs with\r | |
203 | lower DpcTpl values.\r | |
204 | \r | |
205 | @param This Protocol instance pointer.\r | |
206 | \r | |
207 | @retval EFI_SUCCESS One or more DPCs were invoked.\r | |
208 | @retval EFI_NOT_FOUND No DPCs were invoked.\r | |
209 | \r | |
210 | **/\r | |
211 | EFI_STATUS\r | |
212 | EFIAPI\r | |
213 | DpcDispatchDpc (\r | |
214 | IN EFI_DPC_PROTOCOL *This\r | |
215 | )\r | |
216 | {\r | |
217 | EFI_STATUS ReturnStatus;\r | |
218 | EFI_TPL OriginalTpl;\r | |
219 | EFI_TPL Tpl;\r | |
220 | DPC_ENTRY *DpcEntry;\r | |
221 | \r | |
222 | //\r | |
223 | // Assume that no DPCs will be invoked\r | |
224 | //\r | |
225 | ReturnStatus = EFI_NOT_FOUND;\r | |
226 | \r | |
227 | //\r | |
228 | // Raise the TPL level to TPL_HIGH_LEVEL for DPC list operation and save the\r | |
229 | // current TPL value so it can be restored when this function returns.\r | |
230 | //\r | |
231 | OriginalTpl = gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
232 | \r | |
233 | //\r | |
234 | // Check to see if there are 1 or more DPCs currently queued\r | |
235 | //\r | |
236 | if (mDpcQueueDepth > 0) {\r | |
237 | //\r | |
238 | // Loop from TPL_HIGH_LEVEL down to the current TPL value\r | |
239 | //\r | |
240 | for (Tpl = TPL_HIGH_LEVEL; Tpl >= OriginalTpl; Tpl--) {\r | |
241 | //\r | |
242 | // Check to see if the DPC queue is empty\r | |
243 | //\r | |
244 | while (!IsListEmpty (&mDpcQueue[Tpl])) {\r | |
245 | //\r | |
246 | // Retrieve the first DPC entry from the DPC queue specified by Tpl\r | |
247 | //\r | |
248 | DpcEntry = (DPC_ENTRY *)(GetFirstNode (&mDpcQueue[Tpl]));\r | |
249 | \r | |
250 | //\r | |
251 | // Remove the first DPC entry from the DPC queue specified by Tpl\r | |
252 | //\r | |
253 | RemoveEntryList (&DpcEntry->ListEntry);\r | |
254 | \r | |
255 | //\r | |
256 | // Decrement the measured DPC Queue Depth across all TPLs\r | |
257 | //\r | |
258 | mDpcQueueDepth--;\r | |
259 | \r | |
260 | //\r | |
261 | // Lower the TPL to TPL value of the current DPC queue\r | |
262 | //\r | |
263 | gBS->RestoreTPL (Tpl);\r | |
264 | \r | |
265 | //\r | |
266 | // Invoke the DPC passing in its context\r | |
267 | //\r | |
268 | (DpcEntry->DpcProcedure) (DpcEntry->DpcContext);\r | |
269 | \r | |
270 | //\r | |
271 | // At least one DPC has been invoked, so set the return status to EFI_SUCCESS\r | |
272 | //\r | |
273 | ReturnStatus = EFI_SUCCESS;\r | |
274 | \r | |
275 | //\r | |
276 | // Raise the TPL level back to TPL_HIGH_LEVEL for DPC list operations\r | |
277 | //\r | |
278 | gBS->RaiseTPL (TPL_HIGH_LEVEL);\r | |
279 | \r | |
280 | //\r | |
281 | // Add the invoked DPC entry to the DPC free list\r | |
282 | //\r | |
283 | InsertTailList (&mDpcEntryFreeList, &DpcEntry->ListEntry);\r | |
284 | }\r | |
285 | }\r | |
286 | }\r | |
287 | \r | |
288 | //\r | |
289 | // Restore the original TPL level when this function was called\r | |
290 | //\r | |
291 | gBS->RestoreTPL (OriginalTpl);\r | |
292 | \r | |
293 | return ReturnStatus;\r | |
294 | }\r | |
295 | \r | |
7bce0c5a | 296 | /**\r |
297 | The entry point for DPC driver which installs the EFI_DPC_PROTOCOL onto a new handle.\r | |
298 | \r | |
299 | @param ImageHandle The image handle of the driver.\r | |
300 | @param SystemTable The system table.\r | |
301 | \r | |
d80c3d6e | 302 | @retval EFI_SUCCESS The DPC queues were initialized and the EFI_DPC_PROTOCOL was\r |
7bce0c5a | 303 | installed onto a new handle.\r |
304 | @retval Others Failed to install EFI_DPC_PROTOCOL.\r | |
305 | \r | |
306 | **/\r | |
36ee91ca | 307 | EFI_STATUS\r |
308 | EFIAPI\r | |
309 | DpcDriverEntryPoint (\r | |
310 | IN EFI_HANDLE ImageHandle,\r | |
311 | IN EFI_SYSTEM_TABLE *SystemTable\r | |
312 | )\r | |
36ee91ca | 313 | {\r |
314 | EFI_STATUS Status;\r | |
315 | UINTN Index;\r | |
316 | \r | |
317 | //\r | |
318 | // ASSERT() if the EFI_DPC_PROTOCOL is already present in the handle database\r | |
319 | //\r | |
320 | ASSERT_PROTOCOL_ALREADY_INSTALLED (NULL, &gEfiDpcProtocolGuid);\r | |
321 | \r | |
322 | //\r | |
323 | // Initialize the DPC queue for all possible TPL values\r | |
324 | //\r | |
325 | for (Index = 0; Index <= TPL_HIGH_LEVEL; Index++) {\r | |
326 | InitializeListHead (&mDpcQueue[Index]);\r | |
327 | }\r | |
328 | \r | |
329 | //\r | |
330 | // Install the EFI_DPC_PROTOCOL instance onto a new handle\r | |
331 | //\r | |
332 | Status = gBS->InstallMultipleProtocolInterfaces (\r | |
333 | &mDpcHandle,\r | |
d1102dba | 334 | &gEfiDpcProtocolGuid,\r |
c4a62a12 | 335 | &mDpc,\r |
36ee91ca | 336 | NULL\r |
337 | );\r | |
338 | ASSERT_EFI_ERROR (Status);\r | |
339 | \r | |
340 | return Status;\r | |
341 | }\r |