ioremap.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Re-map IO memory to kernel address space so that we can access it.
  3. * This is needed for high PCI addresses that aren't mapped in the
  4. * 640k-1MB IO memory area on PC's
  5. *
  6. * (C) Copyright 1995 1996 Linus Torvalds
  7. */
  8. #include <linux/vmalloc.h>
  9. #include <linux/mm.h>
  10. #include <linux/sched.h>
  11. #include <linux/io.h>
  12. #include <linux/module.h>
  13. #include <asm/cacheflush.h>
  14. #include <asm/pgtable.h>
  15. static int ioremap_pte_range(pmd_t *pmd, unsigned long addr,
  16. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  17. {
  18. pte_t *pte;
  19. u64 pfn;
  20. pfn = phys_addr >> PAGE_SHIFT;
  21. pte = pte_alloc_kernel(pmd, addr);
  22. if (!pte)
  23. return -ENOMEM;
  24. do {
  25. BUG_ON(!pte_none(*pte));
  26. set_pte_at(&init_mm, addr, pte, pfn_pte(pfn, prot));
  27. pfn++;
  28. } while (pte++, addr += PAGE_SIZE, addr != end);
  29. return 0;
  30. }
  31. static inline int ioremap_pmd_range(pud_t *pud, unsigned long addr,
  32. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  33. {
  34. pmd_t *pmd;
  35. unsigned long next;
  36. phys_addr -= addr;
  37. pmd = pmd_alloc(&init_mm, pud, addr);
  38. if (!pmd)
  39. return -ENOMEM;
  40. do {
  41. next = pmd_addr_end(addr, end);
  42. if (ioremap_pte_range(pmd, addr, next, phys_addr + addr, prot))
  43. return -ENOMEM;
  44. } while (pmd++, addr = next, addr != end);
  45. return 0;
  46. }
  47. static inline int ioremap_pud_range(pgd_t *pgd, unsigned long addr,
  48. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  49. {
  50. pud_t *pud;
  51. unsigned long next;
  52. phys_addr -= addr;
  53. pud = pud_alloc(&init_mm, pgd, addr);
  54. if (!pud)
  55. return -ENOMEM;
  56. do {
  57. next = pud_addr_end(addr, end);
  58. if (ioremap_pmd_range(pud, addr, next, phys_addr + addr, prot))
  59. return -ENOMEM;
  60. } while (pud++, addr = next, addr != end);
  61. return 0;
  62. }
  63. int ioremap_page_range(unsigned long addr,
  64. unsigned long end, phys_addr_t phys_addr, pgprot_t prot)
  65. {
  66. pgd_t *pgd;
  67. unsigned long start;
  68. unsigned long next;
  69. int err;
  70. BUG_ON(addr >= end);
  71. start = addr;
  72. phys_addr -= addr;
  73. pgd = pgd_offset_k(addr);
  74. do {
  75. next = pgd_addr_end(addr, end);
  76. err = ioremap_pud_range(pgd, addr, next, phys_addr+addr, prot);
  77. if (err)
  78. break;
  79. } while (pgd++, addr = next, addr != end);
  80. flush_cache_vmap(start, end);
  81. return err;
  82. }
  83. EXPORT_SYMBOL_GPL(ioremap_page_range);