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 #include <subdev/top.h>
28 
29 void
nvkm_mc_unk260(struct nvkm_device * device,u32 data)30 nvkm_mc_unk260(struct nvkm_device *device, u32 data)
31 {
32 	struct nvkm_mc *mc = device->mc;
33 	if (likely(mc) && mc->func->unk260)
34 		mc->func->unk260(mc, data);
35 }
36 
37 void
nvkm_mc_intr_mask(struct nvkm_device * device,enum nvkm_subdev_type type,int inst,bool en)38 nvkm_mc_intr_mask(struct nvkm_device *device, enum nvkm_subdev_type type, int inst, bool en)
39 {
40 	struct nvkm_subdev *subdev = nvkm_device_subdev(device, type, inst);
41 
42 	if (subdev) {
43 		if (en)
44 			nvkm_intr_allow(subdev, NVKM_INTR_SUBDEV);
45 		else
46 			nvkm_intr_block(subdev, NVKM_INTR_SUBDEV);
47 	}
48 }
49 
50 static u32
nvkm_mc_reset_mask(struct nvkm_device * device,bool isauto,enum nvkm_subdev_type type,int inst)51 nvkm_mc_reset_mask(struct nvkm_device *device, bool isauto, enum nvkm_subdev_type type, int inst)
52 {
53 	struct nvkm_mc *mc = device->mc;
54 	const struct nvkm_mc_map *map;
55 	u64 pmc_enable = 0;
56 	if (likely(mc)) {
57 		if (!(pmc_enable = nvkm_top_reset(device, type, inst))) {
58 			for (map = mc->func->reset; map && map->stat; map++) {
59 				if (!isauto || !map->noauto) {
60 					if (map->type == type && map->inst == inst) {
61 						pmc_enable = map->stat;
62 						break;
63 					}
64 				}
65 			}
66 		}
67 	}
68 	return pmc_enable;
69 }
70 
71 void
nvkm_mc_reset(struct nvkm_device * device,enum nvkm_subdev_type type,int inst)72 nvkm_mc_reset(struct nvkm_device *device, enum nvkm_subdev_type type, int inst)
73 {
74 	u64 pmc_enable = nvkm_mc_reset_mask(device, true, type, inst);
75 	if (pmc_enable) {
76 		device->mc->func->device->disable(device->mc, pmc_enable);
77 		device->mc->func->device->enable(device->mc, pmc_enable);
78 	}
79 }
80 
81 void
nvkm_mc_disable(struct nvkm_device * device,enum nvkm_subdev_type type,int inst)82 nvkm_mc_disable(struct nvkm_device *device, enum nvkm_subdev_type type, int inst)
83 {
84 	u64 pmc_enable = nvkm_mc_reset_mask(device, false, type, inst);
85 	if (pmc_enable)
86 		device->mc->func->device->disable(device->mc, pmc_enable);
87 }
88 
89 void
nvkm_mc_enable(struct nvkm_device * device,enum nvkm_subdev_type type,int inst)90 nvkm_mc_enable(struct nvkm_device *device, enum nvkm_subdev_type type, int inst)
91 {
92 	u64 pmc_enable = nvkm_mc_reset_mask(device, false, type, inst);
93 	if (pmc_enable)
94 		device->mc->func->device->enable(device->mc, pmc_enable);
95 }
96 
97 bool
nvkm_mc_enabled(struct nvkm_device * device,enum nvkm_subdev_type type,int inst)98 nvkm_mc_enabled(struct nvkm_device *device, enum nvkm_subdev_type type, int inst)
99 {
100 	u64 pmc_enable = nvkm_mc_reset_mask(device, false, type, inst);
101 
102 	return (pmc_enable != 0) && device->mc->func->device->enabled(device->mc, pmc_enable);
103 }
104 
105 static int
nvkm_mc_init(struct nvkm_subdev * subdev)106 nvkm_mc_init(struct nvkm_subdev *subdev)
107 {
108 	struct nvkm_mc *mc = nvkm_mc(subdev);
109 	if (mc->func->init)
110 		mc->func->init(mc);
111 	return 0;
112 }
113 
114 static void *
nvkm_mc_dtor(struct nvkm_subdev * subdev)115 nvkm_mc_dtor(struct nvkm_subdev *subdev)
116 {
117 	return nvkm_mc(subdev);
118 }
119 
120 static const struct nvkm_subdev_func
121 nvkm_mc = {
122 	.dtor = nvkm_mc_dtor,
123 	.init = nvkm_mc_init,
124 };
125 
126 int
nvkm_mc_new_(const struct nvkm_mc_func * func,struct nvkm_device * device,enum nvkm_subdev_type type,int inst,struct nvkm_mc ** pmc)127 nvkm_mc_new_(const struct nvkm_mc_func *func, struct nvkm_device *device,
128 	     enum nvkm_subdev_type type, int inst, struct nvkm_mc **pmc)
129 {
130 	struct nvkm_mc *mc;
131 	int ret;
132 
133 	if (!(mc = *pmc = kzalloc(sizeof(*mc), GFP_KERNEL)))
134 		return -ENOMEM;
135 
136 	nvkm_subdev_ctor(&nvkm_mc, device, type, inst, &mc->subdev);
137 	mc->func = func;
138 
139 	if (mc->func->intr) {
140 		ret = nvkm_intr_add(mc->func->intr, mc->func->intrs, &mc->subdev,
141 				    mc->func->intr_nonstall ? 2 : 1, &mc->intr);
142 		if (ret)
143 			return ret;
144 	}
145 
146 	return 0;
147 }
148