vmwgfx_fb.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 = 0;
  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. return 0;
  445. }
  446. static int vmw_fb_set_par(struct fb_info *info)
  447. {
  448. struct vmw_fb_par *par = info->par;
  449. struct vmw_private *vmw_priv = par->vmw_priv;
  450. struct drm_mode_set set;
  451. struct fb_var_screeninfo *var = &info->var;
  452. struct drm_display_mode new_mode = { DRM_MODE("fb_mode",
  453. DRM_MODE_TYPE_DRIVER,
  454. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  455. DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
  456. };
  457. struct drm_display_mode *mode;
  458. int ret;
  459. mode = drm_mode_duplicate(vmw_priv->dev, &new_mode);
  460. if (!mode) {
  461. DRM_ERROR("Could not create new fb mode.\n");
  462. return -ENOMEM;
  463. }
  464. mode->hdisplay = var->xres;
  465. mode->vdisplay = var->yres;
  466. vmw_guess_mode_timing(mode);
  467. if (!vmw_kms_validate_mode_vram(vmw_priv,
  468. mode->hdisplay *
  469. DIV_ROUND_UP(var->bits_per_pixel, 8),
  470. mode->vdisplay)) {
  471. drm_mode_destroy(vmw_priv->dev, mode);
  472. return -EINVAL;
  473. }
  474. mutex_lock(&par->bo_mutex);
  475. drm_modeset_lock_all(vmw_priv->dev);
  476. ret = vmw_fb_kms_framebuffer(info);
  477. if (ret)
  478. goto out_unlock;
  479. par->fb_x = var->xoffset;
  480. par->fb_y = var->yoffset;
  481. set.crtc = par->crtc;
  482. set.x = 0;
  483. set.y = 0;
  484. set.mode = mode;
  485. set.fb = par->set_fb;
  486. set.num_connectors = 1;
  487. set.connectors = &par->con;
  488. ret = drm_mode_set_config_internal(&set);
  489. if (ret)
  490. goto out_unlock;
  491. if (!par->bo_ptr) {
  492. struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(set.fb);
  493. /*
  494. * Pin before mapping. Since we don't know in what placement
  495. * to pin, call into KMS to do it for us.
  496. */
  497. ret = vfb->pin(vfb);
  498. if (ret) {
  499. DRM_ERROR("Could not pin the fbdev framebuffer.\n");
  500. goto out_unlock;
  501. }
  502. ret = ttm_bo_kmap(&par->vmw_bo->base, 0,
  503. par->vmw_bo->base.num_pages, &par->map);
  504. if (ret) {
  505. vfb->unpin(vfb);
  506. DRM_ERROR("Could not map the fbdev framebuffer.\n");
  507. goto out_unlock;
  508. }
  509. par->bo_ptr = ttm_kmap_obj_virtual(&par->map, &par->bo_iowrite);
  510. }
  511. vmw_fb_dirty_mark(par, par->fb_x, par->fb_y,
  512. par->set_fb->width, par->set_fb->height);
  513. /* If there already was stuff dirty we wont
  514. * schedule a new work, so lets do it now */
  515. schedule_delayed_work(&par->local_work, 0);
  516. out_unlock:
  517. if (par->set_mode)
  518. drm_mode_destroy(vmw_priv->dev, par->set_mode);
  519. par->set_mode = mode;
  520. drm_modeset_unlock_all(vmw_priv->dev);
  521. mutex_unlock(&par->bo_mutex);
  522. return ret;
  523. }
  524. static struct fb_ops vmw_fb_ops = {
  525. .owner = THIS_MODULE,
  526. .fb_check_var = vmw_fb_check_var,
  527. .fb_set_par = vmw_fb_set_par,
  528. .fb_setcolreg = vmw_fb_setcolreg,
  529. .fb_fillrect = vmw_fb_fillrect,
  530. .fb_copyarea = vmw_fb_copyarea,
  531. .fb_imageblit = vmw_fb_imageblit,
  532. .fb_pan_display = vmw_fb_pan_display,
  533. .fb_blank = vmw_fb_blank,
  534. };
  535. int vmw_fb_init(struct vmw_private *vmw_priv)
  536. {
  537. struct device *device = &vmw_priv->dev->pdev->dev;
  538. struct vmw_fb_par *par;
  539. struct fb_info *info;
  540. unsigned fb_width, fb_height;
  541. unsigned fb_bpp, fb_depth, fb_offset, fb_pitch, fb_size;
  542. struct drm_display_mode *init_mode;
  543. int ret;
  544. fb_bpp = 32;
  545. fb_depth = 24;
  546. /* XXX As shouldn't these be as well. */
  547. fb_width = min(vmw_priv->fb_max_width, (unsigned)2048);
  548. fb_height = min(vmw_priv->fb_max_height, (unsigned)2048);
  549. fb_pitch = fb_width * fb_bpp / 8;
  550. fb_size = fb_pitch * fb_height;
  551. fb_offset = vmw_read(vmw_priv, SVGA_REG_FB_OFFSET);
  552. info = framebuffer_alloc(sizeof(*par), device);
  553. if (!info)
  554. return -ENOMEM;
  555. /*
  556. * Par
  557. */
  558. vmw_priv->fb_info = info;
  559. par = info->par;
  560. memset(par, 0, sizeof(*par));
  561. INIT_DELAYED_WORK(&par->local_work, &vmw_fb_dirty_flush);
  562. par->vmw_priv = vmw_priv;
  563. par->vmalloc = NULL;
  564. par->max_width = fb_width;
  565. par->max_height = fb_height;
  566. drm_modeset_lock_all(vmw_priv->dev);
  567. ret = vmw_kms_fbdev_init_data(vmw_priv, 0, par->max_width,
  568. par->max_height, &par->con,
  569. &par->crtc, &init_mode);
  570. if (ret) {
  571. drm_modeset_unlock_all(vmw_priv->dev);
  572. goto err_kms;
  573. }
  574. info->var.xres = init_mode->hdisplay;
  575. info->var.yres = init_mode->vdisplay;
  576. drm_modeset_unlock_all(vmw_priv->dev);
  577. /*
  578. * Create buffers and alloc memory
  579. */
  580. par->vmalloc = vzalloc(fb_size);
  581. if (unlikely(par->vmalloc == NULL)) {
  582. ret = -ENOMEM;
  583. goto err_free;
  584. }
  585. /*
  586. * Fixed and var
  587. */
  588. strcpy(info->fix.id, "svgadrmfb");
  589. info->fix.type = FB_TYPE_PACKED_PIXELS;
  590. info->fix.visual = FB_VISUAL_TRUECOLOR;
  591. info->fix.type_aux = 0;
  592. info->fix.xpanstep = 1; /* doing it in hw */
  593. info->fix.ypanstep = 1; /* doing it in hw */
  594. info->fix.ywrapstep = 0;
  595. info->fix.accel = FB_ACCEL_NONE;
  596. info->fix.line_length = fb_pitch;
  597. info->fix.smem_start = 0;
  598. info->fix.smem_len = fb_size;
  599. info->pseudo_palette = par->pseudo_palette;
  600. info->screen_base = (char __iomem *)par->vmalloc;
  601. info->screen_size = fb_size;
  602. info->flags = FBINFO_DEFAULT;
  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. (void) vmw_fb_kms_detach(par, true, true);
  667. vfree(par->vmalloc);
  668. framebuffer_release(info);
  669. return 0;
  670. }
  671. int vmw_fb_off(struct vmw_private *vmw_priv)
  672. {
  673. struct fb_info *info;
  674. struct vmw_fb_par *par;
  675. unsigned long flags;
  676. if (!vmw_priv->fb_info)
  677. return -EINVAL;
  678. info = vmw_priv->fb_info;
  679. par = info->par;
  680. spin_lock_irqsave(&par->dirty.lock, flags);
  681. par->dirty.active = false;
  682. spin_unlock_irqrestore(&par->dirty.lock, flags);
  683. flush_delayed_work(&info->deferred_work);
  684. flush_delayed_work(&par->local_work);
  685. mutex_lock(&par->bo_mutex);
  686. drm_modeset_lock_all(vmw_priv->dev);
  687. (void) vmw_fb_kms_detach(par, true, false);
  688. drm_modeset_unlock_all(vmw_priv->dev);
  689. mutex_unlock(&par->bo_mutex);
  690. return 0;
  691. }
  692. int vmw_fb_on(struct vmw_private *vmw_priv)
  693. {
  694. struct fb_info *info;
  695. struct vmw_fb_par *par;
  696. unsigned long flags;
  697. if (!vmw_priv->fb_info)
  698. return -EINVAL;
  699. info = vmw_priv->fb_info;
  700. par = info->par;
  701. vmw_fb_set_par(info);
  702. spin_lock_irqsave(&par->dirty.lock, flags);
  703. par->dirty.active = true;
  704. spin_unlock_irqrestore(&par->dirty.lock, flags);
  705. return 0;
  706. }