blob: 6bf1f716909da5fddf6674adc4b0974f3fbd1ab6 [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>
Jens Axboe8b87d9f2007-07-24 12:38:15 +020038#include <linux/scatterlist.h>
Joerg Roedel395624f2007-10-24 12:49:47 +020039#include <asm/gart.h>
Jon Masone4650582006-06-26 13:58:14 +020040#include <asm/calgary.h>
41#include <asm/tce.h>
42#include <asm/pci-direct.h>
43#include <asm/system.h>
44#include <asm/dma.h>
Laurent Vivierb34e90b2006-12-07 02:14:06 +010045#include <asm/rio.h>
Jon Masone4650582006-06-26 13:58:14 +020046
Muli Ben-Yehudabff65472006-12-07 02:14:07 +010047#ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
48int use_calgary __read_mostly = 1;
49#else
50int use_calgary __read_mostly = 0;
51#endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
52
Jon Masone4650582006-06-26 13:58:14 +020053#define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +020054#define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
Jon Masone4650582006-06-26 13:58:14 +020055
Jon Masone4650582006-06-26 13:58:14 +020056/* register offsets inside the host bridge space */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020057#define CALGARY_CONFIG_REG 0x0108
58#define PHB_CSR_OFFSET 0x0110 /* Channel Status */
Jon Masone4650582006-06-26 13:58:14 +020059#define PHB_PLSSR_OFFSET 0x0120
60#define PHB_CONFIG_RW_OFFSET 0x0160
61#define PHB_IOBASE_BAR_LOW 0x0170
62#define PHB_IOBASE_BAR_HIGH 0x0180
63#define PHB_MEM_1_LOW 0x0190
64#define PHB_MEM_1_HIGH 0x01A0
65#define PHB_IO_ADDR_SIZE 0x01B0
66#define PHB_MEM_1_SIZE 0x01C0
67#define PHB_MEM_ST_OFFSET 0x01D0
68#define PHB_AER_OFFSET 0x0200
69#define PHB_CONFIG_0_HIGH 0x0220
70#define PHB_CONFIG_0_LOW 0x0230
71#define PHB_CONFIG_0_END 0x0240
72#define PHB_MEM_2_LOW 0x02B0
73#define PHB_MEM_2_HIGH 0x02C0
74#define PHB_MEM_2_SIZE_HIGH 0x02D0
75#define PHB_MEM_2_SIZE_LOW 0x02E0
76#define PHB_DOSHOLE_OFFSET 0x08E0
77
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020078/* CalIOC2 specific */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020079#define PHB_SAVIOR_L2 0x0DB0
80#define PHB_PAGE_MIG_CTRL 0x0DA8
81#define PHB_PAGE_MIG_DEBUG 0x0DA0
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +020082#define PHB_ROOT_COMPLEX_STATUS 0x0CB0
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020083
Jon Masone4650582006-06-26 13:58:14 +020084/* PHB_CONFIG_RW */
85#define PHB_TCE_ENABLE 0x20000000
86#define PHB_SLOT_DISABLE 0x1C000000
87#define PHB_DAC_DISABLE 0x01000000
88#define PHB_MEM2_ENABLE 0x00400000
89#define PHB_MCSR_ENABLE 0x00100000
90/* TAR (Table Address Register) */
91#define TAR_SW_BITS 0x0000ffffffff800fUL
92#define TAR_VALID 0x0000000000000008UL
93/* CSR (Channel/DMA Status Register) */
94#define CSR_AGENT_MASK 0xffe0ffff
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020095/* CCR (Calgary Configuration Register) */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020096#define CCR_2SEC_TIMEOUT 0x000000000000000EUL
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +020097/* PMCR/PMDR (Page Migration Control/Debug Registers */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020098#define PMR_SOFTSTOP 0x80000000
99#define PMR_SOFTSTOPFAULT 0x40000000
100#define PMR_HARDSTOP 0x20000000
Jon Masone4650582006-06-26 13:58:14 +0200101
102#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +0200103#define MAX_NUM_CHASSIS 8 /* max number of chassis */
Muli Ben-Yehuda4ea8a5d2006-09-26 10:52:33 +0200104/* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
105#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
Jon Masone4650582006-06-26 13:58:14 +0200106#define PHBS_PER_CALGARY 4
107
108/* register offsets in Calgary's internal register space */
109static const unsigned long tar_offsets[] = {
110 0x0580 /* TAR0 */,
111 0x0588 /* TAR1 */,
112 0x0590 /* TAR2 */,
113 0x0598 /* TAR3 */
114};
115
116static const unsigned long split_queue_offsets[] = {
117 0x4870 /* SPLIT QUEUE 0 */,
118 0x5870 /* SPLIT QUEUE 1 */,
119 0x6870 /* SPLIT QUEUE 2 */,
120 0x7870 /* SPLIT QUEUE 3 */
121};
122
123static const unsigned long phb_offsets[] = {
124 0x8000 /* PHB0 */,
125 0x9000 /* PHB1 */,
126 0xA000 /* PHB2 */,
127 0xB000 /* PHB3 */
128};
129
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100130/* PHB debug registers */
131
132static const unsigned long phb_debug_offsets[] = {
133 0x4000 /* PHB 0 DEBUG */,
134 0x5000 /* PHB 1 DEBUG */,
135 0x6000 /* PHB 2 DEBUG */,
136 0x7000 /* PHB 3 DEBUG */
137};
138
139/*
140 * STUFF register for each debug PHB,
141 * byte 1 = start bus number, byte 2 = end bus number
142 */
143
144#define PHB_DEBUG_STUFF_OFFSET 0x0020
145
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100146#define EMERGENCY_PAGES 32 /* = 128KB */
147
Jon Masone4650582006-06-26 13:58:14 +0200148unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
149static int translate_empty_slots __read_mostly = 0;
150static int calgary_detected __read_mostly = 0;
151
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100152static struct rio_table_hdr *rio_table_hdr __initdata;
153static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100154static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100155
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200156struct calgary_bus_info {
157 void *tce_space;
Muli Ben-Yehuda0577f142006-09-26 10:52:31 +0200158 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200159 signed char phbid;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100160 void __iomem *bbar;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200161};
162
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200163static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
164static void calgary_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200165static void calgary_dump_error_regs(struct iommu_table *tbl);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200166static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200167static void calioc2_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200168static void calioc2_dump_error_regs(struct iommu_table *tbl);
Jon Masone4650582006-06-26 13:58:14 +0200169
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200170static struct cal_chipset_ops calgary_chip_ops = {
171 .handle_quirks = calgary_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200172 .tce_cache_blast = calgary_tce_cache_blast,
173 .dump_error_regs = calgary_dump_error_regs
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200174};
175
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200176static struct cal_chipset_ops calioc2_chip_ops = {
177 .handle_quirks = calioc2_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200178 .tce_cache_blast = calioc2_tce_cache_blast,
179 .dump_error_regs = calioc2_dump_error_regs
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200180};
181
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200182static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200183
184/* enable this to stress test the chip's TCE cache */
185#ifdef CONFIG_IOMMU_DEBUG
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200186int debugging __read_mostly = 1;
187
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200188static inline unsigned long verify_bit_range(unsigned long* bitmap,
189 int expected, unsigned long start, unsigned long end)
190{
191 unsigned long idx = start;
192
193 BUG_ON(start >= end);
194
195 while (idx < end) {
196 if (!!test_bit(idx, bitmap) != expected)
197 return idx;
198 ++idx;
199 }
200
201 /* all bits have the expected value */
202 return ~0UL;
203}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200204#else /* debugging is disabled */
205int debugging __read_mostly = 0;
206
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200207static inline unsigned long verify_bit_range(unsigned long* bitmap,
208 int expected, unsigned long start, unsigned long end)
209{
210 return ~0UL;
211}
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200212
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200213#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200214
215static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
216{
217 unsigned int npages;
218
219 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
220 npages >>= PAGE_SHIFT;
221
222 return npages;
223}
224
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200225static inline int translation_enabled(struct iommu_table *tbl)
226{
227 /* only PHBs with translation enabled have an IOMMU table */
228 return (tbl != NULL);
229}
230
Jon Masone4650582006-06-26 13:58:14 +0200231static void iommu_range_reserve(struct iommu_table *tbl,
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200232 unsigned long start_addr, unsigned int npages)
Jon Masone4650582006-06-26 13:58:14 +0200233{
234 unsigned long index;
235 unsigned long end;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200236 unsigned long badbit;
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200237 unsigned long flags;
Jon Masone4650582006-06-26 13:58:14 +0200238
239 index = start_addr >> PAGE_SHIFT;
240
241 /* bail out if we're asked to reserve a region we don't cover */
242 if (index >= tbl->it_size)
243 return;
244
245 end = index + npages;
246 if (end > tbl->it_size) /* don't go off the table */
247 end = tbl->it_size;
248
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200249 spin_lock_irqsave(&tbl->it_lock, flags);
250
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200251 badbit = verify_bit_range(tbl->it_map, 0, index, end);
252 if (badbit != ~0UL) {
253 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200254 printk(KERN_ERR "Calgary: entry already allocated at "
255 "0x%lx tbl %p dma 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200256 badbit, tbl, start_addr, npages);
Jon Masone4650582006-06-26 13:58:14 +0200257 }
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200258
259 set_bit_string(tbl->it_map, index, npages);
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200260
261 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200262}
263
264static unsigned long iommu_range_alloc(struct iommu_table *tbl,
265 unsigned int npages)
266{
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200267 unsigned long flags;
Jon Masone4650582006-06-26 13:58:14 +0200268 unsigned long offset;
269
270 BUG_ON(npages == 0);
271
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200272 spin_lock_irqsave(&tbl->it_lock, flags);
273
Jon Masone4650582006-06-26 13:58:14 +0200274 offset = find_next_zero_string(tbl->it_map, tbl->it_hint,
275 tbl->it_size, npages);
276 if (offset == ~0UL) {
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200277 tbl->chip_ops->tce_cache_blast(tbl);
Jon Masone4650582006-06-26 13:58:14 +0200278 offset = find_next_zero_string(tbl->it_map, 0,
279 tbl->it_size, npages);
280 if (offset == ~0UL) {
281 printk(KERN_WARNING "Calgary: IOMMU full.\n");
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200282 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200283 if (panic_on_overflow)
284 panic("Calgary: fix the allocator.\n");
285 else
286 return bad_dma_address;
287 }
288 }
289
290 set_bit_string(tbl->it_map, offset, npages);
291 tbl->it_hint = offset + npages;
292 BUG_ON(tbl->it_hint > tbl->it_size);
293
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200294 spin_unlock_irqrestore(&tbl->it_lock, flags);
295
Jon Masone4650582006-06-26 13:58:14 +0200296 return offset;
297}
298
299static dma_addr_t iommu_alloc(struct iommu_table *tbl, void *vaddr,
300 unsigned int npages, int direction)
301{
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200302 unsigned long entry;
Jon Masone4650582006-06-26 13:58:14 +0200303 dma_addr_t ret = bad_dma_address;
304
Jon Masone4650582006-06-26 13:58:14 +0200305 entry = iommu_range_alloc(tbl, npages);
306
307 if (unlikely(entry == bad_dma_address))
308 goto error;
309
310 /* set the return dma address */
311 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
312
313 /* put the TCEs in the HW table */
314 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
315 direction);
316
Jon Masone4650582006-06-26 13:58:14 +0200317 return ret;
318
319error:
Jon Masone4650582006-06-26 13:58:14 +0200320 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
321 "iommu %p\n", npages, tbl);
322 return bad_dma_address;
323}
324
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200325static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
Jon Masone4650582006-06-26 13:58:14 +0200326 unsigned int npages)
327{
328 unsigned long entry;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200329 unsigned long badbit;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100330 unsigned long badend;
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200331 unsigned long flags;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100332
333 /* were we called with bad_dma_address? */
334 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
335 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
336 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
337 "address 0x%Lx\n", dma_addr);
338 WARN_ON(1);
339 return;
340 }
Jon Masone4650582006-06-26 13:58:14 +0200341
342 entry = dma_addr >> PAGE_SHIFT;
343
344 BUG_ON(entry + npages > tbl->it_size);
345
346 tce_free(tbl, entry, npages);
347
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200348 spin_lock_irqsave(&tbl->it_lock, flags);
349
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200350 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
351 if (badbit != ~0UL) {
352 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200353 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
354 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200355 badbit, tbl, dma_addr, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200356 }
357
358 __clear_bit_string(tbl->it_map, entry, npages);
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200359
360 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200361}
362
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200363static inline struct iommu_table *find_iommu_table(struct device *dev)
364{
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200365 struct pci_dev *pdev;
366 struct pci_bus *pbus;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200367 struct iommu_table *tbl;
368
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200369 pdev = to_pci_dev(dev);
370
Murillo Fernandes Bernardesf055a062007-08-10 22:31:00 +0200371 pbus = pdev->bus;
372
373 /* is the device behind a bridge? Look for the root bus */
374 while (pbus->parent)
375 pbus = pbus->parent;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200376
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300377 tbl = pci_iommu(pbus);
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200378
Murillo Fernandes Bernardesf055a062007-08-10 22:31:00 +0200379 BUG_ON(tbl && (tbl->it_busno != pbus->number));
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200380
381 return tbl;
382}
383
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200384static void calgary_unmap_sg(struct device *dev,
Jon Masone4650582006-06-26 13:58:14 +0200385 struct scatterlist *sglist, int nelems, int direction)
386{
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200387 struct iommu_table *tbl = find_iommu_table(dev);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200388 struct scatterlist *s;
389 int i;
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200390
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +0200391 if (!translation_enabled(tbl))
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200392 return;
393
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200394 for_each_sg(sglist, s, nelems, i) {
Jon Masone4650582006-06-26 13:58:14 +0200395 unsigned int npages;
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200396 dma_addr_t dma = s->dma_address;
397 unsigned int dmalen = s->dma_length;
Jon Masone4650582006-06-26 13:58:14 +0200398
399 if (dmalen == 0)
400 break;
401
402 npages = num_dma_pages(dma, dmalen);
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200403 iommu_free(tbl, dma, npages);
Jon Masone4650582006-06-26 13:58:14 +0200404 }
405}
406
Jon Masone4650582006-06-26 13:58:14 +0200407static int calgary_nontranslate_map_sg(struct device* dev,
408 struct scatterlist *sg, int nelems, int direction)
409{
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200410 struct scatterlist *s;
Jon Masone4650582006-06-26 13:58:14 +0200411 int i;
412
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200413 for_each_sg(sg, s, nelems, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200414 struct page *p = sg_page(s);
415
416 BUG_ON(!p);
417 s->dma_address = virt_to_bus(sg_virt(s));
Jon Masone4650582006-06-26 13:58:14 +0200418 s->dma_length = s->length;
419 }
420 return nelems;
421}
422
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200423static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
Jon Masone4650582006-06-26 13:58:14 +0200424 int nelems, int direction)
425{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200426 struct iommu_table *tbl = find_iommu_table(dev);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200427 struct scatterlist *s;
Jon Masone4650582006-06-26 13:58:14 +0200428 unsigned long vaddr;
429 unsigned int npages;
430 unsigned long entry;
431 int i;
432
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200433 if (!translation_enabled(tbl))
Jon Masone4650582006-06-26 13:58:14 +0200434 return calgary_nontranslate_map_sg(dev, sg, nelems, direction);
435
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200436 for_each_sg(sg, s, nelems, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200437 BUG_ON(!sg_page(s));
Jon Masone4650582006-06-26 13:58:14 +0200438
Jens Axboe58b053e2007-10-22 20:02:46 +0200439 vaddr = (unsigned long) sg_virt(s);
Jon Masone4650582006-06-26 13:58:14 +0200440 npages = num_dma_pages(vaddr, s->length);
441
442 entry = iommu_range_alloc(tbl, npages);
443 if (entry == bad_dma_address) {
444 /* makes sure unmap knows to stop */
445 s->dma_length = 0;
446 goto error;
447 }
448
449 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
450
451 /* insert into HW table */
452 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
453 direction);
454
455 s->dma_length = s->length;
456 }
457
Jon Masone4650582006-06-26 13:58:14 +0200458 return nelems;
459error:
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200460 calgary_unmap_sg(dev, sg, nelems, direction);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200461 for_each_sg(sg, s, nelems, i) {
462 sg->dma_address = bad_dma_address;
463 sg->dma_length = 0;
Jon Masone4650582006-06-26 13:58:14 +0200464 }
Jon Masone4650582006-06-26 13:58:14 +0200465 return 0;
466}
467
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200468static dma_addr_t calgary_map_single(struct device *dev, void *vaddr,
Jon Masone4650582006-06-26 13:58:14 +0200469 size_t size, int direction)
470{
471 dma_addr_t dma_handle = bad_dma_address;
472 unsigned long uaddr;
473 unsigned int npages;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200474 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200475
476 uaddr = (unsigned long)vaddr;
477 npages = num_dma_pages(uaddr, size);
478
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200479 if (translation_enabled(tbl))
Jon Masone4650582006-06-26 13:58:14 +0200480 dma_handle = iommu_alloc(tbl, vaddr, npages, direction);
481 else
482 dma_handle = virt_to_bus(vaddr);
483
484 return dma_handle;
485}
486
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200487static void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
Jon Masone4650582006-06-26 13:58:14 +0200488 size_t size, int direction)
489{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200490 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200491 unsigned int npages;
492
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200493 if (!translation_enabled(tbl))
Jon Masone4650582006-06-26 13:58:14 +0200494 return;
495
496 npages = num_dma_pages(dma_handle, size);
497 iommu_free(tbl, dma_handle, npages);
498}
499
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200500static void* calgary_alloc_coherent(struct device *dev, size_t size,
Jon Masone4650582006-06-26 13:58:14 +0200501 dma_addr_t *dma_handle, gfp_t flag)
502{
503 void *ret = NULL;
504 dma_addr_t mapping;
505 unsigned int npages, order;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200506 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200507
508 size = PAGE_ALIGN(size); /* size rounded up to full pages */
509 npages = size >> PAGE_SHIFT;
510 order = get_order(size);
511
512 /* alloc enough pages (and possibly more) */
513 ret = (void *)__get_free_pages(flag, order);
514 if (!ret)
515 goto error;
516 memset(ret, 0, size);
517
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200518 if (translation_enabled(tbl)) {
Jon Masone4650582006-06-26 13:58:14 +0200519 /* set up tces to cover the allocated range */
520 mapping = iommu_alloc(tbl, ret, npages, DMA_BIDIRECTIONAL);
521 if (mapping == bad_dma_address)
522 goto free;
523
524 *dma_handle = mapping;
525 } else /* non translated slot */
526 *dma_handle = virt_to_bus(ret);
527
528 return ret;
529
530free:
531 free_pages((unsigned long)ret, get_order(size));
532 ret = NULL;
533error:
534 return ret;
535}
536
Stephen Hemmingere6584502007-05-02 19:27:06 +0200537static const struct dma_mapping_ops calgary_dma_ops = {
Jon Masone4650582006-06-26 13:58:14 +0200538 .alloc_coherent = calgary_alloc_coherent,
539 .map_single = calgary_map_single,
540 .unmap_single = calgary_unmap_single,
541 .map_sg = calgary_map_sg,
542 .unmap_sg = calgary_unmap_sg,
543};
544
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100545static inline void __iomem * busno_to_bbar(unsigned char num)
546{
547 return bus_info[num].bbar;
548}
549
Jon Masone4650582006-06-26 13:58:14 +0200550static inline int busno_to_phbid(unsigned char num)
551{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200552 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200553}
554
555static inline unsigned long split_queue_offset(unsigned char num)
556{
557 size_t idx = busno_to_phbid(num);
558
559 return split_queue_offsets[idx];
560}
561
562static inline unsigned long tar_offset(unsigned char num)
563{
564 size_t idx = busno_to_phbid(num);
565
566 return tar_offsets[idx];
567}
568
569static inline unsigned long phb_offset(unsigned char num)
570{
571 size_t idx = busno_to_phbid(num);
572
573 return phb_offsets[idx];
574}
575
576static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
577{
578 unsigned long target = ((unsigned long)bar) | offset;
579 return (void __iomem*)target;
580}
581
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200582static inline int is_calioc2(unsigned short device)
583{
584 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
585}
586
587static inline int is_calgary(unsigned short device)
588{
589 return (device == PCI_DEVICE_ID_IBM_CALGARY);
590}
591
592static inline int is_cal_pci_dev(unsigned short device)
593{
594 return (is_calgary(device) || is_calioc2(device));
595}
596
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200597static void calgary_tce_cache_blast(struct iommu_table *tbl)
Jon Masone4650582006-06-26 13:58:14 +0200598{
599 u64 val;
600 u32 aer;
601 int i = 0;
602 void __iomem *bbar = tbl->bbar;
603 void __iomem *target;
604
605 /* disable arbitration on the bus */
606 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
607 aer = readl(target);
608 writel(0, target);
609
610 /* read plssr to ensure it got there */
611 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
612 val = readl(target);
613
614 /* poll split queues until all DMA activity is done */
615 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
616 do {
617 val = readq(target);
618 i++;
619 } while ((val & 0xff) != 0xff && i < 100);
620 if (i == 100)
621 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
622 "continuing anyway\n");
623
624 /* invalidate TCE cache */
625 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
626 writeq(tbl->tar_val, target);
627
628 /* enable arbitration */
629 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
630 writel(aer, target);
631 (void)readl(target); /* flush */
632}
633
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200634static void calioc2_tce_cache_blast(struct iommu_table *tbl)
635{
636 void __iomem *bbar = tbl->bbar;
637 void __iomem *target;
638 u64 val64;
639 u32 val;
640 int i = 0;
641 int count = 1;
642 unsigned char bus = tbl->it_busno;
643
644begin:
645 printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
646 "sequence - count %d\n", bus, count);
647
648 /* 1. using the Page Migration Control reg set SoftStop */
649 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
650 val = be32_to_cpu(readl(target));
651 printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
652 val |= PMR_SOFTSTOP;
653 printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
654 writel(cpu_to_be32(val), target);
655
656 /* 2. poll split queues until all DMA activity is done */
657 printk(KERN_DEBUG "2a. starting to poll split queues\n");
658 target = calgary_reg(bbar, split_queue_offset(bus));
659 do {
660 val64 = readq(target);
661 i++;
662 } while ((val64 & 0xff) != 0xff && i < 100);
663 if (i == 100)
664 printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
665 "continuing anyway\n");
666
667 /* 3. poll Page Migration DEBUG for SoftStopFault */
668 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
669 val = be32_to_cpu(readl(target));
670 printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
671
672 /* 4. if SoftStopFault - goto (1) */
673 if (val & PMR_SOFTSTOPFAULT) {
674 if (++count < 100)
675 goto begin;
676 else {
677 printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
678 "aborting TCE cache flush sequence!\n");
679 return; /* pray for the best */
680 }
681 }
682
683 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
684 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
685 printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
686 val = be32_to_cpu(readl(target));
687 printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
688 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
689 val = be32_to_cpu(readl(target));
690 printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
691
692 /* 6. invalidate TCE cache */
693 printk(KERN_DEBUG "6. invalidating TCE cache\n");
694 target = calgary_reg(bbar, tar_offset(bus));
695 writeq(tbl->tar_val, target);
696
697 /* 7. Re-read PMCR */
698 printk(KERN_DEBUG "7a. Re-reading PMCR\n");
699 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
700 val = be32_to_cpu(readl(target));
701 printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
702
703 /* 8. Remove HardStop */
704 printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
705 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
706 val = 0;
707 printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
708 writel(cpu_to_be32(val), target);
709 val = be32_to_cpu(readl(target));
710 printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
711}
712
Jon Masone4650582006-06-26 13:58:14 +0200713static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
714 u64 limit)
715{
716 unsigned int numpages;
717
718 limit = limit | 0xfffff;
719 limit++;
720
721 numpages = ((limit - start) >> PAGE_SHIFT);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300722 iommu_range_reserve(pci_iommu(dev->bus), start, numpages);
Jon Masone4650582006-06-26 13:58:14 +0200723}
724
725static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
726{
727 void __iomem *target;
728 u64 low, high, sizelow;
729 u64 start, limit;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300730 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200731 unsigned char busnum = dev->bus->number;
732 void __iomem *bbar = tbl->bbar;
733
734 /* peripheral MEM_1 region */
735 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
736 low = be32_to_cpu(readl(target));
737 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
738 high = be32_to_cpu(readl(target));
739 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
740 sizelow = be32_to_cpu(readl(target));
741
742 start = (high << 32) | low;
743 limit = sizelow;
744
745 calgary_reserve_mem_region(dev, start, limit);
746}
747
748static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
749{
750 void __iomem *target;
751 u32 val32;
752 u64 low, high, sizelow, sizehigh;
753 u64 start, limit;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300754 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200755 unsigned char busnum = dev->bus->number;
756 void __iomem *bbar = tbl->bbar;
757
758 /* is it enabled? */
759 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
760 val32 = be32_to_cpu(readl(target));
761 if (!(val32 & PHB_MEM2_ENABLE))
762 return;
763
764 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
765 low = be32_to_cpu(readl(target));
766 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
767 high = be32_to_cpu(readl(target));
768 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
769 sizelow = be32_to_cpu(readl(target));
770 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
771 sizehigh = be32_to_cpu(readl(target));
772
773 start = (high << 32) | low;
774 limit = (sizehigh << 32) | sizelow;
775
776 calgary_reserve_mem_region(dev, start, limit);
777}
778
779/*
780 * some regions of the IO address space do not get translated, so we
781 * must not give devices IO addresses in those regions. The regions
782 * are the 640KB-1MB region and the two PCI peripheral memory holes.
783 * Reserve all of them in the IOMMU bitmap to avoid giving them out
784 * later.
785 */
786static void __init calgary_reserve_regions(struct pci_dev *dev)
787{
788 unsigned int npages;
Jon Masone4650582006-06-26 13:58:14 +0200789 u64 start;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300790 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200791
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100792 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
793 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
Jon Masone4650582006-06-26 13:58:14 +0200794
795 /* avoid the BIOS/VGA first 640KB-1MB region */
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200796 /* for CalIOC2 - avoid the entire first MB */
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200797 if (is_calgary(dev->device)) {
798 start = (640 * 1024);
799 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
800 } else { /* calioc2 */
801 start = 0;
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200802 npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200803 }
Jon Masone4650582006-06-26 13:58:14 +0200804 iommu_range_reserve(tbl, start, npages);
805
806 /* reserve the two PCI peripheral memory regions in IO space */
807 calgary_reserve_peripheral_mem_1(dev);
808 calgary_reserve_peripheral_mem_2(dev);
809}
810
811static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
812{
813 u64 val64;
814 u64 table_phys;
815 void __iomem *target;
816 int ret;
817 struct iommu_table *tbl;
818
819 /* build TCE tables for each PHB */
820 ret = build_tce_table(dev, bbar);
821 if (ret)
822 return ret;
823
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300824 tbl = pci_iommu(dev->bus);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200825 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
826 tce_free(tbl, 0, tbl->it_size);
827
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200828 if (is_calgary(dev->device))
829 tbl->chip_ops = &calgary_chip_ops;
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200830 else if (is_calioc2(dev->device))
831 tbl->chip_ops = &calioc2_chip_ops;
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200832 else
833 BUG();
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200834
Jon Masone4650582006-06-26 13:58:14 +0200835 calgary_reserve_regions(dev);
836
837 /* set TARs for each PHB */
838 target = calgary_reg(bbar, tar_offset(dev->bus->number));
839 val64 = be64_to_cpu(readq(target));
840
841 /* zero out all TAR bits under sw control */
842 val64 &= ~TAR_SW_BITS;
Jon Masone4650582006-06-26 13:58:14 +0200843 table_phys = (u64)__pa(tbl->it_base);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200844
Jon Masone4650582006-06-26 13:58:14 +0200845 val64 |= table_phys;
846
847 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
848 val64 |= (u64) specified_table_size;
849
850 tbl->tar_val = cpu_to_be64(val64);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200851
Jon Masone4650582006-06-26 13:58:14 +0200852 writeq(tbl->tar_val, target);
853 readq(target); /* flush */
854
855 return 0;
856}
857
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200858static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200859{
860 u64 val64;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300861 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200862 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200863 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200864
865 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
866 val64 = be64_to_cpu(readq(target));
867 val64 &= ~TAR_SW_BITS;
868 writeq(cpu_to_be64(val64), target);
869 readq(target); /* flush */
870
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200871 bitmapsz = tbl->it_size / BITS_PER_BYTE;
872 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
873 tbl->it_map = NULL;
874
Jon Masone4650582006-06-26 13:58:14 +0200875 kfree(tbl);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300876
877 set_pci_iommu(dev->bus, NULL);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200878
879 /* Can't free bootmem allocated memory after system is up :-( */
880 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200881}
882
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200883static void calgary_dump_error_regs(struct iommu_table *tbl)
884{
885 void __iomem *bbar = tbl->bbar;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200886 void __iomem *target;
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200887 u32 csr, plssr;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200888
889 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200890 csr = be32_to_cpu(readl(target));
891
892 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
893 plssr = be32_to_cpu(readl(target));
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200894
895 /* If no error, the agent ID in the CSR is not valid */
896 printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200897 "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200898}
899
900static void calioc2_dump_error_regs(struct iommu_table *tbl)
901{
902 void __iomem *bbar = tbl->bbar;
903 u32 csr, csmr, plssr, mck, rcstat;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200904 void __iomem *target;
905 unsigned long phboff = phb_offset(tbl->it_busno);
906 unsigned long erroff;
907 u32 errregs[7];
908 int i;
909
910 /* dump CSR */
911 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
912 csr = be32_to_cpu(readl(target));
913 /* dump PLSSR */
914 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
915 plssr = be32_to_cpu(readl(target));
916 /* dump CSMR */
917 target = calgary_reg(bbar, phboff | 0x290);
918 csmr = be32_to_cpu(readl(target));
919 /* dump mck */
920 target = calgary_reg(bbar, phboff | 0x800);
921 mck = be32_to_cpu(readl(target));
922
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200923 printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
924 tbl->it_busno);
925
926 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
927 csr, plssr, csmr, mck);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200928
929 /* dump rest of error regs */
930 printk(KERN_EMERG "Calgary: ");
931 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200932 /* err regs are at 0x810 - 0x870 */
933 erroff = (0x810 + (i * 0x10));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200934 target = calgary_reg(bbar, phboff | erroff);
935 errregs[i] = be32_to_cpu(readl(target));
936 printk("0x%08x@0x%lx ", errregs[i], erroff);
937 }
938 printk("\n");
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200939
940 /* root complex status */
941 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
942 rcstat = be32_to_cpu(readl(target));
943 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
944 PHB_ROOT_COMPLEX_STATUS);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200945}
946
Jon Masone4650582006-06-26 13:58:14 +0200947static void calgary_watchdog(unsigned long data)
948{
949 struct pci_dev *dev = (struct pci_dev *)data;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300950 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200951 void __iomem *bbar = tbl->bbar;
952 u32 val32;
953 void __iomem *target;
954
955 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
956 val32 = be32_to_cpu(readl(target));
957
958 /* If no error, the agent ID in the CSR is not valid */
959 if (val32 & CSR_AGENT_MASK) {
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200960 tbl->chip_ops->dump_error_regs(tbl);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200961
962 /* reset error */
Jon Masone4650582006-06-26 13:58:14 +0200963 writel(0, target);
964
965 /* Disable bus that caused the error */
966 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200967 PHB_CONFIG_RW_OFFSET);
Jon Masone4650582006-06-26 13:58:14 +0200968 val32 = be32_to_cpu(readl(target));
969 val32 |= PHB_SLOT_DISABLE;
970 writel(cpu_to_be32(val32), target);
971 readl(target); /* flush */
972 } else {
973 /* Reset the timer */
974 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
975 }
976}
977
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200978static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
979 unsigned char busnum, unsigned long timeout)
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200980{
981 u64 val64;
982 void __iomem *target;
Muli Ben-Yehuda58db8542006-12-07 02:14:06 +0100983 unsigned int phb_shift = ~0; /* silence gcc */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200984 u64 mask;
985
986 switch (busno_to_phbid(busnum)) {
987 case 0: phb_shift = (63 - 19);
988 break;
989 case 1: phb_shift = (63 - 23);
990 break;
991 case 2: phb_shift = (63 - 27);
992 break;
993 case 3: phb_shift = (63 - 35);
994 break;
995 default:
996 BUG_ON(busno_to_phbid(busnum));
997 }
998
999 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
1000 val64 = be64_to_cpu(readq(target));
1001
1002 /* zero out this PHB's timer bits */
1003 mask = ~(0xFUL << phb_shift);
1004 val64 &= mask;
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +02001005 val64 |= (timeout << phb_shift);
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +02001006 writeq(cpu_to_be64(val64), target);
1007 readq(target); /* flush */
1008}
1009
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001010static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
1011{
1012 unsigned char busnum = dev->bus->number;
1013 void __iomem *bbar = tbl->bbar;
1014 void __iomem *target;
1015 u32 val;
1016
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +02001017 /*
1018 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
1019 */
1020 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
1021 val = cpu_to_be32(readl(target));
1022 val |= 0x00800000;
1023 writel(cpu_to_be32(val), target);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001024}
1025
1026static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001027{
1028 unsigned char busnum = dev->bus->number;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001029
1030 /*
1031 * Give split completion a longer timeout on bus 1 for aic94xx
1032 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
1033 */
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001034 if (is_calgary(dev->device) && (busnum == 1))
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001035 calgary_set_split_completion_timeout(tbl->bbar, busnum,
1036 CCR_2SEC_TIMEOUT);
1037}
1038
Jon Masone4650582006-06-26 13:58:14 +02001039static void __init calgary_enable_translation(struct pci_dev *dev)
1040{
1041 u32 val32;
1042 unsigned char busnum;
1043 void __iomem *target;
1044 void __iomem *bbar;
1045 struct iommu_table *tbl;
1046
1047 busnum = dev->bus->number;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001048 tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +02001049 bbar = tbl->bbar;
1050
1051 /* enable TCE in PHB Config Register */
1052 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1053 val32 = be32_to_cpu(readl(target));
1054 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
1055
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001056 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
1057 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
1058 "Calgary" : "CalIOC2", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001059 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
1060 "bus.\n");
1061
1062 writel(cpu_to_be32(val32), target);
1063 readl(target); /* flush */
1064
1065 init_timer(&tbl->watchdog_timer);
1066 tbl->watchdog_timer.function = &calgary_watchdog;
1067 tbl->watchdog_timer.data = (unsigned long)dev;
1068 mod_timer(&tbl->watchdog_timer, jiffies);
1069}
1070
1071static void __init calgary_disable_translation(struct pci_dev *dev)
1072{
1073 u32 val32;
1074 unsigned char busnum;
1075 void __iomem *target;
1076 void __iomem *bbar;
1077 struct iommu_table *tbl;
1078
1079 busnum = dev->bus->number;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001080 tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +02001081 bbar = tbl->bbar;
1082
1083 /* disable TCE in PHB Config Register */
1084 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1085 val32 = be32_to_cpu(readl(target));
1086 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1087
Jon Mason70d666d2006-10-05 18:47:21 +02001088 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001089 writel(cpu_to_be32(val32), target);
1090 readl(target); /* flush */
1091
1092 del_timer_sync(&tbl->watchdog_timer);
1093}
1094
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +02001095static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +02001096{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001097 pci_dev_get(dev);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001098 set_pci_iommu(dev->bus, NULL);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001099
1100 /* is the device behind a bridge? */
1101 if (dev->bus->parent)
1102 dev->bus->parent->self = dev;
1103 else
1104 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +02001105}
1106
1107static int __init calgary_init_one(struct pci_dev *dev)
1108{
Jon Masone4650582006-06-26 13:58:14 +02001109 void __iomem *bbar;
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001110 struct iommu_table *tbl;
Jon Masone4650582006-06-26 13:58:14 +02001111 int ret;
1112
Jon Masondedc9932006-10-05 18:47:21 +02001113 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1114
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001115 bbar = busno_to_bbar(dev->bus->number);
Jon Masone4650582006-06-26 13:58:14 +02001116 ret = calgary_setup_tar(dev, bbar);
1117 if (ret)
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001118 goto done;
Jon Masone4650582006-06-26 13:58:14 +02001119
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001120 pci_dev_get(dev);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001121
1122 if (dev->bus->parent) {
1123 if (dev->bus->parent->self)
1124 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1125 "bus->parent->self!\n", dev);
1126 dev->bus->parent->self = dev;
1127 } else
1128 dev->bus->self = dev;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001129
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001130 tbl = pci_iommu(dev->bus);
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001131 tbl->chip_ops->handle_quirks(tbl, dev);
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001132
Jon Masone4650582006-06-26 13:58:14 +02001133 calgary_enable_translation(dev);
1134
1135 return 0;
1136
Jon Masone4650582006-06-26 13:58:14 +02001137done:
1138 return ret;
1139}
1140
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001141static int __init calgary_locate_bbars(void)
Jon Masone4650582006-06-26 13:58:14 +02001142{
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001143 int ret;
1144 int rioidx, phb, bus;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001145 void __iomem *bbar;
1146 void __iomem *target;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001147 unsigned long offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001148 u8 start_bus, end_bus;
1149 u32 val;
1150
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001151 ret = -ENODATA;
1152 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1153 struct rio_detail *rio = rio_devs[rioidx];
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001154
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001155 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001156 continue;
1157
1158 /* map entire 1MB of Calgary config space */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001159 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1160 if (!bbar)
1161 goto error;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001162
1163 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001164 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1165 target = calgary_reg(bbar, offset);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001166
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001167 val = be32_to_cpu(readl(target));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001168
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001169 start_bus = (u8)((val & 0x00FF0000) >> 16);
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001170 end_bus = (u8)((val & 0x0000FF00) >> 8);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001171
1172 if (end_bus) {
1173 for (bus = start_bus; bus <= end_bus; bus++) {
1174 bus_info[bus].bbar = bbar;
1175 bus_info[bus].phbid = phb;
1176 }
1177 } else {
1178 bus_info[start_bus].bbar = bbar;
1179 bus_info[start_bus].phbid = phb;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001180 }
1181 }
1182 }
1183
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001184 return 0;
1185
1186error:
1187 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1188 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1189 if (bus_info[bus].bbar)
1190 iounmap(bus_info[bus].bbar);
1191
1192 return ret;
1193}
1194
1195static int __init calgary_init(void)
1196{
1197 int ret;
1198 struct pci_dev *dev = NULL;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001199 struct calgary_bus_info *info;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001200
1201 ret = calgary_locate_bbars();
1202 if (ret)
1203 return ret;
Jon Masone4650582006-06-26 13:58:14 +02001204
Jon Masondedc9932006-10-05 18:47:21 +02001205 do {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001206 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Jon Masone4650582006-06-26 13:58:14 +02001207 if (!dev)
1208 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001209 if (!is_cal_pci_dev(dev->device))
1210 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001211
1212 info = &bus_info[dev->bus->number];
1213 if (info->translation_disabled) {
Jon Masone4650582006-06-26 13:58:14 +02001214 calgary_init_one_nontraslated(dev);
1215 continue;
1216 }
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001217
1218 if (!info->tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001219 continue;
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001220
Jon Masone4650582006-06-26 13:58:14 +02001221 ret = calgary_init_one(dev);
1222 if (ret)
1223 goto error;
Jon Masondedc9932006-10-05 18:47:21 +02001224 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001225
1226 return ret;
1227
1228error:
Jon Masondedc9932006-10-05 18:47:21 +02001229 do {
Alan Cox7cd8b682006-12-07 02:14:03 +01001230 dev = pci_get_device_reverse(PCI_VENDOR_ID_IBM,
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001231 PCI_ANY_ID, dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +02001232 if (!dev)
1233 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001234 if (!is_cal_pci_dev(dev->device))
1235 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001236
1237 info = &bus_info[dev->bus->number];
1238 if (info->translation_disabled) {
Jon Masone4650582006-06-26 13:58:14 +02001239 pci_dev_put(dev);
1240 continue;
1241 }
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001242 if (!info->tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001243 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001244
Jon Masone4650582006-06-26 13:58:14 +02001245 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +02001246 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001247 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Jon Masondedc9932006-10-05 18:47:21 +02001248 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001249
1250 return ret;
1251}
1252
1253static inline int __init determine_tce_table_size(u64 ram)
1254{
1255 int ret;
1256
1257 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1258 return specified_table_size;
1259
1260 /*
1261 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1262 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1263 * larger table size has twice as many entries, so shift the
1264 * max ram address by 13 to divide by 8K and then look at the
1265 * order of the result to choose between 0-7.
1266 */
1267 ret = get_order(ram >> 13);
1268 if (ret > TCE_TABLE_SIZE_8M)
1269 ret = TCE_TABLE_SIZE_8M;
1270
1271 return ret;
1272}
1273
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001274static int __init build_detail_arrays(void)
1275{
1276 unsigned long ptr;
1277 int i, scal_detail_size, rio_detail_size;
1278
1279 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1280 printk(KERN_WARNING
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001281 "Calgary: MAX_NUMNODES too low! Defined as %d, "
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001282 "but system has %d nodes.\n",
1283 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1284 return -ENODEV;
1285 }
1286
1287 switch (rio_table_hdr->version){
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001288 case 2:
1289 scal_detail_size = 11;
1290 rio_detail_size = 13;
1291 break;
1292 case 3:
1293 scal_detail_size = 12;
1294 rio_detail_size = 15;
1295 break;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001296 default:
1297 printk(KERN_WARNING
1298 "Calgary: Invalid Rio Grande Table Version: %d\n",
1299 rio_table_hdr->version);
1300 return -EPROTO;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001301 }
1302
1303 ptr = ((unsigned long)rio_table_hdr) + 3;
1304 for (i = 0; i < rio_table_hdr->num_scal_dev;
1305 i++, ptr += scal_detail_size)
1306 scal_devs[i] = (struct scal_detail *)ptr;
1307
1308 for (i = 0; i < rio_table_hdr->num_rio_dev;
1309 i++, ptr += rio_detail_size)
1310 rio_devs[i] = (struct rio_detail *)ptr;
1311
1312 return 0;
1313}
1314
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001315static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1316{
1317 int dev;
1318 u32 val;
1319
1320 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1321 /*
1322 * FIXME: properly scan for devices accross the
1323 * PCI-to-PCI bridge on every CalIOC2 port.
1324 */
1325 return 1;
1326 }
1327
1328 for (dev = 1; dev < 8; dev++) {
1329 val = read_pci_config(bus, dev, 0, 0);
1330 if (val != 0xffffffff)
1331 break;
1332 }
1333 return (val != 0xffffffff);
1334}
1335
Jon Masone4650582006-06-26 13:58:14 +02001336void __init detect_calgary(void)
1337{
Jon Masond2105b12006-07-29 21:42:43 +02001338 int bus;
Jon Masone4650582006-06-26 13:58:14 +02001339 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +02001340 int calgary_found = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001341 unsigned long ptr;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001342 unsigned int offset, prev_offset;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001343 int ret;
Jon Masone4650582006-06-26 13:58:14 +02001344
1345 /*
1346 * if the user specified iommu=off or iommu=soft or we found
1347 * another HW IOMMU already, bail out.
1348 */
1349 if (swiotlb || no_iommu || iommu_detected)
1350 return;
1351
Muli Ben-Yehudabff65472006-12-07 02:14:07 +01001352 if (!use_calgary)
1353 return;
1354
Andi Kleen0637a702006-09-26 10:52:41 +02001355 if (!early_pci_allowed())
1356 return;
1357
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001358 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1359
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001360 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1361
1362 rio_table_hdr = NULL;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001363 prev_offset = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001364 offset = 0x180;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001365 /*
1366 * The next offset is stored in the 1st word.
1367 * Only parse up until the offset increases:
1368 */
1369 while (offset > prev_offset) {
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001370 /* The block id is stored in the 2nd word */
1371 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1372 /* set the pointer past the offset & block id */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001373 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001374 break;
1375 }
Ingo Molnar136f1e72006-12-20 11:53:32 +01001376 prev_offset = offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001377 offset = *((unsigned short *)(ptr + offset));
1378 }
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001379 if (!rio_table_hdr) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001380 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1381 "in EBDA - bailing!\n");
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001382 return;
1383 }
1384
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001385 ret = build_detail_arrays();
1386 if (ret) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001387 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001388 return;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001389 }
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001390
Jon Masone4650582006-06-26 13:58:14 +02001391 specified_table_size = determine_tce_table_size(end_pfn * PAGE_SIZE);
1392
Jon Masond2105b12006-07-29 21:42:43 +02001393 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001394 struct calgary_bus_info *info = &bus_info[bus];
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001395 unsigned short pci_device;
1396 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +02001397
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001398 val = read_pci_config(bus, 0, 0, 0);
1399 pci_device = (val & 0xFFFF0000) >> 16;
1400
1401 if (!is_cal_pci_dev(pci_device))
Jon Masone4650582006-06-26 13:58:14 +02001402 continue;
Jon Masond2105b12006-07-29 21:42:43 +02001403
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001404 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +02001405 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001406
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001407 if (calgary_bus_has_devices(bus, pci_device) ||
1408 translate_empty_slots) {
1409 tbl = alloc_tce_table();
1410 if (!tbl)
1411 goto cleanup;
1412 info->tce_space = tbl;
1413 calgary_found = 1;
Jon Masond2105b12006-07-29 21:42:43 +02001414 }
Jon Masone4650582006-06-26 13:58:14 +02001415 }
1416
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001417 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1418 calgary_found ? "found" : "not found");
1419
Jon Masond2105b12006-07-29 21:42:43 +02001420 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +02001421 iommu_detected = 1;
1422 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +02001423 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1424 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1425 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1426 debugging ? "enabled" : "disabled");
Jon Masone4650582006-06-26 13:58:14 +02001427 }
1428 return;
1429
1430cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001431 for (--bus; bus >= 0; --bus) {
1432 struct calgary_bus_info *info = &bus_info[bus];
1433
1434 if (info->tce_space)
1435 free_tce_table(info->tce_space);
1436 }
Jon Masone4650582006-06-26 13:58:14 +02001437}
1438
1439int __init calgary_iommu_init(void)
1440{
1441 int ret;
1442
1443 if (no_iommu || swiotlb)
1444 return -ENODEV;
1445
1446 if (!calgary_detected)
1447 return -ENODEV;
1448
1449 /* ok, we're trying to use Calgary - let's roll */
1450 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1451
1452 ret = calgary_init();
1453 if (ret) {
1454 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1455 "falling back to no_iommu\n", ret);
1456 if (end_pfn > MAX_DMA32_PFN)
1457 printk(KERN_ERR "WARNING more than 4GB of memory, "
1458 "32bit PCI may malfunction.\n");
1459 return ret;
1460 }
1461
1462 force_iommu = 1;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +01001463 bad_dma_address = 0x0;
Jon Masone4650582006-06-26 13:58:14 +02001464 dma_ops = &calgary_dma_ops;
1465
1466 return 0;
1467}
1468
1469static int __init calgary_parse_options(char *p)
1470{
1471 unsigned int bridge;
1472 size_t len;
1473 char* endp;
1474
1475 while (*p) {
1476 if (!strncmp(p, "64k", 3))
1477 specified_table_size = TCE_TABLE_SIZE_64K;
1478 else if (!strncmp(p, "128k", 4))
1479 specified_table_size = TCE_TABLE_SIZE_128K;
1480 else if (!strncmp(p, "256k", 4))
1481 specified_table_size = TCE_TABLE_SIZE_256K;
1482 else if (!strncmp(p, "512k", 4))
1483 specified_table_size = TCE_TABLE_SIZE_512K;
1484 else if (!strncmp(p, "1M", 2))
1485 specified_table_size = TCE_TABLE_SIZE_1M;
1486 else if (!strncmp(p, "2M", 2))
1487 specified_table_size = TCE_TABLE_SIZE_2M;
1488 else if (!strncmp(p, "4M", 2))
1489 specified_table_size = TCE_TABLE_SIZE_4M;
1490 else if (!strncmp(p, "8M", 2))
1491 specified_table_size = TCE_TABLE_SIZE_8M;
1492
1493 len = strlen("translate_empty_slots");
1494 if (!strncmp(p, "translate_empty_slots", len))
1495 translate_empty_slots = 1;
1496
1497 len = strlen("disable");
1498 if (!strncmp(p, "disable", len)) {
1499 p += len;
1500 if (*p == '=')
1501 ++p;
1502 if (*p == '\0')
1503 break;
1504 bridge = simple_strtol(p, &endp, 0);
1505 if (p == endp)
1506 break;
1507
Jon Masond2105b12006-07-29 21:42:43 +02001508 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001509 printk(KERN_INFO "Calgary: disabling "
Jon Mason70d666d2006-10-05 18:47:21 +02001510 "translation for PHB %#x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001511 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001512 }
1513 }
1514
1515 p = strpbrk(p, ",");
1516 if (!p)
1517 break;
1518
1519 p++; /* skip ',' */
1520 }
1521 return 1;
1522}
1523__setup("calgary=", calgary_parse_options);
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001524
1525static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
1526{
1527 struct iommu_table *tbl;
1528 unsigned int npages;
1529 int i;
1530
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001531 tbl = pci_iommu(dev->bus);
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001532
1533 for (i = 0; i < 4; i++) {
1534 struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
1535
1536 /* Don't give out TCEs that map MEM resources */
1537 if (!(r->flags & IORESOURCE_MEM))
1538 continue;
1539
1540 /* 0-based? we reserve the whole 1st MB anyway */
1541 if (!r->start)
1542 continue;
1543
1544 /* cover the whole region */
1545 npages = (r->end - r->start) >> PAGE_SHIFT;
1546 npages++;
1547
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001548 iommu_range_reserve(tbl, r->start, npages);
1549 }
1550}
1551
1552static int __init calgary_fixup_tce_spaces(void)
1553{
1554 struct pci_dev *dev = NULL;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001555 struct calgary_bus_info *info;
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001556
1557 if (no_iommu || swiotlb || !calgary_detected)
1558 return -ENODEV;
1559
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001560 printk(KERN_DEBUG "Calgary: fixing up tce spaces\n");
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001561
1562 do {
1563 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1564 if (!dev)
1565 break;
1566 if (!is_cal_pci_dev(dev->device))
1567 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001568
1569 info = &bus_info[dev->bus->number];
1570 if (info->translation_disabled)
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001571 continue;
1572
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001573 if (!info->tce_space)
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001574 continue;
1575
1576 calgary_fixup_one_tce_space(dev);
1577
1578 } while (1);
1579
1580 return 0;
1581}
1582
1583/*
1584 * We need to be call after pcibios_assign_resources (fs_initcall level)
1585 * and before device_initcall.
1586 */
1587rootfs_initcall(calgary_fixup_tce_spaces);