gvt.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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 <[email protected]>
  25. * Eddie Dong <[email protected]>
  26. *
  27. * Contributors:
  28. * Niu Bing <[email protected]>
  29. * Zhi Wang <[email protected]>
  30. *
  31. */
  32. #ifndef _GVT_H_
  33. #define _GVT_H_
  34. #include <uapi/linux/pci_regs.h>
  35. #include <linux/kvm_host.h>
  36. #include <linux/vfio.h>
  37. #include <linux/mdev.h>
  38. #include "i915_drv.h"
  39. #include "intel_gvt.h"
  40. #include "debug.h"
  41. #include "mmio.h"
  42. #include "reg.h"
  43. #include "interrupt.h"
  44. #include "gtt.h"
  45. #include "display.h"
  46. #include "edid.h"
  47. #include "execlist.h"
  48. #include "scheduler.h"
  49. #include "sched_policy.h"
  50. #include "mmio_context.h"
  51. #include "cmd_parser.h"
  52. #include "fb_decoder.h"
  53. #include "dmabuf.h"
  54. #include "page_track.h"
  55. #define GVT_MAX_VGPU 8
  56. /* Describe per-platform limitations. */
  57. struct intel_gvt_device_info {
  58. u32 max_support_vgpus;
  59. u32 cfg_space_size;
  60. u32 mmio_size;
  61. u32 mmio_bar;
  62. unsigned long msi_cap_offset;
  63. u32 gtt_start_offset;
  64. u32 gtt_entry_size;
  65. u32 gtt_entry_size_shift;
  66. int gmadr_bytes_in_cmd;
  67. u32 max_surface_size;
  68. };
  69. /* GM resources owned by a vGPU */
  70. struct intel_vgpu_gm {
  71. u64 aperture_sz;
  72. u64 hidden_sz;
  73. struct drm_mm_node low_gm_node;
  74. struct drm_mm_node high_gm_node;
  75. };
  76. #define INTEL_GVT_MAX_NUM_FENCES 32
  77. /* Fences owned by a vGPU */
  78. struct intel_vgpu_fence {
  79. struct i915_fence_reg *regs[INTEL_GVT_MAX_NUM_FENCES];
  80. u32 base;
  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. bool mapped;
  104. void *va;
  105. u32 gfn[INTEL_GVT_OPREGION_PAGES];
  106. };
  107. #define vgpu_opregion(vgpu) (&(vgpu->opregion))
  108. struct intel_vgpu_display {
  109. struct intel_vgpu_i2c_edid i2c_edid;
  110. struct intel_vgpu_port ports[I915_MAX_PORTS];
  111. struct intel_vgpu_sbi sbi;
  112. enum port port_num;
  113. };
  114. struct vgpu_sched_ctl {
  115. int weight;
  116. };
  117. enum {
  118. INTEL_VGPU_EXECLIST_SUBMISSION = 1,
  119. INTEL_VGPU_GUC_SUBMISSION,
  120. };
  121. struct intel_vgpu_submission_ops {
  122. const char *name;
  123. int (*init)(struct intel_vgpu *vgpu, intel_engine_mask_t engine_mask);
  124. void (*clean)(struct intel_vgpu *vgpu, intel_engine_mask_t engine_mask);
  125. void (*reset)(struct intel_vgpu *vgpu, intel_engine_mask_t engine_mask);
  126. };
  127. struct intel_vgpu_submission {
  128. struct intel_vgpu_execlist execlist[I915_NUM_ENGINES];
  129. struct list_head workload_q_head[I915_NUM_ENGINES];
  130. struct intel_context *shadow[I915_NUM_ENGINES];
  131. struct kmem_cache *workloads;
  132. atomic_t running_workload_num;
  133. union {
  134. u64 i915_context_pml4;
  135. u64 i915_context_pdps[GEN8_3LVL_PDPES];
  136. };
  137. DECLARE_BITMAP(shadow_ctx_desc_updated, I915_NUM_ENGINES);
  138. DECLARE_BITMAP(tlb_handle_pending, I915_NUM_ENGINES);
  139. void *ring_scan_buffer[I915_NUM_ENGINES];
  140. int ring_scan_buffer_size[I915_NUM_ENGINES];
  141. const struct intel_vgpu_submission_ops *ops;
  142. int virtual_submission_interface;
  143. bool active;
  144. struct {
  145. u32 lrca;
  146. bool valid;
  147. u64 ring_context_gpa;
  148. } last_ctx[I915_NUM_ENGINES];
  149. };
  150. #define KVMGT_DEBUGFS_FILENAME "kvmgt_nr_cache_entries"
  151. struct intel_vgpu {
  152. struct vfio_device vfio_device;
  153. struct intel_gvt *gvt;
  154. struct mutex vgpu_lock;
  155. int id;
  156. bool active;
  157. bool attached;
  158. bool pv_notified;
  159. bool failsafe;
  160. unsigned int resetting_eng;
  161. /* Both sched_data and sched_ctl can be seen a part of the global gvt
  162. * scheduler structure. So below 2 vgpu data are protected
  163. * by sched_lock, not vgpu_lock.
  164. */
  165. void *sched_data;
  166. struct vgpu_sched_ctl sched_ctl;
  167. struct intel_vgpu_fence fence;
  168. struct intel_vgpu_gm gm;
  169. struct intel_vgpu_cfg_space cfg_space;
  170. struct intel_vgpu_mmio mmio;
  171. struct intel_vgpu_irq irq;
  172. struct intel_vgpu_gtt gtt;
  173. struct intel_vgpu_opregion opregion;
  174. struct intel_vgpu_display display;
  175. struct intel_vgpu_submission submission;
  176. struct radix_tree_root page_track_tree;
  177. u32 hws_pga[I915_NUM_ENGINES];
  178. /* Set on PCI_D3, reset on DMLR, not reflecting the actual PM state */
  179. bool d3_entered;
  180. struct dentry *debugfs;
  181. struct list_head dmabuf_obj_list_head;
  182. struct mutex dmabuf_lock;
  183. struct idr object_idr;
  184. struct intel_vgpu_vblank_timer vblank_timer;
  185. u32 scan_nonprivbb;
  186. struct vfio_region *region;
  187. int num_regions;
  188. struct eventfd_ctx *intx_trigger;
  189. struct eventfd_ctx *msi_trigger;
  190. /*
  191. * Two caches are used to avoid mapping duplicated pages (eg.
  192. * scratch pages). This help to reduce dma setup overhead.
  193. */
  194. struct rb_root gfn_cache;
  195. struct rb_root dma_addr_cache;
  196. unsigned long nr_cache_entries;
  197. struct mutex cache_lock;
  198. atomic_t released;
  199. struct kvm_page_track_notifier_node track_node;
  200. #define NR_BKT (1 << 18)
  201. struct hlist_head ptable[NR_BKT];
  202. #undef NR_BKT
  203. };
  204. /* validating GM healthy status*/
  205. #define vgpu_is_vm_unhealthy(ret_val) \
  206. (((ret_val) == -EBADRQC) || ((ret_val) == -EFAULT))
  207. struct intel_gvt_gm {
  208. unsigned long vgpu_allocated_low_gm_size;
  209. unsigned long vgpu_allocated_high_gm_size;
  210. };
  211. struct intel_gvt_fence {
  212. unsigned long vgpu_allocated_fence_num;
  213. };
  214. /* Special MMIO blocks. */
  215. struct gvt_mmio_block {
  216. unsigned int device;
  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. static inline struct intel_gvt *to_gvt(struct drm_i915_private *i915)
  315. {
  316. return i915->gvt;
  317. }
  318. enum {
  319. /* Scheduling trigger by timer */
  320. INTEL_GVT_REQUEST_SCHED = 0,
  321. /* Scheduling trigger by event */
  322. INTEL_GVT_REQUEST_EVENT_SCHED = 1,
  323. /* per-vGPU vblank emulation request */
  324. INTEL_GVT_REQUEST_EMULATE_VBLANK = 2,
  325. INTEL_GVT_REQUEST_EMULATE_VBLANK_MAX = INTEL_GVT_REQUEST_EMULATE_VBLANK
  326. + GVT_MAX_VGPU,
  327. };
  328. static inline void intel_gvt_request_service(struct intel_gvt *gvt,
  329. int service)
  330. {
  331. set_bit(service, (void *)&gvt->service_request);
  332. wake_up(&gvt->service_thread_wq);
  333. }
  334. void intel_gvt_free_firmware(struct intel_gvt *gvt);
  335. int intel_gvt_load_firmware(struct intel_gvt *gvt);
  336. /* Aperture/GM space definitions for GVT device */
  337. #define MB_TO_BYTES(mb) ((mb) << 20ULL)
  338. #define BYTES_TO_MB(b) ((b) >> 20ULL)
  339. #define HOST_LOW_GM_SIZE MB_TO_BYTES(128)
  340. #define HOST_HIGH_GM_SIZE MB_TO_BYTES(384)
  341. #define HOST_FENCE 4
  342. #define gvt_to_ggtt(gvt) ((gvt)->gt->ggtt)
  343. /* Aperture/GM space definitions for GVT device */
  344. #define gvt_aperture_sz(gvt) gvt_to_ggtt(gvt)->mappable_end
  345. #define gvt_aperture_pa_base(gvt) gvt_to_ggtt(gvt)->gmadr.start
  346. #define gvt_ggtt_gm_sz(gvt) gvt_to_ggtt(gvt)->vm.total
  347. #define gvt_ggtt_sz(gvt) (gvt_to_ggtt(gvt)->vm.total >> PAGE_SHIFT << 3)
  348. #define gvt_hidden_sz(gvt) (gvt_ggtt_gm_sz(gvt) - gvt_aperture_sz(gvt))
  349. #define gvt_aperture_gmadr_base(gvt) (0)
  350. #define gvt_aperture_gmadr_end(gvt) (gvt_aperture_gmadr_base(gvt) \
  351. + gvt_aperture_sz(gvt) - 1)
  352. #define gvt_hidden_gmadr_base(gvt) (gvt_aperture_gmadr_base(gvt) \
  353. + gvt_aperture_sz(gvt))
  354. #define gvt_hidden_gmadr_end(gvt) (gvt_hidden_gmadr_base(gvt) \
  355. + gvt_hidden_sz(gvt) - 1)
  356. #define gvt_fence_sz(gvt) (gvt_to_ggtt(gvt)->num_fences)
  357. /* Aperture/GM space definitions for vGPU */
  358. #define vgpu_aperture_offset(vgpu) ((vgpu)->gm.low_gm_node.start)
  359. #define vgpu_hidden_offset(vgpu) ((vgpu)->gm.high_gm_node.start)
  360. #define vgpu_aperture_sz(vgpu) ((vgpu)->gm.aperture_sz)
  361. #define vgpu_hidden_sz(vgpu) ((vgpu)->gm.hidden_sz)
  362. #define vgpu_aperture_pa_base(vgpu) \
  363. (gvt_aperture_pa_base(vgpu->gvt) + vgpu_aperture_offset(vgpu))
  364. #define vgpu_ggtt_gm_sz(vgpu) ((vgpu)->gm.aperture_sz + (vgpu)->gm.hidden_sz)
  365. #define vgpu_aperture_pa_end(vgpu) \
  366. (vgpu_aperture_pa_base(vgpu) + vgpu_aperture_sz(vgpu) - 1)
  367. #define vgpu_aperture_gmadr_base(vgpu) (vgpu_aperture_offset(vgpu))
  368. #define vgpu_aperture_gmadr_end(vgpu) \
  369. (vgpu_aperture_gmadr_base(vgpu) + vgpu_aperture_sz(vgpu) - 1)
  370. #define vgpu_hidden_gmadr_base(vgpu) (vgpu_hidden_offset(vgpu))
  371. #define vgpu_hidden_gmadr_end(vgpu) \
  372. (vgpu_hidden_gmadr_base(vgpu) + vgpu_hidden_sz(vgpu) - 1)
  373. #define vgpu_fence_base(vgpu) (vgpu->fence.base)
  374. #define vgpu_fence_sz(vgpu) (vgpu->fence.size)
  375. /* ring context size i.e. the first 0x50 dwords*/
  376. #define RING_CTX_SIZE 320
  377. int intel_vgpu_alloc_resource(struct intel_vgpu *vgpu,
  378. const struct intel_vgpu_config *conf);
  379. void intel_vgpu_reset_resource(struct intel_vgpu *vgpu);
  380. void intel_vgpu_free_resource(struct intel_vgpu *vgpu);
  381. void intel_vgpu_write_fence(struct intel_vgpu *vgpu,
  382. u32 fence, u64 value);
  383. /* Macros for easily accessing vGPU virtual/shadow register.
  384. Explicitly seperate use for typed MMIO reg or real offset.*/
  385. #define vgpu_vreg_t(vgpu, reg) \
  386. (*(u32 *)(vgpu->mmio.vreg + i915_mmio_reg_offset(reg)))
  387. #define vgpu_vreg(vgpu, offset) \
  388. (*(u32 *)(vgpu->mmio.vreg + (offset)))
  389. #define vgpu_vreg64_t(vgpu, reg) \
  390. (*(u64 *)(vgpu->mmio.vreg + i915_mmio_reg_offset(reg)))
  391. #define vgpu_vreg64(vgpu, offset) \
  392. (*(u64 *)(vgpu->mmio.vreg + (offset)))
  393. #define for_each_active_vgpu(gvt, vgpu, id) \
  394. idr_for_each_entry((&(gvt)->vgpu_idr), (vgpu), (id)) \
  395. for_each_if(vgpu->active)
  396. static inline void intel_vgpu_write_pci_bar(struct intel_vgpu *vgpu,
  397. u32 offset, u32 val, bool low)
  398. {
  399. u32 *pval;
  400. /* BAR offset should be 32 bits algiend */
  401. offset = rounddown(offset, 4);
  402. pval = (u32 *)(vgpu_cfg_space(vgpu) + offset);
  403. if (low) {
  404. /*
  405. * only update bit 31 - bit 4,
  406. * leave the bit 3 - bit 0 unchanged.
  407. */
  408. *pval = (val & GENMASK(31, 4)) | (*pval & GENMASK(3, 0));
  409. } else {
  410. *pval = val;
  411. }
  412. }
  413. int intel_gvt_init_vgpu_types(struct intel_gvt *gvt);
  414. void intel_gvt_clean_vgpu_types(struct intel_gvt *gvt);
  415. struct intel_vgpu *intel_gvt_create_idle_vgpu(struct intel_gvt *gvt);
  416. void intel_gvt_destroy_idle_vgpu(struct intel_vgpu *vgpu);
  417. int intel_gvt_create_vgpu(struct intel_vgpu *vgpu,
  418. const struct intel_vgpu_config *conf);
  419. void intel_gvt_destroy_vgpu(struct intel_vgpu *vgpu);
  420. void intel_gvt_release_vgpu(struct intel_vgpu *vgpu);
  421. void intel_gvt_reset_vgpu_locked(struct intel_vgpu *vgpu, bool dmlr,
  422. intel_engine_mask_t engine_mask);
  423. void intel_gvt_reset_vgpu(struct intel_vgpu *vgpu);
  424. void intel_gvt_activate_vgpu(struct intel_vgpu *vgpu);
  425. void intel_gvt_deactivate_vgpu(struct intel_vgpu *vgpu);
  426. int intel_gvt_set_opregion(struct intel_vgpu *vgpu);
  427. int intel_gvt_set_edid(struct intel_vgpu *vgpu, int port_num);
  428. /* validating GM functions */
  429. #define vgpu_gmadr_is_aperture(vgpu, gmadr) \
  430. ((gmadr >= vgpu_aperture_gmadr_base(vgpu)) && \
  431. (gmadr <= vgpu_aperture_gmadr_end(vgpu)))
  432. #define vgpu_gmadr_is_hidden(vgpu, gmadr) \
  433. ((gmadr >= vgpu_hidden_gmadr_base(vgpu)) && \
  434. (gmadr <= vgpu_hidden_gmadr_end(vgpu)))
  435. #define vgpu_gmadr_is_valid(vgpu, gmadr) \
  436. ((vgpu_gmadr_is_aperture(vgpu, gmadr) || \
  437. (vgpu_gmadr_is_hidden(vgpu, gmadr))))
  438. #define gvt_gmadr_is_aperture(gvt, gmadr) \
  439. ((gmadr >= gvt_aperture_gmadr_base(gvt)) && \
  440. (gmadr <= gvt_aperture_gmadr_end(gvt)))
  441. #define gvt_gmadr_is_hidden(gvt, gmadr) \
  442. ((gmadr >= gvt_hidden_gmadr_base(gvt)) && \
  443. (gmadr <= gvt_hidden_gmadr_end(gvt)))
  444. #define gvt_gmadr_is_valid(gvt, gmadr) \
  445. (gvt_gmadr_is_aperture(gvt, gmadr) || \
  446. gvt_gmadr_is_hidden(gvt, gmadr))
  447. bool intel_gvt_ggtt_validate_range(struct intel_vgpu *vgpu, u64 addr, u32 size);
  448. int intel_gvt_ggtt_gmadr_g2h(struct intel_vgpu *vgpu, u64 g_addr, u64 *h_addr);
  449. int intel_gvt_ggtt_gmadr_h2g(struct intel_vgpu *vgpu, u64 h_addr, u64 *g_addr);
  450. int intel_gvt_ggtt_index_g2h(struct intel_vgpu *vgpu, unsigned long g_index,
  451. unsigned long *h_index);
  452. int intel_gvt_ggtt_h2g_index(struct intel_vgpu *vgpu, unsigned long h_index,
  453. unsigned long *g_index);
  454. void intel_vgpu_init_cfg_space(struct intel_vgpu *vgpu,
  455. bool primary);
  456. void intel_vgpu_reset_cfg_space(struct intel_vgpu *vgpu);
  457. int intel_vgpu_emulate_cfg_read(struct intel_vgpu *vgpu, unsigned int offset,
  458. void *p_data, unsigned int bytes);
  459. int intel_vgpu_emulate_cfg_write(struct intel_vgpu *vgpu, unsigned int offset,
  460. void *p_data, unsigned int bytes);
  461. void intel_vgpu_emulate_hotplug(struct intel_vgpu *vgpu, bool connected);
  462. static inline u64 intel_vgpu_get_bar_gpa(struct intel_vgpu *vgpu, int bar)
  463. {
  464. /* We are 64bit bar. */
  465. return (*(u64 *)(vgpu->cfg_space.virtual_cfg_space + bar)) &
  466. PCI_BASE_ADDRESS_MEM_MASK;
  467. }
  468. void intel_vgpu_clean_opregion(struct intel_vgpu *vgpu);
  469. int intel_vgpu_init_opregion(struct intel_vgpu *vgpu);
  470. int intel_vgpu_opregion_base_write_handler(struct intel_vgpu *vgpu, u32 gpa);
  471. int intel_vgpu_emulate_opregion_request(struct intel_vgpu *vgpu, u32 swsci);
  472. void populate_pvinfo_page(struct intel_vgpu *vgpu);
  473. int intel_gvt_scan_and_shadow_workload(struct intel_vgpu_workload *workload);
  474. void enter_failsafe_mode(struct intel_vgpu *vgpu, int reason);
  475. void intel_vgpu_detach_regions(struct intel_vgpu *vgpu);
  476. enum {
  477. GVT_FAILSAFE_UNSUPPORTED_GUEST,
  478. GVT_FAILSAFE_INSUFFICIENT_RESOURCE,
  479. GVT_FAILSAFE_GUEST_ERR,
  480. };
  481. static inline void mmio_hw_access_pre(struct intel_gt *gt)
  482. {
  483. intel_runtime_pm_get(gt->uncore->rpm);
  484. }
  485. static inline void mmio_hw_access_post(struct intel_gt *gt)
  486. {
  487. intel_runtime_pm_put_unchecked(gt->uncore->rpm);
  488. }
  489. /**
  490. * intel_gvt_mmio_set_accessed - mark a MMIO has been accessed
  491. * @gvt: a GVT device
  492. * @offset: register offset
  493. *
  494. */
  495. static inline void intel_gvt_mmio_set_accessed(
  496. struct intel_gvt *gvt, unsigned int offset)
  497. {
  498. gvt->mmio.mmio_attribute[offset >> 2] |= F_ACCESSED;
  499. }
  500. /**
  501. * intel_gvt_mmio_is_cmd_accessible - if a MMIO could be accessed by command
  502. * @gvt: a GVT device
  503. * @offset: register offset
  504. *
  505. * Returns:
  506. * True if an MMIO is able to be accessed by GPU commands
  507. */
  508. static inline bool intel_gvt_mmio_is_cmd_accessible(
  509. struct intel_gvt *gvt, unsigned int offset)
  510. {
  511. return gvt->mmio.mmio_attribute[offset >> 2] & F_CMD_ACCESS;
  512. }
  513. /**
  514. * intel_gvt_mmio_set_cmd_accessible -
  515. * mark a MMIO could be accessible by command
  516. * @gvt: a GVT device
  517. * @offset: register offset
  518. *
  519. */
  520. static inline void intel_gvt_mmio_set_cmd_accessible(
  521. struct intel_gvt *gvt, unsigned int offset)
  522. {
  523. gvt->mmio.mmio_attribute[offset >> 2] |= F_CMD_ACCESS;
  524. }
  525. /**
  526. * intel_gvt_mmio_is_unalign - mark a MMIO could be accessed unaligned
  527. * @gvt: a GVT device
  528. * @offset: register offset
  529. *
  530. */
  531. static inline bool intel_gvt_mmio_is_unalign(
  532. struct intel_gvt *gvt, unsigned int offset)
  533. {
  534. return gvt->mmio.mmio_attribute[offset >> 2] & F_UNALIGN;
  535. }
  536. /**
  537. * intel_gvt_mmio_has_mode_mask - if a MMIO has a mode mask
  538. * @gvt: a GVT device
  539. * @offset: register offset
  540. *
  541. * Returns:
  542. * True if a MMIO has a mode mask in its higher 16 bits, false if it isn't.
  543. *
  544. */
  545. static inline bool intel_gvt_mmio_has_mode_mask(
  546. struct intel_gvt *gvt, unsigned int offset)
  547. {
  548. return gvt->mmio.mmio_attribute[offset >> 2] & F_MODE_MASK;
  549. }
  550. /**
  551. * intel_gvt_mmio_is_sr_in_ctx -
  552. * check if an MMIO has F_SR_IN_CTX mask
  553. * @gvt: a GVT device
  554. * @offset: register offset
  555. *
  556. * Returns:
  557. * True if an MMIO has an F_SR_IN_CTX mask, false if it isn't.
  558. *
  559. */
  560. static inline bool intel_gvt_mmio_is_sr_in_ctx(
  561. struct intel_gvt *gvt, unsigned int offset)
  562. {
  563. return gvt->mmio.mmio_attribute[offset >> 2] & F_SR_IN_CTX;
  564. }
  565. /**
  566. * intel_gvt_mmio_set_sr_in_ctx -
  567. * mask an MMIO in GVT's mmio save-restore list and also
  568. * in hardware logical context image
  569. * @gvt: a GVT device
  570. * @offset: register offset
  571. *
  572. */
  573. static inline void intel_gvt_mmio_set_sr_in_ctx(
  574. struct intel_gvt *gvt, unsigned int offset)
  575. {
  576. gvt->mmio.mmio_attribute[offset >> 2] |= F_SR_IN_CTX;
  577. }
  578. void intel_gvt_debugfs_add_vgpu(struct intel_vgpu *vgpu);
  579. /**
  580. * intel_gvt_mmio_set_cmd_write_patch -
  581. * mark an MMIO if its cmd write needs to be
  582. * patched
  583. * @gvt: a GVT device
  584. * @offset: register offset
  585. *
  586. */
  587. static inline void intel_gvt_mmio_set_cmd_write_patch(
  588. struct intel_gvt *gvt, unsigned int offset)
  589. {
  590. gvt->mmio.mmio_attribute[offset >> 2] |= F_CMD_WRITE_PATCH;
  591. }
  592. /**
  593. * intel_gvt_mmio_is_cmd_write_patch - check if an mmio's cmd access needs to
  594. * be patched
  595. * @gvt: a GVT device
  596. * @offset: register offset
  597. *
  598. * Returns:
  599. * True if GPU commmand write to an MMIO should be patched
  600. */
  601. static inline bool intel_gvt_mmio_is_cmd_write_patch(
  602. struct intel_gvt *gvt, unsigned int offset)
  603. {
  604. return gvt->mmio.mmio_attribute[offset >> 2] & F_CMD_WRITE_PATCH;
  605. }
  606. /**
  607. * intel_gvt_read_gpa - copy data from GPA to host data buffer
  608. * @vgpu: a vGPU
  609. * @gpa: guest physical address
  610. * @buf: host data buffer
  611. * @len: data length
  612. *
  613. * Returns:
  614. * Zero on success, negative error code if failed.
  615. */
  616. static inline int intel_gvt_read_gpa(struct intel_vgpu *vgpu, unsigned long gpa,
  617. void *buf, unsigned long len)
  618. {
  619. if (!vgpu->attached)
  620. return -ESRCH;
  621. return vfio_dma_rw(&vgpu->vfio_device, gpa, buf, len, false);
  622. }
  623. /**
  624. * intel_gvt_write_gpa - copy data from host data buffer to GPA
  625. * @vgpu: a vGPU
  626. * @gpa: guest physical address
  627. * @buf: host data buffer
  628. * @len: data length
  629. *
  630. * Returns:
  631. * Zero on success, negative error code if failed.
  632. */
  633. static inline int intel_gvt_write_gpa(struct intel_vgpu *vgpu,
  634. unsigned long gpa, void *buf, unsigned long len)
  635. {
  636. if (!vgpu->attached)
  637. return -ESRCH;
  638. return vfio_dma_rw(&vgpu->vfio_device, gpa, buf, len, true);
  639. }
  640. void intel_gvt_debugfs_remove_vgpu(struct intel_vgpu *vgpu);
  641. void intel_gvt_debugfs_init(struct intel_gvt *gvt);
  642. void intel_gvt_debugfs_clean(struct intel_gvt *gvt);
  643. int intel_gvt_page_track_add(struct intel_vgpu *info, u64 gfn);
  644. int intel_gvt_page_track_remove(struct intel_vgpu *info, u64 gfn);
  645. int intel_gvt_dma_pin_guest_page(struct intel_vgpu *vgpu, dma_addr_t dma_addr);
  646. int intel_gvt_dma_map_guest_page(struct intel_vgpu *vgpu, unsigned long gfn,
  647. unsigned long size, dma_addr_t *dma_addr);
  648. void intel_gvt_dma_unmap_guest_page(struct intel_vgpu *vgpu,
  649. dma_addr_t dma_addr);
  650. #include "trace.h"
  651. #endif