blob: ed16a107d2d4a45c740bb56a6885c842f5b22ea4 [file] [log] [blame]
Jon Masone4650582006-06-26 13:58:14 +02001/*
2 * Derived from arch/powerpc/kernel/iommu.c
3 *
Muli Ben-Yehuda98822342007-07-21 17:10:48 +02004 * Copyright IBM Corporation, 2006-2007
Jon Masond8d2bed2006-10-05 18:47:21 +02005 * Copyright (C) 2006 Jon Mason <jdmason@kudzu.us>
Jon Masone4650582006-06-26 13:58:14 +02006 *
Jon Masond8d2bed2006-10-05 18:47:21 +02007 * Author: Jon Mason <jdmason@kudzu.us>
Muli Ben-Yehudaaa0a9f32006-07-10 17:06:15 +02008 * Author: Muli Ben-Yehuda <muli@il.ibm.com>
9
Jon Masone4650582006-06-26 13:58:14 +020010 * 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 Masone4650582006-06-26 13:58:14 +020025#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 Vivierb34e90b2006-12-07 02:14:06 +010044#include <asm/rio.h>
Jon Masone4650582006-06-26 13:58:14 +020045
Muli Ben-Yehudabff65472006-12-07 02:14:07 +010046#ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
47int use_calgary __read_mostly = 1;
48#else
49int use_calgary __read_mostly = 0;
50#endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
51
Jon Masone4650582006-06-26 13:58:14 +020052#define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +020053#define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
Jon Masone4650582006-06-26 13:58:14 +020054
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-Yehudacb01fc72006-10-22 00:41:15 +020061#define CALGARY_CONFIG_REG 0x0108
62#define PHB_CSR_OFFSET 0x0110 /* Channel Status */
Jon Masone4650582006-06-26 13:58:14 +020063#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
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020082/* CalIOC2 specific */
83#define PHB_SAVIOR_L2 0x0DB0
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +020084#define PHB_PAGE_MIG_CTRL 0x0DA8
85#define PHB_PAGE_MIG_DEBUG 0x0DA0
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +020086#define PHB_ROOT_COMPLEX_STATUS 0x0CB0
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020087
Jon Masone4650582006-06-26 13:58:14 +020088/* PHB_CONFIG_RW */
89#define PHB_TCE_ENABLE 0x20000000
90#define PHB_SLOT_DISABLE 0x1C000000
91#define PHB_DAC_DISABLE 0x01000000
92#define PHB_MEM2_ENABLE 0x00400000
93#define PHB_MCSR_ENABLE 0x00100000
94/* TAR (Table Address Register) */
95#define TAR_SW_BITS 0x0000ffffffff800fUL
96#define TAR_VALID 0x0000000000000008UL
97/* CSR (Channel/DMA Status Register) */
98#define CSR_AGENT_MASK 0xffe0ffff
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020099/* CCR (Calgary Configuration Register) */
100#define CCR_2SEC_TIMEOUT 0x000000000000000EUL
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200101/* PMCR/PMDR (Page Migration Control/Debug Registers */
102#define PMR_SOFTSTOP 0x80000000
103#define PMR_SOFTSTOPFAULT 0x40000000
104#define PMR_HARDSTOP 0x20000000
Jon Masone4650582006-06-26 13:58:14 +0200105
106#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +0200107#define MAX_NUM_CHASSIS 8 /* max number of chassis */
Muli Ben-Yehuda4ea8a5d2006-09-26 10:52:33 +0200108/* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
109#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
Jon Masone4650582006-06-26 13:58:14 +0200110#define PHBS_PER_CALGARY 4
111
112/* register offsets in Calgary's internal register space */
113static const unsigned long tar_offsets[] = {
114 0x0580 /* TAR0 */,
115 0x0588 /* TAR1 */,
116 0x0590 /* TAR2 */,
117 0x0598 /* TAR3 */
118};
119
120static const unsigned long split_queue_offsets[] = {
121 0x4870 /* SPLIT QUEUE 0 */,
122 0x5870 /* SPLIT QUEUE 1 */,
123 0x6870 /* SPLIT QUEUE 2 */,
124 0x7870 /* SPLIT QUEUE 3 */
125};
126
127static const unsigned long phb_offsets[] = {
128 0x8000 /* PHB0 */,
129 0x9000 /* PHB1 */,
130 0xA000 /* PHB2 */,
131 0xB000 /* PHB3 */
132};
133
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100134/* PHB debug registers */
135
136static const unsigned long phb_debug_offsets[] = {
137 0x4000 /* PHB 0 DEBUG */,
138 0x5000 /* PHB 1 DEBUG */,
139 0x6000 /* PHB 2 DEBUG */,
140 0x7000 /* PHB 3 DEBUG */
141};
142
143/*
144 * STUFF register for each debug PHB,
145 * byte 1 = start bus number, byte 2 = end bus number
146 */
147
148#define PHB_DEBUG_STUFF_OFFSET 0x0020
149
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100150#define EMERGENCY_PAGES 32 /* = 128KB */
151
Jon Masone4650582006-06-26 13:58:14 +0200152unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
153static int translate_empty_slots __read_mostly = 0;
154static int calgary_detected __read_mostly = 0;
155
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100156static struct rio_table_hdr *rio_table_hdr __initdata;
157static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100158static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100159
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200160struct calgary_bus_info {
161 void *tce_space;
Muli Ben-Yehuda0577f142006-09-26 10:52:31 +0200162 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200163 signed char phbid;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100164 void __iomem *bbar;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200165};
166
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200167static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
168static void calgary_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200169static void calgary_dump_error_regs(struct iommu_table *tbl);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200170static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200171static void calioc2_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200172static void calioc2_dump_error_regs(struct iommu_table *tbl);
Jon Masone4650582006-06-26 13:58:14 +0200173
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200174static struct cal_chipset_ops calgary_chip_ops = {
175 .handle_quirks = calgary_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200176 .tce_cache_blast = calgary_tce_cache_blast,
177 .dump_error_regs = calgary_dump_error_regs
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200178};
179
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200180static struct cal_chipset_ops calioc2_chip_ops = {
181 .handle_quirks = calioc2_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200182 .tce_cache_blast = calioc2_tce_cache_blast,
183 .dump_error_regs = calioc2_dump_error_regs
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200184};
185
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200186static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200187
188/* enable this to stress test the chip's TCE cache */
189#ifdef CONFIG_IOMMU_DEBUG
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200190int debugging __read_mostly = 1;
191
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200192static inline unsigned long verify_bit_range(unsigned long* bitmap,
193 int expected, unsigned long start, unsigned long end)
194{
195 unsigned long idx = start;
196
197 BUG_ON(start >= end);
198
199 while (idx < end) {
200 if (!!test_bit(idx, bitmap) != expected)
201 return idx;
202 ++idx;
203 }
204
205 /* all bits have the expected value */
206 return ~0UL;
207}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200208#else /* debugging is disabled */
209int debugging __read_mostly = 0;
210
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200211static inline unsigned long verify_bit_range(unsigned long* bitmap,
212 int expected, unsigned long start, unsigned long end)
213{
214 return ~0UL;
215}
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200216
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200217#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200218
219static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
220{
221 unsigned int npages;
222
223 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
224 npages >>= PAGE_SHIFT;
225
226 return npages;
227}
228
229static inline int translate_phb(struct pci_dev* dev)
230{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200231 int disabled = bus_info[dev->bus->number].translation_disabled;
Jon Masone4650582006-06-26 13:58:14 +0200232 return !disabled;
233}
234
235static void iommu_range_reserve(struct iommu_table *tbl,
236 unsigned long start_addr, unsigned int npages)
237{
238 unsigned long index;
239 unsigned long end;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200240 unsigned long badbit;
Jon Masone4650582006-06-26 13:58:14 +0200241
242 index = start_addr >> PAGE_SHIFT;
243
244 /* bail out if we're asked to reserve a region we don't cover */
245 if (index >= tbl->it_size)
246 return;
247
248 end = index + npages;
249 if (end > tbl->it_size) /* don't go off the table */
250 end = tbl->it_size;
251
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200252 badbit = verify_bit_range(tbl->it_map, 0, index, end);
253 if (badbit != ~0UL) {
254 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200255 printk(KERN_ERR "Calgary: entry already allocated at "
256 "0x%lx tbl %p dma 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200257 badbit, tbl, start_addr, npages);
Jon Masone4650582006-06-26 13:58:14 +0200258 }
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200259
260 set_bit_string(tbl->it_map, index, npages);
Jon Masone4650582006-06-26 13:58:14 +0200261}
262
263static unsigned long iommu_range_alloc(struct iommu_table *tbl,
264 unsigned int npages)
265{
266 unsigned long offset;
267
268 BUG_ON(npages == 0);
269
270 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
271 tbl->it_size, npages);
272 if (offset == ~0UL) {
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200273 tbl->chip_ops->tce_cache_blast(tbl);
Jon Masone4650582006-06-26 13:58:14 +0200274 offset = find_next_zero_string(tbl->it_map, 0,
275 tbl->it_size, npages);
276 if (offset == ~0UL) {
277 printk(KERN_WARNING "Calgary: IOMMU full.\n");
278 if (panic_on_overflow)
279 panic("Calgary: fix the allocator.\n");
280 else
281 return bad_dma_address;
282 }
283 }
284
285 set_bit_string(tbl->it_map, offset, npages);
286 tbl->it_hint = offset + npages;
287 BUG_ON(tbl->it_hint > tbl->it_size);
288
289 return offset;
290}
291
292static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
293 unsigned int npages, int direction)
294{
295 unsigned long entry, flags;
296 dma_addr_t ret = bad_dma_address;
297
298 spin_lock_irqsave(&tbl->it_lock, flags);
299
300 entry = iommu_range_alloc(tbl, npages);
301
302 if (unlikely(entry == bad_dma_address))
303 goto error;
304
305 /* set the return dma address */
306 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
307
308 /* put the TCEs in the HW table */
309 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
310 direction);
311
312 spin_unlock_irqrestore(&tbl->it_lock, flags);
313
314 return ret;
315
316error:
317 spin_unlock_irqrestore(&tbl->it_lock, flags);
318 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
319 "iommu %p\n", npages, tbl);
320 return bad_dma_address;
321}
322
323static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
324 unsigned int npages)
325{
326 unsigned long entry;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200327 unsigned long badbit;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100328 unsigned long badend;
329
330 /* were we called with bad_dma_address? */
331 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
332 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
333 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
334 "address 0x%Lx\n", dma_addr);
335 WARN_ON(1);
336 return;
337 }
Jon Masone4650582006-06-26 13:58:14 +0200338
339 entry = dma_addr >> PAGE_SHIFT;
340
341 BUG_ON(entry + npages > tbl->it_size);
342
343 tce_free(tbl, entry, npages);
344
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200345 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
346 if (badbit != ~0UL) {
347 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200348 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
349 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200350 badbit, tbl, dma_addr, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200351 }
352
353 __clear_bit_string(tbl->it_map, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200354}
355
356static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
357 unsigned int npages)
358{
359 unsigned long flags;
360
361 spin_lock_irqsave(&tbl->it_lock, flags);
362
363 __iommu_free(tbl, dma_addr, npages);
364
365 spin_unlock_irqrestore(&tbl->it_lock, flags);
366}
367
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200368static inline struct iommu_table *find_iommu_table(struct device *dev)
369{
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200370 struct pci_dev *pdev;
371 struct pci_bus *pbus;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200372 struct iommu_table *tbl;
373
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200374 pdev = to_pci_dev(dev);
375
376 /* is the device behind a bridge? */
377 if (unlikely(pdev->bus->parent))
378 pbus = pdev->bus->parent;
379 else
380 pbus = pdev->bus;
381
382 tbl = pbus->self->sysdata;
383 BUG_ON(pdev->bus->parent && (tbl->it_busno != pdev->bus->parent->number));
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200384
385 return tbl;
386}
387
Jon Masone4650582006-06-26 13:58:14 +0200388static void __calgary_unmap_sg(struct iommu_table *tbl,
389 struct scatterlist *sglist, int nelems, int direction)
390{
391 while (nelems--) {
392 unsigned int npages;
393 dma_addr_t dma = sglist->dma_address;
394 unsigned int dmalen = sglist->dma_length;
395
396 if (dmalen == 0)
397 break;
398
399 npages = num_dma_pages(dma, dmalen);
400 __iommu_free(tbl, dma, npages);
401 sglist++;
402 }
403}
404
405void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
406 int nelems, int direction)
407{
408 unsigned long flags;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200409 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200410
411 if (!translate_phb(to_pci_dev(dev)))
412 return;
413
414 spin_lock_irqsave(&tbl->it_lock, flags);
415
416 __calgary_unmap_sg(tbl, sglist, nelems, direction);
417
418 spin_unlock_irqrestore(&tbl->it_lock, flags);
419}
420
421static int calgary_nontranslate_map_sg(struct device* dev,
422 struct scatterlist *sg, int nelems, int direction)
423{
424 int i;
425
426 for (i = 0; i < nelems; i++ ) {
427 struct scatterlist *s = &sg[i];
428 BUG_ON(!s->page);
429 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
430 s->dma_length = s->length;
431 }
432 return nelems;
433}
434
435int calgary_map_sg(struct device *dev, struct scatterlist *sg,
436 int nelems, int direction)
437{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200438 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200439 unsigned long flags;
440 unsigned long vaddr;
441 unsigned int npages;
442 unsigned long entry;
443 int i;
444
445 if (!translate_phb(to_pci_dev(dev)))
446 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
447
448 spin_lock_irqsave(&tbl->it_lock, flags);
449
450 for (i = 0; i < nelems; i++ ) {
451 struct scatterlist *s = &sg[i];
452 BUG_ON(!s->page);
453
454 vaddr = (unsigned long)page_address(s->page) + s->offset;
455 npages = num_dma_pages(vaddr, s->length);
456
457 entry = iommu_range_alloc(tbl, npages);
458 if (entry == bad_dma_address) {
459 /* makes sure unmap knows to stop */
460 s->dma_length = 0;
461 goto error;
462 }
463
464 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
465
466 /* insert into HW table */
467 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
468 direction);
469
470 s->dma_length = s->length;
471 }
472
473 spin_unlock_irqrestore(&tbl->it_lock, flags);
474
475 return nelems;
476error:
477 __calgary_unmap_sg(tbl, sg, nelems, direction);
478 for (i = 0; i < nelems; i++) {
479 sg[i].dma_address = bad_dma_address;
480 sg[i].dma_length = 0;
481 }
482 spin_unlock_irqrestore(&tbl->it_lock, flags);
483 return 0;
484}
485
486dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
487 size_t size, int direction)
488{
489 dma_addr_t dma_handle = bad_dma_address;
490 unsigned long uaddr;
491 unsigned int npages;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200492 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200493
494 uaddr = (unsigned long)vaddr;
495 npages = num_dma_pages(uaddr, size);
496
497 if (translate_phb(to_pci_dev(dev)))
498 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
499 else
500 dma_handle = virt_to_bus(vaddr);
501
502 return dma_handle;
503}
504
505void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
506 size_t size, int direction)
507{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200508 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200509 unsigned int npages;
510
511 if (!translate_phb(to_pci_dev(dev)))
512 return;
513
514 npages = num_dma_pages(dma_handle, size);
515 iommu_free(tbl, dma_handle, npages);
516}
517
518void* calgary_alloc_coherent(struct device *dev, size_t size,
519 dma_addr_t *dma_handle, gfp_t flag)
520{
521 void *ret = NULL;
522 dma_addr_t mapping;
523 unsigned int npages, order;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200524 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200525
526 size = PAGE_ALIGN(size); /* size rounded up to full pages */
527 npages = size >> PAGE_SHIFT;
528 order = get_order(size);
529
530 /* alloc enough pages (and possibly more) */
531 ret = (void *)__get_free_pages(flag, order);
532 if (!ret)
533 goto error;
534 memset(ret, 0, size);
535
536 if (translate_phb(to_pci_dev(dev))) {
537 /* set up tces to cover the allocated range */
538 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
539 if (mapping == bad_dma_address)
540 goto free;
541
542 *dma_handle = mapping;
543 } else /* non translated slot */
544 *dma_handle = virt_to_bus(ret);
545
546 return ret;
547
548free:
549 free_pages((unsigned long)ret, get_order(size));
550 ret = NULL;
551error:
552 return ret;
553}
554
Stephen Hemmingere6584502007-05-02 19:27:06 +0200555static const struct dma_mapping_ops calgary_dma_ops = {
Jon Masone4650582006-06-26 13:58:14 +0200556 .alloc_coherent = calgary_alloc_coherent,
557 .map_single = calgary_map_single,
558 .unmap_single = calgary_unmap_single,
559 .map_sg = calgary_map_sg,
560 .unmap_sg = calgary_unmap_sg,
561};
562
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100563static inline void __iomem * busno_to_bbar(unsigned char num)
564{
565 return bus_info[num].bbar;
566}
567
Jon Masone4650582006-06-26 13:58:14 +0200568static inline int busno_to_phbid(unsigned char num)
569{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200570 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200571}
572
573static inline unsigned long split_queue_offset(unsigned char num)
574{
575 size_t idx = busno_to_phbid(num);
576
577 return split_queue_offsets[idx];
578}
579
580static inline unsigned long tar_offset(unsigned char num)
581{
582 size_t idx = busno_to_phbid(num);
583
584 return tar_offsets[idx];
585}
586
587static inline unsigned long phb_offset(unsigned char num)
588{
589 size_t idx = busno_to_phbid(num);
590
591 return phb_offsets[idx];
592}
593
594static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
595{
596 unsigned long target = ((unsigned long)bar) | offset;
597 return (void __iomem*)target;
598}
599
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200600static inline int is_calioc2(unsigned short device)
601{
602 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
603}
604
605static inline int is_calgary(unsigned short device)
606{
607 return (device == PCI_DEVICE_ID_IBM_CALGARY);
608}
609
610static inline int is_cal_pci_dev(unsigned short device)
611{
612 return (is_calgary(device) || is_calioc2(device));
613}
614
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200615static void calgary_tce_cache_blast(struct iommu_table *tbl)
Jon Masone4650582006-06-26 13:58:14 +0200616{
617 u64 val;
618 u32 aer;
619 int i = 0;
620 void __iomem *bbar = tbl->bbar;
621 void __iomem *target;
622
623 /* disable arbitration on the bus */
624 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
625 aer = readl(target);
626 writel(0, target);
627
628 /* read plssr to ensure it got there */
629 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
630 val = readl(target);
631
632 /* poll split queues until all DMA activity is done */
633 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
634 do {
635 val = readq(target);
636 i++;
637 } while ((val & 0xff) != 0xff && i < 100);
638 if (i == 100)
639 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
640 "continuing anyway\n");
641
642 /* invalidate TCE cache */
643 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
644 writeq(tbl->tar_val, target);
645
646 /* enable arbitration */
647 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
648 writel(aer, target);
649 (void)readl(target); /* flush */
650}
651
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200652static void calioc2_tce_cache_blast(struct iommu_table *tbl)
653{
654 void __iomem *bbar = tbl->bbar;
655 void __iomem *target;
656 u64 val64;
657 u32 val;
658 int i = 0;
659 int count = 1;
660 unsigned char bus = tbl->it_busno;
661
662begin:
663 printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
664 "sequence - count %d\n", bus, count);
665
666 /* 1. using the Page Migration Control reg set SoftStop */
667 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
668 val = be32_to_cpu(readl(target));
669 printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
670 val |= PMR_SOFTSTOP;
671 printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
672 writel(cpu_to_be32(val), target);
673
674 /* 2. poll split queues until all DMA activity is done */
675 printk(KERN_DEBUG "2a. starting to poll split queues\n");
676 target = calgary_reg(bbar, split_queue_offset(bus));
677 do {
678 val64 = readq(target);
679 i++;
680 } while ((val64 & 0xff) != 0xff && i < 100);
681 if (i == 100)
682 printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
683 "continuing anyway\n");
684
685 /* 3. poll Page Migration DEBUG for SoftStopFault */
686 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
687 val = be32_to_cpu(readl(target));
688 printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
689
690 /* 4. if SoftStopFault - goto (1) */
691 if (val & PMR_SOFTSTOPFAULT) {
692 if (++count < 100)
693 goto begin;
694 else {
695 printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
696 "aborting TCE cache flush sequence!\n");
697 return; /* pray for the best */
698 }
699 }
700
701 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
702 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
703 printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
704 val = be32_to_cpu(readl(target));
705 printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
706 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
707 val = be32_to_cpu(readl(target));
708 printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
709
710 /* 6. invalidate TCE cache */
711 printk(KERN_DEBUG "6. invalidating TCE cache\n");
712 target = calgary_reg(bbar, tar_offset(bus));
713 writeq(tbl->tar_val, target);
714
715 /* 7. Re-read PMCR */
716 printk(KERN_DEBUG "7a. Re-reading PMCR\n");
717 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
718 val = be32_to_cpu(readl(target));
719 printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
720
721 /* 8. Remove HardStop */
722 printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
723 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
724 val = 0;
725 printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
726 writel(cpu_to_be32(val), target);
727 val = be32_to_cpu(readl(target));
728 printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
729}
730
Jon Masone4650582006-06-26 13:58:14 +0200731static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
732 u64 limit)
733{
734 unsigned int numpages;
735
736 limit = limit | 0xfffff;
737 limit++;
738
739 numpages = ((limit - start) >> PAGE_SHIFT);
740 iommu_range_reserve(dev->sysdata, start, numpages);
741}
742
743static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
744{
745 void __iomem *target;
746 u64 low, high, sizelow;
747 u64 start, limit;
748 struct iommu_table *tbl = dev->sysdata;
749 unsigned char busnum = dev->bus->number;
750 void __iomem *bbar = tbl->bbar;
751
752 /* peripheral MEM_1 region */
753 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
754 low = be32_to_cpu(readl(target));
755 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
756 high = be32_to_cpu(readl(target));
757 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
758 sizelow = be32_to_cpu(readl(target));
759
760 start = (high << 32) | low;
761 limit = sizelow;
762
763 calgary_reserve_mem_region(dev, start, limit);
764}
765
766static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
767{
768 void __iomem *target;
769 u32 val32;
770 u64 low, high, sizelow, sizehigh;
771 u64 start, limit;
772 struct iommu_table *tbl = dev->sysdata;
773 unsigned char busnum = dev->bus->number;
774 void __iomem *bbar = tbl->bbar;
775
776 /* is it enabled? */
777 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
778 val32 = be32_to_cpu(readl(target));
779 if (!(val32 & PHB_MEM2_ENABLE))
780 return;
781
782 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
783 low = be32_to_cpu(readl(target));
784 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
785 high = be32_to_cpu(readl(target));
786 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
787 sizelow = be32_to_cpu(readl(target));
788 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
789 sizehigh = be32_to_cpu(readl(target));
790
791 start = (high << 32) | low;
792 limit = (sizehigh << 32) | sizelow;
793
794 calgary_reserve_mem_region(dev, start, limit);
795}
796
797/*
798 * some regions of the IO address space do not get translated, so we
799 * must not give devices IO addresses in those regions. The regions
800 * are the 640KB-1MB region and the two PCI peripheral memory holes.
801 * Reserve all of them in the IOMMU bitmap to avoid giving them out
802 * later.
803 */
804static void __init calgary_reserve_regions(struct pci_dev *dev)
805{
806 unsigned int npages;
Jon Masone4650582006-06-26 13:58:14 +0200807 u64 start;
808 struct iommu_table *tbl = dev->sysdata;
809
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100810 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
811 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
Jon Masone4650582006-06-26 13:58:14 +0200812
813 /* avoid the BIOS/VGA first 640KB-1MB region */
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200814 /* for CalIOC2 - avoid the entire first 2MB */
815 if (is_calgary(dev->device)) {
816 start = (640 * 1024);
817 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
818 } else { /* calioc2 */
819 start = 0;
820 npages = (2 * 1024 * 1024) >> PAGE_SHIFT;
821 }
Jon Masone4650582006-06-26 13:58:14 +0200822 iommu_range_reserve(tbl, start, npages);
823
824 /* reserve the two PCI peripheral memory regions in IO space */
825 calgary_reserve_peripheral_mem_1(dev);
826 calgary_reserve_peripheral_mem_2(dev);
827}
828
829static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
830{
831 u64 val64;
832 u64 table_phys;
833 void __iomem *target;
834 int ret;
835 struct iommu_table *tbl;
836
837 /* build TCE tables for each PHB */
838 ret = build_tce_table(dev, bbar);
839 if (ret)
840 return ret;
841
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200842 tbl = dev->sysdata;
843 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
844 tce_free(tbl, 0, tbl->it_size);
845
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200846 if (is_calgary(dev->device))
847 tbl->chip_ops = &calgary_chip_ops;
848 else if (is_calioc2(dev->device))
849 tbl->chip_ops = &calioc2_chip_ops;
850 else
851 BUG();
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200852
Jon Masone4650582006-06-26 13:58:14 +0200853 calgary_reserve_regions(dev);
854
855 /* set TARs for each PHB */
856 target = calgary_reg(bbar, tar_offset(dev->bus->number));
857 val64 = be64_to_cpu(readq(target));
858
859 /* zero out all TAR bits under sw control */
860 val64 &= ~TAR_SW_BITS;
Jon Masone4650582006-06-26 13:58:14 +0200861 table_phys = (u64)__pa(tbl->it_base);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200862
Jon Masone4650582006-06-26 13:58:14 +0200863 val64 |= table_phys;
864
865 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
866 val64 |= (u64) specified_table_size;
867
868 tbl->tar_val = cpu_to_be64(val64);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200869
Jon Masone4650582006-06-26 13:58:14 +0200870 writeq(tbl->tar_val, target);
871 readq(target); /* flush */
872
873 return 0;
874}
875
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200876static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200877{
878 u64 val64;
879 struct iommu_table *tbl = dev->sysdata;
880 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200881 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200882
883 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
884 val64 = be64_to_cpu(readq(target));
885 val64 &= ~TAR_SW_BITS;
886 writeq(cpu_to_be64(val64), target);
887 readq(target); /* flush */
888
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200889 bitmapsz = tbl->it_size / BITS_PER_BYTE;
890 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
891 tbl->it_map = NULL;
892
Jon Masone4650582006-06-26 13:58:14 +0200893 kfree(tbl);
894 dev->sysdata = NULL;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200895
896 /* Can't free bootmem allocated memory after system is up :-( */
897 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200898}
899
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200900static void calgary_dump_error_regs(struct iommu_table *tbl)
901{
902 void __iomem *bbar = tbl->bbar;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200903 void __iomem *target;
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200904 u32 csr, plssr;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200905
906 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200907 csr = be32_to_cpu(readl(target));
908
909 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
910 plssr = be32_to_cpu(readl(target));
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200911
912 /* If no error, the agent ID in the CSR is not valid */
913 printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200914 "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200915}
916
917static void calioc2_dump_error_regs(struct iommu_table *tbl)
918{
919 void __iomem *bbar = tbl->bbar;
920 u32 csr, csmr, plssr, mck, rcstat;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200921 void __iomem *target;
922 unsigned long phboff = phb_offset(tbl->it_busno);
923 unsigned long erroff;
924 u32 errregs[7];
925 int i;
926
927 /* dump CSR */
928 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
929 csr = be32_to_cpu(readl(target));
930 /* dump PLSSR */
931 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
932 plssr = be32_to_cpu(readl(target));
933 /* dump CSMR */
934 target = calgary_reg(bbar, phboff | 0x290);
935 csmr = be32_to_cpu(readl(target));
936 /* dump mck */
937 target = calgary_reg(bbar, phboff | 0x800);
938 mck = be32_to_cpu(readl(target));
939
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200940 printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
941 tbl->it_busno);
942
943 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
944 csr, plssr, csmr, mck);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200945
946 /* dump rest of error regs */
947 printk(KERN_EMERG "Calgary: ");
948 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
949 erroff = (0x810 + (i * 0x10)); /* err regs are at 0x810 - 0x870 */
950 target = calgary_reg(bbar, phboff | erroff);
951 errregs[i] = be32_to_cpu(readl(target));
952 printk("0x%08x@0x%lx ", errregs[i], erroff);
953 }
954 printk("\n");
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200955
956 /* root complex status */
957 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
958 rcstat = be32_to_cpu(readl(target));
959 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
960 PHB_ROOT_COMPLEX_STATUS);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200961}
962
Jon Masone4650582006-06-26 13:58:14 +0200963static void calgary_watchdog(unsigned long data)
964{
965 struct pci_dev *dev = (struct pci_dev *)data;
966 struct iommu_table *tbl = dev->sysdata;
967 void __iomem *bbar = tbl->bbar;
968 u32 val32;
969 void __iomem *target;
970
971 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
972 val32 = be32_to_cpu(readl(target));
973
974 /* If no error, the agent ID in the CSR is not valid */
975 if (val32 & CSR_AGENT_MASK) {
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200976 tbl->chip_ops->dump_error_regs(tbl);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200977
978 /* reset error */
Jon Masone4650582006-06-26 13:58:14 +0200979 writel(0, target);
980
981 /* Disable bus that caused the error */
982 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200983 PHB_CONFIG_RW_OFFSET);
Jon Masone4650582006-06-26 13:58:14 +0200984 val32 = be32_to_cpu(readl(target));
985 val32 |= PHB_SLOT_DISABLE;
986 writel(cpu_to_be32(val32), target);
987 readl(target); /* flush */
988 } else {
989 /* Reset the timer */
990 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
991 }
992}
993
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200994static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
995 unsigned char busnum, unsigned long timeout)
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200996{
997 u64 val64;
998 void __iomem *target;
Muli Ben-Yehuda58db8542006-12-07 02:14:06 +0100999 unsigned int phb_shift = ~0; /* silence gcc */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +02001000 u64 mask;
1001
1002 switch (busno_to_phbid(busnum)) {
1003 case 0: phb_shift = (63 - 19);
1004 break;
1005 case 1: phb_shift = (63 - 23);
1006 break;
1007 case 2: phb_shift = (63 - 27);
1008 break;
1009 case 3: phb_shift = (63 - 35);
1010 break;
1011 default:
1012 BUG_ON(busno_to_phbid(busnum));
1013 }
1014
1015 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
1016 val64 = be64_to_cpu(readq(target));
1017
1018 /* zero out this PHB's timer bits */
1019 mask = ~(0xFUL << phb_shift);
1020 val64 &= mask;
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +02001021 val64 |= (timeout << phb_shift);
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +02001022 writeq(cpu_to_be64(val64), target);
1023 readq(target); /* flush */
1024}
1025
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001026static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1027{
1028 unsigned char busnum = dev->bus->number;
1029 void __iomem *bbar = tbl->bbar;
1030 void __iomem *target;
1031 u32 val;
1032
1033 /*
1034 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
1035 */
1036 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
1037 val = cpu_to_be32(readl(target));
1038 val |= 0x00800000;
1039 writel(cpu_to_be32(val), target);
1040}
1041
1042static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001043{
1044 unsigned char busnum = dev->bus->number;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001045
1046 /*
1047 * Give split completion a longer timeout on bus 1 for aic94xx
1048 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
1049 */
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001050 if (is_calgary(dev->device) && (busnum == 1))
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001051 calgary_set_split_completion_timeout(tbl->bbar, busnum,
1052 CCR_2SEC_TIMEOUT);
1053}
1054
Jon Masone4650582006-06-26 13:58:14 +02001055static void __init calgary_enable_translation(struct pci_dev *dev)
1056{
1057 u32 val32;
1058 unsigned char busnum;
1059 void __iomem *target;
1060 void __iomem *bbar;
1061 struct iommu_table *tbl;
1062
1063 busnum = dev->bus->number;
1064 tbl = dev->sysdata;
1065 bbar = tbl->bbar;
1066
1067 /* enable TCE in PHB Config Register */
1068 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1069 val32 = be32_to_cpu(readl(target));
1070 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
1071
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001072 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
1073 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
1074 "Calgary" : "CalIOC2", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001075 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
1076 "bus.\n");
1077
1078 writel(cpu_to_be32(val32), target);
1079 readl(target); /* flush */
1080
1081 init_timer(&tbl->watchdog_timer);
1082 tbl->watchdog_timer.function = &calgary_watchdog;
1083 tbl->watchdog_timer.data = (unsigned long)dev;
1084 mod_timer(&tbl->watchdog_timer, jiffies);
1085}
1086
1087static void __init calgary_disable_translation(struct pci_dev *dev)
1088{
1089 u32 val32;
1090 unsigned char busnum;
1091 void __iomem *target;
1092 void __iomem *bbar;
1093 struct iommu_table *tbl;
1094
1095 busnum = dev->bus->number;
1096 tbl = dev->sysdata;
1097 bbar = tbl->bbar;
1098
1099 /* disable TCE in PHB Config Register */
1100 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1101 val32 = be32_to_cpu(readl(target));
1102 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1103
Jon Mason70d666d2006-10-05 18:47:21 +02001104 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001105 writel(cpu_to_be32(val32), target);
1106 readl(target); /* flush */
1107
1108 del_timer_sync(&tbl->watchdog_timer);
1109}
1110
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +02001111static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +02001112{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001113 pci_dev_get(dev);
Jon Masone4650582006-06-26 13:58:14 +02001114 dev->sysdata = NULL;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001115
1116 /* is the device behind a bridge? */
1117 if (dev->bus->parent)
1118 dev->bus->parent->self = dev;
1119 else
1120 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +02001121}
1122
1123static int __init calgary_init_one(struct pci_dev *dev)
1124{
Jon Masone4650582006-06-26 13:58:14 +02001125 void __iomem *bbar;
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001126 struct iommu_table *tbl;
Jon Masone4650582006-06-26 13:58:14 +02001127 int ret;
1128
Jon Masondedc9932006-10-05 18:47:21 +02001129 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1130
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001131 bbar = busno_to_bbar(dev->bus->number);
Jon Masone4650582006-06-26 13:58:14 +02001132 ret = calgary_setup_tar(dev, bbar);
1133 if (ret)
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001134 goto done;
Jon Masone4650582006-06-26 13:58:14 +02001135
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001136 pci_dev_get(dev);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001137
1138 if (dev->bus->parent) {
1139 if (dev->bus->parent->self)
1140 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1141 "bus->parent->self!\n", dev);
1142 dev->bus->parent->self = dev;
1143 } else
1144 dev->bus->self = dev;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001145
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001146 tbl = dev->sysdata;
1147 tbl->chip_ops->handle_quirks(tbl, dev);
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001148
Jon Masone4650582006-06-26 13:58:14 +02001149 calgary_enable_translation(dev);
1150
1151 return 0;
1152
Jon Masone4650582006-06-26 13:58:14 +02001153done:
1154 return ret;
1155}
1156
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001157static int __init calgary_locate_bbars(void)
Jon Masone4650582006-06-26 13:58:14 +02001158{
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001159 int ret;
1160 int rioidx, phb, bus;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001161 void __iomem *bbar;
1162 void __iomem *target;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001163 unsigned long offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001164 u8 start_bus, end_bus;
1165 u32 val;
1166
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001167 ret = -ENODATA;
1168 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1169 struct rio_detail *rio = rio_devs[rioidx];
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001170
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001171 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001172 continue;
1173
1174 /* map entire 1MB of Calgary config space */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001175 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1176 if (!bbar)
1177 goto error;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001178
1179 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001180 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1181 target = calgary_reg(bbar, offset);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001182
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001183 val = be32_to_cpu(readl(target));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001184
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001185 start_bus = (u8)((val & 0x00FF0000) >> 16);
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001186 end_bus = (u8)((val & 0x0000FF00) >> 8);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001187
1188 if (end_bus) {
1189 for (bus = start_bus; bus <= end_bus; bus++) {
1190 bus_info[bus].bbar = bbar;
1191 bus_info[bus].phbid = phb;
1192 }
1193 } else {
1194 bus_info[start_bus].bbar = bbar;
1195 bus_info[start_bus].phbid = phb;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001196 }
1197 }
1198 }
1199
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001200 return 0;
1201
1202error:
1203 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1204 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1205 if (bus_info[bus].bbar)
1206 iounmap(bus_info[bus].bbar);
1207
1208 return ret;
1209}
1210
1211static int __init calgary_init(void)
1212{
1213 int ret;
1214 struct pci_dev *dev = NULL;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001215 void* tce_space;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001216
1217 ret = calgary_locate_bbars();
1218 if (ret)
1219 return ret;
Jon Masone4650582006-06-26 13:58:14 +02001220
Jon Masondedc9932006-10-05 18:47:21 +02001221 do {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001222 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Jon Masone4650582006-06-26 13:58:14 +02001223 if (!dev)
1224 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001225 if (!is_cal_pci_dev(dev->device))
1226 continue;
Jon Masone4650582006-06-26 13:58:14 +02001227 if (!translate_phb(dev)) {
1228 calgary_init_one_nontraslated(dev);
1229 continue;
1230 }
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001231 tce_space = bus_info[dev->bus->number].tce_space;
1232 if (!tce_space && !translate_empty_slots) {
1233 printk("Calg: %p failed tce_space check\n", dev);
Jon Masone4650582006-06-26 13:58:14 +02001234 continue;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001235 }
Jon Masone4650582006-06-26 13:58:14 +02001236 ret = calgary_init_one(dev);
1237 if (ret)
1238 goto error;
Jon Masondedc9932006-10-05 18:47:21 +02001239 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001240
1241 return ret;
1242
1243error:
Jon Masondedc9932006-10-05 18:47:21 +02001244 do {
Alan Cox7cd8b682006-12-07 02:14:03 +01001245 dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001246 PCI_ANY_ID, dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +02001247 if (!dev)
1248 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001249 if (!is_cal_pci_dev(dev->device))
1250 continue;
Jon Masone4650582006-06-26 13:58:14 +02001251 if (!translate_phb(dev)) {
1252 pci_dev_put(dev);
1253 continue;
1254 }
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001255 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001256 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001257
Jon Masone4650582006-06-26 13:58:14 +02001258 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +02001259 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001260 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Jon Masondedc9932006-10-05 18:47:21 +02001261 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001262
1263 return ret;
1264}
1265
1266static inline int __init determine_tce_table_size(u64 ram)
1267{
1268 int ret;
1269
1270 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1271 return specified_table_size;
1272
1273 /*
1274 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1275 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1276 * larger table size has twice as many entries, so shift the
1277 * max ram address by 13 to divide by 8K and then look at the
1278 * order of the result to choose between 0-7.
1279 */
1280 ret = get_order(ram >> 13);
1281 if (ret > TCE_TABLE_SIZE_8M)
1282 ret = TCE_TABLE_SIZE_8M;
1283
1284 return ret;
1285}
1286
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001287static int __init build_detail_arrays(void)
1288{
1289 unsigned long ptr;
1290 int i, scal_detail_size, rio_detail_size;
1291
1292 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1293 printk(KERN_WARNING
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001294 "Calgary: MAX_NUMNODES too low! Defined as %d, "
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001295 "but system has %d nodes.\n",
1296 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1297 return -ENODEV;
1298 }
1299
1300 switch (rio_table_hdr->version){
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001301 case 2:
1302 scal_detail_size = 11;
1303 rio_detail_size = 13;
1304 break;
1305 case 3:
1306 scal_detail_size = 12;
1307 rio_detail_size = 15;
1308 break;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001309 default:
1310 printk(KERN_WARNING
1311 "Calgary: Invalid Rio Grande Table Version: %d\n",
1312 rio_table_hdr->version);
1313 return -EPROTO;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001314 }
1315
1316 ptr = ((unsigned long)rio_table_hdr) + 3;
1317 for (i = 0; i < rio_table_hdr->num_scal_dev;
1318 i++, ptr += scal_detail_size)
1319 scal_devs[i] = (struct scal_detail *)ptr;
1320
1321 for (i = 0; i < rio_table_hdr->num_rio_dev;
1322 i++, ptr += rio_detail_size)
1323 rio_devs[i] = (struct rio_detail *)ptr;
1324
1325 return 0;
1326}
1327
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001328static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1329{
1330 int dev;
1331 u32 val;
1332
1333 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1334 /*
1335 * FIXME: properly scan for devices accross the
1336 * PCI-to-PCI bridge on every CalIOC2 port.
1337 */
1338 return 1;
1339 }
1340
1341 for (dev = 1; dev < 8; dev++) {
1342 val = read_pci_config(bus, dev, 0, 0);
1343 if (val != 0xffffffff)
1344 break;
1345 }
1346 return (val != 0xffffffff);
1347}
1348
Jon Masone4650582006-06-26 13:58:14 +02001349void __init detect_calgary(void)
1350{
Jon Masond2105b12006-07-29 21:42:43 +02001351 int bus;
Jon Masone4650582006-06-26 13:58:14 +02001352 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +02001353 int calgary_found = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001354 unsigned long ptr;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001355 unsigned int offset, prev_offset;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001356 int ret;
Jon Masone4650582006-06-26 13:58:14 +02001357
1358 /*
1359 * if the user specified iommu=off or iommu=soft or we found
1360 * another HW IOMMU already, bail out.
1361 */
1362 if (swiotlb || no_iommu || iommu_detected)
1363 return;
1364
Muli Ben-Yehudabff65472006-12-07 02:14:07 +01001365 if (!use_calgary)
1366 return;
1367
Andi Kleen0637a702006-09-26 10:52:41 +02001368 if (!early_pci_allowed())
1369 return;
1370
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001371 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1372
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001373 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1374
1375 rio_table_hdr = NULL;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001376 prev_offset = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001377 offset = 0x180;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001378 /*
1379 * The next offset is stored in the 1st word.
1380 * Only parse up until the offset increases:
1381 */
1382 while (offset > prev_offset) {
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001383 /* The block id is stored in the 2nd word */
1384 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1385 /* set the pointer past the offset & block id */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001386 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001387 break;
1388 }
Ingo Molnar136f1e72006-12-20 11:53:32 +01001389 prev_offset = offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001390 offset = *((unsigned short *)(ptr + offset));
1391 }
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001392 if (!rio_table_hdr) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001393 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1394 "in EBDA - bailing!\n");
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001395 return;
1396 }
1397
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001398 ret = build_detail_arrays();
1399 if (ret) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001400 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001401 return;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001402 }
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001403
Jon Masone4650582006-06-26 13:58:14 +02001404 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
1405
Jon Masond2105b12006-07-29 21:42:43 +02001406 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001407 struct calgary_bus_info *info = &bus_info[bus];
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001408 unsigned short pci_device;
1409 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +02001410
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001411 val = read_pci_config(bus, 0, 0, 0);
1412 pci_device = (val & 0xFFFF0000) >> 16;
1413
1414 if (!is_cal_pci_dev(pci_device))
Jon Masone4650582006-06-26 13:58:14 +02001415 continue;
Jon Masond2105b12006-07-29 21:42:43 +02001416
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001417 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +02001418 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001419
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001420 if (calgary_bus_has_devices(bus, pci_device) ||
1421 translate_empty_slots) {
1422 tbl = alloc_tce_table();
1423 if (!tbl)
1424 goto cleanup;
1425 info->tce_space = tbl;
1426 calgary_found = 1;
1427 printk("Calg: allocated tce_table %p for bus 0x%x\n",
1428 info->tce_space, bus);
Jon Masond2105b12006-07-29 21:42:43 +02001429 }
Jon Masone4650582006-06-26 13:58:14 +02001430 }
1431
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001432 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1433 calgary_found ? "found" : "not found");
1434
Jon Masond2105b12006-07-29 21:42:43 +02001435 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +02001436 iommu_detected = 1;
1437 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +02001438 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1439 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1440 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1441 debugging ? "enabled" : "disabled");
Jon Masone4650582006-06-26 13:58:14 +02001442 }
1443 return;
1444
1445cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001446 for (--bus; bus >= 0; --bus) {
1447 struct calgary_bus_info *info = &bus_info[bus];
1448
1449 if (info->tce_space)
1450 free_tce_table(info->tce_space);
1451 }
Jon Masone4650582006-06-26 13:58:14 +02001452}
1453
1454int __init calgary_iommu_init(void)
1455{
1456 int ret;
1457
1458 if (no_iommu || swiotlb)
1459 return -ENODEV;
1460
1461 if (!calgary_detected)
1462 return -ENODEV;
1463
1464 /* ok, we're trying to use Calgary - let's roll */
1465 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1466
1467 ret = calgary_init();
1468 if (ret) {
1469 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1470 "falling back to no_iommu\n", ret);
1471 if (end_pfn > MAX_DMA32_PFN)
1472 printk(KERN_ERR "WARNING more than 4GB of memory, "
1473 "32bit PCI may malfunction.\n");
1474 return ret;
1475 }
1476
1477 force_iommu = 1;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +01001478 bad_dma_address = 0x0;
Jon Masone4650582006-06-26 13:58:14 +02001479 dma_ops = &calgary_dma_ops;
1480
1481 return 0;
1482}
1483
1484static int __init calgary_parse_options(char *p)
1485{
1486 unsigned int bridge;
1487 size_t len;
1488 char* endp;
1489
1490 while (*p) {
1491 if (!strncmp(p, "64k", 3))
1492 specified_table_size = TCE_TABLE_SIZE_64K;
1493 else if (!strncmp(p, "128k", 4))
1494 specified_table_size = TCE_TABLE_SIZE_128K;
1495 else if (!strncmp(p, "256k", 4))
1496 specified_table_size = TCE_TABLE_SIZE_256K;
1497 else if (!strncmp(p, "512k", 4))
1498 specified_table_size = TCE_TABLE_SIZE_512K;
1499 else if (!strncmp(p, "1M", 2))
1500 specified_table_size = TCE_TABLE_SIZE_1M;
1501 else if (!strncmp(p, "2M", 2))
1502 specified_table_size = TCE_TABLE_SIZE_2M;
1503 else if (!strncmp(p, "4M", 2))
1504 specified_table_size = TCE_TABLE_SIZE_4M;
1505 else if (!strncmp(p, "8M", 2))
1506 specified_table_size = TCE_TABLE_SIZE_8M;
1507
1508 len = strlen("translate_empty_slots");
1509 if (!strncmp(p, "translate_empty_slots", len))
1510 translate_empty_slots = 1;
1511
1512 len = strlen("disable");
1513 if (!strncmp(p, "disable", len)) {
1514 p += len;
1515 if (*p == '=')
1516 ++p;
1517 if (*p == '\0')
1518 break;
1519 bridge = simple_strtol(p, &endp, 0);
1520 if (p == endp)
1521 break;
1522
Jon Masond2105b12006-07-29 21:42:43 +02001523 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001524 printk(KERN_INFO "Calgary: disabling "
Jon Mason70d666d2006-10-05 18:47:21 +02001525 "translation for PHB %#x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001526 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001527 }
1528 }
1529
1530 p = strpbrk(p, ",");
1531 if (!p)
1532 break;
1533
1534 p++; /* skip ',' */
1535 }
1536 return 1;
1537}
1538__setup("calgary=", calgary_parse_options);