ioport.c 23 KB

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