fb.c 8.7 KB

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