123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599 |
- // SPDX-License-Identifier: GPL-2.0-only
- /*
- * Copyright (c) 2018-2020, The Linux Foundation. All rights reserved.
- */
- #include <linux/module.h>
- #include <linux/rpmsg.h>
- #include <linux/of_platform.h>
- #include <linux/of_fdt.h>
- #include <soc/qcom/secure_buffer.h>
- #include "msm_cvp_dsp.h"
- #include "msm_cvp_internal.h"
- struct cvp_dsp_apps gfa_cv;
- static int hlosVM[HLOS_VM_NUM] = {VMID_HLOS};
- static int dspVM[DSP_VM_NUM] = {VMID_HLOS, VMID_CDSP_Q6};
- static int dspVMperm[DSP_VM_NUM] = { PERM_READ | PERM_WRITE | PERM_EXEC,
- PERM_READ | PERM_WRITE | PERM_EXEC };
- static int hlosVMperm[HLOS_VM_NUM] = { PERM_READ | PERM_WRITE | PERM_EXEC };
- static int cvp_dsp_send_cmd(struct cvp_dsp_cmd_msg *cmd, uint32_t len)
- {
- int rc = 0;
- struct cvp_dsp_apps *me = &gfa_cv;
- dprintk(CVP_DSP, "%s: cmd = %d\n", __func__, cmd->type);
- if (IS_ERR_OR_NULL(me->chan)) {
- dprintk(CVP_ERR, "%s: DSP GLink is not ready\n", __func__);
- rc = -EINVAL;
- goto exit;
- }
- rc = rpmsg_send(me->chan->ept, cmd, len);
- if (rc) {
- dprintk(CVP_ERR, "%s: DSP rpmsg_send failed rc=%d\n",
- __func__, rc);
- goto exit;
- }
- exit:
- return rc;
- }
- static int cvp_dsp_send_cmd_sync(struct cvp_dsp_cmd_msg *cmd, uint32_t len)
- {
- int rc = 0;
- struct cvp_dsp_apps *me = &gfa_cv;
- dprintk(CVP_DSP, "%s: cmd = %d\n", __func__, cmd->type);
- me->pending_dsp2cpu_rsp.type = cmd->type;
- rc = cvp_dsp_send_cmd(cmd, len);
- if (rc) {
- dprintk(CVP_ERR, "%s: cvp_dsp_send_cmd failed rc=%d\n",
- __func__, rc);
- goto exit;
- }
- if (!wait_for_completion_timeout(&me->completions[cmd->type],
- msecs_to_jiffies(CVP_DSP_RESPONSE_TIMEOUT))) {
- dprintk(CVP_ERR, "%s cmd %d timeout\n", __func__, cmd->type);
- rc = -ETIMEDOUT;
- goto exit;
- }
- exit:
- me->pending_dsp2cpu_rsp.type = CVP_INVALID_RPMSG_TYPE;
- return rc;
- }
- static int cvp_dsp_send_cmd_hfi_queue(phys_addr_t *phys_addr,
- uint32_t size_in_bytes)
- {
- int rc = 0;
- struct cvp_dsp_cmd_msg cmd;
- cmd.type = CPU2DSP_SEND_HFI_QUEUE;
- cmd.msg_ptr = (uint64_t)phys_addr;
- cmd.msg_ptr_len = size_in_bytes;
- cmd.ddr_type = of_fdt_get_ddrtype();
- if (cmd.ddr_type < 0) {
- dprintk(CVP_ERR,
- "%s: Incorrect DDR type value %d\n",
- __func__, cmd.ddr_type);
- return -EINVAL;
- }
- dprintk(CVP_DSP,
- "%s: address of buffer, PA=0x%pK size_buff=%d ddr_type=%d\n",
- __func__, phys_addr, size_in_bytes, cmd.ddr_type);
- rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg));
- if (rc) {
- dprintk(CVP_ERR,
- "%s: cvp_dsp_send_cmd failed rc = %d\n",
- __func__, rc);
- goto exit;
- }
- exit:
- return rc;
- }
- static int cvp_hyp_assign_to_dsp(uint64_t addr, uint32_t size)
- {
- int rc = 0;
- struct cvp_dsp_apps *me = &gfa_cv;
- if (!me->hyp_assigned) {
- rc = hyp_assign_phys(addr, size, hlosVM, HLOS_VM_NUM, dspVM,
- dspVMperm, DSP_VM_NUM);
- if (rc) {
- dprintk(CVP_ERR, "%s failed. rc=%d\n", __func__, rc);
- return rc;
- }
- me->addr = addr;
- me->size = size;
- me->hyp_assigned = true;
- }
- return rc;
- }
- static int cvp_hyp_assign_from_dsp(void)
- {
- int rc = 0;
- struct cvp_dsp_apps *me = &gfa_cv;
- if (me->hyp_assigned) {
- rc = hyp_assign_phys(me->addr, me->size, dspVM, DSP_VM_NUM,
- hlosVM, hlosVMperm, HLOS_VM_NUM);
- if (rc) {
- dprintk(CVP_ERR, "%s failed. rc=%d\n", __func__, rc);
- return rc;
- }
- me->addr = 0;
- me->size = 0;
- me->hyp_assigned = false;
- }
- return rc;
- }
- static int cvp_dsp_rpmsg_probe(struct rpmsg_device *rpdev)
- {
- struct cvp_dsp_apps *me = &gfa_cv;
- if (strcmp(rpdev->dev.parent->of_node->name, "cdsp")) {
- dprintk(CVP_ERR,
- "%s: Failed to probe rpmsg device.Node name:%s\n",
- __func__, rpdev->dev.parent->of_node->name);
- return -EINVAL;
- }
- mutex_lock(&me->lock);
- me->chan = rpdev;
- me->state = DSP_PROBED;
- complete(&me->completions[CPU2DSP_MAX_CMD]);
- mutex_unlock(&me->lock);
- return 0;
- }
- static void cvp_dsp_rpmsg_remove(struct rpmsg_device *rpdev)
- {
- struct cvp_dsp_apps *me = &gfa_cv;
- dprintk(CVP_WARN, "%s: CDSP SSR triggered\n", __func__);
- mutex_lock(&me->lock);
- cvp_hyp_assign_from_dsp();
- me->chan = NULL;
- me->state = DSP_UNINIT;
- mutex_unlock(&me->lock);
- /* kernel driver needs clean all dsp sessions */
- }
- static int cvp_dsp_rpmsg_callback(struct rpmsg_device *rpdev,
- void *data, int len, void *priv, u32 addr)
- {
- struct cvp_dsp_rsp_msg *rsp = (struct cvp_dsp_rsp_msg *)data;
- struct cvp_dsp_apps *me = &gfa_cv;
- dprintk(CVP_DSP, "%s: type = 0x%x ret = 0x%x len = 0x%x\n",
- __func__, rsp->type, rsp->ret, len);
- if (rsp->type < CPU2DSP_MAX_CMD && len == sizeof(*rsp)) {
- if (me->pending_dsp2cpu_rsp.type == rsp->type) {
- memcpy(&me->pending_dsp2cpu_rsp, rsp,
- sizeof(struct cvp_dsp_rsp_msg));
- complete(&me->completions[rsp->type]);
- } else {
- dprintk(CVP_ERR, "%s: CPU2DSP resp %d, pending %d\n",
- __func__, rsp->type,
- me->pending_dsp2cpu_rsp.type);
- goto exit;
- }
- } else if (rsp->type < CVP_DSP_MAX_CMD &&
- len == sizeof(struct cvp_dsp2cpu_cmd_msg)) {
- if (me->pending_dsp2cpu_cmd.type != CVP_INVALID_RPMSG_TYPE) {
- dprintk(CVP_ERR, "%s: DSP2CPU cmd:%d pending %d\n",
- __func__, rsp->type,
- me->pending_dsp2cpu_cmd.type);
- goto exit;
- }
- memcpy(&me->pending_dsp2cpu_cmd, rsp,
- sizeof(struct cvp_dsp2cpu_cmd_msg));
- complete(&me->completions[CPU2DSP_MAX_CMD]);
- } else {
- dprintk(CVP_ERR, "%s: Invalid type: %d\n", __func__, rsp->type);
- return 0;
- }
- return 0;
- exit:
- dprintk(CVP_ERR, "concurrent dsp cmd type = %d, rsp type = %d\n",
- me->pending_dsp2cpu_cmd.type,
- me->pending_dsp2cpu_rsp.type);
- return 0;
- }
- int cvp_dsp_suspend(uint32_t session_flag)
- {
- int rc = 0;
- struct cvp_dsp_cmd_msg cmd;
- struct cvp_dsp_apps *me = &gfa_cv;
- cmd.type = CPU2DSP_SUSPEND;
- mutex_lock(&me->lock);
- if (me->state != DSP_READY)
- goto exit;
- /* Use cvp_dsp_send_cmd_sync after dsp driver is ready */
- rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg));
- if (rc) {
- dprintk(CVP_ERR,
- "%s: cvp_dsp_send_cmd failed rc = %d\n",
- __func__, rc);
- goto exit;
- }
- me->state = DSP_SUSPEND;
- exit:
- mutex_unlock(&me->lock);
- return rc;
- }
- int cvp_dsp_resume(uint32_t session_flag)
- {
- int rc = 0;
- struct cvp_dsp_cmd_msg cmd;
- struct cvp_dsp_apps *me = &gfa_cv;
- cmd.type = CPU2DSP_RESUME;
- mutex_lock(&me->lock);
- if (me->state != DSP_SUSPEND)
- goto exit;
- /* Use cvp_dsp_send_cmd_sync after dsp driver is ready */
- rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg));
- if (rc) {
- dprintk(CVP_ERR,
- "%s: cvp_dsp_send_cmd failed rc = %d\n",
- __func__, rc);
- goto exit;
- }
- me->state = DSP_READY;
- exit:
- mutex_unlock(&me->lock);
- return rc;
- }
- int cvp_dsp_shutdown(uint32_t session_flag)
- {
- struct cvp_dsp_apps *me = &gfa_cv;
- int rc = 0;
- struct cvp_dsp_cmd_msg cmd;
- cmd.type = CPU2DSP_SHUTDOWN;
- mutex_lock(&me->lock);
- if (me->state == DSP_INVALID)
- goto exit;
- me->state = DSP_INACTIVE;
- rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg));
- if (rc) {
- dprintk(CVP_ERR,
- "%s: cvp_dsp_send_cmd failed with rc = %d\n",
- __func__, rc);
- cvp_hyp_assign_from_dsp();
- goto exit;
- }
- rc = cvp_hyp_assign_from_dsp();
- exit:
- mutex_unlock(&me->lock);
- return rc;
- }
- int cvp_dsp_register_buffer(uint32_t session_id, uint32_t buff_fd,
- uint32_t buff_fd_size, uint32_t buff_size,
- uint32_t buff_offset, uint32_t buff_index,
- uint32_t buff_fd_iova)
- {
- struct cvp_dsp_cmd_msg cmd;
- int rc;
- struct cvp_dsp_apps *me = &gfa_cv;
- cmd.type = CPU2DSP_REGISTER_BUFFER;
- cmd.session_id = session_id;
- cmd.buff_fd = buff_fd;
- cmd.buff_fd_size = buff_fd_size;
- cmd.buff_size = buff_size;
- cmd.buff_offset = buff_offset;
- cmd.buff_index = buff_index;
- cmd.buff_fd_iova = buff_fd_iova;
- dprintk(CVP_DSP,
- "%s: type=0x%x, buff_fd_iova=0x%x buff_index=0x%x\n",
- __func__, cmd.type, buff_fd_iova,
- cmd.buff_index);
- dprintk(CVP_DSP, "%s: buff_size=0x%x session_id=0x%x\n",
- __func__, cmd.buff_size, cmd.session_id);
- mutex_lock(&me->lock);
- rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg));
- if (rc) {
- dprintk(CVP_ERR, "%s send failed rc = %d\n", __func__, rc);
- goto exit;
- }
- exit:
- mutex_unlock(&me->lock);
- return rc;
- }
- int cvp_dsp_deregister_buffer(uint32_t session_id, uint32_t buff_fd,
- uint32_t buff_fd_size, uint32_t buff_size,
- uint32_t buff_offset, uint32_t buff_index,
- uint32_t buff_fd_iova)
- {
- struct cvp_dsp_cmd_msg cmd;
- int rc;
- struct cvp_dsp_apps *me = &gfa_cv;
- cmd.type = CPU2DSP_DEREGISTER_BUFFER;
- cmd.session_id = session_id;
- cmd.buff_fd = buff_fd;
- cmd.buff_fd_size = buff_fd_size;
- cmd.buff_size = buff_size;
- cmd.buff_offset = buff_offset;
- cmd.buff_index = buff_index;
- cmd.buff_fd_iova = buff_fd_iova;
- dprintk(CVP_DSP,
- "%s: type=0x%x, buff_fd_iova=0x%x buff_index=0x%x\n",
- __func__, cmd.type, buff_fd_iova,
- cmd.buff_index);
- dprintk(CVP_DSP, "%s: buff_size=0x%x session_id=0x%x\n",
- __func__, cmd.buff_size, cmd.session_id);
- mutex_lock(&me->lock);
- rc = cvp_dsp_send_cmd_sync(&cmd, sizeof(struct cvp_dsp_cmd_msg));
- if (rc) {
- dprintk(CVP_ERR, "%s send failed rc = %d\n", __func__, rc);
- goto exit;
- }
- exit:
- mutex_unlock(&me->lock);
- return rc;
- }
- static const struct rpmsg_device_id cvp_dsp_rpmsg_match[] = {
- { CVP_APPS_DSP_GLINK_GUID },
- { },
- };
- static struct rpmsg_driver cvp_dsp_rpmsg_client = {
- .id_table = cvp_dsp_rpmsg_match,
- .probe = cvp_dsp_rpmsg_probe,
- .remove = cvp_dsp_rpmsg_remove,
- .callback = cvp_dsp_rpmsg_callback,
- .drv = {
- .name = "qcom,msm_cvp_dsp_rpmsg",
- },
- };
- void cvp_dsp_send_hfi_queue(void)
- {
- struct msm_cvp_core *core;
- struct iris_hfi_device *device;
- struct cvp_dsp_apps *me = &gfa_cv;
- uint64_t addr;
- uint32_t size;
- int rc;
- core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
- if (core && core->device)
- device = core->device->hfi_device_data;
- else
- return;
- if (!device) {
- dprintk(CVP_ERR, "%s: NULL device\n", __func__);
- return;
- }
- dprintk(CVP_DSP, "Entering %s\n", __func__);
- mutex_lock(&device->lock);
- mutex_lock(&me->lock);
- addr = (uint64_t)device->dsp_iface_q_table.mem_data.dma_handle;
- size = device->dsp_iface_q_table.mem_data.size;
- if (!addr || !size) {
- dprintk(CVP_DSP, "%s: HFI queue is not ready\n", __func__);
- goto exit;
- }
- if (me->state != DSP_PROBED && me->state != DSP_INACTIVE)
- goto exit;
- rc = cvp_hyp_assign_to_dsp(addr, size);
- if (rc) {
- dprintk(CVP_ERR, "%s: cvp_hyp_assign_to_dsp. rc=%d\n",
- __func__, rc);
- goto exit;
- }
- rc = cvp_dsp_send_cmd_hfi_queue((phys_addr_t *)addr, size);
- if (rc) {
- dprintk(CVP_WARN, "%s: Send HFI Queue failed rc = %d\n",
- __func__, rc);
- goto exit;
- }
- dprintk(CVP_DSP, "%s: dsp initialized\n", __func__);
- me->state = DSP_READY;
- exit:
- mutex_unlock(&me->lock);
- mutex_unlock(&device->lock);
- }
- static int cvp_dsp_thread(void *data)
- {
- int rc = 0, old_state;
- struct cvp_dsp_apps *me = &gfa_cv;
- struct cvp_dsp_cmd_msg cmd;
- struct cvp_hfi_device *hdev;
- struct msm_cvp_core *core;
- core = list_first_entry(&cvp_driver->cores, struct msm_cvp_core, list);
- if (!core) {
- dprintk(CVP_ERR, "%s: Failed to find core\n", __func__);
- rc = -EINVAL;
- goto exit;
- }
- hdev = (struct cvp_hfi_device *)core->device;
- if (!hdev) {
- dprintk(CVP_ERR, "%s Invalid device handle\n", __func__);
- rc = -EINVAL;
- goto exit;
- }
- wait_dsp:
- rc = wait_for_completion_interruptible(
- &me->completions[CPU2DSP_MAX_CMD]);
- if (me->state == DSP_INVALID)
- goto exit;
- if (me->state == DSP_UNINIT)
- goto wait_dsp;
- if (me->state == DSP_PROBED) {
- cvp_dsp_send_hfi_queue();
- goto wait_dsp;
- }
- cmd.type = me->pending_dsp2cpu_cmd.type;
- if (rc == -ERESTARTSYS) {
- dprintk(CVP_WARN, "%s received interrupt signal\n", __func__);
- } else {
- mutex_lock(&me->lock);
- switch (me->pending_dsp2cpu_cmd.type) {
- case DSP2CPU_POWERON:
- {
- if (me->state == DSP_READY)
- break;
- mutex_unlock(&me->lock);
- old_state = me->state;
- me->state = DSP_READY;
- rc = call_hfi_op(hdev, resume, hdev->hfi_device_data);
- if (rc) {
- dprintk(CVP_WARN, "%s Failed to resume cvp\n",
- __func__);
- mutex_lock(&me->lock);
- me->state = old_state;
- cmd.ret = 1;
- break;
- }
- mutex_lock(&me->lock);
- cmd.ret = 0;
- break;
- }
- case DSP2CPU_POWEROFF:
- {
- me->state = DSP_SUSPEND;
- cmd.ret = 0;
- break;
- }
- default:
- dprintk(CVP_ERR, "unrecognaized dsp cmds: %d\n",
- me->pending_dsp2cpu_cmd.type);
- break;
- }
- me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
- mutex_unlock(&me->lock);
- }
- /* Responds to DSP */
- rc = cvp_dsp_send_cmd(&cmd, sizeof(struct cvp_dsp_cmd_msg));
- if (rc)
- dprintk(CVP_ERR,
- "%s: cvp_dsp_send_cmd failed rc = %d cmd type=%d\n",
- __func__, rc, cmd.type);
- goto wait_dsp;
- exit:
- dprintk(CVP_DBG, "dsp thread exit\n");
- do_exit(rc);
- }
- int cvp_dsp_device_init(void)
- {
- struct cvp_dsp_apps *me = &gfa_cv;
- char tname[16];
- int rc;
- int i;
- mutex_init(&me->lock);
- me->state = DSP_INVALID;
- me->hyp_assigned = false;
- for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
- init_completion(&me->completions[i]);
- me->pending_dsp2cpu_cmd.type = CVP_INVALID_RPMSG_TYPE;
- me->pending_dsp2cpu_rsp.type = CVP_INVALID_RPMSG_TYPE;
- rc = register_rpmsg_driver(&cvp_dsp_rpmsg_client);
- if (rc) {
- dprintk(CVP_ERR,
- "%s : register_rpmsg_driver failed rc = %d\n",
- __func__, rc);
- goto register_bail;
- }
- snprintf(tname, sizeof(tname), "cvp-dsp-thread");
- me->state = DSP_UNINIT;
- me->dsp_thread = kthread_run(cvp_dsp_thread, me, tname);
- if (!me->dsp_thread) {
- dprintk(CVP_ERR, "%s create %s fail", __func__, tname);
- rc = -ECHILD;
- me->state = DSP_INVALID;
- goto register_bail;
- }
- return 0;
- register_bail:
- return rc;
- }
- void cvp_dsp_device_exit(void)
- {
- struct cvp_dsp_apps *me = &gfa_cv;
- int i;
- mutex_lock(&me->lock);
- me->state = DSP_INVALID;
- mutex_unlock(&me->lock);
- for (i = 0; i <= CPU2DSP_MAX_CMD; i++)
- complete_all(&me->completions[i]);
- mutex_destroy(&me->lock);
- unregister_rpmsg_driver(&cvp_dsp_rpmsg_client);
- }
|