2 * Copyright © 2017 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #ifndef __INTEL_UNCORE_H__
26 #define __INTEL_UNCORE_H__
28 #include <linux/spinlock.h>
29 #include <linux/notifier.h>
30 #include <linux/hrtimer.h>
31 #include <linux/io-64-nonatomic-lo-hi.h>
35 struct drm_i915_private;
36 struct intel_runtime_pm;
40 struct intel_uncore_mmio_debug {
41 spinlock_t lock; /** lock is also taken in irq contexts. */
42 int unclaimed_mmio_check;
47 enum forcewake_domain_id {
48 FW_DOMAIN_ID_RENDER = 0,
49 FW_DOMAIN_ID_GT, /* also includes blitter engine */
51 FW_DOMAIN_ID_MEDIA_VDBOX0,
52 FW_DOMAIN_ID_MEDIA_VDBOX1,
53 FW_DOMAIN_ID_MEDIA_VDBOX2,
54 FW_DOMAIN_ID_MEDIA_VDBOX3,
55 FW_DOMAIN_ID_MEDIA_VDBOX4,
56 FW_DOMAIN_ID_MEDIA_VDBOX5,
57 FW_DOMAIN_ID_MEDIA_VDBOX6,
58 FW_DOMAIN_ID_MEDIA_VDBOX7,
59 FW_DOMAIN_ID_MEDIA_VEBOX0,
60 FW_DOMAIN_ID_MEDIA_VEBOX1,
61 FW_DOMAIN_ID_MEDIA_VEBOX2,
62 FW_DOMAIN_ID_MEDIA_VEBOX3,
67 enum forcewake_domains {
68 FORCEWAKE_RENDER = BIT(FW_DOMAIN_ID_RENDER),
69 FORCEWAKE_GT = BIT(FW_DOMAIN_ID_GT),
70 FORCEWAKE_MEDIA = BIT(FW_DOMAIN_ID_MEDIA),
71 FORCEWAKE_MEDIA_VDBOX0 = BIT(FW_DOMAIN_ID_MEDIA_VDBOX0),
72 FORCEWAKE_MEDIA_VDBOX1 = BIT(FW_DOMAIN_ID_MEDIA_VDBOX1),
73 FORCEWAKE_MEDIA_VDBOX2 = BIT(FW_DOMAIN_ID_MEDIA_VDBOX2),
74 FORCEWAKE_MEDIA_VDBOX3 = BIT(FW_DOMAIN_ID_MEDIA_VDBOX3),
75 FORCEWAKE_MEDIA_VDBOX4 = BIT(FW_DOMAIN_ID_MEDIA_VDBOX4),
76 FORCEWAKE_MEDIA_VDBOX5 = BIT(FW_DOMAIN_ID_MEDIA_VDBOX5),
77 FORCEWAKE_MEDIA_VDBOX6 = BIT(FW_DOMAIN_ID_MEDIA_VDBOX6),
78 FORCEWAKE_MEDIA_VDBOX7 = BIT(FW_DOMAIN_ID_MEDIA_VDBOX7),
79 FORCEWAKE_MEDIA_VEBOX0 = BIT(FW_DOMAIN_ID_MEDIA_VEBOX0),
80 FORCEWAKE_MEDIA_VEBOX1 = BIT(FW_DOMAIN_ID_MEDIA_VEBOX1),
81 FORCEWAKE_MEDIA_VEBOX2 = BIT(FW_DOMAIN_ID_MEDIA_VEBOX2),
82 FORCEWAKE_MEDIA_VEBOX3 = BIT(FW_DOMAIN_ID_MEDIA_VEBOX3),
84 FORCEWAKE_ALL = BIT(FW_DOMAIN_ID_COUNT) - 1,
87 struct intel_uncore_funcs {
88 void (*force_wake_get)(struct intel_uncore *uncore,
89 enum forcewake_domains domains);
90 void (*force_wake_put)(struct intel_uncore *uncore,
91 enum forcewake_domains domains);
93 enum forcewake_domains (*read_fw_domains)(struct intel_uncore *uncore,
95 enum forcewake_domains (*write_fw_domains)(struct intel_uncore *uncore,
98 u8 (*mmio_readb)(struct intel_uncore *uncore,
99 i915_reg_t r, bool trace);
100 u16 (*mmio_readw)(struct intel_uncore *uncore,
101 i915_reg_t r, bool trace);
102 u32 (*mmio_readl)(struct intel_uncore *uncore,
103 i915_reg_t r, bool trace);
104 u64 (*mmio_readq)(struct intel_uncore *uncore,
105 i915_reg_t r, bool trace);
107 void (*mmio_writeb)(struct intel_uncore *uncore,
108 i915_reg_t r, u8 val, bool trace);
109 void (*mmio_writew)(struct intel_uncore *uncore,
110 i915_reg_t r, u16 val, bool trace);
111 void (*mmio_writel)(struct intel_uncore *uncore,
112 i915_reg_t r, u32 val, bool trace);
115 struct intel_forcewake_range {
119 enum forcewake_domains domains;
122 /* Other register ranges (e.g., shadow tables, MCR tables, etc.) */
128 struct intel_uncore {
131 struct drm_i915_private *i915;
132 struct intel_runtime_pm *rpm;
134 spinlock_t lock; /** lock is also taken in irq contexts. */
137 #define UNCORE_HAS_FORCEWAKE BIT(0)
138 #define UNCORE_HAS_FPGA_DBG_UNCLAIMED BIT(1)
139 #define UNCORE_HAS_DBG_UNCLAIMED BIT(2)
140 #define UNCORE_HAS_FIFO BIT(3)
142 const struct intel_forcewake_range *fw_domains_table;
143 unsigned int fw_domains_table_entries;
146 * Shadowed registers are special cases where we can safely write
147 * to the register *without* grabbing forcewake.
149 const struct i915_range *shadowed_reg_table;
150 unsigned int shadowed_reg_table_entries;
152 struct notifier_block pmic_bus_access_nb;
153 struct intel_uncore_funcs funcs;
155 unsigned int fifo_count;
157 enum forcewake_domains fw_domains;
158 enum forcewake_domains fw_domains_active;
159 enum forcewake_domains fw_domains_timer;
160 enum forcewake_domains fw_domains_saved; /* user domains saved for S3 */
162 struct intel_uncore_forcewake_domain {
163 struct intel_uncore *uncore;
164 enum forcewake_domain_id id;
165 enum forcewake_domains mask;
166 unsigned int wake_count;
168 struct hrtimer timer;
169 u32 __iomem *reg_set;
170 u32 __iomem *reg_ack;
171 } *fw_domain[FW_DOMAIN_ID_COUNT];
173 unsigned int user_forcewake_count;
175 struct intel_uncore_mmio_debug *debug;
178 /* Iterate over initialised fw domains */
179 #define for_each_fw_domain_masked(domain__, mask__, uncore__, tmp__) \
180 for (tmp__ = (mask__); tmp__ ;) \
181 for_each_if(domain__ = (uncore__)->fw_domain[__mask_next_bit(tmp__)])
183 #define for_each_fw_domain(domain__, uncore__, tmp__) \
184 for_each_fw_domain_masked(domain__, (uncore__)->fw_domains, uncore__, tmp__)
187 intel_uncore_has_forcewake(const struct intel_uncore *uncore)
189 return uncore->flags & UNCORE_HAS_FORCEWAKE;
193 intel_uncore_has_fpga_dbg_unclaimed(const struct intel_uncore *uncore)
195 return uncore->flags & UNCORE_HAS_FPGA_DBG_UNCLAIMED;
199 intel_uncore_has_dbg_unclaimed(const struct intel_uncore *uncore)
201 return uncore->flags & UNCORE_HAS_DBG_UNCLAIMED;
205 intel_uncore_has_fifo(const struct intel_uncore *uncore)
207 return uncore->flags & UNCORE_HAS_FIFO;
210 u32 intel_uncore_read_with_mcr_steering_fw(struct intel_uncore *uncore,
212 int slice, int subslice);
213 u32 intel_uncore_read_with_mcr_steering(struct intel_uncore *uncore,
214 i915_reg_t reg, int slice, int subslice);
217 intel_uncore_mmio_debug_init_early(struct intel_uncore_mmio_debug *mmio_debug);
218 void intel_uncore_init_early(struct intel_uncore *uncore,
219 struct drm_i915_private *i915);
220 int intel_uncore_init_mmio(struct intel_uncore *uncore);
221 void intel_uncore_prune_engine_fw_domains(struct intel_uncore *uncore,
222 struct intel_gt *gt);
223 bool intel_uncore_unclaimed_mmio(struct intel_uncore *uncore);
224 bool intel_uncore_arm_unclaimed_mmio_detection(struct intel_uncore *uncore);
225 void intel_uncore_fini_mmio(struct intel_uncore *uncore);
226 void intel_uncore_suspend(struct intel_uncore *uncore);
227 void intel_uncore_resume_early(struct intel_uncore *uncore);
228 void intel_uncore_runtime_resume(struct intel_uncore *uncore);
230 void assert_forcewakes_inactive(struct intel_uncore *uncore);
231 void assert_forcewakes_active(struct intel_uncore *uncore,
232 enum forcewake_domains fw_domains);
233 const char *intel_uncore_forcewake_domain_to_str(const enum forcewake_domain_id id);
235 enum forcewake_domains
236 intel_uncore_forcewake_for_reg(struct intel_uncore *uncore,
237 i915_reg_t reg, unsigned int op);
238 #define FW_REG_READ (1)
239 #define FW_REG_WRITE (2)
241 void intel_uncore_forcewake_get(struct intel_uncore *uncore,
242 enum forcewake_domains domains);
243 void intel_uncore_forcewake_put(struct intel_uncore *uncore,
244 enum forcewake_domains domains);
245 void intel_uncore_forcewake_flush(struct intel_uncore *uncore,
246 enum forcewake_domains fw_domains);
249 * Like above but the caller must manage the uncore.lock itself.
250 * Must be used with intel_uncore_read_fw() and friends.
252 void intel_uncore_forcewake_get__locked(struct intel_uncore *uncore,
253 enum forcewake_domains domains);
254 void intel_uncore_forcewake_put__locked(struct intel_uncore *uncore,
255 enum forcewake_domains domains);
257 void intel_uncore_forcewake_user_get(struct intel_uncore *uncore);
258 void intel_uncore_forcewake_user_put(struct intel_uncore *uncore);
260 int __intel_wait_for_register(struct intel_uncore *uncore,
264 unsigned int fast_timeout_us,
265 unsigned int slow_timeout_ms,
268 intel_wait_for_register(struct intel_uncore *uncore,
272 unsigned int timeout_ms)
274 return __intel_wait_for_register(uncore, reg, mask, value, 2,
278 int __intel_wait_for_register_fw(struct intel_uncore *uncore,
282 unsigned int fast_timeout_us,
283 unsigned int slow_timeout_ms,
286 intel_wait_for_register_fw(struct intel_uncore *uncore,
290 unsigned int timeout_ms)
292 return __intel_wait_for_register_fw(uncore, reg, mask, value,
293 2, timeout_ms, NULL);
296 /* register access functions */
297 #define __raw_read(x__, s__) \
298 static inline u##x__ __raw_uncore_read##x__(const struct intel_uncore *uncore, \
301 return read##s__(uncore->regs + i915_mmio_reg_offset(reg)); \
304 #define __raw_write(x__, s__) \
305 static inline void __raw_uncore_write##x__(const struct intel_uncore *uncore, \
306 i915_reg_t reg, u##x__ val) \
308 write##s__(val, uncore->regs + i915_mmio_reg_offset(reg)); \
323 #define __uncore_read(name__, x__, s__, trace__) \
324 static inline u##x__ intel_uncore_##name__(struct intel_uncore *uncore, \
327 return uncore->funcs.mmio_read##s__(uncore, reg, (trace__)); \
330 #define __uncore_write(name__, x__, s__, trace__) \
331 static inline void intel_uncore_##name__(struct intel_uncore *uncore, \
332 i915_reg_t reg, u##x__ val) \
334 uncore->funcs.mmio_write##s__(uncore, reg, val, (trace__)); \
337 __uncore_read(read8, 8, b, true)
338 __uncore_read(read16, 16, w, true)
339 __uncore_read(read, 32, l, true)
340 __uncore_read(read16_notrace, 16, w, false)
341 __uncore_read(read_notrace, 32, l, false)
343 __uncore_write(write8, 8, b, true)
344 __uncore_write(write16, 16, w, true)
345 __uncore_write(write, 32, l, true)
346 __uncore_write(write_notrace, 32, l, false)
348 /* Be very careful with read/write 64-bit values. On 32-bit machines, they
349 * will be implemented using 2 32-bit writes in an arbitrary order with
350 * an arbitrary delay between them. This can cause the hardware to
351 * act upon the intermediate value, possibly leading to corruption and
352 * machine death. For this reason we do not support intel_uncore_write64,
353 * or uncore->funcs.mmio_writeq.
355 * When reading a 64-bit value as two 32-bit values, the delay may cause
356 * the two reads to mismatch, e.g. a timestamp overflowing. Also note that
357 * occasionally a 64-bit register does not actually support a full readq
358 * and must be read using two 32-bit reads.
360 * You have been warned.
362 __uncore_read(read64, 64, q, true)
365 intel_uncore_read64_2x32(struct intel_uncore *uncore,
366 i915_reg_t lower_reg, i915_reg_t upper_reg)
368 u32 upper, lower, old_upper, loop = 0;
369 upper = intel_uncore_read(uncore, upper_reg);
372 lower = intel_uncore_read(uncore, lower_reg);
373 upper = intel_uncore_read(uncore, upper_reg);
374 } while (upper != old_upper && loop++ < 2);
375 return (u64)upper << 32 | lower;
378 #define intel_uncore_posting_read(...) ((void)intel_uncore_read_notrace(__VA_ARGS__))
379 #define intel_uncore_posting_read16(...) ((void)intel_uncore_read16_notrace(__VA_ARGS__))
382 #undef __uncore_write
384 /* These are untraced mmio-accessors that are only valid to be used inside
385 * critical sections, such as inside IRQ handlers, where forcewake is explicitly
388 * Think twice, and think again, before using these.
390 * As an example, these accessors can possibly be used between:
392 * spin_lock_irq(&uncore->lock);
393 * intel_uncore_forcewake_get__locked();
397 * intel_uncore_forcewake_put__locked();
398 * spin_unlock_irq(&uncore->lock);
401 * Note: some registers may not need forcewake held, so
402 * intel_uncore_forcewake_{get,put} can be omitted, see
403 * intel_uncore_forcewake_for_reg().
405 * Certain architectures will die if the same cacheline is concurrently accessed
406 * by different clients (e.g. on Ivybridge). Access to registers should
407 * therefore generally be serialised, by either the dev_priv->uncore.lock or
408 * a more localised lock guarding all access to that bank of registers.
410 #define intel_uncore_read_fw(...) __raw_uncore_read32(__VA_ARGS__)
411 #define intel_uncore_write_fw(...) __raw_uncore_write32(__VA_ARGS__)
412 #define intel_uncore_write64_fw(...) __raw_uncore_write64(__VA_ARGS__)
413 #define intel_uncore_posting_read_fw(...) ((void)intel_uncore_read_fw(__VA_ARGS__))
415 static inline void intel_uncore_rmw(struct intel_uncore *uncore,
416 i915_reg_t reg, u32 clear, u32 set)
420 old = intel_uncore_read(uncore, reg);
421 val = (old & ~clear) | set;
423 intel_uncore_write(uncore, reg, val);
426 static inline void intel_uncore_rmw_fw(struct intel_uncore *uncore,
427 i915_reg_t reg, u32 clear, u32 set)
431 old = intel_uncore_read_fw(uncore, reg);
432 val = (old & ~clear) | set;
434 intel_uncore_write_fw(uncore, reg, val);
437 static inline int intel_uncore_write_and_verify(struct intel_uncore *uncore,
438 i915_reg_t reg, u32 val,
439 u32 mask, u32 expected_val)
443 intel_uncore_write(uncore, reg, val);
444 reg_val = intel_uncore_read(uncore, reg);
446 return (reg_val & mask) != expected_val ? -EINVAL : 0;
449 #define raw_reg_read(base, reg) \
450 readl(base + i915_mmio_reg_offset(reg))
451 #define raw_reg_write(base, reg, value) \
452 writel(value, base + i915_mmio_reg_offset(reg))
454 #endif /* !__INTEL_UNCORE_H__ */