xref: /openbmc/linux/drivers/gpu/drm/i915/gvt/edid.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
104d348aeSZhi Wang /*
204d348aeSZhi Wang  * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
304d348aeSZhi Wang  *
404d348aeSZhi Wang  * Permission is hereby granted, free of charge, to any person obtaining a
504d348aeSZhi Wang  * copy of this software and associated documentation files (the "Software"),
604d348aeSZhi Wang  * to deal in the Software without restriction, including without limitation
704d348aeSZhi Wang  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
804d348aeSZhi Wang  * and/or sell copies of the Software, and to permit persons to whom the
904d348aeSZhi Wang  * Software is furnished to do so, subject to the following conditions:
1004d348aeSZhi Wang  *
1104d348aeSZhi Wang  * The above copyright notice and this permission notice (including the next
1204d348aeSZhi Wang  * paragraph) shall be included in all copies or substantial portions of the
1304d348aeSZhi Wang  * Software.
1404d348aeSZhi Wang  *
1504d348aeSZhi Wang  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1604d348aeSZhi Wang  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1704d348aeSZhi Wang  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1804d348aeSZhi Wang  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1904d348aeSZhi Wang  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2004d348aeSZhi Wang  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2104d348aeSZhi Wang  * SOFTWARE.
2204d348aeSZhi Wang  *
2304d348aeSZhi Wang  * Authors:
2404d348aeSZhi Wang  *    Ke Yu
2504d348aeSZhi Wang  *    Zhiyuan Lv <zhiyuan.lv@intel.com>
2604d348aeSZhi Wang  *
2704d348aeSZhi Wang  * Contributors:
2804d348aeSZhi Wang  *    Terrence Xu <terrence.xu@intel.com>
2904d348aeSZhi Wang  *    Changbin Du <changbin.du@intel.com>
3004d348aeSZhi Wang  *    Bing Niu <bing.niu@intel.com>
3104d348aeSZhi Wang  *    Zhi Wang <zhi.a.wang@intel.com>
3204d348aeSZhi Wang  *
3304d348aeSZhi Wang  */
3404d348aeSZhi Wang 
3589e790ecSJani Nikula #include "display/intel_dp_aux_regs.h"
3651b072deSJani Nikula #include "display/intel_gmbus_regs.h"
3751b072deSJani Nikula #include "gvt.h"
3804d348aeSZhi Wang #include "i915_drv.h"
39ce2fce25SMatt Roper #include "i915_reg.h"
4004d348aeSZhi Wang 
4104d348aeSZhi Wang #define GMBUS1_TOTAL_BYTES_SHIFT 16
4204d348aeSZhi Wang #define GMBUS1_TOTAL_BYTES_MASK 0x1ff
4304d348aeSZhi Wang #define gmbus1_total_byte_count(v) (((v) >> \
4404d348aeSZhi Wang 	GMBUS1_TOTAL_BYTES_SHIFT) & GMBUS1_TOTAL_BYTES_MASK)
4504d348aeSZhi Wang #define gmbus1_slave_addr(v) (((v) & 0xff) >> 1)
4604d348aeSZhi Wang #define gmbus1_slave_index(v) (((v) >> 8) & 0xff)
4704d348aeSZhi Wang #define gmbus1_bus_cycle(v) (((v) >> 25) & 0x7)
4804d348aeSZhi Wang 
4904d348aeSZhi Wang /* GMBUS0 bits definitions */
5004d348aeSZhi Wang #define _GMBUS_PIN_SEL_MASK     (0x7)
5104d348aeSZhi Wang 
edid_get_byte(struct intel_vgpu * vgpu)5204d348aeSZhi Wang static unsigned char edid_get_byte(struct intel_vgpu *vgpu)
5304d348aeSZhi Wang {
5404d348aeSZhi Wang 	struct intel_vgpu_i2c_edid *edid = &vgpu->display.i2c_edid;
5504d348aeSZhi Wang 	unsigned char chr = 0;
5604d348aeSZhi Wang 
5704d348aeSZhi Wang 	if (edid->state == I2C_NOT_SPECIFIED || !edid->slave_selected) {
58695fbc08STina Zhang 		gvt_vgpu_err("Driver tries to read EDID without proper sequence!\n");
5904d348aeSZhi Wang 		return 0;
6004d348aeSZhi Wang 	}
6104d348aeSZhi Wang 	if (edid->current_edid_read >= EDID_SIZE) {
62695fbc08STina Zhang 		gvt_vgpu_err("edid_get_byte() exceeds the size of EDID!\n");
6304d348aeSZhi Wang 		return 0;
6404d348aeSZhi Wang 	}
6504d348aeSZhi Wang 
6604d348aeSZhi Wang 	if (!edid->edid_available) {
67695fbc08STina Zhang 		gvt_vgpu_err("Reading EDID but EDID is not available!\n");
6804d348aeSZhi Wang 		return 0;
6904d348aeSZhi Wang 	}
7004d348aeSZhi Wang 
7104d348aeSZhi Wang 	if (intel_vgpu_has_monitor_on_port(vgpu, edid->port)) {
7204d348aeSZhi Wang 		struct intel_vgpu_edid_data *edid_data =
7304d348aeSZhi Wang 			intel_vgpu_port(vgpu, edid->port)->edid;
7404d348aeSZhi Wang 
7504d348aeSZhi Wang 		chr = edid_data->edid_block[edid->current_edid_read];
7604d348aeSZhi Wang 		edid->current_edid_read++;
7704d348aeSZhi Wang 	} else {
78695fbc08STina Zhang 		gvt_vgpu_err("No EDID available during the reading?\n");
7904d348aeSZhi Wang 	}
8004d348aeSZhi Wang 	return chr;
8104d348aeSZhi Wang }
8204d348aeSZhi Wang 
cnp_get_port_from_gmbus0(u32 gmbus0)835807bb4dSfred gao static inline int cnp_get_port_from_gmbus0(u32 gmbus0)
845807bb4dSfred gao {
855807bb4dSfred gao 	int port_select = gmbus0 & _GMBUS_PIN_SEL_MASK;
865807bb4dSfred gao 	int port = -EINVAL;
875807bb4dSfred gao 
885807bb4dSfred gao 	if (port_select == GMBUS_PIN_1_BXT)
895807bb4dSfred gao 		port = PORT_B;
905807bb4dSfred gao 	else if (port_select == GMBUS_PIN_2_BXT)
915807bb4dSfred gao 		port = PORT_C;
925807bb4dSfred gao 	else if (port_select == GMBUS_PIN_3_BXT)
935807bb4dSfred gao 		port = PORT_D;
945807bb4dSfred gao 	else if (port_select == GMBUS_PIN_4_CNP)
955807bb4dSfred gao 		port = PORT_E;
965807bb4dSfred gao 	return port;
975807bb4dSfred gao }
985807bb4dSfred gao 
bxt_get_port_from_gmbus0(u32 gmbus0)9972bad997SColin Xu static inline int bxt_get_port_from_gmbus0(u32 gmbus0)
10072bad997SColin Xu {
10172bad997SColin Xu 	int port_select = gmbus0 & _GMBUS_PIN_SEL_MASK;
10272bad997SColin Xu 	int port = -EINVAL;
10372bad997SColin Xu 
104360f864eSfred gao 	if (port_select == GMBUS_PIN_1_BXT)
10572bad997SColin Xu 		port = PORT_B;
106360f864eSfred gao 	else if (port_select == GMBUS_PIN_2_BXT)
10772bad997SColin Xu 		port = PORT_C;
108360f864eSfred gao 	else if (port_select == GMBUS_PIN_3_BXT)
10972bad997SColin Xu 		port = PORT_D;
11072bad997SColin Xu 	return port;
11172bad997SColin Xu }
11272bad997SColin Xu 
get_port_from_gmbus0(u32 gmbus0)11304d348aeSZhi Wang static inline int get_port_from_gmbus0(u32 gmbus0)
11404d348aeSZhi Wang {
11504d348aeSZhi Wang 	int port_select = gmbus0 & _GMBUS_PIN_SEL_MASK;
11604d348aeSZhi Wang 	int port = -EINVAL;
11704d348aeSZhi Wang 
118360f864eSfred gao 	if (port_select == GMBUS_PIN_VGADDC)
11904d348aeSZhi Wang 		port = PORT_E;
120360f864eSfred gao 	else if (port_select == GMBUS_PIN_DPC)
12104d348aeSZhi Wang 		port = PORT_C;
122360f864eSfred gao 	else if (port_select == GMBUS_PIN_DPB)
12304d348aeSZhi Wang 		port = PORT_B;
124360f864eSfred gao 	else if (port_select == GMBUS_PIN_DPD)
12504d348aeSZhi Wang 		port = PORT_D;
12604d348aeSZhi Wang 	return port;
12704d348aeSZhi Wang }
12804d348aeSZhi Wang 
reset_gmbus_controller(struct intel_vgpu * vgpu)12904d348aeSZhi Wang static void reset_gmbus_controller(struct intel_vgpu *vgpu)
13004d348aeSZhi Wang {
13190551a12SZhenyu Wang 	vgpu_vreg_t(vgpu, PCH_GMBUS2) = GMBUS_HW_RDY;
13204d348aeSZhi Wang 	if (!vgpu->display.i2c_edid.edid_available)
13390551a12SZhenyu Wang 		vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_SATOER;
13404d348aeSZhi Wang 	vgpu->display.i2c_edid.gmbus.phase = GMBUS_IDLE_PHASE;
13504d348aeSZhi Wang }
13604d348aeSZhi Wang 
13704d348aeSZhi Wang /* GMBUS0 */
gmbus0_mmio_write(struct intel_vgpu * vgpu,unsigned int offset,void * p_data,unsigned int bytes)13804d348aeSZhi Wang static int gmbus0_mmio_write(struct intel_vgpu *vgpu,
13904d348aeSZhi Wang 			unsigned int offset, void *p_data, unsigned int bytes)
14004d348aeSZhi Wang {
141a61ac1e7SChris Wilson 	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
14204d348aeSZhi Wang 	int port, pin_select;
14304d348aeSZhi Wang 
14404d348aeSZhi Wang 	memcpy(&vgpu_vreg(vgpu, offset), p_data, bytes);
14504d348aeSZhi Wang 
14604d348aeSZhi Wang 	pin_select = vgpu_vreg(vgpu, offset) & _GMBUS_PIN_SEL_MASK;
14704d348aeSZhi Wang 
14804d348aeSZhi Wang 	intel_vgpu_init_i2c_edid(vgpu);
14904d348aeSZhi Wang 
15004d348aeSZhi Wang 	if (pin_select == 0)
15104d348aeSZhi Wang 		return 0;
15204d348aeSZhi Wang 
153a61ac1e7SChris Wilson 	if (IS_BROXTON(i915))
15472bad997SColin Xu 		port = bxt_get_port_from_gmbus0(pin_select);
1555f4ae270SChris Wilson 	else if (IS_COFFEELAKE(i915) || IS_COMETLAKE(i915))
1565807bb4dSfred gao 		port = cnp_get_port_from_gmbus0(pin_select);
15772bad997SColin Xu 	else
15804d348aeSZhi Wang 		port = get_port_from_gmbus0(pin_select);
159a61ac1e7SChris Wilson 	if (drm_WARN_ON(&i915->drm, port < 0))
16004d348aeSZhi Wang 		return 0;
16104d348aeSZhi Wang 
16204d348aeSZhi Wang 	vgpu->display.i2c_edid.state = I2C_GMBUS;
16304d348aeSZhi Wang 	vgpu->display.i2c_edid.gmbus.phase = GMBUS_IDLE_PHASE;
16404d348aeSZhi Wang 
16590551a12SZhenyu Wang 	vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_ACTIVE;
16690551a12SZhenyu Wang 	vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_HW_RDY | GMBUS_HW_WAIT_PHASE;
16704d348aeSZhi Wang 
16804d348aeSZhi Wang 	if (intel_vgpu_has_monitor_on_port(vgpu, port) &&
16904d348aeSZhi Wang 			!intel_vgpu_port_is_dp(vgpu, port)) {
17004d348aeSZhi Wang 		vgpu->display.i2c_edid.port = port;
17104d348aeSZhi Wang 		vgpu->display.i2c_edid.edid_available = true;
17290551a12SZhenyu Wang 		vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_SATOER;
17304d348aeSZhi Wang 	} else
17490551a12SZhenyu Wang 		vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_SATOER;
17504d348aeSZhi Wang 	return 0;
17604d348aeSZhi Wang }
17704d348aeSZhi Wang 
gmbus1_mmio_write(struct intel_vgpu * vgpu,unsigned int offset,void * p_data,unsigned int bytes)17804d348aeSZhi Wang static int gmbus1_mmio_write(struct intel_vgpu *vgpu, unsigned int offset,
17904d348aeSZhi Wang 		void *p_data, unsigned int bytes)
18004d348aeSZhi Wang {
18104d348aeSZhi Wang 	struct intel_vgpu_i2c_edid *i2c_edid = &vgpu->display.i2c_edid;
18204d348aeSZhi Wang 	u32 slave_addr;
18304d348aeSZhi Wang 	u32 wvalue = *(u32 *)p_data;
18404d348aeSZhi Wang 
18504d348aeSZhi Wang 	if (vgpu_vreg(vgpu, offset) & GMBUS_SW_CLR_INT) {
18604d348aeSZhi Wang 		if (!(wvalue & GMBUS_SW_CLR_INT)) {
18704d348aeSZhi Wang 			vgpu_vreg(vgpu, offset) &= ~GMBUS_SW_CLR_INT;
18804d348aeSZhi Wang 			reset_gmbus_controller(vgpu);
18904d348aeSZhi Wang 		}
19004d348aeSZhi Wang 		/*
19104d348aeSZhi Wang 		 * TODO: "This bit is cleared to zero when an event
19204d348aeSZhi Wang 		 * causes the HW_RDY bit transition to occur "
19304d348aeSZhi Wang 		 */
19404d348aeSZhi Wang 	} else {
19504d348aeSZhi Wang 		/*
19604d348aeSZhi Wang 		 * per bspec setting this bit can cause:
19704d348aeSZhi Wang 		 * 1) INT status bit cleared
19804d348aeSZhi Wang 		 * 2) HW_RDY bit asserted
19904d348aeSZhi Wang 		 */
20004d348aeSZhi Wang 		if (wvalue & GMBUS_SW_CLR_INT) {
20190551a12SZhenyu Wang 			vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_INT;
20290551a12SZhenyu Wang 			vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_HW_RDY;
20304d348aeSZhi Wang 		}
20404d348aeSZhi Wang 
20504d348aeSZhi Wang 		/* For virtualization, we suppose that HW is always ready,
20604d348aeSZhi Wang 		 * so GMBUS_SW_RDY should always be cleared
20704d348aeSZhi Wang 		 */
20804d348aeSZhi Wang 		if (wvalue & GMBUS_SW_RDY)
20904d348aeSZhi Wang 			wvalue &= ~GMBUS_SW_RDY;
21004d348aeSZhi Wang 
21104d348aeSZhi Wang 		i2c_edid->gmbus.total_byte_count =
21204d348aeSZhi Wang 			gmbus1_total_byte_count(wvalue);
21304d348aeSZhi Wang 		slave_addr = gmbus1_slave_addr(wvalue);
21404d348aeSZhi Wang 
21504d348aeSZhi Wang 		/* vgpu gmbus only support EDID */
21604d348aeSZhi Wang 		if (slave_addr == EDID_ADDR) {
21704d348aeSZhi Wang 			i2c_edid->slave_selected = true;
21804d348aeSZhi Wang 		} else if (slave_addr != 0) {
21904d348aeSZhi Wang 			gvt_dbg_dpy(
22004d348aeSZhi Wang 				"vgpu%d: unsupported gmbus slave addr(0x%x)\n"
22104d348aeSZhi Wang 				"	gmbus operations will be ignored.\n",
22204d348aeSZhi Wang 					vgpu->id, slave_addr);
22304d348aeSZhi Wang 		}
22404d348aeSZhi Wang 
22504d348aeSZhi Wang 		if (wvalue & GMBUS_CYCLE_INDEX)
22604d348aeSZhi Wang 			i2c_edid->current_edid_read =
22704d348aeSZhi Wang 				gmbus1_slave_index(wvalue);
22804d348aeSZhi Wang 
22904d348aeSZhi Wang 		i2c_edid->gmbus.cycle_type = gmbus1_bus_cycle(wvalue);
23004d348aeSZhi Wang 		switch (gmbus1_bus_cycle(wvalue)) {
23104d348aeSZhi Wang 		case GMBUS_NOCYCLE:
23204d348aeSZhi Wang 			break;
23304d348aeSZhi Wang 		case GMBUS_STOP:
23404d348aeSZhi Wang 			/* From spec:
23504d348aeSZhi Wang 			 * This can only cause a STOP to be generated
23604d348aeSZhi Wang 			 * if a GMBUS cycle is generated, the GMBUS is
23704d348aeSZhi Wang 			 * currently in a data/wait/idle phase, or it is in a
23804d348aeSZhi Wang 			 * WAIT phase
23904d348aeSZhi Wang 			 */
24004d348aeSZhi Wang 			if (gmbus1_bus_cycle(vgpu_vreg(vgpu, offset))
24104d348aeSZhi Wang 				!= GMBUS_NOCYCLE) {
24204d348aeSZhi Wang 				intel_vgpu_init_i2c_edid(vgpu);
24304d348aeSZhi Wang 				/* After the 'stop' cycle, hw state would become
24404d348aeSZhi Wang 				 * 'stop phase' and then 'idle phase' after a
24504d348aeSZhi Wang 				 * few milliseconds. In emulation, we just set
24604d348aeSZhi Wang 				 * it as 'idle phase' ('stop phase' is not
24704d348aeSZhi Wang 				 * visible in gmbus interface)
24804d348aeSZhi Wang 				 */
24904d348aeSZhi Wang 				i2c_edid->gmbus.phase = GMBUS_IDLE_PHASE;
25090551a12SZhenyu Wang 				vgpu_vreg_t(vgpu, PCH_GMBUS2) &= ~GMBUS_ACTIVE;
25104d348aeSZhi Wang 			}
25204d348aeSZhi Wang 			break;
25304d348aeSZhi Wang 		case NIDX_NS_W:
25404d348aeSZhi Wang 		case IDX_NS_W:
25504d348aeSZhi Wang 		case NIDX_STOP:
25604d348aeSZhi Wang 		case IDX_STOP:
25704d348aeSZhi Wang 			/* From hw spec the GMBUS phase
25804d348aeSZhi Wang 			 * transition like this:
25904d348aeSZhi Wang 			 * START (-->INDEX) -->DATA
26004d348aeSZhi Wang 			 */
26104d348aeSZhi Wang 			i2c_edid->gmbus.phase = GMBUS_DATA_PHASE;
26290551a12SZhenyu Wang 			vgpu_vreg_t(vgpu, PCH_GMBUS2) |= GMBUS_ACTIVE;
26304d348aeSZhi Wang 			break;
26404d348aeSZhi Wang 		default:
265695fbc08STina Zhang 			gvt_vgpu_err("Unknown/reserved GMBUS cycle detected!\n");
26604d348aeSZhi Wang 			break;
26704d348aeSZhi Wang 		}
26804d348aeSZhi Wang 		/*
26904d348aeSZhi Wang 		 * From hw spec the WAIT state will be
27004d348aeSZhi Wang 		 * cleared:
27104d348aeSZhi Wang 		 * (1) in a new GMBUS cycle
27204d348aeSZhi Wang 		 * (2) by generating a stop
27304d348aeSZhi Wang 		 */
27404d348aeSZhi Wang 		vgpu_vreg(vgpu, offset) = wvalue;
27504d348aeSZhi Wang 	}
27604d348aeSZhi Wang 	return 0;
27704d348aeSZhi Wang }
27804d348aeSZhi Wang 
gmbus3_mmio_write(struct intel_vgpu * vgpu,unsigned int offset,void * p_data,unsigned int bytes)27904d348aeSZhi Wang static int gmbus3_mmio_write(struct intel_vgpu *vgpu, unsigned int offset,
28004d348aeSZhi Wang 	void *p_data, unsigned int bytes)
28104d348aeSZhi Wang {
282a61ac1e7SChris Wilson 	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
28312d58619SPankaj Bharadiya 
28412d58619SPankaj Bharadiya 	drm_WARN_ON(&i915->drm, 1);
28504d348aeSZhi Wang 	return 0;
28604d348aeSZhi Wang }
28704d348aeSZhi Wang 
gmbus3_mmio_read(struct intel_vgpu * vgpu,unsigned int offset,void * p_data,unsigned int bytes)28804d348aeSZhi Wang static int gmbus3_mmio_read(struct intel_vgpu *vgpu, unsigned int offset,
28904d348aeSZhi Wang 		void *p_data, unsigned int bytes)
29004d348aeSZhi Wang {
29104d348aeSZhi Wang 	int i;
29204d348aeSZhi Wang 	unsigned char byte_data;
29304d348aeSZhi Wang 	struct intel_vgpu_i2c_edid *i2c_edid = &vgpu->display.i2c_edid;
29404d348aeSZhi Wang 	int byte_left = i2c_edid->gmbus.total_byte_count -
29504d348aeSZhi Wang 				i2c_edid->current_edid_read;
29604d348aeSZhi Wang 	int byte_count = byte_left;
29704d348aeSZhi Wang 	u32 reg_data = 0;
29804d348aeSZhi Wang 
29904d348aeSZhi Wang 	/* Data can only be recevied if previous settings correct */
30090551a12SZhenyu Wang 	if (vgpu_vreg_t(vgpu, PCH_GMBUS1) & GMBUS_SLAVE_READ) {
30104d348aeSZhi Wang 		if (byte_left <= 0) {
30204d348aeSZhi Wang 			memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
30304d348aeSZhi Wang 			return 0;
30404d348aeSZhi Wang 		}
30504d348aeSZhi Wang 
30604d348aeSZhi Wang 		if (byte_count > 4)
30704d348aeSZhi Wang 			byte_count = 4;
30804d348aeSZhi Wang 		for (i = 0; i < byte_count; i++) {
30904d348aeSZhi Wang 			byte_data = edid_get_byte(vgpu);
31004d348aeSZhi Wang 			reg_data |= (byte_data << (i << 3));
31104d348aeSZhi Wang 		}
31204d348aeSZhi Wang 
31304d348aeSZhi Wang 		memcpy(&vgpu_vreg(vgpu, offset), &reg_data, byte_count);
31404d348aeSZhi Wang 		memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
31504d348aeSZhi Wang 
31604d348aeSZhi Wang 		if (byte_left <= 4) {
31704d348aeSZhi Wang 			switch (i2c_edid->gmbus.cycle_type) {
31804d348aeSZhi Wang 			case NIDX_STOP:
31904d348aeSZhi Wang 			case IDX_STOP:
32004d348aeSZhi Wang 				i2c_edid->gmbus.phase = GMBUS_IDLE_PHASE;
32104d348aeSZhi Wang 				break;
32204d348aeSZhi Wang 			case NIDX_NS_W:
32304d348aeSZhi Wang 			case IDX_NS_W:
32404d348aeSZhi Wang 			default:
32504d348aeSZhi Wang 				i2c_edid->gmbus.phase = GMBUS_WAIT_PHASE;
32604d348aeSZhi Wang 				break;
32704d348aeSZhi Wang 			}
32804d348aeSZhi Wang 			intel_vgpu_init_i2c_edid(vgpu);
32904d348aeSZhi Wang 		}
33004d348aeSZhi Wang 		/*
33104d348aeSZhi Wang 		 * Read GMBUS3 during send operation,
33204d348aeSZhi Wang 		 * return the latest written value
33304d348aeSZhi Wang 		 */
33404d348aeSZhi Wang 	} else {
33504d348aeSZhi Wang 		memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
336695fbc08STina Zhang 		gvt_vgpu_err("warning: gmbus3 read with nothing returned\n");
33704d348aeSZhi Wang 	}
33804d348aeSZhi Wang 	return 0;
33904d348aeSZhi Wang }
34004d348aeSZhi Wang 
gmbus2_mmio_read(struct intel_vgpu * vgpu,unsigned int offset,void * p_data,unsigned int bytes)34104d348aeSZhi Wang static int gmbus2_mmio_read(struct intel_vgpu *vgpu, unsigned int offset,
34204d348aeSZhi Wang 		void *p_data, unsigned int bytes)
34304d348aeSZhi Wang {
34404d348aeSZhi Wang 	u32 value = vgpu_vreg(vgpu, offset);
34504d348aeSZhi Wang 
34604d348aeSZhi Wang 	if (!(vgpu_vreg(vgpu, offset) & GMBUS_INUSE))
34704d348aeSZhi Wang 		vgpu_vreg(vgpu, offset) |= GMBUS_INUSE;
34804d348aeSZhi Wang 	memcpy(p_data, (void *)&value, bytes);
34904d348aeSZhi Wang 	return 0;
35004d348aeSZhi Wang }
35104d348aeSZhi Wang 
gmbus2_mmio_write(struct intel_vgpu * vgpu,unsigned int offset,void * p_data,unsigned int bytes)35204d348aeSZhi Wang static int gmbus2_mmio_write(struct intel_vgpu *vgpu, unsigned int offset,
35304d348aeSZhi Wang 		void *p_data, unsigned int bytes)
35404d348aeSZhi Wang {
35504d348aeSZhi Wang 	u32 wvalue = *(u32 *)p_data;
35604d348aeSZhi Wang 
35704d348aeSZhi Wang 	if (wvalue & GMBUS_INUSE)
35804d348aeSZhi Wang 		vgpu_vreg(vgpu, offset) &= ~GMBUS_INUSE;
35904d348aeSZhi Wang 	/* All other bits are read-only */
36004d348aeSZhi Wang 	return 0;
36104d348aeSZhi Wang }
36204d348aeSZhi Wang 
36304d348aeSZhi Wang /**
36404d348aeSZhi Wang  * intel_gvt_i2c_handle_gmbus_read - emulate gmbus register mmio read
36504d348aeSZhi Wang  * @vgpu: a vGPU
366a752b070SZhenyu Wang  * @offset: reg offset
367a752b070SZhenyu Wang  * @p_data: data return buffer
368a752b070SZhenyu Wang  * @bytes: access data length
36904d348aeSZhi Wang  *
37004d348aeSZhi Wang  * This function is used to emulate gmbus register mmio read
37104d348aeSZhi Wang  *
37204d348aeSZhi Wang  * Returns:
37304d348aeSZhi Wang  * Zero on success, negative error code if failed.
37404d348aeSZhi Wang  *
37504d348aeSZhi Wang  */
intel_gvt_i2c_handle_gmbus_read(struct intel_vgpu * vgpu,unsigned int offset,void * p_data,unsigned int bytes)37604d348aeSZhi Wang int intel_gvt_i2c_handle_gmbus_read(struct intel_vgpu *vgpu,
37704d348aeSZhi Wang 	unsigned int offset, void *p_data, unsigned int bytes)
37804d348aeSZhi Wang {
379a61ac1e7SChris Wilson 	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
38012d58619SPankaj Bharadiya 
38112d58619SPankaj Bharadiya 	if (drm_WARN_ON(&i915->drm, bytes > 8 && (offset & (bytes - 1))))
38204d348aeSZhi Wang 		return -EINVAL;
38304d348aeSZhi Wang 
38404d348aeSZhi Wang 	if (offset == i915_mmio_reg_offset(PCH_GMBUS2))
38504d348aeSZhi Wang 		return gmbus2_mmio_read(vgpu, offset, p_data, bytes);
38604d348aeSZhi Wang 	else if (offset == i915_mmio_reg_offset(PCH_GMBUS3))
38704d348aeSZhi Wang 		return gmbus3_mmio_read(vgpu, offset, p_data, bytes);
38804d348aeSZhi Wang 
38904d348aeSZhi Wang 	memcpy(p_data, &vgpu_vreg(vgpu, offset), bytes);
39004d348aeSZhi Wang 	return 0;
39104d348aeSZhi Wang }
39204d348aeSZhi Wang 
39304d348aeSZhi Wang /**
39404d348aeSZhi Wang  * intel_gvt_i2c_handle_gmbus_write - emulate gmbus register mmio write
39504d348aeSZhi Wang  * @vgpu: a vGPU
396a752b070SZhenyu Wang  * @offset: reg offset
397a752b070SZhenyu Wang  * @p_data: data return buffer
398a752b070SZhenyu Wang  * @bytes: access data length
39904d348aeSZhi Wang  *
40004d348aeSZhi Wang  * This function is used to emulate gmbus register mmio write
40104d348aeSZhi Wang  *
40204d348aeSZhi Wang  * Returns:
40304d348aeSZhi Wang  * Zero on success, negative error code if failed.
40404d348aeSZhi Wang  *
40504d348aeSZhi Wang  */
intel_gvt_i2c_handle_gmbus_write(struct intel_vgpu * vgpu,unsigned int offset,void * p_data,unsigned int bytes)40604d348aeSZhi Wang int intel_gvt_i2c_handle_gmbus_write(struct intel_vgpu *vgpu,
40704d348aeSZhi Wang 		unsigned int offset, void *p_data, unsigned int bytes)
40804d348aeSZhi Wang {
409a61ac1e7SChris Wilson 	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
41012d58619SPankaj Bharadiya 
41112d58619SPankaj Bharadiya 	if (drm_WARN_ON(&i915->drm, bytes > 8 && (offset & (bytes - 1))))
41204d348aeSZhi Wang 		return -EINVAL;
41304d348aeSZhi Wang 
41404d348aeSZhi Wang 	if (offset == i915_mmio_reg_offset(PCH_GMBUS0))
41504d348aeSZhi Wang 		return gmbus0_mmio_write(vgpu, offset, p_data, bytes);
41604d348aeSZhi Wang 	else if (offset == i915_mmio_reg_offset(PCH_GMBUS1))
41704d348aeSZhi Wang 		return gmbus1_mmio_write(vgpu, offset, p_data, bytes);
41804d348aeSZhi Wang 	else if (offset == i915_mmio_reg_offset(PCH_GMBUS2))
41904d348aeSZhi Wang 		return gmbus2_mmio_write(vgpu, offset, p_data, bytes);
42004d348aeSZhi Wang 	else if (offset == i915_mmio_reg_offset(PCH_GMBUS3))
42104d348aeSZhi Wang 		return gmbus3_mmio_write(vgpu, offset, p_data, bytes);
42204d348aeSZhi Wang 
42304d348aeSZhi Wang 	memcpy(&vgpu_vreg(vgpu, offset), p_data, bytes);
42404d348aeSZhi Wang 	return 0;
42504d348aeSZhi Wang }
42604d348aeSZhi Wang 
42704d348aeSZhi Wang enum {
42804d348aeSZhi Wang 	AUX_CH_CTL = 0,
42904d348aeSZhi Wang 	AUX_CH_DATA1,
43004d348aeSZhi Wang 	AUX_CH_DATA2,
43104d348aeSZhi Wang 	AUX_CH_DATA3,
43204d348aeSZhi Wang 	AUX_CH_DATA4,
43304d348aeSZhi Wang 	AUX_CH_DATA5
43404d348aeSZhi Wang };
43504d348aeSZhi Wang 
get_aux_ch_reg(unsigned int offset)43604d348aeSZhi Wang static inline int get_aux_ch_reg(unsigned int offset)
43704d348aeSZhi Wang {
43804d348aeSZhi Wang 	int reg;
43904d348aeSZhi Wang 
44004d348aeSZhi Wang 	switch (offset & 0xff) {
44104d348aeSZhi Wang 	case 0x10:
44204d348aeSZhi Wang 		reg = AUX_CH_CTL;
44304d348aeSZhi Wang 		break;
44404d348aeSZhi Wang 	case 0x14:
44504d348aeSZhi Wang 		reg = AUX_CH_DATA1;
44604d348aeSZhi Wang 		break;
44704d348aeSZhi Wang 	case 0x18:
44804d348aeSZhi Wang 		reg = AUX_CH_DATA2;
44904d348aeSZhi Wang 		break;
45004d348aeSZhi Wang 	case 0x1c:
45104d348aeSZhi Wang 		reg = AUX_CH_DATA3;
45204d348aeSZhi Wang 		break;
45304d348aeSZhi Wang 	case 0x20:
45404d348aeSZhi Wang 		reg = AUX_CH_DATA4;
45504d348aeSZhi Wang 		break;
45604d348aeSZhi Wang 	case 0x24:
45704d348aeSZhi Wang 		reg = AUX_CH_DATA5;
45804d348aeSZhi Wang 		break;
45904d348aeSZhi Wang 	default:
46004d348aeSZhi Wang 		reg = -1;
46104d348aeSZhi Wang 		break;
46204d348aeSZhi Wang 	}
46304d348aeSZhi Wang 	return reg;
46404d348aeSZhi Wang }
46504d348aeSZhi Wang 
46604d348aeSZhi Wang /**
46704d348aeSZhi Wang  * intel_gvt_i2c_handle_aux_ch_write - emulate AUX channel register write
46804d348aeSZhi Wang  * @vgpu: a vGPU
469a752b070SZhenyu Wang  * @port_idx: port index
470a752b070SZhenyu Wang  * @offset: reg offset
471a752b070SZhenyu Wang  * @p_data: write ptr
47204d348aeSZhi Wang  *
47304d348aeSZhi Wang  * This function is used to emulate AUX channel register write
47404d348aeSZhi Wang  *
47504d348aeSZhi Wang  */
intel_gvt_i2c_handle_aux_ch_write(struct intel_vgpu * vgpu,int port_idx,unsigned int offset,void * p_data)47604d348aeSZhi Wang void intel_gvt_i2c_handle_aux_ch_write(struct intel_vgpu *vgpu,
47704d348aeSZhi Wang 				int port_idx,
47804d348aeSZhi Wang 				unsigned int offset,
47904d348aeSZhi Wang 				void *p_data)
48004d348aeSZhi Wang {
481a61ac1e7SChris Wilson 	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
48204d348aeSZhi Wang 	struct intel_vgpu_i2c_edid *i2c_edid = &vgpu->display.i2c_edid;
48304d348aeSZhi Wang 	int msg_length, ret_msg_size;
48404d348aeSZhi Wang 	int msg, addr, ctrl, op;
48504d348aeSZhi Wang 	u32 value = *(u32 *)p_data;
48604d348aeSZhi Wang 	int aux_data_for_write = 0;
48704d348aeSZhi Wang 	int reg = get_aux_ch_reg(offset);
48804d348aeSZhi Wang 
48904d348aeSZhi Wang 	if (reg != AUX_CH_CTL) {
49004d348aeSZhi Wang 		vgpu_vreg(vgpu, offset) = value;
49104d348aeSZhi Wang 		return;
49204d348aeSZhi Wang 	}
49304d348aeSZhi Wang 
494*46d14e17SYan Zhao 	msg_length = REG_FIELD_GET(DP_AUX_CH_CTL_MESSAGE_SIZE_MASK, value);
4950cad796aSVille Syrjälä 
49604d348aeSZhi Wang 	// check the msg in DATA register.
49704d348aeSZhi Wang 	msg = vgpu_vreg(vgpu, offset + 4);
49804d348aeSZhi Wang 	addr = (msg >> 8) & 0xffff;
49904d348aeSZhi Wang 	ctrl = (msg >> 24) & 0xff;
50004d348aeSZhi Wang 	op = ctrl >> 4;
50104d348aeSZhi Wang 	if (!(value & DP_AUX_CH_CTL_SEND_BUSY)) {
50204d348aeSZhi Wang 		/* The ctl write to clear some states */
50304d348aeSZhi Wang 		return;
50404d348aeSZhi Wang 	}
50504d348aeSZhi Wang 
50604d348aeSZhi Wang 	/* Always set the wanted value for vms. */
50704d348aeSZhi Wang 	ret_msg_size = (((op & 0x1) == GVT_AUX_I2C_READ) ? 2 : 1);
50804d348aeSZhi Wang 	vgpu_vreg(vgpu, offset) =
50904d348aeSZhi Wang 		DP_AUX_CH_CTL_DONE |
5100cad796aSVille Syrjälä 		DP_AUX_CH_CTL_MESSAGE_SIZE(ret_msg_size);
51104d348aeSZhi Wang 
51204d348aeSZhi Wang 	if (msg_length == 3) {
51304d348aeSZhi Wang 		if (!(op & GVT_AUX_I2C_MOT)) {
51404d348aeSZhi Wang 			/* stop */
51504d348aeSZhi Wang 			intel_vgpu_init_i2c_edid(vgpu);
51604d348aeSZhi Wang 		} else {
51704d348aeSZhi Wang 			/* start or restart */
51804d348aeSZhi Wang 			i2c_edid->aux_ch.i2c_over_aux_ch = true;
51904d348aeSZhi Wang 			i2c_edid->aux_ch.aux_ch_mot = true;
52004d348aeSZhi Wang 			if (addr == 0) {
52104d348aeSZhi Wang 				/* reset the address */
52204d348aeSZhi Wang 				intel_vgpu_init_i2c_edid(vgpu);
52304d348aeSZhi Wang 			} else if (addr == EDID_ADDR) {
52404d348aeSZhi Wang 				i2c_edid->state = I2C_AUX_CH;
52504d348aeSZhi Wang 				i2c_edid->port = port_idx;
52604d348aeSZhi Wang 				i2c_edid->slave_selected = true;
52704d348aeSZhi Wang 				if (intel_vgpu_has_monitor_on_port(vgpu,
52804d348aeSZhi Wang 					port_idx) &&
52904d348aeSZhi Wang 					intel_vgpu_port_is_dp(vgpu, port_idx))
53004d348aeSZhi Wang 					i2c_edid->edid_available = true;
53104d348aeSZhi Wang 			}
53204d348aeSZhi Wang 		}
53304d348aeSZhi Wang 	} else if ((op & 0x1) == GVT_AUX_I2C_WRITE) {
53404d348aeSZhi Wang 		/* TODO
53504d348aeSZhi Wang 		 * We only support EDID reading from I2C_over_AUX. And
53604d348aeSZhi Wang 		 * we do not expect the index mode to be used. Right now
53704d348aeSZhi Wang 		 * the WRITE operation is ignored. It is good enough to
53804d348aeSZhi Wang 		 * support the gfx driver to do EDID access.
53904d348aeSZhi Wang 		 */
54004d348aeSZhi Wang 	} else {
54112d58619SPankaj Bharadiya 		if (drm_WARN_ON(&i915->drm, (op & 0x1) != GVT_AUX_I2C_READ))
54204d348aeSZhi Wang 			return;
54312d58619SPankaj Bharadiya 		if (drm_WARN_ON(&i915->drm, msg_length != 4))
54404d348aeSZhi Wang 			return;
54504d348aeSZhi Wang 		if (i2c_edid->edid_available && i2c_edid->slave_selected) {
54604d348aeSZhi Wang 			unsigned char val = edid_get_byte(vgpu);
54704d348aeSZhi Wang 
54804d348aeSZhi Wang 			aux_data_for_write = (val << 16);
54914f5ba26SXu Han 		} else
55014f5ba26SXu Han 			aux_data_for_write = (0xff << 16);
55104d348aeSZhi Wang 	}
55204d348aeSZhi Wang 	/* write the return value in AUX_CH_DATA reg which includes:
55304d348aeSZhi Wang 	 * ACK of I2C_WRITE
55404d348aeSZhi Wang 	 * returned byte if it is READ
55504d348aeSZhi Wang 	 */
556ee145f66SZhenyu Wang 	aux_data_for_write |= GVT_AUX_I2C_REPLY_ACK << 24;
55704d348aeSZhi Wang 	vgpu_vreg(vgpu, offset + 4) = aux_data_for_write;
55804d348aeSZhi Wang }
55904d348aeSZhi Wang 
56004d348aeSZhi Wang /**
56104d348aeSZhi Wang  * intel_vgpu_init_i2c_edid - initialize vGPU i2c edid emulation
56204d348aeSZhi Wang  * @vgpu: a vGPU
56304d348aeSZhi Wang  *
56404d348aeSZhi Wang  * This function is used to initialize vGPU i2c edid emulation stuffs
56504d348aeSZhi Wang  *
56604d348aeSZhi Wang  */
intel_vgpu_init_i2c_edid(struct intel_vgpu * vgpu)56704d348aeSZhi Wang void intel_vgpu_init_i2c_edid(struct intel_vgpu *vgpu)
56804d348aeSZhi Wang {
56904d348aeSZhi Wang 	struct intel_vgpu_i2c_edid *edid = &vgpu->display.i2c_edid;
57004d348aeSZhi Wang 
57104d348aeSZhi Wang 	edid->state = I2C_NOT_SPECIFIED;
57204d348aeSZhi Wang 
57304d348aeSZhi Wang 	edid->port = -1;
57404d348aeSZhi Wang 	edid->slave_selected = false;
57504d348aeSZhi Wang 	edid->edid_available = false;
57604d348aeSZhi Wang 	edid->current_edid_read = 0;
57704d348aeSZhi Wang 
57804d348aeSZhi Wang 	memset(&edid->gmbus, 0, sizeof(struct intel_vgpu_i2c_gmbus));
57904d348aeSZhi Wang 
58004d348aeSZhi Wang 	edid->aux_ch.i2c_over_aux_ch = false;
58104d348aeSZhi Wang 	edid->aux_ch.aux_ch_mot = false;
58204d348aeSZhi Wang }
583