Michael Adisumarta | c06df41 | 2017-09-19 10:10:35 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/debugfs.h> |
| 14 | #include "ipa_pm.h" |
| 15 | #include "ipa_i.h" |
| 16 | |
| 17 | static const char *client_state_to_str[IPA_PM_STATE_MAX] = { |
| 18 | __stringify(IPA_PM_DEACTIVATED), |
| 19 | __stringify(IPA_PM_DEACTIVATE_IN_PROGRESS), |
| 20 | __stringify(IPA_PM_ACTIVATE_IN_PROGRESS), |
| 21 | __stringify(IPA_PM_ACTIVATED), |
| 22 | __stringify(IPA_PM_ACTIVATED_PENDING_DEACTIVATION), |
| 23 | __stringify(IPA_PM_ACTIVATED_TIMER_SET), |
| 24 | __stringify(IPA_PM_ACTIVATED_PENDING_RESCHEDULE), |
| 25 | }; |
| 26 | |
| 27 | static const char *ipa_pm_group_to_str[IPA_PM_GROUP_MAX] = { |
| 28 | __stringify(IPA_PM_GROUP_DEFAULT), |
| 29 | __stringify(IPA_PM_GROUP_APPS), |
| 30 | __stringify(IPA_PM_GROUP_MODEM), |
| 31 | }; |
| 32 | |
| 33 | |
| 34 | #define IPA_PM_DRV_NAME "ipa_pm" |
| 35 | |
| 36 | #define IPA_PM_DBG(fmt, args...) \ |
| 37 | do { \ |
| 38 | pr_debug(IPA_PM_DRV_NAME " %s:%d " fmt, \ |
| 39 | __func__, __LINE__, ## args); \ |
| 40 | IPA_IPC_LOGGING(ipa_get_ipc_logbuf(), \ |
| 41 | IPA_PM_DRV_NAME " %s:%d " fmt, ## args); \ |
| 42 | IPA_IPC_LOGGING(ipa_get_ipc_logbuf_low(), \ |
| 43 | IPA_PM_DRV_NAME " %s:%d " fmt, ## args); \ |
| 44 | } while (0) |
| 45 | #define IPA_PM_DBG_LOW(fmt, args...) \ |
| 46 | do { \ |
| 47 | pr_debug(IPA_PM_DRV_NAME " %s:%d " fmt, \ |
| 48 | __func__, __LINE__, ## args); \ |
| 49 | IPA_IPC_LOGGING(ipa_get_ipc_logbuf_low(), \ |
| 50 | IPA_PM_DRV_NAME " %s:%d " fmt, ## args); \ |
| 51 | } while (0) |
| 52 | #define IPA_PM_ERR(fmt, args...) \ |
| 53 | do { \ |
| 54 | pr_err(IPA_PM_DRV_NAME " %s:%d " fmt, \ |
| 55 | __func__, __LINE__, ## args); \ |
| 56 | IPA_IPC_LOGGING(ipa_get_ipc_logbuf(), \ |
| 57 | IPA_PM_DRV_NAME " %s:%d " fmt, ## args); \ |
| 58 | IPA_IPC_LOGGING(ipa_get_ipc_logbuf_low(), \ |
| 59 | IPA_PM_DRV_NAME " %s:%d " fmt, ## args); \ |
| 60 | } while (0) |
| 61 | #define IPA_PM_DBG_STATE(hdl, name, state) \ |
| 62 | IPA_PM_DBG_LOW("Client[%d] %s: %s\n", hdl, name, \ |
| 63 | client_state_to_str[state]) |
| 64 | |
| 65 | |
| 66 | #if IPA_PM_MAX_CLIENTS > 32 |
| 67 | #error max client greater than 32 all bitmask types should be changed |
| 68 | #endif |
| 69 | |
| 70 | /* |
| 71 | * struct ipa_pm_exception_list - holds information about an exception |
| 72 | * @pending: number of clients in exception that have not yet been adctivated |
| 73 | * @bitmask: bitmask of the clients in the exception based on handle |
| 74 | * @threshold: the threshold values for the exception |
| 75 | */ |
| 76 | struct ipa_pm_exception_list { |
| 77 | char clients[IPA_PM_MAX_EX_CL]; |
| 78 | int pending; |
| 79 | u32 bitmask; |
| 80 | int threshold[IPA_PM_THRESHOLD_MAX]; |
| 81 | }; |
| 82 | |
| 83 | /* |
| 84 | * struct clk_scaling_db - holds information about threshholds and exceptions |
| 85 | * @lock: lock the bitmasks and thresholds |
| 86 | * @exception_list: pointer to the list of exceptions |
| 87 | * @work: work for clock scaling algorithm |
| 88 | * @active_client_bitmask: the bits represent handles in the clients array that |
| 89 | * contain non-null client |
| 90 | * @threshold_size: size of the throughput threshold |
| 91 | * @exception_size: size of the exception list |
| 92 | * @cur_vote: idx of the threshold |
| 93 | * @default_threshold: the thresholds used if no exception passes |
| 94 | * @current_threshold: the current threshold of the clock plan |
| 95 | */ |
| 96 | struct clk_scaling_db { |
| 97 | spinlock_t lock; |
| 98 | struct ipa_pm_exception_list exception_list[IPA_PM_EXCEPTION_MAX]; |
| 99 | struct work_struct work; |
| 100 | u32 active_client_bitmask; |
| 101 | int threshold_size; |
| 102 | int exception_size; |
| 103 | int cur_vote; |
| 104 | int default_threshold[IPA_PM_THRESHOLD_MAX]; |
| 105 | int *current_threshold; |
| 106 | }; |
| 107 | |
| 108 | /* |
| 109 | * ipa_pm state names |
| 110 | * |
| 111 | * Timer free states: |
| 112 | * @IPA_PM_DEACTIVATED: client starting state when registered |
| 113 | * @IPA_PM_DEACTIVATE_IN_PROGRESS: deactivate was called in progress of a client |
| 114 | * activating |
| 115 | * @IPA_PM_ACTIVATE_IN_PROGRESS: client is being activated by work_queue |
| 116 | * @IPA_PM_ACTIVATED: client is activated without any timers |
| 117 | * |
| 118 | * Timer set states: |
| 119 | * @IPA_PM_ACTIVATED_PENDING_DEACTIVATION: moves to deactivate once timer pass |
| 120 | * @IPA_PM_ACTIVATED_TIMER_SET: client was activated while timer was set, so |
| 121 | * when the timer pass, client will still be activated |
| 122 | *@IPA_PM_ACTIVATED_PENDING_RESCHEDULE: state signifying extended timer when |
| 123 | * a client is deferred_deactivated when a time ris still active |
| 124 | */ |
| 125 | enum ipa_pm_state { |
| 126 | IPA_PM_DEACTIVATED, |
| 127 | IPA_PM_DEACTIVATE_IN_PROGRESS, |
| 128 | IPA_PM_ACTIVATE_IN_PROGRESS, |
| 129 | IPA_PM_ACTIVATED, |
| 130 | IPA_PM_ACTIVATED_PENDING_DEACTIVATION, |
| 131 | IPA_PM_ACTIVATED_TIMER_SET, |
| 132 | IPA_PM_ACTIVATED_PENDING_RESCHEDULE, |
| 133 | }; |
| 134 | |
| 135 | #define IPA_PM_STATE_ACTIVE(state) \ |
| 136 | (state == IPA_PM_ACTIVATED ||\ |
| 137 | state == IPA_PM_ACTIVATED_PENDING_DEACTIVATION ||\ |
| 138 | state == IPA_PM_ACTIVATED_TIMER_SET ||\ |
| 139 | state == IPA_PM_ACTIVATED_PENDING_RESCHEDULE) |
| 140 | |
| 141 | #define IPA_PM_STATE_IN_PROGRESS(state) \ |
| 142 | (state == IPA_PM_ACTIVATE_IN_PROGRESS \ |
| 143 | || state == IPA_PM_DEACTIVATE_IN_PROGRESS) |
| 144 | |
| 145 | /* |
| 146 | * struct ipa_pm_client - holds information about a specific IPA client |
| 147 | * @name: string name of the client |
| 148 | * @callback: pointer to the client's callback function |
| 149 | * @callback_params: pointer to the client's callback parameters |
| 150 | * @state: Activation state of the client |
| 151 | * @skip_clk_vote: 0 if client votes for clock when activated, 1 if no vote |
| 152 | * @group: the ipa_pm_group the client belongs to |
| 153 | * @hdl: handle of the client |
| 154 | * @throughput: the throughput of the client for clock scaling |
| 155 | * @state_lock: spinlock to lock the pm_states |
| 156 | * @activate_work: work for activate (blocking case) |
| 157 | * @deactivate work: delayed work for deferred_deactivate function |
| 158 | * @complete: generic wait-for-completion handler |
| 159 | */ |
| 160 | struct ipa_pm_client { |
| 161 | char name[IPA_PM_MAX_EX_CL]; |
| 162 | void (*callback)(void*, enum ipa_pm_cb_event); |
| 163 | void *callback_params; |
| 164 | enum ipa_pm_state state; |
| 165 | bool skip_clk_vote; |
| 166 | int group; |
| 167 | int hdl; |
| 168 | int throughput; |
| 169 | spinlock_t state_lock; |
| 170 | struct work_struct activate_work; |
| 171 | struct delayed_work deactivate_work; |
| 172 | struct completion complete; |
| 173 | }; |
| 174 | |
| 175 | /* |
| 176 | * struct ipa_pm_ctx - global ctx that will hold the client arrays and tput info |
| 177 | * @clients: array to the clients with the handle as its index |
| 178 | * @clients_by_pipe: array to the clients with endpoint as the index |
| 179 | * @wq: work queue for deferred deactivate, activate, and clk_scaling work |
| 180 | 8 @clk_scaling: pointer to clock scaling database |
| 181 | * @client_mutex: global mutex to lock the client arrays |
| 182 | * @aggragated_tput: aggragated tput value of all valid activated clients |
| 183 | * @group_tput: combined throughput for the groups |
| 184 | */ |
| 185 | struct ipa_pm_ctx { |
| 186 | struct ipa_pm_client *clients[IPA_PM_MAX_CLIENTS]; |
| 187 | struct ipa_pm_client *clients_by_pipe[IPA3_MAX_NUM_PIPES]; |
| 188 | struct workqueue_struct *wq; |
| 189 | struct clk_scaling_db clk_scaling; |
| 190 | struct mutex client_mutex; |
| 191 | int aggregated_tput; |
| 192 | int group_tput[IPA_PM_GROUP_MAX]; |
| 193 | }; |
| 194 | |
| 195 | static struct ipa_pm_ctx *ipa_pm_ctx; |
| 196 | |
| 197 | /** |
| 198 | * pop_max_from_array() -pop the max and move the last element to where the |
| 199 | * max was popped |
| 200 | * @arr: array to be searched for max |
| 201 | * @n: size of the array |
| 202 | * |
| 203 | * Returns: max value of the array |
| 204 | */ |
| 205 | static int pop_max_from_array(int *arr, int *n) |
| 206 | { |
| 207 | int i; |
| 208 | int max, max_idx; |
| 209 | |
| 210 | max_idx = *n - 1; |
| 211 | max = 0; |
| 212 | |
| 213 | if (*n == 0) |
| 214 | return 0; |
| 215 | |
| 216 | for (i = 0; i < *n; i++) { |
| 217 | if (arr[i] > max) { |
| 218 | max = arr[i]; |
| 219 | max_idx = i; |
| 220 | } |
| 221 | } |
| 222 | (*n)--; |
| 223 | arr[max_idx] = arr[*n]; |
| 224 | |
| 225 | return max; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * calculate_throughput() - calculate the aggregated throughput |
| 230 | * based on active clients |
| 231 | * |
| 232 | * Returns: aggregated tput value |
| 233 | */ |
| 234 | static int calculate_throughput(void) |
| 235 | { |
| 236 | int client_tput[IPA_PM_MAX_CLIENTS] = { 0 }; |
| 237 | bool group_voted[IPA_PM_GROUP_MAX] = { false }; |
| 238 | int i, n; |
| 239 | int max, second_max, aggregated_tput; |
| 240 | struct ipa_pm_client *client; |
| 241 | |
| 242 | /* Create a basic array to hold throughputs*/ |
| 243 | for (i = 0, n = 0; i < IPA_PM_MAX_CLIENTS; i++) { |
| 244 | client = ipa_pm_ctx->clients[i]; |
| 245 | if (client != NULL && IPA_PM_STATE_ACTIVE(client->state)) { |
| 246 | /* default case */ |
| 247 | if (client->group == IPA_PM_GROUP_DEFAULT) { |
| 248 | client_tput[n++] = client->throughput; |
| 249 | } else if (group_voted[client->group] == false) { |
| 250 | client_tput[n++] = ipa_pm_ctx->group_tput |
| 251 | [client->group]; |
| 252 | group_voted[client->group] = true; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | /*the array will only use n+1 spots. n will be the last index used*/ |
| 257 | |
| 258 | aggregated_tput = 0; |
| 259 | |
| 260 | /** |
| 261 | * throughput algorithm: |
| 262 | * 1) pop the max and second_max |
| 263 | * 2) add the 2nd max to aggregated tput |
| 264 | * 3) insert the value of max - 2nd max |
| 265 | * 4) repeat until array is of size 1 |
| 266 | */ |
| 267 | while (n > 1) { |
| 268 | max = pop_max_from_array(client_tput, &n); |
| 269 | second_max = pop_max_from_array(client_tput, &n); |
| 270 | client_tput[n++] = max - second_max; |
| 271 | aggregated_tput += second_max; |
| 272 | } |
| 273 | |
| 274 | IPA_PM_DBG_LOW("Aggregated throughput: %d\n", aggregated_tput); |
| 275 | |
| 276 | return aggregated_tput; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * deactivate_client() - turn off the bit in the active client bitmask based on |
| 281 | * the handle passed in |
| 282 | * @hdl: The index of the client to be deactivated |
| 283 | */ |
| 284 | static void deactivate_client(u32 hdl) |
| 285 | { |
| 286 | unsigned long flags; |
| 287 | |
| 288 | spin_lock_irqsave(&ipa_pm_ctx->clk_scaling.lock, flags); |
| 289 | ipa_pm_ctx->clk_scaling.active_client_bitmask &= ~(1 << hdl); |
| 290 | spin_unlock_irqrestore(&ipa_pm_ctx->clk_scaling.lock, flags); |
| 291 | IPA_PM_DBG_LOW("active bitmask: %x\n", |
| 292 | ipa_pm_ctx->clk_scaling.active_client_bitmask); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * activate_client() - turn on the bit in the active client bitmask based on |
| 297 | * the handle passed in |
| 298 | * @hdl: The index of the client to be activated |
| 299 | */ |
| 300 | static void activate_client(u32 hdl) |
| 301 | { |
| 302 | unsigned long flags; |
| 303 | |
| 304 | spin_lock_irqsave(&ipa_pm_ctx->clk_scaling.lock, flags); |
| 305 | ipa_pm_ctx->clk_scaling.active_client_bitmask |= (1 << hdl); |
| 306 | spin_unlock_irqrestore(&ipa_pm_ctx->clk_scaling.lock, flags); |
| 307 | IPA_PM_DBG_LOW("active bitmask: %x\n", |
| 308 | ipa_pm_ctx->clk_scaling.active_client_bitmask); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * deactivate_client() - get threshold |
| 313 | * |
| 314 | * Returns: threshold of the exception that passes or default if none pass |
| 315 | */ |
| 316 | static void set_current_threshold(void) |
| 317 | { |
| 318 | int i; |
| 319 | struct clk_scaling_db *clk; |
| 320 | struct ipa_pm_exception_list *exception; |
| 321 | unsigned long flags; |
| 322 | |
| 323 | clk = &ipa_pm_ctx->clk_scaling; |
| 324 | |
| 325 | spin_lock_irqsave(&ipa_pm_ctx->clk_scaling.lock, flags); |
| 326 | for (i = 0; i < clk->exception_size; i++) { |
| 327 | exception = &clk->exception_list[i]; |
| 328 | if (exception->pending == 0 && (exception->bitmask |
| 329 | & ~clk->active_client_bitmask) == 0) { |
| 330 | spin_unlock_irqrestore(&ipa_pm_ctx->clk_scaling.lock, |
| 331 | flags); |
| 332 | clk->current_threshold = exception->threshold; |
| 333 | IPA_PM_DBG("Exception %d set\n", i); |
| 334 | return; |
| 335 | } |
| 336 | } |
| 337 | clk->current_threshold = clk->default_threshold; |
| 338 | spin_unlock_irqrestore(&ipa_pm_ctx->clk_scaling.lock, flags); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * do_clk_scaling() - set the clock based on the activated clients |
| 343 | * |
| 344 | * Returns: 0 if success, negative otherwise |
| 345 | */ |
| 346 | static int do_clk_scaling(void) |
| 347 | { |
| 348 | int i, tput; |
| 349 | int new_th_idx = 1; |
| 350 | struct clk_scaling_db *clk_scaling; |
| 351 | |
| 352 | clk_scaling = &ipa_pm_ctx->clk_scaling; |
| 353 | |
| 354 | mutex_lock(&ipa_pm_ctx->client_mutex); |
| 355 | IPA_PM_DBG("clock scaling started\n"); |
| 356 | tput = calculate_throughput(); |
| 357 | ipa_pm_ctx->aggregated_tput = tput; |
| 358 | set_current_threshold(); |
| 359 | |
| 360 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 361 | |
| 362 | for (i = 0; i < clk_scaling->threshold_size; i++) { |
| 363 | if (tput > clk_scaling->current_threshold[i]) |
| 364 | new_th_idx++; |
| 365 | } |
| 366 | |
| 367 | IPA_PM_DBG("old idx was at %d\n", ipa_pm_ctx->clk_scaling.cur_vote); |
| 368 | |
| 369 | |
| 370 | if (ipa_pm_ctx->clk_scaling.cur_vote != new_th_idx) { |
| 371 | ipa_pm_ctx->clk_scaling.cur_vote = new_th_idx; |
| 372 | ipa3_set_clock_plan_from_pm(ipa_pm_ctx->clk_scaling.cur_vote); |
| 373 | } |
| 374 | |
| 375 | IPA_PM_DBG("new idx is at %d\n", ipa_pm_ctx->clk_scaling.cur_vote); |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * clock_scaling_func() - set the clock on a work queue |
| 382 | */ |
| 383 | static void clock_scaling_func(struct work_struct *work) |
| 384 | { |
| 385 | do_clk_scaling(); |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * activate_work_func - activate a client and vote for clock on a work queue |
| 390 | */ |
| 391 | static void activate_work_func(struct work_struct *work) |
| 392 | { |
| 393 | struct ipa_pm_client *client; |
| 394 | bool dec_clk = false; |
| 395 | unsigned long flags; |
| 396 | |
| 397 | client = container_of(work, struct ipa_pm_client, activate_work); |
| 398 | if (!client->skip_clk_vote) |
| 399 | IPA_ACTIVE_CLIENTS_INC_SPECIAL(client->name); |
| 400 | |
| 401 | spin_lock_irqsave(&client->state_lock, flags); |
| 402 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 403 | if (client->state == IPA_PM_ACTIVATE_IN_PROGRESS) { |
| 404 | client->state = IPA_PM_ACTIVATED; |
| 405 | } else if (client->state == IPA_PM_DEACTIVATE_IN_PROGRESS) { |
| 406 | client->state = IPA_PM_DEACTIVATED; |
| 407 | dec_clk = true; |
| 408 | } else { |
| 409 | IPA_PM_ERR("unexpected state %d\n", client->state); |
| 410 | WARN_ON(1); |
| 411 | } |
| 412 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 413 | |
| 414 | complete_all(&client->complete); |
| 415 | |
| 416 | if (dec_clk) { |
| 417 | ipa_set_tag_process_before_gating(true); |
| 418 | if (!client->skip_clk_vote) |
| 419 | IPA_ACTIVE_CLIENTS_DEC_SPECIAL(client->name); |
| 420 | |
| 421 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | activate_client(client->hdl); |
| 426 | |
| 427 | mutex_lock(&ipa_pm_ctx->client_mutex); |
Michael Adisumarta | ce75fc7 | 2017-10-04 11:42:11 -0700 | [diff] [blame] | 428 | if (client->callback) { |
| 429 | client->callback(client->callback_params, |
| 430 | IPA_PM_CLIENT_ACTIVATED); |
| 431 | } else { |
| 432 | IPA_PM_ERR("client has no callback"); |
| 433 | WARN_ON(1); |
| 434 | } |
Michael Adisumarta | c06df41 | 2017-09-19 10:10:35 -0700 | [diff] [blame] | 435 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 436 | |
| 437 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 438 | do_clk_scaling(); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * delayed_deferred_deactivate_work_func - deferred deactivate on a work queue |
| 443 | */ |
| 444 | static void delayed_deferred_deactivate_work_func(struct work_struct *work) |
| 445 | { |
| 446 | struct delayed_work *dwork; |
| 447 | struct ipa_pm_client *client; |
| 448 | unsigned long flags; |
| 449 | |
| 450 | dwork = container_of(work, struct delayed_work, work); |
| 451 | client = container_of(dwork, struct ipa_pm_client, deactivate_work); |
| 452 | |
| 453 | spin_lock_irqsave(&client->state_lock, flags); |
| 454 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 455 | switch (client->state) { |
| 456 | case IPA_PM_ACTIVATED_TIMER_SET: |
| 457 | client->state = IPA_PM_ACTIVATED; |
| 458 | goto bail; |
| 459 | case IPA_PM_ACTIVATED_PENDING_RESCHEDULE: |
| 460 | queue_delayed_work(ipa_pm_ctx->wq, &client->deactivate_work, |
| 461 | msecs_to_jiffies(IPA_PM_DEFERRED_TIMEOUT)); |
| 462 | client->state = IPA_PM_ACTIVATED_PENDING_DEACTIVATION; |
| 463 | goto bail; |
| 464 | case IPA_PM_ACTIVATED_PENDING_DEACTIVATION: |
| 465 | client->state = IPA_PM_DEACTIVATED; |
| 466 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 467 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 468 | ipa_set_tag_process_before_gating(true); |
| 469 | if (!client->skip_clk_vote) |
| 470 | IPA_ACTIVE_CLIENTS_DEC_SPECIAL(client->name); |
| 471 | |
| 472 | deactivate_client(client->hdl); |
| 473 | do_clk_scaling(); |
| 474 | return; |
| 475 | default: |
| 476 | IPA_PM_ERR("unexpected state %d\n", client->state); |
| 477 | WARN_ON(1); |
| 478 | goto bail; |
| 479 | } |
| 480 | |
| 481 | bail: |
| 482 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 483 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 484 | } |
| 485 | |
| 486 | static int find_next_open_array_element(const char *name) |
| 487 | { |
| 488 | int i, n; |
| 489 | |
| 490 | n = -ENOBUFS; |
| 491 | |
| 492 | for (i = IPA_PM_MAX_CLIENTS - 1; i >= 0; i--) { |
| 493 | if (ipa_pm_ctx->clients[i] == NULL) { |
| 494 | n = i; |
| 495 | continue; |
| 496 | } |
| 497 | |
| 498 | if (strlen(name) == strlen(ipa_pm_ctx->clients[i]->name)) |
| 499 | if (!strcmp(name, ipa_pm_ctx->clients[i]->name)) |
| 500 | return -EEXIST; |
| 501 | } |
| 502 | return n; |
| 503 | } |
| 504 | |
| 505 | /** |
| 506 | * add_client_to_exception_list() - add client to the exception list and |
| 507 | * update pending if necessary |
| 508 | * @hdl: index of the IPA client |
| 509 | * |
| 510 | * Returns: 0 if success, negative otherwise |
| 511 | */ |
| 512 | static int add_client_to_exception_list(u32 hdl) |
| 513 | { |
| 514 | int i; |
| 515 | struct ipa_pm_exception_list *exception; |
| 516 | |
| 517 | mutex_lock(&ipa_pm_ctx->client_mutex); |
| 518 | for (i = 0; i < ipa_pm_ctx->clk_scaling.exception_size; i++) { |
| 519 | exception = &ipa_pm_ctx->clk_scaling.exception_list[i]; |
| 520 | if (strnstr(exception->clients, ipa_pm_ctx->clients[hdl]->name, |
| 521 | strlen(exception->clients))) { |
| 522 | exception->pending--; |
| 523 | |
| 524 | if (exception->pending < 0) { |
| 525 | WARN_ON(1); |
| 526 | exception->pending = 0; |
| 527 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 528 | return -EPERM; |
| 529 | } |
| 530 | exception->bitmask |= (1 << hdl); |
| 531 | } |
| 532 | } |
| 533 | IPA_PM_DBG("%s added to exception list\n", |
| 534 | ipa_pm_ctx->clients[hdl]->name); |
| 535 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * remove_client_to_exception_list() - remove client from the exception list and |
| 542 | * update pending if necessary |
| 543 | * @hdl: index of the IPA client |
| 544 | * |
| 545 | * Returns: 0 if success, negative otherwise |
| 546 | */ |
| 547 | static int remove_client_from_exception_list(u32 hdl) |
| 548 | { |
| 549 | int i; |
| 550 | struct ipa_pm_exception_list *exception; |
| 551 | |
| 552 | for (i = 0; i < ipa_pm_ctx->clk_scaling.exception_size; i++) { |
| 553 | exception = &ipa_pm_ctx->clk_scaling.exception_list[i]; |
| 554 | if (exception->bitmask & (1 << hdl)) { |
| 555 | exception->pending++; |
| 556 | exception->bitmask &= ~(1 << hdl); |
| 557 | } |
| 558 | } |
| 559 | IPA_PM_DBG("Client %d removed from exception list\n", hdl); |
| 560 | |
| 561 | return 0; |
| 562 | } |
| 563 | |
| 564 | /** |
| 565 | * ipa_pm_init() - initialize IPA PM Components |
| 566 | * @ipa_pm_init_params: parameters needed to fill exceptions and thresholds |
| 567 | * |
| 568 | * Returns: 0 on success, negative on failure |
| 569 | */ |
| 570 | int ipa_pm_init(struct ipa_pm_init_params *params) |
| 571 | { |
| 572 | int i, j; |
| 573 | struct clk_scaling_db *clk_scaling; |
| 574 | |
| 575 | if (params == NULL) { |
| 576 | IPA_PM_ERR("Invalid Params\n"); |
| 577 | return -EINVAL; |
| 578 | } |
| 579 | |
| 580 | if (params->threshold_size <= 0 |
| 581 | || params->threshold_size > IPA_PM_THRESHOLD_MAX) { |
| 582 | IPA_PM_ERR("Invalid threshold size\n"); |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | |
| 586 | if (params->exception_size < 0 |
| 587 | || params->exception_size > IPA_PM_EXCEPTION_MAX) { |
| 588 | IPA_PM_ERR("Invalid exception size\n"); |
| 589 | return -EINVAL; |
| 590 | } |
| 591 | |
| 592 | IPA_PM_DBG("IPA PM initialization started\n"); |
| 593 | |
| 594 | if (ipa_pm_ctx != NULL) { |
| 595 | IPA_PM_ERR("Already initialized\n"); |
| 596 | return -EPERM; |
| 597 | } |
| 598 | |
| 599 | |
| 600 | ipa_pm_ctx = kzalloc(sizeof(*ipa_pm_ctx), GFP_KERNEL); |
| 601 | if (!ipa_pm_ctx) { |
| 602 | IPA_PM_ERR(":kzalloc err.\n"); |
| 603 | return -ENOMEM; |
| 604 | } |
| 605 | |
| 606 | ipa_pm_ctx->wq = create_singlethread_workqueue("ipa_pm_activate"); |
| 607 | if (!ipa_pm_ctx->wq) { |
| 608 | IPA_PM_ERR("create workqueue failed\n"); |
| 609 | kfree(ipa_pm_ctx); |
| 610 | return -ENOMEM; |
| 611 | } |
| 612 | |
| 613 | mutex_init(&ipa_pm_ctx->client_mutex); |
| 614 | |
| 615 | /* Populate and init locks in clk_scaling_db */ |
| 616 | clk_scaling = &ipa_pm_ctx->clk_scaling; |
| 617 | spin_lock_init(&clk_scaling->lock); |
| 618 | clk_scaling->threshold_size = params->threshold_size; |
| 619 | clk_scaling->exception_size = params->exception_size; |
| 620 | INIT_WORK(&clk_scaling->work, clock_scaling_func); |
| 621 | |
| 622 | for (i = 0; i < params->threshold_size; i++) |
| 623 | clk_scaling->default_threshold[i] = |
| 624 | params->default_threshold[i]; |
| 625 | |
| 626 | /* Populate exception list*/ |
| 627 | for (i = 0; i < params->exception_size; i++) { |
| 628 | strlcpy(clk_scaling->exception_list[i].clients, |
| 629 | params->exceptions[i].usecase, IPA_PM_MAX_EX_CL); |
| 630 | IPA_PM_DBG("Usecase: %s\n", params->exceptions[i].usecase); |
| 631 | |
| 632 | /* Parse the commas to count the size of the clients */ |
| 633 | for (j = 0; j < IPA_PM_MAX_EX_CL && |
| 634 | clk_scaling->exception_list[i].clients[j]; j++) { |
| 635 | if (clk_scaling->exception_list[i].clients[j] == ',') |
| 636 | clk_scaling->exception_list[i].pending++; |
| 637 | } |
| 638 | |
| 639 | clk_scaling->exception_list[i].pending++; |
| 640 | IPA_PM_DBG("Pending: %d\n", clk_scaling-> |
| 641 | exception_list[i].pending); |
| 642 | |
| 643 | /* populate the threshold */ |
| 644 | for (j = 0; j < params->threshold_size; j++) { |
| 645 | clk_scaling->exception_list[i].threshold[j] |
| 646 | = params->exceptions[i].threshold[j]; |
| 647 | } |
| 648 | |
| 649 | } |
| 650 | IPA_PM_DBG("initialization success"); |
| 651 | |
| 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | int ipa_pm_destroy(void) |
| 656 | { |
| 657 | IPA_PM_DBG("IPA PM destroy started\n"); |
| 658 | |
| 659 | if (ipa_pm_ctx == NULL) { |
| 660 | IPA_PM_ERR("Already destroyed\n"); |
| 661 | return -EPERM; |
| 662 | } |
| 663 | |
| 664 | destroy_workqueue(ipa_pm_ctx->wq); |
| 665 | |
| 666 | kfree(ipa_pm_ctx); |
| 667 | ipa_pm_ctx = NULL; |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * ipa_rm_delete_register() - register an IPA PM client with the PM |
| 674 | * @register_params: params for a client like throughput, callback, etc. |
| 675 | * @hdl: int pointer that will be used as an index to access the client |
| 676 | * |
| 677 | * Returns: 0 on success, negative on failure |
| 678 | * |
| 679 | * Side effects: *hdl is replaced with the client index or -EEXIST if |
| 680 | * client is already registered |
| 681 | */ |
| 682 | int ipa_pm_register(struct ipa_pm_register_params *params, u32 *hdl) |
| 683 | { |
| 684 | struct ipa_pm_client *client; |
| 685 | |
Michael Adisumarta | ce75fc7 | 2017-10-04 11:42:11 -0700 | [diff] [blame] | 686 | if (params == NULL || hdl == NULL || params->name == NULL) { |
Michael Adisumarta | c06df41 | 2017-09-19 10:10:35 -0700 | [diff] [blame] | 687 | IPA_PM_ERR("Invalid Params\n"); |
| 688 | return -EINVAL; |
| 689 | } |
| 690 | |
| 691 | IPA_PM_DBG("IPA PM registering client\n"); |
| 692 | |
| 693 | mutex_lock(&ipa_pm_ctx->client_mutex); |
| 694 | |
| 695 | *hdl = find_next_open_array_element(params->name); |
| 696 | |
| 697 | if (*hdl > IPA_CLIENT_MAX) { |
| 698 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 699 | IPA_PM_ERR("client is already registered or array is full\n"); |
| 700 | return *hdl; |
| 701 | } |
| 702 | |
| 703 | ipa_pm_ctx->clients[*hdl] = kzalloc(sizeof |
| 704 | (struct ipa_pm_client), GFP_KERNEL); |
| 705 | if (!ipa_pm_ctx->clients[*hdl]) { |
| 706 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 707 | IPA_PM_ERR(":kzalloc err.\n"); |
| 708 | return -ENOMEM; |
| 709 | } |
| 710 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 711 | |
| 712 | client = ipa_pm_ctx->clients[*hdl]; |
| 713 | |
| 714 | spin_lock_init(&client->state_lock); |
| 715 | |
| 716 | INIT_DELAYED_WORK(&client->deactivate_work, |
| 717 | delayed_deferred_deactivate_work_func); |
| 718 | |
| 719 | INIT_WORK(&client->activate_work, activate_work_func); |
| 720 | |
| 721 | /* populate fields */ |
| 722 | strlcpy(client->name, params->name, IPA_PM_MAX_EX_CL); |
| 723 | client->callback = params->callback; |
| 724 | client->callback_params = params->user_data; |
| 725 | client->group = params->group; |
| 726 | client->hdl = *hdl; |
| 727 | client->skip_clk_vote = params->skip_clk_vote; |
| 728 | |
| 729 | /* add client to exception list */ |
| 730 | if (add_client_to_exception_list(*hdl)) { |
| 731 | ipa_pm_deregister(*hdl); |
| 732 | IPA_PM_ERR("Fail to add client to exception_list\n"); |
| 733 | return -EPERM; |
| 734 | } |
| 735 | |
| 736 | IPA_PM_DBG("IPA PM client registered with handle %d\n", *hdl); |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * ipa_pm_deregister() - deregister IPA client from the PM |
| 742 | * @hdl: index of the client in the array |
| 743 | * |
| 744 | * Returns: 0 on success, negative on failure |
| 745 | */ |
| 746 | int ipa_pm_deregister(u32 hdl) |
| 747 | { |
| 748 | struct ipa_pm_client *client; |
| 749 | int i; |
| 750 | unsigned long flags; |
| 751 | |
| 752 | if (hdl >= IPA_PM_MAX_CLIENTS) { |
| 753 | IPA_PM_ERR("Invalid Param\n"); |
| 754 | return -EINVAL; |
| 755 | } |
| 756 | |
| 757 | if (ipa_pm_ctx->clients[hdl] == NULL) { |
| 758 | IPA_PM_ERR("Client is Null\n"); |
| 759 | return -EINVAL; |
| 760 | } |
| 761 | |
| 762 | IPA_PM_DBG("IPA PM deregistering client\n"); |
| 763 | |
| 764 | client = ipa_pm_ctx->clients[hdl]; |
| 765 | spin_lock_irqsave(&client->state_lock, flags); |
| 766 | if (IPA_PM_STATE_IN_PROGRESS(client->state)) { |
| 767 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 768 | wait_for_completion(&client->complete); |
| 769 | spin_lock_irqsave(&client->state_lock, flags); |
| 770 | } |
| 771 | |
| 772 | if (IPA_PM_STATE_ACTIVE(client->state)) { |
| 773 | IPA_PM_DBG("Activated clients cannot be deregistered"); |
| 774 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 775 | return -EPERM; |
| 776 | } |
| 777 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 778 | |
| 779 | mutex_lock(&ipa_pm_ctx->client_mutex); |
| 780 | |
| 781 | /* nullify pointers in pipe array */ |
| 782 | for (i = 0; i < IPA3_MAX_NUM_PIPES; i++) { |
| 783 | if (ipa_pm_ctx->clients_by_pipe[i] == ipa_pm_ctx->clients[hdl]) |
| 784 | ipa_pm_ctx->clients_by_pipe[i] = NULL; |
| 785 | } |
| 786 | kfree(client); |
| 787 | ipa_pm_ctx->clients[hdl] = NULL; |
| 788 | |
| 789 | remove_client_from_exception_list(hdl); |
| 790 | IPA_PM_DBG("IPA PM client %d deregistered\n", hdl); |
| 791 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 792 | |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /** |
| 797 | * ipa_pm_associate_ipa_cons_to_client() - add mapping to pipe with ipa cllent |
| 798 | * @hdl: index of the client to be mapped |
| 799 | * @consumer: the pipe/consumer name to be pipped to the client |
| 800 | * |
| 801 | * Returns: 0 on success, negative on failure |
| 802 | * |
| 803 | * Side effects: multiple pipes are allowed to be mapped to a single client |
| 804 | */ |
| 805 | int ipa_pm_associate_ipa_cons_to_client(u32 hdl, enum ipa_client_type consumer) |
| 806 | { |
| 807 | int idx; |
| 808 | |
| 809 | if (hdl >= IPA_PM_MAX_CLIENTS || consumer < 0 || |
| 810 | consumer >= IPA_CLIENT_MAX) { |
| 811 | IPA_PM_ERR("invalid params\n"); |
| 812 | return -EINVAL; |
| 813 | } |
| 814 | |
| 815 | mutex_lock(&ipa_pm_ctx->client_mutex); |
| 816 | idx = ipa_get_ep_mapping(consumer); |
| 817 | |
| 818 | IPA_PM_DBG("Mapping pipe %d to client %d\n", idx, hdl); |
| 819 | |
| 820 | if (idx < 0) { |
| 821 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 822 | IPA_PM_DBG("Pipe is not used\n"); |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | if (ipa_pm_ctx->clients[hdl] == NULL) { |
| 827 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 828 | IPA_PM_ERR("Client is NULL\n"); |
| 829 | return -EPERM; |
| 830 | } |
| 831 | |
| 832 | if (ipa_pm_ctx->clients_by_pipe[idx] != NULL) { |
| 833 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 834 | IPA_PM_ERR("Pipe is already mapped\n"); |
| 835 | return -EPERM; |
| 836 | } |
| 837 | ipa_pm_ctx->clients_by_pipe[idx] = ipa_pm_ctx->clients[hdl]; |
| 838 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 839 | |
| 840 | IPA_PM_DBG("Pipe %d is mapped to client %d\n", idx, hdl); |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
| 845 | static int ipa_pm_activate_helper(struct ipa_pm_client *client, bool sync) |
| 846 | { |
| 847 | struct ipa_active_client_logging_info log_info; |
| 848 | int result = 0; |
| 849 | unsigned long flags; |
| 850 | |
| 851 | spin_lock_irqsave(&client->state_lock, flags); |
| 852 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 853 | |
| 854 | if (IPA_PM_STATE_IN_PROGRESS(client->state)) { |
| 855 | if (sync) { |
| 856 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 857 | wait_for_completion(&client->complete); |
| 858 | spin_lock_irqsave(&client->state_lock, flags); |
| 859 | } else { |
| 860 | client->state = IPA_PM_ACTIVATE_IN_PROGRESS; |
| 861 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 862 | return -EINPROGRESS; |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | switch (client->state) { |
| 867 | case IPA_PM_ACTIVATED_PENDING_RESCHEDULE: |
| 868 | case IPA_PM_ACTIVATED_PENDING_DEACTIVATION: |
| 869 | client->state = IPA_PM_ACTIVATED_TIMER_SET; |
| 870 | case IPA_PM_ACTIVATED: |
| 871 | case IPA_PM_ACTIVATED_TIMER_SET: |
| 872 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 873 | return 0; |
| 874 | case IPA_PM_DEACTIVATED: |
| 875 | break; |
| 876 | default: |
| 877 | IPA_PM_ERR("Invalid State\n"); |
| 878 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 879 | return -EPERM; |
| 880 | } |
| 881 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 882 | |
| 883 | IPA_ACTIVE_CLIENTS_PREP_SPECIAL(log_info, client->name); |
| 884 | if (!client->skip_clk_vote) { |
| 885 | if (sync) { |
| 886 | client->state = IPA_PM_ACTIVATE_IN_PROGRESS; |
| 887 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 888 | IPA_ACTIVE_CLIENTS_INC_SPECIAL(client->name); |
| 889 | spin_lock_irqsave(&client->state_lock, flags); |
| 890 | } else |
| 891 | result = ipa3_inc_client_enable_clks_no_block |
| 892 | (&log_info); |
| 893 | } |
| 894 | |
| 895 | /* we got the clocks */ |
| 896 | if (result == 0) { |
| 897 | client->state = IPA_PM_ACTIVATED; |
| 898 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 899 | activate_client(client->hdl); |
| 900 | if (sync) |
| 901 | do_clk_scaling(); |
| 902 | else |
| 903 | queue_work(ipa_pm_ctx->wq, |
| 904 | &ipa_pm_ctx->clk_scaling.work); |
| 905 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 906 | return 0; |
| 907 | } |
| 908 | |
| 909 | client->state = IPA_PM_ACTIVATE_IN_PROGRESS; |
| 910 | init_completion(&client->complete); |
| 911 | queue_work(ipa_pm_ctx->wq, &client->activate_work); |
| 912 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 913 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 914 | return -EINPROGRESS; |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * ipa_pm_activate(): activate ipa client to vote for clock(). Can be called |
| 919 | * from atomic context and returns -EINPROGRESS if cannot be done synchronously |
| 920 | * @hdl: index of the client in the array |
| 921 | * |
| 922 | * Returns: 0 on success, -EINPROGRESS if operation cannot be done synchronously |
| 923 | * and other negatives on failure |
| 924 | */ |
| 925 | int ipa_pm_activate(u32 hdl) |
| 926 | { |
| 927 | if (hdl >= IPA_PM_MAX_CLIENTS || ipa_pm_ctx->clients[hdl] == NULL) { |
| 928 | IPA_PM_ERR("Invalid Param\n"); |
| 929 | return -EINVAL; |
| 930 | } |
| 931 | |
| 932 | return ipa_pm_activate_helper(ipa_pm_ctx->clients[hdl], false); |
| 933 | } |
| 934 | |
| 935 | /** |
| 936 | * ipa_pm_activate(): activate ipa client to vote for clock synchronously. |
| 937 | * Cannot be called from an atomic contex. |
| 938 | * @hdl: index of the client in the array |
| 939 | * |
| 940 | * Returns: 0 on success, negative on failure |
| 941 | */ |
| 942 | int ipa_pm_activate_sync(u32 hdl) |
| 943 | { |
| 944 | if (hdl >= IPA_PM_MAX_CLIENTS || ipa_pm_ctx->clients[hdl] == NULL) { |
| 945 | IPA_PM_ERR("Invalid Param\n"); |
| 946 | return -EINVAL; |
| 947 | } |
| 948 | |
| 949 | return ipa_pm_activate_helper(ipa_pm_ctx->clients[hdl], true); |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * ipa_pm_deferred_deactivate(): schedule a timer to deactivate client and |
| 954 | * devote clock. Can be called from atomic context (asynchronously) |
| 955 | * @hdl: index of the client in the array |
| 956 | * |
| 957 | * Returns: 0 on success, negative on failure |
| 958 | */ |
| 959 | int ipa_pm_deferred_deactivate(u32 hdl) |
| 960 | { |
| 961 | struct ipa_pm_client *client; |
| 962 | unsigned long flags; |
| 963 | |
| 964 | if (hdl >= IPA_PM_MAX_CLIENTS || ipa_pm_ctx->clients[hdl] == NULL) { |
| 965 | IPA_PM_ERR("Invalid Param\n"); |
| 966 | return -EINVAL; |
| 967 | } |
| 968 | |
| 969 | client = ipa_pm_ctx->clients[hdl]; |
| 970 | IPA_PM_DBG_STATE(hdl, client->name, client->state); |
| 971 | |
| 972 | spin_lock_irqsave(&client->state_lock, flags); |
| 973 | switch (client->state) { |
| 974 | case IPA_PM_ACTIVATE_IN_PROGRESS: |
| 975 | client->state = IPA_PM_DEACTIVATE_IN_PROGRESS; |
| 976 | case IPA_PM_DEACTIVATED: |
| 977 | IPA_PM_DBG_STATE(hdl, client->name, client->state); |
| 978 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 979 | return 0; |
| 980 | case IPA_PM_ACTIVATED: |
| 981 | client->state = IPA_PM_ACTIVATED_PENDING_DEACTIVATION; |
| 982 | queue_delayed_work(ipa_pm_ctx->wq, &client->deactivate_work, |
| 983 | msecs_to_jiffies(IPA_PM_DEFERRED_TIMEOUT)); |
| 984 | break; |
| 985 | case IPA_PM_ACTIVATED_TIMER_SET: |
| 986 | case IPA_PM_ACTIVATED_PENDING_DEACTIVATION: |
| 987 | client->state = IPA_PM_ACTIVATED_PENDING_RESCHEDULE; |
| 988 | case IPA_PM_DEACTIVATE_IN_PROGRESS: |
| 989 | case IPA_PM_ACTIVATED_PENDING_RESCHEDULE: |
| 990 | break; |
| 991 | } |
| 992 | IPA_PM_DBG_STATE(hdl, client->name, client->state); |
| 993 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 994 | |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | /** |
| 999 | * ipa_pm_deactivate_all_deferred(): Cancel the deferred deactivation timer and |
| 1000 | * immediately devotes for IPA clocks |
| 1001 | * |
| 1002 | * Returns: 0 on success, negative on failure |
| 1003 | */ |
| 1004 | int ipa_pm_deactivate_all_deferred(void) |
| 1005 | { |
| 1006 | int i; |
| 1007 | bool run_algorithm = false; |
| 1008 | struct ipa_pm_client *client; |
| 1009 | unsigned long flags; |
| 1010 | |
| 1011 | for (i = 0; i < IPA_PM_MAX_CLIENTS; i++) { |
| 1012 | client = ipa_pm_ctx->clients[i]; |
| 1013 | |
| 1014 | if (client == NULL) |
| 1015 | continue; |
| 1016 | |
| 1017 | cancel_delayed_work_sync(&client->deactivate_work); |
| 1018 | |
| 1019 | if (IPA_PM_STATE_IN_PROGRESS(client->state)) { |
| 1020 | wait_for_completion(&client->complete); |
| 1021 | continue; |
| 1022 | } |
| 1023 | |
| 1024 | spin_lock_irqsave(&client->state_lock, flags); |
| 1025 | IPA_PM_DBG_STATE(client->hdl, client->name, client->state); |
| 1026 | |
| 1027 | if (client->state == IPA_PM_ACTIVATED_TIMER_SET) { |
| 1028 | client->state = IPA_PM_ACTIVATED; |
| 1029 | IPA_PM_DBG_STATE(client->hdl, client->name, |
| 1030 | client->state); |
| 1031 | spin_unlock_irqrestore(&client->state_lock, flags); |
Ghanim Fodi | a7af633 | 2017-11-28 20:32:07 +0200 | [diff] [blame] | 1032 | } else if (client->state == |
Chris Fries | 012712b | 2017-12-12 23:30:04 +0200 | [diff] [blame] | 1033 | IPA_PM_ACTIVATED_PENDING_DEACTIVATION || |
| 1034 | client->state == |
| 1035 | IPA_PM_ACTIVATED_PENDING_RESCHEDULE) { |
Michael Adisumarta | c06df41 | 2017-09-19 10:10:35 -0700 | [diff] [blame] | 1036 | run_algorithm = true; |
| 1037 | client->state = IPA_PM_DEACTIVATED; |
| 1038 | IPA_PM_DBG_STATE(client->hdl, client->name, |
| 1039 | client->state); |
| 1040 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 1041 | ipa_set_tag_process_before_gating(true); |
| 1042 | if (!client->skip_clk_vote) |
| 1043 | IPA_ACTIVE_CLIENTS_DEC_SPECIAL(client->name); |
| 1044 | deactivate_client(client->hdl); |
| 1045 | } else /* if activated or deactivated, we do nothing */ |
| 1046 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 1047 | } |
| 1048 | |
| 1049 | if (run_algorithm) |
| 1050 | do_clk_scaling(); |
| 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | /** |
| 1056 | * ipa_pm_deactivate_sync(): deactivate ipa client and devote clock. Cannot be |
| 1057 | * called from atomic context. |
| 1058 | * @hdl: index of the client in the array |
| 1059 | * |
| 1060 | * Returns: 0 on success, negative on failure |
| 1061 | */ |
| 1062 | int ipa_pm_deactivate_sync(u32 hdl) |
| 1063 | { |
| 1064 | struct ipa_pm_client *client = ipa_pm_ctx->clients[hdl]; |
| 1065 | unsigned long flags; |
| 1066 | |
| 1067 | if (hdl >= IPA_PM_MAX_CLIENTS || ipa_pm_ctx->clients[hdl] == NULL) { |
| 1068 | IPA_PM_ERR("Invalid Param\n"); |
| 1069 | return -EINVAL; |
| 1070 | } |
| 1071 | |
| 1072 | cancel_delayed_work_sync(&client->deactivate_work); |
| 1073 | |
| 1074 | if (IPA_PM_STATE_IN_PROGRESS(client->state)) |
| 1075 | wait_for_completion(&client->complete); |
| 1076 | |
| 1077 | spin_lock_irqsave(&client->state_lock, flags); |
| 1078 | IPA_PM_DBG_STATE(hdl, client->name, client->state); |
| 1079 | |
| 1080 | if (client->state == IPA_PM_DEACTIVATED) { |
| 1081 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 1086 | |
| 1087 | /* else case (Deactivates all Activated cases)*/ |
| 1088 | ipa_set_tag_process_before_gating(true); |
| 1089 | if (!client->skip_clk_vote) |
| 1090 | IPA_ACTIVE_CLIENTS_DEC_SPECIAL(client->name); |
| 1091 | |
| 1092 | spin_lock_irqsave(&client->state_lock, flags); |
| 1093 | client->state = IPA_PM_DEACTIVATED; |
| 1094 | IPA_PM_DBG_STATE(hdl, client->name, client->state); |
| 1095 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 1096 | deactivate_client(hdl); |
| 1097 | do_clk_scaling(); |
| 1098 | |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | /** |
| 1103 | * ipa_pm_handle_suspend(): calls the callbacks of suspended clients to wake up |
| 1104 | * @pipe_bitmask: the bits represent the indexes of the clients to be woken up |
| 1105 | * |
| 1106 | * Returns: 0 on success, negative on failure |
| 1107 | */ |
| 1108 | int ipa_pm_handle_suspend(u32 pipe_bitmask) |
| 1109 | { |
| 1110 | int i; |
| 1111 | struct ipa_pm_client *client; |
| 1112 | bool client_notified[IPA_PM_MAX_CLIENTS] = { false }; |
| 1113 | |
| 1114 | IPA_PM_DBG_LOW("bitmask: %d", pipe_bitmask); |
| 1115 | |
| 1116 | if (pipe_bitmask == 0) |
| 1117 | return 0; |
| 1118 | |
| 1119 | mutex_lock(&ipa_pm_ctx->client_mutex); |
| 1120 | for (i = 0; i < IPA3_MAX_NUM_PIPES; i++) { |
| 1121 | if (pipe_bitmask & (1 << i)) { |
| 1122 | client = ipa_pm_ctx->clients_by_pipe[i]; |
| 1123 | if (client && client_notified[client->hdl] == false) { |
Michael Adisumarta | ce75fc7 | 2017-10-04 11:42:11 -0700 | [diff] [blame] | 1124 | if (client->callback) { |
| 1125 | client->callback(client->callback_params |
| 1126 | , IPA_PM_REQUEST_WAKEUP); |
| 1127 | client_notified[client->hdl] = true; |
| 1128 | } else { |
| 1129 | IPA_PM_ERR("client has no callback"); |
| 1130 | WARN_ON(1); |
| 1131 | } |
Michael Adisumarta | c06df41 | 2017-09-19 10:10:35 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * ipa_pm_set_perf_profile(): Adds/changes the throughput requirement to IPA PM |
| 1141 | * to be used for clock scaling |
| 1142 | * @hdl: index of the client in the array |
| 1143 | * @throughput: the new throughput value to be set for that client |
| 1144 | * |
| 1145 | * Returns: 0 on success, negative on failure |
| 1146 | */ |
| 1147 | int ipa_pm_set_perf_profile(u32 hdl, int throughput) |
| 1148 | { |
| 1149 | struct ipa_pm_client *client = ipa_pm_ctx->clients[hdl]; |
| 1150 | unsigned long flags; |
| 1151 | |
| 1152 | if (hdl >= IPA_PM_MAX_CLIENTS || ipa_pm_ctx->clients[hdl] == NULL |
| 1153 | || throughput < 0) { |
| 1154 | IPA_PM_ERR("Invalid Params\n"); |
| 1155 | return -EINVAL; |
| 1156 | } |
| 1157 | |
| 1158 | mutex_lock(&ipa_pm_ctx->client_mutex); |
| 1159 | if (client->group == IPA_PM_GROUP_DEFAULT) |
| 1160 | IPA_PM_DBG_LOW("Old throughput: %d\n", client->throughput); |
| 1161 | else |
| 1162 | IPA_PM_DBG_LOW("old Group %d throughput: %d\n", |
| 1163 | client->group, ipa_pm_ctx->group_tput[client->group]); |
| 1164 | |
| 1165 | if (client->group == IPA_PM_GROUP_DEFAULT) |
| 1166 | client->throughput = throughput; |
| 1167 | else |
| 1168 | ipa_pm_ctx->group_tput[client->group] = throughput; |
| 1169 | |
| 1170 | if (client->group == IPA_PM_GROUP_DEFAULT) |
| 1171 | IPA_PM_DBG_LOW("New throughput: %d\n", client->throughput); |
| 1172 | else |
| 1173 | IPA_PM_DBG_LOW("New Group %d throughput: %d\n", |
| 1174 | client->group, ipa_pm_ctx->group_tput[client->group]); |
| 1175 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 1176 | |
| 1177 | spin_lock_irqsave(&client->state_lock, flags); |
Michael Adisumarta | 3a6f600 | 2017-09-26 16:48:12 -0700 | [diff] [blame] | 1178 | if (IPA_PM_STATE_ACTIVE(client->state) || (client->group != |
| 1179 | IPA_PM_GROUP_DEFAULT)) { |
Michael Adisumarta | c06df41 | 2017-09-19 10:10:35 -0700 | [diff] [blame] | 1180 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 1181 | do_clk_scaling(); |
| 1182 | return 0; |
| 1183 | } |
| 1184 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 1185 | |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | /** |
| 1190 | * ipa_pm_stat() - print PM stat |
| 1191 | * @buf: [in] The user buff used to print |
| 1192 | * @size: [in] The size of buf |
| 1193 | * Returns: number of bytes used on success, negative on failure |
| 1194 | * |
| 1195 | * This function is called by ipa_debugfs in order to receive |
| 1196 | * a picture of the clients in the PM and the throughput, threshold and cur vote |
| 1197 | */ |
| 1198 | int ipa_pm_stat(char *buf, int size) |
| 1199 | { |
| 1200 | struct ipa_pm_client *client; |
| 1201 | struct clk_scaling_db *clk = &ipa_pm_ctx->clk_scaling; |
| 1202 | int i, j, tput, cnt = 0, result = 0; |
| 1203 | unsigned long flags; |
| 1204 | |
| 1205 | if (!buf || size < 0) |
| 1206 | return -EINVAL; |
| 1207 | |
| 1208 | mutex_lock(&ipa_pm_ctx->client_mutex); |
| 1209 | |
| 1210 | result = scnprintf(buf + cnt, size - cnt, "\n\nCurrent threshold: ["); |
| 1211 | cnt += result; |
| 1212 | |
| 1213 | for (i = 0; i < clk->threshold_size; i++) { |
| 1214 | result = scnprintf(buf + cnt, size - cnt, |
| 1215 | "%d, ", clk->current_threshold[i]); |
| 1216 | cnt += result; |
| 1217 | } |
| 1218 | |
| 1219 | result = scnprintf(buf + cnt, size - cnt, "\b\b]\n"); |
| 1220 | cnt += result; |
| 1221 | |
| 1222 | result = scnprintf(buf + cnt, size - cnt, |
| 1223 | "Aggregated tput: %d, Cur vote: %d", |
| 1224 | ipa_pm_ctx->aggregated_tput, clk->cur_vote); |
| 1225 | cnt += result; |
| 1226 | |
| 1227 | result = scnprintf(buf + cnt, size - cnt, "\n\nRegistered Clients:\n"); |
| 1228 | cnt += result; |
| 1229 | |
| 1230 | |
| 1231 | for (i = 0; i < IPA_PM_MAX_CLIENTS; i++) { |
| 1232 | client = ipa_pm_ctx->clients[i]; |
| 1233 | |
| 1234 | if (client == NULL) |
| 1235 | continue; |
| 1236 | |
| 1237 | spin_lock_irqsave(&client->state_lock, flags); |
| 1238 | if (client->group == IPA_PM_GROUP_DEFAULT) |
| 1239 | tput = client->throughput; |
| 1240 | else |
| 1241 | tput = ipa_pm_ctx->group_tput[client->group]; |
| 1242 | |
| 1243 | result = scnprintf(buf + cnt, size - cnt, |
| 1244 | "Client[%d]: %s State:%s\nGroup: %s Throughput: %d Pipes: ", |
| 1245 | i, client->name, client_state_to_str[client->state], |
| 1246 | ipa_pm_group_to_str[client->group], tput); |
| 1247 | cnt += result; |
| 1248 | |
| 1249 | for (j = 0; j < IPA3_MAX_NUM_PIPES; j++) { |
| 1250 | if (ipa_pm_ctx->clients_by_pipe[j] == client) { |
| 1251 | result = scnprintf(buf + cnt, size - cnt, |
| 1252 | "%d, ", j); |
| 1253 | cnt += result; |
| 1254 | } |
| 1255 | } |
| 1256 | |
| 1257 | result = scnprintf(buf + cnt, size - cnt, "\b\b\n\n"); |
| 1258 | cnt += result; |
| 1259 | spin_unlock_irqrestore(&client->state_lock, flags); |
| 1260 | } |
| 1261 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 1262 | |
| 1263 | return cnt; |
| 1264 | } |
| 1265 | |
| 1266 | /** |
| 1267 | * ipa_pm_exceptions_stat() - print PM exceptions stat |
| 1268 | * @buf: [in] The user buff used to print |
| 1269 | * @size: [in] The size of buf |
| 1270 | * Returns: number of bytes used on success, negative on failure |
| 1271 | * |
| 1272 | * This function is called by ipa_debugfs in order to receive |
| 1273 | * a full picture of the exceptions in the PM |
| 1274 | */ |
| 1275 | int ipa_pm_exceptions_stat(char *buf, int size) |
| 1276 | { |
| 1277 | int i, j, cnt = 0, result = 0; |
| 1278 | struct ipa_pm_exception_list *exception; |
| 1279 | |
| 1280 | if (!buf || size < 0) |
| 1281 | return -EINVAL; |
| 1282 | |
| 1283 | result = scnprintf(buf + cnt, size - cnt, "\n"); |
| 1284 | cnt += result; |
| 1285 | |
| 1286 | mutex_lock(&ipa_pm_ctx->client_mutex); |
| 1287 | for (i = 0; i < ipa_pm_ctx->clk_scaling.exception_size; i++) { |
| 1288 | exception = &ipa_pm_ctx->clk_scaling.exception_list[i]; |
| 1289 | if (exception == NULL) { |
| 1290 | result = scnprintf(buf + cnt, size - cnt, |
| 1291 | "Exception %d is NULL\n\n", i); |
| 1292 | cnt += result; |
| 1293 | continue; |
| 1294 | } |
| 1295 | |
| 1296 | result = scnprintf(buf + cnt, size - cnt, |
| 1297 | "Exception %d: %s\nPending: %d Bitmask: %d Threshold: [" |
| 1298 | , i, exception->clients, exception->pending, |
| 1299 | exception->bitmask); |
| 1300 | cnt += result; |
| 1301 | for (j = 0; j < ipa_pm_ctx->clk_scaling.threshold_size; j++) { |
| 1302 | result = scnprintf(buf + cnt, size - cnt, |
| 1303 | "%d, ", exception->threshold[j]); |
| 1304 | cnt += result; |
| 1305 | } |
| 1306 | result = scnprintf(buf + cnt, size - cnt, "\b\b]\n\n"); |
| 1307 | cnt += result; |
| 1308 | } |
| 1309 | mutex_unlock(&ipa_pm_ctx->client_mutex); |
| 1310 | |
| 1311 | return cnt; |
| 1312 | } |