radeon_cursor.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include "drmP.h"
  27. #include "radeon_drm.h"
  28. #include "radeon.h"
  29. #define CURSOR_WIDTH 64
  30. #define CURSOR_HEIGHT 64
  31. static void radeon_lock_cursor(struct drm_crtc *crtc, bool lock)
  32. {
  33. struct radeon_device *rdev = crtc->dev->dev_private;
  34. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  35. uint32_t cur_lock;
  36. if (ASIC_IS_AVIVO(rdev)) {
  37. cur_lock = RREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset);
  38. if (lock)
  39. cur_lock |= AVIVO_D1CURSOR_UPDATE_LOCK;
  40. else
  41. cur_lock &= ~AVIVO_D1CURSOR_UPDATE_LOCK;
  42. WREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset, cur_lock);
  43. } else {
  44. cur_lock = RREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset);
  45. if (lock)
  46. cur_lock |= RADEON_CUR_LOCK;
  47. else
  48. cur_lock &= ~RADEON_CUR_LOCK;
  49. WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, cur_lock);
  50. }
  51. }
  52. static void radeon_hide_cursor(struct drm_crtc *crtc)
  53. {
  54. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  55. struct radeon_device *rdev = crtc->dev->dev_private;
  56. if (ASIC_IS_AVIVO(rdev)) {
  57. WREG32(RADEON_MM_INDEX, AVIVO_D1CUR_CONTROL + radeon_crtc->crtc_offset);
  58. WREG32(RADEON_MM_DATA, (AVIVO_D1CURSOR_MODE_24BPP << AVIVO_D1CURSOR_MODE_SHIFT));
  59. } else {
  60. switch (radeon_crtc->crtc_id) {
  61. case 0:
  62. WREG32(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
  63. break;
  64. case 1:
  65. WREG32(RADEON_MM_INDEX, RADEON_CRTC2_GEN_CNTL);
  66. break;
  67. default:
  68. return;
  69. }
  70. WREG32_P(RADEON_MM_DATA, 0, ~RADEON_CRTC_CUR_EN);
  71. }
  72. }
  73. static void radeon_show_cursor(struct drm_crtc *crtc)
  74. {
  75. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  76. struct radeon_device *rdev = crtc->dev->dev_private;
  77. if (ASIC_IS_AVIVO(rdev)) {
  78. WREG32(RADEON_MM_INDEX, AVIVO_D1CUR_CONTROL + radeon_crtc->crtc_offset);
  79. WREG32(RADEON_MM_DATA, AVIVO_D1CURSOR_EN |
  80. (AVIVO_D1CURSOR_MODE_24BPP << AVIVO_D1CURSOR_MODE_SHIFT));
  81. } else {
  82. switch (radeon_crtc->crtc_id) {
  83. case 0:
  84. WREG32(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
  85. break;
  86. case 1:
  87. WREG32(RADEON_MM_INDEX, RADEON_CRTC2_GEN_CNTL);
  88. break;
  89. default:
  90. return;
  91. }
  92. WREG32_P(RADEON_MM_DATA, (RADEON_CRTC_CUR_EN |
  93. (RADEON_CRTC_CUR_MODE_24BPP << RADEON_CRTC_CUR_MODE_SHIFT)),
  94. ~(RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_MASK));
  95. }
  96. }
  97. static void radeon_set_cursor(struct drm_crtc *crtc, struct drm_gem_object *obj,
  98. uint32_t gpu_addr)
  99. {
  100. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  101. struct radeon_device *rdev = crtc->dev->dev_private;
  102. if (ASIC_IS_AVIVO(rdev))
  103. WREG32(AVIVO_D1CUR_SURFACE_ADDRESS + radeon_crtc->crtc_offset, gpu_addr);
  104. else
  105. /* offset is from DISP(2)_BASE_ADDRESS */
  106. WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, gpu_addr);
  107. }
  108. int radeon_crtc_cursor_set(struct drm_crtc *crtc,
  109. struct drm_file *file_priv,
  110. uint32_t handle,
  111. uint32_t width,
  112. uint32_t height)
  113. {
  114. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  115. struct drm_gem_object *obj;
  116. uint64_t gpu_addr;
  117. int ret;
  118. if (!handle) {
  119. /* turn off cursor */
  120. radeon_hide_cursor(crtc);
  121. obj = NULL;
  122. goto unpin;
  123. }
  124. if ((width > CURSOR_WIDTH) || (height > CURSOR_HEIGHT)) {
  125. DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
  126. return -EINVAL;
  127. }
  128. radeon_crtc->cursor_width = width;
  129. radeon_crtc->cursor_height = height;
  130. obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
  131. if (!obj) {
  132. DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, radeon_crtc->crtc_id);
  133. return -EINVAL;
  134. }
  135. ret = radeon_gem_object_pin(obj, RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
  136. if (ret)
  137. goto fail;
  138. radeon_lock_cursor(crtc, true);
  139. /* XXX only 27 bit offset for legacy cursor */
  140. radeon_set_cursor(crtc, obj, gpu_addr);
  141. radeon_show_cursor(crtc);
  142. radeon_lock_cursor(crtc, false);
  143. unpin:
  144. if (radeon_crtc->cursor_bo) {
  145. radeon_gem_object_unpin(radeon_crtc->cursor_bo);
  146. mutex_lock(&crtc->dev->struct_mutex);
  147. drm_gem_object_unreference(radeon_crtc->cursor_bo);
  148. mutex_unlock(&crtc->dev->struct_mutex);
  149. }
  150. radeon_crtc->cursor_bo = obj;
  151. return 0;
  152. fail:
  153. mutex_lock(&crtc->dev->struct_mutex);
  154. drm_gem_object_unreference(obj);
  155. mutex_unlock(&crtc->dev->struct_mutex);
  156. return 0;
  157. }
  158. int radeon_crtc_cursor_move(struct drm_crtc *crtc,
  159. int x, int y)
  160. {
  161. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  162. struct radeon_device *rdev = crtc->dev->dev_private;
  163. int xorigin = 0, yorigin = 0;
  164. if (x < 0)
  165. xorigin = -x + 1;
  166. if (y < 0)
  167. yorigin = -y + 1;
  168. if (xorigin >= CURSOR_WIDTH)
  169. xorigin = CURSOR_WIDTH - 1;
  170. if (yorigin >= CURSOR_HEIGHT)
  171. yorigin = CURSOR_HEIGHT - 1;
  172. radeon_lock_cursor(crtc, true);
  173. if (ASIC_IS_AVIVO(rdev)) {
  174. int w = radeon_crtc->cursor_width;
  175. int i = 0;
  176. struct drm_crtc *crtc_p;
  177. /* avivo cursor are offset into the total surface */
  178. x += crtc->x;
  179. y += crtc->y;
  180. DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
  181. /* avivo cursor image can't end on 128 pixel boundry or
  182. * go past the end of the frame if both crtcs are enabled
  183. */
  184. list_for_each_entry(crtc_p, &crtc->dev->mode_config.crtc_list, head) {
  185. if (crtc_p->enabled)
  186. i++;
  187. }
  188. if (i > 1) {
  189. int cursor_end, frame_end;
  190. cursor_end = x - xorigin + w;
  191. frame_end = crtc->x + crtc->mode.crtc_hdisplay;
  192. if (cursor_end >= frame_end) {
  193. w = w - (cursor_end - frame_end);
  194. if (!(frame_end & 0x7f))
  195. w--;
  196. } else {
  197. if (!(cursor_end & 0x7f))
  198. w--;
  199. }
  200. if (w <= 0)
  201. w = 1;
  202. }
  203. WREG32(AVIVO_D1CUR_POSITION + radeon_crtc->crtc_offset,
  204. ((xorigin ? 0 : x) << 16) |
  205. (yorigin ? 0 : y));
  206. WREG32(AVIVO_D1CUR_HOT_SPOT + radeon_crtc->crtc_offset, (xorigin << 16) | yorigin);
  207. WREG32(AVIVO_D1CUR_SIZE + radeon_crtc->crtc_offset,
  208. ((w - 1) << 16) | (radeon_crtc->cursor_height - 1));
  209. } else {
  210. if (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)
  211. y *= 2;
  212. WREG32(RADEON_CUR_HORZ_VERT_OFF + radeon_crtc->crtc_offset,
  213. (RADEON_CUR_LOCK
  214. | (xorigin << 16)
  215. | yorigin));
  216. WREG32(RADEON_CUR_HORZ_VERT_POSN + radeon_crtc->crtc_offset,
  217. (RADEON_CUR_LOCK
  218. | ((xorigin ? 0 : x) << 16)
  219. | (yorigin ? 0 : y)));
  220. }
  221. radeon_lock_cursor(crtc, false);
  222. return 0;
  223. }