pci_dma.c 12.8 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2000,2002-2005 Silicon Graphics, Inc. All rights reserved.
 *
 * Routines for PCI DMA mapping.  See Documentation/DMA-API.txt for
 * a description of how these routines should be used.
 */

12
#include <linux/gfp.h>
L
Linus Torvalds 已提交
13
#include <linux/module.h>
14
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
15
#include <asm/dma.h>
M
Mark Maule 已提交
16
#include <asm/sn/intr.h>
17 18
#include <asm/sn/pcibus_provider_defs.h>
#include <asm/sn/pcidev.h>
19
#include <asm/sn/sn_sal.h>
L
Linus Torvalds 已提交
20

J
Jens Axboe 已提交
21
#define SG_ENT_VIRT_ADDRESS(sg)	(sg_virt((sg)))
L
Linus Torvalds 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34
#define SG_ENT_PHYS_ADDRESS(SG)	virt_to_phys(SG_ENT_VIRT_ADDRESS(SG))

/**
 * sn_dma_supported - test a DMA mask
 * @dev: device to test
 * @mask: DMA mask to test
 *
 * Return whether the given PCI device DMA address mask can be supported
 * properly.  For example, if your device can only drive the low 24-bits
 * during PCI bus mastering, then you would pass 0x00ffffff as the mask to
 * this function.  Of course, SN only supports devices that have 32 or more
 * address bits when using the PMU.
 */
35
static int sn_dma_supported(struct device *dev, u64 mask)
L
Linus Torvalds 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
{
	BUG_ON(dev->bus != &pci_bus_type);

	if (mask < 0x7fffffff)
		return 0;
	return 1;
}

/**
 * sn_dma_set_mask - set the DMA mask
 * @dev: device to set
 * @dma_mask: new mask
 *
 * Set @dev's DMA mask if the hw supports it.
 */
int sn_dma_set_mask(struct device *dev, u64 dma_mask)
{
	BUG_ON(dev->bus != &pci_bus_type);

	if (!sn_dma_supported(dev, dma_mask))
		return 0;

	*dev->dma_mask = dma_mask;
	return 1;
}
EXPORT_SYMBOL(sn_dma_set_mask);

/**
 * sn_dma_alloc_coherent - allocate memory for coherent DMA
 * @dev: device to allocate for
 * @size: size of the region
 * @dma_handle: DMA (bus) address
 * @flags: memory allocation flags
 *
 * dma_alloc_coherent() returns a pointer to a memory region suitable for
 * coherent DMA traffic to/from a PCI device.  On SN platforms, this means
 * that @dma_handle will have the %PCIIO_DMA_CMD flag set.
 *
 * This interface is usually used for "command" streams (e.g. the command
 * queue for a SCSI controller).  See Documentation/DMA-API.txt for
 * more information.
 */
78 79
static void *sn_dma_alloc_coherent(struct device *dev, size_t size,
				   dma_addr_t * dma_handle, gfp_t flags)
L
Linus Torvalds 已提交
80 81 82
{
	void *cpuaddr;
	unsigned long phys_addr;
83
	int node;
84 85
	struct pci_dev *pdev = to_pci_dev(dev);
	struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
L
Linus Torvalds 已提交
86 87 88 89 90 91

	BUG_ON(dev->bus != &pci_bus_type);

	/*
	 * Allocate the memory.
	 */
92 93
	node = pcibus_to_node(pdev->bus);
	if (likely(node >=0)) {
94 95
		struct page *p = alloc_pages_exact_node(node,
						flags, get_order(size));
96 97 98 99 100 101

		if (likely(p))
			cpuaddr = page_address(p);
		else
			return NULL;
	} else
102
		cpuaddr = (void *)__get_free_pages(flags, get_order(size));
103 104

	if (unlikely(!cpuaddr))
L
Linus Torvalds 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117
		return NULL;

	memset(cpuaddr, 0x0, size);

	/* physical addr. of the memory we just got */
	phys_addr = __pa(cpuaddr);

	/*
	 * 64 bit address translations should never fail.
	 * 32 bit translations can fail if there are insufficient mapping
	 * resources.
	 */

M
Mark Maule 已提交
118 119
	*dma_handle = provider->dma_map_consistent(pdev, phys_addr, size,
						   SN_DMA_ADDR_PHYS);
L
Linus Torvalds 已提交
120
	if (!*dma_handle) {
121
		printk(KERN_ERR "%s: out of ATEs\n", __func__);
L
Linus Torvalds 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
		free_pages((unsigned long)cpuaddr, get_order(size));
		return NULL;
	}

	return cpuaddr;
}

