ioremap_fixed.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 <asm/fixmap.h>
  18. #include <asm/page.h>
  19. #include <asm/pgalloc.h>
  20. #include <asm/addrspace.h>
  21. #include <asm/cacheflush.h>
  22. #include <asm/tlbflush.h>
  23. #include <asm/mmu.h>
  24. #include <asm/mmu_context.h>
  25. struct ioremap_map {
  26. void __iomem *addr;
  27. unsigned long size;
  28. unsigned long fixmap_addr;
  29. };
  30. static struct ioremap_map ioremap_maps[FIX_N_IOREMAPS];
  31. void __init ioremap_fixed_init(void)
  32. {
  33. struct ioremap_map *map;
  34. int i;
  35. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  36. map = &ioremap_maps[i];
  37. map->fixmap_addr = __fix_to_virt(FIX_IOREMAP_BEGIN + i);
  38. }
  39. }
  40. void __init __iomem *
  41. ioremap_fixed(phys_addr_t phys_addr, unsigned long size, pgprot_t prot)
  42. {
  43. enum fixed_addresses idx0, idx;
  44. struct ioremap_map *map;
  45. unsigned int nrpages;
  46. unsigned long offset;
  47. int i, slot;
  48. /*
  49. * Mappings have to be page-aligned
  50. */
  51. offset = phys_addr & ~PAGE_MASK;
  52. phys_addr &= PAGE_MASK;
  53. size = PAGE_ALIGN(phys_addr + size) - phys_addr;
  54. slot = -1;
  55. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  56. map = &ioremap_maps[i];
  57. if (!map->addr) {
  58. map->size = size;
  59. slot = i;
  60. break;
  61. }
  62. }
  63. if (slot < 0)
  64. return NULL;
  65. /*
  66. * Mappings have to fit in the FIX_IOREMAP area.
  67. */
  68. nrpages = size >> PAGE_SHIFT;
  69. if (nrpages > FIX_N_IOREMAPS)
  70. return NULL;
  71. /*
  72. * Ok, go for it..
  73. */
  74. idx0 = FIX_IOREMAP_BEGIN + slot;
  75. idx = idx0;
  76. while (nrpages > 0) {
  77. pgprot_val(prot) |= _PAGE_WIRED;
  78. __set_fixmap(idx, phys_addr, prot);
  79. phys_addr += PAGE_SIZE;
  80. idx++;
  81. --nrpages;
  82. }
  83. map->addr = (void __iomem *)(offset + map->fixmap_addr);
  84. return map->addr;
  85. }
  86. int iounmap_fixed(void __iomem *addr)
  87. {
  88. enum fixed_addresses idx;
  89. struct ioremap_map *map;
  90. unsigned int nrpages;
  91. int i, slot;
  92. slot = -1;
  93. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  94. map = &ioremap_maps[i];
  95. if (map->addr == addr) {
  96. slot = i;
  97. break;
  98. }
  99. }
  100. /*
  101. * If we don't match, it's not for us.
  102. */
  103. if (slot < 0)
  104. return -EINVAL;
  105. nrpages = map->size >> PAGE_SHIFT;
  106. idx = FIX_IOREMAP_BEGIN + slot + nrpages - 1;
  107. while (nrpages > 0) {
  108. __clear_fixmap(idx, __pgprot(_PAGE_WIRED));
  109. --idx;
  110. --nrpages;
  111. }
  112. map->size = 0;
  113. map->addr = NULL;
  114. return 0;
  115. }