radeon_cursor.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. if (rdev->family >= CHIP_RV770) {
  104. if (radeon_crtc->crtc_id)
  105. WREG32(R700_D2CUR_SURFACE_ADDRESS_HIGH, 0);
  106. else
  107. WREG32(R700_D1CUR_SURFACE_ADDRESS_HIGH, 0);
  108. }
  109. WREG32(AVIVO_D1CUR_SURFACE_ADDRESS + radeon_crtc->crtc_offset, gpu_addr);
  110. } else {
  111. radeon_crtc->legacy_cursor_offset = gpu_addr - radeon_crtc->legacy_display_base_addr;
  112. /* offset is from DISP(2)_BASE_ADDRESS */
  113. WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, radeon_crtc->legacy_cursor_offset);
  114. }
  115. }
  116. int radeon_crtc_cursor_set(struct drm_crtc *crtc,
  117. struct drm_file *file_priv,
  118. uint32_t handle,
  119. uint32_t width,
  120. uint32_t height)
  121. {
  122. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  123. struct drm_gem_object *obj;
  124. uint64_t gpu_addr;
  125. int ret;
  126. if (!handle) {
  127. /* turn off cursor */
  128. radeon_hide_cursor(crtc);
  129. obj = NULL;
  130. goto unpin;
  131. }
  132. if ((width > CURSOR_WIDTH) || (height > CURSOR_HEIGHT)) {
  133. DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
  134. return -EINVAL;
  135. }
  136. radeon_crtc->cursor_width = width;
  137. radeon_crtc->cursor_height = height;
  138. obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
  139. if (!obj) {
  140. DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, radeon_crtc->crtc_id);
  141. return -EINVAL;
  142. }
  143. ret = radeon_gem_object_pin(obj, RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
  144. if (ret)
  145. goto fail;
  146. radeon_lock_cursor(crtc, true);
  147. /* XXX only 27 bit offset for legacy cursor */
  148. radeon_set_cursor(crtc, obj, gpu_addr);
  149. radeon_show_cursor(crtc);
  150. radeon_lock_cursor(crtc, false);
  151. unpin:
  152. if (radeon_crtc->cursor_bo) {
  153. radeon_gem_object_unpin(radeon_crtc->cursor_bo);
  154. mutex_lock(&crtc->dev->struct_mutex);
  155. drm_gem_object_unreference(radeon_crtc->cursor_bo);
  156. mutex_unlock(&crtc->dev->struct_mutex);
  157. }
  158. radeon_crtc->cursor_bo = obj;
  159. return 0;
  160. fail:
  161. mutex_lock(&crtc->dev->struct_mutex);
  162. drm_gem_object_unreference(obj);
  163. mutex_unlock(&crtc->dev->struct_mutex);
  164. return 0;
  165. }
  166. int radeon_crtc_cursor_move(struct drm_crtc *crtc,
  167. int x, int y)
  168. {
  169. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  170. struct radeon_device *rdev = crtc->dev->dev_private;
  171. int xorigin = 0, yorigin = 0;
  172. if (x < 0)
  173. xorigin = -x + 1;
  174. if (y < 0)
  175. yorigin = -y + 1;
  176. if (xorigin >= CURSOR_WIDTH)
  177. xorigin = CURSOR_WIDTH - 1;
  178. if (yorigin >= CURSOR_HEIGHT)
  179. yorigin = CURSOR_HEIGHT - 1;
  180. radeon_lock_cursor(crtc, true);
  181. if (ASIC_IS_AVIVO(rdev)) {
  182. int w = radeon_crtc->cursor_width;
  183. int i = 0;
  184. struct drm_crtc *crtc_p;
  185. /* avivo cursor are offset into the total surface */
  186. x += crtc->x;
  187. y += crtc->y;
  188. DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
  189. /* avivo cursor image can't end on 128 pixel boundry or
  190. * go past the end of the frame if both crtcs are enabled
  191. */
  192. list_for_each_entry(crtc_p, &crtc->dev->mode_config.crtc_list, head) {
  193. if (crtc_p->enabled)
  194. i++;
  195. }
  196. if (i > 1) {
  197. int cursor_end, frame_end;
  198. cursor_end = x - xorigin + w;
  199. frame_end = crtc->x + crtc->mode.crtc_hdisplay;
  200. if (cursor_end >= frame_end) {
  201. w = w - (cursor_end - frame_end);
  202. if (!(frame_end & 0x7f))
  203. w--;
  204. } else {
  205. if (!(cursor_end & 0x7f))
  206. w--;
  207. }
  208. if (w <= 0)
  209. w = 1;
  210. }
  211. WREG32(AVIVO_D1CUR_POSITION + radeon_crtc->crtc_offset,
  212. ((xorigin ? 0 : x) << 16) |
  213. (yorigin ? 0 : y));
  214. WREG32(AVIVO_D1CUR_HOT_SPOT + radeon_crtc->crtc_offset, (xorigin << 16) | yorigin);
  215. WREG32(AVIVO_D1CUR_SIZE + radeon_crtc->crtc_offset,
  216. ((w - 1) << 16) | (radeon_crtc->cursor_height - 1));
  217. } else {
  218. if (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)
  219. y *= 2;
  220. WREG32(RADEON_CUR_HORZ_VERT_OFF + radeon_crtc->crtc_offset,
  221. (RADEON_CUR_LOCK
  222. | (xorigin << 16)
  223. | yorigin));
  224. WREG32(RADEON_CUR_HORZ_VERT_POSN + radeon_crtc->crtc_offset,
  225. (RADEON_CUR_LOCK
  226. | ((xorigin ? 0 : x) << 16)
  227. | (yorigin ? 0 : y)));
  228. /* offset is from DISP(2)_BASE_ADDRESS */
  229. WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, (radeon_crtc->legacy_cursor_offset +
  230. (yorigin * 256)));
  231. }
  232. radeon_lock_cursor(crtc, false);
  233. return 0;
  234. }