nouveau_fbcon.c 13 KB

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