VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/powernotification-r0drv.c

Last change on this file was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1/* $Id: powernotification-r0drv.c 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - Power Management, Ring-0 Driver, Event Notifications.
4 */
5
6/*
7 * Copyright (C) 2008-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include <iprt/power.h>
42#include "internal/iprt.h"
43
44#include <iprt/asm.h>
45#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
46# include <iprt/asm-amd64-x86.h>
47#endif
48#include <iprt/assert.h>
49#include <iprt/err.h>
50#include <iprt/mem.h>
51#include <iprt/spinlock.h>
52#include <iprt/string.h>
53#include <iprt/thread.h>
54#include "r0drv/mp-r0drv.h"
55#include "r0drv/power-r0drv.h"
56
57
58/*********************************************************************************************************************************
59* Structures and Typedefs *
60*********************************************************************************************************************************/
61/**
62 * Notification registration record tracking
63 * RTPowerRegisterNotification() calls.
64 */
65typedef struct RTPOWERNOTIFYREG
66{
67 /** Pointer to the next record. */
68 struct RTPOWERNOTIFYREG * volatile pNext;
69 /** The callback. */
70 PFNRTPOWERNOTIFICATION pfnCallback;
71 /** The user argument. */
72 void *pvUser;
73 /** Bit mask indicating whether we've done this callback or not. */
74 uint8_t bmDone[sizeof(void *)];
75} RTPOWERNOTIFYREG;
76/** Pointer to a registration record. */
77typedef RTPOWERNOTIFYREG *PRTPOWERNOTIFYREG;
78
79
80/*********************************************************************************************************************************
81* Global Variables *
82*********************************************************************************************************************************/
83/** The spinlock protecting the list. */
84static RTSPINLOCK volatile g_hRTPowerNotifySpinLock = NIL_RTSPINLOCK;
85/** List of callbacks, in registration order. */
86static PRTPOWERNOTIFYREG volatile g_pRTPowerCallbackHead = NULL;
87/** The current done bit. */
88static uint32_t volatile g_iRTPowerDoneBit;
89/** The list generation.
90 * This is increased whenever the list has been modified. The callback routine
91 * make use of this to avoid having restart at the list head after each callback. */
92static uint32_t volatile g_iRTPowerGeneration;
93
94
95
96
97RTDECL(int) RTPowerSignalEvent(RTPOWEREVENT enmEvent)
98{
99 PRTPOWERNOTIFYREG pCur;
100 RTSPINLOCK hSpinlock;
101
102 /*
103 * This is a little bit tricky as we cannot be holding the spinlock
104 * while calling the callback. This means that the list might change
105 * while we're walking it, and that multiple events might be running
106 * concurrently (depending on the OS).
107 *
108 * So, the first measure is to employ a 32-bitmask for each
109 * record where we'll use a bit that rotates for each call to
110 * this function to indicate which records that has been
111 * processed. This will take care of both changes to the list
112 * and a reasonable amount of concurrent events.
113 *
114 * In order to avoid having to restart the list walks for every
115 * callback we make, we'll make use a list generation number that is
116 * incremented everytime the list is changed. So, if it remains
117 * unchanged over a callback we can safely continue the iteration.
118 */
119 uint32_t iDone = ASMAtomicIncU32(&g_iRTPowerDoneBit);
120 iDone %= RT_SIZEOFMEMB(RTPOWERNOTIFYREG, bmDone) * 8;
121
122 hSpinlock = g_hRTPowerNotifySpinLock;
123 if (hSpinlock == NIL_RTSPINLOCK)
124 return VERR_ACCESS_DENIED;
125 RTSpinlockAcquire(hSpinlock);
126
127 /* Clear the bit. */
128 for (pCur = g_pRTPowerCallbackHead; pCur; pCur = pCur->pNext)
129 ASMAtomicBitClear(&pCur->bmDone[0], iDone);
130
131 /* Iterate the records and perform the callbacks. */
132 do
133 {
134 uint32_t const iGeneration = ASMAtomicUoReadU32(&g_iRTPowerGeneration);
135
136 pCur = g_pRTPowerCallbackHead;
137 while (pCur)
138 {
139 if (!ASMAtomicBitTestAndSet(&pCur->bmDone[0], iDone))
140 {
141 PFNRTPOWERNOTIFICATION pfnCallback = pCur->pfnCallback;
142 void *pvUser = pCur->pvUser;
143 pCur = pCur->pNext;
144 RTSpinlockRelease(g_hRTPowerNotifySpinLock);
145
146 pfnCallback(enmEvent, pvUser);
147
148 /* carefully require the lock here, see RTR0MpNotificationTerm(). */
149 hSpinlock = g_hRTPowerNotifySpinLock;
150 if (hSpinlock == NIL_RTSPINLOCK)
151 return VERR_ACCESS_DENIED;
152 RTSpinlockAcquire(hSpinlock);
153 if (ASMAtomicUoReadU32(&g_iRTPowerGeneration) != iGeneration)
154 break;
155 }
156 else
157 pCur = pCur->pNext;
158 }
159 } while (pCur);
160
161 RTSpinlockRelease(hSpinlock);
162 return VINF_SUCCESS;
163}
164RT_EXPORT_SYMBOL(RTPowerSignalEvent);
165
166
167RTDECL(int) RTPowerNotificationRegister(PFNRTPOWERNOTIFICATION pfnCallback, void *pvUser)
168{
169 PRTPOWERNOTIFYREG pCur;
170 PRTPOWERNOTIFYREG pNew;
171
172 /*
173 * Validation.
174 */
175 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
176 AssertReturn(g_hRTPowerNotifySpinLock != NIL_RTSPINLOCK, VERR_WRONG_ORDER);
177 RT_ASSERT_PREEMPTIBLE();
178
179 RTSpinlockAcquire(g_hRTPowerNotifySpinLock);
180 for (pCur = g_pRTPowerCallbackHead; pCur; pCur = pCur->pNext)
181 if ( pCur->pvUser == pvUser
182 && pCur->pfnCallback == pfnCallback)
183 break;
184 RTSpinlockRelease(g_hRTPowerNotifySpinLock);
185 AssertMsgReturn(!pCur, ("pCur=%p pfnCallback=%p pvUser=%p\n", pCur, pfnCallback, pvUser), VERR_ALREADY_EXISTS);
186
187 /*
188 * Allocate a new record and attempt to insert it.
189 */
190 pNew = (PRTPOWERNOTIFYREG)RTMemAlloc(sizeof(*pNew));
191 if (!pNew)
192 return VERR_NO_MEMORY;
193
194 pNew->pNext = NULL;
195 pNew->pfnCallback = pfnCallback;
196 pNew->pvUser = pvUser;
197 memset(&pNew->bmDone[0], 0xff, sizeof(pNew->bmDone));
198
199 RTSpinlockAcquire(g_hRTPowerNotifySpinLock);
200
201 pCur = g_pRTPowerCallbackHead;
202 if (!pCur)
203 g_pRTPowerCallbackHead = pNew;
204 else
205 {
206 for (pCur = g_pRTPowerCallbackHead; ; pCur = pCur->pNext)
207 if ( pCur->pvUser == pvUser
208 && pCur->pfnCallback == pfnCallback)
209 break;
210 else if (!pCur->pNext)
211 {
212 pCur->pNext = pNew;
213 pCur = NULL;
214 break;
215 }
216 }
217
218 ASMAtomicIncU32(&g_iRTPowerGeneration);
219
220 RTSpinlockRelease(g_hRTPowerNotifySpinLock);
221
222 /* duplicate? */
223 if (pCur)
224 {
225 RTMemFree(pCur);
226 AssertMsgFailedReturn(("pCur=%p pfnCallback=%p pvUser=%p\n", pCur, pfnCallback, pvUser), VERR_ALREADY_EXISTS);
227 }
228
229 return VINF_SUCCESS;
230}
231RT_EXPORT_SYMBOL(RTPowerNotificationRegister);
232
233
234RTDECL(int) RTPowerNotificationDeregister(PFNRTPOWERNOTIFICATION pfnCallback, void *pvUser)
235{
236 PRTPOWERNOTIFYREG pPrev;
237 PRTPOWERNOTIFYREG pCur;
238
239 /*
240 * Validation.
241 */
242 AssertPtrReturn(pfnCallback, VERR_INVALID_POINTER);
243 AssertReturn(g_hRTPowerNotifySpinLock != NIL_RTSPINLOCK, VERR_WRONG_ORDER);
244 RT_ASSERT_INTS_ON();
245
246 /*
247 * Find and unlink the record from the list.
248 */
249 RTSpinlockAcquire(g_hRTPowerNotifySpinLock);
250 pPrev = NULL;
251 for (pCur = g_pRTPowerCallbackHead; pCur; pCur = pCur->pNext)
252 {
253 if ( pCur->pvUser == pvUser
254 && pCur->pfnCallback == pfnCallback)
255 break;
256 pPrev = pCur;
257 }
258 if (pCur)
259 {
260 if (pPrev)
261 pPrev->pNext = pCur->pNext;
262 else
263 g_pRTPowerCallbackHead = pCur->pNext;
264 ASMAtomicIncU32(&g_iRTPowerGeneration);
265 }
266 RTSpinlockRelease(g_hRTPowerNotifySpinLock);
267
268 if (!pCur)
269 return VERR_NOT_FOUND;
270
271 /*
272 * Invalidate and free the record.
273 */
274 pCur->pNext = NULL;
275 pCur->pfnCallback = NULL;
276 RTMemFree(pCur);
277
278 return VINF_SUCCESS;
279}
280RT_EXPORT_SYMBOL(RTPowerNotificationDeregister);
281
282
283DECLHIDDEN(int) rtR0PowerNotificationInit(void)
284{
285 int rc = RTSpinlockCreate((PRTSPINLOCK)&g_hRTPowerNotifySpinLock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "RTR0Power");
286 if (RT_SUCCESS(rc))
287 {
288 /** @todo OS specific init here */
289 return rc;
290#if 0
291 RTSpinlockDestroy(g_hRTPowerNotifySpinLock);
292 g_hRTPowerNotifySpinLock = NIL_RTSPINLOCK;
293#endif
294 }
295 return rc;
296}
297
298
299DECLHIDDEN(void) rtR0PowerNotificationTerm(void)
300{
301 PRTPOWERNOTIFYREG pHead;
302 RTSPINLOCK hSpinlock = g_hRTPowerNotifySpinLock;
303 AssertReturnVoid(hSpinlock != NIL_RTSPINLOCK);
304
305 /** @todo OS specific term here */
306
307 /* pick up the list and the spinlock. */
308 RTSpinlockAcquire(hSpinlock);
309 ASMAtomicWriteHandle(&g_hRTPowerNotifySpinLock, NIL_RTSPINLOCK);
310 pHead = g_pRTPowerCallbackHead;
311 g_pRTPowerCallbackHead = NULL;
312 ASMAtomicIncU32(&g_iRTPowerGeneration);
313 RTSpinlockRelease(hSpinlock);
314
315 /* free the list. */
316 while (pHead)
317 {
318 PRTPOWERNOTIFYREG pFree = pHead;
319 pHead = pHead->pNext;
320
321 pFree->pNext = NULL;
322 pFree->pfnCallback = NULL;
323 RTMemFree(pFree);
324 }
325
326 RTSpinlockDestroy(hSpinlock);
327}
328
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette