display.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. /* EDID with 1024x768 as its resolution */
  76. static unsigned char virtual_dp_monitor_edid[] = {
  77. /*Header*/
  78. 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
  79. /* Vendor & Product Identification */
  80. 0x22, 0xf0, 0x54, 0x29, 0x00, 0x00, 0x00, 0x00, 0x04, 0x17,
  81. /* Version & Revision */
  82. 0x01, 0x04,
  83. /* Basic Display Parameters & Features */
  84. 0xa5, 0x34, 0x20, 0x78, 0x23,
  85. /* Color Characteristics */
  86. 0xfc, 0x81, 0xa4, 0x55, 0x4d, 0x9d, 0x25, 0x12, 0x50, 0x54,
  87. /* Established Timings: maximum resolution is 1024x768 */
  88. 0x21, 0x08, 0x00,
  89. /* Standard Timings. All invalid */
  90. 0x00, 0xc0, 0x00, 0xc0, 0x00, 0x40, 0x00, 0x80, 0x00, 0x00,
  91. 0x00, 0x40, 0x00, 0x00, 0x00, 0x01,
  92. /* 18 Byte Data Blocks 1: invalid */
  93. 0x00, 0x00, 0x80, 0xa0, 0x70, 0xb0,
  94. 0x23, 0x40, 0x30, 0x20, 0x36, 0x00, 0x06, 0x44, 0x21, 0x00, 0x00, 0x1a,
  95. /* 18 Byte Data Blocks 2: invalid */
  96. 0x00, 0x00, 0x00, 0xfd, 0x00, 0x18, 0x3c, 0x18, 0x50, 0x11, 0x00, 0x0a,
  97. 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
  98. /* 18 Byte Data Blocks 3: invalid */
  99. 0x00, 0x00, 0x00, 0xfc, 0x00, 0x48,
  100. 0x50, 0x20, 0x5a, 0x52, 0x32, 0x34, 0x34, 0x30, 0x77, 0x0a, 0x20, 0x20,
  101. /* 18 Byte Data Blocks 4: invalid */
  102. 0x00, 0x00, 0x00, 0xff, 0x00, 0x43, 0x4e, 0x34, 0x33, 0x30, 0x34, 0x30,
  103. 0x44, 0x58, 0x51, 0x0a, 0x20, 0x20,
  104. /* Extension Block Count */
  105. 0x00,
  106. /* Checksum */
  107. 0xef,
  108. };
  109. #define DPCD_HEADER_SIZE 0xb
  110. static u8 dpcd_fix_data[DPCD_HEADER_SIZE] = {
  111. 0x11, 0x0a, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  112. };
  113. static void emulate_monitor_status_change(struct intel_vgpu *vgpu)
  114. {
  115. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  116. vgpu_vreg(vgpu, SDEISR) &= ~(SDE_PORTB_HOTPLUG_CPT |
  117. SDE_PORTC_HOTPLUG_CPT |
  118. SDE_PORTD_HOTPLUG_CPT);
  119. if (IS_SKYLAKE(dev_priv))
  120. vgpu_vreg(vgpu, SDEISR) &= ~(SDE_PORTA_HOTPLUG_SPT |
  121. SDE_PORTE_HOTPLUG_SPT);
  122. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_B))
  123. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTB_HOTPLUG_CPT;
  124. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_C))
  125. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTC_HOTPLUG_CPT;
  126. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_D))
  127. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTD_HOTPLUG_CPT;
  128. if (IS_SKYLAKE(dev_priv) &&
  129. intel_vgpu_has_monitor_on_port(vgpu, PORT_E)) {
  130. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTE_HOTPLUG_SPT;
  131. }
  132. if (intel_vgpu_has_monitor_on_port(vgpu, PORT_A)) {
  133. if (IS_BROADWELL(dev_priv))
  134. vgpu_vreg(vgpu, GEN8_DE_PORT_ISR) |=
  135. GEN8_PORT_DP_A_HOTPLUG;
  136. else
  137. vgpu_vreg(vgpu, SDEISR) |= SDE_PORTA_HOTPLUG_SPT;
  138. }
  139. }
  140. static void clean_virtual_dp_monitor(struct intel_vgpu *vgpu, int port_num)
  141. {
  142. struct intel_vgpu_port *port = intel_vgpu_port(vgpu, port_num);
  143. kfree(port->edid);
  144. port->edid = NULL;
  145. kfree(port->dpcd);
  146. port->dpcd = NULL;
  147. }
  148. static int setup_virtual_dp_monitor(struct intel_vgpu *vgpu, int port_num,
  149. int type)
  150. {
  151. struct intel_vgpu_port *port = intel_vgpu_port(vgpu, port_num);
  152. port->edid = kzalloc(sizeof(*(port->edid)), GFP_KERNEL);
  153. if (!port->edid)
  154. return -ENOMEM;
  155. port->dpcd = kzalloc(sizeof(*(port->dpcd)), GFP_KERNEL);
  156. if (!port->dpcd) {
  157. kfree(port->edid);
  158. return -ENOMEM;
  159. }
  160. memcpy(port->edid->edid_block, virtual_dp_monitor_edid,
  161. EDID_SIZE);
  162. port->edid->data_valid = true;
  163. memcpy(port->dpcd->data, dpcd_fix_data, DPCD_HEADER_SIZE);
  164. port->dpcd->data_valid = true;
  165. port->dpcd->data[DPCD_SINK_COUNT] = 0x1;
  166. port->type = type;
  167. emulate_monitor_status_change(vgpu);
  168. return 0;
  169. }
  170. /**
  171. * intel_gvt_check_vblank_emulation - check if vblank emulation timer should
  172. * be turned on/off when a virtual pipe is enabled/disabled.
  173. * @gvt: a GVT device
  174. *
  175. * This function is used to turn on/off vblank timer according to currently
  176. * enabled/disabled virtual pipes.
  177. *
  178. */
  179. void intel_gvt_check_vblank_emulation(struct intel_gvt *gvt)
  180. {
  181. struct intel_gvt_irq *irq = &gvt->irq;
  182. struct intel_vgpu *vgpu;
  183. bool have_enabled_pipe = false;
  184. int pipe, id;
  185. if (WARN_ON(!mutex_is_locked(&gvt->lock)))
  186. return;
  187. hrtimer_cancel(&irq->vblank_timer.timer);
  188. for_each_active_vgpu(gvt, vgpu, id) {
  189. for (pipe = 0; pipe < I915_MAX_PIPES; pipe++) {
  190. have_enabled_pipe =
  191. pipe_is_enabled(vgpu, pipe);
  192. if (have_enabled_pipe)
  193. break;
  194. }
  195. }
  196. if (have_enabled_pipe)
  197. hrtimer_start(&irq->vblank_timer.timer,
  198. ktime_add_ns(ktime_get(), irq->vblank_timer.period),
  199. HRTIMER_MODE_ABS);
  200. }
  201. static void emulate_vblank_on_pipe(struct intel_vgpu *vgpu, int pipe)
  202. {
  203. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  204. struct intel_vgpu_irq *irq = &vgpu->irq;
  205. int vblank_event[] = {
  206. [PIPE_A] = PIPE_A_VBLANK,
  207. [PIPE_B] = PIPE_B_VBLANK,
  208. [PIPE_C] = PIPE_C_VBLANK,
  209. };
  210. int event;
  211. if (pipe < PIPE_A || pipe > PIPE_C)
  212. return;
  213. for_each_set_bit(event, irq->flip_done_event[pipe],
  214. INTEL_GVT_EVENT_MAX) {
  215. clear_bit(event, irq->flip_done_event[pipe]);
  216. if (!pipe_is_enabled(vgpu, pipe))
  217. continue;
  218. vgpu_vreg(vgpu, PIPE_FLIPCOUNT_G4X(pipe))++;
  219. intel_vgpu_trigger_virtual_event(vgpu, event);
  220. }
  221. if (pipe_is_enabled(vgpu, pipe)) {
  222. vgpu_vreg(vgpu, PIPE_FRMCOUNT_G4X(pipe))++;
  223. intel_vgpu_trigger_virtual_event(vgpu, vblank_event[pipe]);
  224. }
  225. }
  226. static void emulate_vblank(struct intel_vgpu *vgpu)
  227. {
  228. int pipe;
  229. for_each_pipe(vgpu->gvt->dev_priv, pipe)
  230. emulate_vblank_on_pipe(vgpu, pipe);
  231. }
  232. /**
  233. * intel_gvt_emulate_vblank - trigger vblank events for vGPUs on GVT device
  234. * @gvt: a GVT device
  235. *
  236. * This function is used to trigger vblank interrupts for vGPUs on GVT device
  237. *
  238. */
  239. void intel_gvt_emulate_vblank(struct intel_gvt *gvt)
  240. {
  241. struct intel_vgpu *vgpu;
  242. int id;
  243. if (WARN_ON(!mutex_is_locked(&gvt->lock)))
  244. return;
  245. for_each_active_vgpu(gvt, vgpu, id)
  246. emulate_vblank(vgpu);
  247. }
  248. /**
  249. * intel_vgpu_clean_display - clean vGPU virtual display emulation
  250. * @vgpu: a vGPU
  251. *
  252. * This function is used to clean vGPU virtual display emulation stuffs
  253. *
  254. */
  255. void intel_vgpu_clean_display(struct intel_vgpu *vgpu)
  256. {
  257. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  258. if (IS_SKYLAKE(dev_priv))
  259. clean_virtual_dp_monitor(vgpu, PORT_D);
  260. else
  261. clean_virtual_dp_monitor(vgpu, PORT_B);
  262. }
  263. /**
  264. * intel_vgpu_init_display- initialize vGPU virtual display emulation
  265. * @vgpu: a vGPU
  266. *
  267. * This function is used to initialize vGPU virtual display emulation stuffs
  268. *
  269. * Returns:
  270. * Zero on success, negative error code if failed.
  271. *
  272. */
  273. int intel_vgpu_init_display(struct intel_vgpu *vgpu)
  274. {
  275. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  276. intel_vgpu_init_i2c_edid(vgpu);
  277. if (IS_SKYLAKE(dev_priv))
  278. return setup_virtual_dp_monitor(vgpu, PORT_D, GVT_DP_D);
  279. else
  280. return setup_virtual_dp_monitor(vgpu, PORT_B, GVT_DP_B);
  281. }