qxl_fb.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "drmP.h"
  28. #include "drm/drm.h"
  29. #include "drm/drm_crtc.h"
  30. #include "drm/drm_crtc_helper.h"
  31. #include "qxl_drv.h"
  32. #include "qxl_object.h"
  33. #include "drm_fb_helper.h"
  34. #define QXL_DIRTY_DELAY (HZ / 30)
  35. struct qxl_fbdev {
  36. struct drm_fb_helper helper;
  37. struct qxl_framebuffer qfb;
  38. struct qxl_device *qdev;
  39. spinlock_t delayed_ops_lock;
  40. struct list_head delayed_ops;
  41. void *shadow;
  42. int size;
  43. };
  44. static void qxl_fb_image_init(struct qxl_fb_image *qxl_fb_image,
  45. struct qxl_device *qdev, struct fb_info *info,
  46. const struct fb_image *image)
  47. {
  48. qxl_fb_image->qdev = qdev;
  49. if (info) {
  50. qxl_fb_image->visual = info->fix.visual;
  51. if (qxl_fb_image->visual == FB_VISUAL_TRUECOLOR ||
  52. qxl_fb_image->visual == FB_VISUAL_DIRECTCOLOR)
  53. memcpy(&qxl_fb_image->pseudo_palette,
  54. info->pseudo_palette,
  55. sizeof(qxl_fb_image->pseudo_palette));
  56. } else {
  57. /* fallback */
  58. if (image->depth == 1)
  59. qxl_fb_image->visual = FB_VISUAL_MONO10;
  60. else
  61. qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
  62. }
  63. if (image) {
  64. memcpy(&qxl_fb_image->fb_image, image,
  65. sizeof(qxl_fb_image->fb_image));
  66. }
  67. }
  68. #ifdef CONFIG_DRM_FBDEV_EMULATION
  69. static struct fb_deferred_io qxl_defio = {
  70. .delay = QXL_DIRTY_DELAY,
  71. .deferred_io = drm_fb_helper_deferred_io,
  72. };
  73. #endif
  74. static struct fb_ops qxlfb_ops = {
  75. .owner = THIS_MODULE,
  76. .fb_check_var = drm_fb_helper_check_var,
  77. .fb_set_par = drm_fb_helper_set_par, /* TODO: copy vmwgfx */
  78. .fb_fillrect = drm_fb_helper_sys_fillrect,
  79. .fb_copyarea = drm_fb_helper_sys_copyarea,
  80. .fb_imageblit = drm_fb_helper_sys_imageblit,
  81. .fb_pan_display = drm_fb_helper_pan_display,
  82. .fb_blank = drm_fb_helper_blank,
  83. .fb_setcmap = drm_fb_helper_setcmap,
  84. .fb_debug_enter = drm_fb_helper_debug_enter,
  85. .fb_debug_leave = drm_fb_helper_debug_leave,
  86. };
  87. static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
  88. {
  89. struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
  90. int ret;
  91. ret = qxl_bo_reserve(qbo, false);
  92. if (likely(ret == 0)) {
  93. qxl_bo_kunmap(qbo);
  94. qxl_bo_unpin(qbo);
  95. qxl_bo_unreserve(qbo);
  96. }
  97. drm_gem_object_unreference_unlocked(gobj);
  98. }
  99. int qxl_get_handle_for_primary_fb(struct qxl_device *qdev,
  100. struct drm_file *file_priv,
  101. uint32_t *handle)
  102. {
  103. int r;
  104. struct drm_gem_object *gobj = qdev->fbdev_qfb->obj;
  105. BUG_ON(!gobj);
  106. /* drm_get_handle_create adds a reference - good */
  107. r = drm_gem_handle_create(file_priv, gobj, handle);
  108. if (r)
  109. return r;
  110. return 0;
  111. }
  112. static int qxlfb_create_pinned_object(struct qxl_fbdev *qfbdev,
  113. const struct drm_mode_fb_cmd2 *mode_cmd,
  114. struct drm_gem_object **gobj_p)
  115. {
  116. struct qxl_device *qdev = qfbdev->qdev;
  117. struct drm_gem_object *gobj = NULL;
  118. struct qxl_bo *qbo = NULL;
  119. int ret;
  120. int aligned_size, size;
  121. int height = mode_cmd->height;
  122. size = mode_cmd->pitches[0] * height;
  123. aligned_size = ALIGN(size, PAGE_SIZE);
  124. /* TODO: unallocate and reallocate surface0 for real. Hack to just
  125. * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
  126. ret = qxl_gem_object_create(qdev, aligned_size, 0,
  127. QXL_GEM_DOMAIN_SURFACE,
  128. false, /* is discardable */
  129. false, /* is kernel (false means device) */
  130. NULL,
  131. &gobj);
  132. if (ret) {
  133. pr_err("failed to allocate framebuffer (%d)\n",
  134. aligned_size);
  135. return -ENOMEM;
  136. }
  137. qbo = gem_to_qxl_bo(gobj);
  138. qbo->surf.width = mode_cmd->width;
  139. qbo->surf.height = mode_cmd->height;
  140. qbo->surf.stride = mode_cmd->pitches[0];
  141. qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
  142. ret = qxl_bo_reserve(qbo, false);
  143. if (unlikely(ret != 0))
  144. goto out_unref;
  145. ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
  146. if (ret) {
  147. qxl_bo_unreserve(qbo);
  148. goto out_unref;
  149. }
  150. ret = qxl_bo_kmap(qbo, NULL);
  151. qxl_bo_unreserve(qbo); /* unreserve, will be mmaped */
  152. if (ret)
  153. goto out_unref;
  154. *gobj_p = gobj;
  155. return 0;
  156. out_unref:
  157. qxlfb_destroy_pinned_object(gobj);
  158. *gobj_p = NULL;
  159. return ret;
  160. }
  161. /*
  162. * FIXME
  163. * It should not be necessary to have a special dirty() callback for fbdev.
  164. */
  165. static int qxlfb_framebuffer_dirty(struct drm_framebuffer *fb,
  166. struct drm_file *file_priv,
  167. unsigned flags, unsigned color,
  168. struct drm_clip_rect *clips,
  169. unsigned num_clips)
  170. {
  171. struct qxl_device *qdev = fb->dev->dev_private;
  172. struct fb_info *info = qdev->fbdev_info;
  173. struct qxl_fbdev *qfbdev = info->par;
  174. struct qxl_fb_image qxl_fb_image;
  175. struct fb_image *image = &qxl_fb_image.fb_image;
  176. /* TODO: hard coding 32 bpp */
  177. int stride = qfbdev->qfb.base.pitches[0];
  178. /*
  179. * we are using a shadow draw buffer, at qdev->surface0_shadow
  180. */
  181. qxl_io_log(qdev, "dirty x[%d, %d], y[%d, %d]", clips->x1, clips->x2,
  182. clips->y1, clips->y2);
  183. image->dx = clips->x1;
  184. image->dy = clips->y1;
  185. image->width = clips->x2 - clips->x1;
  186. image->height = clips->y2 - clips->y1;
  187. image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
  188. warnings */
  189. image->bg_color = 0;
  190. image->depth = 32; /* TODO: take from somewhere? */
  191. image->cmap.start = 0;
  192. image->cmap.len = 0;
  193. image->cmap.red = NULL;
  194. image->cmap.green = NULL;
  195. image->cmap.blue = NULL;
  196. image->cmap.transp = NULL;
  197. image->data = qfbdev->shadow + (clips->x1 * 4) + (stride * clips->y1);
  198. qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
  199. qxl_draw_opaque_fb(&qxl_fb_image, stride);
  200. return 0;
  201. }
  202. static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
  203. .destroy = qxl_user_framebuffer_destroy,
  204. .dirty = qxlfb_framebuffer_dirty,
  205. };
  206. static int qxlfb_create(struct qxl_fbdev *qfbdev,
  207. struct drm_fb_helper_surface_size *sizes)
  208. {
  209. struct qxl_device *qdev = qfbdev->qdev;
  210. struct fb_info *info;
  211. struct drm_framebuffer *fb = NULL;
  212. struct drm_mode_fb_cmd2 mode_cmd;
  213. struct drm_gem_object *gobj = NULL;
  214. struct qxl_bo *qbo = NULL;
  215. int ret;
  216. int size;
  217. int bpp = sizes->surface_bpp;
  218. int depth = sizes->surface_depth;
  219. void *shadow;
  220. mode_cmd.width = sizes->surface_width;
  221. mode_cmd.height = sizes->surface_height;
  222. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
  223. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  224. ret = qxlfb_create_pinned_object(qfbdev, &mode_cmd, &gobj);
  225. if (ret < 0)
  226. return ret;
  227. qbo = gem_to_qxl_bo(gobj);
  228. QXL_INFO(qdev, "%s: %dx%d %d\n", __func__, mode_cmd.width,
  229. mode_cmd.height, mode_cmd.pitches[0]);
  230. shadow = vmalloc(mode_cmd.pitches[0] * mode_cmd.height);
  231. /* TODO: what's the usual response to memory allocation errors? */
  232. BUG_ON(!shadow);
  233. QXL_INFO(qdev,
  234. "surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
  235. qxl_bo_gpu_offset(qbo),
  236. qxl_bo_mmap_offset(qbo),
  237. qbo->kptr,
  238. shadow);
  239. size = mode_cmd.pitches[0] * mode_cmd.height;
  240. info = drm_fb_helper_alloc_fbi(&qfbdev->helper);
  241. if (IS_ERR(info)) {
  242. ret = PTR_ERR(info);
  243. goto out_unref;
  244. }
  245. info->par = qfbdev;
  246. qxl_framebuffer_init(qdev->ddev, &qfbdev->qfb, &mode_cmd, gobj,
  247. &qxlfb_fb_funcs);
  248. fb = &qfbdev->qfb.base;
  249. /* setup helper with fb data */
  250. qfbdev->helper.fb = fb;
  251. qfbdev->shadow = shadow;
  252. strcpy(info->fix.id, "qxldrmfb");
  253. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  254. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA | FBINFO_HWACCEL_FILLRECT;
  255. info->fbops = &qxlfb_ops;
  256. /*
  257. * TODO: using gobj->size in various places in this function. Not sure
  258. * what the difference between the different sizes is.
  259. */
  260. info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
  261. info->fix.smem_len = gobj->size;
  262. info->screen_base = qfbdev->shadow;
  263. info->screen_size = gobj->size;
  264. drm_fb_helper_fill_var(info, &qfbdev->helper, sizes->fb_width,
  265. sizes->fb_height);
  266. /* setup aperture base/size for vesafb takeover */
  267. info->apertures->ranges[0].base = qdev->ddev->mode_config.fb_base;
  268. info->apertures->ranges[0].size = qdev->vram_size;
  269. info->fix.mmio_start = 0;
  270. info->fix.mmio_len = 0;
  271. if (info->screen_base == NULL) {
  272. ret = -ENOSPC;
  273. goto out_destroy_fbi;
  274. }
  275. #ifdef CONFIG_DRM_FBDEV_EMULATION
  276. info->fbdefio = &qxl_defio;
  277. fb_deferred_io_init(info);
  278. #endif
  279. qdev->fbdev_info = info;
  280. qdev->fbdev_qfb = &qfbdev->qfb;
  281. DRM_INFO("fb mappable at 0x%lX, size %lu\n", info->fix.smem_start, (unsigned long)info->screen_size);
  282. DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n", fb->depth, fb->pitches[0], fb->width, fb->height);
  283. return 0;
  284. out_destroy_fbi:
  285. drm_fb_helper_release_fbi(&qfbdev->helper);
  286. out_unref:
  287. if (qbo) {
  288. ret = qxl_bo_reserve(qbo, false);
  289. if (likely(ret == 0)) {
  290. qxl_bo_kunmap(qbo);
  291. qxl_bo_unpin(qbo);
  292. qxl_bo_unreserve(qbo);
  293. }
  294. }
  295. if (fb && ret) {
  296. drm_gem_object_unreference_unlocked(gobj);
  297. drm_framebuffer_cleanup(fb);
  298. kfree(fb);
  299. }
  300. drm_gem_object_unreference_unlocked(gobj);
  301. return ret;
  302. }
  303. static int qxl_fb_find_or_create_single(
  304. struct drm_fb_helper *helper,
  305. struct drm_fb_helper_surface_size *sizes)
  306. {
  307. struct qxl_fbdev *qfbdev =
  308. container_of(helper, struct qxl_fbdev, helper);
  309. int new_fb = 0;
  310. int ret;
  311. if (!helper->fb) {
  312. ret = qxlfb_create(qfbdev, sizes);
  313. if (ret)
  314. return ret;
  315. new_fb = 1;
  316. }
  317. return new_fb;
  318. }
  319. static int qxl_fbdev_destroy(struct drm_device *dev, struct qxl_fbdev *qfbdev)
  320. {
  321. struct qxl_framebuffer *qfb = &qfbdev->qfb;
  322. drm_fb_helper_unregister_fbi(&qfbdev->helper);
  323. drm_fb_helper_release_fbi(&qfbdev->helper);
  324. if (qfb->obj) {
  325. qxlfb_destroy_pinned_object(qfb->obj);
  326. qfb->obj = NULL;
  327. }
  328. drm_fb_helper_fini(&qfbdev->helper);
  329. vfree(qfbdev->shadow);
  330. drm_framebuffer_cleanup(&qfb->base);
  331. return 0;
  332. }
  333. static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
  334. .fb_probe = qxl_fb_find_or_create_single,
  335. };
  336. int qxl_fbdev_init(struct qxl_device *qdev)
  337. {
  338. int ret = 0;
  339. #ifdef CONFIG_DRM_FBDEV_EMULATION
  340. struct qxl_fbdev *qfbdev;
  341. int bpp_sel = 32; /* TODO: parameter from somewhere? */
  342. qfbdev = kzalloc(sizeof(struct qxl_fbdev), GFP_KERNEL);
  343. if (!qfbdev)
  344. return -ENOMEM;
  345. qfbdev->qdev = qdev;
  346. qdev->mode_info.qfbdev = qfbdev;
  347. spin_lock_init(&qfbdev->delayed_ops_lock);
  348. INIT_LIST_HEAD(&qfbdev->delayed_ops);
  349. drm_fb_helper_prepare(qdev->ddev, &qfbdev->helper,
  350. &qxl_fb_helper_funcs);
  351. ret = drm_fb_helper_init(qdev->ddev, &qfbdev->helper,
  352. qxl_num_crtc /* num_crtc - QXL supports just 1 */,
  353. QXLFB_CONN_LIMIT);
  354. if (ret)
  355. goto free;
  356. ret = drm_fb_helper_single_add_all_connectors(&qfbdev->helper);
  357. if (ret)
  358. goto fini;
  359. ret = drm_fb_helper_initial_config(&qfbdev->helper, bpp_sel);
  360. if (ret)
  361. goto fini;
  362. return 0;
  363. fini:
  364. drm_fb_helper_fini(&qfbdev->helper);
  365. free:
  366. kfree(qfbdev);
  367. #endif
  368. return ret;
  369. }
  370. void qxl_fbdev_fini(struct qxl_device *qdev)
  371. {
  372. if (!qdev->mode_info.qfbdev)
  373. return;
  374. qxl_fbdev_destroy(qdev->ddev, qdev->mode_info.qfbdev);
  375. kfree(qdev->mode_info.qfbdev);
  376. qdev->mode_info.qfbdev = NULL;
  377. }
  378. void qxl_fbdev_set_suspend(struct qxl_device *qdev, int state)
  379. {
  380. if (!qdev->mode_info.qfbdev)
  381. return;
  382. drm_fb_helper_set_suspend(&qdev->mode_info.qfbdev->helper, state);
  383. }
  384. bool qxl_fbdev_qobj_is_fb(struct qxl_device *qdev, struct qxl_bo *qobj)
  385. {
  386. if (qobj == gem_to_qxl_bo(qdev->mode_info.qfbdev->qfb.obj))
  387. return true;
  388. return false;
  389. }