blob: 0865978c05dc92f38a249a4af77b2fd10eed664a [file] [log] [blame]
Jordan Crouse6d76c4d2012-03-26 09:50:43 -06001/* Copyright (c) 2002,2007-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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_MMU_H
14#define __KGSL_MMU_H
15
Shubhraprakash Das84fdb112012-04-04 12:49:31 -060016/*
17 * These defines control the split between ttbr1 and ttbr0 pagetables of IOMMU
18 * and what ranges of memory we map to them
19 */
20#define KGSL_IOMMU_GLOBAL_MEM_BASE 0xC0000000
21#define KGSL_IOMMU_GLOBAL_MEM_SIZE SZ_4M
22#define KGSL_IOMMU_TTBR1_SPLIT 2
23
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060024#define KGSL_MMU_ALIGN_SHIFT 13
25#define KGSL_MMU_ALIGN_MASK (~((1 << KGSL_MMU_ALIGN_SHIFT) - 1))
26
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070027/* Identifier for the global page table */
28/* Per process page tables will probably pass in the thread group
29 as an identifier */
30
31#define KGSL_MMU_GLOBAL_PT 0
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -060032#define KGSL_MMU_PRIV_BANK_TABLE_NAME 0xFFFFFFFF
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060034struct kgsl_device;
35
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070036#define GSL_PT_SUPER_PTE 8
37#define GSL_PT_PAGE_WV 0x00000001
38#define GSL_PT_PAGE_RV 0x00000002
39#define GSL_PT_PAGE_DIRTY 0x00000004
40
41/* MMU registers - the register locations for all cores are the
42 same. The method for getting to those locations differs between
43 2D and 3D, but the 2D and 3D register functions do that magic
44 for us */
45
46#define MH_MMU_CONFIG 0x0040
47#define MH_MMU_VA_RANGE 0x0041
48#define MH_MMU_PT_BASE 0x0042
49#define MH_MMU_PAGE_FAULT 0x0043
50#define MH_MMU_TRAN_ERROR 0x0044
51#define MH_MMU_INVALIDATE 0x0045
52#define MH_MMU_MPU_BASE 0x0046
53#define MH_MMU_MPU_END 0x0047
54
55#define MH_INTERRUPT_MASK 0x0A42
56#define MH_INTERRUPT_STATUS 0x0A43
57#define MH_INTERRUPT_CLEAR 0x0A44
58#define MH_AXI_ERROR 0x0A45
Jeremy Gebben4e8aada2011-07-12 10:07:47 -060059#define MH_ARBITER_CONFIG 0x0A40
60#define MH_DEBUG_CTRL 0x0A4E
61#define MH_DEBUG_DATA 0x0A4F
62#define MH_AXI_HALT_CONTROL 0x0A50
63#define MH_CLNT_INTF_CTRL_CONFIG1 0x0A54
64#define MH_CLNT_INTF_CTRL_CONFIG2 0x0A55
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070065
66/* MH_MMU_CONFIG bit definitions */
67
68#define MH_MMU_CONFIG__RB_W_CLNT_BEHAVIOR__SHIFT 0x00000004
69#define MH_MMU_CONFIG__CP_W_CLNT_BEHAVIOR__SHIFT 0x00000006
70#define MH_MMU_CONFIG__CP_R0_CLNT_BEHAVIOR__SHIFT 0x00000008
71#define MH_MMU_CONFIG__CP_R1_CLNT_BEHAVIOR__SHIFT 0x0000000a
72#define MH_MMU_CONFIG__CP_R2_CLNT_BEHAVIOR__SHIFT 0x0000000c
73#define MH_MMU_CONFIG__CP_R3_CLNT_BEHAVIOR__SHIFT 0x0000000e
74#define MH_MMU_CONFIG__CP_R4_CLNT_BEHAVIOR__SHIFT 0x00000010
75#define MH_MMU_CONFIG__VGT_R0_CLNT_BEHAVIOR__SHIFT 0x00000012
76#define MH_MMU_CONFIG__VGT_R1_CLNT_BEHAVIOR__SHIFT 0x00000014
77#define MH_MMU_CONFIG__TC_R_CLNT_BEHAVIOR__SHIFT 0x00000016
78#define MH_MMU_CONFIG__PA_W_CLNT_BEHAVIOR__SHIFT 0x00000018
79
80/* MMU Flags */
81#define KGSL_MMUFLAGS_TLBFLUSH 0x10000000
82#define KGSL_MMUFLAGS_PTUPDATE 0x20000000
83
84#define MH_INTERRUPT_MASK__AXI_READ_ERROR 0x00000001L
85#define MH_INTERRUPT_MASK__AXI_WRITE_ERROR 0x00000002L
86#define MH_INTERRUPT_MASK__MMU_PAGE_FAULT 0x00000004L
87
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070088#define KGSL_MMU_INT_MASK \
89 (MH_INTERRUPT_MASK__AXI_READ_ERROR | \
90 MH_INTERRUPT_MASK__AXI_WRITE_ERROR | \
91 MH_INTERRUPT_MASK__MMU_PAGE_FAULT)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070092
Shubhraprakash Das767fdda2011-08-15 15:49:45 -060093enum kgsl_mmutype {
94 KGSL_MMU_TYPE_GPU = 0,
95 KGSL_MMU_TYPE_IOMMU,
96 KGSL_MMU_TYPE_NONE
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070097};
98
99struct kgsl_pagetable {
100 spinlock_t lock;
101 struct kref refcount;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700102 unsigned int max_entries;
103 struct gen_pool *pool;
Shubhraprakash Das84fdb112012-04-04 12:49:31 -0600104 struct gen_pool *kgsl_pool;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700105 struct list_head list;
106 unsigned int name;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700107 struct kobject *kobj;
108
109 struct {
110 unsigned int entries;
111 unsigned int mapped;
112 unsigned int max_mapped;
113 unsigned int max_entries;
114 } stats;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600115 const struct kgsl_mmu_pt_ops *pt_ops;
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600116 unsigned int tlb_flags;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600117 void *priv;
118};
119
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600120struct kgsl_mmu;
121
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600122struct kgsl_mmu_ops {
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600123 int (*mmu_init) (struct kgsl_mmu *mmu);
124 int (*mmu_close) (struct kgsl_mmu *mmu);
125 int (*mmu_start) (struct kgsl_mmu *mmu);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600126 void (*mmu_stop) (struct kgsl_mmu *mmu);
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600127 void (*mmu_setstate) (struct kgsl_mmu *mmu,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600128 struct kgsl_pagetable *pagetable,
129 unsigned int context_id);
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600130 void (*mmu_device_setstate) (struct kgsl_mmu *mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600131 uint32_t flags);
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600132 void (*mmu_pagefault) (struct kgsl_mmu *mmu);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600133 unsigned int (*mmu_get_current_ptbase)
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600134 (struct kgsl_mmu *mmu);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600135 void (*mmu_disable_clk_on_ts)
136 (struct kgsl_mmu *mmu, uint32_t ts, bool ts_valid);
Shubhraprakash Das9fb38ac2012-05-01 00:41:30 -0600137 int (*mmu_enable_clk)
138 (struct kgsl_mmu *mmu, int ctx_id);
Shubhraprakash Dasfce27362012-05-09 17:44:14 -0600139 int (*mmu_get_pt_lsb)(struct kgsl_mmu *mmu,
140 unsigned int unit_id,
141 enum kgsl_iommu_context_id ctx_id);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700142 unsigned int (*mmu_get_reg_gpuaddr)(struct kgsl_mmu *mmu,
143 int iommu_unit_num, int ctx_id, int reg);
144 int (*mmu_get_num_iommu_units)(struct kgsl_mmu *mmu);
145 int (*mmu_pt_equal) (struct kgsl_mmu *mmu,
146 struct kgsl_pagetable *pt,
147 unsigned int pt_base);
148 unsigned int (*mmu_get_pt_base_addr)
149 (struct kgsl_mmu *mmu,
150 struct kgsl_pagetable *pt);
Tarun Karra9c070822012-11-27 16:43:51 -0700151 unsigned int (*mmu_sync_lock)
152 (struct kgsl_mmu *mmu,
153 unsigned int *cmds);
154 unsigned int (*mmu_sync_unlock)
155 (struct kgsl_mmu *mmu,
156 unsigned int *cmds);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600157};
158
159struct kgsl_mmu_pt_ops {
160 int (*mmu_map) (void *mmu_pt,
161 struct kgsl_memdesc *memdesc,
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600162 unsigned int protflags,
163 unsigned int *tlb_flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600164 int (*mmu_unmap) (void *mmu_pt,
Shubhraprakash Das0c811262012-06-06 23:22:19 -0600165 struct kgsl_memdesc *memdesc,
166 unsigned int *tlb_flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600167 void *(*mmu_create_pagetable) (void);
168 void (*mmu_destroy_pagetable) (void *pt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700169};
170
Tarun Karra9c070822012-11-27 16:43:51 -0700171#define KGSL_MMU_FLAGS_IOMMU_SYNC BIT(31)
172
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173struct kgsl_mmu {
174 unsigned int refcnt;
175 uint32_t flags;
176 struct kgsl_device *device;
177 unsigned int config;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600178 struct kgsl_memdesc setstate_memory;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700179 /* current page table object being used by device mmu */
180 struct kgsl_pagetable *defaultpagetable;
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600181 /* pagetable object used for priv bank of IOMMU */
182 struct kgsl_pagetable *priv_bank_table;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700183 struct kgsl_pagetable *hwpagetable;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600184 const struct kgsl_mmu_ops *mmu_ops;
185 void *priv;
Shubhraprakash Das2747cf62012-09-27 23:05:43 -0700186 int fault;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187};
188
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600189#include "kgsl_gpummu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700190
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600191extern struct kgsl_mmu_ops iommu_ops;
192extern struct kgsl_mmu_pt_ops iommu_pt_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700193
194struct kgsl_pagetable *kgsl_mmu_getpagetable(unsigned long name);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600195void kgsl_mmu_putpagetable(struct kgsl_pagetable *pagetable);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600196void kgsl_mh_start(struct kgsl_device *device);
197void kgsl_mh_intrcallback(struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198int kgsl_mmu_init(struct kgsl_device *device);
199int kgsl_mmu_start(struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200int kgsl_mmu_close(struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201int kgsl_mmu_map(struct kgsl_pagetable *pagetable,
202 struct kgsl_memdesc *memdesc,
203 unsigned int protflags);
204int kgsl_mmu_map_global(struct kgsl_pagetable *pagetable,
205 struct kgsl_memdesc *memdesc, unsigned int protflags);
206int kgsl_mmu_unmap(struct kgsl_pagetable *pagetable,
207 struct kgsl_memdesc *memdesc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700208unsigned int kgsl_virtaddr_to_physaddr(void *virtaddr);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600209void kgsl_setstate(struct kgsl_mmu *mmu, unsigned int context_id,
210 uint32_t flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700211int kgsl_mmu_get_ptname_from_ptbase(struct kgsl_mmu *mmu,
212 unsigned int pt_base);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600213int kgsl_mmu_pt_get_flags(struct kgsl_pagetable *pt,
214 enum kgsl_deviceid id);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600215void kgsl_mmu_ptpool_destroy(void *ptpool);
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600216void *kgsl_mmu_ptpool_init(int entries);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600217int kgsl_mmu_enabled(void);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600218void kgsl_mmu_set_mmutype(char *mmutype);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600219enum kgsl_mmutype kgsl_mmu_get_mmutype(void);
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600220unsigned int kgsl_mmu_get_ptsize(void);
Shubhraprakash Dase7652cf2012-08-11 17:15:19 -0700221int kgsl_mmu_gpuaddr_in_range(unsigned int gpuaddr);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600222
223/*
224 * Static inline functions of MMU that simply call the SMMU specific
225 * function using a function pointer. These functions can be thought
226 * of as wrappers around the actual function
227 */
228
229static inline unsigned int kgsl_mmu_get_current_ptbase(struct kgsl_mmu *mmu)
230{
231 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_current_ptbase)
232 return mmu->mmu_ops->mmu_get_current_ptbase(mmu);
233 else
234 return 0;
235}
236
237static inline void kgsl_mmu_setstate(struct kgsl_mmu *mmu,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600238 struct kgsl_pagetable *pagetable,
239 unsigned int context_id)
Shubhraprakash Das79447952012-04-26 18:12:23 -0600240{
241 if (mmu->mmu_ops && mmu->mmu_ops->mmu_setstate)
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600242 mmu->mmu_ops->mmu_setstate(mmu, pagetable, context_id);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600243}
244
245static inline void kgsl_mmu_device_setstate(struct kgsl_mmu *mmu,
246 uint32_t flags)
247{
248 if (mmu->mmu_ops && mmu->mmu_ops->mmu_device_setstate)
249 mmu->mmu_ops->mmu_device_setstate(mmu, flags);
250}
251
252static inline void kgsl_mmu_stop(struct kgsl_mmu *mmu)
253{
254 if (mmu->mmu_ops && mmu->mmu_ops->mmu_stop)
255 mmu->mmu_ops->mmu_stop(mmu);
256}
257
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700258static inline int kgsl_mmu_pt_equal(struct kgsl_mmu *mmu,
259 struct kgsl_pagetable *pt,
Shubhraprakash Das79447952012-04-26 18:12:23 -0600260 unsigned int pt_base)
261{
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700262 if (mmu->mmu_ops && mmu->mmu_ops->mmu_pt_equal)
263 return mmu->mmu_ops->mmu_pt_equal(mmu, pt, pt_base);
264 else
Shubhraprakash Das79447952012-04-26 18:12:23 -0600265 return 1;
Shubhraprakash Das79447952012-04-26 18:12:23 -0600266}
267
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700268static inline unsigned int kgsl_mmu_get_pt_base_addr(struct kgsl_mmu *mmu,
269 struct kgsl_pagetable *pt)
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600270{
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700271 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_pt_base_addr)
272 return mmu->mmu_ops->mmu_get_pt_base_addr(mmu, pt);
Shubhraprakash Dasa5b1db42012-05-09 18:02:34 -0600273 else
274 return 0;
275}
276
Shubhraprakash Daseb6b8542012-05-09 22:42:24 -0600277static inline int kgsl_mmu_get_pt_lsb(struct kgsl_mmu *mmu,
278 unsigned int unit_id,
279 enum kgsl_iommu_context_id ctx_id)
280{
281 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_pt_lsb)
282 return mmu->mmu_ops->mmu_get_pt_lsb(mmu, unit_id, ctx_id);
283 else
284 return 0;
285}
286
Shubhraprakash Daseb6b8542012-05-09 22:42:24 -0600287static inline int kgsl_mmu_enable_clk(struct kgsl_mmu *mmu,
288 int ctx_id)
289{
290 if (mmu->mmu_ops && mmu->mmu_ops->mmu_enable_clk)
291 return mmu->mmu_ops->mmu_enable_clk(mmu, ctx_id);
292 else
293 return 0;
294}
295
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600296static inline void kgsl_mmu_disable_clk_on_ts(struct kgsl_mmu *mmu,
297 unsigned int ts, bool ts_valid)
Shubhraprakash Daseb6b8542012-05-09 22:42:24 -0600298{
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600299 if (mmu->mmu_ops && mmu->mmu_ops->mmu_disable_clk_on_ts)
300 mmu->mmu_ops->mmu_disable_clk_on_ts(mmu, ts, ts_valid);
Shubhraprakash Daseb6b8542012-05-09 22:42:24 -0600301}
302
Anoop Kumar Yerukala5479c9c2012-07-08 14:53:06 +0530303static inline unsigned int kgsl_mmu_get_int_mask(void)
304{
305 /* Dont enable gpummu interrupts, if iommu is enabled */
306 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
307 return KGSL_MMU_INT_MASK;
308 else
309 return (MH_INTERRUPT_MASK__AXI_READ_ERROR |
310 MH_INTERRUPT_MASK__AXI_WRITE_ERROR);
311}
312
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700313static inline unsigned int kgsl_mmu_get_reg_gpuaddr(struct kgsl_mmu *mmu,
314 int iommu_unit_num,
315 int ctx_id, int reg)
316{
317 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_reg_gpuaddr)
318 return mmu->mmu_ops->mmu_get_reg_gpuaddr(mmu, iommu_unit_num,
319 ctx_id, reg);
320 else
321 return 0;
322}
323
324static inline int kgsl_mmu_get_num_iommu_units(struct kgsl_mmu *mmu)
325{
326 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_num_iommu_units)
327 return mmu->mmu_ops->mmu_get_num_iommu_units(mmu);
328 else
329 return 0;
330}
331
Tarun Karra9c070822012-11-27 16:43:51 -0700332static inline int kgsl_mmu_sync_lock(struct kgsl_mmu *mmu,
333 unsigned int *cmds)
334{
335 if ((mmu->flags & KGSL_MMU_FLAGS_IOMMU_SYNC) &&
336 mmu->mmu_ops && mmu->mmu_ops->mmu_sync_lock)
337 return mmu->mmu_ops->mmu_sync_lock(mmu, cmds);
338 else
339 return 0;
340}
341
342static inline int kgsl_mmu_sync_unlock(struct kgsl_mmu *mmu,
343 unsigned int *cmds)
344{
345 if ((mmu->flags & KGSL_MMU_FLAGS_IOMMU_SYNC) &&
346 mmu->mmu_ops && mmu->mmu_ops->mmu_sync_unlock)
347 return mmu->mmu_ops->mmu_sync_unlock(mmu, cmds);
348 else
349 return 0;
350}
351
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700352#endif /* __KGSL_MMU_H */