@@ -104,7 +104,7 @@ static int is_xen_swiotlb_buffer(struct device *dev, dma_addr_t dma_addr)
104104 return 0 ;
105105}
106106
107- static int xen_swiotlb_fixup (void * buf , unsigned long nslabs )
107+ int xen_swiotlb_fixup (void * buf , unsigned long nslabs )
108108{
109109 int rc ;
110110 unsigned int order = get_order (IO_TLB_SEGSIZE << IO_TLB_SHIFT );
@@ -130,132 +130,6 @@ static int xen_swiotlb_fixup(void *buf, unsigned long nslabs)
130130 return 0 ;
131131}
132132
133- enum xen_swiotlb_err {
134- XEN_SWIOTLB_UNKNOWN = 0 ,
135- XEN_SWIOTLB_ENOMEM ,
136- XEN_SWIOTLB_EFIXUP
137- };
138-
139- static const char * xen_swiotlb_error (enum xen_swiotlb_err err )
140- {
141- switch (err ) {
142- case XEN_SWIOTLB_ENOMEM :
143- return "Cannot allocate Xen-SWIOTLB buffer\n" ;
144- case XEN_SWIOTLB_EFIXUP :
145- return "Failed to get contiguous memory for DMA from Xen!\n" \
146- "You either: don't have the permissions, do not have" \
147- " enough free memory under 4GB, or the hypervisor memory" \
148- " is too fragmented!" ;
149- default :
150- break ;
151- }
152- return "" ;
153- }
154-
155- int xen_swiotlb_init (void )
156- {
157- enum xen_swiotlb_err m_ret = XEN_SWIOTLB_UNKNOWN ;
158- unsigned long bytes = swiotlb_size_or_default ();
159- unsigned long nslabs = bytes >> IO_TLB_SHIFT ;
160- unsigned int order , repeat = 3 ;
161- int rc = - ENOMEM ;
162- char * start ;
163-
164- if (io_tlb_default_mem .nslabs ) {
165- pr_warn ("swiotlb buffer already initialized\n" );
166- return - EEXIST ;
167- }
168-
169- retry :
170- m_ret = XEN_SWIOTLB_ENOMEM ;
171- order = get_order (bytes );
172-
173- /*
174- * Get IO TLB memory from any location.
175- */
176- #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
177- #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
178- while ((SLABS_PER_PAGE << order ) > IO_TLB_MIN_SLABS ) {
179- start = (void * )xen_get_swiotlb_free_pages (order );
180- if (start )
181- break ;
182- order -- ;
183- }
184- if (!start )
185- goto exit ;
186- if (order != get_order (bytes )) {
187- pr_warn ("Warning: only able to allocate %ld MB for software IO TLB\n" ,
188- (PAGE_SIZE << order ) >> 20 );
189- nslabs = SLABS_PER_PAGE << order ;
190- bytes = nslabs << IO_TLB_SHIFT ;
191- }
192-
193- /*
194- * And replace that memory with pages under 4GB.
195- */
196- rc = xen_swiotlb_fixup (start , nslabs );
197- if (rc ) {
198- free_pages ((unsigned long )start , order );
199- m_ret = XEN_SWIOTLB_EFIXUP ;
200- goto error ;
201- }
202- rc = swiotlb_late_init_with_tbl (start , nslabs );
203- if (rc )
204- return rc ;
205- return 0 ;
206- error :
207- if (nslabs > 1024 && repeat -- ) {
208- /* Min is 2MB */
209- nslabs = max (1024UL , ALIGN (nslabs >> 1 , IO_TLB_SEGSIZE ));
210- bytes = nslabs << IO_TLB_SHIFT ;
211- pr_info ("Lowering to %luMB\n" , bytes >> 20 );
212- goto retry ;
213- }
214- exit :
215- pr_err ("%s (rc:%d)\n" , xen_swiotlb_error (m_ret ), rc );
216- return rc ;
217- }
218-
219- #ifdef CONFIG_X86
220- void __init xen_swiotlb_init_early (void )
221- {
222- unsigned long bytes = swiotlb_size_or_default ();
223- unsigned long nslabs = bytes >> IO_TLB_SHIFT ;
224- unsigned int repeat = 3 ;
225- char * start ;
226- int rc ;
227-
228- retry :
229- /*
230- * Get IO TLB memory from any location.
231- */
232- start = memblock_alloc (PAGE_ALIGN (bytes ),
233- IO_TLB_SEGSIZE << IO_TLB_SHIFT );
234- if (!start )
235- panic ("%s: Failed to allocate %lu bytes\n" ,
236- __func__ , PAGE_ALIGN (bytes ));
237-
238- /*
239- * And replace that memory with pages under 4GB.
240- */
241- rc = xen_swiotlb_fixup (start , nslabs );
242- if (rc ) {
243- memblock_free (start , PAGE_ALIGN (bytes ));
244- if (nslabs > 1024 && repeat -- ) {
245- /* Min is 2MB */
246- nslabs = max (1024UL , ALIGN (nslabs >> 1 , IO_TLB_SEGSIZE ));
247- bytes = nslabs << IO_TLB_SHIFT ;
248- pr_info ("Lowering to %luMB\n" , bytes >> 20 );
249- goto retry ;
250- }
251- panic ("%s (rc:%d)" , xen_swiotlb_error (XEN_SWIOTLB_EFIXUP ), rc );
252- }
253-
254- if (swiotlb_init_with_tbl (start , nslabs , SWIOTLB_VERBOSE ))
255- panic ("Cannot allocate SWIOTLB buffer" );
256- }
257- #endif /* CONFIG_X86 */
258-
259133static void *
260134xen_swiotlb_alloc_coherent (struct device * hwdev , size_t size ,
261135 dma_addr_t * dma_handle , gfp_t flags ,
0 commit comments