18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2018 Red Hat Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci */
228c2ecf20Sopenharmony_ci#include <nvif/fifo.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic int
258c2ecf20Sopenharmony_cinvif_fifo_runlists(struct nvif_device *device)
268c2ecf20Sopenharmony_ci{
278c2ecf20Sopenharmony_ci	struct nvif_object *object = &device->object;
288c2ecf20Sopenharmony_ci	struct {
298c2ecf20Sopenharmony_ci		struct nv_device_info_v1 m;
308c2ecf20Sopenharmony_ci		struct {
318c2ecf20Sopenharmony_ci			struct nv_device_info_v1_data runlists;
328c2ecf20Sopenharmony_ci			struct nv_device_info_v1_data runlist[64];
338c2ecf20Sopenharmony_ci		} v;
348c2ecf20Sopenharmony_ci	} *a;
358c2ecf20Sopenharmony_ci	int ret, i;
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci	if (device->runlist)
388c2ecf20Sopenharmony_ci		return 0;
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci	if (!(a = kmalloc(sizeof(*a), GFP_KERNEL)))
418c2ecf20Sopenharmony_ci		return -ENOMEM;
428c2ecf20Sopenharmony_ci	a->m.version = 1;
438c2ecf20Sopenharmony_ci	a->m.count = sizeof(a->v) / sizeof(a->v.runlists);
448c2ecf20Sopenharmony_ci	a->v.runlists.mthd = NV_DEVICE_FIFO_RUNLISTS;
458c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(a->v.runlist); i++)
468c2ecf20Sopenharmony_ci		a->v.runlist[i].mthd = NV_DEVICE_FIFO_RUNLIST_ENGINES(i);
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci	ret = nvif_object_mthd(object, NV_DEVICE_V0_INFO, a, sizeof(*a));
498c2ecf20Sopenharmony_ci	if (ret)
508c2ecf20Sopenharmony_ci		goto done;
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	device->runlists = fls64(a->v.runlists.data);
538c2ecf20Sopenharmony_ci	device->runlist = kcalloc(device->runlists, sizeof(*device->runlist),
548c2ecf20Sopenharmony_ci				  GFP_KERNEL);
558c2ecf20Sopenharmony_ci	if (!device->runlist) {
568c2ecf20Sopenharmony_ci		ret = -ENOMEM;
578c2ecf20Sopenharmony_ci		goto done;
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	for (i = 0; i < device->runlists; i++) {
618c2ecf20Sopenharmony_ci		if (a->v.runlists.data & BIT_ULL(i))
628c2ecf20Sopenharmony_ci			device->runlist[i].engines = a->v.runlist[i].data;
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cidone:
668c2ecf20Sopenharmony_ci	kfree(a);
678c2ecf20Sopenharmony_ci	return ret;
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciu64
718c2ecf20Sopenharmony_cinvif_fifo_runlist(struct nvif_device *device, u64 engine)
728c2ecf20Sopenharmony_ci{
738c2ecf20Sopenharmony_ci	struct nvif_object *object = &device->object;
748c2ecf20Sopenharmony_ci	struct {
758c2ecf20Sopenharmony_ci		struct nv_device_info_v1 m;
768c2ecf20Sopenharmony_ci		struct {
778c2ecf20Sopenharmony_ci			struct nv_device_info_v1_data engine;
788c2ecf20Sopenharmony_ci		} v;
798c2ecf20Sopenharmony_ci	} a = {
808c2ecf20Sopenharmony_ci		.m.version = 1,
818c2ecf20Sopenharmony_ci		.m.count = sizeof(a.v) / sizeof(a.v.engine),
828c2ecf20Sopenharmony_ci		.v.engine.mthd = engine,
838c2ecf20Sopenharmony_ci	};
848c2ecf20Sopenharmony_ci	u64 runm = 0;
858c2ecf20Sopenharmony_ci	int ret, i;
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	if ((ret = nvif_fifo_runlists(device)))
888c2ecf20Sopenharmony_ci		return runm;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	ret = nvif_object_mthd(object, NV_DEVICE_V0_INFO, &a, sizeof(a));
918c2ecf20Sopenharmony_ci	if (ret == 0) {
928c2ecf20Sopenharmony_ci		for (i = 0; i < device->runlists; i++) {
938c2ecf20Sopenharmony_ci			if (device->runlist[i].engines & a.v.engine.data)
948c2ecf20Sopenharmony_ci				runm |= BIT_ULL(i);
958c2ecf20Sopenharmony_ci		}
968c2ecf20Sopenharmony_ci	}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	return runm;
998c2ecf20Sopenharmony_ci}
100