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/cl507a.h>
27 #include <nvif/timer.h>
28 
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_plane_helper.h>
31 
32 bool
33 curs507a_space(struct nv50_wndw *wndw)
34 {
35 	nvif_msec(&nouveau_drm(wndw->plane.dev)->client.device, 2,
36 		if (nvif_rd32(&wndw->wimm.base.user, 0x0008) >= 4)
37 			return true;
38 	);
39 	WARN_ON(1);
40 	return false;
41 }
42 
43 static void
44 curs507a_update(struct nv50_wndw *wndw, u32 *interlock)
45 {
46 	if (curs507a_space(wndw))
47 		nvif_wr32(&wndw->wimm.base.user, 0x0080, 0x00000000);
48 }
49 
50 static void
51 curs507a_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
52 {
53 	if (curs507a_space(wndw)) {
54 		nvif_wr32(&wndw->wimm.base.user, 0x0084, asyw->point.y << 16 |
55 							 asyw->point.x);
56 	}
57 }
58 
59 const struct nv50_wimm_func
60 curs507a = {
61 	.point = curs507a_point,
62 	.update = curs507a_update,
63 };
64 
65 static void
66 curs507a_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
67 		 struct nv50_wndw_atom *asyw)
68 {
69 	u32 handle = nv50_disp(wndw->plane.dev)->core->chan.vram.handle;
70 	u32 offset = asyw->image.offset[0];
71 	if (asyh->curs.handle != handle || asyh->curs.offset != offset) {
72 		asyh->curs.handle = handle;
73 		asyh->curs.offset = offset;
74 		asyh->set.curs = asyh->curs.visible;
75 	}
76 }
77 
78 static void
79 curs507a_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
80 		 struct nv50_head_atom *asyh)
81 {
82 	asyh->curs.visible = false;
83 }
84 
85 static int
86 curs507a_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
87 		 struct nv50_head_atom *asyh)
88 {
89 	struct nv50_head *head = nv50_head(asyw->state.crtc);
90 	int ret;
91 
92 	ret = drm_atomic_helper_check_plane_state(&asyw->state, &asyh->state,
93 						  DRM_PLANE_HELPER_NO_SCALING,
94 						  DRM_PLANE_HELPER_NO_SCALING,
95 						  true, true);
96 	asyh->curs.visible = asyw->state.visible;
97 	if (ret || !asyh->curs.visible)
98 		return ret;
99 
100 	if (asyw->image.w != asyw->image.h)
101 		return -EINVAL;
102 
103 	ret = head->func->curs_layout(head, asyw, asyh);
104 	if (ret)
105 		return ret;
106 
107 	return head->func->curs_format(head, asyw, asyh);
108 }
109 
110 static const u32
111 curs507a_format[] = {
112 	DRM_FORMAT_ARGB8888,
113 	0
114 };
115 
116 static const struct nv50_wndw_func
117 curs507a_wndw = {
118 	.acquire = curs507a_acquire,
119 	.release = curs507a_release,
120 	.prepare = curs507a_prepare,
121 };
122 
123 int
124 curs507a_new_(const struct nv50_wimm_func *func, struct nouveau_drm *drm,
125 	      int head, s32 oclass, u32 interlock_data,
126 	      struct nv50_wndw **pwndw)
127 {
128 	struct nv50_disp_cursor_v0 args = {
129 		.head = head,
130 	};
131 	struct nv50_disp *disp = nv50_disp(drm->dev);
132 	struct nv50_wndw *wndw;
133 	int ret;
134 
135 	ret = nv50_wndw_new_(&curs507a_wndw, drm->dev, DRM_PLANE_TYPE_CURSOR,
136 			     "curs", head, curs507a_format, BIT(head),
137 			     NV50_DISP_INTERLOCK_CURS, interlock_data, &wndw);
138 	if (*pwndw = wndw, ret)
139 		return ret;
140 
141 	ret = nvif_object_init(&disp->disp->object, 0, oclass, &args,
142 			       sizeof(args), &wndw->wimm.base.user);
143 	if (ret) {
144 		NV_ERROR(drm, "curs%04x allocation failed: %d\n", oclass, ret);
145 		return ret;
146 	}
147 
148 	nvif_object_map(&wndw->wimm.base.user, NULL, 0);
149 	wndw->immd = func;
150 	wndw->ctxdma.parent = NULL;
151 	return 0;
152 }
153 
154 int
155 curs507a_new(struct nouveau_drm *drm, int head, s32 oclass,
156 	     struct nv50_wndw **pwndw)
157 {
158 	return curs507a_new_(&curs507a, drm, head, oclass,
159 			     0x00000001 << (head * 8), pwndw);
160 }
161