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