vmwgfx_fb.c 20 KB

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