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/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>
46 #ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
47 int use_calgary __read_mostly = 1;
49 int use_calgary __read_mostly = 0;
50 #endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
52 #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
53 #define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
55 /* we need these for register space address calculation */
56 #define START_ADDRESS 0xfe000000
57 #define CHASSIS_BASE 0
58 #define ONE_BASED_CHASSIS_NUM 1
60 /* register offsets inside the host bridge space */
61 #define CALGARY_CONFIG_REG 0x0108
62 #define PHB_CSR_OFFSET 0x0110 /* Channel Status */
63 #define PHB_PLSSR_OFFSET 0x0120
64 #define PHB_CONFIG_RW_OFFSET 0x0160
65 #define PHB_IOBASE_BAR_LOW 0x0170
66 #define PHB_IOBASE_BAR_HIGH 0x0180
67 #define PHB_MEM_1_LOW 0x0190
68 #define PHB_MEM_1_HIGH 0x01A0
69 #define PHB_IO_ADDR_SIZE 0x01B0
70 #define PHB_MEM_1_SIZE 0x01C0
71 #define PHB_MEM_ST_OFFSET 0x01D0
72 #define PHB_AER_OFFSET 0x0200
73 #define PHB_CONFIG_0_HIGH 0x0220
74 #define PHB_CONFIG_0_LOW 0x0230
75 #define PHB_CONFIG_0_END 0x0240
76 #define PHB_MEM_2_LOW 0x02B0
77 #define PHB_MEM_2_HIGH 0x02C0
78 #define PHB_MEM_2_SIZE_HIGH 0x02D0
79 #define PHB_MEM_2_SIZE_LOW 0x02E0
80 #define PHB_DOSHOLE_OFFSET 0x08E0
82 /* CalIOC2 specific */
83 #define PHB_SAVIOR_L2 0x0DB0
84 #define PHB_PAGE_MIG_CTRL 0x0DA8
85 #define PHB_PAGE_MIG_DEBUG 0x0DA0
86 #define PHB_ROOT_COMPLEX_STATUS 0x0CB0
89 #define PHB_TCE_ENABLE 0x20000000
90 #define PHB_SLOT_DISABLE 0x1C000000
91 #define PHB_DAC_DISABLE 0x01000000
92 #define PHB_MEM2_ENABLE 0x00400000
93 #define PHB_MCSR_ENABLE 0x00100000
94 /* TAR (Table Address Register) */
95 #define TAR_SW_BITS 0x0000ffffffff800fUL
96 #define TAR_VALID 0x0000000000000008UL
97 /* CSR (Channel/DMA Status Register) */
98 #define CSR_AGENT_MASK 0xffe0ffff
99 /* CCR (Calgary Configuration Register) */
100 #define CCR_2SEC_TIMEOUT 0x000000000000000EUL
101 /* PMCR/PMDR (Page Migration Control/Debug Registers */
102 #define PMR_SOFTSTOP 0x80000000
103 #define PMR_SOFTSTOPFAULT 0x40000000
104 #define PMR_HARDSTOP 0x20000000
106 #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
107 #define MAX_NUM_CHASSIS 8 /* max number of chassis */
108 /* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
109 #define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
110 #define PHBS_PER_CALGARY 4
112 /* register offsets in Calgary's internal register space */
113 static const unsigned long tar_offsets[] = {
120 static const unsigned long split_queue_offsets[] = {
121 0x4870 /* SPLIT QUEUE 0 */,
122 0x5870 /* SPLIT QUEUE 1 */,
123 0x6870 /* SPLIT QUEUE 2 */,
124 0x7870 /* SPLIT QUEUE 3 */
127 static const unsigned long phb_offsets[] = {
134 /* PHB debug registers */
136 static const unsigned long phb_debug_offsets[] = {
137 0x4000 /* PHB 0 DEBUG */,
138 0x5000 /* PHB 1 DEBUG */,
139 0x6000 /* PHB 2 DEBUG */,
140 0x7000 /* PHB 3 DEBUG */
144 * STUFF register for each debug PHB,
145 * byte 1 = start bus number, byte 2 = end bus number
148 #define PHB_DEBUG_STUFF_OFFSET 0x0020
150 #define EMERGENCY_PAGES 32 /* = 128KB */
152 unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
153 static int translate_empty_slots __read_mostly = 0;
154 static int calgary_detected __read_mostly = 0;
156 static struct rio_table_hdr *rio_table_hdr __initdata;
157 static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
158 static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
160 struct calgary_bus_info {
162 unsigned char translation_disabled;
167 static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
168 static void calgary_tce_cache_blast(struct iommu_table *tbl);
169 static void calgary_dump_error_regs(struct iommu_table *tbl);
170 static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
171 static void calioc2_tce_cache_blast(struct iommu_table *tbl);
172 static void calioc2_dump_error_regs(struct iommu_table *tbl);
174 static struct cal_chipset_ops calgary_chip_ops = {
175 .handle_quirks = calgary_handle_quirks,
176 .tce_cache_blast = calgary_tce_cache_blast,
177 .dump_error_regs = calgary_dump_error_regs
180 static struct cal_chipset_ops calioc2_chip_ops = {
181 .handle_quirks = calioc2_handle_quirks,
182 .tce_cache_blast = calioc2_tce_cache_blast,
183 .dump_error_regs = calioc2_dump_error_regs
186 static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
188 /* enable this to stress test the chip's TCE cache */
189 #ifdef CONFIG_IOMMU_DEBUG
190 int debugging __read_mostly = 1;
192 static inline unsigned long verify_bit_range(unsigned long* bitmap,
193 int expected, unsigned long start, unsigned long end)
195 unsigned long idx = start;
197 BUG_ON(start >= end);
200 if (!!test_bit(idx, bitmap) != expected)
205 /* all bits have the expected value */
208 #else /* debugging is disabled */
209 int debugging __read_mostly = 0;
211 static inline unsigned long verify_bit_range(unsigned long* bitmap,
212 int expected, unsigned long start, unsigned long end)
217 #endif /* CONFIG_IOMMU_DEBUG */
219 static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
223 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
224 npages >>= PAGE_SHIFT;
229 static inline int translate_phb(struct pci_dev* dev)
231 int disabled = bus_info[dev->bus->number].translation_disabled;
235 static void iommu_range_reserve(struct iommu_table *tbl,
236 unsigned long start_addr, unsigned int npages)
240 unsigned long badbit;
242 index = start_addr >> PAGE_SHIFT;
244 /* bail out if we're asked to reserve a region we don't cover */
245 if (index >= tbl->it_size)
248 end = index + npages;
249 if (end > tbl->it_size) /* don't go off the table */
252 badbit = verify_bit_range(tbl->it_map, 0, index, end);
253 if (badbit != ~0UL) {
254 if (printk_ratelimit())
255 printk(KERN_ERR "Calgary: entry already allocated at "
256 "0x%lx tbl %p dma 0x%lx npages %u\n",
257 badbit, tbl, start_addr, npages);
260 set_bit_string(tbl->it_map, index, npages);
263 static unsigned long iommu_range_alloc(struct iommu_table *tbl,
266 unsigned long offset;
270 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
271 tbl->it_size, npages);
272 if (offset == ~0UL) {
273 tbl->chip_ops->tce_cache_blast(tbl);
274 offset = find_next_zero_string(tbl->it_map, 0,
275 tbl->it_size, npages);
276 if (offset == ~0UL) {
277 printk(KERN_WARNING "Calgary: IOMMU full.\n");
278 if (panic_on_overflow)
279 panic("Calgary: fix the allocator.\n");
281 return bad_dma_address;
285 set_bit_string(tbl->it_map, offset, npages);
286 tbl->it_hint = offset + npages;
287 BUG_ON(tbl->it_hint > tbl->it_size);
292 static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
293 unsigned int npages, int direction)
295 unsigned long entry, flags;
296 dma_addr_t ret = bad_dma_address;
298 spin_lock_irqsave(&tbl->it_lock, flags);
300 entry = iommu_range_alloc(tbl, npages);
302 if (unlikely(entry == bad_dma_address))
305 /* set the return dma address */
306 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
308 /* put the TCEs in the HW table */
309 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
312 spin_unlock_irqrestore(&tbl->it_lock, flags);
317 spin_unlock_irqrestore(&tbl->it_lock, flags);
318 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
319 "iommu %p\n", npages, tbl);
320 return bad_dma_address;
323 static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
327 unsigned long badbit;
328 unsigned long badend;
330 /* were we called with bad_dma_address? */
331 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
332 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
333 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
334 "address 0x%Lx\n", dma_addr);
339 entry = dma_addr >> PAGE_SHIFT;
341 BUG_ON(entry + npages > tbl->it_size);
343 tce_free(tbl, entry, npages);
345 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
346 if (badbit != ~0UL) {
347 if (printk_ratelimit())
348 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
349 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
350 badbit, tbl, dma_addr, entry, npages);
353 __clear_bit_string(tbl->it_map, entry, npages);
356 static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
361 spin_lock_irqsave(&tbl->it_lock, flags);
363 __iommu_free(tbl, dma_addr, npages);
365 spin_unlock_irqrestore(&tbl->it_lock, flags);
368 static inline struct iommu_table *find_iommu_table(struct device *dev)
370 struct pci_dev *pdev;
371 struct pci_bus *pbus;
372 struct iommu_table *tbl;
374 pdev = to_pci_dev(dev);
376 /* is the device behind a bridge? */
377 if (unlikely(pdev->bus->parent))
378 pbus = pdev->bus->parent;
382 tbl = pbus->self->sysdata;
383 BUG_ON(pdev->bus->parent && (tbl->it_busno != pdev->bus->parent->number));
388 static void __calgary_unmap_sg(struct iommu_table *tbl,
389 struct scatterlist *sglist, int nelems, int direction)
393 dma_addr_t dma = sglist->dma_address;
394 unsigned int dmalen = sglist->dma_length;
399 npages = num_dma_pages(dma, dmalen);
400 __iommu_free(tbl, dma, npages);
405 void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
406 int nelems, int direction)
409 struct iommu_table *tbl = find_iommu_table(dev);
411 if (!translate_phb(to_pci_dev(dev)))
414 spin_lock_irqsave(&tbl->it_lock, flags);
416 __calgary_unmap_sg(tbl, sglist, nelems, direction);
418 spin_unlock_irqrestore(&tbl->it_lock, flags);
421 static int calgary_nontranslate_map_sg(struct device* dev,
422 struct scatterlist *sg, int nelems, int direction)
426 for (i = 0; i < nelems; i++ ) {
427 struct scatterlist *s = &sg[i];
429 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
430 s->dma_length = s->length;
435 int calgary_map_sg(struct device *dev, struct scatterlist *sg,
436 int nelems, int direction)
438 struct iommu_table *tbl = find_iommu_table(dev);
445 if (!translate_phb(to_pci_dev(dev)))
446 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
448 spin_lock_irqsave(&tbl->it_lock, flags);
450 for (i = 0; i < nelems; i++ ) {
451 struct scatterlist *s = &sg[i];
454 vaddr = (unsigned long)page_address(s->page) + s->offset;
455 npages = num_dma_pages(vaddr, s->length);
457 entry = iommu_range_alloc(tbl, npages);
458 if (entry == bad_dma_address) {
459 /* makes sure unmap knows to stop */
464 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
466 /* insert into HW table */
467 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
470 s->dma_length = s->length;
473 spin_unlock_irqrestore(&tbl->it_lock, flags);
477 __calgary_unmap_sg(tbl, sg, nelems, direction);
478 for (i = 0; i < nelems; i++) {
479 sg[i].dma_address = bad_dma_address;
480 sg[i].dma_length = 0;
482 spin_unlock_irqrestore(&tbl->it_lock, flags);
486 dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
487 size_t size, int direction)
489 dma_addr_t dma_handle = bad_dma_address;
492 struct iommu_table *tbl = find_iommu_table(dev);
494 uaddr = (unsigned long)vaddr;
495 npages = num_dma_pages(uaddr, size);
497 if (translate_phb(to_pci_dev(dev)))
498 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
500 dma_handle = virt_to_bus(vaddr);
505 void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
506 size_t size, int direction)
508 struct iommu_table *tbl = find_iommu_table(dev);
511 if (!translate_phb(to_pci_dev(dev)))
514 npages = num_dma_pages(dma_handle, size);
515 iommu_free(tbl, dma_handle, npages);
518 void* calgary_alloc_coherent(struct device *dev, size_t size,
519 dma_addr_t *dma_handle, gfp_t flag)
523 unsigned int npages, order;
524 struct iommu_table *tbl = find_iommu_table(dev);
526 size = PAGE_ALIGN(size); /* size rounded up to full pages */
527 npages = size >> PAGE_SHIFT;
528 order = get_order(size);
530 /* alloc enough pages (and possibly more) */
531 ret = (void *)__get_free_pages(flag, order);
534 memset(ret, 0, size);
536 if (translate_phb(to_pci_dev(dev))) {
537 /* set up tces to cover the allocated range */
538 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
539 if (mapping == bad_dma_address)
542 *dma_handle = mapping;
543 } else /* non translated slot */
544 *dma_handle = virt_to_bus(ret);
549 free_pages((unsigned long)ret, get_order(size));
555 static const struct dma_mapping_ops calgary_dma_ops = {
556 .alloc_coherent = calgary_alloc_coherent,
557 .map_single = calgary_map_single,
558 .unmap_single = calgary_unmap_single,
559 .map_sg = calgary_map_sg,
560 .unmap_sg = calgary_unmap_sg,
563 static inline void __iomem * busno_to_bbar(unsigned char num)
565 return bus_info[num].bbar;
568 static inline int busno_to_phbid(unsigned char num)
570 return bus_info[num].phbid;
573 static inline unsigned long split_queue_offset(unsigned char num)
575 size_t idx = busno_to_phbid(num);
577 return split_queue_offsets[idx];
580 static inline unsigned long tar_offset(unsigned char num)
582 size_t idx = busno_to_phbid(num);
584 return tar_offsets[idx];
587 static inline unsigned long phb_offset(unsigned char num)
589 size_t idx = busno_to_phbid(num);
591 return phb_offsets[idx];
594 static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
596 unsigned long target = ((unsigned long)bar) | offset;
597 return (void __iomem*)target;
600 static inline int is_calioc2(unsigned short device)
602 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
605 static inline int is_calgary(unsigned short device)
607 return (device == PCI_DEVICE_ID_IBM_CALGARY);
610 static inline int is_cal_pci_dev(unsigned short device)
612 return (is_calgary(device) || is_calioc2(device));
615 static void calgary_tce_cache_blast(struct iommu_table *tbl)
620 void __iomem *bbar = tbl->bbar;
621 void __iomem *target;
623 /* disable arbitration on the bus */
624 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
628 /* read plssr to ensure it got there */
629 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
632 /* poll split queues until all DMA activity is done */
633 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
637 } while ((val & 0xff) != 0xff && i < 100);
639 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
640 "continuing anyway\n");
642 /* invalidate TCE cache */
643 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
644 writeq(tbl->tar_val, target);
646 /* enable arbitration */
647 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
649 (void)readl(target); /* flush */
652 static void calioc2_tce_cache_blast(struct iommu_table *tbl)
654 void __iomem *bbar = tbl->bbar;
655 void __iomem *target;
660 unsigned char bus = tbl->it_busno;
663 printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
664 "sequence - count %d\n", bus, count);
666 /* 1. using the Page Migration Control reg set SoftStop */
667 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
668 val = be32_to_cpu(readl(target));
669 printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
671 printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
672 writel(cpu_to_be32(val), target);
674 /* 2. poll split queues until all DMA activity is done */
675 printk(KERN_DEBUG "2a. starting to poll split queues\n");
676 target = calgary_reg(bbar, split_queue_offset(bus));
678 val64 = readq(target);
680 } while ((val64 & 0xff) != 0xff && i < 100);
682 printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
683 "continuing anyway\n");
685 /* 3. poll Page Migration DEBUG for SoftStopFault */
686 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
687 val = be32_to_cpu(readl(target));
688 printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
690 /* 4. if SoftStopFault - goto (1) */
691 if (val & PMR_SOFTSTOPFAULT) {
695 printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
696 "aborting TCE cache flush sequence!\n");
697 return; /* pray for the best */
701 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
702 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
703 printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
704 val = be32_to_cpu(readl(target));
705 printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
706 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
707 val = be32_to_cpu(readl(target));
708 printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
710 /* 6. invalidate TCE cache */
711 printk(KERN_DEBUG "6. invalidating TCE cache\n");
712 target = calgary_reg(bbar, tar_offset(bus));
713 writeq(tbl->tar_val, target);
715 /* 7. Re-read PMCR */
716 printk(KERN_DEBUG "7a. Re-reading PMCR\n");
717 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
718 val = be32_to_cpu(readl(target));
719 printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
721 /* 8. Remove HardStop */
722 printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
723 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
725 printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
726 writel(cpu_to_be32(val), target);
727 val = be32_to_cpu(readl(target));
728 printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
731 static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
734 unsigned int numpages;
736 limit = limit | 0xfffff;
739 numpages = ((limit - start) >> PAGE_SHIFT);
740 iommu_range_reserve(dev->sysdata, start, numpages);
743 static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
745 void __iomem *target;
746 u64 low, high, sizelow;
748 struct iommu_table *tbl = dev->sysdata;
749 unsigned char busnum = dev->bus->number;
750 void __iomem *bbar = tbl->bbar;
752 /* peripheral MEM_1 region */
753 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
754 low = be32_to_cpu(readl(target));
755 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
756 high = be32_to_cpu(readl(target));
757 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
758 sizelow = be32_to_cpu(readl(target));
760 start = (high << 32) | low;
763 calgary_reserve_mem_region(dev, start, limit);
766 static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
768 void __iomem *target;
770 u64 low, high, sizelow, sizehigh;
772 struct iommu_table *tbl = dev->sysdata;
773 unsigned char busnum = dev->bus->number;
774 void __iomem *bbar = tbl->bbar;
777 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
778 val32 = be32_to_cpu(readl(target));
779 if (!(val32 & PHB_MEM2_ENABLE))
782 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
783 low = be32_to_cpu(readl(target));
784 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
785 high = be32_to_cpu(readl(target));
786 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
787 sizelow = be32_to_cpu(readl(target));
788 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
789 sizehigh = be32_to_cpu(readl(target));
791 start = (high << 32) | low;
792 limit = (sizehigh << 32) | sizelow;
794 calgary_reserve_mem_region(dev, start, limit);
798 * some regions of the IO address space do not get translated, so we
799 * must not give devices IO addresses in those regions. The regions
800 * are the 640KB-1MB region and the two PCI peripheral memory holes.
801 * Reserve all of them in the IOMMU bitmap to avoid giving them out
804 static void __init calgary_reserve_regions(struct pci_dev *dev)
808 struct iommu_table *tbl = dev->sysdata;
810 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
811 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
813 /* avoid the BIOS/VGA first 640KB-1MB region */
814 /* for CalIOC2 - avoid the entire first 2MB */
815 if (is_calgary(dev->device)) {
816 start = (640 * 1024);
817 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
818 } else { /* calioc2 */
820 npages = (2 * 1024 * 1024) >> PAGE_SHIFT;
822 iommu_range_reserve(tbl, start, npages);
824 /* reserve the two PCI peripheral memory regions in IO space */
825 calgary_reserve_peripheral_mem_1(dev);
826 calgary_reserve_peripheral_mem_2(dev);
829 static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
833 void __iomem *target;
835 struct iommu_table *tbl;
837 /* build TCE tables for each PHB */
838 ret = build_tce_table(dev, bbar);
843 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
844 tce_free(tbl, 0, tbl->it_size);
846 if (is_calgary(dev->device))
847 tbl->chip_ops = &calgary_chip_ops;
848 else if (is_calioc2(dev->device))
849 tbl->chip_ops = &calioc2_chip_ops;
853 calgary_reserve_regions(dev);
855 /* set TARs for each PHB */
856 target = calgary_reg(bbar, tar_offset(dev->bus->number));
857 val64 = be64_to_cpu(readq(target));
859 /* zero out all TAR bits under sw control */
860 val64 &= ~TAR_SW_BITS;
861 table_phys = (u64)__pa(tbl->it_base);
865 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
866 val64 |= (u64) specified_table_size;
868 tbl->tar_val = cpu_to_be64(val64);
870 writeq(tbl->tar_val, target);
871 readq(target); /* flush */
876 static void __init calgary_free_bus(struct pci_dev *dev)
879 struct iommu_table *tbl = dev->sysdata;
880 void __iomem *target;
881 unsigned int bitmapsz;
883 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
884 val64 = be64_to_cpu(readq(target));
885 val64 &= ~TAR_SW_BITS;
886 writeq(cpu_to_be64(val64), target);
887 readq(target); /* flush */
889 bitmapsz = tbl->it_size / BITS_PER_BYTE;
890 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
896 /* Can't free bootmem allocated memory after system is up :-( */
897 bus_info[dev->bus->number].tce_space = NULL;
900 static void calgary_dump_error_regs(struct iommu_table *tbl)
902 void __iomem *bbar = tbl->bbar;
903 void __iomem *target;
906 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
907 csr = be32_to_cpu(readl(target));
909 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
910 plssr = be32_to_cpu(readl(target));
912 /* If no error, the agent ID in the CSR is not valid */
913 printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
914 "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
917 static void calioc2_dump_error_regs(struct iommu_table *tbl)
919 void __iomem *bbar = tbl->bbar;
920 u32 csr, csmr, plssr, mck, rcstat;
921 void __iomem *target;
922 unsigned long phboff = phb_offset(tbl->it_busno);
923 unsigned long erroff;
928 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
929 csr = be32_to_cpu(readl(target));
931 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
932 plssr = be32_to_cpu(readl(target));
934 target = calgary_reg(bbar, phboff | 0x290);
935 csmr = be32_to_cpu(readl(target));
937 target = calgary_reg(bbar, phboff | 0x800);
938 mck = be32_to_cpu(readl(target));
940 printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
943 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
944 csr, plssr, csmr, mck);
946 /* dump rest of error regs */
947 printk(KERN_EMERG "Calgary: ");
948 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
949 erroff = (0x810 + (i * 0x10)); /* err regs are at 0x810 - 0x870 */
950 target = calgary_reg(bbar, phboff | erroff);
951 errregs[i] = be32_to_cpu(readl(target));
952 printk("0x%08x@0x%lx ", errregs[i], erroff);
956 /* root complex status */
957 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
958 rcstat = be32_to_cpu(readl(target));
959 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
960 PHB_ROOT_COMPLEX_STATUS);
963 static void calgary_watchdog(unsigned long data)
965 struct pci_dev *dev = (struct pci_dev *)data;
966 struct iommu_table *tbl = dev->sysdata;
967 void __iomem *bbar = tbl->bbar;
969 void __iomem *target;
971 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
972 val32 = be32_to_cpu(readl(target));
974 /* If no error, the agent ID in the CSR is not valid */
975 if (val32 & CSR_AGENT_MASK) {
976 tbl->chip_ops->dump_error_regs(tbl);
981 /* Disable bus that caused the error */
982 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
983 PHB_CONFIG_RW_OFFSET);
984 val32 = be32_to_cpu(readl(target));
985 val32 |= PHB_SLOT_DISABLE;
986 writel(cpu_to_be32(val32), target);
987 readl(target); /* flush */
989 /* Reset the timer */
990 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
994 static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
995 unsigned char busnum, unsigned long timeout)
998 void __iomem *target;
999 unsigned int phb_shift = ~0; /* silence gcc */
1002 switch (busno_to_phbid(busnum)) {
1003 case 0: phb_shift = (63 - 19);
1005 case 1: phb_shift = (63 - 23);
1007 case 2: phb_shift = (63 - 27);
1009 case 3: phb_shift = (63 - 35);
1012 BUG_ON(busno_to_phbid(busnum));
1015 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
1016 val64 = be64_to_cpu(readq(target));
1018 /* zero out this PHB's timer bits */
1019 mask = ~(0xFUL << phb_shift);
1021 val64 |= (timeout << phb_shift);
1022 writeq(cpu_to_be64(val64), target);
1023 readq(target); /* flush */
1026 static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1028 unsigned char busnum = dev->bus->number;
1029 void __iomem *bbar = tbl->bbar;
1030 void __iomem *target;
1034 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
1036 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
1037 val = cpu_to_be32(readl(target));
1039 writel(cpu_to_be32(val), target);
1042 static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1044 unsigned char busnum = dev->bus->number;
1047 * Give split completion a longer timeout on bus 1 for aic94xx
1048 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
1050 if (is_calgary(dev->device) && (busnum == 1))
1051 calgary_set_split_completion_timeout(tbl->bbar, busnum,
1055 static void __init calgary_enable_translation(struct pci_dev *dev)
1058 unsigned char busnum;
1059 void __iomem *target;
1061 struct iommu_table *tbl;
1063 busnum = dev->bus->number;
1067 /* enable TCE in PHB Config Register */
1068 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1069 val32 = be32_to_cpu(readl(target));
1070 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
1072 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
1073 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
1074 "Calgary" : "CalIOC2", busnum);
1075 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
1078 writel(cpu_to_be32(val32), target);
1079 readl(target); /* flush */
1081 init_timer(&tbl->watchdog_timer);
1082 tbl->watchdog_timer.function = &calgary_watchdog;
1083 tbl->watchdog_timer.data = (unsigned long)dev;
1084 mod_timer(&tbl->watchdog_timer, jiffies);
1087 static void __init calgary_disable_translation(struct pci_dev *dev)
1090 unsigned char busnum;
1091 void __iomem *target;
1093 struct iommu_table *tbl;
1095 busnum = dev->bus->number;
1099 /* disable TCE in PHB Config Register */
1100 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1101 val32 = be32_to_cpu(readl(target));
1102 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1104 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
1105 writel(cpu_to_be32(val32), target);
1106 readl(target); /* flush */
1108 del_timer_sync(&tbl->watchdog_timer);
1111 static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
1114 dev->sysdata = NULL;
1116 /* is the device behind a bridge? */
1117 if (dev->bus->parent)
1118 dev->bus->parent->self = dev;
1120 dev->bus->self = dev;
1123 static int __init calgary_init_one(struct pci_dev *dev)
1126 struct iommu_table *tbl;
1129 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1131 bbar = busno_to_bbar(dev->bus->number);
1132 ret = calgary_setup_tar(dev, bbar);
1138 if (dev->bus->parent) {
1139 if (dev->bus->parent->self)
1140 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1141 "bus->parent->self!\n", dev);
1142 dev->bus->parent->self = dev;
1144 dev->bus->self = dev;
1147 tbl->chip_ops->handle_quirks(tbl, dev);
1149 calgary_enable_translation(dev);
1157 static int __init calgary_locate_bbars(void)
1160 int rioidx, phb, bus;
1162 void __iomem *target;
1163 unsigned long offset;
1164 u8 start_bus, end_bus;
1168 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1169 struct rio_detail *rio = rio_devs[rioidx];
1171 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
1174 /* map entire 1MB of Calgary config space */
1175 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1179 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
1180 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1181 target = calgary_reg(bbar, offset);
1183 val = be32_to_cpu(readl(target));
1185 start_bus = (u8)((val & 0x00FF0000) >> 16);
1186 end_bus = (u8)((val & 0x0000FF00) >> 8);
1189 for (bus = start_bus; bus <= end_bus; bus++) {
1190 bus_info[bus].bbar = bbar;
1191 bus_info[bus].phbid = phb;
1194 bus_info[start_bus].bbar = bbar;
1195 bus_info[start_bus].phbid = phb;
1203 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1204 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1205 if (bus_info[bus].bbar)
1206 iounmap(bus_info[bus].bbar);
1211 static int __init calgary_init(void)
1214 struct pci_dev *dev = NULL;
1217 ret = calgary_locate_bbars();
1222 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1225 if (!is_cal_pci_dev(dev->device))
1227 if (!translate_phb(dev)) {
1228 calgary_init_one_nontraslated(dev);
1231 tce_space = bus_info[dev->bus->number].tce_space;
1232 if (!tce_space && !translate_empty_slots) {
1233 printk("Calg: %p failed tce_space check\n", dev);
1236 ret = calgary_init_one(dev);
1245 dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
1249 if (!is_cal_pci_dev(dev->device))
1251 if (!translate_phb(dev)) {
1255 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
1258 calgary_disable_translation(dev);
1259 calgary_free_bus(dev);
1260 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
1266 static inline int __init determine_tce_table_size(u64 ram)
1270 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1271 return specified_table_size;
1274 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1275 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1276 * larger table size has twice as many entries, so shift the
1277 * max ram address by 13 to divide by 8K and then look at the
1278 * order of the result to choose between 0-7.
1280 ret = get_order(ram >> 13);
1281 if (ret > TCE_TABLE_SIZE_8M)
1282 ret = TCE_TABLE_SIZE_8M;
1287 static int __init build_detail_arrays(void)
1290 int i, scal_detail_size, rio_detail_size;
1292 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1294 "Calgary: MAX_NUMNODES too low! Defined as %d, "
1295 "but system has %d nodes.\n",
1296 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1300 switch (rio_table_hdr->version){
1302 scal_detail_size = 11;
1303 rio_detail_size = 13;
1306 scal_detail_size = 12;
1307 rio_detail_size = 15;
1311 "Calgary: Invalid Rio Grande Table Version: %d\n",
1312 rio_table_hdr->version);
1316 ptr = ((unsigned long)rio_table_hdr) + 3;
1317 for (i = 0; i < rio_table_hdr->num_scal_dev;
1318 i++, ptr += scal_detail_size)
1319 scal_devs[i] = (struct scal_detail *)ptr;
1321 for (i = 0; i < rio_table_hdr->num_rio_dev;
1322 i++, ptr += rio_detail_size)
1323 rio_devs[i] = (struct rio_detail *)ptr;
1328 static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1333 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1335 * FIXME: properly scan for devices accross the
1336 * PCI-to-PCI bridge on every CalIOC2 port.
1341 for (dev = 1; dev < 8; dev++) {
1342 val = read_pci_config(bus, dev, 0, 0);
1343 if (val != 0xffffffff)
1346 return (val != 0xffffffff);
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(end_pfn * PAGE_SIZE);
1406 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
1407 struct calgary_bus_info *info = &bus_info[bus];
1408 unsigned short pci_device;
1411 val = read_pci_config(bus, 0, 0, 0);
1412 pci_device = (val & 0xFFFF0000) >> 16;
1414 if (!is_cal_pci_dev(pci_device))
1417 if (info->translation_disabled)
1420 if (calgary_bus_has_devices(bus, pci_device) ||
1421 translate_empty_slots) {
1422 tbl = alloc_tce_table();
1425 info->tce_space = tbl;
1427 printk("Calg: allocated tce_table %p for bus 0x%x\n",
1428 info->tce_space, bus);
1432 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1433 calgary_found ? "found" : "not found");
1435 if (calgary_found) {
1437 calgary_detected = 1;
1438 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1439 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1440 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1441 debugging ? "enabled" : "disabled");
1446 for (--bus; bus >= 0; --bus) {
1447 struct calgary_bus_info *info = &bus_info[bus];
1449 if (info->tce_space)
1450 free_tce_table(info->tce_space);
1454 int __init calgary_iommu_init(void)
1458 if (no_iommu || swiotlb)
1461 if (!calgary_detected)
1464 /* ok, we're trying to use Calgary - let's roll */
1465 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1467 ret = calgary_init();
1469 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1470 "falling back to no_iommu\n", ret);
1471 if (end_pfn > MAX_DMA32_PFN)
1472 printk(KERN_ERR "WARNING more than 4GB of memory, "
1473 "32bit PCI may malfunction.\n");
1478 bad_dma_address = 0x0;
1479 dma_ops = &calgary_dma_ops;
1484 static int __init calgary_parse_options(char *p)
1486 unsigned int bridge;
1491 if (!strncmp(p, "64k", 3))
1492 specified_table_size = TCE_TABLE_SIZE_64K;
1493 else if (!strncmp(p, "128k", 4))
1494 specified_table_size = TCE_TABLE_SIZE_128K;
1495 else if (!strncmp(p, "256k", 4))
1496 specified_table_size = TCE_TABLE_SIZE_256K;
1497 else if (!strncmp(p, "512k", 4))
1498 specified_table_size = TCE_TABLE_SIZE_512K;
1499 else if (!strncmp(p, "1M", 2))
1500 specified_table_size = TCE_TABLE_SIZE_1M;
1501 else if (!strncmp(p, "2M", 2))
1502 specified_table_size = TCE_TABLE_SIZE_2M;
1503 else if (!strncmp(p, "4M", 2))
1504 specified_table_size = TCE_TABLE_SIZE_4M;
1505 else if (!strncmp(p, "8M", 2))
1506 specified_table_size = TCE_TABLE_SIZE_8M;
1508 len = strlen("translate_empty_slots");
1509 if (!strncmp(p, "translate_empty_slots", len))
1510 translate_empty_slots = 1;
1512 len = strlen("disable");
1513 if (!strncmp(p, "disable", len)) {
1519 bridge = simple_strtol(p, &endp, 0);
1523 if (bridge < MAX_PHB_BUS_NUM) {
1524 printk(KERN_INFO "Calgary: disabling "
1525 "translation for PHB %#x\n", bridge);
1526 bus_info[bridge].translation_disabled = 1;
1530 p = strpbrk(p, ",");
1538 __setup("calgary=", calgary_parse_options);
1540 static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
1542 struct iommu_table *tbl;
1543 unsigned int npages;
1548 for (i = 0; i < 4; i++) {
1549 struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
1551 /* Don't give out TCEs that map MEM resources */
1552 if (!(r->flags & IORESOURCE_MEM))
1555 /* 0-based? we reserve the whole 1st MB anyway */
1559 /* cover the whole region */
1560 npages = (r->end - r->start) >> PAGE_SHIFT;
1563 printk(KERN_DEBUG "Calg: dev %p [%x] tbl %p reserving "
1564 "0x%Lx-0x%Lx [0x%x pages]\n", dev, dev->bus->number,
1565 tbl, r->start, r->end, npages);
1567 iommu_range_reserve(tbl, r->start, npages);
1571 static int __init calgary_fixup_tce_spaces(void)
1573 struct pci_dev *dev = NULL;
1576 if (no_iommu || swiotlb || !calgary_detected)
1579 printk(KERN_DEBUG "Calgary: fixing tce spaces\n");
1582 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1585 if (!is_cal_pci_dev(dev->device))
1587 if (!translate_phb(dev))
1590 tce_space = bus_info[dev->bus->number].tce_space;
1594 calgary_fixup_one_tce_space(dev);
1602 * We need to be call after pcibios_assign_resources (fs_initcall level)
1603 * and before device_initcall.
1605 rootfs_initcall(calgary_fixup_tce_spaces);