vmwgfx_fb.c 21 KB

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