radeon_fb.c 9.9 KB

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