nouveau_display.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. /*
  2. * Copyright (C) 2008 Maarten Maathuis.
  3. * All Rights Reserved.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice (including the
  14. * next paragraph) shall be included in all copies or substantial
  15. * portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. *
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/drm_crtc_helper.h>
  28. #include <nvif/class.h>
  29. #include "nouveau_fbcon.h"
  30. #include "dispnv04/hw.h"
  31. #include "nouveau_crtc.h"
  32. #include "nouveau_dma.h"
  33. #include "nouveau_gem.h"
  34. #include "nouveau_connector.h"
  35. #include "nv50_display.h"
  36. #include "nouveau_fence.h"
  37. #include <nvif/cl0046.h>
  38. #include <nvif/event.h>
  39. static int
  40. nouveau_display_vblank_handler(struct nvif_notify *notify)
  41. {
  42. struct nouveau_crtc *nv_crtc =
  43. container_of(notify, typeof(*nv_crtc), vblank);
  44. drm_crtc_handle_vblank(&nv_crtc->base);
  45. return NVIF_NOTIFY_KEEP;
  46. }
  47. int
  48. nouveau_display_vblank_enable(struct drm_device *dev, unsigned int pipe)
  49. {
  50. struct drm_crtc *crtc;
  51. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  52. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  53. if (nv_crtc->index == pipe) {
  54. nvif_notify_get(&nv_crtc->vblank);
  55. return 0;
  56. }
  57. }
  58. return -EINVAL;
  59. }
  60. void
  61. nouveau_display_vblank_disable(struct drm_device *dev, unsigned int pipe)
  62. {
  63. struct drm_crtc *crtc;
  64. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  65. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  66. if (nv_crtc->index == pipe) {
  67. nvif_notify_put(&nv_crtc->vblank);
  68. return;
  69. }
  70. }
  71. }
  72. static inline int
  73. calc(int blanks, int blanke, int total, int line)
  74. {
  75. if (blanke >= blanks) {
  76. if (line >= blanks)
  77. line -= total;
  78. } else {
  79. if (line >= blanks)
  80. line -= total;
  81. line -= blanke + 1;
  82. }
  83. return line;
  84. }
  85. int
  86. nouveau_display_scanoutpos_head(struct drm_crtc *crtc, int *vpos, int *hpos,
  87. ktime_t *stime, ktime_t *etime)
  88. {
  89. struct {
  90. struct nv04_disp_mthd_v0 base;
  91. struct nv04_disp_scanoutpos_v0 scan;
  92. } args = {
  93. .base.method = NV04_DISP_SCANOUTPOS,
  94. .base.head = nouveau_crtc(crtc)->index,
  95. };
  96. struct nouveau_display *disp = nouveau_display(crtc->dev);
  97. struct drm_vblank_crtc *vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
  98. int ret, retry = 1;
  99. do {
  100. ret = nvif_mthd(&disp->disp, 0, &args, sizeof(args));
  101. if (ret != 0)
  102. return 0;
  103. if (args.scan.vline) {
  104. ret |= DRM_SCANOUTPOS_ACCURATE;
  105. ret |= DRM_SCANOUTPOS_VALID;
  106. break;
  107. }
  108. if (retry) ndelay(vblank->linedur_ns);
  109. } while (retry--);
  110. *hpos = args.scan.hline;
  111. *vpos = calc(args.scan.vblanks, args.scan.vblanke,
  112. args.scan.vtotal, args.scan.vline);
  113. if (stime) *stime = ns_to_ktime(args.scan.time[0]);
  114. if (etime) *etime = ns_to_ktime(args.scan.time[1]);
  115. if (*vpos < 0)
  116. ret |= DRM_SCANOUTPOS_IN_VBLANK;
  117. return ret;
  118. }
  119. int
  120. nouveau_display_scanoutpos(struct drm_device *dev, unsigned int pipe,
  121. unsigned int flags, int *vpos, int *hpos,
  122. ktime_t *stime, ktime_t *etime,
  123. const struct drm_display_mode *mode)
  124. {
  125. struct drm_crtc *crtc;
  126. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  127. if (nouveau_crtc(crtc)->index == pipe) {
  128. return nouveau_display_scanoutpos_head(crtc, vpos, hpos,
  129. stime, etime);
  130. }
  131. }
  132. return 0;
  133. }
  134. int
  135. nouveau_display_vblstamp(struct drm_device *dev, unsigned int pipe,
  136. int *max_error, struct timeval *time, unsigned flags)
  137. {
  138. struct drm_crtc *crtc;
  139. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  140. if (nouveau_crtc(crtc)->index == pipe) {
  141. return drm_calc_vbltimestamp_from_scanoutpos(dev,
  142. pipe, max_error, time, flags,
  143. &crtc->hwmode);
  144. }
  145. }
  146. return -EINVAL;
  147. }
  148. static void
  149. nouveau_display_vblank_fini(struct drm_device *dev)
  150. {
  151. struct drm_crtc *crtc;
  152. drm_vblank_cleanup(dev);
  153. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  154. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  155. nvif_notify_fini(&nv_crtc->vblank);
  156. }
  157. }
  158. static int
  159. nouveau_display_vblank_init(struct drm_device *dev)
  160. {
  161. struct nouveau_display *disp = nouveau_display(dev);
  162. struct drm_crtc *crtc;
  163. int ret;
  164. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  165. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  166. ret = nvif_notify_init(&disp->disp,
  167. nouveau_display_vblank_handler, false,
  168. NV04_DISP_NTFY_VBLANK,
  169. &(struct nvif_notify_head_req_v0) {
  170. .head = nv_crtc->index,
  171. },
  172. sizeof(struct nvif_notify_head_req_v0),
  173. sizeof(struct nvif_notify_head_rep_v0),
  174. &nv_crtc->vblank);
  175. if (ret) {
  176. nouveau_display_vblank_fini(dev);
  177. return ret;
  178. }
  179. }
  180. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  181. if (ret) {
  182. nouveau_display_vblank_fini(dev);
  183. return ret;
  184. }
  185. return 0;
  186. }
  187. static void
  188. nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
  189. {
  190. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  191. struct nouveau_display *disp = nouveau_display(drm_fb->dev);
  192. if (disp->fb_dtor)
  193. disp->fb_dtor(drm_fb);
  194. if (fb->nvbo)
  195. drm_gem_object_unreference_unlocked(&fb->nvbo->gem);
  196. drm_framebuffer_cleanup(drm_fb);
  197. kfree(fb);
  198. }
  199. static int
  200. nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
  201. struct drm_file *file_priv,
  202. unsigned int *handle)
  203. {
  204. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  205. return drm_gem_handle_create(file_priv, &fb->nvbo->gem, handle);
  206. }
  207. static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
  208. .destroy = nouveau_user_framebuffer_destroy,
  209. .create_handle = nouveau_user_framebuffer_create_handle,
  210. };
  211. int
  212. nouveau_framebuffer_init(struct drm_device *dev,
  213. struct nouveau_framebuffer *nv_fb,
  214. const struct drm_mode_fb_cmd2 *mode_cmd,
  215. struct nouveau_bo *nvbo)
  216. {
  217. struct nouveau_display *disp = nouveau_display(dev);
  218. struct drm_framebuffer *fb = &nv_fb->base;
  219. int ret;
  220. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  221. nv_fb->nvbo = nvbo;
  222. ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
  223. if (ret)
  224. return ret;
  225. if (disp->fb_ctor) {
  226. ret = disp->fb_ctor(fb);
  227. if (ret)
  228. disp->fb_dtor(fb);
  229. }
  230. return ret;
  231. }
  232. static struct drm_framebuffer *
  233. nouveau_user_framebuffer_create(struct drm_device *dev,
  234. struct drm_file *file_priv,
  235. const struct drm_mode_fb_cmd2 *mode_cmd)
  236. {
  237. struct nouveau_framebuffer *nouveau_fb;
  238. struct drm_gem_object *gem;
  239. int ret = -ENOMEM;
  240. gem = drm_gem_object_lookup(file_priv, mode_cmd->handles[0]);
  241. if (!gem)
  242. return ERR_PTR(-ENOENT);
  243. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  244. if (!nouveau_fb)
  245. goto err_unref;
  246. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  247. if (ret)
  248. goto err;
  249. return &nouveau_fb->base;
  250. err:
  251. kfree(nouveau_fb);
  252. err_unref:
  253. drm_gem_object_unreference_unlocked(gem);
  254. return ERR_PTR(ret);
  255. }
  256. static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  257. .fb_create = nouveau_user_framebuffer_create,
  258. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  259. };
  260. struct nouveau_drm_prop_enum_list {
  261. u8 gen_mask;
  262. int type;
  263. char *name;
  264. };
  265. static struct nouveau_drm_prop_enum_list underscan[] = {
  266. { 6, UNDERSCAN_AUTO, "auto" },
  267. { 6, UNDERSCAN_OFF, "off" },
  268. { 6, UNDERSCAN_ON, "on" },
  269. {}
  270. };
  271. static struct nouveau_drm_prop_enum_list dither_mode[] = {
  272. { 7, DITHERING_MODE_AUTO, "auto" },
  273. { 7, DITHERING_MODE_OFF, "off" },
  274. { 1, DITHERING_MODE_ON, "on" },
  275. { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
  276. { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
  277. { 4, DITHERING_MODE_TEMPORAL, "temporal" },
  278. {}
  279. };
  280. static struct nouveau_drm_prop_enum_list dither_depth[] = {
  281. { 6, DITHERING_DEPTH_AUTO, "auto" },
  282. { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
  283. { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
  284. {}
  285. };
  286. #define PROP_ENUM(p,gen,n,list) do { \
  287. struct nouveau_drm_prop_enum_list *l = (list); \
  288. int c = 0; \
  289. while (l->gen_mask) { \
  290. if (l->gen_mask & (1 << (gen))) \
  291. c++; \
  292. l++; \
  293. } \
  294. if (c) { \
  295. p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
  296. l = (list); \
  297. c = 0; \
  298. while (p && l->gen_mask) { \
  299. if (l->gen_mask & (1 << (gen))) { \
  300. drm_property_add_enum(p, c, l->type, l->name); \
  301. c++; \
  302. } \
  303. l++; \
  304. } \
  305. } \
  306. } while(0)
  307. int
  308. nouveau_display_init(struct drm_device *dev)
  309. {
  310. struct nouveau_display *disp = nouveau_display(dev);
  311. struct nouveau_drm *drm = nouveau_drm(dev);
  312. struct drm_connector *connector;
  313. int ret;
  314. ret = disp->init(dev);
  315. if (ret)
  316. return ret;
  317. /* enable polling for external displays */
  318. drm_kms_helper_poll_enable(dev);
  319. /* enable hotplug interrupts */
  320. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  321. struct nouveau_connector *conn = nouveau_connector(connector);
  322. nvif_notify_get(&conn->hpd);
  323. }
  324. /* enable flip completion events */
  325. nvif_notify_get(&drm->flip);
  326. return ret;
  327. }
  328. void
  329. nouveau_display_fini(struct drm_device *dev)
  330. {
  331. struct nouveau_display *disp = nouveau_display(dev);
  332. struct nouveau_drm *drm = nouveau_drm(dev);
  333. struct drm_connector *connector;
  334. int head;
  335. /* Make sure that drm and hw vblank irqs get properly disabled. */
  336. for (head = 0; head < dev->mode_config.num_crtc; head++)
  337. drm_vblank_off(dev, head);
  338. /* disable flip completion events */
  339. nvif_notify_put(&drm->flip);
  340. /* disable hotplug interrupts */
  341. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  342. struct nouveau_connector *conn = nouveau_connector(connector);
  343. nvif_notify_put(&conn->hpd);
  344. }
  345. drm_kms_helper_poll_disable(dev);
  346. disp->fini(dev);
  347. }
  348. static void
  349. nouveau_display_create_properties(struct drm_device *dev)
  350. {
  351. struct nouveau_display *disp = nouveau_display(dev);
  352. int gen;
  353. if (disp->disp.oclass < NV50_DISP)
  354. gen = 0;
  355. else
  356. if (disp->disp.oclass < GF110_DISP)
  357. gen = 1;
  358. else
  359. gen = 2;
  360. PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
  361. PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
  362. PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
  363. disp->underscan_hborder_property =
  364. drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
  365. disp->underscan_vborder_property =
  366. drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
  367. if (gen < 1)
  368. return;
  369. /* -90..+90 */
  370. disp->vibrant_hue_property =
  371. drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
  372. /* -100..+100 */
  373. disp->color_vibrance_property =
  374. drm_property_create_range(dev, 0, "color vibrance", 0, 200);
  375. }
  376. int
  377. nouveau_display_create(struct drm_device *dev)
  378. {
  379. struct nouveau_drm *drm = nouveau_drm(dev);
  380. struct nvkm_device *device = nvxx_device(&drm->device);
  381. struct nouveau_display *disp;
  382. int ret;
  383. disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
  384. if (!disp)
  385. return -ENOMEM;
  386. drm_mode_config_init(dev);
  387. drm_mode_create_scaling_mode_property(dev);
  388. drm_mode_create_dvi_i_properties(dev);
  389. dev->mode_config.funcs = &nouveau_mode_config_funcs;
  390. dev->mode_config.fb_base = device->func->resource_addr(device, 1);
  391. dev->mode_config.min_width = 0;
  392. dev->mode_config.min_height = 0;
  393. if (drm->device.info.family < NV_DEVICE_INFO_V0_CELSIUS) {
  394. dev->mode_config.max_width = 2048;
  395. dev->mode_config.max_height = 2048;
  396. } else
  397. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  398. dev->mode_config.max_width = 4096;
  399. dev->mode_config.max_height = 4096;
  400. } else
  401. if (drm->device.info.family < NV_DEVICE_INFO_V0_FERMI) {
  402. dev->mode_config.max_width = 8192;
  403. dev->mode_config.max_height = 8192;
  404. } else {
  405. dev->mode_config.max_width = 16384;
  406. dev->mode_config.max_height = 16384;
  407. }
  408. dev->mode_config.preferred_depth = 24;
  409. dev->mode_config.prefer_shadow = 1;
  410. if (drm->device.info.chipset < 0x11)
  411. dev->mode_config.async_page_flip = false;
  412. else
  413. dev->mode_config.async_page_flip = true;
  414. drm_kms_helper_poll_init(dev);
  415. drm_kms_helper_poll_disable(dev);
  416. if (nouveau_modeset != 2 && drm->vbios.dcb.entries) {
  417. static const u16 oclass[] = {
  418. GP104_DISP,
  419. GP100_DISP,
  420. GM200_DISP,
  421. GM107_DISP,
  422. GK110_DISP,
  423. GK104_DISP,
  424. GF110_DISP,
  425. GT214_DISP,
  426. GT206_DISP,
  427. GT200_DISP,
  428. G82_DISP,
  429. NV50_DISP,
  430. NV04_DISP,
  431. };
  432. int i;
  433. for (i = 0, ret = -ENODEV; ret && i < ARRAY_SIZE(oclass); i++) {
  434. ret = nvif_object_init(&drm->device.object, 0,
  435. oclass[i], NULL, 0, &disp->disp);
  436. }
  437. if (ret == 0) {
  438. nouveau_display_create_properties(dev);
  439. if (disp->disp.oclass < NV50_DISP)
  440. ret = nv04_display_create(dev);
  441. else
  442. ret = nv50_display_create(dev);
  443. }
  444. } else {
  445. ret = 0;
  446. }
  447. if (ret)
  448. goto disp_create_err;
  449. if (dev->mode_config.num_crtc) {
  450. ret = nouveau_display_vblank_init(dev);
  451. if (ret)
  452. goto vblank_err;
  453. }
  454. nouveau_backlight_init(dev);
  455. return 0;
  456. vblank_err:
  457. disp->dtor(dev);
  458. disp_create_err:
  459. drm_kms_helper_poll_fini(dev);
  460. drm_mode_config_cleanup(dev);
  461. return ret;
  462. }
  463. void
  464. nouveau_display_destroy(struct drm_device *dev)
  465. {
  466. struct nouveau_display *disp = nouveau_display(dev);
  467. nouveau_backlight_exit(dev);
  468. nouveau_display_vblank_fini(dev);
  469. drm_kms_helper_poll_fini(dev);
  470. drm_crtc_force_disable_all(dev);
  471. drm_mode_config_cleanup(dev);
  472. if (disp->dtor)
  473. disp->dtor(dev);
  474. nvif_object_fini(&disp->disp);
  475. nouveau_drm(dev)->display = NULL;
  476. kfree(disp);
  477. }
  478. int
  479. nouveau_display_suspend(struct drm_device *dev, bool runtime)
  480. {
  481. struct drm_crtc *crtc;
  482. nouveau_display_fini(dev);
  483. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  484. struct nouveau_framebuffer *nouveau_fb;
  485. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  486. if (!nouveau_fb || !nouveau_fb->nvbo)
  487. continue;
  488. nouveau_bo_unpin(nouveau_fb->nvbo);
  489. }
  490. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  491. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  492. if (nv_crtc->cursor.nvbo) {
  493. if (nv_crtc->cursor.set_offset)
  494. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  495. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  496. }
  497. }
  498. return 0;
  499. }
  500. void
  501. nouveau_display_resume(struct drm_device *dev, bool runtime)
  502. {
  503. struct nouveau_drm *drm = nouveau_drm(dev);
  504. struct drm_crtc *crtc;
  505. int ret, head;
  506. /* re-pin fb/cursors */
  507. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  508. struct nouveau_framebuffer *nouveau_fb;
  509. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  510. if (!nouveau_fb || !nouveau_fb->nvbo)
  511. continue;
  512. ret = nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM, true);
  513. if (ret)
  514. NV_ERROR(drm, "Could not pin framebuffer\n");
  515. }
  516. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  517. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  518. if (!nv_crtc->cursor.nvbo)
  519. continue;
  520. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM, true);
  521. if (!ret && nv_crtc->cursor.set_offset)
  522. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  523. if (ret)
  524. NV_ERROR(drm, "Could not pin/map cursor.\n");
  525. }
  526. nouveau_display_init(dev);
  527. /* Force CLUT to get re-loaded during modeset */
  528. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  529. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  530. nv_crtc->lut.depth = 0;
  531. }
  532. /* This should ensure we don't hit a locking problem when someone
  533. * wakes us up via a connector. We should never go into suspend
  534. * while the display is on anyways.
  535. */
  536. if (runtime)
  537. return;
  538. drm_helper_resume_force_mode(dev);
  539. /* Make sure that drm and hw vblank irqs get resumed if needed. */
  540. for (head = 0; head < dev->mode_config.num_crtc; head++)
  541. drm_vblank_on(dev, head);
  542. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  543. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  544. if (!nv_crtc->cursor.nvbo)
  545. continue;
  546. if (nv_crtc->cursor.set_offset)
  547. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
  548. nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
  549. nv_crtc->cursor_saved_y);
  550. }
  551. }
  552. static int
  553. nouveau_page_flip_emit(struct nouveau_channel *chan,
  554. struct nouveau_bo *old_bo,
  555. struct nouveau_bo *new_bo,
  556. struct nouveau_page_flip_state *s,
  557. struct nouveau_fence **pfence)
  558. {
  559. struct nouveau_fence_chan *fctx = chan->fence;
  560. struct nouveau_drm *drm = chan->drm;
  561. struct drm_device *dev = drm->dev;
  562. unsigned long flags;
  563. int ret;
  564. /* Queue it to the pending list */
  565. spin_lock_irqsave(&dev->event_lock, flags);
  566. list_add_tail(&s->head, &fctx->flip);
  567. spin_unlock_irqrestore(&dev->event_lock, flags);
  568. /* Synchronize with the old framebuffer */
  569. ret = nouveau_fence_sync(old_bo, chan, false, false);
  570. if (ret)
  571. goto fail;
  572. /* Emit the pageflip */
  573. ret = RING_SPACE(chan, 2);
  574. if (ret)
  575. goto fail;
  576. if (drm->device.info.family < NV_DEVICE_INFO_V0_FERMI)
  577. BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  578. else
  579. BEGIN_NVC0(chan, FermiSw, NV_SW_PAGE_FLIP, 1);
  580. OUT_RING (chan, 0x00000000);
  581. FIRE_RING (chan);
  582. ret = nouveau_fence_new(chan, false, pfence);
  583. if (ret)
  584. goto fail;
  585. return 0;
  586. fail:
  587. spin_lock_irqsave(&dev->event_lock, flags);
  588. list_del(&s->head);
  589. spin_unlock_irqrestore(&dev->event_lock, flags);
  590. return ret;
  591. }
  592. int
  593. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  594. struct drm_pending_vblank_event *event, u32 flags)
  595. {
  596. const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
  597. struct drm_device *dev = crtc->dev;
  598. struct nouveau_drm *drm = nouveau_drm(dev);
  599. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->primary->fb)->nvbo;
  600. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  601. struct nouveau_page_flip_state *s;
  602. struct nouveau_channel *chan;
  603. struct nouveau_cli *cli;
  604. struct nouveau_fence *fence;
  605. int ret;
  606. chan = drm->channel;
  607. if (!chan)
  608. return -ENODEV;
  609. cli = (void *)chan->user.client;
  610. s = kzalloc(sizeof(*s), GFP_KERNEL);
  611. if (!s)
  612. return -ENOMEM;
  613. if (new_bo != old_bo) {
  614. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM, true);
  615. if (ret)
  616. goto fail_free;
  617. }
  618. mutex_lock(&cli->mutex);
  619. ret = ttm_bo_reserve(&new_bo->bo, true, false, NULL);
  620. if (ret)
  621. goto fail_unpin;
  622. /* synchronise rendering channel with the kernel's channel */
  623. ret = nouveau_fence_sync(new_bo, chan, false, true);
  624. if (ret) {
  625. ttm_bo_unreserve(&new_bo->bo);
  626. goto fail_unpin;
  627. }
  628. if (new_bo != old_bo) {
  629. ttm_bo_unreserve(&new_bo->bo);
  630. ret = ttm_bo_reserve(&old_bo->bo, true, false, NULL);
  631. if (ret)
  632. goto fail_unpin;
  633. }
  634. /* Initialize a page flip struct */
  635. *s = (struct nouveau_page_flip_state)
  636. { { }, event, crtc, fb->bits_per_pixel, fb->pitches[0],
  637. new_bo->bo.offset };
  638. /* Keep vblanks on during flip, for the target crtc of this flip */
  639. drm_crtc_vblank_get(crtc);
  640. /* Emit a page flip */
  641. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  642. ret = nv50_display_flip_next(crtc, fb, chan, swap_interval);
  643. if (ret)
  644. goto fail_unreserve;
  645. } else {
  646. struct nv04_display *dispnv04 = nv04_display(dev);
  647. int head = nouveau_crtc(crtc)->index;
  648. if (swap_interval) {
  649. ret = RING_SPACE(chan, 8);
  650. if (ret)
  651. goto fail_unreserve;
  652. BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
  653. OUT_RING (chan, 0);
  654. BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
  655. OUT_RING (chan, head);
  656. BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
  657. OUT_RING (chan, 0);
  658. BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
  659. OUT_RING (chan, 0);
  660. }
  661. nouveau_bo_ref(new_bo, &dispnv04->image[head]);
  662. }
  663. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  664. if (ret)
  665. goto fail_unreserve;
  666. mutex_unlock(&cli->mutex);
  667. /* Update the crtc struct and cleanup */
  668. crtc->primary->fb = fb;
  669. nouveau_bo_fence(old_bo, fence, false);
  670. ttm_bo_unreserve(&old_bo->bo);
  671. if (old_bo != new_bo)
  672. nouveau_bo_unpin(old_bo);
  673. nouveau_fence_unref(&fence);
  674. return 0;
  675. fail_unreserve:
  676. drm_crtc_vblank_put(crtc);
  677. ttm_bo_unreserve(&old_bo->bo);
  678. fail_unpin:
  679. mutex_unlock(&cli->mutex);
  680. if (old_bo != new_bo)
  681. nouveau_bo_unpin(new_bo);
  682. fail_free:
  683. kfree(s);
  684. return ret;
  685. }
  686. int
  687. nouveau_finish_page_flip(struct nouveau_channel *chan,
  688. struct nouveau_page_flip_state *ps)
  689. {
  690. struct nouveau_fence_chan *fctx = chan->fence;
  691. struct nouveau_drm *drm = chan->drm;
  692. struct drm_device *dev = drm->dev;
  693. struct nouveau_page_flip_state *s;
  694. unsigned long flags;
  695. spin_lock_irqsave(&dev->event_lock, flags);
  696. if (list_empty(&fctx->flip)) {
  697. NV_ERROR(drm, "unexpected pageflip\n");
  698. spin_unlock_irqrestore(&dev->event_lock, flags);
  699. return -EINVAL;
  700. }
  701. s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
  702. if (s->event) {
  703. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  704. drm_crtc_arm_vblank_event(s->crtc, s->event);
  705. } else {
  706. drm_crtc_send_vblank_event(s->crtc, s->event);
  707. /* Give up ownership of vblank for page-flipped crtc */
  708. drm_crtc_vblank_put(s->crtc);
  709. }
  710. }
  711. else {
  712. /* Give up ownership of vblank for page-flipped crtc */
  713. drm_crtc_vblank_put(s->crtc);
  714. }
  715. list_del(&s->head);
  716. if (ps)
  717. *ps = *s;
  718. kfree(s);
  719. spin_unlock_irqrestore(&dev->event_lock, flags);
  720. return 0;
  721. }
  722. int
  723. nouveau_flip_complete(struct nvif_notify *notify)
  724. {
  725. struct nouveau_drm *drm = container_of(notify, typeof(*drm), flip);
  726. struct nouveau_channel *chan = drm->channel;
  727. struct nouveau_page_flip_state state;
  728. if (!nouveau_finish_page_flip(chan, &state)) {
  729. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  730. nv_set_crtc_base(drm->dev, drm_crtc_index(state.crtc),
  731. state.offset + state.crtc->y *
  732. state.pitch + state.crtc->x *
  733. state.bpp / 8);
  734. }
  735. }
  736. return NVIF_NOTIFY_KEEP;
  737. }
  738. int
  739. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  740. struct drm_mode_create_dumb *args)
  741. {
  742. struct nouveau_bo *bo;
  743. uint32_t domain;
  744. int ret;
  745. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  746. args->size = args->pitch * args->height;
  747. args->size = roundup(args->size, PAGE_SIZE);
  748. /* Use VRAM if there is any ; otherwise fallback to system memory */
  749. if (nouveau_drm(dev)->device.info.ram_size != 0)
  750. domain = NOUVEAU_GEM_DOMAIN_VRAM;
  751. else
  752. domain = NOUVEAU_GEM_DOMAIN_GART;
  753. ret = nouveau_gem_new(dev, args->size, 0, domain, 0, 0, &bo);
  754. if (ret)
  755. return ret;
  756. ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
  757. drm_gem_object_unreference_unlocked(&bo->gem);
  758. return ret;
  759. }
  760. int
  761. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  762. struct drm_device *dev,
  763. uint32_t handle, uint64_t *poffset)
  764. {
  765. struct drm_gem_object *gem;
  766. gem = drm_gem_object_lookup(file_priv, handle);
  767. if (gem) {
  768. struct nouveau_bo *bo = nouveau_gem_object(gem);
  769. *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
  770. drm_gem_object_unreference_unlocked(gem);
  771. return 0;
  772. }
  773. return -ENOENT;
  774. }