qxl_fb.c 12 KB

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