nouveau_display.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  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 = 1;
  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. pm_runtime_mark_last_busy(drm->dev->dev);
  307. pm_runtime_put_sync(drm->dev->dev);
  308. }
  309. #ifdef CONFIG_ACPI
  310. /*
  311. * Hans de Goede: This define belongs in acpi/video.h, I've submitted a patch
  312. * to the acpi subsys to move it there from drivers/acpi/acpi_video.c .
  313. * This should be dropped once that is merged.
  314. */
  315. #ifndef ACPI_VIDEO_NOTIFY_PROBE
  316. #define ACPI_VIDEO_NOTIFY_PROBE 0x81
  317. #endif
  318. static int
  319. nouveau_display_acpi_ntfy(struct notifier_block *nb, unsigned long val,
  320. void *data)
  321. {
  322. struct nouveau_drm *drm = container_of(nb, typeof(*drm), acpi_nb);
  323. struct acpi_bus_event *info = data;
  324. if (!strcmp(info->device_class, ACPI_VIDEO_CLASS)) {
  325. if (info->type == ACPI_VIDEO_NOTIFY_PROBE) {
  326. /*
  327. * This may be the only indication we receive of a
  328. * connector hotplug on a runtime suspended GPU,
  329. * schedule hpd_work to check.
  330. */
  331. schedule_work(&drm->hpd_work);
  332. /* acpi-video should not generate keypresses for this */
  333. return NOTIFY_BAD;
  334. }
  335. }
  336. return NOTIFY_DONE;
  337. }
  338. #endif
  339. int
  340. nouveau_display_init(struct drm_device *dev)
  341. {
  342. struct nouveau_display *disp = nouveau_display(dev);
  343. struct nouveau_drm *drm = nouveau_drm(dev);
  344. struct drm_connector *connector;
  345. int ret;
  346. ret = disp->init(dev);
  347. if (ret)
  348. return ret;
  349. /* enable polling for external displays */
  350. drm_kms_helper_poll_enable(dev);
  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. drm_crtc_force_disable_all(dev);
  369. /* Make sure that drm and hw vblank irqs get properly disabled. */
  370. drm_for_each_crtc(crtc, dev)
  371. drm_crtc_vblank_off(crtc);
  372. /* disable flip completion events */
  373. nvif_notify_put(&drm->flip);
  374. /* disable hotplug interrupts */
  375. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  376. struct nouveau_connector *conn = nouveau_connector(connector);
  377. nvif_notify_put(&conn->hpd);
  378. }
  379. drm_kms_helper_poll_disable(dev);
  380. disp->fini(dev);
  381. }
  382. static void
  383. nouveau_display_create_properties(struct drm_device *dev)
  384. {
  385. struct nouveau_display *disp = nouveau_display(dev);
  386. int gen;
  387. if (disp->disp.oclass < NV50_DISP)
  388. gen = 0;
  389. else
  390. if (disp->disp.oclass < GF110_DISP)
  391. gen = 1;
  392. else
  393. gen = 2;
  394. PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
  395. PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
  396. PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
  397. disp->underscan_hborder_property =
  398. drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
  399. disp->underscan_vborder_property =
  400. drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
  401. if (gen < 1)
  402. return;
  403. /* -90..+90 */
  404. disp->vibrant_hue_property =
  405. drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
  406. /* -100..+100 */
  407. disp->color_vibrance_property =
  408. drm_property_create_range(dev, 0, "color vibrance", 0, 200);
  409. }
  410. int
  411. nouveau_display_create(struct drm_device *dev)
  412. {
  413. struct nouveau_drm *drm = nouveau_drm(dev);
  414. struct nvkm_device *device = nvxx_device(&drm->device);
  415. struct nouveau_display *disp;
  416. int ret;
  417. disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
  418. if (!disp)
  419. return -ENOMEM;
  420. drm_mode_config_init(dev);
  421. drm_mode_create_scaling_mode_property(dev);
  422. drm_mode_create_dvi_i_properties(dev);
  423. dev->mode_config.funcs = &nouveau_mode_config_funcs;
  424. dev->mode_config.fb_base = device->func->resource_addr(device, 1);
  425. dev->mode_config.min_width = 0;
  426. dev->mode_config.min_height = 0;
  427. if (drm->device.info.family < NV_DEVICE_INFO_V0_CELSIUS) {
  428. dev->mode_config.max_width = 2048;
  429. dev->mode_config.max_height = 2048;
  430. } else
  431. if (drm->device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  432. dev->mode_config.max_width = 4096;
  433. dev->mode_config.max_height = 4096;
  434. } else
  435. if (drm->device.info.family < NV_DEVICE_INFO_V0_FERMI) {
  436. dev->mode_config.max_width = 8192;
  437. dev->mode_config.max_height = 8192;
  438. } else {
  439. dev->mode_config.max_width = 16384;
  440. dev->mode_config.max_height = 16384;
  441. }
  442. dev->mode_config.preferred_depth = 24;
  443. dev->mode_config.prefer_shadow = 1;
  444. if (drm->device.info.chipset < 0x11)
  445. dev->mode_config.async_page_flip = false;
  446. else
  447. dev->mode_config.async_page_flip = true;
  448. drm_kms_helper_poll_init(dev);
  449. drm_kms_helper_poll_disable(dev);
  450. if (nouveau_modeset != 2 && drm->vbios.dcb.entries) {
  451. static const u16 oclass[] = {
  452. GP102_DISP,
  453. GP100_DISP,
  454. GM200_DISP,
  455. GM107_DISP,
  456. GK110_DISP,
  457. GK104_DISP,
  458. GF110_DISP,
  459. GT214_DISP,
  460. GT206_DISP,
  461. GT200_DISP,
  462. G82_DISP,
  463. NV50_DISP,
  464. NV04_DISP,
  465. };
  466. int i;
  467. for (i = 0, ret = -ENODEV; ret && i < ARRAY_SIZE(oclass); i++) {
  468. ret = nvif_object_init(&drm->device.object, 0,
  469. oclass[i], NULL, 0, &disp->disp);
  470. }
  471. if (ret == 0) {
  472. nouveau_display_create_properties(dev);
  473. if (disp->disp.oclass < NV50_DISP)
  474. ret = nv04_display_create(dev);
  475. else
  476. ret = nv50_display_create(dev);
  477. }
  478. } else {
  479. ret = 0;
  480. }
  481. if (ret)
  482. goto disp_create_err;
  483. drm_mode_config_reset(dev);
  484. if (dev->mode_config.num_crtc) {
  485. ret = nouveau_display_vblank_init(dev);
  486. if (ret)
  487. goto vblank_err;
  488. }
  489. nouveau_backlight_init(dev);
  490. INIT_WORK(&drm->hpd_work, nouveau_display_hpd_work);
  491. #ifdef CONFIG_ACPI
  492. drm->acpi_nb.notifier_call = nouveau_display_acpi_ntfy;
  493. register_acpi_notifier(&drm->acpi_nb);
  494. #endif
  495. return 0;
  496. vblank_err:
  497. disp->dtor(dev);
  498. disp_create_err:
  499. drm_kms_helper_poll_fini(dev);
  500. drm_mode_config_cleanup(dev);
  501. return ret;
  502. }
  503. void
  504. nouveau_display_destroy(struct drm_device *dev)
  505. {
  506. struct nouveau_display *disp = nouveau_display(dev);
  507. #ifdef CONFIG_ACPI
  508. unregister_acpi_notifier(&nouveau_drm(dev)->acpi_nb);
  509. #endif
  510. nouveau_backlight_exit(dev);
  511. nouveau_display_vblank_fini(dev);
  512. drm_kms_helper_poll_fini(dev);
  513. drm_mode_config_cleanup(dev);
  514. if (disp->dtor)
  515. disp->dtor(dev);
  516. nvif_object_fini(&disp->disp);
  517. nouveau_drm(dev)->display = NULL;
  518. kfree(disp);
  519. }
  520. static int
  521. nouveau_atomic_disable_connector(struct drm_atomic_state *state,
  522. struct drm_connector *connector)
  523. {
  524. struct drm_connector_state *connector_state;
  525. struct drm_crtc *crtc;
  526. struct drm_crtc_state *crtc_state;
  527. struct drm_plane_state *plane_state;
  528. struct drm_plane *plane;
  529. int ret;
  530. if (!(crtc = connector->state->crtc))
  531. return 0;
  532. connector_state = drm_atomic_get_connector_state(state, connector);
  533. if (IS_ERR(connector_state))
  534. return PTR_ERR(connector_state);
  535. ret = drm_atomic_set_crtc_for_connector(connector_state, NULL);
  536. if (ret)
  537. return ret;
  538. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  539. if (IS_ERR(crtc_state))
  540. return PTR_ERR(crtc_state);
  541. ret = drm_atomic_set_mode_for_crtc(crtc_state, NULL);
  542. if (ret)
  543. return ret;
  544. crtc_state->active = false;
  545. drm_for_each_plane_mask(plane, connector->dev, crtc_state->plane_mask) {
  546. plane_state = drm_atomic_get_plane_state(state, plane);
  547. if (IS_ERR(plane_state))
  548. return PTR_ERR(plane_state);
  549. ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
  550. if (ret)
  551. return ret;
  552. drm_atomic_set_fb_for_plane(plane_state, NULL);
  553. }
  554. return 0;
  555. }
  556. static int
  557. nouveau_atomic_disable(struct drm_device *dev,
  558. struct drm_modeset_acquire_ctx *ctx)
  559. {
  560. struct drm_atomic_state *state;
  561. struct drm_connector *connector;
  562. int ret;
  563. state = drm_atomic_state_alloc(dev);
  564. if (!state)
  565. return -ENOMEM;
  566. state->acquire_ctx = ctx;
  567. drm_for_each_connector(connector, dev) {
  568. ret = nouveau_atomic_disable_connector(state, connector);
  569. if (ret)
  570. break;
  571. }
  572. if (ret == 0)
  573. ret = drm_atomic_commit(state);
  574. drm_atomic_state_put(state);
  575. return ret;
  576. }
  577. static struct drm_atomic_state *
  578. nouveau_atomic_suspend(struct drm_device *dev)
  579. {
  580. struct drm_modeset_acquire_ctx ctx;
  581. struct drm_atomic_state *state;
  582. int ret;
  583. drm_modeset_acquire_init(&ctx, 0);
  584. retry:
  585. ret = drm_modeset_lock_all_ctx(dev, &ctx);
  586. if (ret < 0) {
  587. state = ERR_PTR(ret);
  588. goto unlock;
  589. }
  590. state = drm_atomic_helper_duplicate_state(dev, &ctx);
  591. if (IS_ERR(state))
  592. goto unlock;
  593. ret = nouveau_atomic_disable(dev, &ctx);
  594. if (ret < 0) {
  595. drm_atomic_state_put(state);
  596. state = ERR_PTR(ret);
  597. goto unlock;
  598. }
  599. unlock:
  600. if (PTR_ERR(state) == -EDEADLK) {
  601. drm_modeset_backoff(&ctx);
  602. goto retry;
  603. }
  604. drm_modeset_drop_locks(&ctx);
  605. drm_modeset_acquire_fini(&ctx);
  606. return state;
  607. }
  608. int
  609. nouveau_display_suspend(struct drm_device *dev, bool runtime)
  610. {
  611. struct nouveau_display *disp = nouveau_display(dev);
  612. struct drm_crtc *crtc;
  613. if (drm_drv_uses_atomic_modeset(dev)) {
  614. if (!runtime) {
  615. disp->suspend = nouveau_atomic_suspend(dev);
  616. if (IS_ERR(disp->suspend)) {
  617. int ret = PTR_ERR(disp->suspend);
  618. disp->suspend = NULL;
  619. return ret;
  620. }
  621. }
  622. nouveau_display_fini(dev, true);
  623. return 0;
  624. }
  625. nouveau_display_fini(dev, true);
  626. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  627. struct nouveau_framebuffer *nouveau_fb;
  628. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  629. if (!nouveau_fb || !nouveau_fb->nvbo)
  630. continue;
  631. nouveau_bo_unpin(nouveau_fb->nvbo);
  632. }
  633. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  634. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  635. if (nv_crtc->cursor.nvbo) {
  636. if (nv_crtc->cursor.set_offset)
  637. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  638. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  639. }
  640. }
  641. return 0;
  642. }
  643. void
  644. nouveau_display_resume(struct drm_device *dev, bool runtime)
  645. {
  646. struct nouveau_display *disp = nouveau_display(dev);
  647. struct nouveau_drm *drm = nouveau_drm(dev);
  648. struct drm_crtc *crtc;
  649. int ret;
  650. if (drm_drv_uses_atomic_modeset(dev)) {
  651. nouveau_display_init(dev);
  652. if (disp->suspend) {
  653. drm_atomic_helper_resume(dev, disp->suspend);
  654. disp->suspend = NULL;
  655. }
  656. return;
  657. }
  658. /* re-pin fb/cursors */
  659. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  660. struct nouveau_framebuffer *nouveau_fb;
  661. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  662. if (!nouveau_fb || !nouveau_fb->nvbo)
  663. continue;
  664. ret = nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM, true);
  665. if (ret)
  666. NV_ERROR(drm, "Could not pin framebuffer\n");
  667. }
  668. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  669. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  670. if (!nv_crtc->cursor.nvbo)
  671. continue;
  672. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM, true);
  673. if (!ret && nv_crtc->cursor.set_offset)
  674. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  675. if (ret)
  676. NV_ERROR(drm, "Could not pin/map cursor.\n");
  677. }
  678. nouveau_display_init(dev);
  679. /* Force CLUT to get re-loaded during modeset */
  680. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  681. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  682. nv_crtc->lut.depth = 0;
  683. }
  684. /* This should ensure we don't hit a locking problem when someone
  685. * wakes us up via a connector. We should never go into suspend
  686. * while the display is on anyways.
  687. */
  688. if (runtime)
  689. return;
  690. drm_helper_resume_force_mode(dev);
  691. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  692. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  693. if (!nv_crtc->cursor.nvbo)
  694. continue;
  695. if (nv_crtc->cursor.set_offset)
  696. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
  697. nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
  698. nv_crtc->cursor_saved_y);
  699. }
  700. }
  701. static int
  702. nouveau_page_flip_emit(struct nouveau_channel *chan,
  703. struct nouveau_bo *old_bo,
  704. struct nouveau_bo *new_bo,
  705. struct nouveau_page_flip_state *s,
  706. struct nouveau_fence **pfence)
  707. {
  708. struct nouveau_fence_chan *fctx = chan->fence;
  709. struct nouveau_drm *drm = chan->drm;
  710. struct drm_device *dev = drm->dev;
  711. unsigned long flags;
  712. int ret;
  713. /* Queue it to the pending list */
  714. spin_lock_irqsave(&dev->event_lock, flags);
  715. list_add_tail(&s->head, &fctx->flip);
  716. spin_unlock_irqrestore(&dev->event_lock, flags);
  717. /* Synchronize with the old framebuffer */
  718. ret = nouveau_fence_sync(old_bo, chan, false, false);
  719. if (ret)
  720. goto fail;
  721. /* Emit the pageflip */
  722. ret = RING_SPACE(chan, 2);
  723. if (ret)
  724. goto fail;
  725. BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  726. OUT_RING (chan, 0x00000000);
  727. FIRE_RING (chan);
  728. ret = nouveau_fence_new(chan, false, pfence);
  729. if (ret)
  730. goto fail;
  731. return 0;
  732. fail:
  733. spin_lock_irqsave(&dev->event_lock, flags);
  734. list_del(&s->head);
  735. spin_unlock_irqrestore(&dev->event_lock, flags);
  736. return ret;
  737. }
  738. int
  739. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  740. struct drm_pending_vblank_event *event, u32 flags)
  741. {
  742. const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
  743. struct drm_device *dev = crtc->dev;
  744. struct nouveau_drm *drm = nouveau_drm(dev);
  745. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->primary->fb)->nvbo;
  746. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  747. struct nouveau_page_flip_state *s;
  748. struct nouveau_channel *chan;
  749. struct nouveau_cli *cli;
  750. struct nouveau_fence *fence;
  751. struct nv04_display *dispnv04 = nv04_display(dev);
  752. int head = nouveau_crtc(crtc)->index;
  753. int ret;
  754. chan = drm->channel;
  755. if (!chan)
  756. return -ENODEV;
  757. cli = (void *)chan->user.client;
  758. s = kzalloc(sizeof(*s), GFP_KERNEL);
  759. if (!s)
  760. return -ENOMEM;
  761. if (new_bo != old_bo) {
  762. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM, true);
  763. if (ret)
  764. goto fail_free;
  765. }
  766. mutex_lock(&cli->mutex);
  767. ret = ttm_bo_reserve(&new_bo->bo, true, false, NULL);
  768. if (ret)
  769. goto fail_unpin;
  770. /* synchronise rendering channel with the kernel's channel */
  771. ret = nouveau_fence_sync(new_bo, chan, false, true);
  772. if (ret) {
  773. ttm_bo_unreserve(&new_bo->bo);
  774. goto fail_unpin;
  775. }
  776. if (new_bo != old_bo) {
  777. ttm_bo_unreserve(&new_bo->bo);
  778. ret = ttm_bo_reserve(&old_bo->bo, true, false, NULL);
  779. if (ret)
  780. goto fail_unpin;
  781. }
  782. /* Initialize a page flip struct */
  783. *s = (struct nouveau_page_flip_state)
  784. { { }, event, crtc, fb->format->cpp[0] * 8, fb->pitches[0],
  785. new_bo->bo.offset };
  786. /* Keep vblanks on during flip, for the target crtc of this flip */
  787. drm_crtc_vblank_get(crtc);
  788. /* Emit a page flip */
  789. if (swap_interval) {
  790. ret = RING_SPACE(chan, 8);
  791. if (ret)
  792. goto fail_unreserve;
  793. BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
  794. OUT_RING (chan, 0);
  795. BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
  796. OUT_RING (chan, head);
  797. BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
  798. OUT_RING (chan, 0);
  799. BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
  800. OUT_RING (chan, 0);
  801. }
  802. nouveau_bo_ref(new_bo, &dispnv04->image[head]);
  803. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  804. if (ret)
  805. goto fail_unreserve;
  806. mutex_unlock(&cli->mutex);
  807. /* Update the crtc struct and cleanup */
  808. crtc->primary->fb = fb;
  809. nouveau_bo_fence(old_bo, fence, false);
  810. ttm_bo_unreserve(&old_bo->bo);
  811. if (old_bo != new_bo)
  812. nouveau_bo_unpin(old_bo);
  813. nouveau_fence_unref(&fence);
  814. return 0;
  815. fail_unreserve:
  816. drm_crtc_vblank_put(crtc);
  817. ttm_bo_unreserve(&old_bo->bo);
  818. fail_unpin:
  819. mutex_unlock(&cli->mutex);
  820. if (old_bo != new_bo)
  821. nouveau_bo_unpin(new_bo);
  822. fail_free:
  823. kfree(s);
  824. return ret;
  825. }
  826. int
  827. nouveau_finish_page_flip(struct nouveau_channel *chan,
  828. struct nouveau_page_flip_state *ps)
  829. {
  830. struct nouveau_fence_chan *fctx = chan->fence;
  831. struct nouveau_drm *drm = chan->drm;
  832. struct drm_device *dev = drm->dev;
  833. struct nouveau_page_flip_state *s;
  834. unsigned long flags;
  835. spin_lock_irqsave(&dev->event_lock, flags);
  836. if (list_empty(&fctx->flip)) {
  837. NV_ERROR(drm, "unexpected pageflip\n");
  838. spin_unlock_irqrestore(&dev->event_lock, flags);
  839. return -EINVAL;
  840. }
  841. s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
  842. if (s->event) {
  843. drm_crtc_arm_vblank_event(s->crtc, s->event);
  844. } else {
  845. /* Give up ownership of vblank for page-flipped crtc */
  846. drm_crtc_vblank_put(s->crtc);
  847. }
  848. list_del(&s->head);
  849. if (ps)
  850. *ps = *s;
  851. kfree(s);
  852. spin_unlock_irqrestore(&dev->event_lock, flags);
  853. return 0;
  854. }
  855. int
  856. nouveau_flip_complete(struct nvif_notify *notify)
  857. {
  858. struct nouveau_drm *drm = container_of(notify, typeof(*drm), flip);
  859. struct nouveau_channel *chan = drm->channel;
  860. struct nouveau_page_flip_state state;
  861. if (!nouveau_finish_page_flip(chan, &state)) {
  862. nv_set_crtc_base(drm->dev, drm_crtc_index(state.crtc),
  863. state.offset + state.crtc->y *
  864. state.pitch + state.crtc->x *
  865. state.bpp / 8);
  866. }
  867. return NVIF_NOTIFY_KEEP;
  868. }
  869. int
  870. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  871. struct drm_mode_create_dumb *args)
  872. {
  873. struct nouveau_bo *bo;
  874. uint32_t domain;
  875. int ret;
  876. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  877. args->size = args->pitch * args->height;
  878. args->size = roundup(args->size, PAGE_SIZE);
  879. /* Use VRAM if there is any ; otherwise fallback to system memory */
  880. if (nouveau_drm(dev)->device.info.ram_size != 0)
  881. domain = NOUVEAU_GEM_DOMAIN_VRAM;
  882. else
  883. domain = NOUVEAU_GEM_DOMAIN_GART;
  884. ret = nouveau_gem_new(dev, args->size, 0, domain, 0, 0, &bo);
  885. if (ret)
  886. return ret;
  887. ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
  888. drm_gem_object_unreference_unlocked(&bo->gem);
  889. return ret;
  890. }
  891. int
  892. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  893. struct drm_device *dev,
  894. uint32_t handle, uint64_t *poffset)
  895. {
  896. struct drm_gem_object *gem;
  897. gem = drm_gem_object_lookup(file_priv, handle);
  898. if (gem) {
  899. struct nouveau_bo *bo = nouveau_gem_object(gem);
  900. *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
  901. drm_gem_object_unreference_unlocked(gem);
  902. return 0;
  903. }
  904. return -ENOENT;
  905. }