ioport.c 23 KB

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