nouveau_fbcon.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/kernel.h>
  28. #include <linux/errno.h>
  29. #include <linux/string.h>
  30. #include <linux/mm.h>
  31. #include <linux/tty.h>
  32. #include <linux/sysrq.h>
  33. #include <linux/delay.h>
  34. #include <linux/fb.h>
  35. #include <linux/init.h>
  36. #include <linux/screen_info.h>
  37. #include <linux/vga_switcheroo.h>
  38. #include "drmP.h"
  39. #include "drm.h"
  40. #include "drm_crtc.h"
  41. #include "drm_crtc_helper.h"
  42. #include "drm_fb_helper.h"
  43. #include "nouveau_drv.h"
  44. #include "nouveau_drm.h"
  45. #include "nouveau_crtc.h"
  46. #include "nouveau_fb.h"
  47. #include "nouveau_fbcon.h"
  48. #include "nouveau_dma.h"
  49. static int
  50. nouveau_fbcon_sync(struct fb_info *info)
  51. {
  52. struct nouveau_fbdev *nfbdev = info->par;
  53. struct drm_device *dev = nfbdev->dev;
  54. struct drm_nouveau_private *dev_priv = dev->dev_private;
  55. struct nouveau_channel *chan = dev_priv->channel;
  56. int ret, i;
  57. if (!chan || !chan->accel_done ||
  58. info->state != FBINFO_STATE_RUNNING ||
  59. info->flags & FBINFO_HWACCEL_DISABLED)
  60. return 0;
  61. if (RING_SPACE(chan, 4)) {
  62. nouveau_fbcon_gpu_lockup(info);
  63. return 0;
  64. }
  65. BEGIN_RING(chan, 0, 0x0104, 1);
  66. OUT_RING(chan, 0);
  67. BEGIN_RING(chan, 0, 0x0100, 1);
  68. OUT_RING(chan, 0);
  69. nouveau_bo_wr32(chan->notifier_bo, chan->m2mf_ntfy + 3, 0xffffffff);
  70. FIRE_RING(chan);
  71. ret = -EBUSY;
  72. for (i = 0; i < 100000; i++) {
  73. if (!nouveau_bo_rd32(chan->notifier_bo, chan->m2mf_ntfy + 3)) {
  74. ret = 0;
  75. break;
  76. }
  77. DRM_UDELAY(1);
  78. }
  79. if (ret) {
  80. nouveau_fbcon_gpu_lockup(info);
  81. return 0;
  82. }
  83. chan->accel_done = false;
  84. return 0;
  85. }
  86. static struct fb_ops nouveau_fbcon_ops = {
  87. .owner = THIS_MODULE,
  88. .fb_check_var = drm_fb_helper_check_var,
  89. .fb_set_par = drm_fb_helper_set_par,
  90. .fb_fillrect = cfb_fillrect,
  91. .fb_copyarea = cfb_copyarea,
  92. .fb_imageblit = cfb_imageblit,
  93. .fb_sync = nouveau_fbcon_sync,
  94. .fb_pan_display = drm_fb_helper_pan_display,
  95. .fb_blank = drm_fb_helper_blank,
  96. .fb_setcmap = drm_fb_helper_setcmap,
  97. .fb_debug_enter = drm_fb_helper_debug_enter,
  98. .fb_debug_leave = drm_fb_helper_debug_leave,
  99. };
  100. static struct fb_ops nv04_fbcon_ops = {
  101. .owner = THIS_MODULE,
  102. .fb_check_var = drm_fb_helper_check_var,
  103. .fb_set_par = drm_fb_helper_set_par,
  104. .fb_fillrect = nv04_fbcon_fillrect,
  105. .fb_copyarea = nv04_fbcon_copyarea,
  106. .fb_imageblit = nv04_fbcon_imageblit,
  107. .fb_sync = nouveau_fbcon_sync,
  108. .fb_pan_display = drm_fb_helper_pan_display,
  109. .fb_blank = drm_fb_helper_blank,
  110. .fb_setcmap = drm_fb_helper_setcmap,
  111. .fb_debug_enter = drm_fb_helper_debug_enter,
  112. .fb_debug_leave = drm_fb_helper_debug_leave,
  113. };
  114. static struct fb_ops nv50_fbcon_ops = {
  115. .owner = THIS_MODULE,
  116. .fb_check_var = drm_fb_helper_check_var,
  117. .fb_set_par = drm_fb_helper_set_par,
  118. .fb_fillrect = nv50_fbcon_fillrect,
  119. .fb_copyarea = nv50_fbcon_copyarea,
  120. .fb_imageblit = nv50_fbcon_imageblit,
  121. .fb_sync = nouveau_fbcon_sync,
  122. .fb_pan_display = drm_fb_helper_pan_display,
  123. .fb_blank = drm_fb_helper_blank,
  124. .fb_setcmap = drm_fb_helper_setcmap,
  125. .fb_debug_enter = drm_fb_helper_debug_enter,
  126. .fb_debug_leave = drm_fb_helper_debug_leave,
  127. };
  128. static void nouveau_fbcon_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  129. u16 blue, int regno)
  130. {
  131. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  132. nv_crtc->lut.r[regno] = red;
  133. nv_crtc->lut.g[regno] = green;
  134. nv_crtc->lut.b[regno] = blue;
  135. }
  136. static void nouveau_fbcon_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  137. u16 *blue, int regno)
  138. {
  139. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  140. *red = nv_crtc->lut.r[regno];
  141. *green = nv_crtc->lut.g[regno];
  142. *blue = nv_crtc->lut.b[regno];
  143. }
  144. static void
  145. nouveau_fbcon_zfill(struct drm_device *dev, struct nouveau_fbdev *nfbdev)
  146. {
  147. struct fb_info *info = nfbdev->helper.fbdev;
  148. struct fb_fillrect rect;
  149. /* Clear the entire fbcon. The drm will program every connector
  150. * with it's preferred mode. If the sizes differ, one display will
  151. * quite likely have garbage around the console.
  152. */
  153. rect.dx = rect.dy = 0;
  154. rect.width = info->var.xres_virtual;
  155. rect.height = info->var.yres_virtual;
  156. rect.color = 0;
  157. rect.rop = ROP_COPY;
  158. info->fbops->fb_fillrect(info, &rect);
  159. }
  160. static int
  161. nouveau_fbcon_create(struct nouveau_fbdev *nfbdev,
  162. struct drm_fb_helper_surface_size *sizes)
  163. {
  164. struct drm_device *dev = nfbdev->dev;
  165. struct drm_nouveau_private *dev_priv = dev->dev_private;
  166. struct fb_info *info;
  167. struct drm_framebuffer *fb;
  168. struct nouveau_framebuffer *nouveau_fb;
  169. struct nouveau_bo *nvbo;
  170. struct drm_mode_fb_cmd mode_cmd;
  171. struct pci_dev *pdev = dev->pdev;
  172. struct device *device = &pdev->dev;
  173. int size, ret;
  174. mode_cmd.width = sizes->surface_width;
  175. mode_cmd.height = sizes->surface_height;
  176. mode_cmd.bpp = sizes->surface_bpp;
  177. mode_cmd.pitch = mode_cmd.width * (mode_cmd.bpp >> 3);
  178. mode_cmd.pitch = roundup(mode_cmd.pitch, 256);
  179. mode_cmd.depth = sizes->surface_depth;
  180. size = mode_cmd.pitch * mode_cmd.height;
  181. size = roundup(size, PAGE_SIZE);
  182. ret = nouveau_gem_new(dev, dev_priv->channel, size, 0, TTM_PL_FLAG_VRAM,
  183. 0, 0x0000, false, true, &nvbo);
  184. if (ret) {
  185. NV_ERROR(dev, "failed to allocate framebuffer\n");
  186. goto out;
  187. }
  188. ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM);
  189. if (ret) {
  190. NV_ERROR(dev, "failed to pin fb: %d\n", ret);
  191. nouveau_bo_ref(NULL, &nvbo);
  192. goto out;
  193. }
  194. ret = nouveau_bo_map(nvbo);
  195. if (ret) {
  196. NV_ERROR(dev, "failed to map fb: %d\n", ret);
  197. nouveau_bo_unpin(nvbo);
  198. nouveau_bo_ref(NULL, &nvbo);
  199. goto out;
  200. }
  201. mutex_lock(&dev->struct_mutex);
  202. info = framebuffer_alloc(0, device);
  203. if (!info) {
  204. ret = -ENOMEM;
  205. goto out_unref;
  206. }
  207. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  208. if (ret) {
  209. ret = -ENOMEM;
  210. goto out_unref;
  211. }
  212. info->par = nfbdev;
  213. nouveau_framebuffer_init(dev, &nfbdev->nouveau_fb, &mode_cmd, nvbo);
  214. nouveau_fb = &nfbdev->nouveau_fb;
  215. fb = &nouveau_fb->base;
  216. /* setup helper */
  217. nfbdev->helper.fb = fb;
  218. nfbdev->helper.fbdev = info;
  219. strcpy(info->fix.id, "nouveaufb");
  220. if (nouveau_nofbaccel)
  221. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_DISABLED;
  222. else
  223. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
  224. FBINFO_HWACCEL_FILLRECT |
  225. FBINFO_HWACCEL_IMAGEBLIT;
  226. info->flags |= FBINFO_CAN_FORCE_OUTPUT;
  227. info->fbops = &nouveau_fbcon_ops;
  228. info->fix.smem_start = dev->mode_config.fb_base + nvbo->bo.offset -
  229. dev_priv->vm_vram_base;
  230. info->fix.smem_len = size;
  231. info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo);
  232. info->screen_size = size;
  233. drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
  234. drm_fb_helper_fill_var(info, &nfbdev->helper, sizes->fb_width, sizes->fb_height);
  235. /* FIXME: we really shouldn't expose mmio space at all */
  236. info->fix.mmio_start = pci_resource_start(pdev, 1);
  237. info->fix.mmio_len = pci_resource_len(pdev, 1);
  238. /* Set aperture base/size for vesafb takeover */
  239. info->apertures = dev_priv->apertures;
  240. if (!info->apertures) {
  241. ret = -ENOMEM;
  242. goto out_unref;
  243. }
  244. info->pixmap.size = 64*1024;
  245. info->pixmap.buf_align = 8;
  246. info->pixmap.access_align = 32;
  247. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  248. info->pixmap.scan_align = 1;
  249. if (dev_priv->channel && !nouveau_nofbaccel) {
  250. switch (dev_priv->card_type) {
  251. case NV_C0:
  252. break;
  253. case NV_50:
  254. nv50_fbcon_accel_init(info);
  255. info->fbops = &nv50_fbcon_ops;
  256. break;
  257. default:
  258. nv04_fbcon_accel_init(info);
  259. info->fbops = &nv04_fbcon_ops;
  260. break;
  261. };
  262. }
  263. nouveau_fbcon_zfill(dev, nfbdev);
  264. /* To allow resizeing without swapping buffers */
  265. NV_INFO(dev, "allocated %dx%d fb: 0x%lx, bo %p\n",
  266. nouveau_fb->base.width,
  267. nouveau_fb->base.height,
  268. nvbo->bo.offset, nvbo);
  269. mutex_unlock(&dev->struct_mutex);
  270. vga_switcheroo_client_fb_set(dev->pdev, info);
  271. return 0;
  272. out_unref:
  273. mutex_unlock(&dev->struct_mutex);
  274. out:
  275. return ret;
  276. }
  277. static int
  278. nouveau_fbcon_find_or_create_single(struct drm_fb_helper *helper,
  279. struct drm_fb_helper_surface_size *sizes)
  280. {
  281. struct nouveau_fbdev *nfbdev = (struct nouveau_fbdev *)helper;
  282. int new_fb = 0;
  283. int ret;
  284. if (!helper->fb) {
  285. ret = nouveau_fbcon_create(nfbdev, sizes);
  286. if (ret)
  287. return ret;
  288. new_fb = 1;
  289. }
  290. return new_fb;
  291. }
  292. void
  293. nouveau_fbcon_output_poll_changed(struct drm_device *dev)
  294. {
  295. struct drm_nouveau_private *dev_priv = dev->dev_private;
  296. drm_fb_helper_hotplug_event(&dev_priv->nfbdev->helper);
  297. }
  298. static int
  299. nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *nfbdev)
  300. {
  301. struct nouveau_framebuffer *nouveau_fb = &nfbdev->nouveau_fb;
  302. struct fb_info *info;
  303. if (nfbdev->helper.fbdev) {
  304. info = nfbdev->helper.fbdev;
  305. unregister_framebuffer(info);
  306. if (info->cmap.len)
  307. fb_dealloc_cmap(&info->cmap);
  308. framebuffer_release(info);
  309. }
  310. if (nouveau_fb->nvbo) {
  311. nouveau_bo_unmap(nouveau_fb->nvbo);
  312. drm_gem_object_unreference_unlocked(nouveau_fb->nvbo->gem);
  313. nouveau_fb->nvbo = NULL;
  314. }
  315. drm_fb_helper_fini(&nfbdev->helper);
  316. drm_framebuffer_cleanup(&nouveau_fb->base);
  317. return 0;
  318. }
  319. void nouveau_fbcon_gpu_lockup(struct fb_info *info)
  320. {
  321. struct nouveau_fbdev *nfbdev = info->par;
  322. struct drm_device *dev = nfbdev->dev;
  323. NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
  324. info->flags |= FBINFO_HWACCEL_DISABLED;
  325. }
  326. static struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
  327. .gamma_set = nouveau_fbcon_gamma_set,
  328. .gamma_get = nouveau_fbcon_gamma_get,
  329. .fb_probe = nouveau_fbcon_find_or_create_single,
  330. };
  331. int nouveau_fbcon_init(struct drm_device *dev)
  332. {
  333. struct drm_nouveau_private *dev_priv = dev->dev_private;
  334. struct nouveau_fbdev *nfbdev;
  335. int ret;
  336. nfbdev = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);
  337. if (!nfbdev)
  338. return -ENOMEM;
  339. nfbdev->dev = dev;
  340. dev_priv->nfbdev = nfbdev;
  341. nfbdev->helper.funcs = &nouveau_fbcon_helper_funcs;
  342. ret = drm_fb_helper_init(dev, &nfbdev->helper,
  343. nv_two_heads(dev) ? 2 : 1, 4);
  344. if (ret) {
  345. kfree(nfbdev);
  346. return ret;
  347. }
  348. drm_fb_helper_single_add_all_connectors(&nfbdev->helper);
  349. drm_fb_helper_initial_config(&nfbdev->helper, 32);
  350. return 0;
  351. }
  352. void nouveau_fbcon_fini(struct drm_device *dev)
  353. {
  354. struct drm_nouveau_private *dev_priv = dev->dev_private;
  355. if (!dev_priv->nfbdev)
  356. return;
  357. nouveau_fbcon_destroy(dev, dev_priv->nfbdev);
  358. kfree(dev_priv->nfbdev);
  359. dev_priv->nfbdev = NULL;
  360. }
  361. void nouveau_fbcon_save_disable_accel(struct drm_device *dev)
  362. {
  363. struct drm_nouveau_private *dev_priv = dev->dev_private;
  364. dev_priv->nfbdev->saved_flags = dev_priv->nfbdev->helper.fbdev->flags;
  365. dev_priv->nfbdev->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
  366. }
  367. void nouveau_fbcon_restore_accel(struct drm_device *dev)
  368. {
  369. struct drm_nouveau_private *dev_priv = dev->dev_private;
  370. dev_priv->nfbdev->helper.fbdev->flags = dev_priv->nfbdev->saved_flags;
  371. }
  372. void nouveau_fbcon_set_suspend(struct drm_device *dev, int state)
  373. {
  374. struct drm_nouveau_private *dev_priv = dev->dev_private;
  375. fb_set_suspend(dev_priv->nfbdev->helper.fbdev, state);
  376. }
  377. void nouveau_fbcon_zfill_all(struct drm_device *dev)
  378. {
  379. struct drm_nouveau_private *dev_priv = dev->dev_private;
  380. nouveau_fbcon_zfill(dev, dev_priv->nfbdev);
  381. }