ioport.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ioport.c: Simple io mapping allocator.
  4. *
  5. * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
  6. * Copyright (C) 1995 Miguel de Icaza (miguel@nuclecu.unam.mx)
  7. *
  8. * 1996: sparc_free_io, 1999: ioremap()/iounmap() by Pete Zaitcev.
  9. *
  10. * 2000/01/29
  11. * <rth> zait: as long as pci_alloc_consistent produces something addressable,
  12. * things are ok.
  13. * <zaitcev> rth: no, it is relevant, because get_free_pages returns you a
  14. * pointer into the big page mapping
  15. * <rth> zait: so what?
  16. * <rth> zait: remap_it_my_way(virt_to_phys(get_free_page()))
  17. * <zaitcev> Hmm
  18. * <zaitcev> Suppose I did this remap_it_my_way(virt_to_phys(get_free_page())).
  19. * So far so good.
  20. * <zaitcev> Now, driver calls pci_free_consistent(with result of
  21. * remap_it_my_way()).
  22. * <zaitcev> How do you find the address to pass to free_pages()?
  23. * <rth> zait: walk the page tables? It's only two or three level after all.
  24. * <rth> zait: you have to walk them anyway to remove the mapping.
  25. * <zaitcev> Hmm
  26. * <zaitcev> Sounds reasonable
  27. */
  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 <linux/seq_file.h>
  39. #include <linux/scatterlist.h>
  40. #include <linux/dma-noncoherent.h>
  41. #include <linux/of_device.h>
  42. #include <asm/io.h>
  43. #include <asm/vaddrs.h>
  44. #include <asm/oplib.h>
  45. #include <asm/prom.h>
  46. #include <asm/page.h>
  47. #include <asm/pgalloc.h>
  48. #include <asm/dma.h>
  49. #include <asm/iommu.h>
  50. #include <asm/io-unit.h>
  51. #include <asm/leon.h>
  52. const struct sparc32_dma_ops *sparc32_dma_ops;
  53. /* This function must make sure that caches and memory are coherent after DMA
  54. * On LEON systems without cache snooping it flushes the entire D-CACHE.
  55. */
  56. static inline void dma_make_coherent(unsigned long pa, unsigned long len)
  57. {
  58. if (sparc_cpu_model == sparc_leon) {
  59. if (!sparc_leon3_snooping_enabled())
  60. leon_flush_dcache_all();
  61. }
  62. }
  63. static void __iomem *_sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz);
  64. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  65. unsigned long size, char *name);
  66. static void _sparc_free_io(struct resource *res);
  67. static void register_proc_sparc_ioport(void);
  68. /* This points to the next to use virtual memory for DVMA mappings */
  69. static struct resource _sparc_dvma = {
  70. .name = "sparc_dvma", .start = DVMA_VADDR, .end = DVMA_END - 1
  71. };
  72. /* This points to the start of I/O mappings, cluable from outside. */
  73. /*ext*/ struct resource sparc_iomap = {
  74. .name = "sparc_iomap", .start = IOBASE_VADDR, .end = IOBASE_END - 1
  75. };
  76. /*
  77. * Our mini-allocator...
  78. * Boy this is gross! We need it because we must map I/O for
  79. * timers and interrupt controller before the kmalloc is available.
  80. */
  81. #define XNMLN 15
  82. #define XNRES 10 /* SS-10 uses 8 */
  83. struct xresource {
  84. struct resource xres; /* Must be first */
  85. int xflag; /* 1 == used */
  86. char xname[XNMLN+1];
  87. };
  88. static struct xresource xresv[XNRES];
  89. static struct xresource *xres_alloc(void) {
  90. struct xresource *xrp;
  91. int n;
  92. xrp = xresv;
  93. for (n = 0; n < XNRES; n++) {
  94. if (xrp->xflag == 0) {
  95. xrp->xflag = 1;
  96. return xrp;
  97. }
  98. xrp++;
  99. }
  100. return NULL;
  101. }
  102. static void xres_free(struct xresource *xrp) {
  103. xrp->xflag = 0;
  104. }
  105. /*
  106. * These are typically used in PCI drivers
  107. * which are trying to be cross-platform.
  108. *
  109. * Bus type is always zero on IIep.
  110. */
  111. void __iomem *ioremap(phys_addr_t offset, size_t size)
  112. {
  113. char name[14];
  114. sprintf(name, "phys_%08x", (u32)offset);
  115. return _sparc_alloc_io(0, (unsigned long)offset, size, name);
  116. }
  117. EXPORT_SYMBOL(ioremap);
  118. /*
  119. * Complementary to ioremap().
  120. */
  121. void iounmap(volatile void __iomem *virtual)
  122. {
  123. unsigned long vaddr = (unsigned long) virtual & PAGE_MASK;
  124. struct resource *res;
  125. /*
  126. * XXX Too slow. Can have 8192 DVMA pages on sun4m in the worst case.
  127. * This probably warrants some sort of hashing.
  128. */
  129. if ((res = lookup_resource(&sparc_iomap, vaddr)) == NULL) {
  130. printk("free_io/iounmap: cannot free %lx\n", vaddr);
  131. return;
  132. }
  133. _sparc_free_io(res);
  134. if ((char *)res >= (char*)xresv && (char *)res < (char *)&xresv[XNRES]) {
  135. xres_free((struct xresource *)res);
  136. } else {
  137. kfree(res);
  138. }
  139. }
  140. EXPORT_SYMBOL(iounmap);
  141. void __iomem *of_ioremap(struct resource *res, unsigned long offset,
  142. unsigned long size, char *name)
  143. {
  144. return _sparc_alloc_io(res->flags & 0xF,
  145. res->start + offset,
  146. size, name);
  147. }
  148. EXPORT_SYMBOL(of_ioremap);
  149. void of_iounmap(struct resource *res, void __iomem *base, unsigned long size)
  150. {
  151. iounmap(base);
  152. }
  153. EXPORT_SYMBOL(of_iounmap);
  154. /*
  155. * Meat of mapping
  156. */
  157. static void __iomem *_sparc_alloc_io(unsigned int busno, unsigned long phys,
  158. unsigned long size, char *name)
  159. {
  160. static int printed_full;
  161. struct xresource *xres;
  162. struct resource *res;
  163. char *tack;
  164. int tlen;
  165. void __iomem *va; /* P3 diag */
  166. if (name == NULL) name = "???";
  167. if ((xres = xres_alloc()) != NULL) {
  168. tack = xres->xname;
  169. res = &xres->xres;
  170. } else {
  171. if (!printed_full) {
  172. printk("ioremap: done with statics, switching to malloc\n");
  173. printed_full = 1;
  174. }
  175. tlen = strlen(name);
  176. tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
  177. if (tack == NULL) return NULL;
  178. memset(tack, 0, sizeof(struct resource));
  179. res = (struct resource *) tack;
  180. tack += sizeof (struct resource);
  181. }
  182. strlcpy(tack, name, XNMLN+1);
  183. res->name = tack;
  184. va = _sparc_ioremap(res, busno, phys, size);
  185. /* printk("ioremap(0x%x:%08lx[0x%lx])=%p\n", busno, phys, size, va); */ /* P3 diag */
  186. return va;
  187. }
  188. /*
  189. */
  190. static void __iomem *
  191. _sparc_ioremap(struct resource *res, u32 bus, u32 pa, int sz)
  192. {
  193. unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
  194. if (allocate_resource(&sparc_iomap, res,
  195. (offset + sz + PAGE_SIZE-1) & PAGE_MASK,
  196. sparc_iomap.start, sparc_iomap.end, PAGE_SIZE, NULL, NULL) != 0) {
  197. /* Usually we cannot see printks in this case. */
  198. prom_printf("alloc_io_res(%s): cannot occupy\n",
  199. (res->name != NULL)? res->name: "???");
  200. prom_halt();
  201. }
  202. pa &= PAGE_MASK;
  203. srmmu_mapiorange(bus, pa, res->start, resource_size(res));
  204. return (void __iomem *)(unsigned long)(res->start + offset);
  205. }
  206. /*
  207. * Complementary to _sparc_ioremap().
  208. */
  209. static void _sparc_free_io(struct resource *res)
  210. {
  211. unsigned long plen;
  212. plen = resource_size(res);
  213. BUG_ON((plen & (PAGE_SIZE-1)) != 0);
  214. srmmu_unmapiorange(res->start, plen);
  215. release_resource(res);
  216. }
  217. #ifdef CONFIG_SBUS
  218. void sbus_set_sbus64(struct device *dev, int x)
  219. {
  220. printk("sbus_set_sbus64: unsupported\n");
  221. }
  222. EXPORT_SYMBOL(sbus_set_sbus64);
  223. /*
  224. * Allocate a chunk of memory suitable for DMA.
  225. * Typically devices use them for control blocks.
  226. * CPU may access them without any explicit flushing.
  227. */
  228. static void *sbus_alloc_coherent(struct device *dev, size_t len,
  229. dma_addr_t *dma_addrp, gfp_t gfp,
  230. unsigned long attrs)
  231. {
  232. struct platform_device *op = to_platform_device(dev);
  233. unsigned long len_total = PAGE_ALIGN(len);
  234. unsigned long va;
  235. struct resource *res;
  236. int order;
  237. /* XXX why are some lengths signed, others unsigned? */
  238. if (len <= 0) {
  239. return NULL;
  240. }
  241. /* XXX So what is maxphys for us and how do drivers know it? */
  242. if (len > 256*1024) { /* __get_free_pages() limit */
  243. return NULL;
  244. }
  245. order = get_order(len_total);
  246. va = __get_free_pages(gfp, order);
  247. if (va == 0)
  248. goto err_nopages;
  249. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL)
  250. goto err_nomem;
  251. if (allocate_resource(&_sparc_dvma, res, len_total,
  252. _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  253. printk("sbus_alloc_consistent: cannot occupy 0x%lx", len_total);
  254. goto err_nova;
  255. }
  256. // XXX The sbus_map_dma_area does this for us below, see comments.
  257. // srmmu_mapiorange(0, virt_to_phys(va), res->start, len_total);
  258. /*
  259. * XXX That's where sdev would be used. Currently we load
  260. * all iommu tables with the same translations.
  261. */
  262. if (sbus_map_dma_area(dev, dma_addrp, va, res->start, len_total) != 0)
  263. goto err_noiommu;
  264. res->name = op->dev.of_node->name;
  265. return (void *)(unsigned long)res->start;
  266. err_noiommu:
  267. release_resource(res);
  268. err_nova:
  269. kfree(res);
  270. err_nomem:
  271. free_pages(va, order);
  272. err_nopages:
  273. return NULL;
  274. }
  275. static void sbus_free_coherent(struct device *dev, size_t n, void *p,
  276. dma_addr_t ba, unsigned long attrs)
  277. {
  278. struct resource *res;
  279. struct page *pgv;
  280. if ((res = lookup_resource(&_sparc_dvma,
  281. (unsigned long)p)) == NULL) {
  282. printk("sbus_free_consistent: cannot free %p\n", p);
  283. return;
  284. }
  285. if (((unsigned long)p & (PAGE_SIZE-1)) != 0) {
  286. printk("sbus_free_consistent: unaligned va %p\n", p);
  287. return;
  288. }
  289. n = PAGE_ALIGN(n);
  290. if (resource_size(res) != n) {
  291. printk("sbus_free_consistent: region 0x%lx asked 0x%zx\n",
  292. (long)resource_size(res), n);
  293. return;
  294. }
  295. release_resource(res);
  296. kfree(res);
  297. pgv = virt_to_page(p);
  298. sbus_unmap_dma_area(dev, ba, n);
  299. __free_pages(pgv, get_order(n));
  300. }
  301. /*
  302. * Map a chunk of memory so that devices can see it.
  303. * CPU view of this memory may be inconsistent with
  304. * a device view and explicit flushing is necessary.
  305. */
  306. static dma_addr_t sbus_map_page(struct device *dev, struct page *page,
  307. unsigned long offset, size_t len,
  308. enum dma_data_direction dir,
  309. unsigned long attrs)
  310. {
  311. void *va = page_address(page) + offset;
  312. /* XXX why are some lengths signed, others unsigned? */
  313. if (len <= 0) {
  314. return 0;
  315. }
  316. /* XXX So what is maxphys for us and how do drivers know it? */
  317. if (len > 256*1024) { /* __get_free_pages() limit */
  318. return 0;
  319. }
  320. return mmu_get_scsi_one(dev, va, len);
  321. }
  322. static void sbus_unmap_page(struct device *dev, dma_addr_t ba, size_t n,
  323. enum dma_data_direction dir, unsigned long attrs)
  324. {
  325. mmu_release_scsi_one(dev, ba, n);
  326. }
  327. static int sbus_map_sg(struct device *dev, struct scatterlist *sg, int n,
  328. enum dma_data_direction dir, unsigned long attrs)
  329. {
  330. mmu_get_scsi_sgl(dev, sg, n);
  331. return n;
  332. }
  333. static void sbus_unmap_sg(struct device *dev, struct scatterlist *sg, int n,
  334. enum dma_data_direction dir, unsigned long attrs)
  335. {
  336. mmu_release_scsi_sgl(dev, sg, n);
  337. }
  338. static void sbus_sync_sg_for_cpu(struct device *dev, struct scatterlist *sg,
  339. int n, enum dma_data_direction dir)
  340. {
  341. BUG();
  342. }
  343. static void sbus_sync_sg_for_device(struct device *dev, struct scatterlist *sg,
  344. int n, enum dma_data_direction dir)
  345. {
  346. BUG();
  347. }
  348. static int sbus_dma_supported(struct device *dev, u64 mask)
  349. {
  350. return 0;
  351. }
  352. static const struct dma_map_ops sbus_dma_ops = {
  353. .alloc = sbus_alloc_coherent,
  354. .free = sbus_free_coherent,
  355. .map_page = sbus_map_page,
  356. .unmap_page = sbus_unmap_page,
  357. .map_sg = sbus_map_sg,
  358. .unmap_sg = sbus_unmap_sg,
  359. .sync_sg_for_cpu = sbus_sync_sg_for_cpu,
  360. .sync_sg_for_device = sbus_sync_sg_for_device,
  361. .dma_supported = sbus_dma_supported,
  362. };
  363. static int __init sparc_register_ioport(void)
  364. {
  365. register_proc_sparc_ioport();
  366. return 0;
  367. }
  368. arch_initcall(sparc_register_ioport);
  369. #endif /* CONFIG_SBUS */
  370. /* Allocate and map kernel buffer using consistent mode DMA for a device.
  371. * hwdev should be valid struct pci_dev pointer for PCI devices.
  372. */
  373. void *arch_dma_alloc(struct device *dev, size_t size, dma_addr_t *dma_handle,
  374. gfp_t gfp, unsigned long attrs)
  375. {
  376. unsigned long len_total = PAGE_ALIGN(size);
  377. void *va;
  378. struct resource *res;
  379. int order;
  380. if (size == 0) {
  381. return NULL;
  382. }
  383. if (size > 256*1024) { /* __get_free_pages() limit */
  384. return NULL;
  385. }
  386. order = get_order(len_total);
  387. va = (void *) __get_free_pages(gfp, order);
  388. if (va == NULL) {
  389. printk("%s: no %ld pages\n", __func__, len_total>>PAGE_SHIFT);
  390. goto err_nopages;
  391. }
  392. if ((res = kzalloc(sizeof(struct resource), GFP_KERNEL)) == NULL) {
  393. printk("%s: no core\n", __func__);
  394. goto err_nomem;
  395. }
  396. if (allocate_resource(&_sparc_dvma, res, len_total,
  397. _sparc_dvma.start, _sparc_dvma.end, PAGE_SIZE, NULL, NULL) != 0) {
  398. printk("%s: cannot occupy 0x%lx", __func__, len_total);
  399. goto err_nova;
  400. }
  401. srmmu_mapiorange(0, virt_to_phys(va), res->start, len_total);
  402. *dma_handle = virt_to_phys(va);
  403. return (void *) res->start;
  404. err_nova:
  405. kfree(res);
  406. err_nomem:
  407. free_pages((unsigned long)va, order);
  408. err_nopages:
  409. return NULL;
  410. }
  411. /* Free and unmap a consistent DMA buffer.
  412. * cpu_addr is what was returned arch_dma_alloc, size must be the same as what
  413. * was passed into arch_dma_alloc, and likewise dma_addr must be the same as
  414. * what *dma_ndler was set to.
  415. *
  416. * References to the memory and mappings associated with cpu_addr/dma_addr
  417. * past this call are illegal.
  418. */
  419. void arch_dma_free(struct device *dev, size_t size, void *cpu_addr,
  420. dma_addr_t dma_addr, unsigned long attrs)
  421. {
  422. struct resource *res;
  423. if ((res = lookup_resource(&_sparc_dvma,
  424. (unsigned long)cpu_addr)) == NULL) {
  425. printk("%s: cannot free %p\n", __func__, cpu_addr);
  426. return;
  427. }
  428. if (((unsigned long)cpu_addr & (PAGE_SIZE-1)) != 0) {
  429. printk("%s: unaligned va %p\n", __func__, cpu_addr);
  430. return;
  431. }
  432. size = PAGE_ALIGN(size);
  433. if (resource_size(res) != size) {
  434. printk("%s: region 0x%lx asked 0x%zx\n", __func__,
  435. (long)resource_size(res), size);
  436. return;
  437. }
  438. dma_make_coherent(dma_addr, size);
  439. srmmu_unmapiorange((unsigned long)cpu_addr, size);
  440. release_resource(res);
  441. kfree(res);
  442. free_pages((unsigned long)phys_to_virt(dma_addr), get_order(size));
  443. }
  444. /* IIep is write-through, not flushing on cpu to device transfer. */
  445. void arch_sync_dma_for_cpu(struct device *dev, phys_addr_t paddr,
  446. size_t size, enum dma_data_direction dir)
  447. {
  448. if (dir != PCI_DMA_TODEVICE)
  449. dma_make_coherent(paddr, PAGE_ALIGN(size));
  450. }
  451. const struct dma_map_ops *dma_ops = &sbus_dma_ops;
  452. EXPORT_SYMBOL(dma_ops);
  453. #ifdef CONFIG_PROC_FS
  454. static int sparc_io_proc_show(struct seq_file *m, void *v)
  455. {
  456. struct resource *root = m->private, *r;
  457. const char *nm;
  458. for (r = root->child; r != NULL; r = r->sibling) {
  459. if ((nm = r->name) == NULL) nm = "???";
  460. seq_printf(m, "%016llx-%016llx: %s\n",
  461. (unsigned long long)r->start,
  462. (unsigned long long)r->end, nm);
  463. }
  464. return 0;
  465. }
  466. #endif /* CONFIG_PROC_FS */
  467. static void register_proc_sparc_ioport(void)
  468. {
  469. #ifdef CONFIG_PROC_FS
  470. proc_create_single_data("io_map", 0, NULL, sparc_io_proc_show,
  471. &sparc_iomap);
  472. proc_create_single_data("dvma_map", 0, NULL, sparc_io_proc_show,
  473. &_sparc_dvma);
  474. #endif
  475. }