display.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. static u8 dpcd_fix_data[DPCD_HEADER_SIZE] = {
  152. 0x11, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  153. };
  154. static void emulate_monitor_status_change(struct intel_vgpu *vgpu)
  155. {
  156. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  157. vgpu_vreg(vgpu, SDEISR) &= ~(SDE_PORTB_HOTPLUG_CPT |
  158. SDE_PORTC_HOTPLUG_CPT |
  159. SDE_PORTD_HOTPLUG_CPT);
  160. if (IS_SKYLAKE(dev_priv))
  161. vgpu_vreg(vgpu, SDEISR) &= ~(SDE_PORTA_HOTPLUG_SPT |
  162. SDE_PORTE_HOTPLUG_SPT);
  163. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_B)) {
  164. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTB_HOTPLUG_CPT;
  165. vgpu_vreg(vgpu, SFUSE_STRAP) |= SFUSE_STRAP_DDIB_DETECTED;
  166. }
  167. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_C)) {
  168. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTC_HOTPLUG_CPT;
  169. vgpu_vreg(vgpu, SFUSE_STRAP) |= SFUSE_STRAP_DDIC_DETECTED;
  170. }
  171. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_D)) {
  172. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTD_HOTPLUG_CPT;
  173. vgpu_vreg(vgpu, SFUSE_STRAP) |= SFUSE_STRAP_DDID_DETECTED;
  174. }
  175. if (IS_SKYLAKE(dev_priv) &&
  176. intel_vgpu_has_monitor_on_port(vgpu, PORT_E)) {
  177. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTE_HOTPLUG_SPT;
  178. }
  179. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_A)) {
  180. if (IS_BROADWELL(dev_priv))
  181. vgpu_vreg(vgpu, GEN8_DE_PORT_ISR) |=
  182. GEN8_PORT_DP_A_HOTPLUG;
  183. else
  184. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTA_HOTPLUG_SPT;
  185. vgpu_vreg(vgpu, DDI_BUF_CTL(PORT_A)) |= DDI_INIT_DISPLAY_DETECTED;
  186. }
  187. }
  188. static void clean_virtual_dp_monitor(struct intel_vgpu *vgpu, int port_num)
  189. {
  190. struct intel_vgpu_port *port = intel_vgpu_port(vgpu, port_num);
  191. kfree(port->edid);
  192. port->edid = NULL;
  193. kfree(port->dpcd);
  194. port->dpcd = NULL;
  195. }
  196. static int setup_virtual_dp_monitor(struct intel_vgpu *vgpu, int port_num,
  197. int type, unsigned int resolution)
  198. {
  199. struct intel_vgpu_port *port = intel_vgpu_port(vgpu, port_num);
  200. if (WARN_ON(resolution >= GVT_EDID_NUM))
  201. return -EINVAL;
  202. port->edid = kzalloc(sizeof(*(port->edid)), GFP_KERNEL);
  203. if (!port->edid)
  204. return -ENOMEM;
  205. port->dpcd = kzalloc(sizeof(*(port->dpcd)), GFP_KERNEL);
  206. if (!port->dpcd) {
  207. kfree(port->edid);
  208. return -ENOMEM;
  209. }
  210. memcpy(port->edid->edid_block, virtual_dp_monitor_edid[resolution],
  211. EDID_SIZE);
  212. port->edid->data_valid = true;
  213. memcpy(port->dpcd->data, dpcd_fix_data, DPCD_HEADER_SIZE);
  214. port->dpcd->data_valid = true;
  215. port->dpcd->data[DPCD_SINK_COUNT] = 0x1;
  216. port->type = type;
  217. emulate_monitor_status_change(vgpu);
  218. return 0;
  219. }
  220. /**
  221. * intel_gvt_check_vblank_emulation - check if vblank emulation timer should
  222. * be turned on/off when a virtual pipe is enabled/disabled.
  223. * @gvt: a GVT device
  224. *
  225. * This function is used to turn on/off vblank timer according to currently
  226. * enabled/disabled virtual pipes.
  227. *
  228. */
  229. void intel_gvt_check_vblank_emulation(struct intel_gvt *gvt)
  230. {
  231. struct intel_gvt_irq *irq = &gvt->irq;
  232. struct intel_vgpu *vgpu;
  233. bool have_enabled_pipe = false;
  234. int pipe, id;
  235. if (WARN_ON(!mutex_is_locked(&gvt->lock)))
  236. return;
  237. hrtimer_cancel(&irq->vblank_timer.timer);
  238. for_each_active_vgpu(gvt, vgpu, id) {
  239. for (pipe = 0; pipe < I915_MAX_PIPES; pipe++) {
  240. have_enabled_pipe =
  241. pipe_is_enabled(vgpu, pipe);
  242. if (have_enabled_pipe)
  243. break;
  244. }
  245. }
  246. if (have_enabled_pipe)
  247. hrtimer_start(&irq->vblank_timer.timer,
  248. ktime_add_ns(ktime_get(), irq->vblank_timer.period),
  249. HRTIMER_MODE_ABS);
  250. }
  251. static void emulate_vblank_on_pipe(struct intel_vgpu *vgpu, int pipe)
  252. {
  253. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  254. struct intel_vgpu_irq *irq = &vgpu->irq;
  255. int vblank_event[] = {
  256. [PIPE_A] = PIPE_A_VBLANK,
  257. [PIPE_B] = PIPE_B_VBLANK,
  258. [PIPE_C] = PIPE_C_VBLANK,
  259. };
  260. int event;
  261. if (pipe < PIPE_A || pipe > PIPE_C)
  262. return;
  263. for_each_set_bit(event, irq->flip_done_event[pipe],
  264. INTEL_GVT_EVENT_MAX) {
  265. clear_bit(event, irq->flip_done_event[pipe]);
  266. if (!pipe_is_enabled(vgpu, pipe))
  267. continue;
  268. vgpu_vreg(vgpu, PIPE_FLIPCOUNT_G4X(pipe))++;
  269. intel_vgpu_trigger_virtual_event(vgpu, event);
  270. }
  271. if (pipe_is_enabled(vgpu, pipe)) {
  272. vgpu_vreg(vgpu, PIPE_FRMCOUNT_G4X(pipe))++;
  273. intel_vgpu_trigger_virtual_event(vgpu, vblank_event[pipe]);
  274. }
  275. }
  276. static void emulate_vblank(struct intel_vgpu *vgpu)
  277. {
  278. int pipe;
  279. for_each_pipe(vgpu->gvt->dev_priv, pipe)
  280. emulate_vblank_on_pipe(vgpu, pipe);
  281. }
  282. /**
  283. * intel_gvt_emulate_vblank - trigger vblank events for vGPUs on GVT device
  284. * @gvt: a GVT device
  285. *
  286. * This function is used to trigger vblank interrupts for vGPUs on GVT device
  287. *
  288. */
  289. void intel_gvt_emulate_vblank(struct intel_gvt *gvt)
  290. {
  291. struct intel_vgpu *vgpu;
  292. int id;
  293. if (WARN_ON(!mutex_is_locked(&gvt->lock)))
  294. return;
  295. for_each_active_vgpu(gvt, vgpu, id)
  296. emulate_vblank(vgpu);
  297. }
  298. /**
  299. * intel_vgpu_clean_display - clean vGPU virtual display emulation
  300. * @vgpu: a vGPU
  301. *
  302. * This function is used to clean vGPU virtual display emulation stuffs
  303. *
  304. */
  305. void intel_vgpu_clean_display(struct intel_vgpu *vgpu)
  306. {
  307. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  308. if (IS_SKYLAKE(dev_priv))
  309. clean_virtual_dp_monitor(vgpu, PORT_D);
  310. else
  311. clean_virtual_dp_monitor(vgpu, PORT_B);
  312. }
  313. /**
  314. * intel_vgpu_init_display- initialize vGPU virtual display emulation
  315. * @vgpu: a vGPU
  316. *
  317. * This function is used to initialize vGPU virtual display emulation stuffs
  318. *
  319. * Returns:
  320. * Zero on success, negative error code if failed.
  321. *
  322. */
  323. int intel_vgpu_init_display(struct intel_vgpu *vgpu, u64 resolution)
  324. {
  325. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  326. intel_vgpu_init_i2c_edid(vgpu);
  327. if (IS_SKYLAKE(dev_priv))
  328. return setup_virtual_dp_monitor(vgpu, PORT_D, GVT_DP_D,
  329. resolution);
  330. else
  331. return setup_virtual_dp_monitor(vgpu, PORT_B, GVT_DP_B,
  332. resolution);
  333. }
  334. /**
  335. * intel_vgpu_reset_display- reset vGPU virtual display emulation
  336. * @vgpu: a vGPU
  337. *
  338. * This function is used to reset vGPU virtual display emulation stuffs
  339. *
  340. */
  341. void intel_vgpu_reset_display(struct intel_vgpu *vgpu)
  342. {
  343. emulate_monitor_status_change(vgpu);
  344. }