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 
33 bool
34 curs507a_space(struct nv50_wndw *wndw)
35 {
36 	nvif_msec(&nouveau_drm(wndw->plane.dev)->client.device, 100,
37 		if (NVIF_TV32(&wndw->wimm.base.user, NV507A, FREE, COUNT, >=, 4))
38 			return true;
39 	);
40 
41 	WARN_ON(1);
42 	return false;
43 }
44 
45 static int
46 curs507a_update(struct nv50_wndw *wndw, u32 *interlock)
47 {
48 	struct nvif_object *user = &wndw->wimm.base.user;
49 	int ret = nvif_chan_wait(&wndw->wimm, 1);
50 	if (ret == 0) {
51 		NVIF_WR32(user, NV507A, UPDATE,
52 			  NVDEF(NV507A, UPDATE, INTERLOCK_WITH_CORE, DISABLE));
53 	}
54 	return ret;
55 }
56 
57 static int
58 curs507a_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
59 {
60 	struct nvif_object *user = &wndw->wimm.base.user;
61 	int ret = nvif_chan_wait(&wndw->wimm, 1);
62 	if (ret == 0) {
63 		NVIF_WR32(user, NV507A, SET_CURSOR_HOT_SPOT_POINT_OUT,
64 			  NVVAL(NV507A, SET_CURSOR_HOT_SPOT_POINT_OUT, X, asyw->point.x) |
65 			  NVVAL(NV507A, SET_CURSOR_HOT_SPOT_POINT_OUT, Y, asyw->point.y));
66 	}
67 	return ret;
68 }
69 
70 const struct nv50_wimm_func
71 curs507a = {
72 	.point = curs507a_point,
73 	.update = curs507a_update,
74 };
75 
76 static void
77 curs507a_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
78 		 struct nv50_wndw_atom *asyw)
79 {
80 	u32 handle = nv50_disp(wndw->plane.dev)->core->chan.vram.handle;
81 	u32 offset = asyw->image.offset[0];
82 	if (asyh->curs.handle != handle || asyh->curs.offset != offset) {
83 		asyh->curs.handle = handle;
84 		asyh->curs.offset = offset;
85 		asyh->set.curs = asyh->curs.visible;
86 	}
87 }
88 
89 static void
90 curs507a_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
91 		 struct nv50_head_atom *asyh)
92 {
93 	asyh->curs.visible = false;
94 }
95 
96 static int
97 curs507a_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
98 		 struct nv50_head_atom *asyh)
99 {
100 	struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
101 	struct nv50_head *head = nv50_head(asyw->state.crtc);
102 	int ret;
103 
104 	ret = drm_atomic_helper_check_plane_state(&asyw->state, &asyh->state,
105 						  DRM_PLANE_NO_SCALING,
106 						  DRM_PLANE_NO_SCALING,
107 						  true, true);
108 	asyh->curs.visible = asyw->state.visible;
109 	if (ret || !asyh->curs.visible)
110 		return ret;
111 
112 	if (asyw->state.crtc_w != asyw->state.crtc_h) {
113 		NV_ATOMIC(drm, "Plane width/height must be equal for cursors\n");
114 		return -EINVAL;
115 	}
116 
117 	if (asyw->image.w != asyw->state.crtc_w) {
118 		NV_ATOMIC(drm, "Plane width must be equal to fb width for cursors (height can be larger though)\n");
119 		return -EINVAL;
120 	}
121 
122 	if (asyw->state.src_x || asyw->state.src_y) {
123 		NV_ATOMIC(drm, "Cursor planes do not support framebuffer offsets\n");
124 		return -EINVAL;
125 	}
126 
127 	ret = head->func->curs_layout(head, asyw, asyh);
128 	if (ret)
129 		return ret;
130 
131 	return head->func->curs_format(head, asyw, asyh);
132 }
133 
134 static const u32
135 curs507a_format[] = {
136 	DRM_FORMAT_ARGB8888,
137 	0
138 };
139 
140 static const struct nv50_wndw_func
141 curs507a_wndw = {
142 	.acquire = curs507a_acquire,
143 	.release = curs507a_release,
144 	.prepare = curs507a_prepare,
145 };
146 
147 int
148 curs507a_new_(const struct nv50_wimm_func *func, struct nouveau_drm *drm,
149 	      int head, s32 oclass, u32 interlock_data,
150 	      struct nv50_wndw **pwndw)
151 {
152 	struct nvif_disp_chan_v0 args = {
153 		.id = head,
154 	};
155 	struct nv50_disp *disp = nv50_disp(drm->dev);
156 	struct nv50_wndw *wndw;
157 	int ret;
158 
159 	ret = nv50_wndw_new_(&curs507a_wndw, drm->dev, DRM_PLANE_TYPE_CURSOR,
160 			     "curs", head, curs507a_format, BIT(head),
161 			     NV50_DISP_INTERLOCK_CURS, interlock_data, &wndw);
162 	if (*pwndw = wndw, ret)
163 		return ret;
164 
165 	ret = nvif_object_ctor(&disp->disp->object, "kmsCurs", 0, oclass,
166 			       &args, sizeof(args), &wndw->wimm.base.user);
167 	if (ret) {
168 		NV_ERROR(drm, "curs%04x allocation failed: %d\n", oclass, ret);
169 		return ret;
170 	}
171 
172 	nvif_object_map(&wndw->wimm.base.user, NULL, 0);
173 	wndw->immd = func;
174 	wndw->ctxdma.parent = NULL;
175 	return 0;
176 }
177 
178 int
179 curs507a_new(struct nouveau_drm *drm, int head, s32 oclass,
180 	     struct nv50_wndw **pwndw)
181 {
182 	return curs507a_new_(&curs507a, drm, head, oclass,
183 			     0x00000001 << (head * 8), pwndw);
184 }
185