pcibr_dma.c 11 KB

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