radeon_cursor.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 <drm/drmP.h>
  27. #include <drm/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_DCE4(rdev)) {
  37. cur_lock = RREG32(EVERGREEN_CUR_UPDATE + radeon_crtc->crtc_offset);
  38. if (lock)
  39. cur_lock |= EVERGREEN_CURSOR_UPDATE_LOCK;
  40. else
  41. cur_lock &= ~EVERGREEN_CURSOR_UPDATE_LOCK;
  42. WREG32(EVERGREEN_CUR_UPDATE + radeon_crtc->crtc_offset, cur_lock);
  43. } else if (ASIC_IS_AVIVO(rdev)) {
  44. cur_lock = RREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset);
  45. if (lock)
  46. cur_lock |= AVIVO_D1CURSOR_UPDATE_LOCK;
  47. else
  48. cur_lock &= ~AVIVO_D1CURSOR_UPDATE_LOCK;
  49. WREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset, cur_lock);
  50. } else {
  51. cur_lock = RREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset);
  52. if (lock)
  53. cur_lock |= RADEON_CUR_LOCK;
  54. else
  55. cur_lock &= ~RADEON_CUR_LOCK;
  56. WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, cur_lock);
  57. }
  58. }
  59. static void radeon_hide_cursor(struct drm_crtc *crtc)
  60. {
  61. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  62. struct radeon_device *rdev = crtc->dev->dev_private;
  63. if (ASIC_IS_DCE4(rdev)) {
  64. WREG32_IDX(EVERGREEN_CUR_CONTROL + radeon_crtc->crtc_offset,
  65. EVERGREEN_CURSOR_MODE(EVERGREEN_CURSOR_24_8_PRE_MULT) |
  66. EVERGREEN_CURSOR_URGENT_CONTROL(EVERGREEN_CURSOR_URGENT_1_2));
  67. } else if (ASIC_IS_AVIVO(rdev)) {
  68. WREG32_IDX(AVIVO_D1CUR_CONTROL + radeon_crtc->crtc_offset,
  69. (AVIVO_D1CURSOR_MODE_24BPP << AVIVO_D1CURSOR_MODE_SHIFT));
  70. } else {
  71. u32 reg;
  72. switch (radeon_crtc->crtc_id) {
  73. case 0:
  74. reg = RADEON_CRTC_GEN_CNTL;
  75. break;
  76. case 1:
  77. reg = RADEON_CRTC2_GEN_CNTL;
  78. break;
  79. default:
  80. return;
  81. }
  82. WREG32_IDX(reg, RREG32_IDX(reg) & ~RADEON_CRTC_CUR_EN);
  83. }
  84. }
  85. static void radeon_show_cursor(struct drm_crtc *crtc)
  86. {
  87. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  88. struct radeon_device *rdev = crtc->dev->dev_private;
  89. if (ASIC_IS_DCE4(rdev)) {
  90. WREG32(RADEON_MM_INDEX, EVERGREEN_CUR_CONTROL + radeon_crtc->crtc_offset);
  91. WREG32(RADEON_MM_DATA, EVERGREEN_CURSOR_EN |
  92. EVERGREEN_CURSOR_MODE(EVERGREEN_CURSOR_24_8_PRE_MULT) |
  93. EVERGREEN_CURSOR_URGENT_CONTROL(EVERGREEN_CURSOR_URGENT_1_2));
  94. } else if (ASIC_IS_AVIVO(rdev)) {
  95. WREG32(RADEON_MM_INDEX, AVIVO_D1CUR_CONTROL + radeon_crtc->crtc_offset);
  96. WREG32(RADEON_MM_DATA, AVIVO_D1CURSOR_EN |
  97. (AVIVO_D1CURSOR_MODE_24BPP << AVIVO_D1CURSOR_MODE_SHIFT));
  98. } else {
  99. switch (radeon_crtc->crtc_id) {
  100. case 0:
  101. WREG32(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
  102. break;
  103. case 1:
  104. WREG32(RADEON_MM_INDEX, RADEON_CRTC2_GEN_CNTL);
  105. break;
  106. default:
  107. return;
  108. }
  109. WREG32_P(RADEON_MM_DATA, (RADEON_CRTC_CUR_EN |
  110. (RADEON_CRTC_CUR_MODE_24BPP << RADEON_CRTC_CUR_MODE_SHIFT)),
  111. ~(RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_MASK));
  112. }
  113. }
  114. static void radeon_set_cursor(struct drm_crtc *crtc, struct drm_gem_object *obj,
  115. uint64_t gpu_addr)
  116. {
  117. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  118. struct radeon_device *rdev = crtc->dev->dev_private;
  119. if (ASIC_IS_DCE4(rdev)) {
  120. WREG32(EVERGREEN_CUR_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
  121. upper_32_bits(gpu_addr));
  122. WREG32(EVERGREEN_CUR_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
  123. gpu_addr & 0xffffffff);
  124. } else if (ASIC_IS_AVIVO(rdev)) {
  125. if (rdev->family >= CHIP_RV770) {
  126. if (radeon_crtc->crtc_id)
  127. WREG32(R700_D2CUR_SURFACE_ADDRESS_HIGH, upper_32_bits(gpu_addr));
  128. else
  129. WREG32(R700_D1CUR_SURFACE_ADDRESS_HIGH, upper_32_bits(gpu_addr));
  130. }
  131. WREG32(AVIVO_D1CUR_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
  132. gpu_addr & 0xffffffff);
  133. } else {
  134. radeon_crtc->legacy_cursor_offset = gpu_addr - radeon_crtc->legacy_display_base_addr;
  135. /* offset is from DISP(2)_BASE_ADDRESS */
  136. WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, radeon_crtc->legacy_cursor_offset);
  137. }
  138. }
  139. int radeon_crtc_cursor_set(struct drm_crtc *crtc,
  140. struct drm_file *file_priv,
  141. uint32_t handle,
  142. uint32_t width,
  143. uint32_t height)
  144. {
  145. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  146. struct radeon_device *rdev = crtc->dev->dev_private;
  147. struct drm_gem_object *obj;
  148. struct radeon_bo *robj;
  149. uint64_t gpu_addr;
  150. int ret;
  151. if (!handle) {
  152. /* turn off cursor */
  153. radeon_hide_cursor(crtc);
  154. obj = NULL;
  155. goto unpin;
  156. }
  157. if ((width > CURSOR_WIDTH) || (height > CURSOR_HEIGHT)) {
  158. DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
  159. return -EINVAL;
  160. }
  161. obj = drm_gem_object_lookup(crtc->dev, file_priv, handle);
  162. if (!obj) {
  163. DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, radeon_crtc->crtc_id);
  164. return -ENOENT;
  165. }
  166. robj = gem_to_radeon_bo(obj);
  167. ret = radeon_bo_reserve(robj, false);
  168. if (unlikely(ret != 0))
  169. goto fail;
  170. /* Only 27 bit offset for legacy cursor */
  171. ret = radeon_bo_pin_restricted(robj, RADEON_GEM_DOMAIN_VRAM,
  172. ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
  173. &gpu_addr);
  174. radeon_bo_unreserve(robj);
  175. if (ret)
  176. goto fail;
  177. radeon_crtc->cursor_width = width;
  178. radeon_crtc->cursor_height = height;
  179. radeon_lock_cursor(crtc, true);
  180. radeon_set_cursor(crtc, obj, gpu_addr);
  181. radeon_show_cursor(crtc);
  182. radeon_lock_cursor(crtc, false);
  183. unpin:
  184. if (radeon_crtc->cursor_bo) {
  185. robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
  186. ret = radeon_bo_reserve(robj, false);
  187. if (likely(ret == 0)) {
  188. radeon_bo_unpin(robj);
  189. radeon_bo_unreserve(robj);
  190. }
  191. drm_gem_object_unreference_unlocked(radeon_crtc->cursor_bo);
  192. }
  193. radeon_crtc->cursor_bo = obj;
  194. return 0;
  195. fail:
  196. drm_gem_object_unreference_unlocked(obj);
  197. return ret;
  198. }
  199. int radeon_crtc_cursor_move(struct drm_crtc *crtc,
  200. int x, int y)
  201. {
  202. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  203. struct radeon_device *rdev = crtc->dev->dev_private;
  204. int xorigin = 0, yorigin = 0;
  205. int w = radeon_crtc->cursor_width;
  206. if (ASIC_IS_AVIVO(rdev)) {
  207. /* avivo cursor are offset into the total surface */
  208. x += crtc->x;
  209. y += crtc->y;
  210. }
  211. DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
  212. if (x < 0) {
  213. xorigin = min(-x, CURSOR_WIDTH - 1);
  214. x = 0;
  215. }
  216. if (y < 0) {
  217. yorigin = min(-y, CURSOR_HEIGHT - 1);
  218. y = 0;
  219. }
  220. /* fixed on DCE6 and newer */
  221. if (ASIC_IS_AVIVO(rdev) && !ASIC_IS_DCE6(rdev)) {
  222. int i = 0;
  223. struct drm_crtc *crtc_p;
  224. /*
  225. * avivo cursor image can't end on 128 pixel boundary or
  226. * go past the end of the frame if both crtcs are enabled
  227. *
  228. * NOTE: It is safe to access crtc->enabled of other crtcs
  229. * without holding either the mode_config lock or the other
  230. * crtc's lock as long as write access to this flag _always_
  231. * grabs all locks.
  232. */
  233. list_for_each_entry(crtc_p, &crtc->dev->mode_config.crtc_list, head) {
  234. if (crtc_p->enabled)
  235. i++;
  236. }
  237. if (i > 1) {
  238. int cursor_end, frame_end;
  239. cursor_end = x - xorigin + w;
  240. frame_end = crtc->x + crtc->mode.crtc_hdisplay;
  241. if (cursor_end >= frame_end) {
  242. w = w - (cursor_end - frame_end);
  243. if (!(frame_end & 0x7f))
  244. w--;
  245. } else {
  246. if (!(cursor_end & 0x7f))
  247. w--;
  248. }
  249. if (w <= 0) {
  250. w = 1;
  251. cursor_end = x - xorigin + w;
  252. if (!(cursor_end & 0x7f)) {
  253. x--;
  254. WARN_ON_ONCE(x < 0);
  255. }
  256. }
  257. }
  258. }
  259. radeon_lock_cursor(crtc, true);
  260. if (ASIC_IS_DCE4(rdev)) {
  261. WREG32(EVERGREEN_CUR_POSITION + radeon_crtc->crtc_offset, (x << 16) | y);
  262. WREG32(EVERGREEN_CUR_HOT_SPOT + radeon_crtc->crtc_offset, (xorigin << 16) | yorigin);
  263. WREG32(EVERGREEN_CUR_SIZE + radeon_crtc->crtc_offset,
  264. ((w - 1) << 16) | (radeon_crtc->cursor_height - 1));
  265. } else if (ASIC_IS_AVIVO(rdev)) {
  266. WREG32(AVIVO_D1CUR_POSITION + radeon_crtc->crtc_offset, (x << 16) | y);
  267. WREG32(AVIVO_D1CUR_HOT_SPOT + radeon_crtc->crtc_offset, (xorigin << 16) | yorigin);
  268. WREG32(AVIVO_D1CUR_SIZE + radeon_crtc->crtc_offset,
  269. ((w - 1) << 16) | (radeon_crtc->cursor_height - 1));
  270. } else {
  271. if (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)
  272. y *= 2;
  273. WREG32(RADEON_CUR_HORZ_VERT_OFF + radeon_crtc->crtc_offset,
  274. (RADEON_CUR_LOCK
  275. | (xorigin << 16)
  276. | yorigin));
  277. WREG32(RADEON_CUR_HORZ_VERT_POSN + radeon_crtc->crtc_offset,
  278. (RADEON_CUR_LOCK
  279. | (x << 16)
  280. | y));
  281. /* offset is from DISP(2)_BASE_ADDRESS */
  282. WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, (radeon_crtc->legacy_cursor_offset +
  283. (yorigin * 256)));
  284. }
  285. radeon_lock_cursor(crtc, false);
  286. return 0;
  287. }