ioremap.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * linux/arch/m32r/mm/ioremap.c
  3. *
  4. * Copyright (c) 2001, 2002 Hiroyuki Kondo
  5. *
  6. * Taken from mips version.
  7. * (C) Copyright 1995 1996 Linus Torvalds
  8. * (C) Copyright 2001 Ralf Baechle
  9. */
  10. /*
  11. * This file is subject to the terms and conditions of the GNU General Public
  12. * License. See the file "COPYING" in the main directory of this archive
  13. * for more details.
  14. *
  15. */
  16. #include <linux/module.h>
  17. #include <asm/addrspace.h>
  18. #include <asm/byteorder.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/io.h>
  21. #include <asm/pgalloc.h>
  22. /*
  23. * Generic mapping function (not visible outside):
  24. */
  25. /*
  26. * Remap an arbitrary physical address space into the kernel virtual
  27. * address space. Needed when the kernel wants to access high addresses
  28. * directly.
  29. *
  30. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  31. * have to convert them into an offset in a page-aligned mapping, but the
  32. * caller shouldn't need to know that small detail.
  33. */
  34. #define IS_LOW512(addr) (!((unsigned long)(addr) & ~0x1fffffffUL))
  35. void __iomem *
  36. __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  37. {
  38. void __iomem * addr;
  39. struct vm_struct * area;
  40. unsigned long offset, last_addr;
  41. pgprot_t pgprot;
  42. /* Don't allow wraparound or zero size */
  43. last_addr = phys_addr + size - 1;
  44. if (!size || last_addr < phys_addr)
  45. return NULL;
  46. /*
  47. * Map objects in the low 512mb of address space using KSEG1, otherwise
  48. * map using page tables.
  49. */
  50. if (IS_LOW512(phys_addr) && IS_LOW512(phys_addr + size - 1))
  51. return (void *) KSEG1ADDR(phys_addr);
  52. /*
  53. * Don't allow anybody to remap normal RAM that we're using..
  54. */
  55. if (phys_addr < virt_to_phys(high_memory)) {
  56. char *t_addr, *t_end;
  57. struct page *page;
  58. t_addr = __va(phys_addr);
  59. t_end = t_addr + (size - 1);
  60. for(page = virt_to_page(t_addr); page <= virt_to_page(t_end); page++)
  61. if(!PageReserved(page))
  62. return NULL;
  63. }
  64. pgprot = __pgprot(_PAGE_GLOBAL | _PAGE_PRESENT | _PAGE_READ
  65. | _PAGE_WRITE | flags);
  66. /*
  67. * Mappings have to be page-aligned
  68. */
  69. offset = phys_addr & ~PAGE_MASK;
  70. phys_addr &= PAGE_MASK;
  71. size = PAGE_ALIGN(last_addr + 1) - phys_addr;
  72. /*
  73. * Ok, go for it..
  74. */
  75. area = get_vm_area(size, VM_IOREMAP);
  76. if (!area)
  77. return NULL;
  78. area->phys_addr = phys_addr;
  79. addr = (void __iomem *) area->addr;
  80. if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
  81. phys_addr, pgprot)) {
  82. vunmap((void __force *) addr);
  83. return NULL;
  84. }
  85. return (void __iomem *) (offset + (char __iomem *)addr);
  86. }
  87. #define IS_KSEG1(addr) (((unsigned long)(addr) & ~0x1fffffffUL) == KSEG1)
  88. void iounmap(volatile void __iomem *addr)
  89. {
  90. if (!IS_KSEG1(addr))
  91. vfree((void *) (PAGE_MASK & (unsigned long) addr));
  92. }