ioport.c 18.6 KB
Newer Older
A
Adrian Bunk 已提交
1
/*
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * ioport.c:  Simple io mapping allocator.
 *
 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
 * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
 *
 * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
 *
 * 2000/01/29
 * <rth> zait: as long as pci_alloc_consistent produces something addressable, 
 *	things are ok.
 * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a
 *	pointer into the big page mapping
 * <rth> zait: so what?
 * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page()))
 * <zaitcev> Hmm
 * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
 *	So far so good.
 * <zaitcev> Now, driver calls pci_free_consistent(with result of
 *	remap_it_my_way()).
 * <zaitcev> How do you find the address to pass to free_pages()?
 * <rth> zait: walk the page tables?  It's only two or three level after all.
 * <rth> zait: you have to walk them anyway to remove the mapping.
 * <zaitcev> Hmm
 * <zaitcev> Sounds reasonable
 */

28
#include <linux/module.h>
L
Linus Torvalds 已提交
29 30 31 32 33 34 35 36 37
#include <linux/sched.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/types.h>
#include <linux/ioport.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/pci.h>		/* struct pci_dev */
#include <linux/proc_fs.h>
38
#include <linux/seq_file.h>
J
Jens Axboe 已提交
39
#include <linux/scatterlist.h>
40
#include <linux/of_device.h>
L
Linus Torvalds 已提交
41 42 43 44

#include <asm/io.h>
#include <asm/vaddrs.h>
#include <asm/oplib.h>
45
#include <asm/prom.h>
L
Linus Torvalds 已提交
46 47 48
#include <asm/page.h>
#include <asm/pgalloc.h>
#include <asm/dma.h>
49 50
#include <asm/iommu.h>
#include <asm/io-unit.h>
K
Konrad Eisele 已提交
51
#include <asm/leon.h>
L
Linus Torvalds 已提交
52

53 54
const struct sparc32_dma_ops *sparc32_dma_ops;

55 56 57 58 59
/* This function must make sure that caches and memory are coherent after DMA
 * On LEON systems without cache snooping it flushes the entire D-CACHE.
 */
static inline void dma_make_coherent(unsigned long pa, unsigned long len)
{
60 61 62 63
	if (sparc_cpu_model == sparc_leon) {
		if (!sparc_leon3_snooping_enabled())
			leon_flush_dcache_all();
	}
64
}
L
Linus Torvalds 已提交
65 66 67 68 69 70

static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
    unsigned long size, char *name);
static void _sparc_free_io(struct resource *res);

A
Adrian Bunk 已提交
71 72
static void register_proc_sparc_ioport(void);

L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
/* This points to the next to use virtual memory for DVMA mappings */
static struct resource _sparc_dvma = {
	.name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
};
/* This points to the start of I/O mappings, cluable from outside. */
/*ext*/ struct resource sparc_iomap = {
	.name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
};

/*
 * Our mini-allocator...
 * Boy this is gross! We need it because we must map I/O for
 * timers and interrupt controller before the kmalloc is available.
 */

#define XNMLN  15
#define XNRES  10	/* SS-10 uses 8 */

struct xresource {
	struct resource xres;	/* Must be first */
	int xflag;		/* 1 == used */
	char xname[XNMLN+1];
};

static struct xresource xresv[XNRES];

static struct xresource *xres_alloc(void) {
	struct xresource *xrp;
	int n;

	xrp = xresv;
	for (n = 0; n < XNRES; n++) {
		if (xrp->xflag == 0) {
			xrp->xflag = 1;
			return xrp;
		}
		xrp++;
	}
	return NULL;
}

static void xres_free(struct xresource *xrp) {
	xrp->xflag = 0;
}

/*
 * These are typically used in PCI drivers
 * which are trying to be cross-platform.
 *
 * Bus type is always zero on IIep.
 */
void __iomem *ioremap(unsigned long offset, unsigned long size)
{
	char name[14];

	sprintf(name, "phys_%08x", (u32)offset);
	return _sparc_alloc_io(0, offset, size, name);
}
131
EXPORT_SYMBOL(ioremap);
L
Linus Torvalds 已提交
132 133

/*
134
 * Complementary to ioremap().
L
Linus Torvalds 已提交
135 136 137 138 139 140
 */
