2 * Copyright 2014 Advanced Micro Devices, 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.
24 #include <linux/slab.h>
25 #include <linux/mutex.h>
26 #include "kfd_device_queue_manager.h"
27 #include "kfd_kernel_queue.h"
29 #include "kfd_pm4_headers.h"
30 #include "kfd_pm4_headers_vi.h"
31 #include "kfd_pm4_opcodes.h"
33 static inline void inc_wptr(unsigned int *wptr, unsigned int increment_bytes,
34 unsigned int buffer_size_bytes)
36 unsigned int temp = *wptr + increment_bytes / sizeof(uint32_t);
38 BUG_ON((temp * sizeof(uint32_t)) > buffer_size_bytes);
42 static unsigned int build_pm4_header(unsigned int opcode, size_t packet_size)
44 union PM4_MES_TYPE_3_HEADER header;
47 header.opcode = opcode;
48 header.count = packet_size/sizeof(uint32_t) - 2;
49 header.type = PM4_TYPE_3;
54 static void pm_calc_rlib_size(struct packet_manager *pm,
55 unsigned int *rlib_size,
56 bool *over_subscription)
58 unsigned int process_count, queue_count;
59 unsigned int map_queue_size;
61 BUG_ON(!pm || !rlib_size || !over_subscription);
63 process_count = pm->dqm->processes_count;
64 queue_count = pm->dqm->queue_count;
66 /* check if there is over subscription*/
67 *over_subscription = false;
68 if ((process_count > 1) ||
69 queue_count > PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE) {
70 *over_subscription = true;
71 pr_debug("kfd: over subscribed runlist\n");
75 (pm->dqm->dev->device_info->asic_family == CHIP_CARRIZO) ?
76 sizeof(struct pm4_mes_map_queues) :
77 sizeof(struct pm4_map_queues);
78 /* calculate run list ib allocation size */
79 *rlib_size = process_count * sizeof(struct pm4_map_process) +
80 queue_count * map_queue_size;
83 * Increase the allocation size in case we need a chained run list
84 * when over subscription
86 if (*over_subscription)
87 *rlib_size += sizeof(struct pm4_runlist);
89 pr_debug("kfd: runlist ib size %d\n", *rlib_size);
92 static int pm_allocate_runlist_ib(struct packet_manager *pm,
93 unsigned int **rl_buffer,
94 uint64_t *rl_gpu_buffer,
95 unsigned int *rl_buffer_size,
96 bool *is_over_subscription)
101 BUG_ON(pm->allocated == true);
102 BUG_ON(is_over_subscription == NULL);
104 pm_calc_rlib_size(pm, rl_buffer_size, is_over_subscription);
106 retval = kfd_gtt_sa_allocate(pm->dqm->dev, *rl_buffer_size,
110 pr_err("kfd: failed to allocate runlist IB\n");
114 *(void **)rl_buffer = pm->ib_buffer_obj->cpu_ptr;
115 *rl_gpu_buffer = pm->ib_buffer_obj->gpu_addr;
117 memset(*rl_buffer, 0, *rl_buffer_size);
118 pm->allocated = true;
122 static int pm_create_runlist(struct packet_manager *pm, uint32_t *buffer,
123 uint64_t ib, size_t ib_size_in_dwords, bool chain)
125 struct pm4_runlist *packet;
127 BUG_ON(!pm || !buffer || !ib);
129 packet = (struct pm4_runlist *)buffer;
131 memset(buffer, 0, sizeof(struct pm4_runlist));
132 packet->header.u32all = build_pm4_header(IT_RUN_LIST,
133 sizeof(struct pm4_runlist));
135 packet->bitfields4.ib_size = ib_size_in_dwords;
136 packet->bitfields4.chain = chain ? 1 : 0;
137 packet->bitfields4.offload_polling = 0;
138 packet->bitfields4.valid = 1;
139 packet->ordinal2 = lower_32_bits(ib);
140 packet->bitfields3.ib_base_hi = upper_32_bits(ib);
145 static int pm_create_map_process(struct packet_manager *pm, uint32_t *buffer,
146 struct qcm_process_device *qpd)
148 struct pm4_map_process *packet;
152 BUG_ON(!pm || !buffer || !qpd);
154 packet = (struct pm4_map_process *)buffer;
156 pr_debug("kfd: In func %s\n", __func__);
158 memset(buffer, 0, sizeof(struct pm4_map_process));
160 packet->header.u32all = build_pm4_header(IT_MAP_PROCESS,
161 sizeof(struct pm4_map_process));
162 packet->bitfields2.diq_enable = (qpd->is_debug) ? 1 : 0;
163 packet->bitfields2.process_quantum = 1;
164 packet->bitfields2.pasid = qpd->pqm->process->pasid;
165 packet->bitfields3.page_table_base = qpd->page_table_base;
166 packet->bitfields10.gds_size = qpd->gds_size;
167 packet->bitfields10.num_gws = qpd->num_gws;
168 packet->bitfields10.num_oac = qpd->num_oac;
170 list_for_each_entry(cur, &qpd->queues_list, list)
172 packet->bitfields10.num_queues = (qpd->is_debug) ? 0 : num_queues;
174 packet->sh_mem_config = qpd->sh_mem_config;
175 packet->sh_mem_bases = qpd->sh_mem_bases;
176 packet->sh_mem_ape1_base = qpd->sh_mem_ape1_base;
177 packet->sh_mem_ape1_limit = qpd->sh_mem_ape1_limit;
179 packet->gds_addr_lo = lower_32_bits(qpd->gds_context_area);
180 packet->gds_addr_hi = upper_32_bits(qpd->gds_context_area);
185 static int pm_create_map_queue(struct packet_manager *pm, uint32_t *buffer,
186 struct queue *q, bool is_static)
188 struct pm4_map_queues *packet;
189 bool use_static = is_static;
191 BUG_ON(!pm || !buffer || !q);
193 pr_debug("kfd: In func %s\n", __func__);
195 packet = (struct pm4_map_queues *)buffer;
196 memset(buffer, 0, sizeof(struct pm4_map_queues));
198 packet->header.u32all = build_pm4_header(IT_MAP_QUEUES,
199 sizeof(struct pm4_map_queues));
200 packet->bitfields2.alloc_format =
201 alloc_format__mes_map_queues__one_per_pipe;
202 packet->bitfields2.num_queues = 1;
203 packet->bitfields2.queue_sel =
204 queue_sel__mes_map_queues__map_to_hws_determined_queue_slots;
206 packet->bitfields2.vidmem = (q->properties.is_interop) ?
207 vidmem__mes_map_queues__uses_video_memory :
208 vidmem__mes_map_queues__uses_no_video_memory;
210 switch (q->properties.type) {
211 case KFD_QUEUE_TYPE_COMPUTE:
212 case KFD_QUEUE_TYPE_DIQ:
213 packet->bitfields2.engine_sel =
214 engine_sel__mes_map_queues__compute;
216 case KFD_QUEUE_TYPE_SDMA:
217 packet->bitfields2.engine_sel =
218 engine_sel__mes_map_queues__sdma0;
219 use_static = false; /* no static queues under SDMA */
226 packet->mes_map_queues_ordinals[0].bitfields3.doorbell_offset =
227 q->properties.doorbell_off;
229 packet->mes_map_queues_ordinals[0].bitfields3.is_static =
230 (use_static == true) ? 1 : 0;
232 packet->mes_map_queues_ordinals[0].mqd_addr_lo =
233 lower_32_bits(q->gart_mqd_addr);
235 packet->mes_map_queues_ordinals[0].mqd_addr_hi =
236 upper_32_bits(q->gart_mqd_addr);
238 packet->mes_map_queues_ordinals[0].wptr_addr_lo =
239 lower_32_bits((uint64_t)q->properties.write_ptr);
241 packet->mes_map_queues_ordinals[0].wptr_addr_hi =
242 upper_32_bits((uint64_t)q->properties.write_ptr);
247 static int pm_create_runlist_ib(struct packet_manager *pm,
248 struct list_head *queues,
249 uint64_t *rl_gpu_addr,
250 size_t *rl_size_bytes)
252 unsigned int alloc_size_bytes;
253 unsigned int *rl_buffer, rl_wptr, i;
254 int retval, proccesses_mapped;
255 struct device_process_node *cur;
256 struct qcm_process_device *qpd;
258 struct kernel_queue *kq;
259 bool is_over_subscription;
261 BUG_ON(!pm || !queues || !rl_size_bytes || !rl_gpu_addr);
263 rl_wptr = retval = proccesses_mapped = 0;
265 retval = pm_allocate_runlist_ib(pm, &rl_buffer, rl_gpu_addr,
266 &alloc_size_bytes, &is_over_subscription);
270 *rl_size_bytes = alloc_size_bytes;
272 pr_debug("kfd: In func %s\n", __func__);
273 pr_debug("kfd: building runlist ib process count: %d queues count %d\n",
274 pm->dqm->processes_count, pm->dqm->queue_count);
276 /* build the run list ib packet */
277 list_for_each_entry(cur, queues, list) {
279 /* build map process packet */
280 if (proccesses_mapped >= pm->dqm->processes_count) {
281 pr_debug("kfd: not enough space left in runlist IB\n");
286 retval = pm_create_map_process(pm, &rl_buffer[rl_wptr], qpd);
291 inc_wptr(&rl_wptr, sizeof(struct pm4_map_process),
294 list_for_each_entry(kq, &qpd->priv_queue_list, list) {
295 if (kq->queue->properties.is_active != true)
298 pr_debug("kfd: static_queue, mapping kernel q %d, is debug status %d\n",
299 kq->queue->queue, qpd->is_debug);
301 retval = pm_create_map_queue(pm, &rl_buffer[rl_wptr],
302 kq->queue, qpd->is_debug);
307 sizeof(struct pm4_map_queues),
311 list_for_each_entry(q, &qpd->queues_list, list) {
312 if (q->properties.is_active != true)
315 pr_debug("kfd: static_queue, mapping user queue %d, is debug status %d\n",
316 q->queue, qpd->is_debug);
318 retval = pm_create_map_queue(pm, &rl_buffer[rl_wptr],
325 sizeof(struct pm4_map_queues),
330 pr_debug("kfd: finished map process and queues to runlist\n");
332 if (is_over_subscription)
333 pm_create_runlist(pm, &rl_buffer[rl_wptr], *rl_gpu_addr,
334 alloc_size_bytes / sizeof(uint32_t), true);
336 for (i = 0; i < alloc_size_bytes / sizeof(uint32_t); i++)
337 pr_debug("0x%2X ", rl_buffer[i]);
343 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm)
348 mutex_init(&pm->lock);
349 pm->priv_queue = kernel_queue_init(dqm->dev, KFD_QUEUE_TYPE_HIQ);
350 if (pm->priv_queue == NULL) {
351 mutex_destroy(&pm->lock);
354 pm->allocated = false;
359 void pm_uninit(struct packet_manager *pm)
363 mutex_destroy(&pm->lock);
364 kernel_queue_uninit(pm->priv_queue);
367 int pm_send_set_resources(struct packet_manager *pm,
368 struct scheduling_resources *res)
370 struct pm4_set_resources *packet;
374 pr_debug("kfd: In func %s\n", __func__);
376 mutex_lock(&pm->lock);
377 pm->priv_queue->ops.acquire_packet_buffer(pm->priv_queue,
378 sizeof(*packet) / sizeof(uint32_t),
379 (unsigned int **)&packet);
380 if (packet == NULL) {
381 mutex_unlock(&pm->lock);
382 pr_err("kfd: failed to allocate buffer on kernel queue\n");
386 memset(packet, 0, sizeof(struct pm4_set_resources));
387 packet->header.u32all = build_pm4_header(IT_SET_RESOURCES,
388 sizeof(struct pm4_set_resources));
390 packet->bitfields2.queue_type =
391 queue_type__mes_set_resources__hsa_interface_queue_hiq;
392 packet->bitfields2.vmid_mask = res->vmid_mask;
393 packet->bitfields2.unmap_latency = KFD_UNMAP_LATENCY;
394 packet->bitfields7.oac_mask = res->oac_mask;
395 packet->bitfields8.gds_heap_base = res->gds_heap_base;
396 packet->bitfields8.gds_heap_size = res->gds_heap_size;
398 packet->gws_mask_lo = lower_32_bits(res->gws_mask);
399 packet->gws_mask_hi = upper_32_bits(res->gws_mask);
401 packet->queue_mask_lo = lower_32_bits(res->queue_mask);
402 packet->queue_mask_hi = upper_32_bits(res->queue_mask);
404 pm->priv_queue->ops.submit_packet(pm->priv_queue);
406 mutex_unlock(&pm->lock);
411 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues)
413 uint64_t rl_gpu_ib_addr;
415 size_t rl_ib_size, packet_size_dwords;
418 BUG_ON(!pm || !dqm_queues);
420 retval = pm_create_runlist_ib(pm, dqm_queues, &rl_gpu_ib_addr,
423 goto fail_create_runlist_ib;
425 pr_debug("kfd: runlist IB address: 0x%llX\n", rl_gpu_ib_addr);
427 packet_size_dwords = sizeof(struct pm4_runlist) / sizeof(uint32_t);
428 mutex_lock(&pm->lock);
430 retval = pm->priv_queue->ops.acquire_packet_buffer(pm->priv_queue,
431 packet_size_dwords, &rl_buffer);
433 goto fail_acquire_packet_buffer;
435 retval = pm_create_runlist(pm, rl_buffer, rl_gpu_ib_addr,
436 rl_ib_size / sizeof(uint32_t), false);
438 goto fail_create_runlist;
440 pm->priv_queue->ops.submit_packet(pm->priv_queue);
442 mutex_unlock(&pm->lock);
447 pm->priv_queue->ops.rollback_packet(pm->priv_queue);
448 fail_acquire_packet_buffer:
449 mutex_unlock(&pm->lock);
450 fail_create_runlist_ib:
451 if (pm->allocated == true)
456 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
457 uint32_t fence_value)
460 struct pm4_query_status *packet;
462 BUG_ON(!pm || !fence_address);
464 mutex_lock(&pm->lock);
465 retval = pm->priv_queue->ops.acquire_packet_buffer(
467 sizeof(struct pm4_query_status) / sizeof(uint32_t),
468 (unsigned int **)&packet);
470 goto fail_acquire_packet_buffer;
472 packet->header.u32all = build_pm4_header(IT_QUERY_STATUS,
473 sizeof(struct pm4_query_status));
475 packet->bitfields2.context_id = 0;
476 packet->bitfields2.interrupt_sel =
477 interrupt_sel__mes_query_status__completion_status;
478 packet->bitfields2.command =
479 command__mes_query_status__fence_only_after_write_ack;
481 packet->addr_hi = upper_32_bits((uint64_t)fence_address);
482 packet->addr_lo = lower_32_bits((uint64_t)fence_address);
483 packet->data_hi = upper_32_bits((uint64_t)fence_value);
484 packet->data_lo = lower_32_bits((uint64_t)fence_value);
486 pm->priv_queue->ops.submit_packet(pm->priv_queue);
487 mutex_unlock(&pm->lock);
491 fail_acquire_packet_buffer:
492 mutex_unlock(&pm->lock);
496 int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
497 enum kfd_preempt_type_filter mode,
498 uint32_t filter_param, bool reset,
499 unsigned int sdma_engine)
503 struct pm4_unmap_queues *packet;
507 mutex_lock(&pm->lock);
508 retval = pm->priv_queue->ops.acquire_packet_buffer(
510 sizeof(struct pm4_unmap_queues) / sizeof(uint32_t),
513 goto err_acquire_packet_buffer;
515 packet = (struct pm4_unmap_queues *)buffer;
516 memset(buffer, 0, sizeof(struct pm4_unmap_queues));
517 pr_debug("kfd: static_queue: unmapping queues: mode is %d , reset is %d , type is %d\n",
519 packet->header.u32all = build_pm4_header(IT_UNMAP_QUEUES,
520 sizeof(struct pm4_unmap_queues));
522 case KFD_QUEUE_TYPE_COMPUTE:
523 case KFD_QUEUE_TYPE_DIQ:
524 packet->bitfields2.engine_sel =
525 engine_sel__mes_unmap_queues__compute;
527 case KFD_QUEUE_TYPE_SDMA:
528 packet->bitfields2.engine_sel =
529 engine_sel__mes_unmap_queues__sdma0 + sdma_engine;
537 packet->bitfields2.action =
538 action__mes_unmap_queues__reset_queues;
540 packet->bitfields2.action =
541 action__mes_unmap_queues__preempt_queues;
544 case KFD_PREEMPT_TYPE_FILTER_SINGLE_QUEUE:
545 packet->bitfields2.queue_sel =
546 queue_sel__mes_unmap_queues__perform_request_on_specified_queues;
547 packet->bitfields2.num_queues = 1;
548 packet->bitfields3b.doorbell_offset0 = filter_param;
550 case KFD_PREEMPT_TYPE_FILTER_BY_PASID:
551 packet->bitfields2.queue_sel =
552 queue_sel__mes_unmap_queues__perform_request_on_pasid_queues;
553 packet->bitfields3a.pasid = filter_param;
555 case KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES:
556 packet->bitfields2.queue_sel =
557 queue_sel__mes_unmap_queues__perform_request_on_all_active_queues;
559 case KFD_PREEMPT_TYPE_FILTER_DYNAMIC_QUEUES:
560 /* in this case, we do not preempt static queues */
561 packet->bitfields2.queue_sel =
562 queue_sel__mes_unmap_queues__perform_request_on_dynamic_queues_only;
569 pm->priv_queue->ops.submit_packet(pm->priv_queue);
571 mutex_unlock(&pm->lock);
574 err_acquire_packet_buffer:
575 mutex_unlock(&pm->lock);
579 void pm_release_ib(struct packet_manager *pm)
583 mutex_lock(&pm->lock);
585 kfd_gtt_sa_free(pm->dqm->dev, pm->ib_buffer_obj);
586 pm->allocated = false;
588 mutex_unlock(&pm->lock);