hyperv_fb.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, Microsoft Corporation.
  4. *
  5. * Author:
  6. * Haiyang Zhang <[email protected]>
  7. */
  8. /*
  9. * Hyper-V Synthetic Video Frame Buffer Driver
  10. *
  11. * This is the driver for the Hyper-V Synthetic Video, which supports
  12. * screen resolution up to Full HD 1920x1080 with 32 bit color on Windows
  13. * Server 2012, and 1600x1200 with 16 bit color on Windows Server 2008 R2
  14. * or earlier.
  15. *
  16. * It also solves the double mouse cursor issue of the emulated video mode.
  17. *
  18. * The default screen resolution is 1152x864, which may be changed by a
  19. * kernel parameter:
  20. * video=hyperv_fb:<width>x<height>
  21. * For example: video=hyperv_fb:1280x1024
  22. *
  23. * Portrait orientation is also supported:
  24. * For example: video=hyperv_fb:864x1152
  25. *
  26. * When a Windows 10 RS5+ host is used, the virtual machine screen
  27. * resolution is obtained from the host. The "video=hyperv_fb" option is
  28. * not needed, but still can be used to overwrite what the host specifies.
  29. * The VM resolution on the host could be set by executing the powershell
  30. * "set-vmvideo" command. For example
  31. * set-vmvideo -vmname name -horizontalresolution:1920 \
  32. * -verticalresolution:1200 -resolutiontype single
  33. *
  34. * Gen 1 VMs also support direct using VM's physical memory for framebuffer.
  35. * It could improve the efficiency and performance for framebuffer and VM.
  36. * This requires to allocate contiguous physical memory from Linux kernel's
  37. * CMA memory allocator. To enable this, supply a kernel parameter to give
  38. * enough memory space to CMA allocator for framebuffer. For example:
  39. * cma=130m
  40. * This gives 130MB memory to CMA allocator that can be allocated to
  41. * framebuffer. For reference, 8K resolution (7680x4320) takes about
  42. * 127MB memory.
  43. */
  44. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  45. #include <linux/aperture.h>
  46. #include <linux/module.h>
  47. #include <linux/kernel.h>
  48. #include <linux/vmalloc.h>
  49. #include <linux/init.h>
  50. #include <linux/completion.h>
  51. #include <linux/fb.h>
  52. #include <linux/pci.h>
  53. #include <linux/panic_notifier.h>
  54. #include <linux/efi.h>
  55. #include <linux/console.h>
  56. #include <linux/hyperv.h>
  57. /* Hyper-V Synthetic Video Protocol definitions and structures */
  58. #define MAX_VMBUS_PKT_SIZE 0x4000
  59. #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
  60. /* Support for VERSION_WIN7 is removed. #define is retained for reference. */
  61. #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
  62. #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
  63. #define SYNTHVID_VERSION_WIN10 SYNTHVID_VERSION(3, 5)
  64. #define SYNTHVID_VER_GET_MAJOR(ver) (ver & 0x0000ffff)
  65. #define SYNTHVID_VER_GET_MINOR(ver) ((ver & 0xffff0000) >> 16)
  66. #define SYNTHVID_DEPTH_WIN8 32
  67. #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
  68. enum pipe_msg_type {
  69. PIPE_MSG_INVALID,
  70. PIPE_MSG_DATA,
  71. PIPE_MSG_MAX
  72. };
  73. struct pipe_msg_hdr {
  74. u32 type;
  75. u32 size; /* size of message after this field */
  76. } __packed;
  77. enum synthvid_msg_type {
  78. SYNTHVID_ERROR = 0,
  79. SYNTHVID_VERSION_REQUEST = 1,
  80. SYNTHVID_VERSION_RESPONSE = 2,
  81. SYNTHVID_VRAM_LOCATION = 3,
  82. SYNTHVID_VRAM_LOCATION_ACK = 4,
  83. SYNTHVID_SITUATION_UPDATE = 5,
  84. SYNTHVID_SITUATION_UPDATE_ACK = 6,
  85. SYNTHVID_POINTER_POSITION = 7,
  86. SYNTHVID_POINTER_SHAPE = 8,
  87. SYNTHVID_FEATURE_CHANGE = 9,
  88. SYNTHVID_DIRT = 10,
  89. SYNTHVID_RESOLUTION_REQUEST = 13,
  90. SYNTHVID_RESOLUTION_RESPONSE = 14,
  91. SYNTHVID_MAX = 15
  92. };
  93. #define SYNTHVID_EDID_BLOCK_SIZE 128
  94. #define SYNTHVID_MAX_RESOLUTION_COUNT 64
  95. struct hvd_screen_info {
  96. u16 width;
  97. u16 height;
  98. } __packed;
  99. struct synthvid_msg_hdr {
  100. u32 type;
  101. u32 size; /* size of this header + payload after this field*/
  102. } __packed;
  103. struct synthvid_version_req {
  104. u32 version;
  105. } __packed;
  106. struct synthvid_version_resp {
  107. u32 version;
  108. u8 is_accepted;
  109. u8 max_video_outputs;
  110. } __packed;
  111. struct synthvid_supported_resolution_req {
  112. u8 maximum_resolution_count;
  113. } __packed;
  114. struct synthvid_supported_resolution_resp {
  115. u8 edid_block[SYNTHVID_EDID_BLOCK_SIZE];
  116. u8 resolution_count;
  117. u8 default_resolution_index;
  118. u8 is_standard;
  119. struct hvd_screen_info
  120. supported_resolution[SYNTHVID_MAX_RESOLUTION_COUNT];
  121. } __packed;
  122. struct synthvid_vram_location {
  123. u64 user_ctx;
  124. u8 is_vram_gpa_specified;
  125. u64 vram_gpa;
  126. } __packed;
  127. struct synthvid_vram_location_ack {
  128. u64 user_ctx;
  129. } __packed;
  130. struct video_output_situation {
  131. u8 active;
  132. u32 vram_offset;
  133. u8 depth_bits;
  134. u32 width_pixels;
  135. u32 height_pixels;
  136. u32 pitch_bytes;
  137. } __packed;
  138. struct synthvid_situation_update {
  139. u64 user_ctx;
  140. u8 video_output_count;
  141. struct video_output_situation video_output[1];
  142. } __packed;
  143. struct synthvid_situation_update_ack {
  144. u64 user_ctx;
  145. } __packed;
  146. struct synthvid_pointer_position {
  147. u8 is_visible;
  148. u8 video_output;
  149. s32 image_x;
  150. s32 image_y;
  151. } __packed;
  152. #define CURSOR_MAX_X 96
  153. #define CURSOR_MAX_Y 96
  154. #define CURSOR_ARGB_PIXEL_SIZE 4
  155. #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
  156. #define CURSOR_COMPLETE (-1)
  157. struct synthvid_pointer_shape {
  158. u8 part_idx;
  159. u8 is_argb;
  160. u32 width; /* CURSOR_MAX_X at most */
  161. u32 height; /* CURSOR_MAX_Y at most */
  162. u32 hot_x; /* hotspot relative to upper-left of pointer image */
  163. u32 hot_y;
  164. u8 data[4];
  165. } __packed;
  166. struct synthvid_feature_change {
  167. u8 is_dirt_needed;
  168. u8 is_ptr_pos_needed;
  169. u8 is_ptr_shape_needed;
  170. u8 is_situ_needed;
  171. } __packed;
  172. struct rect {
  173. s32 x1, y1; /* top left corner */
  174. s32 x2, y2; /* bottom right corner, exclusive */
  175. } __packed;
  176. struct synthvid_dirt {
  177. u8 video_output;
  178. u8 dirt_count;
  179. struct rect rect[1];
  180. } __packed;
  181. struct synthvid_msg {
  182. struct pipe_msg_hdr pipe_hdr;
  183. struct synthvid_msg_hdr vid_hdr;
  184. union {
  185. struct synthvid_version_req ver_req;
  186. struct synthvid_version_resp ver_resp;
  187. struct synthvid_vram_location vram;
  188. struct synthvid_vram_location_ack vram_ack;
  189. struct synthvid_situation_update situ;
  190. struct synthvid_situation_update_ack situ_ack;
  191. struct synthvid_pointer_position ptr_pos;
  192. struct synthvid_pointer_shape ptr_shape;
  193. struct synthvid_feature_change feature_chg;
  194. struct synthvid_dirt dirt;
  195. struct synthvid_supported_resolution_req resolution_req;
  196. struct synthvid_supported_resolution_resp resolution_resp;
  197. };
  198. } __packed;
  199. /* FB driver definitions and structures */
  200. #define HVFB_WIDTH 1152 /* default screen width */
  201. #define HVFB_HEIGHT 864 /* default screen height */
  202. #define HVFB_WIDTH_MIN 640
  203. #define HVFB_HEIGHT_MIN 480
  204. #define RING_BUFSIZE (256 * 1024)
  205. #define VSP_TIMEOUT (10 * HZ)
  206. #define HVFB_UPDATE_DELAY (HZ / 20)
  207. #define HVFB_ONDEMAND_THROTTLE (HZ / 20)
  208. struct hvfb_par {
  209. struct fb_info *info;
  210. struct resource *mem;
  211. bool fb_ready; /* fb device is ready */
  212. struct completion wait;
  213. u32 synthvid_version;
  214. struct delayed_work dwork;
  215. bool update;
  216. bool update_saved; /* The value of 'update' before hibernation */
  217. u32 pseudo_palette[16];
  218. u8 init_buf[MAX_VMBUS_PKT_SIZE];
  219. u8 recv_buf[MAX_VMBUS_PKT_SIZE];
  220. /* If true, the VSC notifies the VSP on every framebuffer change */
  221. bool synchronous_fb;
  222. /* If true, need to copy from deferred IO mem to framebuffer mem */
  223. bool need_docopy;
  224. struct notifier_block hvfb_panic_nb;
  225. /* Memory for deferred IO and frame buffer itself */
  226. unsigned char *dio_vp;
  227. unsigned char *mmio_vp;
  228. phys_addr_t mmio_pp;
  229. /* Dirty rectangle, protected by delayed_refresh_lock */
  230. int x1, y1, x2, y2;
  231. bool delayed_refresh;
  232. spinlock_t delayed_refresh_lock;
  233. };
  234. static uint screen_width = HVFB_WIDTH;
  235. static uint screen_height = HVFB_HEIGHT;
  236. static uint screen_depth;
  237. static uint screen_fb_size;
  238. static uint dio_fb_size; /* FB size for deferred IO */
  239. /* Send message to Hyper-V host */
  240. static inline int synthvid_send(struct hv_device *hdev,
  241. struct synthvid_msg *msg)
  242. {
  243. static atomic64_t request_id = ATOMIC64_INIT(0);
  244. int ret;
  245. msg->pipe_hdr.type = PIPE_MSG_DATA;
  246. msg->pipe_hdr.size = msg->vid_hdr.size;
  247. ret = vmbus_sendpacket(hdev->channel, msg,
  248. msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
  249. atomic64_inc_return(&request_id),
  250. VM_PKT_DATA_INBAND, 0);
  251. if (ret)
  252. pr_err_ratelimited("Unable to send packet via vmbus; error %d\n", ret);
  253. return ret;
  254. }
  255. /* Send screen resolution info to host */
  256. static int synthvid_send_situ(struct hv_device *hdev)
  257. {
  258. struct fb_info *info = hv_get_drvdata(hdev);
  259. struct synthvid_msg msg;
  260. if (!info)
  261. return -ENODEV;
  262. memset(&msg, 0, sizeof(struct synthvid_msg));
  263. msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
  264. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  265. sizeof(struct synthvid_situation_update);
  266. msg.situ.user_ctx = 0;
  267. msg.situ.video_output_count = 1;
  268. msg.situ.video_output[0].active = 1;
  269. msg.situ.video_output[0].vram_offset = 0;
  270. msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
  271. msg.situ.video_output[0].width_pixels = info->var.xres;
  272. msg.situ.video_output[0].height_pixels = info->var.yres;
  273. msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
  274. synthvid_send(hdev, &msg);
  275. return 0;
  276. }
  277. /* Send mouse pointer info to host */
  278. static int synthvid_send_ptr(struct hv_device *hdev)
  279. {
  280. struct synthvid_msg msg;
  281. memset(&msg, 0, sizeof(struct synthvid_msg));
  282. msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
  283. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  284. sizeof(struct synthvid_pointer_position);
  285. msg.ptr_pos.is_visible = 1;
  286. msg.ptr_pos.video_output = 0;
  287. msg.ptr_pos.image_x = 0;
  288. msg.ptr_pos.image_y = 0;
  289. synthvid_send(hdev, &msg);
  290. memset(&msg, 0, sizeof(struct synthvid_msg));
  291. msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
  292. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  293. sizeof(struct synthvid_pointer_shape);
  294. msg.ptr_shape.part_idx = CURSOR_COMPLETE;
  295. msg.ptr_shape.is_argb = 1;
  296. msg.ptr_shape.width = 1;
  297. msg.ptr_shape.height = 1;
  298. msg.ptr_shape.hot_x = 0;
  299. msg.ptr_shape.hot_y = 0;
  300. msg.ptr_shape.data[0] = 0;
  301. msg.ptr_shape.data[1] = 1;
  302. msg.ptr_shape.data[2] = 1;
  303. msg.ptr_shape.data[3] = 1;
  304. synthvid_send(hdev, &msg);
  305. return 0;
  306. }
  307. /* Send updated screen area (dirty rectangle) location to host */
  308. static int
  309. synthvid_update(struct fb_info *info, int x1, int y1, int x2, int y2)
  310. {
  311. struct hv_device *hdev = device_to_hv_device(info->device);
  312. struct synthvid_msg msg;
  313. memset(&msg, 0, sizeof(struct synthvid_msg));
  314. if (x2 == INT_MAX)
  315. x2 = info->var.xres;
  316. if (y2 == INT_MAX)
  317. y2 = info->var.yres;
  318. msg.vid_hdr.type = SYNTHVID_DIRT;
  319. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  320. sizeof(struct synthvid_dirt);
  321. msg.dirt.video_output = 0;
  322. msg.dirt.dirt_count = 1;
  323. msg.dirt.rect[0].x1 = (x1 > x2) ? 0 : x1;
  324. msg.dirt.rect[0].y1 = (y1 > y2) ? 0 : y1;
  325. msg.dirt.rect[0].x2 =
  326. (x2 < x1 || x2 > info->var.xres) ? info->var.xres : x2;
  327. msg.dirt.rect[0].y2 =
  328. (y2 < y1 || y2 > info->var.yres) ? info->var.yres : y2;
  329. synthvid_send(hdev, &msg);
  330. return 0;
  331. }
  332. static void hvfb_docopy(struct hvfb_par *par,
  333. unsigned long offset,
  334. unsigned long size)
  335. {
  336. if (!par || !par->mmio_vp || !par->dio_vp || !par->fb_ready ||
  337. size == 0 || offset >= dio_fb_size)
  338. return;
  339. if (offset + size > dio_fb_size)
  340. size = dio_fb_size - offset;
  341. memcpy(par->mmio_vp + offset, par->dio_vp + offset, size);
  342. }
  343. /* Deferred IO callback */
  344. static void synthvid_deferred_io(struct fb_info *p, struct list_head *pagereflist)
  345. {
  346. struct hvfb_par *par = p->par;
  347. struct fb_deferred_io_pageref *pageref;
  348. unsigned long start, end;
  349. int y1, y2, miny, maxy;
  350. miny = INT_MAX;
  351. maxy = 0;
  352. /*
  353. * Merge dirty pages. It is possible that last page cross
  354. * over the end of frame buffer row yres. This is taken care of
  355. * in synthvid_update function by clamping the y2
  356. * value to yres.
  357. */
  358. list_for_each_entry(pageref, pagereflist, list) {
  359. start = pageref->offset;
  360. end = start + PAGE_SIZE - 1;
  361. y1 = start / p->fix.line_length;
  362. y2 = end / p->fix.line_length;
  363. miny = min_t(int, miny, y1);
  364. maxy = max_t(int, maxy, y2);
  365. /* Copy from dio space to mmio address */
  366. if (par->fb_ready && par->need_docopy)
  367. hvfb_docopy(par, start, PAGE_SIZE);
  368. }
  369. if (par->fb_ready && par->update)
  370. synthvid_update(p, 0, miny, p->var.xres, maxy + 1);
  371. }
  372. static struct fb_deferred_io synthvid_defio = {
  373. .delay = HZ / 20,
  374. .deferred_io = synthvid_deferred_io,
  375. };
  376. /*
  377. * Actions on received messages from host:
  378. * Complete the wait event.
  379. * Or, reply with screen and cursor info.
  380. */
  381. static void synthvid_recv_sub(struct hv_device *hdev)
  382. {
  383. struct fb_info *info = hv_get_drvdata(hdev);
  384. struct hvfb_par *par;
  385. struct synthvid_msg *msg;
  386. if (!info)
  387. return;
  388. par = info->par;
  389. msg = (struct synthvid_msg *)par->recv_buf;
  390. /* Complete the wait event */
  391. if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
  392. msg->vid_hdr.type == SYNTHVID_RESOLUTION_RESPONSE ||
  393. msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
  394. memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
  395. complete(&par->wait);
  396. return;
  397. }
  398. /* Reply with screen and cursor info */
  399. if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
  400. if (par->fb_ready) {
  401. synthvid_send_ptr(hdev);
  402. synthvid_send_situ(hdev);
  403. }
  404. par->update = msg->feature_chg.is_dirt_needed;
  405. if (par->update)
  406. schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
  407. }
  408. }
  409. /* Receive callback for messages from the host */
  410. static void synthvid_receive(void *ctx)
  411. {
  412. struct hv_device *hdev = ctx;
  413. struct fb_info *info = hv_get_drvdata(hdev);
  414. struct hvfb_par *par;
  415. struct synthvid_msg *recv_buf;
  416. u32 bytes_recvd;
  417. u64 req_id;
  418. int ret;
  419. if (!info)
  420. return;
  421. par = info->par;
  422. recv_buf = (struct synthvid_msg *)par->recv_buf;
  423. do {
  424. ret = vmbus_recvpacket(hdev->channel, recv_buf,
  425. MAX_VMBUS_PKT_SIZE,
  426. &bytes_recvd, &req_id);
  427. if (bytes_recvd > 0 &&
  428. recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
  429. synthvid_recv_sub(hdev);
  430. } while (bytes_recvd > 0 && ret == 0);
  431. }
  432. /* Check if the ver1 version is equal or greater than ver2 */
  433. static inline bool synthvid_ver_ge(u32 ver1, u32 ver2)
  434. {
  435. if (SYNTHVID_VER_GET_MAJOR(ver1) > SYNTHVID_VER_GET_MAJOR(ver2) ||
  436. (SYNTHVID_VER_GET_MAJOR(ver1) == SYNTHVID_VER_GET_MAJOR(ver2) &&
  437. SYNTHVID_VER_GET_MINOR(ver1) >= SYNTHVID_VER_GET_MINOR(ver2)))
  438. return true;
  439. return false;
  440. }
  441. /* Check synthetic video protocol version with the host */
  442. static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
  443. {
  444. struct fb_info *info = hv_get_drvdata(hdev);
  445. struct hvfb_par *par = info->par;
  446. struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
  447. int ret = 0;
  448. unsigned long t;
  449. memset(msg, 0, sizeof(struct synthvid_msg));
  450. msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
  451. msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  452. sizeof(struct synthvid_version_req);
  453. msg->ver_req.version = ver;
  454. synthvid_send(hdev, msg);
  455. t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
  456. if (!t) {
  457. pr_err("Time out on waiting version response\n");
  458. ret = -ETIMEDOUT;
  459. goto out;
  460. }
  461. if (!msg->ver_resp.is_accepted) {
  462. ret = -ENODEV;
  463. goto out;
  464. }
  465. par->synthvid_version = ver;
  466. pr_info("Synthvid Version major %d, minor %d\n",
  467. SYNTHVID_VER_GET_MAJOR(ver), SYNTHVID_VER_GET_MINOR(ver));
  468. out:
  469. return ret;
  470. }
  471. /* Get current resolution from the host */
  472. static int synthvid_get_supported_resolution(struct hv_device *hdev)
  473. {
  474. struct fb_info *info = hv_get_drvdata(hdev);
  475. struct hvfb_par *par = info->par;
  476. struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
  477. int ret = 0;
  478. unsigned long t;
  479. u8 index;
  480. memset(msg, 0, sizeof(struct synthvid_msg));
  481. msg->vid_hdr.type = SYNTHVID_RESOLUTION_REQUEST;
  482. msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  483. sizeof(struct synthvid_supported_resolution_req);
  484. msg->resolution_req.maximum_resolution_count =
  485. SYNTHVID_MAX_RESOLUTION_COUNT;
  486. synthvid_send(hdev, msg);
  487. t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
  488. if (!t) {
  489. pr_err("Time out on waiting resolution response\n");
  490. ret = -ETIMEDOUT;
  491. goto out;
  492. }
  493. if (msg->resolution_resp.resolution_count == 0) {
  494. pr_err("No supported resolutions\n");
  495. ret = -ENODEV;
  496. goto out;
  497. }
  498. index = msg->resolution_resp.default_resolution_index;
  499. if (index >= msg->resolution_resp.resolution_count) {
  500. pr_err("Invalid resolution index: %d\n", index);
  501. ret = -ENODEV;
  502. goto out;
  503. }
  504. screen_width =
  505. msg->resolution_resp.supported_resolution[index].width;
  506. screen_height =
  507. msg->resolution_resp.supported_resolution[index].height;
  508. out:
  509. return ret;
  510. }
  511. /* Connect to VSP (Virtual Service Provider) on host */
  512. static int synthvid_connect_vsp(struct hv_device *hdev)
  513. {
  514. struct fb_info *info = hv_get_drvdata(hdev);
  515. struct hvfb_par *par = info->par;
  516. int ret;
  517. ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
  518. NULL, 0, synthvid_receive, hdev);
  519. if (ret) {
  520. pr_err("Unable to open vmbus channel\n");
  521. return ret;
  522. }
  523. /* Negotiate the protocol version with host */
  524. switch (vmbus_proto_version) {
  525. case VERSION_WIN10:
  526. case VERSION_WIN10_V5:
  527. ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
  528. if (!ret)
  529. break;
  530. fallthrough;
  531. case VERSION_WIN8:
  532. case VERSION_WIN8_1:
  533. ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
  534. break;
  535. default:
  536. ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN10);
  537. break;
  538. }
  539. if (ret) {
  540. pr_err("Synthetic video device version not accepted\n");
  541. goto error;
  542. }
  543. screen_depth = SYNTHVID_DEPTH_WIN8;
  544. if (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10)) {
  545. ret = synthvid_get_supported_resolution(hdev);
  546. if (ret)
  547. pr_info("Failed to get supported resolution from host, use default\n");
  548. }
  549. screen_fb_size = hdev->channel->offermsg.offer.
  550. mmio_megabytes * 1024 * 1024;
  551. return 0;
  552. error:
  553. vmbus_close(hdev->channel);
  554. return ret;
  555. }
  556. /* Send VRAM and Situation messages to the host */
  557. static int synthvid_send_config(struct hv_device *hdev)
  558. {
  559. struct fb_info *info = hv_get_drvdata(hdev);
  560. struct hvfb_par *par = info->par;
  561. struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
  562. int ret = 0;
  563. unsigned long t;
  564. /* Send VRAM location */
  565. memset(msg, 0, sizeof(struct synthvid_msg));
  566. msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
  567. msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  568. sizeof(struct synthvid_vram_location);
  569. msg->vram.user_ctx = msg->vram.vram_gpa = par->mmio_pp;
  570. msg->vram.is_vram_gpa_specified = 1;
  571. synthvid_send(hdev, msg);
  572. t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
  573. if (!t) {
  574. pr_err("Time out on waiting vram location ack\n");
  575. ret = -ETIMEDOUT;
  576. goto out;
  577. }
  578. if (msg->vram_ack.user_ctx != par->mmio_pp) {
  579. pr_err("Unable to set VRAM location\n");
  580. ret = -ENODEV;
  581. goto out;
  582. }
  583. /* Send pointer and situation update */
  584. synthvid_send_ptr(hdev);
  585. synthvid_send_situ(hdev);
  586. out:
  587. return ret;
  588. }
  589. /*
  590. * Delayed work callback:
  591. * It is scheduled to call whenever update request is received and it has
  592. * not been called in last HVFB_ONDEMAND_THROTTLE time interval.
  593. */
  594. static void hvfb_update_work(struct work_struct *w)
  595. {
  596. struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
  597. struct fb_info *info = par->info;
  598. unsigned long flags;
  599. int x1, x2, y1, y2;
  600. int j;
  601. spin_lock_irqsave(&par->delayed_refresh_lock, flags);
  602. /* Reset the request flag */
  603. par->delayed_refresh = false;
  604. /* Store the dirty rectangle to local variables */
  605. x1 = par->x1;
  606. x2 = par->x2;
  607. y1 = par->y1;
  608. y2 = par->y2;
  609. /* Clear dirty rectangle */
  610. par->x1 = par->y1 = INT_MAX;
  611. par->x2 = par->y2 = 0;
  612. spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
  613. if (x1 > info->var.xres || x2 > info->var.xres ||
  614. y1 > info->var.yres || y2 > info->var.yres || x2 <= x1)
  615. return;
  616. /* Copy the dirty rectangle to frame buffer memory */
  617. if (par->need_docopy)
  618. for (j = y1; j < y2; j++)
  619. hvfb_docopy(par,
  620. j * info->fix.line_length +
  621. (x1 * screen_depth / 8),
  622. (x2 - x1) * screen_depth / 8);
  623. /* Refresh */
  624. if (par->fb_ready && par->update)
  625. synthvid_update(info, x1, y1, x2, y2);
  626. }
  627. /*
  628. * Control the on-demand refresh frequency. It schedules a delayed
  629. * screen update if it has not yet.
  630. */
  631. static void hvfb_ondemand_refresh_throttle(struct hvfb_par *par,
  632. int x1, int y1, int w, int h)
  633. {
  634. unsigned long flags;
  635. int x2 = x1 + w;
  636. int y2 = y1 + h;
  637. spin_lock_irqsave(&par->delayed_refresh_lock, flags);
  638. /* Merge dirty rectangle */
  639. par->x1 = min_t(int, par->x1, x1);
  640. par->y1 = min_t(int, par->y1, y1);
  641. par->x2 = max_t(int, par->x2, x2);
  642. par->y2 = max_t(int, par->y2, y2);
  643. /* Schedule a delayed screen update if not yet */
  644. if (par->delayed_refresh == false) {
  645. schedule_delayed_work(&par->dwork,
  646. HVFB_ONDEMAND_THROTTLE);
  647. par->delayed_refresh = true;
  648. }
  649. spin_unlock_irqrestore(&par->delayed_refresh_lock, flags);
  650. }
  651. static int hvfb_on_panic(struct notifier_block *nb,
  652. unsigned long e, void *p)
  653. {
  654. struct hv_device *hdev;
  655. struct hvfb_par *par;
  656. struct fb_info *info;
  657. par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
  658. info = par->info;
  659. hdev = device_to_hv_device(info->device);
  660. if (hv_ringbuffer_spinlock_busy(hdev->channel))
  661. return NOTIFY_DONE;
  662. par->synchronous_fb = true;
  663. if (par->need_docopy)
  664. hvfb_docopy(par, 0, dio_fb_size);
  665. synthvid_update(info, 0, 0, INT_MAX, INT_MAX);
  666. return NOTIFY_DONE;
  667. }
  668. /* Framebuffer operation handlers */
  669. static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  670. {
  671. if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
  672. var->xres > screen_width || var->yres > screen_height ||
  673. var->bits_per_pixel != screen_depth)
  674. return -EINVAL;
  675. var->xres_virtual = var->xres;
  676. var->yres_virtual = var->yres;
  677. return 0;
  678. }
  679. static int hvfb_set_par(struct fb_info *info)
  680. {
  681. struct hv_device *hdev = device_to_hv_device(info->device);
  682. return synthvid_send_situ(hdev);
  683. }
  684. static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
  685. {
  686. return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
  687. }
  688. static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  689. unsigned blue, unsigned transp, struct fb_info *info)
  690. {
  691. u32 *pal = info->pseudo_palette;
  692. if (regno > 15)
  693. return -EINVAL;
  694. pal[regno] = chan_to_field(red, &info->var.red)
  695. | chan_to_field(green, &info->var.green)
  696. | chan_to_field(blue, &info->var.blue)
  697. | chan_to_field(transp, &info->var.transp);
  698. return 0;
  699. }
  700. static int hvfb_blank(int blank, struct fb_info *info)
  701. {
  702. return 1; /* get fb_blank to set the colormap to all black */
  703. }
  704. static void hvfb_cfb_fillrect(struct fb_info *p,
  705. const struct fb_fillrect *rect)
  706. {
  707. struct hvfb_par *par = p->par;
  708. cfb_fillrect(p, rect);
  709. if (par->synchronous_fb)
  710. synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
  711. else
  712. hvfb_ondemand_refresh_throttle(par, rect->dx, rect->dy,
  713. rect->width, rect->height);
  714. }
  715. static void hvfb_cfb_copyarea(struct fb_info *p,
  716. const struct fb_copyarea *area)
  717. {
  718. struct hvfb_par *par = p->par;
  719. cfb_copyarea(p, area);
  720. if (par->synchronous_fb)
  721. synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
  722. else
  723. hvfb_ondemand_refresh_throttle(par, area->dx, area->dy,
  724. area->width, area->height);
  725. }
  726. static void hvfb_cfb_imageblit(struct fb_info *p,
  727. const struct fb_image *image)
  728. {
  729. struct hvfb_par *par = p->par;
  730. cfb_imageblit(p, image);
  731. if (par->synchronous_fb)
  732. synthvid_update(p, 0, 0, INT_MAX, INT_MAX);
  733. else
  734. hvfb_ondemand_refresh_throttle(par, image->dx, image->dy,
  735. image->width, image->height);
  736. }
  737. static const struct fb_ops hvfb_ops = {
  738. .owner = THIS_MODULE,
  739. .fb_check_var = hvfb_check_var,
  740. .fb_set_par = hvfb_set_par,
  741. .fb_setcolreg = hvfb_setcolreg,
  742. .fb_fillrect = hvfb_cfb_fillrect,
  743. .fb_copyarea = hvfb_cfb_copyarea,
  744. .fb_imageblit = hvfb_cfb_imageblit,
  745. .fb_blank = hvfb_blank,
  746. .fb_mmap = fb_deferred_io_mmap,
  747. };
  748. /* Get options from kernel paramenter "video=" */
  749. static void hvfb_get_option(struct fb_info *info)
  750. {
  751. struct hvfb_par *par = info->par;
  752. char *opt = NULL, *p;
  753. uint x = 0, y = 0;
  754. if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
  755. return;
  756. p = strsep(&opt, "x");
  757. if (!*p || kstrtouint(p, 0, &x) ||
  758. !opt || !*opt || kstrtouint(opt, 0, &y)) {
  759. pr_err("Screen option is invalid: skipped\n");
  760. return;
  761. }
  762. if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
  763. (synthvid_ver_ge(par->synthvid_version, SYNTHVID_VERSION_WIN10) &&
  764. (x * y * screen_depth / 8 > screen_fb_size)) ||
  765. (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
  766. x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8)) {
  767. pr_err("Screen resolution option is out of range: skipped\n");
  768. return;
  769. }
  770. screen_width = x;
  771. screen_height = y;
  772. return;
  773. }
  774. /*
  775. * Allocate enough contiguous physical memory.
  776. * Return physical address if succeeded or -1 if failed.
  777. */
  778. static phys_addr_t hvfb_get_phymem(struct hv_device *hdev,
  779. unsigned int request_size)
  780. {
  781. struct page *page = NULL;
  782. dma_addr_t dma_handle;
  783. void *vmem;
  784. phys_addr_t paddr = 0;
  785. unsigned int order = get_order(request_size);
  786. if (request_size == 0)
  787. return -1;
  788. if (order < MAX_ORDER) {
  789. /* Call alloc_pages if the size is less than 2^MAX_ORDER */
  790. page = alloc_pages(GFP_KERNEL | __GFP_ZERO, order);
  791. if (!page)
  792. return -1;
  793. paddr = (page_to_pfn(page) << PAGE_SHIFT);
  794. } else {
  795. /* Allocate from CMA */
  796. hdev->device.coherent_dma_mask = DMA_BIT_MASK(64);
  797. vmem = dma_alloc_coherent(&hdev->device,
  798. round_up(request_size, PAGE_SIZE),
  799. &dma_handle,
  800. GFP_KERNEL | __GFP_NOWARN);
  801. if (!vmem)
  802. return -1;
  803. paddr = virt_to_phys(vmem);
  804. }
  805. return paddr;
  806. }
  807. /* Release contiguous physical memory */
  808. static void hvfb_release_phymem(struct hv_device *hdev,
  809. phys_addr_t paddr, unsigned int size)
  810. {
  811. unsigned int order = get_order(size);
  812. if (order < MAX_ORDER)
  813. __free_pages(pfn_to_page(paddr >> PAGE_SHIFT), order);
  814. else
  815. dma_free_coherent(&hdev->device,
  816. round_up(size, PAGE_SIZE),
  817. phys_to_virt(paddr),
  818. paddr);
  819. }
  820. /* Get framebuffer memory from Hyper-V video pci space */
  821. static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
  822. {
  823. struct hvfb_par *par = info->par;
  824. struct pci_dev *pdev = NULL;
  825. void __iomem *fb_virt;
  826. int gen2vm = efi_enabled(EFI_BOOT);
  827. resource_size_t base, size;
  828. phys_addr_t paddr;
  829. int ret;
  830. if (!gen2vm) {
  831. pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
  832. PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
  833. if (!pdev) {
  834. pr_err("Unable to find PCI Hyper-V video\n");
  835. return -ENODEV;
  836. }
  837. base = pci_resource_start(pdev, 0);
  838. size = pci_resource_len(pdev, 0);
  839. /*
  840. * For Gen 1 VM, we can directly use the contiguous memory
  841. * from VM. If we succeed, deferred IO happens directly
  842. * on this allocated framebuffer memory, avoiding extra
  843. * memory copy.
  844. */
  845. paddr = hvfb_get_phymem(hdev, screen_fb_size);
  846. if (paddr != (phys_addr_t) -1) {
  847. par->mmio_pp = paddr;
  848. par->mmio_vp = par->dio_vp = __va(paddr);
  849. info->fix.smem_start = paddr;
  850. info->fix.smem_len = screen_fb_size;
  851. info->screen_base = par->mmio_vp;
  852. info->screen_size = screen_fb_size;
  853. par->need_docopy = false;
  854. goto getmem_done;
  855. }
  856. pr_info("Unable to allocate enough contiguous physical memory on Gen 1 VM. Using MMIO instead.\n");
  857. } else {
  858. base = screen_info.lfb_base;
  859. size = screen_info.lfb_size;
  860. }
  861. /*
  862. * Cannot use the contiguous physical memory.
  863. * Allocate mmio space for framebuffer.
  864. */
  865. dio_fb_size =
  866. screen_width * screen_height * screen_depth / 8;
  867. ret = vmbus_allocate_mmio(&par->mem, hdev, 0, -1,
  868. screen_fb_size, 0x100000, true);
  869. if (ret != 0) {
  870. pr_err("Unable to allocate framebuffer memory\n");
  871. goto err1;
  872. }
  873. /*
  874. * Map the VRAM cacheable for performance. This is also required for
  875. * VM Connect to display properly for ARM64 Linux VM, as the host also
  876. * maps the VRAM cacheable.
  877. */
  878. fb_virt = ioremap_cache(par->mem->start, screen_fb_size);
  879. if (!fb_virt)
  880. goto err2;
  881. /* Allocate memory for deferred IO */
  882. par->dio_vp = vzalloc(round_up(dio_fb_size, PAGE_SIZE));
  883. if (par->dio_vp == NULL)
  884. goto err3;
  885. /* Physical address of FB device */
  886. par->mmio_pp = par->mem->start;
  887. /* Virtual address of FB device */
  888. par->mmio_vp = (unsigned char *) fb_virt;
  889. info->fix.smem_start = par->mem->start;
  890. info->fix.smem_len = dio_fb_size;
  891. info->screen_base = par->dio_vp;
  892. info->screen_size = dio_fb_size;
  893. getmem_done:
  894. aperture_remove_conflicting_devices(base, size, false, KBUILD_MODNAME);
  895. if (gen2vm) {
  896. /* framebuffer is reallocated, clear screen_info to avoid misuse from kexec */
  897. screen_info.lfb_size = 0;
  898. screen_info.lfb_base = 0;
  899. screen_info.orig_video_isVGA = 0;
  900. } else {
  901. pci_dev_put(pdev);
  902. }
  903. return 0;
  904. err3:
  905. iounmap(fb_virt);
  906. err2:
  907. vmbus_free_mmio(par->mem->start, screen_fb_size);
  908. par->mem = NULL;
  909. err1:
  910. if (!gen2vm)
  911. pci_dev_put(pdev);
  912. return -ENOMEM;
  913. }
  914. /* Release the framebuffer */
  915. static void hvfb_putmem(struct hv_device *hdev, struct fb_info *info)
  916. {
  917. struct hvfb_par *par = info->par;
  918. if (par->need_docopy) {
  919. vfree(par->dio_vp);
  920. iounmap(info->screen_base);
  921. vmbus_free_mmio(par->mem->start, screen_fb_size);
  922. } else {
  923. hvfb_release_phymem(hdev, info->fix.smem_start,
  924. screen_fb_size);
  925. }
  926. par->mem = NULL;
  927. }
  928. static int hvfb_probe(struct hv_device *hdev,
  929. const struct hv_vmbus_device_id *dev_id)
  930. {
  931. struct fb_info *info;
  932. struct hvfb_par *par;
  933. int ret;
  934. info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
  935. if (!info)
  936. return -ENOMEM;
  937. par = info->par;
  938. par->info = info;
  939. par->fb_ready = false;
  940. par->need_docopy = true;
  941. init_completion(&par->wait);
  942. INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
  943. par->delayed_refresh = false;
  944. spin_lock_init(&par->delayed_refresh_lock);
  945. par->x1 = par->y1 = INT_MAX;
  946. par->x2 = par->y2 = 0;
  947. /* Connect to VSP */
  948. hv_set_drvdata(hdev, info);
  949. ret = synthvid_connect_vsp(hdev);
  950. if (ret) {
  951. pr_err("Unable to connect to VSP\n");
  952. goto error1;
  953. }
  954. hvfb_get_option(info);
  955. pr_info("Screen resolution: %dx%d, Color depth: %d, Frame buffer size: %d\n",
  956. screen_width, screen_height, screen_depth, screen_fb_size);
  957. ret = hvfb_getmem(hdev, info);
  958. if (ret) {
  959. pr_err("No memory for framebuffer\n");
  960. goto error2;
  961. }
  962. /* Set up fb_info */
  963. info->flags = FBINFO_DEFAULT;
  964. info->var.xres_virtual = info->var.xres = screen_width;
  965. info->var.yres_virtual = info->var.yres = screen_height;
  966. info->var.bits_per_pixel = screen_depth;
  967. if (info->var.bits_per_pixel == 16) {
  968. info->var.red = (struct fb_bitfield){11, 5, 0};
  969. info->var.green = (struct fb_bitfield){5, 6, 0};
  970. info->var.blue = (struct fb_bitfield){0, 5, 0};
  971. info->var.transp = (struct fb_bitfield){0, 0, 0};
  972. } else {
  973. info->var.red = (struct fb_bitfield){16, 8, 0};
  974. info->var.green = (struct fb_bitfield){8, 8, 0};
  975. info->var.blue = (struct fb_bitfield){0, 8, 0};
  976. info->var.transp = (struct fb_bitfield){24, 8, 0};
  977. }
  978. info->var.activate = FB_ACTIVATE_NOW;
  979. info->var.height = -1;
  980. info->var.width = -1;
  981. info->var.vmode = FB_VMODE_NONINTERLACED;
  982. strcpy(info->fix.id, KBUILD_MODNAME);
  983. info->fix.type = FB_TYPE_PACKED_PIXELS;
  984. info->fix.visual = FB_VISUAL_TRUECOLOR;
  985. info->fix.line_length = screen_width * screen_depth / 8;
  986. info->fix.accel = FB_ACCEL_NONE;
  987. info->fbops = &hvfb_ops;
  988. info->pseudo_palette = par->pseudo_palette;
  989. /* Initialize deferred IO */
  990. info->fbdefio = &synthvid_defio;
  991. fb_deferred_io_init(info);
  992. /* Send config to host */
  993. ret = synthvid_send_config(hdev);
  994. if (ret)
  995. goto error;
  996. ret = register_framebuffer(info);
  997. if (ret) {
  998. pr_err("Unable to register framebuffer\n");
  999. goto error;
  1000. }
  1001. par->fb_ready = true;
  1002. par->synchronous_fb = false;
  1003. par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
  1004. atomic_notifier_chain_register(&panic_notifier_list,
  1005. &par->hvfb_panic_nb);
  1006. return 0;
  1007. error:
  1008. fb_deferred_io_cleanup(info);
  1009. hvfb_putmem(hdev, info);
  1010. error2:
  1011. vmbus_close(hdev->channel);
  1012. error1:
  1013. cancel_delayed_work_sync(&par->dwork);
  1014. hv_set_drvdata(hdev, NULL);
  1015. framebuffer_release(info);
  1016. return ret;
  1017. }
  1018. static int hvfb_remove(struct hv_device *hdev)
  1019. {
  1020. struct fb_info *info = hv_get_drvdata(hdev);
  1021. struct hvfb_par *par = info->par;
  1022. atomic_notifier_chain_unregister(&panic_notifier_list,
  1023. &par->hvfb_panic_nb);
  1024. par->update = false;
  1025. par->fb_ready = false;
  1026. fb_deferred_io_cleanup(info);
  1027. unregister_framebuffer(info);
  1028. cancel_delayed_work_sync(&par->dwork);
  1029. vmbus_close(hdev->channel);
  1030. hv_set_drvdata(hdev, NULL);
  1031. hvfb_putmem(hdev, info);
  1032. framebuffer_release(info);
  1033. return 0;
  1034. }
  1035. static int hvfb_suspend(struct hv_device *hdev)
  1036. {
  1037. struct fb_info *info = hv_get_drvdata(hdev);
  1038. struct hvfb_par *par = info->par;
  1039. console_lock();
  1040. /* 1 means do suspend */
  1041. fb_set_suspend(info, 1);
  1042. cancel_delayed_work_sync(&par->dwork);
  1043. cancel_delayed_work_sync(&info->deferred_work);
  1044. par->update_saved = par->update;
  1045. par->update = false;
  1046. par->fb_ready = false;
  1047. vmbus_close(hdev->channel);
  1048. console_unlock();
  1049. return 0;
  1050. }
  1051. static int hvfb_resume(struct hv_device *hdev)
  1052. {
  1053. struct fb_info *info = hv_get_drvdata(hdev);
  1054. struct hvfb_par *par = info->par;
  1055. int ret;
  1056. console_lock();
  1057. ret = synthvid_connect_vsp(hdev);
  1058. if (ret != 0)
  1059. goto out;
  1060. ret = synthvid_send_config(hdev);
  1061. if (ret != 0) {
  1062. vmbus_close(hdev->channel);
  1063. goto out;
  1064. }
  1065. par->fb_ready = true;
  1066. par->update = par->update_saved;
  1067. schedule_delayed_work(&info->deferred_work, info->fbdefio->delay);
  1068. schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
  1069. /* 0 means do resume */
  1070. fb_set_suspend(info, 0);
  1071. out:
  1072. console_unlock();
  1073. return ret;
  1074. }
  1075. static const struct pci_device_id pci_stub_id_table[] = {
  1076. {
  1077. .vendor = PCI_VENDOR_ID_MICROSOFT,
  1078. .device = PCI_DEVICE_ID_HYPERV_VIDEO,
  1079. },
  1080. { /* end of list */ }
  1081. };
  1082. static const struct hv_vmbus_device_id id_table[] = {
  1083. /* Synthetic Video Device GUID */
  1084. {HV_SYNTHVID_GUID},
  1085. {}
  1086. };
  1087. MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
  1088. MODULE_DEVICE_TABLE(vmbus, id_table);
  1089. static struct hv_driver hvfb_drv = {
  1090. .name = KBUILD_MODNAME,
  1091. .id_table = id_table,
  1092. .probe = hvfb_probe,
  1093. .remove = hvfb_remove,
  1094. .suspend = hvfb_suspend,
  1095. .resume = hvfb_resume,
  1096. .driver = {
  1097. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1098. },
  1099. };
  1100. static int hvfb_pci_stub_probe(struct pci_dev *pdev,
  1101. const struct pci_device_id *ent)
  1102. {
  1103. return 0;
  1104. }
  1105. static void hvfb_pci_stub_remove(struct pci_dev *pdev)
  1106. {
  1107. }
  1108. static struct pci_driver hvfb_pci_stub_driver = {
  1109. .name = KBUILD_MODNAME,
  1110. .id_table = pci_stub_id_table,
  1111. .probe = hvfb_pci_stub_probe,
  1112. .remove = hvfb_pci_stub_remove,
  1113. .driver = {
  1114. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  1115. }
  1116. };
  1117. static int __init hvfb_drv_init(void)
  1118. {
  1119. int ret;
  1120. ret = vmbus_driver_register(&hvfb_drv);
  1121. if (ret != 0)
  1122. return ret;
  1123. ret = pci_register_driver(&hvfb_pci_stub_driver);
  1124. if (ret != 0) {
  1125. vmbus_driver_unregister(&hvfb_drv);
  1126. return ret;
  1127. }
  1128. return 0;
  1129. }
  1130. static void __exit hvfb_drv_exit(void)
  1131. {
  1132. pci_unregister_driver(&hvfb_pci_stub_driver);
  1133. vmbus_driver_unregister(&hvfb_drv);
  1134. }
  1135. module_init(hvfb_drv_init);
  1136. module_exit(hvfb_drv_exit);
  1137. MODULE_LICENSE("GPL");
  1138. MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");