ioremap.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * ioremap.c
  3. *
  4. * Support for mapping between dma_addr_t values a phys_addr_t values.
  5. *
  6. * Copyright (C) 2005-2009 Scientific-Atlanta, Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Author: David VomLehn <dvomlehn@cisco.com>
  23. *
  24. * Description: Defines the platform resources for the SA settop.
  25. *
  26. * NOTE: The bootloader allocates persistent memory at an address which is
  27. * 16 MiB below the end of the highest address in KSEG0. All fixed
  28. * address memory reservations must avoid this region.
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <asm/mach-powertv/ioremap.h>
  33. /*
  34. * Define the sizes of and masks for grains in physical and DMA space. The
  35. * values are the same but the types are not.
  36. */
  37. #define IOR_PHYS_GRAIN ((phys_addr_t) 1 << IOR_LSBITS)
  38. #define IOR_PHYS_GRAIN_MASK (IOR_PHYS_GRAIN - 1)
  39. #define IOR_DMA_GRAIN ((dma_addr_t) 1 << IOR_LSBITS)
  40. #define IOR_DMA_GRAIN_MASK (IOR_DMA_GRAIN - 1)
  41. /*
  42. * Values that, when accessed by an index derived from a phys_addr_t and
  43. * added to phys_addr_t value, yield a DMA address
  44. */
  45. struct ior_phys_to_dma _ior_phys_to_dma[IOR_NUM_PHYS_TO_DMA];
  46. EXPORT_SYMBOL(_ior_phys_to_dma);
  47. /*
  48. * Values that, when accessed by an index derived from a dma_addr_t and
  49. * added to that dma_addr_t value, yield a physical address
  50. */
  51. struct ior_dma_to_phys _ior_dma_to_phys[IOR_NUM_DMA_TO_PHYS];
  52. EXPORT_SYMBOL(_ior_dma_to_phys);
  53. /**
  54. * setup_dma_to_phys - set up conversion from DMA to physical addresses
  55. * @dma_idx: Top IOR_LSBITS bits of the DMA address, i.e. an index
  56. * into the array _dma_to_phys.
  57. * @delta: Value that, when added to the DMA address, will yield the
  58. * physical address
  59. * @s: Number of bytes in the section of memory with the given delta
  60. * between DMA and physical addresses.
  61. */
  62. static void setup_dma_to_phys(dma_addr_t dma, phys_addr_t delta, dma_addr_t s)
  63. {
  64. int dma_idx, first_idx, last_idx;
  65. phys_addr_t first, last;
  66. /*
  67. * Calculate the first and last indices, rounding the first up and
  68. * the second down.
  69. */
  70. first = dma & ~IOR_DMA_GRAIN_MASK;
  71. last = (dma + s - 1) & ~IOR_DMA_GRAIN_MASK;
  72. first_idx = first >> IOR_LSBITS; /* Convert to indices */
  73. last_idx = last >> IOR_LSBITS;
  74. for (dma_idx = first_idx; dma_idx <= last_idx; dma_idx++)
  75. _ior_dma_to_phys[dma_idx].offset = delta >> IOR_DMA_SHIFT;
  76. }
  77. /**
  78. * setup_phys_to_dma - set up conversion from DMA to physical addresses
  79. * @phys_idx: Top IOR_LSBITS bits of the DMA address, i.e. an index
  80. * into the array _phys_to_dma.
  81. * @delta: Value that, when added to the DMA address, will yield the
  82. * physical address
  83. * @s: Number of bytes in the section of memory with the given delta
  84. * between DMA and physical addresses.
  85. */
  86. static void setup_phys_to_dma(phys_addr_t phys, dma_addr_t delta, phys_addr_t s)
  87. {
  88. int phys_idx, first_idx, last_idx;
  89. phys_addr_t first, last;
  90. /*
  91. * Calculate the first and last indices, rounding the first up and
  92. * the second down.
  93. */
  94. first = phys & ~IOR_PHYS_GRAIN_MASK;
  95. last = (phys + s - 1) & ~IOR_PHYS_GRAIN_MASK;
  96. first_idx = first >> IOR_LSBITS; /* Convert to indices */
  97. last_idx = last >> IOR_LSBITS;
  98. for (phys_idx = first_idx; phys_idx <= last_idx; phys_idx++)
  99. _ior_phys_to_dma[phys_idx].offset = delta >> IOR_PHYS_SHIFT;
  100. }
  101. /**
  102. * ioremap_add_map - add to the physical and DMA address conversion arrays
  103. * @phys: Process's view of the address of the start of the memory chunk
  104. * @dma: DMA address of the start of the memory chunk
  105. * @size: Size, in bytes, of the chunk of memory
  106. *
  107. * NOTE: It might be obvious, but the assumption is that all @size bytes have
  108. * the same offset between the physical address and the DMA address.
  109. */
  110. void ioremap_add_map(phys_addr_t phys, phys_addr_t dma, phys_addr_t size)
  111. {
  112. if (size == 0)
  113. return;
  114. if ((dma & IOR_DMA_GRAIN_MASK) != 0 ||
  115. (phys & IOR_PHYS_GRAIN_MASK) != 0 ||
  116. (size & IOR_PHYS_GRAIN_MASK) != 0)
  117. pr_crit("Memory allocation must be in chunks of 0x%x bytes\n",
  118. IOR_PHYS_GRAIN);
  119. setup_dma_to_phys(dma, phys - dma, size);
  120. setup_phys_to_dma(phys, dma - phys, size);
  121. }