nouveau_fbcon.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 void
  50. nouveau_fbcon_fillrect(struct fb_info *info, const struct fb_fillrect *rect)
  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. int ret;
  56. if (info->state != FBINFO_STATE_RUNNING)
  57. return;
  58. ret = -ENODEV;
  59. if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
  60. mutex_trylock(&dev_priv->channel->mutex)) {
  61. if (dev_priv->card_type < NV_50)
  62. ret = nv04_fbcon_fillrect(info, rect);
  63. else
  64. if (dev_priv->card_type < NV_C0)
  65. ret = nv50_fbcon_fillrect(info, rect);
  66. else
  67. ret = nvc0_fbcon_fillrect(info, rect);
  68. mutex_unlock(&dev_priv->channel->mutex);
  69. }
  70. if (ret == 0)
  71. return;
  72. if (ret != -ENODEV)
  73. nouveau_fbcon_gpu_lockup(info);
  74. cfb_fillrect(info, rect);
  75. }
  76. static void
  77. nouveau_fbcon_copyarea(struct fb_info *info, const struct fb_copyarea *image)
  78. {
  79. struct nouveau_fbdev *nfbdev = info->par;
  80. struct drm_device *dev = nfbdev->dev;
  81. struct drm_nouveau_private *dev_priv = dev->dev_private;
  82. int ret;
  83. if (info->state != FBINFO_STATE_RUNNING)
  84. return;
  85. ret = -ENODEV;
  86. if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
  87. mutex_trylock(&dev_priv->channel->mutex)) {
  88. if (dev_priv->card_type < NV_50)
  89. ret = nv04_fbcon_copyarea(info, image);
  90. else
  91. if (dev_priv->card_type < NV_C0)
  92. ret = nv50_fbcon_copyarea(info, image);
  93. else
  94. ret = nvc0_fbcon_copyarea(info, image);
  95. mutex_unlock(&dev_priv->channel->mutex);
  96. }
  97. if (ret == 0)
  98. return;
  99. if (ret != -ENODEV)
  100. nouveau_fbcon_gpu_lockup(info);
  101. cfb_copyarea(info, image);
  102. }
  103. static void
  104. nouveau_fbcon_imageblit(struct fb_info *info, const struct fb_image *image)
  105. {
  106. struct nouveau_fbdev *nfbdev = info->par;
  107. struct drm_device *dev = nfbdev->dev;
  108. struct drm_nouveau_private *dev_priv = dev->dev_private;
  109. int ret;
  110. if (info->state != FBINFO_STATE_RUNNING)
  111. return;
  112. ret = -ENODEV;
  113. if (!in_interrupt() && !(info->flags & FBINFO_HWACCEL_DISABLED) &&
  114. mutex_trylock(&dev_priv->channel->mutex)) {
  115. if (dev_priv->card_type < NV_50)
  116. ret = nv04_fbcon_imageblit(info, image);
  117. else
  118. if (dev_priv->card_type < NV_C0)
  119. ret = nv50_fbcon_imageblit(info, image);
  120. else
  121. ret = nvc0_fbcon_imageblit(info, image);
  122. mutex_unlock(&dev_priv->channel->mutex);
  123. }
  124. if (ret == 0)
  125. return;
  126. if (ret != -ENODEV)
  127. nouveau_fbcon_gpu_lockup(info);
  128. cfb_imageblit(info, image);
  129. }
  130. static int
  131. nouveau_fbcon_sync(struct fb_info *info)
  132. {
  133. struct nouveau_fbdev *nfbdev = info->par;
  134. struct drm_device *dev = nfbdev->dev;
  135. struct drm_nouveau_private *dev_priv = dev->dev_private;
  136. struct nouveau_channel *chan = dev_priv->channel;
  137. int ret, i;
  138. if (!chan || !chan->accel_done || in_interrupt() ||
  139. info->state != FBINFO_STATE_RUNNING ||
  140. info->flags & FBINFO_HWACCEL_DISABLED)
  141. return 0;
  142. if (!mutex_trylock(&chan->mutex))
  143. return 0;
  144. ret = RING_SPACE(chan, 4);
  145. if (ret) {
  146. mutex_unlock(&chan->mutex);
  147. nouveau_fbcon_gpu_lockup(info);
  148. return 0;
  149. }
  150. if (dev_priv->card_type >= NV_C0) {
  151. BEGIN_NVC0(chan, 2, NvSub2D, 0x010c, 1);
  152. OUT_RING (chan, 0);
  153. BEGIN_NVC0(chan, 2, NvSub2D, 0x0100, 1);
  154. OUT_RING (chan, 0);
  155. } else {
  156. BEGIN_RING(chan, 0, 0x0104, 1);
  157. OUT_RING (chan, 0);
  158. BEGIN_RING(chan, 0, 0x0100, 1);
  159. OUT_RING (chan, 0);
  160. }
  161. nouveau_bo_wr32(chan->notifier_bo, chan->m2mf_ntfy/4 + 3, 0xffffffff);
  162. FIRE_RING(chan);
  163. mutex_unlock(&chan->mutex);
  164. ret = -EBUSY;
  165. for (i = 0; i < 100000; i++) {
  166. if (!nouveau_bo_rd32(chan->notifier_bo, chan->m2mf_ntfy/4 + 3)) {
  167. ret = 0;
  168. break;
  169. }
  170. DRM_UDELAY(1);
  171. }
  172. if (ret) {
  173. nouveau_fbcon_gpu_lockup(info);
  174. return 0;
  175. }
  176. chan->accel_done = false;
  177. return 0;
  178. }
  179. static struct fb_ops nouveau_fbcon_ops = {
  180. .owner = THIS_MODULE,
  181. .fb_check_var = drm_fb_helper_check_var,
  182. .fb_set_par = drm_fb_helper_set_par,
  183. .fb_fillrect = nouveau_fbcon_fillrect,
  184. .fb_copyarea = nouveau_fbcon_copyarea,
  185. .fb_imageblit = nouveau_fbcon_imageblit,
  186. .fb_sync = nouveau_fbcon_sync,
  187. .fb_pan_display = drm_fb_helper_pan_display,
  188. .fb_blank = drm_fb_helper_blank,
  189. .fb_setcmap = drm_fb_helper_setcmap,
  190. .fb_debug_enter = drm_fb_helper_debug_enter,
  191. .fb_debug_leave = drm_fb_helper_debug_leave,
  192. };
  193. static struct fb_ops nouveau_fbcon_sw_ops = {
  194. .owner = THIS_MODULE,
  195. .fb_check_var = drm_fb_helper_check_var,
  196. .fb_set_par = drm_fb_helper_set_par,
  197. .fb_fillrect = cfb_fillrect,
  198. .fb_copyarea = cfb_copyarea,
  199. .fb_imageblit = cfb_imageblit,
  200. .fb_pan_display = drm_fb_helper_pan_display,
  201. .fb_blank = drm_fb_helper_blank,
  202. .fb_setcmap = drm_fb_helper_setcmap,
  203. .fb_debug_enter = drm_fb_helper_debug_enter,
  204. .fb_debug_leave = drm_fb_helper_debug_leave,
  205. };
  206. static void nouveau_fbcon_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  207. u16 blue, int regno)
  208. {
  209. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  210. nv_crtc->lut.r[regno] = red;
  211. nv_crtc->lut.g[regno] = green;
  212. nv_crtc->lut.b[regno] = blue;
  213. }
  214. static void nouveau_fbcon_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  215. u16 *blue, int regno)
  216. {
  217. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  218. *red = nv_crtc->lut.r[regno];
  219. *green = nv_crtc->lut.g[regno];
  220. *blue = nv_crtc->lut.b[regno];
  221. }
  222. static void
  223. nouveau_fbcon_zfill(struct drm_device *dev, struct nouveau_fbdev *nfbdev)
  224. {
  225. struct fb_info *info = nfbdev->helper.fbdev;
  226. struct fb_fillrect rect;
  227. /* Clear the entire fbcon. The drm will program every connector
  228. * with it's preferred mode. If the sizes differ, one display will
  229. * quite likely have garbage around the console.
  230. */
  231. rect.dx = rect.dy = 0;
  232. rect.width = info->var.xres_virtual;
  233. rect.height = info->var.yres_virtual;
  234. rect.color = 0;
  235. rect.rop = ROP_COPY;
  236. info->fbops->fb_fillrect(info, &rect);
  237. }
  238. static int
  239. nouveau_fbcon_create(struct nouveau_fbdev *nfbdev,
  240. struct drm_fb_helper_surface_size *sizes)
  241. {
  242. struct drm_device *dev = nfbdev->dev;
  243. struct drm_nouveau_private *dev_priv = dev->dev_private;
  244. struct fb_info *info;
  245. struct drm_framebuffer *fb;
  246. struct nouveau_framebuffer *nouveau_fb;
  247. struct nouveau_channel *chan;
  248. struct nouveau_bo *nvbo;
  249. struct drm_mode_fb_cmd mode_cmd;
  250. struct pci_dev *pdev = dev->pdev;
  251. struct device *device = &pdev->dev;
  252. int size, ret;
  253. mode_cmd.width = sizes->surface_width;
  254. mode_cmd.height = sizes->surface_height;
  255. mode_cmd.bpp = sizes->surface_bpp;
  256. mode_cmd.pitch = mode_cmd.width * (mode_cmd.bpp >> 3);
  257. mode_cmd.pitch = roundup(mode_cmd.pitch, 256);
  258. mode_cmd.depth = sizes->surface_depth;
  259. size = mode_cmd.pitch * mode_cmd.height;
  260. size = roundup(size, PAGE_SIZE);
  261. ret = nouveau_gem_new(dev, size, 0, NOUVEAU_GEM_DOMAIN_VRAM,
  262. 0, 0x0000, &nvbo);
  263. if (ret) {
  264. NV_ERROR(dev, "failed to allocate framebuffer\n");
  265. goto out;
  266. }
  267. ret = nouveau_bo_pin(nvbo, TTM_PL_FLAG_VRAM);
  268. if (ret) {
  269. NV_ERROR(dev, "failed to pin fb: %d\n", ret);
  270. nouveau_bo_ref(NULL, &nvbo);
  271. goto out;
  272. }
  273. ret = nouveau_bo_map(nvbo);
  274. if (ret) {
  275. NV_ERROR(dev, "failed to map fb: %d\n", ret);
  276. nouveau_bo_unpin(nvbo);
  277. nouveau_bo_ref(NULL, &nvbo);
  278. goto out;
  279. }
  280. chan = nouveau_nofbaccel ? NULL : dev_priv->channel;
  281. if (chan && dev_priv->card_type >= NV_50) {
  282. ret = nouveau_bo_vma_add(nvbo, chan->vm, &nfbdev->nouveau_fb.vma);
  283. if (ret) {
  284. NV_ERROR(dev, "failed to map fb into chan: %d\n", ret);
  285. chan = NULL;
  286. }
  287. }
  288. mutex_lock(&dev->struct_mutex);
  289. info = framebuffer_alloc(0, device);
  290. if (!info) {
  291. ret = -ENOMEM;
  292. goto out_unref;
  293. }
  294. ret = fb_alloc_cmap(&info->cmap, 256, 0);
  295. if (ret) {
  296. ret = -ENOMEM;
  297. goto out_unref;
  298. }
  299. info->par = nfbdev;
  300. nouveau_framebuffer_init(dev, &nfbdev->nouveau_fb, &mode_cmd, nvbo);
  301. nouveau_fb = &nfbdev->nouveau_fb;
  302. fb = &nouveau_fb->base;
  303. /* setup helper */
  304. nfbdev->helper.fb = fb;
  305. nfbdev->helper.fbdev = info;
  306. strcpy(info->fix.id, "nouveaufb");
  307. if (nouveau_nofbaccel)
  308. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_DISABLED;
  309. else
  310. info->flags = FBINFO_DEFAULT | FBINFO_HWACCEL_COPYAREA |
  311. FBINFO_HWACCEL_FILLRECT |
  312. FBINFO_HWACCEL_IMAGEBLIT;
  313. info->flags |= FBINFO_CAN_FORCE_OUTPUT;
  314. info->fbops = &nouveau_fbcon_sw_ops;
  315. info->fix.smem_start = nvbo->bo.mem.bus.base +
  316. nvbo->bo.mem.bus.offset;
  317. info->fix.smem_len = size;
  318. info->screen_base = nvbo_kmap_obj_iovirtual(nouveau_fb->nvbo);
  319. info->screen_size = size;
  320. drm_fb_helper_fill_fix(info, fb->pitch, fb->depth);
  321. drm_fb_helper_fill_var(info, &nfbdev->helper, sizes->fb_width, sizes->fb_height);
  322. /* Set aperture base/size for vesafb takeover */
  323. info->apertures = dev_priv->apertures;
  324. if (!info->apertures) {
  325. ret = -ENOMEM;
  326. goto out_unref;
  327. }
  328. info->pixmap.size = 64*1024;
  329. info->pixmap.buf_align = 8;
  330. info->pixmap.access_align = 32;
  331. info->pixmap.flags = FB_PIXMAP_SYSTEM;
  332. info->pixmap.scan_align = 1;
  333. mutex_unlock(&dev->struct_mutex);
  334. if (dev_priv->channel && !nouveau_nofbaccel) {
  335. ret = -ENODEV;
  336. if (dev_priv->card_type < NV_50)
  337. ret = nv04_fbcon_accel_init(info);
  338. else
  339. if (dev_priv->card_type < NV_C0)
  340. ret = nv50_fbcon_accel_init(info);
  341. else
  342. ret = nvc0_fbcon_accel_init(info);
  343. if (ret == 0)
  344. info->fbops = &nouveau_fbcon_ops;
  345. }
  346. nouveau_fbcon_zfill(dev, nfbdev);
  347. /* To allow resizeing without swapping buffers */
  348. NV_INFO(dev, "allocated %dx%d fb: 0x%lx, bo %p\n",
  349. nouveau_fb->base.width,
  350. nouveau_fb->base.height,
  351. nvbo->bo.offset, nvbo);
  352. vga_switcheroo_client_fb_set(dev->pdev, info);
  353. return 0;
  354. out_unref:
  355. mutex_unlock(&dev->struct_mutex);
  356. out:
  357. return ret;
  358. }
  359. static int
  360. nouveau_fbcon_find_or_create_single(struct drm_fb_helper *helper,
  361. struct drm_fb_helper_surface_size *sizes)
  362. {
  363. struct nouveau_fbdev *nfbdev = (struct nouveau_fbdev *)helper;
  364. int new_fb = 0;
  365. int ret;
  366. if (!helper->fb) {
  367. ret = nouveau_fbcon_create(nfbdev, sizes);
  368. if (ret)
  369. return ret;
  370. new_fb = 1;
  371. }
  372. return new_fb;
  373. }
  374. void
  375. nouveau_fbcon_output_poll_changed(struct drm_device *dev)
  376. {
  377. struct drm_nouveau_private *dev_priv = dev->dev_private;
  378. drm_fb_helper_hotplug_event(&dev_priv->nfbdev->helper);
  379. }
  380. static int
  381. nouveau_fbcon_destroy(struct drm_device *dev, struct nouveau_fbdev *nfbdev)
  382. {
  383. struct nouveau_framebuffer *nouveau_fb = &nfbdev->nouveau_fb;
  384. struct fb_info *info;
  385. if (nfbdev->helper.fbdev) {
  386. info = nfbdev->helper.fbdev;
  387. unregister_framebuffer(info);
  388. if (info->cmap.len)
  389. fb_dealloc_cmap(&info->cmap);
  390. framebuffer_release(info);
  391. }
  392. if (nouveau_fb->nvbo) {
  393. nouveau_bo_unmap(nouveau_fb->nvbo);
  394. nouveau_bo_vma_del(nouveau_fb->nvbo, &nouveau_fb->vma);
  395. drm_gem_object_unreference_unlocked(nouveau_fb->nvbo->gem);
  396. nouveau_fb->nvbo = NULL;
  397. }
  398. drm_fb_helper_fini(&nfbdev->helper);
  399. drm_framebuffer_cleanup(&nouveau_fb->base);
  400. return 0;
  401. }
  402. void nouveau_fbcon_gpu_lockup(struct fb_info *info)
  403. {
  404. struct nouveau_fbdev *nfbdev = info->par;
  405. struct drm_device *dev = nfbdev->dev;
  406. NV_ERROR(dev, "GPU lockup - switching to software fbcon\n");
  407. info->flags |= FBINFO_HWACCEL_DISABLED;
  408. }
  409. static struct drm_fb_helper_funcs nouveau_fbcon_helper_funcs = {
  410. .gamma_set = nouveau_fbcon_gamma_set,
  411. .gamma_get = nouveau_fbcon_gamma_get,
  412. .fb_probe = nouveau_fbcon_find_or_create_single,
  413. };
  414. int nouveau_fbcon_init(struct drm_device *dev)
  415. {
  416. struct drm_nouveau_private *dev_priv = dev->dev_private;
  417. struct nouveau_fbdev *nfbdev;
  418. int ret;
  419. nfbdev = kzalloc(sizeof(struct nouveau_fbdev), GFP_KERNEL);
  420. if (!nfbdev)
  421. return -ENOMEM;
  422. nfbdev->dev = dev;
  423. dev_priv->nfbdev = nfbdev;
  424. nfbdev->helper.funcs = &nouveau_fbcon_helper_funcs;
  425. ret = drm_fb_helper_init(dev, &nfbdev->helper,
  426. nv_two_heads(dev) ? 2 : 1, 4);
  427. if (ret) {
  428. kfree(nfbdev);
  429. return ret;
  430. }
  431. drm_fb_helper_single_add_all_connectors(&nfbdev->helper);
  432. drm_fb_helper_initial_config(&nfbdev->helper, 32);
  433. return 0;
  434. }
  435. void nouveau_fbcon_fini(struct drm_device *dev)
  436. {
  437. struct drm_nouveau_private *dev_priv = dev->dev_private;
  438. if (!dev_priv->nfbdev)
  439. return;
  440. nouveau_fbcon_destroy(dev, dev_priv->nfbdev);
  441. kfree(dev_priv->nfbdev);
  442. dev_priv->nfbdev = NULL;
  443. }
  444. void nouveau_fbcon_save_disable_accel(struct drm_device *dev)
  445. {
  446. struct drm_nouveau_private *dev_priv = dev->dev_private;
  447. dev_priv->nfbdev->saved_flags = dev_priv->nfbdev->helper.fbdev->flags;
  448. dev_priv->nfbdev->helper.fbdev->flags |= FBINFO_HWACCEL_DISABLED;
  449. }
  450. void nouveau_fbcon_restore_accel(struct drm_device *dev)
  451. {
  452. struct drm_nouveau_private *dev_priv = dev->dev_private;
  453. dev_priv->nfbdev->helper.fbdev->flags = dev_priv->nfbdev->saved_flags;
  454. }
  455. void nouveau_fbcon_set_suspend(struct drm_device *dev, int state)
  456. {
  457. struct drm_nouveau_private *dev_priv = dev->dev_private;
  458. fb_set_suspend(dev_priv->nfbdev->helper.fbdev, state);
  459. }
  460. void nouveau_fbcon_zfill_all(struct drm_device *dev)
  461. {
  462. struct drm_nouveau_private *dev_priv = dev->dev_private;
  463. nouveau_fbcon_zfill(dev, dev_priv->nfbdev);
  464. }