]> git.itanic.dy.fi Git - linux-stable/commitdiff
net: ipa: DMA addresses are nicely aligned
authorAlex Elder <elder@linaro.org>
Sun, 28 Mar 2021 17:31:10 +0000 (12:31 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 28 Sep 2022 09:10:34 +0000 (11:10 +0200)
[ Upstream commit 19aaf72c0c7a26ab7ffc655a6d84da6a379f899b ]

A recent patch avoided doing 64-bit modulo operations by checking
the alignment of some DMA allocations using only the lower 32 bits
of the address.

David Laight pointed out (after the fix was committed) that DMA
allocations might already satisfy the alignment requirements.  And
he was right.

Remove the alignment checks that occur after DMA allocation requests,
and update comments to explain why the constraint is satisfied.  The
only place IPA_TABLE_ALIGN was used was to check the alignment; it is
therefore no longer needed, so get rid of it.

Add comments where GSI_RING_ELEMENT_SIZE and the tre_count and
event_count channel data fields are defined to make explicit they
are required to be powers of 2.

Revise a comment in gsi_trans_pool_init_dma(), taking into account
that dma_alloc_coherent() guarantees its result is aligned to a page
size (or order thereof).

Don't bother printing an error if a DMA allocation fails.

Suggested-by: David Laight <David.Laight@ACULAB.COM>
Signed-off-by: Alex Elder <elder@linaro.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
Stable-dep-of: cf412ec33325 ("net: ipa: properly limit modem routing table use")
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/net/ipa/gsi.c
drivers/net/ipa/gsi_private.h
drivers/net/ipa/gsi_trans.c
drivers/net/ipa/ipa_data.h
drivers/net/ipa/ipa_table.c

index e46d3622f9eb93f2ae978ea4fb3b3113e2332ee4..64b12e462765e12af9638a0e103583ea59ad72f9 100644 (file)
@@ -1256,18 +1256,13 @@ static int gsi_ring_alloc(struct gsi *gsi, struct gsi_ring *ring, u32 count)
        dma_addr_t addr;
 
        /* Hardware requires a 2^n ring size, with alignment equal to size.
-        * The size is a power of 2, so we can check alignment using just
-        * the bottom 32 bits for a DMA address of any size.
+        * The DMA address returned by dma_alloc_coherent() is guaranteed to
+        * be a power-of-2 number of pages, which satisfies the requirement.
         */
        ring->virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
-       if (ring->virt && lower_32_bits(addr) % size) {
-               dma_free_coherent(dev, size, ring->virt, addr);
-               dev_err(dev, "unable to alloc 0x%x-aligned ring buffer\n",
-                       size);
-               return -EINVAL; /* Not a good error value, but distinct */
-       } else if (!ring->virt) {
+       if (!ring->virt)
                return -ENOMEM;
-       }
+
        ring->addr = addr;
        ring->count = count;
 
index 1785c9d3344d1c9ae4bab19c926ef984a7c6ac71..d58dce46e061a6e49c7f1e975ad656c4bf18dc10 100644 (file)
@@ -14,7 +14,7 @@ struct gsi_trans;
 struct gsi_ring;
 struct gsi_channel;
 
-#define GSI_RING_ELEMENT_SIZE  16      /* bytes */
+#define GSI_RING_ELEMENT_SIZE  16      /* bytes; must be a power of 2 */
 
 /* Return the entry that follows one provided in a transaction pool */
 void *gsi_trans_pool_next(struct gsi_trans_pool *pool, void *element);
index 6c3ed5b17b80c42f2f7d30b32867e162a1653429..70c2b585f98d64f4594b485a0a1a0074268b544b 100644 (file)
@@ -153,11 +153,10 @@ int gsi_trans_pool_init_dma(struct device *dev, struct gsi_trans_pool *pool,
        size = __roundup_pow_of_two(size);
        total_size = (count + max_alloc - 1) * size;
 
-       /* The allocator will give us a power-of-2 number of pages.  But we
-        * can't guarantee that, so request it.  That way we won't waste any
-        * memory that would be available beyond the required space.
-        *
-        * Note that gsi_trans_pool_exit_dma() assumes the total allocated
+       /* The allocator will give us a power-of-2 number of pages
+        * sufficient to satisfy our request.  Round up our requested
+        * size to avoid any unused space in the allocation.  This way
+        * gsi_trans_pool_exit_dma() can assume the total allocated
         * size is exactly (count * size).
         */
        total_size = get_order(total_size) << PAGE_SHIFT;
index 7fc1058a5ca936e60f38a0bb6aab189f3521fedf..ba05e26c3c60e8e35d4f900eb4355587e8ca75fc 100644 (file)
@@ -72,8 +72,8 @@
  * that can be included in a single transaction.
  */
 struct gsi_channel_data {
-       u16 tre_count;
-       u16 event_count;
+       u16 tre_count;                  /* must be a power of 2 */
+       u16 event_count;                /* must be a power of 2 */
        u8 tlv_count;
 };
 
index 4f15391aad5fffee2e72d6f887fc29b0eb8ca86e..087bcae29cc7c16aa355760491f428f4ffcda85b 100644 (file)
@@ -96,9 +96,6 @@
  *                 ----------------------
  */
 
-/* IPA hardware constrains filter and route tables alignment */
-#define IPA_TABLE_ALIGN                        128     /* Minimum table alignment */
-
 /* Assignment of route table entries to the modem and AP */
 #define IPA_ROUTE_MODEM_MIN            0
 #define IPA_ROUTE_MODEM_COUNT          8
@@ -656,26 +653,17 @@ int ipa_table_init(struct ipa *ipa)
 
        ipa_table_validate_build();
 
+       /* The IPA hardware requires route and filter table rules to be
+        * aligned on a 128-byte boundary.  We put the "zero rule" at the
+        * base of the table area allocated here.  The DMA address returned
+        * by dma_alloc_coherent() is guaranteed to be a power-of-2 number
+        * of pages, which satisfies the rule alignment requirement.
+        */
        size = IPA_ZERO_RULE_SIZE + (1 + count) * IPA_TABLE_ENTRY_SIZE;
        virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
        if (!virt)
                return -ENOMEM;
 
-       /* We put the "zero rule" at the base of our table area.  The IPA
-        * hardware requires route and filter table rules to be aligned
-        * on a 128-byte boundary.  As long as the alignment constraint
-        * is a power of 2, we can check alignment using just the bottom
-        * 32 bits for a DMA address of any size.
-        */
-       BUILD_BUG_ON(!is_power_of_2(IPA_TABLE_ALIGN));
-       if (lower_32_bits(addr) % IPA_TABLE_ALIGN) {
-               dev_err(dev, "table address %pad not %u-byte aligned\n",
-                       &addr, IPA_TABLE_ALIGN);
-               dma_free_coherent(dev, size, virt, addr);
-
-               return -ERANGE;
-       }
-
        ipa->table_virt = virt;
        ipa->table_addr = addr;