void iounmap(volatile void __iomem *virtual)
{
	unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
	struct resource *res;

141 142 143 144 145
	/*
	 * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
	 * This probably warrants some sort of hashing.
	*/
	if ((res = lookup_resource(&sparc_iomap, vaddr)) == NULL) {
L
Linus Torvalds 已提交
146 147 148 149 150 151 152 153 154 155 156
		printk("free_io/iounmap: cannot free %lx\n", vaddr);
		return;
	}
	_sparc_free_io(res);

	if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
		xres_free((struct xresource *)res);
	} else {
		kfree(res);
	}
}
157
EXPORT_SYMBOL(iounmap);
L
Linus Torvalds 已提交
158

159 160 161 162 163 164 165 166 167
void __iomem *of_ioremap(struct resource *res, unsigned long offset,
			 unsigned long size, char *name)
{
	return _sparc_alloc_io(res->flags & 0xF,
			       res->start + offset,
			       size, name);
}
EXPORT_SYMBOL(of_ioremap);

168
void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
169 170 171 172 173
{
	iounmap(base);
}
EXPORT_SYMBOL(of_iounmap);

L
Linus Torvalds 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
/*
 * Meat of mapping
 */
static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
    unsigned long size, char *name)
{
	static int printed_full;
	struct xresource *xres;
	struct resource *res;
	char *tack;
	int tlen;
	void __iomem *va;	/* P3 diag */

	if (name == NULL) name = "???";

189
	if ((xres = xres_alloc()) != NULL) {
L
Linus Torvalds 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
		tack = xres->xname;
		res = &xres->xres;
	} else {
		if (!printed_full) {
			printk("ioremap: done with statics, switching to malloc\n");
			printed_full = 1;
		}
		tlen = strlen(name);
		tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
		if (tack == NULL) return NULL;
		memset(tack, 0, sizeof(struct resource));
		res = (struct resource *) tack;
		tack += sizeof (struct resource);
	}

	strlcpy(tack, name, XNMLN+1);
	res->name = tack;

	va = _sparc_ioremap(res, busno, phys, size);
	/* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
	return va;
}

/*
 */
static void __iomem *
_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
{
	unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);

	if (allocate_resource(&sparc_iomap, res,
	    (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
	    sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
		/* Usually we cannot see printks in this case. */
		prom_printf("alloc_io_res(%s): cannot occupy\n",
		    (res->name != NULL)? res->name: "???");
		prom_halt();
	}

	pa &= PAGE_MASK;
230
	srmmu_mapiorange(bus, pa, res->start, resource_size(res));
L
Linus Torvalds 已提交
231

232
	return (void __iomem *)(unsigned long)(res->start + offset);
L
Linus Torvalds 已提交
233 234 235
}

/*
236
 * Complementary to _sparc_ioremap().
L
Linus Torvalds 已提交
237 238 239 240 241
 */
static void _sparc_free_io(struct resource *res)
{
	unsigned long plen;

242
	plen = resource_size(res);
243
	BUG_ON((plen & (PAGE_SIZE-1)) != 0);
244
	srmmu_unmapiorange(res->start, plen);
L
Linus Torvalds 已提交
245 246 247 248 249
	release_resource(res);
}

#ifdef CONFIG_SBUS

250
void sbus_set_sbus64(struct device *dev, int x)
251
{
L
Linus Torvalds 已提交
252 253
	printk("sbus_set_sbus64: unsupported\n");
}
254
EXPORT_SYMBOL(sbus_set_sbus64);
L
Linus Torvalds 已提交
255 256 257 258 259 260

/*
 * Allocate a chunk of memory suitable for DMA.
 * Typically devices use them for control blocks.
 * CPU may access them without any explicit flushing.
 */
261
static void *sbus_alloc_coherent(struct device *dev, size_t len,
262
				 dma_addr_t *dma_addrp, gfp_t gfp,
263
				 unsigned long attrs)
L
Linus Torvalds 已提交
264
{
265
	struct platform_device *op = to_platform_device(dev);
266
	unsigned long len_total = PAGE_ALIGN(len);
L
Linus Torvalds 已提交
267 268 269 270
	unsigned long va;
	struct resource *res;
	int order;

271
	/* XXX why are some lengths signed, others unsigned? */
L
Linus Torvalds 已提交
272 273 274 275 276 277 278 279 280
	if (len <= 0) {
		return NULL;
	}
	/* XXX So what is maxphys for us and how do drivers know it? */
	if (len > 256*1024) {			/* __get_free_pages() limit */
		return NULL;
	}

	order = get_order(len_total);
281 282
	va = __get_free_pages(gfp, order);
	if (va == 0)
L
Linus Torvalds 已提交
283 284
		goto err_nopages;

285
	if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
L
Linus Torvalds 已提交
286 287 288 289 290 291 292
		goto err_nomem;

	if (allocate_resource(&_sparc_dvma, res, len_total,
	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
		printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
		goto err_nova;
	}
293

294
	// XXX The sbus_map_dma_area does this for us below, see comments.
295
	// srmmu_mapiorange(0, virt_to_phys(va), res->start, len_total);
L
Linus Torvalds 已提交
296 297 298 299
	/*
	 * XXX That's where sdev would be used. Currently we load
	 * all iommu tables with the same translations.
	 */
300
	if (sbus_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
L
Linus Torvalds 已提交
301 302
		goto err_noiommu;

303
	res->name = op->dev.of_node->name;
304

305
	return (void *)(unsigned long)res->start;
L
Linus Torvalds 已提交
306 307 308 309 310

err_noiommu:
	release_resource(res);
err_nova:
	kfree(res);
311 312
err_nomem:
	free_pages(va, order);
L
Linus Torvalds 已提交
313 314 315 316
err_nopages:
	return NULL;
}

317
static void sbus_free_coherent(struct device *dev, size_t n, void *p,
318
			       dma_addr_t ba, unsigned long attrs)
L
Linus Torvalds 已提交
319 320 321 322
{
	struct resource *res;
	struct page *pgv;

323
	if ((res = lookup_resource(&_sparc_dvma,
L
Linus Torvalds 已提交
324 325 326 327 328 329 330 331 332 333
	    (unsigned long)p)) == NULL) {
		printk("sbus_free_consistent: cannot free %p\n", p);
		return;
	}

	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
		printk("sbus_free_consistent: unaligned va %p\n", p);
		return;
	}

334
	n = PAGE_ALIGN(n);
335
	if (resource_size(res) != n) {
336
		printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n",
337
		    (long)resource_size(res), n);
L
Linus Torvalds 已提交
338 339 340 341 342 343
		return;
	}

	release_resource(res);
	kfree(res);

344
	pgv = virt_to_page(p);
345
	sbus_unmap_dma_area(dev, ba, n);
L
Linus Torvalds 已提交
346 347 348 349 350 351 352 353 354

	__free_pages(pgv, get_order(n));
}

/*
 * Map a chunk of memory so that devices can see it.
 * CPU view of this memory may be inconsistent with
 * a device view and explicit flushing is necessary.
 */
355 356 357
static dma_addr_t sbus_map_page(struct device *dev, struct page *page,
				unsigned long offset, size_t len,
				enum dma_data_direction dir,
358
				unsigned long attrs)
L
Linus Torvalds 已提交
359
{
360 361
	void *va = page_address(page) + offset;

362
	/* XXX why are some lengths signed, others unsigned? */
L
Linus Torvalds 已提交
363 364 365 366 367 368 369
	if (len <= 0) {
		return 0;
	}
	/* XXX So what is maxphys for us and how do drivers know it? */
	if (len > 256*1024) {			/* __get_free_pages() limit */
		return 0;
	}
370
	return mmu_get_scsi_one(dev, va, len);
L
Linus Torvalds 已提交
371 372
}

373
static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n,
374
			    enum dma_data_direction dir, unsigned long attrs)
L
Linus Torvalds 已提交
375
{
376
	mmu_release_scsi_one(dev, ba, n);
L
Linus Torvalds 已提交
377 378
}

379
static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n,
380
		       enum dma_data_direction dir, unsigned long attrs)
