armada_fb.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012 Russell King
  4. */
  5. #include <drm/drm_modeset_helper.h>
  6. #include <drm/drm_fb_helper.h>
  7. #include <drm/drm_fourcc.h>
  8. #include <drm/drm_gem_framebuffer_helper.h>
  9. #include "armada_drm.h"
  10. #include "armada_fb.h"
  11. #include "armada_gem.h"
  12. #include "armada_hw.h"
  13. static const struct drm_framebuffer_funcs armada_fb_funcs = {
  14. .destroy = drm_gem_fb_destroy,
  15. .create_handle = drm_gem_fb_create_handle,
  16. };
  17. struct armada_framebuffer *armada_framebuffer_create(struct drm_device *dev,
  18. const struct drm_mode_fb_cmd2 *mode, struct armada_gem_object *obj)
  19. {
  20. struct armada_framebuffer *dfb;
  21. uint8_t format, config;
  22. int ret;
  23. switch (mode->pixel_format) {
  24. #define FMT(drm, fmt, mod) \
  25. case DRM_FORMAT_##drm: \
  26. format = CFG_##fmt; \
  27. config = mod; \
  28. break
  29. FMT(RGB565, 565, CFG_SWAPRB);
  30. FMT(BGR565, 565, 0);
  31. FMT(ARGB1555, 1555, CFG_SWAPRB);
  32. FMT(ABGR1555, 1555, 0);
  33. FMT(RGB888, 888PACK, CFG_SWAPRB);
  34. FMT(BGR888, 888PACK, 0);
  35. FMT(XRGB8888, X888, CFG_SWAPRB);
  36. FMT(XBGR8888, X888, 0);
  37. FMT(ARGB8888, 8888, CFG_SWAPRB);
  38. FMT(ABGR8888, 8888, 0);
  39. FMT(YUYV, 422PACK, CFG_YUV2RGB | CFG_SWAPYU | CFG_SWAPUV);
  40. FMT(UYVY, 422PACK, CFG_YUV2RGB);
  41. FMT(VYUY, 422PACK, CFG_YUV2RGB | CFG_SWAPUV);
  42. FMT(YVYU, 422PACK, CFG_YUV2RGB | CFG_SWAPYU);
  43. FMT(YUV422, 422, CFG_YUV2RGB);
  44. FMT(YVU422, 422, CFG_YUV2RGB | CFG_SWAPUV);
  45. FMT(YUV420, 420, CFG_YUV2RGB);
  46. FMT(YVU420, 420, CFG_YUV2RGB | CFG_SWAPUV);
  47. FMT(C8, PSEUDO8, 0);
  48. #undef FMT
  49. default:
  50. return ERR_PTR(-EINVAL);
  51. }
  52. dfb = kzalloc(sizeof(*dfb), GFP_KERNEL);
  53. if (!dfb) {
  54. DRM_ERROR("failed to allocate Armada fb object\n");
  55. return ERR_PTR(-ENOMEM);
  56. }
  57. dfb->fmt = format;
  58. dfb->mod = config;
  59. dfb->fb.obj[0] = &obj->obj;
  60. drm_helper_mode_fill_fb_struct(dev, &dfb->fb, mode);
  61. ret = drm_framebuffer_init(dev, &dfb->fb, &armada_fb_funcs);
  62. if (ret) {
  63. kfree(dfb);
  64. return ERR_PTR(ret);
  65. }
  66. /*
  67. * Take a reference on our object as we're successful - the
  68. * caller already holds a reference, which keeps us safe for
  69. * the above call, but the caller will drop their reference
  70. * to it. Hence we need to take our own reference.
  71. */
  72. drm_gem_object_get(&obj->obj);
  73. return dfb;
  74. }
  75. struct drm_framebuffer *armada_fb_create(struct drm_device *dev,
  76. struct drm_file *dfile, const struct drm_mode_fb_cmd2 *mode)
  77. {
  78. const struct drm_format_info *info = drm_get_format_info(dev, mode);
  79. struct armada_gem_object *obj;
  80. struct armada_framebuffer *dfb;
  81. int ret;
  82. DRM_DEBUG_DRIVER("w%u h%u pf%08x f%u p%u,%u,%u\n",
  83. mode->width, mode->height, mode->pixel_format,
  84. mode->flags, mode->pitches[0], mode->pitches[1],
  85. mode->pitches[2]);
  86. /* We can only handle a single plane at the moment */
  87. if (info->num_planes > 1 &&
  88. (mode->handles[0] != mode->handles[1] ||
  89. mode->handles[0] != mode->handles[2])) {
  90. ret = -EINVAL;
  91. goto err;
  92. }
  93. obj = armada_gem_object_lookup(dfile, mode->handles[0]);
  94. if (!obj) {
  95. ret = -ENOENT;
  96. goto err;
  97. }
  98. if (obj->obj.import_attach && !obj->sgt) {
  99. ret = armada_gem_map_import(obj);
  100. if (ret)
  101. goto err_unref;
  102. }
  103. /* Framebuffer objects must have a valid device address for scanout */
  104. if (!obj->mapped) {
  105. ret = -EINVAL;
  106. goto err_unref;
  107. }
  108. dfb = armada_framebuffer_create(dev, mode, obj);
  109. if (IS_ERR(dfb)) {
  110. ret = PTR_ERR(dfb);
  111. goto err;
  112. }
  113. drm_gem_object_put(&obj->obj);
  114. return &dfb->fb;
  115. err_unref:
  116. drm_gem_object_put(&obj->obj);
  117. err:
  118. DRM_ERROR("failed to initialize framebuffer: %d\n", ret);
  119. return ERR_PTR(ret);
  120. }