blob: 6bed2f6bf7cd3ceebef0731293469f3a07706e2e [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. Miller3822b502008-08-30 02:50:29 -070016#include <linux/of_device.h>
David S. Miller8f6a93a2006-02-09 21:32:07 -080017
David S. Miller8f6a93a2006-02-09 21:32:07 -080018#include <asm/iommu.h>
19#include <asm/irq.h>
David S. Miller8f6a93a2006-02-09 21:32:07 -080020#include <asm/hypervisor.h>
David S. Millere87dc352006-06-21 18:18:47 -070021#include <asm/prom.h>
David S. Miller8f6a93a2006-02-09 21:32:07 -080022
23#include "pci_impl.h"
24#include "iommu_common.h"
25
David S. Millerbade5622006-02-09 22:05:54 -080026#include "pci_sun4v.h"
27
David S. Miller3822b502008-08-30 02:50:29 -070028#define DRIVER_NAME "pci_sun4v"
29#define PFX DRIVER_NAME ": "
30
David S. Millere01c0d62007-05-25 01:04:15 -070031static unsigned long vpci_major = 1;
32static unsigned long vpci_minor = 1;
33
David S. Miller7c8f4862006-02-13 21:50:27 -080034#define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
David S. Miller18397942006-02-10 00:08:26 -080035
David S. Miller16ce82d2007-04-26 21:08:21 -070036struct iommu_batch {
David S. Millerad7ad572007-07-27 22:39:14 -070037 struct device *dev; /* Device mapping is for. */
David S. Miller6a32fd42006-02-19 22:21:32 -080038 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. Miller18397942006-02-10 00:08:26 -080042};
43
David S. Millerad7ad572007-07-27 22:39:14 -070044static DEFINE_PER_CPU(struct iommu_batch, iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -080045
46/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -070047static inline void iommu_batch_start(struct device *dev, unsigned long prot, unsigned long entry)
David S. Miller6a32fd42006-02-19 22:21:32 -080048{
David S. Millerad7ad572007-07-27 22:39:14 -070049 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -080050
David S. Millerad7ad572007-07-27 22:39:14 -070051 p->dev = dev;
David S. Miller6a32fd42006-02-19 22:21:32 -080052 p->prot = prot;
53 p->entry = entry;
54 p->npages = 0;
55}
56
57/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -070058static long iommu_batch_flush(struct iommu_batch *p)
David S. Miller6a32fd42006-02-19 22:21:32 -080059{
David S. Millerad7ad572007-07-27 22:39:14 -070060 struct pci_pbm_info *pbm = p->dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -080061 unsigned long devhandle = pbm->devhandle;
David S. Miller6a32fd42006-02-19 22:21:32 -080062 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. Millerd82965c2006-02-20 01:42:51 -080067 while (npages != 0) {
David S. Miller6a32fd42006-02-19 22:21:32 -080068 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. Millerad7ad572007-07-27 22:39:14 -070074 printk("iommu_batch_flush: IOMMU map of "
David S. Miller6a32fd42006-02-19 22:21:32 -080075 "[%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. Millerd82965c2006-02-20 01:42:51 -080085 }
David S. Miller6a32fd42006-02-19 22:21:32 -080086
87 p->entry = entry;
88 p->npages = 0;
89
90 return 0;
91}
92
David S. Miller13fa14e2008-02-09 03:11:01 -080093static 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. Miller6a32fd42006-02-19 22:21:32 -0800104/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -0700105static inline long iommu_batch_add(u64 phys_page)
David S. Miller6a32fd42006-02-19 22:21:32 -0800106{
David S. Millerad7ad572007-07-27 22:39:14 -0700107 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -0800108
109 BUG_ON(p->npages >= PGLIST_NENTS);
110
111 p->pglist[p->npages++] = phys_page;
112 if (p->npages == PGLIST_NENTS)
David S. Millerad7ad572007-07-27 22:39:14 -0700113 return iommu_batch_flush(p);
David S. Miller6a32fd42006-02-19 22:21:32 -0800114
115 return 0;
116}
117
118/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -0700119static inline long iommu_batch_end(void)
David S. Miller6a32fd42006-02-19 22:21:32 -0800120{
David S. Millerad7ad572007-07-27 22:39:14 -0700121 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -0800122
123 BUG_ON(p->npages >= PGLIST_NENTS);
124
David S. Millerad7ad572007-07-27 22:39:14 -0700125 return iommu_batch_flush(p);
David S. Miller6a32fd42006-02-19 22:21:32 -0800126}
David S. Miller18397942006-02-10 00:08:26 -0800127
David S. Millerad7ad572007-07-27 22:39:14 -0700128static void *dma_4v_alloc_coherent(struct device *dev, size_t size,
129 dma_addr_t *dma_addrp, gfp_t gfp)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800130{
David S. Miller7c8f4862006-02-13 21:50:27 -0800131 unsigned long flags, order, first_page, npages, n;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700132 struct iommu *iommu;
133 struct page *page;
David S. Miller18397942006-02-10 00:08:26 -0800134 void *ret;
135 long entry;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700136 int nid;
David S. Miller18397942006-02-10 00:08:26 -0800137
138 size = IO_PAGE_ALIGN(size);
139 order = get_order(size);
David S. Miller6a32fd42006-02-19 22:21:32 -0800140 if (unlikely(order >= MAX_ORDER))
David S. Miller18397942006-02-10 00:08:26 -0800141 return NULL;
142
143 npages = size >> IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800144
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700145 nid = dev->archdata.numa_node;
146 page = alloc_pages_node(nid, gfp, order);
147 if (unlikely(!page))
David S. Miller18397942006-02-10 00:08:26 -0800148 return NULL;
David S. Millere7a04532006-02-15 22:25:27 -0800149
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700150 first_page = (unsigned long) page_address(page);
David S. Miller18397942006-02-10 00:08:26 -0800151 memset((char *)first_page, 0, PAGE_SIZE << order);
152
David S. Millerad7ad572007-07-27 22:39:14 -0700153 iommu = dev->archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800154
155 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800156 entry = iommu_range_alloc(dev, iommu, npages, NULL);
David S. Miller18397942006-02-10 00:08:26 -0800157 spin_unlock_irqrestore(&iommu->lock, flags);
158
David S. Millerd2841422008-02-08 18:05:46 -0800159 if (unlikely(entry == DMA_ERROR_CODE))
160 goto range_alloc_fail;
David S. Miller18397942006-02-10 00:08:26 -0800161
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. Miller6a32fd42006-02-19 22:21:32 -0800167 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800168
David S. Millerad7ad572007-07-27 22:39:14 -0700169 iommu_batch_start(dev,
170 (HV_PCI_MAP_ATTR_READ |
171 HV_PCI_MAP_ATTR_WRITE),
172 entry);
David S. Miller18397942006-02-10 00:08:26 -0800173
David S. Miller6a32fd42006-02-19 22:21:32 -0800174 for (n = 0; n < npages; n++) {
David S. Millerad7ad572007-07-27 22:39:14 -0700175 long err = iommu_batch_add(first_page + (n * PAGE_SIZE));
David S. Miller6a32fd42006-02-19 22:21:32 -0800176 if (unlikely(err < 0L))
177 goto iommu_map_fail;
178 }
David S. Miller18397942006-02-10 00:08:26 -0800179
David S. Millerad7ad572007-07-27 22:39:14 -0700180 if (unlikely(iommu_batch_end() < 0L))
David S. Miller6a32fd42006-02-19 22:21:32 -0800181 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800182
David S. Miller6a32fd42006-02-19 22:21:32 -0800183 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800184
185 return ret;
David S. Miller6a32fd42006-02-19 22:21:32 -0800186
187iommu_map_fail:
188 /* Interrupts are disabled. */
189 spin_lock(&iommu->lock);
David S. Millerd2841422008-02-08 18:05:46 -0800190 iommu_range_free(iommu, *dma_addrp, npages);
David S. Miller6a32fd42006-02-19 22:21:32 -0800191 spin_unlock_irqrestore(&iommu->lock, flags);
192
David S. Millerd2841422008-02-08 18:05:46 -0800193range_alloc_fail:
David S. Miller6a32fd42006-02-19 22:21:32 -0800194 free_pages(first_page, order);
195 return NULL;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800196}
197
David S. Millerad7ad572007-07-27 22:39:14 -0700198static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu,
199 dma_addr_t dvma)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800200{
David S. Millera2fb23a2007-02-28 23:35:04 -0800201 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700202 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800203 unsigned long flags, order, npages, entry;
204 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800205
206 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700207 iommu = dev->archdata.iommu;
208 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800209 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800210 entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
211
212 spin_lock_irqsave(&iommu->lock, flags);
213
David S. Millerd2841422008-02-08 18:05:46 -0800214 iommu_range_free(iommu, dvma, npages);
David S. Miller18397942006-02-10 00:08:26 -0800215
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. Miller8f6a93a2006-02-09 21:32:07 -0800230}
231
David S. Millerad7ad572007-07-27 22:39:14 -0700232static dma_addr_t dma_4v_map_single(struct device *dev, void *ptr, size_t sz,
233 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800234{
David S. Miller16ce82d2007-04-26 21:08:21 -0700235 struct iommu *iommu;
David S. Miller18397942006-02-10 00:08:26 -0800236 unsigned long flags, npages, oaddr;
David S. Miller7c8f4862006-02-13 21:50:27 -0800237 unsigned long i, base_paddr;
David S. Miller6a32fd42006-02-19 22:21:32 -0800238 u32 bus_addr, ret;
David S. Miller18397942006-02-10 00:08:26 -0800239 unsigned long prot;
240 long entry;
David S. Miller18397942006-02-10 00:08:26 -0800241
David S. Millerad7ad572007-07-27 22:39:14 -0700242 iommu = dev->archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800243
David S. Millerad7ad572007-07-27 22:39:14 -0700244 if (unlikely(direction == DMA_NONE))
David S. Miller18397942006-02-10 00:08:26 -0800245 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. Miller18397942006-02-10 00:08:26 -0800250
251 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800252 entry = iommu_range_alloc(dev, iommu, npages, NULL);
David S. Miller18397942006-02-10 00:08:26 -0800253 spin_unlock_irqrestore(&iommu->lock, flags);
254
David S. Millerd2841422008-02-08 18:05:46 -0800255 if (unlikely(entry == DMA_ERROR_CODE))
David S. Miller18397942006-02-10 00:08:26 -0800256 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. Millerad7ad572007-07-27 22:39:14 -0700263 if (direction != DMA_TO_DEVICE)
David S. Miller18397942006-02-10 00:08:26 -0800264 prot |= HV_PCI_MAP_ATTR_WRITE;
265
David S. Miller6a32fd42006-02-19 22:21:32 -0800266 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800267
David S. Millerad7ad572007-07-27 22:39:14 -0700268 iommu_batch_start(dev, prot, entry);
David S. Miller18397942006-02-10 00:08:26 -0800269
David S. Miller6a32fd42006-02-19 22:21:32 -0800270 for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
David S. Millerad7ad572007-07-27 22:39:14 -0700271 long err = iommu_batch_add(base_paddr);
David S. Miller6a32fd42006-02-19 22:21:32 -0800272 if (unlikely(err < 0L))
273 goto iommu_map_fail;
274 }
David S. Millerad7ad572007-07-27 22:39:14 -0700275 if (unlikely(iommu_batch_end() < 0L))
David S. Miller6a32fd42006-02-19 22:21:32 -0800276 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800277
David S. Miller6a32fd42006-02-19 22:21:32 -0800278 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800279
280 return ret;
281
282bad:
283 if (printk_ratelimit())
284 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700285 return DMA_ERROR_CODE;
David S. Miller6a32fd42006-02-19 22:21:32 -0800286
287iommu_map_fail:
288 /* Interrupts are disabled. */
289 spin_lock(&iommu->lock);
David S. Millerd2841422008-02-08 18:05:46 -0800290 iommu_range_free(iommu, bus_addr, npages);
David S. Miller6a32fd42006-02-19 22:21:32 -0800291 spin_unlock_irqrestore(&iommu->lock, flags);
292
David S. Millerad7ad572007-07-27 22:39:14 -0700293 return DMA_ERROR_CODE;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800294}
295
David S. Millerad7ad572007-07-27 22:39:14 -0700296static void dma_4v_unmap_single(struct device *dev, dma_addr_t bus_addr,
297 size_t sz, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800298{
David S. Millera2fb23a2007-02-28 23:35:04 -0800299 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700300 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800301 unsigned long flags, npages;
David S. Miller18397942006-02-10 00:08:26 -0800302 long entry;
David S. Miller7c8f4862006-02-13 21:50:27 -0800303 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800304
David S. Millerad7ad572007-07-27 22:39:14 -0700305 if (unlikely(direction == DMA_NONE)) {
David S. Miller18397942006-02-10 00:08:26 -0800306 if (printk_ratelimit())
307 WARN_ON(1);
308 return;
309 }
310
David S. Millerad7ad572007-07-27 22:39:14 -0700311 iommu = dev->archdata.iommu;
312 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800313 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800314
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. Millerd2841422008-02-08 18:05:46 -0800321 iommu_range_free(iommu, bus_addr, npages);
David S. Miller18397942006-02-10 00:08:26 -0800322
David S. Millerd2841422008-02-08 18:05:46 -0800323 entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800324 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. Millerad7ad572007-07-27 22:39:14 -0700336static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
337 int nelems, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800338{
David S. Miller13fa14e2008-02-09 03:11:01 -0800339 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 Tomonorif0880252008-03-28 15:55:41 -0700343 unsigned long seg_boundary_size;
David S. Miller13fa14e2008-02-09 03:11:01 -0800344 int outcount, incount, i;
David S. Miller16ce82d2007-04-26 21:08:21 -0700345 struct iommu *iommu;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700346 unsigned long base_shift;
David S. Miller13fa14e2008-02-09 03:11:01 -0800347 long err;
David S. Miller18397942006-02-10 00:08:26 -0800348
David S. Miller13fa14e2008-02-09 03:11:01 -0800349 BUG_ON(direction == DMA_NONE);
David S. Miller18397942006-02-10 00:08:26 -0800350
David S. Millerad7ad572007-07-27 22:39:14 -0700351 iommu = dev->archdata.iommu;
David S. Miller13fa14e2008-02-09 03:11:01 -0800352 if (nelems == 0 || !iommu)
353 return 0;
David S. Miller18397942006-02-10 00:08:26 -0800354
David S. Miller18397942006-02-10 00:08:26 -0800355 prot = HV_PCI_MAP_ATTR_READ;
David S. Millerad7ad572007-07-27 22:39:14 -0700356 if (direction != DMA_TO_DEVICE)
David S. Miller18397942006-02-10 00:08:26 -0800357 prot |= HV_PCI_MAP_ATTR_WRITE;
358
David S. Miller13fa14e2008-02-09 03:11:01 -0800359 outs = s = segstart = &sglist[0];
360 outcount = 1;
361 incount = nelems;
362 handle = 0;
David S. Miller38192d52008-02-06 03:50:26 -0800363
David S. Miller13fa14e2008-02-09 03:11:01 -0800364 /* Init first segment length for backout at failure */
365 outs->dma_length = 0;
David S. Miller38192d52008-02-06 03:50:26 -0800366
David S. Miller13fa14e2008-02-09 03:11:01 -0800367 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller38192d52008-02-06 03:50:26 -0800368
David S. Miller13fa14e2008-02-09 03:11:01 -0800369 iommu_batch_start(dev, prot, ~0UL);
David S. Miller38192d52008-02-06 03:50:26 -0800370
David S. Miller13fa14e2008-02-09 03:11:01 -0800371 max_seg_size = dma_get_max_seg_size(dev);
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700372 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. Miller13fa14e2008-02-09 03:11:01 -0800375 for_each_sg(sglist, s, nelems, i) {
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700376 unsigned long paddr, npages, entry, out_entry = 0, slen;
David S. Miller38192d52008-02-06 03:50:26 -0800377
David S. Miller13fa14e2008-02-09 03:11:01 -0800378 slen = s->length;
379 /* Sanity check */
380 if (slen == 0) {
381 dma_next = 0;
382 continue;
David S. Miller38192d52008-02-06 03:50:26 -0800383 }
David S. Miller13fa14e2008-02-09 03:11:01 -0800384 /* 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 Tomonorif0880252008-03-28 15:55:41 -0700419 (outs->dma_length + s->length > max_seg_size) ||
420 (is_span_boundary(out_entry, base_shift,
421 seg_boundary_size, outs, s))) {
David S. Miller13fa14e2008-02-09 03:11:01 -0800422 /* 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 Tomonorif0880252008-03-28 15:55:41 -0700435 out_entry = entry;
David S. Miller13fa14e2008-02-09 03:11:01 -0800436 }
437
438 /* Calculate next page pointer for contiguous check */
439 dma_next = dma_addr + slen;
David S. Miller38192d52008-02-06 03:50:26 -0800440 }
441
442 err = iommu_batch_end();
443
David S. Miller6a32fd42006-02-19 22:21:32 -0800444 if (unlikely(err < 0L))
445 goto iommu_map_failed;
David S. Miller18397942006-02-10 00:08:26 -0800446
David S. Miller13fa14e2008-02-09 03:11:01 -0800447 spin_unlock_irqrestore(&iommu->lock, flags);
David S. Miller18397942006-02-10 00:08:26 -0800448
David S. Miller13fa14e2008-02-09 03:11:01 -0800449 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. Miller6a32fd42006-02-19 22:21:32 -0800456
457iommu_map_failed:
David S. Miller13fa14e2008-02-09 03:11:01 -0800458 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. Miller6a32fd42006-02-19 22:21:32 -0800472 spin_unlock_irqrestore(&iommu->lock, flags);
473
474 return 0;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800475}
476
David S. Millerad7ad572007-07-27 22:39:14 -0700477static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
478 int nelems, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800479{
David S. Millera2fb23a2007-02-28 23:35:04 -0800480 struct pci_pbm_info *pbm;
David S. Miller13fa14e2008-02-09 03:11:01 -0800481 struct scatterlist *sg;
David S. Miller38192d52008-02-06 03:50:26 -0800482 struct iommu *iommu;
David S. Miller13fa14e2008-02-09 03:11:01 -0800483 unsigned long flags;
484 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800485
David S. Miller13fa14e2008-02-09 03:11:01 -0800486 BUG_ON(direction == DMA_NONE);
David S. Miller18397942006-02-10 00:08:26 -0800487
David S. Millerad7ad572007-07-27 22:39:14 -0700488 iommu = dev->archdata.iommu;
489 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800490 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800491
David S. Miller18397942006-02-10 00:08:26 -0800492 spin_lock_irqsave(&iommu->lock, flags);
493
David S. Miller13fa14e2008-02-09 03:11:01 -0800494 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. Miller18397942006-02-10 00:08:26 -0800499
David S. Miller13fa14e2008-02-09 03:11:01 -0800500 if (!len)
501 break;
502 npages = iommu_num_pages(dma_handle, len);
503 iommu_range_free(iommu, dma_handle, npages);
David S. Miller18397942006-02-10 00:08:26 -0800504
David S. Miller13fa14e2008-02-09 03:11:01 -0800505 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. Miller18397942006-02-10 00:08:26 -0800517
518 spin_unlock_irqrestore(&iommu->lock, flags);
David S. Miller8f6a93a2006-02-09 21:32:07 -0800519}
520
David S. Millerad7ad572007-07-27 22:39:14 -0700521static 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. Miller8f6a93a2006-02-09 21:32:07 -0800524{
David S. Miller18397942006-02-10 00:08:26 -0800525 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800526}
527
David S. Millerad7ad572007-07-27 22:39:14 -0700528static void dma_4v_sync_sg_for_cpu(struct device *dev,
529 struct scatterlist *sglist, int nelems,
530 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800531{
David S. Miller18397942006-02-10 00:08:26 -0800532 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800533}
534
Adrian Bunk908f5162008-06-05 11:42:40 -0700535static const struct dma_ops sun4v_dma_ops = {
David S. Millerad7ad572007-07-27 22:39:14 -0700536 .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. Miller8f6a93a2006-02-09 21:32:07 -0800544};
545
David S. Millere822358a2008-09-01 18:32:22 -0700546static void __init pci_sun4v_scan_bus(struct pci_pbm_info *pbm,
547 struct device *parent)
David S. Millerbade5622006-02-09 22:05:54 -0800548{
David S. Millere87dc352006-06-21 18:18:47 -0700549 struct property *prop;
550 struct device_node *dp;
551
David S. Miller34768bc2007-05-07 23:06:27 -0700552 dp = pbm->prom_node;
553 prop = of_find_property(dp, "66mhz-capable", NULL);
554 pbm->is_66mhz_capable = (prop != NULL);
David S. Millere822358a2008-09-01 18:32:22 -0700555 pbm->pci_bus = pci_scan_one_pbm(pbm, parent);
David S. Millerc2609262006-02-12 22:18:52 -0800556
557 /* XXX register error interrupt handlers XXX */
David S. Millerbade5622006-02-09 22:05:54 -0800558}
559
Adrian Bunk4c622252008-02-05 03:01:43 -0800560static unsigned long __init probe_existing_entries(struct pci_pbm_info *pbm,
561 struct iommu *iommu)
David S. Miller18397942006-02-10 00:08:26 -0800562{
David S. Miller9b3627f2007-04-24 23:51:18 -0700563 struct iommu_arena *arena = &iommu->arena;
David S. Millere7a04532006-02-15 22:25:27 -0800564 unsigned long i, cnt = 0;
David S. Miller7c8f4862006-02-13 21:50:27 -0800565 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800566
567 devhandle = pbm->devhandle;
568 for (i = 0; i < arena->limit; i++) {
569 unsigned long ret, io_attrs, ra;
570
571 ret = pci_sun4v_iommu_getmap(devhandle,
572 HV_PCI_TSBID(0, i),
573 &io_attrs, &ra);
David S. Millere7a04532006-02-15 22:25:27 -0800574 if (ret == HV_EOK) {
David S. Millerc2a5a462006-06-22 00:01:56 -0700575 if (page_in_phys_avail(ra)) {
576 pci_sun4v_iommu_demap(devhandle,
577 HV_PCI_TSBID(0, i), 1);
578 } else {
579 cnt++;
580 __set_bit(i, arena->map);
581 }
David S. Millere7a04532006-02-15 22:25:27 -0800582 }
David S. Miller18397942006-02-10 00:08:26 -0800583 }
David S. Millere7a04532006-02-15 22:25:27 -0800584
585 return cnt;
David S. Miller18397942006-02-10 00:08:26 -0800586}
587
David S. Miller3822b502008-08-30 02:50:29 -0700588static int __init pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
David S. Millerbade5622006-02-09 22:05:54 -0800589{
David S. Miller8aef7272008-09-01 20:23:18 -0700590 static const u32 vdma_default[] = { 0x80000000, 0x80000000 };
David S. Miller16ce82d2007-04-26 21:08:21 -0700591 struct iommu *iommu = pbm->iommu;
David S. Miller59db8102007-05-23 18:00:46 -0700592 unsigned long num_tsb_entries, sz, tsbsize;
David S. Miller8aef7272008-09-01 20:23:18 -0700593 u32 dma_mask, dma_offset;
594 const u32 *vdma;
David S. Miller18397942006-02-10 00:08:26 -0800595
David S. Miller8aef7272008-09-01 20:23:18 -0700596 vdma = of_get_property(pbm->prom_node, "virtual-dma", NULL);
597 if (!vdma)
598 vdma = vdma_default;
David S. Miller18397942006-02-10 00:08:26 -0800599
David S. Miller59db8102007-05-23 18:00:46 -0700600 if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
David S. Miller3822b502008-08-30 02:50:29 -0700601 printk(KERN_ERR PFX "Strange virtual-dma[%08x:%08x].\n",
602 vdma[0], vdma[1]);
603 return -EINVAL;
David S. Miller18397942006-02-10 00:08:26 -0800604 };
605
David S. Miller59db8102007-05-23 18:00:46 -0700606 dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
607 num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
608 tsbsize = num_tsb_entries * sizeof(iopte_t);
David S. Miller18397942006-02-10 00:08:26 -0800609
610 dma_offset = vdma[0];
611
612 /* Setup initial software IOMMU state. */
613 spin_lock_init(&iommu->lock);
614 iommu->ctx_lowest_free = 1;
615 iommu->page_table_map_base = dma_offset;
616 iommu->dma_addr_mask = dma_mask;
617
618 /* Allocate and initialize the free area map. */
David S. Miller59db8102007-05-23 18:00:46 -0700619 sz = (num_tsb_entries + 7) / 8;
David S. Miller18397942006-02-10 00:08:26 -0800620 sz = (sz + 7UL) & ~7UL;
Yan Burman982c2062006-11-30 17:13:09 -0800621 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
David S. Miller18397942006-02-10 00:08:26 -0800622 if (!iommu->arena.map) {
David S. Miller3822b502008-08-30 02:50:29 -0700623 printk(KERN_ERR PFX "Error, kmalloc(arena.map) failed.\n");
624 return -ENOMEM;
David S. Miller18397942006-02-10 00:08:26 -0800625 }
David S. Miller18397942006-02-10 00:08:26 -0800626 iommu->arena.limit = num_tsb_entries;
627
David S. Millere7a04532006-02-15 22:25:27 -0800628 sz = probe_existing_entries(pbm, iommu);
David S. Millerc2a5a462006-06-22 00:01:56 -0700629 if (sz)
630 printk("%s: Imported %lu TSB entries from OBP\n",
631 pbm->name, sz);
David S. Miller3822b502008-08-30 02:50:29 -0700632
633 return 0;
David S. Millerbade5622006-02-09 22:05:54 -0800634}
635
David S. Miller35a17eb2007-02-10 17:41:02 -0800636#ifdef CONFIG_PCI_MSI
637struct pci_sun4v_msiq_entry {
638 u64 version_type;
639#define MSIQ_VERSION_MASK 0xffffffff00000000UL
640#define MSIQ_VERSION_SHIFT 32
641#define MSIQ_TYPE_MASK 0x00000000000000ffUL
642#define MSIQ_TYPE_SHIFT 0
643#define MSIQ_TYPE_NONE 0x00
644#define MSIQ_TYPE_MSG 0x01
645#define MSIQ_TYPE_MSI32 0x02
646#define MSIQ_TYPE_MSI64 0x03
647#define MSIQ_TYPE_INTX 0x08
648#define MSIQ_TYPE_NONE2 0xff
649
650 u64 intx_sysino;
651 u64 reserved1;
652 u64 stick;
653 u64 req_id; /* bus/device/func */
654#define MSIQ_REQID_BUS_MASK 0xff00UL
655#define MSIQ_REQID_BUS_SHIFT 8
656#define MSIQ_REQID_DEVICE_MASK 0x00f8UL
657#define MSIQ_REQID_DEVICE_SHIFT 3
658#define MSIQ_REQID_FUNC_MASK 0x0007UL
659#define MSIQ_REQID_FUNC_SHIFT 0
660
661 u64 msi_address;
662
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700663 /* The format of this value is message type dependent.
David S. Miller35a17eb2007-02-10 17:41:02 -0800664 * For MSI bits 15:0 are the data from the MSI packet.
665 * For MSI-X bits 31:0 are the data from the MSI packet.
666 * For MSG, the message code and message routing code where:
667 * bits 39:32 is the bus/device/fn of the msg target-id
668 * bits 18:16 is the message routing code
669 * bits 7:0 is the message code
670 * For INTx the low order 2-bits are:
671 * 00 - INTA
672 * 01 - INTB
673 * 10 - INTC
674 * 11 - INTD
675 */
676 u64 msi_data;
677
678 u64 reserved2;
679};
680
David S. Miller759f89e2007-10-11 03:16:13 -0700681static int pci_sun4v_get_head(struct pci_pbm_info *pbm, unsigned long msiqid,
682 unsigned long *head)
David S. Miller35a17eb2007-02-10 17:41:02 -0800683{
David S. Miller759f89e2007-10-11 03:16:13 -0700684 unsigned long err, limit;
David S. Miller35a17eb2007-02-10 17:41:02 -0800685
David S. Miller759f89e2007-10-11 03:16:13 -0700686 err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, head);
David S. Miller35a17eb2007-02-10 17:41:02 -0800687 if (unlikely(err))
David S. Miller759f89e2007-10-11 03:16:13 -0700688 return -ENXIO;
David S. Miller35a17eb2007-02-10 17:41:02 -0800689
David S. Miller759f89e2007-10-11 03:16:13 -0700690 limit = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
691 if (unlikely(*head >= limit))
692 return -EFBIG;
David S. Miller35a17eb2007-02-10 17:41:02 -0800693
694 return 0;
695}
696
David S. Miller759f89e2007-10-11 03:16:13 -0700697static int pci_sun4v_dequeue_msi(struct pci_pbm_info *pbm,
698 unsigned long msiqid, unsigned long *head,
699 unsigned long *msi)
David S. Miller35a17eb2007-02-10 17:41:02 -0800700{
David S. Miller759f89e2007-10-11 03:16:13 -0700701 struct pci_sun4v_msiq_entry *ep;
702 unsigned long err, type;
703
704 /* Note: void pointer arithmetic, 'head' is a byte offset */
705 ep = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
706 (pbm->msiq_ent_count *
707 sizeof(struct pci_sun4v_msiq_entry))) +
708 *head);
709
710 if ((ep->version_type & MSIQ_TYPE_MASK) == 0)
711 return 0;
712
713 type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
714 if (unlikely(type != MSIQ_TYPE_MSI32 &&
715 type != MSIQ_TYPE_MSI64))
716 return -EINVAL;
717
718 *msi = ep->msi_data;
719
720 err = pci_sun4v_msi_setstate(pbm->devhandle,
721 ep->msi_data /* msi_num */,
722 HV_MSISTATE_IDLE);
723 if (unlikely(err))
724 return -ENXIO;
725
726 /* Clear the entry. */
727 ep->version_type &= ~MSIQ_TYPE_MASK;
728
729 (*head) += sizeof(struct pci_sun4v_msiq_entry);
730 if (*head >=
731 (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry)))
732 *head = 0;
733
734 return 1;
David S. Miller35a17eb2007-02-10 17:41:02 -0800735}
736
David S. Miller759f89e2007-10-11 03:16:13 -0700737static int pci_sun4v_set_head(struct pci_pbm_info *pbm, unsigned long msiqid,
738 unsigned long head)
739{
740 unsigned long err;
741
742 err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
743 if (unlikely(err))
744 return -EINVAL;
745
746 return 0;
747}
748
749static int pci_sun4v_msi_setup(struct pci_pbm_info *pbm, unsigned long msiqid,
750 unsigned long msi, int is_msi64)
751{
752 if (pci_sun4v_msi_setmsiq(pbm->devhandle, msi, msiqid,
753 (is_msi64 ?
754 HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
755 return -ENXIO;
756 if (pci_sun4v_msi_setstate(pbm->devhandle, msi, HV_MSISTATE_IDLE))
757 return -ENXIO;
758 if (pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_VALID))
759 return -ENXIO;
760 return 0;
761}
762
763static int pci_sun4v_msi_teardown(struct pci_pbm_info *pbm, unsigned long msi)
764{
765 unsigned long err, msiqid;
766
767 err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi, &msiqid);
768 if (err)
769 return -ENXIO;
770
771 pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_INVALID);
772
773 return 0;
774}
775
776static int pci_sun4v_msiq_alloc(struct pci_pbm_info *pbm)
David S. Miller35a17eb2007-02-10 17:41:02 -0800777{
778 unsigned long q_size, alloc_size, pages, order;
779 int i;
780
781 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
782 alloc_size = (pbm->msiq_num * q_size);
783 order = get_order(alloc_size);
784 pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
785 if (pages == 0UL) {
786 printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
787 order);
788 return -ENOMEM;
789 }
790 memset((char *)pages, 0, PAGE_SIZE << order);
791 pbm->msi_queues = (void *) pages;
792
793 for (i = 0; i < pbm->msiq_num; i++) {
794 unsigned long err, base = __pa(pages + (i * q_size));
795 unsigned long ret1, ret2;
796
797 err = pci_sun4v_msiq_conf(pbm->devhandle,
798 pbm->msiq_first + i,
799 base, pbm->msiq_ent_count);
800 if (err) {
801 printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
802 err);
803 goto h_error;
804 }
805
806 err = pci_sun4v_msiq_info(pbm->devhandle,
807 pbm->msiq_first + i,
808 &ret1, &ret2);
809 if (err) {
810 printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
811 err);
812 goto h_error;
813 }
814 if (ret1 != base || ret2 != pbm->msiq_ent_count) {
815 printk(KERN_ERR "MSI: Bogus qconf "
816 "expected[%lx:%x] got[%lx:%lx]\n",
817 base, pbm->msiq_ent_count,
818 ret1, ret2);
819 goto h_error;
820 }
821 }
822
823 return 0;
824
825h_error:
826 free_pages(pages, order);
827 return -EINVAL;
828}
829
David S. Miller759f89e2007-10-11 03:16:13 -0700830static void pci_sun4v_msiq_free(struct pci_pbm_info *pbm)
David S. Miller35a17eb2007-02-10 17:41:02 -0800831{
David S. Miller759f89e2007-10-11 03:16:13 -0700832 unsigned long q_size, alloc_size, pages, order;
David S. Miller35a17eb2007-02-10 17:41:02 -0800833 int i;
834
David S. Miller759f89e2007-10-11 03:16:13 -0700835 for (i = 0; i < pbm->msiq_num; i++) {
836 unsigned long msiqid = pbm->msiq_first + i;
837
838 (void) pci_sun4v_msiq_conf(pbm->devhandle, msiqid, 0UL, 0);
David S. Miller35a17eb2007-02-10 17:41:02 -0800839 }
840
David S. Miller759f89e2007-10-11 03:16:13 -0700841 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
842 alloc_size = (pbm->msiq_num * q_size);
843 order = get_order(alloc_size);
844
845 pages = (unsigned long) pbm->msi_queues;
846
847 free_pages(pages, order);
848
849 pbm->msi_queues = NULL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800850}
851
David S. Miller759f89e2007-10-11 03:16:13 -0700852static int pci_sun4v_msiq_build_irq(struct pci_pbm_info *pbm,
853 unsigned long msiqid,
854 unsigned long devino)
David S. Miller35a17eb2007-02-10 17:41:02 -0800855{
David S. Miller759f89e2007-10-11 03:16:13 -0700856 unsigned int virt_irq = sun4v_build_irq(pbm->devhandle, devino);
David S. Miller35a17eb2007-02-10 17:41:02 -0800857
David S. Miller759f89e2007-10-11 03:16:13 -0700858 if (!virt_irq)
859 return -ENOMEM;
David S. Miller35a17eb2007-02-10 17:41:02 -0800860
David S. Miller35a17eb2007-02-10 17:41:02 -0800861 if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
David S. Miller759f89e2007-10-11 03:16:13 -0700862 return -EINVAL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800863 if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
David S. Miller759f89e2007-10-11 03:16:13 -0700864 return -EINVAL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800865
David S. Miller759f89e2007-10-11 03:16:13 -0700866 return virt_irq;
David S. Miller35a17eb2007-02-10 17:41:02 -0800867}
868
David S. Miller759f89e2007-10-11 03:16:13 -0700869static const struct sparc64_msiq_ops pci_sun4v_msiq_ops = {
870 .get_head = pci_sun4v_get_head,
871 .dequeue_msi = pci_sun4v_dequeue_msi,
872 .set_head = pci_sun4v_set_head,
873 .msi_setup = pci_sun4v_msi_setup,
874 .msi_teardown = pci_sun4v_msi_teardown,
875 .msiq_alloc = pci_sun4v_msiq_alloc,
876 .msiq_free = pci_sun4v_msiq_free,
877 .msiq_build_irq = pci_sun4v_msiq_build_irq,
878};
David S. Millere9870c42007-05-07 23:28:50 -0700879
880static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
881{
David S. Miller759f89e2007-10-11 03:16:13 -0700882 sparc64_pbm_msi_init(pbm, &pci_sun4v_msiq_ops);
David S. Millere9870c42007-05-07 23:28:50 -0700883}
David S. Miller35a17eb2007-02-10 17:41:02 -0800884#else /* CONFIG_PCI_MSI */
885static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
886{
887}
888#endif /* !(CONFIG_PCI_MSI) */
889
David S. Miller3822b502008-08-30 02:50:29 -0700890static int __init pci_sun4v_pbm_init(struct pci_controller_info *p,
David S. Millere822358a2008-09-01 18:32:22 -0700891 struct of_device *op, u32 devhandle)
David S. Millerbade5622006-02-09 22:05:54 -0800892{
David S. Millere822358a2008-09-01 18:32:22 -0700893 struct device_node *dp = op->node;
David S. Millerbade5622006-02-09 22:05:54 -0800894 struct pci_pbm_info *pbm;
David S. Miller3822b502008-08-30 02:50:29 -0700895 int err;
David S. Millerbade5622006-02-09 22:05:54 -0800896
David S. Miller38337892006-02-12 22:06:53 -0800897 if (devhandle & 0x40)
898 pbm = &p->pbm_B;
899 else
900 pbm = &p->pbm_A;
David S. Millerbade5622006-02-09 22:05:54 -0800901
David S. Miller34768bc2007-05-07 23:06:27 -0700902 pbm->next = pci_pbm_root;
903 pci_pbm_root = pbm;
904
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700905 pbm->numa_node = of_node_to_nid(dp);
906
David S. Millerca3dd882007-05-09 02:35:27 -0700907 pbm->pci_ops = &sun4v_pci_ops;
908 pbm->config_space_reg_bits = 12;
David S. Miller34768bc2007-05-07 23:06:27 -0700909
David S. Miller6c108f12007-05-07 23:49:01 -0700910 pbm->index = pci_num_pbms++;
911
David S. Millerbade5622006-02-09 22:05:54 -0800912 pbm->parent = p;
David S. Millere87dc352006-06-21 18:18:47 -0700913 pbm->prom_node = dp;
David S. Millerbade5622006-02-09 22:05:54 -0800914
David S. Miller38337892006-02-12 22:06:53 -0800915 pbm->devhandle = devhandle;
David S. Millerbade5622006-02-09 22:05:54 -0800916
David S. Millere87dc352006-06-21 18:18:47 -0700917 pbm->name = dp->full_name;
David S. Millerbade5622006-02-09 22:05:54 -0800918
David S. Millere87dc352006-06-21 18:18:47 -0700919 printk("%s: SUN4V PCI Bus Module\n", pbm->name);
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700920 printk("%s: On NUMA node %d\n", pbm->name, pbm->numa_node);
David S. Millerbade5622006-02-09 22:05:54 -0800921
David S. Miller9fd8b642007-03-08 21:55:49 -0800922 pci_determine_mem_io_space(pbm);
David S. Millerbade5622006-02-09 22:05:54 -0800923
David S. Millercfa06522007-05-07 21:51:41 -0700924 pci_get_pbm_props(pbm);
David S. Miller3822b502008-08-30 02:50:29 -0700925
926 err = pci_sun4v_iommu_init(pbm);
927 if (err)
928 return err;
929
David S. Miller35a17eb2007-02-10 17:41:02 -0800930 pci_sun4v_msi_init(pbm);
David S. Miller3822b502008-08-30 02:50:29 -0700931
David S. Millere822358a2008-09-01 18:32:22 -0700932 pci_sun4v_scan_bus(pbm, &op->dev);
David S. Miller3822b502008-08-30 02:50:29 -0700933
934 return 0;
David S. Millerbade5622006-02-09 22:05:54 -0800935}
936
David S. Miller3822b502008-08-30 02:50:29 -0700937static int __devinit pci_sun4v_probe(struct of_device *op,
938 const struct of_device_id *match)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800939{
David S. Miller3822b502008-08-30 02:50:29 -0700940 const struct linux_prom64_registers *regs;
David S. Millere01c0d62007-05-25 01:04:15 -0700941 static int hvapi_negotiated = 0;
David S. Millerbade5622006-02-09 22:05:54 -0800942 struct pci_controller_info *p;
David S. Miller34768bc2007-05-07 23:06:27 -0700943 struct pci_pbm_info *pbm;
David S. Miller3822b502008-08-30 02:50:29 -0700944 struct device_node *dp;
David S. Miller16ce82d2007-04-26 21:08:21 -0700945 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800946 u32 devhandle;
David S. Millerd7472c32008-08-31 01:33:52 -0700947 int i, err;
David S. Miller38337892006-02-12 22:06:53 -0800948
David S. Miller3822b502008-08-30 02:50:29 -0700949 dp = op->node;
950
David S. Millere01c0d62007-05-25 01:04:15 -0700951 if (!hvapi_negotiated++) {
952 int err = sun4v_hvapi_register(HV_GRP_PCI,
953 vpci_major,
954 &vpci_minor);
955
956 if (err) {
David S. Miller3822b502008-08-30 02:50:29 -0700957 printk(KERN_ERR PFX "Could not register hvapi, "
958 "err=%d\n", err);
959 return err;
David S. Millere01c0d62007-05-25 01:04:15 -0700960 }
David S. Miller3822b502008-08-30 02:50:29 -0700961 printk(KERN_INFO PFX "Registered hvapi major[%lu] minor[%lu]\n",
David S. Millere01c0d62007-05-25 01:04:15 -0700962 vpci_major, vpci_minor);
David S. Millerad7ad572007-07-27 22:39:14 -0700963
964 dma_ops = &sun4v_dma_ops;
David S. Millere01c0d62007-05-25 01:04:15 -0700965 }
966
David S. Miller3822b502008-08-30 02:50:29 -0700967 regs = of_get_property(dp, "reg", NULL);
David S. Millerd7472c32008-08-31 01:33:52 -0700968 err = -ENODEV;
David S. Miller3822b502008-08-30 02:50:29 -0700969 if (!regs) {
970 printk(KERN_ERR PFX "Could not find config registers\n");
David S. Millerd7472c32008-08-31 01:33:52 -0700971 goto out_err;
Cyrill Gorcunov75c6d142007-11-20 17:32:19 -0800972 }
David S. Millere87dc352006-06-21 18:18:47 -0700973 devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
David S. Miller38337892006-02-12 22:06:53 -0800974
David S. Miller34768bc2007-05-07 23:06:27 -0700975 for (pbm = pci_pbm_root; pbm; pbm = pbm->next) {
David S. Miller0b522492006-02-12 22:29:36 -0800976 if (pbm->devhandle == (devhandle ^ 0x40)) {
David S. Millere822358a2008-09-01 18:32:22 -0700977 return pci_sun4v_pbm_init(pbm->parent, op, devhandle);
David S. Miller0b522492006-02-12 22:29:36 -0800978 }
David S. Miller38337892006-02-12 22:06:53 -0800979 }
David S. Millerbade5622006-02-09 22:05:54 -0800980
David S. Millerd7472c32008-08-31 01:33:52 -0700981 err = -ENOMEM;
KAMEZAWA Hiroyukia283a522006-04-10 22:52:52 -0700982 for_each_possible_cpu(i) {
David S. Miller7c8f4862006-02-13 21:50:27 -0800983 unsigned long page = get_zeroed_page(GFP_ATOMIC);
984
985 if (!page)
David S. Millerd7472c32008-08-31 01:33:52 -0700986 goto out_err;
David S. Miller7c8f4862006-02-13 21:50:27 -0800987
David S. Millerad7ad572007-07-27 22:39:14 -0700988 per_cpu(iommu_batch, i).pglist = (u64 *) page;
David S. Millerbade5622006-02-09 22:05:54 -0800989 }
David S. Miller7c8f4862006-02-13 21:50:27 -0800990
Yan Burman982c2062006-11-30 17:13:09 -0800991 p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
David S. Miller3822b502008-08-30 02:50:29 -0700992 if (!p) {
993 printk(KERN_ERR PFX "Could not allocate pci_controller_info\n");
David S. Millerd7472c32008-08-31 01:33:52 -0700994 goto out_err;
David S. Miller3822b502008-08-30 02:50:29 -0700995 }
David S. Miller7c8f4862006-02-13 21:50:27 -0800996
David S. Miller16ce82d2007-04-26 21:08:21 -0700997 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
David S. Miller3822b502008-08-30 02:50:29 -0700998 if (!iommu) {
999 printk(KERN_ERR PFX "Could not allocate pbm A iommu\n");
David S. Millerd7472c32008-08-31 01:33:52 -07001000 goto out_free_controller;
David S. Miller3822b502008-08-30 02:50:29 -07001001 }
David S. Miller7c8f4862006-02-13 21:50:27 -08001002
David S. Millerbade5622006-02-09 22:05:54 -08001003 p->pbm_A.iommu = iommu;
1004
David S. Miller16ce82d2007-04-26 21:08:21 -07001005 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
David S. Miller3822b502008-08-30 02:50:29 -07001006 if (!iommu) {
1007 printk(KERN_ERR PFX "Could not allocate pbm B iommu\n");
David S. Millerd7472c32008-08-31 01:33:52 -07001008 goto out_free_iommu_A;
David S. Miller3822b502008-08-30 02:50:29 -07001009 }
David S. Miller7c8f4862006-02-13 21:50:27 -08001010
David S. Millerbade5622006-02-09 22:05:54 -08001011 p->pbm_B.iommu = iommu;
1012
David S. Millere822358a2008-09-01 18:32:22 -07001013 return pci_sun4v_pbm_init(p, op, devhandle);
David S. Miller7c8f4862006-02-13 21:50:27 -08001014
David S. Millerd7472c32008-08-31 01:33:52 -07001015out_free_iommu_A:
1016 kfree(p->pbm_A.iommu);
1017
1018out_free_controller:
1019 kfree(p);
1020
1021out_err:
1022 return err;
David S. Miller8f6a93a2006-02-09 21:32:07 -08001023}
David S. Miller3822b502008-08-30 02:50:29 -07001024
David S. Millerfd098312008-08-31 01:23:17 -07001025static struct of_device_id __initdata pci_sun4v_match[] = {
David S. Miller3822b502008-08-30 02:50:29 -07001026 {
1027 .name = "pci",
1028 .compatible = "SUNW,sun4v-pci",
1029 },
1030 {},
1031};
1032
1033static struct of_platform_driver pci_sun4v_driver = {
1034 .name = DRIVER_NAME,
1035 .match_table = pci_sun4v_match,
1036 .probe = pci_sun4v_probe,
1037};
1038
1039static int __init pci_sun4v_init(void)
1040{
1041 return of_register_driver(&pci_sun4v_driver, &of_bus_type);
1042}
1043
1044subsys_initcall(pci_sun4v_init);