2019-06-04 08:11:33 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-02-18 14:12:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2011,2016 Samsung Electronics Co., Ltd.
|
2012-05-11 20:56:09 +00:00
|
|
|
* http://www.samsung.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_EXYNOS_IOMMU_DEBUG
|
|
|
|
#define DEBUG
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <linux/clk.h>
|
2015-05-19 13:20:36 +00:00
|
|
|
#include <linux/dma-mapping.h>
|
2012-05-11 20:56:09 +00:00
|
|
|
#include <linux/err.h>
|
2015-05-19 13:20:30 +00:00
|
|
|
#include <linux/io.h>
|
2012-05-11 20:56:09 +00:00
|
|
|
#include <linux/iommu.h>
|
2015-05-19 13:20:30 +00:00
|
|
|
#include <linux/interrupt.h>
|
2018-04-05 23:25:34 +00:00
|
|
|
#include <linux/kmemleak.h>
|
2012-05-11 20:56:09 +00:00
|
|
|
#include <linux/list.h>
|
2015-05-19 13:20:36 +00:00
|
|
|
#include <linux/of.h>
|
|
|
|
#include <linux/of_platform.h>
|
2015-05-19 13:20:30 +00:00
|
|
|
#include <linux/platform_device.h>
|
|
|
|
#include <linux/pm_runtime.h>
|
|
|
|
#include <linux/slab.h>
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2024-04-13 00:25:17 +00:00
|
|
|
#include "iommu-pages.h"
|
|
|
|
|
2014-05-12 06:14:58 +00:00
|
|
|
typedef u32 sysmmu_iova_t;
|
|
|
|
typedef u32 sysmmu_pte_t;
|
2023-09-13 13:43:43 +00:00
|
|
|
static struct iommu_domain exynos_identity_domain;
|
2014-05-12 06:14:58 +00:00
|
|
|
|
2014-08-04 04:36:28 +00:00
|
|
|
/* We do not consider super section mapping (16MB) */
|
2012-05-11 20:56:09 +00: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 06:15:04 +00: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-11 20:56:09 +00: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 14:12:58 +00: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
|
|
|
|
|
2016-11-03 08:04:45 +00:00
|
|
|
static const sysmmu_pte_t *LV1_PROT;
|
|
|
|
static const sysmmu_pte_t SYSMMU_LV1_PROT[] = {
|
|
|
|
((0 << 15) | (0 << 10)), /* no access */
|
|
|
|
((1 << 15) | (1 << 10)), /* IOMMU_READ only */
|
|
|
|
((0 << 15) | (1 << 10)), /* IOMMU_WRITE not supported, use read/write */
|
|
|
|
((0 << 15) | (1 << 10)), /* IOMMU_READ | IOMMU_WRITE */
|
|
|
|
};
|
|
|
|
static const sysmmu_pte_t SYSMMU_V5_LV1_PROT[] = {
|
|
|
|
(0 << 4), /* no access */
|
|
|
|
(1 << 4), /* IOMMU_READ only */
|
|
|
|
(2 << 4), /* IOMMU_WRITE only */
|
|
|
|
(3 << 4), /* IOMMU_READ | IOMMU_WRITE */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const sysmmu_pte_t *LV2_PROT;
|
|
|
|
static const sysmmu_pte_t SYSMMU_LV2_PROT[] = {
|
|
|
|
((0 << 9) | (0 << 4)), /* no access */
|
|
|
|
((1 << 9) | (1 << 4)), /* IOMMU_READ only */
|
|
|
|
((0 << 9) | (1 << 4)), /* IOMMU_WRITE not supported, use read/write */
|
|
|
|
((0 << 9) | (1 << 4)), /* IOMMU_READ | IOMMU_WRITE */
|
|
|
|
};
|
|
|
|
static const sysmmu_pte_t SYSMMU_V5_LV2_PROT[] = {
|
|
|
|
(0 << 2), /* no access */
|
|
|
|
(1 << 2), /* IOMMU_READ only */
|
|
|
|
(2 << 2), /* IOMMU_WRITE only */
|
|
|
|
(3 << 2), /* IOMMU_READ | IOMMU_WRITE */
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SYSMMU_SUPPORTED_PROT_BITS (IOMMU_READ | IOMMU_WRITE)
|
|
|
|
|
2016-02-18 14:12:58 +00:00
|
|
|
#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-11 20:56:09 +00:00
|
|
|
|
|
|
|
#define NUM_LV1ENTRIES 4096
|
2014-05-12 06:14:58 +00:00
|
|
|
#define NUM_LV2ENTRIES (SECT_SIZE / SPAGE_SIZE)
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:14:58 +00: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 14:12:50 +00:00
|
|
|
#define LV1TABLE_SIZE (NUM_LV1ENTRIES * sizeof(sysmmu_pte_t))
|
2014-05-12 06:14:58 +00:00
|
|
|
#define LV2TABLE_SIZE (NUM_LV2ENTRIES * sizeof(sysmmu_pte_t))
|
2012-05-11 20:56:09 +00:00
|
|
|
|
|
|
|
#define SPAGES_PER_LPAGE (LPAGE_SIZE / SPAGE_SIZE)
|
2016-02-18 14:12:58 +00:00
|
|
|
#define lv2table_base(sent) (sect_to_phys(*(sent) & 0xFFFFFFC0))
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2016-11-03 08:04:45 +00:00
|
|
|
#define mk_lv1ent_sect(pa, prot) ((pa >> PG_ENT_SHIFT) | LV1_PROT[prot] | 2)
|
2016-02-18 14:12:58 +00:00
|
|
|
#define mk_lv1ent_page(pa) ((pa >> PG_ENT_SHIFT) | 1)
|
2016-11-03 08:04:45 +00:00
|
|
|
#define mk_lv2ent_lpage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 1)
|
|
|
|
#define mk_lv2ent_spage(pa, prot) ((pa >> PG_ENT_SHIFT) | LV2_PROT[prot] | 2)
|
2012-05-11 20:56:09 +00:00
|
|
|
|
|
|
|
#define CTRL_ENABLE 0x5
|
|
|
|
#define CTRL_BLOCK 0x7
|
|
|
|
#define CTRL_DISABLE 0x0
|
|
|
|
|
2014-05-12 06:15:03 +00:00
|
|
|
#define CFG_LRU 0x1
|
2016-11-03 08:04:45 +00:00
|
|
|
#define CFG_EAP (1 << 2)
|
2014-05-12 06:15:03 +00:00
|
|
|
#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 */
|
|
|
|
|
2022-07-14 16:55:50 +00:00
|
|
|
#define CTRL_VM_ENABLE BIT(0)
|
|
|
|
#define CTRL_VM_FAULT_MODE_STALL BIT(3)
|
2022-07-14 16:55:49 +00:00
|
|
|
#define CAPA0_CAPA1_EXIST BIT(11)
|
|
|
|
#define CAPA1_VCR_ENABLED BIT(14)
|
|
|
|
|
2016-02-18 14:12:58 +00:00
|
|
|
/* common registers */
|
2012-05-11 20:56:09 +00:00
|
|
|
#define REG_MMU_CTRL 0x000
|
|
|
|
#define REG_MMU_CFG 0x004
|
|
|
|
#define REG_MMU_STATUS 0x008
|
2016-02-18 14:12:58 +00: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-11 20:56:09 +00:00
|
|
|
#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 14:12:58 +00:00
|
|
|
/* v5.x registers */
|
|
|
|
#define REG_V5_FAULT_AR_VA 0x070
|
|
|
|
#define REG_V5_FAULT_AW_VA 0x080
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2022-07-14 16:55:49 +00:00
|
|
|
/* v7.x registers */
|
|
|
|
#define REG_V7_CAPA0 0x870
|
|
|
|
#define REG_V7_CAPA1 0x874
|
2022-07-14 16:55:50 +00:00
|
|
|
#define REG_V7_CTRL_VM 0x8000
|
2022-07-14 16:55:49 +00:00
|
|
|
|
2020-06-25 13:08:24 +00:00
|
|
|
#define has_sysmmu(dev) (dev_iommu_priv_get(dev) != NULL)
|
2014-05-12 06:15:02 +00:00
|
|
|
|
2016-02-18 14:12:50 +00:00
|
|
|
static struct device *dma_dev;
|
2014-05-12 06:14:48 +00:00
|
|
|
static struct kmem_cache *lv2table_kmem_cache;
|
2014-05-12 06:15:04 +00:00
|
|
|
static sysmmu_pte_t *zero_lv2_table;
|
|
|
|
#define ZERO_LV2LINK mk_lv1ent_page(virt_to_phys(zero_lv2_table))
|
2014-05-12 06:14:48 +00:00
|
|
|
|
2014-05-12 06:14:58 +00:00
|
|
|
static sysmmu_pte_t *section_entry(sysmmu_pte_t *pgtable, sysmmu_iova_t iova)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
|
|
|
return pgtable + lv1ent_offset(iova);
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:14:58 +00:00
|
|
|
static sysmmu_pte_t *page_entry(sysmmu_pte_t *sent, sysmmu_iova_t iova)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2014-05-12 06:14:58 +00:00
|
|
|
return (sysmmu_pte_t *)phys_to_virt(
|
2014-05-12 06:14:46 +00:00
|
|
|
lv2table_base(sent)) + lv2ent_offset(iova);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2022-07-26 20:07:38 +00:00
|
|
|
struct sysmmu_fault {
|
|
|
|
sysmmu_iova_t addr; /* IOVA address that caused fault */
|
|
|
|
const char *name; /* human readable fault name */
|
|
|
|
unsigned int type; /* fault type for report_iommu_fault() */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct sysmmu_v1_fault_info {
|
|
|
|
unsigned short addr_reg; /* register to read IOVA fault address */
|
2016-02-18 14:12:53 +00:00
|
|
|
const char *name; /* human readable fault name */
|
|
|
|
unsigned int type; /* fault type for report_iommu_fault */
|
2012-05-11 20:56:09 +00:00
|
|
|
};
|
|
|
|
|
2022-07-26 20:07:38 +00:00
|
|
|
static const struct sysmmu_v1_fault_info sysmmu_v1_faults[] = {
|
|
|
|
{ REG_PAGE_FAULT_ADDR, "PAGE", IOMMU_FAULT_READ },
|
|
|
|
{ REG_AR_FAULT_ADDR, "MULTI-HIT", IOMMU_FAULT_READ },
|
|
|
|
{ REG_AW_FAULT_ADDR, "MULTI-HIT", IOMMU_FAULT_WRITE },
|
|
|
|
{ REG_DEFAULT_SLAVE_ADDR, "BUS ERROR", IOMMU_FAULT_READ },
|
|
|
|
{ REG_AR_FAULT_ADDR, "SECURITY PROTECTION", IOMMU_FAULT_READ },
|
|
|
|
{ REG_AR_FAULT_ADDR, "ACCESS PROTECTION", IOMMU_FAULT_READ },
|
|
|
|
{ REG_AW_FAULT_ADDR, "SECURITY PROTECTION", IOMMU_FAULT_WRITE },
|
|
|
|
{ REG_AW_FAULT_ADDR, "ACCESS PROTECTION", IOMMU_FAULT_WRITE },
|
2012-05-11 20:56:09 +00:00
|
|
|
};
|
|
|
|
|
2022-07-26 20:07:38 +00:00
|
|
|
/* SysMMU v5 has the same faults for AR (0..4 bits) and AW (16..20 bits) */
|
|
|
|
static const char * const sysmmu_v5_fault_names[] = {
|
|
|
|
"PTW",
|
|
|
|
"PAGE",
|
|
|
|
"MULTI-HIT",
|
|
|
|
"ACCESS PROTECTION",
|
|
|
|
"SECURITY PROTECTION"
|
2012-05-11 20:56:09 +00:00
|
|
|
};
|
|
|
|
|
2022-07-26 20:07:39 +00:00
|
|
|
static const char * const sysmmu_v7_fault_names[] = {
|
|
|
|
"PTW",
|
|
|
|
"PAGE",
|
|
|
|
"ACCESS PROTECTION",
|
|
|
|
"RESERVED"
|
2016-02-18 14:12:58 +00:00
|
|
|
};
|
|
|
|
|
2015-05-19 13:20:31 +00:00
|
|
|
/*
|
2020-06-25 13:08:24 +00:00
|
|
|
* This structure is attached to dev->iommu->priv of the master device
|
2015-05-19 13:20:31 +00:00
|
|
|
* 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 06:15:02 +00:00
|
|
|
struct exynos_iommu_owner {
|
2015-05-19 13:20:33 +00:00
|
|
|
struct list_head controllers; /* list of sysmmu_drvdata.owner_node */
|
2016-02-18 14:13:00 +00:00
|
|
|
struct iommu_domain *domain; /* domain this device is attached */
|
2016-11-14 10:08:11 +00:00
|
|
|
struct mutex rpm_lock; /* for runtime pm of all sysmmus */
|
2014-05-12 06:15:02 +00:00
|
|
|
};
|
|
|
|
|
2015-05-19 13:20:31 +00: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-11 20:56:09 +00:00
|
|
|
struct exynos_iommu_domain {
|
2015-05-19 13:20:31 +00: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 12:43:11 +00:00
|
|
|
struct iommu_domain domain; /* generic domain data structure */
|
2012-05-11 20:56:09 +00:00
|
|
|
};
|
|
|
|
|
2022-07-26 20:07:38 +00:00
|
|
|
struct sysmmu_drvdata;
|
|
|
|
|
2022-07-14 16:55:48 +00:00
|
|
|
/*
|
|
|
|
* SysMMU version specific data. Contains offsets for the registers which can
|
|
|
|
* be found in different SysMMU variants, but have different offset values.
|
2022-07-26 20:07:38 +00:00
|
|
|
* Also contains version specific callbacks to abstract the hardware.
|
2022-07-14 16:55:48 +00:00
|
|
|
*/
|
|
|
|
struct sysmmu_variant {
|
|
|
|
u32 pt_base; /* page table base address (physical) */
|
|
|
|
u32 flush_all; /* invalidate all TLB entries */
|
|
|
|
u32 flush_entry; /* invalidate specific TLB entry */
|
|
|
|
u32 flush_range; /* invalidate TLB entries in specified range */
|
|
|
|
u32 flush_start; /* start address of range invalidation */
|
|
|
|
u32 flush_end; /* end address of range invalidation */
|
|
|
|
u32 int_status; /* interrupt status information */
|
|
|
|
u32 int_clear; /* clear the interrupt */
|
2022-07-26 20:07:39 +00:00
|
|
|
u32 fault_va; /* IOVA address that caused fault */
|
|
|
|
u32 fault_info; /* fault transaction info */
|
2022-07-26 20:07:38 +00:00
|
|
|
|
|
|
|
int (*get_fault_info)(struct sysmmu_drvdata *data, unsigned int itype,
|
|
|
|
struct sysmmu_fault *fault);
|
2022-07-14 16:55:48 +00:00
|
|
|
};
|
|
|
|
|
2015-05-19 13:20:31 +00: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-11 20:56:09 +00:00
|
|
|
struct sysmmu_drvdata {
|
2015-05-19 13:20:31 +00:00
|
|
|
struct device *sysmmu; /* SYSMMU controller device */
|
|
|
|
struct device *master; /* master device (owner) */
|
2017-09-15 11:05:08 +00:00
|
|
|
struct device_link *link; /* runtime PM link to master */
|
2015-05-19 13:20:31 +00:00
|
|
|
void __iomem *sfrbase; /* our registers */
|
|
|
|
struct clk *clk; /* SYSMMU's clock */
|
2016-02-18 14:12:58 +00:00
|
|
|
struct clk *aclk; /* SYSMMU's aclk clock */
|
|
|
|
struct clk *pclk; /* SYSMMU's pclk clock */
|
2015-05-19 13:20:31 +00:00
|
|
|
struct clk *clk_master; /* master's device clock */
|
|
|
|
spinlock_t lock; /* lock for modyfying state */
|
2016-11-14 10:08:08 +00:00
|
|
|
bool active; /* current status */
|
2015-05-19 13:20:31 +00:00
|
|
|
struct exynos_iommu_domain *domain; /* domain we belong to */
|
|
|
|
struct list_head domain_node; /* node for domain clients list */
|
2015-05-19 13:20:33 +00:00
|
|
|
struct list_head owner_node; /* node for owner controllers list */
|
2015-05-19 13:20:31 +00:00
|
|
|
phys_addr_t pgtable; /* assigned page table structure */
|
|
|
|
unsigned int version; /* our version */
|
2017-02-03 12:23:42 +00:00
|
|
|
|
|
|
|
struct iommu_device iommu; /* IOMMU core handle */
|
2022-07-14 16:55:48 +00:00
|
|
|
const struct sysmmu_variant *variant; /* version specific data */
|
2022-07-14 16:55:49 +00:00
|
|
|
|
|
|
|
/* v7 fields */
|
|
|
|
bool has_vcr; /* virtual machine control register */
|
2022-07-14 16:55:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define SYSMMU_REG(data, reg) ((data)->sfrbase + (data)->variant->reg)
|
|
|
|
|
2022-07-26 20:07:38 +00:00
|
|
|
static int exynos_sysmmu_v1_get_fault_info(struct sysmmu_drvdata *data,
|
|
|
|
unsigned int itype,
|
|
|
|
struct sysmmu_fault *fault)
|
|
|
|
{
|
|
|
|
const struct sysmmu_v1_fault_info *finfo;
|
|
|
|
|
|
|
|
if (itype >= ARRAY_SIZE(sysmmu_v1_faults))
|
|
|
|
return -ENXIO;
|
|
|
|
|
|
|
|
finfo = &sysmmu_v1_faults[itype];
|
|
|
|
fault->addr = readl(data->sfrbase + finfo->addr_reg);
|
|
|
|
fault->name = finfo->name;
|
|
|
|
fault->type = finfo->type;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int exynos_sysmmu_v5_get_fault_info(struct sysmmu_drvdata *data,
|
|
|
|
unsigned int itype,
|
|
|
|
struct sysmmu_fault *fault)
|
|
|
|
{
|
|
|
|
unsigned int addr_reg;
|
|
|
|
|
|
|
|
if (itype < ARRAY_SIZE(sysmmu_v5_fault_names)) {
|
|
|
|
fault->type = IOMMU_FAULT_READ;
|
|
|
|
addr_reg = REG_V5_FAULT_AR_VA;
|
|
|
|
} else if (itype >= 16 && itype <= 20) {
|
|
|
|
fault->type = IOMMU_FAULT_WRITE;
|
|
|
|
addr_reg = REG_V5_FAULT_AW_VA;
|
|
|
|
itype -= 16;
|
|
|
|
} else {
|
|
|
|
return -ENXIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
fault->name = sysmmu_v5_fault_names[itype];
|
|
|
|
fault->addr = readl(data->sfrbase + addr_reg);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-26 20:07:39 +00:00
|
|
|
static int exynos_sysmmu_v7_get_fault_info(struct sysmmu_drvdata *data,
|
|
|
|
unsigned int itype,
|
|
|
|
struct sysmmu_fault *fault)
|
|
|
|
{
|
|
|
|
u32 info = readl(SYSMMU_REG(data, fault_info));
|
|
|
|
|
|
|
|
fault->addr = readl(SYSMMU_REG(data, fault_va));
|
|
|
|
fault->name = sysmmu_v7_fault_names[itype % 4];
|
|
|
|
fault->type = (info & BIT(20)) ? IOMMU_FAULT_WRITE : IOMMU_FAULT_READ;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:55:48 +00:00
|
|
|
/* SysMMU v1..v3 */
|
|
|
|
static const struct sysmmu_variant sysmmu_v1_variant = {
|
|
|
|
.flush_all = 0x0c,
|
|
|
|
.flush_entry = 0x10,
|
|
|
|
.pt_base = 0x14,
|
|
|
|
.int_status = 0x18,
|
|
|
|
.int_clear = 0x1c,
|
2022-07-26 20:07:38 +00:00
|
|
|
|
|
|
|
.get_fault_info = exynos_sysmmu_v1_get_fault_info,
|
2022-07-14 16:55:48 +00:00
|
|
|
};
|
|
|
|
|
2022-07-26 20:07:39 +00:00
|
|
|
/* SysMMU v5 */
|
2022-07-14 16:55:48 +00:00
|
|
|
static const struct sysmmu_variant sysmmu_v5_variant = {
|
|
|
|
.pt_base = 0x0c,
|
|
|
|
.flush_all = 0x10,
|
|
|
|
.flush_entry = 0x14,
|
|
|
|
.flush_range = 0x18,
|
|
|
|
.flush_start = 0x20,
|
|
|
|
.flush_end = 0x24,
|
|
|
|
.int_status = 0x60,
|
|
|
|
.int_clear = 0x64,
|
2022-07-26 20:07:38 +00:00
|
|
|
|
|
|
|
.get_fault_info = exynos_sysmmu_v5_get_fault_info,
|
2012-05-11 20:56:09 +00:00
|
|
|
};
|
|
|
|
|
2022-07-26 20:07:39 +00:00
|
|
|
/* SysMMU v7: non-VM capable register layout */
|
|
|
|
static const struct sysmmu_variant sysmmu_v7_variant = {
|
|
|
|
.pt_base = 0x0c,
|
|
|
|
.flush_all = 0x10,
|
|
|
|
.flush_entry = 0x14,
|
|
|
|
.flush_range = 0x18,
|
|
|
|
.flush_start = 0x20,
|
|
|
|
.flush_end = 0x24,
|
|
|
|
.int_status = 0x60,
|
|
|
|
.int_clear = 0x64,
|
|
|
|
.fault_va = 0x70,
|
|
|
|
.fault_info = 0x78,
|
|
|
|
|
|
|
|
.get_fault_info = exynos_sysmmu_v7_get_fault_info,
|
|
|
|
};
|
|
|
|
|
|
|
|
/* SysMMU v7: VM capable register layout */
|
2022-07-14 16:55:49 +00:00
|
|
|
static const struct sysmmu_variant sysmmu_v7_vm_variant = {
|
|
|
|
.pt_base = 0x800c,
|
|
|
|
.flush_all = 0x8010,
|
|
|
|
.flush_entry = 0x8014,
|
|
|
|
.flush_range = 0x8018,
|
|
|
|
.flush_start = 0x8020,
|
|
|
|
.flush_end = 0x8024,
|
|
|
|
.int_status = 0x60,
|
|
|
|
.int_clear = 0x64,
|
2022-07-26 20:07:39 +00:00
|
|
|
.fault_va = 0x1000,
|
|
|
|
.fault_info = 0x1004,
|
2022-07-26 20:07:38 +00:00
|
|
|
|
2022-07-26 20:07:39 +00:00
|
|
|
.get_fault_info = exynos_sysmmu_v7_get_fault_info,
|
2012-05-11 20:56:09 +00:00
|
|
|
};
|
|
|
|
|
2015-03-26 12:43:11 +00:00
|
|
|
static struct exynos_iommu_domain *to_exynos_domain(struct iommu_domain *dom)
|
|
|
|
{
|
|
|
|
return container_of(dom, struct exynos_iommu_domain, domain);
|
|
|
|
}
|
|
|
|
|
2016-02-18 14:12:52 +00:00
|
|
|
static void sysmmu_unblock(struct sysmmu_drvdata *data)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2016-02-29 12:42:57 +00:00
|
|
|
writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 14:12:52 +00:00
|
|
|
static bool sysmmu_block(struct sysmmu_drvdata *data)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
|
|
|
int i = 120;
|
|
|
|
|
2016-02-29 12:42:57 +00:00
|
|
|
writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
|
|
|
|
while ((i > 0) && !(readl(data->sfrbase + REG_MMU_STATUS) & 1))
|
2012-05-11 20:56:09 +00:00
|
|
|
--i;
|
|
|
|
|
2016-02-29 12:42:57 +00:00
|
|
|
if (!(readl(data->sfrbase + REG_MMU_STATUS) & 1)) {
|
2016-02-18 14:12:52 +00:00
|
|
|
sysmmu_unblock(data);
|
2012-05-11 20:56:09 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-18 14:12:52 +00:00
|
|
|
static void __sysmmu_tlb_invalidate(struct sysmmu_drvdata *data)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2022-07-14 16:55:48 +00:00
|
|
|
writel(0x1, SYSMMU_REG(data, flush_all));
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 14:12:52 +00:00
|
|
|
static void __sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
|
2014-05-12 06:14:58 +00:00
|
|
|
sysmmu_iova_t iova, unsigned int num_inv)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2014-05-12 06:14:49 +00:00
|
|
|
unsigned int i;
|
2014-05-22 04:20:56 +00:00
|
|
|
|
2022-07-14 16:55:48 +00:00
|
|
|
if (MMU_MAJ_VER(data->version) < 5 || num_inv == 1) {
|
2017-03-24 09:19:01 +00:00
|
|
|
for (i = 0; i < num_inv; i++) {
|
2016-02-29 12:42:57 +00:00
|
|
|
writel((iova & SPAGE_MASK) | 1,
|
2022-07-14 16:55:48 +00:00
|
|
|
SYSMMU_REG(data, flush_entry));
|
2017-03-24 09:19:01 +00:00
|
|
|
iova += SPAGE_SIZE;
|
|
|
|
}
|
|
|
|
} else {
|
2022-07-14 16:55:48 +00:00
|
|
|
writel(iova & SPAGE_MASK, SYSMMU_REG(data, flush_start));
|
|
|
|
writel((iova & SPAGE_MASK) + (num_inv - 1) * SPAGE_SIZE,
|
|
|
|
SYSMMU_REG(data, flush_end));
|
|
|
|
writel(0x1, SYSMMU_REG(data, flush_range));
|
2014-05-12 06:14:49 +00:00
|
|
|
}
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 14:12:52 +00:00
|
|
|
static void __sysmmu_set_ptbase(struct sysmmu_drvdata *data, phys_addr_t pgd)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2022-07-14 16:55:48 +00:00
|
|
|
u32 pt_base;
|
|
|
|
|
2016-02-18 14:12:58 +00:00
|
|
|
if (MMU_MAJ_VER(data->version) < 5)
|
2022-07-14 16:55:48 +00:00
|
|
|
pt_base = pgd;
|
2016-02-18 14:12:58 +00:00
|
|
|
else
|
2022-07-14 16:55:48 +00:00
|
|
|
pt_base = pgd >> SPAGE_ORDER;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2022-07-14 16:55:48 +00:00
|
|
|
writel(pt_base, SYSMMU_REG(data, pt_base));
|
2016-02-18 14:12:52 +00:00
|
|
|
__sysmmu_tlb_invalidate(data);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 09:30:09 +00:00
|
|
|
static void __sysmmu_enable_clocks(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
BUG_ON(clk_prepare_enable(data->clk_master));
|
|
|
|
BUG_ON(clk_prepare_enable(data->clk));
|
|
|
|
BUG_ON(clk_prepare_enable(data->pclk));
|
|
|
|
BUG_ON(clk_prepare_enable(data->aclk));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __sysmmu_disable_clocks(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
clk_disable_unprepare(data->aclk);
|
|
|
|
clk_disable_unprepare(data->pclk);
|
|
|
|
clk_disable_unprepare(data->clk);
|
|
|
|
clk_disable_unprepare(data->clk_master);
|
|
|
|
}
|
|
|
|
|
2022-07-14 16:55:49 +00:00
|
|
|
static bool __sysmmu_has_capa1(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
u32 capa0 = readl(data->sfrbase + REG_V7_CAPA0);
|
|
|
|
|
|
|
|
return capa0 & CAPA0_CAPA1_EXIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __sysmmu_get_vcr(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
u32 capa1 = readl(data->sfrbase + REG_V7_CAPA1);
|
|
|
|
|
|
|
|
data->has_vcr = capa1 & CAPA1_VCR_ENABLED;
|
|
|
|
}
|
|
|
|
|
2016-02-18 14:12:56 +00:00
|
|
|
static void __sysmmu_get_version(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
u32 ver;
|
|
|
|
|
2016-05-23 09:30:09 +00:00
|
|
|
__sysmmu_enable_clocks(data);
|
2016-02-18 14:12:56 +00:00
|
|
|
|
2016-02-29 12:42:57 +00:00
|
|
|
ver = readl(data->sfrbase + REG_MMU_VERSION);
|
2016-02-18 14:12:56 +00: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));
|
|
|
|
|
2022-07-14 16:55:49 +00:00
|
|
|
if (MMU_MAJ_VER(data->version) < 5) {
|
2022-07-14 16:55:48 +00:00
|
|
|
data->variant = &sysmmu_v1_variant;
|
2022-07-14 16:55:49 +00:00
|
|
|
} else if (MMU_MAJ_VER(data->version) < 7) {
|
2022-07-14 16:55:48 +00:00
|
|
|
data->variant = &sysmmu_v5_variant;
|
2022-07-14 16:55:49 +00:00
|
|
|
} else {
|
|
|
|
if (__sysmmu_has_capa1(data))
|
|
|
|
__sysmmu_get_vcr(data);
|
|
|
|
if (data->has_vcr)
|
|
|
|
data->variant = &sysmmu_v7_vm_variant;
|
|
|
|
else
|
2022-07-26 20:07:39 +00:00
|
|
|
data->variant = &sysmmu_v7_variant;
|
2022-07-14 16:55:49 +00:00
|
|
|
}
|
2022-07-14 16:55:48 +00:00
|
|
|
|
2016-05-23 09:30:09 +00:00
|
|
|
__sysmmu_disable_clocks(data);
|
2016-02-18 14:12:56 +00:00
|
|
|
}
|
|
|
|
|
2016-02-18 14:12:53 +00:00
|
|
|
static void show_fault_information(struct sysmmu_drvdata *data,
|
2022-07-26 20:07:38 +00:00
|
|
|
const struct sysmmu_fault *fault)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2014-05-12 06:14:58 +00:00
|
|
|
sysmmu_pte_t *ent;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2022-07-26 20:07:38 +00:00
|
|
|
dev_err(data->sysmmu, "%s: [%s] %s FAULT occurred at %#x\n",
|
|
|
|
dev_name(data->master),
|
|
|
|
fault->type == IOMMU_FAULT_READ ? "READ" : "WRITE",
|
|
|
|
fault->name, fault->addr);
|
2017-01-09 12:03:53 +00:00
|
|
|
dev_dbg(data->sysmmu, "Page table base: %pa\n", &data->pgtable);
|
2022-07-26 20:07:38 +00:00
|
|
|
ent = section_entry(phys_to_virt(data->pgtable), fault->addr);
|
2017-01-09 12:03:53 +00:00
|
|
|
dev_dbg(data->sysmmu, "\tLv1 entry: %#x\n", *ent);
|
2012-05-11 20:56:09 +00:00
|
|
|
if (lv1ent_page(ent)) {
|
2022-07-26 20:07:38 +00:00
|
|
|
ent = page_entry(ent, fault->addr);
|
2017-01-09 12:03:53 +00:00
|
|
|
dev_dbg(data->sysmmu, "\t Lv2 entry: %#x\n", *ent);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static irqreturn_t exynos_sysmmu_irq(int irq, void *dev_id)
|
|
|
|
{
|
|
|
|
struct sysmmu_drvdata *data = dev_id;
|
2022-07-26 20:07:38 +00:00
|
|
|
unsigned int itype;
|
|
|
|
struct sysmmu_fault fault;
|
2014-05-12 06:14:46 +00:00
|
|
|
int ret = -ENOSYS;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2016-11-14 10:08:08 +00:00
|
|
|
WARN_ON(!data->active);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:14:57 +00:00
|
|
|
spin_lock(&data->lock);
|
2016-02-18 14:12:51 +00:00
|
|
|
clk_enable(data->clk_master);
|
2014-05-12 06:14:57 +00:00
|
|
|
|
2022-07-14 16:55:48 +00:00
|
|
|
itype = __ffs(readl(SYSMMU_REG(data, int_status)));
|
2022-07-26 20:07:38 +00:00
|
|
|
ret = data->variant->get_fault_info(data, itype, &fault);
|
|
|
|
if (ret) {
|
|
|
|
dev_err(data->sysmmu, "Unhandled interrupt bit %u\n", itype);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
show_fault_information(data, &fault);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2022-07-26 20:07:38 +00:00
|
|
|
if (data->domain) {
|
|
|
|
ret = report_iommu_fault(&data->domain->domain, data->master,
|
|
|
|
fault.addr, fault.type);
|
|
|
|
}
|
|
|
|
if (ret)
|
|
|
|
panic("Unrecoverable System MMU Fault!");
|
|
|
|
|
|
|
|
out:
|
2022-07-14 16:55:48 +00:00
|
|
|
writel(1 << itype, SYSMMU_REG(data, int_clear));
|
2014-05-12 06:14:56 +00:00
|
|
|
|
2022-07-26 20:07:38 +00:00
|
|
|
/* SysMMU is in blocked state when interrupt occurred */
|
2016-02-18 14:12:52 +00:00
|
|
|
sysmmu_unblock(data);
|
2016-02-18 14:12:51 +00:00
|
|
|
clk_disable(data->clk_master);
|
2014-05-12 06:14:57 +00:00
|
|
|
spin_unlock(&data->lock);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
|
|
|
return IRQ_HANDLED;
|
|
|
|
}
|
|
|
|
|
2016-11-14 10:08:08 +00:00
|
|
|
static void __sysmmu_disable(struct sysmmu_drvdata *data)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2016-11-14 10:08:08 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
2016-02-18 14:12:51 +00:00
|
|
|
clk_enable(data->clk_master);
|
2014-05-12 06:14:55 +00:00
|
|
|
|
2016-11-14 10:08:08 +00:00
|
|
|
spin_lock_irqsave(&data->lock, flags);
|
2016-02-29 12:42:57 +00:00
|
|
|
writel(CTRL_DISABLE, data->sfrbase + REG_MMU_CTRL);
|
|
|
|
writel(0, data->sfrbase + REG_MMU_CFG);
|
2016-11-14 10:08:08 +00:00
|
|
|
data->active = false;
|
2014-05-12 06:15:02 +00:00
|
|
|
spin_unlock_irqrestore(&data->lock, flags);
|
|
|
|
|
2016-11-14 10:08:08 +00:00
|
|
|
__sysmmu_disable_clocks(data);
|
2014-05-12 06:15:02 +00:00
|
|
|
}
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:15:02 +00:00
|
|
|
static void __sysmmu_init_config(struct sysmmu_drvdata *data)
|
|
|
|
{
|
2016-02-18 14:12:54 +00: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 06:15:02 +00:00
|
|
|
|
2016-11-03 08:04:45 +00:00
|
|
|
cfg |= CFG_EAP; /* enable access protection bits check */
|
|
|
|
|
2016-02-29 12:42:57 +00:00
|
|
|
writel(cfg, data->sfrbase + REG_MMU_CFG);
|
2014-05-12 06:15:02 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 16:55:50 +00:00
|
|
|
static void __sysmmu_enable_vid(struct sysmmu_drvdata *data)
|
|
|
|
{
|
|
|
|
u32 ctrl;
|
|
|
|
|
|
|
|
if (MMU_MAJ_VER(data->version) < 7 || !data->has_vcr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ctrl = readl(data->sfrbase + REG_V7_CTRL_VM);
|
|
|
|
ctrl |= CTRL_VM_ENABLE | CTRL_VM_FAULT_MODE_STALL;
|
|
|
|
writel(ctrl, data->sfrbase + REG_V7_CTRL_VM);
|
|
|
|
}
|
|
|
|
|
2016-11-14 10:08:08 +00:00
|
|
|
static void __sysmmu_enable(struct sysmmu_drvdata *data)
|
2014-05-12 06:15:02 +00:00
|
|
|
{
|
2016-11-14 10:08:08 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
2016-05-23 09:30:09 +00:00
|
|
|
__sysmmu_enable_clocks(data);
|
2014-05-12 06:14:55 +00:00
|
|
|
|
2016-11-14 10:08:08 +00:00
|
|
|
spin_lock_irqsave(&data->lock, flags);
|
2016-02-29 12:42:57 +00:00
|
|
|
writel(CTRL_BLOCK, data->sfrbase + REG_MMU_CTRL);
|
2014-05-12 06:15:02 +00:00
|
|
|
__sysmmu_init_config(data);
|
2016-02-18 14:12:52 +00:00
|
|
|
__sysmmu_set_ptbase(data, data->pgtable);
|
2022-07-14 16:55:50 +00:00
|
|
|
__sysmmu_enable_vid(data);
|
2016-02-29 12:42:57 +00:00
|
|
|
writel(CTRL_ENABLE, data->sfrbase + REG_MMU_CTRL);
|
2016-11-14 10:08:08 +00:00
|
|
|
data->active = true;
|
|
|
|
spin_unlock_irqrestore(&data->lock, flags);
|
2014-05-12 06:14:46 +00:00
|
|
|
|
2016-05-23 09:30:09 +00:00
|
|
|
/*
|
|
|
|
* SYSMMU driver keeps master's clock enabled only for the short
|
|
|
|
* time, while accessing the registers. For performing address
|
|
|
|
* translation during DMA transaction it relies on the client
|
|
|
|
* driver to enable it.
|
|
|
|
*/
|
2016-02-18 14:12:51 +00:00
|
|
|
clk_disable(data->clk_master);
|
2014-05-12 06:15:02 +00:00
|
|
|
}
|
2014-05-12 06:14:55 +00:00
|
|
|
|
2015-05-19 13:20:27 +00:00
|
|
|
static void sysmmu_tlb_invalidate_flpdcache(struct sysmmu_drvdata *data,
|
2014-05-12 06:15:04 +00:00
|
|
|
sysmmu_iova_t iova)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
|
|
|
spin_lock_irqsave(&data->lock, flags);
|
2016-11-14 10:08:08 +00:00
|
|
|
if (data->active && data->version >= MAKE_MMU_VER(3, 3)) {
|
2016-05-23 09:30:08 +00:00
|
|
|
clk_enable(data->clk_master);
|
2017-03-20 09:17:56 +00:00
|
|
|
if (sysmmu_block(data)) {
|
2017-03-20 09:17:57 +00:00
|
|
|
if (data->version >= MAKE_MMU_VER(5, 0))
|
|
|
|
__sysmmu_tlb_invalidate(data);
|
|
|
|
else
|
|
|
|
__sysmmu_tlb_invalidate_entry(data, iova, 1);
|
2017-03-20 09:17:56 +00:00
|
|
|
sysmmu_unblock(data);
|
|
|
|
}
|
2016-05-23 09:30:08 +00:00
|
|
|
clk_disable(data->clk_master);
|
2016-02-18 14:12:55 +00:00
|
|
|
}
|
2014-05-12 06:15:04 +00:00
|
|
|
spin_unlock_irqrestore(&data->lock, flags);
|
|
|
|
}
|
|
|
|
|
2015-05-19 13:20:27 +00:00
|
|
|
static void sysmmu_tlb_invalidate_entry(struct sysmmu_drvdata *data,
|
|
|
|
sysmmu_iova_t iova, size_t size)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
|
|
|
|
2014-05-12 06:15:02 +00:00
|
|
|
spin_lock_irqsave(&data->lock, flags);
|
2016-11-14 10:08:08 +00:00
|
|
|
if (data->active) {
|
2014-05-12 06:14:49 +00:00
|
|
|
unsigned int num_inv = 1;
|
2014-05-12 06:14:55 +00:00
|
|
|
|
2016-02-18 14:12:51 +00:00
|
|
|
clk_enable(data->clk_master);
|
2014-05-12 06:14:55 +00:00
|
|
|
|
2014-05-12 06:14:49 +00:00
|
|
|
/*
|
|
|
|
* L2TLB invalidation required
|
|
|
|
* 4KB page: 1 invalidation
|
2014-08-04 04:36:28 +00:00
|
|
|
* 64KB page: 16 invalidations
|
|
|
|
* 1MB page: 64 invalidations
|
2014-05-12 06:14:49 +00: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 13:20:24 +00:00
|
|
|
if (MMU_MAJ_VER(data->version) == 2)
|
2022-07-14 16:55:45 +00:00
|
|
|
num_inv = min_t(unsigned int, size / SPAGE_SIZE, 64);
|
2014-05-12 06:14:49 +00:00
|
|
|
|
2016-02-18 14:12:52 +00:00
|
|
|
if (sysmmu_block(data)) {
|
|
|
|
__sysmmu_tlb_invalidate_entry(data, iova, num_inv);
|
|
|
|
sysmmu_unblock(data);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
2016-02-18 14:12:51 +00:00
|
|
|
clk_disable(data->clk_master);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
2014-05-12 06:14:57 +00:00
|
|
|
spin_unlock_irqrestore(&data->lock, flags);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2017-08-28 12:12:05 +00:00
|
|
|
static const struct iommu_ops exynos_iommu_ops;
|
2016-05-23 11:01:27 +00:00
|
|
|
|
2019-08-12 10:32:46 +00:00
|
|
|
static int exynos_sysmmu_probe(struct platform_device *pdev)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2014-05-12 06:14:54 +00:00
|
|
|
int irq, ret;
|
2014-05-12 06:14:46 +00:00
|
|
|
struct device *dev = &pdev->dev;
|
2012-05-11 20:56:09 +00:00
|
|
|
struct sysmmu_drvdata *data;
|
2014-05-12 06:14:46 +00:00
|
|
|
struct resource *res;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:14:54 +00:00
|
|
|
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
|
|
|
|
if (!data)
|
|
|
|
return -ENOMEM;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:14:46 +00:00
|
|
|
res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
|
2014-05-12 06:14:54 +00:00
|
|
|
data->sfrbase = devm_ioremap_resource(dev, res);
|
|
|
|
if (IS_ERR(data->sfrbase))
|
|
|
|
return PTR_ERR(data->sfrbase);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:14:54 +00:00
|
|
|
irq = platform_get_irq(pdev, 0);
|
2019-07-30 18:15:22 +00:00
|
|
|
if (irq <= 0)
|
2014-05-12 06:14:54 +00:00
|
|
|
return irq;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:14:54 +00:00
|
|
|
ret = devm_request_irq(dev, irq, exynos_sysmmu_irq, 0,
|
2014-05-12 06:14:46 +00:00
|
|
|
dev_name(dev), data);
|
|
|
|
if (ret) {
|
2014-05-12 06:14:54 +00:00
|
|
|
dev_err(dev, "Unabled to register handler of irq %d\n", irq);
|
|
|
|
return ret;
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2023-03-26 12:37:50 +00:00
|
|
|
data->clk = devm_clk_get_optional(dev, "sysmmu");
|
|
|
|
if (IS_ERR(data->clk))
|
2016-05-23 09:30:07 +00:00
|
|
|
return PTR_ERR(data->clk);
|
2016-02-18 14:12:58 +00:00
|
|
|
|
2023-03-26 12:37:50 +00:00
|
|
|
data->aclk = devm_clk_get_optional(dev, "aclk");
|
|
|
|
if (IS_ERR(data->aclk))
|
2016-05-23 09:30:07 +00:00
|
|
|
return PTR_ERR(data->aclk);
|
2016-02-18 14:12:58 +00:00
|
|
|
|
2023-03-26 12:37:50 +00:00
|
|
|
data->pclk = devm_clk_get_optional(dev, "pclk");
|
|
|
|
if (IS_ERR(data->pclk))
|
2016-05-23 09:30:07 +00:00
|
|
|
return PTR_ERR(data->pclk);
|
2016-02-18 14:12:58 +00:00
|
|
|
|
|
|
|
if (!data->clk && (!data->aclk || !data->pclk)) {
|
|
|
|
dev_err(dev, "Failed to get device clock(s)!\n");
|
|
|
|
return -ENOSYS;
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2023-03-26 12:37:50 +00:00
|
|
|
data->clk_master = devm_clk_get_optional(dev, "master");
|
|
|
|
if (IS_ERR(data->clk_master))
|
2016-05-23 09:30:07 +00:00
|
|
|
return PTR_ERR(data->clk_master);
|
2014-05-12 06:14:55 +00:00
|
|
|
|
2012-05-11 20:56:09 +00:00
|
|
|
data->sysmmu = dev;
|
2014-05-12 06:14:57 +00:00
|
|
|
spin_lock_init(&data->lock);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2022-07-14 16:55:48 +00:00
|
|
|
__sysmmu_get_version(data);
|
|
|
|
|
2017-02-03 12:23:42 +00:00
|
|
|
ret = iommu_device_sysfs_add(&data->iommu, &pdev->dev, NULL,
|
|
|
|
dev_name(data->sysmmu));
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
2014-05-12 06:14:46 +00:00
|
|
|
platform_set_drvdata(pdev, data);
|
|
|
|
|
2016-02-18 14:12:58 +00:00
|
|
|
if (PG_ENT_SHIFT < 0) {
|
2016-11-03 08:04:45 +00:00
|
|
|
if (MMU_MAJ_VER(data->version) < 5) {
|
2016-02-18 14:12:58 +00:00
|
|
|
PG_ENT_SHIFT = SYSMMU_PG_ENT_SHIFT;
|
2016-11-03 08:04:45 +00:00
|
|
|
LV1_PROT = SYSMMU_LV1_PROT;
|
|
|
|
LV2_PROT = SYSMMU_LV2_PROT;
|
|
|
|
} else {
|
2016-02-18 14:12:58 +00:00
|
|
|
PG_ENT_SHIFT = SYSMMU_V5_PG_ENT_SHIFT;
|
2016-11-03 08:04:45 +00:00
|
|
|
LV1_PROT = SYSMMU_V5_LV1_PROT;
|
|
|
|
LV2_PROT = SYSMMU_V5_LV2_PROT;
|
|
|
|
}
|
2016-02-18 14:12:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 16:55:47 +00:00
|
|
|
if (MMU_MAJ_VER(data->version) >= 5) {
|
|
|
|
ret = dma_set_mask(dev, DMA_BIT_MASK(36));
|
|
|
|
if (ret) {
|
|
|
|
dev_err(dev, "Unable to set DMA mask: %d\n", ret);
|
|
|
|
goto err_dma_set_mask;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 10:28:33 +00: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;
|
|
|
|
|
2014-05-12 06:14:52 +00:00
|
|
|
pm_runtime_enable(dev);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2022-11-10 15:44:07 +00:00
|
|
|
ret = iommu_device_register(&data->iommu, &exynos_iommu_ops, dev);
|
|
|
|
if (ret)
|
|
|
|
goto err_dma_set_mask;
|
|
|
|
|
2012-05-11 20:56:09 +00:00
|
|
|
return 0;
|
2022-07-14 16:55:46 +00:00
|
|
|
|
2022-07-14 16:55:47 +00:00
|
|
|
err_dma_set_mask:
|
2022-07-14 16:55:46 +00:00
|
|
|
iommu_device_sysfs_remove(&data->iommu);
|
|
|
|
return ret;
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 10:08:11 +00:00
|
|
|
static int __maybe_unused exynos_sysmmu_suspend(struct device *dev)
|
2015-05-19 13:20:35 +00:00
|
|
|
{
|
|
|
|
struct sysmmu_drvdata *data = dev_get_drvdata(dev);
|
2016-11-14 10:08:08 +00:00
|
|
|
struct device *master = data->master;
|
2015-05-19 13:20:35 +00:00
|
|
|
|
2016-11-14 10:08:08 +00:00
|
|
|
if (master) {
|
2020-06-25 13:08:24 +00:00
|
|
|
struct exynos_iommu_owner *owner = dev_iommu_priv_get(master);
|
2016-11-14 10:08:11 +00:00
|
|
|
|
|
|
|
mutex_lock(&owner->rpm_lock);
|
2023-09-13 13:43:43 +00:00
|
|
|
if (&data->domain->domain != &exynos_identity_domain) {
|
2016-11-14 10:08:09 +00:00
|
|
|
dev_dbg(data->sysmmu, "saving state\n");
|
|
|
|
__sysmmu_disable(data);
|
|
|
|
}
|
2016-11-14 10:08:11 +00:00
|
|
|
mutex_unlock(&owner->rpm_lock);
|
2015-05-19 13:20:35 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-11-14 10:08:11 +00:00
|
|
|
static int __maybe_unused exynos_sysmmu_resume(struct device *dev)
|
2015-05-19 13:20:35 +00:00
|
|
|
{
|
|
|
|
struct sysmmu_drvdata *data = dev_get_drvdata(dev);
|
2016-11-14 10:08:08 +00:00
|
|
|
struct device *master = data->master;
|
2015-05-19 13:20:35 +00:00
|
|
|
|
2016-11-14 10:08:08 +00:00
|
|
|
if (master) {
|
2020-06-25 13:08:24 +00:00
|
|
|
struct exynos_iommu_owner *owner = dev_iommu_priv_get(master);
|
2016-11-14 10:08:11 +00:00
|
|
|
|
|
|
|
mutex_lock(&owner->rpm_lock);
|
2023-09-13 13:43:43 +00:00
|
|
|
if (&data->domain->domain != &exynos_identity_domain) {
|
2016-11-14 10:08:09 +00:00
|
|
|
dev_dbg(data->sysmmu, "restoring state\n");
|
|
|
|
__sysmmu_enable(data);
|
|
|
|
}
|
2016-11-14 10:08:11 +00:00
|
|
|
mutex_unlock(&owner->rpm_lock);
|
2015-05-19 13:20:35 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct dev_pm_ops sysmmu_pm_ops = {
|
2016-11-14 10:08:11 +00:00
|
|
|
SET_RUNTIME_PM_OPS(exynos_sysmmu_suspend, exynos_sysmmu_resume, NULL)
|
2016-11-14 10:08:12 +00:00
|
|
|
SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
|
|
|
|
pm_runtime_force_resume)
|
2015-05-19 13:20:35 +00:00
|
|
|
};
|
|
|
|
|
2017-10-09 11:40:23 +00:00
|
|
|
static const struct of_device_id sysmmu_of_match[] = {
|
2014-05-12 06:15:02 +00:00
|
|
|
{ .compatible = "samsung,exynos-sysmmu", },
|
|
|
|
{ },
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct platform_driver exynos_sysmmu_driver __refdata = {
|
|
|
|
.probe = exynos_sysmmu_probe,
|
|
|
|
.driver = {
|
2012-05-11 20:56:09 +00:00
|
|
|
.name = "exynos-sysmmu",
|
2014-05-12 06:15:02 +00:00
|
|
|
.of_match_table = sysmmu_of_match,
|
2015-05-19 13:20:35 +00:00
|
|
|
.pm = &sysmmu_pm_ops,
|
2016-05-20 13:48:21 +00:00
|
|
|
.suppress_bind_attrs = true,
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-14 11:59:28 +00:00
|
|
|
static inline void exynos_iommu_set_pte(sysmmu_pte_t *ent, sysmmu_pte_t val)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2016-02-18 14:12:50 +00:00
|
|
|
dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent), sizeof(*ent),
|
|
|
|
DMA_TO_DEVICE);
|
2016-06-08 18:31:10 +00:00
|
|
|
*ent = cpu_to_le32(val);
|
2016-02-18 14:12:50 +00:00
|
|
|
dma_sync_single_for_device(dma_dev, virt_to_phys(ent), sizeof(*ent),
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 13:43:56 +00:00
|
|
|
static struct iommu_domain *exynos_iommu_domain_alloc_paging(struct device *dev)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2015-05-19 13:20:28 +00:00
|
|
|
struct exynos_iommu_domain *domain;
|
2016-02-18 14:12:50 +00:00
|
|
|
dma_addr_t handle;
|
2014-05-12 06:15:04 +00:00
|
|
|
int i;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2016-02-18 14:12:58 +00:00
|
|
|
/* Check if correct PTE offsets are initialized */
|
|
|
|
BUG_ON(PG_ENT_SHIFT < 0 || !dma_dev);
|
2015-03-26 12:43:11 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
domain = kzalloc(sizeof(*domain), GFP_KERNEL);
|
|
|
|
if (!domain)
|
2015-03-26 12:43:11 +00:00
|
|
|
return NULL;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2024-04-13 00:25:17 +00:00
|
|
|
domain->pgtable = iommu_alloc_pages(GFP_KERNEL, 2);
|
2015-05-19 13:20:28 +00:00
|
|
|
if (!domain->pgtable)
|
2021-08-11 12:21:19 +00:00
|
|
|
goto err_pgtable;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2024-04-13 00:25:17 +00:00
|
|
|
domain->lv2entcnt = iommu_alloc_pages(GFP_KERNEL, 1);
|
2015-05-19 13:20:28 +00:00
|
|
|
if (!domain->lv2entcnt)
|
2012-05-11 20:56:09 +00:00
|
|
|
goto err_counter;
|
|
|
|
|
2014-08-04 04:36:28 +00:00
|
|
|
/* Workaround for System MMU v3.3 to prevent caching 1MiB mapping */
|
2017-03-24 09:18:44 +00:00
|
|
|
for (i = 0; i < NUM_LV1ENTRIES; i++)
|
|
|
|
domain->pgtable[i] = ZERO_LV2LINK;
|
2014-05-12 06:15:04 +00:00
|
|
|
|
2016-02-18 14:12:50 +00: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));
|
2017-01-09 12:03:54 +00:00
|
|
|
if (dma_mapping_error(dma_dev, handle))
|
|
|
|
goto err_lv2ent;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_lock_init(&domain->lock);
|
|
|
|
spin_lock_init(&domain->pgtablelock);
|
|
|
|
INIT_LIST_HEAD(&domain->clients);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
domain->domain.geometry.aperture_start = 0;
|
|
|
|
domain->domain.geometry.aperture_end = ~0UL;
|
|
|
|
domain->domain.geometry.force_aperture = true;
|
2012-07-11 10:41:10 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
return &domain->domain;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2017-01-09 12:03:54 +00:00
|
|
|
err_lv2ent:
|
2024-04-13 00:25:17 +00:00
|
|
|
iommu_free_pages(domain->lv2entcnt, 1);
|
2012-05-11 20:56:09 +00:00
|
|
|
err_counter:
|
2024-04-13 00:25:17 +00:00
|
|
|
iommu_free_pages(domain->pgtable, 2);
|
2012-05-11 20:56:09 +00:00
|
|
|
err_pgtable:
|
2015-05-19 13:20:28 +00:00
|
|
|
kfree(domain);
|
2015-03-26 12:43:11 +00:00
|
|
|
return NULL;
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
static void exynos_iommu_domain_free(struct iommu_domain *iommu_domain)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2015-05-19 13:20:28 +00:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2015-05-19 13:20:27 +00:00
|
|
|
struct sysmmu_drvdata *data, *next;
|
2012-05-11 20:56:09 +00:00
|
|
|
unsigned long flags;
|
|
|
|
int i;
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
WARN_ON(!list_empty(&domain->clients));
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_lock_irqsave(&domain->lock, flags);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
|
2016-11-14 10:08:10 +00:00
|
|
|
spin_lock(&data->lock);
|
2016-11-14 10:08:07 +00:00
|
|
|
__sysmmu_disable(data);
|
2016-11-14 10:08:08 +00:00
|
|
|
data->pgtable = 0;
|
|
|
|
data->domain = NULL;
|
2015-05-19 13:20:27 +00:00
|
|
|
list_del_init(&data->domain_node);
|
2016-11-14 10:08:10 +00:00
|
|
|
spin_unlock(&data->lock);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_unlock_irqrestore(&domain->lock, flags);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2016-02-18 14:12:50 +00:00
|
|
|
dma_unmap_single(dma_dev, virt_to_phys(domain->pgtable), LV1TABLE_SIZE,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
|
2012-05-11 20:56:09 +00:00
|
|
|
for (i = 0; i < NUM_LV1ENTRIES; i++)
|
2016-02-18 14:12:50 +00: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 06:14:48 +00:00
|
|
|
kmem_cache_free(lv2table_kmem_cache,
|
2016-02-18 14:12:50 +00:00
|
|
|
phys_to_virt(base));
|
|
|
|
}
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2024-04-13 00:25:17 +00:00
|
|
|
iommu_free_pages(domain->pgtable, 2);
|
|
|
|
iommu_free_pages(domain->lv2entcnt, 1);
|
2015-05-19 13:20:28 +00:00
|
|
|
kfree(domain);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 13:43:43 +00:00
|
|
|
static int exynos_iommu_identity_attach(struct iommu_domain *identity_domain,
|
|
|
|
struct device *dev)
|
2016-02-18 14:13:00 +00:00
|
|
|
{
|
2020-06-25 13:08:24 +00:00
|
|
|
struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
|
2023-09-13 13:43:43 +00:00
|
|
|
struct exynos_iommu_domain *domain;
|
|
|
|
phys_addr_t pagetable;
|
2016-02-18 14:13:00 +00:00
|
|
|
struct sysmmu_drvdata *data, *next;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2023-09-13 13:43:43 +00:00
|
|
|
if (owner->domain == identity_domain)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
domain = to_exynos_domain(owner->domain);
|
|
|
|
pagetable = virt_to_phys(domain->pgtable);
|
2016-02-18 14:13:00 +00:00
|
|
|
|
2016-11-14 10:08:11 +00:00
|
|
|
mutex_lock(&owner->rpm_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(data, &owner->controllers, owner_node) {
|
|
|
|
pm_runtime_get_noresume(data->sysmmu);
|
|
|
|
if (pm_runtime_active(data->sysmmu))
|
|
|
|
__sysmmu_disable(data);
|
2016-11-14 10:08:10 +00:00
|
|
|
pm_runtime_put(data->sysmmu);
|
|
|
|
}
|
|
|
|
|
2016-02-18 14:13:00 +00:00
|
|
|
spin_lock_irqsave(&domain->lock, flags);
|
|
|
|
list_for_each_entry_safe(data, next, &domain->clients, domain_node) {
|
2016-11-14 10:08:10 +00:00
|
|
|
spin_lock(&data->lock);
|
2016-11-14 10:08:08 +00:00
|
|
|
data->pgtable = 0;
|
|
|
|
data->domain = NULL;
|
2016-11-14 10:08:07 +00:00
|
|
|
list_del_init(&data->domain_node);
|
2016-11-14 10:08:10 +00:00
|
|
|
spin_unlock(&data->lock);
|
2016-02-18 14:13:00 +00:00
|
|
|
}
|
2023-09-13 13:43:43 +00:00
|
|
|
owner->domain = identity_domain;
|
2016-02-18 14:13:00 +00:00
|
|
|
spin_unlock_irqrestore(&domain->lock, flags);
|
|
|
|
|
2016-11-14 10:08:11 +00:00
|
|
|
mutex_unlock(&owner->rpm_lock);
|
2016-02-18 14:13:00 +00:00
|
|
|
|
2023-09-13 13:43:43 +00:00
|
|
|
dev_dbg(dev, "%s: Restored IOMMU to IDENTITY from pgtable %pa\n",
|
|
|
|
__func__, &pagetable);
|
|
|
|
return 0;
|
2016-02-18 14:13:00 +00:00
|
|
|
}
|
|
|
|
|
2023-09-13 13:43:43 +00:00
|
|
|
static struct iommu_domain_ops exynos_identity_ops = {
|
|
|
|
.attach_dev = exynos_iommu_identity_attach,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct iommu_domain exynos_identity_domain = {
|
|
|
|
.type = IOMMU_DOMAIN_IDENTITY,
|
|
|
|
.ops = &exynos_identity_ops,
|
|
|
|
};
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
static int exynos_iommu_attach_device(struct iommu_domain *iommu_domain,
|
2012-05-11 20:56:09 +00:00
|
|
|
struct device *dev)
|
|
|
|
{
|
2015-05-19 13:20:28 +00:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2020-06-25 13:08:24 +00:00
|
|
|
struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
|
2015-05-19 13:20:27 +00:00
|
|
|
struct sysmmu_drvdata *data;
|
2015-05-19 13:20:28 +00:00
|
|
|
phys_addr_t pagetable = virt_to_phys(domain->pgtable);
|
2012-05-11 20:56:09 +00:00
|
|
|
unsigned long flags;
|
2023-09-13 13:43:43 +00:00
|
|
|
int err;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2023-09-13 13:43:43 +00:00
|
|
|
err = exynos_iommu_identity_attach(&exynos_identity_domain, dev);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2016-02-18 14:13:00 +00:00
|
|
|
|
2016-11-14 10:08:11 +00:00
|
|
|
mutex_lock(&owner->rpm_lock);
|
|
|
|
|
2016-11-14 10:08:10 +00:00
|
|
|
spin_lock_irqsave(&domain->lock, flags);
|
2015-05-19 13:20:33 +00:00
|
|
|
list_for_each_entry(data, &owner->controllers, owner_node) {
|
2016-11-14 10:08:10 +00:00
|
|
|
spin_lock(&data->lock);
|
2016-11-14 10:08:08 +00:00
|
|
|
data->pgtable = pagetable;
|
|
|
|
data->domain = domain;
|
2016-11-14 10:08:10 +00:00
|
|
|
list_add_tail(&data->domain_node, &domain->clients);
|
|
|
|
spin_unlock(&data->lock);
|
|
|
|
}
|
|
|
|
owner->domain = iommu_domain;
|
|
|
|
spin_unlock_irqrestore(&domain->lock, flags);
|
|
|
|
|
2016-11-14 10:08:11 +00:00
|
|
|
list_for_each_entry(data, &owner->controllers, owner_node) {
|
|
|
|
pm_runtime_get_noresume(data->sysmmu);
|
|
|
|
if (pm_runtime_active(data->sysmmu))
|
|
|
|
__sysmmu_enable(data);
|
|
|
|
pm_runtime_put(data->sysmmu);
|
|
|
|
}
|
|
|
|
|
|
|
|
mutex_unlock(&owner->rpm_lock);
|
|
|
|
|
2016-11-14 10:08:07 +00:00
|
|
|
dev_dbg(dev, "%s: Attached IOMMU with pgtable %pa\n", __func__,
|
|
|
|
&pagetable);
|
2014-05-12 06:14:46 +00:00
|
|
|
|
2016-11-14 10:08:07 +00:00
|
|
|
return 0;
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
static sysmmu_pte_t *alloc_lv2entry(struct exynos_iommu_domain *domain,
|
2014-05-12 06:15:04 +00:00
|
|
|
sysmmu_pte_t *sent, sysmmu_iova_t iova, short *pgcounter)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2014-05-12 06:14:47 +00:00
|
|
|
if (lv1ent_section(sent)) {
|
2014-05-12 06:14:58 +00:00
|
|
|
WARN(1, "Trying mapping on %#08x mapped with 1MiB page", iova);
|
2014-05-12 06:14:47 +00:00
|
|
|
return ERR_PTR(-EADDRINUSE);
|
|
|
|
}
|
|
|
|
|
2012-05-11 20:56:09 +00:00
|
|
|
if (lv1ent_fault(sent)) {
|
2017-01-09 12:03:54 +00:00
|
|
|
dma_addr_t handle;
|
2014-05-12 06:14:58 +00:00
|
|
|
sysmmu_pte_t *pent;
|
2014-05-12 06:15:04 +00:00
|
|
|
bool need_flush_flpd_cache = lv1ent_zero(sent);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:14:48 +00:00
|
|
|
pent = kmem_cache_zalloc(lv2table_kmem_cache, GFP_ATOMIC);
|
2016-02-29 08:45:59 +00:00
|
|
|
BUG_ON((uintptr_t)pent & (LV2TABLE_SIZE - 1));
|
2012-05-11 20:56:09 +00:00
|
|
|
if (!pent)
|
2014-05-12 06:14:47 +00:00
|
|
|
return ERR_PTR(-ENOMEM);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2020-07-14 11:59:28 +00:00
|
|
|
exynos_iommu_set_pte(sent, mk_lv1ent_page(virt_to_phys(pent)));
|
2015-05-09 00:05:44 +00:00
|
|
|
kmemleak_ignore(pent);
|
2012-05-11 20:56:09 +00:00
|
|
|
*pgcounter = NUM_LV2ENTRIES;
|
2017-01-09 12:03:54 +00:00
|
|
|
handle = dma_map_single(dma_dev, pent, LV2TABLE_SIZE,
|
|
|
|
DMA_TO_DEVICE);
|
|
|
|
if (dma_mapping_error(dma_dev, handle)) {
|
|
|
|
kmem_cache_free(lv2table_kmem_cache, pent);
|
|
|
|
return ERR_PTR(-EADDRINUSE);
|
|
|
|
}
|
2014-05-12 06:15:04 +00:00
|
|
|
|
|
|
|
/*
|
2014-08-04 04:36:28 +00: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 06:15:04 +00:00
|
|
|
* Accessing the valid area may cause page fault since FLPD
|
2014-08-04 04:36:28 +00: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 06:15:04 +00: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 13:20:27 +00:00
|
|
|
struct sysmmu_drvdata *data;
|
2014-05-22 04:20:56 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_lock(&domain->lock);
|
|
|
|
list_for_each_entry(data, &domain->clients, domain_node)
|
2015-05-19 13:20:27 +00:00
|
|
|
sysmmu_tlb_invalidate_flpdcache(data, iova);
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_unlock(&domain->lock);
|
2014-05-12 06:15:04 +00:00
|
|
|
}
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return page_entry(sent, iova);
|
|
|
|
}
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
static int lv1set_section(struct exynos_iommu_domain *domain,
|
2014-05-12 06:15:04 +00:00
|
|
|
sysmmu_pte_t *sent, sysmmu_iova_t iova,
|
2016-11-03 08:04:45 +00:00
|
|
|
phys_addr_t paddr, int prot, short *pgcnt)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2014-05-12 06:14:47 +00:00
|
|
|
if (lv1ent_section(sent)) {
|
2014-05-12 06:14:58 +00:00
|
|
|
WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
|
2014-05-12 06:14:47 +00:00
|
|
|
iova);
|
2012-05-11 20:56:09 +00:00
|
|
|
return -EADDRINUSE;
|
2014-05-12 06:14:47 +00:00
|
|
|
}
|
2012-05-11 20:56:09 +00:00
|
|
|
|
|
|
|
if (lv1ent_page(sent)) {
|
2014-05-12 06:14:47 +00:00
|
|
|
if (*pgcnt != NUM_LV2ENTRIES) {
|
2014-05-12 06:14:58 +00:00
|
|
|
WARN(1, "Trying mapping on 1MiB@%#08x that is mapped",
|
2014-05-12 06:14:47 +00:00
|
|
|
iova);
|
2012-05-11 20:56:09 +00:00
|
|
|
return -EADDRINUSE;
|
2014-05-12 06:14:47 +00:00
|
|
|
}
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:14:48 +00:00
|
|
|
kmem_cache_free(lv2table_kmem_cache, page_entry(sent, 0));
|
2012-05-11 20:56:09 +00:00
|
|
|
*pgcnt = 0;
|
|
|
|
}
|
|
|
|
|
2020-07-14 11:59:28 +00:00
|
|
|
exynos_iommu_set_pte(sent, mk_lv1ent_sect(paddr, prot));
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_lock(&domain->lock);
|
2014-05-12 06:15:04 +00:00
|
|
|
if (lv1ent_page_zero(sent)) {
|
2015-05-19 13:20:27 +00:00
|
|
|
struct sysmmu_drvdata *data;
|
2014-05-12 06:15:04 +00: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 13:20:28 +00:00
|
|
|
list_for_each_entry(data, &domain->clients, domain_node)
|
2015-05-19 13:20:27 +00:00
|
|
|
sysmmu_tlb_invalidate_flpdcache(data, iova);
|
2014-05-12 06:15:04 +00:00
|
|
|
}
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_unlock(&domain->lock);
|
2014-05-12 06:15:04 +00:00
|
|
|
|
2012-05-11 20:56:09 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:14:58 +00:00
|
|
|
static int lv2set_page(sysmmu_pte_t *pent, phys_addr_t paddr, size_t size,
|
2016-11-03 08:04:45 +00:00
|
|
|
int prot, short *pgcnt)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
|
|
|
if (size == SPAGE_SIZE) {
|
2014-05-12 06:15:00 +00:00
|
|
|
if (WARN_ON(!lv2ent_fault(pent)))
|
2012-05-11 20:56:09 +00:00
|
|
|
return -EADDRINUSE;
|
|
|
|
|
2020-07-14 11:59:28 +00:00
|
|
|
exynos_iommu_set_pte(pent, mk_lv2ent_spage(paddr, prot));
|
2012-05-11 20:56:09 +00:00
|
|
|
*pgcnt -= 1;
|
|
|
|
} else { /* size == LPAGE_SIZE */
|
|
|
|
int i;
|
2016-02-18 14:12:50 +00:00
|
|
|
dma_addr_t pent_base = virt_to_phys(pent);
|
2014-05-22 04:20:56 +00:00
|
|
|
|
2016-02-18 14:12:50 +00:00
|
|
|
dma_sync_single_for_cpu(dma_dev, pent_base,
|
|
|
|
sizeof(*pent) * SPAGES_PER_LPAGE,
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-11 20:56:09 +00:00
|
|
|
for (i = 0; i < SPAGES_PER_LPAGE; i++, pent++) {
|
2014-05-12 06:15:00 +00:00
|
|
|
if (WARN_ON(!lv2ent_fault(pent))) {
|
2014-05-12 06:14:47 +00:00
|
|
|
if (i > 0)
|
|
|
|
memset(pent - i, 0, sizeof(*pent) * i);
|
2012-05-11 20:56:09 +00:00
|
|
|
return -EADDRINUSE;
|
|
|
|
}
|
|
|
|
|
2016-11-03 08:04:45 +00:00
|
|
|
*pent = mk_lv2ent_lpage(paddr, prot);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
2016-02-18 14:12:50 +00:00
|
|
|
dma_sync_single_for_device(dma_dev, pent_base,
|
|
|
|
sizeof(*pent) * SPAGES_PER_LPAGE,
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-11 20:56:09 +00:00
|
|
|
*pgcnt -= SPAGES_PER_LPAGE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:15:04 +00:00
|
|
|
/*
|
|
|
|
* *CAUTION* to the I/O virtual memory managers that support exynos-iommu:
|
|
|
|
*
|
2014-08-04 04:36:28 +00:00
|
|
|
* System MMU v3.x has advanced logic to improve address translation
|
2014-05-12 06:15:04 +00:00
|
|
|
* performance with caching more page table entries by a page table walk.
|
2014-08-04 04:36:28 +00: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 06:15:04 +00:00
|
|
|
*
|
|
|
|
* Any two consecutive I/O virtual address regions must have a hole of 128KiB
|
2014-08-04 04:36:28 +00:00
|
|
|
* at maximum to prevent misbehavior of System MMU 3.x (workaround for h/w bug).
|
2014-05-12 06:15:04 +00:00
|
|
|
*
|
2014-08-04 04:36:28 +00:00
|
|
|
* Precisely, any start address of I/O virtual region must be aligned with
|
2014-05-12 06:15:04 +00: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 04:36:28 +00: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 06:15:04 +00:00
|
|
|
* - Start address of an I/O virtual region must be aligned by 128KiB.
|
|
|
|
*/
|
2015-05-19 13:20:28 +00:00
|
|
|
static int exynos_iommu_map(struct iommu_domain *iommu_domain,
|
|
|
|
unsigned long l_iova, phys_addr_t paddr, size_t size,
|
2023-09-12 16:18:37 +00:00
|
|
|
size_t count, int prot, gfp_t gfp, size_t *mapped)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2015-05-19 13:20:28 +00:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2014-05-12 06:14:58 +00:00
|
|
|
sysmmu_pte_t *entry;
|
|
|
|
sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
|
2012-05-11 20:56:09 +00:00
|
|
|
unsigned long flags;
|
|
|
|
int ret = -ENOMEM;
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
BUG_ON(domain->pgtable == NULL);
|
2016-11-03 08:04:45 +00:00
|
|
|
prot &= SYSMMU_SUPPORTED_PROT_BITS;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_lock_irqsave(&domain->pgtablelock, flags);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
entry = section_entry(domain->pgtable, iova);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
|
|
|
if (size == SECT_SIZE) {
|
2016-11-03 08:04:45 +00:00
|
|
|
ret = lv1set_section(domain, entry, iova, paddr, prot,
|
2015-05-19 13:20:28 +00:00
|
|
|
&domain->lv2entcnt[lv1ent_offset(iova)]);
|
2012-05-11 20:56:09 +00:00
|
|
|
} else {
|
2014-05-12 06:14:58 +00:00
|
|
|
sysmmu_pte_t *pent;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
pent = alloc_lv2entry(domain, entry, iova,
|
|
|
|
&domain->lv2entcnt[lv1ent_offset(iova)]);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-05-12 06:14:47 +00:00
|
|
|
if (IS_ERR(pent))
|
|
|
|
ret = PTR_ERR(pent);
|
2012-05-11 20:56:09 +00:00
|
|
|
else
|
2016-11-03 08:04:45 +00:00
|
|
|
ret = lv2set_page(pent, paddr, size, prot,
|
2015-05-19 13:20:28 +00:00
|
|
|
&domain->lv2entcnt[lv1ent_offset(iova)]);
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2014-05-12 06:14:47 +00:00
|
|
|
if (ret)
|
2014-05-12 06:15:00 +00:00
|
|
|
pr_err("%s: Failed(%d) to map %#zx bytes @ %#x\n",
|
|
|
|
__func__, ret, size, iova);
|
2023-09-12 16:18:37 +00:00
|
|
|
else
|
|
|
|
*mapped = size;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_unlock_irqrestore(&domain->pgtablelock, flags);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
static void exynos_iommu_tlb_invalidate_entry(struct exynos_iommu_domain *domain,
|
|
|
|
sysmmu_iova_t iova, size_t size)
|
2014-05-12 06:15:04 +00:00
|
|
|
{
|
2015-05-19 13:20:27 +00:00
|
|
|
struct sysmmu_drvdata *data;
|
2014-05-12 06:15:04 +00:00
|
|
|
unsigned long flags;
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_lock_irqsave(&domain->lock, flags);
|
2014-05-12 06:15:04 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
list_for_each_entry(data, &domain->clients, domain_node)
|
2015-05-19 13:20:27 +00:00
|
|
|
sysmmu_tlb_invalidate_entry(data, iova, size);
|
2014-05-12 06:15:04 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_unlock_irqrestore(&domain->lock, flags);
|
2014-05-12 06:15:04 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
static size_t exynos_iommu_unmap(struct iommu_domain *iommu_domain,
|
2023-09-12 16:18:37 +00:00
|
|
|
unsigned long l_iova, size_t size, size_t count,
|
2019-07-02 15:44:06 +00:00
|
|
|
struct iommu_iotlb_gather *gather)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2015-05-19 13:20:28 +00:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2014-05-12 06:14:58 +00:00
|
|
|
sysmmu_iova_t iova = (sysmmu_iova_t)l_iova;
|
|
|
|
sysmmu_pte_t *ent;
|
2014-05-12 06:14:47 +00:00
|
|
|
size_t err_pgsize;
|
2014-05-12 06:14:58 +00:00
|
|
|
unsigned long flags;
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
BUG_ON(domain->pgtable == NULL);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_lock_irqsave(&domain->pgtablelock, flags);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
ent = section_entry(domain->pgtable, iova);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
|
|
|
if (lv1ent_section(ent)) {
|
2014-05-12 06:15:00 +00:00
|
|
|
if (WARN_ON(size < SECT_SIZE)) {
|
2014-05-12 06:14:47 +00:00
|
|
|
err_pgsize = SECT_SIZE;
|
|
|
|
goto err;
|
|
|
|
}
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2014-08-04 04:36:28 +00:00
|
|
|
/* workaround for h/w bug in System MMU v3.3 */
|
2020-07-14 11:59:28 +00:00
|
|
|
exynos_iommu_set_pte(ent, ZERO_LV2LINK);
|
2012-05-11 20:56:09 +00: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)) {
|
2020-07-14 11:59:28 +00:00
|
|
|
exynos_iommu_set_pte(ent, 0);
|
2012-05-11 20:56:09 +00:00
|
|
|
size = SPAGE_SIZE;
|
2015-05-19 13:20:28 +00:00
|
|
|
domain->lv2entcnt[lv1ent_offset(iova)] += 1;
|
2012-05-11 20:56:09 +00:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lv1ent_large(ent) == true here */
|
2014-05-12 06:15:00 +00:00
|
|
|
if (WARN_ON(size < LPAGE_SIZE)) {
|
2014-05-12 06:14:47 +00:00
|
|
|
err_pgsize = LPAGE_SIZE;
|
|
|
|
goto err;
|
|
|
|
}
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2016-02-18 14:12:50 +00:00
|
|
|
dma_sync_single_for_cpu(dma_dev, virt_to_phys(ent),
|
|
|
|
sizeof(*ent) * SPAGES_PER_LPAGE,
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-11 20:56:09 +00:00
|
|
|
memset(ent, 0, sizeof(*ent) * SPAGES_PER_LPAGE);
|
2016-02-18 14:12:50 +00:00
|
|
|
dma_sync_single_for_device(dma_dev, virt_to_phys(ent),
|
|
|
|
sizeof(*ent) * SPAGES_PER_LPAGE,
|
|
|
|
DMA_TO_DEVICE);
|
2012-05-11 20:56:09 +00:00
|
|
|
size = LPAGE_SIZE;
|
2015-05-19 13:20:28 +00:00
|
|
|
domain->lv2entcnt[lv1ent_offset(iova)] += SPAGES_PER_LPAGE;
|
2012-05-11 20:56:09 +00:00
|
|
|
done:
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_unlock_irqrestore(&domain->pgtablelock, flags);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
exynos_iommu_tlb_invalidate_entry(domain, iova, size);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
|
|
|
return size;
|
2014-05-12 06:14:47 +00:00
|
|
|
err:
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_unlock_irqrestore(&domain->pgtablelock, flags);
|
2014-05-12 06:14:47 +00:00
|
|
|
|
2014-05-12 06:15:00 +00:00
|
|
|
pr_err("%s: Failed: size(%#zx) @ %#x is smaller than page size %#zx\n",
|
|
|
|
__func__, size, iova, err_pgsize);
|
2014-05-12 06:14:47 +00:00
|
|
|
|
|
|
|
return 0;
|
2012-05-11 20:56:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
static phys_addr_t exynos_iommu_iova_to_phys(struct iommu_domain *iommu_domain,
|
2013-03-28 19:53:58 +00:00
|
|
|
dma_addr_t iova)
|
2012-05-11 20:56:09 +00:00
|
|
|
{
|
2015-05-19 13:20:28 +00:00
|
|
|
struct exynos_iommu_domain *domain = to_exynos_domain(iommu_domain);
|
2014-05-12 06:14:58 +00:00
|
|
|
sysmmu_pte_t *entry;
|
2012-05-11 20:56:09 +00:00
|
|
|
unsigned long flags;
|
|
|
|
phys_addr_t phys = 0;
|
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
spin_lock_irqsave(&domain->pgtablelock, flags);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
2015-05-19 13:20:28 +00:00
|
|
|
entry = section_entry(domain->pgtable, iova);
|
2012-05-11 20:56:09 +00: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 13:20:28 +00:00
|
|
|
spin_unlock_irqrestore(&domain->pgtablelock, flags);
|
2012-05-11 20:56:09 +00:00
|
|
|
|
|
|
|
return phys;
|
|
|
|
}
|
|
|
|
|
2020-04-29 13:37:09 +00:00
|
|
|
static struct iommu_device *exynos_iommu_probe_device(struct device *dev)
|
2014-05-12 06:14:59 +00:00
|
|
|
{
|
2020-06-25 13:08:24 +00:00
|
|
|
struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
|
2017-09-15 11:05:08 +00:00
|
|
|
struct sysmmu_drvdata *data;
|
2014-05-12 06:14:59 +00:00
|
|
|
|
2015-05-19 13:20:32 +00:00
|
|
|
if (!has_sysmmu(dev))
|
2020-04-29 13:37:09 +00:00
|
|
|
return ERR_PTR(-ENODEV);
|
2014-05-12 06:14:59 +00:00
|
|
|
|
2017-09-15 11:05:08 +00:00
|
|
|
list_for_each_entry(data, &owner->controllers, owner_node) {
|
|
|
|
/*
|
|
|
|
* SYSMMU will be runtime activated via device link
|
|
|
|
* (dependency) to its master device, so there are no
|
|
|
|
* direct calls to pm_runtime_get/put in this driver.
|
|
|
|
*/
|
|
|
|
data->link = device_link_add(dev, data->sysmmu,
|
2019-02-01 00:54:21 +00:00
|
|
|
DL_FLAG_STATELESS |
|
2017-09-15 11:05:08 +00:00
|
|
|
DL_FLAG_PM_RUNTIME);
|
|
|
|
}
|
2014-05-12 06:14:59 +00:00
|
|
|
|
2020-04-29 13:37:08 +00:00
|
|
|
/* There is always at least one entry, see exynos_iommu_of_xlate() */
|
|
|
|
data = list_first_entry(&owner->controllers,
|
|
|
|
struct sysmmu_drvdata, owner_node);
|
|
|
|
|
2020-04-29 13:37:09 +00:00
|
|
|
return &data->iommu;
|
2014-05-12 06:14:59 +00:00
|
|
|
}
|
|
|
|
|
2023-03-15 23:25:14 +00:00
|
|
|
static void exynos_iommu_release_device(struct device *dev)
|
|
|
|
{
|
|
|
|
struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
|
|
|
|
struct sysmmu_drvdata *data;
|
|
|
|
|
2023-09-13 13:43:43 +00:00
|
|
|
WARN_ON(exynos_iommu_identity_attach(&exynos_identity_domain, dev));
|
2017-09-15 11:05:08 +00:00
|
|
|
|
|
|
|
list_for_each_entry(data, &owner->controllers, owner_node)
|
|
|
|
device_link_del(data->link);
|
2014-05-12 06:14:59 +00:00
|
|
|
}
|
|
|
|
|
2015-05-19 13:20:37 +00:00
|
|
|
static int exynos_iommu_of_xlate(struct device *dev,
|
2024-02-16 14:40:25 +00:00
|
|
|
const struct of_phandle_args *spec)
|
2015-05-19 13:20:37 +00:00
|
|
|
{
|
|
|
|
struct platform_device *sysmmu = of_find_device_by_node(spec->np);
|
2020-06-25 13:08:24 +00:00
|
|
|
struct exynos_iommu_owner *owner = dev_iommu_priv_get(dev);
|
2017-01-09 12:03:55 +00:00
|
|
|
struct sysmmu_drvdata *data, *entry;
|
2015-05-19 13:20:37 +00:00
|
|
|
|
|
|
|
if (!sysmmu)
|
|
|
|
return -ENODEV;
|
|
|
|
|
|
|
|
data = platform_get_drvdata(sysmmu);
|
2020-09-18 01:13:35 +00:00
|
|
|
if (!data) {
|
|
|
|
put_device(&sysmmu->dev);
|
2015-05-19 13:20:37 +00:00
|
|
|
return -ENODEV;
|
2020-09-18 01:13:35 +00:00
|
|
|
}
|
2015-05-19 13:20:37 +00:00
|
|
|
|
|
|
|
if (!owner) {
|
|
|
|
owner = kzalloc(sizeof(*owner), GFP_KERNEL);
|
2020-09-18 01:13:35 +00:00
|
|
|
if (!owner) {
|
|
|
|
put_device(&sysmmu->dev);
|
2015-05-19 13:20:37 +00:00
|
|
|
return -ENOMEM;
|
2020-09-18 01:13:35 +00:00
|
|
|
}
|
2015-05-19 13:20:37 +00:00
|
|
|
|
|
|
|
INIT_LIST_HEAD(&owner->controllers);
|
2016-11-14 10:08:11 +00:00
|
|
|
mutex_init(&owner->rpm_lock);
|
2023-09-13 13:43:43 +00:00
|
|
|
owner->domain = &exynos_identity_domain;
|
2020-06-25 13:08:24 +00:00
|
|
|
dev_iommu_priv_set(dev, owner);
|
2015-05-19 13:20:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 12:03:55 +00:00
|
|
|
list_for_each_entry(entry, &owner->controllers, owner_node)
|
|
|
|
if (entry == data)
|
|
|
|
return 0;
|
|
|
|
|
2015-05-19 13:20:37 +00:00
|
|
|
list_add_tail(&data->owner_node, &owner->controllers);
|
2016-11-14 10:08:09 +00:00
|
|
|
data->master = dev;
|
2016-11-14 10:08:12 +00:00
|
|
|
|
2015-05-19 13:20:37 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-28 12:12:05 +00:00
|
|
|
static const struct iommu_ops exynos_iommu_ops = {
|
2023-09-13 13:43:43 +00:00
|
|
|
.identity_domain = &exynos_identity_domain,
|
2023-09-13 13:43:56 +00:00
|
|
|
.domain_alloc_paging = exynos_iommu_domain_alloc_paging,
|
2018-01-24 14:22:09 +00:00
|
|
|
.device_group = generic_device_group,
|
2020-04-29 13:37:09 +00:00
|
|
|
.probe_device = exynos_iommu_probe_device,
|
|
|
|
.release_device = exynos_iommu_release_device,
|
2012-05-11 20:56:09 +00:00
|
|
|
.pgsize_bitmap = SECT_SIZE | LPAGE_SIZE | SPAGE_SIZE,
|
2015-05-19 13:20:37 +00:00
|
|
|
.of_xlate = exynos_iommu_of_xlate,
|
2022-02-16 02:52:49 +00:00
|
|
|
.default_domain_ops = &(const struct iommu_domain_ops) {
|
|
|
|
.attach_dev = exynos_iommu_attach_device,
|
2023-09-12 16:18:37 +00:00
|
|
|
.map_pages = exynos_iommu_map,
|
|
|
|
.unmap_pages = exynos_iommu_unmap,
|
2022-02-16 02:52:49 +00:00
|
|
|
.iova_to_phys = exynos_iommu_iova_to_phys,
|
|
|
|
.free = exynos_iommu_domain_free,
|
|
|
|
}
|
2012-05-11 20:56:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int __init exynos_iommu_init(void)
|
|
|
|
{
|
2018-01-09 15:34:07 +00:00
|
|
|
struct device_node *np;
|
2012-05-11 20:56:09 +00:00
|
|
|
int ret;
|
|
|
|
|
2018-01-09 15:34:07 +00:00
|
|
|
np = of_find_matching_node(NULL, sysmmu_of_match);
|
|
|
|
if (!np)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
of_node_put(np);
|
|
|
|
|
2014-05-12 06:14:48 +00: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;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:15:04 +00: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;
|
|
|
|
}
|
|
|
|
|
2022-11-10 15:44:07 +00:00
|
|
|
ret = platform_driver_register(&exynos_sysmmu_driver);
|
|
|
|
if (ret) {
|
|
|
|
pr_err("%s: Failed to register driver\n", __func__);
|
|
|
|
goto err_reg_driver;
|
|
|
|
}
|
|
|
|
|
2014-05-12 06:14:48 +00:00
|
|
|
return 0;
|
|
|
|
err_reg_driver:
|
2023-01-04 09:57:02 +00:00
|
|
|
kmem_cache_free(lv2table_kmem_cache, zero_lv2_table);
|
2022-11-10 15:44:07 +00:00
|
|
|
err_zero_lv2:
|
2014-05-12 06:14:48 +00:00
|
|
|
kmem_cache_destroy(lv2table_kmem_cache);
|
2012-05-11 20:56:09 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2017-08-04 10:28:33 +00:00
|
|
|
core_initcall(exynos_iommu_init);
|