qxl_fb.c 12 KB

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