hyperv_fb.c 21 KB

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