xref: /openbmc/linux/drivers/gpu/drm/nouveau/dispnv50/curs507a.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11590700dSBen Skeggs /*
21590700dSBen Skeggs  * Copyright 2018 Red Hat Inc.
31590700dSBen Skeggs  *
41590700dSBen Skeggs  * Permission is hereby granted, free of charge, to any person obtaining a
51590700dSBen Skeggs  * copy of this software and associated documentation files (the "Software"),
61590700dSBen Skeggs  * to deal in the Software without restriction, including without limitation
71590700dSBen Skeggs  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
81590700dSBen Skeggs  * and/or sell copies of the Software, and to permit persons to whom the
91590700dSBen Skeggs  * Software is furnished to do so, subject to the following conditions:
101590700dSBen Skeggs  *
111590700dSBen Skeggs  * The above copyright notice and this permission notice shall be included in
121590700dSBen Skeggs  * all copies or substantial portions of the Software.
131590700dSBen Skeggs  *
141590700dSBen Skeggs  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151590700dSBen Skeggs  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161590700dSBen Skeggs  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
171590700dSBen Skeggs  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
181590700dSBen Skeggs  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
191590700dSBen Skeggs  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
201590700dSBen Skeggs  * OTHER DEALINGS IN THE SOFTWARE.
211590700dSBen Skeggs  */
221590700dSBen Skeggs #include "curs.h"
231590700dSBen Skeggs #include "core.h"
2401d380abSBen Skeggs #include "head.h"
251590700dSBen Skeggs 
26889fcbe9SBen Skeggs #include <nvif/if0014.h>
27374b5580SBen Skeggs #include <nvif/timer.h>
281590700dSBen Skeggs 
292a0d40bbSBen Skeggs #include <nvhw/class/cl507a.h>
302a0d40bbSBen Skeggs 
311590700dSBen Skeggs #include <drm/drm_atomic_helper.h>
32*815d091fSSimon Ser #include <drm/drm_fourcc.h>
331590700dSBen Skeggs 
34374b5580SBen Skeggs bool
curs507a_space(struct nv50_wndw * wndw)35374b5580SBen Skeggs curs507a_space(struct nv50_wndw *wndw)
36374b5580SBen Skeggs {
3718d8cf93SBen Skeggs 	nvif_msec(&nouveau_drm(wndw->plane.dev)->client.device, 100,
382a0d40bbSBen Skeggs 		if (NVIF_TV32(&wndw->wimm.base.user, NV507A, FREE, COUNT, >=, 4))
39374b5580SBen Skeggs 			return true;
40374b5580SBen Skeggs 	);
412a0d40bbSBen Skeggs 
42374b5580SBen Skeggs 	WARN_ON(1);
43374b5580SBen Skeggs 	return false;
44374b5580SBen Skeggs }
45374b5580SBen Skeggs 
469659be21SBen Skeggs static int
curs507a_update(struct nv50_wndw * wndw,u32 * interlock)4753e0a3e7SBen Skeggs curs507a_update(struct nv50_wndw *wndw, u32 *interlock)
481590700dSBen Skeggs {
49852dfbdeSBen Skeggs 	struct nvif_object *user = &wndw->wimm.base.user;
509659be21SBen Skeggs 	int ret = nvif_chan_wait(&wndw->wimm, 1);
51852dfbdeSBen Skeggs 	if (ret == 0) {
52852dfbdeSBen Skeggs 		NVIF_WR32(user, NV507A, UPDATE,
53852dfbdeSBen Skeggs 			  NVDEF(NV507A, UPDATE, INTERLOCK_WITH_CORE, DISABLE));
54852dfbdeSBen Skeggs 	}
559659be21SBen Skeggs 	return ret;
561590700dSBen Skeggs }
571590700dSBen Skeggs 
58bea8395cSBen Skeggs static int
curs507a_point(struct nv50_wndw * wndw,struct nv50_wndw_atom * asyw)591590700dSBen Skeggs curs507a_point(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw)
601590700dSBen Skeggs {
6193701408SBen Skeggs 	struct nvif_object *user = &wndw->wimm.base.user;
62bea8395cSBen Skeggs 	int ret = nvif_chan_wait(&wndw->wimm, 1);
63bea8395cSBen Skeggs 	if (ret == 0) {
6493701408SBen Skeggs 		NVIF_WR32(user, NV507A, SET_CURSOR_HOT_SPOT_POINT_OUT,
6593701408SBen Skeggs 			  NVVAL(NV507A, SET_CURSOR_HOT_SPOT_POINT_OUT, X, asyw->point.x) |
6693701408SBen Skeggs 			  NVVAL(NV507A, SET_CURSOR_HOT_SPOT_POINT_OUT, Y, asyw->point.y));
671590700dSBen Skeggs 	}
68bea8395cSBen Skeggs 	return ret;
69374b5580SBen Skeggs }
701590700dSBen Skeggs 
7153e0a3e7SBen Skeggs const struct nv50_wimm_func
721590700dSBen Skeggs curs507a = {
731590700dSBen Skeggs 	.point = curs507a_point,
741590700dSBen Skeggs 	.update = curs507a_update,
751590700dSBen Skeggs };
761590700dSBen Skeggs 
771590700dSBen Skeggs static void
curs507a_prepare(struct nv50_wndw * wndw,struct nv50_head_atom * asyh,struct nv50_wndw_atom * asyw)781590700dSBen Skeggs curs507a_prepare(struct nv50_wndw *wndw, struct nv50_head_atom *asyh,
791590700dSBen Skeggs 		 struct nv50_wndw_atom *asyw)
801590700dSBen Skeggs {
811590700dSBen Skeggs 	u32 handle = nv50_disp(wndw->plane.dev)->core->chan.vram.handle;
82261fcfa9SBen Skeggs 	u32 offset = asyw->image.offset[0];
831590700dSBen Skeggs 	if (asyh->curs.handle != handle || asyh->curs.offset != offset) {
841590700dSBen Skeggs 		asyh->curs.handle = handle;
851590700dSBen Skeggs 		asyh->curs.offset = offset;
861590700dSBen Skeggs 		asyh->set.curs = asyh->curs.visible;
871590700dSBen Skeggs 	}
881590700dSBen Skeggs }
891590700dSBen Skeggs 
901590700dSBen Skeggs static void
curs507a_release(struct nv50_wndw * wndw,struct nv50_wndw_atom * asyw,struct nv50_head_atom * asyh)911590700dSBen Skeggs curs507a_release(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
921590700dSBen Skeggs 		 struct nv50_head_atom *asyh)
931590700dSBen Skeggs {
941590700dSBen Skeggs 	asyh->curs.visible = false;
951590700dSBen Skeggs }
961590700dSBen Skeggs 
971590700dSBen Skeggs static int
curs507a_acquire(struct nv50_wndw * wndw,struct nv50_wndw_atom * asyw,struct nv50_head_atom * asyh)981590700dSBen Skeggs curs507a_acquire(struct nv50_wndw *wndw, struct nv50_wndw_atom *asyw,
991590700dSBen Skeggs 		 struct nv50_head_atom *asyh)
1001590700dSBen Skeggs {
101c0a808b0SLyude Paul 	struct nouveau_drm *drm = nouveau_drm(wndw->plane.dev);
10201d380abSBen Skeggs 	struct nv50_head *head = nv50_head(asyw->state.crtc);
103*815d091fSSimon Ser 	struct drm_framebuffer *fb = asyw->state.fb;
1041590700dSBen Skeggs 	int ret;
1051590700dSBen Skeggs 
1061590700dSBen Skeggs 	ret = drm_atomic_helper_check_plane_state(&asyw->state, &asyh->state,
107cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
108cce32e4eSThomas Zimmermann 						  DRM_PLANE_NO_SCALING,
1091590700dSBen Skeggs 						  true, true);
1101590700dSBen Skeggs 	asyh->curs.visible = asyw->state.visible;
1111590700dSBen Skeggs 	if (ret || !asyh->curs.visible)
1121590700dSBen Skeggs 		return ret;
1131590700dSBen Skeggs 
114c0a808b0SLyude Paul 	if (asyw->state.crtc_w != asyw->state.crtc_h) {
115c0a808b0SLyude Paul 		NV_ATOMIC(drm, "Plane width/height must be equal for cursors\n");
1161590700dSBen Skeggs 		return -EINVAL;
117c0a808b0SLyude Paul 	}
118c0a808b0SLyude Paul 
119c0a808b0SLyude Paul 	if (asyw->image.w != asyw->state.crtc_w) {
120c0a808b0SLyude Paul 		NV_ATOMIC(drm, "Plane width must be equal to fb width for cursors (height can be larger though)\n");
121c0a808b0SLyude Paul 		return -EINVAL;
122c0a808b0SLyude Paul 	}
123c0a808b0SLyude Paul 
124c0a808b0SLyude Paul 	if (asyw->state.src_x || asyw->state.src_y) {
125c0a808b0SLyude Paul 		NV_ATOMIC(drm, "Cursor planes do not support framebuffer offsets\n");
126c0a808b0SLyude Paul 		return -EINVAL;
127c0a808b0SLyude Paul 	}
1281590700dSBen Skeggs 
129*815d091fSSimon Ser 	if (asyw->image.pitch[0] != asyw->image.w * fb->format->cpp[0]) {
130*815d091fSSimon Ser 		NV_ATOMIC(drm,
131*815d091fSSimon Ser 			  "%s: invalid cursor image pitch: image must be packed (pitch = %d, width = %d)\n",
132*815d091fSSimon Ser 			  wndw->plane.name, asyw->image.pitch[0], asyw->image.w);
133*815d091fSSimon Ser 		return -EINVAL;
134*815d091fSSimon Ser 	}
1351590700dSBen Skeggs 
136*815d091fSSimon Ser 	ret = head->func->curs_layout(head, asyw, asyh);
137*815d091fSSimon Ser 	if (ret) {
138*815d091fSSimon Ser 		NV_ATOMIC(drm,
139*815d091fSSimon Ser 			  "%s: invalid cursor image size: unsupported size %dx%d\n",
140*815d091fSSimon Ser 			  wndw->plane.name, asyw->image.w, asyw->image.h);
141*815d091fSSimon Ser 		return ret;
142*815d091fSSimon Ser 	}
143*815d091fSSimon Ser 
144*815d091fSSimon Ser 	ret = head->func->curs_format(head, asyw, asyh);
145*815d091fSSimon Ser 	if (ret) {
146*815d091fSSimon Ser 		NV_ATOMIC(drm,
147*815d091fSSimon Ser 			  "%s: invalid cursor image format 0x%X\n",
148*815d091fSSimon Ser 			  wndw->plane.name, fb->format->format);
149*815d091fSSimon Ser 		return ret;
150*815d091fSSimon Ser 	}
151*815d091fSSimon Ser 
152*815d091fSSimon Ser 	return 0;
1531590700dSBen Skeggs }
1541590700dSBen Skeggs 
1551590700dSBen Skeggs static const u32
1561590700dSBen Skeggs curs507a_format[] = {
1571590700dSBen Skeggs 	DRM_FORMAT_ARGB8888,
1581590700dSBen Skeggs 	0
1591590700dSBen Skeggs };
1601590700dSBen Skeggs 
1611590700dSBen Skeggs static const struct nv50_wndw_func
1621590700dSBen Skeggs curs507a_wndw = {
1631590700dSBen Skeggs 	.acquire = curs507a_acquire,
1641590700dSBen Skeggs 	.release = curs507a_release,
1651590700dSBen Skeggs 	.prepare = curs507a_prepare,
1661590700dSBen Skeggs };
1671590700dSBen Skeggs 
16853e0a3e7SBen Skeggs int
curs507a_new_(const struct nv50_wimm_func * func,struct nouveau_drm * drm,int head,s32 oclass,u32 interlock_data,struct nv50_wndw ** pwndw)1691590700dSBen Skeggs curs507a_new_(const struct nv50_wimm_func *func, struct nouveau_drm *drm,
17053e0a3e7SBen Skeggs 	      int head, s32 oclass, u32 interlock_data,
17153e0a3e7SBen Skeggs 	      struct nv50_wndw **pwndw)
1721590700dSBen Skeggs {
173889fcbe9SBen Skeggs 	struct nvif_disp_chan_v0 args = {
174889fcbe9SBen Skeggs 		.id = head,
1751590700dSBen Skeggs 	};
1761590700dSBen Skeggs 	struct nv50_disp *disp = nv50_disp(drm->dev);
1771590700dSBen Skeggs 	struct nv50_wndw *wndw;
1781590700dSBen Skeggs 	int ret;
1791590700dSBen Skeggs 
1801590700dSBen Skeggs 	ret = nv50_wndw_new_(&curs507a_wndw, drm->dev, DRM_PLANE_TYPE_CURSOR,
18153e0a3e7SBen Skeggs 			     "curs", head, curs507a_format, BIT(head),
18253e0a3e7SBen Skeggs 			     NV50_DISP_INTERLOCK_CURS, interlock_data, &wndw);
1831590700dSBen Skeggs 	if (*pwndw = wndw, ret)
1841590700dSBen Skeggs 		return ret;
1851590700dSBen Skeggs 
1869ac596a4SBen Skeggs 	ret = nvif_object_ctor(&disp->disp->object, "kmsCurs", 0, oclass,
1879ac596a4SBen Skeggs 			       &args, sizeof(args), &wndw->wimm.base.user);
1881590700dSBen Skeggs 	if (ret) {
1891590700dSBen Skeggs 		NV_ERROR(drm, "curs%04x allocation failed: %d\n", oclass, ret);
1901590700dSBen Skeggs 		return ret;
1911590700dSBen Skeggs 	}
1921590700dSBen Skeggs 
1931590700dSBen Skeggs 	nvif_object_map(&wndw->wimm.base.user, NULL, 0);
1941590700dSBen Skeggs 	wndw->immd = func;
1951264f832SBen Skeggs 	wndw->ctxdma.parent = NULL;
1961590700dSBen Skeggs 	return 0;
1971590700dSBen Skeggs }
1981590700dSBen Skeggs 
1991590700dSBen Skeggs int
curs507a_new(struct nouveau_drm * drm,int head,s32 oclass,struct nv50_wndw ** pwndw)2001590700dSBen Skeggs curs507a_new(struct nouveau_drm *drm, int head, s32 oclass,
2011590700dSBen Skeggs 	     struct nv50_wndw **pwndw)
2021590700dSBen Skeggs {
20353e0a3e7SBen Skeggs 	return curs507a_new_(&curs507a, drm, head, oclass,
20453e0a3e7SBen Skeggs 			     0x00000001 << (head * 8), pwndw);
2051590700dSBen Skeggs }
206