Skip to content

Commit 8ceafbf

Browse files
committed
Merge branch 'for-linus-dma-masks' of git://git.linaro.org/people/rmk/linux-arm
Pull DMA mask updates from Russell King: "This series cleans up the handling of DMA masks in a lot of drivers, fixing some bugs as we go. Some of the more serious errors include: - drivers which only set their coherent DMA mask if the attempt to set the streaming mask fails. - drivers which test for a NULL dma mask pointer, and then set the dma mask pointer to a location in their module .data section - which will cause problems if the module is reloaded. To counter these, I have introduced two helper functions: - dma_set_mask_and_coherent() takes care of setting both the streaming and coherent masks at the same time, with the correct error handling as specified by the API. - dma_coerce_mask_and_coherent() which resolves the problem of drivers forcefully setting DMA masks. This is more a marker for future work to further clean these locations up - the code which creates the devices really should be initialising these, but to fix that in one go along with this change could potentially be very disruptive. The last thing this series does is prise away some of Linux's addition to "DMA addresses are physical addresses and RAM always starts at zero". We have ARM LPAE systems where all system memory is above 4GB physical, hence having DMA masks interpreted by (eg) the block layers as describing physical addresses in the range 0..DMAMASK fails on these platforms. Santosh Shilimkar addresses this in this series; the patches were copied to the appropriate people multiple times but were ignored. Fixing this also gets rid of some ARM weirdness in the setup of the max*pfn variables, and brings ARM into line with every other Linux architecture as far as those go" * 'for-linus-dma-masks' of git://git.linaro.org/people/rmk/linux-arm: (52 commits) ARM: 7805/1: mm: change max*pfn to include the physical offset of memory ARM: 7797/1: mmc: Use dma_max_pfn(dev) helper for bounce_limit calculations ARM: 7796/1: scsi: Use dma_max_pfn(dev) helper for bounce_limit calculations ARM: 7795/1: mm: dma-mapping: Add dma_max_pfn(dev) helper function ARM: 7794/1: block: Rename parameter dma_mask to max_addr for blk_queue_bounce_limit() ARM: DMA-API: better handing of DMA masks for coherent allocations ARM: 7857/1: dma: imx-sdma: setup dma mask DMA-API: firmware/google/gsmi.c: avoid direct access to DMA masks DMA-API: dcdbas: update DMA mask handing DMA-API: dma: edma.c: no need to explicitly initialize DMA masks DMA-API: usb: musb: use platform_device_register_full() to avoid directly messing with dma masks DMA-API: crypto: remove last references to 'static struct device *dev' DMA-API: crypto: fix ixp4xx crypto platform device support DMA-API: others: use dma_set_coherent_mask() DMA-API: staging: use dma_set_coherent_mask() DMA-API: usb: use new dma_coerce_mask_and_coherent() DMA-API: usb: use dma_set_coherent_mask() DMA-API: parport: parport_pc.c: use dma_coerce_mask_and_coherent() DMA-API: net: octeon: use dma_coerce_mask_and_coherent() DMA-API: net: nxp/lpc_eth: use dma_coerce_mask_and_coherent() ...
2 parents 42a2d92 + 26ba47b commit 8ceafbf

91 files changed

Lines changed: 447 additions & 448 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎Documentation/DMA-API-HOWTO.txt‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,23 @@ style to do this even if your device holds the default setting,
101101
because this shows that you did think about these issues wrt. your
102102
device.
103103

104-
The query is performed via a call to dma_set_mask():
104+
The query is performed via a call to dma_set_mask_and_coherent():
105105

106-
int dma_set_mask(struct device *dev, u64 mask);
106+
int dma_set_mask_and_coherent(struct device *dev, u64 mask);
107107

108-
The query for consistent allocations is performed via a call to
109-
dma_set_coherent_mask():
108+
which will query the mask for both streaming and coherent APIs together.
109+
If you have some special requirements, then the following two separate
110+
queries can be used instead:
110111

111-
int dma_set_coherent_mask(struct device *dev, u64 mask);
112+
The query for streaming mappings is performed via a call to
113+
dma_set_mask():
114+
115+
int dma_set_mask(struct device *dev, u64 mask);
116+
117+
The query for consistent allocations is performed via a call
118+
to dma_set_coherent_mask():
119+
120+
int dma_set_coherent_mask(struct device *dev, u64 mask);
112121

