dma-octeon.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2000 Ani Joshi <ajoshi@unixbox.com>
  7. * Copyright (C) 2000, 2001 Ralf Baechle <ralf@gnu.org>
  8. * Copyright (C) 2005 Ilya A. Volynets-Evenbakh <ilya@total-knowledge.com>
  9. * swiped from i386, and cloned for MIPS by Geert, polished by Ralf.
  10. * IP32 changes by Ilya.
  11. * Cavium Networks: Create new dma setup for Cavium Networks Octeon based on
  12. * the kernels original.
  13. */
  14. #include <linux/types.h>
  15. #include <linux/mm.h>
  16. #include <linux/module.h>
  17. #include <linux/string.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/scatterlist.h>
  21. #include <linux/cache.h>
  22. #include <linux/io.h>
  23. #include <asm/octeon/octeon.h>
  24. #include <asm/octeon/cvmx-npi-defs.h>
  25. #include <asm/octeon/cvmx-pci-defs.h>
  26. #include <dma-coherence.h>
  27. #ifdef CONFIG_PCI
  28. #include <asm/octeon/pci-octeon.h>
  29. #endif
  30. #define BAR2_PCI_ADDRESS 0x8000000000ul
  31. struct bar1_index_state {
  32. int16_t ref_count; /* Number of PCI mappings using this index */
  33. uint16_t address_bits; /* Upper bits of physical address. This is
  34. shifted 22 bits */
  35. };
  36. #ifdef CONFIG_PCI
  37. static DEFINE_SPINLOCK(bar1_lock);
  38. static struct bar1_index_state bar1_state[32];
  39. #endif
  40. dma_addr_t octeon_map_dma_mem(struct device *dev, void *ptr, size_t size)
  41. {
  42. #ifndef CONFIG_PCI
  43. /* Without PCI/PCIe this function can be called for Octeon internal
  44. devices such as USB. These devices all support 64bit addressing */
  45. mb();
  46. return virt_to_phys(ptr);
  47. #else
  48. unsigned long flags;
  49. uint64_t dma_mask;
  50. int64_t start_index;
  51. dma_addr_t result = -1;
  52. uint64_t physical = virt_to_phys(ptr);
  53. int64_t index;
  54. mb();
  55. /*
  56. * Use the DMA masks to determine the allowed memory
  57. * region. For us it doesn't limit the actual memory, just the
  58. * address visible over PCI. Devices with limits need to use
  59. * lower indexed Bar1 entries.
  60. */
  61. if (dev) {
  62. dma_mask = dev->coherent_dma_mask;
  63. if (dev->dma_mask)
  64. dma_mask = *dev->dma_mask;
  65. } else {
  66. dma_mask = 0xfffffffful;
  67. }
  68. /*
  69. * Platform devices, such as the internal USB, skip all
  70. * translation and use Octeon physical addresses directly.
  71. */
  72. if (!dev || dev->bus == &platform_bus_type)
  73. return physical;
  74. switch (octeon_dma_bar_type) {
  75. case OCTEON_DMA_BAR_TYPE_PCIE:
  76. if (unlikely(physical < (16ul << 10)))
  77. panic("dma_map_single: Not allowed to map first 16KB."
  78. " It interferes with BAR0 special area\n");
  79. else if ((physical + size >= (256ul << 20)) &&
  80. (physical < (512ul << 20)))
  81. panic("dma_map_single: Not allowed to map bootbus\n");
  82. else if ((physical + size >= 0x400000000ull) &&
  83. physical < 0x410000000ull)
  84. panic("dma_map_single: "
  85. "Attempt to map illegal memory address 0x%llx\n",
  86. physical);
  87. else if (physical >= 0x420000000ull)
  88. panic("dma_map_single: "
  89. "Attempt to map illegal memory address 0x%llx\n",
  90. physical);
  91. else if ((physical + size >=
  92. (4ull<<30) - (OCTEON_PCI_BAR1_HOLE_SIZE<<20))
  93. && physical < (4ull<<30))
  94. pr_warning("dma_map_single: Warning: "
  95. "Mapping memory address that might "
  96. "conflict with devices 0x%llx-0x%llx\n",
  97. physical, physical+size-1);
  98. /* The 2nd 256MB is mapped at 256<<20 instead of 0x410000000 */
  99. if ((physical >= 0x410000000ull) && physical < 0x420000000ull)
  100. result = physical - 0x400000000ull;
  101. else
  102. result = physical;
  103. if (((result+size-1) & dma_mask) != result+size-1)
  104. panic("dma_map_single: Attempt to map address "
  105. "0x%llx-0x%llx, which can't be accessed "
  106. "according to the dma mask 0x%llx\n",
  107. physical, physical+size-1, dma_mask);
  108. goto done;
  109. case OCTEON_DMA_BAR_TYPE_BIG:
  110. #ifdef CONFIG_64BIT
  111. /* If the device supports 64bit addressing, then use BAR2 */
  112. if (dma_mask > BAR2_PCI_ADDRESS) {
  113. result = physical + BAR2_PCI_ADDRESS;
  114. goto done;
  115. }
  116. #endif
  117. if (unlikely(physical < (4ul << 10))) {
  118. panic("dma_map_single: Not allowed to map first 4KB. "
  119. "It interferes with BAR0 special area\n");
  120. } else if (physical < (256ul << 20)) {
  121. if (unlikely(physical + size > (256ul << 20)))
  122. panic("dma_map_single: Requested memory spans "
  123. "Bar0 0:256MB and bootbus\n");
  124. result = physical;
  125. goto done;
  126. } else if (unlikely(physical < (512ul << 20))) {
  127. panic("dma_map_single: Not allowed to map bootbus\n");
  128. } else if (physical < (2ul << 30)) {
  129. if (unlikely(physical + size > (2ul << 30)))
  130. panic("dma_map_single: Requested memory spans "
  131. "Bar0 512MB:2GB and BAR1\n");
  132. result = physical;
  133. goto done;
  134. } else if (physical < (2ul << 30) + (128 << 20)) {
  135. /* Fall through */
  136. } else if (physical <
  137. (4ul << 30) - (OCTEON_PCI_BAR1_HOLE_SIZE << 20)) {
  138. if (unlikely
  139. (physical + size >
  140. (4ul << 30) - (OCTEON_PCI_BAR1_HOLE_SIZE << 20)))
  141. panic("dma_map_single: Requested memory "
  142. "extends past Bar1 (4GB-%luMB)\n",
  143. OCTEON_PCI_BAR1_HOLE_SIZE);
  144. result = physical;
  145. goto done;
  146. } else if ((physical >= 0x410000000ull) &&
  147. (physical < 0x420000000ull)) {
  148. if (unlikely(physical + size > 0x420000000ull))
  149. panic("dma_map_single: Requested memory spans "
  150. "non existant memory\n");
  151. /* BAR0 fixed mapping 256MB:512MB ->
  152. * 16GB+256MB:16GB+512MB */
  153. result = physical - 0x400000000ull;
  154. goto done;
  155. } else {
  156. /* Continued below switch statement */
  157. }
  158. break;
  159. case OCTEON_DMA_BAR_TYPE_SMALL:
  160. #ifdef CONFIG_64BIT
  161. /* If the device supports 64bit addressing, then use BAR2 */
  162. if (dma_mask > BAR2_PCI_ADDRESS) {
  163. result = physical + BAR2_PCI_ADDRESS;
  164. goto done;
  165. }
  166. #endif
  167. /* Continued below switch statement */
  168. break;
  169. default:
  170. panic("dma_map_single: Invalid octeon_dma_bar_type\n");
  171. }
  172. /* Don't allow mapping to span multiple Bar entries. The hardware guys
  173. won't guarantee that DMA across boards work */
  174. if (unlikely((physical >> 22) != ((physical + size - 1) >> 22)))
  175. panic("dma_map_single: "
  176. "Requested memory spans more than one Bar1 entry\n");
  177. if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_BIG)
  178. start_index = 31;
  179. else if (unlikely(dma_mask < (1ul << 27)))
  180. start_index = (dma_mask >> 22);
  181. else
  182. start_index = 31;
  183. /* Only one processor can access the Bar register at once */
  184. spin_lock_irqsave(&bar1_lock, flags);
  185. /* Look through Bar1 for existing mapping that will work */
  186. for (index = start_index; index >= 0; index--) {
  187. if ((bar1_state[index].address_bits == physical >> 22) &&
  188. (bar1_state[index].ref_count)) {
  189. /* An existing mapping will work, use it */
  190. bar1_state[index].ref_count++;
  191. if (unlikely(bar1_state[index].ref_count < 0))
  192. panic("dma_map_single: "
  193. "Bar1[%d] reference count overflowed\n",
  194. (int) index);
  195. result = (index << 22) | (physical & ((1 << 22) - 1));
  196. /* Large BAR1 is offset at 2GB */
  197. if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_BIG)
  198. result += 2ul << 30;
  199. goto done_unlock;
  200. }
  201. }
  202. /* No existing mappings, look for a free entry */
  203. for (index = start_index; index >= 0; index--) {
  204. if (unlikely(bar1_state[index].ref_count == 0)) {
  205. union cvmx_pci_bar1_indexx bar1_index;
  206. /* We have a free entry, use it */
  207. bar1_state[index].ref_count = 1;
  208. bar1_state[index].address_bits = physical >> 22;
  209. bar1_index.u32 = 0;
  210. /* Address bits[35:22] sent to L2C */
  211. bar1_index.s.addr_idx = physical >> 22;
  212. /* Don't put PCI accesses in L2. */
  213. bar1_index.s.ca = 1;
  214. /* Endian Swap Mode */
  215. bar1_index.s.end_swp = 1;
  216. /* Set '1' when the selected address range is valid. */
  217. bar1_index.s.addr_v = 1;
  218. octeon_npi_write32(CVMX_NPI_PCI_BAR1_INDEXX(index),
  219. bar1_index.u32);
  220. /* An existing mapping will work, use it */
  221. result = (index << 22) | (physical & ((1 << 22) - 1));
  222. /* Large BAR1 is offset at 2GB */
  223. if (octeon_dma_bar_type == OCTEON_DMA_BAR_TYPE_BIG)
  224. result += 2ul << 30;
  225. goto done_unlock;
  226. }
  227. }
  228. pr_err("dma_map_single: "
  229. "Can't find empty BAR1 index for physical mapping 0x%llx\n",
  230. (unsigned long long) physical);
  231. done_unlock:
  232. spin_unlock_irqrestore(&bar1_lock, flags);
  233. done:
  234. pr_debug("dma_map_single 0x%llx->0x%llx\n", physical, result);
  235. return result;
  236. #endif
  237. }
  238. void octeon_unmap_dma_mem(struct device *dev, dma_addr_t dma_addr)
  239. {
  240. #ifndef CONFIG_PCI
  241. /*
  242. * Without PCI/PCIe this function can be called for Octeon internal
  243. * devices such as USB. These devices all support 64bit addressing.
  244. */
  245. return;
  246. #else
  247. unsigned long flags;
  248. uint64_t index;
  249. /*
  250. * Platform devices, such as the internal USB, skip all
  251. * translation and use Octeon physical addresses directly.
  252. */
  253. if (dev->bus == &platform_bus_type)
  254. return;
  255. switch (octeon_dma_bar_type) {
  256. case OCTEON_DMA_BAR_TYPE_PCIE:
  257. /* Nothing to do, all mappings are static */
  258. goto done;
  259. case OCTEON_DMA_BAR_TYPE_BIG:
  260. #ifdef CONFIG_64BIT
  261. /* Nothing to do for addresses using BAR2 */
  262. if (dma_addr >= BAR2_PCI_ADDRESS)
  263. goto done;
  264. #endif
  265. if (unlikely(dma_addr < (4ul << 10)))
  266. panic("dma_unmap_single: Unexpect DMA address 0x%llx\n",
  267. dma_addr);
  268. else if (dma_addr < (2ul << 30))
  269. /* Nothing to do for addresses using BAR0 */
  270. goto done;
  271. else if (dma_addr < (2ul << 30) + (128ul << 20))
  272. /* Need to unmap, fall through */
  273. index = (dma_addr - (2ul << 30)) >> 22;
  274. else if (dma_addr <
  275. (4ul << 30) - (OCTEON_PCI_BAR1_HOLE_SIZE << 20))
  276. goto done; /* Nothing to do for the rest of BAR1 */
  277. else
  278. panic("dma_unmap_single: Unexpect DMA address 0x%llx\n",
  279. dma_addr);
  280. /* Continued below switch statement */
  281. break;
  282. case OCTEON_DMA_BAR_TYPE_SMALL:
  283. #ifdef CONFIG_64BIT
  284. /* Nothing to do for addresses using BAR2 */
  285. if (dma_addr >= BAR2_PCI_ADDRESS)
  286. goto done;
  287. #endif
  288. index = dma_addr >> 22;
  289. /* Continued below switch statement */
  290. break;
  291. default:
  292. panic("dma_unmap_single: Invalid octeon_dma_bar_type\n");
  293. }
  294. if (unlikely(index > 31))
  295. panic("dma_unmap_single: "
  296. "Attempt to unmap an invalid address (0x%llx)\n",
  297. dma_addr);
  298. spin_lock_irqsave(&bar1_lock, flags);
  299. bar1_state[index].ref_count--;
  300. if (bar1_state[index].ref_count == 0)
  301. octeon_npi_write32(CVMX_NPI_PCI_BAR1_INDEXX(index), 0);
  302. else if (unlikely(bar1_state[index].ref_count < 0))
  303. panic("dma_unmap_single: Bar1[%u] reference count < 0\n",
  304. (int) index);
  305. spin_unlock_irqrestore(&bar1_lock, flags);
  306. done:
  307. pr_debug("dma_unmap_single 0x%llx\n", dma_addr);
  308. return;
  309. #endif
  310. }