1/*
2 * Copyright (c) 2021 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#include <stdint.h>
30#include <sys/queue.h>
31#include <sys/work_interval.h>
32#include <kern/work_interval.h>
33
34#pragma once
35
36#ifdef XNU_KERNEL_PRIVATE
37
38OS_ASSUME_NONNULL_BEGIN
39
40 __BEGIN_DECLS
41
42typedef struct workload_config_ctx workload_config_ctx_t;
43
44extern workload_config_ctx_t workload_config_boot;
45#if DEVELOPMENT || DEBUG
46extern workload_config_ctx_t workload_config_devel;
47#endif
48
49#define WORKLOAD_CONFIG_ID_NAME_MAX WORK_INTERVAL_WORKLOAD_ID_NAME_MAX
50
51typedef struct workload_config {
52 uint32_t wc_thread_group_flags;
53 uint32_t wc_flags;
54 uint32_t wc_create_flags;
55 uint8_t wc_class_offset;
56 wi_class_t wc_class;
57} workload_config_t;
58
59typedef enum {
60 WLC_F_NONE = 0,
61 WLC_F_THREAD_POLICY = 1, // By default set for new workload configs.
62} workload_config_flags_t;
63
64/*!
65 * @function workload_config_init
66 * @abstract Allocate global workload data structures.
67 * @param ctx Configuration context
68 * @result KERN_SUCCESS on success or error.
69 */
70extern kern_return_t workload_config_init(workload_config_ctx_t *ctx);
71
72/*!
73 * @function workload_config_initialized
74 * @abstract Tests whether or not a config context has been initialized
75 * @param ctx Configuration context
76 * @result true if initialized, false if not
77 */
78extern bool workload_config_initialized(const workload_config_ctx_t *ctx);
79
80/*!
81 * @function workload_config_free
82 * @abstract Free global workload data structures.
83 */
84extern void workload_config_free(workload_config_ctx_t *ctx);
85
86/*!
87 * @function workload_config_insert
88 * @abstract Insert a new workload configuration
89 * @param ctx Configuration context
90 * @param id Workload identifier
91 * @param phase Phase assoicated with this config
92 * @param config Configuration
93 * @result KERN_SUCCESS on success or error.
94 */
95extern kern_return_t workload_config_insert(workload_config_ctx_t *ctx,
96 const char *id, const char *phase, const workload_config_t *config);
97
98/*!
99 * @function workload_config_set_default
100 * @abstract Set the default phase for the specified workload ID
101 * @param ctx Configuration context
102 * @param id Workload identifier
103 * @param phase The new default phase
104 * @result KERN_SUCCESS on success or error.
105 * @discussion The configuration for the specified phase must already exist.
106 */
107extern kern_return_t workload_config_set_default(workload_config_ctx_t *ctx,
108 const char *id, const char *phase);
109
110/*!
111 * @function workload_config_lookup
112 * @abstract Find per phase workload configuration
113 * @param id Workload identifier
114 * @param phase Phase assoicated with this config
115 * @param config Returned configuration
116 * @result KERN_SUCCESS on success or error.
117 */
118extern kern_return_t workload_config_lookup(const char *id, const char *phase,
119 workload_config_t *config);
120
121/*!
122 * @function workload_config_lookup_default
123 * @abstract Find the default phase workload configuration
124 * @param id Workload identifier
125 * @param config Returned configuration
126 * @result KERN_SUCCESS on success or error.
127 */
128extern kern_return_t workload_config_lookup_default(const char *id,
129 workload_config_t *config);
130
131/*!
132 * @function workload_config_iterate
133 * @abstract Iterate over the active workload configuration
134 * @param cb Block called per ID
135 * @discussion If cb returns true, the iteration stops.
136 * The phases argument can be passed into workload_config_iterate_phases.
137 */
138extern void workload_config_iterate(bool (^cb)(const char *id,
139 const void *phases));
140
141/*!
142 * @function workload_config_iterate_phase
143 * @abstract Iterate over the phases in a workload configuration
144 * @param phases Phase configuration to iterate over
145 * @param cb Block called per phase
146 * @discussion If cb returns true, the iteration stops.
147 * This must always be called from a workload_config_iterate block.
148 */
149extern void workload_config_phases_iterate(const void *phases,
150 bool (^cb)(const char *phase, const bool is_default,
151 const workload_config_t *config));
152
153/*!
154 * @function workload_config_get_flags
155 * @abstract Read the workload config flags
156 * @param flags Returns set flags
157 * @result KERN_SUCCESS on success or error.
158 */
159extern kern_return_t workload_config_get_flags(workload_config_flags_t *flags);
160
161/*!
162 * @function workload_config_clear_flag
163 * @abstract Clear a workload config flag
164 * @param ctx Configuration context
165 * @param flag Flag to be cleared
166 * @result KERN_SUCCESS on success or error.
167 */
168extern kern_return_t workload_config_clear_flag(workload_config_ctx_t *ctx,
169 workload_config_flags_t flag);
170
171/*!
172 * @function workload_config_available
173 * @abstract See if a workload configuration is available
174 * @result KERN_SUCCESS on success or error.
175 * @discussion Note: this can be racy on kernels that support dynamically
176 * setting and clearing workload configuration.
177 */
178extern bool workload_config_available(void);
179
180__END_DECLS
181
182 OS_ASSUME_NONNULL_END
183
184#endif /* XNU_KERNEL_PRIVATE */
185