blob: c8b6199a5dc4b405b4153eb0b084842266391b71 [file] [log] [blame]
David S. Miller8f6a93a2006-02-09 21:32:07 -08001/* pci_sun4v.c: SUN4V specific PCI controller support.
2 *
David S. Millerd2841422008-02-08 18:05:46 -08003 * Copyright (C) 2006, 2007, 2008 David S. Miller (davem@davemloft.net)
David S. Miller8f6a93a2006-02-09 21:32:07 -08004 */
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. Miller18397942006-02-10 00:08:26 -080012#include <linux/percpu.h>
David S. Miller35a17eb2007-02-10 17:41:02 -080013#include <linux/irq.h>
14#include <linux/msi.h>
David S. Miller59db8102007-05-23 18:00:46 -070015#include <linux/log2.h>
David S. Miller8f6a93a2006-02-09 21:32:07 -080016
David S. Miller8f6a93a2006-02-09 21:32:07 -080017#include <asm/iommu.h>
18#include <asm/irq.h>
19#include <asm/upa.h>
20#include <asm/pstate.h>
21#include <asm/oplib.h>
22#include <asm/hypervisor.h>
David S. Millere87dc352006-06-21 18:18:47 -070023#include <asm/prom.h>
David S. Miller8f6a93a2006-02-09 21:32:07 -080024
25#include "pci_impl.h"
26#include "iommu_common.h"
27
David S. Millerbade5622006-02-09 22:05:54 -080028#include "pci_sun4v.h"
29
David S. Millere01c0d62007-05-25 01:04:15 -070030static unsigned long vpci_major = 1;
31static unsigned long vpci_minor = 1;
32
David S. Miller7c8f4862006-02-13 21:50:27 -080033#define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
David S. Miller18397942006-02-10 00:08:26 -080034
David S. Miller16ce82d2007-04-26 21:08:21 -070035struct iommu_batch {
David S. Millerad7ad572007-07-27 22:39:14 -070036 struct device *dev; /* Device mapping is for. */
David S. Miller6a32fd42006-02-19 22:21:32 -080037 unsigned long prot; /* IOMMU page protections */
38 unsigned long entry; /* Index into IOTSB. */
39 u64 *pglist; /* List of physical pages */
40 unsigned long npages; /* Number of pages in list. */
David S. Miller18397942006-02-10 00:08:26 -080041};
42
David S. Millerad7ad572007-07-27 22:39:14 -070043static DEFINE_PER_CPU(struct iommu_batch, iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -080044
45/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -070046static inline void iommu_batch_start(struct device *dev, unsigned long prot, unsigned long entry)
David S. Miller6a32fd42006-02-19 22:21:32 -080047{
David S. Millerad7ad572007-07-27 22:39:14 -070048 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -080049
David S. Millerad7ad572007-07-27 22:39:14 -070050 p->dev = dev;
David S. Miller6a32fd42006-02-19 22:21:32 -080051 p->prot = prot;
52 p->entry = entry;
53 p->npages = 0;
54}
55
56/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -070057static long iommu_batch_flush(struct iommu_batch *p)
David S. Miller6a32fd42006-02-19 22:21:32 -080058{
David S. Millerad7ad572007-07-27 22:39:14 -070059 struct pci_pbm_info *pbm = p->dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -080060 unsigned long devhandle = pbm->devhandle;
David S. Miller6a32fd42006-02-19 22:21:32 -080061 unsigned long prot = p->prot;
62 unsigned long entry = p->entry;
63 u64 *pglist = p->pglist;
64 unsigned long npages = p->npages;
65
David S. Millerd82965c2006-02-20 01:42:51 -080066 while (npages != 0) {
David S. Miller6a32fd42006-02-19 22:21:32 -080067 long num;
68
69 num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
70 npages, prot, __pa(pglist));
71 if (unlikely(num < 0)) {
72 if (printk_ratelimit())
David S. Millerad7ad572007-07-27 22:39:14 -070073 printk("iommu_batch_flush: IOMMU map of "
David S. Miller6a32fd42006-02-19 22:21:32 -080074 "[%08lx:%08lx:%lx:%lx:%lx] failed with "
75 "status %ld\n",
76 devhandle, HV_PCI_TSBID(0, entry),
77 npages, prot, __pa(pglist), num);
78 return -1;
79 }
80
81 entry += num;
82 npages -= num;
83 pglist += num;
David S. Millerd82965c2006-02-20 01:42:51 -080084 }
David S. Miller6a32fd42006-02-19 22:21:32 -080085
86 p->entry = entry;
87 p->npages = 0;
88
89 return 0;
90}
91
92/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -070093static inline long iommu_batch_add(u64 phys_page)
David S. Miller6a32fd42006-02-19 22:21:32 -080094{
David S. Millerad7ad572007-07-27 22:39:14 -070095 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -080096
97 BUG_ON(p->npages >= PGLIST_NENTS);
98
99 p->pglist[p->npages++] = phys_page;
100 if (p->npages == PGLIST_NENTS)
David S. Millerad7ad572007-07-27 22:39:14 -0700101 return iommu_batch_flush(p);
David S. Miller6a32fd42006-02-19 22:21:32 -0800102
103 return 0;
104}
105
106/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -0700107static inline long iommu_batch_end(void)
David S. Miller6a32fd42006-02-19 22:21:32 -0800108{
David S. Millerad7ad572007-07-27 22:39:14 -0700109 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -0800110
111 BUG_ON(p->npages >= PGLIST_NENTS);
112
David S. Millerad7ad572007-07-27 22:39:14 -0700113 return iommu_batch_flush(p);
David S. Miller6a32fd42006-02-19 22:21:32 -0800114}
David S. Miller18397942006-02-10 00:08:26 -0800115
David S. Millerad7ad572007-07-27 22:39:14 -0700116static void *dma_4v_alloc_coherent(struct device *dev, size_t size,
117 dma_addr_t *dma_addrp, gfp_t gfp)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800118{
David S. Miller16ce82d2007-04-26 21:08:21 -0700119 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800120 unsigned long flags, order, first_page, npages, n;
David S. Miller18397942006-02-10 00:08:26 -0800121 void *ret;
122 long entry;
David S. Miller18397942006-02-10 00:08:26 -0800123
124 size = IO_PAGE_ALIGN(size);
125 order = get_order(size);
David S. Miller6a32fd42006-02-19 22:21:32 -0800126 if (unlikely(order >= MAX_ORDER))
David S. Miller18397942006-02-10 00:08:26 -0800127 return NULL;
128
129 npages = size >> IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800130
David S. Miller42f14232006-05-23 02:07:22 -0700131 first_page = __get_free_pages(gfp, order);
David S. Miller6a32fd42006-02-19 22:21:32 -0800132 if (unlikely(first_page == 0UL))
David S. Miller18397942006-02-10 00:08:26 -0800133 return NULL;
David S. Millere7a04532006-02-15 22:25:27 -0800134
David S. Miller18397942006-02-10 00:08:26 -0800135 memset((char *)first_page, 0, PAGE_SIZE << order);
136
David S. Millerad7ad572007-07-27 22:39:14 -0700137 iommu = dev->archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800138
139 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800140 entry = iommu_range_alloc(dev, iommu, npages, NULL);
David S. Miller18397942006-02-10 00:08:26 -0800141 spin_unlock_irqrestore(&iommu->lock, flags);
142
David S. Millerd2841422008-02-08 18:05:46 -0800143 if (unlikely(entry == DMA_ERROR_CODE))
144 goto range_alloc_fail;
David S. Miller18397942006-02-10 00:08:26 -0800145
146 *dma_addrp = (iommu->page_table_map_base +
147 (entry << IO_PAGE_SHIFT));
148 ret = (void *) first_page;
149 first_page = __pa(first_page);
150
David S. Miller6a32fd42006-02-19 22:21:32 -0800151 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800152
David S. Millerad7ad572007-07-27 22:39:14 -0700153 iommu_batch_start(dev,
154 (HV_PCI_MAP_ATTR_READ |
155 HV_PCI_MAP_ATTR_WRITE),
156 entry);
David S. Miller18397942006-02-10 00:08:26 -0800157
David S. Miller6a32fd42006-02-19 22:21:32 -0800158 for (n = 0; n < npages; n++) {
David S. Millerad7ad572007-07-27 22:39:14 -0700159 long err = iommu_batch_add(first_page + (n * PAGE_SIZE));
David S. Miller6a32fd42006-02-19 22:21:32 -0800160 if (unlikely(err < 0L))
161 goto iommu_map_fail;
162 }
David S. Miller18397942006-02-10 00:08:26 -0800163
David S. Millerad7ad572007-07-27 22:39:14 -0700164 if (unlikely(iommu_batch_end() < 0L))
David S. Miller6a32fd42006-02-19 22:21:32 -0800165 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800166
David S. Miller6a32fd42006-02-19 22:21:32 -0800167 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800168
169 return ret;
David S. Miller6a32fd42006-02-19 22:21:32 -0800170
171iommu_map_fail:
172 /* Interrupts are disabled. */
173 spin_lock(&iommu->lock);
David S. Millerd2841422008-02-08 18:05:46 -0800174 iommu_range_free(iommu, *dma_addrp, npages);
David S. Miller6a32fd42006-02-19 22:21:32 -0800175 spin_unlock_irqrestore(&iommu->lock, flags);
176
David S. Millerd2841422008-02-08 18:05:46 -0800177range_alloc_fail:
David S. Miller6a32fd42006-02-19 22:21:32 -0800178 free_pages(first_page, order);
179 return NULL;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800180}
181
David S. Millerad7ad572007-07-27 22:39:14 -0700182static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu,
183 dma_addr_t dvma)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800184{
David S. Millera2fb23a2007-02-28 23:35:04 -0800185 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700186 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800187 unsigned long flags, order, npages, entry;
188 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800189
190 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700191 iommu = dev->archdata.iommu;
192 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800193 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800194 entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
195
196 spin_lock_irqsave(&iommu->lock, flags);
197
David S. Millerd2841422008-02-08 18:05:46 -0800198 iommu_range_free(iommu, dvma, npages);
David S. Miller18397942006-02-10 00:08:26 -0800199
200 do {
201 unsigned long num;
202
203 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
204 npages);
205 entry += num;
206 npages -= num;
207 } while (npages != 0);
208
209 spin_unlock_irqrestore(&iommu->lock, flags);
210
211 order = get_order(size);
212 if (order < 10)
213 free_pages((unsigned long)cpu, order);
David S. Miller8f6a93a2006-02-09 21:32:07 -0800214}
215
David S. Millerad7ad572007-07-27 22:39:14 -0700216static dma_addr_t dma_4v_map_single(struct device *dev, void *ptr, size_t sz,
217 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800218{
David S. Miller16ce82d2007-04-26 21:08:21 -0700219 struct iommu *iommu;
David S. Miller18397942006-02-10 00:08:26 -0800220 unsigned long flags, npages, oaddr;
David S. Miller7c8f4862006-02-13 21:50:27 -0800221 unsigned long i, base_paddr;
David S. Miller6a32fd42006-02-19 22:21:32 -0800222 u32 bus_addr, ret;
David S. Miller18397942006-02-10 00:08:26 -0800223 unsigned long prot;
224 long entry;
David S. Miller18397942006-02-10 00:08:26 -0800225
David S. Millerad7ad572007-07-27 22:39:14 -0700226 iommu = dev->archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800227
David S. Millerad7ad572007-07-27 22:39:14 -0700228 if (unlikely(direction == DMA_NONE))
David S. Miller18397942006-02-10 00:08:26 -0800229 goto bad;
230
231 oaddr = (unsigned long)ptr;
232 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
233 npages >>= IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800234
235 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800236 entry = iommu_range_alloc(dev, iommu, npages, NULL);
David S. Miller18397942006-02-10 00:08:26 -0800237 spin_unlock_irqrestore(&iommu->lock, flags);
238
David S. Millerd2841422008-02-08 18:05:46 -0800239 if (unlikely(entry == DMA_ERROR_CODE))
David S. Miller18397942006-02-10 00:08:26 -0800240 goto bad;
241
242 bus_addr = (iommu->page_table_map_base +
243 (entry << IO_PAGE_SHIFT));
244 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
245 base_paddr = __pa(oaddr & IO_PAGE_MASK);
246 prot = HV_PCI_MAP_ATTR_READ;
David S. Millerad7ad572007-07-27 22:39:14 -0700247 if (direction != DMA_TO_DEVICE)
David S. Miller18397942006-02-10 00:08:26 -0800248 prot |= HV_PCI_MAP_ATTR_WRITE;
249
David S. Miller6a32fd42006-02-19 22:21:32 -0800250 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800251
David S. Millerad7ad572007-07-27 22:39:14 -0700252 iommu_batch_start(dev, prot, entry);
David S. Miller18397942006-02-10 00:08:26 -0800253
David S. Miller6a32fd42006-02-19 22:21:32 -0800254 for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
David S. Millerad7ad572007-07-27 22:39:14 -0700255 long err = iommu_batch_add(base_paddr);
David S. Miller6a32fd42006-02-19 22:21:32 -0800256 if (unlikely(err < 0L))
257 goto iommu_map_fail;
258 }
David S. Millerad7ad572007-07-27 22:39:14 -0700259 if (unlikely(iommu_batch_end() < 0L))
David S. Miller6a32fd42006-02-19 22:21:32 -0800260 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800261
David S. Miller6a32fd42006-02-19 22:21:32 -0800262 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800263
264 return ret;
265
266bad:
267 if (printk_ratelimit())
268 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700269 return DMA_ERROR_CODE;
David S. Miller6a32fd42006-02-19 22:21:32 -0800270
271iommu_map_fail:
272 /* Interrupts are disabled. */
273 spin_lock(&iommu->lock);
David S. Millerd2841422008-02-08 18:05:46 -0800274 iommu_range_free(iommu, bus_addr, npages);
David S. Miller6a32fd42006-02-19 22:21:32 -0800275 spin_unlock_irqrestore(&iommu->lock, flags);
276
David S. Millerad7ad572007-07-27 22:39:14 -0700277 return DMA_ERROR_CODE;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800278}
279
David S. Millerad7ad572007-07-27 22:39:14 -0700280static void dma_4v_unmap_single(struct device *dev, dma_addr_t bus_addr,
281 size_t sz, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800282{
David S. Millera2fb23a2007-02-28 23:35:04 -0800283 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700284 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800285 unsigned long flags, npages;
David S. Miller18397942006-02-10 00:08:26 -0800286 long entry;
David S. Miller7c8f4862006-02-13 21:50:27 -0800287 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800288
David S. Millerad7ad572007-07-27 22:39:14 -0700289 if (unlikely(direction == DMA_NONE)) {
David S. Miller18397942006-02-10 00:08:26 -0800290 if (printk_ratelimit())
291 WARN_ON(1);
292 return;
293 }
294
David S. Millerad7ad572007-07-27 22:39:14 -0700295 iommu = dev->archdata.iommu;
296 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800297 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800298
299 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
300 npages >>= IO_PAGE_SHIFT;
301 bus_addr &= IO_PAGE_MASK;
302
303 spin_lock_irqsave(&iommu->lock, flags);
304
David S. Millerd2841422008-02-08 18:05:46 -0800305 iommu_range_free(iommu, bus_addr, npages);
David S. Miller18397942006-02-10 00:08:26 -0800306
David S. Millerd2841422008-02-08 18:05:46 -0800307 entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800308 do {
309 unsigned long num;
310
311 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
312 npages);
313 entry += num;
314 npages -= num;
315 } while (npages != 0);
316
317 spin_unlock_irqrestore(&iommu->lock, flags);
318}
319
David S. Millerad7ad572007-07-27 22:39:14 -0700320static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
321 int nelems, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800322{
David S. Miller38192d52008-02-06 03:50:26 -0800323 unsigned long flags, npages, i, prot;
David S. Millerd2841422008-02-08 18:05:46 -0800324 u32 dma_base, orig_dma_base;
David S. Miller38192d52008-02-06 03:50:26 -0800325 struct scatterlist *sg;
David S. Miller16ce82d2007-04-26 21:08:21 -0700326 struct iommu *iommu;
David S. Miller6a32fd42006-02-19 22:21:32 -0800327 long entry, err;
David S. Miller18397942006-02-10 00:08:26 -0800328
329 /* Fast path single entry scatterlists. */
330 if (nelems == 1) {
331 sglist->dma_address =
Jens Axboe58b053e2007-10-22 20:02:46 +0200332 dma_4v_map_single(dev, sg_virt(sglist),
David S. Miller18397942006-02-10 00:08:26 -0800333 sglist->length, direction);
David S. Millerad7ad572007-07-27 22:39:14 -0700334 if (unlikely(sglist->dma_address == DMA_ERROR_CODE))
David S. Miller18397942006-02-10 00:08:26 -0800335 return 0;
336 sglist->dma_length = sglist->length;
337 return 1;
338 }
339
David S. Millerad7ad572007-07-27 22:39:14 -0700340 iommu = dev->archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800341
David S. Millerad7ad572007-07-27 22:39:14 -0700342 if (unlikely(direction == DMA_NONE))
David S. Miller18397942006-02-10 00:08:26 -0800343 goto bad;
344
David S. Miller38192d52008-02-06 03:50:26 -0800345 npages = calc_npages(sglist, nelems);
David S. Miller18397942006-02-10 00:08:26 -0800346
David S. Miller18397942006-02-10 00:08:26 -0800347 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800348 entry = iommu_range_alloc(dev, iommu, npages, NULL);
David S. Miller18397942006-02-10 00:08:26 -0800349 spin_unlock_irqrestore(&iommu->lock, flags);
350
David S. Millerd2841422008-02-08 18:05:46 -0800351 if (unlikely(entry == DMA_ERROR_CODE))
David S. Miller18397942006-02-10 00:08:26 -0800352 goto bad;
353
David S. Millerd2841422008-02-08 18:05:46 -0800354 orig_dma_base = dma_base = iommu->page_table_map_base +
David S. Miller18397942006-02-10 00:08:26 -0800355 (entry << IO_PAGE_SHIFT);
356
David S. Miller18397942006-02-10 00:08:26 -0800357 prot = HV_PCI_MAP_ATTR_READ;
David S. Millerad7ad572007-07-27 22:39:14 -0700358 if (direction != DMA_TO_DEVICE)
David S. Miller18397942006-02-10 00:08:26 -0800359 prot |= HV_PCI_MAP_ATTR_WRITE;
360
David S. Miller38192d52008-02-06 03:50:26 -0800361 local_irq_save(flags);
362
363 iommu_batch_start(dev, prot, entry);
364
365 for_each_sg(sglist, sg, nelems, i) {
366 unsigned long paddr = SG_ENT_PHYS_ADDRESS(sg);
367 unsigned long slen = sg->length;
368 unsigned long this_npages;
369
370 this_npages = iommu_num_pages(paddr, slen);
371
372 sg->dma_address = dma_base | (paddr & ~IO_PAGE_MASK);
373 sg->dma_length = slen;
374
375 paddr &= IO_PAGE_MASK;
376 while (this_npages--) {
377 err = iommu_batch_add(paddr);
378 if (unlikely(err < 0L)) {
379 local_irq_restore(flags);
380 goto iommu_map_failed;
381 }
382
383 paddr += IO_PAGE_SIZE;
384 dma_base += IO_PAGE_SIZE;
385 }
386 }
387
388 err = iommu_batch_end();
389
390 local_irq_restore(flags);
391
David S. Miller6a32fd42006-02-19 22:21:32 -0800392 if (unlikely(err < 0L))
393 goto iommu_map_failed;
David S. Miller18397942006-02-10 00:08:26 -0800394
David S. Miller38192d52008-02-06 03:50:26 -0800395 return nelems;
David S. Miller18397942006-02-10 00:08:26 -0800396
397bad:
398 if (printk_ratelimit())
399 WARN_ON(1);
400 return 0;
David S. Miller6a32fd42006-02-19 22:21:32 -0800401
402iommu_map_failed:
403 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800404 iommu_range_free(iommu, orig_dma_base, npages);
David S. Miller6a32fd42006-02-19 22:21:32 -0800405 spin_unlock_irqrestore(&iommu->lock, flags);
406
407 return 0;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800408}
409
David S. Millerad7ad572007-07-27 22:39:14 -0700410static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
411 int nelems, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800412{
David S. Miller38192d52008-02-06 03:50:26 -0800413 unsigned long flags, npages;
David S. Millera2fb23a2007-02-28 23:35:04 -0800414 struct pci_pbm_info *pbm;
David S. Miller7c8f4862006-02-13 21:50:27 -0800415 u32 devhandle, bus_addr;
David S. Miller38192d52008-02-06 03:50:26 -0800416 struct iommu *iommu;
417 long entry;
David S. Miller18397942006-02-10 00:08:26 -0800418
David S. Millerad7ad572007-07-27 22:39:14 -0700419 if (unlikely(direction == DMA_NONE)) {
David S. Miller18397942006-02-10 00:08:26 -0800420 if (printk_ratelimit())
421 WARN_ON(1);
422 }
423
David S. Millerad7ad572007-07-27 22:39:14 -0700424 iommu = dev->archdata.iommu;
425 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800426 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800427
428 bus_addr = sglist->dma_address & IO_PAGE_MASK;
Jens Axboe2c941a22007-08-07 09:37:10 +0200429
David S. Miller38192d52008-02-06 03:50:26 -0800430 npages = calc_npages(sglist, nelems);
David S. Miller18397942006-02-10 00:08:26 -0800431
432 entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
433
434 spin_lock_irqsave(&iommu->lock, flags);
435
David S. Millerd2841422008-02-08 18:05:46 -0800436 iommu_range_free(iommu, bus_addr, npages);
David S. Miller18397942006-02-10 00:08:26 -0800437
438 do {
439 unsigned long num;
440
441 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
442 npages);
443 entry += num;
444 npages -= num;
445 } while (npages != 0);
446
447 spin_unlock_irqrestore(&iommu->lock, flags);
David S. Miller8f6a93a2006-02-09 21:32:07 -0800448}
449
David S. Millerad7ad572007-07-27 22:39:14 -0700450static void dma_4v_sync_single_for_cpu(struct device *dev,
451 dma_addr_t bus_addr, size_t sz,
452 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800453{
David S. Miller18397942006-02-10 00:08:26 -0800454 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800455}
456
David S. Millerad7ad572007-07-27 22:39:14 -0700457static void dma_4v_sync_sg_for_cpu(struct device *dev,
458 struct scatterlist *sglist, int nelems,
459 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800460{
David S. Miller18397942006-02-10 00:08:26 -0800461 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800462}
463
David S. Millerad7ad572007-07-27 22:39:14 -0700464const struct dma_ops sun4v_dma_ops = {
465 .alloc_coherent = dma_4v_alloc_coherent,
466 .free_coherent = dma_4v_free_coherent,
467 .map_single = dma_4v_map_single,
468 .unmap_single = dma_4v_unmap_single,
469 .map_sg = dma_4v_map_sg,
470 .unmap_sg = dma_4v_unmap_sg,
471 .sync_single_for_cpu = dma_4v_sync_single_for_cpu,
472 .sync_sg_for_cpu = dma_4v_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800473};
474
Sam Ravnborga1f35ba2008-01-21 17:22:46 -0800475static void __init pci_sun4v_scan_bus(struct pci_pbm_info *pbm)
David S. Millerbade5622006-02-09 22:05:54 -0800476{
David S. Millere87dc352006-06-21 18:18:47 -0700477 struct property *prop;
478 struct device_node *dp;
479
David S. Miller34768bc2007-05-07 23:06:27 -0700480 dp = pbm->prom_node;
481 prop = of_find_property(dp, "66mhz-capable", NULL);
482 pbm->is_66mhz_capable = (prop != NULL);
483 pbm->pci_bus = pci_scan_one_pbm(pbm);
David S. Millerc2609262006-02-12 22:18:52 -0800484
485 /* XXX register error interrupt handlers XXX */
David S. Millerbade5622006-02-09 22:05:54 -0800486}
487
Adrian Bunk4c622252008-02-05 03:01:43 -0800488static unsigned long __init probe_existing_entries(struct pci_pbm_info *pbm,
489 struct iommu *iommu)
David S. Miller18397942006-02-10 00:08:26 -0800490{
David S. Miller9b3627f2007-04-24 23:51:18 -0700491 struct iommu_arena *arena = &iommu->arena;
David S. Millere7a04532006-02-15 22:25:27 -0800492 unsigned long i, cnt = 0;
David S. Miller7c8f4862006-02-13 21:50:27 -0800493 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800494
495 devhandle = pbm->devhandle;
496 for (i = 0; i < arena->limit; i++) {
497 unsigned long ret, io_attrs, ra;
498
499 ret = pci_sun4v_iommu_getmap(devhandle,
500 HV_PCI_TSBID(0, i),
501 &io_attrs, &ra);
David S. Millere7a04532006-02-15 22:25:27 -0800502 if (ret == HV_EOK) {
David S. Millerc2a5a462006-06-22 00:01:56 -0700503 if (page_in_phys_avail(ra)) {
504 pci_sun4v_iommu_demap(devhandle,
505 HV_PCI_TSBID(0, i), 1);
506 } else {
507 cnt++;
508 __set_bit(i, arena->map);
509 }
David S. Millere7a04532006-02-15 22:25:27 -0800510 }
David S. Miller18397942006-02-10 00:08:26 -0800511 }
David S. Millere7a04532006-02-15 22:25:27 -0800512
513 return cnt;
David S. Miller18397942006-02-10 00:08:26 -0800514}
515
Adrian Bunk4c622252008-02-05 03:01:43 -0800516static void __init pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
David S. Millerbade5622006-02-09 22:05:54 -0800517{
David S. Miller16ce82d2007-04-26 21:08:21 -0700518 struct iommu *iommu = pbm->iommu;
David S. Millere87dc352006-06-21 18:18:47 -0700519 struct property *prop;
David S. Miller59db8102007-05-23 18:00:46 -0700520 unsigned long num_tsb_entries, sz, tsbsize;
David S. Miller18397942006-02-10 00:08:26 -0800521 u32 vdma[2], dma_mask, dma_offset;
David S. Miller18397942006-02-10 00:08:26 -0800522
David S. Millere87dc352006-06-21 18:18:47 -0700523 prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
524 if (prop) {
525 u32 *val = prop->value;
526
527 vdma[0] = val[0];
528 vdma[1] = val[1];
529 } else {
David S. Miller18397942006-02-10 00:08:26 -0800530 /* No property, use default values. */
531 vdma[0] = 0x80000000;
532 vdma[1] = 0x80000000;
533 }
534
David S. Miller59db8102007-05-23 18:00:46 -0700535 if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
536 prom_printf("PCI-SUN4V: strange virtual-dma[%08x:%08x].\n",
537 vdma[0], vdma[1]);
538 prom_halt();
David S. Miller18397942006-02-10 00:08:26 -0800539 };
540
David S. Miller59db8102007-05-23 18:00:46 -0700541 dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
542 num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
543 tsbsize = num_tsb_entries * sizeof(iopte_t);
David S. Miller18397942006-02-10 00:08:26 -0800544
545 dma_offset = vdma[0];
546
547 /* Setup initial software IOMMU state. */
548 spin_lock_init(&iommu->lock);
549 iommu->ctx_lowest_free = 1;
550 iommu->page_table_map_base = dma_offset;
551 iommu->dma_addr_mask = dma_mask;
552
553 /* Allocate and initialize the free area map. */
David S. Miller59db8102007-05-23 18:00:46 -0700554 sz = (num_tsb_entries + 7) / 8;
David S. Miller18397942006-02-10 00:08:26 -0800555 sz = (sz + 7UL) & ~7UL;
Yan Burman982c2062006-11-30 17:13:09 -0800556 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
David S. Miller18397942006-02-10 00:08:26 -0800557 if (!iommu->arena.map) {
558 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
559 prom_halt();
560 }
David S. Miller18397942006-02-10 00:08:26 -0800561 iommu->arena.limit = num_tsb_entries;
562
David S. Millere7a04532006-02-15 22:25:27 -0800563 sz = probe_existing_entries(pbm, iommu);
David S. Millerc2a5a462006-06-22 00:01:56 -0700564 if (sz)
565 printk("%s: Imported %lu TSB entries from OBP\n",
566 pbm->name, sz);
David S. Millerbade5622006-02-09 22:05:54 -0800567}
568
David S. Miller35a17eb2007-02-10 17:41:02 -0800569#ifdef CONFIG_PCI_MSI
570struct pci_sun4v_msiq_entry {
571 u64 version_type;
572#define MSIQ_VERSION_MASK 0xffffffff00000000UL
573#define MSIQ_VERSION_SHIFT 32
574#define MSIQ_TYPE_MASK 0x00000000000000ffUL
575#define MSIQ_TYPE_SHIFT 0
576#define MSIQ_TYPE_NONE 0x00
577#define MSIQ_TYPE_MSG 0x01
578#define MSIQ_TYPE_MSI32 0x02
579#define MSIQ_TYPE_MSI64 0x03
580#define MSIQ_TYPE_INTX 0x08
581#define MSIQ_TYPE_NONE2 0xff
582
583 u64 intx_sysino;
584 u64 reserved1;
585 u64 stick;
586 u64 req_id; /* bus/device/func */
587#define MSIQ_REQID_BUS_MASK 0xff00UL
588#define MSIQ_REQID_BUS_SHIFT 8
589#define MSIQ_REQID_DEVICE_MASK 0x00f8UL
590#define MSIQ_REQID_DEVICE_SHIFT 3
591#define MSIQ_REQID_FUNC_MASK 0x0007UL
592#define MSIQ_REQID_FUNC_SHIFT 0
593
594 u64 msi_address;
595
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700596 /* The format of this value is message type dependent.
David S. Miller35a17eb2007-02-10 17:41:02 -0800597 * For MSI bits 15:0 are the data from the MSI packet.
598 * For MSI-X bits 31:0 are the data from the MSI packet.
599 * For MSG, the message code and message routing code where:
600 * bits 39:32 is the bus/device/fn of the msg target-id
601 * bits 18:16 is the message routing code
602 * bits 7:0 is the message code
603 * For INTx the low order 2-bits are:
604 * 00 - INTA
605 * 01 - INTB
606 * 10 - INTC
607 * 11 - INTD
608 */
609 u64 msi_data;
610
611 u64 reserved2;
612};
613
David S. Miller759f89e2007-10-11 03:16:13 -0700614static int pci_sun4v_get_head(struct pci_pbm_info *pbm, unsigned long msiqid,
615 unsigned long *head)
David S. Miller35a17eb2007-02-10 17:41:02 -0800616{
David S. Miller759f89e2007-10-11 03:16:13 -0700617 unsigned long err, limit;
David S. Miller35a17eb2007-02-10 17:41:02 -0800618
David S. Miller759f89e2007-10-11 03:16:13 -0700619 err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, head);
David S. Miller35a17eb2007-02-10 17:41:02 -0800620 if (unlikely(err))
David S. Miller759f89e2007-10-11 03:16:13 -0700621 return -ENXIO;
David S. Miller35a17eb2007-02-10 17:41:02 -0800622
David S. Miller759f89e2007-10-11 03:16:13 -0700623 limit = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
624 if (unlikely(*head >= limit))
625 return -EFBIG;
David S. Miller35a17eb2007-02-10 17:41:02 -0800626
627 return 0;
628}
629
David S. Miller759f89e2007-10-11 03:16:13 -0700630static int pci_sun4v_dequeue_msi(struct pci_pbm_info *pbm,
631 unsigned long msiqid, unsigned long *head,
632 unsigned long *msi)
David S. Miller35a17eb2007-02-10 17:41:02 -0800633{
David S. Miller759f89e2007-10-11 03:16:13 -0700634 struct pci_sun4v_msiq_entry *ep;
635 unsigned long err, type;
636
637 /* Note: void pointer arithmetic, 'head' is a byte offset */
638 ep = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
639 (pbm->msiq_ent_count *
640 sizeof(struct pci_sun4v_msiq_entry))) +
641 *head);
642
643 if ((ep->version_type & MSIQ_TYPE_MASK) == 0)
644 return 0;
645
646 type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
647 if (unlikely(type != MSIQ_TYPE_MSI32 &&
648 type != MSIQ_TYPE_MSI64))
649 return -EINVAL;
650
651 *msi = ep->msi_data;
652
653 err = pci_sun4v_msi_setstate(pbm->devhandle,
654 ep->msi_data /* msi_num */,
655 HV_MSISTATE_IDLE);
656 if (unlikely(err))
657 return -ENXIO;
658
659 /* Clear the entry. */
660 ep->version_type &= ~MSIQ_TYPE_MASK;
661
662 (*head) += sizeof(struct pci_sun4v_msiq_entry);
663 if (*head >=
664 (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry)))
665 *head = 0;
666
667 return 1;
David S. Miller35a17eb2007-02-10 17:41:02 -0800668}
669
David S. Miller759f89e2007-10-11 03:16:13 -0700670static int pci_sun4v_set_head(struct pci_pbm_info *pbm, unsigned long msiqid,
671 unsigned long head)
672{
673 unsigned long err;
674
675 err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
676 if (unlikely(err))
677 return -EINVAL;
678
679 return 0;
680}
681
682static int pci_sun4v_msi_setup(struct pci_pbm_info *pbm, unsigned long msiqid,
683 unsigned long msi, int is_msi64)
684{
685 if (pci_sun4v_msi_setmsiq(pbm->devhandle, msi, msiqid,
686 (is_msi64 ?
687 HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
688 return -ENXIO;
689 if (pci_sun4v_msi_setstate(pbm->devhandle, msi, HV_MSISTATE_IDLE))
690 return -ENXIO;
691 if (pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_VALID))
692 return -ENXIO;
693 return 0;
694}
695
696static int pci_sun4v_msi_teardown(struct pci_pbm_info *pbm, unsigned long msi)
697{
698 unsigned long err, msiqid;
699
700 err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi, &msiqid);
701 if (err)
702 return -ENXIO;
703
704 pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_INVALID);
705
706 return 0;
707}
708
709static int pci_sun4v_msiq_alloc(struct pci_pbm_info *pbm)
David S. Miller35a17eb2007-02-10 17:41:02 -0800710{
711 unsigned long q_size, alloc_size, pages, order;
712 int i;
713
714 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
715 alloc_size = (pbm->msiq_num * q_size);
716 order = get_order(alloc_size);
717 pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
718 if (pages == 0UL) {
719 printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
720 order);
721 return -ENOMEM;
722 }
723 memset((char *)pages, 0, PAGE_SIZE << order);
724 pbm->msi_queues = (void *) pages;
725
726 for (i = 0; i < pbm->msiq_num; i++) {
727 unsigned long err, base = __pa(pages + (i * q_size));
728 unsigned long ret1, ret2;
729
730 err = pci_sun4v_msiq_conf(pbm->devhandle,
731 pbm->msiq_first + i,
732 base, pbm->msiq_ent_count);
733 if (err) {
734 printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
735 err);
736 goto h_error;
737 }
738
739 err = pci_sun4v_msiq_info(pbm->devhandle,
740 pbm->msiq_first + i,
741 &ret1, &ret2);
742 if (err) {
743 printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
744 err);
745 goto h_error;
746 }
747 if (ret1 != base || ret2 != pbm->msiq_ent_count) {
748 printk(KERN_ERR "MSI: Bogus qconf "
749 "expected[%lx:%x] got[%lx:%lx]\n",
750 base, pbm->msiq_ent_count,
751 ret1, ret2);
752 goto h_error;
753 }
754 }
755
756 return 0;
757
758h_error:
759 free_pages(pages, order);
760 return -EINVAL;
761}
762
David S. Miller759f89e2007-10-11 03:16:13 -0700763static void pci_sun4v_msiq_free(struct pci_pbm_info *pbm)
David S. Miller35a17eb2007-02-10 17:41:02 -0800764{
David S. Miller759f89e2007-10-11 03:16:13 -0700765 unsigned long q_size, alloc_size, pages, order;
David S. Miller35a17eb2007-02-10 17:41:02 -0800766 int i;
767
David S. Miller759f89e2007-10-11 03:16:13 -0700768 for (i = 0; i < pbm->msiq_num; i++) {
769 unsigned long msiqid = pbm->msiq_first + i;
770
771 (void) pci_sun4v_msiq_conf(pbm->devhandle, msiqid, 0UL, 0);
David S. Miller35a17eb2007-02-10 17:41:02 -0800772 }
773
David S. Miller759f89e2007-10-11 03:16:13 -0700774 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
775 alloc_size = (pbm->msiq_num * q_size);
776 order = get_order(alloc_size);
777
778 pages = (unsigned long) pbm->msi_queues;
779
780 free_pages(pages, order);
781
782 pbm->msi_queues = NULL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800783}
784
David S. Miller759f89e2007-10-11 03:16:13 -0700785static int pci_sun4v_msiq_build_irq(struct pci_pbm_info *pbm,
786 unsigned long msiqid,
787 unsigned long devino)
David S. Miller35a17eb2007-02-10 17:41:02 -0800788{
David S. Miller759f89e2007-10-11 03:16:13 -0700789 unsigned int virt_irq = sun4v_build_irq(pbm->devhandle, devino);
David S. Miller35a17eb2007-02-10 17:41:02 -0800790
David S. Miller759f89e2007-10-11 03:16:13 -0700791 if (!virt_irq)
792 return -ENOMEM;
David S. Miller35a17eb2007-02-10 17:41:02 -0800793
David S. Miller35a17eb2007-02-10 17:41:02 -0800794 if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
David S. Miller759f89e2007-10-11 03:16:13 -0700795 return -EINVAL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800796 if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
David S. Miller759f89e2007-10-11 03:16:13 -0700797 return -EINVAL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800798
David S. Miller759f89e2007-10-11 03:16:13 -0700799 return virt_irq;
David S. Miller35a17eb2007-02-10 17:41:02 -0800800}
801
David S. Miller759f89e2007-10-11 03:16:13 -0700802static const struct sparc64_msiq_ops pci_sun4v_msiq_ops = {
803 .get_head = pci_sun4v_get_head,
804 .dequeue_msi = pci_sun4v_dequeue_msi,
805 .set_head = pci_sun4v_set_head,
806 .msi_setup = pci_sun4v_msi_setup,
807 .msi_teardown = pci_sun4v_msi_teardown,
808 .msiq_alloc = pci_sun4v_msiq_alloc,
809 .msiq_free = pci_sun4v_msiq_free,
810 .msiq_build_irq = pci_sun4v_msiq_build_irq,
811};
David S. Millere9870c42007-05-07 23:28:50 -0700812
813static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
814{
David S. Miller759f89e2007-10-11 03:16:13 -0700815 sparc64_pbm_msi_init(pbm, &pci_sun4v_msiq_ops);
David S. Millere9870c42007-05-07 23:28:50 -0700816}
David S. Miller35a17eb2007-02-10 17:41:02 -0800817#else /* CONFIG_PCI_MSI */
818static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
819{
820}
821#endif /* !(CONFIG_PCI_MSI) */
822
Sam Ravnborga1f35ba2008-01-21 17:22:46 -0800823static void __init pci_sun4v_pbm_init(struct pci_controller_info *p,
824 struct device_node *dp, u32 devhandle)
David S. Millerbade5622006-02-09 22:05:54 -0800825{
826 struct pci_pbm_info *pbm;
David S. Millerbade5622006-02-09 22:05:54 -0800827
David S. Miller38337892006-02-12 22:06:53 -0800828 if (devhandle & 0x40)
829 pbm = &p->pbm_B;
830 else
831 pbm = &p->pbm_A;
David S. Millerbade5622006-02-09 22:05:54 -0800832
David S. Miller34768bc2007-05-07 23:06:27 -0700833 pbm->next = pci_pbm_root;
834 pci_pbm_root = pbm;
835
836 pbm->scan_bus = pci_sun4v_scan_bus;
David S. Millerca3dd882007-05-09 02:35:27 -0700837 pbm->pci_ops = &sun4v_pci_ops;
838 pbm->config_space_reg_bits = 12;
David S. Miller34768bc2007-05-07 23:06:27 -0700839
David S. Miller6c108f12007-05-07 23:49:01 -0700840 pbm->index = pci_num_pbms++;
841
David S. Millerbade5622006-02-09 22:05:54 -0800842 pbm->parent = p;
David S. Millere87dc352006-06-21 18:18:47 -0700843 pbm->prom_node = dp;
David S. Millerbade5622006-02-09 22:05:54 -0800844
David S. Miller38337892006-02-12 22:06:53 -0800845 pbm->devhandle = devhandle;
David S. Millerbade5622006-02-09 22:05:54 -0800846
David S. Millere87dc352006-06-21 18:18:47 -0700847 pbm->name = dp->full_name;
David S. Millerbade5622006-02-09 22:05:54 -0800848
David S. Millere87dc352006-06-21 18:18:47 -0700849 printk("%s: SUN4V PCI Bus Module\n", pbm->name);
David S. Millerbade5622006-02-09 22:05:54 -0800850
David S. Miller9fd8b642007-03-08 21:55:49 -0800851 pci_determine_mem_io_space(pbm);
David S. Millerbade5622006-02-09 22:05:54 -0800852
David S. Millercfa06522007-05-07 21:51:41 -0700853 pci_get_pbm_props(pbm);
David S. Millerbade5622006-02-09 22:05:54 -0800854 pci_sun4v_iommu_init(pbm);
David S. Miller35a17eb2007-02-10 17:41:02 -0800855 pci_sun4v_msi_init(pbm);
David S. Millerbade5622006-02-09 22:05:54 -0800856}
857
Sam Ravnborgf0429bf2007-07-20 17:19:56 -0700858void __init sun4v_pci_init(struct device_node *dp, char *model_name)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800859{
David S. Millere01c0d62007-05-25 01:04:15 -0700860 static int hvapi_negotiated = 0;
David S. Millerbade5622006-02-09 22:05:54 -0800861 struct pci_controller_info *p;
David S. Miller34768bc2007-05-07 23:06:27 -0700862 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700863 struct iommu *iommu;
David S. Millere87dc352006-06-21 18:18:47 -0700864 struct property *prop;
865 struct linux_prom64_registers *regs;
David S. Miller7c8f4862006-02-13 21:50:27 -0800866 u32 devhandle;
867 int i;
David S. Miller38337892006-02-12 22:06:53 -0800868
David S. Millere01c0d62007-05-25 01:04:15 -0700869 if (!hvapi_negotiated++) {
870 int err = sun4v_hvapi_register(HV_GRP_PCI,
871 vpci_major,
872 &vpci_minor);
873
874 if (err) {
875 prom_printf("SUN4V_PCI: Could not register hvapi, "
876 "err=%d\n", err);
877 prom_halt();
878 }
879 printk("SUN4V_PCI: Registered hvapi major[%lu] minor[%lu]\n",
880 vpci_major, vpci_minor);
David S. Millerad7ad572007-07-27 22:39:14 -0700881
882 dma_ops = &sun4v_dma_ops;
David S. Millere01c0d62007-05-25 01:04:15 -0700883 }
884
David S. Millere87dc352006-06-21 18:18:47 -0700885 prop = of_find_property(dp, "reg", NULL);
Cyrill Gorcunov75c6d142007-11-20 17:32:19 -0800886 if (!prop) {
887 prom_printf("SUN4V_PCI: Could not find config registers\n");
888 prom_halt();
889 }
David S. Millere87dc352006-06-21 18:18:47 -0700890 regs = prop->value;
891
892 devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
David S. Miller38337892006-02-12 22:06:53 -0800893
David S. Miller34768bc2007-05-07 23:06:27 -0700894 for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
David S. Miller0b522492006-02-12 22:29:36 -0800895 if (pbm->devhandle == (devhandle ^ 0x40)) {
David S. Miller34768bc2007-05-07 23:06:27 -0700896 pci_sun4v_pbm_init(pbm->parent, dp, devhandle);
David S. Miller0b522492006-02-12 22:29:36 -0800897 return;
898 }
David S. Miller38337892006-02-12 22:06:53 -0800899 }
David S. Millerbade5622006-02-09 22:05:54 -0800900
KAMEZAWA Hiroyukia283a522006-04-10 22:52:52 -0700901 for_each_possible_cpu(i) {
David S. Miller7c8f4862006-02-13 21:50:27 -0800902 unsigned long page = get_zeroed_page(GFP_ATOMIC);
903
904 if (!page)
905 goto fatal_memory_error;
906
David S. Millerad7ad572007-07-27 22:39:14 -0700907 per_cpu(iommu_batch, i).pglist = (u64 *) page;
David S. Millerbade5622006-02-09 22:05:54 -0800908 }
David S. Miller7c8f4862006-02-13 21:50:27 -0800909
Yan Burman982c2062006-11-30 17:13:09 -0800910 p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
David S. Miller7c8f4862006-02-13 21:50:27 -0800911 if (!p)
912 goto fatal_memory_error;
913
David S. Miller16ce82d2007-04-26 21:08:21 -0700914 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
David S. Miller7c8f4862006-02-13 21:50:27 -0800915 if (!iommu)
916 goto fatal_memory_error;
917
David S. Millerbade5622006-02-09 22:05:54 -0800918 p->pbm_A.iommu = iommu;
919
David S. Miller16ce82d2007-04-26 21:08:21 -0700920 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
David S. Miller7c8f4862006-02-13 21:50:27 -0800921 if (!iommu)
922 goto fatal_memory_error;
923
David S. Millerbade5622006-02-09 22:05:54 -0800924 p->pbm_B.iommu = iommu;
925
David S. Millere87dc352006-06-21 18:18:47 -0700926 pci_sun4v_pbm_init(p, dp, devhandle);
David S. Miller7c8f4862006-02-13 21:50:27 -0800927 return;
928
929fatal_memory_error:
930 prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
931 prom_halt();
David S. Miller8f6a93a2006-02-09 21:32:07 -0800932}