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 <nvif/fifo.h>
23
24static int
25nvif_fifo_runlists(struct nvif_device *device)
26{
27	struct nvif_object *object = &device->object;
28	struct {
29		struct nv_device_info_v1 m;
30		struct {
31			struct nv_device_info_v1_data runlists;
32			struct nv_device_info_v1_data runlist[64];
33		} v;
34	} *a;
35	int ret, i;
36
37	if (device->runlist)
38		return 0;
39
40	if (!(a = kmalloc(sizeof(*a), GFP_KERNEL)))
41		return -ENOMEM;
42	a->m.version = 1;
43	a->m.count = sizeof(a->v) / sizeof(a->v.runlists);
44	a->v.runlists.mthd = NV_DEVICE_HOST_RUNLISTS;
45	for (i = 0; i < ARRAY_SIZE(a->v.runlist); i++) {
46		a->v.runlist[i].mthd = NV_DEVICE_HOST_RUNLIST_ENGINES;
47		a->v.runlist[i].data = i;
48	}
49
50	ret = nvif_object_mthd(object, NV_DEVICE_V0_INFO, a, sizeof(*a));
51	if (ret)
52		goto done;
53
54	device->runlists = fls64(a->v.runlists.data);
55	device->runlist = kcalloc(device->runlists, sizeof(*device->runlist),
56				  GFP_KERNEL);
57	if (!device->runlist) {
58		ret = -ENOMEM;
59		goto done;
60	}
61
62	for (i = 0; i < device->runlists; i++) {
63		if (a->v.runlist[i].mthd != NV_DEVICE_INFO_INVALID)
64			device->runlist[i].engines = a->v.runlist[i].data;
65	}
66
67done:
68	kfree(a);
69	return ret;
70}
71
72u64
73nvif_fifo_runlist(struct nvif_device *device, u64 engine)
74{
75	u64 runm = 0;
76	int ret, i;
77
78	if ((ret = nvif_fifo_runlists(device)))
79		return runm;
80
81	for (i = 0; i < device->runlists; i++) {
82		if (device->runlist[i].engines & engine)
83			runm |= BIT_ULL(i);
84	}
85
86	return runm;
87}
88