blob: ddca6c6c0b492709193e1a3c8a86dcb30f884fbf [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
David S. Miller13fa14e2008-02-09 03:11:01 -080092static inline void iommu_batch_new_entry(unsigned long entry)
93{
94 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
95
96 if (p->entry + p->npages == entry)
97 return;
98 if (p->entry != ~0UL)
99 iommu_batch_flush(p);
100 p->entry = entry;
101}
102
David S. Miller6a32fd42006-02-19 22:21:32 -0800103/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -0700104static inline long iommu_batch_add(u64 phys_page)
David S. Miller6a32fd42006-02-19 22:21:32 -0800105{
David S. Millerad7ad572007-07-27 22:39:14 -0700106 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -0800107
108 BUG_ON(p->npages >= PGLIST_NENTS);
109
110 p->pglist[p->npages++] = phys_page;
111 if (p->npages == PGLIST_NENTS)
David S. Millerad7ad572007-07-27 22:39:14 -0700112 return iommu_batch_flush(p);
David S. Miller6a32fd42006-02-19 22:21:32 -0800113
114 return 0;
115}
116
117/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -0700118static inline long iommu_batch_end(void)
David S. Miller6a32fd42006-02-19 22:21:32 -0800119{
David S. Millerad7ad572007-07-27 22:39:14 -0700120 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -0800121
122 BUG_ON(p->npages >= PGLIST_NENTS);
123
David S. Millerad7ad572007-07-27 22:39:14 -0700124 return iommu_batch_flush(p);
David S. Miller6a32fd42006-02-19 22:21:32 -0800125}
David S. Miller18397942006-02-10 00:08:26 -0800126
David S. Millerad7ad572007-07-27 22:39:14 -0700127static void *dma_4v_alloc_coherent(struct device *dev, size_t size,
128 dma_addr_t *dma_addrp, gfp_t gfp)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800129{
David S. Miller16ce82d2007-04-26 21:08:21 -0700130 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800131 unsigned long flags, order, first_page, npages, n;
David S. Miller18397942006-02-10 00:08:26 -0800132 void *ret;
133 long entry;
David S. Miller18397942006-02-10 00:08:26 -0800134
135 size = IO_PAGE_ALIGN(size);
136 order = get_order(size);
David S. Miller6a32fd42006-02-19 22:21:32 -0800137 if (unlikely(order >= MAX_ORDER))
David S. Miller18397942006-02-10 00:08:26 -0800138 return NULL;
139
140 npages = size >> IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800141
David S. Miller42f14232006-05-23 02:07:22 -0700142 first_page = __get_free_pages(gfp, order);
David S. Miller6a32fd42006-02-19 22:21:32 -0800143 if (unlikely(first_page == 0UL))
David S. Miller18397942006-02-10 00:08:26 -0800144 return NULL;
David S. Millere7a04532006-02-15 22:25:27 -0800145
David S. Miller18397942006-02-10 00:08:26 -0800146 memset((char *)first_page, 0, PAGE_SIZE << order);
147
David S. Millerad7ad572007-07-27 22:39:14 -0700148 iommu = dev->archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800149
150 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800151 entry = iommu_range_alloc(dev, iommu, npages, NULL);
David S. Miller18397942006-02-10 00:08:26 -0800152 spin_unlock_irqrestore(&iommu->lock, flags);
153
David S. Millerd2841422008-02-08 18:05:46 -0800154 if (unlikely(entry == DMA_ERROR_CODE))
155 goto range_alloc_fail;
David S. Miller18397942006-02-10 00:08:26 -0800156
157 *dma_addrp = (iommu->page_table_map_base +
158 (entry << IO_PAGE_SHIFT));
159 ret = (void *) first_page;
160 first_page = __pa(first_page);
161
David S. Miller6a32fd42006-02-19 22:21:32 -0800162 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800163
David S. Millerad7ad572007-07-27 22:39:14 -0700164 iommu_batch_start(dev,
165 (HV_PCI_MAP_ATTR_READ |
166 HV_PCI_MAP_ATTR_WRITE),
167 entry);
David S. Miller18397942006-02-10 00:08:26 -0800168
David S. Miller6a32fd42006-02-19 22:21:32 -0800169 for (n = 0; n < npages; n++) {
David S. Millerad7ad572007-07-27 22:39:14 -0700170 long err = iommu_batch_add(first_page + (n * PAGE_SIZE));
David S. Miller6a32fd42006-02-19 22:21:32 -0800171 if (unlikely(err < 0L))
172 goto iommu_map_fail;
173 }
David S. Miller18397942006-02-10 00:08:26 -0800174
David S. Millerad7ad572007-07-27 22:39:14 -0700175 if (unlikely(iommu_batch_end() < 0L))
David S. Miller6a32fd42006-02-19 22:21:32 -0800176 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800177
David S. Miller6a32fd42006-02-19 22:21:32 -0800178 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800179
180 return ret;
David S. Miller6a32fd42006-02-19 22:21:32 -0800181
182iommu_map_fail:
183 /* Interrupts are disabled. */
184 spin_lock(&iommu->lock);
David S. Millerd2841422008-02-08 18:05:46 -0800185 iommu_range_free(iommu, *dma_addrp, npages);
David S. Miller6a32fd42006-02-19 22:21:32 -0800186 spin_unlock_irqrestore(&iommu->lock, flags);
187
David S. Millerd2841422008-02-08 18:05:46 -0800188range_alloc_fail:
David S. Miller6a32fd42006-02-19 22:21:32 -0800189 free_pages(first_page, order);
190 return NULL;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800191}
192
David S. Millerad7ad572007-07-27 22:39:14 -0700193static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu,
194 dma_addr_t dvma)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800195{
David S. Millera2fb23a2007-02-28 23:35:04 -0800196 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700197 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800198 unsigned long flags, order, npages, entry;
199 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800200
201 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700202 iommu = dev->archdata.iommu;
203 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800204 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800205 entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
206
207 spin_lock_irqsave(&iommu->lock, flags);
208
David S. Millerd2841422008-02-08 18:05:46 -0800209 iommu_range_free(iommu, dvma, npages);
David S. Miller18397942006-02-10 00:08:26 -0800210
211 do {
212 unsigned long num;
213
214 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
215 npages);
216 entry += num;
217 npages -= num;
218 } while (npages != 0);
219
220 spin_unlock_irqrestore(&iommu->lock, flags);
221
222 order = get_order(size);
223 if (order < 10)
224 free_pages((unsigned long)cpu, order);
David S. Miller8f6a93a2006-02-09 21:32:07 -0800225}
226
David S. Millerad7ad572007-07-27 22:39:14 -0700227static dma_addr_t dma_4v_map_single(struct device *dev, void *ptr, size_t sz,
228 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800229{
David S. Miller16ce82d2007-04-26 21:08:21 -0700230 struct iommu *iommu;
David S. Miller18397942006-02-10 00:08:26 -0800231 unsigned long flags, npages, oaddr;
David S. Miller7c8f4862006-02-13 21:50:27 -0800232 unsigned long i, base_paddr;
David S. Miller6a32fd42006-02-19 22:21:32 -0800233 u32 bus_addr, ret;
David S. Miller18397942006-02-10 00:08:26 -0800234 unsigned long prot;
235 long entry;
David S. Miller18397942006-02-10 00:08:26 -0800236
David S. Millerad7ad572007-07-27 22:39:14 -0700237 iommu = dev->archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800238
David S. Millerad7ad572007-07-27 22:39:14 -0700239 if (unlikely(direction == DMA_NONE))
David S. Miller18397942006-02-10 00:08:26 -0800240 goto bad;
241
242 oaddr = (unsigned long)ptr;
243 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
244 npages >>= IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800245
246 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800247 entry = iommu_range_alloc(dev, iommu, npages, NULL);
David S. Miller18397942006-02-10 00:08:26 -0800248 spin_unlock_irqrestore(&iommu->lock, flags);
249
David S. Millerd2841422008-02-08 18:05:46 -0800250 if (unlikely(entry == DMA_ERROR_CODE))
David S. Miller18397942006-02-10 00:08:26 -0800251 goto bad;
252
253 bus_addr = (iommu->page_table_map_base +
254 (entry << IO_PAGE_SHIFT));
255 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
256 base_paddr = __pa(oaddr & IO_PAGE_MASK);
257 prot = HV_PCI_MAP_ATTR_READ;
David S. Millerad7ad572007-07-27 22:39:14 -0700258 if (direction != DMA_TO_DEVICE)
David S. Miller18397942006-02-10 00:08:26 -0800259 prot |= HV_PCI_MAP_ATTR_WRITE;
260
David S. Miller6a32fd42006-02-19 22:21:32 -0800261 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800262
David S. Millerad7ad572007-07-27 22:39:14 -0700263 iommu_batch_start(dev, prot, entry);
David S. Miller18397942006-02-10 00:08:26 -0800264
David S. Miller6a32fd42006-02-19 22:21:32 -0800265 for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
David S. Millerad7ad572007-07-27 22:39:14 -0700266 long err = iommu_batch_add(base_paddr);
David S. Miller6a32fd42006-02-19 22:21:32 -0800267 if (unlikely(err < 0L))
268 goto iommu_map_fail;
269 }
David S. Millerad7ad572007-07-27 22:39:14 -0700270 if (unlikely(iommu_batch_end() < 0L))
David S. Miller6a32fd42006-02-19 22:21:32 -0800271 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800272
David S. Miller6a32fd42006-02-19 22:21:32 -0800273 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800274
275 return ret;
276
277bad:
278 if (printk_ratelimit())
279 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700280 return DMA_ERROR_CODE;
David S. Miller6a32fd42006-02-19 22:21:32 -0800281
282iommu_map_fail:
283 /* Interrupts are disabled. */
284 spin_lock(&iommu->lock);
David S. Millerd2841422008-02-08 18:05:46 -0800285 iommu_range_free(iommu, bus_addr, npages);
David S. Miller6a32fd42006-02-19 22:21:32 -0800286 spin_unlock_irqrestore(&iommu->lock, flags);
287
David S. Millerad7ad572007-07-27 22:39:14 -0700288 return DMA_ERROR_CODE;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800289}
290
David S. Millerad7ad572007-07-27 22:39:14 -0700291static void dma_4v_unmap_single(struct device *dev, dma_addr_t bus_addr,
292 size_t sz, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800293{
David S. Millera2fb23a2007-02-28 23:35:04 -0800294 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700295 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800296 unsigned long flags, npages;
David S. Miller18397942006-02-10 00:08:26 -0800297 long entry;
David S. Miller7c8f4862006-02-13 21:50:27 -0800298 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800299
David S. Millerad7ad572007-07-27 22:39:14 -0700300 if (unlikely(direction == DMA_NONE)) {
David S. Miller18397942006-02-10 00:08:26 -0800301 if (printk_ratelimit())
302 WARN_ON(1);
303 return;
304 }
305
David S. Millerad7ad572007-07-27 22:39:14 -0700306 iommu = dev->archdata.iommu;
307 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800308 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800309
310 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
311 npages >>= IO_PAGE_SHIFT;
312 bus_addr &= IO_PAGE_MASK;
313
314 spin_lock_irqsave(&iommu->lock, flags);
315
David S. Millerd2841422008-02-08 18:05:46 -0800316 iommu_range_free(iommu, bus_addr, npages);
David S. Miller18397942006-02-10 00:08:26 -0800317
David S. Millerd2841422008-02-08 18:05:46 -0800318 entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800319 do {
320 unsigned long num;
321
322 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
323 npages);
324 entry += num;
325 npages -= num;
326 } while (npages != 0);
327
328 spin_unlock_irqrestore(&iommu->lock, flags);
329}
330
David S. Millerad7ad572007-07-27 22:39:14 -0700331static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
332 int nelems, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800333{
David S. Miller13fa14e2008-02-09 03:11:01 -0800334 struct scatterlist *s, *outs, *segstart;
335 unsigned long flags, handle, prot;
336 dma_addr_t dma_next = 0, dma_addr;
337 unsigned int max_seg_size;
338 int outcount, incount, i;
David S. Miller16ce82d2007-04-26 21:08:21 -0700339 struct iommu *iommu;
David S. Miller13fa14e2008-02-09 03:11:01 -0800340 long err;
David S. Miller18397942006-02-10 00:08:26 -0800341
David S. Miller13fa14e2008-02-09 03:11:01 -0800342 BUG_ON(direction == DMA_NONE);
David S. Miller18397942006-02-10 00:08:26 -0800343
David S. Millerad7ad572007-07-27 22:39:14 -0700344 iommu = dev->archdata.iommu;
David S. Miller13fa14e2008-02-09 03:11:01 -0800345 if (nelems == 0 || !iommu)
346 return 0;
David S. Miller18397942006-02-10 00:08:26 -0800347
David S. Miller18397942006-02-10 00:08:26 -0800348 prot = HV_PCI_MAP_ATTR_READ;
David S. Millerad7ad572007-07-27 22:39:14 -0700349 if (direction != DMA_TO_DEVICE)
David S. Miller18397942006-02-10 00:08:26 -0800350 prot |= HV_PCI_MAP_ATTR_WRITE;
351
David S. Miller13fa14e2008-02-09 03:11:01 -0800352 outs = s = segstart = &sglist[0];
353 outcount = 1;
354 incount = nelems;
355 handle = 0;
David S. Miller38192d52008-02-06 03:50:26 -0800356
David S. Miller13fa14e2008-02-09 03:11:01 -0800357 /* Init first segment length for backout at failure */
358 outs->dma_length = 0;
David S. Miller38192d52008-02-06 03:50:26 -0800359
David S. Miller13fa14e2008-02-09 03:11:01 -0800360 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller38192d52008-02-06 03:50:26 -0800361
David S. Miller13fa14e2008-02-09 03:11:01 -0800362 iommu_batch_start(dev, prot, ~0UL);
David S. Miller38192d52008-02-06 03:50:26 -0800363
David S. Miller13fa14e2008-02-09 03:11:01 -0800364 max_seg_size = dma_get_max_seg_size(dev);
365 for_each_sg(sglist, s, nelems, i) {
366 unsigned long paddr, npages, entry, slen;
David S. Miller38192d52008-02-06 03:50:26 -0800367
David S. Miller13fa14e2008-02-09 03:11:01 -0800368 slen = s->length;
369 /* Sanity check */
370 if (slen == 0) {
371 dma_next = 0;
372 continue;
David S. Miller38192d52008-02-06 03:50:26 -0800373 }
David S. Miller13fa14e2008-02-09 03:11:01 -0800374 /* Allocate iommu entries for that segment */
375 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
376 npages = iommu_num_pages(paddr, slen);
377 entry = iommu_range_alloc(dev, iommu, npages, &handle);
378
379 /* Handle failure */
380 if (unlikely(entry == DMA_ERROR_CODE)) {
381 if (printk_ratelimit())
382 printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
383 " npages %lx\n", iommu, paddr, npages);
384 goto iommu_map_failed;
385 }
386
387 iommu_batch_new_entry(entry);
388
389 /* Convert entry to a dma_addr_t */
390 dma_addr = iommu->page_table_map_base +
391 (entry << IO_PAGE_SHIFT);
392 dma_addr |= (s->offset & ~IO_PAGE_MASK);
393
394 /* Insert into HW table */
395 paddr &= IO_PAGE_MASK;
396 while (npages--) {
397 err = iommu_batch_add(paddr);
398 if (unlikely(err < 0L))
399 goto iommu_map_failed;
400 paddr += IO_PAGE_SIZE;
401 }
402
403 /* If we are in an open segment, try merging */
404 if (segstart != s) {
405 /* We cannot merge if:
406 * - allocated dma_addr isn't contiguous to previous allocation
407 */
408 if ((dma_addr != dma_next) ||
409 (outs->dma_length + s->length > max_seg_size)) {
410 /* Can't merge: create a new segment */
411 segstart = s;
412 outcount++;
413 outs = sg_next(outs);
414 } else {
415 outs->dma_length += s->length;
416 }
417 }
418
419 if (segstart == s) {
420 /* This is a new segment, fill entries */
421 outs->dma_address = dma_addr;
422 outs->dma_length = slen;
423 }
424
425 /* Calculate next page pointer for contiguous check */
426 dma_next = dma_addr + slen;
David S. Miller38192d52008-02-06 03:50:26 -0800427 }
428
429 err = iommu_batch_end();
430
David S. Miller6a32fd42006-02-19 22:21:32 -0800431 if (unlikely(err < 0L))
432 goto iommu_map_failed;
David S. Miller18397942006-02-10 00:08:26 -0800433
David S. Miller13fa14e2008-02-09 03:11:01 -0800434 spin_unlock_irqrestore(&iommu->lock, flags);
David S. Miller18397942006-02-10 00:08:26 -0800435
David S. Miller13fa14e2008-02-09 03:11:01 -0800436 if (outcount < incount) {
437 outs = sg_next(outs);
438 outs->dma_address = DMA_ERROR_CODE;
439 outs->dma_length = 0;
440 }
441
442 return outcount;
David S. Miller6a32fd42006-02-19 22:21:32 -0800443
444iommu_map_failed:
David S. Miller13fa14e2008-02-09 03:11:01 -0800445 for_each_sg(sglist, s, nelems, i) {
446 if (s->dma_length != 0) {
447 unsigned long vaddr, npages;
448
449 vaddr = s->dma_address & IO_PAGE_MASK;
450 npages = iommu_num_pages(s->dma_address, s->dma_length);
451 iommu_range_free(iommu, vaddr, npages);
452 /* XXX demap? XXX */
453 s->dma_address = DMA_ERROR_CODE;
454 s->dma_length = 0;
455 }
456 if (s == outs)
457 break;
458 }
David S. Miller6a32fd42006-02-19 22:21:32 -0800459 spin_unlock_irqrestore(&iommu->lock, flags);
460
461 return 0;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800462}
463
David S. Millerad7ad572007-07-27 22:39:14 -0700464static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
465 int nelems, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800466{
David S. Millera2fb23a2007-02-28 23:35:04 -0800467 struct pci_pbm_info *pbm;
David S. Miller13fa14e2008-02-09 03:11:01 -0800468 struct scatterlist *sg;
David S. Miller38192d52008-02-06 03:50:26 -0800469 struct iommu *iommu;
David S. Miller13fa14e2008-02-09 03:11:01 -0800470 unsigned long flags;
471 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800472
David S. Miller13fa14e2008-02-09 03:11:01 -0800473 BUG_ON(direction == DMA_NONE);
David S. Miller18397942006-02-10 00:08:26 -0800474
David S. Millerad7ad572007-07-27 22:39:14 -0700475 iommu = dev->archdata.iommu;
476 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800477 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800478
David S. Miller18397942006-02-10 00:08:26 -0800479 spin_lock_irqsave(&iommu->lock, flags);
480
David S. Miller13fa14e2008-02-09 03:11:01 -0800481 sg = sglist;
482 while (nelems--) {
483 dma_addr_t dma_handle = sg->dma_address;
484 unsigned int len = sg->dma_length;
485 unsigned long npages, entry;
David S. Miller18397942006-02-10 00:08:26 -0800486
David S. Miller13fa14e2008-02-09 03:11:01 -0800487 if (!len)
488 break;
489 npages = iommu_num_pages(dma_handle, len);
490 iommu_range_free(iommu, dma_handle, npages);
David S. Miller18397942006-02-10 00:08:26 -0800491
David S. Miller13fa14e2008-02-09 03:11:01 -0800492 entry = ((dma_handle - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
493 while (npages) {
494 unsigned long num;
495
496 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
497 npages);
498 entry += num;
499 npages -= num;
500 }
501
502 sg = sg_next(sg);
503 }
David S. Miller18397942006-02-10 00:08:26 -0800504
505 spin_unlock_irqrestore(&iommu->lock, flags);
David S. Miller8f6a93a2006-02-09 21:32:07 -0800506}
507
David S. Millerad7ad572007-07-27 22:39:14 -0700508static void dma_4v_sync_single_for_cpu(struct device *dev,
509 dma_addr_t bus_addr, size_t sz,
510 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800511{
David S. Miller18397942006-02-10 00:08:26 -0800512 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800513}
514
David S. Millerad7ad572007-07-27 22:39:14 -0700515static void dma_4v_sync_sg_for_cpu(struct device *dev,
516 struct scatterlist *sglist, int nelems,
517 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800518{
David S. Miller18397942006-02-10 00:08:26 -0800519 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800520}
521
David S. Millerad7ad572007-07-27 22:39:14 -0700522const struct dma_ops sun4v_dma_ops = {
523 .alloc_coherent = dma_4v_alloc_coherent,
524 .free_coherent = dma_4v_free_coherent,
525 .map_single = dma_4v_map_single,
526 .unmap_single = dma_4v_unmap_single,
527 .map_sg = dma_4v_map_sg,
528 .unmap_sg = dma_4v_unmap_sg,
529 .sync_single_for_cpu = dma_4v_sync_single_for_cpu,
530 .sync_sg_for_cpu = dma_4v_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800531};
532
Sam Ravnborga1f35ba2008-01-21 17:22:46 -0800533static void __init pci_sun4v_scan_bus(struct pci_pbm_info *pbm)
David S. Millerbade5622006-02-09 22:05:54 -0800534{
David S. Millere87dc352006-06-21 18:18:47 -0700535 struct property *prop;
536 struct device_node *dp;
537
David S. Miller34768bc2007-05-07 23:06:27 -0700538 dp = pbm->prom_node;
539 prop = of_find_property(dp, "66mhz-capable", NULL);
540 pbm->is_66mhz_capable = (prop != NULL);
541 pbm->pci_bus = pci_scan_one_pbm(pbm);
David S. Millerc2609262006-02-12 22:18:52 -0800542
543 /* XXX register error interrupt handlers XXX */
David S. Millerbade5622006-02-09 22:05:54 -0800544}
545
Adrian Bunk4c622252008-02-05 03:01:43 -0800546static unsigned long __init probe_existing_entries(struct pci_pbm_info *pbm,
547 struct iommu *iommu)
David S. Miller18397942006-02-10 00:08:26 -0800548{
David S. Miller9b3627f2007-04-24 23:51:18 -0700549 struct iommu_arena *arena = &iommu->arena;
David S. Millere7a04532006-02-15 22:25:27 -0800550 unsigned long i, cnt = 0;
David S. Miller7c8f4862006-02-13 21:50:27 -0800551 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800552
553 devhandle = pbm->devhandle;
554 for (i = 0; i < arena->limit; i++) {
555 unsigned long ret, io_attrs, ra;
556
557 ret = pci_sun4v_iommu_getmap(devhandle,
558 HV_PCI_TSBID(0, i),
559 &io_attrs, &ra);
David S. Millere7a04532006-02-15 22:25:27 -0800560 if (ret == HV_EOK) {
David S. Millerc2a5a462006-06-22 00:01:56 -0700561 if (page_in_phys_avail(ra)) {
562 pci_sun4v_iommu_demap(devhandle,
563 HV_PCI_TSBID(0, i), 1);
564 } else {
565 cnt++;
566 __set_bit(i, arena->map);
567 }
David S. Millere7a04532006-02-15 22:25:27 -0800568 }
David S. Miller18397942006-02-10 00:08:26 -0800569 }
David S. Millere7a04532006-02-15 22:25:27 -0800570
571 return cnt;
David S. Miller18397942006-02-10 00:08:26 -0800572}
573
Adrian Bunk4c622252008-02-05 03:01:43 -0800574static void __init pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
David S. Millerbade5622006-02-09 22:05:54 -0800575{
David S. Miller16ce82d2007-04-26 21:08:21 -0700576 struct iommu *iommu = pbm->iommu;
David S. Millere87dc352006-06-21 18:18:47 -0700577 struct property *prop;
David S. Miller59db8102007-05-23 18:00:46 -0700578 unsigned long num_tsb_entries, sz, tsbsize;
David S. Miller18397942006-02-10 00:08:26 -0800579 u32 vdma[2], dma_mask, dma_offset;
David S. Miller18397942006-02-10 00:08:26 -0800580
David S. Millere87dc352006-06-21 18:18:47 -0700581 prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
582 if (prop) {
583 u32 *val = prop->value;
584
585 vdma[0] = val[0];
586 vdma[1] = val[1];
587 } else {
David S. Miller18397942006-02-10 00:08:26 -0800588 /* No property, use default values. */
589 vdma[0] = 0x80000000;
590 vdma[1] = 0x80000000;
591 }
592
David S. Miller59db8102007-05-23 18:00:46 -0700593 if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
594 prom_printf("PCI-SUN4V: strange virtual-dma[%08x:%08x].\n",
595 vdma[0], vdma[1]);
596 prom_halt();
David S. Miller18397942006-02-10 00:08:26 -0800597 };
598
David S. Miller59db8102007-05-23 18:00:46 -0700599 dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
600 num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
601 tsbsize = num_tsb_entries * sizeof(iopte_t);
David S. Miller18397942006-02-10 00:08:26 -0800602
603 dma_offset = vdma[0];
604
605 /* Setup initial software IOMMU state. */
606 spin_lock_init(&iommu->lock);
607 iommu->ctx_lowest_free = 1;
608 iommu->page_table_map_base = dma_offset;
609 iommu->dma_addr_mask = dma_mask;
610
611 /* Allocate and initialize the free area map. */
David S. Miller59db8102007-05-23 18:00:46 -0700612 sz = (num_tsb_entries + 7) / 8;
David S. Miller18397942006-02-10 00:08:26 -0800613 sz = (sz + 7UL) & ~7UL;
Yan Burman982c2062006-11-30 17:13:09 -0800614 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
David S. Miller18397942006-02-10 00:08:26 -0800615 if (!iommu->arena.map) {
616 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
617 prom_halt();
618 }
David S. Miller18397942006-02-10 00:08:26 -0800619 iommu->arena.limit = num_tsb_entries;
620
David S. Millere7a04532006-02-15 22:25:27 -0800621 sz = probe_existing_entries(pbm, iommu);
David S. Millerc2a5a462006-06-22 00:01:56 -0700622 if (sz)
623 printk("%s: Imported %lu TSB entries from OBP\n",
624 pbm->name, sz);
David S. Millerbade5622006-02-09 22:05:54 -0800625}
626
David S. Miller35a17eb2007-02-10 17:41:02 -0800627#ifdef CONFIG_PCI_MSI
628struct pci_sun4v_msiq_entry {
629 u64 version_type;
630#define MSIQ_VERSION_MASK 0xffffffff00000000UL
631#define MSIQ_VERSION_SHIFT 32
632#define MSIQ_TYPE_MASK 0x00000000000000ffUL
633#define MSIQ_TYPE_SHIFT 0
634#define MSIQ_TYPE_NONE 0x00
635#define MSIQ_TYPE_MSG 0x01
636#define MSIQ_TYPE_MSI32 0x02
637#define MSIQ_TYPE_MSI64 0x03
638#define MSIQ_TYPE_INTX 0x08
639#define MSIQ_TYPE_NONE2 0xff
640
641 u64 intx_sysino;
642 u64 reserved1;
643 u64 stick;
644 u64 req_id; /* bus/device/func */
645#define MSIQ_REQID_BUS_MASK 0xff00UL
646#define MSIQ_REQID_BUS_SHIFT 8
647#define MSIQ_REQID_DEVICE_MASK 0x00f8UL
648#define MSIQ_REQID_DEVICE_SHIFT 3
649#define MSIQ_REQID_FUNC_MASK 0x0007UL
650#define MSIQ_REQID_FUNC_SHIFT 0
651
652 u64 msi_address;
653
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700654 /* The format of this value is message type dependent.
David S. Miller35a17eb2007-02-10 17:41:02 -0800655 * For MSI bits 15:0 are the data from the MSI packet.
656 * For MSI-X bits 31:0 are the data from the MSI packet.
657 * For MSG, the message code and message routing code where:
658 * bits 39:32 is the bus/device/fn of the msg target-id
659 * bits 18:16 is the message routing code
660 * bits 7:0 is the message code
661 * For INTx the low order 2-bits are:
662 * 00 - INTA
663 * 01 - INTB
664 * 10 - INTC
665 * 11 - INTD
666 */
667 u64 msi_data;
668
669 u64 reserved2;
670};
671
David S. Miller759f89e2007-10-11 03:16:13 -0700672static int pci_sun4v_get_head(struct pci_pbm_info *pbm, unsigned long msiqid,
673 unsigned long *head)
David S. Miller35a17eb2007-02-10 17:41:02 -0800674{
David S. Miller759f89e2007-10-11 03:16:13 -0700675 unsigned long err, limit;
David S. Miller35a17eb2007-02-10 17:41:02 -0800676
David S. Miller759f89e2007-10-11 03:16:13 -0700677 err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, head);
David S. Miller35a17eb2007-02-10 17:41:02 -0800678 if (unlikely(err))
David S. Miller759f89e2007-10-11 03:16:13 -0700679 return -ENXIO;
David S. Miller35a17eb2007-02-10 17:41:02 -0800680
David S. Miller759f89e2007-10-11 03:16:13 -0700681 limit = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
682 if (unlikely(*head >= limit))
683 return -EFBIG;
David S. Miller35a17eb2007-02-10 17:41:02 -0800684
685 return 0;
686}
687
David S. Miller759f89e2007-10-11 03:16:13 -0700688static int pci_sun4v_dequeue_msi(struct pci_pbm_info *pbm,
689 unsigned long msiqid, unsigned long *head,
690 unsigned long *msi)
David S. Miller35a17eb2007-02-10 17:41:02 -0800691{
David S. Miller759f89e2007-10-11 03:16:13 -0700692 struct pci_sun4v_msiq_entry *ep;
693 unsigned long err, type;
694
695 /* Note: void pointer arithmetic, 'head' is a byte offset */
696 ep = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
697 (pbm->msiq_ent_count *
698 sizeof(struct pci_sun4v_msiq_entry))) +
699 *head);
700
701 if ((ep->version_type & MSIQ_TYPE_MASK) == 0)
702 return 0;
703
704 type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
705 if (unlikely(type != MSIQ_TYPE_MSI32 &&
706 type != MSIQ_TYPE_MSI64))
707 return -EINVAL;
708
709 *msi = ep->msi_data;
710
711 err = pci_sun4v_msi_setstate(pbm->devhandle,
712 ep->msi_data /* msi_num */,
713 HV_MSISTATE_IDLE);
714 if (unlikely(err))
715 return -ENXIO;
716
717 /* Clear the entry. */
718 ep->version_type &= ~MSIQ_TYPE_MASK;
719
720 (*head) += sizeof(struct pci_sun4v_msiq_entry);
721 if (*head >=
722 (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry)))
723 *head = 0;
724
725 return 1;
David S. Miller35a17eb2007-02-10 17:41:02 -0800726}
727
David S. Miller759f89e2007-10-11 03:16:13 -0700728static int pci_sun4v_set_head(struct pci_pbm_info *pbm, unsigned long msiqid,
729 unsigned long head)
730{
731 unsigned long err;
732
733 err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
734 if (unlikely(err))
735 return -EINVAL;
736
737 return 0;
738}
739
740static int pci_sun4v_msi_setup(struct pci_pbm_info *pbm, unsigned long msiqid,
741 unsigned long msi, int is_msi64)
742{
743 if (pci_sun4v_msi_setmsiq(pbm->devhandle, msi, msiqid,
744 (is_msi64 ?
745 HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
746 return -ENXIO;
747 if (pci_sun4v_msi_setstate(pbm->devhandle, msi, HV_MSISTATE_IDLE))
748 return -ENXIO;
749 if (pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_VALID))
750 return -ENXIO;
751 return 0;
752}
753
754static int pci_sun4v_msi_teardown(struct pci_pbm_info *pbm, unsigned long msi)
755{
756 unsigned long err, msiqid;
757
758 err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi, &msiqid);
759 if (err)
760 return -ENXIO;
761
762 pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_INVALID);
763
764 return 0;
765}
766
767static int pci_sun4v_msiq_alloc(struct pci_pbm_info *pbm)
David S. Miller35a17eb2007-02-10 17:41:02 -0800768{
769 unsigned long q_size, alloc_size, pages, order;
770 int i;
771
772 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
773 alloc_size = (pbm->msiq_num * q_size);
774 order = get_order(alloc_size);
775 pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
776 if (pages == 0UL) {
777 printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
778 order);
779 return -ENOMEM;
780 }
781 memset((char *)pages, 0, PAGE_SIZE << order);
782 pbm->msi_queues = (void *) pages;
783
784 for (i = 0; i < pbm->msiq_num; i++) {
785 unsigned long err, base = __pa(pages + (i * q_size));
786 unsigned long ret1, ret2;
787
788 err = pci_sun4v_msiq_conf(pbm->devhandle,
789 pbm->msiq_first + i,
790 base, pbm->msiq_ent_count);
791 if (err) {
792 printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
793 err);
794 goto h_error;
795 }
796
797 err = pci_sun4v_msiq_info(pbm->devhandle,
798 pbm->msiq_first + i,
799 &ret1, &ret2);
800 if (err) {
801 printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
802 err);
803 goto h_error;
804 }
805 if (ret1 != base || ret2 != pbm->msiq_ent_count) {
806 printk(KERN_ERR "MSI: Bogus qconf "
807 "expected[%lx:%x] got[%lx:%lx]\n",
808 base, pbm->msiq_ent_count,
809 ret1, ret2);
810 goto h_error;
811 }
812 }
813
814 return 0;
815
816h_error:
817 free_pages(pages, order);
818 return -EINVAL;
819}
820
David S. Miller759f89e2007-10-11 03:16:13 -0700821static void pci_sun4v_msiq_free(struct pci_pbm_info *pbm)
David S. Miller35a17eb2007-02-10 17:41:02 -0800822{
David S. Miller759f89e2007-10-11 03:16:13 -0700823 unsigned long q_size, alloc_size, pages, order;
David S. Miller35a17eb2007-02-10 17:41:02 -0800824 int i;
825
David S. Miller759f89e2007-10-11 03:16:13 -0700826 for (i = 0; i < pbm->msiq_num; i++) {
827 unsigned long msiqid = pbm->msiq_first + i;
828
829 (void) pci_sun4v_msiq_conf(pbm->devhandle, msiqid, 0UL, 0);
David S. Miller35a17eb2007-02-10 17:41:02 -0800830 }
831
David S. Miller759f89e2007-10-11 03:16:13 -0700832 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
833 alloc_size = (pbm->msiq_num * q_size);
834 order = get_order(alloc_size);
835
836 pages = (unsigned long) pbm->msi_queues;
837
838 free_pages(pages, order);
839
840 pbm->msi_queues = NULL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800841}
842
David S. Miller759f89e2007-10-11 03:16:13 -0700843static int pci_sun4v_msiq_build_irq(struct pci_pbm_info *pbm,
844 unsigned long msiqid,
845 unsigned long devino)
David S. Miller35a17eb2007-02-10 17:41:02 -0800846{
David S. Miller759f89e2007-10-11 03:16:13 -0700847 unsigned int virt_irq = sun4v_build_irq(pbm->devhandle, devino);
David S. Miller35a17eb2007-02-10 17:41:02 -0800848
David S. Miller759f89e2007-10-11 03:16:13 -0700849 if (!virt_irq)
850 return -ENOMEM;
David S. Miller35a17eb2007-02-10 17:41:02 -0800851
David S. Miller35a17eb2007-02-10 17:41:02 -0800852 if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
David S. Miller759f89e2007-10-11 03:16:13 -0700853 return -EINVAL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800854 if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
David S. Miller759f89e2007-10-11 03:16:13 -0700855 return -EINVAL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800856
David S. Miller759f89e2007-10-11 03:16:13 -0700857 return virt_irq;
David S. Miller35a17eb2007-02-10 17:41:02 -0800858}
859
David S. Miller759f89e2007-10-11 03:16:13 -0700860static const struct sparc64_msiq_ops pci_sun4v_msiq_ops = {
861 .get_head = pci_sun4v_get_head,
862 .dequeue_msi = pci_sun4v_dequeue_msi,
863 .set_head = pci_sun4v_set_head,
864 .msi_setup = pci_sun4v_msi_setup,
865 .msi_teardown = pci_sun4v_msi_teardown,
866 .msiq_alloc = pci_sun4v_msiq_alloc,
867 .msiq_free = pci_sun4v_msiq_free,
868 .msiq_build_irq = pci_sun4v_msiq_build_irq,
869};
David S. Millere9870c42007-05-07 23:28:50 -0700870
871static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
872{
David S. Miller759f89e2007-10-11 03:16:13 -0700873 sparc64_pbm_msi_init(pbm, &pci_sun4v_msiq_ops);
David S. Millere9870c42007-05-07 23:28:50 -0700874}
David S. Miller35a17eb2007-02-10 17:41:02 -0800875#else /* CONFIG_PCI_MSI */
876static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
877{
878}
879#endif /* !(CONFIG_PCI_MSI) */
880
Sam Ravnborga1f35ba2008-01-21 17:22:46 -0800881static void __init pci_sun4v_pbm_init(struct pci_controller_info *p,
882 struct device_node *dp, u32 devhandle)
David S. Millerbade5622006-02-09 22:05:54 -0800883{
884 struct pci_pbm_info *pbm;
David S. Millerbade5622006-02-09 22:05:54 -0800885
David S. Miller38337892006-02-12 22:06:53 -0800886 if (devhandle & 0x40)
887 pbm = &p->pbm_B;
888 else
889 pbm = &p->pbm_A;
David S. Millerbade5622006-02-09 22:05:54 -0800890
David S. Miller34768bc2007-05-07 23:06:27 -0700891 pbm->next = pci_pbm_root;
892 pci_pbm_root = pbm;
893
894 pbm->scan_bus = pci_sun4v_scan_bus;
David S. Millerca3dd882007-05-09 02:35:27 -0700895 pbm->pci_ops = &sun4v_pci_ops;
896 pbm->config_space_reg_bits = 12;
David S. Miller34768bc2007-05-07 23:06:27 -0700897
David S. Miller6c108f12007-05-07 23:49:01 -0700898 pbm->index = pci_num_pbms++;
899
David S. Millerbade5622006-02-09 22:05:54 -0800900 pbm->parent = p;
David S. Millere87dc352006-06-21 18:18:47 -0700901 pbm->prom_node = dp;
David S. Millerbade5622006-02-09 22:05:54 -0800902
David S. Miller38337892006-02-12 22:06:53 -0800903 pbm->devhandle = devhandle;
David S. Millerbade5622006-02-09 22:05:54 -0800904
David S. Millere87dc352006-06-21 18:18:47 -0700905 pbm->name = dp->full_name;
David S. Millerbade5622006-02-09 22:05:54 -0800906
David S. Millere87dc352006-06-21 18:18:47 -0700907 printk("%s: SUN4V PCI Bus Module\n", pbm->name);
David S. Millerbade5622006-02-09 22:05:54 -0800908
David S. Miller9fd8b642007-03-08 21:55:49 -0800909 pci_determine_mem_io_space(pbm);
David S. Millerbade5622006-02-09 22:05:54 -0800910
David S. Millercfa06522007-05-07 21:51:41 -0700911 pci_get_pbm_props(pbm);
David S. Millerbade5622006-02-09 22:05:54 -0800912 pci_sun4v_iommu_init(pbm);
David S. Miller35a17eb2007-02-10 17:41:02 -0800913 pci_sun4v_msi_init(pbm);
David S. Millerbade5622006-02-09 22:05:54 -0800914}
915
Sam Ravnborgf0429bf2007-07-20 17:19:56 -0700916void __init sun4v_pci_init(struct device_node *dp, char *model_name)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800917{
David S. Millere01c0d62007-05-25 01:04:15 -0700918 static int hvapi_negotiated = 0;
David S. Millerbade5622006-02-09 22:05:54 -0800919 struct pci_controller_info *p;
David S. Miller34768bc2007-05-07 23:06:27 -0700920 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700921 struct iommu *iommu;
David S. Millere87dc352006-06-21 18:18:47 -0700922 struct property *prop;
923 struct linux_prom64_registers *regs;
David S. Miller7c8f4862006-02-13 21:50:27 -0800924 u32 devhandle;
925 int i;
David S. Miller38337892006-02-12 22:06:53 -0800926
David S. Millere01c0d62007-05-25 01:04:15 -0700927 if (!hvapi_negotiated++) {
928 int err = sun4v_hvapi_register(HV_GRP_PCI,
929 vpci_major,
930 &vpci_minor);
931
932 if (err) {
933 prom_printf("SUN4V_PCI: Could not register hvapi, "
934 "err=%d\n", err);
935 prom_halt();
936 }
937 printk("SUN4V_PCI: Registered hvapi major[%lu] minor[%lu]\n",
938 vpci_major, vpci_minor);
David S. Millerad7ad572007-07-27 22:39:14 -0700939
940 dma_ops = &sun4v_dma_ops;
David S. Millere01c0d62007-05-25 01:04:15 -0700941 }
942
David S. Millere87dc352006-06-21 18:18:47 -0700943 prop = of_find_property(dp, "reg", NULL);
Cyrill Gorcunov75c6d142007-11-20 17:32:19 -0800944 if (!prop) {
945 prom_printf("SUN4V_PCI: Could not find config registers\n");
946 prom_halt();
947 }
David S. Millere87dc352006-06-21 18:18:47 -0700948 regs = prop->value;
949
950 devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
David S. Miller38337892006-02-12 22:06:53 -0800951
David S. Miller34768bc2007-05-07 23:06:27 -0700952 for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
David S. Miller0b522492006-02-12 22:29:36 -0800953 if (pbm->devhandle == (devhandle ^ 0x40)) {
David S. Miller34768bc2007-05-07 23:06:27 -0700954 pci_sun4v_pbm_init(pbm->parent, dp, devhandle);
David S. Miller0b522492006-02-12 22:29:36 -0800955 return;
956 }
David S. Miller38337892006-02-12 22:06:53 -0800957 }
David S. Millerbade5622006-02-09 22:05:54 -0800958
KAMEZAWA Hiroyukia283a522006-04-10 22:52:52 -0700959 for_each_possible_cpu(i) {
David S. Miller7c8f4862006-02-13 21:50:27 -0800960 unsigned long page = get_zeroed_page(GFP_ATOMIC);
961
962 if (!page)
963 goto fatal_memory_error;
964
David S. Millerad7ad572007-07-27 22:39:14 -0700965 per_cpu(iommu_batch, i).pglist = (u64 *) page;
David S. Millerbade5622006-02-09 22:05:54 -0800966 }
David S. Miller7c8f4862006-02-13 21:50:27 -0800967
Yan Burman982c2062006-11-30 17:13:09 -0800968 p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
David S. Miller7c8f4862006-02-13 21:50:27 -0800969 if (!p)
970 goto fatal_memory_error;
971
David S. Miller16ce82d2007-04-26 21:08:21 -0700972 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
David S. Miller7c8f4862006-02-13 21:50:27 -0800973 if (!iommu)
974 goto fatal_memory_error;
975
David S. Millerbade5622006-02-09 22:05:54 -0800976 p->pbm_A.iommu = iommu;
977
David S. Miller16ce82d2007-04-26 21:08:21 -0700978 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
David S. Miller7c8f4862006-02-13 21:50:27 -0800979 if (!iommu)
980 goto fatal_memory_error;
981
David S. Millerbade5622006-02-09 22:05:54 -0800982 p->pbm_B.iommu = iommu;
983
David S. Millere87dc352006-06-21 18:18:47 -0700984 pci_sun4v_pbm_init(p, dp, devhandle);
David S. Miller7c8f4862006-02-13 21:50:27 -0800985 return;
986
987fatal_memory_error:
988 prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
989 prom_halt();
David S. Miller8f6a93a2006-02-09 21:32:07 -0800990}