1 // SPDX-License-Identifier: GPL-2.0 2 3 /* Copyright (c) 2012-2018, The Linux Foundation. All rights reserved. 4 * Copyright (C) 2018-2020 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/remoteproc.h> 19 #include <linux/qcom_scm.h> 20 #include <linux/soc/qcom/mdt_loader.h> 21 22 #include "ipa.h" 23 #include "ipa_clock.h" 24 #include "ipa_data.h" 25 #include "ipa_endpoint.h" 26 #include "ipa_cmd.h" 27 #include "ipa_reg.h" 28 #include "ipa_mem.h" 29 #include "ipa_table.h" 30 #include "ipa_modem.h" 31 #include "ipa_uc.h" 32 #include "ipa_interrupt.h" 33 #include "gsi_trans.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_FWS_PATH "ipa_fws.mdt" 71 #define IPA_PAS_ID 15 72 73 /** 74 * ipa_suspend_handler() - Handle the suspend IPA interrupt 75 * @ipa: IPA pointer 76 * @irq_id: IPA interrupt type (unused) 77 * 78 * If an RX endpoint is in suspend state, and the IPA has a packet 79 * destined for that endpoint, the IPA generates a SUSPEND interrupt 80 * to inform the AP that it should resume the endpoint. If we get 81 * one of these interrupts we just resume everything. 82 */ 83 static void ipa_suspend_handler(struct ipa *ipa, enum ipa_irq_id irq_id) 84 { 85 /* Just report the event, and let system resume handle the rest. 86 * More than one endpoint could signal this; if so, ignore 87 * all but the first. 88 */ 89 if (!test_and_set_bit(IPA_FLAG_RESUMED, ipa->flags)) 90 pm_wakeup_dev_event(&ipa->pdev->dev, 0, true); 91 92 /* Acknowledge/clear the suspend interrupt on all endpoints */ 93 ipa_interrupt_suspend_clear_all(ipa->interrupt); 94 } 95 96 /** 97 * ipa_setup() - Set up IPA hardware 98 * @ipa: IPA pointer 99 * 100 * Perform initialization that requires issuing immediate commands on 101 * the command TX endpoint. If the modem is doing GSI firmware load 102 * and initialization, this function will be called when an SMP2P 103 * interrupt has been signaled by the modem. Otherwise it will be 104 * called from ipa_probe() after GSI firmware has been successfully 105 * loaded, authenticated, and started by Trust Zone. 106 */ 107 int ipa_setup(struct ipa *ipa) 108 { 109 struct ipa_endpoint *exception_endpoint; 110 struct ipa_endpoint *command_endpoint; 111 struct device *dev = &ipa->pdev->dev; 112 int ret; 113 114 ret = gsi_setup(&ipa->gsi); 115 if (ret) 116 return ret; 117 118 ipa->interrupt = ipa_interrupt_setup(ipa); 119 if (IS_ERR(ipa->interrupt)) { 120 ret = PTR_ERR(ipa->interrupt); 121 goto err_gsi_teardown; 122 } 123 ipa_interrupt_add(ipa->interrupt, IPA_IRQ_TX_SUSPEND, 124 ipa_suspend_handler); 125 126 ipa_uc_setup(ipa); 127 128 ret = device_init_wakeup(dev, true); 129 if (ret) 130 goto err_uc_teardown; 131 132 ipa_endpoint_setup(ipa); 133 134 /* We need to use the AP command TX endpoint to perform other 135 * initialization, so we enable first. 136 */ 137 command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]; 138 ret = ipa_endpoint_enable_one(command_endpoint); 139 if (ret) 140 goto err_endpoint_teardown; 141 142 ret = ipa_mem_setup(ipa); 143 if (ret) 144 goto err_command_disable; 145 146 ret = ipa_table_setup(ipa); 147 if (ret) 148 goto err_mem_teardown; 149 150 /* Enable the exception handling endpoint, and tell the hardware 151 * to use it by default. 152 */ 153 exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]; 154 ret = ipa_endpoint_enable_one(exception_endpoint); 155 if (ret) 156 goto err_table_teardown; 157 158 ipa_endpoint_default_route_set(ipa, exception_endpoint->endpoint_id); 159 160 /* We're all set. Now prepare for communication with the modem */ 161 ret = ipa_modem_setup(ipa); 162 if (ret) 163 goto err_default_route_clear; 164 165 ipa->setup_complete = true; 166 167 dev_info(dev, "IPA driver setup completed successfully\n"); 168 169 return 0; 170 171 err_default_route_clear: 172 ipa_endpoint_default_route_clear(ipa); 173 ipa_endpoint_disable_one(exception_endpoint); 174 err_table_teardown: 175 ipa_table_teardown(ipa); 176 err_mem_teardown: 177 ipa_mem_teardown(ipa); 178 err_command_disable: 179 ipa_endpoint_disable_one(command_endpoint); 180 err_endpoint_teardown: 181 ipa_endpoint_teardown(ipa); 182 (void)device_init_wakeup(dev, false); 183 err_uc_teardown: 184 ipa_uc_teardown(ipa); 185 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND); 186 ipa_interrupt_teardown(ipa->interrupt); 187 err_gsi_teardown: 188 gsi_teardown(&ipa->gsi); 189 190 return ret; 191 } 192 193 /** 194 * ipa_teardown() - Inverse of ipa_setup() 195 * @ipa: IPA pointer 196 */ 197 static void ipa_teardown(struct ipa *ipa) 198 { 199 struct ipa_endpoint *exception_endpoint; 200 struct ipa_endpoint *command_endpoint; 201 202 ipa_modem_teardown(ipa); 203 ipa_endpoint_default_route_clear(ipa); 204 exception_endpoint = ipa->name_map[IPA_ENDPOINT_AP_LAN_RX]; 205 ipa_endpoint_disable_one(exception_endpoint); 206 ipa_table_teardown(ipa); 207 ipa_mem_teardown(ipa); 208 command_endpoint = ipa->name_map[IPA_ENDPOINT_AP_COMMAND_TX]; 209 ipa_endpoint_disable_one(command_endpoint); 210 ipa_endpoint_teardown(ipa); 211 (void)device_init_wakeup(&ipa->pdev->dev, false); 212 ipa_uc_teardown(ipa); 213 ipa_interrupt_remove(ipa->interrupt, IPA_IRQ_TX_SUSPEND); 214 ipa_interrupt_teardown(ipa->interrupt); 215 gsi_teardown(&ipa->gsi); 216 } 217 218 /* Configure QMB Core Master Port selection */ 219 static void ipa_hardware_config_comp(struct ipa *ipa) 220 { 221 u32 val; 222 223 /* Nothing to configure for IPA v3.5.1 */ 224 if (ipa->version == IPA_VERSION_3_5_1) 225 return; 226 227 val = ioread32(ipa->reg_virt + IPA_REG_COMP_CFG_OFFSET); 228 229 if (ipa->version == IPA_VERSION_4_0) { 230 val &= ~IPA_QMB_SELECT_CONS_EN_FMASK; 231 val &= ~IPA_QMB_SELECT_PROD_EN_FMASK; 232 val &= ~IPA_QMB_SELECT_GLOBAL_EN_FMASK; 233 } else { 234 val |= GSI_MULTI_AXI_MASTERS_DIS_FMASK; 235 } 236 237 val |= GSI_MULTI_INORDER_RD_DIS_FMASK; 238 val |= GSI_MULTI_INORDER_WR_DIS_FMASK; 239 240 iowrite32(val, ipa->reg_virt + IPA_REG_COMP_CFG_OFFSET); 241 } 242 243 /* Configure DDR and PCIe max read/write QSB values */ 244 static void ipa_hardware_config_qsb(struct ipa *ipa) 245 { 246 u32 val; 247 248 /* QMB_0 represents DDR; QMB_1 represents PCIe (not present in 4.2) */ 249 val = u32_encode_bits(8, GEN_QMB_0_MAX_WRITES_FMASK); 250 if (ipa->version == IPA_VERSION_4_2) 251 val |= u32_encode_bits(0, GEN_QMB_1_MAX_WRITES_FMASK); 252 else 253 val |= u32_encode_bits(4, GEN_QMB_1_MAX_WRITES_FMASK); 254 iowrite32(val, ipa->reg_virt + IPA_REG_QSB_MAX_WRITES_OFFSET); 255 256 if (ipa->version == IPA_VERSION_3_5_1) { 257 val = u32_encode_bits(8, GEN_QMB_0_MAX_READS_FMASK); 258 val |= u32_encode_bits(12, GEN_QMB_1_MAX_READS_FMASK); 259 } else { 260 val = u32_encode_bits(12, GEN_QMB_0_MAX_READS_FMASK); 261 if (ipa->version == IPA_VERSION_4_2) 262 val |= u32_encode_bits(0, GEN_QMB_1_MAX_READS_FMASK); 263 else 264 val |= u32_encode_bits(12, GEN_QMB_1_MAX_READS_FMASK); 265 /* GEN_QMB_0_MAX_READS_BEATS is 0 */ 266 /* GEN_QMB_1_MAX_READS_BEATS is 0 */ 267 } 268 iowrite32(val, ipa->reg_virt + IPA_REG_QSB_MAX_READS_OFFSET); 269 } 270 271 static void ipa_idle_indication_cfg(struct ipa *ipa, 272 u32 enter_idle_debounce_thresh, 273 bool const_non_idle_enable) 274 { 275 u32 offset; 276 u32 val; 277 278 val = u32_encode_bits(enter_idle_debounce_thresh, 279 ENTER_IDLE_DEBOUNCE_THRESH_FMASK); 280 if (const_non_idle_enable) 281 val |= CONST_NON_IDLE_ENABLE_FMASK; 282 283 offset = ipa_reg_idle_indication_cfg_offset(ipa->version); 284 iowrite32(val, ipa->reg_virt + offset); 285 } 286 287 /** 288 * ipa_hardware_dcd_config() - Enable dynamic clock division on IPA 289 * @ipa: IPA pointer 290 * 291 * Configures when the IPA signals it is idle to the global clock 292 * controller, which can respond by scalling down the clock to 293 * save power. 294 */ 295 static void ipa_hardware_dcd_config(struct ipa *ipa) 296 { 297 /* Recommended values for IPA 3.5 according to IPA HPG */ 298 ipa_idle_indication_cfg(ipa, 256, false); 299 } 300 301 static void ipa_hardware_dcd_deconfig(struct ipa *ipa) 302 { 303 /* Power-on reset values */ 304 ipa_idle_indication_cfg(ipa, 0, true); 305 } 306 307 /** 308 * ipa_hardware_config() - Primitive hardware initialization 309 * @ipa: IPA pointer 310 */ 311 static void ipa_hardware_config(struct ipa *ipa) 312 { 313 u32 granularity; 314 u32 val; 315 316 /* Fill in backward-compatibility register, based on version */ 317 val = ipa_reg_bcr_val(ipa->version); 318 iowrite32(val, ipa->reg_virt + IPA_REG_BCR_OFFSET); 319 320 if (ipa->version != IPA_VERSION_3_5_1) { 321 /* Enable open global clocks (hardware workaround) */ 322 val = GLOBAL_FMASK; 323 val |= GLOBAL_2X_CLK_FMASK; 324 iowrite32(val, ipa->reg_virt + IPA_REG_CLKON_CFG_OFFSET); 325 326 /* Disable PA mask to allow HOLB drop (hardware workaround) */ 327 val = ioread32(ipa->reg_virt + IPA_REG_TX_CFG_OFFSET); 328 val &= ~PA_MASK_EN_FMASK; 329 iowrite32(val, ipa->reg_virt + IPA_REG_TX_CFG_OFFSET); 330 } 331 332 ipa_hardware_config_comp(ipa); 333 334 /* Configure system bus limits */ 335 ipa_hardware_config_qsb(ipa); 336 337 /* Configure aggregation granularity */ 338 granularity = ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY); 339 val = u32_encode_bits(granularity, AGGR_GRANULARITY_FMASK); 340 iowrite32(val, ipa->reg_virt + IPA_REG_COUNTER_CFG_OFFSET); 341 342 /* IPA v4.2 does not support hashed tables, so disable them */ 343 if (ipa->version == IPA_VERSION_4_2) { 344 u32 offset = ipa_reg_filt_rout_hash_en_offset(ipa->version); 345 346 iowrite32(0, ipa->reg_virt + offset); 347 } 348 349 /* Enable dynamic clock division */ 350 ipa_hardware_dcd_config(ipa); 351 } 352 353 /** 354 * ipa_hardware_deconfig() - Inverse of ipa_hardware_config() 355 * @ipa: IPA pointer 356 * 357 * This restores the power-on reset values (even if they aren't different) 358 */ 359 static void ipa_hardware_deconfig(struct ipa *ipa) 360 { 361 /* Mostly we just leave things as we set them. */ 362 ipa_hardware_dcd_deconfig(ipa); 363 } 364 365 #ifdef IPA_VALIDATION 366 367 static bool ipa_resource_limits_valid(struct ipa *ipa, 368 const struct ipa_resource_data *data) 369 { 370 u32 group_count; 371 u32 i; 372 u32 j; 373 374 /* We program at most 6 source or destination resource group limits */ 375 BUILD_BUG_ON(IPA_RESOURCE_GROUP_SRC_MAX > 6); 376 377 group_count = ipa_resource_group_src_count(ipa->version); 378 if (!group_count || group_count > IPA_RESOURCE_GROUP_SRC_MAX) 379 return false; 380 381 /* Return an error if a non-zero resource limit is specified 382 * for a resource group not supported by hardware. 383 */ 384 for (i = 0; i < data->resource_src_count; i++) { 385 const struct ipa_resource_src *resource; 386 387 resource = &data->resource_src[i]; 388 for (j = group_count; j < IPA_RESOURCE_GROUP_SRC_MAX; j++) 389 if (resource->limits[j].min || resource->limits[j].max) 390 return false; 391 } 392 393 group_count = ipa_resource_group_dst_count(ipa->version); 394 if (!group_count || group_count > IPA_RESOURCE_GROUP_DST_MAX) 395 return false; 396 397 for (i = 0; i < data->resource_dst_count; i++) { 398 const struct ipa_resource_dst *resource; 399 400 resource = &data->resource_dst[i]; 401 for (j = group_count; j < IPA_RESOURCE_GROUP_DST_MAX; j++) 402 if (resource->limits[j].min || resource->limits[j].max) 403 return false; 404 } 405 406 return true; 407 } 408 409 #else /* !IPA_VALIDATION */ 410 411 static bool ipa_resource_limits_valid(struct ipa *ipa, 412 const struct ipa_resource_data *data) 413 { 414 return true; 415 } 416 417 #endif /* !IPA_VALIDATION */ 418 419 static void 420 ipa_resource_config_common(struct ipa *ipa, u32 offset, 421 const struct ipa_resource_limits *xlimits, 422 const struct ipa_resource_limits *ylimits) 423 { 424 u32 val; 425 426 val = u32_encode_bits(xlimits->min, X_MIN_LIM_FMASK); 427 val |= u32_encode_bits(xlimits->max, X_MAX_LIM_FMASK); 428 if (ylimits) { 429 val |= u32_encode_bits(ylimits->min, Y_MIN_LIM_FMASK); 430 val |= u32_encode_bits(ylimits->max, Y_MAX_LIM_FMASK); 431 } 432 433 iowrite32(val, ipa->reg_virt + offset); 434 } 435 436 static void ipa_resource_config_src(struct ipa *ipa, 437 const struct ipa_resource_src *resource) 438 { 439 u32 group_count = ipa_resource_group_src_count(ipa->version); 440 const struct ipa_resource_limits *ylimits; 441 u32 offset; 442 443 offset = IPA_REG_SRC_RSRC_GRP_01_RSRC_TYPE_N_OFFSET(resource->type); 444 ylimits = group_count == 1 ? NULL : &resource->limits[1]; 445 ipa_resource_config_common(ipa, offset, &resource->limits[0], ylimits); 446 447 if (group_count < 2) 448 return; 449 450 offset = IPA_REG_SRC_RSRC_GRP_23_RSRC_TYPE_N_OFFSET(resource->type); 451 ylimits = group_count == 3 ? NULL : &resource->limits[3]; 452 ipa_resource_config_common(ipa, offset, &resource->limits[2], ylimits); 453 454 if (group_count < 4) 455 return; 456 457 offset = IPA_REG_SRC_RSRC_GRP_45_RSRC_TYPE_N_OFFSET(resource->type); 458 ylimits = group_count == 5 ? NULL : &resource->limits[5]; 459 ipa_resource_config_common(ipa, offset, &resource->limits[4], ylimits); 460 } 461 462 static void ipa_resource_config_dst(struct ipa *ipa, 463 const struct ipa_resource_dst *resource) 464 { 465 u32 group_count = ipa_resource_group_dst_count(ipa->version); 466 const struct ipa_resource_limits *ylimits; 467 u32 offset; 468 469 offset = IPA_REG_DST_RSRC_GRP_01_RSRC_TYPE_N_OFFSET(resource->type); 470 ylimits = group_count == 1 ? NULL : &resource->limits[1]; 471 ipa_resource_config_common(ipa, offset, &resource->limits[0], ylimits); 472 473 if (group_count < 2) 474 return; 475 476 offset = IPA_REG_DST_RSRC_GRP_23_RSRC_TYPE_N_OFFSET(resource->type); 477 ylimits = group_count == 3 ? NULL : &resource->limits[3]; 478 ipa_resource_config_common(ipa, offset, &resource->limits[2], ylimits); 479 480 if (group_count < 4) 481 return; 482 483 offset = IPA_REG_DST_RSRC_GRP_45_RSRC_TYPE_N_OFFSET(resource->type); 484 ylimits = group_count == 5 ? NULL : &resource->limits[5]; 485 ipa_resource_config_common(ipa, offset, &resource->limits[4], ylimits); 486 } 487 488 static int 489 ipa_resource_config(struct ipa *ipa, const struct ipa_resource_data *data) 490 { 491 u32 i; 492 493 if (!ipa_resource_limits_valid(ipa, data)) 494 return -EINVAL; 495 496 for (i = 0; i < data->resource_src_count; i++) 497 ipa_resource_config_src(ipa, data->resource_src); 498 499 for (i = 0; i < data->resource_dst_count; i++) 500 ipa_resource_config_dst(ipa, data->resource_dst); 501 502 return 0; 503 } 504 505 static void ipa_resource_deconfig(struct ipa *ipa) 506 { 507 /* Nothing to do */ 508 } 509 510 /** 511 * ipa_config() - Configure IPA hardware 512 * @ipa: IPA pointer 513 * @data: IPA configuration data 514 * 515 * Perform initialization requiring IPA clock to be enabled. 516 */ 517 static int ipa_config(struct ipa *ipa, const struct ipa_data *data) 518 { 519 int ret; 520 521 /* Get a clock reference to allow initialization. This reference 522 * is held after initialization completes, and won't get dropped 523 * unless/until a system suspend request arrives. 524 */ 525 ipa_clock_get(ipa); 526 527 ipa_hardware_config(ipa); 528 529 ret = ipa_endpoint_config(ipa); 530 if (ret) 531 goto err_hardware_deconfig; 532 533 ret = ipa_mem_config(ipa); 534 if (ret) 535 goto err_endpoint_deconfig; 536 537 ipa_table_config(ipa); 538 539 /* Assign resource limitation to each group */ 540 ret = ipa_resource_config(ipa, data->resource_data); 541 if (ret) 542 goto err_table_deconfig; 543 544 ret = ipa_modem_config(ipa); 545 if (ret) 546 goto err_resource_deconfig; 547 548 return 0; 549 550 err_resource_deconfig: 551 ipa_resource_deconfig(ipa); 552 err_table_deconfig: 553 ipa_table_deconfig(ipa); 554 ipa_mem_deconfig(ipa); 555 err_endpoint_deconfig: 556 ipa_endpoint_deconfig(ipa); 557 err_hardware_deconfig: 558 ipa_hardware_deconfig(ipa); 559 ipa_clock_put(ipa); 560 561 return ret; 562 } 563 564 /** 565 * ipa_deconfig() - Inverse of ipa_config() 566 * @ipa: IPA pointer 567 */ 568 static void ipa_deconfig(struct ipa *ipa) 569 { 570 ipa_modem_deconfig(ipa); 571 ipa_resource_deconfig(ipa); 572 ipa_table_deconfig(ipa); 573 ipa_mem_deconfig(ipa); 574 ipa_endpoint_deconfig(ipa); 575 ipa_hardware_deconfig(ipa); 576 ipa_clock_put(ipa); 577 } 578 579 static int ipa_firmware_load(struct device *dev) 580 { 581 const struct firmware *fw; 582 struct device_node *node; 583 struct resource res; 584 phys_addr_t phys; 585 ssize_t size; 586 void *virt; 587 int ret; 588 589 node = of_parse_phandle(dev->of_node, "memory-region", 0); 590 if (!node) { 591 dev_err(dev, "DT error getting \"memory-region\" property\n"); 592 return -EINVAL; 593 } 594 595 ret = of_address_to_resource(node, 0, &res); 596 if (ret) { 597 dev_err(dev, "error %d getting \"memory-region\" resource\n", 598 ret); 599 return ret; 600 } 601 602 ret = request_firmware(&fw, IPA_FWS_PATH, dev); 603 if (ret) { 604 dev_err(dev, "error %d requesting \"%s\"\n", ret, IPA_FWS_PATH); 605 return ret; 606 } 607 608 phys = res.start; 609 size = (size_t)resource_size(&res); 610 virt = memremap(phys, size, MEMREMAP_WC); 611 if (!virt) { 612 dev_err(dev, "unable to remap firmware memory\n"); 613 ret = -ENOMEM; 614 goto out_release_firmware; 615 } 616 617 ret = qcom_mdt_load(dev, fw, IPA_FWS_PATH, IPA_PAS_ID, 618 virt, phys, size, NULL); 619 if (ret) 620 dev_err(dev, "error %d loading \"%s\"\n", ret, IPA_FWS_PATH); 621 else if ((ret = qcom_scm_pas_auth_and_reset(IPA_PAS_ID))) 622 dev_err(dev, "error %d authenticating \"%s\"\n", ret, 623 IPA_FWS_PATH); 624 625 memunmap(virt); 626 out_release_firmware: 627 release_firmware(fw); 628 629 return ret; 630 } 631 632 static const struct of_device_id ipa_match[] = { 633 { 634 .compatible = "qcom,sdm845-ipa", 635 .data = &ipa_data_sdm845, 636 }, 637 { 638 .compatible = "qcom,sc7180-ipa", 639 .data = &ipa_data_sc7180, 640 }, 641 { }, 642 }; 643 MODULE_DEVICE_TABLE(of, ipa_match); 644 645 static phandle of_property_read_phandle(const struct device_node *np, 646 const char *name) 647 { 648 struct property *prop; 649 int len = 0; 650 651 prop = of_find_property(np, name, &len); 652 if (!prop || len != sizeof(__be32)) 653 return 0; 654 655 return be32_to_cpup(prop->value); 656 } 657 658 /* Check things that can be validated at build time. This just 659 * groups these things BUILD_BUG_ON() calls don't clutter the rest 660 * of the code. 661 * */ 662 static void ipa_validate_build(void) 663 { 664 #ifdef IPA_VALIDATE 665 /* We assume we're working on 64-bit hardware */ 666 BUILD_BUG_ON(!IS_ENABLED(CONFIG_64BIT)); 667 668 /* Code assumes the EE ID for the AP is 0 (zeroed structure field) */ 669 BUILD_BUG_ON(GSI_EE_AP != 0); 670 671 /* There's no point if we have no channels or event rings */ 672 BUILD_BUG_ON(!GSI_CHANNEL_COUNT_MAX); 673 BUILD_BUG_ON(!GSI_EVT_RING_COUNT_MAX); 674 675 /* GSI hardware design limits */ 676 BUILD_BUG_ON(GSI_CHANNEL_COUNT_MAX > 32); 677 BUILD_BUG_ON(GSI_EVT_RING_COUNT_MAX > 31); 678 679 /* The number of TREs in a transaction is limited by the channel's 680 * TLV FIFO size. A transaction structure uses 8-bit fields 681 * to represents the number of TREs it has allocated and used. 682 */ 683 BUILD_BUG_ON(GSI_TLV_MAX > U8_MAX); 684 685 /* This is used as a divisor */ 686 BUILD_BUG_ON(!IPA_AGGR_GRANULARITY); 687 688 /* Aggregation granularity value can't be 0, and must fit */ 689 BUILD_BUG_ON(!ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY)); 690 BUILD_BUG_ON(ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY) > 691 field_max(AGGR_GRANULARITY_FMASK)); 692 #endif /* IPA_VALIDATE */ 693 } 694 695 /** 696 * ipa_probe() - IPA platform driver probe function 697 * @pdev: Platform device pointer 698 * 699 * Return: 0 if successful, or a negative error code (possibly 700 * EPROBE_DEFER) 701 * 702 * This is the main entry point for the IPA driver. Initialization proceeds 703 * in several stages: 704 * - The "init" stage involves activities that can be initialized without 705 * access to the IPA hardware. 706 * - The "config" stage requires the IPA clock to be active so IPA registers 707 * can be accessed, but does not require the use of IPA immediate commands. 708 * - The "setup" stage uses IPA immediate commands, and so requires the GSI 709 * layer to be initialized. 710 * 711 * A Boolean Device Tree "modem-init" property determines whether GSI 712 * initialization will be performed by the AP (Trust Zone) or the modem. 713 * If the AP does GSI initialization, the setup phase is entered after 714 * this has completed successfully. Otherwise the modem initializes 715 * the GSI layer and signals it has finished by sending an SMP2P interrupt 716 * to the AP; this triggers the start if IPA setup. 717 */ 718 static int ipa_probe(struct platform_device *pdev) 719 { 720 struct device *dev = &pdev->dev; 721 const struct ipa_data *data; 722 struct ipa_clock *clock; 723 struct rproc *rproc; 724 bool modem_init; 725 struct ipa *ipa; 726 phandle ph; 727 int ret; 728 729 ipa_validate_build(); 730 731 /* Get configuration data early; needed for clock initialization */ 732 data = of_device_get_match_data(dev); 733 if (!data) { 734 /* This is really IPA_VALIDATE (should never happen) */ 735 dev_err(dev, "matched hardware not supported\n"); 736 return -ENODEV; 737 } 738 739 /* If we need Trust Zone, make sure it's available */ 740 modem_init = of_property_read_bool(dev->of_node, "modem-init"); 741 if (!modem_init) 742 if (!qcom_scm_is_available()) 743 return -EPROBE_DEFER; 744 745 /* We rely on remoteproc to tell us about modem state changes */ 746 ph = of_property_read_phandle(dev->of_node, "modem-remoteproc"); 747 if (!ph) { 748 dev_err(dev, "DT missing \"modem-remoteproc\" property\n"); 749 return -EINVAL; 750 } 751 752 rproc = rproc_get_by_phandle(ph); 753 if (!rproc) 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 clock = ipa_clock_init(dev, data->clock_data); 760 if (IS_ERR(clock)) { 761 ret = PTR_ERR(clock); 762 goto err_rproc_put; 763 } 764 765 /* No more EPROBE_DEFER. Allocate and initialize the IPA structure */ 766 ipa = kzalloc(sizeof(*ipa), GFP_KERNEL); 767 if (!ipa) { 768 ret = -ENOMEM; 769 goto err_clock_exit; 770 } 771 772 ipa->pdev = pdev; 773 dev_set_drvdata(dev, ipa); 774 ipa->modem_rproc = rproc; 775 ipa->clock = clock; 776 ipa->version = data->version; 777 778 ret = ipa_reg_init(ipa); 779 if (ret) 780 goto err_kfree_ipa; 781 782 ret = ipa_mem_init(ipa, data->mem_data); 783 if (ret) 784 goto err_reg_exit; 785 786 ret = gsi_init(&ipa->gsi, pdev, ipa->version, data->endpoint_count, 787 data->endpoint_data); 788 if (ret) 789 goto err_mem_exit; 790 791 /* Result is a non-zero mask of endpoints that support filtering */ 792 ipa->filter_map = ipa_endpoint_init(ipa, data->endpoint_count, 793 data->endpoint_data); 794 if (!ipa->filter_map) { 795 ret = -EINVAL; 796 goto err_gsi_exit; 797 } 798 799 ret = ipa_table_init(ipa); 800 if (ret) 801 goto err_endpoint_exit; 802 803 ret = ipa_modem_init(ipa, modem_init); 804 if (ret) 805 goto err_table_exit; 806 807 ret = ipa_config(ipa, data); 808 if (ret) 809 goto err_modem_exit; 810 811 dev_info(dev, "IPA driver initialized"); 812 813 /* If the modem is doing early initialization, it will trigger a 814 * call to ipa_setup() call when it has finished. In that case 815 * we're done here. 816 */ 817 if (modem_init) 818 return 0; 819 820 /* Otherwise we need to load the firmware and have Trust Zone validate 821 * and install it. If that succeeds we can proceed with setup. 822 */ 823 ret = ipa_firmware_load(dev); 824 if (ret) 825 goto err_deconfig; 826 827 ret = ipa_setup(ipa); 828 if (ret) 829 goto err_deconfig; 830 831 return 0; 832 833 err_deconfig: 834 ipa_deconfig(ipa); 835 err_modem_exit: 836 ipa_modem_exit(ipa); 837 err_table_exit: 838 ipa_table_exit(ipa); 839 err_endpoint_exit: 840 ipa_endpoint_exit(ipa); 841 err_gsi_exit: 842 gsi_exit(&ipa->gsi); 843 err_mem_exit: 844 ipa_mem_exit(ipa); 845 err_reg_exit: 846 ipa_reg_exit(ipa); 847 err_kfree_ipa: 848 kfree(ipa); 849 err_clock_exit: 850 ipa_clock_exit(clock); 851 err_rproc_put: 852 rproc_put(rproc); 853 854 return ret; 855 } 856 857 static int ipa_remove(struct platform_device *pdev) 858 { 859 struct ipa *ipa = dev_get_drvdata(&pdev->dev); 860 struct rproc *rproc = ipa->modem_rproc; 861 struct ipa_clock *clock = ipa->clock; 862 int ret; 863 864 if (ipa->setup_complete) { 865 ret = ipa_modem_stop(ipa); 866 /* If starting or stopping is in progress, try once more */ 867 if (ret == -EBUSY) { 868 usleep_range(USEC_PER_MSEC, 2 * USEC_PER_MSEC); 869 ret = ipa_modem_stop(ipa); 870 } 871 if (ret) 872 return ret; 873 874 ipa_teardown(ipa); 875 } 876 877 ipa_deconfig(ipa); 878 ipa_modem_exit(ipa); 879 ipa_table_exit(ipa); 880 ipa_endpoint_exit(ipa); 881 gsi_exit(&ipa->gsi); 882 ipa_mem_exit(ipa); 883 ipa_reg_exit(ipa); 884 kfree(ipa); 885 ipa_clock_exit(clock); 886 rproc_put(rproc); 887 888 return 0; 889 } 890 891 static void ipa_shutdown(struct platform_device *pdev) 892 { 893 int ret; 894 895 ret = ipa_remove(pdev); 896 if (ret) 897 dev_err(&pdev->dev, "shutdown: remove returned %d\n", ret); 898 } 899 900 /** 901 * ipa_suspend() - Power management system suspend callback 902 * @dev: IPA device structure 903 * 904 * Return: Always returns zero 905 * 906 * Called by the PM framework when a system suspend operation is invoked. 907 * Suspends endpoints and releases the clock reference held to keep 908 * the IPA clock running until this point. 909 */ 910 static int ipa_suspend(struct device *dev) 911 { 912 struct ipa *ipa = dev_get_drvdata(dev); 913 914 /* When a suspended RX endpoint has a packet ready to receive, we 915 * get an IPA SUSPEND interrupt. We trigger a system resume in 916 * that case, but only on the first such interrupt since suspend. 917 */ 918 __clear_bit(IPA_FLAG_RESUMED, ipa->flags); 919 920 ipa_endpoint_suspend(ipa); 921 922 ipa_clock_put(ipa); 923 924 return 0; 925 } 926 927 /** 928 * ipa_resume() - Power management system resume callback 929 * @dev: IPA device structure 930 * 931 * Return: Always returns 0 932 * 933 * Called by the PM framework when a system resume operation is invoked. 934 * Takes an IPA clock reference to keep the clock running until suspend, 935 * and resumes endpoints. 936 */ 937 static int ipa_resume(struct device *dev) 938 { 939 struct ipa *ipa = dev_get_drvdata(dev); 940 941 /* This clock reference will keep the IPA out of suspend 942 * until we get a power management suspend request. 943 */ 944 ipa_clock_get(ipa); 945 946 ipa_endpoint_resume(ipa); 947 948 return 0; 949 } 950 951 static const struct dev_pm_ops ipa_pm_ops = { 952 .suspend = ipa_suspend, 953 .resume = ipa_resume, 954 }; 955 956 static struct platform_driver ipa_driver = { 957 .probe = ipa_probe, 958 .remove = ipa_remove, 959 .shutdown = ipa_shutdown, 960 .driver = { 961 .name = "ipa", 962 .pm = &ipa_pm_ops, 963 .of_match_table = ipa_match, 964 }, 965 }; 966 967 module_platform_driver(ipa_driver); 968 969 MODULE_LICENSE("GPL v2"); 970 MODULE_DESCRIPTION("Qualcomm IP Accelerator device driver"); 971