113122
Here, dev is a pointer to the device struct of your device, and mask
114123
is a bit mask describing which bits of an address your device
@@ -137,7 +146,7 @@ exactly why.
137146

138147
The standard 32-bit addressing device would do something like this:
139148

140-
if (dma_set_mask(dev, DMA_BIT_MASK(32))) {
149+
if (dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
141150
printk(KERN_WARNING
142151
"mydev: No suitable DMA available.\n");
143152
goto ignore_this_device;
@@ -171,22 +180,20 @@ the case would look like this:
171180

172181
int using_dac, consistent_using_dac;
173182

174-
if (!dma_set_mask(dev, DMA_BIT_MASK(64))) {
183+
if (!dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64))) {
175184
using_dac = 1;
176185
consistent_using_dac = 1;
177-
dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
178-
} else if (!dma_set_mask(dev, DMA_BIT_MASK(32))) {
186+
} else if (!dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32))) {
179187
using_dac = 0;
180188
consistent_using_dac = 0;
181-
dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
182189
} else {
183190
printk(KERN_WARNING
184191
"mydev: No suitable DMA available.\n");
185192
goto ignore_this_device;
186193
}
187194

188-
dma_set_coherent_mask() will always be able to set the same or a
189-
smaller mask as dma_set_mask(). However for the rare case that a
195+
The coherent coherent mask will always be able to set the same or a
196+
smaller mask as the streaming mask. However for the rare case that a
190197
device driver only uses consistent allocations, one would have to
191198
check the return value from dma_set_coherent_mask().
192199

@@ -199,9 +206,9 @@ address you might do something like:
199206
goto ignore_this_device;
200207
}
201208

202-
When dma_set_mask() is successful, and returns zero, the kernel saves
203-
away this mask you have provided. The kernel will use this
204-
information later when you make DMA mappings.
209+
When dma_set_mask() or dma_set_mask_and_coherent() is successful, and
210+
returns zero, the kernel saves away this mask you have provided. The
211+
kernel will use this information later when you make DMA mappings.
205212

206213
There is a case which we are aware of at this time, which is worth
207214
mentioning in this documentation. If your device supports multiple

‎Documentation/DMA-API.txt‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ won't change the current mask settings. It is more intended as an
141141
internal API for use by the platform than an external API for use by
142142
driver writers.
143143

144+
int
145+
dma_set_mask_and_coherent(struct device *dev, u64 mask)
146+
147+
Checks to see if the mask is possible and updates the device
148+
streaming and coherent DMA mask parameters if it is.
149+
150+
Returns: 0 if successful and a negative error if not.
151+
144152
int
145153
dma_set_mask(struct device *dev, u64 mask)
146154

‎arch/arm/include/asm/dma-mapping.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
6464
{
6565
return (dma_addr_t)__virt_to_bus((unsigned long)(addr));
6666
}
67+
6768
#else
6869
static inline dma_addr_t pfn_to_dma(struct device *dev, unsigned long pfn)
6970
{
@@ -86,6 +87,13 @@ static inline dma_addr_t virt_to_dma(struct device *dev, void *addr)
8687
}
8788
#endif
8889

90+
/* The ARM override for dma_max_pfn() */
91+
static inline unsigned long dma_max_pfn(struct device *dev)
92+
{
93+
return PHYS_PFN_OFFSET + dma_to_pfn(dev, *dev->dma_mask);
94+
}
95+
#define dma_max_pfn(dev) dma_max_pfn(dev)
96+
8997
/*
9098
* DMA errors are defined by all-bits-set in the DMA address.
9199
*/

‎arch/arm/mm/dma-mapping.c‎

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ EXPORT_SYMBOL(arm_coherent_dma_ops);
159159

