vmwgfx_fb.c 19 KB

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