hyperv_fb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. * Copyright (c) 2012, Microsoft Corporation.
  3. *
  4. * Author:
  5. * Haiyang Zhang <haiyangz@microsoft.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  14. * NON INFRINGEMENT. See the GNU General Public License for more
  15. * details.
  16. */
  17. /*
  18. * Hyper-V Synthetic Video Frame Buffer Driver
  19. *
  20. * This is the driver for the Hyper-V Synthetic Video, which supports
  21. * screen resolution up to Full HD 1920x1080 with 32 bit color on Windows
  22. * Server 2012, and 1600x1200 with 16 bit color on Windows Server 2008 R2
  23. * or earlier.
  24. *
  25. * It also solves the double mouse cursor issue of the emulated video mode.
  26. *
  27. * The default screen resolution is 1152x864, which may be changed by a
  28. * kernel parameter:
  29. * video=hyperv_fb:<width>x<height>
  30. * For example: video=hyperv_fb:1280x1024
  31. *
  32. * Portrait orientation is also supported:
  33. * For example: video=hyperv_fb:864x1152
  34. */
  35. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  36. #include <linux/module.h>
  37. #include <linux/kernel.h>
  38. #include <linux/init.h>
  39. #include <linux/completion.h>
  40. #include <linux/fb.h>
  41. #include <linux/pci.h>
  42. #include <linux/hyperv.h>
  43. /* Hyper-V Synthetic Video Protocol definitions and structures */
  44. #define MAX_VMBUS_PKT_SIZE 0x4000
  45. #define SYNTHVID_VERSION(major, minor) ((minor) << 16 | (major))
  46. #define SYNTHVID_VERSION_WIN7 SYNTHVID_VERSION(3, 0)
  47. #define SYNTHVID_VERSION_WIN8 SYNTHVID_VERSION(3, 2)
  48. #define SYNTHVID_DEPTH_WIN7 16
  49. #define SYNTHVID_DEPTH_WIN8 32
  50. #define SYNTHVID_FB_SIZE_WIN7 (4 * 1024 * 1024)
  51. #define SYNTHVID_WIDTH_MAX_WIN7 1600
  52. #define SYNTHVID_HEIGHT_MAX_WIN7 1200
  53. #define SYNTHVID_FB_SIZE_WIN8 (8 * 1024 * 1024)
  54. #define PCI_VENDOR_ID_MICROSOFT 0x1414
  55. #define PCI_DEVICE_ID_HYPERV_VIDEO 0x5353
  56. enum pipe_msg_type {
  57. PIPE_MSG_INVALID,
  58. PIPE_MSG_DATA,
  59. PIPE_MSG_MAX
  60. };
  61. struct pipe_msg_hdr {
  62. u32 type;
  63. u32 size; /* size of message after this field */
  64. } __packed;
  65. enum synthvid_msg_type {
  66. SYNTHVID_ERROR = 0,
  67. SYNTHVID_VERSION_REQUEST = 1,
  68. SYNTHVID_VERSION_RESPONSE = 2,
  69. SYNTHVID_VRAM_LOCATION = 3,
  70. SYNTHVID_VRAM_LOCATION_ACK = 4,
  71. SYNTHVID_SITUATION_UPDATE = 5,
  72. SYNTHVID_SITUATION_UPDATE_ACK = 6,
  73. SYNTHVID_POINTER_POSITION = 7,
  74. SYNTHVID_POINTER_SHAPE = 8,
  75. SYNTHVID_FEATURE_CHANGE = 9,
  76. SYNTHVID_DIRT = 10,
  77. SYNTHVID_MAX = 11
  78. };
  79. struct synthvid_msg_hdr {
  80. u32 type;
  81. u32 size; /* size of this header + payload after this field*/
  82. } __packed;
  83. struct synthvid_version_req {
  84. u32 version;
  85. } __packed;
  86. struct synthvid_version_resp {
  87. u32 version;
  88. u8 is_accepted;
  89. u8 max_video_outputs;
  90. } __packed;
  91. struct synthvid_vram_location {
  92. u64 user_ctx;
  93. u8 is_vram_gpa_specified;
  94. u64 vram_gpa;
  95. } __packed;
  96. struct synthvid_vram_location_ack {
  97. u64 user_ctx;
  98. } __packed;
  99. struct video_output_situation {
  100. u8 active;
  101. u32 vram_offset;
  102. u8 depth_bits;
  103. u32 width_pixels;
  104. u32 height_pixels;
  105. u32 pitch_bytes;
  106. } __packed;
  107. struct synthvid_situation_update {
  108. u64 user_ctx;
  109. u8 video_output_count;
  110. struct video_output_situation video_output[1];
  111. } __packed;
  112. struct synthvid_situation_update_ack {
  113. u64 user_ctx;
  114. } __packed;
  115. struct synthvid_pointer_position {
  116. u8 is_visible;
  117. u8 video_output;
  118. s32 image_x;
  119. s32 image_y;
  120. } __packed;
  121. #define CURSOR_MAX_X 96
  122. #define CURSOR_MAX_Y 96
  123. #define CURSOR_ARGB_PIXEL_SIZE 4
  124. #define CURSOR_MAX_SIZE (CURSOR_MAX_X * CURSOR_MAX_Y * CURSOR_ARGB_PIXEL_SIZE)
  125. #define CURSOR_COMPLETE (-1)
  126. struct synthvid_pointer_shape {
  127. u8 part_idx;
  128. u8 is_argb;
  129. u32 width; /* CURSOR_MAX_X at most */
  130. u32 height; /* CURSOR_MAX_Y at most */
  131. u32 hot_x; /* hotspot relative to upper-left of pointer image */
  132. u32 hot_y;
  133. u8 data[4];
  134. } __packed;
  135. struct synthvid_feature_change {
  136. u8 is_dirt_needed;
  137. u8 is_ptr_pos_needed;
  138. u8 is_ptr_shape_needed;
  139. u8 is_situ_needed;
  140. } __packed;
  141. struct rect {
  142. s32 x1, y1; /* top left corner */
  143. s32 x2, y2; /* bottom right corner, exclusive */
  144. } __packed;
  145. struct synthvid_dirt {
  146. u8 video_output;
  147. u8 dirt_count;
  148. struct rect rect[1];
  149. } __packed;
  150. struct synthvid_msg {
  151. struct pipe_msg_hdr pipe_hdr;
  152. struct synthvid_msg_hdr vid_hdr;
  153. union {
  154. struct synthvid_version_req ver_req;
  155. struct synthvid_version_resp ver_resp;
  156. struct synthvid_vram_location vram;
  157. struct synthvid_vram_location_ack vram_ack;
  158. struct synthvid_situation_update situ;
  159. struct synthvid_situation_update_ack situ_ack;
  160. struct synthvid_pointer_position ptr_pos;
  161. struct synthvid_pointer_shape ptr_shape;
  162. struct synthvid_feature_change feature_chg;
  163. struct synthvid_dirt dirt;
  164. };
  165. } __packed;
  166. /* FB driver definitions and structures */
  167. #define HVFB_WIDTH 1152 /* default screen width */
  168. #define HVFB_HEIGHT 864 /* default screen height */
  169. #define HVFB_WIDTH_MIN 640
  170. #define HVFB_HEIGHT_MIN 480
  171. #define RING_BUFSIZE (256 * 1024)
  172. #define VSP_TIMEOUT (10 * HZ)
  173. #define HVFB_UPDATE_DELAY (HZ / 20)
  174. struct hvfb_par {
  175. struct fb_info *info;
  176. bool fb_ready; /* fb device is ready */
  177. struct completion wait;
  178. u32 synthvid_version;
  179. struct delayed_work dwork;
  180. bool update;
  181. u32 pseudo_palette[16];
  182. u8 init_buf[MAX_VMBUS_PKT_SIZE];
  183. u8 recv_buf[MAX_VMBUS_PKT_SIZE];
  184. };
  185. static uint screen_width = HVFB_WIDTH;
  186. static uint screen_height = HVFB_HEIGHT;
  187. static uint screen_depth;
  188. static uint screen_fb_size;
  189. /* Send message to Hyper-V host */
  190. static inline int synthvid_send(struct hv_device *hdev,
  191. struct synthvid_msg *msg)
  192. {
  193. static atomic64_t request_id = ATOMIC64_INIT(0);
  194. int ret;
  195. msg->pipe_hdr.type = PIPE_MSG_DATA;
  196. msg->pipe_hdr.size = msg->vid_hdr.size;
  197. ret = vmbus_sendpacket(hdev->channel, msg,
  198. msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
  199. atomic64_inc_return(&request_id),
  200. VM_PKT_DATA_INBAND, 0);
  201. if (ret)
  202. pr_err("Unable to send packet via vmbus\n");
  203. return ret;
  204. }
  205. /* Send screen resolution info to host */
  206. static int synthvid_send_situ(struct hv_device *hdev)
  207. {
  208. struct fb_info *info = hv_get_drvdata(hdev);
  209. struct synthvid_msg msg;
  210. if (!info)
  211. return -ENODEV;
  212. memset(&msg, 0, sizeof(struct synthvid_msg));
  213. msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
  214. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  215. sizeof(struct synthvid_situation_update);
  216. msg.situ.user_ctx = 0;
  217. msg.situ.video_output_count = 1;
  218. msg.situ.video_output[0].active = 1;
  219. msg.situ.video_output[0].vram_offset = 0;
  220. msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
  221. msg.situ.video_output[0].width_pixels = info->var.xres;
  222. msg.situ.video_output[0].height_pixels = info->var.yres;
  223. msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
  224. synthvid_send(hdev, &msg);
  225. return 0;
  226. }
  227. /* Send mouse pointer info to host */
  228. static int synthvid_send_ptr(struct hv_device *hdev)
  229. {
  230. struct synthvid_msg msg;
  231. memset(&msg, 0, sizeof(struct synthvid_msg));
  232. msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
  233. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  234. sizeof(struct synthvid_pointer_position);
  235. msg.ptr_pos.is_visible = 1;
  236. msg.ptr_pos.video_output = 0;
  237. msg.ptr_pos.image_x = 0;
  238. msg.ptr_pos.image_y = 0;
  239. synthvid_send(hdev, &msg);
  240. memset(&msg, 0, sizeof(struct synthvid_msg));
  241. msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
  242. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  243. sizeof(struct synthvid_pointer_shape);
  244. msg.ptr_shape.part_idx = CURSOR_COMPLETE;
  245. msg.ptr_shape.is_argb = 1;
  246. msg.ptr_shape.width = 1;
  247. msg.ptr_shape.height = 1;
  248. msg.ptr_shape.hot_x = 0;
  249. msg.ptr_shape.hot_y = 0;
  250. msg.ptr_shape.data[0] = 0;
  251. msg.ptr_shape.data[1] = 1;
  252. msg.ptr_shape.data[2] = 1;
  253. msg.ptr_shape.data[3] = 1;
  254. synthvid_send(hdev, &msg);
  255. return 0;
  256. }
  257. /* Send updated screen area (dirty rectangle) location to host */
  258. static int synthvid_update(struct fb_info *info)
  259. {
  260. struct hv_device *hdev = device_to_hv_device(info->device);
  261. struct synthvid_msg msg;
  262. memset(&msg, 0, sizeof(struct synthvid_msg));
  263. msg.vid_hdr.type = SYNTHVID_DIRT;
  264. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  265. sizeof(struct synthvid_dirt);
  266. msg.dirt.video_output = 0;
  267. msg.dirt.dirt_count = 1;
  268. msg.dirt.rect[0].x1 = 0;
  269. msg.dirt.rect[0].y1 = 0;
  270. msg.dirt.rect[0].x2 = info->var.xres;
  271. msg.dirt.rect[0].y2 = info->var.yres;
  272. synthvid_send(hdev, &msg);
  273. return 0;
  274. }
  275. /*
  276. * Actions on received messages from host:
  277. * Complete the wait event.
  278. * Or, reply with screen and cursor info.
  279. */
  280. static void synthvid_recv_sub(struct hv_device *hdev)
  281. {
  282. struct fb_info *info = hv_get_drvdata(hdev);
  283. struct hvfb_par *par;
  284. struct synthvid_msg *msg;
  285. if (!info)
  286. return;
  287. par = info->par;
  288. msg = (struct synthvid_msg *)par->recv_buf;
  289. /* Complete the wait event */
  290. if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
  291. msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
  292. memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
  293. complete(&par->wait);
  294. return;
  295. }
  296. /* Reply with screen and cursor info */
  297. if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
  298. if (par->fb_ready) {
  299. synthvid_send_ptr(hdev);
  300. synthvid_send_situ(hdev);
  301. }
  302. par->update = msg->feature_chg.is_dirt_needed;
  303. if (par->update)
  304. schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
  305. }
  306. }
  307. /* Receive callback for messages from the host */
  308. static void synthvid_receive(void *ctx)
  309. {
  310. struct hv_device *hdev = ctx;
  311. struct fb_info *info = hv_get_drvdata(hdev);
  312. struct hvfb_par *par;
  313. struct synthvid_msg *recv_buf;
  314. u32 bytes_recvd;
  315. u64 req_id;
  316. int ret;
  317. if (!info)
  318. return;
  319. par = info->par;
  320. recv_buf = (struct synthvid_msg *)par->recv_buf;
  321. do {
  322. ret = vmbus_recvpacket(hdev->channel, recv_buf,
  323. MAX_VMBUS_PKT_SIZE,
  324. &bytes_recvd, &req_id);
  325. if (bytes_recvd > 0 &&
  326. recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
  327. synthvid_recv_sub(hdev);
  328. } while (bytes_recvd > 0 && ret == 0);
  329. }
  330. /* Check synthetic video protocol version with the host */
  331. static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
  332. {
  333. struct fb_info *info = hv_get_drvdata(hdev);
  334. struct hvfb_par *par = info->par;
  335. struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
  336. int t, ret = 0;
  337. memset(msg, 0, sizeof(struct synthvid_msg));
  338. msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
  339. msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  340. sizeof(struct synthvid_version_req);
  341. msg->ver_req.version = ver;
  342. synthvid_send(hdev, msg);
  343. t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
  344. if (!t) {
  345. pr_err("Time out on waiting version response\n");
  346. ret = -ETIMEDOUT;
  347. goto out;
  348. }
  349. if (!msg->ver_resp.is_accepted) {
  350. ret = -ENODEV;
  351. goto out;
  352. }
  353. par->synthvid_version = ver;
  354. out:
  355. return ret;
  356. }
  357. /* Connect to VSP (Virtual Service Provider) on host */
  358. static int synthvid_connect_vsp(struct hv_device *hdev)
  359. {
  360. struct fb_info *info = hv_get_drvdata(hdev);
  361. struct hvfb_par *par = info->par;
  362. int ret;
  363. ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
  364. NULL, 0, synthvid_receive, hdev);
  365. if (ret) {
  366. pr_err("Unable to open vmbus channel\n");
  367. return ret;
  368. }
  369. /* Negotiate the protocol version with host */
  370. if (vmbus_proto_version == VERSION_WS2008 ||
  371. vmbus_proto_version == VERSION_WIN7)
  372. ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7);
  373. else
  374. ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
  375. if (ret) {
  376. pr_err("Synthetic video device version not accepted\n");
  377. goto error;
  378. }
  379. if (par->synthvid_version == SYNTHVID_VERSION_WIN7) {
  380. screen_depth = SYNTHVID_DEPTH_WIN7;
  381. screen_fb_size = SYNTHVID_FB_SIZE_WIN7;
  382. } else {
  383. screen_depth = SYNTHVID_DEPTH_WIN8;
  384. screen_fb_size = SYNTHVID_FB_SIZE_WIN8;
  385. }
  386. return 0;
  387. error:
  388. vmbus_close(hdev->channel);
  389. return ret;
  390. }
  391. /* Send VRAM and Situation messages to the host */
  392. static int synthvid_send_config(struct hv_device *hdev)
  393. {
  394. struct fb_info *info = hv_get_drvdata(hdev);
  395. struct hvfb_par *par = info->par;
  396. struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
  397. int t, ret = 0;
  398. /* Send VRAM location */
  399. memset(msg, 0, sizeof(struct synthvid_msg));
  400. msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
  401. msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  402. sizeof(struct synthvid_vram_location);
  403. msg->vram.user_ctx = msg->vram.vram_gpa = info->fix.smem_start;
  404. msg->vram.is_vram_gpa_specified = 1;
  405. synthvid_send(hdev, msg);
  406. t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
  407. if (!t) {
  408. pr_err("Time out on waiting vram location ack\n");
  409. ret = -ETIMEDOUT;
  410. goto out;
  411. }
  412. if (msg->vram_ack.user_ctx != info->fix.smem_start) {
  413. pr_err("Unable to set VRAM location\n");
  414. ret = -ENODEV;
  415. goto out;
  416. }
  417. /* Send pointer and situation update */
  418. synthvid_send_ptr(hdev);
  419. synthvid_send_situ(hdev);
  420. out:
  421. return ret;
  422. }
  423. /*
  424. * Delayed work callback:
  425. * It is called at HVFB_UPDATE_DELAY or longer time interval to process
  426. * screen updates. It is re-scheduled if further update is necessary.
  427. */
  428. static void hvfb_update_work(struct work_struct *w)
  429. {
  430. struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
  431. struct fb_info *info = par->info;
  432. if (par->fb_ready)
  433. synthvid_update(info);
  434. if (par->update)
  435. schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
  436. }
  437. /* Framebuffer operation handlers */
  438. static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  439. {
  440. if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
  441. var->xres > screen_width || var->yres > screen_height ||
  442. var->bits_per_pixel != screen_depth)
  443. return -EINVAL;
  444. var->xres_virtual = var->xres;
  445. var->yres_virtual = var->yres;
  446. return 0;
  447. }
  448. static int hvfb_set_par(struct fb_info *info)
  449. {
  450. struct hv_device *hdev = device_to_hv_device(info->device);
  451. return synthvid_send_situ(hdev);
  452. }
  453. static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
  454. {
  455. return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
  456. }
  457. static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  458. unsigned blue, unsigned transp, struct fb_info *info)
  459. {
  460. u32 *pal = info->pseudo_palette;
  461. if (regno > 15)
  462. return -EINVAL;
  463. pal[regno] = chan_to_field(red, &info->var.red)
  464. | chan_to_field(green, &info->var.green)
  465. | chan_to_field(blue, &info->var.blue)
  466. | chan_to_field(transp, &info->var.transp);
  467. return 0;
  468. }
  469. static struct fb_ops hvfb_ops = {
  470. .owner = THIS_MODULE,
  471. .fb_check_var = hvfb_check_var,
  472. .fb_set_par = hvfb_set_par,
  473. .fb_setcolreg = hvfb_setcolreg,
  474. .fb_fillrect = cfb_fillrect,
  475. .fb_copyarea = cfb_copyarea,
  476. .fb_imageblit = cfb_imageblit,
  477. };
  478. /* Get options from kernel paramenter "video=" */
  479. static void hvfb_get_option(struct fb_info *info)
  480. {
  481. struct hvfb_par *par = info->par;
  482. char *opt = NULL, *p;
  483. uint x = 0, y = 0;
  484. if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
  485. return;
  486. p = strsep(&opt, "x");
  487. if (!*p || kstrtouint(p, 0, &x) ||
  488. !opt || !*opt || kstrtouint(opt, 0, &y)) {
  489. pr_err("Screen option is invalid: skipped\n");
  490. return;
  491. }
  492. if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
  493. (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
  494. x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) ||
  495. (par->synthvid_version == SYNTHVID_VERSION_WIN7 &&
  496. (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) {
  497. pr_err("Screen resolution option is out of range: skipped\n");
  498. return;
  499. }
  500. screen_width = x;
  501. screen_height = y;
  502. return;
  503. }
  504. /* Get framebuffer memory from Hyper-V video pci space */
  505. static int hvfb_getmem(struct fb_info *info)
  506. {
  507. struct pci_dev *pdev;
  508. ulong fb_phys;
  509. void __iomem *fb_virt;
  510. pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
  511. PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
  512. if (!pdev) {
  513. pr_err("Unable to find PCI Hyper-V video\n");
  514. return -ENODEV;
  515. }
  516. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
  517. pci_resource_len(pdev, 0) < screen_fb_size)
  518. goto err1;
  519. fb_phys = pci_resource_end(pdev, 0) - screen_fb_size + 1;
  520. if (!request_mem_region(fb_phys, screen_fb_size, KBUILD_MODNAME))
  521. goto err1;
  522. fb_virt = ioremap(fb_phys, screen_fb_size);
  523. if (!fb_virt)
  524. goto err2;
  525. info->apertures = alloc_apertures(1);
  526. if (!info->apertures)
  527. goto err3;
  528. info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
  529. info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
  530. info->fix.smem_start = fb_phys;
  531. info->fix.smem_len = screen_fb_size;
  532. info->screen_base = fb_virt;
  533. info->screen_size = screen_fb_size;
  534. pci_dev_put(pdev);
  535. return 0;
  536. err3:
  537. iounmap(fb_virt);
  538. err2:
  539. release_mem_region(fb_phys, screen_fb_size);
  540. err1:
  541. pci_dev_put(pdev);
  542. return -ENOMEM;
  543. }
  544. /* Release the framebuffer */
  545. static void hvfb_putmem(struct fb_info *info)
  546. {
  547. iounmap(info->screen_base);
  548. release_mem_region(info->fix.smem_start, screen_fb_size);
  549. }
  550. static int hvfb_probe(struct hv_device *hdev,
  551. const struct hv_vmbus_device_id *dev_id)
  552. {
  553. struct fb_info *info;
  554. struct hvfb_par *par;
  555. int ret;
  556. info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
  557. if (!info) {
  558. pr_err("No memory for framebuffer info\n");
  559. return -ENOMEM;
  560. }
  561. par = info->par;
  562. par->info = info;
  563. par->fb_ready = false;
  564. init_completion(&par->wait);
  565. INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
  566. /* Connect to VSP */
  567. hv_set_drvdata(hdev, info);
  568. ret = synthvid_connect_vsp(hdev);
  569. if (ret) {
  570. pr_err("Unable to connect to VSP\n");
  571. goto error1;
  572. }
  573. ret = hvfb_getmem(info);
  574. if (ret) {
  575. pr_err("No memory for framebuffer\n");
  576. goto error2;
  577. }
  578. hvfb_get_option(info);
  579. pr_info("Screen resolution: %dx%d, Color depth: %d\n",
  580. screen_width, screen_height, screen_depth);
  581. /* Set up fb_info */
  582. info->flags = FBINFO_DEFAULT;
  583. info->var.xres_virtual = info->var.xres = screen_width;
  584. info->var.yres_virtual = info->var.yres = screen_height;
  585. info->var.bits_per_pixel = screen_depth;
  586. if (info->var.bits_per_pixel == 16) {
  587. info->var.red = (struct fb_bitfield){11, 5, 0};
  588. info->var.green = (struct fb_bitfield){5, 6, 0};
  589. info->var.blue = (struct fb_bitfield){0, 5, 0};
  590. info->var.transp = (struct fb_bitfield){0, 0, 0};
  591. } else {
  592. info->var.red = (struct fb_bitfield){16, 8, 0};
  593. info->var.green = (struct fb_bitfield){8, 8, 0};
  594. info->var.blue = (struct fb_bitfield){0, 8, 0};
  595. info->var.transp = (struct fb_bitfield){24, 8, 0};
  596. }
  597. info->var.activate = FB_ACTIVATE_NOW;
  598. info->var.height = -1;
  599. info->var.width = -1;
  600. info->var.vmode = FB_VMODE_NONINTERLACED;
  601. strcpy(info->fix.id, KBUILD_MODNAME);
  602. info->fix.type = FB_TYPE_PACKED_PIXELS;
  603. info->fix.visual = FB_VISUAL_TRUECOLOR;
  604. info->fix.line_length = screen_width * screen_depth / 8;
  605. info->fix.accel = FB_ACCEL_NONE;
  606. info->fbops = &hvfb_ops;
  607. info->pseudo_palette = par->pseudo_palette;
  608. /* Send config to host */
  609. ret = synthvid_send_config(hdev);
  610. if (ret)
  611. goto error;
  612. ret = register_framebuffer(info);
  613. if (ret) {
  614. pr_err("Unable to register framebuffer\n");
  615. goto error;
  616. }
  617. par->fb_ready = true;
  618. return 0;
  619. error:
  620. hvfb_putmem(info);
  621. error2:
  622. vmbus_close(hdev->channel);
  623. error1:
  624. cancel_delayed_work_sync(&par->dwork);
  625. hv_set_drvdata(hdev, NULL);
  626. framebuffer_release(info);
  627. return ret;
  628. }
  629. static int hvfb_remove(struct hv_device *hdev)
  630. {
  631. struct fb_info *info = hv_get_drvdata(hdev);
  632. struct hvfb_par *par = info->par;
  633. par->update = false;
  634. par->fb_ready = false;
  635. unregister_framebuffer(info);
  636. cancel_delayed_work_sync(&par->dwork);
  637. vmbus_close(hdev->channel);
  638. hv_set_drvdata(hdev, NULL);
  639. hvfb_putmem(info);
  640. framebuffer_release(info);
  641. return 0;
  642. }
  643. static const struct hv_vmbus_device_id id_table[] = {
  644. /* Synthetic Video Device GUID */
  645. {HV_SYNTHVID_GUID},
  646. {}
  647. };
  648. MODULE_DEVICE_TABLE(vmbus, id_table);
  649. static struct hv_driver hvfb_drv = {
  650. .name = KBUILD_MODNAME,
  651. .id_table = id_table,
  652. .probe = hvfb_probe,
  653. .remove = hvfb_remove,
  654. };
  655. static int __init hvfb_drv_init(void)
  656. {
  657. return vmbus_driver_register(&hvfb_drv);
  658. }
  659. static void __exit hvfb_drv_exit(void)
  660. {
  661. vmbus_driver_unregister(&hvfb_drv);
  662. }
  663. module_init(hvfb_drv_init);
  664. module_exit(hvfb_drv_exit);
  665. MODULE_LICENSE("GPL");
  666. MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");