1 /* $Id: ioport.c,v 1.45 2001/10/30 04:54:21 davem Exp $
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 
28 #include <linux/config.h>
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>
38 
39 #include <asm/io.h>
40 #include <asm/vaddrs.h>
41 #include <asm/oplib.h>
42 #include <asm/page.h>
43 #include <asm/pgalloc.h>
44 #include <asm/pgtable.h>
45 
46 #define mmu_inval_dma_area(p, l)	/* Anton pulled it out for 2.4.0-xx */
47 
48 struct resource *_sparc_find_resource(struct resource *r, unsigned long);
49 
50 static void *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
51 static void *_sparc_alloc_io(unsigned int busno, unsigned long phys,
52     unsigned long size, char *name);
53 static void _sparc_free_io(struct resource *res);
54 
55 /* This points to the next to use virtual memory for DVMA mappings */
56 static struct resource _sparc_dvma = {
57 	"sparc_dvma", DVMA_VADDR, DVMA_END - 1
58 };
59 /* This points to the start of I/O mappings, cluable from outside. */
60 /*ext*/ struct resource sparc_iomap = {
61 	"sparc_iomap", IOBASE_VADDR, IOBASE_END - 1
62 };
63 
64 /*
65  * BTFIXUP would do as well but it seems overkill for the case.
66  */
67 static void (*_sparc_mapioaddr)(unsigned long pa, unsigned long va,
68     int bus, int ro);
69 static void (*_sparc_unmapioaddr)(unsigned long va);
70 
71 /*
72  * Our mini-allocator...
73  * Boy this is gross! We need it because we must map I/O for
74  * timers and interrupt controller before the kmalloc is available.
75  */
76 
77 #define XNMLN  15
78 #define XNRES  10	/* SS-10 uses 8 */
79 
80 struct xresource {
81 	struct resource xres;	/* Must be first */
82 	int xflag;		/* 1 == used */
83 	char xname[XNMLN+1];
84 };
85 
86 static struct xresource xresv[XNRES];
87 
xres_alloc(void)88 static struct xresource *xres_alloc(void) {
89 	struct xresource *xrp;
90 	int n;
91 
92 	xrp = xresv;
93 	for (n = 0; n < XNRES; n++) {
94 		if (xrp->xflag == 0) {
95 			xrp->xflag = 1;
96 			return xrp;
97 		}
98 		xrp++;
99 	}
100 	return NULL;
101 }
102 
xres_free(struct xresource * xrp)103 static void xres_free(struct xresource *xrp) {
104 	xrp->xflag = 0;
105 }
106 
107 /*
108  * These are typically used in PCI drivers
109  * which are trying to be cross-platform.
110  *
111  * Bus type is always zero on IIep.
112  */
ioremap(unsigned long offset,unsigned long size)113 void *ioremap(unsigned long offset, unsigned long size)
114 {
115 	char name[14];
116 
117 	sprintf(name, "phys_%08x", (u32)offset);
118 	return _sparc_alloc_io(0, offset, size, name);
119 }
120 
121 /*
122  * Comlimentary to ioremap().
123  */
iounmap(void * virtual)124 void iounmap(void *virtual)
125 {
126 	unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
127 	struct resource *res;
128 
129 	if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
130 		printk("free_io/iounmap: cannot free %lx\n", vaddr);
131 		return;
132 	}
133 	_sparc_free_io(res);
134 
135 	if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
136 		xres_free((struct xresource *)res);
137 	} else {
138 		kfree(res);
139 	}
140 }
141 
142 /*
143  */
sbus_ioremap(struct resource * phyres,unsigned long offset,unsigned long size,char * name)144 unsigned long sbus_ioremap(struct resource *phyres, unsigned long offset,
145     unsigned long size, char *name)
146 {
147 	return (unsigned long) _sparc_alloc_io(phyres->flags & 0xF,
148 	    phyres->start + offset, size, name);
149 }
150 
151 /*
152  */
sbus_iounmap(unsigned long addr,unsigned long size)153 void sbus_iounmap(unsigned long addr, unsigned long size)
154 {
155 	iounmap((void *)addr);
156 }
157 
158 /*
159  * Meat of mapping
160  */
_sparc_alloc_io(unsigned int busno,unsigned long phys,unsigned long size,char * name)161 static void *_sparc_alloc_io(unsigned int busno, unsigned long phys,
162     unsigned long size, char *name)
163 {
164 	static int printed_full;
165 	struct xresource *xres;
166 	struct resource *res;
167 	char *tack;
168 	int tlen;
169 	void *va;	/* P3 diag */
170 
171 	if (name == NULL) name = "???";
172 
173 	if ((xres = xres_alloc()) != 0) {
174 		tack = xres->xname;
175 		res = &xres->xres;
176 	} else {
177 		if (!printed_full) {
178 			printk("ioremap: done with statics, switching to malloc\n");
179 			printed_full = 1;
180 		}
181 		tlen = strlen(name);
182 		tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
183 		if (tack == NULL) return NULL;
184 		memset(tack, 0, sizeof(struct resource));
185 		res = (struct resource *) tack;
186 		tack += sizeof (struct resource);
187 	}
188 
189 	strncpy(tack, name, XNMLN);
190 	tack[XNMLN] = 0;
191 	res->name = tack;
192 
193 	va = _sparc_ioremap(res, busno, phys, size);
194 	/* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
195 	return va;
196 }
197 
198 /*
199  */
200 static void *
_sparc_ioremap(struct resource * res,u32 bus,u32 pa,int sz)201 _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
202 {
203 	unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
204 	unsigned long va;
205 	unsigned int psz;
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 	va = res->start;
217 	pa &= PAGE_MASK;
218 	for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
219 		(*_sparc_mapioaddr)(pa, va, bus, 0);
220 		va += PAGE_SIZE;
221 		pa += PAGE_SIZE;
222 	}
223 
224 	return (void *) (res->start + offset);
225 }
226 
227 /*
228  * Comlimentary to _sparc_ioremap().
229  */
_sparc_free_io(struct resource * res)230 static void _sparc_free_io(struct resource *res)
231 {
232 	unsigned long plen;
233 
234 	plen = res->end - res->start + 1;
235 	if ((plen & (PAGE_SIZE-1)) != 0) BUG();
236 	while (plen != 0) {
237 		plen -= PAGE_SIZE;
238 		(*_sparc_unmapioaddr)(res->start + plen);
239 	}
240 
241 	release_resource(res);
242 }
243 
244 #ifdef CONFIG_SBUS
245 
sbus_set_sbus64(struct sbus_dev * sdev,int x)246 void sbus_set_sbus64(struct sbus_dev *sdev, int x) {
247 	printk("sbus_set_sbus64: unsupported\n");
248 }
249 
250 /*
251  * Allocate a chunk of memory suitable for DMA.
252  * Typically devices use them for control blocks.
253  * CPU may access them without any explicit flushing.
254  *
255  * XXX Some clever people know that sdev is not used and supply NULL. Watch.
256  */
sbus_alloc_consistent(struct sbus_dev * sdev,long len,u32 * dma_addrp)257 void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
258 {
259 	unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
260 	unsigned long va;
261 	struct resource *res;
262 	int order;
263 
264 	/* XXX why are some lenghts signed, others unsigned? */
265 	if (len <= 0) {
266 		return NULL;
267 	}
268 	/* XXX So what is maxphys for us and how do drivers know it? */
269 	if (len > 256*1024) {			/* __get_free_pages() limit */
270 		return NULL;
271 	}
272 
273 	order = get_order(len_total);
274 	va = __get_free_pages(GFP_KERNEL, order);
275 	if (va == 0) {
276 		/*
277 		 * printk here may be flooding... Consider removal XXX.
278 		 */
279 		printk("sbus_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
280 		return NULL;
281 	}
282 
283 	if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
284 		free_pages(va, order);
285 		printk("sbus_alloc_consistent: no core\n");
286 		return NULL;
287 	}
288 	memset((char*)res, 0, sizeof(struct resource));
289 
290 	if (allocate_resource(&_sparc_dvma, res, len_total,
291 	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
292 		printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
293 		free_pages(va, order);
294 		kfree(res);
295 		return NULL;
296 	}
297 
298 	mmu_map_dma_area(va, res->start, len_total);
299 
300 	*dma_addrp = res->start;
301 	return (void *)res->start;
302 }
303 
sbus_free_consistent(struct sbus_dev * sdev,long n,void * p,u32 ba)304 void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
305 {
306 	struct resource *res;
307 	unsigned long pgp;
308 
309 	if ((res = _sparc_find_resource(&_sparc_dvma,
310 	    (unsigned long)p)) == NULL) {
311 		printk("sbus_free_consistent: cannot free %p\n", p);
312 		return;
313 	}
314 
315 	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
316 		printk("sbus_free_consistent: unaligned va %p\n", p);
317 		return;
318 	}
319 
320 	n = (n + PAGE_SIZE-1) & PAGE_MASK;
321 	if ((res->end-res->start)+1 != n) {
322 		printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
323 		    (long)((res->end-res->start)+1), n);
324 		return;
325 	}
326 
327 	release_resource(res);
328 	kfree(res);
329 
330 	/* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
331 	pgp = (unsigned long) phys_to_virt(mmu_translate_dvma(ba));
332 	mmu_unmap_dma_area(ba, n);
333 
334 	free_pages(pgp, get_order(n));
335 }
336 
337 /*
338  * Map a chunk of memory so that devices can see it.
339  * CPU view of this memory may be inconsistent with
340  * a device view and explicit flushing is necessary.
341  */
sbus_map_single(struct sbus_dev * sdev,void * va,size_t len,int direction)342 dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
343 {
344 #if 0 /* This is the version that abuses consistent space */
345 	unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
346 	struct resource *res;
347 
348 	/* XXX why are some lenghts signed, others unsigned? */
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 	}
356 
357 	if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
358 		printk("sbus_map_single: no core\n");
359 		return 0;
360 	}
361 	memset((char*)res, 0, sizeof(struct resource));
362 	res->name = va; /* XXX */
363 
364 	if (allocate_resource(&_sparc_dvma, res, len_total,
365 	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE) != 0) {
366 		printk("sbus_map_single: cannot occupy 0x%lx", len);
367 		kfree(res);
368 		return 0;
369 	}
370 
371 	mmu_map_dma_area(va, res->start, len_total);
372 	mmu_flush_dma_area((unsigned long)va, len_total); /* in all contexts? */
373 
374 	return res->start;
375 #endif
376 #if 1 /* "trampoline" version */
377 	/* XXX why are some lenghts signed, others unsigned? */
378 	if (len <= 0) {
379 		return 0;
380 	}
381 	/* XXX So what is maxphys for us and how do drivers know it? */
382 	if (len > 256*1024) {			/* __get_free_pages() limit */
383 		return 0;
384 	}
385 	return mmu_get_scsi_one(va, len, sdev->bus);
386 #endif
387 }
388 
sbus_unmap_single(struct sbus_dev * sdev,dma_addr_t ba,size_t n,int direction)389 void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
390 {
391 #if 0 /* This is the version that abuses consistent space */
392 	struct resource *res;
393 	unsigned long va;
394 
395 	if ((res = _sparc_find_resource(&_sparc_dvma, ba)) == NULL) {
396 		printk("sbus_unmap_single: cannot find %08x\n", (unsigned)ba);
397 		return;
398 	}
399 
400 	n = (n + PAGE_SIZE-1) & PAGE_MASK;
401 	if ((res->end-res->start)+1 != n) {
402 		printk("sbus_unmap_single: region 0x%lx asked 0x%lx\n",
403 		    (long)((res->end-res->start)+1), n);
404 		return;
405 	}
406 
407 	va = (unsigned long) res->name;	/* XXX Ouch */
408 	mmu_inval_dma_area(va, n);	/* in all contexts, mm's?... */
409 	mmu_unmap_dma_area(ba, n);	/* iounit cache flush is here */
410 	release_resource(res);
411 	kfree(res);
412 #endif
413 #if 1 /* "trampoline" version */
414 	mmu_release_scsi_one(ba, n, sdev->bus);
415 #endif
416 }
417 
sbus_map_sg(struct sbus_dev * sdev,struct scatterlist * sg,int n,int direction)418 int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
419 {
420 	mmu_get_scsi_sgl(sg, n, sdev->bus);
421 
422 	/*
423 	 * XXX sparc64 can return a partial length here. sun4c should do this
424 	 * but it currently panics if it can't fulfill the request - Anton
425 	 */
426 	return n;
427 }
428 
sbus_unmap_sg(struct sbus_dev * sdev,struct scatterlist * sg,int n,int direction)429 void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
430 {
431 	mmu_release_scsi_sgl(sg, n, sdev->bus);
432 }
433 
434 /*
435  */
sbus_dma_sync_single(struct sbus_dev * sdev,dma_addr_t ba,size_t size,int direction)436 void sbus_dma_sync_single(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
437 {
438 #if 0
439 	unsigned long va;
440 	struct resource *res;
441 
442 	/* We do not need the resource, just print a message if invalid. */
443 	res = _sparc_find_resource(&_sparc_dvma, ba);
444 	if (res == NULL)
445 		panic("sbus_dma_sync_single: 0x%x\n", ba);
446 
447 	va = (unsigned long) phys_to_virt(mmu_translate_dvma(ba));
448 	/*
449 	 * XXX This bogosity will be fixed with the iommu rewrite coming soon
450 	 * to a kernel near you. - Anton
451 	 */
452 	/* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
453 #endif
454 }
455 
sbus_dma_sync_sg(struct sbus_dev * sdev,struct scatterlist * sg,int n,int direction)456 void sbus_dma_sync_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
457 {
458 	printk("sbus_dma_sync_sg: not implemented yet\n");
459 }
460 #endif /* CONFIG_SBUS */
461 
462 #ifdef CONFIG_PCI
463 
464 /* Allocate and map kernel buffer using consistent mode DMA for a device.
465  * hwdev should be valid struct pci_dev pointer for PCI devices.
466  */
pci_alloc_consistent(struct pci_dev * pdev,size_t len,dma_addr_t * pba)467 void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
468 {
469 	unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
470 	unsigned long va;
471 	struct resource *res;
472 	int order;
473 
474 	if (len == 0) {
475 		return NULL;
476 	}
477 	if (len > 256*1024) {			/* __get_free_pages() limit */
478 		return NULL;
479 	}
480 
481 	order = get_order(len_total);
482 	va = __get_free_pages(GFP_KERNEL, order);
483 	if (va == 0) {
484 		printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
485 		return NULL;
486 	}
487 
488 	if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
489 		free_pages(va, order);
490 		printk("pci_alloc_consistent: no core\n");
491 		return NULL;
492 	}
493 	memset((char*)res, 0, sizeof(struct resource));
494 
495 	if (allocate_resource(&_sparc_dvma, res, len_total,
496 	    _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
497 		printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
498 		free_pages(va, order);
499 		kfree(res);
500 		return NULL;
501 	}
502 
503 	mmu_inval_dma_area(va, len_total);
504 
505 #if 1
506 /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
507   (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
508 #endif
509 	{
510 		unsigned long xva, xpa;
511 		xva = res->start;
512 		xpa = virt_to_phys(va);
513 		while (len_total != 0) {
514 			len_total -= PAGE_SIZE;
515 			(*_sparc_mapioaddr)(xpa, xva, 0, 0);
516 			xva += PAGE_SIZE;
517 			xpa += PAGE_SIZE;
518 		}
519 	}
520 
521 	*pba = virt_to_bus(va);
522 	return (void *) res->start;
523 }
524 
525 /* Free and unmap a consistent DMA buffer.
526  * cpu_addr is what was returned from pci_alloc_consistent,
527  * size must be the same as what as passed into pci_alloc_consistent,
528  * and likewise dma_addr must be the same as what *dma_addrp was set to.
529  *
530  * References to the memory and mappings assosciated with cpu_addr/dma_addr
531  * past this call are illegal.
532  */
pci_free_consistent(struct pci_dev * pdev,size_t n,void * p,dma_addr_t ba)533 void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
534 {
535 	struct resource *res;
536 	unsigned long pgp;
537 
538 	if ((res = _sparc_find_resource(&_sparc_dvma,
539 	    (unsigned long)p)) == NULL) {
540 		printk("pci_free_consistent: cannot free %p\n", p);
541 		return;
542 	}
543 
544 	if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
545 		printk("pci_free_consistent: unaligned va %p\n", p);
546 		return;
547 	}
548 
549 	n = (n + PAGE_SIZE-1) & PAGE_MASK;
550 	if ((res->end-res->start)+1 != n) {
551 		printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
552 		    (long)((res->end-res->start)+1), (long)n);
553 		return;
554 	}
555 
556 	pgp = (unsigned long) bus_to_virt(ba);
557 	mmu_inval_dma_area(pgp, n);
558 	{
559 		int x;
560 		for (x = 0; x < n; x += PAGE_SIZE) {
561 			(*_sparc_unmapioaddr)((unsigned long)p + n);
562 		}
563 	}
564 
565 	release_resource(res);
566 	kfree(res);
567 
568 	free_pages(pgp, get_order(n));
569 }
570 
571 /* Map a single buffer of the indicated size for DMA in streaming mode.
572  * The 32-bit bus address to use is returned.
573  *
574  * Once the device is given the dma address, the device owns this memory
575  * until either pci_unmap_single or pci_dma_sync_single is performed.
576  */
pci_map_single(struct pci_dev * hwdev,void * ptr,size_t size,int direction)577 dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
578     int direction)
579 {
580 	if (direction == PCI_DMA_NONE)
581 		BUG();
582 	/* IIep is write-through, not flushing. */
583 	return virt_to_bus(ptr);
584 }
585 
586 /* Unmap a single streaming mode DMA translation.  The dma_addr and size
587  * must match what was provided for in a previous pci_map_single call.  All
588  * other usages are undefined.
589  *
590  * After this call, reads by the cpu to the buffer are guarenteed to see
591  * whatever the device wrote there.
592  */
pci_unmap_single(struct pci_dev * hwdev,dma_addr_t ba,size_t size,int direction)593 void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
594     int direction)
595 {
596 	if (direction == PCI_DMA_NONE)
597 		BUG();
598 	if (direction != PCI_DMA_TODEVICE) {
599 		mmu_inval_dma_area((unsigned long)bus_to_virt(ba),
600 		    (size + PAGE_SIZE-1) & PAGE_MASK);
601 	}
602 }
603 
604 /* Map a set of buffers described by scatterlist in streaming
605  * mode for DMA.  This is the scather-gather version of the
606  * above pci_map_single interface.  Here the scatter gather list
607  * elements are each tagged with the appropriate dma address
608  * and length.  They are obtained via sg_dma_{address,length}(SG).
609  *
610  * NOTE: An implementation may be able to use a smaller number of
611  *       DMA address/length pairs than there are SG table elements.
612  *       (for example via virtual mapping capabilities)
613  *       The routine returns the number of addr/length pairs actually
614  *       used, at most nents.
615  *
616  * Device ownership issues as mentioned above for pci_map_single are
617  * the same here.
618  */
pci_map_sg(struct pci_dev * hwdev,struct scatterlist * sg,int nents,int direction)619 int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
620     int direction)
621 {
622 	int n;
623 
624 	if (direction == PCI_DMA_NONE)
625 		BUG();
626 	/* IIep is write-through, not flushing. */
627 	for (n = 0; n < nents; n++) {
628 		sg->dvma_address = virt_to_bus(sg->address);
629 		sg->dvma_length = sg->length;
630 		sg++;
631 	}
632 	return nents;
633 }
634 
635 /* Unmap a set of streaming mode DMA translations.
636  * Again, cpu read rules concerning calls here are the same as for
637  * pci_unmap_single() above.
638  */
pci_unmap_sg(struct pci_dev * hwdev,struct scatterlist * sg,int nents,int direction)639 void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents,
640     int direction)
641 {
642 	int n;
643 
644 	if (direction == PCI_DMA_NONE)
645 		BUG();
646 	if (direction != PCI_DMA_TODEVICE) {
647 		for (n = 0; n < nents; n++) {
648 			mmu_inval_dma_area((unsigned long)sg->address,
649 			    (sg->length + PAGE_SIZE-1) & PAGE_MASK);
650 			sg++;
651 		}
652 	}
653 }
654 
655 /* Make physical memory consistent for a single
656  * streaming mode DMA translation before or after a transfer.
657  *
658  * If you perform a pci_map_single() but wish to interrogate the
659  * buffer using the cpu, yet do not wish to teardown the PCI dma
660  * mapping, you must call this function before doing so.  At the
661  * next point you give the PCI dma address back to the card, the
662  * device again owns the buffer.
663  */
pci_dma_sync_single(struct pci_dev * hwdev,dma_addr_t ba,size_t size,int direction)664 void pci_dma_sync_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
665 {
666 	if (direction == PCI_DMA_NONE)
667 		BUG();
668 	if (direction != PCI_DMA_TODEVICE) {
669 		mmu_inval_dma_area((unsigned long)bus_to_virt(ba),
670 		    (size + PAGE_SIZE-1) & PAGE_MASK);
671 	}
672 }
673 
674 /* Make physical memory consistent for a set of streaming
675  * mode DMA translations after a transfer.
676  *
677  * The same as pci_dma_sync_single but for a scatter-gather list,
678  * same rules and usage.
679  */
pci_dma_sync_sg(struct pci_dev * hwdev,struct scatterlist * sg,int nents,int direction)680 void pci_dma_sync_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
681 {
682 	int n;
683 
684 	if (direction == PCI_DMA_NONE)
685 		BUG();
686 	if (direction != PCI_DMA_TODEVICE) {
687 		for (n = 0; n < nents; n++) {
688 			mmu_inval_dma_area((unsigned long)sg->address,
689 			    (sg->length + PAGE_SIZE-1) & PAGE_MASK);
690 			sg++;
691 		}
692 	}
693 }
694 #endif /* CONFIG_PCI */
695 
696 #ifdef CONFIG_PROC_FS
697 
698 static int
_sparc_io_get_info(char * buf,char ** start,off_t fpos,int length,int * eof,void * data)699 _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
700     void *data)
701 {
702 	char *p = buf, *e = buf + length;
703 	struct resource *r;
704 	const char *nm;
705 
706 	for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
707 		if (p + 32 >= e)	/* Better than nothing */
708 			break;
709 		if ((nm = r->name) == 0) nm = "???";
710 		p += sprintf(p, "%08lx-%08lx: %s\n", r->start, r->end, nm);
711 	}
712 
713 	return p-buf;
714 }
715 
716 #endif /* CONFIG_PROC_FS */
717 
718 /*
719  * This is a version of find_resource and it belongs to kernel/resource.c.
720  * Until we have agreement with Linus and Martin, it lingers here.
721  *
722  * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
723  * This probably warrants some sort of hashing.
724  */
725 struct resource *
_sparc_find_resource(struct resource * root,unsigned long hit)726 _sparc_find_resource(struct resource *root, unsigned long hit)
727 {
728         struct resource *tmp;
729 
730 	for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
731 		if (tmp->start <= hit && tmp->end >= hit)
732 			return tmp;
733 	}
734 	return NULL;
735 }
736 
737 /*
738  * Necessary boot time initializations.
739  */
740 
ioport_init(void)741 void ioport_init(void)
742 {
743 	extern void sun4c_mapioaddr(unsigned long, unsigned long, int, int);
744 	extern void srmmu_mapioaddr(unsigned long, unsigned long, int, int);
745 	extern void sun4c_unmapioaddr(unsigned long);
746 	extern void srmmu_unmapioaddr(unsigned long);
747 
748 	switch(sparc_cpu_model) {
749 	case sun4c:
750 	case sun4:
751 	case sun4e:
752 		_sparc_mapioaddr = sun4c_mapioaddr;
753 		_sparc_unmapioaddr = sun4c_unmapioaddr;
754 		break;
755 	case sun4m:
756 	case sun4d:
757 		_sparc_mapioaddr = srmmu_mapioaddr;
758 		_sparc_unmapioaddr = srmmu_unmapioaddr;
759 		break;
760 	default:
761 		printk("ioport_init: cpu type %d is unknown.\n",
762 		    sparc_cpu_model);
763 		prom_halt();
764 	};
765 
766 }
767 
register_proc_sparc_ioport(void)768 void register_proc_sparc_ioport(void)
769 {
770 #ifdef CONFIG_PROC_FS
771 	create_proc_read_entry("io_map",0,0,_sparc_io_get_info,&sparc_iomap);
772 	create_proc_read_entry("dvma_map",0,0,_sparc_io_get_info,&_sparc_dvma);
773 #endif
774 }
775