ioport.c 23 KB

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