edid.c 14 KB

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