1 // SPDX-License-Identifier: GPL-2.0-only
2 /**************************************************************************
3 * Copyright (c) 2007, Intel Corporation.
6 * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
9 **************************************************************************/
11 #include <drm/drm_vblank.h>
15 #include "psb_intel_reg.h"
24 psb_pipestat(int pipe)
36 mid_pipe_event(int pipe)
39 return _PSB_PIPEA_EVENT_FLAG;
41 return _MDFLD_PIPEB_EVENT_FLAG;
43 return _MDFLD_PIPEC_EVENT_FLAG;
48 mid_pipe_vsync(int pipe)
51 return _PSB_VSYNC_PIPEA_FLAG;
53 return _PSB_VSYNC_PIPEB_FLAG;
55 return _MDFLD_PIPEC_VBLANK_FLAG;
60 mid_pipeconf(int pipe)
72 psb_enable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask)
74 if ((dev_priv->pipestat[pipe] & mask) != mask) {
75 u32 reg = psb_pipestat(pipe);
76 dev_priv->pipestat[pipe] |= mask;
77 /* Enable the interrupt, clear any pending status */
78 if (gma_power_begin(dev_priv->dev, false)) {
79 u32 writeVal = PSB_RVDC32(reg);
80 writeVal |= (mask | (mask >> 16));
81 PSB_WVDC32(writeVal, reg);
82 (void) PSB_RVDC32(reg);
83 gma_power_end(dev_priv->dev);
89 psb_disable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask)
91 if ((dev_priv->pipestat[pipe] & mask) != 0) {
92 u32 reg = psb_pipestat(pipe);
93 dev_priv->pipestat[pipe] &= ~mask;
94 if (gma_power_begin(dev_priv->dev, false)) {
95 u32 writeVal = PSB_RVDC32(reg);
97 PSB_WVDC32(writeVal, reg);
98 (void) PSB_RVDC32(reg);
99 gma_power_end(dev_priv->dev);
104 static void mid_enable_pipe_event(struct drm_psb_private *dev_priv, int pipe)
106 if (gma_power_begin(dev_priv->dev, false)) {
107 u32 pipe_event = mid_pipe_event(pipe);
108 dev_priv->vdc_irq_mask |= pipe_event;
109 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
110 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
111 gma_power_end(dev_priv->dev);
115 static void mid_disable_pipe_event(struct drm_psb_private *dev_priv, int pipe)
117 if (dev_priv->pipestat[pipe] == 0) {
118 if (gma_power_begin(dev_priv->dev, false)) {
119 u32 pipe_event = mid_pipe_event(pipe);
120 dev_priv->vdc_irq_mask &= ~pipe_event;
121 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
122 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
123 gma_power_end(dev_priv->dev);
129 * Display controller interrupt handler for pipe event.
131 static void mid_pipe_event_handler(struct drm_device *dev, int pipe)
133 struct drm_psb_private *dev_priv =
134 (struct drm_psb_private *) dev->dev_private;
136 uint32_t pipe_stat_val = 0;
137 uint32_t pipe_stat_reg = psb_pipestat(pipe);
138 uint32_t pipe_enable = dev_priv->pipestat[pipe];
139 uint32_t pipe_status = dev_priv->pipestat[pipe] >> 16;
143 spin_lock(&dev_priv->irqmask_lock);
145 pipe_stat_val = PSB_RVDC32(pipe_stat_reg);
146 pipe_stat_val &= pipe_enable | pipe_status;
147 pipe_stat_val &= pipe_stat_val >> 16;
149 spin_unlock(&dev_priv->irqmask_lock);
151 /* Clear the 2nd level interrupt status bits
152 * Sometimes the bits are very sticky so we repeat until they unstick */
153 for (i = 0; i < 0xffff; i++) {
154 PSB_WVDC32(PSB_RVDC32(pipe_stat_reg), pipe_stat_reg);
155 pipe_clear = PSB_RVDC32(pipe_stat_reg) & pipe_status;
163 "%s, can't clear status bits for pipe %d, its value = 0x%x.\n",
164 __func__, pipe, PSB_RVDC32(pipe_stat_reg));
166 if (pipe_stat_val & PIPE_VBLANK_STATUS) {
167 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
168 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
171 drm_handle_vblank(dev, pipe);
173 spin_lock_irqsave(&dev->event_lock, flags);
174 if (gma_crtc->page_flip_event) {
175 drm_crtc_send_vblank_event(crtc,
176 gma_crtc->page_flip_event);
177 gma_crtc->page_flip_event = NULL;
178 drm_crtc_vblank_put(crtc);
180 spin_unlock_irqrestore(&dev->event_lock, flags);
185 * Display controller interrupt handler.
187 static void psb_vdc_interrupt(struct drm_device *dev, uint32_t vdc_stat)
189 if (vdc_stat & _PSB_IRQ_ASLE)
190 psb_intel_opregion_asle_intr(dev);
192 if (vdc_stat & _PSB_VSYNC_PIPEA_FLAG)
193 mid_pipe_event_handler(dev, 0);
195 if (vdc_stat & _PSB_VSYNC_PIPEB_FLAG)
196 mid_pipe_event_handler(dev, 1);
200 * SGX interrupt handler
202 static void psb_sgx_interrupt(struct drm_device *dev, u32 stat_1, u32 stat_2)
204 struct drm_psb_private *dev_priv = dev->dev_private;
207 if (stat_1 & _PSB_CE_TWOD_COMPLETE)
208 val = PSB_RSGX32(PSB_CR_2D_BLIT_STATUS);
210 if (stat_2 & _PSB_CE2_BIF_REQUESTER_FAULT) {
211 val = PSB_RSGX32(PSB_CR_BIF_INT_STAT);
212 addr = PSB_RSGX32(PSB_CR_BIF_FAULT);
214 if (val & _PSB_CBI_STAT_PF_N_RW)
215 DRM_ERROR("SGX MMU page fault:");
217 DRM_ERROR("SGX MMU read / write protection fault:");
219 if (val & _PSB_CBI_STAT_FAULT_CACHE)
220 DRM_ERROR("\tCache requestor");
221 if (val & _PSB_CBI_STAT_FAULT_TA)
222 DRM_ERROR("\tTA requestor");
223 if (val & _PSB_CBI_STAT_FAULT_VDM)
224 DRM_ERROR("\tVDM requestor");
225 if (val & _PSB_CBI_STAT_FAULT_2D)
226 DRM_ERROR("\t2D requestor");
227 if (val & _PSB_CBI_STAT_FAULT_PBE)
228 DRM_ERROR("\tPBE requestor");
229 if (val & _PSB_CBI_STAT_FAULT_TSP)
230 DRM_ERROR("\tTSP requestor");
231 if (val & _PSB_CBI_STAT_FAULT_ISP)
232 DRM_ERROR("\tISP requestor");
233 if (val & _PSB_CBI_STAT_FAULT_USSEPDS)
234 DRM_ERROR("\tUSSEPDS requestor");
235 if (val & _PSB_CBI_STAT_FAULT_HOST)
236 DRM_ERROR("\tHost requestor");
238 DRM_ERROR("\tMMU failing address is 0x%08x.\n",
244 PSB_WSGX32(stat_1, PSB_CR_EVENT_HOST_CLEAR);
245 PSB_WSGX32(stat_2, PSB_CR_EVENT_HOST_CLEAR2);
246 PSB_RSGX32(PSB_CR_EVENT_HOST_CLEAR2);
249 irqreturn_t psb_irq_handler(int irq, void *arg)
251 struct drm_device *dev = arg;
252 struct drm_psb_private *dev_priv = dev->dev_private;
253 uint32_t vdc_stat, dsp_int = 0, sgx_int = 0, hotplug_int = 0;
254 u32 sgx_stat_1, sgx_stat_2;
257 spin_lock(&dev_priv->irqmask_lock);
259 vdc_stat = PSB_RVDC32(PSB_INT_IDENTITY_R);
261 if (vdc_stat & (_PSB_PIPE_EVENT_FLAG|_PSB_IRQ_ASLE))
264 if (vdc_stat & _PSB_IRQ_SGX_FLAG)
266 if (vdc_stat & _PSB_IRQ_DISP_HOTSYNC)
269 vdc_stat &= dev_priv->vdc_irq_mask;
270 spin_unlock(&dev_priv->irqmask_lock);
272 if (dsp_int && gma_power_is_on(dev)) {
273 psb_vdc_interrupt(dev, vdc_stat);
278 sgx_stat_1 = PSB_RSGX32(PSB_CR_EVENT_STATUS);
279 sgx_stat_2 = PSB_RSGX32(PSB_CR_EVENT_STATUS2);
280 psb_sgx_interrupt(dev, sgx_stat_1, sgx_stat_2);
284 /* Note: this bit has other meanings on some devices, so we will
285 need to address that later if it ever matters */
286 if (hotplug_int && dev_priv->ops->hotplug) {
287 handled = dev_priv->ops->hotplug(dev);
288 REG_WRITE(PORT_HOTPLUG_STAT, REG_READ(PORT_HOTPLUG_STAT));
291 PSB_WVDC32(vdc_stat, PSB_INT_IDENTITY_R);
292 (void) PSB_RVDC32(PSB_INT_IDENTITY_R);
301 void psb_irq_preinstall(struct drm_device *dev)
303 struct drm_psb_private *dev_priv =
304 (struct drm_psb_private *) dev->dev_private;
305 unsigned long irqflags;
307 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
309 if (gma_power_is_on(dev)) {
310 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
311 PSB_WVDC32(0x00000000, PSB_INT_MASK_R);
312 PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R);
313 PSB_WSGX32(0x00000000, PSB_CR_EVENT_HOST_ENABLE);
314 PSB_RSGX32(PSB_CR_EVENT_HOST_ENABLE);
316 if (dev->vblank[0].enabled)
317 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEA_FLAG;
318 if (dev->vblank[1].enabled)
319 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEB_FLAG;
321 /* Revisit this area - want per device masks ? */
322 if (dev_priv->ops->hotplug)
323 dev_priv->vdc_irq_mask |= _PSB_IRQ_DISP_HOTSYNC;
324 dev_priv->vdc_irq_mask |= _PSB_IRQ_ASLE | _PSB_IRQ_SGX_FLAG;
326 /* This register is safe even if display island is off */
327 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
328 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
331 int psb_irq_postinstall(struct drm_device *dev)
333 struct drm_psb_private *dev_priv = dev->dev_private;
334 unsigned long irqflags;
337 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
339 /* Enable 2D and MMU fault interrupts */
340 PSB_WSGX32(_PSB_CE2_BIF_REQUESTER_FAULT, PSB_CR_EVENT_HOST_ENABLE2);
341 PSB_WSGX32(_PSB_CE_TWOD_COMPLETE, PSB_CR_EVENT_HOST_ENABLE);
342 PSB_RSGX32(PSB_CR_EVENT_HOST_ENABLE); /* Post */
344 /* This register is safe even if display island is off */
345 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
346 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
348 for (i = 0; i < dev->num_crtcs; ++i) {
349 if (dev->vblank[i].enabled)
350 psb_enable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
352 psb_disable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
355 if (dev_priv->ops->hotplug_enable)
356 dev_priv->ops->hotplug_enable(dev, true);
358 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
362 void psb_irq_uninstall(struct drm_device *dev)
364 struct drm_psb_private *dev_priv = dev->dev_private;
365 unsigned long irqflags;
368 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
370 if (dev_priv->ops->hotplug_enable)
371 dev_priv->ops->hotplug_enable(dev, false);
373 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
375 for (i = 0; i < dev->num_crtcs; ++i) {
376 if (dev->vblank[i].enabled)
377 psb_disable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
380 dev_priv->vdc_irq_mask &= _PSB_IRQ_SGX_FLAG |
381 _PSB_IRQ_MSVDX_FLAG |
384 /* These two registers are safe even if display island is off */
385 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
386 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
390 /* This register is safe even if display island is off */
391 PSB_WVDC32(PSB_RVDC32(PSB_INT_IDENTITY_R), PSB_INT_IDENTITY_R);
392 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
395 void psb_irq_turn_on_dpst(struct drm_device *dev)
397 struct drm_psb_private *dev_priv =
398 (struct drm_psb_private *) dev->dev_private;
402 if (gma_power_begin(dev, false)) {
403 PSB_WVDC32(1 << 31, HISTOGRAM_LOGIC_CONTROL);
404 hist_reg = PSB_RVDC32(HISTOGRAM_LOGIC_CONTROL);
405 PSB_WVDC32(1 << 31, HISTOGRAM_INT_CONTROL);
406 hist_reg = PSB_RVDC32(HISTOGRAM_INT_CONTROL);
408 PSB_WVDC32(0x80010100, PWM_CONTROL_LOGIC);
409 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
410 PSB_WVDC32(pwm_reg | PWM_PHASEIN_ENABLE
411 | PWM_PHASEIN_INT_ENABLE,
413 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
415 psb_enable_pipestat(dev_priv, 0, PIPE_DPST_EVENT_ENABLE);
417 hist_reg = PSB_RVDC32(HISTOGRAM_INT_CONTROL);
418 PSB_WVDC32(hist_reg | HISTOGRAM_INT_CTRL_CLEAR,
419 HISTOGRAM_INT_CONTROL);
420 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
421 PSB_WVDC32(pwm_reg | 0x80010100 | PWM_PHASEIN_ENABLE,
428 int psb_irq_enable_dpst(struct drm_device *dev)
430 struct drm_psb_private *dev_priv =
431 (struct drm_psb_private *) dev->dev_private;
432 unsigned long irqflags;
434 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
437 mid_enable_pipe_event(dev_priv, 0);
438 psb_irq_turn_on_dpst(dev);
440 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
444 void psb_irq_turn_off_dpst(struct drm_device *dev)
446 struct drm_psb_private *dev_priv =
447 (struct drm_psb_private *) dev->dev_private;
450 if (gma_power_begin(dev, false)) {
451 PSB_WVDC32(0x00000000, HISTOGRAM_INT_CONTROL);
452 PSB_RVDC32(HISTOGRAM_INT_CONTROL);
454 psb_disable_pipestat(dev_priv, 0, PIPE_DPST_EVENT_ENABLE);
456 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
457 PSB_WVDC32(pwm_reg & ~PWM_PHASEIN_INT_ENABLE,
459 pwm_reg = PSB_RVDC32(PWM_CONTROL_LOGIC);
465 int psb_irq_disable_dpst(struct drm_device *dev)
467 struct drm_psb_private *dev_priv =
468 (struct drm_psb_private *) dev->dev_private;
469 unsigned long irqflags;
471 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
473 mid_disable_pipe_event(dev_priv, 0);
474 psb_irq_turn_off_dpst(dev);
476 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
482 * It is used to enable VBLANK interrupt
484 int psb_enable_vblank(struct drm_crtc *crtc)
486 struct drm_device *dev = crtc->dev;
487 unsigned int pipe = crtc->index;
488 struct drm_psb_private *dev_priv = dev->dev_private;
489 unsigned long irqflags;
490 uint32_t reg_val = 0;
491 uint32_t pipeconf_reg = mid_pipeconf(pipe);
493 if (gma_power_begin(dev, false)) {
494 reg_val = REG_READ(pipeconf_reg);
498 if (!(reg_val & PIPEACONF_ENABLE))
501 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
504 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEA_FLAG;
506 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEB_FLAG;
508 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
509 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
510 psb_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_ENABLE);
512 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
518 * It is used to disable VBLANK interrupt
520 void psb_disable_vblank(struct drm_crtc *crtc)
522 struct drm_device *dev = crtc->dev;
523 unsigned int pipe = crtc->index;
524 struct drm_psb_private *dev_priv = dev->dev_private;
525 unsigned long irqflags;
527 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
530 dev_priv->vdc_irq_mask &= ~_PSB_VSYNC_PIPEA_FLAG;
532 dev_priv->vdc_irq_mask &= ~_PSB_VSYNC_PIPEB_FLAG;
534 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
535 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
536 psb_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_ENABLE);
538 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
541 /* Called from drm generic code, passed a 'crtc', which
542 * we use as a pipe index
544 u32 psb_get_vblank_counter(struct drm_crtc *crtc)
546 struct drm_device *dev = crtc->dev;
547 unsigned int pipe = crtc->index;
548 uint32_t high_frame = PIPEAFRAMEHIGH;
549 uint32_t low_frame = PIPEAFRAMEPIXEL;
550 uint32_t pipeconf_reg = PIPEACONF;
551 uint32_t reg_val = 0;
552 uint32_t high1 = 0, high2 = 0, low = 0, count = 0;
558 high_frame = PIPEBFRAMEHIGH;
559 low_frame = PIPEBFRAMEPIXEL;
560 pipeconf_reg = PIPEBCONF;
563 high_frame = PIPECFRAMEHIGH;
564 low_frame = PIPECFRAMEPIXEL;
565 pipeconf_reg = PIPECCONF;
568 dev_err(dev->dev, "%s, invalid pipe.\n", __func__);
572 if (!gma_power_begin(dev, false))
575 reg_val = REG_READ(pipeconf_reg);
577 if (!(reg_val & PIPEACONF_ENABLE)) {
578 dev_err(dev->dev, "trying to get vblank count for disabled pipe %u\n",
580 goto psb_get_vblank_counter_exit;
584 * High & low register fields aren't synchronized, so make sure
585 * we get a low value that's stable across two reads of the high
589 high1 = ((REG_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
590 PIPE_FRAME_HIGH_SHIFT);
591 low = ((REG_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
592 PIPE_FRAME_LOW_SHIFT);
593 high2 = ((REG_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
594 PIPE_FRAME_HIGH_SHIFT);
595 } while (high1 != high2);
597 count = (high1 << 8) | low;
599 psb_get_vblank_counter_exit: