vmwgfx_fb.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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_buffer_object *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_buffer_object *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_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. 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_buffer_object **out)
  342. {
  343. struct vmw_buffer_object *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_bo_init(vmw_priv, vmw_bo, size,
  352. &vmw_sys_placement,
  353. false,
  354. &vmw_bo_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_bo_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 *mode;
  485. int ret;
  486. mode = drm_mode_duplicate(vmw_priv->dev, &new_mode);
  487. if (!mode) {
  488. DRM_ERROR("Could not create new fb mode.\n");
  489. return -ENOMEM;
  490. }
  491. mode->hdisplay = var->xres;
  492. mode->vdisplay = var->yres;
  493. vmw_guess_mode_timing(mode);
  494. if (!vmw_kms_validate_mode_vram(vmw_priv,
  495. mode->hdisplay *
  496. DIV_ROUND_UP(var->bits_per_pixel, 8),
  497. mode->vdisplay)) {
  498. drm_mode_destroy(vmw_priv->dev, mode);
  499. return -EINVAL;
  500. }
  501. mutex_lock(&par->bo_mutex);
  502. ret = vmw_fb_kms_framebuffer(info);
  503. if (ret)
  504. goto out_unlock;
  505. par->fb_x = var->xoffset;
  506. par->fb_y = var->yoffset;
  507. set.crtc = par->crtc;
  508. set.x = 0;
  509. set.y = 0;
  510. set.mode = mode;
  511. set.fb = par->set_fb;
  512. set.num_connectors = 1;
  513. set.connectors = &par->con;
  514. ret = vmwgfx_set_config_internal(&set);
  515. if (ret)
  516. goto out_unlock;
  517. vmw_fb_dirty_mark(par, par->fb_x, par->fb_y,
  518. par->set_fb->width, par->set_fb->height);
  519. /* If there already was stuff dirty we wont
  520. * schedule a new work, so lets do it now */
  521. schedule_delayed_work(&par->local_work, 0);
  522. out_unlock:
  523. if (par->set_mode)
  524. drm_mode_destroy(vmw_priv->dev, par->set_mode);
  525. par->set_mode = mode;
  526. mutex_unlock(&par->bo_mutex);
  527. return ret;
  528. }
  529. static struct fb_ops vmw_fb_ops = {
  530. .owner = THIS_MODULE,
  531. .fb_check_var = vmw_fb_check_var,
  532. .fb_set_par = vmw_fb_set_par,
  533. .fb_setcolreg = vmw_fb_setcolreg,
  534. .fb_fillrect = vmw_fb_fillrect,
  535. .fb_copyarea = vmw_fb_copyarea,
  536. .fb_imageblit = vmw_fb_imageblit,
  537. .fb_pan_display = vmw_fb_pan_display,
  538. .fb_blank = vmw_fb_blank,
  539. };
  540. int vmw_fb_init(struct vmw_private *vmw_priv)
  541. {
  542. struct device *device = &vmw_priv->dev->pdev->dev;
  543. struct vmw_fb_par *par;
  544. struct fb_info *info;
  545. unsigned fb_width, fb_height;
  546. unsigned fb_bpp, fb_depth, fb_offset, fb_pitch, fb_size;
  547. struct drm_display_mode *init_mode;
  548. int ret;
  549. fb_bpp = 32;
  550. fb_depth = 24;
  551. /* XXX As shouldn't these be as well. */
  552. fb_width = min(vmw_priv->fb_max_width, (unsigned)2048);
  553. fb_height = min(vmw_priv->fb_max_height, (unsigned)2048);
  554. fb_pitch = fb_width * fb_bpp / 8;
  555. fb_size = fb_pitch * fb_height;
  556. fb_offset = vmw_read(vmw_priv, SVGA_REG_FB_OFFSET);
  557. info = framebuffer_alloc(sizeof(*par), device);
  558. if (!info)
  559. return -ENOMEM;
  560. /*
  561. * Par
  562. */
  563. vmw_priv->fb_info = info;
  564. par = info->par;
  565. memset(par, 0, sizeof(*par));
  566. INIT_DELAYED_WORK(&par->local_work, &vmw_fb_dirty_flush);
  567. par->vmw_priv = vmw_priv;
  568. par->vmalloc = NULL;
  569. par->max_width = fb_width;
  570. par->max_height = fb_height;
  571. ret = vmw_kms_fbdev_init_data(vmw_priv, 0, par->max_width,
  572. par->max_height, &par->con,
  573. &par->crtc, &init_mode);
  574. if (ret)
  575. goto err_kms;
  576. info->var.xres = init_mode->hdisplay;
  577. info->var.yres = init_mode->vdisplay;
  578. /*
  579. * Create buffers and alloc memory
  580. */
  581. par->vmalloc = vzalloc(fb_size);
  582. if (unlikely(par->vmalloc == NULL)) {
  583. ret = -ENOMEM;
  584. goto err_free;
  585. }
  586. /*
  587. * Fixed and var
  588. */
  589. strcpy(info->fix.id, "svgadrmfb");
  590. info->fix.type = FB_TYPE_PACKED_PIXELS;
  591. info->fix.visual = FB_VISUAL_TRUECOLOR;
  592. info->fix.type_aux = 0;
  593. info->fix.xpanstep = 1; /* doing it in hw */
  594. info->fix.ypanstep = 1; /* doing it in hw */
  595. info->fix.ywrapstep = 0;
  596. info->fix.accel = FB_ACCEL_NONE;
  597. info->fix.line_length = fb_pitch;
  598. info->fix.smem_start = 0;
  599. info->fix.smem_len = fb_size;
  600. info->pseudo_palette = par->pseudo_palette;
  601. info->screen_base = (char __iomem *)par->vmalloc;
  602. info->screen_size = fb_size;
  603. info->fbops = &vmw_fb_ops;
  604. /* 24 depth per default */
  605. info->var.red.offset = 16;
  606. info->var.green.offset = 8;
  607. info->var.blue.offset = 0;
  608. info->var.red.length = 8;
  609. info->var.green.length = 8;
  610. info->var.blue.length = 8;
  611. info->var.transp.offset = 0;
  612. info->var.transp.length = 0;
  613. info->var.xres_virtual = fb_width;
  614. info->var.yres_virtual = fb_height;
  615. info->var.bits_per_pixel = fb_bpp;
  616. info->var.xoffset = 0;
  617. info->var.yoffset = 0;
  618. info->var.activate = FB_ACTIVATE_NOW;
  619. info->var.height = -1;
  620. info->var.width = -1;
  621. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  622. info->apertures = alloc_apertures(1);
  623. if (!info->apertures) {
  624. ret = -ENOMEM;
  625. goto err_aper;
  626. }
  627. info->apertures->ranges[0].base = vmw_priv->vram_start;
  628. info->apertures->ranges[0].size = vmw_priv->vram_size;
  629. /*
  630. * Dirty & Deferred IO
  631. */
  632. par->dirty.x1 = par->dirty.x2 = 0;
  633. par->dirty.y1 = par->dirty.y2 = 0;
  634. par->dirty.active = true;
  635. spin_lock_init(&par->dirty.lock);
  636. mutex_init(&par->bo_mutex);
  637. info->fbdefio = &vmw_defio;
  638. fb_deferred_io_init(info);
  639. ret = register_framebuffer(info);
  640. if (unlikely(ret != 0))
  641. goto err_defio;
  642. vmw_fb_set_par(info);
  643. return 0;
  644. err_defio:
  645. fb_deferred_io_cleanup(info);
  646. err_aper:
  647. err_free:
  648. vfree(par->vmalloc);
  649. err_kms:
  650. framebuffer_release(info);
  651. vmw_priv->fb_info = NULL;
  652. return ret;
  653. }
  654. int vmw_fb_close(struct vmw_private *vmw_priv)
  655. {
  656. struct fb_info *info;
  657. struct vmw_fb_par *par;
  658. if (!vmw_priv->fb_info)
  659. return 0;
  660. info = vmw_priv->fb_info;
  661. par = info->par;
  662. /* ??? order */
  663. fb_deferred_io_cleanup(info);
  664. cancel_delayed_work_sync(&par->local_work);
  665. unregister_framebuffer(info);
  666. mutex_lock(&par->bo_mutex);
  667. (void) vmw_fb_kms_detach(par, true, true);
  668. mutex_unlock(&par->bo_mutex);
  669. vfree(par->vmalloc);
  670. framebuffer_release(info);
  671. return 0;
  672. }
  673. int vmw_fb_off(struct vmw_private *vmw_priv)
  674. {
  675. struct fb_info *info;
  676. struct vmw_fb_par *par;
  677. unsigned long flags;
  678. if (!vmw_priv->fb_info)
  679. return -EINVAL;
  680. info = vmw_priv->fb_info;
  681. par = info->par;
  682. spin_lock_irqsave(&par->dirty.lock, flags);
  683. par->dirty.active = false;
  684. spin_unlock_irqrestore(&par->dirty.lock, flags);
  685. flush_delayed_work(&info->deferred_work);
  686. flush_delayed_work(&par->local_work);
  687. return 0;
  688. }
  689. int vmw_fb_on(struct vmw_private *vmw_priv)
  690. {
  691. struct fb_info *info;
  692. struct vmw_fb_par *par;
  693. unsigned long flags;
  694. if (!vmw_priv->fb_info)
  695. return -EINVAL;
  696. info = vmw_priv->fb_info;
  697. par = info->par;
  698. spin_lock_irqsave(&par->dirty.lock, flags);
  699. par->dirty.active = true;
  700. spin_unlock_irqrestore(&par->dirty.lock, flags);
  701. /*
  702. * Need to reschedule a dirty update, because otherwise that's
  703. * only done in dirty_mark() if the previous coalesced
  704. * dirty region was empty.
  705. */
  706. schedule_delayed_work(&par->local_work, 0);
  707. return 0;
  708. }