psb_irq.c 13 KB

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