nouveau_display.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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.object, 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.object,
  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_put_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_put_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 = nouveau_fbcon_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. while (p && l->gen_mask) { \
  290. if (l->gen_mask & (1 << (gen))) { \
  291. drm_property_add_enum(p, l->type, l->name); \
  292. } \
  293. l++; \
  294. } \
  295. } \
  296. } while(0)
  297. static void
  298. nouveau_display_hpd_work(struct work_struct *work)
  299. {
  300. struct nouveau_drm *drm = container_of(work, typeof(*drm), hpd_work);
  301. pm_runtime_get_sync(drm->dev->dev);
  302. drm_helper_hpd_irq_event(drm->dev);
  303. pm_runtime_mark_last_busy(drm->dev->dev);
  304. pm_runtime_put_sync(drm->dev->dev);
  305. }
  306. #ifdef CONFIG_ACPI
  307. /*
  308. * Hans de Goede: This define belongs in acpi/video.h, I've submitted a patch
  309. * to the acpi subsys to move it there from drivers/acpi/acpi_video.c .
  310. * This should be dropped once that is merged.
  311. */
  312. #ifndef ACPI_VIDEO_NOTIFY_PROBE
  313. #define ACPI_VIDEO_NOTIFY_PROBE 0x81
  314. #endif
  315. static int
  316. nouveau_display_acpi_ntfy(struct notifier_block *nb, unsigned long val,
  317. void *data)
  318. {
  319. struct nouveau_drm *drm = container_of(nb, typeof(*drm), acpi_nb);
  320. struct acpi_bus_event *info = data;
  321. int ret;
  322. if (!strcmp(info->device_class, ACPI_VIDEO_CLASS)) {
  323. if (info->type == ACPI_VIDEO_NOTIFY_PROBE) {
  324. ret = pm_runtime_get(drm->dev->dev);
  325. if (ret == 1 || ret == -EACCES) {
  326. /* If the GPU is already awake, or in a state
  327. * where we can't wake it up, it can handle
  328. * it's own hotplug events.
  329. */
  330. pm_runtime_put_autosuspend(drm->dev->dev);
  331. } else if (ret == 0) {
  332. /* This may be the only indication we receive
  333. * of a connector hotplug on a runtime
  334. * suspended GPU, schedule hpd_work to check.
  335. */
  336. NV_DEBUG(drm, "ACPI requested connector reprobe\n");
  337. schedule_work(&drm->hpd_work);
  338. pm_runtime_put_noidle(drm->dev->dev);
  339. } else {
  340. NV_WARN(drm, "Dropped ACPI reprobe event due to RPM error: %d\n",
  341. ret);
  342. }
  343. /* acpi-video should not generate keypresses for this */
  344. return NOTIFY_BAD;
  345. }
  346. }
  347. return NOTIFY_DONE;
  348. }
  349. #endif
  350. int
  351. nouveau_display_init(struct drm_device *dev)
  352. {
  353. struct nouveau_display *disp = nouveau_display(dev);
  354. struct nouveau_drm *drm = nouveau_drm(dev);
  355. struct drm_connector *connector;
  356. struct drm_connector_list_iter conn_iter;
  357. int ret;
  358. ret = disp->init(dev);
  359. if (ret)
  360. return ret;
  361. /* enable connector detection and polling for connectors without HPD
  362. * support
  363. */
  364. drm_kms_helper_poll_enable(dev);
  365. /* enable hotplug interrupts */
  366. drm_connector_list_iter_begin(dev, &conn_iter);
  367. nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
  368. struct nouveau_connector *conn = nouveau_connector(connector);
  369. nvif_notify_get(&conn->hpd);
  370. }
  371. drm_connector_list_iter_end(&conn_iter);
  372. /* enable flip completion events */
  373. nvif_notify_get(&drm->flip);
  374. return ret;
  375. }
  376. void
  377. nouveau_display_fini(struct drm_device *dev, bool suspend, bool runtime)
  378. {
  379. struct nouveau_display *disp = nouveau_display(dev);
  380. struct nouveau_drm *drm = nouveau_drm(dev);
  381. struct drm_connector *connector;
  382. struct drm_connector_list_iter conn_iter;
  383. if (!suspend) {
  384. if (drm_drv_uses_atomic_modeset(dev))
  385. drm_atomic_helper_shutdown(dev);
  386. else
  387. drm_crtc_force_disable_all(dev);
  388. }
  389. /* disable flip completion events */
  390. nvif_notify_put(&drm->flip);
  391. /* disable hotplug interrupts */
  392. drm_connector_list_iter_begin(dev, &conn_iter);
  393. nouveau_for_each_non_mst_connector_iter(connector, &conn_iter) {
  394. struct nouveau_connector *conn = nouveau_connector(connector);
  395. nvif_notify_put(&conn->hpd);
  396. }
  397. drm_connector_list_iter_end(&conn_iter);
  398. if (!runtime)
  399. cancel_work_sync(&drm->hpd_work);
  400. drm_kms_helper_poll_disable(dev);
  401. disp->fini(dev);
  402. }
  403. static void
  404. nouveau_display_create_properties(struct drm_device *dev)
  405. {
  406. struct nouveau_display *disp = nouveau_display(dev);
  407. int gen;
  408. if (disp->disp.object.oclass < NV50_DISP)
  409. gen = 0;
  410. else
  411. if (disp->disp.object.oclass < GF110_DISP)
  412. gen = 1;
  413. else
  414. gen = 2;
  415. PROP_ENUM(disp->dithering_mode, gen, "dithering mode", dither_mode);
  416. PROP_ENUM(disp->dithering_depth, gen, "dithering depth", dither_depth);
  417. PROP_ENUM(disp->underscan_property, gen, "underscan", underscan);
  418. disp->underscan_hborder_property =
  419. drm_property_create_range(dev, 0, "underscan hborder", 0, 128);
  420. disp->underscan_vborder_property =
  421. drm_property_create_range(dev, 0, "underscan vborder", 0, 128);
  422. if (gen < 1)
  423. return;
  424. /* -90..+90 */
  425. disp->vibrant_hue_property =
  426. drm_property_create_range(dev, 0, "vibrant hue", 0, 180);
  427. /* -100..+100 */
  428. disp->color_vibrance_property =
  429. drm_property_create_range(dev, 0, "color vibrance", 0, 200);
  430. }
  431. int
  432. nouveau_display_create(struct drm_device *dev)
  433. {
  434. struct nouveau_drm *drm = nouveau_drm(dev);
  435. struct nvkm_device *device = nvxx_device(&drm->client.device);
  436. struct nouveau_display *disp;
  437. int ret;
  438. disp = drm->display = kzalloc(sizeof(*disp), GFP_KERNEL);
  439. if (!disp)
  440. return -ENOMEM;
  441. drm_mode_config_init(dev);
  442. drm_mode_create_scaling_mode_property(dev);
  443. drm_mode_create_dvi_i_properties(dev);
  444. dev->mode_config.funcs = &nouveau_mode_config_funcs;
  445. dev->mode_config.fb_base = device->func->resource_addr(device, 1);
  446. dev->mode_config.min_width = 0;
  447. dev->mode_config.min_height = 0;
  448. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_CELSIUS) {
  449. dev->mode_config.max_width = 2048;
  450. dev->mode_config.max_height = 2048;
  451. } else
  452. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_TESLA) {
  453. dev->mode_config.max_width = 4096;
  454. dev->mode_config.max_height = 4096;
  455. } else
  456. if (drm->client.device.info.family < NV_DEVICE_INFO_V0_FERMI) {
  457. dev->mode_config.max_width = 8192;
  458. dev->mode_config.max_height = 8192;
  459. } else {
  460. dev->mode_config.max_width = 16384;
  461. dev->mode_config.max_height = 16384;
  462. }
  463. dev->mode_config.preferred_depth = 24;
  464. dev->mode_config.prefer_shadow = 1;
  465. if (drm->client.device.info.chipset < 0x11)
  466. dev->mode_config.async_page_flip = false;
  467. else
  468. dev->mode_config.async_page_flip = true;
  469. drm_kms_helper_poll_init(dev);
  470. drm_kms_helper_poll_disable(dev);
  471. if (nouveau_modeset != 2 && drm->vbios.dcb.entries) {
  472. ret = nvif_disp_ctor(&drm->client.device, 0, &disp->disp);
  473. if (ret == 0) {
  474. nouveau_display_create_properties(dev);
  475. if (disp->disp.object.oclass < NV50_DISP)
  476. ret = nv04_display_create(dev);
  477. else
  478. ret = nv50_display_create(dev);
  479. }
  480. } else {
  481. ret = 0;
  482. }
  483. if (ret)
  484. goto disp_create_err;
  485. drm_mode_config_reset(dev);
  486. if (dev->mode_config.num_crtc) {
  487. ret = nouveau_display_vblank_init(dev);
  488. if (ret)
  489. goto vblank_err;
  490. }
  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_display_vblank_fini(dev);
  512. drm_kms_helper_poll_fini(dev);
  513. drm_mode_config_cleanup(dev);
  514. if (disp->dtor)
  515. disp->dtor(dev);
  516. nvif_disp_dtor(&disp->disp);
  517. nouveau_drm(dev)->display = NULL;
  518. kfree(disp);
  519. }
  520. int
  521. nouveau_display_suspend(struct drm_device *dev, bool runtime)
  522. {
  523. struct nouveau_display *disp = nouveau_display(dev);
  524. struct drm_crtc *crtc;
  525. if (drm_drv_uses_atomic_modeset(dev)) {
  526. if (!runtime) {
  527. disp->suspend = drm_atomic_helper_suspend(dev);
  528. if (IS_ERR(disp->suspend)) {
  529. int ret = PTR_ERR(disp->suspend);
  530. disp->suspend = NULL;
  531. return ret;
  532. }
  533. }
  534. nouveau_display_fini(dev, true, runtime);
  535. return 0;
  536. }
  537. nouveau_display_fini(dev, true, runtime);
  538. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  539. struct nouveau_framebuffer *nouveau_fb;
  540. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  541. if (!nouveau_fb || !nouveau_fb->nvbo)
  542. continue;
  543. nouveau_bo_unpin(nouveau_fb->nvbo);
  544. }
  545. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  546. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  547. if (nv_crtc->cursor.nvbo) {
  548. if (nv_crtc->cursor.set_offset)
  549. nouveau_bo_unmap(nv_crtc->cursor.nvbo);
  550. nouveau_bo_unpin(nv_crtc->cursor.nvbo);
  551. }
  552. }
  553. return 0;
  554. }
  555. void
  556. nouveau_display_resume(struct drm_device *dev, bool runtime)
  557. {
  558. struct nouveau_display *disp = nouveau_display(dev);
  559. struct nouveau_drm *drm = nouveau_drm(dev);
  560. struct drm_crtc *crtc;
  561. int ret;
  562. if (drm_drv_uses_atomic_modeset(dev)) {
  563. nouveau_display_init(dev);
  564. if (disp->suspend) {
  565. drm_atomic_helper_resume(dev, disp->suspend);
  566. disp->suspend = NULL;
  567. }
  568. return;
  569. }
  570. /* re-pin fb/cursors */
  571. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  572. struct nouveau_framebuffer *nouveau_fb;
  573. nouveau_fb = nouveau_framebuffer(crtc->primary->fb);
  574. if (!nouveau_fb || !nouveau_fb->nvbo)
  575. continue;
  576. ret = nouveau_bo_pin(nouveau_fb->nvbo, TTM_PL_FLAG_VRAM, true);
  577. if (ret)
  578. NV_ERROR(drm, "Could not pin framebuffer\n");
  579. }
  580. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  581. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  582. if (!nv_crtc->cursor.nvbo)
  583. continue;
  584. ret = nouveau_bo_pin(nv_crtc->cursor.nvbo, TTM_PL_FLAG_VRAM, true);
  585. if (!ret && nv_crtc->cursor.set_offset)
  586. ret = nouveau_bo_map(nv_crtc->cursor.nvbo);
  587. if (ret)
  588. NV_ERROR(drm, "Could not pin/map cursor.\n");
  589. }
  590. nouveau_display_init(dev);
  591. /* Force CLUT to get re-loaded during modeset */
  592. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  593. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  594. nv_crtc->lut.depth = 0;
  595. }
  596. /* This should ensure we don't hit a locking problem when someone
  597. * wakes us up via a connector. We should never go into suspend
  598. * while the display is on anyways.
  599. */
  600. if (runtime)
  601. return;
  602. drm_helper_resume_force_mode(dev);
  603. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
  604. struct nouveau_crtc *nv_crtc = nouveau_crtc(crtc);
  605. if (!nv_crtc->cursor.nvbo)
  606. continue;
  607. if (nv_crtc->cursor.set_offset)
  608. nv_crtc->cursor.set_offset(nv_crtc, nv_crtc->cursor.nvbo->bo.offset);
  609. nv_crtc->cursor.set_pos(nv_crtc, nv_crtc->cursor_saved_x,
  610. nv_crtc->cursor_saved_y);
  611. }
  612. }
  613. static int
  614. nouveau_page_flip_emit(struct nouveau_channel *chan,
  615. struct nouveau_bo *old_bo,
  616. struct nouveau_bo *new_bo,
  617. struct nouveau_page_flip_state *s,
  618. struct nouveau_fence **pfence)
  619. {
  620. struct nouveau_fence_chan *fctx = chan->fence;
  621. struct nouveau_drm *drm = chan->drm;
  622. struct drm_device *dev = drm->dev;
  623. unsigned long flags;
  624. int ret;
  625. /* Queue it to the pending list */
  626. spin_lock_irqsave(&dev->event_lock, flags);
  627. list_add_tail(&s->head, &fctx->flip);
  628. spin_unlock_irqrestore(&dev->event_lock, flags);
  629. /* Synchronize with the old framebuffer */
  630. ret = nouveau_fence_sync(old_bo, chan, false, false);
  631. if (ret)
  632. goto fail;
  633. /* Emit the pageflip */
  634. ret = RING_SPACE(chan, 2);
  635. if (ret)
  636. goto fail;
  637. BEGIN_NV04(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  638. OUT_RING (chan, 0x00000000);
  639. FIRE_RING (chan);
  640. ret = nouveau_fence_new(chan, false, pfence);
  641. if (ret)
  642. goto fail;
  643. return 0;
  644. fail:
  645. spin_lock_irqsave(&dev->event_lock, flags);
  646. list_del(&s->head);
  647. spin_unlock_irqrestore(&dev->event_lock, flags);
  648. return ret;
  649. }
  650. int
  651. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  652. struct drm_pending_vblank_event *event, u32 flags,
  653. struct drm_modeset_acquire_ctx *ctx)
  654. {
  655. const int swap_interval = (flags & DRM_MODE_PAGE_FLIP_ASYNC) ? 0 : 1;
  656. struct drm_device *dev = crtc->dev;
  657. struct nouveau_drm *drm = nouveau_drm(dev);
  658. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->primary->fb)->nvbo;
  659. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  660. struct nouveau_page_flip_state *s;
  661. struct nouveau_channel *chan;
  662. struct nouveau_cli *cli;
  663. struct nouveau_fence *fence;
  664. struct nv04_display *dispnv04 = nv04_display(dev);
  665. int head = nouveau_crtc(crtc)->index;
  666. int ret;
  667. chan = drm->channel;
  668. if (!chan)
  669. return -ENODEV;
  670. cli = (void *)chan->user.client;
  671. s = kzalloc(sizeof(*s), GFP_KERNEL);
  672. if (!s)
  673. return -ENOMEM;
  674. if (new_bo != old_bo) {
  675. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM, true);
  676. if (ret)
  677. goto fail_free;
  678. }
  679. mutex_lock(&cli->mutex);
  680. ret = ttm_bo_reserve(&new_bo->bo, true, false, NULL);
  681. if (ret)
  682. goto fail_unpin;
  683. /* synchronise rendering channel with the kernel's channel */
  684. ret = nouveau_fence_sync(new_bo, chan, false, true);
  685. if (ret) {
  686. ttm_bo_unreserve(&new_bo->bo);
  687. goto fail_unpin;
  688. }
  689. if (new_bo != old_bo) {
  690. ttm_bo_unreserve(&new_bo->bo);
  691. ret = ttm_bo_reserve(&old_bo->bo, true, false, NULL);
  692. if (ret)
  693. goto fail_unpin;
  694. }
  695. /* Initialize a page flip struct */
  696. *s = (struct nouveau_page_flip_state)
  697. { { }, event, crtc, fb->format->cpp[0] * 8, fb->pitches[0],
  698. new_bo->bo.offset };
  699. /* Keep vblanks on during flip, for the target crtc of this flip */
  700. drm_crtc_vblank_get(crtc);
  701. /* Emit a page flip */
  702. if (swap_interval) {
  703. ret = RING_SPACE(chan, 8);
  704. if (ret)
  705. goto fail_unreserve;
  706. BEGIN_NV04(chan, NvSubImageBlit, 0x012c, 1);
  707. OUT_RING (chan, 0);
  708. BEGIN_NV04(chan, NvSubImageBlit, 0x0134, 1);
  709. OUT_RING (chan, head);
  710. BEGIN_NV04(chan, NvSubImageBlit, 0x0100, 1);
  711. OUT_RING (chan, 0);
  712. BEGIN_NV04(chan, NvSubImageBlit, 0x0130, 1);
  713. OUT_RING (chan, 0);
  714. }
  715. nouveau_bo_ref(new_bo, &dispnv04->image[head]);
  716. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  717. if (ret)
  718. goto fail_unreserve;
  719. mutex_unlock(&cli->mutex);
  720. /* Update the crtc struct and cleanup */
  721. crtc->primary->fb = fb;
  722. nouveau_bo_fence(old_bo, fence, false);
  723. ttm_bo_unreserve(&old_bo->bo);
  724. if (old_bo != new_bo)
  725. nouveau_bo_unpin(old_bo);
  726. nouveau_fence_unref(&fence);
  727. return 0;
  728. fail_unreserve:
  729. drm_crtc_vblank_put(crtc);
  730. ttm_bo_unreserve(&old_bo->bo);
  731. fail_unpin:
  732. mutex_unlock(&cli->mutex);
  733. if (old_bo != new_bo)
  734. nouveau_bo_unpin(new_bo);
  735. fail_free:
  736. kfree(s);
  737. return ret;
  738. }
  739. int
  740. nouveau_finish_page_flip(struct nouveau_channel *chan,
  741. struct nouveau_page_flip_state *ps)
  742. {
  743. struct nouveau_fence_chan *fctx = chan->fence;
  744. struct nouveau_drm *drm = chan->drm;
  745. struct drm_device *dev = drm->dev;
  746. struct nouveau_page_flip_state *s;
  747. unsigned long flags;
  748. spin_lock_irqsave(&dev->event_lock, flags);
  749. if (list_empty(&fctx->flip)) {
  750. NV_ERROR(drm, "unexpected pageflip\n");
  751. spin_unlock_irqrestore(&dev->event_lock, flags);
  752. return -EINVAL;
  753. }
  754. s = list_first_entry(&fctx->flip, struct nouveau_page_flip_state, head);
  755. if (s->event) {
  756. drm_crtc_arm_vblank_event(s->crtc, s->event);
  757. } else {
  758. /* Give up ownership of vblank for page-flipped crtc */
  759. drm_crtc_vblank_put(s->crtc);
  760. }
  761. list_del(&s->head);
  762. if (ps)
  763. *ps = *s;
  764. kfree(s);
  765. spin_unlock_irqrestore(&dev->event_lock, flags);
  766. return 0;
  767. }
  768. int
  769. nouveau_flip_complete(struct nvif_notify *notify)
  770. {
  771. struct nouveau_drm *drm = container_of(notify, typeof(*drm), flip);
  772. struct nouveau_channel *chan = drm->channel;
  773. struct nouveau_page_flip_state state;
  774. if (!nouveau_finish_page_flip(chan, &state)) {
  775. nv_set_crtc_base(drm->dev, drm_crtc_index(state.crtc),
  776. state.offset + state.crtc->y *
  777. state.pitch + state.crtc->x *
  778. state.bpp / 8);
  779. }
  780. return NVIF_NOTIFY_KEEP;
  781. }
  782. int
  783. nouveau_display_dumb_create(struct drm_file *file_priv, struct drm_device *dev,
  784. struct drm_mode_create_dumb *args)
  785. {
  786. struct nouveau_cli *cli = nouveau_cli(file_priv);
  787. struct nouveau_bo *bo;
  788. uint32_t domain;
  789. int ret;
  790. args->pitch = roundup(args->width * (args->bpp / 8), 256);
  791. args->size = args->pitch * args->height;
  792. args->size = roundup(args->size, PAGE_SIZE);
  793. /* Use VRAM if there is any ; otherwise fallback to system memory */
  794. if (nouveau_drm(dev)->client.device.info.ram_size != 0)
  795. domain = NOUVEAU_GEM_DOMAIN_VRAM;
  796. else
  797. domain = NOUVEAU_GEM_DOMAIN_GART;
  798. ret = nouveau_gem_new(cli, args->size, 0, domain, 0, 0, &bo);
  799. if (ret)
  800. return ret;
  801. ret = drm_gem_handle_create(file_priv, &bo->gem, &args->handle);
  802. drm_gem_object_put_unlocked(&bo->gem);
  803. return ret;
  804. }
  805. int
  806. nouveau_display_dumb_map_offset(struct drm_file *file_priv,
  807. struct drm_device *dev,
  808. uint32_t handle, uint64_t *poffset)
  809. {
  810. struct drm_gem_object *gem;
  811. gem = drm_gem_object_lookup(file_priv, handle);
  812. if (gem) {
  813. struct nouveau_bo *bo = nouveau_gem_object(gem);
  814. *poffset = drm_vma_node_offset_addr(&bo->bo.vma_node);
  815. drm_gem_object_put_unlocked(gem);
  816. return 0;
  817. }
  818. return -ENOENT;
  819. }