ioremap.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * (c) Copyright 2006, 2007 Hewlett-Packard Development Company, L.P.
  3. * Bjorn Helgaas <bjorn.helgaas@hp.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/compiler.h>
  10. #include <linux/module.h>
  11. #include <linux/efi.h>
  12. #include <linux/io.h>
  13. #include <linux/vmalloc.h>
  14. #include <asm/io.h>
  15. #include <asm/meminit.h>
  16. static inline void __iomem *
  17. __ioremap_uc(unsigned long phys_addr)
  18. {
  19. return (void __iomem *) (__IA64_UNCACHED_OFFSET | phys_addr);
  20. }
  21. void __iomem *
  22. early_ioremap (unsigned long phys_addr, unsigned long size)
  23. {
  24. u64 attr;
  25. attr = kern_mem_attribute(phys_addr, size);
  26. if (attr & EFI_MEMORY_WB)
  27. return (void __iomem *) phys_to_virt(phys_addr);
  28. return __ioremap_uc(phys_addr);
  29. }
  30. void __iomem *
  31. ioremap (unsigned long phys_addr, unsigned long size)
  32. {
  33. void __iomem *addr;
  34. struct vm_struct *area;
  35. unsigned long offset;
  36. pgprot_t prot;
  37. u64 attr;
  38. unsigned long gran_base, gran_size;
  39. unsigned long page_base;
  40. /*
  41. * For things in kern_memmap, we must use the same attribute
  42. * as the rest of the kernel. For more details, see
  43. * Documentation/ia64/aliasing.txt.
  44. */
  45. attr = kern_mem_attribute(phys_addr, size);
  46. if (attr & EFI_MEMORY_WB)
  47. return (void __iomem *) phys_to_virt(phys_addr);
  48. else if (attr & EFI_MEMORY_UC)
  49. return __ioremap_uc(phys_addr);
  50. /*
  51. * Some chipsets don't support UC access to memory. If
  52. * WB is supported for the whole granule, we prefer that.
  53. */
  54. gran_base = GRANULEROUNDDOWN(phys_addr);
  55. gran_size = GRANULEROUNDUP(phys_addr + size) - gran_base;
  56. if (efi_mem_attribute(gran_base, gran_size) & EFI_MEMORY_WB)
  57. return (void __iomem *) phys_to_virt(phys_addr);
  58. /*
  59. * WB is not supported for the whole granule, so we can't use
  60. * the region 7 identity mapping. If we can safely cover the
  61. * area with kernel page table mappings, we can use those
  62. * instead.
  63. */
  64. page_base = phys_addr & PAGE_MASK;
  65. size = PAGE_ALIGN(phys_addr + size) - page_base;
  66. if (efi_mem_attribute(page_base, size) & EFI_MEMORY_WB) {
  67. prot = PAGE_KERNEL;
  68. /*
  69. * Mappings have to be page-aligned
  70. */
  71. offset = phys_addr & ~PAGE_MASK;
  72. phys_addr &= PAGE_MASK;
  73. /*
  74. * Ok, go for it..
  75. */
  76. area = get_vm_area(size, VM_IOREMAP);
  77. if (!area)
  78. return NULL;
  79. area->phys_addr = phys_addr;
  80. addr = (void __iomem *) area->addr;
  81. if (ioremap_page_range((unsigned long) addr,
  82. (unsigned long) addr + size, phys_addr, prot)) {
  83. vunmap((void __force *) addr);
  84. return NULL;
  85. }
  86. return (void __iomem *) (offset + (char __iomem *)addr);
  87. }
  88. return __ioremap_uc(phys_addr);
  89. }
  90. EXPORT_SYMBOL(ioremap);
  91. void __iomem *
  92. ioremap_nocache (unsigned long phys_addr, unsigned long size)
  93. {
  94. if (kern_mem_attribute(phys_addr, size) & EFI_MEMORY_WB)
  95. return NULL;
  96. return __ioremap_uc(phys_addr);
  97. }
  98. EXPORT_SYMBOL(ioremap_nocache);
  99. void
  100. early_iounmap (volatile void __iomem *addr, unsigned long size)
  101. {
  102. }
  103. void
  104. iounmap (volatile void __iomem *addr)
  105. {
  106. if (REGION_NUMBER(addr) == RGN_GATE)
  107. vunmap((void *) ((unsigned long) addr & PAGE_MASK));
  108. }
  109. EXPORT_SYMBOL(iounmap);