ioremap_fixed.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Re-map IO memory to kernel address space so that we can access it.
  3. *
  4. * These functions should only be used when it is necessary to map a
  5. * physical address space into the kernel address space before ioremap()
  6. * can be used, e.g. early in boot before paging_init().
  7. *
  8. * Copyright (C) 2009 Matt Fleming
  9. */
  10. #include <linux/vmalloc.h>
  11. #include <linux/ioport.h>
  12. #include <linux/module.h>
  13. #include <linux/mm.h>
  14. #include <linux/io.h>
  15. #include <linux/bootmem.h>
  16. #include <linux/proc_fs.h>
  17. #include <linux/slab.h>
  18. #include <asm/fixmap.h>
  19. #include <asm/page.h>
  20. #include <asm/pgalloc.h>
  21. #include <asm/addrspace.h>
  22. #include <asm/cacheflush.h>
  23. #include <asm/tlbflush.h>
  24. #include <asm/mmu.h>
  25. #include <asm/mmu_context.h>
  26. struct ioremap_map {
  27. void __iomem *addr;
  28. unsigned long size;
  29. unsigned long fixmap_addr;
  30. };
  31. static struct ioremap_map ioremap_maps[FIX_N_IOREMAPS];
  32. void __init ioremap_fixed_init(void)
  33. {
  34. struct ioremap_map *map;
  35. int i;
  36. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  37. map = &ioremap_maps[i];
  38. map->fixmap_addr = __fix_to_virt(FIX_IOREMAP_BEGIN + i);
  39. }
  40. }
  41. void __init __iomem *
  42. ioremap_fixed(resource_size_t phys_addr, unsigned long size, pgprot_t prot)
  43. {
  44. enum fixed_addresses idx0, idx;
  45. resource_size_t last_addr;
  46. struct ioremap_map *map;
  47. unsigned long offset;
  48. unsigned int nrpages;
  49. int i, slot;
  50. slot = -1;
  51. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  52. map = &ioremap_maps[i];
  53. if (!map->addr) {
  54. map->size = size;
  55. slot = i;
  56. break;
  57. }
  58. }
  59. if (slot < 0)
  60. return NULL;
  61. /* Don't allow wraparound or zero size */
  62. last_addr = phys_addr + size - 1;
  63. if (!size || last_addr < phys_addr)
  64. return NULL;
  65. /*
  66. * Fixmap mappings have to be page-aligned
  67. */
  68. offset = phys_addr & ~PAGE_MASK;
  69. phys_addr &= PAGE_MASK;
  70. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  71. /*
  72. * Mappings have to fit in the FIX_IOREMAP area.
  73. */
  74. nrpages = size >> PAGE_SHIFT;
  75. if (nrpages > FIX_N_IOREMAPS)
  76. return NULL;
  77. /*
  78. * Ok, go for it..
  79. */
  80. idx0 = FIX_IOREMAP_BEGIN + slot;
  81. idx = idx0;
  82. while (nrpages > 0) {
  83. pgprot_val(prot) |= _PAGE_WIRED;
  84. __set_fixmap(idx, phys_addr, prot);
  85. phys_addr += PAGE_SIZE;
  86. idx++;
  87. --nrpages;
  88. }
  89. map->addr = (void __iomem *)(offset + map->fixmap_addr);
  90. return map->addr;
  91. }
  92. void __init iounmap_fixed(void __iomem *addr)
  93. {
  94. enum fixed_addresses idx;
  95. unsigned long virt_addr;
  96. struct ioremap_map *map;
  97. unsigned long offset;
  98. unsigned int nrpages;
  99. int i, slot;
  100. pgprot_t prot;
  101. slot = -1;
  102. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  103. map = &ioremap_maps[i];
  104. if (map->addr == addr) {
  105. slot = i;
  106. break;
  107. }
  108. }
  109. if (slot < 0)
  110. return;
  111. virt_addr = (unsigned long)addr;
  112. offset = virt_addr & ~PAGE_MASK;
  113. nrpages = PAGE_ALIGN(offset + map->size - 1) >> PAGE_SHIFT;
  114. pgprot_val(prot) = _PAGE_WIRED;
  115. idx = FIX_IOREMAP_BEGIN + slot + nrpages;
  116. while (nrpages > 0) {
  117. __clear_fixmap(idx, prot);
  118. --idx;
  119. --nrpages;
  120. }
  121. map->size = 0;
  122. map->addr = NULL;
  123. }