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