nouveau_display.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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/event.h>
  38. static int
  39. nouveau_display_vblank_handler(struct nvif_notify *notify)
  40. {
  41. struct nouveau_crtc *nv_crtc =
  42. container_of(notify, typeof(*nv_crtc), vblank);
  43. drm_handle_vblank(nv_crtc->base.dev, nv_crtc->index);
  44. return NVIF_NOTIFY_KEEP;
  45. }
  46. int
  47. nouveau_display_vblank_enable(struct drm_device *dev, unsigned int pipe)
  48. {
  49. struct drm_crtc *crtc;
  50. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  51. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  52. if (nv_crtc->index == pipe) {
  53. nvif_notify_get(&nv_crtc->vblank);
  54. return 0;
  55. }
  56. }
  57. return -EINVAL;
  58. }
  59. void
  60. nouveau_display_vblank_disable(struct drm_device *dev, unsigned int pipe)
  61. {
  62. struct drm_crtc *crtc;
  63. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  64. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  65. if (nv_crtc->index == pipe) {
  66. nvif_notify_put(&nv_crtc->vblank);
  67. return;
  68. }
  69. }
  70. }
  71. static inline int
  72. calc(int blanks, int blanke, int total, int line)
  73. {
  74. if (blanke >= blanks) {
  75. if (line >= blanks)
  76. line -= total;
  77. } else {
  78. if (line >= blanks)
  79. line -= total;
  80. line -= blanke + 1;
  81. }
  82. return line;
  83. }
  84. int
  85. nouveau_display_scanoutpos_head(struct drm_crtc *crtc, int *vpos, int *hpos,
  86. ktime_t *stime, ktime_t *etime)
  87. {
  88. struct {
  89. struct nv04_disp_mthd_v0 base;
  90. struct nv04_disp_scanoutpos_v0 scan;
  91. } args = {
  92. .base.method = NV04_DISP_SCANOUTPOS,
  93. .base.head = nouveau_crtc(crtc)->index,
  94. };
  95. struct nouveau_display *disp = nouveau_display(crtc->dev);
  96. struct drm_vblank_crtc *vblank = &crtc->dev->vblank[drm_crtc_index(crtc)];
  97. int ret, retry = 20;
  98. do {
  99. ret = nvif_mthd(&disp->disp, 0, &args, sizeof(args));
  100. if (ret != 0)
  101. return 0;
  102. if (args.scan.vline) {
  103. ret |= DRM_SCANOUTPOS_ACCURATE;
  104. ret |= DRM_SCANOUTPOS_VALID;
  105. break;
  106. }
  107. if (retry) ndelay(vblank->linedur_ns);
  108. } while (retry--);
  109. *hpos = args.scan.hline;
  110. *vpos = calc(args.scan.vblanks, args.scan.vblanke,
  111. args.scan.vtotal, args.scan.vline);
  112. if (stime) *stime = ns_to_ktime(args.scan.time[0]);
  113. if (etime) *etime = ns_to_ktime(args.scan.time[1]);
  114. if (*vpos < 0)
  115. ret |= DRM_SCANOUTPOS_IN_VBLANK;
  116. return ret;
  117. }
  118. int
  119. nouveau_display_scanoutpos(struct drm_device *dev, unsigned int pipe,
  120. unsigned int flags, int *vpos, int *hpos,
  121. ktime_t *stime, ktime_t *etime,
  122. const struct drm_display_mode *mode)
  123. {
  124. struct drm_crtc *crtc;
  125. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  126. if (nouveau_crtc(crtc)->index == pipe) {
  127. return nouveau_display_scanoutpos_head(crtc, vpos, hpos,
  128. stime, etime);
  129. }
  130. }
  131. return 0;
  132. }
  133. int
  134. nouveau_display_vblstamp(struct drm_device *dev, unsigned int pipe,
  135. int *max_error, struct timeval *time, unsigned flags)
  136. {
  137. struct drm_crtc *crtc;
  138. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  139. if (nouveau_crtc(crtc)->index == pipe) {
  140. return drm_calc_vbltimestamp_from_scanoutpos(dev,
  141. pipe, max_error, time, flags,
  142. &crtc->hwmode);
  143. }
  144. }
  145. return -EINVAL;
  146. }
  147. static void
  148. nouveau_display_vblank_fini(struct drm_device *dev)
  149. {
  150. struct drm_crtc *crtc;
  151. drm_vblank_cleanup(dev);
  152. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  153. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  154. nvif_notify_fini(&nv_crtc->vblank);
  155. }
  156. }
  157. static int
  158. nouveau_display_vblank_init(struct drm_device *dev)
  159. {
  160. struct nouveau_display *disp = nouveau_display(dev);
  161. struct drm_crtc *crtc;
  162. int ret;
  163. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  164. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  165. ret = nvif_notify_init(&disp->disp,
  166. nouveau_display_vblank_handler, false,
  167. NV04_DISP_NTFY_VBLANK,
  168. &(struct nvif_notify_head_req_v0) {
  169. .head = nv_crtc->index,
  170. },
  171. sizeof(struct nvif_notify_head_req_v0),
  172. sizeof(struct nvif_notify_head_rep_v0),
  173. &nv_crtc->vblank);
  174. if (ret) {
  175. nouveau_display_vblank_fini(dev);
  176. return ret;
  177. }
  178. }
  179. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  180. if (ret) {
  181. nouveau_display_vblank_fini(dev);
  182. return ret;
  183. }
  184. return 0;
  185. }
  186. static void
  187. nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
  188. {
  189. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  190. struct nouveau_display *disp = nouveau_display(drm_fb->dev);
  191. if (disp->fb_dtor)
  192. disp->fb_dtor(drm_fb);
  193. if (fb->nvbo)
  194. drm_gem_object_unreference_unlocked(&fb->nvbo->gem);
  195. drm_framebuffer_cleanup(drm_fb);
  196. kfree(fb);
  197. }
  198. static int
  199. nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
  200. struct drm_file *file_priv,
  201. unsigned int *handle)
  202. {
  203. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  204. return drm_gem_handle_create(file_priv, &fb->nvbo->gem, handle);
  205. }
  206. static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
  207. .destroy = nouveau_user_framebuffer_destroy,
  208. .create_handle = nouveau_user_framebuffer_create_handle,
  209. };
  210. int
  211. nouveau_framebuffer_init(struct drm_device *dev,
  212. struct nouveau_framebuffer *nv_fb,
  213. struct drm_mode_fb_cmd2 *mode_cmd,
  214. struct nouveau_bo *nvbo)
  215. {
  216. struct nouveau_display *disp = nouveau_display(dev);
  217. struct drm_framebuffer *fb = &nv_fb->base;
  218. int ret;
  219. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  220. nv_fb->nvbo = nvbo;
  221. ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
  222. if (ret)
  223. return ret;
  224. if (disp->fb_ctor) {
  225. ret = disp->fb_ctor(fb);
  226. if (ret)
  227. disp->fb_dtor(fb);
  228. }
  229. return ret;
  230. }
  231. static struct drm_framebuffer *
  232. nouveau_user_framebuffer_create(struct drm_device *dev,
  233. struct drm_file *file_priv,
  234. struct drm_mode_fb_cmd2 *mode_cmd)
  235. {
  236. struct nouveau_framebuffer *nouveau_fb;
  237. struct drm_gem_object *gem;
  238. int ret = -ENOMEM;
  239. gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  240. if (!gem)
  241. return ERR_PTR(-ENOENT);
  242. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  243. if (!nouveau_fb)
  244. goto err_unref;
  245. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  246. if (ret)
  247. goto err;
  248. return &nouveau_fb->base;
  249. err:
  250. kfree(nouveau_fb);
  251. err_unref:
  252. drm_gem_object_unreference(gem);
  253. return ERR_PTR(ret);
  254. }
  255. static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  256. .fb_create = nouveau_user_framebuffer_create,
  257. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  258. };
  259. struct nouveau_drm_prop_enum_list {
  260. u8 gen_mask;
  261. int type;
  262. char *name;
  263. };
  264. static struct nouveau_drm_prop_enum_list underscan[] = {
  265. { 6, UNDERSCAN_AUTO, "auto" },
  266. { 6, UNDERSCAN_OFF, "off" },
  267. { 6, UNDERSCAN_ON, "on" },
  268. {}
  269. };
  270. static struct nouveau_drm_prop_enum_list dither_mode[] = {
  271. { 7, DITHERING_MODE_AUTO, "auto" },
  272. { 7, DITHERING_MODE_OFF, "off" },
  273. { 1, DITHERING_MODE_ON, "on" },
  274. { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
  275. { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
  276. { 4, DITHERING_MODE_TEMPORAL, "temporal" },
  277. {}
  278. };
  279. static struct nouveau_drm_prop_enum_list dither_depth[] = {
  280. { 6, DITHERING_DEPTH_AUTO, "auto" },
  281. { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
  282. { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
  283. {}
  284. };
  285. #define PROP_ENUM(p,gen,n,list) do { \
  286. struct nouveau_drm_prop_enum_list *l = (list); \
  287. int c = 0; \
  288. while (l->gen_mask) { \
  289. if (l->gen_mask & (1 << (gen))) \
  290. c++; \
  291. l++; \
  292. } \
  293. if (c) { \
  294. p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
  295. l = (list); \
  296. c = 0; \
  297. while (p && l->gen_mask) { \
  298. if (l->gen_mask & (1 << (gen))) { \
  299. drm_property_add_enum(p, c, l->type, l->name); \
  300. c++; \
  301. } \
  302. l++; \
  303. } \
  304. } \
  305. } while(0)
  306. int
  307. nouveau_display_init(struct drm_device *dev)
  308. {
  309. struct nouveau_display *disp = nouveau_display(dev);
  310. struct nouveau_drm *drm = nouveau_drm(dev);
  311. struct drm_connector *connector;
  312. int ret;
  313. ret = disp->init(dev);
  314. if (ret)
  315. return ret;
  316. /* enable polling for external displays */
  317. if (!dev->mode_config.poll_enabled)
  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. GM204_DISP,
  419. GM107_DISP,
  420. GK110_DISP,
  421. GK104_DISP,
  422. GF110_DISP,
  423. GT214_DISP,
  424. GT206_DISP,
  425. GT200_DISP,
  426. G82_DISP,
  427. NV50_DISP,
  428. NV04_DISP,
  429. };
  430. int i;
  431. for (i = 0, ret = -ENODEV; ret && i < ARRAY_SIZE(oclass); i++) {
  432. ret = nvif_object_init(&drm->device.object, 0,
  433. oclass[i], NULL, 0, &disp->disp);
  434. }
  435. if (ret == 0) {
  436. nouveau_display_create_properties(dev);
  437. if (disp->disp.oclass < NV50_DISP)
  438. ret = nv04_display_create(dev);
  439. else
  440. ret = nv50_display_create(dev);
  441. }
  442. } else {
  443. ret = 0;
  444. }
  445. if (ret)
  446. goto disp_create_err;
  447. if (dev->mode_config.num_crtc) {
  448. ret = nouveau_display_vblank_init(dev);
  449. if (ret)
  450. goto vblank_err;
  451. }
  452. nouveau_backlight_init(dev);
  453. return 0;
  454. vblank_err:
  455. disp->dtor(dev);
  456. disp_create_err:
  457. drm_kms_helper_poll_fini(dev);
  458. drm_mode_config_cleanup(dev);
  459. return ret;
  460. }
  461. void
  462. nouveau_display_destroy(struct drm_device *dev)
  463. {
  464. struct nouveau_display *disp = nouveau_display(dev);
  465. nouveau_backlight_exit(dev);
  466. nouveau_display_vblank_fini(dev);
  467. drm_kms_helper_poll_fini(dev);
  468. drm_mode_config_cleanup(dev);
  469. if (disp->dtor)
  470. disp->dtor(dev);
  471. nvif_object_fini(&disp->disp);
  472. nouveau_drm(dev)->display = NULL;
  473. kfree(disp);
  474. }
  475. int
  476. nouveau_display_suspend(struct drm_device *dev, bool runtime)
  477. {
  478. struct drm_crtc *crtc;
  479. nouveau_display_fini(dev);
  480. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  481. struct nouveau_framebuffer *nouveau_fb;
  482. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  483. if (!nouveau_fb || !nouveau_fb->nvbo)
  484. continue;
  485. nouveau_bo_unpin(nouveau_fb->nvbo);
  486. }
  487. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  488. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  489. if (nv_crtc->cursor.nvbo) {
  490. if (nv_crtc->cursor.set_offset)
  491. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  492. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  493. }
  494. }
  495. return 0;
  496. }
  497. void
  498. nouveau_display_resume(struct drm_device *dev, bool runtime)
  499. {
  500. struct nouveau_drm *drm = nouveau_drm(dev);
  501. struct drm_crtc *crtc;
  502. int ret, head;
  503. /* re-pin fb/cursors */
  504. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  505. struct nouveau_framebuffer *nouveau_fb;
  506. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  507. if (!nouveau_fb || !nouveau_fb->nvbo)
  508. continue;
  509. ret = nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM, true);
  510. if (ret)
  511. NV_ERROR(drm, "Could not pin framebuffer\n");
  512. }
  513. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  514. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  515. if (!nv_crtc->cursor.nvbo)
  516. continue;
  517. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM, true);
  518. if (!ret && nv_crtc->cursor.set_offset)
  519. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  520. if (ret)
  521. NV_ERROR(drm, "Could not pin/map cursor.\n");
  522. }
  523. nouveau_display_init(dev);
  524. /* Force CLUT to get re-loaded during modeset */
  525. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  526. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  527. nv_crtc->lut.depth = 0;
  528. }
  529. /* This should ensure we don't hit a locking problem when someone
  530. * wakes us up via a connector. We should never go into suspend
  531. * while the display is on anyways.
  532. */
  533. if (runtime)
  534. return;
  535. drm_helper_resume_force_mode(dev);
  536. /* Make sure that drm and hw vblank irqs get resumed if needed. */
  537. for (head = 0; head < dev->mode_config.num_crtc; head++)
  538. drm_vblank_on(dev, head);
  539. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  540. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  541. if (!nv_crtc->cursor.nvbo)
  542. continue;
  543. if (nv_crtc->cursor.set_offset)
  544. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
  545. nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
  546. nv_crtc->cursor_saved_y);
  547. }
  548. }
  549. static int
  550. nouveau_page_flip_emit(struct nouveau_channel *chan,
  551. struct nouveau_bo *old_bo,
  552. struct nouveau_bo *new_bo,
  553. struct nouveau_page_flip_state *s,
  554. struct nouveau_fence **pfence)
  555. {
  556. struct nouveau_fence_chan *fctx = chan->fence;
  557. struct nouveau_drm *drm = chan->drm;
  558. struct drm_device *dev = drm->dev;
  559. unsigned long flags;
  560. int ret;
  561. /* Queue it to the pending list */
  562. spin_lock_irqsave(&dev->event_lock, flags);
  563. list_add_tail(&s->head, &fctx->flip);
  564. spin_unlock_irqrestore(&dev->event_lock, flags);
  565. /* Synchronize with the old framebuffer */
  566. ret = nouveau_fence_sync(old_bo, chan, false, false);
  567. if (ret)
  568. goto fail;
  569. /* Emit the pageflip */
  570. ret = RING_SPACE(chan, 2);
  571. if (ret)
  572. goto fail;
  573. if (drm->device.info.family < NV_DEVICE_INFO_V0_FERMI)
  574. BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  575. else
  576. BEGIN_NVC0(chan, FermiSw, NV_SW_PAGE_FLIP, 1);
  577. OUT_RING (chan, 0x00000000);
  578. FIRE_RING (chan);
  579. ret = nouveau_fence_new(chan, false, pfence);
  580. if (ret)
  581. goto fail;
  582. return 0;
  583. fail:
  584. spin_lock_irqsave(&dev->event_lock, flags);
  585. list_del(&s->head);
  586. spin_unlock_irqrestore(&dev->event_lock, flags);
  587. return ret;
  588. }
  589. int
  590. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  591. struct drm_pending_vblank_event *event, u32 flags)
  592. {
  593. const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
  594. struct drm_device *dev = crtc->dev;
  595. struct nouveau_drm *drm = nouveau_drm(dev);
  596. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->primary->fb)->nvbo;
  597. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  598. struct nouveau_page_flip_state *s;
  599. struct nouveau_channel *chan;
  600. struct nouveau_cli *cli;
  601. struct nouveau_fence *fence;
  602. int ret;
  603. chan = drm->channel;
  604. if (!chan)
  605. return -ENODEV;
  606. cli = (void *)chan->user.client;
  607. s = kzalloc(sizeof(*s), GFP_KERNEL);
  608. if (!s)
  609. return -ENOMEM;
  610. if (new_bo != old_bo) {
  611. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM, true);
  612. if (ret)
  613. goto fail_free;
  614. }
  615. mutex_lock(&cli->mutex);
  616. ret = ttm_bo_reserve(&new_bo->bo, true, false, false, NULL);
  617. if (ret)
  618. goto fail_unpin;
  619. /* synchronise rendering channel with the kernel's channel */
  620. ret = nouveau_fence_sync(new_bo, chan, false, true);
  621. if (ret) {
  622. ttm_bo_unreserve(&new_bo->bo);
  623. goto fail_unpin;
  624. }
  625. if (new_bo != old_bo) {
  626. ttm_bo_unreserve(&new_bo->bo);
  627. ret = ttm_bo_reserve(&old_bo->bo, true, false, false, NULL);
  628. if (ret)
  629. goto fail_unpin;
  630. }
  631. /* Initialize a page flip struct */
  632. *s = (struct nouveau_page_flip_state)
  633. { { }, event, nouveau_crtc(crtc)->index,
  634. fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
  635. new_bo->bo.offset };
  636. /* Keep vblanks on during flip, for the target crtc of this flip */
  637. drm_vblank_get(dev, nouveau_crtc(crtc)->index);
  638. /* Emit a page flip */
  639. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  640. ret = nv50_display_flip_next(crtc, fb, chan, swap_interval);
  641. if (ret)
  642. goto fail_unreserve;
  643. } else {
  644. struct nv04_display *dispnv04 = nv04_display(dev);
  645. int head = nouveau_crtc(crtc)->index;
  646. if (swap_interval) {
  647. ret = RING_SPACE(chan, 8);
  648. if (ret)
  649. goto fail_unreserve;
  650. BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
  651. OUT_RING (chan, 0);
  652. BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
  653. OUT_RING (chan, head);
  654. BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
  655. OUT_RING (chan, 0);
  656. BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
  657. OUT_RING (chan, 0);
  658. }
  659. nouveau_bo_ref(new_bo, &dispnv04->image[head]);
  660. }
  661. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  662. if (ret)
  663. goto fail_unreserve;
  664. mutex_unlock(&cli->mutex);
  665. /* Update the crtc struct and cleanup */
  666. crtc->primary->fb = fb;
  667. nouveau_bo_fence(old_bo, fence, false);
  668. ttm_bo_unreserve(&old_bo->bo);
  669. if (old_bo != new_bo)
  670. nouveau_bo_unpin(old_bo);
  671. nouveau_fence_unref(&fence);
  672. return 0;
  673. fail_unreserve:
  674. drm_vblank_put(dev, nouveau_crtc(crtc)->index);
  675. ttm_bo_unreserve(&old_bo->bo);
  676. fail_unpin:
  677. mutex_unlock(&cli->mutex);
  678. if (old_bo != new_bo)
  679. nouveau_bo_unpin(new_bo);
  680. fail_free:
  681. kfree(s);
  682. return ret;
  683. }
  684. int
  685. nouveau_finish_page_flip(struct nouveau_channel *chan,
  686. struct nouveau_page_flip_state *ps)
  687. {
  688. struct nouveau_fence_chan *fctx = chan->fence;
  689. struct nouveau_drm *drm = chan->drm;
  690. struct drm_device *dev = drm->dev;
  691. struct nouveau_page_flip_state *s;
  692. unsigned long flags;
  693. spin_lock_irqsave(&dev->event_lock, flags);
  694. if (list_empty(&fctx->flip)) {
  695. NV_ERROR(drm, "unexpected pageflip\n");
  696. spin_unlock_irqrestore(&dev->event_lock, flags);
  697. return -EINVAL;
  698. }
  699. s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
  700. if (s->event) {
  701. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  702. drm_arm_vblank_event(dev, s->crtc, s->event);
  703. } else {
  704. drm_send_vblank_event(dev, s->crtc, s->event);
  705. /* Give up ownership of vblank for page-flipped crtc */
  706. drm_vblank_put(dev, s->crtc);
  707. }
  708. }
  709. else {
  710. /* Give up ownership of vblank for page-flipped crtc */
  711. drm_vblank_put(dev, s->crtc);
  712. }
  713. list_del(&s->head);
  714. if (ps)
  715. *ps = *s;
  716. kfree(s);
  717. spin_unlock_irqrestore(&dev->event_lock, flags);
  718. return 0;
  719. }
  720. int
  721. nouveau_flip_complete(struct nvif_notify *notify)
  722. {
  723. struct nouveau_drm *drm = container_of(notify, typeof(*drm), flip);
  724. struct nouveau_channel *chan = drm->channel;
  725. struct nouveau_page_flip_state state;
  726. if (!nouveau_finish_page_flip(chan, &state)) {
  727. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  728. nv_set_crtc_base(drm->dev, state.crtc, state.offset +
  729. state.y * state.pitch +
  730. state.x * state.bpp / 8);
  731. }
  732. }
  733. return NVIF_NOTIFY_KEEP;
  734. }
  735. int
  736. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  737. struct drm_mode_create_dumb *args)
  738. {
  739. struct nouveau_bo *bo;
  740. uint32_t domain;
  741. int ret;
  742. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  743. args->size = args->pitch * args->height;
  744. args->size = roundup(args->size, PAGE_SIZE);
  745. /* Use VRAM if there is any ; otherwise fallback to system memory */
  746. if (nouveau_drm(dev)->device.info.ram_size != 0)
  747. domain = NOUVEAU_GEM_DOMAIN_VRAM;
  748. else
  749. domain = NOUVEAU_GEM_DOMAIN_GART;
  750. ret = nouveau_gem_new(dev, args->size, 0, domain, 0, 0, &bo);
  751. if (ret)
  752. return ret;
  753. ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
  754. drm_gem_object_unreference_unlocked(&bo->gem);
  755. return ret;
  756. }
  757. int
  758. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  759. struct drm_device *dev,
  760. uint32_t handle, uint64_t *poffset)
  761. {
  762. struct drm_gem_object *gem;
  763. gem = drm_gem_object_lookup(dev, file_priv, handle);
  764. if (gem) {
  765. struct nouveau_bo *bo = nouveau_gem_object(gem);
  766. *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
  767. drm_gem_object_unreference_unlocked(gem);
  768. return 0;
  769. }
  770. return -ENOENT;
  771. }