ioremap_64.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 <asm/page.h>
  24. #include <asm/pgalloc.h>
  25. #include <asm/addrspace.h>
  26. #include <asm/cacheflush.h>
  27. #include <asm/tlbflush.h>
  28. #include <asm/mmu.h>
  29. static void shmedia_mapioaddr(unsigned long, unsigned long);
  30. static unsigned long shmedia_ioremap(struct resource *, u32, int);
  31. /*
  32. * Generic mapping function (not visible outside):
  33. */
  34. /*
  35. * Remap an arbitrary physical address space into the kernel virtual
  36. * address space. Needed when the kernel wants to access high addresses
  37. * directly.
  38. *
  39. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  40. * have to convert them into an offset in a page-aligned mapping, but the
  41. * caller shouldn't need to know that small detail.
  42. */
  43. void *__ioremap(unsigned long phys_addr, unsigned long size,
  44. unsigned long flags)
  45. {
  46. void * addr;
  47. struct vm_struct * area;
  48. unsigned long offset, last_addr;
  49. pgprot_t pgprot;
  50. /* Don't allow wraparound or zero size */
  51. last_addr = phys_addr + size - 1;
  52. if (!size || last_addr < phys_addr)
  53. return NULL;
  54. pgprot = __pgprot(_PAGE_PRESENT | _PAGE_READ |
  55. _PAGE_WRITE | _PAGE_DIRTY |
  56. _PAGE_ACCESSED | _PAGE_SHARED | flags);
  57. /*
  58. * Mappings have to be page-aligned
  59. */
  60. offset = phys_addr & ~PAGE_MASK;
  61. phys_addr &= PAGE_MASK;
  62. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  63. /*
  64. * Ok, go for it..
  65. */
  66. area = get_vm_area(size, VM_IOREMAP);
  67. if (!area)
  68. return NULL;
  69. pr_debug("Get vm_area returns %p addr %p\n", area, area->addr);
  70. area->phys_addr = phys_addr;
  71. addr = area->addr;
  72. if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
  73. phys_addr, pgprot)) {
  74. vunmap(addr);
  75. return NULL;
  76. }
  77. return (void *) (offset + (char *)addr);
  78. }
  79. EXPORT_SYMBOL(__ioremap);
  80. void __iounmap(void *addr)
  81. {
  82. struct vm_struct *area;
  83. vfree((void *) (PAGE_MASK & (unsigned long) addr));
  84. area = remove_vm_area((void *) (PAGE_MASK & (unsigned long) addr));
  85. if (!area) {
  86. printk(KERN_ERR "iounmap: bad address %p\n", addr);
  87. return;
  88. }
  89. kfree(area);
  90. }
  91. EXPORT_SYMBOL(__iounmap);
  92. static struct resource shmedia_iomap = {
  93. .name = "shmedia_iomap",
  94. .start = IOBASE_VADDR + PAGE_SIZE,
  95. .end = IOBASE_END - 1,
  96. };
  97. static void shmedia_mapioaddr(unsigned long pa, unsigned long va);
  98. static void shmedia_unmapioaddr(unsigned long vaddr);
  99. static unsigned long shmedia_ioremap(struct resource *res, u32 pa, int sz);
  100. /*
  101. * We have the same problem as the SPARC, so lets have the same comment:
  102. * Our mini-allocator...
  103. * Boy this is gross! We need it because we must map I/O for
  104. * timers and interrupt controller before the kmalloc is available.
  105. */
  106. #define XNMLN 15
  107. #define XNRES 10
  108. struct xresource {
  109. struct resource xres; /* Must be first */
  110. int xflag; /* 1 == used */
  111. char xname[XNMLN+1];
  112. };
  113. static struct xresource xresv[XNRES];
  114. static struct xresource *xres_alloc(void)
  115. {
  116. struct xresource *xrp;
  117. int n;
  118. xrp = xresv;
  119. for (n = 0; n < XNRES; n++) {
  120. if (xrp->xflag == 0) {
  121. xrp->xflag = 1;
  122. return xrp;
  123. }
  124. xrp++;
  125. }
  126. return NULL;
  127. }
  128. static void xres_free(struct xresource *xrp)
  129. {
  130. xrp->xflag = 0;
  131. }
  132. static struct resource *shmedia_find_resource(struct resource *root,
  133. unsigned long vaddr)
  134. {
  135. struct resource *res;
  136. for (res = root->child; res; res = res->sibling)
  137. if (res->start <= vaddr && res->end >= vaddr)
  138. return res;
  139. return NULL;
  140. }
  141. static unsigned long shmedia_alloc_io(unsigned long phys, unsigned long size,
  142. const char *name)
  143. {
  144. static int printed_full = 0;
  145. struct xresource *xres;
  146. struct resource *res;
  147. char *tack;
  148. int tlen;
  149. if (name == NULL) name = "???";
  150. if ((xres = xres_alloc()) != 0) {
  151. tack = xres->xname;
  152. res = &xres->xres;
  153. } else {
  154. if (!printed_full) {
  155. printk("%s: done with statics, switching to kmalloc\n",
  156. __func__);
  157. printed_full = 1;
  158. }
  159. tlen = strlen(name);
  160. tack = kmalloc(sizeof (struct resource) + tlen + 1, GFP_KERNEL);
  161. if (!tack)
  162. return -ENOMEM;
  163. memset(tack, 0, sizeof(struct resource));
  164. res = (struct resource *) tack;
  165. tack += sizeof (struct resource);
  166. }
  167. strncpy(tack, name, XNMLN);
  168. tack[XNMLN] = 0;
  169. res->name = tack;
  170. return shmedia_ioremap(res, phys, size);
  171. }
  172. static unsigned long shmedia_ioremap(struct resource *res, u32 pa, int sz)
  173. {
  174. unsigned long offset = ((unsigned long) pa) & (~PAGE_MASK);
  175. unsigned long round_sz = (offset + sz + PAGE_SIZE-1) & PAGE_MASK;
  176. unsigned long va;
  177. unsigned int psz;
  178. if (allocate_resource(&shmedia_iomap, res, round_sz,
  179. shmedia_iomap.start, shmedia_iomap.end,
  180. PAGE_SIZE, NULL, NULL) != 0) {
  181. panic("alloc_io_res(%s): cannot occupy\n",
  182. (res->name != NULL)? res->name: "???");
  183. }
  184. va = res->start;
  185. pa &= PAGE_MASK;
  186. psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
  187. /* log at boot time ... */
  188. printk("mapioaddr: %6s [%2d page%s] va 0x%08lx pa 0x%08x\n",
  189. ((res->name != NULL) ? res->name : "???"),
  190. psz, psz == 1 ? " " : "s", va, pa);
  191. for (psz = res->end - res->start + 1; psz != 0; psz -= PAGE_SIZE) {
  192. shmedia_mapioaddr(pa, va);
  193. va += PAGE_SIZE;
  194. pa += PAGE_SIZE;
  195. }
  196. res->start += offset;
  197. res->end = res->start + sz - 1; /* not strictly necessary.. */
  198. return res->start;
  199. }
  200. static void shmedia_free_io(struct resource *res)
  201. {
  202. unsigned long len = res->end - res->start + 1;
  203. BUG_ON((len & (PAGE_SIZE - 1)) != 0);
  204. while (len) {
  205. len -= PAGE_SIZE;
  206. shmedia_unmapioaddr(res->start + len);
  207. }
  208. release_resource(res);
  209. }
  210. static __init_refok void *sh64_get_page(void)
  211. {
  212. extern int after_bootmem;
  213. void *page;
  214. if (after_bootmem) {
  215. page = (void *)get_zeroed_page(GFP_ATOMIC);
  216. } else {
  217. page = alloc_bootmem_pages(PAGE_SIZE);
  218. }
  219. if (!page || ((unsigned long)page & ~PAGE_MASK))
  220. panic("sh64_get_page: Out of memory already?\n");
  221. return page;
  222. }
  223. static void shmedia_mapioaddr(unsigned long pa, unsigned long va)
  224. {
  225. pgd_t *pgdp;
  226. pud_t *pudp;
  227. pmd_t *pmdp;
  228. pte_t *ptep, pte;
  229. pgprot_t prot;
  230. unsigned long flags = 1; /* 1 = CB0-1 device */
  231. pr_debug("shmedia_mapiopage pa %08lx va %08lx\n", pa, va);
  232. pgdp = pgd_offset_k(va);
  233. if (pgd_none(*pgdp) || !pgd_present(*pgdp)) {
  234. pudp = (pud_t *)sh64_get_page();
  235. set_pgd(pgdp, __pgd((unsigned long)pudp | _KERNPG_TABLE));
  236. }
  237. pudp = pud_offset(pgdp, va);
  238. if (pud_none(*pudp) || !pud_present(*pudp)) {
  239. pmdp = (pmd_t *)sh64_get_page();
  240. set_pud(pudp, __pud((unsigned long)pmdp | _KERNPG_TABLE));
  241. }
  242. pmdp = pmd_offset(pudp, va);
  243. if (pmd_none(*pmdp) || !pmd_present(*pmdp) ) {
  244. ptep = (pte_t *)sh64_get_page();
  245. set_pmd(pmdp, __pmd((unsigned long)ptep + _PAGE_TABLE));
  246. }
  247. prot = __pgprot(_PAGE_PRESENT | _PAGE_READ | _PAGE_WRITE |
  248. _PAGE_DIRTY | _PAGE_ACCESSED | _PAGE_SHARED | flags);
  249. pte = pfn_pte(pa >> PAGE_SHIFT, prot);
  250. ptep = pte_offset_kernel(pmdp, va);
  251. if (!pte_none(*ptep) &&
  252. pte_val(*ptep) != pte_val(pte))
  253. pte_ERROR(*ptep);
  254. set_pte(ptep, pte);
  255. flush_tlb_kernel_range(va, PAGE_SIZE);
  256. }
  257. static void shmedia_unmapioaddr(unsigned long vaddr)
  258. {
  259. pgd_t *pgdp;
  260. pud_t *pudp;
  261. pmd_t *pmdp;
  262. pte_t *ptep;
  263. pgdp = pgd_offset_k(vaddr);
  264. if (pgd_none(*pgdp) || pgd_bad(*pgdp))
  265. return;
  266. pudp = pud_offset(pgdp, vaddr);
  267. if (pud_none(*pudp) || pud_bad(*pudp))
  268. return;
  269. pmdp = pmd_offset(pudp, vaddr);
  270. if (pmd_none(*pmdp) || pmd_bad(*pmdp))
  271. return;
  272. ptep = pte_offset_kernel(pmdp, vaddr);
  273. if (pte_none(*ptep) || !pte_present(*ptep))
  274. return;
  275. clear_page((void *)ptep);
  276. pte_clear(&init_mm, vaddr, ptep);
  277. }
  278. unsigned long onchip_remap(unsigned long phys, unsigned long size, const char *name)
  279. {
  280. if (size < PAGE_SIZE)
  281. size = PAGE_SIZE;
  282. return shmedia_alloc_io(phys, size, name);
  283. }
  284. EXPORT_SYMBOL(onchip_remap);
  285. void onchip_unmap(unsigned long vaddr)
  286. {
  287. struct resource *res;
  288. unsigned int psz;
  289. res = shmedia_find_resource(&shmedia_iomap, vaddr);
  290. if (!res) {
  291. printk(KERN_ERR "%s: Failed to free 0x%08lx\n",
  292. __func__, vaddr);
  293. return;
  294. }
  295. psz = (res->end - res->start + (PAGE_SIZE - 1)) / PAGE_SIZE;
  296. printk(KERN_DEBUG "unmapioaddr: %6s [%2d page%s] freed\n",
  297. res->name, psz, psz == 1 ? " " : "s");
  298. shmedia_free_io(res);
  299. if ((char *)res >= (char *)xresv &&
  300. (char *)res < (char *)&xresv[XNRES]) {
  301. xres_free((struct xresource *)res);
  302. } else {
  303. kfree(res);
  304. }
  305. }
  306. EXPORT_SYMBOL(onchip_unmap);
  307. #ifdef CONFIG_PROC_FS
  308. static int
  309. ioremap_proc_info(char *buf, char **start, off_t fpos, int length, int *eof,
  310. void *data)
  311. {
  312. char *p = buf, *e = buf + length;
  313. struct resource *r;
  314. const char *nm;
  315. for (r = ((struct resource *)data)->child; r != NULL; r = r->sibling) {
  316. if (p + 32 >= e) /* Better than nothing */
  317. break;
  318. if ((nm = r->name) == 0) nm = "???";
  319. p += sprintf(p, "%08lx-%08lx: %s\n",
  320. (unsigned long)r->start,
  321. (unsigned long)r->end, nm);
  322. }
  323. return p-buf;
  324. }
  325. #endif /* CONFIG_PROC_FS */
  326. static int __init register_proc_onchip(void)
  327. {
  328. #ifdef CONFIG_PROC_FS
  329. create_proc_read_entry("io_map",0,0, ioremap_proc_info, &shmedia_iomap);
  330. #endif
  331. return 0;
  332. }
  333. __initcall(register_proc_onchip);