blob: afb020fdb19cbfa30b21ee9fe6838bbaa8698ddf [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>
Chandru95b68de2008-07-25 01:47:55 -070032#include <linux/crash_dump.h>
Jon Masone4650582006-06-26 13:58:14 +020033#include <linux/dma-mapping.h>
Jon Masone4650582006-06-26 13:58:14 +020034#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>
FUJITA Tomonori1b39b072008-02-04 22:28:10 -080039#include <linux/iommu-helper.h>
Alexis Bruemmer1956a962008-07-25 19:44:51 -070040
FUJITA Tomonori46a7fa22008-07-11 10:23:42 +090041#include <asm/iommu.h>
Jon Masone4650582006-06-26 13:58:14 +020042#include <asm/calgary.h>
43#include <asm/tce.h>
44#include <asm/pci-direct.h>
45#include <asm/system.h>
46#include <asm/dma.h>
Laurent Vivierb34e90b2006-12-07 02:14:06 +010047#include <asm/rio.h>
Akinobu Mitaae5830a2008-04-19 23:55:19 +090048#include <asm/bios_ebda.h>
Jon Masone4650582006-06-26 13:58:14 +020049
Muli Ben-Yehudabff65472006-12-07 02:14:07 +010050#ifdef CONFIG_CALGARY_IOMMU_ENABLED_BY_DEFAULT
51int use_calgary __read_mostly = 1;
52#else
53int use_calgary __read_mostly = 0;
54#endif /* CONFIG_CALGARY_DEFAULT_ENABLED */
55
Jon Masone4650582006-06-26 13:58:14 +020056#define PCI_DEVICE_ID_IBM_CALGARY 0x02a1
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +020057#define PCI_DEVICE_ID_IBM_CALIOC2 0x0308
Jon Masone4650582006-06-26 13:58:14 +020058
Jon Masone4650582006-06-26 13:58:14 +020059/* register offsets inside the host bridge space */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020060#define CALGARY_CONFIG_REG 0x0108
61#define PHB_CSR_OFFSET 0x0110 /* Channel Status */
Jon Masone4650582006-06-26 13:58:14 +020062#define PHB_PLSSR_OFFSET 0x0120
63#define PHB_CONFIG_RW_OFFSET 0x0160
64#define PHB_IOBASE_BAR_LOW 0x0170
65#define PHB_IOBASE_BAR_HIGH 0x0180
66#define PHB_MEM_1_LOW 0x0190
67#define PHB_MEM_1_HIGH 0x01A0
68#define PHB_IO_ADDR_SIZE 0x01B0
69#define PHB_MEM_1_SIZE 0x01C0
70#define PHB_MEM_ST_OFFSET 0x01D0
71#define PHB_AER_OFFSET 0x0200
72#define PHB_CONFIG_0_HIGH 0x0220
73#define PHB_CONFIG_0_LOW 0x0230
74#define PHB_CONFIG_0_END 0x0240
75#define PHB_MEM_2_LOW 0x02B0
76#define PHB_MEM_2_HIGH 0x02C0
77#define PHB_MEM_2_SIZE_HIGH 0x02D0
78#define PHB_MEM_2_SIZE_LOW 0x02E0
79#define PHB_DOSHOLE_OFFSET 0x08E0
80
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020081/* CalIOC2 specific */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020082#define PHB_SAVIOR_L2 0x0DB0
83#define PHB_PAGE_MIG_CTRL 0x0DA8
84#define PHB_PAGE_MIG_DEBUG 0x0DA0
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +020085#define PHB_ROOT_COMPLEX_STATUS 0x0CB0
Muli Ben-Yehudac3860102007-07-21 17:10:53 +020086
Jon Masone4650582006-06-26 13:58:14 +020087/* PHB_CONFIG_RW */
88#define PHB_TCE_ENABLE 0x20000000
89#define PHB_SLOT_DISABLE 0x1C000000
90#define PHB_DAC_DISABLE 0x01000000
91#define PHB_MEM2_ENABLE 0x00400000
92#define PHB_MCSR_ENABLE 0x00100000
93/* TAR (Table Address Register) */
94#define TAR_SW_BITS 0x0000ffffffff800fUL
95#define TAR_VALID 0x0000000000000008UL
96/* CSR (Channel/DMA Status Register) */
97#define CSR_AGENT_MASK 0xffe0ffff
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +020098/* CCR (Calgary Configuration Register) */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +020099#define CCR_2SEC_TIMEOUT 0x000000000000000EUL
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200100/* PMCR/PMDR (Page Migration Control/Debug Registers */
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200101#define PMR_SOFTSTOP 0x80000000
102#define PMR_SOFTSTOPFAULT 0x40000000
103#define PMR_HARDSTOP 0x20000000
Jon Masone4650582006-06-26 13:58:14 +0200104
105#define MAX_NUM_OF_PHBS 8 /* how many PHBs in total? */
Jon Masond2105b12006-07-29 21:42:43 +0200106#define MAX_NUM_CHASSIS 8 /* max number of chassis */
Muli Ben-Yehuda4ea8a5d2006-09-26 10:52:33 +0200107/* MAX_PHB_BUS_NUM is the maximal possible dev->bus->number */
108#define MAX_PHB_BUS_NUM (MAX_NUM_OF_PHBS * MAX_NUM_CHASSIS * 2)
Jon Masone4650582006-06-26 13:58:14 +0200109#define PHBS_PER_CALGARY 4
110
111/* register offsets in Calgary's internal register space */
112static const unsigned long tar_offsets[] = {
113 0x0580 /* TAR0 */,
114 0x0588 /* TAR1 */,
115 0x0590 /* TAR2 */,
116 0x0598 /* TAR3 */
117};
118
119static const unsigned long split_queue_offsets[] = {
120 0x4870 /* SPLIT QUEUE 0 */,
121 0x5870 /* SPLIT QUEUE 1 */,
122 0x6870 /* SPLIT QUEUE 2 */,
123 0x7870 /* SPLIT QUEUE 3 */
124};
125
126static const unsigned long phb_offsets[] = {
127 0x8000 /* PHB0 */,
128 0x9000 /* PHB1 */,
129 0xA000 /* PHB2 */,
130 0xB000 /* PHB3 */
131};
132
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100133/* PHB debug registers */
134
135static const unsigned long phb_debug_offsets[] = {
136 0x4000 /* PHB 0 DEBUG */,
137 0x5000 /* PHB 1 DEBUG */,
138 0x6000 /* PHB 2 DEBUG */,
139 0x7000 /* PHB 3 DEBUG */
140};
141
142/*
143 * STUFF register for each debug PHB,
144 * byte 1 = start bus number, byte 2 = end bus number
145 */
146
147#define PHB_DEBUG_STUFF_OFFSET 0x0020
148
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100149#define EMERGENCY_PAGES 32 /* = 128KB */
150
Jon Masone4650582006-06-26 13:58:14 +0200151unsigned int specified_table_size = TCE_TABLE_SIZE_UNSPECIFIED;
152static int translate_empty_slots __read_mostly = 0;
153static int calgary_detected __read_mostly = 0;
154
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100155static struct rio_table_hdr *rio_table_hdr __initdata;
156static struct scal_detail *scal_devs[MAX_NUMNODES] __initdata;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +0100157static struct rio_detail *rio_devs[MAX_NUMNODES * 4] __initdata;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100158
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200159struct calgary_bus_info {
160 void *tce_space;
Muli Ben-Yehuda0577f142006-09-26 10:52:31 +0200161 unsigned char translation_disabled;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200162 signed char phbid;
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100163 void __iomem *bbar;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200164};
165
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200166static void calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
167static void calgary_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200168static void calgary_dump_error_regs(struct iommu_table *tbl);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200169static void calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev);
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200170static void calioc2_tce_cache_blast(struct iommu_table *tbl);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200171static void calioc2_dump_error_regs(struct iommu_table *tbl);
Chandru95b68de2008-07-25 01:47:55 -0700172static void calgary_init_bitmap_from_tce_table(struct iommu_table *tbl);
173static void get_tce_space_from_tar(void);
Jon Masone4650582006-06-26 13:58:14 +0200174
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200175static struct cal_chipset_ops calgary_chip_ops = {
176 .handle_quirks = calgary_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200177 .tce_cache_blast = calgary_tce_cache_blast,
178 .dump_error_regs = calgary_dump_error_regs
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200179};
180
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200181static struct cal_chipset_ops calioc2_chip_ops = {
182 .handle_quirks = calioc2_handle_quirks,
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200183 .tce_cache_blast = calioc2_tce_cache_blast,
184 .dump_error_regs = calioc2_dump_error_regs
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200185};
186
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200187static struct calgary_bus_info bus_info[MAX_PHB_BUS_NUM] = { { NULL, 0, 0 }, };
Jon Masone4650582006-06-26 13:58:14 +0200188
189/* enable this to stress test the chip's TCE cache */
190#ifdef CONFIG_IOMMU_DEBUG
Adrian Bunked652602008-01-30 13:30:31 +0100191static int debugging = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200192
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200193static inline unsigned long verify_bit_range(unsigned long* bitmap,
194 int expected, unsigned long start, unsigned long end)
195{
196 unsigned long idx = start;
197
198 BUG_ON(start >= end);
199
200 while (idx < end) {
201 if (!!test_bit(idx, bitmap) != expected)
202 return idx;
203 ++idx;
204 }
205
206 /* all bits have the expected value */
207 return ~0UL;
208}
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200209#else /* debugging is disabled */
Adrian Bunked652602008-01-30 13:30:31 +0100210static int debugging;
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200211
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200212static inline unsigned long verify_bit_range(unsigned long* bitmap,
213 int expected, unsigned long start, unsigned long end)
214{
215 return ~0UL;
216}
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200217
Muli Ben-Yehudade684652006-09-26 10:52:33 +0200218#endif /* CONFIG_IOMMU_DEBUG */
Jon Masone4650582006-06-26 13:58:14 +0200219
220static inline unsigned int num_dma_pages(unsigned long dma, unsigned int dmalen)
221{
222 unsigned int npages;
223
224 npages = PAGE_ALIGN(dma + dmalen) - (dma & PAGE_MASK);
225 npages >>= PAGE_SHIFT;
226
227 return npages;
228}
229
Muli Ben-Yehudad588ba82007-10-17 18:04:35 +0200230static inline int translation_enabled(struct iommu_table *tbl)
231{
232 /* only PHBs with translation enabled have an IOMMU table */
233 return (tbl != NULL);
234}
235
Jon Masone4650582006-06-26 13:58:14 +0200236static void iommu_range_reserve(struct iommu_table *tbl,
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200237 unsigned long start_addr, unsigned int npages)
Jon Masone4650582006-06-26 13:58:14 +0200238{
239 unsigned long index;
240 unsigned long end;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200241 unsigned long badbit;
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200242 unsigned long flags;
Jon Masone4650582006-06-26 13:58:14 +0200243
244 index = start_addr >> PAGE_SHIFT;
245
246 /* bail out if we're asked to reserve a region we don't cover */
247 if (index >= tbl->it_size)
248 return;
249
250 end = index + npages;
251 if (end > tbl->it_size) /* don't go off the table */
252 end = tbl->it_size;
253
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200254 spin_lock_irqsave(&tbl->it_lock, flags);
255
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200256 badbit = verify_bit_range(tbl->it_map, 0, index, end);
257 if (badbit != ~0UL) {
258 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200259 printk(KERN_ERR "Calgary: entry already allocated at "
260 "0x%lx tbl %p dma 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200261 badbit, tbl, start_addr, npages);
Jon Masone4650582006-06-26 13:58:14 +0200262 }
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200263
264 set_bit_string(tbl->it_map, index, npages);
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200265
266 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200267}
268
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800269static unsigned long iommu_range_alloc(struct device *dev,
270 struct iommu_table *tbl,
271 unsigned int npages)
Jon Masone4650582006-06-26 13:58:14 +0200272{
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200273 unsigned long flags;
Jon Masone4650582006-06-26 13:58:14 +0200274 unsigned long offset;
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800275 unsigned long boundary_size;
276
277 boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
278 PAGE_SIZE) >> PAGE_SHIFT;
Jon Masone4650582006-06-26 13:58:14 +0200279
280 BUG_ON(npages == 0);
281
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200282 spin_lock_irqsave(&tbl->it_lock, flags);
283
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800284 offset = iommu_area_alloc(tbl->it_map, tbl->it_size, tbl->it_hint,
285 npages, 0, boundary_size, 0);
Jon Masone4650582006-06-26 13:58:14 +0200286 if (offset == ~0UL) {
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200287 tbl->chip_ops->tce_cache_blast(tbl);
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800288
289 offset = iommu_area_alloc(tbl->it_map, tbl->it_size, 0,
290 npages, 0, boundary_size, 0);
Jon Masone4650582006-06-26 13:58:14 +0200291 if (offset == ~0UL) {
292 printk(KERN_WARNING "Calgary: IOMMU full.\n");
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200293 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200294 if (panic_on_overflow)
295 panic("Calgary: fix the allocator.\n");
296 else
297 return bad_dma_address;
298 }
299 }
300
Jon Masone4650582006-06-26 13:58:14 +0200301 tbl->it_hint = offset + npages;
302 BUG_ON(tbl->it_hint > tbl->it_size);
303
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200304 spin_unlock_irqrestore(&tbl->it_lock, flags);
305
Jon Masone4650582006-06-26 13:58:14 +0200306 return offset;
307}
308
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800309static dma_addr_t iommu_alloc(struct device *dev, struct iommu_table *tbl,
310 void *vaddr, unsigned int npages, int direction)
Jon Masone4650582006-06-26 13:58:14 +0200311{
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200312 unsigned long entry;
Jon Masone4650582006-06-26 13:58:14 +0200313 dma_addr_t ret = bad_dma_address;
314
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800315 entry = iommu_range_alloc(dev, tbl, npages);
Jon Masone4650582006-06-26 13:58:14 +0200316
317 if (unlikely(entry == bad_dma_address))
318 goto error;
319
320 /* set the return dma address */
321 ret = (entry << PAGE_SHIFT) | ((unsigned long)vaddr & ~PAGE_MASK);
322
323 /* put the TCEs in the HW table */
324 tce_build(tbl, entry, npages, (unsigned long)vaddr & PAGE_MASK,
325 direction);
326
Jon Masone4650582006-06-26 13:58:14 +0200327 return ret;
328
329error:
Jon Masone4650582006-06-26 13:58:14 +0200330 printk(KERN_WARNING "Calgary: failed to allocate %u pages in "
331 "iommu %p\n", npages, tbl);
332 return bad_dma_address;
333}
334
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200335static void iommu_free(struct iommu_table *tbl, dma_addr_t dma_addr,
Jon Masone4650582006-06-26 13:58:14 +0200336 unsigned int npages)
337{
338 unsigned long entry;
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200339 unsigned long badbit;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100340 unsigned long badend;
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200341 unsigned long flags;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100342
343 /* were we called with bad_dma_address? */
344 badend = bad_dma_address + (EMERGENCY_PAGES * PAGE_SIZE);
345 if (unlikely((dma_addr >= bad_dma_address) && (dma_addr < badend))) {
346 printk(KERN_ERR "Calgary: driver tried unmapping bad DMA "
347 "address 0x%Lx\n", dma_addr);
348 WARN_ON(1);
349 return;
350 }
Jon Masone4650582006-06-26 13:58:14 +0200351
352 entry = dma_addr >> PAGE_SHIFT;
353
354 BUG_ON(entry + npages > tbl->it_size);
355
356 tce_free(tbl, entry, npages);
357
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200358 spin_lock_irqsave(&tbl->it_lock, flags);
359
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200360 badbit = verify_bit_range(tbl->it_map, 1, entry, entry + npages);
361 if (badbit != ~0UL) {
362 if (printk_ratelimit())
Jon Masone4650582006-06-26 13:58:14 +0200363 printk(KERN_ERR "Calgary: bit is off at 0x%lx "
364 "tbl %p dma 0x%Lx entry 0x%lx npages %u\n",
Muli Ben-Yehuda796e4392006-09-26 10:52:33 +0200365 badbit, tbl, dma_addr, entry, npages);
Jon Masone4650582006-06-26 13:58:14 +0200366 }
367
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800368 iommu_area_free(tbl->it_map, entry, npages);
Muli Ben-Yehuda820a1492007-07-21 17:11:04 +0200369
370 spin_unlock_irqrestore(&tbl->it_lock, flags);
Jon Masone4650582006-06-26 13:58:14 +0200371}
372
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200373static inline struct iommu_table *find_iommu_table(struct device *dev)
374{
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200375 struct pci_dev *pdev;
376 struct pci_bus *pbus;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200377 struct iommu_table *tbl;
378
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200379 pdev = to_pci_dev(dev);
380
Murillo Fernandes Bernardesf055a062007-08-10 22:31:00 +0200381 pbus = pdev->bus;
382
383 /* is the device behind a bridge? Look for the root bus */
384 while (pbus->parent)
385 pbus = pbus->parent;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200386
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300387 tbl = pci_iommu(pbus);
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200388
Murillo Fernandes Bernardesf055a062007-08-10 22:31:00 +0200389 BUG_ON(tbl && (tbl->it_busno != pbus->number));
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200390
391 return tbl;
392}
393
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200394static void calgary_unmap_sg(struct device *dev,
Jon Masone4650582006-06-26 13:58:14 +0200395 struct scatterlist *sglist, int nelems, int direction)
396{
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200397 struct iommu_table *tbl = find_iommu_table(dev);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200398 struct scatterlist *s;
399 int i;
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200400
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +0200401 if (!translation_enabled(tbl))
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200402 return;
403
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200404 for_each_sg(sglist, s, nelems, i) {
Jon Masone4650582006-06-26 13:58:14 +0200405 unsigned int npages;
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200406 dma_addr_t dma = s->dma_address;
407 unsigned int dmalen = s->dma_length;
Jon Masone4650582006-06-26 13:58:14 +0200408
409 if (dmalen == 0)
410 break;
411
412 npages = num_dma_pages(dma, dmalen);
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200413 iommu_free(tbl, dma, npages);
Jon Masone4650582006-06-26 13:58:14 +0200414 }
415}
416
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200417static int calgary_map_sg(struct device *dev, struct scatterlist *sg,
Jon Masone4650582006-06-26 13:58:14 +0200418 int nelems, int direction)
419{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200420 struct iommu_table *tbl = find_iommu_table(dev);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200421 struct scatterlist *s;
Jon Masone4650582006-06-26 13:58:14 +0200422 unsigned long vaddr;
423 unsigned int npages;
424 unsigned long entry;
425 int i;
426
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200427 for_each_sg(sg, s, nelems, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +0200428 BUG_ON(!sg_page(s));
Jon Masone4650582006-06-26 13:58:14 +0200429
Jens Axboe58b053e2007-10-22 20:02:46 +0200430 vaddr = (unsigned long) sg_virt(s);
Jon Masone4650582006-06-26 13:58:14 +0200431 npages = num_dma_pages(vaddr, s->length);
432
FUJITA Tomonori1b39b072008-02-04 22:28:10 -0800433 entry = iommu_range_alloc(dev, tbl, npages);
Jon Masone4650582006-06-26 13:58:14 +0200434 if (entry == bad_dma_address) {
435 /* makes sure unmap knows to stop */
436 s->dma_length = 0;
437 goto error;
438 }
439
440 s->dma_address = (entry << PAGE_SHIFT) | s->offset;
441
442 /* insert into HW table */
443 tce_build(tbl, entry, npages, vaddr & PAGE_MASK,
444 direction);
445
446 s->dma_length = s->length;
447 }
448
Jon Masone4650582006-06-26 13:58:14 +0200449 return nelems;
450error:
Muli Ben-Yehuda3cc39bd2007-07-21 17:11:06 +0200451 calgary_unmap_sg(dev, sg, nelems, direction);
Jens Axboe8b87d9f2007-07-24 12:38:15 +0200452 for_each_sg(sg, s, nelems, i) {
453 sg->dma_address = bad_dma_address;
454 sg->dma_length = 0;
Jon Masone4650582006-06-26 13:58:14 +0200455 }
Jon Masone4650582006-06-26 13:58:14 +0200456 return 0;
457}
458
Ingo Molnar2be62142008-04-19 19:19:56 +0200459static dma_addr_t calgary_map_single(struct device *dev, phys_addr_t paddr,
Jon Masone4650582006-06-26 13:58:14 +0200460 size_t size, int direction)
461{
Ingo Molnar2be62142008-04-19 19:19:56 +0200462 void *vaddr = phys_to_virt(paddr);
Jon Masone4650582006-06-26 13:58:14 +0200463 unsigned long uaddr;
464 unsigned int npages;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200465 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200466
467 uaddr = (unsigned long)vaddr;
468 npages = num_dma_pages(uaddr, size);
469
Alexis Bruemmer1956a962008-07-25 19:44:51 -0700470 return iommu_alloc(dev, tbl, vaddr, npages, direction);
Jon Masone4650582006-06-26 13:58:14 +0200471}
472
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200473static void calgary_unmap_single(struct device *dev, dma_addr_t dma_handle,
Jon Masone4650582006-06-26 13:58:14 +0200474 size_t size, int direction)
475{
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200476 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200477 unsigned int npages;
478
Jon Masone4650582006-06-26 13:58:14 +0200479 npages = num_dma_pages(dma_handle, size);
480 iommu_free(tbl, dma_handle, npages);
481}
482
Yinghai Lu0b11e1c2007-07-21 17:11:05 +0200483static void* calgary_alloc_coherent(struct device *dev, size_t size,
Jon Masone4650582006-06-26 13:58:14 +0200484 dma_addr_t *dma_handle, gfp_t flag)
485{
486 void *ret = NULL;
487 dma_addr_t mapping;
488 unsigned int npages, order;
Muli Ben-Yehuda35b6dfa2007-07-21 17:10:51 +0200489 struct iommu_table *tbl = find_iommu_table(dev);
Jon Masone4650582006-06-26 13:58:14 +0200490
491 size = PAGE_ALIGN(size); /* size rounded up to full pages */
492 npages = size >> PAGE_SHIFT;
493 order = get_order(size);
494
495 /* alloc enough pages (and possibly more) */
496 ret = (void *)__get_free_pages(flag, order);
497 if (!ret)
498 goto error;
499 memset(ret, 0, size);
500
Alexis Bruemmer1956a962008-07-25 19:44:51 -0700501 /* set up tces to cover the allocated range */
502 mapping = iommu_alloc(dev, tbl, ret, npages, DMA_BIDIRECTIONAL);
503 if (mapping == bad_dma_address)
504 goto free;
505 *dma_handle = mapping;
Jon Masone4650582006-06-26 13:58:14 +0200506 return ret;
Jon Masone4650582006-06-26 13:58:14 +0200507free:
508 free_pages((unsigned long)ret, get_order(size));
509 ret = NULL;
510error:
511 return ret;
512}
513
Joerg Roedele4ad68b2008-08-19 16:32:41 +0200514static void calgary_free_coherent(struct device *dev, size_t size,
515 void *vaddr, dma_addr_t dma_handle)
516{
517 unsigned int npages;
518 struct iommu_table *tbl = find_iommu_table(dev);
519
520 size = PAGE_ALIGN(size);
521 npages = size >> PAGE_SHIFT;
522
523 iommu_free(tbl, dma_handle, npages);
524 free_pages((unsigned long)vaddr, get_order(size));
525}
526
FUJITA Tomonori8d8bb392008-07-25 19:44:49 -0700527static struct dma_mapping_ops calgary_dma_ops = {
Jon Masone4650582006-06-26 13:58:14 +0200528 .alloc_coherent = calgary_alloc_coherent,
Joerg Roedele4ad68b2008-08-19 16:32:41 +0200529 .free_coherent = calgary_free_coherent,
Jon Masone4650582006-06-26 13:58:14 +0200530 .map_single = calgary_map_single,
531 .unmap_single = calgary_unmap_single,
532 .map_sg = calgary_map_sg,
533 .unmap_sg = calgary_unmap_sg,
534};
535
Laurent Vivierb34e90b2006-12-07 02:14:06 +0100536static inline void __iomem * busno_to_bbar(unsigned char num)
537{
538 return bus_info[num].bbar;
539}
540
Jon Masone4650582006-06-26 13:58:14 +0200541static inline int busno_to_phbid(unsigned char num)
542{
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200543 return bus_info[num].phbid;
Jon Masone4650582006-06-26 13:58:14 +0200544}
545
546static inline unsigned long split_queue_offset(unsigned char num)
547{
548 size_t idx = busno_to_phbid(num);
549
550 return split_queue_offsets[idx];
551}
552
553static inline unsigned long tar_offset(unsigned char num)
554{
555 size_t idx = busno_to_phbid(num);
556
557 return tar_offsets[idx];
558}
559
560static inline unsigned long phb_offset(unsigned char num)
561{
562 size_t idx = busno_to_phbid(num);
563
564 return phb_offsets[idx];
565}
566
567static inline void __iomem* calgary_reg(void __iomem *bar, unsigned long offset)
568{
569 unsigned long target = ((unsigned long)bar) | offset;
570 return (void __iomem*)target;
571}
572
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200573static inline int is_calioc2(unsigned short device)
574{
575 return (device == PCI_DEVICE_ID_IBM_CALIOC2);
576}
577
578static inline int is_calgary(unsigned short device)
579{
580 return (device == PCI_DEVICE_ID_IBM_CALGARY);
581}
582
583static inline int is_cal_pci_dev(unsigned short device)
584{
585 return (is_calgary(device) || is_calioc2(device));
586}
587
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200588static void calgary_tce_cache_blast(struct iommu_table *tbl)
Jon Masone4650582006-06-26 13:58:14 +0200589{
590 u64 val;
591 u32 aer;
592 int i = 0;
593 void __iomem *bbar = tbl->bbar;
594 void __iomem *target;
595
596 /* disable arbitration on the bus */
597 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
598 aer = readl(target);
599 writel(0, target);
600
601 /* read plssr to ensure it got there */
602 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
603 val = readl(target);
604
605 /* poll split queues until all DMA activity is done */
606 target = calgary_reg(bbar, split_queue_offset(tbl->it_busno));
607 do {
608 val = readq(target);
609 i++;
610 } while ((val & 0xff) != 0xff && i < 100);
611 if (i == 100)
612 printk(KERN_WARNING "Calgary: PCI bus not quiesced, "
613 "continuing anyway\n");
614
615 /* invalidate TCE cache */
616 target = calgary_reg(bbar, tar_offset(tbl->it_busno));
617 writeq(tbl->tar_val, target);
618
619 /* enable arbitration */
620 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_AER_OFFSET);
621 writel(aer, target);
622 (void)readl(target); /* flush */
623}
624
Muli Ben-Yehuda00be3fa2007-07-21 17:10:54 +0200625static void calioc2_tce_cache_blast(struct iommu_table *tbl)
626{
627 void __iomem *bbar = tbl->bbar;
628 void __iomem *target;
629 u64 val64;
630 u32 val;
631 int i = 0;
632 int count = 1;
633 unsigned char bus = tbl->it_busno;
634
635begin:
636 printk(KERN_DEBUG "Calgary: CalIOC2 bus 0x%x entering tce cache blast "
637 "sequence - count %d\n", bus, count);
638
639 /* 1. using the Page Migration Control reg set SoftStop */
640 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
641 val = be32_to_cpu(readl(target));
642 printk(KERN_DEBUG "1a. read 0x%x [LE] from %p\n", val, target);
643 val |= PMR_SOFTSTOP;
644 printk(KERN_DEBUG "1b. writing 0x%x [LE] to %p\n", val, target);
645 writel(cpu_to_be32(val), target);
646
647 /* 2. poll split queues until all DMA activity is done */
648 printk(KERN_DEBUG "2a. starting to poll split queues\n");
649 target = calgary_reg(bbar, split_queue_offset(bus));
650 do {
651 val64 = readq(target);
652 i++;
653 } while ((val64 & 0xff) != 0xff && i < 100);
654 if (i == 100)
655 printk(KERN_WARNING "CalIOC2: PCI bus not quiesced, "
656 "continuing anyway\n");
657
658 /* 3. poll Page Migration DEBUG for SoftStopFault */
659 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
660 val = be32_to_cpu(readl(target));
661 printk(KERN_DEBUG "3. read 0x%x [LE] from %p\n", val, target);
662
663 /* 4. if SoftStopFault - goto (1) */
664 if (val & PMR_SOFTSTOPFAULT) {
665 if (++count < 100)
666 goto begin;
667 else {
668 printk(KERN_WARNING "CalIOC2: too many SoftStopFaults, "
669 "aborting TCE cache flush sequence!\n");
670 return; /* pray for the best */
671 }
672 }
673
674 /* 5. Slam into HardStop by reading PHB_PAGE_MIG_CTRL */
675 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
676 printk(KERN_DEBUG "5a. slamming into HardStop by reading %p\n", target);
677 val = be32_to_cpu(readl(target));
678 printk(KERN_DEBUG "5b. read 0x%x [LE] from %p\n", val, target);
679 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_DEBUG);
680 val = be32_to_cpu(readl(target));
681 printk(KERN_DEBUG "5c. read 0x%x [LE] from %p (debug)\n", val, target);
682
683 /* 6. invalidate TCE cache */
684 printk(KERN_DEBUG "6. invalidating TCE cache\n");
685 target = calgary_reg(bbar, tar_offset(bus));
686 writeq(tbl->tar_val, target);
687
688 /* 7. Re-read PMCR */
689 printk(KERN_DEBUG "7a. Re-reading PMCR\n");
690 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
691 val = be32_to_cpu(readl(target));
692 printk(KERN_DEBUG "7b. read 0x%x [LE] from %p\n", val, target);
693
694 /* 8. Remove HardStop */
695 printk(KERN_DEBUG "8a. removing HardStop from PMCR\n");
696 target = calgary_reg(bbar, phb_offset(bus) | PHB_PAGE_MIG_CTRL);
697 val = 0;
698 printk(KERN_DEBUG "8b. writing 0x%x [LE] to %p\n", val, target);
699 writel(cpu_to_be32(val), target);
700 val = be32_to_cpu(readl(target));
701 printk(KERN_DEBUG "8c. read 0x%x [LE] from %p\n", val, target);
702}
703
Jon Masone4650582006-06-26 13:58:14 +0200704static void __init calgary_reserve_mem_region(struct pci_dev *dev, u64 start,
705 u64 limit)
706{
707 unsigned int numpages;
708
709 limit = limit | 0xfffff;
710 limit++;
711
712 numpages = ((limit - start) >> PAGE_SHIFT);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300713 iommu_range_reserve(pci_iommu(dev->bus), start, numpages);
Jon Masone4650582006-06-26 13:58:14 +0200714}
715
716static void __init calgary_reserve_peripheral_mem_1(struct pci_dev *dev)
717{
718 void __iomem *target;
719 u64 low, high, sizelow;
720 u64 start, limit;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300721 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200722 unsigned char busnum = dev->bus->number;
723 void __iomem *bbar = tbl->bbar;
724
725 /* peripheral MEM_1 region */
726 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_LOW);
727 low = be32_to_cpu(readl(target));
728 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_HIGH);
729 high = be32_to_cpu(readl(target));
730 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_1_SIZE);
731 sizelow = be32_to_cpu(readl(target));
732
733 start = (high << 32) | low;
734 limit = sizelow;
735
736 calgary_reserve_mem_region(dev, start, limit);
737}
738
739static void __init calgary_reserve_peripheral_mem_2(struct pci_dev *dev)
740{
741 void __iomem *target;
742 u32 val32;
743 u64 low, high, sizelow, sizehigh;
744 u64 start, limit;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300745 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200746 unsigned char busnum = dev->bus->number;
747 void __iomem *bbar = tbl->bbar;
748
749 /* is it enabled? */
750 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
751 val32 = be32_to_cpu(readl(target));
752 if (!(val32 & PHB_MEM2_ENABLE))
753 return;
754
755 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_LOW);
756 low = be32_to_cpu(readl(target));
757 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_HIGH);
758 high = be32_to_cpu(readl(target));
759 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_LOW);
760 sizelow = be32_to_cpu(readl(target));
761 target = calgary_reg(bbar, phb_offset(busnum) | PHB_MEM_2_SIZE_HIGH);
762 sizehigh = be32_to_cpu(readl(target));
763
764 start = (high << 32) | low;
765 limit = (sizehigh << 32) | sizelow;
766
767 calgary_reserve_mem_region(dev, start, limit);
768}
769
770/*
771 * some regions of the IO address space do not get translated, so we
772 * must not give devices IO addresses in those regions. The regions
773 * are the 640KB-1MB region and the two PCI peripheral memory holes.
774 * Reserve all of them in the IOMMU bitmap to avoid giving them out
775 * later.
776 */
777static void __init calgary_reserve_regions(struct pci_dev *dev)
778{
779 unsigned int npages;
Jon Masone4650582006-06-26 13:58:14 +0200780 u64 start;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300781 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200782
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +0100783 /* reserve EMERGENCY_PAGES from bad_dma_address and up */
784 iommu_range_reserve(tbl, bad_dma_address, EMERGENCY_PAGES);
Jon Masone4650582006-06-26 13:58:14 +0200785
786 /* avoid the BIOS/VGA first 640KB-1MB region */
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200787 /* for CalIOC2 - avoid the entire first MB */
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200788 if (is_calgary(dev->device)) {
789 start = (640 * 1024);
790 npages = ((1024 - 640) * 1024) >> PAGE_SHIFT;
791 } else { /* calioc2 */
792 start = 0;
Muli Ben-Yehudae8f20412007-07-21 17:11:01 +0200793 npages = (1 * 1024 * 1024) >> PAGE_SHIFT;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200794 }
Jon Masone4650582006-06-26 13:58:14 +0200795 iommu_range_reserve(tbl, start, npages);
796
797 /* reserve the two PCI peripheral memory regions in IO space */
798 calgary_reserve_peripheral_mem_1(dev);
799 calgary_reserve_peripheral_mem_2(dev);
800}
801
802static int __init calgary_setup_tar(struct pci_dev *dev, void __iomem *bbar)
803{
804 u64 val64;
805 u64 table_phys;
806 void __iomem *target;
807 int ret;
808 struct iommu_table *tbl;
809
810 /* build TCE tables for each PHB */
811 ret = build_tce_table(dev, bbar);
812 if (ret)
813 return ret;
814
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300815 tbl = pci_iommu(dev->bus);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200816 tbl->it_base = (unsigned long)bus_info[dev->bus->number].tce_space;
Chandru95b68de2008-07-25 01:47:55 -0700817
818 if (is_kdump_kernel())
819 calgary_init_bitmap_from_tce_table(tbl);
820 else
821 tce_free(tbl, 0, tbl->it_size);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +0200822
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200823 if (is_calgary(dev->device))
824 tbl->chip_ops = &calgary_chip_ops;
Muli Ben-Yehudac3860102007-07-21 17:10:53 +0200825 else if (is_calioc2(dev->device))
826 tbl->chip_ops = &calioc2_chip_ops;
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +0200827 else
828 BUG();
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +0200829
Jon Masone4650582006-06-26 13:58:14 +0200830 calgary_reserve_regions(dev);
831
832 /* set TARs for each PHB */
833 target = calgary_reg(bbar, tar_offset(dev->bus->number));
834 val64 = be64_to_cpu(readq(target));
835
836 /* zero out all TAR bits under sw control */
837 val64 &= ~TAR_SW_BITS;
Jon Masone4650582006-06-26 13:58:14 +0200838 table_phys = (u64)__pa(tbl->it_base);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200839
Jon Masone4650582006-06-26 13:58:14 +0200840 val64 |= table_phys;
841
842 BUG_ON(specified_table_size > TCE_TABLE_SIZE_8M);
843 val64 |= (u64) specified_table_size;
844
845 tbl->tar_val = cpu_to_be64(val64);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200846
Jon Masone4650582006-06-26 13:58:14 +0200847 writeq(tbl->tar_val, target);
848 readq(target); /* flush */
849
850 return 0;
851}
852
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200853static void __init calgary_free_bus(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +0200854{
855 u64 val64;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300856 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200857 void __iomem *target;
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200858 unsigned int bitmapsz;
Jon Masone4650582006-06-26 13:58:14 +0200859
860 target = calgary_reg(tbl->bbar, tar_offset(dev->bus->number));
861 val64 = be64_to_cpu(readq(target));
862 val64 &= ~TAR_SW_BITS;
863 writeq(cpu_to_be64(val64), target);
864 readq(target); /* flush */
865
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200866 bitmapsz = tbl->it_size / BITS_PER_BYTE;
867 free_pages((unsigned long)tbl->it_map, get_order(bitmapsz));
868 tbl->it_map = NULL;
869
Jon Masone4650582006-06-26 13:58:14 +0200870 kfree(tbl);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300871
872 set_pci_iommu(dev->bus, NULL);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +0200873
874 /* Can't free bootmem allocated memory after system is up :-( */
875 bus_info[dev->bus->number].tce_space = NULL;
Jon Masone4650582006-06-26 13:58:14 +0200876}
877
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200878static void calgary_dump_error_regs(struct iommu_table *tbl)
879{
880 void __iomem *bbar = tbl->bbar;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200881 void __iomem *target;
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200882 u32 csr, plssr;
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200883
884 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200885 csr = be32_to_cpu(readl(target));
886
887 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_PLSSR_OFFSET);
888 plssr = be32_to_cpu(readl(target));
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200889
890 /* If no error, the agent ID in the CSR is not valid */
891 printk(KERN_EMERG "Calgary: DMA error on Calgary PHB 0x%x, "
Muli Ben-Yehudaddbd41b2007-07-21 17:10:57 +0200892 "0x%08x@CSR 0x%08x@PLSSR\n", tbl->it_busno, csr, plssr);
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200893}
894
895static void calioc2_dump_error_regs(struct iommu_table *tbl)
896{
897 void __iomem *bbar = tbl->bbar;
898 u32 csr, csmr, plssr, mck, rcstat;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200899 void __iomem *target;
900 unsigned long phboff = phb_offset(tbl->it_busno);
901 unsigned long erroff;
902 u32 errregs[7];
903 int i;
904
905 /* dump CSR */
906 target = calgary_reg(bbar, phboff | PHB_CSR_OFFSET);
907 csr = be32_to_cpu(readl(target));
908 /* dump PLSSR */
909 target = calgary_reg(bbar, phboff | PHB_PLSSR_OFFSET);
910 plssr = be32_to_cpu(readl(target));
911 /* dump CSMR */
912 target = calgary_reg(bbar, phboff | 0x290);
913 csmr = be32_to_cpu(readl(target));
914 /* dump mck */
915 target = calgary_reg(bbar, phboff | 0x800);
916 mck = be32_to_cpu(readl(target));
917
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200918 printk(KERN_EMERG "Calgary: DMA error on CalIOC2 PHB 0x%x\n",
919 tbl->it_busno);
920
921 printk(KERN_EMERG "Calgary: 0x%08x@CSR 0x%08x@PLSSR 0x%08x@CSMR 0x%08x@MCK\n",
922 csr, plssr, csmr, mck);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200923
924 /* dump rest of error regs */
925 printk(KERN_EMERG "Calgary: ");
926 for (i = 0; i < ARRAY_SIZE(errregs); i++) {
Muli Ben-Yehuda7354b072007-07-21 17:11:03 +0200927 /* err regs are at 0x810 - 0x870 */
928 erroff = (0x810 + (i * 0x10));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200929 target = calgary_reg(bbar, phboff | erroff);
930 errregs[i] = be32_to_cpu(readl(target));
931 printk("0x%08x@0x%lx ", errregs[i], erroff);
932 }
933 printk("\n");
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200934
935 /* root complex status */
936 target = calgary_reg(bbar, phboff | PHB_ROOT_COMPLEX_STATUS);
937 rcstat = be32_to_cpu(readl(target));
938 printk(KERN_EMERG "Calgary: 0x%08x@0x%x\n", rcstat,
939 PHB_ROOT_COMPLEX_STATUS);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200940}
941
Jon Masone4650582006-06-26 13:58:14 +0200942static void calgary_watchdog(unsigned long data)
943{
944 struct pci_dev *dev = (struct pci_dev *)data;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +0300945 struct iommu_table *tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +0200946 void __iomem *bbar = tbl->bbar;
947 u32 val32;
948 void __iomem *target;
949
950 target = calgary_reg(bbar, phb_offset(tbl->it_busno) | PHB_CSR_OFFSET);
951 val32 = be32_to_cpu(readl(target));
952
953 /* If no error, the agent ID in the CSR is not valid */
954 if (val32 & CSR_AGENT_MASK) {
Muli Ben-Yehuda8cb32dc2007-07-21 17:10:55 +0200955 tbl->chip_ops->dump_error_regs(tbl);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200956
957 /* reset error */
Jon Masone4650582006-06-26 13:58:14 +0200958 writel(0, target);
959
960 /* Disable bus that caused the error */
961 target = calgary_reg(bbar, phb_offset(tbl->it_busno) |
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +0200962 PHB_CONFIG_RW_OFFSET);
Jon Masone4650582006-06-26 13:58:14 +0200963 val32 = be32_to_cpu(readl(target));
964 val32 |= PHB_SLOT_DISABLE;
965 writel(cpu_to_be32(val32), target);
966 readl(target); /* flush */
967 } else {
968 /* Reset the timer */
969 mod_timer(&tbl->watchdog_timer, jiffies + 2 * HZ);
970 }
971}
972
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +0200973static void __init calgary_set_split_completion_timeout(void __iomem *bbar,
974 unsigned char busnum, unsigned long timeout)
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200975{
976 u64 val64;
977 void __iomem *target;
Muli Ben-Yehuda58db8542006-12-07 02:14:06 +0100978 unsigned int phb_shift = ~0; /* silence gcc */
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +0200979 u64 mask;
980
981 switch (busno_to_phbid(busnum)) {
982 case 0: phb_shift = (63 - 19);
983 break;
984 case 1: phb_shift = (63 - 23);
985 break;
986 case 2: phb_shift = (63 - 27);
987 break;
988 case 3: phb_shift = (63 - 35);
989 break;
990 default:
991 BUG_ON(busno_to_phbid(busnum));
992 }
993
994 target = calgary_reg(bbar, CALGARY_CONFIG_REG);
995 val64 = be64_to_cpu(readq(target));
996
997 /* zero out this PHB's timer bits */
998 mask = ~(0xFUL << phb_shift);
999 val64 &= mask;
Muli Ben-Yehudaa2b663f2007-07-21 17:10:47 +02001000 val64 |= (timeout << phb_shift);
Muli Ben-Yehudacb01fc72006-10-22 00:41:15 +02001001 writeq(cpu_to_be64(val64), target);
1002 readq(target); /* flush */
1003}
1004
Sam Ravnborg31f3dff2008-02-01 17:49:42 +01001005static void __init calioc2_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001006{
1007 unsigned char busnum = dev->bus->number;
1008 void __iomem *bbar = tbl->bbar;
1009 void __iomem *target;
1010 u32 val;
1011
Muli Ben-Yehuda8bcf7702007-07-21 17:11:00 +02001012 /*
1013 * CalIOC2 designers recommend setting bit 8 in 0xnDB0 to 1
1014 */
1015 target = calgary_reg(bbar, phb_offset(busnum) | PHB_SAVIOR_L2);
1016 val = cpu_to_be32(readl(target));
1017 val |= 0x00800000;
1018 writel(cpu_to_be32(val), target);
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001019}
1020
Sam Ravnborg31f3dff2008-02-01 17:49:42 +01001021static void __init calgary_handle_quirks(struct iommu_table *tbl, struct pci_dev *dev)
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001022{
1023 unsigned char busnum = dev->bus->number;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001024
1025 /*
1026 * Give split completion a longer timeout on bus 1 for aic94xx
1027 * http://bugzilla.kernel.org/show_bug.cgi?id=7180
1028 */
Muli Ben-Yehudac3860102007-07-21 17:10:53 +02001029 if (is_calgary(dev->device) && (busnum == 1))
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001030 calgary_set_split_completion_timeout(tbl->bbar, busnum,
1031 CCR_2SEC_TIMEOUT);
1032}
1033
Jon Masone4650582006-06-26 13:58:14 +02001034static void __init calgary_enable_translation(struct pci_dev *dev)
1035{
1036 u32 val32;
1037 unsigned char busnum;
1038 void __iomem *target;
1039 void __iomem *bbar;
1040 struct iommu_table *tbl;
1041
1042 busnum = dev->bus->number;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001043 tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +02001044 bbar = tbl->bbar;
1045
1046 /* enable TCE in PHB Config Register */
1047 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1048 val32 = be32_to_cpu(readl(target));
1049 val32 |= PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE;
1050
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001051 printk(KERN_INFO "Calgary: enabling translation on %s PHB %#x\n",
1052 (dev->device == PCI_DEVICE_ID_IBM_CALGARY) ?
1053 "Calgary" : "CalIOC2", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001054 printk(KERN_INFO "Calgary: errant DMAs will now be prevented on this "
1055 "bus.\n");
1056
1057 writel(cpu_to_be32(val32), target);
1058 readl(target); /* flush */
1059
1060 init_timer(&tbl->watchdog_timer);
1061 tbl->watchdog_timer.function = &calgary_watchdog;
1062 tbl->watchdog_timer.data = (unsigned long)dev;
1063 mod_timer(&tbl->watchdog_timer, jiffies);
1064}
1065
1066static void __init calgary_disable_translation(struct pci_dev *dev)
1067{
1068 u32 val32;
1069 unsigned char busnum;
1070 void __iomem *target;
1071 void __iomem *bbar;
1072 struct iommu_table *tbl;
1073
1074 busnum = dev->bus->number;
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001075 tbl = pci_iommu(dev->bus);
Jon Masone4650582006-06-26 13:58:14 +02001076 bbar = tbl->bbar;
1077
1078 /* disable TCE in PHB Config Register */
1079 target = calgary_reg(bbar, phb_offset(busnum) | PHB_CONFIG_RW_OFFSET);
1080 val32 = be32_to_cpu(readl(target));
1081 val32 &= ~(PHB_TCE_ENABLE | PHB_DAC_DISABLE | PHB_MCSR_ENABLE);
1082
Jon Mason70d666d2006-10-05 18:47:21 +02001083 printk(KERN_INFO "Calgary: disabling translation on PHB %#x!\n", busnum);
Jon Masone4650582006-06-26 13:58:14 +02001084 writel(cpu_to_be32(val32), target);
1085 readl(target); /* flush */
1086
1087 del_timer_sync(&tbl->watchdog_timer);
1088}
1089
Muli Ben-Yehudaa4fc5202006-09-26 10:52:31 +02001090static void __init calgary_init_one_nontraslated(struct pci_dev *dev)
Jon Masone4650582006-06-26 13:58:14 +02001091{
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001092 pci_dev_get(dev);
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001093 set_pci_iommu(dev->bus, NULL);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001094
1095 /* is the device behind a bridge? */
1096 if (dev->bus->parent)
1097 dev->bus->parent->self = dev;
1098 else
1099 dev->bus->self = dev;
Jon Masone4650582006-06-26 13:58:14 +02001100}
1101
1102static int __init calgary_init_one(struct pci_dev *dev)
1103{
Jon Masone4650582006-06-26 13:58:14 +02001104 void __iomem *bbar;
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001105 struct iommu_table *tbl;
Jon Masone4650582006-06-26 13:58:14 +02001106 int ret;
1107
Jon Masondedc9932006-10-05 18:47:21 +02001108 BUG_ON(dev->bus->number >= MAX_PHB_BUS_NUM);
1109
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001110 bbar = busno_to_bbar(dev->bus->number);
Jon Masone4650582006-06-26 13:58:14 +02001111 ret = calgary_setup_tar(dev, bbar);
1112 if (ret)
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001113 goto done;
Jon Masone4650582006-06-26 13:58:14 +02001114
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001115 pci_dev_get(dev);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001116
1117 if (dev->bus->parent) {
1118 if (dev->bus->parent->self)
1119 printk(KERN_WARNING "Calgary: IEEEE, dev %p has "
1120 "bus->parent->self!\n", dev);
1121 dev->bus->parent->self = dev;
1122 } else
1123 dev->bus->self = dev;
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001124
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001125 tbl = pci_iommu(dev->bus);
Muli Ben-Yehudaff297b82007-07-21 17:10:50 +02001126 tbl->chip_ops->handle_quirks(tbl, dev);
Muli Ben-Yehudab8d2ea12007-07-21 17:10:49 +02001127
Jon Masone4650582006-06-26 13:58:14 +02001128 calgary_enable_translation(dev);
1129
1130 return 0;
1131
Jon Masone4650582006-06-26 13:58:14 +02001132done:
1133 return ret;
1134}
1135
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001136static int __init calgary_locate_bbars(void)
Jon Masone4650582006-06-26 13:58:14 +02001137{
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001138 int ret;
1139 int rioidx, phb, bus;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001140 void __iomem *bbar;
1141 void __iomem *target;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001142 unsigned long offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001143 u8 start_bus, end_bus;
1144 u32 val;
1145
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001146 ret = -ENODATA;
1147 for (rioidx = 0; rioidx < rio_table_hdr->num_rio_dev; rioidx++) {
1148 struct rio_detail *rio = rio_devs[rioidx];
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001149
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001150 if ((rio->type != COMPAT_CALGARY) && (rio->type != ALT_CALGARY))
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001151 continue;
1152
1153 /* map entire 1MB of Calgary config space */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001154 bbar = ioremap_nocache(rio->BBAR, 1024 * 1024);
1155 if (!bbar)
1156 goto error;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001157
1158 for (phb = 0; phb < PHBS_PER_CALGARY; phb++) {
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001159 offset = phb_debug_offsets[phb] | PHB_DEBUG_STUFF_OFFSET;
1160 target = calgary_reg(bbar, offset);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001161
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001162 val = be32_to_cpu(readl(target));
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001163
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001164 start_bus = (u8)((val & 0x00FF0000) >> 16);
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001165 end_bus = (u8)((val & 0x0000FF00) >> 8);
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001166
1167 if (end_bus) {
1168 for (bus = start_bus; bus <= end_bus; bus++) {
1169 bus_info[bus].bbar = bbar;
1170 bus_info[bus].phbid = phb;
1171 }
1172 } else {
1173 bus_info[start_bus].bbar = bbar;
1174 bus_info[start_bus].phbid = phb;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001175 }
1176 }
1177 }
1178
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001179 return 0;
1180
1181error:
1182 /* scan bus_info and iounmap any bbars we previously ioremap'd */
1183 for (bus = 0; bus < ARRAY_SIZE(bus_info); bus++)
1184 if (bus_info[bus].bbar)
1185 iounmap(bus_info[bus].bbar);
1186
1187 return ret;
1188}
1189
1190static int __init calgary_init(void)
1191{
1192 int ret;
1193 struct pci_dev *dev = NULL;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001194 struct calgary_bus_info *info;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001195
1196 ret = calgary_locate_bbars();
1197 if (ret)
1198 return ret;
Jon Masone4650582006-06-26 13:58:14 +02001199
Chandru95b68de2008-07-25 01:47:55 -07001200 /* Purely for kdump kernel case */
1201 if (is_kdump_kernel())
1202 get_tce_space_from_tar();
1203
Jon Masondedc9932006-10-05 18:47:21 +02001204 do {
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001205 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Jon Masone4650582006-06-26 13:58:14 +02001206 if (!dev)
1207 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001208 if (!is_cal_pci_dev(dev->device))
1209 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001210
1211 info = &bus_info[dev->bus->number];
1212 if (info->translation_disabled) {
Jon Masone4650582006-06-26 13:58:14 +02001213 calgary_init_one_nontraslated(dev);
1214 continue;
1215 }
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001216
1217 if (!info->tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001218 continue;
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001219
Jon Masone4650582006-06-26 13:58:14 +02001220 ret = calgary_init_one(dev);
1221 if (ret)
1222 goto error;
Jon Masondedc9932006-10-05 18:47:21 +02001223 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001224
Alexis Bruemmer1956a962008-07-25 19:44:51 -07001225 dev = NULL;
1226 for_each_pci_dev(dev) {
1227 struct iommu_table *tbl;
1228
1229 tbl = find_iommu_table(&dev->dev);
1230
1231 if (translation_enabled(tbl))
1232 dev->dev.archdata.dma_ops = &calgary_dma_ops;
1233 }
1234
Jon Masone4650582006-06-26 13:58:14 +02001235 return ret;
1236
1237error:
Jon Masondedc9932006-10-05 18:47:21 +02001238 do {
Greg Kroah-Hartmana2b5d872008-02-13 09:32:03 -08001239 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
Muli Ben-Yehuda9f2dc462006-09-26 10:52:31 +02001240 if (!dev)
1241 break;
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001242 if (!is_cal_pci_dev(dev->device))
1243 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001244
1245 info = &bus_info[dev->bus->number];
1246 if (info->translation_disabled) {
Jon Masone4650582006-06-26 13:58:14 +02001247 pci_dev_put(dev);
1248 continue;
1249 }
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001250 if (!info->tce_space && !translate_empty_slots)
Jon Masone4650582006-06-26 13:58:14 +02001251 continue;
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001252
Jon Masone4650582006-06-26 13:58:14 +02001253 calgary_disable_translation(dev);
Muli Ben-Yehudab8f4fe62006-09-26 10:52:31 +02001254 calgary_free_bus(dev);
Muli Ben-Yehuda871b1702006-09-26 10:52:31 +02001255 pci_dev_put(dev); /* Undo calgary_init_one()'s pci_dev_get() */
Alexis Bruemmer1956a962008-07-25 19:44:51 -07001256 dev->dev.archdata.dma_ops = NULL;
Jon Masondedc9932006-10-05 18:47:21 +02001257 } while (1);
Jon Masone4650582006-06-26 13:58:14 +02001258
1259 return ret;
1260}
1261
1262static inline int __init determine_tce_table_size(u64 ram)
1263{
1264 int ret;
1265
1266 if (specified_table_size != TCE_TABLE_SIZE_UNSPECIFIED)
1267 return specified_table_size;
1268
1269 /*
1270 * Table sizes are from 0 to 7 (TCE_TABLE_SIZE_64K to
1271 * TCE_TABLE_SIZE_8M). Table size 0 has 8K entries and each
1272 * larger table size has twice as many entries, so shift the
1273 * max ram address by 13 to divide by 8K and then look at the
1274 * order of the result to choose between 0-7.
1275 */
1276 ret = get_order(ram >> 13);
1277 if (ret > TCE_TABLE_SIZE_8M)
1278 ret = TCE_TABLE_SIZE_8M;
1279
1280 return ret;
1281}
1282
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001283static int __init build_detail_arrays(void)
1284{
1285 unsigned long ptr;
1286 int i, scal_detail_size, rio_detail_size;
1287
1288 if (rio_table_hdr->num_scal_dev > MAX_NUMNODES){
1289 printk(KERN_WARNING
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001290 "Calgary: MAX_NUMNODES too low! Defined as %d, "
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001291 "but system has %d nodes.\n",
1292 MAX_NUMNODES, rio_table_hdr->num_scal_dev);
1293 return -ENODEV;
1294 }
1295
1296 switch (rio_table_hdr->version){
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001297 case 2:
1298 scal_detail_size = 11;
1299 rio_detail_size = 13;
1300 break;
1301 case 3:
1302 scal_detail_size = 12;
1303 rio_detail_size = 15;
1304 break;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001305 default:
1306 printk(KERN_WARNING
1307 "Calgary: Invalid Rio Grande Table Version: %d\n",
1308 rio_table_hdr->version);
1309 return -EPROTO;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001310 }
1311
1312 ptr = ((unsigned long)rio_table_hdr) + 3;
1313 for (i = 0; i < rio_table_hdr->num_scal_dev;
1314 i++, ptr += scal_detail_size)
1315 scal_devs[i] = (struct scal_detail *)ptr;
1316
1317 for (i = 0; i < rio_table_hdr->num_rio_dev;
1318 i++, ptr += rio_detail_size)
1319 rio_devs[i] = (struct rio_detail *)ptr;
1320
1321 return 0;
1322}
1323
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001324static int __init calgary_bus_has_devices(int bus, unsigned short pci_dev)
1325{
1326 int dev;
1327 u32 val;
1328
1329 if (pci_dev == PCI_DEVICE_ID_IBM_CALIOC2) {
1330 /*
1331 * FIXME: properly scan for devices accross the
1332 * PCI-to-PCI bridge on every CalIOC2 port.
1333 */
1334 return 1;
1335 }
1336
1337 for (dev = 1; dev < 8; dev++) {
1338 val = read_pci_config(bus, dev, 0, 0);
1339 if (val != 0xffffffff)
1340 break;
1341 }
1342 return (val != 0xffffffff);
1343}
1344
Chandru95b68de2008-07-25 01:47:55 -07001345/*
1346 * calgary_init_bitmap_from_tce_table():
1347 * Funtion for kdump case. In the second/kdump kernel initialize
1348 * the bitmap based on the tce table entries obtained from first kernel
1349 */
1350static void calgary_init_bitmap_from_tce_table(struct iommu_table *tbl)
1351{
1352 u64 *tp;
1353 unsigned int index;
1354 tp = ((u64 *)tbl->it_base);
1355 for (index = 0 ; index < tbl->it_size; index++) {
1356 if (*tp != 0x0)
1357 set_bit(index, tbl->it_map);
1358 tp++;
1359 }
1360}
1361
1362/*
1363 * get_tce_space_from_tar():
1364 * Function for kdump case. Get the tce tables from first kernel
1365 * by reading the contents of the base adress register of calgary iommu
1366 */
Marcin Slusarzf7106662008-08-17 17:50:52 +02001367static void __init get_tce_space_from_tar(void)
Chandru95b68de2008-07-25 01:47:55 -07001368{
1369 int bus;
1370 void __iomem *target;
1371 unsigned long tce_space;
1372
1373 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
1374 struct calgary_bus_info *info = &bus_info[bus];
1375 unsigned short pci_device;
1376 u32 val;
1377
1378 val = read_pci_config(bus, 0, 0, 0);
1379 pci_device = (val & 0xFFFF0000) >> 16;
1380
1381 if (!is_cal_pci_dev(pci_device))
1382 continue;
1383 if (info->translation_disabled)
1384 continue;
1385
1386 if (calgary_bus_has_devices(bus, pci_device) ||
1387 translate_empty_slots) {
1388 target = calgary_reg(bus_info[bus].bbar,
1389 tar_offset(bus));
1390 tce_space = be64_to_cpu(readq(target));
1391 tce_space = tce_space & TAR_SW_BITS;
1392
1393 tce_space = tce_space & (~specified_table_size);
1394 info->tce_space = (u64 *)__va(tce_space);
1395 }
1396 }
1397 return;
1398}
1399
Jon Masone4650582006-06-26 13:58:14 +02001400void __init detect_calgary(void)
1401{
Jon Masond2105b12006-07-29 21:42:43 +02001402 int bus;
Jon Masone4650582006-06-26 13:58:14 +02001403 void *tbl;
Jon Masond2105b12006-07-29 21:42:43 +02001404 int calgary_found = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001405 unsigned long ptr;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001406 unsigned int offset, prev_offset;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001407 int ret;
Jon Masone4650582006-06-26 13:58:14 +02001408
1409 /*
1410 * if the user specified iommu=off or iommu=soft or we found
1411 * another HW IOMMU already, bail out.
1412 */
1413 if (swiotlb || no_iommu || iommu_detected)
1414 return;
1415
Muli Ben-Yehudabff65472006-12-07 02:14:07 +01001416 if (!use_calgary)
1417 return;
1418
Andi Kleen0637a702006-09-26 10:52:41 +02001419 if (!early_pci_allowed())
1420 return;
1421
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001422 printk(KERN_DEBUG "Calgary: detecting Calgary via BIOS EBDA area\n");
1423
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001424 ptr = (unsigned long)phys_to_virt(get_bios_ebda());
1425
1426 rio_table_hdr = NULL;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001427 prev_offset = 0;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001428 offset = 0x180;
Ingo Molnar136f1e72006-12-20 11:53:32 +01001429 /*
1430 * The next offset is stored in the 1st word.
1431 * Only parse up until the offset increases:
1432 */
1433 while (offset > prev_offset) {
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001434 /* The block id is stored in the 2nd word */
1435 if (*((unsigned short *)(ptr + offset + 2)) == 0x4752){
1436 /* set the pointer past the offset & block id */
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001437 rio_table_hdr = (struct rio_table_hdr *)(ptr + offset + 4);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001438 break;
1439 }
Ingo Molnar136f1e72006-12-20 11:53:32 +01001440 prev_offset = offset;
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001441 offset = *((unsigned short *)(ptr + offset));
1442 }
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001443 if (!rio_table_hdr) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001444 printk(KERN_DEBUG "Calgary: Unable to locate Rio Grande table "
1445 "in EBDA - bailing!\n");
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001446 return;
1447 }
1448
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001449 ret = build_detail_arrays();
1450 if (ret) {
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001451 printk(KERN_DEBUG "Calgary: build_detail_arrays ret %d\n", ret);
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001452 return;
Muli Ben-Yehudaeae93752006-12-07 02:14:06 +01001453 }
Laurent Vivierb34e90b2006-12-07 02:14:06 +01001454
Chandru95b68de2008-07-25 01:47:55 -07001455 specified_table_size = determine_tce_table_size((is_kdump_kernel() ?
1456 saved_max_pfn : max_pfn) * PAGE_SIZE);
Jon Masone4650582006-06-26 13:58:14 +02001457
Jon Masond2105b12006-07-29 21:42:43 +02001458 for (bus = 0; bus < MAX_PHB_BUS_NUM; bus++) {
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001459 struct calgary_bus_info *info = &bus_info[bus];
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001460 unsigned short pci_device;
1461 u32 val;
Jon Masond2105b12006-07-29 21:42:43 +02001462
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001463 val = read_pci_config(bus, 0, 0, 0);
1464 pci_device = (val & 0xFFFF0000) >> 16;
1465
1466 if (!is_cal_pci_dev(pci_device))
Jon Masone4650582006-06-26 13:58:14 +02001467 continue;
Jon Masond2105b12006-07-29 21:42:43 +02001468
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001469 if (info->translation_disabled)
Jon Masone4650582006-06-26 13:58:14 +02001470 continue;
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001471
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001472 if (calgary_bus_has_devices(bus, pci_device) ||
1473 translate_empty_slots) {
Chandru95b68de2008-07-25 01:47:55 -07001474 /*
1475 * If it is kdump kernel, find and use tce tables
1476 * from first kernel, else allocate tce tables here
1477 */
1478 if (!is_kdump_kernel()) {
1479 tbl = alloc_tce_table();
1480 if (!tbl)
1481 goto cleanup;
1482 info->tce_space = tbl;
1483 }
Muli Ben-Yehuda8a244592007-07-21 17:10:52 +02001484 calgary_found = 1;
Jon Masond2105b12006-07-29 21:42:43 +02001485 }
Jon Masone4650582006-06-26 13:58:14 +02001486 }
1487
Muli Ben-Yehudab92cc552007-01-11 01:52:44 +01001488 printk(KERN_DEBUG "Calgary: finished detection, Calgary %s\n",
1489 calgary_found ? "found" : "not found");
1490
Jon Masond2105b12006-07-29 21:42:43 +02001491 if (calgary_found) {
Jon Masone4650582006-06-26 13:58:14 +02001492 iommu_detected = 1;
1493 calgary_detected = 1;
Muli Ben-Yehudade684652006-09-26 10:52:33 +02001494 printk(KERN_INFO "PCI-DMA: Calgary IOMMU detected.\n");
1495 printk(KERN_INFO "PCI-DMA: Calgary TCE table spec is %d, "
1496 "CONFIG_IOMMU_DEBUG is %s.\n", specified_table_size,
1497 debugging ? "enabled" : "disabled");
Alexis Bruemmer1956a962008-07-25 19:44:51 -07001498
1499 /* swiotlb for devices that aren't behind the Calgary. */
1500 if (max_pfn > MAX_DMA32_PFN)
1501 swiotlb = 1;
Jon Masone4650582006-06-26 13:58:14 +02001502 }
1503 return;
1504
1505cleanup:
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001506 for (--bus; bus >= 0; --bus) {
1507 struct calgary_bus_info *info = &bus_info[bus];
1508
1509 if (info->tce_space)
1510 free_tce_table(info->tce_space);
1511 }
Jon Masone4650582006-06-26 13:58:14 +02001512}
1513
1514int __init calgary_iommu_init(void)
1515{
1516 int ret;
1517
Alexis Bruemmer1956a962008-07-25 19:44:51 -07001518 if (no_iommu || (swiotlb && !calgary_detected))
Jon Masone4650582006-06-26 13:58:14 +02001519 return -ENODEV;
1520
1521 if (!calgary_detected)
1522 return -ENODEV;
1523
1524 /* ok, we're trying to use Calgary - let's roll */
1525 printk(KERN_INFO "PCI-DMA: Using Calgary IOMMU\n");
1526
1527 ret = calgary_init();
1528 if (ret) {
1529 printk(KERN_ERR "PCI-DMA: Calgary init failed %d, "
1530 "falling back to no_iommu\n", ret);
Jon Masone4650582006-06-26 13:58:14 +02001531 return ret;
1532 }
1533
1534 force_iommu = 1;
Muli Ben-Yehuda310adfd2007-02-13 13:26:24 +01001535 bad_dma_address = 0x0;
Alexis Bruemmer1956a962008-07-25 19:44:51 -07001536 /* dma_ops is set to swiotlb or nommu */
1537 if (!dma_ops)
1538 dma_ops = &nommu_dma_ops;
Jon Masone4650582006-06-26 13:58:14 +02001539
1540 return 0;
1541}
1542
1543static int __init calgary_parse_options(char *p)
1544{
1545 unsigned int bridge;
1546 size_t len;
1547 char* endp;
1548
1549 while (*p) {
1550 if (!strncmp(p, "64k", 3))
1551 specified_table_size = TCE_TABLE_SIZE_64K;
1552 else if (!strncmp(p, "128k", 4))
1553 specified_table_size = TCE_TABLE_SIZE_128K;
1554 else if (!strncmp(p, "256k", 4))
1555 specified_table_size = TCE_TABLE_SIZE_256K;
1556 else if (!strncmp(p, "512k", 4))
1557 specified_table_size = TCE_TABLE_SIZE_512K;
1558 else if (!strncmp(p, "1M", 2))
1559 specified_table_size = TCE_TABLE_SIZE_1M;
1560 else if (!strncmp(p, "2M", 2))
1561 specified_table_size = TCE_TABLE_SIZE_2M;
1562 else if (!strncmp(p, "4M", 2))
1563 specified_table_size = TCE_TABLE_SIZE_4M;
1564 else if (!strncmp(p, "8M", 2))
1565 specified_table_size = TCE_TABLE_SIZE_8M;
1566
1567 len = strlen("translate_empty_slots");
1568 if (!strncmp(p, "translate_empty_slots", len))
1569 translate_empty_slots = 1;
1570
1571 len = strlen("disable");
1572 if (!strncmp(p, "disable", len)) {
1573 p += len;
1574 if (*p == '=')
1575 ++p;
1576 if (*p == '\0')
1577 break;
1578 bridge = simple_strtol(p, &endp, 0);
1579 if (p == endp)
1580 break;
1581
Jon Masond2105b12006-07-29 21:42:43 +02001582 if (bridge < MAX_PHB_BUS_NUM) {
Jon Masone4650582006-06-26 13:58:14 +02001583 printk(KERN_INFO "Calgary: disabling "
Jon Mason70d666d2006-10-05 18:47:21 +02001584 "translation for PHB %#x\n", bridge);
Muli Ben-Yehudaf38db652006-09-26 10:52:31 +02001585 bus_info[bridge].translation_disabled = 1;
Jon Masone4650582006-06-26 13:58:14 +02001586 }
1587 }
1588
1589 p = strpbrk(p, ",");
1590 if (!p)
1591 break;
1592
1593 p++; /* skip ',' */
1594 }
1595 return 1;
1596}
1597__setup("calgary=", calgary_parse_options);
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001598
1599static void __init calgary_fixup_one_tce_space(struct pci_dev *dev)
1600{
1601 struct iommu_table *tbl;
1602 unsigned int npages;
1603 int i;
1604
Muli Ben-Yehuda08f1c192007-07-22 00:23:39 +03001605 tbl = pci_iommu(dev->bus);
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001606
1607 for (i = 0; i < 4; i++) {
1608 struct resource *r = &dev->resource[PCI_BRIDGE_RESOURCES + i];
1609
1610 /* Don't give out TCEs that map MEM resources */
1611 if (!(r->flags & IORESOURCE_MEM))
1612 continue;
1613
1614 /* 0-based? we reserve the whole 1st MB anyway */
1615 if (!r->start)
1616 continue;
1617
1618 /* cover the whole region */
1619 npages = (r->end - r->start) >> PAGE_SHIFT;
1620 npages++;
1621
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001622 iommu_range_reserve(tbl, r->start, npages);
1623 }
1624}
1625
1626static int __init calgary_fixup_tce_spaces(void)
1627{
1628 struct pci_dev *dev = NULL;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001629 struct calgary_bus_info *info;
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001630
1631 if (no_iommu || swiotlb || !calgary_detected)
1632 return -ENODEV;
1633
Muli Ben-Yehuda12de2572007-07-21 17:11:02 +02001634 printk(KERN_DEBUG "Calgary: fixing up tce spaces\n");
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001635
1636 do {
1637 dev = pci_get_device(PCI_VENDOR_ID_IBM, PCI_ANY_ID, dev);
1638 if (!dev)
1639 break;
1640 if (!is_cal_pci_dev(dev->device))
1641 continue;
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001642
1643 info = &bus_info[dev->bus->number];
1644 if (info->translation_disabled)
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001645 continue;
1646
Muli Ben-Yehudabc3c6052007-10-17 18:04:39 +02001647 if (!info->tce_space)
Muli Ben-Yehuda07877cf2007-07-21 17:10:58 +02001648 continue;
1649
1650 calgary_fixup_one_tce_space(dev);
1651
1652 } while (1);
1653
1654 return 0;
1655}
1656
1657/*
1658 * We need to be call after pcibios_assign_resources (fs_initcall level)
1659 * and before device_initcall.
1660 */
1661rootfs_initcall(calgary_fixup_tce_spaces);