Skip to content

Commit 1956a96

Browse files
Alexis Bruemmertorvalds
authored andcommitted
x86 calgary: fix handling of devices that aren't behind the Calgary
The calgary code can give drivers addresses above 4GB which is very bad for hardware that is only 32bit DMA addressable. With this patch, the calgary code sets the global dma_ops to swiotlb or nommu properly, and the dma_ops of devices behind the Calgary/CalIOC2 to calgary_dma_ops. So the calgary code can handle devices safely that aren't behind the Calgary/CalIOC2. [akpm@linux-foundation.org: coding-style fixes] Signed-off-by: Alexis Bruemmer <alexisb@us.ibm.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> Cc: Muli Ben-Yehuda <muli@il.ibm.com> Cc: Ingo Molnar <mingo@elte.hu> Cc: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 8d8bb39 commit 1956a96

2 files changed

Lines changed: 27 additions & 45 deletions

File tree

‎arch/x86/kernel/pci-calgary_64.c‎

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <linux/delay.h>
3838
#include <linux/scatterlist.h>
3939
#include <linux/iommu-helper.h>
40+
4041
#include <asm/iommu.h>
4142
#include <asm/calgary.h>
4243
#include <asm/tce.h>
@@ -413,22 +414,6 @@ static void calgary_unmap_sg(struct device *dev,
413414
}
414415
}
415416

416-
static int calgary_nontranslate_map_sg(struct device* dev,
417-
struct scatterlist *sg, int nelems, int direction)
418-
{
419-
struct scatterlist *s;
420-
int i;
421-
422-
for_each_sg(sg, s, nelems, i) {
423-
struct page *p = sg_page(s);
424-
425-
BUG_ON(!p);
426-
s->dma_address = virt_to_bus(sg_virt(s));
427-
s->dma_length = s->length;
428-
}
429-
return nelems;
430-
}
431-
432417
static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
433418
int nelems, int direction)
434419
{
@@ -439,9 +424,6 @@ static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
439424
unsigned long entry;
440425
int i;
441426

442-
if (!translation_enabled(tbl))
443-
return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
444-
445427
for_each_sg(sg, s, nelems, i) {
446428
BUG_ON(!sg_page(s));
447429

@@ -477,7 +459,6 @@ static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
477459
static dma_addr_t calgary_map_single(struct device *dev, phys_addr_t paddr,
478460
size_t size, int direction)
479461
{
480-
dma_addr_t dma_handle = bad_dma_address;
481462
void *vaddr = phys_to_virt(paddr);
482463
unsigned long uaddr;
483464
unsigned int npages;
@@ -486,12 +467,7 @@ static dma_addr_t calgary_map_single(struct device *dev, phys_addr_t paddr,
486467
uaddr = (unsigned long)vaddr;
487468
npages = num_dma_pages(uaddr, size);
488469

489-
if (translation_enabled(tbl))
490-
dma_handle = iommu_alloc(dev, tbl, vaddr, npages, direction);
491-
else
492-
dma_handle = virt_to_bus(vaddr);
493-
494-
return dma_handle;
470+
return iommu_alloc(dev, tbl, vaddr, npages, direction);
495471
}
496472

497473
static void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
@@ -500,9 +476,6 @@ static void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
500476
struct iommu_table *tbl = find_iommu_table(dev);
501477
unsigned int npages;
502478

503-
if (!translation_enabled(tbl))
504-
return;
505-
506479
npages = num_dma_pages(dma_handle, size);
507480
iommu_free(tbl, dma_handle, npages);
508481
}
@@ -525,18 +498,12 @@ static void* calgary_alloc_coherent(struct device *dev, size_t size,
525498
goto error;
526499
memset(ret, 0, size);
527500

528-
if (translation_enabled(tbl)) {
529-
/* set up tces to cover the allocated range */
530-
mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL);
531-
if (mapping == bad_dma_address)
532-
goto free;
533-
534-
*dma_handle = mapping;
535-
} else /* non translated slot */
536-
*dma_handle = virt_to_bus(ret);
537-
501+
/* set up tces to cover the allocated range */
502+
mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL);
503+
if (mapping == bad_dma_address)
504+
goto free;
505+
*dma_handle = mapping;
538506
return ret;
539-
540507
free:
541508
free_pages((unsigned long)ret, get_order(size));
542509
ret = NULL;
@@ -1241,6 +1208,16 @@ static int __init calgary_init(void)
12411208
goto error;
12421209
} while (1);
12431210

1211+
dev = NULL;
1212+
for_each_pci_dev(dev) {
1213+
struct iommu_table *tbl;
1214+
1215+
tbl = find_iommu_table(&dev->dev);
1216+
1217+
if (translation_enabled(tbl))
1218+
dev->dev.archdata.dma_ops = &calgary_dma_ops;
1219+
}
1220+
12441221
return ret;
12451222

12461223
error:
@@ -1262,6 +1239,7 @@ static int __init calgary_init(void)
12621239
calgary_disable_translation(dev);
12631240
calgary_free_bus(dev);
12641241
pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
1242+
dev->dev.archdata.dma_ops = NULL;
12651243
} while (1);
12661244

12671245
return ret;
@@ -1503,6 +1481,10 @@ void __init detect_calgary(void)
15031481
printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
15041482
"CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
15051483
debugging ? "enabled" : "disabled");
1484+
1485+
/* swiotlb for devices that aren't behind the Calgary. */
1486+
if (max_pfn > MAX_DMA32_PFN)
1487+
swiotlb = 1;
15061488
}
15071489
return;
15081490

@@ -1519,7 +1501,7 @@ int __init calgary_iommu_init(void)
15191501
{
15201502
int ret;
15211503

1522-
if (no_iommu || swiotlb)
1504+
if (no_iommu || (swiotlb && !calgary_detected))
15231505
return -ENODEV;
15241506

15251507
if (!calgary_detected)
@@ -1532,15 +1514,14 @@ int __init calgary_iommu_init(void)
15321514
if (ret) {
15331515
printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
15341516
"falling back to no_iommu\n", ret);
1535-
if (max_pfn > MAX_DMA32_PFN)
1536-
printk(KERN_ERR "WARNING more than 4GB of memory, "
1537-
"32bit PCI may malfunction.\n");
15381517
return ret;
15391518
}
15401519

15411520
force_iommu = 1;
15421521
bad_dma_address = 0x0;
1543-
dma_ops = &calgary_dma_ops;
1522+
/* dma_ops is set to swiotlb or nommu */
1523+
if (!dma_ops)
1524+
dma_ops = &nommu_dma_ops;
15441525

15451526
return 0;
15461527
}

‎include/asm-x86/iommu.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
extern void pci_iommu_shutdown(void);
55
extern void no_iommu_init(void);
6+
extern struct dma_mapping_ops nommu_dma_ops;
67
extern int force_iommu, no_iommu;
78
extern int iommu_detected;
89

0 commit comments

Comments
 (0)