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 static inline u32
36 nvkm_mc_intr_mask(struct nvkm_mc *mc)
37 {
38 	struct nvkm_device *device = mc->subdev.device;
39 	u32 intr = nvkm_rd32(device, 0x000100);
40 	if (intr == 0xffffffff) /* likely fallen off the bus */
41 		intr = 0x00000000;
42 	return intr;
43 }
44 
45 static irqreturn_t
46 nvkm_mc_intr(int irq, void *arg)
47 {
48 	struct nvkm_mc *mc = arg;
49 	struct nvkm_subdev *subdev = &mc->subdev;
50 	struct nvkm_device *device = subdev->device;
51 	const struct nvkm_mc_intr *map = mc->func->intr;
52 	struct nvkm_subdev *unit;
53 	u32 intr;
54 
55 	nvkm_wr32(device, 0x000140, 0x00000000);
56 	nvkm_rd32(device, 0x000140);
57 	intr = nvkm_mc_intr_mask(mc);
58 	if (mc->use_msi)
59 		mc->func->msi_rearm(mc);
60 
61 	if (intr) {
62 		u32 stat = intr = nvkm_mc_intr_mask(mc);
63 		while (map->stat) {
64 			if (intr & map->stat) {
65 				unit = nvkm_device_subdev(device, map->unit);
66 				if (unit)
67 					nvkm_subdev_intr(unit);
68 				stat &= ~map->stat;
69 			}
70 			map++;
71 		}
72 
73 		if (stat)
74 			nvkm_error(subdev, "unknown intr %08x\n", stat);
75 	}
76 
77 	nvkm_wr32(device, 0x000140, 0x00000001);
78 	return intr ? IRQ_HANDLED : IRQ_NONE;
79 }
80 
81 static int
82 nvkm_mc_fini(struct nvkm_subdev *subdev, bool suspend)
83 {
84 	nvkm_wr32(subdev->device, 0x000140, 0x00000000);
85 	return 0;
86 }
87 
88 static int
89 nvkm_mc_oneinit(struct nvkm_subdev *subdev)
90 {
91 	struct nvkm_mc *mc = nvkm_mc(subdev);
92 	return request_irq(mc->irq, nvkm_mc_intr, IRQF_SHARED, "nvkm", mc);
93 }
94 
95 static int
96 nvkm_mc_init(struct nvkm_subdev *subdev)
97 {
98 	struct nvkm_mc *mc = nvkm_mc(subdev);
99 	struct nvkm_device *device = mc->subdev.device;
100 	if (mc->func->init)
101 		mc->func->init(mc);
102 	nvkm_wr32(device, 0x000140, 0x00000001);
103 	return 0;
104 }
105 
106 static void *
107 nvkm_mc_dtor(struct nvkm_subdev *subdev)
108 {
109 	struct nvkm_mc *mc = nvkm_mc(subdev);
110 	struct nvkm_device *device = mc->subdev.device;
111 	free_irq(mc->irq, mc);
112 	if (mc->use_msi)
113 		pci_disable_msi(device->pdev);
114 	return mc;
115 }
116 
117 static const struct nvkm_subdev_func
118 nvkm_mc = {
119 	.dtor = nvkm_mc_dtor,
120 	.oneinit = nvkm_mc_oneinit,
121 	.init = nvkm_mc_init,
122 	.fini = nvkm_mc_fini,
123 };
124 
125 int
126 nvkm_mc_new_(const struct nvkm_mc_func *func, struct nvkm_device *device,
127 	     int index, struct nvkm_mc **pmc)
128 {
129 	struct nvkm_mc *mc;
130 	int ret;
131 
132 	if (!(mc = *pmc = kzalloc(sizeof(*mc), GFP_KERNEL)))
133 		return -ENOMEM;
134 
135 	nvkm_subdev_ctor(&nvkm_mc, device, index, 0, &mc->subdev);
136 	mc->func = func;
137 
138 	if (nv_device_is_pci(device)) {
139 		switch (device->pdev->device & 0x0ff0) {
140 		case 0x00f0:
141 		case 0x02e0:
142 			/* BR02? NFI how these would be handled yet exactly */
143 			break;
144 		default:
145 			switch (device->chipset) {
146 			case 0xaa:
147 				/* reported broken, nv also disable it */
148 				break;
149 			default:
150 				mc->use_msi = true;
151 				break;
152 			}
153 		}
154 
155 		mc->use_msi = nvkm_boolopt(device->cfgopt, "NvMSI",
156 					    mc->use_msi);
157 
158 		if (mc->use_msi && mc->func->msi_rearm) {
159 			mc->use_msi = pci_enable_msi(device->pdev) == 0;
160 			if (mc->use_msi) {
161 				nvkm_debug(&mc->subdev, "MSI enabled\n");
162 				mc->func->msi_rearm(mc);
163 			}
164 		} else {
165 			mc->use_msi = false;
166 		}
167 	}
168 
169 	ret = nv_device_get_irq(device, true);
170 	if (ret < 0)
171 		return ret;
172 	mc->irq = ret;
173 	return 0;
174 }
175