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