radeon_cursor.c 11 KB

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