pcibr_dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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) 2001-2005 Silicon Graphics, Inc. All rights reserved.
  7. */
  8. #include <linux/types.h>
  9. #include <linux/pci.h>
  10. #include <asm/sn/addrs.h>
  11. #include <asm/sn/geo.h>
  12. #include <asm/sn/pcibr_provider.h>
  13. #include <asm/sn/pcibus_provider_defs.h>
  14. #include <asm/sn/pcidev.h>
  15. #include <asm/sn/pic.h>
  16. #include <asm/sn/sn_sal.h>
  17. #include <asm/sn/tiocp.h>
  18. #include "tio.h"
  19. #include "xtalk/xwidgetdev.h"
  20. #include "xtalk/hubdev.h"
  21. extern int sn_ioif_inited;
  22. /* =====================================================================
  23. * DMA MANAGEMENT
  24. *
  25. * The Bridge ASIC provides three methods of doing DMA: via a "direct map"
  26. * register available in 32-bit PCI space (which selects a contiguous 2G
  27. * address space on some other widget), via "direct" addressing via 64-bit
  28. * PCI space (all destination information comes from the PCI address,
  29. * including transfer attributes), and via a "mapped" region that allows
  30. * a bunch of different small mappings to be established with the PMU.
  31. *
  32. * For efficiency, we most prefer to use the 32bit direct mapping facility,
  33. * since it requires no resource allocations. The advantage of using the
  34. * PMU over the 64-bit direct is that single-cycle PCI addressing can be
  35. * used; the advantage of using 64-bit direct over PMU addressing is that
  36. * we do not have to allocate entries in the PMU.
  37. */
  38. static dma_addr_t
  39. pcibr_dmamap_ate32(struct pcidev_info *info,
  40. u64 paddr, size_t req_size, u64 flags, int dma_flags)
  41. {
  42. struct pcidev_info *pcidev_info = info->pdi_host_pcidev_info;
  43. struct pcibus_info *pcibus_info = (struct pcibus_info *)pcidev_info->
  44. pdi_pcibus_info;
  45. u8 internal_device = (PCI_SLOT(pcidev_info->pdi_host_pcidev_info->
  46. pdi_linux_pcidev->devfn)) - 1;
  47. int ate_count;
  48. int ate_index;
  49. u64 ate_flags = flags | PCI32_ATE_V;
  50. u64 ate;
  51. u64 pci_addr;
  52. u64 xio_addr;
  53. u64 offset;
  54. /* PIC in PCI-X mode does not supports 32bit PageMap mode */
  55. if (IS_PIC_SOFT(pcibus_info) && IS_PCIX(pcibus_info)) {
  56. return 0;
  57. }
  58. /* Calculate the number of ATEs needed. */
  59. if (!(MINIMAL_ATE_FLAG(paddr, req_size))) {
  60. ate_count = IOPG((IOPGSIZE - 1) /* worst case start offset */
  61. +req_size /* max mapping bytes */
  62. - 1) + 1; /* round UP */
  63. } else { /* assume requested target is page aligned */
  64. ate_count = IOPG(req_size /* max mapping bytes */
  65. - 1) + 1; /* round UP */
  66. }
  67. /* Get the number of ATEs required. */
  68. ate_index = pcibr_ate_alloc(pcibus_info, ate_count);
  69. if (ate_index < 0)
  70. return 0;
  71. /* In PCI-X mode, Prefetch not supported */
  72. if (IS_PCIX(pcibus_info))
  73. ate_flags &= ~(PCI32_ATE_PREF);
  74. if (SN_DMA_ADDRTYPE(dma_flags == SN_DMA_ADDR_PHYS))
  75. xio_addr = IS_PIC_SOFT(pcibus_info) ? PHYS_TO_DMA(paddr) :
  76. PHYS_TO_TIODMA(paddr);
  77. else
  78. xio_addr = paddr;
  79. offset = IOPGOFF(xio_addr);
  80. ate = ate_flags | (xio_addr - offset);
  81. /* If PIC, put the targetid in the ATE */
  82. if (IS_PIC_SOFT(pcibus_info)) {
  83. ate |= (pcibus_info->pbi_hub_xid << PIC_ATE_TARGETID_SHFT);
  84. }
  85. /*
  86. * If we're mapping for MSI, set the MSI bit in the ATE. If it's a
  87. * TIOCP based pci bus, we also need to set the PIO bit in the ATE.
  88. */
  89. if (dma_flags & SN_DMA_MSI) {
  90. ate |= PCI32_ATE_MSI;
  91. if (IS_TIOCP_SOFT(pcibus_info))
  92. ate |= PCI32_ATE_PIO;
  93. }
  94. ate_write(pcibus_info, ate_index, ate_count, ate);
  95. /*
  96. * Set up the DMA mapped Address.
  97. */
  98. pci_addr = PCI32_MAPPED_BASE + offset + IOPGSIZE * ate_index;
  99. /*
  100. * If swap was set in device in pcibr_endian_set()
  101. * we need to turn swapping on.
  102. */
  103. if (pcibus_info->pbi_devreg[internal_device] & PCIBR_DEV_SWAP_DIR)
  104. ATE_SWAP_ON(pci_addr);
  105. return pci_addr;
  106. }
  107. static dma_addr_t
  108. pcibr_dmatrans_direct64(struct pcidev_info * info, u64 paddr,
  109. u64 dma_attributes, int dma_flags)
  110. {
  111. struct pcibus_info *pcibus_info = (struct pcibus_info *)
  112. ((info->pdi_host_pcidev_info)->pdi_pcibus_info);
  113. u64 pci_addr;
  114. /* Translate to Crosstalk View of Physical Address */
  115. if (SN_DMA_ADDRTYPE(dma_flags) == SN_DMA_ADDR_PHYS)
  116. pci_addr = IS_PIC_SOFT(pcibus_info) ?
  117. PHYS_TO_DMA(paddr) :
  118. PHYS_TO_TIODMA(paddr);
  119. else
  120. pci_addr = paddr;
  121. pci_addr |= dma_attributes;
  122. /* Handle Bus mode */
  123. if (IS_PCIX(pcibus_info))
  124. pci_addr &= ~PCI64_ATTR_PREF;
  125. /* Handle Bridge Chipset differences */
  126. if (IS_PIC_SOFT(pcibus_info)) {
  127. pci_addr |=
  128. ((u64) pcibus_info->
  129. pbi_hub_xid << PIC_PCI64_ATTR_TARG_SHFT);
  130. } else
  131. pci_addr |= (dma_flags & SN_DMA_MSI) ?
  132. TIOCP_PCI64_CMDTYPE_MSI :
  133. TIOCP_PCI64_CMDTYPE_MEM;
  134. /* If PCI mode, func zero uses VCHAN0, every other func uses VCHAN1 */
  135. if (!IS_PCIX(pcibus_info) && PCI_FUNC(info->pdi_linux_pcidev->devfn))
  136. pci_addr |= PCI64_ATTR_VIRTUAL;
  137. return pci_addr;
  138. }
  139. static dma_addr_t
  140. pcibr_dmatrans_direct32(struct pcidev_info * info,
  141. u64 paddr, size_t req_size, u64 flags, int dma_flags)
  142. {
  143. struct pcidev_info *pcidev_info = info->pdi_host_pcidev_info;
  144. struct pcibus_info *pcibus_info = (struct pcibus_info *)pcidev_info->
  145. pdi_pcibus_info;
  146. u64 xio_addr;
  147. u64 xio_base;
  148. u64 offset;
  149. u64 endoff;
  150. if (IS_PCIX(pcibus_info)) {
  151. return 0;
  152. }
  153. if (dma_flags & SN_DMA_MSI)
  154. return 0;
  155. if (SN_DMA_ADDRTYPE(dma_flags) == SN_DMA_ADDR_PHYS)
  156. xio_addr = IS_PIC_SOFT(pcibus_info) ? PHYS_TO_DMA(paddr) :
  157. PHYS_TO_TIODMA(paddr);
  158. else
  159. xio_addr = paddr;
  160. xio_base = pcibus_info->pbi_dir_xbase;
  161. offset = xio_addr - xio_base;
  162. endoff = req_size + offset;
  163. if ((req_size > (1ULL << 31)) || /* Too Big */
  164. (xio_addr < xio_base) || /* Out of range for mappings */
  165. (endoff > (1ULL << 31))) { /* Too Big */
  166. return 0;
  167. }
  168. return PCI32_DIRECT_BASE | offset;
  169. }
  170. /*
  171. * Wrapper routine for freeing DMA maps
  172. * DMA mappings for Direct 64 and 32 do not have any DMA maps.
  173. */
  174. void
  175. pcibr_dma_unmap(struct pci_dev *hwdev, dma_addr_t dma_handle, int direction)
  176. {
  177. struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
  178. struct pcibus_info *pcibus_info =
  179. (struct pcibus_info *)pcidev_info->pdi_pcibus_info;
  180. if (IS_PCI32_MAPPED(dma_handle)) {
  181. int ate_index;
  182. ate_index =
  183. IOPG((ATE_SWAP_OFF(dma_handle) - PCI32_MAPPED_BASE));
  184. pcibr_ate_free(pcibus_info, ate_index);
  185. }
  186. }
  187. /*
  188. * On SN systems there is a race condition between a PIO read response and
  189. * DMA's. In rare cases, the read response may beat the DMA, causing the
  190. * driver to think that data in memory is complete and meaningful. This code
  191. * eliminates that race. This routine is called by the PIO read routines
  192. * after doing the read. For PIC this routine then forces a fake interrupt
  193. * on another line, which is logically associated with the slot that the PIO
  194. * is addressed to. It then spins while watching the memory location that
  195. * the interrupt is targetted to. When the interrupt response arrives, we
  196. * are sure that the DMA has landed in memory and it is safe for the driver
  197. * to proceed. For TIOCP use the Device(x) Write Request Buffer Flush
  198. * Bridge register since it ensures the data has entered the coherence domain,
  199. * unlike the PIC Device(x) Write Request Buffer Flush register.
  200. */
  201. void sn_dma_flush(u64 addr)
  202. {
  203. nasid_t nasid;
  204. int is_tio;
  205. int wid_num;
  206. int i, j;
  207. unsigned long flags;
  208. u64 itte;
  209. struct hubdev_info *hubinfo;
  210. struct sn_flush_device_kernel *p;
  211. struct sn_flush_device_common *common;
  212. struct sn_flush_nasid_entry *flush_nasid_list;
  213. if (!sn_ioif_inited)
  214. return;
  215. nasid = NASID_GET(addr);
  216. if (-1 == nasid_to_cnodeid(nasid))
  217. return;
  218. hubinfo = (NODEPDA(nasid_to_cnodeid(nasid)))->pdinfo;
  219. BUG_ON(!hubinfo);
  220. flush_nasid_list = &hubinfo->hdi_flush_nasid_list;
  221. if (flush_nasid_list->widget_p == NULL)
  222. return;
  223. is_tio = (nasid & 1);
  224. if (is_tio) {
  225. int itte_index;
  226. if (TIO_HWIN(addr))
  227. itte_index = 0;
  228. else if (TIO_BWIN_WINDOWNUM(addr))
  229. itte_index = TIO_BWIN_WINDOWNUM(addr);
  230. else
  231. itte_index = -1;
  232. if (itte_index >= 0) {
  233. itte = flush_nasid_list->iio_itte[itte_index];
  234. if (! TIO_ITTE_VALID(itte))
  235. return;
  236. wid_num = TIO_ITTE_WIDGET(itte);
  237. } else
  238. wid_num = TIO_SWIN_WIDGETNUM(addr);
  239. } else {
  240. if (BWIN_WINDOWNUM(addr)) {
  241. itte = flush_nasid_list->iio_itte[BWIN_WINDOWNUM(addr)];
  242. wid_num = IIO_ITTE_WIDGET(itte);
  243. } else
  244. wid_num = SWIN_WIDGETNUM(addr);
  245. }
  246. if (flush_nasid_list->widget_p[wid_num] == NULL)
  247. return;
  248. p = &flush_nasid_list->widget_p[wid_num][0];
  249. /* find a matching BAR */
  250. for (i = 0; i < DEV_PER_WIDGET; i++,p++) {
  251. common = p->common;
  252. for (j = 0; j < PCI_ROM_RESOURCE; j++) {
  253. if (common->sfdl_bar_list[j].start == 0)
  254. break;
  255. if (addr >= common->sfdl_bar_list[j].start
  256. && addr <= common->sfdl_bar_list[j].end)
  257. break;
  258. }
  259. if (j < PCI_ROM_RESOURCE && common->sfdl_bar_list[j].start != 0)
  260. break;
  261. }
  262. /* if no matching BAR, return without doing anything. */
  263. if (i == DEV_PER_WIDGET)
  264. return;
  265. /*
  266. * For TIOCP use the Device(x) Write Request Buffer Flush Bridge
  267. * register since it ensures the data has entered the coherence
  268. * domain, unlike PIC.
  269. */
  270. if (is_tio) {
  271. /*
  272. * Note: devices behind TIOCE should never be matched in the
  273. * above code, and so the following code is PIC/CP centric.
  274. * If CE ever needs the sn_dma_flush mechanism, we will have
  275. * to account for that here and in tioce_bus_fixup().
  276. */
  277. u32 tio_id = HUB_L(TIO_IOSPACE_ADDR(nasid, TIO_NODE_ID));
  278. u32 revnum = XWIDGET_PART_REV_NUM(tio_id);
  279. /* TIOCP BRINGUP WAR (PV907516): Don't write buffer flush reg */
  280. if ((1 << XWIDGET_PART_REV_NUM_REV(revnum)) & PV907516) {
  281. return;
  282. } else {
  283. pcireg_wrb_flush_get(common->sfdl_pcibus_info,
  284. (common->sfdl_slot - 1));
  285. }
  286. } else {
  287. spin_lock_irqsave(&p->sfdl_flush_lock, flags);
  288. *common->sfdl_flush_addr = 0;
  289. /* force an interrupt. */
  290. *(volatile u32 *)(common->sfdl_force_int_addr) = 1;
  291. /* wait for the interrupt to come back. */
  292. while (*(common->sfdl_flush_addr) != 0x10f)
  293. cpu_relax();
  294. /* okay, everything is synched up. */
  295. spin_unlock_irqrestore(&p->sfdl_flush_lock, flags);
  296. }
  297. return;
  298. }
  299. /*
  300. * DMA interfaces. Called from pci_dma.c routines.
  301. */
  302. dma_addr_t
  303. pcibr_dma_map(struct pci_dev * hwdev, unsigned long phys_addr, size_t size, int dma_flags)
  304. {
  305. dma_addr_t dma_handle;
  306. struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
  307. /* SN cannot support DMA addresses smaller than 32 bits. */
  308. if (hwdev->dma_mask < 0x7fffffff) {
  309. return 0;
  310. }
  311. if (hwdev->dma_mask == ~0UL) {
  312. /*
  313. * Handle the most common case: 64 bit cards. This
  314. * call should always succeed.
  315. */
  316. dma_handle = pcibr_dmatrans_direct64(pcidev_info, phys_addr,
  317. PCI64_ATTR_PREF, dma_flags);
  318. } else {
  319. /* Handle 32-63 bit cards via direct mapping */
  320. dma_handle = pcibr_dmatrans_direct32(pcidev_info, phys_addr,
  321. size, 0, dma_flags);
  322. if (!dma_handle) {
  323. /*
  324. * It is a 32 bit card and we cannot do direct mapping,
  325. * so we use an ATE.
  326. */
  327. dma_handle = pcibr_dmamap_ate32(pcidev_info, phys_addr,
  328. size, PCI32_ATE_PREF,
  329. dma_flags);
  330. }
  331. }
  332. return dma_handle;
  333. }
  334. dma_addr_t
  335. pcibr_dma_map_consistent(struct pci_dev * hwdev, unsigned long phys_addr,
  336. size_t size, int dma_flags)
  337. {
  338. dma_addr_t dma_handle;
  339. struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
  340. if (hwdev->dev.coherent_dma_mask == ~0UL) {
  341. dma_handle = pcibr_dmatrans_direct64(pcidev_info, phys_addr,
  342. PCI64_ATTR_BAR, dma_flags);
  343. } else {
  344. dma_handle = (dma_addr_t) pcibr_dmamap_ate32(pcidev_info,
  345. phys_addr, size,
  346. PCI32_ATE_BAR, dma_flags);
  347. }
  348. return dma_handle;
  349. }
  350. EXPORT_SYMBOL(sn_dma_flush);