L
Linus Torvalds 已提交
381
{
382
	mmu_get_scsi_sgl(dev, sg, n);
L
Linus Torvalds 已提交
383 384 385
	return n;
}

386
static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n,
387
			  enum dma_data_direction dir, unsigned long attrs)
L
Linus Torvalds 已提交
388
{
389
	mmu_release_scsi_sgl(dev, sg, n);
L
Linus Torvalds 已提交
390 391
}

392 393
static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
				 int n,	enum dma_data_direction dir)
L
Linus Torvalds 已提交
394
{
395
	BUG();
L
Linus Torvalds 已提交
396 397
}

398 399
static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
				    int n, enum dma_data_direction dir)
L
Linus Torvalds 已提交
400
{
401
	BUG();
L
Linus Torvalds 已提交
402 403
}

404
static const struct dma_map_ops sbus_dma_ops = {
405 406
	.alloc			= sbus_alloc_coherent,
	.free			= sbus_free_coherent,
407 408 409 410 411 412 413 414
	.map_page		= sbus_map_page,
	.unmap_page		= sbus_unmap_page,
	.map_sg			= sbus_map_sg,
	.unmap_sg		= sbus_unmap_sg,
	.sync_sg_for_cpu	= sbus_sync_sg_for_cpu,
	.sync_sg_for_device	= sbus_sync_sg_for_device,
};

