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