vmwgfx_fb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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/export.h>
  29. #include <drm/drmP.h>
  30. #include "vmwgfx_drv.h"
  31. #include "vmwgfx_kms.h"
  32. #include <drm/ttm/ttm_placement.h>
  33. #define VMW_DIRTY_DELAY (HZ / 30)
  34. struct vmw_fb_par {
  35. struct vmw_private *vmw_priv;
  36. void *vmalloc;
  37. struct mutex bo_mutex;
  38. struct vmw_dma_buffer *vmw_bo;
  39. unsigned bo_size;
  40. struct drm_framebuffer *set_fb;
  41. struct drm_display_mode *set_mode;
  42. u32 fb_x;
  43. u32 fb_y;
  44. bool bo_iowrite;
  45. u32 pseudo_palette[17];
  46. unsigned max_width;
  47. unsigned max_height;
  48. struct {
  49. spinlock_t lock;
  50. bool active;
  51. unsigned x1;
  52. unsigned y1;
  53. unsigned x2;
  54. unsigned y2;
  55. } dirty;
  56. struct drm_crtc *crtc;
  57. struct drm_connector *con;
  58. struct delayed_work local_work;
  59. };
  60. static int vmw_fb_setcolreg(unsigned regno, unsigned red, unsigned green,
  61. unsigned blue, unsigned transp,
  62. struct fb_info *info)
  63. {
  64. struct vmw_fb_par *par = info->par;
  65. u32 *pal = par->pseudo_palette;
  66. if (regno > 15) {
  67. DRM_ERROR("Bad regno %u.\n", regno);
  68. return 1;
  69. }
  70. switch (par->set_fb->format->depth) {
  71. case 24:
  72. case 32:
  73. pal[regno] = ((red & 0xff00) << 8) |
  74. (green & 0xff00) |
  75. ((blue & 0xff00) >> 8);
  76. break;
  77. default:
  78. DRM_ERROR("Bad depth %u, bpp %u.\n",
  79. par->set_fb->format->depth,
  80. par->set_fb->format->cpp[0] * 8);
  81. return 1;
  82. }
  83. return 0;
  84. }
  85. static int vmw_fb_check_var(struct fb_var_screeninfo *var,
  86. struct fb_info *info)
  87. {
  88. int depth = var->bits_per_pixel;
  89. struct vmw_fb_par *par = info->par;
  90. struct vmw_private *vmw_priv = par->vmw_priv;
  91. switch (var->bits_per_pixel) {
  92. case 32:
  93. depth = (var->transp.length > 0) ? 32 : 24;
  94. break;
  95. default:
  96. DRM_ERROR("Bad bpp %u.\n", var->bits_per_pixel);
  97. return -EINVAL;
  98. }
  99. switch (depth) {
  100. case 24:
  101. var->red.offset = 16;
  102. var->green.offset = 8;
  103. var->blue.offset = 0;
  104. var->red.length = 8;
  105. var->green.length = 8;
  106. var->blue.length = 8;
  107. var->transp.length = 0;
  108. var->transp.offset = 0;
  109. break;
  110. case 32:
  111. var->red.offset = 16;
  112. var->green.offset = 8;
  113. var->blue.offset = 0;
  114. var->red.length = 8;
  115. var->green.length = 8;
  116. var->blue.length = 8;
  117. var->transp.length = 8;
  118. var->transp.offset = 24;
  119. break;
  120. default:
  121. DRM_ERROR("Bad depth %u.\n", depth);
  122. return -EINVAL;
  123. }
  124. if ((var->xoffset + var->xres) > par->max_width ||
  125. (var->yoffset + var->yres) > par->max_height) {
  126. DRM_ERROR("Requested geom can not fit in framebuffer\n");
  127. return -EINVAL;
  128. }
  129. if (!vmw_kms_validate_mode_vram(vmw_priv,
  130. var->xres * var->bits_per_pixel/8,
  131. var->yoffset + var->yres)) {
  132. DRM_ERROR("Requested geom can not fit in framebuffer\n");
  133. return -EINVAL;
  134. }
  135. return 0;
  136. }
  137. static int vmw_fb_blank(int blank, struct fb_info *info)
  138. {
  139. return 0;
  140. }
  141. /**
  142. * vmw_fb_dirty_flush - flush dirty regions to the kms framebuffer
  143. *
  144. * @work: The struct work_struct associated with this task.
  145. *
  146. * This function flushes the dirty regions of the vmalloc framebuffer to the
  147. * kms framebuffer, and if the kms framebuffer is visible, also updated the
  148. * corresponding displays. Note that this function runs even if the kms
  149. * framebuffer is not bound to a crtc and thus not visible, but it's turned
  150. * off during hibernation using the par->dirty.active bool.
  151. */
  152. static void vmw_fb_dirty_flush(struct work_struct *work)
  153. {
  154. struct vmw_fb_par *par = container_of(work, struct vmw_fb_par,
  155. local_work.work);
  156. struct vmw_private *vmw_priv = par->vmw_priv;
  157. struct fb_info *info = vmw_priv->fb_info;
  158. unsigned long irq_flags;
  159. s32 dst_x1, dst_x2, dst_y1, dst_y2, w = 0, h = 0;
  160. u32 cpp, max_x, max_y;
  161. struct drm_clip_rect clip;
  162. struct drm_framebuffer *cur_fb;
  163. u8 *src_ptr, *dst_ptr;
  164. struct vmw_dma_buffer *vbo = par->vmw_bo;
  165. void *virtual;
  166. if (!READ_ONCE(par->dirty.active))
  167. return;
  168. mutex_lock(&par->bo_mutex);
  169. cur_fb = par->set_fb;
  170. if (!cur_fb)
  171. goto out_unlock;
  172. (void) ttm_read_lock(&vmw_priv->reservation_sem, false);
  173. (void) ttm_bo_reserve(&vbo->base, false, false, NULL);
  174. virtual = vmw_dma_buffer_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. ttm_read_unlock(&vmw_priv->reservation_sem);
  223. if (w && h) {
  224. WARN_ON_ONCE(par->set_fb->funcs->dirty(cur_fb, NULL, 0, 0,
  225. &clip, 1));
  226. vmw_fifo_flush(vmw_priv, false);
  227. }
  228. out_unlock:
  229. mutex_unlock(&par->bo_mutex);
  230. }
  231. static void vmw_fb_dirty_mark(struct vmw_fb_par *par,
  232. unsigned x1, unsigned y1,
  233. unsigned width, unsigned height)
  234. {
  235. unsigned long flags;
  236. unsigned x2 = x1 + width;
  237. unsigned y2 = y1 + height;
  238. spin_lock_irqsave(&par->dirty.lock, flags);
  239. if (par->dirty.x1 == par->dirty.x2) {
  240. par->dirty.x1 = x1;
  241. par->dirty.y1 = y1;
  242. par->dirty.x2 = x2;
  243. par->dirty.y2 = y2;
  244. /* if we are active start the dirty work
  245. * we share the work with the defio system */
  246. if (par->dirty.active)
  247. schedule_delayed_work(&par->local_work,
  248. VMW_DIRTY_DELAY);
  249. } else {
  250. if (x1 < par->dirty.x1)
  251. par->dirty.x1 = x1;
  252. if (y1 < par->dirty.y1)
  253. par->dirty.y1 = y1;
  254. if (x2 > par->dirty.x2)
  255. par->dirty.x2 = x2;
  256. if (y2 > par->dirty.y2)
  257. par->dirty.y2 = y2;
  258. }
  259. spin_unlock_irqrestore(&par->dirty.lock, flags);
  260. }
  261. static int vmw_fb_pan_display(struct fb_var_screeninfo *var,
  262. struct fb_info *info)
  263. {
  264. struct vmw_fb_par *par = info->par;
  265. if ((var->xoffset + var->xres) > var->xres_virtual ||
  266. (var->yoffset + var->yres) > var->yres_virtual) {
  267. DRM_ERROR("Requested panning can not fit in framebuffer\n");
  268. return -EINVAL;
  269. }
  270. mutex_lock(&par->bo_mutex);
  271. par->fb_x = var->xoffset;
  272. par->fb_y = var->yoffset;
  273. if (par->set_fb)
  274. vmw_fb_dirty_mark(par, par->fb_x, par->fb_y, par->set_fb->width,
  275. par->set_fb->height);
  276. mutex_unlock(&par->bo_mutex);
  277. return 0;
  278. }
  279. static void vmw_deferred_io(struct fb_info *info,
  280. struct list_head *pagelist)
  281. {
  282. struct vmw_fb_par *par = info->par;
  283. unsigned long start, end, min, max;
  284. unsigned long flags;
  285. struct page *page;
  286. int y1, y2;
  287. min = ULONG_MAX;
  288. max = 0;
  289. list_for_each_entry(page, pagelist, lru) {
  290. start = page->index << PAGE_SHIFT;
  291. end = start + PAGE_SIZE - 1;
  292. min = min(min, start);
  293. max = max(max, end);
  294. }
  295. if (min < max) {
  296. y1 = min / info->fix.line_length;
  297. y2 = (max / info->fix.line_length) + 1;
  298. spin_lock_irqsave(&par->dirty.lock, flags);
  299. par->dirty.x1 = 0;
  300. par->dirty.y1 = y1;
  301. par->dirty.x2 = info->var.xres;
  302. par->dirty.y2 = y2;
  303. spin_unlock_irqrestore(&par->dirty.lock, flags);
  304. /*
  305. * Since we've already waited on this work once, try to
  306. * execute asap.
  307. */
  308. cancel_delayed_work(&par->local_work);
  309. schedule_delayed_work(&par->local_work, 0);
  310. }
  311. };
  312. static struct fb_deferred_io vmw_defio = {
  313. .delay = VMW_DIRTY_DELAY,
  314. .deferred_io = vmw_deferred_io,
  315. };
  316. /*
  317. * Draw code
  318. */
  319. static void vmw_fb_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  320. {
  321. cfb_fillrect(info, rect);
  322. vmw_fb_dirty_mark(info->par, rect->dx, rect->dy,
  323. rect->width, rect->height);
  324. }
  325. static void vmw_fb_copyarea(struct fb_info *info, const struct fb_copyarea *region)
  326. {
  327. cfb_copyarea(info, region);
  328. vmw_fb_dirty_mark(info->par, region->dx, region->dy,
  329. region->width, region->height);
  330. }
  331. static void vmw_fb_imageblit(struct fb_info *info, const struct fb_image *image)
  332. {
  333. cfb_imageblit(info, image);
  334. vmw_fb_dirty_mark(info->par, image->dx, image->dy,
  335. image->width, image->height);
  336. }
  337. /*
  338. * Bring up code
  339. */
  340. static int vmw_fb_create_bo(struct vmw_private *vmw_priv,
  341. size_t size, struct vmw_dma_buffer **out)
  342. {
  343. struct vmw_dma_buffer *vmw_bo;
  344. int ret;
  345. (void) ttm_write_lock(&vmw_priv->reservation_sem, false);
  346. vmw_bo = kmalloc(sizeof(*vmw_bo), GFP_KERNEL);
  347. if (!vmw_bo) {
  348. ret = -ENOMEM;
  349. goto err_unlock;
  350. }
  351. ret = vmw_dmabuf_init(vmw_priv, vmw_bo, size,
  352. &vmw_sys_placement,
  353. false,
  354. &vmw_dmabuf_bo_free);
  355. if (unlikely(ret != 0))
  356. goto err_unlock; /* init frees the buffer on failure */
  357. *out = vmw_bo;
  358. ttm_write_unlock(&vmw_priv->reservation_sem);
  359. return 0;
  360. err_unlock:
  361. ttm_write_unlock(&vmw_priv->reservation_sem);
  362. return ret;
  363. }
  364. static int vmw_fb_compute_depth(struct fb_var_screeninfo *var,
  365. int *depth)
  366. {
  367. switch (var->bits_per_pixel) {
  368. case 32:
  369. *depth = (var->transp.length > 0) ? 32 : 24;
  370. break;
  371. default:
  372. DRM_ERROR("Bad bpp %u.\n", var->bits_per_pixel);
  373. return -EINVAL;
  374. }
  375. return 0;
  376. }
  377. static int vmwgfx_set_config_internal(struct drm_mode_set *set)
  378. {
  379. struct drm_crtc *crtc = set->crtc;
  380. struct drm_modeset_acquire_ctx ctx;
  381. int ret;
  382. drm_modeset_acquire_init(&ctx, 0);
  383. restart:
  384. ret = crtc->funcs->set_config(set, &ctx);
  385. if (ret == -EDEADLK) {
  386. drm_modeset_backoff(&ctx);
  387. goto restart;
  388. }
  389. drm_modeset_drop_locks(&ctx);
  390. drm_modeset_acquire_fini(&ctx);
  391. return ret;
  392. }
  393. static int vmw_fb_kms_detach(struct vmw_fb_par *par,
  394. bool detach_bo,
  395. bool unref_bo)
  396. {
  397. struct drm_framebuffer *cur_fb = par->set_fb;
  398. int ret;
  399. /* Detach the KMS framebuffer from crtcs */
  400. if (par->set_mode) {
  401. struct drm_mode_set set;
  402. set.crtc = par->crtc;
  403. set.x = 0;
  404. set.y = 0;
  405. set.mode = NULL;
  406. set.fb = NULL;
  407. set.num_connectors = 0;
  408. set.connectors = &par->con;
  409. ret = vmwgfx_set_config_internal(&set);
  410. if (ret) {
  411. DRM_ERROR("Could not unset a mode.\n");
  412. return ret;
  413. }
  414. drm_mode_destroy(par->vmw_priv->dev, par->set_mode);
  415. par->set_mode = NULL;
  416. }
  417. if (cur_fb) {
  418. drm_framebuffer_put(cur_fb);
  419. par->set_fb = NULL;
  420. }
  421. if (par->vmw_bo && detach_bo && unref_bo)
  422. vmw_dmabuf_unreference(&par->vmw_bo);
  423. return 0;
  424. }
  425. static int vmw_fb_kms_framebuffer(struct fb_info *info)
  426. {
  427. struct drm_mode_fb_cmd2 mode_cmd;
  428. struct vmw_fb_par *par = info->par;
  429. struct fb_var_screeninfo *var = &info->var;
  430. struct drm_framebuffer *cur_fb;
  431. struct vmw_framebuffer *vfb;
  432. int ret = 0, depth;
  433. size_t new_bo_size;
  434. ret = vmw_fb_compute_depth(var, &depth);
  435. if (ret)
  436. return ret;
  437. mode_cmd.width = var->xres;
  438. mode_cmd.height = var->yres;
  439. mode_cmd.pitches[0] = ((var->bits_per_pixel + 7) / 8) * mode_cmd.width;
  440. mode_cmd.pixel_format =
  441. drm_mode_legacy_fb_format(var->bits_per_pixel, depth);
  442. cur_fb = par->set_fb;
  443. if (cur_fb && cur_fb->width == mode_cmd.width &&
  444. cur_fb->height == mode_cmd.height &&
  445. cur_fb->format->format == mode_cmd.pixel_format &&
  446. cur_fb->pitches[0] == mode_cmd.pitches[0])
  447. return 0;
  448. /* Need new buffer object ? */
  449. new_bo_size = (size_t) mode_cmd.pitches[0] * (size_t) mode_cmd.height;
  450. ret = vmw_fb_kms_detach(par,
  451. par->bo_size < new_bo_size ||
  452. par->bo_size > 2*new_bo_size,
  453. true);
  454. if (ret)
  455. return ret;
  456. if (!par->vmw_bo) {
  457. ret = vmw_fb_create_bo(par->vmw_priv, new_bo_size,
  458. &par->vmw_bo);
  459. if (ret) {
  460. DRM_ERROR("Failed creating a buffer object for "
  461. "fbdev.\n");
  462. return ret;
  463. }
  464. par->bo_size = new_bo_size;
  465. }
  466. vfb = vmw_kms_new_framebuffer(par->vmw_priv, par->vmw_bo, NULL,
  467. true, &mode_cmd);
  468. if (IS_ERR(vfb))
  469. return PTR_ERR(vfb);
  470. par->set_fb = &vfb->base;
  471. return 0;
  472. }
  473. static int vmw_fb_set_par(struct fb_info *info)
  474. {
  475. struct vmw_fb_par *par = info->par;
  476. struct vmw_private *vmw_priv = par->vmw_priv;
  477. struct drm_mode_set set;
  478. struct fb_var_screeninfo *var = &info->var;
  479. struct drm_display_mode new_mode = { DRM_MODE("fb_mode",
  480. DRM_MODE_TYPE_DRIVER,
  481. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  482. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  483. };
  484. struct drm_display_mode *old_mode;
  485. struct drm_display_mode *mode;
  486. int ret;
  487. old_mode = par->set_mode;
  488. mode = drm_mode_duplicate(vmw_priv->dev, &new_mode);
  489. if (!mode) {
  490. DRM_ERROR("Could not create new fb mode.\n");
  491. return -ENOMEM;
  492. }
  493. mode->hdisplay = var->xres;
  494. mode->vdisplay = var->yres;
  495. vmw_guess_mode_timing(mode);
  496. if (old_mode && drm_mode_equal(old_mode, mode)) {
  497. drm_mode_destroy(vmw_priv->dev, mode);
  498. mode = old_mode;
  499. old_mode = NULL;
  500. } else if (!vmw_kms_validate_mode_vram(vmw_priv,
  501. mode->hdisplay *
  502. DIV_ROUND_UP(var->bits_per_pixel, 8),
  503. mode->vdisplay)) {
  504. drm_mode_destroy(vmw_priv->dev, mode);
  505. return -EINVAL;
  506. }
  507. mutex_lock(&par->bo_mutex);
  508. ret = vmw_fb_kms_framebuffer(info);
  509. if (ret)
  510. goto out_unlock;
  511. par->fb_x = var->xoffset;
  512. par->fb_y = var->yoffset;
  513. set.crtc = par->crtc;
  514. set.x = 0;
  515. set.y = 0;
  516. set.mode = mode;
  517. set.fb = par->set_fb;
  518. set.num_connectors = 1;
  519. set.connectors = &par->con;
  520. ret = vmwgfx_set_config_internal(&set);
  521. if (ret)
  522. goto out_unlock;
  523. vmw_fb_dirty_mark(par, par->fb_x, par->fb_y,
  524. par->set_fb->width, par->set_fb->height);
  525. /* If there already was stuff dirty we wont
  526. * schedule a new work, so lets do it now */
  527. schedule_delayed_work(&par->local_work, 0);
  528. out_unlock:
  529. if (old_mode)
  530. drm_mode_destroy(vmw_priv->dev, old_mode);
  531. par->set_mode = mode;
  532. mutex_unlock(&par->bo_mutex);
  533. return ret;
  534. }
  535. static struct fb_ops vmw_fb_ops = {
  536. .owner = THIS_MODULE,
  537. .fb_check_var = vmw_fb_check_var,
  538. .fb_set_par = vmw_fb_set_par,
  539. .fb_setcolreg = vmw_fb_setcolreg,
  540. .fb_fillrect = vmw_fb_fillrect,
  541. .fb_copyarea = vmw_fb_copyarea,
  542. .fb_imageblit = vmw_fb_imageblit,
  543. .fb_pan_display = vmw_fb_pan_display,
  544. .fb_blank = vmw_fb_blank,
  545. };
  546. int vmw_fb_init(struct vmw_private *vmw_priv)
  547. {
  548. struct device *device = &vmw_priv->dev->pdev->dev;
  549. struct vmw_fb_par *par;
  550. struct fb_info *info;
  551. unsigned fb_width, fb_height;
  552. unsigned fb_bpp, fb_depth, fb_offset, fb_pitch, fb_size;
  553. struct drm_display_mode *init_mode;
  554. int ret;
  555. fb_bpp = 32;
  556. fb_depth = 24;
  557. /* XXX As shouldn't these be as well. */
  558. fb_width = min(vmw_priv->fb_max_width, (unsigned)2048);
  559. fb_height = min(vmw_priv->fb_max_height, (unsigned)2048);
  560. fb_pitch = fb_width * fb_bpp / 8;
  561. fb_size = fb_pitch * fb_height;
  562. fb_offset = vmw_read(vmw_priv, SVGA_REG_FB_OFFSET);
  563. info = framebuffer_alloc(sizeof(*par), device);
  564. if (!info)
  565. return -ENOMEM;
  566. /*
  567. * Par
  568. */
  569. vmw_priv->fb_info = info;
  570. par = info->par;
  571. memset(par, 0, sizeof(*par));
  572. INIT_DELAYED_WORK(&par->local_work, &vmw_fb_dirty_flush);
  573. par->vmw_priv = vmw_priv;
  574. par->vmalloc = NULL;
  575. par->max_width = fb_width;
  576. par->max_height = fb_height;
  577. ret = vmw_kms_fbdev_init_data(vmw_priv, 0, par->max_width,
  578. par->max_height, &par->con,
  579. &par->crtc, &init_mode);
  580. if (ret)
  581. goto err_kms;
  582. info->var.xres = init_mode->hdisplay;
  583. info->var.yres = init_mode->vdisplay;
  584. /*
  585. * Create buffers and alloc memory
  586. */
  587. par->vmalloc = vzalloc(fb_size);
  588. if (unlikely(par->vmalloc == NULL)) {
  589. ret = -ENOMEM;
  590. goto err_free;
  591. }
  592. /*
  593. * Fixed and var
  594. */
  595. strcpy(info->fix.id, "svgadrmfb");
  596. info->fix.type = FB_TYPE_PACKED_PIXELS;
  597. info->fix.visual = FB_VISUAL_TRUECOLOR;
  598. info->fix.type_aux = 0;
  599. info->fix.xpanstep = 1; /* doing it in hw */
  600. info->fix.ypanstep = 1; /* doing it in hw */
  601. info->fix.ywrapstep = 0;
  602. info->fix.accel = FB_ACCEL_NONE;
  603. info->fix.line_length = fb_pitch;
  604. info->fix.smem_start = 0;
  605. info->fix.smem_len = fb_size;
  606. info->pseudo_palette = par->pseudo_palette;
  607. info->screen_base = (char __iomem *)par->vmalloc;
  608. info->screen_size = fb_size;
  609. info->fbops = &vmw_fb_ops;
  610. /* 24 depth per default */
  611. info->var.red.offset = 16;
  612. info->var.green.offset = 8;
  613. info->var.blue.offset = 0;
  614. info->var.red.length = 8;
  615. info->var.green.length = 8;
  616. info->var.blue.length = 8;
  617. info->var.transp.offset = 0;
  618. info->var.transp.length = 0;
  619. info->var.xres_virtual = fb_width;
  620. info->var.yres_virtual = fb_height;
  621. info->var.bits_per_pixel = fb_bpp;
  622. info->var.xoffset = 0;
  623. info->var.yoffset = 0;
  624. info->var.activate = FB_ACTIVATE_NOW;
  625. info->var.height = -1;
  626. info->var.width = -1;
  627. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  628. info->apertures = alloc_apertures(1);
  629. if (!info->apertures) {
  630. ret = -ENOMEM;
  631. goto err_aper;
  632. }
  633. info->apertures->ranges[0].base = vmw_priv->vram_start;
  634. info->apertures->ranges[0].size = vmw_priv->vram_size;
  635. /*
  636. * Dirty & Deferred IO
  637. */
  638. par->dirty.x1 = par->dirty.x2 = 0;
  639. par->dirty.y1 = par->dirty.y2 = 0;
  640. par->dirty.active = true;
  641. spin_lock_init(&par->dirty.lock);
  642. mutex_init(&par->bo_mutex);
  643. info->fbdefio = &vmw_defio;
  644. fb_deferred_io_init(info);
  645. ret = register_framebuffer(info);
  646. if (unlikely(ret != 0))
  647. goto err_defio;
  648. vmw_fb_set_par(info);
  649. return 0;
  650. err_defio:
  651. fb_deferred_io_cleanup(info);
  652. err_aper:
  653. err_free:
  654. vfree(par->vmalloc);
  655. err_kms:
  656. framebuffer_release(info);
  657. vmw_priv->fb_info = NULL;
  658. return ret;
  659. }
  660. int vmw_fb_close(struct vmw_private *vmw_priv)
  661. {
  662. struct fb_info *info;
  663. struct vmw_fb_par *par;
  664. if (!vmw_priv->fb_info)
  665. return 0;
  666. info = vmw_priv->fb_info;
  667. par = info->par;
  668. /* ??? order */
  669. fb_deferred_io_cleanup(info);
  670. cancel_delayed_work_sync(&par->local_work);
  671. unregister_framebuffer(info);
  672. mutex_lock(&par->bo_mutex);
  673. (void) vmw_fb_kms_detach(par, true, true);
  674. mutex_unlock(&par->bo_mutex);
  675. vfree(par->vmalloc);
  676. framebuffer_release(info);
  677. return 0;
  678. }
  679. int vmw_fb_off(struct vmw_private *vmw_priv)
  680. {
  681. struct fb_info *info;
  682. struct vmw_fb_par *par;
  683. unsigned long flags;
  684. if (!vmw_priv->fb_info)
  685. return -EINVAL;
  686. info = vmw_priv->fb_info;
  687. par = info->par;
  688. spin_lock_irqsave(&par->dirty.lock, flags);
  689. par->dirty.active = false;
  690. spin_unlock_irqrestore(&par->dirty.lock, flags);
  691. flush_delayed_work(&info->deferred_work);
  692. flush_delayed_work(&par->local_work);
  693. return 0;
  694. }
  695. int vmw_fb_on(struct vmw_private *vmw_priv)
  696. {
  697. struct fb_info *info;
  698. struct vmw_fb_par *par;
  699. unsigned long flags;
  700. if (!vmw_priv->fb_info)
  701. return -EINVAL;
  702. info = vmw_priv->fb_info;
  703. par = info->par;
  704. spin_lock_irqsave(&par->dirty.lock, flags);
  705. par->dirty.active = true;
  706. spin_unlock_irqrestore(&par->dirty.lock, flags);
  707. /*
  708. * Need to reschedule a dirty update, because otherwise that's
  709. * only done in dirty_mark() if the previous coalesced
  710. * dirty region was empty.
  711. */
  712. schedule_delayed_work(&par->local_work, 0);
  713. return 0;
  714. }