415
static int __init sparc_register_ioport(void)
416 417 418 419 420 421
{
	register_proc_sparc_ioport();

	return 0;
}

422 423
arch_initcall(sparc_register_ioport);

L
Linus Torvalds 已提交
424 425
#endif /* CONFIG_SBUS */

426

L
Linus Torvalds 已提交
427 428 429
/* Allocate and map kernel buffer using consistent mode DMA for a device.
 * hwdev should be valid struct pci_dev pointer for PCI devices.
 */
430
static void *pci32_alloc_coherent(struct device *dev, size_t len,
431
				  dma_addr_t *pba, gfp_t gfp,
432
				  unsigned long attrs)
L
Linus Torvalds 已提交
433
{
434
	unsigned long len_total = PAGE_ALIGN(len);
435
	void *va;
L
Linus Torvalds 已提交
436 437 438 439 440 441 442 443 444 445 446
	struct resource *res;
	int order;

	if (len == 0) {
		return NULL;
	}
	if (len > 256*1024) {			/* __get_free_pages() limit */
		return NULL;
	}

	order = get_order(len_total);
447
	va = (void *) __get_free_pages(gfp, order);
448
	if (va == NULL) {
L
Linus Torvalds 已提交
449
		printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
450
		goto err_nopages;
L
Linus Torvalds 已提交
451 452
	}

453
	if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
L
Linus Torvalds 已提交
454
		printk("pci_alloc_consistent: no core\n");
455
		goto err_nomem;
L
Linus Torvalds 已提交
456 457 458 459 460
	}

	if (allocate_resource(&_sparc_dvma, res, len_total,
	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
		printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
461
		goto err_nova;
L
Linus Torvalds 已提交
462
	}
463
	srmmu_mapiorange(0, virt_to_phys(va), res->start, len_total);
L
Linus Torvalds 已提交
464 465 466

	*pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
	return (void *) res->start;
467 468 469 470 471 472 473

err_nova:
	kfree(res);
err_nomem:
	free_pages((unsigned long)va, order);
err_nopages:
	return NULL;
L
Linus Torvalds 已提交
474 475 476 477 478 479 480
}

/* Free and unmap a consistent DMA buffer.
 * cpu_addr is what was returned from pci_alloc_consistent,
 * size must be the same as what as passed into pci_alloc_consistent,
 * and likewise dma_addr must be the same as what *dma_addrp was set to.
 *
S
Simon Arlott 已提交
481
 * References to the memory and mappings associated with cpu_addr/dma_addr
L
Linus Torvalds 已提交
482 483
 * past this call are illegal.
 */
484
static void pci32_free_coherent(struct device *dev, size_t n, void *p,
485
				dma_addr_t ba, unsigned long attrs)
L
Linus Torvalds 已提交
486 487 488
{
	struct resource *res;

489
	if ((res = lookup_resource(&_sparc_dvma,
L
Linus Torvalds 已提交
490 491 492 493 494 495 496 497 498 499
	    (unsigned long)p)) == NULL) {
		printk("pci_free_consistent: cannot free %p\n", p);
		return;
	}

	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
		printk("pci_free_consistent: unaligned va %p\n", p);
		return;
	}

500
	n = PAGE_ALIGN(n);
501
	if (resource_size(res) != n) {
L
Linus Torvalds 已提交
502
		printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
503
		    (long)resource_size(res), (long)n);
L
Linus Torvalds 已提交
504 505 506
		return;
	}

507
	dma_make_coherent(ba, n);
508
	srmmu_unmapiorange((unsigned long)p, n);
L
Linus Torvalds 已提交
509 510 511

	release_resource(res);
	kfree(res);
512
	free_pages((unsigned long)phys_to_virt(ba), get_order(n));
L
Linus Torvalds 已提交
513 514 515 516 517
}

/*
 * Same as pci_map_single, but with pages.
 */
518 519 520
static dma_addr_t pci32_map_page(struct device *dev, struct page *page,
				 unsigned long offset, size_t size,
				 enum dma_data_direction dir,
521
				 unsigned long attrs)