/**
 * sn_pci_free_coherent - free memory associated with coherent DMAable region
 * @dev: device to free for
 * @size: size to free
 * @cpu_addr: kernel virtual address to free
 * @dma_handle: DMA address associated with this region
 *
 * Frees the memory allocated by dma_alloc_coherent(), potentially unmapping
 * any associated IOMMU mappings.
 */
139 140
static void sn_dma_free_coherent(struct device *dev, size_t size, void *cpu_addr,
				 dma_addr_t dma_handle)
L
Linus Torvalds 已提交
141
{
142 143
	struct pci_dev *pdev = to_pci_dev(dev);
	struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
L
Linus Torvalds 已提交
144 145 146

	BUG_ON(dev->bus != &pci_bus_type);

147
	provider->dma_unmap(pdev, dma_handle, 0);
L
Linus Torvalds 已提交
148 149 150 151
	free_pages((unsigned long)cpu_addr, get_order(size));
}

/**
152
 * sn_dma_map_single_attrs - map a single page for DMA
L
Linus Torvalds 已提交
153 154 155 156
 * @dev: device to map for
 * @cpu_addr: kernel virtual address of the region to map
 * @size: size of the region
 * @direction: DMA direction
157
 * @attrs: optional dma attributes
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166
 *
 * Map the region pointed to by @cpu_addr for DMA and return the
 * DMA address.
 *
 * We map this to the one step pcibr_dmamap_trans interface rather than
 * the two step pcibr_dmamap_alloc/pcibr_dmamap_addr because we have
 * no way of saving the dmamap handle from the alloc to later free
 * (which is pretty much unacceptable).
 *
167 168 169 170 171
 * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
 * dma_map_consistent() so that writes force a flush of pending DMA.
 * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
 * Document Number: 007-4763-001)
 *
L
Linus Torvalds 已提交
172 173 174
 * TODO: simplify our interface;
 *       figure out how to save dmamap handle so can use two step.
 */
175 176 177 178
static dma_addr_t sn_dma_map_page(struct device *dev, struct page *page,
				  unsigned long offset, size_t size,
				  enum dma_data_direction dir,
				  struct dma_attrs *attrs)
L
Linus Torvalds 已提交
179
{
180
	void *cpu_addr = page_address(page) + offset;
L
Linus Torvalds 已提交
181 182
	dma_addr_t dma_addr;
	unsigned long phys_addr;
183 184
	struct pci_dev *pdev = to_pci_dev(dev);
	struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
185 186 187
	int dmabarr;

	dmabarr = dma_get_attr(DMA_ATTR_WRITE_BARRIER, attrs);
L
Linus Torvalds 已提交
188 189 190 191

	BUG_ON(dev->bus != &pci_bus_type);

	phys_addr = __pa(cpu_addr);
192 193 194 195 196 197 198
	if (dmabarr)
		dma_addr = provider->dma_map_consistent(pdev, phys_addr,
							size, SN_DMA_ADDR_PHYS);
	else
		dma_addr = provider->dma_map(pdev, phys_addr, size,
					     SN_DMA_ADDR_PHYS);

L
Linus Torvalds 已提交
199
	if (!dma_addr) {
200
		printk(KERN_ERR "%s: out of ATEs\n", __func__);
L
Linus Torvalds 已提交
201 202 203 204 205 206
		return 0;
	}
	return dma_addr;
}

/**
207
 * sn_dma_unmap_single_attrs - unamp a DMA mapped page
L
Linus Torvalds 已提交
208 209 210 211
 * @dev: device to sync
 * @dma_addr: DMA address to sync
 * @size: size of region
 * @direction: DMA direction
212
 * @attrs: optional dma attributes
L
Linus Torvalds 已提交
213 214 215 216 217
 *
 * This routine is supposed to sync the DMA region specified
 * by @dma_handle into the coherence domain.  On SN, we're always cache
 * coherent, so we just need to free any ATEs associated with this mapping.
 */
218 219 220
static void sn_dma_unmap_page(struct device *dev, dma_addr_t dma_addr,
			      size_t size, enum dma_data_direction dir,
			      struct dma_attrs *attrs)
L
Linus Torvalds 已提交
221
{
222 223
	struct pci_dev *pdev = to_pci_dev(dev);
	struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
L
Linus Torvalds 已提交
224 225

	BUG_ON(dev->bus != &pci_bus_type);
226

227
	provider->dma_unmap(pdev, dma_addr, dir);
L
Linus Torvalds 已提交
228 229 230
}

/**
231
 * sn_dma_unmap_sg - unmap a DMA scatterlist
L
Linus Torvalds 已提交
232 233 234 235
 * @dev: device to unmap
 * @sg: scatterlist to unmap
 * @nhwentries: number of scatterlist entries
 * @direction: DMA direction
236
 * @attrs: optional dma attributes
L
Linus Torvalds 已提交
237 238 239
 *
 * Unmap a set of streaming mode DMA translations.
 */
