vmwgfx_fb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /**************************************************************************
  2. *
  3. * Copyright © 2007 David Airlie
  4. * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
  5. * All Rights Reserved.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sub license, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the
  16. * next paragraph) shall be included in all copies or substantial portions
  17. * of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  22. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  23. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  24. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  25. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. *
  27. **************************************************************************/
  28. #include <linux/fb.h>
  29. #include <linux/pci.h>
  30. #include <drm/drm_fourcc.h>
  31. #include <drm/ttm/ttm_placement.h>
  32. #include "vmwgfx_drv.h"
  33. #include "vmwgfx_kms.h"
  34. #define VMW_DIRTY_DELAY (HZ / 30)
  35. struct vmw_fb_par {
  36. struct vmw_private *vmw_priv;
  37. void *vmalloc;
  38. struct mutex bo_mutex;
  39. struct vmw_buffer_object *vmw_bo;
  40. unsigned bo_size;
  41. struct drm_framebuffer *set_fb;
  42. struct drm_display_mode *set_mode;
  43. u32 fb_x;
  44. u32 fb_y;
  45. bool bo_iowrite;
  46. u32 pseudo_palette[17];
  47. unsigned max_width;
  48. unsigned max_height;
  49. struct {
  50. spinlock_t lock;
  51. bool active;
  52. unsigned x1;
  53. unsigned y1;
  54. unsigned x2;
  55. unsigned y2;
  56. } dirty;
  57. struct drm_crtc *crtc;
  58. struct drm_connector *con;
  59. struct delayed_work local_work;
  60. };
  61. static int vmw_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  62. unsigned blue, unsigned transp,
  63. struct fb_info *info)
  64. {
  65. struct vmw_fb_par *par = info->par;
  66. u32 *pal = par->pseudo_palette;
  67. if (regno > 15) {
  68. DRM_ERROR("Bad regno %u.\n", regno);
  69. return 1;
  70. }
  71. switch (par->set_fb->format->depth) {
  72. case 24:
  73. case 32:
  74. pal[regno] = ((red & 0xff00) << 8) |
  75. (green & 0xff00) |
  76. ((blue & 0xff00) >> 8);
  77. break;
  78. default:
  79. DRM_ERROR("Bad depth %u, bpp %u.\n",
  80. par->set_fb->format->depth,
  81. par->set_fb->format->cpp[0] * 8);
  82. return 1;
  83. }
  84. return 0;
  85. }
  86. static int vmw_fb_check_var(struct fb_var_screeninfo *var,
  87. struct fb_info *info)
  88. {
  89. int depth = var->bits_per_pixel;
  90. struct vmw_fb_par *par = info->par;
  91. struct vmw_private *vmw_priv = par->vmw_priv;
  92. switch (var->bits_per_pixel) {
  93. case 32:
  94. depth = (var->transp.length > 0) ? 32 : 24;
  95. break;
  96. default:
  97. DRM_ERROR("Bad bpp %u.\n", var->bits_per_pixel);
  98. return -EINVAL;
  99. }
  100. switch (depth) {
  101. case 24:
  102. var->red.offset = 16;
  103. var->green.offset = 8;
  104. var->blue.offset = 0;
  105. var->red.length = 8;
  106. var->green.length = 8;
  107. var->blue.length = 8;
  108. var->transp.length = 0;
  109. var->transp.offset = 0;
  110. break;
  111. case 32:
  112. var->red.offset = 16;
  113. var->green.offset = 8;
  114. var->blue.offset = 0;
  115. var->red.length = 8;
  116. var->green.length = 8;
  117. var->blue.length = 8;
  118. var->transp.length = 8;
  119. var->transp.offset = 24;
  120. break;
  121. default:
  122. DRM_ERROR("Bad depth %u.\n", depth);
  123. return -EINVAL;
  124. }
  125. if ((var->xoffset + var->xres) > par->max_width ||
  126. (var->yoffset + var->yres) > par->max_height) {
  127. DRM_ERROR("Requested geom can not fit in framebuffer\n");
  128. return -EINVAL;
  129. }
  130. if (!vmw_kms_validate_mode_vram(vmw_priv,
  131. var->xres * var->bits_per_pixel/8,
  132. var->yoffset + var->yres)) {
  133. DRM_ERROR("Requested geom can not fit in framebuffer\n");
  134. return -EINVAL;
  135. }
  136. return 0;
  137. }
  138. static int vmw_fb_blank(int blank, struct fb_info *info)
  139. {
  140. return 0;
  141. }
  142. /**
  143. * vmw_fb_dirty_flush - flush dirty regions to the kms framebuffer
  144. *
  145. * @work: The struct work_struct associated with this task.
  146. *
  147. * This function flushes the dirty regions of the vmalloc framebuffer to the
  148. * kms framebuffer, and if the kms framebuffer is visible, also updated the
  149. * corresponding displays. Note that this function runs even if the kms
  150. * framebuffer is not bound to a crtc and thus not visible, but it's turned
  151. * off during hibernation using the par->dirty.active bool.
  152. */
  153. static void vmw_fb_dirty_flush(struct work_struct *work)
  154. {
  155. struct vmw_fb_par *par = container_of(work, struct vmw_fb_par,
  156. local_work.work);
  157. struct vmw_private *vmw_priv = par->vmw_priv;
  158. struct fb_info *info = vmw_priv->fb_info;
  159. unsigned long irq_flags;
  160. s32 dst_x1, dst_x2, dst_y1, dst_y2, w = 0, h = 0;
  161. u32 cpp, max_x, max_y;
  162. struct drm_clip_rect clip;
  163. struct drm_framebuffer *cur_fb;
  164. u8 *src_ptr, *dst_ptr;
  165. struct vmw_buffer_object *vbo = par->vmw_bo;
  166. void *virtual;
  167. if (!READ_ONCE(par->dirty.active))
  168. return;
  169. mutex_lock(&par->bo_mutex);
  170. cur_fb = par->set_fb;
  171. if (!cur_fb)
  172. goto out_unlock;
  173. (void) ttm_bo_reserve(&vbo->base, false, false, NULL);
  174. virtual = vmw_bo_map_and_cache(vbo);
  175. if (!virtual)
  176. goto out_unreserve;
  177. spin_lock_irqsave(&par->dirty.lock, irq_flags);
  178. if (!par->dirty.active) {
  179. spin_unlock_irqrestore(&par->dirty.lock, irq_flags);
  180. goto out_unreserve;
  181. }
  182. /*
  183. * Handle panning when copying from vmalloc to framebuffer.
  184. * Clip dirty area to framebuffer.
  185. */
  186. cpp = cur_fb->format->cpp[0];
  187. max_x = par->fb_x + cur_fb->width;
  188. max_y = par->fb_y + cur_fb->height;
  189. dst_x1 = par->dirty.x1 - par->fb_x;
  190. dst_y1 = par->dirty.y1 - par->fb_y;
  191. dst_x1 = max_t(s32, dst_x1, 0);
  192. dst_y1 = max_t(s32, dst_y1, 0);
  193. dst_x2 = par->dirty.x2 - par->fb_x;
  194. dst_y2 = par->dirty.y2 - par->fb_y;
  195. dst_x2 = min_t(s32, dst_x2, max_x);
  196. dst_y2 = min_t(s32, dst_y2, max_y);
  197. w = dst_x2 - dst_x1;
  198. h = dst_y2 - dst_y1;
  199. w = max_t(s32, 0, w);
  200. h = max_t(s32, 0, h);
  201. par->dirty.x1 = par->dirty.x2 = 0;
  202. par->dirty.y1 = par->dirty.y2 = 0;
  203. spin_unlock_irqrestore(&par->dirty.lock, irq_flags);
  204. if (w && h) {
  205. dst_ptr = (u8 *)virtual +
  206. (dst_y1 * par->set_fb->pitches[0] + dst_x1 * cpp);
  207. src_ptr = (u8 *)par->vmalloc +
  208. ((dst_y1 + par->fb_y) * info->fix.line_length +
  209. (dst_x1 + par->fb_x) * cpp);
  210. while (h-- > 0) {
  211. memcpy(dst_ptr, src_ptr, w*cpp);
  212. dst_ptr += par->set_fb->pitches[0];
  213. src_ptr += info->fix.line_length;
  214. }
  215. clip.x1 = dst_x1;
  216. clip.x2 = dst_x2;
  217. clip.y1 = dst_y1;
  218. clip.y2 = dst_y2;
  219. }
  220. out_unreserve:
  221. ttm_bo_unreserve(&vbo->base);
  222. if (w && h) {
  223. WARN_ON_ONCE(par->set_fb->funcs->dirty(cur_fb, NULL, 0, 0,
  224. &clip, 1));
  225. vmw_cmd_flush(vmw_priv, false);
  226. }
  227. out_unlock:
  228. mutex_unlock(&par->bo_mutex);
  229. }
  230. static void vmw_fb_dirty_mark(struct vmw_fb_par *par,
  231. unsigned x1, unsigned y1,
  232. unsigned width, unsigned height)
  233. {
  234. unsigned long flags;
  235. unsigned x2 = x1 + width;
  236. unsigned y2 = y1 + height;
  237. spin_lock_irqsave(&par->dirty.lock, flags);
  238. if (par->dirty.x1 == par->dirty.x2) {
  239. par->dirty.x1 = x1;
  240. par->dirty.y1 = y1;
  241. par->dirty.x2 = x2;
  242. par->dirty.y2 = y2;
  243. /* if we are active start the dirty work
  244. * we share the work with the defio system */
  245. if (par->dirty.active)
  246. schedule_delayed_work(&par->local_work,
  247. VMW_DIRTY_DELAY);
  248. } else {
  249. if (x1 < par->dirty.x1)
  250. par->dirty.x1 = x1;
  251. if (y1 < par->dirty.y1)
  252. par->dirty.y1 = y1;
  253. if (x2 > par->dirty.x2)
  254. par->dirty.x2 = x2;
  255. if (y2 > par->dirty.y2)
  256. par->dirty.y2 = y2;
  257. }
  258. spin_unlock_irqrestore(&par->dirty.lock, flags);
  259. }
  260. static int vmw_fb_pan_display(struct fb_var_screeninfo *var,
  261. struct fb_info *info)
  262. {
  263. struct vmw_fb_par *par = info->par;
  264. if ((var->xoffset + var->xres) > var->xres_virtual ||
  265. (var->yoffset + var->yres) > var->yres_virtual) {
  266. DRM_ERROR("Requested panning can not fit in framebuffer\n");
  267. return -EINVAL;
  268. }
  269. mutex_lock(&par->bo_mutex);
  270. par->fb_x = var->xoffset;
  271. par->fb_y = var->yoffset;
  272. if (par->set_fb)
  273. vmw_fb_dirty_mark(par, par->fb_x, par->fb_y, par->set_fb->width,
  274. par->set_fb->height);
  275. mutex_unlock(&par->bo_mutex);
  276. return 0;
  277. }
  278. static void vmw_deferred_io(struct fb_info *info, struct list_head *pagereflist)
  279. {
  280. struct vmw_fb_par *par = info->par;
  281. unsigned long start, end, min, max;
  282. unsigned long flags;
  283. struct fb_deferred_io_pageref *pageref;
  284. int y1, y2;
  285. min = ULONG_MAX;
  286. max = 0;
  287. list_for_each_entry(pageref, pagereflist, list) {
  288. start = pageref->offset;
  289. end = start + PAGE_SIZE - 1;
  290. min = min(min, start);
  291. max = max(max, end);
  292. }
  293. if (min < max) {
  294. y1 = min / info->fix.line_length;
  295. y2 = (max / info->fix.line_length) + 1;
  296. spin_lock_irqsave(&par->dirty.lock, flags);
  297. par->dirty.x1 = 0;
  298. par->dirty.y1 = y1;
  299. par->dirty.x2 = info->var.xres;
  300. par->dirty.y2 = y2;
  301. spin_unlock_irqrestore(&par->dirty.lock, flags);
  302. /*
  303. * Since we've already waited on this work once, try to
  304. * execute asap.
  305. */
  306. cancel_delayed_work(&par->local_work);
  307. schedule_delayed_work(&par->local_work, 0);
  308. }
  309. };
  310. static struct fb_deferred_io vmw_defio = {
  311. .delay = VMW_DIRTY_DELAY,
  312. .deferred_io = vmw_deferred_io,
  313. };
  314. /*
  315. * Draw code
  316. */
  317. static void vmw_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  318. {
  319. cfb_fillrect(info, rect);
  320. vmw_fb_dirty_mark(info->par, rect->dx, rect->dy,
  321. rect->width, rect->height);
  322. }
  323. static void vmw_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
  324. {
  325. cfb_copyarea(info, region);
  326. vmw_fb_dirty_mark(info->par, region->dx, region->dy,
  327. region->width, region->height);
  328. }
  329. static void vmw_fb_imageblit(struct fb_info *info, const struct fb_image *image)
  330. {
  331. cfb_imageblit(info, image);
  332. vmw_fb_dirty_mark(info->par, image->dx, image->dy,
  333. image->width, image->height);
  334. }
  335. /*
  336. * Bring up code
  337. */
  338. static int vmw_fb_create_bo(struct vmw_private *vmw_priv,
  339. size_t size, struct vmw_buffer_object **out)
  340. {
  341. struct vmw_buffer_object *vmw_bo;
  342. int ret;
  343. ret = vmw_bo_create(vmw_priv, size,
  344. &vmw_sys_placement,
  345. false, false,
  346. &vmw_bo_bo_free, &vmw_bo);
  347. if (unlikely(ret != 0))
  348. return ret;
  349. *out = vmw_bo;
  350. return ret;
  351. }
  352. static int vmw_fb_compute_depth(struct fb_var_screeninfo *var,
  353. int *depth)
  354. {
  355. switch (var->bits_per_pixel) {
  356. case 32:
  357. *depth = (var->transp.length > 0) ? 32 : 24;
  358. break;
  359. default:
  360. DRM_ERROR("Bad bpp %u.\n", var->bits_per_pixel);
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. static int vmwgfx_set_config_internal(struct drm_mode_set *set)
  366. {
  367. struct drm_crtc *crtc = set->crtc;
  368. struct drm_modeset_acquire_ctx ctx;
  369. int ret;
  370. drm_modeset_acquire_init(&ctx, 0);
  371. restart:
  372. ret = crtc->funcs->set_config(set, &ctx);
  373. if (ret == -EDEADLK) {
  374. drm_modeset_backoff(&ctx);
  375. goto restart;
  376. }
  377. drm_modeset_drop_locks(&ctx);
  378. drm_modeset_acquire_fini(&ctx);
  379. return ret;
  380. }
  381. static int vmw_fb_kms_detach(struct vmw_fb_par *par,
  382. bool detach_bo,
  383. bool unref_bo)
  384. {
  385. struct drm_framebuffer *cur_fb = par->set_fb;
  386. int ret;
  387. /* Detach the KMS framebuffer from crtcs */
  388. if (par->set_mode) {
  389. struct drm_mode_set set;
  390. set.crtc = par->crtc;
  391. set.x = 0;
  392. set.y = 0;
  393. set.mode = NULL;
  394. set.fb = NULL;
  395. set.num_connectors = 0;
  396. set.connectors = &par->con;
  397. ret = vmwgfx_set_config_internal(&set);
  398. if (ret) {
  399. DRM_ERROR("Could not unset a mode.\n");
  400. return ret;
  401. }
  402. drm_mode_destroy(&par->vmw_priv->drm, par->set_mode);
  403. par->set_mode = NULL;
  404. }
  405. if (cur_fb) {
  406. drm_framebuffer_put(cur_fb);
  407. par->set_fb = NULL;
  408. }
  409. if (par->vmw_bo && detach_bo && unref_bo)
  410. vmw_bo_unreference(&par->vmw_bo);
  411. return 0;
  412. }
  413. static int vmw_fb_kms_framebuffer(struct fb_info *info)
  414. {
  415. struct drm_mode_fb_cmd2 mode_cmd = {0};
  416. struct vmw_fb_par *par = info->par;
  417. struct fb_var_screeninfo *var = &info->var;
  418. struct drm_framebuffer *cur_fb;
  419. struct vmw_framebuffer *vfb;
  420. int ret = 0, depth;
  421. size_t new_bo_size;
  422. ret = vmw_fb_compute_depth(var, &depth);
  423. if (ret)
  424. return ret;
  425. mode_cmd.width = var->xres;
  426. mode_cmd.height = var->yres;
  427. mode_cmd.pitches[0] = ((var->bits_per_pixel + 7) / 8) * mode_cmd.width;
  428. mode_cmd.pixel_format =
  429. drm_mode_legacy_fb_format(var->bits_per_pixel, depth);
  430. cur_fb = par->set_fb;
  431. if (cur_fb && cur_fb->width == mode_cmd.width &&
  432. cur_fb->height == mode_cmd.height &&
  433. cur_fb->format->format == mode_cmd.pixel_format &&
  434. cur_fb->pitches[0] == mode_cmd.pitches[0])
  435. return 0;
  436. /* Need new buffer object ? */
  437. new_bo_size = (size_t) mode_cmd.pitches[0] * (size_t) mode_cmd.height;
  438. ret = vmw_fb_kms_detach(par,
  439. par->bo_size < new_bo_size ||
  440. par->bo_size > 2*new_bo_size,
  441. true);
  442. if (ret)
  443. return ret;
  444. if (!par->vmw_bo) {
  445. ret = vmw_fb_create_bo(par->vmw_priv, new_bo_size,
  446. &par->vmw_bo);
  447. if (ret) {
  448. DRM_ERROR("Failed creating a buffer object for "
  449. "fbdev.\n");
  450. return ret;
  451. }
  452. par->bo_size = new_bo_size;
  453. }
  454. vfb = vmw_kms_new_framebuffer(par->vmw_priv, par->vmw_bo, NULL,
  455. true, &mode_cmd);
  456. if (IS_ERR(vfb))
  457. return PTR_ERR(vfb);
  458. par->set_fb = &vfb->base;
  459. return 0;
  460. }
  461. static int vmw_fb_set_par(struct fb_info *info)
  462. {
  463. struct vmw_fb_par *par = info->par;
  464. struct vmw_private *vmw_priv = par->vmw_priv;
  465. struct drm_mode_set set;
  466. struct fb_var_screeninfo *var = &info->var;
  467. struct drm_display_mode new_mode = { DRM_MODE("fb_mode",
  468. DRM_MODE_TYPE_DRIVER,
  469. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  470. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  471. };
  472. struct drm_display_mode *mode;
  473. int ret;
  474. mode = drm_mode_duplicate(&vmw_priv->drm, &new_mode);
  475. if (!mode) {
  476. DRM_ERROR("Could not create new fb mode.\n");
  477. return -ENOMEM;
  478. }
  479. mode->hdisplay = var->xres;
  480. mode->vdisplay = var->yres;
  481. vmw_guess_mode_timing(mode);
  482. if (!vmw_kms_validate_mode_vram(vmw_priv,
  483. mode->hdisplay *
  484. DIV_ROUND_UP(var->bits_per_pixel, 8),
  485. mode->vdisplay)) {
  486. drm_mode_destroy(&vmw_priv->drm, mode);
  487. return -EINVAL;
  488. }
  489. mutex_lock(&par->bo_mutex);
  490. ret = vmw_fb_kms_framebuffer(info);
  491. if (ret)
  492. goto out_unlock;
  493. par->fb_x = var->xoffset;
  494. par->fb_y = var->yoffset;
  495. set.crtc = par->crtc;
  496. set.x = 0;
  497. set.y = 0;
  498. set.mode = mode;
  499. set.fb = par->set_fb;
  500. set.num_connectors = 1;
  501. set.connectors = &par->con;
  502. ret = vmwgfx_set_config_internal(&set);
  503. if (ret)
  504. goto out_unlock;
  505. vmw_fb_dirty_mark(par, par->fb_x, par->fb_y,
  506. par->set_fb->width, par->set_fb->height);
  507. /* If there already was stuff dirty we wont
  508. * schedule a new work, so lets do it now */
  509. schedule_delayed_work(&par->local_work, 0);
  510. out_unlock:
  511. if (par->set_mode)
  512. drm_mode_destroy(&vmw_priv->drm, par->set_mode);
  513. par->set_mode = mode;
  514. mutex_unlock(&par->bo_mutex);
  515. return ret;
  516. }
  517. static const struct fb_ops vmw_fb_ops = {
  518. .owner = THIS_MODULE,
  519. .fb_check_var = vmw_fb_check_var,
  520. .fb_set_par = vmw_fb_set_par,
  521. .fb_setcolreg = vmw_fb_setcolreg,
  522. .fb_fillrect = vmw_fb_fillrect,
  523. .fb_copyarea = vmw_fb_copyarea,
  524. .fb_imageblit = vmw_fb_imageblit,
  525. .fb_pan_display = vmw_fb_pan_display,
  526. .fb_blank = vmw_fb_blank,
  527. .fb_mmap = fb_deferred_io_mmap,
  528. };
  529. int vmw_fb_init(struct vmw_private *vmw_priv)
  530. {
  531. struct device *device = vmw_priv->drm.dev;
  532. struct vmw_fb_par *par;
  533. struct fb_info *info;
  534. unsigned fb_width, fb_height;
  535. unsigned int fb_bpp, fb_pitch, fb_size;
  536. struct drm_display_mode *init_mode;
  537. int ret;
  538. fb_bpp = 32;
  539. /* XXX As shouldn't these be as well. */
  540. fb_width = min(vmw_priv->fb_max_width, (unsigned)2048);
  541. fb_height = min(vmw_priv->fb_max_height, (unsigned)2048);
  542. fb_pitch = fb_width * fb_bpp / 8;
  543. fb_size = fb_pitch * fb_height;
  544. info = framebuffer_alloc(sizeof(*par), device);
  545. if (!info)
  546. return -ENOMEM;
  547. /*
  548. * Par
  549. */
  550. vmw_priv->fb_info = info;
  551. par = info->par;
  552. memset(par, 0, sizeof(*par));
  553. INIT_DELAYED_WORK(&par->local_work, &vmw_fb_dirty_flush);
  554. par->vmw_priv = vmw_priv;
  555. par->vmalloc = NULL;
  556. par->max_width = fb_width;
  557. par->max_height = fb_height;
  558. ret = vmw_kms_fbdev_init_data(vmw_priv, 0, par->max_width,
  559. par->max_height, &par->con,
  560. &par->crtc, &init_mode);
  561. if (ret)
  562. goto err_kms;
  563. info->var.xres = init_mode->hdisplay;
  564. info->var.yres = init_mode->vdisplay;
  565. /*
  566. * Create buffers and alloc memory
  567. */
  568. par->vmalloc = vzalloc(fb_size);
  569. if (unlikely(par->vmalloc == NULL)) {
  570. ret = -ENOMEM;
  571. goto err_free;
  572. }
  573. /*
  574. * Fixed and var
  575. */
  576. strcpy(info->fix.id, "svgadrmfb");
  577. info->fix.type = FB_TYPE_PACKED_PIXELS;
  578. info->fix.visual = FB_VISUAL_TRUECOLOR;
  579. info->fix.type_aux = 0;
  580. info->fix.xpanstep = 1; /* doing it in hw */
  581. info->fix.ypanstep = 1; /* doing it in hw */
  582. info->fix.ywrapstep = 0;
  583. info->fix.accel = FB_ACCEL_NONE;
  584. info->fix.line_length = fb_pitch;
  585. info->fix.smem_start = 0;
  586. info->fix.smem_len = fb_size;
  587. info->pseudo_palette = par->pseudo_palette;
  588. info->screen_base = (char __iomem *)par->vmalloc;
  589. info->screen_size = fb_size;
  590. info->fbops = &vmw_fb_ops;
  591. /* 24 depth per default */
  592. info->var.red.offset = 16;
  593. info->var.green.offset = 8;
  594. info->var.blue.offset = 0;
  595. info->var.red.length = 8;
  596. info->var.green.length = 8;
  597. info->var.blue.length = 8;
  598. info->var.transp.offset = 0;
  599. info->var.transp.length = 0;
  600. info->var.xres_virtual = fb_width;
  601. info->var.yres_virtual = fb_height;
  602. info->var.bits_per_pixel = fb_bpp;
  603. info->var.xoffset = 0;
  604. info->var.yoffset = 0;
  605. info->var.activate = FB_ACTIVATE_NOW;
  606. info->var.height = -1;
  607. info->var.width = -1;
  608. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  609. info->apertures = alloc_apertures(1);
  610. if (!info->apertures) {
  611. ret = -ENOMEM;
  612. goto err_aper;
  613. }
  614. info->apertures->ranges[0].base = vmw_priv->vram_start;
  615. info->apertures->ranges[0].size = vmw_priv->vram_size;
  616. /*
  617. * Dirty & Deferred IO
  618. */
  619. par->dirty.x1 = par->dirty.x2 = 0;
  620. par->dirty.y1 = par->dirty.y2 = 0;
  621. par->dirty.active = true;
  622. spin_lock_init(&par->dirty.lock);
  623. mutex_init(&par->bo_mutex);
  624. info->fbdefio = &vmw_defio;
  625. fb_deferred_io_init(info);
  626. ret = register_framebuffer(info);
  627. if (unlikely(ret != 0))
  628. goto err_defio;
  629. vmw_fb_set_par(info);
  630. return 0;
  631. err_defio:
  632. fb_deferred_io_cleanup(info);
  633. err_aper:
  634. err_free:
  635. vfree(par->vmalloc);
  636. err_kms:
  637. framebuffer_release(info);
  638. vmw_priv->fb_info = NULL;
  639. return ret;
  640. }
  641. int vmw_fb_close(struct vmw_private *vmw_priv)
  642. {
  643. struct fb_info *info;
  644. struct vmw_fb_par *par;
  645. if (!vmw_priv->fb_info)
  646. return 0;
  647. info = vmw_priv->fb_info;
  648. par = info->par;
  649. /* ??? order */
  650. fb_deferred_io_cleanup(info);
  651. cancel_delayed_work_sync(&par->local_work);
  652. unregister_framebuffer(info);
  653. mutex_lock(&par->bo_mutex);
  654. (void) vmw_fb_kms_detach(par, true, true);
  655. mutex_unlock(&par->bo_mutex);
  656. vfree(par->vmalloc);
  657. framebuffer_release(info);
  658. return 0;
  659. }
  660. int vmw_fb_off(struct vmw_private *vmw_priv)
  661. {
  662. struct fb_info *info;
  663. struct vmw_fb_par *par;
  664. unsigned long flags;
  665. if (!vmw_priv->fb_info)
  666. return -EINVAL;
  667. info = vmw_priv->fb_info;
  668. par = info->par;
  669. spin_lock_irqsave(&par->dirty.lock, flags);
  670. par->dirty.active = false;
  671. spin_unlock_irqrestore(&par->dirty.lock, flags);
  672. flush_delayed_work(&info->deferred_work);
  673. flush_delayed_work(&par->local_work);
  674. return 0;
  675. }
  676. int vmw_fb_on(struct vmw_private *vmw_priv)
  677. {
  678. struct fb_info *info;
  679. struct vmw_fb_par *par;
  680. unsigned long flags;
  681. if (!vmw_priv->fb_info)
  682. return -EINVAL;
  683. info = vmw_priv->fb_info;
  684. par = info->par;
  685. spin_lock_irqsave(&par->dirty.lock, flags);
  686. par->dirty.active = true;
  687. spin_unlock_irqrestore(&par->dirty.lock, flags);
  688. /*
  689. * Need to reschedule a dirty update, because otherwise that's
  690. * only done in dirty_mark() if the previous coalesced
  691. * dirty region was empty.
  692. */
  693. schedule_delayed_work(&par->local_work, 0);
  694. return 0;
  695. }