From patchwork Thu Aug 25 04:44:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Heiner Kallweit X-Patchwork-Id: 9298677 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2EF21608A7 for ; Thu, 25 Aug 2016 04:57:33 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1DBDD291C0 for ; Thu, 25 Aug 2016 04:57:33 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 127EE291C7; Thu, 25 Aug 2016 04:57:33 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 696B7291C5 for ; Thu, 25 Aug 2016 04:57:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751611AbcHYEzz (ORCPT ); Thu, 25 Aug 2016 00:55:55 -0400 Received: from mail-wm0-f68.google.com ([74.125.82.68]:36634 "EHLO mail-wm0-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751579AbcHYExL (ORCPT ); Thu, 25 Aug 2016 00:53:11 -0400 Received: by mail-wm0-f68.google.com with SMTP id i138so5622613wmf.3 for ; Wed, 24 Aug 2016 21:53:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:subject:to:references:cc:message-id:date:user-agent :mime-version:in-reply-to:content-transfer-encoding; bh=MN5ni09m4qNmEhFvO3QvlEPKNXZBWm2I7ufqk3gPefM=; b=wdKdF4rvjtY/2NTYYbVYFyphfxek/32d7dWoexvW8kK/C495PWV1WuSkBr28D6XqWZ Uvbr8e5/FILsdUVxtFQ+cn5mN+ExZiNzAlv88uEeeUotgGrxXKc1ewN6l1clk83qmOlK sat35EDpPahgZa3JJeHUKYp0RKAl2seUJu4tvidGgvnaND5D+3RiP97HweKHEtFj37PH gIHK5EwIsUiBt4aA812Mjg40waV8V59/5r0btM/gZWvXSLow8vMwep1yc0ZAAibi7Ojm 5TH35otCklFPyihOGbK+jO//PKonQy+g2vHzMgNcvbw8GyfG2L1AhHf9WCieAU/ivKQb OsJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:subject:to:references:cc:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding; bh=MN5ni09m4qNmEhFvO3QvlEPKNXZBWm2I7ufqk3gPefM=; b=XH22pQk2KnReKRKYk46l7y82sCmy5o9BGt0ys8zppUmc1iXqob7vjip/9M6wrBAJd2 4eRpCmE2JGZfr/8bRICGfPn0HAAI6x9hxBcN3xggcdeCMZwSsNK/EmWPKxlhJ/wieFXj e0MebLH4g0JkCg2a8msX/kGemHBkZ1mxcvrv2SYBTCeXI44pt7GQwqM2wwtxwK5uEz4Y kca7yn9D4GAgR3PvAtPokHOs2ANHD8qcu1VAXCY0cd8fuOMMVRFSYl1ZX+4CruXI2wYG gQaTOqPR7a3iWVomKwAL/jO3t2p5dIpAZO+JiC0Xfqs4y9mZ4XACqYCfhx8svsP+YjYL iTTg== X-Gm-Message-State: AEkoouupjgL8UxBmjg1GxqcrdY0VDQvpW9+l8SHF1P06LKNXTaSXTREDFSuF1Ue2tZ++2Q== X-Received: by 10.28.187.67 with SMTP id l64mr28438503wmf.11.1472100789553; Wed, 24 Aug 2016 21:53:09 -0700 (PDT) Received: from ?IPv6:2003:62:5f43:9500:dd82:2805:e44f:720e? (p200300625F439500DD822805E44F720E.dip0.t-ipconnect.de. [2003:62:5f43:9500:dd82:2805:e44f:720e]) by smtp.googlemail.com with ESMTPSA id g1sm13145165wjy.5.2016.08.24.21.53.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Aug 2016 21:53:08 -0700 (PDT) From: Heiner Kallweit Subject: [PATCH 02/23] spi: fsl-espi: pre-allocate message buffer To: Mark Brown References: Cc: "linux-spi@vger.kernel.org" , Gabor Juhos Message-ID: <448706fe-fd0a-7a56-b37e-0500c3ed4c18@gmail.com> Date: Thu, 25 Aug 2016 06:44:06 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 MIME-Version: 1.0 In-Reply-To: Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently the driver allocates a 64kb buffer with each single message. On systems with little and fragmented memory this can result in memory allocation errors. Solve this by pre-allocating a buffer. This patch was developed in OpenWRT long ago, however it never made it upstream. I slightly modified the original patch to re-initialize the buffer at the beginning of each transfer. Original author: Gabor Juhos Signed-off-by: Heiner Kallweit --- drivers/spi/spi-fsl-espi.c | 39 ++++++++++++++++----------------------- drivers/spi/spi-fsl-lib.h | 1 + 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/drivers/spi/spi-fsl-espi.c b/drivers/spi/spi-fsl-espi.c index d95fdd0..2c4b2f8 100644 --- a/drivers/spi/spi-fsl-espi.c +++ b/drivers/spi/spi-fsl-espi.c @@ -319,57 +319,42 @@ static void fsl_espi_do_trans(struct spi_message *m, static void fsl_espi_cmd_trans(struct spi_message *m, struct fsl_espi_transfer *trans, u8 *rx_buff) { + struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master); struct spi_transfer *t; - u8 *local_buf; int i = 0; struct fsl_espi_transfer *espi_trans = trans; - local_buf = kzalloc(SPCOM_TRANLEN_MAX, GFP_KERNEL); - if (!local_buf) { - espi_trans->status = -ENOMEM; - return; - } - list_for_each_entry(t, &m->transfers, transfer_list) { if (t->tx_buf) { - memcpy(local_buf + i, t->tx_buf, t->len); + memcpy(mspi->local_buf + i, t->tx_buf, t->len); i += t->len; } } - espi_trans->tx_buf = local_buf; - espi_trans->rx_buf = local_buf; + espi_trans->tx_buf = espi_trans->rx_buf = mspi->local_buf; fsl_espi_do_trans(m, espi_trans); espi_trans->actual_length = espi_trans->len; - kfree(local_buf); } static void fsl_espi_rw_trans(struct spi_message *m, struct fsl_espi_transfer *trans, u8 *rx_buff) { + struct mpc8xxx_spi *mspi = spi_master_get_devdata(m->spi->master); struct spi_transfer *t; - u8 *local_buf; unsigned int tx_only = 0; int i = 0; - local_buf = kzalloc(SPCOM_TRANLEN_MAX, GFP_KERNEL); - if (!local_buf) { - trans->status = -ENOMEM; - return; - } - list_for_each_entry(t, &m->transfers, transfer_list) { if (t->tx_buf) { - memcpy(local_buf + i, t->tx_buf, t->len); + memcpy(mspi->local_buf + i, t->tx_buf, t->len); i += t->len; if (!t->rx_buf) tx_only += t->len; } } - trans->tx_buf = local_buf; - trans->rx_buf = local_buf; + trans->tx_buf = trans->rx_buf = mspi->local_buf; fsl_espi_do_trans(m, trans); if (!trans->status) { @@ -379,13 +364,12 @@ static void fsl_espi_rw_trans(struct spi_message *m, trans->len - tx_only); trans->actual_length += trans->len; } - - kfree(local_buf); } static int fsl_espi_do_one_msg(struct spi_master *master, struct spi_message *m) { + struct mpc8xxx_spi *mspi = spi_master_get_devdata(master); struct spi_transfer *t; u8 *rx_buf = NULL; unsigned int n_tx = 0; @@ -393,6 +377,8 @@ static int fsl_espi_do_one_msg(struct spi_master *master, unsigned int xfer_len = 0; struct fsl_espi_transfer espi_trans; + memset(mspi->local_buf, 0, SPCOM_TRANLEN_MAX); + list_for_each_entry(t, &m->transfers, transfer_list) { if (t->tx_buf) n_tx += t->len; @@ -649,6 +635,13 @@ static struct spi_master * fsl_espi_probe(struct device *dev, mpc8xxx_spi = spi_master_get_devdata(master); + mpc8xxx_spi->local_buf = + devm_kmalloc(dev, SPCOM_TRANLEN_MAX, GFP_KERNEL); + if (!mpc8xxx_spi->local_buf) { + ret = -ENOMEM; + goto err_probe; + } + mpc8xxx_spi->reg_base = devm_ioremap_resource(dev, mem); if (IS_ERR(mpc8xxx_spi->reg_base)) { ret = PTR_ERR(mpc8xxx_spi->reg_base); diff --git a/drivers/spi/spi-fsl-lib.h b/drivers/spi/spi-fsl-lib.h index 84f5dcb..065b9db 100644 --- a/drivers/spi/spi-fsl-lib.h +++ b/drivers/spi/spi-fsl-lib.h @@ -30,6 +30,7 @@ struct mpc8xxx_spi { void *rx; #if IS_ENABLED(CONFIG_SPI_FSL_ESPI) int len; + u8 *local_buf; #endif int subblock;