qxl_fb.c 12 KB

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