1 /*
2  * Copyright 2012 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  * Authors: Ben Skeggs
23  */
24 #include "priv.h"
25 
26 #include <core/option.h>
27 
28 void
29 nvkm_mc_unk260(struct nvkm_mc *mc, u32 data)
30 {
31 	if (mc->func->unk260)
32 		mc->func->unk260(mc, data);
33 }
34 
35 void
36 nvkm_mc_intr_unarm(struct nvkm_mc *mc)
37 {
38 	return mc->func->intr_unarm(mc);
39 }
40 
41 void
42 nvkm_mc_intr_rearm(struct nvkm_mc *mc)
43 {
44 	return mc->func->intr_rearm(mc);
45 }
46 
47 static u32
48 nvkm_mc_intr_mask(struct nvkm_mc *mc)
49 {
50 	u32 intr = mc->func->intr_mask(mc);
51 	if (WARN_ON_ONCE(intr == 0xffffffff))
52 		intr = 0; /* likely fallen off the bus */
53 	return intr;
54 }
55 
56 void
57 nvkm_mc_intr(struct nvkm_mc *mc, bool *handled)
58 {
59 	struct nvkm_device *device = mc->subdev.device;
60 	struct nvkm_subdev *subdev;
61 	const struct nvkm_mc_intr *map = mc->func->intr;
62 	u32 stat, intr;
63 
64 	stat = intr = nvkm_mc_intr_mask(mc);
65 	while (map->stat) {
66 		if (intr & map->stat) {
67 			subdev = nvkm_device_subdev(device, map->unit);
68 			if (subdev)
69 				nvkm_subdev_intr(subdev);
70 			stat &= ~map->stat;
71 		}
72 		map++;
73 	}
74 
75 	if (stat)
76 		nvkm_error(&mc->subdev, "intr %08x\n", stat);
77 	*handled = intr != 0;
78 }
79 
80 static int
81 nvkm_mc_fini(struct nvkm_subdev *subdev, bool suspend)
82 {
83 	struct nvkm_mc *mc = nvkm_mc(subdev);
84 	nvkm_mc_intr_unarm(mc);
85 	return 0;
86 }
87 
88 static int
89 nvkm_mc_init(struct nvkm_subdev *subdev)
90 {
91 	struct nvkm_mc *mc = nvkm_mc(subdev);
92 	if (mc->func->init)
93 		mc->func->init(mc);
94 	nvkm_mc_intr_rearm(mc);
95 	return 0;
96 }
97 
98 static void *
99 nvkm_mc_dtor(struct nvkm_subdev *subdev)
100 {
101 	return nvkm_mc(subdev);
102 }
103 
104 static const struct nvkm_subdev_func
105 nvkm_mc = {
106 	.dtor = nvkm_mc_dtor,
107 	.init = nvkm_mc_init,
108 	.fini = nvkm_mc_fini,
109 };
110 
111 int
112 nvkm_mc_new_(const struct nvkm_mc_func *func, struct nvkm_device *device,
113 	     int index, struct nvkm_mc **pmc)
114 {
115 	struct nvkm_mc *mc;
116 
117 	if (!(mc = *pmc = kzalloc(sizeof(*mc), GFP_KERNEL)))
118 		return -ENOMEM;
119 
120 	nvkm_subdev_ctor(&nvkm_mc, device, index, 0, &mc->subdev);
121 	mc->func = func;
122 	return 0;
123 }
124