2011-02-25 02:00:39 +00:00
|
|
|
/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
|
2010-08-25 01:31:10 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 and
|
|
|
|
* only version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
|
|
|
* 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/io.h>
|
|
|
|
#include <linux/interrupt.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/spinlock.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/iommu.h>
|
2011-02-25 02:00:39 +00:00
|
|
|
#include <linux/clk.h>
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
#include <asm/cacheflush.h>
|
|
|
|
#include <asm/sizes.h>
|
|
|
|
|
2013-07-24 20:54:33 +00:00
|
|
|
#include "msm_iommu_hw-8xxx.h"
|
|
|
|
#include "msm_iommu.h"
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2010-11-16 02:20:08 +00:00
|
|
|
#define MRC(reg, processor, op1, crn, crm, op2) \
|
|
|
|
__asm__ __volatile__ ( \
|
|
|
|
" mrc " #processor "," #op1 ", %0," #crn "," #crm "," #op2 "\n" \
|
|
|
|
: "=r" (reg))
|
|
|
|
|
|
|
|
#define RCP15_PRRR(reg) MRC(reg, p15, 0, c10, c2, 0)
|
|
|
|
#define RCP15_NMRR(reg) MRC(reg, p15, 0, c10, c2, 1)
|
|
|
|
|
2011-11-10 09:32:28 +00:00
|
|
|
/* bitmap of the page sizes currently supported */
|
|
|
|
#define MSM_IOMMU_PGSIZES (SZ_4K | SZ_64K | SZ_1M | SZ_16M)
|
|
|
|
|
2010-11-16 02:20:08 +00:00
|
|
|
static int msm_iommu_tex_class[4];
|
|
|
|
|
2010-08-25 01:31:10 +00:00
|
|
|
DEFINE_SPINLOCK(msm_iommu_lock);
|
2016-06-13 11:36:02 +00:00
|
|
|
static LIST_HEAD(qcom_iommu_devices);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
struct msm_priv {
|
|
|
|
unsigned long *pgtable;
|
|
|
|
struct list_head list_attached;
|
2015-03-26 12:43:14 +00:00
|
|
|
struct iommu_domain domain;
|
2010-08-25 01:31:10 +00:00
|
|
|
};
|
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
static struct msm_priv *to_msm_priv(struct iommu_domain *dom)
|
|
|
|
{
|
|
|
|
return container_of(dom, struct msm_priv, domain);
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
static int __enable_clocks(struct msm_iommu_dev *iommu)
|
2011-02-25 02:00:39 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
ret = clk_enable(iommu->pclk);
|
2011-02-25 02:00:39 +00:00
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
if (iommu->clk) {
|
|
|
|
ret = clk_enable(iommu->clk);
|
2011-02-25 02:00:39 +00:00
|
|
|
if (ret)
|
2016-06-13 11:36:02 +00:00
|
|
|
clk_disable(iommu->pclk);
|
2011-02-25 02:00:39 +00:00
|
|
|
}
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
static void __disable_clocks(struct msm_iommu_dev *iommu)
|
2011-02-25 02:00:39 +00:00
|
|
|
{
|
2016-06-13 11:36:02 +00:00
|
|
|
if (iommu->clk)
|
|
|
|
clk_disable(iommu->clk);
|
|
|
|
clk_disable(iommu->pclk);
|
2011-02-25 02:00:39 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 03:30:00 +00:00
|
|
|
static int __flush_iotlb(struct iommu_domain *domain)
|
2010-08-25 01:31:10 +00:00
|
|
|
{
|
2015-03-26 12:43:14 +00:00
|
|
|
struct msm_priv *priv = to_msm_priv(domain);
|
2016-06-13 11:36:02 +00:00
|
|
|
struct msm_iommu_dev *iommu = NULL;
|
|
|
|
struct msm_iommu_ctx_dev *master;
|
2010-11-13 03:30:00 +00:00
|
|
|
int ret = 0;
|
2016-06-13 11:36:02 +00:00
|
|
|
|
2010-08-25 01:31:10 +00:00
|
|
|
#ifndef CONFIG_IOMMU_PGTABLES_L2
|
|
|
|
unsigned long *fl_table = priv->pgtable;
|
|
|
|
int i;
|
|
|
|
|
2010-11-13 03:29:54 +00:00
|
|
|
if (!list_empty(&priv->list_attached)) {
|
|
|
|
dmac_flush_range(fl_table, fl_table + SZ_16K);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2010-11-13 03:29:54 +00:00
|
|
|
for (i = 0; i < NUM_FL_PTE; i++)
|
|
|
|
if ((fl_table[i] & 0x03) == FL_TYPE_TABLE) {
|
|
|
|
void *sl_table = __va(fl_table[i] &
|
|
|
|
FL_BASE_MASK);
|
|
|
|
dmac_flush_range(sl_table, sl_table + SZ_4K);
|
|
|
|
}
|
|
|
|
}
|
2010-08-25 01:31:10 +00:00
|
|
|
#endif
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
list_for_each_entry(iommu, &priv->list_attached, dom_node) {
|
|
|
|
ret = __enable_clocks(iommu);
|
2011-02-25 02:00:39 +00:00
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
list_for_each_entry(master, &iommu->ctx_list, list)
|
|
|
|
SET_CTX_TLBIALL(iommu->base, master->num, 0);
|
|
|
|
|
|
|
|
__disable_clocks(iommu);
|
2010-08-25 01:31:10 +00:00
|
|
|
}
|
2011-02-25 02:00:39 +00:00
|
|
|
fail:
|
2010-11-13 03:30:00 +00:00
|
|
|
return ret;
|
2010-08-25 01:31:10 +00:00
|
|
|
}
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
static int msm_iommu_alloc_ctx(unsigned long *map, int start, int end)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
do {
|
|
|
|
idx = find_next_zero_bit(map, end, start);
|
|
|
|
if (idx == end)
|
|
|
|
return -ENOSPC;
|
|
|
|
} while (test_and_set_bit(idx, map));
|
|
|
|
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void msm_iommu_free_ctx(unsigned long *map, int idx)
|
|
|
|
{
|
|
|
|
clear_bit(idx, map);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void config_mids(struct msm_iommu_dev *iommu,
|
|
|
|
struct msm_iommu_ctx_dev *master)
|
|
|
|
{
|
|
|
|
int mid, ctx, i;
|
|
|
|
|
|
|
|
for (i = 0; i < master->num_mids; i++) {
|
|
|
|
mid = master->mids[i];
|
|
|
|
ctx = master->num;
|
|
|
|
|
|
|
|
SET_M2VCBR_N(iommu->base, mid, 0);
|
|
|
|
SET_CBACR_N(iommu->base, ctx, 0);
|
|
|
|
|
|
|
|
/* Set VMID = 0 */
|
|
|
|
SET_VMID(iommu->base, mid, 0);
|
|
|
|
|
|
|
|
/* Set the context number for that MID to this context */
|
|
|
|
SET_CBNDX(iommu->base, mid, ctx);
|
|
|
|
|
|
|
|
/* Set MID associated with this context bank to 0*/
|
|
|
|
SET_CBVMID(iommu->base, ctx, 0);
|
|
|
|
|
|
|
|
/* Set the ASID for TLB tagging for this context */
|
|
|
|
SET_CONTEXTIDR_ASID(iommu->base, ctx, ctx);
|
|
|
|
|
|
|
|
/* Set security bit override to be Non-secure */
|
|
|
|
SET_NSCFG(iommu->base, mid, 3);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-25 01:31:10 +00:00
|
|
|
static void __reset_context(void __iomem *base, int ctx)
|
|
|
|
{
|
|
|
|
SET_BPRCOSH(base, ctx, 0);
|
|
|
|
SET_BPRCISH(base, ctx, 0);
|
|
|
|
SET_BPRCNSH(base, ctx, 0);
|
|
|
|
SET_BPSHCFG(base, ctx, 0);
|
|
|
|
SET_BPMTCFG(base, ctx, 0);
|
|
|
|
SET_ACTLR(base, ctx, 0);
|
|
|
|
SET_SCTLR(base, ctx, 0);
|
|
|
|
SET_FSRRESTORE(base, ctx, 0);
|
|
|
|
SET_TTBR0(base, ctx, 0);
|
|
|
|
SET_TTBR1(base, ctx, 0);
|
|
|
|
SET_TTBCR(base, ctx, 0);
|
|
|
|
SET_BFBCR(base, ctx, 0);
|
|
|
|
SET_PAR(base, ctx, 0);
|
|
|
|
SET_FAR(base, ctx, 0);
|
|
|
|
SET_CTX_TLBIALL(base, ctx, 0);
|
|
|
|
SET_TLBFLPTER(base, ctx, 0);
|
|
|
|
SET_TLBSLPTER(base, ctx, 0);
|
|
|
|
SET_TLBLKCR(base, ctx, 0);
|
|
|
|
SET_PRRR(base, ctx, 0);
|
|
|
|
SET_NMRR(base, ctx, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __program_context(void __iomem *base, int ctx, phys_addr_t pgtable)
|
|
|
|
{
|
2010-11-16 02:20:08 +00:00
|
|
|
unsigned int prrr, nmrr;
|
2010-08-25 01:31:10 +00:00
|
|
|
__reset_context(base, ctx);
|
|
|
|
|
|
|
|
/* Set up HTW mode */
|
|
|
|
/* TLB miss configuration: perform HTW on miss */
|
|
|
|
SET_TLBMCFG(base, ctx, 0x3);
|
|
|
|
|
|
|
|
/* V2P configuration: HTW for access */
|
|
|
|
SET_V2PCFG(base, ctx, 0x3);
|
|
|
|
|
|
|
|
SET_TTBCR(base, ctx, 0);
|
|
|
|
SET_TTBR0_PA(base, ctx, (pgtable >> 14));
|
|
|
|
|
|
|
|
/* Invalidate the TLB for this context */
|
|
|
|
SET_CTX_TLBIALL(base, ctx, 0);
|
|
|
|
|
|
|
|
/* Set interrupt number to "secure" interrupt */
|
|
|
|
SET_IRPTNDX(base, ctx, 0);
|
|
|
|
|
|
|
|
/* Enable context fault interrupt */
|
|
|
|
SET_CFEIE(base, ctx, 1);
|
|
|
|
|
|
|
|
/* Stall access on a context fault and let the handler deal with it */
|
|
|
|
SET_CFCFG(base, ctx, 1);
|
|
|
|
|
|
|
|
/* Redirect all cacheable requests to L2 slave port. */
|
|
|
|
SET_RCISH(base, ctx, 1);
|
|
|
|
SET_RCOSH(base, ctx, 1);
|
|
|
|
SET_RCNSH(base, ctx, 1);
|
|
|
|
|
|
|
|
/* Turn on TEX Remap */
|
|
|
|
SET_TRE(base, ctx, 1);
|
|
|
|
|
2010-11-16 02:20:08 +00:00
|
|
|
/* Set TEX remap attributes */
|
|
|
|
RCP15_PRRR(prrr);
|
|
|
|
RCP15_NMRR(nmrr);
|
|
|
|
SET_PRRR(base, ctx, prrr);
|
|
|
|
SET_NMRR(base, ctx, nmrr);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
/* Turn on BFB prefetch */
|
|
|
|
SET_BFBDFE(base, ctx, 1);
|
|
|
|
|
|
|
|
#ifdef CONFIG_IOMMU_PGTABLES_L2
|
|
|
|
/* Configure page tables as inner-cacheable and shareable to reduce
|
|
|
|
* the TLB miss penalty.
|
|
|
|
*/
|
|
|
|
SET_TTBR0_SH(base, ctx, 1);
|
|
|
|
SET_TTBR1_SH(base, ctx, 1);
|
|
|
|
|
|
|
|
SET_TTBR0_NOS(base, ctx, 1);
|
|
|
|
SET_TTBR1_NOS(base, ctx, 1);
|
|
|
|
|
|
|
|
SET_TTBR0_IRGNH(base, ctx, 0); /* WB, WA */
|
|
|
|
SET_TTBR0_IRGNL(base, ctx, 1);
|
|
|
|
|
|
|
|
SET_TTBR1_IRGNH(base, ctx, 0); /* WB, WA */
|
|
|
|
SET_TTBR1_IRGNL(base, ctx, 1);
|
|
|
|
|
|
|
|
SET_TTBR0_ORGN(base, ctx, 1); /* WB, WA */
|
|
|
|
SET_TTBR1_ORGN(base, ctx, 1); /* WB, WA */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Enable the MMU */
|
|
|
|
SET_M(base, ctx, 1);
|
|
|
|
}
|
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
static struct iommu_domain *msm_iommu_domain_alloc(unsigned type)
|
2010-08-25 01:31:10 +00:00
|
|
|
{
|
2015-03-26 12:43:14 +00:00
|
|
|
struct msm_priv *priv;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
if (type != IOMMU_DOMAIN_UNMANAGED)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
priv = kzalloc(sizeof(*priv), GFP_KERNEL);
|
2010-08-25 01:31:10 +00:00
|
|
|
if (!priv)
|
|
|
|
goto fail_nomem;
|
|
|
|
|
|
|
|
INIT_LIST_HEAD(&priv->list_attached);
|
|
|
|
priv->pgtable = (unsigned long *)__get_free_pages(GFP_KERNEL,
|
|
|
|
get_order(SZ_16K));
|
|
|
|
|
|
|
|
if (!priv->pgtable)
|
|
|
|
goto fail_nomem;
|
|
|
|
|
|
|
|
memset(priv->pgtable, 0, SZ_16K);
|
2012-01-26 18:40:56 +00:00
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
priv->domain.geometry.aperture_start = 0;
|
|
|
|
priv->domain.geometry.aperture_end = (1ULL << 32) - 1;
|
|
|
|
priv->domain.geometry.force_aperture = true;
|
2012-01-26 18:40:56 +00:00
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
return &priv->domain;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
fail_nomem:
|
|
|
|
kfree(priv);
|
2015-03-26 12:43:14 +00:00
|
|
|
return NULL;
|
2010-08-25 01:31:10 +00:00
|
|
|
}
|
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
static void msm_iommu_domain_free(struct iommu_domain *domain)
|
2010-08-25 01:31:10 +00:00
|
|
|
{
|
|
|
|
struct msm_priv *priv;
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned long *fl_table;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&msm_iommu_lock, flags);
|
2015-03-26 12:43:14 +00:00
|
|
|
priv = to_msm_priv(domain);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
fl_table = priv->pgtable;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
for (i = 0; i < NUM_FL_PTE; i++)
|
|
|
|
if ((fl_table[i] & 0x03) == FL_TYPE_TABLE)
|
|
|
|
free_page((unsigned long) __va(((fl_table[i]) &
|
|
|
|
FL_BASE_MASK)));
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
free_pages((unsigned long)priv->pgtable, get_order(SZ_16K));
|
|
|
|
priv->pgtable = NULL;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
kfree(priv);
|
|
|
|
spin_unlock_irqrestore(&msm_iommu_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int msm_iommu_attach_dev(struct iommu_domain *domain, struct device *dev)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
unsigned long flags;
|
2016-06-13 11:36:02 +00:00
|
|
|
struct msm_iommu_dev *iommu;
|
|
|
|
struct msm_priv *priv = to_msm_priv(domain);
|
|
|
|
struct msm_iommu_ctx_dev *master;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&msm_iommu_lock, flags);
|
2016-06-13 11:36:02 +00:00
|
|
|
list_for_each_entry(iommu, &qcom_iommu_devices, dev_node) {
|
|
|
|
master = list_first_entry(&iommu->ctx_list,
|
|
|
|
struct msm_iommu_ctx_dev,
|
|
|
|
list);
|
|
|
|
if (master->of_node == dev->of_node) {
|
|
|
|
ret = __enable_clocks(iommu);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
list_for_each_entry(master, &iommu->ctx_list, list) {
|
|
|
|
if (master->num) {
|
|
|
|
dev_err(dev, "domain already attached");
|
|
|
|
ret = -EEXIST;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
master->num =
|
|
|
|
msm_iommu_alloc_ctx(iommu->context_map,
|
|
|
|
0, iommu->ncb);
|
|
|
|
if (IS_ERR_VALUE(master->num)) {
|
|
|
|
ret = -ENODEV;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
config_mids(iommu, master);
|
|
|
|
__program_context(iommu->base, master->num,
|
|
|
|
__pa(priv->pgtable));
|
|
|
|
}
|
|
|
|
__disable_clocks(iommu);
|
|
|
|
list_add(&iommu->dom_node, &priv->list_attached);
|
2010-08-25 01:31:10 +00:00
|
|
|
}
|
2016-06-13 11:36:02 +00:00
|
|
|
}
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2010-11-13 03:30:00 +00:00
|
|
|
ret = __flush_iotlb(domain);
|
2010-08-25 01:31:10 +00:00
|
|
|
fail:
|
|
|
|
spin_unlock_irqrestore(&msm_iommu_lock, flags);
|
2016-06-13 11:36:02 +00:00
|
|
|
|
2010-08-25 01:31:10 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void msm_iommu_detach_dev(struct iommu_domain *domain,
|
|
|
|
struct device *dev)
|
|
|
|
{
|
2016-06-13 11:36:02 +00:00
|
|
|
struct msm_priv *priv = to_msm_priv(domain);
|
2010-08-25 01:31:10 +00:00
|
|
|
unsigned long flags;
|
2016-06-13 11:36:02 +00:00
|
|
|
struct msm_iommu_dev *iommu;
|
|
|
|
struct msm_iommu_ctx_dev *master;
|
2010-11-13 03:30:00 +00:00
|
|
|
int ret;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&msm_iommu_lock, flags);
|
2010-11-13 03:30:00 +00:00
|
|
|
ret = __flush_iotlb(domain);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
list_for_each_entry(iommu, &priv->list_attached, dom_node) {
|
|
|
|
ret = __enable_clocks(iommu);
|
|
|
|
if (ret)
|
|
|
|
goto fail;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
list_for_each_entry(master, &iommu->ctx_list, list) {
|
|
|
|
msm_iommu_free_ctx(iommu->context_map, master->num);
|
|
|
|
__reset_context(iommu->base, master->num);
|
|
|
|
}
|
|
|
|
__disable_clocks(iommu);
|
|
|
|
}
|
2010-08-25 01:31:10 +00:00
|
|
|
fail:
|
|
|
|
spin_unlock_irqrestore(&msm_iommu_lock, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int msm_iommu_map(struct iommu_domain *domain, unsigned long va,
|
2011-11-10 09:32:25 +00:00
|
|
|
phys_addr_t pa, size_t len, int prot)
|
2010-08-25 01:31:10 +00:00
|
|
|
{
|
|
|
|
struct msm_priv *priv;
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned long *fl_table;
|
|
|
|
unsigned long *fl_pte;
|
|
|
|
unsigned long fl_offset;
|
|
|
|
unsigned long *sl_table;
|
|
|
|
unsigned long *sl_pte;
|
|
|
|
unsigned long sl_offset;
|
2010-11-16 02:20:08 +00:00
|
|
|
unsigned int pgprot;
|
|
|
|
int ret = 0, tex, sh;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&msm_iommu_lock, flags);
|
|
|
|
|
2010-11-16 02:20:08 +00:00
|
|
|
sh = (prot & MSM_IOMMU_ATTR_SH) ? 1 : 0;
|
|
|
|
tex = msm_iommu_tex_class[prot & MSM_IOMMU_CP_MASK];
|
|
|
|
|
|
|
|
if (tex < 0 || tex > NUM_TEX_CLASS - 1) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
priv = to_msm_priv(domain);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
fl_table = priv->pgtable;
|
|
|
|
|
|
|
|
if (len != SZ_16M && len != SZ_1M &&
|
|
|
|
len != SZ_64K && len != SZ_4K) {
|
|
|
|
pr_debug("Bad size: %d\n", len);
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fl_table) {
|
|
|
|
pr_debug("Null page table\n");
|
|
|
|
ret = -EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2010-11-16 02:20:08 +00:00
|
|
|
if (len == SZ_16M || len == SZ_1M) {
|
|
|
|
pgprot = sh ? FL_SHARED : 0;
|
|
|
|
pgprot |= tex & 0x01 ? FL_BUFFERABLE : 0;
|
|
|
|
pgprot |= tex & 0x02 ? FL_CACHEABLE : 0;
|
|
|
|
pgprot |= tex & 0x04 ? FL_TEX0 : 0;
|
|
|
|
} else {
|
|
|
|
pgprot = sh ? SL_SHARED : 0;
|
|
|
|
pgprot |= tex & 0x01 ? SL_BUFFERABLE : 0;
|
|
|
|
pgprot |= tex & 0x02 ? SL_CACHEABLE : 0;
|
|
|
|
pgprot |= tex & 0x04 ? SL_TEX0 : 0;
|
|
|
|
}
|
|
|
|
|
2010-08-25 01:31:10 +00:00
|
|
|
fl_offset = FL_OFFSET(va); /* Upper 12 bits */
|
|
|
|
fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
|
|
|
|
|
|
|
|
if (len == SZ_16M) {
|
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
*(fl_pte+i) = (pa & 0xFF000000) | FL_SUPERSECTION |
|
|
|
|
FL_AP_READ | FL_AP_WRITE | FL_TYPE_SECT |
|
2011-02-25 02:00:41 +00:00
|
|
|
FL_SHARED | FL_NG | pgprot;
|
2010-08-25 01:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (len == SZ_1M)
|
2011-02-25 02:00:41 +00:00
|
|
|
*fl_pte = (pa & 0xFFF00000) | FL_AP_READ | FL_AP_WRITE | FL_NG |
|
2010-11-16 02:20:08 +00:00
|
|
|
FL_TYPE_SECT | FL_SHARED | pgprot;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
/* Need a 2nd level table */
|
|
|
|
if ((len == SZ_4K || len == SZ_64K) && (*fl_pte) == 0) {
|
|
|
|
unsigned long *sl;
|
2010-12-10 22:12:03 +00:00
|
|
|
sl = (unsigned long *) __get_free_pages(GFP_ATOMIC,
|
2010-08-25 01:31:10 +00:00
|
|
|
get_order(SZ_4K));
|
|
|
|
|
|
|
|
if (!sl) {
|
|
|
|
pr_debug("Could not allocate second level table\n");
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(sl, 0, SZ_4K);
|
|
|
|
*fl_pte = ((((int)__pa(sl)) & FL_BASE_MASK) | FL_TYPE_TABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
|
|
|
|
sl_offset = SL_OFFSET(va);
|
|
|
|
sl_pte = sl_table + sl_offset;
|
|
|
|
|
|
|
|
|
|
|
|
if (len == SZ_4K)
|
2011-02-25 02:00:41 +00:00
|
|
|
*sl_pte = (pa & SL_BASE_MASK_SMALL) | SL_AP0 | SL_AP1 | SL_NG |
|
2010-11-16 02:20:08 +00:00
|
|
|
SL_SHARED | SL_TYPE_SMALL | pgprot;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
if (len == SZ_64K) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
*(sl_pte+i) = (pa & SL_BASE_MASK_LARGE) | SL_AP0 |
|
2011-02-25 02:00:41 +00:00
|
|
|
SL_NG | SL_AP1 | SL_SHARED | SL_TYPE_LARGE | pgprot;
|
2010-08-25 01:31:10 +00:00
|
|
|
}
|
|
|
|
|
2010-11-13 03:30:00 +00:00
|
|
|
ret = __flush_iotlb(domain);
|
2010-08-25 01:31:10 +00:00
|
|
|
fail:
|
|
|
|
spin_unlock_irqrestore(&msm_iommu_lock, flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-10 09:32:25 +00:00
|
|
|
static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long va,
|
|
|
|
size_t len)
|
2010-08-25 01:31:10 +00:00
|
|
|
{
|
|
|
|
struct msm_priv *priv;
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned long *fl_table;
|
|
|
|
unsigned long *fl_pte;
|
|
|
|
unsigned long fl_offset;
|
|
|
|
unsigned long *sl_table;
|
|
|
|
unsigned long *sl_pte;
|
|
|
|
unsigned long sl_offset;
|
|
|
|
int i, ret = 0;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&msm_iommu_lock, flags);
|
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
priv = to_msm_priv(domain);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
fl_table = priv->pgtable;
|
|
|
|
|
|
|
|
if (len != SZ_16M && len != SZ_1M &&
|
|
|
|
len != SZ_64K && len != SZ_4K) {
|
|
|
|
pr_debug("Bad length: %d\n", len);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fl_table) {
|
|
|
|
pr_debug("Null page table\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fl_offset = FL_OFFSET(va); /* Upper 12 bits */
|
|
|
|
fl_pte = fl_table + fl_offset; /* int pointers, 4 bytes */
|
|
|
|
|
|
|
|
if (*fl_pte == 0) {
|
|
|
|
pr_debug("First level PTE is 0\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Unmap supersection */
|
|
|
|
if (len == SZ_16M)
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
*(fl_pte+i) = 0;
|
|
|
|
|
|
|
|
if (len == SZ_1M)
|
|
|
|
*fl_pte = 0;
|
|
|
|
|
|
|
|
sl_table = (unsigned long *) __va(((*fl_pte) & FL_BASE_MASK));
|
|
|
|
sl_offset = SL_OFFSET(va);
|
|
|
|
sl_pte = sl_table + sl_offset;
|
|
|
|
|
|
|
|
if (len == SZ_64K) {
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
*(sl_pte+i) = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len == SZ_4K)
|
|
|
|
*sl_pte = 0;
|
|
|
|
|
|
|
|
if (len == SZ_4K || len == SZ_64K) {
|
|
|
|
int used = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_SL_PTE; i++)
|
|
|
|
if (sl_table[i])
|
|
|
|
used = 1;
|
|
|
|
if (!used) {
|
|
|
|
free_page((unsigned long)sl_table);
|
|
|
|
*fl_pte = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 03:30:00 +00:00
|
|
|
ret = __flush_iotlb(domain);
|
2011-09-02 17:32:34 +00:00
|
|
|
|
2010-08-25 01:31:10 +00:00
|
|
|
fail:
|
|
|
|
spin_unlock_irqrestore(&msm_iommu_lock, flags);
|
2011-11-10 09:32:25 +00:00
|
|
|
|
|
|
|
/* the IOMMU API requires us to return how many bytes were unmapped */
|
|
|
|
len = ret ? 0 : len;
|
|
|
|
return len;
|
2010-08-25 01:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static phys_addr_t msm_iommu_iova_to_phys(struct iommu_domain *domain,
|
2013-03-28 19:53:58 +00:00
|
|
|
dma_addr_t va)
|
2010-08-25 01:31:10 +00:00
|
|
|
{
|
|
|
|
struct msm_priv *priv;
|
2016-06-13 11:36:02 +00:00
|
|
|
struct msm_iommu_dev *iommu;
|
|
|
|
struct msm_iommu_ctx_dev *master;
|
2010-08-25 01:31:10 +00:00
|
|
|
unsigned int par;
|
|
|
|
unsigned long flags;
|
|
|
|
phys_addr_t ret = 0;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&msm_iommu_lock, flags);
|
|
|
|
|
2015-03-26 12:43:14 +00:00
|
|
|
priv = to_msm_priv(domain);
|
2016-06-13 11:36:02 +00:00
|
|
|
iommu = list_first_entry(&priv->list_attached,
|
|
|
|
struct msm_iommu_dev, dom_node);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
if (list_empty(&iommu->ctx_list))
|
|
|
|
goto fail;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
master = list_first_entry(&iommu->ctx_list,
|
|
|
|
struct msm_iommu_ctx_dev, list);
|
|
|
|
if (!master)
|
|
|
|
goto fail;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
ret = __enable_clocks(iommu);
|
2011-02-25 02:00:39 +00:00
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
2010-08-25 01:31:10 +00:00
|
|
|
/* Invalidate context TLB */
|
2016-06-13 11:36:02 +00:00
|
|
|
SET_CTX_TLBIALL(iommu->base, master->num, 0);
|
|
|
|
SET_V2PPR(iommu->base, master->num, va & V2Pxx_VA);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
par = GET_PAR(iommu->base, master->num);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
/* We are dealing with a supersection */
|
2016-06-13 11:36:02 +00:00
|
|
|
if (GET_NOFAULT_SS(iommu->base, master->num))
|
2010-08-25 01:31:10 +00:00
|
|
|
ret = (par & 0xFF000000) | (va & 0x00FFFFFF);
|
|
|
|
else /* Upper 20 bits from PAR, lower 12 from VA */
|
|
|
|
ret = (par & 0xFFFFF000) | (va & 0x00000FFF);
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
if (GET_FAULT(iommu->base, master->num))
|
2010-11-13 03:30:00 +00:00
|
|
|
ret = 0;
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
__disable_clocks(iommu);
|
2010-08-25 01:31:10 +00:00
|
|
|
fail:
|
|
|
|
spin_unlock_irqrestore(&msm_iommu_lock, flags);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-09-05 08:51:14 +00:00
|
|
|
static bool msm_iommu_capable(enum iommu_cap cap)
|
2010-08-25 01:31:10 +00:00
|
|
|
{
|
2014-09-05 08:51:14 +00:00
|
|
|
return false;
|
2010-08-25 01:31:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_ctx_regs(void __iomem *base, int ctx)
|
|
|
|
{
|
|
|
|
unsigned int fsr = GET_FSR(base, ctx);
|
|
|
|
pr_err("FAR = %08x PAR = %08x\n",
|
|
|
|
GET_FAR(base, ctx), GET_PAR(base, ctx));
|
|
|
|
pr_err("FSR = %08x [%s%s%s%s%s%s%s%s%s%s]\n", fsr,
|
|
|
|
(fsr & 0x02) ? "TF " : "",
|
|
|
|
(fsr & 0x04) ? "AFF " : "",
|
|
|
|
(fsr & 0x08) ? "APF " : "",
|
|
|
|
(fsr & 0x10) ? "TLBMF " : "",
|
|
|
|
(fsr & 0x20) ? "HTWDEEF " : "",
|
|
|
|
(fsr & 0x40) ? "HTWSEEF " : "",
|
|
|
|
(fsr & 0x80) ? "MHF " : "",
|
|
|
|
(fsr & 0x10000) ? "SL " : "",
|
|
|
|
(fsr & 0x40000000) ? "SS " : "",
|
|
|
|
(fsr & 0x80000000) ? "MULTI " : "");
|
|
|
|
|
|
|
|
pr_err("FSYNR0 = %08x FSYNR1 = %08x\n",
|
|
|
|
GET_FSYNR0(base, ctx), GET_FSYNR1(base, ctx));
|
|
|
|
pr_err("TTBR0 = %08x TTBR1 = %08x\n",
|
|
|
|
GET_TTBR0(base, ctx), GET_TTBR1(base, ctx));
|
|
|
|
pr_err("SCTLR = %08x ACTLR = %08x\n",
|
|
|
|
GET_SCTLR(base, ctx), GET_ACTLR(base, ctx));
|
|
|
|
pr_err("PRRR = %08x NMRR = %08x\n",
|
|
|
|
GET_PRRR(base, ctx), GET_NMRR(base, ctx));
|
|
|
|
}
|
|
|
|
|
|
|
|
irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id)
|
|
|
|
{
|
2016-06-13 11:36:02 +00:00
|
|
|
struct msm_iommu_dev *iommu = dev_id;
|
2010-11-13 03:30:00 +00:00
|
|
|
unsigned int fsr;
|
2011-02-25 02:00:42 +00:00
|
|
|
int i, ret;
|
2010-08-25 01:31:10 +00:00
|
|
|
|
|
|
|
spin_lock(&msm_iommu_lock);
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
if (!iommu) {
|
2010-08-25 01:31:10 +00:00
|
|
|
pr_err("Invalid device ID in context interrupt handler\n");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_err("Unexpected IOMMU page fault!\n");
|
2016-06-13 11:36:02 +00:00
|
|
|
pr_err("base = %08x\n", (unsigned int)iommu->base);
|
2010-08-25 01:31:10 +00:00
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
ret = __enable_clocks(iommu);
|
2011-02-25 02:00:39 +00:00
|
|
|
if (ret)
|
|
|
|
goto fail;
|
|
|
|
|
2016-06-13 11:36:02 +00:00
|
|
|
for (i = 0; i < iommu->ncb; i++) {
|
|
|
|
fsr = GET_FSR(iommu->base, i);
|
2010-08-25 01:31:10 +00:00
|
|
|
if (fsr) {
|
|
|
|
pr_err("Fault occurred in context %d.\n", i);
|
|
|
|
pr_err("Interesting registers:\n");
|
2016-06-13 11:36:02 +00:00
|
|
|
print_ctx_regs(iommu->base, i);
|
|
|
|
SET_FSR(iommu->base, i, 0x4000000F);
|
2010-08-25 01:31:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-13 11:36:02 +00:00
|
|
|
__disable_clocks(iommu);
|
2010-08-25 01:31:10 +00:00
|
|
|
fail:
|
|
|
|
spin_unlock(&msm_iommu_lock);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-27 07:03:12 +00:00
|
|
|
static const struct iommu_ops msm_iommu_ops = {
|
2014-09-05 08:51:14 +00:00
|
|
|
.capable = msm_iommu_capable,
|
2015-03-26 12:43:14 +00:00
|
|
|
.domain_alloc = msm_iommu_domain_alloc,
|
|
|
|
.domain_free = msm_iommu_domain_free,
|
2010-08-25 01:31:10 +00:00
|
|
|
.attach_dev = msm_iommu_attach_dev,
|
|
|
|
.detach_dev = msm_iommu_detach_dev,
|
|
|
|
.map = msm_iommu_map,
|
|
|
|
.unmap = msm_iommu_unmap,
|
2014-10-25 16:55:16 +00:00
|
|
|
.map_sg = default_iommu_map_sg,
|
2010-08-25 01:31:10 +00:00
|
|
|
.iova_to_phys = msm_iommu_iova_to_phys,
|
2011-11-10 09:32:28 +00:00
|
|
|
.pgsize_bitmap = MSM_IOMMU_PGSIZES,
|
2010-08-25 01:31:10 +00:00
|
|
|
};
|
|
|
|
|
2010-11-16 02:20:08 +00:00
|
|
|
static int __init get_tex_class(int icp, int ocp, int mt, int nos)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
unsigned int prrr = 0;
|
|
|
|
unsigned int nmrr = 0;
|
|
|
|
int c_icp, c_ocp, c_mt, c_nos;
|
|
|
|
|
|
|
|
RCP15_PRRR(prrr);
|
|
|
|
RCP15_NMRR(nmrr);
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_TEX_CLASS; i++) {
|
|
|
|
c_nos = PRRR_NOS(prrr, i);
|
|
|
|
c_mt = PRRR_MT(prrr, i);
|
|
|
|
c_icp = NMRR_ICP(nmrr, i);
|
|
|
|
c_ocp = NMRR_OCP(nmrr, i);
|
|
|
|
|
|
|
|
if (icp == c_icp && ocp == c_ocp && c_mt == mt && c_nos == nos)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __init setup_iommu_tex_classes(void)
|
|
|
|
{
|
|
|
|
msm_iommu_tex_class[MSM_IOMMU_ATTR_NONCACHED] =
|
|
|
|
get_tex_class(CP_NONCACHED, CP_NONCACHED, MT_NORMAL, 1);
|
|
|
|
|
|
|
|
msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_WA] =
|
|
|
|
get_tex_class(CP_WB_WA, CP_WB_WA, MT_NORMAL, 1);
|
|
|
|
|
|
|
|
msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WB_NWA] =
|
|
|
|
get_tex_class(CP_WB_NWA, CP_WB_NWA, MT_NORMAL, 1);
|
|
|
|
|
|
|
|
msm_iommu_tex_class[MSM_IOMMU_ATTR_CACHED_WT] =
|
|
|
|
get_tex_class(CP_WT, CP_WT, MT_NORMAL, 1);
|
|
|
|
}
|
|
|
|
|
2010-11-13 03:29:53 +00:00
|
|
|
static int __init msm_iommu_init(void)
|
2010-08-25 01:31:10 +00:00
|
|
|
{
|
2010-11-16 02:20:08 +00:00
|
|
|
setup_iommu_tex_classes();
|
2011-09-06 15:56:07 +00:00
|
|
|
bus_set_iommu(&platform_bus_type, &msm_iommu_ops);
|
2010-08-25 01:31:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
subsys_initcall(msm_iommu_init);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
MODULE_AUTHOR("Stepan Moskovchenko <stepanm@codeaurora.org>");
|