| Adrian Bunk | 88278ca | 2008-05-19 16:53:02 -0700 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  * ioport.c:  Simple io mapping allocator. | 
 | 3 |  * | 
 | 4 |  * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) | 
 | 5 |  * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx) | 
 | 6 |  * | 
 | 7 |  * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev. | 
 | 8 |  * | 
 | 9 |  * 2000/01/29 | 
 | 10 |  * <rth> zait: as long as pci_alloc_consistent produces something addressable,  | 
 | 11 |  *	things are ok. | 
 | 12 |  * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a | 
 | 13 |  *	pointer into the big page mapping | 
 | 14 |  * <rth> zait: so what? | 
 | 15 |  * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page())) | 
 | 16 |  * <zaitcev> Hmm | 
 | 17 |  * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())). | 
 | 18 |  *	So far so good. | 
 | 19 |  * <zaitcev> Now, driver calls pci_free_consistent(with result of | 
 | 20 |  *	remap_it_my_way()). | 
 | 21 |  * <zaitcev> How do you find the address to pass to free_pages()? | 
 | 22 |  * <rth> zait: walk the page tables?  It's only two or three level after all. | 
 | 23 |  * <rth> zait: you have to walk them anyway to remove the mapping. | 
 | 24 |  * <zaitcev> Hmm | 
 | 25 |  * <zaitcev> Sounds reasonable | 
 | 26 |  */ | 
 | 27 |  | 
| David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 28 | #include <linux/module.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/sched.h> | 
 | 30 | #include <linux/kernel.h> | 
 | 31 | #include <linux/errno.h> | 
 | 32 | #include <linux/types.h> | 
 | 33 | #include <linux/ioport.h> | 
 | 34 | #include <linux/mm.h> | 
 | 35 | #include <linux/slab.h> | 
 | 36 | #include <linux/pci.h>		/* struct pci_dev */ | 
 | 37 | #include <linux/proc_fs.h> | 
| Alexey Dobriyan | e7a088f | 2009-09-01 17:54:07 -0700 | [diff] [blame] | 38 | #include <linux/seq_file.h> | 
| Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 39 | #include <linux/scatterlist.h> | 
| Stephen Rothwell | 764f257 | 2008-08-07 15:33:36 -0700 | [diff] [blame] | 40 | #include <linux/of_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 |  | 
 | 42 | #include <asm/io.h> | 
 | 43 | #include <asm/vaddrs.h> | 
 | 44 | #include <asm/oplib.h> | 
| David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 45 | #include <asm/prom.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | #include <asm/page.h> | 
 | 47 | #include <asm/pgalloc.h> | 
 | 48 | #include <asm/dma.h> | 
| David S. Miller | e003934 | 2008-08-25 22:47:20 -0700 | [diff] [blame] | 49 | #include <asm/iommu.h> | 
 | 50 | #include <asm/io-unit.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 |  | 
 | 52 | #define mmu_inval_dma_area(p, l)	/* Anton pulled it out for 2.4.0-xx */ | 
 | 53 |  | 
| Adrian Bunk | c61c65c | 2008-06-05 11:40:58 -0700 | [diff] [blame] | 54 | static struct resource *_sparc_find_resource(struct resource *r, | 
 | 55 | 					     unsigned long); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 |  | 
 | 57 | static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz); | 
 | 58 | static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys, | 
 | 59 |     unsigned long size, char *name); | 
 | 60 | static void _sparc_free_io(struct resource *res); | 
 | 61 |  | 
| Adrian Bunk | c61c65c | 2008-06-05 11:40:58 -0700 | [diff] [blame] | 62 | static void register_proc_sparc_ioport(void); | 
 | 63 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | /* This points to the next to use virtual memory for DVMA mappings */ | 
 | 65 | static struct resource _sparc_dvma = { | 
 | 66 | 	.name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1 | 
 | 67 | }; | 
 | 68 | /* This points to the start of I/O mappings, cluable from outside. */ | 
 | 69 | /*ext*/ struct resource sparc_iomap = { | 
 | 70 | 	.name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1 | 
 | 71 | }; | 
 | 72 |  | 
 | 73 | /* | 
 | 74 |  * Our mini-allocator... | 
 | 75 |  * Boy this is gross! We need it because we must map I/O for | 
 | 76 |  * timers and interrupt controller before the kmalloc is available. | 
 | 77 |  */ | 
 | 78 |  | 
 | 79 | #define XNMLN  15 | 
 | 80 | #define XNRES  10	/* SS-10 uses 8 */ | 
 | 81 |  | 
 | 82 | struct xresource { | 
 | 83 | 	struct resource xres;	/* Must be first */ | 
 | 84 | 	int xflag;		/* 1 == used */ | 
 | 85 | 	char xname[XNMLN+1]; | 
 | 86 | }; | 
 | 87 |  | 
 | 88 | static struct xresource xresv[XNRES]; | 
 | 89 |  | 
 | 90 | static struct xresource *xres_alloc(void) { | 
 | 91 | 	struct xresource *xrp; | 
 | 92 | 	int n; | 
 | 93 |  | 
 | 94 | 	xrp = xresv; | 
 | 95 | 	for (n = 0; n < XNRES; n++) { | 
 | 96 | 		if (xrp->xflag == 0) { | 
 | 97 | 			xrp->xflag = 1; | 
 | 98 | 			return xrp; | 
 | 99 | 		} | 
 | 100 | 		xrp++; | 
 | 101 | 	} | 
 | 102 | 	return NULL; | 
 | 103 | } | 
 | 104 |  | 
 | 105 | static void xres_free(struct xresource *xrp) { | 
 | 106 | 	xrp->xflag = 0; | 
 | 107 | } | 
 | 108 |  | 
 | 109 | /* | 
 | 110 |  * These are typically used in PCI drivers | 
 | 111 |  * which are trying to be cross-platform. | 
 | 112 |  * | 
 | 113 |  * Bus type is always zero on IIep. | 
 | 114 |  */ | 
 | 115 | void __iomem *ioremap(unsigned long offset, unsigned long size) | 
 | 116 | { | 
 | 117 | 	char name[14]; | 
 | 118 |  | 
 | 119 | 	sprintf(name, "phys_%08x", (u32)offset); | 
 | 120 | 	return _sparc_alloc_io(0, offset, size, name); | 
 | 121 | } | 
| Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 122 | EXPORT_SYMBOL(ioremap); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 |  | 
 | 124 | /* | 
 | 125 |  * Comlimentary to ioremap(). | 
 | 126 |  */ | 
 | 127 | void iounmap(volatile void __iomem *virtual) | 
 | 128 | { | 
 | 129 | 	unsigned long vaddr = (unsigned long) virtual & PAGE_MASK; | 
 | 130 | 	struct resource *res; | 
 | 131 |  | 
 | 132 | 	if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) { | 
 | 133 | 		printk("free_io/iounmap: cannot free %lx\n", vaddr); | 
 | 134 | 		return; | 
 | 135 | 	} | 
 | 136 | 	_sparc_free_io(res); | 
 | 137 |  | 
 | 138 | 	if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) { | 
 | 139 | 		xres_free((struct xresource *)res); | 
 | 140 | 	} else { | 
 | 141 | 		kfree(res); | 
 | 142 | 	} | 
 | 143 | } | 
| Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 144 | EXPORT_SYMBOL(iounmap); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 |  | 
| David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 146 | void __iomem *of_ioremap(struct resource *res, unsigned long offset, | 
 | 147 | 			 unsigned long size, char *name) | 
 | 148 | { | 
 | 149 | 	return _sparc_alloc_io(res->flags & 0xF, | 
 | 150 | 			       res->start + offset, | 
 | 151 | 			       size, name); | 
 | 152 | } | 
 | 153 | EXPORT_SYMBOL(of_ioremap); | 
 | 154 |  | 
| David S. Miller | e3a411a | 2006-12-28 21:01:32 -0800 | [diff] [blame] | 155 | void of_iounmap(struct resource *res, void __iomem *base, unsigned long size) | 
| David S. Miller | 3ca9fab | 2006-06-29 14:35:33 -0700 | [diff] [blame] | 156 | { | 
 | 157 | 	iounmap(base); | 
 | 158 | } | 
 | 159 | EXPORT_SYMBOL(of_iounmap); | 
 | 160 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 |  * Meat of mapping | 
 | 163 |  */ | 
 | 164 | static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys, | 
 | 165 |     unsigned long size, char *name) | 
 | 166 | { | 
 | 167 | 	static int printed_full; | 
 | 168 | 	struct xresource *xres; | 
 | 169 | 	struct resource *res; | 
 | 170 | 	char *tack; | 
 | 171 | 	int tlen; | 
 | 172 | 	void __iomem *va;	/* P3 diag */ | 
 | 173 |  | 
 | 174 | 	if (name == NULL) name = "???"; | 
 | 175 |  | 
 | 176 | 	if ((xres = xres_alloc()) != 0) { | 
 | 177 | 		tack = xres->xname; | 
 | 178 | 		res = &xres->xres; | 
 | 179 | 	} else { | 
 | 180 | 		if (!printed_full) { | 
 | 181 | 			printk("ioremap: done with statics, switching to malloc\n"); | 
 | 182 | 			printed_full = 1; | 
 | 183 | 		} | 
 | 184 | 		tlen = strlen(name); | 
 | 185 | 		tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL); | 
 | 186 | 		if (tack == NULL) return NULL; | 
 | 187 | 		memset(tack, 0, sizeof(struct resource)); | 
 | 188 | 		res = (struct resource *) tack; | 
 | 189 | 		tack += sizeof (struct resource); | 
 | 190 | 	} | 
 | 191 |  | 
 | 192 | 	strlcpy(tack, name, XNMLN+1); | 
 | 193 | 	res->name = tack; | 
 | 194 |  | 
 | 195 | 	va = _sparc_ioremap(res, busno, phys, size); | 
 | 196 | 	/* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */ | 
 | 197 | 	return va; | 
 | 198 | } | 
 | 199 |  | 
 | 200 | /* | 
 | 201 |  */ | 
 | 202 | static void __iomem * | 
 | 203 | _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz) | 
 | 204 | { | 
 | 205 | 	unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK); | 
 | 206 |  | 
 | 207 | 	if (allocate_resource(&sparc_iomap, res, | 
 | 208 | 	    (offset + sz + PAGE_SIZE-1) & PAGE_MASK, | 
 | 209 | 	    sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) { | 
 | 210 | 		/* Usually we cannot see printks in this case. */ | 
 | 211 | 		prom_printf("alloc_io_res(%s): cannot occupy\n", | 
 | 212 | 		    (res->name != NULL)? res->name: "???"); | 
 | 213 | 		prom_halt(); | 
 | 214 | 	} | 
 | 215 |  | 
 | 216 | 	pa &= PAGE_MASK; | 
 | 217 | 	sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1); | 
 | 218 |  | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 219 | 	return (void __iomem *)(unsigned long)(res->start + offset); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } | 
 | 221 |  | 
 | 222 | /* | 
 | 223 |  * Comlimentary to _sparc_ioremap(). | 
 | 224 |  */ | 
 | 225 | static void _sparc_free_io(struct resource *res) | 
 | 226 | { | 
 | 227 | 	unsigned long plen; | 
 | 228 |  | 
 | 229 | 	plen = res->end - res->start + 1; | 
| Eric Sesterhenn | 30d4d1f | 2006-03-10 02:55:20 -0800 | [diff] [blame] | 230 | 	BUG_ON((plen & (PAGE_SIZE-1)) != 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | 	sparc_unmapiorange(res->start, plen); | 
 | 232 | 	release_resource(res); | 
 | 233 | } | 
 | 234 |  | 
 | 235 | #ifdef CONFIG_SBUS | 
 | 236 |  | 
| David S. Miller | 63237ee | 2008-08-26 23:33:42 -0700 | [diff] [blame] | 237 | void sbus_set_sbus64(struct device *dev, int x) | 
| David S. Miller | 8fae097 | 2006-06-20 15:23:28 -0700 | [diff] [blame] | 238 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | 	printk("sbus_set_sbus64: unsupported\n"); | 
 | 240 | } | 
