blob: ebe4e930b64d193b1648968a9ce6b9604788ec42 [file] [log] [blame]
Jon Masone4650582006-06-26 13:58:14 +02001/*
2 * Derived from arch/powerpc/kernel/iommu.c
3 *
Muli Ben-Yehudaaa0a9f32006-07-10 17:06:15 +02004 * Copyright (C) IBM Corporation, 2006
Jon Masone4650582006-06-26 13:58:14 +02005 *
Muli Ben-Yehudaaa0a9f32006-07-10 17:06:15 +02006 * Author: Jon Mason <jdmason@us.ibm.com>
7 * Author: Muli Ben-Yehuda <muli@il.ibm.com>
8
Jon Masone4650582006-06-26 13:58:14 +02009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <linux/config.h>
25#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/slab.h>
29#include <linux/mm.h>
30#include <linux/spinlock.h>
31#include <linux/string.h>
32#include <linux/dma-mapping.h>
33#include <linux/init.h>
34#include <linux/bitops.h>
35#include <linux/pci_ids.h>
36#include <linux/pci.h>
37#include <linux/delay.h>
38#include <asm/proto.h>
39#include <asm/calgary.h>
40#include <asm/tce.h>
41#include <asm/pci-direct.h>
42#include <asm/system.h>
43#include <asm/dma.h>
44
45#define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
46#define PCI_VENDOR_DEVICE_ID_CALGARY \
47 (PCI_VENDOR_ID_IBM | PCI_DEVICE_ID_IBM_CALGARY << 16)
48
49/* we need these for register space address calculation */
50#define START_ADDRESS 0xfe000000
51#define CHASSIS_BASE 0
52#define ONE_BASED_CHASSIS_NUM 1
53
54/* register offsets inside the host bridge space */
55#define PHB_CSR_OFFSET 0x0110
56#define PHB_PLSSR_OFFSET 0x0120
57#define PHB_CONFIG_RW_OFFSET 0x0160
58#define PHB_IOBASE_BAR_LOW 0x0170
59#define PHB_IOBASE_BAR_HIGH 0x0180
60#define PHB_MEM_1_LOW 0x0190
61#define PHB_MEM_1_HIGH 0x01A0
62#define PHB_IO_ADDR_SIZE 0x01B0
63#define PHB_MEM_1_SIZE 0x01C0
64#define PHB_MEM_ST_OFFSET 0x01D0
65#define PHB_AER_OFFSET 0x0200
66#define PHB_CONFIG_0_HIGH 0x0220
67#define PHB_CONFIG_0_LOW 0x0230
68#define PHB_CONFIG_0_END 0x0240
69#define PHB_MEM_2_LOW 0x02B0
70#define PHB_MEM_2_HIGH 0x02C0
71#define PHB_MEM_2_SIZE_HIGH 0x02D0
72#define PHB_MEM_2_SIZE_LOW 0x02E0
73#define PHB_DOSHOLE_OFFSET 0x08E0
74
75/* PHB_CONFIG_RW */
76#define PHB_TCE_ENABLE 0x20000000
77#define PHB_SLOT_DISABLE 0x1C000000
78#define PHB_DAC_DISABLE 0x01000000
79#define PHB_MEM2_ENABLE 0x00400000
80#define PHB_MCSR_ENABLE 0x00100000
81/* TAR (Table Address Register) */
82#define TAR_SW_BITS 0x0000ffffffff800fUL
83#define TAR_VALID 0x0000000000000008UL
84/* CSR (Channel/DMA Status Register) */
85#define CSR_AGENT_MASK 0xffe0ffff
86
87#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +020088#define MAX_NUM_CHASSIS 8 /* max number of chassis */
89#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2) /* max dev->bus->number */
Jon Masone4650582006-06-26 13:58:14 +020090#define PHBS_PER_CALGARY 4
91
92/* register offsets in Calgary's internal register space */
93static const unsigned long tar_offsets[] = {
94 0x0580 /* TAR0 */,
95 0x0588 /* TAR1 */,
96 0x0590 /* TAR2 */,
97 0x0598 /* TAR3 */
98};
99
100static const unsigned long split_queue_offsets[] = {
101 0x4870 /* SPLIT QUEUE 0 */,
102 0x5870 /* SPLIT QUEUE 1 */,
103 0x6870 /* SPLIT QUEUE 2 */,
104 0x7870 /* SPLIT QUEUE 3 */
105};
106
107static const unsigned long phb_offsets[] = {
108 0x8000 /* PHB0 */,
109 0x9000 /* PHB1 */,
110 0xA000 /* PHB2 */,
111 0xB000 /* PHB3 */
112};
113
Jon Masone4650582006-06-26 13:58:14 +0200114unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
115static int translate_empty_slots __read_mostly = 0;
116static int calgary_detected __read_mostly = 0;
117
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200118struct calgary_bus_info {
119 void *tce_space;
Muli Ben-Yehuda0577f142006-09-26 10:52:31 +0200120 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200121 signed char phbid;
122};
123
124static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200125
126static void tce_cache_blast(struct iommu_table *tbl);
127
128/* enable this to stress test the chip's TCE cache */
129#ifdef CONFIG_IOMMU_DEBUG
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200130int debugging __read_mostly = 1;
131
Jon Masone4650582006-06-26 13:58:14 +0200132static inline void tce_cache_blast_stress(struct iommu_table *tbl)
133{
134 tce_cache_blast(tbl);
135}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200136#else /* debugging is disabled */
137int debugging __read_mostly = 0;
138
Jon Masone4650582006-06-26 13:58:14 +0200139static inline void tce_cache_blast_stress(struct iommu_table *tbl)
140{
141}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200142#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200143
144static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
145{
146 unsigned int npages;
147
148 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
149 npages >>= PAGE_SHIFT;
150
151 return npages;
152}
153
154static inline int translate_phb(struct pci_dev* dev)
155{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200156 int disabled = bus_info[dev->bus->number].translation_disabled;
Jon Masone4650582006-06-26 13:58:14 +0200157 return !disabled;
158}
159
160static void iommu_range_reserve(struct iommu_table *tbl,
161 unsigned long start_addr, unsigned int npages)
162{
163 unsigned long index;
164 unsigned long end;
165
166 index = start_addr >> PAGE_SHIFT;
167
168 /* bail out if we're asked to reserve a region we don't cover */
169 if (index >= tbl->it_size)
170 return;
171
172 end = index + npages;
173 if (end > tbl->it_size) /* don't go off the table */
174 end = tbl->it_size;
175
176 while (index < end) {
177 if (test_bit(index, tbl->it_map))
178 printk(KERN_ERR "Calgary: entry already allocated at "
179 "0x%lx tbl %p dma 0x%lx npages %u\n",
180 index, tbl, start_addr, npages);
181 ++index;
182 }
183 set_bit_string(tbl->it_map, start_addr >> PAGE_SHIFT, npages);
184}
185
186static unsigned long iommu_range_alloc(struct iommu_table *tbl,
187 unsigned int npages)
188{
189 unsigned long offset;
190
191 BUG_ON(npages == 0);
192
193 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
194 tbl->it_size, npages);
195 if (offset == ~0UL) {
196 tce_cache_blast(tbl);
197 offset = find_next_zero_string(tbl->it_map, 0,
198 tbl->it_size, npages);
199 if (offset == ~0UL) {
200 printk(KERN_WARNING "Calgary: IOMMU full.\n");
201 if (panic_on_overflow)
202 panic("Calgary: fix the allocator.\n");
203 else
204 return bad_dma_address;
205 }
206 }
207
208 set_bit_string(tbl->it_map, offset, npages);
209 tbl->it_hint = offset + npages;
210 BUG_ON(tbl->it_hint > tbl->it_size);
211
212 return offset;
213}
214
215static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
216 unsigned int npages, int direction)
217{
218 unsigned long entry, flags;
219 dma_addr_t ret = bad_dma_address;
220
221 spin_lock_irqsave(&tbl->it_lock, flags);
222
223 entry = iommu_range_alloc(tbl, npages);
224
225 if (unlikely(entry == bad_dma_address))
226 goto error;
227
228 /* set the return dma address */
229 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
230
231 /* put the TCEs in the HW table */
232 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
233 direction);
234
235 spin_unlock_irqrestore(&tbl->it_lock, flags);
236
237 return ret;
238
239error:
240 spin_unlock_irqrestore(&tbl->it_lock, flags);
241 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
242 "iommu %p\n", npages, tbl);
243 return bad_dma_address;
244}
245
246static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
247 unsigned int npages)
248{
249 unsigned long entry;
250 unsigned long i;
251
252 entry = dma_addr >> PAGE_SHIFT;
253
254 BUG_ON(entry + npages > tbl->it_size);
255
256 tce_free(tbl, entry, npages);
257
258 for (i = 0; i < npages; ++i) {
259 if (!test_bit(entry + i, tbl->it_map))
260 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
261 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
262 entry + i, tbl, dma_addr, entry, npages);
263 }
264
265 __clear_bit_string(tbl->it_map, entry, npages);
266
267 tce_cache_blast_stress(tbl);
268}
269
270static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
271 unsigned int npages)
272{
273 unsigned long flags;
274
275 spin_lock_irqsave(&tbl->it_lock, flags);
276
277 __iommu_free(tbl, dma_addr, npages);
278
279 spin_unlock_irqrestore(&tbl->it_lock, flags);
280}
281
282static void __calgary_unmap_sg(struct iommu_table *tbl,
283 struct scatterlist *sglist, int nelems, int direction)
284{
285 while (nelems--) {
286 unsigned int npages;
287 dma_addr_t dma = sglist->dma_address;
288 unsigned int dmalen = sglist->dma_length;
289
290 if (dmalen == 0)
291 break;
292
293 npages = num_dma_pages(dma, dmalen);
294 __iommu_free(tbl, dma, npages);
295 sglist++;
296 }
297}
298
299void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
300 int nelems, int direction)
301{
302 unsigned long flags;
303 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
304
305 if (!translate_phb(to_pci_dev(dev)))
306 return;
307
308 spin_lock_irqsave(&tbl->it_lock, flags);
309
310 __calgary_unmap_sg(tbl, sglist, nelems, direction);
311
312 spin_unlock_irqrestore(&tbl->it_lock, flags);
313}
314
315static int calgary_nontranslate_map_sg(struct device* dev,
316 struct scatterlist *sg, int nelems, int direction)
317{
318 int i;
319
320 for (i = 0; i < nelems; i++ ) {
321 struct scatterlist *s = &sg[i];
322 BUG_ON(!s->page);
323 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
324 s->dma_length = s->length;
325 }
326 return nelems;
327}
328
329int calgary_map_sg(struct device *dev, struct scatterlist *sg,
330 int nelems, int direction)
331{
332 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
333 unsigned long flags;
334 unsigned long vaddr;
335 unsigned int npages;
336 unsigned long entry;
337 int i;
338
339 if (!translate_phb(to_pci_dev(dev)))
340 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
341
342 spin_lock_irqsave(&tbl->it_lock, flags);
343
344 for (i = 0; i < nelems; i++ ) {
345 struct scatterlist *s = &sg[i];
346 BUG_ON(!s->page);
347
348 vaddr = (unsigned long)page_address(s->page) + s->offset;
349 npages = num_dma_pages(vaddr, s->length);
350
351 entry = iommu_range_alloc(tbl, npages);
352 if (entry == bad_dma_address) {
353 /* makes sure unmap knows to stop */
354 s->dma_length = 0;
355 goto error;
356 }
357
358 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
359
360 /* insert into HW table */
361 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
362 direction);
363
364 s->dma_length = s->length;
365 }
366
367 spin_unlock_irqrestore(&tbl->it_lock, flags);
368
369 return nelems;
370error:
371 __calgary_unmap_sg(tbl, sg, nelems, direction);
372 for (i = 0; i < nelems; i++) {
373 sg[i].dma_address = bad_dma_address;
374 sg[i].dma_length = 0;
375 }
376 spin_unlock_irqrestore(&tbl->it_lock, flags);
377 return 0;
378}
379
380dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
381 size_t size, int direction)
382{
383 dma_addr_t dma_handle = bad_dma_address;
384 unsigned long uaddr;
385 unsigned int npages;
386 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
387
388 uaddr = (unsigned long)vaddr;
389 npages = num_dma_pages(uaddr, size);
390
391 if (translate_phb(to_pci_dev(dev)))
392 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
393 else
394 dma_handle = virt_to_bus(vaddr);
395
396 return dma_handle;
397}
398
399void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
400 size_t size, int direction)
401{
402 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
403 unsigned int npages;
404
405 if (!translate_phb(to_pci_dev(dev)))
406 return;
407
408 npages = num_dma_pages(dma_handle, size);
409 iommu_free(tbl, dma_handle, npages);
410}
411
412void* calgary_alloc_coherent(struct device *dev, size_t size,
413 dma_addr_t *dma_handle, gfp_t flag)
414{
415 void *ret = NULL;
416 dma_addr_t mapping;
417 unsigned int npages, order;
418 struct iommu_table *tbl;
419
420 tbl = to_pci_dev(dev)->bus->self->sysdata;
421
422 size = PAGE_ALIGN(size); /* size rounded up to full pages */
423 npages = size >> PAGE_SHIFT;
424 order = get_order(size);
425
426 /* alloc enough pages (and possibly more) */
427 ret = (void *)__get_free_pages(flag, order);
428 if (!ret)
429 goto error;
430 memset(ret, 0, size);
431
432 if (translate_phb(to_pci_dev(dev))) {
433 /* set up tces to cover the allocated range */
434 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
435 if (mapping == bad_dma_address)
436 goto free;
437
438 *dma_handle = mapping;
439 } else /* non translated slot */
440 *dma_handle = virt_to_bus(ret);
441
442 return ret;
443
444free:
445 free_pages((unsigned long)ret, get_order(size));
446 ret = NULL;
447error:
448 return ret;
449}
450
451static struct dma_mapping_ops calgary_dma_ops = {
452 .alloc_coherent = calgary_alloc_coherent,
453 .map_single = calgary_map_single,
454 .unmap_single = calgary_unmap_single,
455 .map_sg = calgary_map_sg,
456 .unmap_sg = calgary_unmap_sg,
457};
458
459static inline int busno_to_phbid(unsigned char num)
460{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200461 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200462}
463
464static inline unsigned long split_queue_offset(unsigned char num)
465{
466 size_t idx = busno_to_phbid(num);
467
468 return split_queue_offsets[idx];
469}
470
471static inline unsigned long tar_offset(unsigned char num)
472{
473 size_t idx = busno_to_phbid(num);
474
475 return tar_offsets[idx];
476}
477
478static inline unsigned long phb_offset(unsigned char num)
479{
480 size_t idx = busno_to_phbid(num);
481
482 return phb_offsets[idx];
483}
484
485static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
486{
487 unsigned long target = ((unsigned long)bar) | offset;
488 return (void __iomem*)target;
489}
490
491static void tce_cache_blast(struct iommu_table *tbl)
492{
493 u64 val;
494 u32 aer;
495 int i = 0;
496 void __iomem *bbar = tbl->bbar;
497 void __iomem *target;
498
499 /* disable arbitration on the bus */
500 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
501 aer = readl(target);
502 writel(0, target);
503
504 /* read plssr to ensure it got there */
505 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
506 val = readl(target);
507
508 /* poll split queues until all DMA activity is done */
509 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
510 do {
511 val = readq(target);
512 i++;
513 } while ((val & 0xff) != 0xff && i < 100);
514 if (i == 100)
515 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
516 "continuing anyway\n");
517
518 /* invalidate TCE cache */
519 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
520 writeq(tbl->tar_val, target);
521
522 /* enable arbitration */
523 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
524 writel(aer, target);
525 (void)readl(target); /* flush */
526}
527
528static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
529 u64 limit)
530{
531 unsigned int numpages;
532
533 limit = limit | 0xfffff;
534 limit++;
535
536 numpages = ((limit - start) >> PAGE_SHIFT);
537 iommu_range_reserve(dev->sysdata, start, numpages);
538}
539
540static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
541{
542 void __iomem *target;
543 u64 low, high, sizelow;
544 u64 start, limit;
545 struct iommu_table *tbl = dev->sysdata;
546 unsigned char busnum = dev->bus->number;
547 void __iomem *bbar = tbl->bbar;
548
549 /* peripheral MEM_1 region */
550 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
551 low = be32_to_cpu(readl(target));
552 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
553 high = be32_to_cpu(readl(target));
554 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
555 sizelow = be32_to_cpu(readl(target));
556
557 start = (high << 32) | low;
558 limit = sizelow;
559
560 calgary_reserve_mem_region(dev, start, limit);
561}
562
563static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
564{
565 void __iomem *target;
566 u32 val32;
567 u64 low, high, sizelow, sizehigh;
568 u64 start, limit;
569 struct iommu_table *tbl = dev->sysdata;
570 unsigned char busnum = dev->bus->number;
571 void __iomem *bbar = tbl->bbar;
572
573 /* is it enabled? */
574 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
575 val32 = be32_to_cpu(readl(target));
576 if (!(val32 & PHB_MEM2_ENABLE))
577 return;
578
579 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
580 low = be32_to_cpu(readl(target));
581 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
582 high = be32_to_cpu(readl(target));
583 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
584 sizelow = be32_to_cpu(readl(target));
585 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
586 sizehigh = be32_to_cpu(readl(target));
587
588 start = (high << 32) | low;
589 limit = (sizehigh << 32) | sizelow;
590
591 calgary_reserve_mem_region(dev, start, limit);
592}
593
594/*
595 * some regions of the IO address space do not get translated, so we
596 * must not give devices IO addresses in those regions. The regions
597 * are the 640KB-1MB region and the two PCI peripheral memory holes.
598 * Reserve all of them in the IOMMU bitmap to avoid giving them out
599 * later.
600 */
601static void __init calgary_reserve_regions(struct pci_dev *dev)
602{
603 unsigned int npages;
604 void __iomem *bbar;
605 unsigned char busnum;
606 u64 start;
607 struct iommu_table *tbl = dev->sysdata;
608
609 bbar = tbl->bbar;
610 busnum = dev->bus->number;
611
612 /* reserve bad_dma_address in case it's a legal address */
613 iommu_range_reserve(tbl, bad_dma_address, 1);
614
615 /* avoid the BIOS/VGA first 640KB-1MB region */
616 start = (640 * 1024);
617 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
618 iommu_range_reserve(tbl, start, npages);
619
620 /* reserve the two PCI peripheral memory regions in IO space */
621 calgary_reserve_peripheral_mem_1(dev);
622 calgary_reserve_peripheral_mem_2(dev);
623}
624
625static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
626{
627 u64 val64;
628 u64 table_phys;
629 void __iomem *target;
630 int ret;
631 struct iommu_table *tbl;
632
633 /* build TCE tables for each PHB */
634 ret = build_tce_table(dev, bbar);
635 if (ret)
636 return ret;
637
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200638 tbl = dev->sysdata;
639 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
640 tce_free(tbl, 0, tbl->it_size);
641
Jon Masone4650582006-06-26 13:58:14 +0200642 calgary_reserve_regions(dev);
643
644 /* set TARs for each PHB */
645 target = calgary_reg(bbar, tar_offset(dev->bus->number));
646 val64 = be64_to_cpu(readq(target));
647
648 /* zero out all TAR bits under sw control */
649 val64 &= ~TAR_SW_BITS;
650
651 tbl = dev->sysdata;
652 table_phys = (u64)__pa(tbl->it_base);
653 val64 |= table_phys;
654
655 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
656 val64 |= (u64) specified_table_size;
657
658 tbl->tar_val = cpu_to_be64(val64);
659 writeq(tbl->tar_val, target);
660 readq(target); /* flush */
661
662 return 0;
663}
664
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200665static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200666{
667 u64 val64;
668 struct iommu_table *tbl = dev->sysdata;
669 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200670 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200671
672 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
673 val64 = be64_to_cpu(readq(target));
674 val64 &= ~TAR_SW_BITS;
675 writeq(cpu_to_be64(val64), target);
676 readq(target); /* flush */
677
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200678 bitmapsz = tbl->it_size / BITS_PER_BYTE;
679 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
680 tbl->it_map = NULL;
681
Jon Masone4650582006-06-26 13:58:14 +0200682 kfree(tbl);
683 dev->sysdata = NULL;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200684
685 /* Can't free bootmem allocated memory after system is up :-( */
686 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200687}
688
689static void calgary_watchdog(unsigned long data)
690{
691 struct pci_dev *dev = (struct pci_dev *)data;
692 struct iommu_table *tbl = dev->sysdata;
693 void __iomem *bbar = tbl->bbar;
694 u32 val32;
695 void __iomem *target;
696
697 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
698 val32 = be32_to_cpu(readl(target));
699
700 /* If no error, the agent ID in the CSR is not valid */
701 if (val32 & CSR_AGENT_MASK) {
702 printk(KERN_EMERG "calgary_watchdog: DMA error on bus %d, "
703 "CSR = %#x\n", dev->bus->number, val32);
704 writel(0, target);
705
706 /* Disable bus that caused the error */
707 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
708 PHB_CONFIG_RW_OFFSET);
709 val32 = be32_to_cpu(readl(target));
710 val32 |= PHB_SLOT_DISABLE;
711 writel(cpu_to_be32(val32), target);
712 readl(target); /* flush */
713 } else {
714 /* Reset the timer */
715 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
716 }
717}
718
719static void __init calgary_enable_translation(struct pci_dev *dev)
720{
721 u32 val32;
722 unsigned char busnum;
723 void __iomem *target;
724 void __iomem *bbar;
725 struct iommu_table *tbl;
726
727 busnum = dev->bus->number;
728 tbl = dev->sysdata;
729 bbar = tbl->bbar;
730
731 /* enable TCE in PHB Config Register */
732 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
733 val32 = be32_to_cpu(readl(target));
734 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
735
736 printk(KERN_INFO "Calgary: enabling translation on PHB %d\n", busnum);
737 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
738 "bus.\n");
739
740 writel(cpu_to_be32(val32), target);
741 readl(target); /* flush */
742
743 init_timer(&tbl->watchdog_timer);
744 tbl->watchdog_timer.function = &calgary_watchdog;
745 tbl->watchdog_timer.data = (unsigned long)dev;
746 mod_timer(&tbl->watchdog_timer, jiffies);
747}
748
749static void __init calgary_disable_translation(struct pci_dev *dev)
750{
751 u32 val32;
752 unsigned char busnum;
753 void __iomem *target;
754 void __iomem *bbar;
755 struct iommu_table *tbl;
756
757 busnum = dev->bus->number;
758 tbl = dev->sysdata;
759 bbar = tbl->bbar;
760
761 /* disable TCE in PHB Config Register */
762 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
763 val32 = be32_to_cpu(readl(target));
764 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
765
766 printk(KERN_INFO "Calgary: disabling translation on PHB %d!\n", busnum);
767 writel(cpu_to_be32(val32), target);
768 readl(target); /* flush */
769
770 del_timer_sync(&tbl->watchdog_timer);
771}
772
773static inline unsigned int __init locate_register_space(struct pci_dev *dev)
774{
775 int rionodeid;
776 u32 address;
777
778 rionodeid = (dev->bus->number % 15 > 4) ? 3 : 2;
779 /*
780 * register space address calculation as follows:
781 * FE0MB-8MB*OneBasedChassisNumber+1MB*(RioNodeId-ChassisBase)
782 * ChassisBase is always zero for x366/x260/x460
783 * RioNodeId is 2 for first Calgary, 3 for second Calgary
784 */
785 address = START_ADDRESS -
786 (0x800000 * (ONE_BASED_CHASSIS_NUM + dev->bus->number / 15)) +
787 (0x100000) * (rionodeid - CHASSIS_BASE);
788 return address;
789}
790
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +0200791static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200792{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200793 pci_dev_get(dev);
Jon Masone4650582006-06-26 13:58:14 +0200794 dev->sysdata = NULL;
795 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +0200796}
797
798static int __init calgary_init_one(struct pci_dev *dev)
799{
800 u32 address;
801 void __iomem *bbar;
802 int ret;
803
804 address = locate_register_space(dev);
805 /* map entire 1MB of Calgary config space */
806 bbar = ioremap_nocache(address, 1024 * 1024);
807 if (!bbar) {
808 ret = -ENODATA;
809 goto done;
810 }
811
812 ret = calgary_setup_tar(dev, bbar);
813 if (ret)
814 goto iounmap;
815
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200816 pci_dev_get(dev);
Jon Masone4650582006-06-26 13:58:14 +0200817 dev->bus->self = dev;
818 calgary_enable_translation(dev);
819
820 return 0;
821
822iounmap:
823 iounmap(bbar);
824done:
825 return ret;
826}
827
828static int __init calgary_init(void)
829{
830 int i, ret = -ENODEV;
831 struct pci_dev *dev = NULL;
832
Jon Masond2105b12006-07-29 21:42:43 +0200833 for (i = 0; i < MAX_PHB_BUS_NUM; i++) {
Jon Masone4650582006-06-26 13:58:14 +0200834 dev = pci_get_device(PCI_VENDOR_ID_IBM,
835 PCI_DEVICE_ID_IBM_CALGARY,
836 dev);
837 if (!dev)
838 break;
839 if (!translate_phb(dev)) {
840 calgary_init_one_nontraslated(dev);
841 continue;
842 }
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200843 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +0200844 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200845
Jon Masone4650582006-06-26 13:58:14 +0200846 ret = calgary_init_one(dev);
847 if (ret)
848 goto error;
849 }
850
851 return ret;
852
853error:
854 for (i--; i >= 0; i--) {
855 dev = pci_find_device_reverse(PCI_VENDOR_ID_IBM,
856 PCI_DEVICE_ID_IBM_CALGARY,
857 dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +0200858 if (!dev)
859 break;
Jon Masone4650582006-06-26 13:58:14 +0200860 if (!translate_phb(dev)) {
861 pci_dev_put(dev);
862 continue;
863 }
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200864 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +0200865 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200866
Jon Masone4650582006-06-26 13:58:14 +0200867 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200868 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200869 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Jon Masone4650582006-06-26 13:58:14 +0200870 }
871
872 return ret;
873}
874
875static inline int __init determine_tce_table_size(u64 ram)
876{
877 int ret;
878
879 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
880 return specified_table_size;
881
882 /*
883 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
884 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
885 * larger table size has twice as many entries, so shift the
886 * max ram address by 13 to divide by 8K and then look at the
887 * order of the result to choose between 0-7.
888 */
889 ret = get_order(ram >> 13);
890 if (ret > TCE_TABLE_SIZE_8M)
891 ret = TCE_TABLE_SIZE_8M;
892
893 return ret;
894}
895
896void __init detect_calgary(void)
897{
898 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +0200899 int bus;
Jon Masone4650582006-06-26 13:58:14 +0200900 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +0200901 int calgary_found = 0;
902 int phb = -1;
Jon Masone4650582006-06-26 13:58:14 +0200903
904 /*
905 * if the user specified iommu=off or iommu=soft or we found
906 * another HW IOMMU already, bail out.
907 */
908 if (swiotlb || no_iommu || iommu_detected)
909 return;
910
911 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
912
Jon Masond2105b12006-07-29 21:42:43 +0200913 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
914 int dev;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200915 struct calgary_bus_info *info = &bus_info[bus];
916 info->phbid = -1;
Jon Masond2105b12006-07-29 21:42:43 +0200917
Jon Masone4650582006-06-26 13:58:14 +0200918 if (read_pci_config(bus, 0, 0, 0) != PCI_VENDOR_DEVICE_ID_CALGARY)
919 continue;
Jon Masond2105b12006-07-29 21:42:43 +0200920
921 /*
922 * There are 4 PHBs per Calgary chip. Set phb to which phb (0-3)
923 * it is connected to releative to the clagary chip.
924 */
925 phb = (phb + 1) % PHBS_PER_CALGARY;
926
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200927 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +0200928 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200929
Jon Masone4650582006-06-26 13:58:14 +0200930 /*
Jon Masond2105b12006-07-29 21:42:43 +0200931 * Scan the slots of the PCI bus to see if there is a device present.
932 * The parent bus will be the zero-ith device, so start at 1.
Jon Masone4650582006-06-26 13:58:14 +0200933 */
Jon Masond2105b12006-07-29 21:42:43 +0200934 for (dev = 1; dev < 8; dev++) {
935 val = read_pci_config(bus, dev, 0, 0);
936 if (val != 0xffffffff || translate_empty_slots) {
937 tbl = alloc_tce_table();
938 if (!tbl)
939 goto cleanup;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200940 info->tce_space = tbl;
941 info->phbid = phb;
Jon Masond2105b12006-07-29 21:42:43 +0200942 calgary_found = 1;
943 break;
944 }
945 }
Jon Masone4650582006-06-26 13:58:14 +0200946 }
947
Jon Masond2105b12006-07-29 21:42:43 +0200948 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +0200949 iommu_detected = 1;
950 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200951 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
952 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
953 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
954 debugging ? "enabled" : "disabled");
Jon Masone4650582006-06-26 13:58:14 +0200955 }
956 return;
957
958cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200959 for (--bus; bus >= 0; --bus) {
960 struct calgary_bus_info *info = &bus_info[bus];
961
962 if (info->tce_space)
963 free_tce_table(info->tce_space);
964 }
Jon Masone4650582006-06-26 13:58:14 +0200965}
966
967int __init calgary_iommu_init(void)
968{
969 int ret;
970
971 if (no_iommu || swiotlb)
972 return -ENODEV;
973
974 if (!calgary_detected)
975 return -ENODEV;
976
977 /* ok, we're trying to use Calgary - let's roll */
978 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
979
980 ret = calgary_init();
981 if (ret) {
982 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
983 "falling back to no_iommu\n", ret);
984 if (end_pfn > MAX_DMA32_PFN)
985 printk(KERN_ERR "WARNING more than 4GB of memory, "
986 "32bit PCI may malfunction.\n");
987 return ret;
988 }
989
990 force_iommu = 1;
991 dma_ops = &calgary_dma_ops;
992
993 return 0;
994}
995
996static int __init calgary_parse_options(char *p)
997{
998 unsigned int bridge;
999 size_t len;
1000 char* endp;
1001
1002 while (*p) {
1003 if (!strncmp(p, "64k", 3))
1004 specified_table_size = TCE_TABLE_SIZE_64K;
1005 else if (!strncmp(p, "128k", 4))
1006 specified_table_size = TCE_TABLE_SIZE_128K;
1007 else if (!strncmp(p, "256k", 4))
1008 specified_table_size = TCE_TABLE_SIZE_256K;
1009 else if (!strncmp(p, "512k", 4))
1010 specified_table_size = TCE_TABLE_SIZE_512K;
1011 else if (!strncmp(p, "1M", 2))
1012 specified_table_size = TCE_TABLE_SIZE_1M;
1013 else if (!strncmp(p, "2M", 2))
1014 specified_table_size = TCE_TABLE_SIZE_2M;
1015 else if (!strncmp(p, "4M", 2))
1016 specified_table_size = TCE_TABLE_SIZE_4M;
1017 else if (!strncmp(p, "8M", 2))
1018 specified_table_size = TCE_TABLE_SIZE_8M;
1019
1020 len = strlen("translate_empty_slots");
1021 if (!strncmp(p, "translate_empty_slots", len))
1022 translate_empty_slots = 1;
1023
1024 len = strlen("disable");
1025 if (!strncmp(p, "disable", len)) {
1026 p += len;
1027 if (*p == '=')
1028 ++p;
1029 if (*p == '\0')
1030 break;
1031 bridge = simple_strtol(p, &endp, 0);
1032 if (p == endp)
1033 break;
1034
Jon Masond2105b12006-07-29 21:42:43 +02001035 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001036 printk(KERN_INFO "Calgary: disabling "
1037 "translation for PHB 0x%x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001038 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001039 }
1040 }
1041
1042 p = strpbrk(p, ",");
1043 if (!p)
1044 break;
1045
1046 p++; /* skip ',' */
1047 }
1048 return 1;
1049}
1050__setup("calgary=", calgary_parse_options);