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; 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); 340 iowrite32(val, ipa->reg_virt + IPA_REG_COUNTER_CFG_OFFSET); 341 342 /* Disable hashed IPv4 and IPv6 routing and filtering for IPA v4.2 */ 343 if (ipa->version == IPA_VERSION_4_2) 344 iowrite32(0, ipa->reg_virt + IPA_REG_FILT_ROUT_HASH_EN_OFFSET); 345 346 /* Enable dynamic clock division */ 347 ipa_hardware_dcd_config(ipa); 348 } 349 350 /** 351 * ipa_hardware_deconfig() - Inverse of ipa_hardware_config() 352 * @ipa: IPA pointer 353 * 354 * This restores the power-on reset values (even if they aren't different) 355 */ 356 static void ipa_hardware_deconfig(struct ipa *ipa) 357 { 358 /* Mostly we just leave things as we set them. */ 359 ipa_hardware_dcd_deconfig(ipa); 360 } 361 362 #ifdef IPA_VALIDATION 363 364 static bool ipa_resource_limits_valid(struct ipa *ipa, 365 const struct ipa_resource_data *data) 366 { 367 u32 group_count; 368 u32 i; 369 u32 j; 370 371 /* We program at most 6 source or destination resource group limits */ 372 BUILD_BUG_ON(IPA_RESOURCE_GROUP_SRC_MAX > 6); 373 374 group_count = ipa_resource_group_src_count(ipa->version); 375 if (!group_count || group_count > IPA_RESOURCE_GROUP_SRC_MAX) 376 return false; 377 378 /* Return an error if a non-zero resource limit is specified 379 * for a resource group not supported by hardware. 380 */ 381 for (i = 0; i < data->resource_src_count; i++) { 382 const struct ipa_resource_src *resource; 383 384 resource = &data->resource_src[i]; 385 for (j = group_count; j < IPA_RESOURCE_GROUP_SRC_MAX; j++) 386 if (resource->limits[j].min || resource->limits[j].max) 387 return false; 388 } 389 390 group_count = ipa_resource_group_dst_count(ipa->version); 391 if (!group_count || group_count > IPA_RESOURCE_GROUP_DST_MAX) 392 return false; 393 394 for (i = 0; i < data->resource_dst_count; i++) { 395 const struct ipa_resource_dst *resource; 396 397 resource = &data->resource_dst[i]; 398 for (j = group_count; j < IPA_RESOURCE_GROUP_DST_MAX; j++) 399 if (resource->limits[j].min || resource->limits[j].max) 400 return false; 401 } 402 403 return true; 404 } 405 406 #else /* !IPA_VALIDATION */ 407 408 static bool ipa_resource_limits_valid(struct ipa *ipa, 409 const struct ipa_resource_data *data) 410 { 411 return true; 412 } 413 414 #endif /* !IPA_VALIDATION */ 415 416 static void 417 ipa_resource_config_common(struct ipa *ipa, u32 offset, 418 const struct ipa_resource_limits *xlimits, 419 const struct ipa_resource_limits *ylimits) 420 { 421 u32 val; 422 423 val = u32_encode_bits(xlimits->min, X_MIN_LIM_FMASK); 424 val |= u32_encode_bits(xlimits->max, X_MAX_LIM_FMASK); 425 if (ylimits) { 426 val |= u32_encode_bits(ylimits->min, Y_MIN_LIM_FMASK); 427 val |= u32_encode_bits(ylimits->max, Y_MAX_LIM_FMASK); 428 } 429 430 iowrite32(val, ipa->reg_virt + offset); 431 } 432 433 static void ipa_resource_config_src(struct ipa *ipa, 434 const struct ipa_resource_src *resource) 435 { 436 u32 group_count = ipa_resource_group_src_count(ipa->version); 437 const struct ipa_resource_limits *ylimits; 438 u32 offset; 439 440 offset = IPA_REG_SRC_RSRC_GRP_01_RSRC_TYPE_N_OFFSET(resource->type); 441 ylimits = group_count == 1 ? NULL : &resource->limits[1]; 442 ipa_resource_config_common(ipa, offset, &resource->limits[0], ylimits); 443 444 if (group_count < 2) 445 return; 446 447 offset = IPA_REG_SRC_RSRC_GRP_23_RSRC_TYPE_N_OFFSET(resource->type); 448 ylimits = group_count == 3 ? NULL : &resource->limits[3]; 449 ipa_resource_config_common(ipa, offset, &resource->limits[2], ylimits); 450 451 if (group_count < 4) 452 return; 453 454 offset = IPA_REG_SRC_RSRC_GRP_45_RSRC_TYPE_N_OFFSET(resource->type); 455 ylimits = group_count == 5 ? NULL : &resource->limits[5]; 456 ipa_resource_config_common(ipa, offset, &resource->limits[4], ylimits); 457 } 458 459 static void ipa_resource_config_dst(struct ipa *ipa, 460 const struct ipa_resource_dst *resource) 461 { 462 u32 group_count = ipa_resource_group_dst_count(ipa->version); 463 const struct ipa_resource_limits *ylimits; 464 u32 offset; 465 466 offset = IPA_REG_DST_RSRC_GRP_01_RSRC_TYPE_N_OFFSET(resource->type); 467 ylimits = group_count == 1 ? NULL : &resource->limits[1]; 468 ipa_resource_config_common(ipa, offset, &resource->limits[0], ylimits); 469 470 if (group_count < 2) 471 return; 472 473 offset = IPA_REG_DST_RSRC_GRP_23_RSRC_TYPE_N_OFFSET(resource->type); 474 ylimits = group_count == 3 ? NULL : &resource->limits[3]; 475 ipa_resource_config_common(ipa, offset, &resource->limits[2], ylimits); 476 477 if (group_count < 4) 478 return; 479 480 offset = IPA_REG_DST_RSRC_GRP_45_RSRC_TYPE_N_OFFSET(resource->type); 481 ylimits = group_count == 5 ? NULL : &resource->limits[5]; 482 ipa_resource_config_common(ipa, offset, &resource->limits[4], ylimits); 483 } 484 485 static int 486 ipa_resource_config(struct ipa *ipa, const struct ipa_resource_data *data) 487 { 488 u32 i; 489 490 if (!ipa_resource_limits_valid(ipa, data)) 491 return -EINVAL; 492 493 for (i = 0; i < data->resource_src_count; i++) 494 ipa_resource_config_src(ipa, data->resource_src); 495 496 for (i = 0; i < data->resource_dst_count; i++) 497 ipa_resource_config_dst(ipa, data->resource_dst); 498 499 return 0; 500 } 501 502 static void ipa_resource_deconfig(struct ipa *ipa) 503 { 504 /* Nothing to do */ 505 } 506 507 /** 508 * ipa_config() - Configure IPA hardware 509 * @ipa: IPA pointer 510 * @data: IPA configuration data 511 * 512 * Perform initialization requiring IPA clock to be enabled. 513 */ 514 static int ipa_config(struct ipa *ipa, const struct ipa_data *data) 515 { 516 int ret; 517 518 /* Get a clock reference to allow initialization. This reference 519 * is held after initialization completes, and won't get dropped 520 * unless/until a system suspend request arrives. 521 */ 522 ipa_clock_get(ipa); 523 524 ipa_hardware_config(ipa); 525 526 ret = ipa_endpoint_config(ipa); 527 if (ret) 528 goto err_hardware_deconfig; 529 530 ret = ipa_mem_config(ipa); 531 if (ret) 532 goto err_endpoint_deconfig; 533 534 ipa_table_config(ipa); 535 536 /* Assign resource limitation to each group */ 537 ret = ipa_resource_config(ipa, data->resource_data); 538 if (ret) 539 goto err_table_deconfig; 540 541 ret = ipa_modem_config(ipa); 542 if (ret) 543 goto err_resource_deconfig; 544 545 return 0; 546 547 err_resource_deconfig: 548 ipa_resource_deconfig(ipa); 549 err_table_deconfig: 550 ipa_table_deconfig(ipa); 551 ipa_mem_deconfig(ipa); 552 err_endpoint_deconfig: 553 ipa_endpoint_deconfig(ipa); 554 err_hardware_deconfig: 555 ipa_hardware_deconfig(ipa); 556 ipa_clock_put(ipa); 557 558 return ret; 559 } 560 561 /** 562 * ipa_deconfig() - Inverse of ipa_config() 563 * @ipa: IPA pointer 564 */ 565 static void ipa_deconfig(struct ipa *ipa) 566 { 567 ipa_modem_deconfig(ipa); 568 ipa_resource_deconfig(ipa); 569 ipa_table_deconfig(ipa); 570 ipa_mem_deconfig(ipa); 571 ipa_endpoint_deconfig(ipa); 572 ipa_hardware_deconfig(ipa); 573 ipa_clock_put(ipa); 574 } 575 576 static int ipa_firmware_load(struct device *dev) 577 { 578 const struct firmware *fw; 579 struct device_node *node; 580 struct resource res; 581 phys_addr_t phys; 582 ssize_t size; 583 void *virt; 584 int ret; 585 586 node = of_parse_phandle(dev->of_node, "memory-region", 0); 587 if (!node) { 588 dev_err(dev, "DT error getting \"memory-region\" property\n"); 589 return -EINVAL; 590 } 591 592 ret = of_address_to_resource(node, 0, &res); 593 if (ret) { 594 dev_err(dev, "error %d getting \"memory-region\" resource\n", 595 ret); 596 return ret; 597 } 598 599 ret = request_firmware(&fw, IPA_FWS_PATH, dev); 600 if (ret) { 601 dev_err(dev, "error %d requesting \"%s\"\n", ret, IPA_FWS_PATH); 602 return ret; 603 } 604 605 phys = res.start; 606 size = (size_t)resource_size(&res); 607 virt = memremap(phys, size, MEMREMAP_WC); 608 if (!virt) { 609 dev_err(dev, "unable to remap firmware memory\n"); 610 ret = -ENOMEM; 611 goto out_release_firmware; 612 } 613 614 ret = qcom_mdt_load(dev, fw, IPA_FWS_PATH, IPA_PAS_ID, 615 virt, phys, size, NULL); 616 if (ret) 617 dev_err(dev, "error %d loading \"%s\"\n", ret, IPA_FWS_PATH); 618 else if ((ret = qcom_scm_pas_auth_and_reset(IPA_PAS_ID))) 619 dev_err(dev, "error %d authenticating \"%s\"\n", ret, 620 IPA_FWS_PATH); 621 622 memunmap(virt); 623 out_release_firmware: 624 release_firmware(fw); 625 626 return ret; 627 } 628 629 static const struct of_device_id ipa_match[] = { 630 { 631 .compatible = "qcom,sdm845-ipa", 632 .data = &ipa_data_sdm845, 633 }, 634 { 635 .compatible = "qcom,sc7180-ipa", 636 .data = &ipa_data_sc7180, 637 }, 638 { }, 639 }; 640 MODULE_DEVICE_TABLE(of, ipa_match); 641 642 static phandle of_property_read_phandle(const struct device_node *np, 643 const char *name) 644 { 645 struct property *prop; 646 int len = 0; 647 648 prop = of_find_property(np, name, &len); 649 if (!prop || len != sizeof(__be32)) 650 return 0; 651 652 return be32_to_cpup(prop->value); 653 } 654 655 /* Check things that can be validated at build time. This just 656 * groups these things BUILD_BUG_ON() calls don't clutter the rest 657 * of the code. 658 * */ 659 static void ipa_validate_build(void) 660 { 661 #ifdef IPA_VALIDATE 662 /* We assume we're working on 64-bit hardware */ 663 BUILD_BUG_ON(!IS_ENABLED(CONFIG_64BIT)); 664 665 /* Code assumes the EE ID for the AP is 0 (zeroed structure field) */ 666 BUILD_BUG_ON(GSI_EE_AP != 0); 667 668 /* There's no point if we have no channels or event rings */ 669 BUILD_BUG_ON(!GSI_CHANNEL_COUNT_MAX); 670 BUILD_BUG_ON(!GSI_EVT_RING_COUNT_MAX); 671 672 /* GSI hardware design limits */ 673 BUILD_BUG_ON(GSI_CHANNEL_COUNT_MAX > 32); 674 BUILD_BUG_ON(GSI_EVT_RING_COUNT_MAX > 31); 675 676 /* The number of TREs in a transaction is limited by the channel's 677 * TLV FIFO size. A transaction structure uses 8-bit fields 678 * to represents the number of TREs it has allocated and used. 679 */ 680 BUILD_BUG_ON(GSI_TLV_MAX > U8_MAX); 681 682 /* This is used as a divisor */ 683 BUILD_BUG_ON(!IPA_AGGR_GRANULARITY); 684 685 /* Aggregation granularity value can't be 0, and must fit */ 686 BUILD_BUG_ON(!ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY)); 687 BUILD_BUG_ON(ipa_aggr_granularity_val(IPA_AGGR_GRANULARITY) > 688 field_max(AGGR_GRANULARITY)); 689 #endif /* IPA_VALIDATE */ 690 } 691 692 /** 693 * ipa_probe() - IPA platform driver probe function 694 * @pdev: Platform device pointer 695 * 696 * Return: 0 if successful, or a negative error code (possibly 697 * EPROBE_DEFER) 698 * 699 * This is the main entry point for the IPA driver. Initialization proceeds 700 * in several stages: 701 * - The "init" stage involves activities that can be initialized without 702 * access to the IPA hardware. 703 * - The "config" stage requires the IPA clock to be active so IPA registers 704 * can be accessed, but does not require the use of IPA immediate commands. 705 * - The "setup" stage uses IPA immediate commands, and so requires the GSI 706 * layer to be initialized. 707 * 708 * A Boolean Device Tree "modem-init" property determines whether GSI 709 * initialization will be performed by the AP (Trust Zone) or the modem. 710 * If the AP does GSI initialization, the setup phase is entered after 711 * this has completed successfully. Otherwise the modem initializes 712 * the GSI layer and signals it has finished by sending an SMP2P interrupt 713 * to the AP; this triggers the start if IPA setup. 714 */ 715 static int ipa_probe(struct platform_device *pdev) 716 { 717 struct device *dev = &pdev->dev; 718 const struct ipa_data *data; 719 struct ipa_clock *clock; 720 struct rproc *rproc; 721 bool modem_init; 722 struct ipa *ipa; 723 phandle ph; 724 int ret; 725 726 ipa_validate_build(); 727 728 /* If we need Trust Zone, make sure it's available */ 729 modem_init = of_property_read_bool(dev->of_node, "modem-init"); 730 if (!modem_init) 731 if (!qcom_scm_is_available()) 732 return -EPROBE_DEFER; 733 734 /* We rely on remoteproc to tell us about modem state changes */ 735 ph = of_property_read_phandle(dev->of_node, "modem-remoteproc"); 736 if (!ph) { 737 dev_err(dev, "DT missing \"modem-remoteproc\" property\n"); 738 return -EINVAL; 739 } 740 741 rproc = rproc_get_by_phandle(ph); 742 if (!rproc) 743 return -EPROBE_DEFER; 744 745 /* The clock and interconnects might not be ready when we're 746 * probed, so might return -EPROBE_DEFER. 747 */ 748 clock = ipa_clock_init(dev); 749 if (IS_ERR(clock)) { 750 ret = PTR_ERR(clock); 751 goto err_rproc_put; 752 } 753 754 /* No more EPROBE_DEFER. Get our configuration data */ 755 data = of_device_get_match_data(dev); 756 if (!data) { 757 /* This is really IPA_VALIDATE (should never happen) */ 758 dev_err(dev, "matched hardware not supported\n"); 759 ret = -ENOTSUPP; 760 goto err_clock_exit; 761 } 762 763 /* Allocate and initialize the IPA structure */ 764 ipa = kzalloc(sizeof(*ipa), GFP_KERNEL); 765 if (!ipa) { 766 ret = -ENOMEM; 767 goto err_clock_exit; 768 } 769 770 ipa->pdev = pdev; 771 dev_set_drvdata(dev, ipa); 772 ipa->modem_rproc = rproc; 773 ipa->clock = clock; 774 ipa->version = data->version; 775 776 ret = ipa_reg_init(ipa); 777 if (ret) 778 goto err_kfree_ipa; 779 780 ret = ipa_mem_init(ipa, data->mem_data); 781 if (ret) 782 goto err_reg_exit; 783 784 ret = gsi_init(&ipa->gsi, pdev, ipa->version, data->endpoint_count, 785 data->endpoint_data); 786 if (ret) 787 goto err_mem_exit; 788 789 /* Result is a non-zero mask of endpoints that support filtering */ 790 ipa->filter_map = ipa_endpoint_init(ipa, data->endpoint_count, 791 data->endpoint_data); 792 if (!ipa->filter_map) { 793 ret = -EINVAL; 794 goto err_gsi_exit; 795 } 796 797 ret = ipa_table_init(ipa); 798 if (ret) 799 goto err_endpoint_exit; 800 801 ret = ipa_modem_init(ipa, modem_init); 802 if (ret) 803 goto err_table_exit; 804 805 ret = ipa_config(ipa, data); 806 if (ret) 807 goto err_modem_exit; 808 809 dev_info(dev, "IPA driver initialized"); 810 811 /* If the modem is doing early initialization, it will trigger a 812 * call to ipa_setup() call when it has finished. In that case 813 * we're done here. 814 */ 815 if (modem_init) 816 return 0; 817 818 /* Otherwise we need to load the firmware and have Trust Zone validate 819 * and install it. If that succeeds we can proceed with setup. 820 */ 821 ret = ipa_firmware_load(dev); 822 if (ret) 823 goto err_deconfig; 824 825 ret = ipa_setup(ipa); 826 if (ret) 827 goto err_deconfig; 828 829 return 0; 830 831 err_deconfig: 832 ipa_deconfig(ipa); 833 err_modem_exit: 834 ipa_modem_exit(ipa); 835 err_table_exit: 836 ipa_table_exit(ipa); 837 err_endpoint_exit: 838 ipa_endpoint_exit(ipa); 839 err_gsi_exit: 840 gsi_exit(&ipa->gsi); 841 err_mem_exit: 842 ipa_mem_exit(ipa); 843 err_reg_exit: 844 ipa_reg_exit(ipa); 845 err_kfree_ipa: 846 kfree(ipa); 847 err_clock_exit: 848 ipa_clock_exit(clock); 849 err_rproc_put: 850 rproc_put(rproc); 851 852 return ret; 853 } 854 855 static int ipa_remove(struct platform_device *pdev) 856 { 857 struct ipa *ipa = dev_get_drvdata(&pdev->dev); 858 struct rproc *rproc = ipa->modem_rproc; 859 struct ipa_clock *clock = ipa->clock; 860 int ret; 861 862 if (ipa->setup_complete) { 863 ret = ipa_modem_stop(ipa); 864 if (ret) 865 return ret; 866 867 ipa_teardown(ipa); 868 } 869 870 ipa_deconfig(ipa); 871 ipa_modem_exit(ipa); 872 ipa_table_exit(ipa); 873 ipa_endpoint_exit(ipa); 874 gsi_exit(&ipa->gsi); 875 ipa_mem_exit(ipa); 876 ipa_reg_exit(ipa); 877 kfree(ipa); 878 ipa_clock_exit(clock); 879 rproc_put(rproc); 880 881 return 0; 882 } 883 884 /** 885 * ipa_suspend() - Power management system suspend callback 886 * @dev: IPA device structure 887 * 888 * Return: Always returns zero 889 * 890 * Called by the PM framework when a system suspend operation is invoked. 891 * Suspends endpoints and releases the clock reference held to keep 892 * the IPA clock running until this point. 893 */ 894 static int ipa_suspend(struct device *dev) 895 { 896 struct ipa *ipa = dev_get_drvdata(dev); 897 898 /* When a suspended RX endpoint has a packet ready to receive, we 899 * get an IPA SUSPEND interrupt. We trigger a system resume in 900 * that case, but only on the first such interrupt since suspend. 901 */ 902 __clear_bit(IPA_FLAG_RESUMED, ipa->flags); 903 904 ipa_endpoint_suspend(ipa); 905 906 ipa_clock_put(ipa); 907 908 return 0; 909 } 910 911 /** 912 * ipa_resume() - Power management system resume callback 913 * @dev: IPA device structure 914 * 915 * Return: Always returns 0 916 * 917 * Called by the PM framework when a system resume operation is invoked. 918 * Takes an IPA clock reference to keep the clock running until suspend, 919 * and resumes endpoints. 920 */ 921 static int ipa_resume(struct device *dev) 922 { 923 struct ipa *ipa = dev_get_drvdata(dev); 924 925 /* This clock reference will keep the IPA out of suspend 926 * until we get a power management suspend request. 927 */ 928 ipa_clock_get(ipa); 929 930 ipa_endpoint_resume(ipa); 931 932 return 0; 933 } 934 935 static const struct dev_pm_ops ipa_pm_ops = { 936 .suspend = ipa_suspend, 937 .resume = ipa_resume, 938 }; 939 940 static struct platform_driver ipa_driver = { 941 .probe = ipa_probe, 942 .remove = ipa_remove, 943 .driver = { 944 .name = "ipa", 945 .pm = &ipa_pm_ops, 946 .of_match_table = ipa_match, 947 }, 948 }; 949 950 module_platform_driver(ipa_driver); 951 952 MODULE_LICENSE("GPL v2"); 953 MODULE_DESCRIPTION("Qualcomm IP Accelerator device driver"); 954