fb.c 9.1 KB

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