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, *tces;
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 	tces = 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, *tces;
133 
134 	tces = 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 ranges, 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 		/* ranges in cell */
959 		ranges = (len >> 2) / (n_mem_addr_cells + n_mem_size_cells);
960 
961 		start = of_read_number(memcell_buf, n_mem_addr_cells);
962 		memcell_buf += n_mem_addr_cells;
963 		size = of_read_number(memcell_buf, n_mem_size_cells);
964 		memcell_buf += n_mem_size_cells;
965 
966 		max_addr = max_t(phys_addr_t, max_addr, start + size);
967 	}
968 
969 	return max_addr;
970 }
971 
972 /*
973  * If the PE supports dynamic dma windows, and there is space for a table
974  * that can map all pages in a linear offset, then setup such a table,
975  * and record the dma-offset in the struct device.
976  *
977  * dev: the pci device we are checking
978  * pdn: the parent pe node with the ibm,dma_window property
979  * Future: also check if we can remap the base window for our base page size
980  *
981  * returns the dma offset for use by the direct mapped DMA code.
982  */
983 static u64 enable_ddw(struct pci_dev *dev, struct device_node *pdn)
984 {
985 	int len, ret;
986 	struct ddw_query_response query;
987 	struct ddw_create_response create;
988 	int page_shift;
989 	u64 dma_addr, max_addr;
990 	struct device_node *dn;
991 	u32 ddw_avail[3];
992 	struct direct_window *window;
993 	struct property *win64;
994 	struct dynamic_dma_window_prop *ddwprop;
995 	struct failed_ddw_pdn *fpdn;
996 
997 	mutex_lock(&direct_window_init_mutex);
998 
999 	dma_addr = find_existing_ddw(pdn);
1000 	if (dma_addr != 0)
1001 		goto out_unlock;
1002 
1003 	/*
1004 	 * If we already went through this for a previous function of
1005 	 * the same device and failed, we don't want to muck with the
1006 	 * DMA window again, as it will race with in-flight operations
1007 	 * and can lead to EEHs. The above mutex protects access to the
1008 	 * list.
1009 	 */
1010 	list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) {
1011 		if (fpdn->pdn == pdn)
1012 			goto out_unlock;
1013 	}
1014 
1015 	/*
1016 	 * the ibm,ddw-applicable property holds the tokens for:
1017 	 * ibm,query-pe-dma-window
1018 	 * ibm,create-pe-dma-window
1019 	 * ibm,remove-pe-dma-window
1020 	 * for the given node in that order.
1021 	 * the property is actually in the parent, not the PE
1022 	 */
1023 	ret = of_property_read_u32_array(pdn, "ibm,ddw-applicable",
1024 					 &ddw_avail[0], 3);
1025 	if (ret)
1026 		goto out_failed;
1027 
1028        /*
1029 	 * Query if there is a second window of size to map the
1030 	 * whole partition.  Query returns number of windows, largest
1031 	 * block assigned to PE (partition endpoint), and two bitmasks
1032 	 * of page sizes: supported and supported for migrate-dma.
1033 	 */
1034 	dn = pci_device_to_OF_node(dev);
1035 	ret = query_ddw(dev, ddw_avail, &query);
1036 	if (ret != 0)
1037 		goto out_failed;
1038 
1039 	if (query.windows_available == 0) {
1040 		/*
1041 		 * no additional windows are available for this device.
1042 		 * We might be able to reallocate the existing window,
1043 		 * trading in for a larger page size.
1044 		 */
1045 		dev_dbg(&dev->dev, "no free dynamic windows");
1046 		goto out_failed;
1047 	}
1048 	if (query.page_size & 4) {
1049 		page_shift = 24; /* 16MB */
1050 	} else if (query.page_size & 2) {
1051 		page_shift = 16; /* 64kB */
1052 	} else if (query.page_size & 1) {
1053 		page_shift = 12; /* 4kB */
1054 	} else {
1055 		dev_dbg(&dev->dev, "no supported direct page size in mask %x",
1056 			  query.page_size);
1057 		goto out_failed;
1058 	}
1059 	/* verify the window * number of ptes will map the partition */
1060 	/* check largest block * page size > max memory hotplug addr */
1061 	max_addr = ddw_memory_hotplug_max();
1062 	if (query.largest_available_block < (max_addr >> page_shift)) {
1063 		dev_dbg(&dev->dev, "can't map partition max 0x%llx with %u "
1064 			  "%llu-sized pages\n", max_addr,  query.largest_available_block,
1065 			  1ULL << page_shift);
1066 		goto out_failed;
1067 	}
1068 	len = order_base_2(max_addr);
1069 	win64 = kzalloc(sizeof(struct property), GFP_KERNEL);
1070 	if (!win64) {
1071 		dev_info(&dev->dev,
1072 			"couldn't allocate property for 64bit dma window\n");
1073 		goto out_failed;
1074 	}
1075 	win64->name = kstrdup(DIRECT64_PROPNAME, GFP_KERNEL);
1076 	win64->value = ddwprop = kmalloc(sizeof(*ddwprop), GFP_KERNEL);
1077 	win64->length = sizeof(*ddwprop);
1078 	if (!win64->name || !win64->value) {
1079 		dev_info(&dev->dev,
1080 			"couldn't allocate property name and value\n");
1081 		goto out_free_prop;
1082 	}
1083 
1084 	ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
1085 	if (ret != 0)
1086 		goto out_free_prop;
1087 
1088 	ddwprop->liobn = cpu_to_be32(create.liobn);
1089 	ddwprop->dma_base = cpu_to_be64(((u64)create.addr_hi << 32) |
1090 			create.addr_lo);
1091 	ddwprop->tce_shift = cpu_to_be32(page_shift);
1092 	ddwprop->window_shift = cpu_to_be32(len);
1093 
1094 	dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %pOF\n",
1095 		  create.liobn, dn);
1096 
1097 	window = kzalloc(sizeof(*window), GFP_KERNEL);
1098 	if (!window)
1099 		goto out_clear_window;
1100 
1101 	ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
1102 			win64->value, tce_setrange_multi_pSeriesLP_walk);
1103 	if (ret) {
1104 		dev_info(&dev->dev, "failed to map direct window for %pOF: %d\n",
1105 			 dn, ret);
1106 		goto out_free_window;
1107 	}
1108 
1109 	ret = of_add_property(pdn, win64);
1110 	if (ret) {
1111 		dev_err(&dev->dev, "unable to add dma window property for %pOF: %d",
1112 			 pdn, ret);
1113 		goto out_free_window;
1114 	}
1115 
1116 	window->device = pdn;
1117 	window->prop = ddwprop;
1118 	spin_lock(&direct_window_list_lock);
1119 	list_add(&window->list, &direct_window_list);
1120 	spin_unlock(&direct_window_list_lock);
1121 
1122 	dma_addr = be64_to_cpu(ddwprop->dma_base);
1123 	goto out_unlock;
1124 
1125 out_free_window:
1126 	kfree(window);
1127 
1128 out_clear_window:
1129 	remove_ddw(pdn, true);
1130 
1131 out_free_prop:
1132 	kfree(win64->name);
1133 	kfree(win64->value);
1134 	kfree(win64);
1135 
1136 out_failed:
1137 
1138 	fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
1139 	if (!fpdn)
1140 		goto out_unlock;
1141 	fpdn->pdn = pdn;
1142 	list_add(&fpdn->list, &failed_ddw_pdn_list);
1143 
1144 out_unlock:
1145 	mutex_unlock(&direct_window_init_mutex);
1146 	return dma_addr;
1147 }
1148 
1149 static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
1150 {
1151 	struct device_node *pdn, *dn;
1152 	struct iommu_table *tbl;
1153 	const __be32 *dma_window = NULL;
1154 	struct pci_dn *pci;
1155 
1156 	pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
1157 
1158 	/* dev setup for LPAR is a little tricky, since the device tree might
1159 	 * contain the dma-window properties per-device and not necessarily
1160 	 * for the bus. So we need to search upwards in the tree until we
1161 	 * either hit a dma-window property, OR find a parent with a table
1162 	 * already allocated.
1163 	 */
1164 	dn = pci_device_to_OF_node(dev);
1165 	pr_debug("  node is %pOF\n", dn);
1166 
1167 	for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1168 	     pdn = pdn->parent) {
1169 		dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1170 		if (dma_window)
1171 			break;
1172 	}
1173 
1174 	if (!pdn || !PCI_DN(pdn)) {
1175 		printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
1176 		       "no DMA window found for pci dev=%s dn=%pOF\n",
1177 				 pci_name(dev), dn);
1178 		return;
1179 	}
1180 	pr_debug("  parent is %pOF\n", pdn);
1181 
1182 	pci = PCI_DN(pdn);
1183 	if (!pci->table_group) {
1184 		pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
1185 		tbl = pci->table_group->tables[0];
1186 		iommu_table_setparms_lpar(pci->phb, pdn, tbl,
1187 				pci->table_group, dma_window);
1188 		tbl->it_ops = &iommu_table_lpar_multi_ops;
1189 		iommu_init_table(tbl, pci->phb->node);
1190 		iommu_register_group(pci->table_group,
1191 				pci_domain_nr(pci->phb->bus), 0);
1192 		pr_debug("  created table: %p\n", pci->table_group);
1193 	} else {
1194 		pr_debug("  found DMA window, table: %p\n", pci->table_group);
1195 	}
1196 
1197 	set_iommu_table_base(&dev->dev, pci->table_group->tables[0]);
1198 	iommu_add_device(pci->table_group, &dev->dev);
1199 }
1200 
1201 static bool iommu_bypass_supported_pSeriesLP(struct pci_dev *pdev, u64 dma_mask)
1202 {
1203 	struct device_node *dn = pci_device_to_OF_node(pdev), *pdn;
1204 	const __be32 *dma_window = NULL;
1205 
1206 	/* only attempt to use a new window if 64-bit DMA is requested */
1207 	if (dma_mask < DMA_BIT_MASK(64))
1208 		return false;
1209 
1210 	dev_dbg(&pdev->dev, "node is %pOF\n", dn);
1211 
1212 	/*
1213 	 * the device tree might contain the dma-window properties
1214 	 * per-device and not necessarily for the bus. So we need to
1215 	 * search upwards in the tree until we either hit a dma-window
1216 	 * property, OR find a parent with a table already allocated.
1217 	 */
1218 	for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1219 			pdn = pdn->parent) {
1220 		dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1221 		if (dma_window)
1222 			break;
1223 	}
1224 
1225 	if (pdn && PCI_DN(pdn)) {
1226 		pdev->dev.archdata.dma_offset = enable_ddw(pdev, pdn);
1227 		if (pdev->dev.archdata.dma_offset)
1228 			return true;
1229 	}
1230 
1231 	return false;
1232 }
1233 
1234 static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
1235 		void *data)
1236 {
1237 	struct direct_window *window;
1238 	struct memory_notify *arg = data;
1239 	int ret = 0;
1240 
1241 	switch (action) {
1242 	case MEM_GOING_ONLINE:
1243 		spin_lock(&direct_window_list_lock);
1244 		list_for_each_entry(window, &direct_window_list, list) {
1245 			ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
1246 					arg->nr_pages, window->prop);
1247 			/* XXX log error */
1248 		}
1249 		spin_unlock(&direct_window_list_lock);
1250 		break;
1251 	case MEM_CANCEL_ONLINE:
1252 	case MEM_OFFLINE:
1253 		spin_lock(&direct_window_list_lock);
1254 		list_for_each_entry(window, &direct_window_list, list) {
1255 			ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
1256 					arg->nr_pages, window->prop);
1257 			/* XXX log error */
1258 		}
1259 		spin_unlock(&direct_window_list_lock);
1260 		break;
1261 	default:
1262 		break;
1263 	}
1264 	if (ret && action != MEM_CANCEL_ONLINE)
1265 		return NOTIFY_BAD;
1266 
1267 	return NOTIFY_OK;
1268 }
1269 
1270 static struct notifier_block iommu_mem_nb = {
1271 	.notifier_call = iommu_mem_notifier,
1272 };
1273 
1274 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
1275 {
1276 	int err = NOTIFY_OK;
1277 	struct of_reconfig_data *rd = data;
1278 	struct device_node *np = rd->dn;
1279 	struct pci_dn *pci = PCI_DN(np);
1280 	struct direct_window *window;
1281 
1282 	switch (action) {
1283 	case OF_RECONFIG_DETACH_NODE:
1284 		/*
1285 		 * Removing the property will invoke the reconfig
1286 		 * notifier again, which causes dead-lock on the
1287 		 * read-write semaphore of the notifier chain. So
1288 		 * we have to remove the property when releasing
1289 		 * the device node.
1290 		 */
1291 		remove_ddw(np, false);
1292 		if (pci && pci->table_group)
1293 			iommu_pseries_free_group(pci->table_group,
1294 					np->full_name);
1295 
1296 		spin_lock(&direct_window_list_lock);
1297 		list_for_each_entry(window, &direct_window_list, list) {
1298 			if (window->device == np) {
1299 				list_del(&window->list);
1300 				kfree(window);
1301 				break;
1302 			}
1303 		}
1304 		spin_unlock(&direct_window_list_lock);
1305 		break;
1306 	default:
1307 		err = NOTIFY_DONE;
1308 		break;
1309 	}
1310 	return err;
1311 }
1312 
1313 static struct notifier_block iommu_reconfig_nb = {
1314 	.notifier_call = iommu_reconfig_notifier,
1315 };
1316 
1317 /* These are called very early. */
1318 void iommu_init_early_pSeries(void)
1319 {
1320 	if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
1321 		return;
1322 
1323 	if (firmware_has_feature(FW_FEATURE_LPAR)) {
1324 		pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
1325 		pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
1326 		if (!disable_ddw)
1327 			pseries_pci_controller_ops.iommu_bypass_supported =
1328 				iommu_bypass_supported_pSeriesLP;
1329 	} else {
1330 		pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeries;
1331 		pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeries;
1332 	}
1333 
1334 
1335 	of_reconfig_notifier_register(&iommu_reconfig_nb);
1336 	register_memory_notifier(&iommu_mem_nb);
1337 
1338 	set_pci_dma_ops(&dma_iommu_ops);
1339 }
1340 
1341 static int __init disable_multitce(char *str)
1342 {
1343 	if (strcmp(str, "off") == 0 &&
1344 	    firmware_has_feature(FW_FEATURE_LPAR) &&
1345 	    firmware_has_feature(FW_FEATURE_MULTITCE)) {
1346 		printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
1347 		powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
1348 	}
1349 	return 1;
1350 }
1351 
1352 __setup("multitce=", disable_multitce);
1353 
1354 static int tce_iommu_bus_notifier(struct notifier_block *nb,
1355 		unsigned long action, void *data)
1356 {
1357 	struct device *dev = data;
1358 
1359 	switch (action) {
1360 	case BUS_NOTIFY_DEL_DEVICE:
1361 		iommu_del_device(dev);
1362 		return 0;
1363 	default:
1364 		return 0;
1365 	}
1366 }
1367 
1368 static struct notifier_block tce_iommu_bus_nb = {
1369 	.notifier_call = tce_iommu_bus_notifier,
1370 };
1371 
1372 static int __init tce_iommu_bus_notifier_init(void)
1373 {
1374 	bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb);
1375 	return 0;
1376 }
1377 machine_subsys_initcall_sync(pseries, tce_iommu_bus_notifier_init);
1378