240 241 242
static void sn_dma_unmap_sg(struct device *dev, struct scatterlist *sgl,
			    int nhwentries, enum dma_data_direction dir,
			    struct dma_attrs *attrs)
L
Linus Torvalds 已提交
243 244
{
	int i;
245 246
	struct pci_dev *pdev = to_pci_dev(dev);
	struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
J
Jens Axboe 已提交
247
	struct scatterlist *sg;
L
Linus Torvalds 已提交
248 249 250

	BUG_ON(dev->bus != &pci_bus_type);

J
Jens Axboe 已提交
251
	for_each_sg(sgl, sg, nhwentries, i) {
252
		provider->dma_unmap(pdev, sg->dma_address, dir);
L
Linus Torvalds 已提交
253 254 255 256 257 258
		sg->dma_address = (dma_addr_t) NULL;
		sg->dma_length = 0;
	}
}

/**
259
 * sn_dma_map_sg - map a scatterlist for DMA
L
Linus Torvalds 已提交
260 261 262 263
 * @dev: device to map for
 * @sg: scatterlist to map
 * @nhwentries: number of entries
 * @direction: direction of the DMA transaction
264 265 266 267 268 269
 * @attrs: optional dma attributes
 *
 * mappings with the DMA_ATTR_WRITE_BARRIER get mapped with
 * dma_map_consistent() so that writes force a flush of pending DMA.
 * (See "SGI Altix Architecture Considerations for Linux Device Drivers",
 * Document Number: 007-4763-001)
L
Linus Torvalds 已提交
270 271 272
 *
 * Maps each entry of @sg for DMA.
 */
273 274 275
static int sn_dma_map_sg(struct device *dev, struct scatterlist *sgl,
			 int nhwentries, enum dma_data_direction dir,
			 struct dma_attrs *attrs)
L
Linus Torvalds 已提交
276 277
{
	unsigned long phys_addr;
J
Jens Axboe 已提交
278
	struct scatterlist *saved_sg = sgl, *sg;
279 280
	struct pci_dev *pdev = to_pci_dev(dev);
	struct sn_pcibus_provider *provider = SN_PCIDEV_BUSPROVIDER(pdev);
L
Linus Torvalds 已提交
281
	int i;
282 283 284
	int dmabarr;

	dmabarr = dma_get_attr(DMA_ATTR_WRITE_BARRIER, attrs);
L
Linus Torvalds 已提交
285 286 287 288 289 290

	BUG_ON(dev->bus != &pci_bus_type);

	/*
	 * Setup a DMA address for each entry in the scatterlist.
	 */
J
Jens Axboe 已提交
291
	for_each_sg(sgl, sg, nhwentries, i) {
292
		dma_addr_t dma_addr;
L
Linus Torvalds 已提交
293
		phys_addr = SG_ENT_PHYS_ADDRESS(sg);
294 295 296 297 298 299 300 301 302
		if (dmabarr)
			dma_addr = provider->dma_map_consistent(pdev,
								phys_addr,
								sg->length,
								SN_DMA_ADDR_PHYS);
		else
			dma_addr = provider->dma_map(pdev, phys_addr,
						     sg->length,
						     SN_DMA_ADDR_PHYS);
L
Linus Torvalds 已提交
303

304
		sg->dma_address = dma_addr;
L
Linus Torvalds 已提交
305
		if (!sg->dma_address) {
306
			printk(KERN_ERR "%s: out of ATEs\n", __func__);
L
Linus Torvalds 已提交
307 308 309 310 311

			/*
			 * Free any successfully allocated entries.
			 */
			if (i > 0)
312
				sn_dma_unmap_sg(dev, saved_sg, i, dir, attrs);
L
Linus Torvalds 已提交
313 314 315 316 317 318 319 320 321
			return 0;
		}

		sg->dma_length = sg->length;
	}

	return nhwentries;
}

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)
L
Linus Torvalds 已提交
324 325 326 327
{
	BUG_ON(dev->bus != &pci_bus_type);
}

328
static void sn_dma_sync_single_for_device(struct device *dev, dma_addr_t dma_handle,
329 330
					  size_t size,
					  enum dma_data_direction dir)
L
Linus Torvalds 已提交
331 332 333 334
{
	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)
L
Linus Torvalds 已提交
337 338 339 340
{
	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)
L
Linus Torvalds 已提交
343 344 345 346
{
	BUG_ON(dev->bus != &pci_bus_type);
}

347
static int sn_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
L
Linus Torvalds 已提交
348 349 350 351
{
	return 0;
}

352 353
u64 sn_dma_get_required_mask(struct device *dev)
{
354
	return DMA_BIT_MASK(64);
355 356 357
}
EXPORT_SYMBOL_GPL(sn_dma_get_required_mask);

