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/gfp.h>
13 #include <linux/module.h>
14 #include <linux/dma-mapping.h>
16 #include <asm/sn/intr.h>
17 #include <asm/sn/pcibus_provider_defs.h>
18 #include <asm/sn/pcidev.h>
19 #include <asm/sn/sn_sal.h>
21 #define SG_ENT_VIRT_ADDRESS(sg) (sg_virt((sg)))
22 #define SG_ENT_PHYS_ADDRESS(SG) virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))
25 * sn_dma_supported - test a DMA mask
26 * @dev: device to test
27 * @mask: DMA mask to test
29 * Return whether the given PCI device DMA address mask can be supported
30 * properly. For example, if your device can only drive the low 24-bits
31 * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
32 * this function. Of course, SN only supports devices that have 32 or more
33 * address bits when using the PMU.
35 static int sn_dma_supported(struct device
*dev
, u64 mask
)
37 BUG_ON(dev
->bus
!= &pci_bus_type
);
39 if (mask
< 0x7fffffff)
45 * sn_dma_set_mask - set the DMA mask
49 * Set @dev's DMA mask if the hw supports it.
51 int sn_dma_set_mask(struct device
*dev
, u64 dma_mask
)
53 BUG_ON(dev
->bus
!= &pci_bus_type
);
55 if (!sn_dma_supported(dev
, dma_mask
))
58 *dev
->dma_mask
= dma_mask
;
61 EXPORT_SYMBOL(sn_dma_set_mask
);
64 * sn_dma_alloc_coherent - allocate memory for coherent DMA
65 * @dev: device to allocate for
66 * @size: size of the region
67 * @dma_handle: DMA (bus) address
68 * @flags: memory allocation flags
70 * dma_alloc_coherent() returns a pointer to a memory region suitable for
71 * coherent DMA traffic to/from a PCI device. On SN platforms, this means
72 * that @dma_handle will have the %PCIIO_DMA_CMD flag set.
74 * This interface is usually used for "command" streams (e.g. the command
75 * queue for a SCSI controller). See Documentation/DMA-API.txt for
78 static void *sn_dma_alloc_coherent(struct device
*dev
, size_t size
,
79 dma_addr_t
* dma_handle
, gfp_t flags
)
82 unsigned long phys_addr
;
84 struct pci_dev
*pdev
= to_pci_dev(dev
);
85 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
87 BUG_ON(dev
->bus
!= &pci_bus_type
);
90 * Allocate the memory.
92 node
= pcibus_to_node(pdev
->bus
);
93 if (likely(node
>=0)) {
94 struct page
*p
= alloc_pages_exact_node(node
,
95 flags
, get_order(size
));
98 cpuaddr
= page_address(p
);
102 cpuaddr
= (void *)__get_free_pages(flags
, get_order(size
));
104 if (unlikely(!cpuaddr
))
107 memset(cpuaddr
, 0x0, size
);
109 /* physical addr. of the memory we just got */
110 phys_addr
= __pa(cpuaddr
);
113 * 64 bit address translations should never fail.
114 * 32 bit translations can fail if there are insufficient mapping
118 *dma_handle
= provider
->dma_map_consistent(pdev
, phys_addr
, size
,
121 printk(KERN_ERR
"%s: out of ATEs\n", __func__
);
122 free_pages((unsigned long)cpuaddr
, get_order(size
));
130 * sn_pci_free_coherent - free memory associated with coherent DMAable region
131 * @dev: device to free for
132 * @size: size to free
133 * @cpu_addr: kernel virtual address to free
134 * @dma_handle: DMA address associated with this region
136 * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
137 * any associated IOMMU mappings.
139 static void sn_dma_free_coherent(struct device
*dev
, size_t size
, void *cpu_addr
,
140 dma_addr_t dma_handle
)
142 struct pci_dev
*pdev
= to_pci_dev(dev
);
143 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
145 BUG_ON(dev
->bus
!= &pci_bus_type
);
147 provider
->dma_unmap(pdev
, dma_handle
, 0);
148 free_pages((unsigned long)cpu_addr
, get_order(size
));
152 * sn_dma_map_single_attrs - 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
157 * @attrs: optional dma attributes
159 * Map the region pointed to by @cpu_addr for DMA and return the
162 * We map this to the one step pcibr_dmamap_trans interface rather than
163 * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
164 * no way of saving the dmamap handle from the alloc to later free
165 * (which is pretty much unacceptable).
167 * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
168 * dma_map_consistent() so that writes force a flush of pending DMA.
169 * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
170 * Document Number: 007-4763-001)
172 * TODO: simplify our interface;
173 * figure out how to save dmamap handle so can use two step.
175 static dma_addr_t
sn_dma_map_page(struct device
*dev
, struct page
*page
,
176 unsigned long offset
, size_t size
,
177 enum dma_data_direction dir
,
178 struct dma_attrs
*attrs
)
180 void *cpu_addr
= page_address(page
) + offset
;
182 unsigned long phys_addr
;
183 struct pci_dev
*pdev
= to_pci_dev(dev
);
184 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
187 dmabarr
= dma_get_attr(DMA_ATTR_WRITE_BARRIER
, attrs
);
189 BUG_ON(dev
->bus
!= &pci_bus_type
);
191 phys_addr
= __pa(cpu_addr
);
193 dma_addr
= provider
->dma_map_consistent(pdev
, phys_addr
,
194 size
, SN_DMA_ADDR_PHYS
);
196 dma_addr
= provider
->dma_map(pdev
, phys_addr
, size
,
200 printk(KERN_ERR
"%s: out of ATEs\n", __func__
);
207 * sn_dma_unmap_single_attrs - unamp a DMA mapped page
208 * @dev: device to sync
209 * @dma_addr: DMA address to sync
210 * @size: size of region
211 * @direction: DMA direction
212 * @attrs: optional dma attributes
214 * This routine is supposed to sync the DMA region specified
215 * by @dma_handle into the coherence domain. On SN, we're always cache
216 * coherent, so we just need to free any ATEs associated with this mapping.
218 static void sn_dma_unmap_page(struct device
*dev
, dma_addr_t dma_addr
,
219 size_t size
, enum dma_data_direction dir
,
220 struct dma_attrs
*attrs
)
222 struct pci_dev
*pdev
= to_pci_dev(dev
);
223 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
225 BUG_ON(dev
->bus
!= &pci_bus_type
);
227 provider
->dma_unmap(pdev
, dma_addr
, dir
);
231 * sn_dma_unmap_sg - unmap a DMA scatterlist
232 * @dev: device to unmap
233 * @sg: scatterlist to unmap
234 * @nhwentries: number of scatterlist entries
235 * @direction: DMA direction
236 * @attrs: optional dma attributes
238 * Unmap a set of streaming mode DMA translations.
240 static void sn_dma_unmap_sg(struct device
*dev
, struct scatterlist
*sgl
,
241 int nhwentries
, enum dma_data_direction dir
,
242 struct dma_attrs
*attrs
)
245 struct pci_dev
*pdev
= to_pci_dev(dev
);
246 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
247 struct scatterlist
*sg
;
249 BUG_ON(dev
->bus
!= &pci_bus_type
);
251 for_each_sg(sgl
, sg
, nhwentries
, i
) {
252 provider
->dma_unmap(pdev
, sg
->dma_address
, dir
);
253 sg
->dma_address
= (dma_addr_t
) NULL
;
259 * sn_dma_map_sg - map a scatterlist for DMA
260 * @dev: device to map for
261 * @sg: scatterlist to map
262 * @nhwentries: number of entries
263 * @direction: direction of the DMA transaction
264 * @attrs: optional dma attributes
266 * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
267 * dma_map_consistent() so that writes force a flush of pending DMA.
268 * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
269 * Document Number: 007-4763-001)
271 * Maps each entry of @sg for DMA.
273 static int sn_dma_map_sg(struct device
*dev
, struct scatterlist
*sgl
,
274 int nhwentries
, enum dma_data_direction dir
,
275 struct dma_attrs
*attrs
)
277 unsigned long phys_addr
;
278 struct scatterlist
*saved_sg
= sgl
, *sg
;
279 struct pci_dev
*pdev
= to_pci_dev(dev
);
280 struct sn_pcibus_provider
*provider
= SN_PCIDEV_BUSPROVIDER(pdev
);
284 dmabarr
= dma_get_attr(DMA_ATTR_WRITE_BARRIER
, attrs
);
286 BUG_ON(dev
->bus
!= &pci_bus_type
);
289 * Setup a DMA address for each entry in the scatterlist.
291 for_each_sg(sgl
, sg
, nhwentries
, i
) {
293 phys_addr
= SG_ENT_PHYS_ADDRESS(sg
);
295 dma_addr
= provider
->dma_map_consistent(pdev
,
300 dma_addr
= provider
->dma_map(pdev
, phys_addr
,
304 sg
->dma_address
= dma_addr
;
305 if (!sg
->dma_address
) {
306 printk(KERN_ERR
"%s: out of ATEs\n", __func__
);
309 * Free any successfully allocated entries.
312 sn_dma_unmap_sg(dev
, saved_sg
, i
, dir
, attrs
);
316 sg
->dma_length
= sg
->length
;
322 static void sn_dma_sync_single_for_cpu(struct device
*dev
, dma_addr_t dma_handle
,
323 size_t size
, enum dma_data_direction dir
)
325 BUG_ON(dev
->bus
!= &pci_bus_type
);
328 static void sn_dma_sync_single_for_device(struct device
*dev
, dma_addr_t dma_handle
,
330 enum dma_data_direction dir
)
332 BUG_ON(dev
->bus
!= &pci_bus_type
);
335 static void sn_dma_sync_sg_for_cpu(struct device
*dev
, struct scatterlist
*sg
,
336 int nelems
, enum dma_data_direction dir
)
338 BUG_ON(dev
->bus
!= &pci_bus_type
);
341 static void sn_dma_sync_sg_for_device(struct device
*dev
, struct scatterlist
*sg
,
342 int nelems
, enum dma_data_direction dir
)
344 BUG_ON(dev
->bus
!= &pci_bus_type
);
347 static int sn_dma_mapping_error(struct device
*dev
, dma_addr_t dma_addr
)
352 u64
sn_dma_get_required_mask(struct device
*dev
)
354 return DMA_BIT_MASK(64);
356 EXPORT_SYMBOL_GPL(sn_dma_get_required_mask
);
358 char *sn_pci_get_legacy_mem(struct pci_bus
*bus
)
360 if (!SN_PCIBUS_BUSSOFT(bus
))
361 return ERR_PTR(-ENODEV
);
363 return (char *)(SN_PCIBUS_BUSSOFT(bus
)->bs_legacy_mem
| __IA64_UNCACHED_OFFSET
);
366 int sn_pci_legacy_read(struct pci_bus
*bus
, u16 port
, u32
*val
, u8 size
)
370 struct ia64_sal_retval isrv
;
373 * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
374 * around hw issues at the pci bus level. SGI proms older than
375 * 4.10 don't implement this.
378 SAL_CALL(isrv
, SN_SAL_IOIF_PCI_SAFE
,
379 pci_domain_nr(bus
), bus
->number
,
382 port
, size
, __pa(val
));
384 if (isrv
.status
== 0)
388 * If the above failed, retry using the SAL_PROBE call which should
389 * be present in all proms (but which cannot work round PCI chipset
390 * bugs). This code is retained for compatibility with old
391 * pre-4.10 proms, and should be removed at some point in the future.
394 if (!SN_PCIBUS_BUSSOFT(bus
))
397 addr
= SN_PCIBUS_BUSSOFT(bus
)->bs_legacy_io
| __IA64_UNCACHED_OFFSET
;
400 ret
= ia64_sn_probe_mem(addr
, (long)size
, (void *)val
);
411 int sn_pci_legacy_write(struct pci_bus
*bus
, u16 port
, u32 val
, u8 size
)
416 struct ia64_sal_retval isrv
;
419 * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
420 * around hw issues at the pci bus level. SGI proms older than
421 * 4.10 don't implement this.
424 SAL_CALL(isrv
, SN_SAL_IOIF_PCI_SAFE
,
425 pci_domain_nr(bus
), bus
->number
,
428 port
, size
, __pa(&val
));
430 if (isrv
.status
== 0)
434 * If the above failed, retry using the SAL_PROBE call which should
435 * be present in all proms (but which cannot work round PCI chipset
436 * bugs). This code is retained for compatibility with old
437 * pre-4.10 proms, and should be removed at some point in the future.
440 if (!SN_PCIBUS_BUSSOFT(bus
)) {
445 /* Put the phys addr in uncached space */
446 paddr
= SN_PCIBUS_BUSSOFT(bus
)->bs_legacy_io
| __IA64_UNCACHED_OFFSET
;
448 addr
= (unsigned long *)paddr
;
452 *(volatile u8
*)(addr
) = (u8
)(val
);
455 *(volatile u16
*)(addr
) = (u16
)(val
);
458 *(volatile u32
*)(addr
) = (u32
)(val
);
468 static struct dma_map_ops sn_dma_ops
= {
469 .alloc_coherent
= sn_dma_alloc_coherent
,
470 .free_coherent
= sn_dma_free_coherent
,
471 .map_page
= sn_dma_map_page
,
472 .unmap_page
= sn_dma_unmap_page
,
473 .map_sg
= sn_dma_map_sg
,
474 .unmap_sg
= sn_dma_unmap_sg
,
475 .sync_single_for_cpu
= sn_dma_sync_single_for_cpu
,
476 .sync_sg_for_cpu
= sn_dma_sync_sg_for_cpu
,
477 .sync_single_for_device
= sn_dma_sync_single_for_device
,
478 .sync_sg_for_device
= sn_dma_sync_sg_for_device
,
479 .mapping_error
= sn_dma_mapping_error
,
480 .dma_supported
= sn_dma_supported
,
483 void sn_dma_init(void)
485 dma_ops
= &sn_dma_ops
;