blob: 1491ba33058320b993b4586657325adf114a6efb [file] [log] [blame]
David S. Miller8f6a93a2006-02-09 21:32:07 -08001/* pci_sun4v.c: SUN4V specific PCI controller support.
2 *
David S. Miller9fd8b642007-03-08 21:55:49 -08003 * Copyright (C) 2006, 2007 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. Miller8f6a93a2006-02-09 21:32:07 -080015
16#include <asm/pbm.h>
17#include <asm/iommu.h>
18#include <asm/irq.h>
19#include <asm/upa.h>
20#include <asm/pstate.h>
21#include <asm/oplib.h>
22#include <asm/hypervisor.h>
David S. Millere87dc352006-06-21 18:18:47 -070023#include <asm/prom.h>
David S. Miller8f6a93a2006-02-09 21:32:07 -080024
25#include "pci_impl.h"
26#include "iommu_common.h"
27
David S. Millerbade5622006-02-09 22:05:54 -080028#include "pci_sun4v.h"
29
David S. Miller7c8f4862006-02-13 21:50:27 -080030#define PGLIST_NENTS (PAGE_SIZE / sizeof(u64))
David S. Miller18397942006-02-10 00:08:26 -080031
David S. Miller16ce82d2007-04-26 21:08:21 -070032struct iommu_batch {
David S. Miller6a32fd42006-02-19 22:21:32 -080033 struct pci_dev *pdev; /* Device mapping is for. */
34 unsigned long prot; /* IOMMU page protections */
35 unsigned long entry; /* Index into IOTSB. */
36 u64 *pglist; /* List of physical pages */
37 unsigned long npages; /* Number of pages in list. */
David S. Miller18397942006-02-10 00:08:26 -080038};
39
David S. Miller16ce82d2007-04-26 21:08:21 -070040static DEFINE_PER_CPU(struct iommu_batch, pci_iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -080041
42/* Interrupts must be disabled. */
43static inline void pci_iommu_batch_start(struct pci_dev *pdev, unsigned long prot, unsigned long entry)
44{
David S. Miller16ce82d2007-04-26 21:08:21 -070045 struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -080046
47 p->pdev = pdev;
48 p->prot = prot;
49 p->entry = entry;
50 p->npages = 0;
51}
52
53/* Interrupts must be disabled. */
David S. Miller16ce82d2007-04-26 21:08:21 -070054static long pci_iommu_batch_flush(struct iommu_batch *p)
David S. Miller6a32fd42006-02-19 22:21:32 -080055{
David S. Millera2fb23a2007-02-28 23:35:04 -080056 struct pci_pbm_info *pbm = p->pdev->dev.archdata.host_controller;
57 unsigned long devhandle = pbm->devhandle;
David S. Miller6a32fd42006-02-19 22:21:32 -080058 unsigned long prot = p->prot;
59 unsigned long entry = p->entry;
60 u64 *pglist = p->pglist;
61 unsigned long npages = p->npages;
62
David S. Millerd82965c2006-02-20 01:42:51 -080063 while (npages != 0) {
David S. Miller6a32fd42006-02-19 22:21:32 -080064 long num;
65
66 num = pci_sun4v_iommu_map(devhandle, HV_PCI_TSBID(0, entry),
67 npages, prot, __pa(pglist));
68 if (unlikely(num < 0)) {
69 if (printk_ratelimit())
70 printk("pci_iommu_batch_flush: IOMMU map of "
71 "[%08lx:%08lx:%lx:%lx:%lx] failed with "
72 "status %ld\n",
73 devhandle, HV_PCI_TSBID(0, entry),
74 npages, prot, __pa(pglist), num);
75 return -1;
76 }
77
78 entry += num;
79 npages -= num;
80 pglist += num;
David S. Millerd82965c2006-02-20 01:42:51 -080081 }
David S. Miller6a32fd42006-02-19 22:21:32 -080082
83 p->entry = entry;
84 p->npages = 0;
85
86 return 0;
87}
88
89/* Interrupts must be disabled. */
90static inline long pci_iommu_batch_add(u64 phys_page)
91{
David S. Miller16ce82d2007-04-26 21:08:21 -070092 struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -080093
94 BUG_ON(p->npages >= PGLIST_NENTS);
95
96 p->pglist[p->npages++] = phys_page;
97 if (p->npages == PGLIST_NENTS)
98 return pci_iommu_batch_flush(p);
99
100 return 0;
101}
102
103/* Interrupts must be disabled. */
104static inline long pci_iommu_batch_end(void)
105{
David S. Miller16ce82d2007-04-26 21:08:21 -0700106 struct iommu_batch *p = &__get_cpu_var(pci_iommu_batch);
David S. Miller6a32fd42006-02-19 22:21:32 -0800107
108 BUG_ON(p->npages >= PGLIST_NENTS);
109
110 return pci_iommu_batch_flush(p);
111}
David S. Miller18397942006-02-10 00:08:26 -0800112
David S. Miller9b3627f2007-04-24 23:51:18 -0700113static long pci_arena_alloc(struct iommu_arena *arena, unsigned long npages)
David S. Miller18397942006-02-10 00:08:26 -0800114{
115 unsigned long n, i, start, end, limit;
116 int pass;
117
118 limit = arena->limit;
119 start = arena->hint;
120 pass = 0;
121
122again:
123 n = find_next_zero_bit(arena->map, limit, start);
124 end = n + npages;
125 if (unlikely(end >= limit)) {
126 if (likely(pass < 1)) {
127 limit = start;
128 start = 0;
129 pass++;
130 goto again;
131 } else {
132 /* Scanned the whole thing, give up. */
133 return -1;
134 }
135 }
136
137 for (i = n; i < end; i++) {
138 if (test_bit(i, arena->map)) {
139 start = i + 1;
140 goto again;
141 }
142 }
143
144 for (i = n; i < end; i++)
145 __set_bit(i, arena->map);
146
147 arena->hint = end;
148
149 return n;
150}
151
David S. Miller9b3627f2007-04-24 23:51:18 -0700152static void pci_arena_free(struct iommu_arena *arena, unsigned long base, unsigned long npages)
David S. Miller18397942006-02-10 00:08:26 -0800153{
154 unsigned long i;
155
156 for (i = base; i < (base + npages); i++)
157 __clear_bit(i, arena->map);
158}
159
David S. Miller42f14232006-05-23 02:07:22 -0700160static void *pci_4v_alloc_consistent(struct pci_dev *pdev, size_t size, dma_addr_t *dma_addrp, gfp_t gfp)
David S. Miller8f6a93a2006-02-09 21:32:07 -0800161{
David S. Miller16ce82d2007-04-26 21:08:21 -0700162 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800163 unsigned long flags, order, first_page, npages, n;
David S. Miller18397942006-02-10 00:08:26 -0800164 void *ret;
165 long entry;
David S. Miller18397942006-02-10 00:08:26 -0800166
167 size = IO_PAGE_ALIGN(size);
168 order = get_order(size);
David S. Miller6a32fd42006-02-19 22:21:32 -0800169 if (unlikely(order >= MAX_ORDER))
David S. Miller18397942006-02-10 00:08:26 -0800170 return NULL;
171
172 npages = size >> IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800173
David S. Miller42f14232006-05-23 02:07:22 -0700174 first_page = __get_free_pages(gfp, order);
David S. Miller6a32fd42006-02-19 22:21:32 -0800175 if (unlikely(first_page == 0UL))
David S. Miller18397942006-02-10 00:08:26 -0800176 return NULL;
David S. Millere7a04532006-02-15 22:25:27 -0800177
David S. Miller18397942006-02-10 00:08:26 -0800178 memset((char *)first_page, 0, PAGE_SIZE << order);
179
David S. Millera2fb23a2007-02-28 23:35:04 -0800180 iommu = pdev->dev.archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800181
182 spin_lock_irqsave(&iommu->lock, flags);
183 entry = pci_arena_alloc(&iommu->arena, npages);
184 spin_unlock_irqrestore(&iommu->lock, flags);
185
David S. Miller6a32fd42006-02-19 22:21:32 -0800186 if (unlikely(entry < 0L))
187 goto arena_alloc_fail;
David S. Miller18397942006-02-10 00:08:26 -0800188
189 *dma_addrp = (iommu->page_table_map_base +
190 (entry << IO_PAGE_SHIFT));
191 ret = (void *) first_page;
192 first_page = __pa(first_page);
193
David S. Miller6a32fd42006-02-19 22:21:32 -0800194 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800195
David S. Miller6a32fd42006-02-19 22:21:32 -0800196 pci_iommu_batch_start(pdev,
197 (HV_PCI_MAP_ATTR_READ |
198 HV_PCI_MAP_ATTR_WRITE),
199 entry);
David S. Miller18397942006-02-10 00:08:26 -0800200
David S. Miller6a32fd42006-02-19 22:21:32 -0800201 for (n = 0; n < npages; n++) {
202 long err = pci_iommu_batch_add(first_page + (n * PAGE_SIZE));
203 if (unlikely(err < 0L))
204 goto iommu_map_fail;
205 }
David S. Miller18397942006-02-10 00:08:26 -0800206
David S. Miller6a32fd42006-02-19 22:21:32 -0800207 if (unlikely(pci_iommu_batch_end() < 0L))
208 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800209
David S. Miller6a32fd42006-02-19 22:21:32 -0800210 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800211
212 return ret;
David S. Miller6a32fd42006-02-19 22:21:32 -0800213
214iommu_map_fail:
215 /* Interrupts are disabled. */
216 spin_lock(&iommu->lock);
217 pci_arena_free(&iommu->arena, entry, npages);
218 spin_unlock_irqrestore(&iommu->lock, flags);
219
220arena_alloc_fail:
221 free_pages(first_page, order);
222 return NULL;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800223}
224
225static void pci_4v_free_consistent(struct pci_dev *pdev, size_t size, void *cpu, dma_addr_t dvma)
226{
David S. Millera2fb23a2007-02-28 23:35:04 -0800227 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700228 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800229 unsigned long flags, order, npages, entry;
230 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800231
232 npages = IO_PAGE_ALIGN(size) >> IO_PAGE_SHIFT;
David S. Millera2fb23a2007-02-28 23:35:04 -0800233 iommu = pdev->dev.archdata.iommu;
234 pbm = pdev->dev.archdata.host_controller;
235 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800236 entry = ((dvma - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
237
238 spin_lock_irqsave(&iommu->lock, flags);
239
240 pci_arena_free(&iommu->arena, entry, npages);
241
242 do {
243 unsigned long num;
244
245 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
246 npages);
247 entry += num;
248 npages -= num;
249 } while (npages != 0);
250
251 spin_unlock_irqrestore(&iommu->lock, flags);
252
253 order = get_order(size);
254 if (order < 10)
255 free_pages((unsigned long)cpu, order);
David S. Miller8f6a93a2006-02-09 21:32:07 -0800256}
257
258static dma_addr_t pci_4v_map_single(struct pci_dev *pdev, void *ptr, size_t sz, int direction)
259{
David S. Miller16ce82d2007-04-26 21:08:21 -0700260 struct iommu *iommu;
David S. Miller18397942006-02-10 00:08:26 -0800261 unsigned long flags, npages, oaddr;
David S. Miller7c8f4862006-02-13 21:50:27 -0800262 unsigned long i, base_paddr;
David S. Miller6a32fd42006-02-19 22:21:32 -0800263 u32 bus_addr, ret;
David S. Miller18397942006-02-10 00:08:26 -0800264 unsigned long prot;
265 long entry;
David S. Miller18397942006-02-10 00:08:26 -0800266
David S. Millera2fb23a2007-02-28 23:35:04 -0800267 iommu = pdev->dev.archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800268
269 if (unlikely(direction == PCI_DMA_NONE))
270 goto bad;
271
272 oaddr = (unsigned long)ptr;
273 npages = IO_PAGE_ALIGN(oaddr + sz) - (oaddr & IO_PAGE_MASK);
274 npages >>= IO_PAGE_SHIFT;
David S. Miller18397942006-02-10 00:08:26 -0800275
276 spin_lock_irqsave(&iommu->lock, flags);
277 entry = pci_arena_alloc(&iommu->arena, npages);
278 spin_unlock_irqrestore(&iommu->lock, flags);
279
280 if (unlikely(entry < 0L))
281 goto bad;
282
283 bus_addr = (iommu->page_table_map_base +
284 (entry << IO_PAGE_SHIFT));
285 ret = bus_addr | (oaddr & ~IO_PAGE_MASK);
286 base_paddr = __pa(oaddr & IO_PAGE_MASK);
287 prot = HV_PCI_MAP_ATTR_READ;
288 if (direction != PCI_DMA_TODEVICE)
289 prot |= HV_PCI_MAP_ATTR_WRITE;
290
David S. Miller6a32fd42006-02-19 22:21:32 -0800291 local_irq_save(flags);
David S. Miller18397942006-02-10 00:08:26 -0800292
David S. Miller6a32fd42006-02-19 22:21:32 -0800293 pci_iommu_batch_start(pdev, prot, entry);
David S. Miller18397942006-02-10 00:08:26 -0800294
David S. Miller6a32fd42006-02-19 22:21:32 -0800295 for (i = 0; i < npages; i++, base_paddr += IO_PAGE_SIZE) {
296 long err = pci_iommu_batch_add(base_paddr);
297 if (unlikely(err < 0L))
298 goto iommu_map_fail;
299 }
300 if (unlikely(pci_iommu_batch_end() < 0L))
301 goto iommu_map_fail;
David S. Miller18397942006-02-10 00:08:26 -0800302
David S. Miller6a32fd42006-02-19 22:21:32 -0800303 local_irq_restore(flags);
David S. Miller18397942006-02-10 00:08:26 -0800304
305 return ret;
306
307bad:
308 if (printk_ratelimit())
309 WARN_ON(1);
310 return PCI_DMA_ERROR_CODE;
David S. Miller6a32fd42006-02-19 22:21:32 -0800311
312iommu_map_fail:
313 /* Interrupts are disabled. */
314 spin_lock(&iommu->lock);
315 pci_arena_free(&iommu->arena, entry, npages);
316 spin_unlock_irqrestore(&iommu->lock, flags);
317
318 return PCI_DMA_ERROR_CODE;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800319}
320
321static void pci_4v_unmap_single(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
322{
David S. Millera2fb23a2007-02-28 23:35:04 -0800323 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700324 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800325 unsigned long flags, npages;
David S. Miller18397942006-02-10 00:08:26 -0800326 long entry;
David S. Miller7c8f4862006-02-13 21:50:27 -0800327 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800328
329 if (unlikely(direction == PCI_DMA_NONE)) {
330 if (printk_ratelimit())
331 WARN_ON(1);
332 return;
333 }
334
David S. Millera2fb23a2007-02-28 23:35:04 -0800335 iommu = pdev->dev.archdata.iommu;
336 pbm = pdev->dev.archdata.host_controller;
337 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800338
339 npages = IO_PAGE_ALIGN(bus_addr + sz) - (bus_addr & IO_PAGE_MASK);
340 npages >>= IO_PAGE_SHIFT;
341 bus_addr &= IO_PAGE_MASK;
342
343 spin_lock_irqsave(&iommu->lock, flags);
344
345 entry = (bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT;
346 pci_arena_free(&iommu->arena, entry, npages);
347
348 do {
349 unsigned long num;
350
351 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
352 npages);
353 entry += num;
354 npages -= num;
355 } while (npages != 0);
356
357 spin_unlock_irqrestore(&iommu->lock, flags);
358}
359
360#define SG_ENT_PHYS_ADDRESS(SG) \
361 (__pa(page_address((SG)->page)) + (SG)->offset)
362
David S. Miller6a32fd42006-02-19 22:21:32 -0800363static inline long fill_sg(long entry, struct pci_dev *pdev,
David S. Miller18397942006-02-10 00:08:26 -0800364 struct scatterlist *sg,
365 int nused, int nelems, unsigned long prot)
366{
367 struct scatterlist *dma_sg = sg;
368 struct scatterlist *sg_end = sg + nelems;
David S. Miller6a32fd42006-02-19 22:21:32 -0800369 unsigned long flags;
370 int i;
David S. Miller18397942006-02-10 00:08:26 -0800371
David S. Miller6a32fd42006-02-19 22:21:32 -0800372 local_irq_save(flags);
373
374 pci_iommu_batch_start(pdev, prot, entry);
375
David S. Miller18397942006-02-10 00:08:26 -0800376 for (i = 0; i < nused; i++) {
377 unsigned long pteval = ~0UL;
378 u32 dma_npages;
379
380 dma_npages = ((dma_sg->dma_address & (IO_PAGE_SIZE - 1UL)) +
381 dma_sg->dma_length +
382 ((IO_PAGE_SIZE - 1UL))) >> IO_PAGE_SHIFT;
383 do {
384 unsigned long offset;
385 signed int len;
386
387 /* If we are here, we know we have at least one
388 * more page to map. So walk forward until we
389 * hit a page crossing, and begin creating new
390 * mappings from that spot.
391 */
392 for (;;) {
393 unsigned long tmp;
394
395 tmp = SG_ENT_PHYS_ADDRESS(sg);
396 len = sg->length;
397 if (((tmp ^ pteval) >> IO_PAGE_SHIFT) != 0UL) {
398 pteval = tmp & IO_PAGE_MASK;
399 offset = tmp & (IO_PAGE_SIZE - 1UL);
400 break;
401 }
402 if (((tmp ^ (tmp + len - 1UL)) >> IO_PAGE_SHIFT) != 0UL) {
403 pteval = (tmp + IO_PAGE_SIZE) & IO_PAGE_MASK;
404 offset = 0UL;
405 len -= (IO_PAGE_SIZE - (tmp & (IO_PAGE_SIZE - 1UL)));
406 break;
407 }
408 sg++;
409 }
410
411 pteval = (pteval & IOPTE_PAGE);
412 while (len > 0) {
David S. Miller6a32fd42006-02-19 22:21:32 -0800413 long err;
414
415 err = pci_iommu_batch_add(pteval);
416 if (unlikely(err < 0L))
417 goto iommu_map_failed;
418
David S. Miller18397942006-02-10 00:08:26 -0800419 pteval += IO_PAGE_SIZE;
420 len -= (IO_PAGE_SIZE - offset);
421 offset = 0;
422 dma_npages--;
423 }
424
425 pteval = (pteval & IOPTE_PAGE) + len;
426 sg++;
427
428 /* Skip over any tail mappings we've fully mapped,
429 * adjusting pteval along the way. Stop when we
430 * detect a page crossing event.
431 */
432 while (sg < sg_end &&
433 (pteval << (64 - IO_PAGE_SHIFT)) != 0UL &&
434 (pteval == SG_ENT_PHYS_ADDRESS(sg)) &&
435 ((pteval ^
436 (SG_ENT_PHYS_ADDRESS(sg) + sg->length - 1UL)) >> IO_PAGE_SHIFT) == 0UL) {
437 pteval += sg->length;
438 sg++;
439 }
440 if ((pteval << (64 - IO_PAGE_SHIFT)) == 0UL)
441 pteval = ~0UL;
442 } while (dma_npages != 0);
443 dma_sg++;
444 }
445
David S. Miller6a32fd42006-02-19 22:21:32 -0800446 if (unlikely(pci_iommu_batch_end() < 0L))
447 goto iommu_map_failed;
David S. Miller18397942006-02-10 00:08:26 -0800448
David S. Miller6a32fd42006-02-19 22:21:32 -0800449 local_irq_restore(flags);
450 return 0;
David S. Miller18397942006-02-10 00:08:26 -0800451
David S. Miller6a32fd42006-02-19 22:21:32 -0800452iommu_map_failed:
453 local_irq_restore(flags);
454 return -1L;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800455}
456
457static int pci_4v_map_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
458{
David S. Miller16ce82d2007-04-26 21:08:21 -0700459 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800460 unsigned long flags, npages, prot;
David S. Miller6a32fd42006-02-19 22:21:32 -0800461 u32 dma_base;
David S. Miller18397942006-02-10 00:08:26 -0800462 struct scatterlist *sgtmp;
David S. Miller6a32fd42006-02-19 22:21:32 -0800463 long entry, err;
David S. Miller18397942006-02-10 00:08:26 -0800464 int used;
465
466 /* Fast path single entry scatterlists. */
467 if (nelems == 1) {
468 sglist->dma_address =
469 pci_4v_map_single(pdev,
470 (page_address(sglist->page) + sglist->offset),
471 sglist->length, direction);
472 if (unlikely(sglist->dma_address == PCI_DMA_ERROR_CODE))
473 return 0;
474 sglist->dma_length = sglist->length;
475 return 1;
476 }
477
David S. Millera2fb23a2007-02-28 23:35:04 -0800478 iommu = pdev->dev.archdata.iommu;
David S. Miller18397942006-02-10 00:08:26 -0800479
480 if (unlikely(direction == PCI_DMA_NONE))
481 goto bad;
482
483 /* Step 1: Prepare scatter list. */
484 npages = prepare_sg(sglist, nelems);
David S. Miller18397942006-02-10 00:08:26 -0800485
486 /* Step 2: Allocate a cluster and context, if necessary. */
487 spin_lock_irqsave(&iommu->lock, flags);
488 entry = pci_arena_alloc(&iommu->arena, npages);
489 spin_unlock_irqrestore(&iommu->lock, flags);
490
491 if (unlikely(entry < 0L))
492 goto bad;
493
494 dma_base = iommu->page_table_map_base +
495 (entry << IO_PAGE_SHIFT);
496
497 /* Step 3: Normalize DMA addresses. */
498 used = nelems;
499
500 sgtmp = sglist;
501 while (used && sgtmp->dma_length) {
502 sgtmp->dma_address += dma_base;
503 sgtmp++;
504 used--;
505 }
506 used = nelems - used;
507
508 /* Step 4: Create the mappings. */
509 prot = HV_PCI_MAP_ATTR_READ;
510 if (direction != PCI_DMA_TODEVICE)
511 prot |= HV_PCI_MAP_ATTR_WRITE;
512
David S. Miller6a32fd42006-02-19 22:21:32 -0800513 err = fill_sg(entry, pdev, sglist, used, nelems, prot);
514 if (unlikely(err < 0L))
515 goto iommu_map_failed;
David S. Miller18397942006-02-10 00:08:26 -0800516
517 return used;
518
519bad:
520 if (printk_ratelimit())
521 WARN_ON(1);
522 return 0;
David S. Miller6a32fd42006-02-19 22:21:32 -0800523
524iommu_map_failed:
525 spin_lock_irqsave(&iommu->lock, flags);
526 pci_arena_free(&iommu->arena, entry, npages);
527 spin_unlock_irqrestore(&iommu->lock, flags);
528
529 return 0;
David S. Miller8f6a93a2006-02-09 21:32:07 -0800530}
531
532static void pci_4v_unmap_sg(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
533{
David S. Millera2fb23a2007-02-28 23:35:04 -0800534 struct pci_pbm_info *pbm;
David S. Miller16ce82d2007-04-26 21:08:21 -0700535 struct iommu *iommu;
David S. Miller7c8f4862006-02-13 21:50:27 -0800536 unsigned long flags, i, npages;
David S. Miller18397942006-02-10 00:08:26 -0800537 long entry;
David S. Miller7c8f4862006-02-13 21:50:27 -0800538 u32 devhandle, bus_addr;
David S. Miller18397942006-02-10 00:08:26 -0800539
540 if (unlikely(direction == PCI_DMA_NONE)) {
541 if (printk_ratelimit())
542 WARN_ON(1);
543 }
544
David S. Millera2fb23a2007-02-28 23:35:04 -0800545 iommu = pdev->dev.archdata.iommu;
546 pbm = pdev->dev.archdata.host_controller;
547 devhandle = pbm->devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800548
549 bus_addr = sglist->dma_address & IO_PAGE_MASK;
550
551 for (i = 1; i < nelems; i++)
552 if (sglist[i].dma_length == 0)
553 break;
554 i--;
555 npages = (IO_PAGE_ALIGN(sglist[i].dma_address + sglist[i].dma_length) -
556 bus_addr) >> IO_PAGE_SHIFT;
557
558 entry = ((bus_addr - iommu->page_table_map_base) >> IO_PAGE_SHIFT);
559
560 spin_lock_irqsave(&iommu->lock, flags);
561
562 pci_arena_free(&iommu->arena, entry, npages);
563
564 do {
565 unsigned long num;
566
567 num = pci_sun4v_iommu_demap(devhandle, HV_PCI_TSBID(0, entry),
568 npages);
569 entry += num;
570 npages -= num;
571 } while (npages != 0);
572
573 spin_unlock_irqrestore(&iommu->lock, flags);
David S. Miller8f6a93a2006-02-09 21:32:07 -0800574}
575
576static void pci_4v_dma_sync_single_for_cpu(struct pci_dev *pdev, dma_addr_t bus_addr, size_t sz, int direction)
577{
David S. Miller18397942006-02-10 00:08:26 -0800578 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800579}
580
581static void pci_4v_dma_sync_sg_for_cpu(struct pci_dev *pdev, struct scatterlist *sglist, int nelems, int direction)
582{
David S. Miller18397942006-02-10 00:08:26 -0800583 /* Nothing to do... */
David S. Miller8f6a93a2006-02-09 21:32:07 -0800584}
585
David S. Millerc6e87562007-03-09 16:58:43 -0800586const struct pci_iommu_ops pci_sun4v_iommu_ops = {
David S. Miller8f6a93a2006-02-09 21:32:07 -0800587 .alloc_consistent = pci_4v_alloc_consistent,
588 .free_consistent = pci_4v_free_consistent,
589 .map_single = pci_4v_map_single,
590 .unmap_single = pci_4v_unmap_single,
591 .map_sg = pci_4v_map_sg,
592 .unmap_sg = pci_4v_unmap_sg,
593 .dma_sync_single_for_cpu = pci_4v_dma_sync_single_for_cpu,
594 .dma_sync_sg_for_cpu = pci_4v_dma_sync_sg_for_cpu,
595};
596
David S. Miller46b30492006-06-10 01:06:25 -0700597static inline int pci_sun4v_out_of_range(struct pci_pbm_info *pbm, unsigned int bus, unsigned int device, unsigned int func)
598{
David S. Miller059833e2006-02-12 23:49:18 -0800599 if (bus < pbm->pci_first_busno ||
600 bus > pbm->pci_last_busno)
601 return 1;
David S. Millera2fb23a2007-02-28 23:35:04 -0800602 return 0;
David S. Miller059833e2006-02-12 23:49:18 -0800603}
604
David S. Millerbade5622006-02-09 22:05:54 -0800605static int pci_sun4v_read_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
606 int where, int size, u32 *value)
607{
David S. Miller7eae6422006-02-09 22:20:01 -0800608 struct pci_pbm_info *pbm = bus_dev->sysdata;
David S. Miller059833e2006-02-12 23:49:18 -0800609 u32 devhandle = pbm->devhandle;
David S. Miller7eae6422006-02-09 22:20:01 -0800610 unsigned int bus = bus_dev->number;
611 unsigned int device = PCI_SLOT(devfn);
612 unsigned int func = PCI_FUNC(devfn);
613 unsigned long ret;
614
David S. Miller97b3cf02007-03-11 16:42:53 -0700615 if (bus_dev == pbm->pci_bus && devfn == 0x00)
616 return pci_host_bridge_read_pci_cfg(bus_dev, devfn, where,
617 size, value);
David S. Miller987b6de2006-02-14 16:42:11 -0800618 if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
David S. Miller059833e2006-02-12 23:49:18 -0800619 ret = ~0UL;
620 } else {
621 ret = pci_sun4v_config_get(devhandle,
622 HV_PCI_DEVICE_BUILD(bus, device, func),
623 where, size);
David S. Miller10804822006-02-13 18:09:44 -0800624#if 0
David S. Miller987b6de2006-02-14 16:42:11 -0800625 printk("rcfg: [%x:%x:%x:%d]=[%lx]\n",
David S. Miller10804822006-02-13 18:09:44 -0800626 devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
627 where, size, ret);
628#endif
David S. Miller059833e2006-02-12 23:49:18 -0800629 }
David S. Miller7eae6422006-02-09 22:20:01 -0800630 switch (size) {
631 case 1:
632 *value = ret & 0xff;
633 break;
634 case 2:
635 *value = ret & 0xffff;
636 break;
637 case 4:
638 *value = ret & 0xffffffff;
639 break;
640 };
641
642
643 return PCIBIOS_SUCCESSFUL;
David S. Millerbade5622006-02-09 22:05:54 -0800644}
645
646static int pci_sun4v_write_pci_cfg(struct pci_bus *bus_dev, unsigned int devfn,
647 int where, int size, u32 value)
648{
David S. Miller7eae6422006-02-09 22:20:01 -0800649 struct pci_pbm_info *pbm = bus_dev->sysdata;
David S. Miller059833e2006-02-12 23:49:18 -0800650 u32 devhandle = pbm->devhandle;
David S. Miller7eae6422006-02-09 22:20:01 -0800651 unsigned int bus = bus_dev->number;
652 unsigned int device = PCI_SLOT(devfn);
653 unsigned int func = PCI_FUNC(devfn);
654 unsigned long ret;
655
David S. Miller97b3cf02007-03-11 16:42:53 -0700656 if (bus_dev == pbm->pci_bus && devfn == 0x00)
657 return pci_host_bridge_write_pci_cfg(bus_dev, devfn, where,
658 size, value);
David S. Miller987b6de2006-02-14 16:42:11 -0800659 if (pci_sun4v_out_of_range(pbm, bus, device, func)) {
David S. Miller059833e2006-02-12 23:49:18 -0800660 /* Do nothing. */
661 } else {
662 ret = pci_sun4v_config_put(devhandle,
663 HV_PCI_DEVICE_BUILD(bus, device, func),
664 where, size, value);
David S. Miller10804822006-02-13 18:09:44 -0800665#if 0
David S. Miller987b6de2006-02-14 16:42:11 -0800666 printk("wcfg: [%x:%x:%x:%d] v[%x] == [%lx]\n",
David S. Miller10804822006-02-13 18:09:44 -0800667 devhandle, HV_PCI_DEVICE_BUILD(bus, device, func),
668 where, size, value, ret);
669#endif
David S. Miller059833e2006-02-12 23:49:18 -0800670 }
David S. Miller7eae6422006-02-09 22:20:01 -0800671 return PCIBIOS_SUCCESSFUL;
David S. Millerbade5622006-02-09 22:05:54 -0800672}
673
674static struct pci_ops pci_sun4v_ops = {
675 .read = pci_sun4v_read_pci_cfg,
676 .write = pci_sun4v_write_pci_cfg,
677};
678
679
David S. Millerc2609262006-02-12 22:18:52 -0800680static void pbm_scan_bus(struct pci_controller_info *p,
681 struct pci_pbm_info *pbm)
682{
David S. Millera2fb23a2007-02-28 23:35:04 -0800683 pbm->pci_bus = pci_scan_one_pbm(pbm);
David S. Millerc2609262006-02-12 22:18:52 -0800684}
685
David S. Millerbade5622006-02-09 22:05:54 -0800686static void pci_sun4v_scan_bus(struct pci_controller_info *p)
687{
David S. Millere87dc352006-06-21 18:18:47 -0700688 struct property *prop;
689 struct device_node *dp;
690
691 if ((dp = p->pbm_A.prom_node) != NULL) {
692 prop = of_find_property(dp, "66mhz-capable", NULL);
693 p->pbm_A.is_66mhz_capable = (prop != NULL);
David S. Millerc2609262006-02-12 22:18:52 -0800694
695 pbm_scan_bus(p, &p->pbm_A);
696 }
David S. Millere87dc352006-06-21 18:18:47 -0700697 if ((dp = p->pbm_B.prom_node) != NULL) {
698 prop = of_find_property(dp, "66mhz-capable", NULL);
699 p->pbm_B.is_66mhz_capable = (prop != NULL);
David S. Millerc2609262006-02-12 22:18:52 -0800700
701 pbm_scan_bus(p, &p->pbm_B);
702 }
703
704 /* XXX register error interrupt handlers XXX */
David S. Millerbade5622006-02-09 22:05:54 -0800705}
706
David S. Millere7a04532006-02-15 22:25:27 -0800707static unsigned long probe_existing_entries(struct pci_pbm_info *pbm,
David S. Miller16ce82d2007-04-26 21:08:21 -0700708 struct iommu *iommu)
David S. Miller18397942006-02-10 00:08:26 -0800709{
David S. Miller9b3627f2007-04-24 23:51:18 -0700710 struct iommu_arena *arena = &iommu->arena;
David S. Millere7a04532006-02-15 22:25:27 -0800711 unsigned long i, cnt = 0;
David S. Miller7c8f4862006-02-13 21:50:27 -0800712 u32 devhandle;
David S. Miller18397942006-02-10 00:08:26 -0800713
714 devhandle = pbm->devhandle;
715 for (i = 0; i < arena->limit; i++) {
716 unsigned long ret, io_attrs, ra;
717
718 ret = pci_sun4v_iommu_getmap(devhandle,
719 HV_PCI_TSBID(0, i),
720 &io_attrs, &ra);
David S. Millere7a04532006-02-15 22:25:27 -0800721 if (ret == HV_EOK) {
David S. Millerc2a5a462006-06-22 00:01:56 -0700722 if (page_in_phys_avail(ra)) {
723 pci_sun4v_iommu_demap(devhandle,
724 HV_PCI_TSBID(0, i), 1);
725 } else {
726 cnt++;
727 __set_bit(i, arena->map);
728 }
David S. Millere7a04532006-02-15 22:25:27 -0800729 }
David S. Miller18397942006-02-10 00:08:26 -0800730 }
David S. Millere7a04532006-02-15 22:25:27 -0800731
732 return cnt;
David S. Miller18397942006-02-10 00:08:26 -0800733}
734
David S. Millerbade5622006-02-09 22:05:54 -0800735static void pci_sun4v_iommu_init(struct pci_pbm_info *pbm)
736{
David S. Miller16ce82d2007-04-26 21:08:21 -0700737 struct iommu *iommu = pbm->iommu;
David S. Millere87dc352006-06-21 18:18:47 -0700738 struct property *prop;
David S. Miller18397942006-02-10 00:08:26 -0800739 unsigned long num_tsb_entries, sz;
740 u32 vdma[2], dma_mask, dma_offset;
David S. Millere87dc352006-06-21 18:18:47 -0700741 int tsbsize;
David S. Miller18397942006-02-10 00:08:26 -0800742
David S. Millere87dc352006-06-21 18:18:47 -0700743 prop = of_find_property(pbm->prom_node, "virtual-dma", NULL);
744 if (prop) {
745 u32 *val = prop->value;
746
747 vdma[0] = val[0];
748 vdma[1] = val[1];
749 } else {
David S. Miller18397942006-02-10 00:08:26 -0800750 /* No property, use default values. */
751 vdma[0] = 0x80000000;
752 vdma[1] = 0x80000000;
753 }
754
755 dma_mask = vdma[0];
756 switch (vdma[1]) {
757 case 0x20000000:
758 dma_mask |= 0x1fffffff;
759 tsbsize = 64;
760 break;
761
762 case 0x40000000:
763 dma_mask |= 0x3fffffff;
764 tsbsize = 128;
765 break;
766
767 case 0x80000000:
768 dma_mask |= 0x7fffffff;
David S. Millere7a04532006-02-15 22:25:27 -0800769 tsbsize = 256;
David S. Miller18397942006-02-10 00:08:26 -0800770 break;
771
772 default:
773 prom_printf("PCI-SUN4V: strange virtual-dma size.\n");
774 prom_halt();
775 };
776
David S. Millere7a04532006-02-15 22:25:27 -0800777 tsbsize *= (8 * 1024);
778
David S. Miller18397942006-02-10 00:08:26 -0800779 num_tsb_entries = tsbsize / sizeof(iopte_t);
780
781 dma_offset = vdma[0];
782
783 /* Setup initial software IOMMU state. */
784 spin_lock_init(&iommu->lock);
785 iommu->ctx_lowest_free = 1;
786 iommu->page_table_map_base = dma_offset;
787 iommu->dma_addr_mask = dma_mask;
788
789 /* Allocate and initialize the free area map. */
790 sz = num_tsb_entries / 8;
791 sz = (sz + 7UL) & ~7UL;
Yan Burman982c2062006-11-30 17:13:09 -0800792 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
David S. Miller18397942006-02-10 00:08:26 -0800793 if (!iommu->arena.map) {
794 prom_printf("PCI_IOMMU: Error, kmalloc(arena.map) failed.\n");
795 prom_halt();
796 }
David S. Miller18397942006-02-10 00:08:26 -0800797 iommu->arena.limit = num_tsb_entries;
798
David S. Millere7a04532006-02-15 22:25:27 -0800799 sz = probe_existing_entries(pbm, iommu);
David S. Millerc2a5a462006-06-22 00:01:56 -0700800 if (sz)
801 printk("%s: Imported %lu TSB entries from OBP\n",
802 pbm->name, sz);
David S. Millerbade5622006-02-09 22:05:54 -0800803}
804
David S. Miller35a17eb2007-02-10 17:41:02 -0800805#ifdef CONFIG_PCI_MSI
806struct pci_sun4v_msiq_entry {
807 u64 version_type;
808#define MSIQ_VERSION_MASK 0xffffffff00000000UL
809#define MSIQ_VERSION_SHIFT 32
810#define MSIQ_TYPE_MASK 0x00000000000000ffUL
811#define MSIQ_TYPE_SHIFT 0
812#define MSIQ_TYPE_NONE 0x00
813#define MSIQ_TYPE_MSG 0x01
814#define MSIQ_TYPE_MSI32 0x02
815#define MSIQ_TYPE_MSI64 0x03
816#define MSIQ_TYPE_INTX 0x08
817#define MSIQ_TYPE_NONE2 0xff
818
819 u64 intx_sysino;
820 u64 reserved1;
821 u64 stick;
822 u64 req_id; /* bus/device/func */
823#define MSIQ_REQID_BUS_MASK 0xff00UL
824#define MSIQ_REQID_BUS_SHIFT 8
825#define MSIQ_REQID_DEVICE_MASK 0x00f8UL
826#define MSIQ_REQID_DEVICE_SHIFT 3
827#define MSIQ_REQID_FUNC_MASK 0x0007UL
828#define MSIQ_REQID_FUNC_SHIFT 0
829
830 u64 msi_address;
831
832 /* The format of this value is message type dependant.
833 * For MSI bits 15:0 are the data from the MSI packet.
834 * For MSI-X bits 31:0 are the data from the MSI packet.
835 * For MSG, the message code and message routing code where:
836 * bits 39:32 is the bus/device/fn of the msg target-id
837 * bits 18:16 is the message routing code
838 * bits 7:0 is the message code
839 * For INTx the low order 2-bits are:
840 * 00 - INTA
841 * 01 - INTB
842 * 10 - INTC
843 * 11 - INTD
844 */
845 u64 msi_data;
846
847 u64 reserved2;
848};
849
850/* For now this just runs as a pre-handler for the real interrupt handler.
851 * So we just walk through the queue and ACK all the entries, update the
852 * head pointer, and return.
853 *
854 * In the longer term it would be nice to do something more integrated
855 * wherein we can pass in some of this MSI info to the drivers. This
856 * would be most useful for PCIe fabric error messages, although we could
857 * invoke those directly from the loop here in order to pass the info around.
858 */
859static void pci_sun4v_msi_prehandler(unsigned int ino, void *data1, void *data2)
860{
861 struct pci_pbm_info *pbm = data1;
862 struct pci_sun4v_msiq_entry *base, *ep;
863 unsigned long msiqid, orig_head, head, type, err;
864
865 msiqid = (unsigned long) data2;
866
867 head = 0xdeadbeef;
868 err = pci_sun4v_msiq_gethead(pbm->devhandle, msiqid, &head);
869 if (unlikely(err))
870 goto hv_error_get;
871
872 if (unlikely(head >= (pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry))))
873 goto bad_offset;
874
875 head /= sizeof(struct pci_sun4v_msiq_entry);
876 orig_head = head;
877 base = (pbm->msi_queues + ((msiqid - pbm->msiq_first) *
878 (pbm->msiq_ent_count *
879 sizeof(struct pci_sun4v_msiq_entry))));
880 ep = &base[head];
881 while ((ep->version_type & MSIQ_TYPE_MASK) != 0) {
882 type = (ep->version_type & MSIQ_TYPE_MASK) >> MSIQ_TYPE_SHIFT;
883 if (unlikely(type != MSIQ_TYPE_MSI32 &&
884 type != MSIQ_TYPE_MSI64))
885 goto bad_type;
886
887 pci_sun4v_msi_setstate(pbm->devhandle,
888 ep->msi_data /* msi_num */,
889 HV_MSISTATE_IDLE);
890
891 /* Clear the entry. */
892 ep->version_type &= ~MSIQ_TYPE_MASK;
893
894 /* Go to next entry in ring. */
895 head++;
896 if (head >= pbm->msiq_ent_count)
897 head = 0;
898 ep = &base[head];
899 }
900
901 if (likely(head != orig_head)) {
902 /* ACK entries by updating head pointer. */
903 head *= sizeof(struct pci_sun4v_msiq_entry);
904 err = pci_sun4v_msiq_sethead(pbm->devhandle, msiqid, head);
905 if (unlikely(err))
906 goto hv_error_set;
907 }
908 return;
909
910hv_error_set:
911 printk(KERN_EMERG "MSI: Hypervisor set head gives error %lu\n", err);
912 goto hv_error_cont;
913
914hv_error_get:
915 printk(KERN_EMERG "MSI: Hypervisor get head gives error %lu\n", err);
916
917hv_error_cont:
918 printk(KERN_EMERG "MSI: devhandle[%x] msiqid[%lx] head[%lu]\n",
919 pbm->devhandle, msiqid, head);
920 return;
921
922bad_offset:
923 printk(KERN_EMERG "MSI: Hypervisor gives bad offset %lx max(%lx)\n",
924 head, pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry));
925 return;
926
927bad_type:
928 printk(KERN_EMERG "MSI: Entry has bad type %lx\n", type);
929 return;
930}
931
932static int msi_bitmap_alloc(struct pci_pbm_info *pbm)
933{
934 unsigned long size, bits_per_ulong;
935
936 bits_per_ulong = sizeof(unsigned long) * 8;
937 size = (pbm->msi_num + (bits_per_ulong - 1)) & ~(bits_per_ulong - 1);
938 size /= 8;
939 BUG_ON(size % sizeof(unsigned long));
940
941 pbm->msi_bitmap = kzalloc(size, GFP_KERNEL);
942 if (!pbm->msi_bitmap)
943 return -ENOMEM;
944
945 return 0;
946}
947
948static void msi_bitmap_free(struct pci_pbm_info *pbm)
949{
950 kfree(pbm->msi_bitmap);
951 pbm->msi_bitmap = NULL;
952}
953
954static int msi_queue_alloc(struct pci_pbm_info *pbm)
955{
956 unsigned long q_size, alloc_size, pages, order;
957 int i;
958
959 q_size = pbm->msiq_ent_count * sizeof(struct pci_sun4v_msiq_entry);
960 alloc_size = (pbm->msiq_num * q_size);
961 order = get_order(alloc_size);
962 pages = __get_free_pages(GFP_KERNEL | __GFP_COMP, order);
963 if (pages == 0UL) {
964 printk(KERN_ERR "MSI: Cannot allocate MSI queues (o=%lu).\n",
965 order);
966 return -ENOMEM;
967 }
968 memset((char *)pages, 0, PAGE_SIZE << order);
969 pbm->msi_queues = (void *) pages;
970
971 for (i = 0; i < pbm->msiq_num; i++) {
972 unsigned long err, base = __pa(pages + (i * q_size));
973 unsigned long ret1, ret2;
974
975 err = pci_sun4v_msiq_conf(pbm->devhandle,
976 pbm->msiq_first + i,
977 base, pbm->msiq_ent_count);
978 if (err) {
979 printk(KERN_ERR "MSI: msiq register fails (err=%lu)\n",
980 err);
981 goto h_error;
982 }
983
984 err = pci_sun4v_msiq_info(pbm->devhandle,
985 pbm->msiq_first + i,
986 &ret1, &ret2);
987 if (err) {
988 printk(KERN_ERR "MSI: Cannot read msiq (err=%lu)\n",
989 err);
990 goto h_error;
991 }
992 if (ret1 != base || ret2 != pbm->msiq_ent_count) {
993 printk(KERN_ERR "MSI: Bogus qconf "
994 "expected[%lx:%x] got[%lx:%lx]\n",
995 base, pbm->msiq_ent_count,
996 ret1, ret2);
997 goto h_error;
998 }
999 }
1000
1001 return 0;
1002
1003h_error:
1004 free_pages(pages, order);
1005 return -EINVAL;
1006}
1007
1008static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
1009{
Stephen Rothwell6a23acf2007-04-23 15:53:27 -07001010 const u32 *val;
David S. Miller35a17eb2007-02-10 17:41:02 -08001011 int len;
1012
1013 val = of_get_property(pbm->prom_node, "#msi-eqs", &len);
1014 if (!val || len != 4)
1015 goto no_msi;
1016 pbm->msiq_num = *val;
1017 if (pbm->msiq_num) {
Stephen Rothwell6a23acf2007-04-23 15:53:27 -07001018 const struct msiq_prop {
David S. Miller35a17eb2007-02-10 17:41:02 -08001019 u32 first_msiq;
1020 u32 num_msiq;
1021 u32 first_devino;
1022 } *mqp;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -07001023 const struct msi_range_prop {
David S. Miller35a17eb2007-02-10 17:41:02 -08001024 u32 first_msi;
1025 u32 num_msi;
1026 } *mrng;
Stephen Rothwell6a23acf2007-04-23 15:53:27 -07001027 const struct addr_range_prop {
David S. Miller35a17eb2007-02-10 17:41:02 -08001028 u32 msi32_high;
1029 u32 msi32_low;
1030 u32 msi32_len;
1031 u32 msi64_high;
1032 u32 msi64_low;
1033 u32 msi64_len;
1034 } *arng;
1035
1036 val = of_get_property(pbm->prom_node, "msi-eq-size", &len);
1037 if (!val || len != 4)
1038 goto no_msi;
1039
1040 pbm->msiq_ent_count = *val;
1041
1042 mqp = of_get_property(pbm->prom_node,
1043 "msi-eq-to-devino", &len);
1044 if (!mqp || len != sizeof(struct msiq_prop))
1045 goto no_msi;
1046
1047 pbm->msiq_first = mqp->first_msiq;
1048 pbm->msiq_first_devino = mqp->first_devino;
1049
1050 val = of_get_property(pbm->prom_node, "#msi", &len);
1051 if (!val || len != 4)
1052 goto no_msi;
1053 pbm->msi_num = *val;
1054
1055 mrng = of_get_property(pbm->prom_node, "msi-ranges", &len);
1056 if (!mrng || len != sizeof(struct msi_range_prop))
1057 goto no_msi;
1058 pbm->msi_first = mrng->first_msi;
1059
1060 val = of_get_property(pbm->prom_node, "msi-data-mask", &len);
1061 if (!val || len != 4)
1062 goto no_msi;
1063 pbm->msi_data_mask = *val;
1064
1065 val = of_get_property(pbm->prom_node, "msix-data-width", &len);
1066 if (!val || len != 4)
1067 goto no_msi;
1068 pbm->msix_data_width = *val;
1069
1070 arng = of_get_property(pbm->prom_node, "msi-address-ranges",
1071 &len);
1072 if (!arng || len != sizeof(struct addr_range_prop))
1073 goto no_msi;
1074 pbm->msi32_start = ((u64)arng->msi32_high << 32) |
1075 (u64) arng->msi32_low;
1076 pbm->msi64_start = ((u64)arng->msi64_high << 32) |
1077 (u64) arng->msi64_low;
1078 pbm->msi32_len = arng->msi32_len;
1079 pbm->msi64_len = arng->msi64_len;
1080
1081 if (msi_bitmap_alloc(pbm))
1082 goto no_msi;
1083
1084 if (msi_queue_alloc(pbm)) {
1085 msi_bitmap_free(pbm);
1086 goto no_msi;
1087 }
1088
1089 printk(KERN_INFO "%s: MSI Queue first[%u] num[%u] count[%u] "
1090 "devino[0x%x]\n",
1091 pbm->name,
1092 pbm->msiq_first, pbm->msiq_num,
1093 pbm->msiq_ent_count,
1094 pbm->msiq_first_devino);
1095 printk(KERN_INFO "%s: MSI first[%u] num[%u] mask[0x%x] "
1096 "width[%u]\n",
1097 pbm->name,
1098 pbm->msi_first, pbm->msi_num, pbm->msi_data_mask,
1099 pbm->msix_data_width);
1100 printk(KERN_INFO "%s: MSI addr32[0x%lx:0x%x] "
1101 "addr64[0x%lx:0x%x]\n",
1102 pbm->name,
1103 pbm->msi32_start, pbm->msi32_len,
1104 pbm->msi64_start, pbm->msi64_len);
1105 printk(KERN_INFO "%s: MSI queues at RA [%p]\n",
1106 pbm->name,
1107 pbm->msi_queues);
1108 }
1109
1110 return;
1111
1112no_msi:
1113 pbm->msiq_num = 0;
1114 printk(KERN_INFO "%s: No MSI support.\n", pbm->name);
1115}
1116
1117static int alloc_msi(struct pci_pbm_info *pbm)
1118{
1119 int i;
1120
1121 for (i = 0; i < pbm->msi_num; i++) {
1122 if (!test_and_set_bit(i, pbm->msi_bitmap))
1123 return i + pbm->msi_first;
1124 }
1125
1126 return -ENOENT;
1127}
1128
1129static void free_msi(struct pci_pbm_info *pbm, int msi_num)
1130{
1131 msi_num -= pbm->msi_first;
1132 clear_bit(msi_num, pbm->msi_bitmap);
1133}
1134
1135static int pci_sun4v_setup_msi_irq(unsigned int *virt_irq_p,
1136 struct pci_dev *pdev,
1137 struct msi_desc *entry)
1138{
David S. Millera2fb23a2007-02-28 23:35:04 -08001139 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Miller35a17eb2007-02-10 17:41:02 -08001140 unsigned long devino, msiqid;
1141 struct msi_msg msg;
1142 int msi_num, err;
1143
1144 *virt_irq_p = 0;
1145
1146 msi_num = alloc_msi(pbm);
1147 if (msi_num < 0)
1148 return msi_num;
1149
1150 devino = sun4v_build_msi(pbm->devhandle, virt_irq_p,
1151 pbm->msiq_first_devino,
1152 (pbm->msiq_first_devino +
1153 pbm->msiq_num));
1154 err = -ENOMEM;
1155 if (!devino)
1156 goto out_err;
1157
David S. Miller35a17eb2007-02-10 17:41:02 -08001158 msiqid = ((devino - pbm->msiq_first_devino) +
1159 pbm->msiq_first);
1160
1161 err = -EINVAL;
1162 if (pci_sun4v_msiq_setstate(pbm->devhandle, msiqid, HV_MSIQSTATE_IDLE))
1163 if (err)
1164 goto out_err;
1165
1166 if (pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_VALID))
1167 goto out_err;
1168
1169 if (pci_sun4v_msi_setmsiq(pbm->devhandle,
1170 msi_num, msiqid,
1171 (entry->msi_attrib.is_64 ?
1172 HV_MSITYPE_MSI64 : HV_MSITYPE_MSI32)))
1173 goto out_err;
1174
1175 if (pci_sun4v_msi_setstate(pbm->devhandle, msi_num, HV_MSISTATE_IDLE))
1176 goto out_err;
1177
1178 if (pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_VALID))
1179 goto out_err;
1180
David S. Millera2fb23a2007-02-28 23:35:04 -08001181 pdev->dev.archdata.msi_num = msi_num;
David S. Miller35a17eb2007-02-10 17:41:02 -08001182
1183 if (entry->msi_attrib.is_64) {
1184 msg.address_hi = pbm->msi64_start >> 32;
1185 msg.address_lo = pbm->msi64_start & 0xffffffff;
1186 } else {
1187 msg.address_hi = 0;
1188 msg.address_lo = pbm->msi32_start;
1189 }
1190 msg.data = msi_num;
Michael Ellerman7fe37302007-04-18 19:39:21 +10001191
1192 set_irq_msi(*virt_irq_p, entry);
David S. Miller35a17eb2007-02-10 17:41:02 -08001193 write_msi_msg(*virt_irq_p, &msg);
1194
1195 irq_install_pre_handler(*virt_irq_p,
1196 pci_sun4v_msi_prehandler,
1197 pbm, (void *) msiqid);
1198
1199 return 0;
1200
1201out_err:
1202 free_msi(pbm, msi_num);
1203 sun4v_destroy_msi(*virt_irq_p);
1204 *virt_irq_p = 0;
1205 return err;
1206
1207}
1208
1209static void pci_sun4v_teardown_msi_irq(unsigned int virt_irq,
1210 struct pci_dev *pdev)
1211{
David S. Millera2fb23a2007-02-28 23:35:04 -08001212 struct pci_pbm_info *pbm = pdev->dev.archdata.host_controller;
David S. Miller35a17eb2007-02-10 17:41:02 -08001213 unsigned long msiqid, err;
1214 unsigned int msi_num;
1215
David S. Millera2fb23a2007-02-28 23:35:04 -08001216 msi_num = pdev->dev.archdata.msi_num;
David S. Miller35a17eb2007-02-10 17:41:02 -08001217 err = pci_sun4v_msi_getmsiq(pbm->devhandle, msi_num, &msiqid);
1218 if (err) {
1219 printk(KERN_ERR "%s: getmsiq gives error %lu\n",
1220 pbm->name, err);
1221 return;
1222 }
1223
1224 pci_sun4v_msi_setvalid(pbm->devhandle, msi_num, HV_MSIVALID_INVALID);
1225 pci_sun4v_msiq_setvalid(pbm->devhandle, msiqid, HV_MSIQ_INVALID);
1226
1227 free_msi(pbm, msi_num);
1228
1229 /* The sun4v_destroy_msi() will liberate the devino and thus the MSIQ
1230 * allocation.
1231 */
1232 sun4v_destroy_msi(virt_irq);
1233}
1234#else /* CONFIG_PCI_MSI */
1235static void pci_sun4v_msi_init(struct pci_pbm_info *pbm)
1236{
1237}
1238#endif /* !(CONFIG_PCI_MSI) */
1239
David S. Millere87dc352006-06-21 18:18:47 -07001240static void pci_sun4v_pbm_init(struct pci_controller_info *p, struct device_node *dp, u32 devhandle)
David S. Millerbade5622006-02-09 22:05:54 -08001241{
1242 struct pci_pbm_info *pbm;
David S. Millerbade5622006-02-09 22:05:54 -08001243
David S. Miller38337892006-02-12 22:06:53 -08001244 if (devhandle & 0x40)
1245 pbm = &p->pbm_B;
1246 else
1247 pbm = &p->pbm_A;
David S. Millerbade5622006-02-09 22:05:54 -08001248
1249 pbm->parent = p;
David S. Millere87dc352006-06-21 18:18:47 -07001250 pbm->prom_node = dp;
David S. Millerbade5622006-02-09 22:05:54 -08001251
David S. Miller38337892006-02-12 22:06:53 -08001252 pbm->devhandle = devhandle;
David S. Millerbade5622006-02-09 22:05:54 -08001253
David S. Millere87dc352006-06-21 18:18:47 -07001254 pbm->name = dp->full_name;
David S. Millerbade5622006-02-09 22:05:54 -08001255
David S. Millere87dc352006-06-21 18:18:47 -07001256 printk("%s: SUN4V PCI Bus Module\n", pbm->name);
David S. Millerbade5622006-02-09 22:05:54 -08001257
David S. Miller9fd8b642007-03-08 21:55:49 -08001258 pci_determine_mem_io_space(pbm);
David S. Millerbade5622006-02-09 22:05:54 -08001259
David S. Millercfa06522007-05-07 21:51:41 -07001260 pci_get_pbm_props(pbm);
David S. Millerbade5622006-02-09 22:05:54 -08001261 pci_sun4v_iommu_init(pbm);
David S. Miller35a17eb2007-02-10 17:41:02 -08001262 pci_sun4v_msi_init(pbm);
David S. Millerbade5622006-02-09 22:05:54 -08001263}
1264
David S. Millere87dc352006-06-21 18:18:47 -07001265void sun4v_pci_init(struct device_node *dp, char *model_name)
David S. Miller8f6a93a2006-02-09 21:32:07 -08001266{
David S. Millerbade5622006-02-09 22:05:54 -08001267 struct pci_controller_info *p;
David S. Miller16ce82d2007-04-26 21:08:21 -07001268 struct iommu *iommu;
David S. Millere87dc352006-06-21 18:18:47 -07001269 struct property *prop;
1270 struct linux_prom64_registers *regs;
David S. Miller7c8f4862006-02-13 21:50:27 -08001271 u32 devhandle;
1272 int i;
David S. Miller38337892006-02-12 22:06:53 -08001273
David S. Millere87dc352006-06-21 18:18:47 -07001274 prop = of_find_property(dp, "reg", NULL);
1275 regs = prop->value;
1276
1277 devhandle = (regs->phys_addr >> 32UL) & 0x0fffffff;
David S. Miller38337892006-02-12 22:06:53 -08001278
1279 for (p = pci_controller_root; p; p = p->next) {
1280 struct pci_pbm_info *pbm;
1281
1282 if (p->pbm_A.prom_node && p->pbm_B.prom_node)
1283 continue;
1284
1285 pbm = (p->pbm_A.prom_node ?
1286 &p->pbm_A :
1287 &p->pbm_B);
1288
David S. Miller0b522492006-02-12 22:29:36 -08001289 if (pbm->devhandle == (devhandle ^ 0x40)) {
David S. Millere87dc352006-06-21 18:18:47 -07001290 pci_sun4v_pbm_init(p, dp, devhandle);
David S. Miller0b522492006-02-12 22:29:36 -08001291 return;
1292 }
David S. Miller38337892006-02-12 22:06:53 -08001293 }
David S. Millerbade5622006-02-09 22:05:54 -08001294
KAMEZAWA Hiroyukia283a522006-04-10 22:52:52 -07001295 for_each_possible_cpu(i) {
David S. Miller7c8f4862006-02-13 21:50:27 -08001296 unsigned long page = get_zeroed_page(GFP_ATOMIC);
1297
1298 if (!page)
1299 goto fatal_memory_error;
1300
David S. Miller6a32fd42006-02-19 22:21:32 -08001301 per_cpu(pci_iommu_batch, i).pglist = (u64 *) page;
David S. Millerbade5622006-02-09 22:05:54 -08001302 }
David S. Miller7c8f4862006-02-13 21:50:27 -08001303
Yan Burman982c2062006-11-30 17:13:09 -08001304 p = kzalloc(sizeof(struct pci_controller_info), GFP_ATOMIC);
David S. Miller7c8f4862006-02-13 21:50:27 -08001305 if (!p)
1306 goto fatal_memory_error;
1307
David S. Miller16ce82d2007-04-26 21:08:21 -07001308 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
David S. Miller7c8f4862006-02-13 21:50:27 -08001309 if (!iommu)
1310 goto fatal_memory_error;
1311
David S. Millerbade5622006-02-09 22:05:54 -08001312 p->pbm_A.iommu = iommu;
1313
David S. Miller16ce82d2007-04-26 21:08:21 -07001314 iommu = kzalloc(sizeof(struct iommu), GFP_ATOMIC);
David S. Miller7c8f4862006-02-13 21:50:27 -08001315 if (!iommu)
1316 goto fatal_memory_error;
1317
David S. Millerbade5622006-02-09 22:05:54 -08001318 p->pbm_B.iommu = iommu;
1319
1320 p->next = pci_controller_root;
1321 pci_controller_root = p;
1322
1323 p->index = pci_num_controllers++;
David S. Millerbade5622006-02-09 22:05:54 -08001324
1325 p->scan_bus = pci_sun4v_scan_bus;
David S. Miller35a17eb2007-02-10 17:41:02 -08001326#ifdef CONFIG_PCI_MSI
1327 p->setup_msi_irq = pci_sun4v_setup_msi_irq;
1328 p->teardown_msi_irq = pci_sun4v_teardown_msi_irq;
1329#endif
David S. Millerbade5622006-02-09 22:05:54 -08001330 p->pci_ops = &pci_sun4v_ops;
1331
1332 /* Like PSYCHO and SCHIZO we have a 2GB aligned area
1333 * for memory space.
1334 */
1335 pci_memspace_mask = 0x7fffffffUL;
1336
David S. Millere87dc352006-06-21 18:18:47 -07001337 pci_sun4v_pbm_init(p, dp, devhandle);
David S. Miller7c8f4862006-02-13 21:50:27 -08001338 return;
1339
1340fatal_memory_error:
1341 prom_printf("SUN4V_PCI: Fatal memory allocation error.\n");
1342 prom_halt();
David S. Miller8f6a93a2006-02-09 21:32:07 -08001343}