radeon_fb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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/radeon_drm.h>
  33. #include "radeon.h"
  34. #include <drm/drm_fb_helper.h>
  35. #include <linux/vga_switcheroo.h>
  36. /* object hierarchy -
  37. * this contains a helper + a radeon fb
  38. * the helper contains a pointer to radeon framebuffer baseclass.
  39. */
  40. struct radeon_fbdev {
  41. struct drm_fb_helper helper;
  42. struct drm_framebuffer fb;
  43. struct radeon_device *rdev;
  44. };
  45. static int
  46. radeonfb_open(struct fb_info *info, int user)
  47. {
  48. struct radeon_fbdev *rfbdev = info->par;
  49. struct radeon_device *rdev = rfbdev->rdev;
  50. int ret = pm_runtime_get_sync(rdev->ddev->dev);
  51. if (ret < 0 && ret != -EACCES) {
  52. pm_runtime_mark_last_busy(rdev->ddev->dev);
  53. pm_runtime_put_autosuspend(rdev->ddev->dev);
  54. return ret;
  55. }
  56. return 0;
  57. }
  58. static int
  59. radeonfb_release(struct fb_info *info, int user)
  60. {
  61. struct radeon_fbdev *rfbdev = info->par;
  62. struct radeon_device *rdev = rfbdev->rdev;
  63. pm_runtime_mark_last_busy(rdev->ddev->dev);
  64. pm_runtime_put_autosuspend(rdev->ddev->dev);
  65. return 0;
  66. }
  67. static struct fb_ops radeonfb_ops = {
  68. .owner = THIS_MODULE,
  69. DRM_FB_HELPER_DEFAULT_OPS,
  70. .fb_open = radeonfb_open,
  71. .fb_release = radeonfb_release,
  72. .fb_fillrect = drm_fb_helper_cfb_fillrect,
  73. .fb_copyarea = drm_fb_helper_cfb_copyarea,
  74. .fb_imageblit = drm_fb_helper_cfb_imageblit,
  75. };
  76. int radeon_align_pitch(struct radeon_device *rdev, int width, int cpp, bool tiled)
  77. {
  78. int aligned = width;
  79. int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
  80. int pitch_mask = 0;
  81. switch (cpp) {
  82. case 1:
  83. pitch_mask = align_large ? 255 : 127;
  84. break;
  85. case 2:
  86. pitch_mask = align_large ? 127 : 31;
  87. break;
  88. case 3:
  89. case 4:
  90. pitch_mask = align_large ? 63 : 15;
  91. break;
  92. }
  93. aligned += pitch_mask;
  94. aligned &= ~pitch_mask;
  95. return aligned * cpp;
  96. }
  97. static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
  98. {
  99. struct radeon_bo *rbo = gem_to_radeon_bo(gobj);
  100. int ret;
  101. ret = radeon_bo_reserve(rbo, false);
  102. if (likely(ret == 0)) {
  103. radeon_bo_kunmap(rbo);
  104. radeon_bo_unpin(rbo);
  105. radeon_bo_unreserve(rbo);
  106. }
  107. drm_gem_object_put_unlocked(gobj);
  108. }
  109. static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
  110. struct drm_mode_fb_cmd2 *mode_cmd,
  111. struct drm_gem_object **gobj_p)
  112. {
  113. struct radeon_device *rdev = rfbdev->rdev;
  114. struct drm_gem_object *gobj = NULL;
  115. struct radeon_bo *rbo = 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] = radeon_align_pitch(rdev, mode_cmd->width, cpp,
  125. fb_tiled);
  126. if (rdev->family >= CHIP_R600)
  127. height = ALIGN(mode_cmd->height, 8);
  128. size = mode_cmd->pitches[0] * height;
  129. aligned_size = ALIGN(size, PAGE_SIZE);
  130. ret = radeon_gem_object_create(rdev, aligned_size, 0,
  131. RADEON_GEM_DOMAIN_VRAM,
  132. 0, true, &gobj);
  133. if (ret) {
  134. pr_err("failed to allocate framebuffer (%d)\n", aligned_size);
  135. return -ENOMEM;
  136. }
  137. rbo = gem_to_radeon_bo(gobj);
  138. if (fb_tiled)
  139. tiling_flags = RADEON_TILING_MACRO;
  140. #ifdef __BIG_ENDIAN
  141. switch (cpp) {
  142. case 4:
  143. tiling_flags |= RADEON_TILING_SWAP_32BIT;
  144. break;
  145. case 2:
  146. tiling_flags |= RADEON_TILING_SWAP_16BIT;
  147. default:
  148. break;
  149. }
  150. #endif
  151. if (tiling_flags) {
  152. ret = radeon_bo_set_tiling_flags(rbo,
  153. tiling_flags | RADEON_TILING_SURFACE,
  154. mode_cmd->pitches[0]);
  155. if (ret)
  156. dev_err(rdev->dev, "FB failed to set tiling flags\n");
  157. }
  158. ret = radeon_bo_reserve(rbo, false);
  159. if (unlikely(ret != 0))
  160. goto out_unref;
  161. /* Only 27 bit offset for legacy CRTC */
  162. ret = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
  163. ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
  164. NULL);
  165. if (ret) {
  166. radeon_bo_unreserve(rbo);
  167. goto out_unref;
  168. }
  169. if (fb_tiled)
  170. radeon_bo_check_tiling(rbo, 0, 0);
  171. ret = radeon_bo_kmap(rbo, NULL);
  172. radeon_bo_unreserve(rbo);
  173. if (ret) {
  174. goto out_unref;
  175. }
  176. *gobj_p = gobj;
  177. return 0;
  178. out_unref:
  179. radeonfb_destroy_pinned_object(gobj);
  180. *gobj_p = NULL;
  181. return ret;
  182. }
  183. static int radeonfb_create(struct drm_fb_helper *helper,
  184. struct drm_fb_helper_surface_size *sizes)
  185. {
  186. struct radeon_fbdev *rfbdev =
  187. container_of(helper, struct radeon_fbdev, helper);
  188. struct radeon_device *rdev = rfbdev->rdev;
  189. struct fb_info *info;
  190. struct drm_framebuffer *fb = NULL;
  191. struct drm_mode_fb_cmd2 mode_cmd;
  192. struct drm_gem_object *gobj = NULL;
  193. struct radeon_bo *rbo = NULL;
  194. int ret;
  195. unsigned long tmp;
  196. mode_cmd.width = sizes->surface_width;
  197. mode_cmd.height = sizes->surface_height;
  198. /* avivo can't scanout real 24bpp */
  199. if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
  200. sizes->surface_bpp = 32;
  201. mode_cmd.pixel_format = drm_mode_legacy_fb_format(sizes->surface_bpp,
  202. sizes->surface_depth);
  203. ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
  204. if (ret) {
  205. DRM_ERROR("failed to create fbcon object %d\n", ret);
  206. return ret;
  207. }
  208. rbo = gem_to_radeon_bo(gobj);
  209. /* okay we have an object now allocate the framebuffer */
  210. info = drm_fb_helper_alloc_fbi(helper);
  211. if (IS_ERR(info)) {
  212. ret = PTR_ERR(info);
  213. goto out;
  214. }
  215. info->par = rfbdev;
  216. ret = radeon_framebuffer_init(rdev->ddev, &rfbdev->fb, &mode_cmd, gobj);
  217. if (ret) {
  218. DRM_ERROR("failed to initialize framebuffer %d\n", ret);
  219. goto out;
  220. }
  221. fb = &rfbdev->fb;
  222. /* setup helper */
  223. rfbdev->helper.fb = fb;
  224. memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
  225. strcpy(info->fix.id, "radeondrmfb");
  226. drm_fb_helper_fill_fix(info, fb->pitches[0], fb->format->depth);
  227. info->fbops = &radeonfb_ops;
  228. tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
  229. info->fix.smem_start = rdev->mc.aper_base + tmp;
  230. info->fix.smem_len = radeon_bo_size(rbo);
  231. info->screen_base = rbo->kptr;
  232. info->screen_size = radeon_bo_size(rbo);
  233. drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
  234. /* setup aperture base/size for vesafb takeover */
  235. info->apertures->ranges[0].base = rdev->ddev->mode_config.fb_base;
  236. info->apertures->ranges[0].size = rdev->mc.aper_size;
  237. /* Use default scratch pixmap (info->pixmap.flags = FB_PIXMAP_SYSTEM) */
  238. if (info->screen_base == NULL) {
  239. ret = -ENOSPC;
  240. goto out;
  241. }
  242. DRM_INFO("fb mappable at 0x%lX\n", info->fix.smem_start);
  243. DRM_INFO("vram apper at 0x%lX\n", (unsigned long)rdev->mc.aper_base);
  244. DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
  245. DRM_INFO("fb depth is %d\n", fb->format->depth);
  246. DRM_INFO(" pitch is %d\n", fb->pitches[0]);
  247. vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
  248. return 0;
  249. out:
  250. if (rbo) {
  251. }
  252. if (fb && ret) {
  253. drm_gem_object_put_unlocked(gobj);
  254. drm_framebuffer_unregister_private(fb);
  255. drm_framebuffer_cleanup(fb);
  256. kfree(fb);
  257. }
  258. return ret;
  259. }
  260. static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
  261. {
  262. struct drm_framebuffer *fb = &rfbdev->fb;
  263. drm_fb_helper_unregister_fbi(&rfbdev->helper);
  264. if (fb->obj[0]) {
  265. radeonfb_destroy_pinned_object(fb->obj[0]);
  266. fb->obj[0] = NULL;
  267. drm_framebuffer_unregister_private(fb);
  268. drm_framebuffer_cleanup(fb);
  269. }
  270. drm_fb_helper_fini(&rfbdev->helper);
  271. return 0;
  272. }
  273. static const struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
  274. .fb_probe = radeonfb_create,
  275. };
  276. int radeon_fbdev_init(struct radeon_device *rdev)
  277. {
  278. struct radeon_fbdev *rfbdev;
  279. int bpp_sel = 32;
  280. int ret;
  281. /* don't enable fbdev if no connectors */
  282. if (list_empty(&rdev->ddev->mode_config.connector_list))
  283. return 0;
  284. /* select 8 bpp console on 8MB cards, or 16 bpp on RN50 or 32MB */
  285. if (rdev->mc.real_vram_size <= (8*1024*1024))
  286. bpp_sel = 8;
  287. else if (ASIC_IS_RN50(rdev) ||
  288. rdev->mc.real_vram_size <= (32*1024*1024))
  289. bpp_sel = 16;
  290. rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
  291. if (!rfbdev)
  292. return -ENOMEM;
  293. rfbdev->rdev = rdev;
  294. rdev->mode_info.rfbdev = rfbdev;
  295. drm_fb_helper_prepare(rdev->ddev, &rfbdev->helper,
  296. &radeon_fb_helper_funcs);
  297. ret = drm_fb_helper_init(rdev->ddev, &rfbdev->helper,
  298. RADEONFB_CONN_LIMIT);
  299. if (ret)
  300. goto free;
  301. ret = drm_fb_helper_single_add_all_connectors(&rfbdev->helper);
  302. if (ret)
  303. goto fini;
  304. /* disable all the possible outputs/crtcs before entering KMS mode */
  305. drm_helper_disable_unused_functions(rdev->ddev);
  306. ret = drm_fb_helper_initial_config(&rfbdev->helper, bpp_sel);
  307. if (ret)
  308. goto fini;
  309. return 0;
  310. fini:
  311. drm_fb_helper_fini(&rfbdev->helper);
  312. free:
  313. kfree(rfbdev);
  314. return ret;
  315. }
  316. void radeon_fbdev_fini(struct radeon_device *rdev)
  317. {
  318. if (!rdev->mode_info.rfbdev)
  319. return;
  320. radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
  321. kfree(rdev->mode_info.rfbdev);
  322. rdev->mode_info.rfbdev = NULL;
  323. }
  324. void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
  325. {
  326. if (rdev->mode_info.rfbdev)
  327. drm_fb_helper_set_suspend(&rdev->mode_info.rfbdev->helper, state);
  328. }
  329. bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
  330. {
  331. if (!rdev->mode_info.rfbdev)
  332. return false;
  333. if (robj == gem_to_radeon_bo(rdev->mode_info.rfbdev->fb.obj[0]))
  334. return true;
  335. return false;
  336. }
  337. void radeon_fb_add_connector(struct radeon_device *rdev, struct drm_connector *connector)
  338. {
  339. if (rdev->mode_info.rfbdev)
  340. drm_fb_helper_add_one_connector(&rdev->mode_info.rfbdev->helper, connector);
  341. }
  342. void radeon_fb_remove_connector(struct radeon_device *rdev, struct drm_connector *connector)
  343. {
  344. if (rdev->mode_info.rfbdev)
  345. drm_fb_helper_remove_one_connector(&rdev->mode_info.rfbdev->helper, connector);
  346. }