2 * Derived from arch/powerpc/kernel/iommu.c
4 * Copyright (C) IBM Corporation, 2006
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/dma-mapping.h>
33 #include <linux/init.h>
34 #include <linux/bitops.h>
35 #include <linux/pci_ids.h>
36 #include <linux/pci.h>
37 #include <linux/delay.h>
38 #include <asm/proto.h>
39 #include <asm/calgary.h>
41 #include <asm/pci-direct.h>
42 #include <asm/system.h>
45 #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
46 #define PCI_VENDOR_DEVICE_ID_CALGARY \
47 (PCI_VENDOR_ID_IBM | PCI_DEVICE_ID_IBM_CALGARY << 16)
49 /* we need these for register space address calculation */
50 #define START_ADDRESS 0xfe000000
51 #define CHASSIS_BASE 0
52 #define ONE_BASED_CHASSIS_NUM 1
54 /* register offsets inside the host bridge space */
55 #define CALGARY_CONFIG_REG 0x0108
56 #define PHB_CSR_OFFSET 0x0110 /* Channel Status */
57 #define PHB_PLSSR_OFFSET 0x0120
58 #define PHB_CONFIG_RW_OFFSET 0x0160
59 #define PHB_IOBASE_BAR_LOW 0x0170
60 #define PHB_IOBASE_BAR_HIGH 0x0180
61 #define PHB_MEM_1_LOW 0x0190
62 #define PHB_MEM_1_HIGH 0x01A0
63 #define PHB_IO_ADDR_SIZE 0x01B0
64 #define PHB_MEM_1_SIZE 0x01C0
65 #define PHB_MEM_ST_OFFSET 0x01D0
66 #define PHB_AER_OFFSET 0x0200
67 #define PHB_CONFIG_0_HIGH 0x0220
68 #define PHB_CONFIG_0_LOW 0x0230
69 #define PHB_CONFIG_0_END 0x0240
70 #define PHB_MEM_2_LOW 0x02B0
71 #define PHB_MEM_2_HIGH 0x02C0
72 #define PHB_MEM_2_SIZE_HIGH 0x02D0
73 #define PHB_MEM_2_SIZE_LOW 0x02E0
74 #define PHB_DOSHOLE_OFFSET 0x08E0
77 #define PHB_TCE_ENABLE 0x20000000
78 #define PHB_SLOT_DISABLE 0x1C000000
79 #define PHB_DAC_DISABLE 0x01000000
80 #define PHB_MEM2_ENABLE 0x00400000
81 #define PHB_MCSR_ENABLE 0x00100000
82 /* TAR (Table Address Register) */
83 #define TAR_SW_BITS 0x0000ffffffff800fUL
84 #define TAR_VALID 0x0000000000000008UL
85 /* CSR (Channel/DMA Status Register) */
86 #define CSR_AGENT_MASK 0xffe0ffff
87 /* CCR (Calgary Configuration Register) */
88 #define CCR_2SEC_TIMEOUT 0x000000000000000EUL
90 #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
91 #define MAX_NUM_CHASSIS 8 /* max number of chassis */
92 /* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
93 #define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
94 #define PHBS_PER_CALGARY 4
96 /* register offsets in Calgary's internal register space */
97 static const unsigned long tar_offsets[] = {
104 static const unsigned long split_queue_offsets[] = {
105 0x4870 /* SPLIT QUEUE 0 */,
106 0x5870 /* SPLIT QUEUE 1 */,
107 0x6870 /* SPLIT QUEUE 2 */,
108 0x7870 /* SPLIT QUEUE 3 */
111 static const unsigned long phb_offsets[] = {
118 unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
119 static int translate_empty_slots __read_mostly = 0;
120 static int calgary_detected __read_mostly = 0;
122 struct calgary_bus_info {
124 unsigned char translation_disabled;
128 static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
130 static void tce_cache_blast(struct iommu_table *tbl);
132 /* enable this to stress test the chip's TCE cache */
133 #ifdef CONFIG_IOMMU_DEBUG
134 int debugging __read_mostly = 1;
136 static inline unsigned long verify_bit_range(unsigned long* bitmap,
137 int expected, unsigned long start, unsigned long end)
139 unsigned long idx = start;
141 BUG_ON(start >= end);
144 if (!!test_bit(idx, bitmap) != expected)
149 /* all bits have the expected value */
152 #else /* debugging is disabled */
153 int debugging __read_mostly = 0;
155 static inline unsigned long verify_bit_range(unsigned long* bitmap,
156 int expected, unsigned long start, unsigned long end)
160 #endif /* CONFIG_IOMMU_DEBUG */
162 static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
166 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
167 npages >>= PAGE_SHIFT;
172 static inline int translate_phb(struct pci_dev* dev)
174 int disabled = bus_info[dev->bus->number].translation_disabled;
178 static void iommu_range_reserve(struct iommu_table *tbl,
179 unsigned long start_addr, unsigned int npages)
183 unsigned long badbit;
185 index = start_addr >> PAGE_SHIFT;
187 /* bail out if we're asked to reserve a region we don't cover */
188 if (index >= tbl->it_size)
191 end = index + npages;
192 if (end > tbl->it_size) /* don't go off the table */
195 badbit = verify_bit_range(tbl->it_map, 0, index, end);
196 if (badbit != ~0UL) {
197 if (printk_ratelimit())
198 printk(KERN_ERR "Calgary: entry already allocated at "
199 "0x%lx tbl %p dma 0x%lx npages %u\n",
200 badbit, tbl, start_addr, npages);
203 set_bit_string(tbl->it_map, index, npages);
206 static unsigned long iommu_range_alloc(struct iommu_table *tbl,
209 unsigned long offset;
213 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
214 tbl->it_size, npages);
215 if (offset == ~0UL) {
216 tce_cache_blast(tbl);
217 offset = find_next_zero_string(tbl->it_map, 0,
218 tbl->it_size, npages);
219 if (offset == ~0UL) {
220 printk(KERN_WARNING "Calgary: IOMMU full.\n");
221 if (panic_on_overflow)
222 panic("Calgary: fix the allocator.\n");
224 return bad_dma_address;
228 set_bit_string(tbl->it_map, offset, npages);
229 tbl->it_hint = offset + npages;
230 BUG_ON(tbl->it_hint > tbl->it_size);
235 static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
236 unsigned int npages, int direction)
238 unsigned long entry, flags;
239 dma_addr_t ret = bad_dma_address;
241 spin_lock_irqsave(&tbl->it_lock, flags);
243 entry = iommu_range_alloc(tbl, npages);
245 if (unlikely(entry == bad_dma_address))
248 /* set the return dma address */
249 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
251 /* put the TCEs in the HW table */
252 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
255 spin_unlock_irqrestore(&tbl->it_lock, flags);
260 spin_unlock_irqrestore(&tbl->it_lock, flags);
261 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
262 "iommu %p\n", npages, tbl);
263 return bad_dma_address;
266 static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
270 unsigned long badbit;
272 entry = dma_addr >> PAGE_SHIFT;
274 BUG_ON(entry + npages > tbl->it_size);
276 tce_free(tbl, entry, npages);
278 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
279 if (badbit != ~0UL) {
280 if (printk_ratelimit())
281 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
282 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
283 badbit, tbl, dma_addr, entry, npages);
286 __clear_bit_string(tbl->it_map, entry, npages);
289 static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
294 spin_lock_irqsave(&tbl->it_lock, flags);
296 __iommu_free(tbl, dma_addr, npages);
298 spin_unlock_irqrestore(&tbl->it_lock, flags);
301 static void __calgary_unmap_sg(struct iommu_table *tbl,
302 struct scatterlist *sglist, int nelems, int direction)
306 dma_addr_t dma = sglist->dma_address;
307 unsigned int dmalen = sglist->dma_length;
312 npages = num_dma_pages(dma, dmalen);
313 __iommu_free(tbl, dma, npages);
318 void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
319 int nelems, int direction)
322 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
324 if (!translate_phb(to_pci_dev(dev)))
327 spin_lock_irqsave(&tbl->it_lock, flags);
329 __calgary_unmap_sg(tbl, sglist, nelems, direction);
331 spin_unlock_irqrestore(&tbl->it_lock, flags);
334 static int calgary_nontranslate_map_sg(struct device* dev,
335 struct scatterlist *sg, int nelems, int direction)
339 for (i = 0; i < nelems; i++ ) {
340 struct scatterlist *s = &sg[i];
342 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
343 s->dma_length = s->length;
348 int calgary_map_sg(struct device *dev, struct scatterlist *sg,
349 int nelems, int direction)
351 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
358 if (!translate_phb(to_pci_dev(dev)))
359 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
361 spin_lock_irqsave(&tbl->it_lock, flags);
363 for (i = 0; i < nelems; i++ ) {
364 struct scatterlist *s = &sg[i];
367 vaddr = (unsigned long)page_address(s->page) + s->offset;
368 npages = num_dma_pages(vaddr, s->length);
370 entry = iommu_range_alloc(tbl, npages);
371 if (entry == bad_dma_address) {
372 /* makes sure unmap knows to stop */
377 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
379 /* insert into HW table */
380 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
383 s->dma_length = s->length;
386 spin_unlock_irqrestore(&tbl->it_lock, flags);
390 __calgary_unmap_sg(tbl, sg, nelems, direction);
391 for (i = 0; i < nelems; i++) {
392 sg[i].dma_address = bad_dma_address;
393 sg[i].dma_length = 0;
395 spin_unlock_irqrestore(&tbl->it_lock, flags);
399 dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
400 size_t size, int direction)
402 dma_addr_t dma_handle = bad_dma_address;
405 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
407 uaddr = (unsigned long)vaddr;
408 npages = num_dma_pages(uaddr, size);
410 if (translate_phb(to_pci_dev(dev)))
411 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
413 dma_handle = virt_to_bus(vaddr);
418 void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
419 size_t size, int direction)
421 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
424 if (!translate_phb(to_pci_dev(dev)))
427 npages = num_dma_pages(dma_handle, size);
428 iommu_free(tbl, dma_handle, npages);
431 void* calgary_alloc_coherent(struct device *dev, size_t size,
432 dma_addr_t *dma_handle, gfp_t flag)
436 unsigned int npages, order;
437 struct iommu_table *tbl;
439 tbl = to_pci_dev(dev)->bus->self->sysdata;
441 size = PAGE_ALIGN(size); /* size rounded up to full pages */
442 npages = size >> PAGE_SHIFT;
443 order = get_order(size);
445 /* alloc enough pages (and possibly more) */
446 ret = (void *)__get_free_pages(flag, order);
449 memset(ret, 0, size);
451 if (translate_phb(to_pci_dev(dev))) {
452 /* set up tces to cover the allocated range */
453 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
454 if (mapping == bad_dma_address)
457 *dma_handle = mapping;
458 } else /* non translated slot */
459 *dma_handle = virt_to_bus(ret);
464 free_pages((unsigned long)ret, get_order(size));
470 static struct dma_mapping_ops calgary_dma_ops = {
471 .alloc_coherent = calgary_alloc_coherent,
472 .map_single = calgary_map_single,
473 .unmap_single = calgary_unmap_single,
474 .map_sg = calgary_map_sg,
475 .unmap_sg = calgary_unmap_sg,
478 static inline int busno_to_phbid(unsigned char num)
480 return bus_info[num].phbid;
483 static inline unsigned long split_queue_offset(unsigned char num)
485 size_t idx = busno_to_phbid(num);
487 return split_queue_offsets[idx];
490 static inline unsigned long tar_offset(unsigned char num)
492 size_t idx = busno_to_phbid(num);
494 return tar_offsets[idx];
497 static inline unsigned long phb_offset(unsigned char num)
499 size_t idx = busno_to_phbid(num);
501 return phb_offsets[idx];
504 static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
506 unsigned long target = ((unsigned long)bar) | offset;
507 return (void __iomem*)target;
510 static void tce_cache_blast(struct iommu_table *tbl)
515 void __iomem *bbar = tbl->bbar;
516 void __iomem *target;
518 /* disable arbitration on the bus */
519 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
523 /* read plssr to ensure it got there */
524 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
527 /* poll split queues until all DMA activity is done */
528 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
532 } while ((val & 0xff) != 0xff && i < 100);
534 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
535 "continuing anyway\n");
537 /* invalidate TCE cache */
538 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
539 writeq(tbl->tar_val, target);
541 /* enable arbitration */
542 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
544 (void)readl(target); /* flush */
547 static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
550 unsigned int numpages;
552 limit = limit | 0xfffff;
555 numpages = ((limit - start) >> PAGE_SHIFT);
556 iommu_range_reserve(dev->sysdata, start, numpages);
559 static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
561 void __iomem *target;
562 u64 low, high, sizelow;
564 struct iommu_table *tbl = dev->sysdata;
565 unsigned char busnum = dev->bus->number;
566 void __iomem *bbar = tbl->bbar;
568 /* peripheral MEM_1 region */
569 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
570 low = be32_to_cpu(readl(target));
571 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
572 high = be32_to_cpu(readl(target));
573 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
574 sizelow = be32_to_cpu(readl(target));
576 start = (high << 32) | low;
579 calgary_reserve_mem_region(dev, start, limit);
582 static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
584 void __iomem *target;
586 u64 low, high, sizelow, sizehigh;
588 struct iommu_table *tbl = dev->sysdata;
589 unsigned char busnum = dev->bus->number;
590 void __iomem *bbar = tbl->bbar;
593 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
594 val32 = be32_to_cpu(readl(target));
595 if (!(val32 & PHB_MEM2_ENABLE))
598 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
599 low = be32_to_cpu(readl(target));
600 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
601 high = be32_to_cpu(readl(target));
602 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
603 sizelow = be32_to_cpu(readl(target));
604 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
605 sizehigh = be32_to_cpu(readl(target));
607 start = (high << 32) | low;
608 limit = (sizehigh << 32) | sizelow;
610 calgary_reserve_mem_region(dev, start, limit);
614 * some regions of the IO address space do not get translated, so we
615 * must not give devices IO addresses in those regions. The regions
616 * are the 640KB-1MB region and the two PCI peripheral memory holes.
617 * Reserve all of them in the IOMMU bitmap to avoid giving them out
620 static void __init calgary_reserve_regions(struct pci_dev *dev)
624 unsigned char busnum;
626 struct iommu_table *tbl = dev->sysdata;
629 busnum = dev->bus->number;
631 /* reserve bad_dma_address in case it's a legal address */
632 iommu_range_reserve(tbl, bad_dma_address, 1);
634 /* avoid the BIOS/VGA first 640KB-1MB region */
635 start = (640 * 1024);
636 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
637 iommu_range_reserve(tbl, start, npages);
639 /* reserve the two PCI peripheral memory regions in IO space */
640 calgary_reserve_peripheral_mem_1(dev);
641 calgary_reserve_peripheral_mem_2(dev);
644 static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
648 void __iomem *target;
650 struct iommu_table *tbl;
652 /* build TCE tables for each PHB */
653 ret = build_tce_table(dev, bbar);
658 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
659 tce_free(tbl, 0, tbl->it_size);
661 calgary_reserve_regions(dev);
663 /* set TARs for each PHB */
664 target = calgary_reg(bbar, tar_offset(dev->bus->number));
665 val64 = be64_to_cpu(readq(target));
667 /* zero out all TAR bits under sw control */
668 val64 &= ~TAR_SW_BITS;
671 table_phys = (u64)__pa(tbl->it_base);
674 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
675 val64 |= (u64) specified_table_size;
677 tbl->tar_val = cpu_to_be64(val64);
678 writeq(tbl->tar_val, target);
679 readq(target); /* flush */
684 static void __init calgary_free_bus(struct pci_dev *dev)
687 struct iommu_table *tbl = dev->sysdata;
688 void __iomem *target;
689 unsigned int bitmapsz;
691 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
692 val64 = be64_to_cpu(readq(target));
693 val64 &= ~TAR_SW_BITS;
694 writeq(cpu_to_be64(val64), target);
695 readq(target); /* flush */
697 bitmapsz = tbl->it_size / BITS_PER_BYTE;
698 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
704 /* Can't free bootmem allocated memory after system is up :-( */
705 bus_info[dev->bus->number].tce_space = NULL;
708 static void calgary_watchdog(unsigned long data)
710 struct pci_dev *dev = (struct pci_dev *)data;
711 struct iommu_table *tbl = dev->sysdata;
712 void __iomem *bbar = tbl->bbar;
714 void __iomem *target;
716 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
717 val32 = be32_to_cpu(readl(target));
719 /* If no error, the agent ID in the CSR is not valid */
720 if (val32 & CSR_AGENT_MASK) {
721 printk(KERN_EMERG "calgary_watchdog: DMA error on PHB %#x, "
722 "CSR = %#x\n", dev->bus->number, val32);
725 /* Disable bus that caused the error */
726 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
727 PHB_CONFIG_RW_OFFSET);
728 val32 = be32_to_cpu(readl(target));
729 val32 |= PHB_SLOT_DISABLE;
730 writel(cpu_to_be32(val32), target);
731 readl(target); /* flush */
733 /* Reset the timer */
734 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
738 static void __init calgary_increase_split_completion_timeout(void __iomem *bbar,
739 unsigned char busnum)
742 void __iomem *target;
743 unsigned long phb_shift = -1;
746 switch (busno_to_phbid(busnum)) {
747 case 0: phb_shift = (63 - 19);
749 case 1: phb_shift = (63 - 23);
751 case 2: phb_shift = (63 - 27);
753 case 3: phb_shift = (63 - 35);
756 BUG_ON(busno_to_phbid(busnum));
759 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
760 val64 = be64_to_cpu(readq(target));
762 /* zero out this PHB's timer bits */
763 mask = ~(0xFUL << phb_shift);
765 val64 |= (CCR_2SEC_TIMEOUT << phb_shift);
766 writeq(cpu_to_be64(val64), target);
767 readq(target); /* flush */
770 static void __init calgary_enable_translation(struct pci_dev *dev)
773 unsigned char busnum;
774 void __iomem *target;
776 struct iommu_table *tbl;
778 busnum = dev->bus->number;
782 /* enable TCE in PHB Config Register */
783 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
784 val32 = be32_to_cpu(readl(target));
785 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
787 printk(KERN_INFO "Calgary: enabling translation on PHB %#x\n", busnum);
788 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
791 writel(cpu_to_be32(val32), target);
792 readl(target); /* flush */
795 * Give split completion a longer timeout on bus 1 for aic94xx
796 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
799 calgary_increase_split_completion_timeout(bbar, busnum);
801 init_timer(&tbl->watchdog_timer);
802 tbl->watchdog_timer.function = &calgary_watchdog;
803 tbl->watchdog_timer.data = (unsigned long)dev;
804 mod_timer(&tbl->watchdog_timer, jiffies);
807 static void __init calgary_disable_translation(struct pci_dev *dev)
810 unsigned char busnum;
811 void __iomem *target;
813 struct iommu_table *tbl;
815 busnum = dev->bus->number;
819 /* disable TCE in PHB Config Register */
820 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
821 val32 = be32_to_cpu(readl(target));
822 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
824 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
825 writel(cpu_to_be32(val32), target);
826 readl(target); /* flush */
828 del_timer_sync(&tbl->watchdog_timer);
831 static inline unsigned int __init locate_register_space(struct pci_dev *dev)
837 * Each Calgary has four busses. The first four busses (first Calgary)
838 * have RIO node ID 2, then the next four (second Calgary) have RIO
839 * node ID 3, the next four (third Calgary) have node ID 2 again, etc.
840 * We use a gross hack - relying on the dev->bus->number ordering,
841 * modulo 14 - to decide which Calgary a given bus is on. Busses 0, 1,
842 * 2 and 4 are on the first Calgary (id 2), 6, 8, a and c are on the
843 * second (id 3), and then it repeats modulo 14.
845 rionodeid = (dev->bus->number % 14 > 4) ? 3 : 2;
847 * register space address calculation as follows:
848 * FE0MB-8MB*OneBasedChassisNumber+1MB*(RioNodeId-ChassisBase)
849 * ChassisBase is always zero for x366/x260/x460
850 * RioNodeId is 2 for first Calgary, 3 for second Calgary
852 address = START_ADDRESS -
853 (0x800000 * (ONE_BASED_CHASSIS_NUM + dev->bus->number / 14)) +
854 (0x100000) * (rionodeid - CHASSIS_BASE);
858 static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
862 dev->bus->self = dev;
865 static int __init calgary_init_one(struct pci_dev *dev)
871 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
873 address = locate_register_space(dev);
874 /* map entire 1MB of Calgary config space */
875 bbar = ioremap_nocache(address, 1024 * 1024);
881 ret = calgary_setup_tar(dev, bbar);
886 dev->bus->self = dev;
887 calgary_enable_translation(dev);
897 static int __init calgary_init(void)
900 struct pci_dev *dev = NULL;
903 dev = pci_get_device(PCI_VENDOR_ID_IBM,
904 PCI_DEVICE_ID_IBM_CALGARY,
908 if (!translate_phb(dev)) {
909 calgary_init_one_nontraslated(dev);
912 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
915 ret = calgary_init_one(dev);
924 dev = pci_find_device_reverse(PCI_VENDOR_ID_IBM,
925 PCI_DEVICE_ID_IBM_CALGARY,
929 if (!translate_phb(dev)) {
933 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
936 calgary_disable_translation(dev);
937 calgary_free_bus(dev);
938 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
944 static inline int __init determine_tce_table_size(u64 ram)
948 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
949 return specified_table_size;
952 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
953 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
954 * larger table size has twice as many entries, so shift the
955 * max ram address by 13 to divide by 8K and then look at the
956 * order of the result to choose between 0-7.
958 ret = get_order(ram >> 13);
959 if (ret > TCE_TABLE_SIZE_8M)
960 ret = TCE_TABLE_SIZE_8M;
965 void __init detect_calgary(void)
970 int calgary_found = 0;
974 * if the user specified iommu=off or iommu=soft or we found
975 * another HW IOMMU already, bail out.
977 if (swiotlb || no_iommu || iommu_detected)
980 if (!early_pci_allowed())
983 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
985 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
987 struct calgary_bus_info *info = &bus_info[bus];
990 if (read_pci_config(bus, 0, 0, 0) != PCI_VENDOR_DEVICE_ID_CALGARY)
994 * There are 4 PHBs per Calgary chip. Set phb to which phb (0-3)
995 * it is connected to releative to the clagary chip.
997 phb = (phb + 1) % PHBS_PER_CALGARY;
999 if (info->translation_disabled)
1003 * Scan the slots of the PCI bus to see if there is a device present.
1004 * The parent bus will be the zero-ith device, so start at 1.
1006 for (dev = 1; dev < 8; dev++) {
1007 val = read_pci_config(bus, dev, 0, 0);
1008 if (val != 0xffffffff || translate_empty_slots) {
1009 tbl = alloc_tce_table();
1012 info->tce_space = tbl;
1020 if (calgary_found) {
1022 calgary_detected = 1;
1023 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1024 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1025 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1026 debugging ? "enabled" : "disabled");
1031 for (--bus; bus >= 0; --bus) {
1032 struct calgary_bus_info *info = &bus_info[bus];
1034 if (info->tce_space)
1035 free_tce_table(info->tce_space);
1039 int __init calgary_iommu_init(void)
1043 if (no_iommu || swiotlb)
1046 if (!calgary_detected)
1049 /* ok, we're trying to use Calgary - let's roll */
1050 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1052 ret = calgary_init();
1054 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1055 "falling back to no_iommu\n", ret);
1056 if (end_pfn > MAX_DMA32_PFN)
1057 printk(KERN_ERR "WARNING more than 4GB of memory, "
1058 "32bit PCI may malfunction.\n");
1063 dma_ops = &calgary_dma_ops;
1068 static int __init calgary_parse_options(char *p)
1070 unsigned int bridge;
1075 if (!strncmp(p, "64k", 3))
1076 specified_table_size = TCE_TABLE_SIZE_64K;
1077 else if (!strncmp(p, "128k", 4))
1078 specified_table_size = TCE_TABLE_SIZE_128K;
1079 else if (!strncmp(p, "256k", 4))
1080 specified_table_size = TCE_TABLE_SIZE_256K;
1081 else if (!strncmp(p, "512k", 4))
1082 specified_table_size = TCE_TABLE_SIZE_512K;
1083 else if (!strncmp(p, "1M", 2))
1084 specified_table_size = TCE_TABLE_SIZE_1M;
1085 else if (!strncmp(p, "2M", 2))
1086 specified_table_size = TCE_TABLE_SIZE_2M;
1087 else if (!strncmp(p, "4M", 2))
1088 specified_table_size = TCE_TABLE_SIZE_4M;
1089 else if (!strncmp(p, "8M", 2))
1090 specified_table_size = TCE_TABLE_SIZE_8M;
1092 len = strlen("translate_empty_slots");
1093 if (!strncmp(p, "translate_empty_slots", len))
1094 translate_empty_slots = 1;
1096 len = strlen("disable");
1097 if (!strncmp(p, "disable", len)) {
1103 bridge = simple_strtol(p, &endp, 0);
1107 if (bridge < MAX_PHB_BUS_NUM) {
1108 printk(KERN_INFO "Calgary: disabling "
1109 "translation for PHB %#x\n", bridge);
1110 bus_info[bridge].translation_disabled = 1;
1114 p = strpbrk(p, ",");
1122 __setup("calgary=", calgary_parse_options);