GNU Linux-libre 4.4.283-gnu1
[releases.git] / drivers / gpu / drm / nouveau / nvkm / subdev / pci / base.c
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 #include "agp.h"
26
27 #include <core/option.h>
28 #include <core/pci.h>
29 #include <subdev/mc.h>
30
31 u32
32 nvkm_pci_rd32(struct nvkm_pci *pci, u16 addr)
33 {
34         return pci->func->rd32(pci, addr);
35 }
36
37 void
38 nvkm_pci_wr08(struct nvkm_pci *pci, u16 addr, u8 data)
39 {
40         pci->func->wr08(pci, addr, data);
41 }
42
43 void
44 nvkm_pci_wr32(struct nvkm_pci *pci, u16 addr, u32 data)
45 {
46         pci->func->wr32(pci, addr, data);
47 }
48
49 u32
50 nvkm_pci_mask(struct nvkm_pci *pci, u16 addr, u32 mask, u32 value)
51 {
52         u32 data = pci->func->rd32(pci, addr);
53         pci->func->wr32(pci, addr, (data & ~mask) | value);
54         return data;
55 }
56
57 void
58 nvkm_pci_rom_shadow(struct nvkm_pci *pci, bool shadow)
59 {
60         u32 data = nvkm_pci_rd32(pci, 0x0050);
61         if (shadow)
62                 data |=  0x00000001;
63         else
64                 data &= ~0x00000001;
65         nvkm_pci_wr32(pci, 0x0050, data);
66 }
67
68 static irqreturn_t
69 nvkm_pci_intr(int irq, void *arg)
70 {
71         struct nvkm_pci *pci = arg;
72         struct nvkm_mc *mc = pci->subdev.device->mc;
73         bool handled = false;
74         if (likely(mc)) {
75                 nvkm_mc_intr_unarm(mc);
76                 if (pci->msi)
77                         pci->func->msi_rearm(pci);
78                 nvkm_mc_intr(mc, &handled);
79                 nvkm_mc_intr_rearm(mc);
80         }
81         return handled ? IRQ_HANDLED : IRQ_NONE;
82 }
83
84 static int
85 nvkm_pci_fini(struct nvkm_subdev *subdev, bool suspend)
86 {
87         struct nvkm_pci *pci = nvkm_pci(subdev);
88
89         if (pci->irq >= 0) {
90                 free_irq(pci->irq, pci);
91                 pci->irq = -1;
92         };
93
94         if (pci->agp.bridge)
95                 nvkm_agp_fini(pci);
96
97         return 0;
98 }
99
100 static int
101 nvkm_pci_preinit(struct nvkm_subdev *subdev)
102 {
103         struct nvkm_pci *pci = nvkm_pci(subdev);
104         if (pci->agp.bridge)
105                 nvkm_agp_preinit(pci);
106         return 0;
107 }
108
109 static int
110 nvkm_pci_init(struct nvkm_subdev *subdev)
111 {
112         struct nvkm_pci *pci = nvkm_pci(subdev);
113         struct pci_dev *pdev = pci->pdev;
114         int ret;
115
116         if (pci->agp.bridge) {
117                 ret = nvkm_agp_init(pci);
118                 if (ret)
119                         return ret;
120         }
121
122         if (pci->func->init)
123                 pci->func->init(pci);
124
125         ret = request_irq(pdev->irq, nvkm_pci_intr, IRQF_SHARED, "nvkm", pci);
126         if (ret)
127                 return ret;
128
129         pci->irq = pdev->irq;
130
131         /* Ensure MSI interrupts are armed, for the case where there are
132          * already interrupts pending (for whatever reason) at load time.
133          */
134         if (pci->msi)
135                 pci->func->msi_rearm(pci);
136
137         return ret;
138 }
139
140 static void *
141 nvkm_pci_dtor(struct nvkm_subdev *subdev)
142 {
143         struct nvkm_pci *pci = nvkm_pci(subdev);
144         nvkm_agp_dtor(pci);
145         if (pci->msi)
146                 pci_disable_msi(pci->pdev);
147         return nvkm_pci(subdev);
148 }
149
150 static const struct nvkm_subdev_func
151 nvkm_pci_func = {
152         .dtor = nvkm_pci_dtor,
153         .preinit = nvkm_pci_preinit,
154         .init = nvkm_pci_init,
155         .fini = nvkm_pci_fini,
156 };
157
158 int
159 nvkm_pci_new_(const struct nvkm_pci_func *func, struct nvkm_device *device,
160               int index, struct nvkm_pci **ppci)
161 {
162         struct nvkm_pci *pci;
163
164         if (!(pci = *ppci = kzalloc(sizeof(**ppci), GFP_KERNEL)))
165                 return -ENOMEM;
166         nvkm_subdev_ctor(&nvkm_pci_func, device, index, 0, &pci->subdev);
167         pci->func = func;
168         pci->pdev = device->func->pci(device)->pdev;
169         pci->irq = -1;
170
171         if (device->type == NVKM_DEVICE_AGP)
172                 nvkm_agp_ctor(pci);
173
174         switch (pci->pdev->device & 0x0ff0) {
175         case 0x00f0:
176         case 0x02e0:
177                 /* BR02? NFI how these would be handled yet exactly */
178                 break;
179         default:
180                 switch (device->chipset) {
181                 case 0xaa:
182                         /* reported broken, nv also disable it */
183                         break;
184                 default:
185                         pci->msi = true;
186                         break;
187                 }
188         }
189
190 #ifdef __BIG_ENDIAN
191         pci->msi = false;
192 #endif
193
194         pci->msi = nvkm_boolopt(device->cfgopt, "NvMSI", pci->msi);
195         if (pci->msi && func->msi_rearm) {
196                 pci->msi = pci_enable_msi(pci->pdev) == 0;
197                 if (pci->msi)
198                         nvkm_debug(&pci->subdev, "MSI enabled\n");
199         } else {
200                 pci->msi = false;
201         }
202
203         return 0;
204 }