nouveau_display.c 25 KB

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