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
6 * Copyright (C) 2001-2004 Silicon Graphics, Inc. All rights reserved.
9 #include <linux/types.h>
10 #include <linux/pci.h>
11 #include <asm/sn/sn_sal.h>
12 #include <asm/sn/geo.h>
13 #include "xtalk/xwidgetdev.h"
14 #include "xtalk/hubdev.h"
15 #include <asm/sn/pcibus_provider_defs.h>
16 #include <asm/sn/pcidev.h>
17 #include "pci/tiocp.h"
19 #include "pci/pcibr_provider.h"
20 #include "pci/tiocp.h"
22 #include <asm/sn/addrs.h>
24 extern int sn_ioif_inited;
26 /* =====================================================================
29 * The Bridge ASIC provides three methods of doing DMA: via a "direct map"
30 * register available in 32-bit PCI space (which selects a contiguous 2G
31 * address space on some other widget), via "direct" addressing via 64-bit
32 * PCI space (all destination information comes from the PCI address,
33 * including transfer attributes), and via a "mapped" region that allows
34 * a bunch of different small mappings to be established with the PMU.
36 * For efficiency, we most prefer to use the 32bit direct mapping facility,
37 * since it requires no resource allocations. The advantage of using the
38 * PMU over the 64-bit direct is that single-cycle PCI addressing can be
39 * used; the advantage of using 64-bit direct over PMU addressing is that
40 * we do not have to allocate entries in the PMU.
44 pcibr_dmamap_ate32(struct pcidev_info *info,
45 uint64_t paddr, size_t req_size, uint64_t flags)
48 struct pcidev_info *pcidev_info = info->pdi_host_pcidev_info;
49 struct pcibus_info *pcibus_info = (struct pcibus_info *)pcidev_info->
51 uint8_t internal_device = (PCI_SLOT(pcidev_info->pdi_host_pcidev_info->
52 pdi_linux_pcidev->devfn)) - 1;
55 uint64_t ate_flags = flags | PCI32_ATE_V;
61 /* PIC in PCI-X mode does not supports 32bit PageMap mode */
62 if (IS_PIC_SOFT(pcibus_info) && IS_PCIX(pcibus_info)) {
66 /* Calculate the number of ATEs needed. */
67 if (!(MINIMAL_ATE_FLAG(paddr, req_size))) {
68 ate_count = IOPG((IOPGSIZE - 1) /* worst case start offset */
69 +req_size /* max mapping bytes */
70 - 1) + 1; /* round UP */
71 } else { /* assume requested target is page aligned */
72 ate_count = IOPG(req_size /* max mapping bytes */
73 - 1) + 1; /* round UP */
76 /* Get the number of ATEs required. */
77 ate_index = pcibr_ate_alloc(pcibus_info, ate_count);
81 /* In PCI-X mode, Prefetch not supported */
82 if (IS_PCIX(pcibus_info))
83 ate_flags &= ~(PCI32_ATE_PREF);
86 IS_PIC_SOFT(pcibus_info) ? PHYS_TO_DMA(paddr) :
87 PHYS_TO_TIODMA(paddr);
88 offset = IOPGOFF(xio_addr);
89 ate = ate_flags | (xio_addr - offset);
91 /* If PIC, put the targetid in the ATE */
92 if (IS_PIC_SOFT(pcibus_info)) {
93 ate |= (pcibus_info->pbi_hub_xid << PIC_ATE_TARGETID_SHFT);
95 ate_write(pcibus_info, ate_index, ate_count, ate);
98 * Set up the DMA mapped Address.
100 pci_addr = PCI32_MAPPED_BASE + offset + IOPGSIZE * ate_index;
103 * If swap was set in device in pcibr_endian_set()
104 * we need to turn swapping on.
106 if (pcibus_info->pbi_devreg[internal_device] & PCIBR_DEV_SWAP_DIR)
107 ATE_SWAP_ON(pci_addr);
113 pcibr_dmatrans_direct64(struct pcidev_info * info, uint64_t paddr,
114 uint64_t dma_attributes)
116 struct pcibus_info *pcibus_info = (struct pcibus_info *)
117 ((info->pdi_host_pcidev_info)->pdi_pcibus_info);
120 /* Translate to Crosstalk View of Physical Address */
121 pci_addr = (IS_PIC_SOFT(pcibus_info) ? PHYS_TO_DMA(paddr) :
122 PHYS_TO_TIODMA(paddr)) | dma_attributes;
124 /* Handle Bus mode */
125 if (IS_PCIX(pcibus_info))
126 pci_addr &= ~PCI64_ATTR_PREF;
128 /* Handle Bridge Chipset differences */
129 if (IS_PIC_SOFT(pcibus_info)) {
131 ((uint64_t) pcibus_info->
132 pbi_hub_xid << PIC_PCI64_ATTR_TARG_SHFT);
134 pci_addr |= TIOCP_PCI64_CMDTYPE_MEM;
136 /* If PCI mode, func zero uses VCHAN0, every other func uses VCHAN1 */
137 if (!IS_PCIX(pcibus_info) && PCI_FUNC(info->pdi_linux_pcidev->devfn))
138 pci_addr |= PCI64_ATTR_VIRTUAL;
145 pcibr_dmatrans_direct32(struct pcidev_info * info,
146 uint64_t paddr, size_t req_size, uint64_t flags)
149 struct pcidev_info *pcidev_info = info->pdi_host_pcidev_info;
150 struct pcibus_info *pcibus_info = (struct pcibus_info *)pcidev_info->
158 if (IS_PCIX(pcibus_info)) {
162 xio_addr = IS_PIC_SOFT(pcibus_info) ? PHYS_TO_DMA(paddr) :
163 PHYS_TO_TIODMA(paddr);
165 xio_base = pcibus_info->pbi_dir_xbase;
166 offset = xio_addr - xio_base;
167 endoff = req_size + offset;
168 if ((req_size > (1ULL << 31)) || /* Too Big */
169 (xio_addr < xio_base) || /* Out of range for mappings */
170 (endoff > (1ULL << 31))) { /* Too Big */
174 return PCI32_DIRECT_BASE | offset;
179 * Wrapper routine for free'ing DMA maps
180 * DMA mappings for Direct 64 and 32 do not have any DMA maps.
183 pcibr_dma_unmap(struct pci_dev *hwdev, dma_addr_t dma_handle, int direction)
185 struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
186 struct pcibus_info *pcibus_info =
187 (struct pcibus_info *)pcidev_info->pdi_pcibus_info;
189 if (IS_PCI32_MAPPED(dma_handle)) {
193 IOPG((ATE_SWAP_OFF(dma_handle) - PCI32_MAPPED_BASE));
194 pcibr_ate_free(pcibus_info, ate_index);
199 * On SN systems there is a race condition between a PIO read response and
200 * DMA's. In rare cases, the read response may beat the DMA, causing the
201 * driver to think that data in memory is complete and meaningful. This code
202 * eliminates that race. This routine is called by the PIO read routines
203 * after doing the read. For PIC this routine then forces a fake interrupt
204 * on another line, which is logically associated with the slot that the PIO
205 * is addressed to. It then spins while watching the memory location that
206 * the interrupt is targetted to. When the interrupt response arrives, we
207 * are sure that the DMA has landed in memory and it is safe for the driver
208 * to proceed. For TIOCP use the Device(x) Write Request Buffer Flush
209 * Bridge register since it ensures the data has entered the coherence domain,
210 * unlike the PIC Device(x) Write Request Buffer Flush register.
213 void sn_dma_flush(uint64_t addr)
221 struct hubdev_info *hubinfo;
222 volatile struct sn_flush_device_list *p;
223 struct sn_flush_nasid_entry *flush_nasid_list;
228 nasid = NASID_GET(addr);
229 if (-1 == nasid_to_cnodeid(nasid))
232 hubinfo = (NODEPDA(nasid_to_cnodeid(nasid)))->pdinfo;
237 is_tio = (nasid & 1);
239 wid_num = TIO_SWIN_WIDGETNUM(addr);
240 bwin = TIO_BWIN_WINDOWNUM(addr);
242 wid_num = SWIN_WIDGETNUM(addr);
243 bwin = BWIN_WINDOWNUM(addr);
246 flush_nasid_list = &hubinfo->hdi_flush_nasid_list;
247 if (flush_nasid_list->widget_p == NULL)
250 uint64_t itte = flush_nasid_list->iio_itte[bwin];
253 wid_num = (itte >> TIO_ITTE_WIDGET_SHIFT) &
254 TIO_ITTE_WIDGET_MASK;
256 wid_num = (itte >> IIO_ITTE_WIDGET_SHIFT) &
257 IIO_ITTE_WIDGET_MASK;
260 if (flush_nasid_list->widget_p == NULL)
262 if (flush_nasid_list->widget_p[wid_num] == NULL)
264 p = &flush_nasid_list->widget_p[wid_num][0];
266 /* find a matching BAR */
267 for (i = 0; i < DEV_PER_WIDGET; i++) {
268 for (j = 0; j < PCI_ROM_RESOURCE; j++) {
269 if (p->sfdl_bar_list[j].start == 0)
271 if (addr >= p->sfdl_bar_list[j].start
272 && addr <= p->sfdl_bar_list[j].end)
275 if (j < PCI_ROM_RESOURCE && p->sfdl_bar_list[j].start != 0)
280 /* if no matching BAR, return without doing anything. */
281 if (i == DEV_PER_WIDGET)
285 * For TIOCP use the Device(x) Write Request Buffer Flush Bridge
286 * register since it ensures the data has entered the coherence
290 uint32_t tio_id = REMOTE_HUB_L(nasid, TIO_NODE_ID);
291 uint32_t revnum = XWIDGET_PART_REV_NUM(tio_id);
293 /* TIOCP BRINGUP WAR (PV907516): Don't write buffer flush reg */
294 if ((1 << XWIDGET_PART_REV_NUM_REV(revnum)) & PV907516) {
297 pcireg_wrb_flush_get(p->sfdl_pcibus_info,
301 spin_lock_irqsave(&((struct sn_flush_device_list *)p)->
302 sfdl_flush_lock, flags);
304 *p->sfdl_flush_addr = 0;
306 /* force an interrupt. */
307 *(volatile uint32_t *)(p->sfdl_force_int_addr) = 1;
309 /* wait for the interrupt to come back. */
310 while (*(p->sfdl_flush_addr) != 0x10f) ;
312 /* okay, everything is synched up. */
313 spin_unlock_irqrestore((spinlock_t *)&p->sfdl_flush_lock, flags);
319 * DMA interfaces. Called from pci_dma.c routines.
323 pcibr_dma_map(struct pci_dev * hwdev, unsigned long phys_addr, size_t size)
325 dma_addr_t dma_handle;
326 struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
328 /* SN cannot support DMA addresses smaller than 32 bits. */
329 if (hwdev->dma_mask < 0x7fffffff) {
333 if (hwdev->dma_mask == ~0UL) {
335 * Handle the most common case: 64 bit cards. This
336 * call should always succeed.
339 dma_handle = pcibr_dmatrans_direct64(pcidev_info, phys_addr,
342 /* Handle 32-63 bit cards via direct mapping */
343 dma_handle = pcibr_dmatrans_direct32(pcidev_info, phys_addr,
347 * It is a 32 bit card and we cannot do direct mapping,
351 dma_handle = pcibr_dmamap_ate32(pcidev_info, phys_addr,
352 size, PCI32_ATE_PREF);
360 pcibr_dma_map_consistent(struct pci_dev * hwdev, unsigned long phys_addr,
363 dma_addr_t dma_handle;
364 struct pcidev_info *pcidev_info = SN_PCIDEV_INFO(hwdev);
366 if (hwdev->dev.coherent_dma_mask == ~0UL) {
367 dma_handle = pcibr_dmatrans_direct64(pcidev_info, phys_addr,
370 dma_handle = (dma_addr_t) pcibr_dmamap_ate32(pcidev_info,
378 EXPORT_SYMBOL(sn_dma_flush);