ioremap.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * OpenRISC ioremap.c
  3. *
  4. * Linux architectural port borrowing liberally from similar works of
  5. * others. All original copyrights apply as per the original source
  6. * declaration.
  7. *
  8. * Modifications for the OpenRISC architecture:
  9. * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
  10. * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/vmalloc.h>
  18. #include <linux/io.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/kmap_types.h>
  21. #include <asm/fixmap.h>
  22. #include <asm/bug.h>
  23. #include <asm/pgtable.h>
  24. #include <linux/sched.h>
  25. #include <asm/tlbflush.h>
  26. extern int mem_init_done;
  27. static unsigned int fixmaps_used __initdata;
  28. /*
  29. * Remap an arbitrary physical address space into the kernel virtual
  30. * address space. Needed when the kernel wants to access high addresses
  31. * directly.
  32. *
  33. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  34. * have to convert them into an offset in a page-aligned mapping, but the
  35. * caller shouldn't need to know that small detail.
  36. */
  37. void __iomem *__init_refok
  38. __ioremap(phys_addr_t addr, unsigned long size, pgprot_t prot)
  39. {
  40. phys_addr_t p;
  41. unsigned long v;
  42. unsigned long offset, last_addr;
  43. struct vm_struct *area = NULL;
  44. /* Don't allow wraparound or zero size */
  45. last_addr = addr + size - 1;
  46. if (!size || last_addr < addr)
  47. return NULL;
  48. /*
  49. * Mappings have to be page-aligned
  50. */
  51. offset = addr & ~PAGE_MASK;
  52. p = addr & PAGE_MASK;
  53. size = PAGE_ALIGN(last_addr + 1) - p;
  54. if (likely(mem_init_done)) {
  55. area = get_vm_area(size, VM_IOREMAP);
  56. if (!area)
  57. return NULL;
  58. v = (unsigned long)area->addr;
  59. } else {
  60. if ((fixmaps_used + (size >> PAGE_SHIFT)) > FIX_N_IOREMAPS)
  61. return NULL;
  62. v = fix_to_virt(FIX_IOREMAP_BEGIN + fixmaps_used);
  63. fixmaps_used += (size >> PAGE_SHIFT);
  64. }
  65. if (ioremap_page_range(v, v + size, p, prot)) {
  66. if (likely(mem_init_done))
  67. vfree(area->addr);
  68. else
  69. fixmaps_used -= (size >> PAGE_SHIFT);
  70. return NULL;
  71. }
  72. return (void __iomem *)(offset + (char *)v);
  73. }
  74. void iounmap(void *addr)
  75. {
  76. /* If the page is from the fixmap pool then we just clear out
  77. * the fixmap mapping.
  78. */
  79. if (unlikely((unsigned long)addr > FIXADDR_START)) {
  80. /* This is a bit broken... we don't really know
  81. * how big the area is so it's difficult to know
  82. * how many fixed pages to invalidate...
  83. * just flush tlb and hope for the best...
  84. * consider this a FIXME
  85. *
  86. * Really we should be clearing out one or more page
  87. * table entries for these virtual addresses so that
  88. * future references cause a page fault... for now, we
  89. * rely on two things:
  90. * i) this code never gets called on known boards
  91. * ii) invalid accesses to the freed areas aren't made
  92. */
  93. flush_tlb_all();
  94. return;
  95. }
  96. return vfree((void *)(PAGE_MASK & (unsigned long)addr));
  97. }
  98. /**
  99. * OK, this one's a bit tricky... ioremap can get called before memory is
  100. * initialized (early serial console does this) and will want to alloc a page
  101. * for its mapping. No userspace pages will ever get allocated before memory
  102. * is initialized so this applies only to kernel pages. In the event that
  103. * this is called before memory is initialized we allocate the page using
  104. * the memblock infrastructure.
  105. */
  106. pte_t __init_refok *pte_alloc_one_kernel(struct mm_struct *mm,
  107. unsigned long address)
  108. {
  109. pte_t *pte;
  110. if (likely(mem_init_done)) {
  111. pte = (pte_t *) __get_free_page(GFP_KERNEL | __GFP_REPEAT);
  112. } else {
  113. pte = (pte_t *) alloc_bootmem_low_pages(PAGE_SIZE);
  114. #if 0
  115. /* FIXME: use memblock... */
  116. pte = (pte_t *) __va(memblock_alloc(PAGE_SIZE, PAGE_SIZE));
  117. #endif
  118. }
  119. if (pte)
  120. clear_page(pte);
  121. return pte;
  122. }