gvt.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Kevin Tian <kevin.tian@intel.com>
  25. * Eddie Dong <eddie.dong@intel.com>
  26. *
  27. * Contributors:
  28. * Niu Bing <bing.niu@intel.com>
  29. * Zhi Wang <zhi.a.wang@intel.com>
  30. *
  31. */
  32. #ifndef _GVT_H_
  33. #define _GVT_H_
  34. #include <uapi/linux/pci_regs.h>
  35. #include <linux/mdev.h>
  36. #include <linux/vfio.h>
  37. #include <asm/kvm_page_track.h>
  38. #include "display/intel_display_limits.h"
  39. #include "gt/intel_gt.h"
  40. #include "cmd_parser.h"
  41. #include "debug.h"
  42. #include "display.h"
  43. #include "dmabuf.h"
  44. #include "edid.h"
  45. #include "execlist.h"
  46. #include "fb_decoder.h"
  47. #include "gtt.h"
  48. #include "intel_gvt.h"
  49. #include "interrupt.h"
  50. #include "mmio.h"
  51. #include "mmio_context.h"
  52. #include "page_track.h"
  53. #include "reg.h"
  54. #include "scheduler.h"
  55. #define GVT_MAX_VGPU 8
  56. struct engine_mmio;
  57. /* Describe per-platform limitations. */
  58. struct intel_gvt_device_info {
  59. u32 max_support_vgpus;
  60. u32 cfg_space_size;
  61. u32 mmio_size;
  62. u32 mmio_bar;
  63. unsigned long msi_cap_offset;
  64. u32 gtt_start_offset;
  65. u32 gtt_entry_size;
  66. u32 gtt_entry_size_shift;
  67. int gmadr_bytes_in_cmd;
  68. u32 max_surface_size;
  69. };
  70. /* GM resources owned by a vGPU */
  71. struct intel_vgpu_gm {
  72. u64 aperture_sz;
  73. u64 hidden_sz;
  74. struct drm_mm_node low_gm_node;
  75. struct drm_mm_node high_gm_node;
  76. };
  77. #define INTEL_GVT_MAX_NUM_FENCES 32
  78. /* Fences owned by a vGPU */
  79. struct intel_vgpu_fence {
  80. struct i915_fence_reg *regs[INTEL_GVT_MAX_NUM_FENCES];
  81. u32 size;
  82. };
  83. struct intel_vgpu_mmio {
  84. void *vreg;
  85. };
  86. #define INTEL_GVT_MAX_BAR_NUM 4
  87. struct intel_vgpu_pci_bar {
  88. u64 size;
  89. bool tracked;
  90. };
  91. struct intel_vgpu_cfg_space {
  92. unsigned char virtual_cfg_space[PCI_CFG_SPACE_EXP_SIZE];
  93. struct intel_vgpu_pci_bar bar[INTEL_GVT_MAX_BAR_NUM];
  94. u32 pmcsr_off;
  95. };
  96. #define vgpu_cfg_space(vgpu) ((vgpu)->cfg_space.virtual_cfg_space)
  97. struct intel_vgpu_irq {
  98. bool irq_warn_once[INTEL_GVT_EVENT_MAX];
  99. DECLARE_BITMAP(flip_done_event[I915_MAX_PIPES],
  100. INTEL_GVT_EVENT_MAX);
  101. };
  102. struct intel_vgpu_opregion {
  103. void *va;
  104. u32 gfn[INTEL_GVT_OPREGION_PAGES];
  105. };
  106. #define vgpu_opregion(vgpu) (&(vgpu->opregion))
  107. struct intel_vgpu_display {
  108. struct intel_vgpu_i2c_edid i2c_edid;
  109. struct intel_vgpu_port ports[I915_MAX_PORTS];
  110. struct intel_vgpu_sbi sbi;
  111. enum port port_num;
  112. };
  113. struct vgpu_sched_ctl {
  114. int weight;
  115. };
  116. enum {
  117. INTEL_VGPU_EXECLIST_SUBMISSION = 1,
  118. INTEL_VGPU_GUC_SUBMISSION,
  119. };
  120. struct intel_vgpu_submission_ops {
  121. const char *name;
  122. int (*init)(struct intel_vgpu *vgpu, intel_engine_mask_t engine_mask);
  123. void (*clean)(struct intel_vgpu *vgpu, intel_engine_mask_t engine_mask);
  124. void (*reset)(struct intel_vgpu *vgpu, intel_engine_mask_t engine_mask);
  125. };
  126. struct intel_vgpu_submission {
  127. struct intel_vgpu_execlist execlist[I915_NUM_ENGINES];
  128. struct list_head workload_q_head[I915_NUM_ENGINES];
  129. struct intel_context *shadow[I915_NUM_ENGINES];
  130. struct kmem_cache *workloads;
  131. atomic_t running_workload_num;
  132. union {
  133. u64 i915_context_pml4;
  134. u64 i915_context_pdps[GEN8_3LVL_PDPES];
  135. };
  136. DECLARE_BITMAP(shadow_ctx_desc_updated, I915_NUM_ENGINES);
  137. DECLARE_BITMAP(tlb_handle_pending, I915_NUM_ENGINES);
  138. void *ring_scan_buffer[I915_NUM_ENGINES];
  139. int ring_scan_buffer_size[I915_NUM_ENGINES];
  140. const struct intel_vgpu_submission_ops *ops;
  141. int virtual_submission_interface;
  142. bool active;
  143. struct {
  144. u32 lrca;
  145. bool valid;
  146. u64 ring_context_gpa;
  147. } last_ctx[I915_NUM_ENGINES];
  148. };
  149. #define KVMGT_DEBUGFS_FILENAME "kvmgt_nr_cache_entries"
  150. enum {
  151. INTEL_VGPU_STATUS_ATTACHED = 0,
  152. INTEL_VGPU_STATUS_ACTIVE,
  153. INTEL_VGPU_STATUS_NR_BITS,
  154. };
  155. struct intel_vgpu {
  156. struct vfio_device vfio_device;
  157. struct intel_gvt *gvt;
  158. struct mutex vgpu_lock;
  159. int id;
  160. DECLARE_BITMAP(status, INTEL_VGPU_STATUS_NR_BITS);
  161. bool pv_notified;
  162. bool failsafe;
  163. unsigned int resetting_eng;
  164. /* Both sched_data and sched_ctl can be seen a part of the global gvt
  165. * scheduler structure. So below 2 vgpu data are protected
  166. * by sched_lock, not vgpu_lock.
  167. */
  168. void *sched_data;
  169. struct vgpu_sched_ctl sched_ctl;
  170. struct intel_vgpu_fence fence;
  171. struct intel_vgpu_gm gm;
  172. struct intel_vgpu_cfg_space cfg_space;
  173. struct intel_vgpu_mmio mmio;
  174. struct intel_vgpu_irq irq;
  175. struct intel_vgpu_gtt gtt;
  176. struct intel_vgpu_opregion opregion;
  177. struct intel_vgpu_display display;
  178. struct intel_vgpu_submission submission;
  179. struct radix_tree_root page_track_tree;
  180. u32 hws_pga[I915_NUM_ENGINES];
  181. /* Set on PCI_D3, reset on DMLR, not reflecting the actual PM state */
  182. bool d3_entered;
  183. struct dentry *debugfs;
  184. struct list_head dmabuf_obj_list_head;
  185. struct mutex dmabuf_lock;
  186. struct idr object_idr;
  187. struct intel_vgpu_vblank_timer vblank_timer;
  188. u32 scan_nonprivbb;
  189. struct vfio_region *region;
  190. int num_regions;
  191. struct eventfd_ctx *msi_trigger;
  192. /*
  193. * Two caches are used to avoid mapping duplicated pages (eg.
  194. * scratch pages). This help to reduce dma setup overhead.
  195. */
  196. struct rb_root gfn_cache;
  197. struct rb_root dma_addr_cache;
  198. unsigned long nr_cache_entries;
  199. struct mutex cache_lock;
  200. struct kvm_page_track_notifier_node track_node;
  201. #define NR_BKT (1 << 18)
  202. struct hlist_head ptable[NR_BKT];
  203. #undef NR_BKT
  204. };
  205. /* validating GM healthy status*/
  206. #define vgpu_is_vm_unhealthy(ret_val) \
  207. (((ret_val) == -EBADRQC) || ((ret_val) == -EFAULT))
  208. struct intel_gvt_gm {
  209. unsigned long vgpu_allocated_low_gm_size;
  210. unsigned long vgpu_allocated_high_gm_size;
  211. };
  212. struct intel_gvt_fence {
  213. unsigned long vgpu_allocated_fence_num;
  214. };
  215. /* Special MMIO blocks. */
  216. struct gvt_mmio_block {
  217. i915_reg_t offset;
  218. unsigned int size;
  219. gvt_mmio_func read;
  220. gvt_mmio_func write;
  221. };
  222. #define INTEL_GVT_MMIO_HASH_BITS 11
  223. struct intel_gvt_mmio {
  224. u16 *mmio_attribute;
  225. /* Register contains RO bits */
  226. #define F_RO (1 << 0)
  227. /* Register contains graphics address */
  228. #define F_GMADR (1 << 1)
  229. /* Mode mask registers with high 16 bits as the mask bits */
  230. #define F_MODE_MASK (1 << 2)
  231. /* This reg can be accessed by GPU commands */
  232. #define F_CMD_ACCESS (1 << 3)
  233. /* This reg has been accessed by a VM */
  234. #define F_ACCESSED (1 << 4)
  235. /* This reg requires save & restore during host PM suspend/resume */
  236. #define F_PM_SAVE (1 << 5)
  237. /* This reg could be accessed by unaligned address */
  238. #define F_UNALIGN (1 << 6)
  239. /* This reg is in GVT's mmio save-restor list and in hardware
  240. * logical context image
  241. */
  242. #define F_SR_IN_CTX (1 << 7)
  243. /* Value of command write of this reg needs to be patched */
  244. #define F_CMD_WRITE_PATCH (1 << 8)
  245. struct gvt_mmio_block *mmio_block;
  246. unsigned int num_mmio_block;
  247. DECLARE_HASHTABLE(mmio_info_table, INTEL_GVT_MMIO_HASH_BITS);
  248. unsigned long num_tracked_mmio;
  249. };
  250. struct intel_gvt_firmware {
  251. void *cfg_space;
  252. void *mmio;
  253. bool firmware_loaded;
  254. };
  255. struct intel_vgpu_config {
  256. unsigned int low_mm;
  257. unsigned int high_mm;
  258. unsigned int fence;
  259. /*
  260. * A vGPU with a weight of 8 will get twice as much GPU as a vGPU with
  261. * a weight of 4 on a contended host, different vGPU type has different
  262. * weight set. Legal weights range from 1 to 16.
  263. */
  264. unsigned int weight;
  265. enum intel_vgpu_edid edid;
  266. const char *name;
  267. };
  268. struct intel_vgpu_type {
  269. struct mdev_type type;
  270. char name[16];
  271. const struct intel_vgpu_config *conf;
  272. };
  273. struct intel_gvt {
  274. /* GVT scope lock, protect GVT itself, and all resource currently
  275. * not yet protected by special locks(vgpu and scheduler lock).
  276. */
  277. struct mutex lock;
  278. /* scheduler scope lock, protect gvt and vgpu schedule related data */
  279. struct mutex sched_lock;
  280. struct intel_gt *gt;
  281. struct idr vgpu_idr; /* vGPU IDR pool */
  282. struct intel_gvt_device_info device_info;
  283. struct intel_gvt_gm gm;
  284. struct intel_gvt_fence fence;
  285. struct intel_gvt_mmio mmio;
  286. struct intel_gvt_firmware firmware;
  287. struct intel_gvt_irq irq;
  288. struct intel_gvt_gtt gtt;
  289. struct intel_gvt_workload_scheduler scheduler;
  290. struct notifier_block shadow_ctx_notifier_block[I915_NUM_ENGINES];
  291. DECLARE_HASHTABLE(cmd_table, GVT_CMD_HASH_BITS);
  292. struct mdev_parent parent;
  293. struct mdev_type **mdev_types;
  294. struct intel_vgpu_type *types;
  295. unsigned int num_types;
  296. struct intel_vgpu *idle_vgpu;
  297. struct task_struct *service_thread;
  298. wait_queue_head_t service_thread_wq;
  299. /* service_request is always used in bit operation, we should always
  300. * use it with atomic bit ops so that no need to use gvt big lock.
  301. */
  302. unsigned long service_request;
  303. struct {
  304. struct engine_mmio *mmio;
  305. int ctx_mmio_count[I915_NUM_ENGINES];
  306. u32 *tlb_mmio_offset_list;
  307. u32 tlb_mmio_offset_list_cnt;
  308. u32 *mocs_mmio_offset_list;
  309. u32 mocs_mmio_offset_list_cnt;
  310. } engine_mmio_list;
  311. bool is_reg_whitelist_updated;
  312. struct dentry *debugfs_root;
  313. };
  314. enum {
  315. /* Scheduling trigger by timer */
  316. INTEL_GVT_REQUEST_SCHED = 0,
  317. /* Scheduling trigger by event */
  318. INTEL_GVT_REQUEST_EVENT_SCHED = 1,
  319. /* per-vGPU vblank emulation request */
  320. INTEL_GVT_REQUEST_EMULATE_VBLANK = 2,
  321. INTEL_GVT_REQUEST_EMULATE_VBLANK_MAX = INTEL_GVT_REQUEST_EMULATE_VBLANK
  322. + GVT_MAX_VGPU,
  323. };
  324. static inline void intel_gvt_request_service(struct intel_gvt *gvt,
  325. int service)
  326. {
  327. set_bit(service, (void *)&gvt->service_request);
  328. wake_up(&gvt->service_thread_wq);
  329. }
  330. void intel_gvt_free_firmware(struct intel_gvt *gvt);
  331. int intel_gvt_load_firmware(struct intel_gvt *gvt);
  332. /* Aperture/GM space definitions for GVT device */
  333. #define MB_TO_BYTES(mb) ((mb) << 20ULL)
  334. #define BYTES_TO_MB(b) ((b) >> 20ULL)
  335. #define HOST_LOW_GM_SIZE MB_TO_BYTES(128)
  336. #define HOST_HIGH_GM_SIZE MB_TO_BYTES(384)
  337. #define HOST_FENCE 4
  338. #define gvt_to_ggtt(gvt) ((gvt)->gt->ggtt)
  339. /* Aperture/GM space definitions for GVT device */
  340. #define gvt_aperture_sz(gvt) gvt_to_ggtt(gvt)->mappable_end
  341. #define gvt_aperture_pa_base(gvt) gvt_to_ggtt(gvt)->gmadr.start
  342. #define gvt_ggtt_gm_sz(gvt) gvt_to_ggtt(gvt)->vm.total
  343. #define gvt_ggtt_sz(gvt) (gvt_to_ggtt(gvt)->vm.total >> PAGE_SHIFT << 3)
  344. #define gvt_hidden_sz(gvt) (gvt_ggtt_gm_sz(gvt) - gvt_aperture_sz(gvt))
  345. #define gvt_aperture_gmadr_base(gvt) (0)
  346. #define gvt_aperture_gmadr_end(gvt) (gvt_aperture_gmadr_base(gvt) \
  347. + gvt_aperture_sz(gvt) - 1)
  348. #define gvt_hidden_gmadr_base(gvt) (gvt_aperture_gmadr_base(gvt) \
  349. + gvt_aperture_sz(gvt))
  350. #define gvt_hidden_gmadr_end(gvt) (gvt_hidden_gmadr_base(gvt) \
  351. + gvt_hidden_sz(gvt) - 1)
  352. #define gvt_fence_sz(gvt) (gvt_to_ggtt(gvt)->num_fences)
  353. /* Aperture/GM space definitions for vGPU */
  354. #define vgpu_aperture_offset(vgpu) ((vgpu)->gm.low_gm_node.start)
  355. #define vgpu_hidden_offset(vgpu) ((vgpu)->gm.high_gm_node.start)
  356. #define vgpu_aperture_sz(vgpu) ((vgpu)->gm.aperture_sz)
  357. #define vgpu_hidden_sz(vgpu) ((vgpu)->gm.hidden_sz)
  358. #define vgpu_aperture_pa_base(vgpu) \
  359. (gvt_aperture_pa_base(vgpu->gvt) + vgpu_aperture_offset(vgpu))
  360. #define vgpu_ggtt_gm_sz(vgpu) ((vgpu)->gm.aperture_sz + (vgpu)->gm.hidden_sz)
  361. #define vgpu_aperture_pa_end(vgpu) \
  362. (vgpu_aperture_pa_base(vgpu) + vgpu_aperture_sz(vgpu) - 1)
  363. #define vgpu_aperture_gmadr_base(vgpu) (vgpu_aperture_offset(vgpu))
  364. #define vgpu_aperture_gmadr_end(vgpu) \
  365. (vgpu_aperture_gmadr_base(vgpu) + vgpu_aperture_sz(vgpu) - 1)
  366. #define vgpu_hidden_gmadr_base(vgpu) (vgpu_hidden_offset(vgpu))
  367. #define vgpu_hidden_gmadr_end(vgpu) \
  368. (vgpu_hidden_gmadr_base(vgpu) + vgpu_hidden_sz(vgpu) - 1)
  369. #define vgpu_fence_sz(vgpu) (vgpu->fence.size)
  370. /* ring context size i.e. the first 0x50 dwords*/
  371. #define RING_CTX_SIZE 320
  372. int intel_vgpu_alloc_resource(struct intel_vgpu *vgpu,
  373. const struct intel_vgpu_config *conf);
  374. void intel_vgpu_reset_resource(struct intel_vgpu *vgpu);
  375. void intel_vgpu_free_resource(struct intel_vgpu *vgpu);
  376. void intel_vgpu_write_fence(struct intel_vgpu *vgpu,
  377. u32 fence, u64 value);
  378. /*
  379. * Macros for easily accessing vGPU virtual/shadow register.
  380. * Explicitly separate use for typed MMIO reg or real offset.
  381. */
  382. #define vgpu_vreg_t(vgpu, reg) \
  383. (*(u32 *)(vgpu->mmio.vreg + i915_mmio_reg_offset(reg)))
  384. #define vgpu_vreg(vgpu, offset) \
  385. (*(u32 *)(vgpu->mmio.vreg + (offset)))
  386. #define vgpu_vreg64_t(vgpu, reg) \
  387. (*(u64 *)(vgpu->mmio.vreg + i915_mmio_reg_offset(reg)))
  388. #define vgpu_vreg64(vgpu, offset) \
  389. (*(u64 *)(vgpu->mmio.vreg + (offset)))
  390. #define for_each_active_vgpu(gvt, vgpu, id) \
  391. idr_for_each_entry((&(gvt)->vgpu_idr), (vgpu), (id)) \
  392. for_each_if(test_bit(INTEL_VGPU_STATUS_ACTIVE, vgpu->status))
  393. static inline void intel_vgpu_write_pci_bar(struct intel_vgpu *vgpu,
  394. u32 offset, u32 val, bool low)
  395. {
  396. u32 *pval;
  397. /* BAR offset should be 32 bits algiend */
  398. offset = rounddown(offset, 4);
  399. pval = (u32 *)(vgpu_cfg_space(vgpu) + offset);
  400. if (low) {
  401. /*
  402. * only update bit 31 - bit 4,
  403. * leave the bit 3 - bit 0 unchanged.
  404. */
  405. *pval = (val & GENMASK(31, 4)) | (*pval & GENMASK(3, 0));
  406. } else {
  407. *pval = val;
  408. }
  409. }
  410. int intel_gvt_init_vgpu_types(struct intel_gvt *gvt);
  411. void intel_gvt_clean_vgpu_types(struct intel_gvt *gvt);
  412. struct intel_vgpu *intel_gvt_create_idle_vgpu(struct intel_gvt *gvt);
  413. void intel_gvt_destroy_idle_vgpu(struct intel_vgpu *vgpu);
  414. int intel_gvt_create_vgpu(struct intel_vgpu *vgpu,
  415. const struct intel_vgpu_config *conf);
  416. void intel_gvt_destroy_vgpu(struct intel_vgpu *vgpu);
  417. void intel_gvt_release_vgpu(struct intel_vgpu *vgpu);
  418. void intel_gvt_reset_vgpu_locked(struct intel_vgpu *vgpu, bool dmlr,
  419. intel_engine_mask_t engine_mask);
  420. void intel_gvt_reset_vgpu(struct intel_vgpu *vgpu);
  421. void intel_gvt_activate_vgpu(struct intel_vgpu *vgpu);
  422. void intel_gvt_deactivate_vgpu(struct intel_vgpu *vgpu);
  423. int intel_gvt_set_opregion(struct intel_vgpu *vgpu);
  424. int intel_gvt_set_edid(struct intel_vgpu *vgpu, int port_num);
  425. /* validating GM functions */
  426. #define vgpu_gmadr_is_aperture(vgpu, gmadr) \
  427. ((gmadr >= vgpu_aperture_gmadr_base(vgpu)) && \
  428. (gmadr <= vgpu_aperture_gmadr_end(vgpu)))
  429. #define vgpu_gmadr_is_hidden(vgpu, gmadr) \
  430. ((gmadr >= vgpu_hidden_gmadr_base(vgpu)) && \
  431. (gmadr <= vgpu_hidden_gmadr_end(vgpu)))
  432. #define vgpu_gmadr_is_valid(vgpu, gmadr) \
  433. ((vgpu_gmadr_is_aperture(vgpu, gmadr) || \
  434. (vgpu_gmadr_is_hidden(vgpu, gmadr))))
  435. #define gvt_gmadr_is_aperture(gvt, gmadr) \
  436. ((gmadr >= gvt_aperture_gmadr_base(gvt)) && \
  437. (gmadr <= gvt_aperture_gmadr_end(gvt)))
  438. #define gvt_gmadr_is_hidden(gvt, gmadr) \
  439. ((gmadr >= gvt_hidden_gmadr_base(gvt)) && \
  440. (gmadr <= gvt_hidden_gmadr_end(gvt)))
  441. #define gvt_gmadr_is_valid(gvt, gmadr) \
  442. (gvt_gmadr_is_aperture(gvt, gmadr) || \
  443. gvt_gmadr_is_hidden(gvt, gmadr))
  444. bool intel_gvt_ggtt_validate_range(struct intel_vgpu *vgpu, u64 addr, u32 size);
  445. void intel_vgpu_init_cfg_space(struct intel_vgpu *vgpu,
  446. bool primary);
  447. void intel_vgpu_reset_cfg_space(struct intel_vgpu *vgpu);
  448. int intel_vgpu_emulate_cfg_read(struct intel_vgpu *vgpu, unsigned int offset,
  449. void *p_data, unsigned int bytes);
  450. int intel_vgpu_emulate_cfg_write(struct intel_vgpu *vgpu, unsigned int offset,
  451. void *p_data, unsigned int bytes);
  452. void intel_vgpu_emulate_hotplug(struct intel_vgpu *vgpu, bool connected);
  453. static inline u64 intel_vgpu_get_bar_gpa(struct intel_vgpu *vgpu, int bar)
  454. {
  455. /* We are 64bit bar. */
  456. return (*(u64 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
  457. PCI_BASE_ADDRESS_MEM_MASK;
  458. }
  459. void intel_vgpu_clean_opregion(struct intel_vgpu *vgpu);
  460. int intel_vgpu_init_opregion(struct intel_vgpu *vgpu);
  461. int intel_vgpu_opregion_base_write_handler(struct intel_vgpu *vgpu, u32 gpa);
  462. int intel_vgpu_emulate_opregion_request(struct intel_vgpu *vgpu, u32 swsci);
  463. void populate_pvinfo_page(struct intel_vgpu *vgpu);
  464. int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload);
  465. void enter_failsafe_mode(struct intel_vgpu *vgpu, int reason);
  466. void intel_vgpu_detach_regions(struct intel_vgpu *vgpu);
  467. enum {
  468. GVT_FAILSAFE_UNSUPPORTED_GUEST,
  469. GVT_FAILSAFE_INSUFFICIENT_RESOURCE,
  470. GVT_FAILSAFE_GUEST_ERR,
  471. };
  472. static inline intel_wakeref_t mmio_hw_access_pre(struct intel_gt *gt)
  473. {
  474. return intel_runtime_pm_get(gt->uncore->rpm);
  475. }
  476. static inline void mmio_hw_access_post(struct intel_gt *gt,
  477. intel_wakeref_t wakeref)
  478. {
  479. intel_runtime_pm_put(gt->uncore->rpm, wakeref);
  480. }
  481. /**
  482. * intel_gvt_mmio_set_accessed - mark a MMIO has been accessed
  483. * @gvt: a GVT device
  484. * @offset: register offset
  485. *
  486. */
  487. static inline void intel_gvt_mmio_set_accessed(
  488. struct intel_gvt *gvt, unsigned int offset)
  489. {
  490. gvt->mmio.mmio_attribute[offset >> 2] |= F_ACCESSED;
  491. }
  492. /**
  493. * intel_gvt_mmio_is_cmd_accessible - if a MMIO could be accessed by command
  494. * @gvt: a GVT device
  495. * @offset: register offset
  496. *
  497. * Returns:
  498. * True if an MMIO is able to be accessed by GPU commands
  499. */
  500. static inline bool intel_gvt_mmio_is_cmd_accessible(
  501. struct intel_gvt *gvt, unsigned int offset)
  502. {
  503. return gvt->mmio.mmio_attribute[offset >> 2] & F_CMD_ACCESS;
  504. }
  505. /**
  506. * intel_gvt_mmio_set_cmd_accessible -
  507. * mark a MMIO could be accessible by command
  508. * @gvt: a GVT device
  509. * @offset: register offset
  510. *
  511. */
  512. static inline void intel_gvt_mmio_set_cmd_accessible(
  513. struct intel_gvt *gvt, unsigned int offset)
  514. {
  515. gvt->mmio.mmio_attribute[offset >> 2] |= F_CMD_ACCESS;
  516. }
  517. /**
  518. * intel_gvt_mmio_is_unalign - mark a MMIO could be accessed unaligned
  519. * @gvt: a GVT device
  520. * @offset: register offset
  521. *
  522. */
  523. static inline bool intel_gvt_mmio_is_unalign(
  524. struct intel_gvt *gvt, unsigned int offset)
  525. {
  526. return gvt->mmio.mmio_attribute[offset >> 2] & F_UNALIGN;
  527. }
  528. /**
  529. * intel_gvt_mmio_has_mode_mask - if a MMIO has a mode mask
  530. * @gvt: a GVT device
  531. * @offset: register offset
  532. *
  533. * Returns:
  534. * True if a MMIO has a mode mask in its higher 16 bits, false if it isn't.
  535. *
  536. */
  537. static inline bool intel_gvt_mmio_has_mode_mask(
  538. struct intel_gvt *gvt, unsigned int offset)
  539. {
  540. return gvt->mmio.mmio_attribute[offset >> 2] & F_MODE_MASK;
  541. }
  542. /**
  543. * intel_gvt_mmio_is_sr_in_ctx -
  544. * check if an MMIO has F_SR_IN_CTX mask
  545. * @gvt: a GVT device
  546. * @offset: register offset
  547. *
  548. * Returns:
  549. * True if an MMIO has an F_SR_IN_CTX mask, false if it isn't.
  550. *
  551. */
  552. static inline bool intel_gvt_mmio_is_sr_in_ctx(
  553. struct intel_gvt *gvt, unsigned int offset)
  554. {
  555. return gvt->mmio.mmio_attribute[offset >> 2] & F_SR_IN_CTX;
  556. }
  557. /**
  558. * intel_gvt_mmio_set_sr_in_ctx -
  559. * mask an MMIO in GVT's mmio save-restore list and also
  560. * in hardware logical context image
  561. * @gvt: a GVT device
  562. * @offset: register offset
  563. *
  564. */
  565. static inline void intel_gvt_mmio_set_sr_in_ctx(
  566. struct intel_gvt *gvt, unsigned int offset)
  567. {
  568. gvt->mmio.mmio_attribute[offset >> 2] |= F_SR_IN_CTX;
  569. }
  570. void intel_gvt_debugfs_add_vgpu(struct intel_vgpu *vgpu);
  571. /**
  572. * intel_gvt_mmio_set_cmd_write_patch -
  573. * mark an MMIO if its cmd write needs to be
  574. * patched
  575. * @gvt: a GVT device
  576. * @offset: register offset
  577. *
  578. */
  579. static inline void intel_gvt_mmio_set_cmd_write_patch(
  580. struct intel_gvt *gvt, unsigned int offset)
  581. {
  582. gvt->mmio.mmio_attribute[offset >> 2] |= F_CMD_WRITE_PATCH;
  583. }
  584. /**
  585. * intel_gvt_mmio_is_cmd_write_patch - check if an mmio's cmd access needs to
  586. * be patched
  587. * @gvt: a GVT device
  588. * @offset: register offset
  589. *
  590. * Returns:
  591. * True if GPU command write to an MMIO should be patched.
  592. */
  593. static inline bool intel_gvt_mmio_is_cmd_write_patch(
  594. struct intel_gvt *gvt, unsigned int offset)
  595. {
  596. return gvt->mmio.mmio_attribute[offset >> 2] & F_CMD_WRITE_PATCH;
  597. }
  598. /**
  599. * intel_gvt_read_gpa - copy data from GPA to host data buffer
  600. * @vgpu: a vGPU
  601. * @gpa: guest physical address
  602. * @buf: host data buffer
  603. * @len: data length
  604. *
  605. * Returns:
  606. * Zero on success, negative error code if failed.
  607. */
  608. static inline int intel_gvt_read_gpa(struct intel_vgpu *vgpu, unsigned long gpa,
  609. void *buf, unsigned long len)
  610. {
  611. if (!test_bit(INTEL_VGPU_STATUS_ATTACHED, vgpu->status))
  612. return -ESRCH;
  613. return vfio_dma_rw(&vgpu->vfio_device, gpa, buf, len, false);
  614. }
  615. /**
  616. * intel_gvt_write_gpa - copy data from host data buffer to GPA
  617. * @vgpu: a vGPU
  618. * @gpa: guest physical address
  619. * @buf: host data buffer
  620. * @len: data length
  621. *
  622. * Returns:
  623. * Zero on success, negative error code if failed.
  624. */
  625. static inline int intel_gvt_write_gpa(struct intel_vgpu *vgpu,
  626. unsigned long gpa, void *buf, unsigned long len)
  627. {
  628. if (!test_bit(INTEL_VGPU_STATUS_ATTACHED, vgpu->status))
  629. return -ESRCH;
  630. return vfio_dma_rw(&vgpu->vfio_device, gpa, buf, len, true);
  631. }
  632. void intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu);
  633. void intel_gvt_debugfs_init(struct intel_gvt *gvt);
  634. void intel_gvt_debugfs_clean(struct intel_gvt *gvt);
  635. int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn);
  636. int intel_gvt_page_track_remove(struct intel_vgpu *info, u64 gfn);
  637. int intel_gvt_dma_pin_guest_page(struct intel_vgpu *vgpu, dma_addr_t dma_addr);
  638. int intel_gvt_dma_map_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
  639. unsigned long size, dma_addr_t *dma_addr);
  640. void intel_gvt_dma_unmap_guest_page(struct intel_vgpu *vgpu,
  641. dma_addr_t dma_addr);
  642. #include "trace.h"
  643. #endif