1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2018-2022 Linaro Ltd. 5 */ 6 7 #include <linux/types.h> 8 #include <linux/kernel.h> 9 #include <linux/bits.h> 10 #include <linux/bitops.h> 11 #include <linux/bitfield.h> 12 #include <linux/io.h> 13 #include <linux/build_bug.h> 14 #include <linux/device.h> 15 #include <linux/dma-mapping.h> 16 17 #include "ipa.h" 18 #include "ipa_version.h" 19 #include "ipa_endpoint.h" 20 #include "ipa_table.h" 21 #include "ipa_reg.h" 22 #include "ipa_mem.h" 23 #include "ipa_cmd.h" 24 #include "gsi.h" 25 #include "gsi_trans.h" 26 27 /** 28 * DOC: IPA Filter and Route Tables 29 * 30 * The IPA has tables defined in its local (IPA-resident) memory that define 31 * filter and routing rules. An entry in either of these tables is a little 32 * endian 64-bit "slot" that holds the address of a rule definition. (The 33 * size of these slots is 64 bits regardless of the host DMA address size.) 34 * 35 * Separate tables (both filter and route) used for IPv4 and IPv6. There 36 * are normally another set of "hashed" filter and route tables, which are 37 * used with a hash of message metadata. Hashed operation is not supported 38 * by all IPA hardware (IPA v4.2 doesn't support hashed tables). 39 * 40 * Rules can be in local memory or in DRAM (system memory). The offset of 41 * an object (such as a route or filter table) in IPA-resident memory must 42 * 128-byte aligned. An object in system memory (such as a route or filter 43 * rule) must be at an 8-byte aligned address. We currently only place 44 * route or filter rules in system memory. 45 * 46 * A rule consists of a contiguous block of 32-bit values terminated with 47 * 32 zero bits. A special "zero entry" rule consisting of 64 zero bits 48 * represents "no filtering" or "no routing," and is the reset value for 49 * filter or route table rules. 50 * 51 * Each filter rule is associated with an AP or modem TX endpoint, though 52 * not all TX endpoints support filtering. The first 64-bit slot in a 53 * filter table is a bitmap indicating which endpoints have entries in 54 * the table. The low-order bit (bit 0) in this bitmap represents a 55 * special global filter, which applies to all traffic. This is not 56 * used in the current code. Bit 1, if set, indicates that there is an 57 * entry (i.e. slot containing a system address referring to a rule) for 58 * endpoint 0 in the table. Bit 3, if set, indicates there is an entry 59 * for endpoint 2, and so on. Space is set aside in IPA local memory to 60 * hold as many filter table entries as might be required, but typically 61 * they are not all used. 62 * 63 * The AP initializes all entries in a filter table to refer to a "zero" 64 * entry. Once initialized the modem and AP update the entries for 65 * endpoints they "own" directly. Currently the AP does not use the 66 * IPA filtering functionality. 67 * 68 * IPA Filter Table 69 * ---------------------- 70 * endpoint bitmap | 0x0000000000000048 | Bits 3 and 6 set (endpoints 2 and 5) 71 * |--------------------| 72 * 1st endpoint | 0x000123456789abc0 | DMA address for modem endpoint 2 rule 73 * |--------------------| 74 * 2nd endpoint | 0x000123456789abf0 | DMA address for AP endpoint 5 rule 75 * |--------------------| 76 * (unused) | | (Unused space in filter table) 77 * |--------------------| 78 * . . . 79 * |--------------------| 80 * (unused) | | (Unused space in filter table) 81 * ---------------------- 82 * 83 * The set of available route rules is divided about equally between the AP 84 * and modem. The AP initializes all entries in a route table to refer to 85 * a "zero entry". Once initialized, the modem and AP are responsible for 86 * updating their own entries. All entries in a route table are usable, 87 * though the AP currently does not use the IPA routing functionality. 88 * 89 * IPA Route Table 90 * ---------------------- 91 * 1st modem route | 0x0001234500001100 | DMA address for first route rule 92 * |--------------------| 93 * 2nd modem route | 0x0001234500001140 | DMA address for second route rule 94 * |--------------------| 95 * . . . 96 * |--------------------| 97 * Last modem route| 0x0001234500002280 | DMA address for Nth route rule 98 * |--------------------| 99 * 1st AP route | 0x0001234500001100 | DMA address for route rule (N+1) 100 * |--------------------| 101 * 2nd AP route | 0x0001234500001140 | DMA address for next route rule 102 * |--------------------| 103 * . . . 104 * |--------------------| 105 * Last AP route | 0x0001234500002280 | DMA address for last route rule 106 * ---------------------- 107 */ 108 109 /* Filter or route rules consist of a set of 32-bit values followed by a 110 * 32-bit all-zero rule list terminator. The "zero rule" is simply an 111 * all-zero rule followed by the list terminator. 112 */ 113 #define IPA_ZERO_RULE_SIZE (2 * sizeof(__le32)) 114 115 /* Check things that can be validated at build time. */ 116 static void ipa_table_validate_build(void) 117 { 118 /* Filter and route tables contain DMA addresses that refer 119 * to filter or route rules. But the size of a table entry 120 * is 64 bits regardless of what the size of an AP DMA address 121 * is. A fixed constant defines the size of an entry, and 122 * code in ipa_table_init() uses a pointer to __le64 to 123 * initialize tables. 124 */ 125 BUILD_BUG_ON(sizeof(dma_addr_t) > sizeof(__le64)); 126 127 /* A "zero rule" is used to represent no filtering or no routing. 128 * It is a 64-bit block of zeroed memory. Code in ipa_table_init() 129 * assumes that it can be written using a pointer to __le64. 130 */ 131 BUILD_BUG_ON(IPA_ZERO_RULE_SIZE != sizeof(__le64)); 132 } 133 134 static const struct ipa_mem * 135 ipa_table_mem(struct ipa *ipa, bool filter, bool hashed, bool ipv6) 136 { 137 enum ipa_mem_id mem_id; 138 139 mem_id = filter ? hashed ? ipv6 ? IPA_MEM_V6_FILTER_HASHED 140 : IPA_MEM_V4_FILTER_HASHED 141 : ipv6 ? IPA_MEM_V6_FILTER 142 : IPA_MEM_V4_FILTER 143 : hashed ? ipv6 ? IPA_MEM_V6_ROUTE_HASHED 144 : IPA_MEM_V4_ROUTE_HASHED 145 : ipv6 ? IPA_MEM_V6_ROUTE 146 : IPA_MEM_V4_ROUTE; 147 148 return ipa_mem_find(ipa, mem_id); 149 } 150 151 bool ipa_filter_map_valid(struct ipa *ipa, u32 filter_map) 152 { 153 struct device *dev = &ipa->pdev->dev; 154 u32 count; 155 156 if (!filter_map) { 157 dev_err(dev, "at least one filtering endpoint is required\n"); 158 159 return false; 160 } 161 162 count = hweight32(filter_map); 163 if (count > ipa->filter_count) { 164 dev_err(dev, "too many filtering endpoints (%u, max %u)\n", 165 count, ipa->filter_count); 166 167 return false; 168 } 169 170 return true; 171 } 172 173 /* Zero entry count means no table, so just return a 0 address */ 174 static dma_addr_t ipa_table_addr(struct ipa *ipa, bool filter_mask, u16 count) 175 { 176 u32 skip; 177 178 if (!count) 179 return 0; 180 181 WARN_ON(count > max_t(u32, ipa->filter_count, ipa->route_count)); 182 183 /* Skip over the zero rule and possibly the filter mask */ 184 skip = filter_mask ? 1 : 2; 185 186 return ipa->table_addr + skip * sizeof(*ipa->table_virt); 187 } 188 189 static void ipa_table_reset_add(struct gsi_trans *trans, bool filter, 190 u16 first, u16 count, enum ipa_mem_id mem_id) 191 { 192 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi); 193 const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id); 194 dma_addr_t addr; 195 u32 offset; 196 u16 size; 197 198 /* Nothing to do if the table memory region is empty */ 199 if (!mem->size) 200 return; 201 202 if (filter) 203 first++; /* skip over bitmap */ 204 205 offset = mem->offset + first * sizeof(__le64); 206 size = count * sizeof(__le64); 207 addr = ipa_table_addr(ipa, false, count); 208 209 ipa_cmd_dma_shared_mem_add(trans, offset, size, addr, true); 210 } 211 212 /* Reset entries in a single filter table belonging to either the AP or 213 * modem to refer to the zero entry. The memory region supplied will be 214 * for the IPv4 and IPv6 non-hashed and hashed filter tables. 215 */ 216 static int 217 ipa_filter_reset_table(struct ipa *ipa, enum ipa_mem_id mem_id, bool modem) 218 { 219 u32 ep_mask = ipa->filter_map; 220 u32 count = hweight32(ep_mask); 221 struct gsi_trans *trans; 222 enum gsi_ee_id ee_id; 223 224 trans = ipa_cmd_trans_alloc(ipa, count); 225 if (!trans) { 226 dev_err(&ipa->pdev->dev, 227 "no transaction for %s filter reset\n", 228 modem ? "modem" : "AP"); 229 return -EBUSY; 230 } 231 232 ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP; 233 while (ep_mask) { 234 u32 endpoint_id = __ffs(ep_mask); 235 struct ipa_endpoint *endpoint; 236 237 ep_mask ^= BIT(endpoint_id); 238 239 endpoint = &ipa->endpoint[endpoint_id]; 240 if (endpoint->ee_id != ee_id) 241 continue; 242 243 ipa_table_reset_add(trans, true, endpoint_id, 1, mem_id); 244 } 245 246 gsi_trans_commit_wait(trans); 247 248 return 0; 249 } 250 251 /* Theoretically, each filter table could have more filter slots to 252 * update than the maximum number of commands in a transaction. So 253 * we do each table separately. 254 */ 255 static int ipa_filter_reset(struct ipa *ipa, bool modem) 256 { 257 int ret; 258 259 ret = ipa_filter_reset_table(ipa, IPA_MEM_V4_FILTER, modem); 260 if (ret) 261 return ret; 262 263 ret = ipa_filter_reset_table(ipa, IPA_MEM_V4_FILTER_HASHED, modem); 264 if (ret) 265 return ret; 266 267 ret = ipa_filter_reset_table(ipa, IPA_MEM_V6_FILTER, modem); 268 if (ret) 269 return ret; 270 ret = ipa_filter_reset_table(ipa, IPA_MEM_V6_FILTER_HASHED, modem); 271 272 return ret; 273 } 274 275 /* The AP routes and modem routes are each contiguous within the 276 * table. We can update each table with a single command, and we 277 * won't exceed the per-transaction command limit. 278 * */ 279 static int ipa_route_reset(struct ipa *ipa, bool modem) 280 { 281 u32 modem_route_count = ipa->modem_route_count; 282 struct gsi_trans *trans; 283 u16 first; 284 u16 count; 285 286 trans = ipa_cmd_trans_alloc(ipa, 4); 287 if (!trans) { 288 dev_err(&ipa->pdev->dev, 289 "no transaction for %s route reset\n", 290 modem ? "modem" : "AP"); 291 return -EBUSY; 292 } 293 294 if (modem) { 295 first = 0; 296 count = modem_route_count; 297 } else { 298 first = modem_route_count; 299 count = ipa->route_count - modem_route_count; 300 } 301 302 ipa_table_reset_add(trans, false, first, count, IPA_MEM_V4_ROUTE); 303 ipa_table_reset_add(trans, false, first, count, 304 IPA_MEM_V4_ROUTE_HASHED); 305 306 ipa_table_reset_add(trans, false, first, count, IPA_MEM_V6_ROUTE); 307 ipa_table_reset_add(trans, false, first, count, 308 IPA_MEM_V6_ROUTE_HASHED); 309 310 gsi_trans_commit_wait(trans); 311 312 return 0; 313 } 314 315 void ipa_table_reset(struct ipa *ipa, bool modem) 316 { 317 struct device *dev = &ipa->pdev->dev; 318 const char *ee_name; 319 int ret; 320 321 ee_name = modem ? "modem" : "AP"; 322 323 /* Report errors, but reset filter and route tables */ 324 ret = ipa_filter_reset(ipa, modem); 325 if (ret) 326 dev_err(dev, "error %d resetting filter table for %s\n", 327 ret, ee_name); 328 329 ret = ipa_route_reset(ipa, modem); 330 if (ret) 331 dev_err(dev, "error %d resetting route table for %s\n", 332 ret, ee_name); 333 } 334 335 int ipa_table_hash_flush(struct ipa *ipa) 336 { 337 const struct ipa_reg *reg; 338 struct gsi_trans *trans; 339 u32 offset; 340 u32 val; 341 342 if (!ipa_table_hash_support(ipa)) 343 return 0; 344 345 trans = ipa_cmd_trans_alloc(ipa, 1); 346 if (!trans) { 347 dev_err(&ipa->pdev->dev, "no transaction for hash flush\n"); 348 return -EBUSY; 349 } 350 351 reg = ipa_reg(ipa, FILT_ROUT_HASH_FLUSH); 352 offset = ipa_reg_offset(reg); 353 354 val = ipa_reg_bit(reg, IPV6_ROUTER_HASH); 355 val |= ipa_reg_bit(reg, IPV6_FILTER_HASH); 356 val |= ipa_reg_bit(reg, IPV4_ROUTER_HASH); 357 val |= ipa_reg_bit(reg, IPV4_FILTER_HASH); 358 359 ipa_cmd_register_write_add(trans, offset, val, val, false); 360 361 gsi_trans_commit_wait(trans); 362 363 return 0; 364 } 365 366 static void ipa_table_init_add(struct gsi_trans *trans, bool filter, 367 enum ipa_cmd_opcode opcode, 368 enum ipa_mem_id mem_id, 369 enum ipa_mem_id hash_mem_id) 370 { 371 struct ipa *ipa = container_of(trans->gsi, struct ipa, gsi); 372 const struct ipa_mem *hash_mem = ipa_mem_find(ipa, hash_mem_id); 373 const struct ipa_mem *mem = ipa_mem_find(ipa, mem_id); 374 dma_addr_t hash_addr; 375 dma_addr_t addr; 376 u32 zero_offset; 377 u16 hash_count; 378 u32 zero_size; 379 u16 hash_size; 380 u16 count; 381 u16 size; 382 383 /* Compute the number of table entries to initialize */ 384 if (filter) { 385 /* The number of filtering endpoints determines number of 386 * entries in the filter table; we also add one more "slot" 387 * to hold the bitmap itself. The size of the hashed filter 388 * table is either the same as the non-hashed one, or zero. 389 */ 390 count = 1 + hweight32(ipa->filter_map); 391 hash_count = hash_mem->size ? count : 0; 392 } else { 393 /* The size of a route table region determines the number 394 * of entries it has. 395 */ 396 count = mem->size / sizeof(__le64); 397 hash_count = hash_mem->size / sizeof(__le64); 398 } 399 size = count * sizeof(__le64); 400 hash_size = hash_count * sizeof(__le64); 401 402 addr = ipa_table_addr(ipa, filter, count); 403 hash_addr = ipa_table_addr(ipa, filter, hash_count); 404 405 ipa_cmd_table_init_add(trans, opcode, size, mem->offset, addr, 406 hash_size, hash_mem->offset, hash_addr); 407 if (!filter) 408 return; 409 410 /* Zero the unused space in the filter table */ 411 zero_offset = mem->offset + size; 412 zero_size = mem->size - size; 413 ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size, 414 ipa->zero_addr, true); 415 if (!hash_size) 416 return; 417 418 /* Zero the unused space in the hashed filter table */ 419 zero_offset = hash_mem->offset + hash_size; 420 zero_size = hash_mem->size - hash_size; 421 ipa_cmd_dma_shared_mem_add(trans, zero_offset, zero_size, 422 ipa->zero_addr, true); 423 } 424 425 int ipa_table_setup(struct ipa *ipa) 426 { 427 struct gsi_trans *trans; 428 429 /* We will need at most 8 TREs: 430 * - IPv4: 431 * - One for route table initialization (non-hashed and hashed) 432 * - One for filter table initialization (non-hashed and hashed) 433 * - One to zero unused entries in the non-hashed filter table 434 * - One to zero unused entries in the hashed filter table 435 * - IPv6: 436 * - One for route table initialization (non-hashed and hashed) 437 * - One for filter table initialization (non-hashed and hashed) 438 * - One to zero unused entries in the non-hashed filter table 439 * - One to zero unused entries in the hashed filter table 440 * All platforms support at least 8 TREs in a transaction. 441 */ 442 trans = ipa_cmd_trans_alloc(ipa, 8); 443 if (!trans) { 444 dev_err(&ipa->pdev->dev, "no transaction for table setup\n"); 445 return -EBUSY; 446 } 447 448 ipa_table_init_add(trans, false, IPA_CMD_IP_V4_ROUTING_INIT, 449 IPA_MEM_V4_ROUTE, IPA_MEM_V4_ROUTE_HASHED); 450 451 ipa_table_init_add(trans, false, IPA_CMD_IP_V6_ROUTING_INIT, 452 IPA_MEM_V6_ROUTE, IPA_MEM_V6_ROUTE_HASHED); 453 454 ipa_table_init_add(trans, true, IPA_CMD_IP_V4_FILTER_INIT, 455 IPA_MEM_V4_FILTER, IPA_MEM_V4_FILTER_HASHED); 456 457 ipa_table_init_add(trans, true, IPA_CMD_IP_V6_FILTER_INIT, 458 IPA_MEM_V6_FILTER, IPA_MEM_V6_FILTER_HASHED); 459 460 gsi_trans_commit_wait(trans); 461 462 return 0; 463 } 464 465 /** 466 * ipa_filter_tuple_zero() - Zero an endpoint's hashed filter tuple 467 * @endpoint: Endpoint whose filter hash tuple should be zeroed 468 * 469 * Endpoint must be for the AP (not modem) and support filtering. Updates 470 * the filter hash values without changing route ones. 471 */ 472 static void ipa_filter_tuple_zero(struct ipa_endpoint *endpoint) 473 { 474 u32 endpoint_id = endpoint->endpoint_id; 475 struct ipa *ipa = endpoint->ipa; 476 const struct ipa_reg *reg; 477 u32 offset; 478 u32 val; 479 480 reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG); 481 482 offset = ipa_reg_n_offset(reg, endpoint_id); 483 val = ioread32(endpoint->ipa->reg_virt + offset); 484 485 /* Zero all filter-related fields, preserving the rest */ 486 val &= ~ipa_reg_fmask(reg, FILTER_HASH_MSK_ALL); 487 488 iowrite32(val, endpoint->ipa->reg_virt + offset); 489 } 490 491 /* Configure a hashed filter table; there is no ipa_filter_deconfig() */ 492 static void ipa_filter_config(struct ipa *ipa, bool modem) 493 { 494 enum gsi_ee_id ee_id = modem ? GSI_EE_MODEM : GSI_EE_AP; 495 u32 ep_mask = ipa->filter_map; 496 497 if (!ipa_table_hash_support(ipa)) 498 return; 499 500 while (ep_mask) { 501 u32 endpoint_id = __ffs(ep_mask); 502 struct ipa_endpoint *endpoint; 503 504 ep_mask ^= BIT(endpoint_id); 505 506 endpoint = &ipa->endpoint[endpoint_id]; 507 if (endpoint->ee_id == ee_id) 508 ipa_filter_tuple_zero(endpoint); 509 } 510 } 511 512 static bool ipa_route_id_modem(struct ipa *ipa, u32 route_id) 513 { 514 return route_id < ipa->modem_route_count; 515 } 516 517 /** 518 * ipa_route_tuple_zero() - Zero a hashed route table entry tuple 519 * @ipa: IPA pointer 520 * @route_id: Route table entry whose hash tuple should be zeroed 521 * 522 * Updates the route hash values without changing filter ones. 523 */ 524 static void ipa_route_tuple_zero(struct ipa *ipa, u32 route_id) 525 { 526 const struct ipa_reg *reg; 527 u32 offset; 528 u32 val; 529 530 reg = ipa_reg(ipa, ENDP_FILTER_ROUTER_HSH_CFG); 531 offset = ipa_reg_n_offset(reg, route_id); 532 533 val = ioread32(ipa->reg_virt + offset); 534 535 /* Zero all route-related fields, preserving the rest */ 536 val &= ~ipa_reg_fmask(reg, ROUTER_HASH_MSK_ALL); 537 538 iowrite32(val, ipa->reg_virt + offset); 539 } 540 541 /* Configure a hashed route table; there is no ipa_route_deconfig() */ 542 static void ipa_route_config(struct ipa *ipa, bool modem) 543 { 544 u32 route_id; 545 546 if (!ipa_table_hash_support(ipa)) 547 return; 548 549 for (route_id = 0; route_id < ipa->route_count; route_id++) 550 if (ipa_route_id_modem(ipa, route_id) == modem) 551 ipa_route_tuple_zero(ipa, route_id); 552 } 553 554 /* Configure a filter and route tables; there is no ipa_table_deconfig() */ 555 void ipa_table_config(struct ipa *ipa) 556 { 557 ipa_filter_config(ipa, false); 558 ipa_filter_config(ipa, true); 559 ipa_route_config(ipa, false); 560 ipa_route_config(ipa, true); 561 } 562 563 /* Verify the sizes of all IPA table filter or routing table memory regions 564 * are valid. If valid, this records the size of the routing table. 565 */ 566 bool ipa_table_mem_valid(struct ipa *ipa, bool filter) 567 { 568 bool hash_support = ipa_table_hash_support(ipa); 569 const struct ipa_mem *mem_hashed; 570 const struct ipa_mem *mem_ipv4; 571 const struct ipa_mem *mem_ipv6; 572 u32 count; 573 574 /* IPv4 and IPv6 non-hashed tables are expected to be defined and 575 * have the same size. Both must have at least two entries (and 576 * would normally have more than that). 577 */ 578 mem_ipv4 = ipa_table_mem(ipa, filter, false, false); 579 if (!mem_ipv4) 580 return false; 581 582 mem_ipv6 = ipa_table_mem(ipa, filter, false, true); 583 if (!mem_ipv6) 584 return false; 585 586 if (mem_ipv4->size != mem_ipv6->size) 587 return false; 588 589 /* Compute and record the number of entries for each table type */ 590 count = mem_ipv4->size / sizeof(__le64); 591 if (count < 2) 592 return false; 593 if (filter) 594 ipa->filter_count = count - 1; /* Filter map in first entry */ 595 else 596 ipa->route_count = count; 597 598 /* Table offset and size must fit in TABLE_INIT command fields */ 599 if (!ipa_cmd_table_init_valid(ipa, mem_ipv4, !filter)) 600 return false; 601 602 /* Make sure the regions are big enough */ 603 if (filter) { 604 /* Filter tables must able to hold the endpoint bitmap plus 605 * an entry for each endpoint that supports filtering 606 */ 607 if (count < 1 + hweight32(ipa->filter_map)) 608 return false; 609 } else { 610 /* Routing tables must be able to hold all modem entries, 611 * plus at least one entry for the AP. 612 */ 613 if (count < ipa->modem_route_count + 1) 614 return false; 615 } 616 617 /* If hashing is supported, hashed tables are expected to be defined, 618 * and have the same size as non-hashed tables. If hashing is not 619 * supported, hashed tables are expected to have zero size (or not 620 * be defined). 621 */ 622 mem_hashed = ipa_table_mem(ipa, filter, true, false); 623 if (hash_support) { 624 if (!mem_hashed || mem_hashed->size != mem_ipv4->size) 625 return false; 626 } else { 627 if (mem_hashed && mem_hashed->size) 628 return false; 629 } 630 631 /* Same check for IPv6 tables */ 632 mem_hashed = ipa_table_mem(ipa, filter, true, true); 633 if (hash_support) { 634 if (!mem_hashed || mem_hashed->size != mem_ipv6->size) 635 return false; 636 } else { 637 if (mem_hashed && mem_hashed->size) 638 return false; 639 } 640 641 return true; 642 } 643 644 /* Initialize a coherent DMA allocation containing initialized filter and 645 * route table data. This is used when initializing or resetting the IPA 646 * filter or route table. 647 * 648 * The first entry in a filter table contains a bitmap indicating which 649 * endpoints contain entries in the table. In addition to that first entry, 650 * there is a fixed maximum number of entries that follow. Filter table 651 * entries are 64 bits wide, and (other than the bitmap) contain the DMA 652 * address of a filter rule. A "zero rule" indicates no filtering, and 653 * consists of 64 bits of zeroes. When a filter table is initialized (or 654 * reset) its entries are made to refer to the zero rule. 655 * 656 * Each entry in a route table is the DMA address of a routing rule. For 657 * routing there is also a 64-bit "zero rule" that means no routing, and 658 * when a route table is initialized or reset, its entries are made to refer 659 * to the zero rule. The zero rule is shared for route and filter tables. 660 * 661 * Note that the IPA hardware requires a filter or route rule address to be 662 * aligned on a 128 byte boundary. The coherent DMA buffer we allocate here 663 * has a minimum alignment, and we place the zero rule at the base of that 664 * allocated space. In ipa_table_init() we verify the minimum DMA allocation 665 * meets our requirement. 666 * 667 * +-------------------+ 668 * --> | zero rule | 669 * / |-------------------| 670 * | | filter mask | 671 * |\ |-------------------| 672 * | ---- zero rule address | \ 673 * |\ |-------------------| | 674 * | ---- zero rule address | | Max IPA filter count 675 * | |-------------------| > or IPA route count, 676 * | ... | whichever is greater 677 * \ |-------------------| | 678 * ---- zero rule address | / 679 * +-------------------+ 680 */ 681 int ipa_table_init(struct ipa *ipa) 682 { 683 struct device *dev = &ipa->pdev->dev; 684 dma_addr_t addr; 685 __le64 le_addr; 686 __le64 *virt; 687 size_t size; 688 u32 count; 689 690 ipa_table_validate_build(); 691 692 count = max_t(u32, ipa->filter_count, ipa->route_count); 693 694 /* The IPA hardware requires route and filter table rules to be 695 * aligned on a 128-byte boundary. We put the "zero rule" at the 696 * base of the table area allocated here. The DMA address returned 697 * by dma_alloc_coherent() is guaranteed to be a power-of-2 number 698 * of pages, which satisfies the rule alignment requirement. 699 */ 700 size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64); 701 virt = dma_alloc_coherent(dev, size, &addr, GFP_KERNEL); 702 if (!virt) 703 return -ENOMEM; 704 705 ipa->table_virt = virt; 706 ipa->table_addr = addr; 707 708 /* First slot is the zero rule */ 709 *virt++ = 0; 710 711 /* Next is the filter table bitmap. The "soft" bitmap value 712 * must be converted to the hardware representation by shifting 713 * it left one position. (Bit 0 repesents global filtering, 714 * which is possible but not used.) 715 */ 716 *virt++ = cpu_to_le64((u64)ipa->filter_map << 1); 717 718 /* All the rest contain the DMA address of the zero rule */ 719 le_addr = cpu_to_le64(addr); 720 while (count--) 721 *virt++ = le_addr; 722 723 return 0; 724 } 725 726 void ipa_table_exit(struct ipa *ipa) 727 { 728 u32 count = max_t(u32, 1 + ipa->filter_count, ipa->route_count); 729 struct device *dev = &ipa->pdev->dev; 730 size_t size; 731 732 size = IPA_ZERO_RULE_SIZE + (1 + count) * sizeof(__le64); 733 734 dma_free_coherent(dev, size, ipa->table_virt, ipa->table_addr); 735 ipa->table_addr = 0; 736 ipa->table_virt = NULL; 737 } 738