psb_irq.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /**************************************************************************
  3. * Copyright (c) 2007, Intel Corporation.
  4. * All Rights Reserved.
  5. *
  6. * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
  7. * develop this driver.
  8. *
  9. **************************************************************************/
  10. #include <drm/drm_drv.h>
  11. #include <drm/drm_vblank.h>
  12. #include "power.h"
  13. #include "psb_drv.h"
  14. #include "psb_intel_reg.h"
  15. #include "psb_irq.h"
  16. #include "psb_reg.h"
  17. /*
  18. * inline functions
  19. */
  20. static inline u32 gma_pipestat(int pipe)
  21. {
  22. if (pipe == 0)
  23. return PIPEASTAT;
  24. if (pipe == 1)
  25. return PIPEBSTAT;
  26. if (pipe == 2)
  27. return PIPECSTAT;
  28. BUG();
  29. }
  30. static inline u32 gma_pipeconf(int pipe)
  31. {
  32. if (pipe == 0)
  33. return PIPEACONF;
  34. if (pipe == 1)
  35. return PIPEBCONF;
  36. if (pipe == 2)
  37. return PIPECCONF;
  38. BUG();
  39. }
  40. void gma_enable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask)
  41. {
  42. if ((dev_priv->pipestat[pipe] & mask) != mask) {
  43. u32 reg = gma_pipestat(pipe);
  44. dev_priv->pipestat[pipe] |= mask;
  45. /* Enable the interrupt, clear any pending status */
  46. if (gma_power_begin(&dev_priv->dev, false)) {
  47. u32 writeVal = PSB_RVDC32(reg);
  48. writeVal |= (mask | (mask >> 16));
  49. PSB_WVDC32(writeVal, reg);
  50. (void) PSB_RVDC32(reg);
  51. gma_power_end(&dev_priv->dev);
  52. }
  53. }
  54. }
  55. void gma_disable_pipestat(struct drm_psb_private *dev_priv, int pipe, u32 mask)
  56. {
  57. if ((dev_priv->pipestat[pipe] & mask) != 0) {
  58. u32 reg = gma_pipestat(pipe);
  59. dev_priv->pipestat[pipe] &= ~mask;
  60. if (gma_power_begin(&dev_priv->dev, false)) {
  61. u32 writeVal = PSB_RVDC32(reg);
  62. writeVal &= ~mask;
  63. PSB_WVDC32(writeVal, reg);
  64. (void) PSB_RVDC32(reg);
  65. gma_power_end(&dev_priv->dev);
  66. }
  67. }
  68. }
  69. /*
  70. * Display controller interrupt handler for pipe event.
  71. */
  72. static void gma_pipe_event_handler(struct drm_device *dev, int pipe)
  73. {
  74. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  75. uint32_t pipe_stat_val = 0;
  76. uint32_t pipe_stat_reg = gma_pipestat(pipe);
  77. uint32_t pipe_enable = dev_priv->pipestat[pipe];
  78. uint32_t pipe_status = dev_priv->pipestat[pipe] >> 16;
  79. uint32_t pipe_clear;
  80. uint32_t i = 0;
  81. spin_lock(&dev_priv->irqmask_lock);
  82. pipe_stat_val = PSB_RVDC32(pipe_stat_reg);
  83. pipe_stat_val &= pipe_enable | pipe_status;
  84. pipe_stat_val &= pipe_stat_val >> 16;
  85. spin_unlock(&dev_priv->irqmask_lock);
  86. /* Clear the 2nd level interrupt status bits
  87. * Sometimes the bits are very sticky so we repeat until they unstick */
  88. for (i = 0; i < 0xffff; i++) {
  89. PSB_WVDC32(PSB_RVDC32(pipe_stat_reg), pipe_stat_reg);
  90. pipe_clear = PSB_RVDC32(pipe_stat_reg) & pipe_status;
  91. if (pipe_clear == 0)
  92. break;
  93. }
  94. if (pipe_clear)
  95. dev_err(dev->dev,
  96. "%s, can't clear status bits for pipe %d, its value = 0x%x.\n",
  97. __func__, pipe, PSB_RVDC32(pipe_stat_reg));
  98. if (pipe_stat_val & PIPE_VBLANK_STATUS) {
  99. struct drm_crtc *crtc = drm_crtc_from_index(dev, pipe);
  100. struct gma_crtc *gma_crtc = to_gma_crtc(crtc);
  101. unsigned long flags;
  102. drm_handle_vblank(dev, pipe);
  103. spin_lock_irqsave(&dev->event_lock, flags);
  104. if (gma_crtc->page_flip_event) {
  105. drm_crtc_send_vblank_event(crtc,
  106. gma_crtc->page_flip_event);
  107. gma_crtc->page_flip_event = NULL;
  108. drm_crtc_vblank_put(crtc);
  109. }
  110. spin_unlock_irqrestore(&dev->event_lock, flags);
  111. }
  112. }
  113. /*
  114. * Display controller interrupt handler.
  115. */
  116. static void gma_vdc_interrupt(struct drm_device *dev, uint32_t vdc_stat)
  117. {
  118. if (vdc_stat & _PSB_IRQ_ASLE)
  119. psb_intel_opregion_asle_intr(dev);
  120. if (vdc_stat & _PSB_VSYNC_PIPEA_FLAG)
  121. gma_pipe_event_handler(dev, 0);
  122. if (vdc_stat & _PSB_VSYNC_PIPEB_FLAG)
  123. gma_pipe_event_handler(dev, 1);
  124. }
  125. /*
  126. * SGX interrupt handler
  127. */
  128. static void gma_sgx_interrupt(struct drm_device *dev, u32 stat_1, u32 stat_2)
  129. {
  130. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  131. u32 val, addr;
  132. if (stat_1 & _PSB_CE_TWOD_COMPLETE)
  133. val = PSB_RSGX32(PSB_CR_2D_BLIT_STATUS);
  134. if (stat_2 & _PSB_CE2_BIF_REQUESTER_FAULT) {
  135. val = PSB_RSGX32(PSB_CR_BIF_INT_STAT);
  136. addr = PSB_RSGX32(PSB_CR_BIF_FAULT);
  137. if (val) {
  138. if (val & _PSB_CBI_STAT_PF_N_RW)
  139. DRM_ERROR("SGX MMU page fault:");
  140. else
  141. DRM_ERROR("SGX MMU read / write protection fault:");
  142. if (val & _PSB_CBI_STAT_FAULT_CACHE)
  143. DRM_ERROR("\tCache requestor");
  144. if (val & _PSB_CBI_STAT_FAULT_TA)
  145. DRM_ERROR("\tTA requestor");
  146. if (val & _PSB_CBI_STAT_FAULT_VDM)
  147. DRM_ERROR("\tVDM requestor");
  148. if (val & _PSB_CBI_STAT_FAULT_2D)
  149. DRM_ERROR("\t2D requestor");
  150. if (val & _PSB_CBI_STAT_FAULT_PBE)
  151. DRM_ERROR("\tPBE requestor");
  152. if (val & _PSB_CBI_STAT_FAULT_TSP)
  153. DRM_ERROR("\tTSP requestor");
  154. if (val & _PSB_CBI_STAT_FAULT_ISP)
  155. DRM_ERROR("\tISP requestor");
  156. if (val & _PSB_CBI_STAT_FAULT_USSEPDS)
  157. DRM_ERROR("\tUSSEPDS requestor");
  158. if (val & _PSB_CBI_STAT_FAULT_HOST)
  159. DRM_ERROR("\tHost requestor");
  160. DRM_ERROR("\tMMU failing address is 0x%08x.\n",
  161. (unsigned int)addr);
  162. }
  163. }
  164. /* Clear bits */
  165. PSB_WSGX32(stat_1, PSB_CR_EVENT_HOST_CLEAR);
  166. PSB_WSGX32(stat_2, PSB_CR_EVENT_HOST_CLEAR2);
  167. PSB_RSGX32(PSB_CR_EVENT_HOST_CLEAR2);
  168. }
  169. static irqreturn_t gma_irq_handler(int irq, void *arg)
  170. {
  171. struct drm_device *dev = arg;
  172. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  173. uint32_t vdc_stat, dsp_int = 0, sgx_int = 0, hotplug_int = 0;
  174. u32 sgx_stat_1, sgx_stat_2;
  175. int handled = 0;
  176. spin_lock(&dev_priv->irqmask_lock);
  177. vdc_stat = PSB_RVDC32(PSB_INT_IDENTITY_R);
  178. if (vdc_stat & (_PSB_PIPE_EVENT_FLAG|_PSB_IRQ_ASLE))
  179. dsp_int = 1;
  180. if (vdc_stat & _PSB_IRQ_SGX_FLAG)
  181. sgx_int = 1;
  182. if (vdc_stat & _PSB_IRQ_DISP_HOTSYNC)
  183. hotplug_int = 1;
  184. vdc_stat &= dev_priv->vdc_irq_mask;
  185. spin_unlock(&dev_priv->irqmask_lock);
  186. if (dsp_int) {
  187. gma_vdc_interrupt(dev, vdc_stat);
  188. handled = 1;
  189. }
  190. if (sgx_int) {
  191. sgx_stat_1 = PSB_RSGX32(PSB_CR_EVENT_STATUS);
  192. sgx_stat_2 = PSB_RSGX32(PSB_CR_EVENT_STATUS2);
  193. gma_sgx_interrupt(dev, sgx_stat_1, sgx_stat_2);
  194. handled = 1;
  195. }
  196. /* Note: this bit has other meanings on some devices, so we will
  197. need to address that later if it ever matters */
  198. if (hotplug_int && dev_priv->ops->hotplug) {
  199. handled = dev_priv->ops->hotplug(dev);
  200. REG_WRITE(PORT_HOTPLUG_STAT, REG_READ(PORT_HOTPLUG_STAT));
  201. }
  202. PSB_WVDC32(vdc_stat, PSB_INT_IDENTITY_R);
  203. (void) PSB_RVDC32(PSB_INT_IDENTITY_R);
  204. rmb();
  205. if (!handled)
  206. return IRQ_NONE;
  207. return IRQ_HANDLED;
  208. }
  209. void gma_irq_preinstall(struct drm_device *dev)
  210. {
  211. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  212. unsigned long irqflags;
  213. spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
  214. PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
  215. PSB_WVDC32(0x00000000, PSB_INT_MASK_R);
  216. PSB_WVDC32(0x00000000, PSB_INT_ENABLE_R);
  217. PSB_WSGX32(0x00000000, PSB_CR_EVENT_HOST_ENABLE);
  218. PSB_RSGX32(PSB_CR_EVENT_HOST_ENABLE);
  219. if (dev->vblank[0].enabled)
  220. dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEA_FLAG;
  221. if (dev->vblank[1].enabled)
  222. dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEB_FLAG;
  223. /* Revisit this area - want per device masks ? */
  224. if (dev_priv->ops->hotplug)
  225. dev_priv->vdc_irq_mask |= _PSB_IRQ_DISP_HOTSYNC;
  226. dev_priv->vdc_irq_mask |= _PSB_IRQ_ASLE | _PSB_IRQ_SGX_FLAG;
  227. /* This register is safe even if display island is off */
  228. PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
  229. spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
  230. }
  231. void gma_irq_postinstall(struct drm_device *dev)
  232. {
  233. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  234. unsigned long irqflags;
  235. unsigned int i;
  236. spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
  237. /* Enable 2D and MMU fault interrupts */
  238. PSB_WSGX32(_PSB_CE2_BIF_REQUESTER_FAULT, PSB_CR_EVENT_HOST_ENABLE2);
  239. PSB_WSGX32(_PSB_CE_TWOD_COMPLETE, PSB_CR_EVENT_HOST_ENABLE);
  240. PSB_RSGX32(PSB_CR_EVENT_HOST_ENABLE); /* Post */
  241. /* This register is safe even if display island is off */
  242. PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
  243. PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
  244. for (i = 0; i < dev->num_crtcs; ++i) {
  245. if (dev->vblank[i].enabled)
  246. gma_enable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
  247. else
  248. gma_disable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
  249. }
  250. if (dev_priv->ops->hotplug_enable)
  251. dev_priv->ops->hotplug_enable(dev, true);
  252. spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
  253. }
  254. int gma_irq_install(struct drm_device *dev)
  255. {
  256. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  257. struct pci_dev *pdev = to_pci_dev(dev->dev);
  258. int ret;
  259. if (dev_priv->use_msi && pci_enable_msi(pdev)) {
  260. dev_warn(dev->dev, "Enabling MSI failed!\n");
  261. dev_priv->use_msi = false;
  262. }
  263. if (pdev->irq == IRQ_NOTCONNECTED)
  264. return -ENOTCONN;
  265. gma_irq_preinstall(dev);
  266. /* PCI devices require shared interrupts. */
  267. ret = request_irq(pdev->irq, gma_irq_handler, IRQF_SHARED, dev->driver->name, dev);
  268. if (ret)
  269. return ret;
  270. gma_irq_postinstall(dev);
  271. dev_priv->irq_enabled = true;
  272. return 0;
  273. }
  274. void gma_irq_uninstall(struct drm_device *dev)
  275. {
  276. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  277. struct pci_dev *pdev = to_pci_dev(dev->dev);
  278. unsigned long irqflags;
  279. unsigned int i;
  280. if (!dev_priv->irq_enabled)
  281. return;
  282. spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
  283. if (dev_priv->ops->hotplug_enable)
  284. dev_priv->ops->hotplug_enable(dev, false);
  285. PSB_WVDC32(0xFFFFFFFF, PSB_HWSTAM);
  286. for (i = 0; i < dev->num_crtcs; ++i) {
  287. if (dev->vblank[i].enabled)
  288. gma_disable_pipestat(dev_priv, i, PIPE_VBLANK_INTERRUPT_ENABLE);
  289. }
  290. dev_priv->vdc_irq_mask &= _PSB_IRQ_SGX_FLAG |
  291. _PSB_IRQ_MSVDX_FLAG |
  292. _LNC_IRQ_TOPAZ_FLAG;
  293. /* These two registers are safe even if display island is off */
  294. PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
  295. PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
  296. wmb();
  297. /* This register is safe even if display island is off */
  298. PSB_WVDC32(PSB_RVDC32(PSB_INT_IDENTITY_R), PSB_INT_IDENTITY_R);
  299. spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
  300. free_irq(pdev->irq, dev);
  301. if (dev_priv->use_msi)
  302. pci_disable_msi(pdev);
  303. }
  304. int gma_crtc_enable_vblank(struct drm_crtc *crtc)
  305. {
  306. struct drm_device *dev = crtc->dev;
  307. unsigned int pipe = crtc->index;
  308. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  309. unsigned long irqflags;
  310. uint32_t reg_val = 0;
  311. uint32_t pipeconf_reg = gma_pipeconf(pipe);
  312. if (gma_power_begin(dev, false)) {
  313. reg_val = REG_READ(pipeconf_reg);
  314. gma_power_end(dev);
  315. }
  316. if (!(reg_val & PIPEACONF_ENABLE))
  317. return -EINVAL;
  318. spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
  319. if (pipe == 0)
  320. dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEA_FLAG;
  321. else if (pipe == 1)
  322. dev_priv->vdc_irq_mask |= _PSB_VSYNC_PIPEB_FLAG;
  323. PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
  324. PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
  325. gma_enable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_ENABLE);
  326. spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
  327. return 0;
  328. }
  329. void gma_crtc_disable_vblank(struct drm_crtc *crtc)
  330. {
  331. struct drm_device *dev = crtc->dev;
  332. unsigned int pipe = crtc->index;
  333. struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
  334. unsigned long irqflags;
  335. spin_lock_irqsave(&dev_priv->irqmask_lock, irqflags);
  336. if (pipe == 0)
  337. dev_priv->vdc_irq_mask &= ~_PSB_VSYNC_PIPEA_FLAG;
  338. else if (pipe == 1)
  339. dev_priv->vdc_irq_mask &= ~_PSB_VSYNC_PIPEB_FLAG;
  340. PSB_WVDC32(~dev_priv->vdc_irq_mask, PSB_INT_MASK_R);
  341. PSB_WVDC32(dev_priv->vdc_irq_mask, PSB_INT_ENABLE_R);
  342. gma_disable_pipestat(dev_priv, pipe, PIPE_VBLANK_INTERRUPT_ENABLE);
  343. spin_unlock_irqrestore(&dev_priv->irqmask_lock, irqflags);
  344. }
  345. /* Called from drm generic code, passed a 'crtc', which
  346. * we use as a pipe index
  347. */
  348. u32 gma_crtc_get_vblank_counter(struct drm_crtc *crtc)
  349. {
  350. struct drm_device *dev = crtc->dev;
  351. unsigned int pipe = crtc->index;
  352. uint32_t high_frame = PIPEAFRAMEHIGH;
  353. uint32_t low_frame = PIPEAFRAMEPIXEL;
  354. uint32_t pipeconf_reg = PIPEACONF;
  355. uint32_t reg_val = 0;
  356. uint32_t high1 = 0, high2 = 0, low = 0, count = 0;
  357. switch (pipe) {
  358. case 0:
  359. break;
  360. case 1:
  361. high_frame = PIPEBFRAMEHIGH;
  362. low_frame = PIPEBFRAMEPIXEL;
  363. pipeconf_reg = PIPEBCONF;
  364. break;
  365. case 2:
  366. high_frame = PIPECFRAMEHIGH;
  367. low_frame = PIPECFRAMEPIXEL;
  368. pipeconf_reg = PIPECCONF;
  369. break;
  370. default:
  371. dev_err(dev->dev, "%s, invalid pipe.\n", __func__);
  372. return 0;
  373. }
  374. if (!gma_power_begin(dev, false))
  375. return 0;
  376. reg_val = REG_READ(pipeconf_reg);
  377. if (!(reg_val & PIPEACONF_ENABLE)) {
  378. dev_err(dev->dev, "trying to get vblank count for disabled pipe %u\n",
  379. pipe);
  380. goto err_gma_power_end;
  381. }
  382. /*
  383. * High & low register fields aren't synchronized, so make sure
  384. * we get a low value that's stable across two reads of the high
  385. * register.
  386. */
  387. do {
  388. high1 = ((REG_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
  389. PIPE_FRAME_HIGH_SHIFT);
  390. low = ((REG_READ(low_frame) & PIPE_FRAME_LOW_MASK) >>
  391. PIPE_FRAME_LOW_SHIFT);
  392. high2 = ((REG_READ(high_frame) & PIPE_FRAME_HIGH_MASK) >>
  393. PIPE_FRAME_HIGH_SHIFT);
  394. } while (high1 != high2);
  395. count = (high1 << 8) | low;
  396. err_gma_power_end:
  397. gma_power_end(dev);
  398. return count;
  399. }