edid.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. #define GMBUS1_TOTAL_BYTES_SHIFT 16
  37. #define GMBUS1_TOTAL_BYTES_MASK 0x1ff
  38. #define gmbus1_total_byte_count(v) (((v) >> \
  39. GMBUS1_TOTAL_BYTES_SHIFT) & GMBUS1_TOTAL_BYTES_MASK)
  40. #define gmbus1_slave_addr(v) (((v) & 0xff) >> 1)
  41. #define gmbus1_slave_index(v) (((v) >> 8) & 0xff)
  42. #define gmbus1_bus_cycle(v) (((v) >> 25) & 0x7)
  43. /* GMBUS0 bits definitions */
  44. #define _GMBUS_PIN_SEL_MASK (0x7)
  45. static unsigned char edid_get_byte(struct intel_vgpu *vgpu)
  46. {
  47. struct intel_vgpu_i2c_edid *edid = &vgpu->display.i2c_edid;
  48. unsigned char chr = 0;
  49. if (edid->state == I2C_NOT_SPECIFIED || !edid->slave_selected) {
  50. gvt_vgpu_err("Driver tries to read EDID without proper sequence!\n");
  51. return 0;
  52. }
  53. if (edid->current_edid_read >= EDID_SIZE) {
  54. gvt_vgpu_err("edid_get_byte() exceeds the size of EDID!\n");
  55. return 0;
  56. }
  57. if (!edid->edid_available) {
  58. gvt_vgpu_err("Reading EDID but EDID is not available!\n");
  59. return 0;
  60. }
  61. if (intel_vgpu_has_monitor_on_port(vgpu, edid->port)) {
  62. struct intel_vgpu_edid_data *edid_data =
  63. intel_vgpu_port(vgpu, edid->port)->edid;
  64. chr = edid_data->edid_block[edid->current_edid_read];
  65. edid->current_edid_read++;
  66. } else {
  67. gvt_vgpu_err("No EDID available during the reading?\n");
  68. }
  69. return chr;
  70. }
  71. static inline int bxt_get_port_from_gmbus0(u32 gmbus0)
  72. {
  73. int port_select = gmbus0 & _GMBUS_PIN_SEL_MASK;
  74. int port = -EINVAL;
  75. if (port_select == 1)
  76. port = PORT_B;
  77. else if (port_select == 2)
  78. port = PORT_C;
  79. else if (port_select == 3)
  80. port = PORT_D;
  81. return port;
  82. }
  83. static inline int get_port_from_gmbus0(u32 gmbus0)
  84. {
  85. int port_select = gmbus0 & _GMBUS_PIN_SEL_MASK;
  86. int port = -EINVAL;
  87. if (port_select == 2)
  88. port = PORT_E;
  89. else if (port_select == 4)
  90. port = PORT_C;
  91. else if (port_select == 5)
  92. port = PORT_B;
  93. else if (port_select == 6)
  94. port = PORT_D;
  95. return port;
  96. }
  97. static void reset_gmbus_controller(struct intel_vgpu *vgpu)
  98. {
  99. vgpu_vreg_t(vgpu, PCH_GMBUS2) = GMBUS_HW_RDY;
  100. if (!vgpu->display.i2c_edid.edid_available)
  101. vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_SATOER;
  102. vgpu->display.i2c_edid.gmbus.phase = GMBUS_IDLE_PHASE;
  103. }
  104. /* GMBUS0 */
  105. static int gmbus0_mmio_write(struct intel_vgpu *vgpu,
  106. unsigned int offset, void *p_data, unsigned int bytes)
  107. {
  108. struct drm_i915_private *dev_priv = vgpu->gvt->dev_priv;
  109. int port, pin_select;
  110. memcpy(&vgpu_vreg(vgpu, offset), p_data, bytes);
  111. pin_select = vgpu_vreg(vgpu, offset) & _GMBUS_PIN_SEL_MASK;
  112. intel_vgpu_init_i2c_edid(vgpu);
  113. if (pin_select == 0)
  114. return 0;
  115. if (IS_BROXTON(dev_priv))
  116. port = bxt_get_port_from_gmbus0(pin_select);
  117. else
  118. port = get_port_from_gmbus0(pin_select);
  119. if (WARN_ON(port < 0))
  120. return 0;
  121. vgpu->display.i2c_edid.state = I2C_GMBUS;
  122. vgpu->display.i2c_edid.gmbus.phase = GMBUS_IDLE_PHASE;
  123. vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_ACTIVE;
  124. vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_HW_RDY | GMBUS_HW_WAIT_PHASE;
  125. if (intel_vgpu_has_monitor_on_port(vgpu, port) &&
  126. !intel_vgpu_port_is_dp(vgpu, port)) {
  127. vgpu->display.i2c_edid.port = port;
  128. vgpu->display.i2c_edid.edid_available = true;
  129. vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_SATOER;
  130. } else
  131. vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_SATOER;
  132. return 0;
  133. }
  134. static int gmbus1_mmio_write(struct intel_vgpu *vgpu, unsigned int offset,
  135. void *p_data, unsigned int bytes)
  136. {
  137. struct intel_vgpu_i2c_edid *i2c_edid = &vgpu->display.i2c_edid;
  138. u32 slave_addr;
  139. u32 wvalue = *(u32 *)p_data;
  140. if (vgpu_vreg(vgpu, offset) & GMBUS_SW_CLR_INT) {
  141. if (!(wvalue & GMBUS_SW_CLR_INT)) {
  142. vgpu_vreg(vgpu, offset) &= ~GMBUS_SW_CLR_INT;
  143. reset_gmbus_controller(vgpu);
  144. }
  145. /*
  146. * TODO: "This bit is cleared to zero when an event
  147. * causes the HW_RDY bit transition to occur "
  148. */
  149. } else {
  150. /*
  151. * per bspec setting this bit can cause:
  152. * 1) INT status bit cleared
  153. * 2) HW_RDY bit asserted
  154. */
  155. if (wvalue & GMBUS_SW_CLR_INT) {
  156. vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_INT;
  157. vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_HW_RDY;
  158. }
  159. /* For virtualization, we suppose that HW is always ready,
  160. * so GMBUS_SW_RDY should always be cleared
  161. */
  162. if (wvalue & GMBUS_SW_RDY)
  163. wvalue &= ~GMBUS_SW_RDY;
  164. i2c_edid->gmbus.total_byte_count =
  165. gmbus1_total_byte_count(wvalue);
  166. slave_addr = gmbus1_slave_addr(wvalue);
  167. /* vgpu gmbus only support EDID */
  168. if (slave_addr == EDID_ADDR) {
  169. i2c_edid->slave_selected = true;
  170. } else if (slave_addr != 0) {
  171. gvt_dbg_dpy(
  172. "vgpu%d: unsupported gmbus slave addr(0x%x)\n"
  173. " gmbus operations will be ignored.\n",
  174. vgpu->id, slave_addr);
  175. }
  176. if (wvalue & GMBUS_CYCLE_INDEX)
  177. i2c_edid->current_edid_read =
  178. gmbus1_slave_index(wvalue);
  179. i2c_edid->gmbus.cycle_type = gmbus1_bus_cycle(wvalue);
  180. switch (gmbus1_bus_cycle(wvalue)) {
  181. case GMBUS_NOCYCLE:
  182. break;
  183. case GMBUS_STOP:
  184. /* From spec:
  185. * This can only cause a STOP to be generated
  186. * if a GMBUS cycle is generated, the GMBUS is
  187. * currently in a data/wait/idle phase, or it is in a
  188. * WAIT phase
  189. */
  190. if (gmbus1_bus_cycle(vgpu_vreg(vgpu, offset))
  191. != GMBUS_NOCYCLE) {
  192. intel_vgpu_init_i2c_edid(vgpu);
  193. /* After the 'stop' cycle, hw state would become
  194. * 'stop phase' and then 'idle phase' after a
  195. * few milliseconds. In emulation, we just set
  196. * it as 'idle phase' ('stop phase' is not
  197. * visible in gmbus interface)
  198. */
  199. i2c_edid->gmbus.phase = GMBUS_IDLE_PHASE;
  200. vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_ACTIVE;
  201. }
  202. break;
  203. case NIDX_NS_W:
  204. case IDX_NS_W:
  205. case NIDX_STOP:
  206. case IDX_STOP:
  207. /* From hw spec the GMBUS phase
  208. * transition like this:
  209. * START (-->INDEX) -->DATA
  210. */
  211. i2c_edid->gmbus.phase = GMBUS_DATA_PHASE;
  212. vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_ACTIVE;
  213. break;
  214. default:
  215. gvt_vgpu_err("Unknown/reserved GMBUS cycle detected!\n");
  216. break;
  217. }
  218. /*
  219. * From hw spec the WAIT state will be
  220. * cleared:
  221. * (1) in a new GMBUS cycle
  222. * (2) by generating a stop
  223. */
  224. vgpu_vreg(vgpu, offset) = wvalue;
  225. }
  226. return 0;
  227. }
  228. static int gmbus3_mmio_write(struct intel_vgpu *vgpu, unsigned int offset,
  229. void *p_data, unsigned int bytes)
  230. {
  231. WARN_ON(1);
  232. return 0;
  233. }
  234. static int gmbus3_mmio_read(struct intel_vgpu *vgpu, unsigned int offset,
  235. void *p_data, unsigned int bytes)
  236. {
  237. int i;
  238. unsigned char byte_data;
  239. struct intel_vgpu_i2c_edid *i2c_edid = &vgpu->display.i2c_edid;
  240. int byte_left = i2c_edid->gmbus.total_byte_count -
  241. i2c_edid->current_edid_read;
  242. int byte_count = byte_left;
  243. u32 reg_data = 0;
  244. /* Data can only be recevied if previous settings correct */
  245. if (vgpu_vreg_t(vgpu, PCH_GMBUS1) & GMBUS_SLAVE_READ) {
  246. if (byte_left <= 0) {
  247. memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
  248. return 0;
  249. }
  250. if (byte_count > 4)
  251. byte_count = 4;
  252. for (i = 0; i < byte_count; i++) {
  253. byte_data = edid_get_byte(vgpu);
  254. reg_data |= (byte_data << (i << 3));
  255. }
  256. memcpy(&vgpu_vreg(vgpu, offset), &reg_data, byte_count);
  257. memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
  258. if (byte_left <= 4) {
  259. switch (i2c_edid->gmbus.cycle_type) {
  260. case NIDX_STOP:
  261. case IDX_STOP:
  262. i2c_edid->gmbus.phase = GMBUS_IDLE_PHASE;
  263. break;
  264. case NIDX_NS_W:
  265. case IDX_NS_W:
  266. default:
  267. i2c_edid->gmbus.phase = GMBUS_WAIT_PHASE;
  268. break;
  269. }
  270. intel_vgpu_init_i2c_edid(vgpu);
  271. }
  272. /*
  273. * Read GMBUS3 during send operation,
  274. * return the latest written value
  275. */
  276. } else {
  277. memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
  278. gvt_vgpu_err("warning: gmbus3 read with nothing returned\n");
  279. }
  280. return 0;
  281. }
  282. static int gmbus2_mmio_read(struct intel_vgpu *vgpu, unsigned int offset,
  283. void *p_data, unsigned int bytes)
  284. {
  285. u32 value = vgpu_vreg(vgpu, offset);
  286. if (!(vgpu_vreg(vgpu, offset) & GMBUS_INUSE))
  287. vgpu_vreg(vgpu, offset) |= GMBUS_INUSE;
  288. memcpy(p_data, (void *)&value, bytes);
  289. return 0;
  290. }
  291. static int gmbus2_mmio_write(struct intel_vgpu *vgpu, unsigned int offset,
  292. void *p_data, unsigned int bytes)
  293. {
  294. u32 wvalue = *(u32 *)p_data;
  295. if (wvalue & GMBUS_INUSE)
  296. vgpu_vreg(vgpu, offset) &= ~GMBUS_INUSE;
  297. /* All other bits are read-only */
  298. return 0;
  299. }
  300. /**
  301. * intel_gvt_i2c_handle_gmbus_read - emulate gmbus register mmio read
  302. * @vgpu: a vGPU
  303. * @offset: reg offset
  304. * @p_data: data return buffer
  305. * @bytes: access data length
  306. *
  307. * This function is used to emulate gmbus register mmio read
  308. *
  309. * Returns:
  310. * Zero on success, negative error code if failed.
  311. *
  312. */
  313. int intel_gvt_i2c_handle_gmbus_read(struct intel_vgpu *vgpu,
  314. unsigned int offset, void *p_data, unsigned int bytes)
  315. {
  316. if (WARN_ON(bytes > 8 && (offset & (bytes - 1))))
  317. return -EINVAL;
  318. if (offset == i915_mmio_reg_offset(PCH_GMBUS2))
  319. return gmbus2_mmio_read(vgpu, offset, p_data, bytes);
  320. else if (offset == i915_mmio_reg_offset(PCH_GMBUS3))
  321. return gmbus3_mmio_read(vgpu, offset, p_data, bytes);
  322. memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
  323. return 0;
  324. }
  325. /**
  326. * intel_gvt_i2c_handle_gmbus_write - emulate gmbus register mmio write
  327. * @vgpu: a vGPU
  328. * @offset: reg offset
  329. * @p_data: data return buffer
  330. * @bytes: access data length
  331. *
  332. * This function is used to emulate gmbus register mmio write
  333. *
  334. * Returns:
  335. * Zero on success, negative error code if failed.
  336. *
  337. */
  338. int intel_gvt_i2c_handle_gmbus_write(struct intel_vgpu *vgpu,
  339. unsigned int offset, void *p_data, unsigned int bytes)
  340. {
  341. if (WARN_ON(bytes > 8 && (offset & (bytes - 1))))
  342. return -EINVAL;
  343. if (offset == i915_mmio_reg_offset(PCH_GMBUS0))
  344. return gmbus0_mmio_write(vgpu, offset, p_data, bytes);
  345. else if (offset == i915_mmio_reg_offset(PCH_GMBUS1))
  346. return gmbus1_mmio_write(vgpu, offset, p_data, bytes);
  347. else if (offset == i915_mmio_reg_offset(PCH_GMBUS2))
  348. return gmbus2_mmio_write(vgpu, offset, p_data, bytes);
  349. else if (offset == i915_mmio_reg_offset(PCH_GMBUS3))
  350. return gmbus3_mmio_write(vgpu, offset, p_data, bytes);
  351. memcpy(&vgpu_vreg(vgpu, offset), p_data, bytes);
  352. return 0;
  353. }
  354. enum {
  355. AUX_CH_CTL = 0,
  356. AUX_CH_DATA1,
  357. AUX_CH_DATA2,
  358. AUX_CH_DATA3,
  359. AUX_CH_DATA4,
  360. AUX_CH_DATA5
  361. };
  362. static inline int get_aux_ch_reg(unsigned int offset)
  363. {
  364. int reg;
  365. switch (offset & 0xff) {
  366. case 0x10:
  367. reg = AUX_CH_CTL;
  368. break;
  369. case 0x14:
  370. reg = AUX_CH_DATA1;
  371. break;
  372. case 0x18:
  373. reg = AUX_CH_DATA2;
  374. break;
  375. case 0x1c:
  376. reg = AUX_CH_DATA3;
  377. break;
  378. case 0x20:
  379. reg = AUX_CH_DATA4;
  380. break;
  381. case 0x24:
  382. reg = AUX_CH_DATA5;
  383. break;
  384. default:
  385. reg = -1;
  386. break;
  387. }
  388. return reg;
  389. }
  390. #define AUX_CTL_MSG_LENGTH(reg) \
  391. ((reg & DP_AUX_CH_CTL_MESSAGE_SIZE_MASK) >> \
  392. DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT)
  393. /**
  394. * intel_gvt_i2c_handle_aux_ch_write - emulate AUX channel register write
  395. * @vgpu: a vGPU
  396. * @port_idx: port index
  397. * @offset: reg offset
  398. * @p_data: write ptr
  399. *
  400. * This function is used to emulate AUX channel register write
  401. *
  402. */
  403. void intel_gvt_i2c_handle_aux_ch_write(struct intel_vgpu *vgpu,
  404. int port_idx,
  405. unsigned int offset,
  406. void *p_data)
  407. {
  408. struct intel_vgpu_i2c_edid *i2c_edid = &vgpu->display.i2c_edid;
  409. int msg_length, ret_msg_size;
  410. int msg, addr, ctrl, op;
  411. u32 value = *(u32 *)p_data;
  412. int aux_data_for_write = 0;
  413. int reg = get_aux_ch_reg(offset);
  414. if (reg != AUX_CH_CTL) {
  415. vgpu_vreg(vgpu, offset) = value;
  416. return;
  417. }
  418. msg_length = AUX_CTL_MSG_LENGTH(value);
  419. // check the msg in DATA register.
  420. msg = vgpu_vreg(vgpu, offset + 4);
  421. addr = (msg >> 8) & 0xffff;
  422. ctrl = (msg >> 24) & 0xff;
  423. op = ctrl >> 4;
  424. if (!(value & DP_AUX_CH_CTL_SEND_BUSY)) {
  425. /* The ctl write to clear some states */
  426. return;
  427. }
  428. /* Always set the wanted value for vms. */
  429. ret_msg_size = (((op & 0x1) == GVT_AUX_I2C_READ) ? 2 : 1);
  430. vgpu_vreg(vgpu, offset) =
  431. DP_AUX_CH_CTL_DONE |
  432. ((ret_msg_size << DP_AUX_CH_CTL_MESSAGE_SIZE_SHIFT) &
  433. DP_AUX_CH_CTL_MESSAGE_SIZE_MASK);
  434. if (msg_length == 3) {
  435. if (!(op & GVT_AUX_I2C_MOT)) {
  436. /* stop */
  437. intel_vgpu_init_i2c_edid(vgpu);
  438. } else {
  439. /* start or restart */
  440. i2c_edid->aux_ch.i2c_over_aux_ch = true;
  441. i2c_edid->aux_ch.aux_ch_mot = true;
  442. if (addr == 0) {
  443. /* reset the address */
  444. intel_vgpu_init_i2c_edid(vgpu);
  445. } else if (addr == EDID_ADDR) {
  446. i2c_edid->state = I2C_AUX_CH;
  447. i2c_edid->port = port_idx;
  448. i2c_edid->slave_selected = true;
  449. if (intel_vgpu_has_monitor_on_port(vgpu,
  450. port_idx) &&
  451. intel_vgpu_port_is_dp(vgpu, port_idx))
  452. i2c_edid->edid_available = true;
  453. }
  454. }
  455. } else if ((op & 0x1) == GVT_AUX_I2C_WRITE) {
  456. /* TODO
  457. * We only support EDID reading from I2C_over_AUX. And
  458. * we do not expect the index mode to be used. Right now
  459. * the WRITE operation is ignored. It is good enough to
  460. * support the gfx driver to do EDID access.
  461. */
  462. } else {
  463. if (WARN_ON((op & 0x1) != GVT_AUX_I2C_READ))
  464. return;
  465. if (WARN_ON(msg_length != 4))
  466. return;
  467. if (i2c_edid->edid_available && i2c_edid->slave_selected) {
  468. unsigned char val = edid_get_byte(vgpu);
  469. aux_data_for_write = (val << 16);
  470. } else
  471. aux_data_for_write = (0xff << 16);
  472. }
  473. /* write the return value in AUX_CH_DATA reg which includes:
  474. * ACK of I2C_WRITE
  475. * returned byte if it is READ
  476. */
  477. aux_data_for_write |= GVT_AUX_I2C_REPLY_ACK << 24;
  478. vgpu_vreg(vgpu, offset + 4) = aux_data_for_write;
  479. }
  480. /**
  481. * intel_vgpu_init_i2c_edid - initialize vGPU i2c edid emulation
  482. * @vgpu: a vGPU
  483. *
  484. * This function is used to initialize vGPU i2c edid emulation stuffs
  485. *
  486. */
  487. void intel_vgpu_init_i2c_edid(struct intel_vgpu *vgpu)
  488. {
  489. struct intel_vgpu_i2c_edid *edid = &vgpu->display.i2c_edid;
  490. edid->state = I2C_NOT_SPECIFIED;
  491. edid->port = -1;
  492. edid->slave_selected = false;
  493. edid->edid_available = false;
  494. edid->current_edid_read = 0;
  495. memset(&edid->gmbus, 0, sizeof(struct intel_vgpu_i2c_gmbus));
  496. edid->aux_ch.i2c_over_aux_ch = false;
  497. edid->aux_ch.aux_ch_mot = false;
  498. }