Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Derived from arch/powerpc/kernel/iommu.c |
| 3 | * |
Muli Ben-Yehuda | 9882234 | 2007-07-21 17:10:48 +0200 | [diff] [blame] | 4 | * Copyright IBM Corporation, 2006-2007 |
Jon Mason | d8d2bed | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 5 | * Copyright (C) 2006 Jon Mason <jdmason@kudzu.us> |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 6 | * |
Jon Mason | d8d2bed | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 7 | * Author: Jon Mason <jdmason@kudzu.us> |
Muli Ben-Yehuda | aa0a9f3 | 2006-07-10 17:06:15 +0200 | [diff] [blame] | 8 | * Author: Muli Ben-Yehuda <muli@il.ibm.com> |
| 9 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 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. |
| 14 | * |
| 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. |
| 19 | * |
| 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 |
| 23 | */ |
| 24 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 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> |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 44 | #include <asm/rio.h> |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 45 | |
Muli Ben-Yehuda | bff6547 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 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 */ |
| 51 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 52 | #define PCI_DEVICE_ID_IBM_CALGARY 0x02a1 |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 53 | #define PCI_DEVICE_ID_IBM_CALIOC2 0x0308 |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 54 | |
| 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 |
| 59 | |
| 60 | /* register offsets inside the host bridge space */ |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 61 | #define CALGARY_CONFIG_REG 0x0108 |
| 62 | #define PHB_CSR_OFFSET 0x0110 /* Channel Status */ |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 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 |
| 81 | |
| 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 |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 93 | /* CCR (Calgary Configuration Register) */ |
| 94 | #define CCR_2SEC_TIMEOUT 0x000000000000000EUL |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 95 | |
| 96 | #define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */ |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 97 | #define MAX_NUM_CHASSIS 8 /* max number of chassis */ |
Muli Ben-Yehuda | 4ea8a5d | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 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) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 100 | #define PHBS_PER_CALGARY 4 |
| 101 | |
| 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 */ |
| 108 | }; |
| 109 | |
| 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 */ |
| 115 | }; |
| 116 | |
| 117 | static const unsigned long phb_offsets[] = { |
| 118 | 0x8000 /* PHB0 */, |
| 119 | 0x9000 /* PHB1 */, |
| 120 | 0xA000 /* PHB2 */, |
| 121 | 0xB000 /* PHB3 */ |
| 122 | }; |
| 123 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 124 | /* PHB debug registers */ |
| 125 | |
| 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 */ |
| 131 | }; |
| 132 | |
| 133 | /* |
| 134 | * STUFF register for each debug PHB, |
| 135 | * byte 1 = start bus number, byte 2 = end bus number |
| 136 | */ |
| 137 | |
| 138 | #define PHB_DEBUG_STUFF_OFFSET 0x0020 |
| 139 | |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 140 | #define EMERGENCY_PAGES 32 /* = 128KB */ |
| 141 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 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; |
| 145 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 146 | static struct rio_table_hdr *rio_table_hdr __initdata; |
| 147 | static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 148 | static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 149 | |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 150 | struct calgary_bus_info { |
| 151 | void *tce_space; |
Muli Ben-Yehuda | 0577f148 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 152 | unsigned char translation_disabled; |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 153 | signed char phbid; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 154 | void __iomem *bbar; |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 155 | }; |
| 156 | |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 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); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 159 | |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 160 | static struct cal_chipset_ops calgary_chip_ops = { |
| 161 | .handle_quirks = calgary_handle_quirks, |
| 162 | .tce_cache_blast = calgary_tce_cache_blast |
| 163 | }; |
| 164 | |
| 165 | static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, }; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 166 | |
| 167 | /* enable this to stress test the chip's TCE cache */ |
| 168 | #ifdef CONFIG_IOMMU_DEBUG |
Muli Ben-Yehuda | de68465 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 169 | int debugging __read_mostly = 1; |
| 170 | |
Muli Ben-Yehuda | 796e439 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 171 | static inline unsigned long verify_bit_range(unsigned long* bitmap, |
| 172 | int expected, unsigned long start, unsigned long end) |
| 173 | { |
| 174 | unsigned long idx = start; |
| 175 | |
| 176 | BUG_ON(start >= end); |
| 177 | |
| 178 | while (idx < end) { |
| 179 | if (!!test_bit(idx, bitmap) != expected) |
| 180 | return idx; |
| 181 | ++idx; |
| 182 | } |
| 183 | |
| 184 | /* all bits have the expected value */ |
| 185 | return ~0UL; |
| 186 | } |
Muli Ben-Yehuda | de68465 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 187 | #else /* debugging is disabled */ |
| 188 | int debugging __read_mostly = 0; |
| 189 | |
Muli Ben-Yehuda | 796e439 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 190 | static inline unsigned long verify_bit_range(unsigned long* bitmap, |
| 191 | int expected, unsigned long start, unsigned long end) |
| 192 | { |
| 193 | return ~0UL; |
| 194 | } |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 195 | |
Muli Ben-Yehuda | de68465 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 196 | #endif /* CONFIG_IOMMU_DEBUG */ |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 197 | |
| 198 | static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen) |
| 199 | { |
| 200 | unsigned int npages; |
| 201 | |
| 202 | npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK); |
| 203 | npages >>= PAGE_SHIFT; |
| 204 | |
| 205 | return npages; |
| 206 | } |
| 207 | |
| 208 | static inline int translate_phb(struct pci_dev* dev) |
| 209 | { |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 210 | int disabled = bus_info[dev->bus->number].translation_disabled; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 211 | return !disabled; |
| 212 | } |
| 213 | |
| 214 | static void iommu_range_reserve(struct iommu_table *tbl, |
| 215 | unsigned long start_addr, unsigned int npages) |
| 216 | { |
| 217 | unsigned long index; |
| 218 | unsigned long end; |
Muli Ben-Yehuda | 796e439 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 219 | unsigned long badbit; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 220 | |
| 221 | index = start_addr >> PAGE_SHIFT; |
| 222 | |
| 223 | /* bail out if we're asked to reserve a region we don't cover */ |
| 224 | if (index >= tbl->it_size) |
| 225 | return; |
| 226 | |
| 227 | end = index + npages; |
| 228 | if (end > tbl->it_size) /* don't go off the table */ |
| 229 | end = tbl->it_size; |
| 230 | |
Muli Ben-Yehuda | 796e439 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 231 | badbit = verify_bit_range(tbl->it_map, 0, index, end); |
| 232 | if (badbit != ~0UL) { |
| 233 | if (printk_ratelimit()) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 234 | printk(KERN_ERR "Calgary: entry already allocated at " |
| 235 | "0x%lx tbl %p dma 0x%lx npages %u\n", |
Muli Ben-Yehuda | 796e439 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 236 | badbit, tbl, start_addr, npages); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 237 | } |
Muli Ben-Yehuda | 796e439 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 238 | |
| 239 | set_bit_string(tbl->it_map, index, npages); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static unsigned long iommu_range_alloc(struct iommu_table *tbl, |
| 243 | unsigned int npages) |
| 244 | { |
| 245 | unsigned long offset; |
| 246 | |
| 247 | BUG_ON(npages == 0); |
| 248 | |
| 249 | offset = find_next_zero_string(tbl->it_map, tbl->it_hint, |
| 250 | tbl->it_size, npages); |
| 251 | if (offset == ~0UL) { |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 252 | tbl->chip_ops->tce_cache_blast(tbl); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 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; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | set_bit_string(tbl->it_map, offset, npages); |
| 265 | tbl->it_hint = offset + npages; |
| 266 | BUG_ON(tbl->it_hint > tbl->it_size); |
| 267 | |
| 268 | return offset; |
| 269 | } |
| 270 | |
| 271 | static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr, |
| 272 | unsigned int npages, int direction) |
| 273 | { |
| 274 | unsigned long entry, flags; |
| 275 | dma_addr_t ret = bad_dma_address; |
| 276 | |
| 277 | spin_lock_irqsave(&tbl->it_lock, flags); |
| 278 | |
| 279 | entry = iommu_range_alloc(tbl, npages); |
| 280 | |
| 281 | if (unlikely(entry == bad_dma_address)) |
| 282 | goto error; |
| 283 | |
| 284 | /* set the return dma address */ |
| 285 | ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK); |
| 286 | |
| 287 | /* put the TCEs in the HW table */ |
| 288 | tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK, |
| 289 | direction); |
| 290 | |
| 291 | spin_unlock_irqrestore(&tbl->it_lock, flags); |
| 292 | |
| 293 | return ret; |
| 294 | |
| 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; |
| 300 | } |
| 301 | |
| 302 | static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr, |
| 303 | unsigned int npages) |
| 304 | { |
| 305 | unsigned long entry; |
Muli Ben-Yehuda | 796e439 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 306 | unsigned long badbit; |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 307 | unsigned long badend; |
| 308 | |
| 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; |
| 316 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 317 | |
| 318 | entry = dma_addr >> PAGE_SHIFT; |
| 319 | |
| 320 | BUG_ON(entry + npages > tbl->it_size); |
| 321 | |
| 322 | tce_free(tbl, entry, npages); |
| 323 | |
Muli Ben-Yehuda | 796e439 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 324 | badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages); |
| 325 | if (badbit != ~0UL) { |
| 326 | if (printk_ratelimit()) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 327 | printk(KERN_ERR "Calgary: bit is off at 0x%lx " |
| 328 | "tbl %p dma 0x%Lx entry 0x%lx npages %u\n", |
Muli Ben-Yehuda | 796e439 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 329 | badbit, tbl, dma_addr, entry, npages); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | __clear_bit_string(tbl->it_map, entry, npages); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr, |
| 336 | unsigned int npages) |
| 337 | { |
| 338 | unsigned long flags; |
| 339 | |
| 340 | spin_lock_irqsave(&tbl->it_lock, flags); |
| 341 | |
| 342 | __iommu_free(tbl, dma_addr, npages); |
| 343 | |
| 344 | spin_unlock_irqrestore(&tbl->it_lock, flags); |
| 345 | } |
| 346 | |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 347 | static inline struct iommu_table *find_iommu_table(struct device *dev) |
| 348 | { |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 349 | struct pci_dev *pdev; |
| 350 | struct pci_bus *pbus; |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 351 | struct iommu_table *tbl; |
| 352 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 353 | pdev = to_pci_dev(dev); |
| 354 | |
| 355 | /* is the device behind a bridge? */ |
| 356 | if (unlikely(pdev->bus->parent)) |
| 357 | pbus = pdev->bus->parent; |
| 358 | else |
| 359 | pbus = pdev->bus; |
| 360 | |
| 361 | tbl = pbus->self->sysdata; |
| 362 | BUG_ON(pdev->bus->parent && (tbl->it_busno != pdev->bus->parent->number)); |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 363 | |
| 364 | return tbl; |
| 365 | } |
| 366 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 367 | static void __calgary_unmap_sg(struct iommu_table *tbl, |
| 368 | struct scatterlist *sglist, int nelems, int direction) |
| 369 | { |
| 370 | while (nelems--) { |
| 371 | unsigned int npages; |
| 372 | dma_addr_t dma = sglist->dma_address; |
| 373 | unsigned int dmalen = sglist->dma_length; |
| 374 | |
| 375 | if (dmalen == 0) |
| 376 | break; |
| 377 | |
| 378 | npages = num_dma_pages(dma, dmalen); |
| 379 | __iommu_free(tbl, dma, npages); |
| 380 | sglist++; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 385 | int nelems, int direction) |
| 386 | { |
| 387 | unsigned long flags; |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 388 | struct iommu_table *tbl = find_iommu_table(dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 389 | |
| 390 | if (!translate_phb(to_pci_dev(dev))) |
| 391 | return; |
| 392 | |
| 393 | spin_lock_irqsave(&tbl->it_lock, flags); |
| 394 | |
| 395 | __calgary_unmap_sg(tbl, sglist, nelems, direction); |
| 396 | |
| 397 | spin_unlock_irqrestore(&tbl->it_lock, flags); |
| 398 | } |
| 399 | |
| 400 | static int calgary_nontranslate_map_sg(struct device* dev, |
| 401 | struct scatterlist *sg, int nelems, int direction) |
| 402 | { |
| 403 | int i; |
| 404 | |
| 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; |
| 410 | } |
| 411 | return nelems; |
| 412 | } |
| 413 | |
| 414 | int calgary_map_sg(struct device *dev, struct scatterlist *sg, |
| 415 | int nelems, int direction) |
| 416 | { |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 417 | struct iommu_table *tbl = find_iommu_table(dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 418 | unsigned long flags; |
| 419 | unsigned long vaddr; |
| 420 | unsigned int npages; |
| 421 | unsigned long entry; |
| 422 | int i; |
| 423 | |
| 424 | if (!translate_phb(to_pci_dev(dev))) |
| 425 | return calgary_nontranslate_map_sg(dev, sg, nelems, direction); |
| 426 | |
| 427 | spin_lock_irqsave(&tbl->it_lock, flags); |
| 428 | |
| 429 | for (i = 0; i < nelems; i++ ) { |
| 430 | struct scatterlist *s = &sg[i]; |
| 431 | BUG_ON(!s->page); |
| 432 | |
| 433 | vaddr = (unsigned long)page_address(s->page) + s->offset; |
| 434 | npages = num_dma_pages(vaddr, s->length); |
| 435 | |
| 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; |
| 441 | } |
| 442 | |
| 443 | s->dma_address = (entry << PAGE_SHIFT) | s->offset; |
| 444 | |
| 445 | /* insert into HW table */ |
| 446 | tce_build(tbl, entry, npages, vaddr & PAGE_MASK, |
| 447 | direction); |
| 448 | |
| 449 | s->dma_length = s->length; |
| 450 | } |
| 451 | |
| 452 | spin_unlock_irqrestore(&tbl->it_lock, flags); |
| 453 | |
| 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; |
| 460 | } |
| 461 | spin_unlock_irqrestore(&tbl->it_lock, flags); |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | dma_addr_t calgary_map_single(struct device *dev, void *vaddr, |
| 466 | size_t size, int direction) |
| 467 | { |
| 468 | dma_addr_t dma_handle = bad_dma_address; |
| 469 | unsigned long uaddr; |
| 470 | unsigned int npages; |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 471 | struct iommu_table *tbl = find_iommu_table(dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 472 | |
| 473 | uaddr = (unsigned long)vaddr; |
| 474 | npages = num_dma_pages(uaddr, size); |
| 475 | |
| 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); |
| 480 | |
| 481 | return dma_handle; |
| 482 | } |
| 483 | |
| 484 | void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle, |
| 485 | size_t size, int direction) |
| 486 | { |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 487 | struct iommu_table *tbl = find_iommu_table(dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 488 | unsigned int npages; |
| 489 | |
| 490 | if (!translate_phb(to_pci_dev(dev))) |
| 491 | return; |
| 492 | |
| 493 | npages = num_dma_pages(dma_handle, size); |
| 494 | iommu_free(tbl, dma_handle, npages); |
| 495 | } |
| 496 | |
| 497 | void* calgary_alloc_coherent(struct device *dev, size_t size, |
| 498 | dma_addr_t *dma_handle, gfp_t flag) |
| 499 | { |
| 500 | void *ret = NULL; |
| 501 | dma_addr_t mapping; |
| 502 | unsigned int npages, order; |
Muli Ben-Yehuda | 35b6dfa | 2007-07-21 17:10:51 +0200 | [diff] [blame] | 503 | struct iommu_table *tbl = find_iommu_table(dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 504 | |
| 505 | size = PAGE_ALIGN(size); /* size rounded up to full pages */ |
| 506 | npages = size >> PAGE_SHIFT; |
| 507 | order = get_order(size); |
| 508 | |
| 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); |
| 514 | |
| 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; |
| 520 | |
| 521 | *dma_handle = mapping; |
| 522 | } else /* non translated slot */ |
| 523 | *dma_handle = virt_to_bus(ret); |
| 524 | |
| 525 | return ret; |
| 526 | |
| 527 | free: |
| 528 | free_pages((unsigned long)ret, get_order(size)); |
| 529 | ret = NULL; |
| 530 | error: |
| 531 | return ret; |
| 532 | } |
| 533 | |
Stephen Hemminger | e658450 | 2007-05-02 19:27:06 +0200 | [diff] [blame] | 534 | static const struct dma_mapping_ops calgary_dma_ops = { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 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, |
| 540 | }; |
| 541 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 542 | static inline void __iomem * busno_to_bbar(unsigned char num) |
| 543 | { |
| 544 | return bus_info[num].bbar; |
| 545 | } |
| 546 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 547 | static inline int busno_to_phbid(unsigned char num) |
| 548 | { |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 549 | return bus_info[num].phbid; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | static inline unsigned long split_queue_offset(unsigned char num) |
| 553 | { |
| 554 | size_t idx = busno_to_phbid(num); |
| 555 | |
| 556 | return split_queue_offsets[idx]; |
| 557 | } |
| 558 | |
| 559 | static inline unsigned long tar_offset(unsigned char num) |
| 560 | { |
| 561 | size_t idx = busno_to_phbid(num); |
| 562 | |
| 563 | return tar_offsets[idx]; |
| 564 | } |
| 565 | |
| 566 | static inline unsigned long phb_offset(unsigned char num) |
| 567 | { |
| 568 | size_t idx = busno_to_phbid(num); |
| 569 | |
| 570 | return phb_offsets[idx]; |
| 571 | } |
| 572 | |
| 573 | static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset) |
| 574 | { |
| 575 | unsigned long target = ((unsigned long)bar) | offset; |
| 576 | return (void __iomem*)target; |
| 577 | } |
| 578 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 579 | static inline int is_calioc2(unsigned short device) |
| 580 | { |
| 581 | return (device == PCI_DEVICE_ID_IBM_CALIOC2); |
| 582 | } |
| 583 | |
| 584 | static inline int is_calgary(unsigned short device) |
| 585 | { |
| 586 | return (device == PCI_DEVICE_ID_IBM_CALGARY); |
| 587 | } |
| 588 | |
| 589 | static inline int is_cal_pci_dev(unsigned short device) |
| 590 | { |
| 591 | return (is_calgary(device) || is_calioc2(device)); |
| 592 | } |
| 593 | |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 594 | static void calgary_tce_cache_blast(struct iommu_table *tbl) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 595 | { |
| 596 | u64 val; |
| 597 | u32 aer; |
| 598 | int i = 0; |
| 599 | void __iomem *bbar = tbl->bbar; |
| 600 | void __iomem *target; |
| 601 | |
| 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); |
| 606 | |
| 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); |
| 610 | |
| 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"); |
| 620 | |
| 621 | /* invalidate TCE cache */ |
| 622 | target = calgary_reg(bbar, tar_offset(tbl->it_busno)); |
| 623 | writeq(tbl->tar_val, target); |
| 624 | |
| 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 */ |
| 629 | } |
| 630 | |
| 631 | static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start, |
| 632 | u64 limit) |
| 633 | { |
| 634 | unsigned int numpages; |
| 635 | |
| 636 | limit = limit | 0xfffff; |
| 637 | limit++; |
| 638 | |
| 639 | numpages = ((limit - start) >> PAGE_SHIFT); |
| 640 | iommu_range_reserve(dev->sysdata, start, numpages); |
| 641 | } |
| 642 | |
| 643 | static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev) |
| 644 | { |
| 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; |
| 651 | |
| 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)); |
| 659 | |
| 660 | start = (high << 32) | low; |
| 661 | limit = sizelow; |
| 662 | |
| 663 | calgary_reserve_mem_region(dev, start, limit); |
| 664 | } |
| 665 | |
| 666 | static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev) |
| 667 | { |
| 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; |
| 675 | |
| 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; |
| 681 | |
| 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)); |
| 690 | |
| 691 | start = (high << 32) | low; |
| 692 | limit = (sizehigh << 32) | sizelow; |
| 693 | |
| 694 | calgary_reserve_mem_region(dev, start, limit); |
| 695 | } |
| 696 | |
| 697 | /* |
| 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. |
| 703 | */ |
| 704 | static void __init calgary_reserve_regions(struct pci_dev *dev) |
| 705 | { |
| 706 | unsigned int npages; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 707 | u64 start; |
| 708 | struct iommu_table *tbl = dev->sysdata; |
| 709 | |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 710 | /* reserve EMERGENCY_PAGES from bad_dma_address and up */ |
| 711 | iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 712 | |
| 713 | /* avoid the BIOS/VGA first 640KB-1MB region */ |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 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; |
| 721 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 722 | iommu_range_reserve(tbl, start, npages); |
| 723 | |
| 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); |
| 727 | } |
| 728 | |
| 729 | static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar) |
| 730 | { |
| 731 | u64 val64; |
| 732 | u64 table_phys; |
| 733 | void __iomem *target; |
| 734 | int ret; |
| 735 | struct iommu_table *tbl; |
| 736 | |
| 737 | /* build TCE tables for each PHB */ |
| 738 | ret = build_tce_table(dev, bbar); |
| 739 | if (ret) |
| 740 | return ret; |
| 741 | |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 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); |
| 745 | |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 746 | tbl->chip_ops = &calgary_chip_ops; |
| 747 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 748 | calgary_reserve_regions(dev); |
| 749 | |
| 750 | /* set TARs for each PHB */ |
| 751 | target = calgary_reg(bbar, tar_offset(dev->bus->number)); |
| 752 | val64 = be64_to_cpu(readq(target)); |
| 753 | |
| 754 | /* zero out all TAR bits under sw control */ |
| 755 | val64 &= ~TAR_SW_BITS; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 756 | table_phys = (u64)__pa(tbl->it_base); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 757 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 758 | val64 |= table_phys; |
| 759 | |
| 760 | BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M); |
| 761 | val64 |= (u64) specified_table_size; |
| 762 | |
| 763 | tbl->tar_val = cpu_to_be64(val64); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 764 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 765 | writeq(tbl->tar_val, target); |
| 766 | readq(target); /* flush */ |
| 767 | |
| 768 | return 0; |
| 769 | } |
| 770 | |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 771 | static void __init calgary_free_bus(struct pci_dev *dev) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 772 | { |
| 773 | u64 val64; |
| 774 | struct iommu_table *tbl = dev->sysdata; |
| 775 | void __iomem *target; |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 776 | unsigned int bitmapsz; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 777 | |
| 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 */ |
| 783 | |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 784 | bitmapsz = tbl->it_size / BITS_PER_BYTE; |
| 785 | free_pages((unsigned long)tbl->it_map, get_order(bitmapsz)); |
| 786 | tbl->it_map = NULL; |
| 787 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 788 | kfree(tbl); |
| 789 | dev->sysdata = NULL; |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 790 | |
| 791 | /* Can't free bootmem allocated memory after system is up :-( */ |
| 792 | bus_info[dev->bus->number].tce_space = NULL; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 793 | } |
| 794 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 795 | static void calgary_dump_error_regs(struct iommu_table *tbl) |
| 796 | { |
| 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; |
| 804 | |
| 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)); |
| 817 | |
| 818 | printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR " |
| 819 | "0x%08x@MCK\n", csr, plssr, csmr, mck); |
| 820 | |
| 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); |
| 828 | } |
| 829 | printk("\n"); |
| 830 | } |
| 831 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 832 | static void calgary_watchdog(unsigned long data) |
| 833 | { |
| 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; |
| 839 | |
| 840 | target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET); |
| 841 | val32 = be32_to_cpu(readl(target)); |
| 842 | |
| 843 | /* If no error, the agent ID in the CSR is not valid */ |
| 844 | if (val32 & CSR_AGENT_MASK) { |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 845 | printk(KERN_EMERG "Calgary: DMA error on PHB %#x\n", |
| 846 | dev->bus->number); |
| 847 | calgary_dump_error_regs(tbl); |
| 848 | |
| 849 | /* reset error */ |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 850 | writel(0, target); |
| 851 | |
| 852 | /* Disable bus that caused the error */ |
| 853 | target = calgary_reg(bbar, phb_offset(tbl->it_busno) | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 854 | PHB_CONFIG_RW_OFFSET); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 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); |
| 862 | } |
| 863 | } |
| 864 | |
Muli Ben-Yehuda | a2b663f | 2007-07-21 17:10:47 +0200 | [diff] [blame] | 865 | static void __init calgary_set_split_completion_timeout(void __iomem *bbar, |
| 866 | unsigned char busnum, unsigned long timeout) |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 867 | { |
| 868 | u64 val64; |
| 869 | void __iomem *target; |
Muli Ben-Yehuda | 58db854 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 870 | unsigned int phb_shift = ~0; /* silence gcc */ |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 871 | u64 mask; |
| 872 | |
| 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)); |
| 884 | } |
| 885 | |
| 886 | target = calgary_reg(bbar, CALGARY_CONFIG_REG); |
| 887 | val64 = be64_to_cpu(readq(target)); |
| 888 | |
| 889 | /* zero out this PHB's timer bits */ |
| 890 | mask = ~(0xFUL << phb_shift); |
| 891 | val64 &= mask; |
Muli Ben-Yehuda | a2b663f | 2007-07-21 17:10:47 +0200 | [diff] [blame] | 892 | val64 |= (timeout << phb_shift); |
Muli Ben-Yehuda | cb01fc7 | 2006-10-22 00:41:15 +0200 | [diff] [blame] | 893 | writeq(cpu_to_be64(val64), target); |
| 894 | readq(target); /* flush */ |
| 895 | } |
| 896 | |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 897 | static void __init calgary_handle_quirks(struct iommu_table *tbl, |
| 898 | struct pci_dev *dev) |
Muli Ben-Yehuda | b8d2ea1 | 2007-07-21 17:10:49 +0200 | [diff] [blame] | 899 | { |
| 900 | unsigned char busnum = dev->bus->number; |
Muli Ben-Yehuda | b8d2ea1 | 2007-07-21 17:10:49 +0200 | [diff] [blame] | 901 | |
| 902 | /* |
| 903 | * Give split completion a longer timeout on bus 1 for aic94xx |
| 904 | * http://bugzilla.kernel.org/show_bug.cgi?id=7180 |
| 905 | */ |
| 906 | if (busnum == 1) |
| 907 | calgary_set_split_completion_timeout(tbl->bbar, busnum, |
| 908 | CCR_2SEC_TIMEOUT); |
| 909 | } |
| 910 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 911 | static void __init calgary_enable_translation(struct pci_dev *dev) |
| 912 | { |
| 913 | u32 val32; |
| 914 | unsigned char busnum; |
| 915 | void __iomem *target; |
| 916 | void __iomem *bbar; |
| 917 | struct iommu_table *tbl; |
| 918 | |
| 919 | busnum = dev->bus->number; |
| 920 | tbl = dev->sysdata; |
| 921 | bbar = tbl->bbar; |
| 922 | |
| 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; |
| 927 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 928 | printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n", |
| 929 | (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ? |
| 930 | "Calgary" : "CalIOC2", busnum); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 931 | printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this " |
| 932 | "bus.\n"); |
| 933 | |
| 934 | writel(cpu_to_be32(val32), target); |
| 935 | readl(target); /* flush */ |
| 936 | |
| 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); |
| 941 | } |
| 942 | |
| 943 | static void __init calgary_disable_translation(struct pci_dev *dev) |
| 944 | { |
| 945 | u32 val32; |
| 946 | unsigned char busnum; |
| 947 | void __iomem *target; |
| 948 | void __iomem *bbar; |
| 949 | struct iommu_table *tbl; |
| 950 | |
| 951 | busnum = dev->bus->number; |
| 952 | tbl = dev->sysdata; |
| 953 | bbar = tbl->bbar; |
| 954 | |
| 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); |
| 959 | |
Jon Mason | 70d666d | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 960 | printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 961 | writel(cpu_to_be32(val32), target); |
| 962 | readl(target); /* flush */ |
| 963 | |
| 964 | del_timer_sync(&tbl->watchdog_timer); |
| 965 | } |
| 966 | |
Muli Ben-Yehuda | a4fc520 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 967 | static void __init calgary_init_one_nontraslated(struct pci_dev *dev) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 968 | { |
Muli Ben-Yehuda | 871b170 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 969 | pci_dev_get(dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 970 | dev->sysdata = NULL; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 971 | |
| 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; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | static int __init calgary_init_one(struct pci_dev *dev) |
| 980 | { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 981 | void __iomem *bbar; |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 982 | struct iommu_table *tbl; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 983 | int ret; |
| 984 | |
Jon Mason | dedc993 | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 985 | BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM); |
| 986 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 987 | bbar = busno_to_bbar(dev->bus->number); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 988 | ret = calgary_setup_tar(dev, bbar); |
| 989 | if (ret) |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 990 | goto done; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 991 | |
Muli Ben-Yehuda | 871b170 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 992 | pci_dev_get(dev); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 993 | |
| 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; |
Muli Ben-Yehuda | b8d2ea1 | 2007-07-21 17:10:49 +0200 | [diff] [blame] | 1001 | |
Muli Ben-Yehuda | ff297b8 | 2007-07-21 17:10:50 +0200 | [diff] [blame] | 1002 | tbl = dev->sysdata; |
| 1003 | tbl->chip_ops->handle_quirks(tbl, dev); |
Muli Ben-Yehuda | b8d2ea1 | 2007-07-21 17:10:49 +0200 | [diff] [blame] | 1004 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1005 | calgary_enable_translation(dev); |
| 1006 | |
| 1007 | return 0; |
| 1008 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1009 | done: |
| 1010 | return ret; |
| 1011 | } |
| 1012 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1013 | static int __init calgary_locate_bbars(void) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1014 | { |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1015 | int ret; |
| 1016 | int rioidx, phb, bus; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1017 | void __iomem *bbar; |
| 1018 | void __iomem *target; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1019 | unsigned long offset; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1020 | u8 start_bus, end_bus; |
| 1021 | u32 val; |
| 1022 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1023 | ret = -ENODATA; |
| 1024 | for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) { |
| 1025 | struct rio_detail *rio = rio_devs[rioidx]; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1026 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1027 | if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY)) |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1028 | continue; |
| 1029 | |
| 1030 | /* map entire 1MB of Calgary config space */ |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1031 | bbar = ioremap_nocache(rio->BBAR, 1024 * 1024); |
| 1032 | if (!bbar) |
| 1033 | goto error; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1034 | |
| 1035 | for (phb = 0; phb < PHBS_PER_CALGARY; phb++) { |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1036 | offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET; |
| 1037 | target = calgary_reg(bbar, offset); |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1038 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1039 | val = be32_to_cpu(readl(target)); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1040 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1041 | start_bus = (u8)((val & 0x00FF0000) >> 16); |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1042 | end_bus = (u8)((val & 0x0000FF00) >> 8); |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1043 | |
| 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; |
| 1048 | } |
| 1049 | } else { |
| 1050 | bus_info[start_bus].bbar = bbar; |
| 1051 | bus_info[start_bus].phbid = phb; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | } |
| 1055 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1056 | return 0; |
| 1057 | |
| 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); |
| 1063 | |
| 1064 | return ret; |
| 1065 | } |
| 1066 | |
| 1067 | static int __init calgary_init(void) |
| 1068 | { |
| 1069 | int ret; |
| 1070 | struct pci_dev *dev = NULL; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1071 | void* tce_space; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1072 | |
| 1073 | ret = calgary_locate_bbars(); |
| 1074 | if (ret) |
| 1075 | return ret; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1076 | |
Jon Mason | dedc993 | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1077 | do { |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1078 | dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1079 | if (!dev) |
| 1080 | break; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1081 | if (!is_cal_pci_dev(dev->device)) |
| 1082 | continue; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1083 | if (!translate_phb(dev)) { |
| 1084 | calgary_init_one_nontraslated(dev); |
| 1085 | continue; |
| 1086 | } |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 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); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1090 | continue; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1091 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1092 | ret = calgary_init_one(dev); |
| 1093 | if (ret) |
| 1094 | goto error; |
Jon Mason | dedc993 | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1095 | } while (1); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1096 | |
| 1097 | return ret; |
| 1098 | |
| 1099 | error: |
Jon Mason | dedc993 | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1100 | do { |
Alan Cox | 7cd8b68 | 2006-12-07 02:14:03 +0100 | [diff] [blame] | 1101 | dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM, |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1102 | PCI_ANY_ID, dev); |
Muli Ben-Yehuda | 9f2dc46 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1103 | if (!dev) |
| 1104 | break; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1105 | if (!is_cal_pci_dev(dev->device)) |
| 1106 | continue; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1107 | if (!translate_phb(dev)) { |
| 1108 | pci_dev_put(dev); |
| 1109 | continue; |
| 1110 | } |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1111 | if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1112 | continue; |
Muli Ben-Yehuda | 871b170 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1113 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1114 | calgary_disable_translation(dev); |
Muli Ben-Yehuda | b8f4fe6 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1115 | calgary_free_bus(dev); |
Muli Ben-Yehuda | 871b170 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1116 | pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */ |
Jon Mason | dedc993 | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1117 | } while (1); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1118 | |
| 1119 | return ret; |
| 1120 | } |
| 1121 | |
| 1122 | static inline int __init determine_tce_table_size(u64 ram) |
| 1123 | { |
| 1124 | int ret; |
| 1125 | |
| 1126 | if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED) |
| 1127 | return specified_table_size; |
| 1128 | |
| 1129 | /* |
| 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. |
| 1135 | */ |
| 1136 | ret = get_order(ram >> 13); |
| 1137 | if (ret > TCE_TABLE_SIZE_8M) |
| 1138 | ret = TCE_TABLE_SIZE_8M; |
| 1139 | |
| 1140 | return ret; |
| 1141 | } |
| 1142 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1143 | static int __init build_detail_arrays(void) |
| 1144 | { |
| 1145 | unsigned long ptr; |
| 1146 | int i, scal_detail_size, rio_detail_size; |
| 1147 | |
| 1148 | if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){ |
| 1149 | printk(KERN_WARNING |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1150 | "Calgary: MAX_NUMNODES too low! Defined as %d, " |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1151 | "but system has %d nodes.\n", |
| 1152 | MAX_NUMNODES, rio_table_hdr->num_scal_dev); |
| 1153 | return -ENODEV; |
| 1154 | } |
| 1155 | |
| 1156 | switch (rio_table_hdr->version){ |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 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; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1165 | default: |
| 1166 | printk(KERN_WARNING |
| 1167 | "Calgary: Invalid Rio Grande Table Version: %d\n", |
| 1168 | rio_table_hdr->version); |
| 1169 | return -EPROTO; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1170 | } |
| 1171 | |
| 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; |
| 1176 | |
| 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; |
| 1180 | |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1184 | static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev) |
| 1185 | { |
| 1186 | int dev; |
| 1187 | u32 val; |
| 1188 | |
| 1189 | if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) { |
| 1190 | /* |
| 1191 | * FIXME: properly scan for devices accross the |
| 1192 | * PCI-to-PCI bridge on every CalIOC2 port. |
| 1193 | */ |
| 1194 | return 1; |
| 1195 | } |
| 1196 | |
| 1197 | for (dev = 1; dev < 8; dev++) { |
| 1198 | val = read_pci_config(bus, dev, 0, 0); |
| 1199 | if (val != 0xffffffff) |
| 1200 | break; |
| 1201 | } |
| 1202 | return (val != 0xffffffff); |
| 1203 | } |
| 1204 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1205 | void __init detect_calgary(void) |
| 1206 | { |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1207 | int bus; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1208 | void *tbl; |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1209 | int calgary_found = 0; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1210 | unsigned long ptr; |
Ingo Molnar | 136f1e7 | 2006-12-20 11:53:32 +0100 | [diff] [blame] | 1211 | unsigned int offset, prev_offset; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1212 | int ret; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1213 | |
| 1214 | /* |
| 1215 | * if the user specified iommu=off or iommu=soft or we found |
| 1216 | * another HW IOMMU already, bail out. |
| 1217 | */ |
| 1218 | if (swiotlb || no_iommu || iommu_detected) |
| 1219 | return; |
| 1220 | |
Muli Ben-Yehuda | bff6547 | 2006-12-07 02:14:07 +0100 | [diff] [blame] | 1221 | if (!use_calgary) |
| 1222 | return; |
| 1223 | |
Andi Kleen | 0637a70 | 2006-09-26 10:52:41 +0200 | [diff] [blame] | 1224 | if (!early_pci_allowed()) |
| 1225 | return; |
| 1226 | |
Muli Ben-Yehuda | b92cc55 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1227 | printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n"); |
| 1228 | |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1229 | ptr = (unsigned long)phys_to_virt(get_bios_ebda()); |
| 1230 | |
| 1231 | rio_table_hdr = NULL; |
Ingo Molnar | 136f1e7 | 2006-12-20 11:53:32 +0100 | [diff] [blame] | 1232 | prev_offset = 0; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1233 | offset = 0x180; |
Ingo Molnar | 136f1e7 | 2006-12-20 11:53:32 +0100 | [diff] [blame] | 1234 | /* |
| 1235 | * The next offset is stored in the 1st word. |
| 1236 | * Only parse up until the offset increases: |
| 1237 | */ |
| 1238 | while (offset > prev_offset) { |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 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 */ |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1242 | rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4); |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1243 | break; |
| 1244 | } |
Ingo Molnar | 136f1e7 | 2006-12-20 11:53:32 +0100 | [diff] [blame] | 1245 | prev_offset = offset; |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1246 | offset = *((unsigned short *)(ptr + offset)); |
| 1247 | } |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1248 | if (!rio_table_hdr) { |
Muli Ben-Yehuda | b92cc55 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1249 | printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table " |
| 1250 | "in EBDA - bailing!\n"); |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1251 | return; |
| 1252 | } |
| 1253 | |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1254 | ret = build_detail_arrays(); |
| 1255 | if (ret) { |
Muli Ben-Yehuda | b92cc55 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1256 | printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret); |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1257 | return; |
Muli Ben-Yehuda | eae9375 | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1258 | } |
Laurent Vivier | b34e90b | 2006-12-07 02:14:06 +0100 | [diff] [blame] | 1259 | |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1260 | specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE); |
| 1261 | |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1262 | for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) { |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1263 | struct calgary_bus_info *info = &bus_info[bus]; |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1264 | unsigned short pci_device; |
| 1265 | u32 val; |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1266 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 1267 | val = read_pci_config(bus, 0, 0, 0); |
| 1268 | pci_device = (val & 0xFFFF0000) >> 16; |
| 1269 | |
| 1270 | if (!is_cal_pci_dev(pci_device)) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1271 | continue; |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1272 | |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1273 | if (info->translation_disabled) |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1274 | continue; |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1275 | |
Muli Ben-Yehuda | 8a24459 | 2007-07-21 17:10:52 +0200 | [diff] [blame^] | 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); |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1285 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1286 | } |
| 1287 | |
Muli Ben-Yehuda | b92cc55 | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 1288 | printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n", |
| 1289 | calgary_found ? "found" : "not found"); |
| 1290 | |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1291 | if (calgary_found) { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1292 | iommu_detected = 1; |
| 1293 | calgary_detected = 1; |
Muli Ben-Yehuda | de68465 | 2006-09-26 10:52:33 +0200 | [diff] [blame] | 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"); |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1298 | } |
| 1299 | return; |
| 1300 | |
| 1301 | cleanup: |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1302 | for (--bus; bus >= 0; --bus) { |
| 1303 | struct calgary_bus_info *info = &bus_info[bus]; |
| 1304 | |
| 1305 | if (info->tce_space) |
| 1306 | free_tce_table(info->tce_space); |
| 1307 | } |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | int __init calgary_iommu_init(void) |
| 1311 | { |
| 1312 | int ret; |
| 1313 | |
| 1314 | if (no_iommu || swiotlb) |
| 1315 | return -ENODEV; |
| 1316 | |
| 1317 | if (!calgary_detected) |
| 1318 | return -ENODEV; |
| 1319 | |
| 1320 | /* ok, we're trying to use Calgary - let's roll */ |
| 1321 | printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n"); |
| 1322 | |
| 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; |
| 1331 | } |
| 1332 | |
| 1333 | force_iommu = 1; |
Muli Ben-Yehuda | 310adfd | 2007-02-13 13:26:24 +0100 | [diff] [blame] | 1334 | bad_dma_address = 0x0; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1335 | dma_ops = &calgary_dma_ops; |
| 1336 | |
| 1337 | return 0; |
| 1338 | } |
| 1339 | |
| 1340 | static int __init calgary_parse_options(char *p) |
| 1341 | { |
| 1342 | unsigned int bridge; |
| 1343 | size_t len; |
| 1344 | char* endp; |
| 1345 | |
| 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; |
| 1363 | |
| 1364 | len = strlen("translate_empty_slots"); |
| 1365 | if (!strncmp(p, "translate_empty_slots", len)) |
| 1366 | translate_empty_slots = 1; |
| 1367 | |
| 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; |
| 1378 | |
Jon Mason | d2105b1 | 2006-07-29 21:42:43 +0200 | [diff] [blame] | 1379 | if (bridge < MAX_PHB_BUS_NUM) { |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1380 | printk(KERN_INFO "Calgary: disabling " |
Jon Mason | 70d666d | 2006-10-05 18:47:21 +0200 | [diff] [blame] | 1381 | "translation for PHB %#x\n", bridge); |
Muli Ben-Yehuda | f38db65 | 2006-09-26 10:52:31 +0200 | [diff] [blame] | 1382 | bus_info[bridge].translation_disabled = 1; |
Jon Mason | e465058 | 2006-06-26 13:58:14 +0200 | [diff] [blame] | 1383 | } |
| 1384 | } |
| 1385 | |
| 1386 | p = strpbrk(p, ","); |
| 1387 | if (!p) |
| 1388 | break; |
| 1389 | |
| 1390 | p++; /* skip ',' */ |
| 1391 | } |
| 1392 | return 1; |
| 1393 | } |
| 1394 | __setup("calgary=", calgary_parse_options); |