• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 enum {
43 	DDW_QUERY_PE_DMA_WIN  = 0,
44 	DDW_CREATE_PE_DMA_WIN = 1,
45 	DDW_REMOVE_PE_DMA_WIN = 2,
46 
47 	DDW_APPLICABLE_SIZE
48 };
49 
50 enum {
51 	DDW_EXT_SIZE = 0,
52 	DDW_EXT_RESET_DMA_WIN = 1,
53 	DDW_EXT_QUERY_OUT_SIZE = 2
54 };
55 
iommu_pseries_alloc_table(int node)56 static struct iommu_table *iommu_pseries_alloc_table(int node)
57 {
58 	struct iommu_table *tbl;
59 
60 	tbl = kzalloc_node(sizeof(struct iommu_table), GFP_KERNEL, node);
61 	if (!tbl)
62 		return NULL;
63 
64 	INIT_LIST_HEAD_RCU(&tbl->it_group_list);
65 	kref_init(&tbl->it_kref);
66 	return tbl;
67 }
68 
iommu_pseries_alloc_group(int node)69 static struct iommu_table_group *iommu_pseries_alloc_group(int node)
70 {
71 	struct iommu_table_group *table_group;
72 
73 	table_group = kzalloc_node(sizeof(*table_group), GFP_KERNEL, node);
74 	if (!table_group)
75 		return NULL;
76 
77 	table_group->tables[0] = iommu_pseries_alloc_table(node);
78 	if (table_group->tables[0])
79 		return table_group;
80 
81 	kfree(table_group);
82 	return NULL;
83 }
84 
iommu_pseries_free_group(struct iommu_table_group * table_group,const char * node_name)85 static void iommu_pseries_free_group(struct iommu_table_group *table_group,
86 		const char *node_name)
87 {
88 	if (!table_group)
89 		return;
90 
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 
98 	/* Default DMA window table is at index 0, while DDW at 1. SR-IOV
99 	 * adapters only have table on index 1.
100 	 */
101 	if (table_group->tables[0])
102 		iommu_tce_table_put(table_group->tables[0]);
103 
104 	if (table_group->tables[1])
105 		iommu_tce_table_put(table_group->tables[1]);
106 
107 	kfree(table_group);
108 }
109 
tce_build_pSeries(struct iommu_table * tbl,long index,long npages,unsigned long uaddr,enum dma_data_direction direction,unsigned long attrs)110 static int tce_build_pSeries(struct iommu_table *tbl, long index,
111 			      long npages, unsigned long uaddr,
112 			      enum dma_data_direction direction,
113 			      unsigned long attrs)
114 {
115 	u64 proto_tce;
116 	__be64 *tcep;
117 	u64 rpn;
118 	const unsigned long tceshift = tbl->it_page_shift;
119 	const unsigned long pagesize = IOMMU_PAGE_SIZE(tbl);
120 
121 	proto_tce = TCE_PCI_READ; // Read allowed
122 
123 	if (direction != DMA_TO_DEVICE)
124 		proto_tce |= TCE_PCI_WRITE;
125 
126 	tcep = ((__be64 *)tbl->it_base) + index;
127 
128 	while (npages--) {
129 		/* can't move this out since we might cross MEMBLOCK boundary */
130 		rpn = __pa(uaddr) >> tceshift;
131 		*tcep = cpu_to_be64(proto_tce | rpn << tceshift);
132 
133 		uaddr += pagesize;
134 		tcep++;
135 	}
136 	return 0;
137 }
138 
139 
tce_free_pSeries(struct iommu_table * tbl,long index,long npages)140 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
141 {
142 	__be64 *tcep;
143 
144 	tcep = ((__be64 *)tbl->it_base) + index;
145 
146 	while (npages--)
147 		*(tcep++) = 0;
148 }
149 
tce_get_pseries(struct iommu_table * tbl,long index)150 static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
151 {
152 	__be64 *tcep;
153 
154 	tcep = ((__be64 *)tbl->it_base) + index;
155 
156 	return be64_to_cpu(*tcep);
157 }
158 
159 static void tce_free_pSeriesLP(unsigned long liobn, long, long, long);
160 static void tce_freemulti_pSeriesLP(struct iommu_table*, long, long);
161 
tce_build_pSeriesLP(unsigned long liobn,long tcenum,long tceshift,long npages,unsigned long uaddr,enum dma_data_direction direction,unsigned long attrs)162 static int tce_build_pSeriesLP(unsigned long liobn, long tcenum, long tceshift,
163 				long npages, unsigned long uaddr,
164 				enum dma_data_direction direction,
165 				unsigned long attrs)
166 {
167 	u64 rc = 0;
168 	u64 proto_tce, tce;
169 	u64 rpn;
170 	int ret = 0;
171 	long tcenum_start = tcenum, npages_start = npages;
172 
173 	rpn = __pa(uaddr) >> tceshift;
174 	proto_tce = TCE_PCI_READ;
175 	if (direction != DMA_TO_DEVICE)
176 		proto_tce |= TCE_PCI_WRITE;
177 
178 	while (npages--) {
179 		tce = proto_tce | rpn << tceshift;
180 		rc = plpar_tce_put((u64)liobn, (u64)tcenum << tceshift, tce);
181 
182 		if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
183 			ret = (int)rc;
184 			tce_free_pSeriesLP(liobn, tcenum_start, tceshift,
185 			                   (npages_start - (npages + 1)));
186 			break;
187 		}
188 
189 		if (rc && printk_ratelimit()) {
190 			printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
191 			printk("\tindex   = 0x%llx\n", (u64)liobn);
192 			printk("\ttcenum  = 0x%llx\n", (u64)tcenum);
193 			printk("\ttce val = 0x%llx\n", tce );
194 			dump_stack();
195 		}
196 
197 		tcenum++;
198 		rpn++;
199 	}
200 	return ret;
201 }
202 
203 static DEFINE_PER_CPU(__be64 *, tce_page);
204 
tce_buildmulti_pSeriesLP(struct iommu_table * tbl,long tcenum,long npages,unsigned long uaddr,enum dma_data_direction direction,unsigned long attrs)205 static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
206 				     long npages, unsigned long uaddr,
207 				     enum dma_data_direction direction,
208 				     unsigned long attrs)
209 {
210 	u64 rc = 0;
211 	u64 proto_tce;
212 	__be64 *tcep;
213 	u64 rpn;
214 	long l, limit;
215 	long tcenum_start = tcenum, npages_start = npages;
216 	int ret = 0;
217 	unsigned long flags;
218 	const unsigned long tceshift = tbl->it_page_shift;
219 
220 	if ((npages == 1) || !firmware_has_feature(FW_FEATURE_PUT_TCE_IND)) {
221 		return tce_build_pSeriesLP(tbl->it_index, tcenum,
222 					   tceshift, npages, uaddr,
223 		                           direction, attrs);
224 	}
225 
226 	local_irq_save(flags);	/* to protect tcep and the page behind it */
227 
228 	tcep = __this_cpu_read(tce_page);
229 
230 	/* This is safe to do since interrupts are off when we're called
231 	 * from iommu_alloc{,_sg}()
232 	 */
233 	if (!tcep) {
234 		tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
235 		/* If allocation fails, fall back to the loop implementation */
236 		if (!tcep) {
237 			local_irq_restore(flags);
238 			return tce_build_pSeriesLP(tbl->it_index, tcenum,
239 					tceshift,
240 					npages, uaddr, direction, attrs);
241 		}
242 		__this_cpu_write(tce_page, tcep);
243 	}
244 
245 	rpn = __pa(uaddr) >> tceshift;
246 	proto_tce = TCE_PCI_READ;
247 	if (direction != DMA_TO_DEVICE)
248 		proto_tce |= TCE_PCI_WRITE;
249 
250 	/* We can map max one pageful of TCEs at a time */
251 	do {
252 		/*
253 		 * Set up the page with TCE data, looping through and setting
254 		 * the values.
255 		 */
256 		limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
257 
258 		for (l = 0; l < limit; l++) {
259 			tcep[l] = cpu_to_be64(proto_tce | rpn << tceshift);
260 			rpn++;
261 		}
262 
263 		rc = plpar_tce_put_indirect((u64)tbl->it_index,
264 					    (u64)tcenum << tceshift,
265 					    (u64)__pa(tcep),
266 					    limit);
267 
268 		npages -= limit;
269 		tcenum += limit;
270 	} while (npages > 0 && !rc);
271 
272 	local_irq_restore(flags);
273 
274 	if (unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
275 		ret = (int)rc;
276 		tce_freemulti_pSeriesLP(tbl, tcenum_start,
277 		                        (npages_start - (npages + limit)));
278 		return ret;
279 	}
280 
281 	if (rc && printk_ratelimit()) {
282 		printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
283 		printk("\tindex   = 0x%llx\n", (u64)tbl->it_index);
284 		printk("\tnpages  = 0x%llx\n", (u64)npages);
285 		printk("\ttce[0] val = 0x%llx\n", tcep[0]);
286 		dump_stack();
287 	}
288 	return ret;
289 }
290 
tce_free_pSeriesLP(unsigned long liobn,long tcenum,long tceshift,long npages)291 static void tce_free_pSeriesLP(unsigned long liobn, long tcenum, long tceshift,
292 			       long npages)
293 {
294 	u64 rc;
295 
296 	while (npages--) {
297 		rc = plpar_tce_put((u64)liobn, (u64)tcenum << tceshift, 0);
298 
299 		if (rc && printk_ratelimit()) {
300 			printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
301 			printk("\tindex   = 0x%llx\n", (u64)liobn);
302 			printk("\ttcenum  = 0x%llx\n", (u64)tcenum);
303 			dump_stack();
304 		}
305 
306 		tcenum++;
307 	}
308 }
309 
310 
tce_freemulti_pSeriesLP(struct iommu_table * tbl,long tcenum,long npages)311 static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
312 {
313 	u64 rc;
314 	long rpages = npages;
315 	unsigned long limit;
316 
317 	if (!firmware_has_feature(FW_FEATURE_STUFF_TCE))
318 		return tce_free_pSeriesLP(tbl->it_index, tcenum,
319 					  tbl->it_page_shift, npages);
320 
321 	do {
322 		limit = min_t(unsigned long, rpages, 512);
323 
324 		rc = plpar_tce_stuff((u64)tbl->it_index,
325 				     (u64)tcenum << tbl->it_page_shift, 0, limit);
326 
327 		rpages -= limit;
328 		tcenum += limit;
329 	} while (rpages > 0 && !rc);
330 
331 	if (rc && printk_ratelimit()) {
332 		printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
333 		printk("\trc      = %lld\n", rc);
334 		printk("\tindex   = 0x%llx\n", (u64)tbl->it_index);
335 		printk("\tnpages  = 0x%llx\n", (u64)npages);
336 		dump_stack();
337 	}
338 }
339 
tce_get_pSeriesLP(struct iommu_table * tbl,long tcenum)340 static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
341 {
342 	u64 rc;
343 	unsigned long tce_ret;
344 
345 	rc = plpar_tce_get((u64)tbl->it_index,
346 			   (u64)tcenum << tbl->it_page_shift, &tce_ret);
347 
348 	if (rc && printk_ratelimit()) {
349 		printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
350 		printk("\tindex   = 0x%llx\n", (u64)tbl->it_index);
351 		printk("\ttcenum  = 0x%llx\n", (u64)tcenum);
352 		dump_stack();
353 	}
354 
355 	return tce_ret;
356 }
357 
358 /* this is compatible with cells for the device tree property */
359 struct dynamic_dma_window_prop {
360 	__be32	liobn;		/* tce table number */
361 	__be64	dma_base;	/* address hi,lo */
362 	__be32	tce_shift;	/* ilog2(tce_page_size) */
363 	__be32	window_shift;	/* ilog2(tce_window_size) */
364 };
365 
366 struct dma_win {
367 	struct device_node *device;
368 	const struct dynamic_dma_window_prop *prop;
369 	struct list_head list;
370 };
371 
372 /* Dynamic DMA Window support */
373 struct ddw_query_response {
374 	u32 windows_available;
375 	u64 largest_available_block;
376 	u32 page_size;
377 	u32 migration_capable;
378 };
379 
380 struct ddw_create_response {
381 	u32 liobn;
382 	u32 addr_hi;
383 	u32 addr_lo;
384 };
385 
386 static LIST_HEAD(dma_win_list);
387 /* prevents races between memory on/offline and window creation */
388 static DEFINE_SPINLOCK(dma_win_list_lock);
389 /* protects initializing window twice for same device */
390 static DEFINE_MUTEX(dma_win_init_mutex);
391 #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
392 #define DMA64_PROPNAME "linux,dma64-ddr-window-info"
393 
tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,unsigned long num_pfn,const void * arg)394 static int tce_clearrange_multi_pSeriesLP(unsigned long start_pfn,
395 					unsigned long num_pfn, const void *arg)
396 {
397 	const struct dynamic_dma_window_prop *maprange = arg;
398 	int rc;
399 	u64 tce_size, num_tce, dma_offset, next;
400 	u32 tce_shift;
401 	long limit;
402 
403 	tce_shift = be32_to_cpu(maprange->tce_shift);
404 	tce_size = 1ULL << tce_shift;
405 	next = start_pfn << PAGE_SHIFT;
406 	num_tce = num_pfn << PAGE_SHIFT;
407 
408 	/* round back to the beginning of the tce page size */
409 	num_tce += next & (tce_size - 1);
410 	next &= ~(tce_size - 1);
411 
412 	/* covert to number of tces */
413 	num_tce |= tce_size - 1;
414 	num_tce >>= tce_shift;
415 
416 	do {
417 		/*
418 		 * Set up the page with TCE data, looping through and setting
419 		 * the values.
420 		 */
421 		limit = min_t(long, num_tce, 512);
422 		dma_offset = next + be64_to_cpu(maprange->dma_base);
423 
424 		rc = plpar_tce_stuff((u64)be32_to_cpu(maprange->liobn),
425 					     dma_offset,
426 					     0, limit);
427 		next += limit * tce_size;
428 		num_tce -= limit;
429 	} while (num_tce > 0 && !rc);
430 
431 	return rc;
432 }
433 
tce_setrange_multi_pSeriesLP(unsigned long start_pfn,unsigned long num_pfn,const void * arg)434 static int tce_setrange_multi_pSeriesLP(unsigned long start_pfn,
435 					unsigned long num_pfn, const void *arg)
436 {
437 	const struct dynamic_dma_window_prop *maprange = arg;
438 	u64 tce_size, num_tce, dma_offset, next, proto_tce, liobn;
439 	__be64 *tcep;
440 	u32 tce_shift;
441 	u64 rc = 0;
442 	long l, limit;
443 
444 	if (!firmware_has_feature(FW_FEATURE_PUT_TCE_IND)) {
445 		unsigned long tceshift = be32_to_cpu(maprange->tce_shift);
446 		unsigned long dmastart = (start_pfn << PAGE_SHIFT) +
447 				be64_to_cpu(maprange->dma_base);
448 		unsigned long tcenum = dmastart >> tceshift;
449 		unsigned long npages = num_pfn << PAGE_SHIFT >> tceshift;
450 		void *uaddr = __va(start_pfn << PAGE_SHIFT);
451 
452 		return tce_build_pSeriesLP(be32_to_cpu(maprange->liobn),
453 				tcenum, tceshift, npages, (unsigned long) uaddr,
454 				DMA_BIDIRECTIONAL, 0);
455 	}
456 
457 	local_irq_disable();	/* to protect tcep and the page behind it */
458 	tcep = __this_cpu_read(tce_page);
459 
460 	if (!tcep) {
461 		tcep = (__be64 *)__get_free_page(GFP_ATOMIC);
462 		if (!tcep) {
463 			local_irq_enable();
464 			return -ENOMEM;
465 		}
466 		__this_cpu_write(tce_page, tcep);
467 	}
468 
469 	proto_tce = TCE_PCI_READ | TCE_PCI_WRITE;
470 
471 	liobn = (u64)be32_to_cpu(maprange->liobn);
472 	tce_shift = be32_to_cpu(maprange->tce_shift);
473 	tce_size = 1ULL << tce_shift;
474 	next = start_pfn << PAGE_SHIFT;
475 	num_tce = num_pfn << PAGE_SHIFT;
476 
477 	/* round back to the beginning of the tce page size */
478 	num_tce += next & (tce_size - 1);
479 	next &= ~(tce_size - 1);
480 
481 	/* covert to number of tces */
482 	num_tce |= tce_size - 1;
483 	num_tce >>= tce_shift;
484 
485 	/* We can map max one pageful of TCEs at a time */
486 	do {
487 		/*
488 		 * Set up the page with TCE data, looping through and setting
489 		 * the values.
490 		 */
491 		limit = min_t(long, num_tce, 4096/TCE_ENTRY_SIZE);
492 		dma_offset = next + be64_to_cpu(maprange->dma_base);
493 
494 		for (l = 0; l < limit; l++) {
495 			tcep[l] = cpu_to_be64(proto_tce | next);
496 			next += tce_size;
497 		}
498 
499 		rc = plpar_tce_put_indirect(liobn,
500 					    dma_offset,
501 					    (u64)__pa(tcep),
502 					    limit);
503 
504 		num_tce -= limit;
505 	} while (num_tce > 0 && !rc);
506 
507 	/* error cleanup: caller will clear whole range */
508 
509 	local_irq_enable();
510 	return rc;
511 }
512 
tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,unsigned long num_pfn,void * arg)513 static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
514 		unsigned long num_pfn, void *arg)
515 {
516 	return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
517 }
518 
iommu_table_setparms_common(struct iommu_table * tbl,unsigned long busno,unsigned long liobn,unsigned long win_addr,unsigned long window_size,unsigned long page_shift,void * base,struct iommu_table_ops * table_ops)519 static void iommu_table_setparms_common(struct iommu_table *tbl, unsigned long busno,
520 					unsigned long liobn, unsigned long win_addr,
521 					unsigned long window_size, unsigned long page_shift,
522 					void *base, struct iommu_table_ops *table_ops)
523 {
524 	tbl->it_busno = busno;
525 	tbl->it_index = liobn;
526 	tbl->it_offset = win_addr >> page_shift;
527 	tbl->it_size = window_size >> page_shift;
528 	tbl->it_page_shift = page_shift;
529 	tbl->it_base = (unsigned long)base;
530 	tbl->it_blocksize = 16;
531 	tbl->it_type = TCE_PCI;
532 	tbl->it_ops = table_ops;
533 }
534 
535 struct iommu_table_ops iommu_table_pseries_ops;
536 
iommu_table_setparms(struct pci_controller * phb,struct device_node * dn,struct iommu_table * tbl)537 static void iommu_table_setparms(struct pci_controller *phb,
538 				 struct device_node *dn,
539 				 struct iommu_table *tbl)
540 {
541 	struct device_node *node;
542 	const unsigned long *basep;
543 	const u32 *sizep;
544 
545 	/* Test if we are going over 2GB of DMA space */
546 	if (phb->dma_window_base_cur + phb->dma_window_size > SZ_2G) {
547 		udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
548 		panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
549 	}
550 
551 	node = phb->dn;
552 	basep = of_get_property(node, "linux,tce-base", NULL);
553 	sizep = of_get_property(node, "linux,tce-size", NULL);
554 	if (basep == NULL || sizep == NULL) {
555 		printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %pOF has "
556 				"missing tce entries !\n", dn);
557 		return;
558 	}
559 
560 	iommu_table_setparms_common(tbl, phb->bus->number, 0, phb->dma_window_base_cur,
561 				    phb->dma_window_size, IOMMU_PAGE_SHIFT_4K,
562 				    __va(*basep), &iommu_table_pseries_ops);
563 
564 	if (!is_kdump_kernel())
565 		memset((void *)tbl->it_base, 0, *sizep);
566 
567 	phb->dma_window_base_cur += phb->dma_window_size;
568 }
569 
570 struct iommu_table_ops iommu_table_lpar_multi_ops;
571 
572 /*
573  * iommu_table_setparms_lpar
574  *
575  * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
576  */
iommu_table_setparms_lpar(struct pci_controller * phb,struct device_node * dn,struct iommu_table * tbl,struct iommu_table_group * table_group,const __be32 * dma_window)577 static void iommu_table_setparms_lpar(struct pci_controller *phb,
578 				      struct device_node *dn,
579 				      struct iommu_table *tbl,
580 				      struct iommu_table_group *table_group,
581 				      const __be32 *dma_window)
582 {
583 	unsigned long offset, size, liobn;
584 
585 	of_parse_dma_window(dn, dma_window, &liobn, &offset, &size);
586 
587 	iommu_table_setparms_common(tbl, phb->bus->number, liobn, offset, size, IOMMU_PAGE_SHIFT_4K, NULL,
588 				    &iommu_table_lpar_multi_ops);
589 
590 
591 	table_group->tce32_start = offset;
592 	table_group->tce32_size = size;
593 }
594 
595 struct iommu_table_ops iommu_table_pseries_ops = {
596 	.set = tce_build_pSeries,
597 	.clear = tce_free_pSeries,
598 	.get = tce_get_pseries
599 };
600 
pci_dma_bus_setup_pSeries(struct pci_bus * bus)601 static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
602 {
603 	struct device_node *dn;
604 	struct iommu_table *tbl;
605 	struct device_node *isa_dn, *isa_dn_orig;
606 	struct device_node *tmp;
607 	struct pci_dn *pci;
608 	int children;
609 
610 	dn = pci_bus_to_OF_node(bus);
611 
612 	pr_debug("pci_dma_bus_setup_pSeries: setting up bus %pOF\n", dn);
613 
614 	if (bus->self) {
615 		/* This is not a root bus, any setup will be done for the
616 		 * device-side of the bridge in iommu_dev_setup_pSeries().
617 		 */
618 		return;
619 	}
620 	pci = PCI_DN(dn);
621 
622 	/* Check if the ISA bus on the system is under
623 	 * this PHB.
624 	 */
625 	isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
626 
627 	while (isa_dn && isa_dn != dn)
628 		isa_dn = isa_dn->parent;
629 
630 	of_node_put(isa_dn_orig);
631 
632 	/* Count number of direct PCI children of the PHB. */
633 	for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
634 		children++;
635 
636 	pr_debug("Children: %d\n", children);
637 
638 	/* Calculate amount of DMA window per slot. Each window must be
639 	 * a power of two (due to pci_alloc_consistent requirements).
640 	 *
641 	 * Keep 256MB aside for PHBs with ISA.
642 	 */
643 
644 	if (!isa_dn) {
645 		/* No ISA/IDE - just set window size and return */
646 		pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
647 
648 		while (pci->phb->dma_window_size * children > 0x80000000ul)
649 			pci->phb->dma_window_size >>= 1;
650 		pr_debug("No ISA/IDE, window size is 0x%llx\n",
651 			 pci->phb->dma_window_size);
652 		pci->phb->dma_window_base_cur = 0;
653 
654 		return;
655 	}
656 
657 	/* If we have ISA, then we probably have an IDE
658 	 * controller too. Allocate a 128MB table but
659 	 * skip the first 128MB to avoid stepping on ISA
660 	 * space.
661 	 */
662 	pci->phb->dma_window_size = 0x8000000ul;
663 	pci->phb->dma_window_base_cur = 0x8000000ul;
664 
665 	pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
666 	tbl = pci->table_group->tables[0];
667 
668 	iommu_table_setparms(pci->phb, dn, tbl);
669 
670 	if (!iommu_init_table(tbl, pci->phb->node, 0, 0))
671 		panic("Failed to initialize iommu table");
672 
673 	/* Divide the rest (1.75GB) among the children */
674 	pci->phb->dma_window_size = 0x80000000ul;
675 	while (pci->phb->dma_window_size * children > 0x70000000ul)
676 		pci->phb->dma_window_size >>= 1;
677 
678 	pr_debug("ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
679 }
680 
681 #ifdef CONFIG_IOMMU_API
tce_exchange_pseries(struct iommu_table * tbl,long index,unsigned long * tce,enum dma_data_direction * direction,bool realmode)682 static int tce_exchange_pseries(struct iommu_table *tbl, long index, unsigned
683 				long *tce, enum dma_data_direction *direction,
684 				bool realmode)
685 {
686 	long rc;
687 	unsigned long ioba = (unsigned long) index << tbl->it_page_shift;
688 	unsigned long flags, oldtce = 0;
689 	u64 proto_tce = iommu_direction_to_tce_perm(*direction);
690 	unsigned long newtce = *tce | proto_tce;
691 
692 	spin_lock_irqsave(&tbl->large_pool.lock, flags);
693 
694 	rc = plpar_tce_get((u64)tbl->it_index, ioba, &oldtce);
695 	if (!rc)
696 		rc = plpar_tce_put((u64)tbl->it_index, ioba, newtce);
697 
698 	if (!rc) {
699 		*direction = iommu_tce_direction(oldtce);
700 		*tce = oldtce & ~(TCE_PCI_READ | TCE_PCI_WRITE);
701 	}
702 
703 	spin_unlock_irqrestore(&tbl->large_pool.lock, flags);
704 
705 	return rc;
706 }
707 #endif
708 
709 struct iommu_table_ops iommu_table_lpar_multi_ops = {
710 	.set = tce_buildmulti_pSeriesLP,
711 #ifdef CONFIG_IOMMU_API
712 	.xchg_no_kill = tce_exchange_pseries,
713 #endif
714 	.clear = tce_freemulti_pSeriesLP,
715 	.get = tce_get_pSeriesLP
716 };
717 
pci_dma_bus_setup_pSeriesLP(struct pci_bus * bus)718 static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
719 {
720 	struct iommu_table *tbl;
721 	struct device_node *dn, *pdn;
722 	struct pci_dn *ppci;
723 	const __be32 *dma_window = NULL;
724 
725 	dn = pci_bus_to_OF_node(bus);
726 
727 	pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %pOF\n",
728 		 dn);
729 
730 	/*
731 	 * Find nearest ibm,dma-window (default DMA window), walking up the
732 	 * device tree
733 	 */
734 	for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
735 		dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
736 		if (dma_window != NULL)
737 			break;
738 	}
739 
740 	if (dma_window == NULL) {
741 		pr_debug("  no ibm,dma-window property !\n");
742 		return;
743 	}
744 
745 	ppci = PCI_DN(pdn);
746 
747 	pr_debug("  parent is %pOF, iommu_table: 0x%p\n",
748 		 pdn, ppci->table_group);
749 
750 	if (!ppci->table_group) {
751 		ppci->table_group = iommu_pseries_alloc_group(ppci->phb->node);
752 		tbl = ppci->table_group->tables[0];
753 		iommu_table_setparms_lpar(ppci->phb, pdn, tbl,
754 				ppci->table_group, dma_window);
755 
756 		if (!iommu_init_table(tbl, ppci->phb->node, 0, 0))
757 			panic("Failed to initialize iommu table");
758 		iommu_register_group(ppci->table_group,
759 				pci_domain_nr(bus), 0);
760 		pr_debug("  created table: %p\n", ppci->table_group);
761 	}
762 }
763 
764 
pci_dma_dev_setup_pSeries(struct pci_dev * dev)765 static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
766 {
767 	struct device_node *dn;
768 	struct iommu_table *tbl;
769 
770 	pr_debug("pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
771 
772 	dn = dev->dev.of_node;
773 
774 	/* If we're the direct child of a root bus, then we need to allocate
775 	 * an iommu table ourselves. The bus setup code should have setup
776 	 * the window sizes already.
777 	 */
778 	if (!dev->bus->self) {
779 		struct pci_controller *phb = PCI_DN(dn)->phb;
780 
781 		pr_debug(" --> first child, no bridge. Allocating iommu table.\n");
782 		PCI_DN(dn)->table_group = iommu_pseries_alloc_group(phb->node);
783 		tbl = PCI_DN(dn)->table_group->tables[0];
784 		iommu_table_setparms(phb, dn, tbl);
785 
786 		if (!iommu_init_table(tbl, phb->node, 0, 0))
787 			panic("Failed to initialize iommu table");
788 
789 		set_iommu_table_base(&dev->dev, tbl);
790 		return;
791 	}
792 
793 	/* If this device is further down the bus tree, search upwards until
794 	 * an already allocated iommu table is found and use that.
795 	 */
796 
797 	while (dn && PCI_DN(dn) && PCI_DN(dn)->table_group == NULL)
798 		dn = dn->parent;
799 
800 	if (dn && PCI_DN(dn))
801 		set_iommu_table_base(&dev->dev,
802 				PCI_DN(dn)->table_group->tables[0]);
803 	else
804 		printk(KERN_WARNING "iommu: Device %s has no iommu table\n",
805 		       pci_name(dev));
806 }
807 
808 static int __read_mostly disable_ddw;
809 
disable_ddw_setup(char * str)810 static int __init disable_ddw_setup(char *str)
811 {
812 	disable_ddw = 1;
813 	printk(KERN_INFO "ppc iommu: disabling ddw.\n");
814 
815 	return 0;
816 }
817 
818 early_param("disable_ddw", disable_ddw_setup);
819 
clean_dma_window(struct device_node * np,struct dynamic_dma_window_prop * dwp)820 static void clean_dma_window(struct device_node *np, struct dynamic_dma_window_prop *dwp)
821 {
822 	int ret;
823 
824 	ret = tce_clearrange_multi_pSeriesLP(0,
825 		1ULL << (be32_to_cpu(dwp->window_shift) - PAGE_SHIFT), dwp);
826 	if (ret)
827 		pr_warn("%pOF failed to clear tces in window.\n",
828 			np);
829 	else
830 		pr_debug("%pOF successfully cleared tces in window.\n",
831 			 np);
832 }
833 
834 /*
835  * Call only if DMA window is clean.
836  */
__remove_dma_window(struct device_node * np,u32 * ddw_avail,u64 liobn)837 static void __remove_dma_window(struct device_node *np, u32 *ddw_avail, u64 liobn)
838 {
839 	int ret;
840 
841 	ret = rtas_call(ddw_avail[DDW_REMOVE_PE_DMA_WIN], 1, 1, NULL, liobn);
842 	if (ret)
843 		pr_warn("%pOF: failed to remove DMA window: rtas returned "
844 			"%d to ibm,remove-pe-dma-window(%x) %llx\n",
845 			np, ret, ddw_avail[DDW_REMOVE_PE_DMA_WIN], liobn);
846 	else
847 		pr_debug("%pOF: successfully removed DMA window: rtas returned "
848 			"%d to ibm,remove-pe-dma-window(%x) %llx\n",
849 			np, ret, ddw_avail[DDW_REMOVE_PE_DMA_WIN], liobn);
850 }
851 
remove_dma_window(struct device_node * np,u32 * ddw_avail,struct property * win)852 static void remove_dma_window(struct device_node *np, u32 *ddw_avail,
853 			      struct property *win)
854 {
855 	struct dynamic_dma_window_prop *dwp;
856 	u64 liobn;
857 
858 	dwp = win->value;
859 	liobn = (u64)be32_to_cpu(dwp->liobn);
860 
861 	clean_dma_window(np, dwp);
862 	__remove_dma_window(np, ddw_avail, liobn);
863 }
864 
remove_ddw(struct device_node * np,bool remove_prop,const char * win_name)865 static int remove_ddw(struct device_node *np, bool remove_prop, const char *win_name)
866 {
867 	struct property *win;
868 	u32 ddw_avail[DDW_APPLICABLE_SIZE];
869 	int ret = 0;
870 
871 	win = of_find_property(np, win_name, NULL);
872 	if (!win)
873 		return -EINVAL;
874 
875 	ret = of_property_read_u32_array(np, "ibm,ddw-applicable",
876 					 &ddw_avail[0], DDW_APPLICABLE_SIZE);
877 	if (ret)
878 		return 0;
879 
880 
881 	if (win->length >= sizeof(struct dynamic_dma_window_prop))
882 		remove_dma_window(np, ddw_avail, win);
883 
884 	if (!remove_prop)
885 		return 0;
886 
887 	ret = of_remove_property(np, win);
888 	if (ret)
889 		pr_warn("%pOF: failed to remove DMA window property: %d\n",
890 			np, ret);
891 	return 0;
892 }
893 
find_existing_ddw(struct device_node * pdn,u64 * dma_addr,int * window_shift)894 static bool find_existing_ddw(struct device_node *pdn, u64 *dma_addr, int *window_shift)
895 {
896 	struct dma_win *window;
897 	const struct dynamic_dma_window_prop *dma64;
898 	bool found = false;
899 
900 	spin_lock(&dma_win_list_lock);
901 	/* check if we already created a window and dupe that config if so */
902 	list_for_each_entry(window, &dma_win_list, list) {
903 		if (window->device == pdn) {
904 			dma64 = window->prop;
905 			*dma_addr = be64_to_cpu(dma64->dma_base);
906 			*window_shift = be32_to_cpu(dma64->window_shift);
907 			found = true;
908 			break;
909 		}
910 	}
911 	spin_unlock(&dma_win_list_lock);
912 
913 	return found;
914 }
915 
ddw_list_new_entry(struct device_node * pdn,const struct dynamic_dma_window_prop * dma64)916 static struct dma_win *ddw_list_new_entry(struct device_node *pdn,
917 					  const struct dynamic_dma_window_prop *dma64)
918 {
919 	struct dma_win *window;
920 
921 	window = kzalloc(sizeof(*window), GFP_KERNEL);
922 	if (!window)
923 		return NULL;
924 
925 	window->device = pdn;
926 	window->prop = dma64;
927 
928 	return window;
929 }
930 
find_existing_ddw_windows_named(const char * name)931 static void find_existing_ddw_windows_named(const char *name)
932 {
933 	int len;
934 	struct device_node *pdn;
935 	struct dma_win *window;
936 	const struct dynamic_dma_window_prop *dma64;
937 
938 	for_each_node_with_property(pdn, name) {
939 		dma64 = of_get_property(pdn, name, &len);
940 		if (!dma64 || len < sizeof(*dma64)) {
941 			remove_ddw(pdn, true, name);
942 			continue;
943 		}
944 
945 		window = ddw_list_new_entry(pdn, dma64);
946 		if (!window)
947 			break;
948 
949 		spin_lock(&dma_win_list_lock);
950 		list_add(&window->list, &dma_win_list);
951 		spin_unlock(&dma_win_list_lock);
952 	}
953 }
954 
find_existing_ddw_windows(void)955 static int find_existing_ddw_windows(void)
956 {
957 	if (!firmware_has_feature(FW_FEATURE_LPAR))
958 		return 0;
959 
960 	find_existing_ddw_windows_named(DIRECT64_PROPNAME);
961 	find_existing_ddw_windows_named(DMA64_PROPNAME);
962 
963 	return 0;
964 }
965 machine_arch_initcall(pseries, find_existing_ddw_windows);
966 
967 /**
968  * ddw_read_ext - Get the value of an DDW extension
969  * @np:		device node from which the extension value is to be read.
970  * @extnum:	index number of the extension.
971  * @value:	pointer to return value, modified when extension is available.
972  *
973  * Checks if "ibm,ddw-extensions" exists for this node, and get the value
974  * on index 'extnum'.
975  * It can be used only to check if a property exists, passing value == NULL.
976  *
977  * Returns:
978  *	0 if extension successfully read
979  *	-EINVAL if the "ibm,ddw-extensions" does not exist,
980  *	-ENODATA if "ibm,ddw-extensions" does not have a value, and
981  *	-EOVERFLOW if "ibm,ddw-extensions" does not contain this extension.
982  */
ddw_read_ext(const struct device_node * np,int extnum,u32 * value)983 static inline int ddw_read_ext(const struct device_node *np, int extnum,
984 			       u32 *value)
985 {
986 	static const char propname[] = "ibm,ddw-extensions";
987 	u32 count;
988 	int ret;
989 
990 	ret = of_property_read_u32_index(np, propname, DDW_EXT_SIZE, &count);
991 	if (ret)
992 		return ret;
993 
994 	if (count < extnum)
995 		return -EOVERFLOW;
996 
997 	if (!value)
998 		value = &count;
999 
1000 	return of_property_read_u32_index(np, propname, extnum, value);
1001 }
1002 
query_ddw(struct pci_dev * dev,const u32 * ddw_avail,struct ddw_query_response * query,struct device_node * parent)1003 static int query_ddw(struct pci_dev *dev, const u32 *ddw_avail,
1004 		     struct ddw_query_response *query,
1005 		     struct device_node *parent)
1006 {
1007 	struct device_node *dn;
1008 	struct pci_dn *pdn;
1009 	u32 cfg_addr, ext_query, query_out[5];
1010 	u64 buid;
1011 	int ret, out_sz;
1012 
1013 	/*
1014 	 * From LoPAR level 2.8, "ibm,ddw-extensions" index 3 can rule how many
1015 	 * output parameters ibm,query-pe-dma-windows will have, ranging from
1016 	 * 5 to 6.
1017 	 */
1018 	ret = ddw_read_ext(parent, DDW_EXT_QUERY_OUT_SIZE, &ext_query);
1019 	if (!ret && ext_query == 1)
1020 		out_sz = 6;
1021 	else
1022 		out_sz = 5;
1023 
1024 	/*
1025 	 * Get the config address and phb buid of the PE window.
1026 	 * Rely on eeh to retrieve this for us.
1027 	 * Retrieve them from the pci device, not the node with the
1028 	 * dma-window property
1029 	 */
1030 	dn = pci_device_to_OF_node(dev);
1031 	pdn = PCI_DN(dn);
1032 	buid = pdn->phb->buid;
1033 	cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
1034 
1035 	ret = rtas_call(ddw_avail[DDW_QUERY_PE_DMA_WIN], 3, out_sz, query_out,
1036 			cfg_addr, BUID_HI(buid), BUID_LO(buid));
1037 	dev_info(&dev->dev, "ibm,query-pe-dma-windows(%x) %x %x %x returned %d\n",
1038 		 ddw_avail[DDW_QUERY_PE_DMA_WIN], cfg_addr, BUID_HI(buid),
1039 		 BUID_LO(buid), ret);
1040 
1041 	switch (out_sz) {
1042 	case 5:
1043 		query->windows_available = query_out[0];
1044 		query->largest_available_block = query_out[1];
1045 		query->page_size = query_out[2];
1046 		query->migration_capable = query_out[3];
1047 		break;
1048 	case 6:
1049 		query->windows_available = query_out[0];
1050 		query->largest_available_block = ((u64)query_out[1] << 32) |
1051 						 query_out[2];
1052 		query->page_size = query_out[3];
1053 		query->migration_capable = query_out[4];
1054 		break;
1055 	}
1056 
1057 	return ret;
1058 }
1059 
create_ddw(struct pci_dev * dev,const u32 * ddw_avail,struct ddw_create_response * create,int page_shift,int window_shift)1060 static int create_ddw(struct pci_dev *dev, const u32 *ddw_avail,
1061 			struct ddw_create_response *create, int page_shift,
1062 			int window_shift)
1063 {
1064 	struct device_node *dn;
1065 	struct pci_dn *pdn;
1066 	u32 cfg_addr;
1067 	u64 buid;
1068 	int ret;
1069 
1070 	/*
1071 	 * Get the config address and phb buid of the PE window.
1072 	 * Rely on eeh to retrieve this for us.
1073 	 * Retrieve them from the pci device, not the node with the
1074 	 * dma-window property
1075 	 */
1076 	dn = pci_device_to_OF_node(dev);
1077 	pdn = PCI_DN(dn);
1078 	buid = pdn->phb->buid;
1079 	cfg_addr = ((pdn->busno << 16) | (pdn->devfn << 8));
1080 
1081 	do {
1082 		/* extra outputs are LIOBN and dma-addr (hi, lo) */
1083 		ret = rtas_call(ddw_avail[DDW_CREATE_PE_DMA_WIN], 5, 4,
1084 				(u32 *)create, cfg_addr, BUID_HI(buid),
1085 				BUID_LO(buid), page_shift, window_shift);
1086 	} while (rtas_busy_delay(ret));
1087 	dev_info(&dev->dev,
1088 		"ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
1089 		"(liobn = 0x%x starting addr = %x %x)\n",
1090 		 ddw_avail[DDW_CREATE_PE_DMA_WIN], cfg_addr, BUID_HI(buid),
1091 		 BUID_LO(buid), page_shift, window_shift, ret, create->liobn,
1092 		 create->addr_hi, create->addr_lo);
1093 
1094 	return ret;
1095 }
1096 
1097 struct failed_ddw_pdn {
1098 	struct device_node *pdn;
1099 	struct list_head list;
1100 };
1101 
1102 static LIST_HEAD(failed_ddw_pdn_list);
1103 
ddw_memory_hotplug_max(void)1104 static phys_addr_t ddw_memory_hotplug_max(void)
1105 {
1106 	phys_addr_t max_addr = memory_hotplug_max();
1107 	struct device_node *memory;
1108 
1109 	for_each_node_by_type(memory, "memory") {
1110 		unsigned long start, size;
1111 		int n_mem_addr_cells, n_mem_size_cells, len;
1112 		const __be32 *memcell_buf;
1113 
1114 		memcell_buf = of_get_property(memory, "reg", &len);
1115 		if (!memcell_buf || len <= 0)
1116 			continue;
1117 
1118 		n_mem_addr_cells = of_n_addr_cells(memory);
1119 		n_mem_size_cells = of_n_size_cells(memory);
1120 
1121 		start = of_read_number(memcell_buf, n_mem_addr_cells);
1122 		memcell_buf += n_mem_addr_cells;
1123 		size = of_read_number(memcell_buf, n_mem_size_cells);
1124 		memcell_buf += n_mem_size_cells;
1125 
1126 		max_addr = max_t(phys_addr_t, max_addr, start + size);
1127 	}
1128 
1129 	return max_addr;
1130 }
1131 
1132 /*
1133  * Platforms supporting the DDW option starting with LoPAR level 2.7 implement
1134  * ibm,ddw-extensions, which carries the rtas token for
1135  * ibm,reset-pe-dma-windows.
1136  * That rtas-call can be used to restore the default DMA window for the device.
1137  */
reset_dma_window(struct pci_dev * dev,struct device_node * par_dn)1138 static void reset_dma_window(struct pci_dev *dev, struct device_node *par_dn)
1139 {
1140 	int ret;
1141 	u32 cfg_addr, reset_dma_win;
1142 	u64 buid;
1143 	struct device_node *dn;
1144 	struct pci_dn *pdn;
1145 
1146 	ret = ddw_read_ext(par_dn, DDW_EXT_RESET_DMA_WIN, &reset_dma_win);
1147 	if (ret)
1148 		return;
1149 
1150 	dn = pci_device_to_OF_node(dev);
1151 	pdn = PCI_DN(dn);
1152 	buid = pdn->phb->buid;
1153 	cfg_addr = (pdn->busno << 16) | (pdn->devfn << 8);
1154 
1155 	ret = rtas_call(reset_dma_win, 3, 1, NULL, cfg_addr, BUID_HI(buid),
1156 			BUID_LO(buid));
1157 	if (ret)
1158 		dev_info(&dev->dev,
1159 			 "ibm,reset-pe-dma-windows(%x) %x %x %x returned %d ",
1160 			 reset_dma_win, cfg_addr, BUID_HI(buid), BUID_LO(buid),
1161 			 ret);
1162 }
1163 
1164 /* Return largest page shift based on "IO Page Sizes" output of ibm,query-pe-dma-window. */
iommu_get_page_shift(u32 query_page_size)1165 static int iommu_get_page_shift(u32 query_page_size)
1166 {
1167 	/* Supported IO page-sizes according to LoPAR */
1168 	const int shift[] = {
1169 		__builtin_ctzll(SZ_4K),   __builtin_ctzll(SZ_64K), __builtin_ctzll(SZ_16M),
1170 		__builtin_ctzll(SZ_32M),  __builtin_ctzll(SZ_64M), __builtin_ctzll(SZ_128M),
1171 		__builtin_ctzll(SZ_256M), __builtin_ctzll(SZ_16G)
1172 	};
1173 
1174 	int i = ARRAY_SIZE(shift) - 1;
1175 
1176 	/*
1177 	 * On LoPAR, ibm,query-pe-dma-window outputs "IO Page Sizes" using a bit field:
1178 	 * - bit 31 means 4k pages are supported,
1179 	 * - bit 30 means 64k pages are supported, and so on.
1180 	 * Larger pagesizes map more memory with the same amount of TCEs, so start probing them.
1181 	 */
1182 	for (; i >= 0 ; i--) {
1183 		if (query_page_size & (1 << i))
1184 			return shift[i];
1185 	}
1186 
1187 	/* No valid page size found. */
1188 	return 0;
1189 }
1190 
ddw_property_create(const char * propname,u32 liobn,u64 dma_addr,u32 page_shift,u32 window_shift)1191 static struct property *ddw_property_create(const char *propname, u32 liobn, u64 dma_addr,
1192 					    u32 page_shift, u32 window_shift)
1193 {
1194 	struct dynamic_dma_window_prop *ddwprop;
1195 	struct property *win64;
1196 
1197 	win64 = kzalloc(sizeof(*win64), GFP_KERNEL);
1198 	if (!win64)
1199 		return NULL;
1200 
1201 	win64->name = kstrdup(propname, GFP_KERNEL);
1202 	ddwprop = kzalloc(sizeof(*ddwprop), GFP_KERNEL);
1203 	win64->value = ddwprop;
1204 	win64->length = sizeof(*ddwprop);
1205 	if (!win64->name || !win64->value) {
1206 		kfree(win64->name);
1207 		kfree(win64->value);
1208 		kfree(win64);
1209 		return NULL;
1210 	}
1211 
1212 	ddwprop->liobn = cpu_to_be32(liobn);
1213 	ddwprop->dma_base = cpu_to_be64(dma_addr);
1214 	ddwprop->tce_shift = cpu_to_be32(page_shift);
1215 	ddwprop->window_shift = cpu_to_be32(window_shift);
1216 
1217 	return win64;
1218 }
1219 
1220 /*
1221  * If the PE supports dynamic dma windows, and there is space for a table
1222  * that can map all pages in a linear offset, then setup such a table,
1223  * and record the dma-offset in the struct device.
1224  *
1225  * dev: the pci device we are checking
1226  * pdn: the parent pe node with the ibm,dma_window property
1227  * Future: also check if we can remap the base window for our base page size
1228  *
1229  * returns true if can map all pages (direct mapping), false otherwise..
1230  */
enable_ddw(struct pci_dev * dev,struct device_node * pdn)1231 static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
1232 {
1233 	int len = 0, ret;
1234 	int max_ram_len = order_base_2(ddw_memory_hotplug_max());
1235 	struct ddw_query_response query;
1236 	struct ddw_create_response create;
1237 	int page_shift;
1238 	u64 win_addr;
1239 	const char *win_name;
1240 	struct device_node *dn;
1241 	u32 ddw_avail[DDW_APPLICABLE_SIZE];
1242 	struct dma_win *window;
1243 	struct property *win64;
1244 	struct failed_ddw_pdn *fpdn;
1245 	bool default_win_removed = false, direct_mapping = false;
1246 	bool pmem_present;
1247 	struct pci_dn *pci = PCI_DN(pdn);
1248 	struct iommu_table *tbl = pci->table_group->tables[0];
1249 
1250 	dn = of_find_node_by_type(NULL, "ibm,pmemory");
1251 	pmem_present = dn != NULL;
1252 	of_node_put(dn);
1253 
1254 	mutex_lock(&dma_win_init_mutex);
1255 
1256 	if (find_existing_ddw(pdn, &dev->dev.archdata.dma_offset, &len)) {
1257 		direct_mapping = (len >= max_ram_len);
1258 		goto out_unlock;
1259 	}
1260 
1261 	/*
1262 	 * If we already went through this for a previous function of
1263 	 * the same device and failed, we don't want to muck with the
1264 	 * DMA window again, as it will race with in-flight operations
1265 	 * and can lead to EEHs. The above mutex protects access to the
1266 	 * list.
1267 	 */
1268 	list_for_each_entry(fpdn, &failed_ddw_pdn_list, list) {
1269 		if (fpdn->pdn == pdn)
1270 			goto out_unlock;
1271 	}
1272 
1273 	/*
1274 	 * the ibm,ddw-applicable property holds the tokens for:
1275 	 * ibm,query-pe-dma-window
1276 	 * ibm,create-pe-dma-window
1277 	 * ibm,remove-pe-dma-window
1278 	 * for the given node in that order.
1279 	 * the property is actually in the parent, not the PE
1280 	 */
1281 	ret = of_property_read_u32_array(pdn, "ibm,ddw-applicable",
1282 					 &ddw_avail[0], DDW_APPLICABLE_SIZE);
1283 	if (ret)
1284 		goto out_failed;
1285 
1286        /*
1287 	 * Query if there is a second window of size to map the
1288 	 * whole partition.  Query returns number of windows, largest
1289 	 * block assigned to PE (partition endpoint), and two bitmasks
1290 	 * of page sizes: supported and supported for migrate-dma.
1291 	 */
1292 	dn = pci_device_to_OF_node(dev);
1293 	ret = query_ddw(dev, ddw_avail, &query, pdn);
1294 	if (ret != 0)
1295 		goto out_failed;
1296 
1297 	/*
1298 	 * If there is no window available, remove the default DMA window,
1299 	 * if it's present. This will make all the resources available to the
1300 	 * new DDW window.
1301 	 * If anything fails after this, we need to restore it, so also check
1302 	 * for extensions presence.
1303 	 */
1304 	if (query.windows_available == 0) {
1305 		struct property *default_win;
1306 		int reset_win_ext;
1307 
1308 		/* DDW + IOMMU on single window may fail if there is any allocation */
1309 		if (iommu_table_in_use(tbl)) {
1310 			dev_warn(&dev->dev, "current IOMMU table in use, can't be replaced.\n");
1311 			goto out_failed;
1312 		}
1313 
1314 		default_win = of_find_property(pdn, "ibm,dma-window", NULL);
1315 		if (!default_win)
1316 			goto out_failed;
1317 
1318 		reset_win_ext = ddw_read_ext(pdn, DDW_EXT_RESET_DMA_WIN, NULL);
1319 		if (reset_win_ext)
1320 			goto out_failed;
1321 
1322 		remove_dma_window(pdn, ddw_avail, default_win);
1323 		default_win_removed = true;
1324 
1325 		/* Query again, to check if the window is available */
1326 		ret = query_ddw(dev, ddw_avail, &query, pdn);
1327 		if (ret != 0)
1328 			goto out_failed;
1329 
1330 		if (query.windows_available == 0) {
1331 			/* no windows are available for this device. */
1332 			dev_dbg(&dev->dev, "no free dynamic windows");
1333 			goto out_failed;
1334 		}
1335 	}
1336 
1337 	page_shift = iommu_get_page_shift(query.page_size);
1338 	if (!page_shift) {
1339 		dev_dbg(&dev->dev, "no supported page size in mask %x",
1340 			query.page_size);
1341 		goto out_failed;
1342 	}
1343 
1344 
1345 	/*
1346 	 * The "ibm,pmemory" can appear anywhere in the address space.
1347 	 * Assuming it is still backed by page structs, try MAX_PHYSMEM_BITS
1348 	 * for the upper limit and fallback to max RAM otherwise but this
1349 	 * disables device::dma_ops_bypass.
1350 	 */
1351 	len = max_ram_len;
1352 	if (pmem_present) {
1353 		if (query.largest_available_block >=
1354 		    (1ULL << (MAX_PHYSMEM_BITS - page_shift)))
1355 			len = MAX_PHYSMEM_BITS;
1356 		else
1357 			dev_info(&dev->dev, "Skipping ibm,pmemory");
1358 	}
1359 
1360 	/* check if the available block * number of ptes will map everything */
1361 	if (query.largest_available_block < (1ULL << (len - page_shift))) {
1362 		dev_dbg(&dev->dev,
1363 			"can't map partition max 0x%llx with %llu %llu-sized pages\n",
1364 			1ULL << len,
1365 			query.largest_available_block,
1366 			1ULL << page_shift);
1367 
1368 		len = order_base_2(query.largest_available_block << page_shift);
1369 		win_name = DMA64_PROPNAME;
1370 	} else {
1371 		direct_mapping = !default_win_removed ||
1372 			(len == MAX_PHYSMEM_BITS) ||
1373 			(!pmem_present && (len == max_ram_len));
1374 		win_name = direct_mapping ? DIRECT64_PROPNAME : DMA64_PROPNAME;
1375 	}
1376 
1377 	ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
1378 	if (ret != 0)
1379 		goto out_failed;
1380 
1381 	dev_dbg(&dev->dev, "created tce table LIOBN 0x%x for %pOF\n",
1382 		  create.liobn, dn);
1383 
1384 	win_addr = ((u64)create.addr_hi << 32) | create.addr_lo;
1385 	win64 = ddw_property_create(win_name, create.liobn, win_addr, page_shift, len);
1386 
1387 	if (!win64) {
1388 		dev_info(&dev->dev,
1389 			 "couldn't allocate property, property name, or value\n");
1390 		goto out_remove_win;
1391 	}
1392 
1393 	ret = of_add_property(pdn, win64);
1394 	if (ret) {
1395 		dev_err(&dev->dev, "unable to add DMA window property for %pOF: %d",
1396 			pdn, ret);
1397 		goto out_free_prop;
1398 	}
1399 
1400 	window = ddw_list_new_entry(pdn, win64->value);
1401 	if (!window)
1402 		goto out_del_prop;
1403 
1404 	if (direct_mapping) {
1405 		/* DDW maps the whole partition, so enable direct DMA mapping */
1406 		ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
1407 					    win64->value, tce_setrange_multi_pSeriesLP_walk);
1408 		if (ret) {
1409 			dev_info(&dev->dev, "failed to map DMA window for %pOF: %d\n",
1410 				 dn, ret);
1411 
1412 			/* Make sure to clean DDW if any TCE was set*/
1413 			clean_dma_window(pdn, win64->value);
1414 			goto out_del_list;
1415 		}
1416 	} else {
1417 		struct iommu_table *newtbl;
1418 		int i;
1419 		unsigned long start = 0, end = 0;
1420 
1421 		for (i = 0; i < ARRAY_SIZE(pci->phb->mem_resources); i++) {
1422 			const unsigned long mask = IORESOURCE_MEM_64 | IORESOURCE_MEM;
1423 
1424 			/* Look for MMIO32 */
1425 			if ((pci->phb->mem_resources[i].flags & mask) == IORESOURCE_MEM) {
1426 				start = pci->phb->mem_resources[i].start;
1427 				end = pci->phb->mem_resources[i].end;
1428 				break;
1429 			}
1430 		}
1431 
1432 		/* New table for using DDW instead of the default DMA window */
1433 		newtbl = iommu_pseries_alloc_table(pci->phb->node);
1434 		if (!newtbl) {
1435 			dev_dbg(&dev->dev, "couldn't create new IOMMU table\n");
1436 			goto out_del_list;
1437 		}
1438 
1439 		iommu_table_setparms_common(newtbl, pci->phb->bus->number, create.liobn, win_addr,
1440 					    1UL << len, page_shift, NULL, &iommu_table_lpar_multi_ops);
1441 		iommu_init_table(newtbl, pci->phb->node, start, end);
1442 
1443 		pci->table_group->tables[1] = newtbl;
1444 
1445 		/* Keep default DMA window stuct if removed */
1446 		if (default_win_removed) {
1447 			tbl->it_size = 0;
1448 			vfree(tbl->it_map);
1449 			tbl->it_map = NULL;
1450 		}
1451 
1452 		set_iommu_table_base(&dev->dev, newtbl);
1453 	}
1454 
1455 	spin_lock(&dma_win_list_lock);
1456 	list_add(&window->list, &dma_win_list);
1457 	spin_unlock(&dma_win_list_lock);
1458 
1459 	dev->dev.archdata.dma_offset = win_addr;
1460 	goto out_unlock;
1461 
1462 out_del_list:
1463 	kfree(window);
1464 
1465 out_del_prop:
1466 	of_remove_property(pdn, win64);
1467 
1468 out_free_prop:
1469 	kfree(win64->name);
1470 	kfree(win64->value);
1471 	kfree(win64);
1472 
1473 out_remove_win:
1474 	/* DDW is clean, so it's ok to call this directly. */
1475 	__remove_dma_window(pdn, ddw_avail, create.liobn);
1476 
1477 out_failed:
1478 	if (default_win_removed)
1479 		reset_dma_window(dev, pdn);
1480 
1481 	fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
1482 	if (!fpdn)
1483 		goto out_unlock;
1484 	fpdn->pdn = pdn;
1485 	list_add(&fpdn->list, &failed_ddw_pdn_list);
1486 
1487 out_unlock:
1488 	mutex_unlock(&dma_win_init_mutex);
1489 
1490 	/*
1491 	 * If we have persistent memory and the window size is only as big
1492 	 * as RAM, then we failed to create a window to cover persistent
1493 	 * memory and need to set the DMA limit.
1494 	 */
1495 	if (pmem_present && direct_mapping && len == max_ram_len)
1496 		dev->dev.bus_dma_limit = dev->dev.archdata.dma_offset + (1ULL << len);
1497 
1498 	return direct_mapping;
1499 }
1500 
pci_dma_dev_setup_pSeriesLP(struct pci_dev * dev)1501 static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
1502 {
1503 	struct device_node *pdn, *dn;
1504 	struct iommu_table *tbl;
1505 	const __be32 *dma_window = NULL;
1506 	struct pci_dn *pci;
1507 
1508 	pr_debug("pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
1509 
1510 	/* dev setup for LPAR is a little tricky, since the device tree might
1511 	 * contain the dma-window properties per-device and not necessarily
1512 	 * for the bus. So we need to search upwards in the tree until we
1513 	 * either hit a dma-window property, OR find a parent with a table
1514 	 * already allocated.
1515 	 */
1516 	dn = pci_device_to_OF_node(dev);
1517 	pr_debug("  node is %pOF\n", dn);
1518 
1519 	for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1520 	     pdn = pdn->parent) {
1521 		dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1522 		if (dma_window)
1523 			break;
1524 	}
1525 
1526 	if (!pdn || !PCI_DN(pdn)) {
1527 		printk(KERN_WARNING "pci_dma_dev_setup_pSeriesLP: "
1528 		       "no DMA window found for pci dev=%s dn=%pOF\n",
1529 				 pci_name(dev), dn);
1530 		return;
1531 	}
1532 	pr_debug("  parent is %pOF\n", pdn);
1533 
1534 	pci = PCI_DN(pdn);
1535 	if (!pci->table_group) {
1536 		pci->table_group = iommu_pseries_alloc_group(pci->phb->node);
1537 		tbl = pci->table_group->tables[0];
1538 		iommu_table_setparms_lpar(pci->phb, pdn, tbl,
1539 				pci->table_group, dma_window);
1540 
1541 		iommu_init_table(tbl, pci->phb->node, 0, 0);
1542 		iommu_register_group(pci->table_group,
1543 				pci_domain_nr(pci->phb->bus), 0);
1544 		pr_debug("  created table: %p\n", pci->table_group);
1545 	} else {
1546 		pr_debug("  found DMA window, table: %p\n", pci->table_group);
1547 	}
1548 
1549 	set_iommu_table_base(&dev->dev, pci->table_group->tables[0]);
1550 	iommu_add_device(pci->table_group, &dev->dev);
1551 }
1552 
iommu_bypass_supported_pSeriesLP(struct pci_dev * pdev,u64 dma_mask)1553 static bool iommu_bypass_supported_pSeriesLP(struct pci_dev *pdev, u64 dma_mask)
1554 {
1555 	struct device_node *dn = pci_device_to_OF_node(pdev), *pdn;
1556 	const __be32 *dma_window = NULL;
1557 
1558 	/* only attempt to use a new window if 64-bit DMA is requested */
1559 	if (dma_mask < DMA_BIT_MASK(64))
1560 		return false;
1561 
1562 	dev_dbg(&pdev->dev, "node is %pOF\n", dn);
1563 
1564 	/*
1565 	 * the device tree might contain the dma-window properties
1566 	 * per-device and not necessarily for the bus. So we need to
1567 	 * search upwards in the tree until we either hit a dma-window
1568 	 * property, OR find a parent with a table already allocated.
1569 	 */
1570 	for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->table_group;
1571 			pdn = pdn->parent) {
1572 		dma_window = of_get_property(pdn, "ibm,dma-window", NULL);
1573 		if (dma_window)
1574 			break;
1575 	}
1576 
1577 	if (pdn && PCI_DN(pdn))
1578 		return enable_ddw(pdev, pdn);
1579 
1580 	return false;
1581 }
1582 
iommu_mem_notifier(struct notifier_block * nb,unsigned long action,void * data)1583 static int iommu_mem_notifier(struct notifier_block *nb, unsigned long action,
1584 		void *data)
1585 {
1586 	struct dma_win *window;
1587 	struct memory_notify *arg = data;
1588 	int ret = 0;
1589 
1590 	switch (action) {
1591 	case MEM_GOING_ONLINE:
1592 		spin_lock(&dma_win_list_lock);
1593 		list_for_each_entry(window, &dma_win_list, list) {
1594 			ret |= tce_setrange_multi_pSeriesLP(arg->start_pfn,
1595 					arg->nr_pages, window->prop);
1596 			/* XXX log error */
1597 		}
1598 		spin_unlock(&dma_win_list_lock);
1599 		break;
1600 	case MEM_CANCEL_ONLINE:
1601 	case MEM_OFFLINE:
1602 		spin_lock(&dma_win_list_lock);
1603 		list_for_each_entry(window, &dma_win_list, list) {
1604 			ret |= tce_clearrange_multi_pSeriesLP(arg->start_pfn,
1605 					arg->nr_pages, window->prop);
1606 			/* XXX log error */
1607 		}
1608 		spin_unlock(&dma_win_list_lock);
1609 		break;
1610 	default:
1611 		break;
1612 	}
1613 	if (ret && action != MEM_CANCEL_ONLINE)
1614 		return NOTIFY_BAD;
1615 
1616 	return NOTIFY_OK;
1617 }
1618 
1619 static struct notifier_block iommu_mem_nb = {
1620 	.notifier_call = iommu_mem_notifier,
1621 };
1622 
iommu_reconfig_notifier(struct notifier_block * nb,unsigned long action,void * data)1623 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *data)
1624 {
1625 	int err = NOTIFY_OK;
1626 	struct of_reconfig_data *rd = data;
1627 	struct device_node *np = rd->dn;
1628 	struct pci_dn *pci = PCI_DN(np);
1629 	struct dma_win *window;
1630 
1631 	switch (action) {
1632 	case OF_RECONFIG_DETACH_NODE:
1633 		/*
1634 		 * Removing the property will invoke the reconfig
1635 		 * notifier again, which causes dead-lock on the
1636 		 * read-write semaphore of the notifier chain. So
1637 		 * we have to remove the property when releasing
1638 		 * the device node.
1639 		 */
1640 		if (remove_ddw(np, false, DIRECT64_PROPNAME))
1641 			remove_ddw(np, false, DMA64_PROPNAME);
1642 
1643 		if (pci && pci->table_group)
1644 			iommu_pseries_free_group(pci->table_group,
1645 					np->full_name);
1646 
1647 		spin_lock(&dma_win_list_lock);
1648 		list_for_each_entry(window, &dma_win_list, list) {
1649 			if (window->device == np) {
1650 				list_del(&window->list);
1651 				kfree(window);
1652 				break;
1653 			}
1654 		}
1655 		spin_unlock(&dma_win_list_lock);
1656 		break;
1657 	default:
1658 		err = NOTIFY_DONE;
1659 		break;
1660 	}
1661 	return err;
1662 }
1663 
1664 static struct notifier_block iommu_reconfig_nb = {
1665 	.notifier_call = iommu_reconfig_notifier,
1666 };
1667 
1668 /* These are called very early. */
iommu_init_early_pSeries(void)1669 void iommu_init_early_pSeries(void)
1670 {
1671 	if (of_chosen && of_get_property(of_chosen, "linux,iommu-off", NULL))
1672 		return;
1673 
1674 	if (firmware_has_feature(FW_FEATURE_LPAR)) {
1675 		pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeriesLP;
1676 		pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeriesLP;
1677 		if (!disable_ddw)
1678 			pseries_pci_controller_ops.iommu_bypass_supported =
1679 				iommu_bypass_supported_pSeriesLP;
1680 	} else {
1681 		pseries_pci_controller_ops.dma_bus_setup = pci_dma_bus_setup_pSeries;
1682 		pseries_pci_controller_ops.dma_dev_setup = pci_dma_dev_setup_pSeries;
1683 	}
1684 
1685 
1686 	of_reconfig_notifier_register(&iommu_reconfig_nb);
1687 	register_memory_notifier(&iommu_mem_nb);
1688 
1689 	set_pci_dma_ops(&dma_iommu_ops);
1690 }
1691 
disable_multitce(char * str)1692 static int __init disable_multitce(char *str)
1693 {
1694 	if (strcmp(str, "off") == 0 &&
1695 	    firmware_has_feature(FW_FEATURE_LPAR) &&
1696 	    (firmware_has_feature(FW_FEATURE_PUT_TCE_IND) ||
1697 	     firmware_has_feature(FW_FEATURE_STUFF_TCE))) {
1698 		printk(KERN_INFO "Disabling MULTITCE firmware feature\n");
1699 		powerpc_firmware_features &=
1700 			~(FW_FEATURE_PUT_TCE_IND | FW_FEATURE_STUFF_TCE);
1701 	}
1702 	return 1;
1703 }
1704 
1705 __setup("multitce=", disable_multitce);
1706 
tce_iommu_bus_notifier(struct notifier_block * nb,unsigned long action,void * data)1707 static int tce_iommu_bus_notifier(struct notifier_block *nb,
1708 		unsigned long action, void *data)
1709 {
1710 	struct device *dev = data;
1711 
1712 	switch (action) {
1713 	case BUS_NOTIFY_DEL_DEVICE:
1714 		iommu_del_device(dev);
1715 		return 0;
1716 	default:
1717 		return 0;
1718 	}
1719 }
1720 
1721 static struct notifier_block tce_iommu_bus_nb = {
1722 	.notifier_call = tce_iommu_bus_notifier,
1723 };
1724 
tce_iommu_bus_notifier_init(void)1725 static int __init tce_iommu_bus_notifier_init(void)
1726 {
1727 	bus_register_notifier(&pci_bus_type, &tce_iommu_bus_nb);
1728 	return 0;
1729 }
1730 machine_subsys_initcall_sync(pseries, tce_iommu_bus_notifier_init);
1731