blob: 871be4a84c7d752c1a322ed990ff18093b192c0b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Fallback functions when the main IOMMU code is not compiled in. This
2 code is roughly equivalent to i386. */
Andrew Morton8fa3d6f2006-06-26 13:59:05 +02003#include <linux/dma-mapping.h>
Jens Axboeb922f532007-07-24 12:39:27 +02004#include <linux/scatterlist.h>
Jaswinder Singh Rajput1894e362009-03-21 17:01:25 +05305#include <linux/string.h>
6#include <linux/init.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09007#include <linux/gfp.h>
Jaswinder Singh Rajput1894e362009-03-21 17:01:25 +05308#include <linux/pci.h>
9#include <linux/mm.h>
Andrew Morton8fa3d6f2006-06-26 13:59:05 +020010
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <asm/processor.h>
Jaswinder Singh Rajput1894e362009-03-21 17:01:25 +053012#include <asm/iommu.h>
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010013#include <asm/dma.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010015static int
16check_addr(char *name, struct device *hwdev, dma_addr_t bus, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070017{
FUJITA Tomonoria4c2baa2009-07-10 10:04:55 +090018 if (hwdev && !dma_capable(hwdev, bus, size)) {
Yang Hongyang284901a2009-04-06 19:01:15 -070019 if (*hwdev->dma_mask >= DMA_BIT_MASK(32))
Andi Kleenf0fdabf2006-05-15 18:19:38 +020020 printk(KERN_ERR
Andrew Morton8fa3d6f2006-06-26 13:59:05 +020021 "nommu_%s: overflow %Lx+%zu of device mask %Lx\n",
22 name, (long long)bus, size,
23 (long long)*hwdev->dma_mask);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010024 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027}
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
FUJITA Tomonori33feffd2009-01-05 23:47:27 +090029static dma_addr_t nommu_map_page(struct device *dev, struct page *page,
30 unsigned long offset, size_t size,
31 enum dma_data_direction dir,
32 struct dma_attrs *attrs)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010033{
FUJITA Tomonori33feffd2009-01-05 23:47:27 +090034 dma_addr_t bus = page_to_phys(page) + offset;
Glauber Costa5b3e5b722008-04-08 13:20:49 -030035 WARN_ON(size == 0);
FUJITA Tomonori33feffd2009-01-05 23:47:27 +090036 if (!check_addr("map_single", dev, bus, size))
FUJITA Tomonori8fd524b2009-11-15 21:19:53 +090037 return DMA_ERROR_CODE;
Glauber Costae4dcdd62008-04-08 13:20:46 -030038 flush_write_buffers();
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010039 return bus;
40}
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010042/* Map a set of buffers described by scatterlist in streaming
43 * mode for DMA. This is the scatter-gather version of the
44 * above pci_map_single interface. Here the scatter gather list
45 * elements are each tagged with the appropriate dma address
46 * and length. They are obtained via sg_dma_{address,length}(SG).
47 *
48 * NOTE: An implementation may be able to use a smaller number of
49 * DMA address/length pairs than there are SG table elements.
50 * (for example via virtual mapping capabilities)
51 * The routine returns the number of addr/length pairs actually
52 * used, at most nents.
53 *
54 * Device ownership issues as mentioned above for pci_map_single are
55 * the same here.
56 */
Yinghai Lu1048fa52007-07-21 17:11:23 +020057static int nommu_map_sg(struct device *hwdev, struct scatterlist *sg,
FUJITA Tomonori160c1d82009-01-05 23:59:02 +090058 int nents, enum dma_data_direction dir,
59 struct dma_attrs *attrs)
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010060{
Jens Axboeb922f532007-07-24 12:39:27 +020061 struct scatterlist *s;
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010062 int i;
63
Glauber Costa5b3e5b722008-04-08 13:20:49 -030064 WARN_ON(nents == 0 || sg[0].length == 0);
65
Jens Axboeb922f532007-07-24 12:39:27 +020066 for_each_sg(sg, s, nents, i) {
Jens Axboe58b053e2007-10-22 20:02:46 +020067 BUG_ON(!sg_page(s));
Glauber Costa30db2cb2008-04-08 13:20:47 -030068 s->dma_address = sg_phys(s);
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010069 if (!check_addr("map_sg", hwdev, s->dma_address, s->length))
70 return 0;
71 s->dma_length = s->length;
72 }
Glauber Costae4dcdd62008-04-08 13:20:46 -030073 flush_write_buffers();
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +010074 return nents;
75}
76
Arnd Bergmanna8ad5682009-08-10 11:53:10 +090077static void nommu_sync_single_for_device(struct device *dev,
78 dma_addr_t addr, size_t size,
79 enum dma_data_direction dir)
80{
81 flush_write_buffers();
82}
83
84
85static void nommu_sync_sg_for_device(struct device *dev,
86 struct scatterlist *sg, int nelems,
87 enum dma_data_direction dir)
88{
89 flush_write_buffers();
90}
91
FUJITA Tomonori160c1d82009-01-05 23:59:02 +090092struct dma_map_ops nommu_dma_ops = {
Andrzej Pietrasiewiczbaa676f2012-03-27 14:28:18 +020093 .alloc = dma_generic_alloc_coherent,
Marek Szyprowski0a2b9a62011-12-29 13:09:51 +010094 .free = dma_generic_free_coherent,
Arnd Bergmanna8ad5682009-08-10 11:53:10 +090095 .map_sg = nommu_map_sg,
96 .map_page = nommu_map_page,
97 .sync_single_for_device = nommu_sync_single_for_device,
98 .sync_sg_for_device = nommu_sync_sg_for_device,
99 .is_phys = 1,
Muli Ben-Yehuda17a941d2006-01-11 22:44:42 +0100100};