1 /*
2  * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
3  *
4  * Rewrite, cleanup:
5  *
6  * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
7  * Copyright (C) 2006 Olof Johansson <olof@lixom.net>
8  *
9  * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR.
10  *
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
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/string.h>
34 #include <linux/pci.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/crash_dump.h>
37 #include <linux/memory.h>
38 #include <linux/of.h>
39 #include <linux/iommu.h>
40 #include <linux/rculist.h>
41 #include <asm/io.h>
42 #include <asm/prom.h>
43 #include <asm/rtas.h>
44 #include <asm/iommu.h>
45 #include <asm/pci-bridge.h>
46 #include <asm/machdep.h>
47 #include <asm/firmware.h>
48 #include <asm/tce.h>
49 #include <asm/ppc-pci.h>
50 #include <asm/udbg.h>
51 #include <asm/mmzone.h>
52 #include <asm/plpar_wrappers.h>
53 
54 #include "pseries.h"
55 
56 static struct iommu_table_group *iommu_pseries_alloc_group(int node)
57 {
58 	struct iommu_table_group *table_group;
59 	struct iommu_table *tbl;
60 
61 	table_group = kzalloc_node(sizeof(struct iommu_table_group), GFP_KERNEL,
62 			   node);
63 	if (!table_group)
64 		return NULL;
65 
66 	tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, node);
67 	if (!tbl)
68 		goto free_group;
69 
70 	INIT_LIST_HEAD_RCU(&tbl->it_group_list);
71 	kref_init(&tbl->it_kref);
72 
73 	table_group->tables[0] = tbl;
74 
75 	return table_group;
76 
77 free_group:
78 	kfree(table_group);
79 	return NULL;
80 }
81 
82 static void iommu_pseries_free_group(struct iommu_table_group *table_group,
83 		const char *node_name)
84 {
85 	struct iommu_table *tbl;
86 
87 	if (!table_group)
88 		return;
89 
90 	tbl = table_group->tables[0];
91 #ifdef CONFIG_IOMMU_API
92 	if (table_group->group) {
93 		iommu_group_put(table_group->group);
94 		BUG_ON(table_group->group);
95 	}
96 #endif
97 	iommu_tce_table_put(tbl);
98 
99 	kfree(table_group);
100 }
101 
102 static int tce_build_pSeries(struct iommu_table *tbl, long index,
103 			      long npages, unsigned long uaddr,
104 			      enum dma_data_direction direction,
105 			      unsigned long attrs)
106 {
107 	u64 proto_tce;
108 	__be64 *tcep;
109 	u64 rpn;
110 
111 	proto_tce = TCE_PCI_READ; // Read allowed
112 
113 	if (direction != DMA_TO_DEVICE)
114 		proto_tce |= TCE_PCI_WRITE;
115 
116 	tcep = ((__be64 *)tbl->it_base) + index;
117 
118 	while (npages--) {
119 		/* can't move this out since we might cross MEMBLOCK boundary */
120 		rpn = __pa(uaddr) >> TCE_SHIFT;
121 		*tcep = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
122 
123 		uaddr += TCE_PAGE_SIZE;
124 		tcep++;
125 	}
126 	return 0;
127 }
128 
129 
130 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
131 {
132 	__be64 *tcep;
133 
134 	tcep = ((__be64 *)tbl->it_base) + index;
135 
136 	while (npages--)
137 		*(tcep++) = 0;
138 }
139 
140 static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
141 {
142 	__be64 *tcep;
143 
144 	tcep = ((__be64 *)tbl->it_base) + index;
145 
146 	return be64_to_cpu(*tcep);
147 }
148 
149 static void tce_free_pSeriesLP(struct iommu_table*, long, long);
150 static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
151 
152 static int tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
153 				long npages, unsigned long uaddr,
154 				enum dma_data_direction direction,
155 				unsigned long attrs)
156 {
157 	u64 rc = 0;
158 	u64 proto_tce, tce;
159 	u64 rpn;
160 	int ret = 0;
161 	long tcenum_start = tcenum, npages_start = npages;
162 
163 	rpn = __pa(uaddr) >> TCE_SHIFT;
164 	proto_tce = TCE_PCI_READ;
165 	if (direction != DMA_TO_DEVICE)
166 		proto_tce |= TCE_PCI_WRITE;
167 
168 	while (npages--) {
169 		tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
170 		rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
171 
172 		if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
173 			ret = (int)rc;
174 			tce_free_pSeriesLP(tbl, tcenum_start,
175 			                   (npages_start - (npages + 1)));
176 			break;
177 		}
178 
179 		if (rc && printk_ratelimit()) {
180 			printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
181 			printk("\tindex   = 0x%llx\n", (u64)tbl->it_index);
182 			printk("\ttcenum  = 0x%llx\n", (u64)tcenum);
183 			printk("\ttce val = 0x%llx\n", tce );
184 			dump_stack();
185 		}
186 
187 		tcenum++;
188 		rpn++;
189 	}
190 	return ret;
191 }
192 
193 static DEFINE_PER_CPU(__be64 *, tce_page);
194 
195 static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
196 				     long npages, unsigned long uaddr,
197 				     enum dma_data_direction direction,
198 				     unsigned long attrs)
199 {
200 	u64 rc = 0;
201 	u64 proto_tce;
202 	__be64 *tcep;
203 	u64 rpn;
204 	long l, limit;
205 	long tcenum_start = tcenum, npages_start = npages;
206 	int ret = 0;
207 	unsigned long flags;
208 
209 	if ((npages == 1) || !firmware_has_feature(FW_FEATURE_MULTITCE)) {
210 		return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
211 		                           direction, attrs);
212 	}
213 
214 	local_irq_save(flags);	/* to protect tcep and the page behind it */
215 
216 	tcep = __this_cpu_read(tce_page);
217 
218 	/* This is safe to do since interrupts are off when we're called
219 	 * from iommu_alloc{,_sg}()
220 	 */
221 	if (!tcep) {
222 		tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
223 		/* If allocation fails, fall back to the loop implementation */
224 		if (!tcep) {
225 			local_irq_restore(flags);
226 			return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
227 					    direction, attrs);
228 		}
229 		__this_cpu_write(tce_page, tcep);
230 	}
231 
232 	rpn = __pa(uaddr) >> TCE_SHIFT;
233 	proto_tce = TCE_PCI_READ;
234 	if (direction != DMA_TO_DEVICE)
235 		proto_tce |= TCE_PCI_WRITE;
236 
237 	/* We can map max one pageful of TCEs at a time */
238 	do {
239 		/*
240 		 * Set up the page with TCE data, looping through and setting
241 		 * the values.
242 		 */
243 		limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
244 
245 		for (l = 0; l < limit; l++) {
246 			tcep[l] = cpu_to_be64(proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT);
247 			rpn++;
248 		}
249 
250 		rc = plpar_tce_put_indirect((u64)tbl->it_index,
251 					    (u64)tcenum << 12,
252 					    (u64)__pa(tcep),
253 					    limit);
254 
255 		npages -= limit;
256 		tcenum += limit;
257 	} while (npages > 0 && !rc);
258 
259 	local_irq_restore(flags);
260 
261 	if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
262 		ret = (int)rc;
263 		tce_freemulti_pSeriesLP(tbl, tcenum_start,
264 		                        (npages_start - (npages + limit)));
265 		return ret;
266 	}
267 
268 	if (rc && printk_ratelimit()) {
269 		printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
270 		printk("\tindex   = 0x%llx\n", (u64)tbl->it_index);
271 		printk("\tnpages  = 0x%llx\n", (u64)npages);
272 		printk("\ttce[0] val = 0x%llx\n", tcep[0]);
273 		dump_stack();
274 	}
275 	return ret;
276 }
277 
278 static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
279 {
280 	u64 rc;
281 
282 	while (npages--) {
283 		rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
284 
285 		if (rc && printk_ratelimit()) {
286 			printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
287 			printk("\tindex   = 0x%llx\n", (u64)tbl->it_index);
288 			printk("\ttcenum  = 0x%llx\n", (u64)tcenum);
289 			dump_stack();
290 		}
291 
292 		tcenum++;
293 	}
294 }
295 
296 
297 static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
298 {
299 	u64 rc;
300 
301 	if (!firmware_has_feature(FW_FEATURE_MULTITCE))
302 		return tce_free_pSeriesLP(tbl, tcenum, npages);
303 
304 	rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
305 
306 	if (rc && printk_ratelimit()) {
307 		printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
308 		printk("\trc      = %lld\n", rc);
309 		printk("\tindex   = 0x%llx\n", (u64)tbl->it_index);
310 		printk("\tnpages  = 0x%llx\n", (u64)npages);
311 		dump_stack();
312 	}
313 }
314 
315 static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
316 {
317 	u64 rc;
318 	unsigned long tce_ret;
319 
320 	rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
321 
322 	if (rc && printk_ratelimit()) {
323 		printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
324 		printk("\tindex   = 0x%llx\n", (u64)tbl->it_index);
325 		printk("\ttcenum  = 0x%llx\n", (u64)tcenum);
326 		dump_stack();
327 	}
328 
329 	return tce_ret;
330 }
331 
332 /* this is compatible with cells for the device tree property */
333 struct dynamic_dma_window_prop {
334 	__be32	liobn;		/* tce table number */
335 	__be64	dma_base;	/* address hi,lo */
336 	__be32	tce_shift;	/* ilog2(tce_page_size) */
337 	__be32	window_shift;	/* ilog2(tce_window_size) */
338 };
339 
340 struct direct_window {
341 	struct device_node *device;
342 	const struct dynamic_dma_window_prop *prop;
343 	struct list_head list;
344 };
345 
346 /* Dynamic DMA Window support */
347 struct ddw_query_response {
348 	u32 windows_available;
349 	u32 largest_available_block;
350 	u32 page_size;
351 	u32 migration_capable;
352 };
353 
354 struct ddw_create_response {
355 	u32 liobn;
356 	u32 addr_hi;
357 	u32 addr_lo;
358 };
359 
360 static LIST_HEAD(direct_window_list);
361 /* prevents races between memory on/offline and window creation */
362 static DEFINE_SPINLOCK(direct_window_list_lock);
363 /* protects initializing window twice for same device */
364 static DEFINE_MUTEX(direct_window_init_mutex);
365 #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
366 
367 static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,
368 					unsigned long num_pfn, const void *arg)
369 {
370 	const struct dynamic_dma_window_prop *maprange = arg;
371 	int rc;
372 	u64 tce_size, num_tce, dma_offset, next;
373 	u32 tce_shift;
374 	long limit;
375 
376 	tce_shift = be32_to_cpu(maprange->tce_shift);
377 	tce_size = 1ULL << tce_shift;
378 	next = start_pfn << PAGE_SHIFT;
379 	num_tce = num_pfn << PAGE_SHIFT;
380 
381 	/* round back to the beginning of the tce page size */
382 	num_tce += next & (tce_size - 1);
383 	next &= ~(tce_size - 1);
384 
385 	/* covert to number of tces */
386 	num_tce |= tce_size - 1;
387 	num_tce >>= tce_shift;
388 
389 	do {
390 		/*
391 		 * Set up the page with TCE data, looping through and setting
392 		 * the values.
393 		 */
394 		limit = min_t(long, num_tce, 512);
395 		dma_offset = next + be64_to_cpu(maprange->dma_base);
396 
397 		rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn),
398 					     dma_offset,
399 					     0, limit);
400 		next += limit * tce_size;
401 		num_tce -= limit;
402 	} while (num_tce > 0 && !rc);
403 
404 	return rc;
405 }
406 
407 static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
408 					unsigned long num_pfn, const void *arg)
409 {
410 	const struct dynamic_dma_window_prop *maprange = arg;
411 	u64 tce_size, num_tce, dma_offset, next, proto_tce, liobn;
412 	__be64 *tcep;
413 	u32 tce_shift;
414 	u64 rc = 0;
415 	long l, limit;
416 
417 	local_irq_disable();	/* to protect tcep and the page behind it */
418 	tcep = __this_cpu_read(tce_page);
419 
420 	if (!tcep) {
421 		tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
422 		if (!tcep) {
423 			local_irq_enable();
424 			return -ENOMEM;
425 		}
426 		__this_cpu_write(tce_page, tcep);
427 	}
428 
429 	proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
430 
431 	liobn = (u64)be32_to_cpu(maprange->liobn);
432 	tce_shift = be32_to_cpu(maprange->tce_shift);
433 	tce_size = 1ULL << tce_shift;
434 	next = start_pfn << PAGE_SHIFT;
435 	num_tce = num_pfn << PAGE_SHIFT;
436 
437 	/* round back to the beginning of the tce page size */
438 	num_tce += next & (tce_size - 1);
439 	next &= ~(tce_size - 1);
440 
441 	/* covert to number of tces */
442 	num_tce |= tce_size - 1;
443 	num_tce >>= tce_shift;
444 
445 	/* We can map max one pageful of TCEs at a time */
446 	do {
447 		/*
448 		 * Set up the page with TCE data, looping through and setting
449 		 * the values.
450 		 */
451 		limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
452 		dma_offset = next + be64_to_cpu(maprange->dma_base);
453 
454 		for (l = 0; l < limit; l++) {
455 			tcep[l] = cpu_to_be64(proto_tce | next);
456 			next += tce_size;
457 		}
458 
459 		rc = plpar_tce_put_indirect(liobn,
460 					    dma_offset,
461 					    (u64)__pa(tcep),
462 					    limit);
463 
464 		num_tce -= limit;
465 	} while (num_tce > 0 && !rc);
466 
467 	/* error cleanup: caller will clear whole range */
468 
469 	local_irq_enable();
470 	return rc;
471 }
472 
473 static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
474 		unsigned long num_pfn, void *arg)
475 {
476 	return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
477 }
478 
479 static void iommu_table_setparms(struct pci_controller *phb,
480 				 struct device_node *dn,
481 				 struct iommu_table *tbl)
482 {
483 	struct device_node *node;
484 	const unsigned long *basep;
485 	const u32 *sizep;
486 
487 	node = phb->dn;
488 
489 	basep = of_get_property(node, "linux,tce-base", NULL);
490 	sizep = of_get_property(node, "linux,tce-size", NULL);
491 	if (basep == NULL || sizep == NULL) {
492 		printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %pOF has "
493 				"missing tce entries !\n", dn);
494 		return;
495 	}
496 
497 	tbl->it_base = (unsigned long)__va(*basep);
498 
499 	if (!is_kdump_kernel())
500 		memset((void *)tbl->it_base, 0, *sizep);
501 
502 	tbl->it_busno = phb->bus->number;
503 	tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
504 
505 	/* Units of tce entries */
506 	tbl->it_offset = phb->dma_window_base_cur >> tbl->it_page_shift;
507 
508 	/* Test if we are going over 2GB of DMA space */
509 	if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
510 		udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
511 		panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
512 	}
513 
514 	phb->dma_window_base_cur += phb->dma_window_size;
515 
516 	/* Set the tce table size - measured in entries */
517 	tbl->it_size = phb->dma_window_size >> tbl->it_page_shift;
518 
519 	tbl->it_index = 0;
520 	tbl->it_blocksize = 16;
521 	tbl->it_type = TCE_PCI;
522 }
523 
524 /*
525  * iommu_table_setparms_lpar
526  *
527  * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
528  */
529 static void iommu_table_setparms_lpar(struct pci_controller *phb,
530 				      struct device_node *dn,
531 				      struct iommu_table *tbl,
532 				      struct iommu_table_group *table_group,
533 				      const __be32 *dma_window)
534 {
535 	unsigned long offset, size;
536 
537 	of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
538 
539 	tbl->it_busno = phb->bus->number;
540 	tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
541 	tbl->it_base   = 0;
542 	tbl->it_blocksize  = 16;
543 	tbl->it_type = TCE_PCI;
544 	tbl->it_offset = offset >> tbl->it_page_shift;
545 	tbl->it_size = size >> tbl->it_page_shift;
546 
547 	table_group->tce32_start = offset;
548 	table_group->tce32_size = size;
549 }
550 
551 struct iommu_table_ops iommu_table_pseries_ops = {
552 	.set = tce_build_pSeries,
553 	.clear = tce_free_pSeries,
554 	.get = tce_get_pseries
555 };
556 
557 static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
558 {
559 	struct device_node *dn;
560 	struct iommu_table *tbl;
561 	struct device_node *isa_dn, *isa_dn_orig;
562 	struct device_node *tmp;
563 	struct pci_dn *pci;
564 	int children;
565 
566 	dn = pci_bus_to_OF_node(bus);
567 
568 	pr_debug("pci_dma_bus_setup_pSeries: setting up bus %pOF\n", dn);
569 
570 	if (bus->self) {
571 		/* This is not a root bus, any setup will be done for the
572 		 * device-side of the bridge in iommu_dev_setup_pSeries().
573 		 */
574 		return;
575 	}
576 	pci = PCI_DN(dn);
577 
578 	/* Check if the ISA bus on the system is under
579 	 * this PHB.
580 	 */
581 	isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
582 
583 	while (isa_dn && isa_dn != dn)
584 		isa_dn = isa_dn->parent;
585 
586 	of_node_put(isa_dn_orig);
587 
588 	/* Count number of direct PCI children of the PHB. */
589 	for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
590 		children++;
591 
592 	pr_debug("Children: %d\n", children);
593 
594 	/* Calculate amount of DMA window per slot. Each window must be
595 	 * a power of two (due to pci_alloc_consistent requirements).
596 	 *
597 	 * Keep 256MB aside for PHBs with ISA.
598 	 */
599 
600 	if (!isa_dn) {
601 		/* No ISA/IDE - just set window size and return */
602 		pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
603 
604 		while (pci->phb->dma_window_size * children > 0x80000000ul)
605 			pci->phb->dma_window_size >>= 1;
606 		pr_debug("No ISA/IDE, window size is 0x%llx\n",
607 			 pci->phb->dma_window_size);
608 		pci->phb->dma_window_base_cur = 0;
609 
610 		return;
611 	}
612 
613 	/* If we have ISA, then we probably have an IDE
614 	 * controller too. Allocate a 128MB table but
615 	 * skip the first 128MB to avoid stepping on ISA
616 	 * space.
617 	 */
618 	pci->phb->dma_window_size = 0x8000000ul;
619 	pci->phb->dma_window_base_cur = 0x8000000ul;
620 
621 	pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
622 	tbl = pci->table_group->tables[0];
623 
624 	iommu_table_setparms(pci->phb, dn, tbl);
625 	tbl->it_ops = &iommu_table_pseries_ops;
626 	iommu_init_table(tbl, pci->phb->node);
627 
628 	/* Divide the rest (1.75GB) among the children */
629 	pci->phb->dma_window_size = 0x80000000ul;
630 	while (pci->phb->dma_window_size * children > 0x70000000ul)
631 		pci->phb->dma_window_size >>= 1;
632 
633 	pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
634 }
635 
636 #ifdef CONFIG_IOMMU_API
637 static int tce_exchange_pseries(struct iommu_table *tbl, long index, unsigned
638 				long *tce, enum dma_data_direction *direction)
639 {
640 	long rc;
641 	unsigned long ioba = (unsigned long) index << tbl->it_page_shift;
642 	unsigned long flags, oldtce = 0;
643 	u64 proto_tce = iommu_direction_to_tce_perm(*direction);
644 	unsigned long newtce = *tce | proto_tce;
645 
646 	spin_lock_irqsave(&tbl->large_pool.lock, flags);
647 
648 	rc = plpar_tce_get((u64)tbl->it_index, ioba, &oldtce);
649 	if (!rc)
650 		rc = plpar_tce_put((u64)tbl->it_index, ioba, newtce);
651 
652 	if (!rc) {
653 		*direction = iommu_tce_direction(oldtce);
654 		*tce = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
655 	}
656 
657 	spin_unlock_irqrestore(&tbl->large_pool.lock, flags);
658 
659 	return rc;
660 }
661 #endif
662 
663 struct iommu_table_ops iommu_table_lpar_multi_ops = {
664 	.set = tce_buildmulti_pSeriesLP,
665 #ifdef CONFIG_IOMMU_API
666 	.exchange = tce_exchange_pseries,
667 #endif
668 	.clear = tce_freemulti_pSeriesLP,
669 	.get = tce_get_pSeriesLP
670 };
671 
672 static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
673 {
674 	struct iommu_table *tbl;
675 	struct device_node *dn, *pdn;
676 	struct pci_dn *ppci;
677 	const __be32 *dma_window = NULL;
678 
679 	dn = pci_bus_to_OF_node(bus);
680 
681 	pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %pOF\n",
682 		 dn);
683 
684 	/* Find nearest ibm,dma-window, walking up the device tree */
685 	for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
686 		dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
687 		if (dma_window != NULL)
688 			break;
689 	}
690 
691 	if (dma_window == NULL) {
692 		pr_debug("  no ibm,dma-window property !\n");
693 		return;
694 	}
695 
696 	ppci = PCI_DN(pdn);
697 
698 	pr_debug("  parent is %pOF, iommu_table: 0x%p\n",
699 		 pdn, ppci->table_group);
700 
701 	if (!ppci->table_group) {
702 		ppci->table_group = iommu_pseries_alloc_group(ppci->phb->node);
703 		tbl = ppci->table_group->tables[0];
704 		iommu_table_setparms_lpar(ppci->phb, pdn, tbl,
705 				ppci->table_group, dma_window);
706 		tbl->it_ops = &iommu_table_lpar_multi_ops;
707 		iommu_init_table(tbl, ppci->phb->node);
708 		iommu_register_group(ppci->table_group,
709 				pci_domain_nr(bus), 0);
710 		pr_debug("  created table: %p\n", ppci->table_group);
711 	}
712 }
713 
714 
715 static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
716 {
717 	struct device_node *dn;
718 	struct iommu_table *tbl;
719 
720 	pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
721 
722 	dn = dev->dev.of_node;
723 
724 	/* If we're the direct child of a root bus, then we need to allocate
725 	 * an iommu table ourselves. The bus setup code should have setup
726 	 * the window sizes already.
727 	 */
728 	if (!dev->bus->self) {
729 		struct pci_controller *phb = PCI_DN(dn)->phb;
730 
731 		pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
732 		PCI_DN(dn)->table_group = iommu_pseries_alloc_group(phb->node);
733 		tbl = PCI_DN(dn)->table_group->tables[0];
734 		iommu_table_setparms(phb, dn, tbl);
735 		tbl->it_ops = &iommu_table_pseries_ops;
736 		iommu_init_table(tbl, phb->node);
737 		set_iommu_table_base(&dev->dev, tbl);
738 		return;
739 	}
740 
741 	/* If this device is further down the bus tree, search upwards until
742 	 * an already allocated iommu table is found and use that.
743 	 */
744 
745 	while (dn && PCI_DN(dn) && PCI_DN(dn)->table_group == NULL)
746 		dn = dn->parent;
747 
748 	if (dn && PCI_DN(dn))
749 		set_iommu_table_base(&dev->dev,
750 				PCI_DN(dn)->table_group->tables[0]);
751 	else
752 		printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
753 		       pci_name(dev));
754 }
755 
756 static int __read_mostly disable_ddw;
757 
758 static int __init disable_ddw_setup(char *str)
759 {
760 	disable_ddw = 1;
761 	printk(KERN_INFO "ppc iommu: disabling ddw.\n");
762 
763 	return 0;
764 }
765 
766 early_param("disable_ddw", disable_ddw_setup);
767 
768 static void remove_ddw(struct device_node *np, bool remove_prop)
769 {
770 	struct dynamic_dma_window_prop *dwp;
771 	struct property *win64;
772 	u32 ddw_avail[3];
773 	u64 liobn;
774 	int ret = 0;
775 
776 	ret = of_property_read_u32_array(np, "ibm,ddw-applicable",
777 					 &ddw_avail[0], 3);
778 
779 	win64 = of_find_property(np, DIRECT64_PROPNAME, NULL);
780 	if (!win64)
781 		return;
782 
783 	if (ret || win64->length < sizeof(*dwp))
784 		goto delprop;
785 
786 	dwp = win64->value;
787 	liobn = (u64)be32_to_cpu(dwp->liobn);
788 
789 	/* clear the whole window, note the arg is in kernel pages */
790 	ret = tce_clearrange_multi_pSeriesLP(0,
791 		1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
792 	if (ret)
793 		pr_warn("%pOF failed to clear tces in window.\n",
794 			np);
795 	else
796 		pr_debug("%pOF successfully cleared tces in window.\n",
797 			 np);
798 
799 	ret = rtas_call(ddw_avail[2], 1, 1, NULL, liobn);
800 	if (ret)
801 		pr_warn("%pOF: failed to remove direct window: rtas returned "
802 			"%d to ibm,remove-pe-dma-window(%x) %llx\n",
803 			np, ret, ddw_avail[2], liobn);
804 	else
805 		pr_debug("%pOF: successfully removed direct window: rtas returned "
806 			"%d to ibm,remove-pe-dma-window(%x) %llx\n",
807 			np, ret, ddw_avail[2], liobn);
808 
809 delprop:
810 	if (remove_prop)
811 		ret = of_remove_property(np, win64);
812 	if (ret)
813 		pr_warn("%pOF: failed to remove direct window property: %d\n",
814 			np, ret);
815 }
816 
817 static u64 find_existing_ddw(struct device_node *pdn)
818 {
819 	struct direct_window *window;
820 	const struct dynamic_dma_window_prop *direct64;
821 	u64 dma_addr = 0;
822 
823 	spin_lock(&direct_window_list_lock);
824 	/* check if we already created a window and dupe that config if so */
825 	list_for_each_entry(window, &direct_window_list, list) {
826 		if (window->device == pdn) {
827 			direct64 = window->prop;
828 			dma_addr = be64_to_cpu(direct64->dma_base);
829 			break;
830 		}
831 	}
832 	spin_unlock(&direct_window_list_lock);
833 
834 	return dma_addr;
835 }
836 
837 static int find_existing_ddw_windows(void)
838 {
839 	int len;
840 	struct device_node *pdn;
841 	struct direct_window *window;
842 	const struct dynamic_dma_window_prop *direct64;
843 
844 	if (!firmware_has_feature(FW_FEATURE_LPAR))
845 		return 0;
846 
847 	for_each_node_with_property(pdn, DIRECT64_PROPNAME) {
848 		direct64 = of_get_property(pdn, DIRECT64_PROPNAME, &len);
849 		if (!direct64)
850 			continue;
851 
852 		window = kzalloc(sizeof(*window), GFP_KERNEL);
853 		if (!window || len < sizeof(struct dynamic_dma_window_prop)) {
854 			kfree(window);
855 			remove_ddw(pdn, true);
856 			continue;
857 		}
858 
859 		window->device = pdn;
860 		window->prop = direct64;
861 		spin_lock(&direct_window_list_lock);
862 		list_add(&window->list, &direct_window_list);
863 		spin_unlock(&direct_window_list_lock);
864 	}
865 
866 	return 0;
867 }
868 machine_arch_initcall(pseries, find_existing_ddw_windows);
869 
870 static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
871 			struct ddw_query_response *query)
872 {
873 	struct device_node *dn;
874 	struct pci_dn *pdn;
875 	u32 cfg_addr;
876 	u64 buid;
877 	int ret;
878 
879 	/*
880 	 * Get the config address and phb buid of the PE window.
881 	 * Rely on eeh to retrieve this for us.
882 	 * Retrieve them from the pci device, not the node with the
883 	 * dma-window property
884 	 */
885 	dn = pci_device_to_OF_node(dev);
886 	pdn = PCI_DN(dn);
887 	buid = pdn->phb->buid;
888 	cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
889 
890 	ret = rtas_call(ddw_avail[0], 3, 5, (u32 *)query,
891 		  cfg_addr, BUID_HI(buid), BUID_LO(buid));
892 	dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x"
893 		" returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
894 		BUID_LO(buid), ret);
895 	return ret;
896 }
897 
898 static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
899 			struct ddw_create_response *create, int page_shift,
900 			int window_shift)
901 {
902 	struct device_node *dn;
903 	struct pci_dn *pdn;
904 	u32 cfg_addr;
905 	u64 buid;
906 	int ret;
907 
908 	/*
909 	 * Get the config address and phb buid of the PE window.
910 	 * Rely on eeh to retrieve this for us.
911 	 * Retrieve them from the pci device, not the node with the
912 	 * dma-window property
913 	 */
914 	dn = pci_device_to_OF_node(dev);
915 	pdn = PCI_DN(dn);
916 	buid = pdn->phb->buid;
917 	cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
918 
919 	do {
920 		/* extra outputs are LIOBN and dma-addr (hi, lo) */
921 		ret = rtas_call(ddw_avail[1], 5, 4, (u32 *)create,
922 				cfg_addr, BUID_HI(buid), BUID_LO(buid),
923 				page_shift, window_shift);
924 	} while (rtas_busy_delay(ret));
925 	dev_info(&dev->dev,
926 		"ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
927 		"(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
928 		 cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
929 		 window_shift, ret, create->liobn, create->addr_hi, create->addr_lo);
930 
931 	return ret;
932 }
933 
934 struct failed_ddw_pdn {
935 	struct device_node *pdn;
936 	struct list_head list;
937 };
938 
939 static LIST_HEAD(failed_ddw_pdn_list);
940 
941 static phys_addr_t ddw_memory_hotplug_max(void)
942 {
943 	phys_addr_t max_addr = memory_hotplug_max();
944 	struct device_node *memory;
945 
946 	for_each_node_by_type(memory, "memory") {
947 		unsigned long start, size;
948 		int n_mem_addr_cells, n_mem_size_cells, len;
949 		const __be32 *memcell_buf;
950 
951 		memcell_buf = of_get_property(memory, "reg", &len);
952 		if (!memcell_buf || len <= 0)
953 			continue;
954 
955 		n_mem_addr_cells = of_n_addr_cells(memory);
956 		n_mem_size_cells = of_n_size_cells(memory);
957 
958 		start = of_read_number(memcell_buf, n_mem_addr_cells);
959 		memcell_buf += n_mem_addr_cells;
960 		size = of_read_number(memcell_buf, n_mem_size_cells);
961 		memcell_buf += n_mem_size_cells;
962 
963 		max_addr = max_t(phys_addr_t, max_addr, start + size);
964 	}
965 
966 	return max_addr;
967 }
968 
969 /*
970  * If the PE supports dynamic dma windows, and there is space for a table
971  * that can map all pages in a linear offset, then setup such a table,
972  * and record the dma-offset in the struct device.
973  *
974  * dev: the pci device we are checking
975  * pdn: the parent pe node with the ibm,dma_window property
976  * Future: also check if we can remap the base window for our base page size
977  *
978  * returns the dma offset for use by the direct mapped DMA code.
979  */
980 static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
981 {
982 	int len, ret;
983 	struct ddw_query_response query;
984 	struct ddw_create_response create;
985 	int page_shift;
986 	u64 dma_addr, max_addr;
987 	struct device_node *dn;
988 	u32 ddw_avail[3];
989 	struct direct_window *window;
990 	struct property *win64;
991 	struct dynamic_dma_window_prop *ddwprop;
992 	struct failed_ddw_pdn *fpdn;
993 
994 	mutex_lock(&direct_window_init_mutex);
995 
996 	dma_addr = find_existing_ddw(pdn);
997 	if (dma_addr != 0)
998 		goto out_unlock;
999 
1000 	/*
1001 	 * If we already went through this for a previous function of
1002 	 * the same device and failed, we don't want to muck with the
1003 	 * DMA window again, as it will race with in-flight operations
1004 	 * and can lead to EEHs. The above mutex protects access to the
1005 	 * list.
1006 	 */
1007 	list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) {
1008 		if (fpdn->pdn == pdn)
1009 			goto out_unlock;
1010 	}
1011 
1012 	/*
1013 	 * the ibm,ddw-applicable property holds the tokens for:
1014 	 * ibm,query-pe-dma-window
1015 	 * ibm,create-pe-dma-window
1016 	 * ibm,remove-pe-dma-window
1017 	 * for the given node in that order.
1018 	 * the property is actually in the parent, not the PE
1019 	 */
1020 	ret = of_property_read_u32_array(pdn, "ibm,ddw-applicable",
1021 					 &ddw_avail[0], 3);
1022 	if (ret)
1023 		goto out_failed;
1024 
1025        /*
1026 	 * Query if there is a second window of size to map the
1027 	 * whole partition.  Query returns number of windows, largest
1028 	 * block assigned to PE (partition endpoint), and two bitmasks
1029 	 * of page sizes: supported and supported for migrate-dma.
1030 	 */
1031 	dn = pci_device_to_OF_node(dev);
1032 	ret = query_ddw(dev, ddw_avail, &query);
1033 	if (ret != 0)
1034 		goto out_failed;
1035 
1036 	if (query.windows_available == 0) {
1037 		/*
1038 		 * no additional windows are available for this device.
1039 		 * We might be able to reallocate the existing window,
1040 		 * trading in for a larger page size.
1041 		 */
1042 		dev_dbg(&dev->dev, "no free dynamic windows");
1043 		goto out_failed;
1044 	}
1045 	if (query.page_size & 4) {
1046 		page_shift = 24; /* 16MB */
1047 	} else if (query.page_size & 2) {
1048 		page_shift = 16; /* 64kB */
1049 	} else if (query.page_size & 1) {
1050 		page_shift = 12; /* 4kB */
1051 	} else {
1052 		dev_dbg(&dev->dev, "no supported direct page size in mask %x",
1053 			  query.page_size);
1054 		goto out_failed;
1055 	}
1056 	/* verify the window * number of ptes will map the partition */
1057 	/* check largest block * page size > max memory hotplug addr */
1058 	max_addr = ddw_memory_hotplug_max();
1059 	if (query.largest_available_block < (max_addr >> page_shift)) {
1060 		dev_dbg(&dev->dev, "can't map partition max 0x%llx with %u "
1061 			  "%llu-sized pages\n", max_addr,  query.largest_available_block,
1062 			  1ULL << page_shift);
1063 		goto out_failed;
1064 	}
1065 	len = order_base_2(max_addr);
1066 	win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
1067 	if (!win64) {
1068 		dev_info(&dev->dev,
1069 			"couldn't allocate property for 64bit dma window\n");
1070 		goto out_failed;
1071 	}
1072 	win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
1073 	win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
1074 	win64->length = sizeof(*ddwprop);
1075 	if (!win64->name || !win64->value) {
1076 		dev_info(&dev->dev,
1077 			"couldn't allocate property name and value\n");
1078 		goto out_free_prop;
1079 	}
1080 
1081 	ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
1082 	if (ret != 0)
1083 		goto out_free_prop;
1084 
1085 	ddwprop->liobn = cpu_to_be32(create.liobn);
1086 	ddwprop->dma_base = cpu_to_be64(((u64)create.addr_hi << 32) |
1087 			create.addr_lo);
1088 	ddwprop->tce_shift = cpu_to_be32(page_shift);
1089 	ddwprop->window_shift = cpu_to_be32(len);
1090 
1091 	dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %pOF\n",
1092 		  create.liobn, dn);
1093 
1094 	window = kzalloc(sizeof(*window), GFP_KERNEL);
1095 	if (!window)
1096 		goto out_clear_window;
1097 
1098 	ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
1099 			win64->value, tce_setrange_multi_pSeriesLP_walk);
1100 	if (ret) {
1101 		dev_info(&dev->dev, "failed to map direct window for %pOF: %d\n",
1102 			 dn, ret);
1103 		goto out_free_window;
1104 	}
1105 
1106 	ret = of_add_property(pdn, win64);
1107 	if (ret) {
1108 		dev_err(&dev->dev, "unable to add dma window property for %pOF: %d",
1109 			 pdn, ret);
1110 		goto out_free_window;
1111 	}
1112 
1113 	window->device = pdn;
1114 	window->prop = ddwprop;
1115 	spin_lock(&direct_window_list_lock);
1116 	list_add(&window->list, &direct_window_list);
1117 	spin_unlock(&direct_window_list_lock);
1118 
1119 	dma_addr = be64_to_cpu(ddwprop->dma_base);
1120 	goto out_unlock;
1121 
1122 out_free_window:
1123 	kfree(window);
1124 
1125 out_clear_window:
1126 	remove_ddw(pdn, true);
1127 
1128 out_free_prop:
1129 	kfree(win64->name);
1130 	kfree(win64->value);
1131 	kfree(win64);
1132 
1133 out_failed:
1134 
1135 	fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
1136 	if (!fpdn)
1137 		goto out_unlock;
1138 	fpdn->pdn = pdn;
1139 	list_add(&fpdn->list, &failed_ddw_pdn_list);
1140 
1141 out_unlock:
1142 	mutex_unlock(&direct_window_init_mutex);
1143 	return dma_addr;
1144 }
1145 
1146 static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
1147 {
1148 	struct device_node *pdn, *dn;
1149 	struct iommu_table *tbl;
1150 	const __be32 *dma_window = NULL;
1151 	struct pci_dn *pci;
1152 
1153 	pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
1154 
1155 	/* dev setup for LPAR is a little tricky, since the device tree might
1156 	 * contain the dma-window properties per-device and not necessarily
1157 	 * for the bus. So we need to search upwards in the tree until we
1158 	 * either hit a dma-window property, OR find a parent with a table
1159 	 * already allocated.
1160 	 */
1161 	dn = pci_device_to_OF_node(dev);
1162 	pr_debug("  node is %pOF\n", dn);
1163 
1164 	for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1165 	     pdn = pdn->parent) {
1166 		dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1167 		if (dma_window)
1168 			break;
1169 	}
1170 
1171 	if (!pdn || !PCI_DN(pdn)) {
1172 		printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
1173 		       "no DMA window found for pci dev=%s dn=%pOF\n",
1174 				 pci_name(dev), dn);
1175 		return;
1176 	}
1177 	pr_debug("  parent is %pOF\n", pdn);
1178 
1179 	pci = PCI_DN(pdn);
1180 	if (!pci->table_group) {
1181 		pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
1182 		tbl = pci->table_group->tables[0];
1183 		iommu_table_setparms_lpar(pci->phb, pdn, tbl,
1184 				pci->table_group, dma_window);
1185 		tbl->it_ops = &iommu_table_lpar_multi_ops;
1186 		iommu_init_table(tbl, pci->phb->node);
1187 		iommu_register_group(pci->table_group,
1188 				pci_domain_nr(pci->phb->bus), 0);
1189 		pr_debug("  created table: %p\n", pci->table_group);
1190 	} else {
1191 		pr_debug("  found DMA window, table: %p\n", pci->table_group);
1192 	}
1193 
1194 	set_iommu_table_base(&dev->dev, pci->table_group->tables[0]);
1195 	iommu_add_device(pci->table_group, &dev->dev);
1196 }
1197 
1198 static bool iommu_bypass_supported_pSeriesLP(struct pci_dev *pdev, u64 dma_mask)
1199 {
1200 	struct device_node *dn = pci_device_to_OF_node(pdev), *pdn;
1201 	const __be32 *dma_window = NULL;
1202 
1203 	/* only attempt to use a new window if 64-bit DMA is requested */
1204 	if (dma_mask < DMA_BIT_MASK(64))
1205 		return false;
1206 
1207 	dev_dbg(&pdev->dev, "node is %pOF\n", dn);
1208 
1209 	/*
1210 	 * the device tree might contain the dma-window properties
1211 	 * per-device and not necessarily for the bus. So we need to
1212 	 * search upwards in the tree until we either hit a dma-window
1213 	 * property, OR find a parent with a table already allocated.
1214 	 */
1215 	for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1216 			pdn = pdn->parent) {
1217 		dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1218 		if (dma_window)
1219 			break;
1220 	}
1221 
1222 	if (pdn && PCI_DN(pdn)) {
1223 		pdev->dev.archdata.dma_offset = enable_ddw(pdev, pdn);
1224 		if (pdev->dev.archdata.dma_offset)
1225 			return true;
1226 	}
1227 
1228 	return false;
1229 }
1230 
1231 static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
1232 		void *data)
1233 {
1234 	struct direct_window *window;
1235 	struct memory_notify *arg = data;
1236 	int ret = 0;
1237 
1238 	switch (action) {
1239 	case MEM_GOING_ONLINE:
1240 		spin_lock(&direct_window_list_lock);
1241 		list_for_each_entry(window, &direct_window_list, list) {
1242 			ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
1243 					arg->nr_pages, window->prop);
1244 			/* XXX log error */
1245 		}
1246 		spin_unlock(&direct_window_list_lock);
1247 		break;
1248 	case MEM_CANCEL_ONLINE:
1249 	case MEM_OFFLINE:
1250 		spin_lock(&direct_window_list_lock);
1251 		list_for_each_entry(window, &direct_window_list, list) {
1252 			ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
1253 					arg->nr_pages, window->prop);
1254 			/* XXX log error */
1255 		}
1256 		spin_unlock(&direct_window_list_lock);
1257 		break;
1258 	default:
1259 		break;
1260 	}
1261 	if (ret && action != MEM_CANCEL_ONLINE)
1262 		return NOTIFY_BAD;
1263 
1264 	return NOTIFY_OK;
1265 }
1266 
1267 static struct notifier_block iommu_mem_nb = {
1268 	.notifier_call = iommu_mem_notifier,
1269 };
1270 
1271 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
1272 {
1273 	int err = NOTIFY_OK;
1274 	struct of_reconfig_data *rd = data;
1275 	struct device_node *np = rd->dn;
1276 	struct pci_dn *pci = PCI_DN(np);
1277 	struct direct_window *window;
1278 
1279 	switch (action) {
1280 	case OF_RECONFIG_DETACH_NODE:
1281 		/*
1282 		 * Removing the property will invoke the reconfig
1283 		 * notifier again, which causes dead-lock on the
1284 		 * read-write semaphore of the notifier chain. So
1285 		 * we have to remove the property when releasing
1286 		 * the device node.
1287 		 */
1288 		remove_ddw(np, false);
1289 		if (pci && pci->table_group)
1290 			iommu_pseries_free_group(pci->table_group,
1291 					np->full_name);
1292 
1293 		spin_lock(&direct_window_list_lock);
1294 		list_for_each_entry(window, &direct_window_list, list) {
1295 			if (window->device == np) {
1296 				list_del(&window->list);
1297 				kfree(window);
1298 				break;
1299 			}
1300 		}
1301 		spin_unlock(&direct_window_list_lock);
1302 		break;
1303 	default:
1304 		err = NOTIFY_DONE;
1305 		break;
1306 	}
1307 	return err;
1308 }
1309 
1310 static struct notifier_block iommu_reconfig_nb = {
1311 	.notifier_call = iommu_reconfig_notifier,
1312 };
1313 
1314 /* These are called very early. */
1315 void iommu_init_early_pSeries(void)
1316 {
1317 	if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
1318 		return;
1319 
1320 	if (firmware_has_feature(FW_FEATURE_LPAR)) {
1321 		pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
1322 		pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
1323 		if (!disable_ddw)
1324 			pseries_pci_controller_ops.iommu_bypass_supported =
1325 				iommu_bypass_supported_pSeriesLP;
1326 	} else {
1327 		pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeries;
1328 		pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeries;
1329 	}
1330 
1331 
1332 	of_reconfig_notifier_register(&iommu_reconfig_nb);
1333 	register_memory_notifier(&iommu_mem_nb);
1334 
1335 	set_pci_dma_ops(&dma_iommu_ops);
1336 }
1337 
1338 static int __init disable_multitce(char *str)
1339 {
1340 	if (strcmp(str, "off") == 0 &&
1341 	    firmware_has_feature(FW_FEATURE_LPAR) &&
1342 	    firmware_has_feature(FW_FEATURE_MULTITCE)) {
1343 		printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
1344 		powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
1345 	}
1346 	return 1;
1347 }
1348 
1349 __setup("multitce=", disable_multitce);
1350 
1351 static int tce_iommu_bus_notifier(struct notifier_block *nb,
1352 		unsigned long action, void *data)
1353 {
1354 	struct device *dev = data;
1355 
1356 	switch (action) {
1357 	case BUS_NOTIFY_DEL_DEVICE:
1358 		iommu_del_device(dev);
1359 		return 0;
1360 	default:
1361 		return 0;
1362 	}
1363 }
1364 
1365 static struct notifier_block tce_iommu_bus_nb = {
1366 	.notifier_call = tce_iommu_bus_notifier,
1367 };
1368 
1369 static int __init tce_iommu_bus_notifier_init(void)
1370 {
1371 	bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb);
1372 	return 0;
1373 }
1374 machine_subsys_initcall_sync(pseries, tce_iommu_bus_notifier_init);
1375