PageRenderTime 238ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/video/fbdev/hyperv_fb.c

https://github.com/mturquette/linux
C | 961 lines | 707 code | 186 blank | 68 comment | 74 complexity | deba5a9c86359c570cdce6414dbb4f42 MD5 | raw file
  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. /* If true, the VSC notifies the VSP on every framebuffer change */
  187. bool synchronous_fb;
  188. struct notifier_block hvfb_panic_nb;
  189. };
  190. static uint screen_width = HVFB_WIDTH;
  191. static uint screen_height = HVFB_HEIGHT;
  192. static uint screen_depth;
  193. static uint screen_fb_size;
  194. /* Send message to Hyper-V host */
  195. static inline int synthvid_send(struct hv_device *hdev,
  196. struct synthvid_msg *msg)
  197. {
  198. static atomic64_t request_id = ATOMIC64_INIT(0);
  199. int ret;
  200. msg->pipe_hdr.type = PIPE_MSG_DATA;
  201. msg->pipe_hdr.size = msg->vid_hdr.size;
  202. ret = vmbus_sendpacket(hdev->channel, msg,
  203. msg->vid_hdr.size + sizeof(struct pipe_msg_hdr),
  204. atomic64_inc_return(&request_id),
  205. VM_PKT_DATA_INBAND, 0);
  206. if (ret)
  207. pr_err("Unable to send packet via vmbus\n");
  208. return ret;
  209. }
  210. /* Send screen resolution info to host */
  211. static int synthvid_send_situ(struct hv_device *hdev)
  212. {
  213. struct fb_info *info = hv_get_drvdata(hdev);
  214. struct synthvid_msg msg;
  215. if (!info)
  216. return -ENODEV;
  217. memset(&msg, 0, sizeof(struct synthvid_msg));
  218. msg.vid_hdr.type = SYNTHVID_SITUATION_UPDATE;
  219. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  220. sizeof(struct synthvid_situation_update);
  221. msg.situ.user_ctx = 0;
  222. msg.situ.video_output_count = 1;
  223. msg.situ.video_output[0].active = 1;
  224. msg.situ.video_output[0].vram_offset = 0;
  225. msg.situ.video_output[0].depth_bits = info->var.bits_per_pixel;
  226. msg.situ.video_output[0].width_pixels = info->var.xres;
  227. msg.situ.video_output[0].height_pixels = info->var.yres;
  228. msg.situ.video_output[0].pitch_bytes = info->fix.line_length;
  229. synthvid_send(hdev, &msg);
  230. return 0;
  231. }
  232. /* Send mouse pointer info to host */
  233. static int synthvid_send_ptr(struct hv_device *hdev)
  234. {
  235. struct synthvid_msg msg;
  236. memset(&msg, 0, sizeof(struct synthvid_msg));
  237. msg.vid_hdr.type = SYNTHVID_POINTER_POSITION;
  238. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  239. sizeof(struct synthvid_pointer_position);
  240. msg.ptr_pos.is_visible = 1;
  241. msg.ptr_pos.video_output = 0;
  242. msg.ptr_pos.image_x = 0;
  243. msg.ptr_pos.image_y = 0;
  244. synthvid_send(hdev, &msg);
  245. memset(&msg, 0, sizeof(struct synthvid_msg));
  246. msg.vid_hdr.type = SYNTHVID_POINTER_SHAPE;
  247. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  248. sizeof(struct synthvid_pointer_shape);
  249. msg.ptr_shape.part_idx = CURSOR_COMPLETE;
  250. msg.ptr_shape.is_argb = 1;
  251. msg.ptr_shape.width = 1;
  252. msg.ptr_shape.height = 1;
  253. msg.ptr_shape.hot_x = 0;
  254. msg.ptr_shape.hot_y = 0;
  255. msg.ptr_shape.data[0] = 0;
  256. msg.ptr_shape.data[1] = 1;
  257. msg.ptr_shape.data[2] = 1;
  258. msg.ptr_shape.data[3] = 1;
  259. synthvid_send(hdev, &msg);
  260. return 0;
  261. }
  262. /* Send updated screen area (dirty rectangle) location to host */
  263. static int synthvid_update(struct fb_info *info)
  264. {
  265. struct hv_device *hdev = device_to_hv_device(info->device);
  266. struct synthvid_msg msg;
  267. memset(&msg, 0, sizeof(struct synthvid_msg));
  268. msg.vid_hdr.type = SYNTHVID_DIRT;
  269. msg.vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  270. sizeof(struct synthvid_dirt);
  271. msg.dirt.video_output = 0;
  272. msg.dirt.dirt_count = 1;
  273. msg.dirt.rect[0].x1 = 0;
  274. msg.dirt.rect[0].y1 = 0;
  275. msg.dirt.rect[0].x2 = info->var.xres;
  276. msg.dirt.rect[0].y2 = info->var.yres;
  277. synthvid_send(hdev, &msg);
  278. return 0;
  279. }
  280. /*
  281. * Actions on received messages from host:
  282. * Complete the wait event.
  283. * Or, reply with screen and cursor info.
  284. */
  285. static void synthvid_recv_sub(struct hv_device *hdev)
  286. {
  287. struct fb_info *info = hv_get_drvdata(hdev);
  288. struct hvfb_par *par;
  289. struct synthvid_msg *msg;
  290. if (!info)
  291. return;
  292. par = info->par;
  293. msg = (struct synthvid_msg *)par->recv_buf;
  294. /* Complete the wait event */
  295. if (msg->vid_hdr.type == SYNTHVID_VERSION_RESPONSE ||
  296. msg->vid_hdr.type == SYNTHVID_VRAM_LOCATION_ACK) {
  297. memcpy(par->init_buf, msg, MAX_VMBUS_PKT_SIZE);
  298. complete(&par->wait);
  299. return;
  300. }
  301. /* Reply with screen and cursor info */
  302. if (msg->vid_hdr.type == SYNTHVID_FEATURE_CHANGE) {
  303. if (par->fb_ready) {
  304. synthvid_send_ptr(hdev);
  305. synthvid_send_situ(hdev);
  306. }
  307. par->update = msg->feature_chg.is_dirt_needed;
  308. if (par->update)
  309. schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
  310. }
  311. }
  312. /* Receive callback for messages from the host */
  313. static void synthvid_receive(void *ctx)
  314. {
  315. struct hv_device *hdev = ctx;
  316. struct fb_info *info = hv_get_drvdata(hdev);
  317. struct hvfb_par *par;
  318. struct synthvid_msg *recv_buf;
  319. u32 bytes_recvd;
  320. u64 req_id;
  321. int ret;
  322. if (!info)
  323. return;
  324. par = info->par;
  325. recv_buf = (struct synthvid_msg *)par->recv_buf;
  326. do {
  327. ret = vmbus_recvpacket(hdev->channel, recv_buf,
  328. MAX_VMBUS_PKT_SIZE,
  329. &bytes_recvd, &req_id);
  330. if (bytes_recvd > 0 &&
  331. recv_buf->pipe_hdr.type == PIPE_MSG_DATA)
  332. synthvid_recv_sub(hdev);
  333. } while (bytes_recvd > 0 && ret == 0);
  334. }
  335. /* Check synthetic video protocol version with the host */
  336. static int synthvid_negotiate_ver(struct hv_device *hdev, u32 ver)
  337. {
  338. struct fb_info *info = hv_get_drvdata(hdev);
  339. struct hvfb_par *par = info->par;
  340. struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
  341. int ret = 0;
  342. unsigned long t;
  343. memset(msg, 0, sizeof(struct synthvid_msg));
  344. msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
  345. msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  346. sizeof(struct synthvid_version_req);
  347. msg->ver_req.version = ver;
  348. synthvid_send(hdev, msg);
  349. t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
  350. if (!t) {
  351. pr_err("Time out on waiting version response\n");
  352. ret = -ETIMEDOUT;
  353. goto out;
  354. }
  355. if (!msg->ver_resp.is_accepted) {
  356. ret = -ENODEV;
  357. goto out;
  358. }
  359. par->synthvid_version = ver;
  360. out:
  361. return ret;
  362. }
  363. /* Connect to VSP (Virtual Service Provider) on host */
  364. static int synthvid_connect_vsp(struct hv_device *hdev)
  365. {
  366. struct fb_info *info = hv_get_drvdata(hdev);
  367. struct hvfb_par *par = info->par;
  368. int ret;
  369. ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
  370. NULL, 0, synthvid_receive, hdev);
  371. if (ret) {
  372. pr_err("Unable to open vmbus channel\n");
  373. return ret;
  374. }
  375. /* Negotiate the protocol version with host */
  376. if (vmbus_proto_version == VERSION_WS2008 ||
  377. vmbus_proto_version == VERSION_WIN7)
  378. ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7);
  379. else
  380. ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
  381. if (ret) {
  382. pr_err("Synthetic video device version not accepted\n");
  383. goto error;
  384. }
  385. if (par->synthvid_version == SYNTHVID_VERSION_WIN7)
  386. screen_depth = SYNTHVID_DEPTH_WIN7;
  387. else
  388. screen_depth = SYNTHVID_DEPTH_WIN8;
  389. screen_fb_size = hdev->channel->offermsg.offer.
  390. mmio_megabytes * 1024 * 1024;
  391. return 0;
  392. error:
  393. vmbus_close(hdev->channel);
  394. return ret;
  395. }
  396. /* Send VRAM and Situation messages to the host */
  397. static int synthvid_send_config(struct hv_device *hdev)
  398. {
  399. struct fb_info *info = hv_get_drvdata(hdev);
  400. struct hvfb_par *par = info->par;
  401. struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
  402. int ret = 0;
  403. unsigned long t;
  404. /* Send VRAM location */
  405. memset(msg, 0, sizeof(struct synthvid_msg));
  406. msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
  407. msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  408. sizeof(struct synthvid_vram_location);
  409. msg->vram.user_ctx = msg->vram.vram_gpa = info->fix.smem_start;
  410. msg->vram.is_vram_gpa_specified = 1;
  411. synthvid_send(hdev, msg);
  412. t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
  413. if (!t) {
  414. pr_err("Time out on waiting vram location ack\n");
  415. ret = -ETIMEDOUT;
  416. goto out;
  417. }
  418. if (msg->vram_ack.user_ctx != info->fix.smem_start) {
  419. pr_err("Unable to set VRAM location\n");
  420. ret = -ENODEV;
  421. goto out;
  422. }
  423. /* Send pointer and situation update */
  424. synthvid_send_ptr(hdev);
  425. synthvid_send_situ(hdev);
  426. out:
  427. return ret;
  428. }
  429. /*
  430. * Delayed work callback:
  431. * It is called at HVFB_UPDATE_DELAY or longer time interval to process
  432. * screen updates. It is re-scheduled if further update is necessary.
  433. */
  434. static void hvfb_update_work(struct work_struct *w)
  435. {
  436. struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
  437. struct fb_info *info = par->info;
  438. if (par->fb_ready)
  439. synthvid_update(info);
  440. if (par->update)
  441. schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
  442. }
  443. static int hvfb_on_panic(struct notifier_block *nb,
  444. unsigned long e, void *p)
  445. {
  446. struct hvfb_par *par;
  447. struct fb_info *info;
  448. par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
  449. par->synchronous_fb = true;
  450. info = par->info;
  451. synthvid_update(info);
  452. return NOTIFY_DONE;
  453. }
  454. /* Framebuffer operation handlers */
  455. static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  456. {
  457. if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
  458. var->xres > screen_width || var->yres > screen_height ||
  459. var->bits_per_pixel != screen_depth)
  460. return -EINVAL;
  461. var->xres_virtual = var->xres;
  462. var->yres_virtual = var->yres;
  463. return 0;
  464. }
  465. static int hvfb_set_par(struct fb_info *info)
  466. {
  467. struct hv_device *hdev = device_to_hv_device(info->device);
  468. return synthvid_send_situ(hdev);
  469. }
  470. static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
  471. {
  472. return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
  473. }
  474. static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  475. unsigned blue, unsigned transp, struct fb_info *info)
  476. {
  477. u32 *pal = info->pseudo_palette;
  478. if (regno > 15)
  479. return -EINVAL;
  480. pal[regno] = chan_to_field(red, &info->var.red)
  481. | chan_to_field(green, &info->var.green)
  482. | chan_to_field(blue, &info->var.blue)
  483. | chan_to_field(transp, &info->var.transp);
  484. return 0;
  485. }
  486. static int hvfb_blank(int blank, struct fb_info *info)
  487. {
  488. return 1; /* get fb_blank to set the colormap to all black */
  489. }
  490. static void hvfb_cfb_fillrect(struct fb_info *p,
  491. const struct fb_fillrect *rect)
  492. {
  493. struct hvfb_par *par = p->par;
  494. cfb_fillrect(p, rect);
  495. if (par->synchronous_fb)
  496. synthvid_update(p);
  497. }
  498. static void hvfb_cfb_copyarea(struct fb_info *p,
  499. const struct fb_copyarea *area)
  500. {
  501. struct hvfb_par *par = p->par;
  502. cfb_copyarea(p, area);
  503. if (par->synchronous_fb)
  504. synthvid_update(p);
  505. }
  506. static void hvfb_cfb_imageblit(struct fb_info *p,
  507. const struct fb_image *image)
  508. {
  509. struct hvfb_par *par = p->par;
  510. cfb_imageblit(p, image);
  511. if (par->synchronous_fb)
  512. synthvid_update(p);
  513. }
  514. static struct fb_ops hvfb_ops = {
  515. .owner = THIS_MODULE,
  516. .fb_check_var = hvfb_check_var,
  517. .fb_set_par = hvfb_set_par,
  518. .fb_setcolreg = hvfb_setcolreg,
  519. .fb_fillrect = hvfb_cfb_fillrect,
  520. .fb_copyarea = hvfb_cfb_copyarea,
  521. .fb_imageblit = hvfb_cfb_imageblit,
  522. .fb_blank = hvfb_blank,
  523. };
  524. /* Get options from kernel paramenter "video=" */
  525. static void hvfb_get_option(struct fb_info *info)
  526. {
  527. struct hvfb_par *par = info->par;
  528. char *opt = NULL, *p;
  529. uint x = 0, y = 0;
  530. if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
  531. return;
  532. p = strsep(&opt, "x");
  533. if (!*p || kstrtouint(p, 0, &x) ||
  534. !opt || !*opt || kstrtouint(opt, 0, &y)) {
  535. pr_err("Screen option is invalid: skipped\n");
  536. return;
  537. }
  538. if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
  539. (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
  540. x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) ||
  541. (par->synthvid_version == SYNTHVID_VERSION_WIN7 &&
  542. (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) {
  543. pr_err("Screen resolution option is out of range: skipped\n");
  544. return;
  545. }
  546. screen_width = x;
  547. screen_height = y;
  548. return;
  549. }
  550. /* Get framebuffer memory from Hyper-V video pci space */
  551. static int hvfb_getmem(struct hv_device *hdev, struct fb_info *info)
  552. {
  553. struct hvfb_par *par = info->par;
  554. struct pci_dev *pdev = NULL;
  555. void __iomem *fb_virt;
  556. int gen2vm = efi_enabled(EFI_BOOT);
  557. resource_size_t pot_start, pot_end;
  558. int ret;
  559. if (gen2vm) {
  560. pot_start = 0;
  561. pot_end = -1;
  562. } else {
  563. pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
  564. PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
  565. if (!pdev) {
  566. pr_err("Unable to find PCI Hyper-V video\n");
  567. return -ENODEV;
  568. }
  569. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
  570. pci_resource_len(pdev, 0) < screen_fb_size)
  571. goto err1;
  572. pot_end = pci_resource_end(pdev, 0);
  573. pot_start = pot_end - screen_fb_size + 1;
  574. }
  575. ret = vmbus_allocate_mmio(&par->mem, hdev, pot_start, pot_end,
  576. screen_fb_size, 0x100000, true);
  577. if (ret != 0) {
  578. pr_err("Unable to allocate framebuffer memory\n");
  579. goto err1;
  580. }
  581. fb_virt = ioremap(par->mem->start, screen_fb_size);
  582. if (!fb_virt)
  583. goto err2;
  584. info->apertures = alloc_apertures(1);
  585. if (!info->apertures)
  586. goto err3;
  587. if (gen2vm) {
  588. info->apertures->ranges[0].base = screen_info.lfb_base;
  589. info->apertures->ranges[0].size = screen_info.lfb_size;
  590. remove_conflicting_framebuffers(info->apertures,
  591. KBUILD_MODNAME, false);
  592. } else {
  593. info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
  594. info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
  595. }
  596. info->fix.smem_start = par->mem->start;
  597. info->fix.smem_len = screen_fb_size;
  598. info->screen_base = fb_virt;
  599. info->screen_size = screen_fb_size;
  600. if (!gen2vm)
  601. pci_dev_put(pdev);
  602. return 0;
  603. err3:
  604. iounmap(fb_virt);
  605. err2:
  606. vmbus_free_mmio(par->mem->start, screen_fb_size);
  607. par->mem = NULL;
  608. err1:
  609. if (!gen2vm)
  610. pci_dev_put(pdev);
  611. return -ENOMEM;
  612. }
  613. /* Release the framebuffer */
  614. static void hvfb_putmem(struct fb_info *info)
  615. {
  616. struct hvfb_par *par = info->par;
  617. iounmap(info->screen_base);
  618. vmbus_free_mmio(par->mem->start, screen_fb_size);
  619. par->mem = NULL;
  620. }
  621. static int hvfb_probe(struct hv_device *hdev,
  622. const struct hv_vmbus_device_id *dev_id)
  623. {
  624. struct fb_info *info;
  625. struct hvfb_par *par;
  626. int ret;
  627. info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
  628. if (!info) {
  629. pr_err("No memory for framebuffer info\n");
  630. return -ENOMEM;
  631. }
  632. par = info->par;
  633. par->info = info;
  634. par->fb_ready = false;
  635. init_completion(&par->wait);
  636. INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
  637. /* Connect to VSP */
  638. hv_set_drvdata(hdev, info);
  639. ret = synthvid_connect_vsp(hdev);
  640. if (ret) {
  641. pr_err("Unable to connect to VSP\n");
  642. goto error1;
  643. }
  644. ret = hvfb_getmem(hdev, info);
  645. if (ret) {
  646. pr_err("No memory for framebuffer\n");
  647. goto error2;
  648. }
  649. hvfb_get_option(info);
  650. pr_info("Screen resolution: %dx%d, Color depth: %d\n",
  651. screen_width, screen_height, screen_depth);
  652. /* Set up fb_info */
  653. info->flags = FBINFO_DEFAULT;
  654. info->var.xres_virtual = info->var.xres = screen_width;
  655. info->var.yres_virtual = info->var.yres = screen_height;
  656. info->var.bits_per_pixel = screen_depth;
  657. if (info->var.bits_per_pixel == 16) {
  658. info->var.red = (struct fb_bitfield){11, 5, 0};
  659. info->var.green = (struct fb_bitfield){5, 6, 0};
  660. info->var.blue = (struct fb_bitfield){0, 5, 0};
  661. info->var.transp = (struct fb_bitfield){0, 0, 0};
  662. } else {
  663. info->var.red = (struct fb_bitfield){16, 8, 0};
  664. info->var.green = (struct fb_bitfield){8, 8, 0};
  665. info->var.blue = (struct fb_bitfield){0, 8, 0};
  666. info->var.transp = (struct fb_bitfield){24, 8, 0};
  667. }
  668. info->var.activate = FB_ACTIVATE_NOW;
  669. info->var.height = -1;
  670. info->var.width = -1;
  671. info->var.vmode = FB_VMODE_NONINTERLACED;
  672. strcpy(info->fix.id, KBUILD_MODNAME);
  673. info->fix.type = FB_TYPE_PACKED_PIXELS;
  674. info->fix.visual = FB_VISUAL_TRUECOLOR;
  675. info->fix.line_length = screen_width * screen_depth / 8;
  676. info->fix.accel = FB_ACCEL_NONE;
  677. info->fbops = &hvfb_ops;
  678. info->pseudo_palette = par->pseudo_palette;
  679. /* Send config to host */
  680. ret = synthvid_send_config(hdev);
  681. if (ret)
  682. goto error;
  683. ret = register_framebuffer(info);
  684. if (ret) {
  685. pr_err("Unable to register framebuffer\n");
  686. goto error;
  687. }
  688. par->fb_ready = true;
  689. par->synchronous_fb = false;
  690. par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
  691. atomic_notifier_chain_register(&panic_notifier_list,
  692. &par->hvfb_panic_nb);
  693. return 0;
  694. error:
  695. hvfb_putmem(info);
  696. error2:
  697. vmbus_close(hdev->channel);
  698. error1:
  699. cancel_delayed_work_sync(&par->dwork);
  700. hv_set_drvdata(hdev, NULL);
  701. framebuffer_release(info);
  702. return ret;
  703. }
  704. static int hvfb_remove(struct hv_device *hdev)
  705. {
  706. struct fb_info *info = hv_get_drvdata(hdev);
  707. struct hvfb_par *par = info->par;
  708. atomic_notifier_chain_unregister(&panic_notifier_list,
  709. &par->hvfb_panic_nb);
  710. par->update = false;
  711. par->fb_ready = false;
  712. unregister_framebuffer(info);
  713. cancel_delayed_work_sync(&par->dwork);
  714. vmbus_close(hdev->channel);
  715. hv_set_drvdata(hdev, NULL);
  716. hvfb_putmem(info);
  717. framebuffer_release(info);
  718. return 0;
  719. }
  720. static const struct pci_device_id pci_stub_id_table[] = {
  721. {
  722. .vendor = PCI_VENDOR_ID_MICROSOFT,
  723. .device = PCI_DEVICE_ID_HYPERV_VIDEO,
  724. },
  725. { /* end of list */ }
  726. };
  727. static const struct hv_vmbus_device_id id_table[] = {
  728. /* Synthetic Video Device GUID */
  729. {HV_SYNTHVID_GUID},
  730. {}
  731. };
  732. MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
  733. MODULE_DEVICE_TABLE(vmbus, id_table);
  734. static struct hv_driver hvfb_drv = {
  735. .name = KBUILD_MODNAME,
  736. .id_table = id_table,
  737. .probe = hvfb_probe,
  738. .remove = hvfb_remove,
  739. };
  740. static int hvfb_pci_stub_probe(struct pci_dev *pdev,
  741. const struct pci_device_id *ent)
  742. {
  743. return 0;
  744. }
  745. static void hvfb_pci_stub_remove(struct pci_dev *pdev)
  746. {
  747. }
  748. static struct pci_driver hvfb_pci_stub_driver = {
  749. .name = KBUILD_MODNAME,
  750. .id_table = pci_stub_id_table,
  751. .probe = hvfb_pci_stub_probe,
  752. .remove = hvfb_pci_stub_remove,
  753. };
  754. static int __init hvfb_drv_init(void)
  755. {
  756. int ret;
  757. ret = vmbus_driver_register(&hvfb_drv);
  758. if (ret != 0)
  759. return ret;
  760. ret = pci_register_driver(&hvfb_pci_stub_driver);
  761. if (ret != 0) {
  762. vmbus_driver_unregister(&hvfb_drv);
  763. return ret;
  764. }
  765. return 0;
  766. }
  767. static void __exit hvfb_drv_exit(void)
  768. {
  769. pci_unregister_driver(&hvfb_pci_stub_driver);
  770. vmbus_driver_unregister(&hvfb_drv);
  771. }
  772. module_init(hvfb_drv_init);
  773. module_exit(hvfb_drv_exit);
  774. MODULE_LICENSE("GPL");
  775. MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");