160160
static u64 get_coherent_dma_mask(struct device *dev)
161161
{
162-
u64 mask = (u64)arm_dma_limit;
162+
u64 mask = (u64)DMA_BIT_MASK(32);
163163

164164
if (dev) {
165165
mask = dev->coherent_dma_mask;
@@ -173,10 +173,30 @@ static u64 get_coherent_dma_mask(struct device *dev)
173173
return 0;
174174
}
175175

176-
if ((~mask) & (u64)arm_dma_limit) {
177-
dev_warn(dev, "coherent DMA mask %#llx is smaller "
178-
"than system GFP_DMA mask %#llx\n",
179-
mask, (u64)arm_dma_limit);
176+
/*
177+
* If the mask allows for more memory than we can address,
178+
* and we actually have that much memory, then fail the
179+
* allocation.
180+
*/
181+
if (sizeof(mask) != sizeof(dma_addr_t) &&
182+
mask > (dma_addr_t)~0 &&
183+
dma_to_pfn(dev, ~0) > arm_dma_pfn_limit) {
184+
dev_warn(dev, "Coherent DMA mask %#llx is larger than dma_addr_t allows\n",
185+
mask);
186+
dev_warn(dev, "Driver did not use or check the return value from dma_set_coherent_mask()?\n");
187+
return 0;
188+
}
189+
190+
/*
191+
* Now check that the mask, when translated to a PFN,
192+
* fits within the allowable addresses which we can
193+
* allocate.
194+
*/
195+
if (dma_to_pfn(dev, mask) < arm_dma_pfn_limit) {
196+
dev_warn(dev, "Coherent DMA mask %#llx (pfn %#lx-%#lx) covers a smaller range of system memory than the DMA zone pfn 0x0-%#lx\n",
197+
mask,
198+
dma_to_pfn(dev, 0), dma_to_pfn(dev, mask) + 1,
199+
arm_dma_pfn_limit + 1);
180200
return 0;
181201
}
182202
}
@@ -1007,8 +1027,27 @@ void arm_dma_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
10071027
*/
10081028
int dma_supported(struct device *dev, u64 mask)
10091029
{
1010-
if (mask < (u64)arm_dma_limit)
1030+
unsigned long limit;
1031+
1032+
/*
1033+
* If the mask allows for more memory than we can address,
1034+
* and we actually have that much memory, then we must
1035+
* indicate that DMA to this device is not supported.
1036+
*/
1037+
if (sizeof(mask) != sizeof(dma_addr_t) &&
1038+
mask > (dma_addr_t)~0 &&
1039+
dma_to_pfn(dev, ~0) > arm_dma_pfn_limit)
1040+
return 0;
1041+
1042+
/*
1043+
* Translate the device's DMA mask to a PFN limit. This
1044+
* PFN number includes the page which we can DMA to.
1045+
*/
1046+
limit = dma_to_pfn(dev, mask);
1047+
1048+
if (limit < arm_dma_pfn_limit)
10111049
return 0;
1050+
10121051
return 1;
10131052
}
10141053
EXPORT_SYMBOL(dma_supported);

‎arch/arm/mm/init.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ EXPORT_SYMBOL(arm_dma_zone_size);
209209
* so a successful GFP_DMA allocation will always satisfy this.
210210
*/
211211
phys_addr_t arm_dma_limit;
212+
unsigned long arm_dma_pfn_limit;
212213

213214
static void __init arm_adjust_dma_zone(unsigned long *size, unsigned long *hole,
214215
unsigned long dma_size)
@@ -231,6 +232,7 @@ void __init setup_dma_zone(const struct machine_desc *mdesc)
231232
arm_dma_limit = PHYS_OFFSET + arm_dma_zone_size - 1;
232233
} else
233234
arm_dma_limit = 0xffffffff;
235+
arm_dma_pfn_limit = arm_dma_limit >> PAGE_SHIFT;
234236
#endif
235237
}
236238

@@ -418,12 +420,10 @@ void __init bootmem_init(void)
418420
* This doesn't seem to be used by the Linux memory manager any
419421
* more, but is used by ll_rw_block. If we can get rid of it, we
420422
* also get rid of some of the stuff above as well.
421-
*
422-
* Note: max_low_pfn and max_pfn reflect the number of _pages_ in
423-
* the system, not the maximum PFN.
424423
*/
425-
max_low_pfn = max_low - PHYS_PFN_OFFSET;
426-
max_pfn = max_high - PHYS_PFN_OFFSET;
424+
min_low_pfn = min;
425+
max_low_pfn = max_low;
426+
max_pfn = max_high;
427427
}
428428

429429
/*
@@ -529,7 +529,7 @@ static inline void free_area_high(unsigned long pfn, unsigned long end)
529529
static void __init free_highpages(void)
530530
{
531531
#ifdef CONFIG_HIGHMEM
532-
unsigned long max_low = max_low_pfn + PHYS_PFN_OFFSET;
532+
unsigned long max_low = max_low_pfn;
533533
struct memblock_region *mem, *res;
534534

535535
/* set highmem page free */

