Path: blob/master/arch/powerpc/platforms/cell/iommu.c
10818 views
/*1* IOMMU implementation for Cell Broadband Processor Architecture2*3* (C) Copyright IBM Corporation 2006-20084*5* Author: Jeremy Kerr <[email protected]>6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License as published by9* the Free Software Foundation; either version 2, or (at your option)10* any later version.11*12* This program is distributed in the hope that it will be useful,13* but WITHOUT ANY WARRANTY; without even the implied warranty of14* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15* GNU General Public License for more details.16*17* You should have received a copy of the GNU General Public License18* along with this program; if not, write to the Free Software19* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.20*/2122#undef DEBUG2324#include <linux/kernel.h>25#include <linux/init.h>26#include <linux/interrupt.h>27#include <linux/notifier.h>28#include <linux/of.h>29#include <linux/of_platform.h>30#include <linux/slab.h>31#include <linux/memblock.h>3233#include <asm/prom.h>34#include <asm/iommu.h>35#include <asm/machdep.h>36#include <asm/pci-bridge.h>37#include <asm/udbg.h>38#include <asm/firmware.h>39#include <asm/cell-regs.h>4041#include "interrupt.h"4243/* Define CELL_IOMMU_REAL_UNMAP to actually unmap non-used pages44* instead of leaving them mapped to some dummy page. This can be45* enabled once the appropriate workarounds for spider bugs have46* been enabled47*/48#define CELL_IOMMU_REAL_UNMAP4950/* Define CELL_IOMMU_STRICT_PROTECTION to enforce protection of51* IO PTEs based on the transfer direction. That can be enabled52* once spider-net has been fixed to pass the correct direction53* to the DMA mapping functions54*/55#define CELL_IOMMU_STRICT_PROTECTION565758#define NR_IOMMUS 25960/* IOC mmap registers */61#define IOC_Reg_Size 0x20006263#define IOC_IOPT_CacheInvd 0x90864#define IOC_IOPT_CacheInvd_NE_Mask 0xffe0000000000000ul65#define IOC_IOPT_CacheInvd_IOPTE_Mask 0x000003fffffffff8ul66#define IOC_IOPT_CacheInvd_Busy 0x0000000000000001ul6768#define IOC_IOST_Origin 0x91869#define IOC_IOST_Origin_E 0x8000000000000000ul70#define IOC_IOST_Origin_HW 0x0000000000000800ul71#define IOC_IOST_Origin_HL 0x0000000000000400ul7273#define IOC_IO_ExcpStat 0x92074#define IOC_IO_ExcpStat_V 0x8000000000000000ul75#define IOC_IO_ExcpStat_SPF_Mask 0x6000000000000000ul76#define IOC_IO_ExcpStat_SPF_S 0x6000000000000000ul77#define IOC_IO_ExcpStat_SPF_P 0x2000000000000000ul78#define IOC_IO_ExcpStat_ADDR_Mask 0x00000007fffff000ul79#define IOC_IO_ExcpStat_RW_Mask 0x0000000000000800ul80#define IOC_IO_ExcpStat_IOID_Mask 0x00000000000007fful8182#define IOC_IO_ExcpMask 0x92883#define IOC_IO_ExcpMask_SFE 0x4000000000000000ul84#define IOC_IO_ExcpMask_PFE 0x2000000000000000ul8586#define IOC_IOCmd_Offset 0x10008788#define IOC_IOCmd_Cfg 0xc0089#define IOC_IOCmd_Cfg_TE 0x0000800000000000ul909192/* Segment table entries */93#define IOSTE_V 0x8000000000000000ul /* valid */94#define IOSTE_H 0x4000000000000000ul /* cache hint */95#define IOSTE_PT_Base_RPN_Mask 0x3ffffffffffff000ul /* base RPN of IOPT */96#define IOSTE_NPPT_Mask 0x0000000000000fe0ul /* no. pages in IOPT */97#define IOSTE_PS_Mask 0x0000000000000007ul /* page size */98#define IOSTE_PS_4K 0x0000000000000001ul /* - 4kB */99#define IOSTE_PS_64K 0x0000000000000003ul /* - 64kB */100#define IOSTE_PS_1M 0x0000000000000005ul /* - 1MB */101#define IOSTE_PS_16M 0x0000000000000007ul /* - 16MB */102103104/* IOMMU sizing */105#define IO_SEGMENT_SHIFT 28106#define IO_PAGENO_BITS(shift) (IO_SEGMENT_SHIFT - (shift))107108/* The high bit needs to be set on every DMA address */109#define SPIDER_DMA_OFFSET 0x80000000ul110111struct iommu_window {112struct list_head list;113struct cbe_iommu *iommu;114unsigned long offset;115unsigned long size;116unsigned int ioid;117struct iommu_table table;118};119120#define NAMESIZE 8121struct cbe_iommu {122int nid;123char name[NAMESIZE];124void __iomem *xlate_regs;125void __iomem *cmd_regs;126unsigned long *stab;127unsigned long *ptab;128void *pad_page;129struct list_head windows;130};131132/* Static array of iommus, one per node133* each contains a list of windows, keyed from dma_window property134* - on bus setup, look for a matching window, or create one135* - on dev setup, assign iommu_table ptr136*/137static struct cbe_iommu iommus[NR_IOMMUS];138static int cbe_nr_iommus;139140static void invalidate_tce_cache(struct cbe_iommu *iommu, unsigned long *pte,141long n_ptes)142{143u64 __iomem *reg;144u64 val;145long n;146147reg = iommu->xlate_regs + IOC_IOPT_CacheInvd;148149while (n_ptes > 0) {150/* we can invalidate up to 1 << 11 PTEs at once */151n = min(n_ptes, 1l << 11);152val = (((n /*- 1*/) << 53) & IOC_IOPT_CacheInvd_NE_Mask)153| (__pa(pte) & IOC_IOPT_CacheInvd_IOPTE_Mask)154| IOC_IOPT_CacheInvd_Busy;155156out_be64(reg, val);157while (in_be64(reg) & IOC_IOPT_CacheInvd_Busy)158;159160n_ptes -= n;161pte += n;162}163}164165static int tce_build_cell(struct iommu_table *tbl, long index, long npages,166unsigned long uaddr, enum dma_data_direction direction,167struct dma_attrs *attrs)168{169int i;170unsigned long *io_pte, base_pte;171struct iommu_window *window =172container_of(tbl, struct iommu_window, table);173174/* implementing proper protection causes problems with the spidernet175* driver - check mapping directions later, but allow read & write by176* default for now.*/177#ifdef CELL_IOMMU_STRICT_PROTECTION178/* to avoid referencing a global, we use a trick here to setup the179* protection bit. "prot" is setup to be 3 fields of 4 bits apprended180* together for each of the 3 supported direction values. It is then181* shifted left so that the fields matching the desired direction182* lands on the appropriate bits, and other bits are masked out.183*/184const unsigned long prot = 0xc48;185base_pte =186((prot << (52 + 4 * direction)) &187(CBE_IOPTE_PP_W | CBE_IOPTE_PP_R)) |188CBE_IOPTE_M | CBE_IOPTE_SO_RW |189(window->ioid & CBE_IOPTE_IOID_Mask);190#else191base_pte = CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_M |192CBE_IOPTE_SO_RW | (window->ioid & CBE_IOPTE_IOID_Mask);193#endif194if (unlikely(dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs)))195base_pte &= ~CBE_IOPTE_SO_RW;196197io_pte = (unsigned long *)tbl->it_base + (index - tbl->it_offset);198199for (i = 0; i < npages; i++, uaddr += IOMMU_PAGE_SIZE)200io_pte[i] = base_pte | (__pa(uaddr) & CBE_IOPTE_RPN_Mask);201202mb();203204invalidate_tce_cache(window->iommu, io_pte, npages);205206pr_debug("tce_build_cell(index=%lx,n=%lx,dir=%d,base_pte=%lx)\n",207index, npages, direction, base_pte);208return 0;209}210211static void tce_free_cell(struct iommu_table *tbl, long index, long npages)212{213214int i;215unsigned long *io_pte, pte;216struct iommu_window *window =217container_of(tbl, struct iommu_window, table);218219pr_debug("tce_free_cell(index=%lx,n=%lx)\n", index, npages);220221#ifdef CELL_IOMMU_REAL_UNMAP222pte = 0;223#else224/* spider bridge does PCI reads after freeing - insert a mapping225* to a scratch page instead of an invalid entry */226pte = CBE_IOPTE_PP_R | CBE_IOPTE_M | CBE_IOPTE_SO_RW |227__pa(window->iommu->pad_page) |228(window->ioid & CBE_IOPTE_IOID_Mask);229#endif230231io_pte = (unsigned long *)tbl->it_base + (index - tbl->it_offset);232233for (i = 0; i < npages; i++)234io_pte[i] = pte;235236mb();237238invalidate_tce_cache(window->iommu, io_pte, npages);239}240241static irqreturn_t ioc_interrupt(int irq, void *data)242{243unsigned long stat, spf;244struct cbe_iommu *iommu = data;245246stat = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);247spf = stat & IOC_IO_ExcpStat_SPF_Mask;248249/* Might want to rate limit it */250printk(KERN_ERR "iommu: DMA exception 0x%016lx\n", stat);251printk(KERN_ERR " V=%d, SPF=[%c%c], RW=%s, IOID=0x%04x\n",252!!(stat & IOC_IO_ExcpStat_V),253(spf == IOC_IO_ExcpStat_SPF_S) ? 'S' : ' ',254(spf == IOC_IO_ExcpStat_SPF_P) ? 'P' : ' ',255(stat & IOC_IO_ExcpStat_RW_Mask) ? "Read" : "Write",256(unsigned int)(stat & IOC_IO_ExcpStat_IOID_Mask));257printk(KERN_ERR " page=0x%016lx\n",258stat & IOC_IO_ExcpStat_ADDR_Mask);259260/* clear interrupt */261stat &= ~IOC_IO_ExcpStat_V;262out_be64(iommu->xlate_regs + IOC_IO_ExcpStat, stat);263264return IRQ_HANDLED;265}266267static int cell_iommu_find_ioc(int nid, unsigned long *base)268{269struct device_node *np;270struct resource r;271272*base = 0;273274/* First look for new style /be nodes */275for_each_node_by_name(np, "ioc") {276if (of_node_to_nid(np) != nid)277continue;278if (of_address_to_resource(np, 0, &r)) {279printk(KERN_ERR "iommu: can't get address for %s\n",280np->full_name);281continue;282}283*base = r.start;284of_node_put(np);285return 0;286}287288/* Ok, let's try the old way */289for_each_node_by_type(np, "cpu") {290const unsigned int *nidp;291const unsigned long *tmp;292293nidp = of_get_property(np, "node-id", NULL);294if (nidp && *nidp == nid) {295tmp = of_get_property(np, "ioc-translation", NULL);296if (tmp) {297*base = *tmp;298of_node_put(np);299return 0;300}301}302}303304return -ENODEV;305}306307static void cell_iommu_setup_stab(struct cbe_iommu *iommu,308unsigned long dbase, unsigned long dsize,309unsigned long fbase, unsigned long fsize)310{311struct page *page;312unsigned long segments, stab_size;313314segments = max(dbase + dsize, fbase + fsize) >> IO_SEGMENT_SHIFT;315316pr_debug("%s: iommu[%d]: segments: %lu\n",317__func__, iommu->nid, segments);318319/* set up the segment table */320stab_size = segments * sizeof(unsigned long);321page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(stab_size));322BUG_ON(!page);323iommu->stab = page_address(page);324memset(iommu->stab, 0, stab_size);325}326327static unsigned long *cell_iommu_alloc_ptab(struct cbe_iommu *iommu,328unsigned long base, unsigned long size, unsigned long gap_base,329unsigned long gap_size, unsigned long page_shift)330{331struct page *page;332int i;333unsigned long reg, segments, pages_per_segment, ptab_size,334n_pte_pages, start_seg, *ptab;335336start_seg = base >> IO_SEGMENT_SHIFT;337segments = size >> IO_SEGMENT_SHIFT;338pages_per_segment = 1ull << IO_PAGENO_BITS(page_shift);339/* PTEs for each segment must start on a 4K bounday */340pages_per_segment = max(pages_per_segment,341(1 << 12) / sizeof(unsigned long));342343ptab_size = segments * pages_per_segment * sizeof(unsigned long);344pr_debug("%s: iommu[%d]: ptab_size: %lu, order: %d\n", __func__,345iommu->nid, ptab_size, get_order(ptab_size));346page = alloc_pages_node(iommu->nid, GFP_KERNEL, get_order(ptab_size));347BUG_ON(!page);348349ptab = page_address(page);350memset(ptab, 0, ptab_size);351352/* number of 4K pages needed for a page table */353n_pte_pages = (pages_per_segment * sizeof(unsigned long)) >> 12;354355pr_debug("%s: iommu[%d]: stab at %p, ptab at %p, n_pte_pages: %lu\n",356__func__, iommu->nid, iommu->stab, ptab,357n_pte_pages);358359/* initialise the STEs */360reg = IOSTE_V | ((n_pte_pages - 1) << 5);361362switch (page_shift) {363case 12: reg |= IOSTE_PS_4K; break;364case 16: reg |= IOSTE_PS_64K; break;365case 20: reg |= IOSTE_PS_1M; break;366case 24: reg |= IOSTE_PS_16M; break;367default: BUG();368}369370gap_base = gap_base >> IO_SEGMENT_SHIFT;371gap_size = gap_size >> IO_SEGMENT_SHIFT;372373pr_debug("Setting up IOMMU stab:\n");374for (i = start_seg; i < (start_seg + segments); i++) {375if (i >= gap_base && i < (gap_base + gap_size)) {376pr_debug("\toverlap at %d, skipping\n", i);377continue;378}379iommu->stab[i] = reg | (__pa(ptab) + (n_pte_pages << 12) *380(i - start_seg));381pr_debug("\t[%d] 0x%016lx\n", i, iommu->stab[i]);382}383384return ptab;385}386387static void cell_iommu_enable_hardware(struct cbe_iommu *iommu)388{389int ret;390unsigned long reg, xlate_base;391unsigned int virq;392393if (cell_iommu_find_ioc(iommu->nid, &xlate_base))394panic("%s: missing IOC register mappings for node %d\n",395__func__, iommu->nid);396397iommu->xlate_regs = ioremap(xlate_base, IOC_Reg_Size);398iommu->cmd_regs = iommu->xlate_regs + IOC_IOCmd_Offset;399400/* ensure that the STEs have updated */401mb();402403/* setup interrupts for the iommu. */404reg = in_be64(iommu->xlate_regs + IOC_IO_ExcpStat);405out_be64(iommu->xlate_regs + IOC_IO_ExcpStat,406reg & ~IOC_IO_ExcpStat_V);407out_be64(iommu->xlate_regs + IOC_IO_ExcpMask,408IOC_IO_ExcpMask_PFE | IOC_IO_ExcpMask_SFE);409410virq = irq_create_mapping(NULL,411IIC_IRQ_IOEX_ATI | (iommu->nid << IIC_IRQ_NODE_SHIFT));412BUG_ON(virq == NO_IRQ);413414ret = request_irq(virq, ioc_interrupt, IRQF_DISABLED,415iommu->name, iommu);416BUG_ON(ret);417418/* set the IOC segment table origin register (and turn on the iommu) */419reg = IOC_IOST_Origin_E | __pa(iommu->stab) | IOC_IOST_Origin_HW;420out_be64(iommu->xlate_regs + IOC_IOST_Origin, reg);421in_be64(iommu->xlate_regs + IOC_IOST_Origin);422423/* turn on IO translation */424reg = in_be64(iommu->cmd_regs + IOC_IOCmd_Cfg) | IOC_IOCmd_Cfg_TE;425out_be64(iommu->cmd_regs + IOC_IOCmd_Cfg, reg);426}427428static void cell_iommu_setup_hardware(struct cbe_iommu *iommu,429unsigned long base, unsigned long size)430{431cell_iommu_setup_stab(iommu, base, size, 0, 0);432iommu->ptab = cell_iommu_alloc_ptab(iommu, base, size, 0, 0,433IOMMU_PAGE_SHIFT);434cell_iommu_enable_hardware(iommu);435}436437#if 0/* Unused for now */438static struct iommu_window *find_window(struct cbe_iommu *iommu,439unsigned long offset, unsigned long size)440{441struct iommu_window *window;442443/* todo: check for overlapping (but not equal) windows) */444445list_for_each_entry(window, &(iommu->windows), list) {446if (window->offset == offset && window->size == size)447return window;448}449450return NULL;451}452#endif453454static inline u32 cell_iommu_get_ioid(struct device_node *np)455{456const u32 *ioid;457458ioid = of_get_property(np, "ioid", NULL);459if (ioid == NULL) {460printk(KERN_WARNING "iommu: missing ioid for %s using 0\n",461np->full_name);462return 0;463}464465return *ioid;466}467468static struct iommu_window * __init469cell_iommu_setup_window(struct cbe_iommu *iommu, struct device_node *np,470unsigned long offset, unsigned long size,471unsigned long pte_offset)472{473struct iommu_window *window;474struct page *page;475u32 ioid;476477ioid = cell_iommu_get_ioid(np);478479window = kzalloc_node(sizeof(*window), GFP_KERNEL, iommu->nid);480BUG_ON(window == NULL);481482window->offset = offset;483window->size = size;484window->ioid = ioid;485window->iommu = iommu;486487window->table.it_blocksize = 16;488window->table.it_base = (unsigned long)iommu->ptab;489window->table.it_index = iommu->nid;490window->table.it_offset = (offset >> IOMMU_PAGE_SHIFT) + pte_offset;491window->table.it_size = size >> IOMMU_PAGE_SHIFT;492493iommu_init_table(&window->table, iommu->nid);494495pr_debug("\tioid %d\n", window->ioid);496pr_debug("\tblocksize %ld\n", window->table.it_blocksize);497pr_debug("\tbase 0x%016lx\n", window->table.it_base);498pr_debug("\toffset 0x%lx\n", window->table.it_offset);499pr_debug("\tsize %ld\n", window->table.it_size);500501list_add(&window->list, &iommu->windows);502503if (offset != 0)504return window;505506/* We need to map and reserve the first IOMMU page since it's used507* by the spider workaround. In theory, we only need to do that when508* running on spider but it doesn't really matter.509*510* This code also assumes that we have a window that starts at 0,511* which is the case on all spider based blades.512*/513page = alloc_pages_node(iommu->nid, GFP_KERNEL, 0);514BUG_ON(!page);515iommu->pad_page = page_address(page);516clear_page(iommu->pad_page);517518__set_bit(0, window->table.it_map);519tce_build_cell(&window->table, window->table.it_offset, 1,520(unsigned long)iommu->pad_page, DMA_TO_DEVICE, NULL);521window->table.it_hint = window->table.it_blocksize;522523return window;524}525526static struct cbe_iommu *cell_iommu_for_node(int nid)527{528int i;529530for (i = 0; i < cbe_nr_iommus; i++)531if (iommus[i].nid == nid)532return &iommus[i];533return NULL;534}535536static unsigned long cell_dma_direct_offset;537538static unsigned long dma_iommu_fixed_base;539540/* iommu_fixed_is_weak is set if booted with iommu_fixed=weak */541static int iommu_fixed_is_weak;542543static struct iommu_table *cell_get_iommu_table(struct device *dev)544{545struct iommu_window *window;546struct cbe_iommu *iommu;547548/* Current implementation uses the first window available in that549* node's iommu. We -might- do something smarter later though it may550* never be necessary551*/552iommu = cell_iommu_for_node(dev_to_node(dev));553if (iommu == NULL || list_empty(&iommu->windows)) {554printk(KERN_ERR "iommu: missing iommu for %s (node %d)\n",555dev->of_node ? dev->of_node->full_name : "?",556dev_to_node(dev));557return NULL;558}559window = list_entry(iommu->windows.next, struct iommu_window, list);560561return &window->table;562}563564/* A coherent allocation implies strong ordering */565566static void *dma_fixed_alloc_coherent(struct device *dev, size_t size,567dma_addr_t *dma_handle, gfp_t flag)568{569if (iommu_fixed_is_weak)570return iommu_alloc_coherent(dev, cell_get_iommu_table(dev),571size, dma_handle,572device_to_mask(dev), flag,573dev_to_node(dev));574else575return dma_direct_ops.alloc_coherent(dev, size, dma_handle,576flag);577}578579static void dma_fixed_free_coherent(struct device *dev, size_t size,580void *vaddr, dma_addr_t dma_handle)581{582if (iommu_fixed_is_weak)583iommu_free_coherent(cell_get_iommu_table(dev), size, vaddr,584dma_handle);585else586dma_direct_ops.free_coherent(dev, size, vaddr, dma_handle);587}588589static dma_addr_t dma_fixed_map_page(struct device *dev, struct page *page,590unsigned long offset, size_t size,591enum dma_data_direction direction,592struct dma_attrs *attrs)593{594if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))595return dma_direct_ops.map_page(dev, page, offset, size,596direction, attrs);597else598return iommu_map_page(dev, cell_get_iommu_table(dev), page,599offset, size, device_to_mask(dev),600direction, attrs);601}602603static void dma_fixed_unmap_page(struct device *dev, dma_addr_t dma_addr,604size_t size, enum dma_data_direction direction,605struct dma_attrs *attrs)606{607if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))608dma_direct_ops.unmap_page(dev, dma_addr, size, direction,609attrs);610else611iommu_unmap_page(cell_get_iommu_table(dev), dma_addr, size,612direction, attrs);613}614615static int dma_fixed_map_sg(struct device *dev, struct scatterlist *sg,616int nents, enum dma_data_direction direction,617struct dma_attrs *attrs)618{619if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))620return dma_direct_ops.map_sg(dev, sg, nents, direction, attrs);621else622return iommu_map_sg(dev, cell_get_iommu_table(dev), sg, nents,623device_to_mask(dev), direction, attrs);624}625626static void dma_fixed_unmap_sg(struct device *dev, struct scatterlist *sg,627int nents, enum dma_data_direction direction,628struct dma_attrs *attrs)629{630if (iommu_fixed_is_weak == dma_get_attr(DMA_ATTR_WEAK_ORDERING, attrs))631dma_direct_ops.unmap_sg(dev, sg, nents, direction, attrs);632else633iommu_unmap_sg(cell_get_iommu_table(dev), sg, nents, direction,634attrs);635}636637static int dma_fixed_dma_supported(struct device *dev, u64 mask)638{639return mask == DMA_BIT_MASK(64);640}641642static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask);643644struct dma_map_ops dma_iommu_fixed_ops = {645.alloc_coherent = dma_fixed_alloc_coherent,646.free_coherent = dma_fixed_free_coherent,647.map_sg = dma_fixed_map_sg,648.unmap_sg = dma_fixed_unmap_sg,649.dma_supported = dma_fixed_dma_supported,650.set_dma_mask = dma_set_mask_and_switch,651.map_page = dma_fixed_map_page,652.unmap_page = dma_fixed_unmap_page,653};654655static void cell_dma_dev_setup_fixed(struct device *dev);656657static void cell_dma_dev_setup(struct device *dev)658{659/* Order is important here, these are not mutually exclusive */660if (get_dma_ops(dev) == &dma_iommu_fixed_ops)661cell_dma_dev_setup_fixed(dev);662else if (get_pci_dma_ops() == &dma_iommu_ops)663set_iommu_table_base(dev, cell_get_iommu_table(dev));664else if (get_pci_dma_ops() == &dma_direct_ops)665set_dma_offset(dev, cell_dma_direct_offset);666else667BUG();668}669670static void cell_pci_dma_dev_setup(struct pci_dev *dev)671{672cell_dma_dev_setup(&dev->dev);673}674675static int cell_of_bus_notify(struct notifier_block *nb, unsigned long action,676void *data)677{678struct device *dev = data;679680/* We are only intereted in device addition */681if (action != BUS_NOTIFY_ADD_DEVICE)682return 0;683684/* We use the PCI DMA ops */685dev->archdata.dma_ops = get_pci_dma_ops();686687cell_dma_dev_setup(dev);688689return 0;690}691692static struct notifier_block cell_of_bus_notifier = {693.notifier_call = cell_of_bus_notify694};695696static int __init cell_iommu_get_window(struct device_node *np,697unsigned long *base,698unsigned long *size)699{700const void *dma_window;701unsigned long index;702703/* Use ibm,dma-window if available, else, hard code ! */704dma_window = of_get_property(np, "ibm,dma-window", NULL);705if (dma_window == NULL) {706*base = 0;707*size = 0x80000000u;708return -ENODEV;709}710711of_parse_dma_window(np, dma_window, &index, base, size);712return 0;713}714715static struct cbe_iommu * __init cell_iommu_alloc(struct device_node *np)716{717struct cbe_iommu *iommu;718int nid, i;719720/* Get node ID */721nid = of_node_to_nid(np);722if (nid < 0) {723printk(KERN_ERR "iommu: failed to get node for %s\n",724np->full_name);725return NULL;726}727pr_debug("iommu: setting up iommu for node %d (%s)\n",728nid, np->full_name);729730/* XXX todo: If we can have multiple windows on the same IOMMU, which731* isn't the case today, we probably want here to check wether the732* iommu for that node is already setup.733* However, there might be issue with getting the size right so let's734* ignore that for now. We might want to completely get rid of the735* multiple window support since the cell iommu supports per-page ioids736*/737738if (cbe_nr_iommus >= NR_IOMMUS) {739printk(KERN_ERR "iommu: too many IOMMUs detected ! (%s)\n",740np->full_name);741return NULL;742}743744/* Init base fields */745i = cbe_nr_iommus++;746iommu = &iommus[i];747iommu->stab = NULL;748iommu->nid = nid;749snprintf(iommu->name, sizeof(iommu->name), "iommu%d", i);750INIT_LIST_HEAD(&iommu->windows);751752return iommu;753}754755static void __init cell_iommu_init_one(struct device_node *np,756unsigned long offset)757{758struct cbe_iommu *iommu;759unsigned long base, size;760761iommu = cell_iommu_alloc(np);762if (!iommu)763return;764765/* Obtain a window for it */766cell_iommu_get_window(np, &base, &size);767768pr_debug("\ttranslating window 0x%lx...0x%lx\n",769base, base + size - 1);770771/* Initialize the hardware */772cell_iommu_setup_hardware(iommu, base, size);773774/* Setup the iommu_table */775cell_iommu_setup_window(iommu, np, base, size,776offset >> IOMMU_PAGE_SHIFT);777}778779static void __init cell_disable_iommus(void)780{781int node;782unsigned long base, val;783void __iomem *xregs, *cregs;784785/* Make sure IOC translation is disabled on all nodes */786for_each_online_node(node) {787if (cell_iommu_find_ioc(node, &base))788continue;789xregs = ioremap(base, IOC_Reg_Size);790if (xregs == NULL)791continue;792cregs = xregs + IOC_IOCmd_Offset;793794pr_debug("iommu: cleaning up iommu on node %d\n", node);795796out_be64(xregs + IOC_IOST_Origin, 0);797(void)in_be64(xregs + IOC_IOST_Origin);798val = in_be64(cregs + IOC_IOCmd_Cfg);799val &= ~IOC_IOCmd_Cfg_TE;800out_be64(cregs + IOC_IOCmd_Cfg, val);801(void)in_be64(cregs + IOC_IOCmd_Cfg);802803iounmap(xregs);804}805}806807static int __init cell_iommu_init_disabled(void)808{809struct device_node *np = NULL;810unsigned long base = 0, size;811812/* When no iommu is present, we use direct DMA ops */813set_pci_dma_ops(&dma_direct_ops);814815/* First make sure all IOC translation is turned off */816cell_disable_iommus();817818/* If we have no Axon, we set up the spider DMA magic offset */819if (of_find_node_by_name(NULL, "axon") == NULL)820cell_dma_direct_offset = SPIDER_DMA_OFFSET;821822/* Now we need to check to see where the memory is mapped823* in PCI space. We assume that all busses use the same dma824* window which is always the case so far on Cell, thus we825* pick up the first pci-internal node we can find and check826* the DMA window from there.827*/828for_each_node_by_name(np, "axon") {829if (np->parent == NULL || np->parent->parent != NULL)830continue;831if (cell_iommu_get_window(np, &base, &size) == 0)832break;833}834if (np == NULL) {835for_each_node_by_name(np, "pci-internal") {836if (np->parent == NULL || np->parent->parent != NULL)837continue;838if (cell_iommu_get_window(np, &base, &size) == 0)839break;840}841}842of_node_put(np);843844/* If we found a DMA window, we check if it's big enough to enclose845* all of physical memory. If not, we force enable IOMMU846*/847if (np && size < memblock_end_of_DRAM()) {848printk(KERN_WARNING "iommu: force-enabled, dma window"849" (%ldMB) smaller than total memory (%lldMB)\n",850size >> 20, memblock_end_of_DRAM() >> 20);851return -ENODEV;852}853854cell_dma_direct_offset += base;855856if (cell_dma_direct_offset != 0)857ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;858859printk("iommu: disabled, direct DMA offset is 0x%lx\n",860cell_dma_direct_offset);861862return 0;863}864865/*866* Fixed IOMMU mapping support867*868* This code adds support for setting up a fixed IOMMU mapping on certain869* cell machines. For 64-bit devices this avoids the performance overhead of870* mapping and unmapping pages at runtime. 32-bit devices are unable to use871* the fixed mapping.872*873* The fixed mapping is established at boot, and maps all of physical memory874* 1:1 into device space at some offset. On machines with < 30 GB of memory875* we setup the fixed mapping immediately above the normal IOMMU window.876*877* For example a machine with 4GB of memory would end up with the normal878* IOMMU window from 0-2GB and the fixed mapping window from 2GB to 6GB. In879* this case a 64-bit device wishing to DMA to 1GB would be told to DMA to880* 3GB, plus any offset required by firmware. The firmware offset is encoded881* in the "dma-ranges" property.882*883* On machines with 30GB or more of memory, we are unable to place the fixed884* mapping above the normal IOMMU window as we would run out of address space.885* Instead we move the normal IOMMU window to coincide with the hash page886* table, this region does not need to be part of the fixed mapping as no887* device should ever be DMA'ing to it. We then setup the fixed mapping888* from 0 to 32GB.889*/890891static u64 cell_iommu_get_fixed_address(struct device *dev)892{893u64 cpu_addr, size, best_size, dev_addr = OF_BAD_ADDR;894struct device_node *np;895const u32 *ranges = NULL;896int i, len, best, naddr, nsize, pna, range_size;897898np = of_node_get(dev->of_node);899while (1) {900naddr = of_n_addr_cells(np);901nsize = of_n_size_cells(np);902np = of_get_next_parent(np);903if (!np)904break;905906ranges = of_get_property(np, "dma-ranges", &len);907908/* Ignore empty ranges, they imply no translation required */909if (ranges && len > 0)910break;911}912913if (!ranges) {914dev_dbg(dev, "iommu: no dma-ranges found\n");915goto out;916}917918len /= sizeof(u32);919920pna = of_n_addr_cells(np);921range_size = naddr + nsize + pna;922923/* dma-ranges format:924* child addr : naddr cells925* parent addr : pna cells926* size : nsize cells927*/928for (i = 0, best = -1, best_size = 0; i < len; i += range_size) {929cpu_addr = of_translate_dma_address(np, ranges + i + naddr);930size = of_read_number(ranges + i + naddr + pna, nsize);931932if (cpu_addr == 0 && size > best_size) {933best = i;934best_size = size;935}936}937938if (best >= 0) {939dev_addr = of_read_number(ranges + best, naddr);940} else941dev_dbg(dev, "iommu: no suitable range found!\n");942943out:944of_node_put(np);945946return dev_addr;947}948949static int dma_set_mask_and_switch(struct device *dev, u64 dma_mask)950{951if (!dev->dma_mask || !dma_supported(dev, dma_mask))952return -EIO;953954if (dma_mask == DMA_BIT_MASK(64) &&955cell_iommu_get_fixed_address(dev) != OF_BAD_ADDR)956{957dev_dbg(dev, "iommu: 64-bit OK, using fixed ops\n");958set_dma_ops(dev, &dma_iommu_fixed_ops);959} else {960dev_dbg(dev, "iommu: not 64-bit, using default ops\n");961set_dma_ops(dev, get_pci_dma_ops());962}963964cell_dma_dev_setup(dev);965966*dev->dma_mask = dma_mask;967968return 0;969}970971static void cell_dma_dev_setup_fixed(struct device *dev)972{973u64 addr;974975addr = cell_iommu_get_fixed_address(dev) + dma_iommu_fixed_base;976set_dma_offset(dev, addr);977978dev_dbg(dev, "iommu: fixed addr = %llx\n", addr);979}980981static void insert_16M_pte(unsigned long addr, unsigned long *ptab,982unsigned long base_pte)983{984unsigned long segment, offset;985986segment = addr >> IO_SEGMENT_SHIFT;987offset = (addr >> 24) - (segment << IO_PAGENO_BITS(24));988ptab = ptab + (segment * (1 << 12) / sizeof(unsigned long));989990pr_debug("iommu: addr %lx ptab %p segment %lx offset %lx\n",991addr, ptab, segment, offset);992993ptab[offset] = base_pte | (__pa(addr) & CBE_IOPTE_RPN_Mask);994}995996static void cell_iommu_setup_fixed_ptab(struct cbe_iommu *iommu,997struct device_node *np, unsigned long dbase, unsigned long dsize,998unsigned long fbase, unsigned long fsize)999{1000unsigned long base_pte, uaddr, ioaddr, *ptab;10011002ptab = cell_iommu_alloc_ptab(iommu, fbase, fsize, dbase, dsize, 24);10031004dma_iommu_fixed_base = fbase;10051006pr_debug("iommu: mapping 0x%lx pages from 0x%lx\n", fsize, fbase);10071008base_pte = CBE_IOPTE_PP_W | CBE_IOPTE_PP_R | CBE_IOPTE_M |1009(cell_iommu_get_ioid(np) & CBE_IOPTE_IOID_Mask);10101011if (iommu_fixed_is_weak)1012pr_info("IOMMU: Using weak ordering for fixed mapping\n");1013else {1014pr_info("IOMMU: Using strong ordering for fixed mapping\n");1015base_pte |= CBE_IOPTE_SO_RW;1016}10171018for (uaddr = 0; uaddr < fsize; uaddr += (1 << 24)) {1019/* Don't touch the dynamic region */1020ioaddr = uaddr + fbase;1021if (ioaddr >= dbase && ioaddr < (dbase + dsize)) {1022pr_debug("iommu: fixed/dynamic overlap, skipping\n");1023continue;1024}10251026insert_16M_pte(uaddr, ptab, base_pte);1027}10281029mb();1030}10311032static int __init cell_iommu_fixed_mapping_init(void)1033{1034unsigned long dbase, dsize, fbase, fsize, hbase, hend;1035struct cbe_iommu *iommu;1036struct device_node *np;10371038/* The fixed mapping is only supported on axon machines */1039np = of_find_node_by_name(NULL, "axon");1040if (!np) {1041pr_debug("iommu: fixed mapping disabled, no axons found\n");1042return -1;1043}10441045/* We must have dma-ranges properties for fixed mapping to work */1046np = of_find_node_with_property(NULL, "dma-ranges");1047of_node_put(np);10481049if (!np) {1050pr_debug("iommu: no dma-ranges found, no fixed mapping\n");1051return -1;1052}10531054/* The default setup is to have the fixed mapping sit after the1055* dynamic region, so find the top of the largest IOMMU window1056* on any axon, then add the size of RAM and that's our max value.1057* If that is > 32GB we have to do other shennanigans.1058*/1059fbase = 0;1060for_each_node_by_name(np, "axon") {1061cell_iommu_get_window(np, &dbase, &dsize);1062fbase = max(fbase, dbase + dsize);1063}10641065fbase = _ALIGN_UP(fbase, 1 << IO_SEGMENT_SHIFT);1066fsize = memblock_phys_mem_size();10671068if ((fbase + fsize) <= 0x800000000ul)1069hbase = 0; /* use the device tree window */1070else {1071/* If we're over 32 GB we need to cheat. We can't map all of1072* RAM with the fixed mapping, and also fit the dynamic1073* region. So try to place the dynamic region where the hash1074* table sits, drivers never need to DMA to it, we don't1075* need a fixed mapping for that area.1076*/1077if (!htab_address) {1078pr_debug("iommu: htab is NULL, on LPAR? Huh?\n");1079return -1;1080}1081hbase = __pa(htab_address);1082hend = hbase + htab_size_bytes;10831084/* The window must start and end on a segment boundary */1085if ((hbase != _ALIGN_UP(hbase, 1 << IO_SEGMENT_SHIFT)) ||1086(hend != _ALIGN_UP(hend, 1 << IO_SEGMENT_SHIFT))) {1087pr_debug("iommu: hash window not segment aligned\n");1088return -1;1089}10901091/* Check the hash window fits inside the real DMA window */1092for_each_node_by_name(np, "axon") {1093cell_iommu_get_window(np, &dbase, &dsize);10941095if (hbase < dbase || (hend > (dbase + dsize))) {1096pr_debug("iommu: hash window doesn't fit in"1097"real DMA window\n");1098return -1;1099}1100}11011102fbase = 0;1103}11041105/* Setup the dynamic regions */1106for_each_node_by_name(np, "axon") {1107iommu = cell_iommu_alloc(np);1108BUG_ON(!iommu);11091110if (hbase == 0)1111cell_iommu_get_window(np, &dbase, &dsize);1112else {1113dbase = hbase;1114dsize = htab_size_bytes;1115}11161117printk(KERN_DEBUG "iommu: node %d, dynamic window 0x%lx-0x%lx "1118"fixed window 0x%lx-0x%lx\n", iommu->nid, dbase,1119dbase + dsize, fbase, fbase + fsize);11201121cell_iommu_setup_stab(iommu, dbase, dsize, fbase, fsize);1122iommu->ptab = cell_iommu_alloc_ptab(iommu, dbase, dsize, 0, 0,1123IOMMU_PAGE_SHIFT);1124cell_iommu_setup_fixed_ptab(iommu, np, dbase, dsize,1125fbase, fsize);1126cell_iommu_enable_hardware(iommu);1127cell_iommu_setup_window(iommu, np, dbase, dsize, 0);1128}11291130dma_iommu_ops.set_dma_mask = dma_set_mask_and_switch;1131set_pci_dma_ops(&dma_iommu_ops);11321133return 0;1134}11351136static int iommu_fixed_disabled;11371138static int __init setup_iommu_fixed(char *str)1139{1140struct device_node *pciep;11411142if (strcmp(str, "off") == 0)1143iommu_fixed_disabled = 1;11441145/* If we can find a pcie-endpoint in the device tree assume that1146* we're on a triblade or a CAB so by default the fixed mapping1147* should be set to be weakly ordered; but only if the boot1148* option WASN'T set for strong ordering1149*/1150pciep = of_find_node_by_type(NULL, "pcie-endpoint");11511152if (strcmp(str, "weak") == 0 || (pciep && strcmp(str, "strong") != 0))1153iommu_fixed_is_weak = 1;11541155of_node_put(pciep);11561157return 1;1158}1159__setup("iommu_fixed=", setup_iommu_fixed);11601161static int __init cell_iommu_init(void)1162{1163struct device_node *np;11641165/* If IOMMU is disabled or we have little enough RAM to not need1166* to enable it, we setup a direct mapping.1167*1168* Note: should we make sure we have the IOMMU actually disabled ?1169*/1170if (iommu_is_off ||1171(!iommu_force_on && memblock_end_of_DRAM() <= 0x80000000ull))1172if (cell_iommu_init_disabled() == 0)1173goto bail;11741175/* Setup various ppc_md. callbacks */1176ppc_md.pci_dma_dev_setup = cell_pci_dma_dev_setup;1177ppc_md.tce_build = tce_build_cell;1178ppc_md.tce_free = tce_free_cell;11791180if (!iommu_fixed_disabled && cell_iommu_fixed_mapping_init() == 0)1181goto bail;11821183/* Create an iommu for each /axon node. */1184for_each_node_by_name(np, "axon") {1185if (np->parent == NULL || np->parent->parent != NULL)1186continue;1187cell_iommu_init_one(np, 0);1188}11891190/* Create an iommu for each toplevel /pci-internal node for1191* old hardware/firmware1192*/1193for_each_node_by_name(np, "pci-internal") {1194if (np->parent == NULL || np->parent->parent != NULL)1195continue;1196cell_iommu_init_one(np, SPIDER_DMA_OFFSET);1197}11981199/* Setup default PCI iommu ops */1200set_pci_dma_ops(&dma_iommu_ops);12011202bail:1203/* Register callbacks on OF platform device addition/removal1204* to handle linking them to the right DMA operations1205*/1206bus_register_notifier(&platform_bus_type, &cell_of_bus_notifier);12071208return 0;1209}1210machine_arch_initcall(cell, cell_iommu_init);1211machine_arch_initcall(celleb_native, cell_iommu_init);1212121312141215