vmwgfx_fb.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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_framebuffer *fb;
  381. struct drm_crtc *tmp;
  382. struct drm_device *dev = set->crtc->dev;
  383. struct drm_modeset_acquire_ctx ctx;
  384. int ret;
  385. drm_modeset_acquire_init(&ctx, 0);
  386. restart:
  387. /*
  388. * NOTE: ->set_config can also disable other crtcs (if we steal all
  389. * connectors from it), hence we need to refcount the fbs across all
  390. * crtcs. Atomic modeset will have saner semantics ...
  391. */
  392. drm_for_each_crtc(tmp, dev)
  393. tmp->primary->old_fb = tmp->primary->fb;
  394. fb = set->fb;
  395. ret = crtc->funcs->set_config(set, &ctx);
  396. if (ret == 0) {
  397. crtc->primary->crtc = crtc;
  398. crtc->primary->fb = fb;
  399. }
  400. drm_for_each_crtc(tmp, dev) {
  401. if (tmp->primary->fb)
  402. drm_framebuffer_get(tmp->primary->fb);
  403. if (tmp->primary->old_fb)
  404. drm_framebuffer_put(tmp->primary->old_fb);
  405. tmp->primary->old_fb = NULL;
  406. }
  407. if (ret == -EDEADLK) {
  408. drm_modeset_backoff(&ctx);
  409. goto restart;
  410. }
  411. drm_modeset_drop_locks(&ctx);
  412. drm_modeset_acquire_fini(&ctx);
  413. return ret;
  414. }
  415. static int vmw_fb_kms_detach(struct vmw_fb_par *par,
  416. bool detach_bo,
  417. bool unref_bo)
  418. {
  419. struct drm_framebuffer *cur_fb = par->set_fb;
  420. int ret;
  421. /* Detach the KMS framebuffer from crtcs */
  422. if (par->set_mode) {
  423. struct drm_mode_set set;
  424. set.crtc = par->crtc;
  425. set.x = 0;
  426. set.y = 0;
  427. set.mode = NULL;
  428. set.fb = NULL;
  429. set.num_connectors = 0;
  430. set.connectors = &par->con;
  431. ret = vmwgfx_set_config_internal(&set);
  432. if (ret) {
  433. DRM_ERROR("Could not unset a mode.\n");
  434. return ret;
  435. }
  436. drm_mode_destroy(par->vmw_priv->dev, par->set_mode);
  437. par->set_mode = NULL;
  438. }
  439. if (cur_fb) {
  440. drm_framebuffer_put(cur_fb);
  441. par->set_fb = NULL;
  442. }
  443. if (par->vmw_bo && detach_bo && unref_bo)
  444. vmw_dmabuf_unreference(&par->vmw_bo);
  445. return 0;
  446. }
  447. static int vmw_fb_kms_framebuffer(struct fb_info *info)
  448. {
  449. struct drm_mode_fb_cmd2 mode_cmd;
  450. struct vmw_fb_par *par = info->par;
  451. struct fb_var_screeninfo *var = &info->var;
  452. struct drm_framebuffer *cur_fb;
  453. struct vmw_framebuffer *vfb;
  454. int ret = 0, depth;
  455. size_t new_bo_size;
  456. ret = vmw_fb_compute_depth(var, &depth);
  457. if (ret)
  458. return ret;
  459. mode_cmd.width = var->xres;
  460. mode_cmd.height = var->yres;
  461. mode_cmd.pitches[0] = ((var->bits_per_pixel + 7) / 8) * mode_cmd.width;
  462. mode_cmd.pixel_format =
  463. drm_mode_legacy_fb_format(var->bits_per_pixel, depth);
  464. cur_fb = par->set_fb;
  465. if (cur_fb && cur_fb->width == mode_cmd.width &&
  466. cur_fb->height == mode_cmd.height &&
  467. cur_fb->format->format == mode_cmd.pixel_format &&
  468. cur_fb->pitches[0] == mode_cmd.pitches[0])
  469. return 0;
  470. /* Need new buffer object ? */
  471. new_bo_size = (size_t) mode_cmd.pitches[0] * (size_t) mode_cmd.height;
  472. ret = vmw_fb_kms_detach(par,
  473. par->bo_size < new_bo_size ||
  474. par->bo_size > 2*new_bo_size,
  475. true);
  476. if (ret)
  477. return ret;
  478. if (!par->vmw_bo) {
  479. ret = vmw_fb_create_bo(par->vmw_priv, new_bo_size,
  480. &par->vmw_bo);
  481. if (ret) {
  482. DRM_ERROR("Failed creating a buffer object for "
  483. "fbdev.\n");
  484. return ret;
  485. }
  486. par->bo_size = new_bo_size;
  487. }
  488. vfb = vmw_kms_new_framebuffer(par->vmw_priv, par->vmw_bo, NULL,
  489. true, &mode_cmd);
  490. if (IS_ERR(vfb))
  491. return PTR_ERR(vfb);
  492. par->set_fb = &vfb->base;
  493. return 0;
  494. }
  495. static int vmw_fb_set_par(struct fb_info *info)
  496. {
  497. struct vmw_fb_par *par = info->par;
  498. struct vmw_private *vmw_priv = par->vmw_priv;
  499. struct drm_mode_set set;
  500. struct fb_var_screeninfo *var = &info->var;
  501. struct drm_display_mode new_mode = { DRM_MODE("fb_mode",
  502. DRM_MODE_TYPE_DRIVER,
  503. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  504. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  505. };
  506. struct drm_display_mode *old_mode;
  507. struct drm_display_mode *mode;
  508. int ret;
  509. old_mode = par->set_mode;
  510. mode = drm_mode_duplicate(vmw_priv->dev, &new_mode);
  511. if (!mode) {
  512. DRM_ERROR("Could not create new fb mode.\n");
  513. return -ENOMEM;
  514. }
  515. mode->hdisplay = var->xres;
  516. mode->vdisplay = var->yres;
  517. vmw_guess_mode_timing(mode);
  518. if (old_mode && drm_mode_equal(old_mode, mode)) {
  519. drm_mode_destroy(vmw_priv->dev, mode);
  520. mode = old_mode;
  521. old_mode = NULL;
  522. } else if (!vmw_kms_validate_mode_vram(vmw_priv,
  523. mode->hdisplay *
  524. DIV_ROUND_UP(var->bits_per_pixel, 8),
  525. mode->vdisplay)) {
  526. drm_mode_destroy(vmw_priv->dev, mode);
  527. return -EINVAL;
  528. }
  529. mutex_lock(&par->bo_mutex);
  530. ret = vmw_fb_kms_framebuffer(info);
  531. if (ret)
  532. goto out_unlock;
  533. par->fb_x = var->xoffset;
  534. par->fb_y = var->yoffset;
  535. set.crtc = par->crtc;
  536. set.x = 0;
  537. set.y = 0;
  538. set.mode = mode;
  539. set.fb = par->set_fb;
  540. set.num_connectors = 1;
  541. set.connectors = &par->con;
  542. ret = vmwgfx_set_config_internal(&set);
  543. if (ret)
  544. goto out_unlock;
  545. vmw_fb_dirty_mark(par, par->fb_x, par->fb_y,
  546. par->set_fb->width, par->set_fb->height);
  547. /* If there already was stuff dirty we wont
  548. * schedule a new work, so lets do it now */
  549. schedule_delayed_work(&par->local_work, 0);
  550. out_unlock:
  551. if (old_mode)
  552. drm_mode_destroy(vmw_priv->dev, old_mode);
  553. par->set_mode = mode;
  554. mutex_unlock(&par->bo_mutex);
  555. return ret;
  556. }
  557. static struct fb_ops vmw_fb_ops = {
  558. .owner = THIS_MODULE,
  559. .fb_check_var = vmw_fb_check_var,
  560. .fb_set_par = vmw_fb_set_par,
  561. .fb_setcolreg = vmw_fb_setcolreg,
  562. .fb_fillrect = vmw_fb_fillrect,
  563. .fb_copyarea = vmw_fb_copyarea,
  564. .fb_imageblit = vmw_fb_imageblit,
  565. .fb_pan_display = vmw_fb_pan_display,
  566. .fb_blank = vmw_fb_blank,
  567. };
  568. int vmw_fb_init(struct vmw_private *vmw_priv)
  569. {
  570. struct device *device = &vmw_priv->dev->pdev->dev;
  571. struct vmw_fb_par *par;
  572. struct fb_info *info;
  573. unsigned fb_width, fb_height;
  574. unsigned fb_bpp, fb_depth, fb_offset, fb_pitch, fb_size;
  575. struct drm_display_mode *init_mode;
  576. int ret;
  577. fb_bpp = 32;
  578. fb_depth = 24;
  579. /* XXX As shouldn't these be as well. */
  580. fb_width = min(vmw_priv->fb_max_width, (unsigned)2048);
  581. fb_height = min(vmw_priv->fb_max_height, (unsigned)2048);
  582. fb_pitch = fb_width * fb_bpp / 8;
  583. fb_size = fb_pitch * fb_height;
  584. fb_offset = vmw_read(vmw_priv, SVGA_REG_FB_OFFSET);
  585. info = framebuffer_alloc(sizeof(*par), device);
  586. if (!info)
  587. return -ENOMEM;
  588. /*
  589. * Par
  590. */
  591. vmw_priv->fb_info = info;
  592. par = info->par;
  593. memset(par, 0, sizeof(*par));
  594. INIT_DELAYED_WORK(&par->local_work, &vmw_fb_dirty_flush);
  595. par->vmw_priv = vmw_priv;
  596. par->vmalloc = NULL;
  597. par->max_width = fb_width;
  598. par->max_height = fb_height;
  599. ret = vmw_kms_fbdev_init_data(vmw_priv, 0, par->max_width,
  600. par->max_height, &par->con,
  601. &par->crtc, &init_mode);
  602. if (ret)
  603. goto err_kms;
  604. info->var.xres = init_mode->hdisplay;
  605. info->var.yres = init_mode->vdisplay;
  606. /*
  607. * Create buffers and alloc memory
  608. */
  609. par->vmalloc = vzalloc(fb_size);
  610. if (unlikely(par->vmalloc == NULL)) {
  611. ret = -ENOMEM;
  612. goto err_free;
  613. }
  614. /*
  615. * Fixed and var
  616. */
  617. strcpy(info->fix.id, "svgadrmfb");
  618. info->fix.type = FB_TYPE_PACKED_PIXELS;
  619. info->fix.visual = FB_VISUAL_TRUECOLOR;
  620. info->fix.type_aux = 0;
  621. info->fix.xpanstep = 1; /* doing it in hw */
  622. info->fix.ypanstep = 1; /* doing it in hw */
  623. info->fix.ywrapstep = 0;
  624. info->fix.accel = FB_ACCEL_NONE;
  625. info->fix.line_length = fb_pitch;
  626. info->fix.smem_start = 0;
  627. info->fix.smem_len = fb_size;
  628. info->pseudo_palette = par->pseudo_palette;
  629. info->screen_base = (char __iomem *)par->vmalloc;
  630. info->screen_size = fb_size;
  631. info->fbops = &vmw_fb_ops;
  632. /* 24 depth per default */
  633. info->var.red.offset = 16;
  634. info->var.green.offset = 8;
  635. info->var.blue.offset = 0;
  636. info->var.red.length = 8;
  637. info->var.green.length = 8;
  638. info->var.blue.length = 8;
  639. info->var.transp.offset = 0;
  640. info->var.transp.length = 0;
  641. info->var.xres_virtual = fb_width;
  642. info->var.yres_virtual = fb_height;
  643. info->var.bits_per_pixel = fb_bpp;
  644. info->var.xoffset = 0;
  645. info->var.yoffset = 0;
  646. info->var.activate = FB_ACTIVATE_NOW;
  647. info->var.height = -1;
  648. info->var.width = -1;
  649. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  650. info->apertures = alloc_apertures(1);
  651. if (!info->apertures) {
  652. ret = -ENOMEM;
  653. goto err_aper;
  654. }
  655. info->apertures->ranges[0].base = vmw_priv->vram_start;
  656. info->apertures->ranges[0].size = vmw_priv->vram_size;
  657. /*
  658. * Dirty & Deferred IO
  659. */
  660. par->dirty.x1 = par->dirty.x2 = 0;
  661. par->dirty.y1 = par->dirty.y2 = 0;
  662. par->dirty.active = true;
  663. spin_lock_init(&par->dirty.lock);
  664. mutex_init(&par->bo_mutex);
  665. info->fbdefio = &vmw_defio;
  666. fb_deferred_io_init(info);
  667. ret = register_framebuffer(info);
  668. if (unlikely(ret != 0))
  669. goto err_defio;
  670. vmw_fb_set_par(info);
  671. return 0;
  672. err_defio:
  673. fb_deferred_io_cleanup(info);
  674. err_aper:
  675. err_free:
  676. vfree(par->vmalloc);
  677. err_kms:
  678. framebuffer_release(info);
  679. vmw_priv->fb_info = NULL;
  680. return ret;
  681. }
  682. int vmw_fb_close(struct vmw_private *vmw_priv)
  683. {
  684. struct fb_info *info;
  685. struct vmw_fb_par *par;
  686. if (!vmw_priv->fb_info)
  687. return 0;
  688. info = vmw_priv->fb_info;
  689. par = info->par;
  690. /* ??? order */
  691. fb_deferred_io_cleanup(info);
  692. cancel_delayed_work_sync(&par->local_work);
  693. unregister_framebuffer(info);
  694. mutex_lock(&par->bo_mutex);
  695. (void) vmw_fb_kms_detach(par, true, true);
  696. mutex_unlock(&par->bo_mutex);
  697. vfree(par->vmalloc);
  698. framebuffer_release(info);
  699. return 0;
  700. }
  701. int vmw_fb_off(struct vmw_private *vmw_priv)
  702. {
  703. struct fb_info *info;
  704. struct vmw_fb_par *par;
  705. unsigned long flags;
  706. if (!vmw_priv->fb_info)
  707. return -EINVAL;
  708. info = vmw_priv->fb_info;
  709. par = info->par;
  710. spin_lock_irqsave(&par->dirty.lock, flags);
  711. par->dirty.active = false;
  712. spin_unlock_irqrestore(&par->dirty.lock, flags);
  713. flush_delayed_work(&info->deferred_work);
  714. flush_delayed_work(&par->local_work);
  715. return 0;
  716. }
  717. int vmw_fb_on(struct vmw_private *vmw_priv)
  718. {
  719. struct fb_info *info;
  720. struct vmw_fb_par *par;
  721. unsigned long flags;
  722. if (!vmw_priv->fb_info)
  723. return -EINVAL;
  724. info = vmw_priv->fb_info;
  725. par = info->par;
  726. spin_lock_irqsave(&par->dirty.lock, flags);
  727. par->dirty.active = true;
  728. spin_unlock_irqrestore(&par->dirty.lock, flags);
  729. return 0;
  730. }
  731. /**
  732. * vmw_fb_refresh - Refresh fb display
  733. *
  734. * @vmw_priv: Pointer to device private
  735. *
  736. * Call into kms to show the fbdev display(s).
  737. */
  738. void vmw_fb_refresh(struct vmw_private *vmw_priv)
  739. {
  740. if (!vmw_priv->fb_info)
  741. return;
  742. vmw_fb_set_par(vmw_priv->fb_info);
  743. }