1/*	$NetBSD: nouveau_dispnv50_curs507a.c,v 1.2 2021/12/18 23:45:32 riastradh Exp $	*/
2
3/*
4 * Copyright 2018 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24#include <sys/cdefs.h>
25__KERNEL_RCSID(0, "$NetBSD: nouveau_dispnv50_curs507a.c,v 1.2 2021/12/18 23:45:32 riastradh Exp $");
26
27#include "curs.h"
28#include "core.h"
29#include "head.h"
30
31#include <nvif/cl507a.h>
32
33#include <drm/drm_atomic_helper.h>
34#include <drm/drm_plane_helper.h>
35
36static void
37curs507a_update(struct nv50_wndw *wndw, u32 *interlock)
38{
39	nvif_wr32(&wndw->wimm.base.user, 0x0080, 0x00000000);
40}
41
42static void
43curs507a_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
44{
45	nvif_wr32(&wndw->wimm.base.user, 0x0084, asyw->point.y << 16 |
46						 asyw->point.x);
47}
48
49const struct nv50_wimm_func
50curs507a = {
51	.point = curs507a_point,
52	.update = curs507a_update,
53};
54
55static void
56curs507a_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
57		 struct nv50_wndw_atom *asyw)
58{
59	u32 handle = nv50_disp(wndw->plane.dev)->core->chan.vram.handle;
60	u32 offset = asyw->image.offset[0];
61	if (asyh->curs.handle != handle || asyh->curs.offset != offset) {
62		asyh->curs.handle = handle;
63		asyh->curs.offset = offset;
64		asyh->set.curs = asyh->curs.visible;
65	}
66}
67
68static void
69curs507a_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
70		 struct nv50_head_atom *asyh)
71{
72	asyh->curs.visible = false;
73}
74
75static int
76curs507a_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
77		 struct nv50_head_atom *asyh)
78{
79	struct nv50_head *head = nv50_head(asyw->state.crtc);
80	int ret;
81
82	ret = drm_atomic_helper_check_plane_state(&asyw->state, &asyh->state,
83						  DRM_PLANE_HELPER_NO_SCALING,
84						  DRM_PLANE_HELPER_NO_SCALING,
85						  true, true);
86	asyh->curs.visible = asyw->state.visible;
87	if (ret || !asyh->curs.visible)
88		return ret;
89
90	if (asyw->image.w != asyw->image.h)
91		return -EINVAL;
92
93	ret = head->func->curs_layout(head, asyw, asyh);
94	if (ret)
95		return ret;
96
97	return head->func->curs_format(head, asyw, asyh);
98}
99
100static const u32
101curs507a_format[] = {
102	DRM_FORMAT_ARGB8888,
103	0
104};
105
106static const struct nv50_wndw_func
107curs507a_wndw = {
108	.acquire = curs507a_acquire,
109	.release = curs507a_release,
110	.prepare = curs507a_prepare,
111};
112
113int
114curs507a_new_(const struct nv50_wimm_func *func, struct nouveau_drm *drm,
115	      int head, s32 oclass, u32 interlock_data,
116	      struct nv50_wndw **pwndw)
117{
118	struct nv50_disp_cursor_v0 args = {
119		.head = head,
120	};
121	struct nv50_disp *disp = nv50_disp(drm->dev);
122	struct nv50_wndw *wndw;
123	int ret;
124
125	ret = nv50_wndw_new_(&curs507a_wndw, drm->dev, DRM_PLANE_TYPE_CURSOR,
126			     "curs", head, curs507a_format, BIT(head),
127			     NV50_DISP_INTERLOCK_CURS, interlock_data, &wndw);
128	if (*pwndw = wndw, ret)
129		return ret;
130
131	ret = nvif_object_init(&disp->disp->object, 0, oclass, &args,
132			       sizeof(args), &wndw->wimm.base.user);
133	if (ret) {
134		NV_ERROR(drm, "curs%04x allocation failed: %d\n", oclass, ret);
135		return ret;
136	}
137
138	nvif_object_map(&wndw->wimm.base.user, NULL, 0);
139	wndw->immd = func;
140	wndw->ctxdma.parent = NULL;
141	return 0;
142}
143
144int
145curs507a_new(struct nouveau_drm *drm, int head, s32 oclass,
146	     struct nv50_wndw **pwndw)
147{
148	return curs507a_new_(&curs507a, drm, head, oclass,
149			     0x00000001 << (head * 8), pwndw);
150}
151