xref: /linux/arch/powerpc/platforms/pseries/iommu.c (revision 5e8d780d745c1619aba81fe7166c5a4b5cad2b84)
1 /*
2  * Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
3  *
4  * Rewrite, cleanup:
5  *
6  * Copyright (C) 2004 Olof Johansson <olof@lixom.net>, IBM Corporation
7  * Copyright (C) 2006 Olof Johansson <olof@lixom.net>
8  *
9  * Dynamic DMA mapping support, pSeries-specific parts, both SMP and LPAR.
10  *
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
25  */
26 
27 #include <linux/config.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/slab.h>
31 #include <linux/mm.h>
32 #include <linux/spinlock.h>
33 #include <linux/string.h>
34 #include <linux/pci.h>
35 #include <linux/dma-mapping.h>
36 #include <asm/io.h>
37 #include <asm/prom.h>
38 #include <asm/rtas.h>
39 #include <asm/iommu.h>
40 #include <asm/pci-bridge.h>
41 #include <asm/machdep.h>
42 #include <asm/abs_addr.h>
43 #include <asm/pSeries_reconfig.h>
44 #include <asm/firmware.h>
45 #include <asm/tce.h>
46 #include <asm/ppc-pci.h>
47 #include <asm/udbg.h>
48 
49 #include "plpar_wrappers.h"
50 
51 #define DBG(fmt...)
52 
53 static void tce_build_pSeries(struct iommu_table *tbl, long index,
54 			      long npages, unsigned long uaddr,
55 			      enum dma_data_direction direction)
56 {
57 	u64 proto_tce;
58 	u64 *tcep;
59 	u64 rpn;
60 
61 	index <<= TCE_PAGE_FACTOR;
62 	npages <<= TCE_PAGE_FACTOR;
63 
64 	proto_tce = TCE_PCI_READ; // Read allowed
65 
66 	if (direction != DMA_TO_DEVICE)
67 		proto_tce |= TCE_PCI_WRITE;
68 
69 	tcep = ((u64 *)tbl->it_base) + index;
70 
71 	while (npages--) {
72 		/* can't move this out since we might cross LMB boundary */
73 		rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
74 		*tcep = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
75 
76 		uaddr += TCE_PAGE_SIZE;
77 		tcep++;
78 	}
79 }
80 
81 
82 static void tce_free_pSeries(struct iommu_table *tbl, long index, long npages)
83 {
84 	u64 *tcep;
85 
86 	npages <<= TCE_PAGE_FACTOR;
87 	index <<= TCE_PAGE_FACTOR;
88 
89 	tcep = ((u64 *)tbl->it_base) + index;
90 
91 	while (npages--)
92 		*(tcep++) = 0;
93 }
94 
95 static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)
96 {
97 	u64 *tcep;
98 
99 	index <<= TCE_PAGE_FACTOR;
100 	tcep = ((u64 *)tbl->it_base) + index;
101 
102 	return *tcep;
103 }
104 
105 static void tce_build_pSeriesLP(struct iommu_table *tbl, long tcenum,
106 				long npages, unsigned long uaddr,
107 				enum dma_data_direction direction)
108 {
109 	u64 rc;
110 	u64 proto_tce, tce;
111 	u64 rpn;
112 
113 	tcenum <<= TCE_PAGE_FACTOR;
114 	npages <<= TCE_PAGE_FACTOR;
115 
116 	rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
117 	proto_tce = TCE_PCI_READ;
118 	if (direction != DMA_TO_DEVICE)
119 		proto_tce |= TCE_PCI_WRITE;
120 
121 	while (npages--) {
122 		tce = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
123 		rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, tce);
124 
125 		if (rc && printk_ratelimit()) {
126 			printk("tce_build_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
127 			printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
128 			printk("\ttcenum  = 0x%lx\n", (u64)tcenum);
129 			printk("\ttce val = 0x%lx\n", tce );
130 			show_stack(current, (unsigned long *)__get_SP());
131 		}
132 
133 		tcenum++;
134 		rpn++;
135 	}
136 }
137 
138 static DEFINE_PER_CPU(u64 *, tce_page) = NULL;
139 
140 static void tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,
141 				     long npages, unsigned long uaddr,
142 				     enum dma_data_direction direction)
143 {
144 	u64 rc;
145 	u64 proto_tce;
146 	u64 *tcep;
147 	u64 rpn;
148 	long l, limit;
149 
150 	if (TCE_PAGE_FACTOR == 0 && npages == 1)
151 		return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
152 					   direction);
153 
154 	tcep = __get_cpu_var(tce_page);
155 
156 	/* This is safe to do since interrupts are off when we're called
157 	 * from iommu_alloc{,_sg}()
158 	 */
159 	if (!tcep) {
160 		tcep = (u64 *)__get_free_page(GFP_ATOMIC);
161 		/* If allocation fails, fall back to the loop implementation */
162 		if (!tcep)
163 			return tce_build_pSeriesLP(tbl, tcenum, npages,
164 						   uaddr, direction);
165 		__get_cpu_var(tce_page) = tcep;
166 	}
167 
168 	tcenum <<= TCE_PAGE_FACTOR;
169 	npages <<= TCE_PAGE_FACTOR;
170 
171 	rpn = (virt_to_abs(uaddr)) >> TCE_SHIFT;
172 	proto_tce = TCE_PCI_READ;
173 	if (direction != DMA_TO_DEVICE)
174 		proto_tce |= TCE_PCI_WRITE;
175 
176 	/* We can map max one pageful of TCEs at a time */
177 	do {
178 		/*
179 		 * Set up the page with TCE data, looping through and setting
180 		 * the values.
181 		 */
182 		limit = min_t(long, npages, 4096/TCE_ENTRY_SIZE);
183 
184 		for (l = 0; l < limit; l++) {
185 			tcep[l] = proto_tce | (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;
186 			rpn++;
187 		}
188 
189 		rc = plpar_tce_put_indirect((u64)tbl->it_index,
190 					    (u64)tcenum << 12,
191 					    (u64)virt_to_abs(tcep),
192 					    limit);
193 
194 		npages -= limit;
195 		tcenum += limit;
196 	} while (npages > 0 && !rc);
197 
198 	if (rc && printk_ratelimit()) {
199 		printk("tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
200 		printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
201 		printk("\tnpages  = 0x%lx\n", (u64)npages);
202 		printk("\ttce[0] val = 0x%lx\n", tcep[0]);
203 		show_stack(current, (unsigned long *)__get_SP());
204 	}
205 }
206 
207 static void tce_free_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
208 {
209 	u64 rc;
210 
211 	tcenum <<= TCE_PAGE_FACTOR;
212 	npages <<= TCE_PAGE_FACTOR;
213 
214 	while (npages--) {
215 		rc = plpar_tce_put((u64)tbl->it_index, (u64)tcenum << 12, 0);
216 
217 		if (rc && printk_ratelimit()) {
218 			printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%ld\n", rc);
219 			printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
220 			printk("\ttcenum  = 0x%lx\n", (u64)tcenum);
221 			show_stack(current, (unsigned long *)__get_SP());
222 		}
223 
224 		tcenum++;
225 	}
226 }
227 
228 
229 static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long npages)
230 {
231 	u64 rc;
232 
233 	tcenum <<= TCE_PAGE_FACTOR;
234 	npages <<= TCE_PAGE_FACTOR;
235 
236 	rc = plpar_tce_stuff((u64)tbl->it_index, (u64)tcenum << 12, 0, npages);
237 
238 	if (rc && printk_ratelimit()) {
239 		printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
240 		printk("\trc      = %ld\n", rc);
241 		printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
242 		printk("\tnpages  = 0x%lx\n", (u64)npages);
243 		show_stack(current, (unsigned long *)__get_SP());
244 	}
245 }
246 
247 static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)
248 {
249 	u64 rc;
250 	unsigned long tce_ret;
251 
252 	tcenum <<= TCE_PAGE_FACTOR;
253 	rc = plpar_tce_get((u64)tbl->it_index, (u64)tcenum << 12, &tce_ret);
254 
255 	if (rc && printk_ratelimit()) {
256 		printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%ld\n",
257 			rc);
258 		printk("\tindex   = 0x%lx\n", (u64)tbl->it_index);
259 		printk("\ttcenum  = 0x%lx\n", (u64)tcenum);
260 		show_stack(current, (unsigned long *)__get_SP());
261 	}
262 
263 	return tce_ret;
264 }
265 
266 static void iommu_table_setparms(struct pci_controller *phb,
267 				 struct device_node *dn,
268 				 struct iommu_table *tbl)
269 {
270 	struct device_node *node;
271 	unsigned long *basep;
272 	unsigned int *sizep;
273 
274 	node = (struct device_node *)phb->arch_data;
275 
276 	basep = (unsigned long *)get_property(node, "linux,tce-base", NULL);
277 	sizep = (unsigned int *)get_property(node, "linux,tce-size", NULL);
278 	if (basep == NULL || sizep == NULL) {
279 		printk(KERN_ERR "PCI_DMA: iommu_table_setparms: %s has "
280 				"missing tce entries !\n", dn->full_name);
281 		return;
282 	}
283 
284 	tbl->it_base = (unsigned long)__va(*basep);
285 
286 #ifndef CONFIG_CRASH_DUMP
287 	memset((void *)tbl->it_base, 0, *sizep);
288 #endif
289 
290 	tbl->it_busno = phb->bus->number;
291 
292 	/* Units of tce entries */
293 	tbl->it_offset = phb->dma_window_base_cur >> PAGE_SHIFT;
294 
295 	/* Test if we are going over 2GB of DMA space */
296 	if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
297 		udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
298 		panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
299 	}
300 
301 	phb->dma_window_base_cur += phb->dma_window_size;
302 
303 	/* Set the tce table size - measured in entries */
304 	tbl->it_size = phb->dma_window_size >> PAGE_SHIFT;
305 
306 	tbl->it_index = 0;
307 	tbl->it_blocksize = 16;
308 	tbl->it_type = TCE_PCI;
309 }
310 
311 /*
312  * iommu_table_setparms_lpar
313  *
314  * Function: On pSeries LPAR systems, return TCE table info, given a pci bus.
315  */
316 static void iommu_table_setparms_lpar(struct pci_controller *phb,
317 				      struct device_node *dn,
318 				      struct iommu_table *tbl,
319 				      unsigned char *dma_window)
320 {
321 	unsigned long offset, size;
322 
323 	tbl->it_busno  = PCI_DN(dn)->bussubno;
324 	of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
325 
326 	tbl->it_base   = 0;
327 	tbl->it_blocksize  = 16;
328 	tbl->it_type = TCE_PCI;
329 	tbl->it_offset = offset >> PAGE_SHIFT;
330 	tbl->it_size = size >> PAGE_SHIFT;
331 }
332 
333 static void iommu_bus_setup_pSeries(struct pci_bus *bus)
334 {
335 	struct device_node *dn;
336 	struct iommu_table *tbl;
337 	struct device_node *isa_dn, *isa_dn_orig;
338 	struct device_node *tmp;
339 	struct pci_dn *pci;
340 	int children;
341 
342 	DBG("iommu_bus_setup_pSeries, bus %p, bus->self %p\n", bus, bus->self);
343 
344 	dn = pci_bus_to_OF_node(bus);
345 	pci = PCI_DN(dn);
346 
347 	if (bus->self) {
348 		/* This is not a root bus, any setup will be done for the
349 		 * device-side of the bridge in iommu_dev_setup_pSeries().
350 		 */
351 		return;
352 	}
353 
354 	/* Check if the ISA bus on the system is under
355 	 * this PHB.
356 	 */
357 	isa_dn = isa_dn_orig = of_find_node_by_type(NULL, "isa");
358 
359 	while (isa_dn && isa_dn != dn)
360 		isa_dn = isa_dn->parent;
361 
362 	if (isa_dn_orig)
363 		of_node_put(isa_dn_orig);
364 
365 	/* Count number of direct PCI children of the PHB. */
366 	for (children = 0, tmp = dn->child; tmp; tmp = tmp->sibling)
367 		children++;
368 
369 	DBG("Children: %d\n", children);
370 
371 	/* Calculate amount of DMA window per slot. Each window must be
372 	 * a power of two (due to pci_alloc_consistent requirements).
373 	 *
374 	 * Keep 256MB aside for PHBs with ISA.
375 	 */
376 
377 	if (!isa_dn) {
378 		/* No ISA/IDE - just set window size and return */
379 		pci->phb->dma_window_size = 0x80000000ul; /* To be divided */
380 
381 		while (pci->phb->dma_window_size * children > 0x80000000ul)
382 			pci->phb->dma_window_size >>= 1;
383 		DBG("No ISA/IDE, window size is 0x%lx\n",
384 			pci->phb->dma_window_size);
385 		pci->phb->dma_window_base_cur = 0;
386 
387 		return;
388 	}
389 
390 	/* If we have ISA, then we probably have an IDE
391 	 * controller too. Allocate a 128MB table but
392 	 * skip the first 128MB to avoid stepping on ISA
393 	 * space.
394 	 */
395 	pci->phb->dma_window_size = 0x8000000ul;
396 	pci->phb->dma_window_base_cur = 0x8000000ul;
397 
398 	tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
399 			   pci->phb->node);
400 
401 	iommu_table_setparms(pci->phb, dn, tbl);
402 	pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
403 
404 	/* Divide the rest (1.75GB) among the children */
405 	pci->phb->dma_window_size = 0x80000000ul;
406 	while (pci->phb->dma_window_size * children > 0x70000000ul)
407 		pci->phb->dma_window_size >>= 1;
408 
409 	DBG("ISA/IDE, window size is 0x%lx\n", pci->phb->dma_window_size);
410 
411 }
412 
413 
414 static void iommu_bus_setup_pSeriesLP(struct pci_bus *bus)
415 {
416 	struct iommu_table *tbl;
417 	struct device_node *dn, *pdn;
418 	struct pci_dn *ppci;
419 	unsigned char *dma_window = NULL;
420 
421 	DBG("iommu_bus_setup_pSeriesLP, bus %p, bus->self %p\n", bus, bus->self);
422 
423 	dn = pci_bus_to_OF_node(bus);
424 
425 	/* Find nearest ibm,dma-window, walking up the device tree */
426 	for (pdn = dn; pdn != NULL; pdn = pdn->parent) {
427 		dma_window = get_property(pdn, "ibm,dma-window", NULL);
428 		if (dma_window != NULL)
429 			break;
430 	}
431 
432 	if (dma_window == NULL) {
433 		DBG("iommu_bus_setup_pSeriesLP: bus %s seems to have no ibm,dma-window property\n", dn->full_name);
434 		return;
435 	}
436 
437 	ppci = PCI_DN(pdn);
438 	if (!ppci->iommu_table) {
439 		/* Bussubno hasn't been copied yet.
440 		 * Do it now because iommu_table_setparms_lpar needs it.
441 		 */
442 
443 		ppci->bussubno = bus->number;
444 
445 		tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
446 				   ppci->phb->node);
447 
448 		iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
449 
450 		ppci->iommu_table = iommu_init_table(tbl, ppci->phb->node);
451 	}
452 
453 	if (pdn != dn)
454 		PCI_DN(dn)->iommu_table = ppci->iommu_table;
455 }
456 
457 
458 static void iommu_dev_setup_pSeries(struct pci_dev *dev)
459 {
460 	struct device_node *dn, *mydn;
461 	struct iommu_table *tbl;
462 
463 	DBG("iommu_dev_setup_pSeries, dev %p (%s)\n", dev, pci_name(dev));
464 
465 	mydn = dn = pci_device_to_OF_node(dev);
466 
467 	/* If we're the direct child of a root bus, then we need to allocate
468 	 * an iommu table ourselves. The bus setup code should have setup
469 	 * the window sizes already.
470 	 */
471 	if (!dev->bus->self) {
472 		DBG(" --> first child, no bridge. Allocating iommu table.\n");
473 		tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
474 				   PCI_DN(dn)->phb->node);
475 		iommu_table_setparms(PCI_DN(dn)->phb, dn, tbl);
476 		PCI_DN(dn)->iommu_table = iommu_init_table(tbl,
477 						PCI_DN(dn)->phb->node);
478 
479 		return;
480 	}
481 
482 	/* If this device is further down the bus tree, search upwards until
483 	 * an already allocated iommu table is found and use that.
484 	 */
485 
486 	while (dn && PCI_DN(dn) && PCI_DN(dn)->iommu_table == NULL)
487 		dn = dn->parent;
488 
489 	if (dn && PCI_DN(dn)) {
490 		PCI_DN(mydn)->iommu_table = PCI_DN(dn)->iommu_table;
491 	} else {
492 		DBG("iommu_dev_setup_pSeries, dev %p (%s) has no iommu table\n", dev, pci_name(dev));
493 	}
494 }
495 
496 static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long action, void *node)
497 {
498 	int err = NOTIFY_OK;
499 	struct device_node *np = node;
500 	struct pci_dn *pci = PCI_DN(np);
501 
502 	switch (action) {
503 	case PSERIES_RECONFIG_REMOVE:
504 		if (pci && pci->iommu_table &&
505 		    get_property(np, "ibm,dma-window", NULL))
506 			iommu_free_table(np);
507 		break;
508 	default:
509 		err = NOTIFY_DONE;
510 		break;
511 	}
512 	return err;
513 }
514 
515 static struct notifier_block iommu_reconfig_nb = {
516 	.notifier_call = iommu_reconfig_notifier,
517 };
518 
519 static void iommu_dev_setup_pSeriesLP(struct pci_dev *dev)
520 {
521 	struct device_node *pdn, *dn;
522 	struct iommu_table *tbl;
523 	unsigned char *dma_window = NULL;
524 	struct pci_dn *pci;
525 
526 	DBG("iommu_dev_setup_pSeriesLP, dev %p (%s)\n", dev, pci_name(dev));
527 
528 	/* dev setup for LPAR is a little tricky, since the device tree might
529 	 * contain the dma-window properties per-device and not neccesarily
530 	 * for the bus. So we need to search upwards in the tree until we
531 	 * either hit a dma-window property, OR find a parent with a table
532 	 * already allocated.
533 	 */
534 	dn = pci_device_to_OF_node(dev);
535 
536 	for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
537 	     pdn = pdn->parent) {
538 		dma_window = get_property(pdn, "ibm,dma-window", NULL);
539 		if (dma_window)
540 			break;
541 	}
542 
543 	/* Check for parent == NULL so we don't try to setup the empty EADS
544 	 * slots on POWER4 machines.
545 	 */
546 	if (dma_window == NULL || pdn->parent == NULL) {
547 		DBG("No dma window for device, linking to parent\n");
548 		PCI_DN(dn)->iommu_table = PCI_DN(pdn)->iommu_table;
549 		return;
550 	} else {
551 		DBG("Found DMA window, allocating table\n");
552 	}
553 
554 	pci = PCI_DN(pdn);
555 	if (!pci->iommu_table) {
556 		/* iommu_table_setparms_lpar needs bussubno. */
557 		pci->bussubno = pci->phb->bus->number;
558 
559 		tbl = kmalloc_node(sizeof(struct iommu_table), GFP_KERNEL,
560 				   pci->phb->node);
561 
562 		iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
563 
564 		pci->iommu_table = iommu_init_table(tbl, pci->phb->node);
565 	}
566 
567 	if (pdn != dn)
568 		PCI_DN(dn)->iommu_table = pci->iommu_table;
569 }
570 
571 static void iommu_bus_setup_null(struct pci_bus *b) { }
572 static void iommu_dev_setup_null(struct pci_dev *d) { }
573 
574 /* These are called very early. */
575 void iommu_init_early_pSeries(void)
576 {
577 	if (of_chosen && get_property(of_chosen, "linux,iommu-off", NULL)) {
578 		/* Direct I/O, IOMMU off */
579 		ppc_md.iommu_dev_setup = iommu_dev_setup_null;
580 		ppc_md.iommu_bus_setup = iommu_bus_setup_null;
581 		pci_direct_iommu_init();
582 
583 		return;
584 	}
585 
586 	if (firmware_has_feature(FW_FEATURE_LPAR)) {
587 		if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
588 			ppc_md.tce_build = tce_buildmulti_pSeriesLP;
589 			ppc_md.tce_free	 = tce_freemulti_pSeriesLP;
590 		} else {
591 			ppc_md.tce_build = tce_build_pSeriesLP;
592 			ppc_md.tce_free	 = tce_free_pSeriesLP;
593 		}
594 		ppc_md.tce_get   = tce_get_pSeriesLP;
595 		ppc_md.iommu_bus_setup = iommu_bus_setup_pSeriesLP;
596 		ppc_md.iommu_dev_setup = iommu_dev_setup_pSeriesLP;
597 	} else {
598 		ppc_md.tce_build = tce_build_pSeries;
599 		ppc_md.tce_free  = tce_free_pSeries;
600 		ppc_md.tce_get   = tce_get_pseries;
601 		ppc_md.iommu_bus_setup = iommu_bus_setup_pSeries;
602 		ppc_md.iommu_dev_setup = iommu_dev_setup_pSeries;
603 	}
604 
605 
606 	pSeries_reconfig_notifier_register(&iommu_reconfig_nb);
607 
608 	pci_iommu_init();
609 }
610 
611