pcibr_dma.c 11 KB

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