Commit | Line | Data |
---|---|---|
ed6e6a34 BG |
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 | ||
24 | #include <linux/types.h> | |
25 | #include <linux/mutex.h> | |
26 | #include <linux/slab.h> | |
27 | #include <linux/printk.h> | |
9a5634a7 | 28 | #include <linux/sched.h> |
ed6e6a34 BG |
29 | #include "kfd_kernel_queue.h" |
30 | #include "kfd_priv.h" | |
31 | #include "kfd_device_queue_manager.h" | |
32 | #include "kfd_pm4_headers.h" | |
33 | #include "kfd_pm4_opcodes.h" | |
34 | ||
35 | #define PM4_COUNT_ZERO (((1 << 15) - 1) << 16) | |
36 | ||
37 | static bool initialize(struct kernel_queue *kq, struct kfd_dev *dev, | |
38 | enum kfd_queue_type type, unsigned int queue_size) | |
39 | { | |
40 | struct queue_properties prop; | |
41 | int retval; | |
42 | union PM4_MES_TYPE_3_HEADER nop; | |
43 | ||
44 | BUG_ON(!kq || !dev); | |
45 | BUG_ON(type != KFD_QUEUE_TYPE_DIQ && type != KFD_QUEUE_TYPE_HIQ); | |
46 | ||
aaad2d8c | 47 | pr_debug("amdkfd: In func %s initializing queue type %d size %d\n", |
ed6e6a34 BG |
48 | __func__, KFD_QUEUE_TYPE_HIQ, queue_size); |
49 | ||
50 | nop.opcode = IT_NOP; | |
51 | nop.type = PM4_TYPE_3; | |
52 | nop.u32all |= PM4_COUNT_ZERO; | |
53 | ||
54 | kq->dev = dev; | |
55 | kq->nop_packet = nop.u32all; | |
56 | switch (type) { | |
57 | case KFD_QUEUE_TYPE_DIQ: | |
58 | case KFD_QUEUE_TYPE_HIQ: | |
45c9a5e4 | 59 | kq->mqd = dev->dqm->ops.get_mqd_manager(dev->dqm, |
85d258f9 | 60 | KFD_MQD_TYPE_HIQ); |
ed6e6a34 BG |
61 | break; |
62 | default: | |
63 | BUG(); | |
64 | break; | |
65 | } | |
66 | ||
67 | if (kq->mqd == NULL) | |
68 | return false; | |
69 | ||
5cd78de5 | 70 | prop.doorbell_ptr = kfd_get_kernel_doorbell(dev, &prop.doorbell_off); |
ed6e6a34 | 71 | |
aaad2d8c BG |
72 | if (prop.doorbell_ptr == NULL) { |
73 | pr_err("amdkfd: error init doorbell"); | |
ed6e6a34 | 74 | goto err_get_kernel_doorbell; |
aaad2d8c | 75 | } |
ed6e6a34 | 76 | |
a86aa3ca | 77 | retval = kfd_gtt_sa_allocate(dev, queue_size, &kq->pq); |
aaad2d8c BG |
78 | if (retval != 0) { |
79 | pr_err("amdkfd: error init pq queues size (%d)\n", queue_size); | |
ed6e6a34 | 80 | goto err_pq_allocate_vidmem; |
aaad2d8c | 81 | } |
ed6e6a34 BG |
82 | |
83 | kq->pq_kernel_addr = kq->pq->cpu_ptr; | |
84 | kq->pq_gpu_addr = kq->pq->gpu_addr; | |
85 | ||
6898f0a5 BG |
86 | retval = kq->ops_asic_specific.initialize(kq, dev, type, queue_size); |
87 | if (retval == false) | |
88 | goto err_eop_allocate_vidmem; | |
89 | ||
a86aa3ca OG |
90 | retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->rptr_kernel), |
91 | &kq->rptr_mem); | |
ed6e6a34 BG |
92 | |
93 | if (retval != 0) | |
94 | goto err_rptr_allocate_vidmem; | |
95 | ||
96 | kq->rptr_kernel = kq->rptr_mem->cpu_ptr; | |
97 | kq->rptr_gpu_addr = kq->rptr_mem->gpu_addr; | |
98 | ||
a86aa3ca OG |
99 | retval = kfd_gtt_sa_allocate(dev, sizeof(*kq->wptr_kernel), |
100 | &kq->wptr_mem); | |
ed6e6a34 BG |
101 | |
102 | if (retval != 0) | |
103 | goto err_wptr_allocate_vidmem; | |
104 | ||
105 | kq->wptr_kernel = kq->wptr_mem->cpu_ptr; | |
106 | kq->wptr_gpu_addr = kq->wptr_mem->gpu_addr; | |
107 | ||
108 | memset(kq->pq_kernel_addr, 0, queue_size); | |
109 | memset(kq->rptr_kernel, 0, sizeof(*kq->rptr_kernel)); | |
110 | memset(kq->wptr_kernel, 0, sizeof(*kq->wptr_kernel)); | |
111 | ||
112 | prop.queue_size = queue_size; | |
113 | prop.is_interop = false; | |
114 | prop.priority = 1; | |
115 | prop.queue_percent = 100; | |
116 | prop.type = type; | |
117 | prop.vmid = 0; | |
118 | prop.queue_address = kq->pq_gpu_addr; | |
119 | prop.read_ptr = (uint32_t *) kq->rptr_gpu_addr; | |
120 | prop.write_ptr = (uint32_t *) kq->wptr_gpu_addr; | |
6898f0a5 BG |
121 | prop.eop_ring_buffer_address = kq->eop_gpu_addr; |
122 | prop.eop_ring_buffer_size = PAGE_SIZE; | |
ed6e6a34 BG |
123 | |
124 | if (init_queue(&kq->queue, prop) != 0) | |
125 | goto err_init_queue; | |
126 | ||
127 | kq->queue->device = dev; | |
128 | kq->queue->process = kfd_get_process(current); | |
129 | ||
130 | retval = kq->mqd->init_mqd(kq->mqd, &kq->queue->mqd, | |
131 | &kq->queue->mqd_mem_obj, | |
132 | &kq->queue->gart_mqd_addr, | |
133 | &kq->queue->properties); | |
134 | if (retval != 0) | |
135 | goto err_init_mqd; | |
136 | ||
137 | /* assign HIQ to HQD */ | |
138 | if (type == KFD_QUEUE_TYPE_HIQ) { | |
139 | pr_debug("assigning hiq to hqd\n"); | |
140 | kq->queue->pipe = KFD_CIK_HIQ_PIPE; | |
141 | kq->queue->queue = KFD_CIK_HIQ_QUEUE; | |
142 | kq->mqd->load_mqd(kq->mqd, kq->queue->mqd, kq->queue->pipe, | |
143 | kq->queue->queue, NULL); | |
144 | } else { | |
145 | /* allocate fence for DIQ */ | |
146 | ||
a86aa3ca OG |
147 | retval = kfd_gtt_sa_allocate(dev, sizeof(uint32_t), |
148 | &kq->fence_mem_obj); | |
ed6e6a34 BG |
149 | |
150 | if (retval != 0) | |
151 | goto err_alloc_fence; | |
152 | ||
153 | kq->fence_kernel_address = kq->fence_mem_obj->cpu_ptr; | |
154 | kq->fence_gpu_addr = kq->fence_mem_obj->gpu_addr; | |
155 | } | |
156 | ||
157 | print_queue(kq->queue); | |
158 | ||
159 | return true; | |
160 | err_alloc_fence: | |
161 | err_init_mqd: | |
162 | uninit_queue(kq->queue); | |
163 | err_init_queue: | |
a86aa3ca | 164 | kfd_gtt_sa_free(dev, kq->wptr_mem); |
ed6e6a34 | 165 | err_wptr_allocate_vidmem: |
a86aa3ca | 166 | kfd_gtt_sa_free(dev, kq->rptr_mem); |
ed6e6a34 | 167 | err_rptr_allocate_vidmem: |
6898f0a5 BG |
168 | kfd_gtt_sa_free(dev, kq->eop_mem); |
169 | err_eop_allocate_vidmem: | |
a86aa3ca | 170 | kfd_gtt_sa_free(dev, kq->pq); |
ed6e6a34 | 171 | err_pq_allocate_vidmem: |
5cd78de5 | 172 | kfd_release_kernel_doorbell(dev, prop.doorbell_ptr); |
ed6e6a34 | 173 | err_get_kernel_doorbell: |
ed6e6a34 BG |
174 | return false; |
175 | ||
176 | } | |
177 | ||
178 | static void uninitialize(struct kernel_queue *kq) | |
179 | { | |
180 | BUG_ON(!kq); | |
181 | ||
182 | if (kq->queue->properties.type == KFD_QUEUE_TYPE_HIQ) | |
183 | kq->mqd->destroy_mqd(kq->mqd, | |
184 | NULL, | |
185 | false, | |
186 | QUEUE_PREEMPT_DEFAULT_TIMEOUT_MS, | |
187 | kq->queue->pipe, | |
188 | kq->queue->queue); | |
a86aa3ca OG |
189 | else if (kq->queue->properties.type == KFD_QUEUE_TYPE_DIQ) |
190 | kfd_gtt_sa_free(kq->dev, kq->fence_mem_obj); | |
ed6e6a34 | 191 | |
aaad2d8c BG |
192 | kq->mqd->uninit_mqd(kq->mqd, kq->queue->mqd, kq->queue->mqd_mem_obj); |
193 | ||
a86aa3ca OG |
194 | kfd_gtt_sa_free(kq->dev, kq->rptr_mem); |
195 | kfd_gtt_sa_free(kq->dev, kq->wptr_mem); | |
6898f0a5 | 196 | kq->ops_asic_specific.uninitialize(kq); |
a86aa3ca | 197 | kfd_gtt_sa_free(kq->dev, kq->pq); |
ed6e6a34 | 198 | kfd_release_kernel_doorbell(kq->dev, |
5cd78de5 | 199 | kq->queue->properties.doorbell_ptr); |
ed6e6a34 BG |
200 | uninit_queue(kq->queue); |
201 | } | |
202 | ||
203 | static int acquire_packet_buffer(struct kernel_queue *kq, | |
204 | size_t packet_size_in_dwords, unsigned int **buffer_ptr) | |
205 | { | |
206 | size_t available_size; | |
207 | size_t queue_size_dwords; | |
208 | uint32_t wptr, rptr; | |
209 | unsigned int *queue_address; | |
210 | ||
211 | BUG_ON(!kq || !buffer_ptr); | |
212 | ||
213 | rptr = *kq->rptr_kernel; | |
214 | wptr = *kq->wptr_kernel; | |
215 | queue_address = (unsigned int *)kq->pq_kernel_addr; | |
216 | queue_size_dwords = kq->queue->properties.queue_size / sizeof(uint32_t); | |
217 | ||
8856d8e0 OG |
218 | pr_debug("rptr: %d\n", rptr); |
219 | pr_debug("wptr: %d\n", wptr); | |
220 | pr_debug("queue_address 0x%p\n", queue_address); | |
ed6e6a34 BG |
221 | |
222 | available_size = (rptr - 1 - wptr + queue_size_dwords) % | |
223 | queue_size_dwords; | |
224 | ||
225 | if (packet_size_in_dwords >= queue_size_dwords || | |
a550bb3d OG |
226 | packet_size_in_dwords >= available_size) { |
227 | /* | |
228 | * make sure calling functions know | |
229 | * acquire_packet_buffer() failed | |
230 | */ | |
231 | *buffer_ptr = NULL; | |
ed6e6a34 | 232 | return -ENOMEM; |
a550bb3d | 233 | } |
ed6e6a34 BG |
234 | |
235 | if (wptr + packet_size_in_dwords >= queue_size_dwords) { | |
236 | while (wptr > 0) { | |
237 | queue_address[wptr] = kq->nop_packet; | |
238 | wptr = (wptr + 1) % queue_size_dwords; | |
239 | } | |
240 | } | |
241 | ||
242 | *buffer_ptr = &queue_address[wptr]; | |
243 | kq->pending_wptr = wptr + packet_size_in_dwords; | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
248 | static void submit_packet(struct kernel_queue *kq) | |
249 | { | |
250 | #ifdef DEBUG | |
251 | int i; | |
252 | #endif | |
253 | ||
254 | BUG_ON(!kq); | |
255 | ||
256 | #ifdef DEBUG | |
257 | for (i = *kq->wptr_kernel; i < kq->pending_wptr; i++) { | |
258 | pr_debug("0x%2X ", kq->pq_kernel_addr[i]); | |
259 | if (i % 15 == 0) | |
260 | pr_debug("\n"); | |
261 | } | |
262 | pr_debug("\n"); | |
263 | #endif | |
264 | ||
265 | *kq->wptr_kernel = kq->pending_wptr; | |
5cd78de5 | 266 | write_kernel_doorbell(kq->queue->properties.doorbell_ptr, |
ed6e6a34 BG |
267 | kq->pending_wptr); |
268 | } | |
269 | ||
ed6e6a34 BG |
270 | static void rollback_packet(struct kernel_queue *kq) |
271 | { | |
272 | BUG_ON(!kq); | |
273 | kq->pending_wptr = *kq->queue->properties.write_ptr; | |
274 | } | |
275 | ||
276 | struct kernel_queue *kernel_queue_init(struct kfd_dev *dev, | |
277 | enum kfd_queue_type type) | |
278 | { | |
279 | struct kernel_queue *kq; | |
280 | ||
281 | BUG_ON(!dev); | |
282 | ||
283 | kq = kzalloc(sizeof(struct kernel_queue), GFP_KERNEL); | |
284 | if (!kq) | |
285 | return NULL; | |
286 | ||
443fbd5f OG |
287 | kq->ops.initialize = initialize; |
288 | kq->ops.uninitialize = uninitialize; | |
289 | kq->ops.acquire_packet_buffer = acquire_packet_buffer; | |
290 | kq->ops.submit_packet = submit_packet; | |
443fbd5f OG |
291 | kq->ops.rollback_packet = rollback_packet; |
292 | ||
6898f0a5 BG |
293 | switch (dev->device_info->asic_family) { |
294 | case CHIP_CARRIZO: | |
295 | kernel_queue_init_vi(&kq->ops_asic_specific); | |
300dec95 OG |
296 | break; |
297 | ||
6898f0a5 BG |
298 | case CHIP_KAVERI: |
299 | kernel_queue_init_cik(&kq->ops_asic_specific); | |
300dec95 | 300 | break; |
6898f0a5 BG |
301 | } |
302 | ||
443fbd5f | 303 | if (kq->ops.initialize(kq, dev, type, KFD_KERNEL_QUEUE_SIZE) == false) { |
aaad2d8c | 304 | pr_err("amdkfd: failed to init kernel queue\n"); |
ed6e6a34 BG |
305 | kfree(kq); |
306 | return NULL; | |
307 | } | |
308 | return kq; | |
309 | } | |
310 | ||
311 | void kernel_queue_uninit(struct kernel_queue *kq) | |
312 | { | |
313 | BUG_ON(!kq); | |
314 | ||
443fbd5f | 315 | kq->ops.uninitialize(kq); |
ed6e6a34 BG |
316 | kfree(kq); |
317 | } | |
318 | ||
5ef360ea | 319 | static __attribute__((unused)) void test_kq(struct kfd_dev *dev) |
ed6e6a34 BG |
320 | { |
321 | struct kernel_queue *kq; | |
322 | uint32_t *buffer, i; | |
323 | int retval; | |
324 | ||
325 | BUG_ON(!dev); | |
326 | ||
aaad2d8c | 327 | pr_err("amdkfd: starting kernel queue test\n"); |
ed6e6a34 BG |
328 | |
329 | kq = kernel_queue_init(dev, KFD_QUEUE_TYPE_HIQ); | |
330 | BUG_ON(!kq); | |
331 | ||
443fbd5f | 332 | retval = kq->ops.acquire_packet_buffer(kq, 5, &buffer); |
ed6e6a34 BG |
333 | BUG_ON(retval != 0); |
334 | for (i = 0; i < 5; i++) | |
335 | buffer[i] = kq->nop_packet; | |
443fbd5f | 336 | kq->ops.submit_packet(kq); |
ed6e6a34 | 337 | |
aaad2d8c | 338 | pr_err("amdkfd: ending kernel queue test\n"); |
ed6e6a34 BG |
339 | } |
340 | ||
341 |