nouveau_display.c 24 KB

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