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