Path: blob/master/arch/powerpc/platforms/iseries/iommu.c
10820 views
/*1* Copyright (C) 2001 Mike Corrigan & Dave Engebretsen, IBM Corporation2*3* Rewrite, cleanup:4*5* Copyright (C) 2004 Olof Johansson <[email protected]>, IBM Corporation6* Copyright (C) 2006 Olof Johansson <[email protected]>7*8* Dynamic DMA mapping support, iSeries-specific parts.9*10*11* This program is free software; you can redistribute it and/or modify12* it under the terms of the GNU General Public License as published by13* the Free Software Foundation; either version 2 of the License, or14* (at your option) any later version.15*16* This program is distributed in the hope that it will be useful,17* but WITHOUT ANY WARRANTY; without even the implied warranty of18* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the19* GNU General Public License for more details.20*21* You should have received a copy of the GNU General Public License22* along with this program; if not, write to the Free Software23* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA24*/2526#include <linux/types.h>27#include <linux/dma-mapping.h>28#include <linux/list.h>29#include <linux/pci.h>30#include <linux/module.h>31#include <linux/slab.h>3233#include <asm/iommu.h>34#include <asm/vio.h>35#include <asm/tce.h>36#include <asm/machdep.h>37#include <asm/abs_addr.h>38#include <asm/prom.h>39#include <asm/pci-bridge.h>40#include <asm/iseries/hv_call_xm.h>41#include <asm/iseries/hv_call_event.h>42#include <asm/iseries/iommu.h>4344static int tce_build_iSeries(struct iommu_table *tbl, long index, long npages,45unsigned long uaddr, enum dma_data_direction direction,46struct dma_attrs *attrs)47{48u64 rc;49u64 tce, rpn;5051while (npages--) {52rpn = virt_to_abs(uaddr) >> TCE_SHIFT;53tce = (rpn & TCE_RPN_MASK) << TCE_RPN_SHIFT;5455if (tbl->it_type == TCE_VB) {56/* Virtual Bus */57tce |= TCE_VALID|TCE_ALLIO;58if (direction != DMA_TO_DEVICE)59tce |= TCE_VB_WRITE;60} else {61/* PCI Bus */62tce |= TCE_PCI_READ; /* Read allowed */63if (direction != DMA_TO_DEVICE)64tce |= TCE_PCI_WRITE;65}6667rc = HvCallXm_setTce((u64)tbl->it_index, (u64)index, tce);68if (rc)69panic("PCI_DMA: HvCallXm_setTce failed, Rc: 0x%llx\n",70rc);71index++;72uaddr += TCE_PAGE_SIZE;73}74return 0;75}7677static void tce_free_iSeries(struct iommu_table *tbl, long index, long npages)78{79u64 rc;8081while (npages--) {82rc = HvCallXm_setTce((u64)tbl->it_index, (u64)index, 0);83if (rc)84panic("PCI_DMA: HvCallXm_setTce failed, Rc: 0x%llx\n",85rc);86index++;87}88}8990/*91* Structure passed to HvCallXm_getTceTableParms92*/93struct iommu_table_cb {94unsigned long itc_busno; /* Bus number for this tce table */95unsigned long itc_start; /* Will be NULL for secondary */96unsigned long itc_totalsize; /* Size (in pages) of whole table */97unsigned long itc_offset; /* Index into real tce table of the98start of our section */99unsigned long itc_size; /* Size (in pages) of our section */100unsigned long itc_index; /* Index of this tce table */101unsigned short itc_maxtables; /* Max num of tables for partition */102unsigned char itc_virtbus; /* Flag to indicate virtual bus */103unsigned char itc_slotno; /* IOA Tce Slot Index */104unsigned char itc_rsvd[4];105};106107/*108* Call Hv with the architected data structure to get TCE table info.109* info. Put the returned data into the Linux representation of the110* TCE table data.111* The Hardware Tce table comes in three flavors.112* 1. TCE table shared between Buses.113* 2. TCE table per Bus.114* 3. TCE Table per IOA.115*/116void iommu_table_getparms_iSeries(unsigned long busno,117unsigned char slotno,118unsigned char virtbus,119struct iommu_table* tbl)120{121struct iommu_table_cb *parms;122123parms = kzalloc(sizeof(*parms), GFP_KERNEL);124if (parms == NULL)125panic("PCI_DMA: TCE Table Allocation failed.");126127parms->itc_busno = busno;128parms->itc_slotno = slotno;129parms->itc_virtbus = virtbus;130131HvCallXm_getTceTableParms(iseries_hv_addr(parms));132133if (parms->itc_size == 0)134panic("PCI_DMA: parms->size is zero, parms is 0x%p", parms);135136/* itc_size is in pages worth of table, it_size is in # of entries */137tbl->it_size = (parms->itc_size * TCE_PAGE_SIZE) / TCE_ENTRY_SIZE;138tbl->it_busno = parms->itc_busno;139tbl->it_offset = parms->itc_offset;140tbl->it_index = parms->itc_index;141tbl->it_blocksize = 1;142tbl->it_type = virtbus ? TCE_VB : TCE_PCI;143144kfree(parms);145}146147148#ifdef CONFIG_PCI149/*150* This function compares the known tables to find an iommu_table151* that has already been built for hardware TCEs.152*/153static struct iommu_table *iommu_table_find(struct iommu_table * tbl)154{155struct device_node *node;156157for (node = NULL; (node = of_find_all_nodes(node)); ) {158struct pci_dn *pdn = PCI_DN(node);159struct iommu_table *it;160161if (pdn == NULL)162continue;163it = pdn->iommu_table;164if ((it != NULL) &&165(it->it_type == TCE_PCI) &&166(it->it_offset == tbl->it_offset) &&167(it->it_index == tbl->it_index) &&168(it->it_size == tbl->it_size)) {169of_node_put(node);170return it;171}172}173return NULL;174}175176177static void pci_dma_dev_setup_iseries(struct pci_dev *pdev)178{179struct iommu_table *tbl;180struct device_node *dn = pci_device_to_OF_node(pdev);181struct pci_dn *pdn = PCI_DN(dn);182const u32 *lsn = of_get_property(dn, "linux,logical-slot-number", NULL);183184BUG_ON(lsn == NULL);185186tbl = kzalloc(sizeof(struct iommu_table), GFP_KERNEL);187188iommu_table_getparms_iSeries(pdn->busno, *lsn, 0, tbl);189190/* Look for existing tce table */191pdn->iommu_table = iommu_table_find(tbl);192if (pdn->iommu_table == NULL)193pdn->iommu_table = iommu_init_table(tbl, -1);194else195kfree(tbl);196set_iommu_table_base(&pdev->dev, pdn->iommu_table);197}198#else199#define pci_dma_dev_setup_iseries NULL200#endif201202static struct iommu_table veth_iommu_table;203static struct iommu_table vio_iommu_table;204205void *iseries_hv_alloc(size_t size, dma_addr_t *dma_handle, gfp_t flag)206{207return iommu_alloc_coherent(NULL, &vio_iommu_table, size, dma_handle,208DMA_BIT_MASK(32), flag, -1);209}210EXPORT_SYMBOL_GPL(iseries_hv_alloc);211212void iseries_hv_free(size_t size, void *vaddr, dma_addr_t dma_handle)213{214iommu_free_coherent(&vio_iommu_table, size, vaddr, dma_handle);215}216EXPORT_SYMBOL_GPL(iseries_hv_free);217218dma_addr_t iseries_hv_map(void *vaddr, size_t size,219enum dma_data_direction direction)220{221return iommu_map_page(NULL, &vio_iommu_table, virt_to_page(vaddr),222(unsigned long)vaddr % PAGE_SIZE, size,223DMA_BIT_MASK(32), direction, NULL);224}225226void iseries_hv_unmap(dma_addr_t dma_handle, size_t size,227enum dma_data_direction direction)228{229iommu_unmap_page(&vio_iommu_table, dma_handle, size, direction, NULL);230}231232void __init iommu_vio_init(void)233{234iommu_table_getparms_iSeries(255, 0, 0xff, &veth_iommu_table);235veth_iommu_table.it_size /= 2;236vio_iommu_table = veth_iommu_table;237vio_iommu_table.it_offset += veth_iommu_table.it_size;238239if (!iommu_init_table(&veth_iommu_table, -1))240printk("Virtual Bus VETH TCE table failed.\n");241if (!iommu_init_table(&vio_iommu_table, -1))242printk("Virtual Bus VIO TCE table failed.\n");243}244245struct iommu_table *vio_build_iommu_table_iseries(struct vio_dev *dev)246{247if (strcmp(dev->type, "network") == 0)248return &veth_iommu_table;249return &vio_iommu_table;250}251252void iommu_init_early_iSeries(void)253{254ppc_md.tce_build = tce_build_iSeries;255ppc_md.tce_free = tce_free_iSeries;256257ppc_md.pci_dma_dev_setup = pci_dma_dev_setup_iseries;258set_pci_dma_ops(&dma_iommu_ops);259}260261262