3636
3737#include "i915_drv.h"
3838#include "gvt.h"
39+ #include <linux/vfio.h>
40+ #include <linux/mdev.h>
3941
4042struct intel_gvt_host intel_gvt_host ;
4143
@@ -44,6 +46,129 @@ static const char * const supported_hypervisors[] = {
4446 [INTEL_GVT_HYPERVISOR_KVM ] = "KVM" ,
4547};
4648
49+ static struct intel_vgpu_type * intel_gvt_find_vgpu_type (struct intel_gvt * gvt ,
50+ const char * name )
51+ {
52+ int i ;
53+ struct intel_vgpu_type * t ;
54+ const char * driver_name = dev_driver_string (
55+ & gvt -> dev_priv -> drm .pdev -> dev );
56+
57+ for (i = 0 ; i < gvt -> num_types ; i ++ ) {
58+ t = & gvt -> types [i ];
59+ if (!strncmp (t -> name , name + strlen (driver_name ) + 1 ,
60+ sizeof (t -> name )))
61+ return t ;
62+ }
63+
64+ return NULL ;
65+ }
66+
67+ static ssize_t available_instances_show (struct kobject * kobj ,
68+ struct device * dev , char * buf )
69+ {
70+ struct intel_vgpu_type * type ;
71+ unsigned int num = 0 ;
72+ void * gvt = kdev_to_i915 (dev )-> gvt ;
73+
74+ type = intel_gvt_find_vgpu_type (gvt , kobject_name (kobj ));
75+ if (!type )
76+ num = 0 ;
77+ else
78+ num = type -> avail_instance ;
79+
80+ return sprintf (buf , "%u\n" , num );
81+ }
82+
83+ static ssize_t device_api_show (struct kobject * kobj , struct device * dev ,
84+ char * buf )
85+ {
86+ return sprintf (buf , "%s\n" , VFIO_DEVICE_API_PCI_STRING );
87+ }
88+
89+ static ssize_t description_show (struct kobject * kobj , struct device * dev ,
90+ char * buf )
91+ {
92+ struct intel_vgpu_type * type ;
93+ void * gvt = kdev_to_i915 (dev )-> gvt ;
94+
95+ type = intel_gvt_find_vgpu_type (gvt , kobject_name (kobj ));
96+ if (!type )
97+ return 0 ;
98+
99+ return sprintf (buf , "low_gm_size: %dMB\nhigh_gm_size: %dMB\n"
100+ "fence: %d\nresolution: %s\n"
101+ "weight: %d\n" ,
102+ BYTES_TO_MB (type -> low_gm_size ),
103+ BYTES_TO_MB (type -> high_gm_size ),
104+ type -> fence , vgpu_edid_str (type -> resolution ),
105+ type -> weight );
106+ }
107+
108+ static MDEV_TYPE_ATTR_RO (available_instances );
109+ static MDEV_TYPE_ATTR_RO (device_api );
110+ static MDEV_TYPE_ATTR_RO (description );
111+
112+ static struct attribute * gvt_type_attrs [] = {
113+ & mdev_type_attr_available_instances .attr ,
114+ & mdev_type_attr_device_api .attr ,
115+ & mdev_type_attr_description .attr ,
116+ NULL ,
117+ };
118+
119+ static struct attribute_group * gvt_vgpu_type_groups [] = {
120+ [0 ... NR_MAX_INTEL_VGPU_TYPES - 1 ] = NULL ,
121+ };
122+
123+ static bool intel_get_gvt_attrs (struct attribute * * * type_attrs ,
124+ struct attribute_group * * * intel_vgpu_type_groups )
125+ {
126+ * type_attrs = gvt_type_attrs ;
127+ * intel_vgpu_type_groups = gvt_vgpu_type_groups ;
128+ return true;
129+ }
130+
131+ static bool intel_gvt_init_vgpu_type_groups (struct intel_gvt * gvt )
132+ {
133+ int i , j ;
134+ struct intel_vgpu_type * type ;
135+ struct attribute_group * group ;
136+
137+ for (i = 0 ; i < gvt -> num_types ; i ++ ) {
138+ type = & gvt -> types [i ];
139+
140+ group = kzalloc (sizeof (struct attribute_group ), GFP_KERNEL );
141+ if (WARN_ON (!group ))
142+ goto unwind ;
143+
144+ group -> name = type -> name ;
145+ group -> attrs = gvt_type_attrs ;
146+ gvt_vgpu_type_groups [i ] = group ;
147+ }
148+
149+ return true;
150+
151+ unwind :
152+ for (j = 0 ; j < i ; j ++ ) {
153+ group = gvt_vgpu_type_groups [j ];
154+ kfree (group );
155+ }
156+
157+ return false;
158+ }
159+
160+ static void intel_gvt_cleanup_vgpu_type_groups (struct intel_gvt * gvt )
161+ {
162+ int i ;
163+ struct attribute_group * group ;
164+
165+ for (i = 0 ; i < gvt -> num_types ; i ++ ) {
166+ group = gvt_vgpu_type_groups [i ];
167+ gvt_vgpu_type_groups [i ] = NULL ;
168+ kfree (group );
169+ }
170+ }
171+
47172static const struct intel_gvt_ops intel_gvt_ops = {
48173 .emulate_cfg_read = intel_vgpu_emulate_cfg_read ,
49174 .emulate_cfg_write = intel_vgpu_emulate_cfg_write ,
@@ -54,6 +179,8 @@ static const struct intel_gvt_ops intel_gvt_ops = {
54179 .vgpu_reset = intel_gvt_reset_vgpu ,
55180 .vgpu_activate = intel_gvt_activate_vgpu ,
56181 .vgpu_deactivate = intel_gvt_deactivate_vgpu ,
182+ .gvt_find_vgpu_type = intel_gvt_find_vgpu_type ,
183+ .get_gvt_attrs = intel_get_gvt_attrs ,
57184};
58185
59186/**
@@ -202,6 +329,7 @@ void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
202329 intel_gvt_free_firmware (gvt );
203330
204331 intel_gvt_hypervisor_host_exit (& dev_priv -> drm .pdev -> dev , gvt );
332+ intel_gvt_cleanup_vgpu_type_groups (gvt );
205333 intel_gvt_clean_vgpu_types (gvt );
206334
207335 idr_destroy (& gvt -> vgpu_idr );
@@ -292,6 +420,12 @@ int intel_gvt_init_device(struct drm_i915_private *dev_priv)
292420 if (ret )
293421 goto out_clean_thread ;
294422
423+ ret = intel_gvt_init_vgpu_type_groups (gvt );
424+ if (ret == false) {
425+ gvt_err ("failed to init vgpu type groups: %d\n" , ret );
426+ goto out_clean_types ;
427+ }
428+
295429 ret = intel_gvt_hypervisor_host_init (& dev_priv -> drm .pdev -> dev , gvt ,
296430 & intel_gvt_ops );
297431 if (ret ) {
0 commit comments