From nobody Sun Nov 24 14:04:34 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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; dmarc=pass(p=none dis=none) header.from=linaro.org ARC-Seal: i=1; a=rsa-sha256; t=1722948881; cv=none; d=zohomail.com; s=zohoarc; b=WjXhP/v9XYIFlMczhMfwQPYZrdygDXDFvfdOls6YmK8DrZWJ9P2EaRaU10Zkb21hiFTZg2JsboakFHXv9achFkSNDhMF5pZGez/RAgj01XbanF/jG+xJwvhymN2xb0ZrZ0UyeHqtLBWS0zshlKZ3DUQhQruHFy+VzrB6i1kUqXQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1722948881; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=fVXH7TtXrVZxOC7TgTKdtLSEQnAvMfiUURH/wkuD2/0=; b=T+HXTO4knyUwaOmkYnogOHUfeCJcw8FutgJ4K/Qker4A+KuT5fkwHKi0/xG6JtYe0Wdpt6WvJ+vw+yVwc6cLS3F5fZ1IWt27FFuelSI31NwjNPkyIJujJ1hZg4/CM/puvS4dfsKRr60boDit9poTD+zrCF4unHLir/ShDT7evQo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 172294888109713.291975167844043; Tue, 6 Aug 2024 05:54:41 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sbJgs-0001zJ-Hc; Tue, 06 Aug 2024 08:53:26 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sbJgj-0001Cq-Bl for qemu-devel@nongnu.org; Tue, 06 Aug 2024 08:53:21 -0400 Received: from mail-ed1-x52c.google.com ([2a00:1450:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sbJgf-0008VN-8p for qemu-devel@nongnu.org; Tue, 06 Aug 2024 08:53:17 -0400 Received: by mail-ed1-x52c.google.com with SMTP id 4fb4d7f45d1cf-5af6a1afa63so946620a12.0 for ; Tue, 06 Aug 2024 05:53:11 -0700 (PDT) Received: from m1x-phil.lan (cor91-h02-176-184-30-206.dsl.sta.abo.bbox.fr. [176.184.30.206]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5b83c22db88sm5838766a12.95.2024.08.06.05.53.07 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 06 Aug 2024 05:53:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1722948789; x=1723553589; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fVXH7TtXrVZxOC7TgTKdtLSEQnAvMfiUURH/wkuD2/0=; b=iAFFqXmgb0J97bu/2WZ3HMW9POPmV1aHhkovvWuYnhjQX4TSflS7ORKVVVgTzkYhQW ztDqQ0ivn9Ek8FSXl5QrwOOrusFQKjlNRgxR8f11frn/aGfbAIUPgdBOn0RQl25vLXs3 KwSizzi8z1/vHVMI2XowbHp4I5TxXEMbFF05YBcAbl35p8NhhHhpf0sGe2/G9kfPU1hH Qxrj5LdIhoFZ+o+e1P8DPNWQZXEryhTFpqUEk3hUT0gRms3AE2Uwluc+ynDoFv/IBIRq h8MvIMecKpqjGhGtAMUrh3dExWkowkf0NGZZ/jaQVkrDkvmtLIT7PwaoWTpLErwRGWGS onIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722948789; x=1723553589; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=fVXH7TtXrVZxOC7TgTKdtLSEQnAvMfiUURH/wkuD2/0=; b=v3MAjktmyxbkTrNjuANzNBvBsgyA5fYQim6J+ppLJHN+ctnECpJDl+BYLnil7q3mxD NC8js7UDrPtg91mqxmA0vNHs8wyFQ5FB/ndfMh9t8RWWOqksK6um0U5IKRdyyAl+dQrF Dngv5LH+utBRMp8LKJe6UGFg8BXxp/qIunoyYS8TeXq6LavFloZetEb2jgM1My9h8mJ7 bQpTKpHfiFY5zYcQAWwhIrGoYktTCkj2GLFiF3zAjSeLOoSmsLX1RCQqg3izgI4Igg6K cAcLTEay4xiDp1dHje+AlLarQ9rL8O+aSgX7MI2mvWdaMcpEOGinXBgbjJHOhtNZGWnD oHsg== X-Gm-Message-State: AOJu0YzRPFUMCsiZ1qcv31cornM5OnDLNhug3ZA74+77RFiKsniDW9f/ mU8CqqVoKX16qFQHWMEH7Bp4VgglFyy/sToEB0bYcJ/51K7cmpFKbv7crTpLBSREAo7fAakPY6x E X-Google-Smtp-Source: AGHT+IFoYa+s60W+/iNx2gnqzUUkxooxfTG4kZf5iiEcSLFDdqcCAUF4Fwdov8UCqWapmnVN8PcUxA== X-Received: by 2002:a05:6402:a50:b0:578:60a6:7c69 with SMTP id 4fb4d7f45d1cf-5b7f5413b46mr9672712a12.30.1722948789418; Tue, 06 Aug 2024 05:53:09 -0700 (PDT) From: =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= To: qemu-devel@nongnu.org Cc: Bibo Mao , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Song Gao , Richard Henderson , Jiaxun Yang Subject: [PULL 11/28] hw/intc/loongson_ipi: Move common code to loongson_ipi_common.c Date: Tue, 6 Aug 2024 14:51:39 +0200 Message-ID: <20240806125157.91185-12-philmd@linaro.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240806125157.91185-1-philmd@linaro.org> References: <20240806125157.91185-1-philmd@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::52c; envelope-from=philmd@linaro.org; helo=mail-ed1-x52c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, 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: 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-ZohoMail-DKIM: pass (identity @linaro.org) X-ZM-MESSAGEID: 1722948881931116600 From: Bibo Mao Move the common code from loongson_ipi.c to loongson_ipi_common.c, call parent_realize() instead of loongson_ipi_common_realize() in loongson_ipi_realize(). Signed-off-by: Bibo Mao [PMD: Extracted from bigger commit, added commit description] Co-Developed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: Philippe Mathieu-Daud=C3=A9 Reviewed-by: Bibo Mao Tested-by: Bibo Mao Acked-by: Song Gao Reviewed-by: Richard Henderson Reviewed-by: Jiaxun Yang Tested-by: Jiaxun Yang Message-Id: <20240805180622.21001-12-philmd@linaro.org> --- include/hw/intc/loongson_ipi_common.h | 2 + hw/intc/loongson_ipi.c | 279 +------------------------ hw/intc/loongson_ipi_common.c | 283 ++++++++++++++++++++++++++ 3 files changed, 289 insertions(+), 275 deletions(-) diff --git a/include/hw/intc/loongson_ipi_common.h b/include/hw/intc/loongs= on_ipi_common.h index 65f8ef7957..df9d9c5168 100644 --- a/include/hw/intc/loongson_ipi_common.h +++ b/include/hw/intc/loongson_ipi_common.h @@ -41,6 +41,8 @@ struct LoongsonIPICommonState { struct LoongsonIPICommonClass { SysBusDeviceClass parent_class; =20 + DeviceRealize parent_realize; + DeviceUnrealize parent_unrealize; AddressSpace *(*get_iocsr_as)(CPUState *cpu); CPUState *(*cpu_by_arch_id)(int64_t id); }; diff --git a/hw/intc/loongson_ipi.c b/hw/intc/loongson_ipi.c index c13cb5a1d2..0b88ae3230 100644 --- a/hw/intc/loongson_ipi.c +++ b/hw/intc/loongson_ipi.c @@ -24,56 +24,6 @@ #endif #include "trace.h" =20 -MemTxResult loongson_ipi_core_readl(void *opaque, hwaddr addr, uint64_t *d= ata, - unsigned size, MemTxAttrs attrs) -{ - IPICore *s =3D opaque; - uint64_t ret =3D 0; - int index =3D 0; - - addr &=3D 0xff; - switch (addr) { - case CORE_STATUS_OFF: - ret =3D s->status; - break; - case CORE_EN_OFF: - ret =3D s->en; - break; - case CORE_SET_OFF: - ret =3D 0; - break; - case CORE_CLEAR_OFF: - ret =3D 0; - break; - case CORE_BUF_20 ... CORE_BUF_38 + 4: - index =3D (addr - CORE_BUF_20) >> 2; - ret =3D s->buf[index]; - break; - default: - qemu_log_mask(LOG_UNIMP, "invalid read: %x", (uint32_t)addr); - break; - } - - trace_loongson_ipi_read(size, (uint64_t)addr, ret); - *data =3D ret; - return MEMTX_OK; -} - -static MemTxResult loongson_ipi_iocsr_readl(void *opaque, hwaddr addr, - uint64_t *data, - unsigned size, MemTxAttrs attr= s) -{ - LoongsonIPICommonState *ipi =3D opaque; - IPICore *s; - - if (attrs.requester_id >=3D ipi->num_cpu) { - return MEMTX_DECODE_ERROR; - } - - s =3D &ipi->cpu[attrs.requester_id]; - return loongson_ipi_core_readl(s, addr, data, size, attrs); -} - #ifdef TARGET_LOONGARCH64 static AddressSpace *get_iocsr_as(CPUState *cpu) { @@ -92,148 +42,6 @@ static AddressSpace *get_iocsr_as(CPUState *cpu) } #endif =20 -static MemTxResult send_ipi_data(LoongsonIPICommonState *ipi, CPUState *cp= u, - uint64_t val, hwaddr addr, MemTxAttrs att= rs) -{ - LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(ipi); - int i, mask =3D 0, data =3D 0; - AddressSpace *iocsr_as =3D licc->get_iocsr_as(cpu); - - if (!iocsr_as) { - return MEMTX_DECODE_ERROR; - } - - /* - * bit 27-30 is mask for byte writing, - * if the mask is 0, we need not to do anything. - */ - if ((val >> 27) & 0xf) { - data =3D address_space_ldl_le(iocsr_as, addr, attrs, NULL); - for (i =3D 0; i < 4; i++) { - /* get mask for byte writing */ - if (val & (0x1 << (27 + i))) { - mask |=3D 0xff << (i * 8); - } - } - } - - data &=3D mask; - data |=3D (val >> 32) & ~mask; - address_space_stl_le(iocsr_as, addr, data, attrs, NULL); - - return MEMTX_OK; -} - -static MemTxResult mail_send(LoongsonIPICommonState *ipi, - uint64_t val, MemTxAttrs attrs) -{ - LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(ipi); - uint32_t cpuid; - hwaddr addr; - CPUState *cs; - - cpuid =3D extract32(val, 16, 10); - cs =3D licc->cpu_by_arch_id(cpuid); - if (cs =3D=3D NULL) { - return MEMTX_DECODE_ERROR; - } - - /* override requester_id */ - addr =3D SMP_IPI_MAILBOX + CORE_BUF_20 + (val & 0x1c); - attrs.requester_id =3D cs->cpu_index; - return send_ipi_data(ipi, cs, val, addr, attrs); -} - -static MemTxResult any_send(LoongsonIPICommonState *ipi, - uint64_t val, MemTxAttrs attrs) -{ - LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(ipi); - uint32_t cpuid; - hwaddr addr; - CPUState *cs; - - cpuid =3D extract32(val, 16, 10); - cs =3D licc->cpu_by_arch_id(cpuid); - if (cs =3D=3D NULL) { - return MEMTX_DECODE_ERROR; - } - - /* override requester_id */ - addr =3D val & 0xffff; - attrs.requester_id =3D cs->cpu_index; - return send_ipi_data(ipi, cs, val, addr, attrs); -} - -MemTxResult loongson_ipi_core_writel(void *opaque, hwaddr addr, uint64_t v= al, - unsigned size, MemTxAttrs attrs) -{ - IPICore *s =3D opaque; - LoongsonIPICommonState *ipi =3D s->ipi; - LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(ipi); - int index =3D 0; - uint32_t cpuid; - uint8_t vector; - CPUState *cs; - - addr &=3D 0xff; - trace_loongson_ipi_write(size, (uint64_t)addr, val); - switch (addr) { - case CORE_STATUS_OFF: - qemu_log_mask(LOG_GUEST_ERROR, "can not be written"); - break; - case CORE_EN_OFF: - s->en =3D val; - break; - case CORE_SET_OFF: - s->status |=3D val; - if (s->status !=3D 0 && (s->status & s->en) !=3D 0) { - qemu_irq_raise(s->irq); - } - break; - case CORE_CLEAR_OFF: - s->status &=3D ~val; - if (s->status =3D=3D 0 && s->en !=3D 0) { - qemu_irq_lower(s->irq); - } - break; - case CORE_BUF_20 ... CORE_BUF_38 + 4: - index =3D (addr - CORE_BUF_20) >> 2; - s->buf[index] =3D val; - break; - case IOCSR_IPI_SEND: - cpuid =3D extract32(val, 16, 10); - /* IPI status vector */ - vector =3D extract8(val, 0, 5); - cs =3D licc->cpu_by_arch_id(cpuid); - if (cs =3D=3D NULL || cs->cpu_index >=3D ipi->num_cpu) { - return MEMTX_DECODE_ERROR; - } - loongson_ipi_core_writel(&ipi->cpu[cs->cpu_index], CORE_SET_OFF, - BIT(vector), 4, attrs); - break; - default: - qemu_log_mask(LOG_UNIMP, "invalid write: %x", (uint32_t)addr); - break; - } - - return MEMTX_OK; -} - -static MemTxResult loongson_ipi_iocsr_writel(void *opaque, hwaddr addr, - uint64_t val, unsigned size, - MemTxAttrs attrs) -{ - LoongsonIPICommonState *ipi =3D opaque; - IPICore *s; - - if (attrs.requester_id >=3D ipi->num_cpu) { - return MEMTX_DECODE_ERROR; - } - - s =3D &ipi->cpu[attrs.requester_id]; - return loongson_ipi_core_writel(s, addr, val, size, attrs); -} - static const MemoryRegionOps loongson_ipi_core_ops =3D { .read_with_attrs =3D loongson_ipi_core_readl, .write_with_attrs =3D loongson_ipi_core_writel, @@ -244,88 +52,15 @@ static const MemoryRegionOps loongson_ipi_core_ops =3D= { .endianness =3D DEVICE_LITTLE_ENDIAN, }; =20 -static const MemoryRegionOps loongson_ipi_iocsr_ops =3D { - .read_with_attrs =3D loongson_ipi_iocsr_readl, - .write_with_attrs =3D loongson_ipi_iocsr_writel, - .impl.min_access_size =3D 4, - .impl.max_access_size =3D 4, - .valid.min_access_size =3D 4, - .valid.max_access_size =3D 8, - .endianness =3D DEVICE_LITTLE_ENDIAN, -}; - -/* mail send and any send only support writeq */ -static MemTxResult loongson_ipi_writeq(void *opaque, hwaddr addr, uint64_t= val, - unsigned size, MemTxAttrs attrs) -{ - LoongsonIPICommonState *ipi =3D opaque; - MemTxResult ret =3D MEMTX_OK; - - addr &=3D 0xfff; - switch (addr) { - case MAIL_SEND_OFFSET: - ret =3D mail_send(ipi, val, attrs); - break; - case ANY_SEND_OFFSET: - ret =3D any_send(ipi, val, attrs); - break; - default: - break; - } - - return ret; -} - -static const MemoryRegionOps loongson_ipi64_ops =3D { - .write_with_attrs =3D loongson_ipi_writeq, - .impl.min_access_size =3D 8, - .impl.max_access_size =3D 8, - .valid.min_access_size =3D 8, - .valid.max_access_size =3D 8, - .endianness =3D DEVICE_LITTLE_ENDIAN, -}; - -static void loongson_ipi_common_realize(DeviceState *dev, Error **errp) -{ - LoongsonIPICommonState *s =3D LOONGSON_IPI_COMMON(dev); - SysBusDevice *sbd =3D SYS_BUS_DEVICE(dev); - int i; - - if (s->num_cpu =3D=3D 0) { - error_setg(errp, "num-cpu must be at least 1"); - return; - } - - memory_region_init_io(&s->ipi_iocsr_mem, OBJECT(dev), - &loongson_ipi_iocsr_ops, - s, "loongson_ipi_iocsr", 0x48); - - /* loongson_ipi_iocsr performs re-entrant IO through ipi_send */ - s->ipi_iocsr_mem.disable_reentrancy_guard =3D true; - - sysbus_init_mmio(sbd, &s->ipi_iocsr_mem); - - memory_region_init_io(&s->ipi64_iocsr_mem, OBJECT(dev), - &loongson_ipi64_ops, - s, "loongson_ipi64_iocsr", 0x118); - sysbus_init_mmio(sbd, &s->ipi64_iocsr_mem); - - s->cpu =3D g_new0(IPICore, s->num_cpu); - for (i =3D 0; i < s->num_cpu; i++) { - s->cpu[i].ipi =3D s; - - qdev_init_gpio_out(dev, &s->cpu[i].irq, 1); - } -} - static void loongson_ipi_realize(DeviceState *dev, Error **errp) { LoongsonIPICommonState *sc =3D LOONGSON_IPI_COMMON(dev); LoongsonIPIState *s =3D LOONGSON_IPI(dev); + LoongsonIPIClass *lic =3D LOONGSON_IPI_GET_CLASS(dev); SysBusDevice *sbd =3D SYS_BUS_DEVICE(dev); Error *local_err =3D NULL; =20 - loongson_ipi_common_realize(dev, &local_err); + lic->parent_realize(dev, &local_err); if (local_err) { error_propagate(errp, local_err); return; @@ -341,20 +76,14 @@ static void loongson_ipi_realize(DeviceState *dev, Err= or **errp) } } =20 -static void loongson_ipi_common_unrealize(DeviceState *dev) -{ - LoongsonIPICommonState *s =3D LOONGSON_IPI_COMMON(dev); - - g_free(s->cpu); -} - static void loongson_ipi_unrealize(DeviceState *dev) { LoongsonIPIState *s =3D LOONGSON_IPI(dev); + LoongsonIPIClass *k =3D LOONGSON_IPI_GET_CLASS(dev); =20 g_free(s->ipi_mmio_mem); =20 - loongson_ipi_common_unrealize(dev); + k->parent_unrealize(dev); } =20 static void loongson_ipi_class_init(ObjectClass *klass, void *data) diff --git a/hw/intc/loongson_ipi_common.c b/hw/intc/loongson_ipi_common.c index 47796f7ece..a6ce0181f6 100644 --- a/hw/intc/loongson_ipi_common.c +++ b/hw/intc/loongson_ipi_common.c @@ -8,8 +8,286 @@ #include "qemu/osdep.h" #include "hw/sysbus.h" #include "hw/intc/loongson_ipi_common.h" +#include "hw/irq.h" #include "hw/qdev-properties.h" +#include "qapi/error.h" +#include "qemu/log.h" #include "migration/vmstate.h" +#include "trace.h" + +MemTxResult loongson_ipi_core_readl(void *opaque, hwaddr addr, uint64_t *d= ata, + unsigned size, MemTxAttrs attrs) +{ + IPICore *s =3D opaque; + uint64_t ret =3D 0; + int index =3D 0; + + addr &=3D 0xff; + switch (addr) { + case CORE_STATUS_OFF: + ret =3D s->status; + break; + case CORE_EN_OFF: + ret =3D s->en; + break; + case CORE_SET_OFF: + ret =3D 0; + break; + case CORE_CLEAR_OFF: + ret =3D 0; + break; + case CORE_BUF_20 ... CORE_BUF_38 + 4: + index =3D (addr - CORE_BUF_20) >> 2; + ret =3D s->buf[index]; + break; + default: + qemu_log_mask(LOG_UNIMP, "invalid read: %x", (uint32_t)addr); + break; + } + + trace_loongson_ipi_read(size, (uint64_t)addr, ret); + *data =3D ret; + + return MEMTX_OK; +} + +static MemTxResult loongson_ipi_iocsr_readl(void *opaque, hwaddr addr, + uint64_t *data, unsigned size, + MemTxAttrs attrs) +{ + LoongsonIPICommonState *ipi =3D opaque; + IPICore *s; + + if (attrs.requester_id >=3D ipi->num_cpu) { + return MEMTX_DECODE_ERROR; + } + + s =3D &ipi->cpu[attrs.requester_id]; + return loongson_ipi_core_readl(s, addr, data, size, attrs); +} + +static MemTxResult send_ipi_data(LoongsonIPICommonState *ipi, CPUState *cp= u, + uint64_t val, hwaddr addr, MemTxAttrs att= rs) +{ + LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(ipi); + int i, mask =3D 0, data =3D 0; + AddressSpace *iocsr_as =3D licc->get_iocsr_as(cpu); + + if (!iocsr_as) { + return MEMTX_DECODE_ERROR; + } + + /* + * bit 27-30 is mask for byte writing, + * if the mask is 0, we need not to do anything. + */ + if ((val >> 27) & 0xf) { + data =3D address_space_ldl_le(iocsr_as, addr, attrs, NULL); + for (i =3D 0; i < 4; i++) { + /* get mask for byte writing */ + if (val & (0x1 << (27 + i))) { + mask |=3D 0xff << (i * 8); + } + } + } + + data &=3D mask; + data |=3D (val >> 32) & ~mask; + address_space_stl_le(iocsr_as, addr, data, attrs, NULL); + + return MEMTX_OK; +} + +static MemTxResult mail_send(LoongsonIPICommonState *ipi, + uint64_t val, MemTxAttrs attrs) +{ + LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(ipi); + uint32_t cpuid; + hwaddr addr; + CPUState *cs; + + cpuid =3D extract32(val, 16, 10); + cs =3D licc->cpu_by_arch_id(cpuid); + if (cs =3D=3D NULL) { + return MEMTX_DECODE_ERROR; + } + + /* override requester_id */ + addr =3D SMP_IPI_MAILBOX + CORE_BUF_20 + (val & 0x1c); + attrs.requester_id =3D cs->cpu_index; + return send_ipi_data(ipi, cs, val, addr, attrs); +} + +static MemTxResult any_send(LoongsonIPICommonState *ipi, + uint64_t val, MemTxAttrs attrs) +{ + LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(ipi); + uint32_t cpuid; + hwaddr addr; + CPUState *cs; + + cpuid =3D extract32(val, 16, 10); + cs =3D licc->cpu_by_arch_id(cpuid); + if (cs =3D=3D NULL) { + return MEMTX_DECODE_ERROR; + } + + /* override requester_id */ + addr =3D val & 0xffff; + attrs.requester_id =3D cs->cpu_index; + return send_ipi_data(ipi, cs, val, addr, attrs); +} + +MemTxResult loongson_ipi_core_writel(void *opaque, hwaddr addr, uint64_t v= al, + unsigned size, MemTxAttrs attrs) +{ + IPICore *s =3D opaque; + LoongsonIPICommonState *ipi =3D s->ipi; + LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_GET_CLASS(ipi); + int index =3D 0; + uint32_t cpuid; + uint8_t vector; + CPUState *cs; + + addr &=3D 0xff; + trace_loongson_ipi_write(size, (uint64_t)addr, val); + switch (addr) { + case CORE_STATUS_OFF: + qemu_log_mask(LOG_GUEST_ERROR, "can not be written"); + break; + case CORE_EN_OFF: + s->en =3D val; + break; + case CORE_SET_OFF: + s->status |=3D val; + if (s->status !=3D 0 && (s->status & s->en) !=3D 0) { + qemu_irq_raise(s->irq); + } + break; + case CORE_CLEAR_OFF: + s->status &=3D ~val; + if (s->status =3D=3D 0 && s->en !=3D 0) { + qemu_irq_lower(s->irq); + } + break; + case CORE_BUF_20 ... CORE_BUF_38 + 4: + index =3D (addr - CORE_BUF_20) >> 2; + s->buf[index] =3D val; + break; + case IOCSR_IPI_SEND: + cpuid =3D extract32(val, 16, 10); + /* IPI status vector */ + vector =3D extract8(val, 0, 5); + cs =3D licc->cpu_by_arch_id(cpuid); + if (cs =3D=3D NULL || cs->cpu_index >=3D ipi->num_cpu) { + return MEMTX_DECODE_ERROR; + } + loongson_ipi_core_writel(&ipi->cpu[cs->cpu_index], CORE_SET_OFF, + BIT(vector), 4, attrs); + break; + default: + qemu_log_mask(LOG_UNIMP, "invalid write: %x", (uint32_t)addr); + break; + } + + return MEMTX_OK; +} + +static MemTxResult loongson_ipi_iocsr_writel(void *opaque, hwaddr addr, + uint64_t val, unsigned size, + MemTxAttrs attrs) +{ + LoongsonIPICommonState *ipi =3D opaque; + IPICore *s; + + if (attrs.requester_id >=3D ipi->num_cpu) { + return MEMTX_DECODE_ERROR; + } + + s =3D &ipi->cpu[attrs.requester_id]; + return loongson_ipi_core_writel(s, addr, val, size, attrs); +} + +static const MemoryRegionOps loongson_ipi_iocsr_ops =3D { + .read_with_attrs =3D loongson_ipi_iocsr_readl, + .write_with_attrs =3D loongson_ipi_iocsr_writel, + .impl.min_access_size =3D 4, + .impl.max_access_size =3D 4, + .valid.min_access_size =3D 4, + .valid.max_access_size =3D 8, + .endianness =3D DEVICE_LITTLE_ENDIAN, +}; + +/* mail send and any send only support writeq */ +static MemTxResult loongson_ipi_writeq(void *opaque, hwaddr addr, uint64_t= val, + unsigned size, MemTxAttrs attrs) +{ + LoongsonIPICommonState *ipi =3D opaque; + MemTxResult ret =3D MEMTX_OK; + + addr &=3D 0xfff; + switch (addr) { + case MAIL_SEND_OFFSET: + ret =3D mail_send(ipi, val, attrs); + break; + case ANY_SEND_OFFSET: + ret =3D any_send(ipi, val, attrs); + break; + default: + break; + } + + return ret; +} + +static const MemoryRegionOps loongson_ipi64_ops =3D { + .write_with_attrs =3D loongson_ipi_writeq, + .impl.min_access_size =3D 8, + .impl.max_access_size =3D 8, + .valid.min_access_size =3D 8, + .valid.max_access_size =3D 8, + .endianness =3D DEVICE_LITTLE_ENDIAN, +}; + +static void loongson_ipi_common_realize(DeviceState *dev, Error **errp) +{ + LoongsonIPICommonState *s =3D LOONGSON_IPI_COMMON(dev); + SysBusDevice *sbd =3D SYS_BUS_DEVICE(dev); + int i; + + if (s->num_cpu =3D=3D 0) { + error_setg(errp, "num-cpu must be at least 1"); + return; + } + + memory_region_init_io(&s->ipi_iocsr_mem, OBJECT(dev), + &loongson_ipi_iocsr_ops, + s, "loongson_ipi_iocsr", 0x48); + + /* loongson_ipi_iocsr performs re-entrant IO through ipi_send */ + s->ipi_iocsr_mem.disable_reentrancy_guard =3D true; + + sysbus_init_mmio(sbd, &s->ipi_iocsr_mem); + + memory_region_init_io(&s->ipi64_iocsr_mem, OBJECT(dev), + &loongson_ipi64_ops, + s, "loongson_ipi64_iocsr", 0x118); + sysbus_init_mmio(sbd, &s->ipi64_iocsr_mem); + + s->cpu =3D g_new0(IPICore, s->num_cpu); + for (i =3D 0; i < s->num_cpu; i++) { + s->cpu[i].ipi =3D s; + + qdev_init_gpio_out(dev, &s->cpu[i].irq, 1); + } +} + +static void loongson_ipi_common_unrealize(DeviceState *dev) +{ + LoongsonIPICommonState *s =3D LOONGSON_IPI_COMMON(dev); + + g_free(s->cpu); +} =20 static const VMStateDescription vmstate_ipi_core =3D { .name =3D "ipi-single", @@ -45,7 +323,12 @@ static Property ipi_common_properties[] =3D { static void loongson_ipi_common_class_init(ObjectClass *klass, void *data) { DeviceClass *dc =3D DEVICE_CLASS(klass); + LoongsonIPICommonClass *licc =3D LOONGSON_IPI_COMMON_CLASS(klass); =20 + device_class_set_parent_realize(dc, loongson_ipi_common_realize, + &licc->parent_realize); + device_class_set_parent_unrealize(dc, loongson_ipi_common_unrealize, + &licc->parent_unrealize); device_class_set_props(dc, ipi_common_properties); dc->vmsd =3D &vmstate_loongson_ipi_common; } --=20 2.45.2