fb.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. int tegra_fb_get_tiling(struct drm_framebuffer *framebuffer,
  40. struct tegra_bo_tiling *tiling)
  41. {
  42. struct tegra_fb *fb = to_tegra_fb(framebuffer);
  43. /* TODO: handle YUV formats? */
  44. *tiling = fb->planes[0]->tiling;
  45. return 0;
  46. }
  47. static void tegra_fb_destroy(struct drm_framebuffer *framebuffer)
  48. {
  49. struct tegra_fb *fb = to_tegra_fb(framebuffer);
  50. unsigned int i;
  51. for (i = 0; i < fb->num_planes; i++) {
  52. struct tegra_bo *bo = fb->planes[i];
  53. if (bo) {
  54. if (bo->pages && bo->vaddr)
  55. vunmap(bo->vaddr);
  56. drm_gem_object_unreference_unlocked(&bo->gem);
  57. }
  58. }
  59. drm_framebuffer_cleanup(framebuffer);
  60. kfree(fb->planes);
  61. kfree(fb);
  62. }
  63. static int tegra_fb_create_handle(struct drm_framebuffer *framebuffer,
  64. struct drm_file *file, unsigned int *handle)
  65. {
  66. struct tegra_fb *fb = to_tegra_fb(framebuffer);
  67. return drm_gem_handle_create(file, &fb->planes[0]->gem, handle);
  68. }
  69. static struct drm_framebuffer_funcs tegra_fb_funcs = {
  70. .destroy = tegra_fb_destroy,
  71. .create_handle = tegra_fb_create_handle,
  72. };
  73. static struct tegra_fb *tegra_fb_alloc(struct drm_device *drm,
  74. struct drm_mode_fb_cmd2 *mode_cmd,
  75. struct tegra_bo **planes,
  76. unsigned int num_planes)
  77. {
  78. struct tegra_fb *fb;
  79. unsigned int i;
  80. int err;
  81. fb = kzalloc(sizeof(*fb), GFP_KERNEL);
  82. if (!fb)
  83. return ERR_PTR(-ENOMEM);
  84. fb->planes = kzalloc(num_planes * sizeof(*planes), GFP_KERNEL);
  85. if (!fb->planes) {
  86. kfree(fb);
  87. return ERR_PTR(-ENOMEM);
  88. }
  89. fb->num_planes = num_planes;
  90. drm_helper_mode_fill_fb_struct(&fb->base, mode_cmd);
  91. for (i = 0; i < fb->num_planes; i++)
  92. fb->planes[i] = planes[i];
  93. err = drm_framebuffer_init(drm, &fb->base, &tegra_fb_funcs);
  94. if (err < 0) {
  95. dev_err(drm->dev, "failed to initialize framebuffer: %d\n",
  96. err);
  97. kfree(fb->planes);
  98. kfree(fb);
  99. return ERR_PTR(err);
  100. }
  101. return fb;
  102. }
  103. struct drm_framebuffer *tegra_fb_create(struct drm_device *drm,
  104. struct drm_file *file,
  105. struct drm_mode_fb_cmd2 *cmd)
  106. {
  107. unsigned int hsub, vsub, i;
  108. struct tegra_bo *planes[4];
  109. struct drm_gem_object *gem;
  110. struct tegra_fb *fb;
  111. int err;
  112. hsub = drm_format_horz_chroma_subsampling(cmd->pixel_format);
  113. vsub = drm_format_vert_chroma_subsampling(cmd->pixel_format);
  114. for (i = 0; i < drm_format_num_planes(cmd->pixel_format); i++) {
  115. unsigned int width = cmd->width / (i ? hsub : 1);
  116. unsigned int height = cmd->height / (i ? vsub : 1);
  117. unsigned int size, bpp;
  118. gem = drm_gem_object_lookup(drm, file, cmd->handles[i]);
  119. if (!gem) {
  120. err = -ENXIO;
  121. goto unreference;
  122. }
  123. bpp = drm_format_plane_cpp(cmd->pixel_format, i);
  124. size = (height - 1) * cmd->pitches[i] +
  125. width * bpp + cmd->offsets[i];
  126. if (gem->size < size) {
  127. err = -EINVAL;
  128. goto unreference;
  129. }
  130. planes[i] = to_tegra_bo(gem);
  131. }
  132. fb = tegra_fb_alloc(drm, cmd, planes, i);
  133. if (IS_ERR(fb)) {
  134. err = PTR_ERR(fb);
  135. goto unreference;
  136. }
  137. return &fb->base;
  138. unreference:
  139. while (i--)
  140. drm_gem_object_unreference_unlocked(&planes[i]->gem);
  141. return ERR_PTR(err);
  142. }
  143. #ifdef CONFIG_DRM_TEGRA_FBDEV
  144. static struct fb_ops tegra_fb_ops = {
  145. .owner = THIS_MODULE,
  146. .fb_fillrect = drm_fb_helper_sys_fillrect,
  147. .fb_copyarea = drm_fb_helper_sys_copyarea,
  148. .fb_imageblit = drm_fb_helper_sys_imageblit,
  149. .fb_check_var = drm_fb_helper_check_var,
  150. .fb_set_par = drm_fb_helper_set_par,
  151. .fb_blank = drm_fb_helper_blank,
  152. .fb_pan_display = drm_fb_helper_pan_display,
  153. .fb_setcmap = drm_fb_helper_setcmap,
  154. };
  155. static int tegra_fbdev_probe(struct drm_fb_helper *helper,
  156. struct drm_fb_helper_surface_size *sizes)
  157. {
  158. struct tegra_fbdev *fbdev = to_tegra_fbdev(helper);
  159. struct tegra_drm *tegra = helper->dev->dev_private;
  160. struct drm_device *drm = helper->dev;
  161. struct drm_mode_fb_cmd2 cmd = { 0 };
  162. unsigned int bytes_per_pixel;
  163. struct drm_framebuffer *fb;
  164. unsigned long offset;
  165. struct fb_info *info;
  166. struct tegra_bo *bo;
  167. size_t size;
  168. int err;
  169. bytes_per_pixel = DIV_ROUND_UP(sizes->surface_bpp, 8);
  170. cmd.width = sizes->surface_width;
  171. cmd.height = sizes->surface_height;
  172. cmd.pitches[0] = round_up(sizes->surface_width * bytes_per_pixel,
  173. tegra->pitch_align);
  174. cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  175. sizes->surface_depth);
  176. size = cmd.pitches[0] * cmd.height;
  177. bo = tegra_bo_create(drm, size, 0);
  178. if (IS_ERR(bo))
  179. return PTR_ERR(bo);
  180. info = drm_fb_helper_alloc_fbi(helper);
  181. if (IS_ERR(info)) {
  182. dev_err(drm->dev, "failed to allocate framebuffer info\n");
  183. drm_gem_object_unreference_unlocked(&bo->gem);
  184. return PTR_ERR(info);
  185. }
  186. fbdev->fb = tegra_fb_alloc(drm, &cmd, &bo, 1);
  187. if (IS_ERR(fbdev->fb)) {
  188. err = PTR_ERR(fbdev->fb);
  189. dev_err(drm->dev, "failed to allocate DRM framebuffer: %d\n",
  190. err);
  191. drm_gem_object_unreference_unlocked(&bo->gem);
  192. goto release;
  193. }
  194. fb = &fbdev->fb->base;
  195. helper->fb = fb;
  196. helper->fbdev = info;
  197. info->par = helper;
  198. info->flags = FBINFO_FLAG_DEFAULT;
  199. info->fbops = &tegra_fb_ops;
  200. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->depth);
  201. drm_fb_helper_fill_var(info, helper, fb->width, fb->height);
  202. offset = info->var.xoffset * bytes_per_pixel +
  203. info->var.yoffset * fb->pitches[0];
  204. if (bo->pages) {
  205. bo->vaddr = vmap(bo->pages, bo->num_pages, VM_MAP,
  206. pgprot_writecombine(PAGE_KERNEL));
  207. if (!bo->vaddr) {
  208. dev_err(drm->dev, "failed to vmap() framebuffer\n");
  209. err = -ENOMEM;
  210. goto destroy;
  211. }
  212. }
  213. drm->mode_config.fb_base = (resource_size_t)bo->paddr;
  214. info->screen_base = (void __iomem *)bo->vaddr + offset;
  215. info->screen_size = size;
  216. info->fix.smem_start = (unsigned long)(bo->paddr + offset);
  217. info->fix.smem_len = size;
  218. return 0;
  219. destroy:
  220. drm_framebuffer_unregister_private(fb);
  221. tegra_fb_destroy(fb);
  222. release:
  223. drm_fb_helper_release_fbi(helper);
  224. return err;
  225. }
  226. static const struct drm_fb_helper_funcs tegra_fb_helper_funcs = {
  227. .fb_probe = tegra_fbdev_probe,
  228. };
  229. static struct tegra_fbdev *tegra_fbdev_create(struct drm_device *drm)
  230. {
  231. struct tegra_fbdev *fbdev;
  232. fbdev = kzalloc(sizeof(*fbdev), GFP_KERNEL);
  233. if (!fbdev) {
  234. dev_err(drm->dev, "failed to allocate DRM fbdev\n");
  235. return ERR_PTR(-ENOMEM);
  236. }
  237. drm_fb_helper_prepare(drm, &fbdev->base, &tegra_fb_helper_funcs);
  238. return fbdev;
  239. }
  240. static void tegra_fbdev_free(struct tegra_fbdev *fbdev)
  241. {
  242. kfree(fbdev);
  243. }
  244. static int tegra_fbdev_init(struct tegra_fbdev *fbdev,
  245. unsigned int preferred_bpp,
  246. unsigned int num_crtc,
  247. unsigned int max_connectors)
  248. {
  249. struct drm_device *drm = fbdev->base.dev;
  250. int err;
  251. err = drm_fb_helper_init(drm, &fbdev->base, num_crtc, max_connectors);
  252. if (err < 0) {
  253. dev_err(drm->dev, "failed to initialize DRM FB helper: %d\n",
  254. err);
  255. return err;
  256. }
  257. err = drm_fb_helper_single_add_all_connectors(&fbdev->base);
  258. if (err < 0) {
  259. dev_err(drm->dev, "failed to add connectors: %d\n", err);
  260. goto fini;
  261. }
  262. err = drm_fb_helper_initial_config(&fbdev->base, preferred_bpp);
  263. if (err < 0) {
  264. dev_err(drm->dev, "failed to set initial configuration: %d\n",
  265. err);
  266. goto fini;
  267. }
  268. return 0;
  269. fini:
  270. drm_fb_helper_fini(&fbdev->base);
  271. return err;
  272. }
  273. static void tegra_fbdev_exit(struct tegra_fbdev *fbdev)
  274. {
  275. drm_fb_helper_unregister_fbi(&fbdev->base);
  276. drm_fb_helper_release_fbi(&fbdev->base);
  277. if (fbdev->fb) {
  278. drm_framebuffer_unregister_private(&fbdev->fb->base);
  279. drm_framebuffer_remove(&fbdev->fb->base);
  280. }
  281. drm_fb_helper_fini(&fbdev->base);
  282. tegra_fbdev_free(fbdev);
  283. }
  284. void tegra_fbdev_restore_mode(struct tegra_fbdev *fbdev)
  285. {
  286. if (fbdev)
  287. drm_fb_helper_restore_fbdev_mode_unlocked(&fbdev->base);
  288. }
  289. void tegra_fb_output_poll_changed(struct drm_device *drm)
  290. {
  291. struct tegra_drm *tegra = drm->dev_private;
  292. if (tegra->fbdev)
  293. drm_fb_helper_hotplug_event(&tegra->fbdev->base);
  294. }
  295. #endif
  296. int tegra_drm_fb_prepare(struct drm_device *drm)
  297. {
  298. #ifdef CONFIG_DRM_TEGRA_FBDEV
  299. struct tegra_drm *tegra = drm->dev_private;
  300. tegra->fbdev = tegra_fbdev_create(drm);
  301. if (IS_ERR(tegra->fbdev))
  302. return PTR_ERR(tegra->fbdev);
  303. #endif
  304. return 0;
  305. }
  306. void tegra_drm_fb_free(struct drm_device *drm)
  307. {
  308. #ifdef CONFIG_DRM_TEGRA_FBDEV
  309. struct tegra_drm *tegra = drm->dev_private;
  310. tegra_fbdev_free(tegra->fbdev);
  311. #endif
  312. }
  313. int tegra_drm_fb_init(struct drm_device *drm)
  314. {
  315. #ifdef CONFIG_DRM_TEGRA_FBDEV
  316. struct tegra_drm *tegra = drm->dev_private;
  317. int err;
  318. err = tegra_fbdev_init(tegra->fbdev, 32, drm->mode_config.num_crtc,
  319. drm->mode_config.num_connector);
  320. if (err < 0)
  321. return err;
  322. #endif
  323. return 0;
  324. }
  325. void tegra_drm_fb_exit(struct drm_device *drm)
  326. {
  327. #ifdef CONFIG_DRM_TEGRA_FBDEV
  328. struct tegra_drm *tegra = drm->dev_private;
  329. tegra_fbdev_exit(tegra->fbdev);
  330. #endif
  331. }