From owner-svn-src-all@freebsd.org Sun Dec 29 04:26:10 2019 Return-Path: Delivered-To: svn-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id D534C1D3D68; Sun, 29 Dec 2019 04:26:10 +0000 (UTC) (envelope-from philip@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 47lnYt4GxFz461v; Sun, 29 Dec 2019 04:26:10 +0000 (UTC) (envelope-from philip@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8DF89C1E8; Sun, 29 Dec 2019 04:26:10 +0000 (UTC) (envelope-from philip@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id xBT4QAKW097524; Sun, 29 Dec 2019 04:26:10 GMT (envelope-from philip@FreeBSD.org) Received: (from philip@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id xBT4QAp0097523; Sun, 29 Dec 2019 04:26:10 GMT (envelope-from philip@FreeBSD.org) Message-Id: <201912290426.xBT4QAp0097523@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: philip set sender to philip@FreeBSD.org using -f From: Philip Paeps Date: Sun, 29 Dec 2019 04:26:10 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r356166 - head/sys/riscv/sifive X-SVN-Group: head X-SVN-Commit-Author: philip X-SVN-Commit-Paths: head/sys/riscv/sifive X-SVN-Commit-Revision: 356166 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 29 Dec 2019 04:26:10 -0000 Author: philip Date: Sun Dec 29 04:26:10 2019 New Revision: 356166 URL: https://svnweb.freebsd.org/changeset/base/356166 Log: fuspi: Fix 'sleepable after non-sleepable' lock With WITNESS enabled we see the following warning: lock order reversal: (sleepable after non-sleepable) 1st 0xffffffd0847c7210 fu540spi0 (fu540spi0) @ /usr/home/kp/axiado/hornet-freebsd/src/sys/riscv/sifive/fu540_spi.c:297 2nd 0xffffffc00372bb30 Clock topology lock (Clock topology lock) @ /usr/home/kp/axiado/hornet-freebsd/src/sys/dev/extres/clk/clk.c:1137 stack backtrace: #0 0xffffffc0002a579e at witness_checkorder+0xb72 #1 0xffffffc0002a5556 at witness_checkorder+0x92a #2 0xffffffc000254c7a at _sx_slock_int+0x66 #3 0xffffffc00025537a at _sx_slock+0x8 #4 0xffffffc000123022 at clk_get_freq+0x38 #5 0xffffffc0005463e4 at __clzdi2+0x2bb8 #6 0xffffffc00014af58 at randomdev_getkey+0x76e #7 0xffffffc0001278b0 at simplebus_add_device+0x7ee #8 0xffffffc00027c9a8 at device_attach+0x2e6 #9 0xffffffc00027c634 at device_probe_and_attach+0x7a #10 0xffffffc00027d76a at bus_generic_attach+0x10 #11 0xffffffc00014aab0 at randomdev_getkey+0x2c6 #12 0xffffffc00027c9a8 at device_attach+0x2e6 #13 0xffffffc00027c634 at device_probe_and_attach+0x7a #14 0xffffffc00027d76a at bus_generic_attach+0x10 #15 0xffffffc000278bd2 at config_intrhook_oneshot+0x52 #16 0xffffffc000278b3e at config_intrhook_establish+0x146 #17 0xffffffc000278cf2 at config_intrhook_disestablish+0xfe The clock topology lock can sleep, which means we cannot attempt to acquire it while holding the non-sleepable mutex. Fix that by retrieving the clock speed once, during attach and not every time during SPI transaction setup. Submitted by: kp Sponsored by: Axiado Modified: head/sys/riscv/sifive/fu540_spi.c Modified: head/sys/riscv/sifive/fu540_spi.c ============================================================================== --- head/sys/riscv/sifive/fu540_spi.c Sun Dec 29 02:12:18 2019 (r356165) +++ head/sys/riscv/sifive/fu540_spi.c Sun Dec 29 04:26:10 2019 (r356166) @@ -80,6 +80,7 @@ struct fuspi_softc { void *ih; clk_t clk; + uint64_t freq; uint32_t cs_max; }; @@ -207,22 +208,14 @@ fuspi_setup(struct fuspi_softc *sc, uint32_t cs, uint3 uint32_t freq) { uint32_t csmode, fmt, sckdiv, sckmode; - uint64_t clock; - int ret; FUSPI_ASSERT_LOCKED(sc); - ret = clk_get_freq(sc->clk, &clock); - if (ret) { - device_printf(sc->dev, "Cannot get clock frequency: %d\n", ret); - return (ret); - } - /* * Fsck = Fin / 2 * (div + 1) * -> div = Fin / (2 * Fsck) - 1 */ - sckdiv = (howmany(clock >> 1, freq) - 1) & FUSPI_SCKDIV_MASK; + sckdiv = (howmany(sc->freq >> 1, freq) - 1) & FUSPI_SCKDIV_MASK; FUSPI_WRITE(sc, FUSPI_REG_SCKDIV, sckdiv); switch (mode) { @@ -328,6 +321,12 @@ fuspi_attach(device_t dev) error = clk_enable(sc->clk); if (error) { device_printf(dev, "Couldn't enable clock: %d\n", error); + goto fail; + } + + error = clk_get_freq(sc->clk, &sc->freq); + if (error) { + device_printf(sc->dev, "Couldn't get frequency: %d\n", error); goto fail; }