cirrus_fbdev.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright 2012 Red Hat
  3. *
  4. * This file is subject to the terms and conditions of the GNU General
  5. * Public License version 2. See the file COPYING in the main
  6. * directory of this archive for more details.
  7. *
  8. * Authors: Matthew Garrett
  9. * Dave Airlie
  10. */
  11. #include <linux/module.h>
  12. #include <drm/drmP.h>
  13. #include <drm/drm_fb_helper.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include "cirrus_drv.h"
  16. static void cirrus_dirty_update(struct cirrus_fbdev *afbdev,
  17. int x, int y, int width, int height)
  18. {
  19. int i;
  20. struct drm_gem_object *obj;
  21. struct cirrus_bo *bo;
  22. int src_offset, dst_offset;
  23. int bpp = (afbdev->gfb.base.bits_per_pixel + 7)/8;
  24. int ret = -EBUSY;
  25. bool unmap = false;
  26. bool store_for_later = false;
  27. int x2, y2;
  28. unsigned long flags;
  29. obj = afbdev->gfb.obj;
  30. bo = gem_to_cirrus_bo(obj);
  31. /*
  32. * try and reserve the BO, if we fail with busy
  33. * then the BO is being moved and we should
  34. * store up the damage until later.
  35. */
  36. if (drm_can_sleep())
  37. ret = cirrus_bo_reserve(bo, true);
  38. if (ret) {
  39. if (ret != -EBUSY)
  40. return;
  41. store_for_later = true;
  42. }
  43. x2 = x + width - 1;
  44. y2 = y + height - 1;
  45. spin_lock_irqsave(&afbdev->dirty_lock, flags);
  46. if (afbdev->y1 < y)
  47. y = afbdev->y1;
  48. if (afbdev->y2 > y2)
  49. y2 = afbdev->y2;
  50. if (afbdev->x1 < x)
  51. x = afbdev->x1;
  52. if (afbdev->x2 > x2)
  53. x2 = afbdev->x2;
  54. if (store_for_later) {
  55. afbdev->x1 = x;
  56. afbdev->x2 = x2;
  57. afbdev->y1 = y;
  58. afbdev->y2 = y2;
  59. spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
  60. return;
  61. }
  62. afbdev->x1 = afbdev->y1 = INT_MAX;
  63. afbdev->x2 = afbdev->y2 = 0;
  64. spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
  65. if (!bo->kmap.virtual) {
  66. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  67. if (ret) {
  68. DRM_ERROR("failed to kmap fb updates\n");
  69. cirrus_bo_unreserve(bo);
  70. return;
  71. }
  72. unmap = true;
  73. }
  74. for (i = y; i < y + height; i++) {
  75. /* assume equal stride for now */
  76. src_offset = dst_offset = i * afbdev->gfb.base.pitches[0] + (x * bpp);
  77. memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
  78. }
  79. if (unmap)
  80. ttm_bo_kunmap(&bo->kmap);
  81. cirrus_bo_unreserve(bo);
  82. }
  83. static void cirrus_fillrect(struct fb_info *info,
  84. const struct fb_fillrect *rect)
  85. {
  86. struct cirrus_fbdev *afbdev = info->par;
  87. drm_fb_helper_sys_fillrect(info, rect);
  88. cirrus_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
  89. rect->height);
  90. }
  91. static void cirrus_copyarea(struct fb_info *info,
  92. const struct fb_copyarea *area)
  93. {
  94. struct cirrus_fbdev *afbdev = info->par;
  95. drm_fb_helper_sys_copyarea(info, area);
  96. cirrus_dirty_update(afbdev, area->dx, area->dy, area->width,
  97. area->height);
  98. }
  99. static void cirrus_imageblit(struct fb_info *info,
  100. const struct fb_image *image)
  101. {
  102. struct cirrus_fbdev *afbdev = info->par;
  103. drm_fb_helper_sys_imageblit(info, image);
  104. cirrus_dirty_update(afbdev, image->dx, image->dy, image->width,
  105. image->height);
  106. }
  107. static struct fb_ops cirrusfb_ops = {
  108. .owner = THIS_MODULE,
  109. .fb_check_var = drm_fb_helper_check_var,
  110. .fb_set_par = drm_fb_helper_set_par,
  111. .fb_fillrect = cirrus_fillrect,
  112. .fb_copyarea = cirrus_copyarea,
  113. .fb_imageblit = cirrus_imageblit,
  114. .fb_pan_display = drm_fb_helper_pan_display,
  115. .fb_blank = drm_fb_helper_blank,
  116. .fb_setcmap = drm_fb_helper_setcmap,
  117. };
  118. static int cirrusfb_create_object(struct cirrus_fbdev *afbdev,
  119. const struct drm_mode_fb_cmd2 *mode_cmd,
  120. struct drm_gem_object **gobj_p)
  121. {
  122. struct drm_device *dev = afbdev->helper.dev;
  123. struct cirrus_device *cdev = dev->dev_private;
  124. u32 bpp, depth;
  125. u32 size;
  126. struct drm_gem_object *gobj;
  127. int ret = 0;
  128. drm_fb_get_bpp_depth(mode_cmd->pixel_format, &depth, &bpp);
  129. if (!cirrus_check_framebuffer(cdev, mode_cmd->width, mode_cmd->height,
  130. bpp, mode_cmd->pitches[0]))
  131. return -EINVAL;
  132. size = mode_cmd->pitches[0] * mode_cmd->height;
  133. ret = cirrus_gem_create(dev, size, true, &gobj);
  134. if (ret)
  135. return ret;
  136. *gobj_p = gobj;
  137. return ret;
  138. }
  139. static int cirrusfb_create(struct drm_fb_helper *helper,
  140. struct drm_fb_helper_surface_size *sizes)
  141. {
  142. struct cirrus_fbdev *gfbdev =
  143. container_of(helper, struct cirrus_fbdev, helper);
  144. struct cirrus_device *cdev = gfbdev->helper.dev->dev_private;
  145. struct fb_info *info;
  146. struct drm_framebuffer *fb;
  147. struct drm_mode_fb_cmd2 mode_cmd;
  148. void *sysram;
  149. struct drm_gem_object *gobj = NULL;
  150. struct cirrus_bo *bo = NULL;
  151. int size, ret;
  152. mode_cmd.width = sizes->surface_width;
  153. mode_cmd.height = sizes->surface_height;
  154. mode_cmd.pitches[0] = mode_cmd.width * ((sizes->surface_bpp + 7) / 8);
  155. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  156. sizes->surface_depth);
  157. size = mode_cmd.pitches[0] * mode_cmd.height;
  158. ret = cirrusfb_create_object(gfbdev, &mode_cmd, &gobj);
  159. if (ret) {
  160. DRM_ERROR("failed to create fbcon backing object %d\n", ret);
  161. return ret;
  162. }
  163. bo = gem_to_cirrus_bo(gobj);
  164. sysram = vmalloc(size);
  165. if (!sysram)
  166. return -ENOMEM;
  167. info = drm_fb_helper_alloc_fbi(helper);
  168. if (IS_ERR(info))
  169. return PTR_ERR(info);
  170. info->par = gfbdev;
  171. ret = cirrus_framebuffer_init(cdev->dev, &gfbdev->gfb, &mode_cmd, gobj);
  172. if (ret)
  173. return ret;
  174. gfbdev->sysram = sysram;
  175. gfbdev->size = size;
  176. fb = &gfbdev->gfb.base;
  177. if (!fb) {
  178. DRM_INFO("fb is NULL\n");
  179. return -EINVAL;
  180. }
  181. /* setup helper */
  182. gfbdev->helper.fb = fb;
  183. strcpy(info->fix.id, "cirrusdrmfb");
  184. info->flags = FBINFO_DEFAULT;
  185. info->fbops = &cirrusfb_ops;
  186. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  187. drm_fb_helper_fill_var(info, &gfbdev->helper, sizes->fb_width,
  188. sizes->fb_height);
  189. /* setup aperture base/size for vesafb takeover */
  190. info->apertures->ranges[0].base = cdev->dev->mode_config.fb_base;
  191. info->apertures->ranges[0].size = cdev->mc.vram_size;
  192. info->fix.smem_start = cdev->dev->mode_config.fb_base;
  193. info->fix.smem_len = cdev->mc.vram_size;
  194. info->screen_base = sysram;
  195. info->screen_size = size;
  196. info->fix.mmio_start = 0;
  197. info->fix.mmio_len = 0;
  198. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  199. DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info->fix.smem_start);
  200. DRM_INFO("size %lu\n", (unsigned long)info->fix.smem_len);
  201. DRM_INFO("fb depth is %d\n", fb->depth);
  202. DRM_INFO(" pitch is %d\n", fb->pitches[0]);
  203. return 0;
  204. }
  205. static int cirrus_fbdev_destroy(struct drm_device *dev,
  206. struct cirrus_fbdev *gfbdev)
  207. {
  208. struct cirrus_framebuffer *gfb = &gfbdev->gfb;
  209. drm_fb_helper_unregister_fbi(&gfbdev->helper);
  210. drm_fb_helper_release_fbi(&gfbdev->helper);
  211. if (gfb->obj) {
  212. drm_gem_object_unreference_unlocked(gfb->obj);
  213. gfb->obj = NULL;
  214. }
  215. vfree(gfbdev->sysram);
  216. drm_fb_helper_fini(&gfbdev->helper);
  217. drm_framebuffer_unregister_private(&gfb->base);
  218. drm_framebuffer_cleanup(&gfb->base);
  219. return 0;
  220. }
  221. static const struct drm_fb_helper_funcs cirrus_fb_helper_funcs = {
  222. .gamma_set = cirrus_crtc_fb_gamma_set,
  223. .gamma_get = cirrus_crtc_fb_gamma_get,
  224. .fb_probe = cirrusfb_create,
  225. };
  226. int cirrus_fbdev_init(struct cirrus_device *cdev)
  227. {
  228. struct cirrus_fbdev *gfbdev;
  229. int ret;
  230. int bpp_sel = 24;
  231. /*bpp_sel = 8;*/
  232. gfbdev = kzalloc(sizeof(struct cirrus_fbdev), GFP_KERNEL);
  233. if (!gfbdev)
  234. return -ENOMEM;
  235. cdev->mode_info.gfbdev = gfbdev;
  236. spin_lock_init(&gfbdev->dirty_lock);
  237. drm_fb_helper_prepare(cdev->dev, &gfbdev->helper,
  238. &cirrus_fb_helper_funcs);
  239. ret = drm_fb_helper_init(cdev->dev, &gfbdev->helper,
  240. cdev->num_crtc, CIRRUSFB_CONN_LIMIT);
  241. if (ret)
  242. return ret;
  243. ret = drm_fb_helper_single_add_all_connectors(&gfbdev->helper);
  244. if (ret)
  245. return ret;
  246. /* disable all the possible outputs/crtcs before entering KMS mode */
  247. drm_helper_disable_unused_functions(cdev->dev);
  248. return drm_fb_helper_initial_config(&gfbdev->helper, bpp_sel);
  249. }
  250. void cirrus_fbdev_fini(struct cirrus_device *cdev)
  251. {
  252. if (!cdev->mode_info.gfbdev)
  253. return;
  254. cirrus_fbdev_destroy(cdev->dev, cdev->mode_info.gfbdev);
  255. kfree(cdev->mode_info.gfbdev);
  256. cdev->mode_info.gfbdev = NULL;
  257. }