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_drv.h>
12 #include <drm/drm_vblank.h>
16 #include "psb_intel_reg.h"
25 psb_pipestat(int pipe)
37 mid_pipe_event(int pipe)
40 return _PSB_PIPEA_EVENT_FLAG;
42 return _MDFLD_PIPEB_EVENT_FLAG;
44 return _MDFLD_PIPEC_EVENT_FLAG;
49 mid_pipe_vsync(int pipe)
52 return _PSB_VSYNC_PIPEA_FLAG;
54 return _PSB_VSYNC_PIPEB_FLAG;
56 return _MDFLD_PIPEC_VBLANK_FLAG;
61 mid_pipeconf(int pipe)
73 psb_enable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask)
75 if ((dev_priv->pipestat[pipe] & mask) != mask) {
76 u32 reg = psb_pipestat(pipe);
77 dev_priv->pipestat[pipe] |= mask;
78 /* Enable the interrupt, clear any pending status */
79 if (gma_power_begin(dev_priv->dev, false)) {
80 u32 writeVal = PSB_RVDC32(reg);
81 writeVal |= (mask | (mask >> 16));
82 PSB_WVDC32(writeVal, reg);
83 (void) PSB_RVDC32(reg);
84 gma_power_end(dev_priv->dev);
90 psb_disable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask)
92 if ((dev_priv->pipestat[pipe] & mask) != 0) {
93 u32 reg = psb_pipestat(pipe);
94 dev_priv->pipestat[pipe] &= ~mask;
95 if (gma_power_begin(dev_priv->dev, false)) {
96 u32 writeVal = PSB_RVDC32(reg);
98 PSB_WVDC32(writeVal, reg);
99 (void) PSB_RVDC32(reg);
100 gma_power_end(dev_priv->dev);
106 * Display controller interrupt handler for pipe event.
108 static void mid_pipe_event_handler(struct drm_device *dev, int pipe)
110 struct drm_psb_private *dev_priv =
111 (struct drm_psb_private *) dev->dev_private;
113 uint32_t pipe_stat_val = 0;
114 uint32_t pipe_stat_reg = psb_pipestat(pipe);
115 uint32_t pipe_enable = dev_priv->pipestat[pipe];
116 uint32_t pipe_status = dev_priv->pipestat[pipe] >> 16;
120 spin_lock(&dev_priv->irqmask_lock);
122 pipe_stat_val = PSB_RVDC32(pipe_stat_reg);
123 pipe_stat_val &= pipe_enable | pipe_status;
124 pipe_stat_val &= pipe_stat_val >> 16;
126 spin_unlock(&dev_priv->irqmask_lock);
128 /* Clear the 2nd level interrupt status bits
129 * Sometimes the bits are very sticky so we repeat until they unstick */
130 for (i = 0; i < 0xffff; i++) {
131 PSB_WVDC32(PSB_RVDC32(pipe_stat_reg), pipe_stat_reg);
132 pipe_clear = PSB_RVDC32(pipe_stat_reg) & pipe_status;
140 "%s, can't clear status bits for pipe %d, its value = 0x%x.\n",
141 __func__, pipe, PSB_RVDC32(pipe_stat_reg));
143 if (pipe_stat_val & PIPE_VBLANK_STATUS) {
144 struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
145 struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
148 drm_handle_vblank(dev, pipe);
150 spin_lock_irqsave(&dev->event_lock, flags);
151 if (gma_crtc->page_flip_event) {
152 drm_crtc_send_vblank_event(crtc,
153 gma_crtc->page_flip_event);
154 gma_crtc->page_flip_event = NULL;
155 drm_crtc_vblank_put(crtc);
157 spin_unlock_irqrestore(&dev->event_lock, flags);
162 * Display controller interrupt handler.
164 static void psb_vdc_interrupt(struct drm_device *dev, uint32_t vdc_stat)
166 if (vdc_stat & _PSB_IRQ_ASLE)
167 psb_intel_opregion_asle_intr(dev);
169 if (vdc_stat & _PSB_VSYNC_PIPEA_FLAG)
170 mid_pipe_event_handler(dev, 0);
172 if (vdc_stat & _PSB_VSYNC_PIPEB_FLAG)
173 mid_pipe_event_handler(dev, 1);
177 * SGX interrupt handler
179 static void psb_sgx_interrupt(struct drm_device *dev, u32 stat_1, u32 stat_2)
181 struct drm_psb_private *dev_priv = dev->dev_private;
184 if (stat_1 & _PSB_CE_TWOD_COMPLETE)
185 val = PSB_RSGX32(PSB_CR_2D_BLIT_STATUS);
187 if (stat_2 & _PSB_CE2_BIF_REQUESTER_FAULT) {
188 val = PSB_RSGX32(PSB_CR_BIF_INT_STAT);
189 addr = PSB_RSGX32(PSB_CR_BIF_FAULT);
191 if (val & _PSB_CBI_STAT_PF_N_RW)
192 DRM_ERROR("SGX MMU page fault:");
194 DRM_ERROR("SGX MMU read / write protection fault:");
196 if (val & _PSB_CBI_STAT_FAULT_CACHE)
197 DRM_ERROR("\tCache requestor");
198 if (val & _PSB_CBI_STAT_FAULT_TA)
199 DRM_ERROR("\tTA requestor");
200 if (val & _PSB_CBI_STAT_FAULT_VDM)
201 DRM_ERROR("\tVDM requestor");
202 if (val & _PSB_CBI_STAT_FAULT_2D)
203 DRM_ERROR("\t2D requestor");
204 if (val & _PSB_CBI_STAT_FAULT_PBE)
205 DRM_ERROR("\tPBE requestor");
206 if (val & _PSB_CBI_STAT_FAULT_TSP)
207 DRM_ERROR("\tTSP requestor");
208 if (val & _PSB_CBI_STAT_FAULT_ISP)
209 DRM_ERROR("\tISP requestor");
210 if (val & _PSB_CBI_STAT_FAULT_USSEPDS)
211 DRM_ERROR("\tUSSEPDS requestor");
212 if (val & _PSB_CBI_STAT_FAULT_HOST)
213 DRM_ERROR("\tHost requestor");
215 DRM_ERROR("\tMMU failing address is 0x%08x.\n",
221 PSB_WSGX32(stat_1, PSB_CR_EVENT_HOST_CLEAR);
222 PSB_WSGX32(stat_2, PSB_CR_EVENT_HOST_CLEAR2);
223 PSB_RSGX32(PSB_CR_EVENT_HOST_CLEAR2);
226 static irqreturn_t psb_irq_handler(int irq, void *arg)
228 struct drm_device *dev = arg;
229 struct drm_psb_private *dev_priv = dev->dev_private;
230 uint32_t vdc_stat, dsp_int = 0, sgx_int = 0, hotplug_int = 0;
231 u32 sgx_stat_1, sgx_stat_2;
234 spin_lock(&dev_priv->irqmask_lock);
236 vdc_stat = PSB_RVDC32(PSB_INT_IDENTITY_R);
238 if (vdc_stat & (_PSB_PIPE_EVENT_FLAG|_PSB_IRQ_ASLE))
241 if (vdc_stat & _PSB_IRQ_SGX_FLAG)
243 if (vdc_stat & _PSB_IRQ_DISP_HOTSYNC)
246 vdc_stat &= dev_priv->vdc_irq_mask;
247 spin_unlock(&dev_priv->irqmask_lock);
249 if (dsp_int && gma_power_is_on(dev)) {
250 psb_vdc_interrupt(dev, vdc_stat);
255 sgx_stat_1 = PSB_RSGX32(PSB_CR_EVENT_STATUS);
256 sgx_stat_2 = PSB_RSGX32(PSB_CR_EVENT_STATUS2);
257 psb_sgx_interrupt(dev, sgx_stat_1, sgx_stat_2);
261 /* Note: this bit has other meanings on some devices, so we will
262 need to address that later if it ever matters */
263 if (hotplug_int && dev_priv->ops->hotplug) {
264 handled = dev_priv->ops->hotplug(dev);
265 REG_WRITE(PORT_HOTPLUG_STAT, REG_READ(PORT_HOTPLUG_STAT));
268 PSB_WVDC32(vdc_stat, PSB_INT_IDENTITY_R);
269 (void) PSB_RVDC32(PSB_INT_IDENTITY_R);
278 void psb_irq_preinstall(struct drm_device *dev)
280 struct drm_psb_private *dev_priv =
281 (struct drm_psb_private *) dev->dev_private;
282 unsigned long irqflags;
284 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
286 if (gma_power_is_on(dev)) {
287 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
288 PSB_WVDC32(0x00000000, PSB_INT_MASK_R);
289 PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R);
290 PSB_WSGX32(0x00000000, PSB_CR_EVENT_HOST_ENABLE);
291 PSB_RSGX32(PSB_CR_EVENT_HOST_ENABLE);
293 if (dev->vblank[0].enabled)
294 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEA_FLAG;
295 if (dev->vblank[1].enabled)
296 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEB_FLAG;
298 /* Revisit this area - want per device masks ? */
299 if (dev_priv->ops->hotplug)
300 dev_priv->vdc_irq_mask |= _PSB_IRQ_DISP_HOTSYNC;
301 dev_priv->vdc_irq_mask |= _PSB_IRQ_ASLE | _PSB_IRQ_SGX_FLAG;
303 /* This register is safe even if display island is off */
304 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
305 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
308 void psb_irq_postinstall(struct drm_device *dev)
310 struct drm_psb_private *dev_priv = dev->dev_private;
311 unsigned long irqflags;
314 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
316 /* Enable 2D and MMU fault interrupts */
317 PSB_WSGX32(_PSB_CE2_BIF_REQUESTER_FAULT, PSB_CR_EVENT_HOST_ENABLE2);
318 PSB_WSGX32(_PSB_CE_TWOD_COMPLETE, PSB_CR_EVENT_HOST_ENABLE);
319 PSB_RSGX32(PSB_CR_EVENT_HOST_ENABLE); /* Post */
321 /* This register is safe even if display island is off */
322 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
323 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
325 for (i = 0; i < dev->num_crtcs; ++i) {
326 if (dev->vblank[i].enabled)
327 psb_enable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
329 psb_disable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
332 if (dev_priv->ops->hotplug_enable)
333 dev_priv->ops->hotplug_enable(dev, true);
335 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
338 int psb_irq_install(struct drm_device *dev, unsigned int irq)
342 if (irq == IRQ_NOTCONNECTED)
345 psb_irq_preinstall(dev);
347 /* PCI devices require shared interrupts. */
348 ret = request_irq(irq, psb_irq_handler, IRQF_SHARED, dev->driver->name, dev);
352 psb_irq_postinstall(dev);
357 void psb_irq_uninstall(struct drm_device *dev)
359 struct drm_psb_private *dev_priv = dev->dev_private;
360 struct pci_dev *pdev = to_pci_dev(dev->dev);
361 unsigned long irqflags;
364 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
366 if (dev_priv->ops->hotplug_enable)
367 dev_priv->ops->hotplug_enable(dev, false);
369 PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
371 for (i = 0; i < dev->num_crtcs; ++i) {
372 if (dev->vblank[i].enabled)
373 psb_disable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
376 dev_priv->vdc_irq_mask &= _PSB_IRQ_SGX_FLAG |
377 _PSB_IRQ_MSVDX_FLAG |
380 /* These two registers are safe even if display island is off */
381 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
382 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
386 /* This register is safe even if display island is off */
387 PSB_WVDC32(PSB_RVDC32(PSB_INT_IDENTITY_R), PSB_INT_IDENTITY_R);
388 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
390 free_irq(pdev->irq, dev);
394 * It is used to enable VBLANK interrupt
396 int psb_enable_vblank(struct drm_crtc *crtc)
398 struct drm_device *dev = crtc->dev;
399 unsigned int pipe = crtc->index;
400 struct drm_psb_private *dev_priv = dev->dev_private;
401 unsigned long irqflags;
402 uint32_t reg_val = 0;
403 uint32_t pipeconf_reg = mid_pipeconf(pipe);
405 if (gma_power_begin(dev, false)) {
406 reg_val = REG_READ(pipeconf_reg);
410 if (!(reg_val & PIPEACONF_ENABLE))
413 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
416 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEA_FLAG;
418 dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEB_FLAG;
420 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
421 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
422 psb_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_ENABLE);
424 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
430 * It is used to disable VBLANK interrupt
432 void psb_disable_vblank(struct drm_crtc *crtc)
434 struct drm_device *dev = crtc->dev;
435 unsigned int pipe = crtc->index;
436 struct drm_psb_private *dev_priv = dev->dev_private;
437 unsigned long irqflags;
439 spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
442 dev_priv->vdc_irq_mask &= ~_PSB_VSYNC_PIPEA_FLAG;
444 dev_priv->vdc_irq_mask &= ~_PSB_VSYNC_PIPEB_FLAG;
446 PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
447 PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
448 psb_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_ENABLE);
450 spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
453 /* Called from drm generic code, passed a 'crtc', which
454 * we use as a pipe index
456 u32 psb_get_vblank_counter(struct drm_crtc *crtc)
458 struct drm_device *dev = crtc->dev;
459 unsigned int pipe = crtc->index;
460 uint32_t high_frame = PIPEAFRAMEHIGH;
461 uint32_t low_frame = PIPEAFRAMEPIXEL;
462 uint32_t pipeconf_reg = PIPEACONF;
463 uint32_t reg_val = 0;
464 uint32_t high1 = 0, high2 = 0, low = 0, count = 0;
470 high_frame = PIPEBFRAMEHIGH;
471 low_frame = PIPEBFRAMEPIXEL;
472 pipeconf_reg = PIPEBCONF;
475 high_frame = PIPECFRAMEHIGH;
476 low_frame = PIPECFRAMEPIXEL;
477 pipeconf_reg = PIPECCONF;
480 dev_err(dev->dev, "%s, invalid pipe.\n", __func__);
484 if (!gma_power_begin(dev, false))
487 reg_val = REG_READ(pipeconf_reg);
489 if (!(reg_val & PIPEACONF_ENABLE)) {
490 dev_err(dev->dev, "trying to get vblank count for disabled pipe %u\n",
492 goto psb_get_vblank_counter_exit;
496 * High & low register fields aren't synchronized, so make sure
497 * we get a low value that's stable across two reads of the high
501 high1 = ((REG_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
502 PIPE_FRAME_HIGH_SHIFT);
503 low = ((REG_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
504 PIPE_FRAME_LOW_SHIFT);
505 high2 = ((REG_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
506 PIPE_FRAME_HIGH_SHIFT);
507 } while (high1 != high2);
509 count = (high1 << 8) | low;
511 psb_get_vblank_counter_exit: