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