Blame view

lib/swiotlb.c 25.6 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
  /*
   * Dynamic DMA mapping support.
   *
563aaf064   Jan Beulich   [IA64] swiotlb cl...
4
   * This implementation is a fallback for platforms that do not support
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
6
7
8
9
10
11
12
13
   * I/O TLBs (aka DMA address translation hardware).
   * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
   * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
   * Copyright (C) 2000, 2003 Hewlett-Packard Co
   *	David Mosberger-Tang <davidm@hpl.hp.com>
   *
   * 03/05/07 davidm	Switch from PCI-DMA to generic device DMA API.
   * 00/12/13 davidm	Rename to swiotlb.c and add mark_clean() to avoid
   *			unnecessary i-cache flushing.
569c8bf5d   John W. Linville   [PATCH] swiotlb: ...
14
15
16
   * 04/07/.. ak		Better overflow handling. Assorted fixes.
   * 05/09/10 linville	Add support for syncing ranges, support syncing for
   *			DMA_BIDIRECTIONAL mappings, miscellaneous cleanup.
fb05a3792   Becky Bruce   swiotlb: add supp...
17
   * 08/12/11 beckyb	Add highmem support
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
20
   */
  
  #include <linux/cache.h>
17e5ad6c0   Tony Luck   [PATCH] Removed r...
21
  #include <linux/dma-mapping.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
23
  #include <linux/mm.h>
  #include <linux/module.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
25
  #include <linux/spinlock.h>
  #include <linux/string.h>
0016fdee9   Ian Campbell   swiotlb: move som...
26
  #include <linux/swiotlb.h>
fb05a3792   Becky Bruce   swiotlb: add supp...
27
  #include <linux/pfn.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
  #include <linux/types.h>
  #include <linux/ctype.h>
ef9b18935   Jeremy Fitzhardinge   swiotlb: support ...
30
  #include <linux/highmem.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
31
  #include <linux/gfp.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
32
33
  
  #include <asm/io.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
  #include <asm/dma.h>
17e5ad6c0   Tony Luck   [PATCH] Removed r...
35
  #include <asm/scatterlist.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
36
37
38
  
  #include <linux/init.h>
  #include <linux/bootmem.h>
a85225092   FUJITA Tomonori   swiotlb: use iomm...
39
  #include <linux/iommu-helper.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
41
42
  
  #define OFFSET(val,align) ((unsigned long)	\
  	                   ( (val) & ( (align) - 1)))
0b9afede3   Alex Williamson   [IA64] more robus...
43
44
45
46
47
48
49
50
  #define SLABS_PER_PAGE (1 << (PAGE_SHIFT - IO_TLB_SHIFT))
  
  /*
   * Minimum IO TLB size to bother booting with.  Systems with mainly
   * 64bit capable cards will only lightly use the swiotlb.  If we can't
   * allocate a contiguous 1MB, we're probably in trouble anyway.
   */
  #define IO_TLB_MIN_SLABS ((1<<20) >> IO_TLB_SHIFT)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
52
53
  int swiotlb_force;
  
  /*
bfc5501f6   Konrad Rzeszutek Wilk   swiotlb: Make int...
54
55
   * Used to do a quick range check in swiotlb_tbl_unmap_single and
   * swiotlb_tbl_sync_single_*, to see if the memory was in fact allocated by this
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
56
57
58
59
60
   * API.
   */
  static char *io_tlb_start, *io_tlb_end;
  
  /*
b595076a1   Uwe Kleine-König   tree-wide: fix co...
61
   * The number of IO TLB blocks (in groups of 64) between io_tlb_start and
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
63
64
65
66
67
68
69
   * io_tlb_end.  This is command line adjustable via setup_io_tlb_npages.
   */
  static unsigned long io_tlb_nslabs;
  
  /*
   * When the IOMMU overflows we return a fallback buffer. This sets the size.
   */
  static unsigned long io_tlb_overflow = 32*1024;
03620b2d7   FUJITA Tomonori   swiotlb: make io_...
70
  static void *io_tlb_overflow_buffer;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
72
73
74
75
76
77
78
79
80
81
82
  
  /*
   * This is a free list describing the number of free entries available from
   * each index
   */
  static unsigned int *io_tlb_list;
  static unsigned int io_tlb_index;
  
  /*
   * We need to save away the original address corresponding to a mapped entry
   * for the sync operations.
   */
bc40ac669   Becky Bruce   swiotlb: store ph...
83
  static phys_addr_t *io_tlb_orig_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
84
85
86
87
88
  
  /*
   * Protect the above data structures in the map and unmap calls
   */
  static DEFINE_SPINLOCK(io_tlb_lock);
5740afdb6   FUJITA Tomonori   swiotlb: Add swio...
89
  static int late_alloc;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
93
  static int __init
  setup_io_tlb_npages(char *str)
  {
  	if (isdigit(*str)) {
e8579e72c   Alex Williamson   [IA64, X86_64] fi...
94
  		io_tlb_nslabs = simple_strtoul(str, &str, 0);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
95
96
97
98
99
  		/* avoid tail segment of size < IO_TLB_SEGSIZE */
  		io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  	}
  	if (*str == ',')
  		++str;
b18485e7a   FUJITA Tomonori   swiotlb: Remove t...
100
  	if (!strcmp(str, "force"))
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
101
  		swiotlb_force = 1;
b18485e7a   FUJITA Tomonori   swiotlb: Remove t...
102

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
105
106
  	return 1;
  }
  __setup("swiotlb=", setup_io_tlb_npages);
  /* make io_tlb_overflow tunable too? */
02ca646e7   FUJITA Tomonori   swiotlb: remove u...
107
  /* Note that this doesn't work with highmem page */
70a7d3cc1   Jeremy Fitzhardinge   swiotlb: add hwde...
108
109
  static dma_addr_t swiotlb_virt_to_bus(struct device *hwdev,
  				      volatile void *address)
e08e1f7ad   Ian Campbell   swiotlb: allow ar...
110
  {
862d196b2   FUJITA Tomonori   swiotlb: use phys...
111
  	return phys_to_dma(hwdev, virt_to_phys(address));
e08e1f7ad   Ian Campbell   swiotlb: allow ar...
112
  }
ad32e8cb8   FUJITA Tomonori   swiotlb: Defer sw...
113
  void swiotlb_print_info(void)
2e5b2b86b   Ian Campbell   swiotlb: consolid...
114
  {
ad32e8cb8   FUJITA Tomonori   swiotlb: Defer sw...
115
  	unsigned long bytes = io_tlb_nslabs << IO_TLB_SHIFT;
2e5b2b86b   Ian Campbell   swiotlb: consolid...
116
  	phys_addr_t pstart, pend;
2e5b2b86b   Ian Campbell   swiotlb: consolid...
117
118
119
  
  	pstart = virt_to_phys(io_tlb_start);
  	pend = virt_to_phys(io_tlb_end);
2e5b2b86b   Ian Campbell   swiotlb: consolid...
120
121
122
  	printk(KERN_INFO "Placing %luMB software IO TLB between %p - %p
  ",
  	       bytes >> 20, io_tlb_start, io_tlb_end);
70a7d3cc1   Jeremy Fitzhardinge   swiotlb: add hwde...
123
124
125
126
  	printk(KERN_INFO "software IO TLB at phys %#llx - %#llx
  ",
  	       (unsigned long long)pstart,
  	       (unsigned long long)pend);
2e5b2b86b   Ian Campbell   swiotlb: consolid...
127
  }
