xref: /dragonfly/sys/dev/drm/radeon/radeon_cursor.c (revision 3f2dd94a)
1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot  * Copyright 2007-8 Advanced Micro Devices, Inc.
3926deccbSFrançois Tigeot  * Copyright 2008 Red Hat Inc.
4926deccbSFrançois Tigeot  *
5926deccbSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
6926deccbSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
7926deccbSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
8926deccbSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9926deccbSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
10926deccbSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
11926deccbSFrançois Tigeot  *
12926deccbSFrançois Tigeot  * The above copyright notice and this permission notice shall be included in
13926deccbSFrançois Tigeot  * all copies or substantial portions of the Software.
14926deccbSFrançois Tigeot  *
15926deccbSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16926deccbSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17926deccbSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18926deccbSFrançois Tigeot  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19926deccbSFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20926deccbSFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21926deccbSFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
22926deccbSFrançois Tigeot  *
23926deccbSFrançois Tigeot  * Authors: Dave Airlie
24926deccbSFrançois Tigeot  *          Alex Deucher
25926deccbSFrançois Tigeot  */
26926deccbSFrançois Tigeot #include <drm/drmP.h>
2783b4b9b9SFrançois Tigeot #include <drm/radeon_drm.h>
28926deccbSFrançois Tigeot #include "radeon.h"
29926deccbSFrançois Tigeot 
radeon_lock_cursor(struct drm_crtc * crtc,bool lock)30926deccbSFrançois Tigeot static void radeon_lock_cursor(struct drm_crtc *crtc, bool lock)
31926deccbSFrançois Tigeot {
32926deccbSFrançois Tigeot 	struct radeon_device *rdev = crtc->dev->dev_private;
33926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
34926deccbSFrançois Tigeot 	uint32_t cur_lock;
35926deccbSFrançois Tigeot 
36926deccbSFrançois Tigeot 	if (ASIC_IS_DCE4(rdev)) {
37926deccbSFrançois Tigeot 		cur_lock = RREG32(EVERGREEN_CUR_UPDATE + radeon_crtc->crtc_offset);
38926deccbSFrançois Tigeot 		if (lock)
39926deccbSFrançois Tigeot 			cur_lock |= EVERGREEN_CURSOR_UPDATE_LOCK;
40926deccbSFrançois Tigeot 		else
41926deccbSFrançois Tigeot 			cur_lock &= ~EVERGREEN_CURSOR_UPDATE_LOCK;
42926deccbSFrançois Tigeot 		WREG32(EVERGREEN_CUR_UPDATE + radeon_crtc->crtc_offset, cur_lock);
43926deccbSFrançois Tigeot 	} else if (ASIC_IS_AVIVO(rdev)) {
44926deccbSFrançois Tigeot 		cur_lock = RREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset);
45926deccbSFrançois Tigeot 		if (lock)
46926deccbSFrançois Tigeot 			cur_lock |= AVIVO_D1CURSOR_UPDATE_LOCK;
47926deccbSFrançois Tigeot 		else
48926deccbSFrançois Tigeot 			cur_lock &= ~AVIVO_D1CURSOR_UPDATE_LOCK;
49926deccbSFrançois Tigeot 		WREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset, cur_lock);
50926deccbSFrançois Tigeot 	} else {
51926deccbSFrançois Tigeot 		cur_lock = RREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset);
52926deccbSFrançois Tigeot 		if (lock)
53926deccbSFrançois Tigeot 			cur_lock |= RADEON_CUR_LOCK;
54926deccbSFrançois Tigeot 		else
55926deccbSFrançois Tigeot 			cur_lock &= ~RADEON_CUR_LOCK;
56926deccbSFrançois Tigeot 		WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, cur_lock);
57926deccbSFrançois Tigeot 	}
58926deccbSFrançois Tigeot }
59926deccbSFrançois Tigeot 
radeon_hide_cursor(struct drm_crtc * crtc)60926deccbSFrançois Tigeot static void radeon_hide_cursor(struct drm_crtc *crtc)
61926deccbSFrançois Tigeot {
62926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
63926deccbSFrançois Tigeot 	struct radeon_device *rdev = crtc->dev->dev_private;
64926deccbSFrançois Tigeot 
65926deccbSFrançois Tigeot 	if (ASIC_IS_DCE4(rdev)) {
66926deccbSFrançois Tigeot 		WREG32_IDX(EVERGREEN_CUR_CONTROL + radeon_crtc->crtc_offset,
67926deccbSFrançois Tigeot 			   EVERGREEN_CURSOR_MODE(EVERGREEN_CURSOR_24_8_PRE_MULT) |
68926deccbSFrançois Tigeot 			   EVERGREEN_CURSOR_URGENT_CONTROL(EVERGREEN_CURSOR_URGENT_1_2));
69926deccbSFrançois Tigeot 	} else if (ASIC_IS_AVIVO(rdev)) {
70926deccbSFrançois Tigeot 		WREG32_IDX(AVIVO_D1CUR_CONTROL + radeon_crtc->crtc_offset,
71926deccbSFrançois Tigeot 			   (AVIVO_D1CURSOR_MODE_24BPP << AVIVO_D1CURSOR_MODE_SHIFT));
72926deccbSFrançois Tigeot 	} else {
73926deccbSFrançois Tigeot 		u32 reg;
74926deccbSFrançois Tigeot 		switch (radeon_crtc->crtc_id) {
75926deccbSFrançois Tigeot 		case 0:
76926deccbSFrançois Tigeot 			reg = RADEON_CRTC_GEN_CNTL;
77926deccbSFrançois Tigeot 			break;
78926deccbSFrançois Tigeot 		case 1:
79926deccbSFrançois Tigeot 			reg = RADEON_CRTC2_GEN_CNTL;
80926deccbSFrançois Tigeot 			break;
81926deccbSFrançois Tigeot 		default:
82926deccbSFrançois Tigeot 			return;
83926deccbSFrançois Tigeot 		}
84926deccbSFrançois Tigeot 		WREG32_IDX(reg, RREG32_IDX(reg) & ~RADEON_CRTC_CUR_EN);
85926deccbSFrançois Tigeot 	}
86926deccbSFrançois Tigeot }
87926deccbSFrançois Tigeot 
radeon_show_cursor(struct drm_crtc * crtc)88926deccbSFrançois Tigeot static void radeon_show_cursor(struct drm_crtc *crtc)
89926deccbSFrançois Tigeot {
90926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
91926deccbSFrançois Tigeot 	struct radeon_device *rdev = crtc->dev->dev_private;
92926deccbSFrançois Tigeot 
93c59a5c48SFrançois Tigeot 	if (radeon_crtc->cursor_out_of_bounds)
94c59a5c48SFrançois Tigeot 		return;
95c59a5c48SFrançois Tigeot 
96926deccbSFrançois Tigeot 	if (ASIC_IS_DCE4(rdev)) {
97c59a5c48SFrançois Tigeot 		WREG32(EVERGREEN_CUR_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
98c59a5c48SFrançois Tigeot 		       upper_32_bits(radeon_crtc->cursor_addr));
99c59a5c48SFrançois Tigeot 		WREG32(EVERGREEN_CUR_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
100c59a5c48SFrançois Tigeot 		       lower_32_bits(radeon_crtc->cursor_addr));
101926deccbSFrançois Tigeot 		WREG32(RADEON_MM_INDEX, EVERGREEN_CUR_CONTROL + radeon_crtc->crtc_offset);
102926deccbSFrançois Tigeot 		WREG32(RADEON_MM_DATA, EVERGREEN_CURSOR_EN |
103926deccbSFrançois Tigeot 		       EVERGREEN_CURSOR_MODE(EVERGREEN_CURSOR_24_8_PRE_MULT) |
104926deccbSFrançois Tigeot 		       EVERGREEN_CURSOR_URGENT_CONTROL(EVERGREEN_CURSOR_URGENT_1_2));
105926deccbSFrançois Tigeot 	} else if (ASIC_IS_AVIVO(rdev)) {
106c59a5c48SFrançois Tigeot 		if (rdev->family >= CHIP_RV770) {
107c59a5c48SFrançois Tigeot 			if (radeon_crtc->crtc_id)
108c59a5c48SFrançois Tigeot 				WREG32(R700_D2CUR_SURFACE_ADDRESS_HIGH,
109c59a5c48SFrançois Tigeot 				       upper_32_bits(radeon_crtc->cursor_addr));
110c59a5c48SFrançois Tigeot 			else
111c59a5c48SFrançois Tigeot 				WREG32(R700_D1CUR_SURFACE_ADDRESS_HIGH,
112c59a5c48SFrançois Tigeot 				       upper_32_bits(radeon_crtc->cursor_addr));
113c59a5c48SFrançois Tigeot 		}
114c59a5c48SFrançois Tigeot 
115c59a5c48SFrançois Tigeot 		WREG32(AVIVO_D1CUR_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
116c59a5c48SFrançois Tigeot 		       lower_32_bits(radeon_crtc->cursor_addr));
117926deccbSFrançois Tigeot 		WREG32(RADEON_MM_INDEX, AVIVO_D1CUR_CONTROL + radeon_crtc->crtc_offset);
118926deccbSFrançois Tigeot 		WREG32(RADEON_MM_DATA, AVIVO_D1CURSOR_EN |
119926deccbSFrançois Tigeot 		       (AVIVO_D1CURSOR_MODE_24BPP << AVIVO_D1CURSOR_MODE_SHIFT));
120926deccbSFrançois Tigeot 	} else {
121c59a5c48SFrançois Tigeot 		/* offset is from DISP(2)_BASE_ADDRESS */
122c59a5c48SFrançois Tigeot 		WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset,
123c59a5c48SFrançois Tigeot 		       radeon_crtc->cursor_addr - radeon_crtc->legacy_display_base_addr);
124c59a5c48SFrançois Tigeot 
125926deccbSFrançois Tigeot 		switch (radeon_crtc->crtc_id) {
126926deccbSFrançois Tigeot 		case 0:
127926deccbSFrançois Tigeot 			WREG32(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
128926deccbSFrançois Tigeot 			break;
129926deccbSFrançois Tigeot 		case 1:
130926deccbSFrançois Tigeot 			WREG32(RADEON_MM_INDEX, RADEON_CRTC2_GEN_CNTL);
131926deccbSFrançois Tigeot 			break;
132926deccbSFrançois Tigeot 		default:
133926deccbSFrançois Tigeot 			return;
134926deccbSFrançois Tigeot 		}
135926deccbSFrançois Tigeot 
136926deccbSFrançois Tigeot 		WREG32_P(RADEON_MM_DATA, (RADEON_CRTC_CUR_EN |
137926deccbSFrançois Tigeot 					  (RADEON_CRTC_CUR_MODE_24BPP << RADEON_CRTC_CUR_MODE_SHIFT)),
138926deccbSFrançois Tigeot 			 ~(RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_MASK));
139926deccbSFrançois Tigeot 	}
140926deccbSFrançois Tigeot }
141926deccbSFrançois Tigeot 
radeon_cursor_move_locked(struct drm_crtc * crtc,int x,int y)1427dcf36dcSFrançois Tigeot static int radeon_cursor_move_locked(struct drm_crtc *crtc, int x, int y)
143926deccbSFrançois Tigeot {
144926deccbSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
145926deccbSFrançois Tigeot 	struct radeon_device *rdev = crtc->dev->dev_private;
146926deccbSFrançois Tigeot 	int xorigin = 0, yorigin = 0;
147926deccbSFrançois Tigeot 	int w = radeon_crtc->cursor_width;
148926deccbSFrançois Tigeot 
149c59a5c48SFrançois Tigeot 	radeon_crtc->cursor_x = x;
150c59a5c48SFrançois Tigeot 	radeon_crtc->cursor_y = y;
151c59a5c48SFrançois Tigeot 
152926deccbSFrançois Tigeot 	if (ASIC_IS_AVIVO(rdev)) {
153926deccbSFrançois Tigeot 		/* avivo cursor are offset into the total surface */
154926deccbSFrançois Tigeot 		x += crtc->x;
155926deccbSFrançois Tigeot 		y += crtc->y;
156926deccbSFrançois Tigeot 	}
157926deccbSFrançois Tigeot 
158c59a5c48SFrançois Tigeot 	if (x < 0)
15957e252bfSMichael Neumann 		xorigin = min(-x, radeon_crtc->max_cursor_width - 1);
160c59a5c48SFrançois Tigeot 	if (y < 0)
16157e252bfSMichael Neumann 		yorigin = min(-y, radeon_crtc->max_cursor_height - 1);
162c59a5c48SFrançois Tigeot 
163c59a5c48SFrançois Tigeot 	if (!ASIC_IS_AVIVO(rdev)) {
164c59a5c48SFrançois Tigeot 		x += crtc->x;
165c59a5c48SFrançois Tigeot 		y += crtc->y;
166926deccbSFrançois Tigeot 	}
167c59a5c48SFrançois Tigeot 	DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
168926deccbSFrançois Tigeot 
169926deccbSFrançois Tigeot 	/* fixed on DCE6 and newer */
170926deccbSFrançois Tigeot 	if (ASIC_IS_AVIVO(rdev) && !ASIC_IS_DCE6(rdev)) {
171926deccbSFrançois Tigeot 		int i = 0;
172926deccbSFrançois Tigeot 		struct drm_crtc *crtc_p;
173926deccbSFrançois Tigeot 
174b403bed8SMichael Neumann 		/*
175b403bed8SMichael Neumann 		 * avivo cursor image can't end on 128 pixel boundary or
176926deccbSFrançois Tigeot 		 * go past the end of the frame if both crtcs are enabled
177b403bed8SMichael Neumann 		 *
178b403bed8SMichael Neumann 		 * NOTE: It is safe to access crtc->enabled of other crtcs
179b403bed8SMichael Neumann 		 * without holding either the mode_config lock or the other
180b403bed8SMichael Neumann 		 * crtc's lock as long as write access to this flag _always_
181b403bed8SMichael Neumann 		 * grabs all locks.
182926deccbSFrançois Tigeot 		 */
183926deccbSFrançois Tigeot 		list_for_each_entry(crtc_p, &crtc->dev->mode_config.crtc_list, head) {
184926deccbSFrançois Tigeot 			if (crtc_p->enabled)
185926deccbSFrançois Tigeot 				i++;
186926deccbSFrançois Tigeot 		}
187926deccbSFrançois Tigeot 		if (i > 1) {
188926deccbSFrançois Tigeot 			int cursor_end, frame_end;
189926deccbSFrançois Tigeot 
190c59a5c48SFrançois Tigeot 			cursor_end = x + w;
191926deccbSFrançois Tigeot 			frame_end = crtc->x + crtc->mode.crtc_hdisplay;
192926deccbSFrançois Tigeot 			if (cursor_end >= frame_end) {
193926deccbSFrançois Tigeot 				w = w - (cursor_end - frame_end);
194926deccbSFrançois Tigeot 				if (!(frame_end & 0x7f))
195926deccbSFrançois Tigeot 					w--;
196c59a5c48SFrançois Tigeot 			} else if (cursor_end <= 0) {
197c59a5c48SFrançois Tigeot 				goto out_of_bounds;
198c59a5c48SFrançois Tigeot 			} else if (!(cursor_end & 0x7f)) {
199926deccbSFrançois Tigeot 				w--;
200926deccbSFrançois Tigeot 			}
201926deccbSFrançois Tigeot 			if (w <= 0) {
202c59a5c48SFrançois Tigeot 				goto out_of_bounds;
203926deccbSFrançois Tigeot 			}
204926deccbSFrançois Tigeot 		}
205926deccbSFrançois Tigeot 	}
206c59a5c48SFrançois Tigeot 
207c59a5c48SFrançois Tigeot 	if (x <= (crtc->x - w) || y <= (crtc->y - radeon_crtc->cursor_height) ||
208c59a5c48SFrançois Tigeot 	    x >= (crtc->x + crtc->mode.hdisplay) ||
209c59a5c48SFrançois Tigeot 	    y >= (crtc->y + crtc->mode.vdisplay))
210c59a5c48SFrançois Tigeot 		goto out_of_bounds;
211c59a5c48SFrançois Tigeot 
212c59a5c48SFrançois Tigeot 	x += xorigin;
213c59a5c48SFrançois Tigeot 	y += yorigin;
214926deccbSFrançois Tigeot 
215926deccbSFrançois Tigeot 	if (ASIC_IS_DCE4(rdev)) {
216926deccbSFrançois Tigeot 		WREG32(EVERGREEN_CUR_POSITION + radeon_crtc->crtc_offset, (x << 16) | y);
217926deccbSFrançois Tigeot 		WREG32(EVERGREEN_CUR_HOT_SPOT + radeon_crtc->crtc_offset, (xorigin << 16) | yorigin);
218926deccbSFrançois Tigeot 		WREG32(EVERGREEN_CUR_SIZE + radeon_crtc->crtc_offset,
219926deccbSFrançois Tigeot 		       ((w - 1) << 16) | (radeon_crtc->cursor_height - 1));
220926deccbSFrançois Tigeot 	} else if (ASIC_IS_AVIVO(rdev)) {
221926deccbSFrançois Tigeot 		WREG32(AVIVO_D1CUR_POSITION + radeon_crtc->crtc_offset, (x << 16) | y);
222926deccbSFrançois Tigeot 		WREG32(AVIVO_D1CUR_HOT_SPOT + radeon_crtc->crtc_offset, (xorigin << 16) | yorigin);
223926deccbSFrançois Tigeot 		WREG32(AVIVO_D1CUR_SIZE + radeon_crtc->crtc_offset,
224926deccbSFrançois Tigeot 		       ((w - 1) << 16) | (radeon_crtc->cursor_height - 1));
225926deccbSFrançois Tigeot 	} else {
226c59a5c48SFrançois Tigeot 		x -= crtc->x;
227c59a5c48SFrançois Tigeot 		y -= crtc->y;
228c59a5c48SFrançois Tigeot 
229926deccbSFrançois Tigeot 		if (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)
230926deccbSFrançois Tigeot 			y *= 2;
231926deccbSFrançois Tigeot 
232926deccbSFrançois Tigeot 		WREG32(RADEON_CUR_HORZ_VERT_OFF + radeon_crtc->crtc_offset,
233926deccbSFrançois Tigeot 		       (RADEON_CUR_LOCK
234926deccbSFrançois Tigeot 			| (xorigin << 16)
235926deccbSFrançois Tigeot 			| yorigin));
236926deccbSFrançois Tigeot 		WREG32(RADEON_CUR_HORZ_VERT_POSN + radeon_crtc->crtc_offset,
237926deccbSFrançois Tigeot 		       (RADEON_CUR_LOCK
238926deccbSFrançois Tigeot 			| (x << 16)
239926deccbSFrançois Tigeot 			| y));
240926deccbSFrançois Tigeot 		/* offset is from DISP(2)_BASE_ADDRESS */
241c59a5c48SFrançois Tigeot 		WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset,
242c59a5c48SFrançois Tigeot 		       radeon_crtc->cursor_addr - radeon_crtc->legacy_display_base_addr +
243c59a5c48SFrançois Tigeot 		       yorigin * 256);
244926deccbSFrançois Tigeot 	}
2457dcf36dcSFrançois Tigeot 
246c59a5c48SFrançois Tigeot 	if (radeon_crtc->cursor_out_of_bounds) {
247c59a5c48SFrançois Tigeot 		radeon_crtc->cursor_out_of_bounds = false;
248c59a5c48SFrançois Tigeot 		if (radeon_crtc->cursor_bo)
249c59a5c48SFrançois Tigeot 			radeon_show_cursor(crtc);
250c59a5c48SFrançois Tigeot 	}
251926deccbSFrançois Tigeot 
252926deccbSFrançois Tigeot 	return 0;
253c59a5c48SFrançois Tigeot 
254c59a5c48SFrançois Tigeot  out_of_bounds:
255c59a5c48SFrançois Tigeot 	if (!radeon_crtc->cursor_out_of_bounds) {
256c59a5c48SFrançois Tigeot 		radeon_hide_cursor(crtc);
257c59a5c48SFrançois Tigeot 		radeon_crtc->cursor_out_of_bounds = true;
258c59a5c48SFrançois Tigeot 	}
259c59a5c48SFrançois Tigeot 	return 0;
260926deccbSFrançois Tigeot }
2617dcf36dcSFrançois Tigeot 
radeon_crtc_cursor_move(struct drm_crtc * crtc,int x,int y)2627dcf36dcSFrançois Tigeot int radeon_crtc_cursor_move(struct drm_crtc *crtc,
2637dcf36dcSFrançois Tigeot 			    int x, int y)
2647dcf36dcSFrançois Tigeot {
2657dcf36dcSFrançois Tigeot 	int ret;
2667dcf36dcSFrançois Tigeot 
2677dcf36dcSFrançois Tigeot 	radeon_lock_cursor(crtc, true);
2687dcf36dcSFrançois Tigeot 	ret = radeon_cursor_move_locked(crtc, x, y);
2697dcf36dcSFrançois Tigeot 	radeon_lock_cursor(crtc, false);
2707dcf36dcSFrançois Tigeot 
2717dcf36dcSFrançois Tigeot 	return ret;
2727dcf36dcSFrançois Tigeot }
2737dcf36dcSFrançois Tigeot 
radeon_crtc_cursor_set2(struct drm_crtc * crtc,struct drm_file * file_priv,uint32_t handle,uint32_t width,uint32_t height,int32_t hot_x,int32_t hot_y)2747dcf36dcSFrançois Tigeot int radeon_crtc_cursor_set2(struct drm_crtc *crtc,
2757dcf36dcSFrançois Tigeot 			    struct drm_file *file_priv,
2767dcf36dcSFrançois Tigeot 			    uint32_t handle,
2777dcf36dcSFrançois Tigeot 			    uint32_t width,
2787dcf36dcSFrançois Tigeot 			    uint32_t height,
2797dcf36dcSFrançois Tigeot 			    int32_t hot_x,
2807dcf36dcSFrançois Tigeot 			    int32_t hot_y)
2817dcf36dcSFrançois Tigeot {
2827dcf36dcSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
283c59a5c48SFrançois Tigeot 	struct radeon_device *rdev = crtc->dev->dev_private;
2847dcf36dcSFrançois Tigeot 	struct drm_gem_object *obj;
285c59a5c48SFrançois Tigeot 	struct radeon_bo *robj;
2867dcf36dcSFrançois Tigeot 	int ret;
2877dcf36dcSFrançois Tigeot 
2887dcf36dcSFrançois Tigeot 	if (!handle) {
2897dcf36dcSFrançois Tigeot 		/* turn off cursor */
2907dcf36dcSFrançois Tigeot 		radeon_hide_cursor(crtc);
2917dcf36dcSFrançois Tigeot 		obj = NULL;
2927dcf36dcSFrançois Tigeot 		goto unpin;
2937dcf36dcSFrançois Tigeot 	}
2947dcf36dcSFrançois Tigeot 
2957dcf36dcSFrançois Tigeot 	if ((width > radeon_crtc->max_cursor_width) ||
2967dcf36dcSFrançois Tigeot 	    (height > radeon_crtc->max_cursor_height)) {
2977dcf36dcSFrançois Tigeot 		DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
2987dcf36dcSFrançois Tigeot 		return -EINVAL;
2997dcf36dcSFrançois Tigeot 	}
3007dcf36dcSFrançois Tigeot 
3017dcf36dcSFrançois Tigeot 	obj = drm_gem_object_lookup(file_priv, handle);
3027dcf36dcSFrançois Tigeot 	if (!obj) {
3037dcf36dcSFrançois Tigeot 		DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, radeon_crtc->crtc_id);
3047dcf36dcSFrançois Tigeot 		return -ENOENT;
3057dcf36dcSFrançois Tigeot 	}
3067dcf36dcSFrançois Tigeot 
307c59a5c48SFrançois Tigeot 	robj = gem_to_radeon_bo(obj);
308c59a5c48SFrançois Tigeot 	ret = radeon_bo_reserve(robj, false);
309c59a5c48SFrançois Tigeot 	if (ret != 0) {
310*3f2dd94aSFrançois Tigeot 		drm_gem_object_put_unlocked(obj);
311c59a5c48SFrançois Tigeot 		return ret;
312c59a5c48SFrançois Tigeot 	}
313c59a5c48SFrançois Tigeot 	/* Only 27 bit offset for legacy cursor */
314c59a5c48SFrançois Tigeot 	ret = radeon_bo_pin_restricted(robj, RADEON_GEM_DOMAIN_VRAM,
315c59a5c48SFrançois Tigeot 				       ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
316c59a5c48SFrançois Tigeot 				       (u64 *)&radeon_crtc->cursor_addr);
317c59a5c48SFrançois Tigeot 	radeon_bo_unreserve(robj);
318c59a5c48SFrançois Tigeot 	if (ret) {
319c59a5c48SFrançois Tigeot 		DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
320*3f2dd94aSFrançois Tigeot 		drm_gem_object_put_unlocked(obj);
321c59a5c48SFrançois Tigeot 		return ret;
322c59a5c48SFrançois Tigeot 	}
3237dcf36dcSFrançois Tigeot 
3247dcf36dcSFrançois Tigeot 	radeon_lock_cursor(crtc, true);
3257dcf36dcSFrançois Tigeot 
326c59a5c48SFrançois Tigeot 	if (width != radeon_crtc->cursor_width ||
327c59a5c48SFrançois Tigeot 	    height != radeon_crtc->cursor_height ||
328c59a5c48SFrançois Tigeot 	    hot_x != radeon_crtc->cursor_hot_x ||
3297dcf36dcSFrançois Tigeot 	    hot_y != radeon_crtc->cursor_hot_y) {
3307dcf36dcSFrançois Tigeot 		int x, y;
3317dcf36dcSFrançois Tigeot 
3327dcf36dcSFrançois Tigeot 		x = radeon_crtc->cursor_x + radeon_crtc->cursor_hot_x - hot_x;
3337dcf36dcSFrançois Tigeot 		y = radeon_crtc->cursor_y + radeon_crtc->cursor_hot_y - hot_y;
3347dcf36dcSFrançois Tigeot 
335c59a5c48SFrançois Tigeot 		radeon_crtc->cursor_width = width;
336c59a5c48SFrançois Tigeot 		radeon_crtc->cursor_height = height;
3377dcf36dcSFrançois Tigeot 		radeon_crtc->cursor_hot_x = hot_x;
3387dcf36dcSFrançois Tigeot 		radeon_crtc->cursor_hot_y = hot_y;
339c59a5c48SFrançois Tigeot 
340c59a5c48SFrançois Tigeot 		radeon_cursor_move_locked(crtc, x, y);
3417dcf36dcSFrançois Tigeot 	}
3427dcf36dcSFrançois Tigeot 
3437dcf36dcSFrançois Tigeot 	radeon_show_cursor(crtc);
3447dcf36dcSFrançois Tigeot 
3457dcf36dcSFrançois Tigeot 	radeon_lock_cursor(crtc, false);
3467dcf36dcSFrançois Tigeot 
3477dcf36dcSFrançois Tigeot unpin:
3487dcf36dcSFrançois Tigeot 	if (radeon_crtc->cursor_bo) {
3497dcf36dcSFrançois Tigeot 		struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
3507dcf36dcSFrançois Tigeot 		ret = radeon_bo_reserve(robj, false);
3517dcf36dcSFrançois Tigeot 		if (likely(ret == 0)) {
3527dcf36dcSFrançois Tigeot 			radeon_bo_unpin(robj);
3537dcf36dcSFrançois Tigeot 			radeon_bo_unreserve(robj);
3547dcf36dcSFrançois Tigeot 		}
355*3f2dd94aSFrançois Tigeot 		drm_gem_object_put_unlocked(radeon_crtc->cursor_bo);
3567dcf36dcSFrançois Tigeot 	}
3577dcf36dcSFrançois Tigeot 
3587dcf36dcSFrançois Tigeot 	radeon_crtc->cursor_bo = obj;
3597dcf36dcSFrançois Tigeot 	return 0;
3607dcf36dcSFrançois Tigeot }
3617dcf36dcSFrançois Tigeot 
3627dcf36dcSFrançois Tigeot /**
3637dcf36dcSFrançois Tigeot  * radeon_cursor_reset - Re-set the current cursor, if any.
3647dcf36dcSFrançois Tigeot  *
3657dcf36dcSFrançois Tigeot  * @crtc: drm crtc
3667dcf36dcSFrançois Tigeot  *
3677dcf36dcSFrançois Tigeot  * If the CRTC passed in currently has a cursor assigned, this function
3687dcf36dcSFrançois Tigeot  * makes sure it's visible.
3697dcf36dcSFrançois Tigeot  */
radeon_cursor_reset(struct drm_crtc * crtc)3707dcf36dcSFrançois Tigeot void radeon_cursor_reset(struct drm_crtc *crtc)
3717dcf36dcSFrançois Tigeot {
3727dcf36dcSFrançois Tigeot 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
3737dcf36dcSFrançois Tigeot 
3747dcf36dcSFrançois Tigeot 	if (radeon_crtc->cursor_bo) {
3757dcf36dcSFrançois Tigeot 		radeon_lock_cursor(crtc, true);
3767dcf36dcSFrançois Tigeot 
3777dcf36dcSFrançois Tigeot 		radeon_cursor_move_locked(crtc, radeon_crtc->cursor_x,
3787dcf36dcSFrançois Tigeot 					  radeon_crtc->cursor_y);
3797dcf36dcSFrançois Tigeot 
3807dcf36dcSFrançois Tigeot 		radeon_show_cursor(crtc);
3817dcf36dcSFrançois Tigeot 
3827dcf36dcSFrançois Tigeot 		radeon_lock_cursor(crtc, false);
3837dcf36dcSFrançois Tigeot 	}
3847dcf36dcSFrançois Tigeot }
385