162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-only */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * omap iommu: main structures
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (C) 2008-2009 Nokia Corporation
662306a36Sopenharmony_ci *
762306a36Sopenharmony_ci * Written by Hiroshi DOYU <Hiroshi.DOYU@nokia.com>
862306a36Sopenharmony_ci */
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#ifndef _OMAP_IOMMU_H
1162306a36Sopenharmony_ci#define _OMAP_IOMMU_H
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include <linux/bitops.h>
1462306a36Sopenharmony_ci#include <linux/iommu.h>
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci#define for_each_iotlb_cr(obj, n, __i, cr)				\
1762306a36Sopenharmony_ci	for (__i = 0;							\
1862306a36Sopenharmony_ci	     (__i < (n)) && (cr = __iotlb_read_cr((obj), __i), true);	\
1962306a36Sopenharmony_ci	     __i++)
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_cistruct iotlb_entry {
2262306a36Sopenharmony_ci	u32 da;
2362306a36Sopenharmony_ci	u32 pa;
2462306a36Sopenharmony_ci	u32 pgsz, prsvd, valid;
2562306a36Sopenharmony_ci	u32 endian, elsz, mixed;
2662306a36Sopenharmony_ci};
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci/**
2962306a36Sopenharmony_ci * struct omap_iommu_device - omap iommu device data
3062306a36Sopenharmony_ci * @pgtable:	page table used by an omap iommu attached to a domain
3162306a36Sopenharmony_ci * @iommu_dev:	pointer to store an omap iommu instance attached to a domain
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_cistruct omap_iommu_device {
3462306a36Sopenharmony_ci	u32 *pgtable;
3562306a36Sopenharmony_ci	struct omap_iommu *iommu_dev;
3662306a36Sopenharmony_ci};
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci/**
3962306a36Sopenharmony_ci * struct omap_iommu_domain - omap iommu domain
4062306a36Sopenharmony_ci * @num_iommus: number of iommus in this domain
4162306a36Sopenharmony_ci * @iommus:	omap iommu device data for all iommus in this domain
4262306a36Sopenharmony_ci * @dev:	Device using this domain.
4362306a36Sopenharmony_ci * @lock:	domain lock, should be taken when attaching/detaching
4462306a36Sopenharmony_ci * @domain:	generic domain handle used by iommu core code
4562306a36Sopenharmony_ci */
4662306a36Sopenharmony_cistruct omap_iommu_domain {
4762306a36Sopenharmony_ci	u32 num_iommus;
4862306a36Sopenharmony_ci	struct omap_iommu_device *iommus;
4962306a36Sopenharmony_ci	struct device *dev;
5062306a36Sopenharmony_ci	spinlock_t lock;
5162306a36Sopenharmony_ci	struct iommu_domain domain;
5262306a36Sopenharmony_ci};
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_cistruct omap_iommu {
5562306a36Sopenharmony_ci	const char	*name;
5662306a36Sopenharmony_ci	void __iomem	*regbase;
5762306a36Sopenharmony_ci	struct regmap	*syscfg;
5862306a36Sopenharmony_ci	struct device	*dev;
5962306a36Sopenharmony_ci	struct iommu_domain *domain;
6062306a36Sopenharmony_ci	struct dentry	*debug_dir;
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci	spinlock_t	iommu_lock;	/* global for this whole object */
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	/*
6562306a36Sopenharmony_ci	 * We don't change iopgd for a situation like pgd for a task,
6662306a36Sopenharmony_ci	 * but share it globally for each iommu.
6762306a36Sopenharmony_ci	 */
6862306a36Sopenharmony_ci	u32		*iopgd;
6962306a36Sopenharmony_ci	spinlock_t	page_table_lock; /* protect iopgd */
7062306a36Sopenharmony_ci	dma_addr_t	pd_dma;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	int		nr_tlb_entries;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	void *ctx; /* iommu context: registres saved area */
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci	struct cr_regs *cr_ctx;
7762306a36Sopenharmony_ci	u32 num_cr_ctx;
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_ci	int has_bus_err_back;
8062306a36Sopenharmony_ci	u32 id;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	struct iommu_device iommu;
8362306a36Sopenharmony_ci	struct iommu_group *group;
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	u8 pwrst;
8662306a36Sopenharmony_ci};
8762306a36Sopenharmony_ci
8862306a36Sopenharmony_ci/**
8962306a36Sopenharmony_ci * struct omap_iommu_arch_data - omap iommu private data
9062306a36Sopenharmony_ci * @iommu_dev: handle of the OMAP iommu device
9162306a36Sopenharmony_ci * @dev: handle of the iommu device
9262306a36Sopenharmony_ci *
9362306a36Sopenharmony_ci * This is an omap iommu private data object, which binds an iommu user
9462306a36Sopenharmony_ci * to its iommu device. This object should be placed at the iommu user's
9562306a36Sopenharmony_ci * dev_archdata so generic IOMMU API can be used without having to
9662306a36Sopenharmony_ci * utilize omap-specific plumbing anymore.
9762306a36Sopenharmony_ci */
9862306a36Sopenharmony_cistruct omap_iommu_arch_data {
9962306a36Sopenharmony_ci	struct omap_iommu *iommu_dev;
10062306a36Sopenharmony_ci	struct device *dev;
10162306a36Sopenharmony_ci};
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_cistruct cr_regs {
10462306a36Sopenharmony_ci	u32 cam;
10562306a36Sopenharmony_ci	u32 ram;
10662306a36Sopenharmony_ci};
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cistruct iotlb_lock {
10962306a36Sopenharmony_ci	short base;
11062306a36Sopenharmony_ci	short vict;
11162306a36Sopenharmony_ci};
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci/*
11462306a36Sopenharmony_ci * MMU Register offsets
11562306a36Sopenharmony_ci */
11662306a36Sopenharmony_ci#define MMU_REVISION		0x00
11762306a36Sopenharmony_ci#define MMU_IRQSTATUS		0x18
11862306a36Sopenharmony_ci#define MMU_IRQENABLE		0x1c
11962306a36Sopenharmony_ci#define MMU_WALKING_ST		0x40
12062306a36Sopenharmony_ci#define MMU_CNTL		0x44
12162306a36Sopenharmony_ci#define MMU_FAULT_AD		0x48
12262306a36Sopenharmony_ci#define MMU_TTB			0x4c
12362306a36Sopenharmony_ci#define MMU_LOCK		0x50
12462306a36Sopenharmony_ci#define MMU_LD_TLB		0x54
12562306a36Sopenharmony_ci#define MMU_CAM			0x58
12662306a36Sopenharmony_ci#define MMU_RAM			0x5c
12762306a36Sopenharmony_ci#define MMU_GFLUSH		0x60
12862306a36Sopenharmony_ci#define MMU_FLUSH_ENTRY		0x64
12962306a36Sopenharmony_ci#define MMU_READ_CAM		0x68
13062306a36Sopenharmony_ci#define MMU_READ_RAM		0x6c
13162306a36Sopenharmony_ci#define MMU_EMU_FAULT_AD	0x70
13262306a36Sopenharmony_ci#define MMU_GP_REG		0x88
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci#define MMU_REG_SIZE		256
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci/*
13762306a36Sopenharmony_ci * MMU Register bit definitions
13862306a36Sopenharmony_ci */
13962306a36Sopenharmony_ci/* IRQSTATUS & IRQENABLE */
14062306a36Sopenharmony_ci#define MMU_IRQ_MULTIHITFAULT	BIT(4)
14162306a36Sopenharmony_ci#define MMU_IRQ_TABLEWALKFAULT	BIT(3)
14262306a36Sopenharmony_ci#define MMU_IRQ_EMUMISS		BIT(2)
14362306a36Sopenharmony_ci#define MMU_IRQ_TRANSLATIONFAULT	BIT(1)
14462306a36Sopenharmony_ci#define MMU_IRQ_TLBMISS		BIT(0)
14562306a36Sopenharmony_ci
14662306a36Sopenharmony_ci#define __MMU_IRQ_FAULT		\
14762306a36Sopenharmony_ci	(MMU_IRQ_MULTIHITFAULT | MMU_IRQ_EMUMISS | MMU_IRQ_TRANSLATIONFAULT)
14862306a36Sopenharmony_ci#define MMU_IRQ_MASK		\
14962306a36Sopenharmony_ci	(__MMU_IRQ_FAULT | MMU_IRQ_TABLEWALKFAULT | MMU_IRQ_TLBMISS)
15062306a36Sopenharmony_ci#define MMU_IRQ_TWL_MASK	(__MMU_IRQ_FAULT | MMU_IRQ_TABLEWALKFAULT)
15162306a36Sopenharmony_ci#define MMU_IRQ_TLB_MISS_MASK	(__MMU_IRQ_FAULT | MMU_IRQ_TLBMISS)
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci/* MMU_CNTL */
15462306a36Sopenharmony_ci#define MMU_CNTL_SHIFT		1
15562306a36Sopenharmony_ci#define MMU_CNTL_MASK		(7 << MMU_CNTL_SHIFT)
15662306a36Sopenharmony_ci#define MMU_CNTL_EML_TLB	BIT(3)
15762306a36Sopenharmony_ci#define MMU_CNTL_TWL_EN		BIT(2)
15862306a36Sopenharmony_ci#define MMU_CNTL_MMU_EN		BIT(1)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci/* CAM */
16162306a36Sopenharmony_ci#define MMU_CAM_VATAG_SHIFT	12
16262306a36Sopenharmony_ci#define MMU_CAM_VATAG_MASK \
16362306a36Sopenharmony_ci	((~0UL >> MMU_CAM_VATAG_SHIFT) << MMU_CAM_VATAG_SHIFT)
16462306a36Sopenharmony_ci#define MMU_CAM_P		BIT(3)
16562306a36Sopenharmony_ci#define MMU_CAM_V		BIT(2)
16662306a36Sopenharmony_ci#define MMU_CAM_PGSZ_MASK	3
16762306a36Sopenharmony_ci#define MMU_CAM_PGSZ_1M		(0 << 0)
16862306a36Sopenharmony_ci#define MMU_CAM_PGSZ_64K	(1 << 0)
16962306a36Sopenharmony_ci#define MMU_CAM_PGSZ_4K		(2 << 0)
17062306a36Sopenharmony_ci#define MMU_CAM_PGSZ_16M	(3 << 0)
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/* RAM */
17362306a36Sopenharmony_ci#define MMU_RAM_PADDR_SHIFT	12
17462306a36Sopenharmony_ci#define MMU_RAM_PADDR_MASK \
17562306a36Sopenharmony_ci	((~0UL >> MMU_RAM_PADDR_SHIFT) << MMU_RAM_PADDR_SHIFT)
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci#define MMU_RAM_ENDIAN_SHIFT	9
17862306a36Sopenharmony_ci#define MMU_RAM_ENDIAN_MASK	BIT(MMU_RAM_ENDIAN_SHIFT)
17962306a36Sopenharmony_ci#define MMU_RAM_ENDIAN_LITTLE	(0 << MMU_RAM_ENDIAN_SHIFT)
18062306a36Sopenharmony_ci#define MMU_RAM_ENDIAN_BIG	BIT(MMU_RAM_ENDIAN_SHIFT)
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci#define MMU_RAM_ELSZ_SHIFT	7
18362306a36Sopenharmony_ci#define MMU_RAM_ELSZ_MASK	(3 << MMU_RAM_ELSZ_SHIFT)
18462306a36Sopenharmony_ci#define MMU_RAM_ELSZ_8		(0 << MMU_RAM_ELSZ_SHIFT)
18562306a36Sopenharmony_ci#define MMU_RAM_ELSZ_16		(1 << MMU_RAM_ELSZ_SHIFT)
18662306a36Sopenharmony_ci#define MMU_RAM_ELSZ_32		(2 << MMU_RAM_ELSZ_SHIFT)
18762306a36Sopenharmony_ci#define MMU_RAM_ELSZ_NONE	(3 << MMU_RAM_ELSZ_SHIFT)
18862306a36Sopenharmony_ci#define MMU_RAM_MIXED_SHIFT	6
18962306a36Sopenharmony_ci#define MMU_RAM_MIXED_MASK	BIT(MMU_RAM_MIXED_SHIFT)
19062306a36Sopenharmony_ci#define MMU_RAM_MIXED		MMU_RAM_MIXED_MASK
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci#define MMU_GP_REG_BUS_ERR_BACK_EN	0x1
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci#define get_cam_va_mask(pgsz)				\
19562306a36Sopenharmony_ci	(((pgsz) == MMU_CAM_PGSZ_16M) ? 0xff000000 :	\
19662306a36Sopenharmony_ci	 ((pgsz) == MMU_CAM_PGSZ_1M)  ? 0xfff00000 :	\
19762306a36Sopenharmony_ci	 ((pgsz) == MMU_CAM_PGSZ_64K) ? 0xffff0000 :	\
19862306a36Sopenharmony_ci	 ((pgsz) == MMU_CAM_PGSZ_4K)  ? 0xfffff000 : 0)
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci/*
20162306a36Sopenharmony_ci * DSP_SYSTEM registers and bit definitions (applicable only for DRA7xx DSP)
20262306a36Sopenharmony_ci */
20362306a36Sopenharmony_ci#define DSP_SYS_REVISION		0x00
20462306a36Sopenharmony_ci#define DSP_SYS_MMU_CONFIG		0x18
20562306a36Sopenharmony_ci#define DSP_SYS_MMU_CONFIG_EN_SHIFT	4
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci/*
20862306a36Sopenharmony_ci * utilities for super page(16MB, 1MB, 64KB and 4KB)
20962306a36Sopenharmony_ci */
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci#define iopgsz_max(bytes)			\
21262306a36Sopenharmony_ci	(((bytes) >= SZ_16M) ? SZ_16M :		\
21362306a36Sopenharmony_ci	 ((bytes) >= SZ_1M)  ? SZ_1M  :		\
21462306a36Sopenharmony_ci	 ((bytes) >= SZ_64K) ? SZ_64K :		\
21562306a36Sopenharmony_ci	 ((bytes) >= SZ_4K)  ? SZ_4K  :	0)
21662306a36Sopenharmony_ci
21762306a36Sopenharmony_ci#define bytes_to_iopgsz(bytes)				\
21862306a36Sopenharmony_ci	(((bytes) == SZ_16M) ? MMU_CAM_PGSZ_16M :	\
21962306a36Sopenharmony_ci	 ((bytes) == SZ_1M)  ? MMU_CAM_PGSZ_1M  :	\
22062306a36Sopenharmony_ci	 ((bytes) == SZ_64K) ? MMU_CAM_PGSZ_64K :	\
22162306a36Sopenharmony_ci	 ((bytes) == SZ_4K)  ? MMU_CAM_PGSZ_4K  : -1)
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci#define iopgsz_to_bytes(iopgsz)				\
22462306a36Sopenharmony_ci	(((iopgsz) == MMU_CAM_PGSZ_16M)	? SZ_16M :	\
22562306a36Sopenharmony_ci	 ((iopgsz) == MMU_CAM_PGSZ_1M)	? SZ_1M  :	\
22662306a36Sopenharmony_ci	 ((iopgsz) == MMU_CAM_PGSZ_64K)	? SZ_64K :	\
22762306a36Sopenharmony_ci	 ((iopgsz) == MMU_CAM_PGSZ_4K)	? SZ_4K  : 0)
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci#define iopgsz_ok(bytes) (bytes_to_iopgsz(bytes) >= 0)
23062306a36Sopenharmony_ci
23162306a36Sopenharmony_ci/*
23262306a36Sopenharmony_ci * global functions
23362306a36Sopenharmony_ci */
23462306a36Sopenharmony_ci
23562306a36Sopenharmony_cistruct cr_regs __iotlb_read_cr(struct omap_iommu *obj, int n);
23662306a36Sopenharmony_civoid iotlb_lock_get(struct omap_iommu *obj, struct iotlb_lock *l);
23762306a36Sopenharmony_civoid iotlb_lock_set(struct omap_iommu *obj, struct iotlb_lock *l);
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci#ifdef CONFIG_OMAP_IOMMU_DEBUG
24062306a36Sopenharmony_civoid omap_iommu_debugfs_init(void);
24162306a36Sopenharmony_civoid omap_iommu_debugfs_exit(void);
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_civoid omap_iommu_debugfs_add(struct omap_iommu *obj);
24462306a36Sopenharmony_civoid omap_iommu_debugfs_remove(struct omap_iommu *obj);
24562306a36Sopenharmony_ci#else
24662306a36Sopenharmony_cistatic inline void omap_iommu_debugfs_init(void) { }
24762306a36Sopenharmony_cistatic inline void omap_iommu_debugfs_exit(void) { }
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_cistatic inline void omap_iommu_debugfs_add(struct omap_iommu *obj) { }
25062306a36Sopenharmony_cistatic inline void omap_iommu_debugfs_remove(struct omap_iommu *obj) { }
25162306a36Sopenharmony_ci#endif
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci/*
25462306a36Sopenharmony_ci * register accessors
25562306a36Sopenharmony_ci */
25662306a36Sopenharmony_cistatic inline u32 iommu_read_reg(struct omap_iommu *obj, size_t offs)
25762306a36Sopenharmony_ci{
25862306a36Sopenharmony_ci	return __raw_readl(obj->regbase + offs);
25962306a36Sopenharmony_ci}
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_cistatic inline void iommu_write_reg(struct omap_iommu *obj, u32 val, size_t offs)
26262306a36Sopenharmony_ci{
26362306a36Sopenharmony_ci	__raw_writel(val, obj->regbase + offs);
26462306a36Sopenharmony_ci}
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_cistatic inline int iotlb_cr_valid(struct cr_regs *cr)
26762306a36Sopenharmony_ci{
26862306a36Sopenharmony_ci	if (!cr)
26962306a36Sopenharmony_ci		return -EINVAL;
27062306a36Sopenharmony_ci
27162306a36Sopenharmony_ci	return cr->cam & MMU_CAM_V;
27262306a36Sopenharmony_ci}
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci#endif /* _OMAP_IOMMU_H */
275