nouveau_display.c 25 KB

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