Re: [PATCH v8 5/5] remoteproc: Add initial zynqmp R5 remoteproc driver
From: Michael Auchter
Date: Tue Aug 25 2020 - 17:39:55 EST
Hello Ben,
On Mon, Aug 10, 2020 at 08:32:13PM -0700, Ben Levinsky wrote:
> +/**
> + * zynqmp_r5_release() - ZynqMP R5 device release function
> + * @dev: pointer to the device struct of ZynqMP R5
> + *
> + * Function to release ZynqMP R5 device.
> + */
> +static void zynqmp_r5_release(struct device *dev)
> +{
> + struct zynqmp_r5_pdata *pdata;
> + struct rproc *rproc;
> + struct sk_buff *skb;
> +
> + pdata = dev_get_drvdata(dev);
> + rproc = pdata->rproc;
> + if (rproc) {
> + rproc_del(rproc);
> + rproc_free(rproc);
> + }
> + if (pdata->tx_chan)
> + mbox_free_channel(pdata->tx_chan);
> + if (pdata->rx_chan)
> + mbox_free_channel(pdata->rx_chan);
> + /* Discard all SKBs */
> + while (!skb_queue_empty(&pdata->tx_mc_skbs)) {
> + skb = skb_dequeue(&pdata->tx_mc_skbs);
> + kfree_skb(skb);
In the event that zynqmp_r5_probe() fails before zynqmp_r5_setup_mbox()
has run, this will be called on an uninitialized skb_queue. (Also
obviously an issue once mailboxes are made optional again).
> + }
> +
> + put_device(dev->parent);
> +}
> +
> +/**
> + * event_notified_idr_cb() - event notified idr callback
> + * @id: idr id
> + * @ptr: pointer to idr private data
> + * @data: data passed to idr_for_each callback
> + *
> + * Pass notification to remoteproc virtio
> + *
> + * Return: 0. having return is to satisfy the idr_for_each() function
> + * pointer input argument requirement.
> + **/
> +static int event_notified_idr_cb(int id, void *ptr, void *data)
> +{
> + struct rproc *rproc = data;
> +
> + (void)rproc_vq_interrupt(rproc, id);
> + return 0;
> +}
> +
> +/**
> + * handle_event_notified() - remoteproc notification work funciton
> + * @work: pointer to the work structure
> + *
> + * It checks each registered remoteproc notify IDs.
> + */
> +static void handle_event_notified(struct work_struct *work)
> +{
> + struct rproc *rproc;
> + struct zynqmp_r5_pdata *pdata;
> +
> + pdata = container_of(work, struct zynqmp_r5_pdata, mbox_work);
> +
> + (void)mbox_send_message(pdata->rx_chan, NULL);
> + rproc = pdata->rproc;
> + /*
> + * We only use IPI for interrupt. The firmware side may or may
> + * not write the notifyid when it trigger IPI.
> + * And thus, we scan through all the registered notifyids.
> + */
> + idr_for_each(&rproc->notifyids, event_notified_idr_cb, rproc);
> +}
> +
> +/**
> + * zynqmp_r5_mb_rx_cb() - Receive channel mailbox callback
> + * @cl: mailbox client
> + * @mssg: message pointer
> + *
> + * It will schedule the R5 notification work.
> + */
> +static void zynqmp_r5_mb_rx_cb(struct mbox_client *cl, void *mssg)
> +{
> + struct zynqmp_r5_pdata *pdata;
> +
> + pdata = container_of(cl, struct zynqmp_r5_pdata, rx_mc);
> + if (mssg) {
> + struct zynqmp_ipi_message *ipi_msg, *buf_msg;
> + size_t len;
> +
> + ipi_msg = (struct zynqmp_ipi_message *)mssg;
> + buf_msg = (struct zynqmp_ipi_message *)pdata->rx_mc_buf;
> + len = (ipi_msg->len >= IPI_BUF_LEN_MAX) ?
> + IPI_BUF_LEN_MAX : ipi_msg->len;
> + buf_msg->len = len;
> + memcpy(buf_msg->data, ipi_msg->data, len);
> + }
> + schedule_work(&pdata->mbox_work);
> +}
> +
> +/**
> + * zynqmp_r5_mb_tx_done() - Request has been sent to the remote
> + * @cl: mailbox client
> + * @mssg: pointer to the message which has been sent
> + * @r: status of last TX - OK or error
> + *
> + * It will be called by the mailbox framework when the last TX has done.
> + */
> +static void zynqmp_r5_mb_tx_done(struct mbox_client *cl, void *mssg, int r)
> +{
> + struct zynqmp_r5_pdata *pdata;
> + struct sk_buff *skb;
> +
> + if (!mssg)
> + return;
> + pdata = container_of(cl, struct zynqmp_r5_pdata, tx_mc);
> + skb = skb_dequeue(&pdata->tx_mc_skbs);
> + kfree_skb(skb);
> +}
> +
> +/**
> + * zynqmp_r5_setup_mbox() - Setup mailboxes
> + *
> + * @pdata: pointer to the ZynqMP R5 processor platform data
> + * @node: pointer of the device node
> + *
> + * Function to setup mailboxes to talk to RPU.
> + *
> + * Return: 0 for success, negative value for failure.
> + */
> +static int zynqmp_r5_setup_mbox(struct zynqmp_r5_pdata *pdata,
> + struct device_node *node)
> +{
> + struct device *dev = &pdata->dev;
> + struct mbox_client *mclient;
> +
> + /* Setup TX mailbox channel client */
> + mclient = &pdata->tx_mc;
> + mclient->dev = dev;
> + mclient->rx_callback = NULL;
> + mclient->tx_block = false;
> + mclient->knows_txdone = false;
> + mclient->tx_done = zynqmp_r5_mb_tx_done;
> +
> + /* Setup TX mailbox channel client */
> + mclient = &pdata->rx_mc;
> + mclient->dev = dev;
> + mclient->rx_callback = zynqmp_r5_mb_rx_cb;
> + mclient->tx_block = false;
> + mclient->knows_txdone = false;
> +
> + INIT_WORK(&pdata->mbox_work, handle_event_notified);
> +
> + /* Request TX and RX channels */
> + pdata->tx_chan = mbox_request_channel_byname(&pdata->tx_mc, "tx");
> + if (IS_ERR(pdata->tx_chan)) {
> + dev_err(dev, "failed to request mbox tx channel.\n");
> + pdata->tx_chan = NULL;
> + return -EINVAL;
> + }
> + pdata->rx_chan = mbox_request_channel_byname(&pdata->rx_mc, "rx");
> + if (IS_ERR(pdata->rx_chan)) {
> + dev_err(dev, "failed to request mbox rx channel.\n");
> + pdata->rx_chan = NULL;
> + return -EINVAL;
> + }
> + skb_queue_head_init(&pdata->tx_mc_skbs);
> + return 0;
> +}
> +
> +/**
> + * zynqmp_r5_probe() - Probes ZynqMP R5 processor device node
> + * @pdata: pointer to the ZynqMP R5 processor platform data
> + * @pdev: parent RPU domain platform device
> + * @node: pointer of the device node
> + *
> + * Function to retrieve the information of the ZynqMP R5 device node.
> + *
> + * Return: 0 for success, negative value for failure.
> + */
> +static int zynqmp_r5_probe(struct zynqmp_r5_pdata *pdata,
> + struct platform_device *pdev,
> + struct device_node *node)
> +{
> + struct device *dev = &pdata->dev;
> + struct rproc *rproc;
> + struct device_node *nc;
> + int ret;
> +
> + /* Create device for ZynqMP R5 device */
> + dev->parent = &pdev->dev;
> + dev->release = zynqmp_r5_release;
> + dev->of_node = node;
> + dev_set_name(dev, "%s", of_node_full_name(node));
> + dev_set_drvdata(dev, pdata);
> + ret = device_register(dev);
> + if (ret) {
> + dev_err(dev, "failed to register device.\n");
> + return ret;
> + }
> + get_device(&pdev->dev);
> +
> + /* Allocate remoteproc instance */
> + rproc = rproc_alloc(dev, dev_name(dev), &zynqmp_r5_rproc_ops, NULL, 0);
> + if (!rproc) {
> + dev_err(dev, "rproc allocation failed.\n");
> + ret = -ENOMEM;
> + goto error;
> + }
> + rproc->auto_boot = autoboot;
> + pdata->rproc = rproc;
> + rproc->priv = pdata;
> +
> + /*
> + * The device has not been spawned from a device tree, so
> + * arch_setup_dma_ops has not been called, thus leaving
> + * the device with dummy DMA ops.
> + * Fix this by inheriting the parent's DMA ops and mask.
> + */
> + rproc->dev.dma_mask = pdev->dev.dma_mask;
> + set_dma_ops(&rproc->dev, get_dma_ops(&pdev->dev));
> +
> + /* Probe R5 memory devices */
> + INIT_LIST_HEAD(&pdata->mems);
> + for_each_available_child_of_node(node, nc) {
> + ret = zynqmp_r5_mem_probe(pdata, nc);
> + if (ret) {
> + dev_err(dev, "failed to probe memory %s.\n",
> + of_node_full_name(nc));
> + goto error;
> + }
> + }
> +
> + /* Set up DMA mask */
> + ret = dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
> + if (ret) {
> + dev_warn(dev, "dma_set_coherent_mask failed: %d\n", ret);
> + /* If DMA is not configured yet, try to configure it. */
> + ret = of_dma_configure(dev, node, true);
> + if (ret) {
> + dev_err(dev, "failed to configure DMA.\n");
> + goto error;
> + }
> + }
> +
> + /* Get R5 power domain node */
> + ret = of_property_read_u32(node, "pnode-id", &pdata->pnode_id);
> + if (ret) {
> + dev_err(dev, "failed to get power node id.\n");
> + goto error;
> + }
> +
> + /* TODO Check if R5 is running */
> +
> + /* Set up R5 if not already setup */
> + ret = pdata->is_r5_mode_set ? 0 : r5_set_mode(pdata);
> + if (ret) {
> + dev_err(dev, "failed to set R5 operation mode.\n");
> + return ret;
> + }
> +
> + if (!of_get_property(dev->of_node, "mboxes", NULL)) {
> + dev_dbg(dev, "no mailboxes.\n");
> + goto error;
> + } else {
> + ret = zynqmp_r5_setup_mbox(pdata, node);
> + if (ret < 0)
> + goto error;
> + }
> +
> + /* Add R5 remoteproc */
> + ret = rproc_add(rproc);
> + if (ret) {
> + dev_err(dev, "rproc registration failed\n");
> + goto error;
> + }
> + return 0;
> +error:
> + if (pdata->rproc)
> + rproc_free(pdata->rproc);
> + pdata->rproc = NULL;
> + device_unregister(dev);
> + put_device(&pdev->dev);
> + return ret;
> +}
> +
> +static int zynqmp_r5_remoteproc_probe(struct platform_device *pdev)
> +{
> + int ret, i = 0;
> + u32 *lockstep_mode;
> + struct device *dev = &pdev->dev;
> + struct device_node *nc;
> + struct zynqmp_r5_pdata *pdata;
> +
> + pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
> + lockstep_mode = devm_kzalloc(dev, sizeof(u32 *), GFP_KERNEL);
> + if (!pdata || !lockstep_mode)
> + return -ENOMEM;
> +
> + platform_set_drvdata(pdev, pdata);
> +
> + of_property_read_u32(dev->of_node, "lockstep-mode", lockstep_mode);
> +
> + if (!(*lockstep_mode)) {
> + rpu_mode = PM_RPU_MODE_SPLIT;
> + } else if (*lockstep_mode == 1) {
> + rpu_mode = PM_RPU_MODE_LOCKSTEP;
> + } else {
> + dev_err(dev,
> + "Invalid lockstep-mode mode provided - %x %d\n",
> + *lockstep_mode, rpu_mode);
> + return -EINVAL;
> + }
> + dev_dbg(dev, "RPU configuration: %s\r\n",
> + (*lockstep_mode) ? "lockstep" : "split");
> +
> + for_each_available_child_of_node(dev->of_node, nc) {
> + ret = zynqmp_r5_probe(&rpus[i], pdev, nc);
> + if (ret) {
> + dev_err(dev, "failed to probe rpu %s.\n",
> + of_node_full_name(nc));
> + return ret;
> + }
> + i++;
> + }
> +
> + return 0;
> +}
> +
> +static int zynqmp_r5_remoteproc_remove(struct platform_device *pdev)
> +{
> + int i;
> +
> + for (i = 0; i < MAX_RPROCS; i++) {
> + struct zynqmp_r5_pdata *pdata = &rpus[i];
> + struct rproc *rproc;
> +
> + rproc = pdata->rproc;
> + if (rproc) {
> + rproc_del(rproc);
> + rproc_free(rproc);
> + pdata->rproc = NULL;
> + }
> + if (pdata->tx_chan) {
> + mbox_free_channel(pdata->tx_chan);
> + pdata->tx_chan = NULL;
> + }
> + if (pdata->rx_chan) {
> + mbox_free_channel(pdata->rx_chan);
> + pdata->rx_chan = NULL;
> + }
> +
> + device_unregister(&pdata->dev);
> + }
> +
> + return 0;
> +}
> +
> +/* Match table for OF platform binding */
> +static const struct of_device_id zynqmp_r5_remoteproc_match[] = {
> + { .compatible = "xlnx,zynqmp-r5-remoteproc-1.0", },
> + { /* end of list */ },
> +};
> +MODULE_DEVICE_TABLE(of, zynqmp_r5_remoteproc_match);
> +
> +static struct platform_driver zynqmp_r5_remoteproc_driver = {
> + .probe = zynqmp_r5_remoteproc_probe,
> + .remove = zynqmp_r5_remoteproc_remove,
> + .driver = {
> + .name = "zynqmp_r5_remoteproc",
> + .of_match_table = zynqmp_r5_remoteproc_match,
> + },
> +};
> +module_platform_driver(zynqmp_r5_remoteproc_driver);
> +
> +module_param_named(autoboot, autoboot, bool, 0444);
> +MODULE_PARM_DESC(autoboot,
> + "enable | disable autoboot. (default: false)");
> +
> +MODULE_AUTHOR("Ben Levinsky <ben.levinsky@xxxxxxxxxx>");
> +MODULE_LICENSE("GPL v2");
> --
> 2.17.1
>
Thanks,
Michael