nouveau_display.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. static void
  35. nouveau_user_framebuffer_destroy(struct drm_framebuffer *drm_fb)
  36. {
  37. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  38. if (fb->nvbo)
  39. drm_gem_object_unreference_unlocked(fb->nvbo->gem);
  40. drm_framebuffer_cleanup(drm_fb);
  41. kfree(fb);
  42. }
  43. static int
  44. nouveau_user_framebuffer_create_handle(struct drm_framebuffer *drm_fb,
  45. struct drm_file *file_priv,
  46. unsigned int *handle)
  47. {
  48. struct nouveau_framebuffer *fb = nouveau_framebuffer(drm_fb);
  49. return drm_gem_handle_create(file_priv, fb->nvbo->gem, handle);
  50. }
  51. static const struct drm_framebuffer_funcs nouveau_framebuffer_funcs = {
  52. .destroy = nouveau_user_framebuffer_destroy,
  53. .create_handle = nouveau_user_framebuffer_create_handle,
  54. };
  55. int
  56. nouveau_framebuffer_init(struct drm_device *dev, struct nouveau_framebuffer *nouveau_fb,
  57. struct drm_mode_fb_cmd *mode_cmd, struct nouveau_bo *nvbo)
  58. {
  59. int ret;
  60. ret = drm_framebuffer_init(dev, &nouveau_fb->base, &nouveau_framebuffer_funcs);
  61. if (ret) {
  62. return ret;
  63. }
  64. drm_helper_mode_fill_fb_struct(&nouveau_fb->base, mode_cmd);
  65. nouveau_fb->nvbo = nvbo;
  66. return 0;
  67. }
  68. static struct drm_framebuffer *
  69. nouveau_user_framebuffer_create(struct drm_device *dev,
  70. struct drm_file *file_priv,
  71. struct drm_mode_fb_cmd *mode_cmd)
  72. {
  73. struct nouveau_framebuffer *nouveau_fb;
  74. struct drm_gem_object *gem;
  75. int ret;
  76. gem = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  77. if (!gem)
  78. return ERR_PTR(-ENOENT);
  79. nouveau_fb = kzalloc(sizeof(struct nouveau_framebuffer), GFP_KERNEL);
  80. if (!nouveau_fb)
  81. return ERR_PTR(-ENOMEM);
  82. ret = nouveau_framebuffer_init(dev, nouveau_fb, mode_cmd, nouveau_gem_object(gem));
  83. if (ret) {
  84. drm_gem_object_unreference(gem);
  85. return ERR_PTR(ret);
  86. }
  87. return &nouveau_fb->base;
  88. }
  89. const struct drm_mode_config_funcs nouveau_mode_config_funcs = {
  90. .fb_create = nouveau_user_framebuffer_create,
  91. .output_poll_changed = nouveau_fbcon_output_poll_changed,
  92. };
  93. int
  94. nouveau_vblank_enable(struct drm_device *dev, int crtc)
  95. {
  96. struct drm_nouveau_private *dev_priv = dev->dev_private;
  97. if (dev_priv->card_type >= NV_50)
  98. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1, 0,
  99. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc));
  100. else
  101. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0,
  102. NV_PCRTC_INTR_0_VBLANK);
  103. return 0;
  104. }
  105. void
  106. nouveau_vblank_disable(struct drm_device *dev, int crtc)
  107. {
  108. struct drm_nouveau_private *dev_priv = dev->dev_private;
  109. if (dev_priv->card_type >= NV_50)
  110. nv_mask(dev, NV50_PDISPLAY_INTR_EN_1,
  111. NV50_PDISPLAY_INTR_EN_1_VBLANK_CRTC_(crtc), 0);
  112. else
  113. NVWriteCRTC(dev, crtc, NV_PCRTC_INTR_EN_0, 0);
  114. }
  115. static int
  116. nouveau_page_flip_reserve(struct nouveau_bo *old_bo,
  117. struct nouveau_bo *new_bo)
  118. {
  119. int ret;
  120. ret = nouveau_bo_pin(new_bo, TTM_PL_FLAG_VRAM);
  121. if (ret)
  122. return ret;
  123. ret = ttm_bo_reserve(&new_bo->bo, false, false, false, 0);
  124. if (ret)
  125. goto fail;
  126. ret = ttm_bo_reserve(&old_bo->bo, false, false, false, 0);
  127. if (ret)
  128. goto fail_unreserve;
  129. return 0;
  130. fail_unreserve:
  131. ttm_bo_unreserve(&new_bo->bo);
  132. fail:
  133. nouveau_bo_unpin(new_bo);
  134. return ret;
  135. }
  136. static void
  137. nouveau_page_flip_unreserve(struct nouveau_bo *old_bo,
  138. struct nouveau_bo *new_bo,
  139. struct nouveau_fence *fence)
  140. {
  141. nouveau_bo_fence(new_bo, fence);
  142. ttm_bo_unreserve(&new_bo->bo);
  143. nouveau_bo_fence(old_bo, fence);
  144. ttm_bo_unreserve(&old_bo->bo);
  145. nouveau_bo_unpin(old_bo);
  146. }
  147. static int
  148. nouveau_page_flip_emit(struct nouveau_channel *chan,
  149. struct nouveau_bo *old_bo,
  150. struct nouveau_bo *new_bo,
  151. struct nouveau_page_flip_state *s,
  152. struct nouveau_fence **pfence)
  153. {
  154. struct drm_device *dev = chan->dev;
  155. unsigned long flags;
  156. int ret;
  157. /* Queue it to the pending list */
  158. spin_lock_irqsave(&dev->event_lock, flags);
  159. list_add_tail(&s->head, &chan->nvsw.flip);
  160. spin_unlock_irqrestore(&dev->event_lock, flags);
  161. /* Synchronize with the old framebuffer */
  162. ret = nouveau_fence_sync(old_bo->bo.sync_obj, chan);
  163. if (ret)
  164. goto fail;
  165. /* Emit the pageflip */
  166. ret = RING_SPACE(chan, 2);
  167. if (ret)
  168. goto fail;
  169. BEGIN_RING(chan, NvSubSw, NV_SW_PAGE_FLIP, 1);
  170. OUT_RING(chan, 0);
  171. FIRE_RING(chan);
  172. ret = nouveau_fence_new(chan, pfence, true);
  173. if (ret)
  174. goto fail;
  175. return 0;
  176. fail:
  177. spin_lock_irqsave(&dev->event_lock, flags);
  178. list_del(&s->head);
  179. spin_unlock_irqrestore(&dev->event_lock, flags);
  180. return ret;
  181. }
  182. int
  183. nouveau_crtc_page_flip(struct drm_crtc *crtc, struct drm_framebuffer *fb,
  184. struct drm_pending_vblank_event *event)
  185. {
  186. struct drm_device *dev = crtc->dev;
  187. struct drm_nouveau_private *dev_priv = dev->dev_private;
  188. struct nouveau_bo *old_bo = nouveau_framebuffer(crtc->fb)->nvbo;
  189. struct nouveau_bo *new_bo = nouveau_framebuffer(fb)->nvbo;
  190. struct nouveau_page_flip_state *s;
  191. struct nouveau_channel *chan;
  192. struct nouveau_fence *fence;
  193. int ret;
  194. if (dev_priv->engine.graph.accel_blocked)
  195. return -ENODEV;
  196. s = kzalloc(sizeof(*s), GFP_KERNEL);
  197. if (!s)
  198. return -ENOMEM;
  199. /* Don't let the buffers go away while we flip */
  200. ret = nouveau_page_flip_reserve(old_bo, new_bo);
  201. if (ret)
  202. goto fail_free;
  203. /* Initialize a page flip struct */
  204. *s = (struct nouveau_page_flip_state)
  205. { { }, s->event, nouveau_crtc(crtc)->index,
  206. fb->bits_per_pixel, fb->pitch, crtc->x, crtc->y,
  207. new_bo->bo.offset };
  208. /* Choose the channel the flip will be handled in */
  209. chan = nouveau_fence_channel(new_bo->bo.sync_obj);
  210. if (!chan)
  211. chan = nouveau_channel_get_unlocked(dev_priv->channel);
  212. mutex_lock(&chan->mutex);
  213. /* Emit a page flip */
  214. ret = nouveau_page_flip_emit(chan, old_bo, new_bo, s, &fence);
  215. nouveau_channel_put(&chan);
  216. if (ret)
  217. goto fail_unreserve;
  218. /* Update the crtc struct and cleanup */
  219. crtc->fb = fb;
  220. nouveau_page_flip_unreserve(old_bo, new_bo, fence);
  221. nouveau_fence_unref(&fence);
  222. return 0;
  223. fail_unreserve:
  224. nouveau_page_flip_unreserve(old_bo, new_bo, NULL);
  225. fail_free:
  226. kfree(s);
  227. return ret;
  228. }
  229. int
  230. nouveau_finish_page_flip(struct nouveau_channel *chan,
  231. struct nouveau_page_flip_state *ps)
  232. {
  233. struct drm_device *dev = chan->dev;
  234. struct nouveau_page_flip_state *s;
  235. unsigned long flags;
  236. spin_lock_irqsave(&dev->event_lock, flags);
  237. if (list_empty(&chan->nvsw.flip)) {
  238. NV_ERROR(dev, "Unexpected pageflip in channel %d.\n", chan->id);
  239. spin_unlock_irqrestore(&dev->event_lock, flags);
  240. return -EINVAL;
  241. }
  242. s = list_first_entry(&chan->nvsw.flip,
  243. struct nouveau_page_flip_state, head);
  244. if (s->event) {
  245. struct drm_pending_vblank_event *e = s->event;
  246. struct timeval now;
  247. do_gettimeofday(&now);
  248. e->event.sequence = 0;
  249. e->event.tv_sec = now.tv_sec;
  250. e->event.tv_usec = now.tv_usec;
  251. list_add_tail(&e->base.link, &e->base.file_priv->event_list);
  252. wake_up_interruptible(&e->base.file_priv->event_wait);
  253. }
  254. list_del(&s->head);
  255. *ps = *s;
  256. kfree(s);
  257. spin_unlock_irqrestore(&dev->event_lock, flags);
  258. return 0;
  259. }