An HPASS domain can load either an audio firmware image or a compute firmware image, and only a compute image exposes an unsigned PD node in addition to the signed one. Which image is loaded is only knowable by querying the DSP's capability attributes, and the driver needs that answer before it can decide which /dev/fastrpc-* device nodes to create. Today the query happens lazily on the first FASTRPC_IOCTL_GET_DSP_INFO call from userspace, well after the device nodes have already been created from the domain id alone.
Move the query to probe time, before the device nodes are registered, so the set of nodes created reflects the DSP's actual capabilities. A synchronous query in fastrpc_rpmsg_probe() would work but fastrpc_internal_invoke() can block for up to 10 seconds waiting on the DSP, and a target can have several DSP domains probing concurrently, so a single slow or silent domain would delay bring-up of the rest. Instead, run the query on a per-channel high-priority ordered workqueue (WQ_HIGHPRI | WQ_MEM_RECLAIM) so that probe() returns immediately, and register the device nodes from that work item once the query completes or times out. fastrpc_rpmsg_remove() cancels the pending work and destroys the per-channel workqueue before tearing down the rest of the channel state. The temporary client used to drive the query is linked into cctx->users for the duration of the invoke so that a concurrent channel removal aborts it via fastrpc_notify_users() instead of blocking on the full invoke timeout. Signed-off-by: Anandu Krishnan E <[email protected]> --- drivers/misc/fastrpc.c | 225 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 177 insertions(+), 48 deletions(-) diff --git a/drivers/misc/fastrpc.c b/drivers/misc/fastrpc.c index c515097c3d3b..59ef755feeaf 100644 --- a/drivers/misc/fastrpc.c +++ b/drivers/misc/fastrpc.c @@ -338,6 +338,10 @@ struct fastrpc_channel_ctx { atomic_t ctx_seq; u64 dma_mask; const struct fastrpc_soc_data *soc_data; + /* Per-channel workqueue for the probe-time DSP capability query */ + struct workqueue_struct *cap_wq; + struct work_struct cap_query_work; + char domain[8]; }; struct fastrpc_device { @@ -2704,6 +2708,159 @@ static int fastrpc_init_reserved_mem(struct fastrpc_channel_ctx *cctx, cctx->vmperms, cctx->vmcount); } +static int fastrpc_register_devices(struct fastrpc_channel_ctx *cctx, + struct rpmsg_device *rpdev) +{ + struct device *rdev = &rpdev->dev; + int err, i; + + switch (cctx->domain_id) { + case ADSP_DOMAIN_ID: + case MDSP_DOMAIN_ID: + case SDSP_DOMAIN_ID: + case HPASS_DOMAIN_ID: + cctx->unsigned_support = false; + err = fastrpc_device_register(rdev, cctx, cctx->secure, cctx->domain); + if (err) + goto err_depopulate; + break; + case CDSP_DOMAIN_ID: + case GDSP_DOMAIN_ID: + cctx->unsigned_support = true; + err = fastrpc_device_register(rdev, cctx, true, cctx->domain); + if (err) + goto err_depopulate; + err = fastrpc_device_register(rdev, cctx, false, cctx->domain); + if (err) { + misc_deregister(&cctx->secure_fdevice->miscdev); + cctx->secure_fdevice = NULL; + goto err_depopulate; + } + break; + default: + err = -EINVAL; + goto err_depopulate; + } + + /* Duplicate the last session so multiple processes can share + * the same compute bank. + */ + if (cctx->domain_id == ADSP_DOMAIN_ID && cctx->sesscount > 0) { + struct fastrpc_session_ctx *last_sess; + struct fastrpc_session_ctx *dup_sess; + unsigned long flags; + + spin_lock_irqsave(&cctx->lock, flags); + last_sess = &cctx->session[cctx->sesscount - 1]; + for (i = 0; i < FASTRPC_DUP_SESSIONS; i++) { + if (cctx->sesscount >= FASTRPC_MAX_SESSIONS) + break; + dup_sess = &cctx->session[cctx->sesscount++]; + memcpy(dup_sess, last_sess, sizeof(*dup_sess)); + } + spin_unlock_irqrestore(&cctx->lock, flags); + } + + return 0; + +err_depopulate: + fastrpc_cb_devices_destroy(rpdev); + return err; +} + +static void fastrpc_cap_query_work(struct work_struct *work) +{ + struct fastrpc_channel_ctx *cctx = + container_of(work, struct fastrpc_channel_ctx, cap_query_work); + struct rpmsg_device *rpdev = cctx->rpdev; + struct device *rdev = &rpdev->dev; + u32 *dsp_attributes; + struct fastrpc_user *fl; + unsigned long flags; + int err; + + fl = kzalloc_obj(*fl); + if (!fl) + goto register_devices; + + spin_lock_init(&fl->lock); + mutex_init(&fl->mutex); + INIT_LIST_HEAD(&fl->pending); + INIT_LIST_HEAD(&fl->maps); + INIT_LIST_HEAD(&fl->mmaps); + INIT_LIST_HEAD(&fl->user); + fl->cctx = cctx; + fl->pd = ROOT_PD; + kref_init(&fl->refcount); + fastrpc_channel_ctx_get(cctx); + + fl->sctx = fastrpc_session_alloc(fl); + if (!fl->sctx) { + dev_warn(rdev, "no session for capability query, skipping\n"); + fastrpc_user_put(fl); + goto register_devices; + } + + dsp_attributes = kzalloc_objs(*dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES); + if (!dsp_attributes) + goto err_cleanup_register; + + /* Linked into cctx->users so fastrpc_rpmsg_remove() can abort us + * via fastrpc_notify_users() instead of waiting out the timeout. + */ + spin_lock_irqsave(&cctx->lock, flags); + if (!cctx->rpdev) { + spin_unlock_irqrestore(&cctx->lock, flags); + goto err_cleanup_noreg; + } + list_add_tail(&fl->user, &cctx->users); + spin_unlock_irqrestore(&cctx->lock, flags); + + err = fastrpc_get_info_from_dsp(fl, dsp_attributes, FASTRPC_MAX_DSP_ATTRIBUTES); + + spin_lock_irqsave(&cctx->lock, flags); + list_del(&fl->user); + spin_unlock_irqrestore(&cctx->lock, flags); + + if (err == -EPIPE) { + /* Channel is going away; remove() will tear it down. */ + goto err_cleanup_noreg; + } + + if (!err) { + spin_lock_irqsave(&cctx->lock, flags); + memcpy(cctx->dsp_attributes, dsp_attributes, + FASTRPC_MAX_DSP_ATTRIBUTES_LEN); + cctx->valid_attributes = true; + spin_unlock_irqrestore(&cctx->lock, flags); + dev_dbg(rdev, "DSP capabilities cached at probe time\n"); + } else if (err == DSP_UNSUPPORTED_API) { + dev_info(rdev, "DSP does not support capability query\n"); + } else { + dev_warn(rdev, + "probe-time capability query failed (%d), will retry on demand\n", + err); + } + +err_cleanup_register: + kfree(dsp_attributes); + fastrpc_session_free(cctx, fl->sctx); + fl->sctx = NULL; + fastrpc_user_put(fl); + goto register_devices; + +err_cleanup_noreg: + kfree(dsp_attributes); + fastrpc_session_free(cctx, fl->sctx); + fl->sctx = NULL; + fastrpc_user_put(fl); + return; + +register_devices: + if (fastrpc_register_devices(cctx, rpdev)) + dev_err(rdev, "failed to register device nodes\n"); +} + static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) { struct device *rdev = &rpdev->dev; @@ -2761,34 +2918,6 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) data->poll_mode_supported = soc_data->poll_mode_supported || of_machine_get_match(fastrpc_poll_supported_machines); - switch (domain_id) { - case ADSP_DOMAIN_ID: - case MDSP_DOMAIN_ID: - case SDSP_DOMAIN_ID: - case HPASS_DOMAIN_ID: - /* Unsigned PD offloading is only supported on CDSP and GDSP */ - data->unsigned_support = false; - err = fastrpc_device_register(rdev, data, secure_dsp, domain); - if (err) - goto err_unmap_heap; - break; - case CDSP_DOMAIN_ID: - case GDSP_DOMAIN_ID: - data->unsigned_support = true; - /* Create both device nodes so that we can allow both Signed and Unsigned PD */ - err = fastrpc_device_register(rdev, data, true, domain); - if (err) - goto err_unmap_heap; - - err = fastrpc_device_register(rdev, data, false, domain); - if (err) - goto err_deregister_fdev; - break; - default: - err = -EINVAL; - goto err_unmap_heap; - } - kref_init(&data->refcount); atomic_set(&data->ctx_seq, 0); @@ -2804,31 +2933,27 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) err = fastrpc_cb_devices_create(rpdev); if (err) - goto err_deregister_fdev; - - if (data->domain_id == ADSP_DOMAIN_ID && data->sesscount > 0) { - struct fastrpc_session_ctx *last_sess; - struct fastrpc_session_ctx *dup_sess; - unsigned long flags; + goto err_unmap_heap; - spin_lock_irqsave(&data->lock, flags); - last_sess = &data->session[data->sesscount - 1]; - for (i = 0; i < FASTRPC_DUP_SESSIONS; i++) { - if (data->sesscount >= FASTRPC_MAX_SESSIONS) - break; - dup_sess = &data->session[data->sesscount++]; - memcpy(dup_sess, last_sess, sizeof(*dup_sess)); - } - spin_unlock_irqrestore(&data->lock, flags); + /* Probe returns immediately; the capability query and device node + * registration happen asynchronously on this channel's own queue. + */ + data->cap_wq = alloc_ordered_workqueue("fastrpc_cap_wq_%s", + WQ_HIGHPRI | WQ_MEM_RECLAIM, + domain); + if (!data->cap_wq) { + err = -ENOMEM; + goto err_destroy_cb_devices; } + strscpy(data->domain, domain, sizeof(data->domain)); + INIT_WORK(&data->cap_query_work, fastrpc_cap_query_work); + queue_work(data->cap_wq, &data->cap_query_work); + return 0; -err_deregister_fdev: - if (data->fdevice) - misc_deregister(&data->fdevice->miscdev); - if (data->secure_fdevice) - misc_deregister(&data->secure_fdevice->miscdev); +err_destroy_cb_devices: + fastrpc_cb_devices_destroy(rpdev); err_unmap_heap: fastrpc_remote_heap_release(data, rpdev); @@ -2864,6 +2989,10 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev) fastrpc_notify_users(user); spin_unlock_irqrestore(&cctx->lock, flags); + /* Wait for a pending capability query before tearing down cctx. */ + cancel_work_sync(&cctx->cap_query_work); + destroy_workqueue(cctx->cap_wq); + if (cctx->fdevice) misc_deregister(&cctx->fdevice->miscdev); -- 2.34.1
