ioport.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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/page.h>
  44. #include <asm/pgalloc.h>
  45. #include <asm/dma.h>
  46. #include <asm/iommu.h>
  47. #include <asm/io-unit.h>
  48. #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
  49. static struct resource *_sparc_find_resource(struct resource *r,
  50. unsigned long);
  51. static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
  52. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  53. unsigned long size, char *name);
  54. static void _sparc_free_io(struct resource *res);
  55. static void register_proc_sparc_ioport(void);
  56. /* This points to the next to use virtual memory for DVMA mappings */
  57. static struct resource _sparc_dvma = {
  58. .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
  59. };
  60. /* This points to the start of I/O mappings, cluable from outside. */
  61. /*ext*/ struct resource sparc_iomap = {
  62. .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
  63. };
  64. /*
  65. * Our mini-allocator...
  66. * Boy this is gross! We need it because we must map I/O for
  67. * timers and interrupt controller before the kmalloc is available.
  68. */
  69. #define XNMLN 15
  70. #define XNRES 10 /* SS-10 uses 8 */
  71. struct xresource {
  72. struct resource xres; /* Must be first */
  73. int xflag; /* 1 == used */
  74. char xname[XNMLN+1];
  75. };
  76. static struct xresource xresv[XNRES];
  77. static struct xresource *xres_alloc(void) {
  78. struct xresource *xrp;
  79. int n;
  80. xrp = xresv;
  81. for (n = 0; n < XNRES; n++) {
  82. if (xrp->xflag == 0) {
  83. xrp->xflag = 1;
  84. return xrp;
  85. }
  86. xrp++;
  87. }
  88. return NULL;
  89. }
  90. static void xres_free(struct xresource *xrp) {
  91. xrp->xflag = 0;
  92. }
  93. /*
  94. * These are typically used in PCI drivers
  95. * which are trying to be cross-platform.
  96. *
  97. * Bus type is always zero on IIep.
  98. */
  99. void __iomem *ioremap(unsigned long offset, unsigned long size)
  100. {
  101. char name[14];
  102. sprintf(name, "phys_%08x", (u32)offset);
  103. return _sparc_alloc_io(0, offset, size, name);
  104. }
  105. EXPORT_SYMBOL(ioremap);
  106. /*
  107. * Comlimentary to ioremap().
  108. */
  109. void iounmap(volatile void __iomem *virtual)
  110. {
  111. unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
  112. struct resource *res;
  113. if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
  114. printk("free_io/iounmap: cannot free %lx\n", vaddr);
  115. return;
  116. }
  117. _sparc_free_io(res);
  118. if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
  119. xres_free((struct xresource *)res);
  120. } else {
  121. kfree(res);
  122. }
  123. }
  124. EXPORT_SYMBOL(iounmap);
  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. EXPORT_SYMBOL(sbus_set_sbus64);
  207. /*
  208. * Allocate a chunk of memory suitable for DMA.
  209. * Typically devices use them for control blocks.
  210. * CPU may access them without any explicit flushing.
  211. */
  212. static void *sbus_alloc_coherent(struct device *dev, size_t len,
  213. dma_addr_t *dma_addrp, gfp_t gfp)
  214. {
  215. struct of_device *op = to_of_device(dev);
  216. unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
  217. unsigned long va;
  218. struct resource *res;
  219. int order;
  220. /* XXX why are some lengths signed, others unsigned? */
  221. if (len <= 0) {
  222. return NULL;
  223. }
  224. /* XXX So what is maxphys for us and how do drivers know it? */
  225. if (len > 256*1024) { /* __get_free_pages() limit */
  226. return NULL;
  227. }
  228. order = get_order(len_total);
  229. if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
  230. goto err_nopages;
  231. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
  232. goto err_nomem;
  233. if (allocate_resource(&_sparc_dvma, res, len_total,
  234. _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  235. printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
  236. goto err_nova;
  237. }
  238. mmu_inval_dma_area(va, len_total);
  239. // XXX The mmu_map_dma_area does this for us below, see comments.
  240. // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
  241. /*
  242. * XXX That's where sdev would be used. Currently we load
  243. * all iommu tables with the same translations.
  244. */
  245. if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
  246. goto err_noiommu;
  247. res->name = op->node->name;
  248. return (void *)(unsigned long)res->start;
  249. err_noiommu:
  250. release_resource(res);
  251. err_nova:
  252. free_pages(va, order);
  253. err_nomem:
  254. kfree(res);
  255. err_nopages:
  256. return NULL;
  257. }
  258. static void sbus_free_coherent(struct device *dev, size_t n, void *p,
  259. dma_addr_t ba)
  260. {
  261. struct resource *res;
  262. struct page *pgv;
  263. if ((res = _sparc_find_resource(&_sparc_dvma,
  264. (unsigned long)p)) == NULL) {
  265. printk("sbus_free_consistent: cannot free %p\n", p);
  266. return;
  267. }
  268. if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
  269. printk("sbus_free_consistent: unaligned va %p\n", p);
  270. return;
  271. }
  272. n = (n + PAGE_SIZE-1) & PAGE_MASK;
  273. if ((res->end-res->start)+1 != n) {
  274. printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n",
  275. (long)((res->end-res->start)+1), n);
  276. return;
  277. }
  278. release_resource(res);
  279. kfree(res);
  280. /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
  281. pgv = virt_to_page(p);
  282. mmu_unmap_dma_area(dev, ba, n);
  283. __free_pages(pgv, get_order(n));
  284. }
  285. /*
  286. * Map a chunk of memory so that devices can see it.
  287. * CPU view of this memory may be inconsistent with
  288. * a device view and explicit flushing is necessary.
  289. */
  290. static dma_addr_t sbus_map_page(struct device *dev, struct page *page,
  291. unsigned long offset, size_t len,
  292. enum dma_data_direction dir,
  293. struct dma_attrs *attrs)
  294. {
  295. void *va = page_address(page) + offset;
  296. /* XXX why are some lengths signed, others unsigned? */
  297. if (len <= 0) {
  298. return 0;
  299. }
  300. /* XXX So what is maxphys for us and how do drivers know it? */
  301. if (len > 256*1024) { /* __get_free_pages() limit */
  302. return 0;
  303. }
  304. return mmu_get_scsi_one(dev, va, len);
  305. }
  306. static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n,
  307. enum dma_data_direction dir, struct dma_attrs *attrs)
  308. {
  309. mmu_release_scsi_one(dev, ba, n);
  310. }
  311. static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n,
  312. enum dma_data_direction dir, struct dma_attrs *attrs)
  313. {
  314. mmu_get_scsi_sgl(dev, sg, n);
  315. /*
  316. * XXX sparc64 can return a partial length here. sun4c should do this
  317. * but it currently panics if it can't fulfill the request - Anton
  318. */
  319. return n;
  320. }
  321. static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n,
  322. enum dma_data_direction dir, struct dma_attrs *attrs)
  323. {
  324. mmu_release_scsi_sgl(dev, sg, n);
  325. }
  326. static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  327. int n, enum dma_data_direction dir)
  328. {
  329. BUG();
  330. }
  331. static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  332. int n, enum dma_data_direction dir)
  333. {
  334. BUG();
  335. }
  336. struct dma_map_ops sbus_dma_ops = {
  337. .alloc_coherent = sbus_alloc_coherent,
  338. .free_coherent = sbus_free_coherent,
  339. .map_page = sbus_map_page,
  340. .unmap_page = sbus_unmap_page,
  341. .map_sg = sbus_map_sg,
  342. .unmap_sg = sbus_unmap_sg,
  343. .sync_sg_for_cpu = sbus_sync_sg_for_cpu,
  344. .sync_sg_for_device = sbus_sync_sg_for_device,
  345. };
  346. struct dma_map_ops *dma_ops = &sbus_dma_ops;
  347. EXPORT_SYMBOL(dma_ops);
  348. static int __init sparc_register_ioport(void)
  349. {
  350. register_proc_sparc_ioport();
  351. return 0;
  352. }
  353. arch_initcall(sparc_register_ioport);
  354. #endif /* CONFIG_SBUS */
  355. #ifdef CONFIG_PCI
  356. /* Allocate and map kernel buffer using consistent mode DMA for a device.
  357. * hwdev should be valid struct pci_dev pointer for PCI devices.
  358. */
  359. static void *pci32_alloc_coherent(struct device *dev, size_t len,
  360. dma_addr_t *pba, gfp_t gfp)
  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. static void pci32_free_coherent(struct device *dev, size_t n, void *p,
  408. dma_addr_t ba)
  409. {
  410. struct resource *res;
  411. unsigned long pgp;
  412. if ((res = _sparc_find_resource(&_sparc_dvma,
  413. (unsigned long)p)) == NULL) {
  414. printk("pci_free_consistent: cannot free %p\n", p);
  415. return;
  416. }
  417. if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
  418. printk("pci_free_consistent: unaligned va %p\n", p);
  419. return;
  420. }
  421. n = (n + PAGE_SIZE-1) & PAGE_MASK;
  422. if ((res->end-res->start)+1 != n) {
  423. printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
  424. (long)((res->end-res->start)+1), (long)n);
  425. return;
  426. }
  427. pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
  428. mmu_inval_dma_area(pgp, n);
  429. sparc_unmapiorange((unsigned long)p, n);
  430. release_resource(res);
  431. kfree(res);
  432. free_pages(pgp, get_order(n));
  433. }
  434. /*
  435. * Same as pci_map_single, but with pages.
  436. */
  437. static dma_addr_t pci32_map_page(struct device *dev, struct page *page,
  438. unsigned long offset, size_t size,
  439. enum dma_data_direction dir,
  440. struct dma_attrs *attrs)
  441. {
  442. /* IIep is write-through, not flushing. */
  443. return page_to_phys(page) + offset;
  444. }
  445. /* Map a set of buffers described by scatterlist in streaming
  446. * mode for DMA. This is the scather-gather version of the
  447. * above pci_map_single interface. Here the scatter gather list
  448. * elements are each tagged with the appropriate dma address
  449. * and length. They are obtained via sg_dma_{address,length}(SG).
  450. *
  451. * NOTE: An implementation may be able to use a smaller number of
  452. * DMA address/length pairs than there are SG table elements.
  453. * (for example via virtual mapping capabilities)
  454. * The routine returns the number of addr/length pairs actually
  455. * used, at most nents.
  456. *
  457. * Device ownership issues as mentioned above for pci_map_single are
  458. * the same here.
  459. */
  460. static int pci32_map_sg(struct device *device, struct scatterlist *sgl,
  461. int nents, enum dma_data_direction dir,
  462. struct dma_attrs *attrs)
  463. {
  464. struct scatterlist *sg;
  465. int n;
  466. /* IIep is write-through, not flushing. */
  467. for_each_sg(sgl, sg, nents, n) {
  468. BUG_ON(page_address(sg_page(sg)) == NULL);
  469. sg->dma_address = virt_to_phys(sg_virt(sg));
  470. sg->dma_length = sg->length;
  471. }
  472. return nents;
  473. }
  474. /* Unmap a set of streaming mode DMA translations.
  475. * Again, cpu read rules concerning calls here are the same as for
  476. * pci_unmap_single() above.
  477. */
  478. static void pci32_unmap_sg(struct device *dev, struct scatterlist *sgl,
  479. int nents, enum dma_data_direction dir,
  480. struct dma_attrs *attrs)
  481. {
  482. struct scatterlist *sg;
  483. int n;
  484. if (dir != PCI_DMA_TODEVICE) {
  485. for_each_sg(sgl, sg, nents, n) {
  486. BUG_ON(page_address(sg_page(sg)) == NULL);
  487. mmu_inval_dma_area(
  488. (unsigned long) page_address(sg_page(sg)),
  489. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  490. }
  491. }
  492. }
  493. /* Make physical memory consistent for a single
  494. * streaming mode DMA translation before or after a transfer.
  495. *
  496. * If you perform a pci_map_single() but wish to interrogate the
  497. * buffer using the cpu, yet do not wish to teardown the PCI dma
  498. * mapping, you must call this function before doing so. At the
  499. * next point you give the PCI dma address back to the card, you
  500. * must first perform a pci_dma_sync_for_device, and then the
  501. * device again owns the buffer.
  502. */
  503. static void pci32_sync_single_for_cpu(struct device *dev, dma_addr_t ba,
  504. size_t size, enum dma_data_direction dir)
  505. {
  506. if (dir != PCI_DMA_TODEVICE) {
  507. mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
  508. (size + PAGE_SIZE-1) & PAGE_MASK);
  509. }
  510. }
  511. static void pci32_sync_single_for_device(struct device *dev, dma_addr_t ba,
  512. size_t size, enum dma_data_direction dir)
  513. {
  514. if (dir != PCI_DMA_TODEVICE) {
  515. mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
  516. (size + PAGE_SIZE-1) & PAGE_MASK);
  517. }
  518. }
  519. /* Make physical memory consistent for a set of streaming
  520. * mode DMA translations after a transfer.
  521. *
  522. * The same as pci_dma_sync_single_* but for a scatter-gather list,
  523. * same rules and usage.
  524. */
  525. static void pci32_sync_sg_for_cpu(struct device *dev, struct scatterlist *sgl,
  526. int nents, enum dma_data_direction dir)
  527. {
  528. struct scatterlist *sg;
  529. int n;
  530. if (dir != PCI_DMA_TODEVICE) {
  531. for_each_sg(sgl, sg, nents, n) {
  532. BUG_ON(page_address(sg_page(sg)) == NULL);
  533. mmu_inval_dma_area(
  534. (unsigned long) page_address(sg_page(sg)),
  535. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  536. }
  537. }
  538. }
  539. static void pci32_sync_sg_for_device(struct device *device, struct scatterlist *sgl,
  540. int nents, enum dma_data_direction dir)
  541. {
  542. struct scatterlist *sg;
  543. int n;
  544. if (dir != PCI_DMA_TODEVICE) {
  545. for_each_sg(sgl, sg, nents, n) {
  546. BUG_ON(page_address(sg_page(sg)) == NULL);
  547. mmu_inval_dma_area(
  548. (unsigned long) page_address(sg_page(sg)),
  549. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  550. }
  551. }
  552. }
  553. struct dma_map_ops pci32_dma_ops = {
  554. .alloc_coherent = pci32_alloc_coherent,
  555. .free_coherent = pci32_free_coherent,
  556. .map_page = pci32_map_page,
  557. .map_sg = pci32_map_sg,
  558. .unmap_sg = pci32_unmap_sg,
  559. .sync_single_for_cpu = pci32_sync_single_for_cpu,
  560. .sync_single_for_device = pci32_sync_single_for_device,
  561. .sync_sg_for_cpu = pci32_sync_sg_for_cpu,
  562. .sync_sg_for_device = pci32_sync_sg_for_device,
  563. };
  564. EXPORT_SYMBOL(pci32_dma_ops);
  565. #endif /* CONFIG_PCI */
  566. #ifdef CONFIG_PROC_FS
  567. static int
  568. _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
  569. void *data)
  570. {
  571. char *p = buf, *e = buf + length;
  572. struct resource *r;
  573. const char *nm;
  574. for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
  575. if (p + 32 >= e) /* Better than nothing */
  576. break;
  577. if ((nm = r->name) == 0) nm = "???";
  578. p += sprintf(p, "%016llx-%016llx: %s\n",
  579. (unsigned long long)r->start,
  580. (unsigned long long)r->end, nm);
  581. }
  582. return p-buf;
  583. }
  584. #endif /* CONFIG_PROC_FS */
  585. /*
  586. * This is a version of find_resource and it belongs to kernel/resource.c.
  587. * Until we have agreement with Linus and Martin, it lingers here.
  588. *
  589. * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
  590. * This probably warrants some sort of hashing.
  591. */
  592. static struct resource *_sparc_find_resource(struct resource *root,
  593. unsigned long hit)
  594. {
  595. struct resource *tmp;
  596. for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
  597. if (tmp->start <= hit && tmp->end >= hit)
  598. return tmp;
  599. }
  600. return NULL;
  601. }
  602. static void register_proc_sparc_ioport(void)
  603. {
  604. #ifdef CONFIG_PROC_FS
  605. create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
  606. create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
  607. #endif
  608. }