Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 
0003 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved.
0004  * Copyright (C) 2018-2021 Linaro Ltd.
0005  */
0006 
0007 #include <linux/types.h>
0008 #include <linux/kernel.h>
0009 #include <linux/bits.h>
0010 #include <linux/bitops.h>
0011 #include <linux/bitfield.h>
0012 #include <linux/io.h>
0013 #include <linux/build_bug.h>
0014 #include <linux/device.h>
0015 #include <linux/dma-mapping.h>
0016 
0017 #include "ipa.h"
0018 #include "ipa_version.h"
0019 #include "ipa_endpoint.h"
0020 #include "ipa_table.h"
0021 #include "ipa_reg.h"
0022 #include "ipa_mem.h"
0023 #include "ipa_cmd.h"
0024 #include "gsi.h"
0025 #include "gsi_trans.h"
0026 
0027 /**
0028  * DOC: IPA Filter and Route Tables
0029  *
0030  * The IPA has tables defined in its local (IPA-resident) memory that define
0031  * filter and routing rules.  An entry in either of these tables is a little
0032  * endian 64-bit "slot" that holds the address of a rule definition.  (The
0033  * size of these slots is 64 bits regardless of the host DMA address size.)
0034  *
0035  * Separate tables (both filter and route) used for IPv4 and IPv6.  There
0036  * are normally another set of "hashed" filter and route tables, which are
0037  * used with a hash of message metadata.  Hashed operation is not supported
0038  * by all IPA hardware (IPA v4.2 doesn't support hashed tables).
0039  *
0040  * Rules can be in local memory or in DRAM (system memory).  The offset of
0041  * an object (such as a route or filter table) in IPA-resident memory must
0042  * 128-byte aligned.  An object in system memory (such as a route or filter
0043  * rule) must be at an 8-byte aligned address.  We currently only place
0044  * route or filter rules in system memory.
0045  *
0046  * A rule consists of a contiguous block of 32-bit values terminated with
0047  * 32 zero bits.  A special "zero entry" rule consisting of 64 zero bits
0048  * represents "no filtering" or "no routing," and is the reset value for
0049  * filter or route table rules.
0050  *
0051  * Each filter rule is associated with an AP or modem TX endpoint, though
0052  * not all TX endpoints support filtering.  The first 64-bit slot in a
0053  * filter table is a bitmap indicating which endpoints have entries in
0054  * the table.  The low-order bit (bit 0) in this bitmap represents a
0055  * special global filter, which applies to all traffic.  This is not
0056  * used in the current code.  Bit 1, if set, indicates that there is an
0057  * entry (i.e. slot containing a system address referring to a rule) for
0058  * endpoint 0 in the table.  Bit 3, if set, indicates there is an entry
0059  * for endpoint 2, and so on.  Space is set aside in IPA local memory to
0060  * hold as many filter table entries as might be required, but typically
0061  * they are not all used.
0062  *
0063  * The AP initializes all entries in a filter table to refer to a "zero"
0064  * entry.  Once initialized the modem and AP update the entries for
0065  * endpoints they "own" directly.  Currently the AP does not use the
0066  * IPA filtering functionality.
0067  *
0068  *                    IPA Filter Table
0069  *                 ----------------------
0070  * endpoint bitmap | 0x0000000000000048 | Bits 3 and 6 set (endpoints 2 and 5)
0071  *                 |--------------------|
0072  * 1st endpoint    | 0x000123456789abc0 | DMA address for modem endpoint 2 rule
0073  *                 |--------------------|
0074  * 2nd endpoint    | 0x000123456789abf0 | DMA address for AP endpoint 5 rule
0075  *                 |--------------------|
0076  * (unused)        |                    | (Unused space in filter table)
0077  *                 |--------------------|
0078  *                          . . .
0079  *                 |--------------------|
0080  * (unused)        |                    | (Unused space in filter table)
0081  *                 ----------------------
0082  *
0083  * The set of available route rules is divided about equally between the AP
0084  * and modem.  The AP initializes all entries in a route table to refer to
0085  * a "zero entry".  Once initialized, the modem and AP are responsible for
0086  * updating their own entries.  All entries in a route table are usable,
0087  * though the AP currently does not use the IPA routing functionality.
0088  *
0089  *                    IPA Route Table
0090  *                 ----------------------
0091  * 1st modem route | 0x0001234500001100 | DMA address for first route rule
0092  *                 |--------------------|
0093  * 2nd modem route | 0x0001234500001140 | DMA address for second route rule
0094  *                 |--------------------|
0095  *                          . . .
0096  *                 |--------------------|
0097  * Last modem route| 0x0001234500002280 | DMA address for Nth route rule
0098  *                 |--------------------|
0099  * 1st AP route    | 0x0001234500001100 | DMA address for route rule (N+1)
0100  *                 |--------------------|
0101  * 2nd AP route    | 0x0001234500001140 | DMA address for next route rule
0102  *                 |--------------------|
0103  *                          . . .
0104  *                 |--------------------|
0105  * Last AP route   | 0x0001234500002280 | DMA address for last route rule
0106  *                 ----------------------
0107  */
0108 
0109 /* Assignment of route table entries to the modem and AP */
0110 #define IPA_ROUTE_MODEM_MIN     0
0111 #define IPA_ROUTE_AP_MIN        IPA_ROUTE_MODEM_COUNT
0112 #define IPA_ROUTE_AP_COUNT \
0113         (IPA_ROUTE_COUNT_MAX - IPA_ROUTE_MODEM_COUNT)
0114 
0115 /* Filter or route rules consist of a set of 32-bit values followed by a
0116  * 32-bit all-zero rule list terminator.  The "zero rule" is simply an
0117  * all-zero rule followed by the list terminator.
0118  */
0119 #define IPA_ZERO_RULE_SIZE      (2 * sizeof(__le32))
0120 
0121 /* Check things that can be validated at build time. */
0122 static void ipa_table_validate_build(void)
0123 {
0124     /* Filter and route tables contain DMA addresses that refer
0125      * to filter or route rules.  But the size of a table entry
0126      * is 64 bits regardless of what the size of an AP DMA address
0127      * is.  A fixed constant defines the size of an entry, and
0128      * code in ipa_table_init() uses a pointer to __le64 to
0129      * initialize tables.
0130      */
0131     BUILD_BUG_ON(sizeof(dma_addr_t) > sizeof(__le64));
0132 
0133     /* A "zero rule" is used to represent no filtering or no routing.
0134      * It is a 64-bit block of zeroed memory.  Code in ipa_table_init()
0135      * assumes that it can be written using a pointer to __le64.
0136      */
0137     BUILD_BUG_ON(IPA_ZERO_RULE_SIZE != sizeof(__le64));
0138 
0139     /* Impose a practical limit on the number of routes */
0140     BUILD_BUG_ON(IPA_ROUTE_COUNT_MAX > 32);
0141     /* The modem must be allotted at least one route table entry */
0142     BUILD_BUG_ON(!IPA_ROUTE_MODEM_COUNT);
0143     /* But it can't have more than what is available */
0144     BUILD_BUG_ON(IPA_ROUTE_MODEM_COUNT > IPA_ROUTE_COUNT_MAX);
0145 
0146 }
0147 
0148 static bool
0149 ipa_table_valid_one(struct ipa *ipa, enum ipa_mem_id mem_id, bool route)
0150 {
0151     const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
0152     struct device *dev = &ipa->pdev->dev;
0153     u32 size;
0154 
0155     if (route)
0156         size = IPA_ROUTE_COUNT_MAX * sizeof(__le64);
0157     else
0158         size = (1 + IPA_FILTER_COUNT_MAX) * sizeof(__le64);
0159 
0160     if (!ipa_cmd_table_valid(ipa, mem, route))
0161         return false;
0162 
0163     /* mem->size >= size is sufficient, but we'll demand more */
0164     if (mem->size == size)
0165         return true;
0166 
0167     /* Hashed table regions can be zero size if hashing is not supported */
0168     if (ipa_table_hash_support(ipa) && !mem->size)
0169         return true;
0170 
0171     dev_err(dev, "%s table region %u size 0x%02x, expected 0x%02x\n",
0172         route ? "route" : "filter", mem_id, mem->size, size);
0173 
0174     return false;
0175 }
0176 
0177 /* Verify the filter and route table memory regions are the expected size */
0178 bool ipa_table_valid(struct ipa *ipa)
0179 {
0180     bool valid;
0181 
0182     valid = ipa_table_valid_one(ipa, IPA_MEM_V4_FILTER, false);
0183     valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V6_FILTER, false);
0184     valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V4_ROUTE, true);
0185     valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V6_ROUTE, true);
0186 
0187     if (!ipa_table_hash_support(ipa))
0188         return valid;
0189 
0190     valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V4_FILTER_HASHED,
0191                          false);
0192     valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V6_FILTER_HASHED,
0193                          false);
0194     valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V4_ROUTE_HASHED,
0195                          true);
0196     valid = valid && ipa_table_valid_one(ipa, IPA_MEM_V6_ROUTE_HASHED,
0197                          true);
0198 
0199     return valid;
0200 }
0201 
0202 bool ipa_filter_map_valid(struct ipa *ipa, u32 filter_map)
0203 {
0204     struct device *dev = &ipa->pdev->dev;
0205     u32 count;
0206 
0207     if (!filter_map) {
0208         dev_err(dev, "at least one filtering endpoint is required\n");
0209 
0210         return false;
0211     }
0212 
0213     count = hweight32(filter_map);
0214     if (count > IPA_FILTER_COUNT_MAX) {
0215         dev_err(dev, "too many filtering endpoints (%u, max %u)\n",
0216             count, IPA_FILTER_COUNT_MAX);
0217 
0218         return false;
0219     }
0220 
0221     return true;
0222 }
0223 
0224 /* Zero entry count means no table, so just return a 0 address */
0225 static dma_addr_t ipa_table_addr(struct ipa *ipa, bool filter_mask, u16 count)
0226 {
0227     u32 skip;
0228 
0229     if (!count)
0230         return 0;
0231 
0232     WARN_ON(count > max_t(u32, IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX));
0233 
0234     /* Skip over the zero rule and possibly the filter mask */
0235     skip = filter_mask ? 1 : 2;
0236 
0237     return ipa->table_addr + skip * sizeof(*ipa->table_virt);
0238 }
0239 
0240 static void ipa_table_reset_add(struct gsi_trans *trans, bool filter,
0241                 u16 first, u16 count, enum ipa_mem_id mem_id)
0242 {
0243     struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
0244     const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
0245     dma_addr_t addr;
0246     u32 offset;
0247     u16 size;
0248 
0249     /* Nothing to do if the table memory region is empty */
0250     if (!mem->size)
0251         return;
0252 
0253     if (filter)
0254         first++;    /* skip over bitmap */
0255 
0256     offset = mem->offset + first * sizeof(__le64);
0257     size = count * sizeof(__le64);
0258     addr = ipa_table_addr(ipa, false, count);
0259 
0260     ipa_cmd_dma_shared_mem_add(trans, offset, size, addr, true);
0261 }
0262 
0263 /* Reset entries in a single filter table belonging to either the AP or
0264  * modem to refer to the zero entry.  The memory region supplied will be
0265  * for the IPv4 and IPv6 non-hashed and hashed filter tables.
0266  */
0267 static int
0268 ipa_filter_reset_table(struct ipa *ipa, enum ipa_mem_id mem_id, bool modem)
0269 {
0270     u32 ep_mask = ipa->filter_map;
0271     u32 count = hweight32(ep_mask);
0272     struct gsi_trans *trans;
0273     enum gsi_ee_id ee_id;
0274 
0275     trans = ipa_cmd_trans_alloc(ipa, count);
0276     if (!trans) {
0277         dev_err(&ipa->pdev->dev,
0278             "no transaction for %s filter reset\n",
0279             modem ? "modem" : "AP");
0280         return -EBUSY;
0281     }
0282 
0283     ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
0284     while (ep_mask) {
0285         u32 endpoint_id = __ffs(ep_mask);
0286         struct ipa_endpoint *endpoint;
0287 
0288         ep_mask ^= BIT(endpoint_id);
0289 
0290         endpoint = &ipa->endpoint[endpoint_id];
0291         if (endpoint->ee_id != ee_id)
0292             continue;
0293 
0294         ipa_table_reset_add(trans, true, endpoint_id, 1, mem_id);
0295     }
0296 
0297     gsi_trans_commit_wait(trans);
0298 
0299     return 0;
0300 }
0301 
0302 /* Theoretically, each filter table could have more filter slots to
0303  * update than the maximum number of commands in a transaction.  So
0304  * we do each table separately.
0305  */
0306 static int ipa_filter_reset(struct ipa *ipa, bool modem)
0307 {
0308     int ret;
0309 
0310     ret = ipa_filter_reset_table(ipa, IPA_MEM_V4_FILTER, modem);
0311     if (ret)
0312         return ret;
0313 
0314     ret = ipa_filter_reset_table(ipa, IPA_MEM_V4_FILTER_HASHED, modem);
0315     if (ret)
0316         return ret;
0317 
0318     ret = ipa_filter_reset_table(ipa, IPA_MEM_V6_FILTER, modem);
0319     if (ret)
0320         return ret;
0321     ret = ipa_filter_reset_table(ipa, IPA_MEM_V6_FILTER_HASHED, modem);
0322 
0323     return ret;
0324 }
0325 
0326 /* The AP routes and modem routes are each contiguous within the
0327  * table.  We can update each table with a single command, and we
0328  * won't exceed the per-transaction command limit.
0329  * */
0330 static int ipa_route_reset(struct ipa *ipa, bool modem)
0331 {
0332     struct gsi_trans *trans;
0333     u16 first;
0334     u16 count;
0335 
0336     trans = ipa_cmd_trans_alloc(ipa, 4);
0337     if (!trans) {
0338         dev_err(&ipa->pdev->dev,
0339             "no transaction for %s route reset\n",
0340             modem ? "modem" : "AP");
0341         return -EBUSY;
0342     }
0343 
0344     if (modem) {
0345         first = IPA_ROUTE_MODEM_MIN;
0346         count = IPA_ROUTE_MODEM_COUNT;
0347     } else {
0348         first = IPA_ROUTE_AP_MIN;
0349         count = IPA_ROUTE_AP_COUNT;
0350     }
0351 
0352     ipa_table_reset_add(trans, false, first, count, IPA_MEM_V4_ROUTE);
0353     ipa_table_reset_add(trans, false, first, count,
0354                 IPA_MEM_V4_ROUTE_HASHED);
0355 
0356     ipa_table_reset_add(trans, false, first, count, IPA_MEM_V6_ROUTE);
0357     ipa_table_reset_add(trans, false, first, count,
0358                 IPA_MEM_V6_ROUTE_HASHED);
0359 
0360     gsi_trans_commit_wait(trans);
0361 
0362     return 0;
0363 }
0364 
0365 void ipa_table_reset(struct ipa *ipa, bool modem)
0366 {
0367     struct device *dev = &ipa->pdev->dev;
0368     const char *ee_name;
0369     int ret;
0370 
0371     ee_name = modem ? "modem" : "AP";
0372 
0373     /* Report errors, but reset filter and route tables */
0374     ret = ipa_filter_reset(ipa, modem);
0375     if (ret)
0376         dev_err(dev, "error %d resetting filter table for %s\n",
0377                 ret, ee_name);
0378 
0379     ret = ipa_route_reset(ipa, modem);
0380     if (ret)
0381         dev_err(dev, "error %d resetting route table for %s\n",
0382                 ret, ee_name);
0383 }
0384 
0385 int ipa_table_hash_flush(struct ipa *ipa)
0386 {
0387     u32 offset = ipa_reg_filt_rout_hash_flush_offset(ipa->version);
0388     struct gsi_trans *trans;
0389     u32 val;
0390 
0391     if (!ipa_table_hash_support(ipa))
0392         return 0;
0393 
0394     trans = ipa_cmd_trans_alloc(ipa, 1);
0395     if (!trans) {
0396         dev_err(&ipa->pdev->dev, "no transaction for hash flush\n");
0397         return -EBUSY;
0398     }
0399 
0400     val = IPV4_FILTER_HASH_FMASK | IPV6_FILTER_HASH_FMASK;
0401     val |= IPV6_ROUTER_HASH_FMASK | IPV4_ROUTER_HASH_FMASK;
0402 
0403     ipa_cmd_register_write_add(trans, offset, val, val, false);
0404 
0405     gsi_trans_commit_wait(trans);
0406 
0407     return 0;
0408 }
0409 
0410 static void ipa_table_init_add(struct gsi_trans *trans, bool filter,
0411                    enum ipa_cmd_opcode opcode,
0412                    enum ipa_mem_id mem_id,
0413                    enum ipa_mem_id hash_mem_id)
0414 {
0415     struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi);
0416     const struct ipa_mem *hash_mem = ipa_mem_find(ipa, hash_mem_id);
0417     const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id);
0418     dma_addr_t hash_addr;
0419     dma_addr_t addr;
0420     u32 zero_offset;
0421     u16 hash_count;
0422     u32 zero_size;
0423     u16 hash_size;
0424     u16 count;
0425     u16 size;
0426 
0427     /* Compute the number of table entries to initialize */
0428     if (filter) {
0429         /* The number of filtering endpoints determines number of
0430          * entries in the filter table; we also add one more "slot"
0431          * to hold the bitmap itself.  The size of the hashed filter
0432          * table is either the same as the non-hashed one, or zero.
0433          */
0434         count = 1 + hweight32(ipa->filter_map);
0435         hash_count = hash_mem->size ? count : 0;
0436     } else {
0437         /* The size of a route table region determines the number
0438          * of entries it has.
0439          */
0440         count = mem->size / sizeof(__le64);
0441         hash_count = hash_mem->size / sizeof(__le64);
0442     }
0443     size = count * sizeof(__le64);
0444     hash_size = hash_count * sizeof(__le64);
0445 
0446     addr = ipa_table_addr(ipa, filter, count);
0447     hash_addr = ipa_table_addr(ipa, filter, hash_count);
0448 
0449     ipa_cmd_table_init_add(trans, opcode, size, mem->offset, addr,
0450                    hash_size, hash_mem->offset, hash_addr);
0451     if (!filter)
0452         return;
0453 
0454     /* Zero the unused space in the filter table */
0455     zero_offset = mem->offset + size;
0456     zero_size = mem->size - size;
0457     ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
0458                    ipa->zero_addr, true);
0459     if (!hash_size)
0460         return;
0461 
0462     /* Zero the unused space in the hashed filter table */
0463     zero_offset = hash_mem->offset + hash_size;
0464     zero_size = hash_mem->size - hash_size;
0465     ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size,
0466                    ipa->zero_addr, true);
0467 }
0468 
0469 int ipa_table_setup(struct ipa *ipa)
0470 {
0471     struct gsi_trans *trans;
0472 
0473     /* We will need at most 8 TREs:
0474      * - IPv4:
0475      *     - One for route table initialization (non-hashed and hashed)
0476      *     - One for filter table initialization (non-hashed and hashed)
0477      *     - One to zero unused entries in the non-hashed filter table
0478      *     - One to zero unused entries in the hashed filter table
0479      * - IPv6:
0480      *     - One for route table initialization (non-hashed and hashed)
0481      *     - One for filter table initialization (non-hashed and hashed)
0482      *     - One to zero unused entries in the non-hashed filter table
0483      *     - One to zero unused entries in the hashed filter table
0484      * All platforms support at least 8 TREs in a transaction.
0485      */
0486     trans = ipa_cmd_trans_alloc(ipa, 8);
0487     if (!trans) {
0488         dev_err(&ipa->pdev->dev, "no transaction for table setup\n");
0489         return -EBUSY;
0490     }
0491 
0492     ipa_table_init_add(trans, false, IPA_CMD_IP_V4_ROUTING_INIT,
0493                IPA_MEM_V4_ROUTE, IPA_MEM_V4_ROUTE_HASHED);
0494 
0495     ipa_table_init_add(trans, false, IPA_CMD_IP_V6_ROUTING_INIT,
0496                IPA_MEM_V6_ROUTE, IPA_MEM_V6_ROUTE_HASHED);
0497 
0498     ipa_table_init_add(trans, true, IPA_CMD_IP_V4_FILTER_INIT,
0499                IPA_MEM_V4_FILTER, IPA_MEM_V4_FILTER_HASHED);
0500 
0501     ipa_table_init_add(trans, true, IPA_CMD_IP_V6_FILTER_INIT,
0502                IPA_MEM_V6_FILTER, IPA_MEM_V6_FILTER_HASHED);
0503 
0504     gsi_trans_commit_wait(trans);
0505 
0506     return 0;
0507 }
0508 
0509 /**
0510  * ipa_filter_tuple_zero() - Zero an endpoint's hashed filter tuple
0511  * @endpoint:   Endpoint whose filter hash tuple should be zeroed
0512  *
0513  * Endpoint must be for the AP (not modem) and support filtering. Updates
0514  * the filter hash values without changing route ones.
0515  */
0516 static void ipa_filter_tuple_zero(struct ipa_endpoint *endpoint)
0517 {
0518     u32 endpoint_id = endpoint->endpoint_id;
0519     u32 offset;
0520     u32 val;
0521 
0522     offset = IPA_REG_ENDP_FILTER_ROUTER_HSH_CFG_N_OFFSET(endpoint_id);
0523 
0524     val = ioread32(endpoint->ipa->reg_virt + offset);
0525 
0526     /* Zero all filter-related fields, preserving the rest */
0527     u32p_replace_bits(&val, 0, IPA_REG_ENDP_FILTER_HASH_MSK_ALL);
0528 
0529     iowrite32(val, endpoint->ipa->reg_virt + offset);
0530 }
0531 
0532 /* Configure a hashed filter table; there is no ipa_filter_deconfig() */
0533 static void ipa_filter_config(struct ipa *ipa, bool modem)
0534 {
0535     enum gsi_ee_id ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP;
0536     u32 ep_mask = ipa->filter_map;
0537 
0538     if (!ipa_table_hash_support(ipa))
0539         return;
0540 
0541     while (ep_mask) {
0542         u32 endpoint_id = __ffs(ep_mask);
0543         struct ipa_endpoint *endpoint;
0544 
0545         ep_mask ^= BIT(endpoint_id);
0546 
0547         endpoint = &ipa->endpoint[endpoint_id];
0548         if (endpoint->ee_id == ee_id)
0549             ipa_filter_tuple_zero(endpoint);
0550     }
0551 }
0552 
0553 static bool ipa_route_id_modem(u32 route_id)
0554 {
0555     return route_id >= IPA_ROUTE_MODEM_MIN &&
0556         route_id <= IPA_ROUTE_MODEM_MIN + IPA_ROUTE_MODEM_COUNT - 1;
0557 }
0558 
0559 /**
0560  * ipa_route_tuple_zero() - Zero a hashed route table entry tuple
0561  * @ipa:    IPA pointer
0562  * @route_id:   Route table entry whose hash tuple should be zeroed
0563  *
0564  * Updates the route hash values without changing filter ones.
0565  */
0566 static void ipa_route_tuple_zero(struct ipa *ipa, u32 route_id)
0567 {
0568     u32 offset = IPA_REG_ENDP_FILTER_ROUTER_HSH_CFG_N_OFFSET(route_id);
0569     u32 val;
0570 
0571     val = ioread32(ipa->reg_virt + offset);
0572 
0573     /* Zero all route-related fields, preserving the rest */
0574     u32p_replace_bits(&val, 0, IPA_REG_ENDP_ROUTER_HASH_MSK_ALL);
0575 
0576     iowrite32(val, ipa->reg_virt + offset);
0577 }
0578 
0579 /* Configure a hashed route table; there is no ipa_route_deconfig() */
0580 static void ipa_route_config(struct ipa *ipa, bool modem)
0581 {
0582     u32 route_id;
0583 
0584     if (!ipa_table_hash_support(ipa))
0585         return;
0586 
0587     for (route_id = 0; route_id < IPA_ROUTE_COUNT_MAX; route_id++)
0588         if (ipa_route_id_modem(route_id) == modem)
0589             ipa_route_tuple_zero(ipa, route_id);
0590 }
0591 
0592 /* Configure a filter and route tables; there is no ipa_table_deconfig() */
0593 void ipa_table_config(struct ipa *ipa)
0594 {
0595     ipa_filter_config(ipa, false);
0596     ipa_filter_config(ipa, true);
0597     ipa_route_config(ipa, false);
0598     ipa_route_config(ipa, true);
0599 }
0600 
0601 /*
0602  * Initialize a coherent DMA allocation containing initialized filter and
0603  * route table data.  This is used when initializing or resetting the IPA
0604  * filter or route table.
0605  *
0606  * The first entry in a filter table contains a bitmap indicating which
0607  * endpoints contain entries in the table.  In addition to that first entry,
0608  * there are at most IPA_FILTER_COUNT_MAX entries that follow.  Filter table
0609  * entries are 64 bits wide, and (other than the bitmap) contain the DMA
0610  * address of a filter rule.  A "zero rule" indicates no filtering, and
0611  * consists of 64 bits of zeroes.  When a filter table is initialized (or
0612  * reset) its entries are made to refer to the zero rule.
0613  *
0614  * Each entry in a route table is the DMA address of a routing rule.  For
0615  * routing there is also a 64-bit "zero rule" that means no routing, and
0616  * when a route table is initialized or reset, its entries are made to refer
0617  * to the zero rule.  The zero rule is shared for route and filter tables.
0618  *
0619  * Note that the IPA hardware requires a filter or route rule address to be
0620  * aligned on a 128 byte boundary.  The coherent DMA buffer we allocate here
0621  * has a minimum alignment, and we place the zero rule at the base of that
0622  * allocated space.  In ipa_table_init() we verify the minimum DMA allocation
0623  * meets our requirement.
0624  *
0625  *       +-------------------+
0626  *   --> |     zero rule     |
0627  *  /    |-------------------|
0628  *  |    |     filter mask   |
0629  *  |\   |-------------------|
0630  *  | ---- zero rule address | \
0631  *  |\   |-------------------|  |
0632  *  | ---- zero rule address |  |   IPA_FILTER_COUNT_MAX
0633  *  |    |-------------------|   >  or IPA_ROUTE_COUNT_MAX,
0634  *  |         ...       |   whichever is greater
0635  *   \   |-------------------|  |
0636  *    ---- zero rule address | /
0637  *       +-------------------+
0638  */
0639 int ipa_table_init(struct ipa *ipa)
0640 {
0641     u32 count = max_t(u32, IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX);
0642     struct device *dev = &ipa->pdev->dev;
0643     dma_addr_t addr;
0644     __le64 le_addr;
0645     __le64 *virt;
0646     size_t size;
0647 
0648     ipa_table_validate_build();
0649 
0650     /* The IPA hardware requires route and filter table rules to be
0651      * aligned on a 128-byte boundary.  We put the "zero rule" at the
0652      * base of the table area allocated here.  The DMA address returned
0653      * by dma_alloc_coherent() is guaranteed to be a power-of-2 number
0654      * of pages, which satisfies the rule alignment requirement.
0655      */
0656     size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
0657     virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL);
0658     if (!virt)
0659         return -ENOMEM;
0660 
0661     ipa->table_virt = virt;
0662     ipa->table_addr = addr;
0663 
0664     /* First slot is the zero rule */
0665     *virt++ = 0;
0666 
0667     /* Next is the filter table bitmap.  The "soft" bitmap value
0668      * must be converted to the hardware representation by shifting
0669      * it left one position.  (Bit 0 repesents global filtering,
0670      * which is possible but not used.)
0671      */
0672     *virt++ = cpu_to_le64((u64)ipa->filter_map << 1);
0673 
0674     /* All the rest contain the DMA address of the zero rule */
0675     le_addr = cpu_to_le64(addr);
0676     while (count--)
0677         *virt++ = le_addr;
0678 
0679     return 0;
0680 }
0681 
0682 void ipa_table_exit(struct ipa *ipa)
0683 {
0684     u32 count = max_t(u32, 1 + IPA_FILTER_COUNT_MAX, IPA_ROUTE_COUNT_MAX);
0685     struct device *dev = &ipa->pdev->dev;
0686     size_t size;
0687 
0688     size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64);
0689 
0690     dma_free_coherent(dev, size, ipa->table_virt, ipa->table_addr);
0691     ipa->table_addr = 0;
0692     ipa->table_virt = NULL;
0693 }