blob: 7c43cb0f71a3a984cccf94695aacfb1749d859b0 [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-Yehuda796e4392006-09-26 10:52:33 +0200136
137static inline unsigned long verify_bit_range(unsigned long* bitmap,
138 int expected, unsigned long start, unsigned long end)
139{
140 unsigned long idx = start;
141
142 BUG_ON(start >= end);
143
144 while (idx < end) {
145 if (!!test_bit(idx, bitmap) != expected)
146 return idx;
147 ++idx;
148 }
149
150 /* all bits have the expected value */
151 return ~0UL;
152}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200153#else /* debugging is disabled */
154int debugging __read_mostly = 0;
155
Jon Masone4650582006-06-26 13:58:14 +0200156static inline void tce_cache_blast_stress(struct iommu_table *tbl)
157{
158}
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200159
160static inline unsigned long verify_bit_range(unsigned long* bitmap,
161 int expected, unsigned long start, unsigned long end)
162{
163 return ~0UL;
164}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200165#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200166
167static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
168{
169 unsigned int npages;
170
171 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
172 npages >>= PAGE_SHIFT;
173
174 return npages;
175}
176
177static inline int translate_phb(struct pci_dev* dev)
178{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200179 int disabled = bus_info[dev->bus->number].translation_disabled;
Jon Masone4650582006-06-26 13:58:14 +0200180 return !disabled;
181}
182
183static void iommu_range_reserve(struct iommu_table *tbl,
184 unsigned long start_addr, unsigned int npages)
185{
186 unsigned long index;
187 unsigned long end;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200188 unsigned long badbit;
Jon Masone4650582006-06-26 13:58:14 +0200189
190 index = start_addr >> PAGE_SHIFT;
191
192 /* bail out if we're asked to reserve a region we don't cover */
193 if (index >= tbl->it_size)
194 return;
195
196 end = index + npages;
197 if (end > tbl->it_size) /* don't go off the table */
198 end = tbl->it_size;
199
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200200 badbit = verify_bit_range(tbl->it_map, 0, index, end);
201 if (badbit != ~0UL) {
202 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200203 printk(KERN_ERR "Calgary: entry already allocated at "
204 "0x%lx tbl %p dma 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200205 badbit, tbl, start_addr, npages);
Jon Masone4650582006-06-26 13:58:14 +0200206 }
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200207
208 set_bit_string(tbl->it_map, index, npages);
Jon Masone4650582006-06-26 13:58:14 +0200209}
210
211static unsigned long iommu_range_alloc(struct iommu_table *tbl,
212 unsigned int npages)
213{
214 unsigned long offset;
215
216 BUG_ON(npages == 0);
217
218 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
219 tbl->it_size, npages);
220 if (offset == ~0UL) {
221 tce_cache_blast(tbl);
222 offset = find_next_zero_string(tbl->it_map, 0,
223 tbl->it_size, npages);
224 if (offset == ~0UL) {
225 printk(KERN_WARNING "Calgary: IOMMU full.\n");
226 if (panic_on_overflow)
227 panic("Calgary: fix the allocator.\n");
228 else
229 return bad_dma_address;
230 }
231 }
232
233 set_bit_string(tbl->it_map, offset, npages);
234 tbl->it_hint = offset + npages;
235 BUG_ON(tbl->it_hint > tbl->it_size);
236
237 return offset;
238}
239
240static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
241 unsigned int npages, int direction)
242{
243 unsigned long entry, flags;
244 dma_addr_t ret = bad_dma_address;
245
246 spin_lock_irqsave(&tbl->it_lock, flags);
247
248 entry = iommu_range_alloc(tbl, npages);
249
250 if (unlikely(entry == bad_dma_address))
251 goto error;
252
253 /* set the return dma address */
254 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
255
256 /* put the TCEs in the HW table */
257 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
258 direction);
259
260 spin_unlock_irqrestore(&tbl->it_lock, flags);
261
262 return ret;
263
264error:
265 spin_unlock_irqrestore(&tbl->it_lock, flags);
266 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
267 "iommu %p\n", npages, tbl);
268 return bad_dma_address;
269}
270
271static void __iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
272 unsigned int npages)
273{
274 unsigned long entry;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200275 unsigned long badbit;
Jon Masone4650582006-06-26 13:58:14 +0200276
277 entry = dma_addr >> PAGE_SHIFT;
278
279 BUG_ON(entry + npages > tbl->it_size);
280
281 tce_free(tbl, entry, npages);
282
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200283 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
284 if (badbit != ~0UL) {
285 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200286 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
287 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200288 badbit, tbl, dma_addr, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200289 }
290
291 __clear_bit_string(tbl->it_map, entry, npages);
292
293 tce_cache_blast_stress(tbl);
294}
295
296static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
297 unsigned int npages)
298{
299 unsigned long flags;
300
301 spin_lock_irqsave(&tbl->it_lock, flags);
302
303 __iommu_free(tbl, dma_addr, npages);
304
305 spin_unlock_irqrestore(&tbl->it_lock, flags);
306}
307
308static void __calgary_unmap_sg(struct iommu_table *tbl,
309 struct scatterlist *sglist, int nelems, int direction)
310{
311 while (nelems--) {
312 unsigned int npages;
313 dma_addr_t dma = sglist->dma_address;
314 unsigned int dmalen = sglist->dma_length;
315
316 if (dmalen == 0)
317 break;
318
319 npages = num_dma_pages(dma, dmalen);
320 __iommu_free(tbl, dma, npages);
321 sglist++;
322 }
323}
324
325void calgary_unmap_sg(struct device *dev, struct scatterlist *sglist,
326 int nelems, int direction)
327{
328 unsigned long flags;
329 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
330
331 if (!translate_phb(to_pci_dev(dev)))
332 return;
333
334 spin_lock_irqsave(&tbl->it_lock, flags);
335
336 __calgary_unmap_sg(tbl, sglist, nelems, direction);
337
338 spin_unlock_irqrestore(&tbl->it_lock, flags);
339}
340
341static int calgary_nontranslate_map_sg(struct device* dev,
342 struct scatterlist *sg, int nelems, int direction)
343{
344 int i;
345
346 for (i = 0; i < nelems; i++ ) {
347 struct scatterlist *s = &sg[i];
348 BUG_ON(!s->page);
349 s->dma_address = virt_to_bus(page_address(s->page) +s->offset);
350 s->dma_length = s->length;
351 }
352 return nelems;
353}
354
355int calgary_map_sg(struct device *dev, struct scatterlist *sg,
356 int nelems, int direction)
357{
358 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
359 unsigned long flags;
360 unsigned long vaddr;
361 unsigned int npages;
362 unsigned long entry;
363 int i;
364
365 if (!translate_phb(to_pci_dev(dev)))
366 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
367
368 spin_lock_irqsave(&tbl->it_lock, flags);
369
370 for (i = 0; i < nelems; i++ ) {
371 struct scatterlist *s = &sg[i];
372 BUG_ON(!s->page);
373
374 vaddr = (unsigned long)page_address(s->page) + s->offset;
375 npages = num_dma_pages(vaddr, s->length);
376
377 entry = iommu_range_alloc(tbl, npages);
378 if (entry == bad_dma_address) {
379 /* makes sure unmap knows to stop */
380 s->dma_length = 0;
381 goto error;
382 }
383
384 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
385
386 /* insert into HW table */
387 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
388 direction);
389
390 s->dma_length = s->length;
391 }
392
393 spin_unlock_irqrestore(&tbl->it_lock, flags);
394
395 return nelems;
396error:
397 __calgary_unmap_sg(tbl, sg, nelems, direction);
398 for (i = 0; i < nelems; i++) {
399 sg[i].dma_address = bad_dma_address;
400 sg[i].dma_length = 0;
401 }
402 spin_unlock_irqrestore(&tbl->it_lock, flags);
403 return 0;
404}
405
406dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
407 size_t size, int direction)
408{
409 dma_addr_t dma_handle = bad_dma_address;
410 unsigned long uaddr;
411 unsigned int npages;
412 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
413
414 uaddr = (unsigned long)vaddr;
415 npages = num_dma_pages(uaddr, size);
416
417 if (translate_phb(to_pci_dev(dev)))
418 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
419 else
420 dma_handle = virt_to_bus(vaddr);
421
422 return dma_handle;
423}
424
425void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
426 size_t size, int direction)
427{
428 struct iommu_table *tbl = to_pci_dev(dev)->bus->self->sysdata;
429 unsigned int npages;
430
431 if (!translate_phb(to_pci_dev(dev)))
432 return;
433
434 npages = num_dma_pages(dma_handle, size);
435 iommu_free(tbl, dma_handle, npages);
436}
437
438void* calgary_alloc_coherent(struct device *dev, size_t size,
439 dma_addr_t *dma_handle, gfp_t flag)
440{
441 void *ret = NULL;
442 dma_addr_t mapping;
443 unsigned int npages, order;
444 struct iommu_table *tbl;
445
446 tbl = to_pci_dev(dev)->bus->self->sysdata;
447
448 size = PAGE_ALIGN(size); /* size rounded up to full pages */
449 npages = size >> PAGE_SHIFT;
450 order = get_order(size);
451
452 /* alloc enough pages (and possibly more) */
453 ret = (void *)__get_free_pages(flag, order);
454 if (!ret)
455 goto error;
456 memset(ret, 0, size);
457
458 if (translate_phb(to_pci_dev(dev))) {
459 /* set up tces to cover the allocated range */
460 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
461 if (mapping == bad_dma_address)
462 goto free;
463
464 *dma_handle = mapping;
465 } else /* non translated slot */
466 *dma_handle = virt_to_bus(ret);
467
468 return ret;
469
470free:
471 free_pages((unsigned long)ret, get_order(size));
472 ret = NULL;
473error:
474 return ret;
475}
476
477static struct dma_mapping_ops calgary_dma_ops = {
478 .alloc_coherent = calgary_alloc_coherent,
479 .map_single = calgary_map_single,
480 .unmap_single = calgary_unmap_single,
481 .map_sg = calgary_map_sg,
482 .unmap_sg = calgary_unmap_sg,
483};
484
485static inline int busno_to_phbid(unsigned char num)
486{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200487 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200488}
489
490static inline unsigned long split_queue_offset(unsigned char num)
491{
492 size_t idx = busno_to_phbid(num);
493
494 return split_queue_offsets[idx];
495}
496
497static inline unsigned long tar_offset(unsigned char num)
498{
499 size_t idx = busno_to_phbid(num);
500
501 return tar_offsets[idx];
502}
503
504static inline unsigned long phb_offset(unsigned char num)
505{
506 size_t idx = busno_to_phbid(num);
507
508 return phb_offsets[idx];
509}
510
511static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
512{
513 unsigned long target = ((unsigned long)bar) | offset;
514 return (void __iomem*)target;
515}
516
517static void tce_cache_blast(struct iommu_table *tbl)
518{
519 u64 val;
520 u32 aer;
521 int i = 0;
522 void __iomem *bbar = tbl->bbar;
523 void __iomem *target;
524
525 /* disable arbitration on the bus */
526 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
527 aer = readl(target);
528 writel(0, target);
529
530 /* read plssr to ensure it got there */
531 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
532 val = readl(target);
533
534 /* poll split queues until all DMA activity is done */
535 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
536 do {
537 val = readq(target);
538 i++;
539 } while ((val & 0xff) != 0xff && i < 100);
540 if (i == 100)
541 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
542 "continuing anyway\n");
543
544 /* invalidate TCE cache */
545 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
546 writeq(tbl->tar_val, target);
547
548 /* enable arbitration */
549 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
550 writel(aer, target);
551 (void)readl(target); /* flush */
552}
553
554static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
555 u64 limit)
556{
557 unsigned int numpages;
558
559 limit = limit | 0xfffff;
560 limit++;
561
562 numpages = ((limit - start) >> PAGE_SHIFT);
563 iommu_range_reserve(dev->sysdata, start, numpages);
564}
565
566static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
567{
568 void __iomem *target;
569 u64 low, high, sizelow;
570 u64 start, limit;
571 struct iommu_table *tbl = dev->sysdata;
572 unsigned char busnum = dev->bus->number;
573 void __iomem *bbar = tbl->bbar;
574
575 /* peripheral MEM_1 region */
576 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
577 low = be32_to_cpu(readl(target));
578 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
579 high = be32_to_cpu(readl(target));
580 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
581 sizelow = be32_to_cpu(readl(target));
582
583 start = (high << 32) | low;
584 limit = sizelow;
585
586 calgary_reserve_mem_region(dev, start, limit);
587}
588
589static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
590{
591 void __iomem *target;
592 u32 val32;
593 u64 low, high, sizelow, sizehigh;
594 u64 start, limit;
595 struct iommu_table *tbl = dev->sysdata;
596 unsigned char busnum = dev->bus->number;
597 void __iomem *bbar = tbl->bbar;
598
599 /* is it enabled? */
600 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
601 val32 = be32_to_cpu(readl(target));
602 if (!(val32 & PHB_MEM2_ENABLE))
603 return;
604
605 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
606 low = be32_to_cpu(readl(target));
607 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
608 high = be32_to_cpu(readl(target));
609 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
610 sizelow = be32_to_cpu(readl(target));
611 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
612 sizehigh = be32_to_cpu(readl(target));
613
614 start = (high << 32) | low;
615 limit = (sizehigh << 32) | sizelow;
616
617 calgary_reserve_mem_region(dev, start, limit);
618}
619
620/*
621 * some regions of the IO address space do not get translated, so we
622 * must not give devices IO addresses in those regions. The regions
623 * are the 640KB-1MB region and the two PCI peripheral memory holes.
624 * Reserve all of them in the IOMMU bitmap to avoid giving them out
625 * later.
626 */
627static void __init calgary_reserve_regions(struct pci_dev *dev)
628{
629 unsigned int npages;
630 void __iomem *bbar;
631 unsigned char busnum;
632 u64 start;
633 struct iommu_table *tbl = dev->sysdata;
634
635 bbar = tbl->bbar;
636 busnum = dev->bus->number;
637
638 /* reserve bad_dma_address in case it's a legal address */
639 iommu_range_reserve(tbl, bad_dma_address, 1);
640
641 /* avoid the BIOS/VGA first 640KB-1MB region */
642 start = (640 * 1024);
643 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
644 iommu_range_reserve(tbl, start, npages);
645
646 /* reserve the two PCI peripheral memory regions in IO space */
647 calgary_reserve_peripheral_mem_1(dev);
648 calgary_reserve_peripheral_mem_2(dev);
649}
650
651static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
652{
653 u64 val64;
654 u64 table_phys;
655 void __iomem *target;
656 int ret;
657 struct iommu_table *tbl;
658
659 /* build TCE tables for each PHB */
660 ret = build_tce_table(dev, bbar);
661 if (ret)
662 return ret;
663
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200664 tbl = dev->sysdata;
665 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
666 tce_free(tbl, 0, tbl->it_size);
667
Jon Masone4650582006-06-26 13:58:14 +0200668 calgary_reserve_regions(dev);
669
670 /* set TARs for each PHB */
671 target = calgary_reg(bbar, tar_offset(dev->bus->number));
672 val64 = be64_to_cpu(readq(target));
673
674 /* zero out all TAR bits under sw control */
675 val64 &= ~TAR_SW_BITS;
676
677 tbl = dev->sysdata;
678 table_phys = (u64)__pa(tbl->it_base);
679 val64 |= table_phys;
680
681 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
682 val64 |= (u64) specified_table_size;
683
684 tbl->tar_val = cpu_to_be64(val64);
685 writeq(tbl->tar_val, target);
686 readq(target); /* flush */
687
688 return 0;
689}
690
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200691static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200692{
693 u64 val64;
694 struct iommu_table *tbl = dev->sysdata;
695 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200696 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200697
698 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
699 val64 = be64_to_cpu(readq(target));
700 val64 &= ~TAR_SW_BITS;
701 writeq(cpu_to_be64(val64), target);
702 readq(target); /* flush */
703
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200704 bitmapsz = tbl->it_size / BITS_PER_BYTE;
705 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
706 tbl->it_map = NULL;
707
Jon Masone4650582006-06-26 13:58:14 +0200708 kfree(tbl);
709 dev->sysdata = NULL;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200710
711 /* Can't free bootmem allocated memory after system is up :-( */
712 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200713}
714
715static void calgary_watchdog(unsigned long data)
716{
717 struct pci_dev *dev = (struct pci_dev *)data;
718 struct iommu_table *tbl = dev->sysdata;
719 void __iomem *bbar = tbl->bbar;
720 u32 val32;
721 void __iomem *target;
722
723 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
724 val32 = be32_to_cpu(readl(target));
725
726 /* If no error, the agent ID in the CSR is not valid */
727 if (val32 & CSR_AGENT_MASK) {
728 printk(KERN_EMERG "calgary_watchdog: DMA error on bus %d, "
729 "CSR = %#x\n", dev->bus->number, val32);
730 writel(0, target);
731
732 /* Disable bus that caused the error */
733 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
734 PHB_CONFIG_RW_OFFSET);
735 val32 = be32_to_cpu(readl(target));
736 val32 |= PHB_SLOT_DISABLE;
737 writel(cpu_to_be32(val32), target);
738 readl(target); /* flush */
739 } else {
740 /* Reset the timer */
741 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
742 }
743}
744
745static void __init calgary_enable_translation(struct pci_dev *dev)
746{
747 u32 val32;
748 unsigned char busnum;
749 void __iomem *target;
750 void __iomem *bbar;
751 struct iommu_table *tbl;
752
753 busnum = dev->bus->number;
754 tbl = dev->sysdata;
755 bbar = tbl->bbar;
756
757 /* enable TCE in PHB Config Register */
758 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
759 val32 = be32_to_cpu(readl(target));
760 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
761
762 printk(KERN_INFO "Calgary: enabling translation on PHB %d\n", busnum);
763 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
764 "bus.\n");
765
766 writel(cpu_to_be32(val32), target);
767 readl(target); /* flush */
768
769 init_timer(&tbl->watchdog_timer);
770 tbl->watchdog_timer.function = &calgary_watchdog;
771 tbl->watchdog_timer.data = (unsigned long)dev;
772 mod_timer(&tbl->watchdog_timer, jiffies);
773}
774
775static void __init calgary_disable_translation(struct pci_dev *dev)
776{
777 u32 val32;
778 unsigned char busnum;
779 void __iomem *target;
780 void __iomem *bbar;
781 struct iommu_table *tbl;
782
783 busnum = dev->bus->number;
784 tbl = dev->sysdata;
785 bbar = tbl->bbar;
786
787 /* disable TCE in PHB Config Register */
788 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
789 val32 = be32_to_cpu(readl(target));
790 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
791
792 printk(KERN_INFO "Calgary: disabling translation on PHB %d!\n", busnum);
793 writel(cpu_to_be32(val32), target);
794 readl(target); /* flush */
795
796 del_timer_sync(&tbl->watchdog_timer);
797}
798
799static inline unsigned int __init locate_register_space(struct pci_dev *dev)
800{
801 int rionodeid;
802 u32 address;
803
804 rionodeid = (dev->bus->number % 15 > 4) ? 3 : 2;
805 /*
806 * register space address calculation as follows:
807 * FE0MB-8MB*OneBasedChassisNumber+1MB*(RioNodeId-ChassisBase)
808 * ChassisBase is always zero for x366/x260/x460
809 * RioNodeId is 2 for first Calgary, 3 for second Calgary
810 */
811 address = START_ADDRESS -
812 (0x800000 * (ONE_BASED_CHASSIS_NUM + dev->bus->number / 15)) +
813 (0x100000) * (rionodeid - CHASSIS_BASE);
814 return address;
815}
816
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +0200817static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200818{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200819 pci_dev_get(dev);
Jon Masone4650582006-06-26 13:58:14 +0200820 dev->sysdata = NULL;
821 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +0200822}
823
824static int __init calgary_init_one(struct pci_dev *dev)
825{
826 u32 address;
827 void __iomem *bbar;
828 int ret;
829
830 address = locate_register_space(dev);
831 /* map entire 1MB of Calgary config space */
832 bbar = ioremap_nocache(address, 1024 * 1024);
833 if (!bbar) {
834 ret = -ENODATA;
835 goto done;
836 }
837
838 ret = calgary_setup_tar(dev, bbar);
839 if (ret)
840 goto iounmap;
841
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200842 pci_dev_get(dev);
Jon Masone4650582006-06-26 13:58:14 +0200843 dev->bus->self = dev;
844 calgary_enable_translation(dev);
845
846 return 0;
847
848iounmap:
849 iounmap(bbar);
850done:
851 return ret;
852}
853
854static int __init calgary_init(void)
855{
856 int i, ret = -ENODEV;
857 struct pci_dev *dev = NULL;
858
Jon Masond2105b12006-07-29 21:42:43 +0200859 for (i = 0; i < MAX_PHB_BUS_NUM; i++) {
Jon Masone4650582006-06-26 13:58:14 +0200860 dev = pci_get_device(PCI_VENDOR_ID_IBM,
861 PCI_DEVICE_ID_IBM_CALGARY,
862 dev);
863 if (!dev)
864 break;
865 if (!translate_phb(dev)) {
866 calgary_init_one_nontraslated(dev);
867 continue;
868 }
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200869 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +0200870 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200871
Jon Masone4650582006-06-26 13:58:14 +0200872 ret = calgary_init_one(dev);
873 if (ret)
874 goto error;
875 }
876
877 return ret;
878
879error:
880 for (i--; i >= 0; i--) {
881 dev = pci_find_device_reverse(PCI_VENDOR_ID_IBM,
882 PCI_DEVICE_ID_IBM_CALGARY,
883 dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +0200884 if (!dev)
885 break;
Jon Masone4650582006-06-26 13:58:14 +0200886 if (!translate_phb(dev)) {
887 pci_dev_put(dev);
888 continue;
889 }
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200890 if (!bus_info[dev->bus->number].tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +0200891 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200892
Jon Masone4650582006-06-26 13:58:14 +0200893 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200894 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +0200895 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Jon Masone4650582006-06-26 13:58:14 +0200896 }
897
898 return ret;
899}
900
901static inline int __init determine_tce_table_size(u64 ram)
902{
903 int ret;
904
905 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
906 return specified_table_size;
907
908 /*
909 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
910 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
911 * larger table size has twice as many entries, so shift the
912 * max ram address by 13 to divide by 8K and then look at the
913 * order of the result to choose between 0-7.
914 */
915 ret = get_order(ram >> 13);
916 if (ret > TCE_TABLE_SIZE_8M)
917 ret = TCE_TABLE_SIZE_8M;
918
919 return ret;
920}
921
922void __init detect_calgary(void)
923{
924 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +0200925 int bus;
Jon Masone4650582006-06-26 13:58:14 +0200926 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +0200927 int calgary_found = 0;
928 int phb = -1;
Jon Masone4650582006-06-26 13:58:14 +0200929
930 /*
931 * if the user specified iommu=off or iommu=soft or we found
932 * another HW IOMMU already, bail out.
933 */
934 if (swiotlb || no_iommu || iommu_detected)
935 return;
936
937 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
938
Jon Masond2105b12006-07-29 21:42:43 +0200939 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
940 int dev;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200941 struct calgary_bus_info *info = &bus_info[bus];
942 info->phbid = -1;
Jon Masond2105b12006-07-29 21:42:43 +0200943
Jon Masone4650582006-06-26 13:58:14 +0200944 if (read_pci_config(bus, 0, 0, 0) != PCI_VENDOR_DEVICE_ID_CALGARY)
945 continue;
Jon Masond2105b12006-07-29 21:42:43 +0200946
947 /*
948 * There are 4 PHBs per Calgary chip. Set phb to which phb (0-3)
949 * it is connected to releative to the clagary chip.
950 */
951 phb = (phb + 1) % PHBS_PER_CALGARY;
952
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200953 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +0200954 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200955
Jon Masone4650582006-06-26 13:58:14 +0200956 /*
Jon Masond2105b12006-07-29 21:42:43 +0200957 * Scan the slots of the PCI bus to see if there is a device present.
958 * The parent bus will be the zero-ith device, so start at 1.
Jon Masone4650582006-06-26 13:58:14 +0200959 */
Jon Masond2105b12006-07-29 21:42:43 +0200960 for (dev = 1; dev < 8; dev++) {
961 val = read_pci_config(bus, dev, 0, 0);
962 if (val != 0xffffffff || translate_empty_slots) {
963 tbl = alloc_tce_table();
964 if (!tbl)
965 goto cleanup;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200966 info->tce_space = tbl;
967 info->phbid = phb;
Jon Masond2105b12006-07-29 21:42:43 +0200968 calgary_found = 1;
969 break;
970 }
971 }
Jon Masone4650582006-06-26 13:58:14 +0200972 }
973
Jon Masond2105b12006-07-29 21:42:43 +0200974 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +0200975 iommu_detected = 1;
976 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200977 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
978 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
979 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
980 debugging ? "enabled" : "disabled");
Jon Masone4650582006-06-26 13:58:14 +0200981 }
982 return;
983
984cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200985 for (--bus; bus >= 0; --bus) {
986 struct calgary_bus_info *info = &bus_info[bus];
987
988 if (info->tce_space)
989 free_tce_table(info->tce_space);
990 }
Jon Masone4650582006-06-26 13:58:14 +0200991}
992
993int __init calgary_iommu_init(void)
994{
995 int ret;
996
997 if (no_iommu || swiotlb)
998 return -ENODEV;
999
1000 if (!calgary_detected)
1001 return -ENODEV;
1002
1003 /* ok, we're trying to use Calgary - let's roll */
1004 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1005
1006 ret = calgary_init();
1007 if (ret) {
1008 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1009 "falling back to no_iommu\n", ret);
1010 if (end_pfn > MAX_DMA32_PFN)
1011 printk(KERN_ERR "WARNING more than 4GB of memory, "
1012 "32bit PCI may malfunction.\n");
1013 return ret;
1014 }
1015
1016 force_iommu = 1;
1017 dma_ops = &calgary_dma_ops;
1018
1019 return 0;
1020}
1021
1022static int __init calgary_parse_options(char *p)
1023{
1024 unsigned int bridge;
1025 size_t len;
1026 char* endp;
1027
1028 while (*p) {
1029 if (!strncmp(p, "64k", 3))
1030 specified_table_size = TCE_TABLE_SIZE_64K;
1031 else if (!strncmp(p, "128k", 4))
1032 specified_table_size = TCE_TABLE_SIZE_128K;
1033 else if (!strncmp(p, "256k", 4))
1034 specified_table_size = TCE_TABLE_SIZE_256K;
1035 else if (!strncmp(p, "512k", 4))
1036 specified_table_size = TCE_TABLE_SIZE_512K;
1037 else if (!strncmp(p, "1M", 2))
1038 specified_table_size = TCE_TABLE_SIZE_1M;
1039 else if (!strncmp(p, "2M", 2))
1040 specified_table_size = TCE_TABLE_SIZE_2M;
1041 else if (!strncmp(p, "4M", 2))
1042 specified_table_size = TCE_TABLE_SIZE_4M;
1043 else if (!strncmp(p, "8M", 2))
1044 specified_table_size = TCE_TABLE_SIZE_8M;
1045
1046 len = strlen("translate_empty_slots");
1047 if (!strncmp(p, "translate_empty_slots", len))
1048 translate_empty_slots = 1;
1049
1050 len = strlen("disable");
1051 if (!strncmp(p, "disable", len)) {
1052 p += len;
1053 if (*p == '=')
1054 ++p;
1055 if (*p == '\0')
1056 break;
1057 bridge = simple_strtol(p, &endp, 0);
1058 if (p == endp)
1059 break;
1060
Jon Masond2105b12006-07-29 21:42:43 +02001061 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001062 printk(KERN_INFO "Calgary: disabling "
1063 "translation for PHB 0x%x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001064 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001065 }
1066 }
1067
1068 p = strpbrk(p, ",");
1069 if (!p)
1070 break;
1071
1072 p++; /* skip ',' */
1073 }
1074 return 1;
1075}
1076__setup("calgary=", calgary_parse_options);