1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/init.h>
28#include <linux/types.h>
29#include <linux/slab.h>
30#include <linux/mm.h>
31#include <linux/memblock.h>
32#include <linux/spinlock.h>
33#include <linux/sched.h>
34#include <linux/string.h>
35#include <linux/pci.h>
36#include <linux/dma-mapping.h>
37#include <linux/crash_dump.h>
38#include <linux/memory.h>
39#include <linux/of.h>
40#include <linux/iommu.h>
41#include <linux/rculist.h>
42#include <asm/io.h>
43#include <asm/prom.h>
44#include <asm/rtas.h>
45#include <asm/iommu.h>
46#include <asm/pci-bridge.h>
47#include <asm/machdep.h>
48#include <asm/firmware.h>
49#include <asm/tce.h>
50#include <asm/ppc-pci.h>
51#include <asm/udbg.h>
52#include <asm/mmzone.h>
53#include <asm/plpar_wrappers.h>
54
55#include "pseries.h"
56
57static struct iommu_table_group *iommu_pseries_alloc_group(int node)
58{
59 struct iommu_table_group *table_group = NULL;
60 struct iommu_table *tbl = NULL;
61 struct iommu_table_group_link *tgl = NULL;
62
63 table_group = kzalloc_node(sizeof(struct iommu_table_group), GFP_KERNEL,
64 node);
65 if (!table_group)
66 goto fail_exit;
67
68 tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, node);
69 if (!tbl)
70 goto fail_exit;
71
72 tgl = kzalloc_node(sizeof(struct iommu_table_group_link), GFP_KERNEL,
73 node);
74 if (!tgl)
75 goto fail_exit;
76
77 INIT_LIST_HEAD_RCU(&tbl->it_group_list);
78 tgl->table_group = table_group;
79 list_add_rcu(&tgl->next, &tbl->it_group_list);
80
81 table_group->tables[0] = tbl;
82
83 return table_group;
84
85fail_exit:
86 kfree(tgl);
87 kfree(table_group);
88 kfree(tbl);
89
90 return NULL;
91}
92
93static void iommu_pseries_free_group(struct iommu_table_group *table_group,
94 const char *node_name)
95{
96 struct iommu_table *tbl;
97#ifdef CONFIG_IOMMU_API
98 struct iommu_table_group_link *tgl;
99#endif
100
101 if (!table_group)
102 return;
103
104 tbl = table_group->tables[0];
105#ifdef CONFIG_IOMMU_API
106 tgl = list_first_entry_or_null(&tbl->it_group_list,
107 struct iommu_table_group_link, next);
108
109 WARN_ON_ONCE(!tgl);
110 if (tgl) {
111 list_del_rcu(&tgl->next);
112 kfree(tgl);
113 }
114 if (table_group->group) {
115 iommu_group_put(table_group->group);
116 BUG_ON(table_group->group);
117 }
118#endif
119 iommu_free_table(tbl, node_name);
120
121 kfree(table_group);
122}
123
124static void tce_invalidate_pSeries_sw(struct iommu_table *tbl,
125 __be64 *startp, __be64 *endp)
126{
127 u64 __iomem *invalidate = (u64 __iomem *)tbl->it_index;
128 unsigned long start, end, inc;
129
130 start = __pa(startp);
131 end = __pa(endp);
132 inc = L1_CACHE_BYTES;
133
134
135
136 if (tbl->it_busno) {
137 start <<= 12;
138 end <<= 12;
139 inc <<= 12;
140 start |= tbl->it_busno;
141 end |= tbl->it_busno;
142 }
143
144 end |= inc - 1;
145
146 mb();
147 while (start <= end) {
148 out_be64(invalidate, start);
149 start += inc;
150 }
151}
152
153static int tce_build_pSeries(struct iommu_table *tbl, long index,
154 long npages, unsigned long uaddr,
155 enum dma_data_direction direction,
156 struct dma_attrs *attrs)
157{
158 u64 proto_tce;
159 __be64 *tcep, *tces;
160 u64 rpn;
161
162 proto_tce = TCE_PCI_READ;
163
164 if (direction != DMA_TO_DEVICE)
165 proto_tce |= TCE_PCI_WRITE;
166
167 tces = tcep = ((__be64 *)tbl->it_base) + index;
168
169 while (npages--) {
170
171 rpn = __pa(uaddr) >> TCE_SHIFT;
172 *tcep = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
173
174 uaddr += TCE_PAGE_SIZE;
175 tcep++;
176 }
177
178 if (tbl->it_type & TCE_PCI_SWINV_CREATE)
179 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
180 return 0;
181}
182
183
184static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
185{
186 __be64 *tcep, *tces;
187
188 tces = tcep = ((__be64 *)tbl->it_base) + index;
189
190 while (npages--)
191 *(tcep++) = 0;
192
193 if (tbl->it_type & TCE_PCI_SWINV_FREE)
194 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
195}
196
197static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
198{
199 __be64 *tcep;
200
201 tcep = ((__be64 *)tbl->it_base) + index;
202
203 return be64_to_cpu(*tcep);
204}
205
206static void tce_free_pSeriesLP(struct iommu_table*, long, long);
207static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
208
209static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
210 long npages, unsigned long uaddr,
211 enum dma_data_direction direction,
212 struct dma_attrs *attrs)
213{
214 u64 rc = 0;
215 u64 proto_tce, tce;
216 u64 rpn;
217 int ret = 0;
218 long tcenum_start = tcenum, npages_start = npages;
219
220 rpn = __pa(uaddr) >> TCE_SHIFT;
221 proto_tce = TCE_PCI_READ;
222 if (direction != DMA_TO_DEVICE)
223 proto_tce |= TCE_PCI_WRITE;
224
225 while (npages--) {
226 tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
227 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
228
229 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
230 ret = (int)rc;
231 tce_free_pSeriesLP(tbl, tcenum_start,
232 (npages_start - (npages + 1)));
233 break;
234 }
235
236 if (rc && printk_ratelimit()) {
237 printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
238 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
239 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
240 printk("\ttce val = 0x%llx\n", tce );
241 show_stack(current, (unsigned long *)__get_SP());
242 }
243
244 tcenum++;
245 rpn++;
246 }
247 return ret;
248}
249
250static DEFINE_PER_CPU(__be64 *, tce_page);
251
252static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
253 long npages, unsigned long uaddr,
254 enum dma_data_direction direction,
255 struct dma_attrs *attrs)
256{
257 u64 rc = 0;
258 u64 proto_tce;
259 __be64 *tcep;
260 u64 rpn;
261 long l, limit;
262 long tcenum_start = tcenum, npages_start = npages;
263 int ret = 0;
264 unsigned long flags;
265
266 if ((npages == 1) || !firmware_has_feature(FW_FEATURE_MULTITCE)) {
267 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
268 direction, attrs);
269 }
270
271 local_irq_save(flags);
272
273 tcep = __get_cpu_var(tce_page);
274
275
276
277
278 if (!tcep) {
279 tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
280
281 if (!tcep) {
282 local_irq_restore(flags);
283 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
284 direction, attrs);
285 }
286 __get_cpu_var(tce_page) = tcep;
287 }
288
289 rpn = __pa(uaddr) >> TCE_SHIFT;
290 proto_tce = TCE_PCI_READ;
291 if (direction != DMA_TO_DEVICE)
292 proto_tce |= TCE_PCI_WRITE;
293
294
295 do {
296
297
298
299
300 limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
301
302 for (l = 0; l < limit; l++) {
303 tcep[l] = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
304 rpn++;
305 }
306
307 rc = plpar_tce_put_indirect((u64)tbl->it_index,
308 (u64)tcenum << 12,
309 (u64)__pa(tcep),
310 limit);
311
312 npages -= limit;
313 tcenum += limit;
314 } while (npages > 0 && !rc);
315
316 local_irq_restore(flags);
317
318 if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
319 ret = (int)rc;
320 tce_freemulti_pSeriesLP(tbl, tcenum_start,
321 (npages_start - (npages + limit)));
322 return ret;
323 }
324
325 if (rc && printk_ratelimit()) {
326 printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
327 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
328 printk("\tnpages = 0x%llx\n", (u64)npages);
329 printk("\ttce[0] val = 0x%llx\n", tcep[0]);
330 show_stack(current, (unsigned long *)__get_SP());
331 }
332 return ret;
333}
334
335static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
336{
337 u64 rc;
338
339 while (npages--) {
340 rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
341
342 if (rc && printk_ratelimit()) {
343 printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
344 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
345 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
346 show_stack(current, (unsigned long *)__get_SP());
347 }
348
349 tcenum++;
350 }
351}
352
353
354static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
355{
356 u64 rc;
357
358 if (!firmware_has_feature(FW_FEATURE_MULTITCE))
359 return tce_free_pSeriesLP(tbl, tcenum, npages);
360
361 rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
362
363 if (rc && printk_ratelimit()) {
364 printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
365 printk("\trc = %lld\n", rc);
366 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
367 printk("\tnpages = 0x%llx\n", (u64)npages);
368 show_stack(current, (unsigned long *)__get_SP());
369 }
370}
371
372static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
373{
374 u64 rc;
375 unsigned long tce_ret;
376
377 rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
378
379 if (rc && printk_ratelimit()) {
380 printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
381 printk("\tindex = 0x%llx\n", (u64)tbl->it_index);
382 printk("\ttcenum = 0x%llx\n", (u64)tcenum);
383 show_stack(current, (unsigned long *)__get_SP());
384 }
385
386 return tce_ret;
387}
388
389
390struct dynamic_dma_window_prop {
391 __be32 liobn;
392 __be64 dma_base;
393 __be32 tce_shift;
394 __be32 window_shift;
395};
396
397struct direct_window {
398 struct device_node *device;
399 const struct dynamic_dma_window_prop *prop;
400 struct list_head list;
401};
402
403
404struct ddw_query_response {
405 u32 windows_available;
406 u32 largest_available_block;
407 u32 page_size;
408 u32 migration_capable;
409};
410
411struct ddw_create_response {
412 u32 liobn;
413 u32 addr_hi;
414 u32 addr_lo;
415};
416
417static LIST_HEAD(direct_window_list);
418
419static DEFINE_SPINLOCK(direct_window_list_lock);
420
421static DEFINE_MUTEX(direct_window_init_mutex);
422#define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
423
424static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,
425 unsigned long num_pfn, const void *arg)
426{
427 const struct dynamic_dma_window_prop *maprange = arg;
428 int rc;
429 u64 tce_size, num_tce, dma_offset, next;
430 u32 tce_shift;
431 long limit;
432
433 tce_shift = be32_to_cpu(maprange->tce_shift);
434 tce_size = 1ULL << tce_shift;
435 next = start_pfn << PAGE_SHIFT;
436 num_tce = num_pfn << PAGE_SHIFT;
437
438
439 num_tce += next & (tce_size - 1);
440 next &= ~(tce_size - 1);
441
442
443 num_tce |= tce_size - 1;
444 num_tce >>= tce_shift;
445
446 do {
447
448
449
450
451 limit = min_t(long, num_tce, 512);
452 dma_offset = next + be64_to_cpu(maprange->dma_base);
453
454 rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn),
455 dma_offset,
456 0, limit);
457 next += limit * tce_size;
458 num_tce -= limit;
459 } while (num_tce > 0 && !rc);
460
461 return rc;
462}
463
464static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
465 unsigned long num_pfn, const void *arg)
466{
467 const struct dynamic_dma_window_prop *maprange = arg;
468 u64 tce_size, num_tce, dma_offset, next, proto_tce, liobn;
469 __be64 *tcep;
470 u32 tce_shift;
471 u64 rc = 0;
472 long l, limit;
473
474 local_irq_disable();
475 tcep = __get_cpu_var(tce_page);
476
477 if (!tcep) {
478 tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
479 if (!tcep) {
480 local_irq_enable();
481 return -ENOMEM;
482 }
483 __get_cpu_var(tce_page) = tcep;
484 }
485
486 proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
487
488 liobn = (u64)be32_to_cpu(maprange->liobn);
489 tce_shift = be32_to_cpu(maprange->tce_shift);
490 tce_size = 1ULL << tce_shift;
491 next = start_pfn << PAGE_SHIFT;
492 num_tce = num_pfn << PAGE_SHIFT;
493
494
495 num_tce += next & (tce_size - 1);
496 next &= ~(tce_size - 1);
497
498
499 num_tce |= tce_size - 1;
500 num_tce >>= tce_shift;
501
502
503 do {
504
505
506
507
508 limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
509 dma_offset = next + be64_to_cpu(maprange->dma_base);
510
511 for (l = 0; l < limit; l++) {
512 tcep[l] = cpu_to_be64(proto_tce | next);
513 next += tce_size;
514 }
515
516 rc = plpar_tce_put_indirect(liobn,
517 dma_offset,
518 (u64)__pa(tcep),
519 limit);
520
521 num_tce -= limit;
522 } while (num_tce > 0 && !rc);
523
524
525
526 local_irq_enable();
527 return rc;
528}
529
530static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
531 unsigned long num_pfn, void *arg)
532{
533 return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
534}
535
536#ifdef CONFIG_PCI
537static void iommu_table_setparms(struct pci_controller *phb,
538 struct device_node *dn,
539 struct iommu_table *tbl)
540{
541 struct device_node *node;
542 const unsigned long *basep, *sw_inval;
543 const u32 *sizep;
544
545 node = phb->dn;
546
547 basep = of_get_property(node, "linux,tce-base", NULL);
548 sizep = of_get_property(node, "linux,tce-size", NULL);
549 if (basep == NULL || sizep == NULL) {
550 printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
551 "missing tce entries !\n", dn->full_name);
552 return;
553 }
554
555 tbl->it_base = (unsigned long)__va(*basep);
556
557 if (!is_kdump_kernel())
558 memset((void *)tbl->it_base, 0, *sizep);
559
560 tbl->it_busno = phb->bus->number;
561 tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
562
563
564 tbl->it_offset = phb->dma_window_base_cur >> tbl->it_page_shift;
565
566
567 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
568 udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
569 panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
570 }
571
572 phb->dma_window_base_cur += phb->dma_window_size;
573
574
575 tbl->it_size = phb->dma_window_size >> tbl->it_page_shift;
576
577 tbl->it_index = 0;
578 tbl->it_blocksize = 16;
579 tbl->it_type = TCE_PCI;
580
581 sw_inval = of_get_property(node, "linux,tce-sw-invalidate-info", NULL);
582 if (sw_inval) {
583
584
585
586
587
588
589
590
591
592 tbl->it_index = (unsigned long) ioremap(sw_inval[0], 8);
593 tbl->it_busno = sw_inval[1];
594 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
595 }
596}
597
598
599
600
601
602
603static void iommu_table_setparms_lpar(struct pci_controller *phb,
604 struct device_node *dn,
605 struct iommu_table *tbl,
606 const __be32 *dma_window)
607{
608 unsigned long offset, size;
609
610 of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
611
612 tbl->it_busno = phb->bus->number;
613 tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
614 tbl->it_base = 0;
615 tbl->it_blocksize = 16;
616 tbl->it_type = TCE_PCI;
617 tbl->it_offset = offset >> tbl->it_page_shift;
618 tbl->it_size = size >> tbl->it_page_shift;
619}
620
621struct iommu_table_ops iommu_table_pseries_ops = {
622 .set = tce_build_pSeries,
623 .clear = tce_free_pSeries,
624 .get = tce_get_pseries
625};
626
627static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
628{
629 struct device_node *dn;
630 struct iommu_table *tbl;
631 struct device_node *isa_dn, *isa_dn_orig;
632 struct device_node *tmp;
633 struct pci_dn *pci;
634 int children;
635
636 dn = pci_bus_to_OF_node(bus);
637
638 pr_debug("pci_dma_bus_setup_pSeries: setting up bus %s\n", dn->full_name);
639
640 if (bus->self) {
641
642
643
644 return;
645 }
646 pci = PCI_DN(dn);
647
648
649
650
651 isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
652
653 while (isa_dn && isa_dn != dn)
654 isa_dn = isa_dn->parent;
655
656 if (isa_dn_orig)
657 of_node_put(isa_dn_orig);
658
659
660 for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
661 children++;
662
663 pr_debug("Children: %d\n", children);
664
665
666
667
668
669
670
671 if (!isa_dn) {
672
673 pci->phb->dma_window_size = 0x80000000ul;
674
675 while (pci->phb->dma_window_size * children > 0x80000000ul)
676 pci->phb->dma_window_size >>= 1;
677 pr_debug("No ISA/IDE, window size is 0x%llx\n",
678 pci->phb->dma_window_size);
679 pci->phb->dma_window_base_cur = 0;
680
681 return;
682 }
683
684
685
686
687
688
689 pci->phb->dma_window_size = 0x8000000ul;
690 pci->phb->dma_window_base_cur = 0x8000000ul;
691
692 pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
693 tbl = pci->table_group->tables[0];
694
695 iommu_table_setparms(pci->phb, dn, tbl);
696 tbl->it_ops = &iommu_table_pseries_ops;
697 iommu_init_table(tbl, pci->phb->node);
698 iommu_register_group(pci->table_group, pci_domain_nr(bus), 0);
699
700
701 pci->phb->dma_window_size = 0x80000000ul;
702 while (pci->phb->dma_window_size * children > 0x70000000ul)
703 pci->phb->dma_window_size >>= 1;
704
705 pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
706}
707
708struct iommu_table_ops iommu_table_lpar_multi_ops = {
709 .set = tce_buildmulti_pSeriesLP,
710 .clear = tce_freemulti_pSeriesLP,
711 .get = tce_get_pSeriesLP
712};
713
714static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
715{
716 struct iommu_table *tbl;
717 struct device_node *dn, *pdn;
718 struct pci_dn *ppci;
719 const __be32 *dma_window = NULL;
720
721 dn = pci_bus_to_OF_node(bus);
722
723 pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
724 dn->full_name);
725
726
727 for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
728 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
729 if (dma_window != NULL)
730 break;
731 }
732
733 if (dma_window == NULL) {
734 pr_debug(" no ibm,dma-window property !\n");
735 return;
736 }
737
738 ppci = PCI_DN(pdn);
739
740 pr_debug(" parent is %s, iommu_table: 0x%p\n",
741 pdn->full_name, ppci->table_group);
742
743 if (!ppci->table_group) {
744 ppci->table_group = iommu_pseries_alloc_group(ppci->phb->node);
745 tbl = ppci->table_group->tables[0];
746 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
747 tbl->it_ops = &iommu_table_lpar_multi_ops;
748 iommu_init_table(tbl, ppci->phb->node);
749 iommu_register_group(ppci->table_group,
750 pci_domain_nr(bus), 0);
751 pr_debug(" created table: %p\n", ppci->table_group);
752 }
753}
754
755
756static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
757{
758 struct device_node *dn;
759 struct iommu_table *tbl;
760
761 pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
762
763 dn = dev->dev.of_node;
764
765
766
767
768
769 if (!dev->bus->self) {
770 struct pci_controller *phb = PCI_DN(dn)->phb;
771
772 pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
773 PCI_DN(dn)->table_group = iommu_pseries_alloc_group(phb->node);
774 tbl = PCI_DN(dn)->table_group->tables[0];
775 iommu_table_setparms(phb, dn, tbl);
776 tbl->it_ops = &iommu_table_pseries_ops;
777 iommu_init_table(tbl, phb->node);
778 iommu_register_group(PCI_DN(dn)->table_group,
779 pci_domain_nr(phb->bus), 0);
780 set_iommu_table_base(&dev->dev, tbl);
781 iommu_add_device(&dev->dev);
782 return;
783 }
784
785
786
787
788
789 while (dn && PCI_DN(dn) && PCI_DN(dn)->table_group == NULL)
790 dn = dn->parent;
791
792 if (dn && PCI_DN(dn)) {
793 set_iommu_table_base(&dev->dev,
794 PCI_DN(dn)->table_group->tables[0]);
795 iommu_add_device(&dev->dev);
796 } else
797 printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
798 pci_name(dev));
799}
800
801static int __read_mostly disable_ddw;
802
803static int __init disable_ddw_setup(char *str)
804{
805 disable_ddw = 1;
806 printk(KERN_INFO "ppc iommu: disabling ddw.\n");
807
808 return 0;
809}
810
811early_param("disable_ddw", disable_ddw_setup);
812
813static void remove_ddw(struct device_node *np, bool remove_prop)
814{
815 struct dynamic_dma_window_prop *dwp;
816 struct property *win64;
817 u32 ddw_avail[3];
818 u64 liobn;
819 int ret = 0;
820
821 ret = of_property_read_u32_array(np, "ibm,ddw-applicable",
822 &ddw_avail[0], 3);
823
824 win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
825 if (!win64)
826 return;
827
828 if (ret || win64->length < sizeof(*dwp))
829 goto delprop;
830
831 dwp = win64->value;
832 liobn = (u64)be32_to_cpu(dwp->liobn);
833
834
835 ret = tce_clearrange_multi_pSeriesLP(0,
836 1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
837 if (ret)
838 pr_warning("%s failed to clear tces in window.\n",
839 np->full_name);
840 else
841 pr_debug("%s successfully cleared tces in window.\n",
842 np->full_name);
843
844 ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
845 if (ret)
846 pr_warning("%s: failed to remove DMA window: rtas returned "
847 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
848 np->full_name, ret, ddw_avail[2], liobn);
849 else
850 pr_debug("%s: successfully removed DMA window: rtas returned "
851 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
852 np->full_name, ret, ddw_avail[2], liobn);
853
854delprop:
855 if (remove_prop)
856 ret = of_remove_property(np, win64);
857 if (ret)
858 pr_warning("%s: failed to remove direct window property: %d\n",
859 np->full_name, ret);
860}
861
862static u64 find_existing_ddw(struct device_node *pdn)
863{
864 struct direct_window *window;
865 const struct dynamic_dma_window_prop *direct64;
866 u64 dma_addr = 0;
867
868 spin_lock(&direct_window_list_lock);
869
870 list_for_each_entry(window, &direct_window_list, list) {
871 if (window->device == pdn) {
872 direct64 = window->prop;
873 dma_addr = be64_to_cpu(direct64->dma_base);
874 break;
875 }
876 }
877 spin_unlock(&direct_window_list_lock);
878
879 return dma_addr;
880}
881
882static int find_existing_ddw_windows(void)
883{
884 int len;
885 struct device_node *pdn;
886 struct direct_window *window;
887 const struct dynamic_dma_window_prop *direct64;
888
889 if (!firmware_has_feature(FW_FEATURE_LPAR))
890 return 0;
891
892 for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
893 direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
894 if (!direct64)
895 continue;
896
897 window = kzalloc(sizeof(*window), GFP_KERNEL);
898 if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
899 kfree(window);
900 remove_ddw(pdn, true);
901 continue;
902 }
903
904 window->device = pdn;
905 window->prop = direct64;
906 spin_lock(&direct_window_list_lock);
907 list_add(&window->list, &direct_window_list);
908 spin_unlock(&direct_window_list_lock);
909 }
910
911 return 0;
912}
913machine_arch_initcall(pseries, find_existing_ddw_windows);
914
915static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
916 struct ddw_query_response *query)
917{
918 struct device_node *dn;
919 struct pci_dn *pdn;
920 u32 cfg_addr;
921 u64 buid;
922 int ret;
923
924
925
926
927
928
929
930 dn = pci_device_to_OF_node(dev);
931 pdn = PCI_DN(dn);
932 buid = pdn->phb->buid;
933 cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
934
935 ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
936 cfg_addr, BUID_HI(buid), BUID_LO(buid));
937 dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
938 " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
939 BUID_LO(buid), ret);
940 return ret;
941}
942
943static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
944 struct ddw_create_response *create, int page_shift,
945 int window_shift)
946{
947 struct device_node *dn;
948 struct pci_dn *pdn;
949 u32 cfg_addr;
950 u64 buid;
951 int ret;
952
953
954
955
956
957
958
959 dn = pci_device_to_OF_node(dev);
960 pdn = PCI_DN(dn);
961 buid = pdn->phb->buid;
962 cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
963
964 do {
965
966 ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create,
967 cfg_addr, BUID_HI(buid), BUID_LO(buid),
968 page_shift, window_shift);
969 } while (rtas_busy_delay(ret));
970 dev_info(&dev->dev,
971 "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
972 "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
973 cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
974 window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
975
976 return ret;
977}
978
979struct failed_ddw_pdn {
980 struct device_node *pdn;
981 struct list_head list;
982};
983
984static LIST_HEAD(failed_ddw_pdn_list);
985
986
987
988
989
990
991
992
993
994
995
996
997static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
998{
999 int len, ret;
1000 struct ddw_query_response query;
1001 struct ddw_create_response create;
1002 int page_shift;
1003 u64 dma_addr, max_addr;
1004 struct device_node *dn;
1005 u32 ddw_avail[3];
1006 struct direct_window *window;
1007 struct property *win64;
1008 struct dynamic_dma_window_prop *ddwprop;
1009 struct failed_ddw_pdn *fpdn;
1010
1011 mutex_lock(&direct_window_init_mutex);
1012
1013 dma_addr = find_existing_ddw(pdn);
1014 if (dma_addr != 0)
1015 goto out_unlock;
1016
1017
1018
1019
1020
1021
1022
1023
1024 list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) {
1025 if (!strcmp(fpdn->pdn->full_name, pdn->full_name))
1026 goto out_unlock;
1027 }
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037 ret = of_property_read_u32_array(pdn, "ibm,ddw-applicable",
1038 &ddw_avail[0], 3);
1039 if (ret)
1040 goto out_failed;
1041
1042
1043
1044
1045
1046
1047
1048 dn = pci_device_to_OF_node(dev);
1049 ret = query_ddw(dev, ddw_avail, &query);
1050 if (ret != 0)
1051 goto out_failed;
1052
1053 if (query.windows_available == 0) {
1054
1055
1056
1057
1058
1059 dev_dbg(&dev->dev, "no free dynamic windows");
1060 goto out_failed;
1061 }
1062 if (query.page_size & 4) {
1063 page_shift = 24;
1064 } else if (query.page_size & 2) {
1065 page_shift = 16;
1066 } else if (query.page_size & 1) {
1067 page_shift = 12;
1068 } else {
1069 dev_dbg(&dev->dev, "no supported direct page size in mask %x",
1070 query.page_size);
1071 goto out_failed;
1072 }
1073
1074
1075 max_addr = memory_hotplug_max();
1076 if (query.largest_available_block < (max_addr >> page_shift)) {
1077 dev_dbg(&dev->dev, "can't map partiton max 0x%llx with %u "
1078 "%llu-sized pages\n", max_addr, query.largest_available_block,
1079 1ULL << page_shift);
1080 goto out_failed;
1081 }
1082 len = order_base_2(max_addr);
1083 win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
1084 if (!win64) {
1085 dev_info(&dev->dev,
1086 "couldn't allocate property for 64bit dma window\n");
1087 goto out_failed;
1088 }
1089 win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
1090 win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
1091 win64->length = sizeof(*ddwprop);
1092 if (!win64->name || !win64->value) {
1093 dev_info(&dev->dev,
1094 "couldn't allocate property name and value\n");
1095 goto out_free_prop;
1096 }
1097
1098 ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
1099 if (ret != 0)
1100 goto out_free_prop;
1101
1102 ddwprop->liobn = cpu_to_be32(create.liobn);
1103 ddwprop->dma_base = cpu_to_be64(((u64)create.addr_hi << 32) |
1104 create.addr_lo);
1105 ddwprop->tce_shift = cpu_to_be32(page_shift);
1106 ddwprop->window_shift = cpu_to_be32(len);
1107
1108 dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %s\n",
1109 create.liobn, dn->full_name);
1110
1111 window = kzalloc(sizeof(*window), GFP_KERNEL);
1112 if (!window)
1113 goto out_clear_window;
1114
1115 ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
1116 win64->value, tce_setrange_multi_pSeriesLP_walk);
1117 if (ret) {
1118 dev_info(&dev->dev, "failed to map direct window for %s: %d\n",
1119 dn->full_name, ret);
1120 goto out_free_window;
1121 }
1122
1123 ret = of_add_property(pdn, win64);
1124 if (ret) {
1125 dev_err(&dev->dev, "unable to add dma window property for %s: %d",
1126 pdn->full_name, ret);
1127 goto out_free_window;
1128 }
1129
1130 window->device = pdn;
1131 window->prop = ddwprop;
1132 spin_lock(&direct_window_list_lock);
1133 list_add(&window->list, &direct_window_list);
1134 spin_unlock(&direct_window_list_lock);
1135
1136 dma_addr = be64_to_cpu(ddwprop->dma_base);
1137 goto out_unlock;
1138
1139out_free_window:
1140 kfree(window);
1141
1142out_clear_window:
1143 remove_ddw(pdn, true);
1144
1145out_free_prop:
1146 kfree(win64->name);
1147 kfree(win64->value);
1148 kfree(win64);
1149
1150out_failed:
1151 fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
1152 if (!fpdn)
1153 goto out_unlock;
1154 fpdn->pdn = pdn;
1155 list_add(&fpdn->list, &failed_ddw_pdn_list);
1156
1157out_unlock:
1158 mutex_unlock(&direct_window_init_mutex);
1159 return dma_addr;
1160}
1161
1162static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
1163{
1164 struct device_node *pdn, *dn;
1165 struct iommu_table *tbl;
1166 const __be32 *dma_window = NULL;
1167 struct pci_dn *pci;
1168
1169 pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
1170
1171
1172
1173
1174
1175
1176
1177 dn = pci_device_to_OF_node(dev);
1178 pr_debug(" node is %s\n", dn->full_name);
1179
1180 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1181 pdn = pdn->parent) {
1182 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1183 if (dma_window)
1184 break;
1185 }
1186
1187 if (!pdn || !PCI_DN(pdn)) {
1188 printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
1189 "no DMA window found for pci dev=%s dn=%s\n",
1190 pci_name(dev), of_node_full_name(dn));
1191 return;
1192 }
1193 pr_debug(" parent is %s\n", pdn->full_name);
1194
1195 pci = PCI_DN(pdn);
1196 if (!pci->table_group) {
1197 pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
1198 tbl = pci->table_group->tables[0];
1199 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
1200 tbl->it_ops = &iommu_table_lpar_multi_ops;
1201 iommu_init_table(tbl, pci->phb->node);
1202 iommu_register_group(pci->table_group,
1203 pci_domain_nr(pci->phb->bus), 0);
1204 pr_debug(" created table: %p\n", pci->table_group);
1205 } else {
1206 pr_debug(" found DMA window, table: %p\n", pci->table_group);
1207 }
1208
1209 set_iommu_table_base(&dev->dev, pci->table_group->tables[0]);
1210 iommu_add_device(&dev->dev);
1211}
1212
1213static int dma_set_mask_pSeriesLP(struct device *dev, u64 dma_mask)
1214{
1215 bool ddw_enabled = false;
1216 struct device_node *pdn, *dn;
1217 struct pci_dev *pdev;
1218 const __be32 *dma_window = NULL;
1219 u64 dma_offset;
1220
1221 if (!dev->dma_mask)
1222 return -EIO;
1223
1224 if (!dev_is_pci(dev))
1225 goto check_mask;
1226
1227 pdev = to_pci_dev(dev);
1228
1229
1230 if (!disable_ddw && dma_mask == DMA_BIT_MASK(64)) {
1231 dn = pci_device_to_OF_node(pdev);
1232 dev_dbg(dev, "node is %s\n", dn->full_name);
1233
1234
1235
1236
1237
1238
1239
1240 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1241 pdn = pdn->parent) {
1242 dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1243 if (dma_window)
1244 break;
1245 }
1246 if (pdn && PCI_DN(pdn)) {
1247 dma_offset = enable_ddw(pdev, pdn);
1248 if (dma_offset != 0) {
1249 dev_info(dev, "Using 64-bit direct DMA at offset %llx\n", dma_offset);
1250 set_dma_offset(dev, dma_offset);
1251 set_dma_ops(dev, &dma_direct_ops);
1252 ddw_enabled = true;
1253 }
1254 }
1255 }
1256
1257
1258 if (!ddw_enabled && get_dma_ops(dev) != &dma_iommu_ops) {
1259 dev_info(dev, "Restoring 32-bit DMA via iommu\n");
1260 set_dma_ops(dev, &dma_iommu_ops);
1261 }
1262
1263check_mask:
1264 if (!dma_supported(dev, dma_mask))
1265 return -EIO;
1266
1267 *dev->dma_mask = dma_mask;
1268 return 0;
1269}
1270
1271static u64 dma_get_required_mask_pSeriesLP(struct device *dev)
1272{
1273 if (!dev->dma_mask)
1274 return 0;
1275
1276 if (!disable_ddw && dev_is_pci(dev)) {
1277 struct pci_dev *pdev = to_pci_dev(dev);
1278 struct device_node *dn;
1279
1280 dn = pci_device_to_OF_node(pdev);
1281
1282
1283 for (; dn && PCI_DN(dn) && !PCI_DN(dn)->table_group;
1284 dn = dn->parent)
1285 if (of_get_property(dn, "ibm,dma-window", NULL))
1286 break;
1287
1288 if (dn && PCI_DN(dn) &&
1289 of_get_property(dn, "ibm,ddw-applicable", NULL))
1290 return DMA_BIT_MASK(64);
1291 }
1292
1293 return dma_iommu_ops.get_required_mask(dev);
1294}
1295
1296#else
1297#define pci_dma_bus_setup_pSeries NULL
1298#define pci_dma_dev_setup_pSeries NULL
1299#define pci_dma_bus_setup_pSeriesLP NULL
1300#define pci_dma_dev_setup_pSeriesLP NULL
1301#define dma_set_mask_pSeriesLP NULL
1302#define dma_get_required_mask_pSeriesLP NULL
1303#endif
1304
1305static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
1306 void *data)
1307{
1308 struct direct_window *window;
1309 struct memory_notify *arg = data;
1310 int ret = 0;
1311
1312 switch (action) {
1313 case MEM_GOING_ONLINE:
1314 spin_lock(&direct_window_list_lock);
1315 list_for_each_entry(window, &direct_window_list, list) {
1316 ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
1317 arg->nr_pages, window->prop);
1318
1319 }
1320 spin_unlock(&direct_window_list_lock);
1321 break;
1322 case MEM_CANCEL_ONLINE:
1323 case MEM_OFFLINE:
1324 spin_lock(&direct_window_list_lock);
1325 list_for_each_entry(window, &direct_window_list, list) {
1326 ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
1327 arg->nr_pages, window->prop);
1328
1329 }
1330 spin_unlock(&direct_window_list_lock);
1331 break;
1332 default:
1333 break;
1334 }
1335 if (ret && action != MEM_CANCEL_ONLINE)
1336 return NOTIFY_BAD;
1337
1338 return NOTIFY_OK;
1339}
1340
1341static struct notifier_block iommu_mem_nb = {
1342 .notifier_call = iommu_mem_notifier,
1343};
1344
1345static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
1346{
1347 int err = NOTIFY_OK;
1348 struct device_node *np = node;
1349 struct pci_dn *pci = PCI_DN(np);
1350 struct direct_window *window;
1351
1352 switch (action) {
1353 case OF_RECONFIG_DETACH_NODE:
1354
1355
1356
1357
1358
1359
1360
1361 remove_ddw(np, false);
1362 if (pci && pci->table_group)
1363 iommu_pseries_free_group(pci->table_group,
1364 np->full_name);
1365
1366 spin_lock(&direct_window_list_lock);
1367 list_for_each_entry(window, &direct_window_list, list) {
1368 if (window->device == np) {
1369 list_del(&window->list);
1370 kfree(window);
1371 break;
1372 }
1373 }
1374 spin_unlock(&direct_window_list_lock);
1375 break;
1376 default:
1377 err = NOTIFY_DONE;
1378 break;
1379 }
1380 return err;
1381}
1382
1383static struct notifier_block iommu_reconfig_nb = {
1384 .notifier_call = iommu_reconfig_notifier,
1385};
1386
1387
1388void iommu_init_early_pSeries(void)
1389{
1390 if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
1391 return;
1392
1393 if (firmware_has_feature(FW_FEATURE_LPAR)) {
1394 pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
1395 pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
1396 ppc_md.dma_set_mask = dma_set_mask_pSeriesLP;
1397 ppc_md.dma_get_required_mask = dma_get_required_mask_pSeriesLP;
1398 } else {
1399 pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeries;
1400 pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeries;
1401 }
1402
1403
1404 of_reconfig_notifier_register(&iommu_reconfig_nb);
1405 register_memory_notifier(&iommu_mem_nb);
1406
1407 set_pci_dma_ops(&dma_iommu_ops);
1408}
1409
1410static int __init disable_multitce(char *str)
1411{
1412 if (strcmp(str, "off") == 0 &&
1413 firmware_has_feature(FW_FEATURE_LPAR) &&
1414 firmware_has_feature(FW_FEATURE_MULTITCE)) {
1415 printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
1416 powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
1417 }
1418 return 1;
1419}
1420
1421__setup("multitce=", disable_multitce);
1422
1423machine_subsys_initcall_sync(pseries, tce_iommu_bus_notifier_init);
1424