ioport.c 20 KB

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