blob: 6337a48633b9deb623c199cd27a5f14556b7f48c [file] [log] [blame]
Shrenuj Bansala419c792016-10-20 14:05:11 -07001/* Copyright (c) 2012-2017, The Linux Foundation. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13#ifndef __KGSL_IOMMU_H
14#define __KGSL_IOMMU_H
15
16#ifdef CONFIG_QCOM_IOMMU
17#include <linux/qcom_iommu.h>
18#endif
19#include <linux/of.h>
20#include "kgsl.h"
21
22/*
23 * These defines control the address range for allocations that
24 * are mapped into all pagetables.
25 */
26#define KGSL_IOMMU_GLOBAL_MEM_SIZE SZ_8M
27#define KGSL_IOMMU_GLOBAL_MEM_BASE 0xf8000000
28
29#define KGSL_IOMMU_SECURE_SIZE SZ_256M
30#define KGSL_IOMMU_SECURE_END KGSL_IOMMU_GLOBAL_MEM_BASE
31#define KGSL_IOMMU_SECURE_BASE \
32 (KGSL_IOMMU_GLOBAL_MEM_BASE - KGSL_IOMMU_SECURE_SIZE)
33
34#define KGSL_IOMMU_SVM_BASE32 0x300000
35#define KGSL_IOMMU_SVM_END32 (0xC0000000 - SZ_16M)
36
37#define KGSL_IOMMU_VA_BASE64 0x500000000ULL
38#define KGSL_IOMMU_VA_END64 0x600000000ULL
39/*
40 * Note: currently we only support 36 bit addresses,
41 * but the CPU supports 39. Eventually this range
42 * should change to high part of the 39 bit address
43 * space just like the CPU.
44 */
45#define KGSL_IOMMU_SVM_BASE64 0x700000000ULL
46#define KGSL_IOMMU_SVM_END64 0x800000000ULL
47
48/* TLBSTATUS register fields */
49#define KGSL_IOMMU_CTX_TLBSTATUS_SACTIVE BIT(0)
50
51/* IMPLDEF_MICRO_MMU_CTRL register fields */
52#define KGSL_IOMMU_IMPLDEF_MICRO_MMU_CTRL_HALT 0x00000004
53#define KGSL_IOMMU_IMPLDEF_MICRO_MMU_CTRL_IDLE 0x00000008
54
55/* SCTLR fields */
56#define KGSL_IOMMU_SCTLR_HUPCF_SHIFT 8
57#define KGSL_IOMMU_SCTLR_CFCFG_SHIFT 7
58#define KGSL_IOMMU_SCTLR_CFIE_SHIFT 6
59
60enum kgsl_iommu_reg_map {
61 KGSL_IOMMU_CTX_SCTLR = 0,
62 KGSL_IOMMU_CTX_TTBR0,
63 KGSL_IOMMU_CTX_CONTEXTIDR,
64 KGSL_IOMMU_CTX_FSR,
65 KGSL_IOMMU_CTX_FAR,
66 KGSL_IOMMU_CTX_TLBIALL,
67 KGSL_IOMMU_CTX_RESUME,
68 KGSL_IOMMU_CTX_FSYNR0,
69 KGSL_IOMMU_CTX_FSYNR1,
70 KGSL_IOMMU_CTX_TLBSYNC,
71 KGSL_IOMMU_CTX_TLBSTATUS,
72 KGSL_IOMMU_REG_MAX
73};
74
75/* Max number of iommu clks per IOMMU unit */
76#define KGSL_IOMMU_MAX_CLKS 5
77
78enum kgsl_iommu_context_id {
79 KGSL_IOMMU_CONTEXT_USER = 0,
80 KGSL_IOMMU_CONTEXT_SECURE = 1,
81 KGSL_IOMMU_CONTEXT_MAX,
82};
83
84/* offset at which a nop command is placed in setstate */
85#define KGSL_IOMMU_SETSTATE_NOP_OFFSET 1024
86
87/*
88 * struct kgsl_iommu_context - Structure holding data about an iommu context
89 * bank
90 * @dev: pointer to the iommu context's device
91 * @name: context name
92 * @id: The id of the context, used for deciding how it is used.
93 * @cb_num: The hardware context bank number, used for calculating register
94 * offsets.
95 * @kgsldev: The kgsl device that uses this context.
96 * @fault: Flag when set indicates that this iommu device has caused a page
97 * fault
98 * @gpu_offset: Offset of this context bank in the GPU register space
99 * @default_pt: The default pagetable for this context,
100 * it may be changed by self programming.
101 */
102struct kgsl_iommu_context {
103 struct device *dev;
104 const char *name;
105 enum kgsl_iommu_context_id id;
106 unsigned int cb_num;
107 struct kgsl_device *kgsldev;
108 int fault;
109 void __iomem *regbase;
110 unsigned int gpu_offset;
111 struct kgsl_pagetable *default_pt;
112};
113
114/*
115 * struct kgsl_iommu - Structure holding iommu data for kgsl driver
116 * @ctx: Array of kgsl_iommu_context structs
117 * @regbase: Virtual address of the IOMMU register base
118 * @regstart: Physical address of the iommu registers
119 * @regsize: Length of the iommu register region.
120 * @setstate: Scratch GPU memory for IOMMU operations
121 * @clk_enable_count: The ref count of clock enable calls
122 * @clks: Array of pointers to IOMMU clocks
123 * @micro_mmu_ctrl: GPU register offset of this glob al register
124 * @smmu_info: smmu info used in a5xx preemption
125 * @protect: register protection settings for the iommu.
126 * @pagefault_suppression_count: Total number of pagefaults
127 * suppressed since boot.
128 */
129struct kgsl_iommu {
130 struct kgsl_iommu_context ctx[KGSL_IOMMU_CONTEXT_MAX];
131 void __iomem *regbase;
132 unsigned long regstart;
133 unsigned int regsize;
134 struct kgsl_memdesc setstate;
135 atomic_t clk_enable_count;
136 struct clk *clks[KGSL_IOMMU_MAX_CLKS];
137 unsigned int micro_mmu_ctrl;
138 struct kgsl_memdesc smmu_info;
139 unsigned int version;
140 struct kgsl_protected_registers protect;
141 u32 pagefault_suppression_count;
142};
143
144/*
145 * struct kgsl_iommu_pt - Iommu pagetable structure private to kgsl driver
146 * @domain: Pointer to the iommu domain that contains the iommu pagetable
147 * @ttbr0: register value to set when using this pagetable
148 * @contextidr: register value to set when using this pagetable
149 * @attached: is the pagetable attached?
150 * @rbtree: all buffers mapped into the pagetable, indexed by gpuaddr
151 * @va_start: Start of virtual range used in this pagetable.
152 * @va_end: End of virtual range.
153 * @svm_start: Start of shared virtual memory range. Addresses in this
154 * range are also valid in the process's CPU address space.
155 * @svm_end: End of the shared virtual memory range.
156 * @svm_start: 32 bit compatible range, for old clients who lack bits
157 * @svm_end: end of 32 bit compatible range
158 */
159struct kgsl_iommu_pt {
160 struct iommu_domain *domain;
161 u64 ttbr0;
162 u32 contextidr;
163 bool attached;
164
165 struct rb_root rbtree;
166
167 uint64_t va_start;
168 uint64_t va_end;
169 uint64_t svm_start;
170 uint64_t svm_end;
171 uint64_t compat_va_start;
172 uint64_t compat_va_end;
173};
174
175/*
176 * offset of context bank 0 from the start of the SMMU register space.
177 */
178#define KGSL_IOMMU_CB0_OFFSET 0x8000
179/* size of each context bank's register space */
180#define KGSL_IOMMU_CB_SHIFT 12
181
182/* Macros to read/write IOMMU registers */
183extern const unsigned int kgsl_iommu_reg_list[KGSL_IOMMU_REG_MAX];
184
185/*
186 * Don't use this function directly. Use the macros below to read/write
187 * IOMMU registers.
188 */
189static inline void __iomem *
190kgsl_iommu_reg(struct kgsl_iommu_context *ctx, enum kgsl_iommu_reg_map reg)
191{
192 return ctx->regbase + kgsl_iommu_reg_list[reg];
193}
194
195#define KGSL_IOMMU_SET_CTX_REG_Q(_ctx, REG, val) \
196 writeq_relaxed((val), \
197 kgsl_iommu_reg((_ctx), KGSL_IOMMU_CTX_##REG))
198
199#define KGSL_IOMMU_GET_CTX_REG_Q(_ctx, REG) \
200 readq_relaxed(kgsl_iommu_reg((_ctx), KGSL_IOMMU_CTX_##REG))
201
202#define KGSL_IOMMU_SET_CTX_REG(_ctx, REG, val) \
203 writel_relaxed((val), \
204 kgsl_iommu_reg((_ctx), KGSL_IOMMU_CTX_##REG))
205
206#define KGSL_IOMMU_GET_CTX_REG(_ctx, REG) \
207 readl_relaxed(kgsl_iommu_reg((_ctx), KGSL_IOMMU_CTX_##REG))
208
209
210#endif