x86_64: make dump_error_regs a chip op
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86_64 / kernel / pci-calgary.c
blobf5828db15220aab13e4a5ba7c8c698d08f659a61
1 /*
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>
29 #include <linux/mm.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>
40 #include <asm/tce.h>
41 #include <asm/pci-direct.h>
42 #include <asm/system.h>
43 #include <asm/dma.h>
44 #include <asm/rio.h>
46 #ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
47 int use_calgary __read_mostly = 1;
48 #else
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
88 /* PHB_CONFIG_RW */
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[] = {
114 0x0580 /* TAR0 */,
115 0x0588 /* TAR1 */,
116 0x0590 /* TAR2 */,
117 0x0598 /* TAR3 */
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[] = {
128 0x8000 /* PHB0 */,
129 0x9000 /* PHB1 */,
130 0xA000 /* PHB2 */,
131 0xB000 /* PHB3 */
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 {
161 void *tce_space;
162 unsigned char translation_disabled;
163 signed char phbid;
164 void __iomem *bbar;
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);
199 while (idx < end) {
200 if (!!test_bit(idx, bitmap) != expected)
201 return idx;
202 ++idx;
205 /* all bits have the expected value */
206 return ~0UL;
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)
214 return ~0UL;
217 #endif /* CONFIG_IOMMU_DEBUG */
219 static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
221 unsigned int npages;
223 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
224 npages >>= PAGE_SHIFT;
226 return npages;
229 static inline int translate_phb(struct pci_dev* dev)
231 int disabled = bus_info[dev->bus->number].translation_disabled;
232 return !disabled;
235 static void iommu_range_reserve(struct iommu_table *tbl,
236 unsigned long start_addr, unsigned int npages)
238 unsigned long index;
239 unsigned long end;
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)
246 return;
248 end = index + npages;
249 if (end > tbl->it_size) /* don't go off the table */
250 end = tbl->it_size;
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,
264 unsigned int npages)
266 unsigned long offset;
268 BUG_ON(npages == 0);
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");
280 else
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);
289 return offset;
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))
303 goto error;
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,
310 direction);
312 spin_unlock_irqrestore(&tbl->it_lock, flags);
314 return ret;
316 error:
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,
324 unsigned int npages)
326 unsigned long entry;
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);
335 WARN_ON(1);
336 return;
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,
357 unsigned int npages)
359 unsigned long flags;
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;
379 else
380 pbus = pdev->bus;
382 tbl = pbus->self->sysdata;
383 BUG_ON(pdev->bus->parent && (tbl->it_busno != pdev->bus->parent->number));
385 return tbl;
388 static void __calgary_unmap_sg(struct iommu_table *tbl,
389 struct scatterlist *sglist, int nelems, int direction)
391 while (nelems--) {
392 unsigned int npages;
393 dma_addr_t dma = sglist->dma_address;
394 unsigned int dmalen = sglist->dma_length;
396 if (dmalen == 0)
397 break;
399 npages = num_dma_pages(dma, dmalen);
400 __iommu_free(tbl, dma, npages);
401 sglist++;
405 void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
406 int nelems, int direction)
408 unsigned long flags;
409 struct iommu_table *tbl = find_iommu_table(dev);
411 if (!translate_phb(to_pci_dev(dev)))
412 return;
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)
424 int i;
426 for (i = 0; i < nelems; i++ ) {
427 struct scatterlist *s = &sg[i];
428 BUG_ON(!s->page);
429 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
430 s->dma_length = s->length;
432 return nelems;
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);
439 unsigned long flags;
440 unsigned long vaddr;
441 unsigned int npages;
442 unsigned long entry;
443 int i;
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];
452 BUG_ON(!s->page);
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 */
460 s->dma_length = 0;
461 goto error;
464 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
466 /* insert into HW table */
467 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
468 direction);
470 s->dma_length = s->length;
473 spin_unlock_irqrestore(&tbl->it_lock, flags);
475 return nelems;
476 error:
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);
483 return 0;
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;
490 unsigned long uaddr;
491 unsigned int npages;
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);
499 else
500 dma_handle = virt_to_bus(vaddr);
502 return dma_handle;
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);
509 unsigned int npages;
511 if (!translate_phb(to_pci_dev(dev)))
512 return;
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)
521 void *ret = NULL;
522 dma_addr_t mapping;
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);
532 if (!ret)
533 goto error;
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)
540 goto free;
542 *dma_handle = mapping;
543 } else /* non translated slot */
544 *dma_handle = virt_to_bus(ret);
546 return ret;
548 free:
549 free_pages((unsigned long)ret, get_order(size));
550 ret = NULL;
551 error:
552 return ret;
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)
617 u64 val;
618 u32 aer;
619 int i = 0;
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);
625 aer = readl(target);
626 writel(0, target);
628 /* read plssr to ensure it got there */
629 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
630 val = readl(target);
632 /* poll split queues until all DMA activity is done */
633 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
634 do {
635 val = readq(target);
636 i++;
637 } while ((val & 0xff) != 0xff && i < 100);
638 if (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);
648 writel(aer, target);
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;
656 u64 val64;
657 u32 val;
658 int i = 0;
659 int count = 1;
660 unsigned char bus = tbl->it_busno;
662 begin:
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);
670 val |= PMR_SOFTSTOP;
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));
677 do {
678 val64 = readq(target);
679 i++;
680 } while ((val64 & 0xff) != 0xff && i < 100);
681 if (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) {
692 if (++count < 100)
693 goto begin;
694 else {
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);
724 val = 0;
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,
732 u64 limit)
734 unsigned int numpages;
736 limit = limit | 0xfffff;
737 limit++;
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;
747 u64 start, limit;
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;
761 limit = sizelow;
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;
769 u32 val32;
770 u64 low, high, sizelow, sizehigh;
771 u64 start, limit;
772 struct iommu_table *tbl = dev->sysdata;
773 unsigned char busnum = dev->bus->number;
774 void __iomem *bbar = tbl->bbar;
776 /* is it enabled? */
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))
780 return;
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
802 * later.
804 static void __init calgary_reserve_regions(struct pci_dev *dev)
806 unsigned int npages;
807 u64 start;
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 */
819 start = 0;
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)
831 u64 val64;
832 u64 table_phys;
833 void __iomem *target;
834 int ret;
835 struct iommu_table *tbl;
837 /* build TCE tables for each PHB */
838 ret = build_tce_table(dev, bbar);
839 if (ret)
840 return ret;
842 tbl = dev->sysdata;
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;
850 else
851 BUG();
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);
863 val64 |= table_phys;
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 */
873 return 0;
876 static void __init calgary_free_bus(struct pci_dev *dev)
878 u64 val64;
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));
891 tbl->it_map = NULL;
893 kfree(tbl);
894 dev->sysdata = NULL;
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 u32 val32;
904 void __iomem *target;
906 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
907 val32 = be32_to_cpu(readl(target));
909 /* If no error, the agent ID in the CSR is not valid */
910 printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
911 "CSR = 0x%08x\n", tbl->it_busno, val32);
914 static void calioc2_dump_error_regs(struct iommu_table *tbl)
916 void __iomem *bbar = tbl->bbar;
917 u32 csr, csmr, plssr, mck, rcstat;
918 void __iomem *target;
919 unsigned long phboff = phb_offset(tbl->it_busno);
920 unsigned long erroff;
921 u32 errregs[7];
922 int i;
924 /* dump CSR */
925 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
926 csr = be32_to_cpu(readl(target));
927 /* dump PLSSR */
928 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
929 plssr = be32_to_cpu(readl(target));
930 /* dump CSMR */
931 target = calgary_reg(bbar, phboff | 0x290);
932 csmr = be32_to_cpu(readl(target));
933 /* dump mck */
934 target = calgary_reg(bbar, phboff | 0x800);
935 mck = be32_to_cpu(readl(target));
937 printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
938 tbl->it_busno);
940 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
941 csr, plssr, csmr, mck);
943 /* dump rest of error regs */
944 printk(KERN_EMERG "Calgary: ");
945 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
946 erroff = (0x810 + (i * 0x10)); /* err regs are at 0x810 - 0x870 */
947 target = calgary_reg(bbar, phboff | erroff);
948 errregs[i] = be32_to_cpu(readl(target));
949 printk("0x%08x@0x%lx ", errregs[i], erroff);
951 printk("\n");
953 /* root complex status */
954 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
955 rcstat = be32_to_cpu(readl(target));
956 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
957 PHB_ROOT_COMPLEX_STATUS);
960 static void calgary_watchdog(unsigned long data)
962 struct pci_dev *dev = (struct pci_dev *)data;
963 struct iommu_table *tbl = dev->sysdata;
964 void __iomem *bbar = tbl->bbar;
965 u32 val32;
966 void __iomem *target;
968 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
969 val32 = be32_to_cpu(readl(target));
971 /* If no error, the agent ID in the CSR is not valid */
972 if (val32 & CSR_AGENT_MASK) {
973 tbl->chip_ops->dump_error_regs(tbl);
975 /* reset error */
976 writel(0, target);
978 /* Disable bus that caused the error */
979 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
980 PHB_CONFIG_RW_OFFSET);
981 val32 = be32_to_cpu(readl(target));
982 val32 |= PHB_SLOT_DISABLE;
983 writel(cpu_to_be32(val32), target);
984 readl(target); /* flush */
985 } else {
986 /* Reset the timer */
987 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
991 static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
992 unsigned char busnum, unsigned long timeout)
994 u64 val64;
995 void __iomem *target;
996 unsigned int phb_shift = ~0; /* silence gcc */
997 u64 mask;
999 switch (busno_to_phbid(busnum)) {
1000 case 0: phb_shift = (63 - 19);
1001 break;
1002 case 1: phb_shift = (63 - 23);
1003 break;
1004 case 2: phb_shift = (63 - 27);
1005 break;
1006 case 3: phb_shift = (63 - 35);
1007 break;
1008 default:
1009 BUG_ON(busno_to_phbid(busnum));
1012 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
1013 val64 = be64_to_cpu(readq(target));
1015 /* zero out this PHB's timer bits */
1016 mask = ~(0xFUL << phb_shift);
1017 val64 &= mask;
1018 val64 |= (timeout << phb_shift);
1019 writeq(cpu_to_be64(val64), target);
1020 readq(target); /* flush */
1023 static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1025 unsigned char busnum = dev->bus->number;
1026 void __iomem *bbar = tbl->bbar;
1027 void __iomem *target;
1028 u32 val;
1031 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
1033 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
1034 val = cpu_to_be32(readl(target));
1035 val |= 0x00800000;
1036 writel(cpu_to_be32(val), target);
1039 static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1041 unsigned char busnum = dev->bus->number;
1044 * Give split completion a longer timeout on bus 1 for aic94xx
1045 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
1047 if (is_calgary(dev->device) && (busnum == 1))
1048 calgary_set_split_completion_timeout(tbl->bbar, busnum,
1049 CCR_2SEC_TIMEOUT);
1052 static void __init calgary_enable_translation(struct pci_dev *dev)
1054 u32 val32;
1055 unsigned char busnum;
1056 void __iomem *target;
1057 void __iomem *bbar;
1058 struct iommu_table *tbl;
1060 busnum = dev->bus->number;
1061 tbl = dev->sysdata;
1062 bbar = tbl->bbar;
1064 /* enable TCE in PHB Config Register */
1065 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1066 val32 = be32_to_cpu(readl(target));
1067 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
1069 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
1070 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
1071 "Calgary" : "CalIOC2", busnum);
1072 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
1073 "bus.\n");
1075 writel(cpu_to_be32(val32), target);
1076 readl(target); /* flush */
1078 init_timer(&tbl->watchdog_timer);
1079 tbl->watchdog_timer.function = &calgary_watchdog;
1080 tbl->watchdog_timer.data = (unsigned long)dev;
1081 mod_timer(&tbl->watchdog_timer, jiffies);
1084 static void __init calgary_disable_translation(struct pci_dev *dev)
1086 u32 val32;
1087 unsigned char busnum;
1088 void __iomem *target;
1089 void __iomem *bbar;
1090 struct iommu_table *tbl;
1092 busnum = dev->bus->number;
1093 tbl = dev->sysdata;
1094 bbar = tbl->bbar;
1096 /* disable TCE in PHB Config Register */
1097 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1098 val32 = be32_to_cpu(readl(target));
1099 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1101 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
1102 writel(cpu_to_be32(val32), target);
1103 readl(target); /* flush */
1105 del_timer_sync(&tbl->watchdog_timer);
1108 static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
1110 pci_dev_get(dev);
1111 dev->sysdata = NULL;
1113 /* is the device behind a bridge? */
1114 if (dev->bus->parent)
1115 dev->bus->parent->self = dev;
1116 else
1117 dev->bus->self = dev;
1120 static int __init calgary_init_one(struct pci_dev *dev)
1122 void __iomem *bbar;
1123 struct iommu_table *tbl;
1124 int ret;
1126 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1128 bbar = busno_to_bbar(dev->bus->number);
1129 ret = calgary_setup_tar(dev, bbar);
1130 if (ret)
1131 goto done;
1133 pci_dev_get(dev);
1135 if (dev->bus->parent) {
1136 if (dev->bus->parent->self)
1137 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1138 "bus->parent->self!\n", dev);
1139 dev->bus->parent->self = dev;
1140 } else
1141 dev->bus->self = dev;
1143 tbl = dev->sysdata;
1144 tbl->chip_ops->handle_quirks(tbl, dev);
1146 calgary_enable_translation(dev);
1148 return 0;
1150 done:
1151 return ret;
1154 static int __init calgary_locate_bbars(void)
1156 int ret;
1157 int rioidx, phb, bus;
1158 void __iomem *bbar;
1159 void __iomem *target;
1160 unsigned long offset;
1161 u8 start_bus, end_bus;
1162 u32 val;
1164 ret = -ENODATA;
1165 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1166 struct rio_detail *rio = rio_devs[rioidx];
1168 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
1169 continue;
1171 /* map entire 1MB of Calgary config space */
1172 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1173 if (!bbar)
1174 goto error;
1176 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
1177 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1178 target = calgary_reg(bbar, offset);
1180 val = be32_to_cpu(readl(target));
1182 start_bus = (u8)((val & 0x00FF0000) >> 16);
1183 end_bus = (u8)((val & 0x0000FF00) >> 8);
1185 if (end_bus) {
1186 for (bus = start_bus; bus <= end_bus; bus++) {
1187 bus_info[bus].bbar = bbar;
1188 bus_info[bus].phbid = phb;
1190 } else {
1191 bus_info[start_bus].bbar = bbar;
1192 bus_info[start_bus].phbid = phb;
1197 return 0;
1199 error:
1200 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1201 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1202 if (bus_info[bus].bbar)
1203 iounmap(bus_info[bus].bbar);
1205 return ret;
1208 static int __init calgary_init(void)
1210 int ret;
1211 struct pci_dev *dev = NULL;
1212 void* tce_space;
1214 ret = calgary_locate_bbars();
1215 if (ret)
1216 return ret;
1218 do {
1219 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1220 if (!dev)
1221 break;
1222 if (!is_cal_pci_dev(dev->device))
1223 continue;
1224 if (!translate_phb(dev)) {
1225 calgary_init_one_nontraslated(dev);
1226 continue;
1228 tce_space = bus_info[dev->bus->number].tce_space;
1229 if (!tce_space && !translate_empty_slots) {
1230 printk("Calg: %p failed tce_space check\n", dev);
1231 continue;
1233 ret = calgary_init_one(dev);
1234 if (ret)
1235 goto error;
1236 } while (1);
1238 return ret;
1240 error:
1241 do {
1242 dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
1243 PCI_ANY_ID, dev);
1244 if (!dev)
1245 break;
1246 if (!is_cal_pci_dev(dev->device))
1247 continue;
1248 if (!translate_phb(dev)) {
1249 pci_dev_put(dev);
1250 continue;
1252 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
1253 continue;
1255 calgary_disable_translation(dev);
1256 calgary_free_bus(dev);
1257 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
1258 } while (1);
1260 return ret;
1263 static inline int __init determine_tce_table_size(u64 ram)
1265 int ret;
1267 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1268 return specified_table_size;
1271 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1272 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1273 * larger table size has twice as many entries, so shift the
1274 * max ram address by 13 to divide by 8K and then look at the
1275 * order of the result to choose between 0-7.
1277 ret = get_order(ram >> 13);
1278 if (ret > TCE_TABLE_SIZE_8M)
1279 ret = TCE_TABLE_SIZE_8M;
1281 return ret;
1284 static int __init build_detail_arrays(void)
1286 unsigned long ptr;
1287 int i, scal_detail_size, rio_detail_size;
1289 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1290 printk(KERN_WARNING
1291 "Calgary: MAX_NUMNODES too low! Defined as %d, "
1292 "but system has %d nodes.\n",
1293 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1294 return -ENODEV;
1297 switch (rio_table_hdr->version){
1298 case 2:
1299 scal_detail_size = 11;
1300 rio_detail_size = 13;
1301 break;
1302 case 3:
1303 scal_detail_size = 12;
1304 rio_detail_size = 15;
1305 break;
1306 default:
1307 printk(KERN_WARNING
1308 "Calgary: Invalid Rio Grande Table Version: %d\n",
1309 rio_table_hdr->version);
1310 return -EPROTO;
1313 ptr = ((unsigned long)rio_table_hdr) + 3;
1314 for (i = 0; i < rio_table_hdr->num_scal_dev;
1315 i++, ptr += scal_detail_size)
1316 scal_devs[i] = (struct scal_detail *)ptr;
1318 for (i = 0; i < rio_table_hdr->num_rio_dev;
1319 i++, ptr += rio_detail_size)
1320 rio_devs[i] = (struct rio_detail *)ptr;
1322 return 0;
1325 static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1327 int dev;
1328 u32 val;
1330 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1332 * FIXME: properly scan for devices accross the
1333 * PCI-to-PCI bridge on every CalIOC2 port.
1335 return 1;
1338 for (dev = 1; dev < 8; dev++) {
1339 val = read_pci_config(bus, dev, 0, 0);
1340 if (val != 0xffffffff)
1341 break;
1343 return (val != 0xffffffff);
1346 void __init detect_calgary(void)
1348 int bus;
1349 void *tbl;
1350 int calgary_found = 0;
1351 unsigned long ptr;
1352 unsigned int offset, prev_offset;
1353 int ret;
1356 * if the user specified iommu=off or iommu=soft or we found
1357 * another HW IOMMU already, bail out.
1359 if (swiotlb || no_iommu || iommu_detected)
1360 return;
1362 if (!use_calgary)
1363 return;
1365 if (!early_pci_allowed())
1366 return;
1368 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1370 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1372 rio_table_hdr = NULL;
1373 prev_offset = 0;
1374 offset = 0x180;
1376 * The next offset is stored in the 1st word.
1377 * Only parse up until the offset increases:
1379 while (offset > prev_offset) {
1380 /* The block id is stored in the 2nd word */
1381 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1382 /* set the pointer past the offset & block id */
1383 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
1384 break;
1386 prev_offset = offset;
1387 offset = *((unsigned short *)(ptr + offset));
1389 if (!rio_table_hdr) {
1390 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1391 "in EBDA - bailing!\n");
1392 return;
1395 ret = build_detail_arrays();
1396 if (ret) {
1397 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
1398 return;
1401 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
1403 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
1404 struct calgary_bus_info *info = &bus_info[bus];
1405 unsigned short pci_device;
1406 u32 val;
1408 val = read_pci_config(bus, 0, 0, 0);
1409 pci_device = (val & 0xFFFF0000) >> 16;
1411 if (!is_cal_pci_dev(pci_device))
1412 continue;
1414 if (info->translation_disabled)
1415 continue;
1417 if (calgary_bus_has_devices(bus, pci_device) ||
1418 translate_empty_slots) {
1419 tbl = alloc_tce_table();
1420 if (!tbl)
1421 goto cleanup;
1422 info->tce_space = tbl;
1423 calgary_found = 1;
1424 printk("Calg: allocated tce_table %p for bus 0x%x\n",
1425 info->tce_space, bus);
1429 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1430 calgary_found ? "found" : "not found");
1432 if (calgary_found) {
1433 iommu_detected = 1;
1434 calgary_detected = 1;
1435 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1436 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1437 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1438 debugging ? "enabled" : "disabled");
1440 return;
1442 cleanup:
1443 for (--bus; bus >= 0; --bus) {
1444 struct calgary_bus_info *info = &bus_info[bus];
1446 if (info->tce_space)
1447 free_tce_table(info->tce_space);
1451 int __init calgary_iommu_init(void)
1453 int ret;
1455 if (no_iommu || swiotlb)
1456 return -ENODEV;
1458 if (!calgary_detected)
1459 return -ENODEV;
1461 /* ok, we're trying to use Calgary - let's roll */
1462 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1464 ret = calgary_init();
1465 if (ret) {
1466 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1467 "falling back to no_iommu\n", ret);
1468 if (end_pfn > MAX_DMA32_PFN)
1469 printk(KERN_ERR "WARNING more than 4GB of memory, "
1470 "32bit PCI may malfunction.\n");
1471 return ret;
1474 force_iommu = 1;
1475 bad_dma_address = 0x0;
1476 dma_ops = &calgary_dma_ops;
1478 return 0;
1481 static int __init calgary_parse_options(char *p)
1483 unsigned int bridge;
1484 size_t len;
1485 char* endp;
1487 while (*p) {
1488 if (!strncmp(p, "64k", 3))
1489 specified_table_size = TCE_TABLE_SIZE_64K;
1490 else if (!strncmp(p, "128k", 4))
1491 specified_table_size = TCE_TABLE_SIZE_128K;
1492 else if (!strncmp(p, "256k", 4))
1493 specified_table_size = TCE_TABLE_SIZE_256K;
1494 else if (!strncmp(p, "512k", 4))
1495 specified_table_size = TCE_TABLE_SIZE_512K;
1496 else if (!strncmp(p, "1M", 2))
1497 specified_table_size = TCE_TABLE_SIZE_1M;
1498 else if (!strncmp(p, "2M", 2))
1499 specified_table_size = TCE_TABLE_SIZE_2M;
1500 else if (!strncmp(p, "4M", 2))
1501 specified_table_size = TCE_TABLE_SIZE_4M;
1502 else if (!strncmp(p, "8M", 2))
1503 specified_table_size = TCE_TABLE_SIZE_8M;
1505 len = strlen("translate_empty_slots");
1506 if (!strncmp(p, "translate_empty_slots", len))
1507 translate_empty_slots = 1;
1509 len = strlen("disable");
1510 if (!strncmp(p, "disable", len)) {
1511 p += len;
1512 if (*p == '=')
1513 ++p;
1514 if (*p == '\0')
1515 break;
1516 bridge = simple_strtol(p, &endp, 0);
1517 if (p == endp)
1518 break;
1520 if (bridge < MAX_PHB_BUS_NUM) {
1521 printk(KERN_INFO "Calgary: disabling "
1522 "translation for PHB %#x\n", bridge);
1523 bus_info[bridge].translation_disabled = 1;
1527 p = strpbrk(p, ",");
1528 if (!p)
1529 break;
1531 p++; /* skip ',' */
1533 return 1;
1535 __setup("calgary=", calgary_parse_options);