ioport.c 19 KB

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