2 * Qualcomm Technologies HIDMA DMA engine interface
4 * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 and
8 * only version 2 as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 * Copyright (C) Freescale Semicondutor, Inc. 2007, 2008.
18 * Copyright (C) Semihalf 2009
19 * Copyright (C) Ilya Yanok, Emcraft Systems 2010
20 * Copyright (C) Alexander Popov, Promcontroller 2014
23 * (defines, structures and comments) was taken from MPC5121 DMA driver
26 * Approved as OSADL project by a majority of OSADL members and funded
27 * by OSADL membership fees in 2009; for details see www.osadl.org.
29 * This program is free software; you can redistribute it and/or modify it
30 * under the terms of the GNU General Public License as published by the Free
31 * Software Foundation; either version 2 of the License, or (at your option)
34 * This program is distributed in the hope that it will be useful, but WITHOUT
35 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
36 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
39 * The full GNU General Public License is included in this distribution in the
40 * file called COPYING.
43 /* Linux Foundation elects GPLv2 license only. */
45 #include <linux/dmaengine.h>
46 #include <linux/dma-mapping.h>
47 #include <linux/list.h>
48 #include <linux/mod_devicetable.h>
49 #include <linux/module.h>
50 #include <linux/platform_device.h>
51 #include <linux/slab.h>
52 #include <linux/spinlock.h>
53 #include <linux/property.h>
54 #include <linux/delay.h>
55 #include <linux/acpi.h>
56 #include <linux/irq.h>
57 #include <linux/atomic.h>
58 #include <linux/pm_runtime.h>
59 #include <linux/msi.h>
61 #include "../dmaengine.h"
65 * Default idle time is 2 seconds. This parameter can
66 * be overridden by changing the following
67 * /sys/bus/platform/devices/QCOM8061:<xy>/power/autosuspend_delay_ms
70 #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
71 #define HIDMA_ERR_INFO_SW 0xFF
72 #define HIDMA_ERR_CODE_UNEXPECTED_TERMINATE 0x0
73 #define HIDMA_NR_DEFAULT_DESC 10
74 #define HIDMA_MSI_INTS 11
76 static inline struct hidma_dev *to_hidma_dev(struct dma_device *dmadev)
78 return container_of(dmadev, struct hidma_dev, ddev);
82 struct hidma_dev *to_hidma_dev_from_lldev(struct hidma_lldev **_lldevp)
84 return container_of(_lldevp, struct hidma_dev, lldev);
87 static inline struct hidma_chan *to_hidma_chan(struct dma_chan *dmach)
89 return container_of(dmach, struct hidma_chan, chan);
92 static void hidma_free(struct hidma_dev *dmadev)
94 INIT_LIST_HEAD(&dmadev->ddev.channels);
97 static unsigned int nr_desc_prm;
98 module_param(nr_desc_prm, uint, 0644);
99 MODULE_PARM_DESC(nr_desc_prm, "number of descriptors (default: 0)");
106 /* process completed descriptors */
107 static void hidma_process_completed(struct hidma_chan *mchan)
109 struct dma_device *ddev = mchan->chan.device;
110 struct hidma_dev *mdma = to_hidma_dev(ddev);
111 struct dma_async_tx_descriptor *desc;
112 dma_cookie_t last_cookie;
113 struct hidma_desc *mdesc;
114 struct hidma_desc *next;
115 unsigned long irqflags;
116 struct list_head list;
118 INIT_LIST_HEAD(&list);
120 /* Get all completed descriptors */
121 spin_lock_irqsave(&mchan->lock, irqflags);
122 list_splice_tail_init(&mchan->completed, &list);
123 spin_unlock_irqrestore(&mchan->lock, irqflags);
125 /* Execute callbacks and run dependencies */
126 list_for_each_entry_safe(mdesc, next, &list, node) {
127 enum dma_status llstat;
128 struct dmaengine_desc_callback cb;
129 struct dmaengine_result result;
132 last_cookie = desc->cookie;
134 llstat = hidma_ll_status(mdma->lldev, mdesc->tre_ch);
136 spin_lock_irqsave(&mchan->lock, irqflags);
137 if (llstat == DMA_COMPLETE) {
138 mchan->last_success = last_cookie;
139 result.result = DMA_TRANS_NOERROR;
141 result.result = DMA_TRANS_ABORTED;
144 dma_cookie_complete(desc);
145 spin_unlock_irqrestore(&mchan->lock, irqflags);
147 dmaengine_desc_get_callback(desc, &cb);
149 dma_run_dependencies(desc);
151 spin_lock_irqsave(&mchan->lock, irqflags);
152 list_move(&mdesc->node, &mchan->free);
153 spin_unlock_irqrestore(&mchan->lock, irqflags);
155 dmaengine_desc_callback_invoke(&cb, &result);
160 * Called once for each submitted descriptor.
161 * PM is locked once for each descriptor that is currently
164 static void hidma_callback(void *data)
166 struct hidma_desc *mdesc = data;
167 struct hidma_chan *mchan = to_hidma_chan(mdesc->desc.chan);
168 struct dma_device *ddev = mchan->chan.device;
169 struct hidma_dev *dmadev = to_hidma_dev(ddev);
170 unsigned long irqflags;
173 spin_lock_irqsave(&mchan->lock, irqflags);
174 if (mdesc->node.next) {
175 /* Delete from the active list, add to completed list */
176 list_move_tail(&mdesc->node, &mchan->completed);
179 /* calculate the next running descriptor */
180 mchan->running = list_first_entry(&mchan->active,
181 struct hidma_desc, node);
183 spin_unlock_irqrestore(&mchan->lock, irqflags);
185 hidma_process_completed(mchan);
188 pm_runtime_mark_last_busy(dmadev->ddev.dev);
189 pm_runtime_put_autosuspend(dmadev->ddev.dev);
193 static int hidma_chan_init(struct hidma_dev *dmadev, u32 dma_sig)
195 struct hidma_chan *mchan;
196 struct dma_device *ddev;
198 mchan = devm_kzalloc(dmadev->ddev.dev, sizeof(*mchan), GFP_KERNEL);
202 ddev = &dmadev->ddev;
203 mchan->dma_sig = dma_sig;
204 mchan->dmadev = dmadev;
205 mchan->chan.device = ddev;
206 dma_cookie_init(&mchan->chan);
208 INIT_LIST_HEAD(&mchan->free);
209 INIT_LIST_HEAD(&mchan->prepared);
210 INIT_LIST_HEAD(&mchan->active);
211 INIT_LIST_HEAD(&mchan->completed);
212 INIT_LIST_HEAD(&mchan->queued);
214 spin_lock_init(&mchan->lock);
215 list_add_tail(&mchan->chan.device_node, &ddev->channels);
219 static void hidma_issue_task(struct tasklet_struct *t)
221 struct hidma_dev *dmadev = from_tasklet(dmadev, t, task);
223 pm_runtime_get_sync(dmadev->ddev.dev);
224 hidma_ll_start(dmadev->lldev);
227 static void hidma_issue_pending(struct dma_chan *dmach)
229 struct hidma_chan *mchan = to_hidma_chan(dmach);
230 struct hidma_dev *dmadev = mchan->dmadev;
232 struct hidma_desc *qdesc, *next;
235 spin_lock_irqsave(&mchan->lock, flags);
236 list_for_each_entry_safe(qdesc, next, &mchan->queued, node) {
237 hidma_ll_queue_request(dmadev->lldev, qdesc->tre_ch);
238 list_move_tail(&qdesc->node, &mchan->active);
241 if (!mchan->running) {
242 struct hidma_desc *desc = list_first_entry(&mchan->active,
245 mchan->running = desc;
247 spin_unlock_irqrestore(&mchan->lock, flags);
249 /* PM will be released in hidma_callback function. */
250 status = pm_runtime_get(dmadev->ddev.dev);
252 tasklet_schedule(&dmadev->task);
254 hidma_ll_start(dmadev->lldev);
257 static inline bool hidma_txn_is_success(dma_cookie_t cookie,
258 dma_cookie_t last_success, dma_cookie_t last_used)
260 if (last_success <= last_used) {
261 if ((cookie <= last_success) || (cookie > last_used))
264 if ((cookie <= last_success) && (cookie > last_used))
270 static enum dma_status hidma_tx_status(struct dma_chan *dmach,
272 struct dma_tx_state *txstate)
274 struct hidma_chan *mchan = to_hidma_chan(dmach);
277 ret = dma_cookie_status(dmach, cookie, txstate);
278 if (ret == DMA_COMPLETE) {
281 is_success = hidma_txn_is_success(cookie, mchan->last_success,
283 return is_success ? ret : DMA_ERROR;
286 if (mchan->paused && (ret == DMA_IN_PROGRESS)) {
288 dma_cookie_t runcookie;
290 spin_lock_irqsave(&mchan->lock, flags);
292 runcookie = mchan->running->desc.cookie;
296 if (runcookie == cookie)
299 spin_unlock_irqrestore(&mchan->lock, flags);
306 * Submit descriptor to hardware.
307 * Lock the PM for each descriptor we are sending.
309 static dma_cookie_t hidma_tx_submit(struct dma_async_tx_descriptor *txd)
311 struct hidma_chan *mchan = to_hidma_chan(txd->chan);
312 struct hidma_dev *dmadev = mchan->dmadev;
313 struct hidma_desc *mdesc;
314 unsigned long irqflags;
317 pm_runtime_get_sync(dmadev->ddev.dev);
318 if (!hidma_ll_isenabled(dmadev->lldev)) {
319 pm_runtime_mark_last_busy(dmadev->ddev.dev);
320 pm_runtime_put_autosuspend(dmadev->ddev.dev);
323 pm_runtime_mark_last_busy(dmadev->ddev.dev);
324 pm_runtime_put_autosuspend(dmadev->ddev.dev);
326 mdesc = container_of(txd, struct hidma_desc, desc);
327 spin_lock_irqsave(&mchan->lock, irqflags);
329 /* Move descriptor to queued */
330 list_move_tail(&mdesc->node, &mchan->queued);
333 cookie = dma_cookie_assign(txd);
335 spin_unlock_irqrestore(&mchan->lock, irqflags);
340 static int hidma_alloc_chan_resources(struct dma_chan *dmach)
342 struct hidma_chan *mchan = to_hidma_chan(dmach);
343 struct hidma_dev *dmadev = mchan->dmadev;
344 struct hidma_desc *mdesc, *tmp;
345 unsigned long irqflags;
350 if (mchan->allocated)
353 /* Alloc descriptors for this channel */
354 for (i = 0; i < dmadev->nr_descriptors; i++) {
355 mdesc = kzalloc(sizeof(struct hidma_desc), GFP_NOWAIT);
360 dma_async_tx_descriptor_init(&mdesc->desc, dmach);
361 mdesc->desc.tx_submit = hidma_tx_submit;
363 rc = hidma_ll_request(dmadev->lldev, mchan->dma_sig,
364 "DMA engine", hidma_callback, mdesc,
367 dev_err(dmach->device->dev,
368 "channel alloc failed at %u\n", i);
372 list_add_tail(&mdesc->node, &descs);
376 /* return the allocated descriptors */
377 list_for_each_entry_safe(mdesc, tmp, &descs, node) {
378 hidma_ll_free(dmadev->lldev, mdesc->tre_ch);
384 spin_lock_irqsave(&mchan->lock, irqflags);
385 list_splice_tail_init(&descs, &mchan->free);
386 mchan->allocated = true;
387 spin_unlock_irqrestore(&mchan->lock, irqflags);
391 static struct dma_async_tx_descriptor *
392 hidma_prep_dma_memcpy(struct dma_chan *dmach, dma_addr_t dest, dma_addr_t src,
393 size_t len, unsigned long flags)
395 struct hidma_chan *mchan = to_hidma_chan(dmach);
396 struct hidma_desc *mdesc = NULL;
397 struct hidma_dev *mdma = mchan->dmadev;
398 unsigned long irqflags;
400 /* Get free descriptor */
401 spin_lock_irqsave(&mchan->lock, irqflags);
402 if (!list_empty(&mchan->free)) {
403 mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
404 list_del(&mdesc->node);
406 spin_unlock_irqrestore(&mchan->lock, irqflags);
411 mdesc->desc.flags = flags;
412 hidma_ll_set_transfer_params(mdma->lldev, mdesc->tre_ch,
413 src, dest, len, flags,
416 /* Place descriptor in prepared list */
417 spin_lock_irqsave(&mchan->lock, irqflags);
418 list_add_tail(&mdesc->node, &mchan->prepared);
419 spin_unlock_irqrestore(&mchan->lock, irqflags);
424 static struct dma_async_tx_descriptor *
425 hidma_prep_dma_memset(struct dma_chan *dmach, dma_addr_t dest, int value,
426 size_t len, unsigned long flags)
428 struct hidma_chan *mchan = to_hidma_chan(dmach);
429 struct hidma_desc *mdesc = NULL;
430 struct hidma_dev *mdma = mchan->dmadev;
431 unsigned long irqflags;
432 u64 byte_pattern, fill_pattern;
434 /* Get free descriptor */
435 spin_lock_irqsave(&mchan->lock, irqflags);
436 if (!list_empty(&mchan->free)) {
437 mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
438 list_del(&mdesc->node);
440 spin_unlock_irqrestore(&mchan->lock, irqflags);
445 byte_pattern = (char)value;
446 fill_pattern = (byte_pattern << 56) |
447 (byte_pattern << 48) |
448 (byte_pattern << 40) |
449 (byte_pattern << 32) |
450 (byte_pattern << 24) |
451 (byte_pattern << 16) |
452 (byte_pattern << 8) |
455 mdesc->desc.flags = flags;
456 hidma_ll_set_transfer_params(mdma->lldev, mdesc->tre_ch,
457 fill_pattern, dest, len, flags,
460 /* Place descriptor in prepared list */
461 spin_lock_irqsave(&mchan->lock, irqflags);
462 list_add_tail(&mdesc->node, &mchan->prepared);
463 spin_unlock_irqrestore(&mchan->lock, irqflags);
468 static int hidma_terminate_channel(struct dma_chan *chan)
470 struct hidma_chan *mchan = to_hidma_chan(chan);
471 struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
472 struct hidma_desc *tmp, *mdesc;
473 unsigned long irqflags;
477 pm_runtime_get_sync(dmadev->ddev.dev);
478 /* give completed requests a chance to finish */
479 hidma_process_completed(mchan);
481 spin_lock_irqsave(&mchan->lock, irqflags);
482 mchan->last_success = 0;
483 list_splice_init(&mchan->active, &list);
484 list_splice_init(&mchan->prepared, &list);
485 list_splice_init(&mchan->completed, &list);
486 list_splice_init(&mchan->queued, &list);
487 spin_unlock_irqrestore(&mchan->lock, irqflags);
489 /* this suspends the existing transfer */
490 rc = hidma_ll_disable(dmadev->lldev);
492 dev_err(dmadev->ddev.dev, "channel did not pause\n");
496 /* return all user requests */
497 list_for_each_entry_safe(mdesc, tmp, &list, node) {
498 struct dma_async_tx_descriptor *txd = &mdesc->desc;
500 dma_descriptor_unmap(txd);
501 dmaengine_desc_get_callback_invoke(txd, NULL);
502 dma_run_dependencies(txd);
504 /* move myself to free_list */
505 list_move(&mdesc->node, &mchan->free);
508 rc = hidma_ll_enable(dmadev->lldev);
510 pm_runtime_mark_last_busy(dmadev->ddev.dev);
511 pm_runtime_put_autosuspend(dmadev->ddev.dev);
515 static int hidma_terminate_all(struct dma_chan *chan)
517 struct hidma_chan *mchan = to_hidma_chan(chan);
518 struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
521 rc = hidma_terminate_channel(chan);
525 /* reinitialize the hardware */
526 pm_runtime_get_sync(dmadev->ddev.dev);
527 rc = hidma_ll_setup(dmadev->lldev);
528 pm_runtime_mark_last_busy(dmadev->ddev.dev);
529 pm_runtime_put_autosuspend(dmadev->ddev.dev);
533 static void hidma_free_chan_resources(struct dma_chan *dmach)
535 struct hidma_chan *mchan = to_hidma_chan(dmach);
536 struct hidma_dev *mdma = mchan->dmadev;
537 struct hidma_desc *mdesc, *tmp;
538 unsigned long irqflags;
541 /* terminate running transactions and free descriptors */
542 hidma_terminate_channel(dmach);
544 spin_lock_irqsave(&mchan->lock, irqflags);
547 list_splice_tail_init(&mchan->free, &descs);
549 /* Free descriptors */
550 list_for_each_entry_safe(mdesc, tmp, &descs, node) {
551 hidma_ll_free(mdma->lldev, mdesc->tre_ch);
552 list_del(&mdesc->node);
556 mchan->allocated = false;
557 spin_unlock_irqrestore(&mchan->lock, irqflags);
560 static int hidma_pause(struct dma_chan *chan)
562 struct hidma_chan *mchan;
563 struct hidma_dev *dmadev;
565 mchan = to_hidma_chan(chan);
566 dmadev = to_hidma_dev(mchan->chan.device);
567 if (!mchan->paused) {
568 pm_runtime_get_sync(dmadev->ddev.dev);
569 if (hidma_ll_disable(dmadev->lldev))
570 dev_warn(dmadev->ddev.dev, "channel did not stop\n");
571 mchan->paused = true;
572 pm_runtime_mark_last_busy(dmadev->ddev.dev);
573 pm_runtime_put_autosuspend(dmadev->ddev.dev);
578 static int hidma_resume(struct dma_chan *chan)
580 struct hidma_chan *mchan;
581 struct hidma_dev *dmadev;
584 mchan = to_hidma_chan(chan);
585 dmadev = to_hidma_dev(mchan->chan.device);
587 pm_runtime_get_sync(dmadev->ddev.dev);
588 rc = hidma_ll_enable(dmadev->lldev);
590 mchan->paused = false;
592 dev_err(dmadev->ddev.dev,
593 "failed to resume the channel");
594 pm_runtime_mark_last_busy(dmadev->ddev.dev);
595 pm_runtime_put_autosuspend(dmadev->ddev.dev);
600 static irqreturn_t hidma_chirq_handler(int chirq, void *arg)
602 struct hidma_lldev *lldev = arg;
605 * All interrupts are request driven.
606 * HW doesn't send an interrupt by itself.
608 return hidma_ll_inthandler(chirq, lldev);
611 #ifdef CONFIG_GENERIC_MSI_IRQ
612 static irqreturn_t hidma_chirq_handler_msi(int chirq, void *arg)
614 struct hidma_lldev **lldevp = arg;
615 struct hidma_dev *dmadev = to_hidma_dev_from_lldev(lldevp);
617 return hidma_ll_inthandler_msi(chirq, *lldevp,
618 1 << (chirq - dmadev->msi_virqbase));
622 static ssize_t hidma_show_values(struct device *dev,
623 struct device_attribute *attr, char *buf)
625 struct hidma_dev *mdev = dev_get_drvdata(dev);
629 if (strcmp(attr->attr.name, "chid") == 0)
630 sprintf(buf, "%d\n", mdev->chidx);
635 static inline void hidma_sysfs_uninit(struct hidma_dev *dev)
637 device_remove_file(dev->ddev.dev, dev->chid_attrs);
640 static struct device_attribute*
641 hidma_create_sysfs_entry(struct hidma_dev *dev, char *name, int mode)
643 struct device_attribute *attrs;
646 attrs = devm_kmalloc(dev->ddev.dev, sizeof(struct device_attribute),
651 name_copy = devm_kstrdup(dev->ddev.dev, name, GFP_KERNEL);
655 attrs->attr.name = name_copy;
656 attrs->attr.mode = mode;
657 attrs->show = hidma_show_values;
658 sysfs_attr_init(&attrs->attr);
663 static int hidma_sysfs_init(struct hidma_dev *dev)
665 dev->chid_attrs = hidma_create_sysfs_entry(dev, "chid", S_IRUGO);
666 if (!dev->chid_attrs)
669 return device_create_file(dev->ddev.dev, dev->chid_attrs);
672 #ifdef CONFIG_GENERIC_MSI_IRQ
673 static void hidma_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
675 struct device *dev = msi_desc_to_dev(desc);
676 struct hidma_dev *dmadev = dev_get_drvdata(dev);
678 if (!desc->msi_index) {
679 writel(msg->address_lo, dmadev->dev_evca + 0x118);
680 writel(msg->address_hi, dmadev->dev_evca + 0x11C);
681 writel(msg->data, dmadev->dev_evca + 0x120);
686 static void hidma_free_msis(struct hidma_dev *dmadev)
688 #ifdef CONFIG_GENERIC_MSI_IRQ
689 struct device *dev = dmadev->ddev.dev;
692 for (i = 0; i < HIDMA_MSI_INTS; i++) {
693 virq = msi_get_virq(dev, i);
695 devm_free_irq(dev, virq, &dmadev->lldev);
698 platform_device_msi_free_irqs_all(dev);
702 static int hidma_request_msi(struct hidma_dev *dmadev,
703 struct platform_device *pdev)
705 #ifdef CONFIG_GENERIC_MSI_IRQ
708 rc = platform_device_msi_init_and_alloc_irqs(&pdev->dev, HIDMA_MSI_INTS,
709 hidma_write_msi_msg);
713 for (i = 0; i < HIDMA_MSI_INTS; i++) {
714 virq = msi_get_virq(&pdev->dev, i);
715 rc = devm_request_irq(&pdev->dev, virq,
716 hidma_chirq_handler_msi,
722 dmadev->msi_virqbase = virq;
726 /* free allocated MSI interrupts above */
727 for (--i; i >= 0; i--) {
728 virq = msi_get_virq(&pdev->dev, i);
729 devm_free_irq(&pdev->dev, virq, &dmadev->lldev);
732 "failed to request MSI irq, falling back to wired IRQ\n");
734 /* Add callback to free MSIs on teardown */
735 hidma_ll_setup_irq(dmadev->lldev, true);
743 static bool hidma_test_capability(struct device *dev, enum hidma_cap test_cap)
747 cap = (uintptr_t) device_get_match_data(dev);
748 return cap ? ((cap & test_cap) > 0) : 0;
751 static int hidma_probe(struct platform_device *pdev)
753 struct hidma_dev *dmadev;
754 struct resource *trca_resource;
755 struct resource *evca_resource;
762 pm_runtime_set_autosuspend_delay(&pdev->dev, HIDMA_AUTOSUSPEND_TIMEOUT);
763 pm_runtime_use_autosuspend(&pdev->dev);
764 pm_runtime_set_active(&pdev->dev);
765 pm_runtime_enable(&pdev->dev);
767 trca = devm_platform_get_and_ioremap_resource(pdev, 0, &trca_resource);
773 evca = devm_platform_get_and_ioremap_resource(pdev, 1, &evca_resource);
780 * This driver only handles the channel IRQs.
781 * Common IRQ is handled by the management driver.
783 chirq = platform_get_irq(pdev, 0);
789 dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
795 INIT_LIST_HEAD(&dmadev->ddev.channels);
796 spin_lock_init(&dmadev->lock);
797 dmadev->ddev.dev = &pdev->dev;
798 pm_runtime_get_sync(dmadev->ddev.dev);
800 dma_cap_set(DMA_MEMCPY, dmadev->ddev.cap_mask);
801 dma_cap_set(DMA_MEMSET, dmadev->ddev.cap_mask);
802 if (WARN_ON(!pdev->dev.dma_mask)) {
807 dmadev->dev_evca = evca;
808 dmadev->evca_resource = evca_resource;
809 dmadev->dev_trca = trca;
810 dmadev->trca_resource = trca_resource;
811 dmadev->ddev.device_prep_dma_memcpy = hidma_prep_dma_memcpy;
812 dmadev->ddev.device_prep_dma_memset = hidma_prep_dma_memset;
813 dmadev->ddev.device_alloc_chan_resources = hidma_alloc_chan_resources;
814 dmadev->ddev.device_free_chan_resources = hidma_free_chan_resources;
815 dmadev->ddev.device_tx_status = hidma_tx_status;
816 dmadev->ddev.device_issue_pending = hidma_issue_pending;
817 dmadev->ddev.device_pause = hidma_pause;
818 dmadev->ddev.device_resume = hidma_resume;
819 dmadev->ddev.device_terminate_all = hidma_terminate_all;
820 dmadev->ddev.copy_align = 8;
823 * Determine the MSI capability of the platform. Old HW doesn't
826 msi = hidma_test_capability(&pdev->dev, HIDMA_MSI_CAP);
827 device_property_read_u32(&pdev->dev, "desc-count",
828 &dmadev->nr_descriptors);
831 dev_info(&pdev->dev, "overriding number of descriptors as %d\n",
833 dmadev->nr_descriptors = nr_desc_prm;
836 if (!dmadev->nr_descriptors)
837 dmadev->nr_descriptors = HIDMA_NR_DEFAULT_DESC;
839 if (hidma_test_capability(&pdev->dev, HIDMA_IDENTITY_CAP))
840 dmadev->chidx = readl(dmadev->dev_trca + 0x40);
842 dmadev->chidx = readl(dmadev->dev_trca + 0x28);
844 /* Set DMA mask to 64 bits. */
845 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
847 dev_warn(&pdev->dev, "unable to set coherent mask to 64");
851 dmadev->lldev = hidma_ll_init(dmadev->ddev.dev,
852 dmadev->nr_descriptors, dmadev->dev_trca,
853 dmadev->dev_evca, dmadev->chidx);
854 if (!dmadev->lldev) {
859 platform_set_drvdata(pdev, dmadev);
861 rc = hidma_request_msi(dmadev, pdev);
864 hidma_ll_setup_irq(dmadev->lldev, false);
865 rc = devm_request_irq(&pdev->dev, chirq, hidma_chirq_handler,
866 0, "qcom-hidma", dmadev->lldev);
871 INIT_LIST_HEAD(&dmadev->ddev.channels);
872 rc = hidma_chan_init(dmadev, 0);
876 rc = dma_async_device_register(&dmadev->ddev);
881 tasklet_setup(&dmadev->task, hidma_issue_task);
882 hidma_debug_init(dmadev);
883 hidma_sysfs_init(dmadev);
884 dev_info(&pdev->dev, "HI-DMA engine driver registration complete\n");
885 pm_runtime_mark_last_busy(dmadev->ddev.dev);
886 pm_runtime_put_autosuspend(dmadev->ddev.dev);
891 hidma_free_msis(dmadev);
893 hidma_ll_uninit(dmadev->lldev);
898 pm_runtime_put_sync(&pdev->dev);
899 pm_runtime_disable(&pdev->dev);
903 static void hidma_shutdown(struct platform_device *pdev)
905 struct hidma_dev *dmadev = platform_get_drvdata(pdev);
907 dev_info(dmadev->ddev.dev, "HI-DMA engine shutdown\n");
909 pm_runtime_get_sync(dmadev->ddev.dev);
910 if (hidma_ll_disable(dmadev->lldev))
911 dev_warn(dmadev->ddev.dev, "channel did not stop\n");
912 pm_runtime_mark_last_busy(dmadev->ddev.dev);
913 pm_runtime_put_autosuspend(dmadev->ddev.dev);
917 static void hidma_remove(struct platform_device *pdev)
919 struct hidma_dev *dmadev = platform_get_drvdata(pdev);
921 pm_runtime_get_sync(dmadev->ddev.dev);
922 dma_async_device_unregister(&dmadev->ddev);
923 if (!dmadev->lldev->msi_support)
924 devm_free_irq(dmadev->ddev.dev, dmadev->irq, dmadev->lldev);
926 hidma_free_msis(dmadev);
928 tasklet_kill(&dmadev->task);
929 hidma_sysfs_uninit(dmadev);
930 hidma_debug_uninit(dmadev);
931 hidma_ll_uninit(dmadev->lldev);
934 dev_info(&pdev->dev, "HI-DMA engine removed\n");
935 pm_runtime_put_sync_suspend(&pdev->dev);
936 pm_runtime_disable(&pdev->dev);
939 #if IS_ENABLED(CONFIG_ACPI)
940 static const struct acpi_device_id hidma_acpi_ids[] = {
942 {"QCOM8062", HIDMA_MSI_CAP},
943 {"QCOM8063", (HIDMA_MSI_CAP | HIDMA_IDENTITY_CAP)},
946 MODULE_DEVICE_TABLE(acpi, hidma_acpi_ids);
949 static struct platform_driver hidma_driver = {
950 .probe = hidma_probe,
951 .remove = hidma_remove,
952 .shutdown = hidma_shutdown,
955 .acpi_match_table = ACPI_PTR(hidma_acpi_ids),
959 module_platform_driver(hidma_driver);
960 MODULE_DESCRIPTION("Qualcomm Technologies HIDMA Channel support");
961 MODULE_LICENSE("GPL v2");