drm/nouveau/device: cleaner abstraction for device resource functions
[deliverable/linux.git] / drivers / gpu / drm / nouveau / nvkm / engine / device / pci.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 <core/pci.h>
25 #include "priv.h"
26
27 static struct nvkm_device_pci *
28 nvkm_device_pci(struct nvkm_device *device)
29 {
30 return container_of(device, struct nvkm_device_pci, device);
31 }
32
33 static resource_size_t
34 nvkm_device_pci_resource_addr(struct nvkm_device *device, unsigned bar)
35 {
36 struct nvkm_device_pci *pdev = nvkm_device_pci(device);
37 return pci_resource_start(pdev->pdev, bar);
38 }
39
40 static resource_size_t
41 nvkm_device_pci_resource_size(struct nvkm_device *device, unsigned bar)
42 {
43 struct nvkm_device_pci *pdev = nvkm_device_pci(device);
44 return pci_resource_len(pdev->pdev, bar);
45 }
46
47 static void
48 nvkm_device_pci_fini(struct nvkm_device *device, bool suspend)
49 {
50 struct nvkm_device_pci *pdev = nvkm_device_pci(device);
51 if (suspend) {
52 pci_disable_device(pdev->pdev);
53 pdev->suspend = true;
54 }
55 }
56
57 static int
58 nvkm_device_pci_preinit(struct nvkm_device *device)
59 {
60 struct nvkm_device_pci *pdev = nvkm_device_pci(device);
61 if (pdev->suspend) {
62 int ret = pci_enable_device(pdev->pdev);
63 if (ret)
64 return ret;
65 pci_set_master(pdev->pdev);
66 pdev->suspend = false;
67 }
68 return 0;
69 }
70
71 static void *
72 nvkm_device_pci_dtor(struct nvkm_device *device)
73 {
74 struct nvkm_device_pci *pdev = nvkm_device_pci(device);
75 pci_disable_device(pdev->pdev);
76 return pdev;
77 }
78
79 static const struct nvkm_device_func
80 nvkm_device_pci_func = {
81 .pci = nvkm_device_pci,
82 .dtor = nvkm_device_pci_dtor,
83 .preinit = nvkm_device_pci_preinit,
84 .fini = nvkm_device_pci_fini,
85 .resource_addr = nvkm_device_pci_resource_addr,
86 .resource_size = nvkm_device_pci_resource_size,
87 };
88
89 int
90 nvkm_device_pci_new(struct pci_dev *pci_dev, const char *cfg, const char *dbg,
91 bool detect, bool mmio, u64 subdev_mask,
92 struct nvkm_device **pdevice)
93 {
94 struct nvkm_device_pci *pdev;
95 int ret;
96
97 ret = pci_enable_device(pci_dev);
98 if (ret)
99 return ret;
100
101 if (!(pdev = kzalloc(sizeof(*pdev), GFP_KERNEL))) {
102 pci_disable_device(pci_dev);
103 return -ENOMEM;
104 }
105 *pdevice = &pdev->device;
106 pdev->pdev = pci_dev;
107
108 return nvkm_device_ctor(&nvkm_device_pci_func, NULL,
109 pci_dev, NVKM_BUS_PCI,
110 (u64)pci_domain_nr(pci_dev->bus) << 32 |
111 pci_dev->bus->number << 16 |
112 PCI_SLOT(pci_dev->devfn) << 8 |
113 PCI_FUNC(pci_dev->devfn), NULL,
114 cfg, dbg, detect, mmio, subdev_mask,
115 &pdev->device);
116 }
This page took 0.043745 seconds and 5 git commands to generate.