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