aboutsummaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/nouveau/nvkm/engine/device/pci.c
blob: 1a1d1e584a7f9fc265fbbfcd5d26e3b0203fad73 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
 * Copyright 2015 Red Hat Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Ben Skeggs <bskeggs@redhat.com>
 */
#include <core/pci.h>
#include "priv.h"

static struct nvkm_device_pci *
nvkm_device_pci(struct nvkm_device *device)
{
	return container_of(device, struct nvkm_device_pci, device);
}

static void
nvkm_device_pci_fini(struct nvkm_device *device, bool suspend)
{
	struct nvkm_device_pci *pdev = nvkm_device_pci(device);
	if (suspend) {
		pci_disable_device(pdev->pdev);
		pdev->suspend = true;
	}
}

static int
nvkm_device_pci_preinit(struct nvkm_device *device)
{
	struct nvkm_device_pci *pdev = nvkm_device_pci(device);
	if (pdev->suspend) {
		int ret = pci_enable_device(pdev->pdev);
		if (ret)
			return ret;
		pci_set_master(pdev->pdev);
		pdev->suspend = false;
	}
	return 0;
}

static void *
nvkm_device_pci_dtor(struct nvkm_device *device)
{
	struct nvkm_device_pci *pdev = nvkm_device_pci(device);
	pci_disable_device(pdev->pdev);
	return pdev;
}

static const struct nvkm_device_func
nvkm_device_pci_func = {
	.pci = nvkm_device_pci,
	.dtor = nvkm_device_pci_dtor,
	.preinit = nvkm_device_pci_preinit,
	.fini = nvkm_device_pci_fini,
};

int
nvkm_device_pci_new(struct pci_dev *pci_dev, const char *cfg, const char *dbg,
		    bool detect, bool mmio, u64 subdev_mask,
		    struct nvkm_device **pdevice)
{
	struct nvkm_device_pci *pdev;
	int ret;

	ret = pci_enable_device(pci_dev);
	if (ret)
		return ret;

	if (!(pdev = kzalloc(sizeof(*pdev), GFP_KERNEL))) {
		pci_disable_device(pci_dev);
		return -ENOMEM;
	}
	*pdevice = &pdev->device;
	pdev->pdev = pci_dev;

	return nvkm_device_ctor(&nvkm_device_pci_func, NULL,
				pci_dev, NVKM_BUS_PCI,
				(u64)pci_domain_nr(pci_dev->bus) << 32 |
				     pci_dev->bus->number << 16 |
				     PCI_SLOT(pci_dev->devfn) << 8 |
				     PCI_FUNC(pci_dev->devfn), NULL,
				cfg, dbg, detect, mmio, subdev_mask,
				&pdev->device);
}