2 * Derived from arch/powerpc/kernel/iommu.c
4 * Copyright IBM Corporation, 2006-2007
5 * Copyright (C) 2006 Jon Mason <jdmason@kudzu.us>
7 * Author: Jon Mason <jdmason@kudzu.us>
8 * Author: Muli Ben-Yehuda <muli@il.ibm.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
30 #include <linux/spinlock.h>
31 #include <linux/string.h>
32 #include <linux/crash_dump.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/bitops.h>
35 #include <linux/pci_ids.h>
36 #include <linux/pci.h>
37 #include <linux/delay.h>
38 #include <linux/scatterlist.h>
39 #include <linux/iommu-helper.h>
41 #include <asm/iommu.h>
42 #include <asm/calgary.h>
44 #include <asm/pci-direct.h>
45 #include <asm/system.h>
48 #include <asm/bios_ebda.h>
50 #ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
51 int use_calgary __read_mostly
= 1;
53 int use_calgary __read_mostly
= 0;
54 #endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
56 #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
57 #define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
59 /* register offsets inside the host bridge space */
60 #define CALGARY_CONFIG_REG 0x0108
61 #define PHB_CSR_OFFSET 0x0110 /* Channel Status */
62 #define PHB_PLSSR_OFFSET 0x0120
63 #define PHB_CONFIG_RW_OFFSET 0x0160
64 #define PHB_IOBASE_BAR_LOW 0x0170
65 #define PHB_IOBASE_BAR_HIGH 0x0180
66 #define PHB_MEM_1_LOW 0x0190
67 #define PHB_MEM_1_HIGH 0x01A0
68 #define PHB_IO_ADDR_SIZE 0x01B0
69 #define PHB_MEM_1_SIZE 0x01C0
70 #define PHB_MEM_ST_OFFSET 0x01D0
71 #define PHB_AER_OFFSET 0x0200
72 #define PHB_CONFIG_0_HIGH 0x0220
73 #define PHB_CONFIG_0_LOW 0x0230
74 #define PHB_CONFIG_0_END 0x0240
75 #define PHB_MEM_2_LOW 0x02B0
76 #define PHB_MEM_2_HIGH 0x02C0
77 #define PHB_MEM_2_SIZE_HIGH 0x02D0
78 #define PHB_MEM_2_SIZE_LOW 0x02E0
79 #define PHB_DOSHOLE_OFFSET 0x08E0
81 /* CalIOC2 specific */
82 #define PHB_SAVIOR_L2 0x0DB0
83 #define PHB_PAGE_MIG_CTRL 0x0DA8
84 #define PHB_PAGE_MIG_DEBUG 0x0DA0
85 #define PHB_ROOT_COMPLEX_STATUS 0x0CB0
88 #define PHB_TCE_ENABLE 0x20000000
89 #define PHB_SLOT_DISABLE 0x1C000000
90 #define PHB_DAC_DISABLE 0x01000000
91 #define PHB_MEM2_ENABLE 0x00400000
92 #define PHB_MCSR_ENABLE 0x00100000
93 /* TAR (Table Address Register) */
94 #define TAR_SW_BITS 0x0000ffffffff800fUL
95 #define TAR_VALID 0x0000000000000008UL
96 /* CSR (Channel/DMA Status Register) */
97 #define CSR_AGENT_MASK 0xffe0ffff
98 /* CCR (Calgary Configuration Register) */
99 #define CCR_2SEC_TIMEOUT 0x000000000000000EUL
100 /* PMCR/PMDR (Page Migration Control/Debug Registers */
101 #define PMR_SOFTSTOP 0x80000000
102 #define PMR_SOFTSTOPFAULT 0x40000000
103 #define PMR_HARDSTOP 0x20000000
105 #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
106 #define MAX_NUM_CHASSIS 8 /* max number of chassis */
107 /* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
108 #define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
109 #define PHBS_PER_CALGARY 4
111 /* register offsets in Calgary's internal register space */
112 static const unsigned long tar_offsets
[] = {
119 static const unsigned long split_queue_offsets
[] = {
120 0x4870 /* SPLIT QUEUE 0 */,
121 0x5870 /* SPLIT QUEUE 1 */,
122 0x6870 /* SPLIT QUEUE 2 */,
123 0x7870 /* SPLIT QUEUE 3 */
126 static const unsigned long phb_offsets
[] = {
133 /* PHB debug registers */
135 static const unsigned long phb_debug_offsets
[] = {
136 0x4000 /* PHB 0 DEBUG */,
137 0x5000 /* PHB 1 DEBUG */,
138 0x6000 /* PHB 2 DEBUG */,
139 0x7000 /* PHB 3 DEBUG */
143 * STUFF register for each debug PHB,
144 * byte 1 = start bus number, byte 2 = end bus number
147 #define PHB_DEBUG_STUFF_OFFSET 0x0020
149 #define EMERGENCY_PAGES 32 /* = 128KB */
151 unsigned int specified_table_size
= TCE_TABLE_SIZE_UNSPECIFIED
;
152 static int translate_empty_slots __read_mostly
= 0;
153 static int calgary_detected __read_mostly
= 0;
155 static struct rio_table_hdr
*rio_table_hdr __initdata
;
156 static struct scal_detail
*scal_devs
[MAX_NUMNODES
] __initdata
;
157 static struct rio_detail
*rio_devs
[MAX_NUMNODES
* 4] __initdata
;
159 struct calgary_bus_info
{
161 unsigned char translation_disabled
;
166 static void calgary_handle_quirks(struct iommu_table
*tbl
, struct pci_dev
*dev
);
167 static void calgary_tce_cache_blast(struct iommu_table
*tbl
);
168 static void calgary_dump_error_regs(struct iommu_table
*tbl
);
169 static void calioc2_handle_quirks(struct iommu_table
*tbl
, struct pci_dev
*dev
);
170 static void calioc2_tce_cache_blast(struct iommu_table
*tbl
);
171 static void calioc2_dump_error_regs(struct iommu_table
*tbl
);
172 static void calgary_init_bitmap_from_tce_table(struct iommu_table
*tbl
);
173 static void get_tce_space_from_tar(void);
175 static struct cal_chipset_ops calgary_chip_ops
= {
176 .handle_quirks
= calgary_handle_quirks
,
177 .tce_cache_blast
= calgary_tce_cache_blast
,
178 .dump_error_regs
= calgary_dump_error_regs
181 static struct cal_chipset_ops calioc2_chip_ops
= {
182 .handle_quirks
= calioc2_handle_quirks
,
183 .tce_cache_blast
= calioc2_tce_cache_blast
,
184 .dump_error_regs
= calioc2_dump_error_regs
187 static struct calgary_bus_info bus_info
[MAX_PHB_BUS_NUM
] = { { NULL
, 0, 0 }, };
189 static inline int translation_enabled(struct iommu_table
*tbl
)
191 /* only PHBs with translation enabled have an IOMMU table */
192 return (tbl
!= NULL
);
195 static void iommu_range_reserve(struct iommu_table
*tbl
,
196 unsigned long start_addr
, unsigned int npages
)
202 index
= start_addr
>> PAGE_SHIFT
;
204 /* bail out if we're asked to reserve a region we don't cover */
205 if (index
>= tbl
->it_size
)
208 end
= index
+ npages
;
209 if (end
> tbl
->it_size
) /* don't go off the table */
212 spin_lock_irqsave(&tbl
->it_lock
, flags
);
214 iommu_area_reserve(tbl
->it_map
, index
, npages
);
216 spin_unlock_irqrestore(&tbl
->it_lock
, flags
);
219 static unsigned long iommu_range_alloc(struct device
*dev
,
220 struct iommu_table
*tbl
,
224 unsigned long offset
;
225 unsigned long boundary_size
;
227 boundary_size
= ALIGN(dma_get_seg_boundary(dev
) + 1,
228 PAGE_SIZE
) >> PAGE_SHIFT
;
232 spin_lock_irqsave(&tbl
->it_lock
, flags
);
234 offset
= iommu_area_alloc(tbl
->it_map
, tbl
->it_size
, tbl
->it_hint
,
235 npages
, 0, boundary_size
, 0);
236 if (offset
== ~0UL) {
237 tbl
->chip_ops
->tce_cache_blast(tbl
);
239 offset
= iommu_area_alloc(tbl
->it_map
, tbl
->it_size
, 0,
240 npages
, 0, boundary_size
, 0);
241 if (offset
== ~0UL) {
242 printk(KERN_WARNING
"Calgary: IOMMU full.\n");
243 spin_unlock_irqrestore(&tbl
->it_lock
, flags
);
244 if (panic_on_overflow
)
245 panic("Calgary: fix the allocator.\n");
247 return bad_dma_address
;
251 tbl
->it_hint
= offset
+ npages
;
252 BUG_ON(tbl
->it_hint
> tbl
->it_size
);
254 spin_unlock_irqrestore(&tbl
->it_lock
, flags
);
259 static dma_addr_t
iommu_alloc(struct device
*dev
, struct iommu_table
*tbl
,
260 void *vaddr
, unsigned int npages
, int direction
)
263 dma_addr_t ret
= bad_dma_address
;
265 entry
= iommu_range_alloc(dev
, tbl
, npages
);
267 if (unlikely(entry
== bad_dma_address
))
270 /* set the return dma address */
271 ret
= (entry
<< PAGE_SHIFT
) | ((unsigned long)vaddr
& ~PAGE_MASK
);
273 /* put the TCEs in the HW table */
274 tce_build(tbl
, entry
, npages
, (unsigned long)vaddr
& PAGE_MASK
,
280 printk(KERN_WARNING
"Calgary: failed to allocate %u pages in "
281 "iommu %p\n", npages
, tbl
);
282 return bad_dma_address
;
285 static void iommu_free(struct iommu_table
*tbl
, dma_addr_t dma_addr
,
289 unsigned long badend
;
292 /* were we called with bad_dma_address? */
293 badend
= bad_dma_address
+ (EMERGENCY_PAGES
* PAGE_SIZE
);
294 if (unlikely((dma_addr
>= bad_dma_address
) && (dma_addr
< badend
))) {
295 WARN(1, KERN_ERR
"Calgary: driver tried unmapping bad DMA "
296 "address 0x%Lx\n", dma_addr
);
300 entry
= dma_addr
>> PAGE_SHIFT
;
302 BUG_ON(entry
+ npages
> tbl
->it_size
);
304 tce_free(tbl
, entry
, npages
);
306 spin_lock_irqsave(&tbl
->it_lock
, flags
);
308 iommu_area_free(tbl
->it_map
, entry
, npages
);
310 spin_unlock_irqrestore(&tbl
->it_lock
, flags
);
313 static inline struct iommu_table
*find_iommu_table(struct device
*dev
)
315 struct pci_dev
*pdev
;
316 struct pci_bus
*pbus
;
317 struct iommu_table
*tbl
;
319 pdev
= to_pci_dev(dev
);
321 /* search up the device tree for an iommu */
324 tbl
= pci_iommu(pbus
);
325 if (tbl
&& tbl
->it_busno
== pbus
->number
)
331 BUG_ON(tbl
&& (tbl
->it_busno
!= pbus
->number
));
336 static void calgary_unmap_sg(struct device
*dev
, struct scatterlist
*sglist
,
337 int nelems
,enum dma_data_direction dir
,
338 struct dma_attrs
*attrs
)
340 struct iommu_table
*tbl
= find_iommu_table(dev
);
341 struct scatterlist
*s
;
344 if (!translation_enabled(tbl
))
347 for_each_sg(sglist
, s
, nelems
, i
) {
349 dma_addr_t dma
= s
->dma_address
;
350 unsigned int dmalen
= s
->dma_length
;
355 npages
= iommu_num_pages(dma
, dmalen
, PAGE_SIZE
);
356 iommu_free(tbl
, dma
, npages
);
360 static int calgary_map_sg(struct device
*dev
, struct scatterlist
*sg
,
361 int nelems
, enum dma_data_direction dir
,
362 struct dma_attrs
*attrs
)
364 struct iommu_table
*tbl
= find_iommu_table(dev
);
365 struct scatterlist
*s
;
371 for_each_sg(sg
, s
, nelems
, i
) {
374 vaddr
= (unsigned long) sg_virt(s
);
375 npages
= iommu_num_pages(vaddr
, s
->length
, PAGE_SIZE
);
377 entry
= iommu_range_alloc(dev
, tbl
, npages
);
378 if (entry
== bad_dma_address
) {
379 /* makes sure unmap knows to stop */
384 s
->dma_address
= (entry
<< PAGE_SHIFT
) | s
->offset
;
386 /* insert into HW table */
387 tce_build(tbl
, entry
, npages
, vaddr
& PAGE_MASK
, dir
);
389 s
->dma_length
= s
->length
;
394 calgary_unmap_sg(dev
, sg
, nelems
, dir
, NULL
);
395 for_each_sg(sg
, s
, nelems
, i
) {
396 sg
->dma_address
= bad_dma_address
;
402 static dma_addr_t
calgary_map_page(struct device
*dev
, struct page
*page
,
403 unsigned long offset
, size_t size
,
404 enum dma_data_direction dir
,
405 struct dma_attrs
*attrs
)
407 void *vaddr
= page_address(page
) + offset
;
410 struct iommu_table
*tbl
= find_iommu_table(dev
);
412 uaddr
= (unsigned long)vaddr
;
413 npages
= iommu_num_pages(uaddr
, size
, PAGE_SIZE
);
415 return iommu_alloc(dev
, tbl
, vaddr
, npages
, dir
);
418 static void calgary_unmap_page(struct device
*dev
, dma_addr_t dma_addr
,
419 size_t size
, enum dma_data_direction dir
,
420 struct dma_attrs
*attrs
)
422 struct iommu_table
*tbl
= find_iommu_table(dev
);
425 npages
= iommu_num_pages(dma_addr
, size
, PAGE_SIZE
);
426 iommu_free(tbl
, dma_addr
, npages
);
429 static void* calgary_alloc_coherent(struct device
*dev
, size_t size
,
430 dma_addr_t
*dma_handle
, gfp_t flag
)
434 unsigned int npages
, order
;
435 struct iommu_table
*tbl
= find_iommu_table(dev
);
437 size
= PAGE_ALIGN(size
); /* size rounded up to full pages */
438 npages
= size
>> PAGE_SHIFT
;
439 order
= get_order(size
);
441 flag
&= ~(__GFP_DMA
| __GFP_HIGHMEM
| __GFP_DMA32
);
443 /* alloc enough pages (and possibly more) */
444 ret
= (void *)__get_free_pages(flag
, order
);
447 memset(ret
, 0, size
);
449 /* set up tces to cover the allocated range */
450 mapping
= iommu_alloc(dev
, tbl
, ret
, npages
, DMA_BIDIRECTIONAL
);
451 if (mapping
== bad_dma_address
)
453 *dma_handle
= mapping
;
456 free_pages((unsigned long)ret
, get_order(size
));
462 static void calgary_free_coherent(struct device
*dev
, size_t size
,
463 void *vaddr
, dma_addr_t dma_handle
)
466 struct iommu_table
*tbl
= find_iommu_table(dev
);
468 size
= PAGE_ALIGN(size
);
469 npages
= size
>> PAGE_SHIFT
;
471 iommu_free(tbl
, dma_handle
, npages
);
472 free_pages((unsigned long)vaddr
, get_order(size
));
475 static struct dma_map_ops calgary_dma_ops
= {
476 .alloc_coherent
= calgary_alloc_coherent
,
477 .free_coherent
= calgary_free_coherent
,
478 .map_sg
= calgary_map_sg
,
479 .unmap_sg
= calgary_unmap_sg
,
480 .map_page
= calgary_map_page
,
481 .unmap_page
= calgary_unmap_page
,
484 static inline void __iomem
* busno_to_bbar(unsigned char num
)
486 return bus_info
[num
].bbar
;
489 static inline int busno_to_phbid(unsigned char num
)
491 return bus_info
[num
].phbid
;
494 static inline unsigned long split_queue_offset(unsigned char num
)
496 size_t idx
= busno_to_phbid(num
);
498 return split_queue_offsets
[idx
];
501 static inline unsigned long tar_offset(unsigned char num
)
503 size_t idx
= busno_to_phbid(num
);
505 return tar_offsets
[idx
];
508 static inline unsigned long phb_offset(unsigned char num
)
510 size_t idx
= busno_to_phbid(num
);
512 return phb_offsets
[idx
];
515 static inline void __iomem
* calgary_reg(void __iomem
*bar
, unsigned long offset
)
517 unsigned long target
= ((unsigned long)bar
) | offset
;
518 return (void __iomem
*)target
;
521 static inline int is_calioc2(unsigned short device
)
523 return (device
== PCI_DEVICE_ID_IBM_CALIOC2
);
526 static inline int is_calgary(unsigned short device
)
528 return (device
== PCI_DEVICE_ID_IBM_CALGARY
);
531 static inline int is_cal_pci_dev(unsigned short device
)
533 return (is_calgary(device
) || is_calioc2(device
));
536 static void calgary_tce_cache_blast(struct iommu_table
*tbl
)
541 void __iomem
*bbar
= tbl
->bbar
;
542 void __iomem
*target
;
544 /* disable arbitration on the bus */
545 target
= calgary_reg(bbar
, phb_offset(tbl
->it_busno
) | PHB_AER_OFFSET
);
549 /* read plssr to ensure it got there */
550 target
= calgary_reg(bbar
, phb_offset(tbl
->it_busno
) | PHB_PLSSR_OFFSET
);
553 /* poll split queues until all DMA activity is done */
554 target
= calgary_reg(bbar
, split_queue_offset(tbl
->it_busno
));
558 } while ((val
& 0xff) != 0xff && i
< 100);
560 printk(KERN_WARNING
"Calgary: PCI bus not quiesced, "
561 "continuing anyway\n");
563 /* invalidate TCE cache */
564 target
= calgary_reg(bbar
, tar_offset(tbl
->it_busno
));
565 writeq(tbl
->tar_val
, target
);
567 /* enable arbitration */
568 target
= calgary_reg(bbar
, phb_offset(tbl
->it_busno
) | PHB_AER_OFFSET
);
570 (void)readl(target
); /* flush */
573 static void calioc2_tce_cache_blast(struct iommu_table
*tbl
)
575 void __iomem
*bbar
= tbl
->bbar
;
576 void __iomem
*target
;
581 unsigned char bus
= tbl
->it_busno
;
584 printk(KERN_DEBUG
"Calgary: CalIOC2 bus 0x%x entering tce cache blast "
585 "sequence - count %d\n", bus
, count
);
587 /* 1. using the Page Migration Control reg set SoftStop */
588 target
= calgary_reg(bbar
, phb_offset(bus
) | PHB_PAGE_MIG_CTRL
);
589 val
= be32_to_cpu(readl(target
));
590 printk(KERN_DEBUG
"1a. read 0x%x [LE] from %p\n", val
, target
);
592 printk(KERN_DEBUG
"1b. writing 0x%x [LE] to %p\n", val
, target
);
593 writel(cpu_to_be32(val
), target
);
595 /* 2. poll split queues until all DMA activity is done */
596 printk(KERN_DEBUG
"2a. starting to poll split queues\n");
597 target
= calgary_reg(bbar
, split_queue_offset(bus
));
599 val64
= readq(target
);
601 } while ((val64
& 0xff) != 0xff && i
< 100);
603 printk(KERN_WARNING
"CalIOC2: PCI bus not quiesced, "
604 "continuing anyway\n");
606 /* 3. poll Page Migration DEBUG for SoftStopFault */
607 target
= calgary_reg(bbar
, phb_offset(bus
) | PHB_PAGE_MIG_DEBUG
);
608 val
= be32_to_cpu(readl(target
));
609 printk(KERN_DEBUG
"3. read 0x%x [LE] from %p\n", val
, target
);
611 /* 4. if SoftStopFault - goto (1) */
612 if (val
& PMR_SOFTSTOPFAULT
) {
616 printk(KERN_WARNING
"CalIOC2: too many SoftStopFaults, "
617 "aborting TCE cache flush sequence!\n");
618 return; /* pray for the best */
622 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
623 target
= calgary_reg(bbar
, phb_offset(bus
) | PHB_PAGE_MIG_CTRL
);
624 printk(KERN_DEBUG
"5a. slamming into HardStop by reading %p\n", target
);
625 val
= be32_to_cpu(readl(target
));
626 printk(KERN_DEBUG
"5b. read 0x%x [LE] from %p\n", val
, target
);
627 target
= calgary_reg(bbar
, phb_offset(bus
) | PHB_PAGE_MIG_DEBUG
);
628 val
= be32_to_cpu(readl(target
));
629 printk(KERN_DEBUG
"5c. read 0x%x [LE] from %p (debug)\n", val
, target
);
631 /* 6. invalidate TCE cache */
632 printk(KERN_DEBUG
"6. invalidating TCE cache\n");
633 target
= calgary_reg(bbar
, tar_offset(bus
));
634 writeq(tbl
->tar_val
, target
);
636 /* 7. Re-read PMCR */
637 printk(KERN_DEBUG
"7a. Re-reading PMCR\n");
638 target
= calgary_reg(bbar
, phb_offset(bus
) | PHB_PAGE_MIG_CTRL
);
639 val
= be32_to_cpu(readl(target
));
640 printk(KERN_DEBUG
"7b. read 0x%x [LE] from %p\n", val
, target
);
642 /* 8. Remove HardStop */
643 printk(KERN_DEBUG
"8a. removing HardStop from PMCR\n");
644 target
= calgary_reg(bbar
, phb_offset(bus
) | PHB_PAGE_MIG_CTRL
);
646 printk(KERN_DEBUG
"8b. writing 0x%x [LE] to %p\n", val
, target
);
647 writel(cpu_to_be32(val
), target
);
648 val
= be32_to_cpu(readl(target
));
649 printk(KERN_DEBUG
"8c. read 0x%x [LE] from %p\n", val
, target
);
652 static void __init
calgary_reserve_mem_region(struct pci_dev
*dev
, u64 start
,
655 unsigned int numpages
;
657 limit
= limit
| 0xfffff;
660 numpages
= ((limit
- start
) >> PAGE_SHIFT
);
661 iommu_range_reserve(pci_iommu(dev
->bus
), start
, numpages
);
664 static void __init
calgary_reserve_peripheral_mem_1(struct pci_dev
*dev
)
666 void __iomem
*target
;
667 u64 low
, high
, sizelow
;
669 struct iommu_table
*tbl
= pci_iommu(dev
->bus
);
670 unsigned char busnum
= dev
->bus
->number
;
671 void __iomem
*bbar
= tbl
->bbar
;
673 /* peripheral MEM_1 region */
674 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_MEM_1_LOW
);
675 low
= be32_to_cpu(readl(target
));
676 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_MEM_1_HIGH
);
677 high
= be32_to_cpu(readl(target
));
678 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_MEM_1_SIZE
);
679 sizelow
= be32_to_cpu(readl(target
));
681 start
= (high
<< 32) | low
;
684 calgary_reserve_mem_region(dev
, start
, limit
);
687 static void __init
calgary_reserve_peripheral_mem_2(struct pci_dev
*dev
)
689 void __iomem
*target
;
691 u64 low
, high
, sizelow
, sizehigh
;
693 struct iommu_table
*tbl
= pci_iommu(dev
->bus
);
694 unsigned char busnum
= dev
->bus
->number
;
695 void __iomem
*bbar
= tbl
->bbar
;
698 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_CONFIG_RW_OFFSET
);
699 val32
= be32_to_cpu(readl(target
));
700 if (!(val32
& PHB_MEM2_ENABLE
))
703 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_MEM_2_LOW
);
704 low
= be32_to_cpu(readl(target
));
705 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_MEM_2_HIGH
);
706 high
= be32_to_cpu(readl(target
));
707 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_MEM_2_SIZE_LOW
);
708 sizelow
= be32_to_cpu(readl(target
));
709 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_MEM_2_SIZE_HIGH
);
710 sizehigh
= be32_to_cpu(readl(target
));
712 start
= (high
<< 32) | low
;
713 limit
= (sizehigh
<< 32) | sizelow
;
715 calgary_reserve_mem_region(dev
, start
, limit
);
719 * some regions of the IO address space do not get translated, so we
720 * must not give devices IO addresses in those regions. The regions
721 * are the 640KB-1MB region and the two PCI peripheral memory holes.
722 * Reserve all of them in the IOMMU bitmap to avoid giving them out
725 static void __init
calgary_reserve_regions(struct pci_dev
*dev
)
729 struct iommu_table
*tbl
= pci_iommu(dev
->bus
);
731 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
732 iommu_range_reserve(tbl
, bad_dma_address
, EMERGENCY_PAGES
);
734 /* avoid the BIOS/VGA first 640KB-1MB region */
735 /* for CalIOC2 - avoid the entire first MB */
736 if (is_calgary(dev
->device
)) {
737 start
= (640 * 1024);
738 npages
= ((1024 - 640) * 1024) >> PAGE_SHIFT
;
739 } else { /* calioc2 */
741 npages
= (1 * 1024 * 1024) >> PAGE_SHIFT
;
743 iommu_range_reserve(tbl
, start
, npages
);
745 /* reserve the two PCI peripheral memory regions in IO space */
746 calgary_reserve_peripheral_mem_1(dev
);
747 calgary_reserve_peripheral_mem_2(dev
);
750 static int __init
calgary_setup_tar(struct pci_dev
*dev
, void __iomem
*bbar
)
754 void __iomem
*target
;
756 struct iommu_table
*tbl
;
758 /* build TCE tables for each PHB */
759 ret
= build_tce_table(dev
, bbar
);
763 tbl
= pci_iommu(dev
->bus
);
764 tbl
->it_base
= (unsigned long)bus_info
[dev
->bus
->number
].tce_space
;
766 if (is_kdump_kernel())
767 calgary_init_bitmap_from_tce_table(tbl
);
769 tce_free(tbl
, 0, tbl
->it_size
);
771 if (is_calgary(dev
->device
))
772 tbl
->chip_ops
= &calgary_chip_ops
;
773 else if (is_calioc2(dev
->device
))
774 tbl
->chip_ops
= &calioc2_chip_ops
;
778 calgary_reserve_regions(dev
);
780 /* set TARs for each PHB */
781 target
= calgary_reg(bbar
, tar_offset(dev
->bus
->number
));
782 val64
= be64_to_cpu(readq(target
));
784 /* zero out all TAR bits under sw control */
785 val64
&= ~TAR_SW_BITS
;
786 table_phys
= (u64
)__pa(tbl
->it_base
);
790 BUG_ON(specified_table_size
> TCE_TABLE_SIZE_8M
);
791 val64
|= (u64
) specified_table_size
;
793 tbl
->tar_val
= cpu_to_be64(val64
);
795 writeq(tbl
->tar_val
, target
);
796 readq(target
); /* flush */
801 static void __init
calgary_free_bus(struct pci_dev
*dev
)
804 struct iommu_table
*tbl
= pci_iommu(dev
->bus
);
805 void __iomem
*target
;
806 unsigned int bitmapsz
;
808 target
= calgary_reg(tbl
->bbar
, tar_offset(dev
->bus
->number
));
809 val64
= be64_to_cpu(readq(target
));
810 val64
&= ~TAR_SW_BITS
;
811 writeq(cpu_to_be64(val64
), target
);
812 readq(target
); /* flush */
814 bitmapsz
= tbl
->it_size
/ BITS_PER_BYTE
;
815 free_pages((unsigned long)tbl
->it_map
, get_order(bitmapsz
));
820 set_pci_iommu(dev
->bus
, NULL
);
822 /* Can't free bootmem allocated memory after system is up :-( */
823 bus_info
[dev
->bus
->number
].tce_space
= NULL
;
826 static void calgary_dump_error_regs(struct iommu_table
*tbl
)
828 void __iomem
*bbar
= tbl
->bbar
;
829 void __iomem
*target
;
832 target
= calgary_reg(bbar
, phb_offset(tbl
->it_busno
) | PHB_CSR_OFFSET
);
833 csr
= be32_to_cpu(readl(target
));
835 target
= calgary_reg(bbar
, phb_offset(tbl
->it_busno
) | PHB_PLSSR_OFFSET
);
836 plssr
= be32_to_cpu(readl(target
));
838 /* If no error, the agent ID in the CSR is not valid */
839 printk(KERN_EMERG
"Calgary: DMA error on Calgary PHB 0x%x, "
840 "0x%08x@CSR 0x%08x@PLSSR\n", tbl
->it_busno
, csr
, plssr
);
843 static void calioc2_dump_error_regs(struct iommu_table
*tbl
)
845 void __iomem
*bbar
= tbl
->bbar
;
846 u32 csr
, csmr
, plssr
, mck
, rcstat
;
847 void __iomem
*target
;
848 unsigned long phboff
= phb_offset(tbl
->it_busno
);
849 unsigned long erroff
;
854 target
= calgary_reg(bbar
, phboff
| PHB_CSR_OFFSET
);
855 csr
= be32_to_cpu(readl(target
));
857 target
= calgary_reg(bbar
, phboff
| PHB_PLSSR_OFFSET
);
858 plssr
= be32_to_cpu(readl(target
));
860 target
= calgary_reg(bbar
, phboff
| 0x290);
861 csmr
= be32_to_cpu(readl(target
));
863 target
= calgary_reg(bbar
, phboff
| 0x800);
864 mck
= be32_to_cpu(readl(target
));
866 printk(KERN_EMERG
"Calgary: DMA error on CalIOC2 PHB 0x%x\n",
869 printk(KERN_EMERG
"Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
870 csr
, plssr
, csmr
, mck
);
872 /* dump rest of error regs */
873 printk(KERN_EMERG
"Calgary: ");
874 for (i
= 0; i
< ARRAY_SIZE(errregs
); i
++) {
875 /* err regs are at 0x810 - 0x870 */
876 erroff
= (0x810 + (i
* 0x10));
877 target
= calgary_reg(bbar
, phboff
| erroff
);
878 errregs
[i
] = be32_to_cpu(readl(target
));
879 printk("0x%08x@0x%lx ", errregs
[i
], erroff
);
883 /* root complex status */
884 target
= calgary_reg(bbar
, phboff
| PHB_ROOT_COMPLEX_STATUS
);
885 rcstat
= be32_to_cpu(readl(target
));
886 printk(KERN_EMERG
"Calgary: 0x%08x@0x%x\n", rcstat
,
887 PHB_ROOT_COMPLEX_STATUS
);
890 static void calgary_watchdog(unsigned long data
)
892 struct pci_dev
*dev
= (struct pci_dev
*)data
;
893 struct iommu_table
*tbl
= pci_iommu(dev
->bus
);
894 void __iomem
*bbar
= tbl
->bbar
;
896 void __iomem
*target
;
898 target
= calgary_reg(bbar
, phb_offset(tbl
->it_busno
) | PHB_CSR_OFFSET
);
899 val32
= be32_to_cpu(readl(target
));
901 /* If no error, the agent ID in the CSR is not valid */
902 if (val32
& CSR_AGENT_MASK
) {
903 tbl
->chip_ops
->dump_error_regs(tbl
);
908 /* Disable bus that caused the error */
909 target
= calgary_reg(bbar
, phb_offset(tbl
->it_busno
) |
910 PHB_CONFIG_RW_OFFSET
);
911 val32
= be32_to_cpu(readl(target
));
912 val32
|= PHB_SLOT_DISABLE
;
913 writel(cpu_to_be32(val32
), target
);
914 readl(target
); /* flush */
916 /* Reset the timer */
917 mod_timer(&tbl
->watchdog_timer
, jiffies
+ 2 * HZ
);
921 static void __init
calgary_set_split_completion_timeout(void __iomem
*bbar
,
922 unsigned char busnum
, unsigned long timeout
)
925 void __iomem
*target
;
926 unsigned int phb_shift
= ~0; /* silence gcc */
929 switch (busno_to_phbid(busnum
)) {
930 case 0: phb_shift
= (63 - 19);
932 case 1: phb_shift
= (63 - 23);
934 case 2: phb_shift
= (63 - 27);
936 case 3: phb_shift
= (63 - 35);
939 BUG_ON(busno_to_phbid(busnum
));
942 target
= calgary_reg(bbar
, CALGARY_CONFIG_REG
);
943 val64
= be64_to_cpu(readq(target
));
945 /* zero out this PHB's timer bits */
946 mask
= ~(0xFUL
<< phb_shift
);
948 val64
|= (timeout
<< phb_shift
);
949 writeq(cpu_to_be64(val64
), target
);
950 readq(target
); /* flush */
953 static void __init
calioc2_handle_quirks(struct iommu_table
*tbl
, struct pci_dev
*dev
)
955 unsigned char busnum
= dev
->bus
->number
;
956 void __iomem
*bbar
= tbl
->bbar
;
957 void __iomem
*target
;
961 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
963 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_SAVIOR_L2
);
964 val
= cpu_to_be32(readl(target
));
966 writel(cpu_to_be32(val
), target
);
969 static void __init
calgary_handle_quirks(struct iommu_table
*tbl
, struct pci_dev
*dev
)
971 unsigned char busnum
= dev
->bus
->number
;
974 * Give split completion a longer timeout on bus 1 for aic94xx
975 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
977 if (is_calgary(dev
->device
) && (busnum
== 1))
978 calgary_set_split_completion_timeout(tbl
->bbar
, busnum
,
982 static void __init
calgary_enable_translation(struct pci_dev
*dev
)
985 unsigned char busnum
;
986 void __iomem
*target
;
988 struct iommu_table
*tbl
;
990 busnum
= dev
->bus
->number
;
991 tbl
= pci_iommu(dev
->bus
);
994 /* enable TCE in PHB Config Register */
995 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_CONFIG_RW_OFFSET
);
996 val32
= be32_to_cpu(readl(target
));
997 val32
|= PHB_TCE_ENABLE
| PHB_DAC_DISABLE
| PHB_MCSR_ENABLE
;
999 printk(KERN_INFO
"Calgary: enabling translation on %s PHB %#x\n",
1000 (dev
->device
== PCI_DEVICE_ID_IBM_CALGARY
) ?
1001 "Calgary" : "CalIOC2", busnum
);
1002 printk(KERN_INFO
"Calgary: errant DMAs will now be prevented on this "
1005 writel(cpu_to_be32(val32
), target
);
1006 readl(target
); /* flush */
1008 init_timer(&tbl
->watchdog_timer
);
1009 tbl
->watchdog_timer
.function
= &calgary_watchdog
;
1010 tbl
->watchdog_timer
.data
= (unsigned long)dev
;
1011 mod_timer(&tbl
->watchdog_timer
, jiffies
);
1014 static void __init
calgary_disable_translation(struct pci_dev
*dev
)
1017 unsigned char busnum
;
1018 void __iomem
*target
;
1020 struct iommu_table
*tbl
;
1022 busnum
= dev
->bus
->number
;
1023 tbl
= pci_iommu(dev
->bus
);
1026 /* disable TCE in PHB Config Register */
1027 target
= calgary_reg(bbar
, phb_offset(busnum
) | PHB_CONFIG_RW_OFFSET
);
1028 val32
= be32_to_cpu(readl(target
));
1029 val32
&= ~(PHB_TCE_ENABLE
| PHB_DAC_DISABLE
| PHB_MCSR_ENABLE
);
1031 printk(KERN_INFO
"Calgary: disabling translation on PHB %#x!\n", busnum
);
1032 writel(cpu_to_be32(val32
), target
);
1033 readl(target
); /* flush */
1035 del_timer_sync(&tbl
->watchdog_timer
);
1038 static void __init
calgary_init_one_nontraslated(struct pci_dev
*dev
)
1041 set_pci_iommu(dev
->bus
, NULL
);
1043 /* is the device behind a bridge? */
1044 if (dev
->bus
->parent
)
1045 dev
->bus
->parent
->self
= dev
;
1047 dev
->bus
->self
= dev
;
1050 static int __init
calgary_init_one(struct pci_dev
*dev
)
1053 struct iommu_table
*tbl
;
1056 BUG_ON(dev
->bus
->number
>= MAX_PHB_BUS_NUM
);
1058 bbar
= busno_to_bbar(dev
->bus
->number
);
1059 ret
= calgary_setup_tar(dev
, bbar
);
1065 if (dev
->bus
->parent
) {
1066 if (dev
->bus
->parent
->self
)
1067 printk(KERN_WARNING
"Calgary: IEEEE, dev %p has "
1068 "bus->parent->self!\n", dev
);
1069 dev
->bus
->parent
->self
= dev
;
1071 dev
->bus
->self
= dev
;
1073 tbl
= pci_iommu(dev
->bus
);
1074 tbl
->chip_ops
->handle_quirks(tbl
, dev
);
1076 calgary_enable_translation(dev
);
1084 static int __init
calgary_locate_bbars(void)
1087 int rioidx
, phb
, bus
;
1089 void __iomem
*target
;
1090 unsigned long offset
;
1091 u8 start_bus
, end_bus
;
1095 for (rioidx
= 0; rioidx
< rio_table_hdr
->num_rio_dev
; rioidx
++) {
1096 struct rio_detail
*rio
= rio_devs
[rioidx
];
1098 if ((rio
->type
!= COMPAT_CALGARY
) && (rio
->type
!= ALT_CALGARY
))
1101 /* map entire 1MB of Calgary config space */
1102 bbar
= ioremap_nocache(rio
->BBAR
, 1024 * 1024);
1106 for (phb
= 0; phb
< PHBS_PER_CALGARY
; phb
++) {
1107 offset
= phb_debug_offsets
[phb
] | PHB_DEBUG_STUFF_OFFSET
;
1108 target
= calgary_reg(bbar
, offset
);
1110 val
= be32_to_cpu(readl(target
));
1112 start_bus
= (u8
)((val
& 0x00FF0000) >> 16);
1113 end_bus
= (u8
)((val
& 0x0000FF00) >> 8);
1116 for (bus
= start_bus
; bus
<= end_bus
; bus
++) {
1117 bus_info
[bus
].bbar
= bbar
;
1118 bus_info
[bus
].phbid
= phb
;
1121 bus_info
[start_bus
].bbar
= bbar
;
1122 bus_info
[start_bus
].phbid
= phb
;
1130 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1131 for (bus
= 0; bus
< ARRAY_SIZE(bus_info
); bus
++)
1132 if (bus_info
[bus
].bbar
)
1133 iounmap(bus_info
[bus
].bbar
);
1138 static int __init
calgary_init(void)
1141 struct pci_dev
*dev
= NULL
;
1142 struct calgary_bus_info
*info
;
1144 ret
= calgary_locate_bbars();
1148 /* Purely for kdump kernel case */
1149 if (is_kdump_kernel())
1150 get_tce_space_from_tar();
1153 dev
= pci_get_device(PCI_VENDOR_ID_IBM
, PCI_ANY_ID
, dev
);
1156 if (!is_cal_pci_dev(dev
->device
))
1159 info
= &bus_info
[dev
->bus
->number
];
1160 if (info
->translation_disabled
) {
1161 calgary_init_one_nontraslated(dev
);
1165 if (!info
->tce_space
&& !translate_empty_slots
)
1168 ret
= calgary_init_one(dev
);
1174 for_each_pci_dev(dev
) {
1175 struct iommu_table
*tbl
;
1177 tbl
= find_iommu_table(&dev
->dev
);
1179 if (translation_enabled(tbl
))
1180 dev
->dev
.archdata
.dma_ops
= &calgary_dma_ops
;
1187 dev
= pci_get_device(PCI_VENDOR_ID_IBM
, PCI_ANY_ID
, dev
);
1190 if (!is_cal_pci_dev(dev
->device
))
1193 info
= &bus_info
[dev
->bus
->number
];
1194 if (info
->translation_disabled
) {
1198 if (!info
->tce_space
&& !translate_empty_slots
)
1201 calgary_disable_translation(dev
);
1202 calgary_free_bus(dev
);
1203 pci_dev_put(dev
); /* Undo calgary_init_one()'s pci_dev_get() */
1204 dev
->dev
.archdata
.dma_ops
= NULL
;
1210 static inline int __init
determine_tce_table_size(u64 ram
)
1214 if (specified_table_size
!= TCE_TABLE_SIZE_UNSPECIFIED
)
1215 return specified_table_size
;
1218 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1219 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1220 * larger table size has twice as many entries, so shift the
1221 * max ram address by 13 to divide by 8K and then look at the
1222 * order of the result to choose between 0-7.
1224 ret
= get_order(ram
>> 13);
1225 if (ret
> TCE_TABLE_SIZE_8M
)
1226 ret
= TCE_TABLE_SIZE_8M
;
1231 static int __init
build_detail_arrays(void)
1234 unsigned numnodes
, i
;
1235 int scal_detail_size
, rio_detail_size
;
1237 numnodes
= rio_table_hdr
->num_scal_dev
;
1238 if (numnodes
> MAX_NUMNODES
){
1240 "Calgary: MAX_NUMNODES too low! Defined as %d, "
1241 "but system has %d nodes.\n",
1242 MAX_NUMNODES
, numnodes
);
1246 switch (rio_table_hdr
->version
){
1248 scal_detail_size
= 11;
1249 rio_detail_size
= 13;
1252 scal_detail_size
= 12;
1253 rio_detail_size
= 15;
1257 "Calgary: Invalid Rio Grande Table Version: %d\n",
1258 rio_table_hdr
->version
);
1262 ptr
= ((unsigned long)rio_table_hdr
) + 3;
1263 for (i
= 0; i
< numnodes
; i
++, ptr
+= scal_detail_size
)
1264 scal_devs
[i
] = (struct scal_detail
*)ptr
;
1266 for (i
= 0; i
< rio_table_hdr
->num_rio_dev
;
1267 i
++, ptr
+= rio_detail_size
)
1268 rio_devs
[i
] = (struct rio_detail
*)ptr
;
1273 static int __init
calgary_bus_has_devices(int bus
, unsigned short pci_dev
)
1278 if (pci_dev
== PCI_DEVICE_ID_IBM_CALIOC2
) {
1280 * FIXME: properly scan for devices accross the
1281 * PCI-to-PCI bridge on every CalIOC2 port.
1286 for (dev
= 1; dev
< 8; dev
++) {
1287 val
= read_pci_config(bus
, dev
, 0, 0);
1288 if (val
!= 0xffffffff)
1291 return (val
!= 0xffffffff);
1295 * calgary_init_bitmap_from_tce_table():
1296 * Funtion for kdump case. In the second/kdump kernel initialize
1297 * the bitmap based on the tce table entries obtained from first kernel
1299 static void calgary_init_bitmap_from_tce_table(struct iommu_table
*tbl
)
1303 tp
= ((u64
*)tbl
->it_base
);
1304 for (index
= 0 ; index
< tbl
->it_size
; index
++) {
1306 set_bit(index
, tbl
->it_map
);
1312 * get_tce_space_from_tar():
1313 * Function for kdump case. Get the tce tables from first kernel
1314 * by reading the contents of the base adress register of calgary iommu
1316 static void __init
get_tce_space_from_tar(void)
1319 void __iomem
*target
;
1320 unsigned long tce_space
;
1322 for (bus
= 0; bus
< MAX_PHB_BUS_NUM
; bus
++) {
1323 struct calgary_bus_info
*info
= &bus_info
[bus
];
1324 unsigned short pci_device
;
1327 val
= read_pci_config(bus
, 0, 0, 0);
1328 pci_device
= (val
& 0xFFFF0000) >> 16;
1330 if (!is_cal_pci_dev(pci_device
))
1332 if (info
->translation_disabled
)
1335 if (calgary_bus_has_devices(bus
, pci_device
) ||
1336 translate_empty_slots
) {
1337 target
= calgary_reg(bus_info
[bus
].bbar
,
1339 tce_space
= be64_to_cpu(readq(target
));
1340 tce_space
= tce_space
& TAR_SW_BITS
;
1342 tce_space
= tce_space
& (~specified_table_size
);
1343 info
->tce_space
= (u64
*)__va(tce_space
);
1349 void __init
detect_calgary(void)
1353 int calgary_found
= 0;
1355 unsigned int offset
, prev_offset
;
1359 * if the user specified iommu=off or iommu=soft or we found
1360 * another HW IOMMU already, bail out.
1362 if (swiotlb
|| no_iommu
|| iommu_detected
)
1368 if (!early_pci_allowed())
1371 printk(KERN_DEBUG
"Calgary: detecting Calgary via BIOS EBDA area\n");
1373 ptr
= (unsigned long)phys_to_virt(get_bios_ebda());
1375 rio_table_hdr
= NULL
;
1379 * The next offset is stored in the 1st word.
1380 * Only parse up until the offset increases:
1382 while (offset
> prev_offset
) {
1383 /* The block id is stored in the 2nd word */
1384 if (*((unsigned short *)(ptr
+ offset
+ 2)) == 0x4752){
1385 /* set the pointer past the offset & block id */
1386 rio_table_hdr
= (struct rio_table_hdr
*)(ptr
+ offset
+ 4);
1389 prev_offset
= offset
;
1390 offset
= *((unsigned short *)(ptr
+ offset
));
1392 if (!rio_table_hdr
) {
1393 printk(KERN_DEBUG
"Calgary: Unable to locate Rio Grande table "
1394 "in EBDA - bailing!\n");
1398 ret
= build_detail_arrays();
1400 printk(KERN_DEBUG
"Calgary: build_detail_arrays ret %d\n", ret
);
1404 specified_table_size
= determine_tce_table_size((is_kdump_kernel() ?
1405 saved_max_pfn
: max_pfn
) * PAGE_SIZE
);
1407 for (bus
= 0; bus
< MAX_PHB_BUS_NUM
; bus
++) {
1408 struct calgary_bus_info
*info
= &bus_info
[bus
];
1409 unsigned short pci_device
;
1412 val
= read_pci_config(bus
, 0, 0, 0);
1413 pci_device
= (val
& 0xFFFF0000) >> 16;
1415 if (!is_cal_pci_dev(pci_device
))
1418 if (info
->translation_disabled
)
1421 if (calgary_bus_has_devices(bus
, pci_device
) ||
1422 translate_empty_slots
) {
1424 * If it is kdump kernel, find and use tce tables
1425 * from first kernel, else allocate tce tables here
1427 if (!is_kdump_kernel()) {
1428 tbl
= alloc_tce_table();
1431 info
->tce_space
= tbl
;
1437 printk(KERN_DEBUG
"Calgary: finished detection, Calgary %s\n",
1438 calgary_found
? "found" : "not found");
1440 if (calgary_found
) {
1442 calgary_detected
= 1;
1443 printk(KERN_INFO
"PCI-DMA: Calgary IOMMU detected.\n");
1444 printk(KERN_INFO
"PCI-DMA: Calgary TCE table spec is %d\n",
1445 specified_table_size
);
1447 /* swiotlb for devices that aren't behind the Calgary. */
1448 if (max_pfn
> MAX_DMA32_PFN
)
1454 for (--bus
; bus
>= 0; --bus
) {
1455 struct calgary_bus_info
*info
= &bus_info
[bus
];
1457 if (info
->tce_space
)
1458 free_tce_table(info
->tce_space
);
1462 int __init
calgary_iommu_init(void)
1466 if (no_iommu
|| (swiotlb
&& !calgary_detected
))
1469 if (!calgary_detected
)
1472 /* ok, we're trying to use Calgary - let's roll */
1473 printk(KERN_INFO
"PCI-DMA: Using Calgary IOMMU\n");
1475 ret
= calgary_init();
1477 printk(KERN_ERR
"PCI-DMA: Calgary init failed %d, "
1478 "falling back to no_iommu\n", ret
);
1483 bad_dma_address
= 0x0;
1484 /* dma_ops is set to swiotlb or nommu */
1486 dma_ops
= &nommu_dma_ops
;
1491 static int __init
calgary_parse_options(char *p
)
1493 unsigned int bridge
;
1498 if (!strncmp(p
, "64k", 3))
1499 specified_table_size
= TCE_TABLE_SIZE_64K
;
1500 else if (!strncmp(p
, "128k", 4))
1501 specified_table_size
= TCE_TABLE_SIZE_128K
;
1502 else if (!strncmp(p
, "256k", 4))
1503 specified_table_size
= TCE_TABLE_SIZE_256K
;
1504 else if (!strncmp(p
, "512k", 4))
1505 specified_table_size
= TCE_TABLE_SIZE_512K
;
1506 else if (!strncmp(p
, "1M", 2))
1507 specified_table_size
= TCE_TABLE_SIZE_1M
;
1508 else if (!strncmp(p
, "2M", 2))
1509 specified_table_size
= TCE_TABLE_SIZE_2M
;
1510 else if (!strncmp(p
, "4M", 2))
1511 specified_table_size
= TCE_TABLE_SIZE_4M
;
1512 else if (!strncmp(p
, "8M", 2))
1513 specified_table_size
= TCE_TABLE_SIZE_8M
;
1515 len
= strlen("translate_empty_slots");
1516 if (!strncmp(p
, "translate_empty_slots", len
))
1517 translate_empty_slots
= 1;
1519 len
= strlen("disable");
1520 if (!strncmp(p
, "disable", len
)) {
1526 bridge
= simple_strtoul(p
, &endp
, 0);
1530 if (bridge
< MAX_PHB_BUS_NUM
) {
1531 printk(KERN_INFO
"Calgary: disabling "
1532 "translation for PHB %#x\n", bridge
);
1533 bus_info
[bridge
].translation_disabled
= 1;
1537 p
= strpbrk(p
, ",");
1545 __setup("calgary=", calgary_parse_options
);
1547 static void __init
calgary_fixup_one_tce_space(struct pci_dev
*dev
)
1549 struct iommu_table
*tbl
;
1550 unsigned int npages
;
1553 tbl
= pci_iommu(dev
->bus
);
1555 for (i
= 0; i
< 4; i
++) {
1556 struct resource
*r
= &dev
->resource
[PCI_BRIDGE_RESOURCES
+ i
];
1558 /* Don't give out TCEs that map MEM resources */
1559 if (!(r
->flags
& IORESOURCE_MEM
))
1562 /* 0-based? we reserve the whole 1st MB anyway */
1566 /* cover the whole region */
1567 npages
= (r
->end
- r
->start
) >> PAGE_SHIFT
;
1570 iommu_range_reserve(tbl
, r
->start
, npages
);
1574 static int __init
calgary_fixup_tce_spaces(void)
1576 struct pci_dev
*dev
= NULL
;
1577 struct calgary_bus_info
*info
;
1579 if (no_iommu
|| swiotlb
|| !calgary_detected
)
1582 printk(KERN_DEBUG
"Calgary: fixing up tce spaces\n");
1585 dev
= pci_get_device(PCI_VENDOR_ID_IBM
, PCI_ANY_ID
, dev
);
1588 if (!is_cal_pci_dev(dev
->device
))
1591 info
= &bus_info
[dev
->bus
->number
];
1592 if (info
->translation_disabled
)
1595 if (!info
->tce_space
)
1598 calgary_fixup_one_tce_space(dev
);
1606 * We need to be call after pcibios_assign_resources (fs_initcall level)
1607 * and before device_initcall.
1609 rootfs_initcall(calgary_fixup_tce_spaces
);