1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2018-2024 Linaro Ltd. 5 */ 6 7 #include <linux/types.h> 8 #include <linux/bug.h> 9 #include <linux/io.h> 10 #include <linux/firmware.h> 11 #include <linux/module.h> 12 #include <linux/of.h> 13 #include <linux/of_address.h> 14 #include <linux/platform_device.h> 15 #include <linux/pm_runtime.h> 16 #include <linux/firmware/qcom/qcom_scm.h> 17 #include <linux/soc/qcom/mdt_loader.h> 18 19 #include "ipa.h" 20 #include "ipa_power.h" 21 #include "ipa_data.h" 22 #include "ipa_endpoint.h" 23 #include "ipa_resource.h" 24 #include "ipa_cmd.h" 25 #include "ipa_reg.h" 26 #include "ipa_mem.h" 27 #include "ipa_table.h" 28 #include "ipa_smp2p.h" 29 #include "ipa_modem.h" 30 #include "ipa_uc.h" 31 #include "ipa_interrupt.h" 32 #include "ipa_sysfs.h" 33 #include "ipa_version.h" 34 35 /** 36 * DOC: The IP Accelerator 37 * 38 * This driver supports the Qualcomm IP Accelerator (IPA), which is a 39 * networking component found in many Qualcomm SoCs. The IPA is connected 40 * to the application processor (AP), but is also connected (and partially 41 * controlled by) other "execution environments" (EEs), such as a modem. 42 * 43 * The IPA is the conduit between the AP and the modem that carries network 44 * traffic. This driver presents a network interface representing the 45 * connection of the modem to external (e.g. LTE) networks. 46 * 47 * The IPA provides protocol checksum calculation, offloading this work 48 * from the AP. The IPA offers additional functionality, including routing, 49 * filtering, and NAT support, but that more advanced functionality is not 50 * currently supported. Despite that, some resources--including routing 51 * tables and filter tables--are defined in this driver because they must 52 * be initialized even when the advanced hardware features are not used. 53 * 54 * There are two distinct layers that implement the IPA hardware, and this 55 * is reflected in the organization of the driver. The generic software 56 * interface (GSI) is an integral component of the IPA, providing a 57 * well-defined communication layer between the AP subsystem and the IPA 58 * core. The GSI implements a set of "channels" used for communication 59 * between the AP and the IPA. 60 * 61 * The IPA layer uses GSI channels to implement its "endpoints". And while 62 * a GSI channel carries data between the AP and the IPA, a pair of IPA 63 * endpoints is used to carry traffic between two EEs. Specifically, the main 64 * modem network interface is implemented by two pairs of endpoints: a TX 65 * endpoint on the AP coupled with an RX endpoint on the modem; and another 66 * RX endpoint on the AP receiving data from a TX endpoint on the modem. 67 */ 68 69 /* The name of the GSI firmware file relative to /lib/firmware */ 70 #define IPA_FW_PATH_DEFAULT "ipa_fws.mdt" 71 #define IPA_PAS_ID 15 72 73 /* Shift of 19.2 MHz timestamp to achieve lower resolution timestamps */ 74 /* IPA v5.5+ does not specify Qtime timestamp config for DPL */ 75 #define DPL_TIMESTAMP_SHIFT 14 /* ~1.172 kHz, ~853 usec per tick */ 76 #define TAG_TIMESTAMP_SHIFT 14 77 #define NAT_TIMESTAMP_SHIFT 24 /* ~1.144 Hz, ~874 msec per tick */ 78 79 /* Divider for 19.2 MHz crystal oscillator clock to get common timer clock */ 80 #define IPA_XO_CLOCK_DIVIDER 192 /* 1 is subtracted where used */ 81 82 /** 83 * enum ipa_firmware_loader: How GSI firmware gets loaded 84 * 85 * @IPA_LOADER_DEFER: System not ready; try again later 86 * @IPA_LOADER_SELF: AP loads GSI firmware 87 * @IPA_LOADER_MODEM: Modem loads GSI firmware, signals when done 88 * @IPA_LOADER_SKIP: Neither AP nor modem need to load GSI firmware 89 * @IPA_LOADER_INVALID: GSI firmware loader specification is invalid 90 */ 91 enum ipa_firmware_loader { 92 IPA_LOADER_DEFER, 93 IPA_LOADER_SELF, 94 IPA_LOADER_MODEM, 95 IPA_LOADER_SKIP, 96 IPA_LOADER_INVALID, 97 }; 98 99 /** 100 * ipa_setup() - Set up IPA hardware 101 * @ipa: IPA pointer 102 * 103 * Perform initialization that requires issuing immediate commands on 104 * the command TX endpoint. If the modem is doing GSI firmware load 105 * and initialization, this function will be called when an SMP2P 106 * interrupt has been signaled by the modem. Otherwise it will be 107 * called from ipa_probe() after GSI firmware has been successfully 108 * loaded, authenticated, and started by Trust Zone. 109 */ 110 int ipa_setup(struct ipa *ipa) 111 { 112 struct ipa_endpoint *exception_endpoint; 113 struct ipa_endpoint *command_endpoint; 114 struct device *dev = ipa->dev; 115 int ret; 116 117 ret = gsi_setup(&ipa->gsi); 118 if (ret) 119 return ret; 120 121 ret = ipa_power_setup(ipa); 122 if (ret) 123 goto err_gsi_teardown; 124 125 ipa_endpoint_setup(ipa); 126 127 /* We need to use the AP command TX endpoint to perform other 128 * initialization, so we enable first. 129 */ 130 command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]; 131 ret = ipa_endpoint_enable_one(command_endpoint); 132 if (ret) 133 goto err_endpoint_teardown; 134 135 ret = ipa_mem_setup(ipa); /* No matching teardown required */ 136 if (ret) 137 goto err_command_disable; 138 139 ret = ipa_table_setup(ipa); /* No matching teardown required */ 140 if (ret) 141 goto err_command_disable; 142 143 /* Enable the exception handling endpoint, and tell the hardware 144 * to use it by default. 145 */ 146 exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]; 147 ret = ipa_endpoint_enable_one(exception_endpoint); 148 if (ret) 149 goto err_command_disable; 150 151 ipa_endpoint_default_route_set(ipa, exception_endpoint->endpoint_id); 152 153 /* We're all set. Now prepare for communication with the modem */ 154 ret = ipa_qmi_setup(ipa); 155 if (ret) 156 goto err_default_route_clear; 157 158 ipa->setup_complete = true; 159 160 dev_info(dev, "IPA driver setup completed successfully\n"); 161 162 return 0; 163 164 err_default_route_clear: 165 ipa_endpoint_default_route_clear(ipa); 166 ipa_endpoint_disable_one(exception_endpoint); 167 err_command_disable: 168 ipa_endpoint_disable_one(command_endpoint); 169 err_endpoint_teardown: 170 ipa_endpoint_teardown(ipa); 171 ipa_power_teardown(ipa); 172 err_gsi_teardown: 173 gsi_teardown(&ipa->gsi); 174 175 return ret; 176 } 177 178 /** 179 * ipa_teardown() - Inverse of ipa_setup() 180 * @ipa: IPA pointer 181 */ 182 static void ipa_teardown(struct ipa *ipa) 183 { 184 struct ipa_endpoint *exception_endpoint; 185 struct ipa_endpoint *command_endpoint; 186 187 /* We're going to tear everything down, as if setup never completed */ 188 ipa->setup_complete = false; 189 190 ipa_qmi_teardown(ipa); 191 ipa_endpoint_default_route_clear(ipa); 192 exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]; 193 ipa_endpoint_disable_one(exception_endpoint); 194 command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]; 195 ipa_endpoint_disable_one(command_endpoint); 196 ipa_endpoint_teardown(ipa); 197 ipa_power_teardown(ipa); 198 gsi_teardown(&ipa->gsi); 199 } 200 201 static void 202 ipa_hardware_config_bcr(struct ipa *ipa, const struct ipa_data *data) 203 { 204 const struct reg *reg; 205 u32 val; 206 207 /* IPA v4.5+ has no backward compatibility register */ 208 if (ipa->version >= IPA_VERSION_4_5) 209 return; 210 211 reg = ipa_reg(ipa, IPA_BCR); 212 val = data->backward_compat; 213 iowrite32(val, ipa->reg_virt + reg_offset(reg)); 214 } 215 216 static void ipa_hardware_config_tx(struct ipa *ipa) 217 { 218 enum ipa_version version = ipa->version; 219 const struct reg *reg; 220 u32 offset; 221 u32 val; 222 223 if (version <= IPA_VERSION_4_0 || version >= IPA_VERSION_4_5) 224 return; 225 226 /* Disable PA mask to allow HOLB drop */ 227 reg = ipa_reg(ipa, IPA_TX_CFG); 228 offset = reg_offset(reg); 229 230 val = ioread32(ipa->reg_virt + offset); 231 232 val &= ~reg_bit(reg, PA_MASK_EN); 233 234 iowrite32(val, ipa->reg_virt + offset); 235 } 236 237 static void ipa_hardware_config_clkon(struct ipa *ipa) 238 { 239 enum ipa_version version = ipa->version; 240 const struct reg *reg; 241 u32 val; 242 243 if (version >= IPA_VERSION_4_5) 244 return; 245 246 if (version < IPA_VERSION_4_0 && version != IPA_VERSION_3_1) 247 return; 248 249 /* Implement some hardware workarounds */ 250 reg = ipa_reg(ipa, CLKON_CFG); 251 if (version == IPA_VERSION_3_1) { 252 /* Disable MISC clock gating */ 253 val = reg_bit(reg, CLKON_MISC); 254 } else { /* IPA v4.0+ */ 255 /* Enable open global clocks in the CLKON configuration */ 256 val = reg_bit(reg, CLKON_GLOBAL); 257 val |= reg_bit(reg, GLOBAL_2X_CLK); 258 } 259 260 iowrite32(val, ipa->reg_virt + reg_offset(reg)); 261 } 262 263 /* Configure bus access behavior for IPA components */ 264 static void ipa_hardware_config_comp(struct ipa *ipa) 265 { 266 const struct reg *reg; 267 u32 offset; 268 u32 val; 269 270 /* Nothing to configure prior to IPA v4.0 */ 271 if (ipa->version < IPA_VERSION_4_0) 272 return; 273 274 reg = ipa_reg(ipa, COMP_CFG); 275 offset = reg_offset(reg); 276 277 val = ioread32(ipa->reg_virt + offset); 278 279 if (ipa->version == IPA_VERSION_4_0) { 280 val &= ~reg_bit(reg, IPA_QMB_SELECT_CONS_EN); 281 val &= ~reg_bit(reg, IPA_QMB_SELECT_PROD_EN); 282 val &= ~reg_bit(reg, IPA_QMB_SELECT_GLOBAL_EN); 283 } else if (ipa->version < IPA_VERSION_4_5) { 284 val |= reg_bit(reg, GSI_MULTI_AXI_MASTERS_DIS); 285 } else { 286 /* For IPA v4.5+ FULL_FLUSH_WAIT_RS_CLOSURE_EN is 0 */ 287 } 288 289 val |= reg_bit(reg, GSI_MULTI_INORDER_RD_DIS); 290 val |= reg_bit(reg, GSI_MULTI_INORDER_WR_DIS); 291 292 iowrite32(val, ipa->reg_virt + offset); 293 } 294 295 /* Configure DDR and (possibly) PCIe max read/write QSB values */ 296 static void 297 ipa_hardware_config_qsb(struct ipa *ipa, const struct ipa_data *data) 298 { 299 const struct ipa_qsb_data *data0; 300 const struct ipa_qsb_data *data1; 301 const struct reg *reg; 302 u32 val; 303 304 /* QMB 0 represents DDR; QMB 1 (if present) represents PCIe */ 305 data0 = &data->qsb_data[IPA_QSB_MASTER_DDR]; 306 if (data->qsb_count > 1) 307 data1 = &data->qsb_data[IPA_QSB_MASTER_PCIE]; 308 309 /* Max outstanding write accesses for QSB masters */ 310 reg = ipa_reg(ipa, QSB_MAX_WRITES); 311 312 val = reg_encode(reg, GEN_QMB_0_MAX_WRITES, data0->max_writes); 313 if (data->qsb_count > 1) 314 val |= reg_encode(reg, GEN_QMB_1_MAX_WRITES, data1->max_writes); 315 316 iowrite32(val, ipa->reg_virt + reg_offset(reg)); 317 318 /* Max outstanding read accesses for QSB masters */ 319 reg = ipa_reg(ipa, QSB_MAX_READS); 320 321 val = reg_encode(reg, GEN_QMB_0_MAX_READS, data0->max_reads); 322 if (ipa->version >= IPA_VERSION_4_0) 323 val |= reg_encode(reg, GEN_QMB_0_MAX_READS_BEATS, 324 data0->max_reads_beats); 325 if (data->qsb_count > 1) { 326 val = reg_encode(reg, GEN_QMB_1_MAX_READS, data1->max_reads); 327 if (ipa->version >= IPA_VERSION_4_0) 328 val |= reg_encode(reg, GEN_QMB_1_MAX_READS_BEATS, 329 data1->max_reads_beats); 330 } 331 332 iowrite32(val, ipa->reg_virt + reg_offset(reg)); 333 } 334 335 /* The internal inactivity timer clock is used for the aggregation timer */ 336 #define TIMER_FREQUENCY 32000 /* 32 KHz inactivity timer clock */ 337 338 /* Compute the value to use in the COUNTER_CFG register AGGR_GRANULARITY 339 * field to represent the given number of microseconds. The value is one 340 * less than the number of timer ticks in the requested period. 0 is not 341 * a valid granularity value (so for example @usec must be at least 16 for 342 * a TIMER_FREQUENCY of 32000). 343 */ 344 static __always_inline u32 ipa_aggr_granularity_val(u32 usec) 345 { 346 return DIV_ROUND_CLOSEST(usec * TIMER_FREQUENCY, USEC_PER_SEC) - 1; 347 } 348 349 /* IPA uses unified Qtime starting at IPA v4.5, implementing various 350 * timestamps and timers independent of the IPA core clock rate. The 351 * Qtimer is based on a 56-bit timestamp incremented at each tick of 352 * a 19.2 MHz SoC crystal oscillator (XO clock). 353 * 354 * For IPA timestamps (tag, NAT, data path logging) a lower resolution 355 * timestamp is achieved by shifting the Qtimer timestamp value right 356 * some number of bits to produce the low-order bits of the coarser 357 * granularity timestamp. 358 * 359 * For timers, a common timer clock is derived from the XO clock using 360 * a divider (we use 192, to produce a 100kHz timer clock). From 361 * this common clock, three "pulse generators" are used to produce 362 * timer ticks at a configurable frequency. IPA timers (such as 363 * those used for aggregation or head-of-line block handling) now 364 * define their period based on one of these pulse generators. 365 */ 366 static void ipa_qtime_config(struct ipa *ipa) 367 { 368 const struct reg *reg; 369 u32 offset; 370 u32 val; 371 372 /* Timer clock divider must be disabled when we change the rate */ 373 reg = ipa_reg(ipa, TIMERS_XO_CLK_DIV_CFG); 374 iowrite32(0, ipa->reg_virt + reg_offset(reg)); 375 376 reg = ipa_reg(ipa, QTIME_TIMESTAMP_CFG); 377 if (ipa->version < IPA_VERSION_5_5) { 378 /* Set DPL time stamp resolution to use Qtime (not 1 msec) */ 379 val = reg_encode(reg, DPL_TIMESTAMP_LSB, DPL_TIMESTAMP_SHIFT); 380 val |= reg_bit(reg, DPL_TIMESTAMP_SEL); 381 } 382 /* Configure tag and NAT Qtime timestamp resolution as well */ 383 val = reg_encode(reg, TAG_TIMESTAMP_LSB, TAG_TIMESTAMP_SHIFT); 384 val = reg_encode(reg, NAT_TIMESTAMP_LSB, NAT_TIMESTAMP_SHIFT); 385 386 iowrite32(val, ipa->reg_virt + reg_offset(reg)); 387 388 /* Set granularity of pulse generators used for other timers */ 389 reg = ipa_reg(ipa, TIMERS_PULSE_GRAN_CFG); 390 val = reg_encode(reg, PULSE_GRAN_0, IPA_GRAN_100_US); 391 val |= reg_encode(reg, PULSE_GRAN_1, IPA_GRAN_1_MS); 392 if (ipa->version >= IPA_VERSION_5_0) { 393 val |= reg_encode(reg, PULSE_GRAN_2, IPA_GRAN_10_MS); 394 val |= reg_encode(reg, PULSE_GRAN_3, IPA_GRAN_10_MS); 395 } else { 396 val |= reg_encode(reg, PULSE_GRAN_2, IPA_GRAN_1_MS); 397 } 398 399 iowrite32(val, ipa->reg_virt + reg_offset(reg)); 400 401 /* Actual divider is 1 more than value supplied here */ 402 reg = ipa_reg(ipa, TIMERS_XO_CLK_DIV_CFG); 403 offset = reg_offset(reg); 404 405 val = reg_encode(reg, DIV_VALUE, IPA_XO_CLOCK_DIVIDER - 1); 406 407 iowrite32(val, ipa->reg_virt + offset); 408 409 /* Divider value is set; re-enable the common timer clock divider */ 410 val |= reg_bit(reg, DIV_ENABLE); 411 412 iowrite32(val, ipa->reg_virt + offset); 413 } 414 415 /* Before IPA v4.5 timing is controlled by a counter register */ 416 static void ipa_hardware_config_counter(struct ipa *ipa) 417 { 418 u32 granularity = ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY); 419 const struct reg *reg; 420 u32 val; 421 422 reg = ipa_reg(ipa, COUNTER_CFG); 423 /* If defined, EOT_COAL_GRANULARITY is 0 */ 424 val = reg_encode(reg, AGGR_GRANULARITY, granularity); 425 iowrite32(val, ipa->reg_virt + reg_offset(reg)); 426 } 427 428 static void ipa_hardware_config_timing(struct ipa *ipa) 429 { 430 if (ipa->version < IPA_VERSION_4_5) 431 ipa_hardware_config_counter(ipa); 432 else 433 ipa_qtime_config(ipa); 434 } 435 436 static void ipa_hardware_config_hashing(struct ipa *ipa) 437 { 438 const struct reg *reg; 439 440 /* Other than IPA v4.2, all versions enable "hashing". Starting 441 * with IPA v5.0, the filter and router tables are implemented 442 * differently, but the default configuration enables this feature 443 * (now referred to as "cacheing"), so there's nothing to do here. 444 */ 445 if (ipa->version != IPA_VERSION_4_2) 446 return; 447 448 /* IPA v4.2 does not support hashed tables, so disable them */ 449 reg = ipa_reg(ipa, FILT_ROUT_HASH_EN); 450 451 /* IPV6_ROUTER_HASH, IPV6_FILTER_HASH, IPV4_ROUTER_HASH, 452 * IPV4_FILTER_HASH are all zero. 453 */ 454 iowrite32(0, ipa->reg_virt + reg_offset(reg)); 455 } 456 457 static void ipa_idle_indication_cfg(struct ipa *ipa, 458 u32 enter_idle_debounce_thresh, 459 bool const_non_idle_enable) 460 { 461 const struct reg *reg; 462 u32 val; 463 464 if (ipa->version < IPA_VERSION_3_5_1) 465 return; 466 467 reg = ipa_reg(ipa, IDLE_INDICATION_CFG); 468 val = reg_encode(reg, ENTER_IDLE_DEBOUNCE_THRESH, 469 enter_idle_debounce_thresh); 470 if (const_non_idle_enable) 471 val |= reg_bit(reg, CONST_NON_IDLE_ENABLE); 472 473 iowrite32(val, ipa->reg_virt + reg_offset(reg)); 474 } 475 476 /** 477 * ipa_hardware_dcd_config() - Enable dynamic clock division on IPA 478 * @ipa: IPA pointer 479 * 480 * Configures when the IPA signals it is idle to the global clock 481 * controller, which can respond by scaling down the clock to save 482 * power. 483 */ 484 static void ipa_hardware_dcd_config(struct ipa *ipa) 485 { 486 /* Recommended values for IPA 3.5 and later according to IPA HPG */ 487 ipa_idle_indication_cfg(ipa, 256, false); 488 } 489 490 static void ipa_hardware_dcd_deconfig(struct ipa *ipa) 491 { 492 /* Power-on reset values */ 493 ipa_idle_indication_cfg(ipa, 0, true); 494 } 495 496 /** 497 * ipa_hardware_config() - Primitive hardware initialization 498 * @ipa: IPA pointer 499 * @data: IPA configuration data 500 */ 501 static void ipa_hardware_config(struct ipa *ipa, const struct ipa_data *data) 502 { 503 ipa_hardware_config_bcr(ipa, data); 504 ipa_hardware_config_tx(ipa); 505 ipa_hardware_config_clkon(ipa); 506 ipa_hardware_config_comp(ipa); 507 ipa_hardware_config_qsb(ipa, data); 508 ipa_hardware_config_timing(ipa); 509 ipa_hardware_config_hashing(ipa); 510 ipa_hardware_dcd_config(ipa); 511 } 512 513 /** 514 * ipa_hardware_deconfig() - Inverse of ipa_hardware_config() 515 * @ipa: IPA pointer 516 * 517 * This restores the power-on reset values (even if they aren't different) 518 */ 519 static void ipa_hardware_deconfig(struct ipa *ipa) 520 { 521 /* Mostly we just leave things as we set them. */ 522 ipa_hardware_dcd_deconfig(ipa); 523 } 524 525 /** 526 * ipa_config() - Configure IPA hardware 527 * @ipa: IPA pointer 528 * @data: IPA configuration data 529 * 530 * Perform initialization requiring IPA power to be enabled. 531 */ 532 static int ipa_config(struct ipa *ipa, const struct ipa_data *data) 533 { 534 int ret; 535 536 ipa_hardware_config(ipa, data); 537 538 ret = ipa_mem_config(ipa); 539 if (ret) 540 goto err_hardware_deconfig; 541 542 ret = ipa_interrupt_config(ipa); 543 if (ret) 544 goto err_mem_deconfig; 545 546 ipa_uc_config(ipa); 547 548 ret = ipa_endpoint_config(ipa); 549 if (ret) 550 goto err_uc_deconfig; 551 552 ipa_table_config(ipa); /* No deconfig required */ 553 554 /* Assign resource limitation to each group; no deconfig required */ 555 ret = ipa_resource_config(ipa, data->resource_data); 556 if (ret) 557 goto err_endpoint_deconfig; 558 559 ret = ipa_modem_config(ipa); 560 if (ret) 561 goto err_endpoint_deconfig; 562 563 return 0; 564 565 err_endpoint_deconfig: 566 ipa_endpoint_deconfig(ipa); 567 err_uc_deconfig: 568 ipa_uc_deconfig(ipa); 569 ipa_interrupt_deconfig(ipa); 570 err_mem_deconfig: 571 ipa_mem_deconfig(ipa); 572 err_hardware_deconfig: 573 ipa_hardware_deconfig(ipa); 574 575 return ret; 576 } 577 578 /** 579 * ipa_deconfig() - Inverse of ipa_config() 580 * @ipa: IPA pointer 581 */ 582 static void ipa_deconfig(struct ipa *ipa) 583 { 584 ipa_modem_deconfig(ipa); 585 ipa_endpoint_deconfig(ipa); 586 ipa_uc_deconfig(ipa); 587 ipa_interrupt_deconfig(ipa); 588 ipa_mem_deconfig(ipa); 589 ipa_hardware_deconfig(ipa); 590 } 591 592 static int ipa_firmware_load(struct device *dev) 593 { 594 const struct firmware *fw; 595 struct device_node *node; 596 struct resource res; 597 phys_addr_t phys; 598 const char *path; 599 ssize_t size; 600 void *virt; 601 int ret; 602 603 node = of_parse_phandle(dev->of_node, "memory-region", 0); 604 if (!node) { 605 dev_err(dev, "DT error getting \"memory-region\" property\n"); 606 return -EINVAL; 607 } 608 609 ret = of_address_to_resource(node, 0, &res); 610 of_node_put(node); 611 if (ret) { 612 dev_err(dev, "error %d getting \"memory-region\" resource\n", 613 ret); 614 return ret; 615 } 616 617 /* Use name from DTB if specified; use default for *any* error */ 618 ret = of_property_read_string(dev->of_node, "firmware-name", &path); 619 if (ret) { 620 dev_dbg(dev, "error %d getting \"firmware-name\" resource\n", 621 ret); 622 path = IPA_FW_PATH_DEFAULT; 623 } 624 625 ret = request_firmware(&fw, path, dev); 626 if (ret) { 627 dev_err(dev, "error %d requesting \"%s\"\n", ret, path); 628 return ret; 629 } 630 631 phys = res.start; 632 size = (size_t)resource_size(&res); 633 virt = memremap(phys, size, MEMREMAP_WC); 634 if (!virt) { 635 dev_err(dev, "unable to remap firmware memory\n"); 636 ret = -ENOMEM; 637 goto out_release_firmware; 638 } 639 640 ret = qcom_mdt_load(dev, fw, path, IPA_PAS_ID, virt, phys, size, NULL); 641 if (ret) 642 dev_err(dev, "error %d loading \"%s\"\n", ret, path); 643 else if ((ret = qcom_scm_pas_auth_and_reset(IPA_PAS_ID))) 644 dev_err(dev, "error %d authenticating \"%s\"\n", ret, path); 645 646 memunmap(virt); 647 out_release_firmware: 648 release_firmware(fw); 649 650 return ret; 651 } 652 653 static const struct of_device_id ipa_match[] = { 654 { 655 .compatible = "qcom,msm8998-ipa", 656 .data = &ipa_data_v3_1, 657 }, 658 { 659 .compatible = "qcom,sdm845-ipa", 660 .data = &ipa_data_v3_5_1, 661 }, 662 { 663 .compatible = "qcom,sc7180-ipa", 664 .data = &ipa_data_v4_2, 665 }, 666 { 667 .compatible = "qcom,sdx55-ipa", 668 .data = &ipa_data_v4_5, 669 }, 670 { 671 .compatible = "qcom,sm6350-ipa", 672 .data = &ipa_data_v4_7, 673 }, 674 { 675 .compatible = "qcom,sm8350-ipa", 676 .data = &ipa_data_v4_9, 677 }, 678 { 679 .compatible = "qcom,sc7280-ipa", 680 .data = &ipa_data_v4_11, 681 }, 682 { 683 .compatible = "qcom,sdx65-ipa", 684 .data = &ipa_data_v5_0, 685 }, 686 { 687 .compatible = "qcom,sm8550-ipa", 688 .data = &ipa_data_v5_5, 689 }, 690 { }, 691 }; 692 MODULE_DEVICE_TABLE(of, ipa_match); 693 694 /* Check things that can be validated at build time. This just 695 * groups these things BUILD_BUG_ON() calls don't clutter the rest 696 * of the code. 697 * */ 698 static void ipa_validate_build(void) 699 { 700 /* At one time we assumed a 64-bit build, allowing some do_div() 701 * calls to be replaced by simple division or modulo operations. 702 * We currently only perform divide and modulo operations on u32, 703 * u16, or size_t objects, and of those only size_t has any chance 704 * of being a 64-bit value. (It should be guaranteed 32 bits wide 705 * on a 32-bit build, but there is no harm in verifying that.) 706 */ 707 BUILD_BUG_ON(!IS_ENABLED(CONFIG_64BIT) && sizeof(size_t) != 4); 708 709 /* Code assumes the EE ID for the AP is 0 (zeroed structure field) */ 710 BUILD_BUG_ON(GSI_EE_AP != 0); 711 712 /* There's no point if we have no channels or event rings */ 713 BUILD_BUG_ON(!GSI_CHANNEL_COUNT_MAX); 714 BUILD_BUG_ON(!GSI_EVT_RING_COUNT_MAX); 715 716 /* GSI hardware design limits */ 717 BUILD_BUG_ON(GSI_CHANNEL_COUNT_MAX > 32); 718 BUILD_BUG_ON(GSI_EVT_RING_COUNT_MAX > 31); 719 720 /* The number of TREs in a transaction is limited by the channel's 721 * TLV FIFO size. A transaction structure uses 8-bit fields 722 * to represents the number of TREs it has allocated and used. 723 */ 724 BUILD_BUG_ON(GSI_TLV_MAX > U8_MAX); 725 726 /* This is used as a divisor */ 727 BUILD_BUG_ON(!IPA_AGGR_GRANULARITY); 728 729 /* Aggregation granularity value can't be 0, and must fit */ 730 BUILD_BUG_ON(!ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY)); 731 } 732 733 static enum ipa_firmware_loader ipa_firmware_loader(struct device *dev) 734 { 735 bool modem_init; 736 const char *str; 737 int ret; 738 739 /* Look up the old and new properties by name */ 740 modem_init = of_property_read_bool(dev->of_node, "modem-init"); 741 ret = of_property_read_string(dev->of_node, "qcom,gsi-loader", &str); 742 743 /* If the new property doesn't exist, it's legacy behavior */ 744 if (ret == -EINVAL) { 745 if (modem_init) 746 return IPA_LOADER_MODEM; 747 goto out_self; 748 } 749 750 /* Any other error on the new property means it's poorly defined */ 751 if (ret) 752 return IPA_LOADER_INVALID; 753 754 /* New property value exists; if old one does too, that's invalid */ 755 if (modem_init) 756 return IPA_LOADER_INVALID; 757 758 /* Modem loads GSI firmware for "modem" */ 759 if (!strcmp(str, "modem")) 760 return IPA_LOADER_MODEM; 761 762 /* No GSI firmware load is needed for "skip" */ 763 if (!strcmp(str, "skip")) 764 return IPA_LOADER_SKIP; 765 766 /* Any value other than "self" is an error */ 767 if (strcmp(str, "self")) 768 return IPA_LOADER_INVALID; 769 out_self: 770 /* We need Trust Zone to load firmware; make sure it's available */ 771 if (qcom_scm_is_available()) 772 return IPA_LOADER_SELF; 773 774 return IPA_LOADER_DEFER; 775 } 776 777 /** 778 * ipa_probe() - IPA platform driver probe function 779 * @pdev: Platform device pointer 780 * 781 * Return: 0 if successful, or a negative error code (possibly 782 * EPROBE_DEFER) 783 * 784 * This is the main entry point for the IPA driver. Initialization proceeds 785 * in several stages: 786 * - The "init" stage involves activities that can be initialized without 787 * access to the IPA hardware. 788 * - The "config" stage requires IPA power to be active so IPA registers 789 * can be accessed, but does not require the use of IPA immediate commands. 790 * - The "setup" stage uses IPA immediate commands, and so requires the GSI 791 * layer to be initialized. 792 * 793 * A Boolean Device Tree "modem-init" property determines whether GSI 794 * initialization will be performed by the AP (Trust Zone) or the modem. 795 * If the AP does GSI initialization, the setup phase is entered after 796 * this has completed successfully. Otherwise the modem initializes 797 * the GSI layer and signals it has finished by sending an SMP2P interrupt 798 * to the AP; this triggers the start if IPA setup. 799 */ 800 static int ipa_probe(struct platform_device *pdev) 801 { 802 struct device *dev = &pdev->dev; 803 struct ipa_interrupt *interrupt; 804 enum ipa_firmware_loader loader; 805 const struct ipa_data *data; 806 struct ipa_power *power; 807 struct ipa *ipa; 808 int ret; 809 810 ipa_validate_build(); 811 812 /* Get configuration data early; needed for power initialization */ 813 data = of_device_get_match_data(dev); 814 if (!data) { 815 dev_err(dev, "matched hardware not supported\n"); 816 return -ENODEV; 817 } 818 819 if (!ipa_version_supported(data->version)) { 820 dev_err(dev, "unsupported IPA version %u\n", data->version); 821 return -EINVAL; 822 } 823 824 if (!data->modem_route_count) { 825 dev_err(dev, "modem_route_count cannot be zero\n"); 826 return -EINVAL; 827 } 828 829 loader = ipa_firmware_loader(dev); 830 if (loader == IPA_LOADER_INVALID) 831 return -EINVAL; 832 if (loader == IPA_LOADER_DEFER) 833 return -EPROBE_DEFER; 834 835 /* The IPA interrupt might not be ready when we're probed, so this 836 * might return -EPROBE_DEFER. 837 */ 838 interrupt = ipa_interrupt_init(pdev); 839 if (IS_ERR(interrupt)) 840 return PTR_ERR(interrupt); 841 842 /* The clock and interconnects might not be ready when we're probed, 843 * so this might return -EPROBE_DEFER. 844 */ 845 power = ipa_power_init(dev, data->power_data); 846 if (IS_ERR(power)) { 847 ret = PTR_ERR(power); 848 goto err_interrupt_exit; 849 } 850 851 /* No more EPROBE_DEFER. Allocate and initialize the IPA structure */ 852 ipa = kzalloc(sizeof(*ipa), GFP_KERNEL); 853 if (!ipa) { 854 ret = -ENOMEM; 855 goto err_power_exit; 856 } 857 858 ipa->dev = dev; 859 dev_set_drvdata(dev, ipa); 860 ipa->interrupt = interrupt; 861 ipa->power = power; 862 ipa->version = data->version; 863 ipa->modem_route_count = data->modem_route_count; 864 init_completion(&ipa->completion); 865 866 ret = ipa_reg_init(ipa, pdev); 867 if (ret) 868 goto err_kfree_ipa; 869 870 ret = ipa_mem_init(ipa, pdev, data->mem_data); 871 if (ret) 872 goto err_reg_exit; 873 874 ret = gsi_init(&ipa->gsi, pdev, ipa->version, data->endpoint_count, 875 data->endpoint_data); 876 if (ret) 877 goto err_mem_exit; 878 879 /* Result is a non-zero mask of endpoints that support filtering */ 880 ret = ipa_endpoint_init(ipa, data->endpoint_count, data->endpoint_data); 881 if (ret) 882 goto err_gsi_exit; 883 884 ret = ipa_table_init(ipa); 885 if (ret) 886 goto err_endpoint_exit; 887 888 ret = ipa_smp2p_init(ipa, pdev, loader == IPA_LOADER_MODEM); 889 if (ret) 890 goto err_table_exit; 891 892 /* Power needs to be active for config and setup */ 893 ret = pm_runtime_get_sync(dev); 894 if (WARN_ON(ret < 0)) 895 goto err_power_put; 896 897 ret = ipa_config(ipa, data); 898 if (ret) 899 goto err_power_put; 900 901 dev_info(dev, "IPA driver initialized"); 902 903 /* If the modem is loading GSI firmware, it will trigger a call to 904 * ipa_setup() when it has finished. In that case we're done here. 905 */ 906 if (loader == IPA_LOADER_MODEM) 907 goto done; 908 909 if (loader == IPA_LOADER_SELF) { 910 /* The AP is loading GSI firmware; do so now */ 911 ret = ipa_firmware_load(dev); 912 if (ret) 913 goto err_deconfig; 914 } /* Otherwise loader == IPA_LOADER_SKIP */ 915 916 /* GSI firmware is loaded; proceed to setup */ 917 ret = ipa_setup(ipa); 918 if (ret) 919 goto err_deconfig; 920 done: 921 pm_runtime_mark_last_busy(dev); 922 (void)pm_runtime_put_autosuspend(dev); 923 924 return 0; 925 926 err_deconfig: 927 ipa_deconfig(ipa); 928 err_power_put: 929 pm_runtime_put_noidle(dev); 930 ipa_smp2p_exit(ipa); 931 err_table_exit: 932 ipa_table_exit(ipa); 933 err_endpoint_exit: 934 ipa_endpoint_exit(ipa); 935 err_gsi_exit: 936 gsi_exit(&ipa->gsi); 937 err_mem_exit: 938 ipa_mem_exit(ipa); 939 err_reg_exit: 940 ipa_reg_exit(ipa); 941 err_kfree_ipa: 942 kfree(ipa); 943 err_power_exit: 944 ipa_power_exit(power); 945 err_interrupt_exit: 946 ipa_interrupt_exit(interrupt); 947 948 return ret; 949 } 950 951 static void ipa_remove(struct platform_device *pdev) 952 { 953 struct ipa_interrupt *interrupt; 954 struct ipa_power *power; 955 struct device *dev; 956 struct ipa *ipa; 957 int ret; 958 959 ipa = dev_get_drvdata(&pdev->dev); 960 dev = ipa->dev; 961 WARN_ON(dev != &pdev->dev); 962 963 power = ipa->power; 964 interrupt = ipa->interrupt; 965 966 /* Prevent the modem from triggering a call to ipa_setup(). This 967 * also ensures a modem-initiated setup that's underway completes. 968 */ 969 ipa_smp2p_irq_disable_setup(ipa); 970 971 ret = pm_runtime_get_sync(dev); 972 if (WARN_ON(ret < 0)) 973 goto out_power_put; 974 975 if (ipa->setup_complete) { 976 ret = ipa_modem_stop(ipa); 977 /* If starting or stopping is in progress, try once more */ 978 if (ret == -EBUSY) { 979 usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC); 980 ret = ipa_modem_stop(ipa); 981 } 982 if (ret) { 983 /* 984 * Not cleaning up here properly might also yield a 985 * crash later on. As the device is still unregistered 986 * in this case, this might even yield a crash later on. 987 */ 988 dev_err(dev, "Failed to stop modem (%pe), leaking resources\n", 989 ERR_PTR(ret)); 990 return; 991 } 992 993 ipa_teardown(ipa); 994 } 995 996 ipa_deconfig(ipa); 997 out_power_put: 998 pm_runtime_put_noidle(dev); 999 ipa_smp2p_exit(ipa); 1000 ipa_table_exit(ipa); 1001 ipa_endpoint_exit(ipa); 1002 gsi_exit(&ipa->gsi); 1003 ipa_mem_exit(ipa); 1004 ipa_reg_exit(ipa); 1005 kfree(ipa); 1006 ipa_power_exit(power); 1007 ipa_interrupt_exit(interrupt); 1008 1009 dev_info(dev, "IPA driver removed"); 1010 } 1011 1012 static const struct attribute_group *ipa_attribute_groups[] = { 1013 &ipa_attribute_group, 1014 &ipa_feature_attribute_group, 1015 &ipa_endpoint_id_attribute_group, 1016 &ipa_modem_attribute_group, 1017 NULL, 1018 }; 1019 1020 static struct platform_driver ipa_driver = { 1021 .probe = ipa_probe, 1022 .remove_new = ipa_remove, 1023 .shutdown = ipa_remove, 1024 .driver = { 1025 .name = "ipa", 1026 .pm = &ipa_pm_ops, 1027 .of_match_table = ipa_match, 1028 .dev_groups = ipa_attribute_groups, 1029 }, 1030 }; 1031 1032 module_platform_driver(ipa_driver); 1033 1034 MODULE_LICENSE("GPL v2"); 1035 MODULE_DESCRIPTION("Qualcomm IP Accelerator device driver"); 1036