18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice (including the next
128c2ecf20Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
138c2ecf20Sopenharmony_ci * Software.
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
168c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
178c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
188c2ecf20Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
198c2ecf20Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
208c2ecf20Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
218c2ecf20Sopenharmony_ci * SOFTWARE.
228c2ecf20Sopenharmony_ci *
238c2ecf20Sopenharmony_ci * Authors:
248c2ecf20Sopenharmony_ci *    Kevin Tian <kevin.tian@intel.com>
258c2ecf20Sopenharmony_ci *    Zhi Wang <zhi.a.wang@intel.com>
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci * Contributors:
288c2ecf20Sopenharmony_ci *    Min he <min.he@intel.com>
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#include "i915_drv.h"
338c2ecf20Sopenharmony_ci#include "gvt.h"
348c2ecf20Sopenharmony_ci#include "trace.h"
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* common offset among interrupt control registers */
378c2ecf20Sopenharmony_ci#define regbase_to_isr(base)	(base)
388c2ecf20Sopenharmony_ci#define regbase_to_imr(base)	(base + 0x4)
398c2ecf20Sopenharmony_ci#define regbase_to_iir(base)	(base + 0x8)
408c2ecf20Sopenharmony_ci#define regbase_to_ier(base)	(base + 0xC)
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define iir_to_regbase(iir)    (iir - 0x8)
438c2ecf20Sopenharmony_ci#define ier_to_regbase(ier)    (ier - 0xC)
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define get_event_virt_handler(irq, e)	(irq->events[e].v_handler)
468c2ecf20Sopenharmony_ci#define get_irq_info(irq, e)		(irq->events[e].info)
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define irq_to_gvt(irq) \
498c2ecf20Sopenharmony_ci	container_of(irq, struct intel_gvt, irq)
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic void update_upstream_irq(struct intel_vgpu *vgpu,
528c2ecf20Sopenharmony_ci		struct intel_gvt_irq_info *info);
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic const char * const irq_name[INTEL_GVT_EVENT_MAX] = {
558c2ecf20Sopenharmony_ci	[RCS_MI_USER_INTERRUPT] = "Render CS MI USER INTERRUPT",
568c2ecf20Sopenharmony_ci	[RCS_DEBUG] = "Render EU debug from SVG",
578c2ecf20Sopenharmony_ci	[RCS_MMIO_SYNC_FLUSH] = "Render MMIO sync flush status",
588c2ecf20Sopenharmony_ci	[RCS_CMD_STREAMER_ERR] = "Render CS error interrupt",
598c2ecf20Sopenharmony_ci	[RCS_PIPE_CONTROL] = "Render PIPE CONTROL notify",
608c2ecf20Sopenharmony_ci	[RCS_WATCHDOG_EXCEEDED] = "Render CS Watchdog counter exceeded",
618c2ecf20Sopenharmony_ci	[RCS_PAGE_DIRECTORY_FAULT] = "Render page directory faults",
628c2ecf20Sopenharmony_ci	[RCS_AS_CONTEXT_SWITCH] = "Render AS Context Switch Interrupt",
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	[VCS_MI_USER_INTERRUPT] = "Video CS MI USER INTERRUPT",
658c2ecf20Sopenharmony_ci	[VCS_MMIO_SYNC_FLUSH] = "Video MMIO sync flush status",
668c2ecf20Sopenharmony_ci	[VCS_CMD_STREAMER_ERR] = "Video CS error interrupt",
678c2ecf20Sopenharmony_ci	[VCS_MI_FLUSH_DW] = "Video MI FLUSH DW notify",
688c2ecf20Sopenharmony_ci	[VCS_WATCHDOG_EXCEEDED] = "Video CS Watchdog counter exceeded",
698c2ecf20Sopenharmony_ci	[VCS_PAGE_DIRECTORY_FAULT] = "Video page directory faults",
708c2ecf20Sopenharmony_ci	[VCS_AS_CONTEXT_SWITCH] = "Video AS Context Switch Interrupt",
718c2ecf20Sopenharmony_ci	[VCS2_MI_USER_INTERRUPT] = "VCS2 Video CS MI USER INTERRUPT",
728c2ecf20Sopenharmony_ci	[VCS2_MI_FLUSH_DW] = "VCS2 Video MI FLUSH DW notify",
738c2ecf20Sopenharmony_ci	[VCS2_AS_CONTEXT_SWITCH] = "VCS2 Context Switch Interrupt",
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	[BCS_MI_USER_INTERRUPT] = "Blitter CS MI USER INTERRUPT",
768c2ecf20Sopenharmony_ci	[BCS_MMIO_SYNC_FLUSH] = "Billter MMIO sync flush status",
778c2ecf20Sopenharmony_ci	[BCS_CMD_STREAMER_ERR] = "Blitter CS error interrupt",
788c2ecf20Sopenharmony_ci	[BCS_MI_FLUSH_DW] = "Blitter MI FLUSH DW notify",
798c2ecf20Sopenharmony_ci	[BCS_PAGE_DIRECTORY_FAULT] = "Blitter page directory faults",
808c2ecf20Sopenharmony_ci	[BCS_AS_CONTEXT_SWITCH] = "Blitter AS Context Switch Interrupt",
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	[VECS_MI_FLUSH_DW] = "Video Enhanced Streamer MI FLUSH DW notify",
838c2ecf20Sopenharmony_ci	[VECS_AS_CONTEXT_SWITCH] = "VECS Context Switch Interrupt",
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	[PIPE_A_FIFO_UNDERRUN] = "Pipe A FIFO underrun",
868c2ecf20Sopenharmony_ci	[PIPE_A_CRC_ERR] = "Pipe A CRC error",
878c2ecf20Sopenharmony_ci	[PIPE_A_CRC_DONE] = "Pipe A CRC done",
888c2ecf20Sopenharmony_ci	[PIPE_A_VSYNC] = "Pipe A vsync",
898c2ecf20Sopenharmony_ci	[PIPE_A_LINE_COMPARE] = "Pipe A line compare",
908c2ecf20Sopenharmony_ci	[PIPE_A_ODD_FIELD] = "Pipe A odd field",
918c2ecf20Sopenharmony_ci	[PIPE_A_EVEN_FIELD] = "Pipe A even field",
928c2ecf20Sopenharmony_ci	[PIPE_A_VBLANK] = "Pipe A vblank",
938c2ecf20Sopenharmony_ci	[PIPE_B_FIFO_UNDERRUN] = "Pipe B FIFO underrun",
948c2ecf20Sopenharmony_ci	[PIPE_B_CRC_ERR] = "Pipe B CRC error",
958c2ecf20Sopenharmony_ci	[PIPE_B_CRC_DONE] = "Pipe B CRC done",
968c2ecf20Sopenharmony_ci	[PIPE_B_VSYNC] = "Pipe B vsync",
978c2ecf20Sopenharmony_ci	[PIPE_B_LINE_COMPARE] = "Pipe B line compare",
988c2ecf20Sopenharmony_ci	[PIPE_B_ODD_FIELD] = "Pipe B odd field",
998c2ecf20Sopenharmony_ci	[PIPE_B_EVEN_FIELD] = "Pipe B even field",
1008c2ecf20Sopenharmony_ci	[PIPE_B_VBLANK] = "Pipe B vblank",
1018c2ecf20Sopenharmony_ci	[PIPE_C_VBLANK] = "Pipe C vblank",
1028c2ecf20Sopenharmony_ci	[DPST_PHASE_IN] = "DPST phase in event",
1038c2ecf20Sopenharmony_ci	[DPST_HISTOGRAM] = "DPST histogram event",
1048c2ecf20Sopenharmony_ci	[GSE] = "GSE",
1058c2ecf20Sopenharmony_ci	[DP_A_HOTPLUG] = "DP A Hotplug",
1068c2ecf20Sopenharmony_ci	[AUX_CHANNEL_A] = "AUX Channel A",
1078c2ecf20Sopenharmony_ci	[PERF_COUNTER] = "Performance counter",
1088c2ecf20Sopenharmony_ci	[POISON] = "Poison",
1098c2ecf20Sopenharmony_ci	[GTT_FAULT] = "GTT fault",
1108c2ecf20Sopenharmony_ci	[PRIMARY_A_FLIP_DONE] = "Primary Plane A flip done",
1118c2ecf20Sopenharmony_ci	[PRIMARY_B_FLIP_DONE] = "Primary Plane B flip done",
1128c2ecf20Sopenharmony_ci	[PRIMARY_C_FLIP_DONE] = "Primary Plane C flip done",
1138c2ecf20Sopenharmony_ci	[SPRITE_A_FLIP_DONE] = "Sprite Plane A flip done",
1148c2ecf20Sopenharmony_ci	[SPRITE_B_FLIP_DONE] = "Sprite Plane B flip done",
1158c2ecf20Sopenharmony_ci	[SPRITE_C_FLIP_DONE] = "Sprite Plane C flip done",
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	[PCU_THERMAL] = "PCU Thermal Event",
1188c2ecf20Sopenharmony_ci	[PCU_PCODE2DRIVER_MAILBOX] = "PCU pcode2driver mailbox event",
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	[FDI_RX_INTERRUPTS_TRANSCODER_A] = "FDI RX Interrupts Combined A",
1218c2ecf20Sopenharmony_ci	[AUDIO_CP_CHANGE_TRANSCODER_A] = "Audio CP Change Transcoder A",
1228c2ecf20Sopenharmony_ci	[AUDIO_CP_REQUEST_TRANSCODER_A] = "Audio CP Request Transcoder A",
1238c2ecf20Sopenharmony_ci	[FDI_RX_INTERRUPTS_TRANSCODER_B] = "FDI RX Interrupts Combined B",
1248c2ecf20Sopenharmony_ci	[AUDIO_CP_CHANGE_TRANSCODER_B] = "Audio CP Change Transcoder B",
1258c2ecf20Sopenharmony_ci	[AUDIO_CP_REQUEST_TRANSCODER_B] = "Audio CP Request Transcoder B",
1268c2ecf20Sopenharmony_ci	[FDI_RX_INTERRUPTS_TRANSCODER_C] = "FDI RX Interrupts Combined C",
1278c2ecf20Sopenharmony_ci	[AUDIO_CP_CHANGE_TRANSCODER_C] = "Audio CP Change Transcoder C",
1288c2ecf20Sopenharmony_ci	[AUDIO_CP_REQUEST_TRANSCODER_C] = "Audio CP Request Transcoder C",
1298c2ecf20Sopenharmony_ci	[ERR_AND_DBG] = "South Error and Debug Interrupts Combined",
1308c2ecf20Sopenharmony_ci	[GMBUS] = "Gmbus",
1318c2ecf20Sopenharmony_ci	[SDVO_B_HOTPLUG] = "SDVO B hotplug",
1328c2ecf20Sopenharmony_ci	[CRT_HOTPLUG] = "CRT Hotplug",
1338c2ecf20Sopenharmony_ci	[DP_B_HOTPLUG] = "DisplayPort/HDMI/DVI B Hotplug",
1348c2ecf20Sopenharmony_ci	[DP_C_HOTPLUG] = "DisplayPort/HDMI/DVI C Hotplug",
1358c2ecf20Sopenharmony_ci	[DP_D_HOTPLUG] = "DisplayPort/HDMI/DVI D Hotplug",
1368c2ecf20Sopenharmony_ci	[AUX_CHANNEL_B] = "AUX Channel B",
1378c2ecf20Sopenharmony_ci	[AUX_CHANNEL_C] = "AUX Channel C",
1388c2ecf20Sopenharmony_ci	[AUX_CHANNEL_D] = "AUX Channel D",
1398c2ecf20Sopenharmony_ci	[AUDIO_POWER_STATE_CHANGE_B] = "Audio Power State change Port B",
1408c2ecf20Sopenharmony_ci	[AUDIO_POWER_STATE_CHANGE_C] = "Audio Power State change Port C",
1418c2ecf20Sopenharmony_ci	[AUDIO_POWER_STATE_CHANGE_D] = "Audio Power State change Port D",
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	[INTEL_GVT_EVENT_RESERVED] = "RESERVED EVENTS!!!",
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic inline struct intel_gvt_irq_info *regbase_to_irq_info(
1478c2ecf20Sopenharmony_ci		struct intel_gvt *gvt,
1488c2ecf20Sopenharmony_ci		unsigned int reg)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	struct intel_gvt_irq *irq = &gvt->irq;
1518c2ecf20Sopenharmony_ci	int i;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	for_each_set_bit(i, irq->irq_info_bitmap, INTEL_GVT_IRQ_INFO_MAX) {
1548c2ecf20Sopenharmony_ci		if (i915_mmio_reg_offset(irq->info[i]->reg_base) == reg)
1558c2ecf20Sopenharmony_ci			return irq->info[i];
1568c2ecf20Sopenharmony_ci	}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	return NULL;
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci/**
1628c2ecf20Sopenharmony_ci * intel_vgpu_reg_imr_handler - Generic IMR register emulation write handler
1638c2ecf20Sopenharmony_ci * @vgpu: a vGPU
1648c2ecf20Sopenharmony_ci * @reg: register offset written by guest
1658c2ecf20Sopenharmony_ci * @p_data: register data written by guest
1668c2ecf20Sopenharmony_ci * @bytes: register data length
1678c2ecf20Sopenharmony_ci *
1688c2ecf20Sopenharmony_ci * This function is used to emulate the generic IMR register bit change
1698c2ecf20Sopenharmony_ci * behavior.
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci * Returns:
1728c2ecf20Sopenharmony_ci * Zero on success, negative error code if failed.
1738c2ecf20Sopenharmony_ci *
1748c2ecf20Sopenharmony_ci */
1758c2ecf20Sopenharmony_ciint intel_vgpu_reg_imr_handler(struct intel_vgpu *vgpu,
1768c2ecf20Sopenharmony_ci	unsigned int reg, void *p_data, unsigned int bytes)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct intel_gvt *gvt = vgpu->gvt;
1798c2ecf20Sopenharmony_ci	struct intel_gvt_irq_ops *ops = gvt->irq.ops;
1808c2ecf20Sopenharmony_ci	u32 imr = *(u32 *)p_data;
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	trace_write_ir(vgpu->id, "IMR", reg, imr, vgpu_vreg(vgpu, reg),
1838c2ecf20Sopenharmony_ci		       (vgpu_vreg(vgpu, reg) ^ imr));
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	vgpu_vreg(vgpu, reg) = imr;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	ops->check_pending_irq(vgpu);
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	return 0;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/**
1938c2ecf20Sopenharmony_ci * intel_vgpu_reg_master_irq_handler - master IRQ write emulation handler
1948c2ecf20Sopenharmony_ci * @vgpu: a vGPU
1958c2ecf20Sopenharmony_ci * @reg: register offset written by guest
1968c2ecf20Sopenharmony_ci * @p_data: register data written by guest
1978c2ecf20Sopenharmony_ci * @bytes: register data length
1988c2ecf20Sopenharmony_ci *
1998c2ecf20Sopenharmony_ci * This function is used to emulate the master IRQ register on gen8+.
2008c2ecf20Sopenharmony_ci *
2018c2ecf20Sopenharmony_ci * Returns:
2028c2ecf20Sopenharmony_ci * Zero on success, negative error code if failed.
2038c2ecf20Sopenharmony_ci *
2048c2ecf20Sopenharmony_ci */
2058c2ecf20Sopenharmony_ciint intel_vgpu_reg_master_irq_handler(struct intel_vgpu *vgpu,
2068c2ecf20Sopenharmony_ci	unsigned int reg, void *p_data, unsigned int bytes)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	struct intel_gvt *gvt = vgpu->gvt;
2098c2ecf20Sopenharmony_ci	struct intel_gvt_irq_ops *ops = gvt->irq.ops;
2108c2ecf20Sopenharmony_ci	u32 ier = *(u32 *)p_data;
2118c2ecf20Sopenharmony_ci	u32 virtual_ier = vgpu_vreg(vgpu, reg);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	trace_write_ir(vgpu->id, "MASTER_IRQ", reg, ier, virtual_ier,
2148c2ecf20Sopenharmony_ci		       (virtual_ier ^ ier));
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/*
2178c2ecf20Sopenharmony_ci	 * GEN8_MASTER_IRQ is a special irq register,
2188c2ecf20Sopenharmony_ci	 * only bit 31 is allowed to be modified
2198c2ecf20Sopenharmony_ci	 * and treated as an IER bit.
2208c2ecf20Sopenharmony_ci	 */
2218c2ecf20Sopenharmony_ci	ier &= GEN8_MASTER_IRQ_CONTROL;
2228c2ecf20Sopenharmony_ci	virtual_ier &= GEN8_MASTER_IRQ_CONTROL;
2238c2ecf20Sopenharmony_ci	vgpu_vreg(vgpu, reg) &= ~GEN8_MASTER_IRQ_CONTROL;
2248c2ecf20Sopenharmony_ci	vgpu_vreg(vgpu, reg) |= ier;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	ops->check_pending_irq(vgpu);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	return 0;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci/**
2328c2ecf20Sopenharmony_ci * intel_vgpu_reg_ier_handler - Generic IER write emulation handler
2338c2ecf20Sopenharmony_ci * @vgpu: a vGPU
2348c2ecf20Sopenharmony_ci * @reg: register offset written by guest
2358c2ecf20Sopenharmony_ci * @p_data: register data written by guest
2368c2ecf20Sopenharmony_ci * @bytes: register data length
2378c2ecf20Sopenharmony_ci *
2388c2ecf20Sopenharmony_ci * This function is used to emulate the generic IER register behavior.
2398c2ecf20Sopenharmony_ci *
2408c2ecf20Sopenharmony_ci * Returns:
2418c2ecf20Sopenharmony_ci * Zero on success, negative error code if failed.
2428c2ecf20Sopenharmony_ci *
2438c2ecf20Sopenharmony_ci */
2448c2ecf20Sopenharmony_ciint intel_vgpu_reg_ier_handler(struct intel_vgpu *vgpu,
2458c2ecf20Sopenharmony_ci	unsigned int reg, void *p_data, unsigned int bytes)
2468c2ecf20Sopenharmony_ci{
2478c2ecf20Sopenharmony_ci	struct intel_gvt *gvt = vgpu->gvt;
2488c2ecf20Sopenharmony_ci	struct drm_i915_private *i915 = gvt->gt->i915;
2498c2ecf20Sopenharmony_ci	struct intel_gvt_irq_ops *ops = gvt->irq.ops;
2508c2ecf20Sopenharmony_ci	struct intel_gvt_irq_info *info;
2518c2ecf20Sopenharmony_ci	u32 ier = *(u32 *)p_data;
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	trace_write_ir(vgpu->id, "IER", reg, ier, vgpu_vreg(vgpu, reg),
2548c2ecf20Sopenharmony_ci		       (vgpu_vreg(vgpu, reg) ^ ier));
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci	vgpu_vreg(vgpu, reg) = ier;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	info = regbase_to_irq_info(gvt, ier_to_regbase(reg));
2598c2ecf20Sopenharmony_ci	if (drm_WARN_ON(&i915->drm, !info))
2608c2ecf20Sopenharmony_ci		return -EINVAL;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	if (info->has_upstream_irq)
2638c2ecf20Sopenharmony_ci		update_upstream_irq(vgpu, info);
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	ops->check_pending_irq(vgpu);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return 0;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci/**
2718c2ecf20Sopenharmony_ci * intel_vgpu_reg_iir_handler - Generic IIR write emulation handler
2728c2ecf20Sopenharmony_ci * @vgpu: a vGPU
2738c2ecf20Sopenharmony_ci * @reg: register offset written by guest
2748c2ecf20Sopenharmony_ci * @p_data: register data written by guest
2758c2ecf20Sopenharmony_ci * @bytes: register data length
2768c2ecf20Sopenharmony_ci *
2778c2ecf20Sopenharmony_ci * This function is used to emulate the generic IIR register behavior.
2788c2ecf20Sopenharmony_ci *
2798c2ecf20Sopenharmony_ci * Returns:
2808c2ecf20Sopenharmony_ci * Zero on success, negative error code if failed.
2818c2ecf20Sopenharmony_ci *
2828c2ecf20Sopenharmony_ci */
2838c2ecf20Sopenharmony_ciint intel_vgpu_reg_iir_handler(struct intel_vgpu *vgpu, unsigned int reg,
2848c2ecf20Sopenharmony_ci	void *p_data, unsigned int bytes)
2858c2ecf20Sopenharmony_ci{
2868c2ecf20Sopenharmony_ci	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
2878c2ecf20Sopenharmony_ci	struct intel_gvt_irq_info *info = regbase_to_irq_info(vgpu->gvt,
2888c2ecf20Sopenharmony_ci		iir_to_regbase(reg));
2898c2ecf20Sopenharmony_ci	u32 iir = *(u32 *)p_data;
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	trace_write_ir(vgpu->id, "IIR", reg, iir, vgpu_vreg(vgpu, reg),
2928c2ecf20Sopenharmony_ci		       (vgpu_vreg(vgpu, reg) ^ iir));
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	if (drm_WARN_ON(&i915->drm, !info))
2958c2ecf20Sopenharmony_ci		return -EINVAL;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	vgpu_vreg(vgpu, reg) &= ~iir;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if (info->has_upstream_irq)
3008c2ecf20Sopenharmony_ci		update_upstream_irq(vgpu, info);
3018c2ecf20Sopenharmony_ci	return 0;
3028c2ecf20Sopenharmony_ci}
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_cistatic struct intel_gvt_irq_map gen8_irq_map[] = {
3058c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 0, INTEL_GVT_IRQ_INFO_GT0, 0xffff },
3068c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 1, INTEL_GVT_IRQ_INFO_GT0, 0xffff0000 },
3078c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 2, INTEL_GVT_IRQ_INFO_GT1, 0xffff },
3088c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 3, INTEL_GVT_IRQ_INFO_GT1, 0xffff0000 },
3098c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 4, INTEL_GVT_IRQ_INFO_GT2, 0xffff },
3108c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 6, INTEL_GVT_IRQ_INFO_GT3, 0xffff },
3118c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 16, INTEL_GVT_IRQ_INFO_DE_PIPE_A, ~0 },
3128c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 17, INTEL_GVT_IRQ_INFO_DE_PIPE_B, ~0 },
3138c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 18, INTEL_GVT_IRQ_INFO_DE_PIPE_C, ~0 },
3148c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 20, INTEL_GVT_IRQ_INFO_DE_PORT, ~0 },
3158c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 22, INTEL_GVT_IRQ_INFO_DE_MISC, ~0 },
3168c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 23, INTEL_GVT_IRQ_INFO_PCH, ~0 },
3178c2ecf20Sopenharmony_ci	{ INTEL_GVT_IRQ_INFO_MASTER, 30, INTEL_GVT_IRQ_INFO_PCU, ~0 },
3188c2ecf20Sopenharmony_ci	{ -1, -1, ~0 },
3198c2ecf20Sopenharmony_ci};
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cistatic void update_upstream_irq(struct intel_vgpu *vgpu,
3228c2ecf20Sopenharmony_ci		struct intel_gvt_irq_info *info)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
3258c2ecf20Sopenharmony_ci	struct intel_gvt_irq *irq = &vgpu->gvt->irq;
3268c2ecf20Sopenharmony_ci	struct intel_gvt_irq_map *map = irq->irq_map;
3278c2ecf20Sopenharmony_ci	struct intel_gvt_irq_info *up_irq_info = NULL;
3288c2ecf20Sopenharmony_ci	u32 set_bits = 0;
3298c2ecf20Sopenharmony_ci	u32 clear_bits = 0;
3308c2ecf20Sopenharmony_ci	int bit;
3318c2ecf20Sopenharmony_ci	u32 val = vgpu_vreg(vgpu,
3328c2ecf20Sopenharmony_ci			regbase_to_iir(i915_mmio_reg_offset(info->reg_base)))
3338c2ecf20Sopenharmony_ci		& vgpu_vreg(vgpu,
3348c2ecf20Sopenharmony_ci			regbase_to_ier(i915_mmio_reg_offset(info->reg_base)));
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci	if (!info->has_upstream_irq)
3378c2ecf20Sopenharmony_ci		return;
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	for (map = irq->irq_map; map->up_irq_bit != -1; map++) {
3408c2ecf20Sopenharmony_ci		if (info->group != map->down_irq_group)
3418c2ecf20Sopenharmony_ci			continue;
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci		if (!up_irq_info)
3448c2ecf20Sopenharmony_ci			up_irq_info = irq->info[map->up_irq_group];
3458c2ecf20Sopenharmony_ci		else
3468c2ecf20Sopenharmony_ci			drm_WARN_ON(&i915->drm, up_irq_info !=
3478c2ecf20Sopenharmony_ci				    irq->info[map->up_irq_group]);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci		bit = map->up_irq_bit;
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci		if (val & map->down_irq_bitmask)
3528c2ecf20Sopenharmony_ci			set_bits |= (1 << bit);
3538c2ecf20Sopenharmony_ci		else
3548c2ecf20Sopenharmony_ci			clear_bits |= (1 << bit);
3558c2ecf20Sopenharmony_ci	}
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci	if (drm_WARN_ON(&i915->drm, !up_irq_info))
3588c2ecf20Sopenharmony_ci		return;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	if (up_irq_info->group == INTEL_GVT_IRQ_INFO_MASTER) {
3618c2ecf20Sopenharmony_ci		u32 isr = i915_mmio_reg_offset(up_irq_info->reg_base);
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ci		vgpu_vreg(vgpu, isr) &= ~clear_bits;
3648c2ecf20Sopenharmony_ci		vgpu_vreg(vgpu, isr) |= set_bits;
3658c2ecf20Sopenharmony_ci	} else {
3668c2ecf20Sopenharmony_ci		u32 iir = regbase_to_iir(
3678c2ecf20Sopenharmony_ci			i915_mmio_reg_offset(up_irq_info->reg_base));
3688c2ecf20Sopenharmony_ci		u32 imr = regbase_to_imr(
3698c2ecf20Sopenharmony_ci			i915_mmio_reg_offset(up_irq_info->reg_base));
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci		vgpu_vreg(vgpu, iir) |= (set_bits & ~vgpu_vreg(vgpu, imr));
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (up_irq_info->has_upstream_irq)
3758c2ecf20Sopenharmony_ci		update_upstream_irq(vgpu, up_irq_info);
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic void init_irq_map(struct intel_gvt_irq *irq)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	struct intel_gvt_irq_map *map;
3818c2ecf20Sopenharmony_ci	struct intel_gvt_irq_info *up_info, *down_info;
3828c2ecf20Sopenharmony_ci	int up_bit;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	for (map = irq->irq_map; map->up_irq_bit != -1; map++) {
3858c2ecf20Sopenharmony_ci		up_info = irq->info[map->up_irq_group];
3868c2ecf20Sopenharmony_ci		up_bit = map->up_irq_bit;
3878c2ecf20Sopenharmony_ci		down_info = irq->info[map->down_irq_group];
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci		set_bit(up_bit, up_info->downstream_irq_bitmap);
3908c2ecf20Sopenharmony_ci		down_info->has_upstream_irq = true;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci		gvt_dbg_irq("[up] grp %d bit %d -> [down] grp %d bitmask %x\n",
3938c2ecf20Sopenharmony_ci			up_info->group, up_bit,
3948c2ecf20Sopenharmony_ci			down_info->group, map->down_irq_bitmask);
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci/* =======================vEvent injection===================== */
3998c2ecf20Sopenharmony_cistatic int inject_virtual_interrupt(struct intel_vgpu *vgpu)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	return intel_gvt_hypervisor_inject_msi(vgpu);
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic void propagate_event(struct intel_gvt_irq *irq,
4058c2ecf20Sopenharmony_ci	enum intel_gvt_event_type event, struct intel_vgpu *vgpu)
4068c2ecf20Sopenharmony_ci{
4078c2ecf20Sopenharmony_ci	struct intel_gvt_irq_info *info;
4088c2ecf20Sopenharmony_ci	unsigned int reg_base;
4098c2ecf20Sopenharmony_ci	int bit;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	info = get_irq_info(irq, event);
4128c2ecf20Sopenharmony_ci	if (WARN_ON(!info))
4138c2ecf20Sopenharmony_ci		return;
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	reg_base = i915_mmio_reg_offset(info->reg_base);
4168c2ecf20Sopenharmony_ci	bit = irq->events[event].bit;
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	if (!test_bit(bit, (void *)&vgpu_vreg(vgpu,
4198c2ecf20Sopenharmony_ci					regbase_to_imr(reg_base)))) {
4208c2ecf20Sopenharmony_ci		trace_propagate_event(vgpu->id, irq_name[event], bit);
4218c2ecf20Sopenharmony_ci		set_bit(bit, (void *)&vgpu_vreg(vgpu,
4228c2ecf20Sopenharmony_ci					regbase_to_iir(reg_base)));
4238c2ecf20Sopenharmony_ci	}
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_ci/* =======================vEvent Handlers===================== */
4278c2ecf20Sopenharmony_cistatic void handle_default_event_virt(struct intel_gvt_irq *irq,
4288c2ecf20Sopenharmony_ci	enum intel_gvt_event_type event, struct intel_vgpu *vgpu)
4298c2ecf20Sopenharmony_ci{
4308c2ecf20Sopenharmony_ci	if (!vgpu->irq.irq_warn_once[event]) {
4318c2ecf20Sopenharmony_ci		gvt_dbg_core("vgpu%d: IRQ receive event %d (%s)\n",
4328c2ecf20Sopenharmony_ci			vgpu->id, event, irq_name[event]);
4338c2ecf20Sopenharmony_ci		vgpu->irq.irq_warn_once[event] = true;
4348c2ecf20Sopenharmony_ci	}
4358c2ecf20Sopenharmony_ci	propagate_event(irq, event, vgpu);
4368c2ecf20Sopenharmony_ci}
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci/* =====================GEN specific logic======================= */
4398c2ecf20Sopenharmony_ci/* GEN8 interrupt routines. */
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci#define DEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(regname, regbase) \
4428c2ecf20Sopenharmony_cistatic struct intel_gvt_irq_info gen8_##regname##_info = { \
4438c2ecf20Sopenharmony_ci	.name = #regname"-IRQ", \
4448c2ecf20Sopenharmony_ci	.reg_base = (regbase), \
4458c2ecf20Sopenharmony_ci	.bit_to_event = {[0 ... INTEL_GVT_IRQ_BITWIDTH-1] = \
4468c2ecf20Sopenharmony_ci		INTEL_GVT_EVENT_RESERVED}, \
4478c2ecf20Sopenharmony_ci}
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(gt0, GEN8_GT_ISR(0));
4508c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(gt1, GEN8_GT_ISR(1));
4518c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(gt2, GEN8_GT_ISR(2));
4528c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(gt3, GEN8_GT_ISR(3));
4538c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(de_pipe_a, GEN8_DE_PIPE_ISR(PIPE_A));
4548c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(de_pipe_b, GEN8_DE_PIPE_ISR(PIPE_B));
4558c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(de_pipe_c, GEN8_DE_PIPE_ISR(PIPE_C));
4568c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(de_port, GEN8_DE_PORT_ISR);
4578c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(de_misc, GEN8_DE_MISC_ISR);
4588c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(pcu, GEN8_PCU_ISR);
4598c2ecf20Sopenharmony_ciDEFINE_GVT_GEN8_INTEL_GVT_IRQ_INFO(master, GEN8_MASTER_IRQ);
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic struct intel_gvt_irq_info gvt_base_pch_info = {
4628c2ecf20Sopenharmony_ci	.name = "PCH-IRQ",
4638c2ecf20Sopenharmony_ci	.reg_base = SDEISR,
4648c2ecf20Sopenharmony_ci	.bit_to_event = {[0 ... INTEL_GVT_IRQ_BITWIDTH-1] =
4658c2ecf20Sopenharmony_ci		INTEL_GVT_EVENT_RESERVED},
4668c2ecf20Sopenharmony_ci};
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_cistatic void gen8_check_pending_irq(struct intel_vgpu *vgpu)
4698c2ecf20Sopenharmony_ci{
4708c2ecf20Sopenharmony_ci	struct intel_gvt_irq *irq = &vgpu->gvt->irq;
4718c2ecf20Sopenharmony_ci	int i;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	if (!(vgpu_vreg(vgpu, i915_mmio_reg_offset(GEN8_MASTER_IRQ)) &
4748c2ecf20Sopenharmony_ci				GEN8_MASTER_IRQ_CONTROL))
4758c2ecf20Sopenharmony_ci		return;
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_ci	for_each_set_bit(i, irq->irq_info_bitmap, INTEL_GVT_IRQ_INFO_MAX) {
4788c2ecf20Sopenharmony_ci		struct intel_gvt_irq_info *info = irq->info[i];
4798c2ecf20Sopenharmony_ci		u32 reg_base;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci		if (!info->has_upstream_irq)
4828c2ecf20Sopenharmony_ci			continue;
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci		reg_base = i915_mmio_reg_offset(info->reg_base);
4858c2ecf20Sopenharmony_ci		if ((vgpu_vreg(vgpu, regbase_to_iir(reg_base))
4868c2ecf20Sopenharmony_ci				& vgpu_vreg(vgpu, regbase_to_ier(reg_base))))
4878c2ecf20Sopenharmony_ci			update_upstream_irq(vgpu, info);
4888c2ecf20Sopenharmony_ci	}
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci	if (vgpu_vreg(vgpu, i915_mmio_reg_offset(GEN8_MASTER_IRQ))
4918c2ecf20Sopenharmony_ci			& ~GEN8_MASTER_IRQ_CONTROL)
4928c2ecf20Sopenharmony_ci		inject_virtual_interrupt(vgpu);
4938c2ecf20Sopenharmony_ci}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_cistatic void gen8_init_irq(
4968c2ecf20Sopenharmony_ci		struct intel_gvt_irq *irq)
4978c2ecf20Sopenharmony_ci{
4988c2ecf20Sopenharmony_ci	struct intel_gvt *gvt = irq_to_gvt(irq);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci#define SET_BIT_INFO(s, b, e, i)		\
5018c2ecf20Sopenharmony_ci	do {					\
5028c2ecf20Sopenharmony_ci		s->events[e].bit = b;		\
5038c2ecf20Sopenharmony_ci		s->events[e].info = s->info[i];	\
5048c2ecf20Sopenharmony_ci		s->info[i]->bit_to_event[b] = e;\
5058c2ecf20Sopenharmony_ci	} while (0)
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci#define SET_IRQ_GROUP(s, g, i) \
5088c2ecf20Sopenharmony_ci	do { \
5098c2ecf20Sopenharmony_ci		s->info[g] = i; \
5108c2ecf20Sopenharmony_ci		(i)->group = g; \
5118c2ecf20Sopenharmony_ci		set_bit(g, s->irq_info_bitmap); \
5128c2ecf20Sopenharmony_ci	} while (0)
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_MASTER, &gen8_master_info);
5158c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_GT0, &gen8_gt0_info);
5168c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_GT1, &gen8_gt1_info);
5178c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_GT2, &gen8_gt2_info);
5188c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_GT3, &gen8_gt3_info);
5198c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_DE_PIPE_A, &gen8_de_pipe_a_info);
5208c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_DE_PIPE_B, &gen8_de_pipe_b_info);
5218c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_DE_PIPE_C, &gen8_de_pipe_c_info);
5228c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_DE_PORT, &gen8_de_port_info);
5238c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_DE_MISC, &gen8_de_misc_info);
5248c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_PCU, &gen8_pcu_info);
5258c2ecf20Sopenharmony_ci	SET_IRQ_GROUP(irq, INTEL_GVT_IRQ_INFO_PCH, &gvt_base_pch_info);
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	/* GEN8 level 2 interrupts. */
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	/* GEN8 interrupt GT0 events */
5308c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 0, RCS_MI_USER_INTERRUPT, INTEL_GVT_IRQ_INFO_GT0);
5318c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 4, RCS_PIPE_CONTROL, INTEL_GVT_IRQ_INFO_GT0);
5328c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 8, RCS_AS_CONTEXT_SWITCH, INTEL_GVT_IRQ_INFO_GT0);
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 16, BCS_MI_USER_INTERRUPT, INTEL_GVT_IRQ_INFO_GT0);
5358c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 20, BCS_MI_FLUSH_DW, INTEL_GVT_IRQ_INFO_GT0);
5368c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 24, BCS_AS_CONTEXT_SWITCH, INTEL_GVT_IRQ_INFO_GT0);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	/* GEN8 interrupt GT1 events */
5398c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 0, VCS_MI_USER_INTERRUPT, INTEL_GVT_IRQ_INFO_GT1);
5408c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 4, VCS_MI_FLUSH_DW, INTEL_GVT_IRQ_INFO_GT1);
5418c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 8, VCS_AS_CONTEXT_SWITCH, INTEL_GVT_IRQ_INFO_GT1);
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_ci	if (HAS_ENGINE(gvt->gt, VCS1)) {
5448c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 16, VCS2_MI_USER_INTERRUPT,
5458c2ecf20Sopenharmony_ci			INTEL_GVT_IRQ_INFO_GT1);
5468c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 20, VCS2_MI_FLUSH_DW,
5478c2ecf20Sopenharmony_ci			INTEL_GVT_IRQ_INFO_GT1);
5488c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 24, VCS2_AS_CONTEXT_SWITCH,
5498c2ecf20Sopenharmony_ci			INTEL_GVT_IRQ_INFO_GT1);
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	/* GEN8 interrupt GT3 events */
5538c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 0, VECS_MI_USER_INTERRUPT, INTEL_GVT_IRQ_INFO_GT3);
5548c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 4, VECS_MI_FLUSH_DW, INTEL_GVT_IRQ_INFO_GT3);
5558c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 8, VECS_AS_CONTEXT_SWITCH, INTEL_GVT_IRQ_INFO_GT3);
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 0, PIPE_A_VBLANK, INTEL_GVT_IRQ_INFO_DE_PIPE_A);
5588c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 0, PIPE_B_VBLANK, INTEL_GVT_IRQ_INFO_DE_PIPE_B);
5598c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 0, PIPE_C_VBLANK, INTEL_GVT_IRQ_INFO_DE_PIPE_C);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci	/* GEN8 interrupt DE PORT events */
5628c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 0, AUX_CHANNEL_A, INTEL_GVT_IRQ_INFO_DE_PORT);
5638c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 3, DP_A_HOTPLUG, INTEL_GVT_IRQ_INFO_DE_PORT);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	/* GEN8 interrupt DE MISC events */
5668c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 0, GSE, INTEL_GVT_IRQ_INFO_DE_MISC);
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	/* PCH events */
5698c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 17, GMBUS, INTEL_GVT_IRQ_INFO_PCH);
5708c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 19, CRT_HOTPLUG, INTEL_GVT_IRQ_INFO_PCH);
5718c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 21, DP_B_HOTPLUG, INTEL_GVT_IRQ_INFO_PCH);
5728c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 22, DP_C_HOTPLUG, INTEL_GVT_IRQ_INFO_PCH);
5738c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 23, DP_D_HOTPLUG, INTEL_GVT_IRQ_INFO_PCH);
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_ci	if (IS_BROADWELL(gvt->gt->i915)) {
5768c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 25, AUX_CHANNEL_B, INTEL_GVT_IRQ_INFO_PCH);
5778c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 26, AUX_CHANNEL_C, INTEL_GVT_IRQ_INFO_PCH);
5788c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 27, AUX_CHANNEL_D, INTEL_GVT_IRQ_INFO_PCH);
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 4, PRIMARY_A_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_A);
5818c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 5, SPRITE_A_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_A);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 4, PRIMARY_B_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_B);
5848c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 5, SPRITE_B_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_B);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 4, PRIMARY_C_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_C);
5878c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 5, SPRITE_C_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_C);
5888c2ecf20Sopenharmony_ci	} else if (INTEL_GEN(gvt->gt->i915) >= 9) {
5898c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 25, AUX_CHANNEL_B, INTEL_GVT_IRQ_INFO_DE_PORT);
5908c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 26, AUX_CHANNEL_C, INTEL_GVT_IRQ_INFO_DE_PORT);
5918c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 27, AUX_CHANNEL_D, INTEL_GVT_IRQ_INFO_DE_PORT);
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 3, PRIMARY_A_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_A);
5948c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 3, PRIMARY_B_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_B);
5958c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 3, PRIMARY_C_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_C);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 4, SPRITE_A_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_A);
5988c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 4, SPRITE_B_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_B);
5998c2ecf20Sopenharmony_ci		SET_BIT_INFO(irq, 4, SPRITE_C_FLIP_DONE, INTEL_GVT_IRQ_INFO_DE_PIPE_C);
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	/* GEN8 interrupt PCU events */
6038c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 24, PCU_THERMAL, INTEL_GVT_IRQ_INFO_PCU);
6048c2ecf20Sopenharmony_ci	SET_BIT_INFO(irq, 25, PCU_PCODE2DRIVER_MAILBOX, INTEL_GVT_IRQ_INFO_PCU);
6058c2ecf20Sopenharmony_ci}
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_cistatic struct intel_gvt_irq_ops gen8_irq_ops = {
6088c2ecf20Sopenharmony_ci	.init_irq = gen8_init_irq,
6098c2ecf20Sopenharmony_ci	.check_pending_irq = gen8_check_pending_irq,
6108c2ecf20Sopenharmony_ci};
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci/**
6138c2ecf20Sopenharmony_ci * intel_vgpu_trigger_virtual_event - Trigger a virtual event for a vGPU
6148c2ecf20Sopenharmony_ci * @vgpu: a vGPU
6158c2ecf20Sopenharmony_ci * @event: interrupt event
6168c2ecf20Sopenharmony_ci *
6178c2ecf20Sopenharmony_ci * This function is used to trigger a virtual interrupt event for vGPU.
6188c2ecf20Sopenharmony_ci * The caller provides the event to be triggered, the framework itself
6198c2ecf20Sopenharmony_ci * will emulate the IRQ register bit change.
6208c2ecf20Sopenharmony_ci *
6218c2ecf20Sopenharmony_ci */
6228c2ecf20Sopenharmony_civoid intel_vgpu_trigger_virtual_event(struct intel_vgpu *vgpu,
6238c2ecf20Sopenharmony_ci	enum intel_gvt_event_type event)
6248c2ecf20Sopenharmony_ci{
6258c2ecf20Sopenharmony_ci	struct drm_i915_private *i915 = vgpu->gvt->gt->i915;
6268c2ecf20Sopenharmony_ci	struct intel_gvt *gvt = vgpu->gvt;
6278c2ecf20Sopenharmony_ci	struct intel_gvt_irq *irq = &gvt->irq;
6288c2ecf20Sopenharmony_ci	gvt_event_virt_handler_t handler;
6298c2ecf20Sopenharmony_ci	struct intel_gvt_irq_ops *ops = gvt->irq.ops;
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	handler = get_event_virt_handler(irq, event);
6328c2ecf20Sopenharmony_ci	drm_WARN_ON(&i915->drm, !handler);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	handler(irq, event, vgpu);
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	ops->check_pending_irq(vgpu);
6378c2ecf20Sopenharmony_ci}
6388c2ecf20Sopenharmony_ci
6398c2ecf20Sopenharmony_cistatic void init_events(
6408c2ecf20Sopenharmony_ci	struct intel_gvt_irq *irq)
6418c2ecf20Sopenharmony_ci{
6428c2ecf20Sopenharmony_ci	int i;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	for (i = 0; i < INTEL_GVT_EVENT_MAX; i++) {
6458c2ecf20Sopenharmony_ci		irq->events[i].info = NULL;
6468c2ecf20Sopenharmony_ci		irq->events[i].v_handler = handle_default_event_virt;
6478c2ecf20Sopenharmony_ci	}
6488c2ecf20Sopenharmony_ci}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_cistatic enum hrtimer_restart vblank_timer_fn(struct hrtimer *data)
6518c2ecf20Sopenharmony_ci{
6528c2ecf20Sopenharmony_ci	struct intel_gvt_vblank_timer *vblank_timer;
6538c2ecf20Sopenharmony_ci	struct intel_gvt_irq *irq;
6548c2ecf20Sopenharmony_ci	struct intel_gvt *gvt;
6558c2ecf20Sopenharmony_ci
6568c2ecf20Sopenharmony_ci	vblank_timer = container_of(data, struct intel_gvt_vblank_timer, timer);
6578c2ecf20Sopenharmony_ci	irq = container_of(vblank_timer, struct intel_gvt_irq, vblank_timer);
6588c2ecf20Sopenharmony_ci	gvt = container_of(irq, struct intel_gvt, irq);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	intel_gvt_request_service(gvt, INTEL_GVT_REQUEST_EMULATE_VBLANK);
6618c2ecf20Sopenharmony_ci	hrtimer_add_expires_ns(&vblank_timer->timer, vblank_timer->period);
6628c2ecf20Sopenharmony_ci	return HRTIMER_RESTART;
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci/**
6668c2ecf20Sopenharmony_ci * intel_gvt_clean_irq - clean up GVT-g IRQ emulation subsystem
6678c2ecf20Sopenharmony_ci * @gvt: a GVT device
6688c2ecf20Sopenharmony_ci *
6698c2ecf20Sopenharmony_ci * This function is called at driver unloading stage, to clean up GVT-g IRQ
6708c2ecf20Sopenharmony_ci * emulation subsystem.
6718c2ecf20Sopenharmony_ci *
6728c2ecf20Sopenharmony_ci */
6738c2ecf20Sopenharmony_civoid intel_gvt_clean_irq(struct intel_gvt *gvt)
6748c2ecf20Sopenharmony_ci{
6758c2ecf20Sopenharmony_ci	struct intel_gvt_irq *irq = &gvt->irq;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	hrtimer_cancel(&irq->vblank_timer.timer);
6788c2ecf20Sopenharmony_ci}
6798c2ecf20Sopenharmony_ci
6808c2ecf20Sopenharmony_ci#define VBLANK_TIMER_PERIOD 16000000
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci/**
6838c2ecf20Sopenharmony_ci * intel_gvt_init_irq - initialize GVT-g IRQ emulation subsystem
6848c2ecf20Sopenharmony_ci * @gvt: a GVT device
6858c2ecf20Sopenharmony_ci *
6868c2ecf20Sopenharmony_ci * This function is called at driver loading stage, to initialize the GVT-g IRQ
6878c2ecf20Sopenharmony_ci * emulation subsystem.
6888c2ecf20Sopenharmony_ci *
6898c2ecf20Sopenharmony_ci * Returns:
6908c2ecf20Sopenharmony_ci * Zero on success, negative error code if failed.
6918c2ecf20Sopenharmony_ci */
6928c2ecf20Sopenharmony_ciint intel_gvt_init_irq(struct intel_gvt *gvt)
6938c2ecf20Sopenharmony_ci{
6948c2ecf20Sopenharmony_ci	struct intel_gvt_irq *irq = &gvt->irq;
6958c2ecf20Sopenharmony_ci	struct intel_gvt_vblank_timer *vblank_timer = &irq->vblank_timer;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	gvt_dbg_core("init irq framework\n");
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	irq->ops = &gen8_irq_ops;
7008c2ecf20Sopenharmony_ci	irq->irq_map = gen8_irq_map;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	/* common event initialization */
7038c2ecf20Sopenharmony_ci	init_events(irq);
7048c2ecf20Sopenharmony_ci
7058c2ecf20Sopenharmony_ci	/* gen specific initialization */
7068c2ecf20Sopenharmony_ci	irq->ops->init_irq(irq);
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	init_irq_map(irq);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	hrtimer_init(&vblank_timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
7118c2ecf20Sopenharmony_ci	vblank_timer->timer.function = vblank_timer_fn;
7128c2ecf20Sopenharmony_ci	vblank_timer->period = VBLANK_TIMER_PERIOD;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	return 0;
7158c2ecf20Sopenharmony_ci}
716