]> git.saurik.com Git - apple/xnu.git/blame - iokit/IOKit/IOReportMacros.h
xnu-7195.101.1.tar.gz
[apple/xnu.git] / iokit / IOKit / IOReportMacros.h
CommitLineData
39236c6e 1/*
f427ee49 2 * Copyright (c) 2012-2020 Apple Computer, Inc. All Rights Reserved.
39236c6e
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 5 *
39236c6e
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
0a7de745 14 *
39236c6e
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
0a7de745 17 *
39236c6e
A
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
0a7de745 25 *
39236c6e
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#ifndef _IOREPORT_MACROS_H_
30#define _IOREPORT_MACROS_H_
31
32#include "IOReportTypes.h"
39037602 33#include <string.h>
39236c6e
A
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
fe8ab488
A
39#ifndef IOREPORT_ABORT
40#define IOREPORT_ABORT panic
41#endif
42
39236c6e 43/*
0a7de745
A
44 * Background
45 *
46 * These macros allow non-I/O Kit software to generate IOReporting
47 * reports. Clients must prevent concurrent access to any given
48 * report buffer from multiple threads.
49 *
50 * While these macros allow non-I/O Kit software to participate
51 * in IOReporting, an IOService instance must lend its driver ID,
52 * respond to the appropriate IOService overrides, and shuttle
53 * data back and forth. In some cases, it may be useful to have
54 * the I/O Kit driver initialize the report buffer with the
55 * appropriate macro.
56 */
39236c6e 57
fe8ab488
A
58
59/* ----- Reporting Single Integers (SimpleReport) ----- */
60
39236c6e 61/*
fe8ab488 62 * The buffer size required for a SimpleReport.
39236c6e 63 */
fe8ab488 64
39236c6e
A
65#define SIMPLEREPORT_BUFSIZE (sizeof(IOReportElement))
66
fe8ab488 67
39236c6e 68/*
fe8ab488 69 * Initialize a buffer to hold a SimpleReport.
39236c6e
A
70 *
71 * void* buffer - ptr to SIMPLEREPORT_BUFSIZE bytes
72 * size_t bufSize - sanity check of buffer's size
73 * uint64_t providerID - registry Entry ID of the reporting service
74 * uint64_t channelID - the report's channel ID
75 * IOReportCategories categories - categories of this channel
76 *
fe8ab488 77 * If the buffer is not of sufficient size, the macro calls IOREPORT_ABORT().
39037602 78 * If that returns, the buffer is left full of '&'.
39236c6e 79 */
fe8ab488 80
39037602 81#define SIMPLEREPORT_INIT(buf, bufSize, providerID, channelID, cats) \
39236c6e 82do { \
39037602
A
83 memset((buf), '&', (bufSize)); \
84 IOReportElement *__elem = (IOReportElement *)(buf); \
39236c6e
A
85 IOSimpleReportValues *__vals; \
86 if ((bufSize) >= SIMPLEREPORT_BUFSIZE) { \
0a7de745
A
87 __elem->provider_id = (providerID); \
88 __elem->channel_id = (channelID); \
89 __elem->channel_type.report_format = kIOReportFormatSimple; \
90 __elem->channel_type.reserved = 0; \
91 __elem->channel_type.categories = (cats); \
92 __elem->channel_type.nelements = 1; \
93 __elem->channel_type.element_idx = 0; \
94 __elem->timestamp = 0; \
95 __vals = (IOSimpleReportValues*)&__elem->values; \
96 __vals->simple_value = kIOReportInvalidIntValue; \
39236c6e
A
97 } \
98 else { \
0a7de745 99 IOREPORT_ABORT("bufSize is smaller than the required size\n"); \
39236c6e
A
100 } \
101} while(0)
102
103
104/*
fe8ab488 105 * Set a SimpleReport to a new value.
39236c6e 106 *
fe8ab488
A
107 * void* simp_buf - ptr to memory initialized by SIMPLEREPORT_INIT()
108 * int64_t new_value - new value for the report
39236c6e 109 */
fe8ab488 110
39236c6e
A
111#define SIMPLEREPORT_SETVALUE(simp_buf, new_value) \
112do { \
113 IOReportElement *__elem = (IOReportElement *)(simp_buf); \
114 IOSimpleReportValues *__vals; \
115 __vals = (IOSimpleReportValues*)&__elem->values; \
116 __vals->simple_value = (new_value); \
117} while(0)
118
fe8ab488 119
39236c6e 120/*
fe8ab488
A
121 * Increment the value of a SimpleReport.
122 *
123 * void* simp_buf - ptr to memory initialized by SIMPLEREPORT_INIT()
124 * int64_t increment - amount by which to increment the value
125 */
126#define SIMPLEREPORT_INCREMENTVALUE(simp_buf, new_value) \
127do { \
128 IOReportElement *__elem = (IOReportElement *)(simp_buf); \
129 IOSimpleReportValues *__vals; \
130 __vals = (IOSimpleReportValues*)&__elem->values; \
131 __vals->simple_value += (new_value); \
132} while(0)
133
134
135/*
136 * Prepare a SimpleReport for
39236c6e
A
137 * IOService::updateReport(kIOReportCopyChannelData...)
138 *
139 * void* simp_buf - Ptr to memory updated by SIMPLEREPORT_SETVALUE()
140 * void* ptr2cpy - On return, 'ptr2cpy' points to the memory that needs to be
141 * copied for kIOReportCopyChannelData.
142 * size_t size2cpy - On return, 'size2cpy' is set to the size of the report
143 * data that needs to be copied for kIOReportCopyChannelData.
144 */
fe8ab488 145
39236c6e
A
146#define SIMPLEREPORT_UPDATEPREP(simp_buf, ptr2cpy, size2cpy) \
147do { \
148 (ptr2cpy) = (simp_buf); \
149 (size2cpy) = sizeof(IOReportElement); \
150} while(0)
151
152
153/*
fe8ab488 154 * Update the result field received as a parameter for
39236c6e
A
155 * kIOReportGetDimensions & kIOReportCopyChannelData actions.
156 *
157 * IOReportConfigureAction action - configure/updateReport() 'action' param
158 * void* result - configure/updateReport() 'result' param
159 */
160
161#define SIMPLEREPORT_UPDATERES(action, result) \
162do { \
163 if (((action) == kIOReportGetDimensions) || ((action) == kIOReportCopyChannelData)) { \
0a7de745
A
164 int *__nElements = (int *)(result); \
165 *__nElements += 1; \
39236c6e
A
166 } \
167} while (0)
168
169
39236c6e 170/*
fe8ab488 171 * Get the 64-bit channel ID of a SimpleReport.
39236c6e
A
172 *
173 * void* simp_buf - ptr to memory initialized by SIMPLEREPORT_INIT()
174 */
175
176#define SIMPLEREPORT_GETCHID(simp_buf) \
fe8ab488
A
177 (((IOReportElement *)(simp_buf))->channel_id)
178
179/*
180 * Get the IOReportChannelType of a SimpleReport.
181 *
182 * void* simp_buf - ptr to memory initialized by SIMPLEREPORT_INIT()
183 */
184
185#define SIMPLEREPORT_GETCHTYPE(simp_buf) \
186 (*(uint64_t*)&(((IOReportElement *)(simp_buf))->channel_type))
187
188
189/*
190 * Get the integer value of a SimpleReport.
191 *
192 * void* simp_buf - memory initialized by SIMPLEREPORT_INIT()
193 */
194
195#define SIMPLEREPORT_GETVALUE(simp_buf) \
196 (((IOSimpleReportValues*)&(((IOReportElement*)(simp_buf))->values)) \
0a7de745 197 ->simple_value)
39236c6e
A
198
199
fe8ab488 200/* ----- State Machine Reporting (StateReport) ----- */
39236c6e 201
fe8ab488 202// Internal struct for StateReport
39236c6e 203typedef struct {
0a7de745
A
204 uint16_t curr_state;
205 uint64_t update_ts;
206 IOReportElement elem[]; // Array of elements
39236c6e
A
207} IOStateReportInfo;
208
209/*
fe8ab488 210 * Determine the size required for a StateReport buffer.
39236c6e 211 *
fe8ab488 212 * int nstates - number of states to be reported
39236c6e
A
213 */
214#define STATEREPORT_BUFSIZE(nstates) \
215 (sizeof(IOStateReportInfo) + (nstates) * sizeof(IOReportElement))
216
217
218/*
fe8ab488 219 * Initialize a StateReport buffer.
39236c6e 220 *
f427ee49 221 * uint16_t nstates - number of states to be reported
39236c6e
A
222 * void* buffer - ptr to STATEREPORT_BUFSIZE(nstates) bytes
223 * size_t bufSize - sanity check of buffer's size
224 * uint64_t providerID - registry Entry ID of the reporting service
225 * uint64_t channelID - ID of this channel, see IOREPORT_MAKEID()
226 * IOReportCategories categories - categories of this channel
227 *
fe8ab488 228 * If the buffer is not of sufficient size, the macro invokes IOREPORT_ABORT.
39037602 229 * If that returns, the buffer is left full of '&'.
39236c6e
A
230 */
231#define STATEREPORT_INIT(nstates, buf, bufSize, providerID, channelID, cats) \
232do { \
39037602 233 memset((buf), '&', (bufSize)); \
39236c6e
A
234 IOStateReportInfo *__info = (IOStateReportInfo *)(buf); \
235 IOStateReportValues *__rep; \
236 IOReportElement *__elem; \
237 if ((bufSize) >= STATEREPORT_BUFSIZE(nstates)) { \
f427ee49 238 for (uint16_t __no = 0; __no < (nstates); __no++) { \
0a7de745
A
239 __elem = &(__info->elem[__no]); \
240 __rep = (IOStateReportValues *) &(__elem->values); \
241 __elem->provider_id = (providerID); \
242 __elem->channel_id = (channelID); \
243 __elem->channel_type.report_format = kIOReportFormatState; \
244 __elem->channel_type.reserved = 0; \
245 __elem->channel_type.categories = (cats); \
246 __elem->channel_type.nelements = (nstates); \
247 __elem->channel_type.element_idx = __no; \
248 __elem->timestamp = 0; \
249 __rep->state_id = __no; \
250 __rep->intransitions = 0; \
251 __rep->upticks = 0; \
252 __rep->last_intransition = 0; \
253 } \
254 __info->curr_state = 0; \
255 __info->update_ts = 0; \
39236c6e
A
256 } \
257 else { \
0a7de745 258 IOREPORT_ABORT("bufSize is smaller than the required size\n"); \
39236c6e
A
259 } \
260} while(0)
261
262/*
fe8ab488
A
263 * Initialize the state id field of a state with the specified value. By
264 * default, STATEREPORT_INIT() initializes the state IDs with the index of
39236c6e
A
265 * that state. This macro can be used to provide a more descriptive state id.
266 *
267 * void* state_buf - ptr to memory initialized by STATEREPORT_INIT()
268 * unsigned stateIdx - index of the state, out of bounds -> no-op
269 * uint64_t stateID - new state id, see IOREPORT_MAKEID()
270 */
271#define STATEREPORT_SETSTATEID(state_buf, stateIdx, stateID) \
272do { \
273 IOStateReportInfo *__info = (IOStateReportInfo *)(state_buf); \
274 IOStateReportValues *__rep; \
275 if ((stateIdx) < __info->elem[0].channel_type.nelements) { \
0a7de745
A
276 __rep = (IOStateReportValues*) &(__info->elem[(stateIdx)].values); \
277 __rep->state_id = (stateID); \
39236c6e
A
278 } \
279} while (0)
280
281
282/*
fe8ab488 283 * Set the state of a StateReport.
39236c6e
A
284 *
285 * void* state_buf - pointer to memory initialized by STATEREPORT_INIT()
286 * unsigned newStateIdx - index of new state, out of bounds -> no-op
287 * uint64_t changeTime - time at which the transition occurred
288 */
289#define STATEREPORT_SETSTATE(state_buf, newStateIdx, changeTime) \
290do { \
291 IOStateReportInfo *__info = (IOStateReportInfo *)(state_buf); \
292 IOStateReportValues *__rep; \
293 if ((newStateIdx) < __info->elem[0].channel_type.nelements ) { \
0a7de745
A
294 __rep = (IOStateReportValues*) &(__info->elem[__info->curr_state].values); \
295 if (__info->update_ts) \
296 __rep->upticks += (changeTime) - __info->update_ts; \
297 __info->elem[(newStateIdx)].timestamp = (changeTime); \
298 __rep = (IOStateReportValues*) &(__info->elem[(newStateIdx)].values); \
299 __rep->intransitions++; \
300 __info->curr_state = (newStateIdx); \
301 __info->update_ts = (changeTime); \
39236c6e
A
302 } \
303} while(0)
304
305/*
fe8ab488
A
306 * Prepare a StateReport for
307 * IOService::updateReport(kIOReportCopyChannelData...)
39236c6e
A
308 *
309 * void* state_buf - ptr to memory initialized by STATEREPORT_INIT()
310 * uint64_t currentTime - current timestamp
311 * void* ptr2cpy - filled in with pointer to buffer to be copied out
312 * size_t size2cpy - filled in with the size of the buffer to copy out
313 */
314#define STATEREPORT_UPDATEPREP(state_buf, currentTime, ptr2cpy, size2cpy) \
315do { \
316 IOStateReportInfo *__info = (IOStateReportInfo *)(state_buf); \
317 IOReportElement *__elem; \
318 IOStateReportValues *__state; \
319 (size2cpy) = __info->elem[0].channel_type.nelements * sizeof(IOReportElement); \
320 (ptr2cpy) = (void *) &__info->elem[0]; \
321 if (__info->update_ts) { \
0a7de745
A
322 __elem = &__info->elem[__info->curr_state]; \
323 __state = (IOStateReportValues *)&__elem->values; \
324 __elem->timestamp = (currentTime); \
325 __state->upticks += (currentTime) - __info->update_ts; \
326 __info->update_ts = (currentTime); \
39236c6e
A
327 } \
328} while(0)
329
330/*
fe8ab488 331 * Update the result field received as a parameter for kIOReportGetDimensions &
39236c6e
A
332 * kIOReportCopyChannelData actions.
333 *
334 * void* state_buf - memory initialized by STATEREPORT_INIT()
335 * IOReportConfigureAction action - configure/updateReport() 'action'
336 * void* result - configure/updateReport() 'result'
337 */
338
339#define STATEREPORT_UPDATERES(state_buf, action, result) \
340do { \
341 IOStateReportInfo *__info = (IOStateReportInfo *)(state_buf); \
342 IOReportElement *__elem; \
343 int *__nElements = (int *)(result); \
344 if (((action) == kIOReportGetDimensions) || ((action) == kIOReportCopyChannelData)) { \
0a7de745
A
345 __elem = &(__info->elem[0]); \
346 *__nElements += __elem->channel_type.nelements; \
39236c6e
A
347 } \
348} while (0)
349
350
39236c6e 351/*
fe8ab488 352 * Get the 64-bit channel ID of a StateReport.
39236c6e
A
353 *
354 * void* state_buf - ptr to memory initialized by STATEREPORT_INIT()
355 */
39236c6e
A
356#define STATEREPORT_GETCHID(state_buf) \
357 (((IOStateReportInfo *)(state_buf))->elem[0].channel_id)
358
359/*
fe8ab488
A
360 * Get the IOReportChannelType of a StateReport.
361 *
362 * void* state_buf - ptr to memory initialized by STATEREPORT_INIT()
363 */
364#define STATEREPORT_GETCHTYPE(state_buf) \
365 (*(uint64_t*)&(((IOStateReportInfo *)(state_buf))->elem[0].channel_type))
366
367/*
368 * Get the number of transitions into a given state.
39236c6e
A
369 *
370 * void* state_buf - ptr to memory initialized by STATEREPORT_INIT()
371 * unsigned stateIdx - index of state, out of bounds -> kIOReportInvalidValue
372 *
373 */
39236c6e
A
374#define STATEREPORT_GETTRANSITIONS(state_buf, stateIdx) \
375 (((stateIdx) < ((IOStateReportInfo *)(state_buf))->elem[0].channel_type.nelements) \
0a7de745
A
376 ? ((IOStateReportValues*)&(((IOStateReportInfo*)(state_buf))->elem[(stateIdx)].values))->intransitions \
377 : kIOReportInvalidValue)
39236c6e
A
378
379/*
fe8ab488 380 * Get the total number of ticks spent in a given state.
39236c6e
A
381 *
382 * void* state_buf - ptr to memory initialized by STATEREPORT_INIT()
383 * unsigned stateIdx - index of state, out of bounds -> kIOReportInvalidValue
384 */
39236c6e
A
385#define STATEREPORT_GETTICKS(state_buf, stateIdx) \
386 (((stateIdx) < ((IOStateReportInfo*)(state_buf))->elem[0].channel_type.nelements) \
0a7de745
A
387 ? ((IOStateReportValues*)&(((IOStateReportInfo*)(state_buf))->elem[(stateIdx)].values))->upticks \
388 : kIOReportInvalidValue)
39236c6e
A
389
390
fe8ab488
A
391/* ----- Reporting an Array of Integers (SimpleArrayReport) ----- */
392
393/*
394 * Determine the buffer size for a SimpleArrayReport.
395 *
396 * int nValues - number of values to be reported
397 */
398
399#define SIMPLEARRAY_BUFSIZE(nValues) \
400 ((((nValues)/IOR_VALUES_PER_ELEMENT) + (((nValues) % IOR_VALUES_PER_ELEMENT) ? 1:0)) \
0a7de745 401 * sizeof(IOReportElement))
fe8ab488
A
402
403/*
404 * Initialize a buffer for use as a SimpleArrayReport.
405 *
406 * int nValues - number of elements to be reported
407 * void* buf - ptr to SIMPLEARRAY_BUFSIZE(nValues) bytes
408 * size_t bufSize - sanity check of buffer's size
409 * uint64_t providerID - registry Entry ID of the reporting service
410 * uint64_t channelID - ID of this channel, see IOREPORT_MAKEID()
411 * IOReportCategories categories - categories of this channel
412 *
39037602
A
413 * If the buffer is not of sufficient size, the macro invokes IOREPORT_ABORT().
414 * If that returns, the buffer is left full of '&'.
fe8ab488
A
415 */
416
417#define SIMPLEARRAY_INIT(nValues, buf, bufSize, providerID, channelID, cats) \
418do { \
39037602 419 memset((buf), '&', (bufSize)); \
fe8ab488
A
420 IOSimpleArrayReportValues *__rep; \
421 IOReportElement *__elem; \
422 uint32_t __nElems = (((nValues) / IOR_VALUES_PER_ELEMENT) + \
0a7de745 423 (((nValues) % IOR_VALUES_PER_ELEMENT) ? 1 : 0)); \
fe8ab488 424 if ((bufSize) >= SIMPLEARRAY_BUFSIZE(nValues)) { \
0a7de745
A
425 for (unsigned __no = 0; __no < __nElems; __no++) { \
426 __elem = &(((IOReportElement *)(buf))[__no]); \
427 __rep = (IOSimpleArrayReportValues *) &(__elem->values); \
428 __elem->provider_id = (providerID); \
429 __elem->channel_id = (channelID); \
430 __elem->channel_type.report_format = kIOReportFormatSimpleArray; \
431 __elem->channel_type.reserved = 0; \
432 __elem->channel_type.categories = (cats); \
433 __elem->channel_type.nelements = (__nElems); \
434 __elem->channel_type.element_idx = __no; \
435 __elem->timestamp = 0; \
436 __rep->simple_values[0] = kIOReportInvalidIntValue; \
437 __rep->simple_values[1] = kIOReportInvalidIntValue; \
438 __rep->simple_values[2] = kIOReportInvalidIntValue; \
439 __rep->simple_values[3] = kIOReportInvalidIntValue; \
440 } \
fe8ab488
A
441 } \
442 else { \
0a7de745 443 IOREPORT_ABORT("bufSize is smaller than the required size\n"); \
fe8ab488
A
444 } \
445} while(0)
446
447
448/* SimpleArrayReport helpers */
449
450 #define __SA_FINDREP(array_buf, idx) \
0a7de745
A
451 IOSimpleArrayReportValues *__rep; \
452 IOReportElement *__elem; \
453 unsigned __elemIdx = (idx) / IOR_VALUES_PER_ELEMENT; \
454 unsigned __valueIdx = (idx) % IOR_VALUES_PER_ELEMENT; \
455 __elem = &(((IOReportElement *)(array_buf))[0]); \
456 if (__elemIdx < __elem->channel_type.nelements) { \
457 __elem = &(((IOReportElement *)(array_buf))[__elemIdx]); \
458 __rep = (IOSimpleArrayReportValues *) &(__elem->values); \
fe8ab488
A
459
460 #define __SA_MAXINDEX(array_buf) \
0a7de745
A
461 ((((IOReportElement*)(array_buf))->channel_type.nelements) \
462 * IOR_VALUES_PER_ELEMENT) - 1
fe8ab488
A
463
464/*
465 * Set a value at a specified index in a SimpleArrayReport.
466 *
467 * void* array_bufbuf - ptr to memory initialized by SIMPLEARRAY_INIT()
468 * unsigned idx - array index, out of bounds -> no-op
469 * uint64_t newValue - new value to be stored at array[idx]
470 */
471#define SIMPLEARRAY_SETVALUE(array_buf, idx, newValue) \
472do { \
473 __SA_FINDREP((array_buf), (idx)) \
0a7de745 474 __rep->simple_values[__valueIdx] = (newValue); \
fe8ab488
A
475 } \
476} while(0)
477
478/*
479 * Increment an array value within a SimpleArrayReport.
480 *
481 * void* array_buf - ptr to memory initialized by SIMPLEARRAY_INIT()
482 * unsigned idx - array index to increment, out of bounds -> no-op
483 * int64_t value - amount by which to increment array[idx]
484 */
485#define SIMPLEARRAY_INCREMENTVALUE(array_buf, idx, value) \
486do { \
487 __SA_FINDREP((array_buf), (idx)) \
0a7de745 488 __rep->simple_values[__valueIdx] += (value); \
fe8ab488
A
489 } \
490} while(0)
491
492
493/*
494 * Prepare a SimpleArrayReport for
495 * IOService::updateReport(kIOReportCopyChannelData...)
496 *
497 * void* array_buf - ptr to memory initialized by SIMPLEARRAY_INIT()
498 * void* ptr2cpy - filled in with pointer to buffer to be copied out
499 * size_t size2cpy - filled in with the size of the buffer to copy out
500 */
501
502#define SIMPLEARRAY_UPDATEPREP(array_buf, ptr2cpy, size2cpy) \
503do { \
504 IOReportElement *__elem; \
505 __elem = &(((IOReportElement *)(array_buf))[0]); \
506 (ptr2cpy) = (void *) (array_buf); \
507 (size2cpy) = __elem->channel_type.nelements * sizeof(IOReportElement); \
508} while(0)
509
510
511/*
512 * Update the result field received as a parameter for kIOReportGetDimensions &
513 * kIOReportCopyChannelData actions.
514 *
515 * void* array_buf - memory initialized by SIMPLEARRAY_INIT()
516 * IOReportConfigureAction action - configure/updateReport() 'action'
517 * void* result - configure/updateReport() 'result'
518 */
519
520#define SIMPLEARRAY_UPDATERES(array_buf, action, result) \
39236c6e 521do { \
fe8ab488
A
522 IOReportElement *__elem; \
523 int *__nElements = (int *)(result); \
524 __elem = &(((IOReportElement *)(array_buf))[0]); \
525 if (((action) == kIOReportGetDimensions) || ((action) == kIOReportCopyChannelData)) { \
0a7de745 526 *__nElements += __elem->channel_type.nelements; \
fe8ab488 527 } \
39236c6e
A
528} while (0)
529
fe8ab488
A
530
531/*
532 * Get the 64-bit channel ID of a SimpleArrayReport.
533 *
534 * void* array_buf - ptr to memory initialized by SIMPLEARRAY_INIT()
535 */
536#define SIMPLEARRAY_GETCHID(array_buf) \
537 (((IOReportElement *)(array_buf))->channel_id)
538
539
540/*
541 * Get the IOReportChannelType of a SimpleArrayReport.
542 *
543 * void* simp_buf - ptr to memory initialized by SIMPLEREPORT_INIT()
544 */
545#define SIMPLEARRAY_GETCHTYPE(array_buf) \
546 (*(uint64_t*)&(((IOReportElement *)(array_buf))->channel_type))
547
548/*
549 * Get a value from a SimpleArrayReport.
550 *
551 * void* array_buf - ptr to memory initialized by SIMPLEARRAY_INIT()
552 * unsigned idx - index of the value, out of bounds -> kIOReportInvalidValue
553 */
554#define SIMPLEARRAY_GETVALUE(array_buf, idx) \
555 (((idx) > __SA_MAXINDEX(array_buf) || (idx) < 0) ? kIOReportInvalidIntValue : \
556 ((IOSimpleArrayReportValues*)&( \
0a7de745
A
557 ((IOReportElement*)(array_buf))[(idx) / IOR_VALUES_PER_ELEMENT].values)) \
558 ->simple_values[(idx) % IOR_VALUES_PER_ELEMENT])
fe8ab488
A
559
560
3e170ce0
A
561/* ----- Histogram Reporting (HistogramReport) ----- */
562
563// Internal struct for HistogramReport
564typedef struct {
0a7de745
A
565 int bucketWidth;
566 IOReportElement elem[]; // Array of elements
3e170ce0
A
567} IOHistReportInfo;
568
569/*
570 * Determine the size required for a HistogramReport buffer.
571 *
572 * int nbuckets - number of buckets in the histogram
573 */
574#define HISTREPORT_BUFSIZE(nbuckets) \
575 (sizeof(IOHistReportInfo) + ((nbuckets) * sizeof(IOReportElement)))
576
577/*
578 * Initialize a HistogramReport buffer. Supports only linear scale histogram.
579 *
f427ee49
A
580 * uint16_t nbuckets - number of buckets data is combined into
581 * uint32_t bucketWidth - size of each bucket
3e170ce0
A
582 * void* buffer - ptr to HISTREPORT_BUFSIZE(nbuckets) bytes
583 * size_t bufSize - sanity check of buffer's size
584 * uint64_t providerID - registry Entry ID of the reporting service
585 * uint64_t channelID - ID of this channel, see IOREPORT_MAKEID()
586 * IOReportCategories categories - categories of this channel
587 *
588 * If the buffer is not of sufficient size, the macro invokes IOREPORT_ABORT.
39037602 589 * If that returns, the buffer is left full of '&'.
3e170ce0
A
590 */
591#define HISTREPORT_INIT(nbuckets, bktSize, buf, bufSize, providerID, channelID, cats) \
592do { \
39037602 593 memset((buf), '&', (bufSize)); \
3e170ce0
A
594 IOHistReportInfo *__info = (IOHistReportInfo *)(buf); \
595 IOReportElement *__elem; \
596 IOHistogramReportValues *__rep; \
597 if ((bufSize) >= HISTREPORT_BUFSIZE(nbuckets)) { \
0a7de745 598 __info->bucketWidth = (bktSize); \
f427ee49 599 for (uint16_t __no = 0; __no < (nbuckets); __no++) { \
0a7de745
A
600 __elem = &(__info->elem[__no]); \
601 __rep = (IOHistogramReportValues *) &(__elem->values); \
602 __elem->provider_id = (providerID); \
603 __elem->channel_id = (channelID); \
604 __elem->channel_type.report_format = kIOReportFormatHistogram; \
605 __elem->channel_type.reserved = 0; \
606 __elem->channel_type.categories = (cats); \
607 __elem->channel_type.nelements = (nbuckets); \
608 __elem->channel_type.element_idx = __no; \
609 __elem->timestamp = 0; \
610 memset(__rep, '\0', sizeof(IOHistogramReportValues)); \
611 } \
3e170ce0
A
612 } \
613 else { \
0a7de745 614 IOREPORT_ABORT("bufSize is smaller than the required size\n"); \
3e170ce0
A
615 } \
616} while (0)
617
618/*
619 * Update histogram with a new value.
620 *
621 *
622 * void* hist_buf - pointer to memory initialized by HISTREPORT_INIT()
623 * int64_t value - new value to add to the histogram
624 */
625#define HISTREPORT_TALLYVALUE(hist_buf, value) \
626do { \
627 IOHistReportInfo *__info = (IOHistReportInfo *)(hist_buf); \
628 IOReportElement *__elem; \
629 IOHistogramReportValues *__rep; \
630 for (unsigned __no = 0; __no < __info->elem[0].channel_type.nelements; __no++) { \
0a7de745
A
631 if ((value) <= __info->bucketWidth * (__no+1)) { \
632 __elem = &(__info->elem[__no]); \
633 __rep = (IOHistogramReportValues *) &(__elem->values); \
634 if (__rep->bucket_hits == 0) { \
635 __rep->bucket_min = __rep->bucket_max = (value); \
636 } \
637 else if ((value) < __rep->bucket_min) { \
638 __rep->bucket_min = (value); \
639 } \
640 else if ((value) > __rep->bucket_max) { \
641 __rep->bucket_max = (value); \
642 } \
643 __rep->bucket_sum += (value); \
644 __rep->bucket_hits++; \
645 break; \
646 } \
3e170ce0
A
647 } \
648} while (0)
649
650/*
651 * Prepare a HistogramReport for
652 * IOService::updateReport(kIOReportCopyChannelData...)
653 *
654 * void* array_buf - ptr to memory initialized by HISTREPORT_INIT()
655 * void* ptr2cpy - filled in with pointer to buffer to be copied out
656 * size_t size2cpy - filled in with the size of the buffer to copy out
657 */
658
659#define HISTREPORT_UPDATEPREP(hist_buf, ptr2cpy, size2cpy) \
660do { \
661 IOHistReportInfo *__info = (IOHistReportInfo *)(hist_buf); \
662 (size2cpy) = __info->elem[0].channel_type.nelements * sizeof(IOReportElement); \
663 (ptr2cpy) = (void *) &__info->elem[0]; \
664} while(0)
665
666
667/*
668 * Update the result field received as a parameter for kIOReportGetDimensions &
669 * kIOReportCopyChannelData actions.
670 *
671 * void* array_buf - memory initialized by HISTREPORT_INIT()
672 * IOReportConfigureAction action - configure/updateReport() 'action'
673 * void* result - configure/updateReport() 'result'
674 */
675
676#define HISTREPORT_UPDATERES(hist_buf, action, result) \
677do { \
678 IOHistReportInfo *__info = (IOHistReportInfo *)(hist_buf); \
679 int *__nElements = (int *)(result); \
680 if (((action) == kIOReportGetDimensions) || ((action) == kIOReportCopyChannelData)) { \
0a7de745 681 *__nElements += __info->elem[0].channel_type.nelements; \
3e170ce0
A
682 } \
683} while (0)
684
685/*
686 * Get the 64-bit channel ID of a HistogramReport.
687 *
688 * void* hist_buf - ptr to memory initialized by HISTREPORT_INIT()
689 */
690#define HISTREPORT_GETCHID(hist_buf) \
691 (((IOHistReportInfo *)(hist_buf))->elem[0].channel_id)
692
693/*
694 * Get the IOReportChannelType of a HistogramReport.
695 *
696 * void* hist_buf - ptr to memory initialized by HISTREPORT_INIT()
697 */
698#define HISTREPORT_GETCHTYPE(hist_buf) \
699 (*(uint64_t*)&(((IOHistReportInfo *)(hist_buf))->elem[0].channel_type))
700
39236c6e
A
701#ifdef __cplusplus
702}
703#endif
704
705#endif // _IOREPORT_MACROS_H_