amdkfd: Add process queue manager module
[deliverable/linux.git] / drivers / gpu / drm / amd / amdkfd / kfd_device.c
CommitLineData
4a488a7a
OG
1/*
2 * Copyright 2014 Advanced Micro Devices, 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
23#include <linux/amd-iommu.h>
24#include <linux/bsearch.h>
25#include <linux/pci.h>
26#include <linux/slab.h>
27#include "kfd_priv.h"
28
19f6d2a6
OG
29#define MQD_SIZE_ALIGNED 768
30
4a488a7a
OG
31static const struct kfd_device_info kaveri_device_info = {
32 .max_pasid_bits = 16,
19f6d2a6 33 .mqd_size_aligned = MQD_SIZE_ALIGNED
4a488a7a
OG
34};
35
36struct kfd_deviceid {
37 unsigned short did;
38 const struct kfd_device_info *device_info;
39};
40
41/* Please keep this sorted by increasing device id. */
42static const struct kfd_deviceid supported_devices[] = {
43 { 0x1304, &kaveri_device_info }, /* Kaveri */
44 { 0x1305, &kaveri_device_info }, /* Kaveri */
45 { 0x1306, &kaveri_device_info }, /* Kaveri */
46 { 0x1307, &kaveri_device_info }, /* Kaveri */
47 { 0x1309, &kaveri_device_info }, /* Kaveri */
48 { 0x130A, &kaveri_device_info }, /* Kaveri */
49 { 0x130B, &kaveri_device_info }, /* Kaveri */
50 { 0x130C, &kaveri_device_info }, /* Kaveri */
51 { 0x130D, &kaveri_device_info }, /* Kaveri */
52 { 0x130E, &kaveri_device_info }, /* Kaveri */
53 { 0x130F, &kaveri_device_info }, /* Kaveri */
54 { 0x1310, &kaveri_device_info }, /* Kaveri */
55 { 0x1311, &kaveri_device_info }, /* Kaveri */
56 { 0x1312, &kaveri_device_info }, /* Kaveri */
57 { 0x1313, &kaveri_device_info }, /* Kaveri */
58 { 0x1315, &kaveri_device_info }, /* Kaveri */
59 { 0x1316, &kaveri_device_info }, /* Kaveri */
60 { 0x1317, &kaveri_device_info }, /* Kaveri */
61 { 0x1318, &kaveri_device_info }, /* Kaveri */
62 { 0x131B, &kaveri_device_info }, /* Kaveri */
63 { 0x131C, &kaveri_device_info }, /* Kaveri */
64 { 0x131D, &kaveri_device_info }, /* Kaveri */
65};
66
67static const struct kfd_device_info *lookup_device_info(unsigned short did)
68{
69 size_t i;
70
71 for (i = 0; i < ARRAY_SIZE(supported_devices); i++) {
72 if (supported_devices[i].did == did) {
73 BUG_ON(supported_devices[i].device_info == NULL);
74 return supported_devices[i].device_info;
75 }
76 }
77
78 return NULL;
79}
80
81struct kfd_dev *kgd2kfd_probe(struct kgd_dev *kgd, struct pci_dev *pdev)
82{
83 struct kfd_dev *kfd;
84
85 const struct kfd_device_info *device_info =
86 lookup_device_info(pdev->device);
87
88 if (!device_info)
89 return NULL;
90
91 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
92 if (!kfd)
93 return NULL;
94
95 kfd->kgd = kgd;
96 kfd->device_info = device_info;
97 kfd->pdev = pdev;
19f6d2a6 98 kfd->init_complete = false;
4a488a7a
OG
99
100 return kfd;
101}
102
b17f068a
OG
103static bool device_iommu_pasid_init(struct kfd_dev *kfd)
104{
105 const u32 required_iommu_flags = AMD_IOMMU_DEVICE_FLAG_ATS_SUP |
106 AMD_IOMMU_DEVICE_FLAG_PRI_SUP |
107 AMD_IOMMU_DEVICE_FLAG_PASID_SUP;
108
109 struct amd_iommu_device_info iommu_info;
110 unsigned int pasid_limit;
111 int err;
112
113 err = amd_iommu_device_info(kfd->pdev, &iommu_info);
114 if (err < 0) {
115 dev_err(kfd_device,
116 "error getting iommu info. is the iommu enabled?\n");
117 return false;
118 }
119
120 if ((iommu_info.flags & required_iommu_flags) != required_iommu_flags) {
121 dev_err(kfd_device, "error required iommu flags ats(%i), pri(%i), pasid(%i)\n",
122 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_ATS_SUP) != 0,
123 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PRI_SUP) != 0,
124 (iommu_info.flags & AMD_IOMMU_DEVICE_FLAG_PASID_SUP) != 0);
125 return false;
126 }
127
128 pasid_limit = min_t(unsigned int,
129 (unsigned int)1 << kfd->device_info->max_pasid_bits,
130 iommu_info.max_pasids);
131 /*
132 * last pasid is used for kernel queues doorbells
133 * in the future the last pasid might be used for a kernel thread.
134 */
135 pasid_limit = min_t(unsigned int,
136 pasid_limit,
137 kfd->doorbell_process_limit - 1);
138
139 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
140 if (err < 0) {
141 dev_err(kfd_device, "error initializing iommu device\n");
142 return false;
143 }
144
145 if (!kfd_set_pasid_limit(pasid_limit)) {
146 dev_err(kfd_device, "error setting pasid limit\n");
147 amd_iommu_free_device(kfd->pdev);
148 return false;
149 }
150
151 return true;
152}
153
154static void iommu_pasid_shutdown_callback(struct pci_dev *pdev, int pasid)
155{
156 struct kfd_dev *dev = kfd_device_by_pci_dev(pdev);
157
158 if (dev)
159 kfd_unbind_process_from_device(dev, pasid);
160}
161
4a488a7a
OG
162bool kgd2kfd_device_init(struct kfd_dev *kfd,
163 const struct kgd2kfd_shared_resources *gpu_resources)
164{
19f6d2a6
OG
165 unsigned int size;
166
4a488a7a
OG
167 kfd->shared_resources = *gpu_resources;
168
19f6d2a6
OG
169 /* calculate max size of mqds needed for queues */
170 size = max_num_of_processes *
171 max_num_of_queues_per_process *
172 kfd->device_info->mqd_size_aligned;
173
174 /* add another 512KB for all other allocations on gart */
175 size += 512 * 1024;
176
177 if (kfd2kgd->init_sa_manager(kfd->kgd, size)) {
178 dev_err(kfd_device,
179 "Error initializing sa manager for device (%x:%x)\n",
180 kfd->pdev->vendor, kfd->pdev->device);
181 goto out;
182 }
183
184 kfd_doorbell_init(kfd);
185
186 if (kfd_topology_add_device(kfd) != 0) {
187 dev_err(kfd_device,
188 "Error adding device (%x:%x) to topology\n",
189 kfd->pdev->vendor, kfd->pdev->device);
190 goto kfd_topology_add_device_error;
191 }
192
b17f068a
OG
193 if (!device_iommu_pasid_init(kfd)) {
194 dev_err(kfd_device,
195 "Error initializing iommuv2 for device (%x:%x)\n",
196 kfd->pdev->vendor, kfd->pdev->device);
197 goto device_iommu_pasid_error;
198 }
199 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
200 iommu_pasid_shutdown_callback);
5b5c4e40 201
4a488a7a
OG
202 kfd->init_complete = true;
203 dev_info(kfd_device, "added device (%x:%x)\n", kfd->pdev->vendor,
204 kfd->pdev->device);
205
19f6d2a6
OG
206 goto out;
207
b17f068a
OG
208device_iommu_pasid_error:
209 kfd_topology_remove_device(kfd);
19f6d2a6
OG
210kfd_topology_add_device_error:
211 kfd2kgd->fini_sa_manager(kfd->kgd);
212 dev_err(kfd_device,
213 "device (%x:%x) NOT added due to errors\n",
214 kfd->pdev->vendor, kfd->pdev->device);
215out:
216 return kfd->init_complete;
4a488a7a
OG
217}
218
219void kgd2kfd_device_exit(struct kfd_dev *kfd)
220{
b17f068a
OG
221 if (kfd->init_complete) {
222 amd_iommu_free_device(kfd->pdev);
223 kfd_topology_remove_device(kfd);
224 }
5b5c4e40 225
4a488a7a
OG
226 kfree(kfd);
227}
228
229void kgd2kfd_suspend(struct kfd_dev *kfd)
230{
231 BUG_ON(kfd == NULL);
b17f068a
OG
232
233 if (kfd->init_complete)
234 amd_iommu_free_device(kfd->pdev);
4a488a7a
OG
235}
236
237int kgd2kfd_resume(struct kfd_dev *kfd)
238{
b17f068a
OG
239 unsigned int pasid_limit;
240 int err;
241
4a488a7a
OG
242 BUG_ON(kfd == NULL);
243
b17f068a
OG
244 pasid_limit = kfd_get_pasid_limit();
245
246 if (kfd->init_complete) {
247 err = amd_iommu_init_device(kfd->pdev, pasid_limit);
248 if (err < 0)
249 return -ENXIO;
250 amd_iommu_set_invalidate_ctx_cb(kfd->pdev,
251 iommu_pasid_shutdown_callback);
252 }
253
4a488a7a
OG
254 return 0;
255}
256
257void kgd2kfd_interrupt(struct kfd_dev *dev, const void *ih_ring_entry)
258{
259}
This page took 0.039559 seconds and 5 git commands to generate.