amdgpu_fb.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/pm_runtime.h>
  29. #include <drm/drmP.h>
  30. #include <drm/drm_crtc.h>
  31. #include <drm/drm_crtc_helper.h>
  32. #include <drm/amdgpu_drm.h>
  33. #include "amdgpu.h"
  34. #include "cikd.h"
  35. #include <drm/drm_fb_helper.h>
  36. #include <linux/vga_switcheroo.h>
  37. /* object hierarchy -
  38. this contains a helper + a amdgpu fb
  39. the helper contains a pointer to amdgpu framebuffer baseclass.
  40. */
  41. static int
  42. amdgpufb_open(struct fb_info *info, int user)
  43. {
  44. struct amdgpu_fbdev *rfbdev = info->par;
  45. struct amdgpu_device *adev = rfbdev->adev;
  46. int ret = pm_runtime_get_sync(adev->ddev->dev);
  47. if (ret < 0 && ret != -EACCES) {
  48. pm_runtime_mark_last_busy(adev->ddev->dev);
  49. pm_runtime_put_autosuspend(adev->ddev->dev);
  50. return ret;
  51. }
  52. return 0;
  53. }
  54. static int
  55. amdgpufb_release(struct fb_info *info, int user)
  56. {
  57. struct amdgpu_fbdev *rfbdev = info->par;
  58. struct amdgpu_device *adev = rfbdev->adev;
  59. pm_runtime_mark_last_busy(adev->ddev->dev);
  60. pm_runtime_put_autosuspend(adev->ddev->dev);
  61. return 0;
  62. }
  63. static struct fb_ops amdgpufb_ops = {
  64. .owner = THIS_MODULE,
  65. DRM_FB_HELPER_DEFAULT_OPS,
  66. .fb_open = amdgpufb_open,
  67. .fb_release = amdgpufb_release,
  68. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  69. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  70. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  71. };
  72. int amdgpu_align_pitch(struct amdgpu_device *adev, int width, int cpp, bool tiled)
  73. {
  74. int aligned = width;
  75. int pitch_mask = 0;
  76. switch (cpp) {
  77. case 1:
  78. pitch_mask = 255;
  79. break;
  80. case 2:
  81. pitch_mask = 127;
  82. break;
  83. case 3:
  84. case 4:
  85. pitch_mask = 63;
  86. break;
  87. }
  88. aligned += pitch_mask;
  89. aligned &= ~pitch_mask;
  90. return aligned * cpp;
  91. }
  92. static void amdgpufb_destroy_pinned_object(struct drm_gem_object *gobj)
  93. {
  94. struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
  95. int ret;
  96. ret = amdgpu_bo_reserve(abo, true);
  97. if (likely(ret == 0)) {
  98. amdgpu_bo_kunmap(abo);
  99. amdgpu_bo_unpin(abo);
  100. amdgpu_bo_unreserve(abo);
  101. }
  102. drm_gem_object_put_unlocked(gobj);
  103. }
  104. static int amdgpufb_create_pinned_object(struct amdgpu_fbdev *rfbdev,
  105. struct drm_mode_fb_cmd2 *mode_cmd,
  106. struct drm_gem_object **gobj_p)
  107. {
  108. struct amdgpu_device *adev = rfbdev->adev;
  109. struct drm_gem_object *gobj = NULL;
  110. struct amdgpu_bo *abo = NULL;
  111. bool fb_tiled = false; /* useful for testing */
  112. u32 tiling_flags = 0;
  113. int ret;
  114. int aligned_size, size;
  115. int height = mode_cmd->height;
  116. u32 cpp;
  117. cpp = drm_format_plane_cpp(mode_cmd->pixel_format, 0);
  118. /* need to align pitch with crtc limits */
  119. mode_cmd->pitches[0] = amdgpu_align_pitch(adev, mode_cmd->width, cpp,
  120. fb_tiled);
  121. height = ALIGN(mode_cmd->height, 8);
  122. size = mode_cmd->pitches[0] * height;
  123. aligned_size = ALIGN(size, PAGE_SIZE);
  124. ret = amdgpu_gem_object_create(adev, aligned_size, 0,
  125. AMDGPU_GEM_DOMAIN_VRAM,
  126. AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
  127. AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
  128. AMDGPU_GEM_CREATE_VRAM_CLEARED,
  129. true, NULL, &gobj);
  130. if (ret) {
  131. pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
  132. return -ENOMEM;
  133. }
  134. abo = gem_to_amdgpu_bo(gobj);
  135. if (fb_tiled)
  136. tiling_flags = AMDGPU_TILING_SET(ARRAY_MODE, GRPH_ARRAY_2D_TILED_THIN1);
  137. ret = amdgpu_bo_reserve(abo, false);
  138. if (unlikely(ret != 0))
  139. goto out_unref;
  140. if (tiling_flags) {
  141. ret = amdgpu_bo_set_tiling_flags(abo,
  142. tiling_flags);
  143. if (ret)
  144. dev_err(adev->dev, "FB failed to set tiling flags\n");
  145. }
  146. ret = amdgpu_bo_pin(abo, AMDGPU_GEM_DOMAIN_VRAM, NULL);
  147. if (ret) {
  148. amdgpu_bo_unreserve(abo);
  149. goto out_unref;
  150. }
  151. ret = amdgpu_bo_kmap(abo, NULL);
  152. amdgpu_bo_unreserve(abo);
  153. if (ret) {
  154. goto out_unref;
  155. }
  156. *gobj_p = gobj;
  157. return 0;
  158. out_unref:
  159. amdgpufb_destroy_pinned_object(gobj);
  160. *gobj_p = NULL;
  161. return ret;
  162. }
  163. static int amdgpufb_create(struct drm_fb_helper *helper,
  164. struct drm_fb_helper_surface_size *sizes)
  165. {
  166. struct amdgpu_fbdev *rfbdev = (struct amdgpu_fbdev *)helper;
  167. struct amdgpu_device *adev = rfbdev->adev;
  168. struct fb_info *info;
  169. struct drm_framebuffer *fb = NULL;
  170. struct drm_mode_fb_cmd2 mode_cmd;
  171. struct drm_gem_object *gobj = NULL;
  172. struct amdgpu_bo *abo = NULL;
  173. int ret;
  174. unsigned long tmp;
  175. mode_cmd.width = sizes->surface_width;
  176. mode_cmd.height = sizes->surface_height;
  177. if (sizes->surface_bpp == 24)
  178. sizes->surface_bpp = 32;
  179. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  180. sizes->surface_depth);
  181. ret = amdgpufb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
  182. if (ret) {
  183. DRM_ERROR("failed to create fbcon object %d\n", ret);
  184. return ret;
  185. }
  186. abo = gem_to_amdgpu_bo(gobj);
  187. /* okay we have an object now allocate the framebuffer */
  188. info = drm_fb_helper_alloc_fbi(helper);
  189. if (IS_ERR(info)) {
  190. ret = PTR_ERR(info);
  191. goto out;
  192. }
  193. info->par = rfbdev;
  194. info->skip_vt_switch = true;
  195. ret = amdgpu_framebuffer_init(adev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
  196. if (ret) {
  197. DRM_ERROR("failed to initialize framebuffer %d\n", ret);
  198. goto out;
  199. }
  200. fb = &rfbdev->rfb.base;
  201. /* setup helper */
  202. rfbdev->helper.fb = fb;
  203. strcpy(info->fix.id, "amdgpudrmfb");
  204. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
  205. info->fbops = &amdgpufb_ops;
  206. tmp = amdgpu_bo_gpu_offset(abo) - adev->mc.vram_start;
  207. info->fix.smem_start = adev->mc.aper_base + tmp;
  208. info->fix.smem_len = amdgpu_bo_size(abo);
  209. info->screen_base = amdgpu_bo_kptr(abo);
  210. info->screen_size = amdgpu_bo_size(abo);
  211. drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
  212. /* setup aperture base/size for vesafb takeover */
  213. info->apertures->ranges[0].base = adev->ddev->mode_config.fb_base;
  214. info->apertures->ranges[0].size = adev->mc.aper_size;
  215. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  216. if (info->screen_base == NULL) {
  217. ret = -ENOSPC;
  218. goto out;
  219. }
  220. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  221. DRM_INFO("vram apper at 0x%lX\n", (unsigned long)adev->mc.aper_base);
  222. DRM_INFO("size %lu\n", (unsigned long)amdgpu_bo_size(abo));
  223. DRM_INFO("fb depth is %d\n", fb->format->depth);
  224. DRM_INFO(" pitch is %d\n", fb->pitches[0]);
  225. vga_switcheroo_client_fb_set(adev->ddev->pdev, info);
  226. return 0;
  227. out:
  228. if (abo) {
  229. }
  230. if (fb && ret) {
  231. drm_gem_object_put_unlocked(gobj);
  232. drm_framebuffer_unregister_private(fb);
  233. drm_framebuffer_cleanup(fb);
  234. kfree(fb);
  235. }
  236. return ret;
  237. }
  238. static int amdgpu_fbdev_destroy(struct drm_device *dev, struct amdgpu_fbdev *rfbdev)
  239. {
  240. struct amdgpu_framebuffer *rfb = &rfbdev->rfb;
  241. drm_fb_helper_unregister_fbi(&rfbdev->helper);
  242. if (rfb->obj) {
  243. amdgpufb_destroy_pinned_object(rfb->obj);
  244. rfb->obj = NULL;
  245. drm_framebuffer_unregister_private(&rfb->base);
  246. drm_framebuffer_cleanup(&rfb->base);
  247. }
  248. drm_fb_helper_fini(&rfbdev->helper);
  249. return 0;
  250. }
  251. static const struct drm_fb_helper_funcs amdgpu_fb_helper_funcs = {
  252. .fb_probe = amdgpufb_create,
  253. };
  254. int amdgpu_fbdev_init(struct amdgpu_device *adev)
  255. {
  256. struct amdgpu_fbdev *rfbdev;
  257. int bpp_sel = 32;
  258. int ret;
  259. /* don't init fbdev on hw without DCE */
  260. if (!adev->mode_info.mode_config_initialized)
  261. return 0;
  262. /* don't init fbdev if there are no connectors */
  263. if (list_empty(&adev->ddev->mode_config.connector_list))
  264. return 0;
  265. /* select 8 bpp console on low vram cards */
  266. if (adev->mc.real_vram_size <= (32*1024*1024))
  267. bpp_sel = 8;
  268. rfbdev = kzalloc(sizeof(struct amdgpu_fbdev), GFP_KERNEL);
  269. if (!rfbdev)
  270. return -ENOMEM;
  271. rfbdev->adev = adev;
  272. adev->mode_info.rfbdev = rfbdev;
  273. drm_fb_helper_prepare(adev->ddev, &rfbdev->helper,
  274. &amdgpu_fb_helper_funcs);
  275. ret = drm_fb_helper_init(adev->ddev, &rfbdev->helper,
  276. AMDGPUFB_CONN_LIMIT);
  277. if (ret) {
  278. kfree(rfbdev);
  279. return ret;
  280. }
  281. drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
  282. /* disable all the possible outputs/crtcs before entering KMS mode */
  283. if (!amdgpu_device_has_dc_support(adev))
  284. drm_helper_disable_unused_functions(adev->ddev);
  285. drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
  286. return 0;
  287. }
  288. void amdgpu_fbdev_fini(struct amdgpu_device *adev)
  289. {
  290. if (!adev->mode_info.rfbdev)
  291. return;
  292. amdgpu_fbdev_destroy(adev->ddev, adev->mode_info.rfbdev);
  293. kfree(adev->mode_info.rfbdev);
  294. adev->mode_info.rfbdev = NULL;
  295. }
  296. void amdgpu_fbdev_set_suspend(struct amdgpu_device *adev, int state)
  297. {
  298. if (adev->mode_info.rfbdev)
  299. drm_fb_helper_set_suspend(&adev->mode_info.rfbdev->helper,
  300. state);
  301. }
  302. int amdgpu_fbdev_total_size(struct amdgpu_device *adev)
  303. {
  304. struct amdgpu_bo *robj;
  305. int size = 0;
  306. if (!adev->mode_info.rfbdev)
  307. return 0;
  308. robj = gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj);
  309. size += amdgpu_bo_size(robj);
  310. return size;
  311. }
  312. bool amdgpu_fbdev_robj_is_fb(struct amdgpu_device *adev, struct amdgpu_bo *robj)
  313. {
  314. if (!adev->mode_info.rfbdev)
  315. return false;
  316. if (robj == gem_to_amdgpu_bo(adev->mode_info.rfbdev->rfb.obj))
  317. return true;
  318. return false;
  319. }