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 <linux/scatterlist.h>
40 #include <asm/calgary.h>
42 #include <asm/pci-direct.h>
43 #include <asm/system.h>
47 #ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
48 int use_calgary __read_mostly = 1;
50 int use_calgary __read_mostly = 0;
51 #endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
53 #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
54 #define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
56 /* register offsets inside the host bridge space */
57 #define CALGARY_CONFIG_REG 0x0108
58 #define PHB_CSR_OFFSET 0x0110 /* Channel Status */
59 #define PHB_PLSSR_OFFSET 0x0120
60 #define PHB_CONFIG_RW_OFFSET 0x0160
61 #define PHB_IOBASE_BAR_LOW 0x0170
62 #define PHB_IOBASE_BAR_HIGH 0x0180
63 #define PHB_MEM_1_LOW 0x0190
64 #define PHB_MEM_1_HIGH 0x01A0
65 #define PHB_IO_ADDR_SIZE 0x01B0
66 #define PHB_MEM_1_SIZE 0x01C0
67 #define PHB_MEM_ST_OFFSET 0x01D0
68 #define PHB_AER_OFFSET 0x0200
69 #define PHB_CONFIG_0_HIGH 0x0220
70 #define PHB_CONFIG_0_LOW 0x0230
71 #define PHB_CONFIG_0_END 0x0240
72 #define PHB_MEM_2_LOW 0x02B0
73 #define PHB_MEM_2_HIGH 0x02C0
74 #define PHB_MEM_2_SIZE_HIGH 0x02D0
75 #define PHB_MEM_2_SIZE_LOW 0x02E0
76 #define PHB_DOSHOLE_OFFSET 0x08E0
78 /* CalIOC2 specific */
79 #define PHB_SAVIOR_L2 0x0DB0
80 #define PHB_PAGE_MIG_CTRL 0x0DA8
81 #define PHB_PAGE_MIG_DEBUG 0x0DA0
82 #define PHB_ROOT_COMPLEX_STATUS 0x0CB0
85 #define PHB_TCE_ENABLE 0x20000000
86 #define PHB_SLOT_DISABLE 0x1C000000
87 #define PHB_DAC_DISABLE 0x01000000
88 #define PHB_MEM2_ENABLE 0x00400000
89 #define PHB_MCSR_ENABLE 0x00100000
90 /* TAR (Table Address Register) */
91 #define TAR_SW_BITS 0x0000ffffffff800fUL
92 #define TAR_VALID 0x0000000000000008UL
93 /* CSR (Channel/DMA Status Register) */
94 #define CSR_AGENT_MASK 0xffe0ffff
95 /* CCR (Calgary Configuration Register) */
96 #define CCR_2SEC_TIMEOUT 0x000000000000000EUL
97 /* PMCR/PMDR (Page Migration Control/Debug Registers */
98 #define PMR_SOFTSTOP 0x80000000
99 #define PMR_SOFTSTOPFAULT 0x40000000
100 #define PMR_HARDSTOP 0x20000000
102 #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
103 #define MAX_NUM_CHASSIS 8 /* max number of chassis */
104 /* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
105 #define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
106 #define PHBS_PER_CALGARY 4
108 /* register offsets in Calgary's internal register space */
109 static const unsigned long tar_offsets[] = {
116 static const unsigned long split_queue_offsets[] = {
117 0x4870 /* SPLIT QUEUE 0 */,
118 0x5870 /* SPLIT QUEUE 1 */,
119 0x6870 /* SPLIT QUEUE 2 */,
120 0x7870 /* SPLIT QUEUE 3 */
123 static const unsigned long phb_offsets[] = {
130 /* PHB debug registers */
132 static const unsigned long phb_debug_offsets[] = {
133 0x4000 /* PHB 0 DEBUG */,
134 0x5000 /* PHB 1 DEBUG */,
135 0x6000 /* PHB 2 DEBUG */,
136 0x7000 /* PHB 3 DEBUG */
140 * STUFF register for each debug PHB,
141 * byte 1 = start bus number, byte 2 = end bus number
144 #define PHB_DEBUG_STUFF_OFFSET 0x0020
146 #define EMERGENCY_PAGES 32 /* = 128KB */
148 unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
149 static int translate_empty_slots __read_mostly = 0;
150 static int calgary_detected __read_mostly = 0;
152 static struct rio_table_hdr *rio_table_hdr __initdata;
153 static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
154 static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
156 struct calgary_bus_info {
158 unsigned char translation_disabled;
163 static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
164 static void calgary_tce_cache_blast(struct iommu_table *tbl);
165 static void calgary_dump_error_regs(struct iommu_table *tbl);
166 static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
167 static void calioc2_tce_cache_blast(struct iommu_table *tbl);
168 static void calioc2_dump_error_regs(struct iommu_table *tbl);
170 static struct cal_chipset_ops calgary_chip_ops = {
171 .handle_quirks = calgary_handle_quirks,
172 .tce_cache_blast = calgary_tce_cache_blast,
173 .dump_error_regs = calgary_dump_error_regs
176 static struct cal_chipset_ops calioc2_chip_ops = {
177 .handle_quirks = calioc2_handle_quirks,
178 .tce_cache_blast = calioc2_tce_cache_blast,
179 .dump_error_regs = calioc2_dump_error_regs
182 static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
184 /* enable this to stress test the chip's TCE cache */
185 #ifdef CONFIG_IOMMU_DEBUG
186 int debugging __read_mostly = 1;
188 static inline unsigned long verify_bit_range(unsigned long* bitmap,
189 int expected, unsigned long start, unsigned long end)
191 unsigned long idx = start;
193 BUG_ON(start >= end);
196 if (!!test_bit(idx, bitmap) != expected)
201 /* all bits have the expected value */
204 #else /* debugging is disabled */
205 int debugging __read_mostly = 0;
207 static inline unsigned long verify_bit_range(unsigned long* bitmap,
208 int expected, unsigned long start, unsigned long end)
213 #endif /* CONFIG_IOMMU_DEBUG */
215 static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
219 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
220 npages >>= PAGE_SHIFT;
225 static inline int translation_enabled(struct iommu_table *tbl)
227 /* only PHBs with translation enabled have an IOMMU table */
228 return (tbl != NULL);
231 static void iommu_range_reserve(struct iommu_table *tbl,
232 unsigned long start_addr, unsigned int npages)
236 unsigned long badbit;
239 index = start_addr >> PAGE_SHIFT;
241 /* bail out if we're asked to reserve a region we don't cover */
242 if (index >= tbl->it_size)
245 end = index + npages;
246 if (end > tbl->it_size) /* don't go off the table */
249 spin_lock_irqsave(&tbl->it_lock, flags);
251 badbit = verify_bit_range(tbl->it_map, 0, index, end);
252 if (badbit != ~0UL) {
253 if (printk_ratelimit())
254 printk(KERN_ERR "Calgary: entry already allocated at "
255 "0x%lx tbl %p dma 0x%lx npages %u\n",
256 badbit, tbl, start_addr, npages);
259 set_bit_string(tbl->it_map, index, npages);
261 spin_unlock_irqrestore(&tbl->it_lock, flags);
264 static unsigned long iommu_range_alloc(struct iommu_table *tbl,
268 unsigned long offset;
272 spin_lock_irqsave(&tbl->it_lock, flags);
274 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
275 tbl->it_size, npages);
276 if (offset == ~0UL) {
277 tbl->chip_ops->tce_cache_blast(tbl);
278 offset = find_next_zero_string(tbl->it_map, 0,
279 tbl->it_size, npages);
280 if (offset == ~0UL) {
281 printk(KERN_WARNING "Calgary: IOMMU full.\n");
282 spin_unlock_irqrestore(&tbl->it_lock, flags);
283 if (panic_on_overflow)
284 panic("Calgary: fix the allocator.\n");
286 return bad_dma_address;
290 set_bit_string(tbl->it_map, offset, npages);
291 tbl->it_hint = offset + npages;
292 BUG_ON(tbl->it_hint > tbl->it_size);
294 spin_unlock_irqrestore(&tbl->it_lock, flags);
299 static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
300 unsigned int npages, int direction)
303 dma_addr_t ret = bad_dma_address;
305 entry = iommu_range_alloc(tbl, npages);
307 if (unlikely(entry == bad_dma_address))
310 /* set the return dma address */
311 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
313 /* put the TCEs in the HW table */
314 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
320 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
321 "iommu %p\n", npages, tbl);
322 return bad_dma_address;
325 static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
329 unsigned long badbit;
330 unsigned long badend;
333 /* were we called with bad_dma_address? */
334 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
335 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
336 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
337 "address 0x%Lx\n", dma_addr);
342 entry = dma_addr >> PAGE_SHIFT;
344 BUG_ON(entry + npages > tbl->it_size);
346 tce_free(tbl, entry, npages);
348 spin_lock_irqsave(&tbl->it_lock, flags);
350 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
351 if (badbit != ~0UL) {
352 if (printk_ratelimit())
353 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
354 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
355 badbit, tbl, dma_addr, entry, npages);
358 __clear_bit_string(tbl->it_map, entry, npages);
360 spin_unlock_irqrestore(&tbl->it_lock, flags);
363 static inline struct iommu_table *find_iommu_table(struct device *dev)
365 struct pci_dev *pdev;
366 struct pci_bus *pbus;
367 struct iommu_table *tbl;
369 pdev = to_pci_dev(dev);
373 /* is the device behind a bridge? Look for the root bus */
377 tbl = pci_iommu(pbus);
379 BUG_ON(tbl && (tbl->it_busno != pbus->number));
384 static void calgary_unmap_sg(struct device *dev,
385 struct scatterlist *sglist, int nelems, int direction)
387 struct iommu_table *tbl = find_iommu_table(dev);
388 struct scatterlist *s;
391 if (!translation_enabled(tbl))
394 for_each_sg(sglist, s, nelems, i) {
396 dma_addr_t dma = s->dma_address;
397 unsigned int dmalen = s->dma_length;
402 npages = num_dma_pages(dma, dmalen);
403 iommu_free(tbl, dma, npages);
407 static int calgary_nontranslate_map_sg(struct device* dev,
408 struct scatterlist *sg, int nelems, int direction)
410 struct scatterlist *s;
413 for_each_sg(sg, s, nelems, i) {
414 struct page *p = sg_page(s);
417 s->dma_address = virt_to_bus(sg_virt(s));
418 s->dma_length = s->length;
423 static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
424 int nelems, int direction)
426 struct iommu_table *tbl = find_iommu_table(dev);
427 struct scatterlist *s;
433 if (!translation_enabled(tbl))
434 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
436 for_each_sg(sg, s, nelems, i) {
439 vaddr = (unsigned long) sg_virt(s);
440 npages = num_dma_pages(vaddr, s->length);
442 entry = iommu_range_alloc(tbl, npages);
443 if (entry == bad_dma_address) {
444 /* makes sure unmap knows to stop */
449 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
451 /* insert into HW table */
452 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
455 s->dma_length = s->length;
460 calgary_unmap_sg(dev, sg, nelems, direction);
461 for_each_sg(sg, s, nelems, i) {
462 sg->dma_address = bad_dma_address;
468 static dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
469 size_t size, int direction)
471 dma_addr_t dma_handle = bad_dma_address;
474 struct iommu_table *tbl = find_iommu_table(dev);
476 uaddr = (unsigned long)vaddr;
477 npages = num_dma_pages(uaddr, size);
479 if (translation_enabled(tbl))
480 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
482 dma_handle = virt_to_bus(vaddr);
487 static void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
488 size_t size, int direction)
490 struct iommu_table *tbl = find_iommu_table(dev);
493 if (!translation_enabled(tbl))
496 npages = num_dma_pages(dma_handle, size);
497 iommu_free(tbl, dma_handle, npages);
500 static void* calgary_alloc_coherent(struct device *dev, size_t size,
501 dma_addr_t *dma_handle, gfp_t flag)
505 unsigned int npages, order;
506 struct iommu_table *tbl = find_iommu_table(dev);
508 size = PAGE_ALIGN(size); /* size rounded up to full pages */
509 npages = size >> PAGE_SHIFT;
510 order = get_order(size);
512 /* alloc enough pages (and possibly more) */
513 ret = (void *)__get_free_pages(flag, order);
516 memset(ret, 0, size);
518 if (translation_enabled(tbl)) {
519 /* set up tces to cover the allocated range */
520 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
521 if (mapping == bad_dma_address)
524 *dma_handle = mapping;
525 } else /* non translated slot */
526 *dma_handle = virt_to_bus(ret);
531 free_pages((unsigned long)ret, get_order(size));
537 static const struct dma_mapping_ops calgary_dma_ops = {
538 .alloc_coherent = calgary_alloc_coherent,
539 .map_single = calgary_map_single,
540 .unmap_single = calgary_unmap_single,
541 .map_sg = calgary_map_sg,
542 .unmap_sg = calgary_unmap_sg,
545 static inline void __iomem * busno_to_bbar(unsigned char num)
547 return bus_info[num].bbar;
550 static inline int busno_to_phbid(unsigned char num)
552 return bus_info[num].phbid;
555 static inline unsigned long split_queue_offset(unsigned char num)
557 size_t idx = busno_to_phbid(num);
559 return split_queue_offsets[idx];
562 static inline unsigned long tar_offset(unsigned char num)
564 size_t idx = busno_to_phbid(num);
566 return tar_offsets[idx];
569 static inline unsigned long phb_offset(unsigned char num)
571 size_t idx = busno_to_phbid(num);
573 return phb_offsets[idx];
576 static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
578 unsigned long target = ((unsigned long)bar) | offset;
579 return (void __iomem*)target;
582 static inline int is_calioc2(unsigned short device)
584 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
587 static inline int is_calgary(unsigned short device)
589 return (device == PCI_DEVICE_ID_IBM_CALGARY);
592 static inline int is_cal_pci_dev(unsigned short device)
594 return (is_calgary(device) || is_calioc2(device));
597 static void calgary_tce_cache_blast(struct iommu_table *tbl)
602 void __iomem *bbar = tbl->bbar;
603 void __iomem *target;
605 /* disable arbitration on the bus */
606 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
610 /* read plssr to ensure it got there */
611 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
614 /* poll split queues until all DMA activity is done */
615 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
619 } while ((val & 0xff) != 0xff && i < 100);
621 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
622 "continuing anyway\n");
624 /* invalidate TCE cache */
625 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
626 writeq(tbl->tar_val, target);
628 /* enable arbitration */
629 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
631 (void)readl(target); /* flush */
634 static void calioc2_tce_cache_blast(struct iommu_table *tbl)
636 void __iomem *bbar = tbl->bbar;
637 void __iomem *target;
642 unsigned char bus = tbl->it_busno;
645 printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
646 "sequence - count %d\n", bus, count);
648 /* 1. using the Page Migration Control reg set SoftStop */
649 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
650 val = be32_to_cpu(readl(target));
651 printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
653 printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
654 writel(cpu_to_be32(val), target);
656 /* 2. poll split queues until all DMA activity is done */
657 printk(KERN_DEBUG "2a. starting to poll split queues\n");
658 target = calgary_reg(bbar, split_queue_offset(bus));
660 val64 = readq(target);
662 } while ((val64 & 0xff) != 0xff && i < 100);
664 printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
665 "continuing anyway\n");
667 /* 3. poll Page Migration DEBUG for SoftStopFault */
668 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
669 val = be32_to_cpu(readl(target));
670 printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
672 /* 4. if SoftStopFault - goto (1) */
673 if (val & PMR_SOFTSTOPFAULT) {
677 printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
678 "aborting TCE cache flush sequence!\n");
679 return; /* pray for the best */
683 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
684 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
685 printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
686 val = be32_to_cpu(readl(target));
687 printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
688 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
689 val = be32_to_cpu(readl(target));
690 printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
692 /* 6. invalidate TCE cache */
693 printk(KERN_DEBUG "6. invalidating TCE cache\n");
694 target = calgary_reg(bbar, tar_offset(bus));
695 writeq(tbl->tar_val, target);
697 /* 7. Re-read PMCR */
698 printk(KERN_DEBUG "7a. Re-reading PMCR\n");
699 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
700 val = be32_to_cpu(readl(target));
701 printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
703 /* 8. Remove HardStop */
704 printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
705 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
707 printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
708 writel(cpu_to_be32(val), target);
709 val = be32_to_cpu(readl(target));
710 printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
713 static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
716 unsigned int numpages;
718 limit = limit | 0xfffff;
721 numpages = ((limit - start) >> PAGE_SHIFT);
722 iommu_range_reserve(pci_iommu(dev->bus), start, numpages);
725 static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
727 void __iomem *target;
728 u64 low, high, sizelow;
730 struct iommu_table *tbl = pci_iommu(dev->bus);
731 unsigned char busnum = dev->bus->number;
732 void __iomem *bbar = tbl->bbar;
734 /* peripheral MEM_1 region */
735 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
736 low = be32_to_cpu(readl(target));
737 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
738 high = be32_to_cpu(readl(target));
739 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
740 sizelow = be32_to_cpu(readl(target));
742 start = (high << 32) | low;
745 calgary_reserve_mem_region(dev, start, limit);
748 static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
750 void __iomem *target;
752 u64 low, high, sizelow, sizehigh;
754 struct iommu_table *tbl = pci_iommu(dev->bus);
755 unsigned char busnum = dev->bus->number;
756 void __iomem *bbar = tbl->bbar;
759 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
760 val32 = be32_to_cpu(readl(target));
761 if (!(val32 & PHB_MEM2_ENABLE))
764 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
765 low = be32_to_cpu(readl(target));
766 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
767 high = be32_to_cpu(readl(target));
768 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
769 sizelow = be32_to_cpu(readl(target));
770 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
771 sizehigh = be32_to_cpu(readl(target));
773 start = (high << 32) | low;
774 limit = (sizehigh << 32) | sizelow;
776 calgary_reserve_mem_region(dev, start, limit);
780 * some regions of the IO address space do not get translated, so we
781 * must not give devices IO addresses in those regions. The regions
782 * are the 640KB-1MB region and the two PCI peripheral memory holes.
783 * Reserve all of them in the IOMMU bitmap to avoid giving them out
786 static void __init calgary_reserve_regions(struct pci_dev *dev)
790 struct iommu_table *tbl = pci_iommu(dev->bus);
792 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
793 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
795 /* avoid the BIOS/VGA first 640KB-1MB region */
796 /* for CalIOC2 - avoid the entire first MB */
797 if (is_calgary(dev->device)) {
798 start = (640 * 1024);
799 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
800 } else { /* calioc2 */
802 npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
804 iommu_range_reserve(tbl, start, npages);
806 /* reserve the two PCI peripheral memory regions in IO space */
807 calgary_reserve_peripheral_mem_1(dev);
808 calgary_reserve_peripheral_mem_2(dev);
811 static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
815 void __iomem *target;
817 struct iommu_table *tbl;
819 /* build TCE tables for each PHB */
820 ret = build_tce_table(dev, bbar);
824 tbl = pci_iommu(dev->bus);
825 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
826 tce_free(tbl, 0, tbl->it_size);
828 if (is_calgary(dev->device))
829 tbl->chip_ops = &calgary_chip_ops;
830 else if (is_calioc2(dev->device))
831 tbl->chip_ops = &calioc2_chip_ops;
835 calgary_reserve_regions(dev);
837 /* set TARs for each PHB */
838 target = calgary_reg(bbar, tar_offset(dev->bus->number));
839 val64 = be64_to_cpu(readq(target));
841 /* zero out all TAR bits under sw control */
842 val64 &= ~TAR_SW_BITS;
843 table_phys = (u64)__pa(tbl->it_base);
847 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
848 val64 |= (u64) specified_table_size;
850 tbl->tar_val = cpu_to_be64(val64);
852 writeq(tbl->tar_val, target);
853 readq(target); /* flush */
858 static void __init calgary_free_bus(struct pci_dev *dev)
861 struct iommu_table *tbl = pci_iommu(dev->bus);
862 void __iomem *target;
863 unsigned int bitmapsz;
865 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
866 val64 = be64_to_cpu(readq(target));
867 val64 &= ~TAR_SW_BITS;
868 writeq(cpu_to_be64(val64), target);
869 readq(target); /* flush */
871 bitmapsz = tbl->it_size / BITS_PER_BYTE;
872 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
877 set_pci_iommu(dev->bus, NULL);
879 /* Can't free bootmem allocated memory after system is up :-( */
880 bus_info[dev->bus->number].tce_space = NULL;
883 static void calgary_dump_error_regs(struct iommu_table *tbl)
885 void __iomem *bbar = tbl->bbar;
886 void __iomem *target;
889 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
890 csr = be32_to_cpu(readl(target));
892 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
893 plssr = be32_to_cpu(readl(target));
895 /* If no error, the agent ID in the CSR is not valid */
896 printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
897 "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
900 static void calioc2_dump_error_regs(struct iommu_table *tbl)
902 void __iomem *bbar = tbl->bbar;
903 u32 csr, csmr, plssr, mck, rcstat;
904 void __iomem *target;
905 unsigned long phboff = phb_offset(tbl->it_busno);
906 unsigned long erroff;
911 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
912 csr = be32_to_cpu(readl(target));
914 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
915 plssr = be32_to_cpu(readl(target));
917 target = calgary_reg(bbar, phboff | 0x290);
918 csmr = be32_to_cpu(readl(target));
920 target = calgary_reg(bbar, phboff | 0x800);
921 mck = be32_to_cpu(readl(target));
923 printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
926 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
927 csr, plssr, csmr, mck);
929 /* dump rest of error regs */
930 printk(KERN_EMERG "Calgary: ");
931 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
932 /* err regs are at 0x810 - 0x870 */
933 erroff = (0x810 + (i * 0x10));
934 target = calgary_reg(bbar, phboff | erroff);
935 errregs[i] = be32_to_cpu(readl(target));
936 printk("0x%08x@0x%lx ", errregs[i], erroff);
940 /* root complex status */
941 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
942 rcstat = be32_to_cpu(readl(target));
943 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
944 PHB_ROOT_COMPLEX_STATUS);
947 static void calgary_watchdog(unsigned long data)
949 struct pci_dev *dev = (struct pci_dev *)data;
950 struct iommu_table *tbl = pci_iommu(dev->bus);
951 void __iomem *bbar = tbl->bbar;
953 void __iomem *target;
955 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
956 val32 = be32_to_cpu(readl(target));
958 /* If no error, the agent ID in the CSR is not valid */
959 if (val32 & CSR_AGENT_MASK) {
960 tbl->chip_ops->dump_error_regs(tbl);
965 /* Disable bus that caused the error */
966 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
967 PHB_CONFIG_RW_OFFSET);
968 val32 = be32_to_cpu(readl(target));
969 val32 |= PHB_SLOT_DISABLE;
970 writel(cpu_to_be32(val32), target);
971 readl(target); /* flush */
973 /* Reset the timer */
974 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
978 static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
979 unsigned char busnum, unsigned long timeout)
982 void __iomem *target;
983 unsigned int phb_shift = ~0; /* silence gcc */
986 switch (busno_to_phbid(busnum)) {
987 case 0: phb_shift = (63 - 19);
989 case 1: phb_shift = (63 - 23);
991 case 2: phb_shift = (63 - 27);
993 case 3: phb_shift = (63 - 35);
996 BUG_ON(busno_to_phbid(busnum));
999 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
1000 val64 = be64_to_cpu(readq(target));
1002 /* zero out this PHB's timer bits */
1003 mask = ~(0xFUL << phb_shift);
1005 val64 |= (timeout << phb_shift);
1006 writeq(cpu_to_be64(val64), target);
1007 readq(target); /* flush */
1010 static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1012 unsigned char busnum = dev->bus->number;
1013 void __iomem *bbar = tbl->bbar;
1014 void __iomem *target;
1018 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
1020 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
1021 val = cpu_to_be32(readl(target));
1023 writel(cpu_to_be32(val), target);
1026 static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1028 unsigned char busnum = dev->bus->number;
1031 * Give split completion a longer timeout on bus 1 for aic94xx
1032 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
1034 if (is_calgary(dev->device) && (busnum == 1))
1035 calgary_set_split_completion_timeout(tbl->bbar, busnum,
1039 static void __init calgary_enable_translation(struct pci_dev *dev)
1042 unsigned char busnum;
1043 void __iomem *target;
1045 struct iommu_table *tbl;
1047 busnum = dev->bus->number;
1048 tbl = pci_iommu(dev->bus);
1051 /* enable TCE in PHB Config Register */
1052 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1053 val32 = be32_to_cpu(readl(target));
1054 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
1056 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
1057 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
1058 "Calgary" : "CalIOC2", busnum);
1059 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
1062 writel(cpu_to_be32(val32), target);
1063 readl(target); /* flush */
1065 init_timer(&tbl->watchdog_timer);
1066 tbl->watchdog_timer.function = &calgary_watchdog;
1067 tbl->watchdog_timer.data = (unsigned long)dev;
1068 mod_timer(&tbl->watchdog_timer, jiffies);
1071 static void __init calgary_disable_translation(struct pci_dev *dev)
1074 unsigned char busnum;
1075 void __iomem *target;
1077 struct iommu_table *tbl;
1079 busnum = dev->bus->number;
1080 tbl = pci_iommu(dev->bus);
1083 /* disable TCE in PHB Config Register */
1084 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1085 val32 = be32_to_cpu(readl(target));
1086 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1088 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
1089 writel(cpu_to_be32(val32), target);
1090 readl(target); /* flush */
1092 del_timer_sync(&tbl->watchdog_timer);
1095 static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
1098 set_pci_iommu(dev->bus, NULL);
1100 /* is the device behind a bridge? */
1101 if (dev->bus->parent)
1102 dev->bus->parent->self = dev;
1104 dev->bus->self = dev;
1107 static int __init calgary_init_one(struct pci_dev *dev)
1110 struct iommu_table *tbl;
1113 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1115 bbar = busno_to_bbar(dev->bus->number);
1116 ret = calgary_setup_tar(dev, bbar);
1122 if (dev->bus->parent) {
1123 if (dev->bus->parent->self)
1124 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1125 "bus->parent->self!\n", dev);
1126 dev->bus->parent->self = dev;
1128 dev->bus->self = dev;
1130 tbl = pci_iommu(dev->bus);
1131 tbl->chip_ops->handle_quirks(tbl, dev);
1133 calgary_enable_translation(dev);
1141 static int __init calgary_locate_bbars(void)
1144 int rioidx, phb, bus;
1146 void __iomem *target;
1147 unsigned long offset;
1148 u8 start_bus, end_bus;
1152 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1153 struct rio_detail *rio = rio_devs[rioidx];
1155 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
1158 /* map entire 1MB of Calgary config space */
1159 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1163 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
1164 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1165 target = calgary_reg(bbar, offset);
1167 val = be32_to_cpu(readl(target));
1169 start_bus = (u8)((val & 0x00FF0000) >> 16);
1170 end_bus = (u8)((val & 0x0000FF00) >> 8);
1173 for (bus = start_bus; bus <= end_bus; bus++) {
1174 bus_info[bus].bbar = bbar;
1175 bus_info[bus].phbid = phb;
1178 bus_info[start_bus].bbar = bbar;
1179 bus_info[start_bus].phbid = phb;
1187 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1188 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1189 if (bus_info[bus].bbar)
1190 iounmap(bus_info[bus].bbar);
1195 static int __init calgary_init(void)
1198 struct pci_dev *dev = NULL;
1199 struct calgary_bus_info *info;
1201 ret = calgary_locate_bbars();
1206 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1209 if (!is_cal_pci_dev(dev->device))
1212 info = &bus_info[dev->bus->number];
1213 if (info->translation_disabled) {
1214 calgary_init_one_nontraslated(dev);
1218 if (!info->tce_space && !translate_empty_slots)
1221 ret = calgary_init_one(dev);
1230 dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
1234 if (!is_cal_pci_dev(dev->device))
1237 info = &bus_info[dev->bus->number];
1238 if (info->translation_disabled) {
1242 if (!info->tce_space && !translate_empty_slots)
1245 calgary_disable_translation(dev);
1246 calgary_free_bus(dev);
1247 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
1253 static inline int __init determine_tce_table_size(u64 ram)
1257 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1258 return specified_table_size;
1261 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1262 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1263 * larger table size has twice as many entries, so shift the
1264 * max ram address by 13 to divide by 8K and then look at the
1265 * order of the result to choose between 0-7.
1267 ret = get_order(ram >> 13);
1268 if (ret > TCE_TABLE_SIZE_8M)
1269 ret = TCE_TABLE_SIZE_8M;
1274 static int __init build_detail_arrays(void)
1277 int i, scal_detail_size, rio_detail_size;
1279 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1281 "Calgary: MAX_NUMNODES too low! Defined as %d, "
1282 "but system has %d nodes.\n",
1283 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1287 switch (rio_table_hdr->version){
1289 scal_detail_size = 11;
1290 rio_detail_size = 13;
1293 scal_detail_size = 12;
1294 rio_detail_size = 15;
1298 "Calgary: Invalid Rio Grande Table Version: %d\n",
1299 rio_table_hdr->version);
1303 ptr = ((unsigned long)rio_table_hdr) + 3;
1304 for (i = 0; i < rio_table_hdr->num_scal_dev;
1305 i++, ptr += scal_detail_size)
1306 scal_devs[i] = (struct scal_detail *)ptr;
1308 for (i = 0; i < rio_table_hdr->num_rio_dev;
1309 i++, ptr += rio_detail_size)
1310 rio_devs[i] = (struct rio_detail *)ptr;
1315 static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1320 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1322 * FIXME: properly scan for devices accross the
1323 * PCI-to-PCI bridge on every CalIOC2 port.
1328 for (dev = 1; dev < 8; dev++) {
1329 val = read_pci_config(bus, dev, 0, 0);
1330 if (val != 0xffffffff)
1333 return (val != 0xffffffff);
1336 void __init detect_calgary(void)
1340 int calgary_found = 0;
1342 unsigned int offset, prev_offset;
1346 * if the user specified iommu=off or iommu=soft or we found
1347 * another HW IOMMU already, bail out.
1349 if (swiotlb || no_iommu || iommu_detected)
1355 if (!early_pci_allowed())
1358 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1360 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1362 rio_table_hdr = NULL;
1366 * The next offset is stored in the 1st word.
1367 * Only parse up until the offset increases:
1369 while (offset > prev_offset) {
1370 /* The block id is stored in the 2nd word */
1371 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1372 /* set the pointer past the offset & block id */
1373 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
1376 prev_offset = offset;
1377 offset = *((unsigned short *)(ptr + offset));
1379 if (!rio_table_hdr) {
1380 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1381 "in EBDA - bailing!\n");
1385 ret = build_detail_arrays();
1387 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
1391 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
1393 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
1394 struct calgary_bus_info *info = &bus_info[bus];
1395 unsigned short pci_device;
1398 val = read_pci_config(bus, 0, 0, 0);
1399 pci_device = (val & 0xFFFF0000) >> 16;
1401 if (!is_cal_pci_dev(pci_device))
1404 if (info->translation_disabled)
1407 if (calgary_bus_has_devices(bus, pci_device) ||
1408 translate_empty_slots) {
1409 tbl = alloc_tce_table();
1412 info->tce_space = tbl;
1417 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1418 calgary_found ? "found" : "not found");
1420 if (calgary_found) {
1422 calgary_detected = 1;
1423 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1424 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1425 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1426 debugging ? "enabled" : "disabled");
1431 for (--bus; bus >= 0; --bus) {
1432 struct calgary_bus_info *info = &bus_info[bus];
1434 if (info->tce_space)
1435 free_tce_table(info->tce_space);
1439 int __init calgary_iommu_init(void)
1443 if (no_iommu || swiotlb)
1446 if (!calgary_detected)
1449 /* ok, we're trying to use Calgary - let's roll */
1450 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1452 ret = calgary_init();
1454 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1455 "falling back to no_iommu\n", ret);
1456 if (end_pfn > MAX_DMA32_PFN)
1457 printk(KERN_ERR "WARNING more than 4GB of memory, "
1458 "32bit PCI may malfunction.\n");
1463 bad_dma_address = 0x0;
1464 dma_ops = &calgary_dma_ops;
1469 static int __init calgary_parse_options(char *p)
1471 unsigned int bridge;
1476 if (!strncmp(p, "64k", 3))
1477 specified_table_size = TCE_TABLE_SIZE_64K;
1478 else if (!strncmp(p, "128k", 4))
1479 specified_table_size = TCE_TABLE_SIZE_128K;
1480 else if (!strncmp(p, "256k", 4))
1481 specified_table_size = TCE_TABLE_SIZE_256K;
1482 else if (!strncmp(p, "512k", 4))
1483 specified_table_size = TCE_TABLE_SIZE_512K;
1484 else if (!strncmp(p, "1M", 2))
1485 specified_table_size = TCE_TABLE_SIZE_1M;
1486 else if (!strncmp(p, "2M", 2))
1487 specified_table_size = TCE_TABLE_SIZE_2M;
1488 else if (!strncmp(p, "4M", 2))
1489 specified_table_size = TCE_TABLE_SIZE_4M;
1490 else if (!strncmp(p, "8M", 2))
1491 specified_table_size = TCE_TABLE_SIZE_8M;
1493 len = strlen("translate_empty_slots");
1494 if (!strncmp(p, "translate_empty_slots", len))
1495 translate_empty_slots = 1;
1497 len = strlen("disable");
1498 if (!strncmp(p, "disable", len)) {
1504 bridge = simple_strtol(p, &endp, 0);
1508 if (bridge < MAX_PHB_BUS_NUM) {
1509 printk(KERN_INFO "Calgary: disabling "
1510 "translation for PHB %#x\n", bridge);
1511 bus_info[bridge].translation_disabled = 1;
1515 p = strpbrk(p, ",");
1523 __setup("calgary=", calgary_parse_options);
1525 static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
1527 struct iommu_table *tbl;
1528 unsigned int npages;
1531 tbl = pci_iommu(dev->bus);
1533 for (i = 0; i < 4; i++) {
1534 struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
1536 /* Don't give out TCEs that map MEM resources */
1537 if (!(r->flags & IORESOURCE_MEM))
1540 /* 0-based? we reserve the whole 1st MB anyway */
1544 /* cover the whole region */
1545 npages = (r->end - r->start) >> PAGE_SHIFT;
1548 iommu_range_reserve(tbl, r->start, npages);
1552 static int __init calgary_fixup_tce_spaces(void)
1554 struct pci_dev *dev = NULL;
1555 struct calgary_bus_info *info;
1557 if (no_iommu || swiotlb || !calgary_detected)
1560 printk(KERN_DEBUG "Calgary: fixing up tce spaces\n");
1563 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1566 if (!is_cal_pci_dev(dev->device))
1569 info = &bus_info[dev->bus->number];
1570 if (info->translation_disabled)
1573 if (!info->tce_space)
1576 calgary_fixup_one_tce_space(dev);
1584 * We need to be call after pcibios_assign_resources (fs_initcall level)
1585 * and before device_initcall.
1587 rootfs_initcall(calgary_fixup_tce_spaces);