2005-04-17 00:20:36 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation
|
2005-10-14 06:51:42 +02:00
|
|
|
*
|
|
|
|
* Rewrite, cleanup, new allocation schemes:
|
2005-04-17 00:20:36 +02:00
|
|
|
* Copyright (C) 2004 Olof Johansson, IBM Corporation
|
|
|
|
*
|
|
|
|
* Dynamic DMA mapping support, platform-independent parts.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2005-10-14 06:51:42 +02:00
|
|
|
*
|
2005-04-17 00:20:36 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2005-10-14 06:51:42 +02:00
|
|
|
*
|
2005-04-17 00:20:36 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/mm.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/pci.h>
|
|
|
|
#include <linux/dma-mapping.h>
|
|
|
|
#include <asm/io.h>
|
|
|
|
#include <asm/prom.h>
|
|
|
|
#include <asm/iommu.h>
|
|
|
|
#include <asm/pci-bridge.h>
|
|
|
|
#include <asm/machdep.h>
|
2005-09-27 18:50:25 +02:00
|
|
|
#include <asm/ppc-pci.h>
|
2005-04-17 00:20:36 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We can use ->sysdata directly and avoid the extra work in
|
|
|
|
* pci_device_to_OF_node since ->sysdata will have been initialised
|
|
|
|
* in the iommu init code for all devices.
|
|
|
|
*/
|
|
|
|
#define PCI_GET_DN(dev) ((struct device_node *)((dev)->sysdata))
|
|
|
|
|
2006-04-28 15:57:09 +02:00
|
|
|
static inline struct iommu_table *device_to_table(struct device *hwdev)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
|
2006-04-28 15:57:09 +02:00
|
|
|
if (!hwdev) {
|
2005-04-17 00:20:36 +02:00
|
|
|
pdev = ppc64_isabridge_dev;
|
|
|
|
if (!pdev)
|
|
|
|
return NULL;
|
|
|
|
} else
|
2006-04-28 15:57:09 +02:00
|
|
|
pdev = to_pci_dev(hwdev);
|
2005-04-17 00:20:36 +02:00
|
|
|
|
2005-09-06 05:17:54 +02:00
|
|
|
return PCI_DN(PCI_GET_DN(pdev))->iommu_table;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-13 04:05:59 +02:00
|
|
|
static inline unsigned long device_to_mask(struct device *hwdev)
|
|
|
|
{
|
|
|
|
struct pci_dev *pdev;
|
|
|
|
|
|
|
|
if (!hwdev) {
|
|
|
|
pdev = ppc64_isabridge_dev;
|
|
|
|
if (!pdev) /* This is the best guess we can do */
|
|
|
|
return 0xfffffffful;
|
|
|
|
} else
|
|
|
|
pdev = to_pci_dev(hwdev);
|
|
|
|
|
|
|
|
if (pdev->dma_mask)
|
|
|
|
return pdev->dma_mask;
|
|
|
|
|
|
|
|
/* Assume devices without mask can take 32 bit addresses */
|
|
|
|
return 0xfffffffful;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-17 00:20:36 +02:00
|
|
|
/* Allocates a contiguous real buffer and creates mappings over it.
|
|
|
|
* Returns the virtual address of the buffer and sets dma_handle
|
|
|
|
* to the dma address (mapping) of the first page.
|
|
|
|
*/
|
|
|
|
static void *pci_iommu_alloc_coherent(struct device *hwdev, size_t size,
|
2005-10-07 08:46:04 +02:00
|
|
|
dma_addr_t *dma_handle, gfp_t flag)
|
2005-04-17 00:20:36 +02:00
|
|
|
{
|
2006-04-28 15:57:09 +02:00
|
|
|
return iommu_alloc_coherent(device_to_table(hwdev), size, dma_handle,
|
2006-04-13 04:05:59 +02:00
|
|
|
device_to_mask(hwdev), flag);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_iommu_free_coherent(struct device *hwdev, size_t size,
|
|
|
|
void *vaddr, dma_addr_t dma_handle)
|
|
|
|
{
|
2006-04-28 15:57:09 +02:00
|
|
|
iommu_free_coherent(device_to_table(hwdev), size, vaddr, dma_handle);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Creates TCEs for a user provided buffer. The user buffer must be
|
|
|
|
* contiguous real kernel storage (not vmalloc). The address of the buffer
|
|
|
|
* passed here is the kernel (virtual) address of the buffer. The buffer
|
|
|
|
* need not be page aligned, the dma_addr_t returned will point to the same
|
|
|
|
* byte within the page as vaddr.
|
|
|
|
*/
|
|
|
|
static dma_addr_t pci_iommu_map_single(struct device *hwdev, void *vaddr,
|
|
|
|
size_t size, enum dma_data_direction direction)
|
|
|
|
{
|
2006-04-28 15:57:09 +02:00
|
|
|
return iommu_map_single(device_to_table(hwdev), vaddr, size,
|
2006-04-13 04:05:59 +02:00
|
|
|
device_to_mask(hwdev), direction);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void pci_iommu_unmap_single(struct device *hwdev, dma_addr_t dma_handle,
|
|
|
|
size_t size, enum dma_data_direction direction)
|
|
|
|
{
|
2006-04-28 15:57:09 +02:00
|
|
|
iommu_unmap_single(device_to_table(hwdev), dma_handle, size, direction);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int pci_iommu_map_sg(struct device *pdev, struct scatterlist *sglist,
|
|
|
|
int nelems, enum dma_data_direction direction)
|
|
|
|
{
|
2006-04-28 15:57:09 +02:00
|
|
|
return iommu_map_sg(pdev, device_to_table(pdev), sglist,
|
2006-04-13 04:05:59 +02:00
|
|
|
nelems, device_to_mask(pdev), direction);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void pci_iommu_unmap_sg(struct device *pdev, struct scatterlist *sglist,
|
|
|
|
int nelems, enum dma_data_direction direction)
|
|
|
|
{
|
2006-04-28 15:57:09 +02:00
|
|
|
iommu_unmap_sg(device_to_table(pdev), sglist, nelems, direction);
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We support DMA to/from any memory page via the iommu */
|
|
|
|
static int pci_iommu_dma_supported(struct device *dev, u64 mask)
|
|
|
|
{
|
2006-04-28 15:57:09 +02:00
|
|
|
struct iommu_table *tbl = device_to_table(dev);
|
2006-04-13 04:05:59 +02:00
|
|
|
|
|
|
|
if (!tbl || tbl->it_offset > mask) {
|
|
|
|
printk(KERN_INFO "Warning: IOMMU table offset too big for device mask\n");
|
|
|
|
if (tbl)
|
|
|
|
printk(KERN_INFO "mask: 0x%08lx, table offset: 0x%08lx\n",
|
|
|
|
mask, tbl->it_offset);
|
|
|
|
else
|
|
|
|
printk(KERN_INFO "mask: 0x%08lx, table unavailable\n",
|
|
|
|
mask);
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
return 1;
|
2005-04-17 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void pci_iommu_init(void)
|
|
|
|
{
|
|
|
|
pci_dma_ops.alloc_coherent = pci_iommu_alloc_coherent;
|
|
|
|
pci_dma_ops.free_coherent = pci_iommu_free_coherent;
|
|
|
|
pci_dma_ops.map_single = pci_iommu_map_single;
|
|
|
|
pci_dma_ops.unmap_single = pci_iommu_unmap_single;
|
|
|
|
pci_dma_ops.map_sg = pci_iommu_map_sg;
|
|
|
|
pci_dma_ops.unmap_sg = pci_iommu_unmap_sg;
|
|
|
|
pci_dma_ops.dma_supported = pci_iommu_dma_supported;
|
|
|
|
}
|