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-2003 Silicon Graphics, Inc. All rights reserved.
8 * Routines for PCI DMA mapping. See Documentation/DMA-mapping.txt for
9 * a description of how these routines should be used.
12 #include <linux/config.h>
13 #include <linux/types.h>
15 #include <linux/string.h>
16 #include <linux/pci.h>
17 #include <linux/slab.h>
18 #include <linux/devfs_fs_kernel.h>
19 #include <linux/module.h>
21 #include <asm/delay.h>
23 #include <asm/sn/sgi.h>
24 #include <asm/sn/io.h>
25 #include <asm/sn/invent.h>
26 #include <asm/sn/hcl.h>
27 #include <asm/sn/pci/pcibr.h>
28 #include <asm/sn/pci/pcibr_private.h>
29 #include <asm/sn/driver.h>
30 #include <asm/sn/types.h>
31 #include <asm/sn/alenlist.h>
32 #include <asm/sn/pci/pci_bus_cvlink.h>
33 #include <asm/sn/nag.h>
38 pciio_dmamap_t get_free_pciio_dmamap(vertex_hdl_t);
39 void free_pciio_dmamap(pcibr_dmamap_t);
40 static struct sn_dma_maps_s *find_sn_dma_map(dma_addr_t, unsigned char);
41 void sn_pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction);
46 extern vertex_hdl_t busnum_to_pcibr_vhdl[];
47 extern nasid_t busnum_to_nid[];
48 extern void * busnum_to_atedmamaps[];
51 * get_free_pciio_dmamap - find and allocate an ATE
52 * @pci_bus: PCI bus to get an entry for
54 * Finds and allocates an ATE on the PCI bus specified
58 get_free_pciio_dmamap(vertex_hdl_t pci_bus)
61 struct sn_dma_maps_s *sn_dma_map = NULL;
64 * Darn, we need to get the maps allocated for this bus.
66 for (i = 0; i < MAX_PCI_XWIDGET; i++) {
67 if (busnum_to_pcibr_vhdl[i] == pci_bus) {
68 sn_dma_map = busnum_to_atedmamaps[i];
73 * Now get a free dmamap entry from this list.
75 for (i = 0; i < MAX_ATE_MAPS; i++, sn_dma_map++) {
76 if (!sn_dma_map->dma_addr) {
77 sn_dma_map->dma_addr = -1;
78 return( (pciio_dmamap_t) sn_dma_map );
86 * free_pciio_dmamap - free an ATE
87 * @dma_map: ATE to free
89 * Frees the ATE specified by @dma_map.
92 free_pciio_dmamap(pcibr_dmamap_t dma_map)
94 struct sn_dma_maps_s *sn_dma_map;
96 sn_dma_map = (struct sn_dma_maps_s *) dma_map;
97 sn_dma_map->dma_addr = 0;
101 * find_sn_dma_map - find an ATE associated with @dma_addr and @busnum
102 * @dma_addr: DMA address to look for
103 * @busnum: PCI bus to look on
105 * Finds the ATE associated with @dma_addr and @busnum.
107 static struct sn_dma_maps_s *
108 find_sn_dma_map(dma_addr_t dma_addr, unsigned char busnum)
111 struct sn_dma_maps_s *sn_dma_map = NULL;
114 sn_dma_map = busnum_to_atedmamaps[busnum];
116 for (i = 0; i < MAX_ATE_MAPS; i++, sn_dma_map++) {
117 if (sn_dma_map->dma_addr == dma_addr) {
126 * sn_pci_alloc_consistent - allocate memory for coherent DMA
127 * @hwdev: device to allocate for
128 * @size: size of the region
129 * @dma_handle: DMA (bus) address
131 * pci_alloc_consistent() returns a pointer to a memory region suitable for
132 * coherent DMA traffic to/from a PCI device. On SN platforms, this means
133 * that @dma_handle will have the %PCIIO_DMA_CMD flag set.
135 * This interface is usually used for "command" streams (e.g. the command
136 * queue for a SCSI controller). See Documentation/DMA-mapping.txt for
137 * more information. Note that this routine will always put a 32 bit
138 * DMA address into @dma_handle. This is because most devices
139 * that are capable of 64 bit PCI DMA transactions can't do 64 bit _coherent_
140 * DMAs, and unfortunately this interface has to cater to the LCD. Oh well.
142 * Also known as platform_pci_alloc_consistent() by the IA64 machvec code.
145 sn_pci_alloc_consistent(struct pci_dev *hwdev, size_t size, dma_addr_t *dma_handle)
149 struct sn_device_sysdata *device_sysdata;
150 unsigned long phys_addr;
151 pciio_dmamap_t dma_map = 0;
152 struct sn_dma_maps_s *sn_dma_map;
156 if (hwdev->dma_mask < 0xffffffffUL)
160 * Get hwgraph vertex for the device
162 device_sysdata = (struct sn_device_sysdata *) hwdev->sysdata;
163 vhdl = device_sysdata->vhdl;
166 * Allocate the memory. FIXME: if we're allocating for
167 * two devices on the same bus, we should at least try to
168 * allocate memory in the same 2 GB window to avoid using
169 * ATEs for the translation. See the comment above about the
170 * 32 bit requirement for this function.
172 if(!(cpuaddr = (void *)__get_free_pages(GFP_ATOMIC, get_order(size))))
175 /* physical addr. of the memory we just got */
176 phys_addr = __pa(cpuaddr);
179 * This will try to use a Direct Map register to do the
180 * 32 bit DMA mapping, but it may not succeed if another
181 * device on the same bus is already mapped with different
182 * attributes or to a different memory region.
184 *dma_handle = pciio_dmatrans_addr(vhdl, NULL, phys_addr, size,
188 * If this device is in PCI-X mode, the system would have
189 * automatically allocated a 64Bits DMA Address. Error out if the
190 * device cannot support DAC.
192 if (*dma_handle > hwdev->consistent_dma_mask) {
193 free_pages((unsigned long) cpuaddr, get_order(size));
198 * It is a 32 bit card and we cannot do direct mapping,
199 * so we try to use an ATE.
201 if (!(*dma_handle)) {
202 dma_map = pciio_dmamap_alloc(vhdl, NULL, size,
205 printk(KERN_ERR "sn_pci_alloc_consistent: Unable to "
206 "allocate anymore 32 bit page map entries.\n");
209 *dma_handle = (dma_addr_t) pciio_dmamap_addr(dma_map,phys_addr,
211 sn_dma_map = (struct sn_dma_maps_s *)dma_map;
212 sn_dma_map->dma_addr = *dma_handle;
219 * sn_pci_free_consistent - free memory associated with coherent DMAable region
220 * @hwdev: device to free for
221 * @size: size to free
222 * @vaddr: kernel virtual address to free
223 * @dma_handle: DMA address associated with this region
225 * Frees the memory allocated by pci_alloc_consistent(). Also known
226 * as platform_pci_free_consistent() by the IA64 machvec code.
229 sn_pci_free_consistent(struct pci_dev *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
231 struct sn_dma_maps_s *sn_dma_map = NULL;
234 * Get the sn_dma_map entry.
236 if (IS_PCI32_MAPPED(dma_handle))
237 sn_dma_map = find_sn_dma_map(dma_handle, hwdev->bus->number);
240 * and free it if necessary...
243 pciio_dmamap_done((pciio_dmamap_t)sn_dma_map);
244 pciio_dmamap_free((pciio_dmamap_t)sn_dma_map);
245 sn_dma_map->dma_addr = (dma_addr_t)NULL;
247 free_pages((unsigned long) vaddr, get_order(size));
251 * sn_pci_map_sg - map a scatter-gather list for DMA
252 * @hwdev: device to map for
253 * @sg: scatterlist to map
254 * @nents: number of entries
255 * @direction: direction of the DMA transaction
257 * Maps each entry of @sg for DMA. Also known as platform_pci_map_sg by the
261 sn_pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
266 unsigned long phys_addr;
267 struct sn_device_sysdata *device_sysdata;
268 pciio_dmamap_t dma_map;
269 struct sn_dma_maps_s *sn_dma_map;
270 struct scatterlist *saved_sg = sg;
272 /* can't go anywhere w/o a direction in life */
273 if (direction == PCI_DMA_NONE)
277 * Get the hwgraph vertex for the device
279 device_sysdata = (struct sn_device_sysdata *) hwdev->sysdata;
280 vhdl = device_sysdata->vhdl;
283 * Setup a DMA address for each entry in the
286 for (i = 0; i < nents; i++, sg++) {
287 phys_addr = __pa(sg->address ? sg->address :
288 page_address(sg->page) + sg->offset);
291 * Handle the most common case: 64 bit cards. This
292 * call should always succeed.
294 if (IS_PCIA64(hwdev)) {
295 sg->dma_address = pciio_dmatrans_addr(vhdl, NULL, phys_addr,
296 sg->length, PCIIO_DMA_DATA | PCIIO_DMA_A64);
297 sg->dma_length = sg->length;
302 * Handle 32-63 bit cards via direct mapping
304 if (IS_PCI32G(hwdev)) {
305 sg->dma_address = pciio_dmatrans_addr(vhdl, NULL, phys_addr,
306 sg->length, PCIIO_DMA_DATA);
307 sg->dma_length = sg->length;
309 * See if we got a direct map entry
311 if (sg->dma_address) {
318 * It is a 32 bit card and we cannot do direct mapping,
321 dma_map = pciio_dmamap_alloc(vhdl, NULL, sg->length, PCIIO_DMA_DATA);
323 printk(KERN_ERR "sn_pci_map_sg: Unable to allocate "
324 "anymore 32 bit page map entries.\n");
326 * We will need to free all previously allocated entries.
329 sn_pci_unmap_sg(hwdev, saved_sg, i, direction);
334 sg->dma_address = pciio_dmamap_addr(dma_map, phys_addr, sg->length);
335 sg->dma_length = sg->length;
336 sn_dma_map = (struct sn_dma_maps_s *)dma_map;
337 sn_dma_map->dma_addr = sg->dma_address;
345 * sn_pci_unmap_sg - unmap a scatter-gather list
346 * @hwdev: device to unmap
347 * @sg: scatterlist to unmap
348 * @nents: number of scatterlist entries
349 * @direction: DMA direction
351 * Unmap a set of streaming mode DMA translations. Again, cpu read rules
352 * concerning calls here are the same as for pci_unmap_single() below. Also
353 * known as sn_pci_unmap_sg() by the IA64 machvec code.
356 sn_pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
359 struct sn_dma_maps_s *sn_dma_map;
361 /* can't go anywhere w/o a direction in life */
362 if (direction == PCI_DMA_NONE)
365 for (i = 0; i < nents; i++, sg++){
367 if (IS_PCI32_MAPPED(sg->dma_address)) {
369 sn_dma_map = find_sn_dma_map(sg->dma_address, hwdev->bus->number);
371 pciio_dmamap_done((pciio_dmamap_t)sn_dma_map);
372 pciio_dmamap_free((pciio_dmamap_t)sn_dma_map);
373 sn_dma_map->dma_addr = (dma_addr_t)NULL;
377 sg->dma_address = (dma_addr_t)NULL;
383 * sn_pci_map_single - map a single region for DMA
384 * @hwdev: device to map for
385 * @ptr: kernel virtual address of the region to map
386 * @size: size of the region
387 * @direction: DMA direction
389 * Map the region pointed to by @ptr for DMA and return the
390 * DMA address. Also known as platform_pci_map_single() by
391 * the IA64 machvec code.
393 * We map this to the one step pciio_dmamap_trans interface rather than
394 * the two step pciio_dmamap_alloc/pciio_dmamap_addr because we have
395 * no way of saving the dmamap handle from the alloc to later free
396 * (which is pretty much unacceptable).
398 * TODO: simplify our interface;
399 * get rid of dev_desc and vhdl (seems redundant given a pci_dev);
400 * figure out how to save dmamap handle so can use two step.
403 sn_pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size, int direction)
407 unsigned long phys_addr;
408 struct sn_device_sysdata *device_sysdata;
409 pciio_dmamap_t dma_map = NULL;
410 struct sn_dma_maps_s *sn_dma_map;
412 if (direction == PCI_DMA_NONE)
415 /* SN cannot support DMA addresses smaller than 32 bits. */
416 if (IS_PCI32L(hwdev))
420 * find vertex for the device
422 device_sysdata = (struct sn_device_sysdata *)hwdev->sysdata;
423 vhdl = device_sysdata->vhdl;
426 * Call our dmamap interface
429 phys_addr = __pa(ptr);
431 if (IS_PCIA64(hwdev)) {
432 /* This device supports 64 bit DMA addresses. */
433 dma_addr = pciio_dmatrans_addr(vhdl, NULL, phys_addr, size,
434 PCIIO_DMA_DATA | PCIIO_DMA_A64);
439 * Devices that support 32 bit to 63 bit DMA addresses get
440 * 32 bit DMA addresses.
442 * First try to get a 32 bit direct map register.
444 if (IS_PCI32G(hwdev)) {
445 dma_addr = pciio_dmatrans_addr(vhdl, NULL, phys_addr, size,
452 * It's a 32 bit card and we cannot do direct mapping so
453 * let's use the PMU instead.
456 dma_map = pciio_dmamap_alloc(vhdl, NULL, size, PCIIO_DMA_DATA);
459 printk(KERN_ERR "pci_map_single: Unable to allocate anymore "
460 "32 bit page map entries.\n");
464 dma_addr = (dma_addr_t) pciio_dmamap_addr(dma_map, phys_addr, size);
465 sn_dma_map = (struct sn_dma_maps_s *)dma_map;
466 sn_dma_map->dma_addr = dma_addr;
468 return ((dma_addr_t)dma_addr);
472 * sn_pci_unmap_single - unmap a region used for DMA
473 * @hwdev: device to unmap
474 * @dma_addr: DMA address to unmap
475 * @size: size of region
476 * @direction: DMA direction
478 * Unmaps the region pointed to by @dma_addr. Also known as
479 * platform_pci_unmap_single() by the IA64 machvec code.
482 sn_pci_unmap_single(struct pci_dev *hwdev, dma_addr_t dma_addr, size_t size, int direction)
484 struct sn_dma_maps_s *sn_dma_map = NULL;
486 if (direction == PCI_DMA_NONE)
490 * Get the sn_dma_map entry.
492 if (IS_PCI32_MAPPED(dma_addr))
493 sn_dma_map = find_sn_dma_map(dma_addr, hwdev->bus->number);
496 * and free it if necessary...
499 pciio_dmamap_done((pciio_dmamap_t)sn_dma_map);
500 pciio_dmamap_free((pciio_dmamap_t)sn_dma_map);
501 sn_dma_map->dma_addr = (dma_addr_t)NULL;
506 * sn_pci_dma_sync_single - make sure all DMAs have completed
507 * @hwdev: device to sync
508 * @dma_handle: DMA address to sync
509 * @size: size of region
510 * @direction: DMA direction
512 * This routine is supposed to sync the DMA region specified
513 * by @dma_handle into the 'coherence domain'. We do not need to do
514 * anything on our platform.
517 sn_pci_dma_sync_single(struct pci_dev *hwdev, dma_addr_t dma_handle, size_t size, int direction)
524 * sn_pci_dma_sync_sg - make sure all DMAs have completed
525 * @hwdev: device to sync
526 * @sg: scatterlist to sync
527 * @nents: number of entries in the scatterlist
528 * @direction: DMA direction
530 * This routine is supposed to sync the DMA regions specified
531 * by @sg into the 'coherence domain'. We do not need to do anything
535 sn_pci_dma_sync_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
542 * sn_dma_supported - test a DMA mask
543 * @hwdev: device to test
544 * @mask: DMA mask to test
546 * Return whether the given PCI device DMA address mask can be supported
547 * properly. For example, if your device can only drive the low 24-bits
548 * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
549 * this function. Of course, SN only supports devices that have 32 or more
550 * address bits when using the PMU. We could theoretically support <32 bit
551 * cards using direct mapping, but we'll worry about that later--on the off
552 * chance that someone actually wants to use such a card.
555 sn_pci_dma_supported(struct pci_dev *hwdev, u64 mask)
557 if (mask < 0xffffffff)
562 EXPORT_SYMBOL(sn_pci_unmap_single);
563 EXPORT_SYMBOL(sn_pci_map_single);
564 EXPORT_SYMBOL(sn_pci_dma_sync_single);
565 EXPORT_SYMBOL(sn_pci_map_sg);
566 EXPORT_SYMBOL(sn_pci_unmap_sg);
567 EXPORT_SYMBOL(sn_pci_alloc_consistent);
568 EXPORT_SYMBOL(sn_pci_free_consistent);
569 EXPORT_SYMBOL(sn_pci_dma_supported);