L
Linus Torvalds 已提交
358 359 360 361 362 363 364 365 366 367 368 369
char *sn_pci_get_legacy_mem(struct pci_bus *bus)
{
	if (!SN_PCIBUS_BUSSOFT(bus))
		return ERR_PTR(-ENODEV);

	return (char *)(SN_PCIBUS_BUSSOFT(bus)->bs_legacy_mem | __IA64_UNCACHED_OFFSET);
}

int sn_pci_legacy_read(struct pci_bus *bus, u16 port, u32 *val, u8 size)
{
	unsigned long addr;
	int ret;
370 371 372 373 374
	struct ia64_sal_retval isrv;

	/*
	 * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
	 * around hw issues at the pci bus level.  SGI proms older than
S
Simon Arlott 已提交
375
	 * 4.10 don't implement this.
376 377 378
	 */

	SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
379 380 381 382
		 pci_domain_nr(bus), bus->number,
		 0, /* io */
		 0, /* read */
		 port, size, __pa(val));
383 384 385 386 387 388 389

	if (isrv.status == 0)
		return size;

	/*
	 * If the above failed, retry using the SAL_PROBE call which should
	 * be present in all proms (but which cannot work round PCI chipset
S
Simon Arlott 已提交
390
	 * bugs).  This code is retained for compatibility with old
391 392
	 * pre-4.10 proms, and should be removed at some point in the future.
	 */
L
Linus Torvalds 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415

	if (!SN_PCIBUS_BUSSOFT(bus))
		return -ENODEV;

	addr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
	addr += port;

	ret = ia64_sn_probe_mem(addr, (long)size, (void *)val);

	if (ret == 2)
		return -EINVAL;

	if (ret == 1)
		*val = -1;

	return size;
}

int sn_pci_legacy_write(struct pci_bus *bus, u16 port, u32 val, u8 size)
{
	int ret = size;
	unsigned long paddr;
	unsigned long *addr;
416 417 418 419 420
	struct ia64_sal_retval isrv;

	/*
	 * First, try the SN_SAL_IOIF_PCI_SAFE SAL call which can work
	 * around hw issues at the pci bus level.  SGI proms older than
S
Simon Arlott 已提交
421
	 * 4.10 don't implement this.
422 423 424
	 */

	SAL_CALL(isrv, SN_SAL_IOIF_PCI_SAFE,
425 426 427 428
		 pci_domain_nr(bus), bus->number,
		 0, /* io */
		 1, /* write */
		 port, size, __pa(&val));
429 430 431 432 433 434 435

	if (isrv.status == 0)
		return size;

	/*
	 * If the above failed, retry using the SAL_PROBE call which should
	 * be present in all proms (but which cannot work round PCI chipset
S
Simon Arlott 已提交
436
	 * bugs).  This code is retained for compatibility with old
437 438
	 * pre-4.10 proms, and should be removed at some point in the future.
	 */
L
Linus Torvalds 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

	if (!SN_PCIBUS_BUSSOFT(bus)) {
		ret = -ENODEV;
		goto out;
	}

	/* Put the phys addr in uncached space */
	paddr = SN_PCIBUS_BUSSOFT(bus)->bs_legacy_io | __IA64_UNCACHED_OFFSET;
	paddr += port;
	addr = (unsigned long *)paddr;

	switch (size) {
	case 1:
		*(volatile u8 *)(addr) = (u8)(val);
		break;
	case 2:
		*(volatile u16 *)(addr) = (u16)(val);
		break;
	case 4:
		*(volatile u32 *)(addr) = (u32)(val);
		break;
	default:
		ret = -EINVAL;
		break;
	}
 out:
	return ret;
}
467

468
static struct dma_map_ops sn_dma_ops = {
469 470
	.alloc_coherent		= sn_dma_alloc_coherent,
	.free_coherent		= sn_dma_free_coherent,
471 472 473 474
	.map_page		= sn_dma_map_page,
	.unmap_page		= sn_dma_unmap_page,
	.map_sg			= sn_dma_map_sg,
	.unmap_sg		= sn_dma_unmap_sg,
475 476 477 478 479
	.sync_single_for_cpu 	= sn_dma_sync_single_for_cpu,
	.sync_sg_for_cpu	= sn_dma_sync_sg_for_cpu,
	.sync_single_for_device = sn_dma_sync_single_for_device,
	.sync_sg_for_device	= sn_dma_sync_sg_for_device,
	.mapping_error		= sn_dma_mapping_error,
480
	.dma_supported		= sn_dma_supported,
481
};
F
FUJITA Tomonori 已提交
482 483 484 485 486

void sn_dma_init(void)
{
	dma_ops = &sn_dma_ops;
}