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