nouveau_fbcon.c 15 KB

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