ioremap.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * arch/sh/mm/ioremap.c
  3. *
  4. * (C) Copyright 1995 1996 Linus Torvalds
  5. * (C) Copyright 2005 - 2010 Paul Mundt
  6. *
  7. * Re-map IO memory to kernel address space so that we can access it.
  8. * This is needed for high PCI addresses that aren't mapped in the
  9. * 640k-1MB IO memory area on PC's
  10. *
  11. * This file is subject to the terms and conditions of the GNU General
  12. * Public License. See the file "COPYING" in the main directory of this
  13. * archive for more details.
  14. */
  15. #include <linux/vmalloc.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/mm.h>
  19. #include <linux/pci.h>
  20. #include <linux/io.h>
  21. #include <asm/page.h>
  22. #include <asm/pgalloc.h>
  23. #include <asm/addrspace.h>
  24. #include <asm/cacheflush.h>
  25. #include <asm/tlbflush.h>
  26. #include <asm/mmu.h>
  27. /*
  28. * Remap an arbitrary physical address space into the kernel virtual
  29. * address space. Needed when the kernel wants to access high addresses
  30. * directly.
  31. *
  32. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  33. * have to convert them into an offset in a page-aligned mapping, but the
  34. * caller shouldn't need to know that small detail.
  35. */
  36. void __iomem * __init_refok
  37. __ioremap_caller(phys_addr_t phys_addr, unsigned long size,
  38. pgprot_t pgprot, void *caller)
  39. {
  40. struct vm_struct *area;
  41. unsigned long offset, last_addr, addr, orig_addr;
  42. void __iomem *mapped;
  43. /* Don't allow wraparound or zero size */
  44. last_addr = phys_addr + size - 1;
  45. if (!size || last_addr < phys_addr)
  46. return NULL;
  47. /*
  48. * If we can't yet use the regular approach, go the fixmap route.
  49. */
  50. if (!mem_init_done)
  51. return ioremap_fixed(phys_addr, size, pgprot);
  52. /*
  53. * First try to remap through the PMB.
  54. * PMB entries are all pre-faulted.
  55. */
  56. mapped = pmb_remap_caller(phys_addr, size, pgprot, caller);
  57. if (mapped && !IS_ERR(mapped))
  58. return mapped;
  59. /*
  60. * Mappings have to be page-aligned
  61. */
  62. offset = phys_addr & ~PAGE_MASK;
  63. phys_addr &= PAGE_MASK;
  64. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  65. /*
  66. * Ok, go for it..
  67. */
  68. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  69. if (!area)
  70. return NULL;
  71. area->phys_addr = phys_addr;
  72. orig_addr = addr = (unsigned long)area->addr;
  73. if (ioremap_page_range(addr, addr + size, phys_addr, pgprot)) {
  74. vunmap((void *)orig_addr);
  75. return NULL;
  76. }
  77. return (void __iomem *)(offset + (char *)orig_addr);
  78. }
  79. EXPORT_SYMBOL(__ioremap_caller);
  80. /*
  81. * Simple checks for non-translatable mappings.
  82. */
  83. static inline int iomapping_nontranslatable(unsigned long offset)
  84. {
  85. #ifdef CONFIG_29BIT
  86. /*
  87. * In 29-bit mode this includes the fixed P1/P2 areas, as well as
  88. * parts of P3.
  89. */
  90. if (PXSEG(offset) < P3SEG || offset >= P3_ADDR_MAX)
  91. return 1;
  92. #endif
  93. return 0;
  94. }
  95. void __iounmap(void __iomem *addr)
  96. {
  97. unsigned long vaddr = (unsigned long __force)addr;
  98. struct vm_struct *p;
  99. /*
  100. * Nothing to do if there is no translatable mapping.
  101. */
  102. if (iomapping_nontranslatable(vaddr))
  103. return;
  104. /*
  105. * There's no VMA if it's from an early fixed mapping.
  106. */
  107. if (iounmap_fixed(addr) == 0)
  108. return;
  109. /*
  110. * If the PMB handled it, there's nothing else to do.
  111. */
  112. if (pmb_unmap(addr) == 0)
  113. return;
  114. p = remove_vm_area((void *)(vaddr & PAGE_MASK));
  115. if (!p) {
  116. printk(KERN_ERR "%s: bad address %p\n", __func__, addr);
  117. return;
  118. }
  119. kfree(p);
  120. }
  121. EXPORT_SYMBOL(__iounmap);