1 /* $NetBSD: nouveau_nvif_fifo.c,v 1.2 2021/12/18 23:45:33 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_nvif_fifo.c,v 1.2 2021/12/18 23:45:33 riastradh Exp $");
26
27 #include <nvif/fifo.h>
28
29 static int
nvif_fifo_runlists(struct nvif_device * device)30 nvif_fifo_runlists(struct nvif_device *device)
31 {
32 struct nvif_object *object = &device->object;
33 struct {
34 struct nv_device_info_v1 m;
35 struct {
36 struct nv_device_info_v1_data runlists;
37 struct nv_device_info_v1_data runlist[64];
38 } v;
39 } *a;
40 int ret, i;
41
42 if (device->runlist)
43 return 0;
44
45 if (!(a = kmalloc(sizeof(*a), GFP_KERNEL)))
46 return -ENOMEM;
47 a->m.version = 1;
48 a->m.count = sizeof(a->v) / sizeof(a->v.runlists);
49 a->v.runlists.mthd = NV_DEVICE_FIFO_RUNLISTS;
50 for (i = 0; i < ARRAY_SIZE(a->v.runlist); i++)
51 a->v.runlist[i].mthd = NV_DEVICE_FIFO_RUNLIST_ENGINES(i);
52
53 ret = nvif_object_mthd(object, NV_DEVICE_V0_INFO, a, sizeof(*a));
54 if (ret)
55 goto done;
56
57 device->runlists = fls64(a->v.runlists.data);
58 device->runlist = kcalloc(device->runlists, sizeof(*device->runlist),
59 GFP_KERNEL);
60 if (!device->runlist) {
61 ret = -ENOMEM;
62 goto done;
63 }
64
65 for (i = 0; i < device->runlists; i++) {
66 if (a->v.runlists.data & BIT_ULL(i))
67 device->runlist[i].engines = a->v.runlist[i].data;
68 }
69
70 done:
71 kfree(a);
72 return ret;
73 }
74
75 u64
nvif_fifo_runlist(struct nvif_device * device,u64 engine)76 nvif_fifo_runlist(struct nvif_device *device, u64 engine)
77 {
78 struct nvif_object *object = &device->object;
79 struct {
80 struct nv_device_info_v1 m;
81 struct {
82 struct nv_device_info_v1_data engine;
83 } v;
84 } a = {
85 .m.version = 1,
86 .m.count = sizeof(a.v) / sizeof(a.v.engine),
87 .v.engine.mthd = engine,
88 };
89 u64 runm = 0;
90 int ret, i;
91
92 if ((ret = nvif_fifo_runlists(device)))
93 return runm;
94
95 ret = nvif_object_mthd(object, NV_DEVICE_V0_INFO, &a, sizeof(a));
96 if (ret == 0) {
97 for (i = 0; i < device->runlists; i++) {
98 if (device->runlist[i].engines & a.v.engine.data)
99 runm |= BIT_ULL(i);
100 }
101 }
102
103 return runm;
104 }
105