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) 2000,2002-2003 Silicon Graphics, Inc. All rights reserved.
7  *
8  * Routines for PCI DMA mapping.  See Documentation/DMA-mapping.txt for
9  * a description of how these routines should be used.
10  */
11 
12 #include <linux/config.h>
13 #include <linux/types.h>
14 #include <linux/mm.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>
20 
21 #include <asm/delay.h>
22 #include <asm/io.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>
34 
35 /*
36  * For ATE allocations
37  */
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);
42 
43 /*
44  * Toplogy stuff
45  */
46 extern vertex_hdl_t busnum_to_pcibr_vhdl[];
47 extern nasid_t busnum_to_nid[];
48 extern void * busnum_to_atedmamaps[];
49 
50 /**
51  * get_free_pciio_dmamap - find and allocate an ATE
52  * @pci_bus: PCI bus to get an entry for
53  *
54  * Finds and allocates an ATE on the PCI bus specified
55  * by @pci_bus.
56  */
57 pciio_dmamap_t
get_free_pciio_dmamap(vertex_hdl_t pci_bus)58 get_free_pciio_dmamap(vertex_hdl_t pci_bus)
59 {
60 	int i;
61 	struct sn_dma_maps_s *sn_dma_map = NULL;
62 
63 	/*
64 	 * Darn, we need to get the maps allocated for this bus.
65 	 */
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];
69 		}
70 	}
71 
72 	/*
73 	 * Now get a free dmamap entry from this list.
74 	 */
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 );
79 		}
80 	}
81 
82 	return NULL;
83 }
84 
85 /**
86  * free_pciio_dmamap - free an ATE
87  * @dma_map: ATE to free
88  *
89  * Frees the ATE specified by @dma_map.
90  */
91 void
free_pciio_dmamap(pcibr_dmamap_t dma_map)92 free_pciio_dmamap(pcibr_dmamap_t dma_map)
93 {
94 	struct sn_dma_maps_s *sn_dma_map;
95 
96 	sn_dma_map = (struct sn_dma_maps_s *) dma_map;
97 	sn_dma_map->dma_addr = 0;
98 }
99 
100 /**
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
104  *
105  * Finds the ATE associated with @dma_addr and @busnum.
106  */
107 static struct sn_dma_maps_s *
find_sn_dma_map(dma_addr_t dma_addr,unsigned char busnum)108 find_sn_dma_map(dma_addr_t dma_addr, unsigned char busnum)
109 {
110 
111 	struct sn_dma_maps_s *sn_dma_map = NULL;
112 	int i;
113 
114 	sn_dma_map = busnum_to_atedmamaps[busnum];
115 
116 	for (i = 0; i < MAX_ATE_MAPS; i++, sn_dma_map++) {
117 		if (sn_dma_map->dma_addr == dma_addr) {
118 			return sn_dma_map;
119 		}
120 	}
121 
122 	return NULL;
123 }
124 
125 /**
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
130  *
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.
134  *
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.
141  *
142  * Also known as platform_pci_alloc_consistent() by the IA64 machvec code.
143  */
144 void *
sn_pci_alloc_consistent(struct pci_dev * hwdev,size_t size,dma_addr_t * dma_handle)145 sn_pci_alloc_consistent(struct pci_dev *hwdev, size_t size, dma_addr_t *dma_handle)
146 {
147         void *cpuaddr;
148 	vertex_hdl_t vhdl;
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;
153 
154 	*dma_handle = 0;
155 
156 	if (hwdev->dma_mask < 0xffffffffUL)
157 		return NULL;
158 
159 	/*
160 	 * Get hwgraph vertex for the device
161 	 */
162 	device_sysdata = (struct sn_device_sysdata *) hwdev->sysdata;
163 	vhdl = device_sysdata->vhdl;
164 
165 	/*
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.
171 	 */
172 	if(!(cpuaddr = (void *)__get_free_pages(GFP_ATOMIC, get_order(size))))
173 		return NULL;
174 
175 	/* physical addr. of the memory we just got */
176 	phys_addr = __pa(cpuaddr);
177 
178 	/*
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.
183 	 */
184 	*dma_handle = pciio_dmatrans_addr(vhdl, NULL, phys_addr, size,
185 					  PCIIO_DMA_CMD);
186 
187         /*
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.
191 	 */
192 	if (*dma_handle > hwdev->consistent_dma_mask) {
193 		free_pages((unsigned long) cpuaddr, get_order(size));
194 		return NULL;
195 	}
196 
197 	/*
198 	 * It is a 32 bit card and we cannot do direct mapping,
199 	 * so we try to use an ATE.
200 	 */
201 	if (!(*dma_handle)) {
202 		dma_map = pciio_dmamap_alloc(vhdl, NULL, size,
203 					     PCIIO_DMA_CMD);
204 		if (!dma_map) {
205 			printk(KERN_ERR "sn_pci_alloc_consistent: Unable to "
206 			       "allocate anymore 32 bit page map entries.\n");
207 			return 0;
208 		}
209 		*dma_handle = (dma_addr_t) pciio_dmamap_addr(dma_map,phys_addr,
210 							     size);
211 		sn_dma_map = (struct sn_dma_maps_s *)dma_map;
212 		sn_dma_map->dma_addr = *dma_handle;
213 	}
214 
215         return cpuaddr;
216 }
217 
218 /**
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
224  *
225  * Frees the memory allocated by pci_alloc_consistent().  Also known
226  * as platform_pci_free_consistent() by the IA64 machvec code.
227  */
228 void
sn_pci_free_consistent(struct pci_dev * hwdev,size_t size,void * vaddr,dma_addr_t dma_handle)229 sn_pci_free_consistent(struct pci_dev *hwdev, size_t size, void *vaddr, dma_addr_t dma_handle)
230 {
231 	struct sn_dma_maps_s *sn_dma_map = NULL;
232 
233 	/*
234 	 * Get the sn_dma_map entry.
235 	 */
236 	if (IS_PCI32_MAPPED(dma_handle))
237 		sn_dma_map = find_sn_dma_map(dma_handle, hwdev->bus->number);
238 
239 	/*
240 	 * and free it if necessary...
241 	 */
242 	if (sn_dma_map) {
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;
246 	}
247 	free_pages((unsigned long) vaddr, get_order(size));
248 }
249 
250 /**
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
256  *
257  * Maps each entry of @sg for DMA.  Also known as platform_pci_map_sg by the
258  * IA64 machvec code.
259  */
260 int
sn_pci_map_sg(struct pci_dev * hwdev,struct scatterlist * sg,int nents,int direction)261 sn_pci_map_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
262 {
263 
264 	int i;
265 	vertex_hdl_t vhdl;
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;
271 
272 	/* can't go anywhere w/o a direction in life */
273 	if (direction == PCI_DMA_NONE)
274 		BUG();
275 
276 	/*
277 	 * Get the hwgraph vertex for the device
278 	 */
279 	device_sysdata = (struct sn_device_sysdata *) hwdev->sysdata;
280 	vhdl = device_sysdata->vhdl;
281 
282 	/*
283 	 * Setup a DMA address for each entry in the
284 	 * scatterlist.
285 	 */
286 	for (i = 0; i < nents; i++, sg++) {
287 		phys_addr = __pa(sg->address ? sg->address :
288 			page_address(sg->page) + sg->offset);
289 
290 		/*
291 		 * Handle the most common case: 64 bit cards.  This
292 		 * call should always succeed.
293 		 */
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;
298 			continue;
299 		}
300 
301 		/*
302 		 * Handle 32-63 bit cards via direct mapping
303 		 */
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;
308 			/*
309 			 * See if we got a direct map entry
310 			 */
311 			if (sg->dma_address) {
312 				continue;
313 			}
314 
315 		}
316 
317 		/*
318 		 * It is a 32 bit card and we cannot do direct mapping,
319 		 * so we use an ATE.
320 		 */
321 		dma_map = pciio_dmamap_alloc(vhdl, NULL, sg->length, PCIIO_DMA_DATA);
322 		if (!dma_map) {
323 			printk(KERN_ERR "sn_pci_map_sg: Unable to allocate "
324 			       "anymore 32 bit page map entries.\n");
325 			/*
326 			 * We will need to free all previously allocated entries.
327 			 */
328 			if (i > 0) {
329 				sn_pci_unmap_sg(hwdev, saved_sg, i, direction);
330 			}
331 			return (0);
332 		}
333 
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;
338 	}
339 
340 	return nents;
341 
342 }
343 
344 /**
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
350  *
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.
354  */
355 void
sn_pci_unmap_sg(struct pci_dev * hwdev,struct scatterlist * sg,int nents,int direction)356 sn_pci_unmap_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
357 {
358 	int i;
359 	struct sn_dma_maps_s *sn_dma_map;
360 
361 	/* can't go anywhere w/o a direction in life */
362 	if (direction == PCI_DMA_NONE)
363 		BUG();
364 
365 	for (i = 0; i < nents; i++, sg++){
366 
367 		if (IS_PCI32_MAPPED(sg->dma_address)) {
368 			sn_dma_map = NULL;
369                 	sn_dma_map = find_sn_dma_map(sg->dma_address, hwdev->bus->number);
370         		if (sn_dma_map) {
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;
374 			}
375         	}
376 
377 		sg->dma_address = (dma_addr_t)NULL;
378 		sg->dma_length = 0;
379 	}
380 }
381 
382 /**
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
388  *
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.
392  *
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).
397  *
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.
401  */
402 dma_addr_t
sn_pci_map_single(struct pci_dev * hwdev,void * ptr,size_t size,int direction)403 sn_pci_map_single(struct pci_dev *hwdev, void *ptr, size_t size, int direction)
404 {
405 	vertex_hdl_t vhdl;
406 	dma_addr_t dma_addr;
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;
411 
412 	if (direction == PCI_DMA_NONE)
413 		BUG();
414 
415 	/* SN cannot support DMA addresses smaller than 32 bits. */
416 	if (IS_PCI32L(hwdev))
417 		return 0;
418 
419 	/*
420 	 * find vertex for the device
421 	 */
422 	device_sysdata = (struct sn_device_sysdata *)hwdev->sysdata;
423 	vhdl = device_sysdata->vhdl;
424 
425 	/*
426 	 * Call our dmamap interface
427 	 */
428 	dma_addr = 0;
429 	phys_addr = __pa(ptr);
430 
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);
435 		return dma_addr;
436 	}
437 
438 	/*
439 	 * Devices that support 32 bit to 63 bit DMA addresses get
440 	 * 32 bit DMA addresses.
441 	 *
442 	 * First try to get a 32 bit direct map register.
443 	 */
444 	if (IS_PCI32G(hwdev)) {
445 		dma_addr = pciio_dmatrans_addr(vhdl, NULL, phys_addr, size,
446 					       PCIIO_DMA_DATA);
447 		if (dma_addr)
448 			return dma_addr;
449 	}
450 
451 	/*
452 	 * It's a 32 bit card and we cannot do direct mapping so
453 	 * let's use the PMU instead.
454 	 */
455 	dma_map = NULL;
456 	dma_map = pciio_dmamap_alloc(vhdl, NULL, size, PCIIO_DMA_DATA);
457 
458 	if (!dma_map) {
459 		printk(KERN_ERR "pci_map_single: Unable to allocate anymore "
460 		       "32 bit page map entries.\n");
461 		return 0;
462 	}
463 
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;
467 
468 	return ((dma_addr_t)dma_addr);
469 }
470 
471 /**
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
477  *
478  * Unmaps the region pointed to by @dma_addr.  Also known as
479  * platform_pci_unmap_single() by the IA64 machvec code.
480  */
481 void
sn_pci_unmap_single(struct pci_dev * hwdev,dma_addr_t dma_addr,size_t size,int direction)482 sn_pci_unmap_single(struct pci_dev *hwdev, dma_addr_t dma_addr, size_t size, int direction)
483 {
484 	struct sn_dma_maps_s *sn_dma_map = NULL;
485 
486         if (direction == PCI_DMA_NONE)
487 		BUG();
488 
489 	/*
490 	 * Get the sn_dma_map entry.
491 	 */
492 	if (IS_PCI32_MAPPED(dma_addr))
493 		sn_dma_map = find_sn_dma_map(dma_addr, hwdev->bus->number);
494 
495 	/*
496 	 * and free it if necessary...
497 	 */
498 	if (sn_dma_map) {
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;
502 	}
503 }
504 
505 /**
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
511  *
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.
515  */
516 void
sn_pci_dma_sync_single(struct pci_dev * hwdev,dma_addr_t dma_handle,size_t size,int direction)517 sn_pci_dma_sync_single(struct pci_dev *hwdev, dma_addr_t dma_handle, size_t size, int direction)
518 {
519 	return;
520 
521 }
522 
523 /**
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
529  *
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
532  * on our platform.
533  */
534 void
sn_pci_dma_sync_sg(struct pci_dev * hwdev,struct scatterlist * sg,int nents,int direction)535 sn_pci_dma_sync_sg(struct pci_dev *hwdev, struct scatterlist *sg, int nents, int direction)
536 {
537 	return;
538 
539 }
540 
541 /**
542  * sn_dma_supported - test a DMA mask
543  * @hwdev: device to test
544  * @mask: DMA mask to test
545  *
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.
553  */
554 int
sn_pci_dma_supported(struct pci_dev * hwdev,u64 mask)555 sn_pci_dma_supported(struct pci_dev *hwdev, u64 mask)
556 {
557 	if (mask < 0xffffffff)
558 		return 0;
559 	return 1;
560 }
561 
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);
570 
571