1 // SPDX-License-Identifier: MIT 2 /* 3 * Copyright © 2022 Intel Corporation 4 */ 5 6 #include <linux/debugfs.h> 7 8 #include "hsw_ips.h" 9 #include "i915_drv.h" 10 #include "i915_reg.h" 11 #include "intel_color_regs.h" 12 #include "intel_de.h" 13 #include "intel_display_regs.h" 14 #include "intel_display_rpm.h" 15 #include "intel_display_types.h" 16 #include "intel_pcode.h" 17 18 static void hsw_ips_enable(const struct intel_crtc_state *crtc_state) 19 { 20 struct intel_display *display = to_intel_display(crtc_state); 21 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 22 struct drm_i915_private *i915 = to_i915(crtc->base.dev); 23 u32 val; 24 25 if (!crtc_state->ips_enabled) 26 return; 27 28 /* 29 * We can only enable IPS after we enable a plane and wait for a vblank 30 * This function is called from post_plane_update, which is run after 31 * a vblank wait. 32 */ 33 drm_WARN_ON(display->drm, 34 !(crtc_state->active_planes & ~BIT(PLANE_CURSOR))); 35 36 val = IPS_ENABLE; 37 38 if (display->ips.false_color) 39 val |= IPS_FALSE_COLOR; 40 41 if (display->platform.broadwell) { 42 drm_WARN_ON(display->drm, 43 snb_pcode_write(&i915->uncore, DISPLAY_IPS_CONTROL, 44 val | IPS_PCODE_CONTROL)); 45 /* 46 * Quoting Art Runyan: "its not safe to expect any particular 47 * value in IPS_CTL bit 31 after enabling IPS through the 48 * mailbox." Moreover, the mailbox may return a bogus state, 49 * so we need to just enable it and continue on. 50 */ 51 } else { 52 intel_de_write(display, IPS_CTL, val); 53 /* 54 * The bit only becomes 1 in the next vblank, so this wait here 55 * is essentially intel_wait_for_vblank. If we don't have this 56 * and don't wait for vblanks until the end of crtc_enable, then 57 * the HW state readout code will complain that the expected 58 * IPS_CTL value is not the one we read. 59 */ 60 if (intel_de_wait_for_set(display, IPS_CTL, IPS_ENABLE, 50)) 61 drm_err(display->drm, 62 "Timed out waiting for IPS enable\n"); 63 } 64 } 65 66 bool hsw_ips_disable(const struct intel_crtc_state *crtc_state) 67 { 68 struct intel_display *display = to_intel_display(crtc_state); 69 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 70 struct drm_i915_private *i915 = to_i915(crtc->base.dev); 71 bool need_vblank_wait = false; 72 73 if (!crtc_state->ips_enabled) 74 return need_vblank_wait; 75 76 if (display->platform.broadwell) { 77 drm_WARN_ON(display->drm, 78 snb_pcode_write(&i915->uncore, DISPLAY_IPS_CONTROL, 0)); 79 /* 80 * Wait for PCODE to finish disabling IPS. The BSpec specified 81 * 42ms timeout value leads to occasional timeouts so use 100ms 82 * instead. 83 */ 84 if (intel_de_wait_for_clear(display, IPS_CTL, IPS_ENABLE, 100)) 85 drm_err(display->drm, 86 "Timed out waiting for IPS disable\n"); 87 } else { 88 intel_de_write(display, IPS_CTL, 0); 89 intel_de_posting_read(display, IPS_CTL); 90 } 91 92 /* We need to wait for a vblank before we can disable the plane. */ 93 need_vblank_wait = true; 94 95 return need_vblank_wait; 96 } 97 98 static bool hsw_ips_need_disable(struct intel_atomic_state *state, 99 struct intel_crtc *crtc) 100 { 101 struct intel_display *display = to_intel_display(state); 102 const struct intel_crtc_state *old_crtc_state = 103 intel_atomic_get_old_crtc_state(state, crtc); 104 const struct intel_crtc_state *new_crtc_state = 105 intel_atomic_get_new_crtc_state(state, crtc); 106 107 if (!old_crtc_state->ips_enabled) 108 return false; 109 110 if (intel_crtc_needs_modeset(new_crtc_state)) 111 return true; 112 113 /* 114 * Workaround : Do not read or write the pipe palette/gamma data while 115 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled. 116 * 117 * Disable IPS before we program the LUT. 118 */ 119 if (display->platform.haswell && 120 intel_crtc_needs_color_update(new_crtc_state) && 121 new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT) 122 return true; 123 124 return !new_crtc_state->ips_enabled; 125 } 126 127 bool hsw_ips_pre_update(struct intel_atomic_state *state, 128 struct intel_crtc *crtc) 129 { 130 const struct intel_crtc_state *old_crtc_state = 131 intel_atomic_get_old_crtc_state(state, crtc); 132 133 if (!hsw_ips_need_disable(state, crtc)) 134 return false; 135 136 return hsw_ips_disable(old_crtc_state); 137 } 138 139 static bool hsw_ips_need_enable(struct intel_atomic_state *state, 140 struct intel_crtc *crtc) 141 { 142 struct intel_display *display = to_intel_display(state); 143 const struct intel_crtc_state *old_crtc_state = 144 intel_atomic_get_old_crtc_state(state, crtc); 145 const struct intel_crtc_state *new_crtc_state = 146 intel_atomic_get_new_crtc_state(state, crtc); 147 148 if (!new_crtc_state->ips_enabled) 149 return false; 150 151 if (intel_crtc_needs_modeset(new_crtc_state)) 152 return true; 153 154 /* 155 * Workaround : Do not read or write the pipe palette/gamma data while 156 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled. 157 * 158 * Re-enable IPS after the LUT has been programmed. 159 */ 160 if (display->platform.haswell && 161 intel_crtc_needs_color_update(new_crtc_state) && 162 new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT) 163 return true; 164 165 /* 166 * We can't read out IPS on broadwell, assume the worst and 167 * forcibly enable IPS on the first fastset. 168 */ 169 if (intel_crtc_needs_fastset(new_crtc_state) && old_crtc_state->inherited) 170 return true; 171 172 return !old_crtc_state->ips_enabled; 173 } 174 175 void hsw_ips_post_update(struct intel_atomic_state *state, 176 struct intel_crtc *crtc) 177 { 178 const struct intel_crtc_state *new_crtc_state = 179 intel_atomic_get_new_crtc_state(state, crtc); 180 181 if (!hsw_ips_need_enable(state, crtc)) 182 return; 183 184 hsw_ips_enable(new_crtc_state); 185 } 186 187 /* IPS only exists on ULT machines and is tied to pipe A. */ 188 bool hsw_crtc_supports_ips(struct intel_crtc *crtc) 189 { 190 struct intel_display *display = to_intel_display(crtc); 191 192 return HAS_IPS(display) && crtc->pipe == PIPE_A; 193 } 194 195 static bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state) 196 { 197 struct intel_display *display = to_intel_display(crtc_state); 198 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 199 200 /* IPS only exists on ULT machines and is tied to pipe A. */ 201 if (!hsw_crtc_supports_ips(crtc)) 202 return false; 203 204 if (!display->params.enable_ips) 205 return false; 206 207 if (crtc_state->pipe_bpp > 24) 208 return false; 209 210 /* 211 * We compare against max which means we must take 212 * the increased cdclk requirement into account when 213 * calculating the new cdclk. 214 * 215 * Should measure whether using a lower cdclk w/o IPS 216 */ 217 if (display->platform.broadwell && 218 crtc_state->pixel_rate > display->cdclk.max_cdclk_freq * 95 / 100) 219 return false; 220 221 return true; 222 } 223 224 int hsw_ips_min_cdclk(const struct intel_crtc_state *crtc_state) 225 { 226 struct intel_display *display = to_intel_display(crtc_state); 227 228 if (!display->platform.broadwell) 229 return 0; 230 231 if (!hsw_crtc_state_ips_capable(crtc_state)) 232 return 0; 233 234 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */ 235 return DIV_ROUND_UP(crtc_state->pixel_rate * 100, 95); 236 } 237 238 int hsw_ips_compute_config(struct intel_atomic_state *state, 239 struct intel_crtc *crtc) 240 { 241 struct intel_display *display = to_intel_display(state); 242 struct intel_crtc_state *crtc_state = 243 intel_atomic_get_new_crtc_state(state, crtc); 244 245 crtc_state->ips_enabled = false; 246 247 if (!hsw_crtc_state_ips_capable(crtc_state)) 248 return 0; 249 250 /* 251 * When IPS gets enabled, the pipe CRC changes. Since IPS gets 252 * enabled and disabled dynamically based on package C states, 253 * user space can't make reliable use of the CRCs, so let's just 254 * completely disable it. 255 */ 256 if (crtc_state->crc_enabled) 257 return 0; 258 259 /* IPS should be fine as long as at least one plane is enabled. */ 260 if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR))) 261 return 0; 262 263 if (display->platform.broadwell) { 264 const struct intel_cdclk_state *cdclk_state; 265 266 cdclk_state = intel_atomic_get_cdclk_state(state); 267 if (IS_ERR(cdclk_state)) 268 return PTR_ERR(cdclk_state); 269 270 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */ 271 if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100) 272 return 0; 273 } 274 275 crtc_state->ips_enabled = true; 276 277 return 0; 278 } 279 280 void hsw_ips_get_config(struct intel_crtc_state *crtc_state) 281 { 282 struct intel_display *display = to_intel_display(crtc_state); 283 struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc); 284 285 if (!hsw_crtc_supports_ips(crtc)) 286 return; 287 288 if (display->platform.haswell) { 289 crtc_state->ips_enabled = intel_de_read(display, IPS_CTL) & IPS_ENABLE; 290 } else { 291 /* 292 * We cannot readout IPS state on broadwell, set to 293 * true so we can set it to a defined state on first 294 * commit. 295 */ 296 crtc_state->ips_enabled = true; 297 } 298 } 299 300 static int hsw_ips_debugfs_false_color_get(void *data, u64 *val) 301 { 302 struct intel_crtc *crtc = data; 303 struct intel_display *display = to_intel_display(crtc); 304 305 *val = display->ips.false_color; 306 307 return 0; 308 } 309 310 static int hsw_ips_debugfs_false_color_set(void *data, u64 val) 311 { 312 struct intel_crtc *crtc = data; 313 struct intel_display *display = to_intel_display(crtc); 314 struct intel_crtc_state *crtc_state; 315 int ret; 316 317 ret = drm_modeset_lock(&crtc->base.mutex, NULL); 318 if (ret) 319 return ret; 320 321 display->ips.false_color = val; 322 323 crtc_state = to_intel_crtc_state(crtc->base.state); 324 325 if (!crtc_state->hw.active) 326 goto unlock; 327 328 if (crtc_state->uapi.commit && 329 !try_wait_for_completion(&crtc_state->uapi.commit->hw_done)) 330 goto unlock; 331 332 hsw_ips_enable(crtc_state); 333 334 unlock: 335 drm_modeset_unlock(&crtc->base.mutex); 336 337 return ret; 338 } 339 340 DEFINE_DEBUGFS_ATTRIBUTE(hsw_ips_debugfs_false_color_fops, 341 hsw_ips_debugfs_false_color_get, 342 hsw_ips_debugfs_false_color_set, 343 "%llu\n"); 344 345 static int hsw_ips_debugfs_status_show(struct seq_file *m, void *unused) 346 { 347 struct intel_crtc *crtc = m->private; 348 struct intel_display *display = to_intel_display(crtc); 349 struct ref_tracker *wakeref; 350 351 wakeref = intel_display_rpm_get(display); 352 353 seq_printf(m, "Enabled by kernel parameter: %s\n", 354 str_yes_no(display->params.enable_ips)); 355 356 if (DISPLAY_VER(display) >= 8) { 357 seq_puts(m, "Currently: unknown\n"); 358 } else { 359 if (intel_de_read(display, IPS_CTL) & IPS_ENABLE) 360 seq_puts(m, "Currently: enabled\n"); 361 else 362 seq_puts(m, "Currently: disabled\n"); 363 } 364 365 intel_display_rpm_put(display, wakeref); 366 367 return 0; 368 } 369 370 DEFINE_SHOW_ATTRIBUTE(hsw_ips_debugfs_status); 371 372 void hsw_ips_crtc_debugfs_add(struct intel_crtc *crtc) 373 { 374 if (!hsw_crtc_supports_ips(crtc)) 375 return; 376 377 debugfs_create_file("i915_ips_false_color", 0644, crtc->base.debugfs_entry, 378 crtc, &hsw_ips_debugfs_false_color_fops); 379 380 debugfs_create_file("i915_ips_status", 0444, crtc->base.debugfs_entry, 381 crtc, &hsw_ips_debugfs_status_fops); 382 } 383