ioremap_64.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. static int printed_full;
  85. struct xresource *xres;
  86. struct resource *res;
  87. char *tack;
  88. int tlen;
  89. if (name == NULL)
  90. name = "???";
  91. xres = xres_alloc();
  92. if (xres != 0) {
  93. tack = xres->xname;
  94. res = &xres->xres;
  95. } else {
  96. if (!printed_full) {
  97. printk(KERN_NOTICE "%s: done with statics, "
  98. "switching to kmalloc\n", __func__);
  99. printed_full = 1;
  100. }
  101. tlen = strlen(name);
  102. tack = kmalloc(sizeof(struct resource) + tlen + 1, GFP_KERNEL);
  103. if (!tack)
  104. return NULL;
  105. memset(tack, 0, sizeof(struct resource));
  106. res = (struct resource *) tack;
  107. tack += sizeof(struct resource);
  108. }
  109. strncpy(tack, name, XNMLN);
  110. tack[XNMLN] = 0;
  111. res->name = tack;
  112. return shmedia_ioremap(res, phys, size, flags);
  113. }
  114. static void __iomem *shmedia_ioremap(struct resource *res, u32 pa, int sz,
  115. unsigned long flags)
  116. {
  117. unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
  118. unsigned long round_sz = (offset + sz + PAGE_SIZE-1) & PAGE_MASK;
  119. unsigned long va;
  120. unsigned int psz;
  121. if (allocate_resource(&shmedia_iomap, res, round_sz,
  122. shmedia_iomap.start, shmedia_iomap.end,
  123. PAGE_SIZE, NULL, NULL) != 0) {
  124. panic("alloc_io_res(%s): cannot occupy\n",
  125. (res->name != NULL) ? res->name : "???");
  126. }
  127. va = res->start;
  128. pa &= PAGE_MASK;
  129. psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
  130. for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
  131. shmedia_mapioaddr(pa, va, flags);
  132. va += PAGE_SIZE;
  133. pa += PAGE_SIZE;
  134. }
  135. return (void __iomem *)(unsigned long)(res->start + offset);
  136. }
  137. static void shmedia_free_io(struct resource *res)
  138. {
  139. unsigned long len = res->end - res->start + 1;
  140. BUG_ON((len & (PAGE_SIZE - 1)) != 0);
  141. while (len) {
  142. len -= PAGE_SIZE;
  143. shmedia_unmapioaddr(res->start + len);
  144. }
  145. release_resource(res);
  146. }
  147. static __init_refok void *sh64_get_page(void)
  148. {
  149. void *page;
  150. if (slab_is_available())
  151. page = (void *)get_zeroed_page(GFP_KERNEL);
  152. else
  153. page = alloc_bootmem_pages(PAGE_SIZE);
  154. if (!page || ((unsigned long)page & ~PAGE_MASK))
  155. panic("sh64_get_page: Out of memory already?\n");
  156. return page;
  157. }
  158. static void shmedia_mapioaddr(unsigned long pa, unsigned long va,
  159. unsigned long flags)
  160. {
  161. pgd_t *pgdp;
  162. pud_t *pudp;
  163. pmd_t *pmdp;
  164. pte_t *ptep, pte;
  165. pgprot_t prot;
  166. pr_debug("shmedia_mapiopage pa %08lx va %08lx\n", pa, va);
  167. if (!flags)
  168. flags = 1; /* 1 = CB0-1 device */
  169. pgdp = pgd_offset_k(va);
  170. if (pgd_none(*pgdp) || !pgd_present(*pgdp)) {
  171. pudp = (pud_t *)sh64_get_page();
  172. set_pgd(pgdp, __pgd((unsigned long)pudp | _KERNPG_TABLE));
  173. }
  174. pudp = pud_offset(pgdp, va);
  175. if (pud_none(*pudp) || !pud_present(*pudp)) {
  176. pmdp = (pmd_t *)sh64_get_page();
  177. set_pud(pudp, __pud((unsigned long)pmdp | _KERNPG_TABLE));
  178. }
  179. pmdp = pmd_offset(pudp, va);
  180. if (pmd_none(*pmdp) || !pmd_present(*pmdp)) {
  181. ptep = (pte_t *)sh64_get_page();
  182. set_pmd(pmdp, __pmd((unsigned long)ptep + _PAGE_TABLE));
  183. }
  184. prot = __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE |
  185. _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SHARED | flags);
  186. pte = pfn_pte(pa >> PAGE_SHIFT, prot);
  187. ptep = pte_offset_kernel(pmdp, va);
  188. if (!pte_none(*ptep) &&
  189. pte_val(*ptep) != pte_val(pte))
  190. pte_ERROR(*ptep);
  191. set_pte(ptep, pte);
  192. flush_tlb_kernel_range(va, PAGE_SIZE);
  193. }
  194. static void shmedia_unmapioaddr(unsigned long vaddr)
  195. {
  196. pgd_t *pgdp;
  197. pud_t *pudp;
  198. pmd_t *pmdp;
  199. pte_t *ptep;
  200. pgdp = pgd_offset_k(vaddr);
  201. if (pgd_none(*pgdp) || pgd_bad(*pgdp))
  202. return;
  203. pudp = pud_offset(pgdp, vaddr);
  204. if (pud_none(*pudp) || pud_bad(*pudp))
  205. return;
  206. pmdp = pmd_offset(pudp, vaddr);
  207. if (pmd_none(*pmdp) || pmd_bad(*pmdp))
  208. return;
  209. ptep = pte_offset_kernel(pmdp, vaddr);
  210. if (pte_none(*ptep) || !pte_present(*ptep))
  211. return;
  212. clear_page((void *)ptep);
  213. pte_clear(&init_mm, vaddr, ptep);
  214. }
  215. void __iomem *__ioremap(unsigned long offset, unsigned long size,
  216. unsigned long flags)
  217. {
  218. char name[14];
  219. sprintf(name, "phys_%08x", (u32)offset);
  220. return shmedia_alloc_io(offset, size, name, flags);
  221. }
  222. EXPORT_SYMBOL(__ioremap);
  223. void __iounmap(void __iomem *virtual)
  224. {
  225. unsigned long vaddr = (unsigned long)virtual & PAGE_MASK;
  226. struct resource *res;
  227. unsigned int psz;
  228. res = shmedia_find_resource(&shmedia_iomap, vaddr);
  229. if (!res) {
  230. printk(KERN_ERR "%s: Failed to free 0x%08lx\n",
  231. __func__, vaddr);
  232. return;
  233. }
  234. psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
  235. shmedia_free_io(res);
  236. if ((char *)res >= (char *)xresv &&
  237. (char *)res < (char *)&xresv[XNRES]) {
  238. xres_free((struct xresource *)res);
  239. } else {
  240. kfree(res);
  241. }
  242. }
  243. EXPORT_SYMBOL(__iounmap);
  244. static int
  245. ioremap_proc_info(char *buf, char **start, off_t fpos, int length, int *eof,
  246. void *data)
  247. {
  248. char *p = buf, *e = buf + length;
  249. struct resource *r;
  250. const char *nm;
  251. for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
  252. if (p + 32 >= e) /* Better than nothing */
  253. break;
  254. nm = r->name;
  255. if (nm == NULL)
  256. nm = "???";
  257. p += sprintf(p, "%08lx-%08lx: %s\n",
  258. (unsigned long)r->start,
  259. (unsigned long)r->end, nm);
  260. }
  261. return p-buf;
  262. }
  263. static int __init register_proc_onchip(void)
  264. {
  265. create_proc_read_entry("io_map", 0, 0, ioremap_proc_info,
  266. &shmedia_iomap);
  267. return 0;
  268. }
  269. late_initcall(register_proc_onchip);