Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation |
| 3 | * |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 4 | * Rewrite, cleanup: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
Olof Johansson | 91f1448 | 2005-11-21 02:12:32 -0600 | [diff] [blame] | 6 | * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 7 | * Copyright (C) 2006 Olof Johansson <olof@lixom.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR. |
| 10 | * |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 11 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 16 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 21 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <linux/config.h> |
| 28 | #include <linux/init.h> |
| 29 | #include <linux/types.h> |
| 30 | #include <linux/slab.h> |
| 31 | #include <linux/mm.h> |
| 32 | #include <linux/spinlock.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/pci.h> |
| 35 | #include <linux/dma-mapping.h> |
| 36 | #include <asm/io.h> |
| 37 | #include <asm/prom.h> |
| 38 | #include <asm/rtas.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #include <asm/iommu.h> |
| 40 | #include <asm/pci-bridge.h> |
| 41 | #include <asm/machdep.h> |
| 42 | #include <asm/abs_addr.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/pSeries_reconfig.h> |
Stephen Rothwell | 1ababe1 | 2005-08-03 14:35:25 +1000 | [diff] [blame] | 44 | #include <asm/firmware.h> |
Olof Johansson | c707ffc | 2005-09-20 13:45:41 +1000 | [diff] [blame] | 45 | #include <asm/tce.h> |
Stephen Rothwell | d387899 | 2005-09-28 02:50:25 +1000 | [diff] [blame] | 46 | #include <asm/ppc-pci.h> |
Paul Mackerras | 2249ca9 | 2005-11-07 13:18:13 +1100 | [diff] [blame] | 47 | #include <asm/udbg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Michael Ellerman | a121872 | 2005-11-03 15:33:31 +1100 | [diff] [blame] | 49 | #include "plpar_wrappers.h" |
| 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #define DBG(fmt...) |
| 52 | |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 53 | static void tce_build_pSeries(struct iommu_table *tbl, long index, |
| 54 | long npages, unsigned long uaddr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | enum dma_data_direction direction) |
| 56 | { |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 57 | u64 proto_tce; |
| 58 | u64 *tcep; |
| 59 | u64 rpn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
Olof Johansson | d0035c62 | 2005-09-20 13:46:44 +1000 | [diff] [blame] | 61 | index <<= TCE_PAGE_FACTOR; |
| 62 | npages <<= TCE_PAGE_FACTOR; |
| 63 | |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 64 | proto_tce = TCE_PCI_READ; // Read allowed |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | if (direction != DMA_TO_DEVICE) |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 67 | proto_tce |= TCE_PCI_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 69 | tcep = ((u64 *)tbl->it_base) + index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | while (npages--) { |
| 72 | /* can't move this out since we might cross LMB boundary */ |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 73 | rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT; |
| 74 | *tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Olof Johansson | d0035c62 | 2005-09-20 13:46:44 +1000 | [diff] [blame] | 76 | uaddr += TCE_PAGE_SIZE; |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 77 | tcep++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | |
| 82 | static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages) |
| 83 | { |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 84 | u64 *tcep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Olof Johansson | d0035c62 | 2005-09-20 13:46:44 +1000 | [diff] [blame] | 86 | npages <<= TCE_PAGE_FACTOR; |
| 87 | index <<= TCE_PAGE_FACTOR; |
| 88 | |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 89 | tcep = ((u64 *)tbl->it_base) + index; |
| 90 | |
| 91 | while (npages--) |
| 92 | *(tcep++) = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum, |
| 97 | long npages, unsigned long uaddr, |
| 98 | enum dma_data_direction direction) |
| 99 | { |
| 100 | u64 rc; |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 101 | u64 proto_tce, tce; |
| 102 | u64 rpn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Michal Ostrowski | cc8b5c9 | 2005-12-02 13:09:13 +1100 | [diff] [blame] | 104 | tcenum <<= TCE_PAGE_FACTOR; |
| 105 | npages <<= TCE_PAGE_FACTOR; |
| 106 | |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 107 | rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT; |
| 108 | proto_tce = TCE_PCI_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | if (direction != DMA_TO_DEVICE) |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 110 | proto_tce |= TCE_PCI_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | while (npages--) { |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 113 | tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT; |
| 114 | rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce); |
| 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (rc && printk_ratelimit()) { |
| 117 | printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc); |
| 118 | printk("\tindex = 0x%lx\n", (u64)tbl->it_index); |
| 119 | printk("\ttcenum = 0x%lx\n", (u64)tcenum); |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 120 | printk("\ttce val = 0x%lx\n", tce ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | show_stack(current, (unsigned long *)__get_SP()); |
| 122 | } |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | tcenum++; |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 125 | rpn++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 129 | static DEFINE_PER_CPU(u64 *, tce_page) = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
| 131 | static void tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum, |
| 132 | long npages, unsigned long uaddr, |
| 133 | enum dma_data_direction direction) |
| 134 | { |
| 135 | u64 rc; |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 136 | u64 proto_tce; |
| 137 | u64 *tcep; |
| 138 | u64 rpn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | long l, limit; |
| 140 | |
Paul Mackerras | 6fbb618 | 2005-12-05 14:19:10 +1100 | [diff] [blame] | 141 | if (TCE_PAGE_FACTOR == 0 && npages == 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr, |
| 143 | direction); |
| 144 | |
| 145 | tcep = __get_cpu_var(tce_page); |
| 146 | |
| 147 | /* This is safe to do since interrupts are off when we're called |
| 148 | * from iommu_alloc{,_sg}() |
| 149 | */ |
| 150 | if (!tcep) { |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 151 | tcep = (u64 *)__get_free_page(GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* If allocation fails, fall back to the loop implementation */ |
| 153 | if (!tcep) |
| 154 | return tce_build_pSeriesLP(tbl, tcenum, npages, |
| 155 | uaddr, direction); |
| 156 | __get_cpu_var(tce_page) = tcep; |
| 157 | } |
| 158 | |
Michal Ostrowski | cc8b5c9 | 2005-12-02 13:09:13 +1100 | [diff] [blame] | 159 | tcenum <<= TCE_PAGE_FACTOR; |
| 160 | npages <<= TCE_PAGE_FACTOR; |
| 161 | |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 162 | rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT; |
| 163 | proto_tce = TCE_PCI_READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if (direction != DMA_TO_DEVICE) |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 165 | proto_tce |= TCE_PCI_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
| 167 | /* We can map max one pageful of TCEs at a time */ |
| 168 | do { |
| 169 | /* |
| 170 | * Set up the page with TCE data, looping through and setting |
| 171 | * the values. |
| 172 | */ |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 173 | limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | for (l = 0; l < limit; l++) { |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 176 | tcep[l] = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT; |
| 177 | rpn++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | rc = plpar_tce_put_indirect((u64)tbl->it_index, |
| 181 | (u64)tcenum << 12, |
| 182 | (u64)virt_to_abs(tcep), |
| 183 | limit); |
| 184 | |
| 185 | npages -= limit; |
| 186 | tcenum += limit; |
| 187 | } while (npages > 0 && !rc); |
| 188 | |
| 189 | if (rc && printk_ratelimit()) { |
| 190 | printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc); |
| 191 | printk("\tindex = 0x%lx\n", (u64)tbl->it_index); |
| 192 | printk("\tnpages = 0x%lx\n", (u64)npages); |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 193 | printk("\ttce[0] val = 0x%lx\n", tcep[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | show_stack(current, (unsigned long *)__get_SP()); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages) |
| 199 | { |
| 200 | u64 rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Olof Johansson | d0035c62 | 2005-09-20 13:46:44 +1000 | [diff] [blame] | 202 | tcenum <<= TCE_PAGE_FACTOR; |
| 203 | npages <<= TCE_PAGE_FACTOR; |
| 204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | while (npages--) { |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 206 | rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | |
| 208 | if (rc && printk_ratelimit()) { |
| 209 | printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc); |
| 210 | printk("\tindex = 0x%lx\n", (u64)tbl->it_index); |
| 211 | printk("\ttcenum = 0x%lx\n", (u64)tcenum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | show_stack(current, (unsigned long *)__get_SP()); |
| 213 | } |
| 214 | |
| 215 | tcenum++; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
| 220 | static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages) |
| 221 | { |
| 222 | u64 rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Olof Johansson | d0035c62 | 2005-09-20 13:46:44 +1000 | [diff] [blame] | 224 | tcenum <<= TCE_PAGE_FACTOR; |
| 225 | npages <<= TCE_PAGE_FACTOR; |
| 226 | |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 227 | rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | |
| 229 | if (rc && printk_ratelimit()) { |
| 230 | printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n"); |
| 231 | printk("\trc = %ld\n", rc); |
| 232 | printk("\tindex = 0x%lx\n", (u64)tbl->it_index); |
| 233 | printk("\tnpages = 0x%lx\n", (u64)npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | show_stack(current, (unsigned long *)__get_SP()); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | static void iommu_table_setparms(struct pci_controller *phb, |
| 239 | struct device_node *dn, |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 240 | struct iommu_table *tbl) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | struct device_node *node; |
| 243 | unsigned long *basep; |
| 244 | unsigned int *sizep; |
| 245 | |
| 246 | node = (struct device_node *)phb->arch_data; |
| 247 | |
| 248 | basep = (unsigned long *)get_property(node, "linux,tce-base", NULL); |
| 249 | sizep = (unsigned int *)get_property(node, "linux,tce-size", NULL); |
| 250 | if (basep == NULL || sizep == NULL) { |
| 251 | printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has " |
| 252 | "missing tce entries !\n", dn->full_name); |
| 253 | return; |
| 254 | } |
| 255 | |
| 256 | tbl->it_base = (unsigned long)__va(*basep); |
| 257 | memset((void *)tbl->it_base, 0, *sizep); |
| 258 | |
| 259 | tbl->it_busno = phb->bus->number; |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | /* Units of tce entries */ |
| 262 | tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT; |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | /* Test if we are going over 2GB of DMA space */ |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 265 | if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) { |
| 266 | udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n"); |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 267 | panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n"); |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 268 | } |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 269 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | phb->dma_window_base_cur += phb->dma_window_size; |
| 271 | |
| 272 | /* Set the tce table size - measured in entries */ |
| 273 | tbl->it_size = phb->dma_window_size >> PAGE_SHIFT; |
| 274 | |
| 275 | tbl->it_index = 0; |
| 276 | tbl->it_blocksize = 16; |
| 277 | tbl->it_type = TCE_PCI; |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * iommu_table_setparms_lpar |
| 282 | * |
| 283 | * Function: On pSeries LPAR systems, return TCE table info, given a pci bus. |
| 284 | * |
| 285 | * ToDo: properly interpret the ibm,dma-window property. The definition is: |
| 286 | * logical-bus-number (1 word) |
| 287 | * phys-address (#address-cells words) |
| 288 | * size (#cell-size words) |
| 289 | * |
| 290 | * Currently we hard code these sizes (more or less). |
| 291 | */ |
| 292 | static void iommu_table_setparms_lpar(struct pci_controller *phb, |
| 293 | struct device_node *dn, |
| 294 | struct iommu_table *tbl, |
| 295 | unsigned int *dma_window) |
| 296 | { |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 297 | tbl->it_busno = PCI_DN(dn)->bussubno; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | /* TODO: Parse field size properties properly. */ |
| 300 | tbl->it_size = (((unsigned long)dma_window[4] << 32) | |
| 301 | (unsigned long)dma_window[5]) >> PAGE_SHIFT; |
| 302 | tbl->it_offset = (((unsigned long)dma_window[2] << 32) | |
| 303 | (unsigned long)dma_window[3]) >> PAGE_SHIFT; |
| 304 | tbl->it_base = 0; |
| 305 | tbl->it_index = dma_window[0]; |
| 306 | tbl->it_blocksize = 16; |
| 307 | tbl->it_type = TCE_PCI; |
| 308 | } |
| 309 | |
| 310 | static void iommu_bus_setup_pSeries(struct pci_bus *bus) |
| 311 | { |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 312 | struct device_node *dn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | struct iommu_table *tbl; |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 314 | struct device_node *isa_dn, *isa_dn_orig; |
| 315 | struct device_node *tmp; |
| 316 | struct pci_dn *pci; |
| 317 | int children; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self); |
| 320 | |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 321 | dn = pci_bus_to_OF_node(bus); |
| 322 | pci = PCI_DN(dn); |
| 323 | |
| 324 | if (bus->self) { |
| 325 | /* This is not a root bus, any setup will be done for the |
| 326 | * device-side of the bridge in iommu_dev_setup_pSeries(). |
| 327 | */ |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | /* Check if the ISA bus on the system is under |
| 332 | * this PHB. |
| 333 | */ |
| 334 | isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa"); |
| 335 | |
| 336 | while (isa_dn && isa_dn != dn) |
| 337 | isa_dn = isa_dn->parent; |
| 338 | |
| 339 | if (isa_dn_orig) |
| 340 | of_node_put(isa_dn_orig); |
| 341 | |
| 342 | /* Count number of direct PCI children of the PHB. |
| 343 | * All PCI device nodes have class-code property, so it's |
| 344 | * an easy way to find them. |
| 345 | */ |
| 346 | for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling) |
| 347 | if (get_property(tmp, "class-code", NULL)) |
| 348 | children++; |
| 349 | |
| 350 | DBG("Children: %d\n", children); |
| 351 | |
| 352 | /* Calculate amount of DMA window per slot. Each window must be |
| 353 | * a power of two (due to pci_alloc_consistent requirements). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | * |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 355 | * Keep 256MB aside for PHBs with ISA. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | */ |
| 357 | |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 358 | if (!isa_dn) { |
| 359 | /* No ISA/IDE - just set window size and return */ |
| 360 | pci->phb->dma_window_size = 0x80000000ul; /* To be divided */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 362 | while (pci->phb->dma_window_size * children > 0x80000000ul) |
| 363 | pci->phb->dma_window_size >>= 1; |
Anton Blanchard | f951da3 | 2005-09-22 21:44:05 -0700 | [diff] [blame] | 364 | DBG("No ISA/IDE, window size is 0x%lx\n", |
| 365 | pci->phb->dma_window_size); |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 366 | pci->phb->dma_window_base_cur = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 368 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | } |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 370 | |
| 371 | /* If we have ISA, then we probably have an IDE |
| 372 | * controller too. Allocate a 128MB table but |
| 373 | * skip the first 128MB to avoid stepping on ISA |
| 374 | * space. |
| 375 | */ |
| 376 | pci->phb->dma_window_size = 0x8000000ul; |
| 377 | pci->phb->dma_window_base_cur = 0x8000000ul; |
| 378 | |
| 379 | tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL); |
| 380 | |
| 381 | iommu_table_setparms(pci->phb, dn, tbl); |
| 382 | pci->iommu_table = iommu_init_table(tbl); |
| 383 | |
| 384 | /* Divide the rest (1.75GB) among the children */ |
| 385 | pci->phb->dma_window_size = 0x80000000ul; |
| 386 | while (pci->phb->dma_window_size * children > 0x70000000ul) |
| 387 | pci->phb->dma_window_size >>= 1; |
| 388 | |
Anton Blanchard | f951da3 | 2005-09-22 21:44:05 -0700 | [diff] [blame] | 389 | DBG("ISA/IDE, window size is 0x%lx\n", pci->phb->dma_window_size); |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 390 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | |
| 394 | static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus) |
| 395 | { |
| 396 | struct iommu_table *tbl; |
| 397 | struct device_node *dn, *pdn; |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 398 | struct pci_dn *ppci; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | unsigned int *dma_window = NULL; |
| 400 | |
| 401 | DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self); |
| 402 | |
| 403 | dn = pci_bus_to_OF_node(bus); |
| 404 | |
| 405 | /* Find nearest ibm,dma-window, walking up the device tree */ |
| 406 | for (pdn = dn; pdn != NULL; pdn = pdn->parent) { |
| 407 | dma_window = (unsigned int *)get_property(pdn, "ibm,dma-window", NULL); |
| 408 | if (dma_window != NULL) |
| 409 | break; |
| 410 | } |
| 411 | |
| 412 | if (dma_window == NULL) { |
| 413 | DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name); |
| 414 | return; |
| 415 | } |
| 416 | |
linas | e07102d | 2005-12-05 19:37:35 -0600 | [diff] [blame] | 417 | ppci = PCI_DN(pdn); |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 418 | if (!ppci->iommu_table) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | /* Bussubno hasn't been copied yet. |
| 420 | * Do it now because iommu_table_setparms_lpar needs it. |
| 421 | */ |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 422 | |
| 423 | ppci->bussubno = bus->number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
| 425 | tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table), |
| 426 | GFP_KERNEL); |
Olof Johansson | bc97ce9 | 2006-04-28 22:51:59 -0500 | [diff] [blame^] | 427 | |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 428 | iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 430 | ppci->iommu_table = iommu_init_table(tbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | if (pdn != dn) |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 434 | PCI_DN(dn)->iommu_table = ppci->iommu_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | |
| 438 | static void iommu_dev_setup_pSeries(struct pci_dev *dev) |
| 439 | { |
| 440 | struct device_node *dn, *mydn; |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 441 | struct iommu_table *tbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Anton Blanchard | f951da3 | 2005-09-22 21:44:05 -0700 | [diff] [blame] | 443 | DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, pci_name(dev)); |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | mydn = dn = pci_device_to_OF_node(dev); |
| 446 | |
Olof Johansson | 3c2822c | 2005-09-21 09:55:31 -0700 | [diff] [blame] | 447 | /* If we're the direct child of a root bus, then we need to allocate |
| 448 | * an iommu table ourselves. The bus setup code should have setup |
| 449 | * the window sizes already. |
| 450 | */ |
| 451 | if (!dev->bus->self) { |
| 452 | DBG(" --> first child, no bridge. Allocating iommu table.\n"); |
| 453 | tbl = kmalloc(sizeof(struct iommu_table), GFP_KERNEL); |
| 454 | iommu_table_setparms(PCI_DN(dn)->phb, dn, tbl); |
| 455 | PCI_DN(mydn)->iommu_table = iommu_init_table(tbl); |
| 456 | |
| 457 | return; |
| 458 | } |
| 459 | |
| 460 | /* If this device is further down the bus tree, search upwards until |
| 461 | * an already allocated iommu table is found and use that. |
| 462 | */ |
| 463 | |
linas | e07102d | 2005-12-05 19:37:35 -0600 | [diff] [blame] | 464 | while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | dn = dn->parent; |
| 466 | |
linas | e07102d | 2005-12-05 19:37:35 -0600 | [diff] [blame] | 467 | if (dn && PCI_DN(dn)) { |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 468 | PCI_DN(mydn)->iommu_table = PCI_DN(dn)->iommu_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | } else { |
Anton Blanchard | f951da3 | 2005-09-22 21:44:05 -0700 | [diff] [blame] | 470 | DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, pci_name(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
| 474 | static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node) |
| 475 | { |
| 476 | int err = NOTIFY_OK; |
| 477 | struct device_node *np = node; |
linas | e07102d | 2005-12-05 19:37:35 -0600 | [diff] [blame] | 478 | struct pci_dn *pci = PCI_DN(np); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
| 480 | switch (action) { |
| 481 | case PSERIES_RECONFIG_REMOVE: |
John Rose | 8902e87 | 2005-11-02 10:29:55 -0600 | [diff] [blame] | 482 | if (pci && pci->iommu_table && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | get_property(np, "ibm,dma-window", NULL)) |
| 484 | iommu_free_table(np); |
| 485 | break; |
| 486 | default: |
| 487 | err = NOTIFY_DONE; |
| 488 | break; |
| 489 | } |
| 490 | return err; |
| 491 | } |
| 492 | |
| 493 | static struct notifier_block iommu_reconfig_nb = { |
| 494 | .notifier_call = iommu_reconfig_notifier, |
| 495 | }; |
| 496 | |
| 497 | static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev) |
| 498 | { |
| 499 | struct device_node *pdn, *dn; |
| 500 | struct iommu_table *tbl; |
| 501 | int *dma_window = NULL; |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 502 | struct pci_dn *pci; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | |
Anton Blanchard | f951da3 | 2005-09-22 21:44:05 -0700 | [diff] [blame] | 504 | DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, pci_name(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | /* dev setup for LPAR is a little tricky, since the device tree might |
| 507 | * contain the dma-window properties per-device and not neccesarily |
| 508 | * for the bus. So we need to search upwards in the tree until we |
| 509 | * either hit a dma-window property, OR find a parent with a table |
| 510 | * already allocated. |
| 511 | */ |
| 512 | dn = pci_device_to_OF_node(dev); |
| 513 | |
linas | e07102d | 2005-12-05 19:37:35 -0600 | [diff] [blame] | 514 | for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table; |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 515 | pdn = pdn->parent) { |
| 516 | dma_window = (unsigned int *) |
| 517 | get_property(pdn, "ibm,dma-window", NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | if (dma_window) |
| 519 | break; |
| 520 | } |
| 521 | |
| 522 | /* Check for parent == NULL so we don't try to setup the empty EADS |
| 523 | * slots on POWER4 machines. |
| 524 | */ |
| 525 | if (dma_window == NULL || pdn->parent == NULL) { |
Anton Blanchard | 586a90e | 2005-09-22 21:44:04 -0700 | [diff] [blame] | 526 | DBG("No dma window for device, linking to parent\n"); |
| 527 | PCI_DN(dn)->iommu_table = PCI_DN(pdn)->iommu_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | return; |
| 529 | } else { |
| 530 | DBG("Found DMA window, allocating table\n"); |
| 531 | } |
| 532 | |
linas | e07102d | 2005-12-05 19:37:35 -0600 | [diff] [blame] | 533 | pci = PCI_DN(pdn); |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 534 | if (!pci->iommu_table) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | /* iommu_table_setparms_lpar needs bussubno. */ |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 536 | pci->bussubno = pci->phb->bus->number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
| 538 | tbl = (struct iommu_table *)kmalloc(sizeof(struct iommu_table), |
| 539 | GFP_KERNEL); |
| 540 | |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 541 | iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 543 | pci->iommu_table = iommu_init_table(tbl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | if (pdn != dn) |
Paul Mackerras | 1635317 | 2005-09-06 13:17:54 +1000 | [diff] [blame] | 547 | PCI_DN(dn)->iommu_table = pci->iommu_table; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | static void iommu_bus_setup_null(struct pci_bus *b) { } |
| 551 | static void iommu_dev_setup_null(struct pci_dev *d) { } |
| 552 | |
| 553 | /* These are called very early. */ |
| 554 | void iommu_init_early_pSeries(void) |
| 555 | { |
| 556 | if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) { |
| 557 | /* Direct I/O, IOMMU off */ |
| 558 | ppc_md.iommu_dev_setup = iommu_dev_setup_null; |
| 559 | ppc_md.iommu_bus_setup = iommu_bus_setup_null; |
| 560 | pci_direct_iommu_init(); |
| 561 | |
| 562 | return; |
| 563 | } |
| 564 | |
Michael Ellerman | 57cfb81 | 2006-03-21 20:45:59 +1100 | [diff] [blame] | 565 | if (firmware_has_feature(FW_FEATURE_LPAR)) { |
Stephen Rothwell | 1ababe1 | 2005-08-03 14:35:25 +1000 | [diff] [blame] | 566 | if (firmware_has_feature(FW_FEATURE_MULTITCE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | ppc_md.tce_build = tce_buildmulti_pSeriesLP; |
| 568 | ppc_md.tce_free = tce_freemulti_pSeriesLP; |
| 569 | } else { |
| 570 | ppc_md.tce_build = tce_build_pSeriesLP; |
| 571 | ppc_md.tce_free = tce_free_pSeriesLP; |
| 572 | } |
| 573 | ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP; |
| 574 | ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP; |
| 575 | } else { |
| 576 | ppc_md.tce_build = tce_build_pSeries; |
| 577 | ppc_md.tce_free = tce_free_pSeries; |
| 578 | ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries; |
| 579 | ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | pSeries_reconfig_notifier_register(&iommu_reconfig_nb); |
| 584 | |
| 585 | pci_iommu_init(); |
| 586 | } |
| 587 | |