blob: 377f3423623167e7ccc7165f18b46cd7bc3d3886 [file] [log] [blame]
Duy Truonge833aca2013-02-12 13:35:08 -08001/* Copyright (c) 2002,2007-2012, The Linux Foundation. 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;
Tarun Karrab8107322013-02-07 13:46:02 -0800117 unsigned int fault_addr;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600118 void *priv;
119};
120
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600121struct kgsl_mmu;
122
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600123struct kgsl_mmu_ops {
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600124 int (*mmu_init) (struct kgsl_mmu *mmu);
125 int (*mmu_close) (struct kgsl_mmu *mmu);
126 int (*mmu_start) (struct kgsl_mmu *mmu);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600127 void (*mmu_stop) (struct kgsl_mmu *mmu);
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600128 void (*mmu_setstate) (struct kgsl_mmu *mmu,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600129 struct kgsl_pagetable *pagetable,
130 unsigned int context_id);
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600131 void (*mmu_device_setstate) (struct kgsl_mmu *mmu,
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600132 uint32_t flags);
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600133 void (*mmu_pagefault) (struct kgsl_mmu *mmu);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600134 unsigned int (*mmu_get_current_ptbase)
Shubhraprakash Das1c528262012-04-26 17:38:13 -0600135 (struct kgsl_mmu *mmu);
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600136 void (*mmu_disable_clk_on_ts)
137 (struct kgsl_mmu *mmu, uint32_t ts, bool ts_valid);
Shubhraprakash Das9fb38ac2012-05-01 00:41:30 -0600138 int (*mmu_enable_clk)
139 (struct kgsl_mmu *mmu, int ctx_id);
Shubhraprakash Dasfce27362012-05-09 17:44:14 -0600140 int (*mmu_get_pt_lsb)(struct kgsl_mmu *mmu,
141 unsigned int unit_id,
142 enum kgsl_iommu_context_id ctx_id);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700143 unsigned int (*mmu_get_reg_gpuaddr)(struct kgsl_mmu *mmu,
144 int iommu_unit_num, int ctx_id, int reg);
145 int (*mmu_get_num_iommu_units)(struct kgsl_mmu *mmu);
146 int (*mmu_pt_equal) (struct kgsl_mmu *mmu,
147 struct kgsl_pagetable *pt,
148 unsigned int pt_base);
149 unsigned int (*mmu_get_pt_base_addr)
150 (struct kgsl_mmu *mmu,
151 struct kgsl_pagetable *pt);
Tarun Karra9c070822012-11-27 16:43:51 -0700152 unsigned int (*mmu_sync_lock)
153 (struct kgsl_mmu *mmu,
154 unsigned int *cmds);
155 unsigned int (*mmu_sync_unlock)
156 (struct kgsl_mmu *mmu,
157 unsigned int *cmds);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600158};
159
160struct kgsl_mmu_pt_ops {
161 int (*mmu_map) (void *mmu_pt,
162 struct kgsl_memdesc *memdesc,
Shubhraprakash Dasf764e462012-04-26 15:38:09 -0600163 unsigned int protflags,
164 unsigned int *tlb_flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600165 int (*mmu_unmap) (void *mmu_pt,
Shubhraprakash Das0c811262012-06-06 23:22:19 -0600166 struct kgsl_memdesc *memdesc,
167 unsigned int *tlb_flags);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600168 void *(*mmu_create_pagetable) (void);
169 void (*mmu_destroy_pagetable) (void *pt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170};
171
Tarun Karra9c070822012-11-27 16:43:51 -0700172#define KGSL_MMU_FLAGS_IOMMU_SYNC BIT(31)
173
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700174struct kgsl_mmu {
175 unsigned int refcnt;
176 uint32_t flags;
177 struct kgsl_device *device;
178 unsigned int config;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600179 struct kgsl_memdesc setstate_memory;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180 /* current page table object being used by device mmu */
181 struct kgsl_pagetable *defaultpagetable;
Shubhraprakash Das19ca4a62012-05-18 12:11:20 -0600182 /* pagetable object used for priv bank of IOMMU */
183 struct kgsl_pagetable *priv_bank_table;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700184 struct kgsl_pagetable *hwpagetable;
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600185 const struct kgsl_mmu_ops *mmu_ops;
186 void *priv;
Shubhraprakash Das2747cf62012-09-27 23:05:43 -0700187 int fault;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700188};
189
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600190#include "kgsl_gpummu.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600192extern struct kgsl_mmu_ops iommu_ops;
193extern struct kgsl_mmu_pt_ops iommu_pt_ops;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700194
195struct kgsl_pagetable *kgsl_mmu_getpagetable(unsigned long name);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600196void kgsl_mmu_putpagetable(struct kgsl_pagetable *pagetable);
Jeremy Gebben4e8aada2011-07-12 10:07:47 -0600197void kgsl_mh_start(struct kgsl_device *device);
198void kgsl_mh_intrcallback(struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700199int kgsl_mmu_init(struct kgsl_device *device);
200int kgsl_mmu_start(struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700201int kgsl_mmu_close(struct kgsl_device *device);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700202int kgsl_mmu_map(struct kgsl_pagetable *pagetable,
203 struct kgsl_memdesc *memdesc,
204 unsigned int protflags);
205int kgsl_mmu_map_global(struct kgsl_pagetable *pagetable,
206 struct kgsl_memdesc *memdesc, unsigned int protflags);
207int kgsl_mmu_unmap(struct kgsl_pagetable *pagetable,
208 struct kgsl_memdesc *memdesc);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700209unsigned int kgsl_virtaddr_to_physaddr(void *virtaddr);
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600210void kgsl_setstate(struct kgsl_mmu *mmu, unsigned int context_id,
211 uint32_t flags);
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700212int kgsl_mmu_get_ptname_from_ptbase(struct kgsl_mmu *mmu,
213 unsigned int pt_base);
Tarun Karrab8107322013-02-07 13:46:02 -0800214unsigned int kgsl_mmu_log_fault_addr(struct kgsl_mmu *mmu,
215 unsigned int pt_base, unsigned int addr);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600216int kgsl_mmu_pt_get_flags(struct kgsl_pagetable *pt,
217 enum kgsl_deviceid id);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600218void kgsl_mmu_ptpool_destroy(void *ptpool);
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600219void *kgsl_mmu_ptpool_init(int entries);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600220int kgsl_mmu_enabled(void);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600221void kgsl_mmu_set_mmutype(char *mmutype);
Shubhraprakash Das767fdda2011-08-15 15:49:45 -0600222enum kgsl_mmutype kgsl_mmu_get_mmutype(void);
Jordan Crouse6d76c4d2012-03-26 09:50:43 -0600223unsigned int kgsl_mmu_get_ptsize(void);
Shubhraprakash Dase7652cf2012-08-11 17:15:19 -0700224int kgsl_mmu_gpuaddr_in_range(unsigned int gpuaddr);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600225
226/*
227 * Static inline functions of MMU that simply call the SMMU specific
228 * function using a function pointer. These functions can be thought
229 * of as wrappers around the actual function
230 */
231
232static inline unsigned int kgsl_mmu_get_current_ptbase(struct kgsl_mmu *mmu)
233{
234 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_current_ptbase)
235 return mmu->mmu_ops->mmu_get_current_ptbase(mmu);
236 else
237 return 0;
238}
239
240static inline void kgsl_mmu_setstate(struct kgsl_mmu *mmu,
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600241 struct kgsl_pagetable *pagetable,
242 unsigned int context_id)
Shubhraprakash Das79447952012-04-26 18:12:23 -0600243{
244 if (mmu->mmu_ops && mmu->mmu_ops->mmu_setstate)
Shubhraprakash Dasb2abc452012-06-08 16:33:03 -0600245 mmu->mmu_ops->mmu_setstate(mmu, pagetable, context_id);
Shubhraprakash Das79447952012-04-26 18:12:23 -0600246}
247
248static inline void kgsl_mmu_device_setstate(struct kgsl_mmu *mmu,
249 uint32_t flags)
250{
251 if (mmu->mmu_ops && mmu->mmu_ops->mmu_device_setstate)
252 mmu->mmu_ops->mmu_device_setstate(mmu, flags);
253}
254
255static inline void kgsl_mmu_stop(struct kgsl_mmu *mmu)
256{
257 if (mmu->mmu_ops && mmu->mmu_ops->mmu_stop)
258 mmu->mmu_ops->mmu_stop(mmu);
259}
260
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700261static inline int kgsl_mmu_pt_equal(struct kgsl_mmu *mmu,
262 struct kgsl_pagetable *pt,
Shubhraprakash Das79447952012-04-26 18:12:23 -0600263 unsigned int pt_base)
264{
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700265 if (mmu->mmu_ops && mmu->mmu_ops->mmu_pt_equal)
266 return mmu->mmu_ops->mmu_pt_equal(mmu, pt, pt_base);
267 else
Shubhraprakash Das79447952012-04-26 18:12:23 -0600268 return 1;
Shubhraprakash Das79447952012-04-26 18:12:23 -0600269}
270
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700271static inline unsigned int kgsl_mmu_get_pt_base_addr(struct kgsl_mmu *mmu,
272 struct kgsl_pagetable *pt)
Shubhraprakash Das5a610b52012-05-09 17:31:54 -0600273{
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700274 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_pt_base_addr)
275 return mmu->mmu_ops->mmu_get_pt_base_addr(mmu, pt);
Shubhraprakash Dasa5b1db42012-05-09 18:02:34 -0600276 else
277 return 0;
278}
279
Shubhraprakash Daseb6b8542012-05-09 22:42:24 -0600280static inline int kgsl_mmu_get_pt_lsb(struct kgsl_mmu *mmu,
281 unsigned int unit_id,
282 enum kgsl_iommu_context_id ctx_id)
283{
284 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_pt_lsb)
285 return mmu->mmu_ops->mmu_get_pt_lsb(mmu, unit_id, ctx_id);
286 else
287 return 0;
288}
289
Shubhraprakash Daseb6b8542012-05-09 22:42:24 -0600290static inline int kgsl_mmu_enable_clk(struct kgsl_mmu *mmu,
291 int ctx_id)
292{
293 if (mmu->mmu_ops && mmu->mmu_ops->mmu_enable_clk)
294 return mmu->mmu_ops->mmu_enable_clk(mmu, ctx_id);
295 else
296 return 0;
297}
298
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600299static inline void kgsl_mmu_disable_clk_on_ts(struct kgsl_mmu *mmu,
300 unsigned int ts, bool ts_valid)
Shubhraprakash Daseb6b8542012-05-09 22:42:24 -0600301{
Shubhraprakash Dascb068072012-06-07 17:52:41 -0600302 if (mmu->mmu_ops && mmu->mmu_ops->mmu_disable_clk_on_ts)
303 mmu->mmu_ops->mmu_disable_clk_on_ts(mmu, ts, ts_valid);
Shubhraprakash Daseb6b8542012-05-09 22:42:24 -0600304}
305
Anoop Kumar Yerukala5479c9c2012-07-08 14:53:06 +0530306static inline unsigned int kgsl_mmu_get_int_mask(void)
307{
308 /* Dont enable gpummu interrupts, if iommu is enabled */
309 if (KGSL_MMU_TYPE_GPU == kgsl_mmu_get_mmutype())
310 return KGSL_MMU_INT_MASK;
311 else
312 return (MH_INTERRUPT_MASK__AXI_READ_ERROR |
313 MH_INTERRUPT_MASK__AXI_WRITE_ERROR);
314}
315
Shubhraprakash Das3cf33be2012-08-16 22:42:55 -0700316static inline unsigned int kgsl_mmu_get_reg_gpuaddr(struct kgsl_mmu *mmu,
317 int iommu_unit_num,
318 int ctx_id, int reg)
319{
320 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_reg_gpuaddr)
321 return mmu->mmu_ops->mmu_get_reg_gpuaddr(mmu, iommu_unit_num,
322 ctx_id, reg);
323 else
324 return 0;
325}
326
327static inline int kgsl_mmu_get_num_iommu_units(struct kgsl_mmu *mmu)
328{
329 if (mmu->mmu_ops && mmu->mmu_ops->mmu_get_num_iommu_units)
330 return mmu->mmu_ops->mmu_get_num_iommu_units(mmu);
331 else
332 return 0;
333}
334
Tarun Karra9c070822012-11-27 16:43:51 -0700335static inline int kgsl_mmu_sync_lock(struct kgsl_mmu *mmu,
336 unsigned int *cmds)
337{
338 if ((mmu->flags & KGSL_MMU_FLAGS_IOMMU_SYNC) &&
339 mmu->mmu_ops && mmu->mmu_ops->mmu_sync_lock)
340 return mmu->mmu_ops->mmu_sync_lock(mmu, cmds);
341 else
342 return 0;
343}
344
345static inline int kgsl_mmu_sync_unlock(struct kgsl_mmu *mmu,
346 unsigned int *cmds)
347{
348 if ((mmu->flags & KGSL_MMU_FLAGS_IOMMU_SYNC) &&
349 mmu->mmu_ops && mmu->mmu_ops->mmu_sync_unlock)
350 return mmu->mmu_ops->mmu_sync_unlock(mmu, cmds);
351 else
352 return 0;
353}
354
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700355#endif /* __KGSL_MMU_H */