ioremap.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Based on arch/arm/mm/ioremap.c
  3. *
  4. * (C) Copyright 1995 1996 Linus Torvalds
  5. * Hacked for ARM by Phil Blundell <philb@gnu.org>
  6. * Hacked to allow all architectures to build, and various cleanups
  7. * by Russell King
  8. * Copyright (C) 2012 ARM Ltd.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/export.h>
  23. #include <linux/mm.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/io.h>
  26. #include <asm/fixmap.h>
  27. #include <asm/tlbflush.h>
  28. #include <asm/pgalloc.h>
  29. static void __iomem *__ioremap_caller(phys_addr_t phys_addr, size_t size,
  30. pgprot_t prot, void *caller)
  31. {
  32. unsigned long last_addr;
  33. unsigned long offset = phys_addr & ~PAGE_MASK;
  34. int err;
  35. unsigned long addr;
  36. struct vm_struct *area;
  37. /*
  38. * Page align the mapping address and size, taking account of any
  39. * offset.
  40. */
  41. phys_addr &= PAGE_MASK;
  42. size = PAGE_ALIGN(size + offset);
  43. /*
  44. * Don't allow wraparound, zero size or outside PHYS_MASK.
  45. */
  46. last_addr = phys_addr + size - 1;
  47. if (!size || last_addr < phys_addr || (last_addr & ~PHYS_MASK))
  48. return NULL;
  49. /*
  50. * Don't allow RAM to be mapped.
  51. */
  52. if (WARN_ON(pfn_valid(__phys_to_pfn(phys_addr))))
  53. return NULL;
  54. area = get_vm_area_caller(size, VM_IOREMAP, caller);
  55. if (!area)
  56. return NULL;
  57. addr = (unsigned long)area->addr;
  58. area->phys_addr = phys_addr;
  59. err = ioremap_page_range(addr, addr + size, phys_addr, prot);
  60. if (err) {
  61. vunmap((void *)addr);
  62. return NULL;
  63. }
  64. return (void __iomem *)(offset + addr);
  65. }
  66. void __iomem *__ioremap(phys_addr_t phys_addr, size_t size, pgprot_t prot)
  67. {
  68. return __ioremap_caller(phys_addr, size, prot,
  69. __builtin_return_address(0));
  70. }
  71. EXPORT_SYMBOL(__ioremap);
  72. void __iounmap(volatile void __iomem *io_addr)
  73. {
  74. unsigned long addr = (unsigned long)io_addr & PAGE_MASK;
  75. /*
  76. * We could get an address outside vmalloc range in case
  77. * of ioremap_cache() reusing a RAM mapping.
  78. */
  79. if (VMALLOC_START <= addr && addr < VMALLOC_END)
  80. vunmap((void *)addr);
  81. }
  82. EXPORT_SYMBOL(__iounmap);
  83. void __iomem *ioremap_cache(phys_addr_t phys_addr, size_t size)
  84. {
  85. /* For normal memory we already have a cacheable mapping. */
  86. if (pfn_valid(__phys_to_pfn(phys_addr)))
  87. return (void __iomem *)__phys_to_virt(phys_addr);
  88. return __ioremap_caller(phys_addr, size, __pgprot(PROT_NORMAL),
  89. __builtin_return_address(0));
  90. }
  91. EXPORT_SYMBOL(ioremap_cache);
  92. /*
  93. * Must be called after early_fixmap_init
  94. */
  95. void __init early_ioremap_init(void)
  96. {
  97. early_ioremap_setup();
  98. }