display.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Ke Yu
  25. * Zhiyuan Lv <zhiyuan.lv@intel.com>
  26. *
  27. * Contributors:
  28. * Terrence Xu <terrence.xu@intel.com>
  29. * Changbin Du <changbin.du@intel.com>
  30. * Bing Niu <bing.niu@intel.com>
  31. * Zhi Wang <zhi.a.wang@intel.com>
  32. *
  33. */
  34. #include "i915_drv.h"
  35. #include "gvt.h"
  36. static int get_edp_pipe(struct intel_vgpu *vgpu)
  37. {
  38. u32 data = vgpu_vreg(vgpu, _TRANS_DDI_FUNC_CTL_EDP);
  39. int pipe = -1;
  40. switch (data & TRANS_DDI_EDP_INPUT_MASK) {
  41. case TRANS_DDI_EDP_INPUT_A_ON:
  42. case TRANS_DDI_EDP_INPUT_A_ONOFF:
  43. pipe = PIPE_A;
  44. break;
  45. case TRANS_DDI_EDP_INPUT_B_ONOFF:
  46. pipe = PIPE_B;
  47. break;
  48. case TRANS_DDI_EDP_INPUT_C_ONOFF:
  49. pipe = PIPE_C;
  50. break;
  51. }
  52. return pipe;
  53. }
  54. static int edp_pipe_is_enabled(struct intel_vgpu *vgpu)
  55. {
  56. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  57. if (!(vgpu_vreg(vgpu, PIPECONF(_PIPE_EDP)) & PIPECONF_ENABLE))
  58. return 0;
  59. if (!(vgpu_vreg(vgpu, _TRANS_DDI_FUNC_CTL_EDP) & TRANS_DDI_FUNC_ENABLE))
  60. return 0;
  61. return 1;
  62. }
  63. static int pipe_is_enabled(struct intel_vgpu *vgpu, int pipe)
  64. {
  65. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  66. if (WARN_ON(pipe < PIPE_A || pipe >= I915_MAX_PIPES))
  67. return -EINVAL;
  68. if (vgpu_vreg(vgpu, PIPECONF(pipe)) & PIPECONF_ENABLE)
  69. return 1;
  70. if (edp_pipe_is_enabled(vgpu) &&
  71. get_edp_pipe(vgpu) == pipe)
  72. return 1;
  73. return 0;
  74. }
  75. static unsigned char virtual_dp_monitor_edid[GVT_EDID_NUM][EDID_SIZE] = {
  76. {
  77. /* EDID with 1024x768 as its resolution */
  78. /*Header*/
  79. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  80. /* Vendor & Product Identification */
  81. 0x22, 0xf0, 0x54, 0x29, 0x00, 0x00, 0x00, 0x00, 0x04, 0x17,
  82. /* Version & Revision */
  83. 0x01, 0x04,
  84. /* Basic Display Parameters & Features */
  85. 0xa5, 0x34, 0x20, 0x78, 0x23,
  86. /* Color Characteristics */
  87. 0xfc, 0x81, 0xa4, 0x55, 0x4d, 0x9d, 0x25, 0x12, 0x50, 0x54,
  88. /* Established Timings: maximum resolution is 1024x768 */
  89. 0x21, 0x08, 0x00,
  90. /* Standard Timings. All invalid */
  91. 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x40, 0x00, 0x80, 0x00, 0x00,
  92. 0x00, 0x40, 0x00, 0x00, 0x00, 0x01,
  93. /* 18 Byte Data Blocks 1: invalid */
  94. 0x00, 0x00, 0x80, 0xa0, 0x70, 0xb0,
  95. 0x23, 0x40, 0x30, 0x20, 0x36, 0x00, 0x06, 0x44, 0x21, 0x00, 0x00, 0x1a,
  96. /* 18 Byte Data Blocks 2: invalid */
  97. 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x3c, 0x18, 0x50, 0x11, 0x00, 0x0a,
  98. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  99. /* 18 Byte Data Blocks 3: invalid */
  100. 0x00, 0x00, 0x00, 0xfc, 0x00, 0x48,
  101. 0x50, 0x20, 0x5a, 0x52, 0x32, 0x34, 0x34, 0x30, 0x77, 0x0a, 0x20, 0x20,
  102. /* 18 Byte Data Blocks 4: invalid */
  103. 0x00, 0x00, 0x00, 0xff, 0x00, 0x43, 0x4e, 0x34, 0x33, 0x30, 0x34, 0x30,
  104. 0x44, 0x58, 0x51, 0x0a, 0x20, 0x20,
  105. /* Extension Block Count */
  106. 0x00,
  107. /* Checksum */
  108. 0xef,
  109. },
  110. {
  111. /* EDID with 1920x1200 as its resolution */
  112. /*Header*/
  113. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  114. /* Vendor & Product Identification */
  115. 0x22, 0xf0, 0x54, 0x29, 0x00, 0x00, 0x00, 0x00, 0x04, 0x17,
  116. /* Version & Revision */
  117. 0x01, 0x04,
  118. /* Basic Display Parameters & Features */
  119. 0xa5, 0x34, 0x20, 0x78, 0x23,
  120. /* Color Characteristics */
  121. 0xfc, 0x81, 0xa4, 0x55, 0x4d, 0x9d, 0x25, 0x12, 0x50, 0x54,
  122. /* Established Timings: maximum resolution is 1024x768 */
  123. 0x21, 0x08, 0x00,
  124. /*
  125. * Standard Timings.
  126. * below new resolutions can be supported:
  127. * 1920x1080, 1280x720, 1280x960, 1280x1024,
  128. * 1440x900, 1600x1200, 1680x1050
  129. */
  130. 0xd1, 0xc0, 0x81, 0xc0, 0x81, 0x40, 0x81, 0x80, 0x95, 0x00,
  131. 0xa9, 0x40, 0xb3, 0x00, 0x01, 0x01,
  132. /* 18 Byte Data Blocks 1: max resolution is 1920x1200 */
  133. 0x28, 0x3c, 0x80, 0xa0, 0x70, 0xb0,
  134. 0x23, 0x40, 0x30, 0x20, 0x36, 0x00, 0x06, 0x44, 0x21, 0x00, 0x00, 0x1a,
  135. /* 18 Byte Data Blocks 2: invalid */
  136. 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x3c, 0x18, 0x50, 0x11, 0x00, 0x0a,
  137. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  138. /* 18 Byte Data Blocks 3: invalid */
  139. 0x00, 0x00, 0x00, 0xfc, 0x00, 0x48,
  140. 0x50, 0x20, 0x5a, 0x52, 0x32, 0x34, 0x34, 0x30, 0x77, 0x0a, 0x20, 0x20,
  141. /* 18 Byte Data Blocks 4: invalid */
  142. 0x00, 0x00, 0x00, 0xff, 0x00, 0x43, 0x4e, 0x34, 0x33, 0x30, 0x34, 0x30,
  143. 0x44, 0x58, 0x51, 0x0a, 0x20, 0x20,
  144. /* Extension Block Count */
  145. 0x00,
  146. /* Checksum */
  147. 0x45,
  148. },
  149. };
  150. #define DPCD_HEADER_SIZE 0xb
  151. /* let the virtual display supports DP1.2 */
  152. static u8 dpcd_fix_data[DPCD_HEADER_SIZE] = {
  153. 0x12, 0x014, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  154. };
  155. static void emulate_monitor_status_change(struct intel_vgpu *vgpu)
  156. {
  157. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  158. vgpu_vreg(vgpu, SDEISR) &= ~(SDE_PORTB_HOTPLUG_CPT |
  159. SDE_PORTC_HOTPLUG_CPT |
  160. SDE_PORTD_HOTPLUG_CPT);
  161. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) {
  162. vgpu_vreg(vgpu, SDEISR) &= ~(SDE_PORTA_HOTPLUG_SPT |
  163. SDE_PORTE_HOTPLUG_SPT);
  164. vgpu_vreg(vgpu, SKL_FUSE_STATUS) |=
  165. SKL_FUSE_DOWNLOAD_STATUS |
  166. SKL_FUSE_PG0_DIST_STATUS |
  167. SKL_FUSE_PG1_DIST_STATUS |
  168. SKL_FUSE_PG2_DIST_STATUS;
  169. vgpu_vreg(vgpu, LCPLL1_CTL) |=
  170. LCPLL_PLL_ENABLE |
  171. LCPLL_PLL_LOCK;
  172. vgpu_vreg(vgpu, LCPLL2_CTL) |= LCPLL_PLL_ENABLE;
  173. }
  174. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_B)) {
  175. vgpu_vreg(vgpu, SFUSE_STRAP) |= SFUSE_STRAP_DDIB_DETECTED;
  176. vgpu_vreg(vgpu, TRANS_DDI_FUNC_CTL(TRANSCODER_A)) &=
  177. ~(TRANS_DDI_BPC_MASK | TRANS_DDI_MODE_SELECT_MASK |
  178. TRANS_DDI_PORT_MASK);
  179. vgpu_vreg(vgpu, TRANS_DDI_FUNC_CTL(TRANSCODER_A)) |=
  180. (TRANS_DDI_BPC_8 | TRANS_DDI_MODE_SELECT_DP_SST |
  181. (PORT_B << TRANS_DDI_PORT_SHIFT) |
  182. TRANS_DDI_FUNC_ENABLE);
  183. vgpu_vreg(vgpu, DDI_BUF_CTL(PORT_B)) |= DDI_BUF_CTL_ENABLE;
  184. vgpu_vreg(vgpu, DDI_BUF_CTL(PORT_B)) &= ~DDI_BUF_IS_IDLE;
  185. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTB_HOTPLUG_CPT;
  186. }
  187. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_C)) {
  188. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTC_HOTPLUG_CPT;
  189. vgpu_vreg(vgpu, TRANS_DDI_FUNC_CTL(TRANSCODER_A)) &=
  190. ~(TRANS_DDI_BPC_MASK | TRANS_DDI_MODE_SELECT_MASK |
  191. TRANS_DDI_PORT_MASK);
  192. vgpu_vreg(vgpu, TRANS_DDI_FUNC_CTL(TRANSCODER_A)) |=
  193. (TRANS_DDI_BPC_8 | TRANS_DDI_MODE_SELECT_DP_SST |
  194. (PORT_C << TRANS_DDI_PORT_SHIFT) |
  195. TRANS_DDI_FUNC_ENABLE);
  196. vgpu_vreg(vgpu, DDI_BUF_CTL(PORT_C)) |= DDI_BUF_CTL_ENABLE;
  197. vgpu_vreg(vgpu, DDI_BUF_CTL(PORT_C)) &= ~DDI_BUF_IS_IDLE;
  198. vgpu_vreg(vgpu, SFUSE_STRAP) |= SFUSE_STRAP_DDIC_DETECTED;
  199. }
  200. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_D)) {
  201. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTD_HOTPLUG_CPT;
  202. vgpu_vreg(vgpu, TRANS_DDI_FUNC_CTL(TRANSCODER_A)) &=
  203. ~(TRANS_DDI_BPC_MASK | TRANS_DDI_MODE_SELECT_MASK |
  204. TRANS_DDI_PORT_MASK);
  205. vgpu_vreg(vgpu, TRANS_DDI_FUNC_CTL(TRANSCODER_A)) |=
  206. (TRANS_DDI_BPC_8 | TRANS_DDI_MODE_SELECT_DP_SST |
  207. (PORT_D << TRANS_DDI_PORT_SHIFT) |
  208. TRANS_DDI_FUNC_ENABLE);
  209. vgpu_vreg(vgpu, DDI_BUF_CTL(PORT_D)) |= DDI_BUF_CTL_ENABLE;
  210. vgpu_vreg(vgpu, DDI_BUF_CTL(PORT_D)) &= ~DDI_BUF_IS_IDLE;
  211. vgpu_vreg(vgpu, SFUSE_STRAP) |= SFUSE_STRAP_DDID_DETECTED;
  212. }
  213. if ((IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv)) &&
  214. intel_vgpu_has_monitor_on_port(vgpu, PORT_E)) {
  215. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTE_HOTPLUG_SPT;
  216. }
  217. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_A)) {
  218. if (IS_BROADWELL(dev_priv))
  219. vgpu_vreg(vgpu, GEN8_DE_PORT_ISR) |=
  220. GEN8_PORT_DP_A_HOTPLUG;
  221. else
  222. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTA_HOTPLUG_SPT;
  223. vgpu_vreg(vgpu, DDI_BUF_CTL(PORT_A)) |= DDI_INIT_DISPLAY_DETECTED;
  224. }
  225. }
  226. static void clean_virtual_dp_monitor(struct intel_vgpu *vgpu, int port_num)
  227. {
  228. struct intel_vgpu_port *port = intel_vgpu_port(vgpu, port_num);
  229. kfree(port->edid);
  230. port->edid = NULL;
  231. kfree(port->dpcd);
  232. port->dpcd = NULL;
  233. }
  234. static int setup_virtual_dp_monitor(struct intel_vgpu *vgpu, int port_num,
  235. int type, unsigned int resolution)
  236. {
  237. struct intel_vgpu_port *port = intel_vgpu_port(vgpu, port_num);
  238. if (WARN_ON(resolution >= GVT_EDID_NUM))
  239. return -EINVAL;
  240. port->edid = kzalloc(sizeof(*(port->edid)), GFP_KERNEL);
  241. if (!port->edid)
  242. return -ENOMEM;
  243. port->dpcd = kzalloc(sizeof(*(port->dpcd)), GFP_KERNEL);
  244. if (!port->dpcd) {
  245. kfree(port->edid);
  246. return -ENOMEM;
  247. }
  248. memcpy(port->edid->edid_block, virtual_dp_monitor_edid[resolution],
  249. EDID_SIZE);
  250. port->edid->data_valid = true;
  251. memcpy(port->dpcd->data, dpcd_fix_data, DPCD_HEADER_SIZE);
  252. port->dpcd->data_valid = true;
  253. port->dpcd->data[DPCD_SINK_COUNT] = 0x1;
  254. port->type = type;
  255. emulate_monitor_status_change(vgpu);
  256. return 0;
  257. }
  258. /**
  259. * intel_gvt_check_vblank_emulation - check if vblank emulation timer should
  260. * be turned on/off when a virtual pipe is enabled/disabled.
  261. * @gvt: a GVT device
  262. *
  263. * This function is used to turn on/off vblank timer according to currently
  264. * enabled/disabled virtual pipes.
  265. *
  266. */
  267. void intel_gvt_check_vblank_emulation(struct intel_gvt *gvt)
  268. {
  269. struct intel_gvt_irq *irq = &gvt->irq;
  270. struct intel_vgpu *vgpu;
  271. bool have_enabled_pipe = false;
  272. int pipe, id;
  273. if (WARN_ON(!mutex_is_locked(&gvt->lock)))
  274. return;
  275. hrtimer_cancel(&irq->vblank_timer.timer);
  276. for_each_active_vgpu(gvt, vgpu, id) {
  277. for (pipe = 0; pipe < I915_MAX_PIPES; pipe++) {
  278. have_enabled_pipe =
  279. pipe_is_enabled(vgpu, pipe);
  280. if (have_enabled_pipe)
  281. break;
  282. }
  283. }
  284. if (have_enabled_pipe)
  285. hrtimer_start(&irq->vblank_timer.timer,
  286. ktime_add_ns(ktime_get(), irq->vblank_timer.period),
  287. HRTIMER_MODE_ABS);
  288. }
  289. static void emulate_vblank_on_pipe(struct intel_vgpu *vgpu, int pipe)
  290. {
  291. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  292. struct intel_vgpu_irq *irq = &vgpu->irq;
  293. int vblank_event[] = {
  294. [PIPE_A] = PIPE_A_VBLANK,
  295. [PIPE_B] = PIPE_B_VBLANK,
  296. [PIPE_C] = PIPE_C_VBLANK,
  297. };
  298. int event;
  299. if (pipe < PIPE_A || pipe > PIPE_C)
  300. return;
  301. for_each_set_bit(event, irq->flip_done_event[pipe],
  302. INTEL_GVT_EVENT_MAX) {
  303. clear_bit(event, irq->flip_done_event[pipe]);
  304. if (!pipe_is_enabled(vgpu, pipe))
  305. continue;
  306. vgpu_vreg(vgpu, PIPE_FLIPCOUNT_G4X(pipe))++;
  307. intel_vgpu_trigger_virtual_event(vgpu, event);
  308. }
  309. if (pipe_is_enabled(vgpu, pipe)) {
  310. vgpu_vreg(vgpu, PIPE_FRMCOUNT_G4X(pipe))++;
  311. intel_vgpu_trigger_virtual_event(vgpu, vblank_event[pipe]);
  312. }
  313. }
  314. static void emulate_vblank(struct intel_vgpu *vgpu)
  315. {
  316. int pipe;
  317. for_each_pipe(vgpu->gvt->dev_priv, pipe)
  318. emulate_vblank_on_pipe(vgpu, pipe);
  319. }
  320. /**
  321. * intel_gvt_emulate_vblank - trigger vblank events for vGPUs on GVT device
  322. * @gvt: a GVT device
  323. *
  324. * This function is used to trigger vblank interrupts for vGPUs on GVT device
  325. *
  326. */
  327. void intel_gvt_emulate_vblank(struct intel_gvt *gvt)
  328. {
  329. struct intel_vgpu *vgpu;
  330. int id;
  331. if (WARN_ON(!mutex_is_locked(&gvt->lock)))
  332. return;
  333. for_each_active_vgpu(gvt, vgpu, id)
  334. emulate_vblank(vgpu);
  335. }
  336. /**
  337. * intel_vgpu_clean_display - clean vGPU virtual display emulation
  338. * @vgpu: a vGPU
  339. *
  340. * This function is used to clean vGPU virtual display emulation stuffs
  341. *
  342. */
  343. void intel_vgpu_clean_display(struct intel_vgpu *vgpu)
  344. {
  345. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  346. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  347. clean_virtual_dp_monitor(vgpu, PORT_D);
  348. else
  349. clean_virtual_dp_monitor(vgpu, PORT_B);
  350. }
  351. /**
  352. * intel_vgpu_init_display- initialize vGPU virtual display emulation
  353. * @vgpu: a vGPU
  354. *
  355. * This function is used to initialize vGPU virtual display emulation stuffs
  356. *
  357. * Returns:
  358. * Zero on success, negative error code if failed.
  359. *
  360. */
  361. int intel_vgpu_init_display(struct intel_vgpu *vgpu, u64 resolution)
  362. {
  363. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  364. intel_vgpu_init_i2c_edid(vgpu);
  365. if (IS_SKYLAKE(dev_priv) || IS_KABYLAKE(dev_priv))
  366. return setup_virtual_dp_monitor(vgpu, PORT_D, GVT_DP_D,
  367. resolution);
  368. else
  369. return setup_virtual_dp_monitor(vgpu, PORT_B, GVT_DP_B,
  370. resolution);
  371. }
  372. /**
  373. * intel_vgpu_reset_display- reset vGPU virtual display emulation
  374. * @vgpu: a vGPU
  375. *
  376. * This function is used to reset vGPU virtual display emulation stuffs
  377. *
  378. */
  379. void intel_vgpu_reset_display(struct intel_vgpu *vgpu)
  380. {
  381. emulate_monitor_status_change(vgpu);
  382. }