From nobody Mon Sep 28 00:34:17 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists1p.gnu.org (lists1p.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1786437771626773.6679675027307; Tue, 11 Aug 2026 01:42:51 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists1p.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wti47-0003Ea-10; Tue, 11 Aug 2026 04:42:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists1p.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1wti44-0003EQ-P0 for qemu-devel@nongnu.org; Tue, 11 Aug 2026 04:42:28 -0400 Received: from mail.loongson.cn ([114.242.206.163]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1wti42-00010p-Dk for qemu-devel@nongnu.org; Tue, 11 Aug 2026 04:42:28 -0400 Received: from loongson.cn (unknown [10.40.46.54]) by gateway (Coremail) with SMTP id _____8DxC9Jr4Hpqih4AAA--.375S3; Tue, 11 Aug 2026 16:42:19 +0800 (CST) Received: from fedora.loongson.cn (unknown [10.40.46.54]) by front1 (Coremail) with SMTP id qMiowJAxnM5q4Hpq114AAA--.1042S2; Tue, 11 Aug 2026 16:42:18 +0800 (CST) From: Bibo Mao To: Song Gao <17746591750@163.com> Cc: Xianglai Li , Jiaxun Yang , qemu-devel@nongnu.org Subject: [PATCH] hw/intc/loongarch_dintc: Add loongarch_dintc_cpu_by_arch_id() function Date: Tue, 11 Aug 2026 16:43:31 +0800 Message-ID: <20260811084331.23526-1-maobibo@loongson.cn> X-Mailer: git-send-email 2.54.0 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: qMiowJAxnM5q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1lIxAIcVC2z280aVCY1x 0267AKxVWUJVW8JbIYCTnIWIevJa73UjIFyTuYvjxU25EfUUUUU Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists1p.gnu.org; Received-SPF: pass client-ip=114.242.206.163; envelope-from=maobibo@loongson.cn; helo=mail.loongson.cn X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: qemu development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1786437777018158500 Content-Type: text/plain; charset="utf-8" Similar with IPI, add function loongarch_dintc_cpu_by_arch_id() for dintc driver. It searches dintc device from physical CPUID, also it used binary search method, rather than one by one with common API cpu_by_arch_id(). And use this newly added API in loongarch_dintc_get_cpu() and loongarch_dintc_mem_write(). Signed-off-by: Bibo Mao --- hw/intc/loongarch_dintc.c | 49 +++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/hw/intc/loongarch_dintc.c b/hw/intc/loongarch_dintc.c index e40292887c..68c1cfce43 100644 --- a/hw/intc/loongarch_dintc.c +++ b/hw/intc/loongarch_dintc.c @@ -26,6 +26,25 @@ FIELD(MSG_ADDR, IRQ_NUM, 4, 8) FIELD(MSG_ADDR, CPU_NUM, 12, 8) FIELD(MSG_ADDR, FIX, 28, 12) =20 +static int loongarch_dintc_cmp(const void *a, const void *b) +{ + DINTCCore *dintc_a =3D (DINTCCore *)a; + DINTCCore *dintc_b =3D (DINTCCore *)b; + + return dintc_a->arch_id - dintc_b->arch_id; +} + +static DINTCCore *loongarch_dintc_cpu_by_arch_id(LoongArchDINTCState *s, + int64_t arch_id) +{ + DINTCCore dintc, *found; + + dintc.arch_id =3D arch_id; + found =3D bsearch(&dintc, s->cpu, s->num_cpu, sizeof(DINTCCore), + loongarch_dintc_cmp); + return found; +} + static uint64_t loongarch_dintc_mem_read(void *opaque, hwaddr addr, unsigned size) { @@ -49,23 +68,20 @@ static void loongarch_dintc_mem_write(void *opaque, hwa= ddr addr, { int irq_num, cpu_num =3D 0; LoongArchDINTCState *s =3D LOONGARCH_DINTC(opaque); - uint64_t msg_addr =3D addr + VIRT_DINTC_BASE; + uint64_t msg_addr =3D addr + VIRT_DINTC_BASE, arch_id; + DINTCCore *core; CPUState *cs; =20 - cpu_num =3D FIELD_EX64(msg_addr, MSG_ADDR, CPU_NUM); - - /* Validate cpu_num against the configured number of CPUs */ - if (cpu_num >=3D s->num_cpu) { - qemu_log_mask(LOG_GUEST_ERROR, - "loongarch-dintc: invalid cpu number%d\n", cpu_num); - return; - } - cs =3D cpu_by_arch_id(cpu_num); - if (!cs) { + arch_id =3D FIELD_EX64(msg_addr, MSG_ADDR, CPU_NUM); + core =3D loongarch_dintc_cpu_by_arch_id(s, arch_id); + if (!core || !core->cpu) { qemu_log_mask(LOG_GUEST_ERROR, - "loongarch-dintc: no CPU for arch_id %d\n", cpu_num); + "loongarch-dintc: no CPU for arch_id %"PRId64"\n", + arch_id); return; } + cpu_num =3D core - s->cpu; + cs =3D core->cpu; irq_num =3D FIELD_EX64(msg_addr, MSG_ADDR, IRQ_NUM); =20 async_run_on_cpu(cs, do_set_vcpu_dintc_irq, @@ -135,15 +151,8 @@ static DINTCCore *loongarch_dintc_get_cpu(LoongArchDIN= TCState *s, { CPUClass *k =3D CPU_GET_CLASS(dev); uint64_t arch_id =3D k->get_arch_id(CPU(dev)); - int i; - - for (i =3D 0; i < s->num_cpu; i++) { - if (s->cpu[i].arch_id =3D=3D arch_id) { - return &s->cpu[i]; - } - } =20 - return NULL; + return loongarch_dintc_cpu_by_arch_id(s, arch_id); } =20 static void loongarch_dintc_cpu_plug(HotplugHandler *hotplug_dev, base-commit: 3e3ccab106f879b1512f8e0d51a827dd4de30e22 --=20 2.54.0