hyperv_fb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 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. int ret;
  558. par->mem.name = KBUILD_MODNAME;
  559. par->mem.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  560. if (gen2vm) {
  561. ret = allocate_resource(&hyperv_mmio, &par->mem,
  562. screen_fb_size,
  563. 0, -1,
  564. screen_fb_size,
  565. NULL, NULL);
  566. if (ret != 0) {
  567. pr_err("Unable to allocate framebuffer memory\n");
  568. return -ENODEV;
  569. }
  570. } else {
  571. pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
  572. PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
  573. if (!pdev) {
  574. pr_err("Unable to find PCI Hyper-V video\n");
  575. return -ENODEV;
  576. }
  577. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
  578. pci_resource_len(pdev, 0) < screen_fb_size)
  579. goto err1;
  580. par->mem.end = pci_resource_end(pdev, 0);
  581. par->mem.start = par->mem.end - screen_fb_size + 1;
  582. ret = request_resource(&pdev->resource[0], &par->mem);
  583. if (ret != 0) {
  584. pr_err("Unable to request framebuffer memory\n");
  585. goto err1;
  586. }
  587. }
  588. fb_virt = ioremap(par->mem.start, screen_fb_size);
  589. if (!fb_virt)
  590. goto err2;
  591. info->apertures = alloc_apertures(1);
  592. if (!info->apertures)
  593. goto err3;
  594. if (gen2vm) {
  595. info->apertures->ranges[0].base = screen_info.lfb_base;
  596. info->apertures->ranges[0].size = screen_info.lfb_size;
  597. remove_conflicting_framebuffers(info->apertures,
  598. KBUILD_MODNAME, false);
  599. } else {
  600. info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
  601. info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
  602. }
  603. info->fix.smem_start = par->mem.start;
  604. info->fix.smem_len = screen_fb_size;
  605. info->screen_base = fb_virt;
  606. info->screen_size = screen_fb_size;
  607. if (!gen2vm)
  608. pci_dev_put(pdev);
  609. return 0;
  610. err3:
  611. iounmap(fb_virt);
  612. err2:
  613. release_resource(&par->mem);
  614. err1:
  615. if (!gen2vm)
  616. pci_dev_put(pdev);
  617. return -ENOMEM;
  618. }
  619. /* Release the framebuffer */
  620. static void hvfb_putmem(struct fb_info *info)
  621. {
  622. struct hvfb_par *par = info->par;
  623. iounmap(info->screen_base);
  624. release_resource(&par->mem);
  625. }
  626. static int hvfb_probe(struct hv_device *hdev,
  627. const struct hv_vmbus_device_id *dev_id)
  628. {
  629. struct fb_info *info;
  630. struct hvfb_par *par;
  631. int ret;
  632. info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
  633. if (!info) {
  634. pr_err("No memory for framebuffer info\n");
  635. return -ENOMEM;
  636. }
  637. par = info->par;
  638. par->info = info;
  639. par->fb_ready = false;
  640. init_completion(&par->wait);
  641. INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
  642. /* Connect to VSP */
  643. hv_set_drvdata(hdev, info);
  644. ret = synthvid_connect_vsp(hdev);
  645. if (ret) {
  646. pr_err("Unable to connect to VSP\n");
  647. goto error1;
  648. }
  649. ret = hvfb_getmem(info);
  650. if (ret) {
  651. pr_err("No memory for framebuffer\n");
  652. goto error2;
  653. }
  654. hvfb_get_option(info);
  655. pr_info("Screen resolution: %dx%d, Color depth: %d\n",
  656. screen_width, screen_height, screen_depth);
  657. /* Set up fb_info */
  658. info->flags = FBINFO_DEFAULT;
  659. info->var.xres_virtual = info->var.xres = screen_width;
  660. info->var.yres_virtual = info->var.yres = screen_height;
  661. info->var.bits_per_pixel = screen_depth;
  662. if (info->var.bits_per_pixel == 16) {
  663. info->var.red = (struct fb_bitfield){11, 5, 0};
  664. info->var.green = (struct fb_bitfield){5, 6, 0};
  665. info->var.blue = (struct fb_bitfield){0, 5, 0};
  666. info->var.transp = (struct fb_bitfield){0, 0, 0};
  667. } else {
  668. info->var.red = (struct fb_bitfield){16, 8, 0};
  669. info->var.green = (struct fb_bitfield){8, 8, 0};
  670. info->var.blue = (struct fb_bitfield){0, 8, 0};
  671. info->var.transp = (struct fb_bitfield){24, 8, 0};
  672. }
  673. info->var.activate = FB_ACTIVATE_NOW;
  674. info->var.height = -1;
  675. info->var.width = -1;
  676. info->var.vmode = FB_VMODE_NONINTERLACED;
  677. strcpy(info->fix.id, KBUILD_MODNAME);
  678. info->fix.type = FB_TYPE_PACKED_PIXELS;
  679. info->fix.visual = FB_VISUAL_TRUECOLOR;
  680. info->fix.line_length = screen_width * screen_depth / 8;
  681. info->fix.accel = FB_ACCEL_NONE;
  682. info->fbops = &hvfb_ops;
  683. info->pseudo_palette = par->pseudo_palette;
  684. /* Send config to host */
  685. ret = synthvid_send_config(hdev);
  686. if (ret)
  687. goto error;
  688. ret = register_framebuffer(info);
  689. if (ret) {
  690. pr_err("Unable to register framebuffer\n");
  691. goto error;
  692. }
  693. par->fb_ready = true;
  694. par->synchronous_fb = false;
  695. par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
  696. atomic_notifier_chain_register(&panic_notifier_list,
  697. &par->hvfb_panic_nb);
  698. return 0;
  699. error:
  700. hvfb_putmem(info);
  701. error2:
  702. vmbus_close(hdev->channel);
  703. error1:
  704. cancel_delayed_work_sync(&par->dwork);
  705. hv_set_drvdata(hdev, NULL);
  706. framebuffer_release(info);
  707. return ret;
  708. }
  709. static int hvfb_remove(struct hv_device *hdev)
  710. {
  711. struct fb_info *info = hv_get_drvdata(hdev);
  712. struct hvfb_par *par = info->par;
  713. atomic_notifier_chain_unregister(&panic_notifier_list,
  714. &par->hvfb_panic_nb);
  715. par->update = false;
  716. par->fb_ready = false;
  717. unregister_framebuffer(info);
  718. cancel_delayed_work_sync(&par->dwork);
  719. vmbus_close(hdev->channel);
  720. hv_set_drvdata(hdev, NULL);
  721. hvfb_putmem(info);
  722. framebuffer_release(info);
  723. return 0;
  724. }
  725. static const struct pci_device_id pci_stub_id_table[] = {
  726. {
  727. .vendor = PCI_VENDOR_ID_MICROSOFT,
  728. .device = PCI_DEVICE_ID_HYPERV_VIDEO,
  729. },
  730. { /* end of list */ }
  731. };
  732. static const struct hv_vmbus_device_id id_table[] = {
  733. /* Synthetic Video Device GUID */
  734. {HV_SYNTHVID_GUID},
  735. {}
  736. };
  737. MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
  738. MODULE_DEVICE_TABLE(vmbus, id_table);
  739. static struct hv_driver hvfb_drv = {
  740. .name = KBUILD_MODNAME,
  741. .id_table = id_table,
  742. .probe = hvfb_probe,
  743. .remove = hvfb_remove,
  744. };
  745. static int hvfb_pci_stub_probe(struct pci_dev *pdev,
  746. const struct pci_device_id *ent)
  747. {
  748. return 0;
  749. }
  750. static void hvfb_pci_stub_remove(struct pci_dev *pdev)
  751. {
  752. }
  753. static struct pci_driver hvfb_pci_stub_driver = {
  754. .name = KBUILD_MODNAME,
  755. .id_table = pci_stub_id_table,
  756. .probe = hvfb_pci_stub_probe,
  757. .remove = hvfb_pci_stub_remove,
  758. };
  759. static int __init hvfb_drv_init(void)
  760. {
  761. int ret;
  762. ret = vmbus_driver_register(&hvfb_drv);
  763. if (ret != 0)
  764. return ret;
  765. ret = pci_register_driver(&hvfb_pci_stub_driver);
  766. if (ret != 0) {
  767. vmbus_driver_unregister(&hvfb_drv);
  768. return ret;
  769. }
  770. return 0;
  771. }
  772. static void __exit hvfb_drv_exit(void)
  773. {
  774. pci_unregister_driver(&hvfb_pci_stub_driver);
  775. vmbus_driver_unregister(&hvfb_drv);
  776. }
  777. module_init(hvfb_drv_init);
  778. module_exit(hvfb_drv_exit);
  779. MODULE_LICENSE("GPL");
  780. MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");