abbceff7d   FUJITA Tomonori   swiotlb: add the ...
128
  void __init swiotlb_init_with_tbl(char *tlb, unsigned long nslabs, int verbose)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
  {
563aaf064   Jan Beulich   [IA64] swiotlb cl...
130
  	unsigned long i, bytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131

abbceff7d   FUJITA Tomonori   swiotlb: add the ...
132
  	bytes = nslabs << IO_TLB_SHIFT;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
133

abbceff7d   FUJITA Tomonori   swiotlb: add the ...
134
135
  	io_tlb_nslabs = nslabs;
  	io_tlb_start = tlb;
563aaf064   Jan Beulich   [IA64] swiotlb cl...
136
  	io_tlb_end = io_tlb_start + bytes;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
137
138
139
140
141
142
  
  	/*
  	 * Allocate and initialize the free list array.  This array is used
  	 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
  	 * between io_tlb_start and io_tlb_end.
  	 */
e79f86b2e   Yinghai Lu   swiotlb: Use page...
143
  	io_tlb_list = alloc_bootmem_pages(PAGE_ALIGN(io_tlb_nslabs * sizeof(int)));
25667d675   Tony Luck   Revert "[IA64] sw...
144
  	for (i = 0; i < io_tlb_nslabs; i++)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
146
   		io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
  	io_tlb_index = 0;
e79f86b2e   Yinghai Lu   swiotlb: Use page...
147
  	io_tlb_orig_addr = alloc_bootmem_pages(PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
  
  	/*
  	 * Get the overflow emergency buffer
  	 */
e79f86b2e   Yinghai Lu   swiotlb: Use page...
152
  	io_tlb_overflow_buffer = alloc_bootmem_low_pages(PAGE_ALIGN(io_tlb_overflow));
563aaf064   Jan Beulich   [IA64] swiotlb cl...
153
154
155
  	if (!io_tlb_overflow_buffer)
  		panic("Cannot allocate SWIOTLB overflow buffer!
  ");
ad32e8cb8   FUJITA Tomonori   swiotlb: Defer sw...
156
157
  	if (verbose)
  		swiotlb_print_info();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
158
  }
abbceff7d   FUJITA Tomonori   swiotlb: add the ...
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  /*
   * Statically reserve bounce buffer space and initialize bounce buffer data
   * structures for the software IO TLB used to implement the DMA API.
   */
  void __init
  swiotlb_init_with_default_size(size_t default_size, int verbose)
  {
  	unsigned long bytes;
  
  	if (!io_tlb_nslabs) {
  		io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
  		io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  	}
  
  	bytes = io_tlb_nslabs << IO_TLB_SHIFT;
  
  	/*
  	 * Get IO TLB memory from the low pages
  	 */
e79f86b2e   Yinghai Lu   swiotlb: Use page...
178
  	io_tlb_start = alloc_bootmem_low_pages(PAGE_ALIGN(bytes));
abbceff7d   FUJITA Tomonori   swiotlb: add the ...
179
180
181
182
183
  	if (!io_tlb_start)
  		panic("Cannot allocate SWIOTLB buffer");
  
  	swiotlb_init_with_tbl(io_tlb_start, io_tlb_nslabs, verbose);
  }
563aaf064   Jan Beulich   [IA64] swiotlb cl...
184
  void __init
ad32e8cb8   FUJITA Tomonori   swiotlb: Defer sw...
185
  swiotlb_init(int verbose)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
  {
ad32e8cb8   FUJITA Tomonori   swiotlb: Defer sw...
187
  	swiotlb_init_with_default_size(64 * (1<<20), verbose);	/* default to 64MB */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
  }
0b9afede3   Alex Williamson   [IA64] more robus...
189
190
191
192
193
194
  /*
   * Systems with larger DMA zones (those that don't support ISA) can
   * initialize the swiotlb later using the slab allocator if needed.
   * This should be just like above, but with some error catching.
   */
  int
563aaf064   Jan Beulich   [IA64] swiotlb cl...
195
  swiotlb_late_init_with_default_size(size_t default_size)
0b9afede3   Alex Williamson   [IA64] more robus...
196
  {
563aaf064   Jan Beulich   [IA64] swiotlb cl...
197
  	unsigned long i, bytes, req_nslabs = io_tlb_nslabs;
0b9afede3   Alex Williamson   [IA64] more robus...
198
199
200
201
202
203
204
205
206
207
  	unsigned int order;
  
  	if (!io_tlb_nslabs) {
  		io_tlb_nslabs = (default_size >> IO_TLB_SHIFT);
  		io_tlb_nslabs = ALIGN(io_tlb_nslabs, IO_TLB_SEGSIZE);
  	}
  
  	/*
  	 * Get IO TLB memory from the low pages
  	 */
563aaf064   Jan Beulich   [IA64] swiotlb cl...
208
  	order = get_order(io_tlb_nslabs << IO_TLB_SHIFT);
0b9afede3   Alex Williamson   [IA64] more robus...
209
  	io_tlb_nslabs = SLABS_PER_PAGE << order;
563aaf064   Jan Beulich   [IA64] swiotlb cl...
210
  	bytes = io_tlb_nslabs << IO_TLB_SHIFT;
0b9afede3   Alex Williamson   [IA64] more robus...
211
212
  
  	while ((SLABS_PER_PAGE << order) > IO_TLB_MIN_SLABS) {
bb52196be   FUJITA Tomonori   swiotlb: remove u...
213
214
  		io_tlb_start = (void *)__get_free_pages(GFP_DMA | __GFP_NOWARN,
  							order);
0b9afede3   Alex Williamson   [IA64] more robus...
215
216
217
218
219
220
221
  		if (io_tlb_start)
  			break;
  		order--;
  	}
  
  	if (!io_tlb_start)
  		goto cleanup1;
563aaf064   Jan Beulich   [IA64] swiotlb cl...
222
  	if (order != get_order(bytes)) {
0b9afede3   Alex Williamson   [IA64] more robus...
223
224
225
226
  		printk(KERN_WARNING "Warning: only able to allocate %ld MB "
  		       "for software IO TLB
  ", (PAGE_SIZE << order) >> 20);
  		io_tlb_nslabs = SLABS_PER_PAGE << order;
563aaf064   Jan Beulich   [IA64] swiotlb cl...
227
  		bytes = io_tlb_nslabs << IO_TLB_SHIFT;
0b9afede3   Alex Williamson   [IA64] more robus...
228
  	}
563aaf064   Jan Beulich   [IA64] swiotlb cl...
229
230
  	io_tlb_end = io_tlb_start + bytes;
  	memset(io_tlb_start, 0, bytes);
0b9afede3   Alex Williamson   [IA64] more robus...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
  
  	/*
  	 * Allocate and initialize the free list array.  This array is used
  	 * to find contiguous free memory regions of size up to IO_TLB_SEGSIZE
  	 * between io_tlb_start and io_tlb_end.
  	 */
  	io_tlb_list = (unsigned int *)__get_free_pages(GFP_KERNEL,
  	                              get_order(io_tlb_nslabs * sizeof(int)));
  	if (!io_tlb_list)
  		goto cleanup2;
  
  	for (i = 0; i < io_tlb_nslabs; i++)
   		io_tlb_list[i] = IO_TLB_SEGSIZE - OFFSET(i, IO_TLB_SEGSIZE);
  	io_tlb_index = 0;
bc40ac669   Becky Bruce   swiotlb: store ph...
245
246
247
248
  	io_tlb_orig_addr = (phys_addr_t *)
  		__get_free_pages(GFP_KERNEL,
  				 get_order(io_tlb_nslabs *
  					   sizeof(phys_addr_t)));
0b9afede3   Alex Williamson   [IA64] more robus...
249
250
  	if (!io_tlb_orig_addr)
  		goto cleanup3;
bc40ac669   Becky Bruce   swiotlb: store ph...
251
  	memset(io_tlb_orig_addr, 0, io_tlb_nslabs * sizeof(phys_addr_t));
0b9afede3   Alex Williamson   [IA64] more robus...
252
253
254
255
256
257
258
259
  
  	/*
  	 * Get the overflow emergency buffer
  	 */
  	io_tlb_overflow_buffer = (void *)__get_free_pages(GFP_DMA,
  	                                          get_order(io_tlb_overflow));
  	if (!io_tlb_overflow_buffer)
  		goto cleanup4;
ad32e8cb8   FUJITA Tomonori   swiotlb: Defer sw...
260
  	swiotlb_print_info();
0b9afede3   Alex Williamson   [IA64] more robus...
261

5740afdb6   FUJITA Tomonori   swiotlb: Add swio...
262
  	late_alloc = 1;
0b9afede3   Alex Williamson   [IA64] more robus...
263
264
265
  	return 0;
  
  cleanup4:
bc40ac669   Becky Bruce   swiotlb: store ph...
266
267
  	free_pages((unsigned long)io_tlb_orig_addr,
  		   get_order(io_tlb_nslabs * sizeof(phys_addr_t)));
0b9afede3   Alex Williamson   [IA64] more robus...
268
269
  	io_tlb_orig_addr = NULL;
  cleanup3:
25667d675   Tony Luck   Revert "[IA64] sw...
270
271
  	free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
  	                                                 sizeof(int)));
0b9afede3   Alex Williamson   [IA64] more robus...
272
  	io_tlb_list = NULL;
0b9afede3   Alex Williamson   [IA64] more robus...
273
  cleanup2:
563aaf064   Jan Beulich   [IA64] swiotlb cl...
274
  	io_tlb_end = NULL;
0b9afede3   Alex Williamson   [IA64] more robus...
275
276
277
278
279
280
  	free_pages((unsigned long)io_tlb_start, order);
  	io_tlb_start = NULL;
  cleanup1:
  	io_tlb_nslabs = req_nslabs;
  	return -ENOMEM;
  }
5740afdb6   FUJITA Tomonori   swiotlb: Add swio...
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
  void __init swiotlb_free(void)
  {
  	if (!io_tlb_overflow_buffer)
  		return;
  
  	if (late_alloc) {
  		free_pages((unsigned long)io_tlb_overflow_buffer,
  			   get_order(io_tlb_overflow));
  		free_pages((unsigned long)io_tlb_orig_addr,
  			   get_order(io_tlb_nslabs * sizeof(phys_addr_t)));
  		free_pages((unsigned long)io_tlb_list, get_order(io_tlb_nslabs *
  								 sizeof(int)));
  		free_pages((unsigned long)io_tlb_start,
  			   get_order(io_tlb_nslabs << IO_TLB_SHIFT));
  	} else {
  		free_bootmem_late(__pa(io_tlb_overflow_buffer),
e79f86b2e   Yinghai Lu   swiotlb: Use page...
297
  				  PAGE_ALIGN(io_tlb_overflow));
5740afdb6   FUJITA Tomonori   swiotlb: Add swio...
298
  		free_bootmem_late(__pa(io_tlb_orig_addr),
e79f86b2e   Yinghai Lu   swiotlb: Use page...
299
  				  PAGE_ALIGN(io_tlb_nslabs * sizeof(phys_addr_t)));
5740afdb6   FUJITA Tomonori   swiotlb: Add swio...
300
  		free_bootmem_late(__pa(io_tlb_list),
e79f86b2e   Yinghai Lu   swiotlb: Use page...
301
  				  PAGE_ALIGN(io_tlb_nslabs * sizeof(int)));
5740afdb6   FUJITA Tomonori   swiotlb: Add swio...
302
  		free_bootmem_late(__pa(io_tlb_start),
e79f86b2e   Yinghai Lu   swiotlb: Use page...
303
  				  PAGE_ALIGN(io_tlb_nslabs << IO_TLB_SHIFT));
5740afdb6   FUJITA Tomonori   swiotlb: Add swio...
304
305
  	}
  }
02ca646e7   FUJITA Tomonori   swiotlb: remove u...
306
  static int is_swiotlb_buffer(phys_addr_t paddr)
640aebfe0   FUJITA Tomonori   swiotlb: add is_s...
307
  {
02ca646e7   FUJITA Tomonori   swiotlb: remove u...
308
309
  	return paddr >= virt_to_phys(io_tlb_start) &&
  		paddr < virt_to_phys(io_tlb_end);
640aebfe0   FUJITA Tomonori   swiotlb: add is_s...
310
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
311
  /*
fb05a3792   Becky Bruce   swiotlb: add supp...
312
313
   * Bounce: copy the swiotlb buffer back to the original dma location
   */
d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
314
315
  void swiotlb_bounce(phys_addr_t phys, char *dma_addr, size_t size,
  		    enum dma_data_direction dir)
fb05a3792   Becky Bruce   swiotlb: add supp...
316
317
318
319
320
321
322
323
324
325
326
  {
  	unsigned long pfn = PFN_DOWN(phys);
  
  	if (PageHighMem(pfn_to_page(pfn))) {
  		/* The buffer does not have a mapping.  Map it in and copy */
  		unsigned int offset = phys & ~PAGE_MASK;
  		char *buffer;
  		unsigned int sz = 0;
  		unsigned long flags;
  
  		while (size) {
67131ad05   Becky Bruce   swiotlb: fix comp...
327
  			sz = min_t(size_t, PAGE_SIZE - offset, size);
fb05a3792   Becky Bruce   swiotlb: add supp...
328
329
330
331
332
333
  
  			local_irq_save(flags);
  			buffer = kmap_atomic(pfn_to_page(pfn),
  					     KM_BOUNCE_READ);
  			if (dir == DMA_TO_DEVICE)
  				memcpy(dma_addr, buffer + offset, sz);
ef9b18935   Jeremy Fitzhardinge   swiotlb: support ...
334
  			else
fb05a3792   Becky Bruce   swiotlb: add supp...
335
336
  				memcpy(buffer + offset, dma_addr, sz);
  			kunmap_atomic(buffer, KM_BOUNCE_READ);
ef9b18935   Jeremy Fitzhardinge   swiotlb: support ...
337
  			local_irq_restore(flags);
fb05a3792   Becky Bruce   swiotlb: add supp...
338
339
340
341
342
  
  			size -= sz;
  			pfn++;
  			dma_addr += sz;
  			offset = 0;
ef9b18935   Jeremy Fitzhardinge   swiotlb: support ...
343
344
  		}
  	} else {
ef9b18935   Jeremy Fitzhardinge   swiotlb: support ...
345
  		if (dir == DMA_TO_DEVICE)
fb05a3792   Becky Bruce   swiotlb: add supp...
346
  			memcpy(dma_addr, phys_to_virt(phys), size);
ef9b18935   Jeremy Fitzhardinge   swiotlb: support ...
347
  		else
fb05a3792   Becky Bruce   swiotlb: add supp...
348
  			memcpy(phys_to_virt(phys), dma_addr, size);
ef9b18935   Jeremy Fitzhardinge   swiotlb: support ...
349
  	}
1b548f667   Jeremy Fitzhardinge   swiotlb: factor o...
350
  }
d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
351
  EXPORT_SYMBOL_GPL(swiotlb_bounce);
1b548f667   Jeremy Fitzhardinge   swiotlb: factor o...
352

eb605a575   FUJITA Tomonori   swiotlb: add swio...
353
  void *swiotlb_tbl_map_single(struct device *hwdev, dma_addr_t tbl_dma_addr,
22d482699   Konrad Rzeszutek Wilk   swiotlb: search a...
354
355
  			     phys_addr_t phys, size_t size,
  			     enum dma_data_direction dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
356
357
358
359
360
  {
  	unsigned long flags;
  	char *dma_addr;
  	unsigned int nslots, stride, index, wrap;
  	int i;
681cc5cd3   FUJITA Tomonori   iommu sg merging:...
361
362
363
364
365
  	unsigned long mask;
  	unsigned long offset_slots;
  	unsigned long max_slots;
  
  	mask = dma_get_seg_boundary(hwdev);
681cc5cd3   FUJITA Tomonori   iommu sg merging:...
366

eb605a575   FUJITA Tomonori   swiotlb: add swio...
367
368
369
  	tbl_dma_addr &= mask;
  
  	offset_slots = ALIGN(tbl_dma_addr, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
a5ddde4a5   Ian Campbell   swiotlb: add comm...
370
371
372
373
  
  	/*
   	 * Carefully handle integer overflow which can occur when mask == ~0UL.
   	 */
b15a3891c   Jan Beulich   avoid endless loo...
374
375
376
  	max_slots = mask + 1
  		    ? ALIGN(mask + 1, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT
  		    : 1UL << (BITS_PER_LONG - IO_TLB_SHIFT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
377
378
379
380
381
382
383
384
385
386
  
  	/*
  	 * For mappings greater than a page, we limit the stride (and
  	 * hence alignment) to a page size.
  	 */
  	nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
  	if (size > PAGE_SIZE)
  		stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
  	else
  		stride = 1;
348145458   Eric Sesterhenn   BUG_ON() Conversi...
387
  	BUG_ON(!nslots);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
389
390
391
392
393
  
  	/*
  	 * Find suitable number of IO TLB entries size that will fit this
  	 * request and allocate a buffer from that IO TLB pool.
  	 */
  	spin_lock_irqsave(&io_tlb_lock, flags);
a7133a155   Andrew Morton   lib/swiotlb.c: cl...
394
395
396
397
398
399
  	index = ALIGN(io_tlb_index, stride);
  	if (index >= io_tlb_nslabs)
  		index = 0;
  	wrap = index;
  
  	do {
a85225092   FUJITA Tomonori   swiotlb: use iomm...
400
401
  		while (iommu_is_span_boundary(index, nslots, offset_slots,
  					      max_slots)) {
b15a3891c   Jan Beulich   avoid endless loo...
402
403
404
  			index += stride;
  			if (index >= io_tlb_nslabs)
  				index = 0;
a7133a155   Andrew Morton   lib/swiotlb.c: cl...
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
  			if (index == wrap)
  				goto not_found;
  		}
  
  		/*
  		 * If we find a slot that indicates we have 'nslots' number of
  		 * contiguous buffers, we allocate the buffers from that slot
  		 * and mark the entries as '0' indicating unavailable.
  		 */
  		if (io_tlb_list[index] >= nslots) {
  			int count = 0;
  
  			for (i = index; i < (int) (index + nslots); i++)
  				io_tlb_list[i] = 0;
  			for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE - 1) && io_tlb_list[i]; i--)
  				io_tlb_list[i] = ++count;
  			dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
422

a7133a155   Andrew Morton   lib/swiotlb.c: cl...
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
  			/*
  			 * Update the indices to avoid searching in the next
  			 * round.
  			 */
  			io_tlb_index = ((index + nslots) < io_tlb_nslabs
  					? (index + nslots) : 0);
  
  			goto found;
  		}
  		index += stride;
  		if (index >= io_tlb_nslabs)
  			index = 0;
  	} while (index != wrap);
  
  not_found:
  	spin_unlock_irqrestore(&io_tlb_lock, flags);
  	return NULL;
  found:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
441
442
443
444
445
446
447
  	spin_unlock_irqrestore(&io_tlb_lock, flags);
  
  	/*
  	 * Save away the mapping from the original address to the DMA address.
  	 * This is needed when we sync the memory.  Then we sync the buffer if
  	 * needed.
  	 */
bc40ac669   Becky Bruce   swiotlb: store ph...
448
449
  	for (i = 0; i < nslots; i++)
  		io_tlb_orig_addr[index+i] = phys + (i << IO_TLB_SHIFT);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
  	if (dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL)
fb05a3792   Becky Bruce   swiotlb: add supp...
451
  		swiotlb_bounce(phys, dma_addr, size, DMA_TO_DEVICE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
452
453
454
  
  	return dma_addr;
  }
d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
455
  EXPORT_SYMBOL_GPL(swiotlb_tbl_map_single);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456
457
  
  /*
eb605a575   FUJITA Tomonori   swiotlb: add swio...
458
459
460
461
   * Allocates bounce buffer and returns its kernel virtual address.
   */
  
  static void *
22d482699   Konrad Rzeszutek Wilk   swiotlb: search a...
462
463
  map_single(struct device *hwdev, phys_addr_t phys, size_t size,
  	   enum dma_data_direction dir)
eb605a575   FUJITA Tomonori   swiotlb: add swio...
464
465
466
467
468
469
470
  {
  	dma_addr_t start_dma_addr = swiotlb_virt_to_bus(hwdev, io_tlb_start);
  
  	return swiotlb_tbl_map_single(hwdev, start_dma_addr, phys, size, dir);
  }
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
471
472
   * dma_addr is the kernel virtual address of the bounce buffer to unmap.
   */
d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
473
  void
bfc5501f6   Konrad Rzeszutek Wilk   swiotlb: Make int...
474
  swiotlb_tbl_unmap_single(struct device *hwdev, char *dma_addr, size_t size,
22d482699   Konrad Rzeszutek Wilk   swiotlb: search a...
475
  			enum dma_data_direction dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
476
477
478
479
  {
  	unsigned long flags;
  	int i, count, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
  	int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
bc40ac669   Becky Bruce   swiotlb: store ph...
480
  	phys_addr_t phys = io_tlb_orig_addr[index];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
481
482
483
484
  
  	/*
  	 * First, sync the memory before unmapping the entry
  	 */
bc40ac669   Becky Bruce   swiotlb: store ph...
485
  	if (phys && ((dir == DMA_FROM_DEVICE) || (dir == DMA_BIDIRECTIONAL)))
fb05a3792   Becky Bruce   swiotlb: add supp...
486
  		swiotlb_bounce(phys, dma_addr, size, DMA_FROM_DEVICE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
487
488
489
  
  	/*
  	 * Return the buffer to the free list by setting the corresponding
af901ca18   André Goddard Rosa   tree-wide: fix as...
490
  	 * entries to indicate the number of contiguous entries available.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
  	 * While returning the entries to the free list, we merge the entries
  	 * with slots below and above the pool being returned.
  	 */
  	spin_lock_irqsave(&io_tlb_lock, flags);
  	{
  		count = ((index + nslots) < ALIGN(index + 1, IO_TLB_SEGSIZE) ?
  			 io_tlb_list[index + nslots] : 0);
  		/*
  		 * Step 1: return the slots to the free list, merging the
  		 * slots with superceeding slots
  		 */
  		for (i = index + nslots - 1; i >= index; i--)
  			io_tlb_list[i] = ++count;
  		/*
  		 * Step 2: merge the returned slots with the preceding slots,
  		 * if available (non zero)
  		 */
  		for (i = index - 1; (OFFSET(i, IO_TLB_SEGSIZE) != IO_TLB_SEGSIZE -1) && io_tlb_list[i]; i--)
  			io_tlb_list[i] = ++count;
  	}
  	spin_unlock_irqrestore(&io_tlb_lock, flags);
  }
d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
513
  EXPORT_SYMBOL_GPL(swiotlb_tbl_unmap_single);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
514

d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
515
  void
bfc5501f6   Konrad Rzeszutek Wilk   swiotlb: Make int...
516
  swiotlb_tbl_sync_single(struct device *hwdev, char *dma_addr, size_t size,
d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
517
518
  			enum dma_data_direction dir,
  			enum dma_sync_target target)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
519
  {
bc40ac669   Becky Bruce   swiotlb: store ph...
520
521
522
523
  	int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
  	phys_addr_t phys = io_tlb_orig_addr[index];
  
  	phys += ((unsigned long)dma_addr & ((1 << IO_TLB_SHIFT) - 1));
df336d1c7   Keir Fraser   Fix swiotlb_sync_...
524

de69e0f0b   John W. Linville   [PATCH] swiotlb: ...
525
526
527
  	switch (target) {
  	case SYNC_FOR_CPU:
  		if (likely(dir == DMA_FROM_DEVICE || dir == DMA_BIDIRECTIONAL))
fb05a3792   Becky Bruce   swiotlb: add supp...
528
  			swiotlb_bounce(phys, dma_addr, size, DMA_FROM_DEVICE);
348145458   Eric Sesterhenn   BUG_ON() Conversi...
529
530
  		else
  			BUG_ON(dir != DMA_TO_DEVICE);
de69e0f0b   John W. Linville   [PATCH] swiotlb: ...
531
532
533
  		break;
  	case SYNC_FOR_DEVICE:
  		if (likely(dir == DMA_TO_DEVICE || dir == DMA_BIDIRECTIONAL))
fb05a3792   Becky Bruce   swiotlb: add supp...
534
  			swiotlb_bounce(phys, dma_addr, size, DMA_TO_DEVICE);
348145458   Eric Sesterhenn   BUG_ON() Conversi...
535
536
  		else
  			BUG_ON(dir != DMA_FROM_DEVICE);
de69e0f0b   John W. Linville   [PATCH] swiotlb: ...
537
538
  		break;
  	default:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
539
  		BUG();
de69e0f0b   John W. Linville   [PATCH] swiotlb: ...
540
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
541
  }
d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
542
  EXPORT_SYMBOL_GPL(swiotlb_tbl_sync_single);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
543
544
545
  
  void *
  swiotlb_alloc_coherent(struct device *hwdev, size_t size,
06a544971   Al Viro   [PATCH] gfp_t: dm...
546
  		       dma_addr_t *dma_handle, gfp_t flags)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
547
  {
563aaf064   Jan Beulich   [IA64] swiotlb cl...
548
  	dma_addr_t dev_addr;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
549
550
  	void *ret;
  	int order = get_order(size);
284901a90   Yang Hongyang   dma-mapping: repl...
551
  	u64 dma_mask = DMA_BIT_MASK(32);
1e74f3000   FUJITA Tomonori   swiotlb: use cohe...
552
553
554
  
  	if (hwdev && hwdev->coherent_dma_mask)
  		dma_mask = hwdev->coherent_dma_mask;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
555

25667d675   Tony Luck   Revert "[IA64] sw...
556
  	ret = (void *)__get_free_pages(flags, order);
ac2b3e67d   Jan Beulich   dma-mapping: fix ...
557
  	if (ret && swiotlb_virt_to_bus(hwdev, ret) + size - 1 > dma_mask) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
558
559
  		/*
  		 * The allocated memory isn't reachable by the device.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
560
561
562
563
564
565
  		 */
  		free_pages((unsigned long) ret, order);
  		ret = NULL;
  	}
  	if (!ret) {
  		/*
bfc5501f6   Konrad Rzeszutek Wilk   swiotlb: Make int...
566
567
  		 * We are either out of memory or the device can't DMA to
  		 * GFP_DMA memory; fall back on map_single(), which
ceb5ac326   Becky Bruce   swiotlb: comment ...
568
  		 * will grab memory from the lowest available address range.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
569
  		 */
bc40ac669   Becky Bruce   swiotlb: store ph...
570
  		ret = map_single(hwdev, 0, size, DMA_FROM_DEVICE);
9dfda12b8   FUJITA Tomonori   swiotlb: use map_...
571
  		if (!ret)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
572
  			return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
573
574
575
  	}
  
  	memset(ret, 0, size);
70a7d3cc1   Jeremy Fitzhardinge   swiotlb: add hwde...
576
  	dev_addr = swiotlb_virt_to_bus(hwdev, ret);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
577
578
  
  	/* Confirm address can be DMA'd by device */
ac2b3e67d   Jan Beulich   dma-mapping: fix ...
579
  	if (dev_addr + size - 1 > dma_mask) {
563aaf064   Jan Beulich   [IA64] swiotlb cl...
580
581
  		printk("hwdev DMA mask = 0x%016Lx, dev_addr = 0x%016Lx
  ",
1e74f3000   FUJITA Tomonori   swiotlb: use cohe...
582
  		       (unsigned long long)dma_mask,
563aaf064   Jan Beulich   [IA64] swiotlb cl...
583
  		       (unsigned long long)dev_addr);
a2b89b596   FUJITA Tomonori   swiotlb: remove p...
584
585
  
  		/* DMA_TO_DEVICE to avoid memcpy in unmap_single */
bfc5501f6   Konrad Rzeszutek Wilk   swiotlb: Make int...
586
  		swiotlb_tbl_unmap_single(hwdev, ret, size, DMA_TO_DEVICE);
a2b89b596   FUJITA Tomonori   swiotlb: remove p...
587
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
588
589
590
591
  	}
  	*dma_handle = dev_addr;
  	return ret;
  }
874d6a955   FUJITA Tomonori   swiotlb: clean up...
592
  EXPORT_SYMBOL(swiotlb_alloc_coherent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
593
594
595
  
  void
  swiotlb_free_coherent(struct device *hwdev, size_t size, void *vaddr,
02ca646e7   FUJITA Tomonori   swiotlb: remove u...
596
  		      dma_addr_t dev_addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
597
  {
862d196b2   FUJITA Tomonori   swiotlb: use phys...
598
  	phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
02ca646e7   FUJITA Tomonori   swiotlb: remove u...
599

aa24886e3   David Brownell   dma_free_coherent...
600
  	WARN_ON(irqs_disabled());
02ca646e7   FUJITA Tomonori   swiotlb: remove u...
601
602
  	if (!is_swiotlb_buffer(paddr))
  		free_pages((unsigned long)vaddr, get_order(size));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
603
  	else
bfc5501f6   Konrad Rzeszutek Wilk   swiotlb: Make int...
604
605
  		/* DMA_TO_DEVICE to avoid memcpy in swiotlb_tbl_unmap_single */
  		swiotlb_tbl_unmap_single(hwdev, vaddr, size, DMA_TO_DEVICE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
606
  }
874d6a955   FUJITA Tomonori   swiotlb: clean up...
607
  EXPORT_SYMBOL(swiotlb_free_coherent);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
608
609
  
  static void
22d482699   Konrad Rzeszutek Wilk   swiotlb: search a...
610
611
  swiotlb_full(struct device *dev, size_t size, enum dma_data_direction dir,
  	     int do_panic)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
612
613
614
615
  {
  	/*
  	 * Ran out of IOMMU space for this operation. This is very bad.
  	 * Unfortunately the drivers cannot handle this operation properly.
17e5ad6c0   Tony Luck   [PATCH] Removed r...
616
  	 * unless they check for dma_mapping_error (most don't)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
617
618
619
  	 * When the mapping is small enough return a static buffer to limit
  	 * the damage, or panic when the transfer is too big.
  	 */
563aaf064   Jan Beulich   [IA64] swiotlb cl...
620
  	printk(KERN_ERR "DMA: Out of SW-IOMMU space for %zu bytes at "
94b324864   Kay Sievers   swiotlb: struct d...
621
622
  	       "device %s
  ", size, dev ? dev_name(dev) : "?");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
623

c7084b35e   Casey Dahlin   lib/swiotlb.c: Fi...
624
625
626
627
628
629
630
631
632
633
634
635
  	if (size <= io_tlb_overflow || !do_panic)
  		return;
  
  	if (dir == DMA_BIDIRECTIONAL)
  		panic("DMA: Random memory could be DMA accessed
  ");
  	if (dir == DMA_FROM_DEVICE)
  		panic("DMA: Random memory could be DMA written
  ");
  	if (dir == DMA_TO_DEVICE)
  		panic("DMA: Random memory could be DMA read
  ");
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
636
637
638
639
  }
  
  /*
   * Map a single buffer of the indicated size for DMA in streaming mode.  The
17e5ad6c0   Tony Luck   [PATCH] Removed r...
640
   * physical address to use is returned.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
641
642
   *
   * Once the device is given the dma address, the device owns this memory until
ceb5ac326   Becky Bruce   swiotlb: comment ...
643
   * either swiotlb_unmap_page or swiotlb_dma_sync_single is performed.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
644
   */
f98eee8ea   FUJITA Tomonori   x86, ia64: remove...
645
646
647
648
  dma_addr_t swiotlb_map_page(struct device *dev, struct page *page,
  			    unsigned long offset, size_t size,
  			    enum dma_data_direction dir,
  			    struct dma_attrs *attrs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
649
  {
f98eee8ea   FUJITA Tomonori   x86, ia64: remove...
650
  	phys_addr_t phys = page_to_phys(page) + offset;
862d196b2   FUJITA Tomonori   swiotlb: use phys...
651
  	dma_addr_t dev_addr = phys_to_dma(dev, phys);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
652
  	void *map;
348145458   Eric Sesterhenn   BUG_ON() Conversi...
653
  	BUG_ON(dir == DMA_NONE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
654
  	/*
ceb5ac326   Becky Bruce   swiotlb: comment ...
655
  	 * If the address happens to be in the device's DMA window,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
656
657
658
  	 * we can safely return the device addr and not worry about bounce
  	 * buffering it.
  	 */
b9394647a   FUJITA Tomonori   swiotlb: use dma_...
659
  	if (dma_capable(dev, dev_addr, size) && !swiotlb_force)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
660
661
662
663
664
  		return dev_addr;
  
  	/*
  	 * Oh well, have to allocate and map a bounce buffer.
  	 */
f98eee8ea   FUJITA Tomonori   x86, ia64: remove...
665
  	map = map_single(dev, phys, size, dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
666
  	if (!map) {
f98eee8ea   FUJITA Tomonori   x86, ia64: remove...
667
  		swiotlb_full(dev, size, dir, 1);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
668
669
  		map = io_tlb_overflow_buffer;
  	}
f98eee8ea   FUJITA Tomonori   x86, ia64: remove...
670
  	dev_addr = swiotlb_virt_to_bus(dev, map);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
671
672
673
674
  
  	/*
  	 * Ensure that the address returned is DMA'ble
  	 */
fba99fa38   FUJITA Tomonori   swiotlb: fix wron...
675
676
677
678
  	if (!dma_capable(dev, dev_addr, size)) {
  		swiotlb_tbl_unmap_single(dev, map, size, dir);
  		dev_addr = swiotlb_virt_to_bus(dev, io_tlb_overflow_buffer);
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
679
680
681
  
  	return dev_addr;
  }
f98eee8ea   FUJITA Tomonori   x86, ia64: remove...
682
  EXPORT_SYMBOL_GPL(swiotlb_map_page);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
683
684
  
  /*
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
685
   * Unmap a single streaming mode DMA translation.  The dma_addr and size must
ceb5ac326   Becky Bruce   swiotlb: comment ...
686
   * match what was provided for in a previous swiotlb_map_page call.  All
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
687
688
689
690
691
   * other usages are undefined.
   *
   * After this call, reads by the cpu to the buffer are guaranteed to see
   * whatever the device wrote there.
   */
7fcebbd2d   Becky Bruce   swiotlb: rename u...
692
  static void unmap_single(struct device *hwdev, dma_addr_t dev_addr,
22d482699   Konrad Rzeszutek Wilk   swiotlb: search a...
693
  			 size_t size, enum dma_data_direction dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
694
  {
862d196b2   FUJITA Tomonori   swiotlb: use phys...
695
  	phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
696

348145458   Eric Sesterhenn   BUG_ON() Conversi...
697
  	BUG_ON(dir == DMA_NONE);
7fcebbd2d   Becky Bruce   swiotlb: rename u...
698

02ca646e7   FUJITA Tomonori   swiotlb: remove u...
699
  	if (is_swiotlb_buffer(paddr)) {
bfc5501f6   Konrad Rzeszutek Wilk   swiotlb: Make int...
700
  		swiotlb_tbl_unmap_single(hwdev, phys_to_virt(paddr), size, dir);
7fcebbd2d   Becky Bruce   swiotlb: rename u...
701
702
703
704
705
  		return;
  	}
  
  	if (dir != DMA_FROM_DEVICE)
  		return;
02ca646e7   FUJITA Tomonori   swiotlb: remove u...
706
707
708
709
710
711
712
  	/*
  	 * phys_to_virt doesn't work with hihgmem page but we could
  	 * call dma_mark_clean() with hihgmem page here. However, we
  	 * are fine since dma_mark_clean() is null on POWERPC. We can
  	 * make dma_mark_clean() take a physical address if necessary.
  	 */
  	dma_mark_clean(phys_to_virt(paddr), size);
7fcebbd2d   Becky Bruce   swiotlb: rename u...
713
714
715
716
717
718
719
  }
  
  void swiotlb_unmap_page(struct device *hwdev, dma_addr_t dev_addr,
  			size_t size, enum dma_data_direction dir,
  			struct dma_attrs *attrs)
  {
  	unmap_single(hwdev, dev_addr, size, dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
720
  }
f98eee8ea   FUJITA Tomonori   x86, ia64: remove...
721
  EXPORT_SYMBOL_GPL(swiotlb_unmap_page);
874d6a955   FUJITA Tomonori   swiotlb: clean up...
722

1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
723
724
725
726
  /*
   * Make physical memory consistent for a single streaming mode DMA translation
   * after a transfer.
   *
ceb5ac326   Becky Bruce   swiotlb: comment ...
727
   * If you perform a swiotlb_map_page() but wish to interrogate the buffer
17e5ad6c0   Tony Luck   [PATCH] Removed r...
728
729
   * using the cpu, yet do not wish to teardown the dma mapping, you must
   * call this function before doing so.  At the next point you give the dma
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
730
731
732
   * address back to the card, you must first perform a
   * swiotlb_dma_sync_for_device, and then the device again owns the buffer
   */
be6b02678   Andrew Morton   [PATCH] swiotlb u...
733
  static void
8270f3f1a   John W. Linville   [PATCH] swiotlb: ...
734
  swiotlb_sync_single(struct device *hwdev, dma_addr_t dev_addr,
d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
735
736
  		    size_t size, enum dma_data_direction dir,
  		    enum dma_sync_target target)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
737
  {
862d196b2   FUJITA Tomonori   swiotlb: use phys...
738
  	phys_addr_t paddr = dma_to_phys(hwdev, dev_addr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
739

348145458   Eric Sesterhenn   BUG_ON() Conversi...
740
  	BUG_ON(dir == DMA_NONE);
380d68783   Becky Bruce   swiotlb: use swio...
741

02ca646e7   FUJITA Tomonori   swiotlb: remove u...
742
  	if (is_swiotlb_buffer(paddr)) {
bfc5501f6   Konrad Rzeszutek Wilk   swiotlb: Make int...
743
744
  		swiotlb_tbl_sync_single(hwdev, phys_to_virt(paddr), size, dir,
  				       target);
380d68783   Becky Bruce   swiotlb: use swio...
745
746
747
748
749
  		return;
  	}
  
  	if (dir != DMA_FROM_DEVICE)
  		return;
02ca646e7   FUJITA Tomonori   swiotlb: remove u...
750
  	dma_mark_clean(phys_to_virt(paddr), size);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
751
752
753
  }
  
  void
8270f3f1a   John W. Linville   [PATCH] swiotlb: ...
754
  swiotlb_sync_single_for_cpu(struct device *hwdev, dma_addr_t dev_addr,
160c1d8e4   FUJITA Tomonori   x86, ia64: conver...
755
  			    size_t size, enum dma_data_direction dir)
8270f3f1a   John W. Linville   [PATCH] swiotlb: ...
756
  {
de69e0f0b   John W. Linville   [PATCH] swiotlb: ...
757
  	swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_CPU);
8270f3f1a   John W. Linville   [PATCH] swiotlb: ...
758
  }
874d6a955   FUJITA Tomonori   swiotlb: clean up...
759
  EXPORT_SYMBOL(swiotlb_sync_single_for_cpu);
8270f3f1a   John W. Linville   [PATCH] swiotlb: ...
760
761
  
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
762
  swiotlb_sync_single_for_device(struct device *hwdev, dma_addr_t dev_addr,
160c1d8e4   FUJITA Tomonori   x86, ia64: conver...
763
  			       size_t size, enum dma_data_direction dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
764
  {
de69e0f0b   John W. Linville   [PATCH] swiotlb: ...
765
  	swiotlb_sync_single(hwdev, dev_addr, size, dir, SYNC_FOR_DEVICE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
766
  }
874d6a955   FUJITA Tomonori   swiotlb: clean up...
767
  EXPORT_SYMBOL(swiotlb_sync_single_for_device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
768
769
770
  
  /*
   * Map a set of buffers described by scatterlist in streaming mode for DMA.
ceb5ac326   Becky Bruce   swiotlb: comment ...
771
   * This is the scatter-gather version of the above swiotlb_map_page
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
772
773
774
775
776
777
778
779
780
781
   * interface.  Here the scatter gather list elements are each tagged with the
   * appropriate dma address and length.  They are obtained via
   * sg_dma_{address,length}(SG).
   *
   * NOTE: An implementation may be able to use a smaller number of
   *       DMA address/length pairs than there are SG table elements.
   *       (for example via virtual mapping capabilities)
   *       The routine returns the number of addr/length pairs actually
   *       used, at most nents.
   *
ceb5ac326   Becky Bruce   swiotlb: comment ...
782
   * Device ownership issues as mentioned above for swiotlb_map_page are the
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
783
784
785
   * same here.
   */
  int
309df0c50   Arthur Kepner   dma/ia64: update ...
786
  swiotlb_map_sg_attrs(struct device *hwdev, struct scatterlist *sgl, int nelems,
160c1d8e4   FUJITA Tomonori   x86, ia64: conver...
787
  		     enum dma_data_direction dir, struct dma_attrs *attrs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
788
  {
dbfd49fe9   Jens Axboe   swiotlb: sg chain...
789
  	struct scatterlist *sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
790
  	int i;
348145458   Eric Sesterhenn   BUG_ON() Conversi...
791
  	BUG_ON(dir == DMA_NONE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
792

dbfd49fe9   Jens Axboe   swiotlb: sg chain...
793
  	for_each_sg(sgl, sg, nelems, i) {
961d7d0ee   Ian Campbell   swiotlb: do not u...
794
  		phys_addr_t paddr = sg_phys(sg);
862d196b2   FUJITA Tomonori   swiotlb: use phys...
795
  		dma_addr_t dev_addr = phys_to_dma(hwdev, paddr);
bc40ac669   Becky Bruce   swiotlb: store ph...
796

cf56e3f2e   FUJITA Tomonori   swiotlb: remove s...
797
  		if (swiotlb_force ||
b9394647a   FUJITA Tomonori   swiotlb: use dma_...
798
  		    !dma_capable(hwdev, dev_addr, sg->length)) {
bc40ac669   Becky Bruce   swiotlb: store ph...
799
800
  			void *map = map_single(hwdev, sg_phys(sg),
  					       sg->length, dir);
7e8702334   Andi Kleen   [PATCH] Fix swiot...
801
  			if (!map) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
802
803
804
  				/* Don't panic here, we expect map_sg users
  				   to do proper error handling. */
  				swiotlb_full(hwdev, sg->length, dir, 0);
309df0c50   Arthur Kepner   dma/ia64: update ...
805
806
  				swiotlb_unmap_sg_attrs(hwdev, sgl, i, dir,
  						       attrs);
dbfd49fe9   Jens Axboe   swiotlb: sg chain...
807
  				sgl[0].dma_length = 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
808
809
  				return 0;
  			}
70a7d3cc1   Jeremy Fitzhardinge   swiotlb: add hwde...
810
  			sg->dma_address = swiotlb_virt_to_bus(hwdev, map);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
811
812
813
814
815
816
  		} else
  			sg->dma_address = dev_addr;
  		sg->dma_length = sg->length;
  	}
  	return nelems;
  }
309df0c50   Arthur Kepner   dma/ia64: update ...
817
818
819
820
  EXPORT_SYMBOL(swiotlb_map_sg_attrs);
  
  int
  swiotlb_map_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
22d482699   Konrad Rzeszutek Wilk   swiotlb: search a...
821
  	       enum dma_data_direction dir)
309df0c50   Arthur Kepner   dma/ia64: update ...
822
823
824
  {
  	return swiotlb_map_sg_attrs(hwdev, sgl, nelems, dir, NULL);
  }
874d6a955   FUJITA Tomonori   swiotlb: clean up...
825
  EXPORT_SYMBOL(swiotlb_map_sg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
826
827
828
  
  /*
   * Unmap a set of streaming mode DMA translations.  Again, cpu read rules
ceb5ac326   Becky Bruce   swiotlb: comment ...
829
   * concerning calls here are the same as for swiotlb_unmap_page() above.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
830
831
   */
  void
309df0c50   Arthur Kepner   dma/ia64: update ...
832
  swiotlb_unmap_sg_attrs(struct device *hwdev, struct scatterlist *sgl,
160c1d8e4   FUJITA Tomonori   x86, ia64: conver...
833
  		       int nelems, enum dma_data_direction dir, struct dma_attrs *attrs)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
834
  {
dbfd49fe9   Jens Axboe   swiotlb: sg chain...
835
  	struct scatterlist *sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
836
  	int i;
348145458   Eric Sesterhenn   BUG_ON() Conversi...
837
  	BUG_ON(dir == DMA_NONE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
838

7fcebbd2d   Becky Bruce   swiotlb: rename u...
839
840
  	for_each_sg(sgl, sg, nelems, i)
  		unmap_single(hwdev, sg->dma_address, sg->dma_length, dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
841
  }
309df0c50   Arthur Kepner   dma/ia64: update ...
842
843
844
845
  EXPORT_SYMBOL(swiotlb_unmap_sg_attrs);
  
  void
  swiotlb_unmap_sg(struct device *hwdev, struct scatterlist *sgl, int nelems,
22d482699   Konrad Rzeszutek Wilk   swiotlb: search a...
846
  		 enum dma_data_direction dir)
309df0c50   Arthur Kepner   dma/ia64: update ...
847
848
849
  {
  	return swiotlb_unmap_sg_attrs(hwdev, sgl, nelems, dir, NULL);
  }
874d6a955   FUJITA Tomonori   swiotlb: clean up...
850
  EXPORT_SYMBOL(swiotlb_unmap_sg);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
851
852
853
854
855
856
857
858
  
  /*
   * Make physical memory consistent for a set of streaming mode DMA translations
   * after a transfer.
   *
   * The same as swiotlb_sync_single_* but for a scatter-gather list, same rules
   * and usage.
   */
be6b02678   Andrew Morton   [PATCH] swiotlb u...
859
  static void
dbfd49fe9   Jens Axboe   swiotlb: sg chain...
860
  swiotlb_sync_sg(struct device *hwdev, struct scatterlist *sgl,
d7ef1533a   Konrad Rzeszutek Wilk   swiotlb: Make swi...
861
862
  		int nelems, enum dma_data_direction dir,
  		enum dma_sync_target target)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
863
  {
dbfd49fe9   Jens Axboe   swiotlb: sg chain...
864
  	struct scatterlist *sg;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
865
  	int i;
380d68783   Becky Bruce   swiotlb: use swio...
866
867
  	for_each_sg(sgl, sg, nelems, i)
  		swiotlb_sync_single(hwdev, sg->dma_address,
de69e0f0b   John W. Linville   [PATCH] swiotlb: ...
868
  				    sg->dma_length, dir, target);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
869
870
871
  }
  
  void
8270f3f1a   John W. Linville   [PATCH] swiotlb: ...
872
  swiotlb_sync_sg_for_cpu(struct device *hwdev, struct scatterlist *sg,
160c1d8e4   FUJITA Tomonori   x86, ia64: conver...
873
  			int nelems, enum dma_data_direction dir)
8270f3f1a   John W. Linville   [PATCH] swiotlb: ...
874
  {
de69e0f0b   John W. Linville   [PATCH] swiotlb: ...
875
  	swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_CPU);
8270f3f1a   John W. Linville   [PATCH] swiotlb: ...
876
  }
874d6a955   FUJITA Tomonori   swiotlb: clean up...
877
  EXPORT_SYMBOL(swiotlb_sync_sg_for_cpu);
8270f3f1a   John W. Linville   [PATCH] swiotlb: ...
878
879
  
  void
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
880
  swiotlb_sync_sg_for_device(struct device *hwdev, struct scatterlist *sg,
160c1d8e4   FUJITA Tomonori   x86, ia64: conver...
881
  			   int nelems, enum dma_data_direction dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
882
  {
de69e0f0b   John W. Linville   [PATCH] swiotlb: ...
883
  	swiotlb_sync_sg(hwdev, sg, nelems, dir, SYNC_FOR_DEVICE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
884
  }
874d6a955   FUJITA Tomonori   swiotlb: clean up...
885
  EXPORT_SYMBOL(swiotlb_sync_sg_for_device);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
886
887
  
  int
8d8bb39b9   FUJITA Tomonori   dma-mapping: add ...
888
  swiotlb_dma_mapping_error(struct device *hwdev, dma_addr_t dma_addr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
889
  {
70a7d3cc1   Jeremy Fitzhardinge   swiotlb: add hwde...
890
  	return (dma_addr == swiotlb_virt_to_bus(hwdev, io_tlb_overflow_buffer));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
891
  }
874d6a955   FUJITA Tomonori   swiotlb: clean up...
892
  EXPORT_SYMBOL(swiotlb_dma_mapping_error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
893
894
  
  /*
17e5ad6c0   Tony Luck   [PATCH] Removed r...
895
   * Return whether the given device DMA address mask can be supported
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
896
   * properly.  For example, if your device can only drive the low 24-bits
17e5ad6c0   Tony Luck   [PATCH] Removed r...
897
   * during bus mastering, then you would pass 0x00ffffff as the mask to
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
898
899
900
   * this function.
   */
  int
563aaf064   Jan Beulich   [IA64] swiotlb cl...
901
  swiotlb_dma_supported(struct device *hwdev, u64 mask)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
902
  {
70a7d3cc1   Jeremy Fitzhardinge   swiotlb: add hwde...
903
  	return swiotlb_virt_to_bus(hwdev, io_tlb_end - 1) <= mask;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
904
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
905
  EXPORT_SYMBOL(swiotlb_dma_supported);