ioport.c 19 KB

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