ioremap_fixed.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Re-map IO memory to kernel address space so that we can access it.
  4. *
  5. * These functions should only be used when it is necessary to map a
  6. * physical address space into the kernel address space before ioremap()
  7. * can be used, e.g. early in boot before paging_init().
  8. *
  9. * Copyright (C) 2009 Matt Fleming
  10. */
  11. #include <linux/vmalloc.h>
  12. #include <linux/ioport.h>
  13. #include <linux/module.h>
  14. #include <linux/mm.h>
  15. #include <linux/io.h>
  16. #include <linux/bootmem.h>
  17. #include <linux/proc_fs.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(phys_addr_t phys_addr, unsigned long size, pgprot_t prot)
  43. {
  44. enum fixed_addresses idx0, idx;
  45. struct ioremap_map *map;
  46. unsigned int nrpages;
  47. unsigned long offset;
  48. int i, slot;
  49. /*
  50. * Mappings have to be page-aligned
  51. */
  52. offset = phys_addr & ~PAGE_MASK;
  53. phys_addr &= PAGE_MASK;
  54. size = PAGE_ALIGN(phys_addr + size) - phys_addr;
  55. slot = -1;
  56. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  57. map = &ioremap_maps[i];
  58. if (!map->addr) {
  59. map->size = size;
  60. slot = i;
  61. break;
  62. }
  63. }
  64. if (slot < 0)
  65. return NULL;
  66. /*
  67. * Mappings have to fit in the FIX_IOREMAP area.
  68. */
  69. nrpages = size >> PAGE_SHIFT;
  70. if (nrpages > FIX_N_IOREMAPS)
  71. return NULL;
  72. /*
  73. * Ok, go for it..
  74. */
  75. idx0 = FIX_IOREMAP_BEGIN + slot;
  76. idx = idx0;
  77. while (nrpages > 0) {
  78. pgprot_val(prot) |= _PAGE_WIRED;
  79. __set_fixmap(idx, phys_addr, prot);
  80. phys_addr += PAGE_SIZE;
  81. idx++;
  82. --nrpages;
  83. }
  84. map->addr = (void __iomem *)(offset + map->fixmap_addr);
  85. return map->addr;
  86. }
  87. int iounmap_fixed(void __iomem *addr)
  88. {
  89. enum fixed_addresses idx;
  90. struct ioremap_map *map;
  91. unsigned int nrpages;
  92. int i, slot;
  93. slot = -1;
  94. for (i = 0; i < FIX_N_IOREMAPS; i++) {
  95. map = &ioremap_maps[i];
  96. if (map->addr == addr) {
  97. slot = i;
  98. break;
  99. }
  100. }
  101. /*
  102. * If we don't match, it's not for us.
  103. */
  104. if (slot < 0)
  105. return -EINVAL;
  106. nrpages = map->size >> PAGE_SHIFT;
  107. idx = FIX_IOREMAP_BEGIN + slot + nrpages - 1;
  108. while (nrpages > 0) {
  109. __clear_fixmap(idx, __pgprot(_PAGE_WIRED));
  110. --idx;
  111. --nrpages;
  112. }
  113. map->size = 0;
  114. map->addr = NULL;
  115. return 0;
  116. }