ioremap_fixed.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 offset,
  43. unsigned long size, pgprot_t prot)
  44. {
  45. enum fixed_addresses idx0, idx;
  46. struct ioremap_map *map;
  47. unsigned int nrpages;
  48. int i, slot;
  49. slot = -1;
  50. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  51. map = &ioremap_maps[i];
  52. if (!map->addr) {
  53. map->size = size;
  54. slot = i;
  55. break;
  56. }
  57. }
  58. if (slot < 0)
  59. return NULL;
  60. /*
  61. * Mappings have to fit in the FIX_IOREMAP area.
  62. */
  63. nrpages = size >> PAGE_SHIFT;
  64. if (nrpages > FIX_N_IOREMAPS)
  65. return NULL;
  66. /*
  67. * Ok, go for it..
  68. */
  69. idx0 = FIX_IOREMAP_BEGIN + slot;
  70. idx = idx0;
  71. while (nrpages > 0) {
  72. pgprot_val(prot) |= _PAGE_WIRED;
  73. __set_fixmap(idx, phys_addr, prot);
  74. phys_addr += PAGE_SIZE;
  75. idx++;
  76. --nrpages;
  77. }
  78. map->addr = (void __iomem *)(offset + map->fixmap_addr);
  79. return map->addr;
  80. }
  81. int iounmap_fixed(void __iomem *addr)
  82. {
  83. enum fixed_addresses idx;
  84. unsigned long virt_addr;
  85. struct ioremap_map *map;
  86. unsigned long offset;
  87. unsigned int nrpages;
  88. int i, slot;
  89. slot = -1;
  90. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  91. map = &ioremap_maps[i];
  92. if (map->addr == addr) {
  93. slot = i;
  94. break;
  95. }
  96. }
  97. /*
  98. * If we don't match, it's not for us.
  99. */
  100. if (slot < 0)
  101. return -EINVAL;
  102. virt_addr = (unsigned long)addr;
  103. offset = virt_addr & ~PAGE_MASK;
  104. nrpages = PAGE_ALIGN(offset + map->size - 1) >> PAGE_SHIFT;
  105. idx = FIX_IOREMAP_BEGIN + slot + nrpages;
  106. while (nrpages > 0) {
  107. __clear_fixmap(idx, __pgprot(_PAGE_WIRED));
  108. --idx;
  109. --nrpages;
  110. }
  111. map->size = 0;
  112. map->addr = NULL;
  113. return 0;
  114. }