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) 2000,2002-2005 Silicon Graphics, Inc. All rights reserved.
8 * Routines for PCI DMA mapping. See Documentation/DMA-API.txt for
9 * a description of how these routines should be used.
12 #include <linux/module.h>
14 #include <asm/sn/intr.h>
15 #include <asm/sn/pcibus_provider_defs.h>
16 #include <asm/sn/pcidev.h>
17 #include <asm/sn/sn_sal.h>
19 #define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
20 #define SG_ENT_PHYS_ADDRESS(SG) virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
23 * sn_dma_supported - test a DMA mask
24 * @dev: device to test
25 * @mask: DMA mask to test
27 * Return whether the given PCI device DMA address mask can be supported
28 * properly. For example, if your device can only drive the low 24-bits
29 * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
30 * this function. Of course, SN only supports devices that have 32 or more
31 * address bits when using the PMU.
33 int sn_dma_supported(struct device
*dev
, u64 mask
)
35 BUG_ON(dev
->bus
!= &pci_bus_type
);
37 if (mask
< 0x7fffffff)
41 EXPORT_SYMBOL(sn_dma_supported
);
44 * sn_dma_set_mask - set the DMA mask
48 * Set @dev's DMA mask if the hw supports it.
50 int sn_dma_set_mask(struct device
*dev
, u64 dma_mask
)
52 BUG_ON(dev
->bus
!= &pci_bus_type
);
54 if (!sn_dma_supported(dev
, dma_mask
))
57 *dev
->dma_mask
= dma_mask
;
60 EXPORT_SYMBOL(sn_dma_set_mask
);
63 * sn_dma_alloc_coherent - allocate memory for coherent DMA
64 * @dev: device to allocate for
65 * @size: size of the region
66 * @dma_handle: DMA (bus) address
67 * @flags: memory allocation flags
69 * dma_alloc_coherent() returns a pointer to a memory region suitable for
70 * coherent DMA traffic to/from a PCI device. On SN platforms, this means
71 * that @dma_handle will have the %PCIIO_DMA_CMD flag set.
73 * This interface is usually used for "command" streams (e.g. the command
74 * queue for a SCSI controller). See Documentation/DMA-API.txt for
77 void *sn_dma_alloc_coherent(struct device
*dev
, size_t size
,
78 dma_addr_t
* dma_handle
, gfp_t flags
)
81 unsigned long phys_addr
;
83 struct pci_dev
*pdev
= to_pci_dev(dev
);
84 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
86 BUG_ON(dev
->bus
!= &pci_bus_type
);
89 * Allocate the memory.
91 node
= pcibus_to_node(pdev
->bus
);
92 if (likely(node
>=0)) {
93 struct page
*p
= alloc_pages_node(node
, flags
, get_order(size
));
96 cpuaddr
= page_address(p
);
100 cpuaddr
= (void *)__get_free_pages(flags
, get_order(size
));
102 if (unlikely(!cpuaddr
))
105 memset(cpuaddr
, 0x0, size
);
107 /* physical addr. of the memory we just got */
108 phys_addr
= __pa(cpuaddr
);
111 * 64 bit address translations should never fail.
112 * 32 bit translations can fail if there are insufficient mapping
116 *dma_handle
= provider
->dma_map_consistent(pdev
, phys_addr
, size
,
119 <<<<<<< HEAD
:arch
/ia64
/sn
/pci
/pci_dma
.c
120 printk(KERN_ERR
"%s: out of ATEs\n", __FUNCTION__
);
122 printk(KERN_ERR
"%s: out of ATEs\n", __func__
);
123 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/ia64
/sn
/pci
/pci_dma
.c
124 free_pages((unsigned long)cpuaddr
, get_order(size
));
130 EXPORT_SYMBOL(sn_dma_alloc_coherent
);
133 * sn_pci_free_coherent - free memory associated with coherent DMAable region
134 * @dev: device to free for
135 * @size: size to free
136 * @cpu_addr: kernel virtual address to free
137 * @dma_handle: DMA address associated with this region
139 * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
140 * any associated IOMMU mappings.
142 void sn_dma_free_coherent(struct device
*dev
, size_t size
, void *cpu_addr
,
143 dma_addr_t dma_handle
)
145 struct pci_dev
*pdev
= to_pci_dev(dev
);
146 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
148 BUG_ON(dev
->bus
!= &pci_bus_type
);
150 provider
->dma_unmap(pdev
, dma_handle
, 0);
151 free_pages((unsigned long)cpu_addr
, get_order(size
));
153 EXPORT_SYMBOL(sn_dma_free_coherent
);
156 * sn_dma_map_single - map a single page for DMA
157 * @dev: device to map for
158 * @cpu_addr: kernel virtual address of the region to map
159 * @size: size of the region
160 * @direction: DMA direction
162 * Map the region pointed to by @cpu_addr for DMA and return the
165 * We map this to the one step pcibr_dmamap_trans interface rather than
166 * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
167 * no way of saving the dmamap handle from the alloc to later free
168 * (which is pretty much unacceptable).
170 * TODO: simplify our interface;
171 * figure out how to save dmamap handle so can use two step.
173 dma_addr_t
sn_dma_map_single(struct device
*dev
, void *cpu_addr
, size_t size
,
177 unsigned long phys_addr
;
178 struct pci_dev
*pdev
= to_pci_dev(dev
);
179 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
181 BUG_ON(dev
->bus
!= &pci_bus_type
);
183 phys_addr
= __pa(cpu_addr
);
184 dma_addr
= provider
->dma_map(pdev
, phys_addr
, size
, SN_DMA_ADDR_PHYS
);
186 <<<<<<< HEAD
:arch
/ia64
/sn
/pci
/pci_dma
.c
187 printk(KERN_ERR
"%s: out of ATEs\n", __FUNCTION__
);
189 printk(KERN_ERR
"%s: out of ATEs\n", __func__
);
190 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/ia64
/sn
/pci
/pci_dma
.c
195 EXPORT_SYMBOL(sn_dma_map_single
);
198 * sn_dma_unmap_single - unamp a DMA mapped page
199 * @dev: device to sync
200 * @dma_addr: DMA address to sync
201 * @size: size of region
202 * @direction: DMA direction
204 * This routine is supposed to sync the DMA region specified
205 * by @dma_handle into the coherence domain. On SN, we're always cache
206 * coherent, so we just need to free any ATEs associated with this mapping.
208 void sn_dma_unmap_single(struct device
*dev
, dma_addr_t dma_addr
, size_t size
,
211 struct pci_dev
*pdev
= to_pci_dev(dev
);
212 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
214 BUG_ON(dev
->bus
!= &pci_bus_type
);
216 provider
->dma_unmap(pdev
, dma_addr
, direction
);
218 EXPORT_SYMBOL(sn_dma_unmap_single
);
221 * sn_dma_unmap_sg - unmap a DMA scatterlist
222 * @dev: device to unmap
223 * @sg: scatterlist to unmap
224 * @nhwentries: number of scatterlist entries
225 * @direction: DMA direction
227 * Unmap a set of streaming mode DMA translations.
229 void sn_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sgl
,
230 int nhwentries
, int direction
)
233 struct pci_dev
*pdev
= to_pci_dev(dev
);
234 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
235 struct scatterlist
*sg
;
237 BUG_ON(dev
->bus
!= &pci_bus_type
);
239 for_each_sg(sgl
, sg
, nhwentries
, i
) {
240 provider
->dma_unmap(pdev
, sg
->dma_address
, direction
);
241 sg
->dma_address
= (dma_addr_t
) NULL
;
245 EXPORT_SYMBOL(sn_dma_unmap_sg
);
248 * sn_dma_map_sg - map a scatterlist for DMA
249 * @dev: device to map for
250 * @sg: scatterlist to map
251 * @nhwentries: number of entries
252 * @direction: direction of the DMA transaction
254 * Maps each entry of @sg for DMA.
256 int sn_dma_map_sg(struct device
*dev
, struct scatterlist
*sgl
, int nhwentries
,
259 unsigned long phys_addr
;
260 struct scatterlist
*saved_sg
= sgl
, *sg
;
261 struct pci_dev
*pdev
= to_pci_dev(dev
);
262 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
265 BUG_ON(dev
->bus
!= &pci_bus_type
);
268 * Setup a DMA address for each entry in the scatterlist.
270 for_each_sg(sgl
, sg
, nhwentries
, i
) {
271 phys_addr
= SG_ENT_PHYS_ADDRESS(sg
);
272 sg
->dma_address
= provider
->dma_map(pdev
,
273 phys_addr
, sg
->length
,
276 if (!sg
->dma_address
) {
277 <<<<<<< HEAD
:arch
/ia64
/sn
/pci
/pci_dma
.c
278 printk(KERN_ERR
"%s: out of ATEs\n", __FUNCTION__
);
280 printk(KERN_ERR
"%s: out of ATEs\n", __func__
);
281 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a
:arch
/ia64
/sn
/pci
/pci_dma
.c
284 * Free any successfully allocated entries.
287 sn_dma_unmap_sg(dev
, saved_sg
, i
, direction
);
291 sg
->dma_length
= sg
->length
;
296 EXPORT_SYMBOL(sn_dma_map_sg
);
298 void sn_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
299 size_t size
, int direction
)
301 BUG_ON(dev
->bus
!= &pci_bus_type
);
303 EXPORT_SYMBOL(sn_dma_sync_single_for_cpu
);
305 void sn_dma_sync_single_for_device(struct device
*dev
, dma_addr_t dma_handle
,
306 size_t size
, int direction
)
308 BUG_ON(dev
->bus
!= &pci_bus_type
);
310 EXPORT_SYMBOL(sn_dma_sync_single_for_device
);
312 void sn_dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
,
313 int nelems
, int direction
)
315 BUG_ON(dev
->bus
!= &pci_bus_type
);
317 EXPORT_SYMBOL(sn_dma_sync_sg_for_cpu
);
319 void sn_dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
,
320 int nelems
, int direction
)
322 BUG_ON(dev
->bus
!= &pci_bus_type
);
324 EXPORT_SYMBOL(sn_dma_sync_sg_for_device
);
326 int sn_dma_mapping_error(dma_addr_t dma_addr
)
330 EXPORT_SYMBOL(sn_dma_mapping_error
);
332 char *sn_pci_get_legacy_mem(struct pci_bus
*bus
)
334 if (!SN_PCIBUS_BUSSOFT(bus
))
335 return ERR_PTR(-ENODEV
);
337 return (char *)(SN_PCIBUS_BUSSOFT(bus
)->bs_legacy_mem
| __IA64_UNCACHED_OFFSET
);
340 int sn_pci_legacy_read(struct pci_bus
*bus
, u16 port
, u32
*val
, u8 size
)
344 struct ia64_sal_retval isrv
;
347 * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
348 * around hw issues at the pci bus level. SGI proms older than
349 * 4.10 don't implement this.
352 SAL_CALL(isrv
, SN_SAL_IOIF_PCI_SAFE
,
353 pci_domain_nr(bus
), bus
->number
,
356 port
, size
, __pa(val
));
358 if (isrv
.status
== 0)
362 * If the above failed, retry using the SAL_PROBE call which should
363 * be present in all proms (but which cannot work round PCI chipset
364 * bugs). This code is retained for compatibility with old
365 * pre-4.10 proms, and should be removed at some point in the future.
368 if (!SN_PCIBUS_BUSSOFT(bus
))
371 addr
= SN_PCIBUS_BUSSOFT(bus
)->bs_legacy_io
| __IA64_UNCACHED_OFFSET
;
374 ret
= ia64_sn_probe_mem(addr
, (long)size
, (void *)val
);
385 int sn_pci_legacy_write(struct pci_bus
*bus
, u16 port
, u32 val
, u8 size
)
390 struct ia64_sal_retval isrv
;
393 * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
394 * around hw issues at the pci bus level. SGI proms older than
395 * 4.10 don't implement this.
398 SAL_CALL(isrv
, SN_SAL_IOIF_PCI_SAFE
,
399 pci_domain_nr(bus
), bus
->number
,
402 port
, size
, __pa(&val
));
404 if (isrv
.status
== 0)
408 * If the above failed, retry using the SAL_PROBE call which should
409 * be present in all proms (but which cannot work round PCI chipset
410 * bugs). This code is retained for compatibility with old
411 * pre-4.10 proms, and should be removed at some point in the future.
414 if (!SN_PCIBUS_BUSSOFT(bus
)) {
419 /* Put the phys addr in uncached space */
420 paddr
= SN_PCIBUS_BUSSOFT(bus
)->bs_legacy_io
| __IA64_UNCACHED_OFFSET
;
422 addr
= (unsigned long *)paddr
;
426 *(volatile u8
*)(addr
) = (u8
)(val
);
429 *(volatile u16
*)(addr
) = (u16
)(val
);
432 *(volatile u32
*)(addr
) = (u32
)(val
);