hyperv_fb.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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 t, ret = 0;
  342. memset(msg, 0, sizeof(struct synthvid_msg));
  343. msg->vid_hdr.type = SYNTHVID_VERSION_REQUEST;
  344. msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  345. sizeof(struct synthvid_version_req);
  346. msg->ver_req.version = ver;
  347. synthvid_send(hdev, msg);
  348. t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
  349. if (!t) {
  350. pr_err("Time out on waiting version response\n");
  351. ret = -ETIMEDOUT;
  352. goto out;
  353. }
  354. if (!msg->ver_resp.is_accepted) {
  355. ret = -ENODEV;
  356. goto out;
  357. }
  358. par->synthvid_version = ver;
  359. out:
  360. return ret;
  361. }
  362. /* Connect to VSP (Virtual Service Provider) on host */
  363. static int synthvid_connect_vsp(struct hv_device *hdev)
  364. {
  365. struct fb_info *info = hv_get_drvdata(hdev);
  366. struct hvfb_par *par = info->par;
  367. int ret;
  368. ret = vmbus_open(hdev->channel, RING_BUFSIZE, RING_BUFSIZE,
  369. NULL, 0, synthvid_receive, hdev);
  370. if (ret) {
  371. pr_err("Unable to open vmbus channel\n");
  372. return ret;
  373. }
  374. /* Negotiate the protocol version with host */
  375. if (vmbus_proto_version == VERSION_WS2008 ||
  376. vmbus_proto_version == VERSION_WIN7)
  377. ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN7);
  378. else
  379. ret = synthvid_negotiate_ver(hdev, SYNTHVID_VERSION_WIN8);
  380. if (ret) {
  381. pr_err("Synthetic video device version not accepted\n");
  382. goto error;
  383. }
  384. if (par->synthvid_version == SYNTHVID_VERSION_WIN7)
  385. screen_depth = SYNTHVID_DEPTH_WIN7;
  386. else
  387. screen_depth = SYNTHVID_DEPTH_WIN8;
  388. screen_fb_size = hdev->channel->offermsg.offer.
  389. mmio_megabytes * 1024 * 1024;
  390. return 0;
  391. error:
  392. vmbus_close(hdev->channel);
  393. return ret;
  394. }
  395. /* Send VRAM and Situation messages to the host */
  396. static int synthvid_send_config(struct hv_device *hdev)
  397. {
  398. struct fb_info *info = hv_get_drvdata(hdev);
  399. struct hvfb_par *par = info->par;
  400. struct synthvid_msg *msg = (struct synthvid_msg *)par->init_buf;
  401. int t, ret = 0;
  402. /* Send VRAM location */
  403. memset(msg, 0, sizeof(struct synthvid_msg));
  404. msg->vid_hdr.type = SYNTHVID_VRAM_LOCATION;
  405. msg->vid_hdr.size = sizeof(struct synthvid_msg_hdr) +
  406. sizeof(struct synthvid_vram_location);
  407. msg->vram.user_ctx = msg->vram.vram_gpa = info->fix.smem_start;
  408. msg->vram.is_vram_gpa_specified = 1;
  409. synthvid_send(hdev, msg);
  410. t = wait_for_completion_timeout(&par->wait, VSP_TIMEOUT);
  411. if (!t) {
  412. pr_err("Time out on waiting vram location ack\n");
  413. ret = -ETIMEDOUT;
  414. goto out;
  415. }
  416. if (msg->vram_ack.user_ctx != info->fix.smem_start) {
  417. pr_err("Unable to set VRAM location\n");
  418. ret = -ENODEV;
  419. goto out;
  420. }
  421. /* Send pointer and situation update */
  422. synthvid_send_ptr(hdev);
  423. synthvid_send_situ(hdev);
  424. out:
  425. return ret;
  426. }
  427. /*
  428. * Delayed work callback:
  429. * It is called at HVFB_UPDATE_DELAY or longer time interval to process
  430. * screen updates. It is re-scheduled if further update is necessary.
  431. */
  432. static void hvfb_update_work(struct work_struct *w)
  433. {
  434. struct hvfb_par *par = container_of(w, struct hvfb_par, dwork.work);
  435. struct fb_info *info = par->info;
  436. if (par->fb_ready)
  437. synthvid_update(info);
  438. if (par->update)
  439. schedule_delayed_work(&par->dwork, HVFB_UPDATE_DELAY);
  440. }
  441. static int hvfb_on_panic(struct notifier_block *nb,
  442. unsigned long e, void *p)
  443. {
  444. struct hvfb_par *par;
  445. struct fb_info *info;
  446. par = container_of(nb, struct hvfb_par, hvfb_panic_nb);
  447. par->synchronous_fb = true;
  448. info = par->info;
  449. synthvid_update(info);
  450. return NOTIFY_DONE;
  451. }
  452. /* Framebuffer operation handlers */
  453. static int hvfb_check_var(struct fb_var_screeninfo *var, struct fb_info *info)
  454. {
  455. if (var->xres < HVFB_WIDTH_MIN || var->yres < HVFB_HEIGHT_MIN ||
  456. var->xres > screen_width || var->yres > screen_height ||
  457. var->bits_per_pixel != screen_depth)
  458. return -EINVAL;
  459. var->xres_virtual = var->xres;
  460. var->yres_virtual = var->yres;
  461. return 0;
  462. }
  463. static int hvfb_set_par(struct fb_info *info)
  464. {
  465. struct hv_device *hdev = device_to_hv_device(info->device);
  466. return synthvid_send_situ(hdev);
  467. }
  468. static inline u32 chan_to_field(u32 chan, struct fb_bitfield *bf)
  469. {
  470. return ((chan & 0xffff) >> (16 - bf->length)) << bf->offset;
  471. }
  472. static int hvfb_setcolreg(unsigned regno, unsigned red, unsigned green,
  473. unsigned blue, unsigned transp, struct fb_info *info)
  474. {
  475. u32 *pal = info->pseudo_palette;
  476. if (regno > 15)
  477. return -EINVAL;
  478. pal[regno] = chan_to_field(red, &info->var.red)
  479. | chan_to_field(green, &info->var.green)
  480. | chan_to_field(blue, &info->var.blue)
  481. | chan_to_field(transp, &info->var.transp);
  482. return 0;
  483. }
  484. static int hvfb_blank(int blank, struct fb_info *info)
  485. {
  486. return 1; /* get fb_blank to set the colormap to all black */
  487. }
  488. static void hvfb_cfb_fillrect(struct fb_info *p,
  489. const struct fb_fillrect *rect)
  490. {
  491. struct hvfb_par *par = p->par;
  492. cfb_fillrect(p, rect);
  493. if (par->synchronous_fb)
  494. synthvid_update(p);
  495. }
  496. static void hvfb_cfb_copyarea(struct fb_info *p,
  497. const struct fb_copyarea *area)
  498. {
  499. struct hvfb_par *par = p->par;
  500. cfb_copyarea(p, area);
  501. if (par->synchronous_fb)
  502. synthvid_update(p);
  503. }
  504. static void hvfb_cfb_imageblit(struct fb_info *p,
  505. const struct fb_image *image)
  506. {
  507. struct hvfb_par *par = p->par;
  508. cfb_imageblit(p, image);
  509. if (par->synchronous_fb)
  510. synthvid_update(p);
  511. }
  512. static struct fb_ops hvfb_ops = {
  513. .owner = THIS_MODULE,
  514. .fb_check_var = hvfb_check_var,
  515. .fb_set_par = hvfb_set_par,
  516. .fb_setcolreg = hvfb_setcolreg,
  517. .fb_fillrect = hvfb_cfb_fillrect,
  518. .fb_copyarea = hvfb_cfb_copyarea,
  519. .fb_imageblit = hvfb_cfb_imageblit,
  520. .fb_blank = hvfb_blank,
  521. };
  522. /* Get options from kernel paramenter "video=" */
  523. static void hvfb_get_option(struct fb_info *info)
  524. {
  525. struct hvfb_par *par = info->par;
  526. char *opt = NULL, *p;
  527. uint x = 0, y = 0;
  528. if (fb_get_options(KBUILD_MODNAME, &opt) || !opt || !*opt)
  529. return;
  530. p = strsep(&opt, "x");
  531. if (!*p || kstrtouint(p, 0, &x) ||
  532. !opt || !*opt || kstrtouint(opt, 0, &y)) {
  533. pr_err("Screen option is invalid: skipped\n");
  534. return;
  535. }
  536. if (x < HVFB_WIDTH_MIN || y < HVFB_HEIGHT_MIN ||
  537. (par->synthvid_version == SYNTHVID_VERSION_WIN8 &&
  538. x * y * screen_depth / 8 > SYNTHVID_FB_SIZE_WIN8) ||
  539. (par->synthvid_version == SYNTHVID_VERSION_WIN7 &&
  540. (x > SYNTHVID_WIDTH_MAX_WIN7 || y > SYNTHVID_HEIGHT_MAX_WIN7))) {
  541. pr_err("Screen resolution option is out of range: skipped\n");
  542. return;
  543. }
  544. screen_width = x;
  545. screen_height = y;
  546. return;
  547. }
  548. /* Get framebuffer memory from Hyper-V video pci space */
  549. static int hvfb_getmem(struct fb_info *info)
  550. {
  551. struct hvfb_par *par = info->par;
  552. struct pci_dev *pdev = NULL;
  553. void __iomem *fb_virt;
  554. int gen2vm = efi_enabled(EFI_BOOT);
  555. int ret;
  556. par->mem.name = KBUILD_MODNAME;
  557. par->mem.flags = IORESOURCE_MEM | IORESOURCE_BUSY;
  558. if (gen2vm) {
  559. ret = allocate_resource(&hyperv_mmio, &par->mem,
  560. screen_fb_size,
  561. 0, -1,
  562. screen_fb_size,
  563. NULL, NULL);
  564. if (ret != 0) {
  565. pr_err("Unable to allocate framebuffer memory\n");
  566. return -ENODEV;
  567. }
  568. } else {
  569. pdev = pci_get_device(PCI_VENDOR_ID_MICROSOFT,
  570. PCI_DEVICE_ID_HYPERV_VIDEO, NULL);
  571. if (!pdev) {
  572. pr_err("Unable to find PCI Hyper-V video\n");
  573. return -ENODEV;
  574. }
  575. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM) ||
  576. pci_resource_len(pdev, 0) < screen_fb_size)
  577. goto err1;
  578. par->mem.end = pci_resource_end(pdev, 0);
  579. par->mem.start = par->mem.end - screen_fb_size + 1;
  580. ret = request_resource(&pdev->resource[0], &par->mem);
  581. if (ret != 0) {
  582. pr_err("Unable to request framebuffer memory\n");
  583. goto err1;
  584. }
  585. }
  586. fb_virt = ioremap(par->mem.start, screen_fb_size);
  587. if (!fb_virt)
  588. goto err2;
  589. info->apertures = alloc_apertures(1);
  590. if (!info->apertures)
  591. goto err3;
  592. if (gen2vm) {
  593. info->apertures->ranges[0].base = screen_info.lfb_base;
  594. info->apertures->ranges[0].size = screen_info.lfb_size;
  595. remove_conflicting_framebuffers(info->apertures,
  596. KBUILD_MODNAME, false);
  597. } else {
  598. info->apertures->ranges[0].base = pci_resource_start(pdev, 0);
  599. info->apertures->ranges[0].size = pci_resource_len(pdev, 0);
  600. }
  601. info->fix.smem_start = par->mem.start;
  602. info->fix.smem_len = screen_fb_size;
  603. info->screen_base = fb_virt;
  604. info->screen_size = screen_fb_size;
  605. if (!gen2vm)
  606. pci_dev_put(pdev);
  607. return 0;
  608. err3:
  609. iounmap(fb_virt);
  610. err2:
  611. release_resource(&par->mem);
  612. err1:
  613. if (!gen2vm)
  614. pci_dev_put(pdev);
  615. return -ENOMEM;
  616. }
  617. /* Release the framebuffer */
  618. static void hvfb_putmem(struct fb_info *info)
  619. {
  620. struct hvfb_par *par = info->par;
  621. iounmap(info->screen_base);
  622. release_resource(&par->mem);
  623. }
  624. static int hvfb_probe(struct hv_device *hdev,
  625. const struct hv_vmbus_device_id *dev_id)
  626. {
  627. struct fb_info *info;
  628. struct hvfb_par *par;
  629. int ret;
  630. info = framebuffer_alloc(sizeof(struct hvfb_par), &hdev->device);
  631. if (!info) {
  632. pr_err("No memory for framebuffer info\n");
  633. return -ENOMEM;
  634. }
  635. par = info->par;
  636. par->info = info;
  637. par->fb_ready = false;
  638. init_completion(&par->wait);
  639. INIT_DELAYED_WORK(&par->dwork, hvfb_update_work);
  640. /* Connect to VSP */
  641. hv_set_drvdata(hdev, info);
  642. ret = synthvid_connect_vsp(hdev);
  643. if (ret) {
  644. pr_err("Unable to connect to VSP\n");
  645. goto error1;
  646. }
  647. ret = hvfb_getmem(info);
  648. if (ret) {
  649. pr_err("No memory for framebuffer\n");
  650. goto error2;
  651. }
  652. hvfb_get_option(info);
  653. pr_info("Screen resolution: %dx%d, Color depth: %d\n",
  654. screen_width, screen_height, screen_depth);
  655. /* Set up fb_info */
  656. info->flags = FBINFO_DEFAULT;
  657. info->var.xres_virtual = info->var.xres = screen_width;
  658. info->var.yres_virtual = info->var.yres = screen_height;
  659. info->var.bits_per_pixel = screen_depth;
  660. if (info->var.bits_per_pixel == 16) {
  661. info->var.red = (struct fb_bitfield){11, 5, 0};
  662. info->var.green = (struct fb_bitfield){5, 6, 0};
  663. info->var.blue = (struct fb_bitfield){0, 5, 0};
  664. info->var.transp = (struct fb_bitfield){0, 0, 0};
  665. } else {
  666. info->var.red = (struct fb_bitfield){16, 8, 0};
  667. info->var.green = (struct fb_bitfield){8, 8, 0};
  668. info->var.blue = (struct fb_bitfield){0, 8, 0};
  669. info->var.transp = (struct fb_bitfield){24, 8, 0};
  670. }
  671. info->var.activate = FB_ACTIVATE_NOW;
  672. info->var.height = -1;
  673. info->var.width = -1;
  674. info->var.vmode = FB_VMODE_NONINTERLACED;
  675. strcpy(info->fix.id, KBUILD_MODNAME);
  676. info->fix.type = FB_TYPE_PACKED_PIXELS;
  677. info->fix.visual = FB_VISUAL_TRUECOLOR;
  678. info->fix.line_length = screen_width * screen_depth / 8;
  679. info->fix.accel = FB_ACCEL_NONE;
  680. info->fbops = &hvfb_ops;
  681. info->pseudo_palette = par->pseudo_palette;
  682. /* Send config to host */
  683. ret = synthvid_send_config(hdev);
  684. if (ret)
  685. goto error;
  686. ret = register_framebuffer(info);
  687. if (ret) {
  688. pr_err("Unable to register framebuffer\n");
  689. goto error;
  690. }
  691. par->fb_ready = true;
  692. par->synchronous_fb = false;
  693. par->hvfb_panic_nb.notifier_call = hvfb_on_panic;
  694. atomic_notifier_chain_register(&panic_notifier_list,
  695. &par->hvfb_panic_nb);
  696. return 0;
  697. error:
  698. hvfb_putmem(info);
  699. error2:
  700. vmbus_close(hdev->channel);
  701. error1:
  702. cancel_delayed_work_sync(&par->dwork);
  703. hv_set_drvdata(hdev, NULL);
  704. framebuffer_release(info);
  705. return ret;
  706. }
  707. static int hvfb_remove(struct hv_device *hdev)
  708. {
  709. struct fb_info *info = hv_get_drvdata(hdev);
  710. struct hvfb_par *par = info->par;
  711. atomic_notifier_chain_unregister(&panic_notifier_list,
  712. &par->hvfb_panic_nb);
  713. par->update = false;
  714. par->fb_ready = false;
  715. unregister_framebuffer(info);
  716. cancel_delayed_work_sync(&par->dwork);
  717. vmbus_close(hdev->channel);
  718. hv_set_drvdata(hdev, NULL);
  719. hvfb_putmem(info);
  720. framebuffer_release(info);
  721. return 0;
  722. }
  723. static const struct pci_device_id pci_stub_id_table[] = {
  724. {
  725. .vendor = PCI_VENDOR_ID_MICROSOFT,
  726. .device = PCI_DEVICE_ID_HYPERV_VIDEO,
  727. },
  728. { /* end of list */ }
  729. };
  730. static const struct hv_vmbus_device_id id_table[] = {
  731. /* Synthetic Video Device GUID */
  732. {HV_SYNTHVID_GUID},
  733. {}
  734. };
  735. MODULE_DEVICE_TABLE(pci, pci_stub_id_table);
  736. MODULE_DEVICE_TABLE(vmbus, id_table);
  737. static struct hv_driver hvfb_drv = {
  738. .name = KBUILD_MODNAME,
  739. .id_table = id_table,
  740. .probe = hvfb_probe,
  741. .remove = hvfb_remove,
  742. };
  743. static int hvfb_pci_stub_probe(struct pci_dev *pdev,
  744. const struct pci_device_id *ent)
  745. {
  746. return 0;
  747. }
  748. static void hvfb_pci_stub_remove(struct pci_dev *pdev)
  749. {
  750. }
  751. static struct pci_driver hvfb_pci_stub_driver = {
  752. .name = KBUILD_MODNAME,
  753. .id_table = pci_stub_id_table,
  754. .probe = hvfb_pci_stub_probe,
  755. .remove = hvfb_pci_stub_remove,
  756. };
  757. static int __init hvfb_drv_init(void)
  758. {
  759. int ret;
  760. ret = vmbus_driver_register(&hvfb_drv);
  761. if (ret != 0)
  762. return ret;
  763. ret = pci_register_driver(&hvfb_pci_stub_driver);
  764. if (ret != 0) {
  765. vmbus_driver_unregister(&hvfb_drv);
  766. return ret;
  767. }
  768. return 0;
  769. }
  770. static void __exit hvfb_drv_exit(void)
  771. {
  772. pci_unregister_driver(&hvfb_pci_stub_driver);
  773. vmbus_driver_unregister(&hvfb_drv);
  774. }
  775. module_init(hvfb_drv_init);
  776. module_exit(hvfb_drv_exit);
  777. MODULE_LICENSE("GPL");
  778. MODULE_DESCRIPTION("Microsoft Hyper-V Synthetic Video Frame Buffer Driver");