ioport.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  1. /*
  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. #include <linux/module.h>
  28. #include <linux/sched.h>
  29. #include <linux/kernel.h>
  30. #include <linux/errno.h>
  31. #include <linux/types.h>
  32. #include <linux/ioport.h>
  33. #include <linux/mm.h>
  34. #include <linux/slab.h>
  35. #include <linux/pci.h> /* struct pci_dev */
  36. #include <linux/proc_fs.h>
  37. #include <linux/scatterlist.h>
  38. #include <linux/of_device.h>
  39. #include <asm/io.h>
  40. #include <asm/vaddrs.h>
  41. #include <asm/oplib.h>
  42. #include <asm/prom.h>
  43. #include <asm/sbus.h>
  44. #include <asm/page.h>
  45. #include <asm/pgalloc.h>
  46. #include <asm/dma.h>
  47. #include <asm/iommu.h>
  48. #include <asm/io-unit.h>
  49. #include "dma.h"
  50. #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
  51. static struct resource *_sparc_find_resource(struct resource *r,
  52. unsigned long);
  53. static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
  54. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  55. unsigned long size, char *name);
  56. static void _sparc_free_io(struct resource *res);
  57. static void register_proc_sparc_ioport(void);
  58. /* This points to the next to use virtual memory for DVMA mappings */
  59. static struct resource _sparc_dvma = {
  60. .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
  61. };
  62. /* This points to the start of I/O mappings, cluable from outside. */
  63. /*ext*/ struct resource sparc_iomap = {
  64. .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
  65. };
  66. /*
  67. * Our mini-allocator...
  68. * Boy this is gross! We need it because we must map I/O for
  69. * timers and interrupt controller before the kmalloc is available.
  70. */
  71. #define XNMLN 15
  72. #define XNRES 10 /* SS-10 uses 8 */
  73. struct xresource {
  74. struct resource xres; /* Must be first */
  75. int xflag; /* 1 == used */
  76. char xname[XNMLN+1];
  77. };
  78. static struct xresource xresv[XNRES];
  79. static struct xresource *xres_alloc(void) {
  80. struct xresource *xrp;
  81. int n;
  82. xrp = xresv;
  83. for (n = 0; n < XNRES; n++) {
  84. if (xrp->xflag == 0) {
  85. xrp->xflag = 1;
  86. return xrp;
  87. }
  88. xrp++;
  89. }
  90. return NULL;
  91. }
  92. static void xres_free(struct xresource *xrp) {
  93. xrp->xflag = 0;
  94. }
  95. /*
  96. * These are typically used in PCI drivers
  97. * which are trying to be cross-platform.
  98. *
  99. * Bus type is always zero on IIep.
  100. */
  101. void __iomem *ioremap(unsigned long offset, unsigned long size)
  102. {
  103. char name[14];
  104. sprintf(name, "phys_%08x", (u32)offset);
  105. return _sparc_alloc_io(0, offset, size, name);
  106. }
  107. /*
  108. * Comlimentary to ioremap().
  109. */
  110. void iounmap(volatile void __iomem *virtual)
  111. {
  112. unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
  113. struct resource *res;
  114. if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
  115. printk("free_io/iounmap: cannot free %lx\n", vaddr);
  116. return;
  117. }
  118. _sparc_free_io(res);
  119. if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
  120. xres_free((struct xresource *)res);
  121. } else {
  122. kfree(res);
  123. }
  124. }
  125. void __iomem *of_ioremap(struct resource *res, unsigned long offset,
  126. unsigned long size, char *name)
  127. {
  128. return _sparc_alloc_io(res->flags & 0xF,
  129. res->start + offset,
  130. size, name);
  131. }
  132. EXPORT_SYMBOL(of_ioremap);
  133. void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
  134. {
  135. iounmap(base);
  136. }
  137. EXPORT_SYMBOL(of_iounmap);
  138. /*
  139. * Meat of mapping
  140. */
  141. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  142. unsigned long size, char *name)
  143. {
  144. static int printed_full;
  145. struct xresource *xres;
  146. struct resource *res;
  147. char *tack;
  148. int tlen;
  149. void __iomem *va; /* P3 diag */
  150. if (name == NULL) name = "???";
  151. if ((xres = xres_alloc()) != 0) {
  152. tack = xres->xname;
  153. res = &xres->xres;
  154. } else {
  155. if (!printed_full) {
  156. printk("ioremap: done with statics, switching to malloc\n");
  157. printed_full = 1;
  158. }
  159. tlen = strlen(name);
  160. tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
  161. if (tack == NULL) return NULL;
  162. memset(tack, 0, sizeof(struct resource));
  163. res = (struct resource *) tack;
  164. tack += sizeof (struct resource);
  165. }
  166. strlcpy(tack, name, XNMLN+1);
  167. res->name = tack;
  168. va = _sparc_ioremap(res, busno, phys, size);
  169. /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
  170. return va;
  171. }
  172. /*
  173. */
  174. static void __iomem *
  175. _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
  176. {
  177. unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
  178. if (allocate_resource(&sparc_iomap, res,
  179. (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
  180. sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
  181. /* Usually we cannot see printks in this case. */
  182. prom_printf("alloc_io_res(%s): cannot occupy\n",
  183. (res->name != NULL)? res->name: "???");
  184. prom_halt();
  185. }
  186. pa &= PAGE_MASK;
  187. sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
  188. return (void __iomem *)(unsigned long)(res->start + offset);
  189. }
  190. /*
  191. * Comlimentary to _sparc_ioremap().
  192. */
  193. static void _sparc_free_io(struct resource *res)
  194. {
  195. unsigned long plen;
  196. plen = res->end - res->start + 1;
  197. BUG_ON((plen & (PAGE_SIZE-1)) != 0);
  198. sparc_unmapiorange(res->start, plen);
  199. release_resource(res);
  200. }
  201. #ifdef CONFIG_SBUS
  202. void sbus_set_sbus64(struct device *dev, int x)
  203. {
  204. printk("sbus_set_sbus64: unsupported\n");
  205. }
  206. /*
  207. * Allocate a chunk of memory suitable for DMA.
  208. * Typically devices use them for control blocks.
  209. * CPU may access them without any explicit flushing.
  210. */
  211. void *sbus_alloc_consistent(struct device *dev, long len, u32 *dma_addrp)
  212. {
  213. struct of_device *op = to_of_device(dev);
  214. unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
  215. unsigned long va;
  216. struct resource *res;
  217. int order;
  218. /* XXX why are some lengths signed, others unsigned? */
  219. if (len <= 0) {
  220. return NULL;
  221. }
  222. /* XXX So what is maxphys for us and how do drivers know it? */
  223. if (len > 256*1024) { /* __get_free_pages() limit */
  224. return NULL;
  225. }
  226. order = get_order(len_total);
  227. if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
  228. goto err_nopages;
  229. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
  230. goto err_nomem;
  231. if (allocate_resource(&_sparc_dvma, res, len_total,
  232. _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  233. printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
  234. goto err_nova;
  235. }
  236. mmu_inval_dma_area(va, len_total);
  237. // XXX The mmu_map_dma_area does this for us below, see comments.
  238. // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
  239. /*
  240. * XXX That's where sdev would be used. Currently we load
  241. * all iommu tables with the same translations.
  242. */
  243. if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
  244. goto err_noiommu;
  245. res->name = op->node->name;
  246. return (void *)(unsigned long)res->start;
  247. err_noiommu:
  248. release_resource(res);
  249. err_nova:
  250. free_pages(va, order);
  251. err_nomem:
  252. kfree(res);
  253. err_nopages:
  254. return NULL;
  255. }
  256. void sbus_free_consistent(struct device *dev, long n, void *p, u32 ba)
  257. {
  258. struct resource *res;
  259. struct page *pgv;
  260. if ((res = _sparc_find_resource(&_sparc_dvma,
  261. (unsigned long)p)) == NULL) {
  262. printk("sbus_free_consistent: cannot free %p\n", p);
  263. return;
  264. }
  265. if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
  266. printk("sbus_free_consistent: unaligned va %p\n", p);
  267. return;
  268. }
  269. n = (n + PAGE_SIZE-1) & PAGE_MASK;
  270. if ((res->end-res->start)+1 != n) {
  271. printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
  272. (long)((res->end-res->start)+1), n);
  273. return;
  274. }
  275. release_resource(res);
  276. kfree(res);
  277. /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
  278. pgv = virt_to_page(p);
  279. mmu_unmap_dma_area(dev, ba, n);
  280. __free_pages(pgv, get_order(n));
  281. }
  282. /*
  283. * Map a chunk of memory so that devices can see it.
  284. * CPU view of this memory may be inconsistent with
  285. * a device view and explicit flushing is necessary.
  286. */
  287. dma_addr_t sbus_map_single(struct device *dev, void *va, size_t len, int direction)
  288. {
  289. /* XXX why are some lengths signed, others unsigned? */
  290. if (len <= 0) {
  291. return 0;
  292. }
  293. /* XXX So what is maxphys for us and how do drivers know it? */
  294. if (len > 256*1024) { /* __get_free_pages() limit */
  295. return 0;
  296. }
  297. return mmu_get_scsi_one(dev, va, len);
  298. }
  299. void sbus_unmap_single(struct device *dev, dma_addr_t ba, size_t n, int direction)
  300. {
  301. mmu_release_scsi_one(dev, ba, n);
  302. }
  303. int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
  304. {
  305. mmu_get_scsi_sgl(dev, sg, n);
  306. /*
  307. * XXX sparc64 can return a partial length here. sun4c should do this
  308. * but it currently panics if it can't fulfill the request - Anton
  309. */
  310. return n;
  311. }
  312. void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
  313. {
  314. mmu_release_scsi_sgl(dev, sg, n);
  315. }
  316. void sbus_dma_sync_single_for_cpu(struct device *dev, dma_addr_t ba, size_t size, int direction)
  317. {
  318. }
  319. void sbus_dma_sync_single_for_device(struct device *dev, dma_addr_t ba, size_t size, int direction)
  320. {
  321. }
  322. /* Support code for sbus_init(). */
  323. void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
  324. {
  325. #ifndef CONFIG_SUN4
  326. struct device_node *parent = dp->parent;
  327. if (sparc_cpu_model != sun4d &&
  328. parent != NULL &&
  329. !strcmp(parent->name, "iommu"))
  330. iommu_init(parent, sbus);
  331. if (sparc_cpu_model == sun4d)
  332. iounit_init(sbus);
  333. #endif
  334. }
  335. int __init sbus_arch_preinit(void)
  336. {
  337. register_proc_sparc_ioport();
  338. #ifdef CONFIG_SUN4
  339. {
  340. extern void sun4_dvma_init(void);
  341. sun4_dvma_init();
  342. }
  343. return 1;
  344. #else
  345. return 0;
  346. #endif
  347. }
  348. void __init sbus_arch_postinit(void)
  349. {
  350. if (sparc_cpu_model == sun4d) {
  351. extern void sun4d_init_sbi_irq(void);
  352. sun4d_init_sbi_irq();
  353. }
  354. }
  355. #endif /* CONFIG_SBUS */
  356. #ifdef CONFIG_PCI
  357. /* Allocate and map kernel buffer using consistent mode DMA for a device.
  358. * hwdev should be valid struct pci_dev pointer for PCI devices.
  359. */
  360. void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
  361. {
  362. unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
  363. unsigned long va;
  364. struct resource *res;
  365. int order;
  366. if (len == 0) {
  367. return NULL;
  368. }
  369. if (len > 256*1024) { /* __get_free_pages() limit */
  370. return NULL;
  371. }
  372. order = get_order(len_total);
  373. va = __get_free_pages(GFP_KERNEL, order);
  374. if (va == 0) {
  375. printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
  376. return NULL;
  377. }
  378. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
  379. free_pages(va, order);
  380. printk("pci_alloc_consistent: no core\n");
  381. return NULL;
  382. }
  383. if (allocate_resource(&_sparc_dvma, res, len_total,
  384. _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  385. printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
  386. free_pages(va, order);
  387. kfree(res);
  388. return NULL;
  389. }
  390. mmu_inval_dma_area(va, len_total);
  391. #if 0
  392. /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
  393. (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
  394. #endif
  395. sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
  396. *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
  397. return (void *) res->start;
  398. }
  399. /* Free and unmap a consistent DMA buffer.
  400. * cpu_addr is what was returned from pci_alloc_consistent,
  401. * size must be the same as what as passed into pci_alloc_consistent,
  402. * and likewise dma_addr must be the same as what *dma_addrp was set to.
  403. *
  404. * References to the memory and mappings associated with cpu_addr/dma_addr
  405. * past this call are illegal.
  406. */
  407. void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
  408. {
  409. struct resource *res;
  410. unsigned long pgp;
  411. if ((res = _sparc_find_resource(&_sparc_dvma,
  412. (unsigned long)p)) == NULL) {
  413. printk("pci_free_consistent: cannot free %p\n", p);
  414. return;
  415. }
  416. if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
  417. printk("pci_free_consistent: unaligned va %p\n", p);
  418. return;
  419. }
  420. n = (n + PAGE_SIZE-1) & PAGE_MASK;
  421. if ((res->end-res->start)+1 != n) {
  422. printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
  423. (long)((res->end-res->start)+1), (long)n);
  424. return;
  425. }
  426. pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
  427. mmu_inval_dma_area(pgp, n);
  428. sparc_unmapiorange((unsigned long)p, n);
  429. release_resource(res);
  430. kfree(res);
  431. free_pages(pgp, get_order(n));
  432. }
  433. /* Map a single buffer of the indicated size for DMA in streaming mode.
  434. * The 32-bit bus address to use is returned.
  435. *
  436. * Once the device is given the dma address, the device owns this memory
  437. * until either pci_unmap_single or pci_dma_sync_single_* is performed.
  438. */
  439. dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
  440. int direction)
  441. {
  442. BUG_ON(direction == PCI_DMA_NONE);
  443. /* IIep is write-through, not flushing. */
  444. return virt_to_phys(ptr);
  445. }
  446. /* Unmap a single streaming mode DMA translation. The dma_addr and size
  447. * must match what was provided for in a previous pci_map_single call. All
  448. * other usages are undefined.
  449. *
  450. * After this call, reads by the cpu to the buffer are guaranteed to see
  451. * whatever the device wrote there.
  452. */
  453. void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
  454. int direction)
  455. {
  456. BUG_ON(direction == PCI_DMA_NONE);
  457. if (direction != PCI_DMA_TODEVICE) {
  458. mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
  459. (size + PAGE_SIZE-1) & PAGE_MASK);
  460. }
  461. }
  462. /*
  463. * Same as pci_map_single, but with pages.
  464. */
  465. dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
  466. unsigned long offset, size_t size, int direction)
  467. {
  468. BUG_ON(direction == PCI_DMA_NONE);
  469. /* IIep is write-through, not flushing. */
  470. return page_to_phys(page) + offset;
  471. }
  472. void pci_unmap_page(struct pci_dev *hwdev,
  473. dma_addr_t dma_address, size_t size, int direction)
  474. {
  475. BUG_ON(direction == PCI_DMA_NONE);
  476. /* mmu_inval_dma_area XXX */
  477. }
  478. /* Map a set of buffers described by scatterlist in streaming
  479. * mode for DMA. This is the scather-gather version of the
  480. * above pci_map_single interface. Here the scatter gather list
  481. * elements are each tagged with the appropriate dma address
  482. * and length. They are obtained via sg_dma_{address,length}(SG).
  483. *
  484. * NOTE: An implementation may be able to use a smaller number of
  485. * DMA address/length pairs than there are SG table elements.
  486. * (for example via virtual mapping capabilities)
  487. * The routine returns the number of addr/length pairs actually
  488. * used, at most nents.
  489. *
  490. * Device ownership issues as mentioned above for pci_map_single are
  491. * the same here.
  492. */
  493. int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
  494. int direction)
  495. {
  496. struct scatterlist *sg;
  497. int n;
  498. BUG_ON(direction == PCI_DMA_NONE);
  499. /* IIep is write-through, not flushing. */
  500. for_each_sg(sgl, sg, nents, n) {
  501. BUG_ON(page_address(sg_page(sg)) == NULL);
  502. sg->dvma_address = virt_to_phys(sg_virt(sg));
  503. sg->dvma_length = sg->length;
  504. }
  505. return nents;
  506. }
  507. /* Unmap a set of streaming mode DMA translations.
  508. * Again, cpu read rules concerning calls here are the same as for
  509. * pci_unmap_single() above.
  510. */
  511. void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
  512. int direction)
  513. {
  514. struct scatterlist *sg;
  515. int n;
  516. BUG_ON(direction == PCI_DMA_NONE);
  517. if (direction != PCI_DMA_TODEVICE) {
  518. for_each_sg(sgl, sg, nents, n) {
  519. BUG_ON(page_address(sg_page(sg)) == NULL);
  520. mmu_inval_dma_area(
  521. (unsigned long) page_address(sg_page(sg)),
  522. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  523. }
  524. }
  525. }
  526. /* Make physical memory consistent for a single
  527. * streaming mode DMA translation before or after a transfer.
  528. *
  529. * If you perform a pci_map_single() but wish to interrogate the
  530. * buffer using the cpu, yet do not wish to teardown the PCI dma
  531. * mapping, you must call this function before doing so. At the
  532. * next point you give the PCI dma address back to the card, you
  533. * must first perform a pci_dma_sync_for_device, and then the
  534. * device again owns the buffer.
  535. */
  536. void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
  537. {
  538. BUG_ON(direction == PCI_DMA_NONE);
  539. if (direction != PCI_DMA_TODEVICE) {
  540. mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
  541. (size + PAGE_SIZE-1) & PAGE_MASK);
  542. }
  543. }
  544. void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
  545. {
  546. BUG_ON(direction == PCI_DMA_NONE);
  547. if (direction != PCI_DMA_TODEVICE) {
  548. mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
  549. (size + PAGE_SIZE-1) & PAGE_MASK);
  550. }
  551. }
  552. /* Make physical memory consistent for a set of streaming
  553. * mode DMA translations after a transfer.
  554. *
  555. * The same as pci_dma_sync_single_* but for a scatter-gather list,
  556. * same rules and usage.
  557. */
  558. void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
  559. {
  560. struct scatterlist *sg;
  561. int n;
  562. BUG_ON(direction == PCI_DMA_NONE);
  563. if (direction != PCI_DMA_TODEVICE) {
  564. for_each_sg(sgl, sg, nents, n) {
  565. BUG_ON(page_address(sg_page(sg)) == NULL);
  566. mmu_inval_dma_area(
  567. (unsigned long) page_address(sg_page(sg)),
  568. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  569. }
  570. }
  571. }
  572. void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
  573. {
  574. struct scatterlist *sg;
  575. int n;
  576. BUG_ON(direction == PCI_DMA_NONE);
  577. if (direction != PCI_DMA_TODEVICE) {
  578. for_each_sg(sgl, sg, nents, n) {
  579. BUG_ON(page_address(sg_page(sg)) == NULL);
  580. mmu_inval_dma_area(
  581. (unsigned long) page_address(sg_page(sg)),
  582. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  583. }
  584. }
  585. }
  586. #endif /* CONFIG_PCI */
  587. #ifdef CONFIG_PROC_FS
  588. static int
  589. _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
  590. void *data)
  591. {
  592. char *p = buf, *e = buf + length;
  593. struct resource *r;
  594. const char *nm;
  595. for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
  596. if (p + 32 >= e) /* Better than nothing */
  597. break;
  598. if ((nm = r->name) == 0) nm = "???";
  599. p += sprintf(p, "%016llx-%016llx: %s\n",
  600. (unsigned long long)r->start,
  601. (unsigned long long)r->end, nm);
  602. }
  603. return p-buf;
  604. }
  605. #endif /* CONFIG_PROC_FS */
  606. /*
  607. * This is a version of find_resource and it belongs to kernel/resource.c.
  608. * Until we have agreement with Linus and Martin, it lingers here.
  609. *
  610. * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
  611. * This probably warrants some sort of hashing.
  612. */
  613. static struct resource *_sparc_find_resource(struct resource *root,
  614. unsigned long hit)
  615. {
  616. struct resource *tmp;
  617. for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
  618. if (tmp->start <= hit && tmp->end >= hit)
  619. return tmp;
  620. }
  621. return NULL;
  622. }
  623. static void register_proc_sparc_ioport(void)
  624. {
  625. #ifdef CONFIG_PROC_FS
  626. create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
  627. create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
  628. #endif
  629. }