qxl_fb.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <drm/drmP.h>
  28. #include <drm/drm.h>
  29. #include <drm/drm_crtc.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include <drm/drm_fb_helper.h>
  32. #include <drm/drm_gem_framebuffer_helper.h>
  33. #include "qxl_drv.h"
  34. #include "qxl_object.h"
  35. static void qxl_fb_image_init(struct qxl_fb_image *qxl_fb_image,
  36. struct qxl_device *qdev, struct fb_info *info,
  37. const struct fb_image *image)
  38. {
  39. qxl_fb_image->qdev = qdev;
  40. if (info) {
  41. qxl_fb_image->visual = info->fix.visual;
  42. if (qxl_fb_image->visual == FB_VISUAL_TRUECOLOR ||
  43. qxl_fb_image->visual == FB_VISUAL_DIRECTCOLOR)
  44. memcpy(&qxl_fb_image->pseudo_palette,
  45. info->pseudo_palette,
  46. sizeof(qxl_fb_image->pseudo_palette));
  47. } else {
  48. /* fallback */
  49. if (image->depth == 1)
  50. qxl_fb_image->visual = FB_VISUAL_MONO10;
  51. else
  52. qxl_fb_image->visual = FB_VISUAL_DIRECTCOLOR;
  53. }
  54. if (image) {
  55. memcpy(&qxl_fb_image->fb_image, image,
  56. sizeof(qxl_fb_image->fb_image));
  57. }
  58. }
  59. static struct fb_ops qxlfb_ops = {
  60. .owner = THIS_MODULE,
  61. DRM_FB_HELPER_DEFAULT_OPS,
  62. .fb_fillrect = drm_fb_helper_sys_fillrect,
  63. .fb_copyarea = drm_fb_helper_sys_copyarea,
  64. .fb_imageblit = drm_fb_helper_sys_imageblit,
  65. };
  66. static void qxlfb_destroy_pinned_object(struct drm_gem_object *gobj)
  67. {
  68. struct qxl_bo *qbo = gem_to_qxl_bo(gobj);
  69. qxl_bo_kunmap(qbo);
  70. qxl_bo_unpin(qbo);
  71. drm_gem_object_put_unlocked(gobj);
  72. }
  73. static int qxlfb_create_pinned_object(struct qxl_device *qdev,
  74. const struct drm_mode_fb_cmd2 *mode_cmd,
  75. struct drm_gem_object **gobj_p)
  76. {
  77. struct drm_gem_object *gobj = NULL;
  78. struct qxl_bo *qbo = NULL;
  79. int ret;
  80. int aligned_size, size;
  81. int height = mode_cmd->height;
  82. size = mode_cmd->pitches[0] * height;
  83. aligned_size = ALIGN(size, PAGE_SIZE);
  84. /* TODO: unallocate and reallocate surface0 for real. Hack to just
  85. * have a large enough surface0 for 1024x768 Xorg 32bpp mode */
  86. ret = qxl_gem_object_create(qdev, aligned_size, 0,
  87. QXL_GEM_DOMAIN_SURFACE,
  88. false, /* is discardable */
  89. false, /* is kernel (false means device) */
  90. NULL,
  91. &gobj);
  92. if (ret) {
  93. pr_err("failed to allocate framebuffer (%d)\n",
  94. aligned_size);
  95. return -ENOMEM;
  96. }
  97. qbo = gem_to_qxl_bo(gobj);
  98. qbo->surf.width = mode_cmd->width;
  99. qbo->surf.height = mode_cmd->height;
  100. qbo->surf.stride = mode_cmd->pitches[0];
  101. qbo->surf.format = SPICE_SURFACE_FMT_32_xRGB;
  102. ret = qxl_bo_pin(qbo, QXL_GEM_DOMAIN_SURFACE, NULL);
  103. if (ret) {
  104. goto out_unref;
  105. }
  106. ret = qxl_bo_kmap(qbo, NULL);
  107. if (ret)
  108. goto out_unref;
  109. *gobj_p = gobj;
  110. return 0;
  111. out_unref:
  112. qxlfb_destroy_pinned_object(gobj);
  113. *gobj_p = NULL;
  114. return ret;
  115. }
  116. /*
  117. * FIXME
  118. * It should not be necessary to have a special dirty() callback for fbdev.
  119. */
  120. static int qxlfb_framebuffer_dirty(struct drm_framebuffer *fb,
  121. struct drm_file *file_priv,
  122. unsigned flags, unsigned color,
  123. struct drm_clip_rect *clips,
  124. unsigned num_clips)
  125. {
  126. struct qxl_device *qdev = fb->dev->dev_private;
  127. struct fb_info *info = qdev->fb_helper.fbdev;
  128. struct qxl_fb_image qxl_fb_image;
  129. struct fb_image *image = &qxl_fb_image.fb_image;
  130. /* TODO: hard coding 32 bpp */
  131. int stride = fb->pitches[0];
  132. /*
  133. * we are using a shadow draw buffer, at qdev->surface0_shadow
  134. */
  135. image->dx = clips->x1;
  136. image->dy = clips->y1;
  137. image->width = clips->x2 - clips->x1;
  138. image->height = clips->y2 - clips->y1;
  139. image->fg_color = 0xffffffff; /* unused, just to avoid uninitialized
  140. warnings */
  141. image->bg_color = 0;
  142. image->depth = 32; /* TODO: take from somewhere? */
  143. image->cmap.start = 0;
  144. image->cmap.len = 0;
  145. image->cmap.red = NULL;
  146. image->cmap.green = NULL;
  147. image->cmap.blue = NULL;
  148. image->cmap.transp = NULL;
  149. image->data = info->screen_base + (clips->x1 * 4) + (stride * clips->y1);
  150. qxl_fb_image_init(&qxl_fb_image, qdev, info, NULL);
  151. qxl_draw_opaque_fb(&qxl_fb_image, stride);
  152. return 0;
  153. }
  154. static const struct drm_framebuffer_funcs qxlfb_fb_funcs = {
  155. .destroy = drm_gem_fb_destroy,
  156. .create_handle = drm_gem_fb_create_handle,
  157. .dirty = qxlfb_framebuffer_dirty,
  158. };
  159. static int qxlfb_create(struct drm_fb_helper *helper,
  160. struct drm_fb_helper_surface_size *sizes)
  161. {
  162. struct qxl_device *qdev =
  163. container_of(helper, struct qxl_device, fb_helper);
  164. struct fb_info *info;
  165. struct drm_framebuffer *fb = NULL;
  166. struct drm_mode_fb_cmd2 mode_cmd;
  167. struct drm_gem_object *gobj = NULL;
  168. struct qxl_bo *qbo = NULL;
  169. int ret;
  170. int bpp = sizes->surface_bpp;
  171. int depth = sizes->surface_depth;
  172. void *shadow;
  173. mode_cmd.width = sizes->surface_width;
  174. mode_cmd.height = sizes->surface_height;
  175. mode_cmd.pitches[0] = ALIGN(mode_cmd.width * ((bpp + 1) / 8), 64);
  176. mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
  177. ret = qxlfb_create_pinned_object(qdev, &mode_cmd, &gobj);
  178. if (ret < 0)
  179. return ret;
  180. qbo = gem_to_qxl_bo(gobj);
  181. DRM_DEBUG_DRIVER("%dx%d %d\n", mode_cmd.width,
  182. mode_cmd.height, mode_cmd.pitches[0]);
  183. shadow = vmalloc(array_size(mode_cmd.pitches[0], mode_cmd.height));
  184. /* TODO: what's the usual response to memory allocation errors? */
  185. BUG_ON(!shadow);
  186. DRM_DEBUG_DRIVER("surface0 at gpu offset %lld, mmap_offset %lld (virt %p, shadow %p)\n",
  187. qxl_bo_gpu_offset(qbo), qxl_bo_mmap_offset(qbo),
  188. qbo->kptr, shadow);
  189. info = drm_fb_helper_alloc_fbi(helper);
  190. if (IS_ERR(info)) {
  191. ret = PTR_ERR(info);
  192. goto out_unref;
  193. }
  194. info->par = helper;
  195. fb = drm_gem_fbdev_fb_create(&qdev->ddev, sizes, 64, gobj,
  196. &qxlfb_fb_funcs);
  197. if (IS_ERR(fb)) {
  198. DRM_ERROR("Failed to create framebuffer: %ld\n", PTR_ERR(fb));
  199. ret = PTR_ERR(fb);
  200. goto out_unref;
  201. }
  202. /* setup helper with fb data */
  203. qdev->fb_helper.fb = fb;
  204. strcpy(info->fix.id, "qxldrmfb");
  205. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
  206. info->fbops = &qxlfb_ops;
  207. /*
  208. * TODO: using gobj->size in various places in this function. Not sure
  209. * what the difference between the different sizes is.
  210. */
  211. info->fix.smem_start = qdev->vram_base; /* TODO - correct? */
  212. info->fix.smem_len = gobj->size;
  213. info->screen_base = shadow;
  214. info->screen_size = gobj->size;
  215. drm_fb_helper_fill_var(info, &qdev->fb_helper, sizes->fb_width,
  216. sizes->fb_height);
  217. /* setup aperture base/size for vesafb takeover */
  218. info->apertures->ranges[0].base = qdev->ddev.mode_config.fb_base;
  219. info->apertures->ranges[0].size = qdev->vram_size;
  220. info->fix.mmio_start = 0;
  221. info->fix.mmio_len = 0;
  222. if (info->screen_base == NULL) {
  223. ret = -ENOSPC;
  224. goto out_unref;
  225. }
  226. /* XXX error handling. */
  227. drm_fb_helper_defio_init(helper);
  228. DRM_INFO("fb mappable at 0x%lX, size %lu\n", info->fix.smem_start, (unsigned long)info->screen_size);
  229. DRM_INFO("fb: depth %d, pitch %d, width %d, height %d\n",
  230. fb->format->depth, fb->pitches[0], fb->width, fb->height);
  231. return 0;
  232. out_unref:
  233. if (qbo) {
  234. qxl_bo_kunmap(qbo);
  235. qxl_bo_unpin(qbo);
  236. }
  237. drm_gem_object_put_unlocked(gobj);
  238. return ret;
  239. }
  240. static const struct drm_fb_helper_funcs qxl_fb_helper_funcs = {
  241. .fb_probe = qxlfb_create,
  242. };
  243. int qxl_fbdev_init(struct qxl_device *qdev)
  244. {
  245. return drm_fb_helper_fbdev_setup(&qdev->ddev, &qdev->fb_helper,
  246. &qxl_fb_helper_funcs, 32,
  247. QXLFB_CONN_LIMIT);
  248. }
  249. void qxl_fbdev_fini(struct qxl_device *qdev)
  250. {
  251. struct fb_info *fbi = qdev->fb_helper.fbdev;
  252. void *shadow = fbi ? fbi->screen_buffer : NULL;
  253. drm_fb_helper_fbdev_teardown(&qdev->ddev);
  254. vfree(shadow);
  255. }