L
Linus Torvalds 已提交
522 523 524 525 526
{
	/* IIep is write-through, not flushing. */
	return page_to_phys(page) + offset;
}

K
Kristoffer Glembo 已提交
527
static void pci32_unmap_page(struct device *dev, dma_addr_t ba, size_t size,
528
			     enum dma_data_direction dir, unsigned long attrs)
K
Kristoffer Glembo 已提交
529
{
530
	if (dir != PCI_DMA_TODEVICE && !(attrs & DMA_ATTR_SKIP_CPU_SYNC))
531
		dma_make_coherent(ba, PAGE_ALIGN(size));
K
Kristoffer Glembo 已提交
532 533
}

L
Linus Torvalds 已提交
534
/* Map a set of buffers described by scatterlist in streaming
535
 * mode for DMA.  This is the scatter-gather version of the
L
Linus Torvalds 已提交
536 537 538 539 540 541 542 543 544 545 546 547 548
 * above pci_map_single 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.
 *
 * Device ownership issues as mentioned above for pci_map_single are
 * the same here.
 */
549 550
static int pci32_map_sg(struct device *device, struct scatterlist *sgl,
			int nents, enum dma_data_direction dir,
551
			unsigned long attrs)
L
Linus Torvalds 已提交
552
{
J
Jens Axboe 已提交
553
	struct scatterlist *sg;
L
Linus Torvalds 已提交
554 555 556
	int n;

	/* IIep is write-through, not flushing. */
J
Jens Axboe 已提交
557
	for_each_sg(sgl, sg, nents, n) {
558
		sg->dma_address = sg_phys(sg);
559
		sg->dma_length = sg->length;
L
Linus Torvalds 已提交
560 561 562 563 564 565 566 567
	}
	return nents;
}

/* Unmap a set of streaming mode DMA translations.
 * Again, cpu read rules concerning calls here are the same as for
 * pci_unmap_single() above.
 */
568 569
static void pci32_unmap_sg(struct device *dev, struct scatterlist *sgl,
			   int nents, enum dma_data_direction dir,
570
			   unsigned long attrs)
L
Linus Torvalds 已提交
571
{
J
Jens Axboe 已提交
572
	struct scatterlist *sg;
L
Linus Torvalds 已提交
573 574
	int n;

575
	if (dir != PCI_DMA_TODEVICE && !(attrs & DMA_ATTR_SKIP_CPU_SYNC)) {
J
Jens Axboe 已提交
576
		for_each_sg(sgl, sg, nents, n) {
577
			dma_make_coherent(sg_phys(sg), PAGE_ALIGN(sg->length));
L
Linus Torvalds 已提交
578 579 580 581 582 583 584 585 586 587 588 589 590 591
		}
	}
}

/* Make physical memory consistent for a single
 * streaming mode DMA translation before or after a transfer.
 *
 * If you perform a pci_map_single() but wish to interrogate the
 * buffer using the cpu, yet do not wish to teardown the PCI dma
 * mapping, you must call this function before doing so.  At the
 * next point you give the PCI dma address back to the card, you
 * must first perform a pci_dma_sync_for_device, and then the
 * device again owns the buffer.
 */
592 593
static void pci32_sync_single_for_cpu(struct device *dev, dma_addr_t ba,
				      size_t size, enum dma_data_direction dir)
L
Linus Torvalds 已提交
594
{
595
	if (dir != PCI_DMA_TODEVICE) {
596
		dma_make_coherent(ba, PAGE_ALIGN(size));
L
Linus Torvalds 已提交
597 598 599
	}
}

600 601
static void pci32_sync_single_for_device(struct device *dev, dma_addr_t ba,
					 size_t size, enum dma_data_direction dir)
L
Linus Torvalds 已提交
602
{
603
	if (dir != PCI_DMA_TODEVICE) {
604
		dma_make_coherent(ba, PAGE_ALIGN(size));
L
Linus Torvalds 已提交
605 606 607 608 609 610 611 612 613
	}
}

/* Make physical memory consistent for a set of streaming
 * mode DMA translations after a transfer.
 *
 * The same as pci_dma_sync_single_* but for a scatter-gather list,
 * same rules and usage.
 */
614 615
static void pci32_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
				  int nents, enum dma_data_direction dir)
