2016-02-18 21:12:58 +07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011,2016 Samsung Electronics Co., Ltd.
|
2012-05-12 03:56:09 +07:00
|
|
|
* http://www.samsung.com
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXYNOS_IOMMU_DEBUG
|
|
|
|
#define DEBUG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
2015-05-19 20:20:36 +07:00
|
|
|
#include <linux/dma-mapping.h>
|
2012-05-12 03:56:09 +07:00
|
|
|
#include <linux/err.h>
|
2015-05-19 20:20:30 +07:00
|
|
|
#include <linux/io.h>
|
2012-05-12 03:56:09 +07:00
|
|
|
#include <linux/iommu.h>
|
2015-05-19 20:20:30 +07:00
|
|
|
#include <linux/interrupt.h>
|
2012-05-12 03:56:09 +07:00
|
|
|
#include <linux/list.h>
|
2015-05-19 20:20:36 +07:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_iommu.h>
|
|
|
|
#include <linux/of_platform.h>
|
2015-05-19 20:20:30 +07:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/pm_runtime.h>
|
|
|
|
#include <linux/slab.h>
|
2016-02-18 21:12:49 +07:00
|
|
|
#include <linux/dma-iommu.h>
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:58 +07:00
|
|
|
typedef u32 sysmmu_iova_t;
|
|
|
|
typedef u32 sysmmu_pte_t;
|
|
|
|
|
2014-08-04 11:36:28 +07:00
|
|
|
/* We do not consider super section mapping (16MB) */
|
2012-05-12 03:56:09 +07:00
|
|
|
#define SECT_ORDER 20
|
|
|
|
#define LPAGE_ORDER 16
|
|
|
|
#define SPAGE_ORDER 12
|
|
|
|
|
|
|
|
#define SECT_SIZE (1 << SECT_ORDER)
|
|
|
|
#define LPAGE_SIZE (1 << LPAGE_ORDER)
|
|
|
|
#define SPAGE_SIZE (1 << SPAGE_ORDER)
|
|
|
|
|
|
|
|
#define SECT_MASK (~(SECT_SIZE - 1))
|
|
|
|
#define LPAGE_MASK (~(LPAGE_SIZE - 1))
|
|
|
|
#define SPAGE_MASK (~(SPAGE_SIZE - 1))
|
|
|
|
|
2014-05-12 13:15:04 +07:00
|
|
|
#define lv1ent_fault(sent) ((*(sent) == ZERO_LV2LINK) || \
|
|
|
|
((*(sent) & 3) == 0) || ((*(sent) & 3) == 3))
|
|
|
|
#define lv1ent_zero(sent) (*(sent) == ZERO_LV2LINK)
|
|
|
|
#define lv1ent_page_zero(sent) ((*(sent) & 3) == 1)
|
|
|
|
#define lv1ent_page(sent) ((*(sent) != ZERO_LV2LINK) && \
|
|
|
|
((*(sent) & 3) == 1))
|
2012-05-12 03:56:09 +07:00
|
|
|
#define lv1ent_section(sent) ((*(sent) & 3) == 2)
|
|
|
|
|
|
|
|
#define lv2ent_fault(pent) ((*(pent) & 3) == 0)
|
|
|
|
#define lv2ent_small(pent) ((*(pent) & 2) == 2)
|
|
|
|
#define lv2ent_large(pent) ((*(pent) & 3) == 1)
|
|
|
|
|
2016-02-18 21:12:58 +07:00
|
|
|
/*
|
|
|
|
* v1.x - v3.x SYSMMU supports 32bit physical and 32bit virtual address spaces
|
|
|
|
* v5.0 introduced support for 36bit physical address space by shifting
|
|
|
|
* all page entry values by 4 bits.
|
|
|
|
* All SYSMMU controllers in the system support the address spaces of the same
|
|
|
|
* size, so PG_ENT_SHIFT can be initialized on first SYSMMU probe to proper
|
|
|
|
* value (0 or 4).
|
|
|
|
*/
|
|
|
|
static short PG_ENT_SHIFT = -1;
|
|
|
|
#define SYSMMU_PG_ENT_SHIFT 0
|
|
|
|
#define SYSMMU_V5_PG_ENT_SHIFT 4
|
|
|
|
|
|
|
|
#define sect_to_phys(ent) (((phys_addr_t) ent) << PG_ENT_SHIFT)
|
|
|
|
#define section_phys(sent) (sect_to_phys(*(sent)) & SECT_MASK)
|
|
|
|
#define section_offs(iova) (iova & (SECT_SIZE - 1))
|
|
|
|
#define lpage_phys(pent) (sect_to_phys(*(pent)) & LPAGE_MASK)
|
|
|
|
#define lpage_offs(iova) (iova & (LPAGE_SIZE - 1))
|
|
|
|
#define spage_phys(pent) (sect_to_phys(*(pent)) & SPAGE_MASK)
|
|
|
|
#define spage_offs(iova) (iova & (SPAGE_SIZE - 1))
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
#define NUM_LV1ENTRIES 4096
|
2014-05-12 13:14:58 +07:00
|
|
|
#define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:58 +07:00
|
|
|
static u32 lv1ent_offset(sysmmu_iova_t iova)
|
|
|
|
{
|
|
|
|
return iova >> SECT_ORDER;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 lv2ent_offset(sysmmu_iova_t iova)
|
|
|
|
{
|
|
|
|
return (iova >> SPAGE_ORDER) & (NUM_LV2ENTRIES - 1);
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
#define LV1TABLE_SIZE (NUM_LV1ENTRIES * sizeof(sysmmu_pte_t))
|
2014-05-12 13:14:58 +07:00
|
|
|
#define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
#define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
|
2016-02-18 21:12:58 +07:00
|
|
|
#define lv2table_base(sent) (sect_to_phys(*(sent) & 0xFFFFFFC0))
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:58 +07:00
|
|
|
#define mk_lv1ent_sect(pa) ((pa >> PG_ENT_SHIFT) | 2)
|
|
|
|
#define mk_lv1ent_page(pa) ((pa >> PG_ENT_SHIFT) | 1)
|
|
|
|
#define mk_lv2ent_lpage(pa) ((pa >> PG_ENT_SHIFT) | 1)
|
|
|
|
#define mk_lv2ent_spage(pa) ((pa >> PG_ENT_SHIFT) | 2)
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
#define CTRL_ENABLE 0x5
|
|
|
|
#define CTRL_BLOCK 0x7
|
|
|
|
#define CTRL_DISABLE 0x0
|
|
|
|
|
2014-05-12 13:15:03 +07:00
|
|
|
#define CFG_LRU 0x1
|
|
|
|
#define CFG_QOS(n) ((n & 0xF) << 7)
|
|
|
|
#define CFG_ACGEN (1 << 24) /* System MMU 3.3 only */
|
|
|
|
#define CFG_SYSSEL (1 << 22) /* System MMU 3.2 only */
|
|
|
|
#define CFG_FLPDCACHE (1 << 20) /* System MMU 3.2+ only */
|
|
|
|
|
2016-02-18 21:12:58 +07:00
|
|
|
/* common registers */
|
2012-05-12 03:56:09 +07:00
|
|
|
#define REG_MMU_CTRL 0x000
|
|
|
|
#define REG_MMU_CFG 0x004
|
|
|
|
#define REG_MMU_STATUS 0x008
|
2016-02-18 21:12:58 +07:00
|
|
|
#define REG_MMU_VERSION 0x034
|
|
|
|
|
|
|
|
#define MMU_MAJ_VER(val) ((val) >> 7)
|
|
|
|
#define MMU_MIN_VER(val) ((val) & 0x7F)
|
|
|
|
#define MMU_RAW_VER(reg) (((reg) >> 21) & ((1 << 11) - 1)) /* 11 bits */
|
|
|
|
|
|
|
|
#define MAKE_MMU_VER(maj, min) ((((maj) & 0xF) << 7) | ((min) & 0x7F))
|
|
|
|
|
|
|
|
/* v1.x - v3.x registers */
|
2012-05-12 03:56:09 +07:00
|
|
|
#define REG_MMU_FLUSH 0x00C
|
|
|
|
#define REG_MMU_FLUSH_ENTRY 0x010
|
|
|
|
#define REG_PT_BASE_ADDR 0x014
|
|
|
|
#define REG_INT_STATUS 0x018
|
|
|
|
#define REG_INT_CLEAR 0x01C
|
|
|
|
|
|
|
|
#define REG_PAGE_FAULT_ADDR 0x024
|
|
|
|
#define REG_AW_FAULT_ADDR 0x028
|
|
|
|
#define REG_AR_FAULT_ADDR 0x02C
|
|
|
|
#define REG_DEFAULT_SLAVE_ADDR 0x030
|
|
|
|
|
2016-02-18 21:12:58 +07:00
|
|
|
/* v5.x registers */
|
|
|
|
#define REG_V5_PT_BASE_PFN 0x00C
|
|
|
|
#define REG_V5_MMU_FLUSH_ALL 0x010
|
|
|
|
#define REG_V5_MMU_FLUSH_ENTRY 0x014
|
|
|
|
#define REG_V5_INT_STATUS 0x060
|
|
|
|
#define REG_V5_INT_CLEAR 0x064
|
|
|
|
#define REG_V5_FAULT_AR_VA 0x070
|
|
|
|
#define REG_V5_FAULT_AW_VA 0x080
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
#define has_sysmmu(dev) (dev->archdata.iommu != NULL)
|
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
static struct device *dma_dev;
|
2014-05-12 13:14:48 +07:00
|
|
|
static struct kmem_cache *lv2table_kmem_cache;
|
2014-05-12 13:15:04 +07:00
|
|
|
static sysmmu_pte_t *zero_lv2_table;
|
|
|
|
#define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
|
2014-05-12 13:14:48 +07:00
|
|
|
|
2014-05-12 13:14:58 +07:00
|
|
|
static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
|
|
|
return pgtable + lv1ent_offset(iova);
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:58 +07:00
|
|
|
static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2014-05-12 13:14:58 +07:00
|
|
|
return (sysmmu_pte_t *)phys_to_virt(
|
2014-05-12 13:14:46 +07:00
|
|
|
lv2table_base(sent)) + lv2ent_offset(iova);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:53 +07:00
|
|
|
/*
|
|
|
|
* IOMMU fault information register
|
|
|
|
*/
|
|
|
|
struct sysmmu_fault_info {
|
|
|
|
unsigned int bit; /* bit number in STATUS register */
|
|
|
|
unsigned short addr_reg; /* register to read VA fault address */
|
|
|
|
const char *name; /* human readable fault name */
|
|
|
|
unsigned int type; /* fault type for report_iommu_fault */
|
2012-05-12 03:56:09 +07:00
|
|
|
};
|
|
|
|
|
2016-02-18 21:12:53 +07:00
|
|
|
static const struct sysmmu_fault_info sysmmu_faults[] = {
|
|
|
|
{ 0, REG_PAGE_FAULT_ADDR, "PAGE", IOMMU_FAULT_READ },
|
|
|
|
{ 1, REG_AR_FAULT_ADDR, "AR MULTI-HIT", IOMMU_FAULT_READ },
|
|
|
|
{ 2, REG_AW_FAULT_ADDR, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
|
|
|
|
{ 3, REG_DEFAULT_SLAVE_ADDR, "BUS ERROR", IOMMU_FAULT_READ },
|
|
|
|
{ 4, REG_AR_FAULT_ADDR, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
|
|
|
|
{ 5, REG_AR_FAULT_ADDR, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
|
|
|
|
{ 6, REG_AW_FAULT_ADDR, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
|
|
|
|
{ 7, REG_AW_FAULT_ADDR, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
|
2012-05-12 03:56:09 +07:00
|
|
|
};
|
|
|
|
|
2016-02-18 21:12:58 +07:00
|
|
|
static const struct sysmmu_fault_info sysmmu_v5_faults[] = {
|
|
|
|
{ 0, REG_V5_FAULT_AR_VA, "AR PTW", IOMMU_FAULT_READ },
|
|
|
|
{ 1, REG_V5_FAULT_AR_VA, "AR PAGE", IOMMU_FAULT_READ },
|
|
|
|
{ 2, REG_V5_FAULT_AR_VA, "AR MULTI-HIT", IOMMU_FAULT_READ },
|
|
|
|
{ 3, REG_V5_FAULT_AR_VA, "AR ACCESS PROTECTION", IOMMU_FAULT_READ },
|
|
|
|
{ 4, REG_V5_FAULT_AR_VA, "AR SECURITY PROTECTION", IOMMU_FAULT_READ },
|
|
|
|
{ 16, REG_V5_FAULT_AW_VA, "AW PTW", IOMMU_FAULT_WRITE },
|
|
|
|
{ 17, REG_V5_FAULT_AW_VA, "AW PAGE", IOMMU_FAULT_WRITE },
|
|
|
|
{ 18, REG_V5_FAULT_AW_VA, "AW MULTI-HIT", IOMMU_FAULT_WRITE },
|
|
|
|
{ 19, REG_V5_FAULT_AW_VA, "AW ACCESS PROTECTION", IOMMU_FAULT_WRITE },
|
|
|
|
{ 20, REG_V5_FAULT_AW_VA, "AW SECURITY PROTECTION", IOMMU_FAULT_WRITE },
|
|
|
|
};
|
|
|
|
|
2015-05-19 20:20:31 +07:00
|
|
|
/*
|
|
|
|
* This structure is attached to dev.archdata.iommu of the master device
|
|
|
|
* on device add, contains a list of SYSMMU controllers defined by device tree,
|
|
|
|
* which are bound to given master device. It is usually referenced by 'owner'
|
|
|
|
* pointer.
|
|
|
|
*/
|
2014-05-12 13:15:02 +07:00
|
|
|
struct exynos_iommu_owner {
|
2015-05-19 20:20:33 +07:00
|
|
|
struct list_head controllers; /* list of sysmmu_drvdata.owner_node */
|
2016-02-18 21:13:00 +07:00
|
|
|
struct iommu_domain *domain; /* domain this device is attached */
|
2014-05-12 13:15:02 +07:00
|
|
|
};
|
|
|
|
|
2015-05-19 20:20:31 +07:00
|
|
|
/*
|
|
|
|
* This structure exynos specific generalization of struct iommu_domain.
|
|
|
|
* It contains list of SYSMMU controllers from all master devices, which has
|
|
|
|
* been attached to this domain and page tables of IO address space defined by
|
|
|
|
* it. It is usually referenced by 'domain' pointer.
|
|
|
|
*/
|
2012-05-12 03:56:09 +07:00
|
|
|
struct exynos_iommu_domain {
|
2015-05-19 20:20:31 +07:00
|
|
|
struct list_head clients; /* list of sysmmu_drvdata.domain_node */
|
|
|
|
sysmmu_pte_t *pgtable; /* lv1 page table, 16KB */
|
|
|
|
short *lv2entcnt; /* free lv2 entry counter for each section */
|
|
|
|
spinlock_t lock; /* lock for modyfying list of clients */
|
|
|
|
spinlock_t pgtablelock; /* lock for modifying page table @ pgtable */
|
2015-03-26 19:43:11 +07:00
|
|
|
struct iommu_domain domain; /* generic domain data structure */
|
2012-05-12 03:56:09 +07:00
|
|
|
};
|
|
|
|
|
2015-05-19 20:20:31 +07:00
|
|
|
/*
|
|
|
|
* This structure hold all data of a single SYSMMU controller, this includes
|
|
|
|
* hw resources like registers and clocks, pointers and list nodes to connect
|
|
|
|
* it to all other structures, internal state and parameters read from device
|
|
|
|
* tree. It is usually referenced by 'data' pointer.
|
|
|
|
*/
|
2012-05-12 03:56:09 +07:00
|
|
|
struct sysmmu_drvdata {
|
2015-05-19 20:20:31 +07:00
|
|
|
struct device *sysmmu; /* SYSMMU controller device */
|
|
|
|
struct device *master; /* master device (owner) */
|
|
|
|
void __iomem *sfrbase; /* our registers */
|
|
|
|
struct clk *clk; /* SYSMMU's clock */
|
2016-02-18 21:12:58 +07:00
|
|
|
struct clk *aclk; /* SYSMMU's aclk clock */
|
|
|
|
struct clk *pclk; /* SYSMMU's pclk clock */
|
2015-05-19 20:20:31 +07:00
|
|
|
struct clk *clk_master; /* master's device clock */
|
|
|
|
int activations; /* number of calls to sysmmu_enable */
|
|
|
|
spinlock_t lock; /* lock for modyfying state */
|
|
|
|
struct exynos_iommu_domain *domain; /* domain we belong to */
|
|
|
|
struct list_head domain_node; /* node for domain clients list */
|
2015-05-19 20:20:33 +07:00
|
|
|
struct list_head owner_node; /* node for owner controllers list */
|
2015-05-19 20:20:31 +07:00
|
|
|
phys_addr_t pgtable; /* assigned page table structure */
|
|
|
|
unsigned int version; /* our version */
|
2012-05-12 03:56:09 +07:00
|
|
|
};
|
|
|
|
|
2015-03-26 19:43:11 +07:00
|
|
|
static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
|
|
|
|
{
|
|
|
|
return container_of(dom, struct exynos_iommu_domain, domain);
|
|
|
|
}
|
|
|
|
|
2012-05-12 03:56:09 +07:00
|
|
|
static bool set_sysmmu_active(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
/* return true if the System MMU was not active previously
|
|
|
|
and it needs to be initialized */
|
|
|
|
return ++data->activations == 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool set_sysmmu_inactive(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
/* return true if the System MMU is needed to be disabled */
|
|
|
|
BUG_ON(data->activations < 1);
|
|
|
|
return --data->activations == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_sysmmu_active(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
return data->activations > 0;
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:52 +07:00
|
|
|
static void sysmmu_unblock(struct sysmmu_drvdata *data)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:52 +07:00
|
|
|
static bool sysmmu_block(struct sysmmu_drvdata *data)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
|
|
|
int i = 120;
|
|
|
|
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
|
|
|
|
while ((i > 0) && !(readl(data->sfrbase + REG_MMU_STATUS) & 1))
|
2012-05-12 03:56:09 +07:00
|
|
|
--i;
|
|
|
|
|
2016-02-29 19:42:57 +07:00
|
|
|
if (!(readl(data->sfrbase + REG_MMU_STATUS) & 1)) {
|
2016-02-18 21:12:52 +07:00
|
|
|
sysmmu_unblock(data);
|
2012-05-12 03:56:09 +07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:52 +07:00
|
|
|
static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2016-02-18 21:12:58 +07:00
|
|
|
if (MMU_MAJ_VER(data->version) < 5)
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(0x1, data->sfrbase + REG_MMU_FLUSH);
|
2016-02-18 21:12:58 +07:00
|
|
|
else
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(0x1, data->sfrbase + REG_V5_MMU_FLUSH_ALL);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:52 +07:00
|
|
|
static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
|
2014-05-12 13:14:58 +07:00
|
|
|
sysmmu_iova_t iova, unsigned int num_inv)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2014-05-12 13:14:49 +07:00
|
|
|
unsigned int i;
|
2014-05-22 11:20:56 +07:00
|
|
|
|
2014-05-12 13:14:49 +07:00
|
|
|
for (i = 0; i < num_inv; i++) {
|
2016-02-18 21:12:58 +07:00
|
|
|
if (MMU_MAJ_VER(data->version) < 5)
|
2016-02-29 19:42:57 +07:00
|
|
|
writel((iova & SPAGE_MASK) | 1,
|
2016-02-18 21:12:58 +07:00
|
|
|
data->sfrbase + REG_MMU_FLUSH_ENTRY);
|
|
|
|
else
|
2016-02-29 19:42:57 +07:00
|
|
|
writel((iova & SPAGE_MASK) | 1,
|
2016-02-18 21:12:58 +07:00
|
|
|
data->sfrbase + REG_V5_MMU_FLUSH_ENTRY);
|
2014-05-12 13:14:49 +07:00
|
|
|
iova += SPAGE_SIZE;
|
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:52 +07:00
|
|
|
static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2016-02-18 21:12:58 +07:00
|
|
|
if (MMU_MAJ_VER(data->version) < 5)
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(pgd, data->sfrbase + REG_PT_BASE_ADDR);
|
2016-02-18 21:12:58 +07:00
|
|
|
else
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(pgd >> PAGE_SHIFT,
|
2016-02-18 21:12:58 +07:00
|
|
|
data->sfrbase + REG_V5_PT_BASE_PFN);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:52 +07:00
|
|
|
__sysmmu_tlb_invalidate(data);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:56 +07:00
|
|
|
static void __sysmmu_get_version(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
u32 ver;
|
|
|
|
|
|
|
|
clk_enable(data->clk_master);
|
|
|
|
clk_enable(data->clk);
|
2016-02-18 21:12:58 +07:00
|
|
|
clk_enable(data->pclk);
|
|
|
|
clk_enable(data->aclk);
|
2016-02-18 21:12:56 +07:00
|
|
|
|
2016-02-29 19:42:57 +07:00
|
|
|
ver = readl(data->sfrbase + REG_MMU_VERSION);
|
2016-02-18 21:12:56 +07:00
|
|
|
|
|
|
|
/* controllers on some SoCs don't report proper version */
|
|
|
|
if (ver == 0x80000001u)
|
|
|
|
data->version = MAKE_MMU_VER(1, 0);
|
|
|
|
else
|
|
|
|
data->version = MMU_RAW_VER(ver);
|
|
|
|
|
|
|
|
dev_dbg(data->sysmmu, "hardware version: %d.%d\n",
|
|
|
|
MMU_MAJ_VER(data->version), MMU_MIN_VER(data->version));
|
|
|
|
|
2016-02-18 21:12:58 +07:00
|
|
|
clk_disable(data->aclk);
|
|
|
|
clk_disable(data->pclk);
|
2016-02-18 21:12:56 +07:00
|
|
|
clk_disable(data->clk);
|
|
|
|
clk_disable(data->clk_master);
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:53 +07:00
|
|
|
static void show_fault_information(struct sysmmu_drvdata *data,
|
|
|
|
const struct sysmmu_fault_info *finfo,
|
|
|
|
sysmmu_iova_t fault_addr)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2014-05-12 13:14:58 +07:00
|
|
|
sysmmu_pte_t *ent;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:53 +07:00
|
|
|
dev_err(data->sysmmu, "%s FAULT occurred at %#x (page table base: %pa)\n",
|
|
|
|
finfo->name, fault_addr, &data->pgtable);
|
|
|
|
ent = section_entry(phys_to_virt(data->pgtable), fault_addr);
|
|
|
|
dev_err(data->sysmmu, "\tLv1 entry: %#x\n", *ent);
|
2012-05-12 03:56:09 +07:00
|
|
|
if (lv1ent_page(ent)) {
|
|
|
|
ent = page_entry(ent, fault_addr);
|
2016-02-18 21:12:53 +07:00
|
|
|
dev_err(data->sysmmu, "\t Lv2 entry: %#x\n", *ent);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
|
|
|
|
{
|
2014-08-04 11:36:28 +07:00
|
|
|
/* SYSMMU is in blocked state when interrupt occurred. */
|
2012-05-12 03:56:09 +07:00
|
|
|
struct sysmmu_drvdata *data = dev_id;
|
2016-02-18 21:12:58 +07:00
|
|
|
const struct sysmmu_fault_info *finfo;
|
|
|
|
unsigned int i, n, itype;
|
2016-02-18 21:12:53 +07:00
|
|
|
sysmmu_iova_t fault_addr = -1;
|
2016-02-18 21:12:58 +07:00
|
|
|
unsigned short reg_status, reg_clear;
|
2014-05-12 13:14:46 +07:00
|
|
|
int ret = -ENOSYS;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
WARN_ON(!is_sysmmu_active(data));
|
|
|
|
|
2016-02-18 21:12:58 +07:00
|
|
|
if (MMU_MAJ_VER(data->version) < 5) {
|
|
|
|
reg_status = REG_INT_STATUS;
|
|
|
|
reg_clear = REG_INT_CLEAR;
|
|
|
|
finfo = sysmmu_faults;
|
|
|
|
n = ARRAY_SIZE(sysmmu_faults);
|
|
|
|
} else {
|
|
|
|
reg_status = REG_V5_INT_STATUS;
|
|
|
|
reg_clear = REG_V5_INT_CLEAR;
|
|
|
|
finfo = sysmmu_v5_faults;
|
|
|
|
n = ARRAY_SIZE(sysmmu_v5_faults);
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:57 +07:00
|
|
|
spin_lock(&data->lock);
|
|
|
|
|
2016-02-18 21:12:51 +07:00
|
|
|
clk_enable(data->clk_master);
|
2014-05-12 13:14:57 +07:00
|
|
|
|
2016-02-29 19:42:57 +07:00
|
|
|
itype = __ffs(readl(data->sfrbase + reg_status));
|
2016-02-18 21:12:53 +07:00
|
|
|
for (i = 0; i < n; i++, finfo++)
|
|
|
|
if (finfo->bit == itype)
|
|
|
|
break;
|
|
|
|
/* unknown/unsupported fault */
|
|
|
|
BUG_ON(i == n);
|
|
|
|
|
|
|
|
/* print debug message */
|
2016-02-29 19:42:57 +07:00
|
|
|
fault_addr = readl(data->sfrbase + finfo->addr_reg);
|
2016-02-18 21:12:53 +07:00
|
|
|
show_fault_information(data, finfo, fault_addr);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:53 +07:00
|
|
|
if (data->domain)
|
|
|
|
ret = report_iommu_fault(&data->domain->domain,
|
|
|
|
data->master, fault_addr, finfo->type);
|
2014-05-12 13:14:56 +07:00
|
|
|
/* fault is not recovered by fault handler */
|
|
|
|
BUG_ON(ret != 0);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(1 << itype, data->sfrbase + reg_clear);
|
2014-05-12 13:14:56 +07:00
|
|
|
|
2016-02-18 21:12:52 +07:00
|
|
|
sysmmu_unblock(data);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:51 +07:00
|
|
|
clk_disable(data->clk_master);
|
2014-05-12 13:14:55 +07:00
|
|
|
|
2014-05-12 13:14:57 +07:00
|
|
|
spin_unlock(&data->lock);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
static void __sysmmu_disable_nocount(struct sysmmu_drvdata *data)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2016-02-18 21:12:51 +07:00
|
|
|
clk_enable(data->clk_master);
|
2014-05-12 13:14:55 +07:00
|
|
|
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
|
|
|
|
writel(0, data->sfrbase + REG_MMU_CFG);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:58 +07:00
|
|
|
clk_disable(data->aclk);
|
|
|
|
clk_disable(data->pclk);
|
2014-05-12 13:14:54 +07:00
|
|
|
clk_disable(data->clk);
|
2016-02-18 21:12:51 +07:00
|
|
|
clk_disable(data->clk_master);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
static bool __sysmmu_disable(struct sysmmu_drvdata *data)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2014-05-12 13:15:02 +07:00
|
|
|
bool disabled;
|
2012-05-12 03:56:09 +07:00
|
|
|
unsigned long flags;
|
|
|
|
|
2014-05-12 13:14:57 +07:00
|
|
|
spin_lock_irqsave(&data->lock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
disabled = set_sysmmu_inactive(data);
|
|
|
|
|
|
|
|
if (disabled) {
|
|
|
|
data->pgtable = 0;
|
|
|
|
data->domain = NULL;
|
|
|
|
|
|
|
|
__sysmmu_disable_nocount(data);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
dev_dbg(data->sysmmu, "Disabled\n");
|
|
|
|
} else {
|
|
|
|
dev_dbg(data->sysmmu, "%d times left to disable\n",
|
|
|
|
data->activations);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
spin_unlock_irqrestore(&data->lock, flags);
|
|
|
|
|
|
|
|
return disabled;
|
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
static void __sysmmu_init_config(struct sysmmu_drvdata *data)
|
|
|
|
{
|
2016-02-18 21:12:54 +07:00
|
|
|
unsigned int cfg;
|
|
|
|
|
|
|
|
if (data->version <= MAKE_MMU_VER(3, 1))
|
|
|
|
cfg = CFG_LRU | CFG_QOS(15);
|
|
|
|
else if (data->version <= MAKE_MMU_VER(3, 2))
|
|
|
|
cfg = CFG_LRU | CFG_QOS(15) | CFG_FLPDCACHE | CFG_SYSSEL;
|
|
|
|
else
|
|
|
|
cfg = CFG_QOS(15) | CFG_FLPDCACHE | CFG_ACGEN;
|
2014-05-12 13:15:02 +07:00
|
|
|
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(cfg, data->sfrbase + REG_MMU_CFG);
|
2014-05-12 13:15:02 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void __sysmmu_enable_nocount(struct sysmmu_drvdata *data)
|
|
|
|
{
|
2016-02-18 21:12:51 +07:00
|
|
|
clk_enable(data->clk_master);
|
2014-05-12 13:14:55 +07:00
|
|
|
clk_enable(data->clk);
|
2016-02-18 21:12:58 +07:00
|
|
|
clk_enable(data->pclk);
|
|
|
|
clk_enable(data->aclk);
|
2014-05-12 13:14:55 +07:00
|
|
|
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
|
2014-05-12 13:15:02 +07:00
|
|
|
|
|
|
|
__sysmmu_init_config(data);
|
|
|
|
|
2016-02-18 21:12:52 +07:00
|
|
|
__sysmmu_set_ptbase(data, data->pgtable);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-29 19:42:57 +07:00
|
|
|
writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
|
2014-05-12 13:14:46 +07:00
|
|
|
|
2016-02-18 21:12:51 +07:00
|
|
|
clk_disable(data->clk_master);
|
2014-05-12 13:15:02 +07:00
|
|
|
}
|
2014-05-12 13:14:55 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
static int __sysmmu_enable(struct sysmmu_drvdata *data, phys_addr_t pgtable,
|
2015-05-19 20:20:29 +07:00
|
|
|
struct exynos_iommu_domain *domain)
|
2014-05-12 13:15:02 +07:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&data->lock, flags);
|
|
|
|
if (set_sysmmu_active(data)) {
|
|
|
|
data->pgtable = pgtable;
|
2015-05-19 20:20:29 +07:00
|
|
|
data->domain = domain;
|
2014-05-12 13:15:02 +07:00
|
|
|
|
|
|
|
__sysmmu_enable_nocount(data);
|
|
|
|
|
|
|
|
dev_dbg(data->sysmmu, "Enabled\n");
|
|
|
|
} else {
|
|
|
|
ret = (pgtable == data->pgtable) ? 1 : -EBUSY;
|
|
|
|
|
|
|
|
dev_dbg(data->sysmmu, "already enabled\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WARN_ON(ret < 0))
|
|
|
|
set_sysmmu_inactive(data); /* decrement count */
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:57 +07:00
|
|
|
spin_unlock_irqrestore(&data->lock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:27 +07:00
|
|
|
static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
|
2014-05-12 13:15:04 +07:00
|
|
|
sysmmu_iova_t iova)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
|
|
|
|
spin_lock_irqsave(&data->lock, flags);
|
2016-05-23 16:30:08 +07:00
|
|
|
if (is_sysmmu_active(data) && data->version >= MAKE_MMU_VER(3, 3)) {
|
|
|
|
clk_enable(data->clk_master);
|
|
|
|
__sysmmu_tlb_invalidate_entry(data, iova, 1);
|
|
|
|
clk_disable(data->clk_master);
|
2016-02-18 21:12:55 +07:00
|
|
|
}
|
2014-05-12 13:15:04 +07:00
|
|
|
spin_unlock_irqrestore(&data->lock, flags);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:27 +07:00
|
|
|
static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
|
|
|
|
sysmmu_iova_t iova, size_t size)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
spin_lock_irqsave(&data->lock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
if (is_sysmmu_active(data)) {
|
2014-05-12 13:14:49 +07:00
|
|
|
unsigned int num_inv = 1;
|
2014-05-12 13:14:55 +07:00
|
|
|
|
2016-02-18 21:12:51 +07:00
|
|
|
clk_enable(data->clk_master);
|
2014-05-12 13:14:55 +07:00
|
|
|
|
2014-05-12 13:14:49 +07:00
|
|
|
/*
|
|
|
|
* L2TLB invalidation required
|
|
|
|
* 4KB page: 1 invalidation
|
2014-08-04 11:36:28 +07:00
|
|
|
* 64KB page: 16 invalidations
|
|
|
|
* 1MB page: 64 invalidations
|
2014-05-12 13:14:49 +07:00
|
|
|
* because it is set-associative TLB
|
|
|
|
* with 8-way and 64 sets.
|
|
|
|
* 1MB page can be cached in one of all sets.
|
|
|
|
* 64KB page can be one of 16 consecutive sets.
|
|
|
|
*/
|
2015-05-19 20:20:24 +07:00
|
|
|
if (MMU_MAJ_VER(data->version) == 2)
|
2014-05-12 13:14:49 +07:00
|
|
|
num_inv = min_t(unsigned int, size / PAGE_SIZE, 64);
|
|
|
|
|
2016-02-18 21:12:52 +07:00
|
|
|
if (sysmmu_block(data)) {
|
|
|
|
__sysmmu_tlb_invalidate_entry(data, iova, num_inv);
|
|
|
|
sysmmu_unblock(data);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
2016-02-18 21:12:51 +07:00
|
|
|
clk_disable(data->clk_master);
|
2012-05-12 03:56:09 +07:00
|
|
|
} else {
|
2015-05-19 20:20:27 +07:00
|
|
|
dev_dbg(data->master,
|
|
|
|
"disabled. Skipping TLB invalidation @ %#x\n", iova);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
2014-05-12 13:14:57 +07:00
|
|
|
spin_unlock_irqrestore(&data->lock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
static int __init exynos_sysmmu_probe(struct platform_device *pdev)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2014-05-12 13:14:54 +07:00
|
|
|
int irq, ret;
|
2014-05-12 13:14:46 +07:00
|
|
|
struct device *dev = &pdev->dev;
|
2012-05-12 03:56:09 +07:00
|
|
|
struct sysmmu_drvdata *data;
|
2014-05-12 13:14:46 +07:00
|
|
|
struct resource *res;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:54 +07:00
|
|
|
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:46 +07:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2014-05-12 13:14:54 +07:00
|
|
|
data->sfrbase = devm_ioremap_resource(dev, res);
|
|
|
|
if (IS_ERR(data->sfrbase))
|
|
|
|
return PTR_ERR(data->sfrbase);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:54 +07:00
|
|
|
irq = platform_get_irq(pdev, 0);
|
|
|
|
if (irq <= 0) {
|
2014-05-12 13:15:00 +07:00
|
|
|
dev_err(dev, "Unable to find IRQ resource\n");
|
2014-05-12 13:14:54 +07:00
|
|
|
return irq;
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:54 +07:00
|
|
|
ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
|
2014-05-12 13:14:46 +07:00
|
|
|
dev_name(dev), data);
|
|
|
|
if (ret) {
|
2014-05-12 13:14:54 +07:00
|
|
|
dev_err(dev, "Unabled to register handler of irq %d\n", irq);
|
|
|
|
return ret;
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:54 +07:00
|
|
|
data->clk = devm_clk_get(dev, "sysmmu");
|
2016-05-23 16:30:07 +07:00
|
|
|
if (PTR_ERR(data->clk) == -ENOENT)
|
2016-02-18 21:12:58 +07:00
|
|
|
data->clk = NULL;
|
2016-05-23 16:30:07 +07:00
|
|
|
else if (IS_ERR(data->clk))
|
|
|
|
return PTR_ERR(data->clk);
|
|
|
|
ret = clk_prepare(data->clk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-02-18 21:12:58 +07:00
|
|
|
|
|
|
|
data->aclk = devm_clk_get(dev, "aclk");
|
2016-05-23 16:30:07 +07:00
|
|
|
if (PTR_ERR(data->aclk) == -ENOENT)
|
2016-02-18 21:12:58 +07:00
|
|
|
data->aclk = NULL;
|
2016-05-23 16:30:07 +07:00
|
|
|
else if (IS_ERR(data->aclk))
|
|
|
|
return PTR_ERR(data->aclk);
|
|
|
|
ret = clk_prepare(data->aclk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-02-18 21:12:58 +07:00
|
|
|
|
|
|
|
data->pclk = devm_clk_get(dev, "pclk");
|
2016-05-23 16:30:07 +07:00
|
|
|
if (PTR_ERR(data->pclk) == -ENOENT)
|
2016-02-18 21:12:58 +07:00
|
|
|
data->pclk = NULL;
|
2016-05-23 16:30:07 +07:00
|
|
|
else if (IS_ERR(data->pclk))
|
|
|
|
return PTR_ERR(data->pclk);
|
|
|
|
ret = clk_prepare(data->pclk);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-02-18 21:12:58 +07:00
|
|
|
|
|
|
|
if (!data->clk && (!data->aclk || !data->pclk)) {
|
|
|
|
dev_err(dev, "Failed to get device clock(s)!\n");
|
|
|
|
return -ENOSYS;
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:55 +07:00
|
|
|
data->clk_master = devm_clk_get(dev, "master");
|
2016-05-23 16:30:07 +07:00
|
|
|
if (PTR_ERR(data->clk_master) == -ENOENT)
|
2016-02-18 21:12:51 +07:00
|
|
|
data->clk_master = NULL;
|
2016-05-23 16:30:07 +07:00
|
|
|
else if (IS_ERR(data->clk_master))
|
|
|
|
return PTR_ERR(data->clk_master);
|
|
|
|
ret = clk_prepare(data->clk_master);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2014-05-12 13:14:55 +07:00
|
|
|
|
2012-05-12 03:56:09 +07:00
|
|
|
data->sysmmu = dev;
|
2014-05-12 13:14:57 +07:00
|
|
|
spin_lock_init(&data->lock);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:46 +07:00
|
|
|
platform_set_drvdata(pdev, data);
|
|
|
|
|
2016-02-18 21:12:56 +07:00
|
|
|
__sysmmu_get_version(data);
|
2016-02-18 21:12:58 +07:00
|
|
|
if (PG_ENT_SHIFT < 0) {
|
|
|
|
if (MMU_MAJ_VER(data->version) < 5)
|
|
|
|
PG_ENT_SHIFT = SYSMMU_PG_ENT_SHIFT;
|
|
|
|
else
|
|
|
|
PG_ENT_SHIFT = SYSMMU_V5_PG_ENT_SHIFT;
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:52 +07:00
|
|
|
pm_runtime_enable(dev);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:35 +07:00
|
|
|
#ifdef CONFIG_PM_SLEEP
|
|
|
|
static int exynos_sysmmu_suspend(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sysmmu_drvdata *data = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
dev_dbg(dev, "suspend\n");
|
|
|
|
if (is_sysmmu_active(data)) {
|
|
|
|
__sysmmu_disable_nocount(data);
|
|
|
|
pm_runtime_put(dev);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int exynos_sysmmu_resume(struct device *dev)
|
|
|
|
{
|
|
|
|
struct sysmmu_drvdata *data = dev_get_drvdata(dev);
|
|
|
|
|
|
|
|
dev_dbg(dev, "resume\n");
|
|
|
|
if (is_sysmmu_active(data)) {
|
|
|
|
pm_runtime_get_sync(dev);
|
|
|
|
__sysmmu_enable_nocount(data);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct dev_pm_ops sysmmu_pm_ops = {
|
|
|
|
SET_LATE_SYSTEM_SLEEP_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume)
|
|
|
|
};
|
|
|
|
|
2014-05-12 13:15:02 +07:00
|
|
|
static const struct of_device_id sysmmu_of_match[] __initconst = {
|
|
|
|
{ .compatible = "samsung,exynos-sysmmu", },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct platform_driver exynos_sysmmu_driver __refdata = {
|
|
|
|
.probe = exynos_sysmmu_probe,
|
|
|
|
.driver = {
|
2012-05-12 03:56:09 +07:00
|
|
|
.name = "exynos-sysmmu",
|
2014-05-12 13:15:02 +07:00
|
|
|
.of_match_table = sysmmu_of_match,
|
2015-05-19 20:20:35 +07:00
|
|
|
.pm = &sysmmu_pm_ops,
|
2016-05-20 20:48:21 +07:00
|
|
|
.suppress_bind_attrs = true,
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
static inline void update_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2016-02-18 21:12:50 +07:00
|
|
|
dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
*ent = val;
|
|
|
|
dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2015-03-26 19:43:11 +07:00
|
|
|
static struct iommu_domain *exynos_iommu_domain_alloc(unsigned type)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2015-05-19 20:20:28 +07:00
|
|
|
struct exynos_iommu_domain *domain;
|
2016-02-18 21:12:50 +07:00
|
|
|
dma_addr_t handle;
|
2014-05-12 13:15:04 +07:00
|
|
|
int i;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:58 +07:00
|
|
|
/* Check if correct PTE offsets are initialized */
|
|
|
|
BUG_ON(PG_ENT_SHIFT < 0 || !dma_dev);
|
2015-03-26 19:43:11 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
domain = kzalloc(sizeof(*domain), GFP_KERNEL);
|
|
|
|
if (!domain)
|
2015-03-26 19:43:11 +07:00
|
|
|
return NULL;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:49 +07:00
|
|
|
if (type == IOMMU_DOMAIN_DMA) {
|
|
|
|
if (iommu_get_dma_cookie(&domain->domain) != 0)
|
|
|
|
goto err_pgtable;
|
|
|
|
} else if (type != IOMMU_DOMAIN_UNMANAGED) {
|
|
|
|
goto err_pgtable;
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
domain->pgtable = (sysmmu_pte_t *)__get_free_pages(GFP_KERNEL, 2);
|
|
|
|
if (!domain->pgtable)
|
2016-02-18 21:12:49 +07:00
|
|
|
goto err_dma_cookie;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
domain->lv2entcnt = (short *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 1);
|
|
|
|
if (!domain->lv2entcnt)
|
2012-05-12 03:56:09 +07:00
|
|
|
goto err_counter;
|
|
|
|
|
2014-08-04 11:36:28 +07:00
|
|
|
/* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
|
2014-05-12 13:15:04 +07:00
|
|
|
for (i = 0; i < NUM_LV1ENTRIES; i += 8) {
|
2015-05-19 20:20:28 +07:00
|
|
|
domain->pgtable[i + 0] = ZERO_LV2LINK;
|
|
|
|
domain->pgtable[i + 1] = ZERO_LV2LINK;
|
|
|
|
domain->pgtable[i + 2] = ZERO_LV2LINK;
|
|
|
|
domain->pgtable[i + 3] = ZERO_LV2LINK;
|
|
|
|
domain->pgtable[i + 4] = ZERO_LV2LINK;
|
|
|
|
domain->pgtable[i + 5] = ZERO_LV2LINK;
|
|
|
|
domain->pgtable[i + 6] = ZERO_LV2LINK;
|
|
|
|
domain->pgtable[i + 7] = ZERO_LV2LINK;
|
2014-05-12 13:15:04 +07:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
handle = dma_map_single(dma_dev, domain->pgtable, LV1TABLE_SIZE,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
/* For mapping page table entries we rely on dma == phys */
|
|
|
|
BUG_ON(handle != virt_to_phys(domain->pgtable));
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_lock_init(&domain->lock);
|
|
|
|
spin_lock_init(&domain->pgtablelock);
|
|
|
|
INIT_LIST_HEAD(&domain->clients);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
domain->domain.geometry.aperture_start = 0;
|
|
|
|
domain->domain.geometry.aperture_end = ~0UL;
|
|
|
|
domain->domain.geometry.force_aperture = true;
|
2012-07-11 17:41:10 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
return &domain->domain;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
err_counter:
|
2015-05-19 20:20:28 +07:00
|
|
|
free_pages((unsigned long)domain->pgtable, 2);
|
2016-02-18 21:12:49 +07:00
|
|
|
err_dma_cookie:
|
|
|
|
if (type == IOMMU_DOMAIN_DMA)
|
|
|
|
iommu_put_dma_cookie(&domain->domain);
|
2012-05-12 03:56:09 +07:00
|
|
|
err_pgtable:
|
2015-05-19 20:20:28 +07:00
|
|
|
kfree(domain);
|
2015-03-26 19:43:11 +07:00
|
|
|
return NULL;
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2015-05-19 20:20:28 +07:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2015-05-19 20:20:27 +07:00
|
|
|
struct sysmmu_drvdata *data, *next;
|
2012-05-12 03:56:09 +07:00
|
|
|
unsigned long flags;
|
|
|
|
int i;
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
WARN_ON(!list_empty(&domain->clients));
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_lock_irqsave(&domain->lock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
|
2015-05-19 20:20:27 +07:00
|
|
|
if (__sysmmu_disable(data))
|
|
|
|
data->master = NULL;
|
|
|
|
list_del_init(&data->domain_node);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_unlock_irqrestore(&domain->lock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:49 +07:00
|
|
|
if (iommu_domain->type == IOMMU_DOMAIN_DMA)
|
|
|
|
iommu_put_dma_cookie(iommu_domain);
|
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
|
2012-05-12 03:56:09 +07:00
|
|
|
for (i = 0; i < NUM_LV1ENTRIES; i++)
|
2016-02-18 21:12:50 +07:00
|
|
|
if (lv1ent_page(domain->pgtable + i)) {
|
|
|
|
phys_addr_t base = lv2table_base(domain->pgtable + i);
|
|
|
|
|
|
|
|
dma_unmap_single(dma_dev, base, LV2TABLE_SIZE,
|
|
|
|
DMA_TO_DEVICE);
|
2014-05-12 13:14:48 +07:00
|
|
|
kmem_cache_free(lv2table_kmem_cache,
|
2016-02-18 21:12:50 +07:00
|
|
|
phys_to_virt(base));
|
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
free_pages((unsigned long)domain->pgtable, 2);
|
|
|
|
free_pages((unsigned long)domain->lv2entcnt, 1);
|
|
|
|
kfree(domain);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:13:00 +07:00
|
|
|
static void exynos_iommu_detach_device(struct iommu_domain *iommu_domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
|
|
|
struct exynos_iommu_owner *owner = dev->archdata.iommu;
|
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
|
|
|
phys_addr_t pagetable = virt_to_phys(domain->pgtable);
|
|
|
|
struct sysmmu_drvdata *data, *next;
|
|
|
|
unsigned long flags;
|
|
|
|
bool found = false;
|
|
|
|
|
|
|
|
if (!has_sysmmu(dev) || owner->domain != iommu_domain)
|
|
|
|
return;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&domain->lock, flags);
|
|
|
|
list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
|
|
|
|
if (data->master == dev) {
|
|
|
|
if (__sysmmu_disable(data)) {
|
|
|
|
data->master = NULL;
|
|
|
|
list_del_init(&data->domain_node);
|
|
|
|
}
|
|
|
|
pm_runtime_put(data->sysmmu);
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&domain->lock, flags);
|
|
|
|
|
|
|
|
owner->domain = NULL;
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
dev_dbg(dev, "%s: Detached IOMMU with pgtable %pa\n",
|
|
|
|
__func__, &pagetable);
|
|
|
|
else
|
|
|
|
dev_err(dev, "%s: No IOMMU is attached\n", __func__);
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
|
2012-05-12 03:56:09 +07:00
|
|
|
struct device *dev)
|
|
|
|
{
|
2014-05-12 13:15:02 +07:00
|
|
|
struct exynos_iommu_owner *owner = dev->archdata.iommu;
|
2015-05-19 20:20:28 +07:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2015-05-19 20:20:27 +07:00
|
|
|
struct sysmmu_drvdata *data;
|
2015-05-19 20:20:28 +07:00
|
|
|
phys_addr_t pagetable = virt_to_phys(domain->pgtable);
|
2012-05-12 03:56:09 +07:00
|
|
|
unsigned long flags;
|
2015-05-19 20:20:27 +07:00
|
|
|
int ret = -ENODEV;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:27 +07:00
|
|
|
if (!has_sysmmu(dev))
|
|
|
|
return -ENODEV;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:13:00 +07:00
|
|
|
if (owner->domain)
|
|
|
|
exynos_iommu_detach_device(owner->domain, dev);
|
|
|
|
|
2015-05-19 20:20:33 +07:00
|
|
|
list_for_each_entry(data, &owner->controllers, owner_node) {
|
2015-05-19 20:20:34 +07:00
|
|
|
pm_runtime_get_sync(data->sysmmu);
|
2015-05-19 20:20:29 +07:00
|
|
|
ret = __sysmmu_enable(data, pagetable, domain);
|
2015-05-19 20:20:27 +07:00
|
|
|
if (ret >= 0) {
|
|
|
|
data->master = dev;
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_lock_irqsave(&domain->lock, flags);
|
|
|
|
list_add_tail(&data->domain_node, &domain->clients);
|
|
|
|
spin_unlock_irqrestore(&domain->lock, flags);
|
2015-05-19 20:20:27 +07:00
|
|
|
}
|
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
if (ret < 0) {
|
2014-05-12 13:14:46 +07:00
|
|
|
dev_err(dev, "%s: Failed to attach IOMMU with pgtable %pa\n",
|
|
|
|
__func__, &pagetable);
|
|
|
|
return ret;
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2016-02-18 21:13:00 +07:00
|
|
|
owner->domain = iommu_domain;
|
2014-05-12 13:14:46 +07:00
|
|
|
dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa %s\n",
|
|
|
|
__func__, &pagetable, (ret == 0) ? "" : ", again");
|
|
|
|
|
2012-05-12 03:56:09 +07:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
|
2014-05-12 13:15:04 +07:00
|
|
|
sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2014-05-12 13:14:47 +07:00
|
|
|
if (lv1ent_section(sent)) {
|
2014-05-12 13:14:58 +07:00
|
|
|
WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
|
2014-05-12 13:14:47 +07:00
|
|
|
return ERR_PTR(-EADDRINUSE);
|
|
|
|
}
|
|
|
|
|
2012-05-12 03:56:09 +07:00
|
|
|
if (lv1ent_fault(sent)) {
|
2014-05-12 13:14:58 +07:00
|
|
|
sysmmu_pte_t *pent;
|
2014-05-12 13:15:04 +07:00
|
|
|
bool need_flush_flpd_cache = lv1ent_zero(sent);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:48 +07:00
|
|
|
pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
|
2016-02-29 15:45:59 +07:00
|
|
|
BUG_ON((uintptr_t)pent & (LV2TABLE_SIZE - 1));
|
2012-05-12 03:56:09 +07:00
|
|
|
if (!pent)
|
2014-05-12 13:14:47 +07:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
update_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
|
2015-05-09 07:05:44 +07:00
|
|
|
kmemleak_ignore(pent);
|
2012-05-12 03:56:09 +07:00
|
|
|
*pgcounter = NUM_LV2ENTRIES;
|
2016-02-18 21:12:50 +07:00
|
|
|
dma_map_single(dma_dev, pent, LV2TABLE_SIZE, DMA_TO_DEVICE);
|
2014-05-12 13:15:04 +07:00
|
|
|
|
|
|
|
/*
|
2014-08-04 11:36:28 +07:00
|
|
|
* If pre-fetched SLPD is a faulty SLPD in zero_l2_table,
|
|
|
|
* FLPD cache may cache the address of zero_l2_table. This
|
|
|
|
* function replaces the zero_l2_table with new L2 page table
|
|
|
|
* to write valid mappings.
|
2014-05-12 13:15:04 +07:00
|
|
|
* Accessing the valid area may cause page fault since FLPD
|
2014-08-04 11:36:28 +07:00
|
|
|
* cache may still cache zero_l2_table for the valid area
|
|
|
|
* instead of new L2 page table that has the mapping
|
|
|
|
* information of the valid area.
|
2014-05-12 13:15:04 +07:00
|
|
|
* Thus any replacement of zero_l2_table with other valid L2
|
|
|
|
* page table must involve FLPD cache invalidation for System
|
|
|
|
* MMU v3.3.
|
|
|
|
* FLPD cache invalidation is performed with TLB invalidation
|
|
|
|
* by VPN without blocking. It is safe to invalidate TLB without
|
|
|
|
* blocking because the target address of TLB invalidation is
|
|
|
|
* not currently mapped.
|
|
|
|
*/
|
|
|
|
if (need_flush_flpd_cache) {
|
2015-05-19 20:20:27 +07:00
|
|
|
struct sysmmu_drvdata *data;
|
2014-05-22 11:20:56 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_lock(&domain->lock);
|
|
|
|
list_for_each_entry(data, &domain->clients, domain_node)
|
2015-05-19 20:20:27 +07:00
|
|
|
sysmmu_tlb_invalidate_flpdcache(data, iova);
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_unlock(&domain->lock);
|
2014-05-12 13:15:04 +07:00
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return page_entry(sent, iova);
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
static int lv1set_section(struct exynos_iommu_domain *domain,
|
2014-05-12 13:15:04 +07:00
|
|
|
sysmmu_pte_t *sent, sysmmu_iova_t iova,
|
2014-05-12 13:14:47 +07:00
|
|
|
phys_addr_t paddr, short *pgcnt)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2014-05-12 13:14:47 +07:00
|
|
|
if (lv1ent_section(sent)) {
|
2014-05-12 13:14:58 +07:00
|
|
|
WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
|
2014-05-12 13:14:47 +07:00
|
|
|
iova);
|
2012-05-12 03:56:09 +07:00
|
|
|
return -EADDRINUSE;
|
2014-05-12 13:14:47 +07:00
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
if (lv1ent_page(sent)) {
|
2014-05-12 13:14:47 +07:00
|
|
|
if (*pgcnt != NUM_LV2ENTRIES) {
|
2014-05-12 13:14:58 +07:00
|
|
|
WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
|
2014-05-12 13:14:47 +07:00
|
|
|
iova);
|
2012-05-12 03:56:09 +07:00
|
|
|
return -EADDRINUSE;
|
2014-05-12 13:14:47 +07:00
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:48 +07:00
|
|
|
kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
|
2012-05-12 03:56:09 +07:00
|
|
|
*pgcnt = 0;
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
update_pte(sent, mk_lv1ent_sect(paddr));
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_lock(&domain->lock);
|
2014-05-12 13:15:04 +07:00
|
|
|
if (lv1ent_page_zero(sent)) {
|
2015-05-19 20:20:27 +07:00
|
|
|
struct sysmmu_drvdata *data;
|
2014-05-12 13:15:04 +07:00
|
|
|
/*
|
|
|
|
* Flushing FLPD cache in System MMU v3.3 that may cache a FLPD
|
|
|
|
* entry by speculative prefetch of SLPD which has no mapping.
|
|
|
|
*/
|
2015-05-19 20:20:28 +07:00
|
|
|
list_for_each_entry(data, &domain->clients, domain_node)
|
2015-05-19 20:20:27 +07:00
|
|
|
sysmmu_tlb_invalidate_flpdcache(data, iova);
|
2014-05-12 13:15:04 +07:00
|
|
|
}
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_unlock(&domain->lock);
|
2014-05-12 13:15:04 +07:00
|
|
|
|
2012-05-12 03:56:09 +07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:58 +07:00
|
|
|
static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
|
2012-05-12 03:56:09 +07:00
|
|
|
short *pgcnt)
|
|
|
|
{
|
|
|
|
if (size == SPAGE_SIZE) {
|
2014-05-12 13:15:00 +07:00
|
|
|
if (WARN_ON(!lv2ent_fault(pent)))
|
2012-05-12 03:56:09 +07:00
|
|
|
return -EADDRINUSE;
|
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
update_pte(pent, mk_lv2ent_spage(paddr));
|
2012-05-12 03:56:09 +07:00
|
|
|
*pgcnt -= 1;
|
|
|
|
} else { /* size == LPAGE_SIZE */
|
|
|
|
int i;
|
2016-02-18 21:12:50 +07:00
|
|
|
dma_addr_t pent_base = virt_to_phys(pent);
|
2014-05-22 11:20:56 +07:00
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
dma_sync_single_for_cpu(dma_dev, pent_base,
|
|
|
|
sizeof(*pent) * SPAGES_PER_LPAGE,
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-12 03:56:09 +07:00
|
|
|
for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
|
2014-05-12 13:15:00 +07:00
|
|
|
if (WARN_ON(!lv2ent_fault(pent))) {
|
2014-05-12 13:14:47 +07:00
|
|
|
if (i > 0)
|
|
|
|
memset(pent - i, 0, sizeof(*pent) * i);
|
2012-05-12 03:56:09 +07:00
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pent = mk_lv2ent_lpage(paddr);
|
|
|
|
}
|
2016-02-18 21:12:50 +07:00
|
|
|
dma_sync_single_for_device(dma_dev, pent_base,
|
|
|
|
sizeof(*pent) * SPAGES_PER_LPAGE,
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-12 03:56:09 +07:00
|
|
|
*pgcnt -= SPAGES_PER_LPAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:15:04 +07:00
|
|
|
/*
|
|
|
|
* *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
|
|
|
|
*
|
2014-08-04 11:36:28 +07:00
|
|
|
* System MMU v3.x has advanced logic to improve address translation
|
2014-05-12 13:15:04 +07:00
|
|
|
* performance with caching more page table entries by a page table walk.
|
2014-08-04 11:36:28 +07:00
|
|
|
* However, the logic has a bug that while caching faulty page table entries,
|
|
|
|
* System MMU reports page fault if the cached fault entry is hit even though
|
|
|
|
* the fault entry is updated to a valid entry after the entry is cached.
|
|
|
|
* To prevent caching faulty page table entries which may be updated to valid
|
|
|
|
* entries later, the virtual memory manager should care about the workaround
|
|
|
|
* for the problem. The following describes the workaround.
|
2014-05-12 13:15:04 +07:00
|
|
|
*
|
|
|
|
* Any two consecutive I/O virtual address regions must have a hole of 128KiB
|
2014-08-04 11:36:28 +07:00
|
|
|
* at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
|
2014-05-12 13:15:04 +07:00
|
|
|
*
|
2014-08-04 11:36:28 +07:00
|
|
|
* Precisely, any start address of I/O virtual region must be aligned with
|
2014-05-12 13:15:04 +07:00
|
|
|
* the following sizes for System MMU v3.1 and v3.2.
|
|
|
|
* System MMU v3.1: 128KiB
|
|
|
|
* System MMU v3.2: 256KiB
|
|
|
|
*
|
|
|
|
* Because System MMU v3.3 caches page table entries more aggressively, it needs
|
2014-08-04 11:36:28 +07:00
|
|
|
* more workarounds.
|
|
|
|
* - Any two consecutive I/O virtual regions must have a hole of size larger
|
|
|
|
* than or equal to 128KiB.
|
2014-05-12 13:15:04 +07:00
|
|
|
* - Start address of an I/O virtual region must be aligned by 128KiB.
|
|
|
|
*/
|
2015-05-19 20:20:28 +07:00
|
|
|
static int exynos_iommu_map(struct iommu_domain *iommu_domain,
|
|
|
|
unsigned long l_iova, phys_addr_t paddr, size_t size,
|
|
|
|
int prot)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2015-05-19 20:20:28 +07:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2014-05-12 13:14:58 +07:00
|
|
|
sysmmu_pte_t *entry;
|
|
|
|
sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
|
2012-05-12 03:56:09 +07:00
|
|
|
unsigned long flags;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
BUG_ON(domain->pgtable == NULL);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_lock_irqsave(&domain->pgtablelock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
entry = section_entry(domain->pgtable, iova);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
if (size == SECT_SIZE) {
|
2015-05-19 20:20:28 +07:00
|
|
|
ret = lv1set_section(domain, entry, iova, paddr,
|
|
|
|
&domain->lv2entcnt[lv1ent_offset(iova)]);
|
2012-05-12 03:56:09 +07:00
|
|
|
} else {
|
2014-05-12 13:14:58 +07:00
|
|
|
sysmmu_pte_t *pent;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
pent = alloc_lv2entry(domain, entry, iova,
|
|
|
|
&domain->lv2entcnt[lv1ent_offset(iova)]);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:14:47 +07:00
|
|
|
if (IS_ERR(pent))
|
|
|
|
ret = PTR_ERR(pent);
|
2012-05-12 03:56:09 +07:00
|
|
|
else
|
|
|
|
ret = lv2set_page(pent, paddr, size,
|
2015-05-19 20:20:28 +07:00
|
|
|
&domain->lv2entcnt[lv1ent_offset(iova)]);
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:47 +07:00
|
|
|
if (ret)
|
2014-05-12 13:15:00 +07:00
|
|
|
pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
|
|
|
|
__func__, ret, size, iova);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_unlock_irqrestore(&domain->pgtablelock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
|
|
|
|
sysmmu_iova_t iova, size_t size)
|
2014-05-12 13:15:04 +07:00
|
|
|
{
|
2015-05-19 20:20:27 +07:00
|
|
|
struct sysmmu_drvdata *data;
|
2014-05-12 13:15:04 +07:00
|
|
|
unsigned long flags;
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_lock_irqsave(&domain->lock, flags);
|
2014-05-12 13:15:04 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
list_for_each_entry(data, &domain->clients, domain_node)
|
2015-05-19 20:20:27 +07:00
|
|
|
sysmmu_tlb_invalidate_entry(data, iova, size);
|
2014-05-12 13:15:04 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_unlock_irqrestore(&domain->lock, flags);
|
2014-05-12 13:15:04 +07:00
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
|
|
|
|
unsigned long l_iova, size_t size)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2015-05-19 20:20:28 +07:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2014-05-12 13:14:58 +07:00
|
|
|
sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
|
|
|
|
sysmmu_pte_t *ent;
|
2014-05-12 13:14:47 +07:00
|
|
|
size_t err_pgsize;
|
2014-05-12 13:14:58 +07:00
|
|
|
unsigned long flags;
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
BUG_ON(domain->pgtable == NULL);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_lock_irqsave(&domain->pgtablelock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
ent = section_entry(domain->pgtable, iova);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
if (lv1ent_section(ent)) {
|
2014-05-12 13:15:00 +07:00
|
|
|
if (WARN_ON(size < SECT_SIZE)) {
|
2014-05-12 13:14:47 +07:00
|
|
|
err_pgsize = SECT_SIZE;
|
|
|
|
goto err;
|
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-08-04 11:36:28 +07:00
|
|
|
/* workaround for h/w bug in System MMU v3.3 */
|
2016-02-18 21:12:50 +07:00
|
|
|
update_pte(ent, ZERO_LV2LINK);
|
2012-05-12 03:56:09 +07:00
|
|
|
size = SECT_SIZE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unlikely(lv1ent_fault(ent))) {
|
|
|
|
if (size > SECT_SIZE)
|
|
|
|
size = SECT_SIZE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lv1ent_page(sent) == true here */
|
|
|
|
|
|
|
|
ent = page_entry(ent, iova);
|
|
|
|
|
|
|
|
if (unlikely(lv2ent_fault(ent))) {
|
|
|
|
size = SPAGE_SIZE;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lv2ent_small(ent)) {
|
2016-02-18 21:12:50 +07:00
|
|
|
update_pte(ent, 0);
|
2012-05-12 03:56:09 +07:00
|
|
|
size = SPAGE_SIZE;
|
2015-05-19 20:20:28 +07:00
|
|
|
domain->lv2entcnt[lv1ent_offset(iova)] += 1;
|
2012-05-12 03:56:09 +07:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lv1ent_large(ent) == true here */
|
2014-05-12 13:15:00 +07:00
|
|
|
if (WARN_ON(size < LPAGE_SIZE)) {
|
2014-05-12 13:14:47 +07:00
|
|
|
err_pgsize = LPAGE_SIZE;
|
|
|
|
goto err;
|
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
|
|
|
|
sizeof(*ent) * SPAGES_PER_LPAGE,
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-12 03:56:09 +07:00
|
|
|
memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
|
2016-02-18 21:12:50 +07:00
|
|
|
dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
|
|
|
|
sizeof(*ent) * SPAGES_PER_LPAGE,
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-12 03:56:09 +07:00
|
|
|
size = LPAGE_SIZE;
|
2015-05-19 20:20:28 +07:00
|
|
|
domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
|
2012-05-12 03:56:09 +07:00
|
|
|
done:
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_unlock_irqrestore(&domain->pgtablelock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
exynos_iommu_tlb_invalidate_entry(domain, iova, size);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
return size;
|
2014-05-12 13:14:47 +07:00
|
|
|
err:
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_unlock_irqrestore(&domain->pgtablelock, flags);
|
2014-05-12 13:14:47 +07:00
|
|
|
|
2014-05-12 13:15:00 +07:00
|
|
|
pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
|
|
|
|
__func__, size, iova, err_pgsize);
|
2014-05-12 13:14:47 +07:00
|
|
|
|
|
|
|
return 0;
|
2012-05-12 03:56:09 +07:00
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
|
2013-03-29 02:53:58 +07:00
|
|
|
dma_addr_t iova)
|
2012-05-12 03:56:09 +07:00
|
|
|
{
|
2015-05-19 20:20:28 +07:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2014-05-12 13:14:58 +07:00
|
|
|
sysmmu_pte_t *entry;
|
2012-05-12 03:56:09 +07:00
|
|
|
unsigned long flags;
|
|
|
|
phys_addr_t phys = 0;
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_lock_irqsave(&domain->pgtablelock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
entry = section_entry(domain->pgtable, iova);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
if (lv1ent_section(entry)) {
|
|
|
|
phys = section_phys(entry) + section_offs(iova);
|
|
|
|
} else if (lv1ent_page(entry)) {
|
|
|
|
entry = page_entry(entry, iova);
|
|
|
|
|
|
|
|
if (lv2ent_large(entry))
|
|
|
|
phys = lpage_phys(entry) + lpage_offs(iova);
|
|
|
|
else if (lv2ent_small(entry))
|
|
|
|
phys = spage_phys(entry) + spage_offs(iova);
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:28 +07:00
|
|
|
spin_unlock_irqrestore(&domain->pgtablelock, flags);
|
2012-05-12 03:56:09 +07:00
|
|
|
|
|
|
|
return phys;
|
|
|
|
}
|
|
|
|
|
2016-02-18 21:12:48 +07:00
|
|
|
static struct iommu_group *get_device_iommu_group(struct device *dev)
|
|
|
|
{
|
|
|
|
struct iommu_group *group;
|
|
|
|
|
|
|
|
group = iommu_group_get(dev);
|
|
|
|
if (!group)
|
|
|
|
group = iommu_group_alloc();
|
|
|
|
|
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:59 +07:00
|
|
|
static int exynos_iommu_add_device(struct device *dev)
|
|
|
|
{
|
|
|
|
struct iommu_group *group;
|
|
|
|
|
2015-05-19 20:20:32 +07:00
|
|
|
if (!has_sysmmu(dev))
|
|
|
|
return -ENODEV;
|
|
|
|
|
2016-02-18 21:12:48 +07:00
|
|
|
group = iommu_group_get_for_dev(dev);
|
2014-05-12 13:14:59 +07:00
|
|
|
|
2016-02-18 21:12:48 +07:00
|
|
|
if (IS_ERR(group))
|
|
|
|
return PTR_ERR(group);
|
2014-05-12 13:14:59 +07:00
|
|
|
|
|
|
|
iommu_group_put(group);
|
|
|
|
|
2016-02-18 21:12:48 +07:00
|
|
|
return 0;
|
2014-05-12 13:14:59 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void exynos_iommu_remove_device(struct device *dev)
|
|
|
|
{
|
2015-05-19 20:20:32 +07:00
|
|
|
if (!has_sysmmu(dev))
|
|
|
|
return;
|
|
|
|
|
2014-05-12 13:14:59 +07:00
|
|
|
iommu_group_remove_device(dev);
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:37 +07:00
|
|
|
static int exynos_iommu_of_xlate(struct device *dev,
|
|
|
|
struct of_phandle_args *spec)
|
|
|
|
{
|
|
|
|
struct exynos_iommu_owner *owner = dev->archdata.iommu;
|
|
|
|
struct platform_device *sysmmu = of_find_device_by_node(spec->np);
|
|
|
|
struct sysmmu_drvdata *data;
|
|
|
|
|
|
|
|
if (!sysmmu)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
data = platform_get_drvdata(sysmmu);
|
|
|
|
if (!data)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
if (!owner) {
|
|
|
|
owner = kzalloc(sizeof(*owner), GFP_KERNEL);
|
|
|
|
if (!owner)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&owner->controllers);
|
|
|
|
dev->archdata.iommu = owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
list_add_tail(&data->owner_node, &owner->controllers);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:20:36 +07:00
|
|
|
static struct iommu_ops exynos_iommu_ops = {
|
2015-03-26 19:43:11 +07:00
|
|
|
.domain_alloc = exynos_iommu_domain_alloc,
|
|
|
|
.domain_free = exynos_iommu_domain_free,
|
2014-05-09 03:49:14 +07:00
|
|
|
.attach_dev = exynos_iommu_attach_device,
|
|
|
|
.detach_dev = exynos_iommu_detach_device,
|
|
|
|
.map = exynos_iommu_map,
|
|
|
|
.unmap = exynos_iommu_unmap,
|
2014-10-25 23:55:16 +07:00
|
|
|
.map_sg = default_iommu_map_sg,
|
2014-05-09 03:49:14 +07:00
|
|
|
.iova_to_phys = exynos_iommu_iova_to_phys,
|
2016-02-18 21:12:48 +07:00
|
|
|
.device_group = get_device_iommu_group,
|
2014-05-09 03:49:14 +07:00
|
|
|
.add_device = exynos_iommu_add_device,
|
|
|
|
.remove_device = exynos_iommu_remove_device,
|
2012-05-12 03:56:09 +07:00
|
|
|
.pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
|
2015-05-19 20:20:37 +07:00
|
|
|
.of_xlate = exynos_iommu_of_xlate,
|
2012-05-12 03:56:09 +07:00
|
|
|
};
|
|
|
|
|
2015-05-19 20:20:36 +07:00
|
|
|
static bool init_done;
|
|
|
|
|
2012-05-12 03:56:09 +07:00
|
|
|
static int __init exynos_iommu_init(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2014-05-12 13:14:48 +07:00
|
|
|
lv2table_kmem_cache = kmem_cache_create("exynos-iommu-lv2table",
|
|
|
|
LV2TABLE_SIZE, LV2TABLE_SIZE, 0, NULL);
|
|
|
|
if (!lv2table_kmem_cache) {
|
|
|
|
pr_err("%s: Failed to create kmem cache\n", __func__);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2012-05-12 03:56:09 +07:00
|
|
|
ret = platform_driver_register(&exynos_sysmmu_driver);
|
2014-05-12 13:14:48 +07:00
|
|
|
if (ret) {
|
|
|
|
pr_err("%s: Failed to register driver\n", __func__);
|
|
|
|
goto err_reg_driver;
|
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2014-05-12 13:15:04 +07:00
|
|
|
zero_lv2_table = kmem_cache_zalloc(lv2table_kmem_cache, GFP_KERNEL);
|
|
|
|
if (zero_lv2_table == NULL) {
|
|
|
|
pr_err("%s: Failed to allocate zero level2 page table\n",
|
|
|
|
__func__);
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto err_zero_lv2;
|
|
|
|
}
|
|
|
|
|
2014-05-12 13:14:48 +07:00
|
|
|
ret = bus_set_iommu(&platform_bus_type, &exynos_iommu_ops);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("%s: Failed to register exynos-iommu driver.\n",
|
|
|
|
__func__);
|
|
|
|
goto err_set_iommu;
|
|
|
|
}
|
2012-05-12 03:56:09 +07:00
|
|
|
|
2015-05-19 20:20:36 +07:00
|
|
|
init_done = true;
|
|
|
|
|
2014-05-12 13:14:48 +07:00
|
|
|
return 0;
|
|
|
|
err_set_iommu:
|
2014-05-12 13:15:04 +07:00
|
|
|
kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
|
|
|
|
err_zero_lv2:
|
2014-05-12 13:14:48 +07:00
|
|
|
platform_driver_unregister(&exynos_sysmmu_driver);
|
|
|
|
err_reg_driver:
|
|
|
|
kmem_cache_destroy(lv2table_kmem_cache);
|
2012-05-12 03:56:09 +07:00
|
|
|
return ret;
|
|
|
|
}
|
2015-05-19 20:20:36 +07:00
|
|
|
|
|
|
|
static int __init exynos_iommu_of_setup(struct device_node *np)
|
|
|
|
{
|
|
|
|
struct platform_device *pdev;
|
|
|
|
|
|
|
|
if (!init_done)
|
|
|
|
exynos_iommu_init();
|
|
|
|
|
|
|
|
pdev = of_platform_device_create(np, NULL, platform_bus_type.dev_root);
|
|
|
|
if (IS_ERR(pdev))
|
|
|
|
return PTR_ERR(pdev);
|
|
|
|
|
2016-02-18 21:12:50 +07:00
|
|
|
/*
|
|
|
|
* use the first registered sysmmu device for performing
|
|
|
|
* dma mapping operations on iommu page tables (cpu cache flush)
|
|
|
|
*/
|
|
|
|
if (!dma_dev)
|
|
|
|
dma_dev = &pdev->dev;
|
|
|
|
|
2015-05-19 20:20:36 +07:00
|
|
|
of_iommu_set_ops(np, &exynos_iommu_ops);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
IOMMU_OF_DECLARE(exynos_iommu_of, "samsung,exynos-sysmmu",
|
|
|
|
exynos_iommu_of_setup);
|