hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/dma/bcm2835-dma.c
....@@ -1,8 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0+
12 /*
23 * BCM2835 DMA engine support
3
- *
4
- * This driver only supports cyclic DMA transfers
5
- * as needed for the I2S module.
64 *
75 * Author: Florian Meier <florian.meier@koalo.de>
86 * Copyright 2013
....@@ -18,16 +16,6 @@
1816 *
1917 * MARVELL MMP Peripheral DMA Driver
2018 * Copyright 2012 Marvell International Ltd.
21
- *
22
- * This program is free software; you can redistribute it and/or modify
23
- * it under the terms of the GNU General Public License as published by
24
- * the Free Software Foundation; either version 2 of the License, or
25
- * (at your option) any later version.
26
- *
27
- * This program is distributed in the hope that it will be useful,
28
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30
- * GNU General Public License for more details.
3119 */
3220 #include <linux/dmaengine.h>
3321 #include <linux/dma-mapping.h>
....@@ -49,11 +37,17 @@
4937 #define BCM2835_DMA_MAX_DMA_CHAN_SUPPORTED 14
5038 #define BCM2835_DMA_CHAN_NAME_SIZE 8
5139
40
+/**
41
+ * struct bcm2835_dmadev - BCM2835 DMA controller
42
+ * @ddev: DMA device
43
+ * @base: base address of register map
44
+ * @zero_page: bus address of zero page (to detect transactions copying from
45
+ * zero page and avoid accessing memory if so)
46
+ */
5247 struct bcm2835_dmadev {
5348 struct dma_device ddev;
54
- spinlock_t lock;
5549 void __iomem *base;
56
- struct device_dma_parameters dma_parms;
50
+ dma_addr_t zero_page;
5751 };
5852
5953 struct bcm2835_dma_cb {
....@@ -73,7 +67,6 @@
7367
7468 struct bcm2835_chan {
7569 struct virt_dma_chan vc;
76
- struct list_head node;
7770
7871 struct dma_slave_config cfg;
7972 unsigned int dreq;
....@@ -321,8 +314,7 @@
321314 return NULL;
322315
323316 /* allocate and setup the descriptor. */
324
- d = kzalloc(sizeof(*d) + frames * sizeof(struct bcm2835_cb_entry),
325
- gfp);
317
+ d = kzalloc(struct_size(d, cb_list, frames), gfp);
326318 if (!d)
327319 return NULL;
328320
....@@ -415,7 +407,7 @@
415407 }
416408 }
417409
418
-static int bcm2835_dma_abort(struct bcm2835_chan *c)
410
+static void bcm2835_dma_abort(struct bcm2835_chan *c)
419411 {
420412 void __iomem *chan_base = c->chan_base;
421413 long int timeout = 10000;
....@@ -425,7 +417,7 @@
425417 * (The ACTIVE flag in the CS register is not a reliable indicator.)
426418 */
427419 if (!readl(chan_base + BCM2835_DMA_ADDR))
428
- return 0;
420
+ return;
429421
430422 /* Write 0 to the active bit - Pause the DMA */
431423 writel(0, chan_base + BCM2835_DMA_CS);
....@@ -441,7 +433,6 @@
441433 "failed to complete outstanding writes\n");
442434
443435 writel(BCM2835_DMA_RESET, chan_base + BCM2835_DMA_CS);
444
- return 0;
445436 }
446437
447438 static void bcm2835_dma_start_desc(struct bcm2835_chan *c)
....@@ -513,8 +504,12 @@
513504
514505 dev_dbg(dev, "Allocating DMA channel %d\n", c->ch);
515506
507
+ /*
508
+ * Control blocks are 256 bit in length and must start at a 256 bit
509
+ * (32 byte) aligned address (BCM2835 ARM Peripherals, sec. 4.2.1.1).
510
+ */
516511 c->cb_pool = dma_pool_create(dev_name(dev), dev,
517
- sizeof(struct bcm2835_dma_cb), 0, 0);
512
+ sizeof(struct bcm2835_dma_cb), 32, 0);
518513 if (!c->cb_pool) {
519514 dev_err(dev, "unable to allocate descriptor pool\n");
520515 return -ENOMEM;
....@@ -683,7 +678,7 @@
683678 d = bcm2835_dma_create_cb_chain(chan, direction, false,
684679 info, extra,
685680 frames, src, dst, 0, 0,
686
- GFP_KERNEL);
681
+ GFP_NOWAIT);
687682 if (!d)
688683 return NULL;
689684
....@@ -699,11 +694,12 @@
699694 size_t period_len, enum dma_transfer_direction direction,
700695 unsigned long flags)
701696 {
697
+ struct bcm2835_dmadev *od = to_bcm2835_dma_dev(chan->device);
702698 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
703699 struct bcm2835_desc *d;
704700 dma_addr_t src, dst;
705701 u32 info = BCM2835_DMA_WAIT_RESP;
706
- u32 extra = BCM2835_DMA_INT_EN;
702
+ u32 extra = 0;
707703 size_t max_len = bcm2835_dma_max_frame_length(c);
708704 size_t frames;
709705
....@@ -718,6 +714,11 @@
718714 "%s: bad buffer length (= 0)\n", __func__);
719715 return NULL;
720716 }
717
+
718
+ if (flags & DMA_PREP_INTERRUPT)
719
+ extra |= BCM2835_DMA_INT_EN;
720
+ else
721
+ period_len = buf_len;
721722
722723 /*
723724 * warn if buf_len is not a multiple of period_len - this may leed
....@@ -744,6 +745,10 @@
744745 dst = c->cfg.dst_addr;
745746 src = buf_addr;
746747 info |= BCM2835_DMA_D_DREQ | BCM2835_DMA_S_INC;
748
+
749
+ /* non-lite channels can write zeroes w/o accessing memory */
750
+ if (buf_addr == od->zero_page && !c->is_lite_channel)
751
+ info |= BCM2835_DMA_S_IGNORE;
747752 }
748753
749754 /* calculate number of frames */
....@@ -775,14 +780,6 @@
775780 {
776781 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
777782
778
- if ((cfg->direction == DMA_DEV_TO_MEM &&
779
- cfg->src_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
780
- (cfg->direction == DMA_MEM_TO_DEV &&
781
- cfg->dst_addr_width != DMA_SLAVE_BUSWIDTH_4_BYTES) ||
782
- !is_slave_direction(cfg->direction)) {
783
- return -EINVAL;
784
- }
785
-
786783 c->cfg = *cfg;
787784
788785 return 0;
....@@ -791,16 +788,10 @@
791788 static int bcm2835_dma_terminate_all(struct dma_chan *chan)
792789 {
793790 struct bcm2835_chan *c = to_bcm2835_dma_chan(chan);
794
- struct bcm2835_dmadev *d = to_bcm2835_dma_dev(c->vc.chan.device);
795791 unsigned long flags;
796792 LIST_HEAD(head);
797793
798794 spin_lock_irqsave(&c->vc.lock, flags);
799
-
800
- /* Prevent this channel being scheduled */
801
- spin_lock(&d->lock);
802
- list_del_init(&c->node);
803
- spin_unlock(&d->lock);
804795
805796 /* stop DMA activity */
806797 if (c->desc) {
....@@ -834,7 +825,6 @@
834825
835826 c->vc.desc_free = bcm2835_dma_desc_free;
836827 vchan_init(&c->vc, &d->ddev);
837
- INIT_LIST_HEAD(&c->node);
838828
839829 c->chan_base = BCM2835_DMA_CHANIO(d->base, chan_id);
840830 c->ch = chan_id;
....@@ -858,6 +848,9 @@
858848 list_del(&c->vc.chan.device_node);
859849 tasklet_kill(&c->vc.task);
860850 }
851
+
852
+ dma_unmap_page_attrs(od->ddev.dev, od->zero_page, PAGE_SIZE,
853
+ DMA_TO_DEVICE, DMA_ATTR_SKIP_CPU_SYNC);
861854 }
862855
863856 static const struct of_device_id bcm2835_dma_of_match[] = {
....@@ -907,7 +900,6 @@
907900 if (!od)
908901 return -ENOMEM;
909902
910
- pdev->dev.dma_parms = &od->dma_parms;
911903 dma_set_max_seg_size(&pdev->dev, 0x3FFFFFFF);
912904
913905 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
....@@ -920,7 +912,6 @@
920912 dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
921913 dma_cap_set(DMA_PRIVATE, od->ddev.cap_mask);
922914 dma_cap_set(DMA_CYCLIC, od->ddev.cap_mask);
923
- dma_cap_set(DMA_SLAVE, od->ddev.cap_mask);
924915 dma_cap_set(DMA_MEMCPY, od->ddev.cap_mask);
925916 od->ddev.device_alloc_chan_resources = bcm2835_dma_alloc_chan_resources;
926917 od->ddev.device_free_chan_resources = bcm2835_dma_free_chan_resources;
....@@ -937,11 +928,19 @@
937928 od->ddev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) |
938929 BIT(DMA_MEM_TO_MEM);
939930 od->ddev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
931
+ od->ddev.descriptor_reuse = true;
940932 od->ddev.dev = &pdev->dev;
941933 INIT_LIST_HEAD(&od->ddev.channels);
942
- spin_lock_init(&od->lock);
943934
944935 platform_set_drvdata(pdev, od);
936
+
937
+ od->zero_page = dma_map_page_attrs(od->ddev.dev, ZERO_PAGE(0), 0,
938
+ PAGE_SIZE, DMA_TO_DEVICE,
939
+ DMA_ATTR_SKIP_CPU_SYNC);
940
+ if (dma_mapping_error(od->ddev.dev, od->zero_page)) {
941
+ dev_err(&pdev->dev, "Failed to map zero page\n");
942
+ return -ENOMEM;
943
+ }
945944
946945 /* Request DMA channel mask from device tree */
947946 if (of_property_read_u32(pdev->dev.of_node,
....@@ -1046,4 +1045,4 @@
10461045 MODULE_ALIAS("platform:bcm2835-dma");
10471046 MODULE_DESCRIPTION("BCM2835 DMA engine driver");
10481047 MODULE_AUTHOR("Florian Meier <florian.meier@koalo.de>");
1049
-MODULE_LICENSE("GPL v2");
1048
+MODULE_LICENSE("GPL");