nouveau_display.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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, int head)
  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 == head) {
  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, int head)
  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 == head) {
  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. int ret, retry = 1;
  97. do {
  98. ret = nvif_mthd(&disp->disp, 0, &args, sizeof(args));
  99. if (ret != 0)
  100. return 0;
  101. if (args.scan.vline) {
  102. ret |= DRM_SCANOUTPOS_ACCURATE;
  103. ret |= DRM_SCANOUTPOS_VALID;
  104. break;
  105. }
  106. if (retry) ndelay(crtc->linedur_ns);
  107. } while (retry--);
  108. *hpos = args.scan.hline;
  109. *vpos = calc(args.scan.vblanks, args.scan.vblanke,
  110. args.scan.vtotal, args.scan.vline);
  111. if (stime) *stime = ns_to_ktime(args.scan.time[0]);
  112. if (etime) *etime = ns_to_ktime(args.scan.time[1]);
  113. if (*vpos < 0)
  114. ret |= DRM_SCANOUTPOS_IN_VBLANK;
  115. return ret;
  116. }
  117. int
  118. nouveau_display_scanoutpos(struct drm_device *dev, int head, unsigned int flags,
  119. int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
  120. {
  121. struct drm_crtc *crtc;
  122. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  123. if (nouveau_crtc(crtc)->index == head) {
  124. return nouveau_display_scanoutpos_head(crtc, vpos, hpos,
  125. stime, etime);
  126. }
  127. }
  128. return 0;
  129. }
  130. int
  131. nouveau_display_vblstamp(struct drm_device *dev, int head, int *max_error,
  132. struct timeval *time, unsigned flags)
  133. {
  134. struct drm_crtc *crtc;
  135. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  136. if (nouveau_crtc(crtc)->index == head) {
  137. return drm_calc_vbltimestamp_from_scanoutpos(dev,
  138. head, max_error, time, flags, crtc,
  139. &crtc->hwmode);
  140. }
  141. }
  142. return -EINVAL;
  143. }
  144. static void
  145. nouveau_display_vblank_fini(struct drm_device *dev)
  146. {
  147. struct drm_crtc *crtc;
  148. drm_vblank_cleanup(dev);
  149. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  150. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  151. nvif_notify_fini(&nv_crtc->vblank);
  152. }
  153. }
  154. static int
  155. nouveau_display_vblank_init(struct drm_device *dev)
  156. {
  157. struct nouveau_display *disp = nouveau_display(dev);
  158. struct drm_crtc *crtc;
  159. int ret;
  160. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  161. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  162. ret = nvif_notify_init(&disp->disp,
  163. nouveau_display_vblank_handler, false,
  164. NV04_DISP_NTFY_VBLANK,
  165. &(struct nvif_notify_head_req_v0) {
  166. .head = nv_crtc->index,
  167. },
  168. sizeof(struct nvif_notify_head_req_v0),
  169. sizeof(struct nvif_notify_head_rep_v0),
  170. &nv_crtc->vblank);
  171. if (ret) {
  172. nouveau_display_vblank_fini(dev);
  173. return ret;
  174. }
  175. }
  176. ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
  177. if (ret) {
  178. nouveau_display_vblank_fini(dev);
  179. return ret;
  180. }
  181. return 0;
  182. }
  183. static void
  184. nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
  185. {
  186. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  187. struct nouveau_display *disp = nouveau_display(drm_fb->dev);
  188. if (disp->fb_dtor)
  189. disp->fb_dtor(drm_fb);
  190. if (fb->nvbo)
  191. drm_gem_object_unreference_unlocked(&fb->nvbo->gem);
  192. drm_framebuffer_cleanup(drm_fb);
  193. kfree(fb);
  194. }
  195. static int
  196. nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
  197. struct drm_file *file_priv,
  198. unsigned int *handle)
  199. {
  200. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  201. return drm_gem_handle_create(file_priv, &fb->nvbo->gem, handle);
  202. }
  203. static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
  204. .destroy = nouveau_user_framebuffer_destroy,
  205. .create_handle = nouveau_user_framebuffer_create_handle,
  206. };
  207. int
  208. nouveau_framebuffer_init(struct drm_device *dev,
  209. struct nouveau_framebuffer *nv_fb,
  210. struct drm_mode_fb_cmd2 *mode_cmd,
  211. struct nouveau_bo *nvbo)
  212. {
  213. struct nouveau_display *disp = nouveau_display(dev);
  214. struct drm_framebuffer *fb = &nv_fb->base;
  215. int ret;
  216. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  217. nv_fb->nvbo = nvbo;
  218. ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
  219. if (ret)
  220. return ret;
  221. if (disp->fb_ctor) {
  222. ret = disp->fb_ctor(fb);
  223. if (ret)
  224. disp->fb_dtor(fb);
  225. }
  226. return ret;
  227. }
  228. static struct drm_framebuffer *
  229. nouveau_user_framebuffer_create(struct drm_device *dev,
  230. struct drm_file *file_priv,
  231. struct drm_mode_fb_cmd2 *mode_cmd)
  232. {
  233. struct nouveau_framebuffer *nouveau_fb;
  234. struct drm_gem_object *gem;
  235. int ret = -ENOMEM;
  236. gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  237. if (!gem)
  238. return ERR_PTR(-ENOENT);
  239. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  240. if (!nouveau_fb)
  241. goto err_unref;
  242. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  243. if (ret)
  244. goto err;
  245. return &nouveau_fb->base;
  246. err:
  247. kfree(nouveau_fb);
  248. err_unref:
  249. drm_gem_object_unreference(gem);
  250. return ERR_PTR(ret);
  251. }
  252. static const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  253. .fb_create = nouveau_user_framebuffer_create,
  254. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  255. };
  256. struct nouveau_drm_prop_enum_list {
  257. u8 gen_mask;
  258. int type;
  259. char *name;
  260. };
  261. static struct nouveau_drm_prop_enum_list underscan[] = {
  262. { 6, UNDERSCAN_AUTO, "auto" },
  263. { 6, UNDERSCAN_OFF, "off" },
  264. { 6, UNDERSCAN_ON, "on" },
  265. {}
  266. };
  267. static struct nouveau_drm_prop_enum_list dither_mode[] = {
  268. { 7, DITHERING_MODE_AUTO, "auto" },
  269. { 7, DITHERING_MODE_OFF, "off" },
  270. { 1, DITHERING_MODE_ON, "on" },
  271. { 6, DITHERING_MODE_STATIC2X2, "static 2x2" },
  272. { 6, DITHERING_MODE_DYNAMIC2X2, "dynamic 2x2" },
  273. { 4, DITHERING_MODE_TEMPORAL, "temporal" },
  274. {}
  275. };
  276. static struct nouveau_drm_prop_enum_list dither_depth[] = {
  277. { 6, DITHERING_DEPTH_AUTO, "auto" },
  278. { 6, DITHERING_DEPTH_6BPC, "6 bpc" },
  279. { 6, DITHERING_DEPTH_8BPC, "8 bpc" },
  280. {}
  281. };
  282. #define PROP_ENUM(p,gen,n,list) do { \
  283. struct nouveau_drm_prop_enum_list *l = (list); \
  284. int c = 0; \
  285. while (l->gen_mask) { \
  286. if (l->gen_mask & (1 << (gen))) \
  287. c++; \
  288. l++; \
  289. } \
  290. if (c) { \
  291. p = drm_property_create(dev, DRM_MODE_PROP_ENUM, n, c); \
  292. l = (list); \
  293. c = 0; \
  294. while (p && l->gen_mask) { \
  295. if (l->gen_mask & (1 << (gen))) { \
  296. drm_property_add_enum(p, c, l->type, l->name); \
  297. c++; \
  298. } \
  299. l++; \
  300. } \
  301. } \
  302. } while(0)
  303. int
  304. nouveau_display_init(struct drm_device *dev)
  305. {
  306. struct nouveau_display *disp = nouveau_display(dev);
  307. struct nouveau_drm *drm = nouveau_drm(dev);
  308. struct drm_connector *connector;
  309. int ret;
  310. ret = disp->init(dev);
  311. if (ret)
  312. return ret;
  313. /* enable polling for external displays */
  314. drm_kms_helper_poll_enable(dev);
  315. /* enable hotplug interrupts */
  316. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  317. struct nouveau_connector *conn = nouveau_connector(connector);
  318. nvif_notify_get(&conn->hpd);
  319. }
  320. /* enable flip completion events */
  321. nvif_notify_get(&drm->flip);
  322. return ret;
  323. }
  324. void
  325. nouveau_display_fini(struct drm_device *dev)
  326. {
  327. struct nouveau_display *disp = nouveau_display(dev);
  328. struct nouveau_drm *drm = nouveau_drm(dev);
  329. struct drm_connector *connector;
  330. int head;
  331. /* Make sure that drm and hw vblank irqs get properly disabled. */
  332. for (head = 0; head < dev->mode_config.num_crtc; head++)
  333. drm_vblank_off(dev, head);
  334. /* disable flip completion events */
  335. nvif_notify_put(&drm->flip);
  336. /* disable hotplug interrupts */
  337. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  338. struct nouveau_connector *conn = nouveau_connector(connector);
  339. nvif_notify_put(&conn->hpd);
  340. }
  341. drm_kms_helper_poll_disable(dev);
  342. disp->fini(dev);
  343. }
  344. static void
  345. nouveau_display_create_properties(struct drm_device *dev)
  346. {
  347. struct nouveau_display *disp = nouveau_display(dev);
  348. int gen;
  349. if (disp->disp.oclass < NV50_DISP)
  350. gen = 0;
  351. else
  352. if (disp->disp.oclass < GF110_DISP)
  353. gen = 1;
  354. else
  355. gen = 2;
  356. PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
  357. PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
  358. PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
  359. disp->underscan_hborder_property =
  360. drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
  361. disp->underscan_vborder_property =
  362. drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
  363. if (gen < 1)
  364. return;
  365. /* -90..+90 */
  366. disp->vibrant_hue_property =
  367. drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
  368. /* -100..+100 */
  369. disp->color_vibrance_property =
  370. drm_property_create_range(dev, 0, "color vibrance", 0, 200);
  371. }
  372. int
  373. nouveau_display_create(struct drm_device *dev)
  374. {
  375. struct nouveau_drm *drm = nouveau_drm(dev);
  376. struct nvkm_device *device = nvxx_device(&drm->device);
  377. struct nouveau_display *disp;
  378. int ret;
  379. disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
  380. if (!disp)
  381. return -ENOMEM;
  382. drm_mode_config_init(dev);
  383. drm_mode_create_scaling_mode_property(dev);
  384. drm_mode_create_dvi_i_properties(dev);
  385. dev->mode_config.funcs = &nouveau_mode_config_funcs;
  386. dev->mode_config.fb_base = device->func->resource_addr(device, 1);
  387. dev->mode_config.min_width = 0;
  388. dev->mode_config.min_height = 0;
  389. if (drm->device.info.family < NV_DEVICE_INFO_V0_CELSIUS) {
  390. dev->mode_config.max_width = 2048;
  391. dev->mode_config.max_height = 2048;
  392. } else
  393. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  394. dev->mode_config.max_width = 4096;
  395. dev->mode_config.max_height = 4096;
  396. } else
  397. if (drm->device.info.family < NV_DEVICE_INFO_V0_FERMI) {
  398. dev->mode_config.max_width = 8192;
  399. dev->mode_config.max_height = 8192;
  400. } else {
  401. dev->mode_config.max_width = 16384;
  402. dev->mode_config.max_height = 16384;
  403. }
  404. dev->mode_config.preferred_depth = 24;
  405. dev->mode_config.prefer_shadow = 1;
  406. if (drm->device.info.chipset < 0x11)
  407. dev->mode_config.async_page_flip = false;
  408. else
  409. dev->mode_config.async_page_flip = true;
  410. drm_kms_helper_poll_init(dev);
  411. drm_kms_helper_poll_disable(dev);
  412. if (nouveau_modeset != 2 && drm->vbios.dcb.entries) {
  413. static const u16 oclass[] = {
  414. GM204_DISP,
  415. GM107_DISP,
  416. GK110_DISP,
  417. GK104_DISP,
  418. GF110_DISP,
  419. GT214_DISP,
  420. GT206_DISP,
  421. GT200_DISP,
  422. G82_DISP,
  423. NV50_DISP,
  424. NV04_DISP,
  425. };
  426. int i;
  427. for (i = 0, ret = -ENODEV; ret && i < ARRAY_SIZE(oclass); i++) {
  428. ret = nvif_object_init(&drm->device.object,
  429. NVDRM_DISPLAY, oclass[i],
  430. NULL, 0, &disp->disp);
  431. }
  432. if (ret == 0) {
  433. nouveau_display_create_properties(dev);
  434. if (disp->disp.oclass < NV50_DISP)
  435. ret = nv04_display_create(dev);
  436. else
  437. ret = nv50_display_create(dev);
  438. }
  439. } else {
  440. ret = 0;
  441. }
  442. if (ret)
  443. goto disp_create_err;
  444. if (dev->mode_config.num_crtc) {
  445. ret = nouveau_display_vblank_init(dev);
  446. if (ret)
  447. goto vblank_err;
  448. }
  449. nouveau_backlight_init(dev);
  450. return 0;
  451. vblank_err:
  452. disp->dtor(dev);
  453. disp_create_err:
  454. drm_kms_helper_poll_fini(dev);
  455. drm_mode_config_cleanup(dev);
  456. return ret;
  457. }
  458. void
  459. nouveau_display_destroy(struct drm_device *dev)
  460. {
  461. struct nouveau_display *disp = nouveau_display(dev);
  462. nouveau_backlight_exit(dev);
  463. nouveau_display_vblank_fini(dev);
  464. drm_kms_helper_poll_fini(dev);
  465. drm_mode_config_cleanup(dev);
  466. if (disp->dtor)
  467. disp->dtor(dev);
  468. nvif_object_fini(&disp->disp);
  469. nouveau_drm(dev)->display = NULL;
  470. kfree(disp);
  471. }
  472. int
  473. nouveau_display_suspend(struct drm_device *dev, bool runtime)
  474. {
  475. struct drm_crtc *crtc;
  476. nouveau_display_fini(dev);
  477. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  478. struct nouveau_framebuffer *nouveau_fb;
  479. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  480. if (!nouveau_fb || !nouveau_fb->nvbo)
  481. continue;
  482. nouveau_bo_unpin(nouveau_fb->nvbo);
  483. }
  484. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  485. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  486. if (nv_crtc->cursor.nvbo) {
  487. if (nv_crtc->cursor.set_offset)
  488. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  489. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  490. }
  491. }
  492. return 0;
  493. }
  494. void
  495. nouveau_display_resume(struct drm_device *dev, bool runtime)
  496. {
  497. struct nouveau_drm *drm = nouveau_drm(dev);
  498. struct drm_crtc *crtc;
  499. int ret, head;
  500. /* re-pin fb/cursors */
  501. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  502. struct nouveau_framebuffer *nouveau_fb;
  503. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  504. if (!nouveau_fb || !nouveau_fb->nvbo)
  505. continue;
  506. ret = nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM, true);
  507. if (ret)
  508. NV_ERROR(drm, "Could not pin framebuffer\n");
  509. }
  510. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  511. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  512. if (!nv_crtc->cursor.nvbo)
  513. continue;
  514. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM, true);
  515. if (!ret && nv_crtc->cursor.set_offset)
  516. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  517. if (ret)
  518. NV_ERROR(drm, "Could not pin/map cursor.\n");
  519. }
  520. nouveau_display_init(dev);
  521. /* Force CLUT to get re-loaded during modeset */
  522. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  523. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  524. nv_crtc->lut.depth = 0;
  525. }
  526. /* Make sure that drm and hw vblank irqs get resumed if needed. */
  527. for (head = 0; head < dev->mode_config.num_crtc; head++)
  528. drm_vblank_on(dev, head);
  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. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  537. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  538. if (!nv_crtc->cursor.nvbo)
  539. continue;
  540. if (nv_crtc->cursor.set_offset)
  541. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
  542. nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
  543. nv_crtc->cursor_saved_y);
  544. }
  545. }
  546. static int
  547. nouveau_page_flip_emit(struct nouveau_channel *chan,
  548. struct nouveau_bo *old_bo,
  549. struct nouveau_bo *new_bo,
  550. struct nouveau_page_flip_state *s,
  551. struct nouveau_fence **pfence)
  552. {
  553. struct nouveau_fence_chan *fctx = chan->fence;
  554. struct nouveau_drm *drm = chan->drm;
  555. struct drm_device *dev = drm->dev;
  556. unsigned long flags;
  557. int ret;
  558. /* Queue it to the pending list */
  559. spin_lock_irqsave(&dev->event_lock, flags);
  560. list_add_tail(&s->head, &fctx->flip);
  561. spin_unlock_irqrestore(&dev->event_lock, flags);
  562. /* Synchronize with the old framebuffer */
  563. ret = nouveau_fence_sync(old_bo, chan, false, false);
  564. if (ret)
  565. goto fail;
  566. /* Emit the pageflip */
  567. ret = RING_SPACE(chan, 2);
  568. if (ret)
  569. goto fail;
  570. if (drm->device.info.family < NV_DEVICE_INFO_V0_FERMI)
  571. BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  572. else
  573. BEGIN_NVC0(chan, FermiSw, NV_SW_PAGE_FLIP, 1);
  574. OUT_RING (chan, 0x00000000);
  575. FIRE_RING (chan);
  576. ret = nouveau_fence_new(chan, false, pfence);
  577. if (ret)
  578. goto fail;
  579. return 0;
  580. fail:
  581. spin_lock_irqsave(&dev->event_lock, flags);
  582. list_del(&s->head);
  583. spin_unlock_irqrestore(&dev->event_lock, flags);
  584. return ret;
  585. }
  586. int
  587. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  588. struct drm_pending_vblank_event *event, u32 flags)
  589. {
  590. const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
  591. struct drm_device *dev = crtc->dev;
  592. struct nouveau_drm *drm = nouveau_drm(dev);
  593. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->primary->fb)->nvbo;
  594. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  595. struct nouveau_page_flip_state *s;
  596. struct nouveau_channel *chan;
  597. struct nouveau_cli *cli;
  598. struct nouveau_fence *fence;
  599. int ret;
  600. chan = drm->channel;
  601. if (!chan)
  602. return -ENODEV;
  603. cli = (void *)chan->user.client;
  604. s = kzalloc(sizeof(*s), GFP_KERNEL);
  605. if (!s)
  606. return -ENOMEM;
  607. if (new_bo != old_bo) {
  608. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM, true);
  609. if (ret)
  610. goto fail_free;
  611. }
  612. mutex_lock(&cli->mutex);
  613. ret = ttm_bo_reserve(&new_bo->bo, true, false, false, NULL);
  614. if (ret)
  615. goto fail_unpin;
  616. /* synchronise rendering channel with the kernel's channel */
  617. ret = nouveau_fence_sync(new_bo, chan, false, true);
  618. if (ret) {
  619. ttm_bo_unreserve(&new_bo->bo);
  620. goto fail_unpin;
  621. }
  622. if (new_bo != old_bo) {
  623. ttm_bo_unreserve(&new_bo->bo);
  624. ret = ttm_bo_reserve(&old_bo->bo, true, false, false, NULL);
  625. if (ret)
  626. goto fail_unpin;
  627. }
  628. /* Initialize a page flip struct */
  629. *s = (struct nouveau_page_flip_state)
  630. { { }, event, nouveau_crtc(crtc)->index,
  631. fb->bits_per_pixel, fb->pitches[0], crtc->x, crtc->y,
  632. new_bo->bo.offset };
  633. /* Keep vblanks on during flip, for the target crtc of this flip */
  634. drm_vblank_get(dev, nouveau_crtc(crtc)->index);
  635. /* Emit a page flip */
  636. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA) {
  637. ret = nv50_display_flip_next(crtc, fb, chan, swap_interval);
  638. if (ret)
  639. goto fail_unreserve;
  640. } else {
  641. struct nv04_display *dispnv04 = nv04_display(dev);
  642. int head = nouveau_crtc(crtc)->index;
  643. if (swap_interval) {
  644. ret = RING_SPACE(chan, 8);
  645. if (ret)
  646. goto fail_unreserve;
  647. BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
  648. OUT_RING (chan, 0);
  649. BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
  650. OUT_RING (chan, head);
  651. BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
  652. OUT_RING (chan, 0);
  653. BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
  654. OUT_RING (chan, 0);
  655. }
  656. nouveau_bo_ref(new_bo, &dispnv04->image[head]);
  657. }
  658. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  659. if (ret)
  660. goto fail_unreserve;
  661. mutex_unlock(&cli->mutex);
  662. /* Update the crtc struct and cleanup */
  663. crtc->primary->fb = fb;
  664. nouveau_bo_fence(old_bo, fence, false);
  665. ttm_bo_unreserve(&old_bo->bo);
  666. if (old_bo != new_bo)
  667. nouveau_bo_unpin(old_bo);
  668. nouveau_fence_unref(&fence);
  669. return 0;
  670. fail_unreserve:
  671. drm_vblank_put(dev, nouveau_crtc(crtc)->index);
  672. ttm_bo_unreserve(&old_bo->bo);
  673. fail_unpin:
  674. mutex_unlock(&cli->mutex);
  675. if (old_bo != new_bo)
  676. nouveau_bo_unpin(new_bo);
  677. fail_free:
  678. kfree(s);
  679. return ret;
  680. }
  681. int
  682. nouveau_finish_page_flip(struct nouveau_channel *chan,
  683. struct nouveau_page_flip_state *ps)
  684. {
  685. struct nouveau_fence_chan *fctx = chan->fence;
  686. struct nouveau_drm *drm = chan->drm;
  687. struct drm_device *dev = drm->dev;
  688. struct nouveau_page_flip_state *s;
  689. unsigned long flags;
  690. int crtcid = -1;
  691. spin_lock_irqsave(&dev->event_lock, flags);
  692. if (list_empty(&fctx->flip)) {
  693. NV_ERROR(drm, "unexpected pageflip\n");
  694. spin_unlock_irqrestore(&dev->event_lock, flags);
  695. return -EINVAL;
  696. }
  697. s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
  698. if (s->event) {
  699. /* Vblank timestamps/counts are only correct on >= NV-50 */
  700. if (drm->device.info.family >= NV_DEVICE_INFO_V0_TESLA)
  701. crtcid = s->crtc;
  702. drm_send_vblank_event(dev, crtcid, s->event);
  703. }
  704. /* Give up ownership of vblank for page-flipped crtc */
  705. drm_vblank_put(dev, s->crtc);
  706. list_del(&s->head);
  707. if (ps)
  708. *ps = *s;
  709. kfree(s);
  710. spin_unlock_irqrestore(&dev->event_lock, flags);
  711. return 0;
  712. }
  713. int
  714. nouveau_flip_complete(struct nvif_notify *notify)
  715. {
  716. struct nouveau_drm *drm = container_of(notify, typeof(*drm), flip);
  717. struct nouveau_channel *chan = drm->channel;
  718. struct nouveau_page_flip_state state;
  719. if (!nouveau_finish_page_flip(chan, &state)) {
  720. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  721. nv_set_crtc_base(drm->dev, state.crtc, state.offset +
  722. state.y * state.pitch +
  723. state.x * state.bpp / 8);
  724. }
  725. }
  726. return NVIF_NOTIFY_KEEP;
  727. }
  728. int
  729. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  730. struct drm_mode_create_dumb *args)
  731. {
  732. struct nouveau_bo *bo;
  733. uint32_t domain;
  734. int ret;
  735. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  736. args->size = args->pitch * args->height;
  737. args->size = roundup(args->size, PAGE_SIZE);
  738. /* Use VRAM if there is any ; otherwise fallback to system memory */
  739. if (nouveau_drm(dev)->device.info.ram_size != 0)
  740. domain = NOUVEAU_GEM_DOMAIN_VRAM;
  741. else
  742. domain = NOUVEAU_GEM_DOMAIN_GART;
  743. ret = nouveau_gem_new(dev, args->size, 0, domain, 0, 0, &bo);
  744. if (ret)
  745. return ret;
  746. ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
  747. drm_gem_object_unreference_unlocked(&bo->gem);
  748. return ret;
  749. }
  750. int
  751. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  752. struct drm_device *dev,
  753. uint32_t handle, uint64_t *poffset)
  754. {
  755. struct drm_gem_object *gem;
  756. gem = drm_gem_object_lookup(dev, file_priv, handle);
  757. if (gem) {
  758. struct nouveau_bo *bo = nouveau_gem_object(gem);
  759. *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
  760. drm_gem_object_unreference_unlocked(gem);
  761. return 0;
  762. }
  763. return -ENOENT;
  764. }