1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
8 #include "intel_display_types.h"
10 #define DSB_BUF_SIZE (2 * PAGE_SIZE)
13 #define DSB_OPCODE_SHIFT 24
14 #define DSB_OPCODE_MMIO_WRITE 0x1
15 #define DSB_OPCODE_INDEXED_WRITE 0x9
16 #define DSB_BYTE_EN 0xF
17 #define DSB_BYTE_EN_SHIFT 20
18 #define DSB_REG_VALUE_MASK 0xfffff
20 static inline bool is_dsb_busy(struct intel_dsb *dsb)
22 struct intel_crtc *crtc = container_of(dsb, typeof(*crtc), dsb);
23 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
24 enum pipe pipe = crtc->pipe;
26 return DSB_STATUS & I915_READ(DSB_CTRL(pipe, dsb->id));
29 static inline bool intel_dsb_enable_engine(struct intel_dsb *dsb)
31 struct intel_crtc *crtc = container_of(dsb, typeof(*crtc), dsb);
32 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
33 enum pipe pipe = crtc->pipe;
36 dsb_ctrl = I915_READ(DSB_CTRL(pipe, dsb->id));
37 if (DSB_STATUS & dsb_ctrl) {
38 DRM_DEBUG_KMS("DSB engine is busy.\n");
42 dsb_ctrl |= DSB_ENABLE;
43 I915_WRITE(DSB_CTRL(pipe, dsb->id), dsb_ctrl);
45 POSTING_READ(DSB_CTRL(pipe, dsb->id));
49 static inline bool intel_dsb_disable_engine(struct intel_dsb *dsb)
51 struct intel_crtc *crtc = container_of(dsb, typeof(*crtc), dsb);
52 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
53 enum pipe pipe = crtc->pipe;
56 dsb_ctrl = I915_READ(DSB_CTRL(pipe, dsb->id));
57 if (DSB_STATUS & dsb_ctrl) {
58 DRM_DEBUG_KMS("DSB engine is busy.\n");
62 dsb_ctrl &= ~DSB_ENABLE;
63 I915_WRITE(DSB_CTRL(pipe, dsb->id), dsb_ctrl);
65 POSTING_READ(DSB_CTRL(pipe, dsb->id));
70 intel_dsb_get(struct intel_crtc *crtc)
72 struct drm_device *dev = crtc->base.dev;
73 struct drm_i915_private *i915 = to_i915(dev);
74 struct intel_dsb *dsb = &crtc->dsb;
75 struct drm_i915_gem_object *obj;
77 intel_wakeref_t wakeref;
82 if (atomic_add_return(1, &dsb->refcount) != 1)
86 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
88 obj = i915_gem_object_create_internal(i915, DSB_BUF_SIZE);
90 DRM_ERROR("Gem object creation failed\n");
94 mutex_lock(&i915->drm.struct_mutex);
95 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, PIN_MAPPABLE);
96 mutex_unlock(&i915->drm.struct_mutex);
98 DRM_ERROR("Vma creation failed\n");
99 i915_gem_object_put(obj);
100 atomic_dec(&dsb->refcount);
104 dsb->cmd_buf = i915_gem_object_pin_map(vma->obj, I915_MAP_WC);
105 if (IS_ERR(dsb->cmd_buf)) {
106 DRM_ERROR("Command buffer creation failed\n");
107 i915_vma_unpin_and_release(&vma, 0);
109 atomic_dec(&dsb->refcount);
115 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
119 void intel_dsb_put(struct intel_dsb *dsb)
121 struct intel_crtc *crtc = container_of(dsb, typeof(*crtc), dsb);
122 struct drm_i915_private *i915 = to_i915(crtc->base.dev);
127 if (WARN_ON(atomic_read(&dsb->refcount) == 0))
130 if (atomic_dec_and_test(&dsb->refcount)) {
131 mutex_lock(&i915->drm.struct_mutex);
132 i915_gem_object_unpin_map(dsb->vma->obj);
133 i915_vma_unpin_and_release(&dsb->vma, 0);
134 mutex_unlock(&i915->drm.struct_mutex);
137 dsb->ins_start_offset = 0;
141 void intel_dsb_indexed_reg_write(struct intel_dsb *dsb, i915_reg_t reg,
144 struct intel_crtc *crtc = container_of(dsb, typeof(*crtc), dsb);
145 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
146 u32 *buf = dsb->cmd_buf;
150 I915_WRITE(reg, val);
154 if (WARN_ON(dsb->free_pos >= DSB_BUF_SIZE)) {
155 DRM_DEBUG_KMS("DSB buffer overflow\n");
160 * For example the buffer will look like below for 3 dwords for auto
161 * increment register:
162 * +--------------------------------------------------------+
163 * | size = 3 | offset &| value1 | value2 | value3 | zero |
164 * | | opcode | | | | |
165 * +--------------------------------------------------------+
170 * As every instruction is 8 byte aligned the index of dsb instruction
171 * will start always from even number while dealing with u32 array. If
172 * we are writing odd no of dwords, Zeros will be added in the end for
175 reg_val = buf[dsb->ins_start_offset + 1] & DSB_REG_VALUE_MASK;
176 if (reg_val != i915_mmio_reg_offset(reg)) {
177 /* Every instruction should be 8 byte aligned. */
178 dsb->free_pos = ALIGN(dsb->free_pos, 2);
180 dsb->ins_start_offset = dsb->free_pos;
182 /* Update the size. */
183 buf[dsb->free_pos++] = 1;
185 /* Update the opcode and reg. */
186 buf[dsb->free_pos++] = (DSB_OPCODE_INDEXED_WRITE <<
188 i915_mmio_reg_offset(reg);
190 /* Update the value. */
191 buf[dsb->free_pos++] = val;
193 /* Update the new value. */
194 buf[dsb->free_pos++] = val;
196 /* Update the size. */
197 buf[dsb->ins_start_offset]++;
200 /* if number of data words is odd, then the last dword should be 0.*/
201 if (dsb->free_pos & 0x1)
202 buf[dsb->free_pos] = 0;
205 void intel_dsb_reg_write(struct intel_dsb *dsb, i915_reg_t reg, u32 val)
207 struct intel_crtc *crtc = container_of(dsb, typeof(*crtc), dsb);
208 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
209 u32 *buf = dsb->cmd_buf;
212 I915_WRITE(reg, val);
216 if (WARN_ON(dsb->free_pos >= DSB_BUF_SIZE)) {
217 DRM_DEBUG_KMS("DSB buffer overflow\n");
221 dsb->ins_start_offset = dsb->free_pos;
222 buf[dsb->free_pos++] = val;
223 buf[dsb->free_pos++] = (DSB_OPCODE_MMIO_WRITE << DSB_OPCODE_SHIFT) |
224 (DSB_BYTE_EN << DSB_BYTE_EN_SHIFT) |
225 i915_mmio_reg_offset(reg);