fb.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright (C) 2012-2013 Avionic Design GmbH
  3. * Copyright (C) 2012 NVIDIA CORPORATION. All rights reserved.
  4. *
  5. * Based on the KMS/FB CMA helpers
  6. * Copyright (C) 2012 Analog Device Inc.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. */
  12. #include "drm.h"
  13. #include "gem.h"
  14. static inline struct tegra_fb *to_tegra_fb(struct drm_framebuffer *fb)
  15. {
  16. return container_of(fb, struct tegra_fb, base);
  17. }
  18. #ifdef CONFIG_DRM_TEGRA_FBDEV
  19. static inline struct tegra_fbdev *to_tegra_fbdev(struct drm_fb_helper *helper)
  20. {
  21. return container_of(helper, struct tegra_fbdev, base);
  22. }
  23. #endif
  24. struct tegra_bo *tegra_fb_get_plane(struct drm_framebuffer *framebuffer,
  25. unsigned int index)
  26. {
  27. struct tegra_fb *fb = to_tegra_fb(framebuffer);
  28. if (index >= drm_format_num_planes(framebuffer->pixel_format))
  29. return NULL;
  30. return fb->planes[index];
  31. }
  32. bool tegra_fb_is_bottom_up(struct drm_framebuffer *framebuffer)
  33. {
  34. struct tegra_fb *fb = to_tegra_fb(framebuffer);
  35. if (fb->planes[0]->flags & TEGRA_BO_BOTTOM_UP)
  36. return true;
  37. return false;
  38. }
  39. bool tegra_fb_is_tiled(struct drm_framebuffer *framebuffer)
  40. {
  41. struct tegra_fb *fb = to_tegra_fb(framebuffer);
  42. if (fb->planes[0]->flags & TEGRA_BO_TILED)
  43. return true;
  44. return false;
  45. }
  46. static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
  47. {
  48. struct tegra_fb *fb = to_tegra_fb(framebuffer);
  49. unsigned int i;
  50. for (i = 0; i < fb->num_planes; i++) {
  51. struct tegra_bo *bo = fb->planes[i];
  52. if (bo)
  53. drm_gem_object_unreference_unlocked(&bo->gem);
  54. }
  55. drm_framebuffer_cleanup(framebuffer);
  56. kfree(fb->planes);
  57. kfree(fb);
  58. }
  59. static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
  60. struct drm_file *file, unsigned int *handle)
  61. {
  62. struct tegra_fb *fb = to_tegra_fb(framebuffer);
  63. return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
  64. }
  65. static struct drm_framebuffer_funcs tegra_fb_funcs = {
  66. .destroy = tegra_fb_destroy,
  67. .create_handle = tegra_fb_create_handle,
  68. };
  69. static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
  70. struct drm_mode_fb_cmd2 *mode_cmd,
  71. struct tegra_bo **planes,
  72. unsigned int num_planes)
  73. {
  74. struct tegra_fb *fb;
  75. unsigned int i;
  76. int err;
  77. fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  78. if (!fb)
  79. return ERR_PTR(-ENOMEM);
  80. fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
  81. if (!fb->planes) {
  82. kfree(fb);
  83. return ERR_PTR(-ENOMEM);
  84. }
  85. fb->num_planes = num_planes;
  86. drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
  87. for (i = 0; i < fb->num_planes; i++)
  88. fb->planes[i] = planes[i];
  89. err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
  90. if (err < 0) {
  91. dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
  92. err);
  93. kfree(fb->planes);
  94. kfree(fb);
  95. return ERR_PTR(err);
  96. }
  97. return fb;
  98. }
  99. static struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
  100. struct drm_file *file,
  101. struct drm_mode_fb_cmd2 *cmd)
  102. {
  103. unsigned int hsub, vsub, i;
  104. struct tegra_bo *planes[4];
  105. struct drm_gem_object *gem;
  106. struct tegra_fb *fb;
  107. int err;
  108. hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
  109. vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
  110. for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
  111. unsigned int width = cmd->width / (i ? hsub : 1);
  112. unsigned int height = cmd->height / (i ? vsub : 1);
  113. unsigned int size, bpp;
  114. gem = drm_gem_object_lookup(drm, file, cmd->handles[i]);
  115. if (!gem) {
  116. err = -ENXIO;
  117. goto unreference;
  118. }
  119. bpp = drm_format_plane_cpp(cmd->pixel_format, i);
  120. size = (height - 1) * cmd->pitches[i] +
  121. width * bpp + cmd->offsets[i];
  122. if (gem->size < size) {
  123. err = -EINVAL;
  124. goto unreference;
  125. }
  126. planes[i] = to_tegra_bo(gem);
  127. }
  128. fb = tegra_fb_alloc(drm, cmd, planes, i);
  129. if (IS_ERR(fb)) {
  130. err = PTR_ERR(fb);
  131. goto unreference;
  132. }
  133. return &fb->base;
  134. unreference:
  135. while (i--)
  136. drm_gem_object_unreference_unlocked(&planes[i]->gem);
  137. return ERR_PTR(err);
  138. }
  139. #ifdef CONFIG_DRM_TEGRA_FBDEV
  140. static struct fb_ops tegra_fb_ops = {
  141. .owner = THIS_MODULE,
  142. .fb_fillrect = sys_fillrect,
  143. .fb_copyarea = sys_copyarea,
  144. .fb_imageblit = sys_imageblit,
  145. .fb_check_var = drm_fb_helper_check_var,
  146. .fb_set_par = drm_fb_helper_set_par,
  147. .fb_blank = drm_fb_helper_blank,
  148. .fb_pan_display = drm_fb_helper_pan_display,
  149. .fb_setcmap = drm_fb_helper_setcmap,
  150. };
  151. static int tegra_fbdev_probe(struct drm_fb_helper *helper,
  152. struct drm_fb_helper_surface_size *sizes)
  153. {
  154. struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
  155. struct drm_device *drm = helper->dev;
  156. struct drm_mode_fb_cmd2 cmd = { 0 };
  157. unsigned int bytes_per_pixel;
  158. struct drm_framebuffer *fb;
  159. unsigned long offset;
  160. struct fb_info *info;
  161. struct tegra_bo *bo;
  162. size_t size;
  163. int err;
  164. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  165. cmd.width = sizes->surface_width;
  166. cmd.height = sizes->surface_height;
  167. cmd.pitches[0] = sizes->surface_width * bytes_per_pixel;
  168. cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  169. sizes->surface_depth);
  170. size = cmd.pitches[0] * cmd.height;
  171. bo = tegra_bo_create(drm, size, 0);
  172. if (IS_ERR(bo))
  173. return PTR_ERR(bo);
  174. info = framebuffer_alloc(0, drm->dev);
  175. if (!info) {
  176. dev_err(drm->dev, "failed to allocate framebuffer info\n");
  177. tegra_bo_free_object(&bo->gem);
  178. return -ENOMEM;
  179. }
  180. fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
  181. if (IS_ERR(fbdev->fb)) {
  182. dev_err(drm->dev, "failed to allocate DRM framebuffer\n");
  183. err = PTR_ERR(fbdev->fb);
  184. goto release;
  185. }
  186. fb = &fbdev->fb->base;
  187. helper->fb = fb;
  188. helper->fbdev = info;
  189. info->par = helper;
  190. info->flags = FBINFO_FLAG_DEFAULT;
  191. info->fbops = &tegra_fb_ops;
  192. err = fb_alloc_cmap(&info->cmap, 256, 0);
  193. if (err < 0) {
  194. dev_err(drm->dev, "failed to allocate color map: %d\n", err);
  195. goto destroy;
  196. }
  197. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  198. drm_fb_helper_fill_var(info, helper, fb->width, fb->height);
  199. offset = info->var.xoffset * bytes_per_pixel +
  200. info->var.yoffset * fb->pitches[0];
  201. drm->mode_config.fb_base = (resource_size_t)bo->paddr;
  202. info->screen_base = (void __iomem *)bo->vaddr + offset;
  203. info->screen_size = size;
  204. info->fix.smem_start = (unsigned long)(bo->paddr + offset);
  205. info->fix.smem_len = size;
  206. return 0;
  207. destroy:
  208. drm_framebuffer_unregister_private(fb);
  209. tegra_fb_destroy(fb);
  210. release:
  211. framebuffer_release(info);
  212. return err;
  213. }
  214. static struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
  215. .fb_probe = tegra_fbdev_probe,
  216. };
  217. static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm,
  218. unsigned int preferred_bpp,
  219. unsigned int num_crtc,
  220. unsigned int max_connectors)
  221. {
  222. struct drm_fb_helper *helper;
  223. struct tegra_fbdev *fbdev;
  224. int err;
  225. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  226. if (!fbdev) {
  227. dev_err(drm->dev, "failed to allocate DRM fbdev\n");
  228. return ERR_PTR(-ENOMEM);
  229. }
  230. fbdev->base.funcs = &tegra_fb_helper_funcs;
  231. helper = &fbdev->base;
  232. err = drm_fb_helper_init(drm, &fbdev->base, num_crtc, max_connectors);
  233. if (err < 0) {
  234. dev_err(drm->dev, "failed to initialize DRM FB helper\n");
  235. goto free;
  236. }
  237. err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
  238. if (err < 0) {
  239. dev_err(drm->dev, "failed to add connectors\n");
  240. goto fini;
  241. }
  242. drm_helper_disable_unused_functions(drm);
  243. err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
  244. if (err < 0) {
  245. dev_err(drm->dev, "failed to set initial configuration\n");
  246. goto fini;
  247. }
  248. return fbdev;
  249. fini:
  250. drm_fb_helper_fini(&fbdev->base);
  251. free:
  252. kfree(fbdev);
  253. return ERR_PTR(err);
  254. }
  255. static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
  256. {
  257. struct fb_info *info = fbdev->base.fbdev;
  258. if (info) {
  259. int err;
  260. err = unregister_framebuffer(info);
  261. if (err < 0)
  262. DRM_DEBUG_KMS("failed to unregister framebuffer\n");
  263. if (info->cmap.len)
  264. fb_dealloc_cmap(&info->cmap);
  265. framebuffer_release(info);
  266. }
  267. if (fbdev->fb) {
  268. drm_framebuffer_unregister_private(&fbdev->fb->base);
  269. tegra_fb_destroy(&fbdev->fb->base);
  270. }
  271. drm_fb_helper_fini(&fbdev->base);
  272. kfree(fbdev);
  273. }
  274. void tegra_fbdev_restore_mode(struct tegra_fbdev *fbdev)
  275. {
  276. if (fbdev)
  277. drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev->base);
  278. }
  279. static void tegra_fb_output_poll_changed(struct drm_device *drm)
  280. {
  281. struct tegra_drm *tegra = drm->dev_private;
  282. if (tegra->fbdev)
  283. drm_fb_helper_hotplug_event(&tegra->fbdev->base);
  284. }
  285. #endif
  286. static const struct drm_mode_config_funcs tegra_drm_mode_funcs = {
  287. .fb_create = tegra_fb_create,
  288. #ifdef CONFIG_DRM_TEGRA_FBDEV
  289. .output_poll_changed = tegra_fb_output_poll_changed,
  290. #endif
  291. };
  292. int tegra_drm_fb_init(struct drm_device *drm)
  293. {
  294. #ifdef CONFIG_DRM_TEGRA_FBDEV
  295. struct tegra_drm *tegra = drm->dev_private;
  296. #endif
  297. drm->mode_config.min_width = 0;
  298. drm->mode_config.min_height = 0;
  299. drm->mode_config.max_width = 4096;
  300. drm->mode_config.max_height = 4096;
  301. drm->mode_config.funcs = &tegra_drm_mode_funcs;
  302. #ifdef CONFIG_DRM_TEGRA_FBDEV
  303. tegra->fbdev = tegra_fbdev_create(drm, 32, drm->mode_config.num_crtc,
  304. drm->mode_config.num_connector);
  305. if (IS_ERR(tegra->fbdev))
  306. return PTR_ERR(tegra->fbdev);
  307. #endif
  308. return 0;
  309. }
  310. void tegra_drm_fb_exit(struct drm_device *drm)
  311. {
  312. #ifdef CONFIG_DRM_TEGRA_FBDEV
  313. struct tegra_drm *tegra = drm->dev_private;
  314. tegra_fbdev_free(tegra->fbdev);
  315. #endif
  316. }