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