2 * Derived from arch/powerpc/platforms/pseries/iommu.c
4 * Copyright (C) IBM Corporation, 2006
6 * Author: Jon Mason <jdmason@us.ibm.com>
7 * Author: Muli Ben-Yehuda <muli@il.ibm.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/types.h>
25 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/string.h>
29 #include <linux/pci.h>
30 #include <linux/dma-mapping.h>
31 #include <linux/bootmem.h>
33 #include <asm/calgary.h>
34 #include <asm/proto.h>
36 /* flush a tce at 'tceaddr' to main memory */
37 static inline void flush_tce(void* tceaddr)
39 /* a single tce can't cross a cache line */
41 asm volatile("clflush (%0)" :: "r" (tceaddr));
43 asm volatile("wbinvd":::"memory");
46 void tce_build(struct iommu_table *tbl, unsigned long index,
47 unsigned int npages, unsigned long uaddr, int direction)
53 t = (1 << TCE_READ_SHIFT);
54 if (direction != DMA_TO_DEVICE)
55 t |= (1 << TCE_WRITE_SHIFT);
57 tp = ((u64*)tbl->it_base) + index;
60 rpn = (virt_to_bus((void*)uaddr)) >> PAGE_SHIFT;
62 t |= (rpn << TCE_RPN_SHIFT);
72 void tce_free(struct iommu_table *tbl, long index, unsigned int npages)
76 tp = ((u64*)tbl->it_base) + index;
85 static inline unsigned int table_size_to_number_of_entries(unsigned char size)
88 * size is the order of the table, 0-7
89 * smallest table is 8K entries, so shift result by 13 to
92 return (1 << size) << 13;
95 static int tce_table_setparms(struct pci_dev *dev, struct iommu_table *tbl)
97 unsigned int bitmapsz;
98 unsigned long bmppages;
101 tbl->it_busno = dev->bus->number;
103 /* set the tce table size - measured in entries */
104 tbl->it_size = table_size_to_number_of_entries(specified_table_size);
106 tbl->it_base = (unsigned long)tce_table_kva[dev->bus->number];
108 printk(KERN_ERR "Calgary: iommu_table_setparms: "
109 "no table allocated?!\n");
115 * number of bytes needed for the bitmap size in number of
116 * entries; we need one bit per entry
118 bitmapsz = tbl->it_size / BITS_PER_BYTE;
119 bmppages = __get_free_pages(GFP_KERNEL, get_order(bitmapsz));
121 printk(KERN_ERR "Calgary: cannot allocate bitmap\n");
126 tbl->it_map = (unsigned long*)bmppages;
128 memset(tbl->it_map, 0, bitmapsz);
132 spin_lock_init(&tbl->it_lock);
140 int build_tce_table(struct pci_dev *dev, void __iomem *bbar)
142 struct iommu_table *tbl;
146 printk(KERN_ERR "Calgary: dev %p has sysdata %p\n",
151 tbl = kzalloc(sizeof(struct iommu_table), GFP_KERNEL);
153 printk(KERN_ERR "Calgary: error allocating iommu_table\n");
158 ret = tce_table_setparms(dev, tbl);
162 tce_free(tbl, 0, tbl->it_size);
167 * NUMA is already using the bus's sysdata pointer, so we use
168 * the bus's pci_dev's sysdata instead.
180 void* alloc_tce_table(void)
184 size = table_size_to_number_of_entries(specified_table_size);
185 size *= TCE_ENTRY_SIZE;
187 return __alloc_bootmem_low(size, size, 0);
190 void free_tce_table(void *tbl)
197 size = table_size_to_number_of_entries(specified_table_size);
198 size *= TCE_ENTRY_SIZE;
200 free_bootmem(__pa(tbl), size);