ioport.c 23 KB

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