blob: 2485eaa231019676eb1780bfe9f5ff3965394540 [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. Millerd3ae4b52008-09-09 23:54:02 -070045static int iommu_batch_initialized;
David S. Miller6a32fd42006-02-19 22:21:32 -080046
47/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -070048static inline void iommu_batch_start(struct device *dev, unsigned long prot, unsigned long entry)
David S. Miller6a32fd42006-02-19 22:21:32 -080049{
David S. Millerad7ad572007-07-27 22:39:14 -070050 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -080051
David S. Millerad7ad572007-07-27 22:39:14 -070052 p->dev = dev;
David S. Miller6a32fd42006-02-19 22:21:32 -080053 p->prot = prot;
54 p->entry = entry;
55 p->npages = 0;
56}
57
58/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -070059static long iommu_batch_flush(struct iommu_batch *p)
David S. Miller6a32fd42006-02-19 22:21:32 -080060{
David S. Millerad7ad572007-07-27 22:39:14 -070061 struct pci_pbm_info *pbm = p->dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -080062 unsigned long devhandle = pbm->devhandle;
David S. Miller6a32fd42006-02-19 22:21:32 -080063 unsigned long prot = p->prot;
64 unsigned long entry = p->entry;
65 u64 *pglist = p->pglist;
66 unsigned long npages = p->npages;
67
David S. Millerd82965c2006-02-20 01:42:51 -080068 while (npages != 0) {
David S. Miller6a32fd42006-02-19 22:21:32 -080069 long num;
70
71 num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
72 npages, prot, __pa(pglist));
73 if (unlikely(num < 0)) {
74 if (printk_ratelimit())
David S. Millerad7ad572007-07-27 22:39:14 -070075 printk("iommu_batch_flush: IOMMU map of "
Sam Ravnborg90181132009-01-06 13:19:28 -080076 "[%08lx:%08llx:%lx:%lx:%lx] failed with "
David S. Miller6a32fd42006-02-19 22:21:32 -080077 "status %ld\n",
78 devhandle, HV_PCI_TSBID(0, entry),
79 npages, prot, __pa(pglist), num);
80 return -1;
81 }
82
83 entry += num;
84 npages -= num;
85 pglist += num;
David S. Millerd82965c2006-02-20 01:42:51 -080086 }
David S. Miller6a32fd42006-02-19 22:21:32 -080087
88 p->entry = entry;
89 p->npages = 0;
90
91 return 0;
92}
93
David S. Miller13fa14e2008-02-09 03:11:01 -080094static inline void iommu_batch_new_entry(unsigned long entry)
95{
96 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
97
98 if (p->entry + p->npages == entry)
99 return;
100 if (p->entry != ~0UL)
101 iommu_batch_flush(p);
102 p->entry = entry;
103}
104
David S. Miller6a32fd42006-02-19 22:21:32 -0800105/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -0700106static inline long iommu_batch_add(u64 phys_page)
David S. Miller6a32fd42006-02-19 22:21:32 -0800107{
David S. Millerad7ad572007-07-27 22:39:14 -0700108 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -0800109
110 BUG_ON(p->npages >= PGLIST_NENTS);
111
112 p->pglist[p->npages++] = phys_page;
113 if (p->npages == PGLIST_NENTS)
David S. Millerad7ad572007-07-27 22:39:14 -0700114 return iommu_batch_flush(p);
David S. Miller6a32fd42006-02-19 22:21:32 -0800115
116 return 0;
117}
118
119/* Interrupts must be disabled. */
David S. Millerad7ad572007-07-27 22:39:14 -0700120static inline long iommu_batch_end(void)
David S. Miller6a32fd42006-02-19 22:21:32 -0800121{
David S. Millerad7ad572007-07-27 22:39:14 -0700122 struct iommu_batch *p = &__get_cpu_var(iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -0800123
124 BUG_ON(p->npages >= PGLIST_NENTS);
125
David S. Millerad7ad572007-07-27 22:39:14 -0700126 return iommu_batch_flush(p);
David S. Miller6a32fd42006-02-19 22:21:32 -0800127}
David S. Miller18397942006-02-10 00:08:26 -0800128
David S. Millerad7ad572007-07-27 22:39:14 -0700129static void *dma_4v_alloc_coherent(struct device *dev, size_t size,
130 dma_addr_t *dma_addrp, gfp_t gfp)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800131{
David S. Miller7c8f4862006-02-13 21:50:27 -0800132 unsigned long flags, order, first_page, npages, n;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700133 struct iommu *iommu;
134 struct page *page;
David S. Miller18397942006-02-10 00:08:26 -0800135 void *ret;
136 long entry;
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700137 int nid;
David S. Miller18397942006-02-10 00:08:26 -0800138
139 size = IO_PAGE_ALIGN(size);
140 order = get_order(size);
David S. Miller6a32fd42006-02-19 22:21:32 -0800141 if (unlikely(order >= MAX_ORDER))
David S. Miller18397942006-02-10 00:08:26 -0800142 return NULL;
143
144 npages = size >> IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800145
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700146 nid = dev->archdata.numa_node;
147 page = alloc_pages_node(nid, gfp, order);
148 if (unlikely(!page))
David S. Miller18397942006-02-10 00:08:26 -0800149 return NULL;
David S. Millere7a04532006-02-15 22:25:27 -0800150
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700151 first_page = (unsigned long) page_address(page);
David S. Miller18397942006-02-10 00:08:26 -0800152 memset((char *)first_page, 0, PAGE_SIZE << order);
153
David S. Millerad7ad572007-07-27 22:39:14 -0700154 iommu = dev->archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800155
156 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800157 entry = iommu_range_alloc(dev, iommu, npages, NULL);
David S. Miller18397942006-02-10 00:08:26 -0800158 spin_unlock_irqrestore(&iommu->lock, flags);
159
David S. Millerd2841422008-02-08 18:05:46 -0800160 if (unlikely(entry == DMA_ERROR_CODE))
161 goto range_alloc_fail;
David S. Miller18397942006-02-10 00:08:26 -0800162
163 *dma_addrp = (iommu->page_table_map_base +
164 (entry << IO_PAGE_SHIFT));
165 ret = (void *) first_page;
166 first_page = __pa(first_page);
167
David S. Miller6a32fd42006-02-19 22:21:32 -0800168 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800169
David S. Millerad7ad572007-07-27 22:39:14 -0700170 iommu_batch_start(dev,
171 (HV_PCI_MAP_ATTR_READ |
172 HV_PCI_MAP_ATTR_WRITE),
173 entry);
David S. Miller18397942006-02-10 00:08:26 -0800174
David S. Miller6a32fd42006-02-19 22:21:32 -0800175 for (n = 0; n < npages; n++) {
David S. Millerad7ad572007-07-27 22:39:14 -0700176 long err = iommu_batch_add(first_page + (n * PAGE_SIZE));
David S. Miller6a32fd42006-02-19 22:21:32 -0800177 if (unlikely(err < 0L))
178 goto iommu_map_fail;
179 }
David S. Miller18397942006-02-10 00:08:26 -0800180
David S. Millerad7ad572007-07-27 22:39:14 -0700181 if (unlikely(iommu_batch_end() < 0L))
David S. Miller6a32fd42006-02-19 22:21:32 -0800182 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800183
David S. Miller6a32fd42006-02-19 22:21:32 -0800184 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800185
186 return ret;
David S. Miller6a32fd42006-02-19 22:21:32 -0800187
188iommu_map_fail:
189 /* Interrupts are disabled. */
190 spin_lock(&iommu->lock);
David S. Millerd2841422008-02-08 18:05:46 -0800191 iommu_range_free(iommu, *dma_addrp, npages);
David S. Miller6a32fd42006-02-19 22:21:32 -0800192 spin_unlock_irqrestore(&iommu->lock, flags);
193
David S. Millerd2841422008-02-08 18:05:46 -0800194range_alloc_fail:
David S. Miller6a32fd42006-02-19 22:21:32 -0800195 free_pages(first_page, order);
196 return NULL;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800197}
198
David S. Millerad7ad572007-07-27 22:39:14 -0700199static void dma_4v_free_coherent(struct device *dev, size_t size, void *cpu,
200 dma_addr_t dvma)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800201{
David S. Millera2fb23a2007-02-28 23:35:04 -0800202 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700203 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800204 unsigned long flags, order, npages, entry;
205 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800206
207 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millerad7ad572007-07-27 22:39:14 -0700208 iommu = dev->archdata.iommu;
209 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800210 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800211 entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
212
213 spin_lock_irqsave(&iommu->lock, flags);
214
David S. Millerd2841422008-02-08 18:05:46 -0800215 iommu_range_free(iommu, dvma, npages);
David S. Miller18397942006-02-10 00:08:26 -0800216
217 do {
218 unsigned long num;
219
220 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
221 npages);
222 entry += num;
223 npages -= num;
224 } while (npages != 0);
225
226 spin_unlock_irqrestore(&iommu->lock, flags);
227
228 order = get_order(size);
229 if (order < 10)
230 free_pages((unsigned long)cpu, order);
David S. Miller8f6a93a2006-02-09 21:32:07 -0800231}
232
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000233static dma_addr_t dma_4v_map_page(struct device *dev, struct page *page,
234 unsigned long offset, size_t sz,
235 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800236{
David S. Miller16ce82d2007-04-26 21:08:21 -0700237 struct iommu *iommu;
David S. Miller18397942006-02-10 00:08:26 -0800238 unsigned long flags, npages, oaddr;
David S. Miller7c8f4862006-02-13 21:50:27 -0800239 unsigned long i, base_paddr;
David S. Miller6a32fd42006-02-19 22:21:32 -0800240 u32 bus_addr, ret;
David S. Miller18397942006-02-10 00:08:26 -0800241 unsigned long prot;
242 long entry;
David S. Miller18397942006-02-10 00:08:26 -0800243
David S. Millerad7ad572007-07-27 22:39:14 -0700244 iommu = dev->archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800245
David S. Millerad7ad572007-07-27 22:39:14 -0700246 if (unlikely(direction == DMA_NONE))
David S. Miller18397942006-02-10 00:08:26 -0800247 goto bad;
248
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000249 oaddr = (unsigned long)(page_address(page) + offset);
David S. Miller18397942006-02-10 00:08:26 -0800250 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
251 npages >>= IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800252
253 spin_lock_irqsave(&iommu->lock, flags);
David S. Millerd2841422008-02-08 18:05:46 -0800254 entry = iommu_range_alloc(dev, iommu, npages, NULL);
David S. Miller18397942006-02-10 00:08:26 -0800255 spin_unlock_irqrestore(&iommu->lock, flags);
256
David S. Millerd2841422008-02-08 18:05:46 -0800257 if (unlikely(entry == DMA_ERROR_CODE))
David S. Miller18397942006-02-10 00:08:26 -0800258 goto bad;
259
260 bus_addr = (iommu->page_table_map_base +
261 (entry << IO_PAGE_SHIFT));
262 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
263 base_paddr = __pa(oaddr & IO_PAGE_MASK);
264 prot = HV_PCI_MAP_ATTR_READ;
David S. Millerad7ad572007-07-27 22:39:14 -0700265 if (direction != DMA_TO_DEVICE)
David S. Miller18397942006-02-10 00:08:26 -0800266 prot |= HV_PCI_MAP_ATTR_WRITE;
267
David S. Miller6a32fd42006-02-19 22:21:32 -0800268 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800269
David S. Millerad7ad572007-07-27 22:39:14 -0700270 iommu_batch_start(dev, prot, entry);
David S. Miller18397942006-02-10 00:08:26 -0800271
David S. Miller6a32fd42006-02-19 22:21:32 -0800272 for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
David S. Millerad7ad572007-07-27 22:39:14 -0700273 long err = iommu_batch_add(base_paddr);
David S. Miller6a32fd42006-02-19 22:21:32 -0800274 if (unlikely(err < 0L))
275 goto iommu_map_fail;
276 }
David S. Millerad7ad572007-07-27 22:39:14 -0700277 if (unlikely(iommu_batch_end() < 0L))
David S. Miller6a32fd42006-02-19 22:21:32 -0800278 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800279
David S. Miller6a32fd42006-02-19 22:21:32 -0800280 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800281
282 return ret;
283
284bad:
285 if (printk_ratelimit())
286 WARN_ON(1);
David S. Millerad7ad572007-07-27 22:39:14 -0700287 return DMA_ERROR_CODE;
David S. Miller6a32fd42006-02-19 22:21:32 -0800288
289iommu_map_fail:
290 /* Interrupts are disabled. */
291 spin_lock(&iommu->lock);
David S. Millerd2841422008-02-08 18:05:46 -0800292 iommu_range_free(iommu, bus_addr, npages);
David S. Miller6a32fd42006-02-19 22:21:32 -0800293 spin_unlock_irqrestore(&iommu->lock, flags);
294
David S. Millerad7ad572007-07-27 22:39:14 -0700295 return DMA_ERROR_CODE;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800296}
297
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000298static void dma_4v_unmap_page(struct device *dev, dma_addr_t bus_addr,
299 size_t sz, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800300{
David S. Millera2fb23a2007-02-28 23:35:04 -0800301 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700302 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800303 unsigned long flags, npages;
David S. Miller18397942006-02-10 00:08:26 -0800304 long entry;
David S. Miller7c8f4862006-02-13 21:50:27 -0800305 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800306
David S. Millerad7ad572007-07-27 22:39:14 -0700307 if (unlikely(direction == DMA_NONE)) {
David S. Miller18397942006-02-10 00:08:26 -0800308 if (printk_ratelimit())
309 WARN_ON(1);
310 return;
311 }
312
David S. Millerad7ad572007-07-27 22:39:14 -0700313 iommu = dev->archdata.iommu;
314 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800315 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800316
317 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
318 npages >>= IO_PAGE_SHIFT;
319 bus_addr &= IO_PAGE_MASK;
320
321 spin_lock_irqsave(&iommu->lock, flags);
322
David S. Millerd2841422008-02-08 18:05:46 -0800323 iommu_range_free(iommu, bus_addr, npages);
David S. Miller18397942006-02-10 00:08:26 -0800324
David S. Millerd2841422008-02-08 18:05:46 -0800325 entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800326 do {
327 unsigned long num;
328
329 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
330 npages);
331 entry += num;
332 npages -= num;
333 } while (npages != 0);
334
335 spin_unlock_irqrestore(&iommu->lock, flags);
336}
337
David S. Millerad7ad572007-07-27 22:39:14 -0700338static int dma_4v_map_sg(struct device *dev, struct scatterlist *sglist,
339 int nelems, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800340{
David S. Miller13fa14e2008-02-09 03:11:01 -0800341 struct scatterlist *s, *outs, *segstart;
342 unsigned long flags, handle, prot;
343 dma_addr_t dma_next = 0, dma_addr;
344 unsigned int max_seg_size;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700345 unsigned long seg_boundary_size;
David S. Miller13fa14e2008-02-09 03:11:01 -0800346 int outcount, incount, i;
David S. Miller16ce82d2007-04-26 21:08:21 -0700347 struct iommu *iommu;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700348 unsigned long base_shift;
David S. Miller13fa14e2008-02-09 03:11:01 -0800349 long err;
David S. Miller18397942006-02-10 00:08:26 -0800350
David S. Miller13fa14e2008-02-09 03:11:01 -0800351 BUG_ON(direction == DMA_NONE);
David S. Miller18397942006-02-10 00:08:26 -0800352
David S. Millerad7ad572007-07-27 22:39:14 -0700353 iommu = dev->archdata.iommu;
David S. Miller13fa14e2008-02-09 03:11:01 -0800354 if (nelems == 0 || !iommu)
355 return 0;
David S. Miller18397942006-02-10 00:08:26 -0800356
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. Miller13fa14e2008-02-09 03:11:01 -0800361 outs = s = segstart = &sglist[0];
362 outcount = 1;
363 incount = nelems;
364 handle = 0;
David S. Miller38192d52008-02-06 03:50:26 -0800365
David S. Miller13fa14e2008-02-09 03:11:01 -0800366 /* Init first segment length for backout at failure */
367 outs->dma_length = 0;
David S. Miller38192d52008-02-06 03:50:26 -0800368
David S. Miller13fa14e2008-02-09 03:11:01 -0800369 spin_lock_irqsave(&iommu->lock, flags);
David S. Miller38192d52008-02-06 03:50:26 -0800370
David S. Miller13fa14e2008-02-09 03:11:01 -0800371 iommu_batch_start(dev, prot, ~0UL);
David S. Miller38192d52008-02-06 03:50:26 -0800372
David S. Miller13fa14e2008-02-09 03:11:01 -0800373 max_seg_size = dma_get_max_seg_size(dev);
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700374 seg_boundary_size = ALIGN(dma_get_seg_boundary(dev) + 1,
375 IO_PAGE_SIZE) >> IO_PAGE_SHIFT;
376 base_shift = iommu->page_table_map_base >> IO_PAGE_SHIFT;
David S. Miller13fa14e2008-02-09 03:11:01 -0800377 for_each_sg(sglist, s, nelems, i) {
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700378 unsigned long paddr, npages, entry, out_entry = 0, slen;
David S. Miller38192d52008-02-06 03:50:26 -0800379
David S. Miller13fa14e2008-02-09 03:11:01 -0800380 slen = s->length;
381 /* Sanity check */
382 if (slen == 0) {
383 dma_next = 0;
384 continue;
David S. Miller38192d52008-02-06 03:50:26 -0800385 }
David S. Miller13fa14e2008-02-09 03:11:01 -0800386 /* Allocate iommu entries for that segment */
387 paddr = (unsigned long) SG_ENT_PHYS_ADDRESS(s);
Joerg Roedel0fcff282008-10-15 22:02:14 -0700388 npages = iommu_num_pages(paddr, slen, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800389 entry = iommu_range_alloc(dev, iommu, npages, &handle);
390
391 /* Handle failure */
392 if (unlikely(entry == DMA_ERROR_CODE)) {
393 if (printk_ratelimit())
394 printk(KERN_INFO "iommu_alloc failed, iommu %p paddr %lx"
395 " npages %lx\n", iommu, paddr, npages);
396 goto iommu_map_failed;
397 }
398
399 iommu_batch_new_entry(entry);
400
401 /* Convert entry to a dma_addr_t */
402 dma_addr = iommu->page_table_map_base +
403 (entry << IO_PAGE_SHIFT);
404 dma_addr |= (s->offset & ~IO_PAGE_MASK);
405
406 /* Insert into HW table */
407 paddr &= IO_PAGE_MASK;
408 while (npages--) {
409 err = iommu_batch_add(paddr);
410 if (unlikely(err < 0L))
411 goto iommu_map_failed;
412 paddr += IO_PAGE_SIZE;
413 }
414
415 /* If we are in an open segment, try merging */
416 if (segstart != s) {
417 /* We cannot merge if:
418 * - allocated dma_addr isn't contiguous to previous allocation
419 */
420 if ((dma_addr != dma_next) ||
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700421 (outs->dma_length + s->length > max_seg_size) ||
422 (is_span_boundary(out_entry, base_shift,
423 seg_boundary_size, outs, s))) {
David S. Miller13fa14e2008-02-09 03:11:01 -0800424 /* Can't merge: create a new segment */
425 segstart = s;
426 outcount++;
427 outs = sg_next(outs);
428 } else {
429 outs->dma_length += s->length;
430 }
431 }
432
433 if (segstart == s) {
434 /* This is a new segment, fill entries */
435 outs->dma_address = dma_addr;
436 outs->dma_length = slen;
FUJITA Tomonorif0880252008-03-28 15:55:41 -0700437 out_entry = entry;
David S. Miller13fa14e2008-02-09 03:11:01 -0800438 }
439
440 /* Calculate next page pointer for contiguous check */
441 dma_next = dma_addr + slen;
David S. Miller38192d52008-02-06 03:50:26 -0800442 }
443
444 err = iommu_batch_end();
445
David S. Miller6a32fd42006-02-19 22:21:32 -0800446 if (unlikely(err < 0L))
447 goto iommu_map_failed;
David S. Miller18397942006-02-10 00:08:26 -0800448
David S. Miller13fa14e2008-02-09 03:11:01 -0800449 spin_unlock_irqrestore(&iommu->lock, flags);
David S. Miller18397942006-02-10 00:08:26 -0800450
David S. Miller13fa14e2008-02-09 03:11:01 -0800451 if (outcount < incount) {
452 outs = sg_next(outs);
453 outs->dma_address = DMA_ERROR_CODE;
454 outs->dma_length = 0;
455 }
456
457 return outcount;
David S. Miller6a32fd42006-02-19 22:21:32 -0800458
459iommu_map_failed:
David S. Miller13fa14e2008-02-09 03:11:01 -0800460 for_each_sg(sglist, s, nelems, i) {
461 if (s->dma_length != 0) {
462 unsigned long vaddr, npages;
463
464 vaddr = s->dma_address & IO_PAGE_MASK;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700465 npages = iommu_num_pages(s->dma_address, s->dma_length,
466 IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800467 iommu_range_free(iommu, vaddr, npages);
468 /* XXX demap? XXX */
469 s->dma_address = DMA_ERROR_CODE;
470 s->dma_length = 0;
471 }
472 if (s == outs)
473 break;
474 }
David S. Miller6a32fd42006-02-19 22:21:32 -0800475 spin_unlock_irqrestore(&iommu->lock, flags);
476
477 return 0;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800478}
479
David S. Millerad7ad572007-07-27 22:39:14 -0700480static void dma_4v_unmap_sg(struct device *dev, struct scatterlist *sglist,
481 int nelems, enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800482{
David S. Millera2fb23a2007-02-28 23:35:04 -0800483 struct pci_pbm_info *pbm;
David S. Miller13fa14e2008-02-09 03:11:01 -0800484 struct scatterlist *sg;
David S. Miller38192d52008-02-06 03:50:26 -0800485 struct iommu *iommu;
David S. Miller13fa14e2008-02-09 03:11:01 -0800486 unsigned long flags;
487 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800488
David S. Miller13fa14e2008-02-09 03:11:01 -0800489 BUG_ON(direction == DMA_NONE);
David S. Miller18397942006-02-10 00:08:26 -0800490
David S. Millerad7ad572007-07-27 22:39:14 -0700491 iommu = dev->archdata.iommu;
492 pbm = dev->archdata.host_controller;
David S. Millera2fb23a2007-02-28 23:35:04 -0800493 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800494
David S. Miller18397942006-02-10 00:08:26 -0800495 spin_lock_irqsave(&iommu->lock, flags);
496
David S. Miller13fa14e2008-02-09 03:11:01 -0800497 sg = sglist;
498 while (nelems--) {
499 dma_addr_t dma_handle = sg->dma_address;
500 unsigned int len = sg->dma_length;
501 unsigned long npages, entry;
David S. Miller18397942006-02-10 00:08:26 -0800502
David S. Miller13fa14e2008-02-09 03:11:01 -0800503 if (!len)
504 break;
Joerg Roedel0fcff282008-10-15 22:02:14 -0700505 npages = iommu_num_pages(dma_handle, len, IO_PAGE_SIZE);
David S. Miller13fa14e2008-02-09 03:11:01 -0800506 iommu_range_free(iommu, dma_handle, npages);
David S. Miller18397942006-02-10 00:08:26 -0800507
David S. Miller13fa14e2008-02-09 03:11:01 -0800508 entry = ((dma_handle - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
509 while (npages) {
510 unsigned long num;
511
512 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
513 npages);
514 entry += num;
515 npages -= num;
516 }
517
518 sg = sg_next(sg);
519 }
David S. Miller18397942006-02-10 00:08:26 -0800520
521 spin_unlock_irqrestore(&iommu->lock, flags);
David S. Miller8f6a93a2006-02-09 21:32:07 -0800522}
523
David S. Millerad7ad572007-07-27 22:39:14 -0700524static void dma_4v_sync_single_for_cpu(struct device *dev,
525 dma_addr_t bus_addr, size_t sz,
526 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800527{
David S. Miller18397942006-02-10 00:08:26 -0800528 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800529}
530
David S. Millerad7ad572007-07-27 22:39:14 -0700531static void dma_4v_sync_sg_for_cpu(struct device *dev,
532 struct scatterlist *sglist, int nelems,
533 enum dma_data_direction direction)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800534{
David S. Miller18397942006-02-10 00:08:26 -0800535 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800536}
537
Adrian Bunk908f5162008-06-05 11:42:40 -0700538static const struct dma_ops sun4v_dma_ops = {
David S. Millerad7ad572007-07-27 22:39:14 -0700539 .alloc_coherent = dma_4v_alloc_coherent,
540 .free_coherent = dma_4v_free_coherent,
FUJITA Tomonori797a7562009-05-14 16:23:10 +0000541 .map_page = dma_4v_map_page,
542 .unmap_page = dma_4v_unmap_page,
David S. Millerad7ad572007-07-27 22:39:14 -0700543 .map_sg = dma_4v_map_sg,
544 .unmap_sg = dma_4v_unmap_sg,
545 .sync_single_for_cpu = dma_4v_sync_single_for_cpu,
546 .sync_sg_for_cpu = dma_4v_sync_sg_for_cpu,
David S. Miller8f6a93a2006-02-09 21:32:07 -0800547};
548
David S. Miller9a2ed5c2009-04-07 01:03:58 -0700549static void __devinit pci_sun4v_scan_bus(struct pci_pbm_info *pbm,
550 struct device *parent)
David S. Millerbade5622006-02-09 22:05:54 -0800551{
David S. Millere87dc352006-06-21 18:18:47 -0700552 struct property *prop;
553 struct device_node *dp;
554
David S. Miller22fecba2008-09-10 00:19:28 -0700555 dp = pbm->op->node;
David S. Miller34768bc2007-05-07 23:06:27 -0700556 prop = of_find_property(dp, "66mhz-capable", NULL);
557 pbm->is_66mhz_capable = (prop != NULL);
David S. Millere822358a2008-09-01 18:32:22 -0700558 pbm->pci_bus = pci_scan_one_pbm(pbm, parent);
David S. Millerc2609262006-02-12 22:18:52 -0800559
560 /* XXX register error interrupt handlers XXX */
David S. Millerbade5622006-02-09 22:05:54 -0800561}
562
David S. Miller9a2ed5c2009-04-07 01:03:58 -0700563static unsigned long __devinit probe_existing_entries(struct pci_pbm_info *pbm,
564 struct iommu *iommu)
David S. Miller18397942006-02-10 00:08:26 -0800565{
David S. Miller9b3627f2007-04-24 23:51:18 -0700566 struct iommu_arena *arena = &iommu->arena;
David S. Millere7a04532006-02-15 22:25:27 -0800567 unsigned long i, cnt = 0;
David S. Miller7c8f4862006-02-13 21:50:27 -0800568 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800569
570 devhandle = pbm->devhandle;
571 for (i = 0; i < arena->limit; i++) {
572 unsigned long ret, io_attrs, ra;
573
574 ret = pci_sun4v_iommu_getmap(devhandle,
575 HV_PCI_TSBID(0, i),
576 &io_attrs, &ra);
David S. Millere7a04532006-02-15 22:25:27 -0800577 if (ret == HV_EOK) {
David S. Millerc2a5a462006-06-22 00:01:56 -0700578 if (page_in_phys_avail(ra)) {
579 pci_sun4v_iommu_demap(devhandle,
580 HV_PCI_TSBID(0, i), 1);
581 } else {
582 cnt++;
583 __set_bit(i, arena->map);
584 }
David S. Millere7a04532006-02-15 22:25:27 -0800585 }
David S. Miller18397942006-02-10 00:08:26 -0800586 }
David S. Millere7a04532006-02-15 22:25:27 -0800587
588 return cnt;
David S. Miller18397942006-02-10 00:08:26 -0800589}
590
David S. Miller9a2ed5c2009-04-07 01:03:58 -0700591static int __devinit pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
David S. Millerbade5622006-02-09 22:05:54 -0800592{
David S. Miller8aef7272008-09-01 20:23:18 -0700593 static const u32 vdma_default[] = { 0x80000000, 0x80000000 };
David S. Miller16ce82d2007-04-26 21:08:21 -0700594 struct iommu *iommu = pbm->iommu;
David S. Miller59db8102007-05-23 18:00:46 -0700595 unsigned long num_tsb_entries, sz, tsbsize;
David S. Miller8aef7272008-09-01 20:23:18 -0700596 u32 dma_mask, dma_offset;
597 const u32 *vdma;
David S. Miller18397942006-02-10 00:08:26 -0800598
David S. Miller22fecba2008-09-10 00:19:28 -0700599 vdma = of_get_property(pbm->op->node, "virtual-dma", NULL);
David S. Miller8aef7272008-09-01 20:23:18 -0700600 if (!vdma)
601 vdma = vdma_default;
David S. Miller18397942006-02-10 00:08:26 -0800602
David S. Miller59db8102007-05-23 18:00:46 -0700603 if ((vdma[0] | vdma[1]) & ~IO_PAGE_MASK) {
David S. Miller3822b502008-08-30 02:50:29 -0700604 printk(KERN_ERR PFX "Strange virtual-dma[%08x:%08x].\n",
605 vdma[0], vdma[1]);
606 return -EINVAL;
David S. Miller18397942006-02-10 00:08:26 -0800607 };
608
David S. Miller59db8102007-05-23 18:00:46 -0700609 dma_mask = (roundup_pow_of_two(vdma[1]) - 1UL);
610 num_tsb_entries = vdma[1] / IO_PAGE_SIZE;
611 tsbsize = num_tsb_entries * sizeof(iopte_t);
David S. Miller18397942006-02-10 00:08:26 -0800612
613 dma_offset = vdma[0];
614
615 /* Setup initial software IOMMU state. */
616 spin_lock_init(&iommu->lock);
617 iommu->ctx_lowest_free = 1;
618 iommu->page_table_map_base = dma_offset;
619 iommu->dma_addr_mask = dma_mask;
620
621 /* Allocate and initialize the free area map. */
David S. Miller59db8102007-05-23 18:00:46 -0700622 sz = (num_tsb_entries + 7) / 8;
David S. Miller18397942006-02-10 00:08:26 -0800623 sz = (sz + 7UL) & ~7UL;
Yan Burman982c2062006-11-30 17:13:09 -0800624 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
David S. Miller18397942006-02-10 00:08:26 -0800625 if (!iommu->arena.map) {
David S. Miller3822b502008-08-30 02:50:29 -0700626 printk(KERN_ERR PFX "Error, kmalloc(arena.map) failed.\n");
627 return -ENOMEM;
David S. Miller18397942006-02-10 00:08:26 -0800628 }
David S. Miller18397942006-02-10 00:08:26 -0800629 iommu->arena.limit = num_tsb_entries;
630
David S. Millere7a04532006-02-15 22:25:27 -0800631 sz = probe_existing_entries(pbm, iommu);
David S. Millerc2a5a462006-06-22 00:01:56 -0700632 if (sz)
633 printk("%s: Imported %lu TSB entries from OBP\n",
634 pbm->name, sz);
David S. Miller3822b502008-08-30 02:50:29 -0700635
636 return 0;
David S. Millerbade5622006-02-09 22:05:54 -0800637}
638
David S. Miller35a17eb2007-02-10 17:41:02 -0800639#ifdef CONFIG_PCI_MSI
640struct pci_sun4v_msiq_entry {
641 u64 version_type;
642#define MSIQ_VERSION_MASK 0xffffffff00000000UL
643#define MSIQ_VERSION_SHIFT 32
644#define MSIQ_TYPE_MASK 0x00000000000000ffUL
645#define MSIQ_TYPE_SHIFT 0
646#define MSIQ_TYPE_NONE 0x00
647#define MSIQ_TYPE_MSG 0x01
648#define MSIQ_TYPE_MSI32 0x02
649#define MSIQ_TYPE_MSI64 0x03
650#define MSIQ_TYPE_INTX 0x08
651#define MSIQ_TYPE_NONE2 0xff
652
653 u64 intx_sysino;
654 u64 reserved1;
655 u64 stick;
656 u64 req_id; /* bus/device/func */
657#define MSIQ_REQID_BUS_MASK 0xff00UL
658#define MSIQ_REQID_BUS_SHIFT 8
659#define MSIQ_REQID_DEVICE_MASK 0x00f8UL
660#define MSIQ_REQID_DEVICE_SHIFT 3
661#define MSIQ_REQID_FUNC_MASK 0x0007UL
662#define MSIQ_REQID_FUNC_SHIFT 0
663
664 u64 msi_address;
665
Simon Arlotte5dd42e2007-05-11 13:52:08 -0700666 /* The format of this value is message type dependent.
David S. Miller35a17eb2007-02-10 17:41:02 -0800667 * For MSI bits 15:0 are the data from the MSI packet.
668 * For MSI-X bits 31:0 are the data from the MSI packet.
669 * For MSG, the message code and message routing code where:
670 * bits 39:32 is the bus/device/fn of the msg target-id
671 * bits 18:16 is the message routing code
672 * bits 7:0 is the message code
673 * For INTx the low order 2-bits are:
674 * 00 - INTA
675 * 01 - INTB
676 * 10 - INTC
677 * 11 - INTD
678 */
679 u64 msi_data;
680
681 u64 reserved2;
682};
683
David S. Miller759f89e2007-10-11 03:16:13 -0700684static int pci_sun4v_get_head(struct pci_pbm_info *pbm, unsigned long msiqid,
685 unsigned long *head)
David S. Miller35a17eb2007-02-10 17:41:02 -0800686{
David S. Miller759f89e2007-10-11 03:16:13 -0700687 unsigned long err, limit;
David S. Miller35a17eb2007-02-10 17:41:02 -0800688
David S. Miller759f89e2007-10-11 03:16:13 -0700689 err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, head);
David S. Miller35a17eb2007-02-10 17:41:02 -0800690 if (unlikely(err))
David S. Miller759f89e2007-10-11 03:16:13 -0700691 return -ENXIO;
David S. Miller35a17eb2007-02-10 17:41:02 -0800692
David S. Miller759f89e2007-10-11 03:16:13 -0700693 limit = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
694 if (unlikely(*head >= limit))
695 return -EFBIG;
David S. Miller35a17eb2007-02-10 17:41:02 -0800696
697 return 0;
698}
699
David S. Miller759f89e2007-10-11 03:16:13 -0700700static int pci_sun4v_dequeue_msi(struct pci_pbm_info *pbm,
701 unsigned long msiqid, unsigned long *head,
702 unsigned long *msi)
David S. Miller35a17eb2007-02-10 17:41:02 -0800703{
David S. Miller759f89e2007-10-11 03:16:13 -0700704 struct pci_sun4v_msiq_entry *ep;
705 unsigned long err, type;
706
707 /* Note: void pointer arithmetic, 'head' is a byte offset */
708 ep = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
709 (pbm->msiq_ent_count *
710 sizeof(struct pci_sun4v_msiq_entry))) +
711 *head);
712
713 if ((ep->version_type & MSIQ_TYPE_MASK) == 0)
714 return 0;
715
716 type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
717 if (unlikely(type != MSIQ_TYPE_MSI32 &&
718 type != MSIQ_TYPE_MSI64))
719 return -EINVAL;
720
721 *msi = ep->msi_data;
722
723 err = pci_sun4v_msi_setstate(pbm->devhandle,
724 ep->msi_data /* msi_num */,
725 HV_MSISTATE_IDLE);
726 if (unlikely(err))
727 return -ENXIO;
728
729 /* Clear the entry. */
730 ep->version_type &= ~MSIQ_TYPE_MASK;
731
732 (*head) += sizeof(struct pci_sun4v_msiq_entry);
733 if (*head >=
734 (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry)))
735 *head = 0;
736
737 return 1;
David S. Miller35a17eb2007-02-10 17:41:02 -0800738}
739
David S. Miller759f89e2007-10-11 03:16:13 -0700740static int pci_sun4v_set_head(struct pci_pbm_info *pbm, unsigned long msiqid,
741 unsigned long head)
742{
743 unsigned long err;
744
745 err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
746 if (unlikely(err))
747 return -EINVAL;
748
749 return 0;
750}
751
752static int pci_sun4v_msi_setup(struct pci_pbm_info *pbm, unsigned long msiqid,
753 unsigned long msi, int is_msi64)
754{
755 if (pci_sun4v_msi_setmsiq(pbm->devhandle, msi, msiqid,
756 (is_msi64 ?
757 HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
758 return -ENXIO;
759 if (pci_sun4v_msi_setstate(pbm->devhandle, msi, HV_MSISTATE_IDLE))
760 return -ENXIO;
761 if (pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_VALID))
762 return -ENXIO;
763 return 0;
764}
765
766static int pci_sun4v_msi_teardown(struct pci_pbm_info *pbm, unsigned long msi)
767{
768 unsigned long err, msiqid;
769
770 err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi, &msiqid);
771 if (err)
772 return -ENXIO;
773
774 pci_sun4v_msi_setvalid(pbm->devhandle, msi, HV_MSIVALID_INVALID);
775
776 return 0;
777}
778
779static int pci_sun4v_msiq_alloc(struct pci_pbm_info *pbm)
David S. Miller35a17eb2007-02-10 17:41:02 -0800780{
781 unsigned long q_size, alloc_size, pages, order;
782 int i;
783
784 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
785 alloc_size = (pbm->msiq_num * q_size);
786 order = get_order(alloc_size);
787 pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
788 if (pages == 0UL) {
789 printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
790 order);
791 return -ENOMEM;
792 }
793 memset((char *)pages, 0, PAGE_SIZE << order);
794 pbm->msi_queues = (void *) pages;
795
796 for (i = 0; i < pbm->msiq_num; i++) {
797 unsigned long err, base = __pa(pages + (i * q_size));
798 unsigned long ret1, ret2;
799
800 err = pci_sun4v_msiq_conf(pbm->devhandle,
801 pbm->msiq_first + i,
802 base, pbm->msiq_ent_count);
803 if (err) {
804 printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
805 err);
806 goto h_error;
807 }
808
809 err = pci_sun4v_msiq_info(pbm->devhandle,
810 pbm->msiq_first + i,
811 &ret1, &ret2);
812 if (err) {
813 printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
814 err);
815 goto h_error;
816 }
817 if (ret1 != base || ret2 != pbm->msiq_ent_count) {
818 printk(KERN_ERR "MSI: Bogus qconf "
819 "expected[%lx:%x] got[%lx:%lx]\n",
820 base, pbm->msiq_ent_count,
821 ret1, ret2);
822 goto h_error;
823 }
824 }
825
826 return 0;
827
828h_error:
829 free_pages(pages, order);
830 return -EINVAL;
831}
832
David S. Miller759f89e2007-10-11 03:16:13 -0700833static void pci_sun4v_msiq_free(struct pci_pbm_info *pbm)
David S. Miller35a17eb2007-02-10 17:41:02 -0800834{
David S. Miller759f89e2007-10-11 03:16:13 -0700835 unsigned long q_size, alloc_size, pages, order;
David S. Miller35a17eb2007-02-10 17:41:02 -0800836 int i;
837
David S. Miller759f89e2007-10-11 03:16:13 -0700838 for (i = 0; i < pbm->msiq_num; i++) {
839 unsigned long msiqid = pbm->msiq_first + i;
840
841 (void) pci_sun4v_msiq_conf(pbm->devhandle, msiqid, 0UL, 0);
David S. Miller35a17eb2007-02-10 17:41:02 -0800842 }
843
David S. Miller759f89e2007-10-11 03:16:13 -0700844 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
845 alloc_size = (pbm->msiq_num * q_size);
846 order = get_order(alloc_size);
847
848 pages = (unsigned long) pbm->msi_queues;
849
850 free_pages(pages, order);
851
852 pbm->msi_queues = NULL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800853}
854
David S. Miller759f89e2007-10-11 03:16:13 -0700855static int pci_sun4v_msiq_build_irq(struct pci_pbm_info *pbm,
856 unsigned long msiqid,
857 unsigned long devino)
David S. Miller35a17eb2007-02-10 17:41:02 -0800858{
David S. Miller759f89e2007-10-11 03:16:13 -0700859 unsigned int virt_irq = sun4v_build_irq(pbm->devhandle, devino);
David S. Miller35a17eb2007-02-10 17:41:02 -0800860
David S. Miller759f89e2007-10-11 03:16:13 -0700861 if (!virt_irq)
862 return -ENOMEM;
David S. Miller35a17eb2007-02-10 17:41:02 -0800863
David S. Miller35a17eb2007-02-10 17:41:02 -0800864 if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
David S. Miller759f89e2007-10-11 03:16:13 -0700865 return -EINVAL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800866 if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
David S. Miller759f89e2007-10-11 03:16:13 -0700867 return -EINVAL;
David S. Miller35a17eb2007-02-10 17:41:02 -0800868
David S. Miller759f89e2007-10-11 03:16:13 -0700869 return virt_irq;
David S. Miller35a17eb2007-02-10 17:41:02 -0800870}
871
David S. Miller759f89e2007-10-11 03:16:13 -0700872static const struct sparc64_msiq_ops pci_sun4v_msiq_ops = {
873 .get_head = pci_sun4v_get_head,
874 .dequeue_msi = pci_sun4v_dequeue_msi,
875 .set_head = pci_sun4v_set_head,
876 .msi_setup = pci_sun4v_msi_setup,
877 .msi_teardown = pci_sun4v_msi_teardown,
878 .msiq_alloc = pci_sun4v_msiq_alloc,
879 .msiq_free = pci_sun4v_msiq_free,
880 .msiq_build_irq = pci_sun4v_msiq_build_irq,
881};
David S. Millere9870c42007-05-07 23:28:50 -0700882
883static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
884{
David S. Miller759f89e2007-10-11 03:16:13 -0700885 sparc64_pbm_msi_init(pbm, &pci_sun4v_msiq_ops);
David S. Millere9870c42007-05-07 23:28:50 -0700886}
David S. Miller35a17eb2007-02-10 17:41:02 -0800887#else /* CONFIG_PCI_MSI */
888static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
889{
890}
891#endif /* !(CONFIG_PCI_MSI) */
892
David S. Miller9a2ed5c2009-04-07 01:03:58 -0700893static int __devinit pci_sun4v_pbm_init(struct pci_pbm_info *pbm,
894 struct of_device *op, u32 devhandle)
David S. Millerbade5622006-02-09 22:05:54 -0800895{
David S. Millere822358a2008-09-01 18:32:22 -0700896 struct device_node *dp = op->node;
David S. Miller3822b502008-08-30 02:50:29 -0700897 int err;
David S. Millerbade5622006-02-09 22:05:54 -0800898
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700899 pbm->numa_node = of_node_to_nid(dp);
900
David S. Millerca3dd882007-05-09 02:35:27 -0700901 pbm->pci_ops = &sun4v_pci_ops;
902 pbm->config_space_reg_bits = 12;
David S. Miller34768bc2007-05-07 23:06:27 -0700903
David S. Miller6c108f12007-05-07 23:49:01 -0700904 pbm->index = pci_num_pbms++;
905
David S. Miller22fecba2008-09-10 00:19:28 -0700906 pbm->op = op;
David S. Millerbade5622006-02-09 22:05:54 -0800907
David S. Miller38337892006-02-12 22:06:53 -0800908 pbm->devhandle = devhandle;
David S. Millerbade5622006-02-09 22:05:54 -0800909
David S. Millere87dc352006-06-21 18:18:47 -0700910 pbm->name = dp->full_name;
David S. Millerbade5622006-02-09 22:05:54 -0800911
David S. Millere87dc352006-06-21 18:18:47 -0700912 printk("%s: SUN4V PCI Bus Module\n", pbm->name);
David S. Millerc1b1a5f2008-03-19 04:52:48 -0700913 printk("%s: On NUMA node %d\n", pbm->name, pbm->numa_node);
David S. Millerbade5622006-02-09 22:05:54 -0800914
David S. Miller9fd8b642007-03-08 21:55:49 -0800915 pci_determine_mem_io_space(pbm);
David S. Millerbade5622006-02-09 22:05:54 -0800916
David S. Millercfa06522007-05-07 21:51:41 -0700917 pci_get_pbm_props(pbm);
David S. Miller3822b502008-08-30 02:50:29 -0700918
919 err = pci_sun4v_iommu_init(pbm);
920 if (err)
921 return err;
922
David S. Miller35a17eb2007-02-10 17:41:02 -0800923 pci_sun4v_msi_init(pbm);
David S. Miller3822b502008-08-30 02:50:29 -0700924
David S. Millere822358a2008-09-01 18:32:22 -0700925 pci_sun4v_scan_bus(pbm, &op->dev);
David S. Miller3822b502008-08-30 02:50:29 -0700926
David S. Millerd3ae4b52008-09-09 23:54:02 -0700927 pbm->next = pci_pbm_root;
928 pci_pbm_root = pbm;
929
David S. Miller3822b502008-08-30 02:50:29 -0700930 return 0;
David S. Millerbade5622006-02-09 22:05:54 -0800931}
932
Linus Torvalds33b07db2008-12-01 07:55:14 -0800933static int __devinit pci_sun4v_probe(struct of_device *op,
David S. Miller3822b502008-08-30 02:50:29 -0700934 const struct of_device_id *match)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800935{
David S. Miller3822b502008-08-30 02:50:29 -0700936 const struct linux_prom64_registers *regs;
David S. Millere01c0d62007-05-25 01:04:15 -0700937 static int hvapi_negotiated = 0;
David S. Miller34768bc2007-05-07 23:06:27 -0700938 struct pci_pbm_info *pbm;
David S. Miller3822b502008-08-30 02:50:29 -0700939 struct device_node *dp;
David S. Miller16ce82d2007-04-26 21:08:21 -0700940 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800941 u32 devhandle;
David S. Millerd7472c32008-08-31 01:33:52 -0700942 int i, err;
David S. Miller38337892006-02-12 22:06:53 -0800943
David S. Miller3822b502008-08-30 02:50:29 -0700944 dp = op->node;
945
David S. Millere01c0d62007-05-25 01:04:15 -0700946 if (!hvapi_negotiated++) {
David S. Miller8d2aec52008-09-12 00:01:03 -0700947 err = sun4v_hvapi_register(HV_GRP_PCI,
948 vpci_major,
949 &vpci_minor);
David S. Millere01c0d62007-05-25 01:04:15 -0700950
951 if (err) {
David S. Miller3822b502008-08-30 02:50:29 -0700952 printk(KERN_ERR PFX "Could not register hvapi, "
953 "err=%d\n", err);
954 return err;
David S. Millere01c0d62007-05-25 01:04:15 -0700955 }
David S. Miller3822b502008-08-30 02:50:29 -0700956 printk(KERN_INFO PFX "Registered hvapi major[%lu] minor[%lu]\n",
David S. Millere01c0d62007-05-25 01:04:15 -0700957 vpci_major, vpci_minor);
David S. Millerad7ad572007-07-27 22:39:14 -0700958
959 dma_ops = &sun4v_dma_ops;
David S. Millere01c0d62007-05-25 01:04:15 -0700960 }
961
David S. Miller3822b502008-08-30 02:50:29 -0700962 regs = of_get_property(dp, "reg", NULL);
David S. Millerd7472c32008-08-31 01:33:52 -0700963 err = -ENODEV;
David S. Miller3822b502008-08-30 02:50:29 -0700964 if (!regs) {
965 printk(KERN_ERR PFX "Could not find config registers\n");
David S. Millerd7472c32008-08-31 01:33:52 -0700966 goto out_err;
Cyrill Gorcunov75c6d142007-11-20 17:32:19 -0800967 }
David S. Millere87dc352006-06-21 18:18:47 -0700968 devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
David S. Miller38337892006-02-12 22:06:53 -0800969
David S. Millerd7472c32008-08-31 01:33:52 -0700970 err = -ENOMEM;
David S. Millerd3ae4b52008-09-09 23:54:02 -0700971 if (!iommu_batch_initialized) {
972 for_each_possible_cpu(i) {
973 unsigned long page = get_zeroed_page(GFP_KERNEL);
David S. Miller7c8f4862006-02-13 21:50:27 -0800974
David S. Millerd3ae4b52008-09-09 23:54:02 -0700975 if (!page)
976 goto out_err;
David S. Miller7c8f4862006-02-13 21:50:27 -0800977
David S. Millerd3ae4b52008-09-09 23:54:02 -0700978 per_cpu(iommu_batch, i).pglist = (u64 *) page;
979 }
980 iommu_batch_initialized = 1;
David S. Millerbade5622006-02-09 22:05:54 -0800981 }
David S. Miller7c8f4862006-02-13 21:50:27 -0800982
David S. Millerd3ae4b52008-09-09 23:54:02 -0700983 pbm = kzalloc(sizeof(*pbm), GFP_KERNEL);
984 if (!pbm) {
985 printk(KERN_ERR PFX "Could not allocate pci_pbm_info\n");
David S. Millerd7472c32008-08-31 01:33:52 -0700986 goto out_err;
David S. Miller3822b502008-08-30 02:50:29 -0700987 }
David S. Miller7c8f4862006-02-13 21:50:27 -0800988
David S. Millerd3ae4b52008-09-09 23:54:02 -0700989 iommu = kzalloc(sizeof(struct iommu), GFP_KERNEL);
David S. Miller3822b502008-08-30 02:50:29 -0700990 if (!iommu) {
David S. Millerd3ae4b52008-09-09 23:54:02 -0700991 printk(KERN_ERR PFX "Could not allocate pbm iommu\n");
David S. Millerd7472c32008-08-31 01:33:52 -0700992 goto out_free_controller;
David S. Miller3822b502008-08-30 02:50:29 -0700993 }
David S. Miller7c8f4862006-02-13 21:50:27 -0800994
David S. Millerd3ae4b52008-09-09 23:54:02 -0700995 pbm->iommu = iommu;
David S. Millerbade5622006-02-09 22:05:54 -0800996
David S. Millerd3ae4b52008-09-09 23:54:02 -0700997 err = pci_sun4v_pbm_init(pbm, op, devhandle);
998 if (err)
999 goto out_free_iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -08001000
David S. Millerd3ae4b52008-09-09 23:54:02 -07001001 dev_set_drvdata(&op->dev, pbm);
David S. Millerbade5622006-02-09 22:05:54 -08001002
David S. Millerd3ae4b52008-09-09 23:54:02 -07001003 return 0;
David S. Miller7c8f4862006-02-13 21:50:27 -08001004
David S. Millerd3ae4b52008-09-09 23:54:02 -07001005out_free_iommu:
1006 kfree(pbm->iommu);
David S. Millerd7472c32008-08-31 01:33:52 -07001007
1008out_free_controller:
David S. Millerd3ae4b52008-09-09 23:54:02 -07001009 kfree(pbm);
David S. Millerd7472c32008-08-31 01:33:52 -07001010
1011out_err:
1012 return err;
David S. Miller8f6a93a2006-02-09 21:32:07 -08001013}
David S. Miller3822b502008-08-30 02:50:29 -07001014
David S. Millerfd098312008-08-31 01:23:17 -07001015static struct of_device_id __initdata pci_sun4v_match[] = {
David S. Miller3822b502008-08-30 02:50:29 -07001016 {
1017 .name = "pci",
1018 .compatible = "SUNW,sun4v-pci",
1019 },
1020 {},
1021};
1022
1023static struct of_platform_driver pci_sun4v_driver = {
1024 .name = DRIVER_NAME,
1025 .match_table = pci_sun4v_match,
1026 .probe = pci_sun4v_probe,
1027};
1028
1029static int __init pci_sun4v_init(void)
1030{
1031 return of_register_driver(&pci_sun4v_driver, &of_bus_type);
1032}
1033
1034subsys_initcall(pci_sun4v_init);