amdgpu_fb.c 11 KB

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