ioremap_64.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /*
  2. * arch/sh/mm/ioremap_64.c
  3. *
  4. * Copyright (C) 2000, 2001 Paolo Alberelli
  5. * Copyright (C) 2003 - 2007 Paul Mundt
  6. *
  7. * Mostly derived from arch/sh/mm/ioremap.c which, in turn is mostly
  8. * derived from arch/i386/mm/ioremap.c .
  9. *
  10. * (C) Copyright 1995 1996 Linus Torvalds
  11. *
  12. * This file is subject to the terms and conditions of the GNU General Public
  13. * License. See the file "COPYING" in the main directory of this archive
  14. * for more details.
  15. */
  16. #include <linux/vmalloc.h>
  17. #include <linux/ioport.h>
  18. #include <linux/module.h>
  19. #include <linux/mm.h>
  20. #include <linux/io.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/proc_fs.h>
  23. #include <linux/slab.h>
  24. #include <asm/page.h>
  25. #include <asm/pgalloc.h>
  26. #include <asm/addrspace.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/tlbflush.h>
  29. #include <asm/mmu.h>
  30. static struct resource shmedia_iomap = {
  31. .name = "shmedia_iomap",
  32. .start = IOBASE_VADDR + PAGE_SIZE,
  33. .end = IOBASE_END - 1,
  34. };
  35. static void shmedia_mapioaddr(unsigned long pa, unsigned long va,
  36. unsigned long flags);
  37. static void shmedia_unmapioaddr(unsigned long vaddr);
  38. static void __iomem *shmedia_ioremap(struct resource *res, u32 pa,
  39. int sz, unsigned long flags);
  40. /*
  41. * We have the same problem as the SPARC, so lets have the same comment:
  42. * Our mini-allocator...
  43. * Boy this is gross! We need it because we must map I/O for
  44. * timers and interrupt controller before the kmalloc is available.
  45. */
  46. #define XNMLN 15
  47. #define XNRES 10
  48. struct xresource {
  49. struct resource xres; /* Must be first */
  50. int xflag; /* 1 == used */
  51. char xname[XNMLN+1];
  52. };
  53. static struct xresource xresv[XNRES];
  54. static struct xresource *xres_alloc(void)
  55. {
  56. struct xresource *xrp;
  57. int n;
  58. xrp = xresv;
  59. for (n = 0; n < XNRES; n++) {
  60. if (xrp->xflag == 0) {
  61. xrp->xflag = 1;
  62. return xrp;
  63. }
  64. xrp++;
  65. }
  66. return NULL;
  67. }
  68. static void xres_free(struct xresource *xrp)
  69. {
  70. xrp->xflag = 0;
  71. }
  72. static struct resource *shmedia_find_resource(struct resource *root,
  73. unsigned long vaddr)
  74. {
  75. struct resource *res;
  76. for (res = root->child; res; res = res->sibling)
  77. if (res->start <= vaddr && res->end >= vaddr)
  78. return res;
  79. return NULL;
  80. }
  81. static void __iomem *shmedia_alloc_io(unsigned long phys, unsigned long size,
  82. const char *name, unsigned long flags)
  83. {
  84. struct xresource *xres;
  85. struct resource *res;
  86. char *tack;
  87. int tlen;
  88. if (name == NULL)
  89. name = "???";
  90. xres = xres_alloc();
  91. if (xres != 0) {
  92. tack = xres->xname;
  93. res = &xres->xres;
  94. } else {
  95. printk_once(KERN_NOTICE "%s: done with statics, "
  96. "switching to kmalloc\n", __func__);
  97. tlen = strlen(name);
  98. tack = kmalloc(sizeof(struct resource) + tlen + 1, GFP_KERNEL);
  99. if (!tack)
  100. return NULL;
  101. memset(tack, 0, sizeof(struct resource));
  102. res = (struct resource *) tack;
  103. tack += sizeof(struct resource);
  104. }
  105. strncpy(tack, name, XNMLN);
  106. tack[XNMLN] = 0;
  107. res->name = tack;
  108. return shmedia_ioremap(res, phys, size, flags);
  109. }
  110. static void __iomem *shmedia_ioremap(struct resource *res, u32 pa, int sz,
  111. unsigned long flags)
  112. {
  113. unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
  114. unsigned long round_sz = (offset + sz + PAGE_SIZE-1) & PAGE_MASK;
  115. unsigned long va;
  116. unsigned int psz;
  117. if (allocate_resource(&shmedia_iomap, res, round_sz,
  118. shmedia_iomap.start, shmedia_iomap.end,
  119. PAGE_SIZE, NULL, NULL) != 0) {
  120. panic("alloc_io_res(%s): cannot occupy\n",
  121. (res->name != NULL) ? res->name : "???");
  122. }
  123. va = res->start;
  124. pa &= PAGE_MASK;
  125. psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
  126. for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
  127. shmedia_mapioaddr(pa, va, flags);
  128. va += PAGE_SIZE;
  129. pa += PAGE_SIZE;
  130. }
  131. return (void __iomem *)(unsigned long)(res->start + offset);
  132. }
  133. static void shmedia_free_io(struct resource *res)
  134. {
  135. unsigned long len = res->end - res->start + 1;
  136. BUG_ON((len & (PAGE_SIZE - 1)) != 0);
  137. while (len) {
  138. len -= PAGE_SIZE;
  139. shmedia_unmapioaddr(res->start + len);
  140. }
  141. release_resource(res);
  142. }
  143. static __init_refok void *sh64_get_page(void)
  144. {
  145. void *page;
  146. if (slab_is_available())
  147. page = (void *)get_zeroed_page(GFP_KERNEL);
  148. else
  149. page = alloc_bootmem_pages(PAGE_SIZE);
  150. if (!page || ((unsigned long)page & ~PAGE_MASK))
  151. panic("sh64_get_page: Out of memory already?\n");
  152. return page;
  153. }
  154. static void shmedia_mapioaddr(unsigned long pa, unsigned long va,
  155. unsigned long flags)
  156. {
  157. pgd_t *pgdp;
  158. pud_t *pudp;
  159. pmd_t *pmdp;
  160. pte_t *ptep, pte;
  161. pgprot_t prot;
  162. pr_debug("shmedia_mapiopage pa %08lx va %08lx\n", pa, va);
  163. if (!flags)
  164. flags = 1; /* 1 = CB0-1 device */
  165. pgdp = pgd_offset_k(va);
  166. if (pgd_none(*pgdp) || !pgd_present(*pgdp)) {
  167. pudp = (pud_t *)sh64_get_page();
  168. set_pgd(pgdp, __pgd((unsigned long)pudp | _KERNPG_TABLE));
  169. }
  170. pudp = pud_offset(pgdp, va);
  171. if (pud_none(*pudp) || !pud_present(*pudp)) {
  172. pmdp = (pmd_t *)sh64_get_page();
  173. set_pud(pudp, __pud((unsigned long)pmdp | _KERNPG_TABLE));
  174. }
  175. pmdp = pmd_offset(pudp, va);
  176. if (pmd_none(*pmdp) || !pmd_present(*pmdp)) {
  177. ptep = (pte_t *)sh64_get_page();
  178. set_pmd(pmdp, __pmd((unsigned long)ptep + _PAGE_TABLE));
  179. }
  180. prot = __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE |
  181. _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SHARED | flags);
  182. pte = pfn_pte(pa >> PAGE_SHIFT, prot);
  183. ptep = pte_offset_kernel(pmdp, va);
  184. if (!pte_none(*ptep) &&
  185. pte_val(*ptep) != pte_val(pte))
  186. pte_ERROR(*ptep);
  187. set_pte(ptep, pte);
  188. flush_tlb_kernel_range(va, PAGE_SIZE);
  189. }
  190. static void shmedia_unmapioaddr(unsigned long vaddr)
  191. {
  192. pgd_t *pgdp;
  193. pud_t *pudp;
  194. pmd_t *pmdp;
  195. pte_t *ptep;
  196. pgdp = pgd_offset_k(vaddr);
  197. if (pgd_none(*pgdp) || pgd_bad(*pgdp))
  198. return;
  199. pudp = pud_offset(pgdp, vaddr);
  200. if (pud_none(*pudp) || pud_bad(*pudp))
  201. return;
  202. pmdp = pmd_offset(pudp, vaddr);
  203. if (pmd_none(*pmdp) || pmd_bad(*pmdp))
  204. return;
  205. ptep = pte_offset_kernel(pmdp, vaddr);
  206. if (pte_none(*ptep) || !pte_present(*ptep))
  207. return;
  208. clear_page((void *)ptep);
  209. pte_clear(&init_mm, vaddr, ptep);
  210. }
  211. void __iomem *__ioremap(unsigned long offset, unsigned long size,
  212. unsigned long flags)
  213. {
  214. char name[14];
  215. sprintf(name, "phys_%08x", (u32)offset);
  216. return shmedia_alloc_io(offset, size, name, flags);
  217. }
  218. EXPORT_SYMBOL(__ioremap);
  219. void __iounmap(void __iomem *virtual)
  220. {
  221. unsigned long vaddr = (unsigned long)virtual & PAGE_MASK;
  222. struct resource *res;
  223. unsigned int psz;
  224. res = shmedia_find_resource(&shmedia_iomap, vaddr);
  225. if (!res) {
  226. printk(KERN_ERR "%s: Failed to free 0x%08lx\n",
  227. __func__, vaddr);
  228. return;
  229. }
  230. psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
  231. shmedia_free_io(res);
  232. if ((char *)res >= (char *)xresv &&
  233. (char *)res < (char *)&xresv[XNRES]) {
  234. xres_free((struct xresource *)res);
  235. } else {
  236. kfree(res);
  237. }
  238. }
  239. EXPORT_SYMBOL(__iounmap);
  240. static int
  241. ioremap_proc_info(char *buf, char **start, off_t fpos, int length, int *eof,
  242. void *data)
  243. {
  244. char *p = buf, *e = buf + length;
  245. struct resource *r;
  246. const char *nm;
  247. for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
  248. if (p + 32 >= e) /* Better than nothing */
  249. break;
  250. nm = r->name;
  251. if (nm == NULL)
  252. nm = "???";
  253. p += sprintf(p, "%08lx-%08lx: %s\n",
  254. (unsigned long)r->start,
  255. (unsigned long)r->end, nm);
  256. }
  257. return p-buf;
  258. }
  259. static int __init register_proc_onchip(void)
  260. {
  261. create_proc_read_entry("io_map", 0, 0, ioremap_proc_info,
  262. &shmedia_iomap);
  263. return 0;
  264. }
  265. late_initcall(register_proc_onchip);