radeon_fb.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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/fb.h>
  28. #include "drmP.h"
  29. #include "drm.h"
  30. #include "drm_crtc.h"
  31. #include "drm_crtc_helper.h"
  32. #include "radeon_drm.h"
  33. #include "radeon.h"
  34. #include "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 radeon_framebuffer rfb;
  43. struct list_head fbdev_list;
  44. struct radeon_device *rdev;
  45. };
  46. static struct fb_ops radeonfb_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_setcolreg = drm_fb_helper_setcolreg,
  51. .fb_fillrect = cfb_fillrect,
  52. .fb_copyarea = cfb_copyarea,
  53. .fb_imageblit = 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. };
  58. static int radeon_align_pitch(struct radeon_device *rdev, int width, int bpp, bool tiled)
  59. {
  60. int aligned = width;
  61. int align_large = (ASIC_IS_AVIVO(rdev)) || tiled;
  62. int pitch_mask = 0;
  63. switch (bpp / 8) {
  64. case 1:
  65. pitch_mask = align_large ? 255 : 127;
  66. break;
  67. case 2:
  68. pitch_mask = align_large ? 127 : 31;
  69. break;
  70. case 3:
  71. case 4:
  72. pitch_mask = align_large ? 63 : 15;
  73. break;
  74. }
  75. aligned += pitch_mask;
  76. aligned &= ~pitch_mask;
  77. return aligned;
  78. }
  79. static struct drm_fb_helper_funcs radeon_fb_helper_funcs = {
  80. .gamma_set = radeon_crtc_fb_gamma_set,
  81. .gamma_get = radeon_crtc_fb_gamma_get,
  82. };
  83. static void radeonfb_destroy_pinned_object(struct drm_gem_object *gobj)
  84. {
  85. struct radeon_bo *rbo = gobj->driver_private;
  86. int ret;
  87. ret = radeon_bo_reserve(rbo, false);
  88. if (likely(ret == 0)) {
  89. radeon_bo_kunmap(rbo);
  90. radeon_bo_unreserve(rbo);
  91. }
  92. drm_gem_object_unreference_unlocked(gobj);
  93. }
  94. static int radeonfb_create_pinned_object(struct radeon_fbdev *rfbdev,
  95. struct drm_mode_fb_cmd *mode_cmd,
  96. struct drm_gem_object **gobj_p)
  97. {
  98. struct radeon_device *rdev = rfbdev->rdev;
  99. struct drm_gem_object *gobj = NULL;
  100. struct radeon_bo *rbo = NULL;
  101. bool fb_tiled = false; /* useful for testing */
  102. u32 tiling_flags = 0;
  103. int ret;
  104. int aligned_size, size;
  105. /* need to align pitch with crtc limits */
  106. mode_cmd->pitch = radeon_align_pitch(rdev, mode_cmd->width, mode_cmd->bpp, fb_tiled) * ((mode_cmd->bpp + 1) / 8);
  107. size = mode_cmd->pitch * mode_cmd->height;
  108. aligned_size = ALIGN(size, PAGE_SIZE);
  109. ret = radeon_gem_object_create(rdev, aligned_size, 0,
  110. RADEON_GEM_DOMAIN_VRAM,
  111. false, ttm_bo_type_kernel,
  112. &gobj);
  113. if (ret) {
  114. printk(KERN_ERR "failed to allocate framebuffer (%d)\n",
  115. aligned_size);
  116. return -ENOMEM;
  117. }
  118. rbo = gobj->driver_private;
  119. if (fb_tiled)
  120. tiling_flags = RADEON_TILING_MACRO;
  121. #ifdef __BIG_ENDIAN
  122. switch (mode_cmd->bpp) {
  123. case 32:
  124. tiling_flags |= RADEON_TILING_SWAP_32BIT;
  125. break;
  126. case 16:
  127. tiling_flags |= RADEON_TILING_SWAP_16BIT;
  128. default:
  129. break;
  130. }
  131. #endif
  132. if (tiling_flags) {
  133. ret = radeon_bo_set_tiling_flags(rbo,
  134. tiling_flags | RADEON_TILING_SURFACE,
  135. mode_cmd->pitch);
  136. if (ret)
  137. dev_err(rdev->dev, "FB failed to set tiling flags\n");
  138. }
  139. ret = radeon_bo_reserve(rbo, false);
  140. if (unlikely(ret != 0))
  141. goto out_unref;
  142. ret = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, NULL);
  143. if (ret) {
  144. radeon_bo_unreserve(rbo);
  145. goto out_unref;
  146. }
  147. if (fb_tiled)
  148. radeon_bo_check_tiling(rbo, 0, 0);
  149. ret = radeon_bo_kmap(rbo, NULL);
  150. radeon_bo_unreserve(rbo);
  151. if (ret) {
  152. goto out_unref;
  153. }
  154. *gobj_p = gobj;
  155. return 0;
  156. out_unref:
  157. radeonfb_destroy_pinned_object(gobj);
  158. *gobj_p = NULL;
  159. return ret;
  160. }
  161. static int radeonfb_create(struct radeon_fbdev *rfbdev,
  162. struct drm_fb_helper_surface_size *sizes)
  163. {
  164. struct radeon_device *rdev = rfbdev->rdev;
  165. struct fb_info *info;
  166. struct drm_framebuffer *fb = NULL;
  167. struct drm_mode_fb_cmd mode_cmd;
  168. struct drm_gem_object *gobj = NULL;
  169. struct radeon_bo *rbo = NULL;
  170. struct device *device = &rdev->pdev->dev;
  171. int ret;
  172. unsigned long tmp;
  173. mode_cmd.width = sizes->surface_width;
  174. mode_cmd.height = sizes->surface_height;
  175. /* avivo can't scanout real 24bpp */
  176. if ((sizes->surface_bpp == 24) && ASIC_IS_AVIVO(rdev))
  177. sizes->surface_bpp = 32;
  178. mode_cmd.bpp = sizes->surface_bpp;
  179. mode_cmd.depth = sizes->surface_depth;
  180. ret = radeonfb_create_pinned_object(rfbdev, &mode_cmd, &gobj);
  181. rbo = gobj->driver_private;
  182. /* okay we have an object now allocate the framebuffer */
  183. info = framebuffer_alloc(0, device);
  184. if (info == NULL) {
  185. ret = -ENOMEM;
  186. goto out_unref;
  187. }
  188. info->par = rfbdev;
  189. radeon_framebuffer_init(rdev->ddev, &rfbdev->rfb, &mode_cmd, gobj);
  190. fb = &rfbdev->rfb.base;
  191. /* setup helper */
  192. rfbdev->helper.fb = fb;
  193. rfbdev->helper.fbdev = info;
  194. rfbdev->helper.funcs = &radeon_fb_helper_funcs;
  195. memset_io(rbo->kptr, 0x0, radeon_bo_size(rbo));
  196. strcpy(info->fix.id, "radeondrmfb");
  197. drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
  198. info->flags = FBINFO_DEFAULT;
  199. info->fbops = &radeonfb_ops;
  200. tmp = radeon_bo_gpu_offset(rbo) - rdev->mc.vram_start;
  201. info->fix.smem_start = rdev->mc.aper_base + tmp;
  202. info->fix.smem_len = radeon_bo_size(rbo);
  203. info->screen_base = rbo->kptr;
  204. info->screen_size = radeon_bo_size(rbo);
  205. drm_fb_helper_fill_var(info, &rfbdev->helper, sizes->fb_width, sizes->fb_height);
  206. /* setup aperture base/size for vesafb takeover */
  207. info->aperture_base = rdev->ddev->mode_config.fb_base;
  208. info->aperture_size = rdev->mc.real_vram_size;
  209. info->fix.mmio_start = 0;
  210. info->fix.mmio_len = 0;
  211. info->pixmap.size = 64*1024;
  212. info->pixmap.buf_align = 8;
  213. info->pixmap.access_align = 32;
  214. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  215. info->pixmap.scan_align = 1;
  216. if (info->screen_base == NULL) {
  217. ret = -ENOSPC;
  218. goto out_unref;
  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)rdev->mc.aper_base);
  222. DRM_INFO("size %lu\n", (unsigned long)radeon_bo_size(rbo));
  223. DRM_INFO("fb depth is %d\n", fb->depth);
  224. DRM_INFO(" pitch is %d\n", fb->pitch);
  225. vga_switcheroo_client_fb_set(rdev->ddev->pdev, info);
  226. return 0;
  227. out_unref:
  228. if (rbo) {
  229. }
  230. if (fb && ret) {
  231. drm_gem_object_unreference(gobj);
  232. drm_framebuffer_cleanup(fb);
  233. kfree(fb);
  234. }
  235. out:
  236. return ret;
  237. }
  238. static int radeon_fb_find_or_create_single(struct drm_fb_helper *helper,
  239. struct drm_fb_helper_surface_size *sizes)
  240. {
  241. struct radeon_fbdev *rfbdev = (struct radeon_fbdev *)helper;
  242. int new_fb = 0;
  243. int ret;
  244. if (!helper->fb) {
  245. ret = radeonfb_create(rfbdev, sizes);
  246. if (ret)
  247. return ret;
  248. new_fb = 1;
  249. }
  250. return new_fb;
  251. }
  252. static char *mode_option;
  253. int radeon_parse_options(char *options)
  254. {
  255. char *this_opt;
  256. if (!options || !*options)
  257. return 0;
  258. while ((this_opt = strsep(&options, ",")) != NULL) {
  259. if (!*this_opt)
  260. continue;
  261. mode_option = this_opt;
  262. }
  263. return 0;
  264. }
  265. static int radeonfb_probe(struct radeon_fbdev *rfbdev)
  266. {
  267. struct radeon_device *rdev = rfbdev->rdev;
  268. int bpp_sel = 32;
  269. /* select 8 bpp console on RN50 or 16MB cards */
  270. if (ASIC_IS_RN50(rdev) || rdev->mc.real_vram_size <= (32*1024*1024))
  271. bpp_sel = 8;
  272. return drm_fb_helper_single_fb_probe(&rfbdev->helper, bpp_sel);
  273. }
  274. void radeonfb_hotplug(struct drm_device *dev)
  275. {
  276. struct radeon_device *rdev = dev->dev_private;
  277. int max_width, max_height;
  278. max_width = rdev->mode_info.rfbdev->rfb.base.width;
  279. max_height = rdev->mode_info.rfbdev->rfb.base.height;
  280. drm_helper_fb_hotplug_event(&rdev->mode_info.rfbdev->helper, max_width, max_height);
  281. radeonfb_probe(rdev->mode_info.rfbdev);
  282. }
  283. static int radeon_fbdev_destroy(struct drm_device *dev, struct radeon_fbdev *rfbdev)
  284. {
  285. struct fb_info *info;
  286. struct radeon_framebuffer *rfb = &rfbdev->rfb;
  287. struct radeon_bo *rbo;
  288. int r;
  289. if (rfbdev->helper.fbdev) {
  290. info = rfbdev->helper.fbdev;
  291. unregister_framebuffer(info);
  292. framebuffer_release(info);
  293. }
  294. if (rfb->obj) {
  295. rbo = rfb->obj->driver_private;
  296. r = radeon_bo_reserve(rbo, false);
  297. if (likely(r == 0)) {
  298. radeon_bo_kunmap(rbo);
  299. radeon_bo_unpin(rbo);
  300. radeon_bo_unreserve(rbo);
  301. }
  302. drm_gem_object_unreference_unlocked(rfb->obj);
  303. }
  304. drm_fb_helper_free(&rfbdev->helper);
  305. drm_framebuffer_cleanup(&rfb->base);
  306. return 0;
  307. }
  308. MODULE_LICENSE("GPL");
  309. int radeon_fbdev_init(struct radeon_device *rdev)
  310. {
  311. struct radeon_fbdev *rfbdev;
  312. rfbdev = kzalloc(sizeof(struct radeon_fbdev), GFP_KERNEL);
  313. if (!rfbdev)
  314. return -ENOMEM;
  315. rfbdev->rdev = rdev;
  316. rdev->mode_info.rfbdev = rfbdev;
  317. drm_fb_helper_init_crtc_count(rdev->ddev, &rfbdev->helper,
  318. rdev->num_crtc,
  319. RADEONFB_CONN_LIMIT);
  320. rfbdev->helper.fb_probe = radeon_fb_find_or_create_single;
  321. drm_fb_helper_initial_config(&rfbdev->helper);
  322. radeonfb_probe(rfbdev);
  323. return 0;
  324. }
  325. void radeon_fbdev_fini(struct radeon_device *rdev)
  326. {
  327. if (!rdev->mode_info.rfbdev)
  328. return;
  329. radeon_fbdev_destroy(rdev->ddev, rdev->mode_info.rfbdev);
  330. kfree(rdev->mode_info.rfbdev);
  331. rdev->mode_info.rfbdev = NULL;
  332. }
  333. void radeon_fbdev_set_suspend(struct radeon_device *rdev, int state)
  334. {
  335. fb_set_suspend(rdev->mode_info.rfbdev->helper.fbdev, state);
  336. }
  337. int radeon_fbdev_total_size(struct radeon_device *rdev)
  338. {
  339. struct radeon_bo *robj;
  340. int size = 0;
  341. robj = rdev->mode_info.rfbdev->rfb.obj->driver_private;
  342. size += radeon_bo_size(robj);
  343. return size;
  344. }
  345. bool radeon_fbdev_robj_is_fb(struct radeon_device *rdev, struct radeon_bo *robj)
  346. {
  347. if (robj == rdev->mode_info.rfbdev->rfb.obj->driver_private)
  348. return true;
  349. return false;
  350. }