1 /*
2  * Copyright 2015 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 <bskeggs@redhat.com>
23  */
24 #include "priv.h"
25 
26 #include <core/option.h>
27 #include <core/pci.h>
28 #include <subdev/mc.h>
29 
30 u32
31 nvkm_pci_rd32(struct nvkm_pci *pci, u16 addr)
32 {
33 	return pci->func->rd32(pci, addr);
34 }
35 
36 void
37 nvkm_pci_wr08(struct nvkm_pci *pci, u16 addr, u8 data)
38 {
39 	pci->func->wr08(pci, addr, data);
40 }
41 
42 void
43 nvkm_pci_wr32(struct nvkm_pci *pci, u16 addr, u32 data)
44 {
45 	pci->func->wr32(pci, addr, data);
46 }
47 
48 void
49 nvkm_pci_rom_shadow(struct nvkm_pci *pci, bool shadow)
50 {
51 	u32 data = nvkm_pci_rd32(pci, 0x0050);
52 	if (shadow)
53 		data |=  0x00000001;
54 	else
55 		data &= ~0x00000001;
56 	nvkm_pci_wr32(pci, 0x0050, data);
57 }
58 
59 static irqreturn_t
60 nvkm_pci_intr(int irq, void *arg)
61 {
62 	struct nvkm_pci *pci = arg;
63 	struct nvkm_mc *mc = pci->subdev.device->mc;
64 	bool handled = false;
65 	if (likely(mc)) {
66 		nvkm_mc_intr_unarm(mc);
67 		if (pci->msi)
68 			pci->func->msi_rearm(pci);
69 		nvkm_mc_intr(mc, &handled);
70 		nvkm_mc_intr_rearm(mc);
71 	}
72 	return handled ? IRQ_HANDLED : IRQ_NONE;
73 }
74 
75 static int
76 nvkm_pci_fini(struct nvkm_subdev *subdev, bool suspend)
77 {
78 	struct nvkm_pci *pci = nvkm_pci(subdev);
79 	if (pci->irq >= 0) {
80 		free_irq(pci->irq, pci);
81 		pci->irq = -1;
82 	};
83 	return 0;
84 }
85 
86 static int
87 nvkm_pci_init(struct nvkm_subdev *subdev)
88 {
89 	struct nvkm_pci *pci = nvkm_pci(subdev);
90 	struct pci_dev *pdev = pci->pdev;
91 	int ret;
92 
93 	ret = request_irq(pdev->irq, nvkm_pci_intr, IRQF_SHARED, "nvkm", pci);
94 	if (ret)
95 		return ret;
96 
97 	pci->irq = pdev->irq;
98 	return ret;
99 }
100 
101 static void *
102 nvkm_pci_dtor(struct nvkm_subdev *subdev)
103 {
104 	struct nvkm_pci *pci = nvkm_pci(subdev);
105 	if (pci->msi)
106 		pci_disable_msi(pci->pdev);
107 	return nvkm_pci(subdev);
108 }
109 
110 static const struct nvkm_subdev_func
111 nvkm_pci_func = {
112 	.dtor = nvkm_pci_dtor,
113 	.init = nvkm_pci_init,
114 	.fini = nvkm_pci_fini,
115 };
116 
117 int
118 nvkm_pci_new_(const struct nvkm_pci_func *func, struct nvkm_device *device,
119 	      int index, struct nvkm_pci **ppci)
120 {
121 	struct nvkm_pci *pci;
122 
123 	if (!(pci = *ppci = kzalloc(sizeof(**ppci), GFP_KERNEL)))
124 		return -ENOMEM;
125 	nvkm_subdev_ctor(&nvkm_pci_func, device, index, 0, &pci->subdev);
126 	pci->func = func;
127 	pci->pdev = device->func->pci(device)->pdev;
128 	pci->irq = -1;
129 
130 	switch (pci->pdev->device & 0x0ff0) {
131 	case 0x00f0:
132 	case 0x02e0:
133 		/* BR02? NFI how these would be handled yet exactly */
134 		break;
135 	default:
136 		switch (device->chipset) {
137 		case 0xaa:
138 			/* reported broken, nv also disable it */
139 			break;
140 		default:
141 			pci->msi = true;
142 			break;
143 		}
144 	}
145 
146 	pci->msi = nvkm_boolopt(device->cfgopt, "NvMSI", pci->msi);
147 	if (pci->msi && func->msi_rearm) {
148 		pci->msi = pci_enable_msi(pci->pdev) == 0;
149 		if (pci->msi)
150 			nvkm_debug(&pci->subdev, "MSI enabled\n");
151 	} else {
152 		pci->msi = false;
153 	}
154 
155 	return 0;
156 }
157