Path: blob/master/arch/mips/cavium-octeon/dma-octeon.c
10817 views
/*1* This file is subject to the terms and conditions of the GNU General Public2* License. See the file "COPYING" in the main directory of this archive3* for more details.4*5* Copyright (C) 2000 Ani Joshi <[email protected]>6* Copyright (C) 2000, 2001 Ralf Baechle <[email protected]>7* Copyright (C) 2005 Ilya A. Volynets-Evenbakh <[email protected]>8* swiped from i386, and cloned for MIPS by Geert, polished by Ralf.9* IP32 changes by Ilya.10* Copyright (C) 2010 Cavium Networks, Inc.11*/12#include <linux/dma-mapping.h>13#include <linux/scatterlist.h>14#include <linux/bootmem.h>15#include <linux/swiotlb.h>16#include <linux/types.h>17#include <linux/init.h>18#include <linux/mm.h>1920#include <asm/bootinfo.h>2122#include <asm/octeon/octeon.h>2324#ifdef CONFIG_PCI25#include <asm/octeon/pci-octeon.h>26#include <asm/octeon/cvmx-npi-defs.h>27#include <asm/octeon/cvmx-pci-defs.h>2829static dma_addr_t octeon_hole_phys_to_dma(phys_addr_t paddr)30{31if (paddr >= CVMX_PCIE_BAR1_PHYS_BASE && paddr < (CVMX_PCIE_BAR1_PHYS_BASE + CVMX_PCIE_BAR1_PHYS_SIZE))32return paddr - CVMX_PCIE_BAR1_PHYS_BASE + CVMX_PCIE_BAR1_RC_BASE;33else34return paddr;35}3637static phys_addr_t octeon_hole_dma_to_phys(dma_addr_t daddr)38{39if (daddr >= CVMX_PCIE_BAR1_RC_BASE)40return daddr + CVMX_PCIE_BAR1_PHYS_BASE - CVMX_PCIE_BAR1_RC_BASE;41else42return daddr;43}4445static dma_addr_t octeon_gen1_phys_to_dma(struct device *dev, phys_addr_t paddr)46{47if (paddr >= 0x410000000ull && paddr < 0x420000000ull)48paddr -= 0x400000000ull;49return octeon_hole_phys_to_dma(paddr);50}5152static phys_addr_t octeon_gen1_dma_to_phys(struct device *dev, dma_addr_t daddr)53{54daddr = octeon_hole_dma_to_phys(daddr);5556if (daddr >= 0x10000000ull && daddr < 0x20000000ull)57daddr += 0x400000000ull;5859return daddr;60}6162static dma_addr_t octeon_big_phys_to_dma(struct device *dev, phys_addr_t paddr)63{64if (paddr >= 0x410000000ull && paddr < 0x420000000ull)65paddr -= 0x400000000ull;6667/* Anything in the BAR1 hole or above goes via BAR2 */68if (paddr >= 0xf0000000ull)69paddr = OCTEON_BAR2_PCI_ADDRESS + paddr;7071return paddr;72}7374static phys_addr_t octeon_big_dma_to_phys(struct device *dev, dma_addr_t daddr)75{76if (daddr >= OCTEON_BAR2_PCI_ADDRESS)77daddr -= OCTEON_BAR2_PCI_ADDRESS;7879if (daddr >= 0x10000000ull && daddr < 0x20000000ull)80daddr += 0x400000000ull;81return daddr;82}8384static dma_addr_t octeon_small_phys_to_dma(struct device *dev,85phys_addr_t paddr)86{87if (paddr >= 0x410000000ull && paddr < 0x420000000ull)88paddr -= 0x400000000ull;8990/* Anything not in the BAR1 range goes via BAR2 */91if (paddr >= octeon_bar1_pci_phys && paddr < octeon_bar1_pci_phys + 0x8000000ull)92paddr = paddr - octeon_bar1_pci_phys;93else94paddr = OCTEON_BAR2_PCI_ADDRESS + paddr;9596return paddr;97}9899static phys_addr_t octeon_small_dma_to_phys(struct device *dev,100dma_addr_t daddr)101{102if (daddr >= OCTEON_BAR2_PCI_ADDRESS)103daddr -= OCTEON_BAR2_PCI_ADDRESS;104else105daddr += octeon_bar1_pci_phys;106107if (daddr >= 0x10000000ull && daddr < 0x20000000ull)108daddr += 0x400000000ull;109return daddr;110}111112#endif /* CONFIG_PCI */113114static dma_addr_t octeon_dma_map_page(struct device *dev, struct page *page,115unsigned long offset, size_t size, enum dma_data_direction direction,116struct dma_attrs *attrs)117{118dma_addr_t daddr = swiotlb_map_page(dev, page, offset, size,119direction, attrs);120mb();121122return daddr;123}124125static int octeon_dma_map_sg(struct device *dev, struct scatterlist *sg,126int nents, enum dma_data_direction direction, struct dma_attrs *attrs)127{128int r = swiotlb_map_sg_attrs(dev, sg, nents, direction, attrs);129mb();130return r;131}132133static void octeon_dma_sync_single_for_device(struct device *dev,134dma_addr_t dma_handle, size_t size, enum dma_data_direction direction)135{136swiotlb_sync_single_for_device(dev, dma_handle, size, direction);137mb();138}139140static void octeon_dma_sync_sg_for_device(struct device *dev,141struct scatterlist *sg, int nelems, enum dma_data_direction direction)142{143swiotlb_sync_sg_for_device(dev, sg, nelems, direction);144mb();145}146147static void *octeon_dma_alloc_coherent(struct device *dev, size_t size,148dma_addr_t *dma_handle, gfp_t gfp)149{150void *ret;151152if (dma_alloc_from_coherent(dev, size, dma_handle, &ret))153return ret;154155/* ignore region specifiers */156gfp &= ~(__GFP_DMA | __GFP_DMA32 | __GFP_HIGHMEM);157158#ifdef CONFIG_ZONE_DMA159if (dev == NULL)160gfp |= __GFP_DMA;161else if (dev->coherent_dma_mask <= DMA_BIT_MASK(24))162gfp |= __GFP_DMA;163else164#endif165#ifdef CONFIG_ZONE_DMA32166if (dev->coherent_dma_mask <= DMA_BIT_MASK(32))167gfp |= __GFP_DMA32;168else169#endif170;171172/* Don't invoke OOM killer */173gfp |= __GFP_NORETRY;174175ret = swiotlb_alloc_coherent(dev, size, dma_handle, gfp);176177mb();178179return ret;180}181182static void octeon_dma_free_coherent(struct device *dev, size_t size,183void *vaddr, dma_addr_t dma_handle)184{185int order = get_order(size);186187if (dma_release_from_coherent(dev, order, vaddr))188return;189190swiotlb_free_coherent(dev, size, vaddr, dma_handle);191}192193static dma_addr_t octeon_unity_phys_to_dma(struct device *dev, phys_addr_t paddr)194{195return paddr;196}197198static phys_addr_t octeon_unity_dma_to_phys(struct device *dev, dma_addr_t daddr)199{200return daddr;201}202203struct octeon_dma_map_ops {204struct dma_map_ops dma_map_ops;205dma_addr_t (*phys_to_dma)(struct device *dev, phys_addr_t paddr);206phys_addr_t (*dma_to_phys)(struct device *dev, dma_addr_t daddr);207};208209dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)210{211struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),212struct octeon_dma_map_ops,213dma_map_ops);214215return ops->phys_to_dma(dev, paddr);216}217EXPORT_SYMBOL(phys_to_dma);218219phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)220{221struct octeon_dma_map_ops *ops = container_of(get_dma_ops(dev),222struct octeon_dma_map_ops,223dma_map_ops);224225return ops->dma_to_phys(dev, daddr);226}227EXPORT_SYMBOL(dma_to_phys);228229static struct octeon_dma_map_ops octeon_linear_dma_map_ops = {230.dma_map_ops = {231.alloc_coherent = octeon_dma_alloc_coherent,232.free_coherent = octeon_dma_free_coherent,233.map_page = octeon_dma_map_page,234.unmap_page = swiotlb_unmap_page,235.map_sg = octeon_dma_map_sg,236.unmap_sg = swiotlb_unmap_sg_attrs,237.sync_single_for_cpu = swiotlb_sync_single_for_cpu,238.sync_single_for_device = octeon_dma_sync_single_for_device,239.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,240.sync_sg_for_device = octeon_dma_sync_sg_for_device,241.mapping_error = swiotlb_dma_mapping_error,242.dma_supported = swiotlb_dma_supported243},244.phys_to_dma = octeon_unity_phys_to_dma,245.dma_to_phys = octeon_unity_dma_to_phys246};247248char *octeon_swiotlb;249250void __init plat_swiotlb_setup(void)251{252int i;253phys_t max_addr;254phys_t addr_size;255size_t swiotlbsize;256unsigned long swiotlb_nslabs;257258max_addr = 0;259addr_size = 0;260261for (i = 0 ; i < boot_mem_map.nr_map; i++) {262struct boot_mem_map_entry *e = &boot_mem_map.map[i];263if (e->type != BOOT_MEM_RAM)264continue;265266/* These addresses map low for PCI. */267if (e->addr > 0x410000000ull)268continue;269270addr_size += e->size;271272if (max_addr < e->addr + e->size)273max_addr = e->addr + e->size;274275}276277swiotlbsize = PAGE_SIZE;278279#ifdef CONFIG_PCI280/*281* For OCTEON_DMA_BAR_TYPE_SMALL, size the iotlb at 1/4 memory282* size to a maximum of 64MB283*/284if (OCTEON_IS_MODEL(OCTEON_CN31XX)285|| OCTEON_IS_MODEL(OCTEON_CN38XX_PASS2)) {286swiotlbsize = addr_size / 4;287if (swiotlbsize > 64 * (1<<20))288swiotlbsize = 64 * (1<<20);289} else if (max_addr > 0xf0000000ul) {290/*291* Otherwise only allocate a big iotlb if there is292* memory past the BAR1 hole.293*/294swiotlbsize = 64 * (1<<20);295}296#endif297swiotlb_nslabs = swiotlbsize >> IO_TLB_SHIFT;298swiotlb_nslabs = ALIGN(swiotlb_nslabs, IO_TLB_SEGSIZE);299swiotlbsize = swiotlb_nslabs << IO_TLB_SHIFT;300301octeon_swiotlb = alloc_bootmem_low_pages(swiotlbsize);302303swiotlb_init_with_tbl(octeon_swiotlb, swiotlb_nslabs, 1);304305mips_dma_map_ops = &octeon_linear_dma_map_ops.dma_map_ops;306}307308#ifdef CONFIG_PCI309static struct octeon_dma_map_ops _octeon_pci_dma_map_ops = {310.dma_map_ops = {311.alloc_coherent = octeon_dma_alloc_coherent,312.free_coherent = octeon_dma_free_coherent,313.map_page = octeon_dma_map_page,314.unmap_page = swiotlb_unmap_page,315.map_sg = octeon_dma_map_sg,316.unmap_sg = swiotlb_unmap_sg_attrs,317.sync_single_for_cpu = swiotlb_sync_single_for_cpu,318.sync_single_for_device = octeon_dma_sync_single_for_device,319.sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,320.sync_sg_for_device = octeon_dma_sync_sg_for_device,321.mapping_error = swiotlb_dma_mapping_error,322.dma_supported = swiotlb_dma_supported323},324};325326struct dma_map_ops *octeon_pci_dma_map_ops;327328void __init octeon_pci_dma_init(void)329{330switch (octeon_dma_bar_type) {331case OCTEON_DMA_BAR_TYPE_PCIE:332_octeon_pci_dma_map_ops.phys_to_dma = octeon_gen1_phys_to_dma;333_octeon_pci_dma_map_ops.dma_to_phys = octeon_gen1_dma_to_phys;334break;335case OCTEON_DMA_BAR_TYPE_BIG:336_octeon_pci_dma_map_ops.phys_to_dma = octeon_big_phys_to_dma;337_octeon_pci_dma_map_ops.dma_to_phys = octeon_big_dma_to_phys;338break;339case OCTEON_DMA_BAR_TYPE_SMALL:340_octeon_pci_dma_map_ops.phys_to_dma = octeon_small_phys_to_dma;341_octeon_pci_dma_map_ops.dma_to_phys = octeon_small_dma_to_phys;342break;343default:344BUG();345}346octeon_pci_dma_map_ops = &_octeon_pci_dma_map_ops.dma_map_ops;347}348#endif /* CONFIG_PCI */349350351