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_opcodes.h"
32 static inline void inc_wptr(unsigned int *wptr, unsigned int increment_bytes,
33 unsigned int buffer_size_bytes)
35 unsigned int temp = *wptr + increment_bytes / sizeof(uint32_t);
37 BUG_ON((temp * sizeof(uint32_t)) > buffer_size_bytes);
41 static unsigned int build_pm4_header(unsigned int opcode, size_t packet_size)
43 union PM4_MES_TYPE_3_HEADER header;
46 header.opcode = opcode;
47 header.count = packet_size/sizeof(uint32_t) - 2;
48 header.type = PM4_TYPE_3;
53 static void pm_calc_rlib_size(struct packet_manager *pm,
54 unsigned int *rlib_size,
55 bool *over_subscription)
57 unsigned int process_count, queue_count;
59 BUG_ON(!pm || !rlib_size || !over_subscription);
61 process_count = pm->dqm->processes_count;
62 queue_count = pm->dqm->queue_count;
64 /* check if there is over subscription*/
65 *over_subscription = false;
66 if ((process_count > 1) ||
67 queue_count > PIPE_PER_ME_CP_SCHEDULING * QUEUES_PER_PIPE) {
68 *over_subscription = true;
69 pr_debug("kfd: over subscribed runlist\n");
72 /* calculate run list ib allocation size */
73 *rlib_size = process_count * sizeof(struct pm4_map_process) +
74 queue_count * sizeof(struct pm4_map_queues);
77 * Increase the allocation size in case we need a chained run list
78 * when over subscription
80 if (*over_subscription)
81 *rlib_size += sizeof(struct pm4_runlist);
83 pr_debug("kfd: runlist ib size %d\n", *rlib_size);
86 static int pm_allocate_runlist_ib(struct packet_manager *pm,
87 unsigned int **rl_buffer,
88 uint64_t *rl_gpu_buffer,
89 unsigned int *rl_buffer_size,
90 bool *is_over_subscription)
95 BUG_ON(pm->allocated == true);
96 BUG_ON(is_over_subscription == NULL);
98 pm_calc_rlib_size(pm, rl_buffer_size, is_over_subscription);
100 retval = kfd_gtt_sa_allocate(pm->dqm->dev, *rl_buffer_size,
104 pr_err("kfd: failed to allocate runlist IB\n");
108 *(void **)rl_buffer = pm->ib_buffer_obj->cpu_ptr;
109 *rl_gpu_buffer = pm->ib_buffer_obj->gpu_addr;
111 memset(*rl_buffer, 0, *rl_buffer_size);
112 pm->allocated = true;
116 static int pm_create_runlist(struct packet_manager *pm, uint32_t *buffer,
117 uint64_t ib, size_t ib_size_in_dwords, bool chain)
119 struct pm4_runlist *packet;
121 BUG_ON(!pm || !buffer || !ib);
123 packet = (struct pm4_runlist *)buffer;
125 memset(buffer, 0, sizeof(struct pm4_runlist));
126 packet->header.u32all = build_pm4_header(IT_RUN_LIST,
127 sizeof(struct pm4_runlist));
129 packet->bitfields4.ib_size = ib_size_in_dwords;
130 packet->bitfields4.chain = chain ? 1 : 0;
131 packet->bitfields4.offload_polling = 0;
132 packet->bitfields4.valid = 1;
133 packet->ordinal2 = lower_32_bits(ib);
134 packet->bitfields3.ib_base_hi = upper_32_bits(ib);
139 static int pm_create_map_process(struct packet_manager *pm, uint32_t *buffer,
140 struct qcm_process_device *qpd)
142 struct pm4_map_process *packet;
146 BUG_ON(!pm || !buffer || !qpd);
148 packet = (struct pm4_map_process *)buffer;
150 pr_debug("kfd: In func %s\n", __func__);
152 memset(buffer, 0, sizeof(struct pm4_map_process));
154 packet->header.u32all = build_pm4_header(IT_MAP_PROCESS,
155 sizeof(struct pm4_map_process));
156 packet->bitfields2.diq_enable = (qpd->is_debug) ? 1 : 0;
157 packet->bitfields2.process_quantum = 1;
158 packet->bitfields2.pasid = qpd->pqm->process->pasid;
159 packet->bitfields3.page_table_base = qpd->page_table_base;
160 packet->bitfields10.gds_size = qpd->gds_size;
161 packet->bitfields10.num_gws = qpd->num_gws;
162 packet->bitfields10.num_oac = qpd->num_oac;
164 list_for_each_entry(cur, &qpd->queues_list, list)
166 packet->bitfields10.num_queues = num_queues;
168 packet->sh_mem_config = qpd->sh_mem_config;
169 packet->sh_mem_bases = qpd->sh_mem_bases;
170 packet->sh_mem_ape1_base = qpd->sh_mem_ape1_base;
171 packet->sh_mem_ape1_limit = qpd->sh_mem_ape1_limit;
173 packet->gds_addr_lo = lower_32_bits(qpd->gds_context_area);
174 packet->gds_addr_hi = upper_32_bits(qpd->gds_context_area);
179 static int pm_create_map_queue(struct packet_manager *pm, uint32_t *buffer,
182 struct pm4_map_queues *packet;
184 BUG_ON(!pm || !buffer || !q);
186 pr_debug("kfd: In func %s\n", __func__);
188 packet = (struct pm4_map_queues *)buffer;
189 memset(buffer, 0, sizeof(struct pm4_map_queues));
191 packet->header.u32all = build_pm4_header(IT_MAP_QUEUES,
192 sizeof(struct pm4_map_queues));
193 packet->bitfields2.alloc_format =
194 alloc_format__mes_map_queues__one_per_pipe;
195 packet->bitfields2.num_queues = 1;
196 packet->bitfields2.queue_sel =
197 queue_sel__mes_map_queues__map_to_hws_determined_queue_slots;
199 packet->bitfields2.vidmem = (q->properties.is_interop) ?
200 vidmem__mes_map_queues__uses_video_memory :
201 vidmem__mes_map_queues__uses_no_video_memory;
203 switch (q->properties.type) {
204 case KFD_QUEUE_TYPE_COMPUTE:
205 case KFD_QUEUE_TYPE_DIQ:
206 packet->bitfields2.engine_sel =
207 engine_sel__mes_map_queues__compute;
209 case KFD_QUEUE_TYPE_SDMA:
210 packet->bitfields2.engine_sel =
211 engine_sel__mes_map_queues__sdma0;
218 packet->mes_map_queues_ordinals[0].bitfields3.doorbell_offset =
219 q->properties.doorbell_off;
221 packet->mes_map_queues_ordinals[0].mqd_addr_lo =
222 lower_32_bits(q->gart_mqd_addr);
224 packet->mes_map_queues_ordinals[0].mqd_addr_hi =
225 upper_32_bits(q->gart_mqd_addr);
227 packet->mes_map_queues_ordinals[0].wptr_addr_lo =
228 lower_32_bits((uint64_t)q->properties.write_ptr);
230 packet->mes_map_queues_ordinals[0].wptr_addr_hi =
231 upper_32_bits((uint64_t)q->properties.write_ptr);
236 static int pm_create_runlist_ib(struct packet_manager *pm,
237 struct list_head *queues,
238 uint64_t *rl_gpu_addr,
239 size_t *rl_size_bytes)
241 unsigned int alloc_size_bytes;
242 unsigned int *rl_buffer, rl_wptr, i;
243 int retval, proccesses_mapped;
244 struct device_process_node *cur;
245 struct qcm_process_device *qpd;
247 struct kernel_queue *kq;
248 bool is_over_subscription;
250 BUG_ON(!pm || !queues || !rl_size_bytes || !rl_gpu_addr);
252 rl_wptr = retval = proccesses_mapped = 0;
254 retval = pm_allocate_runlist_ib(pm, &rl_buffer, rl_gpu_addr,
255 &alloc_size_bytes, &is_over_subscription);
259 *rl_size_bytes = alloc_size_bytes;
261 pr_debug("kfd: In func %s\n", __func__);
262 pr_debug("kfd: building runlist ib process count: %d queues count %d\n",
263 pm->dqm->processes_count, pm->dqm->queue_count);
265 /* build the run list ib packet */
266 list_for_each_entry(cur, queues, list) {
268 /* build map process packet */
269 if (proccesses_mapped >= pm->dqm->processes_count) {
270 pr_debug("kfd: not enough space left in runlist IB\n");
274 retval = pm_create_map_process(pm, &rl_buffer[rl_wptr], qpd);
278 inc_wptr(&rl_wptr, sizeof(struct pm4_map_process),
281 list_for_each_entry(kq, &qpd->priv_queue_list, list) {
282 if (kq->queue->properties.is_active != true)
284 retval = pm_create_map_queue(pm, &rl_buffer[rl_wptr],
288 inc_wptr(&rl_wptr, sizeof(struct pm4_map_queues),
292 list_for_each_entry(q, &qpd->queues_list, list) {
293 if (q->properties.is_active != true)
295 retval = pm_create_map_queue(pm,
296 &rl_buffer[rl_wptr], q);
299 inc_wptr(&rl_wptr, sizeof(struct pm4_map_queues),
304 pr_debug("kfd: finished map process and queues to runlist\n");
306 if (is_over_subscription)
307 pm_create_runlist(pm, &rl_buffer[rl_wptr], *rl_gpu_addr,
308 alloc_size_bytes / sizeof(uint32_t), true);
310 for (i = 0; i < alloc_size_bytes / sizeof(uint32_t); i++)
311 pr_debug("0x%2X ", rl_buffer[i]);
317 int pm_init(struct packet_manager *pm, struct device_queue_manager *dqm)
322 mutex_init(&pm->lock);
323 pm->priv_queue = kernel_queue_init(dqm->dev, KFD_QUEUE_TYPE_HIQ);
324 if (pm->priv_queue == NULL) {
325 mutex_destroy(&pm->lock);
328 pm->allocated = false;
333 void pm_uninit(struct packet_manager *pm)
337 mutex_destroy(&pm->lock);
338 kernel_queue_uninit(pm->priv_queue);
341 int pm_send_set_resources(struct packet_manager *pm,
342 struct scheduling_resources *res)
344 struct pm4_set_resources *packet;
348 pr_debug("kfd: In func %s\n", __func__);
350 mutex_lock(&pm->lock);
351 pm->priv_queue->ops.acquire_packet_buffer(pm->priv_queue,
352 sizeof(*packet) / sizeof(uint32_t),
353 (unsigned int **)&packet);
354 if (packet == NULL) {
355 mutex_unlock(&pm->lock);
356 pr_err("kfd: failed to allocate buffer on kernel queue\n");
360 memset(packet, 0, sizeof(struct pm4_set_resources));
361 packet->header.u32all = build_pm4_header(IT_SET_RESOURCES,
362 sizeof(struct pm4_set_resources));
364 packet->bitfields2.queue_type =
365 queue_type__mes_set_resources__hsa_interface_queue_hiq;
366 packet->bitfields2.vmid_mask = res->vmid_mask;
367 packet->bitfields2.unmap_latency = KFD_UNMAP_LATENCY;
368 packet->bitfields7.oac_mask = res->oac_mask;
369 packet->bitfields8.gds_heap_base = res->gds_heap_base;
370 packet->bitfields8.gds_heap_size = res->gds_heap_size;
372 packet->gws_mask_lo = lower_32_bits(res->gws_mask);
373 packet->gws_mask_hi = upper_32_bits(res->gws_mask);
375 packet->queue_mask_lo = lower_32_bits(res->queue_mask);
376 packet->queue_mask_hi = upper_32_bits(res->queue_mask);
378 pm->priv_queue->ops.submit_packet(pm->priv_queue);
380 mutex_unlock(&pm->lock);
385 int pm_send_runlist(struct packet_manager *pm, struct list_head *dqm_queues)
387 uint64_t rl_gpu_ib_addr;
389 size_t rl_ib_size, packet_size_dwords;
392 BUG_ON(!pm || !dqm_queues);
394 retval = pm_create_runlist_ib(pm, dqm_queues, &rl_gpu_ib_addr,
397 goto fail_create_runlist_ib;
399 pr_debug("kfd: runlist IB address: 0x%llX\n", rl_gpu_ib_addr);
401 packet_size_dwords = sizeof(struct pm4_runlist) / sizeof(uint32_t);
402 mutex_lock(&pm->lock);
404 retval = pm->priv_queue->ops.acquire_packet_buffer(pm->priv_queue,
405 packet_size_dwords, &rl_buffer);
407 goto fail_acquire_packet_buffer;
409 retval = pm_create_runlist(pm, rl_buffer, rl_gpu_ib_addr,
410 rl_ib_size / sizeof(uint32_t), false);
412 goto fail_create_runlist;
414 pm->priv_queue->ops.submit_packet(pm->priv_queue);
416 mutex_unlock(&pm->lock);
421 pm->priv_queue->ops.rollback_packet(pm->priv_queue);
422 fail_acquire_packet_buffer:
423 mutex_unlock(&pm->lock);
424 fail_create_runlist_ib:
425 if (pm->allocated == true)
430 int pm_send_query_status(struct packet_manager *pm, uint64_t fence_address,
431 uint32_t fence_value)
434 struct pm4_query_status *packet;
436 BUG_ON(!pm || !fence_address);
438 mutex_lock(&pm->lock);
439 retval = pm->priv_queue->ops.acquire_packet_buffer(
441 sizeof(struct pm4_query_status) / sizeof(uint32_t),
442 (unsigned int **)&packet);
444 goto fail_acquire_packet_buffer;
446 packet->header.u32all = build_pm4_header(IT_QUERY_STATUS,
447 sizeof(struct pm4_query_status));
449 packet->bitfields2.context_id = 0;
450 packet->bitfields2.interrupt_sel =
451 interrupt_sel__mes_query_status__completion_status;
452 packet->bitfields2.command =
453 command__mes_query_status__fence_only_after_write_ack;
455 packet->addr_hi = upper_32_bits((uint64_t)fence_address);
456 packet->addr_lo = lower_32_bits((uint64_t)fence_address);
457 packet->data_hi = upper_32_bits((uint64_t)fence_value);
458 packet->data_lo = lower_32_bits((uint64_t)fence_value);
460 pm->priv_queue->ops.submit_packet(pm->priv_queue);
461 mutex_unlock(&pm->lock);
465 fail_acquire_packet_buffer:
466 mutex_unlock(&pm->lock);
470 int pm_send_unmap_queue(struct packet_manager *pm, enum kfd_queue_type type,
471 enum kfd_preempt_type_filter mode,
472 uint32_t filter_param, bool reset,
473 unsigned int sdma_engine)
477 struct pm4_unmap_queues *packet;
481 mutex_lock(&pm->lock);
482 retval = pm->priv_queue->ops.acquire_packet_buffer(
484 sizeof(struct pm4_unmap_queues) / sizeof(uint32_t),
487 goto err_acquire_packet_buffer;
489 packet = (struct pm4_unmap_queues *)buffer;
490 memset(buffer, 0, sizeof(struct pm4_unmap_queues));
492 packet->header.u32all = build_pm4_header(IT_UNMAP_QUEUES,
493 sizeof(struct pm4_unmap_queues));
495 case KFD_QUEUE_TYPE_COMPUTE:
496 case KFD_QUEUE_TYPE_DIQ:
497 packet->bitfields2.engine_sel =
498 engine_sel__mes_unmap_queues__compute;
500 case KFD_QUEUE_TYPE_SDMA:
501 packet->bitfields2.engine_sel =
502 engine_sel__mes_unmap_queues__sdma0 + sdma_engine;
510 packet->bitfields2.action =
511 action__mes_unmap_queues__reset_queues;
513 packet->bitfields2.action =
514 action__mes_unmap_queues__preempt_queues;
517 case KFD_PREEMPT_TYPE_FILTER_SINGLE_QUEUE:
518 packet->bitfields2.queue_sel =
519 queue_sel__mes_unmap_queues__perform_request_on_specified_queues;
520 packet->bitfields2.num_queues = 1;
521 packet->bitfields3b.doorbell_offset0 = filter_param;
523 case KFD_PREEMPT_TYPE_FILTER_BY_PASID:
524 packet->bitfields2.queue_sel =
525 queue_sel__mes_unmap_queues__perform_request_on_pasid_queues;
526 packet->bitfields3a.pasid = filter_param;
528 case KFD_PREEMPT_TYPE_FILTER_ALL_QUEUES:
529 packet->bitfields2.queue_sel =
530 queue_sel__mes_unmap_queues__perform_request_on_all_active_queues;
537 pm->priv_queue->ops.submit_packet(pm->priv_queue);
539 mutex_unlock(&pm->lock);
542 err_acquire_packet_buffer:
543 mutex_unlock(&pm->lock);
547 void pm_release_ib(struct packet_manager *pm)
551 mutex_lock(&pm->lock);
553 kfd_gtt_sa_free(pm->dqm->dev, pm->ib_buffer_obj);
554 pm->allocated = false;
556 mutex_unlock(&pm->lock);