cirrus_fbdev.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <linux/fb.h>
  16. #include "cirrus_drv.h"
  17. static void cirrus_dirty_update(struct cirrus_fbdev *afbdev,
  18. int x, int y, int width, int height)
  19. {
  20. int i;
  21. struct drm_gem_object *obj;
  22. struct cirrus_bo *bo;
  23. int src_offset, dst_offset;
  24. int bpp = (afbdev->gfb.base.bits_per_pixel + 7)/8;
  25. int ret = -EBUSY;
  26. bool unmap = false;
  27. bool store_for_later = false;
  28. int x2, y2;
  29. unsigned long flags;
  30. obj = afbdev->gfb.obj;
  31. bo = gem_to_cirrus_bo(obj);
  32. /*
  33. * try and reserve the BO, if we fail with busy
  34. * then the BO is being moved and we should
  35. * store up the damage until later.
  36. */
  37. if (!in_interrupt())
  38. ret = cirrus_bo_reserve(bo, true);
  39. if (ret) {
  40. if (ret != -EBUSY)
  41. return;
  42. store_for_later = true;
  43. }
  44. x2 = x + width - 1;
  45. y2 = y + height - 1;
  46. spin_lock_irqsave(&afbdev->dirty_lock, flags);
  47. if (afbdev->y1 < y)
  48. y = afbdev->y1;
  49. if (afbdev->y2 > y2)
  50. y2 = afbdev->y2;
  51. if (afbdev->x1 < x)
  52. x = afbdev->x1;
  53. if (afbdev->x2 > x2)
  54. x2 = afbdev->x2;
  55. if (store_for_later) {
  56. afbdev->x1 = x;
  57. afbdev->x2 = x2;
  58. afbdev->y1 = y;
  59. afbdev->y2 = y2;
  60. spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
  61. return;
  62. }
  63. afbdev->x1 = afbdev->y1 = INT_MAX;
  64. afbdev->x2 = afbdev->y2 = 0;
  65. spin_unlock_irqrestore(&afbdev->dirty_lock, flags);
  66. if (!bo->kmap.virtual) {
  67. ret = ttm_bo_kmap(&bo->bo, 0, bo->bo.num_pages, &bo->kmap);
  68. if (ret) {
  69. DRM_ERROR("failed to kmap fb updates\n");
  70. cirrus_bo_unreserve(bo);
  71. return;
  72. }
  73. unmap = true;
  74. }
  75. for (i = y; i < y + height; i++) {
  76. /* assume equal stride for now */
  77. src_offset = dst_offset = i * afbdev->gfb.base.pitches[0] + (x * bpp);
  78. memcpy_toio(bo->kmap.virtual + src_offset, afbdev->sysram + src_offset, width * bpp);
  79. }
  80. if (unmap)
  81. ttm_bo_kunmap(&bo->kmap);
  82. cirrus_bo_unreserve(bo);
  83. }
  84. static void cirrus_fillrect(struct fb_info *info,
  85. const struct fb_fillrect *rect)
  86. {
  87. struct cirrus_fbdev *afbdev = info->par;
  88. sys_fillrect(info, rect);
  89. cirrus_dirty_update(afbdev, rect->dx, rect->dy, rect->width,
  90. rect->height);
  91. }
  92. static void cirrus_copyarea(struct fb_info *info,
  93. const struct fb_copyarea *area)
  94. {
  95. struct cirrus_fbdev *afbdev = info->par;
  96. sys_copyarea(info, area);
  97. cirrus_dirty_update(afbdev, area->dx, area->dy, area->width,
  98. area->height);
  99. }
  100. static void cirrus_imageblit(struct fb_info *info,
  101. const struct fb_image *image)
  102. {
  103. struct cirrus_fbdev *afbdev = info->par;
  104. sys_imageblit(info, image);
  105. cirrus_dirty_update(afbdev, image->dx, image->dy, image->width,
  106. image->height);
  107. }
  108. static struct fb_ops cirrusfb_ops = {
  109. .owner = THIS_MODULE,
  110. .fb_check_var = drm_fb_helper_check_var,
  111. .fb_set_par = drm_fb_helper_set_par,
  112. .fb_fillrect = cirrus_fillrect,
  113. .fb_copyarea = cirrus_copyarea,
  114. .fb_imageblit = cirrus_imageblit,
  115. .fb_pan_display = drm_fb_helper_pan_display,
  116. .fb_blank = drm_fb_helper_blank,
  117. .fb_setcmap = drm_fb_helper_setcmap,
  118. };
  119. static int cirrusfb_create_object(struct cirrus_fbdev *afbdev,
  120. struct drm_mode_fb_cmd2 *mode_cmd,
  121. struct drm_gem_object **gobj_p)
  122. {
  123. struct drm_device *dev = afbdev->helper.dev;
  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 (bpp > 24)
  130. return -EINVAL;
  131. size = mode_cmd->pitches[0] * mode_cmd->height;
  132. ret = cirrus_gem_create(dev, size, true, &gobj);
  133. if (ret)
  134. return ret;
  135. *gobj_p = gobj;
  136. return ret;
  137. }
  138. static int cirrusfb_create(struct drm_fb_helper *helper,
  139. struct drm_fb_helper_surface_size *sizes)
  140. {
  141. struct cirrus_fbdev *gfbdev = (struct cirrus_fbdev *)helper;
  142. struct drm_device *dev = gfbdev->helper.dev;
  143. struct cirrus_device *cdev = gfbdev->helper.dev->dev_private;
  144. struct fb_info *info;
  145. struct drm_framebuffer *fb;
  146. struct drm_mode_fb_cmd2 mode_cmd;
  147. struct device *device = &dev->pdev->dev;
  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 = framebuffer_alloc(0, device);
  168. if (info == NULL)
  169. return -ENOMEM;
  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. gfbdev->helper.fbdev = info;
  184. strcpy(info->fix.id, "cirrusdrmfb");
  185. info->flags = FBINFO_DEFAULT;
  186. info->fbops = &cirrusfb_ops;
  187. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  188. drm_fb_helper_fill_var(info, &gfbdev->helper, sizes->fb_width,
  189. sizes->fb_height);
  190. /* setup aperture base/size for vesafb takeover */
  191. info->apertures = alloc_apertures(1);
  192. if (!info->apertures) {
  193. ret = -ENOMEM;
  194. goto out_iounmap;
  195. }
  196. info->apertures->ranges[0].base = cdev->dev->mode_config.fb_base;
  197. info->apertures->ranges[0].size = cdev->mc.vram_size;
  198. info->screen_base = sysram;
  199. info->screen_size = size;
  200. info->fix.mmio_start = 0;
  201. info->fix.mmio_len = 0;
  202. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  203. if (ret) {
  204. DRM_ERROR("%s: can't allocate color map\n", info->fix.id);
  205. ret = -ENOMEM;
  206. goto out_iounmap;
  207. }
  208. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  209. DRM_INFO("vram aper at 0x%lX\n", (unsigned long)info->fix.smem_start);
  210. DRM_INFO("size %lu\n", (unsigned long)info->fix.smem_len);
  211. DRM_INFO("fb depth is %d\n", fb->depth);
  212. DRM_INFO(" pitch is %d\n", fb->pitches[0]);
  213. return 0;
  214. out_iounmap:
  215. return ret;
  216. }
  217. static int cirrus_fbdev_destroy(struct drm_device *dev,
  218. struct cirrus_fbdev *gfbdev)
  219. {
  220. struct fb_info *info;
  221. struct cirrus_framebuffer *gfb = &gfbdev->gfb;
  222. if (gfbdev->helper.fbdev) {
  223. info = gfbdev->helper.fbdev;
  224. unregister_framebuffer(info);
  225. if (info->cmap.len)
  226. fb_dealloc_cmap(&info->cmap);
  227. framebuffer_release(info);
  228. }
  229. if (gfb->obj) {
  230. drm_gem_object_unreference_unlocked(gfb->obj);
  231. gfb->obj = NULL;
  232. }
  233. vfree(gfbdev->sysram);
  234. drm_fb_helper_fini(&gfbdev->helper);
  235. drm_framebuffer_unregister_private(&gfb->base);
  236. drm_framebuffer_cleanup(&gfb->base);
  237. return 0;
  238. }
  239. static struct drm_fb_helper_funcs cirrus_fb_helper_funcs = {
  240. .gamma_set = cirrus_crtc_fb_gamma_set,
  241. .gamma_get = cirrus_crtc_fb_gamma_get,
  242. .fb_probe = cirrusfb_create,
  243. };
  244. int cirrus_fbdev_init(struct cirrus_device *cdev)
  245. {
  246. struct cirrus_fbdev *gfbdev;
  247. int ret;
  248. int bpp_sel = 24;
  249. /*bpp_sel = 8;*/
  250. gfbdev = kzalloc(sizeof(struct cirrus_fbdev), GFP_KERNEL);
  251. if (!gfbdev)
  252. return -ENOMEM;
  253. cdev->mode_info.gfbdev = gfbdev;
  254. gfbdev->helper.funcs = &cirrus_fb_helper_funcs;
  255. spin_lock_init(&gfbdev->dirty_lock);
  256. ret = drm_fb_helper_init(cdev->dev, &gfbdev->helper,
  257. cdev->num_crtc, CIRRUSFB_CONN_LIMIT);
  258. if (ret) {
  259. kfree(gfbdev);
  260. return ret;
  261. }
  262. drm_fb_helper_single_add_all_connectors(&gfbdev->helper);
  263. /* disable all the possible outputs/crtcs before entering KMS mode */
  264. drm_helper_disable_unused_functions(cdev->dev);
  265. drm_fb_helper_initial_config(&gfbdev->helper, bpp_sel);
  266. return 0;
  267. }
  268. void cirrus_fbdev_fini(struct cirrus_device *cdev)
  269. {
  270. if (!cdev->mode_info.gfbdev)
  271. return;
  272. cirrus_fbdev_destroy(cdev->dev, cdev->mode_info.gfbdev);
  273. kfree(cdev->mode_info.gfbdev);
  274. cdev->mode_info.gfbdev = NULL;
  275. }