1/*	$NetBSD: nouveau_nvkm_engine_disp_head.c,v 1.2 2021/12/18 23:45:35 riastradh Exp $	*/
2
3/*
4 * Copyright 2017 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 * Authors: Ben Skeggs <bskeggs@redhat.com>
25 */
26#include <sys/cdefs.h>
27__KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_engine_disp_head.c,v 1.2 2021/12/18 23:45:35 riastradh Exp $");
28
29#include "head.h"
30
31#include <core/client.h>
32
33#include <nvif/cl0046.h>
34#include <nvif/unpack.h>
35
36struct nvkm_head *
37nvkm_head_find(struct nvkm_disp *disp, int id)
38{
39	struct nvkm_head *head;
40	list_for_each_entry(head, &disp->head, head) {
41		if (head->id == id)
42			return head;
43	}
44	return NULL;
45}
46
47int
48nvkm_head_mthd_scanoutpos(struct nvkm_object *object,
49			  struct nvkm_head *head, void *data, u32 size)
50{
51	union {
52		struct nv04_disp_scanoutpos_v0 v0;
53	} *args = data;
54	int ret = -ENOSYS;
55
56	nvif_ioctl(object, "head scanoutpos size %d\n", size);
57	if (!(ret = nvif_unpack(ret, &data, &size, args->v0, 0, 0, false))) {
58		nvif_ioctl(object, "head scanoutpos vers %d\n",
59			   args->v0.version);
60
61		head->func->state(head, &head->arm);
62		args->v0.vtotal  = head->arm.vtotal;
63		args->v0.vblanks = head->arm.vblanks;
64		args->v0.vblanke = head->arm.vblanke;
65		args->v0.htotal  = head->arm.htotal;
66		args->v0.hblanks = head->arm.hblanks;
67		args->v0.hblanke = head->arm.hblanke;
68
69		/* We don't support reading htotal/vtotal on pre-NV50 VGA,
70		 * so we have to give up and trigger the timestamping
71		 * fallback in the drm core.
72		 */
73		if (!args->v0.vtotal || !args->v0.htotal)
74			return -ENOTSUPP;
75
76		args->v0.time[0] = ktime_to_ns(ktime_get());
77		head->func->rgpos(head, &args->v0.hline, &args->v0.vline);
78		args->v0.time[1] = ktime_to_ns(ktime_get());
79	} else
80		return ret;
81
82	return 0;
83}
84
85void
86nvkm_head_del(struct nvkm_head **phead)
87{
88	struct nvkm_head *head = *phead;
89	if (head) {
90		HEAD_DBG(head, "dtor");
91		list_del(&head->head);
92		kfree(*phead);
93		*phead = NULL;
94	}
95}
96
97int
98nvkm_head_new_(const struct nvkm_head_func *func,
99	       struct nvkm_disp *disp, int id)
100{
101	struct nvkm_head *head;
102	if (!(head = kzalloc(sizeof(*head), GFP_KERNEL)))
103		return -ENOMEM;
104	head->func = func;
105	head->disp = disp;
106	head->id = id;
107	list_add_tail(&head->head, &disp->head);
108	HEAD_DBG(head, "ctor");
109	return 0;
110}
111