diff --git a/drivers/media/platform/apple/ave/apple-ave-dma.c b/drivers/media/platform/apple/ave/apple-ave-dma.c index 8483045031c39..ac5e67762edc2 100644 --- a/drivers/media/platform/apple/ave/apple-ave-dma.c +++ b/drivers/media/platform/apple/ave/apple-ave-dma.c @@ -5,7 +5,11 @@ #include #include #include +#include #include +#include +#include +#include #define AVE_DMA_ERROR_STATUS 0x40 #define AVE_DMA_ERROR_PENDING BIT(31) @@ -21,37 +25,99 @@ struct apple_ave_dma { struct device *dev; void __iomem *base; + struct notifier_block pd_nb; + /* + * Held by the handler from its check through its last register access + * and by the domain's PRE_OFF notification, so no read can outlive the + * domain, threaded handlers (threadirqs) included. Raw: the notification + * comes under the irq-safe PMGR domain's lock. + */ + raw_spinlock_t lock; + /* ps_venc_sys is on: the apertures answer */ + bool powered; + /* free_irq() has started; CONFIG_DEBUG_SHIRQ calls the handler there */ + bool stopping; }; +/* + * The device itself stays runtime-suspended even while the encoder runs, so + * the domain's own on/off notifications tell the handler when it may read. + */ +static int apple_ave_dma_pd_notify(struct notifier_block *nb, + unsigned long action, void *data) +{ + struct apple_ave_dma *dma = container_of(nb, struct apple_ave_dma, pd_nb); + unsigned long flags; + + if (action != GENPD_NOTIFY_ON && action != GENPD_NOTIFY_PRE_OFF) + return NOTIFY_OK; + + /* A handler already reading finishes before the domain goes off */ + raw_spin_lock_irqsave(&dma->lock, flags); + dma->powered = action == GENPD_NOTIFY_ON; + raw_spin_unlock_irqrestore(&dma->lock, flags); + return NOTIFY_OK; +} + +static void apple_ave_dma_remove_notifier(void *data) +{ + dev_pm_genpd_remove_notifier(data); +} + +static void apple_ave_dma_stop(void *data) +{ + struct apple_ave_dma *dma = data; + unsigned long flags; + + raw_spin_lock_irqsave(&dma->lock, flags); + dma->stopping = true; + raw_spin_unlock_irqrestore(&dma->lock, flags); +} + static irqreturn_t apple_ave_dma_irq(int irq, void *data) { struct apple_ave_dma *dma = data; - u32 status = readl(dma->base + AVE_DMA_ERROR_STATUS); + unsigned long flags; + u32 status, info[7]; - if (!(status & AVE_DMA_ERROR_PENDING)) + raw_spin_lock_irqsave(&dma->lock, flags); + /* A sharer's interrupt, a spurious one or teardown: no MMIO while off */ + if (dma->stopping || !dma->powered) { + raw_spin_unlock_irqrestore(&dma->lock, flags); return IRQ_NONE; + } - dev_err_ratelimited(dma->dev, - "DMA fault: status %08x info %08x %08x %08x address %08x info %08x %08x %08x\n", - status, readl(dma->base + AVE_DMA_ERROR_INFO0), - readl(dma->base + AVE_DMA_ERROR_INFO1), - readl(dma->base + AVE_DMA_ERROR_INFO2), - readl(dma->base + AVE_DMA_ERROR_ADDRESS), - readl(dma->base + AVE_DMA_ERROR_INFO3), - readl(dma->base + AVE_DMA_ERROR_INFO4), - readl(dma->base + AVE_DMA_ERROR_INFO5)); + status = readl(dma->base + AVE_DMA_ERROR_STATUS); + if (!(status & AVE_DMA_ERROR_PENDING)) { + raw_spin_unlock_irqrestore(&dma->lock, flags); + return IRQ_NONE; + } + + info[0] = readl(dma->base + AVE_DMA_ERROR_INFO0); + info[1] = readl(dma->base + AVE_DMA_ERROR_INFO1); + info[2] = readl(dma->base + AVE_DMA_ERROR_INFO2); + info[3] = readl(dma->base + AVE_DMA_ERROR_ADDRESS); + info[4] = readl(dma->base + AVE_DMA_ERROR_INFO3); + info[5] = readl(dma->base + AVE_DMA_ERROR_INFO4); + info[6] = readl(dma->base + AVE_DMA_ERROR_INFO5); /* The implemented error bits are write-one-to-clear. */ writel(status & AVE_DMA_ERROR_ACK_MASK, dma->base + AVE_DMA_ERROR_STATUS); readl(dma->base + AVE_DMA_ERROR_STATUS); + raw_spin_unlock_irqrestore(&dma->lock, flags); + + dev_err_ratelimited(dma->dev, + "DMA fault: status %08x info %08x %08x %08x address %08x info %08x %08x %08x\n", + status, info[0], info[1], info[2], info[3], info[4], + info[5], info[6]); return IRQ_HANDLED; } static int apple_ave_dma_probe(struct platform_device *pdev) { struct apple_ave_dma *dma; - int irq; + int irq, ret; dma = devm_kzalloc(&pdev->dev, sizeof(*dma), GFP_KERNEL); if (!dma) @@ -66,12 +132,44 @@ static int apple_ave_dma_probe(struct platform_device *pdev) if (irq < 0) return irq; - return devm_request_irq(&pdev->dev, irq, apple_ave_dma_irq, - IRQF_SHARED, dev_name(&pdev->dev), dma); + /* + * The domain is on here: genpd powered it for the probe, and with + * runtime PM still disabled it counts this device as active. Watch + * it from now on, before runtime PM can let it go. + */ + raw_spin_lock_init(&dma->lock); + dma->powered = true; + dma->pd_nb.notifier_call = apple_ave_dma_pd_notify; + ret = dev_pm_genpd_add_notifier(&pdev->dev, &dma->pd_nb); + if (ret) + return dev_err_probe(&pdev->dev, ret, + "can't watch the power domain\n"); + ret = devm_add_action_or_reset(&pdev->dev, + apple_ave_dma_remove_notifier, &pdev->dev); + if (ret) + return ret; + + /* + * The apertures only report faults; nothing here needs the block + * powered. Stay runtime-suspended so the encoder and its DARTs alone + * decide when ps_venc_sys is on: the firmware needs the domain + * power-cycled between sessions, and deep sleep needs it off. + */ + ret = devm_pm_runtime_enable(&pdev->dev); + if (ret) + return ret; + + ret = devm_request_irq(&pdev->dev, irq, apple_ave_dma_irq, + IRQF_SHARED, dev_name(&pdev->dev), dma); + if (ret) + return ret; + + /* Released first, so the handler stops reading before free_irq() */ + return devm_add_action_or_reset(&pdev->dev, apple_ave_dma_stop, dma); } static const struct of_device_id apple_ave_dma_of_match[] = { - { .compatible = "apple,t6000-ave-dma" }, + { .compatible = "apple,ave-dma" }, {} }; MODULE_DEVICE_TABLE(of, apple_ave_dma_of_match);