ioport.c 20 KB

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