nouveau_display.c 25 KB

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