2 * Copyright 2015 Red Hat Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
22 * Authors: Ben Skeggs <bskeggs@redhat.com>
27 static struct nvkm_device_pci
*
28 nvkm_device_pci(struct nvkm_device
*device
)
30 return container_of(device
, struct nvkm_device_pci
, device
);
33 static resource_size_t
34 nvkm_device_pci_resource_addr(struct nvkm_device
*device
, unsigned bar
)
36 struct nvkm_device_pci
*pdev
= nvkm_device_pci(device
);
37 return pci_resource_start(pdev
->pdev
, bar
);
40 static resource_size_t
41 nvkm_device_pci_resource_size(struct nvkm_device
*device
, unsigned bar
)
43 struct nvkm_device_pci
*pdev
= nvkm_device_pci(device
);
44 return pci_resource_len(pdev
->pdev
, bar
);
48 nvkm_device_pci_fini(struct nvkm_device
*device
, bool suspend
)
50 struct nvkm_device_pci
*pdev
= nvkm_device_pci(device
);
52 pci_disable_device(pdev
->pdev
);
58 nvkm_device_pci_preinit(struct nvkm_device
*device
)
60 struct nvkm_device_pci
*pdev
= nvkm_device_pci(device
);
62 int ret
= pci_enable_device(pdev
->pdev
);
65 pci_set_master(pdev
->pdev
);
66 pdev
->suspend
= false;
72 nvkm_device_pci_dtor(struct nvkm_device
*device
)
74 struct nvkm_device_pci
*pdev
= nvkm_device_pci(device
);
75 pci_disable_device(pdev
->pdev
);
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
,
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
)
94 struct nvkm_device_pci
*pdev
;
97 ret
= pci_enable_device(pci_dev
);
101 if (!(pdev
= kzalloc(sizeof(*pdev
), GFP_KERNEL
))) {
102 pci_disable_device(pci_dev
);
105 *pdevice
= &pdev
->device
;
106 pdev
->pdev
= pci_dev
;
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
,