‎arch/arm/mm/mm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,10 @@ extern __init void add_static_vm_early(struct static_vm *svm);
8181

8282
#ifdef CONFIG_ZONE_DMA
8383
extern phys_addr_t arm_dma_limit;
84+
extern unsigned long arm_dma_pfn_limit;
8485
#else
8586
#define arm_dma_limit ((phys_addr_t)~0)
87+
#define arm_dma_pfn_limit (~0ul >> PAGE_SHIFT)
8688
#endif
8789

8890
extern phys_addr_t arm_lowmem_limit;

‎arch/powerpc/kernel/vio.c‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,8 +1419,7 @@ struct vio_dev *vio_register_device_node(struct device_node *of_node)
14191419

14201420
/* needed to ensure proper operation of coherent allocations
14211421
* later, in case driver doesn't set it explicitly */
1422-
dma_set_mask(&viodev->dev, DMA_BIT_MASK(64));
1423-
dma_set_coherent_mask(&viodev->dev, DMA_BIT_MASK(64));
1422+
dma_set_mask_and_coherent(&viodev->dev, DMA_BIT_MASK(64));
14241423
}
14251424

14261425
/* register with generic device framework */

‎block/blk-settings.c‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,17 @@ EXPORT_SYMBOL(blk_queue_make_request);
195195
/**
196196
* blk_queue_bounce_limit - set bounce buffer limit for queue
197197
* @q: the request queue for the device
198-
* @dma_mask: the maximum address the device can handle
198+
* @max_addr: the maximum address the device can handle
199199
*
200200
* Description:
201201
* Different hardware can have different requirements as to what pages
202202
* it can do I/O directly to. A low level driver can call
203203
* blk_queue_bounce_limit to have lower memory pages allocated as bounce
204-
* buffers for doing I/O to pages residing above @dma_mask.
204+
* buffers for doing I/O to pages residing above @max_addr.
205205
**/
206-
void blk_queue_bounce_limit(struct request_queue *q, u64 dma_mask)
206+
void blk_queue_bounce_limit(struct request_queue *q, u64 max_addr)
207207
{
208-
unsigned long b_pfn = dma_mask >> PAGE_SHIFT;
208+
unsigned long b_pfn = max_addr >> PAGE_SHIFT;
209209
int dma = 0;
210210

211211
q->bounce_gfp = GFP_NOIO;

‎drivers/amba/bus.c‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,6 @@ amba_aphb_device_add(struct device *parent, const char *name,
552552
if (!dev)
553553
return ERR_PTR(-ENOMEM);
554554

555-
dev->dma_mask = dma_mask;
556555
dev->dev.coherent_dma_mask = dma_mask;
557556
dev->irq[0] = irq1;
558557
dev->irq[1] = irq2;
@@ -619,7 +618,7 @@ static void amba_device_initialize(struct amba_device *dev, const char *name)
619618
dev_set_name(&dev->dev, "%s", name);
620619
dev->dev.release = amba_device_release;
621620
dev->dev.bus = &amba_bustype;
622-
dev->dev.dma_mask = &dev->dma_mask;
621+
dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
623622
dev->res.name = dev_name(&dev->dev);
624623
}
625624

@@ -663,9 +662,6 @@ int amba_device_register(struct amba_device *dev, struct resource *parent)
663662
amba_device_initialize(dev, dev->dev.init_name);
664663
dev->dev.init_name = NULL;
665664

666-
if (!dev->dev.coherent_dma_mask && dev->dma_mask)
667-
dev_warn(&dev->dev, "coherent dma mask is unset\n");
668-
669665
return amba_device_add(dev, parent);
670666
}
671667

‎drivers/ata/pata_ixp4xx_cf.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ static int ixp4xx_pata_probe(struct platform_device *pdev)
144144
struct ata_host *host;
145145
struct ata_port *ap;
146146
struct ixp4xx_pata_data *data = dev_get_platdata(&pdev->dev);
147+
int ret;
147148

148149
cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149150
cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
@@ -157,7 +158,9 @@ static int ixp4xx_pata_probe(struct platform_device *pdev)
157158
return -ENOMEM;
158159

159160
/* acquire resources and fill host */
160-
pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
161+
ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
162+
if (ret)
163+
return ret;
161164

162165
data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000);
163166
data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000);

0 commit comments

Comments
 (0)