xref: /OK3568_Linux_fs/kernel/drivers/gpu/drm/radeon/radeon_cursor.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2007-8 Advanced Micro Devices, Inc.
3*4882a593Smuzhiyun  * Copyright 2008 Red Hat Inc.
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Permission is hereby granted, free of charge, to any person obtaining a
6*4882a593Smuzhiyun  * copy of this software and associated documentation files (the "Software"),
7*4882a593Smuzhiyun  * to deal in the Software without restriction, including without limitation
8*4882a593Smuzhiyun  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9*4882a593Smuzhiyun  * and/or sell copies of the Software, and to permit persons to whom the
10*4882a593Smuzhiyun  * Software is furnished to do so, subject to the following conditions:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  * The above copyright notice and this permission notice shall be included in
13*4882a593Smuzhiyun  * all copies or substantial portions of the Software.
14*4882a593Smuzhiyun  *
15*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*4882a593Smuzhiyun  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19*4882a593Smuzhiyun  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20*4882a593Smuzhiyun  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21*4882a593Smuzhiyun  * OTHER DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Authors: Dave Airlie
24*4882a593Smuzhiyun  *          Alex Deucher
25*4882a593Smuzhiyun  */
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #include <drm/drm_device.h>
28*4882a593Smuzhiyun #include <drm/radeon_drm.h>
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #include "radeon.h"
31*4882a593Smuzhiyun 
radeon_lock_cursor(struct drm_crtc * crtc,bool lock)32*4882a593Smuzhiyun static void radeon_lock_cursor(struct drm_crtc *crtc, bool lock)
33*4882a593Smuzhiyun {
34*4882a593Smuzhiyun 	struct radeon_device *rdev = crtc->dev->dev_private;
35*4882a593Smuzhiyun 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
36*4882a593Smuzhiyun 	uint32_t cur_lock;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	if (ASIC_IS_DCE4(rdev)) {
39*4882a593Smuzhiyun 		cur_lock = RREG32(EVERGREEN_CUR_UPDATE + radeon_crtc->crtc_offset);
40*4882a593Smuzhiyun 		if (lock)
41*4882a593Smuzhiyun 			cur_lock |= EVERGREEN_CURSOR_UPDATE_LOCK;
42*4882a593Smuzhiyun 		else
43*4882a593Smuzhiyun 			cur_lock &= ~EVERGREEN_CURSOR_UPDATE_LOCK;
44*4882a593Smuzhiyun 		WREG32(EVERGREEN_CUR_UPDATE + radeon_crtc->crtc_offset, cur_lock);
45*4882a593Smuzhiyun 	} else if (ASIC_IS_AVIVO(rdev)) {
46*4882a593Smuzhiyun 		cur_lock = RREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset);
47*4882a593Smuzhiyun 		if (lock)
48*4882a593Smuzhiyun 			cur_lock |= AVIVO_D1CURSOR_UPDATE_LOCK;
49*4882a593Smuzhiyun 		else
50*4882a593Smuzhiyun 			cur_lock &= ~AVIVO_D1CURSOR_UPDATE_LOCK;
51*4882a593Smuzhiyun 		WREG32(AVIVO_D1CUR_UPDATE + radeon_crtc->crtc_offset, cur_lock);
52*4882a593Smuzhiyun 	} else {
53*4882a593Smuzhiyun 		cur_lock = RREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset);
54*4882a593Smuzhiyun 		if (lock)
55*4882a593Smuzhiyun 			cur_lock |= RADEON_CUR_LOCK;
56*4882a593Smuzhiyun 		else
57*4882a593Smuzhiyun 			cur_lock &= ~RADEON_CUR_LOCK;
58*4882a593Smuzhiyun 		WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset, cur_lock);
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
radeon_hide_cursor(struct drm_crtc * crtc)62*4882a593Smuzhiyun static void radeon_hide_cursor(struct drm_crtc *crtc)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
65*4882a593Smuzhiyun 	struct radeon_device *rdev = crtc->dev->dev_private;
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun 	if (ASIC_IS_DCE4(rdev)) {
68*4882a593Smuzhiyun 		WREG32_IDX(EVERGREEN_CUR_CONTROL + radeon_crtc->crtc_offset,
69*4882a593Smuzhiyun 			   EVERGREEN_CURSOR_MODE(EVERGREEN_CURSOR_24_8_PRE_MULT) |
70*4882a593Smuzhiyun 			   EVERGREEN_CURSOR_URGENT_CONTROL(EVERGREEN_CURSOR_URGENT_1_2));
71*4882a593Smuzhiyun 	} else if (ASIC_IS_AVIVO(rdev)) {
72*4882a593Smuzhiyun 		WREG32_IDX(AVIVO_D1CUR_CONTROL + radeon_crtc->crtc_offset,
73*4882a593Smuzhiyun 			   (AVIVO_D1CURSOR_MODE_24BPP << AVIVO_D1CURSOR_MODE_SHIFT));
74*4882a593Smuzhiyun 	} else {
75*4882a593Smuzhiyun 		u32 reg;
76*4882a593Smuzhiyun 		switch (radeon_crtc->crtc_id) {
77*4882a593Smuzhiyun 		case 0:
78*4882a593Smuzhiyun 			reg = RADEON_CRTC_GEN_CNTL;
79*4882a593Smuzhiyun 			break;
80*4882a593Smuzhiyun 		case 1:
81*4882a593Smuzhiyun 			reg = RADEON_CRTC2_GEN_CNTL;
82*4882a593Smuzhiyun 			break;
83*4882a593Smuzhiyun 		default:
84*4882a593Smuzhiyun 			return;
85*4882a593Smuzhiyun 		}
86*4882a593Smuzhiyun 		WREG32_IDX(reg, RREG32_IDX(reg) & ~RADEON_CRTC_CUR_EN);
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun 
radeon_show_cursor(struct drm_crtc * crtc)90*4882a593Smuzhiyun static void radeon_show_cursor(struct drm_crtc *crtc)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
93*4882a593Smuzhiyun 	struct radeon_device *rdev = crtc->dev->dev_private;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (radeon_crtc->cursor_out_of_bounds)
96*4882a593Smuzhiyun 		return;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	if (ASIC_IS_DCE4(rdev)) {
99*4882a593Smuzhiyun 		WREG32(EVERGREEN_CUR_SURFACE_ADDRESS_HIGH + radeon_crtc->crtc_offset,
100*4882a593Smuzhiyun 		       upper_32_bits(radeon_crtc->cursor_addr));
101*4882a593Smuzhiyun 		WREG32(EVERGREEN_CUR_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
102*4882a593Smuzhiyun 		       lower_32_bits(radeon_crtc->cursor_addr));
103*4882a593Smuzhiyun 		WREG32(RADEON_MM_INDEX, EVERGREEN_CUR_CONTROL + radeon_crtc->crtc_offset);
104*4882a593Smuzhiyun 		WREG32(RADEON_MM_DATA, EVERGREEN_CURSOR_EN |
105*4882a593Smuzhiyun 		       EVERGREEN_CURSOR_MODE(EVERGREEN_CURSOR_24_8_PRE_MULT) |
106*4882a593Smuzhiyun 		       EVERGREEN_CURSOR_URGENT_CONTROL(EVERGREEN_CURSOR_URGENT_1_2));
107*4882a593Smuzhiyun 	} else if (ASIC_IS_AVIVO(rdev)) {
108*4882a593Smuzhiyun 		if (rdev->family >= CHIP_RV770) {
109*4882a593Smuzhiyun 			if (radeon_crtc->crtc_id)
110*4882a593Smuzhiyun 				WREG32(R700_D2CUR_SURFACE_ADDRESS_HIGH,
111*4882a593Smuzhiyun 				       upper_32_bits(radeon_crtc->cursor_addr));
112*4882a593Smuzhiyun 			else
113*4882a593Smuzhiyun 				WREG32(R700_D1CUR_SURFACE_ADDRESS_HIGH,
114*4882a593Smuzhiyun 				       upper_32_bits(radeon_crtc->cursor_addr));
115*4882a593Smuzhiyun 		}
116*4882a593Smuzhiyun 
117*4882a593Smuzhiyun 		WREG32(AVIVO_D1CUR_SURFACE_ADDRESS + radeon_crtc->crtc_offset,
118*4882a593Smuzhiyun 		       lower_32_bits(radeon_crtc->cursor_addr));
119*4882a593Smuzhiyun 		WREG32(RADEON_MM_INDEX, AVIVO_D1CUR_CONTROL + radeon_crtc->crtc_offset);
120*4882a593Smuzhiyun 		WREG32(RADEON_MM_DATA, AVIVO_D1CURSOR_EN |
121*4882a593Smuzhiyun 		       (AVIVO_D1CURSOR_MODE_24BPP << AVIVO_D1CURSOR_MODE_SHIFT));
122*4882a593Smuzhiyun 	} else {
123*4882a593Smuzhiyun 		/* offset is from DISP(2)_BASE_ADDRESS */
124*4882a593Smuzhiyun 		WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset,
125*4882a593Smuzhiyun 		       radeon_crtc->cursor_addr - radeon_crtc->legacy_display_base_addr);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 		switch (radeon_crtc->crtc_id) {
128*4882a593Smuzhiyun 		case 0:
129*4882a593Smuzhiyun 			WREG32(RADEON_MM_INDEX, RADEON_CRTC_GEN_CNTL);
130*4882a593Smuzhiyun 			break;
131*4882a593Smuzhiyun 		case 1:
132*4882a593Smuzhiyun 			WREG32(RADEON_MM_INDEX, RADEON_CRTC2_GEN_CNTL);
133*4882a593Smuzhiyun 			break;
134*4882a593Smuzhiyun 		default:
135*4882a593Smuzhiyun 			return;
136*4882a593Smuzhiyun 		}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 		WREG32_P(RADEON_MM_DATA, (RADEON_CRTC_CUR_EN |
139*4882a593Smuzhiyun 					  (RADEON_CRTC_CUR_MODE_24BPP << RADEON_CRTC_CUR_MODE_SHIFT)),
140*4882a593Smuzhiyun 			 ~(RADEON_CRTC_CUR_EN | RADEON_CRTC_CUR_MODE_MASK));
141*4882a593Smuzhiyun 	}
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun 
radeon_cursor_move_locked(struct drm_crtc * crtc,int x,int y)144*4882a593Smuzhiyun static int radeon_cursor_move_locked(struct drm_crtc *crtc, int x, int y)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
147*4882a593Smuzhiyun 	struct radeon_device *rdev = crtc->dev->dev_private;
148*4882a593Smuzhiyun 	int xorigin = 0, yorigin = 0;
149*4882a593Smuzhiyun 	int w = radeon_crtc->cursor_width;
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	radeon_crtc->cursor_x = x;
152*4882a593Smuzhiyun 	radeon_crtc->cursor_y = y;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	if (ASIC_IS_AVIVO(rdev)) {
155*4882a593Smuzhiyun 		/* avivo cursor are offset into the total surface */
156*4882a593Smuzhiyun 		x += crtc->x;
157*4882a593Smuzhiyun 		y += crtc->y;
158*4882a593Smuzhiyun 	}
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	if (x < 0)
161*4882a593Smuzhiyun 		xorigin = min(-x, radeon_crtc->max_cursor_width - 1);
162*4882a593Smuzhiyun 	if (y < 0)
163*4882a593Smuzhiyun 		yorigin = min(-y, radeon_crtc->max_cursor_height - 1);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (!ASIC_IS_AVIVO(rdev)) {
166*4882a593Smuzhiyun 		x += crtc->x;
167*4882a593Smuzhiyun 		y += crtc->y;
168*4882a593Smuzhiyun 	}
169*4882a593Smuzhiyun 	DRM_DEBUG("x %d y %d c->x %d c->y %d\n", x, y, crtc->x, crtc->y);
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	/* fixed on DCE6 and newer */
172*4882a593Smuzhiyun 	if (ASIC_IS_AVIVO(rdev) && !ASIC_IS_DCE6(rdev)) {
173*4882a593Smuzhiyun 		int i = 0;
174*4882a593Smuzhiyun 		struct drm_crtc *crtc_p;
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 		/*
177*4882a593Smuzhiyun 		 * avivo cursor image can't end on 128 pixel boundary or
178*4882a593Smuzhiyun 		 * go past the end of the frame if both crtcs are enabled
179*4882a593Smuzhiyun 		 *
180*4882a593Smuzhiyun 		 * NOTE: It is safe to access crtc->enabled of other crtcs
181*4882a593Smuzhiyun 		 * without holding either the mode_config lock or the other
182*4882a593Smuzhiyun 		 * crtc's lock as long as write access to this flag _always_
183*4882a593Smuzhiyun 		 * grabs all locks.
184*4882a593Smuzhiyun 		 */
185*4882a593Smuzhiyun 		list_for_each_entry(crtc_p, &crtc->dev->mode_config.crtc_list, head) {
186*4882a593Smuzhiyun 			if (crtc_p->enabled)
187*4882a593Smuzhiyun 				i++;
188*4882a593Smuzhiyun 		}
189*4882a593Smuzhiyun 		if (i > 1) {
190*4882a593Smuzhiyun 			int cursor_end, frame_end;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 			cursor_end = x + w;
193*4882a593Smuzhiyun 			frame_end = crtc->x + crtc->mode.crtc_hdisplay;
194*4882a593Smuzhiyun 			if (cursor_end >= frame_end) {
195*4882a593Smuzhiyun 				w = w - (cursor_end - frame_end);
196*4882a593Smuzhiyun 				if (!(frame_end & 0x7f))
197*4882a593Smuzhiyun 					w--;
198*4882a593Smuzhiyun 			} else if (cursor_end <= 0) {
199*4882a593Smuzhiyun 				goto out_of_bounds;
200*4882a593Smuzhiyun 			} else if (!(cursor_end & 0x7f)) {
201*4882a593Smuzhiyun 				w--;
202*4882a593Smuzhiyun 			}
203*4882a593Smuzhiyun 			if (w <= 0) {
204*4882a593Smuzhiyun 				goto out_of_bounds;
205*4882a593Smuzhiyun 			}
206*4882a593Smuzhiyun 		}
207*4882a593Smuzhiyun 	}
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 	if (x <= (crtc->x - w) || y <= (crtc->y - radeon_crtc->cursor_height) ||
210*4882a593Smuzhiyun 	    x >= (crtc->x + crtc->mode.hdisplay) ||
211*4882a593Smuzhiyun 	    y >= (crtc->y + crtc->mode.vdisplay))
212*4882a593Smuzhiyun 		goto out_of_bounds;
213*4882a593Smuzhiyun 
214*4882a593Smuzhiyun 	x += xorigin;
215*4882a593Smuzhiyun 	y += yorigin;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun 	if (ASIC_IS_DCE4(rdev)) {
218*4882a593Smuzhiyun 		WREG32(EVERGREEN_CUR_POSITION + radeon_crtc->crtc_offset, (x << 16) | y);
219*4882a593Smuzhiyun 		WREG32(EVERGREEN_CUR_HOT_SPOT + radeon_crtc->crtc_offset, (xorigin << 16) | yorigin);
220*4882a593Smuzhiyun 		WREG32(EVERGREEN_CUR_SIZE + radeon_crtc->crtc_offset,
221*4882a593Smuzhiyun 		       ((w - 1) << 16) | (radeon_crtc->cursor_height - 1));
222*4882a593Smuzhiyun 	} else if (ASIC_IS_AVIVO(rdev)) {
223*4882a593Smuzhiyun 		WREG32(AVIVO_D1CUR_POSITION + radeon_crtc->crtc_offset, (x << 16) | y);
224*4882a593Smuzhiyun 		WREG32(AVIVO_D1CUR_HOT_SPOT + radeon_crtc->crtc_offset, (xorigin << 16) | yorigin);
225*4882a593Smuzhiyun 		WREG32(AVIVO_D1CUR_SIZE + radeon_crtc->crtc_offset,
226*4882a593Smuzhiyun 		       ((w - 1) << 16) | (radeon_crtc->cursor_height - 1));
227*4882a593Smuzhiyun 	} else {
228*4882a593Smuzhiyun 		x -= crtc->x;
229*4882a593Smuzhiyun 		y -= crtc->y;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 		if (crtc->mode.flags & DRM_MODE_FLAG_DBLSCAN)
232*4882a593Smuzhiyun 			y *= 2;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 		WREG32(RADEON_CUR_HORZ_VERT_OFF + radeon_crtc->crtc_offset,
235*4882a593Smuzhiyun 		       (RADEON_CUR_LOCK
236*4882a593Smuzhiyun 			| (xorigin << 16)
237*4882a593Smuzhiyun 			| yorigin));
238*4882a593Smuzhiyun 		WREG32(RADEON_CUR_HORZ_VERT_POSN + radeon_crtc->crtc_offset,
239*4882a593Smuzhiyun 		       (RADEON_CUR_LOCK
240*4882a593Smuzhiyun 			| (x << 16)
241*4882a593Smuzhiyun 			| y));
242*4882a593Smuzhiyun 		/* offset is from DISP(2)_BASE_ADDRESS */
243*4882a593Smuzhiyun 		WREG32(RADEON_CUR_OFFSET + radeon_crtc->crtc_offset,
244*4882a593Smuzhiyun 		       radeon_crtc->cursor_addr - radeon_crtc->legacy_display_base_addr +
245*4882a593Smuzhiyun 		       yorigin * 256);
246*4882a593Smuzhiyun 	}
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 	if (radeon_crtc->cursor_out_of_bounds) {
249*4882a593Smuzhiyun 		radeon_crtc->cursor_out_of_bounds = false;
250*4882a593Smuzhiyun 		if (radeon_crtc->cursor_bo)
251*4882a593Smuzhiyun 			radeon_show_cursor(crtc);
252*4882a593Smuzhiyun 	}
253*4882a593Smuzhiyun 
254*4882a593Smuzhiyun 	return 0;
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun  out_of_bounds:
257*4882a593Smuzhiyun 	if (!radeon_crtc->cursor_out_of_bounds) {
258*4882a593Smuzhiyun 		radeon_hide_cursor(crtc);
259*4882a593Smuzhiyun 		radeon_crtc->cursor_out_of_bounds = true;
260*4882a593Smuzhiyun 	}
261*4882a593Smuzhiyun 	return 0;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun 
radeon_crtc_cursor_move(struct drm_crtc * crtc,int x,int y)264*4882a593Smuzhiyun int radeon_crtc_cursor_move(struct drm_crtc *crtc,
265*4882a593Smuzhiyun 			    int x, int y)
266*4882a593Smuzhiyun {
267*4882a593Smuzhiyun 	int ret;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	radeon_lock_cursor(crtc, true);
270*4882a593Smuzhiyun 	ret = radeon_cursor_move_locked(crtc, x, y);
271*4882a593Smuzhiyun 	radeon_lock_cursor(crtc, false);
272*4882a593Smuzhiyun 
273*4882a593Smuzhiyun 	return ret;
274*4882a593Smuzhiyun }
275*4882a593Smuzhiyun 
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)276*4882a593Smuzhiyun int radeon_crtc_cursor_set2(struct drm_crtc *crtc,
277*4882a593Smuzhiyun 			    struct drm_file *file_priv,
278*4882a593Smuzhiyun 			    uint32_t handle,
279*4882a593Smuzhiyun 			    uint32_t width,
280*4882a593Smuzhiyun 			    uint32_t height,
281*4882a593Smuzhiyun 			    int32_t hot_x,
282*4882a593Smuzhiyun 			    int32_t hot_y)
283*4882a593Smuzhiyun {
284*4882a593Smuzhiyun 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
285*4882a593Smuzhiyun 	struct radeon_device *rdev = crtc->dev->dev_private;
286*4882a593Smuzhiyun 	struct drm_gem_object *obj;
287*4882a593Smuzhiyun 	struct radeon_bo *robj;
288*4882a593Smuzhiyun 	int ret;
289*4882a593Smuzhiyun 
290*4882a593Smuzhiyun 	if (!handle) {
291*4882a593Smuzhiyun 		/* turn off cursor */
292*4882a593Smuzhiyun 		radeon_hide_cursor(crtc);
293*4882a593Smuzhiyun 		obj = NULL;
294*4882a593Smuzhiyun 		goto unpin;
295*4882a593Smuzhiyun 	}
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	if ((width > radeon_crtc->max_cursor_width) ||
298*4882a593Smuzhiyun 	    (height > radeon_crtc->max_cursor_height)) {
299*4882a593Smuzhiyun 		DRM_ERROR("bad cursor width or height %d x %d\n", width, height);
300*4882a593Smuzhiyun 		return -EINVAL;
301*4882a593Smuzhiyun 	}
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun 	obj = drm_gem_object_lookup(file_priv, handle);
304*4882a593Smuzhiyun 	if (!obj) {
305*4882a593Smuzhiyun 		DRM_ERROR("Cannot find cursor object %x for crtc %d\n", handle, radeon_crtc->crtc_id);
306*4882a593Smuzhiyun 		return -ENOENT;
307*4882a593Smuzhiyun 	}
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	robj = gem_to_radeon_bo(obj);
310*4882a593Smuzhiyun 	ret = radeon_bo_reserve(robj, false);
311*4882a593Smuzhiyun 	if (ret != 0) {
312*4882a593Smuzhiyun 		drm_gem_object_put(obj);
313*4882a593Smuzhiyun 		return ret;
314*4882a593Smuzhiyun 	}
315*4882a593Smuzhiyun 	/* Only 27 bit offset for legacy cursor */
316*4882a593Smuzhiyun 	ret = radeon_bo_pin_restricted(robj, RADEON_GEM_DOMAIN_VRAM,
317*4882a593Smuzhiyun 				       ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27,
318*4882a593Smuzhiyun 				       &radeon_crtc->cursor_addr);
319*4882a593Smuzhiyun 	radeon_bo_unreserve(robj);
320*4882a593Smuzhiyun 	if (ret) {
321*4882a593Smuzhiyun 		DRM_ERROR("Failed to pin new cursor BO (%d)\n", ret);
322*4882a593Smuzhiyun 		drm_gem_object_put(obj);
323*4882a593Smuzhiyun 		return ret;
324*4882a593Smuzhiyun 	}
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	radeon_lock_cursor(crtc, true);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (width != radeon_crtc->cursor_width ||
329*4882a593Smuzhiyun 	    height != radeon_crtc->cursor_height ||
330*4882a593Smuzhiyun 	    hot_x != radeon_crtc->cursor_hot_x ||
331*4882a593Smuzhiyun 	    hot_y != radeon_crtc->cursor_hot_y) {
332*4882a593Smuzhiyun 		int x, y;
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 		x = radeon_crtc->cursor_x + radeon_crtc->cursor_hot_x - hot_x;
335*4882a593Smuzhiyun 		y = radeon_crtc->cursor_y + radeon_crtc->cursor_hot_y - hot_y;
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun 		radeon_crtc->cursor_width = width;
338*4882a593Smuzhiyun 		radeon_crtc->cursor_height = height;
339*4882a593Smuzhiyun 		radeon_crtc->cursor_hot_x = hot_x;
340*4882a593Smuzhiyun 		radeon_crtc->cursor_hot_y = hot_y;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 		radeon_cursor_move_locked(crtc, x, y);
343*4882a593Smuzhiyun 	}
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	radeon_show_cursor(crtc);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 	radeon_lock_cursor(crtc, false);
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun unpin:
350*4882a593Smuzhiyun 	if (radeon_crtc->cursor_bo) {
351*4882a593Smuzhiyun 		struct radeon_bo *robj = gem_to_radeon_bo(radeon_crtc->cursor_bo);
352*4882a593Smuzhiyun 		ret = radeon_bo_reserve(robj, false);
353*4882a593Smuzhiyun 		if (likely(ret == 0)) {
354*4882a593Smuzhiyun 			radeon_bo_unpin(robj);
355*4882a593Smuzhiyun 			radeon_bo_unreserve(robj);
356*4882a593Smuzhiyun 		}
357*4882a593Smuzhiyun 		drm_gem_object_put(radeon_crtc->cursor_bo);
358*4882a593Smuzhiyun 	}
359*4882a593Smuzhiyun 
360*4882a593Smuzhiyun 	radeon_crtc->cursor_bo = obj;
361*4882a593Smuzhiyun 	return 0;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun /**
365*4882a593Smuzhiyun  * radeon_cursor_reset - Re-set the current cursor, if any.
366*4882a593Smuzhiyun  *
367*4882a593Smuzhiyun  * @crtc: drm crtc
368*4882a593Smuzhiyun  *
369*4882a593Smuzhiyun  * If the CRTC passed in currently has a cursor assigned, this function
370*4882a593Smuzhiyun  * makes sure it's visible.
371*4882a593Smuzhiyun  */
radeon_cursor_reset(struct drm_crtc * crtc)372*4882a593Smuzhiyun void radeon_cursor_reset(struct drm_crtc *crtc)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun 	struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	if (radeon_crtc->cursor_bo) {
377*4882a593Smuzhiyun 		radeon_lock_cursor(crtc, true);
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 		radeon_cursor_move_locked(crtc, radeon_crtc->cursor_x,
380*4882a593Smuzhiyun 					  radeon_crtc->cursor_y);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 		radeon_show_cursor(crtc);
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun 		radeon_lock_cursor(crtc, false);
385*4882a593Smuzhiyun 	}
386*4882a593Smuzhiyun }
387