1 /*
2  * Copyright 2021 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 "priv.h"
23 
24 #include <nvif/class.h>
25 
26 static void
27 tu102_vfn_intr_reset(struct nvkm_intr *intr, int leaf, u32 mask)
28 {
29 	struct nvkm_vfn *vfn = container_of(intr, typeof(*vfn), intr);
30 
31 	nvkm_wr32(vfn->subdev.device, vfn->addr.priv + 0x1000 + (leaf * 4), mask);
32 }
33 
34 static void
35 tu102_vfn_intr_allow(struct nvkm_intr *intr, int leaf, u32 mask)
36 {
37 	struct nvkm_vfn *vfn = container_of(intr, typeof(*vfn), intr);
38 
39 	nvkm_wr32(vfn->subdev.device, vfn->addr.priv + 0x1200 + (leaf * 4), mask);
40 }
41 
42 static void
43 tu102_vfn_intr_block(struct nvkm_intr *intr, int leaf, u32 mask)
44 {
45 	struct nvkm_vfn *vfn = container_of(intr, typeof(*vfn), intr);
46 
47 	nvkm_wr32(vfn->subdev.device, vfn->addr.priv + 0x1400 + (leaf * 4), mask);
48 }
49 
50 static void
51 tu102_vfn_intr_rearm(struct nvkm_intr *intr)
52 {
53 	struct nvkm_vfn *vfn = container_of(intr, typeof(*vfn), intr);
54 
55 	nvkm_wr32(vfn->subdev.device, vfn->addr.priv + 0x1608, 0x0000000f);
56 }
57 
58 static void
59 tu102_vfn_intr_unarm(struct nvkm_intr *intr)
60 {
61 	struct nvkm_vfn *vfn = container_of(intr, typeof(*vfn), intr);
62 
63 	nvkm_wr32(vfn->subdev.device, vfn->addr.priv + 0x1610, 0x0000000f);
64 }
65 
66 static bool
67 tu102_vfn_intr_pending(struct nvkm_intr *intr)
68 {
69 	struct nvkm_vfn *vfn = container_of(intr, typeof(*vfn), intr);
70 	struct nvkm_device *device = vfn->subdev.device;
71 	u32 intr_top = nvkm_rd32(device, vfn->addr.priv + 0x1600);
72 	int pending = 0, leaf;
73 
74 	for (leaf = 0; leaf < 8; leaf++) {
75 		if (intr_top & BIT(leaf / 2)) {
76 			intr->stat[leaf] = nvkm_rd32(device, vfn->addr.priv + 0x1000 + (leaf * 4));
77 			if (intr->stat[leaf])
78 				pending++;
79 		} else {
80 			intr->stat[leaf] = 0;
81 		}
82 	}
83 
84 	return pending != 0;
85 }
86 
87 const struct nvkm_intr_func
88 tu102_vfn_intr = {
89 	.pending = tu102_vfn_intr_pending,
90 	.unarm = tu102_vfn_intr_unarm,
91 	.rearm = tu102_vfn_intr_rearm,
92 	.block = tu102_vfn_intr_block,
93 	.allow = tu102_vfn_intr_allow,
94 	.reset = tu102_vfn_intr_reset,
95 };
96 
97 static const struct nvkm_vfn_func
98 tu102_vfn = {
99 	.intr = &tu102_vfn_intr,
100 	.user = { 0x030000, 0x010000, { -1, -1, TURING_USERMODE_A } },
101 };
102 
103 int
104 tu102_vfn_new(struct nvkm_device *device,
105 	      enum nvkm_subdev_type type, int inst, struct nvkm_vfn **pvfn)
106 {
107 	return nvkm_vfn_new_(&tu102_vfn, device, type, inst, 0xb80000, pvfn);
108 }
109