From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B0A28C636D6 for ; Mon, 20 Feb 2023 21:59:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=efI4tiPkfHc6Y3dAIVsROjR4Zeq0UzWP7PKSPd638X8=; b=QRdsin5fbeHYRU aEj6rzqtdrYKp4+0gEPs14CLD8sQie2jlUb3GqtSm3THO+vegElQ0ngzbrGzXeMLSi5ztLaoERLPE d3iP1Q+W8g78xppB6Ux5Us+wTSVoO9VeaIoQCk2DToau8IU3/Bf3E2PoDuxRHGPkdpKIYEaopig32 poezWFvw8uPiTWCdtiBB4C7qcIOvu3aoVJlT2A5OMPWNHnrQk2H2zc/mrpO3mAXIjuR8CBhwZfxZv tJi9Q5whtLfjPli7aBeX7CzBBE6O5zfoTNDKInQZ1Osg9Ao+mv9wVWvSFEsTEquRFzTY0ZtEa5acu 85gTI+LqF+r4PrsxYoDQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pUEAi-005iek-7I; Mon, 20 Feb 2023 21:58:08 +0000 Received: from mail-pf1-x433.google.com ([2607:f8b0:4864:20::433]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pUEAf-005idT-35 for linux-arm-kernel@lists.infradead.org; Mon, 20 Feb 2023 21:58:06 +0000 Received: by mail-pf1-x433.google.com with SMTP id f11so1342724pfe.2 for ; Mon, 20 Feb 2023 13:58:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=o6uK3JRKD9glmPZVrtfk/3zmVSx28VEnBekWkjPdr5Y=; b=JQdsgB85zsGpr7bJJtGBrfD6jVZ3ad3XG7mHodDwjrQOppO96CfKnvWDvn027EwEHC 96gBnPMYLefqcV9GtDQ0+KdVQAIS6RA+3R1CqkJMaXPu7rOJpoBPfnZY1CbEx4/KGiY+ VfP4cOljStALIu5BZ2RNwopsKcX+99WTfzFTQNDzn+y3tdQCnMs5sWt7qUe0ODIy7Iik rlyymb0gEjbJtJyh7Rd+hwcHOS4yxtkxXZAaAzwGwiygiwlezXQcXtGzZKN1IVWJrIZ6 KYTBZopgGfTqij71ZSZLw/vf2qKwz80hU2KGTAh3EobzSPjUtDhENfl1b0+KTbQSPJ7m IUdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=o6uK3JRKD9glmPZVrtfk/3zmVSx28VEnBekWkjPdr5Y=; b=p4w1B+C9jNJcW1BQb8v6/IOyW+1FtiER0oXeWtaA75F13gUTdiBHu33g/7a4gucZGy mk97Zjay8Zrsm8YOdcrs//Vb/DDcMtdReHpL/anh+ltA4VW1Rv89Nsqri6KZtF0qDvY8 VBHyjnhK1GtS0u25h+2neDpnP4vCpLG/nDUY7PikP9Ov86IVBRnDanLFGgzQSEXz0udu /+OL5XXHbrNKkpuqK5a0xdN6dGE5GcZATm3OOHeXwk9fHr4VJi6j3M29DdoQI1waypeA 3Pc66uRL8awScMWy+/CtIsPx4D8nD6XcUY4fzS9WyDj/Ohj0eCLT5pG59AMECAwVfjLK Kg1w== X-Gm-Message-State: AO0yUKV0F7FnpWy5TPOhYeFdtJ69kIXCbFs5tgy3MWitzepLGoTuOS6H tOXsaBowrX5HD6p+WnTNrdHghA== X-Google-Smtp-Source: AK7set+5H75omPr1jx07B6MYxxT9l6o0A4rTTnvrjrtvO9CaWBAnSNy7c5wG62GfASXCoYugnTS1OA== X-Received: by 2002:a05:6a00:4194:b0:5a8:cc39:fc58 with SMTP id ca20-20020a056a00419400b005a8cc39fc58mr3447013pfb.6.1676930282962; Mon, 20 Feb 2023 13:58:02 -0800 (PST) Received: from p14s ([2604:3d09:148c:c800:66c:5b9c:15ea:e519]) by smtp.gmail.com with ESMTPSA id m20-20020aa79014000000b00593c1c5bd0esm1433029pfo.164.2023.02.20.13.58.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Feb 2023 13:58:02 -0800 (PST) Date: Mon, 20 Feb 2023 14:58:00 -0700 From: Mathieu Poirier To: "Iuliana Prodan (OSS)" Cc: Bjorn Andersson , Shawn Guo , Sascha Hauer , "S.J. Wang" , Fabio Estevam , Daniel Baluta , Iuliana Prodan , linux-imx , linux-remoteproc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, Pengutronix Kernel Team Subject: Re: [PATCH v4] remoteproc: imx_dsp_rproc: add custom memory copy implementation for i.MX DSP Cores Message-ID: <20230220215800.GA794395@p14s> References: <20230207134401.26724-1-iuliana.prodan@oss.nxp.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20230207134401.26724-1-iuliana.prodan@oss.nxp.com> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230220_135805_207207_14C461A4 X-CRM114-Status: GOOD ( 38.10 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Hi Iuliana, On Tue, Feb 07, 2023 at 03:44:01PM +0200, Iuliana Prodan (OSS) wrote: > From: Iuliana Prodan > > The IRAM is part of the HiFi DSP. > According to hardware specification only 32-bits write are allowed > otherwise we get a Kernel panic. > > Therefore add a custom memory copy and memset functions to deal with > the above restriction. > > Signed-off-by: Iuliana Prodan > > --- > Changes since v3 > - remove Reported-by > > Changes since v2 > - fix warning "cast from pointer to integer of different size" > reported by kernel test robot. > > Changes since v1 > - added missing check for cases when the memory slot is bigger than the file size; > - added a custom memset function > - removed is_iomem flag since is not used here > - updated custom memcpy function to avoid reading after end of source > > --- > drivers/remoteproc/imx_dsp_rproc.c | 181 ++++++++++++++++++++++++++++- > 1 file changed, 180 insertions(+), 1 deletion(-) > > diff --git a/drivers/remoteproc/imx_dsp_rproc.c b/drivers/remoteproc/imx_dsp_rproc.c > index 2d9f4214a4c51..6a7cef1329329 100644 > --- a/drivers/remoteproc/imx_dsp_rproc.c > +++ b/drivers/remoteproc/imx_dsp_rproc.c > @@ -715,6 +715,185 @@ static void imx_dsp_rproc_kick(struct rproc *rproc, int vqid) > dev_err(dev, "%s: failed (%d, err:%d)\n", __func__, vqid, err); > } > > +/* > + * Custom memory copy implementation for i.MX DSP Cores > + * > + * The IRAM is part of the HiFi DSP. > + * According to hw specs only 32-bits writes are allowed. > + */ > +static int imx_dsp_rproc_memcpy(void *dest, const void *src, size_t size) > +{ > + const u8 *src_byte = src; > + u32 affected_mask; > + u32 tmp; > + int i, q, r; > + const u8 *src_byte = src; u32 affected_mask; int i, q, r; u32 tmp; > + /* destination must be 32bit aligned */ > + if (!IS_ALIGNED((uintptr_t)dest, 4)) > + return -EINVAL; > + > + q = size / 4; > + r = size % 4; > + > + /* __iowrite32_copy use 32bit size values so divide by 4 */ > + __iowrite32_copy(dest, src, q); > + > + if (r) { > + affected_mask = (1 << (8 * r)) - 1; Please use GENMASK() > + > + /* first read the 32bit data of dest, then change affected > + * bytes, and write back to dest. > + * For unaffected bytes, it should not be changed > + */ Wrong multi-line comment format. > + tmp = ioread32(dest + q * 4); This turns into readl(). > + tmp &= ~affected_mask; > + > + /* avoid reading after end of source */ > + for (i = 0; i < r; i++) > + tmp |= (src_byte[q * 4 + i] << (8 * i)); > + > + iowrite32(tmp, dest + q * 4); As far as I can tell this turns into a simple writel(), leading me to beleive the above __iowrite32_copy() can safely be replaced by a loop that calls writel(). > + } > + > + return 0; > +} > + > +/* > + * Custom memset implementation for i.MX DSP Cores > + * > + * The IRAM is part of the HiFi DSP. > + * According to hw specs only 32-bits writes are allowed. > + */ > +static int imx_dsp_rproc_memset(void *addr, u8 value, size_t size) > +{ > + u32 affected_mask; > + u32 tmp_val = value; > + u32 *tmp_dst = addr; > + u32 tmp; > + int q, r; > + > + /* destination must be 32bit aligned */ > + if (!IS_ALIGNED((uintptr_t)addr, 4)) > + return -EINVAL; > + Same as above > + tmp_val |= tmp_val << 8; > + tmp_val |= tmp_val << 16; > + > + q = size / 4; > + r = size % 4; > + > + while (q--) > + iowrite32(tmp_val, tmp_dst++); > + This proves my point about __iowrite32_copy() above. > + if (r) { > + affected_mask = (1 << (8 * r)) - 1; Please use GENMASK() > + > + /* first read the 32bit data of addr, then change affected > + * bytes, and write back to addr. > + * For unaffected bytes, it should not be changed > + */ Wrong multi-line comment format. > + tmp = ioread32(tmp_dst); readl(); > + tmp &= ~affected_mask; > + > + tmp |= (tmp_val & affected_mask); > + iowrite32(tmp, tmp_dst); writel(); Thanks, Mathieu > + } > + > + return 0; > +} > +/** > + * imx_dsp_rproc_elf_load_segments() - load firmware segments to memory > + * @rproc: remote processor which will be booted using these fw segments > + * @fw: the ELF firmware image > + * > + * This function loads the firmware segments to memory, where the remote > + * processor expects them. > + * > + * Return: 0 on success and an appropriate error code otherwise > + */ > +static int imx_dsp_rproc_elf_load_segments(struct rproc *rproc, const struct firmware *fw) > +{ > + struct device *dev = &rproc->dev; > + const void *ehdr, *phdr; > + int i, ret = 0; > + u16 phnum; > + const u8 *elf_data = fw->data; > + u8 class = fw_elf_get_class(fw); > + u32 elf_phdr_get_size = elf_size_of_phdr(class); > + > + ehdr = elf_data; > + phnum = elf_hdr_get_e_phnum(class, ehdr); > + phdr = elf_data + elf_hdr_get_e_phoff(class, ehdr); > + > + /* go through the available ELF segments */ > + for (i = 0; i < phnum; i++, phdr += elf_phdr_get_size) { > + u64 da = elf_phdr_get_p_paddr(class, phdr); > + u64 memsz = elf_phdr_get_p_memsz(class, phdr); > + u64 filesz = elf_phdr_get_p_filesz(class, phdr); > + u64 offset = elf_phdr_get_p_offset(class, phdr); > + u32 type = elf_phdr_get_p_type(class, phdr); > + void *ptr; > + > + if (type != PT_LOAD || !memsz) > + continue; > + > + dev_dbg(dev, "phdr: type %d da 0x%llx memsz 0x%llx filesz 0x%llx\n", > + type, da, memsz, filesz); > + > + if (filesz > memsz) { > + dev_err(dev, "bad phdr filesz 0x%llx memsz 0x%llx\n", > + filesz, memsz); > + ret = -EINVAL; > + break; > + } > + > + if (offset + filesz > fw->size) { > + dev_err(dev, "truncated fw: need 0x%llx avail 0x%zx\n", > + offset + filesz, fw->size); > + ret = -EINVAL; > + break; > + } > + > + if (!rproc_u64_fit_in_size_t(memsz)) { > + dev_err(dev, "size (%llx) does not fit in size_t type\n", > + memsz); > + ret = -EOVERFLOW; > + break; > + } > + > + /* grab the kernel address for this device address */ > + ptr = rproc_da_to_va(rproc, da, memsz, NULL); > + if (!ptr) { > + dev_err(dev, "bad phdr da 0x%llx mem 0x%llx\n", da, > + memsz); > + ret = -EINVAL; > + break; > + } > + > + /* put the segment where the remote processor expects it */ > + if (filesz) { > + ret = imx_dsp_rproc_memcpy(ptr, elf_data + offset, filesz); > + if (ret) { > + dev_err(dev, "memory copy failed for da 0x%llx memsz 0x%llx\n", > + da, memsz); > + break; > + } > + } > + > + /* zero out remaining memory for this segment */ > + if (memsz > filesz) { > + ret = imx_dsp_rproc_memset(ptr + filesz, 0, memsz - filesz); > + if (ret) { > + dev_err(dev, "memset failed for da 0x%llx memsz 0x%llx\n", > + da, memsz); > + break; > + } > + } > + } > + > + return ret; > +} > + > static int imx_dsp_rproc_parse_fw(struct rproc *rproc, const struct firmware *fw) > { > if (rproc_elf_load_rsc_table(rproc, fw)) > @@ -729,7 +908,7 @@ static const struct rproc_ops imx_dsp_rproc_ops = { > .start = imx_dsp_rproc_start, > .stop = imx_dsp_rproc_stop, > .kick = imx_dsp_rproc_kick, > - .load = rproc_elf_load_segments, > + .load = imx_dsp_rproc_elf_load_segments, > .parse_fw = imx_dsp_rproc_parse_fw, > .sanity_check = rproc_elf_sanity_check, > .get_boot_addr = rproc_elf_get_boot_addr, > -- > 2.17.1 > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel