forked from tenstorrent/tt-kmd
/
enumerate.c
168 lines (126 loc) · 4.62 KB
/
enumerate.c
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// SPDX-FileCopyrightText: © 2023 Tenstorrent Inc.
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/slab.h>
#include <linux/idr.h>
#include <linux/mutex.h>
#include <linux/version.h>
#include "enumerate.h"
#include "interrupt.h"
#include "chardev.h"
#include "grayskull.h"
#include "module.h"
#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 0, 0)
#define pci_enable_pcie_error_reporting(dev) do { } while (0)
#define pci_disable_pcie_error_reporting(dev) do { } while (0)
#else
#include <linux/aer.h>
#endif
DEFINE_IDR(tenstorrent_dev_idr);
DEFINE_MUTEX(tenstorrent_dev_idr_mutex);
static int tenstorrent_reboot_notifier(struct notifier_block *nb,
unsigned long action, void *data) {
struct tenstorrent_device *tt_dev = container_of(nb, struct tenstorrent_device, reboot_notifier);
if (action != SYS_POWER_OFF)
tt_dev->dev_class->reboot(tt_dev);
return NOTIFY_DONE;
}
static int tenstorrent_pci_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
struct tenstorrent_device *tt_dev = NULL;
int ordinal;
const struct tenstorrent_device_class *device_class = (const struct tenstorrent_device_class *)id->driver_data;
printk(KERN_INFO "Found a Tenstorrent %s device at bus %04x:%d.\n",
device_class->name, (unsigned)pci_domain_nr(dev->bus), (int)dev->bus->number);
// During pre-test, unflashed boards have no class code which trips up __dev_sort_resources.
// Assign the proper class code and rerun resource assignment to clear things up.
if (dev->class >> 8 == PCI_CLASS_NOT_DEFINED) {
dev->class = 0x120000; // Processing Accelerator - vendor-specific interface
pci_assign_unassigned_bus_resources(dev->bus);
}
if (pci_enable_device(dev) < 0)
return -EIO;
tt_dev = kzalloc(device_class->instance_size, GFP_KERNEL);
if (tt_dev == NULL)
return -ENOMEM;
mutex_lock(&tenstorrent_dev_idr_mutex);
ordinal = idr_alloc(&tenstorrent_dev_idr, tt_dev, 0, 0, GFP_KERNEL);
mutex_unlock(&tenstorrent_dev_idr_mutex);
if (ordinal < 0) {
kfree(tt_dev);
pci_disable_device(dev);
return ordinal;
}
// The refcount created here persists until remove.
kref_init(&tt_dev->kref);
tt_dev->dev_class = device_class;
tt_dev->pdev = pci_dev_get(dev);
tt_dev->ordinal = ordinal;
mutex_init(&tt_dev->chardev_mutex);
tt_dev->dma_capable = (dma_set_mask_and_coherent(&dev->dev, DMA_BIT_MASK(dma_address_bits ?: 32)) == 0);
pci_set_master(dev);
pci_enable_pcie_error_reporting(dev);
pci_set_drvdata(dev, tt_dev);
tt_dev->interrupt_enabled = tenstorrent_enable_interrupts(tt_dev);
if (device_class->init_device(tt_dev))
device_class->init_hardware(tt_dev);
pci_save_state(dev);
tenstorrent_register_device(tt_dev);
if (device_class->reboot) {
tt_dev->reboot_notifier.notifier_call = tenstorrent_reboot_notifier;
register_reboot_notifier(&tt_dev->reboot_notifier);
}
return 0;
}
static void tenstorrent_pci_remove(struct pci_dev *dev)
{
struct tenstorrent_device *tt_dev = pci_get_drvdata(dev);
// These remove child sysfs entries which must happen before remove returns.
tenstorrent_unregister_device(tt_dev);
tenstorrent_disable_interrupts(tt_dev);
pci_set_drvdata(dev, NULL);
// If this is postponed, a subsequent probe is forced to use a different ordinal.
mutex_lock(&tenstorrent_dev_idr_mutex);
idr_remove(&tenstorrent_dev_idr, tt_dev->ordinal);
mutex_unlock(&tenstorrent_dev_idr_mutex);
tenstorrent_device_put(tt_dev);
}
static void tt_dev_release(struct kref *tt_dev_kref) {
struct tenstorrent_device *tt_dev = container_of(tt_dev_kref, struct tenstorrent_device, kref);
struct pci_dev *pdev = tt_dev->pdev;
if (tt_dev->dev_class->reboot)
unregister_reboot_notifier(&tt_dev->reboot_notifier);
tt_dev->dev_class->cleanup_device(tt_dev);
pci_disable_pcie_error_reporting(pdev);
pci_disable_device(pdev);
pci_dev_put(pdev);
kfree(tt_dev);
}
void tenstorrent_device_put(struct tenstorrent_device *tt_dev) {
kref_put(&tt_dev->kref, tt_dev_release);
}
extern const struct pci_device_id tenstorrent_ids[];
static struct pci_driver tenstorrent_pci_driver = {
.name = TENSTORRENT,
.id_table = tenstorrent_ids,
.probe = tenstorrent_pci_probe,
.remove = tenstorrent_pci_remove,
.shutdown = tenstorrent_pci_remove,
};
int tenstorrent_pci_register_driver(void)
{
return pci_register_driver(&tenstorrent_pci_driver);
}
void tenstorrent_pci_unregister_driver(void)
{
pci_unregister_driver(&tenstorrent_pci_driver);
}
struct tenstorrent_device *tenstorrent_lookup_device(unsigned minor)
{
struct tenstorrent_device *dev;
mutex_lock(&tenstorrent_dev_idr_mutex);
dev = idr_find(&tenstorrent_dev_idr, minor);
mutex_unlock(&tenstorrent_dev_idr_mutex);
return dev;
}