qxl_fb.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * Copyright © 2013 Red Hat
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * David Airlie
  25. */
  26. #include <linux/module.h>
  27. #include <linux/fb.h>
  28. #include "drmP.h"
  29. #include "drm/drm.h"
  30. #include "drm/drm_crtc.h"
  31. #include "drm/drm_crtc_helper.h"
  32. #include "qxl_drv.h"
  33. #include "qxl_object.h"
  34. #include "drm_fb_helper.h"
  35. #define QXL_DIRTY_DELAY (HZ / 30)
  36. struct qxl_fbdev {
  37. struct drm_fb_helper helper;
  38. struct qxl_framebuffer qfb;
  39. struct list_head fbdev_list;
  40. struct qxl_device *qdev;
  41. void *shadow;
  42. int size;
  43. /* dirty memory logging */
  44. struct {
  45. spinlock_t lock;
  46. bool active;
  47. unsigned x1;
  48. unsigned y1;
  49. unsigned x2;
  50. unsigned y2;
  51. } dirty;
  52. };
  53. static void qxl_fb_image_init(struct qxl_fb_image *qxl_fb_image,
  54. struct qxl_device *qdev, struct fb_info *info,
  55. const struct fb_image *image)
  56. {
  57. qxl_fb_image->qdev = qdev;
  58. if (info) {
  59. qxl_fb_image->visual = info->fix.visual;
  60. if (qxl_fb_image->visual == FB_VISUAL_TRUECOLOR ||
  61. qxl_fb_image->visual == FB_VISUAL_DIRECTCOLOR)
  62. memcpy(&qxl_fb_image->pseudo_palette,
  63. info->pseudo_palette,
  64. sizeof(qxl_fb_image->pseudo_palette));
  65. } else {
  66. /* fallback */
  67. if (image->depth == 1)
  68. qxl_fb_image->visual = FB_VISUAL_MONO10;
  69. else
  70. qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
  71. }
  72. if (image) {
  73. memcpy(&qxl_fb_image->fb_image, image,
  74. sizeof(qxl_fb_image->fb_image));
  75. }
  76. }
  77. static void qxl_fb_dirty_flush(struct fb_info *info)
  78. {
  79. struct qxl_fbdev *qfbdev = info->par;
  80. struct qxl_device *qdev = qfbdev->qdev;
  81. struct qxl_fb_image qxl_fb_image;
  82. struct fb_image *image = &qxl_fb_image.fb_image;
  83. u32 x1, x2, y1, y2;
  84. /* TODO: hard coding 32 bpp */
  85. int stride = qfbdev->qfb.base.pitches[0] * 4;
  86. x1 = qfbdev->dirty.x1;
  87. x2 = qfbdev->dirty.x2;
  88. y1 = qfbdev->dirty.y1;
  89. y2 = qfbdev->dirty.y2;
  90. /*
  91. * we are using a shadow draw buffer, at qdev->surface0_shadow
  92. */
  93. qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]", x1, x2, y1, y2);
  94. image->dx = x1;
  95. image->dy = y1;
  96. image->width = x2 - x1;
  97. image->height = y2 - y1;
  98. image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
  99. warnings */
  100. image->bg_color = 0;
  101. image->depth = 32; /* TODO: take from somewhere? */
  102. image->cmap.start = 0;
  103. image->cmap.len = 0;
  104. image->cmap.red = NULL;
  105. image->cmap.green = NULL;
  106. image->cmap.blue = NULL;
  107. image->cmap.transp = NULL;
  108. image->data = qfbdev->shadow + (x1 * 4) + (stride * y1);
  109. qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
  110. qxl_draw_opaque_fb(&qxl_fb_image, stride);
  111. qfbdev->dirty.x1 = 0;
  112. qfbdev->dirty.x2 = 0;
  113. qfbdev->dirty.y1 = 0;
  114. qfbdev->dirty.y2 = 0;
  115. }
  116. static void qxl_deferred_io(struct fb_info *info,
  117. struct list_head *pagelist)
  118. {
  119. struct qxl_fbdev *qfbdev = info->par;
  120. unsigned long start, end, min, max;
  121. struct page *page;
  122. int y1, y2;
  123. min = ULONG_MAX;
  124. max = 0;
  125. list_for_each_entry(page, pagelist, lru) {
  126. start = page->index << PAGE_SHIFT;
  127. end = start + PAGE_SIZE - 1;
  128. min = min(min, start);
  129. max = max(max, end);
  130. }
  131. if (min < max) {
  132. y1 = min / info->fix.line_length;
  133. y2 = (max / info->fix.line_length) + 1;
  134. /* TODO: add spin lock? */
  135. /* spin_lock_irqsave(&qfbdev->dirty.lock, flags); */
  136. qfbdev->dirty.x1 = 0;
  137. qfbdev->dirty.y1 = y1;
  138. qfbdev->dirty.x2 = info->var.xres;
  139. qfbdev->dirty.y2 = y2;
  140. /* spin_unlock_irqrestore(&qfbdev->dirty.lock, flags); */
  141. }
  142. qxl_fb_dirty_flush(info);
  143. };
  144. static struct fb_deferred_io qxl_defio = {
  145. .delay = QXL_DIRTY_DELAY,
  146. .deferred_io = qxl_deferred_io,
  147. };
  148. static void qxl_fb_fillrect(struct fb_info *info,
  149. const struct fb_fillrect *fb_rect)
  150. {
  151. struct qxl_fbdev *qfbdev = info->par;
  152. struct qxl_device *qdev = qfbdev->qdev;
  153. struct qxl_rect rect;
  154. uint32_t color;
  155. int x = fb_rect->dx;
  156. int y = fb_rect->dy;
  157. int width = fb_rect->width;
  158. int height = fb_rect->height;
  159. uint16_t rop;
  160. struct qxl_draw_fill qxl_draw_fill_rec;
  161. if (info->fix.visual == FB_VISUAL_TRUECOLOR ||
  162. info->fix.visual == FB_VISUAL_DIRECTCOLOR)
  163. color = ((u32 *) (info->pseudo_palette))[fb_rect->color];
  164. else
  165. color = fb_rect->color;
  166. rect.left = x;
  167. rect.right = x + width;
  168. rect.top = y;
  169. rect.bottom = y + height;
  170. switch (fb_rect->rop) {
  171. case ROP_XOR:
  172. rop = SPICE_ROPD_OP_XOR;
  173. break;
  174. case ROP_COPY:
  175. rop = SPICE_ROPD_OP_PUT;
  176. break;
  177. default:
  178. pr_err("qxl_fb_fillrect(): unknown rop, "
  179. "defaulting to SPICE_ROPD_OP_PUT\n");
  180. rop = SPICE_ROPD_OP_PUT;
  181. }
  182. qxl_draw_fill_rec.qdev = qdev;
  183. qxl_draw_fill_rec.rect = rect;
  184. qxl_draw_fill_rec.color = color;
  185. qxl_draw_fill_rec.rop = rop;
  186. if (!drm_can_sleep()) {
  187. qxl_io_log(qdev,
  188. "%s: TODO use RCU, mysterious locks with spin_lock\n",
  189. __func__);
  190. return;
  191. }
  192. qxl_draw_fill(&qxl_draw_fill_rec);
  193. }
  194. static void qxl_fb_copyarea(struct fb_info *info,
  195. const struct fb_copyarea *region)
  196. {
  197. struct qxl_fbdev *qfbdev = info->par;
  198. qxl_draw_copyarea(qfbdev->qdev,
  199. region->width, region->height,
  200. region->sx, region->sy,
  201. region->dx, region->dy);
  202. }
  203. static void qxl_fb_imageblit_safe(struct qxl_fb_image *qxl_fb_image)
  204. {
  205. qxl_draw_opaque_fb(qxl_fb_image, 0);
  206. }
  207. static void qxl_fb_imageblit(struct fb_info *info,
  208. const struct fb_image *image)
  209. {
  210. struct qxl_fbdev *qfbdev = info->par;
  211. struct qxl_device *qdev = qfbdev->qdev;
  212. struct qxl_fb_image qxl_fb_image;
  213. if (!drm_can_sleep()) {
  214. /* we cannot do any ttm_bo allocation since that will fail on
  215. * ioremap_wc..__get_vm_area_node, so queue the work item
  216. * instead This can happen from printk inside an interrupt
  217. * context, i.e.: smp_apic_timer_interrupt..check_cpu_stall */
  218. qxl_io_log(qdev,
  219. "%s: TODO use RCU, mysterious locks with spin_lock\n",
  220. __func__);
  221. return;
  222. }
  223. /* ensure proper order of rendering operations - TODO: must do this
  224. * for everything. */
  225. qxl_fb_image_init(&qxl_fb_image, qfbdev->qdev, info, image);
  226. qxl_fb_imageblit_safe(&qxl_fb_image);
  227. }
  228. int qxl_fb_init(struct qxl_device *qdev)
  229. {
  230. return 0;
  231. }
  232. static struct fb_ops qxlfb_ops = {
  233. .owner = THIS_MODULE,
  234. .fb_check_var = drm_fb_helper_check_var,
  235. .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
  236. .fb_fillrect = qxl_fb_fillrect,
  237. .fb_copyarea = qxl_fb_copyarea,
  238. .fb_imageblit = qxl_fb_imageblit,
  239. .fb_pan_display = drm_fb_helper_pan_display,
  240. .fb_blank = drm_fb_helper_blank,
  241. .fb_setcmap = drm_fb_helper_setcmap,
  242. .fb_debug_enter = drm_fb_helper_debug_enter,
  243. .fb_debug_leave = drm_fb_helper_debug_leave,
  244. };
  245. static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
  246. {
  247. struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
  248. int ret;
  249. ret = qxl_bo_reserve(qbo, false);
  250. if (likely(ret == 0)) {
  251. qxl_bo_kunmap(qbo);
  252. qxl_bo_unpin(qbo);
  253. qxl_bo_unreserve(qbo);
  254. }
  255. drm_gem_object_unreference_unlocked(gobj);
  256. }
  257. int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
  258. struct drm_file *file_priv,
  259. uint32_t *handle)
  260. {
  261. int r;
  262. struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
  263. BUG_ON(!gobj);
  264. /* drm_get_handle_create adds a reference - good */
  265. r = drm_gem_handle_create(file_priv, gobj, handle);
  266. if (r)
  267. return r;
  268. return 0;
  269. }
  270. static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
  271. struct drm_mode_fb_cmd2 *mode_cmd,
  272. struct drm_gem_object **gobj_p)
  273. {
  274. struct qxl_device *qdev = qfbdev->qdev;
  275. struct drm_gem_object *gobj = NULL;
  276. struct qxl_bo *qbo = NULL;
  277. int ret;
  278. int aligned_size, size;
  279. int height = mode_cmd->height;
  280. int bpp;
  281. int depth;
  282. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &bpp, &depth);
  283. size = mode_cmd->pitches[0] * height;
  284. aligned_size = ALIGN(size, PAGE_SIZE);
  285. /* TODO: unallocate and reallocate surface0 for real. Hack to just
  286. * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
  287. ret = qxl_gem_object_create(qdev, aligned_size, 0,
  288. QXL_GEM_DOMAIN_SURFACE,
  289. false, /* is discardable */
  290. false, /* is kernel (false means device) */
  291. NULL,
  292. &gobj);
  293. if (ret) {
  294. pr_err("failed to allocate framebuffer (%d)\n",
  295. aligned_size);
  296. return -ENOMEM;
  297. }
  298. qbo = gem_to_qxl_bo(gobj);
  299. qbo->surf.width = mode_cmd->width;
  300. qbo->surf.height = mode_cmd->height;
  301. qbo->surf.stride = mode_cmd->pitches[0];
  302. qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
  303. ret = qxl_bo_reserve(qbo, false);
  304. if (unlikely(ret != 0))
  305. goto out_unref;
  306. ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
  307. if (ret) {
  308. qxl_bo_unreserve(qbo);
  309. goto out_unref;
  310. }
  311. ret = qxl_bo_kmap(qbo, NULL);
  312. qxl_bo_unreserve(qbo); /* unreserve, will be mmaped */
  313. if (ret)
  314. goto out_unref;
  315. *gobj_p = gobj;
  316. return 0;
  317. out_unref:
  318. qxlfb_destroy_pinned_object(gobj);
  319. *gobj_p = NULL;
  320. return ret;
  321. }
  322. static int qxlfb_create(struct qxl_fbdev *qfbdev,
  323. struct drm_fb_helper_surface_size *sizes)
  324. {
  325. struct qxl_device *qdev = qfbdev->qdev;
  326. struct fb_info *info;
  327. struct drm_framebuffer *fb = NULL;
  328. struct drm_mode_fb_cmd2 mode_cmd;
  329. struct drm_gem_object *gobj = NULL;
  330. struct qxl_bo *qbo = NULL;
  331. struct device *device = &qdev->pdev->dev;
  332. int ret;
  333. int size;
  334. int bpp = sizes->surface_bpp;
  335. int depth = sizes->surface_depth;
  336. void *shadow;
  337. mode_cmd.width = sizes->surface_width;
  338. mode_cmd.height = sizes->surface_height;
  339. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
  340. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  341. ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
  342. qbo = gem_to_qxl_bo(gobj);
  343. QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
  344. mode_cmd.height, mode_cmd.pitches[0]);
  345. shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
  346. /* TODO: what's the usual response to memory allocation errors? */
  347. BUG_ON(!shadow);
  348. QXL_INFO(qdev,
  349. "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
  350. qxl_bo_gpu_offset(qbo),
  351. qxl_bo_mmap_offset(qbo),
  352. qbo->kptr,
  353. shadow);
  354. size = mode_cmd.pitches[0] * mode_cmd.height;
  355. info = framebuffer_alloc(0, device);
  356. if (info == NULL) {
  357. ret = -ENOMEM;
  358. goto out_unref;
  359. }
  360. info->par = qfbdev;
  361. qxl_framebuffer_init(qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj);
  362. fb = &qfbdev->qfb.base;
  363. /* setup helper with fb data */
  364. qfbdev->helper.fb = fb;
  365. qfbdev->helper.fbdev = info;
  366. qfbdev->shadow = shadow;
  367. strcpy(info->fix.id, "qxldrmfb");
  368. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  369. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
  370. info->fbops = &qxlfb_ops;
  371. /*
  372. * TODO: using gobj->size in various places in this function. Not sure
  373. * what the difference between the different sizes is.
  374. */
  375. info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
  376. info->fix.smem_len = gobj->size;
  377. info->screen_base = qfbdev->shadow;
  378. info->screen_size = gobj->size;
  379. drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
  380. sizes->fb_height);
  381. /* setup aperture base/size for vesafb takeover */
  382. info->apertures = alloc_apertures(1);
  383. if (!info->apertures) {
  384. ret = -ENOMEM;
  385. goto out_unref;
  386. }
  387. info->apertures->ranges[0].base = qdev->ddev->mode_config.fb_base;
  388. info->apertures->ranges[0].size = qdev->vram_size;
  389. info->fix.mmio_start = 0;
  390. info->fix.mmio_len = 0;
  391. if (info->screen_base == NULL) {
  392. ret = -ENOSPC;
  393. goto out_unref;
  394. }
  395. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  396. if (ret) {
  397. ret = -ENOMEM;
  398. goto out_unref;
  399. }
  400. info->fbdefio = &qxl_defio;
  401. fb_deferred_io_init(info);
  402. qdev->fbdev_info = info;
  403. qdev->fbdev_qfb = &qfbdev->qfb;
  404. DRM_INFO("fb mappable at 0x%lX, size %lu\n", info->fix.smem_start, (unsigned long)info->screen_size);
  405. DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb->depth, fb->pitches[0], fb->width, fb->height);
  406. return 0;
  407. out_unref:
  408. if (qbo) {
  409. ret = qxl_bo_reserve(qbo, false);
  410. if (likely(ret == 0)) {
  411. qxl_bo_kunmap(qbo);
  412. qxl_bo_unpin(qbo);
  413. qxl_bo_unreserve(qbo);
  414. }
  415. }
  416. if (fb && ret) {
  417. drm_gem_object_unreference(gobj);
  418. drm_framebuffer_cleanup(fb);
  419. kfree(fb);
  420. }
  421. drm_gem_object_unreference(gobj);
  422. return ret;
  423. }
  424. static int qxl_fb_find_or_create_single(
  425. struct drm_fb_helper *helper,
  426. struct drm_fb_helper_surface_size *sizes)
  427. {
  428. struct qxl_fbdev *qfbdev = (struct qxl_fbdev *)helper;
  429. int new_fb = 0;
  430. int ret;
  431. if (!helper->fb) {
  432. ret = qxlfb_create(qfbdev, sizes);
  433. if (ret)
  434. return ret;
  435. new_fb = 1;
  436. }
  437. return new_fb;
  438. }
  439. static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
  440. {
  441. struct fb_info *info;
  442. struct qxl_framebuffer *qfb = &qfbdev->qfb;
  443. if (qfbdev->helper.fbdev) {
  444. info = qfbdev->helper.fbdev;
  445. unregister_framebuffer(info);
  446. framebuffer_release(info);
  447. }
  448. if (qfb->obj) {
  449. qxlfb_destroy_pinned_object(qfb->obj);
  450. qfb->obj = NULL;
  451. }
  452. drm_fb_helper_fini(&qfbdev->helper);
  453. vfree(qfbdev->shadow);
  454. drm_framebuffer_cleanup(&qfb->base);
  455. return 0;
  456. }
  457. static struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
  458. .fb_probe = qxl_fb_find_or_create_single,
  459. };
  460. int qxl_fbdev_init(struct qxl_device *qdev)
  461. {
  462. struct qxl_fbdev *qfbdev;
  463. int bpp_sel = 32; /* TODO: parameter from somewhere? */
  464. int ret;
  465. qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
  466. if (!qfbdev)
  467. return -ENOMEM;
  468. qfbdev->qdev = qdev;
  469. qdev->mode_info.qfbdev = qfbdev;
  470. qfbdev->helper.funcs = &qxl_fb_helper_funcs;
  471. ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
  472. qxl_num_crtc /* num_crtc - QXL supports just 1 */,
  473. QXLFB_CONN_LIMIT);
  474. if (ret) {
  475. kfree(qfbdev);
  476. return ret;
  477. }
  478. drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
  479. drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
  480. return 0;
  481. }
  482. void qxl_fbdev_fini(struct qxl_device *qdev)
  483. {
  484. if (!qdev->mode_info.qfbdev)
  485. return;
  486. qxl_fbdev_destroy(qdev->ddev, qdev->mode_info.qfbdev);
  487. kfree(qdev->mode_info.qfbdev);
  488. qdev->mode_info.qfbdev = NULL;
  489. }
  490. void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
  491. {
  492. fb_set_suspend(qdev->mode_info.qfbdev->helper.fbdev, state);
  493. }
  494. bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
  495. {
  496. if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))
  497. return true;
  498. return false;
  499. }