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/config.h>
  28. #include <linux/module.h>
  29. #include <linux/sched.h>
  30. #include <linux/kernel.h>
  31. #include <linux/errno.h>
  32. #include <linux/types.h>
  33. #include <linux/ioport.h>
  34. #include <linux/mm.h>
  35. #include <linux/slab.h>
  36. #include <linux/pci.h> /* struct pci_dev */
  37. #include <linux/proc_fs.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(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 lenghts 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 = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
  280. goto err_nomem;
  281. memset((char*)res, 0, sizeof(struct resource));
  282. if (allocate_resource(&_sparc_dvma, res, len_total,
  283. _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  284. printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
  285. goto err_nova;
  286. }
  287. mmu_inval_dma_area(va, len_total);
  288. // XXX The mmu_map_dma_area does this for us below, see comments.
  289. // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
  290. /*
  291. * XXX That's where sdev would be used. Currently we load
  292. * all iommu tables with the same translations.
  293. */
  294. if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0)
  295. goto err_noiommu;
  296. /* Set the resource name, if known. */
  297. if (sdev) {
  298. res->name = sdev->prom_name;
  299. }
  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 sbus_dev *sdev, 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 = mmu_translate_dvma(ba);
  333. mmu_unmap_dma_area(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 sbus_dev *sdev, void *va, size_t len, int direction)
  342. {
  343. /* XXX why are some lenghts 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(va, len, sdev->bus);
  352. }
  353. void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
  354. {
  355. mmu_release_scsi_one(ba, n, sdev->bus);
  356. }
  357. int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
  358. {
  359. mmu_get_scsi_sgl(sg, n, sdev->bus);
  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 sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
  367. {
  368. mmu_release_scsi_sgl(sg, n, sdev->bus);
  369. }
  370. /*
  371. */
  372. void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
  373. {
  374. #if 0
  375. unsigned long va;
  376. struct resource *res;
  377. /* We do not need the resource, just print a message if invalid. */
  378. res = _sparc_find_resource(&_sparc_dvma, ba);
  379. if (res == NULL)
  380. panic("sbus_dma_sync_single: 0x%x\n", ba);
  381. va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
  382. /*
  383. * XXX This bogosity will be fixed with the iommu rewrite coming soon
  384. * to a kernel near you. - Anton
  385. */
  386. /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
  387. #endif
  388. }
  389. void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
  390. {
  391. #if 0
  392. unsigned long va;
  393. struct resource *res;
  394. /* We do not need the resource, just print a message if invalid. */
  395. res = _sparc_find_resource(&_sparc_dvma, ba);
  396. if (res == NULL)
  397. panic("sbus_dma_sync_single: 0x%x\n", ba);
  398. va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
  399. /*
  400. * XXX This bogosity will be fixed with the iommu rewrite coming soon
  401. * to a kernel near you. - Anton
  402. */
  403. /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
  404. #endif
  405. }
  406. void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
  407. {
  408. printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n");
  409. }
  410. void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
  411. {
  412. printk("sbus_dma_sync_sg_for_device: not implemented yet\n");
  413. }
  414. /* Support code for sbus_init(). */
  415. /*
  416. * XXX This functions appears to be a distorted version of
  417. * prom_sbus_ranges_init(), with all sun4d stuff cut away.
  418. * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
  419. */
  420. /* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
  421. void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
  422. {
  423. int parent_node = pn->node;
  424. if (sparc_cpu_model == sun4d) {
  425. struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
  426. int num_iounit_ranges, len;
  427. len = prom_getproperty(parent_node, "ranges",
  428. (char *) iounit_ranges,
  429. sizeof (iounit_ranges));
  430. if (len != -1) {
  431. num_iounit_ranges =
  432. (len / sizeof(struct linux_prom_ranges));
  433. prom_adjust_ranges(sbus->sbus_ranges,
  434. sbus->num_sbus_ranges,
  435. iounit_ranges, num_iounit_ranges);
  436. }
  437. }
  438. }
  439. void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
  440. {
  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. }
  454. void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
  455. {
  456. if (sparc_cpu_model == sun4d) {
  457. struct device_node *parent = dp->parent;
  458. sbus->devid = of_getintprop_default(parent, "device-id", 0);
  459. sbus->board = of_getintprop_default(parent, "board#", 0);
  460. }
  461. }
  462. int __init sbus_arch_preinit(void)
  463. {
  464. extern void register_proc_sparc_ioport(void);
  465. register_proc_sparc_ioport();
  466. #ifdef CONFIG_SUN4
  467. {
  468. extern void sun4_dvma_init(void);
  469. sun4_dvma_init();
  470. }
  471. return 1;
  472. #else
  473. return 0;
  474. #endif
  475. }
  476. void __init sbus_arch_postinit(void)
  477. {
  478. if (sparc_cpu_model == sun4d) {
  479. extern void sun4d_init_sbi_irq(void);
  480. sun4d_init_sbi_irq();
  481. }
  482. }
  483. #endif /* CONFIG_SBUS */
  484. #ifdef CONFIG_PCI
  485. /* Allocate and map kernel buffer using consistent mode DMA for a device.
  486. * hwdev should be valid struct pci_dev pointer for PCI devices.
  487. */
  488. void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
  489. {
  490. unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
  491. unsigned long va;
  492. struct resource *res;
  493. int order;
  494. if (len == 0) {
  495. return NULL;
  496. }
  497. if (len > 256*1024) { /* __get_free_pages() limit */
  498. return NULL;
  499. }
  500. order = get_order(len_total);
  501. va = __get_free_pages(GFP_KERNEL, order);
  502. if (va == 0) {
  503. printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
  504. return NULL;
  505. }
  506. if ((res = kmalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
  507. free_pages(va, order);
  508. printk("pci_alloc_consistent: no core\n");
  509. return NULL;
  510. }
  511. memset((char*)res, 0, sizeof(struct resource));
  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 assosciated 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 *sg, int nents,
  623. int direction)
  624. {
  625. int n;
  626. BUG_ON(direction == PCI_DMA_NONE);
  627. /* IIep is write-through, not flushing. */
  628. for (n = 0; n < nents; n++) {
  629. BUG_ON(page_address(sg->page) == NULL);
  630. sg->dvma_address = virt_to_phys(page_address(sg->page));
  631. sg->dvma_length = sg->length;
  632. sg++;
  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 *sg, int nents,
  641. int direction)
  642. {
  643. int n;
  644. BUG_ON(direction == PCI_DMA_NONE);
  645. if (direction != PCI_DMA_TODEVICE) {
  646. for (n = 0; n < nents; n++) {
  647. BUG_ON(page_address(sg->page) == NULL);
  648. mmu_inval_dma_area(
  649. (unsigned long) page_address(sg->page),
  650. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  651. sg++;
  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 *sg, int nents, int direction)
  688. {
  689. int n;
  690. BUG_ON(direction == PCI_DMA_NONE);
  691. if (direction != PCI_DMA_TODEVICE) {
  692. for (n = 0; n < nents; n++) {
  693. BUG_ON(page_address(sg->page) == NULL);
  694. mmu_inval_dma_area(
  695. (unsigned long) page_address(sg->page),
  696. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  697. sg++;
  698. }
  699. }
  700. }
  701. void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
  702. {
  703. int n;
  704. BUG_ON(direction == PCI_DMA_NONE);
  705. if (direction != PCI_DMA_TODEVICE) {
  706. for (n = 0; n < nents; n++) {
  707. BUG_ON(page_address(sg->page) == NULL);
  708. mmu_inval_dma_area(
  709. (unsigned long) page_address(sg->page),
  710. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  711. sg++;
  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. }