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