fb.c 10 KB

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