3333#include <linux/bitmap.h>
3434#include <linux/iommu-helper.h>
3535#include <linux/crash_dump.h>
36+ #include <linux/hash.h>
3637#include <asm/io.h>
3738#include <asm/prom.h>
3839#include <asm/iommu.h>
@@ -58,6 +59,26 @@ static int __init setup_iommu(char *str)
5859
5960__setup ("iommu=" , setup_iommu );
6061
62+ static DEFINE_PER_CPU (unsigned int , iommu_pool_hash ) ;
63+
64+ /*
65+ * We precalculate the hash to avoid doing it on every allocation.
66+ *
67+ * The hash is important to spread CPUs across all the pools. For example,
68+ * on a POWER7 with 4 way SMT we want interrupts on the primary threads and
69+ * with 4 pools all primary threads would map to the same pool.
70+ */
71+ static int __init setup_iommu_pool_hash (void )
72+ {
73+ unsigned int i ;
74+
75+ for_each_possible_cpu (i )
76+ per_cpu (iommu_pool_hash , i ) = hash_32 (i , IOMMU_POOL_HASHBITS );
77+
78+ return 0 ;
79+ }
80+ subsys_initcall (setup_iommu_pool_hash );
81+
6182static unsigned long iommu_range_alloc (struct device * dev ,
6283 struct iommu_table * tbl ,
6384 unsigned long npages ,
@@ -72,6 +93,8 @@ static unsigned long iommu_range_alloc(struct device *dev,
7293 unsigned long align_mask ;
7394 unsigned long boundary_size ;
7495 unsigned long flags ;
96+ unsigned int pool_nr ;
97+ struct iommu_pool * pool ;
7598
7699 align_mask = 0xffffffffffffffffl >> (64 - align_order );
77100
@@ -84,38 +107,46 @@ static unsigned long iommu_range_alloc(struct device *dev,
84107 return DMA_ERROR_CODE ;
85108 }
86109
87- spin_lock_irqsave (& (tbl -> it_lock ), flags );
110+ /*
111+ * We don't need to disable preemption here because any CPU can
112+ * safely use any IOMMU pool.
113+ */
114+ pool_nr = __raw_get_cpu_var (iommu_pool_hash ) & (tbl -> nr_pools - 1 );
88115
89- if (handle && * handle )
90- start = * handle ;
116+ if (largealloc )
117+ pool = & ( tbl -> large_pool ) ;
91118 else
92- start = largealloc ? tbl -> it_largehint : tbl -> it_hint ;
119+ pool = & ( tbl -> pools [ pool_nr ]) ;
93120
94- /* Use only half of the table for small allocs (15 pages or less) */
95- limit = largealloc ? tbl -> it_size : tbl -> it_halfpoint ;
121+ spin_lock_irqsave (& (pool -> lock ), flags );
122+
123+ again :
124+ if ((pass == 0 ) && handle && * handle )
125+ start = * handle ;
126+ else
127+ start = pool -> hint ;
96128
97- if (largealloc && start < tbl -> it_halfpoint )
98- start = tbl -> it_halfpoint ;
129+ limit = pool -> end ;
99130
100131 /* The case below can happen if we have a small segment appended
101132 * to a large, or when the previous alloc was at the very end of
102133 * the available space. If so, go back to the initial start.
103134 */
104135 if (start >= limit )
105- start = largealloc ? tbl -> it_largehint : tbl -> it_hint ;
106-
107- again :
136+ start = pool -> start ;
108137
109138 if (limit + tbl -> it_offset > mask ) {
110139 limit = mask - tbl -> it_offset + 1 ;
111140 /* If we're constrained on address range, first try
112141 * at the masked hint to avoid O(n) search complexity,
113- * but on second pass, start at 0.
142+ * but on second pass, start at 0 in pool 0 .
114143 */
115- if ((start & mask ) >= limit || pass > 0 )
116- start = 0 ;
117- else
144+ if ((start & mask ) >= limit || pass > 0 ) {
145+ pool = & (tbl -> pools [0 ]);
146+ start = pool -> start ;
147+ } else {
118148 start &= mask ;
149+ }
119150 }
120151
121152 if (dev )
@@ -129,17 +160,25 @@ static unsigned long iommu_range_alloc(struct device *dev,
129160 tbl -> it_offset , boundary_size >> IOMMU_PAGE_SHIFT ,
130161 align_mask );
131162 if (n == -1 ) {
132- if (likely (pass < 2 )) {
133- /* First failure, just rescan the half of the table.
134- * Second failure, rescan the other half of the table.
135- */
136- start = (largealloc ^ pass ) ? tbl -> it_halfpoint : 0 ;
137- limit = pass ? tbl -> it_size : limit ;
163+ if (likely (pass == 0 )) {
164+ /* First try the pool from the start */
165+ pool -> hint = pool -> start ;
138166 pass ++ ;
139167 goto again ;
168+
169+ } else if (pass <= tbl -> nr_pools ) {
170+ /* Now try scanning all the other pools */
171+ spin_unlock (& (pool -> lock ));
172+ pool_nr = (pool_nr + 1 ) & (tbl -> nr_pools - 1 );
173+ pool = & tbl -> pools [pool_nr ];
174+ spin_lock (& (pool -> lock ));
175+ pool -> hint = pool -> start ;
176+ pass ++ ;
177+ goto again ;
178+
140179 } else {
141- /* Third failure, give up */
142- spin_unlock_irqrestore (& (tbl -> it_lock ), flags );
180+ /* Give up */
181+ spin_unlock_irqrestore (& (pool -> lock ), flags );
143182 return DMA_ERROR_CODE ;
144183 }
145184 }
@@ -149,18 +188,19 @@ static unsigned long iommu_range_alloc(struct device *dev,
149188 /* Bump the hint to a new block for small allocs. */
150189 if (largealloc ) {
151190 /* Don't bump to new block to avoid fragmentation */
152- tbl -> it_largehint = end ;
191+ pool -> hint = end ;
153192 } else {
154193 /* Overflow will be taken care of at the next allocation */
155- tbl -> it_hint = (end + tbl -> it_blocksize - 1 ) &
194+ pool -> hint = (end + tbl -> it_blocksize - 1 ) &
156195 ~(tbl -> it_blocksize - 1 );
157196 }
158197
159198 /* Update handle for SG allocations */
160199 if (handle )
161200 * handle = end ;
162201
163- spin_unlock_irqrestore (& (tbl -> it_lock ), flags );
202+ spin_unlock_irqrestore (& (pool -> lock ), flags );
203+
164204 return n ;
165205}
166206
@@ -235,23 +275,45 @@ static bool iommu_free_check(struct iommu_table *tbl, dma_addr_t dma_addr,
235275 return true;
236276}
237277
278+ static struct iommu_pool * get_pool (struct iommu_table * tbl ,
279+ unsigned long entry )
280+ {
281+ struct iommu_pool * p ;
282+ unsigned long largepool_start = tbl -> large_pool .start ;
283+
284+ /* The large pool is the last pool at the top of the table */
285+ if (entry >= largepool_start ) {
286+ p = & tbl -> large_pool ;
287+ } else {
288+ unsigned int pool_nr = entry / tbl -> poolsize ;
289+
290+ BUG_ON (pool_nr > tbl -> nr_pools );
291+ p = & tbl -> pools [pool_nr ];
292+ }
293+
294+ return p ;
295+ }
296+
238297static void __iommu_free (struct iommu_table * tbl , dma_addr_t dma_addr ,
239298 unsigned int npages )
240299{
241300 unsigned long entry , free_entry ;
242301 unsigned long flags ;
302+ struct iommu_pool * pool ;
243303
244304 entry = dma_addr >> IOMMU_PAGE_SHIFT ;
245305 free_entry = entry - tbl -> it_offset ;
246306
307+ pool = get_pool (tbl , free_entry );
308+
247309 if (!iommu_free_check (tbl , dma_addr , npages ))
248310 return ;
249311
250312 ppc_md .tce_free (tbl , entry , npages );
251313
252- spin_lock_irqsave (& (tbl -> it_lock ), flags );
314+ spin_lock_irqsave (& (pool -> lock ), flags );
253315 bitmap_clear (tbl -> it_map , free_entry , npages );
254- spin_unlock_irqrestore (& (tbl -> it_lock ), flags );
316+ spin_unlock_irqrestore (& (pool -> lock ), flags );
255317}
256318
257319static void iommu_free (struct iommu_table * tbl , dma_addr_t dma_addr ,
@@ -493,9 +555,8 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
493555 unsigned long sz ;
494556 static int welcomed = 0 ;
495557 struct page * page ;
496-
497- /* Set aside 1/4 of the table for large allocations. */
498- tbl -> it_halfpoint = tbl -> it_size * 3 / 4 ;
558+ unsigned int i ;
559+ struct iommu_pool * p ;
499560
500561 /* number of bytes needed for the bitmap */
501562 sz = (tbl -> it_size + 7 ) >> 3 ;
@@ -514,9 +575,28 @@ struct iommu_table *iommu_init_table(struct iommu_table *tbl, int nid)
514575 if (tbl -> it_offset == 0 )
515576 set_bit (0 , tbl -> it_map );
516577
517- tbl -> it_hint = 0 ;
518- tbl -> it_largehint = tbl -> it_halfpoint ;
519- spin_lock_init (& tbl -> it_lock );
578+ /* We only split the IOMMU table if we have 1GB or more of space */
579+ if ((tbl -> it_size << IOMMU_PAGE_SHIFT ) >= (1UL * 1024 * 1024 * 1024 ))
580+ tbl -> nr_pools = IOMMU_NR_POOLS ;
581+ else
582+ tbl -> nr_pools = 1 ;
583+
584+ /* We reserve the top 1/4 of the table for large allocations */
585+ tbl -> poolsize = (tbl -> it_size * 3 / 4 ) / IOMMU_NR_POOLS ;
586+
587+ for (i = 0 ; i < IOMMU_NR_POOLS ; i ++ ) {
588+ p = & tbl -> pools [i ];
589+ spin_lock_init (& (p -> lock ));
590+ p -> start = tbl -> poolsize * i ;
591+ p -> hint = p -> start ;
592+ p -> end = p -> start + tbl -> poolsize ;
593+ }
594+
595+ p = & tbl -> large_pool ;
596+ spin_lock_init (& (p -> lock ));
597+ p -> start = tbl -> poolsize * i ;
598+ p -> hint = p -> start ;
599+ p -> end = tbl -> it_size ;
520600
521601 iommu_table_clear (tbl );
522602
0 commit comments