| Sam Ravnborg | 6943f3d | 2009-01-08 16:58:05 -0800 | [diff] [blame] | 241 | EXPORT_SYMBOL(sbus_set_sbus64); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 |  | 
 | 243 | /* | 
 | 244 |  * Allocate a chunk of memory suitable for DMA. | 
 | 245 |  * Typically devices use them for control blocks. | 
 | 246 |  * CPU may access them without any explicit flushing. | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 |  */ | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 248 | static void *sbus_alloc_coherent(struct device *dev, size_t len, | 
 | 249 | 				 dma_addr_t *dma_addrp, gfp_t gfp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { | 
| David S. Miller | 7a715f4 | 2008-08-27 18:37:58 -0700 | [diff] [blame] | 251 | 	struct of_device *op = to_of_device(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | 	unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK; | 
 | 253 | 	unsigned long va; | 
 | 254 | 	struct resource *res; | 
 | 255 | 	int order; | 
 | 256 |  | 
| Paulius Zaleckas | efad798b | 2008-02-03 15:42:53 +0200 | [diff] [blame] | 257 | 	/* XXX why are some lengths signed, others unsigned? */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | 	if (len <= 0) { | 
 | 259 | 		return NULL; | 
 | 260 | 	} | 
 | 261 | 	/* XXX So what is maxphys for us and how do drivers know it? */ | 
 | 262 | 	if (len > 256*1024) {			/* __get_free_pages() limit */ | 
 | 263 | 		return NULL; | 
 | 264 | 	} | 
 | 265 |  | 
 | 266 | 	order = get_order(len_total); | 
| Hugh Dickins | f3d48f0 | 2005-11-21 21:32:22 -0800 | [diff] [blame] | 267 | 	if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | 		goto err_nopages; | 
 | 269 |  | 
| Yan Burman | c80892d | 2006-11-30 17:07:04 -0800 | [diff] [blame] | 270 | 	if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | 		goto err_nomem; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 |  | 
 | 273 | 	if (allocate_resource(&_sparc_dvma, res, len_total, | 
 | 274 | 	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { | 
 | 275 | 		printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total); | 
 | 276 | 		goto err_nova; | 
 | 277 | 	} | 
 | 278 | 	mmu_inval_dma_area(va, len_total); | 
 | 279 | 	// XXX The mmu_map_dma_area does this for us below, see comments. | 
 | 280 | 	// sparc_mapiorange(0, virt_to_phys(va), res->start, len_total); | 
 | 281 | 	/* | 
 | 282 | 	 * XXX That's where sdev would be used. Currently we load | 
 | 283 | 	 * all iommu tables with the same translations. | 
 | 284 | 	 */ | 
| David S. Miller | 4b1c5df | 2008-08-27 18:40:38 -0700 | [diff] [blame] | 285 | 	if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | 		goto err_noiommu; | 
 | 287 |  | 
| David S. Miller | 7a715f4 | 2008-08-27 18:37:58 -0700 | [diff] [blame] | 288 | 	res->name = op->node->name; | 
| Martin Habets | 4cfbd7e | 2006-05-07 23:43:19 -0700 | [diff] [blame] | 289 |  | 
| Greg Kroah-Hartman | d75fc8b | 2006-06-12 16:09:23 -0700 | [diff] [blame] | 290 | 	return (void *)(unsigned long)res->start; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 |  | 
 | 292 | err_noiommu: | 
 | 293 | 	release_resource(res); | 
 | 294 | err_nova: | 
 | 295 | 	free_pages(va, order); | 
 | 296 | err_nomem: | 
 | 297 | 	kfree(res); | 
 | 298 | err_nopages: | 
 | 299 | 	return NULL; | 
 | 300 | } | 
 | 301 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 302 | static void sbus_free_coherent(struct device *dev, size_t n, void *p, | 
 | 303 | 			       dma_addr_t ba) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { | 
 | 305 | 	struct resource *res; | 
 | 306 | 	struct page *pgv; | 
 | 307 |  | 
 | 308 | 	if ((res = _sparc_find_resource(&_sparc_dvma, | 
 | 309 | 	    (unsigned long)p)) == NULL) { | 
 | 310 | 		printk("sbus_free_consistent: cannot free %p\n", p); | 
 | 311 | 		return; | 
 | 312 | 	} | 
 | 313 |  | 
 | 314 | 	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) { | 
 | 315 | 		printk("sbus_free_consistent: unaligned va %p\n", p); | 
 | 316 | 		return; | 
 | 317 | 	} | 
 | 318 |  | 
 | 319 | 	n = (n + PAGE_SIZE-1) & PAGE_MASK; | 
 | 320 | 	if ((res->end-res->start)+1 != n) { | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 321 | 		printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | 		    (long)((res->end-res->start)+1), n); | 
 | 323 | 		return; | 
 | 324 | 	} | 
 | 325 |  | 
 | 326 | 	release_resource(res); | 
 | 327 | 	kfree(res); | 
 | 328 |  | 
 | 329 | 	/* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */ | 
| David S. Miller | aba945e | 2008-08-27 02:20:35 -0700 | [diff] [blame] | 330 | 	pgv = virt_to_page(p); | 
| David S. Miller | 4b1c5df | 2008-08-27 18:40:38 -0700 | [diff] [blame] | 331 | 	mmu_unmap_dma_area(dev, ba, n); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 |  | 
 | 333 | 	__free_pages(pgv, get_order(n)); | 
 | 334 | } | 
 | 335 |  | 
 | 336 | /* | 
 | 337 |  * Map a chunk of memory so that devices can see it. | 
 | 338 |  * CPU view of this memory may be inconsistent with | 
 | 339 |  * a device view and explicit flushing is necessary. | 
 | 340 |  */ | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 341 | static dma_addr_t sbus_map_page(struct device *dev, struct page *page, | 
 | 342 | 				unsigned long offset, size_t len, | 
 | 343 | 				enum dma_data_direction dir, | 
 | 344 | 				struct dma_attrs *attrs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | { | 
| FUJITA Tomonori | c2c07db | 2009-08-10 11:53:15 +0900 | [diff] [blame] | 346 | 	void *va = page_address(page) + offset; | 
 | 347 |  | 
| Paulius Zaleckas | efad798b | 2008-02-03 15:42:53 +0200 | [diff] [blame] | 348 | 	/* XXX why are some lengths signed, others unsigned? */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | 	if (len <= 0) { | 
 | 350 | 		return 0; | 
 | 351 | 	} | 
 | 352 | 	/* XXX So what is maxphys for us and how do drivers know it? */ | 
 | 353 | 	if (len > 256*1024) {			/* __get_free_pages() limit */ | 
 | 354 | 		return 0; | 
 | 355 | 	} | 
| David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 356 | 	return mmu_get_scsi_one(dev, va, len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } | 
 | 358 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 359 | static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n, | 
 | 360 | 			    enum dma_data_direction dir, struct dma_attrs *attrs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { | 
| David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 362 | 	mmu_release_scsi_one(dev, ba, n); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | } | 
 | 364 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 365 | static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, | 
 | 366 | 		       enum dma_data_direction dir, struct dma_attrs *attrs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { | 
| David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 368 | 	mmu_get_scsi_sgl(dev, sg, n); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 |  | 
 | 370 | 	/* | 
 | 371 | 	 * XXX sparc64 can return a partial length here. sun4c should do this | 
 | 372 | 	 * but it currently panics if it can't fulfill the request - Anton | 
 | 373 | 	 */ | 
 | 374 | 	return n; | 
 | 375 | } | 
 | 376 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 377 | static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, | 
 | 378 | 			  enum dma_data_direction dir, struct dma_attrs *attrs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | { | 
| David S. Miller | 260489f | 2008-08-26 23:00:58 -0700 | [diff] [blame] | 380 | 	mmu_release_scsi_sgl(dev, sg, n); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } | 
 | 382 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 383 | static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg, | 
 | 384 | 				 int n,	enum dma_data_direction dir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | { | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 386 | 	BUG(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } | 
 | 388 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 389 | static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg, | 
 | 390 | 				    int n, enum dma_data_direction dir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | { | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 392 | 	BUG(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } | 
 | 394 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 395 | struct dma_map_ops sbus_dma_ops = { | 
 | 396 | 	.alloc_coherent		= sbus_alloc_coherent, | 
 | 397 | 	.free_coherent		= sbus_free_coherent, | 
 | 398 | 	.map_page		= sbus_map_page, | 
 | 399 | 	.unmap_page		= sbus_unmap_page, | 
 | 400 | 	.map_sg			= sbus_map_sg, | 
 | 401 | 	.unmap_sg		= sbus_unmap_sg, | 
 | 402 | 	.sync_sg_for_cpu	= sbus_sync_sg_for_cpu, | 
 | 403 | 	.sync_sg_for_device	= sbus_sync_sg_for_device, | 
 | 404 | }; | 
 | 405 |  | 
 | 406 | struct dma_map_ops *dma_ops = &sbus_dma_ops; | 
 | 407 | EXPORT_SYMBOL(dma_ops); | 
 | 408 |  | 
| David S. Miller | f8e4d32 | 2008-08-27 04:20:14 -0700 | [diff] [blame] | 409 | static int __init sparc_register_ioport(void) | 
| David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 410 | { | 
| David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 411 | 	register_proc_sparc_ioport(); | 
 | 412 |  | 
| David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 413 | 	return 0; | 
| David S. Miller | 576c352 | 2006-06-23 15:55:45 -0700 | [diff] [blame] | 414 | } | 
 | 415 |  | 
| David S. Miller | f8e4d32 | 2008-08-27 04:20:14 -0700 | [diff] [blame] | 416 | arch_initcall(sparc_register_ioport); | 
 | 417 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | #endif /* CONFIG_SBUS */ | 
 | 419 |  | 
 | 420 | #ifdef CONFIG_PCI | 
 | 421 |  | 
 | 422 | /* Allocate and map kernel buffer using consistent mode DMA for a device. | 
 | 423 |  * hwdev should be valid struct pci_dev pointer for PCI devices. | 
 | 424 |  */ | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 425 | static void *pci32_alloc_coherent(struct device *dev, size_t len, | 
 | 426 | 				  dma_addr_t *pba, gfp_t gfp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { | 
 | 428 | 	unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK; | 
 | 429 | 	unsigned long va; | 
 | 430 | 	struct resource *res; | 
 | 431 | 	int order; | 
 | 432 |  | 
 | 433 | 	if (len == 0) { | 
 | 434 | 		return NULL; | 
 | 435 | 	} | 
 | 436 | 	if (len > 256*1024) {			/* __get_free_pages() limit */ | 
 | 437 | 		return NULL; | 
 | 438 | 	} | 
 | 439 |  | 
 | 440 | 	order = get_order(len_total); | 
 | 441 | 	va = __get_free_pages(GFP_KERNEL, order); | 
 | 442 | 	if (va == 0) { | 
 | 443 | 		printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT); | 
 | 444 | 		return NULL; | 
 | 445 | 	} | 
 | 446 |  | 
| Yan Burman | c80892d | 2006-11-30 17:07:04 -0800 | [diff] [blame] | 447 | 	if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | 		free_pages(va, order); | 
 | 449 | 		printk("pci_alloc_consistent: no core\n"); | 
 | 450 | 		return NULL; | 
 | 451 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 |  | 
 | 453 | 	if (allocate_resource(&_sparc_dvma, res, len_total, | 
 | 454 | 	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) { | 
 | 455 | 		printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total); | 
 | 456 | 		free_pages(va, order); | 
 | 457 | 		kfree(res); | 
 | 458 | 		return NULL; | 
 | 459 | 	} | 
 | 460 | 	mmu_inval_dma_area(va, len_total); | 
 | 461 | #if 0 | 
 | 462 | /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n", | 
 | 463 |   (long)va, (long)res->start, (long)virt_to_phys(va), len_total); | 
 | 464 | #endif | 
 | 465 | 	sparc_mapiorange(0, virt_to_phys(va), res->start, len_total); | 
 | 466 |  | 
 | 467 | 	*pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */ | 
 | 468 | 	return (void *) res->start; | 
 | 469 | } | 
 | 470 |  | 
 | 471 | /* Free and unmap a consistent DMA buffer. | 
 | 472 |  * cpu_addr is what was returned from pci_alloc_consistent, | 
 | 473 |  * size must be the same as what as passed into pci_alloc_consistent, | 
 | 474 |  * and likewise dma_addr must be the same as what *dma_addrp was set to. | 
 | 475 |  * | 
| Simon Arlott | d1a78c3 | 2007-05-11 13:51:23 -0700 | [diff] [blame] | 476 |  * References to the memory and mappings associated with cpu_addr/dma_addr | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 |  * past this call are illegal. | 
 | 478 |  */ | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 479 | static void pci32_free_coherent(struct device *dev, size_t n, void *p, | 
 | 480 | 				dma_addr_t ba) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | { | 
 | 482 | 	struct resource *res; | 
 | 483 | 	unsigned long pgp; | 
 | 484 |  | 
 | 485 | 	if ((res = _sparc_find_resource(&_sparc_dvma, | 
 | 486 | 	    (unsigned long)p)) == NULL) { | 
 | 487 | 		printk("pci_free_consistent: cannot free %p\n", p); | 
 | 488 | 		return; | 
 | 489 | 	} | 
 | 490 |  | 
 | 491 | 	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) { | 
 | 492 | 		printk("pci_free_consistent: unaligned va %p\n", p); | 
 | 493 | 		return; | 
 | 494 | 	} | 
 | 495 |  | 
 | 496 | 	n = (n + PAGE_SIZE-1) & PAGE_MASK; | 
 | 497 | 	if ((res->end-res->start)+1 != n) { | 
 | 498 | 		printk("pci_free_consistent: region 0x%lx asked 0x%lx\n", | 
 | 499 | 		    (long)((res->end-res->start)+1), (long)n); | 
 | 500 | 		return; | 
 | 501 | 	} | 
 | 502 |  | 
 | 503 | 	pgp = (unsigned long) phys_to_virt(ba);	/* bus_to_virt actually */ | 
 | 504 | 	mmu_inval_dma_area(pgp, n); | 
 | 505 | 	sparc_unmapiorange((unsigned long)p, n); | 
 | 506 |  | 
 | 507 | 	release_resource(res); | 
 | 508 | 	kfree(res); | 
 | 509 |  | 
 | 510 | 	free_pages(pgp, get_order(n)); | 
 | 511 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 |  | 
 | 513 | /* | 
 | 514 |  * Same as pci_map_single, but with pages. | 
 | 515 |  */ | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 516 | static dma_addr_t pci32_map_page(struct device *dev, struct page *page, | 
 | 517 | 				 unsigned long offset, size_t size, | 
 | 518 | 				 enum dma_data_direction dir, | 
 | 519 | 				 struct dma_attrs *attrs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | 	/* IIep is write-through, not flushing. */ | 
 | 522 | 	return page_to_phys(page) + offset; | 
 | 523 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 |  | 
 | 525 | /* Map a set of buffers described by scatterlist in streaming | 
 | 526 |  * mode for DMA.  This is the scather-gather version of the | 
 | 527 |  * above pci_map_single interface.  Here the scatter gather list | 
 | 528 |  * elements are each tagged with the appropriate dma address | 
 | 529 |  * and length.  They are obtained via sg_dma_{address,length}(SG). | 
 | 530 |  * | 
 | 531 |  * NOTE: An implementation may be able to use a smaller number of | 
 | 532 |  *       DMA address/length pairs than there are SG table elements. | 
 | 533 |  *       (for example via virtual mapping capabilities) | 
 | 534 |  *       The routine returns the number of addr/length pairs actually | 
 | 535 |  *       used, at most nents. | 
 | 536 |  * | 
 | 537 |  * Device ownership issues as mentioned above for pci_map_single are | 
 | 538 |  * the same here. | 
 | 539 |  */ | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 540 | static int pci32_map_sg(struct device *device, struct scatterlist *sgl, | 
 | 541 | 			int nents, enum dma_data_direction dir, | 
 | 542 | 			struct dma_attrs *attrs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | { | 
| Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 544 | 	struct scatterlist *sg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | 	int n; | 
 | 546 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | 	/* IIep is write-through, not flushing. */ | 
| Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 548 | 	for_each_sg(sgl, sg, nents, n) { | 
| Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 549 | 		BUG_ON(page_address(sg_page(sg)) == NULL); | 
| Robert Reif | aa83a26 | 2008-12-11 20:24:58 -0800 | [diff] [blame] | 550 | 		sg->dma_address = virt_to_phys(sg_virt(sg)); | 
 | 551 | 		sg->dma_length = sg->length; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | 	} | 
 | 553 | 	return nents; | 
 | 554 | } | 
 | 555 |  | 
 | 556 | /* Unmap a set of streaming mode DMA translations. | 
 | 557 |  * Again, cpu read rules concerning calls here are the same as for | 
 | 558 |  * pci_unmap_single() above. | 
 | 559 |  */ | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 560 | static void pci32_unmap_sg(struct device *dev, struct scatterlist *sgl, | 
 | 561 | 			   int nents, enum dma_data_direction dir, | 
 | 562 | 			   struct dma_attrs *attrs) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | { | 
| Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 564 | 	struct scatterlist *sg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | 	int n; | 
 | 566 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 567 | 	if (dir != PCI_DMA_TODEVICE) { | 
| Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 568 | 		for_each_sg(sgl, sg, nents, n) { | 
| Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 569 | 			BUG_ON(page_address(sg_page(sg)) == NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | 			mmu_inval_dma_area( | 
| Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 571 | 			    (unsigned long) page_address(sg_page(sg)), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | 			    (sg->length + PAGE_SIZE-1) & PAGE_MASK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | 		} | 
 | 574 | 	} | 
 | 575 | } | 
 | 576 |  | 
 | 577 | /* Make physical memory consistent for a single | 
 | 578 |  * streaming mode DMA translation before or after a transfer. | 
 | 579 |  * | 
 | 580 |  * If you perform a pci_map_single() but wish to interrogate the | 
 | 581 |  * buffer using the cpu, yet do not wish to teardown the PCI dma | 
 | 582 |  * mapping, you must call this function before doing so.  At the | 
 | 583 |  * next point you give the PCI dma address back to the card, you | 
 | 584 |  * must first perform a pci_dma_sync_for_device, and then the | 
 | 585 |  * device again owns the buffer. | 
 | 586 |  */ | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 587 | static void pci32_sync_single_for_cpu(struct device *dev, dma_addr_t ba, | 
 | 588 | 				      size_t size, enum dma_data_direction dir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | { | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 590 | 	if (dir != PCI_DMA_TODEVICE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | 		mmu_inval_dma_area((unsigned long)phys_to_virt(ba), | 
 | 592 | 		    (size + PAGE_SIZE-1) & PAGE_MASK); | 
 | 593 | 	} | 
 | 594 | } | 
 | 595 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 596 | static void pci32_sync_single_for_device(struct device *dev, dma_addr_t ba, | 
 | 597 | 					 size_t size, enum dma_data_direction dir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | { | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 599 | 	if (dir != PCI_DMA_TODEVICE) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | 		mmu_inval_dma_area((unsigned long)phys_to_virt(ba), | 
 | 601 | 		    (size + PAGE_SIZE-1) & PAGE_MASK); | 
 | 602 | 	} | 
 | 603 | } | 
 | 604 |  | 
 | 605 | /* Make physical memory consistent for a set of streaming | 
 | 606 |  * mode DMA translations after a transfer. | 
 | 607 |  * | 
 | 608 |  * The same as pci_dma_sync_single_* but for a scatter-gather list, | 
 | 609 |  * same rules and usage. | 
 | 610 |  */ | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 611 | static void pci32_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl, | 
 | 612 | 				  int nents, enum dma_data_direction dir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | { | 
| Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 614 | 	struct scatterlist *sg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | 	int n; | 
 | 616 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 617 | 	if (dir != PCI_DMA_TODEVICE) { | 
| Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 618 | 		for_each_sg(sgl, sg, nents, n) { | 
| Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 619 | 			BUG_ON(page_address(sg_page(sg)) == NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | 			mmu_inval_dma_area( | 
| Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 621 | 			    (unsigned long) page_address(sg_page(sg)), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | 			    (sg->length + PAGE_SIZE-1) & PAGE_MASK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | 		} | 
 | 624 | 	} | 
 | 625 | } | 
 | 626 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 627 | static void pci32_sync_sg_for_device(struct device *device, struct scatterlist *sgl, | 
 | 628 | 				     int nents, enum dma_data_direction dir) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { | 
| Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 630 | 	struct scatterlist *sg; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | 	int n; | 
 | 632 |  | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 633 | 	if (dir != PCI_DMA_TODEVICE) { | 
| Jens Axboe | 0912a5d | 2007-05-14 15:44:38 +0200 | [diff] [blame] | 634 | 		for_each_sg(sgl, sg, nents, n) { | 
| Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 635 | 			BUG_ON(page_address(sg_page(sg)) == NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | 			mmu_inval_dma_area( | 
| Jens Axboe | 58b053e | 2007-10-22 20:02:46 +0200 | [diff] [blame] | 637 | 			    (unsigned long) page_address(sg_page(sg)), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | 			    (sg->length + PAGE_SIZE-1) & PAGE_MASK); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | 		} | 
 | 640 | 	} | 
 | 641 | } | 
| FUJITA Tomonori | ee664a9 | 2009-08-10 11:53:16 +0900 | [diff] [blame] | 642 |  | 
 | 643 | struct dma_map_ops pci32_dma_ops = { | 
 | 644 | 	.alloc_coherent		= pci32_alloc_coherent, | 
 | 645 | 	.free_coherent		= pci32_free_coherent, | 
 | 646 | 	.map_page		= pci32_map_page, | 
 | 647 | 	.map_sg			= pci32_map_sg, | 
 | 648 | 	.unmap_sg		= pci32_unmap_sg, | 
 | 649 | 	.sync_single_for_cpu	= pci32_sync_single_for_cpu, | 
 | 650 | 	.sync_single_for_device	= pci32_sync_single_for_device, | 
 | 651 | 	.sync_sg_for_cpu	= pci32_sync_sg_for_cpu, | 
 | 652 | 	.sync_sg_for_device	= pci32_sync_sg_for_device, | 
 | 653 | }; | 
 | 654 | EXPORT_SYMBOL(pci32_dma_ops); | 
 | 655 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | #endif /* CONFIG_PCI */ | 
 | 657 |  | 
| FUJITA Tomonori | 451d740 | 2009-08-10 11:53:17 +0900 | [diff] [blame] | 658 | /* | 
 | 659 |  * Return whether the given PCI device DMA address mask can be | 
 | 660 |  * supported properly.  For example, if your device can only drive the | 
 | 661 |  * low 24-bits during PCI bus mastering, then you would pass | 
 | 662 |  * 0x00ffffff as the mask to this function. | 
 | 663 |  */ | 
 | 664 | int dma_supported(struct device *dev, u64 mask) | 
 | 665 | { | 
 | 666 | #ifdef CONFIG_PCI | 
 | 667 | 	if (dev->bus == &pci_bus_type) | 
 | 668 | 		return 1; | 
 | 669 | #endif | 
 | 670 | 	return 0; | 
 | 671 | } | 
 | 672 | EXPORT_SYMBOL(dma_supported); | 
 | 673 |  | 
 | 674 | int dma_set_mask(struct device *dev, u64 dma_mask) | 
 | 675 | { | 
 | 676 | #ifdef CONFIG_PCI | 
 | 677 | 	if (dev->bus == &pci_bus_type) | 
 | 678 | 		return pci_set_dma_mask(to_pci_dev(dev), dma_mask); | 
 | 679 | #endif | 
 | 680 | 	return -EOPNOTSUPP; | 
 | 681 | } | 
 | 682 | EXPORT_SYMBOL(dma_set_mask); | 
 | 683 |  | 
 | 684 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | #ifdef CONFIG_PROC_FS | 
 | 686 |  | 
| Alexey Dobriyan | e7a088f | 2009-09-01 17:54:07 -0700 | [diff] [blame] | 687 | static int sparc_io_proc_show(struct seq_file *m, void *v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | { | 
| Alexey Dobriyan | e7a088f | 2009-09-01 17:54:07 -0700 | [diff] [blame] | 689 | 	struct resource *root = m->private, *r; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | 	const char *nm; | 
 | 691 |  | 
| Alexey Dobriyan | e7a088f | 2009-09-01 17:54:07 -0700 | [diff] [blame] | 692 | 	for (r = root->child; r != NULL; r = r->sibling) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | 		if ((nm = r->name) == 0) nm = "???"; | 
| Alexey Dobriyan | e7a088f | 2009-09-01 17:54:07 -0700 | [diff] [blame] | 694 | 		seq_printf(m, "%016llx-%016llx: %s\n", | 
| Greg Kroah-Hartman | 685143a | 2006-06-12 15:18:31 -0700 | [diff] [blame] | 695 | 				(unsigned long long)r->start, | 
 | 696 | 				(unsigned long long)r->end, nm); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | 	} | 
 | 698 |  | 
| Alexey Dobriyan | e7a088f | 2009-09-01 17:54:07 -0700 | [diff] [blame] | 699 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | } | 
 | 701 |  | 
| Alexey Dobriyan | e7a088f | 2009-09-01 17:54:07 -0700 | [diff] [blame] | 702 | static int sparc_io_proc_open(struct inode *inode, struct file *file) | 
 | 703 | { | 
 | 704 | 	return single_open(file, sparc_io_proc_show, PDE(inode)->data); | 
 | 705 | } | 
 | 706 |  | 
 | 707 | static const struct file_operations sparc_io_proc_fops = { | 
 | 708 | 	.owner		= THIS_MODULE, | 
 | 709 | 	.open		= sparc_io_proc_open, | 
 | 710 | 	.read		= seq_read, | 
 | 711 | 	.llseek		= seq_lseek, | 
 | 712 | 	.release	= single_release, | 
 | 713 | }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | #endif /* CONFIG_PROC_FS */ | 
 | 715 |  | 
 | 716 | /* | 
 | 717 |  * This is a version of find_resource and it belongs to kernel/resource.c. | 
 | 718 |  * Until we have agreement with Linus and Martin, it lingers here. | 
 | 719 |  * | 
 | 720 |  * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case. | 
 | 721 |  * This probably warrants some sort of hashing. | 
 | 722 |  */ | 
| Adrian Bunk | c61c65c | 2008-06-05 11:40:58 -0700 | [diff] [blame] | 723 | static struct resource *_sparc_find_resource(struct resource *root, | 
 | 724 | 					     unsigned long hit) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | { | 
 | 726 |         struct resource *tmp; | 
 | 727 |  | 
 | 728 | 	for (tmp = root->child; tmp != 0; tmp = tmp->sibling) { | 
 | 729 | 		if (tmp->start <= hit && tmp->end >= hit) | 
 | 730 | 			return tmp; | 
 | 731 | 	} | 
 | 732 | 	return NULL; | 
 | 733 | } | 
 | 734 |  | 
| Adrian Bunk | c61c65c | 2008-06-05 11:40:58 -0700 | [diff] [blame] | 735 | static void register_proc_sparc_ioport(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | { | 
 | 737 | #ifdef CONFIG_PROC_FS | 
| Alexey Dobriyan | e7a088f | 2009-09-01 17:54:07 -0700 | [diff] [blame] | 738 | 	proc_create_data("io_map", 0, NULL, &sparc_io_proc_fops, &sparc_iomap); | 
 | 739 | 	proc_create_data("dvma_map", 0, NULL, &sparc_io_proc_fops, &_sparc_dvma); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | #endif | 
 | 741 | } |