Linux 4.18.10
[linux/fpc-iii.git] / drivers / dma / qcom / hidma.c
blob43d4b00b81388e061b8b9bc2f251617509030821
1 /*
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
22 * Written by Piotr Ziecik <kosmo@semihalf.com>. Hardware description
23 * (defines, structures and comments) was taken from MPC5121 DMA driver
24 * written by Hongjun Chen <hong-jun.chen@freescale.com>.
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)
32 * any later version.
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
37 * more details.
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/module.h>
49 #include <linux/platform_device.h>
50 #include <linux/slab.h>
51 #include <linux/spinlock.h>
52 #include <linux/of_dma.h>
53 #include <linux/of_device.h>
54 #include <linux/property.h>
55 #include <linux/delay.h>
56 #include <linux/acpi.h>
57 #include <linux/irq.h>
58 #include <linux/atomic.h>
59 #include <linux/pm_runtime.h>
60 #include <linux/msi.h>
62 #include "../dmaengine.h"
63 #include "hidma.h"
66 * Default idle time is 2 seconds. This parameter can
67 * be overridden by changing the following
68 * /sys/bus/platform/devices/QCOM8061:<xy>/power/autosuspend_delay_ms
69 * during kernel boot.
71 #define HIDMA_AUTOSUSPEND_TIMEOUT 2000
72 #define HIDMA_ERR_INFO_SW 0xFF
73 #define HIDMA_ERR_CODE_UNEXPECTED_TERMINATE 0x0
74 #define HIDMA_NR_DEFAULT_DESC 10
75 #define HIDMA_MSI_INTS 11
77 static inline struct hidma_dev *to_hidma_dev(struct dma_device *dmadev)
79 return container_of(dmadev, struct hidma_dev, ddev);
82 static inline
83 struct hidma_dev *to_hidma_dev_from_lldev(struct hidma_lldev **_lldevp)
85 return container_of(_lldevp, struct hidma_dev, lldev);
88 static inline struct hidma_chan *to_hidma_chan(struct dma_chan *dmach)
90 return container_of(dmach, struct hidma_chan, chan);
93 static inline
94 struct hidma_desc *to_hidma_desc(struct dma_async_tx_descriptor *t)
96 return container_of(t, struct hidma_desc, desc);
99 static void hidma_free(struct hidma_dev *dmadev)
101 INIT_LIST_HEAD(&dmadev->ddev.channels);
104 static unsigned int nr_desc_prm;
105 module_param(nr_desc_prm, uint, 0644);
106 MODULE_PARM_DESC(nr_desc_prm, "number of descriptors (default: 0)");
108 enum hidma_cap {
109 HIDMA_MSI_CAP = 1,
110 HIDMA_IDENTITY_CAP,
113 /* process completed descriptors */
114 static void hidma_process_completed(struct hidma_chan *mchan)
116 struct dma_device *ddev = mchan->chan.device;
117 struct hidma_dev *mdma = to_hidma_dev(ddev);
118 struct dma_async_tx_descriptor *desc;
119 dma_cookie_t last_cookie;
120 struct hidma_desc *mdesc;
121 struct hidma_desc *next;
122 unsigned long irqflags;
123 struct list_head list;
125 INIT_LIST_HEAD(&list);
127 /* Get all completed descriptors */
128 spin_lock_irqsave(&mchan->lock, irqflags);
129 list_splice_tail_init(&mchan->completed, &list);
130 spin_unlock_irqrestore(&mchan->lock, irqflags);
132 /* Execute callbacks and run dependencies */
133 list_for_each_entry_safe(mdesc, next, &list, node) {
134 enum dma_status llstat;
135 struct dmaengine_desc_callback cb;
136 struct dmaengine_result result;
138 desc = &mdesc->desc;
139 last_cookie = desc->cookie;
141 spin_lock_irqsave(&mchan->lock, irqflags);
142 dma_cookie_complete(desc);
143 spin_unlock_irqrestore(&mchan->lock, irqflags);
145 llstat = hidma_ll_status(mdma->lldev, mdesc->tre_ch);
146 dmaengine_desc_get_callback(desc, &cb);
148 dma_run_dependencies(desc);
150 spin_lock_irqsave(&mchan->lock, irqflags);
151 list_move(&mdesc->node, &mchan->free);
153 if (llstat == DMA_COMPLETE) {
154 mchan->last_success = last_cookie;
155 result.result = DMA_TRANS_NOERROR;
156 } else
157 result.result = DMA_TRANS_ABORTED;
159 spin_unlock_irqrestore(&mchan->lock, irqflags);
161 dmaengine_desc_callback_invoke(&cb, &result);
166 * Called once for each submitted descriptor.
167 * PM is locked once for each descriptor that is currently
168 * in execution.
170 static void hidma_callback(void *data)
172 struct hidma_desc *mdesc = data;
173 struct hidma_chan *mchan = to_hidma_chan(mdesc->desc.chan);
174 struct dma_device *ddev = mchan->chan.device;
175 struct hidma_dev *dmadev = to_hidma_dev(ddev);
176 unsigned long irqflags;
177 bool queued = false;
179 spin_lock_irqsave(&mchan->lock, irqflags);
180 if (mdesc->node.next) {
181 /* Delete from the active list, add to completed list */
182 list_move_tail(&mdesc->node, &mchan->completed);
183 queued = true;
185 /* calculate the next running descriptor */
186 mchan->running = list_first_entry(&mchan->active,
187 struct hidma_desc, node);
189 spin_unlock_irqrestore(&mchan->lock, irqflags);
191 hidma_process_completed(mchan);
193 if (queued) {
194 pm_runtime_mark_last_busy(dmadev->ddev.dev);
195 pm_runtime_put_autosuspend(dmadev->ddev.dev);
199 static int hidma_chan_init(struct hidma_dev *dmadev, u32 dma_sig)
201 struct hidma_chan *mchan;
202 struct dma_device *ddev;
204 mchan = devm_kzalloc(dmadev->ddev.dev, sizeof(*mchan), GFP_KERNEL);
205 if (!mchan)
206 return -ENOMEM;
208 ddev = &dmadev->ddev;
209 mchan->dma_sig = dma_sig;
210 mchan->dmadev = dmadev;
211 mchan->chan.device = ddev;
212 dma_cookie_init(&mchan->chan);
214 INIT_LIST_HEAD(&mchan->free);
215 INIT_LIST_HEAD(&mchan->prepared);
216 INIT_LIST_HEAD(&mchan->active);
217 INIT_LIST_HEAD(&mchan->completed);
218 INIT_LIST_HEAD(&mchan->queued);
220 spin_lock_init(&mchan->lock);
221 list_add_tail(&mchan->chan.device_node, &ddev->channels);
222 dmadev->ddev.chancnt++;
223 return 0;
226 static void hidma_issue_task(unsigned long arg)
228 struct hidma_dev *dmadev = (struct hidma_dev *)arg;
230 pm_runtime_get_sync(dmadev->ddev.dev);
231 hidma_ll_start(dmadev->lldev);
234 static void hidma_issue_pending(struct dma_chan *dmach)
236 struct hidma_chan *mchan = to_hidma_chan(dmach);
237 struct hidma_dev *dmadev = mchan->dmadev;
238 unsigned long flags;
239 struct hidma_desc *qdesc, *next;
240 int status;
242 spin_lock_irqsave(&mchan->lock, flags);
243 list_for_each_entry_safe(qdesc, next, &mchan->queued, node) {
244 hidma_ll_queue_request(dmadev->lldev, qdesc->tre_ch);
245 list_move_tail(&qdesc->node, &mchan->active);
248 if (!mchan->running) {
249 struct hidma_desc *desc = list_first_entry(&mchan->active,
250 struct hidma_desc,
251 node);
252 mchan->running = desc;
254 spin_unlock_irqrestore(&mchan->lock, flags);
256 /* PM will be released in hidma_callback function. */
257 status = pm_runtime_get(dmadev->ddev.dev);
258 if (status < 0)
259 tasklet_schedule(&dmadev->task);
260 else
261 hidma_ll_start(dmadev->lldev);
264 static inline bool hidma_txn_is_success(dma_cookie_t cookie,
265 dma_cookie_t last_success, dma_cookie_t last_used)
267 if (last_success <= last_used) {
268 if ((cookie <= last_success) || (cookie > last_used))
269 return true;
270 } else {
271 if ((cookie <= last_success) && (cookie > last_used))
272 return true;
274 return false;
277 static enum dma_status hidma_tx_status(struct dma_chan *dmach,
278 dma_cookie_t cookie,
279 struct dma_tx_state *txstate)
281 struct hidma_chan *mchan = to_hidma_chan(dmach);
282 enum dma_status ret;
284 ret = dma_cookie_status(dmach, cookie, txstate);
285 if (ret == DMA_COMPLETE) {
286 bool is_success;
288 is_success = hidma_txn_is_success(cookie, mchan->last_success,
289 dmach->cookie);
290 return is_success ? ret : DMA_ERROR;
293 if (mchan->paused && (ret == DMA_IN_PROGRESS)) {
294 unsigned long flags;
295 dma_cookie_t runcookie;
297 spin_lock_irqsave(&mchan->lock, flags);
298 if (mchan->running)
299 runcookie = mchan->running->desc.cookie;
300 else
301 runcookie = -EINVAL;
303 if (runcookie == cookie)
304 ret = DMA_PAUSED;
306 spin_unlock_irqrestore(&mchan->lock, flags);
309 return ret;
313 * Submit descriptor to hardware.
314 * Lock the PM for each descriptor we are sending.
316 static dma_cookie_t hidma_tx_submit(struct dma_async_tx_descriptor *txd)
318 struct hidma_chan *mchan = to_hidma_chan(txd->chan);
319 struct hidma_dev *dmadev = mchan->dmadev;
320 struct hidma_desc *mdesc;
321 unsigned long irqflags;
322 dma_cookie_t cookie;
324 pm_runtime_get_sync(dmadev->ddev.dev);
325 if (!hidma_ll_isenabled(dmadev->lldev)) {
326 pm_runtime_mark_last_busy(dmadev->ddev.dev);
327 pm_runtime_put_autosuspend(dmadev->ddev.dev);
328 return -ENODEV;
330 pm_runtime_mark_last_busy(dmadev->ddev.dev);
331 pm_runtime_put_autosuspend(dmadev->ddev.dev);
333 mdesc = container_of(txd, struct hidma_desc, desc);
334 spin_lock_irqsave(&mchan->lock, irqflags);
336 /* Move descriptor to queued */
337 list_move_tail(&mdesc->node, &mchan->queued);
339 /* Update cookie */
340 cookie = dma_cookie_assign(txd);
342 spin_unlock_irqrestore(&mchan->lock, irqflags);
344 return cookie;
347 static int hidma_alloc_chan_resources(struct dma_chan *dmach)
349 struct hidma_chan *mchan = to_hidma_chan(dmach);
350 struct hidma_dev *dmadev = mchan->dmadev;
351 struct hidma_desc *mdesc, *tmp;
352 unsigned long irqflags;
353 LIST_HEAD(descs);
354 unsigned int i;
355 int rc = 0;
357 if (mchan->allocated)
358 return 0;
360 /* Alloc descriptors for this channel */
361 for (i = 0; i < dmadev->nr_descriptors; i++) {
362 mdesc = kzalloc(sizeof(struct hidma_desc), GFP_NOWAIT);
363 if (!mdesc) {
364 rc = -ENOMEM;
365 break;
367 dma_async_tx_descriptor_init(&mdesc->desc, dmach);
368 mdesc->desc.tx_submit = hidma_tx_submit;
370 rc = hidma_ll_request(dmadev->lldev, mchan->dma_sig,
371 "DMA engine", hidma_callback, mdesc,
372 &mdesc->tre_ch);
373 if (rc) {
374 dev_err(dmach->device->dev,
375 "channel alloc failed at %u\n", i);
376 kfree(mdesc);
377 break;
379 list_add_tail(&mdesc->node, &descs);
382 if (rc) {
383 /* return the allocated descriptors */
384 list_for_each_entry_safe(mdesc, tmp, &descs, node) {
385 hidma_ll_free(dmadev->lldev, mdesc->tre_ch);
386 kfree(mdesc);
388 return rc;
391 spin_lock_irqsave(&mchan->lock, irqflags);
392 list_splice_tail_init(&descs, &mchan->free);
393 mchan->allocated = true;
394 spin_unlock_irqrestore(&mchan->lock, irqflags);
395 return 1;
398 static struct dma_async_tx_descriptor *
399 hidma_prep_dma_memcpy(struct dma_chan *dmach, dma_addr_t dest, dma_addr_t src,
400 size_t len, unsigned long flags)
402 struct hidma_chan *mchan = to_hidma_chan(dmach);
403 struct hidma_desc *mdesc = NULL;
404 struct hidma_dev *mdma = mchan->dmadev;
405 unsigned long irqflags;
407 /* Get free descriptor */
408 spin_lock_irqsave(&mchan->lock, irqflags);
409 if (!list_empty(&mchan->free)) {
410 mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
411 list_del(&mdesc->node);
413 spin_unlock_irqrestore(&mchan->lock, irqflags);
415 if (!mdesc)
416 return NULL;
418 hidma_ll_set_transfer_params(mdma->lldev, mdesc->tre_ch,
419 src, dest, len, flags,
420 HIDMA_TRE_MEMCPY);
422 /* Place descriptor in prepared list */
423 spin_lock_irqsave(&mchan->lock, irqflags);
424 list_add_tail(&mdesc->node, &mchan->prepared);
425 spin_unlock_irqrestore(&mchan->lock, irqflags);
427 return &mdesc->desc;
430 static struct dma_async_tx_descriptor *
431 hidma_prep_dma_memset(struct dma_chan *dmach, dma_addr_t dest, int value,
432 size_t len, unsigned long flags)
434 struct hidma_chan *mchan = to_hidma_chan(dmach);
435 struct hidma_desc *mdesc = NULL;
436 struct hidma_dev *mdma = mchan->dmadev;
437 unsigned long irqflags;
439 /* Get free descriptor */
440 spin_lock_irqsave(&mchan->lock, irqflags);
441 if (!list_empty(&mchan->free)) {
442 mdesc = list_first_entry(&mchan->free, struct hidma_desc, node);
443 list_del(&mdesc->node);
445 spin_unlock_irqrestore(&mchan->lock, irqflags);
447 if (!mdesc)
448 return NULL;
450 hidma_ll_set_transfer_params(mdma->lldev, mdesc->tre_ch,
451 value, dest, len, flags,
452 HIDMA_TRE_MEMSET);
454 /* Place descriptor in prepared list */
455 spin_lock_irqsave(&mchan->lock, irqflags);
456 list_add_tail(&mdesc->node, &mchan->prepared);
457 spin_unlock_irqrestore(&mchan->lock, irqflags);
459 return &mdesc->desc;
462 static int hidma_terminate_channel(struct dma_chan *chan)
464 struct hidma_chan *mchan = to_hidma_chan(chan);
465 struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
466 struct hidma_desc *tmp, *mdesc;
467 unsigned long irqflags;
468 LIST_HEAD(list);
469 int rc;
471 pm_runtime_get_sync(dmadev->ddev.dev);
472 /* give completed requests a chance to finish */
473 hidma_process_completed(mchan);
475 spin_lock_irqsave(&mchan->lock, irqflags);
476 mchan->last_success = 0;
477 list_splice_init(&mchan->active, &list);
478 list_splice_init(&mchan->prepared, &list);
479 list_splice_init(&mchan->completed, &list);
480 list_splice_init(&mchan->queued, &list);
481 spin_unlock_irqrestore(&mchan->lock, irqflags);
483 /* this suspends the existing transfer */
484 rc = hidma_ll_disable(dmadev->lldev);
485 if (rc) {
486 dev_err(dmadev->ddev.dev, "channel did not pause\n");
487 goto out;
490 /* return all user requests */
491 list_for_each_entry_safe(mdesc, tmp, &list, node) {
492 struct dma_async_tx_descriptor *txd = &mdesc->desc;
494 dma_descriptor_unmap(txd);
495 dmaengine_desc_get_callback_invoke(txd, NULL);
496 dma_run_dependencies(txd);
498 /* move myself to free_list */
499 list_move(&mdesc->node, &mchan->free);
502 rc = hidma_ll_enable(dmadev->lldev);
503 out:
504 pm_runtime_mark_last_busy(dmadev->ddev.dev);
505 pm_runtime_put_autosuspend(dmadev->ddev.dev);
506 return rc;
509 static int hidma_terminate_all(struct dma_chan *chan)
511 struct hidma_chan *mchan = to_hidma_chan(chan);
512 struct hidma_dev *dmadev = to_hidma_dev(mchan->chan.device);
513 int rc;
515 rc = hidma_terminate_channel(chan);
516 if (rc)
517 return rc;
519 /* reinitialize the hardware */
520 pm_runtime_get_sync(dmadev->ddev.dev);
521 rc = hidma_ll_setup(dmadev->lldev);
522 pm_runtime_mark_last_busy(dmadev->ddev.dev);
523 pm_runtime_put_autosuspend(dmadev->ddev.dev);
524 return rc;
527 static void hidma_free_chan_resources(struct dma_chan *dmach)
529 struct hidma_chan *mchan = to_hidma_chan(dmach);
530 struct hidma_dev *mdma = mchan->dmadev;
531 struct hidma_desc *mdesc, *tmp;
532 unsigned long irqflags;
533 LIST_HEAD(descs);
535 /* terminate running transactions and free descriptors */
536 hidma_terminate_channel(dmach);
538 spin_lock_irqsave(&mchan->lock, irqflags);
540 /* Move data */
541 list_splice_tail_init(&mchan->free, &descs);
543 /* Free descriptors */
544 list_for_each_entry_safe(mdesc, tmp, &descs, node) {
545 hidma_ll_free(mdma->lldev, mdesc->tre_ch);
546 list_del(&mdesc->node);
547 kfree(mdesc);
550 mchan->allocated = 0;
551 spin_unlock_irqrestore(&mchan->lock, irqflags);
554 static int hidma_pause(struct dma_chan *chan)
556 struct hidma_chan *mchan;
557 struct hidma_dev *dmadev;
559 mchan = to_hidma_chan(chan);
560 dmadev = to_hidma_dev(mchan->chan.device);
561 if (!mchan->paused) {
562 pm_runtime_get_sync(dmadev->ddev.dev);
563 if (hidma_ll_disable(dmadev->lldev))
564 dev_warn(dmadev->ddev.dev, "channel did not stop\n");
565 mchan->paused = true;
566 pm_runtime_mark_last_busy(dmadev->ddev.dev);
567 pm_runtime_put_autosuspend(dmadev->ddev.dev);
569 return 0;
572 static int hidma_resume(struct dma_chan *chan)
574 struct hidma_chan *mchan;
575 struct hidma_dev *dmadev;
576 int rc = 0;
578 mchan = to_hidma_chan(chan);
579 dmadev = to_hidma_dev(mchan->chan.device);
580 if (mchan->paused) {
581 pm_runtime_get_sync(dmadev->ddev.dev);
582 rc = hidma_ll_enable(dmadev->lldev);
583 if (!rc)
584 mchan->paused = false;
585 else
586 dev_err(dmadev->ddev.dev,
587 "failed to resume the channel");
588 pm_runtime_mark_last_busy(dmadev->ddev.dev);
589 pm_runtime_put_autosuspend(dmadev->ddev.dev);
591 return rc;
594 static irqreturn_t hidma_chirq_handler(int chirq, void *arg)
596 struct hidma_lldev *lldev = arg;
599 * All interrupts are request driven.
600 * HW doesn't send an interrupt by itself.
602 return hidma_ll_inthandler(chirq, lldev);
605 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
606 static irqreturn_t hidma_chirq_handler_msi(int chirq, void *arg)
608 struct hidma_lldev **lldevp = arg;
609 struct hidma_dev *dmadev = to_hidma_dev_from_lldev(lldevp);
611 return hidma_ll_inthandler_msi(chirq, *lldevp,
612 1 << (chirq - dmadev->msi_virqbase));
614 #endif
616 static ssize_t hidma_show_values(struct device *dev,
617 struct device_attribute *attr, char *buf)
619 struct hidma_dev *mdev = dev_get_drvdata(dev);
621 buf[0] = 0;
623 if (strcmp(attr->attr.name, "chid") == 0)
624 sprintf(buf, "%d\n", mdev->chidx);
626 return strlen(buf);
629 static inline void hidma_sysfs_uninit(struct hidma_dev *dev)
631 device_remove_file(dev->ddev.dev, dev->chid_attrs);
634 static struct device_attribute*
635 hidma_create_sysfs_entry(struct hidma_dev *dev, char *name, int mode)
637 struct device_attribute *attrs;
638 char *name_copy;
640 attrs = devm_kmalloc(dev->ddev.dev, sizeof(struct device_attribute),
641 GFP_KERNEL);
642 if (!attrs)
643 return NULL;
645 name_copy = devm_kstrdup(dev->ddev.dev, name, GFP_KERNEL);
646 if (!name_copy)
647 return NULL;
649 attrs->attr.name = name_copy;
650 attrs->attr.mode = mode;
651 attrs->show = hidma_show_values;
652 sysfs_attr_init(&attrs->attr);
654 return attrs;
657 static int hidma_sysfs_init(struct hidma_dev *dev)
659 dev->chid_attrs = hidma_create_sysfs_entry(dev, "chid", S_IRUGO);
660 if (!dev->chid_attrs)
661 return -ENOMEM;
663 return device_create_file(dev->ddev.dev, dev->chid_attrs);
666 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
667 static void hidma_write_msi_msg(struct msi_desc *desc, struct msi_msg *msg)
669 struct device *dev = msi_desc_to_dev(desc);
670 struct hidma_dev *dmadev = dev_get_drvdata(dev);
672 if (!desc->platform.msi_index) {
673 writel(msg->address_lo, dmadev->dev_evca + 0x118);
674 writel(msg->address_hi, dmadev->dev_evca + 0x11C);
675 writel(msg->data, dmadev->dev_evca + 0x120);
678 #endif
680 static void hidma_free_msis(struct hidma_dev *dmadev)
682 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
683 struct device *dev = dmadev->ddev.dev;
684 struct msi_desc *desc;
686 /* free allocated MSI interrupts above */
687 for_each_msi_entry(desc, dev)
688 devm_free_irq(dev, desc->irq, &dmadev->lldev);
690 platform_msi_domain_free_irqs(dev);
691 #endif
694 static int hidma_request_msi(struct hidma_dev *dmadev,
695 struct platform_device *pdev)
697 #ifdef CONFIG_GENERIC_MSI_IRQ_DOMAIN
698 int rc;
699 struct msi_desc *desc;
700 struct msi_desc *failed_desc = NULL;
702 rc = platform_msi_domain_alloc_irqs(&pdev->dev, HIDMA_MSI_INTS,
703 hidma_write_msi_msg);
704 if (rc)
705 return rc;
707 for_each_msi_entry(desc, &pdev->dev) {
708 if (!desc->platform.msi_index)
709 dmadev->msi_virqbase = desc->irq;
711 rc = devm_request_irq(&pdev->dev, desc->irq,
712 hidma_chirq_handler_msi,
713 0, "qcom-hidma-msi",
714 &dmadev->lldev);
715 if (rc) {
716 failed_desc = desc;
717 break;
721 if (rc) {
722 /* free allocated MSI interrupts above */
723 for_each_msi_entry(desc, &pdev->dev) {
724 if (desc == failed_desc)
725 break;
726 devm_free_irq(&pdev->dev, desc->irq,
727 &dmadev->lldev);
729 } else {
730 /* Add callback to free MSIs on teardown */
731 hidma_ll_setup_irq(dmadev->lldev, true);
734 if (rc)
735 dev_warn(&pdev->dev,
736 "failed to request MSI irq, falling back to wired IRQ\n");
737 return rc;
738 #else
739 return -EINVAL;
740 #endif
743 static bool hidma_test_capability(struct device *dev, enum hidma_cap test_cap)
745 enum hidma_cap cap;
747 cap = (enum hidma_cap) 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;
756 int chirq;
757 void __iomem *evca;
758 void __iomem *trca;
759 int rc;
760 bool msi;
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_resource = platform_get_resource(pdev, IORESOURCE_MEM, 0);
768 trca = devm_ioremap_resource(&pdev->dev, trca_resource);
769 if (IS_ERR(trca)) {
770 rc = -ENOMEM;
771 goto bailout;
774 evca_resource = platform_get_resource(pdev, IORESOURCE_MEM, 1);
775 evca = devm_ioremap_resource(&pdev->dev, evca_resource);
776 if (IS_ERR(evca)) {
777 rc = -ENOMEM;
778 goto bailout;
782 * This driver only handles the channel IRQs.
783 * Common IRQ is handled by the management driver.
785 chirq = platform_get_irq(pdev, 0);
786 if (chirq < 0) {
787 rc = -ENODEV;
788 goto bailout;
791 dmadev = devm_kzalloc(&pdev->dev, sizeof(*dmadev), GFP_KERNEL);
792 if (!dmadev) {
793 rc = -ENOMEM;
794 goto bailout;
797 INIT_LIST_HEAD(&dmadev->ddev.channels);
798 spin_lock_init(&dmadev->lock);
799 dmadev->ddev.dev = &pdev->dev;
800 pm_runtime_get_sync(dmadev->ddev.dev);
802 dma_cap_set(DMA_MEMCPY, dmadev->ddev.cap_mask);
803 dma_cap_set(DMA_MEMSET, dmadev->ddev.cap_mask);
804 if (WARN_ON(!pdev->dev.dma_mask)) {
805 rc = -ENXIO;
806 goto dmafree;
809 dmadev->dev_evca = evca;
810 dmadev->evca_resource = evca_resource;
811 dmadev->dev_trca = trca;
812 dmadev->trca_resource = trca_resource;
813 dmadev->ddev.device_prep_dma_memcpy = hidma_prep_dma_memcpy;
814 dmadev->ddev.device_prep_dma_memset = hidma_prep_dma_memset;
815 dmadev->ddev.device_alloc_chan_resources = hidma_alloc_chan_resources;
816 dmadev->ddev.device_free_chan_resources = hidma_free_chan_resources;
817 dmadev->ddev.device_tx_status = hidma_tx_status;
818 dmadev->ddev.device_issue_pending = hidma_issue_pending;
819 dmadev->ddev.device_pause = hidma_pause;
820 dmadev->ddev.device_resume = hidma_resume;
821 dmadev->ddev.device_terminate_all = hidma_terminate_all;
822 dmadev->ddev.copy_align = 8;
825 * Determine the MSI capability of the platform. Old HW doesn't
826 * support MSI.
828 msi = hidma_test_capability(&pdev->dev, HIDMA_MSI_CAP);
829 device_property_read_u32(&pdev->dev, "desc-count",
830 &dmadev->nr_descriptors);
832 if (nr_desc_prm) {
833 dev_info(&pdev->dev, "overriding number of descriptors as %d\n",
834 nr_desc_prm);
835 dmadev->nr_descriptors = nr_desc_prm;
838 if (!dmadev->nr_descriptors)
839 dmadev->nr_descriptors = HIDMA_NR_DEFAULT_DESC;
841 if (hidma_test_capability(&pdev->dev, HIDMA_IDENTITY_CAP))
842 dmadev->chidx = readl(dmadev->dev_trca + 0x40);
843 else
844 dmadev->chidx = readl(dmadev->dev_trca + 0x28);
846 /* Set DMA mask to 64 bits. */
847 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
848 if (rc) {
849 dev_warn(&pdev->dev, "unable to set coherent mask to 64");
850 rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
851 if (rc)
852 goto dmafree;
855 dmadev->lldev = hidma_ll_init(dmadev->ddev.dev,
856 dmadev->nr_descriptors, dmadev->dev_trca,
857 dmadev->dev_evca, dmadev->chidx);
858 if (!dmadev->lldev) {
859 rc = -EPROBE_DEFER;
860 goto dmafree;
863 platform_set_drvdata(pdev, dmadev);
864 if (msi)
865 rc = hidma_request_msi(dmadev, pdev);
867 if (!msi || rc) {
868 hidma_ll_setup_irq(dmadev->lldev, false);
869 rc = devm_request_irq(&pdev->dev, chirq, hidma_chirq_handler,
870 0, "qcom-hidma", dmadev->lldev);
871 if (rc)
872 goto uninit;
875 INIT_LIST_HEAD(&dmadev->ddev.channels);
876 rc = hidma_chan_init(dmadev, 0);
877 if (rc)
878 goto uninit;
880 rc = dma_async_device_register(&dmadev->ddev);
881 if (rc)
882 goto uninit;
884 dmadev->irq = chirq;
885 tasklet_init(&dmadev->task, hidma_issue_task, (unsigned long)dmadev);
886 hidma_debug_init(dmadev);
887 hidma_sysfs_init(dmadev);
888 dev_info(&pdev->dev, "HI-DMA engine driver registration complete\n");
889 pm_runtime_mark_last_busy(dmadev->ddev.dev);
890 pm_runtime_put_autosuspend(dmadev->ddev.dev);
891 return 0;
893 uninit:
894 if (msi)
895 hidma_free_msis(dmadev);
897 hidma_debug_uninit(dmadev);
898 hidma_ll_uninit(dmadev->lldev);
899 dmafree:
900 if (dmadev)
901 hidma_free(dmadev);
902 bailout:
903 pm_runtime_put_sync(&pdev->dev);
904 pm_runtime_disable(&pdev->dev);
905 return rc;
908 static void hidma_shutdown(struct platform_device *pdev)
910 struct hidma_dev *dmadev = platform_get_drvdata(pdev);
912 dev_info(dmadev->ddev.dev, "HI-DMA engine shutdown\n");
914 pm_runtime_get_sync(dmadev->ddev.dev);
915 if (hidma_ll_disable(dmadev->lldev))
916 dev_warn(dmadev->ddev.dev, "channel did not stop\n");
917 pm_runtime_mark_last_busy(dmadev->ddev.dev);
918 pm_runtime_put_autosuspend(dmadev->ddev.dev);
922 static int hidma_remove(struct platform_device *pdev)
924 struct hidma_dev *dmadev = platform_get_drvdata(pdev);
926 pm_runtime_get_sync(dmadev->ddev.dev);
927 dma_async_device_unregister(&dmadev->ddev);
928 if (!dmadev->lldev->msi_support)
929 devm_free_irq(dmadev->ddev.dev, dmadev->irq, dmadev->lldev);
930 else
931 hidma_free_msis(dmadev);
933 tasklet_kill(&dmadev->task);
934 hidma_sysfs_uninit(dmadev);
935 hidma_debug_uninit(dmadev);
936 hidma_ll_uninit(dmadev->lldev);
937 hidma_free(dmadev);
939 dev_info(&pdev->dev, "HI-DMA engine removed\n");
940 pm_runtime_put_sync_suspend(&pdev->dev);
941 pm_runtime_disable(&pdev->dev);
943 return 0;
946 #if IS_ENABLED(CONFIG_ACPI)
947 static const struct acpi_device_id hidma_acpi_ids[] = {
948 {"QCOM8061"},
949 {"QCOM8062", HIDMA_MSI_CAP},
950 {"QCOM8063", (HIDMA_MSI_CAP | HIDMA_IDENTITY_CAP)},
953 MODULE_DEVICE_TABLE(acpi, hidma_acpi_ids);
954 #endif
956 static const struct of_device_id hidma_match[] = {
957 {.compatible = "qcom,hidma-1.0",},
958 {.compatible = "qcom,hidma-1.1", .data = (void *)(HIDMA_MSI_CAP),},
959 {.compatible = "qcom,hidma-1.2",
960 .data = (void *)(HIDMA_MSI_CAP | HIDMA_IDENTITY_CAP),},
963 MODULE_DEVICE_TABLE(of, hidma_match);
965 static struct platform_driver hidma_driver = {
966 .probe = hidma_probe,
967 .remove = hidma_remove,
968 .shutdown = hidma_shutdown,
969 .driver = {
970 .name = "hidma",
971 .of_match_table = hidma_match,
972 .acpi_match_table = ACPI_PTR(hidma_acpi_ids),
976 module_platform_driver(hidma_driver);
977 MODULE_LICENSE("GPL v2");