// SPDX-License-Identifier: GPL-2.0-or-later /* * SN Platform GRU Driver * * KERNEL SERVICES THAT USE THE GRU * * Copyright (c) 2008 Silicon Graphics, Inc. All Rights Reserved.
*/
/* * Kernel GRU Usage * * The following is an interim algorithm for management of kernel GRU * resources. This will likely be replaced when we better understand the * kernel/user requirements. * * Blade percpu resources reserved for kernel use. These resources are * reserved whenever the kernel context for the blade is loaded. Note * that the kernel context is not guaranteed to be always available. It is * loaded on demand & can be stolen by a user if the user demand exceeds the * kernel demand. The kernel can always reload the kernel context but * a SLEEP may be required!!!. * * Async Overview: * * Each blade has one "kernel context" that owns GRU kernel resources * located on the blade. Kernel drivers use GRU resources in this context * for sending messages, zeroing memory, etc. * * The kernel context is dynamically loaded on demand. If it is not in * use by the kernel, the kernel context can be unloaded & given to a user. * The kernel context will be reloaded when needed. This may require that * a context be stolen from a user. * NOTE: frequent unloading/reloading of the kernel context is * expensive. We are depending on batch schedulers, cpusets, sane * drivers or some other mechanism to prevent the need for frequent * stealing/reloading. * * The kernel context consists of two parts: * - 1 CB & a few DSRs that are reserved for each cpu on the blade. * Each cpu has it's own private resources & does not share them * with other cpus. These resources are used serially, ie, * locked, used & unlocked on each call to a function in * grukservices. * (Now that we have dynamic loading of kernel contexts, I * may rethink this & allow sharing between cpus....) * * - Additional resources can be reserved long term & used directly * by UV drivers located in the kernel. Drivers using these GRU * resources can use asynchronous GRU instructions that send * interrupts on completion. * - these resources must be explicitly locked/unlocked * - locked resources prevent (obviously) the kernel * context from being unloaded. * - drivers using these resource directly issue their own * GRU instruction and must wait/check completion. * * When these resources are reserved, the caller can optionally * associate a wait_queue with the resources and use asynchronous * GRU instructions. When an async GRU instruction completes, the * driver will do a wakeup on the event. *
*/
/* * Reload the blade's kernel context into a GRU chiplet. Called holding * the bs_kgts_sema for READ. Will steal user contexts if necessary.
*/ staticvoid gru_load_kernel_context(struct gru_blade_state *bs, int blade_id)
{ struct gru_state *gru; struct gru_thread_state *kgts; void *vaddr; int ctxnum, ncpus;
/* * Free all kernel contexts that are not currently in use. * Returns 0 if all freed, else number of inuse context.
*/ staticint gru_free_kernel_contexts(void)
{ struct gru_blade_state *bs; struct gru_thread_state *kgts; int bid, ret = 0;
for (bid = 0; bid < GRU_MAX_BLADES; bid++) {
bs = gru_base[bid]; if (!bs) continue;
/* Handle the case where migration occurred while waiting for the sema */
down_read(&bs->bs_kgts_sema); if (blade_id < 0 && bid != uv_numa_blade_id()) {
up_read(&bs->bs_kgts_sema); goto again;
} if (!bs->bs_kgts || !bs->bs_kgts->ts_gru)
gru_load_kernel_context(bs, bid); return bs;
}
/* * Unlock the kernel context for the specified blade. Context is not * unloaded but may be stolen before next use.
*/ staticvoid gru_unlock_kernel_context(int blade_id)
{ struct gru_blade_state *bs;
/* * Reserve & get pointers to the DSR/CBRs reserved for the current cpu. * - returns with preemption disabled
*/ staticint gru_get_cpu_resources(int dsr_bytes, void **cb, void **dsr)
{ struct gru_blade_state *bs; int lcpu;
/* * Free the current cpus reserved DSR/CBR resources.
*/ staticvoid gru_free_cpu_resources(void *cb, void *dsr)
{
gru_unlock_kernel_context(uv_numa_blade_id());
}
/* * Reserve GRU resources to be used asynchronously. * Note: currently supports only 1 reservation per blade. * * input: * blade_id - blade on which resources should be reserved * cbrs - number of CBRs * dsr_bytes - number of DSR bytes needed * output: * handle to identify resource * (0 = async resources already reserved)
*/ unsignedlong gru_reserve_async_resources(int blade_id, int cbrs, int dsr_bytes, struct completion *cmp)
{ struct gru_blade_state *bs; struct gru_thread_state *kgts; int ret = 0;
/* Internal status . These are NOT returned to the user. */ #define MQIE_AGAIN -1 /* try again */
/* * Save/restore the "present" flag that is in the second line of 2-line * messages
*/ staticinlineint get_present2(void *p)
{ struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES; return mhdr->present;
}
staticinlinevoid restore_present2(void *p, int val)
{ struct message_header *mhdr = p + GRU_CACHE_LINE_BYTES;
mhdr->present = val;
}
/* * Create a message queue. * qlines - message queue size in cache lines. Includes 2-line header.
*/ int gru_create_message_queue(struct gru_message_queue_desc *mqd, void *p, unsignedint bytes, int nasid, int vector, int apicid)
{ struct message_queue *mq = p; unsignedint qlines;
/* * Send a NOOP message to a message queue * Returns: * 0 - if queue is full after the send. This is the normal case * but various races can change this. * -1 - if mesq sent successfully but queue not full * >0 - unexpected error. MQE_xxx returned
*/ staticint send_noop_message(void *cb, struct gru_message_queue_desc *mqd, void *mesg)
{ conststruct message_header noop_header = {
.present = MQS_NOOP, .lines = 1}; unsignedlong m; int substatus, ret; struct message_header save_mhdr, *mhdr = mesg;
if (ret) {
substatus = gru_get_cb_message_queue_substatus(cb); switch (substatus) { case CBSS_NO_ERROR:
STAT(mesq_noop_unexpected_error);
ret = MQE_UNEXPECTED_CB_ERR; break; case CBSS_LB_OVERFLOWED:
STAT(mesq_noop_lb_overflow);
ret = MQE_CONGESTION; break; case CBSS_QLIMIT_REACHED:
STAT(mesq_noop_qlimit_reached);
ret = 0; break; case CBSS_AMO_NACKED:
STAT(mesq_noop_amo_nacked);
ret = MQE_CONGESTION; break; case CBSS_PUT_NACKED:
STAT(mesq_noop_put_nacked);
m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6);
gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, 1, 1,
IMA); if (gru_wait(cb) == CBS_IDLE)
ret = MQIE_AGAIN; else
ret = MQE_UNEXPECTED_CB_ERR; break; case CBSS_PAGE_OVERFLOW:
STAT(mesq_noop_page_overflow);
fallthrough; default:
BUG();
}
}
*mhdr = save_mhdr; return ret;
}
/* * Handle a gru_mesq full.
*/ staticint send_message_queue_full(void *cb, struct gru_message_queue_desc *mqd, void *mesg, int lines)
{ union gru_mesqhead mqh; unsignedint limit, head; unsignedlong avalue; int half, qlines;
/* Determine if switching to first/second half of q */
avalue = gru_get_amo_value(cb);
head = gru_get_amo_value_head(cb);
limit = gru_get_amo_value_limit(cb);
/* Try to get lock for switching head pointer */
gru_gamir(cb, EOP_IR_CLR, HSTATUS(mqd->mq_gpa, half), XTYPE_DW, IMA); if (gru_wait(cb) != CBS_IDLE) goto cberr; if (!gru_get_amo_value(cb)) {
STAT(mesq_qf_locked); return MQE_QUEUE_FULL;
}
/* Got the lock. Send optional NOP if queue not full, */ if (head != limit) { if (send_noop_message(cb, mqd, mesg)) {
gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half),
XTYPE_DW, IMA); if (gru_wait(cb) != CBS_IDLE) goto cberr;
STAT(mesq_qf_noop_not_full); return MQIE_AGAIN;
}
avalue++;
}
/* Then flip queuehead to other half of queue. */
gru_gamer(cb, EOP_ERR_CSWAP, mqd->mq_gpa, XTYPE_DW, mqh.val, avalue,
IMA); if (gru_wait(cb) != CBS_IDLE) goto cberr;
/* If not successfully in swapping queue head, clear the hstatus lock */ if (gru_get_amo_value(cb) != avalue) {
STAT(mesq_qf_switch_head_failed);
gru_gamir(cb, EOP_IR_INC, HSTATUS(mqd->mq_gpa, half), XTYPE_DW,
IMA); if (gru_wait(cb) != CBS_IDLE) goto cberr;
} return MQIE_AGAIN;
cberr:
STAT(mesq_qf_unexpected_error); return MQE_UNEXPECTED_CB_ERR;
}
/* * Handle a PUT failure. Note: if message was a 2-line message, one of the * lines might have successfully have been written. Before sending the * message, "present" must be cleared in BOTH lines to prevent the receiver * from prematurely seeing the full message.
*/ staticint send_message_put_nacked(void *cb, struct gru_message_queue_desc *mqd, void *mesg, int lines)
{ unsignedlong m; int ret, loops = 200; /* experimentally determined */
m = mqd->mq_gpa + (gru_get_amo_value_head(cb) << 6); if (lines == 2) {
gru_vset(cb, m, 0, XTYPE_CL, lines, 1, IMA); if (gru_wait(cb) != CBS_IDLE) return MQE_UNEXPECTED_CB_ERR;
}
gru_vstore(cb, m, gru_get_tri(mesg), XTYPE_CL, lines, 1, IMA); if (gru_wait(cb) != CBS_IDLE) return MQE_UNEXPECTED_CB_ERR;
if (!mqd->interrupt_vector) return MQE_OK;
/* * Send a noop message in order to deliver a cross-partition interrupt * to the SSI that contains the target message queue. Normally, the * interrupt is automatically delivered by hardware following mesq * operations, but some error conditions require explicit delivery. * The noop message will trigger delivery. Otherwise partition failures * could cause unrecovered errors.
*/ do {
ret = send_noop_message(cb, mqd, mesg);
} while ((ret == MQIE_AGAIN || ret == MQE_CONGESTION) && (loops-- > 0));
if (ret == MQIE_AGAIN || ret == MQE_CONGESTION) { /* * Don't indicate to the app to resend the message, as it's * already been successfully sent. We simply send an OK * (rather than fail the send with MQE_UNEXPECTED_CB_ERR), * assuming that the other side is receiving enough * interrupts to get this message processed anyway.
*/
ret = MQE_OK;
} return ret;
}
/* * Handle a gru_mesq failure. Some of these failures are software recoverable * or retryable.
*/ staticint send_message_failure(void *cb, struct gru_message_queue_desc *mqd, void *mesg, int lines)
{ int substatus, ret = 0;
substatus = gru_get_cb_message_queue_substatus(cb); switch (substatus) { case CBSS_NO_ERROR:
STAT(mesq_send_unexpected_error);
ret = MQE_UNEXPECTED_CB_ERR; break; case CBSS_LB_OVERFLOWED:
STAT(mesq_send_lb_overflow);
ret = MQE_CONGESTION; break; case CBSS_QLIMIT_REACHED:
STAT(mesq_send_qlimit_reached);
ret = send_message_queue_full(cb, mqd, mesg, lines); break; case CBSS_AMO_NACKED:
STAT(mesq_send_amo_nacked);
ret = MQE_CONGESTION; break; case CBSS_PUT_NACKED:
STAT(mesq_send_put_nacked);
ret = send_message_put_nacked(cb, mqd, mesg, lines); break; case CBSS_PAGE_OVERFLOW:
STAT(mesq_page_overflow);
fallthrough; default:
BUG();
} return ret;
}
/* * Send a message to a message queue * mqd message queue descriptor * mesg message. ust be vaddr within a GSEG * bytes message size (<= 2 CL)
*/ int gru_send_message_gpa(struct gru_message_queue_desc *mqd, void *mesg, unsignedint bytes)
{ struct message_header *mhdr; void *cb; void *dsr; int istatus, clines, ret;
/* * Get next message from message queue. Return NULL if no message * present. User must call next_message() to move to next message. * rmq message queue
*/ void *gru_get_next_message(struct gru_message_queue_desc *mqd)
{ struct message_queue *mq = mqd->mq; struct message_header *mhdr = mq->next; int present = mhdr->present;
/* ---------------------- GRU DATA COPY FUNCTIONS ---------------------------*/
/* * Load a DW from a global GPA. The GPA can be a memory or MMR address.
*/ int gru_read_gpa(unsignedlong *value, unsignedlong gpa)
{ void *cb; void *dsr; int ret, iaa;
/* * Copy a block of data using the GRU resources
*/ int gru_copy_gpa(unsignedlong dest_gpa, unsignedlong src_gpa, unsignedint bytes)
{ void *cb; void *dsr; int ret;
staticint quicktest1(unsignedlong arg)
{ struct gru_message_queue_desc mqd; void *p, *mq; int i, ret = -EIO; char mes[GRU_CACHE_LINE_BYTES], *m;
/* Need 1K cacheline aligned that does not cross page boundary */
p = kmalloc(4096, 0); if (p == NULL) return -ENOMEM;
mq = ALIGNUP(p, 1024);
memset(mes, 0xee, sizeof(mes));
gru_create_message_queue(&mqd, mq, 8 * GRU_CACHE_LINE_BYTES, 0, 0, 0); for (i = 0; i < 6; i++) {
mes[8] = i; do {
ret = gru_send_message_gpa(&mqd, mes, sizeof(mes));
} while (ret == MQE_CONGESTION); if (ret) break;
} if (ret != MQE_QUEUE_FULL || i != 4) {
printk(KERN_DEBUG "GRU:%d quicktest1: unexpected status %d, i %d\n",
smp_processor_id(), ret, i); goto done;
}
for (i = 0; i < 6; i++) {
m = gru_get_next_message(&mqd); if (!m || m[8] != i) break;
gru_free_message(&mqd, m);
} if (i != 4) {
printk(KERN_DEBUG "GRU:%d quicktest2: bad message, i %d, m %p, m8 %d\n",
smp_processor_id(), i, m, m ? m[8] : -1); goto done;
}
ret = 0;
done:
kfree(p); return ret;
}
staticint quicktest2(unsignedlong arg)
{ static DECLARE_COMPLETION(cmp); unsignedlong han; int blade_id = 0; int numcb = 4; int ret = 0; unsignedlong *buf; void *cb0, *cb; struct gru_control_block_status *gen; int i, k, istatus, bytes;
/* * Debugging only. User hook for various kernel tests * of driver & gru.
*/ int gru_ktest(unsignedlong arg)
{ int ret = -EINVAL;
switch (arg & 0xff) { case 0:
ret = quicktest0(arg); break; case 1:
ret = quicktest1(arg); break; case 2:
ret = quicktest2(arg); break; case 3:
ret = quicktest3(arg); break; case 99:
ret = gru_free_kernel_contexts(); break;
} return ret;
}
int gru_kservices_init(void)
{ return 0;
}
void gru_kservices_exit(void)
{ if (gru_free_kernel_contexts())
BUG();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.