L
Linus Torvalds 已提交
616
{
J
Jens Axboe 已提交
617
	struct scatterlist *sg;
L
Linus Torvalds 已提交
618 619
	int n;

620
	if (dir != PCI_DMA_TODEVICE) {
J
Jens Axboe 已提交
621
		for_each_sg(sgl, sg, nents, n) {
622
			dma_make_coherent(sg_phys(sg), PAGE_ALIGN(sg->length));
L
Linus Torvalds 已提交
623 624 625 626
		}
	}
}

627 628
static void pci32_sync_sg_for_device(struct device *device, struct scatterlist *sgl,
				     int nents, enum dma_data_direction dir)
L
Linus Torvalds 已提交
629
{
J
Jens Axboe 已提交
630
	struct scatterlist *sg;
L
Linus Torvalds 已提交
631 632
	int n;

633
	if (dir != PCI_DMA_TODEVICE) {
J
Jens Axboe 已提交
634
		for_each_sg(sgl, sg, nents, n) {
635
			dma_make_coherent(sg_phys(sg), PAGE_ALIGN(sg->length));
L
Linus Torvalds 已提交
636 637 638
		}
	}
}
639

640
const struct dma_map_ops pci32_dma_ops = {
641 642
	.alloc			= pci32_alloc_coherent,
	.free			= pci32_free_coherent,
643
	.map_page		= pci32_map_page,
K
Kristoffer Glembo 已提交
644
	.unmap_page		= pci32_unmap_page,
645 646 647 648 649 650 651 652 653
	.map_sg			= pci32_map_sg,
	.unmap_sg		= pci32_unmap_sg,
	.sync_single_for_cpu	= pci32_sync_single_for_cpu,
	.sync_single_for_device	= pci32_sync_single_for_device,
	.sync_sg_for_cpu	= pci32_sync_sg_for_cpu,
	.sync_sg_for_device	= pci32_sync_sg_for_device,
};
EXPORT_SYMBOL(pci32_dma_ops);

654
/* leon re-uses pci32_dma_ops */
655
const struct dma_map_ops *leon_dma_ops = &pci32_dma_ops;
656
EXPORT_SYMBOL(leon_dma_ops);
657

658
const struct dma_map_ops *dma_ops = &sbus_dma_ops;
659 660
EXPORT_SYMBOL(dma_ops);

L
Linus Torvalds 已提交
661

662 663 664 665 666 667 668 669
/*
 * Return whether the given PCI device DMA address mask can be
 * supported properly.  For example, if your device can only drive the
 * low 24-bits during PCI bus mastering, then you would pass
 * 0x00ffffff as the mask to this function.
 */
int dma_supported(struct device *dev, u64 mask)
{
670
	if (dev_is_pci(dev))
671
		return 1;
672

673 674 675 676
	return 0;
}
EXPORT_SYMBOL(dma_supported);

L
Linus Torvalds 已提交
677 678
#ifdef CONFIG_PROC_FS

679
static int sparc_io_proc_show(struct seq_file *m, void *v)
L
Linus Torvalds 已提交
680
{
681
	struct resource *root = m->private, *r;
L
Linus Torvalds 已提交
682 683
	const char *nm;

684
	for (r = root->child; r != NULL; r = r->sibling) {
685
		if ((nm = r->name) == NULL) nm = "???";
686
		seq_printf(m, "%016llx-%016llx: %s\n",
687 688
				(unsigned long long)r->start,
				(unsigned long long)r->end, nm);
L
Linus Torvalds 已提交
689 690
	}

691
	return 0;
L
Linus Torvalds 已提交
692 693
}

694 695
static int sparc_io_proc_open(struct inode *inode, struct file *file)
{
A
Al Viro 已提交
696
	return single_open(file, sparc_io_proc_show, PDE_DATA(inode));
697 698 699 700 701 702 703 704 705
}

static const struct file_operations sparc_io_proc_fops = {
	.owner		= THIS_MODULE,
	.open		= sparc_io_proc_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= single_release,
};
L
Linus Torvalds 已提交
706 707
#endif /* CONFIG_PROC_FS */

A
Adrian Bunk 已提交
708
static void register_proc_sparc_ioport(void)
L
Linus Torvalds 已提交
709 710
{
#ifdef CONFIG_PROC_FS
711 712
	proc_create_data("io_map", 0, NULL, &sparc_io_proc_fops, &sparc_iomap);
	proc_create_data("dvma_map", 0, NULL, &sparc_io_proc_fops, &_sparc_dvma);
L
Linus Torvalds 已提交
713 714
#endif
}