pcibr_dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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
  87. */
  88. if (dma_flags & SN_DMA_MSI)
  89. ate |= PCI32_ATE_MSI;
  90. ate_write(pcibus_info, ate_index, ate_count, ate);
  91. /*
  92. * Set up the DMA mapped Address.
  93. */
  94. pci_addr = PCI32_MAPPED_BASE + offset + IOPGSIZE * ate_index;
  95. /*
  96. * If swap was set in device in pcibr_endian_set()
  97. * we need to turn swapping on.
  98. */
  99. if (pcibus_info->pbi_devreg[internal_device] & PCIBR_DEV_SWAP_DIR)
  100. ATE_SWAP_ON(pci_addr);
  101. return pci_addr;
  102. }
  103. static dma_addr_t
  104. pcibr_dmatrans_direct64(struct pcidev_info * info, u64 paddr,
  105. u64 dma_attributes, int dma_flags)
  106. {
  107. struct pcibus_info *pcibus_info = (struct pcibus_info *)
  108. ((info->pdi_host_pcidev_info)->pdi_pcibus_info);
  109. u64 pci_addr;
  110. /* Translate to Crosstalk View of Physical Address */
  111. if (SN_DMA_ADDRTYPE(dma_flags) == SN_DMA_ADDR_PHYS)
  112. pci_addr = IS_PIC_SOFT(pcibus_info) ?
  113. PHYS_TO_DMA(paddr) :
  114. PHYS_TO_TIODMA(paddr) | dma_attributes;
  115. else
  116. pci_addr = IS_PIC_SOFT(pcibus_info) ?
  117. paddr :
  118. paddr | dma_attributes;
  119. /* Handle Bus mode */
  120. if (IS_PCIX(pcibus_info))
  121. pci_addr &= ~PCI64_ATTR_PREF;
  122. /* Handle Bridge Chipset differences */
  123. if (IS_PIC_SOFT(pcibus_info)) {
  124. pci_addr |=
  125. ((u64) pcibus_info->
  126. pbi_hub_xid << PIC_PCI64_ATTR_TARG_SHFT);
  127. } else
  128. pci_addr |= (dma_flags & SN_DMA_MSI) ?
  129. TIOCP_PCI64_CMDTYPE_MSI :
  130. TIOCP_PCI64_CMDTYPE_MEM;
  131. /* If PCI mode, func zero uses VCHAN0, every other func uses VCHAN1 */
  132. if (!IS_PCIX(pcibus_info) && PCI_FUNC(info->pdi_linux_pcidev->devfn))
  133. pci_addr |= PCI64_ATTR_VIRTUAL;
  134. return pci_addr;
  135. }
  136. static dma_addr_t
  137. pcibr_dmatrans_direct32(struct pcidev_info * info,
  138. u64 paddr, size_t req_size, u64 flags, int dma_flags)
  139. {
  140. struct pcidev_info *pcidev_info = info->pdi_host_pcidev_info;
  141. struct pcibus_info *pcibus_info = (struct pcibus_info *)pcidev_info->
  142. pdi_pcibus_info;
  143. u64 xio_addr;
  144. u64 xio_base;
  145. u64 offset;
  146. u64 endoff;
  147. if (IS_PCIX(pcibus_info)) {
  148. return 0;
  149. }
  150. if (dma_flags & SN_DMA_MSI)
  151. return 0;
  152. if (SN_DMA_ADDRTYPE(dma_flags) == SN_DMA_ADDR_PHYS)
  153. xio_addr = IS_PIC_SOFT(pcibus_info) ? PHYS_TO_DMA(paddr) :
  154. PHYS_TO_TIODMA(paddr);
  155. else
  156. xio_addr = paddr;
  157. xio_base = pcibus_info->pbi_dir_xbase;
  158. offset = xio_addr - xio_base;
  159. endoff = req_size + offset;
  160. if ((req_size > (1ULL << 31)) || /* Too Big */
  161. (xio_addr < xio_base) || /* Out of range for mappings */
  162. (endoff > (1ULL << 31))) { /* Too Big */
  163. return 0;
  164. }
  165. return PCI32_DIRECT_BASE | offset;
  166. }
  167. /*
  168. * Wrapper routine for free'ing DMA maps
  169. * DMA mappings for Direct 64 and 32 do not have any DMA maps.
  170. */
  171. void
  172. pcibr_dma_unmap(struct pci_dev *hwdev, dma_addr_t dma_handle, int direction)
  173. {
  174. struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
  175. struct pcibus_info *pcibus_info =
  176. (struct pcibus_info *)pcidev_info->pdi_pcibus_info;
  177. if (IS_PCI32_MAPPED(dma_handle)) {
  178. int ate_index;
  179. ate_index =
  180. IOPG((ATE_SWAP_OFF(dma_handle) - PCI32_MAPPED_BASE));
  181. pcibr_ate_free(pcibus_info, ate_index);
  182. }
  183. }
  184. /*
  185. * On SN systems there is a race condition between a PIO read response and
  186. * DMA's. In rare cases, the read response may beat the DMA, causing the
  187. * driver to think that data in memory is complete and meaningful. This code
  188. * eliminates that race. This routine is called by the PIO read routines
  189. * after doing the read. For PIC this routine then forces a fake interrupt
  190. * on another line, which is logically associated with the slot that the PIO
  191. * is addressed to. It then spins while watching the memory location that
  192. * the interrupt is targetted to. When the interrupt response arrives, we
  193. * are sure that the DMA has landed in memory and it is safe for the driver
  194. * to proceed. For TIOCP use the Device(x) Write Request Buffer Flush
  195. * Bridge register since it ensures the data has entered the coherence domain,
  196. * unlike the PIC Device(x) Write Request Buffer Flush register.
  197. */
  198. void sn_dma_flush(u64 addr)
  199. {
  200. nasid_t nasid;
  201. int is_tio;
  202. int wid_num;
  203. int i, j;
  204. unsigned long flags;
  205. u64 itte;
  206. struct hubdev_info *hubinfo;
  207. struct sn_flush_device_kernel *p;
  208. struct sn_flush_device_common *common;
  209. struct sn_flush_nasid_entry *flush_nasid_list;
  210. if (!sn_ioif_inited)
  211. return;
  212. nasid = NASID_GET(addr);
  213. if (-1 == nasid_to_cnodeid(nasid))
  214. return;
  215. hubinfo = (NODEPDA(nasid_to_cnodeid(nasid)))->pdinfo;
  216. if (!hubinfo) {
  217. BUG();
  218. }
  219. flush_nasid_list = &hubinfo->hdi_flush_nasid_list;
  220. if (flush_nasid_list->widget_p == NULL)
  221. return;
  222. is_tio = (nasid & 1);
  223. if (is_tio) {
  224. int itte_index;
  225. if (TIO_HWIN(addr))
  226. itte_index = 0;
  227. else if (TIO_BWIN_WINDOWNUM(addr))
  228. itte_index = TIO_BWIN_WINDOWNUM(addr);
  229. else
  230. itte_index = -1;
  231. if (itte_index >= 0) {
  232. itte = flush_nasid_list->iio_itte[itte_index];
  233. if (! TIO_ITTE_VALID(itte))
  234. return;
  235. wid_num = TIO_ITTE_WIDGET(itte);
  236. } else
  237. wid_num = TIO_SWIN_WIDGETNUM(addr);
  238. } else {
  239. if (BWIN_WINDOWNUM(addr)) {
  240. itte = flush_nasid_list->iio_itte[BWIN_WINDOWNUM(addr)];
  241. wid_num = IIO_ITTE_WIDGET(itte);
  242. } else
  243. wid_num = SWIN_WIDGETNUM(addr);
  244. }
  245. if (flush_nasid_list->widget_p[wid_num] == NULL)
  246. return;
  247. p = &flush_nasid_list->widget_p[wid_num][0];
  248. /* find a matching BAR */
  249. for (i = 0; i < DEV_PER_WIDGET; i++,p++) {
  250. common = p->common;
  251. for (j = 0; j < PCI_ROM_RESOURCE; j++) {
  252. if (common->sfdl_bar_list[j].start == 0)
  253. break;
  254. if (addr >= common->sfdl_bar_list[j].start
  255. && addr <= common->sfdl_bar_list[j].end)
  256. break;
  257. }
  258. if (j < PCI_ROM_RESOURCE && common->sfdl_bar_list[j].start != 0)
  259. break;
  260. }
  261. /* if no matching BAR, return without doing anything. */
  262. if (i == DEV_PER_WIDGET)
  263. return;
  264. /*
  265. * For TIOCP use the Device(x) Write Request Buffer Flush Bridge
  266. * register since it ensures the data has entered the coherence
  267. * domain, unlike PIC.
  268. */
  269. if (is_tio) {
  270. /*
  271. * Note: devices behind TIOCE should never be matched in the
  272. * above code, and so the following code is PIC/CP centric.
  273. * If CE ever needs the sn_dma_flush mechanism, we will have
  274. * to account for that here and in tioce_bus_fixup().
  275. */
  276. u32 tio_id = HUB_L(TIO_IOSPACE_ADDR(nasid, TIO_NODE_ID));
  277. u32 revnum = XWIDGET_PART_REV_NUM(tio_id);
  278. /* TIOCP BRINGUP WAR (PV907516): Don't write buffer flush reg */
  279. if ((1 << XWIDGET_PART_REV_NUM_REV(revnum)) & PV907516) {
  280. return;
  281. } else {
  282. pcireg_wrb_flush_get(common->sfdl_pcibus_info,
  283. (common->sfdl_slot - 1));
  284. }
  285. } else {
  286. spin_lock_irqsave(&p->sfdl_flush_lock, flags);
  287. *common->sfdl_flush_addr = 0;
  288. /* force an interrupt. */
  289. *(volatile u32 *)(common->sfdl_force_int_addr) = 1;
  290. /* wait for the interrupt to come back. */
  291. while (*(common->sfdl_flush_addr) != 0x10f)
  292. cpu_relax();
  293. /* okay, everything is synched up. */
  294. spin_unlock_irqrestore(&p->sfdl_flush_lock, flags);
  295. }
  296. return;
  297. }
  298. /*
  299. * DMA interfaces. Called from pci_dma.c routines.
  300. */
  301. dma_addr_t
  302. pcibr_dma_map(struct pci_dev * hwdev, unsigned long phys_addr, size_t size, int dma_flags)
  303. {
  304. dma_addr_t dma_handle;
  305. struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
  306. /* SN cannot support DMA addresses smaller than 32 bits. */
  307. if (hwdev->dma_mask < 0x7fffffff) {
  308. return 0;
  309. }
  310. if (hwdev->dma_mask == ~0UL) {
  311. /*
  312. * Handle the most common case: 64 bit cards. This
  313. * call should always succeed.
  314. */
  315. dma_handle = pcibr_dmatrans_direct64(pcidev_info, phys_addr,
  316. PCI64_ATTR_PREF, dma_flags);
  317. } else {
  318. /* Handle 32-63 bit cards via direct mapping */
  319. dma_handle = pcibr_dmatrans_direct32(pcidev_info, phys_addr,
  320. size, 0, dma_flags);
  321. if (!dma_handle) {
  322. /*
  323. * It is a 32 bit card and we cannot do direct mapping,
  324. * so we use an ATE.
  325. */
  326. dma_handle = pcibr_dmamap_ate32(pcidev_info, phys_addr,
  327. size, PCI32_ATE_PREF,
  328. dma_flags);
  329. }
  330. }
  331. return dma_handle;
  332. }
  333. dma_addr_t
  334. pcibr_dma_map_consistent(struct pci_dev * hwdev, unsigned long phys_addr,
  335. size_t size, int dma_flags)
  336. {
  337. dma_addr_t dma_handle;
  338. struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
  339. if (hwdev->dev.coherent_dma_mask == ~0UL) {
  340. dma_handle = pcibr_dmatrans_direct64(pcidev_info, phys_addr,
  341. PCI64_ATTR_BAR, dma_flags);
  342. } else {
  343. dma_handle = (dma_addr_t) pcibr_dmamap_ate32(pcidev_info,
  344. phys_addr, size,
  345. PCI32_ATE_BAR, dma_flags);
  346. }
  347. return dma_handle;
  348. }
  349. EXPORT_SYMBOL(sn_dma_flush);