ioremap.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2004, 2007-2010, 2011-2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #include <linux/vmalloc.h>
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/io.h>
  12. #include <linux/mm.h>
  13. #include <linux/slab.h>
  14. #include <linux/cache.h>
  15. static inline bool arc_uncached_addr_space(phys_addr_t paddr)
  16. {
  17. if (is_isa_arcompact()) {
  18. if (paddr >= ARC_UNCACHED_ADDR_SPACE)
  19. return true;
  20. } else if (paddr >= perip_base && paddr <= perip_end) {
  21. return true;
  22. }
  23. return false;
  24. }
  25. void __iomem *ioremap(phys_addr_t paddr, unsigned long size)
  26. {
  27. phys_addr_t end;
  28. /* Don't allow wraparound or zero size */
  29. end = paddr + size - 1;
  30. if (!size || (end < paddr))
  31. return NULL;
  32. /*
  33. * If the region is h/w uncached, MMU mapping can be elided as optim
  34. * The cast to u32 is fine as this region can only be inside 4GB
  35. */
  36. if (arc_uncached_addr_space(paddr))
  37. return (void __iomem *)(u32)paddr;
  38. return ioremap_prot(paddr, size, PAGE_KERNEL_NO_CACHE);
  39. }
  40. EXPORT_SYMBOL(ioremap);
  41. /*
  42. * ioremap with access flags
  43. * Cache semantics wise it is same as ioremap - "forced" uncached.
  44. * However unlike vanilla ioremap which bypasses ARC MMU for addresses in
  45. * ARC hardware uncached region, this one still goes thru the MMU as caller
  46. * might need finer access control (R/W/X)
  47. */
  48. void __iomem *ioremap_prot(phys_addr_t paddr, unsigned long size,
  49. unsigned long flags)
  50. {
  51. unsigned long vaddr;
  52. struct vm_struct *area;
  53. phys_addr_t off, end;
  54. pgprot_t prot = __pgprot(flags);
  55. /* Don't allow wraparound, zero size */
  56. end = paddr + size - 1;
  57. if ((!size) || (end < paddr))
  58. return NULL;
  59. /* An early platform driver might end up here */
  60. if (!slab_is_available())
  61. return NULL;
  62. /* force uncached */
  63. prot = pgprot_noncached(prot);
  64. /* Mappings have to be page-aligned */
  65. off = paddr & ~PAGE_MASK;
  66. paddr &= PAGE_MASK;
  67. size = PAGE_ALIGN(end + 1) - paddr;
  68. /*
  69. * Ok, go for it..
  70. */
  71. area = get_vm_area(size, VM_IOREMAP);
  72. if (!area)
  73. return NULL;
  74. area->phys_addr = paddr;
  75. vaddr = (unsigned long)area->addr;
  76. if (ioremap_page_range(vaddr, vaddr + size, paddr, prot)) {
  77. vunmap((void __force *)vaddr);
  78. return NULL;
  79. }
  80. return (void __iomem *)(off + (char __iomem *)vaddr);
  81. }
  82. EXPORT_SYMBOL(ioremap_prot);
  83. void iounmap(const void __iomem *addr)
  84. {
  85. /* weird double cast to handle phys_addr_t > 32 bits */
  86. if (arc_uncached_addr_space((phys_addr_t)(u32)addr))
  87. return;
  88. vfree((void *)(PAGE_MASK & (unsigned long __force)addr));
  89. }
  90. EXPORT_SYMBOL(iounmap);