2 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
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:
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
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
25 * Zhiyuan Lv <zhiyuan.lv@intel.com>
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>
35 #include "display/intel_dp_aux_regs.h"
36 #include "display/intel_gmbus_regs.h"
41 #define GMBUS1_TOTAL_BYTES_SHIFT 16
42 #define GMBUS1_TOTAL_BYTES_MASK 0x1ff
43 #define gmbus1_total_byte_count(v) (((v) >> \
44 GMBUS1_TOTAL_BYTES_SHIFT) & GMBUS1_TOTAL_BYTES_MASK)
45 #define gmbus1_slave_addr(v) (((v) & 0xff) >> 1)
46 #define gmbus1_slave_index(v) (((v) >> 8) & 0xff)
47 #define gmbus1_bus_cycle(v) (((v) >> 25) & 0x7)
49 /* GMBUS0 bits definitions */
50 #define _GMBUS_PIN_SEL_MASK (0x7)
52 static unsigned char edid_get_byte(struct intel_vgpu *vgpu)
54 struct intel_vgpu_i2c_edid *edid = &vgpu->display.i2c_edid;
55 unsigned char chr = 0;
57 if (edid->state == I2C_NOT_SPECIFIED || !edid->slave_selected) {
58 gvt_vgpu_err("Driver tries to read EDID without proper sequence!\n");
61 if (edid->current_edid_read >= EDID_SIZE) {
62 gvt_vgpu_err("edid_get_byte() exceeds the size of EDID!\n");
66 if (!edid->edid_available) {
67 gvt_vgpu_err("Reading EDID but EDID is not available!\n");
71 if (intel_vgpu_has_monitor_on_port(vgpu, edid->port)) {
72 struct intel_vgpu_edid_data *edid_data =
73 intel_vgpu_port(vgpu, edid->port)->edid;
75 chr = edid_data->edid_block[edid->current_edid_read];
76 edid->current_edid_read++;
78 gvt_vgpu_err("No EDID available during the reading?\n");
83 static inline int cnp_get_port_from_gmbus0(u32 gmbus0)
85 int port_select = gmbus0 & _GMBUS_PIN_SEL_MASK;
88 if (port_select == GMBUS_PIN_1_BXT)
90 else if (port_select == GMBUS_PIN_2_BXT)
92 else if (port_select == GMBUS_PIN_3_BXT)
94 else if (port_select == GMBUS_PIN_4_CNP)
99 static inline int bxt_get_port_from_gmbus0(u32 gmbus0)
101 int port_select = gmbus0 & _GMBUS_PIN_SEL_MASK;
104 if (port_select == GMBUS_PIN_1_BXT)
106 else if (port_select == GMBUS_PIN_2_BXT)
108 else if (port_select == GMBUS_PIN_3_BXT)
113 static inline int get_port_from_gmbus0(u32 gmbus0)
115 int port_select = gmbus0 & _GMBUS_PIN_SEL_MASK;
118 if (port_select == GMBUS_PIN_VGADDC)
120 else if (port_select == GMBUS_PIN_DPC)
122 else if (port_select == GMBUS_PIN_DPB)
124 else if (port_select == GMBUS_PIN_DPD)
129 static void reset_gmbus_controller(struct intel_vgpu *vgpu)
131 vgpu_vreg_t(vgpu, PCH_GMBUS2) = GMBUS_HW_RDY;
132 if (!vgpu->display.i2c_edid.edid_available)
133 vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_SATOER;
134 vgpu->display.i2c_edid.gmbus.phase = GMBUS_IDLE_PHASE;
138 static int gmbus0_mmio_write(struct intel_vgpu *vgpu,
139 unsigned int offset, void *p_data, unsigned int bytes)
141 struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
142 int port, pin_select;
144 memcpy(&vgpu_vreg(vgpu, offset), p_data, bytes);
146 pin_select = vgpu_vreg(vgpu, offset) & _GMBUS_PIN_SEL_MASK;
148 intel_vgpu_init_i2c_edid(vgpu);
153 if (IS_BROXTON(i915))
154 port = bxt_get_port_from_gmbus0(pin_select);
155 else if (IS_COFFEELAKE(i915) || IS_COMETLAKE(i915))
156 port = cnp_get_port_from_gmbus0(pin_select);
158 port = get_port_from_gmbus0(pin_select);
159 if (drm_WARN_ON(&i915->drm, port < 0))
162 vgpu->display.i2c_edid.state = I2C_GMBUS;
163 vgpu->display.i2c_edid.gmbus.phase = GMBUS_IDLE_PHASE;
165 vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_ACTIVE;
166 vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_HW_RDY | GMBUS_HW_WAIT_PHASE;
168 if (intel_vgpu_has_monitor_on_port(vgpu, port) &&
169 !intel_vgpu_port_is_dp(vgpu, port)) {
170 vgpu->display.i2c_edid.port = port;
171 vgpu->display.i2c_edid.edid_available = true;
172 vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_SATOER;
174 vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_SATOER;
178 static int gmbus1_mmio_write(struct intel_vgpu *vgpu, unsigned int offset,
179 void *p_data, unsigned int bytes)
181 struct intel_vgpu_i2c_edid *i2c_edid = &vgpu->display.i2c_edid;
183 u32 wvalue = *(u32 *)p_data;
185 if (vgpu_vreg(vgpu, offset) & GMBUS_SW_CLR_INT) {
186 if (!(wvalue & GMBUS_SW_CLR_INT)) {
187 vgpu_vreg(vgpu, offset) &= ~GMBUS_SW_CLR_INT;
188 reset_gmbus_controller(vgpu);
191 * TODO: "This bit is cleared to zero when an event
192 * causes the HW_RDY bit transition to occur "
196 * per bspec setting this bit can cause:
197 * 1) INT status bit cleared
198 * 2) HW_RDY bit asserted
200 if (wvalue & GMBUS_SW_CLR_INT) {
201 vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_INT;
202 vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_HW_RDY;
205 /* For virtualization, we suppose that HW is always ready,
206 * so GMBUS_SW_RDY should always be cleared
208 if (wvalue & GMBUS_SW_RDY)
209 wvalue &= ~GMBUS_SW_RDY;
211 i2c_edid->gmbus.total_byte_count =
212 gmbus1_total_byte_count(wvalue);
213 slave_addr = gmbus1_slave_addr(wvalue);
215 /* vgpu gmbus only support EDID */
216 if (slave_addr == EDID_ADDR) {
217 i2c_edid->slave_selected = true;
218 } else if (slave_addr != 0) {
220 "vgpu%d: unsupported gmbus slave addr(0x%x)\n"
221 " gmbus operations will be ignored.\n",
222 vgpu->id, slave_addr);
225 if (wvalue & GMBUS_CYCLE_INDEX)
226 i2c_edid->current_edid_read =
227 gmbus1_slave_index(wvalue);
229 i2c_edid->gmbus.cycle_type = gmbus1_bus_cycle(wvalue);
230 switch (gmbus1_bus_cycle(wvalue)) {
235 * This can only cause a STOP to be generated
236 * if a GMBUS cycle is generated, the GMBUS is
237 * currently in a data/wait/idle phase, or it is in a
240 if (gmbus1_bus_cycle(vgpu_vreg(vgpu, offset))
242 intel_vgpu_init_i2c_edid(vgpu);
243 /* After the 'stop' cycle, hw state would become
244 * 'stop phase' and then 'idle phase' after a
245 * few milliseconds. In emulation, we just set
246 * it as 'idle phase' ('stop phase' is not
247 * visible in gmbus interface)
249 i2c_edid->gmbus.phase = GMBUS_IDLE_PHASE;
250 vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_ACTIVE;
257 /* From hw spec the GMBUS phase
258 * transition like this:
259 * START (-->INDEX) -->DATA
261 i2c_edid->gmbus.phase = GMBUS_DATA_PHASE;
262 vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_ACTIVE;
265 gvt_vgpu_err("Unknown/reserved GMBUS cycle detected!\n");
269 * From hw spec the WAIT state will be
271 * (1) in a new GMBUS cycle
272 * (2) by generating a stop
274 vgpu_vreg(vgpu, offset) = wvalue;
279 static int gmbus3_mmio_write(struct intel_vgpu *vgpu, unsigned int offset,
280 void *p_data, unsigned int bytes)
282 struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
284 drm_WARN_ON(&i915->drm, 1);
288 static int gmbus3_mmio_read(struct intel_vgpu *vgpu, unsigned int offset,
289 void *p_data, unsigned int bytes)
292 unsigned char byte_data;
293 struct intel_vgpu_i2c_edid *i2c_edid = &vgpu->display.i2c_edid;
294 int byte_left = i2c_edid->gmbus.total_byte_count -
295 i2c_edid->current_edid_read;
296 int byte_count = byte_left;
299 /* Data can only be recevied if previous settings correct */
300 if (vgpu_vreg_t(vgpu, PCH_GMBUS1) & GMBUS_SLAVE_READ) {
301 if (byte_left <= 0) {
302 memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
308 for (i = 0; i < byte_count; i++) {
309 byte_data = edid_get_byte(vgpu);
310 reg_data |= (byte_data << (i << 3));
313 memcpy(&vgpu_vreg(vgpu, offset), ®_data, byte_count);
314 memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
316 if (byte_left <= 4) {
317 switch (i2c_edid->gmbus.cycle_type) {
320 i2c_edid->gmbus.phase = GMBUS_IDLE_PHASE;
325 i2c_edid->gmbus.phase = GMBUS_WAIT_PHASE;
328 intel_vgpu_init_i2c_edid(vgpu);
331 * Read GMBUS3 during send operation,
332 * return the latest written value
335 memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
336 gvt_vgpu_err("warning: gmbus3 read with nothing returned\n");
341 static int gmbus2_mmio_read(struct intel_vgpu *vgpu, unsigned int offset,
342 void *p_data, unsigned int bytes)
344 u32 value = vgpu_vreg(vgpu, offset);
346 if (!(vgpu_vreg(vgpu, offset) & GMBUS_INUSE))
347 vgpu_vreg(vgpu, offset) |= GMBUS_INUSE;
348 memcpy(p_data, (void *)&value, bytes);
352 static int gmbus2_mmio_write(struct intel_vgpu *vgpu, unsigned int offset,
353 void *p_data, unsigned int bytes)
355 u32 wvalue = *(u32 *)p_data;
357 if (wvalue & GMBUS_INUSE)
358 vgpu_vreg(vgpu, offset) &= ~GMBUS_INUSE;
359 /* All other bits are read-only */
364 * intel_gvt_i2c_handle_gmbus_read - emulate gmbus register mmio read
366 * @offset: reg offset
367 * @p_data: data return buffer
368 * @bytes: access data length
370 * This function is used to emulate gmbus register mmio read
373 * Zero on success, negative error code if failed.
376 int intel_gvt_i2c_handle_gmbus_read(struct intel_vgpu *vgpu,
377 unsigned int offset, void *p_data, unsigned int bytes)
379 struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
381 if (drm_WARN_ON(&i915->drm, bytes > 8 && (offset & (bytes - 1))))
384 if (offset == i915_mmio_reg_offset(PCH_GMBUS2))
385 return gmbus2_mmio_read(vgpu, offset, p_data, bytes);
386 else if (offset == i915_mmio_reg_offset(PCH_GMBUS3))
387 return gmbus3_mmio_read(vgpu, offset, p_data, bytes);
389 memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
394 * intel_gvt_i2c_handle_gmbus_write - emulate gmbus register mmio write
396 * @offset: reg offset
397 * @p_data: data return buffer
398 * @bytes: access data length
400 * This function is used to emulate gmbus register mmio write
403 * Zero on success, negative error code if failed.
406 int intel_gvt_i2c_handle_gmbus_write(struct intel_vgpu *vgpu,
407 unsigned int offset, void *p_data, unsigned int bytes)
409 struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
411 if (drm_WARN_ON(&i915->drm, bytes > 8 && (offset & (bytes - 1))))
414 if (offset == i915_mmio_reg_offset(PCH_GMBUS0))
415 return gmbus0_mmio_write(vgpu, offset, p_data, bytes);
416 else if (offset == i915_mmio_reg_offset(PCH_GMBUS1))
417 return gmbus1_mmio_write(vgpu, offset, p_data, bytes);
418 else if (offset == i915_mmio_reg_offset(PCH_GMBUS2))
419 return gmbus2_mmio_write(vgpu, offset, p_data, bytes);
420 else if (offset == i915_mmio_reg_offset(PCH_GMBUS3))
421 return gmbus3_mmio_write(vgpu, offset, p_data, bytes);
423 memcpy(&vgpu_vreg(vgpu, offset), p_data, bytes);
436 static inline int get_aux_ch_reg(unsigned int offset)
440 switch (offset & 0xff) {
467 * intel_gvt_i2c_handle_aux_ch_write - emulate AUX channel register write
469 * @port_idx: port index
470 * @offset: reg offset
473 * This function is used to emulate AUX channel register write
476 void intel_gvt_i2c_handle_aux_ch_write(struct intel_vgpu *vgpu,
481 struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
482 struct intel_vgpu_i2c_edid *i2c_edid = &vgpu->display.i2c_edid;
483 int msg_length, ret_msg_size;
484 int msg, addr, ctrl, op;
485 u32 value = *(u32 *)p_data;
486 int aux_data_for_write = 0;
487 int reg = get_aux_ch_reg(offset);
489 if (reg != AUX_CH_CTL) {
490 vgpu_vreg(vgpu, offset) = value;
494 msg_length = REG_FIELD_GET(DP_AUX_CH_CTL_MESSAGE_SIZE_MASK, reg);
496 // check the msg in DATA register.
497 msg = vgpu_vreg(vgpu, offset + 4);
498 addr = (msg >> 8) & 0xffff;
499 ctrl = (msg >> 24) & 0xff;
501 if (!(value & DP_AUX_CH_CTL_SEND_BUSY)) {
502 /* The ctl write to clear some states */
506 /* Always set the wanted value for vms. */
507 ret_msg_size = (((op & 0x1) == GVT_AUX_I2C_READ) ? 2 : 1);
508 vgpu_vreg(vgpu, offset) =
510 DP_AUX_CH_CTL_MESSAGE_SIZE(ret_msg_size);
512 if (msg_length == 3) {
513 if (!(op & GVT_AUX_I2C_MOT)) {
515 intel_vgpu_init_i2c_edid(vgpu);
517 /* start or restart */
518 i2c_edid->aux_ch.i2c_over_aux_ch = true;
519 i2c_edid->aux_ch.aux_ch_mot = true;
521 /* reset the address */
522 intel_vgpu_init_i2c_edid(vgpu);
523 } else if (addr == EDID_ADDR) {
524 i2c_edid->state = I2C_AUX_CH;
525 i2c_edid->port = port_idx;
526 i2c_edid->slave_selected = true;
527 if (intel_vgpu_has_monitor_on_port(vgpu,
529 intel_vgpu_port_is_dp(vgpu, port_idx))
530 i2c_edid->edid_available = true;
533 } else if ((op & 0x1) == GVT_AUX_I2C_WRITE) {
535 * We only support EDID reading from I2C_over_AUX. And
536 * we do not expect the index mode to be used. Right now
537 * the WRITE operation is ignored. It is good enough to
538 * support the gfx driver to do EDID access.
541 if (drm_WARN_ON(&i915->drm, (op & 0x1) != GVT_AUX_I2C_READ))
543 if (drm_WARN_ON(&i915->drm, msg_length != 4))
545 if (i2c_edid->edid_available && i2c_edid->slave_selected) {
546 unsigned char val = edid_get_byte(vgpu);
548 aux_data_for_write = (val << 16);
550 aux_data_for_write = (0xff << 16);
552 /* write the return value in AUX_CH_DATA reg which includes:
554 * returned byte if it is READ
556 aux_data_for_write |= GVT_AUX_I2C_REPLY_ACK << 24;
557 vgpu_vreg(vgpu, offset + 4) = aux_data_for_write;
561 * intel_vgpu_init_i2c_edid - initialize vGPU i2c edid emulation
564 * This function is used to initialize vGPU i2c edid emulation stuffs
567 void intel_vgpu_init_i2c_edid(struct intel_vgpu *vgpu)
569 struct intel_vgpu_i2c_edid *edid = &vgpu->display.i2c_edid;
571 edid->state = I2C_NOT_SPECIFIED;
574 edid->slave_selected = false;
575 edid->edid_available = false;
576 edid->current_edid_read = 0;
578 memset(&edid->gmbus, 0, sizeof(struct intel_vgpu_i2c_gmbus));
580 edid->aux_ch.i2c_over_aux_ch = false;
581 edid->aux_ch.aux_ch_mot = false;