File: /usr/src/linux/arch/ia64/lib/swiotlb.c
1 /*
2 * Dynamic DMA mapping support.
3 *
4 * This implementation is for IA-64 platforms that do not support
5 * I/O TLBs (aka DMA address translation hardware).
6 * Copyright (C) 2000 Asit Mallick <Asit.K.Mallick@intel.com>
7 * Copyright (C) 2000 Goutham Rao <goutham.rao@intel.com>
8 *
9 * 00/12/13 davidm Rename to swiotlb.c and add mark_clean() to avoid
10 * unnecessary i-cache flushing.
11 */
12
13 #include <linux/mm.h>
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 #include <linux/spinlock.h>
17 #include <linux/string.h>
18 #include <linux/types.h>
19
20 #include <asm/io.h>
21 #include <asm/pci.h>
22 #include <asm/dma.h>
23
24 #include <linux/init.h>
25 #include <linux/bootmem.h>
26
27 #define ALIGN(val, align) ((unsigned long) \
28 (((unsigned long) (val) + ((align) - 1)) & ~((align) - 1)))
29
30 /*
31 * log of the size of each IO TLB slab. The number of slabs is command line controllable.
32 */
33 #define IO_TLB_SHIFT 11
34
35 /*
36 * Used to do a quick range check in swiotlb_unmap_single and swiotlb_sync_single, to see
37 * if the memory was in fact allocated by this API.
38 */
39 static char *io_tlb_start, *io_tlb_end;
40
41 /*
42 * The number of IO TLB blocks (in groups of 64) betweeen io_tlb_start and io_tlb_end.
43 * This is command line adjustable via setup_io_tlb_npages.
44 */
45 static unsigned long io_tlb_nslabs = 1024;
46
47 /*
48 * This is a free list describing the number of free entries available from each index
49 */
50 static unsigned int *io_tlb_list;
51 static unsigned int io_tlb_index;
52
53 /*
54 * We need to save away the original address corresponding to a mapped entry for the sync
55 * operations.
56 */
57 static unsigned char **io_tlb_orig_addr;
58
59 /*
60 * Protect the above data structures in the map and unmap calls
61 */
62 static spinlock_t io_tlb_lock = SPIN_LOCK_UNLOCKED;
63
64 static int __init
65 setup_io_tlb_npages (char *str)
66 {
67 io_tlb_nslabs = simple_strtoul(str, NULL, 0) << (PAGE_SHIFT - IO_TLB_SHIFT);
68 return 1;
69 }
70 __setup("swiotlb=", setup_io_tlb_npages);
71
72 /*
73 * Statically reserve bounce buffer space and initialize bounce buffer data structures for
74 * the software IO TLB used to implement the PCI DMA API.
75 */
76 void
77 swiotlb_init (void)
78 {
79 int i;
80
81 /*
82 * Get IO TLB memory from the low pages
83 */
84 io_tlb_start = alloc_bootmem_low_pages(io_tlb_nslabs * (1 << IO_TLB_SHIFT));
85 if (!io_tlb_start)
86 BUG();
87 io_tlb_end = io_tlb_start + io_tlb_nslabs * (1 << IO_TLB_SHIFT);
88
89 /*
90 * Allocate and initialize the free list array. This array is used
91 * to find contiguous free memory regions of size 2^IO_TLB_SHIFT between
92 * io_tlb_start and io_tlb_end.
93 */
94 io_tlb_list = alloc_bootmem(io_tlb_nslabs * sizeof(int));
95 for (i = 0; i < io_tlb_nslabs; i++)
96 io_tlb_list[i] = io_tlb_nslabs - i;
97 io_tlb_index = 0;
98 io_tlb_orig_addr = alloc_bootmem(io_tlb_nslabs * sizeof(char *));
99
100 printk("Placing software IO TLB between 0x%p - 0x%p\n",
101 (void *) io_tlb_start, (void *) io_tlb_end);
102 }
103
104 /*
105 * Allocates bounce buffer and returns its kernel virtual address.
106 */
107 static void *
108 map_single (struct pci_dev *hwdev, char *buffer, size_t size, int direction)
109 {
110 unsigned long flags;
111 char *dma_addr;
112 unsigned int nslots, stride, index, wrap;
113 int i;
114
115 /*
116 * For mappings greater than a page size, we limit the stride (and hence alignment)
117 * to a page size.
118 */
119 nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
120 if (size > (1 << PAGE_SHIFT))
121 stride = (1 << (PAGE_SHIFT - IO_TLB_SHIFT));
122 else
123 stride = nslots;
124
125 if (!nslots)
126 BUG();
127
128 /*
129 * Find suitable number of IO TLB entries size that will fit this request and
130 * allocate a buffer from that IO TLB pool.
131 */
132 spin_lock_irqsave(&io_tlb_lock, flags);
133 {
134 wrap = index = ALIGN(io_tlb_index, stride);
135
136 if (index >= io_tlb_nslabs)
137 wrap = index = 0;
138
139 do {
140 /*
141 * If we find a slot that indicates we have 'nslots' number of
142 * contiguous buffers, we allocate the buffers from that slot and
143 * mark the entries as '0' indicating unavailable.
144 */
145 if (io_tlb_list[index] >= nslots) {
146 int count = 0;
147
148 for (i = index; i < index + nslots; i++)
149 io_tlb_list[i] = 0;
150 for (i = index - 1; (i >= 0) && io_tlb_list[i]; i--)
151 io_tlb_list[i] = ++count;
152 dma_addr = io_tlb_start + (index << IO_TLB_SHIFT);
153
154 /*
155 * Update the indices to avoid searching in the next round.
156 */
157 io_tlb_index = ((index + nslots) < io_tlb_nslabs
158 ? (index + nslots) : 0);
159
160 goto found;
161 }
162 index += stride;
163 if (index >= io_tlb_nslabs)
164 index = 0;
165 } while (index != wrap);
166
167 /*
168 * XXX What is a suitable recovery mechanism here? We cannot
169 * sleep because we are called from with in interrupts!
170 */
171 panic("map_single: could not allocate software IO TLB (%ld bytes)", size);
172 }
173 found:
174 spin_unlock_irqrestore(&io_tlb_lock, flags);
175
176 /*
177 * Save away the mapping from the original address to the DMA address. This is
178 * needed when we sync the memory. Then we sync the buffer if needed.
179 */
180 io_tlb_orig_addr[index] = buffer;
181 if (direction == PCI_DMA_TODEVICE || direction == PCI_DMA_BIDIRECTIONAL)
182 memcpy(dma_addr, buffer, size);
183
184 return dma_addr;
185 }
186
187 /*
188 * dma_addr is the kernel virtual address of the bounce buffer to unmap.
189 */
190 static void
191 unmap_single (struct pci_dev *hwdev, char *dma_addr, size_t size, int direction)
192 {
193 unsigned long flags;
194 int i, nslots = ALIGN(size, 1 << IO_TLB_SHIFT) >> IO_TLB_SHIFT;
195 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
196 char *buffer = io_tlb_orig_addr[index];
197
198 /*
199 * First, sync the memory before unmapping the entry
200 */
201 if ((direction == PCI_DMA_FROMDEVICE) || (direction == PCI_DMA_BIDIRECTIONAL))
202 /*
203 * bounce... copy the data back into the original buffer * and delete the
204 * bounce buffer.
205 */
206 memcpy(buffer, dma_addr, size);
207
208 /*
209 * Return the buffer to the free list by setting the corresponding entries to
210 * indicate the number of contigous entries available. While returning the
211 * entries to the free list, we merge the entries with slots below and above the
212 * pool being returned.
213 */
214 spin_lock_irqsave(&io_tlb_lock, flags);
215 {
216 int count = ((index + nslots) < io_tlb_nslabs ? io_tlb_list[index + nslots] : 0);
217 /*
218 * Step 1: return the slots to the free list, merging the slots with
219 * superceeding slots
220 */
221 for (i = index + nslots - 1; i >= index; i--)
222 io_tlb_list[i] = ++count;
223 /*
224 * Step 2: merge the returned slots with the preceeding slots, if
225 * available (non zero)
226 */
227 for (i = index - 1; (i >= 0) && io_tlb_list[i]; i--)
228 io_tlb_list[i] = ++count;
229 }
230 spin_unlock_irqrestore(&io_tlb_lock, flags);
231 }
232
233 static void
234 sync_single (struct pci_dev *hwdev, char *dma_addr, size_t size, int direction)
235 {
236 int index = (dma_addr - io_tlb_start) >> IO_TLB_SHIFT;
237 char *buffer = io_tlb_orig_addr[index];
238
239 /*
240 * bounce... copy the data back into/from the original buffer
241 * XXX How do you handle PCI_DMA_BIDIRECTIONAL here ?
242 */
243 if (direction == PCI_DMA_FROMDEVICE)
244 memcpy(buffer, dma_addr, size);
245 else if (direction == PCI_DMA_TODEVICE)
246 memcpy(dma_addr, buffer, size);
247 else
248 BUG();
249 }
250
251 void *
252 swiotlb_alloc_consistent (struct pci_dev *hwdev, size_t size, dma_addr_t *dma_handle)
253 {
254 unsigned long pci_addr;
255 int gfp = GFP_ATOMIC;
256 void *ret;
257
258 if (!hwdev || hwdev->dma_mask <= 0xffffffff)
259 gfp |= GFP_DMA; /* XXX fix me: should change this to GFP_32BIT or ZONE_32BIT */
260 ret = (void *)__get_free_pages(gfp, get_order(size));
261 if (!ret)
262 return NULL;
263
264 memset(ret, 0, size);
265 pci_addr = virt_to_phys(ret);
266 if (hwdev && (pci_addr & ~hwdev->dma_mask) != 0)
267 panic("swiotlb_alloc_consistent: allocated memory is out of range for PCI device");
268 *dma_handle = pci_addr;
269 return ret;
270 }
271
272 void
273 swiotlb_free_consistent (struct pci_dev *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
274 {
275 free_pages((unsigned long) vaddr, get_order(size));
276 }
277
278 /*
279 * Map a single buffer of the indicated size for DMA in streaming mode. The PCI address
280 * to use is returned.
281 *
282 * Once the device is given the dma address, the device owns this memory until either
283 * swiotlb_unmap_single or swiotlb_dma_sync_single is performed.
284 */
285 dma_addr_t
286 swiotlb_map_single (struct pci_dev *hwdev, void *ptr, size_t size, int direction)
287 {
288 unsigned long pci_addr = virt_to_phys(ptr);
289
290 if (direction == PCI_DMA_NONE)
291 BUG();
292 /*
293 * Check if the PCI device can DMA to ptr... if so, just return ptr
294 */
295 if ((pci_addr & ~hwdev->dma_mask) == 0)
296 /*
297 * Device is bit capable of DMA'ing to the buffer... just return the PCI
298 * address of ptr
299 */
300 return pci_addr;
301
302 /*
303 * get a bounce buffer:
304 */
305 pci_addr = virt_to_phys(map_single(hwdev, ptr, size, direction));
306
307 /*
308 * Ensure that the address returned is DMA'ble:
309 */
310 if ((pci_addr & ~hwdev->dma_mask) != 0)
311 panic("map_single: bounce buffer is not DMA'ble");
312
313 return pci_addr;
314 }
315
316 /*
317 * Since DMA is i-cache coherent, any (complete) pages that were written via
318 * DMA can be marked as "clean" so that update_mmu_cache() doesn't have to
319 * flush them when they get mapped into an executable vm-area.
320 */
321 static void
322 mark_clean (void *addr, size_t size)
323 {
324 unsigned long pg_addr, end;
325
326 pg_addr = PAGE_ALIGN((unsigned long) addr);
327 end = (unsigned long) addr + size;
328 while (pg_addr + PAGE_SIZE <= end) {
329 struct page *page = virt_to_page(pg_addr);
330 set_bit(PG_arch_1, &page->flags);
331 pg_addr += PAGE_SIZE;
332 }
333 }
334
335 /*
336 * Unmap a single streaming mode DMA translation. The dma_addr and size must match what
337 * was provided for in a previous swiotlb_map_single call. All other usages are
338 * undefined.
339 *
340 * After this call, reads by the cpu to the buffer are guarenteed to see whatever the
341 * device wrote there.
342 */
343 void
344 swiotlb_unmap_single (struct pci_dev *hwdev, dma_addr_t pci_addr, size_t size, int direction)
345 {
346 char *dma_addr = phys_to_virt(pci_addr);
347
348 if (direction == PCI_DMA_NONE)
349 BUG();
350 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
351 unmap_single(hwdev, dma_addr, size, direction);
352 else if (direction == PCI_DMA_FROMDEVICE)
353 mark_clean(dma_addr, size);
354 }
355
356 /*
357 * Make physical memory consistent for a single streaming mode DMA translation after a
358 * transfer.
359 *
360 * If you perform a swiotlb_map_single() but wish to interrogate the buffer using the cpu,
361 * yet do not wish to teardown the PCI dma mapping, you must call this function before
362 * doing so. At the next point you give the PCI dma address back to the card, the device
363 * again owns the buffer.
364 */
365 void
366 swiotlb_sync_single (struct pci_dev *hwdev, dma_addr_t pci_addr, size_t size, int direction)
367 {
368 char *dma_addr = phys_to_virt(pci_addr);
369
370 if (direction == PCI_DMA_NONE)
371 BUG();
372 if (dma_addr >= io_tlb_start && dma_addr < io_tlb_end)
373 sync_single(hwdev, dma_addr, size, direction);
374 else if (direction == PCI_DMA_FROMDEVICE)
375 mark_clean(dma_addr, size);
376 }
377
378 /*
379 * Map a set of buffers described by scatterlist in streaming mode for DMA. This is the
380 * scather-gather version of the above swiotlb_map_single interface. Here the scatter
381 * gather list elements are each tagged with the appropriate dma address and length. They
382 * are obtained via sg_dma_{address,length}(SG).
383 *
384 * NOTE: An implementation may be able to use a smaller number of
385 * DMA address/length pairs than there are SG table elements.
386 * (for example via virtual mapping capabilities)
387 * The routine returns the number of addr/length pairs actually
388 * used, at most nents.
389 *
390 * Device ownership issues as mentioned above for swiotlb_map_single are the same here.
391 */
392 int
393 swiotlb_map_sg (struct pci_dev *hwdev, struct scatterlist *sg, int nelems, int direction)
394 {
395 int i;
396
397 if (direction == PCI_DMA_NONE)
398 BUG();
399
400 for (i = 0; i < nelems; i++, sg++) {
401 sg->orig_address = sg->address;
402 if ((virt_to_phys(sg->address) & ~hwdev->dma_mask) != 0) {
403 sg->address = map_single(hwdev, sg->address, sg->length, direction);
404 }
405 }
406 return nelems;
407 }
408
409 /*
410 * Unmap a set of streaming mode DMA translations. Again, cpu read rules concerning calls
411 * here are the same as for swiotlb_unmap_single() above.
412 */
413 void
414 swiotlb_unmap_sg (struct pci_dev *hwdev, struct scatterlist *sg, int nelems, int direction)
415 {
416 int i;
417
418 if (direction == PCI_DMA_NONE)
419 BUG();
420
421 for (i = 0; i < nelems; i++, sg++)
422 if (sg->orig_address != sg->address) {
423 unmap_single(hwdev, sg->address, sg->length, direction);
424 sg->address = sg->orig_address;
425 } else if (direction == PCI_DMA_FROMDEVICE)
426 mark_clean(sg->address, sg->length);
427 }
428
429 /*
430 * Make physical memory consistent for a set of streaming mode DMA translations after a
431 * transfer.
432 *
433 * The same as swiotlb_dma_sync_single but for a scatter-gather list, same rules and
434 * usage.
435 */
436 void
437 swiotlb_sync_sg (struct pci_dev *hwdev, struct scatterlist *sg, int nelems, int direction)
438 {
439 int i;
440
441 if (direction == PCI_DMA_NONE)
442 BUG();
443
444 for (i = 0; i < nelems; i++, sg++)
445 if (sg->orig_address != sg->address)
446 sync_single(hwdev, sg->address, sg->length, direction);
447 }
448
449 unsigned long
450 swiotlb_dma_address (struct scatterlist *sg)
451 {
452 return virt_to_phys(sg->address);
453 }
454
455 EXPORT_SYMBOL(swiotlb_init);
456 EXPORT_SYMBOL(swiotlb_map_single);
457 EXPORT_SYMBOL(swiotlb_unmap_single);
458 EXPORT_SYMBOL(swiotlb_map_sg);
459 EXPORT_SYMBOL(swiotlb_unmap_sg);
460 EXPORT_SYMBOL(swiotlb_sync_single);
461 EXPORT_SYMBOL(swiotlb_sync_sg);
462 EXPORT_SYMBOL(swiotlb_dma_address);
463 EXPORT_SYMBOL(swiotlb_alloc_consistent);
464 EXPORT_SYMBOL(swiotlb_free_consistent);
465