acpi_pcc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Author: Sudeep Holla <[email protected]>
  4. * Copyright 2021 Arm Limited
  5. *
  6. * The PCC Address Space also referred as PCC Operation Region pertains to the
  7. * region of PCC subspace that succeeds the PCC signature. The PCC Operation
  8. * Region works in conjunction with the PCC Table(Platform Communications
  9. * Channel Table). PCC subspaces that are marked for use as PCC Operation
  10. * Regions must not be used as PCC subspaces for the standard ACPI features
  11. * such as CPPC, RASF, PDTT and MPST. These standard features must always use
  12. * the PCC Table instead.
  13. *
  14. * This driver sets up the PCC Address Space and installs an handler to enable
  15. * handling of PCC OpRegion in the firmware.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/acpi.h>
  20. #include <linux/completion.h>
  21. #include <linux/idr.h>
  22. #include <linux/io.h>
  23. #include <acpi/pcc.h>
  24. /*
  25. * Arbitrary retries in case the remote processor is slow to respond
  26. * to PCC commands
  27. */
  28. #define PCC_CMD_WAIT_RETRIES_NUM 500ULL
  29. struct pcc_data {
  30. struct pcc_mbox_chan *pcc_chan;
  31. void __iomem *pcc_comm_addr;
  32. struct completion done;
  33. struct mbox_client cl;
  34. struct acpi_pcc_info ctx;
  35. };
  36. static struct acpi_pcc_info pcc_ctx;
  37. static void pcc_rx_callback(struct mbox_client *cl, void *m)
  38. {
  39. struct pcc_data *data = container_of(cl, struct pcc_data, cl);
  40. complete(&data->done);
  41. }
  42. static acpi_status
  43. acpi_pcc_address_space_setup(acpi_handle region_handle, u32 function,
  44. void *handler_context, void **region_context)
  45. {
  46. struct pcc_data *data;
  47. struct acpi_pcc_info *ctx = handler_context;
  48. struct pcc_mbox_chan *pcc_chan;
  49. data = kzalloc(sizeof(*data), GFP_KERNEL);
  50. if (!data)
  51. return AE_NO_MEMORY;
  52. data->cl.rx_callback = pcc_rx_callback;
  53. data->cl.knows_txdone = true;
  54. data->ctx.length = ctx->length;
  55. data->ctx.subspace_id = ctx->subspace_id;
  56. data->ctx.internal_buffer = ctx->internal_buffer;
  57. init_completion(&data->done);
  58. data->pcc_chan = pcc_mbox_request_channel(&data->cl, ctx->subspace_id);
  59. if (IS_ERR(data->pcc_chan)) {
  60. pr_err("Failed to find PCC channel for subspace %d\n",
  61. ctx->subspace_id);
  62. kfree(data);
  63. return AE_NOT_FOUND;
  64. }
  65. pcc_chan = data->pcc_chan;
  66. data->pcc_comm_addr = acpi_os_ioremap(pcc_chan->shmem_base_addr,
  67. pcc_chan->shmem_size);
  68. if (!data->pcc_comm_addr) {
  69. pr_err("Failed to ioremap PCC comm region mem for %d\n",
  70. ctx->subspace_id);
  71. pcc_mbox_free_channel(data->pcc_chan);
  72. kfree(data);
  73. return AE_NO_MEMORY;
  74. }
  75. *region_context = data;
  76. return AE_OK;
  77. }
  78. static acpi_status
  79. acpi_pcc_address_space_handler(u32 function, acpi_physical_address addr,
  80. u32 bits, acpi_integer *value,
  81. void *handler_context, void *region_context)
  82. {
  83. int ret;
  84. struct pcc_data *data = region_context;
  85. u64 usecs_lat;
  86. reinit_completion(&data->done);
  87. /* Write to Shared Memory */
  88. memcpy_toio(data->pcc_comm_addr, (void *)value, data->ctx.length);
  89. ret = mbox_send_message(data->pcc_chan->mchan, NULL);
  90. if (ret < 0)
  91. return AE_ERROR;
  92. if (data->pcc_chan->mchan->mbox->txdone_irq) {
  93. /*
  94. * pcc_chan->latency is just a Nominal value. In reality the remote
  95. * processor could be much slower to reply. So add an arbitrary
  96. * amount of wait on top of Nominal.
  97. */
  98. usecs_lat = PCC_CMD_WAIT_RETRIES_NUM * data->pcc_chan->latency;
  99. ret = wait_for_completion_timeout(&data->done,
  100. usecs_to_jiffies(usecs_lat));
  101. if (ret == 0) {
  102. pr_err("PCC command executed timeout!\n");
  103. return AE_TIME;
  104. }
  105. }
  106. mbox_chan_txdone(data->pcc_chan->mchan, ret);
  107. memcpy_fromio(value, data->pcc_comm_addr, data->ctx.length);
  108. return AE_OK;
  109. }
  110. void __init acpi_init_pcc(void)
  111. {
  112. acpi_status status;
  113. status = acpi_install_address_space_handler(ACPI_ROOT_OBJECT,
  114. ACPI_ADR_SPACE_PLATFORM_COMM,
  115. &acpi_pcc_address_space_handler,
  116. &acpi_pcc_address_space_setup,
  117. &pcc_ctx);
  118. if (ACPI_FAILURE(status))
  119. pr_alert("OperationRegion handler could not be installed\n");
  120. }