ioport.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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/sbus.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 "dma.h"
  50. #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
  51. static struct resource *_sparc_find_resource(struct resource *r,
  52. unsigned long);
  53. static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
  54. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  55. unsigned long size, char *name);
  56. static void _sparc_free_io(struct resource *res);
  57. static void register_proc_sparc_ioport(void);
  58. /* This points to the next to use virtual memory for DVMA mappings */
  59. static struct resource _sparc_dvma = {
  60. .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
  61. };
  62. /* This points to the start of I/O mappings, cluable from outside. */
  63. /*ext*/ struct resource sparc_iomap = {
  64. .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
  65. };
  66. /*
  67. * Our mini-allocator...
  68. * Boy this is gross! We need it because we must map I/O for
  69. * timers and interrupt controller before the kmalloc is available.
  70. */
  71. #define XNMLN 15
  72. #define XNRES 10 /* SS-10 uses 8 */
  73. struct xresource {
  74. struct resource xres; /* Must be first */
  75. int xflag; /* 1 == used */
  76. char xname[XNMLN+1];
  77. };
  78. static struct xresource xresv[XNRES];
  79. static struct xresource *xres_alloc(void) {
  80. struct xresource *xrp;
  81. int n;
  82. xrp = xresv;
  83. for (n = 0; n < XNRES; n++) {
  84. if (xrp->xflag == 0) {
  85. xrp->xflag = 1;
  86. return xrp;
  87. }
  88. xrp++;
  89. }
  90. return NULL;
  91. }
  92. static void xres_free(struct xresource *xrp) {
  93. xrp->xflag = 0;
  94. }
  95. /*
  96. * These are typically used in PCI drivers
  97. * which are trying to be cross-platform.
  98. *
  99. * Bus type is always zero on IIep.
  100. */
  101. void __iomem *ioremap(unsigned long offset, unsigned long size)
  102. {
  103. char name[14];
  104. sprintf(name, "phys_%08x", (u32)offset);
  105. return _sparc_alloc_io(0, offset, size, name);
  106. }
  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. /*
  126. */
  127. void __iomem *sbus_ioremap(struct resource *phyres, unsigned long offset,
  128. unsigned long size, char *name)
  129. {
  130. return _sparc_alloc_io(phyres->flags & 0xF,
  131. phyres->start + offset, size, name);
  132. }
  133. void __iomem *of_ioremap(struct resource *res, unsigned long offset,
  134. unsigned long size, char *name)
  135. {
  136. return _sparc_alloc_io(res->flags & 0xF,
  137. res->start + offset,
  138. size, name);
  139. }
  140. EXPORT_SYMBOL(of_ioremap);
  141. void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
  142. {
  143. iounmap(base);
  144. }
  145. EXPORT_SYMBOL(of_iounmap);
  146. /*
  147. */
  148. void sbus_iounmap(volatile void __iomem *addr, unsigned long size)
  149. {
  150. iounmap(addr);
  151. }
  152. /*
  153. * Meat of mapping
  154. */
  155. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  156. unsigned long size, char *name)
  157. {
  158. static int printed_full;
  159. struct xresource *xres;
  160. struct resource *res;
  161. char *tack;
  162. int tlen;
  163. void __iomem *va; /* P3 diag */
  164. if (name == NULL) name = "???";
  165. if ((xres = xres_alloc()) != 0) {
  166. tack = xres->xname;
  167. res = &xres->xres;
  168. } else {
  169. if (!printed_full) {
  170. printk("ioremap: done with statics, switching to malloc\n");
  171. printed_full = 1;
  172. }
  173. tlen = strlen(name);
  174. tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
  175. if (tack == NULL) return NULL;
  176. memset(tack, 0, sizeof(struct resource));
  177. res = (struct resource *) tack;
  178. tack += sizeof (struct resource);
  179. }
  180. strlcpy(tack, name, XNMLN+1);
  181. res->name = tack;
  182. va = _sparc_ioremap(res, busno, phys, size);
  183. /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
  184. return va;
  185. }
  186. /*
  187. */
  188. static void __iomem *
  189. _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
  190. {
  191. unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
  192. if (allocate_resource(&sparc_iomap, res,
  193. (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
  194. sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
  195. /* Usually we cannot see printks in this case. */
  196. prom_printf("alloc_io_res(%s): cannot occupy\n",
  197. (res->name != NULL)? res->name: "???");
  198. prom_halt();
  199. }
  200. pa &= PAGE_MASK;
  201. sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
  202. return (void __iomem *)(unsigned long)(res->start + offset);
  203. }
  204. /*
  205. * Comlimentary to _sparc_ioremap().
  206. */
  207. static void _sparc_free_io(struct resource *res)
  208. {
  209. unsigned long plen;
  210. plen = res->end - res->start + 1;
  211. BUG_ON((plen & (PAGE_SIZE-1)) != 0);
  212. sparc_unmapiorange(res->start, plen);
  213. release_resource(res);
  214. }
  215. #ifdef CONFIG_SBUS
  216. void sbus_set_sbus64(struct device *dev, int x)
  217. {
  218. printk("sbus_set_sbus64: unsupported\n");
  219. }
  220. extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
  221. void __init sbus_fill_device_irq(struct sbus_dev *sdev)
  222. {
  223. struct linux_prom_irqs irqs[PROMINTR_MAX];
  224. int len;
  225. len = prom_getproperty(sdev->prom_node, "intr",
  226. (char *)irqs, sizeof(irqs));
  227. if (len != -1) {
  228. sdev->num_irqs = len / 8;
  229. if (sdev->num_irqs == 0) {
  230. sdev->irqs[0] = 0;
  231. } else if (sparc_cpu_model == sun4d) {
  232. for (len = 0; len < sdev->num_irqs; len++)
  233. sdev->irqs[len] =
  234. sun4d_build_irq(sdev, irqs[len].pri);
  235. } else {
  236. for (len = 0; len < sdev->num_irqs; len++)
  237. sdev->irqs[len] = irqs[len].pri;
  238. }
  239. } else {
  240. int interrupts[PROMINTR_MAX];
  241. /* No "intr" node found-- check for "interrupts" node.
  242. * This node contains SBus interrupt levels, not IPLs
  243. * as in "intr", and no vector values. We convert
  244. * SBus interrupt levels to PILs (platform specific).
  245. */
  246. len = prom_getproperty(sdev->prom_node, "interrupts",
  247. (char *)interrupts, sizeof(interrupts));
  248. if (len == -1) {
  249. sdev->irqs[0] = 0;
  250. sdev->num_irqs = 0;
  251. } else {
  252. sdev->num_irqs = len / sizeof(int);
  253. for (len = 0; len < sdev->num_irqs; len++) {
  254. sdev->irqs[len] =
  255. sbint_to_irq(sdev, interrupts[len]);
  256. }
  257. }
  258. }
  259. }
  260. /*
  261. * Allocate a chunk of memory suitable for DMA.
  262. * Typically devices use them for control blocks.
  263. * CPU may access them without any explicit flushing.
  264. */
  265. void *sbus_alloc_consistent(struct device *dev, long len, u32 *dma_addrp)
  266. {
  267. struct of_device *op = to_of_device(dev);
  268. unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
  269. unsigned long va;
  270. struct resource *res;
  271. int order;
  272. /* XXX why are some lengths signed, others unsigned? */
  273. if (len <= 0) {
  274. return NULL;
  275. }
  276. /* XXX So what is maxphys for us and how do drivers know it? */
  277. if (len > 256*1024) { /* __get_free_pages() limit */
  278. return NULL;
  279. }
  280. order = get_order(len_total);
  281. if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
  282. goto err_nopages;
  283. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
  284. goto err_nomem;
  285. if (allocate_resource(&_sparc_dvma, res, len_total,
  286. _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  287. printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
  288. goto err_nova;
  289. }
  290. mmu_inval_dma_area(va, len_total);
  291. // XXX The mmu_map_dma_area does this for us below, see comments.
  292. // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
  293. /*
  294. * XXX That's where sdev would be used. Currently we load
  295. * all iommu tables with the same translations.
  296. */
  297. if (mmu_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
  298. goto err_noiommu;
  299. res->name = op->node->name;
  300. return (void *)(unsigned long)res->start;
  301. err_noiommu:
  302. release_resource(res);
  303. err_nova:
  304. free_pages(va, order);
  305. err_nomem:
  306. kfree(res);
  307. err_nopages:
  308. return NULL;
  309. }
  310. void sbus_free_consistent(struct device *dev, long n, void *p, u32 ba)
  311. {
  312. struct resource *res;
  313. struct page *pgv;
  314. if ((res = _sparc_find_resource(&_sparc_dvma,
  315. (unsigned long)p)) == NULL) {
  316. printk("sbus_free_consistent: cannot free %p\n", p);
  317. return;
  318. }
  319. if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
  320. printk("sbus_free_consistent: unaligned va %p\n", p);
  321. return;
  322. }
  323. n = (n + PAGE_SIZE-1) & PAGE_MASK;
  324. if ((res->end-res->start)+1 != n) {
  325. printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
  326. (long)((res->end-res->start)+1), n);
  327. return;
  328. }
  329. release_resource(res);
  330. kfree(res);
  331. /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
  332. pgv = virt_to_page(p);
  333. mmu_unmap_dma_area(dev, ba, n);
  334. __free_pages(pgv, get_order(n));
  335. }
  336. /*
  337. * Map a chunk of memory so that devices can see it.
  338. * CPU view of this memory may be inconsistent with
  339. * a device view and explicit flushing is necessary.
  340. */
  341. dma_addr_t sbus_map_single(struct device *dev, void *va, size_t len, int direction)
  342. {
  343. /* XXX why are some lengths signed, others unsigned? */
  344. if (len <= 0) {
  345. return 0;
  346. }
  347. /* XXX So what is maxphys for us and how do drivers know it? */
  348. if (len > 256*1024) { /* __get_free_pages() limit */
  349. return 0;
  350. }
  351. return mmu_get_scsi_one(dev, va, len);
  352. }
  353. void sbus_unmap_single(struct device *dev, dma_addr_t ba, size_t n, int direction)
  354. {
  355. mmu_release_scsi_one(dev, ba, n);
  356. }
  357. int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
  358. {
  359. mmu_get_scsi_sgl(dev, sg, n);
  360. /*
  361. * XXX sparc64 can return a partial length here. sun4c should do this
  362. * but it currently panics if it can't fulfill the request - Anton
  363. */
  364. return n;
  365. }
  366. void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n, int direction)
  367. {
  368. mmu_release_scsi_sgl(dev, sg, n);
  369. }
  370. void sbus_dma_sync_single_for_cpu(struct device *dev, dma_addr_t ba, size_t size, int direction)
  371. {
  372. }
  373. void sbus_dma_sync_single_for_device(struct device *dev, dma_addr_t ba, size_t size, int direction)
  374. {
  375. }
  376. /* Support code for sbus_init(). */
  377. /*
  378. * XXX This functions appears to be a distorted version of
  379. * prom_sbus_ranges_init(), with all sun4d stuff cut away.
  380. * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
  381. */
  382. /* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
  383. void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
  384. {
  385. int parent_node = pn->node;
  386. if (sparc_cpu_model == sun4d) {
  387. struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
  388. int num_iounit_ranges, len;
  389. len = prom_getproperty(parent_node, "ranges",
  390. (char *) iounit_ranges,
  391. sizeof (iounit_ranges));
  392. if (len != -1) {
  393. num_iounit_ranges =
  394. (len / sizeof(struct linux_prom_ranges));
  395. prom_adjust_ranges(sbus->sbus_ranges,
  396. sbus->num_sbus_ranges,
  397. iounit_ranges, num_iounit_ranges);
  398. }
  399. }
  400. }
  401. void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
  402. {
  403. #ifndef CONFIG_SUN4
  404. struct device_node *parent = dp->parent;
  405. if (sparc_cpu_model != sun4d &&
  406. parent != NULL &&
  407. !strcmp(parent->name, "iommu"))
  408. iommu_init(parent, sbus);
  409. if (sparc_cpu_model == sun4d)
  410. iounit_init(sbus);
  411. #endif
  412. }
  413. void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
  414. {
  415. if (sparc_cpu_model == sun4d) {
  416. struct device_node *parent = dp->parent;
  417. sbus->devid = of_getintprop_default(parent, "device-id", 0);
  418. sbus->board = of_getintprop_default(parent, "board#", 0);
  419. }
  420. }
  421. int __init sbus_arch_preinit(void)
  422. {
  423. register_proc_sparc_ioport();
  424. #ifdef CONFIG_SUN4
  425. {
  426. extern void sun4_dvma_init(void);
  427. sun4_dvma_init();
  428. }
  429. return 1;
  430. #else
  431. return 0;
  432. #endif
  433. }
  434. void __init sbus_arch_postinit(void)
  435. {
  436. if (sparc_cpu_model == sun4d) {
  437. extern void sun4d_init_sbi_irq(void);
  438. sun4d_init_sbi_irq();
  439. }
  440. }
  441. #endif /* CONFIG_SBUS */
  442. #ifdef CONFIG_PCI
  443. /* Allocate and map kernel buffer using consistent mode DMA for a device.
  444. * hwdev should be valid struct pci_dev pointer for PCI devices.
  445. */
  446. void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
  447. {
  448. unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
  449. unsigned long va;
  450. struct resource *res;
  451. int order;
  452. if (len == 0) {
  453. return NULL;
  454. }
  455. if (len > 256*1024) { /* __get_free_pages() limit */
  456. return NULL;
  457. }
  458. order = get_order(len_total);
  459. va = __get_free_pages(GFP_KERNEL, order);
  460. if (va == 0) {
  461. printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
  462. return NULL;
  463. }
  464. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
  465. free_pages(va, order);
  466. printk("pci_alloc_consistent: no core\n");
  467. return NULL;
  468. }
  469. if (allocate_resource(&_sparc_dvma, res, len_total,
  470. _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  471. printk("pci_alloc_consistent: cannot occupy 0x%lx", len_total);
  472. free_pages(va, order);
  473. kfree(res);
  474. return NULL;
  475. }
  476. mmu_inval_dma_area(va, len_total);
  477. #if 0
  478. /* P3 */ printk("pci_alloc_consistent: kva %lx uncva %lx phys %lx size %lx\n",
  479. (long)va, (long)res->start, (long)virt_to_phys(va), len_total);
  480. #endif
  481. sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
  482. *pba = virt_to_phys(va); /* equals virt_to_bus (R.I.P.) for us. */
  483. return (void *) res->start;
  484. }
  485. /* Free and unmap a consistent DMA buffer.
  486. * cpu_addr is what was returned from pci_alloc_consistent,
  487. * size must be the same as what as passed into pci_alloc_consistent,
  488. * and likewise dma_addr must be the same as what *dma_addrp was set to.
  489. *
  490. * References to the memory and mappings associated with cpu_addr/dma_addr
  491. * past this call are illegal.
  492. */
  493. void pci_free_consistent(struct pci_dev *pdev, size_t n, void *p, dma_addr_t ba)
  494. {
  495. struct resource *res;
  496. unsigned long pgp;
  497. if ((res = _sparc_find_resource(&_sparc_dvma,
  498. (unsigned long)p)) == NULL) {
  499. printk("pci_free_consistent: cannot free %p\n", p);
  500. return;
  501. }
  502. if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
  503. printk("pci_free_consistent: unaligned va %p\n", p);
  504. return;
  505. }
  506. n = (n + PAGE_SIZE-1) & PAGE_MASK;
  507. if ((res->end-res->start)+1 != n) {
  508. printk("pci_free_consistent: region 0x%lx asked 0x%lx\n",
  509. (long)((res->end-res->start)+1), (long)n);
  510. return;
  511. }
  512. pgp = (unsigned long) phys_to_virt(ba); /* bus_to_virt actually */
  513. mmu_inval_dma_area(pgp, n);
  514. sparc_unmapiorange((unsigned long)p, n);
  515. release_resource(res);
  516. kfree(res);
  517. free_pages(pgp, get_order(n));
  518. }
  519. /* Map a single buffer of the indicated size for DMA in streaming mode.
  520. * The 32-bit bus address to use is returned.
  521. *
  522. * Once the device is given the dma address, the device owns this memory
  523. * until either pci_unmap_single or pci_dma_sync_single_* is performed.
  524. */
  525. dma_addr_t pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size,
  526. int direction)
  527. {
  528. BUG_ON(direction == PCI_DMA_NONE);
  529. /* IIep is write-through, not flushing. */
  530. return virt_to_phys(ptr);
  531. }
  532. /* Unmap a single streaming mode DMA translation. The dma_addr and size
  533. * must match what was provided for in a previous pci_map_single call. All
  534. * other usages are undefined.
  535. *
  536. * After this call, reads by the cpu to the buffer are guaranteed to see
  537. * whatever the device wrote there.
  538. */
  539. void pci_unmap_single(struct pci_dev *hwdev, dma_addr_t ba, size_t size,
  540. int direction)
  541. {
  542. BUG_ON(direction == PCI_DMA_NONE);
  543. if (direction != PCI_DMA_TODEVICE) {
  544. mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
  545. (size + PAGE_SIZE-1) & PAGE_MASK);
  546. }
  547. }
  548. /*
  549. * Same as pci_map_single, but with pages.
  550. */
  551. dma_addr_t pci_map_page(struct pci_dev *hwdev, struct page *page,
  552. unsigned long offset, size_t size, int direction)
  553. {
  554. BUG_ON(direction == PCI_DMA_NONE);
  555. /* IIep is write-through, not flushing. */
  556. return page_to_phys(page) + offset;
  557. }
  558. void pci_unmap_page(struct pci_dev *hwdev,
  559. dma_addr_t dma_address, size_t size, int direction)
  560. {
  561. BUG_ON(direction == PCI_DMA_NONE);
  562. /* mmu_inval_dma_area XXX */
  563. }
  564. /* Map a set of buffers described by scatterlist in streaming
  565. * mode for DMA. This is the scather-gather version of the
  566. * above pci_map_single interface. Here the scatter gather list
  567. * elements are each tagged with the appropriate dma address
  568. * and length. They are obtained via sg_dma_{address,length}(SG).
  569. *
  570. * NOTE: An implementation may be able to use a smaller number of
  571. * DMA address/length pairs than there are SG table elements.
  572. * (for example via virtual mapping capabilities)
  573. * The routine returns the number of addr/length pairs actually
  574. * used, at most nents.
  575. *
  576. * Device ownership issues as mentioned above for pci_map_single are
  577. * the same here.
  578. */
  579. int pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
  580. int direction)
  581. {
  582. struct scatterlist *sg;
  583. int n;
  584. BUG_ON(direction == PCI_DMA_NONE);
  585. /* IIep is write-through, not flushing. */
  586. for_each_sg(sgl, sg, nents, n) {
  587. BUG_ON(page_address(sg_page(sg)) == NULL);
  588. sg->dvma_address = virt_to_phys(sg_virt(sg));
  589. sg->dvma_length = sg->length;
  590. }
  591. return nents;
  592. }
  593. /* Unmap a set of streaming mode DMA translations.
  594. * Again, cpu read rules concerning calls here are the same as for
  595. * pci_unmap_single() above.
  596. */
  597. void pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sgl, int nents,
  598. int direction)
  599. {
  600. struct scatterlist *sg;
  601. int n;
  602. BUG_ON(direction == PCI_DMA_NONE);
  603. if (direction != PCI_DMA_TODEVICE) {
  604. for_each_sg(sgl, sg, nents, n) {
  605. BUG_ON(page_address(sg_page(sg)) == NULL);
  606. mmu_inval_dma_area(
  607. (unsigned long) page_address(sg_page(sg)),
  608. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  609. }
  610. }
  611. }
  612. /* Make physical memory consistent for a single
  613. * streaming mode DMA translation before or after a transfer.
  614. *
  615. * If you perform a pci_map_single() but wish to interrogate the
  616. * buffer using the cpu, yet do not wish to teardown the PCI dma
  617. * mapping, you must call this function before doing so. At the
  618. * next point you give the PCI dma address back to the card, you
  619. * must first perform a pci_dma_sync_for_device, and then the
  620. * device again owns the buffer.
  621. */
  622. void pci_dma_sync_single_for_cpu(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
  623. {
  624. BUG_ON(direction == PCI_DMA_NONE);
  625. if (direction != PCI_DMA_TODEVICE) {
  626. mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
  627. (size + PAGE_SIZE-1) & PAGE_MASK);
  628. }
  629. }
  630. void pci_dma_sync_single_for_device(struct pci_dev *hwdev, dma_addr_t ba, size_t size, int direction)
  631. {
  632. BUG_ON(direction == PCI_DMA_NONE);
  633. if (direction != PCI_DMA_TODEVICE) {
  634. mmu_inval_dma_area((unsigned long)phys_to_virt(ba),
  635. (size + PAGE_SIZE-1) & PAGE_MASK);
  636. }
  637. }
  638. /* Make physical memory consistent for a set of streaming
  639. * mode DMA translations after a transfer.
  640. *
  641. * The same as pci_dma_sync_single_* but for a scatter-gather list,
  642. * same rules and usage.
  643. */
  644. void pci_dma_sync_sg_for_cpu(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
  645. {
  646. struct scatterlist *sg;
  647. int n;
  648. BUG_ON(direction == PCI_DMA_NONE);
  649. if (direction != PCI_DMA_TODEVICE) {
  650. for_each_sg(sgl, sg, nents, n) {
  651. BUG_ON(page_address(sg_page(sg)) == NULL);
  652. mmu_inval_dma_area(
  653. (unsigned long) page_address(sg_page(sg)),
  654. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  655. }
  656. }
  657. }
  658. void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
  659. {
  660. struct scatterlist *sg;
  661. int n;
  662. BUG_ON(direction == PCI_DMA_NONE);
  663. if (direction != PCI_DMA_TODEVICE) {
  664. for_each_sg(sgl, sg, nents, n) {
  665. BUG_ON(page_address(sg_page(sg)) == NULL);
  666. mmu_inval_dma_area(
  667. (unsigned long) page_address(sg_page(sg)),
  668. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  669. }
  670. }
  671. }
  672. #endif /* CONFIG_PCI */
  673. #ifdef CONFIG_PROC_FS
  674. static int
  675. _sparc_io_get_info(char *buf, char **start, off_t fpos, int length, int *eof,
  676. void *data)
  677. {
  678. char *p = buf, *e = buf + length;
  679. struct resource *r;
  680. const char *nm;
  681. for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
  682. if (p + 32 >= e) /* Better than nothing */
  683. break;
  684. if ((nm = r->name) == 0) nm = "???";
  685. p += sprintf(p, "%016llx-%016llx: %s\n",
  686. (unsigned long long)r->start,
  687. (unsigned long long)r->end, nm);
  688. }
  689. return p-buf;
  690. }
  691. #endif /* CONFIG_PROC_FS */
  692. /*
  693. * This is a version of find_resource and it belongs to kernel/resource.c.
  694. * Until we have agreement with Linus and Martin, it lingers here.
  695. *
  696. * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
  697. * This probably warrants some sort of hashing.
  698. */
  699. static struct resource *_sparc_find_resource(struct resource *root,
  700. unsigned long hit)
  701. {
  702. struct resource *tmp;
  703. for (tmp = root->child; tmp != 0; tmp = tmp->sibling) {
  704. if (tmp->start <= hit && tmp->end >= hit)
  705. return tmp;
  706. }
  707. return NULL;
  708. }
  709. static void register_proc_sparc_ioport(void)
  710. {
  711. #ifdef CONFIG_PROC_FS
  712. create_proc_read_entry("io_map",0,NULL,_sparc_io_get_info,&sparc_iomap);
  713. create_proc_read_entry("dvma_map",0,NULL,_sparc_io_get_info,&_sparc_dvma);
  714. #endif
  715. }