1f6ffbd4fSLucas Stach /* SPDX-License-Identifier: GPL-2.0 */
2a8c21a54SThe etnaviv authors /*
3f6ffbd4fSLucas Stach  * Copyright (C) 2015-2018 Etnaviv Project
4a8c21a54SThe etnaviv authors  */
5a8c21a54SThe etnaviv authors 
6a8c21a54SThe etnaviv authors #ifndef __ETNAVIV_MMU_H__
7a8c21a54SThe etnaviv authors #define __ETNAVIV_MMU_H__
8a8c21a54SThe etnaviv authors 
9b6709083SLucas Stach #define ETNAVIV_PROT_READ	(1 << 0)
10b6709083SLucas Stach #define ETNAVIV_PROT_WRITE	(1 << 1)
11a8c21a54SThe etnaviv authors 
12a8c21a54SThe etnaviv authors enum etnaviv_iommu_version {
13a8c21a54SThe etnaviv authors 	ETNAVIV_IOMMU_V1 = 0,
14a8c21a54SThe etnaviv authors 	ETNAVIV_IOMMU_V2,
15a8c21a54SThe etnaviv authors };
16a8c21a54SThe etnaviv authors 
17a8c21a54SThe etnaviv authors struct etnaviv_gpu;
18a8c21a54SThe etnaviv authors struct etnaviv_vram_mapping;
1927b67278SLucas Stach struct etnaviv_iommu_global;
2027b67278SLucas Stach struct etnaviv_iommu_context;
21a8c21a54SThe etnaviv authors 
2227b67278SLucas Stach struct etnaviv_iommu_ops {
2327b67278SLucas Stach 	struct etnaviv_iommu_context *(*init)(struct etnaviv_iommu_global *);
2427b67278SLucas Stach 	void (*free)(struct etnaviv_iommu_context *);
2527b67278SLucas Stach 	int (*map)(struct etnaviv_iommu_context *context, unsigned long iova,
26b6709083SLucas Stach 		   phys_addr_t paddr, size_t size, int prot);
2727b67278SLucas Stach 	size_t (*unmap)(struct etnaviv_iommu_context *context, unsigned long iova,
28b6709083SLucas Stach 			size_t size);
2927b67278SLucas Stach 	size_t (*dump_size)(struct etnaviv_iommu_context *);
3027b67278SLucas Stach 	void (*dump)(struct etnaviv_iommu_context *, void *);
3127b67278SLucas Stach 	void (*restore)(struct etnaviv_gpu *, struct etnaviv_iommu_context *);
32b6709083SLucas Stach };
33b6709083SLucas Stach 
3427b67278SLucas Stach extern const struct etnaviv_iommu_ops etnaviv_iommuv1_ops;
3527b67278SLucas Stach extern const struct etnaviv_iommu_ops etnaviv_iommuv2_ops;
3627b67278SLucas Stach 
3727b67278SLucas Stach #define ETNAVIV_PTA_SIZE	SZ_4K
3827b67278SLucas Stach #define ETNAVIV_PTA_ENTRIES	(ETNAVIV_PTA_SIZE / sizeof(u64))
3927b67278SLucas Stach 
4027b67278SLucas Stach struct etnaviv_iommu_global {
41b6709083SLucas Stach 	struct device *dev;
4227b67278SLucas Stach 	enum etnaviv_iommu_version version;
4327b67278SLucas Stach 	const struct etnaviv_iommu_ops *ops;
4427b67278SLucas Stach 	unsigned int use;
4527b67278SLucas Stach 	struct mutex lock;
4627b67278SLucas Stach 
47b6709083SLucas Stach 	void *bad_page_cpu;
48b6709083SLucas Stach 	dma_addr_t bad_page_dma;
49b6709083SLucas Stach 
5017e4660aSLucas Stach 	u32 memory_base;
5117e4660aSLucas Stach 
5227b67278SLucas Stach 	/*
5327b67278SLucas Stach 	 * This union holds members needed by either MMUv1 or MMUv2, which
5427b67278SLucas Stach 	 * can not exist at the same time.
5527b67278SLucas Stach 	 */
5627b67278SLucas Stach 	union {
5727b67278SLucas Stach 		struct {
5827b67278SLucas Stach 			struct etnaviv_iommu_context *shared_context;
5927b67278SLucas Stach 		} v1;
6027b67278SLucas Stach 		struct {
6127b67278SLucas Stach 			/* P(age) T(able) A(rray) */
6227b67278SLucas Stach 			u64 *pta_cpu;
6327b67278SLucas Stach 			dma_addr_t pta_dma;
6427b67278SLucas Stach 			struct spinlock pta_lock;
6527b67278SLucas Stach 			DECLARE_BITMAP(pta_alloc, ETNAVIV_PTA_ENTRIES);
6627b67278SLucas Stach 		} v2;
6727b67278SLucas Stach 	};
68a8c21a54SThe etnaviv authors };
69a8c21a54SThe etnaviv authors 
7027b67278SLucas Stach struct etnaviv_iommu_context {
7127b67278SLucas Stach 	struct kref refcount;
7227b67278SLucas Stach 	struct etnaviv_iommu_global *global;
73a8c21a54SThe etnaviv authors 
74a8c21a54SThe etnaviv authors 	/* memory manager for GPU address area */
75a8c21a54SThe etnaviv authors 	struct mutex lock;
76a8c21a54SThe etnaviv authors 	struct list_head mappings;
77a8c21a54SThe etnaviv authors 	struct drm_mm mm;
784900dda9SLucas Stach 	unsigned int flush_seq;
7917e4660aSLucas Stach 
8017e4660aSLucas Stach 	/* Not part of the context, but needs to have the same lifetime */
8117e4660aSLucas Stach 	struct etnaviv_vram_mapping cmdbuf_mapping;
82a8c21a54SThe etnaviv authors };
83a8c21a54SThe etnaviv authors 
8427b67278SLucas Stach int etnaviv_iommu_global_init(struct etnaviv_gpu *gpu);
8527b67278SLucas Stach void etnaviv_iommu_global_fini(struct etnaviv_gpu *gpu);
8627b67278SLucas Stach 
87a8c21a54SThe etnaviv authors struct etnaviv_gem_object;
88a8c21a54SThe etnaviv authors 
8927b67278SLucas Stach int etnaviv_iommu_map_gem(struct etnaviv_iommu_context *context,
90a8c21a54SThe etnaviv authors 	struct etnaviv_gem_object *etnaviv_obj, u32 memory_base,
9117eae23bSLucas Stach 	struct etnaviv_vram_mapping *mapping, u64 va);
9227b67278SLucas Stach void etnaviv_iommu_unmap_gem(struct etnaviv_iommu_context *context,
93a8c21a54SThe etnaviv authors 	struct etnaviv_vram_mapping *mapping);
94*5a40837dSLucas Stach void etnaviv_iommu_reap_mapping(struct etnaviv_vram_mapping *mapping);
95a8c21a54SThe etnaviv authors 
9627b67278SLucas Stach int etnaviv_iommu_get_suballoc_va(struct etnaviv_iommu_context *ctx,
97db82a043SLucas Stach 				  struct etnaviv_vram_mapping *mapping,
98db82a043SLucas Stach 				  u32 memory_base, dma_addr_t paddr,
99db82a043SLucas Stach 				  size_t size);
10027b67278SLucas Stach void etnaviv_iommu_put_suballoc_va(struct etnaviv_iommu_context *ctx,
101db82a043SLucas Stach 				   struct etnaviv_vram_mapping *mapping);
102e07c0db5SLucas Stach 
10327b67278SLucas Stach size_t etnaviv_iommu_dump_size(struct etnaviv_iommu_context *ctx);
10427b67278SLucas Stach void etnaviv_iommu_dump(struct etnaviv_iommu_context *ctx, void *buf);
105a8c21a54SThe etnaviv authors 
10627b67278SLucas Stach struct etnaviv_iommu_context *
10717e4660aSLucas Stach etnaviv_iommu_context_init(struct etnaviv_iommu_global *global,
10817e4660aSLucas Stach 			   struct etnaviv_cmdbuf_suballoc *suballoc);
10978edefc0SLucas Stach static inline struct etnaviv_iommu_context *
etnaviv_iommu_context_get(struct etnaviv_iommu_context * ctx)11078edefc0SLucas Stach etnaviv_iommu_context_get(struct etnaviv_iommu_context *ctx)
11127b67278SLucas Stach {
11227b67278SLucas Stach 	kref_get(&ctx->refcount);
11378edefc0SLucas Stach 	return ctx;
11427b67278SLucas Stach }
11527b67278SLucas Stach void etnaviv_iommu_context_put(struct etnaviv_iommu_context *ctx);
11627b67278SLucas Stach void etnaviv_iommu_restore(struct etnaviv_gpu *gpu,
11727b67278SLucas Stach 			   struct etnaviv_iommu_context *ctx);
11827b67278SLucas Stach 
11927b67278SLucas Stach struct etnaviv_iommu_context *
12027b67278SLucas Stach etnaviv_iommuv1_context_alloc(struct etnaviv_iommu_global *global);
12127b67278SLucas Stach struct etnaviv_iommu_context *
12227b67278SLucas Stach etnaviv_iommuv2_context_alloc(struct etnaviv_iommu_global *global);
123a8c21a54SThe etnaviv authors 
12417e4660aSLucas Stach u32 etnaviv_iommuv2_get_mtlb_addr(struct etnaviv_iommu_context *context);
12517e4660aSLucas Stach unsigned short etnaviv_iommuv2_get_pta_id(struct etnaviv_iommu_context *context);
12617e4660aSLucas Stach 
127a8c21a54SThe etnaviv authors #endif /* __ETNAVIV_MMU_H__ */
128