radeon_fbdev.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright © 2007 David Airlie
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors:
  24. * David Airlie
  25. */
  26. #include <linux/fb.h>
  27. #include <linux/pci.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/vga_switcheroo.h>
  30. #include <drm/drm_crtc_helper.h>
  31. #include <drm/drm_drv.h>
  32. #include <drm/drm_fb_helper.h>
  33. #include <drm/drm_fourcc.h>
  34. #include <drm/drm_framebuffer.h>
  35. #include <drm/drm_gem_framebuffer_helper.h>
  36. #include "radeon.h"
  37. static void radeon_fbdev_destroy_pinned_object(struct drm_gem_object *gobj)
  38. {
  39. struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
  40. int ret;
  41. ret = radeon_bo_reserve(rbo, false);
  42. if (likely(ret == 0)) {
  43. radeon_bo_kunmap(rbo);
  44. radeon_bo_unpin(rbo);
  45. radeon_bo_unreserve(rbo);
  46. }
  47. drm_gem_object_put(gobj);
  48. }
  49. static int radeon_fbdev_create_pinned_object(struct drm_fb_helper *fb_helper,
  50. const struct drm_format_info *info,
  51. struct drm_mode_fb_cmd2 *mode_cmd,
  52. struct drm_gem_object **gobj_p)
  53. {
  54. struct radeon_device *rdev = fb_helper->dev->dev_private;
  55. struct drm_gem_object *gobj = NULL;
  56. struct radeon_bo *rbo = NULL;
  57. bool fb_tiled = false; /* useful for testing */
  58. u32 tiling_flags = 0;
  59. int ret;
  60. int aligned_size, size;
  61. int height = mode_cmd->height;
  62. u32 cpp;
  63. cpp = info->cpp[0];
  64. /* need to align pitch with crtc limits */
  65. mode_cmd->pitches[0] = radeon_align_pitch(rdev, mode_cmd->width, cpp,
  66. fb_tiled);
  67. if (rdev->family >= CHIP_R600)
  68. height = ALIGN(mode_cmd->height, 8);
  69. size = mode_cmd->pitches[0] * height;
  70. aligned_size = ALIGN(size, PAGE_SIZE);
  71. ret = radeon_gem_object_create(rdev, aligned_size, 0,
  72. RADEON_GEM_DOMAIN_VRAM,
  73. 0, true, &gobj);
  74. if (ret) {
  75. pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
  76. return -ENOMEM;
  77. }
  78. rbo = gem_to_radeon_bo(gobj);
  79. if (fb_tiled)
  80. tiling_flags = RADEON_TILING_MACRO;
  81. #ifdef __BIG_ENDIAN
  82. switch (cpp) {
  83. case 4:
  84. tiling_flags |= RADEON_TILING_SWAP_32BIT;
  85. break;
  86. case 2:
  87. tiling_flags |= RADEON_TILING_SWAP_16BIT;
  88. break;
  89. default:
  90. break;
  91. }
  92. #endif
  93. if (tiling_flags) {
  94. ret = radeon_bo_set_tiling_flags(rbo,
  95. tiling_flags | RADEON_TILING_SURFACE,
  96. mode_cmd->pitches[0]);
  97. if (ret)
  98. dev_err(rdev->dev, "FB failed to set tiling flags\n");
  99. }
  100. ret = radeon_bo_reserve(rbo, false);
  101. if (unlikely(ret != 0))
  102. goto err_radeon_fbdev_destroy_pinned_object;
  103. /* Only 27 bit offset for legacy CRTC */
  104. ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
  105. ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
  106. NULL);
  107. if (ret) {
  108. radeon_bo_unreserve(rbo);
  109. goto err_radeon_fbdev_destroy_pinned_object;
  110. }
  111. if (fb_tiled)
  112. radeon_bo_check_tiling(rbo, 0, 0);
  113. ret = radeon_bo_kmap(rbo, NULL);
  114. radeon_bo_unreserve(rbo);
  115. if (ret)
  116. goto err_radeon_fbdev_destroy_pinned_object;
  117. *gobj_p = gobj;
  118. return 0;
  119. err_radeon_fbdev_destroy_pinned_object:
  120. radeon_fbdev_destroy_pinned_object(gobj);
  121. *gobj_p = NULL;
  122. return ret;
  123. }
  124. /*
  125. * Fbdev ops and struct fb_ops
  126. */
  127. static int radeon_fbdev_fb_open(struct fb_info *info, int user)
  128. {
  129. struct drm_fb_helper *fb_helper = info->par;
  130. struct radeon_device *rdev = fb_helper->dev->dev_private;
  131. int ret;
  132. ret = pm_runtime_get_sync(rdev_to_drm(rdev)->dev);
  133. if (ret < 0 && ret != -EACCES)
  134. goto err_pm_runtime_mark_last_busy;
  135. return 0;
  136. err_pm_runtime_mark_last_busy:
  137. pm_runtime_put_autosuspend(rdev_to_drm(rdev)->dev);
  138. return ret;
  139. }
  140. static int radeon_fbdev_fb_release(struct fb_info *info, int user)
  141. {
  142. struct drm_fb_helper *fb_helper = info->par;
  143. struct radeon_device *rdev = fb_helper->dev->dev_private;
  144. pm_runtime_put_autosuspend(rdev_to_drm(rdev)->dev);
  145. return 0;
  146. }
  147. static void radeon_fbdev_fb_destroy(struct fb_info *info)
  148. {
  149. struct drm_fb_helper *fb_helper = info->par;
  150. struct drm_framebuffer *fb = fb_helper->fb;
  151. struct drm_gem_object *gobj = drm_gem_fb_get_obj(fb, 0);
  152. drm_fb_helper_fini(fb_helper);
  153. drm_framebuffer_unregister_private(fb);
  154. drm_framebuffer_cleanup(fb);
  155. kfree(fb);
  156. radeon_fbdev_destroy_pinned_object(gobj);
  157. drm_client_release(&fb_helper->client);
  158. }
  159. static const struct fb_ops radeon_fbdev_fb_ops = {
  160. .owner = THIS_MODULE,
  161. .fb_open = radeon_fbdev_fb_open,
  162. .fb_release = radeon_fbdev_fb_release,
  163. FB_DEFAULT_IOMEM_OPS,
  164. DRM_FB_HELPER_DEFAULT_OPS,
  165. .fb_destroy = radeon_fbdev_fb_destroy,
  166. };
  167. static const struct drm_fb_helper_funcs radeon_fbdev_fb_helper_funcs = {
  168. };
  169. int radeon_fbdev_driver_fbdev_probe(struct drm_fb_helper *fb_helper,
  170. struct drm_fb_helper_surface_size *sizes)
  171. {
  172. struct radeon_device *rdev = fb_helper->dev->dev_private;
  173. const struct drm_format_info *format_info;
  174. struct drm_mode_fb_cmd2 mode_cmd = { };
  175. struct fb_info *info = fb_helper->info;
  176. struct drm_gem_object *gobj;
  177. struct radeon_bo *rbo;
  178. struct drm_framebuffer *fb;
  179. int ret;
  180. unsigned long tmp;
  181. mode_cmd.width = sizes->surface_width;
  182. mode_cmd.height = sizes->surface_height;
  183. /* avivo can't scanout real 24bpp */
  184. if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
  185. sizes->surface_bpp = 32;
  186. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  187. sizes->surface_depth);
  188. format_info = drm_get_format_info(rdev_to_drm(rdev), mode_cmd.pixel_format,
  189. mode_cmd.modifier[0]);
  190. ret = radeon_fbdev_create_pinned_object(fb_helper, format_info, &mode_cmd, &gobj);
  191. if (ret) {
  192. DRM_ERROR("failed to create fbcon object %d\n", ret);
  193. return ret;
  194. }
  195. rbo = gem_to_radeon_bo(gobj);
  196. fb = kzalloc_obj(*fb);
  197. if (!fb) {
  198. ret = -ENOMEM;
  199. goto err_radeon_fbdev_destroy_pinned_object;
  200. }
  201. ret = radeon_framebuffer_init(rdev_to_drm(rdev), fb, format_info, &mode_cmd, gobj);
  202. if (ret) {
  203. DRM_ERROR("failed to initialize framebuffer %d\n", ret);
  204. goto err_kfree;
  205. }
  206. /* setup helper */
  207. fb_helper->funcs = &radeon_fbdev_fb_helper_funcs;
  208. fb_helper->fb = fb;
  209. info->fbops = &radeon_fbdev_fb_ops;
  210. /* radeon resume is fragile and needs a vt switch to help it along */
  211. info->skip_vt_switch = false;
  212. drm_fb_helper_fill_info(info, fb_helper, sizes);
  213. tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
  214. info->fix.smem_start = rdev->mc.aper_base + tmp;
  215. info->fix.smem_len = radeon_bo_size(rbo);
  216. info->screen_base = (__force void __iomem *)rbo->kptr;
  217. info->screen_size = radeon_bo_size(rbo);
  218. memset_io(info->screen_base, 0, info->screen_size);
  219. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  220. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  221. DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
  222. DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
  223. DRM_INFO("fb depth is %d\n", fb->format->depth);
  224. DRM_INFO(" pitch is %d\n", fb->pitches[0]);
  225. return 0;
  226. err_kfree:
  227. kfree(fb);
  228. err_radeon_fbdev_destroy_pinned_object:
  229. radeon_fbdev_destroy_pinned_object(gobj);
  230. return ret;
  231. }
  232. bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
  233. {
  234. struct drm_fb_helper *fb_helper = rdev_to_drm(rdev)->fb_helper;
  235. struct drm_gem_object *gobj;
  236. if (!fb_helper)
  237. return false;
  238. gobj = drm_gem_fb_get_obj(fb_helper->fb, 0);
  239. if (!gobj)
  240. return false;
  241. if (gobj != &robj->tbo.base)
  242. return false;
  243. return true;
  244. }