ioremap.c 9.4 KB

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