x86_64: introduce CalIOC2 support
[linux-2.6/verdex.git] / arch / x86_64 / kernel / pci-calgary.c
blob558e68aa6b05ca425ac7b0df6443fedf42257c12
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 /* PHB_CONFIG_RW */
83 #define PHB_TCE_ENABLE 0x20000000
84 #define PHB_SLOT_DISABLE 0x1C000000
85 #define PHB_DAC_DISABLE 0x01000000
86 #define PHB_MEM2_ENABLE 0x00400000
87 #define PHB_MCSR_ENABLE 0x00100000
88 /* TAR (Table Address Register) */
89 #define TAR_SW_BITS 0x0000ffffffff800fUL
90 #define TAR_VALID 0x0000000000000008UL
91 /* CSR (Channel/DMA Status Register) */
92 #define CSR_AGENT_MASK 0xffe0ffff
93 /* CCR (Calgary Configuration Register) */
94 #define CCR_2SEC_TIMEOUT 0x000000000000000EUL
96 #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
97 #define MAX_NUM_CHASSIS 8 /* max number of chassis */
98 /* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
99 #define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
100 #define PHBS_PER_CALGARY 4
102 /* register offsets in Calgary's internal register space */
103 static const unsigned long tar_offsets[] = {
104 0x0580 /* TAR0 */,
105 0x0588 /* TAR1 */,
106 0x0590 /* TAR2 */,
107 0x0598 /* TAR3 */
110 static const unsigned long split_queue_offsets[] = {
111 0x4870 /* SPLIT QUEUE 0 */,
112 0x5870 /* SPLIT QUEUE 1 */,
113 0x6870 /* SPLIT QUEUE 2 */,
114 0x7870 /* SPLIT QUEUE 3 */
117 static const unsigned long phb_offsets[] = {
118 0x8000 /* PHB0 */,
119 0x9000 /* PHB1 */,
120 0xA000 /* PHB2 */,
121 0xB000 /* PHB3 */
124 /* PHB debug registers */
126 static const unsigned long phb_debug_offsets[] = {
127 0x4000 /* PHB 0 DEBUG */,
128 0x5000 /* PHB 1 DEBUG */,
129 0x6000 /* PHB 2 DEBUG */,
130 0x7000 /* PHB 3 DEBUG */
134 * STUFF register for each debug PHB,
135 * byte 1 = start bus number, byte 2 = end bus number
138 #define PHB_DEBUG_STUFF_OFFSET 0x0020
140 #define EMERGENCY_PAGES 32 /* = 128KB */
142 unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
143 static int translate_empty_slots __read_mostly = 0;
144 static int calgary_detected __read_mostly = 0;
146 static struct rio_table_hdr *rio_table_hdr __initdata;
147 static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
148 static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
150 struct calgary_bus_info {
151 void *tce_space;
152 unsigned char translation_disabled;
153 signed char phbid;
154 void __iomem *bbar;
157 static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
158 static void calgary_tce_cache_blast(struct iommu_table *tbl);
160 static struct cal_chipset_ops calgary_chip_ops = {
161 .handle_quirks = calgary_handle_quirks,
162 .tce_cache_blast = calgary_tce_cache_blast
165 static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
167 /* enable this to stress test the chip's TCE cache */
168 #ifdef CONFIG_IOMMU_DEBUG
169 int debugging __read_mostly = 1;
171 static inline unsigned long verify_bit_range(unsigned long* bitmap,
172 int expected, unsigned long start, unsigned long end)
174 unsigned long idx = start;
176 BUG_ON(start >= end);
178 while (idx < end) {
179 if (!!test_bit(idx, bitmap) != expected)
180 return idx;
181 ++idx;
184 /* all bits have the expected value */
185 return ~0UL;
187 #else /* debugging is disabled */
188 int debugging __read_mostly = 0;
190 static inline unsigned long verify_bit_range(unsigned long* bitmap,
191 int expected, unsigned long start, unsigned long end)
193 return ~0UL;
196 #endif /* CONFIG_IOMMU_DEBUG */
198 static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
200 unsigned int npages;
202 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
203 npages >>= PAGE_SHIFT;
205 return npages;
208 static inline int translate_phb(struct pci_dev* dev)
210 int disabled = bus_info[dev->bus->number].translation_disabled;
211 return !disabled;
214 static void iommu_range_reserve(struct iommu_table *tbl,
215 unsigned long start_addr, unsigned int npages)
217 unsigned long index;
218 unsigned long end;
219 unsigned long badbit;
221 index = start_addr >> PAGE_SHIFT;
223 /* bail out if we're asked to reserve a region we don't cover */
224 if (index >= tbl->it_size)
225 return;
227 end = index + npages;
228 if (end > tbl->it_size) /* don't go off the table */
229 end = tbl->it_size;
231 badbit = verify_bit_range(tbl->it_map, 0, index, end);
232 if (badbit != ~0UL) {
233 if (printk_ratelimit())
234 printk(KERN_ERR "Calgary: entry already allocated at "
235 "0x%lx tbl %p dma 0x%lx npages %u\n",
236 badbit, tbl, start_addr, npages);
239 set_bit_string(tbl->it_map, index, npages);
242 static unsigned long iommu_range_alloc(struct iommu_table *tbl,
243 unsigned int npages)
245 unsigned long offset;
247 BUG_ON(npages == 0);
249 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
250 tbl->it_size, npages);
251 if (offset == ~0UL) {
252 tbl->chip_ops->tce_cache_blast(tbl);
253 offset = find_next_zero_string(tbl->it_map, 0,
254 tbl->it_size, npages);
255 if (offset == ~0UL) {
256 printk(KERN_WARNING "Calgary: IOMMU full.\n");
257 if (panic_on_overflow)
258 panic("Calgary: fix the allocator.\n");
259 else
260 return bad_dma_address;
264 set_bit_string(tbl->it_map, offset, npages);
265 tbl->it_hint = offset + npages;
266 BUG_ON(tbl->it_hint > tbl->it_size);
268 return offset;
271 static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
272 unsigned int npages, int direction)
274 unsigned long entry, flags;
275 dma_addr_t ret = bad_dma_address;
277 spin_lock_irqsave(&tbl->it_lock, flags);
279 entry = iommu_range_alloc(tbl, npages);
281 if (unlikely(entry == bad_dma_address))
282 goto error;
284 /* set the return dma address */
285 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
287 /* put the TCEs in the HW table */
288 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
289 direction);
291 spin_unlock_irqrestore(&tbl->it_lock, flags);
293 return ret;
295 error:
296 spin_unlock_irqrestore(&tbl->it_lock, flags);
297 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
298 "iommu %p\n", npages, tbl);
299 return bad_dma_address;
302 static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
303 unsigned int npages)
305 unsigned long entry;
306 unsigned long badbit;
307 unsigned long badend;
309 /* were we called with bad_dma_address? */
310 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
311 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
312 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
313 "address 0x%Lx\n", dma_addr);
314 WARN_ON(1);
315 return;
318 entry = dma_addr >> PAGE_SHIFT;
320 BUG_ON(entry + npages > tbl->it_size);
322 tce_free(tbl, entry, npages);
324 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
325 if (badbit != ~0UL) {
326 if (printk_ratelimit())
327 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
328 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
329 badbit, tbl, dma_addr, entry, npages);
332 __clear_bit_string(tbl->it_map, entry, npages);
335 static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
336 unsigned int npages)
338 unsigned long flags;
340 spin_lock_irqsave(&tbl->it_lock, flags);
342 __iommu_free(tbl, dma_addr, npages);
344 spin_unlock_irqrestore(&tbl->it_lock, flags);
347 static inline struct iommu_table *find_iommu_table(struct device *dev)
349 struct pci_dev *pdev;
350 struct pci_bus *pbus;
351 struct iommu_table *tbl;
353 pdev = to_pci_dev(dev);
355 /* is the device behind a bridge? */
356 if (unlikely(pdev->bus->parent))
357 pbus = pdev->bus->parent;
358 else
359 pbus = pdev->bus;
361 tbl = pbus->self->sysdata;
362 BUG_ON(pdev->bus->parent && (tbl->it_busno != pdev->bus->parent->number));
364 return tbl;
367 static void __calgary_unmap_sg(struct iommu_table *tbl,
368 struct scatterlist *sglist, int nelems, int direction)
370 while (nelems--) {
371 unsigned int npages;
372 dma_addr_t dma = sglist->dma_address;
373 unsigned int dmalen = sglist->dma_length;
375 if (dmalen == 0)
376 break;
378 npages = num_dma_pages(dma, dmalen);
379 __iommu_free(tbl, dma, npages);
380 sglist++;
384 void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
385 int nelems, int direction)
387 unsigned long flags;
388 struct iommu_table *tbl = find_iommu_table(dev);
390 if (!translate_phb(to_pci_dev(dev)))
391 return;
393 spin_lock_irqsave(&tbl->it_lock, flags);
395 __calgary_unmap_sg(tbl, sglist, nelems, direction);
397 spin_unlock_irqrestore(&tbl->it_lock, flags);
400 static int calgary_nontranslate_map_sg(struct device* dev,
401 struct scatterlist *sg, int nelems, int direction)
403 int i;
405 for (i = 0; i < nelems; i++ ) {
406 struct scatterlist *s = &sg[i];
407 BUG_ON(!s->page);
408 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
409 s->dma_length = s->length;
411 return nelems;
414 int calgary_map_sg(struct device *dev, struct scatterlist *sg,
415 int nelems, int direction)
417 struct iommu_table *tbl = find_iommu_table(dev);
418 unsigned long flags;
419 unsigned long vaddr;
420 unsigned int npages;
421 unsigned long entry;
422 int i;
424 if (!translate_phb(to_pci_dev(dev)))
425 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
427 spin_lock_irqsave(&tbl->it_lock, flags);
429 for (i = 0; i < nelems; i++ ) {
430 struct scatterlist *s = &sg[i];
431 BUG_ON(!s->page);
433 vaddr = (unsigned long)page_address(s->page) + s->offset;
434 npages = num_dma_pages(vaddr, s->length);
436 entry = iommu_range_alloc(tbl, npages);
437 if (entry == bad_dma_address) {
438 /* makes sure unmap knows to stop */
439 s->dma_length = 0;
440 goto error;
443 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
445 /* insert into HW table */
446 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
447 direction);
449 s->dma_length = s->length;
452 spin_unlock_irqrestore(&tbl->it_lock, flags);
454 return nelems;
455 error:
456 __calgary_unmap_sg(tbl, sg, nelems, direction);
457 for (i = 0; i < nelems; i++) {
458 sg[i].dma_address = bad_dma_address;
459 sg[i].dma_length = 0;
461 spin_unlock_irqrestore(&tbl->it_lock, flags);
462 return 0;
465 dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
466 size_t size, int direction)
468 dma_addr_t dma_handle = bad_dma_address;
469 unsigned long uaddr;
470 unsigned int npages;
471 struct iommu_table *tbl = find_iommu_table(dev);
473 uaddr = (unsigned long)vaddr;
474 npages = num_dma_pages(uaddr, size);
476 if (translate_phb(to_pci_dev(dev)))
477 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
478 else
479 dma_handle = virt_to_bus(vaddr);
481 return dma_handle;
484 void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
485 size_t size, int direction)
487 struct iommu_table *tbl = find_iommu_table(dev);
488 unsigned int npages;
490 if (!translate_phb(to_pci_dev(dev)))
491 return;
493 npages = num_dma_pages(dma_handle, size);
494 iommu_free(tbl, dma_handle, npages);
497 void* calgary_alloc_coherent(struct device *dev, size_t size,
498 dma_addr_t *dma_handle, gfp_t flag)
500 void *ret = NULL;
501 dma_addr_t mapping;
502 unsigned int npages, order;
503 struct iommu_table *tbl = find_iommu_table(dev);
505 size = PAGE_ALIGN(size); /* size rounded up to full pages */
506 npages = size >> PAGE_SHIFT;
507 order = get_order(size);
509 /* alloc enough pages (and possibly more) */
510 ret = (void *)__get_free_pages(flag, order);
511 if (!ret)
512 goto error;
513 memset(ret, 0, size);
515 if (translate_phb(to_pci_dev(dev))) {
516 /* set up tces to cover the allocated range */
517 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
518 if (mapping == bad_dma_address)
519 goto free;
521 *dma_handle = mapping;
522 } else /* non translated slot */
523 *dma_handle = virt_to_bus(ret);
525 return ret;
527 free:
528 free_pages((unsigned long)ret, get_order(size));
529 ret = NULL;
530 error:
531 return ret;
534 static const struct dma_mapping_ops calgary_dma_ops = {
535 .alloc_coherent = calgary_alloc_coherent,
536 .map_single = calgary_map_single,
537 .unmap_single = calgary_unmap_single,
538 .map_sg = calgary_map_sg,
539 .unmap_sg = calgary_unmap_sg,
542 static inline void __iomem * busno_to_bbar(unsigned char num)
544 return bus_info[num].bbar;
547 static inline int busno_to_phbid(unsigned char num)
549 return bus_info[num].phbid;
552 static inline unsigned long split_queue_offset(unsigned char num)
554 size_t idx = busno_to_phbid(num);
556 return split_queue_offsets[idx];
559 static inline unsigned long tar_offset(unsigned char num)
561 size_t idx = busno_to_phbid(num);
563 return tar_offsets[idx];
566 static inline unsigned long phb_offset(unsigned char num)
568 size_t idx = busno_to_phbid(num);
570 return phb_offsets[idx];
573 static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
575 unsigned long target = ((unsigned long)bar) | offset;
576 return (void __iomem*)target;
579 static inline int is_calioc2(unsigned short device)
581 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
584 static inline int is_calgary(unsigned short device)
586 return (device == PCI_DEVICE_ID_IBM_CALGARY);
589 static inline int is_cal_pci_dev(unsigned short device)
591 return (is_calgary(device) || is_calioc2(device));
594 static void calgary_tce_cache_blast(struct iommu_table *tbl)
596 u64 val;
597 u32 aer;
598 int i = 0;
599 void __iomem *bbar = tbl->bbar;
600 void __iomem *target;
602 /* disable arbitration on the bus */
603 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
604 aer = readl(target);
605 writel(0, target);
607 /* read plssr to ensure it got there */
608 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
609 val = readl(target);
611 /* poll split queues until all DMA activity is done */
612 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
613 do {
614 val = readq(target);
615 i++;
616 } while ((val & 0xff) != 0xff && i < 100);
617 if (i == 100)
618 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
619 "continuing anyway\n");
621 /* invalidate TCE cache */
622 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
623 writeq(tbl->tar_val, target);
625 /* enable arbitration */
626 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
627 writel(aer, target);
628 (void)readl(target); /* flush */
631 static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
632 u64 limit)
634 unsigned int numpages;
636 limit = limit | 0xfffff;
637 limit++;
639 numpages = ((limit - start) >> PAGE_SHIFT);
640 iommu_range_reserve(dev->sysdata, start, numpages);
643 static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
645 void __iomem *target;
646 u64 low, high, sizelow;
647 u64 start, limit;
648 struct iommu_table *tbl = dev->sysdata;
649 unsigned char busnum = dev->bus->number;
650 void __iomem *bbar = tbl->bbar;
652 /* peripheral MEM_1 region */
653 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
654 low = be32_to_cpu(readl(target));
655 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
656 high = be32_to_cpu(readl(target));
657 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
658 sizelow = be32_to_cpu(readl(target));
660 start = (high << 32) | low;
661 limit = sizelow;
663 calgary_reserve_mem_region(dev, start, limit);
666 static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
668 void __iomem *target;
669 u32 val32;
670 u64 low, high, sizelow, sizehigh;
671 u64 start, limit;
672 struct iommu_table *tbl = dev->sysdata;
673 unsigned char busnum = dev->bus->number;
674 void __iomem *bbar = tbl->bbar;
676 /* is it enabled? */
677 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
678 val32 = be32_to_cpu(readl(target));
679 if (!(val32 & PHB_MEM2_ENABLE))
680 return;
682 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
683 low = be32_to_cpu(readl(target));
684 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
685 high = be32_to_cpu(readl(target));
686 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
687 sizelow = be32_to_cpu(readl(target));
688 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
689 sizehigh = be32_to_cpu(readl(target));
691 start = (high << 32) | low;
692 limit = (sizehigh << 32) | sizelow;
694 calgary_reserve_mem_region(dev, start, limit);
698 * some regions of the IO address space do not get translated, so we
699 * must not give devices IO addresses in those regions. The regions
700 * are the 640KB-1MB region and the two PCI peripheral memory holes.
701 * Reserve all of them in the IOMMU bitmap to avoid giving them out
702 * later.
704 static void __init calgary_reserve_regions(struct pci_dev *dev)
706 unsigned int npages;
707 u64 start;
708 struct iommu_table *tbl = dev->sysdata;
710 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
711 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
713 /* avoid the BIOS/VGA first 640KB-1MB region */
714 /* for CalIOC2 - avoid the entire first 2MB */
715 if (is_calgary(dev->device)) {
716 start = (640 * 1024);
717 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
718 } else { /* calioc2 */
719 start = 0;
720 npages = (2 * 1024 * 1024) >> PAGE_SHIFT;
722 iommu_range_reserve(tbl, start, npages);
724 /* reserve the two PCI peripheral memory regions in IO space */
725 calgary_reserve_peripheral_mem_1(dev);
726 calgary_reserve_peripheral_mem_2(dev);
729 static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
731 u64 val64;
732 u64 table_phys;
733 void __iomem *target;
734 int ret;
735 struct iommu_table *tbl;
737 /* build TCE tables for each PHB */
738 ret = build_tce_table(dev, bbar);
739 if (ret)
740 return ret;
742 tbl = dev->sysdata;
743 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
744 tce_free(tbl, 0, tbl->it_size);
746 tbl->chip_ops = &calgary_chip_ops;
748 calgary_reserve_regions(dev);
750 /* set TARs for each PHB */
751 target = calgary_reg(bbar, tar_offset(dev->bus->number));
752 val64 = be64_to_cpu(readq(target));
754 /* zero out all TAR bits under sw control */
755 val64 &= ~TAR_SW_BITS;
756 table_phys = (u64)__pa(tbl->it_base);
758 val64 |= table_phys;
760 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
761 val64 |= (u64) specified_table_size;
763 tbl->tar_val = cpu_to_be64(val64);
765 writeq(tbl->tar_val, target);
766 readq(target); /* flush */
768 return 0;
771 static void __init calgary_free_bus(struct pci_dev *dev)
773 u64 val64;
774 struct iommu_table *tbl = dev->sysdata;
775 void __iomem *target;
776 unsigned int bitmapsz;
778 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
779 val64 = be64_to_cpu(readq(target));
780 val64 &= ~TAR_SW_BITS;
781 writeq(cpu_to_be64(val64), target);
782 readq(target); /* flush */
784 bitmapsz = tbl->it_size / BITS_PER_BYTE;
785 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
786 tbl->it_map = NULL;
788 kfree(tbl);
789 dev->sysdata = NULL;
791 /* Can't free bootmem allocated memory after system is up :-( */
792 bus_info[dev->bus->number].tce_space = NULL;
795 static void calgary_dump_error_regs(struct iommu_table *tbl)
797 void __iomem *bbar = tbl->bbar;
798 u32 csr, csmr, plssr, mck;
799 void __iomem *target;
800 unsigned long phboff = phb_offset(tbl->it_busno);
801 unsigned long erroff;
802 u32 errregs[7];
803 int i;
805 /* dump CSR */
806 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
807 csr = be32_to_cpu(readl(target));
808 /* dump PLSSR */
809 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
810 plssr = be32_to_cpu(readl(target));
811 /* dump CSMR */
812 target = calgary_reg(bbar, phboff | 0x290);
813 csmr = be32_to_cpu(readl(target));
814 /* dump mck */
815 target = calgary_reg(bbar, phboff | 0x800);
816 mck = be32_to_cpu(readl(target));
818 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR "
819 "0x%08x@MCK\n", csr, plssr, csmr, mck);
821 /* dump rest of error regs */
822 printk(KERN_EMERG "Calgary: ");
823 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
824 erroff = (0x810 + (i * 0x10)); /* err regs are at 0x810 - 0x870 */
825 target = calgary_reg(bbar, phboff | erroff);
826 errregs[i] = be32_to_cpu(readl(target));
827 printk("0x%08x@0x%lx ", errregs[i], erroff);
829 printk("\n");
832 static void calgary_watchdog(unsigned long data)
834 struct pci_dev *dev = (struct pci_dev *)data;
835 struct iommu_table *tbl = dev->sysdata;
836 void __iomem *bbar = tbl->bbar;
837 u32 val32;
838 void __iomem *target;
840 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
841 val32 = be32_to_cpu(readl(target));
843 /* If no error, the agent ID in the CSR is not valid */
844 if (val32 & CSR_AGENT_MASK) {
845 printk(KERN_EMERG "Calgary: DMA error on PHB %#x\n",
846 dev->bus->number);
847 calgary_dump_error_regs(tbl);
849 /* reset error */
850 writel(0, target);
852 /* Disable bus that caused the error */
853 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
854 PHB_CONFIG_RW_OFFSET);
855 val32 = be32_to_cpu(readl(target));
856 val32 |= PHB_SLOT_DISABLE;
857 writel(cpu_to_be32(val32), target);
858 readl(target); /* flush */
859 } else {
860 /* Reset the timer */
861 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
865 static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
866 unsigned char busnum, unsigned long timeout)
868 u64 val64;
869 void __iomem *target;
870 unsigned int phb_shift = ~0; /* silence gcc */
871 u64 mask;
873 switch (busno_to_phbid(busnum)) {
874 case 0: phb_shift = (63 - 19);
875 break;
876 case 1: phb_shift = (63 - 23);
877 break;
878 case 2: phb_shift = (63 - 27);
879 break;
880 case 3: phb_shift = (63 - 35);
881 break;
882 default:
883 BUG_ON(busno_to_phbid(busnum));
886 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
887 val64 = be64_to_cpu(readq(target));
889 /* zero out this PHB's timer bits */
890 mask = ~(0xFUL << phb_shift);
891 val64 &= mask;
892 val64 |= (timeout << phb_shift);
893 writeq(cpu_to_be64(val64), target);
894 readq(target); /* flush */
897 static void __init calgary_handle_quirks(struct iommu_table *tbl,
898 struct pci_dev *dev)
900 unsigned char busnum = dev->bus->number;
903 * Give split completion a longer timeout on bus 1 for aic94xx
904 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
906 if (busnum == 1)
907 calgary_set_split_completion_timeout(tbl->bbar, busnum,
908 CCR_2SEC_TIMEOUT);
911 static void __init calgary_enable_translation(struct pci_dev *dev)
913 u32 val32;
914 unsigned char busnum;
915 void __iomem *target;
916 void __iomem *bbar;
917 struct iommu_table *tbl;
919 busnum = dev->bus->number;
920 tbl = dev->sysdata;
921 bbar = tbl->bbar;
923 /* enable TCE in PHB Config Register */
924 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
925 val32 = be32_to_cpu(readl(target));
926 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
928 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
929 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
930 "Calgary" : "CalIOC2", busnum);
931 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
932 "bus.\n");
934 writel(cpu_to_be32(val32), target);
935 readl(target); /* flush */
937 init_timer(&tbl->watchdog_timer);
938 tbl->watchdog_timer.function = &calgary_watchdog;
939 tbl->watchdog_timer.data = (unsigned long)dev;
940 mod_timer(&tbl->watchdog_timer, jiffies);
943 static void __init calgary_disable_translation(struct pci_dev *dev)
945 u32 val32;
946 unsigned char busnum;
947 void __iomem *target;
948 void __iomem *bbar;
949 struct iommu_table *tbl;
951 busnum = dev->bus->number;
952 tbl = dev->sysdata;
953 bbar = tbl->bbar;
955 /* disable TCE in PHB Config Register */
956 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
957 val32 = be32_to_cpu(readl(target));
958 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
960 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
961 writel(cpu_to_be32(val32), target);
962 readl(target); /* flush */
964 del_timer_sync(&tbl->watchdog_timer);
967 static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
969 pci_dev_get(dev);
970 dev->sysdata = NULL;
972 /* is the device behind a bridge? */
973 if (dev->bus->parent)
974 dev->bus->parent->self = dev;
975 else
976 dev->bus->self = dev;
979 static int __init calgary_init_one(struct pci_dev *dev)
981 void __iomem *bbar;
982 struct iommu_table *tbl;
983 int ret;
985 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
987 bbar = busno_to_bbar(dev->bus->number);
988 ret = calgary_setup_tar(dev, bbar);
989 if (ret)
990 goto done;
992 pci_dev_get(dev);
994 if (dev->bus->parent) {
995 if (dev->bus->parent->self)
996 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
997 "bus->parent->self!\n", dev);
998 dev->bus->parent->self = dev;
999 } else
1000 dev->bus->self = dev;
1002 tbl = dev->sysdata;
1003 tbl->chip_ops->handle_quirks(tbl, dev);
1005 calgary_enable_translation(dev);
1007 return 0;
1009 done:
1010 return ret;
1013 static int __init calgary_locate_bbars(void)
1015 int ret;
1016 int rioidx, phb, bus;
1017 void __iomem *bbar;
1018 void __iomem *target;
1019 unsigned long offset;
1020 u8 start_bus, end_bus;
1021 u32 val;
1023 ret = -ENODATA;
1024 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1025 struct rio_detail *rio = rio_devs[rioidx];
1027 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
1028 continue;
1030 /* map entire 1MB of Calgary config space */
1031 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1032 if (!bbar)
1033 goto error;
1035 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
1036 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1037 target = calgary_reg(bbar, offset);
1039 val = be32_to_cpu(readl(target));
1041 start_bus = (u8)((val & 0x00FF0000) >> 16);
1042 end_bus = (u8)((val & 0x0000FF00) >> 8);
1044 if (end_bus) {
1045 for (bus = start_bus; bus <= end_bus; bus++) {
1046 bus_info[bus].bbar = bbar;
1047 bus_info[bus].phbid = phb;
1049 } else {
1050 bus_info[start_bus].bbar = bbar;
1051 bus_info[start_bus].phbid = phb;
1056 return 0;
1058 error:
1059 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1060 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1061 if (bus_info[bus].bbar)
1062 iounmap(bus_info[bus].bbar);
1064 return ret;
1067 static int __init calgary_init(void)
1069 int ret;
1070 struct pci_dev *dev = NULL;
1071 void* tce_space;
1073 ret = calgary_locate_bbars();
1074 if (ret)
1075 return ret;
1077 do {
1078 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1079 if (!dev)
1080 break;
1081 if (!is_cal_pci_dev(dev->device))
1082 continue;
1083 if (!translate_phb(dev)) {
1084 calgary_init_one_nontraslated(dev);
1085 continue;
1087 tce_space = bus_info[dev->bus->number].tce_space;
1088 if (!tce_space && !translate_empty_slots) {
1089 printk("Calg: %p failed tce_space check\n", dev);
1090 continue;
1092 ret = calgary_init_one(dev);
1093 if (ret)
1094 goto error;
1095 } while (1);
1097 return ret;
1099 error:
1100 do {
1101 dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
1102 PCI_ANY_ID, dev);
1103 if (!dev)
1104 break;
1105 if (!is_cal_pci_dev(dev->device))
1106 continue;
1107 if (!translate_phb(dev)) {
1108 pci_dev_put(dev);
1109 continue;
1111 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
1112 continue;
1114 calgary_disable_translation(dev);
1115 calgary_free_bus(dev);
1116 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
1117 } while (1);
1119 return ret;
1122 static inline int __init determine_tce_table_size(u64 ram)
1124 int ret;
1126 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1127 return specified_table_size;
1130 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1131 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1132 * larger table size has twice as many entries, so shift the
1133 * max ram address by 13 to divide by 8K and then look at the
1134 * order of the result to choose between 0-7.
1136 ret = get_order(ram >> 13);
1137 if (ret > TCE_TABLE_SIZE_8M)
1138 ret = TCE_TABLE_SIZE_8M;
1140 return ret;
1143 static int __init build_detail_arrays(void)
1145 unsigned long ptr;
1146 int i, scal_detail_size, rio_detail_size;
1148 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1149 printk(KERN_WARNING
1150 "Calgary: MAX_NUMNODES too low! Defined as %d, "
1151 "but system has %d nodes.\n",
1152 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1153 return -ENODEV;
1156 switch (rio_table_hdr->version){
1157 case 2:
1158 scal_detail_size = 11;
1159 rio_detail_size = 13;
1160 break;
1161 case 3:
1162 scal_detail_size = 12;
1163 rio_detail_size = 15;
1164 break;
1165 default:
1166 printk(KERN_WARNING
1167 "Calgary: Invalid Rio Grande Table Version: %d\n",
1168 rio_table_hdr->version);
1169 return -EPROTO;
1172 ptr = ((unsigned long)rio_table_hdr) + 3;
1173 for (i = 0; i < rio_table_hdr->num_scal_dev;
1174 i++, ptr += scal_detail_size)
1175 scal_devs[i] = (struct scal_detail *)ptr;
1177 for (i = 0; i < rio_table_hdr->num_rio_dev;
1178 i++, ptr += rio_detail_size)
1179 rio_devs[i] = (struct rio_detail *)ptr;
1181 return 0;
1184 static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1186 int dev;
1187 u32 val;
1189 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1191 * FIXME: properly scan for devices accross the
1192 * PCI-to-PCI bridge on every CalIOC2 port.
1194 return 1;
1197 for (dev = 1; dev < 8; dev++) {
1198 val = read_pci_config(bus, dev, 0, 0);
1199 if (val != 0xffffffff)
1200 break;
1202 return (val != 0xffffffff);
1205 void __init detect_calgary(void)
1207 int bus;
1208 void *tbl;
1209 int calgary_found = 0;
1210 unsigned long ptr;
1211 unsigned int offset, prev_offset;
1212 int ret;
1215 * if the user specified iommu=off or iommu=soft or we found
1216 * another HW IOMMU already, bail out.
1218 if (swiotlb || no_iommu || iommu_detected)
1219 return;
1221 if (!use_calgary)
1222 return;
1224 if (!early_pci_allowed())
1225 return;
1227 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1229 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1231 rio_table_hdr = NULL;
1232 prev_offset = 0;
1233 offset = 0x180;
1235 * The next offset is stored in the 1st word.
1236 * Only parse up until the offset increases:
1238 while (offset > prev_offset) {
1239 /* The block id is stored in the 2nd word */
1240 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1241 /* set the pointer past the offset & block id */
1242 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
1243 break;
1245 prev_offset = offset;
1246 offset = *((unsigned short *)(ptr + offset));
1248 if (!rio_table_hdr) {
1249 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1250 "in EBDA - bailing!\n");
1251 return;
1254 ret = build_detail_arrays();
1255 if (ret) {
1256 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
1257 return;
1260 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
1262 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
1263 struct calgary_bus_info *info = &bus_info[bus];
1264 unsigned short pci_device;
1265 u32 val;
1267 val = read_pci_config(bus, 0, 0, 0);
1268 pci_device = (val & 0xFFFF0000) >> 16;
1270 if (!is_cal_pci_dev(pci_device))
1271 continue;
1273 if (info->translation_disabled)
1274 continue;
1276 if (calgary_bus_has_devices(bus, pci_device) ||
1277 translate_empty_slots) {
1278 tbl = alloc_tce_table();
1279 if (!tbl)
1280 goto cleanup;
1281 info->tce_space = tbl;
1282 calgary_found = 1;
1283 printk("Calg: allocated tce_table %p for bus 0x%x\n",
1284 info->tce_space, bus);
1288 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1289 calgary_found ? "found" : "not found");
1291 if (calgary_found) {
1292 iommu_detected = 1;
1293 calgary_detected = 1;
1294 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1295 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1296 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1297 debugging ? "enabled" : "disabled");
1299 return;
1301 cleanup:
1302 for (--bus; bus >= 0; --bus) {
1303 struct calgary_bus_info *info = &bus_info[bus];
1305 if (info->tce_space)
1306 free_tce_table(info->tce_space);
1310 int __init calgary_iommu_init(void)
1312 int ret;
1314 if (no_iommu || swiotlb)
1315 return -ENODEV;
1317 if (!calgary_detected)
1318 return -ENODEV;
1320 /* ok, we're trying to use Calgary - let's roll */
1321 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1323 ret = calgary_init();
1324 if (ret) {
1325 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1326 "falling back to no_iommu\n", ret);
1327 if (end_pfn > MAX_DMA32_PFN)
1328 printk(KERN_ERR "WARNING more than 4GB of memory, "
1329 "32bit PCI may malfunction.\n");
1330 return ret;
1333 force_iommu = 1;
1334 bad_dma_address = 0x0;
1335 dma_ops = &calgary_dma_ops;
1337 return 0;
1340 static int __init calgary_parse_options(char *p)
1342 unsigned int bridge;
1343 size_t len;
1344 char* endp;
1346 while (*p) {
1347 if (!strncmp(p, "64k", 3))
1348 specified_table_size = TCE_TABLE_SIZE_64K;
1349 else if (!strncmp(p, "128k", 4))
1350 specified_table_size = TCE_TABLE_SIZE_128K;
1351 else if (!strncmp(p, "256k", 4))
1352 specified_table_size = TCE_TABLE_SIZE_256K;
1353 else if (!strncmp(p, "512k", 4))
1354 specified_table_size = TCE_TABLE_SIZE_512K;
1355 else if (!strncmp(p, "1M", 2))
1356 specified_table_size = TCE_TABLE_SIZE_1M;
1357 else if (!strncmp(p, "2M", 2))
1358 specified_table_size = TCE_TABLE_SIZE_2M;
1359 else if (!strncmp(p, "4M", 2))
1360 specified_table_size = TCE_TABLE_SIZE_4M;
1361 else if (!strncmp(p, "8M", 2))
1362 specified_table_size = TCE_TABLE_SIZE_8M;
1364 len = strlen("translate_empty_slots");
1365 if (!strncmp(p, "translate_empty_slots", len))
1366 translate_empty_slots = 1;
1368 len = strlen("disable");
1369 if (!strncmp(p, "disable", len)) {
1370 p += len;
1371 if (*p == '=')
1372 ++p;
1373 if (*p == '\0')
1374 break;
1375 bridge = simple_strtol(p, &endp, 0);
1376 if (p == endp)
1377 break;
1379 if (bridge < MAX_PHB_BUS_NUM) {
1380 printk(KERN_INFO "Calgary: disabling "
1381 "translation for PHB %#x\n", bridge);
1382 bus_info[bridge].translation_disabled = 1;
1386 p = strpbrk(p, ",");
1387 if (!p)
1388 break;
1390 p++; /* skip ',' */
1392 return 1;
1394 __setup("calgary=", calgary_parse_options);