nouveau_display.c 27 KB

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