nouveau_display.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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 "drmP.h"
  27. #include "drm_crtc_helper.h"
  28. #include "nouveau_drv.h"
  29. #include "nouveau_fb.h"
  30. #include "nouveau_fbcon.h"
  31. #include "nouveau_hw.h"
  32. #include "nouveau_crtc.h"
  33. #include "nouveau_dma.h"
  34. #include "nv50_display.h"
  35. static void
  36. nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
  37. {
  38. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  39. if (fb->nvbo)
  40. drm_gem_object_unreference_unlocked(fb->nvbo->gem);
  41. drm_framebuffer_cleanup(drm_fb);
  42. kfree(fb);
  43. }
  44. static int
  45. nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
  46. struct drm_file *file_priv,
  47. unsigned int *handle)
  48. {
  49. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  50. return drm_gem_handle_create(file_priv, fb->nvbo->gem, handle);
  51. }
  52. static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
  53. .destroy = nouveau_user_framebuffer_destroy,
  54. .create_handle = nouveau_user_framebuffer_create_handle,
  55. };
  56. int
  57. nouveau_framebuffer_init(struct drm_device *dev,
  58. struct nouveau_framebuffer *nv_fb,
  59. struct drm_mode_fb_cmd *mode_cmd,
  60. struct nouveau_bo *nvbo)
  61. {
  62. struct drm_nouveau_private *dev_priv = dev->dev_private;
  63. struct drm_framebuffer *fb = &nv_fb->base;
  64. int ret;
  65. ret = drm_framebuffer_init(dev, fb, &nouveau_framebuffer_funcs);
  66. if (ret) {
  67. return ret;
  68. }
  69. drm_helper_mode_fill_fb_struct(fb, mode_cmd);
  70. nv_fb->nvbo = nvbo;
  71. if (dev_priv->card_type >= NV_50) {
  72. u32 tile_flags = nouveau_bo_tile_layout(nvbo);
  73. if (tile_flags == 0x7a00 ||
  74. tile_flags == 0xfe00)
  75. nv_fb->r_dma = NvEvoFB32;
  76. else
  77. if (tile_flags == 0x7000)
  78. nv_fb->r_dma = NvEvoFB16;
  79. else
  80. nv_fb->r_dma = NvEvoVRAM_LP;
  81. switch (fb->depth) {
  82. case 8: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_8; break;
  83. case 15: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_15; break;
  84. case 16: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_16; break;
  85. case 24:
  86. case 32: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_24; break;
  87. case 30: nv_fb->r_format = NV50_EVO_CRTC_FB_DEPTH_30; break;
  88. default:
  89. NV_ERROR(dev, "unknown depth %d\n", fb->depth);
  90. return -EINVAL;
  91. }
  92. if (dev_priv->chipset == 0x50)
  93. nv_fb->r_format |= (tile_flags << 8);
  94. if (!tile_flags)
  95. nv_fb->r_pitch = 0x00100000 | fb->pitch;
  96. else {
  97. u32 mode = nvbo->tile_mode;
  98. if (dev_priv->card_type >= NV_C0)
  99. mode >>= 4;
  100. nv_fb->r_pitch = ((fb->pitch / 4) << 4) | mode;
  101. }
  102. }
  103. return 0;
  104. }
  105. static struct drm_framebuffer *
  106. nouveau_user_framebuffer_create(struct drm_device *dev,
  107. struct drm_file *file_priv,
  108. struct drm_mode_fb_cmd *mode_cmd)
  109. {
  110. struct nouveau_framebuffer *nouveau_fb;
  111. struct drm_gem_object *gem;
  112. int ret;
  113. gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  114. if (!gem)
  115. return ERR_PTR(-ENOENT);
  116. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  117. if (!nouveau_fb)
  118. return ERR_PTR(-ENOMEM);
  119. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  120. if (ret) {
  121. drm_gem_object_unreference(gem);
  122. return ERR_PTR(ret);
  123. }
  124. return &nouveau_fb->base;
  125. }
  126. const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  127. .fb_create = nouveau_user_framebuffer_create,
  128. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  129. };
  130. int
  131. nouveau_vblank_enable(struct drm_device *dev, int crtc)
  132. {
  133. struct drm_nouveau_private *dev_priv = dev->dev_private;
  134. if (dev_priv->card_type >= NV_50)
  135. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
  136. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc));
  137. else
  138. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
  139. NV_PCRTC_INTR_0_VBLANK);
  140. return 0;
  141. }
  142. void
  143. nouveau_vblank_disable(struct drm_device *dev, int crtc)
  144. {
  145. struct drm_nouveau_private *dev_priv = dev->dev_private;
  146. if (dev_priv->card_type >= NV_50)
  147. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1,
  148. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0);
  149. else
  150. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
  151. }
  152. static int
  153. nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
  154. struct nouveau_bo *new_bo)
  155. {
  156. int ret;
  157. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
  158. if (ret)
  159. return ret;
  160. ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
  161. if (ret)
  162. goto fail;
  163. ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
  164. if (ret)
  165. goto fail_unreserve;
  166. return 0;
  167. fail_unreserve:
  168. ttm_bo_unreserve(&new_bo->bo);
  169. fail:
  170. nouveau_bo_unpin(new_bo);
  171. return ret;
  172. }
  173. static void
  174. nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
  175. struct nouveau_bo *new_bo,
  176. struct nouveau_fence *fence)
  177. {
  178. nouveau_bo_fence(new_bo, fence);
  179. ttm_bo_unreserve(&new_bo->bo);
  180. nouveau_bo_fence(old_bo, fence);
  181. ttm_bo_unreserve(&old_bo->bo);
  182. nouveau_bo_unpin(old_bo);
  183. }
  184. static int
  185. nouveau_page_flip_emit(struct nouveau_channel *chan,
  186. struct nouveau_bo *old_bo,
  187. struct nouveau_bo *new_bo,
  188. struct nouveau_page_flip_state *s,
  189. struct nouveau_fence **pfence)
  190. {
  191. struct drm_nouveau_private *dev_priv = chan->dev->dev_private;
  192. struct drm_device *dev = chan->dev;
  193. unsigned long flags;
  194. int ret;
  195. /* Queue it to the pending list */
  196. spin_lock_irqsave(&dev->event_lock, flags);
  197. list_add_tail(&s->head, &chan->nvsw.flip);
  198. spin_unlock_irqrestore(&dev->event_lock, flags);
  199. /* Synchronize with the old framebuffer */
  200. ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
  201. if (ret)
  202. goto fail;
  203. /* Emit the pageflip */
  204. ret = RING_SPACE(chan, 2);
  205. if (ret)
  206. goto fail;
  207. if (dev_priv->card_type < NV_C0)
  208. BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  209. else
  210. BEGIN_NVC0(chan, 2, NvSubM2MF, 0x0500, 1);
  211. OUT_RING (chan, 0);
  212. FIRE_RING (chan);
  213. ret = nouveau_fence_new(chan, pfence, true);
  214. if (ret)
  215. goto fail;
  216. return 0;
  217. fail:
  218. spin_lock_irqsave(&dev->event_lock, flags);
  219. list_del(&s->head);
  220. spin_unlock_irqrestore(&dev->event_lock, flags);
  221. return ret;
  222. }
  223. int
  224. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  225. struct drm_pending_vblank_event *event)
  226. {
  227. struct drm_device *dev = crtc->dev;
  228. struct drm_nouveau_private *dev_priv = dev->dev_private;
  229. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
  230. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  231. struct nouveau_page_flip_state *s;
  232. struct nouveau_channel *chan;
  233. struct nouveau_fence *fence;
  234. int ret;
  235. if (!dev_priv->channel)
  236. return -ENODEV;
  237. s = kzalloc(sizeof(*s), GFP_KERNEL);
  238. if (!s)
  239. return -ENOMEM;
  240. /* Don't let the buffers go away while we flip */
  241. ret = nouveau_page_flip_reserve(old_bo, new_bo);
  242. if (ret)
  243. goto fail_free;
  244. /* Initialize a page flip struct */
  245. *s = (struct nouveau_page_flip_state)
  246. { { }, event, nouveau_crtc(crtc)->index,
  247. fb->bits_per_pixel, fb->pitch, crtc->x, crtc->y,
  248. new_bo->bo.offset };
  249. /* Choose the channel the flip will be handled in */
  250. chan = nouveau_fence_channel(new_bo->bo.sync_obj);
  251. if (!chan)
  252. chan = nouveau_channel_get_unlocked(dev_priv->channel);
  253. mutex_lock(&chan->mutex);
  254. /* Emit a page flip */
  255. if (dev_priv->card_type >= NV_50) {
  256. ret = nv50_display_flip_next(crtc, fb, chan);
  257. if (ret) {
  258. nouveau_channel_put(&chan);
  259. goto fail_unreserve;
  260. }
  261. }
  262. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  263. nouveau_channel_put(&chan);
  264. if (ret)
  265. goto fail_unreserve;
  266. /* Update the crtc struct and cleanup */
  267. crtc->fb = fb;
  268. nouveau_page_flip_unreserve(old_bo, new_bo, fence);
  269. nouveau_fence_unref(&fence);
  270. return 0;
  271. fail_unreserve:
  272. nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
  273. fail_free:
  274. kfree(s);
  275. return ret;
  276. }
  277. int
  278. nouveau_finish_page_flip(struct nouveau_channel *chan,
  279. struct nouveau_page_flip_state *ps)
  280. {
  281. struct drm_device *dev = chan->dev;
  282. struct nouveau_page_flip_state *s;
  283. unsigned long flags;
  284. spin_lock_irqsave(&dev->event_lock, flags);
  285. if (list_empty(&chan->nvsw.flip)) {
  286. NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id);
  287. spin_unlock_irqrestore(&dev->event_lock, flags);
  288. return -EINVAL;
  289. }
  290. s = list_first_entry(&chan->nvsw.flip,
  291. struct nouveau_page_flip_state, head);
  292. if (s->event) {
  293. struct drm_pending_vblank_event *e = s->event;
  294. struct timeval now;
  295. do_gettimeofday(&now);
  296. e->event.sequence = 0;
  297. e->event.tv_sec = now.tv_sec;
  298. e->event.tv_usec = now.tv_usec;
  299. list_add_tail(&e->base.link, &e->base.file_priv->event_list);
  300. wake_up_interruptible(&e->base.file_priv->event_wait);
  301. }
  302. list_del(&s->head);
  303. if (ps)
  304. *ps = *s;
  305. kfree(s);
  306. spin_unlock_irqrestore(&dev->event_lock, flags);
  307. return 0;
  308. }