xref: /linux/drivers/gpu/drm/nouveau/dispnv50/curs507a.c (revision 39d3389331abd712461f50249722f7ed9d815068)
1 /*
2  * Copyright 2018 Red Hat Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  */
22 #include "curs.h"
23 #include "core.h"
24 #include "head.h"
25 
26 #include <nvif/if0014.h>
27 #include <nvif/timer.h>
28 
29 #include <nvhw/class/cl507a.h>
30 
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_fourcc.h>
33 
34 bool
curs507a_space(struct nv50_wndw * wndw)35 curs507a_space(struct nv50_wndw *wndw)
36 {
37 	nvif_msec(&nouveau_drm(wndw->plane.dev)->client.device, 100,
38 		if (NVIF_TV32(&wndw->wimm.base.user, NV507A, FREE, COUNT, >=, 4))
39 			return true;
40 	);
41 
42 	WARN_ON(1);
43 	return false;
44 }
45 
46 static int
curs507a_update(struct nv50_wndw * wndw,u32 * interlock)47 curs507a_update(struct nv50_wndw *wndw, u32 *interlock)
48 {
49 	struct nvif_object *user = &wndw->wimm.base.user;
50 	int ret = nvif_chan_wait(&wndw->wimm, 1);
51 	if (ret == 0) {
52 		NVIF_WR32(user, NV507A, UPDATE,
53 			  NVDEF(NV507A, UPDATE, INTERLOCK_WITH_CORE, DISABLE));
54 	}
55 	return ret;
56 }
57 
58 static int
curs507a_point(struct nv50_wndw * wndw,struct nv50_wndw_atom * asyw)59 curs507a_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
60 {
61 	struct nvif_object *user = &wndw->wimm.base.user;
62 	int ret = nvif_chan_wait(&wndw->wimm, 1);
63 	if (ret == 0) {
64 		NVIF_WR32(user, NV507A, SET_CURSOR_HOT_SPOT_POINT_OUT,
65 			  NVVAL(NV507A, SET_CURSOR_HOT_SPOT_POINT_OUT, X, asyw->point.x) |
66 			  NVVAL(NV507A, SET_CURSOR_HOT_SPOT_POINT_OUT, Y, asyw->point.y));
67 	}
68 	return ret;
69 }
70 
71 const struct nv50_wimm_func
72 curs507a = {
73 	.point = curs507a_point,
74 	.update = curs507a_update,
75 };
76 
77 static void
curs507a_prepare(struct nv50_wndw * wndw,struct nv50_head_atom * asyh,struct nv50_wndw_atom * asyw)78 curs507a_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
79 		 struct nv50_wndw_atom *asyw)
80 {
81 	u32 handle = nv50_disp(wndw->plane.dev)->core->chan.vram.handle;
82 	u32 offset = asyw->image.offset[0];
83 	if (asyh->curs.handle != handle || asyh->curs.offset != offset) {
84 		asyh->curs.handle = handle;
85 		asyh->curs.offset = offset;
86 		asyh->set.curs = asyh->curs.visible;
87 		nv50_atom(asyh->state.state)->lock_core = true;
88 	}
89 }
90 
91 static void
curs507a_release(struct nv50_wndw * wndw,struct nv50_wndw_atom * asyw,struct nv50_head_atom * asyh)92 curs507a_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
93 		 struct nv50_head_atom *asyh)
94 {
95 	asyh->curs.visible = false;
96 }
97 
98 static int
curs507a_acquire(struct nv50_wndw * wndw,struct nv50_wndw_atom * asyw,struct nv50_head_atom * asyh)99 curs507a_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
100 		 struct nv50_head_atom *asyh)
101 {
102 	struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
103 	struct nv50_head *head = nv50_head(asyw->state.crtc);
104 	struct drm_framebuffer *fb = asyw->state.fb;
105 	int ret;
106 
107 	ret = drm_atomic_helper_check_plane_state(&asyw->state, &asyh->state,
108 						  DRM_PLANE_NO_SCALING,
109 						  DRM_PLANE_NO_SCALING,
110 						  true, true);
111 	asyh->curs.visible = asyw->state.visible;
112 	if (ret || !asyh->curs.visible)
113 		return ret;
114 
115 	if (asyw->state.crtc_w != asyw->state.crtc_h) {
116 		NV_ATOMIC(drm, "Plane width/height must be equal for cursors\n");
117 		return -EINVAL;
118 	}
119 
120 	if (asyw->image.w != asyw->state.crtc_w) {
121 		NV_ATOMIC(drm, "Plane width must be equal to fb width for cursors (height can be larger though)\n");
122 		return -EINVAL;
123 	}
124 
125 	if (asyw->state.src_x || asyw->state.src_y) {
126 		NV_ATOMIC(drm, "Cursor planes do not support framebuffer offsets\n");
127 		return -EINVAL;
128 	}
129 
130 	if (asyw->image.pitch[0] != asyw->image.w * fb->format->cpp[0]) {
131 		NV_ATOMIC(drm,
132 			  "%s: invalid cursor image pitch: image must be packed (pitch = %d, width = %d)\n",
133 			  wndw->plane.name, asyw->image.pitch[0], asyw->image.w);
134 		return -EINVAL;
135 	}
136 
137 	ret = head->func->curs_layout(head, asyw, asyh);
138 	if (ret) {
139 		NV_ATOMIC(drm,
140 			  "%s: invalid cursor image size: unsupported size %dx%d\n",
141 			  wndw->plane.name, asyw->image.w, asyw->image.h);
142 		return ret;
143 	}
144 
145 	ret = head->func->curs_format(head, asyw, asyh);
146 	if (ret) {
147 		NV_ATOMIC(drm,
148 			  "%s: invalid cursor image format 0x%X\n",
149 			  wndw->plane.name, fb->format->format);
150 		return ret;
151 	}
152 
153 	return 0;
154 }
155 
156 static const u32
157 curs507a_format[] = {
158 	DRM_FORMAT_ARGB8888,
159 	0
160 };
161 
162 static const struct nv50_wndw_func
163 curs507a_wndw = {
164 	.acquire = curs507a_acquire,
165 	.release = curs507a_release,
166 	.prepare = curs507a_prepare,
167 };
168 
169 int
curs507a_new_(const struct nv50_wimm_func * func,struct nouveau_drm * drm,int head,s32 oclass,u32 interlock_data,struct nv50_wndw ** pwndw)170 curs507a_new_(const struct nv50_wimm_func *func, struct nouveau_drm *drm,
171 	      int head, s32 oclass, u32 interlock_data,
172 	      struct nv50_wndw **pwndw)
173 {
174 	struct nvif_disp_chan_v0 args = {
175 		.id = head,
176 	};
177 	struct nv50_disp *disp = nv50_disp(drm->dev);
178 	struct nv50_wndw *wndw;
179 	int ret;
180 
181 	ret = nv50_wndw_new_(&curs507a_wndw, drm->dev, DRM_PLANE_TYPE_CURSOR,
182 			     "curs", head, curs507a_format, BIT(head),
183 			     NV50_DISP_INTERLOCK_CURS, interlock_data, &wndw);
184 	if (*pwndw = wndw, ret)
185 		return ret;
186 
187 	ret = nvif_object_ctor(&disp->disp->object, "kmsCurs", 0, oclass,
188 			       &args, sizeof(args), &wndw->wimm.base.user);
189 	if (ret) {
190 		NV_ERROR(drm, "curs%04x allocation failed: %d\n", oclass, ret);
191 		return ret;
192 	}
193 
194 	nvif_object_map(&wndw->wimm.base.user, NULL, 0);
195 	wndw->immd = func;
196 	wndw->ctxdma.parent = NULL;
197 	return 0;
198 }
199 
200 int
curs507a_new(struct nouveau_drm * drm,int head,s32 oclass,struct nv50_wndw ** pwndw)201 curs507a_new(struct nouveau_drm *drm, int head, s32 oclass,
202 	     struct nv50_wndw **pwndw)
203 {
204 	return curs507a_new_(&curs507a, drm, head, oclass,
205 			     0x00000001 << (head * 8), pwndw);
206 }
207