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 printk(KERN_ERR
"%s: out of ATEs\n", __func__
);
120 free_pages((unsigned long)cpuaddr
, get_order(size
));
126 EXPORT_SYMBOL(sn_dma_alloc_coherent
);
129 * sn_pci_free_coherent - free memory associated with coherent DMAable region
130 * @dev: device to free for
131 * @size: size to free
132 * @cpu_addr: kernel virtual address to free
133 * @dma_handle: DMA address associated with this region
135 * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
136 * any associated IOMMU mappings.
138 void sn_dma_free_coherent(struct device
*dev
, size_t size
, void *cpu_addr
,
139 dma_addr_t dma_handle
)
141 struct pci_dev
*pdev
= to_pci_dev(dev
);
142 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
144 BUG_ON(dev
->bus
!= &pci_bus_type
);
146 provider
->dma_unmap(pdev
, dma_handle
, 0);
147 free_pages((unsigned long)cpu_addr
, get_order(size
));
149 EXPORT_SYMBOL(sn_dma_free_coherent
);
152 * sn_dma_map_single - map a single page for DMA
153 * @dev: device to map for
154 * @cpu_addr: kernel virtual address of the region to map
155 * @size: size of the region
156 * @direction: DMA direction
158 * Map the region pointed to by @cpu_addr for DMA and return the
161 * We map this to the one step pcibr_dmamap_trans interface rather than
162 * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
163 * no way of saving the dmamap handle from the alloc to later free
164 * (which is pretty much unacceptable).
166 * TODO: simplify our interface;
167 * figure out how to save dmamap handle so can use two step.
169 dma_addr_t
sn_dma_map_single(struct device
*dev
, void *cpu_addr
, size_t size
,
173 unsigned long phys_addr
;
174 struct pci_dev
*pdev
= to_pci_dev(dev
);
175 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
177 BUG_ON(dev
->bus
!= &pci_bus_type
);
179 phys_addr
= __pa(cpu_addr
);
180 dma_addr
= provider
->dma_map(pdev
, phys_addr
, size
, SN_DMA_ADDR_PHYS
);
182 printk(KERN_ERR
"%s: out of ATEs\n", __func__
);
187 EXPORT_SYMBOL(sn_dma_map_single
);
190 * sn_dma_unmap_single - unamp a DMA mapped page
191 * @dev: device to sync
192 * @dma_addr: DMA address to sync
193 * @size: size of region
194 * @direction: DMA direction
196 * This routine is supposed to sync the DMA region specified
197 * by @dma_handle into the coherence domain. On SN, we're always cache
198 * coherent, so we just need to free any ATEs associated with this mapping.
200 void sn_dma_unmap_single(struct device
*dev
, dma_addr_t dma_addr
, size_t size
,
203 struct pci_dev
*pdev
= to_pci_dev(dev
);
204 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
206 BUG_ON(dev
->bus
!= &pci_bus_type
);
208 provider
->dma_unmap(pdev
, dma_addr
, direction
);
210 EXPORT_SYMBOL(sn_dma_unmap_single
);
213 * sn_dma_unmap_sg - unmap a DMA scatterlist
214 * @dev: device to unmap
215 * @sg: scatterlist to unmap
216 * @nhwentries: number of scatterlist entries
217 * @direction: DMA direction
219 * Unmap a set of streaming mode DMA translations.
221 void sn_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sgl
,
222 int nhwentries
, int direction
)
225 struct pci_dev
*pdev
= to_pci_dev(dev
);
226 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
227 struct scatterlist
*sg
;
229 BUG_ON(dev
->bus
!= &pci_bus_type
);
231 for_each_sg(sgl
, sg
, nhwentries
, i
) {
232 provider
->dma_unmap(pdev
, sg
->dma_address
, direction
);
233 sg
->dma_address
= (dma_addr_t
) NULL
;
237 EXPORT_SYMBOL(sn_dma_unmap_sg
);
240 * sn_dma_map_sg - map a scatterlist for DMA
241 * @dev: device to map for
242 * @sg: scatterlist to map
243 * @nhwentries: number of entries
244 * @direction: direction of the DMA transaction
246 * Maps each entry of @sg for DMA.
248 int sn_dma_map_sg(struct device
*dev
, struct scatterlist
*sgl
, int nhwentries
,
251 unsigned long phys_addr
;
252 struct scatterlist
*saved_sg
= sgl
, *sg
;
253 struct pci_dev
*pdev
= to_pci_dev(dev
);
254 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
257 BUG_ON(dev
->bus
!= &pci_bus_type
);
260 * Setup a DMA address for each entry in the scatterlist.
262 for_each_sg(sgl
, sg
, nhwentries
, i
) {
263 phys_addr
= SG_ENT_PHYS_ADDRESS(sg
);
264 sg
->dma_address
= provider
->dma_map(pdev
,
265 phys_addr
, sg
->length
,
268 if (!sg
->dma_address
) {
269 printk(KERN_ERR
"%s: out of ATEs\n", __func__
);
272 * Free any successfully allocated entries.
275 sn_dma_unmap_sg(dev
, saved_sg
, i
, direction
);
279 sg
->dma_length
= sg
->length
;
284 EXPORT_SYMBOL(sn_dma_map_sg
);
286 void sn_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
287 size_t size
, int direction
)
289 BUG_ON(dev
->bus
!= &pci_bus_type
);
291 EXPORT_SYMBOL(sn_dma_sync_single_for_cpu
);
293 void sn_dma_sync_single_for_device(struct device
*dev
, dma_addr_t dma_handle
,
294 size_t size
, int direction
)
296 BUG_ON(dev
->bus
!= &pci_bus_type
);
298 EXPORT_SYMBOL(sn_dma_sync_single_for_device
);
300 void sn_dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
,
301 int nelems
, int direction
)
303 BUG_ON(dev
->bus
!= &pci_bus_type
);
305 EXPORT_SYMBOL(sn_dma_sync_sg_for_cpu
);
307 void sn_dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
,
308 int nelems
, int direction
)
310 BUG_ON(dev
->bus
!= &pci_bus_type
);
312 EXPORT_SYMBOL(sn_dma_sync_sg_for_device
);
314 int sn_dma_mapping_error(dma_addr_t dma_addr
)
318 EXPORT_SYMBOL(sn_dma_mapping_error
);
320 char *sn_pci_get_legacy_mem(struct pci_bus
*bus
)
322 if (!SN_PCIBUS_BUSSOFT(bus
))
323 return ERR_PTR(-ENODEV
);
325 return (char *)(SN_PCIBUS_BUSSOFT(bus
)->bs_legacy_mem
| __IA64_UNCACHED_OFFSET
);
328 int sn_pci_legacy_read(struct pci_bus
*bus
, u16 port
, u32
*val
, u8 size
)
332 struct ia64_sal_retval isrv
;
335 * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
336 * around hw issues at the pci bus level. SGI proms older than
337 * 4.10 don't implement this.
340 SAL_CALL(isrv
, SN_SAL_IOIF_PCI_SAFE
,
341 pci_domain_nr(bus
), bus
->number
,
344 port
, size
, __pa(val
));
346 if (isrv
.status
== 0)
350 * If the above failed, retry using the SAL_PROBE call which should
351 * be present in all proms (but which cannot work round PCI chipset
352 * bugs). This code is retained for compatibility with old
353 * pre-4.10 proms, and should be removed at some point in the future.
356 if (!SN_PCIBUS_BUSSOFT(bus
))
359 addr
= SN_PCIBUS_BUSSOFT(bus
)->bs_legacy_io
| __IA64_UNCACHED_OFFSET
;
362 ret
= ia64_sn_probe_mem(addr
, (long)size
, (void *)val
);
373 int sn_pci_legacy_write(struct pci_bus
*bus
, u16 port
, u32 val
, u8 size
)
378 struct ia64_sal_retval isrv
;
381 * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
382 * around hw issues at the pci bus level. SGI proms older than
383 * 4.10 don't implement this.
386 SAL_CALL(isrv
, SN_SAL_IOIF_PCI_SAFE
,
387 pci_domain_nr(bus
), bus
->number
,
390 port
, size
, __pa(&val
));
392 if (isrv
.status
== 0)
396 * If the above failed, retry using the SAL_PROBE call which should
397 * be present in all proms (but which cannot work round PCI chipset
398 * bugs). This code is retained for compatibility with old
399 * pre-4.10 proms, and should be removed at some point in the future.
402 if (!SN_PCIBUS_BUSSOFT(bus
)) {
407 /* Put the phys addr in uncached space */
408 paddr
= SN_PCIBUS_BUSSOFT(bus
)->bs_legacy_io
| __IA64_UNCACHED_OFFSET
;
410 addr
= (unsigned long *)paddr
;
414 *(volatile u8
*)(addr
) = (u8
)(val
);
417 *(volatile u16
*)(addr
) = (u16
)(val
);
420 *(volatile u32
*)(addr
) = (u32
)(val
);