ioport.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /*
  2. * ioport.c: Simple io mapping allocator.
  3. *
  4. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  5. * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
  6. *
  7. * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
  8. *
  9. * 2000/01/29
  10. * <rth> zait: as long as pci_alloc_consistent produces something addressable,
  11. * things are ok.
  12. * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a
  13. * pointer into the big page mapping
  14. * <rth> zait: so what?
  15. * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page()))
  16. * <zaitcev> Hmm
  17. * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
  18. * So far so good.
  19. * <zaitcev> Now, driver calls pci_free_consistent(with result of
  20. * remap_it_my_way()).
  21. * <zaitcev> How do you find the address to pass to free_pages()?
  22. * <rth> zait: walk the page tables? It's only two or three level after all.
  23. * <rth> zait: you have to walk them anyway to remove the mapping.
  24. * <zaitcev> Hmm
  25. * <zaitcev> Sounds reasonable
  26. */
  27. #include <linux/module.h>
  28. #include <linux/sched.h>
  29. #include <linux/kernel.h>
  30. #include <linux/errno.h>
  31. #include <linux/types.h>
  32. #include <linux/ioport.h>
  33. #include <linux/mm.h>
  34. #include <linux/slab.h>
  35. #include <linux/pci.h> /* struct pci_dev */
  36. #include <linux/proc_fs.h>
  37. #include <linux/scatterlist.h>
  38. #include <linux/of_device.h>
  39. #include <asm/io.h>
  40. #include <asm/vaddrs.h>
  41. #include <asm/oplib.h>
  42. #include <asm/prom.h>
  43. #include <asm/sbus.h>
  44. #include <asm/page.h>
  45. #include <asm/pgalloc.h>
  46. #include <asm/dma.h>
  47. #define mmu_inval_dma_area(p, l) /* Anton pulled it out for 2.4.0-xx */
  48. static struct resource *_sparc_find_resource(struct resource *r,
  49. unsigned long);
  50. static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
  51. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  52. unsigned long size, char *name);
  53. static void _sparc_free_io(struct resource *res);
  54. static void register_proc_sparc_ioport(void);
  55. /* This points to the next to use virtual memory for DVMA mappings */
  56. static struct resource _sparc_dvma = {
  57. .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
  58. };
  59. /* This points to the start of I/O mappings, cluable from outside. */
  60. /*ext*/ struct resource sparc_iomap = {
  61. .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
  62. };
  63. /*
  64. * Our mini-allocator...
  65. * Boy this is gross! We need it because we must map I/O for
  66. * timers and interrupt controller before the kmalloc is available.
  67. */
  68. #define XNMLN 15
  69. #define XNRES 10 /* SS-10 uses 8 */
  70. struct xresource {
  71. struct resource xres; /* Must be first */
  72. int xflag; /* 1 == used */
  73. char xname[XNMLN+1];
  74. };
  75. static struct xresource xresv[XNRES];
  76. static struct xresource *xres_alloc(void) {
  77. struct xresource *xrp;
  78. int n;
  79. xrp = xresv;
  80. for (n = 0; n < XNRES; n++) {
  81. if (xrp->xflag == 0) {
  82. xrp->xflag = 1;
  83. return xrp;
  84. }
  85. xrp++;
  86. }
  87. return NULL;
  88. }
  89. static void xres_free(struct xresource *xrp) {
  90. xrp->xflag = 0;
  91. }
  92. /*
  93. * These are typically used in PCI drivers
  94. * which are trying to be cross-platform.
  95. *
  96. * Bus type is always zero on IIep.
  97. */
  98. void __iomem *ioremap(unsigned long offset, unsigned long size)
  99. {
  100. char name[14];
  101. sprintf(name, "phys_%08x", (u32)offset);
  102. return _sparc_alloc_io(0, offset, size, name);
  103. }
  104. /*
  105. * Comlimentary to ioremap().
  106. */
  107. void iounmap(volatile void __iomem *virtual)
  108. {
  109. unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
  110. struct resource *res;
  111. if ((res = _sparc_find_resource(&sparc_iomap, vaddr)) == NULL) {
  112. printk("free_io/iounmap: cannot free %lx\n", vaddr);
  113. return;
  114. }
  115. _sparc_free_io(res);
  116. if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
  117. xres_free((struct xresource *)res);
  118. } else {
  119. kfree(res);
  120. }
  121. }
  122. /*
  123. */
  124. void __iomem *sbus_ioremap(struct resource *phyres, unsigned long offset,
  125. unsigned long size, char *name)
  126. {
  127. return _sparc_alloc_io(phyres->flags & 0xF,
  128. phyres->start + offset, size, name);
  129. }
  130. void __iomem *of_ioremap(struct resource *res, unsigned long offset,
  131. unsigned long size, char *name)
  132. {
  133. return _sparc_alloc_io(res->flags & 0xF,
  134. res->start + offset,
  135. size, name);
  136. }
  137. EXPORT_SYMBOL(of_ioremap);
  138. void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
  139. {
  140. iounmap(base);
  141. }
  142. EXPORT_SYMBOL(of_iounmap);
  143. /*
  144. */
  145. void sbus_iounmap(volatile void __iomem *addr, unsigned long size)
  146. {
  147. iounmap(addr);
  148. }
  149. /*
  150. * Meat of mapping
  151. */
  152. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  153. unsigned long size, char *name)
  154. {
  155. static int printed_full;
  156. struct xresource *xres;
  157. struct resource *res;
  158. char *tack;
  159. int tlen;
  160. void __iomem *va; /* P3 diag */
  161. if (name == NULL) name = "???";
  162. if ((xres = xres_alloc()) != 0) {
  163. tack = xres->xname;
  164. res = &xres->xres;
  165. } else {
  166. if (!printed_full) {
  167. printk("ioremap: done with statics, switching to malloc\n");
  168. printed_full = 1;
  169. }
  170. tlen = strlen(name);
  171. tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
  172. if (tack == NULL) return NULL;
  173. memset(tack, 0, sizeof(struct resource));
  174. res = (struct resource *) tack;
  175. tack += sizeof (struct resource);
  176. }
  177. strlcpy(tack, name, XNMLN+1);
  178. res->name = tack;
  179. va = _sparc_ioremap(res, busno, phys, size);
  180. /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
  181. return va;
  182. }
  183. /*
  184. */
  185. static void __iomem *
  186. _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
  187. {
  188. unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
  189. if (allocate_resource(&sparc_iomap, res,
  190. (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
  191. sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
  192. /* Usually we cannot see printks in this case. */
  193. prom_printf("alloc_io_res(%s): cannot occupy\n",
  194. (res->name != NULL)? res->name: "???");
  195. prom_halt();
  196. }
  197. pa &= PAGE_MASK;
  198. sparc_mapiorange(bus, pa, res->start, res->end - res->start + 1);
  199. return (void __iomem *)(unsigned long)(res->start + offset);
  200. }
  201. /*
  202. * Comlimentary to _sparc_ioremap().
  203. */
  204. static void _sparc_free_io(struct resource *res)
  205. {
  206. unsigned long plen;
  207. plen = res->end - res->start + 1;
  208. BUG_ON((plen & (PAGE_SIZE-1)) != 0);
  209. sparc_unmapiorange(res->start, plen);
  210. release_resource(res);
  211. }
  212. #ifdef CONFIG_SBUS
  213. void sbus_set_sbus64(struct sbus_dev *sdev, int x)
  214. {
  215. printk("sbus_set_sbus64: unsupported\n");
  216. }
  217. extern unsigned int sun4d_build_irq(struct sbus_dev *sdev, int irq);
  218. void __init sbus_fill_device_irq(struct sbus_dev *sdev)
  219. {
  220. struct linux_prom_irqs irqs[PROMINTR_MAX];
  221. int len;
  222. len = prom_getproperty(sdev->prom_node, "intr",
  223. (char *)irqs, sizeof(irqs));
  224. if (len != -1) {
  225. sdev->num_irqs = len / 8;
  226. if (sdev->num_irqs == 0) {
  227. sdev->irqs[0] = 0;
  228. } else if (sparc_cpu_model == sun4d) {
  229. for (len = 0; len < sdev->num_irqs; len++)
  230. sdev->irqs[len] =
  231. sun4d_build_irq(sdev, irqs[len].pri);
  232. } else {
  233. for (len = 0; len < sdev->num_irqs; len++)
  234. sdev->irqs[len] = irqs[len].pri;
  235. }
  236. } else {
  237. int interrupts[PROMINTR_MAX];
  238. /* No "intr" node found-- check for "interrupts" node.
  239. * This node contains SBus interrupt levels, not IPLs
  240. * as in "intr", and no vector values. We convert
  241. * SBus interrupt levels to PILs (platform specific).
  242. */
  243. len = prom_getproperty(sdev->prom_node, "interrupts",
  244. (char *)interrupts, sizeof(interrupts));
  245. if (len == -1) {
  246. sdev->irqs[0] = 0;
  247. sdev->num_irqs = 0;
  248. } else {
  249. sdev->num_irqs = len / sizeof(int);
  250. for (len = 0; len < sdev->num_irqs; len++) {
  251. sdev->irqs[len] =
  252. sbint_to_irq(sdev, interrupts[len]);
  253. }
  254. }
  255. }
  256. }
  257. /*
  258. * Allocate a chunk of memory suitable for DMA.
  259. * Typically devices use them for control blocks.
  260. * CPU may access them without any explicit flushing.
  261. *
  262. * XXX Some clever people know that sdev is not used and supply NULL. Watch.
  263. */
  264. void *sbus_alloc_consistent(struct sbus_dev *sdev, long len, u32 *dma_addrp)
  265. {
  266. unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
  267. unsigned long va;
  268. struct resource *res;
  269. int order;
  270. /* XXX why are some lengths signed, others unsigned? */
  271. if (len <= 0) {
  272. return NULL;
  273. }
  274. /* XXX So what is maxphys for us and how do drivers know it? */
  275. if (len > 256*1024) { /* __get_free_pages() limit */
  276. return NULL;
  277. }
  278. order = get_order(len_total);
  279. if ((va = __get_free_pages(GFP_KERNEL|__GFP_COMP, order)) == 0)
  280. goto err_nopages;
  281. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
  282. goto err_nomem;
  283. if (allocate_resource(&_sparc_dvma, res, len_total,
  284. _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  285. printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
  286. goto err_nova;
  287. }
  288. mmu_inval_dma_area(va, len_total);
  289. // XXX The mmu_map_dma_area does this for us below, see comments.
  290. // sparc_mapiorange(0, virt_to_phys(va), res->start, len_total);
  291. /*
  292. * XXX That's where sdev would be used. Currently we load
  293. * all iommu tables with the same translations.
  294. */
  295. if (mmu_map_dma_area(dma_addrp, va, res->start, len_total) != 0)
  296. goto err_noiommu;
  297. /* Set the resource name, if known. */
  298. if (sdev) {
  299. res->name = sdev->prom_name;
  300. }
  301. return (void *)(unsigned long)res->start;
  302. err_noiommu:
  303. release_resource(res);
  304. err_nova:
  305. free_pages(va, order);
  306. err_nomem:
  307. kfree(res);
  308. err_nopages:
  309. return NULL;
  310. }
  311. void sbus_free_consistent(struct sbus_dev *sdev, long n, void *p, u32 ba)
  312. {
  313. struct resource *res;
  314. struct page *pgv;
  315. if ((res = _sparc_find_resource(&_sparc_dvma,
  316. (unsigned long)p)) == NULL) {
  317. printk("sbus_free_consistent: cannot free %p\n", p);
  318. return;
  319. }
  320. if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
  321. printk("sbus_free_consistent: unaligned va %p\n", p);
  322. return;
  323. }
  324. n = (n + PAGE_SIZE-1) & PAGE_MASK;
  325. if ((res->end-res->start)+1 != n) {
  326. printk("sbus_free_consistent: region 0x%lx asked 0x%lx\n",
  327. (long)((res->end-res->start)+1), n);
  328. return;
  329. }
  330. release_resource(res);
  331. kfree(res);
  332. /* mmu_inval_dma_area(va, n); */ /* it's consistent, isn't it */
  333. pgv = mmu_translate_dvma(ba);
  334. mmu_unmap_dma_area(ba, n);
  335. __free_pages(pgv, get_order(n));
  336. }
  337. /*
  338. * Map a chunk of memory so that devices can see it.
  339. * CPU view of this memory may be inconsistent with
  340. * a device view and explicit flushing is necessary.
  341. */
  342. dma_addr_t sbus_map_single(struct sbus_dev *sdev, void *va, size_t len, int direction)
  343. {
  344. /* XXX why are some lengths signed, others unsigned? */
  345. if (len <= 0) {
  346. return 0;
  347. }
  348. /* XXX So what is maxphys for us and how do drivers know it? */
  349. if (len > 256*1024) { /* __get_free_pages() limit */
  350. return 0;
  351. }
  352. return mmu_get_scsi_one(va, len, sdev->bus);
  353. }
  354. void sbus_unmap_single(struct sbus_dev *sdev, dma_addr_t ba, size_t n, int direction)
  355. {
  356. mmu_release_scsi_one(ba, n, sdev->bus);
  357. }
  358. int sbus_map_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
  359. {
  360. mmu_get_scsi_sgl(sg, n, sdev->bus);
  361. /*
  362. * XXX sparc64 can return a partial length here. sun4c should do this
  363. * but it currently panics if it can't fulfill the request - Anton
  364. */
  365. return n;
  366. }
  367. void sbus_unmap_sg(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
  368. {
  369. mmu_release_scsi_sgl(sg, n, sdev->bus);
  370. }
  371. /*
  372. */
  373. void sbus_dma_sync_single_for_cpu(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
  374. {
  375. #if 0
  376. unsigned long va;
  377. struct resource *res;
  378. /* We do not need the resource, just print a message if invalid. */
  379. res = _sparc_find_resource(&_sparc_dvma, ba);
  380. if (res == NULL)
  381. panic("sbus_dma_sync_single: 0x%x\n", ba);
  382. va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
  383. /*
  384. * XXX This bogosity will be fixed with the iommu rewrite coming soon
  385. * to a kernel near you. - Anton
  386. */
  387. /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
  388. #endif
  389. }
  390. void sbus_dma_sync_single_for_device(struct sbus_dev *sdev, dma_addr_t ba, size_t size, int direction)
  391. {
  392. #if 0
  393. unsigned long va;
  394. struct resource *res;
  395. /* We do not need the resource, just print a message if invalid. */
  396. res = _sparc_find_resource(&_sparc_dvma, ba);
  397. if (res == NULL)
  398. panic("sbus_dma_sync_single: 0x%x\n", ba);
  399. va = page_address(mmu_translate_dvma(ba)); /* XXX higmem */
  400. /*
  401. * XXX This bogosity will be fixed with the iommu rewrite coming soon
  402. * to a kernel near you. - Anton
  403. */
  404. /* mmu_inval_dma_area(va, (size + PAGE_SIZE-1) & PAGE_MASK); */
  405. #endif
  406. }
  407. void sbus_dma_sync_sg_for_cpu(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
  408. {
  409. printk("sbus_dma_sync_sg_for_cpu: not implemented yet\n");
  410. }
  411. void sbus_dma_sync_sg_for_device(struct sbus_dev *sdev, struct scatterlist *sg, int n, int direction)
  412. {
  413. printk("sbus_dma_sync_sg_for_device: not implemented yet\n");
  414. }
  415. /* Support code for sbus_init(). */
  416. /*
  417. * XXX This functions appears to be a distorted version of
  418. * prom_sbus_ranges_init(), with all sun4d stuff cut away.
  419. * Ask DaveM what is going on here, how is sun4d supposed to work... XXX
  420. */
  421. /* added back sun4d patch from Thomas Bogendoerfer - should be OK (crn) */
  422. void __init sbus_arch_bus_ranges_init(struct device_node *pn, struct sbus_bus *sbus)
  423. {
  424. int parent_node = pn->node;
  425. if (sparc_cpu_model == sun4d) {
  426. struct linux_prom_ranges iounit_ranges[PROMREG_MAX];
  427. int num_iounit_ranges, len;
  428. len = prom_getproperty(parent_node, "ranges",
  429. (char *) iounit_ranges,
  430. sizeof (iounit_ranges));
  431. if (len != -1) {
  432. num_iounit_ranges =
  433. (len / sizeof(struct linux_prom_ranges));
  434. prom_adjust_ranges(sbus->sbus_ranges,
  435. sbus->num_sbus_ranges,
  436. iounit_ranges, num_iounit_ranges);
  437. }
  438. }
  439. }
  440. void __init sbus_setup_iommu(struct sbus_bus *sbus, struct device_node *dp)
  441. {
  442. #ifndef CONFIG_SUN4
  443. struct device_node *parent = dp->parent;
  444. if (sparc_cpu_model != sun4d &&
  445. parent != NULL &&
  446. !strcmp(parent->name, "iommu")) {
  447. extern void iommu_init(int iommu_node, struct sbus_bus *sbus);
  448. iommu_init(parent->node, sbus);
  449. }
  450. if (sparc_cpu_model == sun4d) {
  451. extern void iounit_init(int sbi_node, int iounit_node,
  452. struct sbus_bus *sbus);
  453. iounit_init(dp->node, parent->node, sbus);
  454. }
  455. #endif
  456. }
  457. void __init sbus_setup_arch_props(struct sbus_bus *sbus, struct device_node *dp)
  458. {
  459. if (sparc_cpu_model == sun4d) {
  460. struct device_node *parent = dp->parent;
  461. sbus->devid = of_getintprop_default(parent, "device-id", 0);
  462. sbus->board = of_getintprop_default(parent, "board#", 0);
  463. }
  464. }
  465. int __init sbus_arch_preinit(void)
  466. {
  467. register_proc_sparc_ioport();
  468. #ifdef CONFIG_SUN4
  469. {
  470. extern void sun4_dvma_init(void);
  471. sun4_dvma_init();
  472. }
  473. return 1;
  474. #else
  475. return 0;
  476. #endif
  477. }
  478. void __init sbus_arch_postinit(void)
  479. {
  480. if (sparc_cpu_model == sun4d) {
  481. extern void sun4d_init_sbi_irq(void);
  482. sun4d_init_sbi_irq();
  483. }
  484. }
  485. #endif /* CONFIG_SBUS */
  486. #ifdef CONFIG_PCI
  487. /* Allocate and map kernel buffer using consistent mode DMA for a device.
  488. * hwdev should be valid struct pci_dev pointer for PCI devices.
  489. */
  490. void *pci_alloc_consistent(struct pci_dev *pdev, size_t len, dma_addr_t *pba)
  491. {
  492. unsigned long len_total = (len + PAGE_SIZE-1) & PAGE_MASK;
  493. unsigned long va;
  494. struct resource *res;
  495. int order;
  496. if (len == 0) {
  497. return NULL;
  498. }
  499. if (len > 256*1024) { /* __get_free_pages() limit */
  500. return NULL;
  501. }
  502. order = get_order(len_total);
  503. va = __get_free_pages(GFP_KERNEL, order);
  504. if (va == 0) {
  505. printk("pci_alloc_consistent: no %ld pages\n", len_total>>PAGE_SHIFT);
  506. return NULL;
  507. }
  508. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
  509. free_pages(va, order);
  510. printk("pci_alloc_consistent: no core\n");
  511. return NULL;
  512. }
  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 associated 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 *sgl, int nents,
  624. int direction)
  625. {
  626. struct scatterlist *sg;
  627. int n;
  628. BUG_ON(direction == PCI_DMA_NONE);
  629. /* IIep is write-through, not flushing. */
  630. for_each_sg(sgl, sg, nents, n) {
  631. BUG_ON(page_address(sg_page(sg)) == NULL);
  632. sg->dvma_address = virt_to_phys(sg_virt(sg));
  633. sg->dvma_length = sg->length;
  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 *sgl, int nents,
  642. int direction)
  643. {
  644. struct scatterlist *sg;
  645. int n;
  646. BUG_ON(direction == PCI_DMA_NONE);
  647. if (direction != PCI_DMA_TODEVICE) {
  648. for_each_sg(sgl, sg, nents, n) {
  649. BUG_ON(page_address(sg_page(sg)) == NULL);
  650. mmu_inval_dma_area(
  651. (unsigned long) page_address(sg_page(sg)),
  652. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  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 *sgl, int nents, int direction)
  689. {
  690. struct scatterlist *sg;
  691. int n;
  692. BUG_ON(direction == PCI_DMA_NONE);
  693. if (direction != PCI_DMA_TODEVICE) {
  694. for_each_sg(sgl, sg, nents, n) {
  695. BUG_ON(page_address(sg_page(sg)) == NULL);
  696. mmu_inval_dma_area(
  697. (unsigned long) page_address(sg_page(sg)),
  698. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  699. }
  700. }
  701. }
  702. void pci_dma_sync_sg_for_device(struct pci_dev *hwdev, struct scatterlist *sgl, int nents, int direction)
  703. {
  704. struct scatterlist *sg;
  705. int n;
  706. BUG_ON(direction == PCI_DMA_NONE);
  707. if (direction != PCI_DMA_TODEVICE) {
  708. for_each_sg(sgl, sg, nents, n) {
  709. BUG_ON(page_address(sg_page(sg)) == NULL);
  710. mmu_inval_dma_area(
  711. (unsigned long) page_address(sg_page(sg)),
  712. (sg->length + PAGE_SIZE-1) & PAGE_MASK);
  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. static struct resource *_sparc_find_resource(struct resource *root,
  744. 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. static 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. }