pcibr_dma.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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) | dma_attributes;
  119. else
  120. pci_addr = IS_PIC_SOFT(pcibus_info) ?
  121. paddr :
  122. paddr | 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 targetted 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. if (!hubinfo) {
  221. BUG();
  222. }
  223. flush_nasid_list = &hubinfo->hdi_flush_nasid_list;
  224. if (flush_nasid_list->widget_p == NULL)
  225. return;
  226. is_tio = (nasid & 1);
  227. if (is_tio) {
  228. int itte_index;
  229. if (TIO_HWIN(addr))
  230. itte_index = 0;
  231. else if (TIO_BWIN_WINDOWNUM(addr))
  232. itte_index = TIO_BWIN_WINDOWNUM(addr);
  233. else
  234. itte_index = -1;
  235. if (itte_index >= 0) {
  236. itte = flush_nasid_list->iio_itte[itte_index];
  237. if (! TIO_ITTE_VALID(itte))
  238. return;
  239. wid_num = TIO_ITTE_WIDGET(itte);
  240. } else
  241. wid_num = TIO_SWIN_WIDGETNUM(addr);
  242. } else {
  243. if (BWIN_WINDOWNUM(addr)) {
  244. itte = flush_nasid_list->iio_itte[BWIN_WINDOWNUM(addr)];
  245. wid_num = IIO_ITTE_WIDGET(itte);
  246. } else
  247. wid_num = SWIN_WIDGETNUM(addr);
  248. }
  249. if (flush_nasid_list->widget_p[wid_num] == NULL)
  250. return;
  251. p = &flush_nasid_list->widget_p[wid_num][0];
  252. /* find a matching BAR */
  253. for (i = 0; i < DEV_PER_WIDGET; i++,p++) {
  254. common = p->common;
  255. for (j = 0; j < PCI_ROM_RESOURCE; j++) {
  256. if (common->sfdl_bar_list[j].start == 0)
  257. break;
  258. if (addr >= common->sfdl_bar_list[j].start
  259. && addr <= common->sfdl_bar_list[j].end)
  260. break;
  261. }
  262. if (j < PCI_ROM_RESOURCE && common->sfdl_bar_list[j].start != 0)
  263. break;
  264. }
  265. /* if no matching BAR, return without doing anything. */
  266. if (i == DEV_PER_WIDGET)
  267. return;
  268. /*
  269. * For TIOCP use the Device(x) Write Request Buffer Flush Bridge
  270. * register since it ensures the data has entered the coherence
  271. * domain, unlike PIC.
  272. */
  273. if (is_tio) {
  274. /*
  275. * Note: devices behind TIOCE should never be matched in the
  276. * above code, and so the following code is PIC/CP centric.
  277. * If CE ever needs the sn_dma_flush mechanism, we will have
  278. * to account for that here and in tioce_bus_fixup().
  279. */
  280. u32 tio_id = HUB_L(TIO_IOSPACE_ADDR(nasid, TIO_NODE_ID));
  281. u32 revnum = XWIDGET_PART_REV_NUM(tio_id);
  282. /* TIOCP BRINGUP WAR (PV907516): Don't write buffer flush reg */
  283. if ((1 << XWIDGET_PART_REV_NUM_REV(revnum)) & PV907516) {
  284. return;
  285. } else {
  286. pcireg_wrb_flush_get(common->sfdl_pcibus_info,
  287. (common->sfdl_slot - 1));
  288. }
  289. } else {
  290. spin_lock_irqsave(&p->sfdl_flush_lock, flags);
  291. *common->sfdl_flush_addr = 0;
  292. /* force an interrupt. */
  293. *(volatile u32 *)(common->sfdl_force_int_addr) = 1;
  294. /* wait for the interrupt to come back. */
  295. while (*(common->sfdl_flush_addr) != 0x10f)
  296. cpu_relax();
  297. /* okay, everything is synched up. */
  298. spin_unlock_irqrestore(&p->sfdl_flush_lock, flags);
  299. }
  300. return;
  301. }
  302. /*
  303. * DMA interfaces. Called from pci_dma.c routines.
  304. */
  305. dma_addr_t
  306. pcibr_dma_map(struct pci_dev * hwdev, unsigned long phys_addr, size_t size, int dma_flags)
  307. {
  308. dma_addr_t dma_handle;
  309. struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
  310. /* SN cannot support DMA addresses smaller than 32 bits. */
  311. if (hwdev->dma_mask < 0x7fffffff) {
  312. return 0;
  313. }
  314. if (hwdev->dma_mask == ~0UL) {
  315. /*
  316. * Handle the most common case: 64 bit cards. This
  317. * call should always succeed.
  318. */
  319. dma_handle = pcibr_dmatrans_direct64(pcidev_info, phys_addr,
  320. PCI64_ATTR_PREF, dma_flags);
  321. } else {
  322. /* Handle 32-63 bit cards via direct mapping */
  323. dma_handle = pcibr_dmatrans_direct32(pcidev_info, phys_addr,
  324. size, 0, dma_flags);
  325. if (!dma_handle) {
  326. /*
  327. * It is a 32 bit card and we cannot do direct mapping,
  328. * so we use an ATE.
  329. */
  330. dma_handle = pcibr_dmamap_ate32(pcidev_info, phys_addr,
  331. size, PCI32_ATE_PREF,
  332. dma_flags);
  333. }
  334. }
  335. return dma_handle;
  336. }
  337. dma_addr_t
  338. pcibr_dma_map_consistent(struct pci_dev * hwdev, unsigned long phys_addr,
  339. size_t size, int dma_flags)
  340. {
  341. dma_addr_t dma_handle;
  342. struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
  343. if (hwdev->dev.coherent_dma_mask == ~0UL) {
  344. dma_handle = pcibr_dmatrans_direct64(pcidev_info, phys_addr,
  345. PCI64_ATTR_BAR, dma_flags);
  346. } else {
  347. dma_handle = (dma_addr_t) pcibr_dmamap_ate32(pcidev_info,
  348. phys_addr, size,
  349. PCI32_ATE_BAR, dma_flags);
  350. }
  351. return dma_handle;
  352. }
  353. EXPORT_SYMBOL(sn_dma_flush);