blob: 68cfb2ec8701ec82510fa4d2b7d6aa814719060c [file] [log] [blame]
Olav Haugan3c7fb382013-01-02 17:32:25 -08001/* Copyright (c) 2010-2013, The Linux Foundation. All rights reserved.
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -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.
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070011 */
12
13#ifndef MSM_IOMMU_H
14#define MSM_IOMMU_H
15
16#include <linux/interrupt.h>
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -080017#include <linux/clk.h>
Stephen Boyd55742b72012-08-08 11:40:26 -070018#include <linux/list.h>
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -070019#include <linux/regulator/consumer.h>
Stepan Moskovchenko15f209c2011-10-31 15:32:44 -070020#include <mach/socinfo.h>
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070021
Stepan Moskovchenko6ee3be82011-11-08 15:24:53 -080022extern pgprot_t pgprot_kernel;
Laura Abbott0d135652012-10-04 12:59:03 -070023extern struct bus_type msm_iommu_sec_bus_type;
Olav Haugan0858ae02013-06-04 16:51:50 -070024extern struct iommu_access_ops iommu_access_ops_v0;
25extern struct iommu_access_ops iommu_access_ops_v1;
Stepan Moskovchenko08bd6832010-11-15 18:19:35 -080026
Stepan Moskovchenkob2438892011-08-31 17:16:19 -070027/* Domain attributes */
28#define MSM_IOMMU_DOMAIN_PT_CACHEABLE 0x1
Laura Abbott0d135652012-10-04 12:59:03 -070029#define MSM_IOMMU_DOMAIN_PT_SECURE 0x2
Stepan Moskovchenkob2438892011-08-31 17:16:19 -070030
Stepan Moskovchenko08bd6832010-11-15 18:19:35 -080031/* Mask for the cache policy attribute */
32#define MSM_IOMMU_CP_MASK 0x03
33
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070034/* Maximum number of Machine IDs that we are allowing to be mapped to the same
35 * context bank. The number of MIDs mapped to the same CB does not affect
36 * performance, but there is a practical limit on how many distinct MIDs may
37 * be present. These mappings are typically determined at design time and are
38 * not expected to change at run time.
39 */
Stepan Moskovchenko23513c32010-11-12 19:29:47 -080040#define MAX_NUM_MIDS 32
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070041
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070042/* Maximum number of SMT entries allowed by the system */
43#define MAX_NUM_SMR 128
44
Stepan Moskovchenko880a3182012-10-01 12:35:24 -070045#define MAX_NUM_BFB_REGS 32
46
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070047/**
48 * struct msm_iommu_dev - a single IOMMU hardware instance
49 * name Human-readable name given to this IOMMU HW instance
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080050 * ncb Number of context banks present on this IOMMU HW instance
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070051 */
52struct msm_iommu_dev {
53 const char *name;
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080054 int ncb;
Shubhraprakash Das935e6a52012-04-05 14:47:30 -060055 int ttbr_split;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070056};
57
58/**
59 * struct msm_iommu_ctx_dev - an IOMMU context bank instance
60 * name Human-readable name given to this context bank
61 * num Index of this context bank within the hardware
62 * mids List of Machine IDs that are to be mapped into this context
63 * bank, terminated by -1. The MID is a set of signals on the
64 * AXI bus that identifies the function associated with a specific
65 * memory request. (See ARM spec).
66 */
67struct msm_iommu_ctx_dev {
68 const char *name;
69 int num;
70 int mids[MAX_NUM_MIDS];
71};
72
Stepan Moskovchenko880a3182012-10-01 12:35:24 -070073/**
74 * struct msm_iommu_bfb_settings - a set of IOMMU BFB tuning parameters
75 * regs An array of register offsets to configure
76 * data Values to write to corresponding registers
77 * length Number of valid entries in the offset/val arrays
78 */
79struct msm_iommu_bfb_settings {
80 unsigned int regs[MAX_NUM_BFB_REGS];
81 unsigned int data[MAX_NUM_BFB_REGS];
82 int length;
83};
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070084
85/**
86 * struct msm_iommu_drvdata - A single IOMMU hardware instance
87 * @base: IOMMU config port base address (VA)
Olav Haugan95d24162012-12-05 14:47:47 -080088 * @glb_base: IOMMU config port base address for global register space (VA)
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -080089 * @ncb The number of contexts on this IOMMU
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -070090 * @irq: Interrupt number
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -080091 * @clk: The bus clock for this IOMMU hardware instance
92 * @pclk: The clock for the IOMMU bus interconnect
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -070093 * @aclk: Alternate clock for this IOMMU core, if any
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -070094 * @name: Human-readable name of this IOMMU device
95 * @gdsc: Regulator needed to power this HW block (v2 only)
Stepan Moskovchenko880a3182012-10-01 12:35:24 -070096 * @bfb_settings: Optional BFB performance tuning parameters
Stephen Boyd55742b72012-08-08 11:40:26 -070097 * @dev: Struct device this hardware instance is tied to
98 * @list: List head to link all iommus together
Olav Haugan3c7fb382013-01-02 17:32:25 -080099 * @clk_reg_virt: Optional clock register virtual address.
Olav Haugancd932192013-01-31 18:30:15 -0800100 * @halt_enabled: Set to 1 if IOMMU halt is supported in the IOMMU, 0 otherwise.
Olav Haugan4e315c42013-03-06 10:14:28 -0800101 * @asid: List of ASID and their usage count (index is ASID value).
Olav Haugane3885392013-03-06 16:22:53 -0800102 * @ctx_attach_count: Count of how many context are attached.
Olav Haugan236970a2013-05-14 17:00:02 -0700103 * @bus_client : Bus client needed to vote for bus bandwidth.
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800104 *
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700105 * A msm_iommu_drvdata holds the global driver data about a single piece
106 * of an IOMMU hardware instance.
107 */
108struct msm_iommu_drvdata {
109 void __iomem *base;
Olav Haugan95d24162012-12-05 14:47:47 -0800110 void __iomem *glb_base;
Stepan Moskovchenkoa43d8c12011-02-24 18:00:42 -0800111 int ncb;
Shubhraprakash Das935e6a52012-04-05 14:47:30 -0600112 int ttbr_split;
Stepan Moskovchenko41f3f512011-02-24 18:00:39 -0800113 struct clk *clk;
114 struct clk *pclk;
Stepan Moskovchenko17ae71e2012-07-24 19:24:14 -0700115 struct clk *aclk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700116 const char *name;
Stepan Moskovchenko6751acc2012-06-21 17:36:47 -0700117 struct regulator *gdsc;
Olav Haugan2648d972013-01-07 17:32:31 -0800118 struct regulator *alt_gdsc;
Stepan Moskovchenko880a3182012-10-01 12:35:24 -0700119 struct msm_iommu_bfb_settings *bfb_settings;
Laura Abbott0d135652012-10-04 12:59:03 -0700120 int sec_id;
Stephen Boyd55742b72012-08-08 11:40:26 -0700121 struct device *dev;
122 struct list_head list;
Olav Haugan3c7fb382013-01-02 17:32:25 -0800123 void __iomem *clk_reg_virt;
Olav Haugancd932192013-01-31 18:30:15 -0800124 int halt_enabled;
Olav Haugan4e315c42013-03-06 10:14:28 -0800125 int *asid;
Olav Haugane3885392013-03-06 16:22:53 -0800126 unsigned int ctx_attach_count;
Olav Haugan236970a2013-05-14 17:00:02 -0700127 unsigned int bus_client;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700128};
129
Olav Hauganeece7e52013-04-02 10:22:21 -0700130/**
131 * struct iommu_access_ops - Callbacks for accessing IOMMU
132 * @iommu_power_on: Turn on power to unit
133 * @iommu_power_off: Turn off power to unit
Olav Haugan236970a2013-05-14 17:00:02 -0700134 * @iommu_bus_vote: Vote for bus bandwidth
Olav Hauganeece7e52013-04-02 10:22:21 -0700135 * @iommu_clk_on: Turn on clks to unit
136 * @iommu_clk_off: Turn off clks to unit
Jordan Crouse64bf39f2013-04-18 15:48:13 -0600137 * @iommu_lock_initialize: Initialize the remote lock
Olav Hauganeece7e52013-04-02 10:22:21 -0700138 * @iommu_lock_acquire: Acquire any locks needed
139 * @iommu_lock_release: Release locks needed
140 */
141struct iommu_access_ops {
142 int (*iommu_power_on)(struct msm_iommu_drvdata *);
143 void (*iommu_power_off)(struct msm_iommu_drvdata *);
Olav Haugan236970a2013-05-14 17:00:02 -0700144 int (*iommu_bus_vote)(struct msm_iommu_drvdata *drvdata,
145 unsigned int vote);
Olav Hauganeece7e52013-04-02 10:22:21 -0700146 int (*iommu_clk_on)(struct msm_iommu_drvdata *);
147 void (*iommu_clk_off)(struct msm_iommu_drvdata *);
Jordan Crouse64bf39f2013-04-18 15:48:13 -0600148 void * (*iommu_lock_initialize)(void);
Olav Hauganeece7e52013-04-02 10:22:21 -0700149 void (*iommu_lock_acquire)(void);
150 void (*iommu_lock_release)(void);
151};
152
Stephen Boyd55742b72012-08-08 11:40:26 -0700153void msm_iommu_add_drv(struct msm_iommu_drvdata *drv);
154void msm_iommu_remove_drv(struct msm_iommu_drvdata *drv);
Olav Hauganf3782732013-01-11 11:23:30 -0800155void program_iommu_bfb_settings(void __iomem *base,
156 const struct msm_iommu_bfb_settings *bfb_settings);
Olav Haugancd932192013-01-31 18:30:15 -0800157void iommu_halt(const struct msm_iommu_drvdata *iommu_drvdata);
158void iommu_resume(const struct msm_iommu_drvdata *iommu_drvdata);
Stephen Boyd55742b72012-08-08 11:40:26 -0700159
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700160/**
161 * struct msm_iommu_ctx_drvdata - an IOMMU context bank instance
162 * @num: Hardware context number of this context
163 * @pdev: Platform device associated wit this HW instance
164 * @attached_elm: List element for domains to track which devices are
165 * attached to them
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700166 * @attached_domain Domain currently attached to this context (if any)
167 * @name Human-readable name of this context device
Olav Haugan95d24162012-12-05 14:47:47 -0800168 * @sids List of Stream IDs mapped to this context
169 * @nsid Number of Stream IDs mapped to this context
Olav Haugan26ddd432012-12-07 11:39:21 -0800170 * @secure_context true if this is a secure context programmed by
171 the secure environment, false otherwise
172 * @asid ASID used with this context.
Olav Haugane99ee7e2012-12-11 15:02:02 -0800173 * @attach_count Number of time this context has been attached.
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700174 *
175 * A msm_iommu_ctx_drvdata holds the driver data for a single context bank
176 * within each IOMMU hardware instance
177 */
178struct msm_iommu_ctx_drvdata {
179 int num;
180 struct platform_device *pdev;
181 struct list_head attached_elm;
Stepan Moskovchenko73a50f62012-05-03 17:29:12 -0700182 struct iommu_domain *attached_domain;
183 const char *name;
Stepan Moskovchenko4575bdd2012-06-28 14:59:00 -0700184 u32 sids[MAX_NUM_SMR];
185 unsigned int nsid;
Olav Haugan26ddd432012-12-07 11:39:21 -0800186 unsigned int secure_context;
187 int asid;
Olav Haugane99ee7e2012-12-11 15:02:02 -0800188 int attach_count;
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700189};
190
Mitchel Humpherys9e90db32013-05-21 17:37:22 -0700191struct msm_iommu_context_regs {
192 uint32_t far;
193 uint32_t par;
194 uint32_t fsr;
195 uint32_t fsynr0;
196 uint32_t fsynr1;
197 uint32_t ttbr0;
198 uint32_t ttbr1;
199 uint32_t sctlr;
200 uint32_t actlr;
201 uint32_t prrr;
202 uint32_t nmrr;
203};
204
205void print_ctx_regs(struct msm_iommu_context_regs *regs);
206
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700207/*
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700208 * Interrupt handler for the IOMMU context fault interrupt. Hooking the
209 * interrupt is not supported in the API yet, but this will print an error
210 * message and dump useful IOMMU registers.
211 */
212irqreturn_t msm_iommu_fault_handler(int irq, void *dev_id);
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800213irqreturn_t msm_iommu_fault_handler_v2(int irq, void *dev_id);
Mitchel Humpherysf3b50912013-05-21 17:46:04 -0700214irqreturn_t msm_iommu_secure_fault_handler_v2(int irq, void *dev_id);
Stepan Moskovchenko0720d1f2010-08-24 18:31:10 -0700215
Olav Haugan65209cd2012-11-07 15:02:56 -0800216enum {
217 PROC_APPS,
218 PROC_GPU,
219 PROC_MAX
220};
221
222/* Expose structure to allow kgsl iommu driver to use the same structure to
223 * communicate to GPU the addresses of the flag and turn variables.
224 */
225struct remote_iommu_petersons_spinlock {
226 uint32_t flag[PROC_MAX];
227 uint32_t turn;
228};
229
230#ifdef CONFIG_MSM_IOMMU
231void *msm_iommu_lock_initialize(void);
232void msm_iommu_mutex_lock(void);
233void msm_iommu_mutex_unlock(void);
Olav Haugan0858ae02013-06-04 16:51:50 -0700234void msm_set_iommu_access_ops(struct iommu_access_ops *ops);
235struct iommu_access_ops *msm_get_iommu_access_ops(void);
Olav Haugan65209cd2012-11-07 15:02:56 -0800236#else
237static inline void *msm_iommu_lock_initialize(void)
238{
239 return NULL;
240}
241static inline void msm_iommu_mutex_lock(void) { }
242static inline void msm_iommu_mutex_unlock(void) { }
Olav Haugan0858ae02013-06-04 16:51:50 -0700243static inline void msm_set_iommu_access_ops(struct iommu_access_ops *ops)
244{
245
246}
247static inline struct iommu_access_ops *msm_get_iommu_access_ops(void)
248{
249 return NULL;
250}
Olav Haugan65209cd2012-11-07 15:02:56 -0800251#endif
252
253#ifdef CONFIG_MSM_IOMMU_GPU_SYNC
254void msm_iommu_remote_p0_spin_lock(void);
255void msm_iommu_remote_p0_spin_unlock(void);
256
257#define msm_iommu_remote_lock_init() _msm_iommu_remote_spin_lock_init()
258#define msm_iommu_remote_spin_lock() msm_iommu_remote_p0_spin_lock()
259#define msm_iommu_remote_spin_unlock() msm_iommu_remote_p0_spin_unlock()
260#else
261#define msm_iommu_remote_lock_init()
262#define msm_iommu_remote_spin_lock()
263#define msm_iommu_remote_spin_unlock()
264#endif
265
266/* Allows kgsl iommu driver to acquire lock */
267#define msm_iommu_lock() \
268 do { \
269 msm_iommu_mutex_lock(); \
270 msm_iommu_remote_spin_lock(); \
271 } while (0)
272
273#define msm_iommu_unlock() \
274 do { \
275 msm_iommu_remote_spin_unlock(); \
276 msm_iommu_mutex_unlock(); \
277 } while (0)
278
Shubhraprakash Dasf4f600f2011-08-12 13:27:34 -0600279#ifdef CONFIG_MSM_IOMMU
280/*
281 * Look up an IOMMU context device by its context name. NULL if none found.
282 * Useful for testing and drivers that do not yet fully have IOMMU stuff in
283 * their platform devices.
284 */
285struct device *msm_iommu_get_ctx(const char *ctx_name);
286#else
287static inline struct device *msm_iommu_get_ctx(const char *ctx_name)
288{
289 return NULL;
290}
291#endif
292
Laura Abbottf4daa692012-10-10 19:31:53 -0700293/*
294 * Function to program the global registers of an IOMMU securely.
295 * This should only be called on IOMMUs for which kernel programming
296 * of global registers is not possible
297 */
Olav Hauganeece7e52013-04-02 10:22:21 -0700298void msm_iommu_sec_set_access_ops(struct iommu_access_ops *access_ops);
Laura Abbottf4daa692012-10-10 19:31:53 -0700299int msm_iommu_sec_program_iommu(int sec_id);
Stepan Moskovchenko15f209c2011-10-31 15:32:44 -0700300
Olav Haugan0e22c482013-01-28 17:39:36 -0800301static inline int msm_soc_version_supports_iommu_v0(void)
Stepan Moskovchenko15f209c2011-10-31 15:32:44 -0700302{
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800303#ifdef CONFIG_OF
304 struct device_node *node;
305
Olav Haugan0e22c482013-01-28 17:39:36 -0800306 node = of_find_compatible_node(NULL, NULL, "qcom,msm-smmu-v1");
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800307 if (node) {
308 of_node_put(node);
309 return 0;
310 }
Olav Haugan95d24162012-12-05 14:47:47 -0800311
Olav Haugan0e22c482013-01-28 17:39:36 -0800312 node = of_find_compatible_node(NULL, NULL, "qcom,msm-smmu-v0");
Olav Haugan95d24162012-12-05 14:47:47 -0800313 if (node) {
314 of_node_put(node);
315 return 1;
316 }
Sathish Ambleyd1b89ed2012-02-07 21:47:47 -0800317#endif
Stepan Moskovchenko15f209c2011-10-31 15:32:44 -0700318 if (cpu_is_msm8960() &&
319 SOCINFO_VERSION_MAJOR(socinfo_get_version()) < 2)
320 return 0;
321
322 if (cpu_is_msm8x60() &&
323 (SOCINFO_VERSION_MAJOR(socinfo_get_version()) != 2 ||
324 SOCINFO_VERSION_MINOR(socinfo_get_version()) < 1)) {
325 return 0;
326 }
327 return 1;
328}
Jeremy Gebben2dfe0022012-11-01 11:03:21 -0600329#endif