ioremap.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * arch/cris/mm/ioremap.c
  3. *
  4. * Re-map IO memory to kernel address space so that we can access it.
  5. * Needed for memory-mapped I/O devices mapped outside our normal DRAM
  6. * window (that is, all memory-mapped I/O devices).
  7. *
  8. * (C) Copyright 1995 1996 Linus Torvalds
  9. * CRIS-port by Axis Communications AB
  10. */
  11. #include <linux/vmalloc.h>
  12. #include <linux/io.h>
  13. #include <asm/pgalloc.h>
  14. #include <arch/memmap.h>
  15. /*
  16. * Generic mapping function (not visible outside):
  17. */
  18. /*
  19. * Remap an arbitrary physical address space into the kernel virtual
  20. * address space. Needed when the kernel wants to access high addresses
  21. * directly.
  22. *
  23. * NOTE! We need to allow non-page-aligned mappings too: we will obviously
  24. * have to convert them into an offset in a page-aligned mapping, but the
  25. * caller shouldn't need to know that small detail.
  26. */
  27. void __iomem * __ioremap_prot(unsigned long phys_addr, unsigned long size, pgprot_t prot)
  28. {
  29. void __iomem * addr;
  30. struct vm_struct * area;
  31. unsigned long offset, last_addr;
  32. /* Don't allow wraparound or zero size */
  33. last_addr = phys_addr + size - 1;
  34. if (!size || last_addr < phys_addr)
  35. return NULL;
  36. /*
  37. * Mappings have to be page-aligned
  38. */
  39. offset = phys_addr & ~PAGE_MASK;
  40. phys_addr &= PAGE_MASK;
  41. size = PAGE_ALIGN(last_addr+1) - phys_addr;
  42. /*
  43. * Ok, go for it..
  44. */
  45. area = get_vm_area(size, VM_IOREMAP);
  46. if (!area)
  47. return NULL;
  48. addr = (void __iomem *)area->addr;
  49. if (ioremap_page_range((unsigned long)addr, (unsigned long)addr + size,
  50. phys_addr, prot)) {
  51. vfree((void __force *)addr);
  52. return NULL;
  53. }
  54. return (void __iomem *) (offset + (char __iomem *)addr);
  55. }
  56. void __iomem * __ioremap(unsigned long phys_addr, unsigned long size, unsigned long flags)
  57. {
  58. return __ioremap_prot(phys_addr, size,
  59. __pgprot(_PAGE_PRESENT | __READABLE |
  60. __WRITEABLE | _PAGE_GLOBAL |
  61. _PAGE_KERNEL | flags));
  62. }
  63. /**
  64. * ioremap_nocache - map bus memory into CPU space
  65. * @offset: bus address of the memory
  66. * @size: size of the resource to map
  67. *
  68. * Must be freed with iounmap.
  69. */
  70. void __iomem *ioremap_nocache(unsigned long phys_addr, unsigned long size)
  71. {
  72. return __ioremap(phys_addr | MEM_NON_CACHEABLE, size, 0);
  73. }
  74. EXPORT_SYMBOL(ioremap_nocache);
  75. void iounmap(volatile void __iomem *addr)
  76. {
  77. if (addr > high_memory)
  78. return vfree((void *) (PAGE_MASK & (unsigned long) addr));
  79. }