David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 1 | /* pci_sun4v.c: SUN4V specific PCI controller support. |
| 2 | * |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 3 | * Copyright (C) 2006, 2007, 2008 David S. Miller (davem@davemloft.net) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/pci.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/interrupt.h> |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 12 | #include <linux/percpu.h> |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 13 | #include <linux/irq.h> |
| 14 | #include <linux/msi.h> |
David S. Miller | 59db810 | 2007-05-23 18:00:46 -0700 | [diff] [blame] | 15 | #include <linux/log2.h> |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 16 | #include <linux/of_device.h> |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 17 | |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 18 | #include <asm/iommu.h> |
| 19 | #include <asm/irq.h> |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 20 | #include <asm/hypervisor.h> |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 21 | #include <asm/prom.h> |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 22 | |
| 23 | #include "pci_impl.h" |
| 24 | #include "iommu_common.h" |
| 25 | |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 26 | #include "pci_sun4v.h" |
| 27 | |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 28 | #define DRIVER_NAME "pci_sun4v" |
| 29 | #define PFX DRIVER_NAME ": " |
| 30 | |
David S. Miller | e01c0d6 | 2007-05-25 01:04:15 -0700 | [diff] [blame] | 31 | static unsigned long vpci_major = 1; |
| 32 | static unsigned long vpci_minor = 1; |
| 33 | |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 34 | #define PGLIST_NENTS (PAGE_SIZE / sizeof(u64)) |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 35 | |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 36 | struct iommu_batch { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 37 | struct device *dev; /* Device mapping is for. */ |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 38 | unsigned long prot; /* IOMMU page protections */ |
| 39 | unsigned long entry; /* Index into IOTSB. */ |
| 40 | u64 *pglist; /* List of physical pages */ |
| 41 | unsigned long npages; /* Number of pages in list. */ |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 42 | }; |
| 43 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 44 | static DEFINE_PER_CPU(struct iommu_batch, iommu_batch); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 45 | |
| 46 | /* Interrupts must be disabled. */ |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 47 | static inline void iommu_batch_start(struct device *dev, unsigned long prot, unsigned long entry) |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 48 | { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 49 | struct iommu_batch *p = &__get_cpu_var(iommu_batch); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 50 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 51 | p->dev = dev; |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 52 | p->prot = prot; |
| 53 | p->entry = entry; |
| 54 | p->npages = 0; |
| 55 | } |
| 56 | |
| 57 | /* Interrupts must be disabled. */ |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 58 | static long iommu_batch_flush(struct iommu_batch *p) |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 59 | { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 60 | struct pci_pbm_info *pbm = p->dev->archdata.host_controller; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 61 | unsigned long devhandle = pbm->devhandle; |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 62 | unsigned long prot = p->prot; |
| 63 | unsigned long entry = p->entry; |
| 64 | u64 *pglist = p->pglist; |
| 65 | unsigned long npages = p->npages; |
| 66 | |
David S. Miller | d82965c | 2006-02-20 01:42:51 -0800 | [diff] [blame] | 67 | while (npages != 0) { |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 68 | long num; |
| 69 | |
| 70 | num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry), |
| 71 | npages, prot, __pa(pglist)); |
| 72 | if (unlikely(num < 0)) { |
| 73 | if (printk_ratelimit()) |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 74 | printk("iommu_batch_flush: IOMMU map of " |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 75 | "[%08lx:%08lx:%lx:%lx:%lx] failed with " |
| 76 | "status %ld\n", |
| 77 | devhandle, HV_PCI_TSBID(0, entry), |
| 78 | npages, prot, __pa(pglist), num); |
| 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | entry += num; |
| 83 | npages -= num; |
| 84 | pglist += num; |
David S. Miller | d82965c | 2006-02-20 01:42:51 -0800 | [diff] [blame] | 85 | } |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 86 | |
| 87 | p->entry = entry; |
| 88 | p->npages = 0; |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 93 | static inline void iommu_batch_new_entry(unsigned long entry) |
| 94 | { |
| 95 | struct iommu_batch *p = &__get_cpu_var(iommu_batch); |
| 96 | |
| 97 | if (p->entry + p->npages == entry) |
| 98 | return; |
| 99 | if (p->entry != ~0UL) |
| 100 | iommu_batch_flush(p); |
| 101 | p->entry = entry; |
| 102 | } |
| 103 | |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 104 | /* Interrupts must be disabled. */ |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 105 | static inline long iommu_batch_add(u64 phys_page) |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 106 | { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 107 | struct iommu_batch *p = &__get_cpu_var(iommu_batch); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 108 | |
| 109 | BUG_ON(p->npages >= PGLIST_NENTS); |
| 110 | |
| 111 | p->pglist[p->npages++] = phys_page; |
| 112 | if (p->npages == PGLIST_NENTS) |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 113 | return iommu_batch_flush(p); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | /* Interrupts must be disabled. */ |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 119 | static inline long iommu_batch_end(void) |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 120 | { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 121 | struct iommu_batch *p = &__get_cpu_var(iommu_batch); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 122 | |
| 123 | BUG_ON(p->npages >= PGLIST_NENTS); |
| 124 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 125 | return iommu_batch_flush(p); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 126 | } |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 127 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 128 | static void *dma_4v_alloc_coherent(struct device *dev, size_t size, |
| 129 | dma_addr_t *dma_addrp, gfp_t gfp) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 130 | { |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 131 | unsigned long flags, order, first_page, npages, n; |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 132 | struct iommu *iommu; |
| 133 | struct page *page; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 134 | void *ret; |
| 135 | long entry; |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 136 | int nid; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 137 | |
| 138 | size = IO_PAGE_ALIGN(size); |
| 139 | order = get_order(size); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 140 | if (unlikely(order >= MAX_ORDER)) |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 141 | return NULL; |
| 142 | |
| 143 | npages = size >> IO_PAGE_SHIFT; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 144 | |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 145 | nid = dev->archdata.numa_node; |
| 146 | page = alloc_pages_node(nid, gfp, order); |
| 147 | if (unlikely(!page)) |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 148 | return NULL; |
David S. Miller | e7a0453 | 2006-02-15 22:25:27 -0800 | [diff] [blame] | 149 | |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 150 | first_page = (unsigned long) page_address(page); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 151 | memset((char *)first_page, 0, PAGE_SIZE << order); |
| 152 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 153 | iommu = dev->archdata.iommu; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 154 | |
| 155 | spin_lock_irqsave(&iommu->lock, flags); |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 156 | entry = iommu_range_alloc(dev, iommu, npages, NULL); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 157 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 158 | |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 159 | if (unlikely(entry == DMA_ERROR_CODE)) |
| 160 | goto range_alloc_fail; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 161 | |
| 162 | *dma_addrp = (iommu->page_table_map_base + |
| 163 | (entry << IO_PAGE_SHIFT)); |
| 164 | ret = (void *) first_page; |
| 165 | first_page = __pa(first_page); |
| 166 | |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 167 | local_irq_save(flags); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 168 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 169 | iommu_batch_start(dev, |
| 170 | (HV_PCI_MAP_ATTR_READ | |
| 171 | HV_PCI_MAP_ATTR_WRITE), |
| 172 | entry); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 173 | |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 174 | for (n = 0; n < npages; n++) { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 175 | long err = iommu_batch_add(first_page + (n * PAGE_SIZE)); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 176 | if (unlikely(err < 0L)) |
| 177 | goto iommu_map_fail; |
| 178 | } |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 179 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 180 | if (unlikely(iommu_batch_end() < 0L)) |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 181 | goto iommu_map_fail; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 182 | |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 183 | local_irq_restore(flags); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 184 | |
| 185 | return ret; |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 186 | |
| 187 | iommu_map_fail: |
| 188 | /* Interrupts are disabled. */ |
| 189 | spin_lock(&iommu->lock); |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 190 | iommu_range_free(iommu, *dma_addrp, npages); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 191 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 192 | |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 193 | range_alloc_fail: |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 194 | free_pages(first_page, order); |
| 195 | return NULL; |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 196 | } |
| 197 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 198 | static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu, |
| 199 | dma_addr_t dvma) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 200 | { |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 201 | struct pci_pbm_info *pbm; |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 202 | struct iommu *iommu; |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 203 | unsigned long flags, order, npages, entry; |
| 204 | u32 devhandle; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 205 | |
| 206 | npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT; |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 207 | iommu = dev->archdata.iommu; |
| 208 | pbm = dev->archdata.host_controller; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 209 | devhandle = pbm->devhandle; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 210 | entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT); |
| 211 | |
| 212 | spin_lock_irqsave(&iommu->lock, flags); |
| 213 | |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 214 | iommu_range_free(iommu, dvma, npages); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 215 | |
| 216 | do { |
| 217 | unsigned long num; |
| 218 | |
| 219 | num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry), |
| 220 | npages); |
| 221 | entry += num; |
| 222 | npages -= num; |
| 223 | } while (npages != 0); |
| 224 | |
| 225 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 226 | |
| 227 | order = get_order(size); |
| 228 | if (order < 10) |
| 229 | free_pages((unsigned long)cpu, order); |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 230 | } |
| 231 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 232 | static dma_addr_t dma_4v_map_single(struct device *dev, void *ptr, size_t sz, |
| 233 | enum dma_data_direction direction) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 234 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 235 | struct iommu *iommu; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 236 | unsigned long flags, npages, oaddr; |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 237 | unsigned long i, base_paddr; |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 238 | u32 bus_addr, ret; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 239 | unsigned long prot; |
| 240 | long entry; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 241 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 242 | iommu = dev->archdata.iommu; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 243 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 244 | if (unlikely(direction == DMA_NONE)) |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 245 | goto bad; |
| 246 | |
| 247 | oaddr = (unsigned long)ptr; |
| 248 | npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK); |
| 249 | npages >>= IO_PAGE_SHIFT; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 250 | |
| 251 | spin_lock_irqsave(&iommu->lock, flags); |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 252 | entry = iommu_range_alloc(dev, iommu, npages, NULL); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 253 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 254 | |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 255 | if (unlikely(entry == DMA_ERROR_CODE)) |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 256 | goto bad; |
| 257 | |
| 258 | bus_addr = (iommu->page_table_map_base + |
| 259 | (entry << IO_PAGE_SHIFT)); |
| 260 | ret = bus_addr | (oaddr & ~IO_PAGE_MASK); |
| 261 | base_paddr = __pa(oaddr & IO_PAGE_MASK); |
| 262 | prot = HV_PCI_MAP_ATTR_READ; |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 263 | if (direction != DMA_TO_DEVICE) |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 264 | prot |= HV_PCI_MAP_ATTR_WRITE; |
| 265 | |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 266 | local_irq_save(flags); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 267 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 268 | iommu_batch_start(dev, prot, entry); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 269 | |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 270 | for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 271 | long err = iommu_batch_add(base_paddr); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 272 | if (unlikely(err < 0L)) |
| 273 | goto iommu_map_fail; |
| 274 | } |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 275 | if (unlikely(iommu_batch_end() < 0L)) |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 276 | goto iommu_map_fail; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 277 | |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 278 | local_irq_restore(flags); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 279 | |
| 280 | return ret; |
| 281 | |
| 282 | bad: |
| 283 | if (printk_ratelimit()) |
| 284 | WARN_ON(1); |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 285 | return DMA_ERROR_CODE; |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 286 | |
| 287 | iommu_map_fail: |
| 288 | /* Interrupts are disabled. */ |
| 289 | spin_lock(&iommu->lock); |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 290 | iommu_range_free(iommu, bus_addr, npages); |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 291 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 292 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 293 | return DMA_ERROR_CODE; |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 294 | } |
| 295 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 296 | static void dma_4v_unmap_single(struct device *dev, dma_addr_t bus_addr, |
| 297 | size_t sz, enum dma_data_direction direction) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 298 | { |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 299 | struct pci_pbm_info *pbm; |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 300 | struct iommu *iommu; |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 301 | unsigned long flags, npages; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 302 | long entry; |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 303 | u32 devhandle; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 304 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 305 | if (unlikely(direction == DMA_NONE)) { |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 306 | if (printk_ratelimit()) |
| 307 | WARN_ON(1); |
| 308 | return; |
| 309 | } |
| 310 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 311 | iommu = dev->archdata.iommu; |
| 312 | pbm = dev->archdata.host_controller; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 313 | devhandle = pbm->devhandle; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 314 | |
| 315 | npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK); |
| 316 | npages >>= IO_PAGE_SHIFT; |
| 317 | bus_addr &= IO_PAGE_MASK; |
| 318 | |
| 319 | spin_lock_irqsave(&iommu->lock, flags); |
| 320 | |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 321 | iommu_range_free(iommu, bus_addr, npages); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 322 | |
David S. Miller | d284142 | 2008-02-08 18:05:46 -0800 | [diff] [blame] | 323 | entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 324 | do { |
| 325 | unsigned long num; |
| 326 | |
| 327 | num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry), |
| 328 | npages); |
| 329 | entry += num; |
| 330 | npages -= num; |
| 331 | } while (npages != 0); |
| 332 | |
| 333 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 334 | } |
| 335 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 336 | static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist, |
| 337 | int nelems, enum dma_data_direction direction) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 338 | { |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 339 | struct scatterlist *s, *outs, *segstart; |
| 340 | unsigned long flags, handle, prot; |
| 341 | dma_addr_t dma_next = 0, dma_addr; |
| 342 | unsigned int max_seg_size; |
FUJITA Tomonori | f088025 | 2008-03-28 15:55:41 -0700 | [diff] [blame] | 343 | unsigned long seg_boundary_size; |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 344 | int outcount, incount, i; |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 345 | struct iommu *iommu; |
FUJITA Tomonori | f088025 | 2008-03-28 15:55:41 -0700 | [diff] [blame] | 346 | unsigned long base_shift; |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 347 | long err; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 348 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 349 | BUG_ON(direction == DMA_NONE); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 350 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 351 | iommu = dev->archdata.iommu; |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 352 | if (nelems == 0 || !iommu) |
| 353 | return 0; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 354 | |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 355 | prot = HV_PCI_MAP_ATTR_READ; |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 356 | if (direction != DMA_TO_DEVICE) |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 357 | prot |= HV_PCI_MAP_ATTR_WRITE; |
| 358 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 359 | outs = s = segstart = &sglist[0]; |
| 360 | outcount = 1; |
| 361 | incount = nelems; |
| 362 | handle = 0; |
David S. Miller | 38192d5 | 2008-02-06 03:50:26 -0800 | [diff] [blame] | 363 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 364 | /* Init first segment length for backout at failure */ |
| 365 | outs->dma_length = 0; |
David S. Miller | 38192d5 | 2008-02-06 03:50:26 -0800 | [diff] [blame] | 366 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 367 | spin_lock_irqsave(&iommu->lock, flags); |
David S. Miller | 38192d5 | 2008-02-06 03:50:26 -0800 | [diff] [blame] | 368 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 369 | iommu_batch_start(dev, prot, ~0UL); |
David S. Miller | 38192d5 | 2008-02-06 03:50:26 -0800 | [diff] [blame] | 370 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 371 | max_seg_size = dma_get_max_seg_size(dev); |
FUJITA Tomonori | f088025 | 2008-03-28 15:55:41 -0700 | [diff] [blame] | 372 | seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1, |
| 373 | IO_PAGE_SIZE) >> IO_PAGE_SHIFT; |
| 374 | base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT; |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 375 | for_each_sg(sglist, s, nelems, i) { |
FUJITA Tomonori | f088025 | 2008-03-28 15:55:41 -0700 | [diff] [blame] | 376 | unsigned long paddr, npages, entry, out_entry = 0, slen; |
David S. Miller | 38192d5 | 2008-02-06 03:50:26 -0800 | [diff] [blame] | 377 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 378 | slen = s->length; |
| 379 | /* Sanity check */ |
| 380 | if (slen == 0) { |
| 381 | dma_next = 0; |
| 382 | continue; |
David S. Miller | 38192d5 | 2008-02-06 03:50:26 -0800 | [diff] [blame] | 383 | } |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 384 | /* Allocate iommu entries for that segment */ |
| 385 | paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s); |
| 386 | npages = iommu_num_pages(paddr, slen); |
| 387 | entry = iommu_range_alloc(dev, iommu, npages, &handle); |
| 388 | |
| 389 | /* Handle failure */ |
| 390 | if (unlikely(entry == DMA_ERROR_CODE)) { |
| 391 | if (printk_ratelimit()) |
| 392 | printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx" |
| 393 | " npages %lx\n", iommu, paddr, npages); |
| 394 | goto iommu_map_failed; |
| 395 | } |
| 396 | |
| 397 | iommu_batch_new_entry(entry); |
| 398 | |
| 399 | /* Convert entry to a dma_addr_t */ |
| 400 | dma_addr = iommu->page_table_map_base + |
| 401 | (entry << IO_PAGE_SHIFT); |
| 402 | dma_addr |= (s->offset & ~IO_PAGE_MASK); |
| 403 | |
| 404 | /* Insert into HW table */ |
| 405 | paddr &= IO_PAGE_MASK; |
| 406 | while (npages--) { |
| 407 | err = iommu_batch_add(paddr); |
| 408 | if (unlikely(err < 0L)) |
| 409 | goto iommu_map_failed; |
| 410 | paddr += IO_PAGE_SIZE; |
| 411 | } |
| 412 | |
| 413 | /* If we are in an open segment, try merging */ |
| 414 | if (segstart != s) { |
| 415 | /* We cannot merge if: |
| 416 | * - allocated dma_addr isn't contiguous to previous allocation |
| 417 | */ |
| 418 | if ((dma_addr != dma_next) || |
FUJITA Tomonori | f088025 | 2008-03-28 15:55:41 -0700 | [diff] [blame] | 419 | (outs->dma_length + s->length > max_seg_size) || |
| 420 | (is_span_boundary(out_entry, base_shift, |
| 421 | seg_boundary_size, outs, s))) { |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 422 | /* Can't merge: create a new segment */ |
| 423 | segstart = s; |
| 424 | outcount++; |
| 425 | outs = sg_next(outs); |
| 426 | } else { |
| 427 | outs->dma_length += s->length; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if (segstart == s) { |
| 432 | /* This is a new segment, fill entries */ |
| 433 | outs->dma_address = dma_addr; |
| 434 | outs->dma_length = slen; |
FUJITA Tomonori | f088025 | 2008-03-28 15:55:41 -0700 | [diff] [blame] | 435 | out_entry = entry; |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /* Calculate next page pointer for contiguous check */ |
| 439 | dma_next = dma_addr + slen; |
David S. Miller | 38192d5 | 2008-02-06 03:50:26 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | err = iommu_batch_end(); |
| 443 | |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 444 | if (unlikely(err < 0L)) |
| 445 | goto iommu_map_failed; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 446 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 447 | spin_unlock_irqrestore(&iommu->lock, flags); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 448 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 449 | if (outcount < incount) { |
| 450 | outs = sg_next(outs); |
| 451 | outs->dma_address = DMA_ERROR_CODE; |
| 452 | outs->dma_length = 0; |
| 453 | } |
| 454 | |
| 455 | return outcount; |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 456 | |
| 457 | iommu_map_failed: |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 458 | for_each_sg(sglist, s, nelems, i) { |
| 459 | if (s->dma_length != 0) { |
| 460 | unsigned long vaddr, npages; |
| 461 | |
| 462 | vaddr = s->dma_address & IO_PAGE_MASK; |
| 463 | npages = iommu_num_pages(s->dma_address, s->dma_length); |
| 464 | iommu_range_free(iommu, vaddr, npages); |
| 465 | /* XXX demap? XXX */ |
| 466 | s->dma_address = DMA_ERROR_CODE; |
| 467 | s->dma_length = 0; |
| 468 | } |
| 469 | if (s == outs) |
| 470 | break; |
| 471 | } |
David S. Miller | 6a32fd4 | 2006-02-19 22:21:32 -0800 | [diff] [blame] | 472 | spin_unlock_irqrestore(&iommu->lock, flags); |
| 473 | |
| 474 | return 0; |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 475 | } |
| 476 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 477 | static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist, |
| 478 | int nelems, enum dma_data_direction direction) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 479 | { |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 480 | struct pci_pbm_info *pbm; |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 481 | struct scatterlist *sg; |
David S. Miller | 38192d5 | 2008-02-06 03:50:26 -0800 | [diff] [blame] | 482 | struct iommu *iommu; |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 483 | unsigned long flags; |
| 484 | u32 devhandle; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 485 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 486 | BUG_ON(direction == DMA_NONE); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 487 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 488 | iommu = dev->archdata.iommu; |
| 489 | pbm = dev->archdata.host_controller; |
David S. Miller | a2fb23a | 2007-02-28 23:35:04 -0800 | [diff] [blame] | 490 | devhandle = pbm->devhandle; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 491 | |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 492 | spin_lock_irqsave(&iommu->lock, flags); |
| 493 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 494 | sg = sglist; |
| 495 | while (nelems--) { |
| 496 | dma_addr_t dma_handle = sg->dma_address; |
| 497 | unsigned int len = sg->dma_length; |
| 498 | unsigned long npages, entry; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 499 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 500 | if (!len) |
| 501 | break; |
| 502 | npages = iommu_num_pages(dma_handle, len); |
| 503 | iommu_range_free(iommu, dma_handle, npages); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 504 | |
David S. Miller | 13fa14e | 2008-02-09 03:11:01 -0800 | [diff] [blame] | 505 | entry = ((dma_handle - iommu->page_table_map_base) >> IO_PAGE_SHIFT); |
| 506 | while (npages) { |
| 507 | unsigned long num; |
| 508 | |
| 509 | num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry), |
| 510 | npages); |
| 511 | entry += num; |
| 512 | npages -= num; |
| 513 | } |
| 514 | |
| 515 | sg = sg_next(sg); |
| 516 | } |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 517 | |
| 518 | spin_unlock_irqrestore(&iommu->lock, flags); |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 519 | } |
| 520 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 521 | static void dma_4v_sync_single_for_cpu(struct device *dev, |
| 522 | dma_addr_t bus_addr, size_t sz, |
| 523 | enum dma_data_direction direction) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 524 | { |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 525 | /* Nothing to do... */ |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 526 | } |
| 527 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 528 | static void dma_4v_sync_sg_for_cpu(struct device *dev, |
| 529 | struct scatterlist *sglist, int nelems, |
| 530 | enum dma_data_direction direction) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 531 | { |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 532 | /* Nothing to do... */ |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Adrian Bunk | 908f516 | 2008-06-05 11:42:40 -0700 | [diff] [blame] | 535 | static const struct dma_ops sun4v_dma_ops = { |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 536 | .alloc_coherent = dma_4v_alloc_coherent, |
| 537 | .free_coherent = dma_4v_free_coherent, |
| 538 | .map_single = dma_4v_map_single, |
| 539 | .unmap_single = dma_4v_unmap_single, |
| 540 | .map_sg = dma_4v_map_sg, |
| 541 | .unmap_sg = dma_4v_unmap_sg, |
| 542 | .sync_single_for_cpu = dma_4v_sync_single_for_cpu, |
| 543 | .sync_sg_for_cpu = dma_4v_sync_sg_for_cpu, |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 544 | }; |
| 545 | |
Sam Ravnborg | a1f35ba | 2008-01-21 17:22:46 -0800 | [diff] [blame] | 546 | static void __init pci_sun4v_scan_bus(struct pci_pbm_info *pbm) |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 547 | { |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 548 | struct property *prop; |
| 549 | struct device_node *dp; |
| 550 | |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 551 | dp = pbm->prom_node; |
| 552 | prop = of_find_property(dp, "66mhz-capable", NULL); |
| 553 | pbm->is_66mhz_capable = (prop != NULL); |
| 554 | pbm->pci_bus = pci_scan_one_pbm(pbm); |
David S. Miller | c260926 | 2006-02-12 22:18:52 -0800 | [diff] [blame] | 555 | |
| 556 | /* XXX register error interrupt handlers XXX */ |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 557 | } |
| 558 | |
Adrian Bunk | 4c62225 | 2008-02-05 03:01:43 -0800 | [diff] [blame] | 559 | static unsigned long __init probe_existing_entries(struct pci_pbm_info *pbm, |
| 560 | struct iommu *iommu) |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 561 | { |
David S. Miller | 9b3627f | 2007-04-24 23:51:18 -0700 | [diff] [blame] | 562 | struct iommu_arena *arena = &iommu->arena; |
David S. Miller | e7a0453 | 2006-02-15 22:25:27 -0800 | [diff] [blame] | 563 | unsigned long i, cnt = 0; |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 564 | u32 devhandle; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 565 | |
| 566 | devhandle = pbm->devhandle; |
| 567 | for (i = 0; i < arena->limit; i++) { |
| 568 | unsigned long ret, io_attrs, ra; |
| 569 | |
| 570 | ret = pci_sun4v_iommu_getmap(devhandle, |
| 571 | HV_PCI_TSBID(0, i), |
| 572 | &io_attrs, &ra); |
David S. Miller | e7a0453 | 2006-02-15 22:25:27 -0800 | [diff] [blame] | 573 | if (ret == HV_EOK) { |
David S. Miller | c2a5a46 | 2006-06-22 00:01:56 -0700 | [diff] [blame] | 574 | if (page_in_phys_avail(ra)) { |
| 575 | pci_sun4v_iommu_demap(devhandle, |
| 576 | HV_PCI_TSBID(0, i), 1); |
| 577 | } else { |
| 578 | cnt++; |
| 579 | __set_bit(i, arena->map); |
| 580 | } |
David S. Miller | e7a0453 | 2006-02-15 22:25:27 -0800 | [diff] [blame] | 581 | } |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 582 | } |
David S. Miller | e7a0453 | 2006-02-15 22:25:27 -0800 | [diff] [blame] | 583 | |
| 584 | return cnt; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 585 | } |
| 586 | |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 587 | static int __init pci_sun4v_iommu_init(struct pci_pbm_info *pbm) |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 588 | { |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 589 | struct iommu *iommu = pbm->iommu; |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 590 | struct property *prop; |
David S. Miller | 59db810 | 2007-05-23 18:00:46 -0700 | [diff] [blame] | 591 | unsigned long num_tsb_entries, sz, tsbsize; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 592 | u32 vdma[2], dma_mask, dma_offset; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 593 | |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 594 | prop = of_find_property(pbm->prom_node, "virtual-dma", NULL); |
| 595 | if (prop) { |
| 596 | u32 *val = prop->value; |
| 597 | |
| 598 | vdma[0] = val[0]; |
| 599 | vdma[1] = val[1]; |
| 600 | } else { |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 601 | /* No property, use default values. */ |
| 602 | vdma[0] = 0x80000000; |
| 603 | vdma[1] = 0x80000000; |
| 604 | } |
| 605 | |
David S. Miller | 59db810 | 2007-05-23 18:00:46 -0700 | [diff] [blame] | 606 | if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) { |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 607 | printk(KERN_ERR PFX "Strange virtual-dma[%08x:%08x].\n", |
| 608 | vdma[0], vdma[1]); |
| 609 | return -EINVAL; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 610 | }; |
| 611 | |
David S. Miller | 59db810 | 2007-05-23 18:00:46 -0700 | [diff] [blame] | 612 | dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL); |
| 613 | num_tsb_entries = vdma[1] / IO_PAGE_SIZE; |
| 614 | tsbsize = num_tsb_entries * sizeof(iopte_t); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 615 | |
| 616 | dma_offset = vdma[0]; |
| 617 | |
| 618 | /* Setup initial software IOMMU state. */ |
| 619 | spin_lock_init(&iommu->lock); |
| 620 | iommu->ctx_lowest_free = 1; |
| 621 | iommu->page_table_map_base = dma_offset; |
| 622 | iommu->dma_addr_mask = dma_mask; |
| 623 | |
| 624 | /* Allocate and initialize the free area map. */ |
David S. Miller | 59db810 | 2007-05-23 18:00:46 -0700 | [diff] [blame] | 625 | sz = (num_tsb_entries + 7) / 8; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 626 | sz = (sz + 7UL) & ~7UL; |
Yan Burman | 982c206 | 2006-11-30 17:13:09 -0800 | [diff] [blame] | 627 | iommu->arena.map = kzalloc(sz, GFP_KERNEL); |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 628 | if (!iommu->arena.map) { |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 629 | printk(KERN_ERR PFX "Error, kmalloc(arena.map) failed.\n"); |
| 630 | return -ENOMEM; |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 631 | } |
David S. Miller | 1839794 | 2006-02-10 00:08:26 -0800 | [diff] [blame] | 632 | iommu->arena.limit = num_tsb_entries; |
| 633 | |
David S. Miller | e7a0453 | 2006-02-15 22:25:27 -0800 | [diff] [blame] | 634 | sz = probe_existing_entries(pbm, iommu); |
David S. Miller | c2a5a46 | 2006-06-22 00:01:56 -0700 | [diff] [blame] | 635 | if (sz) |
| 636 | printk("%s: Imported %lu TSB entries from OBP\n", |
| 637 | pbm->name, sz); |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 638 | |
| 639 | return 0; |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 640 | } |
| 641 | |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 642 | #ifdef CONFIG_PCI_MSI |
| 643 | struct pci_sun4v_msiq_entry { |
| 644 | u64 version_type; |
| 645 | #define MSIQ_VERSION_MASK 0xffffffff00000000UL |
| 646 | #define MSIQ_VERSION_SHIFT 32 |
| 647 | #define MSIQ_TYPE_MASK 0x00000000000000ffUL |
| 648 | #define MSIQ_TYPE_SHIFT 0 |
| 649 | #define MSIQ_TYPE_NONE 0x00 |
| 650 | #define MSIQ_TYPE_MSG 0x01 |
| 651 | #define MSIQ_TYPE_MSI32 0x02 |
| 652 | #define MSIQ_TYPE_MSI64 0x03 |
| 653 | #define MSIQ_TYPE_INTX 0x08 |
| 654 | #define MSIQ_TYPE_NONE2 0xff |
| 655 | |
| 656 | u64 intx_sysino; |
| 657 | u64 reserved1; |
| 658 | u64 stick; |
| 659 | u64 req_id; /* bus/device/func */ |
| 660 | #define MSIQ_REQID_BUS_MASK 0xff00UL |
| 661 | #define MSIQ_REQID_BUS_SHIFT 8 |
| 662 | #define MSIQ_REQID_DEVICE_MASK 0x00f8UL |
| 663 | #define MSIQ_REQID_DEVICE_SHIFT 3 |
| 664 | #define MSIQ_REQID_FUNC_MASK 0x0007UL |
| 665 | #define MSIQ_REQID_FUNC_SHIFT 0 |
| 666 | |
| 667 | u64 msi_address; |
| 668 | |
Simon Arlott | e5dd42e | 2007-05-11 13:52:08 -0700 | [diff] [blame] | 669 | /* The format of this value is message type dependent. |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 670 | * For MSI bits 15:0 are the data from the MSI packet. |
| 671 | * For MSI-X bits 31:0 are the data from the MSI packet. |
| 672 | * For MSG, the message code and message routing code where: |
| 673 | * bits 39:32 is the bus/device/fn of the msg target-id |
| 674 | * bits 18:16 is the message routing code |
| 675 | * bits 7:0 is the message code |
| 676 | * For INTx the low order 2-bits are: |
| 677 | * 00 - INTA |
| 678 | * 01 - INTB |
| 679 | * 10 - INTC |
| 680 | * 11 - INTD |
| 681 | */ |
| 682 | u64 msi_data; |
| 683 | |
| 684 | u64 reserved2; |
| 685 | }; |
| 686 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 687 | static int pci_sun4v_get_head(struct pci_pbm_info *pbm, unsigned long msiqid, |
| 688 | unsigned long *head) |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 689 | { |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 690 | unsigned long err, limit; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 691 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 692 | err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, head); |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 693 | if (unlikely(err)) |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 694 | return -ENXIO; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 695 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 696 | limit = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry); |
| 697 | if (unlikely(*head >= limit)) |
| 698 | return -EFBIG; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 703 | static int pci_sun4v_dequeue_msi(struct pci_pbm_info *pbm, |
| 704 | unsigned long msiqid, unsigned long *head, |
| 705 | unsigned long *msi) |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 706 | { |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 707 | struct pci_sun4v_msiq_entry *ep; |
| 708 | unsigned long err, type; |
| 709 | |
| 710 | /* Note: void pointer arithmetic, 'head' is a byte offset */ |
| 711 | ep = (pbm->msi_queues + ((msiqid - pbm->msiq_first) * |
| 712 | (pbm->msiq_ent_count * |
| 713 | sizeof(struct pci_sun4v_msiq_entry))) + |
| 714 | *head); |
| 715 | |
| 716 | if ((ep->version_type & MSIQ_TYPE_MASK) == 0) |
| 717 | return 0; |
| 718 | |
| 719 | type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT; |
| 720 | if (unlikely(type != MSIQ_TYPE_MSI32 && |
| 721 | type != MSIQ_TYPE_MSI64)) |
| 722 | return -EINVAL; |
| 723 | |
| 724 | *msi = ep->msi_data; |
| 725 | |
| 726 | err = pci_sun4v_msi_setstate(pbm->devhandle, |
| 727 | ep->msi_data /* msi_num */, |
| 728 | HV_MSISTATE_IDLE); |
| 729 | if (unlikely(err)) |
| 730 | return -ENXIO; |
| 731 | |
| 732 | /* Clear the entry. */ |
| 733 | ep->version_type &= ~MSIQ_TYPE_MASK; |
| 734 | |
| 735 | (*head) += sizeof(struct pci_sun4v_msiq_entry); |
| 736 | if (*head >= |
| 737 | (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry))) |
| 738 | *head = 0; |
| 739 | |
| 740 | return 1; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 741 | } |
| 742 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 743 | static int pci_sun4v_set_head(struct pci_pbm_info *pbm, unsigned long msiqid, |
| 744 | unsigned long head) |
| 745 | { |
| 746 | unsigned long err; |
| 747 | |
| 748 | err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head); |
| 749 | if (unlikely(err)) |
| 750 | return -EINVAL; |
| 751 | |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | static int pci_sun4v_msi_setup(struct pci_pbm_info *pbm, unsigned long msiqid, |
| 756 | unsigned long msi, int is_msi64) |
| 757 | { |
| 758 | if (pci_sun4v_msi_setmsiq(pbm->devhandle, msi, msiqid, |
| 759 | (is_msi64 ? |
| 760 | HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32))) |
| 761 | return -ENXIO; |
| 762 | if (pci_sun4v_msi_setstate(pbm->devhandle, msi, HV_MSISTATE_IDLE)) |
| 763 | return -ENXIO; |
| 764 | if (pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_VALID)) |
| 765 | return -ENXIO; |
| 766 | return 0; |
| 767 | } |
| 768 | |
| 769 | static int pci_sun4v_msi_teardown(struct pci_pbm_info *pbm, unsigned long msi) |
| 770 | { |
| 771 | unsigned long err, msiqid; |
| 772 | |
| 773 | err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi, &msiqid); |
| 774 | if (err) |
| 775 | return -ENXIO; |
| 776 | |
| 777 | pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_INVALID); |
| 778 | |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | static int pci_sun4v_msiq_alloc(struct pci_pbm_info *pbm) |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 783 | { |
| 784 | unsigned long q_size, alloc_size, pages, order; |
| 785 | int i; |
| 786 | |
| 787 | q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry); |
| 788 | alloc_size = (pbm->msiq_num * q_size); |
| 789 | order = get_order(alloc_size); |
| 790 | pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order); |
| 791 | if (pages == 0UL) { |
| 792 | printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n", |
| 793 | order); |
| 794 | return -ENOMEM; |
| 795 | } |
| 796 | memset((char *)pages, 0, PAGE_SIZE << order); |
| 797 | pbm->msi_queues = (void *) pages; |
| 798 | |
| 799 | for (i = 0; i < pbm->msiq_num; i++) { |
| 800 | unsigned long err, base = __pa(pages + (i * q_size)); |
| 801 | unsigned long ret1, ret2; |
| 802 | |
| 803 | err = pci_sun4v_msiq_conf(pbm->devhandle, |
| 804 | pbm->msiq_first + i, |
| 805 | base, pbm->msiq_ent_count); |
| 806 | if (err) { |
| 807 | printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n", |
| 808 | err); |
| 809 | goto h_error; |
| 810 | } |
| 811 | |
| 812 | err = pci_sun4v_msiq_info(pbm->devhandle, |
| 813 | pbm->msiq_first + i, |
| 814 | &ret1, &ret2); |
| 815 | if (err) { |
| 816 | printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n", |
| 817 | err); |
| 818 | goto h_error; |
| 819 | } |
| 820 | if (ret1 != base || ret2 != pbm->msiq_ent_count) { |
| 821 | printk(KERN_ERR "MSI: Bogus qconf " |
| 822 | "expected[%lx:%x] got[%lx:%lx]\n", |
| 823 | base, pbm->msiq_ent_count, |
| 824 | ret1, ret2); |
| 825 | goto h_error; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | return 0; |
| 830 | |
| 831 | h_error: |
| 832 | free_pages(pages, order); |
| 833 | return -EINVAL; |
| 834 | } |
| 835 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 836 | static void pci_sun4v_msiq_free(struct pci_pbm_info *pbm) |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 837 | { |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 838 | unsigned long q_size, alloc_size, pages, order; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 839 | int i; |
| 840 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 841 | for (i = 0; i < pbm->msiq_num; i++) { |
| 842 | unsigned long msiqid = pbm->msiq_first + i; |
| 843 | |
| 844 | (void) pci_sun4v_msiq_conf(pbm->devhandle, msiqid, 0UL, 0); |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 845 | } |
| 846 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 847 | q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry); |
| 848 | alloc_size = (pbm->msiq_num * q_size); |
| 849 | order = get_order(alloc_size); |
| 850 | |
| 851 | pages = (unsigned long) pbm->msi_queues; |
| 852 | |
| 853 | free_pages(pages, order); |
| 854 | |
| 855 | pbm->msi_queues = NULL; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 856 | } |
| 857 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 858 | static int pci_sun4v_msiq_build_irq(struct pci_pbm_info *pbm, |
| 859 | unsigned long msiqid, |
| 860 | unsigned long devino) |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 861 | { |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 862 | unsigned int virt_irq = sun4v_build_irq(pbm->devhandle, devino); |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 863 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 864 | if (!virt_irq) |
| 865 | return -ENOMEM; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 866 | |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 867 | if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE)) |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 868 | return -EINVAL; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 869 | if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID)) |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 870 | return -EINVAL; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 871 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 872 | return virt_irq; |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 873 | } |
| 874 | |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 875 | static const struct sparc64_msiq_ops pci_sun4v_msiq_ops = { |
| 876 | .get_head = pci_sun4v_get_head, |
| 877 | .dequeue_msi = pci_sun4v_dequeue_msi, |
| 878 | .set_head = pci_sun4v_set_head, |
| 879 | .msi_setup = pci_sun4v_msi_setup, |
| 880 | .msi_teardown = pci_sun4v_msi_teardown, |
| 881 | .msiq_alloc = pci_sun4v_msiq_alloc, |
| 882 | .msiq_free = pci_sun4v_msiq_free, |
| 883 | .msiq_build_irq = pci_sun4v_msiq_build_irq, |
| 884 | }; |
David S. Miller | e9870c4 | 2007-05-07 23:28:50 -0700 | [diff] [blame] | 885 | |
| 886 | static void pci_sun4v_msi_init(struct pci_pbm_info *pbm) |
| 887 | { |
David S. Miller | 759f89e | 2007-10-11 03:16:13 -0700 | [diff] [blame] | 888 | sparc64_pbm_msi_init(pbm, &pci_sun4v_msiq_ops); |
David S. Miller | e9870c4 | 2007-05-07 23:28:50 -0700 | [diff] [blame] | 889 | } |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 890 | #else /* CONFIG_PCI_MSI */ |
| 891 | static void pci_sun4v_msi_init(struct pci_pbm_info *pbm) |
| 892 | { |
| 893 | } |
| 894 | #endif /* !(CONFIG_PCI_MSI) */ |
| 895 | |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 896 | static int __init pci_sun4v_pbm_init(struct pci_controller_info *p, |
| 897 | struct device_node *dp, u32 devhandle) |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 898 | { |
| 899 | struct pci_pbm_info *pbm; |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 900 | int err; |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 901 | |
David S. Miller | 3833789 | 2006-02-12 22:06:53 -0800 | [diff] [blame] | 902 | if (devhandle & 0x40) |
| 903 | pbm = &p->pbm_B; |
| 904 | else |
| 905 | pbm = &p->pbm_A; |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 906 | |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 907 | pbm->next = pci_pbm_root; |
| 908 | pci_pbm_root = pbm; |
| 909 | |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 910 | pbm->numa_node = of_node_to_nid(dp); |
| 911 | |
David S. Miller | ca3dd88 | 2007-05-09 02:35:27 -0700 | [diff] [blame] | 912 | pbm->pci_ops = &sun4v_pci_ops; |
| 913 | pbm->config_space_reg_bits = 12; |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 914 | |
David S. Miller | 6c108f1 | 2007-05-07 23:49:01 -0700 | [diff] [blame] | 915 | pbm->index = pci_num_pbms++; |
| 916 | |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 917 | pbm->parent = p; |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 918 | pbm->prom_node = dp; |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 919 | |
David S. Miller | 3833789 | 2006-02-12 22:06:53 -0800 | [diff] [blame] | 920 | pbm->devhandle = devhandle; |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 921 | |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 922 | pbm->name = dp->full_name; |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 923 | |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 924 | printk("%s: SUN4V PCI Bus Module\n", pbm->name); |
David S. Miller | c1b1a5f | 2008-03-19 04:52:48 -0700 | [diff] [blame] | 925 | printk("%s: On NUMA node %d\n", pbm->name, pbm->numa_node); |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 926 | |
David S. Miller | 9fd8b64 | 2007-03-08 21:55:49 -0800 | [diff] [blame] | 927 | pci_determine_mem_io_space(pbm); |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 928 | |
David S. Miller | cfa0652 | 2007-05-07 21:51:41 -0700 | [diff] [blame] | 929 | pci_get_pbm_props(pbm); |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 930 | |
| 931 | err = pci_sun4v_iommu_init(pbm); |
| 932 | if (err) |
| 933 | return err; |
| 934 | |
David S. Miller | 35a17eb | 2007-02-10 17:41:02 -0800 | [diff] [blame] | 935 | pci_sun4v_msi_init(pbm); |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 936 | |
| 937 | pci_sun4v_scan_bus(pbm); |
| 938 | |
| 939 | return 0; |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 940 | } |
| 941 | |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 942 | static int __devinit pci_sun4v_probe(struct of_device *op, |
| 943 | const struct of_device_id *match) |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 944 | { |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 945 | const struct linux_prom64_registers *regs; |
David S. Miller | e01c0d6 | 2007-05-25 01:04:15 -0700 | [diff] [blame] | 946 | static int hvapi_negotiated = 0; |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 947 | struct pci_controller_info *p; |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 948 | struct pci_pbm_info *pbm; |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 949 | struct device_node *dp; |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 950 | struct iommu *iommu; |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 951 | u32 devhandle; |
David S. Miller | d7472c3 | 2008-08-31 01:33:52 -0700 | [diff] [blame^] | 952 | int i, err; |
David S. Miller | 3833789 | 2006-02-12 22:06:53 -0800 | [diff] [blame] | 953 | |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 954 | dp = op->node; |
| 955 | |
David S. Miller | e01c0d6 | 2007-05-25 01:04:15 -0700 | [diff] [blame] | 956 | if (!hvapi_negotiated++) { |
| 957 | int err = sun4v_hvapi_register(HV_GRP_PCI, |
| 958 | vpci_major, |
| 959 | &vpci_minor); |
| 960 | |
| 961 | if (err) { |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 962 | printk(KERN_ERR PFX "Could not register hvapi, " |
| 963 | "err=%d\n", err); |
| 964 | return err; |
David S. Miller | e01c0d6 | 2007-05-25 01:04:15 -0700 | [diff] [blame] | 965 | } |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 966 | printk(KERN_INFO PFX "Registered hvapi major[%lu] minor[%lu]\n", |
David S. Miller | e01c0d6 | 2007-05-25 01:04:15 -0700 | [diff] [blame] | 967 | vpci_major, vpci_minor); |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 968 | |
| 969 | dma_ops = &sun4v_dma_ops; |
David S. Miller | e01c0d6 | 2007-05-25 01:04:15 -0700 | [diff] [blame] | 970 | } |
| 971 | |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 972 | regs = of_get_property(dp, "reg", NULL); |
David S. Miller | d7472c3 | 2008-08-31 01:33:52 -0700 | [diff] [blame^] | 973 | err = -ENODEV; |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 974 | if (!regs) { |
| 975 | printk(KERN_ERR PFX "Could not find config registers\n"); |
David S. Miller | d7472c3 | 2008-08-31 01:33:52 -0700 | [diff] [blame^] | 976 | goto out_err; |
Cyrill Gorcunov | 75c6d14 | 2007-11-20 17:32:19 -0800 | [diff] [blame] | 977 | } |
David S. Miller | e87dc35 | 2006-06-21 18:18:47 -0700 | [diff] [blame] | 978 | devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff; |
David S. Miller | 3833789 | 2006-02-12 22:06:53 -0800 | [diff] [blame] | 979 | |
David S. Miller | 34768bc | 2007-05-07 23:06:27 -0700 | [diff] [blame] | 980 | for (pbm = pci_pbm_root; pbm; pbm = pbm->next) { |
David S. Miller | 0b52249 | 2006-02-12 22:29:36 -0800 | [diff] [blame] | 981 | if (pbm->devhandle == (devhandle ^ 0x40)) { |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 982 | return pci_sun4v_pbm_init(pbm->parent, dp, devhandle); |
David S. Miller | 0b52249 | 2006-02-12 22:29:36 -0800 | [diff] [blame] | 983 | } |
David S. Miller | 3833789 | 2006-02-12 22:06:53 -0800 | [diff] [blame] | 984 | } |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 985 | |
David S. Miller | d7472c3 | 2008-08-31 01:33:52 -0700 | [diff] [blame^] | 986 | err = -ENOMEM; |
KAMEZAWA Hiroyuki | a283a52 | 2006-04-10 22:52:52 -0700 | [diff] [blame] | 987 | for_each_possible_cpu(i) { |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 988 | unsigned long page = get_zeroed_page(GFP_ATOMIC); |
| 989 | |
| 990 | if (!page) |
David S. Miller | d7472c3 | 2008-08-31 01:33:52 -0700 | [diff] [blame^] | 991 | goto out_err; |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 992 | |
David S. Miller | ad7ad57 | 2007-07-27 22:39:14 -0700 | [diff] [blame] | 993 | per_cpu(iommu_batch, i).pglist = (u64 *) page; |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 994 | } |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 995 | |
Yan Burman | 982c206 | 2006-11-30 17:13:09 -0800 | [diff] [blame] | 996 | p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC); |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 997 | if (!p) { |
| 998 | printk(KERN_ERR PFX "Could not allocate pci_controller_info\n"); |
David S. Miller | d7472c3 | 2008-08-31 01:33:52 -0700 | [diff] [blame^] | 999 | goto out_err; |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 1000 | } |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 1001 | |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 1002 | iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC); |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 1003 | if (!iommu) { |
| 1004 | printk(KERN_ERR PFX "Could not allocate pbm A iommu\n"); |
David S. Miller | d7472c3 | 2008-08-31 01:33:52 -0700 | [diff] [blame^] | 1005 | goto out_free_controller; |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 1006 | } |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 1007 | |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 1008 | p->pbm_A.iommu = iommu; |
| 1009 | |
David S. Miller | 16ce82d | 2007-04-26 21:08:21 -0700 | [diff] [blame] | 1010 | iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC); |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 1011 | if (!iommu) { |
| 1012 | printk(KERN_ERR PFX "Could not allocate pbm B iommu\n"); |
David S. Miller | d7472c3 | 2008-08-31 01:33:52 -0700 | [diff] [blame^] | 1013 | goto out_free_iommu_A; |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 1014 | } |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 1015 | |
David S. Miller | bade562 | 2006-02-09 22:05:54 -0800 | [diff] [blame] | 1016 | p->pbm_B.iommu = iommu; |
| 1017 | |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 1018 | return pci_sun4v_pbm_init(p, dp, devhandle); |
David S. Miller | 7c8f486 | 2006-02-13 21:50:27 -0800 | [diff] [blame] | 1019 | |
David S. Miller | d7472c3 | 2008-08-31 01:33:52 -0700 | [diff] [blame^] | 1020 | out_free_iommu_A: |
| 1021 | kfree(p->pbm_A.iommu); |
| 1022 | |
| 1023 | out_free_controller: |
| 1024 | kfree(p); |
| 1025 | |
| 1026 | out_err: |
| 1027 | return err; |
David S. Miller | 8f6a93a | 2006-02-09 21:32:07 -0800 | [diff] [blame] | 1028 | } |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 1029 | |
David S. Miller | fd09831 | 2008-08-31 01:23:17 -0700 | [diff] [blame] | 1030 | static struct of_device_id __initdata pci_sun4v_match[] = { |
David S. Miller | 3822b50 | 2008-08-30 02:50:29 -0700 | [diff] [blame] | 1031 | { |
| 1032 | .name = "pci", |
| 1033 | .compatible = "SUNW,sun4v-pci", |
| 1034 | }, |
| 1035 | {}, |
| 1036 | }; |
| 1037 | |
| 1038 | static struct of_platform_driver pci_sun4v_driver = { |
| 1039 | .name = DRIVER_NAME, |
| 1040 | .match_table = pci_sun4v_match, |
| 1041 | .probe = pci_sun4v_probe, |
| 1042 | }; |
| 1043 | |
| 1044 | static int __init pci_sun4v_init(void) |
| 1045 | { |
| 1046 | return of_register_driver(&pci_sun4v_driver, &of_bus_type); |
| 1047 | } |
| 1048 | |
| 1049 | subsys_initcall(pci_sun4v_init); |