1a3bb9fccSApple OSS Distributions /*
2a3bb9fccSApple OSS Distributions * Copyright (c) 2012-2013 Apple Computer, Inc. All Rights Reserved.
3a3bb9fccSApple OSS Distributions *
4a3bb9fccSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5a3bb9fccSApple OSS Distributions *
6a3bb9fccSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7a3bb9fccSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8a3bb9fccSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9a3bb9fccSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10a3bb9fccSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11a3bb9fccSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12a3bb9fccSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13a3bb9fccSApple OSS Distributions * terms of an Apple operating system software license agreement.
14a3bb9fccSApple OSS Distributions *
15a3bb9fccSApple OSS Distributions * Please obtain a copy of the License at
16a3bb9fccSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17a3bb9fccSApple OSS Distributions *
18a3bb9fccSApple OSS Distributions * The Original Code and all software distributed under the License are
19a3bb9fccSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20a3bb9fccSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21a3bb9fccSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22a3bb9fccSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23a3bb9fccSApple OSS Distributions * Please see the License for the specific language governing rights and
24a3bb9fccSApple OSS Distributions * limitations under the License.
25a3bb9fccSApple OSS Distributions *
26a3bb9fccSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27a3bb9fccSApple OSS Distributions */
28a3bb9fccSApple OSS Distributions
29bb611c8fSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
30bb611c8fSApple OSS Distributions
31bb611c8fSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
32a3bb9fccSApple OSS Distributions #include <IOKit/IOKernelReportStructs.h>
33a3bb9fccSApple OSS Distributions #include <IOKit/IOKernelReporters.h>
34a3bb9fccSApple OSS Distributions #include "IOReporterDefs.h"
35a3bb9fccSApple OSS Distributions
36a3bb9fccSApple OSS Distributions
37a3bb9fccSApple OSS Distributions #define super IOReporter
38a3bb9fccSApple OSS Distributions OSDefineMetaClassAndStructors(IOStateReporter, IOReporter);
39a3bb9fccSApple OSS Distributions
40a3bb9fccSApple OSS Distributions
41a3bb9fccSApple OSS Distributions /* static */
42bb611c8fSApple OSS Distributions OSSharedPtr<IOStateReporter>
with(IOService * reportingService,IOReportCategories categories,int nstates,IOReportUnit unit)43a3bb9fccSApple OSS Distributions IOStateReporter::with(IOService *reportingService,
44a3bb9fccSApple OSS Distributions IOReportCategories categories,
45a3bb9fccSApple OSS Distributions int nstates,
4676e12aa3SApple OSS Distributions IOReportUnit unit /* = kIOReportUnitHWTicks*/)
47a3bb9fccSApple OSS Distributions {
48bb611c8fSApple OSS Distributions OSSharedPtr<IOStateReporter> reporter;
49a3bb9fccSApple OSS Distributions
50bb611c8fSApple OSS Distributions if (nstates > INT16_MAX) {
51bb611c8fSApple OSS Distributions return nullptr;
52bb611c8fSApple OSS Distributions }
53a3bb9fccSApple OSS Distributions
54bb611c8fSApple OSS Distributions reporter = OSMakeShared<IOStateReporter>();
55a5e72196SApple OSS Distributions if (!reporter) {
56bb611c8fSApple OSS Distributions return nullptr;
57a5e72196SApple OSS Distributions }
58a3bb9fccSApple OSS Distributions
59bb611c8fSApple OSS Distributions if (!reporter->initWith(reportingService, categories, (int16_t) nstates, unit)) {
60bb611c8fSApple OSS Distributions return nullptr;
61a3bb9fccSApple OSS Distributions }
62a3bb9fccSApple OSS Distributions
63bb611c8fSApple OSS Distributions return reporter;
64a3bb9fccSApple OSS Distributions }
65a3bb9fccSApple OSS Distributions
66a3bb9fccSApple OSS Distributions bool
initWith(IOService * reportingService,IOReportCategories categories,int16_t nstates,IOReportUnit unit)67a3bb9fccSApple OSS Distributions IOStateReporter::initWith(IOService *reportingService,
68a3bb9fccSApple OSS Distributions IOReportCategories categories,
69a3bb9fccSApple OSS Distributions int16_t nstates,
7076e12aa3SApple OSS Distributions IOReportUnit unit)
71a3bb9fccSApple OSS Distributions {
72a3bb9fccSApple OSS Distributions bool success = false;
73a3bb9fccSApple OSS Distributions
74a3bb9fccSApple OSS Distributions IOReportChannelType channelType = {
75a3bb9fccSApple OSS Distributions .categories = categories,
76a3bb9fccSApple OSS Distributions .report_format = kIOReportFormatState,
77a3bb9fccSApple OSS Distributions .nelements = static_cast<uint16_t>(nstates),
78a3bb9fccSApple OSS Distributions .element_idx = 0
79a3bb9fccSApple OSS Distributions };
80a3bb9fccSApple OSS Distributions
81a3bb9fccSApple OSS Distributions if (super::init(reportingService, channelType, unit) != true) {
82a3bb9fccSApple OSS Distributions IORLOG("ERROR super::initWith failed");
83a3bb9fccSApple OSS Distributions success = false;
84a3bb9fccSApple OSS Distributions goto finish;
85a3bb9fccSApple OSS Distributions }
86a3bb9fccSApple OSS Distributions
87a3bb9fccSApple OSS Distributions _currentStates = NULL;
88a3bb9fccSApple OSS Distributions _lastUpdateTimes = NULL;
89a3bb9fccSApple OSS Distributions
90a3bb9fccSApple OSS Distributions success = true;
91a3bb9fccSApple OSS Distributions
92a3bb9fccSApple OSS Distributions finish:
93a3bb9fccSApple OSS Distributions return success;
94a3bb9fccSApple OSS Distributions }
95a3bb9fccSApple OSS Distributions
96a3bb9fccSApple OSS Distributions
97a3bb9fccSApple OSS Distributions void
free(void)98a3bb9fccSApple OSS Distributions IOStateReporter::free(void)
99a3bb9fccSApple OSS Distributions {
100a3bb9fccSApple OSS Distributions if (_currentStates) {
101a3bb9fccSApple OSS Distributions PREFL_MEMOP_PANIC(_nChannels, int);
102*e6231be0SApple OSS Distributions IOFreeData(_currentStates, (size_t)_nChannels * sizeof(int));
103a3bb9fccSApple OSS Distributions }
104a3bb9fccSApple OSS Distributions if (_lastUpdateTimes) {
105a3bb9fccSApple OSS Distributions PREFL_MEMOP_PANIC(_nChannels, uint64_t);
106*e6231be0SApple OSS Distributions IOFreeData(_lastUpdateTimes, (size_t)_nChannels * sizeof(uint64_t));
107a3bb9fccSApple OSS Distributions }
108a3bb9fccSApple OSS Distributions
109a3bb9fccSApple OSS Distributions super::free();
110a3bb9fccSApple OSS Distributions }
111a3bb9fccSApple OSS Distributions
112a3bb9fccSApple OSS Distributions
113a3bb9fccSApple OSS Distributions IOReturn
handleSwapPrepare(int newNChannels)114a3bb9fccSApple OSS Distributions IOStateReporter::handleSwapPrepare(int newNChannels)
115a3bb9fccSApple OSS Distributions {
116a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
117a3bb9fccSApple OSS Distributions size_t newCurStatesSize, newTSSize;
118a3bb9fccSApple OSS Distributions
119a3bb9fccSApple OSS Distributions //IORLOG("handleSwapPrepare (state) _nChannels before = %u", _nChannels);
120a3bb9fccSApple OSS Distributions
121a3bb9fccSApple OSS Distributions IOREPORTER_CHECK_CONFIG_LOCK();
122a3bb9fccSApple OSS Distributions
123a3bb9fccSApple OSS Distributions if (_swapCurrentStates || _swapLastUpdateTimes) {
124a3bb9fccSApple OSS Distributions panic("IOStateReporter::_swap* already in use");
125a3bb9fccSApple OSS Distributions }
126a3bb9fccSApple OSS Distributions
127a3bb9fccSApple OSS Distributions // new currentStates buffer
128a3bb9fccSApple OSS Distributions PREFL_MEMOP_FAIL(newNChannels, int);
129a3bb9fccSApple OSS Distributions newCurStatesSize = (size_t)newNChannels * sizeof(int);
130*e6231be0SApple OSS Distributions _swapCurrentStates = (int*)IOMallocData(newCurStatesSize);
131a3bb9fccSApple OSS Distributions if (_swapCurrentStates == NULL) {
132a3bb9fccSApple OSS Distributions res = kIOReturnNoMemory; goto finish;
133a3bb9fccSApple OSS Distributions }
134a3bb9fccSApple OSS Distributions memset(_swapCurrentStates, -1, newCurStatesSize); // init w/"no state"
135a3bb9fccSApple OSS Distributions
136a3bb9fccSApple OSS Distributions // new timestamps buffer
137a3bb9fccSApple OSS Distributions PREFL_MEMOP_FAIL(newNChannels, uint64_t);
138a3bb9fccSApple OSS Distributions newTSSize = (size_t)newNChannels * sizeof(uint64_t);
139*e6231be0SApple OSS Distributions _swapLastUpdateTimes = (uint64_t *)IOMallocZeroData(newTSSize);
140a3bb9fccSApple OSS Distributions if (_swapLastUpdateTimes == NULL) {
141a3bb9fccSApple OSS Distributions res = kIOReturnNoMemory; goto finish;
142a3bb9fccSApple OSS Distributions }
143a3bb9fccSApple OSS Distributions
144a3bb9fccSApple OSS Distributions res = super::handleSwapPrepare(newNChannels);
145a3bb9fccSApple OSS Distributions
146a3bb9fccSApple OSS Distributions finish:
147a3bb9fccSApple OSS Distributions if (res) {
148a3bb9fccSApple OSS Distributions if (_swapCurrentStates) {
149*e6231be0SApple OSS Distributions IOFreeData(_swapCurrentStates, newCurStatesSize);
150a3bb9fccSApple OSS Distributions _swapCurrentStates = NULL;
151a3bb9fccSApple OSS Distributions }
152a3bb9fccSApple OSS Distributions if (_swapLastUpdateTimes) {
153*e6231be0SApple OSS Distributions IOFreeData(_swapLastUpdateTimes, newTSSize);
154a3bb9fccSApple OSS Distributions _swapLastUpdateTimes = NULL;
155a3bb9fccSApple OSS Distributions }
156a3bb9fccSApple OSS Distributions }
157a3bb9fccSApple OSS Distributions
158a3bb9fccSApple OSS Distributions return res;
159a3bb9fccSApple OSS Distributions }
160a3bb9fccSApple OSS Distributions
161a3bb9fccSApple OSS Distributions IOReturn
handleAddChannelSwap(uint64_t channelID,const OSSymbol * symChannelName)162a3bb9fccSApple OSS Distributions IOStateReporter::handleAddChannelSwap(uint64_t channelID,
163a3bb9fccSApple OSS Distributions const OSSymbol *symChannelName)
164a3bb9fccSApple OSS Distributions {
165a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
166a3bb9fccSApple OSS Distributions int cnt;
167a3bb9fccSApple OSS Distributions int *tmpCurStates;
168a3bb9fccSApple OSS Distributions uint64_t *tmpTimestamps;
169a3bb9fccSApple OSS Distributions bool swapComplete = false;
170a3bb9fccSApple OSS Distributions
171a3bb9fccSApple OSS Distributions //IORLOG("IOStateReporter::handleSwap");
172a3bb9fccSApple OSS Distributions
173a3bb9fccSApple OSS Distributions if (!_swapCurrentStates || !_swapLastUpdateTimes) {
174a3bb9fccSApple OSS Distributions IORLOG("IOReporter::handleSwap ERROR swap variables uninitialized!");
175a3bb9fccSApple OSS Distributions goto finish;
176a3bb9fccSApple OSS Distributions }
177a3bb9fccSApple OSS Distributions
178a3bb9fccSApple OSS Distributions IOREPORTER_CHECK_CONFIG_LOCK();
179a3bb9fccSApple OSS Distributions IOREPORTER_CHECK_LOCK();
180a3bb9fccSApple OSS Distributions
181a3bb9fccSApple OSS Distributions // Copy any existing buffers
182a3bb9fccSApple OSS Distributions if (_currentStates) {
183a3bb9fccSApple OSS Distributions PREFL_MEMOP_FAIL(_nChannels, int);
184a3bb9fccSApple OSS Distributions memcpy(_swapCurrentStates, _currentStates,
185a3bb9fccSApple OSS Distributions (size_t)_nChannels * sizeof(int));
186a3bb9fccSApple OSS Distributions
187a3bb9fccSApple OSS Distributions if (!_lastUpdateTimes) {
188a3bb9fccSApple OSS Distributions panic("IOStateReporter::handleAddChannelSwap _lastUpdateTimes unset despite non-NULL _currentStates");
189a3bb9fccSApple OSS Distributions }
190a3bb9fccSApple OSS Distributions PREFL_MEMOP_FAIL(_nChannels, uint64_t);
191a3bb9fccSApple OSS Distributions memcpy(_swapLastUpdateTimes, _lastUpdateTimes,
192a3bb9fccSApple OSS Distributions (size_t)_nChannels * sizeof(uint64_t));
193a3bb9fccSApple OSS Distributions }
194a3bb9fccSApple OSS Distributions
195a3bb9fccSApple OSS Distributions // Update principal instance variables, keep old values in _swap* for cleanup
196a3bb9fccSApple OSS Distributions tmpCurStates = _currentStates;
197a3bb9fccSApple OSS Distributions _currentStates = _swapCurrentStates;
198a3bb9fccSApple OSS Distributions _swapCurrentStates = tmpCurStates;
199a3bb9fccSApple OSS Distributions
200a3bb9fccSApple OSS Distributions tmpTimestamps = _lastUpdateTimes;
201a3bb9fccSApple OSS Distributions _lastUpdateTimes = _swapLastUpdateTimes;
202a3bb9fccSApple OSS Distributions _swapLastUpdateTimes = tmpTimestamps;
203a3bb9fccSApple OSS Distributions
204a3bb9fccSApple OSS Distributions swapComplete = true;
205a3bb9fccSApple OSS Distributions
206a3bb9fccSApple OSS Distributions // subclass success
207a3bb9fccSApple OSS Distributions
208a3bb9fccSApple OSS Distributions // invoke superclass(es): base class updates _nChannels & _nElements
209a3bb9fccSApple OSS Distributions res = super::handleAddChannelSwap(channelID, symChannelName);
210a3bb9fccSApple OSS Distributions if (res) {
211a3bb9fccSApple OSS Distributions IORLOG("handleSwap(state) ERROR super::handleSwap failed!");
212a3bb9fccSApple OSS Distributions goto finish;
213a3bb9fccSApple OSS Distributions }
214a3bb9fccSApple OSS Distributions
215a3bb9fccSApple OSS Distributions // Channel added successfully, initialize the new channel's state_ids to 0..nStates-1
216a3bb9fccSApple OSS Distributions for (cnt = 0; cnt < _channelDimension; cnt++) {
217a3bb9fccSApple OSS Distributions handleSetStateID(channelID, cnt, (uint64_t)cnt);
218a3bb9fccSApple OSS Distributions }
219a3bb9fccSApple OSS Distributions
220a3bb9fccSApple OSS Distributions finish:
221a3bb9fccSApple OSS Distributions if (res && swapComplete) {
222a3bb9fccSApple OSS Distributions // unswap so the unused buffers get cleaned up
223a3bb9fccSApple OSS Distributions tmpCurStates = _currentStates;
224a3bb9fccSApple OSS Distributions _currentStates = _swapCurrentStates;
225a3bb9fccSApple OSS Distributions _swapCurrentStates = tmpCurStates;
226a3bb9fccSApple OSS Distributions
227a3bb9fccSApple OSS Distributions tmpTimestamps = _lastUpdateTimes;
228a3bb9fccSApple OSS Distributions _lastUpdateTimes = _swapLastUpdateTimes;
229a3bb9fccSApple OSS Distributions _swapLastUpdateTimes = tmpTimestamps;
230a3bb9fccSApple OSS Distributions }
231a3bb9fccSApple OSS Distributions
232a3bb9fccSApple OSS Distributions return res;
233a3bb9fccSApple OSS Distributions }
234a3bb9fccSApple OSS Distributions
235a3bb9fccSApple OSS Distributions
236a3bb9fccSApple OSS Distributions void
handleSwapCleanup(int swapNChannels)237a3bb9fccSApple OSS Distributions IOStateReporter::handleSwapCleanup(int swapNChannels)
238a3bb9fccSApple OSS Distributions {
239a3bb9fccSApple OSS Distributions IOREPORTER_CHECK_CONFIG_LOCK();
240a3bb9fccSApple OSS Distributions
241a3bb9fccSApple OSS Distributions super::handleSwapCleanup(swapNChannels);
242a3bb9fccSApple OSS Distributions
243a3bb9fccSApple OSS Distributions if (_swapCurrentStates) {
244a3bb9fccSApple OSS Distributions PREFL_MEMOP_PANIC(swapNChannels, int);
245*e6231be0SApple OSS Distributions IOFreeData(_swapCurrentStates, (size_t)swapNChannels * sizeof(int));
246a3bb9fccSApple OSS Distributions _swapCurrentStates = NULL;
247a3bb9fccSApple OSS Distributions }
248a3bb9fccSApple OSS Distributions if (_swapLastUpdateTimes) {
249a3bb9fccSApple OSS Distributions PREFL_MEMOP_PANIC(swapNChannels, uint64_t);
250*e6231be0SApple OSS Distributions IOFreeData(_swapLastUpdateTimes, (size_t)swapNChannels * sizeof(uint64_t));
251a3bb9fccSApple OSS Distributions _swapLastUpdateTimes = NULL;
252a3bb9fccSApple OSS Distributions }
253a3bb9fccSApple OSS Distributions }
254a3bb9fccSApple OSS Distributions
255a3bb9fccSApple OSS Distributions
256a3bb9fccSApple OSS Distributions IOReturn
_getStateIndices(uint64_t channel_id,uint64_t state_id,int * channel_index,int * state_index)257a3bb9fccSApple OSS Distributions IOStateReporter::_getStateIndices(uint64_t channel_id,
258a3bb9fccSApple OSS Distributions uint64_t state_id,
259a3bb9fccSApple OSS Distributions int *channel_index,
260a3bb9fccSApple OSS Distributions int *state_index)
261a3bb9fccSApple OSS Distributions {
262a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
263a3bb9fccSApple OSS Distributions int cnt;
264a3bb9fccSApple OSS Distributions IOStateReportValues *values;
265a3bb9fccSApple OSS Distributions int element_index = 0;
266a3bb9fccSApple OSS Distributions
267a3bb9fccSApple OSS Distributions IOREPORTER_CHECK_LOCK();
268a3bb9fccSApple OSS Distributions
269a3bb9fccSApple OSS Distributions if (getChannelIndices(channel_id,
270a3bb9fccSApple OSS Distributions channel_index,
271a3bb9fccSApple OSS Distributions &element_index) != kIOReturnSuccess) {
272a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument;
273a3bb9fccSApple OSS Distributions
274a3bb9fccSApple OSS Distributions goto finish;
275a3bb9fccSApple OSS Distributions }
276a3bb9fccSApple OSS Distributions
277a3bb9fccSApple OSS Distributions for (cnt = 0; cnt < _channelDimension; cnt++) {
278a3bb9fccSApple OSS Distributions values = (IOStateReportValues *)getElementValues(element_index + cnt);
279a3bb9fccSApple OSS Distributions
280a3bb9fccSApple OSS Distributions if (values == NULL) {
281a3bb9fccSApple OSS Distributions res = kIOReturnError;
282a3bb9fccSApple OSS Distributions goto finish;
283a3bb9fccSApple OSS Distributions }
284a3bb9fccSApple OSS Distributions
285a3bb9fccSApple OSS Distributions if (values->state_id == state_id) {
286a3bb9fccSApple OSS Distributions *state_index = cnt;
287a3bb9fccSApple OSS Distributions res = kIOReturnSuccess;
288a3bb9fccSApple OSS Distributions goto finish;
289a3bb9fccSApple OSS Distributions }
290a3bb9fccSApple OSS Distributions }
291a3bb9fccSApple OSS Distributions
292a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument;
293a3bb9fccSApple OSS Distributions
294a3bb9fccSApple OSS Distributions finish:
295a3bb9fccSApple OSS Distributions return res;
296a3bb9fccSApple OSS Distributions }
297a3bb9fccSApple OSS Distributions
298a3bb9fccSApple OSS Distributions
299a3bb9fccSApple OSS Distributions IOReturn
setChannelState(uint64_t channel_id,uint64_t new_state_id)300a3bb9fccSApple OSS Distributions IOStateReporter::setChannelState(uint64_t channel_id,
301a3bb9fccSApple OSS Distributions uint64_t new_state_id)
302a3bb9fccSApple OSS Distributions {
303a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
304a3bb9fccSApple OSS Distributions int channel_index, new_state_index;
305a3bb9fccSApple OSS Distributions uint64_t last_intransition = 0;
306a3bb9fccSApple OSS Distributions uint64_t prev_state_residency = 0;
307a3bb9fccSApple OSS Distributions
308a3bb9fccSApple OSS Distributions lockReporter();
309a3bb9fccSApple OSS Distributions
310a3bb9fccSApple OSS Distributions if (_getStateIndices(channel_id, new_state_id, &channel_index, &new_state_index) == kIOReturnSuccess) {
311a3bb9fccSApple OSS Distributions res = handleSetStateByIndices(channel_index, new_state_index,
312a3bb9fccSApple OSS Distributions last_intransition,
313a3bb9fccSApple OSS Distributions prev_state_residency);
314a3bb9fccSApple OSS Distributions goto finish;
315a3bb9fccSApple OSS Distributions }
316a3bb9fccSApple OSS Distributions
317a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument;
318a3bb9fccSApple OSS Distributions
319a3bb9fccSApple OSS Distributions finish:
320a3bb9fccSApple OSS Distributions unlockReporter();
321a3bb9fccSApple OSS Distributions return res;
322a3bb9fccSApple OSS Distributions }
323a3bb9fccSApple OSS Distributions
324a3bb9fccSApple OSS Distributions IOReturn
setChannelState(uint64_t channel_id,uint64_t new_state_id,uint64_t last_intransition,uint64_t prev_state_residency)325a3bb9fccSApple OSS Distributions IOStateReporter::setChannelState(uint64_t channel_id,
326a3bb9fccSApple OSS Distributions uint64_t new_state_id,
327a3bb9fccSApple OSS Distributions uint64_t last_intransition,
328a3bb9fccSApple OSS Distributions uint64_t prev_state_residency)
329a3bb9fccSApple OSS Distributions {
330a3bb9fccSApple OSS Distributions return setChannelState(channel_id, new_state_id);
331a3bb9fccSApple OSS Distributions }
332a3bb9fccSApple OSS Distributions
333a3bb9fccSApple OSS Distributions IOReturn
overrideChannelState(uint64_t channel_id,uint64_t state_id,uint64_t time_in_state,uint64_t intransitions,uint64_t last_intransition)334a3bb9fccSApple OSS Distributions IOStateReporter::overrideChannelState(uint64_t channel_id,
335a3bb9fccSApple OSS Distributions uint64_t state_id,
336a3bb9fccSApple OSS Distributions uint64_t time_in_state,
337a3bb9fccSApple OSS Distributions uint64_t intransitions,
338a3bb9fccSApple OSS Distributions uint64_t last_intransition /*=0*/)
339a3bb9fccSApple OSS Distributions {
340a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
341a3bb9fccSApple OSS Distributions int channel_index, state_index;
342a3bb9fccSApple OSS Distributions
343a3bb9fccSApple OSS Distributions lockReporter();
344a3bb9fccSApple OSS Distributions
345a3bb9fccSApple OSS Distributions if (_getStateIndices(channel_id, state_id, &channel_index, &state_index) == kIOReturnSuccess) {
346a3bb9fccSApple OSS Distributions if (_lastUpdateTimes[channel_index]) {
347*e6231be0SApple OSS Distributions panic("overrideChannelState() cannot be used after setChannelState()!");
348a3bb9fccSApple OSS Distributions }
349a3bb9fccSApple OSS Distributions
350a3bb9fccSApple OSS Distributions res = handleOverrideChannelStateByIndices(channel_index, state_index,
351a3bb9fccSApple OSS Distributions time_in_state, intransitions,
352a3bb9fccSApple OSS Distributions last_intransition);
353a3bb9fccSApple OSS Distributions goto finish;
354a3bb9fccSApple OSS Distributions }
355a3bb9fccSApple OSS Distributions
356a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument;
357a3bb9fccSApple OSS Distributions
358a3bb9fccSApple OSS Distributions finish:
359a3bb9fccSApple OSS Distributions unlockReporter();
360a3bb9fccSApple OSS Distributions return res;
361a3bb9fccSApple OSS Distributions }
362a3bb9fccSApple OSS Distributions
363a3bb9fccSApple OSS Distributions
364a3bb9fccSApple OSS Distributions IOReturn
handleOverrideChannelStateByIndices(int channel_index,int state_index,uint64_t time_in_state,uint64_t intransitions,uint64_t last_intransition)365a3bb9fccSApple OSS Distributions IOStateReporter::handleOverrideChannelStateByIndices(int channel_index,
366a3bb9fccSApple OSS Distributions int state_index,
367a3bb9fccSApple OSS Distributions uint64_t time_in_state,
368a3bb9fccSApple OSS Distributions uint64_t intransitions,
369a3bb9fccSApple OSS Distributions uint64_t last_intransition /*=0*/)
370a3bb9fccSApple OSS Distributions {
371a3bb9fccSApple OSS Distributions IOReturn kerr, result = kIOReturnError;
372a3bb9fccSApple OSS Distributions IOStateReportValues state_values;
373a3bb9fccSApple OSS Distributions int element_index;
374a3bb9fccSApple OSS Distributions
375a3bb9fccSApple OSS Distributions if (channel_index < 0 || channel_index >= _nChannels) {
376a3bb9fccSApple OSS Distributions result = kIOReturnBadArgument; goto finish;
377a3bb9fccSApple OSS Distributions }
378a3bb9fccSApple OSS Distributions
379a3bb9fccSApple OSS Distributions if (channel_index < 0 || channel_index > (_nElements - state_index)
380a3bb9fccSApple OSS Distributions / _channelDimension) {
381a3bb9fccSApple OSS Distributions result = kIOReturnOverrun; goto finish;
382a3bb9fccSApple OSS Distributions }
383a3bb9fccSApple OSS Distributions element_index = channel_index * _channelDimension + state_index;
384a3bb9fccSApple OSS Distributions
385a3bb9fccSApple OSS Distributions kerr = copyElementValues(element_index, (IOReportElementValues*)&state_values);
386a3bb9fccSApple OSS Distributions if (kerr) {
387a3bb9fccSApple OSS Distributions result = kerr; goto finish;
388a3bb9fccSApple OSS Distributions }
389a3bb9fccSApple OSS Distributions
390a3bb9fccSApple OSS Distributions // last_intransition = 0 -> no current state ("residency summary only")
391a3bb9fccSApple OSS Distributions state_values.last_intransition = last_intransition;
392a3bb9fccSApple OSS Distributions state_values.intransitions = intransitions;
393a3bb9fccSApple OSS Distributions state_values.upticks = time_in_state;
394a3bb9fccSApple OSS Distributions
395a3bb9fccSApple OSS Distributions // determines current time for metadata
396a3bb9fccSApple OSS Distributions kerr = setElementValues(element_index, (IOReportElementValues *)&state_values);
397a3bb9fccSApple OSS Distributions if (kerr) {
398a3bb9fccSApple OSS Distributions result = kerr; goto finish;
399a3bb9fccSApple OSS Distributions }
400a3bb9fccSApple OSS Distributions
401a3bb9fccSApple OSS Distributions // success
402a3bb9fccSApple OSS Distributions result = kIOReturnSuccess;
403a3bb9fccSApple OSS Distributions
404a3bb9fccSApple OSS Distributions finish:
405a3bb9fccSApple OSS Distributions return result;
406a3bb9fccSApple OSS Distributions }
407a3bb9fccSApple OSS Distributions
408a3bb9fccSApple OSS Distributions
409a3bb9fccSApple OSS Distributions IOReturn
incrementChannelState(uint64_t channel_id,uint64_t state_id,uint64_t time_in_state,uint64_t intransitions,uint64_t last_intransition)410a3bb9fccSApple OSS Distributions IOStateReporter::incrementChannelState(uint64_t channel_id,
411a3bb9fccSApple OSS Distributions uint64_t state_id,
412a3bb9fccSApple OSS Distributions uint64_t time_in_state,
413a3bb9fccSApple OSS Distributions uint64_t intransitions,
414a3bb9fccSApple OSS Distributions uint64_t last_intransition /*=0*/)
415a3bb9fccSApple OSS Distributions {
416a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
417a3bb9fccSApple OSS Distributions int channel_index, state_index;
418a3bb9fccSApple OSS Distributions
419a3bb9fccSApple OSS Distributions lockReporter();
420a3bb9fccSApple OSS Distributions
421a3bb9fccSApple OSS Distributions if (_getStateIndices(channel_id, state_id, &channel_index, &state_index) == kIOReturnSuccess) {
422a3bb9fccSApple OSS Distributions if (_lastUpdateTimes[channel_index]) {
423*e6231be0SApple OSS Distributions panic("incrementChannelState() cannot be used after setChannelState()!");
424a3bb9fccSApple OSS Distributions }
425a3bb9fccSApple OSS Distributions
426a3bb9fccSApple OSS Distributions res = handleIncrementChannelStateByIndices(channel_index, state_index,
427a3bb9fccSApple OSS Distributions time_in_state, intransitions,
428a3bb9fccSApple OSS Distributions last_intransition);
429a3bb9fccSApple OSS Distributions goto finish;
430a3bb9fccSApple OSS Distributions }
431a3bb9fccSApple OSS Distributions
432a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument;
433a3bb9fccSApple OSS Distributions
434a3bb9fccSApple OSS Distributions finish:
435a3bb9fccSApple OSS Distributions unlockReporter();
436a3bb9fccSApple OSS Distributions return res;
437a3bb9fccSApple OSS Distributions }
438a3bb9fccSApple OSS Distributions
439a3bb9fccSApple OSS Distributions
440a3bb9fccSApple OSS Distributions IOReturn
handleIncrementChannelStateByIndices(int channel_index,int state_index,uint64_t time_in_state,uint64_t intransitions,uint64_t last_intransition)441a3bb9fccSApple OSS Distributions IOStateReporter::handleIncrementChannelStateByIndices(int channel_index,
442a3bb9fccSApple OSS Distributions int state_index,
443a3bb9fccSApple OSS Distributions uint64_t time_in_state,
444a3bb9fccSApple OSS Distributions uint64_t intransitions,
445a3bb9fccSApple OSS Distributions uint64_t last_intransition /*=0*/)
446a3bb9fccSApple OSS Distributions {
447a3bb9fccSApple OSS Distributions IOReturn kerr, result = kIOReturnError;
448a3bb9fccSApple OSS Distributions IOStateReportValues state_values;
449a3bb9fccSApple OSS Distributions int element_index;
450a3bb9fccSApple OSS Distributions
451a3bb9fccSApple OSS Distributions if (channel_index < 0 || channel_index >= _nChannels) {
452a3bb9fccSApple OSS Distributions result = kIOReturnBadArgument; goto finish;
453a3bb9fccSApple OSS Distributions }
454a3bb9fccSApple OSS Distributions
455a3bb9fccSApple OSS Distributions if (channel_index < 0 || channel_index > (_nElements - state_index)
456a3bb9fccSApple OSS Distributions / _channelDimension) {
457a3bb9fccSApple OSS Distributions result = kIOReturnOverrun; goto finish;
458a3bb9fccSApple OSS Distributions }
459a3bb9fccSApple OSS Distributions element_index = channel_index * _channelDimension + state_index;
460a3bb9fccSApple OSS Distributions
461a3bb9fccSApple OSS Distributions kerr = copyElementValues(element_index, (IOReportElementValues*)&state_values);
462a3bb9fccSApple OSS Distributions if (kerr) {
463a3bb9fccSApple OSS Distributions result = kerr;
464a3bb9fccSApple OSS Distributions goto finish;
465a3bb9fccSApple OSS Distributions }
466a3bb9fccSApple OSS Distributions
467a3bb9fccSApple OSS Distributions state_values.last_intransition = last_intransition;
468a3bb9fccSApple OSS Distributions state_values.intransitions += intransitions;
469a3bb9fccSApple OSS Distributions state_values.upticks += time_in_state;
470a3bb9fccSApple OSS Distributions
471a3bb9fccSApple OSS Distributions // determines current time for metadata
472a3bb9fccSApple OSS Distributions kerr = setElementValues(element_index, (IOReportElementValues *)&state_values);
473a3bb9fccSApple OSS Distributions if (kerr) {
474a3bb9fccSApple OSS Distributions result = kerr;
475a3bb9fccSApple OSS Distributions goto finish;
476a3bb9fccSApple OSS Distributions }
477a3bb9fccSApple OSS Distributions
478a3bb9fccSApple OSS Distributions // success
479a3bb9fccSApple OSS Distributions result = kIOReturnSuccess;
480a3bb9fccSApple OSS Distributions
481a3bb9fccSApple OSS Distributions finish:
482a3bb9fccSApple OSS Distributions return result;
483a3bb9fccSApple OSS Distributions }
484a3bb9fccSApple OSS Distributions
485a3bb9fccSApple OSS Distributions
486a3bb9fccSApple OSS Distributions IOReturn
setState(uint64_t new_state_id)487a3bb9fccSApple OSS Distributions IOStateReporter::setState(uint64_t new_state_id)
488a3bb9fccSApple OSS Distributions {
489a3bb9fccSApple OSS Distributions uint64_t last_intransition = 0;
490a3bb9fccSApple OSS Distributions uint64_t prev_state_residency = 0;
491a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
492a3bb9fccSApple OSS Distributions IOStateReportValues *values;
493a3bb9fccSApple OSS Distributions int channel_index = 0, element_index = 0, new_state_index = 0;
494a3bb9fccSApple OSS Distributions int cnt;
495a3bb9fccSApple OSS Distributions
496a3bb9fccSApple OSS Distributions lockReporter();
497a3bb9fccSApple OSS Distributions
498a3bb9fccSApple OSS Distributions if (_nChannels == 1) {
499a3bb9fccSApple OSS Distributions for (cnt = 0; cnt < _channelDimension; cnt++) {
500a3bb9fccSApple OSS Distributions new_state_index = element_index + cnt;
501a3bb9fccSApple OSS Distributions
502a3bb9fccSApple OSS Distributions values = (IOStateReportValues *)getElementValues(new_state_index);
503a3bb9fccSApple OSS Distributions
504a3bb9fccSApple OSS Distributions if (values == NULL) {
505a3bb9fccSApple OSS Distributions res = kIOReturnError;
506a3bb9fccSApple OSS Distributions goto finish;
507a3bb9fccSApple OSS Distributions }
508a3bb9fccSApple OSS Distributions
509a3bb9fccSApple OSS Distributions if (values->state_id == new_state_id) {
510a3bb9fccSApple OSS Distributions res = handleSetStateByIndices(channel_index, new_state_index,
511a3bb9fccSApple OSS Distributions last_intransition,
512a3bb9fccSApple OSS Distributions prev_state_residency);
513a3bb9fccSApple OSS Distributions goto finish;
514a3bb9fccSApple OSS Distributions }
515a3bb9fccSApple OSS Distributions }
516a3bb9fccSApple OSS Distributions }
517a3bb9fccSApple OSS Distributions
518a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument;
519a3bb9fccSApple OSS Distributions
520a3bb9fccSApple OSS Distributions finish:
521a3bb9fccSApple OSS Distributions unlockReporter();
522a3bb9fccSApple OSS Distributions return res;
523a3bb9fccSApple OSS Distributions }
524a3bb9fccSApple OSS Distributions
525a3bb9fccSApple OSS Distributions IOReturn
setState(uint64_t new_state_id,uint64_t last_intransition,uint64_t prev_state_residency)526a3bb9fccSApple OSS Distributions IOStateReporter::setState(uint64_t new_state_id,
527a3bb9fccSApple OSS Distributions uint64_t last_intransition,
528a3bb9fccSApple OSS Distributions uint64_t prev_state_residency)
529a3bb9fccSApple OSS Distributions {
530a3bb9fccSApple OSS Distributions return setState(new_state_id);
531a3bb9fccSApple OSS Distributions }
532a3bb9fccSApple OSS Distributions
533a3bb9fccSApple OSS Distributions IOReturn
setStateID(uint64_t channel_id,int state_index,uint64_t state_id)534a3bb9fccSApple OSS Distributions IOStateReporter::setStateID(uint64_t channel_id,
535a3bb9fccSApple OSS Distributions int state_index,
536a3bb9fccSApple OSS Distributions uint64_t state_id)
537a3bb9fccSApple OSS Distributions {
538a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
539a3bb9fccSApple OSS Distributions
540a3bb9fccSApple OSS Distributions lockReporter();
541a3bb9fccSApple OSS Distributions
542a3bb9fccSApple OSS Distributions res = handleSetStateID(channel_id, state_index, state_id);
543a3bb9fccSApple OSS Distributions
544a3bb9fccSApple OSS Distributions unlockReporter();
545a3bb9fccSApple OSS Distributions
546a3bb9fccSApple OSS Distributions return res;
547a3bb9fccSApple OSS Distributions }
548a3bb9fccSApple OSS Distributions
549a3bb9fccSApple OSS Distributions
550a3bb9fccSApple OSS Distributions IOReturn
handleSetStateID(uint64_t channel_id,int state_index,uint64_t state_id)551a3bb9fccSApple OSS Distributions IOStateReporter::handleSetStateID(uint64_t channel_id,
552a3bb9fccSApple OSS Distributions int state_index,
553a3bb9fccSApple OSS Distributions uint64_t state_id)
554a3bb9fccSApple OSS Distributions {
555a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
556a3bb9fccSApple OSS Distributions IOStateReportValues state_values;
557a3bb9fccSApple OSS Distributions int element_index = 0;
558a3bb9fccSApple OSS Distributions
559a3bb9fccSApple OSS Distributions IOREPORTER_CHECK_LOCK();
560a3bb9fccSApple OSS Distributions
561a3bb9fccSApple OSS Distributions if (getFirstElementIndex(channel_id, &element_index) == kIOReturnSuccess) {
562a3bb9fccSApple OSS Distributions if (state_index >= _channelDimension) {
563a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument; goto finish;
564a3bb9fccSApple OSS Distributions }
565a3bb9fccSApple OSS Distributions if (_nElements - state_index <= element_index) {
566a3bb9fccSApple OSS Distributions res = kIOReturnOverrun; goto finish;
567a3bb9fccSApple OSS Distributions }
568a3bb9fccSApple OSS Distributions element_index += state_index;
569a3bb9fccSApple OSS Distributions
570a3bb9fccSApple OSS Distributions if (copyElementValues(element_index, (IOReportElementValues *)&state_values) != kIOReturnSuccess) {
571a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument;
572a3bb9fccSApple OSS Distributions goto finish;
573a3bb9fccSApple OSS Distributions }
574a3bb9fccSApple OSS Distributions
575a3bb9fccSApple OSS Distributions state_values.state_id = state_id;
576a3bb9fccSApple OSS Distributions
577a3bb9fccSApple OSS Distributions res = setElementValues(element_index, (IOReportElementValues *)&state_values);
578a3bb9fccSApple OSS Distributions }
579a3bb9fccSApple OSS Distributions
580a3bb9fccSApple OSS Distributions // FIXME: set a bit somewhere (reporter-wide?) that state_ids can no longer be
581a3bb9fccSApple OSS Distributions // assumed to be contiguous
582a3bb9fccSApple OSS Distributions finish:
583a3bb9fccSApple OSS Distributions return res;
584a3bb9fccSApple OSS Distributions }
585a3bb9fccSApple OSS Distributions
586a3bb9fccSApple OSS Distributions IOReturn
setStateByIndices(int channel_index,int new_state_index)587a3bb9fccSApple OSS Distributions IOStateReporter::setStateByIndices(int channel_index,
588a3bb9fccSApple OSS Distributions int new_state_index)
589a3bb9fccSApple OSS Distributions {
590a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
591a3bb9fccSApple OSS Distributions uint64_t last_intransition = 0;
592a3bb9fccSApple OSS Distributions uint64_t prev_state_residency = 0;
593a3bb9fccSApple OSS Distributions
594a3bb9fccSApple OSS Distributions lockReporter();
595a3bb9fccSApple OSS Distributions
596a3bb9fccSApple OSS Distributions res = handleSetStateByIndices(channel_index, new_state_index,
597a3bb9fccSApple OSS Distributions last_intransition, prev_state_residency);
598a3bb9fccSApple OSS Distributions
599a3bb9fccSApple OSS Distributions unlockReporter();
600a3bb9fccSApple OSS Distributions
601a3bb9fccSApple OSS Distributions return res;
602a3bb9fccSApple OSS Distributions }
603a3bb9fccSApple OSS Distributions
604a3bb9fccSApple OSS Distributions IOReturn
setStateByIndices(int channel_index,int new_state_index,uint64_t last_intransition,uint64_t prev_state_residency)605a3bb9fccSApple OSS Distributions IOStateReporter::setStateByIndices(int channel_index,
606a3bb9fccSApple OSS Distributions int new_state_index,
607a3bb9fccSApple OSS Distributions uint64_t last_intransition,
608a3bb9fccSApple OSS Distributions uint64_t prev_state_residency)
609a3bb9fccSApple OSS Distributions {
610a3bb9fccSApple OSS Distributions return setStateByIndices(channel_index, new_state_index);
611a3bb9fccSApple OSS Distributions }
612a3bb9fccSApple OSS Distributions
613a3bb9fccSApple OSS Distributions IOReturn
handleSetStateByIndices(int channel_index,int new_state_index,uint64_t last_intransition,uint64_t prev_state_residency)614a3bb9fccSApple OSS Distributions IOStateReporter::handleSetStateByIndices(int channel_index,
615a3bb9fccSApple OSS Distributions int new_state_index,
616a3bb9fccSApple OSS Distributions uint64_t last_intransition,
617a3bb9fccSApple OSS Distributions uint64_t prev_state_residency)
618a3bb9fccSApple OSS Distributions {
619a3bb9fccSApple OSS Distributions IOReturn res = kIOReturnError;
620a3bb9fccSApple OSS Distributions
621a3bb9fccSApple OSS Distributions IOStateReportValues curr_state_values, new_state_values;
622a3bb9fccSApple OSS Distributions int curr_state_index = 0;
623a3bb9fccSApple OSS Distributions int curr_element_index, new_element_index;
624a3bb9fccSApple OSS Distributions uint64_t last_ch_update_time = 0;
625a3bb9fccSApple OSS Distributions uint64_t recordTime = mach_absolute_time();
626a3bb9fccSApple OSS Distributions
627a3bb9fccSApple OSS Distributions IOREPORTER_CHECK_LOCK();
628a3bb9fccSApple OSS Distributions
629a3bb9fccSApple OSS Distributions if (channel_index < 0 || channel_index >= _nChannels) {
630a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument; goto finish;
631a3bb9fccSApple OSS Distributions }
632a3bb9fccSApple OSS Distributions
633a3bb9fccSApple OSS Distributions // if no timestamp provided, last_intransition = time of recording (now)
634a3bb9fccSApple OSS Distributions if (last_intransition == 0) {
635a3bb9fccSApple OSS Distributions last_intransition = recordTime;
636a3bb9fccSApple OSS Distributions }
637a3bb9fccSApple OSS Distributions
638a3bb9fccSApple OSS Distributions // First update target state if different than the current state
639a3bb9fccSApple OSS Distributions // _currentStates[] initialized to -1 to detect first state transition
640a3bb9fccSApple OSS Distributions curr_state_index = _currentStates[channel_index];
641a3bb9fccSApple OSS Distributions if (new_state_index != curr_state_index) {
642a3bb9fccSApple OSS Distributions // fetch element data
643a3bb9fccSApple OSS Distributions if (channel_index < 0 || channel_index > (_nElements - new_state_index)
644a3bb9fccSApple OSS Distributions / _channelDimension) {
645a3bb9fccSApple OSS Distributions res = kIOReturnOverrun; goto finish;
646a3bb9fccSApple OSS Distributions }
647a3bb9fccSApple OSS Distributions new_element_index = channel_index * _channelDimension + new_state_index;
648a3bb9fccSApple OSS Distributions if (copyElementValues(new_element_index,
649a3bb9fccSApple OSS Distributions (IOReportElementValues *)&new_state_values)) {
650a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument;
651a3bb9fccSApple OSS Distributions goto finish;
652a3bb9fccSApple OSS Distributions }
653a3bb9fccSApple OSS Distributions
654a3bb9fccSApple OSS Distributions // Update new state's transition info
655a3bb9fccSApple OSS Distributions new_state_values.intransitions += 1;
656a3bb9fccSApple OSS Distributions new_state_values.last_intransition = last_intransition;
657a3bb9fccSApple OSS Distributions
658a3bb9fccSApple OSS Distributions // and store the values
659a3bb9fccSApple OSS Distributions res = setElementValues(new_element_index,
660a3bb9fccSApple OSS Distributions (IOReportElementValues *)&new_state_values,
661a3bb9fccSApple OSS Distributions recordTime);
662a3bb9fccSApple OSS Distributions
663a3bb9fccSApple OSS Distributions if (res != kIOReturnSuccess) {
664a3bb9fccSApple OSS Distributions goto finish;
665a3bb9fccSApple OSS Distributions }
666a3bb9fccSApple OSS Distributions
667a3bb9fccSApple OSS Distributions _currentStates[channel_index] = new_state_index;
668a3bb9fccSApple OSS Distributions }
669a3bb9fccSApple OSS Distributions
670a3bb9fccSApple OSS Distributions /* Now update time spent in any previous state
671a5e72196SApple OSS Distributions * If new_state_index = curr_state_index, this updates time in the
672a5e72196SApple OSS Distributions * current state. If this is the channel's first state transition,
673a5e72196SApple OSS Distributions * the last update time will be zero.
674a5e72196SApple OSS Distributions *
675a5e72196SApple OSS Distributions * Note: While setState() should never be called on a channel being
676a5e72196SApple OSS Distributions * updated with increment/overrideChannelState(), that's another way
677a5e72196SApple OSS Distributions * that the last update time might not exist. Regardless, if there
678a5e72196SApple OSS Distributions * is no basis for determining time spent in previous state, there's
679a5e72196SApple OSS Distributions * nothing to update!
680a3bb9fccSApple OSS Distributions */
681a3bb9fccSApple OSS Distributions last_ch_update_time = _lastUpdateTimes[channel_index];
682a3bb9fccSApple OSS Distributions if (last_ch_update_time != 0) {
683a3bb9fccSApple OSS Distributions if (channel_index < 0 || channel_index > (_nElements - curr_state_index)
684a3bb9fccSApple OSS Distributions / _channelDimension) {
685a3bb9fccSApple OSS Distributions res = kIOReturnOverrun; goto finish;
686a3bb9fccSApple OSS Distributions }
687a3bb9fccSApple OSS Distributions curr_element_index = channel_index * _channelDimension + curr_state_index;
688a3bb9fccSApple OSS Distributions if (copyElementValues(curr_element_index,
689a3bb9fccSApple OSS Distributions (IOReportElementValues *)&curr_state_values)) {
690a3bb9fccSApple OSS Distributions res = kIOReturnBadArgument;
691a3bb9fccSApple OSS Distributions goto finish;
692a3bb9fccSApple OSS Distributions }
693a3bb9fccSApple OSS Distributions // compute the time spent in previous state, unless provided
694a3bb9fccSApple OSS Distributions if (prev_state_residency == 0) {
695a3bb9fccSApple OSS Distributions prev_state_residency = last_intransition - last_ch_update_time;
696a3bb9fccSApple OSS Distributions }
697a3bb9fccSApple OSS Distributions
698a3bb9fccSApple OSS Distributions curr_state_values.upticks += prev_state_residency;
699a3bb9fccSApple OSS Distributions
700a3bb9fccSApple OSS Distributions res = setElementValues(curr_element_index,
701a3bb9fccSApple OSS Distributions (IOReportElementValues*)&curr_state_values,
702a3bb9fccSApple OSS Distributions recordTime);
703a3bb9fccSApple OSS Distributions
704a3bb9fccSApple OSS Distributions if (res != kIOReturnSuccess) {
705a3bb9fccSApple OSS Distributions goto finish;
706a3bb9fccSApple OSS Distributions }
707a3bb9fccSApple OSS Distributions }
708a3bb9fccSApple OSS Distributions
709a3bb9fccSApple OSS Distributions // record basis for next "time in prior state" calculation
710a3bb9fccSApple OSS Distributions // (also arms a panic in override/incrementChannelState())
711a3bb9fccSApple OSS Distributions _lastUpdateTimes[channel_index] = last_intransition;
712a3bb9fccSApple OSS Distributions
713a3bb9fccSApple OSS Distributions finish:
714a3bb9fccSApple OSS Distributions return res;
715a3bb9fccSApple OSS Distributions }
716a3bb9fccSApple OSS Distributions
717a3bb9fccSApple OSS Distributions
718a3bb9fccSApple OSS Distributions // blocks might make this slightly easier?
719a3bb9fccSApple OSS Distributions uint64_t
getStateInTransitions(uint64_t channel_id,uint64_t state_id)720a3bb9fccSApple OSS Distributions IOStateReporter::getStateInTransitions(uint64_t channel_id,
721a3bb9fccSApple OSS Distributions uint64_t state_id)
722a3bb9fccSApple OSS Distributions {
723a3bb9fccSApple OSS Distributions return _getStateValue(channel_id, state_id, kInTransitions);
724a3bb9fccSApple OSS Distributions }
725a3bb9fccSApple OSS Distributions
726a3bb9fccSApple OSS Distributions uint64_t
getStateResidencyTime(uint64_t channel_id,uint64_t state_id)727a3bb9fccSApple OSS Distributions IOStateReporter::getStateResidencyTime(uint64_t channel_id,
728a3bb9fccSApple OSS Distributions uint64_t state_id)
729a3bb9fccSApple OSS Distributions {
730a3bb9fccSApple OSS Distributions return _getStateValue(channel_id, state_id, kResidencyTime);
731a3bb9fccSApple OSS Distributions }
732a3bb9fccSApple OSS Distributions
733a3bb9fccSApple OSS Distributions uint64_t
getStateLastTransitionTime(uint64_t channel_id,uint64_t state_id)734a3bb9fccSApple OSS Distributions IOStateReporter::getStateLastTransitionTime(uint64_t channel_id,
735a3bb9fccSApple OSS Distributions uint64_t state_id)
736a3bb9fccSApple OSS Distributions {
737a3bb9fccSApple OSS Distributions return _getStateValue(channel_id, state_id, kLastTransitionTime);
738a3bb9fccSApple OSS Distributions }
739a3bb9fccSApple OSS Distributions
740a3bb9fccSApple OSS Distributions uint64_t
_getStateValue(uint64_t channel_id,uint64_t state_id,enum valueSelector value)741a3bb9fccSApple OSS Distributions IOStateReporter::_getStateValue(uint64_t channel_id,
742a3bb9fccSApple OSS Distributions uint64_t state_id,
743a3bb9fccSApple OSS Distributions enum valueSelector value)
744a3bb9fccSApple OSS Distributions {
745a3bb9fccSApple OSS Distributions int channel_index = 0, element_index = 0, cnt;
746a3bb9fccSApple OSS Distributions IOStateReportValues *values = NULL;
747a3bb9fccSApple OSS Distributions uint64_t result = kIOReportInvalidValue;
748a3bb9fccSApple OSS Distributions
749a3bb9fccSApple OSS Distributions lockReporter();
750a3bb9fccSApple OSS Distributions
751a3bb9fccSApple OSS Distributions if (getChannelIndices(channel_id, &channel_index, &element_index) == kIOReturnSuccess) {
752a3bb9fccSApple OSS Distributions if (updateChannelValues(channel_index) == kIOReturnSuccess) {
753a3bb9fccSApple OSS Distributions for (cnt = 0; cnt < _channelDimension; cnt++) {
754a3bb9fccSApple OSS Distributions values = (IOStateReportValues *)getElementValues(element_index);
755a3bb9fccSApple OSS Distributions
756a3bb9fccSApple OSS Distributions if (state_id == values->state_id) {
757a3bb9fccSApple OSS Distributions switch (value) {
758a3bb9fccSApple OSS Distributions case kInTransitions:
759a3bb9fccSApple OSS Distributions result = values->intransitions;
760a3bb9fccSApple OSS Distributions break;
761a3bb9fccSApple OSS Distributions case kResidencyTime:
762a3bb9fccSApple OSS Distributions result = values->upticks;
763a3bb9fccSApple OSS Distributions break;
764a3bb9fccSApple OSS Distributions case kLastTransitionTime:
765a3bb9fccSApple OSS Distributions result = values->last_intransition;
76688cc0b97SApple OSS Distributions break;
767a3bb9fccSApple OSS Distributions default:
768a3bb9fccSApple OSS Distributions break;
769a3bb9fccSApple OSS Distributions }
770a3bb9fccSApple OSS Distributions
771a3bb9fccSApple OSS Distributions break;
772a3bb9fccSApple OSS Distributions }
773a3bb9fccSApple OSS Distributions
774a3bb9fccSApple OSS Distributions element_index++;
775a3bb9fccSApple OSS Distributions }
776a3bb9fccSApple OSS Distributions }
777a3bb9fccSApple OSS Distributions }
778a3bb9fccSApple OSS Distributions
779a3bb9fccSApple OSS Distributions unlockReporter();
780a3bb9fccSApple OSS Distributions return result;
781a3bb9fccSApple OSS Distributions }
782a3bb9fccSApple OSS Distributions
783a3bb9fccSApple OSS Distributions
784a3bb9fccSApple OSS Distributions uint64_t
getStateLastChannelUpdateTime(uint64_t channel_id)785a3bb9fccSApple OSS Distributions IOStateReporter::getStateLastChannelUpdateTime(uint64_t channel_id)
786a3bb9fccSApple OSS Distributions {
787a3bb9fccSApple OSS Distributions int channel_index;
788a3bb9fccSApple OSS Distributions uint64_t result = kIOReportInvalidValue;
789a3bb9fccSApple OSS Distributions
790a3bb9fccSApple OSS Distributions lockReporter();
791a3bb9fccSApple OSS Distributions
792a3bb9fccSApple OSS Distributions if (getChannelIndex(channel_id, &channel_index) == kIOReturnSuccess) {
793a3bb9fccSApple OSS Distributions result = _lastUpdateTimes[channel_index];
794a3bb9fccSApple OSS Distributions }
795a3bb9fccSApple OSS Distributions
796a3bb9fccSApple OSS Distributions unlockReporter();
797a3bb9fccSApple OSS Distributions
798a3bb9fccSApple OSS Distributions return result;
799a3bb9fccSApple OSS Distributions }
800a3bb9fccSApple OSS Distributions
801a3bb9fccSApple OSS Distributions
802a3bb9fccSApple OSS Distributions /* updateChannelValues() is called to refresh state before being
803a5e72196SApple OSS Distributions * reported outside the reporter. In the case of IOStateReporter,
804a5e72196SApple OSS Distributions * this is primarily an update to the "time in state" data.
805a3bb9fccSApple OSS Distributions */
806a3bb9fccSApple OSS Distributions IOReturn
updateChannelValues(int channel_index)807a3bb9fccSApple OSS Distributions IOStateReporter::updateChannelValues(int channel_index)
808a3bb9fccSApple OSS Distributions {
809a3bb9fccSApple OSS Distributions IOReturn kerr, result = kIOReturnError;
810a3bb9fccSApple OSS Distributions
811a3bb9fccSApple OSS Distributions int state_index, element_idx;
812a3bb9fccSApple OSS Distributions uint64_t currentTime;
813a3bb9fccSApple OSS Distributions uint64_t last_ch_update_time;
814a3bb9fccSApple OSS Distributions uint64_t time_in_state;
815a3bb9fccSApple OSS Distributions IOStateReportValues state_values;
816a3bb9fccSApple OSS Distributions
817a3bb9fccSApple OSS Distributions IOREPORTER_CHECK_LOCK();
818a3bb9fccSApple OSS Distributions
819a3bb9fccSApple OSS Distributions if (channel_index < 0 || channel_index >= _nChannels) {
820a3bb9fccSApple OSS Distributions result = kIOReturnBadArgument; goto finish;
821a3bb9fccSApple OSS Distributions }
822a3bb9fccSApple OSS Distributions
823a3bb9fccSApple OSS Distributions /* First check to see whether this channel has begun self-
824a5e72196SApple OSS Distributions * calculation of time in state. It's possible this channel
825a5e72196SApple OSS Distributions * has yet to be initialized or that the driver is updating
826a5e72196SApple OSS Distributions * the channel with override/incrementChannelState() which
827a5e72196SApple OSS Distributions * never enable automatic time-in-state updates. In that case,
828a5e72196SApple OSS Distributions * there is nothing to update and we return success.
829a3bb9fccSApple OSS Distributions */
830a3bb9fccSApple OSS Distributions last_ch_update_time = _lastUpdateTimes[channel_index];
831a3bb9fccSApple OSS Distributions if (last_ch_update_time == 0) {
832a3bb9fccSApple OSS Distributions result = kIOReturnSuccess; goto finish;
833a3bb9fccSApple OSS Distributions }
834a3bb9fccSApple OSS Distributions
835a3bb9fccSApple OSS Distributions // figure out the current state (if any)
836a3bb9fccSApple OSS Distributions state_index = _currentStates[channel_index];
837a3bb9fccSApple OSS Distributions
838a3bb9fccSApple OSS Distributions // e.g. given 4 4-state channels, the boundary is ch[3].st[3] <- _elems[15]
839a3bb9fccSApple OSS Distributions if (channel_index < 0 || channel_index > (_nElements - state_index)
840a3bb9fccSApple OSS Distributions / _channelDimension) {
841a3bb9fccSApple OSS Distributions result = kIOReturnOverrun; goto finish;
842a3bb9fccSApple OSS Distributions }
843a3bb9fccSApple OSS Distributions element_idx = channel_index * _channelDimension + state_index;
844a3bb9fccSApple OSS Distributions
845a3bb9fccSApple OSS Distributions // get the current values
846a3bb9fccSApple OSS Distributions kerr = copyElementValues(element_idx, (IOReportElementValues*)&state_values);
847a3bb9fccSApple OSS Distributions if (kerr) {
848a3bb9fccSApple OSS Distributions result = kerr; goto finish;
849a3bb9fccSApple OSS Distributions }
850a3bb9fccSApple OSS Distributions
851a3bb9fccSApple OSS Distributions // calculate time in state
852a3bb9fccSApple OSS Distributions currentTime = mach_absolute_time();
853a3bb9fccSApple OSS Distributions time_in_state = currentTime - last_ch_update_time;
854a3bb9fccSApple OSS Distributions state_values.upticks += time_in_state;
855a3bb9fccSApple OSS Distributions
856a3bb9fccSApple OSS Distributions // and store the values
857a3bb9fccSApple OSS Distributions kerr = setElementValues(element_idx,
858a3bb9fccSApple OSS Distributions (IOReportElementValues *)&state_values,
859a3bb9fccSApple OSS Distributions currentTime);
860a3bb9fccSApple OSS Distributions if (kerr) {
861a3bb9fccSApple OSS Distributions result = kerr; goto finish;
862a3bb9fccSApple OSS Distributions }
863a3bb9fccSApple OSS Distributions
864a3bb9fccSApple OSS Distributions // Record basis for next "prior time" calculation
865a3bb9fccSApple OSS Distributions _lastUpdateTimes[channel_index] = currentTime;
866a3bb9fccSApple OSS Distributions
867a3bb9fccSApple OSS Distributions
868a3bb9fccSApple OSS Distributions // success
869a3bb9fccSApple OSS Distributions result = kIOReturnSuccess;
870a3bb9fccSApple OSS Distributions
871a3bb9fccSApple OSS Distributions finish:
872a3bb9fccSApple OSS Distributions return result;
873a3bb9fccSApple OSS Distributions }
874*e6231be0SApple OSS Distributions
875*e6231be0SApple OSS Distributions /* static */ OSPtr<IOReportLegendEntry>
createLegend(const uint64_t * channelIDs,const char ** channelNames,int channelCount,int nstates,IOReportCategories categories,IOReportUnit unit)876*e6231be0SApple OSS Distributions IOStateReporter::createLegend(const uint64_t *channelIDs,
877*e6231be0SApple OSS Distributions const char **channelNames,
878*e6231be0SApple OSS Distributions int channelCount,
879*e6231be0SApple OSS Distributions int nstates,
880*e6231be0SApple OSS Distributions IOReportCategories categories,
881*e6231be0SApple OSS Distributions IOReportUnit unit)
882*e6231be0SApple OSS Distributions {
883*e6231be0SApple OSS Distributions IOReportChannelType channelType = {
884*e6231be0SApple OSS Distributions .categories = categories,
885*e6231be0SApple OSS Distributions .report_format = kIOReportFormatState,
886*e6231be0SApple OSS Distributions .nelements = static_cast<uint16_t>(nstates),
887*e6231be0SApple OSS Distributions .element_idx = 0
888*e6231be0SApple OSS Distributions };
889*e6231be0SApple OSS Distributions
890*e6231be0SApple OSS Distributions return IOReporter::legendWith(channelIDs, channelNames, channelCount, channelType, unit);
891*e6231be0SApple OSS Distributions }
892