Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drivers: qcom: rpmh: add support for batch RPMH request

Platform drivers need make a lot of resource state requests at the same
time, say, at the start or end of an usecase. It can be quite
inefficient to send each request separately. Instead they can give the
RPMH library a batch of requests to be sent and wait on the whole
transaction to be complete.

rpmh_write_batch() is a blocking call that can be used to send multiple
RPMH command sets. Each RPMH command set is set asynchronously and the
API blocks until all the command sets are complete and receive their
tx_done callbacks.

Signed-off-by: Lina Iyer <ilina@codeaurora.org>
Signed-off-by: Raju P.L.S.S.S.N <rplsssn@codeaurora.org>
Reviewed-by: Matthias Kaehlcke <mka@chromium.org>
Signed-off-by: Andy Gross <andy.gross@linaro.org>

authored by

Lina Iyer and committed by
Andy Gross
c8790cb6 564b5e24

+169 -3
+2
drivers/soc/qcom/rpmh-internal.h
··· 71 71 * @cache: the list of cached requests 72 72 * @cache_lock: synchronize access to the cache data 73 73 * @dirty: was the cache updated since flush 74 + * @batch_cache: Cache sleep and wake requests sent as batch 74 75 */ 75 76 struct rpmh_ctrlr { 76 77 struct list_head cache; 77 78 spinlock_t cache_lock; 78 79 bool dirty; 80 + struct list_head batch_cache; 79 81 }; 80 82 81 83 /**
+1
drivers/soc/qcom/rpmh-rsc.c
··· 636 636 637 637 spin_lock_init(&drv->client.cache_lock); 638 638 INIT_LIST_HEAD(&drv->client.cache); 639 + INIT_LIST_HEAD(&drv->client.batch_cache); 639 640 640 641 dev_set_drvdata(&pdev->dev, drv); 641 642
+158 -3
drivers/soc/qcom/rpmh.c
··· 54 54 struct list_head list; 55 55 }; 56 56 57 + /** 58 + * struct batch_cache_req - An entry in our batch catch 59 + * 60 + * @list: linked list obj 61 + * @count: number of messages 62 + * @rpm_msgs: the messages 63 + */ 64 + 65 + struct batch_cache_req { 66 + struct list_head list; 67 + int count; 68 + struct rpmh_request rpm_msgs[]; 69 + }; 70 + 57 71 static struct rpmh_ctrlr *get_rpmh_ctrlr(const struct device *dev) 58 72 { 59 73 struct rsc_drv *drv = dev_get_drvdata(dev->parent); ··· 87 73 dev_err(rpm_msg->dev, "RPMH TX fail in msg addr=%#x, err=%d\n", 88 74 rpm_msg->msg.cmds[0].addr, r); 89 75 90 - /* Signal the blocking thread we are done */ 91 - if (compl) 92 - complete(compl); 76 + if (!compl) 77 + goto exit; 93 78 79 + /* Signal the blocking thread we are done */ 80 + complete(compl); 81 + 82 + exit: 94 83 if (rpm_msg->needs_free) 95 84 kfree(rpm_msg); 96 85 } ··· 281 264 } 282 265 EXPORT_SYMBOL(rpmh_write); 283 266 267 + static void cache_batch(struct rpmh_ctrlr *ctrlr, struct batch_cache_req *req) 268 + { 269 + unsigned long flags; 270 + 271 + spin_lock_irqsave(&ctrlr->cache_lock, flags); 272 + list_add_tail(&req->list, &ctrlr->batch_cache); 273 + spin_unlock_irqrestore(&ctrlr->cache_lock, flags); 274 + } 275 + 276 + static int flush_batch(struct rpmh_ctrlr *ctrlr) 277 + { 278 + struct batch_cache_req *req; 279 + const struct rpmh_request *rpm_msg; 280 + unsigned long flags; 281 + int ret = 0; 282 + int i; 283 + 284 + /* Send Sleep/Wake requests to the controller, expect no response */ 285 + spin_lock_irqsave(&ctrlr->cache_lock, flags); 286 + list_for_each_entry(req, &ctrlr->batch_cache, list) { 287 + for (i = 0; i < req->count; i++) { 288 + rpm_msg = req->rpm_msgs + i; 289 + ret = rpmh_rsc_write_ctrl_data(ctrlr_to_drv(ctrlr), 290 + &rpm_msg->msg); 291 + if (ret) 292 + break; 293 + } 294 + } 295 + spin_unlock_irqrestore(&ctrlr->cache_lock, flags); 296 + 297 + return ret; 298 + } 299 + 300 + static void invalidate_batch(struct rpmh_ctrlr *ctrlr) 301 + { 302 + struct batch_cache_req *req, *tmp; 303 + unsigned long flags; 304 + 305 + spin_lock_irqsave(&ctrlr->cache_lock, flags); 306 + list_for_each_entry_safe(req, tmp, &ctrlr->batch_cache, list) 307 + kfree(req); 308 + INIT_LIST_HEAD(&ctrlr->batch_cache); 309 + spin_unlock_irqrestore(&ctrlr->cache_lock, flags); 310 + } 311 + 312 + /** 313 + * rpmh_write_batch: Write multiple sets of RPMH commands and wait for the 314 + * batch to finish. 315 + * 316 + * @dev: the device making the request 317 + * @state: Active/sleep set 318 + * @cmd: The payload data 319 + * @n: The array of count of elements in each batch, 0 terminated. 320 + * 321 + * Write a request to the RSC controller without caching. If the request 322 + * state is ACTIVE, then the requests are treated as completion request 323 + * and sent to the controller immediately. The function waits until all the 324 + * commands are complete. If the request was to SLEEP or WAKE_ONLY, then the 325 + * request is sent as fire-n-forget and no ack is expected. 326 + * 327 + * May sleep. Do not call from atomic contexts for ACTIVE_ONLY requests. 328 + */ 329 + int rpmh_write_batch(const struct device *dev, enum rpmh_state state, 330 + const struct tcs_cmd *cmd, u32 *n) 331 + { 332 + struct batch_cache_req *req; 333 + struct rpmh_request *rpm_msgs; 334 + DECLARE_COMPLETION_ONSTACK(compl); 335 + struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev); 336 + unsigned long time_left; 337 + int count = 0; 338 + int ret, i, j; 339 + 340 + if (!cmd || !n) 341 + return -EINVAL; 342 + 343 + while (n[count] > 0) 344 + count++; 345 + if (!count) 346 + return -EINVAL; 347 + 348 + req = kzalloc(sizeof(*req) + count * sizeof(req->rpm_msgs[0]), 349 + GFP_ATOMIC); 350 + if (!req) 351 + return -ENOMEM; 352 + req->count = count; 353 + rpm_msgs = req->rpm_msgs; 354 + 355 + for (i = 0; i < count; i++) { 356 + __fill_rpmh_msg(rpm_msgs + i, state, cmd, n[i]); 357 + cmd += n[i]; 358 + } 359 + 360 + if (state != RPMH_ACTIVE_ONLY_STATE) { 361 + cache_batch(ctrlr, req); 362 + return 0; 363 + } 364 + 365 + for (i = 0; i < count; i++) { 366 + rpm_msgs[i].completion = &compl; 367 + ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msgs[i].msg); 368 + if (ret) { 369 + pr_err("Error(%d) sending RPMH message addr=%#x\n", 370 + ret, rpm_msgs[i].msg.cmds[0].addr); 371 + for (j = i; j < count; j++) 372 + rpmh_tx_done(&rpm_msgs[j].msg, ret); 373 + break; 374 + } 375 + } 376 + 377 + time_left = RPMH_TIMEOUT_MS; 378 + for (i = 0; i < count; i++) { 379 + time_left = wait_for_completion_timeout(&compl, time_left); 380 + if (!time_left) { 381 + /* 382 + * Better hope they never finish because they'll signal 383 + * the completion on our stack and that's bad once 384 + * we've returned from the function. 385 + */ 386 + WARN_ON(1); 387 + ret = -ETIMEDOUT; 388 + goto exit; 389 + } 390 + } 391 + 392 + exit: 393 + kfree(req); 394 + 395 + return ret; 396 + } 397 + EXPORT_SYMBOL(rpmh_write_batch); 398 + 284 399 static int is_req_valid(struct cache_req *req) 285 400 { 286 401 return (req->sleep_val != UINT_MAX && ··· 458 309 return 0; 459 310 } 460 311 312 + /* First flush the cached batch requests */ 313 + ret = flush_batch(ctrlr); 314 + if (ret) 315 + return ret; 316 + 461 317 /* 462 318 * Nobody else should be calling this function other than system PM, 463 319 * hence we can run without locks. ··· 501 347 struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev); 502 348 int ret; 503 349 350 + invalidate_batch(ctrlr); 504 351 ctrlr->dirty = true; 505 352 506 353 do {
+8
include/soc/qcom/rpmh.h
··· 17 17 int rpmh_write_async(const struct device *dev, enum rpmh_state state, 18 18 const struct tcs_cmd *cmd, u32 n); 19 19 20 + int rpmh_write_batch(const struct device *dev, enum rpmh_state state, 21 + const struct tcs_cmd *cmd, u32 *n); 22 + 20 23 int rpmh_flush(const struct device *dev); 21 24 22 25 int rpmh_invalidate(const struct device *dev); ··· 33 30 static inline int rpmh_write_async(const struct device *dev, 34 31 enum rpmh_state state, 35 32 const struct tcs_cmd *cmd, u32 n) 33 + { return -ENODEV; } 34 + 35 + static inline int rpmh_write_batch(const struct device *dev, 36 + enum rpmh_state state, 37 + const struct tcs_cmd *cmd, u32 *n) 36 38 { return -ENODEV; } 37 39 38 40 static inline int rpmh_flush(const struct device *dev)