xref: /linux/drivers/gpu/drm/ast/ast_cursor.c (revision 1260ed77798502de9c98020040d2995008de10cc)
1*e82e1a0cSThomas Zimmermann // SPDX-License-Identifier: MIT
2*e82e1a0cSThomas Zimmermann /*
3*e82e1a0cSThomas Zimmermann  * Permission is hereby granted, free of charge, to any person obtaining a
4*e82e1a0cSThomas Zimmermann  * copy of this software and associated documentation files (the
5*e82e1a0cSThomas Zimmermann  * "Software"), to deal in the Software without restriction, including
6*e82e1a0cSThomas Zimmermann  * without limitation the rights to use, copy, modify, merge, publish,
7*e82e1a0cSThomas Zimmermann  * distribute, sub license, and/or sell copies of the Software, and to
8*e82e1a0cSThomas Zimmermann  * permit persons to whom the Software is furnished to do so, subject to
9*e82e1a0cSThomas Zimmermann  * the following conditions:
10*e82e1a0cSThomas Zimmermann  *
11*e82e1a0cSThomas Zimmermann  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12*e82e1a0cSThomas Zimmermann  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13*e82e1a0cSThomas Zimmermann  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
14*e82e1a0cSThomas Zimmermann  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
15*e82e1a0cSThomas Zimmermann  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16*e82e1a0cSThomas Zimmermann  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
17*e82e1a0cSThomas Zimmermann  * USE OR OTHER DEALINGS IN THE SOFTWARE.
18*e82e1a0cSThomas Zimmermann  *
19*e82e1a0cSThomas Zimmermann  * The above copyright notice and this permission notice (including the
20*e82e1a0cSThomas Zimmermann  * next paragraph) shall be included in all copies or substantial portions
21*e82e1a0cSThomas Zimmermann  * of the Software.
22*e82e1a0cSThomas Zimmermann  */
23*e82e1a0cSThomas Zimmermann 
24*e82e1a0cSThomas Zimmermann #include <linux/bits.h>
25*e82e1a0cSThomas Zimmermann #include <linux/sizes.h>
26*e82e1a0cSThomas Zimmermann 
27*e82e1a0cSThomas Zimmermann #include <drm/drm_atomic.h>
28*e82e1a0cSThomas Zimmermann #include <drm/drm_damage_helper.h>
29*e82e1a0cSThomas Zimmermann #include <drm/drm_format_helper.h>
30*e82e1a0cSThomas Zimmermann #include <drm/drm_gem_atomic_helper.h>
31*e82e1a0cSThomas Zimmermann #include <drm/drm_print.h>
32*e82e1a0cSThomas Zimmermann 
33*e82e1a0cSThomas Zimmermann #include "ast_drv.h"
34*e82e1a0cSThomas Zimmermann 
35*e82e1a0cSThomas Zimmermann /*
36*e82e1a0cSThomas Zimmermann  * Hardware cursor
37*e82e1a0cSThomas Zimmermann  */
38*e82e1a0cSThomas Zimmermann 
39*e82e1a0cSThomas Zimmermann /* define for signature structure */
40*e82e1a0cSThomas Zimmermann #define AST_HWC_SIGNATURE_CHECKSUM	0x00
41*e82e1a0cSThomas Zimmermann #define AST_HWC_SIGNATURE_SizeX		0x04
42*e82e1a0cSThomas Zimmermann #define AST_HWC_SIGNATURE_SizeY		0x08
43*e82e1a0cSThomas Zimmermann #define AST_HWC_SIGNATURE_X		0x0C
44*e82e1a0cSThomas Zimmermann #define AST_HWC_SIGNATURE_Y		0x10
45*e82e1a0cSThomas Zimmermann #define AST_HWC_SIGNATURE_HOTSPOTX	0x14
46*e82e1a0cSThomas Zimmermann #define AST_HWC_SIGNATURE_HOTSPOTY	0x18
47*e82e1a0cSThomas Zimmermann 
48*e82e1a0cSThomas Zimmermann static u32 ast_cursor_calculate_checksum(const void *src, unsigned int width, unsigned int height)
49*e82e1a0cSThomas Zimmermann {
50*e82e1a0cSThomas Zimmermann 	u32 csum = 0;
51*e82e1a0cSThomas Zimmermann 	unsigned int one_pixel_copy = width & BIT(0);
52*e82e1a0cSThomas Zimmermann 	unsigned int two_pixel_copy = width - one_pixel_copy;
53*e82e1a0cSThomas Zimmermann 	unsigned int trailing_bytes = (AST_MAX_HWC_WIDTH - width) * sizeof(u16);
54*e82e1a0cSThomas Zimmermann 	unsigned int x, y;
55*e82e1a0cSThomas Zimmermann 
56*e82e1a0cSThomas Zimmermann 	for (y = 0; y < height; y++) {
57*e82e1a0cSThomas Zimmermann 		for (x = 0; x < two_pixel_copy; x += 2) {
58*e82e1a0cSThomas Zimmermann 			const u32 *src32 = (const u32 *)src;
59*e82e1a0cSThomas Zimmermann 
60*e82e1a0cSThomas Zimmermann 			csum += *src32;
61*e82e1a0cSThomas Zimmermann 			src += SZ_4;
62*e82e1a0cSThomas Zimmermann 		}
63*e82e1a0cSThomas Zimmermann 		if (one_pixel_copy) {
64*e82e1a0cSThomas Zimmermann 			const u16 *src16 = (const u16 *)src;
65*e82e1a0cSThomas Zimmermann 
66*e82e1a0cSThomas Zimmermann 			csum += *src16;
67*e82e1a0cSThomas Zimmermann 			src += SZ_2;
68*e82e1a0cSThomas Zimmermann 		}
69*e82e1a0cSThomas Zimmermann 		src += trailing_bytes;
70*e82e1a0cSThomas Zimmermann 	}
71*e82e1a0cSThomas Zimmermann 
72*e82e1a0cSThomas Zimmermann 	return csum;
73*e82e1a0cSThomas Zimmermann }
74*e82e1a0cSThomas Zimmermann 
75*e82e1a0cSThomas Zimmermann static void ast_set_cursor_image(struct ast_device *ast, const u8 *src,
76*e82e1a0cSThomas Zimmermann 				 unsigned int width, unsigned int height)
77*e82e1a0cSThomas Zimmermann {
78*e82e1a0cSThomas Zimmermann 	u8 __iomem *dst = ast->cursor_plane.base.vaddr;
79*e82e1a0cSThomas Zimmermann 	u32 csum;
80*e82e1a0cSThomas Zimmermann 
81*e82e1a0cSThomas Zimmermann 	csum = ast_cursor_calculate_checksum(src, width, height);
82*e82e1a0cSThomas Zimmermann 
83*e82e1a0cSThomas Zimmermann 	/* write pixel data */
84*e82e1a0cSThomas Zimmermann 	memcpy_toio(dst, src, AST_HWC_SIZE);
85*e82e1a0cSThomas Zimmermann 
86*e82e1a0cSThomas Zimmermann 	/* write checksum + signature */
87*e82e1a0cSThomas Zimmermann 	dst += AST_HWC_SIZE;
88*e82e1a0cSThomas Zimmermann 	writel(csum, dst);
89*e82e1a0cSThomas Zimmermann 	writel(width, dst + AST_HWC_SIGNATURE_SizeX);
90*e82e1a0cSThomas Zimmermann 	writel(height, dst + AST_HWC_SIGNATURE_SizeY);
91*e82e1a0cSThomas Zimmermann 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTX);
92*e82e1a0cSThomas Zimmermann 	writel(0, dst + AST_HWC_SIGNATURE_HOTSPOTY);
93*e82e1a0cSThomas Zimmermann }
94*e82e1a0cSThomas Zimmermann 
95*e82e1a0cSThomas Zimmermann static void ast_set_cursor_base(struct ast_device *ast, u64 address)
96*e82e1a0cSThomas Zimmermann {
97*e82e1a0cSThomas Zimmermann 	u8 addr0 = (address >> 3) & 0xff;
98*e82e1a0cSThomas Zimmermann 	u8 addr1 = (address >> 11) & 0xff;
99*e82e1a0cSThomas Zimmermann 	u8 addr2 = (address >> 19) & 0xff;
100*e82e1a0cSThomas Zimmermann 
101*e82e1a0cSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc8, addr0);
102*e82e1a0cSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc9, addr1);
103*e82e1a0cSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xca, addr2);
104*e82e1a0cSThomas Zimmermann }
105*e82e1a0cSThomas Zimmermann 
106*e82e1a0cSThomas Zimmermann static void ast_set_cursor_location(struct ast_device *ast, u16 x, u16 y,
107*e82e1a0cSThomas Zimmermann 				    u8 x_offset, u8 y_offset)
108*e82e1a0cSThomas Zimmermann {
109*e82e1a0cSThomas Zimmermann 	u8 x0 = (x & 0x00ff);
110*e82e1a0cSThomas Zimmermann 	u8 x1 = (x & 0x0f00) >> 8;
111*e82e1a0cSThomas Zimmermann 	u8 y0 = (y & 0x00ff);
112*e82e1a0cSThomas Zimmermann 	u8 y1 = (y & 0x0700) >> 8;
113*e82e1a0cSThomas Zimmermann 
114*e82e1a0cSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc2, x_offset);
115*e82e1a0cSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc3, y_offset);
116*e82e1a0cSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc4, x0);
117*e82e1a0cSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc5, x1);
118*e82e1a0cSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc6, y0);
119*e82e1a0cSThomas Zimmermann 	ast_set_index_reg(ast, AST_IO_VGACRI, 0xc7, y1);
120*e82e1a0cSThomas Zimmermann }
121*e82e1a0cSThomas Zimmermann 
122*e82e1a0cSThomas Zimmermann static void ast_set_cursor_enabled(struct ast_device *ast, bool enabled)
123*e82e1a0cSThomas Zimmermann {
124*e82e1a0cSThomas Zimmermann 	static const u8 mask = (u8)~(AST_IO_VGACRCB_HWC_16BPP |
125*e82e1a0cSThomas Zimmermann 				     AST_IO_VGACRCB_HWC_ENABLED);
126*e82e1a0cSThomas Zimmermann 
127*e82e1a0cSThomas Zimmermann 	u8 vgacrcb = AST_IO_VGACRCB_HWC_16BPP;
128*e82e1a0cSThomas Zimmermann 
129*e82e1a0cSThomas Zimmermann 	if (enabled)
130*e82e1a0cSThomas Zimmermann 		vgacrcb |= AST_IO_VGACRCB_HWC_ENABLED;
131*e82e1a0cSThomas Zimmermann 
132*e82e1a0cSThomas Zimmermann 	ast_set_index_reg_mask(ast, AST_IO_VGACRI, 0xcb, mask, vgacrcb);
133*e82e1a0cSThomas Zimmermann }
134*e82e1a0cSThomas Zimmermann 
135*e82e1a0cSThomas Zimmermann /*
136*e82e1a0cSThomas Zimmermann  * Cursor plane
137*e82e1a0cSThomas Zimmermann  */
138*e82e1a0cSThomas Zimmermann 
139*e82e1a0cSThomas Zimmermann static const uint32_t ast_cursor_plane_formats[] = {
140*e82e1a0cSThomas Zimmermann 	DRM_FORMAT_ARGB4444,
141*e82e1a0cSThomas Zimmermann 	DRM_FORMAT_ARGB8888,
142*e82e1a0cSThomas Zimmermann };
143*e82e1a0cSThomas Zimmermann 
144*e82e1a0cSThomas Zimmermann static int ast_cursor_plane_helper_atomic_check(struct drm_plane *plane,
145*e82e1a0cSThomas Zimmermann 						struct drm_atomic_state *state)
146*e82e1a0cSThomas Zimmermann {
147*e82e1a0cSThomas Zimmermann 	struct drm_plane_state *new_plane_state = drm_atomic_get_new_plane_state(state, plane);
148*e82e1a0cSThomas Zimmermann 	struct drm_framebuffer *new_fb = new_plane_state->fb;
149*e82e1a0cSThomas Zimmermann 	struct drm_crtc_state *new_crtc_state = NULL;
150*e82e1a0cSThomas Zimmermann 	int ret;
151*e82e1a0cSThomas Zimmermann 
152*e82e1a0cSThomas Zimmermann 	if (new_plane_state->crtc)
153*e82e1a0cSThomas Zimmermann 		new_crtc_state = drm_atomic_get_new_crtc_state(state, new_plane_state->crtc);
154*e82e1a0cSThomas Zimmermann 
155*e82e1a0cSThomas Zimmermann 	ret = drm_atomic_helper_check_plane_state(new_plane_state, new_crtc_state,
156*e82e1a0cSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
157*e82e1a0cSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
158*e82e1a0cSThomas Zimmermann 						  true, true);
159*e82e1a0cSThomas Zimmermann 	if (ret || !new_plane_state->visible)
160*e82e1a0cSThomas Zimmermann 		return ret;
161*e82e1a0cSThomas Zimmermann 
162*e82e1a0cSThomas Zimmermann 	if (new_fb->width > AST_MAX_HWC_WIDTH || new_fb->height > AST_MAX_HWC_HEIGHT)
163*e82e1a0cSThomas Zimmermann 		return -EINVAL;
164*e82e1a0cSThomas Zimmermann 
165*e82e1a0cSThomas Zimmermann 	return 0;
166*e82e1a0cSThomas Zimmermann }
167*e82e1a0cSThomas Zimmermann 
168*e82e1a0cSThomas Zimmermann static void ast_cursor_plane_helper_atomic_update(struct drm_plane *plane,
169*e82e1a0cSThomas Zimmermann 						  struct drm_atomic_state *state)
170*e82e1a0cSThomas Zimmermann {
171*e82e1a0cSThomas Zimmermann 	struct ast_cursor_plane *ast_cursor_plane = to_ast_cursor_plane(plane);
172*e82e1a0cSThomas Zimmermann 	struct ast_plane *ast_plane = to_ast_plane(plane);
173*e82e1a0cSThomas Zimmermann 	struct drm_plane_state *plane_state = drm_atomic_get_new_plane_state(state, plane);
174*e82e1a0cSThomas Zimmermann 	struct drm_shadow_plane_state *shadow_plane_state = to_drm_shadow_plane_state(plane_state);
175*e82e1a0cSThomas Zimmermann 	struct drm_framebuffer *fb = plane_state->fb;
176*e82e1a0cSThomas Zimmermann 	struct drm_plane_state *old_plane_state = drm_atomic_get_old_plane_state(state, plane);
177*e82e1a0cSThomas Zimmermann 	struct ast_device *ast = to_ast_device(plane->dev);
178*e82e1a0cSThomas Zimmermann 	struct drm_rect damage;
179*e82e1a0cSThomas Zimmermann 	u64 dst_off = ast_plane->offset;
180*e82e1a0cSThomas Zimmermann 	u8 __iomem *dst = ast_plane->vaddr; /* TODO: Use mapping abstraction properly */
181*e82e1a0cSThomas Zimmermann 	u8 __iomem *sig = dst + AST_HWC_SIZE; /* TODO: Use mapping abstraction properly */
182*e82e1a0cSThomas Zimmermann 	unsigned int offset_x, offset_y;
183*e82e1a0cSThomas Zimmermann 	u16 x, y;
184*e82e1a0cSThomas Zimmermann 	u8 x_offset, y_offset;
185*e82e1a0cSThomas Zimmermann 
186*e82e1a0cSThomas Zimmermann 	/*
187*e82e1a0cSThomas Zimmermann 	 * Do data transfer to hardware buffer and point the scanout
188*e82e1a0cSThomas Zimmermann 	 * engine to the offset.
189*e82e1a0cSThomas Zimmermann 	 */
190*e82e1a0cSThomas Zimmermann 
191*e82e1a0cSThomas Zimmermann 	if (drm_atomic_helper_damage_merged(old_plane_state, plane_state, &damage)) {
192*e82e1a0cSThomas Zimmermann 		u8 *argb4444;
193*e82e1a0cSThomas Zimmermann 
194*e82e1a0cSThomas Zimmermann 		switch (fb->format->format) {
195*e82e1a0cSThomas Zimmermann 		case DRM_FORMAT_ARGB4444:
196*e82e1a0cSThomas Zimmermann 			argb4444 = shadow_plane_state->data[0].vaddr;
197*e82e1a0cSThomas Zimmermann 			break;
198*e82e1a0cSThomas Zimmermann 		default:
199*e82e1a0cSThomas Zimmermann 			argb4444 = ast_cursor_plane->argb4444;
200*e82e1a0cSThomas Zimmermann 			{
201*e82e1a0cSThomas Zimmermann 				struct iosys_map argb4444_dst[DRM_FORMAT_MAX_PLANES] = {
202*e82e1a0cSThomas Zimmermann 					IOSYS_MAP_INIT_VADDR(argb4444),
203*e82e1a0cSThomas Zimmermann 				};
204*e82e1a0cSThomas Zimmermann 				unsigned int argb4444_dst_pitch[DRM_FORMAT_MAX_PLANES] = {
205*e82e1a0cSThomas Zimmermann 					AST_HWC_PITCH,
206*e82e1a0cSThomas Zimmermann 				};
207*e82e1a0cSThomas Zimmermann 
208*e82e1a0cSThomas Zimmermann 				drm_fb_argb8888_to_argb4444(argb4444_dst, argb4444_dst_pitch,
209*e82e1a0cSThomas Zimmermann 							    shadow_plane_state->data, fb, &damage,
210*e82e1a0cSThomas Zimmermann 							    &shadow_plane_state->fmtcnv_state);
211*e82e1a0cSThomas Zimmermann 			}
212*e82e1a0cSThomas Zimmermann 			break;
213*e82e1a0cSThomas Zimmermann 		}
214*e82e1a0cSThomas Zimmermann 		ast_set_cursor_image(ast, argb4444, fb->width, fb->height);
215*e82e1a0cSThomas Zimmermann 		ast_set_cursor_base(ast, dst_off);
216*e82e1a0cSThomas Zimmermann 	}
217*e82e1a0cSThomas Zimmermann 
218*e82e1a0cSThomas Zimmermann 	/*
219*e82e1a0cSThomas Zimmermann 	 * Update location in HWC signature and registers.
220*e82e1a0cSThomas Zimmermann 	 */
221*e82e1a0cSThomas Zimmermann 
222*e82e1a0cSThomas Zimmermann 	writel(plane_state->crtc_x, sig + AST_HWC_SIGNATURE_X);
223*e82e1a0cSThomas Zimmermann 	writel(plane_state->crtc_y, sig + AST_HWC_SIGNATURE_Y);
224*e82e1a0cSThomas Zimmermann 
225*e82e1a0cSThomas Zimmermann 	offset_x = AST_MAX_HWC_WIDTH - fb->width;
226*e82e1a0cSThomas Zimmermann 	offset_y = AST_MAX_HWC_HEIGHT - fb->height;
227*e82e1a0cSThomas Zimmermann 
228*e82e1a0cSThomas Zimmermann 	if (plane_state->crtc_x < 0) {
229*e82e1a0cSThomas Zimmermann 		x_offset = (-plane_state->crtc_x) + offset_x;
230*e82e1a0cSThomas Zimmermann 		x = 0;
231*e82e1a0cSThomas Zimmermann 	} else {
232*e82e1a0cSThomas Zimmermann 		x_offset = offset_x;
233*e82e1a0cSThomas Zimmermann 		x = plane_state->crtc_x;
234*e82e1a0cSThomas Zimmermann 	}
235*e82e1a0cSThomas Zimmermann 	if (plane_state->crtc_y < 0) {
236*e82e1a0cSThomas Zimmermann 		y_offset = (-plane_state->crtc_y) + offset_y;
237*e82e1a0cSThomas Zimmermann 		y = 0;
238*e82e1a0cSThomas Zimmermann 	} else {
239*e82e1a0cSThomas Zimmermann 		y_offset = offset_y;
240*e82e1a0cSThomas Zimmermann 		y = plane_state->crtc_y;
241*e82e1a0cSThomas Zimmermann 	}
242*e82e1a0cSThomas Zimmermann 
243*e82e1a0cSThomas Zimmermann 	ast_set_cursor_location(ast, x, y, x_offset, y_offset);
244*e82e1a0cSThomas Zimmermann 
245*e82e1a0cSThomas Zimmermann 	/* Dummy write to enable HWC and make the HW pick-up the changes. */
246*e82e1a0cSThomas Zimmermann 	ast_set_cursor_enabled(ast, true);
247*e82e1a0cSThomas Zimmermann }
248*e82e1a0cSThomas Zimmermann 
249*e82e1a0cSThomas Zimmermann static void ast_cursor_plane_helper_atomic_disable(struct drm_plane *plane,
250*e82e1a0cSThomas Zimmermann 						   struct drm_atomic_state *state)
251*e82e1a0cSThomas Zimmermann {
252*e82e1a0cSThomas Zimmermann 	struct ast_device *ast = to_ast_device(plane->dev);
253*e82e1a0cSThomas Zimmermann 
254*e82e1a0cSThomas Zimmermann 	ast_set_cursor_enabled(ast, false);
255*e82e1a0cSThomas Zimmermann }
256*e82e1a0cSThomas Zimmermann 
257*e82e1a0cSThomas Zimmermann static const struct drm_plane_helper_funcs ast_cursor_plane_helper_funcs = {
258*e82e1a0cSThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_HELPER_FUNCS,
259*e82e1a0cSThomas Zimmermann 	.atomic_check = ast_cursor_plane_helper_atomic_check,
260*e82e1a0cSThomas Zimmermann 	.atomic_update = ast_cursor_plane_helper_atomic_update,
261*e82e1a0cSThomas Zimmermann 	.atomic_disable = ast_cursor_plane_helper_atomic_disable,
262*e82e1a0cSThomas Zimmermann };
263*e82e1a0cSThomas Zimmermann 
264*e82e1a0cSThomas Zimmermann static const struct drm_plane_funcs ast_cursor_plane_funcs = {
265*e82e1a0cSThomas Zimmermann 	.update_plane = drm_atomic_helper_update_plane,
266*e82e1a0cSThomas Zimmermann 	.disable_plane = drm_atomic_helper_disable_plane,
267*e82e1a0cSThomas Zimmermann 	.destroy = drm_plane_cleanup,
268*e82e1a0cSThomas Zimmermann 	DRM_GEM_SHADOW_PLANE_FUNCS,
269*e82e1a0cSThomas Zimmermann };
270*e82e1a0cSThomas Zimmermann 
271*e82e1a0cSThomas Zimmermann int ast_cursor_plane_init(struct ast_device *ast)
272*e82e1a0cSThomas Zimmermann {
273*e82e1a0cSThomas Zimmermann 	struct drm_device *dev = &ast->base;
274*e82e1a0cSThomas Zimmermann 	struct ast_cursor_plane *ast_cursor_plane = &ast->cursor_plane;
275*e82e1a0cSThomas Zimmermann 	struct ast_plane *ast_plane = &ast_cursor_plane->base;
276*e82e1a0cSThomas Zimmermann 	struct drm_plane *cursor_plane = &ast_plane->base;
277*e82e1a0cSThomas Zimmermann 	size_t size;
278*e82e1a0cSThomas Zimmermann 	void __iomem *vaddr;
279*e82e1a0cSThomas Zimmermann 	u64 offset;
280*e82e1a0cSThomas Zimmermann 	int ret;
281*e82e1a0cSThomas Zimmermann 
282*e82e1a0cSThomas Zimmermann 	/*
283*e82e1a0cSThomas Zimmermann 	 * Allocate backing storage for cursors. The BOs are permanently
284*e82e1a0cSThomas Zimmermann 	 * pinned to the top end of the VRAM.
285*e82e1a0cSThomas Zimmermann 	 */
286*e82e1a0cSThomas Zimmermann 
287*e82e1a0cSThomas Zimmermann 	size = roundup(AST_HWC_SIZE + AST_HWC_SIGNATURE_SIZE, PAGE_SIZE);
288*e82e1a0cSThomas Zimmermann 
289*e82e1a0cSThomas Zimmermann 	if (ast->vram_fb_available < size)
290*e82e1a0cSThomas Zimmermann 		return -ENOMEM;
291*e82e1a0cSThomas Zimmermann 
292*e82e1a0cSThomas Zimmermann 	vaddr = ast->vram + ast->vram_fb_available - size;
293*e82e1a0cSThomas Zimmermann 	offset = ast->vram_fb_available - size;
294*e82e1a0cSThomas Zimmermann 
295*e82e1a0cSThomas Zimmermann 	ret = ast_plane_init(dev, ast_plane, vaddr, offset, size,
296*e82e1a0cSThomas Zimmermann 			     0x01, &ast_cursor_plane_funcs,
297*e82e1a0cSThomas Zimmermann 			     ast_cursor_plane_formats, ARRAY_SIZE(ast_cursor_plane_formats),
298*e82e1a0cSThomas Zimmermann 			     NULL, DRM_PLANE_TYPE_CURSOR);
299*e82e1a0cSThomas Zimmermann 	if (ret) {
300*e82e1a0cSThomas Zimmermann 		drm_err(dev, "ast_plane_init() failed: %d\n", ret);
301*e82e1a0cSThomas Zimmermann 		return ret;
302*e82e1a0cSThomas Zimmermann 	}
303*e82e1a0cSThomas Zimmermann 	drm_plane_helper_add(cursor_plane, &ast_cursor_plane_helper_funcs);
304*e82e1a0cSThomas Zimmermann 	drm_plane_enable_fb_damage_clips(cursor_plane);
305*e82e1a0cSThomas Zimmermann 
306*e82e1a0cSThomas Zimmermann 	ast->vram_fb_available -= size;
307*e82e1a0cSThomas Zimmermann 
308*e82e1a0cSThomas Zimmermann 	return 0;
309*e82e1a0cSThomas Zimmermann }
310