amdgpu_fb.c 10 KB

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